]>
git.proxmox.com Git - qemu.git/blob - osdep.c
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 /* memory allocation */
149 //#define DEBUG_MALLOC
151 #define MALLOC_BASE 0xab000000
152 #define PHYS_RAM_BASE 0xac000000
154 #define MALLOC_ALIGN 16
155 #define BLOCK_HEADER_SIZE 16
157 typedef struct MemoryBlock
{
158 struct MemoryBlock
*next
;
159 unsigned long size
; /* size of block, including header */
162 static MemoryBlock
*first_free_block
;
163 static unsigned long malloc_addr
= MALLOC_BASE
;
165 static void *malloc_get_space(size_t size
)
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
)
178 void *qemu_malloc(size_t size
)
180 MemoryBlock
*mb
, *mb1
, **pmb
;
182 size_t size1
, area_size
;
187 size
= (size
+ BLOCK_HEADER_SIZE
+ MALLOC_ALIGN
- 1) & ~(MALLOC_ALIGN
- 1);
188 pmb
= &first_free_block
;
193 if (size
<= mb
->size
)
197 /* no big enough blocks found: get new space */
198 area_size
= TARGET_PAGE_ALIGN(size
);
199 mb
= malloc_get_space(area_size
);
202 size1
= area_size
- size
;
204 /* create a new free block */
205 mb1
= (MemoryBlock
*)((uint8_t *)mb
+ size
);
212 /* a free block was found: use it */
213 size1
= mb
->size
- size
;
215 /* create a new free block */
216 mb1
= (MemoryBlock
*)((uint8_t *)mb
+ size
);
217 mb1
->next
= mb
->next
;
221 /* suppress the first block */
227 ptr
= ((uint8_t *)mb
+ BLOCK_HEADER_SIZE
);
229 qemu_printf("malloc: size=0x%x ptr=0x%lx\n", size
, (unsigned long)ptr
);
234 void qemu_free(void *ptr
)
240 mb
= (MemoryBlock
*)((uint8_t *)ptr
- BLOCK_HEADER_SIZE
);
241 mb
->next
= first_free_block
;
242 first_free_block
= mb
;
245 /****************************************************************/
246 /* virtual memory allocation */
248 unsigned long mmap_addr
= PHYS_RAM_BASE
;
250 void *get_mmap_addr(unsigned long size
)
254 mmap_addr
+= ((size
+ 4095) & ~4095) + 4096;
260 int qemu_write(int fd
, const void *buf
, size_t n
)
263 ret
= write(fd
, buf
, n
);
270 void *get_mmap_addr(unsigned long size
)
275 void qemu_free(void *ptr
)
280 void *qemu_malloc(size_t size
)
287 void *qemu_mallocz(size_t size
)
290 ptr
= qemu_malloc(size
);
293 memset(ptr
, 0, size
);
297 /****************************************************************/
300 static inline int qemu_isdigit(int c
)
302 return c
>= '0' && c
<= '9';
305 #define OUTCHAR(c) (buflen > 0? (--buflen, *buf++ = (c)): 0)
307 /* from BSD ppp sources */
308 int qemu_vsnprintf(char *buf
, int buflen
, const char *fmt
, va_list args
)
311 int width
, prec
, fillch
;
313 unsigned long val
= 0;
317 static const char hexchars
[] = "0123456789abcdef";
322 for (f
= fmt
; *f
!= '%' && *f
!= 0; ++f
)
328 memcpy(buf
, fmt
, len
);
343 width
= va_arg(args
, int);
346 while (qemu_isdigit(c
)) {
347 width
= width
* 10 + c
- '0';
354 prec
= va_arg(args
, int);
357 while (qemu_isdigit(c
)) {
358 prec
= prec
* 10 + c
- '0';
377 i
= va_arg(args
, int);
386 val
= va_arg(args
, unsigned int);
391 val
= va_arg(args
, unsigned int);
395 val
= (unsigned long) va_arg(args
, void *);
400 str
= va_arg(args
, char *);
403 num
[0] = va_arg(args
, int);
410 --fmt
; /* so %z outputs %z etc. */
415 str
= num
+ sizeof(num
);
417 while (str
> num
+ neg
) {
418 *--str
= hexchars
[val
% base
];
420 if (--prec
<= 0 && val
== 0)
432 len
= num
+ sizeof(num
) - 1 - str
;
435 if (prec
> 0 && len
> prec
)
441 if ((n
= width
- len
) > 0) {
449 memcpy(buf
, str
, len
);
457 void qemu_vprintf(const char *fmt
, va_list ap
)
462 len
= qemu_vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
463 qemu_write(1, buf
, len
);
466 void qemu_printf(const char *fmt
, ...)
470 qemu_vprintf(fmt
, ap
);