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