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