root / osdep.c @ 0443eaf6
History | View | Annotate | Download (13 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 | 49b470eb | bellard | #include <malloc.h> |
277 | 49b470eb | bellard | |
278 | ea88812f | bellard | int qemu_write(int fd, const void *buf, size_t n) |
279 | ea88812f | bellard | { |
280 | ea88812f | bellard | int ret;
|
281 | ea88812f | bellard | ret = write(fd, buf, n); |
282 | ea88812f | bellard | if (ret < 0) |
283 | ea88812f | bellard | return -errno;
|
284 | ea88812f | bellard | else
|
285 | ea88812f | bellard | return ret;
|
286 | ea88812f | bellard | } |
287 | ea88812f | bellard | |
288 | ea88812f | bellard | void *get_mmap_addr(unsigned long size) |
289 | ea88812f | bellard | { |
290 | ea88812f | bellard | return NULL; |
291 | ea88812f | bellard | } |
292 | ea88812f | bellard | |
293 | ea88812f | bellard | void qemu_free(void *ptr) |
294 | ea88812f | bellard | { |
295 | ea88812f | bellard | free(ptr); |
296 | ea88812f | bellard | } |
297 | ea88812f | bellard | |
298 | ea88812f | bellard | void *qemu_malloc(size_t size)
|
299 | ea88812f | bellard | { |
300 | ea88812f | bellard | return malloc(size);
|
301 | ea88812f | bellard | } |
302 | ea88812f | bellard | |
303 | 49b470eb | bellard | #if defined(USE_KQEMU)
|
304 | 49b470eb | bellard | |
305 | 49b470eb | bellard | #include <sys/mman.h> |
306 | 49b470eb | bellard | #include <fcntl.h> |
307 | 49b470eb | bellard | |
308 | 49b470eb | bellard | void *qemu_vmalloc(size_t size)
|
309 | 49b470eb | bellard | { |
310 | 49b470eb | bellard | static int phys_ram_fd = -1; |
311 | 49b470eb | bellard | static int phys_ram_size = 0; |
312 | 49b470eb | bellard | const char *tmpdir; |
313 | 49b470eb | bellard | char phys_ram_file[1024]; |
314 | 49b470eb | bellard | void *ptr;
|
315 | 49b470eb | bellard | |
316 | 49b470eb | bellard | if (phys_ram_fd < 0) { |
317 | 49b470eb | bellard | tmpdir = getenv("QEMU_TMPDIR");
|
318 | 49b470eb | bellard | if (!tmpdir)
|
319 | 49b470eb | bellard | tmpdir = "/dev/shm";
|
320 | 49b470eb | bellard | snprintf(phys_ram_file, sizeof(phys_ram_file), "%s/qemuXXXXXX", |
321 | 49b470eb | bellard | tmpdir); |
322 | 49b470eb | bellard | if (mkstemp(phys_ram_file) < 0) { |
323 | 49b470eb | bellard | fprintf(stderr, |
324 | 49b470eb | bellard | "warning: could not create temporary file in '%s'.\n"
|
325 | 49b470eb | bellard | "Use QEMU_TMPDIR to select a directory in a tmpfs filesystem.\n"
|
326 | 49b470eb | bellard | "Using '/tmp' as fallback.\n",
|
327 | 49b470eb | bellard | tmpdir); |
328 | 49b470eb | bellard | snprintf(phys_ram_file, sizeof(phys_ram_file), "%s/qemuXXXXXX", |
329 | 49b470eb | bellard | "/tmp");
|
330 | 49b470eb | bellard | if (mkstemp(phys_ram_file) < 0) { |
331 | 49b470eb | bellard | fprintf(stderr, "Could not create temporary memory file '%s'\n",
|
332 | 49b470eb | bellard | phys_ram_file); |
333 | 49b470eb | bellard | exit(1);
|
334 | 49b470eb | bellard | } |
335 | 49b470eb | bellard | } |
336 | 49b470eb | bellard | phys_ram_fd = open(phys_ram_file, O_CREAT | O_TRUNC | O_RDWR, 0600);
|
337 | 49b470eb | bellard | if (phys_ram_fd < 0) { |
338 | 49b470eb | bellard | fprintf(stderr, "Could not open temporary memory file '%s'\n",
|
339 | 49b470eb | bellard | phys_ram_file); |
340 | 49b470eb | bellard | exit(1);
|
341 | 49b470eb | bellard | } |
342 | 49b470eb | bellard | unlink(phys_ram_file); |
343 | 49b470eb | bellard | } |
344 | 49b470eb | bellard | size = (size + 4095) & ~4095; |
345 | 49b470eb | bellard | ftruncate(phys_ram_fd, phys_ram_size + size); |
346 | 49b470eb | bellard | ptr = mmap(NULL,
|
347 | 49b470eb | bellard | size, |
348 | 49b470eb | bellard | PROT_WRITE | PROT_READ, MAP_SHARED, |
349 | 49b470eb | bellard | phys_ram_fd, phys_ram_size); |
350 | 49b470eb | bellard | if (ptr == MAP_FAILED) {
|
351 | 49b470eb | bellard | fprintf(stderr, "Could not map physical memory\n");
|
352 | 49b470eb | bellard | exit(1);
|
353 | 49b470eb | bellard | } |
354 | 49b470eb | bellard | phys_ram_size += size; |
355 | 49b470eb | bellard | return ptr;
|
356 | 49b470eb | bellard | } |
357 | 49b470eb | bellard | |
358 | 49b470eb | bellard | void qemu_vfree(void *ptr) |
359 | 49b470eb | bellard | { |
360 | 49b470eb | bellard | /* may be useful some day, but currently we do not need to free */
|
361 | 49b470eb | bellard | } |
362 | 49b470eb | bellard | |
363 | 49b470eb | bellard | #else
|
364 | 49b470eb | bellard | |
365 | 49b470eb | bellard | /* alloc shared memory pages */
|
366 | 49b470eb | bellard | void *qemu_vmalloc(size_t size)
|
367 | 49b470eb | bellard | { |
368 | 49b470eb | bellard | #ifdef _BSD
|
369 | 49b470eb | bellard | return valloc(size);
|
370 | 49b470eb | bellard | #else
|
371 | 49b470eb | bellard | return memalign(4096, size); |
372 | 49b470eb | bellard | #endif
|
373 | 49b470eb | bellard | } |
374 | 49b470eb | bellard | |
375 | 49b470eb | bellard | void qemu_vfree(void *ptr) |
376 | 49b470eb | bellard | { |
377 | 49b470eb | bellard | free(ptr); |
378 | 49b470eb | bellard | } |
379 | 49b470eb | bellard | |
380 | 49b470eb | bellard | #endif
|
381 | 49b470eb | bellard | |
382 | ea88812f | bellard | #endif
|
383 | ea88812f | bellard | |
384 | 07d89866 | bellard | void *qemu_mallocz(size_t size)
|
385 | 07d89866 | bellard | { |
386 | 07d89866 | bellard | void *ptr;
|
387 | 07d89866 | bellard | ptr = qemu_malloc(size); |
388 | 07d89866 | bellard | if (!ptr)
|
389 | 07d89866 | bellard | return NULL; |
390 | 07d89866 | bellard | memset(ptr, 0, size);
|
391 | 07d89866 | bellard | return ptr;
|
392 | 07d89866 | bellard | } |
393 | 07d89866 | bellard | |
394 | 2571929a | bellard | char *qemu_strdup(const char *str) |
395 | 2571929a | bellard | { |
396 | 2571929a | bellard | char *ptr;
|
397 | 2571929a | bellard | ptr = qemu_malloc(strlen(str) + 1);
|
398 | 2571929a | bellard | if (!ptr)
|
399 | 2571929a | bellard | return NULL; |
400 | 2571929a | bellard | strcpy(ptr, str); |
401 | 2571929a | bellard | return ptr;
|
402 | 2571929a | bellard | } |
403 | 2571929a | bellard | |
404 | ea88812f | bellard | /****************************************************************/
|
405 | ea88812f | bellard | /* printf support */
|
406 | ea88812f | bellard | |
407 | ea88812f | bellard | static inline int qemu_isdigit(int c) |
408 | ea88812f | bellard | { |
409 | ea88812f | bellard | return c >= '0' && c <= '9'; |
410 | ea88812f | bellard | } |
411 | ea88812f | bellard | |
412 | ea88812f | bellard | #define OUTCHAR(c) (buflen > 0? (--buflen, *buf++ = (c)): 0) |
413 | ea88812f | bellard | |
414 | ea88812f | bellard | /* from BSD ppp sources */
|
415 | ea88812f | bellard | int qemu_vsnprintf(char *buf, int buflen, const char *fmt, va_list args) |
416 | ea88812f | bellard | { |
417 | ea88812f | bellard | int c, i, n;
|
418 | ea88812f | bellard | int width, prec, fillch;
|
419 | ea88812f | bellard | int base, len, neg;
|
420 | ea88812f | bellard | unsigned long val = 0; |
421 | ea88812f | bellard | const char *f; |
422 | ea88812f | bellard | char *str, *buf0;
|
423 | ea88812f | bellard | char num[32]; |
424 | ea88812f | bellard | static const char hexchars[] = "0123456789abcdef"; |
425 | ea88812f | bellard | |
426 | ea88812f | bellard | buf0 = buf; |
427 | ea88812f | bellard | --buflen; |
428 | ea88812f | bellard | while (buflen > 0) { |
429 | ea88812f | bellard | for (f = fmt; *f != '%' && *f != 0; ++f) |
430 | ea88812f | bellard | ; |
431 | ea88812f | bellard | if (f > fmt) {
|
432 | ea88812f | bellard | len = f - fmt; |
433 | ea88812f | bellard | if (len > buflen)
|
434 | ea88812f | bellard | len = buflen; |
435 | ea88812f | bellard | memcpy(buf, fmt, len); |
436 | ea88812f | bellard | buf += len; |
437 | ea88812f | bellard | buflen -= len; |
438 | ea88812f | bellard | fmt = f; |
439 | ea88812f | bellard | } |
440 | ea88812f | bellard | if (*fmt == 0) |
441 | ea88812f | bellard | break;
|
442 | ea88812f | bellard | c = *++fmt; |
443 | ea88812f | bellard | width = prec = 0;
|
444 | ea88812f | bellard | fillch = ' ';
|
445 | ea88812f | bellard | if (c == '0') { |
446 | ea88812f | bellard | fillch = '0';
|
447 | ea88812f | bellard | c = *++fmt; |
448 | ea88812f | bellard | } |
449 | ea88812f | bellard | if (c == '*') { |
450 | ea88812f | bellard | width = va_arg(args, int);
|
451 | ea88812f | bellard | c = *++fmt; |
452 | ea88812f | bellard | } else {
|
453 | ea88812f | bellard | while (qemu_isdigit(c)) {
|
454 | ea88812f | bellard | width = width * 10 + c - '0'; |
455 | ea88812f | bellard | c = *++fmt; |
456 | ea88812f | bellard | } |
457 | ea88812f | bellard | } |
458 | ea88812f | bellard | if (c == '.') { |
459 | ea88812f | bellard | c = *++fmt; |
460 | ea88812f | bellard | if (c == '*') { |
461 | ea88812f | bellard | prec = va_arg(args, int);
|
462 | ea88812f | bellard | c = *++fmt; |
463 | ea88812f | bellard | } else {
|
464 | ea88812f | bellard | while (qemu_isdigit(c)) {
|
465 | ea88812f | bellard | prec = prec * 10 + c - '0'; |
466 | ea88812f | bellard | c = *++fmt; |
467 | ea88812f | bellard | } |
468 | ea88812f | bellard | } |
469 | ea88812f | bellard | } |
470 | ea88812f | bellard | /* modifiers */
|
471 | ea88812f | bellard | switch(c) {
|
472 | ea88812f | bellard | case 'l': |
473 | ea88812f | bellard | c = *++fmt; |
474 | ea88812f | bellard | break;
|
475 | ea88812f | bellard | default:
|
476 | ea88812f | bellard | break;
|
477 | ea88812f | bellard | } |
478 | ea88812f | bellard | str = 0;
|
479 | ea88812f | bellard | base = 0;
|
480 | ea88812f | bellard | neg = 0;
|
481 | ea88812f | bellard | ++fmt; |
482 | ea88812f | bellard | switch (c) {
|
483 | ea88812f | bellard | case 'd': |
484 | ea88812f | bellard | i = va_arg(args, int);
|
485 | ea88812f | bellard | if (i < 0) { |
486 | ea88812f | bellard | neg = 1;
|
487 | ea88812f | bellard | val = -i; |
488 | ea88812f | bellard | } else
|
489 | ea88812f | bellard | val = i; |
490 | ea88812f | bellard | base = 10;
|
491 | ea88812f | bellard | break;
|
492 | ea88812f | bellard | case 'o': |
493 | ea88812f | bellard | val = va_arg(args, unsigned int); |
494 | ea88812f | bellard | base = 8;
|
495 | ea88812f | bellard | break;
|
496 | ea88812f | bellard | case 'x': |
497 | ea88812f | bellard | case 'X': |
498 | ea88812f | bellard | val = va_arg(args, unsigned int); |
499 | ea88812f | bellard | base = 16;
|
500 | ea88812f | bellard | break;
|
501 | ea88812f | bellard | case 'p': |
502 | ea88812f | bellard | val = (unsigned long) va_arg(args, void *); |
503 | ea88812f | bellard | base = 16;
|
504 | ea88812f | bellard | neg = 2;
|
505 | ea88812f | bellard | break;
|
506 | ea88812f | bellard | case 's': |
507 | ea88812f | bellard | str = va_arg(args, char *);
|
508 | ea88812f | bellard | break;
|
509 | ea88812f | bellard | case 'c': |
510 | ea88812f | bellard | num[0] = va_arg(args, int); |
511 | ea88812f | bellard | num[1] = 0; |
512 | ea88812f | bellard | str = num; |
513 | ea88812f | bellard | break;
|
514 | ea88812f | bellard | default:
|
515 | ea88812f | bellard | *buf++ = '%';
|
516 | ea88812f | bellard | if (c != '%') |
517 | ea88812f | bellard | --fmt; /* so %z outputs %z etc. */
|
518 | ea88812f | bellard | --buflen; |
519 | ea88812f | bellard | continue;
|
520 | ea88812f | bellard | } |
521 | ea88812f | bellard | if (base != 0) { |
522 | ea88812f | bellard | str = num + sizeof(num);
|
523 | ea88812f | bellard | *--str = 0;
|
524 | ea88812f | bellard | while (str > num + neg) {
|
525 | ea88812f | bellard | *--str = hexchars[val % base]; |
526 | ea88812f | bellard | val = val / base; |
527 | ea88812f | bellard | if (--prec <= 0 && val == 0) |
528 | ea88812f | bellard | break;
|
529 | ea88812f | bellard | } |
530 | ea88812f | bellard | switch (neg) {
|
531 | ea88812f | bellard | case 1: |
532 | ea88812f | bellard | *--str = '-';
|
533 | ea88812f | bellard | break;
|
534 | ea88812f | bellard | case 2: |
535 | ea88812f | bellard | *--str = 'x';
|
536 | ea88812f | bellard | *--str = '0';
|
537 | ea88812f | bellard | break;
|
538 | ea88812f | bellard | } |
539 | ea88812f | bellard | len = num + sizeof(num) - 1 - str; |
540 | ea88812f | bellard | } else {
|
541 | ea88812f | bellard | len = strlen(str); |
542 | ea88812f | bellard | if (prec > 0 && len > prec) |
543 | ea88812f | bellard | len = prec; |
544 | ea88812f | bellard | } |
545 | ea88812f | bellard | if (width > 0) { |
546 | ea88812f | bellard | if (width > buflen)
|
547 | ea88812f | bellard | width = buflen; |
548 | ea88812f | bellard | if ((n = width - len) > 0) { |
549 | ea88812f | bellard | buflen -= n; |
550 | ea88812f | bellard | for (; n > 0; --n) |
551 | ea88812f | bellard | *buf++ = fillch; |
552 | ea88812f | bellard | } |
553 | ea88812f | bellard | } |
554 | ea88812f | bellard | if (len > buflen)
|
555 | ea88812f | bellard | len = buflen; |
556 | ea88812f | bellard | memcpy(buf, str, len); |
557 | ea88812f | bellard | buf += len; |
558 | ea88812f | bellard | buflen -= len; |
559 | ea88812f | bellard | } |
560 | ea88812f | bellard | *buf = 0;
|
561 | ea88812f | bellard | return buf - buf0;
|
562 | ea88812f | bellard | } |
563 | ea88812f | bellard | |
564 | ea88812f | bellard | void qemu_vprintf(const char *fmt, va_list ap) |
565 | ea88812f | bellard | { |
566 | ea88812f | bellard | char buf[1024]; |
567 | ea88812f | bellard | int len;
|
568 | ea88812f | bellard | |
569 | ea88812f | bellard | len = qemu_vsnprintf(buf, sizeof(buf), fmt, ap);
|
570 | ea88812f | bellard | qemu_write(1, buf, len);
|
571 | ea88812f | bellard | } |
572 | ea88812f | bellard | |
573 | ea88812f | bellard | void qemu_printf(const char *fmt, ...) |
574 | ea88812f | bellard | { |
575 | ea88812f | bellard | va_list ap; |
576 | ea88812f | bellard | va_start(ap, fmt); |
577 | ea88812f | bellard | qemu_vprintf(fmt, ap); |
578 | ea88812f | bellard | va_end(ap); |
579 | ea88812f | bellard | } |