direct-io.hg

view tools/firmware/vmxassist/util.c @ 7477:5a7baecb1c70

Fix an issue for passing arguement from control panel to deivce model
for some arguemnt like 'localtime', 'isa', device model need an argument
"-localtime", instead of "-localtime 1"
Signed-off-by: Xiaofeng Ling <xiaofeng.ling@intel.com>
author kaf24@firebug.cl.cam.ac.uk
date Sun Oct 23 16:51:47 2005 +0100 (2005-10-23)
parents 332eb7887996
children f9e912842006
line source
1 /*
2 * util.c: Commonly used utility functions.
3 *
4 * Leendert van Doorn, leendert@watson.ibm.com
5 * Copyright (c) 2005, International Business Machines Corporation.
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms and conditions of the GNU General Public License,
9 * version 2, as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
18 * Place - Suite 330, Boston, MA 02111-1307 USA.
19 */
20 #include <stdarg.h>
21 #include <vm86.h>
23 #include "util.h"
24 #include "machine.h"
26 #define isdigit(c) ((c) >= '0' && (c) <= '9')
27 #define min(a, b) ((a) < (b) ? (a) : (b))
29 static void putchar(int);
30 static char *printnum(char *, unsigned long, int);
31 static void _doprint(void (*)(int), char const *, va_list);
34 void
35 dump_regs(struct regs *regs)
36 {
37 printf("eax %8x ecx %8x edx %8x ebx %8x\n",
38 regs->eax, regs->ecx, regs->edx, regs->ebx);
39 printf("esp %8x ebp %8x esi %8x edi %8x\n",
40 regs->esp, regs->ebp, regs->esi, regs->edi);
41 printf("eip %8x eflags %8x cs %8x ds %8x\n",
42 regs->eip, regs->eflags, regs->cs, regs->ds);
43 printf("es %8x fs %8x uss %8x uesp %8x\n",
44 regs->es, regs->fs, regs->uss, regs->uesp);
45 printf("ves %8x vds %8x vfs %8x vgs %8x\n",
46 regs->ves, regs->vds, regs->vfs, regs->vgs);
47 if (regs->trapno != -1 || regs->errno != -1)
48 printf("trapno %8x errno %8x\n", regs->trapno, regs->errno);
50 printf("cr0 %8lx cr2 %8x cr3 %8lx cr4 %8lx\n",
51 oldctx.cr0, get_cr2(), oldctx.cr3, oldctx.cr4);
52 }
54 #ifdef DEBUG
55 void
56 hexdump(unsigned char *data, int sz)
57 {
58 unsigned char *d;
59 int i;
61 for (d = data; sz > 0; d += 16, sz -= 16) {
62 int n = sz > 16 ? 16 : sz;
64 printf("%08x: ", (unsigned)d);
65 for (i = 0; i < n; i++)
66 printf("%02x%c", d[i], i == 7 ? '-' : ' ');
67 for (; i < 16; i++)
68 printf(" %c", i == 7 ? '-' : ' ');
69 printf(" ");
70 for (i = 0; i < n; i++)
71 printf("%c", d[i] >= ' ' && d[i] <= '~' ? d[i] : '.');
72 printf("\n");
73 }
74 }
76 void
77 print_e820_map(struct e820entry *map, int entries)
78 {
79 struct e820entry *m;
81 if (entries > 32)
82 entries = 32;
84 for (m = map; m < &map[entries]; m++) {
85 printf("%08lx%08lx - %08lx%08lx ",
86 (unsigned long) (m->addr >> 32),
87 (unsigned long) (m->addr),
88 (unsigned long) ((m->addr+m->size) >> 32),
89 (unsigned long) ((m->addr+m->size)));
91 switch (m->type) {
92 case E820_RAM:
93 printf("(RAM)\n"); break;
94 case E820_RESERVED:
95 printf("(Reserved)\n"); break;
96 case E820_ACPI:
97 printf("(ACPI Data)\n"); break;
98 case E820_NVS:
99 printf("(ACPI NVS)\n"); break;
100 default:
101 printf("(Type %ld)\n", m->type); break;
102 }
103 }
104 }
106 void
107 dump_dtr(unsigned long base, unsigned long limit)
108 {
109 unsigned long long entry;
110 int i;
112 for (i = 0; i < limit; i += 8) {
113 entry = ((unsigned long long *) base)[i >> 3];
114 printf("[0x%x] = 0x%08x%08x\n", i,
115 (unsigned)(entry >> 32), (unsigned)(entry));
116 }
117 }
119 void
120 dump_vmx_context(struct vmx_assist_context *c)
121 {
122 printf("eip 0x%lx, esp 0x%lx, eflags 0x%lx\n",
123 c->eip, c->esp, c->eflags);
125 printf("cr0 0x%lx, cr3 0x%lx, cr4 0x%lx\n", c->cr0, c->cr3, c->cr4);
127 printf("idtr: limit 0x%lx, base 0x%lx\n",
128 c->idtr_limit, c->idtr_base);
130 printf("gdtr: limit 0x%lx, base 0x%lx\n",
131 c->gdtr_limit, c->gdtr_base);
133 printf("cs: sel 0x%lx, limit 0x%lx, base 0x%lx\n",
134 c->cs_sel, c->cs_limit, c->cs_base);
135 printf("\ttype %d, s %d, dpl %d, p %d, avl %d, ops %d, g %d, nul %d\n",
136 c->cs_arbytes.fields.seg_type,
137 c->cs_arbytes.fields.s,
138 c->cs_arbytes.fields.dpl,
139 c->cs_arbytes.fields.p,
140 c->cs_arbytes.fields.avl,
141 c->cs_arbytes.fields.default_ops_size,
142 c->cs_arbytes.fields.g,
143 c->cs_arbytes.fields.null_bit);
145 printf("ds: sel 0x%lx, limit 0x%lx, base 0x%lx\n",
146 c->ds_sel, c->ds_limit, c->ds_base);
147 printf("\ttype %d, s %d, dpl %d, p %d, avl %d, ops %d, g %d, nul %d\n",
148 c->ds_arbytes.fields.seg_type,
149 c->ds_arbytes.fields.s,
150 c->ds_arbytes.fields.dpl,
151 c->ds_arbytes.fields.p,
152 c->ds_arbytes.fields.avl,
153 c->ds_arbytes.fields.default_ops_size,
154 c->ds_arbytes.fields.g,
155 c->ds_arbytes.fields.null_bit);
157 printf("es: sel 0x%lx, limit 0x%lx, base 0x%lx\n",
158 c->es_sel, c->es_limit, c->es_base);
159 printf("\ttype %d, s %d, dpl %d, p %d, avl %d, ops %d, g %d, nul %d\n",
160 c->es_arbytes.fields.seg_type,
161 c->es_arbytes.fields.s,
162 c->es_arbytes.fields.dpl,
163 c->es_arbytes.fields.p,
164 c->es_arbytes.fields.avl,
165 c->es_arbytes.fields.default_ops_size,
166 c->es_arbytes.fields.g,
167 c->es_arbytes.fields.null_bit);
169 printf("ss: sel 0x%lx, limit 0x%lx, base 0x%lx\n",
170 c->ss_sel, c->ss_limit, c->ss_base);
171 printf("\ttype %d, s %d, dpl %d, p %d, avl %d, ops %d, g %d, nul %d\n",
172 c->ss_arbytes.fields.seg_type,
173 c->ss_arbytes.fields.s,
174 c->ss_arbytes.fields.dpl,
175 c->ss_arbytes.fields.p,
176 c->ss_arbytes.fields.avl,
177 c->ss_arbytes.fields.default_ops_size,
178 c->ss_arbytes.fields.g,
179 c->ss_arbytes.fields.null_bit);
181 printf("fs: sel 0x%lx, limit 0x%lx, base 0x%lx\n",
182 c->fs_sel, c->fs_limit, c->fs_base);
183 printf("\ttype %d, s %d, dpl %d, p %d, avl %d, ops %d, g %d, nul %d\n",
184 c->fs_arbytes.fields.seg_type,
185 c->fs_arbytes.fields.s,
186 c->fs_arbytes.fields.dpl,
187 c->fs_arbytes.fields.p,
188 c->fs_arbytes.fields.avl,
189 c->fs_arbytes.fields.default_ops_size,
190 c->fs_arbytes.fields.g,
191 c->fs_arbytes.fields.null_bit);
193 printf("gs: sel 0x%lx, limit 0x%lx, base 0x%lx\n",
194 c->gs_sel, c->gs_limit, c->gs_base);
195 printf("\ttype %d, s %d, dpl %d, p %d, avl %d, ops %d, g %d, nul %d\n",
196 c->gs_arbytes.fields.seg_type,
197 c->gs_arbytes.fields.s,
198 c->gs_arbytes.fields.dpl,
199 c->gs_arbytes.fields.p,
200 c->gs_arbytes.fields.avl,
201 c->gs_arbytes.fields.default_ops_size,
202 c->gs_arbytes.fields.g,
203 c->gs_arbytes.fields.null_bit);
205 printf("tr: sel 0x%lx, limit 0x%lx, base 0x%lx\n",
206 c->tr_sel, c->tr_limit, c->tr_base);
207 printf("\ttype %d, s %d, dpl %d, p %d, avl %d, ops %d, g %d, nul %d\n",
208 c->tr_arbytes.fields.seg_type,
209 c->tr_arbytes.fields.s,
210 c->tr_arbytes.fields.dpl,
211 c->tr_arbytes.fields.p,
212 c->tr_arbytes.fields.avl,
213 c->tr_arbytes.fields.default_ops_size,
214 c->tr_arbytes.fields.g,
215 c->tr_arbytes.fields.null_bit);
217 printf("ldtr: sel 0x%lx, limit 0x%lx, base 0x%lx\n",
218 c->ldtr_sel, c->ldtr_limit, c->ldtr_base);
219 printf("\ttype %d, s %d, dpl %d, p %d, avl %d, ops %d, g %d, nul %d\n",
220 c->ldtr_arbytes.fields.seg_type,
221 c->ldtr_arbytes.fields.s,
222 c->ldtr_arbytes.fields.dpl,
223 c->ldtr_arbytes.fields.p,
224 c->ldtr_arbytes.fields.avl,
225 c->ldtr_arbytes.fields.default_ops_size,
226 c->ldtr_arbytes.fields.g,
227 c->ldtr_arbytes.fields.null_bit);
229 printf("GDTR <0x%lx,0x%lx>:\n", c->gdtr_base, c->gdtr_limit);
230 dump_dtr(c->gdtr_base, c->gdtr_limit);
231 }
232 #endif /* DEBUG */
234 /*
235 * Lightweight printf that doesn't drag in everything under the sun.
236 */
237 int
238 printf(const char *fmt, ...)
239 {
240 va_list ap;
242 va_start(ap, fmt);
243 _doprint(putchar, fmt, ap);
244 va_end(ap);
245 return 0; /* for gcc compat */
246 }
248 int
249 vprintf(const char *fmt, va_list ap)
250 {
251 _doprint(putchar, fmt, ap);
252 return 0; /* for gcc compat */
253 }
255 void
256 panic(const char *fmt, ...)
257 {
258 va_list ap;
260 va_start(ap, fmt);
261 _doprint(putchar, fmt, ap);
262 putchar('\n');
263 va_end(ap);
264 halt();
265 }
267 unsigned
268 strlen(const char *s)
269 {
270 const char *q = s;
272 while (*s++)
273 /* void */;
274 return s - q - 1;
275 }
277 static void
278 putchar(int ch)
279 {
280 outb(0xE9, ch);
281 }
283 /*
284 * A stripped down version of doprint,
285 * but still powerful enough for most tasks.
286 */
287 static void
288 _doprint(void (*put)(int), char const *fmt, va_list ap)
289 {
290 register char *str, c;
291 int lflag, zflag, nflag;
292 char buffer[17];
293 unsigned value;
294 int i, slen, pad;
296 for ( ; *fmt != '\0'; fmt++) {
297 pad = zflag = nflag = lflag = 0;
298 if (*fmt == '%') {
299 c = *++fmt;
300 if (c == '-' || isdigit(c)) {
301 if (c == '-') {
302 nflag = 1;
303 c = *++fmt;
304 }
305 zflag = c == '0';
306 for (pad = 0; isdigit(c); c = *++fmt)
307 pad = (pad * 10) + c - '0';
308 }
309 if (c == 'l') { /* long extension */
310 lflag = 1;
311 c = *++fmt;
312 }
313 if (c == 'd' || c == 'u' || c == 'o' || c == 'x') {
314 if (lflag)
315 value = va_arg(ap, unsigned);
316 else
317 value = (unsigned) va_arg(ap, unsigned int);
318 str = buffer;
319 printnum(str, value,
320 c == 'o' ? 8 : (c == 'x' ? 16 : 10));
321 goto printn;
322 } else if (c == 'O' || c == 'D' || c == 'X') {
323 value = va_arg(ap, unsigned);
324 str = buffer;
325 printnum(str, value,
326 c == 'O' ? 8 : (c == 'X' ? 16 : 10));
327 printn:
328 slen = strlen(str);
329 for (i = pad - slen; i > 0; i--)
330 put(zflag ? '0' : ' ');
331 while (*str) put(*str++);
332 } else if (c == 's') {
333 str = va_arg(ap, char *);
334 slen = strlen(str);
335 if (nflag == 0)
336 for (i = pad - slen; i > 0; i--) put(' ');
337 while (*str) put(*str++);
338 if (nflag)
339 for (i = pad - slen; i > 0; i--) put(' ');
340 } else if (c == 'c')
341 put(va_arg(ap, int));
342 else
343 put(*fmt);
344 } else
345 put(*fmt);
346 }
347 }
349 static char *
350 printnum(char *p, unsigned long num, int base)
351 {
352 unsigned long n;
354 if ((n = num/base) > 0)
355 p = printnum(p, n, base);
356 *p++ = "0123456789ABCDEF"[(int)(num % base)];
357 *p = '\0';
358 return p;
359 }
361 void *
362 memset(void *s, int c, unsigned n)
363 {
364 int t0, t1;
366 __asm__ __volatile__ ("cld; rep; stosb"
367 : "=&c" (t0), "=&D" (t1)
368 : "a" (c), "1" (s), "0" (n)
369 : "memory");
370 return s;
371 }
373 void *
374 memcpy(void *dest, const void *src, unsigned n)
375 {
376 int t0, t1, t2;
378 __asm__ __volatile__(
379 "cld\n"
380 "rep; movsl\n"
381 "testb $2,%b4\n"
382 "je 1f\n"
383 "movsw\n"
384 "1: testb $1,%b4\n"
385 "je 2f\n"
386 "movsb\n"
387 "2:"
388 : "=&c" (t0), "=&D" (t1), "=&S" (t2)
389 : "0" (n/4), "q" (n), "1" ((long) dest), "2" ((long) src)
390 : "memory"
391 );
392 return dest;
393 }