debuggers.hg

view tools/ioemu/osdep.c @ 0:7d21f7218375

Exact replica of unstable on 051908 + README-this
author Mukesh Rathor
date Mon May 19 15:34:57 2008 -0700 (2008-05-19)
parents
children
line source
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 */
25 #include "config-host.h"
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <stdarg.h>
29 #include <string.h>
30 #include <errno.h>
31 #include <unistd.h>
32 #ifdef HOST_SOLARIS
33 #include <sys/types.h>
34 #include <sys/statvfs.h>
35 #endif
37 #if defined(USE_KQEMU)
38 #include "vl.h"
39 #endif
41 #ifdef _WIN32
42 #include <windows.h>
43 #elif defined(_BSD)
44 #include <stdlib.h>
45 #else
46 #include <malloc.h>
47 #endif
49 void *get_mmap_addr(unsigned long size)
50 {
51 return NULL;
52 }
54 void qemu_free(void *ptr)
55 {
56 free(ptr);
57 }
59 void *qemu_malloc(size_t size)
60 {
61 return malloc(size);
62 }
64 #if defined(_WIN32)
65 void *qemu_memalign(size_t alignment, size_t size)
66 {
67 return VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE);
68 }
70 void *qemu_vmalloc(size_t size)
71 {
72 /* FIXME: this is not exactly optimal solution since VirtualAlloc
73 has 64Kb granularity, but at least it guarantees us that the
74 memory is page aligned. */
75 return VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE);
76 }
78 void qemu_vfree(void *ptr)
79 {
80 VirtualFree(ptr, 0, MEM_RELEASE);
81 }
83 #else
85 #if defined(USE_KQEMU)
87 #include <sys/vfs.h>
88 #include <sys/mman.h>
89 #include <fcntl.h>
91 void *kqemu_vmalloc(size_t size)
92 {
93 static int phys_ram_fd = -1;
94 static int phys_ram_size = 0;
95 const char *tmpdir;
96 char phys_ram_file[1024];
97 void *ptr;
98 #ifdef HOST_SOLARIS
99 struct statvfs stfs;
100 #else
101 struct statfs stfs;
102 #endif
104 if (phys_ram_fd < 0) {
105 tmpdir = getenv("QEMU_TMPDIR");
106 if (!tmpdir)
107 #ifdef HOST_SOLARIS
108 tmpdir = "/tmp";
109 if (statvfs(tmpdir, &stfs) == 0) {
110 #else
111 tmpdir = "/dev/shm";
112 if (statfs(tmpdir, &stfs) == 0) {
113 #endif
114 int64_t free_space;
115 int ram_mb;
117 extern int ram_size;
118 free_space = (int64_t)stfs.f_bavail * stfs.f_bsize;
119 if ((ram_size + 8192 * 1024) >= free_space) {
120 ram_mb = (ram_size / (1024 * 1024));
121 fprintf(stderr,
122 "You do not have enough space in '%s' for the %d MB of QEMU virtual RAM.\n",
123 tmpdir, ram_mb);
124 if (strcmp(tmpdir, "/dev/shm") == 0) {
125 fprintf(stderr, "To have more space available provided you have enough RAM and swap, do as root:\n"
126 "umount /dev/shm\n"
127 "mount -t tmpfs -o size=%dm none /dev/shm\n",
128 ram_mb + 16);
129 } else {
130 fprintf(stderr,
131 "Use the '-m' option of QEMU to diminish the amount of virtual RAM or use the\n"
132 "QEMU_TMPDIR environment variable to set another directory where the QEMU\n"
133 "temporary RAM file will be opened.\n");
134 }
135 fprintf(stderr, "Or disable the accelerator module with -no-kqemu\n");
136 exit(1);
137 }
138 }
139 snprintf(phys_ram_file, sizeof(phys_ram_file), "%s/qemuXXXXXX",
140 tmpdir);
141 phys_ram_fd = mkstemp(phys_ram_file);
142 if (phys_ram_fd < 0) {
143 fprintf(stderr,
144 "warning: could not create temporary file in '%s'.\n"
145 "Use QEMU_TMPDIR to select a directory in a tmpfs filesystem.\n"
146 "Using '/tmp' as fallback.\n",
147 tmpdir);
148 snprintf(phys_ram_file, sizeof(phys_ram_file), "%s/qemuXXXXXX",
149 "/tmp");
150 phys_ram_fd = mkstemp(phys_ram_file);
151 if (phys_ram_fd < 0) {
152 fprintf(stderr, "Could not create temporary memory file '%s'\n",
153 phys_ram_file);
154 exit(1);
155 }
156 }
157 unlink(phys_ram_file);
158 }
159 size = (size + 4095) & ~4095;
160 ftruncate(phys_ram_fd, phys_ram_size + size);
161 ptr = mmap(NULL,
162 size,
163 PROT_WRITE | PROT_READ, MAP_SHARED,
164 phys_ram_fd, phys_ram_size);
165 if (ptr == MAP_FAILED) {
166 fprintf(stderr, "Could not map physical memory\n");
167 exit(1);
168 }
169 phys_ram_size += size;
170 return ptr;
171 }
173 void kqemu_vfree(void *ptr)
174 {
175 /* may be useful some day, but currently we do not need to free */
176 }
178 #endif
180 void *qemu_memalign(size_t alignment, size_t size)
181 {
182 #if defined(_POSIX_C_SOURCE) && !defined(__sun__)
183 int ret;
184 void *ptr;
185 ret = posix_memalign(&ptr, alignment, size);
186 if (ret != 0)
187 return NULL;
188 return ptr;
189 #elif defined(_BSD)
190 return valloc(size);
191 #else
192 return memalign(alignment, size);
193 #endif
194 }
196 /* alloc shared memory pages */
197 void *qemu_vmalloc(size_t size)
198 {
199 #if defined(USE_KQEMU)
200 if (kqemu_allowed)
201 return kqemu_vmalloc(size);
202 #endif
203 #ifdef _BSD
204 return valloc(size);
205 #else
206 return memalign(4096, size);
207 #endif
208 }
210 void qemu_vfree(void *ptr)
211 {
212 #if defined(USE_KQEMU)
213 if (kqemu_allowed)
214 kqemu_vfree(ptr);
215 #endif
216 free(ptr);
217 }
219 #endif
221 void *qemu_mallocz(size_t size)
222 {
223 void *ptr;
224 ptr = qemu_malloc(size);
225 if (!ptr)
226 return NULL;
227 memset(ptr, 0, size);
228 return ptr;
229 }
231 char *qemu_strdup(const char *str)
232 {
233 char *ptr;
234 ptr = qemu_malloc(strlen(str) + 1);
235 if (!ptr)
236 return NULL;
237 strcpy(ptr, str);
238 return ptr;
239 }