hw/ppc/spapr: simplify usb controller creation logic
[qemu/cris-port.git] / util / oslib-win32.c
blob87cfbe08343a1406619543c8ebfe3337fbb67a82
1 /*
2 * os-win32.c
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 * Copyright (c) 2010 Red Hat, Inc.
7 * QEMU library functions for win32 which are shared between QEMU and
8 * the QEMU tools.
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:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
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.
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.
32 #include <windows.h>
33 #include <glib.h>
34 #include <stdlib.h>
35 #include "config-host.h"
36 #include "sysemu/sysemu.h"
37 #include "qemu/main-loop.h"
38 #include "trace.h"
39 #include "qemu/sockets.h"
41 /* this must come after including "trace.h" */
42 #include <shlobj.h>
44 void *qemu_oom_check(void *ptr)
46 if (ptr == NULL) {
47 fprintf(stderr, "Failed to allocate memory: %lu\n", GetLastError());
48 abort();
50 return ptr;
53 void *qemu_try_memalign(size_t alignment, size_t size)
55 void *ptr;
57 if (!size) {
58 abort();
60 ptr = VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE);
61 trace_qemu_memalign(alignment, size, ptr);
62 return ptr;
65 void *qemu_memalign(size_t alignment, size_t size)
67 return qemu_oom_check(qemu_try_memalign(alignment, size));
70 void *qemu_anon_ram_alloc(size_t size, uint64_t *align)
72 void *ptr;
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. */
77 ptr = VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE);
78 trace_qemu_anon_ram_alloc(size, ptr);
79 return ptr;
82 void qemu_vfree(void *ptr)
84 trace_qemu_vfree(ptr);
85 if (ptr) {
86 VirtualFree(ptr, 0, MEM_RELEASE);
90 void qemu_anon_ram_free(void *ptr, size_t size)
92 trace_qemu_anon_ram_free(ptr, size);
93 if (ptr) {
94 VirtualFree(ptr, 0, MEM_RELEASE);
98 /* FIXME: add proper locking */
99 struct tm *gmtime_r(const time_t *timep, struct tm *result)
101 struct tm *p = gmtime(timep);
102 memset(result, 0, sizeof(*result));
103 if (p) {
104 *result = *p;
105 p = result;
107 return p;
110 /* FIXME: add proper locking */
111 struct tm *localtime_r(const time_t *timep, struct tm *result)
113 struct tm *p = localtime(timep);
114 memset(result, 0, sizeof(*result));
115 if (p) {
116 *result = *p;
117 p = result;
119 return p;
122 void qemu_set_block(int fd)
124 unsigned long opt = 0;
125 WSAEventSelect(fd, NULL, 0);
126 ioctlsocket(fd, FIONBIO, &opt);
129 void qemu_set_nonblock(int fd)
131 unsigned long opt = 1;
132 ioctlsocket(fd, FIONBIO, &opt);
133 qemu_fd_register(fd);
136 int socket_set_fast_reuse(int fd)
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;
146 int inet_aton(const char *cp, struct in_addr *ia)
148 uint32_t addr = inet_addr(cp);
149 if (addr == 0xffffffff) {
150 return 0;
152 ia->s_addr = addr;
153 return 1;
156 void qemu_set_cloexec(int fd)
160 /* Offset between 1/1/1601 and 1/1/1970 in 100 nanosec units */
161 #define _W32_FT_OFFSET (116444736000000000ULL)
163 int qemu_gettimeofday(qemu_timeval *tp)
165 union {
166 unsigned long long ns100; /*time since 1 Jan 1601 in 100ns units */
167 FILETIME ft;
168 } _now;
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);
175 /* Always return 0 as per Open Group Base Specifications Issue 6.
176 Do not set errno on error. */
177 return 0;
180 int qemu_get_thread_id(void)
182 return GetCurrentThreadId();
185 char *
186 qemu_get_local_state_pathname(const char *relative_pathname)
188 HRESULT result;
189 char base_path[MAX_PATH+1] = "";
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();
198 return g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s", base_path,
199 relative_pathname);
202 void qemu_set_tty_echo(int fd, bool echo)
204 HANDLE handle = (HANDLE)_get_osfhandle(fd);
205 DWORD dwMode = 0;
207 if (handle == INVALID_HANDLE_VALUE) {
208 return;
211 GetConsoleMode(handle, &dwMode);
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));
221 static char exec_dir[PATH_MAX];
223 void qemu_init_exec_dir(const char *argv0)
226 char *p;
227 char buf[MAX_PATH];
228 DWORD len;
230 len = GetModuleFileName(NULL, buf, sizeof(buf) - 1);
231 if (len == 0) {
232 return;
235 buf[len] = 0;
236 p = buf + len - 1;
237 while (p != buf && *p != '\\') {
238 p--;
240 *p = 0;
241 if (access(buf, R_OK) == 0) {
242 pstrcpy(exec_dir, sizeof(exec_dir), buf);
246 char *qemu_get_exec_dir(void)
248 return g_strdup(exec_dir);
252 * The original implementation of g_poll from glib has a problem on Windows
253 * when using timeouts < 10 ms.
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.
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'.
265 * gpoll.c: poll(2) abstraction
266 * Copyright 1998 Owen Taylor
267 * Copyright 2008 Red Hat, Inc.
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.
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.
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/>.
283 static int poll_rest(gboolean poll_msgs, HANDLE *handles, gint nhandles,
284 GPollFD *fds, guint nfds, gint timeout)
286 DWORD ready;
287 GPollFD *f;
288 int recursed_result;
290 if (poll_msgs) {
291 /* Wait for either messages or handles
292 * -> Use MsgWaitForMultipleObjectsEx
294 ready = MsgWaitForMultipleObjectsEx(nhandles, handles, timeout,
295 QS_ALLINPUT, MWMO_ALERTABLE);
297 if (ready == WAIT_FAILED) {
298 gchar *emsg = g_win32_error_message(GetLastError());
299 g_warning("MsgWaitForMultipleObjectsEx failed: %s", emsg);
300 g_free(emsg);
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;
310 } else {
311 /* Wait for just handles
312 * -> Use WaitForMultipleObjectsEx
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);
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;
334 /* If we have a timeout, or no handles to poll, be satisfied
335 * with just noticing we have messages waiting.
337 if (timeout != 0 || nhandles == 0) {
338 return 1;
341 /* If no timeout and handles to poll, recurse to poll them,
342 * too.
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;
355 /* If no timeout and polling several handles, recurse to poll
356 * the rest of them.
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];
366 nhandles--;
367 recursed_result = poll_rest(FALSE, handles, nhandles, fds, nfds, 0);
368 return (recursed_result == -1) ? -1 : 1 + recursed_result;
370 return 1;
373 return 0;
376 gint g_poll(GPollFD *fds, guint nfds, gint timeout)
378 HANDLE handles[MAXIMUM_WAIT_OBJECTS];
379 gboolean poll_msgs = FALSE;
380 GPollFD *f;
381 gint nhandles = 0;
382 int retval;
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.
392 gint i;
394 for (i = 0; i < nhandles; i++) {
395 if (handles[i] == (HANDLE) f->fd) {
396 break;
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;
411 for (f = fds; f < &fds[nfds]; ++f) {
412 f->revents = 0;
415 if (timeout == -1) {
416 timeout = INFINITE;
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
424 retval = poll_rest(poll_msgs, handles, nhandles, fds, nfds, 0);
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.
433 * Modification for QEMU: replaced timeout >= 10 by timeout > 0.
435 if (retval == 0 && (timeout == INFINITE || timeout > 0)) {
436 retval = poll_rest(poll_msgs, handles, nhandles,
437 fds, nfds, timeout);
439 } else {
440 /* Just polling for one thing, so no need to check first if
441 * available immediately
443 retval = poll_rest(poll_msgs, handles, nhandles, fds, nfds, timeout);
446 if (retval == -1) {
447 for (f = fds; f < &fds[nfds]; ++f) {
448 f->revents = 0;
452 return retval;
455 size_t getpagesize(void)
457 SYSTEM_INFO system_info;
459 GetSystemInfo(&system_info);
460 return system_info.dwPageSize;
463 void os_mem_prealloc(int fd, char *area, size_t memory)
465 int i;
466 size_t pagesize = getpagesize();
468 memory = (memory + pagesize - 1) & -pagesize;
469 for (i = 0; i < memory / pagesize; i++) {
470 memset(area + pagesize * i, 0, 1);