2 * QEMU low level functions
4 * Copyright (c) 2003 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
33 #if defined(__i386__) && !defined(CONFIG_SOFTMMU) && !defined(CONFIG_USER_ONLY)
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 */
42 #include <linux/unistd.h>
44 #define QEMU_SYSCALL0(name) \
47 __asm__ volatile ("int $0x80" \
49 : "0" (__NR_##name)); \
53 #define QEMU_SYSCALL1(name,arg1) \
56 __asm__ volatile ("int $0x80" \
58 : "0" (__NR_##name),"b" ((long)(arg1))); \
62 #define QEMU_SYSCALL2(name,arg1,arg2) \
65 __asm__ volatile ("int $0x80" \
67 : "0" (__NR_##name),"b" ((long)(arg1)),"c" ((long)(arg2))); \
71 #define QEMU_SYSCALL3(name,arg1,arg2,arg3) \
74 __asm__ volatile ("int $0x80" \
76 : "0" (__NR_##name),"b" ((long)(arg1)),"c" ((long)(arg2)), \
77 "d" ((long)(arg3))); \
81 #define QEMU_SYSCALL4(name,arg1,arg2,arg3,arg4) \
84 __asm__ volatile ("int $0x80" \
86 : "0" (__NR_##name),"b" ((long)(arg1)),"c" ((long)(arg2)), \
87 "d" ((long)(arg3)),"S" ((long)(arg4))); \
91 #define QEMU_SYSCALL5(name,arg1,arg2,arg3,arg4,arg5) \
94 __asm__ volatile ("int $0x80" \
96 : "0" (__NR_##name),"b" ((long)(arg1)),"c" ((long)(arg2)), \
97 "d" ((long)(arg3)),"S" ((long)(arg4)),"D" ((long)(arg5))); \
101 #define QEMU_SYSCALL6(name,arg1,arg2,arg3,arg4,arg5,arg6) \
104 __asm__ volatile ("push %%ebp ; movl %%eax,%%ebp ; movl %1,%%eax ; int $0x80 ; pop %%ebp" \
106 : "i" (__NR_##name),"b" ((long)(arg1)),"c" ((long)(arg2)), \
107 "d" ((long)(arg3)),"S" ((long)(arg4)),"D" ((long)(arg5)), \
108 "0" ((long)(arg6))); \
112 int qemu_write(int fd, const void *buf, size_t n)
114 QEMU_SYSCALL3(write, fd, buf, n);
119 /****************************************************************/
120 /* shmat replacement */
122 int qemu_ipc(int call, unsigned long first,
123 unsigned long second, unsigned long third,
124 void *ptr, unsigned long fifth)
126 QEMU_SYSCALL6(ipc, call, first, second, third, ptr, fifth);
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)
137 /* we give an address in the right memory area */
139 shmaddr = get_mmap_addr(8192 * 1024);
140 ret = qemu_ipc(SHMAT, shmid, shmflg, (unsigned long)&ptr, (void *)shmaddr, 0);
146 /****************************************************************/
147 /* sigaction bypassing the threads */
149 static int kernel_sigaction(int signum, const struct qemu_sigaction *act,
150 struct qemu_sigaction *oldact,
153 QEMU_SYSCALL4(rt_sigaction, signum, act, oldact, sigsetsize);
156 int qemu_sigaction(int signum, const struct qemu_sigaction *act,
157 struct qemu_sigaction *oldact)
159 return kernel_sigaction(signum, act, oldact, 8);
162 /****************************************************************/
163 /* memory allocation */
165 //#define DEBUG_MALLOC
167 #define MALLOC_BASE 0xab000000
168 #define PHYS_RAM_BASE 0xac000000
170 #define MALLOC_ALIGN 16
171 #define BLOCK_HEADER_SIZE 16
173 typedef struct MemoryBlock {
174 struct MemoryBlock *next;
175 unsigned long size; /* size of block, including header */
178 static MemoryBlock *first_free_block;
179 static unsigned long malloc_addr = MALLOC_BASE;
181 static void *malloc_get_space(size_t size)
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)
194 void *qemu_malloc(size_t size)
196 MemoryBlock *mb, *mb1, **pmb;
198 size_t size1, area_size;
203 size = (size + BLOCK_HEADER_SIZE + MALLOC_ALIGN - 1) & ~(MALLOC_ALIGN - 1);
204 pmb = &first_free_block;
209 if (size <= mb->size)
213 /* no big enough blocks found: get new space */
214 area_size = TARGET_PAGE_ALIGN(size);
215 mb = malloc_get_space(area_size);
218 size1 = area_size - size;
220 /* create a new free block */
221 mb1 = (MemoryBlock *)((uint8_t *)mb + size);
228 /* a free block was found: use it */
229 size1 = mb->size - size;
231 /* create a new free block */
232 mb1 = (MemoryBlock *)((uint8_t *)mb + size);
233 mb1->next = mb->next;
237 /* suppress the first block */
243 ptr = ((uint8_t *)mb + BLOCK_HEADER_SIZE);
245 qemu_printf("malloc: size=0x%x ptr=0x%lx\n", size, (unsigned long)ptr);
250 void qemu_free(void *ptr)
256 mb = (MemoryBlock *)((uint8_t *)ptr - BLOCK_HEADER_SIZE);
257 mb->next = first_free_block;
258 first_free_block = mb;
261 /****************************************************************/
262 /* virtual memory allocation */
264 unsigned long mmap_addr = PHYS_RAM_BASE;
266 void *get_mmap_addr(unsigned long size)
270 mmap_addr += ((size + 4095) & ~4095) + 4096;
282 /* XXX: find a solution to have page aligned data */
283 #define memalign(align, size) malloc(size)
286 int qemu_write(int fd, const void *buf, size_t n)
289 ret = write(fd, buf, n);
296 void *get_mmap_addr(unsigned long size)
301 void qemu_free(void *ptr)
306 void *qemu_malloc(size_t size)
311 #if defined(USE_KQEMU)
313 #include <sys/mman.h>
316 void *qemu_vmalloc(size_t size)
318 static int phys_ram_fd = -1;
319 static int phys_ram_size = 0;
321 char phys_ram_file[1024];
324 if (phys_ram_fd < 0) {
325 tmpdir = getenv("QEMU_TMPDIR");
328 snprintf(phys_ram_file, sizeof(phys_ram_file), "%s/qemuXXXXXX",
330 if (mkstemp(phys_ram_file) < 0) {
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",
336 snprintf(phys_ram_file, sizeof(phys_ram_file), "%s/qemuXXXXXX",
338 if (mkstemp(phys_ram_file) < 0) {
339 fprintf(stderr, "Could not create temporary memory file '%s'\n",
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",
350 unlink(phys_ram_file);
352 size = (size + 4095) & ~4095;
353 ftruncate(phys_ram_fd, phys_ram_size + 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");
362 phys_ram_size += size;
366 void qemu_vfree(void *ptr)
368 /* may be useful some day, but currently we do not need to free */
373 /* alloc shared memory pages */
374 void *qemu_vmalloc(size_t size)
379 return memalign(4096, size);
383 void qemu_vfree(void *ptr)
392 void *qemu_mallocz(size_t size)
395 ptr = qemu_malloc(size);
398 memset(ptr, 0, size);
402 char *qemu_strdup(const char *str)
405 ptr = qemu_malloc(strlen(str) + 1);
412 /****************************************************************/
415 static inline int qemu_isdigit(int c)
417 return c >= '0' && c <= '9';
420 #define OUTCHAR(c) (buflen > 0? (--buflen, *buf++ = (c)): 0)
422 /* from BSD ppp sources */
423 int qemu_vsnprintf(char *buf, int buflen, const char *fmt, va_list args)
426 int width, prec, fillch;
428 unsigned long val = 0;
432 static const char hexchars[] = "0123456789abcdef";
437 for (f = fmt; *f != '%' && *f != 0; ++f)
443 memcpy(buf, fmt, len);
458 width = va_arg(args, int);
461 while (qemu_isdigit(c)) {
462 width = width * 10 + c - '0';
469 prec = va_arg(args, int);
472 while (qemu_isdigit(c)) {
473 prec = prec * 10 + c - '0';
492 i = va_arg(args, int);
501 val = va_arg(args, unsigned int);
506 val = va_arg(args, unsigned int);
510 val = (unsigned long) va_arg(args, void *);
515 str = va_arg(args, char *);
518 num[0] = va_arg(args, int);
525 --fmt; /* so %z outputs %z etc. */
530 str = num + sizeof(num);
532 while (str > num + neg) {
533 *--str = hexchars[val % base];
535 if (--prec <= 0 && val == 0)
547 len = num + sizeof(num) - 1 - str;
550 if (prec > 0 && len > prec)
556 if ((n = width - len) > 0) {
564 memcpy(buf, str, len);
572 void qemu_vprintf(const char *fmt, va_list ap)
577 len = qemu_vsnprintf(buf, sizeof(buf), fmt, ap);
578 qemu_write(1, buf, len);
581 void qemu_printf(const char *fmt, ...)
585 qemu_vprintf(fmt, ap);