root / osdep.c @ cbeb0857
History | View | Annotate | Download (7 kB)
1 | ea88812f | bellard | /*
|
---|---|---|---|
2 | ea88812f | bellard | * QEMU low level functions
|
3 | ea88812f | bellard | *
|
4 | ea88812f | bellard | * Copyright (c) 2003 Fabrice Bellard
|
5 | ea88812f | bellard | *
|
6 | ea88812f | bellard | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 | ea88812f | bellard | * of this software and associated documentation files (the "Software"), to deal
|
8 | ea88812f | bellard | * in the Software without restriction, including without limitation the rights
|
9 | ea88812f | bellard | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 | ea88812f | bellard | * copies of the Software, and to permit persons to whom the Software is
|
11 | ea88812f | bellard | * furnished to do so, subject to the following conditions:
|
12 | ea88812f | bellard | *
|
13 | ea88812f | bellard | * The above copyright notice and this permission notice shall be included in
|
14 | ea88812f | bellard | * all copies or substantial portions of the Software.
|
15 | ea88812f | bellard | *
|
16 | ea88812f | bellard | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 | ea88812f | bellard | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 | ea88812f | bellard | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
19 | ea88812f | bellard | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 | ea88812f | bellard | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 | ea88812f | bellard | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22 | ea88812f | bellard | * THE SOFTWARE.
|
23 | ea88812f | bellard | */
|
24 | ea88812f | bellard | #include <stdlib.h> |
25 | ea88812f | bellard | #include <stdio.h> |
26 | ea88812f | bellard | #include <stdarg.h> |
27 | ea88812f | bellard | #include <string.h> |
28 | ea88812f | bellard | #include <errno.h> |
29 | ea88812f | bellard | #include <unistd.h> |
30 | aa26bb2d | ths | #include <fcntl.h> |
31 | 605686cd | ths | #ifdef HOST_SOLARIS
|
32 | 605686cd | ths | #include <sys/types.h> |
33 | 605686cd | ths | #include <sys/statvfs.h> |
34 | 605686cd | ths | #endif
|
35 | ea88812f | bellard | |
36 | ea88812f | bellard | #include "cpu.h" |
37 | 6cb7ee85 | pbrook | #if defined(USE_KQEMU)
|
38 | 6cb7ee85 | pbrook | #include "vl.h" |
39 | 6cb7ee85 | pbrook | #endif
|
40 | ea88812f | bellard | |
41 | 6e4255f6 | bellard | #ifdef _WIN32
|
42 | 6e4255f6 | bellard | #include <windows.h> |
43 | 6e4255f6 | bellard | #elif defined(_BSD)
|
44 | 194884dd | bellard | #include <stdlib.h> |
45 | 194884dd | bellard | #else
|
46 | 49b470eb | bellard | #include <malloc.h> |
47 | 194884dd | bellard | #endif
|
48 | 49b470eb | bellard | |
49 | ea88812f | bellard | void *get_mmap_addr(unsigned long size) |
50 | ea88812f | bellard | { |
51 | ea88812f | bellard | return NULL; |
52 | ea88812f | bellard | } |
53 | ea88812f | bellard | |
54 | ea88812f | bellard | void qemu_free(void *ptr) |
55 | ea88812f | bellard | { |
56 | ea88812f | bellard | free(ptr); |
57 | ea88812f | bellard | } |
58 | ea88812f | bellard | |
59 | ea88812f | bellard | void *qemu_malloc(size_t size)
|
60 | ea88812f | bellard | { |
61 | ea88812f | bellard | return malloc(size);
|
62 | ea88812f | bellard | } |
63 | ea88812f | bellard | |
64 | 6e4255f6 | bellard | #if defined(_WIN32)
|
65 | 6e4255f6 | bellard | |
66 | 6e4255f6 | bellard | void *qemu_vmalloc(size_t size)
|
67 | 6e4255f6 | bellard | { |
68 | 6e4255f6 | bellard | /* FIXME: this is not exactly optimal solution since VirtualAlloc
|
69 | 6e4255f6 | bellard | has 64Kb granularity, but at least it guarantees us that the
|
70 | 6e4255f6 | bellard | memory is page aligned. */
|
71 | 6e4255f6 | bellard | return VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE); |
72 | 6e4255f6 | bellard | } |
73 | 6e4255f6 | bellard | |
74 | 6e4255f6 | bellard | void qemu_vfree(void *ptr) |
75 | 6e4255f6 | bellard | { |
76 | 6e4255f6 | bellard | VirtualFree(ptr, 0, MEM_RELEASE);
|
77 | 6e4255f6 | bellard | } |
78 | 6e4255f6 | bellard | |
79 | 6cb7ee85 | pbrook | #else
|
80 | 6cb7ee85 | pbrook | |
81 | 6cb7ee85 | pbrook | #if defined(USE_KQEMU)
|
82 | 49b470eb | bellard | |
83 | 6bae7ed8 | bellard | #include <sys/vfs.h> |
84 | 49b470eb | bellard | #include <sys/mman.h> |
85 | 49b470eb | bellard | #include <fcntl.h> |
86 | 49b470eb | bellard | |
87 | 6cb7ee85 | pbrook | void *kqemu_vmalloc(size_t size)
|
88 | 49b470eb | bellard | { |
89 | 49b470eb | bellard | static int phys_ram_fd = -1; |
90 | 49b470eb | bellard | static int phys_ram_size = 0; |
91 | 49b470eb | bellard | const char *tmpdir; |
92 | 49b470eb | bellard | char phys_ram_file[1024]; |
93 | 49b470eb | bellard | void *ptr;
|
94 | 605686cd | ths | #ifdef HOST_SOLARIS
|
95 | 605686cd | ths | struct statvfs stfs;
|
96 | 605686cd | ths | #else
|
97 | 6bae7ed8 | bellard | struct statfs stfs;
|
98 | 605686cd | ths | #endif
|
99 | 49b470eb | bellard | |
100 | 49b470eb | bellard | if (phys_ram_fd < 0) { |
101 | 49b470eb | bellard | tmpdir = getenv("QEMU_TMPDIR");
|
102 | 49b470eb | bellard | if (!tmpdir)
|
103 | 605686cd | ths | #ifdef HOST_SOLARIS
|
104 | 605686cd | ths | tmpdir = "/tmp";
|
105 | 605686cd | ths | if (statvfs(tmpdir, &stfs) == 0) { |
106 | 605686cd | ths | #else
|
107 | 49b470eb | bellard | tmpdir = "/dev/shm";
|
108 | 6bae7ed8 | bellard | if (statfs(tmpdir, &stfs) == 0) { |
109 | 605686cd | ths | #endif
|
110 | 6bae7ed8 | bellard | int64_t free_space; |
111 | 6bae7ed8 | bellard | int ram_mb;
|
112 | 6bae7ed8 | bellard | |
113 | 6bae7ed8 | bellard | extern int ram_size; |
114 | 6bae7ed8 | bellard | free_space = (int64_t)stfs.f_bavail * stfs.f_bsize; |
115 | 6bae7ed8 | bellard | if ((ram_size + 8192 * 1024) >= free_space) { |
116 | 6bae7ed8 | bellard | ram_mb = (ram_size / (1024 * 1024)); |
117 | 6bae7ed8 | bellard | fprintf(stderr, |
118 | 6bae7ed8 | bellard | "You do not have enough space in '%s' for the %d MB of QEMU virtual RAM.\n",
|
119 | 6bae7ed8 | bellard | tmpdir, ram_mb); |
120 | 6bae7ed8 | bellard | if (strcmp(tmpdir, "/dev/shm") == 0) { |
121 | 6bae7ed8 | bellard | fprintf(stderr, "To have more space available provided you have enough RAM and swap, do as root:\n"
|
122 | 6bae7ed8 | bellard | "umount /dev/shm\n"
|
123 | 6bae7ed8 | bellard | "mount -t tmpfs -o size=%dm none /dev/shm\n",
|
124 | 6bae7ed8 | bellard | ram_mb + 16);
|
125 | 6bae7ed8 | bellard | } else {
|
126 | 6bae7ed8 | bellard | fprintf(stderr, |
127 | 6bae7ed8 | bellard | "Use the '-m' option of QEMU to diminish the amount of virtual RAM or use the\n"
|
128 | 6bae7ed8 | bellard | "QEMU_TMPDIR environment variable to set another directory where the QEMU\n"
|
129 | 6bae7ed8 | bellard | "temporary RAM file will be opened.\n");
|
130 | 6bae7ed8 | bellard | } |
131 | 6cb7ee85 | pbrook | fprintf(stderr, "Or disable the accelerator module with -no-kqemu\n");
|
132 | 6bae7ed8 | bellard | exit(1);
|
133 | 6bae7ed8 | bellard | } |
134 | 6bae7ed8 | bellard | } |
135 | 49b470eb | bellard | snprintf(phys_ram_file, sizeof(phys_ram_file), "%s/qemuXXXXXX", |
136 | 49b470eb | bellard | tmpdir); |
137 | 243a273e | bellard | phys_ram_fd = mkstemp(phys_ram_file); |
138 | 243a273e | bellard | if (phys_ram_fd < 0) { |
139 | 49b470eb | bellard | fprintf(stderr, |
140 | 49b470eb | bellard | "warning: could not create temporary file in '%s'.\n"
|
141 | 49b470eb | bellard | "Use QEMU_TMPDIR to select a directory in a tmpfs filesystem.\n"
|
142 | 49b470eb | bellard | "Using '/tmp' as fallback.\n",
|
143 | 49b470eb | bellard | tmpdir); |
144 | 49b470eb | bellard | snprintf(phys_ram_file, sizeof(phys_ram_file), "%s/qemuXXXXXX", |
145 | 49b470eb | bellard | "/tmp");
|
146 | 243a273e | bellard | phys_ram_fd = mkstemp(phys_ram_file); |
147 | 243a273e | bellard | if (phys_ram_fd < 0) { |
148 | 49b470eb | bellard | fprintf(stderr, "Could not create temporary memory file '%s'\n",
|
149 | 49b470eb | bellard | phys_ram_file); |
150 | 49b470eb | bellard | exit(1);
|
151 | 49b470eb | bellard | } |
152 | 49b470eb | bellard | } |
153 | 49b470eb | bellard | unlink(phys_ram_file); |
154 | 49b470eb | bellard | } |
155 | 49b470eb | bellard | size = (size + 4095) & ~4095; |
156 | 49b470eb | bellard | ftruncate(phys_ram_fd, phys_ram_size + size); |
157 | 49b470eb | bellard | ptr = mmap(NULL,
|
158 | 49b470eb | bellard | size, |
159 | 49b470eb | bellard | PROT_WRITE | PROT_READ, MAP_SHARED, |
160 | 49b470eb | bellard | phys_ram_fd, phys_ram_size); |
161 | 49b470eb | bellard | if (ptr == MAP_FAILED) {
|
162 | 49b470eb | bellard | fprintf(stderr, "Could not map physical memory\n");
|
163 | 49b470eb | bellard | exit(1);
|
164 | 49b470eb | bellard | } |
165 | 49b470eb | bellard | phys_ram_size += size; |
166 | 49b470eb | bellard | return ptr;
|
167 | 49b470eb | bellard | } |
168 | 49b470eb | bellard | |
169 | 6cb7ee85 | pbrook | void kqemu_vfree(void *ptr) |
170 | 49b470eb | bellard | { |
171 | 49b470eb | bellard | /* may be useful some day, but currently we do not need to free */
|
172 | 49b470eb | bellard | } |
173 | 49b470eb | bellard | |
174 | 6cb7ee85 | pbrook | #endif
|
175 | 49b470eb | bellard | |
176 | 49b470eb | bellard | /* alloc shared memory pages */
|
177 | 49b470eb | bellard | void *qemu_vmalloc(size_t size)
|
178 | 49b470eb | bellard | { |
179 | 6cb7ee85 | pbrook | #if defined(USE_KQEMU)
|
180 | 6cb7ee85 | pbrook | if (kqemu_allowed)
|
181 | 6cb7ee85 | pbrook | return kqemu_vmalloc(size);
|
182 | 6cb7ee85 | pbrook | #endif
|
183 | 49b470eb | bellard | #ifdef _BSD
|
184 | 49b470eb | bellard | return valloc(size);
|
185 | 49b470eb | bellard | #else
|
186 | 49b470eb | bellard | return memalign(4096, size); |
187 | 49b470eb | bellard | #endif
|
188 | 49b470eb | bellard | } |
189 | 49b470eb | bellard | |
190 | 49b470eb | bellard | void qemu_vfree(void *ptr) |
191 | 49b470eb | bellard | { |
192 | 6cb7ee85 | pbrook | #if defined(USE_KQEMU)
|
193 | 6cb7ee85 | pbrook | if (kqemu_allowed)
|
194 | 6cb7ee85 | pbrook | kqemu_vfree(ptr); |
195 | 6cb7ee85 | pbrook | #endif
|
196 | 49b470eb | bellard | free(ptr); |
197 | 49b470eb | bellard | } |
198 | 49b470eb | bellard | |
199 | 49b470eb | bellard | #endif
|
200 | 49b470eb | bellard | |
201 | 07d89866 | bellard | void *qemu_mallocz(size_t size)
|
202 | 07d89866 | bellard | { |
203 | 07d89866 | bellard | void *ptr;
|
204 | 07d89866 | bellard | ptr = qemu_malloc(size); |
205 | 07d89866 | bellard | if (!ptr)
|
206 | 07d89866 | bellard | return NULL; |
207 | 07d89866 | bellard | memset(ptr, 0, size);
|
208 | 07d89866 | bellard | return ptr;
|
209 | 07d89866 | bellard | } |
210 | 07d89866 | bellard | |
211 | 2571929a | bellard | char *qemu_strdup(const char *str) |
212 | 2571929a | bellard | { |
213 | 2571929a | bellard | char *ptr;
|
214 | 2571929a | bellard | ptr = qemu_malloc(strlen(str) + 1);
|
215 | 2571929a | bellard | if (!ptr)
|
216 | 2571929a | bellard | return NULL; |
217 | 2571929a | bellard | strcpy(ptr, str); |
218 | 2571929a | bellard | return ptr;
|
219 | 2571929a | bellard | } |
220 | aa26bb2d | ths | |
221 | aa26bb2d | ths | int qemu_create_pidfile(const char *filename) |
222 | aa26bb2d | ths | { |
223 | aa26bb2d | ths | char buffer[128]; |
224 | aa26bb2d | ths | int len;
|
225 | aa26bb2d | ths | #ifndef _WIN32
|
226 | aa26bb2d | ths | int fd;
|
227 | aa26bb2d | ths | |
228 | aa26bb2d | ths | fd = open(filename, O_RDWR | O_CREAT, 0600);
|
229 | aa26bb2d | ths | if (fd == -1) |
230 | aa26bb2d | ths | return -1; |
231 | aa26bb2d | ths | |
232 | aa26bb2d | ths | if (lockf(fd, F_TLOCK, 0) == -1) |
233 | aa26bb2d | ths | return -1; |
234 | aa26bb2d | ths | |
235 | aa26bb2d | ths | len = snprintf(buffer, sizeof(buffer), "%ld\n", (long)getpid()); |
236 | aa26bb2d | ths | if (write(fd, buffer, len) != len)
|
237 | aa26bb2d | ths | return -1; |
238 | aa26bb2d | ths | #else
|
239 | aa26bb2d | ths | HANDLE file; |
240 | aa26bb2d | ths | DWORD flags; |
241 | aa26bb2d | ths | OVERLAPPED overlap; |
242 | aa26bb2d | ths | BOOL ret; |
243 | aa26bb2d | ths | |
244 | aa26bb2d | ths | /* Open for writing with no sharing. */
|
245 | aa26bb2d | ths | file = CreateFile(filename, GENERIC_WRITE, 0, NULL, |
246 | aa26bb2d | ths | OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
|
247 | aa26bb2d | ths | |
248 | aa26bb2d | ths | if (file == INVALID_HANDLE_VALUE)
|
249 | aa26bb2d | ths | return -1; |
250 | aa26bb2d | ths | |
251 | aa26bb2d | ths | flags = LOCKFILE_EXCLUSIVE_LOCK | LOCKFILE_FAIL_IMMEDIATELY; |
252 | aa26bb2d | ths | overlap.hEvent = 0;
|
253 | aa26bb2d | ths | /* Lock 1 byte. */
|
254 | aa26bb2d | ths | ret = LockFileEx(file, flags, 0, 0, 1, &overlap); |
255 | aa26bb2d | ths | if (ret == 0) |
256 | aa26bb2d | ths | return -1; |
257 | aa26bb2d | ths | |
258 | aa26bb2d | ths | /* Write PID to file. */
|
259 | aa26bb2d | ths | len = snprintf(buffer, sizeof(buffer), "%ld\n", (long)getpid()); |
260 | aa26bb2d | ths | ret = WriteFileEx(file, (LPCVOID)buffer, (DWORD)len, |
261 | aa26bb2d | ths | &overlap, NULL);
|
262 | aa26bb2d | ths | if (ret == 0) |
263 | aa26bb2d | ths | return -1; |
264 | aa26bb2d | ths | #endif
|
265 | aa26bb2d | ths | return 0; |
266 | aa26bb2d | ths | } |