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