ia64/xen-unstable

view xen/common/lib.c @ 6832:5959fae4722a

Set NE bit for VMX guest CR0. VMCS guest CR0.NE bit must
be set, else it will cause "vm-entry failed".

Signed-off-by: Chengyuan Li <chengyuan.li@intel.com>
author kaf24@firebug.cl.cam.ac.uk
date Wed Sep 14 13:37:50 2005 +0000 (2005-09-14)
parents dd668f7527cb
children b2f4823b6ff0 b35215021b32 9af349b055e5 3233e7ecfa9f
line source
2 #include <xen/ctype.h>
3 #include <xen/lib.h>
6 /* for inc/ctype.h */
7 unsigned char _ctype[] = {
8 _C,_C,_C,_C,_C,_C,_C,_C, /* 0-7 */
9 _C,_C|_S,_C|_S,_C|_S,_C|_S,_C|_S,_C,_C, /* 8-15 */
10 _C,_C,_C,_C,_C,_C,_C,_C, /* 16-23 */
11 _C,_C,_C,_C,_C,_C,_C,_C, /* 24-31 */
12 _S|_SP,_P,_P,_P,_P,_P,_P,_P, /* 32-39 */
13 _P,_P,_P,_P,_P,_P,_P,_P, /* 40-47 */
14 _D,_D,_D,_D,_D,_D,_D,_D, /* 48-55 */
15 _D,_D,_P,_P,_P,_P,_P,_P, /* 56-63 */
16 _P,_U|_X,_U|_X,_U|_X,_U|_X,_U|_X,_U|_X,_U, /* 64-71 */
17 _U,_U,_U,_U,_U,_U,_U,_U, /* 72-79 */
18 _U,_U,_U,_U,_U,_U,_U,_U, /* 80-87 */
19 _U,_U,_U,_P,_P,_P,_P,_P, /* 88-95 */
20 _P,_L|_X,_L|_X,_L|_X,_L|_X,_L|_X,_L|_X,_L, /* 96-103 */
21 _L,_L,_L,_L,_L,_L,_L,_L, /* 104-111 */
22 _L,_L,_L,_L,_L,_L,_L,_L, /* 112-119 */
23 _L,_L,_L,_P,_P,_P,_P,_C, /* 120-127 */
24 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 128-143 */
25 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 144-159 */
26 _S|_SP,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P, /* 160-175 */
27 _P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P,_P, /* 176-191 */
28 _U,_U,_U,_U,_U,_U,_U,_U,_U,_U,_U,_U,_U,_U,_U,_U, /* 192-207 */
29 _U,_U,_U,_U,_U,_U,_U,_P,_U,_U,_U,_U,_U,_U,_U,_L, /* 208-223 */
30 _L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L,_L, /* 224-239 */
31 _L,_L,_L,_L,_L,_L,_L,_P,_L,_L,_L,_L,_L,_L,_L,_L}; /* 240-255 */
34 /* a couple of 64 bit operations ported from freebsd */
36 /*-
37 * Copyright (c) 1992, 1993
38 * The Regents of the University of California. All rights reserved.
39 *
40 * This software was developed by the Computer Systems Engineering group
41 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
42 * contributed to Berkeley.
43 *
44 * Redistribution and use in source and binary forms, with or without
45 * modification, are permitted provided that the following conditions
46 * are met:
47 * 1. Redistributions of source code must retain the above copyright
48 * notice, this list of conditions and the following disclaimer.
49 * 2. Redistributions in binary form must reproduce the above copyright
50 * notice, this list of conditions and the following disclaimer in the
51 * documentation and/or other materials provided with the distribution.
52 * 3. All advertising materials mentioning features or use of this software
53 * must display the following acknowledgement:
54 * This product includes software developed by the University of
55 * California, Berkeley and its contributors.
56 * 4. Neither the name of the University nor the names of its contributors
57 * may be used to endorse or promote products derived from this software
58 * without specific prior written permission.
59 *
60 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
61 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
62 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
63 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
64 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
65 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
66 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
67 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
68 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
69 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
70 * SUCH DAMAGE.
71 *
72 * $FreeBSD: src/sys/libkern/divdi3.c,v 1.6 1999/08/28 00:46:31 peter Exp $
73 */
75 #include <asm/types.h>
77 #if BITS_PER_LONG == 32
79 /*
80 * Depending on the desired operation, we view a `long long' (aka quad_t) in
81 * one or more of the following formats.
82 */
83 union uu {
84 s64 q; /* as a (signed) quad */
85 s64 uq; /* as an unsigned quad */
86 long sl[2]; /* as two signed longs */
87 unsigned long ul[2]; /* as two unsigned longs */
88 };
89 /* XXX RN: Yuck hardcoded endianess :) */
90 #define _QUAD_HIGHWORD 1
91 #define _QUAD_LOWWORD 0
92 /*
93 * Define high and low longwords.
94 */
95 #define H _QUAD_HIGHWORD
96 #define L _QUAD_LOWWORD
98 /*
99 * Total number of bits in a quad_t and in the pieces that make it up.
100 * These are used for shifting, and also below for halfword extraction
101 * and assembly.
102 */
103 #define CHAR_BIT 8 /* number of bits in a char */
104 #define QUAD_BITS (sizeof(s64) * CHAR_BIT)
105 #define LONG_BITS (sizeof(long) * CHAR_BIT)
106 #define HALF_BITS (sizeof(long) * CHAR_BIT / 2)
108 /*
109 * Extract high and low shortwords from longword, and move low shortword of
110 * longword to upper half of long, i.e., produce the upper longword of
111 * ((quad_t)(x) << (number_of_bits_in_long/2)). (`x' must actually be u_long.)
112 *
113 * These are used in the multiply code, to split a longword into upper
114 * and lower halves, and to reassemble a product as a quad_t, shifted left
115 * (sizeof(long)*CHAR_BIT/2).
116 */
117 #define HHALF(x) ((x) >> HALF_BITS)
118 #define LHALF(x) ((x) & ((1 << HALF_BITS) - 1))
119 #define LHUP(x) ((x) << HALF_BITS)
121 /*
122 * Multiprecision divide. This algorithm is from Knuth vol. 2 (2nd ed),
123 * section 4.3.1, pp. 257--259.
124 */
125 #define B (1 << HALF_BITS) /* digit base */
127 /* Combine two `digits' to make a single two-digit number. */
128 #define COMBINE(a, b) (((u_long)(a) << HALF_BITS) | (b))
130 /* select a type for digits in base B: use unsigned short if they fit */
131 #if ULONG_MAX == 0xffffffff && USHRT_MAX >= 0xffff
132 typedef unsigned short digit;
133 #else
134 typedef u_long digit;
135 #endif
137 /*
138 * Shift p[0]..p[len] left `sh' bits, ignoring any bits that
139 * `fall out' the left (there never will be any such anyway).
140 * We may assume len >= 0. NOTE THAT THIS WRITES len+1 DIGITS.
141 */
142 static void
143 shl(register digit *p, register int len, register int sh)
144 {
145 register int i;
147 for (i = 0; i < len; i++)
148 p[i] = LHALF(p[i] << sh) | (p[i + 1] >> (HALF_BITS - sh));
149 p[i] = LHALF(p[i] << sh);
150 }
152 /*
153 * __qdivrem(u, v, rem) returns u/v and, optionally, sets *rem to u%v.
154 *
155 * We do this in base 2-sup-HALF_BITS, so that all intermediate products
156 * fit within u_long. As a consequence, the maximum length dividend and
157 * divisor are 4 `digits' in this base (they are shorter if they have
158 * leading zeros).
159 */
160 u64
161 __qdivrem(uq, vq, arq)
162 u64 uq, vq, *arq;
163 {
164 union uu tmp;
165 digit *u, *v, *q;
166 register digit v1, v2;
167 u_long qhat, rhat, t;
168 int m, n, d, j, i;
169 digit uspace[5], vspace[5], qspace[5];
171 /*
172 * Take care of special cases: divide by zero, and u < v.
173 */
174 if (vq == 0) {
175 /* divide by zero. */
176 static volatile const unsigned int zero = 0;
178 tmp.ul[H] = tmp.ul[L] = 1 / zero;
179 if (arq)
180 *arq = uq;
181 return (tmp.q);
182 }
183 if (uq < vq) {
184 if (arq)
185 *arq = uq;
186 return (0);
187 }
188 u = &uspace[0];
189 v = &vspace[0];
190 q = &qspace[0];
192 /*
193 * Break dividend and divisor into digits in base B, then
194 * count leading zeros to determine m and n. When done, we
195 * will have:
196 * u = (u[1]u[2]...u[m+n]) sub B
197 * v = (v[1]v[2]...v[n]) sub B
198 * v[1] != 0
199 * 1 < n <= 4 (if n = 1, we use a different division algorithm)
200 * m >= 0 (otherwise u < v, which we already checked)
201 * m + n = 4
202 * and thus
203 * m = 4 - n <= 2
204 */
205 tmp.uq = uq;
206 u[0] = 0;
207 u[1] = HHALF(tmp.ul[H]);
208 u[2] = LHALF(tmp.ul[H]);
209 u[3] = HHALF(tmp.ul[L]);
210 u[4] = LHALF(tmp.ul[L]);
211 tmp.uq = vq;
212 v[1] = HHALF(tmp.ul[H]);
213 v[2] = LHALF(tmp.ul[H]);
214 v[3] = HHALF(tmp.ul[L]);
215 v[4] = LHALF(tmp.ul[L]);
216 for (n = 4; v[1] == 0; v++) {
217 if (--n == 1) {
218 u_long rbj; /* r*B+u[j] (not root boy jim) */
219 digit q1, q2, q3, q4;
221 /*
222 * Change of plan, per exercise 16.
223 * r = 0;
224 * for j = 1..4:
225 * q[j] = floor((r*B + u[j]) / v),
226 * r = (r*B + u[j]) % v;
227 * We unroll this completely here.
228 */
229 t = v[2]; /* nonzero, by definition */
230 q1 = u[1] / t;
231 rbj = COMBINE(u[1] % t, u[2]);
232 q2 = rbj / t;
233 rbj = COMBINE(rbj % t, u[3]);
234 q3 = rbj / t;
235 rbj = COMBINE(rbj % t, u[4]);
236 q4 = rbj / t;
237 if (arq)
238 *arq = rbj % t;
239 tmp.ul[H] = COMBINE(q1, q2);
240 tmp.ul[L] = COMBINE(q3, q4);
241 return (tmp.q);
242 }
243 }
245 /*
246 * By adjusting q once we determine m, we can guarantee that
247 * there is a complete four-digit quotient at &qspace[1] when
248 * we finally stop.
249 */
250 for (m = 4 - n; u[1] == 0; u++)
251 m--;
252 for (i = 4 - m; --i >= 0;)
253 q[i] = 0;
254 q += 4 - m;
256 /*
257 * Here we run Program D, translated from MIX to C and acquiring
258 * a few minor changes.
259 *
260 * D1: choose multiplier 1 << d to ensure v[1] >= B/2.
261 */
262 d = 0;
263 for (t = v[1]; t < B / 2; t <<= 1)
264 d++;
265 if (d > 0) {
266 shl(&u[0], m + n, d); /* u <<= d */
267 shl(&v[1], n - 1, d); /* v <<= d */
268 }
269 /*
270 * D2: j = 0.
271 */
272 j = 0;
273 v1 = v[1]; /* for D3 -- note that v[1..n] are constant */
274 v2 = v[2]; /* for D3 */
275 do {
276 register digit uj0, uj1, uj2;
278 /*
279 * D3: Calculate qhat (\^q, in TeX notation).
280 * Let qhat = min((u[j]*B + u[j+1])/v[1], B-1), and
281 * let rhat = (u[j]*B + u[j+1]) mod v[1].
282 * While rhat < B and v[2]*qhat > rhat*B+u[j+2],
283 * decrement qhat and increase rhat correspondingly.
284 * Note that if rhat >= B, v[2]*qhat < rhat*B.
285 */
286 uj0 = u[j + 0]; /* for D3 only -- note that u[j+...] change */
287 uj1 = u[j + 1]; /* for D3 only */
288 uj2 = u[j + 2]; /* for D3 only */
289 if (uj0 == v1) {
290 qhat = B;
291 rhat = uj1;
292 goto qhat_too_big;
293 } else {
294 u_long nn = COMBINE(uj0, uj1);
295 qhat = nn / v1;
296 rhat = nn % v1;
297 }
298 while (v2 * qhat > COMBINE(rhat, uj2)) {
299 qhat_too_big:
300 qhat--;
301 if ((rhat += v1) >= B)
302 break;
303 }
304 /*
305 * D4: Multiply and subtract.
306 * The variable `t' holds any borrows across the loop.
307 * We split this up so that we do not require v[0] = 0,
308 * and to eliminate a final special case.
309 */
310 for (t = 0, i = n; i > 0; i--) {
311 t = u[i + j] - v[i] * qhat - t;
312 u[i + j] = LHALF(t);
313 t = (B - HHALF(t)) & (B - 1);
314 }
315 t = u[j] - t;
316 u[j] = LHALF(t);
317 /*
318 * D5: test remainder.
319 * There is a borrow if and only if HHALF(t) is nonzero;
320 * in that (rare) case, qhat was too large (by exactly 1).
321 * Fix it by adding v[1..n] to u[j..j+n].
322 */
323 if (HHALF(t)) {
324 qhat--;
325 for (t = 0, i = n; i > 0; i--) { /* D6: add back. */
326 t += u[i + j] + v[i];
327 u[i + j] = LHALF(t);
328 t = HHALF(t);
329 }
330 u[j] = LHALF(u[j] + t);
331 }
332 q[j] = qhat;
333 } while (++j <= m); /* D7: loop on j. */
335 /*
336 * If caller wants the remainder, we have to calculate it as
337 * u[m..m+n] >> d (this is at most n digits and thus fits in
338 * u[m+1..m+n], but we may need more source digits).
339 */
340 if (arq) {
341 if (d) {
342 for (i = m + n; i > m; --i)
343 u[i] = (u[i] >> d) |
344 LHALF(u[i - 1] << (HALF_BITS - d));
345 u[i] = 0;
346 }
347 tmp.ul[H] = COMBINE(uspace[1], uspace[2]);
348 tmp.ul[L] = COMBINE(uspace[3], uspace[4]);
349 *arq = tmp.q;
350 }
352 tmp.ul[H] = COMBINE(qspace[1], qspace[2]);
353 tmp.ul[L] = COMBINE(qspace[3], qspace[4]);
354 return (tmp.q);
355 }
357 /*
358 * Divide two signed quads.
359 * ??? if -1/2 should produce -1 on this machine, this code is wrong
360 * (Grzegorz Milos) Note for the above: -1/2 is 0. And so it should.
361 */
362 s64
363 __divdi3(s64 a, s64 b)
364 {
365 u64 ua, ub, uq;
366 int neg;
368 if (a < 0)
369 ua = -(u64)a, neg = 1;
370 else
371 ua = a, neg = 0;
372 if (b < 0)
373 ub = -(u64)b, neg ^= 1;
374 else
375 ub = b;
376 uq = __qdivrem(ua, ub, (u64 *)0);
377 return (neg ? -uq : uq);
378 }
381 /*
382 * Divide two unsigned quads.
383 */
384 u64
385 __udivdi3(a, b)
386 u64 a, b;
387 {
389 return (__qdivrem(a, b, (u64 *)0));
390 }
392 /*
393 * Remainder of unsigned quad division
394 */
395 u64 __umoddi3(u64 a, u64 b)
396 {
397 u64 rem;
398 __qdivrem(a, b, &rem);
399 return rem;
400 }
402 /*
403 * Remainder of signed quad division.
404 * The result of mod is not always equal to division
405 * remainder. The following example shows the result for all
406 * four possible cases:
407 * 11 % 5 = 1
408 * -11 % 5 = 4
409 * 11 % -5 = -4
410 * -11 % -5 = -1
411 */
412 s64 __moddi3(s64 a, s64 b)
413 {
414 u64 ua, ub, urem;
415 int neg1, neg2;
417 if (a < 0)
418 ua = -(u64)a, neg1 = 1;
419 else
420 ua = a, neg1 = 0;
422 if (b < 0)
423 ub = -(u64)b, neg2 = 1;
424 else
425 ub = b, neg2 = 0;
426 __qdivrem(ua, ub, &urem);
428 /* There 4 different cases: */
429 if (neg1) {
430 if (neg2)
431 return -urem;
432 else
433 return ub - urem;
434 } else {
435 if (neg2)
436 return -ub + urem;
437 else
438 return urem;
439 }
440 }
442 #endif /* BITS_PER_LONG == 32 */
444 unsigned long long parse_size_and_unit(char *s)
445 {
446 unsigned long long ret = simple_strtoull(s, &s, 0);
448 switch (*s) {
449 case 'G': case 'g':
450 ret <<= 10;
451 case 'M': case 'm':
452 ret <<= 10;
453 case 'K': case 'k': default:
454 ret <<= 10;
455 case 'B': case 'b':
456 break;
457 }
459 return ret;
460 }
462 /*
463 * Local variables:
464 * mode: C
465 * c-set-style: "BSD"
466 * c-basic-offset: 8
467 * tab-width: 8
468 * indent-tabs-mode: t
469 * End:
470 */