root / osdep.c @ 2aebb3eb
History | View | Annotate | Download (10.3 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 | ea88812f | bellard | /* memory allocation */
|
148 | ea88812f | bellard | |
149 | ea88812f | bellard | //#define DEBUG_MALLOC
|
150 | ea88812f | bellard | |
151 | ea88812f | bellard | #define MALLOC_BASE 0xab000000 |
152 | ea88812f | bellard | #define PHYS_RAM_BASE 0xac000000 |
153 | ea88812f | bellard | |
154 | ea88812f | bellard | #define MALLOC_ALIGN 16 |
155 | ea88812f | bellard | #define BLOCK_HEADER_SIZE 16 |
156 | ea88812f | bellard | |
157 | ea88812f | bellard | typedef struct MemoryBlock { |
158 | ea88812f | bellard | struct MemoryBlock *next;
|
159 | ea88812f | bellard | unsigned long size; /* size of block, including header */ |
160 | ea88812f | bellard | } MemoryBlock; |
161 | ea88812f | bellard | |
162 | ea88812f | bellard | static MemoryBlock *first_free_block;
|
163 | ea88812f | bellard | static unsigned long malloc_addr = MALLOC_BASE; |
164 | ea88812f | bellard | |
165 | ea88812f | bellard | static void *malloc_get_space(size_t size) |
166 | ea88812f | bellard | { |
167 | ea88812f | bellard | void *ptr;
|
168 | ea88812f | bellard | size = TARGET_PAGE_ALIGN(size); |
169 | ea88812f | bellard | ptr = mmap((void *)malloc_addr, size,
|
170 | ea88812f | bellard | PROT_WRITE | PROT_READ, |
171 | ea88812f | bellard | MAP_PRIVATE | MAP_FIXED | MAP_ANON, -1, 0); |
172 | ea88812f | bellard | if (ptr == MAP_FAILED)
|
173 | ea88812f | bellard | return NULL; |
174 | ea88812f | bellard | malloc_addr += size; |
175 | ea88812f | bellard | return ptr;
|
176 | ea88812f | bellard | } |
177 | ea88812f | bellard | |
178 | ea88812f | bellard | void *qemu_malloc(size_t size)
|
179 | ea88812f | bellard | { |
180 | ea88812f | bellard | MemoryBlock *mb, *mb1, **pmb; |
181 | ea88812f | bellard | void *ptr;
|
182 | ea88812f | bellard | size_t size1, area_size; |
183 | ea88812f | bellard | |
184 | ea88812f | bellard | if (size == 0) |
185 | ea88812f | bellard | return NULL; |
186 | ea88812f | bellard | |
187 | ea88812f | bellard | size = (size + BLOCK_HEADER_SIZE + MALLOC_ALIGN - 1) & ~(MALLOC_ALIGN - 1); |
188 | ea88812f | bellard | pmb = &first_free_block; |
189 | ea88812f | bellard | for(;;) {
|
190 | ea88812f | bellard | mb = *pmb; |
191 | ea88812f | bellard | if (mb == NULL) |
192 | ea88812f | bellard | break;
|
193 | ea88812f | bellard | if (size <= mb->size)
|
194 | ea88812f | bellard | goto found;
|
195 | ea88812f | bellard | pmb = &mb->next; |
196 | ea88812f | bellard | } |
197 | ea88812f | bellard | /* no big enough blocks found: get new space */
|
198 | ea88812f | bellard | area_size = TARGET_PAGE_ALIGN(size); |
199 | ea88812f | bellard | mb = malloc_get_space(area_size); |
200 | ea88812f | bellard | if (!mb)
|
201 | ea88812f | bellard | return NULL; |
202 | ea88812f | bellard | size1 = area_size - size; |
203 | ea88812f | bellard | if (size1 > 0) { |
204 | ea88812f | bellard | /* create a new free block */
|
205 | ea88812f | bellard | mb1 = (MemoryBlock *)((uint8_t *)mb + size); |
206 | ea88812f | bellard | mb1->next = NULL;
|
207 | ea88812f | bellard | mb1->size = size1; |
208 | ea88812f | bellard | *pmb = mb1; |
209 | ea88812f | bellard | } |
210 | ea88812f | bellard | goto the_end;
|
211 | ea88812f | bellard | found:
|
212 | ea88812f | bellard | /* a free block was found: use it */
|
213 | ea88812f | bellard | size1 = mb->size - size; |
214 | ea88812f | bellard | if (size1 > 0) { |
215 | ea88812f | bellard | /* create a new free block */
|
216 | ea88812f | bellard | mb1 = (MemoryBlock *)((uint8_t *)mb + size); |
217 | ea88812f | bellard | mb1->next = mb->next; |
218 | ea88812f | bellard | mb1->size = size1; |
219 | ea88812f | bellard | *pmb = mb1; |
220 | ea88812f | bellard | } else {
|
221 | ea88812f | bellard | /* suppress the first block */
|
222 | ea88812f | bellard | *pmb = mb->next; |
223 | ea88812f | bellard | } |
224 | ea88812f | bellard | the_end:
|
225 | ea88812f | bellard | mb->size = size; |
226 | ea88812f | bellard | mb->next = NULL;
|
227 | ea88812f | bellard | ptr = ((uint8_t *)mb + BLOCK_HEADER_SIZE); |
228 | ea88812f | bellard | #ifdef DEBUG_MALLOC
|
229 | ea88812f | bellard | qemu_printf("malloc: size=0x%x ptr=0x%lx\n", size, (unsigned long)ptr); |
230 | ea88812f | bellard | #endif
|
231 | ea88812f | bellard | return ptr;
|
232 | ea88812f | bellard | } |
233 | ea88812f | bellard | |
234 | ea88812f | bellard | void qemu_free(void *ptr) |
235 | ea88812f | bellard | { |
236 | ea88812f | bellard | MemoryBlock *mb; |
237 | ea88812f | bellard | |
238 | 57c30724 | bellard | if (!ptr)
|
239 | 57c30724 | bellard | return;
|
240 | ea88812f | bellard | mb = (MemoryBlock *)((uint8_t *)ptr - BLOCK_HEADER_SIZE); |
241 | ea88812f | bellard | mb->next = first_free_block; |
242 | ea88812f | bellard | first_free_block = mb; |
243 | ea88812f | bellard | } |
244 | ea88812f | bellard | |
245 | ea88812f | bellard | /****************************************************************/
|
246 | ea88812f | bellard | /* virtual memory allocation */
|
247 | ea88812f | bellard | |
248 | ea88812f | bellard | unsigned long mmap_addr = PHYS_RAM_BASE; |
249 | ea88812f | bellard | |
250 | ea88812f | bellard | void *get_mmap_addr(unsigned long size) |
251 | ea88812f | bellard | { |
252 | ea88812f | bellard | unsigned long addr; |
253 | ea88812f | bellard | addr = mmap_addr; |
254 | ea88812f | bellard | mmap_addr += ((size + 4095) & ~4095) + 4096; |
255 | ea88812f | bellard | return (void *)addr; |
256 | ea88812f | bellard | } |
257 | ea88812f | bellard | |
258 | ea88812f | bellard | #else
|
259 | ea88812f | bellard | |
260 | ea88812f | bellard | int qemu_write(int fd, const void *buf, size_t n) |
261 | ea88812f | bellard | { |
262 | ea88812f | bellard | int ret;
|
263 | ea88812f | bellard | ret = write(fd, buf, n); |
264 | ea88812f | bellard | if (ret < 0) |
265 | ea88812f | bellard | return -errno;
|
266 | ea88812f | bellard | else
|
267 | ea88812f | bellard | return ret;
|
268 | ea88812f | bellard | } |
269 | ea88812f | bellard | |
270 | ea88812f | bellard | void *get_mmap_addr(unsigned long size) |
271 | ea88812f | bellard | { |
272 | ea88812f | bellard | return NULL; |
273 | ea88812f | bellard | } |
274 | ea88812f | bellard | |
275 | ea88812f | bellard | void qemu_free(void *ptr) |
276 | ea88812f | bellard | { |
277 | ea88812f | bellard | free(ptr); |
278 | ea88812f | bellard | } |
279 | ea88812f | bellard | |
280 | ea88812f | bellard | void *qemu_malloc(size_t size)
|
281 | ea88812f | bellard | { |
282 | ea88812f | bellard | return malloc(size);
|
283 | ea88812f | bellard | } |
284 | ea88812f | bellard | |
285 | ea88812f | bellard | #endif
|
286 | ea88812f | bellard | |
287 | 07d89866 | bellard | void *qemu_mallocz(size_t size)
|
288 | 07d89866 | bellard | { |
289 | 07d89866 | bellard | void *ptr;
|
290 | 07d89866 | bellard | ptr = qemu_malloc(size); |
291 | 07d89866 | bellard | if (!ptr)
|
292 | 07d89866 | bellard | return NULL; |
293 | 07d89866 | bellard | memset(ptr, 0, size);
|
294 | 07d89866 | bellard | return ptr;
|
295 | 07d89866 | bellard | } |
296 | 07d89866 | bellard | |
297 | ea88812f | bellard | /****************************************************************/
|
298 | ea88812f | bellard | /* printf support */
|
299 | ea88812f | bellard | |
300 | ea88812f | bellard | static inline int qemu_isdigit(int c) |
301 | ea88812f | bellard | { |
302 | ea88812f | bellard | return c >= '0' && c <= '9'; |
303 | ea88812f | bellard | } |
304 | ea88812f | bellard | |
305 | ea88812f | bellard | #define OUTCHAR(c) (buflen > 0? (--buflen, *buf++ = (c)): 0) |
306 | ea88812f | bellard | |
307 | ea88812f | bellard | /* from BSD ppp sources */
|
308 | ea88812f | bellard | int qemu_vsnprintf(char *buf, int buflen, const char *fmt, va_list args) |
309 | ea88812f | bellard | { |
310 | ea88812f | bellard | int c, i, n;
|
311 | ea88812f | bellard | int width, prec, fillch;
|
312 | ea88812f | bellard | int base, len, neg;
|
313 | ea88812f | bellard | unsigned long val = 0; |
314 | ea88812f | bellard | const char *f; |
315 | ea88812f | bellard | char *str, *buf0;
|
316 | ea88812f | bellard | char num[32]; |
317 | ea88812f | bellard | static const char hexchars[] = "0123456789abcdef"; |
318 | ea88812f | bellard | |
319 | ea88812f | bellard | buf0 = buf; |
320 | ea88812f | bellard | --buflen; |
321 | ea88812f | bellard | while (buflen > 0) { |
322 | ea88812f | bellard | for (f = fmt; *f != '%' && *f != 0; ++f) |
323 | ea88812f | bellard | ; |
324 | ea88812f | bellard | if (f > fmt) {
|
325 | ea88812f | bellard | len = f - fmt; |
326 | ea88812f | bellard | if (len > buflen)
|
327 | ea88812f | bellard | len = buflen; |
328 | ea88812f | bellard | memcpy(buf, fmt, len); |
329 | ea88812f | bellard | buf += len; |
330 | ea88812f | bellard | buflen -= len; |
331 | ea88812f | bellard | fmt = f; |
332 | ea88812f | bellard | } |
333 | ea88812f | bellard | if (*fmt == 0) |
334 | ea88812f | bellard | break;
|
335 | ea88812f | bellard | c = *++fmt; |
336 | ea88812f | bellard | width = prec = 0;
|
337 | ea88812f | bellard | fillch = ' ';
|
338 | ea88812f | bellard | if (c == '0') { |
339 | ea88812f | bellard | fillch = '0';
|
340 | ea88812f | bellard | c = *++fmt; |
341 | ea88812f | bellard | } |
342 | ea88812f | bellard | if (c == '*') { |
343 | ea88812f | bellard | width = va_arg(args, int);
|
344 | ea88812f | bellard | c = *++fmt; |
345 | ea88812f | bellard | } else {
|
346 | ea88812f | bellard | while (qemu_isdigit(c)) {
|
347 | ea88812f | bellard | width = width * 10 + c - '0'; |
348 | ea88812f | bellard | c = *++fmt; |
349 | ea88812f | bellard | } |
350 | ea88812f | bellard | } |
351 | ea88812f | bellard | if (c == '.') { |
352 | ea88812f | bellard | c = *++fmt; |
353 | ea88812f | bellard | if (c == '*') { |
354 | ea88812f | bellard | prec = va_arg(args, int);
|
355 | ea88812f | bellard | c = *++fmt; |
356 | ea88812f | bellard | } else {
|
357 | ea88812f | bellard | while (qemu_isdigit(c)) {
|
358 | ea88812f | bellard | prec = prec * 10 + c - '0'; |
359 | ea88812f | bellard | c = *++fmt; |
360 | ea88812f | bellard | } |
361 | ea88812f | bellard | } |
362 | ea88812f | bellard | } |
363 | ea88812f | bellard | /* modifiers */
|
364 | ea88812f | bellard | switch(c) {
|
365 | ea88812f | bellard | case 'l': |
366 | ea88812f | bellard | c = *++fmt; |
367 | ea88812f | bellard | break;
|
368 | ea88812f | bellard | default:
|
369 | ea88812f | bellard | break;
|
370 | ea88812f | bellard | } |
371 | ea88812f | bellard | str = 0;
|
372 | ea88812f | bellard | base = 0;
|
373 | ea88812f | bellard | neg = 0;
|
374 | ea88812f | bellard | ++fmt; |
375 | ea88812f | bellard | switch (c) {
|
376 | ea88812f | bellard | case 'd': |
377 | ea88812f | bellard | i = va_arg(args, int);
|
378 | ea88812f | bellard | if (i < 0) { |
379 | ea88812f | bellard | neg = 1;
|
380 | ea88812f | bellard | val = -i; |
381 | ea88812f | bellard | } else
|
382 | ea88812f | bellard | val = i; |
383 | ea88812f | bellard | base = 10;
|
384 | ea88812f | bellard | break;
|
385 | ea88812f | bellard | case 'o': |
386 | ea88812f | bellard | val = va_arg(args, unsigned int); |
387 | ea88812f | bellard | base = 8;
|
388 | ea88812f | bellard | break;
|
389 | ea88812f | bellard | case 'x': |
390 | ea88812f | bellard | case 'X': |
391 | ea88812f | bellard | val = va_arg(args, unsigned int); |
392 | ea88812f | bellard | base = 16;
|
393 | ea88812f | bellard | break;
|
394 | ea88812f | bellard | case 'p': |
395 | ea88812f | bellard | val = (unsigned long) va_arg(args, void *); |
396 | ea88812f | bellard | base = 16;
|
397 | ea88812f | bellard | neg = 2;
|
398 | ea88812f | bellard | break;
|
399 | ea88812f | bellard | case 's': |
400 | ea88812f | bellard | str = va_arg(args, char *);
|
401 | ea88812f | bellard | break;
|
402 | ea88812f | bellard | case 'c': |
403 | ea88812f | bellard | num[0] = va_arg(args, int); |
404 | ea88812f | bellard | num[1] = 0; |
405 | ea88812f | bellard | str = num; |
406 | ea88812f | bellard | break;
|
407 | ea88812f | bellard | default:
|
408 | ea88812f | bellard | *buf++ = '%';
|
409 | ea88812f | bellard | if (c != '%') |
410 | ea88812f | bellard | --fmt; /* so %z outputs %z etc. */
|
411 | ea88812f | bellard | --buflen; |
412 | ea88812f | bellard | continue;
|
413 | ea88812f | bellard | } |
414 | ea88812f | bellard | if (base != 0) { |
415 | ea88812f | bellard | str = num + sizeof(num);
|
416 | ea88812f | bellard | *--str = 0;
|
417 | ea88812f | bellard | while (str > num + neg) {
|
418 | ea88812f | bellard | *--str = hexchars[val % base]; |
419 | ea88812f | bellard | val = val / base; |
420 | ea88812f | bellard | if (--prec <= 0 && val == 0) |
421 | ea88812f | bellard | break;
|
422 | ea88812f | bellard | } |
423 | ea88812f | bellard | switch (neg) {
|
424 | ea88812f | bellard | case 1: |
425 | ea88812f | bellard | *--str = '-';
|
426 | ea88812f | bellard | break;
|
427 | ea88812f | bellard | case 2: |
428 | ea88812f | bellard | *--str = 'x';
|
429 | ea88812f | bellard | *--str = '0';
|
430 | ea88812f | bellard | break;
|
431 | ea88812f | bellard | } |
432 | ea88812f | bellard | len = num + sizeof(num) - 1 - str; |
433 | ea88812f | bellard | } else {
|
434 | ea88812f | bellard | len = strlen(str); |
435 | ea88812f | bellard | if (prec > 0 && len > prec) |
436 | ea88812f | bellard | len = prec; |
437 | ea88812f | bellard | } |
438 | ea88812f | bellard | if (width > 0) { |
439 | ea88812f | bellard | if (width > buflen)
|
440 | ea88812f | bellard | width = buflen; |
441 | ea88812f | bellard | if ((n = width - len) > 0) { |
442 | ea88812f | bellard | buflen -= n; |
443 | ea88812f | bellard | for (; n > 0; --n) |
444 | ea88812f | bellard | *buf++ = fillch; |
445 | ea88812f | bellard | } |
446 | ea88812f | bellard | } |
447 | ea88812f | bellard | if (len > buflen)
|
448 | ea88812f | bellard | len = buflen; |
449 | ea88812f | bellard | memcpy(buf, str, len); |
450 | ea88812f | bellard | buf += len; |
451 | ea88812f | bellard | buflen -= len; |
452 | ea88812f | bellard | } |
453 | ea88812f | bellard | *buf = 0;
|
454 | ea88812f | bellard | return buf - buf0;
|
455 | ea88812f | bellard | } |
456 | ea88812f | bellard | |
457 | ea88812f | bellard | void qemu_vprintf(const char *fmt, va_list ap) |
458 | ea88812f | bellard | { |
459 | ea88812f | bellard | char buf[1024]; |
460 | ea88812f | bellard | int len;
|
461 | ea88812f | bellard | |
462 | ea88812f | bellard | len = qemu_vsnprintf(buf, sizeof(buf), fmt, ap);
|
463 | ea88812f | bellard | qemu_write(1, buf, len);
|
464 | ea88812f | bellard | } |
465 | ea88812f | bellard | |
466 | ea88812f | bellard | void qemu_printf(const char *fmt, ...) |
467 | ea88812f | bellard | { |
468 | ea88812f | bellard | va_list ap; |
469 | ea88812f | bellard | va_start(ap, fmt); |
470 | ea88812f | bellard | qemu_vprintf(fmt, ap); |
471 | ea88812f | bellard | va_end(ap); |
472 | ea88812f | bellard | } |