]> git.proxmox.com Git - mirror_qemu.git/blame - util/oslib-win32.c
Merge remote-tracking branch 'remotes/kraxel/tags/pull-ui-20160309-1' into staging
[mirror_qemu.git] / util / oslib-win32.c
CommitLineData
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 31 */
aafd7584 32#include "qemu/osdep.h"
c1b0b93b 33#include <windows.h>
e2ea3515 34#include <glib.h>
9c17d615 35#include "sysemu/sysemu.h"
1de7afc9 36#include "qemu/main-loop.h"
c1b0b93b 37#include "trace.h"
1de7afc9 38#include "qemu/sockets.h"
c1b0b93b 39
e2ea3515
LE
40/* this must come after including "trace.h" */
41#include <shlobj.h>
42
b152aa84 43void *qemu_oom_check(void *ptr)
c1b0b93b
JS
44{
45 if (ptr == NULL) {
46 fprintf(stderr, "Failed to allocate memory: %lu\n", GetLastError());
47 abort();
48 }
49 return ptr;
50}
51
7d2a35cc 52void *qemu_try_memalign(size_t alignment, size_t size)
c1b0b93b
JS
53{
54 void *ptr;
55
56 if (!size) {
57 abort();
58 }
7d2a35cc 59 ptr = VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE);
c1b0b93b
JS
60 trace_qemu_memalign(alignment, size, ptr);
61 return ptr;
62}
63
7d2a35cc
KW
64void *qemu_memalign(size_t alignment, size_t size)
65{
66 return qemu_oom_check(qemu_try_memalign(alignment, size));
67}
68
a2b257d6 69void *qemu_anon_ram_alloc(size_t size, uint64_t *align)
c1b0b93b
JS
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. */
39228250 76 ptr = VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE);
6eebf958 77 trace_qemu_anon_ram_alloc(size, ptr);
c1b0b93b
JS
78 return ptr;
79}
80
81void qemu_vfree(void *ptr)
82{
83 trace_qemu_vfree(ptr);
94c8ff3a
MA
84 if (ptr) {
85 VirtualFree(ptr, 0, MEM_RELEASE);
86 }
c1b0b93b 87}
9549e764 88
e7a09b92
PB
89void 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
4d9310f4 97#ifndef CONFIG_LOCALTIME_R
d3e8f957
SW
98/* FIXME: add proper locking */
99struct 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 */
111struct 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}
4d9310f4 121#endif /* CONFIG_LOCALTIME_R */
d3e8f957 122
f9e8cacc 123void qemu_set_block(int fd)
154b9a0c
PB
124{
125 unsigned long opt = 0;
d3385eb4 126 WSAEventSelect(fd, NULL, 0);
154b9a0c
PB
127 ioctlsocket(fd, FIONBIO, &opt);
128}
129
f9e8cacc 130void qemu_set_nonblock(int fd)
9549e764
JS
131{
132 unsigned long opt = 1;
133 ioctlsocket(fd, FIONBIO, &opt);
d3385eb4 134 qemu_fd_register(fd);
9549e764
JS
135}
136
606600a1
SO
137int 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
9549e764
JS
147int inet_aton(const char *cp, struct in_addr *ia)
148{
149 uint32_t addr = inet_addr(cp);
150 if (addr == 0xffffffff) {
e637aa66 151 return 0;
9549e764
JS
152 }
153 ia->s_addr = addr;
154 return 1;
155}
156
157void qemu_set_cloexec(int fd)
158{
159}
dc786bc9 160
dc786bc9
JS
161/* Offset between 1/1/1601 and 1/1/1970 in 100 nanosec units */
162#define _W32_FT_OFFSET (116444736000000000ULL)
163
164int 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}
cbcfa041
PB
180
181int qemu_get_thread_id(void)
182{
183 return GetCurrentThreadId();
184}
e2ea3515
LE
185
186char *
187qemu_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}
13401ba0
SH
202
203void 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}
10f5bff6
FZ
221
222static char exec_dir[PATH_MAX];
223
224void 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
247char *qemu_get_exec_dir(void)
248{
249 return g_strdup(exec_dir);
250}
5a007547
SP
251
252/*
e637aa66
SW
253 * The original implementation of g_poll from glib has a problem on Windows
254 * when using timeouts < 10 ms.
5a007547 255 *
e637aa66
SW
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.
5a007547 258 *
e637aa66
SW
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.
5a007547 269 *
e637aa66
SW
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/>.
5a007547 282 */
e637aa66
SW
283
284static int poll_rest(gboolean poll_msgs, HANDLE *handles, gint nhandles,
285 GPollFD *fds, guint nfds, gint timeout)
5a007547 286{
e637aa66
SW
287 DWORD ready;
288 GPollFD *f;
289 int recursed_result;
5a007547 290
e637aa66
SW
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);
5a007547 297
e637aa66
SW
298 if (ready == WAIT_FAILED) {
299 gchar *emsg = g_win32_error_message(GetLastError());
300 g_warning("MsgWaitForMultipleObjectsEx failed: %s", emsg);
301 g_free(emsg);
5a007547 302 }
e637aa66
SW
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
5a007547 314 */
e637aa66
SW
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);
5a007547 321 }
e637aa66 322 }
5a007547 323
e637aa66
SW
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;
5a007547
SP
332 }
333 }
5a007547 334
e637aa66
SW
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 }
5a007547 341
e637aa66
SW
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 }
5a007547 355
e637aa66
SW
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;
5a007547 370 }
e637aa66 371 return 1;
5a007547
SP
372 }
373
e637aa66
SW
374 return 0;
375}
5a007547 376
e637aa66
SW
377gint 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 }
5a007547
SP
399 }
400
e637aa66
SW
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 }
5a007547
SP
408 }
409 }
e637aa66 410 }
5a007547 411
e637aa66
SW
412 for (f = fds; f < &fds[nfds]; ++f) {
413 f->revents = 0;
414 }
5a007547 415
e637aa66
SW
416 if (timeout == -1) {
417 timeout = INFINITE;
418 }
5a007547 419
e637aa66
SW
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.
5a007547 435 */
e637aa66
SW
436 if (retval == 0 && (timeout == INFINITE || timeout > 0)) {
437 retval = poll_rest(poll_msgs, handles, nhandles,
438 fds, nfds, timeout);
5a007547 439 }
e637aa66
SW
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 }
5a007547 446
e637aa66
SW
447 if (retval == -1) {
448 for (f = fds; f < &fds[nfds]; ++f) {
449 f->revents = 0;
450 }
5a007547
SP
451 }
452
e637aa66 453 return retval;
5a007547 454}
38183310 455
a28c2f2d 456int getpagesize(void)
38183310
PB
457{
458 SYSTEM_INFO system_info;
459
460 GetSystemInfo(&system_info);
461 return system_info.dwPageSize;
462}
463
464void 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}
d57e4e48
DB
474
475
476/* XXX: put correct support for win32 */
477int 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}
57cb38b3
DB
498
499
500pid_t qemu_fork(Error **errp)
501{
502 errno = ENOSYS;
503 error_setg_errno(errp, errno,
504 "cannot fork child process");
505 return -1;
506}