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
35 #if defined(__i386__) && !defined(CONFIG_SOFTMMU) && !defined(CONFIG_USER_ONLY)
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 */
41 #include <linux/unistd.h>
43 #define QEMU_SYSCALL0(name) \
46 __asm__ volatile ("int $0x80" \
48 : "0" (__NR_##name)); \
52 #define QEMU_SYSCALL1(name,arg1) \
55 __asm__ volatile ("int $0x80" \
57 : "0" (__NR_##name),"b" ((long)(arg1))); \
61 #define QEMU_SYSCALL2(name,arg1,arg2) \
64 __asm__ volatile ("int $0x80" \
66 : "0" (__NR_##name),"b" ((long)(arg1)),"c" ((long)(arg2))); \
70 #define QEMU_SYSCALL3(name,arg1,arg2,arg3) \
73 __asm__ volatile ("int $0x80" \
75 : "0" (__NR_##name),"b" ((long)(arg1)),"c" ((long)(arg2)), \
76 "d" ((long)(arg3))); \
80 #define QEMU_SYSCALL4(name,arg1,arg2,arg3,arg4) \
83 __asm__ volatile ("int $0x80" \
85 : "0" (__NR_##name),"b" ((long)(arg1)),"c" ((long)(arg2)), \
86 "d" ((long)(arg3)),"S" ((long)(arg4))); \
90 #define QEMU_SYSCALL5(name,arg1,arg2,arg3,arg4,arg5) \
93 __asm__ volatile ("int $0x80" \
95 : "0" (__NR_##name),"b" ((long)(arg1)),"c" ((long)(arg2)), \
96 "d" ((long)(arg3)),"S" ((long)(arg4)),"D" ((long)(arg5))); \
100 #define QEMU_SYSCALL6(name,arg1,arg2,arg3,arg4,arg5,arg6) \
103 __asm__ volatile ("push %%ebp ; movl %%eax,%%ebp ; movl %1,%%eax ; int $0x80 ; pop %%ebp" \
105 : "i" (__NR_##name),"b" ((long)(arg1)),"c" ((long)(arg2)), \
106 "d" ((long)(arg3)),"S" ((long)(arg4)),"D" ((long)(arg5)), \
107 "0" ((long)(arg6))); \
111 int qemu_write(int fd
, const void *buf
, size_t n
)
113 QEMU_SYSCALL3(write
, fd
, buf
, n
);
118 /****************************************************************/
119 /* shmat replacement */
121 int qemu_ipc(int call
, unsigned long first
,
122 unsigned long second
, unsigned long third
,
123 void *ptr
, unsigned long fifth
)
125 QEMU_SYSCALL6(ipc
, call
, first
, second
, third
, ptr
, fifth
);
130 /* we must define shmat so that a specific address will be used when
131 mapping the X11 ximage */
132 void *shmat(int shmid
, const void *shmaddr
, int shmflg
)
136 /* we give an address in the right memory area */
138 shmaddr
= get_mmap_addr(8192 * 1024);
139 ret
= qemu_ipc(SHMAT
, shmid
, shmflg
, (unsigned long)&ptr
, (void *)shmaddr
, 0);
145 /****************************************************************/
146 /* memory allocation */
148 //#define DEBUG_MALLOC
150 #define MALLOC_BASE 0xab000000
151 #define PHYS_RAM_BASE 0xac000000
153 #define MALLOC_ALIGN 16
154 #define BLOCK_HEADER_SIZE 16
156 typedef struct MemoryBlock
{
157 struct MemoryBlock
*next
;
158 unsigned long size
; /* size of block, including header */
161 static MemoryBlock
*first_free_block
;
162 static unsigned long malloc_addr
= MALLOC_BASE
;
164 static void *malloc_get_space(size_t size
)
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
)
177 void *qemu_malloc(size_t size
)
179 MemoryBlock
*mb
, *mb1
, **pmb
;
181 size_t size1
, area_size
;
186 size
= (size
+ BLOCK_HEADER_SIZE
+ MALLOC_ALIGN
- 1) & ~(MALLOC_ALIGN
- 1);
187 pmb
= &first_free_block
;
192 if (size
<= mb
->size
)
196 /* no big enough blocks found: get new space */
197 area_size
= TARGET_PAGE_ALIGN(size
);
198 mb
= malloc_get_space(area_size
);
201 size1
= area_size
- size
;
203 /* create a new free block */
204 mb1
= (MemoryBlock
*)((uint8_t *)mb
+ size
);
211 /* a free block was found: use it */
212 size1
= mb
->size
- size
;
214 /* create a new free block */
215 mb1
= (MemoryBlock
*)((uint8_t *)mb
+ size
);
216 mb1
->next
= mb
->next
;
220 /* suppress the first block */
226 ptr
= ((uint8_t *)mb
+ BLOCK_HEADER_SIZE
);
228 qemu_printf("malloc: size=0x%x ptr=0x%lx\n", size
, (unsigned long)ptr
);
233 void qemu_free(void *ptr
)
237 mb
= (MemoryBlock
*)((uint8_t *)ptr
- BLOCK_HEADER_SIZE
);
238 mb
->next
= first_free_block
;
239 first_free_block
= mb
;
242 /****************************************************************/
243 /* virtual memory allocation */
245 unsigned long mmap_addr
= PHYS_RAM_BASE
;
247 void *get_mmap_addr(unsigned long size
)
251 mmap_addr
+= ((size
+ 4095) & ~4095) + 4096;
257 int qemu_write(int fd
, const void *buf
, size_t n
)
260 ret
= write(fd
, buf
, n
);
267 void *get_mmap_addr(unsigned long size
)
272 void qemu_free(void *ptr
)
277 void *qemu_malloc(size_t size
)
284 void *qemu_mallocz(size_t size
)
287 ptr
= qemu_malloc(size
);
290 memset(ptr
, 0, size
);
294 /****************************************************************/
297 static inline int qemu_isdigit(int c
)
299 return c
>= '0' && c
<= '9';
302 #define OUTCHAR(c) (buflen > 0? (--buflen, *buf++ = (c)): 0)
304 /* from BSD ppp sources */
305 int qemu_vsnprintf(char *buf
, int buflen
, const char *fmt
, va_list args
)
308 int width
, prec
, fillch
;
310 unsigned long val
= 0;
314 static const char hexchars
[] = "0123456789abcdef";
319 for (f
= fmt
; *f
!= '%' && *f
!= 0; ++f
)
325 memcpy(buf
, fmt
, len
);
340 width
= va_arg(args
, int);
343 while (qemu_isdigit(c
)) {
344 width
= width
* 10 + c
- '0';
351 prec
= va_arg(args
, int);
354 while (qemu_isdigit(c
)) {
355 prec
= prec
* 10 + c
- '0';
374 i
= va_arg(args
, int);
383 val
= va_arg(args
, unsigned int);
388 val
= va_arg(args
, unsigned int);
392 val
= (unsigned long) va_arg(args
, void *);
397 str
= va_arg(args
, char *);
400 num
[0] = va_arg(args
, int);
407 --fmt
; /* so %z outputs %z etc. */
412 str
= num
+ sizeof(num
);
414 while (str
> num
+ neg
) {
415 *--str
= hexchars
[val
% base
];
417 if (--prec
<= 0 && val
== 0)
429 len
= num
+ sizeof(num
) - 1 - str
;
432 if (prec
> 0 && len
> prec
)
438 if ((n
= width
- len
) > 0) {
446 memcpy(buf
, str
, len
);
454 void qemu_vprintf(const char *fmt
, va_list ap
)
459 len
= qemu_vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
460 qemu_write(1, buf
, len
);
463 void qemu_printf(const char *fmt
, ...)
467 qemu_vprintf(fmt
, ap
);