fixing free
[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     if (!ptr)
239         return;
240     mb = (MemoryBlock *)((uint8_t *)ptr - BLOCK_HEADER_SIZE);
241     mb->next = first_free_block;
242     first_free_block = mb;
243 }
244
245 /****************************************************************/
246 /* virtual memory allocation */
247
248 unsigned long mmap_addr = PHYS_RAM_BASE;
249
250 void *get_mmap_addr(unsigned long size)
251 {
252     unsigned long addr;
253     addr = mmap_addr;
254     mmap_addr += ((size + 4095) & ~4095) + 4096;
255     return (void *)addr;
256 }
257
258 #else
259
260 int qemu_write(int fd, const void *buf, size_t n)
261 {
262     int ret;
263     ret = write(fd, buf, n);
264     if (ret < 0)
265         return -errno;
266     else
267         return ret;
268 }
269
270 void *get_mmap_addr(unsigned long size)
271 {
272     return NULL;
273 }
274
275 void qemu_free(void *ptr)
276 {
277     free(ptr);
278 }
279
280 void *qemu_malloc(size_t size)
281 {
282     return malloc(size);
283 }
284
285 #endif
286
287 void *qemu_mallocz(size_t size)
288 {
289     void *ptr;
290     ptr = qemu_malloc(size);
291     if (!ptr)
292         return NULL;
293     memset(ptr, 0, size);
294     return ptr;
295 }
296
297 /****************************************************************/
298 /* printf support */
299
300 static inline int qemu_isdigit(int c)
301 {
302     return c >= '0' && c <= '9';
303 }
304
305 #define OUTCHAR(c)      (buflen > 0? (--buflen, *buf++ = (c)): 0)
306
307 /* from BSD ppp sources */
308 int qemu_vsnprintf(char *buf, int buflen, const char *fmt, va_list args)
309 {
310     int c, i, n;
311     int width, prec, fillch;
312     int base, len, neg;
313     unsigned long val = 0;
314     const char *f;
315     char *str, *buf0;
316     char num[32];
317     static const char hexchars[] = "0123456789abcdef";
318
319     buf0 = buf;
320     --buflen;
321     while (buflen > 0) {
322         for (f = fmt; *f != '%' && *f != 0; ++f)
323             ;
324         if (f > fmt) {
325             len = f - fmt;
326             if (len > buflen)
327                 len = buflen;
328             memcpy(buf, fmt, len);
329             buf += len;
330             buflen -= len;
331             fmt = f;
332         }
333         if (*fmt == 0)
334             break;
335         c = *++fmt;
336         width = prec = 0;
337         fillch = ' ';
338         if (c == '0') {
339             fillch = '0';
340             c = *++fmt;
341         }
342         if (c == '*') {
343             width = va_arg(args, int);
344             c = *++fmt;
345         } else {
346             while (qemu_isdigit(c)) {
347                 width = width * 10 + c - '0';
348                 c = *++fmt;
349             }
350         }
351         if (c == '.') {
352             c = *++fmt;
353             if (c == '*') {
354                 prec = va_arg(args, int);
355                 c = *++fmt;
356             } else {
357                 while (qemu_isdigit(c)) {
358                     prec = prec * 10 + c - '0';
359                     c = *++fmt;
360                 }
361             }
362         }
363         /* modifiers */
364         switch(c) {
365         case 'l':
366             c = *++fmt;
367             break;
368         default:
369             break;
370         }
371         str = 0;
372         base = 0;
373         neg = 0;
374         ++fmt;
375         switch (c) {
376         case 'd':
377             i = va_arg(args, int);
378             if (i < 0) {
379                 neg = 1;
380                 val = -i;
381             } else
382                 val = i;
383             base = 10;
384             break;
385         case 'o':
386             val = va_arg(args, unsigned int);
387             base = 8;
388             break;
389         case 'x':
390         case 'X':
391             val = va_arg(args, unsigned int);
392             base = 16;
393             break;
394         case 'p':
395             val = (unsigned long) va_arg(args, void *);
396             base = 16;
397             neg = 2;
398             break;
399         case 's':
400             str = va_arg(args, char *);
401             break;
402         case 'c':
403             num[0] = va_arg(args, int);
404             num[1] = 0;
405             str = num;
406             break;
407         default:
408             *buf++ = '%';
409             if (c != '%')
410                 --fmt;          /* so %z outputs %z etc. */
411             --buflen;
412             continue;
413         }
414         if (base != 0) {
415             str = num + sizeof(num);
416             *--str = 0;
417             while (str > num + neg) {
418                 *--str = hexchars[val % base];
419                 val = val / base;
420                 if (--prec <= 0 && val == 0)
421                     break;
422             }
423             switch (neg) {
424             case 1:
425                 *--str = '-';
426                 break;
427             case 2:
428                 *--str = 'x';
429                 *--str = '0';
430                 break;
431             }
432             len = num + sizeof(num) - 1 - str;
433         } else {
434             len = strlen(str);
435             if (prec > 0 && len > prec)
436                 len = prec;
437         }
438         if (width > 0) {
439             if (width > buflen)
440                 width = buflen;
441             if ((n = width - len) > 0) {
442                 buflen -= n;
443                 for (; n > 0; --n)
444                     *buf++ = fillch;
445             }
446         }
447         if (len > buflen)
448             len = buflen;
449         memcpy(buf, str, len);
450         buf += len;
451         buflen -= len;
452     }
453     *buf = 0;
454     return buf - buf0;
455 }
456
457 void qemu_vprintf(const char *fmt, va_list ap)
458 {
459     char buf[1024];
460     int len;
461     
462     len = qemu_vsnprintf(buf, sizeof(buf), fmt, ap);
463     qemu_write(1, buf, len);
464 }
465
466 void qemu_printf(const char *fmt, ...)
467 {
468     va_list ap;
469     va_start(ap, fmt);
470     qemu_vprintf(fmt, ap);
471     va_end(ap);
472 }
473