root / osdep.c @ d3c61721
History | View | Annotate | Download (10.9 kB)
1 | ea88812f | bellard | /*
|
---|---|---|---|
2 | ea88812f | bellard | * QEMU low level functions
|
3 | ea88812f | bellard | *
|
4 | ea88812f | bellard | * Copyright (c) 2003 Fabrice Bellard
|
5 | ea88812f | bellard | *
|
6 | ea88812f | bellard | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 | ea88812f | bellard | * of this software and associated documentation files (the "Software"), to deal
|
8 | ea88812f | bellard | * in the Software without restriction, including without limitation the rights
|
9 | ea88812f | bellard | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 | ea88812f | bellard | * copies of the Software, and to permit persons to whom the Software is
|
11 | ea88812f | bellard | * furnished to do so, subject to the following conditions:
|
12 | ea88812f | bellard | *
|
13 | ea88812f | bellard | * The above copyright notice and this permission notice shall be included in
|
14 | ea88812f | bellard | * all copies or substantial portions of the Software.
|
15 | ea88812f | bellard | *
|
16 | ea88812f | bellard | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 | ea88812f | bellard | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 | ea88812f | bellard | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
19 | ea88812f | bellard | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 | ea88812f | bellard | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 | ea88812f | bellard | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22 | ea88812f | bellard | * THE SOFTWARE.
|
23 | ea88812f | bellard | */
|
24 | ea88812f | bellard | #include <stdlib.h> |
25 | ea88812f | bellard | #include <stdio.h> |
26 | ea88812f | bellard | #include <stdarg.h> |
27 | ea88812f | bellard | #include <string.h> |
28 | ea88812f | bellard | #include <errno.h> |
29 | ea88812f | bellard | #include <unistd.h> |
30 | ea88812f | bellard | |
31 | ea88812f | bellard | #include "cpu.h" |
32 | ea88812f | bellard | |
33 | ea88812f | bellard | #if defined(__i386__) && !defined(CONFIG_SOFTMMU) && !defined(CONFIG_USER_ONLY)
|
34 | ea88812f | bellard | |
35 | 67b915a5 | bellard | #include <sys/mman.h> |
36 | 67b915a5 | bellard | #include <sys/ipc.h> |
37 | 67b915a5 | bellard | |
38 | ea88812f | bellard | /* When not using soft mmu, libc independant functions are needed for
|
39 | ea88812f | bellard | the CPU core because it needs to use alternates stacks and
|
40 | ea88812f | bellard | libc/thread incompatibles settings */
|
41 | ea88812f | bellard | |
42 | ea88812f | bellard | #include <linux/unistd.h> |
43 | ea88812f | bellard | |
44 | ea88812f | bellard | #define QEMU_SYSCALL0(name) \
|
45 | ea88812f | bellard | { \ |
46 | ea88812f | bellard | long __res; \
|
47 | ea88812f | bellard | __asm__ volatile ("int $0x80" \ |
48 | ea88812f | bellard | : "=a" (__res) \
|
49 | ea88812f | bellard | : "0" (__NR_##name)); \ |
50 | ea88812f | bellard | return __res; \
|
51 | ea88812f | bellard | } |
52 | ea88812f | bellard | |
53 | ea88812f | bellard | #define QEMU_SYSCALL1(name,arg1) \
|
54 | ea88812f | bellard | { \ |
55 | ea88812f | bellard | long __res; \
|
56 | ea88812f | bellard | __asm__ volatile ("int $0x80" \ |
57 | ea88812f | bellard | : "=a" (__res) \
|
58 | ea88812f | bellard | : "0" (__NR_##name),"b" ((long)(arg1))); \ |
59 | ea88812f | bellard | return __res; \
|
60 | ea88812f | bellard | } |
61 | ea88812f | bellard | |
62 | ea88812f | bellard | #define QEMU_SYSCALL2(name,arg1,arg2) \
|
63 | ea88812f | bellard | { \ |
64 | ea88812f | bellard | long __res; \
|
65 | ea88812f | bellard | __asm__ volatile ("int $0x80" \ |
66 | ea88812f | bellard | : "=a" (__res) \
|
67 | ea88812f | bellard | : "0" (__NR_##name),"b" ((long)(arg1)),"c" ((long)(arg2))); \ |
68 | ea88812f | bellard | return __res; \
|
69 | ea88812f | bellard | } |
70 | ea88812f | bellard | |
71 | ea88812f | bellard | #define QEMU_SYSCALL3(name,arg1,arg2,arg3) \
|
72 | ea88812f | bellard | { \ |
73 | ea88812f | bellard | long __res; \
|
74 | ea88812f | bellard | __asm__ volatile ("int $0x80" \ |
75 | ea88812f | bellard | : "=a" (__res) \
|
76 | ea88812f | bellard | : "0" (__NR_##name),"b" ((long)(arg1)),"c" ((long)(arg2)), \ |
77 | ea88812f | bellard | "d" ((long)(arg3))); \ |
78 | ea88812f | bellard | return __res; \
|
79 | ea88812f | bellard | } |
80 | ea88812f | bellard | |
81 | ea88812f | bellard | #define QEMU_SYSCALL4(name,arg1,arg2,arg3,arg4) \
|
82 | ea88812f | bellard | { \ |
83 | ea88812f | bellard | long __res; \
|
84 | ea88812f | bellard | __asm__ volatile ("int $0x80" \ |
85 | ea88812f | bellard | : "=a" (__res) \
|
86 | ea88812f | bellard | : "0" (__NR_##name),"b" ((long)(arg1)),"c" ((long)(arg2)), \ |
87 | ea88812f | bellard | "d" ((long)(arg3)),"S" ((long)(arg4))); \ |
88 | ea88812f | bellard | return __res; \
|
89 | ea88812f | bellard | } |
90 | ea88812f | bellard | |
91 | ea88812f | bellard | #define QEMU_SYSCALL5(name,arg1,arg2,arg3,arg4,arg5) \
|
92 | ea88812f | bellard | { \ |
93 | ea88812f | bellard | long __res; \
|
94 | ea88812f | bellard | __asm__ volatile ("int $0x80" \ |
95 | ea88812f | bellard | : "=a" (__res) \
|
96 | ea88812f | bellard | : "0" (__NR_##name),"b" ((long)(arg1)),"c" ((long)(arg2)), \ |
97 | ea88812f | bellard | "d" ((long)(arg3)),"S" ((long)(arg4)),"D" ((long)(arg5))); \ |
98 | ea88812f | bellard | return __res; \
|
99 | ea88812f | bellard | } |
100 | ea88812f | bellard | |
101 | ea88812f | bellard | #define QEMU_SYSCALL6(name,arg1,arg2,arg3,arg4,arg5,arg6) \
|
102 | ea88812f | bellard | { \ |
103 | ea88812f | bellard | long __res; \
|
104 | ea88812f | bellard | __asm__ volatile ("push %%ebp ; movl %%eax,%%ebp ; movl %1,%%eax ; int $0x80 ; pop %%ebp" \ |
105 | ea88812f | bellard | : "=a" (__res) \
|
106 | ea88812f | bellard | : "i" (__NR_##name),"b" ((long)(arg1)),"c" ((long)(arg2)), \ |
107 | ea88812f | bellard | "d" ((long)(arg3)),"S" ((long)(arg4)),"D" ((long)(arg5)), \ |
108 | ea88812f | bellard | "0" ((long)(arg6))); \ |
109 | ea88812f | bellard | return __res; \
|
110 | ea88812f | bellard | } |
111 | ea88812f | bellard | |
112 | ea88812f | bellard | int qemu_write(int fd, const void *buf, size_t n) |
113 | ea88812f | bellard | { |
114 | ea88812f | bellard | QEMU_SYSCALL3(write, fd, buf, n); |
115 | ea88812f | bellard | } |
116 | ea88812f | bellard | |
117 | ea88812f | bellard | |
118 | ea88812f | bellard | |
119 | ea88812f | bellard | /****************************************************************/
|
120 | ea88812f | bellard | /* shmat replacement */
|
121 | ea88812f | bellard | |
122 | ea88812f | bellard | int qemu_ipc(int call, unsigned long first, |
123 | ea88812f | bellard | unsigned long second, unsigned long third, |
124 | ea88812f | bellard | void *ptr, unsigned long fifth) |
125 | ea88812f | bellard | { |
126 | ea88812f | bellard | QEMU_SYSCALL6(ipc, call, first, second, third, ptr, fifth); |
127 | ea88812f | bellard | } |
128 | ea88812f | bellard | |
129 | ea88812f | bellard | #define SHMAT 21 |
130 | ea88812f | bellard | |
131 | ea88812f | bellard | /* we must define shmat so that a specific address will be used when
|
132 | ea88812f | bellard | mapping the X11 ximage */
|
133 | ea88812f | bellard | void *shmat(int shmid, const void *shmaddr, int shmflg) |
134 | ea88812f | bellard | { |
135 | ea88812f | bellard | void *ptr;
|
136 | ea88812f | bellard | int ret;
|
137 | ea88812f | bellard | /* we give an address in the right memory area */
|
138 | ea88812f | bellard | if (!shmaddr)
|
139 | ea88812f | bellard | shmaddr = get_mmap_addr(8192 * 1024); |
140 | ea88812f | bellard | ret = qemu_ipc(SHMAT, shmid, shmflg, (unsigned long)&ptr, (void *)shmaddr, 0); |
141 | ea88812f | bellard | if (ret < 0) |
142 | ea88812f | bellard | return NULL; |
143 | ea88812f | bellard | return ptr;
|
144 | ea88812f | bellard | } |
145 | ea88812f | bellard | |
146 | ea88812f | bellard | /****************************************************************/
|
147 | d2bfb39a | bellard | /* sigaction bypassing the threads */
|
148 | d2bfb39a | bellard | |
149 | d2bfb39a | bellard | static int kernel_sigaction(int signum, const struct qemu_sigaction *act, |
150 | d2bfb39a | bellard | struct qemu_sigaction *oldact,
|
151 | d2bfb39a | bellard | int sigsetsize)
|
152 | d2bfb39a | bellard | { |
153 | d2bfb39a | bellard | QEMU_SYSCALL4(rt_sigaction, signum, act, oldact, sigsetsize); |
154 | d2bfb39a | bellard | } |
155 | d2bfb39a | bellard | |
156 | d2bfb39a | bellard | int qemu_sigaction(int signum, const struct qemu_sigaction *act, |
157 | d2bfb39a | bellard | struct qemu_sigaction *oldact)
|
158 | d2bfb39a | bellard | { |
159 | d2bfb39a | bellard | return kernel_sigaction(signum, act, oldact, 8); |
160 | d2bfb39a | bellard | } |
161 | d2bfb39a | bellard | |
162 | d2bfb39a | bellard | /****************************************************************/
|
163 | ea88812f | bellard | /* memory allocation */
|
164 | ea88812f | bellard | |
165 | ea88812f | bellard | //#define DEBUG_MALLOC
|
166 | ea88812f | bellard | |
167 | ea88812f | bellard | #define MALLOC_BASE 0xab000000 |
168 | ea88812f | bellard | #define PHYS_RAM_BASE 0xac000000 |
169 | ea88812f | bellard | |
170 | ea88812f | bellard | #define MALLOC_ALIGN 16 |
171 | ea88812f | bellard | #define BLOCK_HEADER_SIZE 16 |
172 | ea88812f | bellard | |
173 | ea88812f | bellard | typedef struct MemoryBlock { |
174 | ea88812f | bellard | struct MemoryBlock *next;
|
175 | ea88812f | bellard | unsigned long size; /* size of block, including header */ |
176 | ea88812f | bellard | } MemoryBlock; |
177 | ea88812f | bellard | |
178 | ea88812f | bellard | static MemoryBlock *first_free_block;
|
179 | ea88812f | bellard | static unsigned long malloc_addr = MALLOC_BASE; |
180 | ea88812f | bellard | |
181 | ea88812f | bellard | static void *malloc_get_space(size_t size) |
182 | ea88812f | bellard | { |
183 | ea88812f | bellard | void *ptr;
|
184 | ea88812f | bellard | size = TARGET_PAGE_ALIGN(size); |
185 | ea88812f | bellard | ptr = mmap((void *)malloc_addr, size,
|
186 | ea88812f | bellard | PROT_WRITE | PROT_READ, |
187 | ea88812f | bellard | MAP_PRIVATE | MAP_FIXED | MAP_ANON, -1, 0); |
188 | ea88812f | bellard | if (ptr == MAP_FAILED)
|
189 | ea88812f | bellard | return NULL; |
190 | ea88812f | bellard | malloc_addr += size; |
191 | ea88812f | bellard | return ptr;
|
192 | ea88812f | bellard | } |
193 | ea88812f | bellard | |
194 | ea88812f | bellard | void *qemu_malloc(size_t size)
|
195 | ea88812f | bellard | { |
196 | ea88812f | bellard | MemoryBlock *mb, *mb1, **pmb; |
197 | ea88812f | bellard | void *ptr;
|
198 | ea88812f | bellard | size_t size1, area_size; |
199 | ea88812f | bellard | |
200 | ea88812f | bellard | if (size == 0) |
201 | ea88812f | bellard | return NULL; |
202 | ea88812f | bellard | |
203 | ea88812f | bellard | size = (size + BLOCK_HEADER_SIZE + MALLOC_ALIGN - 1) & ~(MALLOC_ALIGN - 1); |
204 | ea88812f | bellard | pmb = &first_free_block; |
205 | ea88812f | bellard | for(;;) {
|
206 | ea88812f | bellard | mb = *pmb; |
207 | ea88812f | bellard | if (mb == NULL) |
208 | ea88812f | bellard | break;
|
209 | ea88812f | bellard | if (size <= mb->size)
|
210 | ea88812f | bellard | goto found;
|
211 | ea88812f | bellard | pmb = &mb->next; |
212 | ea88812f | bellard | } |
213 | ea88812f | bellard | /* no big enough blocks found: get new space */
|
214 | ea88812f | bellard | area_size = TARGET_PAGE_ALIGN(size); |
215 | ea88812f | bellard | mb = malloc_get_space(area_size); |
216 | ea88812f | bellard | if (!mb)
|
217 | ea88812f | bellard | return NULL; |
218 | ea88812f | bellard | size1 = area_size - size; |
219 | ea88812f | bellard | if (size1 > 0) { |
220 | ea88812f | bellard | /* create a new free block */
|
221 | ea88812f | bellard | mb1 = (MemoryBlock *)((uint8_t *)mb + size); |
222 | ea88812f | bellard | mb1->next = NULL;
|
223 | ea88812f | bellard | mb1->size = size1; |
224 | ea88812f | bellard | *pmb = mb1; |
225 | ea88812f | bellard | } |
226 | ea88812f | bellard | goto the_end;
|
227 | ea88812f | bellard | found:
|
228 | ea88812f | bellard | /* a free block was found: use it */
|
229 | ea88812f | bellard | size1 = mb->size - size; |
230 | ea88812f | bellard | if (size1 > 0) { |
231 | ea88812f | bellard | /* create a new free block */
|
232 | ea88812f | bellard | mb1 = (MemoryBlock *)((uint8_t *)mb + size); |
233 | ea88812f | bellard | mb1->next = mb->next; |
234 | ea88812f | bellard | mb1->size = size1; |
235 | ea88812f | bellard | *pmb = mb1; |
236 | ea88812f | bellard | } else {
|
237 | ea88812f | bellard | /* suppress the first block */
|
238 | ea88812f | bellard | *pmb = mb->next; |
239 | ea88812f | bellard | } |
240 | ea88812f | bellard | the_end:
|
241 | ea88812f | bellard | mb->size = size; |
242 | ea88812f | bellard | mb->next = NULL;
|
243 | ea88812f | bellard | ptr = ((uint8_t *)mb + BLOCK_HEADER_SIZE); |
244 | ea88812f | bellard | #ifdef DEBUG_MALLOC
|
245 | ea88812f | bellard | qemu_printf("malloc: size=0x%x ptr=0x%lx\n", size, (unsigned long)ptr); |
246 | ea88812f | bellard | #endif
|
247 | ea88812f | bellard | return ptr;
|
248 | ea88812f | bellard | } |
249 | ea88812f | bellard | |
250 | ea88812f | bellard | void qemu_free(void *ptr) |
251 | ea88812f | bellard | { |
252 | ea88812f | bellard | MemoryBlock *mb; |
253 | ea88812f | bellard | |
254 | 57c30724 | bellard | if (!ptr)
|
255 | 57c30724 | bellard | return;
|
256 | ea88812f | bellard | mb = (MemoryBlock *)((uint8_t *)ptr - BLOCK_HEADER_SIZE); |
257 | ea88812f | bellard | mb->next = first_free_block; |
258 | ea88812f | bellard | first_free_block = mb; |
259 | ea88812f | bellard | } |
260 | ea88812f | bellard | |
261 | ea88812f | bellard | /****************************************************************/
|
262 | ea88812f | bellard | /* virtual memory allocation */
|
263 | ea88812f | bellard | |
264 | ea88812f | bellard | unsigned long mmap_addr = PHYS_RAM_BASE; |
265 | ea88812f | bellard | |
266 | ea88812f | bellard | void *get_mmap_addr(unsigned long size) |
267 | ea88812f | bellard | { |
268 | ea88812f | bellard | unsigned long addr; |
269 | ea88812f | bellard | addr = mmap_addr; |
270 | ea88812f | bellard | mmap_addr += ((size + 4095) & ~4095) + 4096; |
271 | ea88812f | bellard | return (void *)addr; |
272 | ea88812f | bellard | } |
273 | ea88812f | bellard | |
274 | ea88812f | bellard | #else
|
275 | ea88812f | bellard | |
276 | ea88812f | bellard | int qemu_write(int fd, const void *buf, size_t n) |
277 | ea88812f | bellard | { |
278 | ea88812f | bellard | int ret;
|
279 | ea88812f | bellard | ret = write(fd, buf, n); |
280 | ea88812f | bellard | if (ret < 0) |
281 | ea88812f | bellard | return -errno;
|
282 | ea88812f | bellard | else
|
283 | ea88812f | bellard | return ret;
|
284 | ea88812f | bellard | } |
285 | ea88812f | bellard | |
286 | ea88812f | bellard | void *get_mmap_addr(unsigned long size) |
287 | ea88812f | bellard | { |
288 | ea88812f | bellard | return NULL; |
289 | ea88812f | bellard | } |
290 | ea88812f | bellard | |
291 | ea88812f | bellard | void qemu_free(void *ptr) |
292 | ea88812f | bellard | { |
293 | ea88812f | bellard | free(ptr); |
294 | ea88812f | bellard | } |
295 | ea88812f | bellard | |
296 | ea88812f | bellard | void *qemu_malloc(size_t size)
|
297 | ea88812f | bellard | { |
298 | ea88812f | bellard | return malloc(size);
|
299 | ea88812f | bellard | } |
300 | ea88812f | bellard | |
301 | ea88812f | bellard | #endif
|
302 | ea88812f | bellard | |
303 | 07d89866 | bellard | void *qemu_mallocz(size_t size)
|
304 | 07d89866 | bellard | { |
305 | 07d89866 | bellard | void *ptr;
|
306 | 07d89866 | bellard | ptr = qemu_malloc(size); |
307 | 07d89866 | bellard | if (!ptr)
|
308 | 07d89866 | bellard | return NULL; |
309 | 07d89866 | bellard | memset(ptr, 0, size);
|
310 | 07d89866 | bellard | return ptr;
|
311 | 07d89866 | bellard | } |
312 | 07d89866 | bellard | |
313 | 2571929a | bellard | char *qemu_strdup(const char *str) |
314 | 2571929a | bellard | { |
315 | 2571929a | bellard | char *ptr;
|
316 | 2571929a | bellard | ptr = qemu_malloc(strlen(str) + 1);
|
317 | 2571929a | bellard | if (!ptr)
|
318 | 2571929a | bellard | return NULL; |
319 | 2571929a | bellard | strcpy(ptr, str); |
320 | 2571929a | bellard | return ptr;
|
321 | 2571929a | bellard | } |
322 | 2571929a | bellard | |
323 | ea88812f | bellard | /****************************************************************/
|
324 | ea88812f | bellard | /* printf support */
|
325 | ea88812f | bellard | |
326 | ea88812f | bellard | static inline int qemu_isdigit(int c) |
327 | ea88812f | bellard | { |
328 | ea88812f | bellard | return c >= '0' && c <= '9'; |
329 | ea88812f | bellard | } |
330 | ea88812f | bellard | |
331 | ea88812f | bellard | #define OUTCHAR(c) (buflen > 0? (--buflen, *buf++ = (c)): 0) |
332 | ea88812f | bellard | |
333 | ea88812f | bellard | /* from BSD ppp sources */
|
334 | ea88812f | bellard | int qemu_vsnprintf(char *buf, int buflen, const char *fmt, va_list args) |
335 | ea88812f | bellard | { |
336 | ea88812f | bellard | int c, i, n;
|
337 | ea88812f | bellard | int width, prec, fillch;
|
338 | ea88812f | bellard | int base, len, neg;
|
339 | ea88812f | bellard | unsigned long val = 0; |
340 | ea88812f | bellard | const char *f; |
341 | ea88812f | bellard | char *str, *buf0;
|
342 | ea88812f | bellard | char num[32]; |
343 | ea88812f | bellard | static const char hexchars[] = "0123456789abcdef"; |
344 | ea88812f | bellard | |
345 | ea88812f | bellard | buf0 = buf; |
346 | ea88812f | bellard | --buflen; |
347 | ea88812f | bellard | while (buflen > 0) { |
348 | ea88812f | bellard | for (f = fmt; *f != '%' && *f != 0; ++f) |
349 | ea88812f | bellard | ; |
350 | ea88812f | bellard | if (f > fmt) {
|
351 | ea88812f | bellard | len = f - fmt; |
352 | ea88812f | bellard | if (len > buflen)
|
353 | ea88812f | bellard | len = buflen; |
354 | ea88812f | bellard | memcpy(buf, fmt, len); |
355 | ea88812f | bellard | buf += len; |
356 | ea88812f | bellard | buflen -= len; |
357 | ea88812f | bellard | fmt = f; |
358 | ea88812f | bellard | } |
359 | ea88812f | bellard | if (*fmt == 0) |
360 | ea88812f | bellard | break;
|
361 | ea88812f | bellard | c = *++fmt; |
362 | ea88812f | bellard | width = prec = 0;
|
363 | ea88812f | bellard | fillch = ' ';
|
364 | ea88812f | bellard | if (c == '0') { |
365 | ea88812f | bellard | fillch = '0';
|
366 | ea88812f | bellard | c = *++fmt; |
367 | ea88812f | bellard | } |
368 | ea88812f | bellard | if (c == '*') { |
369 | ea88812f | bellard | width = va_arg(args, int);
|
370 | ea88812f | bellard | c = *++fmt; |
371 | ea88812f | bellard | } else {
|
372 | ea88812f | bellard | while (qemu_isdigit(c)) {
|
373 | ea88812f | bellard | width = width * 10 + c - '0'; |
374 | ea88812f | bellard | c = *++fmt; |
375 | ea88812f | bellard | } |
376 | ea88812f | bellard | } |
377 | ea88812f | bellard | if (c == '.') { |
378 | ea88812f | bellard | c = *++fmt; |
379 | ea88812f | bellard | if (c == '*') { |
380 | ea88812f | bellard | prec = va_arg(args, int);
|
381 | ea88812f | bellard | c = *++fmt; |
382 | ea88812f | bellard | } else {
|
383 | ea88812f | bellard | while (qemu_isdigit(c)) {
|
384 | ea88812f | bellard | prec = prec * 10 + c - '0'; |
385 | ea88812f | bellard | c = *++fmt; |
386 | ea88812f | bellard | } |
387 | ea88812f | bellard | } |
388 | ea88812f | bellard | } |
389 | ea88812f | bellard | /* modifiers */
|
390 | ea88812f | bellard | switch(c) {
|
391 | ea88812f | bellard | case 'l': |
392 | ea88812f | bellard | c = *++fmt; |
393 | ea88812f | bellard | break;
|
394 | ea88812f | bellard | default:
|
395 | ea88812f | bellard | break;
|
396 | ea88812f | bellard | } |
397 | ea88812f | bellard | str = 0;
|
398 | ea88812f | bellard | base = 0;
|
399 | ea88812f | bellard | neg = 0;
|
400 | ea88812f | bellard | ++fmt; |
401 | ea88812f | bellard | switch (c) {
|
402 | ea88812f | bellard | case 'd': |
403 | ea88812f | bellard | i = va_arg(args, int);
|
404 | ea88812f | bellard | if (i < 0) { |
405 | ea88812f | bellard | neg = 1;
|
406 | ea88812f | bellard | val = -i; |
407 | ea88812f | bellard | } else
|
408 | ea88812f | bellard | val = i; |
409 | ea88812f | bellard | base = 10;
|
410 | ea88812f | bellard | break;
|
411 | ea88812f | bellard | case 'o': |
412 | ea88812f | bellard | val = va_arg(args, unsigned int); |
413 | ea88812f | bellard | base = 8;
|
414 | ea88812f | bellard | break;
|
415 | ea88812f | bellard | case 'x': |
416 | ea88812f | bellard | case 'X': |
417 | ea88812f | bellard | val = va_arg(args, unsigned int); |
418 | ea88812f | bellard | base = 16;
|
419 | ea88812f | bellard | break;
|
420 | ea88812f | bellard | case 'p': |
421 | ea88812f | bellard | val = (unsigned long) va_arg(args, void *); |
422 | ea88812f | bellard | base = 16;
|
423 | ea88812f | bellard | neg = 2;
|
424 | ea88812f | bellard | break;
|
425 | ea88812f | bellard | case 's': |
426 | ea88812f | bellard | str = va_arg(args, char *);
|
427 | ea88812f | bellard | break;
|
428 | ea88812f | bellard | case 'c': |
429 | ea88812f | bellard | num[0] = va_arg(args, int); |
430 | ea88812f | bellard | num[1] = 0; |
431 | ea88812f | bellard | str = num; |
432 | ea88812f | bellard | break;
|
433 | ea88812f | bellard | default:
|
434 | ea88812f | bellard | *buf++ = '%';
|
435 | ea88812f | bellard | if (c != '%') |
436 | ea88812f | bellard | --fmt; /* so %z outputs %z etc. */
|
437 | ea88812f | bellard | --buflen; |
438 | ea88812f | bellard | continue;
|
439 | ea88812f | bellard | } |
440 | ea88812f | bellard | if (base != 0) { |
441 | ea88812f | bellard | str = num + sizeof(num);
|
442 | ea88812f | bellard | *--str = 0;
|
443 | ea88812f | bellard | while (str > num + neg) {
|
444 | ea88812f | bellard | *--str = hexchars[val % base]; |
445 | ea88812f | bellard | val = val / base; |
446 | ea88812f | bellard | if (--prec <= 0 && val == 0) |
447 | ea88812f | bellard | break;
|
448 | ea88812f | bellard | } |
449 | ea88812f | bellard | switch (neg) {
|
450 | ea88812f | bellard | case 1: |
451 | ea88812f | bellard | *--str = '-';
|
452 | ea88812f | bellard | break;
|
453 | ea88812f | bellard | case 2: |
454 | ea88812f | bellard | *--str = 'x';
|
455 | ea88812f | bellard | *--str = '0';
|
456 | ea88812f | bellard | break;
|
457 | ea88812f | bellard | } |
458 | ea88812f | bellard | len = num + sizeof(num) - 1 - str; |
459 | ea88812f | bellard | } else {
|
460 | ea88812f | bellard | len = strlen(str); |
461 | ea88812f | bellard | if (prec > 0 && len > prec) |
462 | ea88812f | bellard | len = prec; |
463 | ea88812f | bellard | } |
464 | ea88812f | bellard | if (width > 0) { |
465 | ea88812f | bellard | if (width > buflen)
|
466 | ea88812f | bellard | width = buflen; |
467 | ea88812f | bellard | if ((n = width - len) > 0) { |
468 | ea88812f | bellard | buflen -= n; |
469 | ea88812f | bellard | for (; n > 0; --n) |
470 | ea88812f | bellard | *buf++ = fillch; |
471 | ea88812f | bellard | } |
472 | ea88812f | bellard | } |
473 | ea88812f | bellard | if (len > buflen)
|
474 | ea88812f | bellard | len = buflen; |
475 | ea88812f | bellard | memcpy(buf, str, len); |
476 | ea88812f | bellard | buf += len; |
477 | ea88812f | bellard | buflen -= len; |
478 | ea88812f | bellard | } |
479 | ea88812f | bellard | *buf = 0;
|
480 | ea88812f | bellard | return buf - buf0;
|
481 | ea88812f | bellard | } |
482 | ea88812f | bellard | |
483 | ea88812f | bellard | void qemu_vprintf(const char *fmt, va_list ap) |
484 | ea88812f | bellard | { |
485 | ea88812f | bellard | char buf[1024]; |
486 | ea88812f | bellard | int len;
|
487 | ea88812f | bellard | |
488 | ea88812f | bellard | len = qemu_vsnprintf(buf, sizeof(buf), fmt, ap);
|
489 | ea88812f | bellard | qemu_write(1, buf, len);
|
490 | ea88812f | bellard | } |
491 | ea88812f | bellard | |
492 | ea88812f | bellard | void qemu_printf(const char *fmt, ...) |
493 | ea88812f | bellard | { |
494 | ea88812f | bellard | va_list ap; |
495 | ea88812f | bellard | va_start(ap, fmt); |
496 | ea88812f | bellard | qemu_vprintf(fmt, ap); |
497 | ea88812f | bellard | va_end(ap); |
498 | ea88812f | bellard | } |