]> git.proxmox.com Git - mirror_qemu.git/blame - util/oslib-win32.c
gtk: avoid redefining _WIN32_WINNT macro
[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
JS
31 */
32#include <windows.h>
e2ea3515
LE
33#include <glib.h>
34#include <stdlib.h>
c1b0b93b 35#include "config-host.h"
9c17d615 36#include "sysemu/sysemu.h"
1de7afc9 37#include "qemu/main-loop.h"
c1b0b93b 38#include "trace.h"
1de7afc9 39#include "qemu/sockets.h"
c1b0b93b 40
e2ea3515
LE
41/* this must come after including "trace.h" */
42#include <shlobj.h>
43
b152aa84 44void *qemu_oom_check(void *ptr)
c1b0b93b
JS
45{
46 if (ptr == NULL) {
47 fprintf(stderr, "Failed to allocate memory: %lu\n", GetLastError());
48 abort();
49 }
50 return ptr;
51}
52
7d2a35cc 53void *qemu_try_memalign(size_t alignment, size_t size)
c1b0b93b
JS
54{
55 void *ptr;
56
57 if (!size) {
58 abort();
59 }
7d2a35cc 60 ptr = VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE);
c1b0b93b
JS
61 trace_qemu_memalign(alignment, size, ptr);
62 return ptr;
63}
64
7d2a35cc
KW
65void *qemu_memalign(size_t alignment, size_t size)
66{
67 return qemu_oom_check(qemu_try_memalign(alignment, size));
68}
69
a2b257d6 70void *qemu_anon_ram_alloc(size_t size, uint64_t *align)
c1b0b93b
JS
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. */
39228250 77 ptr = VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE);
6eebf958 78 trace_qemu_anon_ram_alloc(size, ptr);
c1b0b93b
JS
79 return ptr;
80}
81
82void qemu_vfree(void *ptr)
83{
84 trace_qemu_vfree(ptr);
94c8ff3a
MA
85 if (ptr) {
86 VirtualFree(ptr, 0, MEM_RELEASE);
87 }
c1b0b93b 88}
9549e764 89
e7a09b92
PB
90void 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
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}
121
f9e8cacc 122void qemu_set_block(int fd)
154b9a0c
PB
123{
124 unsigned long opt = 0;
d3385eb4 125 WSAEventSelect(fd, NULL, 0);
154b9a0c
PB
126 ioctlsocket(fd, FIONBIO, &opt);
127}
128
f9e8cacc 129void qemu_set_nonblock(int fd)
9549e764
JS
130{
131 unsigned long opt = 1;
132 ioctlsocket(fd, FIONBIO, &opt);
d3385eb4 133 qemu_fd_register(fd);
9549e764
JS
134}
135
606600a1
SO
136int 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
9549e764
JS
146int inet_aton(const char *cp, struct in_addr *ia)
147{
148 uint32_t addr = inet_addr(cp);
149 if (addr == 0xffffffff) {
e637aa66 150 return 0;
9549e764
JS
151 }
152 ia->s_addr = addr;
153 return 1;
154}
155
156void qemu_set_cloexec(int fd)
157{
158}
dc786bc9 159
dc786bc9
JS
160/* Offset between 1/1/1601 and 1/1/1970 in 100 nanosec units */
161#define _W32_FT_OFFSET (116444736000000000ULL)
162
163int 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}
cbcfa041
PB
179
180int qemu_get_thread_id(void)
181{
182 return GetCurrentThreadId();
183}
e2ea3515
LE
184
185char *
186qemu_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}
13401ba0
SH
201
202void 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}
10f5bff6
FZ
220
221static char exec_dir[PATH_MAX];
222
223void 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
246char *qemu_get_exec_dir(void)
247{
248 return g_strdup(exec_dir);
249}
5a007547
SP
250
251/*
e637aa66
SW
252 * The original implementation of g_poll from glib has a problem on Windows
253 * when using timeouts < 10 ms.
5a007547 254 *
e637aa66
SW
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.
5a007547 257 *
e637aa66
SW
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'.
262 */
263
264/*
265 * gpoll.c: poll(2) abstraction
266 * Copyright 1998 Owen Taylor
267 * Copyright 2008 Red Hat, Inc.
5a007547 268 *
e637aa66
SW
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/>.
5a007547 281 */
e637aa66
SW
282
283static int poll_rest(gboolean poll_msgs, HANDLE *handles, gint nhandles,
284 GPollFD *fds, guint nfds, gint timeout)
5a007547 285{
e637aa66
SW
286 DWORD ready;
287 GPollFD *f;
288 int recursed_result;
5a007547 289
e637aa66
SW
290 if (poll_msgs) {
291 /* Wait for either messages or handles
292 * -> Use MsgWaitForMultipleObjectsEx
293 */
294 ready = MsgWaitForMultipleObjectsEx(nhandles, handles, timeout,
295 QS_ALLINPUT, MWMO_ALERTABLE);
5a007547 296
e637aa66
SW
297 if (ready == WAIT_FAILED) {
298 gchar *emsg = g_win32_error_message(GetLastError());
299 g_warning("MsgWaitForMultipleObjectsEx failed: %s", emsg);
300 g_free(emsg);
5a007547 301 }
e637aa66
SW
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
5a007547 313 */
e637aa66
SW
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);
5a007547 320 }
e637aa66 321 }
5a007547 322
e637aa66
SW
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;
5a007547
SP
331 }
332 }
5a007547 333
e637aa66
SW
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 }
5a007547 340
e637aa66
SW
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 }
5a007547 354
e637aa66
SW
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;
5a007547 369 }
e637aa66 370 return 1;
5a007547
SP
371 }
372
e637aa66
SW
373 return 0;
374}
5a007547 375
e637aa66
SW
376gint 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 }
5a007547
SP
398 }
399
e637aa66
SW
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 }
5a007547
SP
407 }
408 }
e637aa66 409 }
5a007547 410
e637aa66
SW
411 for (f = fds; f < &fds[nfds]; ++f) {
412 f->revents = 0;
413 }
5a007547 414
e637aa66
SW
415 if (timeout == -1) {
416 timeout = INFINITE;
417 }
5a007547 418
e637aa66
SW
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
423 */
424 retval = poll_rest(poll_msgs, handles, nhandles, fds, nfds, 0);
425
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.
5a007547 434 */
e637aa66
SW
435 if (retval == 0 && (timeout == INFINITE || timeout > 0)) {
436 retval = poll_rest(poll_msgs, handles, nhandles,
437 fds, nfds, timeout);
5a007547 438 }
e637aa66
SW
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);
444 }
5a007547 445
e637aa66
SW
446 if (retval == -1) {
447 for (f = fds; f < &fds[nfds]; ++f) {
448 f->revents = 0;
449 }
5a007547
SP
450 }
451
e637aa66 452 return retval;
5a007547 453}
38183310
PB
454
455size_t getpagesize(void)
456{
457 SYSTEM_INFO system_info;
458
459 GetSystemInfo(&system_info);
460 return system_info.dwPageSize;
461}
462
463void 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}
d57e4e48
DB
473
474
475/* XXX: put correct support for win32 */
476int qemu_read_password(char *buf, int buf_size)
477{
478 int c, i;
479
480 printf("Password: ");
481 fflush(stdout);
482 i = 0;
483 for (;;) {
484 c = getchar();
485 if (c < 0) {
486 buf[i] = '\0';
487 return -1;
488 } else if (c == '\n') {
489 break;
490 } else if (i < (buf_size - 1)) {
491 buf[i++] = c;
492 }
493 }
494 buf[i] = '\0';
495 return 0;
496}