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