1 /* 2 * os-win32.c 3 * 4 * Copyright (c) 2003-2008 Fabrice Bellard 5 * Copyright (c) 2010-2016 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 33 #include "qemu/osdep.h" 34 #include <windows.h> 35 #include "qemu-common.h" 36 #include "qapi/error.h" 37 #include "qemu/main-loop.h" 38 #include "trace.h" 39 #include "qemu/sockets.h" 40 #include "qemu/cutils.h" 41 #include "qemu/error-report.h" 42 #include <malloc.h> 43 44 /* this must come after including "trace.h" */ 45 #include <shlobj.h> 46 47 void *qemu_oom_check(void *ptr) 48 { 49 if (ptr == NULL) { 50 fprintf(stderr, "Failed to allocate memory: %lu\n", GetLastError()); 51 abort(); 52 } 53 return ptr; 54 } 55 56 void *qemu_try_memalign(size_t alignment, size_t size) 57 { 58 void *ptr; 59 60 g_assert(size != 0); 61 g_assert(is_power_of_2(alignment)); 62 ptr = _aligned_malloc(size, alignment); 63 trace_qemu_memalign(alignment, size, ptr); 64 return ptr; 65 } 66 67 void *qemu_memalign(size_t alignment, size_t size) 68 { 69 return qemu_oom_check(qemu_try_memalign(alignment, size)); 70 } 71 72 static int get_allocation_granularity(void) 73 { 74 SYSTEM_INFO system_info; 75 76 GetSystemInfo(&system_info); 77 return system_info.dwAllocationGranularity; 78 } 79 80 void *qemu_anon_ram_alloc(size_t size, uint64_t *align, bool shared, 81 bool noreserve) 82 { 83 void *ptr; 84 85 if (noreserve) { 86 /* 87 * We need a MEM_COMMIT before accessing any memory in a MEM_RESERVE 88 * area; we cannot easily mimic POSIX MAP_NORESERVE semantics. 89 */ 90 error_report("Skipping reservation of swap space is not supported."); 91 return NULL; 92 } 93 94 ptr = VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE); 95 trace_qemu_anon_ram_alloc(size, ptr); 96 97 if (ptr && align) { 98 *align = MAX(get_allocation_granularity(), getpagesize()); 99 } 100 return ptr; 101 } 102 103 void qemu_vfree(void *ptr) 104 { 105 trace_qemu_vfree(ptr); 106 _aligned_free(ptr); 107 } 108 109 void qemu_anon_ram_free(void *ptr, size_t size) 110 { 111 trace_qemu_anon_ram_free(ptr, size); 112 if (ptr) { 113 VirtualFree(ptr, 0, MEM_RELEASE); 114 } 115 } 116 117 #ifndef _POSIX_THREAD_SAFE_FUNCTIONS 118 /* FIXME: add proper locking */ 119 struct tm *gmtime_r(const time_t *timep, struct tm *result) 120 { 121 struct tm *p = gmtime(timep); 122 memset(result, 0, sizeof(*result)); 123 if (p) { 124 *result = *p; 125 p = result; 126 } 127 return p; 128 } 129 130 /* FIXME: add proper locking */ 131 struct tm *localtime_r(const time_t *timep, struct tm *result) 132 { 133 struct tm *p = localtime(timep); 134 memset(result, 0, sizeof(*result)); 135 if (p) { 136 *result = *p; 137 p = result; 138 } 139 return p; 140 } 141 #endif /* _POSIX_THREAD_SAFE_FUNCTIONS */ 142 143 static int socket_error(void) 144 { 145 switch (WSAGetLastError()) { 146 case 0: 147 return 0; 148 case WSAEINTR: 149 return EINTR; 150 case WSAEINVAL: 151 return EINVAL; 152 case WSA_INVALID_HANDLE: 153 return EBADF; 154 case WSA_NOT_ENOUGH_MEMORY: 155 return ENOMEM; 156 case WSA_INVALID_PARAMETER: 157 return EINVAL; 158 case WSAENAMETOOLONG: 159 return ENAMETOOLONG; 160 case WSAENOTEMPTY: 161 return ENOTEMPTY; 162 case WSAEWOULDBLOCK: 163 /* not using EWOULDBLOCK as we don't want code to have 164 * to check both EWOULDBLOCK and EAGAIN */ 165 return EAGAIN; 166 case WSAEINPROGRESS: 167 return EINPROGRESS; 168 case WSAEALREADY: 169 return EALREADY; 170 case WSAENOTSOCK: 171 return ENOTSOCK; 172 case WSAEDESTADDRREQ: 173 return EDESTADDRREQ; 174 case WSAEMSGSIZE: 175 return EMSGSIZE; 176 case WSAEPROTOTYPE: 177 return EPROTOTYPE; 178 case WSAENOPROTOOPT: 179 return ENOPROTOOPT; 180 case WSAEPROTONOSUPPORT: 181 return EPROTONOSUPPORT; 182 case WSAEOPNOTSUPP: 183 return EOPNOTSUPP; 184 case WSAEAFNOSUPPORT: 185 return EAFNOSUPPORT; 186 case WSAEADDRINUSE: 187 return EADDRINUSE; 188 case WSAEADDRNOTAVAIL: 189 return EADDRNOTAVAIL; 190 case WSAENETDOWN: 191 return ENETDOWN; 192 case WSAENETUNREACH: 193 return ENETUNREACH; 194 case WSAENETRESET: 195 return ENETRESET; 196 case WSAECONNABORTED: 197 return ECONNABORTED; 198 case WSAECONNRESET: 199 return ECONNRESET; 200 case WSAENOBUFS: 201 return ENOBUFS; 202 case WSAEISCONN: 203 return EISCONN; 204 case WSAENOTCONN: 205 return ENOTCONN; 206 case WSAETIMEDOUT: 207 return ETIMEDOUT; 208 case WSAECONNREFUSED: 209 return ECONNREFUSED; 210 case WSAELOOP: 211 return ELOOP; 212 case WSAEHOSTUNREACH: 213 return EHOSTUNREACH; 214 default: 215 return EIO; 216 } 217 } 218 219 void qemu_set_block(int fd) 220 { 221 unsigned long opt = 0; 222 WSAEventSelect(fd, NULL, 0); 223 ioctlsocket(fd, FIONBIO, &opt); 224 } 225 226 int qemu_try_set_nonblock(int fd) 227 { 228 unsigned long opt = 1; 229 if (ioctlsocket(fd, FIONBIO, &opt) != NO_ERROR) { 230 return -socket_error(); 231 } 232 return 0; 233 } 234 235 void qemu_set_nonblock(int fd) 236 { 237 (void)qemu_try_set_nonblock(fd); 238 } 239 240 int socket_set_fast_reuse(int fd) 241 { 242 /* Enabling the reuse of an endpoint that was used by a socket still in 243 * TIME_WAIT state is usually performed by setting SO_REUSEADDR. On Windows 244 * fast reuse is the default and SO_REUSEADDR does strange things. So we 245 * don't have to do anything here. More info can be found at: 246 * http://msdn.microsoft.com/en-us/library/windows/desktop/ms740621.aspx */ 247 return 0; 248 } 249 250 int inet_aton(const char *cp, struct in_addr *ia) 251 { 252 uint32_t addr = inet_addr(cp); 253 if (addr == 0xffffffff) { 254 return 0; 255 } 256 ia->s_addr = addr; 257 return 1; 258 } 259 260 void qemu_set_cloexec(int fd) 261 { 262 } 263 264 /* Offset between 1/1/1601 and 1/1/1970 in 100 nanosec units */ 265 #define _W32_FT_OFFSET (116444736000000000ULL) 266 267 int qemu_gettimeofday(qemu_timeval *tp) 268 { 269 union { 270 unsigned long long ns100; /*time since 1 Jan 1601 in 100ns units */ 271 FILETIME ft; 272 } _now; 273 274 if(tp) { 275 GetSystemTimeAsFileTime (&_now.ft); 276 tp->tv_usec=(long)((_now.ns100 / 10ULL) % 1000000ULL ); 277 tp->tv_sec= (long)((_now.ns100 - _W32_FT_OFFSET) / 10000000ULL); 278 } 279 /* Always return 0 as per Open Group Base Specifications Issue 6. 280 Do not set errno on error. */ 281 return 0; 282 } 283 284 int qemu_get_thread_id(void) 285 { 286 return GetCurrentThreadId(); 287 } 288 289 char * 290 qemu_get_local_state_pathname(const char *relative_pathname) 291 { 292 HRESULT result; 293 char base_path[MAX_PATH+1] = ""; 294 295 result = SHGetFolderPath(NULL, CSIDL_COMMON_APPDATA, NULL, 296 /* SHGFP_TYPE_CURRENT */ 0, base_path); 297 if (result != S_OK) { 298 /* misconfigured environment */ 299 g_critical("CSIDL_COMMON_APPDATA unavailable: %ld", (long)result); 300 abort(); 301 } 302 return g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s", base_path, 303 relative_pathname); 304 } 305 306 void qemu_set_tty_echo(int fd, bool echo) 307 { 308 HANDLE handle = (HANDLE)_get_osfhandle(fd); 309 DWORD dwMode = 0; 310 311 if (handle == INVALID_HANDLE_VALUE) { 312 return; 313 } 314 315 GetConsoleMode(handle, &dwMode); 316 317 if (echo) { 318 SetConsoleMode(handle, dwMode | ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT); 319 } else { 320 SetConsoleMode(handle, 321 dwMode & ~(ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT)); 322 } 323 } 324 325 static const char *exec_dir; 326 327 void qemu_init_exec_dir(const char *argv0) 328 { 329 330 char *p; 331 char buf[MAX_PATH]; 332 DWORD len; 333 334 if (exec_dir) { 335 return; 336 } 337 338 len = GetModuleFileName(NULL, buf, sizeof(buf) - 1); 339 if (len == 0) { 340 return; 341 } 342 343 buf[len] = 0; 344 p = buf + len - 1; 345 while (p != buf && *p != '\\') { 346 p--; 347 } 348 *p = 0; 349 if (access(buf, R_OK) == 0) { 350 exec_dir = g_strdup(buf); 351 } else { 352 exec_dir = CONFIG_BINDIR; 353 } 354 } 355 356 const char *qemu_get_exec_dir(void) 357 { 358 return exec_dir; 359 } 360 361 int getpagesize(void) 362 { 363 SYSTEM_INFO system_info; 364 365 GetSystemInfo(&system_info); 366 return system_info.dwPageSize; 367 } 368 369 void os_mem_prealloc(int fd, char *area, size_t memory, int smp_cpus, 370 Error **errp) 371 { 372 int i; 373 size_t pagesize = qemu_real_host_page_size; 374 375 memory = (memory + pagesize - 1) & -pagesize; 376 for (i = 0; i < memory / pagesize; i++) { 377 memset(area + pagesize * i, 0, 1); 378 } 379 } 380 381 char *qemu_get_pid_name(pid_t pid) 382 { 383 /* XXX Implement me */ 384 abort(); 385 } 386 387 388 pid_t qemu_fork(Error **errp) 389 { 390 errno = ENOSYS; 391 error_setg_errno(errp, errno, 392 "cannot fork child process"); 393 return -1; 394 } 395 396 397 #undef connect 398 int qemu_connect_wrap(int sockfd, const struct sockaddr *addr, 399 socklen_t addrlen) 400 { 401 int ret; 402 ret = connect(sockfd, addr, addrlen); 403 if (ret < 0) { 404 if (WSAGetLastError() == WSAEWOULDBLOCK) { 405 errno = EINPROGRESS; 406 } else { 407 errno = socket_error(); 408 } 409 } 410 return ret; 411 } 412 413 414 #undef listen 415 int qemu_listen_wrap(int sockfd, int backlog) 416 { 417 int ret; 418 ret = listen(sockfd, backlog); 419 if (ret < 0) { 420 errno = socket_error(); 421 } 422 return ret; 423 } 424 425 426 #undef bind 427 int qemu_bind_wrap(int sockfd, const struct sockaddr *addr, 428 socklen_t addrlen) 429 { 430 int ret; 431 ret = bind(sockfd, addr, addrlen); 432 if (ret < 0) { 433 errno = socket_error(); 434 } 435 return ret; 436 } 437 438 439 #undef socket 440 int qemu_socket_wrap(int domain, int type, int protocol) 441 { 442 int ret; 443 ret = socket(domain, type, protocol); 444 if (ret < 0) { 445 errno = socket_error(); 446 } 447 return ret; 448 } 449 450 451 #undef accept 452 int qemu_accept_wrap(int sockfd, struct sockaddr *addr, 453 socklen_t *addrlen) 454 { 455 int ret; 456 ret = accept(sockfd, addr, addrlen); 457 if (ret < 0) { 458 errno = socket_error(); 459 } 460 return ret; 461 } 462 463 464 #undef shutdown 465 int qemu_shutdown_wrap(int sockfd, int how) 466 { 467 int ret; 468 ret = shutdown(sockfd, how); 469 if (ret < 0) { 470 errno = socket_error(); 471 } 472 return ret; 473 } 474 475 476 #undef ioctlsocket 477 int qemu_ioctlsocket_wrap(int fd, int req, void *val) 478 { 479 int ret; 480 ret = ioctlsocket(fd, req, val); 481 if (ret < 0) { 482 errno = socket_error(); 483 } 484 return ret; 485 } 486 487 488 #undef closesocket 489 int qemu_closesocket_wrap(int fd) 490 { 491 int ret; 492 ret = closesocket(fd); 493 if (ret < 0) { 494 errno = socket_error(); 495 } 496 return ret; 497 } 498 499 500 #undef getsockopt 501 int qemu_getsockopt_wrap(int sockfd, int level, int optname, 502 void *optval, socklen_t *optlen) 503 { 504 int ret; 505 ret = getsockopt(sockfd, level, optname, optval, optlen); 506 if (ret < 0) { 507 errno = socket_error(); 508 } 509 return ret; 510 } 511 512 513 #undef setsockopt 514 int qemu_setsockopt_wrap(int sockfd, int level, int optname, 515 const void *optval, socklen_t optlen) 516 { 517 int ret; 518 ret = setsockopt(sockfd, level, optname, optval, optlen); 519 if (ret < 0) { 520 errno = socket_error(); 521 } 522 return ret; 523 } 524 525 526 #undef getpeername 527 int qemu_getpeername_wrap(int sockfd, struct sockaddr *addr, 528 socklen_t *addrlen) 529 { 530 int ret; 531 ret = getpeername(sockfd, addr, addrlen); 532 if (ret < 0) { 533 errno = socket_error(); 534 } 535 return ret; 536 } 537 538 539 #undef getsockname 540 int qemu_getsockname_wrap(int sockfd, struct sockaddr *addr, 541 socklen_t *addrlen) 542 { 543 int ret; 544 ret = getsockname(sockfd, addr, addrlen); 545 if (ret < 0) { 546 errno = socket_error(); 547 } 548 return ret; 549 } 550 551 552 #undef send 553 ssize_t qemu_send_wrap(int sockfd, const void *buf, size_t len, int flags) 554 { 555 int ret; 556 ret = send(sockfd, buf, len, flags); 557 if (ret < 0) { 558 errno = socket_error(); 559 } 560 return ret; 561 } 562 563 564 #undef sendto 565 ssize_t qemu_sendto_wrap(int sockfd, const void *buf, size_t len, int flags, 566 const struct sockaddr *addr, socklen_t addrlen) 567 { 568 int ret; 569 ret = sendto(sockfd, buf, len, flags, addr, addrlen); 570 if (ret < 0) { 571 errno = socket_error(); 572 } 573 return ret; 574 } 575 576 577 #undef recv 578 ssize_t qemu_recv_wrap(int sockfd, void *buf, size_t len, int flags) 579 { 580 int ret; 581 ret = recv(sockfd, buf, len, flags); 582 if (ret < 0) { 583 errno = socket_error(); 584 } 585 return ret; 586 } 587 588 589 #undef recvfrom 590 ssize_t qemu_recvfrom_wrap(int sockfd, void *buf, size_t len, int flags, 591 struct sockaddr *addr, socklen_t *addrlen) 592 { 593 int ret; 594 ret = recvfrom(sockfd, buf, len, flags, addr, addrlen); 595 if (ret < 0) { 596 errno = socket_error(); 597 } 598 return ret; 599 } 600 601 bool qemu_write_pidfile(const char *filename, Error **errp) 602 { 603 char buffer[128]; 604 int len; 605 HANDLE file; 606 OVERLAPPED overlap; 607 BOOL ret; 608 memset(&overlap, 0, sizeof(overlap)); 609 610 file = CreateFile(filename, GENERIC_WRITE, FILE_SHARE_READ, NULL, 611 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); 612 613 if (file == INVALID_HANDLE_VALUE) { 614 error_setg(errp, "Failed to create PID file"); 615 return false; 616 } 617 len = snprintf(buffer, sizeof(buffer), FMT_pid "\n", (pid_t)getpid()); 618 ret = WriteFile(file, (LPCVOID)buffer, (DWORD)len, 619 NULL, &overlap); 620 CloseHandle(file); 621 if (ret == 0) { 622 error_setg(errp, "Failed to write PID file"); 623 return false; 624 } 625 return true; 626 } 627 628 char *qemu_get_host_name(Error **errp) 629 { 630 wchar_t tmp[MAX_COMPUTERNAME_LENGTH + 1]; 631 DWORD size = G_N_ELEMENTS(tmp); 632 633 if (GetComputerNameW(tmp, &size) == 0) { 634 error_setg_win32(errp, GetLastError(), "failed close handle"); 635 return NULL; 636 } 637 638 return g_utf16_to_utf8(tmp, size, NULL, NULL, NULL); 639 } 640 641 size_t qemu_get_host_physmem(void) 642 { 643 MEMORYSTATUSEX statex; 644 statex.dwLength = sizeof(statex); 645 646 if (GlobalMemoryStatusEx(&statex)) { 647 return statex.ullTotalPhys; 648 } 649 return 0; 650 } 651