Jes Sorensen | c1b0b93 | 2010-10-26 10:39:19 +0200 | [diff] [blame] | 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. |
Stefan Weil | e637aa6 | 2014-05-28 17:42:24 +0200 | [diff] [blame] | 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. |
Jes Sorensen | c1b0b93 | 2010-10-26 10:39:19 +0200 | [diff] [blame] | 31 | */ |
| 32 | #include <windows.h> |
Laszlo Ersek | e2ea351 | 2013-05-18 06:31:48 +0200 | [diff] [blame] | 33 | #include <glib.h> |
| 34 | #include <stdlib.h> |
Jes Sorensen | c1b0b93 | 2010-10-26 10:39:19 +0200 | [diff] [blame] | 35 | #include "config-host.h" |
Paolo Bonzini | 9c17d61 | 2012-12-17 18:20:04 +0100 | [diff] [blame] | 36 | #include "sysemu/sysemu.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 37 | #include "qemu/main-loop.h" |
Jes Sorensen | c1b0b93 | 2010-10-26 10:39:19 +0200 | [diff] [blame] | 38 | #include "trace.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 39 | #include "qemu/sockets.h" |
Jes Sorensen | c1b0b93 | 2010-10-26 10:39:19 +0200 | [diff] [blame] | 40 | |
Laszlo Ersek | e2ea351 | 2013-05-18 06:31:48 +0200 | [diff] [blame] | 41 | /* this must come after including "trace.h" */ |
| 42 | #include <shlobj.h> |
| 43 | |
Jes Sorensen | b152aa8 | 2010-10-26 10:39:26 +0200 | [diff] [blame] | 44 | void *qemu_oom_check(void *ptr) |
Jes Sorensen | c1b0b93 | 2010-10-26 10:39:19 +0200 | [diff] [blame] | 45 | { |
| 46 | if (ptr == NULL) { |
| 47 | fprintf(stderr, "Failed to allocate memory: %lu\n", GetLastError()); |
| 48 | abort(); |
| 49 | } |
| 50 | return ptr; |
| 51 | } |
| 52 | |
Kevin Wolf | 7d2a35c | 2014-05-20 12:24:05 +0200 | [diff] [blame] | 53 | void *qemu_try_memalign(size_t alignment, size_t size) |
Jes Sorensen | c1b0b93 | 2010-10-26 10:39:19 +0200 | [diff] [blame] | 54 | { |
| 55 | void *ptr; |
| 56 | |
| 57 | if (!size) { |
| 58 | abort(); |
| 59 | } |
Kevin Wolf | 7d2a35c | 2014-05-20 12:24:05 +0200 | [diff] [blame] | 60 | ptr = VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE); |
Jes Sorensen | c1b0b93 | 2010-10-26 10:39:19 +0200 | [diff] [blame] | 61 | trace_qemu_memalign(alignment, size, ptr); |
| 62 | return ptr; |
| 63 | } |
| 64 | |
Kevin Wolf | 7d2a35c | 2014-05-20 12:24:05 +0200 | [diff] [blame] | 65 | void *qemu_memalign(size_t alignment, size_t size) |
| 66 | { |
| 67 | return qemu_oom_check(qemu_try_memalign(alignment, size)); |
| 68 | } |
| 69 | |
Igor Mammedov | a2b257d | 2014-10-31 16:38:37 +0000 | [diff] [blame] | 70 | void *qemu_anon_ram_alloc(size_t size, uint64_t *align) |
Jes Sorensen | c1b0b93 | 2010-10-26 10:39:19 +0200 | [diff] [blame] | 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. */ |
Markus Armbruster | 3922825 | 2013-07-31 15:11:11 +0200 | [diff] [blame] | 77 | ptr = VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE); |
Paolo Bonzini | 6eebf95 | 2013-05-13 16:19:55 +0200 | [diff] [blame] | 78 | trace_qemu_anon_ram_alloc(size, ptr); |
Jes Sorensen | c1b0b93 | 2010-10-26 10:39:19 +0200 | [diff] [blame] | 79 | return ptr; |
| 80 | } |
| 81 | |
| 82 | void qemu_vfree(void *ptr) |
| 83 | { |
| 84 | trace_qemu_vfree(ptr); |
Markus Armbruster | 94c8ff3 | 2013-01-15 14:23:37 +0100 | [diff] [blame] | 85 | if (ptr) { |
| 86 | VirtualFree(ptr, 0, MEM_RELEASE); |
| 87 | } |
Jes Sorensen | c1b0b93 | 2010-10-26 10:39:19 +0200 | [diff] [blame] | 88 | } |
Jes Sorensen | 9549e76 | 2010-10-26 10:39:20 +0200 | [diff] [blame] | 89 | |
Paolo Bonzini | e7a09b9 | 2013-05-13 16:19:56 +0200 | [diff] [blame] | 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 | |
Stefan Weil | d3e8f95 | 2012-09-22 22:26:19 +0200 | [diff] [blame] | 98 | /* FIXME: add proper locking */ |
| 99 | struct tm *gmtime_r(const time_t *timep, struct tm *result) |
| 100 | { |
| 101 | struct tm *p = gmtime(timep); |
| 102 | memset(result, 0, sizeof(*result)); |
| 103 | if (p) { |
| 104 | *result = *p; |
| 105 | p = result; |
| 106 | } |
| 107 | return p; |
| 108 | } |
| 109 | |
| 110 | /* FIXME: add proper locking */ |
| 111 | struct tm *localtime_r(const time_t *timep, struct tm *result) |
| 112 | { |
| 113 | struct tm *p = localtime(timep); |
| 114 | memset(result, 0, sizeof(*result)); |
| 115 | if (p) { |
| 116 | *result = *p; |
| 117 | p = result; |
| 118 | } |
| 119 | return p; |
| 120 | } |
| 121 | |
Stefan Hajnoczi | f9e8cac | 2013-03-27 10:10:43 +0100 | [diff] [blame] | 122 | void qemu_set_block(int fd) |
Paolo Bonzini | 154b9a0 | 2011-10-05 09:17:32 +0200 | [diff] [blame] | 123 | { |
| 124 | unsigned long opt = 0; |
Paolo Bonzini | d3385eb | 2012-03-20 10:49:19 +0100 | [diff] [blame] | 125 | WSAEventSelect(fd, NULL, 0); |
Paolo Bonzini | 154b9a0 | 2011-10-05 09:17:32 +0200 | [diff] [blame] | 126 | ioctlsocket(fd, FIONBIO, &opt); |
| 127 | } |
| 128 | |
Stefan Hajnoczi | f9e8cac | 2013-03-27 10:10:43 +0100 | [diff] [blame] | 129 | void qemu_set_nonblock(int fd) |
Jes Sorensen | 9549e76 | 2010-10-26 10:39:20 +0200 | [diff] [blame] | 130 | { |
| 131 | unsigned long opt = 1; |
| 132 | ioctlsocket(fd, FIONBIO, &opt); |
Paolo Bonzini | d3385eb | 2012-03-20 10:49:19 +0100 | [diff] [blame] | 133 | qemu_fd_register(fd); |
Jes Sorensen | 9549e76 | 2010-10-26 10:39:20 +0200 | [diff] [blame] | 134 | } |
| 135 | |
Sebastian Ottlik | 606600a | 2013-10-02 12:23:12 +0200 | [diff] [blame] | 136 | int socket_set_fast_reuse(int fd) |
| 137 | { |
| 138 | /* Enabling the reuse of an endpoint that was used by a socket still in |
| 139 | * TIME_WAIT state is usually performed by setting SO_REUSEADDR. On Windows |
| 140 | * fast reuse is the default and SO_REUSEADDR does strange things. So we |
| 141 | * don't have to do anything here. More info can be found at: |
| 142 | * http://msdn.microsoft.com/en-us/library/windows/desktop/ms740621.aspx */ |
| 143 | return 0; |
| 144 | } |
| 145 | |
Jes Sorensen | 9549e76 | 2010-10-26 10:39:20 +0200 | [diff] [blame] | 146 | int inet_aton(const char *cp, struct in_addr *ia) |
| 147 | { |
| 148 | uint32_t addr = inet_addr(cp); |
| 149 | if (addr == 0xffffffff) { |
Stefan Weil | e637aa6 | 2014-05-28 17:42:24 +0200 | [diff] [blame] | 150 | return 0; |
Jes Sorensen | 9549e76 | 2010-10-26 10:39:20 +0200 | [diff] [blame] | 151 | } |
| 152 | ia->s_addr = addr; |
| 153 | return 1; |
| 154 | } |
| 155 | |
| 156 | void qemu_set_cloexec(int fd) |
| 157 | { |
| 158 | } |
Jes Sorensen | dc786bc | 2010-10-26 10:39:23 +0200 | [diff] [blame] | 159 | |
Jes Sorensen | dc786bc | 2010-10-26 10:39:23 +0200 | [diff] [blame] | 160 | /* Offset between 1/1/1601 and 1/1/1970 in 100 nanosec units */ |
| 161 | #define _W32_FT_OFFSET (116444736000000000ULL) |
| 162 | |
| 163 | int qemu_gettimeofday(qemu_timeval *tp) |
| 164 | { |
| 165 | union { |
| 166 | unsigned long long ns100; /*time since 1 Jan 1601 in 100ns units */ |
| 167 | FILETIME ft; |
| 168 | } _now; |
| 169 | |
| 170 | if(tp) { |
| 171 | GetSystemTimeAsFileTime (&_now.ft); |
| 172 | tp->tv_usec=(long)((_now.ns100 / 10ULL) % 1000000ULL ); |
| 173 | tp->tv_sec= (long)((_now.ns100 - _W32_FT_OFFSET) / 10000000ULL); |
| 174 | } |
| 175 | /* Always return 0 as per Open Group Base Specifications Issue 6. |
| 176 | Do not set errno on error. */ |
| 177 | return 0; |
| 178 | } |
Paolo Bonzini | cbcfa04 | 2011-09-12 16:20:11 +0200 | [diff] [blame] | 179 | |
| 180 | int qemu_get_thread_id(void) |
| 181 | { |
| 182 | return GetCurrentThreadId(); |
| 183 | } |
Laszlo Ersek | e2ea351 | 2013-05-18 06:31:48 +0200 | [diff] [blame] | 184 | |
| 185 | char * |
| 186 | qemu_get_local_state_pathname(const char *relative_pathname) |
| 187 | { |
| 188 | HRESULT result; |
| 189 | char base_path[MAX_PATH+1] = ""; |
| 190 | |
| 191 | result = SHGetFolderPath(NULL, CSIDL_COMMON_APPDATA, NULL, |
| 192 | /* SHGFP_TYPE_CURRENT */ 0, base_path); |
| 193 | if (result != S_OK) { |
| 194 | /* misconfigured environment */ |
| 195 | g_critical("CSIDL_COMMON_APPDATA unavailable: %ld", (long)result); |
| 196 | abort(); |
| 197 | } |
| 198 | return g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s", base_path, |
| 199 | relative_pathname); |
| 200 | } |
Stefan Hajnoczi | 13401ba | 2013-11-14 11:54:16 +0100 | [diff] [blame] | 201 | |
| 202 | void qemu_set_tty_echo(int fd, bool echo) |
| 203 | { |
| 204 | HANDLE handle = (HANDLE)_get_osfhandle(fd); |
| 205 | DWORD dwMode = 0; |
| 206 | |
| 207 | if (handle == INVALID_HANDLE_VALUE) { |
| 208 | return; |
| 209 | } |
| 210 | |
| 211 | GetConsoleMode(handle, &dwMode); |
| 212 | |
| 213 | if (echo) { |
| 214 | SetConsoleMode(handle, dwMode | ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT); |
| 215 | } else { |
| 216 | SetConsoleMode(handle, |
| 217 | dwMode & ~(ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT)); |
| 218 | } |
| 219 | } |
Fam Zheng | 10f5bff | 2014-02-10 14:48:51 +0800 | [diff] [blame] | 220 | |
| 221 | static char exec_dir[PATH_MAX]; |
| 222 | |
| 223 | void qemu_init_exec_dir(const char *argv0) |
| 224 | { |
| 225 | |
| 226 | char *p; |
| 227 | char buf[MAX_PATH]; |
| 228 | DWORD len; |
| 229 | |
| 230 | len = GetModuleFileName(NULL, buf, sizeof(buf) - 1); |
| 231 | if (len == 0) { |
| 232 | return; |
| 233 | } |
| 234 | |
| 235 | buf[len] = 0; |
| 236 | p = buf + len - 1; |
| 237 | while (p != buf && *p != '\\') { |
| 238 | p--; |
| 239 | } |
| 240 | *p = 0; |
| 241 | if (access(buf, R_OK) == 0) { |
| 242 | pstrcpy(exec_dir, sizeof(exec_dir), buf); |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | char *qemu_get_exec_dir(void) |
| 247 | { |
| 248 | return g_strdup(exec_dir); |
| 249 | } |
Sangho Park | 5a00754 | 2014-05-08 12:47:10 +0400 | [diff] [blame] | 250 | |
| 251 | /* |
Stefan Weil | e637aa6 | 2014-05-28 17:42:24 +0200 | [diff] [blame] | 252 | * The original implementation of g_poll from glib has a problem on Windows |
| 253 | * when using timeouts < 10 ms. |
Sangho Park | 5a00754 | 2014-05-08 12:47:10 +0400 | [diff] [blame] | 254 | * |
Stefan Weil | e637aa6 | 2014-05-28 17:42:24 +0200 | [diff] [blame] | 255 | * Whenever g_poll is called with timeout < 10 ms, it does a quick poll instead |
| 256 | * of wait. This causes significant performance degradation of QEMU. |
Sangho Park | 5a00754 | 2014-05-08 12:47:10 +0400 | [diff] [blame] | 257 | * |
Stefan Weil | e637aa6 | 2014-05-28 17:42:24 +0200 | [diff] [blame] | 258 | * The following code is a copy of the original code from glib/gpoll.c |
| 259 | * (glib commit 20f4d1820b8d4d0fc4447188e33efffd6d4a88d8 from 2014-02-19). |
| 260 | * Some debug code was removed and the code was reformatted. |
| 261 | * All other code modifications are marked with 'QEMU'. |
Sangho Park | 5a00754 | 2014-05-08 12:47:10 +0400 | [diff] [blame] | 262 | */ |
Stefan Weil | e637aa6 | 2014-05-28 17:42:24 +0200 | [diff] [blame] | 263 | |
| 264 | /* |
| 265 | * gpoll.c: poll(2) abstraction |
| 266 | * Copyright 1998 Owen Taylor |
| 267 | * Copyright 2008 Red Hat, Inc. |
| 268 | * |
| 269 | * This library is free software; you can redistribute it and/or |
| 270 | * modify it under the terms of the GNU Lesser General Public |
| 271 | * License as published by the Free Software Foundation; either |
| 272 | * version 2 of the License, or (at your option) any later version. |
| 273 | * |
| 274 | * This library is distributed in the hope that it will be useful, |
| 275 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 276 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 277 | * Lesser General Public License for more details. |
| 278 | * |
| 279 | * You should have received a copy of the GNU Lesser General Public |
| 280 | * License along with this library; if not, see <http://www.gnu.org/licenses/>. |
| 281 | */ |
| 282 | |
| 283 | static int poll_rest(gboolean poll_msgs, HANDLE *handles, gint nhandles, |
| 284 | GPollFD *fds, guint nfds, gint timeout) |
Sangho Park | 5a00754 | 2014-05-08 12:47:10 +0400 | [diff] [blame] | 285 | { |
Stefan Weil | e637aa6 | 2014-05-28 17:42:24 +0200 | [diff] [blame] | 286 | DWORD ready; |
| 287 | GPollFD *f; |
| 288 | int recursed_result; |
Sangho Park | 5a00754 | 2014-05-08 12:47:10 +0400 | [diff] [blame] | 289 | |
Stefan Weil | e637aa6 | 2014-05-28 17:42:24 +0200 | [diff] [blame] | 290 | if (poll_msgs) { |
| 291 | /* Wait for either messages or handles |
| 292 | * -> Use MsgWaitForMultipleObjectsEx |
Sangho Park | 5a00754 | 2014-05-08 12:47:10 +0400 | [diff] [blame] | 293 | */ |
Stefan Weil | e637aa6 | 2014-05-28 17:42:24 +0200 | [diff] [blame] | 294 | ready = MsgWaitForMultipleObjectsEx(nhandles, handles, timeout, |
| 295 | QS_ALLINPUT, MWMO_ALERTABLE); |
| 296 | |
| 297 | if (ready == WAIT_FAILED) { |
| 298 | gchar *emsg = g_win32_error_message(GetLastError()); |
| 299 | g_warning("MsgWaitForMultipleObjectsEx failed: %s", emsg); |
| 300 | g_free(emsg); |
| 301 | } |
| 302 | } else if (nhandles == 0) { |
| 303 | /* No handles to wait for, just the timeout */ |
| 304 | if (timeout == INFINITE) { |
| 305 | ready = WAIT_FAILED; |
| 306 | } else { |
| 307 | SleepEx(timeout, TRUE); |
| 308 | ready = WAIT_TIMEOUT; |
| 309 | } |
| 310 | } else { |
| 311 | /* Wait for just handles |
| 312 | * -> Use WaitForMultipleObjectsEx |
| 313 | */ |
| 314 | ready = |
| 315 | WaitForMultipleObjectsEx(nhandles, handles, FALSE, timeout, TRUE); |
| 316 | if (ready == WAIT_FAILED) { |
| 317 | gchar *emsg = g_win32_error_message(GetLastError()); |
| 318 | g_warning("WaitForMultipleObjectsEx failed: %s", emsg); |
| 319 | g_free(emsg); |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | if (ready == WAIT_FAILED) { |
| 324 | return -1; |
| 325 | } else if (ready == WAIT_TIMEOUT || ready == WAIT_IO_COMPLETION) { |
| 326 | return 0; |
| 327 | } else if (poll_msgs && ready == WAIT_OBJECT_0 + nhandles) { |
| 328 | for (f = fds; f < &fds[nfds]; ++f) { |
| 329 | if (f->fd == G_WIN32_MSG_HANDLE && f->events & G_IO_IN) { |
| 330 | f->revents |= G_IO_IN; |
Sangho Park | 5a00754 | 2014-05-08 12:47:10 +0400 | [diff] [blame] | 331 | } |
| 332 | } |
| 333 | |
Stefan Weil | e637aa6 | 2014-05-28 17:42:24 +0200 | [diff] [blame] | 334 | /* If we have a timeout, or no handles to poll, be satisfied |
| 335 | * with just noticing we have messages waiting. |
| 336 | */ |
| 337 | if (timeout != 0 || nhandles == 0) { |
| 338 | return 1; |
| 339 | } |
| 340 | |
| 341 | /* If no timeout and handles to poll, recurse to poll them, |
| 342 | * too. |
| 343 | */ |
| 344 | recursed_result = poll_rest(FALSE, handles, nhandles, fds, nfds, 0); |
| 345 | return (recursed_result == -1) ? -1 : 1 + recursed_result; |
| 346 | } else if (/* QEMU: removed the following unneeded statement which causes |
| 347 | * a compiler warning: ready >= WAIT_OBJECT_0 && */ |
| 348 | ready < WAIT_OBJECT_0 + nhandles) { |
| 349 | for (f = fds; f < &fds[nfds]; ++f) { |
| 350 | if ((HANDLE) f->fd == handles[ready - WAIT_OBJECT_0]) { |
| 351 | f->revents = f->events; |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | /* If no timeout and polling several handles, recurse to poll |
| 356 | * the rest of them. |
| 357 | */ |
| 358 | if (timeout == 0 && nhandles > 1) { |
| 359 | /* Remove the handle that fired */ |
| 360 | int i; |
| 361 | if (ready < nhandles - 1) { |
| 362 | for (i = ready - WAIT_OBJECT_0 + 1; i < nhandles; i++) { |
| 363 | handles[i-1] = handles[i]; |
| 364 | } |
| 365 | } |
| 366 | nhandles--; |
| 367 | recursed_result = poll_rest(FALSE, handles, nhandles, fds, nfds, 0); |
| 368 | return (recursed_result == -1) ? -1 : 1 + recursed_result; |
| 369 | } |
| 370 | return 1; |
| 371 | } |
| 372 | |
| 373 | return 0; |
| 374 | } |
| 375 | |
| 376 | gint g_poll(GPollFD *fds, guint nfds, gint timeout) |
| 377 | { |
| 378 | HANDLE handles[MAXIMUM_WAIT_OBJECTS]; |
| 379 | gboolean poll_msgs = FALSE; |
| 380 | GPollFD *f; |
| 381 | gint nhandles = 0; |
| 382 | int retval; |
| 383 | |
| 384 | for (f = fds; f < &fds[nfds]; ++f) { |
| 385 | if (f->fd == G_WIN32_MSG_HANDLE && (f->events & G_IO_IN)) { |
| 386 | poll_msgs = TRUE; |
| 387 | } else if (f->fd > 0) { |
| 388 | /* Don't add the same handle several times into the array, as |
| 389 | * docs say that is not allowed, even if it actually does seem |
| 390 | * to work. |
| 391 | */ |
| 392 | gint i; |
| 393 | |
| 394 | for (i = 0; i < nhandles; i++) { |
| 395 | if (handles[i] == (HANDLE) f->fd) { |
| 396 | break; |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | if (i == nhandles) { |
| 401 | if (nhandles == MAXIMUM_WAIT_OBJECTS) { |
| 402 | g_warning("Too many handles to wait for!\n"); |
| 403 | break; |
| 404 | } else { |
| 405 | handles[nhandles++] = (HANDLE) f->fd; |
| 406 | } |
Sangho Park | 5a00754 | 2014-05-08 12:47:10 +0400 | [diff] [blame] | 407 | } |
| 408 | } |
| 409 | } |
| 410 | |
Stefan Weil | e637aa6 | 2014-05-28 17:42:24 +0200 | [diff] [blame] | 411 | for (f = fds; f < &fds[nfds]; ++f) { |
| 412 | f->revents = 0; |
Sangho Park | 5a00754 | 2014-05-08 12:47:10 +0400 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | if (timeout == -1) { |
| 416 | timeout = INFINITE; |
| 417 | } |
| 418 | |
Stefan Weil | e637aa6 | 2014-05-28 17:42:24 +0200 | [diff] [blame] | 419 | /* Polling for several things? */ |
| 420 | if (nhandles > 1 || (nhandles > 0 && poll_msgs)) { |
| 421 | /* First check if one or several of them are immediately |
| 422 | * available |
Sangho Park | 5a00754 | 2014-05-08 12:47:10 +0400 | [diff] [blame] | 423 | */ |
Stefan Weil | e637aa6 | 2014-05-28 17:42:24 +0200 | [diff] [blame] | 424 | retval = poll_rest(poll_msgs, handles, nhandles, fds, nfds, 0); |
Sangho Park | 5a00754 | 2014-05-08 12:47:10 +0400 | [diff] [blame] | 425 | |
Stefan Weil | e637aa6 | 2014-05-28 17:42:24 +0200 | [diff] [blame] | 426 | /* If not, and we have a significant timeout, poll again with |
| 427 | * timeout then. Note that this will return indication for only |
| 428 | * one event, or only for messages. We ignore timeouts less than |
| 429 | * ten milliseconds as they are mostly pointless on Windows, the |
| 430 | * MsgWaitForMultipleObjectsEx() call will timeout right away |
| 431 | * anyway. |
| 432 | * |
| 433 | * Modification for QEMU: replaced timeout >= 10 by timeout > 0. |
| 434 | */ |
| 435 | if (retval == 0 && (timeout == INFINITE || timeout > 0)) { |
| 436 | retval = poll_rest(poll_msgs, handles, nhandles, |
| 437 | fds, nfds, timeout); |
| 438 | } |
| 439 | } else { |
| 440 | /* Just polling for one thing, so no need to check first if |
| 441 | * available immediately |
| 442 | */ |
| 443 | retval = poll_rest(poll_msgs, handles, nhandles, fds, nfds, timeout); |
Sangho Park | 5a00754 | 2014-05-08 12:47:10 +0400 | [diff] [blame] | 444 | } |
| 445 | |
Stefan Weil | e637aa6 | 2014-05-28 17:42:24 +0200 | [diff] [blame] | 446 | if (retval == -1) { |
| 447 | for (f = fds; f < &fds[nfds]; ++f) { |
| 448 | f->revents = 0; |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | return retval; |
Sangho Park | 5a00754 | 2014-05-08 12:47:10 +0400 | [diff] [blame] | 453 | } |
Paolo Bonzini | 3818331 | 2014-05-14 17:43:21 +0800 | [diff] [blame] | 454 | |
| 455 | size_t getpagesize(void) |
| 456 | { |
| 457 | SYSTEM_INFO system_info; |
| 458 | |
| 459 | GetSystemInfo(&system_info); |
| 460 | return system_info.dwPageSize; |
| 461 | } |
| 462 | |
| 463 | void os_mem_prealloc(int fd, char *area, size_t memory) |
| 464 | { |
| 465 | int i; |
| 466 | size_t pagesize = getpagesize(); |
| 467 | |
| 468 | memory = (memory + pagesize - 1) & -pagesize; |
| 469 | for (i = 0; i < memory / pagesize; i++) { |
| 470 | memset(area + pagesize * i, 0, 1); |
| 471 | } |
| 472 | } |