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 |
Daniel P. Berrange | c619644 | 2016-03-07 11:19:18 +0000 | [diff] [blame] | 5 | * Copyright (c) 2010-2016 Red Hat, Inc. |
Jes Sorensen | c1b0b93 | 2010-10-26 10:39:19 +0200 | [diff] [blame] | 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 | */ |
Peter Maydell | aafd758 | 2016-01-29 17:49:55 +0000 | [diff] [blame] | 32 | #include "qemu/osdep.h" |
Jes Sorensen | c1b0b93 | 2010-10-26 10:39:19 +0200 | [diff] [blame] | 33 | #include <windows.h> |
Markus Armbruster | da34e65 | 2016-03-14 09:01:28 +0100 | [diff] [blame] | 34 | #include "qapi/error.h" |
Paolo Bonzini | 9c17d61 | 2012-12-17 18:20:04 +0100 | [diff] [blame] | 35 | #include "sysemu/sysemu.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 36 | #include "qemu/main-loop.h" |
Jes Sorensen | c1b0b93 | 2010-10-26 10:39:19 +0200 | [diff] [blame] | 37 | #include "trace.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 38 | #include "qemu/sockets.h" |
Veronia Bahaa | f348b6d | 2016-03-20 19:16:19 +0200 | [diff] [blame] | 39 | #include "qemu/cutils.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 | |
Daniel P. Berrange | 4d9310f | 2015-09-22 15:13:26 +0100 | [diff] [blame] | 98 | #ifndef CONFIG_LOCALTIME_R |
Stefan Weil | d3e8f95 | 2012-09-22 22:26:19 +0200 | [diff] [blame] | 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 | } |
Daniel P. Berrange | 4d9310f | 2015-09-22 15:13:26 +0100 | [diff] [blame] | 122 | #endif /* CONFIG_LOCALTIME_R */ |
Stefan Weil | d3e8f95 | 2012-09-22 22:26:19 +0200 | [diff] [blame] | 123 | |
Stefan Hajnoczi | f9e8cac | 2013-03-27 10:10:43 +0100 | [diff] [blame] | 124 | void qemu_set_block(int fd) |
Paolo Bonzini | 154b9a0 | 2011-10-05 09:17:32 +0200 | [diff] [blame] | 125 | { |
| 126 | unsigned long opt = 0; |
Paolo Bonzini | d3385eb | 2012-03-20 10:49:19 +0100 | [diff] [blame] | 127 | WSAEventSelect(fd, NULL, 0); |
Paolo Bonzini | 154b9a0 | 2011-10-05 09:17:32 +0200 | [diff] [blame] | 128 | ioctlsocket(fd, FIONBIO, &opt); |
| 129 | } |
| 130 | |
Stefan Hajnoczi | f9e8cac | 2013-03-27 10:10:43 +0100 | [diff] [blame] | 131 | void qemu_set_nonblock(int fd) |
Jes Sorensen | 9549e76 | 2010-10-26 10:39:20 +0200 | [diff] [blame] | 132 | { |
| 133 | unsigned long opt = 1; |
| 134 | ioctlsocket(fd, FIONBIO, &opt); |
Paolo Bonzini | d3385eb | 2012-03-20 10:49:19 +0100 | [diff] [blame] | 135 | qemu_fd_register(fd); |
Jes Sorensen | 9549e76 | 2010-10-26 10:39:20 +0200 | [diff] [blame] | 136 | } |
| 137 | |
Sebastian Ottlik | 606600a | 2013-10-02 12:23:12 +0200 | [diff] [blame] | 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 | |
Daniel P. Berrange | c619644 | 2016-03-07 11:19:18 +0000 | [diff] [blame] | 148 | |
Daniel P. Berrange | b16a44e | 2016-03-07 20:36:03 +0000 | [diff] [blame] | 149 | static int socket_error(void) |
Daniel P. Berrange | c619644 | 2016-03-07 11:19:18 +0000 | [diff] [blame] | 150 | { |
| 151 | switch (WSAGetLastError()) { |
| 152 | case 0: |
| 153 | return 0; |
| 154 | case WSAEINTR: |
| 155 | return EINTR; |
| 156 | case WSAEINVAL: |
| 157 | return EINVAL; |
| 158 | case WSA_INVALID_HANDLE: |
| 159 | return EBADF; |
| 160 | case WSA_NOT_ENOUGH_MEMORY: |
| 161 | return ENOMEM; |
| 162 | case WSA_INVALID_PARAMETER: |
| 163 | return EINVAL; |
| 164 | case WSAENAMETOOLONG: |
| 165 | return ENAMETOOLONG; |
| 166 | case WSAENOTEMPTY: |
| 167 | return ENOTEMPTY; |
| 168 | case WSAEWOULDBLOCK: |
| 169 | /* not using EWOULDBLOCK as we don't want code to have |
| 170 | * to check both EWOULDBLOCK and EAGAIN */ |
| 171 | return EAGAIN; |
| 172 | case WSAEINPROGRESS: |
| 173 | return EINPROGRESS; |
| 174 | case WSAEALREADY: |
| 175 | return EALREADY; |
| 176 | case WSAENOTSOCK: |
| 177 | return ENOTSOCK; |
| 178 | case WSAEDESTADDRREQ: |
| 179 | return EDESTADDRREQ; |
| 180 | case WSAEMSGSIZE: |
| 181 | return EMSGSIZE; |
| 182 | case WSAEPROTOTYPE: |
| 183 | return EPROTOTYPE; |
| 184 | case WSAENOPROTOOPT: |
| 185 | return ENOPROTOOPT; |
| 186 | case WSAEPROTONOSUPPORT: |
| 187 | return EPROTONOSUPPORT; |
| 188 | case WSAEOPNOTSUPP: |
| 189 | return EOPNOTSUPP; |
| 190 | case WSAEAFNOSUPPORT: |
| 191 | return EAFNOSUPPORT; |
| 192 | case WSAEADDRINUSE: |
| 193 | return EADDRINUSE; |
| 194 | case WSAEADDRNOTAVAIL: |
| 195 | return EADDRNOTAVAIL; |
| 196 | case WSAENETDOWN: |
| 197 | return ENETDOWN; |
| 198 | case WSAENETUNREACH: |
| 199 | return ENETUNREACH; |
| 200 | case WSAENETRESET: |
| 201 | return ENETRESET; |
| 202 | case WSAECONNABORTED: |
| 203 | return ECONNABORTED; |
| 204 | case WSAECONNRESET: |
| 205 | return ECONNRESET; |
| 206 | case WSAENOBUFS: |
| 207 | return ENOBUFS; |
| 208 | case WSAEISCONN: |
| 209 | return EISCONN; |
| 210 | case WSAENOTCONN: |
| 211 | return ENOTCONN; |
| 212 | case WSAETIMEDOUT: |
| 213 | return ETIMEDOUT; |
| 214 | case WSAECONNREFUSED: |
| 215 | return ECONNREFUSED; |
| 216 | case WSAELOOP: |
| 217 | return ELOOP; |
| 218 | case WSAEHOSTUNREACH: |
| 219 | return EHOSTUNREACH; |
| 220 | default: |
| 221 | return EIO; |
| 222 | } |
| 223 | } |
| 224 | |
Jes Sorensen | 9549e76 | 2010-10-26 10:39:20 +0200 | [diff] [blame] | 225 | int inet_aton(const char *cp, struct in_addr *ia) |
| 226 | { |
| 227 | uint32_t addr = inet_addr(cp); |
| 228 | if (addr == 0xffffffff) { |
Stefan Weil | e637aa6 | 2014-05-28 17:42:24 +0200 | [diff] [blame] | 229 | return 0; |
Jes Sorensen | 9549e76 | 2010-10-26 10:39:20 +0200 | [diff] [blame] | 230 | } |
| 231 | ia->s_addr = addr; |
| 232 | return 1; |
| 233 | } |
| 234 | |
| 235 | void qemu_set_cloexec(int fd) |
| 236 | { |
| 237 | } |
Jes Sorensen | dc786bc | 2010-10-26 10:39:23 +0200 | [diff] [blame] | 238 | |
Jes Sorensen | dc786bc | 2010-10-26 10:39:23 +0200 | [diff] [blame] | 239 | /* Offset between 1/1/1601 and 1/1/1970 in 100 nanosec units */ |
| 240 | #define _W32_FT_OFFSET (116444736000000000ULL) |
| 241 | |
| 242 | int qemu_gettimeofday(qemu_timeval *tp) |
| 243 | { |
| 244 | union { |
| 245 | unsigned long long ns100; /*time since 1 Jan 1601 in 100ns units */ |
| 246 | FILETIME ft; |
| 247 | } _now; |
| 248 | |
| 249 | if(tp) { |
| 250 | GetSystemTimeAsFileTime (&_now.ft); |
| 251 | tp->tv_usec=(long)((_now.ns100 / 10ULL) % 1000000ULL ); |
| 252 | tp->tv_sec= (long)((_now.ns100 - _W32_FT_OFFSET) / 10000000ULL); |
| 253 | } |
| 254 | /* Always return 0 as per Open Group Base Specifications Issue 6. |
| 255 | Do not set errno on error. */ |
| 256 | return 0; |
| 257 | } |
Paolo Bonzini | cbcfa04 | 2011-09-12 16:20:11 +0200 | [diff] [blame] | 258 | |
| 259 | int qemu_get_thread_id(void) |
| 260 | { |
| 261 | return GetCurrentThreadId(); |
| 262 | } |
Laszlo Ersek | e2ea351 | 2013-05-18 06:31:48 +0200 | [diff] [blame] | 263 | |
| 264 | char * |
| 265 | qemu_get_local_state_pathname(const char *relative_pathname) |
| 266 | { |
| 267 | HRESULT result; |
| 268 | char base_path[MAX_PATH+1] = ""; |
| 269 | |
| 270 | result = SHGetFolderPath(NULL, CSIDL_COMMON_APPDATA, NULL, |
| 271 | /* SHGFP_TYPE_CURRENT */ 0, base_path); |
| 272 | if (result != S_OK) { |
| 273 | /* misconfigured environment */ |
| 274 | g_critical("CSIDL_COMMON_APPDATA unavailable: %ld", (long)result); |
| 275 | abort(); |
| 276 | } |
| 277 | return g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s", base_path, |
| 278 | relative_pathname); |
| 279 | } |
Stefan Hajnoczi | 13401ba | 2013-11-14 11:54:16 +0100 | [diff] [blame] | 280 | |
| 281 | void qemu_set_tty_echo(int fd, bool echo) |
| 282 | { |
| 283 | HANDLE handle = (HANDLE)_get_osfhandle(fd); |
| 284 | DWORD dwMode = 0; |
| 285 | |
| 286 | if (handle == INVALID_HANDLE_VALUE) { |
| 287 | return; |
| 288 | } |
| 289 | |
| 290 | GetConsoleMode(handle, &dwMode); |
| 291 | |
| 292 | if (echo) { |
| 293 | SetConsoleMode(handle, dwMode | ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT); |
| 294 | } else { |
| 295 | SetConsoleMode(handle, |
| 296 | dwMode & ~(ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT)); |
| 297 | } |
| 298 | } |
Fam Zheng | 10f5bff | 2014-02-10 14:48:51 +0800 | [diff] [blame] | 299 | |
| 300 | static char exec_dir[PATH_MAX]; |
| 301 | |
| 302 | void qemu_init_exec_dir(const char *argv0) |
| 303 | { |
| 304 | |
| 305 | char *p; |
| 306 | char buf[MAX_PATH]; |
| 307 | DWORD len; |
| 308 | |
| 309 | len = GetModuleFileName(NULL, buf, sizeof(buf) - 1); |
| 310 | if (len == 0) { |
| 311 | return; |
| 312 | } |
| 313 | |
| 314 | buf[len] = 0; |
| 315 | p = buf + len - 1; |
| 316 | while (p != buf && *p != '\\') { |
| 317 | p--; |
| 318 | } |
| 319 | *p = 0; |
| 320 | if (access(buf, R_OK) == 0) { |
| 321 | pstrcpy(exec_dir, sizeof(exec_dir), buf); |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | char *qemu_get_exec_dir(void) |
| 326 | { |
| 327 | return g_strdup(exec_dir); |
| 328 | } |
Sangho Park | 5a00754 | 2014-05-08 12:47:10 +0400 | [diff] [blame] | 329 | |
Marc-André Lureau | 1706e9d | 2017-01-03 20:19:33 +0100 | [diff] [blame] | 330 | #if !GLIB_CHECK_VERSION(2, 50, 0) |
Sangho Park | 5a00754 | 2014-05-08 12:47:10 +0400 | [diff] [blame] | 331 | /* |
Stefan Weil | e637aa6 | 2014-05-28 17:42:24 +0200 | [diff] [blame] | 332 | * The original implementation of g_poll from glib has a problem on Windows |
| 333 | * when using timeouts < 10 ms. |
Sangho Park | 5a00754 | 2014-05-08 12:47:10 +0400 | [diff] [blame] | 334 | * |
Stefan Weil | e637aa6 | 2014-05-28 17:42:24 +0200 | [diff] [blame] | 335 | * Whenever g_poll is called with timeout < 10 ms, it does a quick poll instead |
| 336 | * of wait. This causes significant performance degradation of QEMU. |
Sangho Park | 5a00754 | 2014-05-08 12:47:10 +0400 | [diff] [blame] | 337 | * |
Stefan Weil | e637aa6 | 2014-05-28 17:42:24 +0200 | [diff] [blame] | 338 | * The following code is a copy of the original code from glib/gpoll.c |
| 339 | * (glib commit 20f4d1820b8d4d0fc4447188e33efffd6d4a88d8 from 2014-02-19). |
| 340 | * Some debug code was removed and the code was reformatted. |
| 341 | * All other code modifications are marked with 'QEMU'. |
Sangho Park | 5a00754 | 2014-05-08 12:47:10 +0400 | [diff] [blame] | 342 | */ |
Stefan Weil | e637aa6 | 2014-05-28 17:42:24 +0200 | [diff] [blame] | 343 | |
| 344 | /* |
| 345 | * gpoll.c: poll(2) abstraction |
| 346 | * Copyright 1998 Owen Taylor |
| 347 | * Copyright 2008 Red Hat, Inc. |
| 348 | * |
| 349 | * This library is free software; you can redistribute it and/or |
| 350 | * modify it under the terms of the GNU Lesser General Public |
| 351 | * License as published by the Free Software Foundation; either |
| 352 | * version 2 of the License, or (at your option) any later version. |
| 353 | * |
| 354 | * This library is distributed in the hope that it will be useful, |
| 355 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 356 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 357 | * Lesser General Public License for more details. |
| 358 | * |
| 359 | * You should have received a copy of the GNU Lesser General Public |
| 360 | * License along with this library; if not, see <http://www.gnu.org/licenses/>. |
| 361 | */ |
| 362 | |
| 363 | static int poll_rest(gboolean poll_msgs, HANDLE *handles, gint nhandles, |
| 364 | GPollFD *fds, guint nfds, gint timeout) |
Sangho Park | 5a00754 | 2014-05-08 12:47:10 +0400 | [diff] [blame] | 365 | { |
Stefan Weil | e637aa6 | 2014-05-28 17:42:24 +0200 | [diff] [blame] | 366 | DWORD ready; |
| 367 | GPollFD *f; |
| 368 | int recursed_result; |
Sangho Park | 5a00754 | 2014-05-08 12:47:10 +0400 | [diff] [blame] | 369 | |
Stefan Weil | e637aa6 | 2014-05-28 17:42:24 +0200 | [diff] [blame] | 370 | if (poll_msgs) { |
| 371 | /* Wait for either messages or handles |
| 372 | * -> Use MsgWaitForMultipleObjectsEx |
Sangho Park | 5a00754 | 2014-05-08 12:47:10 +0400 | [diff] [blame] | 373 | */ |
Stefan Weil | e637aa6 | 2014-05-28 17:42:24 +0200 | [diff] [blame] | 374 | ready = MsgWaitForMultipleObjectsEx(nhandles, handles, timeout, |
| 375 | QS_ALLINPUT, MWMO_ALERTABLE); |
| 376 | |
| 377 | if (ready == WAIT_FAILED) { |
| 378 | gchar *emsg = g_win32_error_message(GetLastError()); |
| 379 | g_warning("MsgWaitForMultipleObjectsEx failed: %s", emsg); |
| 380 | g_free(emsg); |
| 381 | } |
| 382 | } else if (nhandles == 0) { |
| 383 | /* No handles to wait for, just the timeout */ |
| 384 | if (timeout == INFINITE) { |
| 385 | ready = WAIT_FAILED; |
| 386 | } else { |
| 387 | SleepEx(timeout, TRUE); |
| 388 | ready = WAIT_TIMEOUT; |
| 389 | } |
| 390 | } else { |
| 391 | /* Wait for just handles |
| 392 | * -> Use WaitForMultipleObjectsEx |
| 393 | */ |
| 394 | ready = |
| 395 | WaitForMultipleObjectsEx(nhandles, handles, FALSE, timeout, TRUE); |
| 396 | if (ready == WAIT_FAILED) { |
| 397 | gchar *emsg = g_win32_error_message(GetLastError()); |
| 398 | g_warning("WaitForMultipleObjectsEx failed: %s", emsg); |
| 399 | g_free(emsg); |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | if (ready == WAIT_FAILED) { |
| 404 | return -1; |
| 405 | } else if (ready == WAIT_TIMEOUT || ready == WAIT_IO_COMPLETION) { |
| 406 | return 0; |
| 407 | } else if (poll_msgs && ready == WAIT_OBJECT_0 + nhandles) { |
| 408 | for (f = fds; f < &fds[nfds]; ++f) { |
| 409 | if (f->fd == G_WIN32_MSG_HANDLE && f->events & G_IO_IN) { |
| 410 | f->revents |= G_IO_IN; |
Sangho Park | 5a00754 | 2014-05-08 12:47:10 +0400 | [diff] [blame] | 411 | } |
| 412 | } |
| 413 | |
Stefan Weil | e637aa6 | 2014-05-28 17:42:24 +0200 | [diff] [blame] | 414 | /* If we have a timeout, or no handles to poll, be satisfied |
| 415 | * with just noticing we have messages waiting. |
| 416 | */ |
| 417 | if (timeout != 0 || nhandles == 0) { |
| 418 | return 1; |
| 419 | } |
| 420 | |
| 421 | /* If no timeout and handles to poll, recurse to poll them, |
| 422 | * too. |
| 423 | */ |
| 424 | recursed_result = poll_rest(FALSE, handles, nhandles, fds, nfds, 0); |
| 425 | return (recursed_result == -1) ? -1 : 1 + recursed_result; |
| 426 | } else if (/* QEMU: removed the following unneeded statement which causes |
| 427 | * a compiler warning: ready >= WAIT_OBJECT_0 && */ |
| 428 | ready < WAIT_OBJECT_0 + nhandles) { |
| 429 | for (f = fds; f < &fds[nfds]; ++f) { |
| 430 | if ((HANDLE) f->fd == handles[ready - WAIT_OBJECT_0]) { |
| 431 | f->revents = f->events; |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | /* If no timeout and polling several handles, recurse to poll |
| 436 | * the rest of them. |
| 437 | */ |
| 438 | if (timeout == 0 && nhandles > 1) { |
| 439 | /* Remove the handle that fired */ |
| 440 | int i; |
Alistair Francis | 0ec7b53 | 2017-06-29 10:16:35 -0700 | [diff] [blame] | 441 | for (i = ready - WAIT_OBJECT_0 + 1; i < nhandles; i++) { |
| 442 | handles[i-1] = handles[i]; |
Stefan Weil | e637aa6 | 2014-05-28 17:42:24 +0200 | [diff] [blame] | 443 | } |
| 444 | nhandles--; |
| 445 | recursed_result = poll_rest(FALSE, handles, nhandles, fds, nfds, 0); |
| 446 | return (recursed_result == -1) ? -1 : 1 + recursed_result; |
| 447 | } |
| 448 | return 1; |
| 449 | } |
| 450 | |
| 451 | return 0; |
| 452 | } |
| 453 | |
| 454 | gint g_poll(GPollFD *fds, guint nfds, gint timeout) |
| 455 | { |
| 456 | HANDLE handles[MAXIMUM_WAIT_OBJECTS]; |
| 457 | gboolean poll_msgs = FALSE; |
| 458 | GPollFD *f; |
| 459 | gint nhandles = 0; |
| 460 | int retval; |
| 461 | |
| 462 | for (f = fds; f < &fds[nfds]; ++f) { |
| 463 | if (f->fd == G_WIN32_MSG_HANDLE && (f->events & G_IO_IN)) { |
| 464 | poll_msgs = TRUE; |
| 465 | } else if (f->fd > 0) { |
| 466 | /* Don't add the same handle several times into the array, as |
| 467 | * docs say that is not allowed, even if it actually does seem |
| 468 | * to work. |
| 469 | */ |
| 470 | gint i; |
| 471 | |
| 472 | for (i = 0; i < nhandles; i++) { |
| 473 | if (handles[i] == (HANDLE) f->fd) { |
| 474 | break; |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | if (i == nhandles) { |
| 479 | if (nhandles == MAXIMUM_WAIT_OBJECTS) { |
| 480 | g_warning("Too many handles to wait for!\n"); |
| 481 | break; |
| 482 | } else { |
| 483 | handles[nhandles++] = (HANDLE) f->fd; |
| 484 | } |
Sangho Park | 5a00754 | 2014-05-08 12:47:10 +0400 | [diff] [blame] | 485 | } |
| 486 | } |
| 487 | } |
| 488 | |
Stefan Weil | e637aa6 | 2014-05-28 17:42:24 +0200 | [diff] [blame] | 489 | for (f = fds; f < &fds[nfds]; ++f) { |
| 490 | f->revents = 0; |
Sangho Park | 5a00754 | 2014-05-08 12:47:10 +0400 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | if (timeout == -1) { |
| 494 | timeout = INFINITE; |
| 495 | } |
| 496 | |
Stefan Weil | e637aa6 | 2014-05-28 17:42:24 +0200 | [diff] [blame] | 497 | /* Polling for several things? */ |
| 498 | if (nhandles > 1 || (nhandles > 0 && poll_msgs)) { |
| 499 | /* First check if one or several of them are immediately |
| 500 | * available |
Sangho Park | 5a00754 | 2014-05-08 12:47:10 +0400 | [diff] [blame] | 501 | */ |
Stefan Weil | e637aa6 | 2014-05-28 17:42:24 +0200 | [diff] [blame] | 502 | retval = poll_rest(poll_msgs, handles, nhandles, fds, nfds, 0); |
Sangho Park | 5a00754 | 2014-05-08 12:47:10 +0400 | [diff] [blame] | 503 | |
Stefan Weil | e637aa6 | 2014-05-28 17:42:24 +0200 | [diff] [blame] | 504 | /* If not, and we have a significant timeout, poll again with |
| 505 | * timeout then. Note that this will return indication for only |
| 506 | * one event, or only for messages. We ignore timeouts less than |
| 507 | * ten milliseconds as they are mostly pointless on Windows, the |
| 508 | * MsgWaitForMultipleObjectsEx() call will timeout right away |
| 509 | * anyway. |
| 510 | * |
| 511 | * Modification for QEMU: replaced timeout >= 10 by timeout > 0. |
| 512 | */ |
| 513 | if (retval == 0 && (timeout == INFINITE || timeout > 0)) { |
| 514 | retval = poll_rest(poll_msgs, handles, nhandles, |
| 515 | fds, nfds, timeout); |
| 516 | } |
| 517 | } else { |
| 518 | /* Just polling for one thing, so no need to check first if |
| 519 | * available immediately |
| 520 | */ |
| 521 | retval = poll_rest(poll_msgs, handles, nhandles, fds, nfds, timeout); |
Sangho Park | 5a00754 | 2014-05-08 12:47:10 +0400 | [diff] [blame] | 522 | } |
| 523 | |
Stefan Weil | e637aa6 | 2014-05-28 17:42:24 +0200 | [diff] [blame] | 524 | if (retval == -1) { |
| 525 | for (f = fds; f < &fds[nfds]; ++f) { |
| 526 | f->revents = 0; |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | return retval; |
Sangho Park | 5a00754 | 2014-05-08 12:47:10 +0400 | [diff] [blame] | 531 | } |
Marc-André Lureau | 1706e9d | 2017-01-03 20:19:33 +0100 | [diff] [blame] | 532 | #endif |
Paolo Bonzini | 3818331 | 2014-05-14 17:43:21 +0800 | [diff] [blame] | 533 | |
Stefan Weil | a28c2f2 | 2015-11-14 20:25:44 +0100 | [diff] [blame] | 534 | int getpagesize(void) |
Paolo Bonzini | 3818331 | 2014-05-14 17:43:21 +0800 | [diff] [blame] | 535 | { |
| 536 | SYSTEM_INFO system_info; |
| 537 | |
| 538 | GetSystemInfo(&system_info); |
| 539 | return system_info.dwPageSize; |
| 540 | } |
| 541 | |
Jitendra Kolhe | 1e356fc | 2017-02-24 09:01:43 +0530 | [diff] [blame] | 542 | void os_mem_prealloc(int fd, char *area, size_t memory, int smp_cpus, |
| 543 | Error **errp) |
Paolo Bonzini | 3818331 | 2014-05-14 17:43:21 +0800 | [diff] [blame] | 544 | { |
| 545 | int i; |
| 546 | size_t pagesize = getpagesize(); |
| 547 | |
| 548 | memory = (memory + pagesize - 1) & -pagesize; |
| 549 | for (i = 0; i < memory / pagesize; i++) { |
| 550 | memset(area + pagesize * i, 0, 1); |
| 551 | } |
| 552 | } |
Daniel P. Berrange | d57e4e4 | 2015-05-12 17:09:19 +0100 | [diff] [blame] | 553 | |
| 554 | |
Michal Privoznik | 7dc9ae4 | 2016-09-27 17:24:56 +0200 | [diff] [blame] | 555 | char *qemu_get_pid_name(pid_t pid) |
| 556 | { |
| 557 | /* XXX Implement me */ |
| 558 | abort(); |
| 559 | } |
| 560 | |
| 561 | |
Daniel P. Berrange | 57cb38b | 2015-08-28 14:40:01 +0100 | [diff] [blame] | 562 | pid_t qemu_fork(Error **errp) |
| 563 | { |
| 564 | errno = ENOSYS; |
| 565 | error_setg_errno(errp, errno, |
| 566 | "cannot fork child process"); |
| 567 | return -1; |
| 568 | } |
Daniel P. Berrange | a2d96af | 2016-03-07 20:25:19 +0000 | [diff] [blame] | 569 | |
| 570 | |
| 571 | #undef connect |
| 572 | int qemu_connect_wrap(int sockfd, const struct sockaddr *addr, |
| 573 | socklen_t addrlen) |
| 574 | { |
| 575 | int ret; |
| 576 | ret = connect(sockfd, addr, addrlen); |
| 577 | if (ret < 0) { |
| 578 | errno = socket_error(); |
| 579 | } |
| 580 | return ret; |
| 581 | } |
| 582 | |
| 583 | |
| 584 | #undef listen |
| 585 | int qemu_listen_wrap(int sockfd, int backlog) |
| 586 | { |
| 587 | int ret; |
| 588 | ret = listen(sockfd, backlog); |
| 589 | if (ret < 0) { |
| 590 | errno = socket_error(); |
| 591 | } |
| 592 | return ret; |
| 593 | } |
| 594 | |
| 595 | |
| 596 | #undef bind |
| 597 | int qemu_bind_wrap(int sockfd, const struct sockaddr *addr, |
| 598 | socklen_t addrlen) |
| 599 | { |
| 600 | int ret; |
| 601 | ret = bind(sockfd, addr, addrlen); |
| 602 | if (ret < 0) { |
| 603 | errno = socket_error(); |
| 604 | } |
| 605 | return ret; |
| 606 | } |
| 607 | |
| 608 | |
| 609 | #undef socket |
| 610 | int qemu_socket_wrap(int domain, int type, int protocol) |
| 611 | { |
| 612 | int ret; |
| 613 | ret = socket(domain, type, protocol); |
| 614 | if (ret < 0) { |
| 615 | errno = socket_error(); |
| 616 | } |
| 617 | return ret; |
| 618 | } |
| 619 | |
| 620 | |
| 621 | #undef accept |
| 622 | int qemu_accept_wrap(int sockfd, struct sockaddr *addr, |
| 623 | socklen_t *addrlen) |
| 624 | { |
| 625 | int ret; |
| 626 | ret = accept(sockfd, addr, addrlen); |
| 627 | if (ret < 0) { |
| 628 | errno = socket_error(); |
| 629 | } |
| 630 | return ret; |
| 631 | } |
| 632 | |
| 633 | |
| 634 | #undef shutdown |
| 635 | int qemu_shutdown_wrap(int sockfd, int how) |
| 636 | { |
| 637 | int ret; |
| 638 | ret = shutdown(sockfd, how); |
| 639 | if (ret < 0) { |
| 640 | errno = socket_error(); |
| 641 | } |
| 642 | return ret; |
| 643 | } |
| 644 | |
| 645 | |
| 646 | #undef ioctlsocket |
| 647 | int qemu_ioctlsocket_wrap(int fd, int req, void *val) |
| 648 | { |
| 649 | int ret; |
| 650 | ret = ioctlsocket(fd, req, val); |
| 651 | if (ret < 0) { |
| 652 | errno = socket_error(); |
| 653 | } |
| 654 | return ret; |
| 655 | } |
| 656 | |
| 657 | |
| 658 | #undef closesocket |
| 659 | int qemu_closesocket_wrap(int fd) |
| 660 | { |
| 661 | int ret; |
| 662 | ret = closesocket(fd); |
| 663 | if (ret < 0) { |
| 664 | errno = socket_error(); |
| 665 | } |
| 666 | return ret; |
| 667 | } |
| 668 | |
| 669 | |
| 670 | #undef getsockopt |
| 671 | int qemu_getsockopt_wrap(int sockfd, int level, int optname, |
| 672 | void *optval, socklen_t *optlen) |
| 673 | { |
| 674 | int ret; |
| 675 | ret = getsockopt(sockfd, level, optname, optval, optlen); |
| 676 | if (ret < 0) { |
| 677 | errno = socket_error(); |
| 678 | } |
| 679 | return ret; |
| 680 | } |
| 681 | |
| 682 | |
| 683 | #undef setsockopt |
| 684 | int qemu_setsockopt_wrap(int sockfd, int level, int optname, |
| 685 | const void *optval, socklen_t optlen) |
| 686 | { |
| 687 | int ret; |
| 688 | ret = setsockopt(sockfd, level, optname, optval, optlen); |
| 689 | if (ret < 0) { |
| 690 | errno = socket_error(); |
| 691 | } |
| 692 | return ret; |
| 693 | } |
| 694 | |
| 695 | |
| 696 | #undef getpeername |
| 697 | int qemu_getpeername_wrap(int sockfd, struct sockaddr *addr, |
| 698 | socklen_t *addrlen) |
| 699 | { |
| 700 | int ret; |
| 701 | ret = getpeername(sockfd, addr, addrlen); |
| 702 | if (ret < 0) { |
| 703 | errno = socket_error(); |
| 704 | } |
| 705 | return ret; |
| 706 | } |
| 707 | |
| 708 | |
| 709 | #undef getsockname |
| 710 | int qemu_getsockname_wrap(int sockfd, struct sockaddr *addr, |
| 711 | socklen_t *addrlen) |
| 712 | { |
| 713 | int ret; |
| 714 | ret = getsockname(sockfd, addr, addrlen); |
| 715 | if (ret < 0) { |
| 716 | errno = socket_error(); |
| 717 | } |
| 718 | return ret; |
| 719 | } |
| 720 | |
| 721 | |
| 722 | #undef send |
| 723 | ssize_t qemu_send_wrap(int sockfd, const void *buf, size_t len, int flags) |
| 724 | { |
| 725 | int ret; |
| 726 | ret = send(sockfd, buf, len, flags); |
| 727 | if (ret < 0) { |
| 728 | errno = socket_error(); |
| 729 | } |
| 730 | return ret; |
| 731 | } |
| 732 | |
| 733 | |
| 734 | #undef sendto |
| 735 | ssize_t qemu_sendto_wrap(int sockfd, const void *buf, size_t len, int flags, |
| 736 | const struct sockaddr *addr, socklen_t addrlen) |
| 737 | { |
| 738 | int ret; |
| 739 | ret = sendto(sockfd, buf, len, flags, addr, addrlen); |
| 740 | if (ret < 0) { |
| 741 | errno = socket_error(); |
| 742 | } |
| 743 | return ret; |
| 744 | } |
| 745 | |
| 746 | |
| 747 | #undef recv |
| 748 | ssize_t qemu_recv_wrap(int sockfd, void *buf, size_t len, int flags) |
| 749 | { |
| 750 | int ret; |
| 751 | ret = recv(sockfd, buf, len, flags); |
| 752 | if (ret < 0) { |
| 753 | errno = socket_error(); |
| 754 | } |
| 755 | return ret; |
| 756 | } |
| 757 | |
| 758 | |
| 759 | #undef recvfrom |
| 760 | ssize_t qemu_recvfrom_wrap(int sockfd, void *buf, size_t len, int flags, |
| 761 | struct sockaddr *addr, socklen_t *addrlen) |
| 762 | { |
| 763 | int ret; |
| 764 | ret = recvfrom(sockfd, buf, len, flags, addr, addrlen); |
| 765 | if (ret < 0) { |
| 766 | errno = socket_error(); |
| 767 | } |
| 768 | return ret; |
| 769 | } |