]> git.proxmox.com Git - qemu.git/blame - osdep.c
update
[qemu.git] / osdep.c
CommitLineData
ea88812f
FB
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 <sys/mman.h>
29#include <sys/ipc.h>
30#include <errno.h>
31#include <unistd.h>
32
33#include "cpu.h"
34
35#if defined(__i386__) && !defined(CONFIG_SOFTMMU) && !defined(CONFIG_USER_ONLY)
36
37/* When not using soft mmu, libc independant functions are needed for
38 the CPU core because it needs to use alternates stacks and
39 libc/thread incompatibles settings */
40
41#include <linux/unistd.h>
42
43#define QEMU_SYSCALL0(name) \
44{ \
45long __res; \
46__asm__ volatile ("int $0x80" \
47 : "=a" (__res) \
48 : "0" (__NR_##name)); \
49return __res; \
50}
51
52#define QEMU_SYSCALL1(name,arg1) \
53{ \
54long __res; \
55__asm__ volatile ("int $0x80" \
56 : "=a" (__res) \
57 : "0" (__NR_##name),"b" ((long)(arg1))); \
58return __res; \
59}
60
61#define QEMU_SYSCALL2(name,arg1,arg2) \
62{ \
63long __res; \
64__asm__ volatile ("int $0x80" \
65 : "=a" (__res) \
66 : "0" (__NR_##name),"b" ((long)(arg1)),"c" ((long)(arg2))); \
67return __res; \
68}
69
70#define QEMU_SYSCALL3(name,arg1,arg2,arg3) \
71{ \
72long __res; \
73__asm__ volatile ("int $0x80" \
74 : "=a" (__res) \
75 : "0" (__NR_##name),"b" ((long)(arg1)),"c" ((long)(arg2)), \
76 "d" ((long)(arg3))); \
77return __res; \
78}
79
80#define QEMU_SYSCALL4(name,arg1,arg2,arg3,arg4) \
81{ \
82long __res; \
83__asm__ volatile ("int $0x80" \
84 : "=a" (__res) \
85 : "0" (__NR_##name),"b" ((long)(arg1)),"c" ((long)(arg2)), \
86 "d" ((long)(arg3)),"S" ((long)(arg4))); \
87return __res; \
88}
89
90#define QEMU_SYSCALL5(name,arg1,arg2,arg3,arg4,arg5) \
91{ \
92long __res; \
93__asm__ volatile ("int $0x80" \
94 : "=a" (__res) \
95 : "0" (__NR_##name),"b" ((long)(arg1)),"c" ((long)(arg2)), \
96 "d" ((long)(arg3)),"S" ((long)(arg4)),"D" ((long)(arg5))); \
97return __res; \
98}
99
100#define QEMU_SYSCALL6(name,arg1,arg2,arg3,arg4,arg5,arg6) \
101{ \
102long __res; \
103__asm__ volatile ("push %%ebp ; movl %%eax,%%ebp ; movl %1,%%eax ; int $0x80 ; pop %%ebp" \
104 : "=a" (__res) \
105 : "i" (__NR_##name),"b" ((long)(arg1)),"c" ((long)(arg2)), \
106 "d" ((long)(arg3)),"S" ((long)(arg4)),"D" ((long)(arg5)), \
107 "0" ((long)(arg6))); \
108return __res; \
109}
110
111int qemu_write(int fd, const void *buf, size_t n)
112{
113 QEMU_SYSCALL3(write, fd, buf, n);
114}
115
116
117
118/****************************************************************/
119/* shmat replacement */
120
121int qemu_ipc(int call, unsigned long first,
122 unsigned long second, unsigned long third,
123 void *ptr, unsigned long fifth)
124{
125 QEMU_SYSCALL6(ipc, call, first, second, third, ptr, fifth);
126}
127
128#define SHMAT 21
129
130/* we must define shmat so that a specific address will be used when
131 mapping the X11 ximage */
132void *shmat(int shmid, const void *shmaddr, int shmflg)
133{
134 void *ptr;
135 int ret;
136 /* we give an address in the right memory area */
137 if (!shmaddr)
138 shmaddr = get_mmap_addr(8192 * 1024);
139 ret = qemu_ipc(SHMAT, shmid, shmflg, (unsigned long)&ptr, (void *)shmaddr, 0);
140 if (ret < 0)
141 return NULL;
142 return ptr;
143}
144
145/****************************************************************/
146/* memory allocation */
147
148//#define DEBUG_MALLOC
149
150#define MALLOC_BASE 0xab000000
151#define PHYS_RAM_BASE 0xac000000
152
153#define MALLOC_ALIGN 16
154#define BLOCK_HEADER_SIZE 16
155
156typedef struct MemoryBlock {
157 struct MemoryBlock *next;
158 unsigned long size; /* size of block, including header */
159} MemoryBlock;
160
161static MemoryBlock *first_free_block;
162static unsigned long malloc_addr = MALLOC_BASE;
163
164static void *malloc_get_space(size_t size)
165{
166 void *ptr;
167 size = TARGET_PAGE_ALIGN(size);
168 ptr = mmap((void *)malloc_addr, size,
169 PROT_WRITE | PROT_READ,
170 MAP_PRIVATE | MAP_FIXED | MAP_ANON, -1, 0);
171 if (ptr == MAP_FAILED)
172 return NULL;
173 malloc_addr += size;
174 return ptr;
175}
176
177void *qemu_malloc(size_t size)
178{
179 MemoryBlock *mb, *mb1, **pmb;
180 void *ptr;
181 size_t size1, area_size;
182
183 if (size == 0)
184 return NULL;
185
186 size = (size + BLOCK_HEADER_SIZE + MALLOC_ALIGN - 1) & ~(MALLOC_ALIGN - 1);
187 pmb = &first_free_block;
188 for(;;) {
189 mb = *pmb;
190 if (mb == NULL)
191 break;
192 if (size <= mb->size)
193 goto found;
194 pmb = &mb->next;
195 }
196 /* no big enough blocks found: get new space */
197 area_size = TARGET_PAGE_ALIGN(size);
198 mb = malloc_get_space(area_size);
199 if (!mb)
200 return NULL;
201 size1 = area_size - size;
202 if (size1 > 0) {
203 /* create a new free block */
204 mb1 = (MemoryBlock *)((uint8_t *)mb + size);
205 mb1->next = NULL;
206 mb1->size = size1;
207 *pmb = mb1;
208 }
209 goto the_end;
210 found:
211 /* a free block was found: use it */
212 size1 = mb->size - size;
213 if (size1 > 0) {
214 /* create a new free block */
215 mb1 = (MemoryBlock *)((uint8_t *)mb + size);
216 mb1->next = mb->next;
217 mb1->size = size1;
218 *pmb = mb1;
219 } else {
220 /* suppress the first block */
221 *pmb = mb->next;
222 }
223 the_end:
224 mb->size = size;
225 mb->next = NULL;
226 ptr = ((uint8_t *)mb + BLOCK_HEADER_SIZE);
227#ifdef DEBUG_MALLOC
228 qemu_printf("malloc: size=0x%x ptr=0x%lx\n", size, (unsigned long)ptr);
229#endif
230 return ptr;
231}
232
233void qemu_free(void *ptr)
234{
235 MemoryBlock *mb;
236
237 mb = (MemoryBlock *)((uint8_t *)ptr - BLOCK_HEADER_SIZE);
238 mb->next = first_free_block;
239 first_free_block = mb;
240}
241
242/****************************************************************/
243/* virtual memory allocation */
244
245unsigned long mmap_addr = PHYS_RAM_BASE;
246
247void *get_mmap_addr(unsigned long size)
248{
249 unsigned long addr;
250 addr = mmap_addr;
251 mmap_addr += ((size + 4095) & ~4095) + 4096;
252 return (void *)addr;
253}
254
255#else
256
257int qemu_write(int fd, const void *buf, size_t n)
258{
259 int ret;
260 ret = write(fd, buf, n);
261 if (ret < 0)
262 return -errno;
263 else
264 return ret;
265}
266
267void *get_mmap_addr(unsigned long size)
268{
269 return NULL;
270}
271
272void qemu_free(void *ptr)
273{
274 free(ptr);
275}
276
277void *qemu_malloc(size_t size)
278{
279 return malloc(size);
280}
281
282#endif
283
07d89866
FB
284void *qemu_mallocz(size_t size)
285{
286 void *ptr;
287 ptr = qemu_malloc(size);
288 if (!ptr)
289 return NULL;
290 memset(ptr, 0, size);
291 return ptr;
292}
293
ea88812f
FB
294/****************************************************************/
295/* printf support */
296
297static inline int qemu_isdigit(int c)
298{
299 return c >= '0' && c <= '9';
300}
301
302#define OUTCHAR(c) (buflen > 0? (--buflen, *buf++ = (c)): 0)
303
304/* from BSD ppp sources */
305int qemu_vsnprintf(char *buf, int buflen, const char *fmt, va_list args)
306{
307 int c, i, n;
308 int width, prec, fillch;
309 int base, len, neg;
310 unsigned long val = 0;
311 const char *f;
312 char *str, *buf0;
313 char num[32];
314 static const char hexchars[] = "0123456789abcdef";
315
316 buf0 = buf;
317 --buflen;
318 while (buflen > 0) {
319 for (f = fmt; *f != '%' && *f != 0; ++f)
320 ;
321 if (f > fmt) {
322 len = f - fmt;
323 if (len > buflen)
324 len = buflen;
325 memcpy(buf, fmt, len);
326 buf += len;
327 buflen -= len;
328 fmt = f;
329 }
330 if (*fmt == 0)
331 break;
332 c = *++fmt;
333 width = prec = 0;
334 fillch = ' ';
335 if (c == '0') {
336 fillch = '0';
337 c = *++fmt;
338 }
339 if (c == '*') {
340 width = va_arg(args, int);
341 c = *++fmt;
342 } else {
343 while (qemu_isdigit(c)) {
344 width = width * 10 + c - '0';
345 c = *++fmt;
346 }
347 }
348 if (c == '.') {
349 c = *++fmt;
350 if (c == '*') {
351 prec = va_arg(args, int);
352 c = *++fmt;
353 } else {
354 while (qemu_isdigit(c)) {
355 prec = prec * 10 + c - '0';
356 c = *++fmt;
357 }
358 }
359 }
360 /* modifiers */
361 switch(c) {
362 case 'l':
363 c = *++fmt;
364 break;
365 default:
366 break;
367 }
368 str = 0;
369 base = 0;
370 neg = 0;
371 ++fmt;
372 switch (c) {
373 case 'd':
374 i = va_arg(args, int);
375 if (i < 0) {
376 neg = 1;
377 val = -i;
378 } else
379 val = i;
380 base = 10;
381 break;
382 case 'o':
383 val = va_arg(args, unsigned int);
384 base = 8;
385 break;
386 case 'x':
387 case 'X':
388 val = va_arg(args, unsigned int);
389 base = 16;
390 break;
391 case 'p':
392 val = (unsigned long) va_arg(args, void *);
393 base = 16;
394 neg = 2;
395 break;
396 case 's':
397 str = va_arg(args, char *);
398 break;
399 case 'c':
400 num[0] = va_arg(args, int);
401 num[1] = 0;
402 str = num;
403 break;
404 default:
405 *buf++ = '%';
406 if (c != '%')
407 --fmt; /* so %z outputs %z etc. */
408 --buflen;
409 continue;
410 }
411 if (base != 0) {
412 str = num + sizeof(num);
413 *--str = 0;
414 while (str > num + neg) {
415 *--str = hexchars[val % base];
416 val = val / base;
417 if (--prec <= 0 && val == 0)
418 break;
419 }
420 switch (neg) {
421 case 1:
422 *--str = '-';
423 break;
424 case 2:
425 *--str = 'x';
426 *--str = '0';
427 break;
428 }
429 len = num + sizeof(num) - 1 - str;
430 } else {
431 len = strlen(str);
432 if (prec > 0 && len > prec)
433 len = prec;
434 }
435 if (width > 0) {
436 if (width > buflen)
437 width = buflen;
438 if ((n = width - len) > 0) {
439 buflen -= n;
440 for (; n > 0; --n)
441 *buf++ = fillch;
442 }
443 }
444 if (len > buflen)
445 len = buflen;
446 memcpy(buf, str, len);
447 buf += len;
448 buflen -= len;
449 }
450 *buf = 0;
451 return buf - buf0;
452}
453
454void qemu_vprintf(const char *fmt, va_list ap)
455{
456 char buf[1024];
457 int len;
458
459 len = qemu_vsnprintf(buf, sizeof(buf), fmt, ap);
460 qemu_write(1, buf, len);
461}
462
463void qemu_printf(const char *fmt, ...)
464{
465 va_list ap;
466 va_start(ap, fmt);
467 qemu_vprintf(fmt, ap);
468 va_end(ap);
469}
470