]>
Commit | Line | Data |
---|---|---|
c1b0b93b JS |
1 | /* |
2 | * os-win32.c | |
3 | * | |
4 | * Copyright (c) 2003-2008 Fabrice Bellard | |
5 | * Copyright (c) 2010 Red Hat, Inc. | |
6 | * | |
7 | * QEMU library functions for win32 which are shared between QEMU and | |
8 | * the QEMU tools. | |
9 | * | |
10 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
11 | * of this software and associated documentation files (the "Software"), to deal | |
12 | * in the Software without restriction, including without limitation the rights | |
13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
14 | * copies of the Software, and to permit persons to whom the Software is | |
15 | * furnished to do so, subject to the following conditions: | |
16 | * | |
17 | * The above copyright notice and this permission notice shall be included in | |
18 | * all copies or substantial portions of the Software. | |
19 | * | |
20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
23 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
26 | * THE SOFTWARE. | |
e637aa66 SW |
27 | * |
28 | * The implementation of g_poll (functions poll_rest, g_poll) at the end of | |
29 | * this file are based on code from GNOME glib-2 and use a different license, | |
30 | * see the license comment there. | |
c1b0b93b JS |
31 | */ |
32 | #include <windows.h> | |
e2ea3515 LE |
33 | #include <glib.h> |
34 | #include <stdlib.h> | |
c1b0b93b | 35 | #include "config-host.h" |
9c17d615 | 36 | #include "sysemu/sysemu.h" |
1de7afc9 | 37 | #include "qemu/main-loop.h" |
c1b0b93b | 38 | #include "trace.h" |
1de7afc9 | 39 | #include "qemu/sockets.h" |
c1b0b93b | 40 | |
e2ea3515 LE |
41 | /* this must come after including "trace.h" */ |
42 | #include <shlobj.h> | |
43 | ||
b152aa84 | 44 | void *qemu_oom_check(void *ptr) |
c1b0b93b JS |
45 | { |
46 | if (ptr == NULL) { | |
47 | fprintf(stderr, "Failed to allocate memory: %lu\n", GetLastError()); | |
48 | abort(); | |
49 | } | |
50 | return ptr; | |
51 | } | |
52 | ||
7d2a35cc | 53 | void *qemu_try_memalign(size_t alignment, size_t size) |
c1b0b93b JS |
54 | { |
55 | void *ptr; | |
56 | ||
57 | if (!size) { | |
58 | abort(); | |
59 | } | |
7d2a35cc | 60 | ptr = VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE); |
c1b0b93b JS |
61 | trace_qemu_memalign(alignment, size, ptr); |
62 | return ptr; | |
63 | } | |
64 | ||
7d2a35cc KW |
65 | void *qemu_memalign(size_t alignment, size_t size) |
66 | { | |
67 | return qemu_oom_check(qemu_try_memalign(alignment, size)); | |
68 | } | |
69 | ||
a2b257d6 | 70 | void *qemu_anon_ram_alloc(size_t size, uint64_t *align) |
c1b0b93b JS |
71 | { |
72 | void *ptr; | |
73 | ||
74 | /* FIXME: this is not exactly optimal solution since VirtualAlloc | |
75 | has 64Kb granularity, but at least it guarantees us that the | |
76 | memory is page aligned. */ | |
39228250 | 77 | ptr = VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE); |
6eebf958 | 78 | trace_qemu_anon_ram_alloc(size, ptr); |
c1b0b93b JS |
79 | return ptr; |
80 | } | |
81 | ||
82 | void qemu_vfree(void *ptr) | |
83 | { | |
84 | trace_qemu_vfree(ptr); | |
94c8ff3a MA |
85 | if (ptr) { |
86 | VirtualFree(ptr, 0, MEM_RELEASE); | |
87 | } | |
c1b0b93b | 88 | } |
9549e764 | 89 | |
e7a09b92 PB |
90 | void qemu_anon_ram_free(void *ptr, size_t size) |
91 | { | |
92 | trace_qemu_anon_ram_free(ptr, size); | |
93 | if (ptr) { | |
94 | VirtualFree(ptr, 0, MEM_RELEASE); | |
95 | } | |
96 | } | |
97 | ||
4d9310f4 | 98 | #ifndef CONFIG_LOCALTIME_R |
d3e8f957 SW |
99 | /* FIXME: add proper locking */ |
100 | struct tm *gmtime_r(const time_t *timep, struct tm *result) | |
101 | { | |
102 | struct tm *p = gmtime(timep); | |
103 | memset(result, 0, sizeof(*result)); | |
104 | if (p) { | |
105 | *result = *p; | |
106 | p = result; | |
107 | } | |
108 | return p; | |
109 | } | |
110 | ||
111 | /* FIXME: add proper locking */ | |
112 | struct tm *localtime_r(const time_t *timep, struct tm *result) | |
113 | { | |
114 | struct tm *p = localtime(timep); | |
115 | memset(result, 0, sizeof(*result)); | |
116 | if (p) { | |
117 | *result = *p; | |
118 | p = result; | |
119 | } | |
120 | return p; | |
121 | } | |
4d9310f4 | 122 | #endif /* CONFIG_LOCALTIME_R */ |
d3e8f957 | 123 | |
f9e8cacc | 124 | void qemu_set_block(int fd) |
154b9a0c PB |
125 | { |
126 | unsigned long opt = 0; | |
d3385eb4 | 127 | WSAEventSelect(fd, NULL, 0); |
154b9a0c PB |
128 | ioctlsocket(fd, FIONBIO, &opt); |
129 | } | |
130 | ||
f9e8cacc | 131 | void qemu_set_nonblock(int fd) |
9549e764 JS |
132 | { |
133 | unsigned long opt = 1; | |
134 | ioctlsocket(fd, FIONBIO, &opt); | |
d3385eb4 | 135 | qemu_fd_register(fd); |
9549e764 JS |
136 | } |
137 | ||
606600a1 SO |
138 | int socket_set_fast_reuse(int fd) |
139 | { | |
140 | /* Enabling the reuse of an endpoint that was used by a socket still in | |
141 | * TIME_WAIT state is usually performed by setting SO_REUSEADDR. On Windows | |
142 | * fast reuse is the default and SO_REUSEADDR does strange things. So we | |
143 | * don't have to do anything here. More info can be found at: | |
144 | * http://msdn.microsoft.com/en-us/library/windows/desktop/ms740621.aspx */ | |
145 | return 0; | |
146 | } | |
147 | ||
9549e764 JS |
148 | int inet_aton(const char *cp, struct in_addr *ia) |
149 | { | |
150 | uint32_t addr = inet_addr(cp); | |
151 | if (addr == 0xffffffff) { | |
e637aa66 | 152 | return 0; |
9549e764 JS |
153 | } |
154 | ia->s_addr = addr; | |
155 | return 1; | |
156 | } | |
157 | ||
158 | void qemu_set_cloexec(int fd) | |
159 | { | |
160 | } | |
dc786bc9 | 161 | |
dc786bc9 JS |
162 | /* Offset between 1/1/1601 and 1/1/1970 in 100 nanosec units */ |
163 | #define _W32_FT_OFFSET (116444736000000000ULL) | |
164 | ||
165 | int qemu_gettimeofday(qemu_timeval *tp) | |
166 | { | |
167 | union { | |
168 | unsigned long long ns100; /*time since 1 Jan 1601 in 100ns units */ | |
169 | FILETIME ft; | |
170 | } _now; | |
171 | ||
172 | if(tp) { | |
173 | GetSystemTimeAsFileTime (&_now.ft); | |
174 | tp->tv_usec=(long)((_now.ns100 / 10ULL) % 1000000ULL ); | |
175 | tp->tv_sec= (long)((_now.ns100 - _W32_FT_OFFSET) / 10000000ULL); | |
176 | } | |
177 | /* Always return 0 as per Open Group Base Specifications Issue 6. | |
178 | Do not set errno on error. */ | |
179 | return 0; | |
180 | } | |
cbcfa041 PB |
181 | |
182 | int qemu_get_thread_id(void) | |
183 | { | |
184 | return GetCurrentThreadId(); | |
185 | } | |
e2ea3515 LE |
186 | |
187 | char * | |
188 | qemu_get_local_state_pathname(const char *relative_pathname) | |
189 | { | |
190 | HRESULT result; | |
191 | char base_path[MAX_PATH+1] = ""; | |
192 | ||
193 | result = SHGetFolderPath(NULL, CSIDL_COMMON_APPDATA, NULL, | |
194 | /* SHGFP_TYPE_CURRENT */ 0, base_path); | |
195 | if (result != S_OK) { | |
196 | /* misconfigured environment */ | |
197 | g_critical("CSIDL_COMMON_APPDATA unavailable: %ld", (long)result); | |
198 | abort(); | |
199 | } | |
200 | return g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s", base_path, | |
201 | relative_pathname); | |
202 | } | |
13401ba0 SH |
203 | |
204 | void qemu_set_tty_echo(int fd, bool echo) | |
205 | { | |
206 | HANDLE handle = (HANDLE)_get_osfhandle(fd); | |
207 | DWORD dwMode = 0; | |
208 | ||
209 | if (handle == INVALID_HANDLE_VALUE) { | |
210 | return; | |
211 | } | |
212 | ||
213 | GetConsoleMode(handle, &dwMode); | |
214 | ||
215 | if (echo) { | |
216 | SetConsoleMode(handle, dwMode | ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT); | |
217 | } else { | |
218 | SetConsoleMode(handle, | |
219 | dwMode & ~(ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT)); | |
220 | } | |
221 | } | |
10f5bff6 FZ |
222 | |
223 | static char exec_dir[PATH_MAX]; | |
224 | ||
225 | void qemu_init_exec_dir(const char *argv0) | |
226 | { | |
227 | ||
228 | char *p; | |
229 | char buf[MAX_PATH]; | |
230 | DWORD len; | |
231 | ||
232 | len = GetModuleFileName(NULL, buf, sizeof(buf) - 1); | |
233 | if (len == 0) { | |
234 | return; | |
235 | } | |
236 | ||
237 | buf[len] = 0; | |
238 | p = buf + len - 1; | |
239 | while (p != buf && *p != '\\') { | |
240 | p--; | |
241 | } | |
242 | *p = 0; | |
243 | if (access(buf, R_OK) == 0) { | |
244 | pstrcpy(exec_dir, sizeof(exec_dir), buf); | |
245 | } | |
246 | } | |
247 | ||
248 | char *qemu_get_exec_dir(void) | |
249 | { | |
250 | return g_strdup(exec_dir); | |
251 | } | |
5a007547 SP |
252 | |
253 | /* | |
e637aa66 SW |
254 | * The original implementation of g_poll from glib has a problem on Windows |
255 | * when using timeouts < 10 ms. | |
5a007547 | 256 | * |
e637aa66 SW |
257 | * Whenever g_poll is called with timeout < 10 ms, it does a quick poll instead |
258 | * of wait. This causes significant performance degradation of QEMU. | |
5a007547 | 259 | * |
e637aa66 SW |
260 | * The following code is a copy of the original code from glib/gpoll.c |
261 | * (glib commit 20f4d1820b8d4d0fc4447188e33efffd6d4a88d8 from 2014-02-19). | |
262 | * Some debug code was removed and the code was reformatted. | |
263 | * All other code modifications are marked with 'QEMU'. | |
264 | */ | |
265 | ||
266 | /* | |
267 | * gpoll.c: poll(2) abstraction | |
268 | * Copyright 1998 Owen Taylor | |
269 | * Copyright 2008 Red Hat, Inc. | |
5a007547 | 270 | * |
e637aa66 SW |
271 | * This library is free software; you can redistribute it and/or |
272 | * modify it under the terms of the GNU Lesser General Public | |
273 | * License as published by the Free Software Foundation; either | |
274 | * version 2 of the License, or (at your option) any later version. | |
275 | * | |
276 | * This library is distributed in the hope that it will be useful, | |
277 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
278 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
279 | * Lesser General Public License for more details. | |
280 | * | |
281 | * You should have received a copy of the GNU Lesser General Public | |
282 | * License along with this library; if not, see <http://www.gnu.org/licenses/>. | |
5a007547 | 283 | */ |
e637aa66 SW |
284 | |
285 | static int poll_rest(gboolean poll_msgs, HANDLE *handles, gint nhandles, | |
286 | GPollFD *fds, guint nfds, gint timeout) | |
5a007547 | 287 | { |
e637aa66 SW |
288 | DWORD ready; |
289 | GPollFD *f; | |
290 | int recursed_result; | |
5a007547 | 291 | |
e637aa66 SW |
292 | if (poll_msgs) { |
293 | /* Wait for either messages or handles | |
294 | * -> Use MsgWaitForMultipleObjectsEx | |
295 | */ | |
296 | ready = MsgWaitForMultipleObjectsEx(nhandles, handles, timeout, | |
297 | QS_ALLINPUT, MWMO_ALERTABLE); | |
5a007547 | 298 | |
e637aa66 SW |
299 | if (ready == WAIT_FAILED) { |
300 | gchar *emsg = g_win32_error_message(GetLastError()); | |
301 | g_warning("MsgWaitForMultipleObjectsEx failed: %s", emsg); | |
302 | g_free(emsg); | |
5a007547 | 303 | } |
e637aa66 SW |
304 | } else if (nhandles == 0) { |
305 | /* No handles to wait for, just the timeout */ | |
306 | if (timeout == INFINITE) { | |
307 | ready = WAIT_FAILED; | |
308 | } else { | |
309 | SleepEx(timeout, TRUE); | |
310 | ready = WAIT_TIMEOUT; | |
311 | } | |
312 | } else { | |
313 | /* Wait for just handles | |
314 | * -> Use WaitForMultipleObjectsEx | |
5a007547 | 315 | */ |
e637aa66 SW |
316 | ready = |
317 | WaitForMultipleObjectsEx(nhandles, handles, FALSE, timeout, TRUE); | |
318 | if (ready == WAIT_FAILED) { | |
319 | gchar *emsg = g_win32_error_message(GetLastError()); | |
320 | g_warning("WaitForMultipleObjectsEx failed: %s", emsg); | |
321 | g_free(emsg); | |
5a007547 | 322 | } |
e637aa66 | 323 | } |
5a007547 | 324 | |
e637aa66 SW |
325 | if (ready == WAIT_FAILED) { |
326 | return -1; | |
327 | } else if (ready == WAIT_TIMEOUT || ready == WAIT_IO_COMPLETION) { | |
328 | return 0; | |
329 | } else if (poll_msgs && ready == WAIT_OBJECT_0 + nhandles) { | |
330 | for (f = fds; f < &fds[nfds]; ++f) { | |
331 | if (f->fd == G_WIN32_MSG_HANDLE && f->events & G_IO_IN) { | |
332 | f->revents |= G_IO_IN; | |
5a007547 SP |
333 | } |
334 | } | |
5a007547 | 335 | |
e637aa66 SW |
336 | /* If we have a timeout, or no handles to poll, be satisfied |
337 | * with just noticing we have messages waiting. | |
338 | */ | |
339 | if (timeout != 0 || nhandles == 0) { | |
340 | return 1; | |
341 | } | |
5a007547 | 342 | |
e637aa66 SW |
343 | /* If no timeout and handles to poll, recurse to poll them, |
344 | * too. | |
345 | */ | |
346 | recursed_result = poll_rest(FALSE, handles, nhandles, fds, nfds, 0); | |
347 | return (recursed_result == -1) ? -1 : 1 + recursed_result; | |
348 | } else if (/* QEMU: removed the following unneeded statement which causes | |
349 | * a compiler warning: ready >= WAIT_OBJECT_0 && */ | |
350 | ready < WAIT_OBJECT_0 + nhandles) { | |
351 | for (f = fds; f < &fds[nfds]; ++f) { | |
352 | if ((HANDLE) f->fd == handles[ready - WAIT_OBJECT_0]) { | |
353 | f->revents = f->events; | |
354 | } | |
355 | } | |
5a007547 | 356 | |
e637aa66 SW |
357 | /* If no timeout and polling several handles, recurse to poll |
358 | * the rest of them. | |
359 | */ | |
360 | if (timeout == 0 && nhandles > 1) { | |
361 | /* Remove the handle that fired */ | |
362 | int i; | |
363 | if (ready < nhandles - 1) { | |
364 | for (i = ready - WAIT_OBJECT_0 + 1; i < nhandles; i++) { | |
365 | handles[i-1] = handles[i]; | |
366 | } | |
367 | } | |
368 | nhandles--; | |
369 | recursed_result = poll_rest(FALSE, handles, nhandles, fds, nfds, 0); | |
370 | return (recursed_result == -1) ? -1 : 1 + recursed_result; | |
5a007547 | 371 | } |
e637aa66 | 372 | return 1; |
5a007547 SP |
373 | } |
374 | ||
e637aa66 SW |
375 | return 0; |
376 | } | |
5a007547 | 377 | |
e637aa66 SW |
378 | gint g_poll(GPollFD *fds, guint nfds, gint timeout) |
379 | { | |
380 | HANDLE handles[MAXIMUM_WAIT_OBJECTS]; | |
381 | gboolean poll_msgs = FALSE; | |
382 | GPollFD *f; | |
383 | gint nhandles = 0; | |
384 | int retval; | |
385 | ||
386 | for (f = fds; f < &fds[nfds]; ++f) { | |
387 | if (f->fd == G_WIN32_MSG_HANDLE && (f->events & G_IO_IN)) { | |
388 | poll_msgs = TRUE; | |
389 | } else if (f->fd > 0) { | |
390 | /* Don't add the same handle several times into the array, as | |
391 | * docs say that is not allowed, even if it actually does seem | |
392 | * to work. | |
393 | */ | |
394 | gint i; | |
395 | ||
396 | for (i = 0; i < nhandles; i++) { | |
397 | if (handles[i] == (HANDLE) f->fd) { | |
398 | break; | |
399 | } | |
5a007547 SP |
400 | } |
401 | ||
e637aa66 SW |
402 | if (i == nhandles) { |
403 | if (nhandles == MAXIMUM_WAIT_OBJECTS) { | |
404 | g_warning("Too many handles to wait for!\n"); | |
405 | break; | |
406 | } else { | |
407 | handles[nhandles++] = (HANDLE) f->fd; | |
408 | } | |
5a007547 SP |
409 | } |
410 | } | |
e637aa66 | 411 | } |
5a007547 | 412 | |
e637aa66 SW |
413 | for (f = fds; f < &fds[nfds]; ++f) { |
414 | f->revents = 0; | |
415 | } | |
5a007547 | 416 | |
e637aa66 SW |
417 | if (timeout == -1) { |
418 | timeout = INFINITE; | |
419 | } | |
5a007547 | 420 | |
e637aa66 SW |
421 | /* Polling for several things? */ |
422 | if (nhandles > 1 || (nhandles > 0 && poll_msgs)) { | |
423 | /* First check if one or several of them are immediately | |
424 | * available | |
425 | */ | |
426 | retval = poll_rest(poll_msgs, handles, nhandles, fds, nfds, 0); | |
427 | ||
428 | /* If not, and we have a significant timeout, poll again with | |
429 | * timeout then. Note that this will return indication for only | |
430 | * one event, or only for messages. We ignore timeouts less than | |
431 | * ten milliseconds as they are mostly pointless on Windows, the | |
432 | * MsgWaitForMultipleObjectsEx() call will timeout right away | |
433 | * anyway. | |
434 | * | |
435 | * Modification for QEMU: replaced timeout >= 10 by timeout > 0. | |
5a007547 | 436 | */ |
e637aa66 SW |
437 | if (retval == 0 && (timeout == INFINITE || timeout > 0)) { |
438 | retval = poll_rest(poll_msgs, handles, nhandles, | |
439 | fds, nfds, timeout); | |
5a007547 | 440 | } |
e637aa66 SW |
441 | } else { |
442 | /* Just polling for one thing, so no need to check first if | |
443 | * available immediately | |
444 | */ | |
445 | retval = poll_rest(poll_msgs, handles, nhandles, fds, nfds, timeout); | |
446 | } | |
5a007547 | 447 | |
e637aa66 SW |
448 | if (retval == -1) { |
449 | for (f = fds; f < &fds[nfds]; ++f) { | |
450 | f->revents = 0; | |
451 | } | |
5a007547 SP |
452 | } |
453 | ||
e637aa66 | 454 | return retval; |
5a007547 | 455 | } |
38183310 | 456 | |
a28c2f2d | 457 | int getpagesize(void) |
38183310 PB |
458 | { |
459 | SYSTEM_INFO system_info; | |
460 | ||
461 | GetSystemInfo(&system_info); | |
462 | return system_info.dwPageSize; | |
463 | } | |
464 | ||
465 | void os_mem_prealloc(int fd, char *area, size_t memory) | |
466 | { | |
467 | int i; | |
468 | size_t pagesize = getpagesize(); | |
469 | ||
470 | memory = (memory + pagesize - 1) & -pagesize; | |
471 | for (i = 0; i < memory / pagesize; i++) { | |
472 | memset(area + pagesize * i, 0, 1); | |
473 | } | |
474 | } | |
d57e4e48 DB |
475 | |
476 | ||
477 | /* XXX: put correct support for win32 */ | |
478 | int qemu_read_password(char *buf, int buf_size) | |
479 | { | |
480 | int c, i; | |
481 | ||
482 | printf("Password: "); | |
483 | fflush(stdout); | |
484 | i = 0; | |
485 | for (;;) { | |
486 | c = getchar(); | |
487 | if (c < 0) { | |
488 | buf[i] = '\0'; | |
489 | return -1; | |
490 | } else if (c == '\n') { | |
491 | break; | |
492 | } else if (i < (buf_size - 1)) { | |
493 | buf[i++] = c; | |
494 | } | |
495 | } | |
496 | buf[i] = '\0'; | |
497 | return 0; | |
498 | } | |
57cb38b3 DB |
499 | |
500 | ||
501 | pid_t qemu_fork(Error **errp) | |
502 | { | |
503 | errno = ENOSYS; | |
504 | error_setg_errno(errp, errno, | |
505 | "cannot fork child process"); | |
506 | return -1; | |
507 | } |