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
32 #if defined(USE_KQEMU)
36 #if defined(__i386__) && !defined(CONFIG_SOFTMMU) && !defined(CONFIG_USER_ONLY)
41 /* When not using soft mmu, libc independant functions are needed for
42 the CPU core because it needs to use alternates stacks and
43 libc/thread incompatibles settings */
45 #include <linux/unistd.h>
47 #define QEMU_SYSCALL0(name) \
50 __asm__ volatile ("int $0x80" \
52 : "0" (__NR_##name)); \
56 #define QEMU_SYSCALL1(name,arg1) \
59 __asm__ volatile ("int $0x80" \
61 : "0" (__NR_##name),"b" ((long)(arg1))); \
65 #define QEMU_SYSCALL2(name,arg1,arg2) \
68 __asm__ volatile ("int $0x80" \
70 : "0" (__NR_##name),"b" ((long)(arg1)),"c" ((long)(arg2))); \
74 #define QEMU_SYSCALL3(name,arg1,arg2,arg3) \
77 __asm__ volatile ("int $0x80" \
79 : "0" (__NR_##name),"b" ((long)(arg1)),"c" ((long)(arg2)), \
80 "d" ((long)(arg3))); \
84 #define QEMU_SYSCALL4(name,arg1,arg2,arg3,arg4) \
87 __asm__ volatile ("int $0x80" \
89 : "0" (__NR_##name),"b" ((long)(arg1)),"c" ((long)(arg2)), \
90 "d" ((long)(arg3)),"S" ((long)(arg4))); \
94 #define QEMU_SYSCALL5(name,arg1,arg2,arg3,arg4,arg5) \
97 __asm__ volatile ("int $0x80" \
99 : "0" (__NR_##name),"b" ((long)(arg1)),"c" ((long)(arg2)), \
100 "d" ((long)(arg3)),"S" ((long)(arg4)),"D" ((long)(arg5))); \
104 #define QEMU_SYSCALL6(name,arg1,arg2,arg3,arg4,arg5,arg6) \
107 __asm__ volatile ("push %%ebp ; movl %%eax,%%ebp ; movl %1,%%eax ; int $0x80 ; pop %%ebp" \
109 : "i" (__NR_##name),"b" ((long)(arg1)),"c" ((long)(arg2)), \
110 "d" ((long)(arg3)),"S" ((long)(arg4)),"D" ((long)(arg5)), \
111 "0" ((long)(arg6))); \
115 int qemu_write(int fd
, const void *buf
, size_t n
)
117 QEMU_SYSCALL3(write
, fd
, buf
, n
);
122 /****************************************************************/
123 /* shmat replacement */
125 int qemu_ipc(int call
, unsigned long first
,
126 unsigned long second
, unsigned long third
,
127 void *ptr
, unsigned long fifth
)
129 QEMU_SYSCALL6(ipc
, call
, first
, second
, third
, ptr
, fifth
);
134 /* we must define shmat so that a specific address will be used when
135 mapping the X11 ximage */
136 void *shmat(int shmid
, const void *shmaddr
, int shmflg
)
140 /* we give an address in the right memory area */
142 shmaddr
= get_mmap_addr(8192 * 1024);
143 ret
= qemu_ipc(SHMAT
, shmid
, shmflg
, (unsigned long)&ptr
, (void *)shmaddr
, 0);
149 /****************************************************************/
150 /* sigaction bypassing the threads */
152 static int kernel_sigaction(int signum
, const struct qemu_sigaction
*act
,
153 struct qemu_sigaction
*oldact
,
156 QEMU_SYSCALL4(rt_sigaction
, signum
, act
, oldact
, sigsetsize
);
159 int qemu_sigaction(int signum
, const struct qemu_sigaction
*act
,
160 struct qemu_sigaction
*oldact
)
162 return kernel_sigaction(signum
, act
, oldact
, 8);
165 /****************************************************************/
166 /* memory allocation */
168 //#define DEBUG_MALLOC
170 #define MALLOC_BASE 0xab000000
171 #define PHYS_RAM_BASE 0xac000000
173 #define MALLOC_ALIGN 16
174 #define BLOCK_HEADER_SIZE 16
176 typedef struct MemoryBlock
{
177 struct MemoryBlock
*next
;
178 unsigned long size
; /* size of block, including header */
181 static MemoryBlock
*first_free_block
;
182 static unsigned long malloc_addr
= MALLOC_BASE
;
184 static void *malloc_get_space(size_t size
)
187 size
= TARGET_PAGE_ALIGN(size
);
188 ptr
= mmap((void *)malloc_addr
, size
,
189 PROT_WRITE
| PROT_READ
,
190 MAP_PRIVATE
| MAP_FIXED
| MAP_ANON
, -1, 0);
191 if (ptr
== MAP_FAILED
)
197 void *qemu_malloc(size_t size
)
199 MemoryBlock
*mb
, *mb1
, **pmb
;
201 size_t size1
, area_size
;
206 size
= (size
+ BLOCK_HEADER_SIZE
+ MALLOC_ALIGN
- 1) & ~(MALLOC_ALIGN
- 1);
207 pmb
= &first_free_block
;
212 if (size
<= mb
->size
)
216 /* no big enough blocks found: get new space */
217 area_size
= TARGET_PAGE_ALIGN(size
);
218 mb
= malloc_get_space(area_size
);
221 size1
= area_size
- size
;
223 /* create a new free block */
224 mb1
= (MemoryBlock
*)((uint8_t *)mb
+ size
);
231 /* a free block was found: use it */
232 size1
= mb
->size
- size
;
234 /* create a new free block */
235 mb1
= (MemoryBlock
*)((uint8_t *)mb
+ size
);
236 mb1
->next
= mb
->next
;
240 /* suppress the first block */
246 ptr
= ((uint8_t *)mb
+ BLOCK_HEADER_SIZE
);
248 qemu_printf("malloc: size=0x%x ptr=0x%lx\n", size
, (unsigned long)ptr
);
253 void qemu_free(void *ptr
)
259 mb
= (MemoryBlock
*)((uint8_t *)ptr
- BLOCK_HEADER_SIZE
);
260 mb
->next
= first_free_block
;
261 first_free_block
= mb
;
264 /****************************************************************/
265 /* virtual memory allocation */
267 unsigned long mmap_addr
= PHYS_RAM_BASE
;
269 void *get_mmap_addr(unsigned long size
)
273 mmap_addr
+= ((size
+ 4095) & ~4095) + 4096;
287 int qemu_write(int fd
, const void *buf
, size_t n
)
290 ret
= write(fd
, buf
, n
);
297 void *get_mmap_addr(unsigned long size
)
302 void qemu_free(void *ptr
)
307 void *qemu_malloc(size_t size
)
314 void *qemu_vmalloc(size_t size
)
316 /* FIXME: this is not exactly optimal solution since VirtualAlloc
317 has 64Kb granularity, but at least it guarantees us that the
318 memory is page aligned. */
319 return VirtualAlloc(NULL
, size
, MEM_COMMIT
, PAGE_READWRITE
);
322 void qemu_vfree(void *ptr
)
324 VirtualFree(ptr
, 0, MEM_RELEASE
);
329 #if defined(USE_KQEMU)
332 #include <sys/mman.h>
335 void *kqemu_vmalloc(size_t size
)
337 static int phys_ram_fd
= -1;
338 static int phys_ram_size
= 0;
340 char phys_ram_file
[1024];
344 if (phys_ram_fd
< 0) {
345 tmpdir
= getenv("QEMU_TMPDIR");
348 if (statfs(tmpdir
, &stfs
) == 0) {
353 free_space
= (int64_t)stfs
.f_bavail
* stfs
.f_bsize
;
354 if ((ram_size
+ 8192 * 1024) >= free_space
) {
355 ram_mb
= (ram_size
/ (1024 * 1024));
357 "You do not have enough space in '%s' for the %d MB of QEMU virtual RAM.\n",
359 if (strcmp(tmpdir
, "/dev/shm") == 0) {
360 fprintf(stderr
, "To have more space available provided you have enough RAM and swap, do as root:\n"
362 "mount -t tmpfs -o size=%dm none /dev/shm\n",
366 "Use the '-m' option of QEMU to diminish the amount of virtual RAM or use the\n"
367 "QEMU_TMPDIR environment variable to set another directory where the QEMU\n"
368 "temporary RAM file will be opened.\n");
370 fprintf(stderr
, "Or disable the accelerator module with -no-kqemu\n");
374 snprintf(phys_ram_file
, sizeof(phys_ram_file
), "%s/qemuXXXXXX",
376 if (mkstemp(phys_ram_file
) < 0) {
378 "warning: could not create temporary file in '%s'.\n"
379 "Use QEMU_TMPDIR to select a directory in a tmpfs filesystem.\n"
380 "Using '/tmp' as fallback.\n",
382 snprintf(phys_ram_file
, sizeof(phys_ram_file
), "%s/qemuXXXXXX",
384 if (mkstemp(phys_ram_file
) < 0) {
385 fprintf(stderr
, "Could not create temporary memory file '%s'\n",
390 phys_ram_fd
= open(phys_ram_file
, O_CREAT
| O_TRUNC
| O_RDWR
, 0600);
391 if (phys_ram_fd
< 0) {
392 fprintf(stderr
, "Could not open temporary memory file '%s'\n",
396 unlink(phys_ram_file
);
398 size
= (size
+ 4095) & ~4095;
399 ftruncate(phys_ram_fd
, phys_ram_size
+ size
);
402 PROT_WRITE
| PROT_READ
, MAP_SHARED
,
403 phys_ram_fd
, phys_ram_size
);
404 if (ptr
== MAP_FAILED
) {
405 fprintf(stderr
, "Could not map physical memory\n");
408 phys_ram_size
+= size
;
412 void kqemu_vfree(void *ptr
)
414 /* may be useful some day, but currently we do not need to free */
419 /* alloc shared memory pages */
420 void *qemu_vmalloc(size_t size
)
422 #if defined(USE_KQEMU)
424 return kqemu_vmalloc(size
);
429 return memalign(4096, size
);
433 void qemu_vfree(void *ptr
)
435 #if defined(USE_KQEMU)
446 void *qemu_mallocz(size_t size
)
449 ptr
= qemu_malloc(size
);
452 memset(ptr
, 0, size
);
456 char *qemu_strdup(const char *str
)
459 ptr
= qemu_malloc(strlen(str
) + 1);
466 /****************************************************************/
469 static inline int qemu_isdigit(int c
)
471 return c
>= '0' && c
<= '9';
474 #define OUTCHAR(c) (buflen > 0? (--buflen, *buf++ = (c)): 0)
476 /* from BSD ppp sources */
477 int qemu_vsnprintf(char *buf
, int buflen
, const char *fmt
, va_list args
)
480 int width
, prec
, fillch
;
482 unsigned long val
= 0;
486 static const char hexchars
[] = "0123456789abcdef";
491 for (f
= fmt
; *f
!= '%' && *f
!= 0; ++f
)
497 memcpy(buf
, fmt
, len
);
512 width
= va_arg(args
, int);
515 while (qemu_isdigit(c
)) {
516 width
= width
* 10 + c
- '0';
523 prec
= va_arg(args
, int);
526 while (qemu_isdigit(c
)) {
527 prec
= prec
* 10 + c
- '0';
546 i
= va_arg(args
, int);
555 val
= va_arg(args
, unsigned int);
560 val
= va_arg(args
, unsigned int);
564 val
= (unsigned long) va_arg(args
, void *);
569 str
= va_arg(args
, char *);
572 num
[0] = va_arg(args
, int);
579 --fmt
; /* so %z outputs %z etc. */
584 str
= num
+ sizeof(num
);
586 while (str
> num
+ neg
) {
587 *--str
= hexchars
[val
% base
];
589 if (--prec
<= 0 && val
== 0)
601 len
= num
+ sizeof(num
) - 1 - str
;
604 if (prec
> 0 && len
> prec
)
610 if ((n
= width
- len
) > 0) {
618 memcpy(buf
, str
, len
);
626 void qemu_vprintf(const char *fmt
, va_list ap
)
631 len
= qemu_vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
632 qemu_write(1, buf
, len
);
635 void qemu_printf(const char *fmt
, ...)
639 qemu_vprintf(fmt
, ap
);