1baacf047SPaolo Bonzini /* 2baacf047SPaolo Bonzini * os-win32.c 3baacf047SPaolo Bonzini * 4baacf047SPaolo Bonzini * Copyright (c) 2003-2008 Fabrice Bellard 5baacf047SPaolo Bonzini * Copyright (c) 2010 Red Hat, Inc. 6baacf047SPaolo Bonzini * 7baacf047SPaolo Bonzini * QEMU library functions for win32 which are shared between QEMU and 8baacf047SPaolo Bonzini * the QEMU tools. 9baacf047SPaolo Bonzini * 10baacf047SPaolo Bonzini * Permission is hereby granted, free of charge, to any person obtaining a copy 11baacf047SPaolo Bonzini * of this software and associated documentation files (the "Software"), to deal 12baacf047SPaolo Bonzini * in the Software without restriction, including without limitation the rights 13baacf047SPaolo Bonzini * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 14baacf047SPaolo Bonzini * copies of the Software, and to permit persons to whom the Software is 15baacf047SPaolo Bonzini * furnished to do so, subject to the following conditions: 16baacf047SPaolo Bonzini * 17baacf047SPaolo Bonzini * The above copyright notice and this permission notice shall be included in 18baacf047SPaolo Bonzini * all copies or substantial portions of the Software. 19baacf047SPaolo Bonzini * 20baacf047SPaolo Bonzini * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21baacf047SPaolo Bonzini * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22baacf047SPaolo Bonzini * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 23baacf047SPaolo Bonzini * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 24baacf047SPaolo Bonzini * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 25baacf047SPaolo Bonzini * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 26baacf047SPaolo Bonzini * THE SOFTWARE. 27baacf047SPaolo Bonzini */ 28baacf047SPaolo Bonzini #include <windows.h> 29baacf047SPaolo Bonzini #include "config-host.h" 30baacf047SPaolo Bonzini #include "sysemu/sysemu.h" 31baacf047SPaolo Bonzini #include "qemu/main-loop.h" 32baacf047SPaolo Bonzini #include "trace.h" 33baacf047SPaolo Bonzini #include "qemu/sockets.h" 34baacf047SPaolo Bonzini 35baacf047SPaolo Bonzini void *qemu_oom_check(void *ptr) 36baacf047SPaolo Bonzini { 37baacf047SPaolo Bonzini if (ptr == NULL) { 38baacf047SPaolo Bonzini fprintf(stderr, "Failed to allocate memory: %lu\n", GetLastError()); 39baacf047SPaolo Bonzini abort(); 40baacf047SPaolo Bonzini } 41baacf047SPaolo Bonzini return ptr; 42baacf047SPaolo Bonzini } 43baacf047SPaolo Bonzini 44baacf047SPaolo Bonzini void *qemu_memalign(size_t alignment, size_t size) 45baacf047SPaolo Bonzini { 46baacf047SPaolo Bonzini void *ptr; 47baacf047SPaolo Bonzini 48baacf047SPaolo Bonzini if (!size) { 49baacf047SPaolo Bonzini abort(); 50baacf047SPaolo Bonzini } 51baacf047SPaolo Bonzini ptr = qemu_oom_check(VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE)); 52baacf047SPaolo Bonzini trace_qemu_memalign(alignment, size, ptr); 53baacf047SPaolo Bonzini return ptr; 54baacf047SPaolo Bonzini } 55baacf047SPaolo Bonzini 56baacf047SPaolo Bonzini void *qemu_vmalloc(size_t size) 57baacf047SPaolo Bonzini { 58baacf047SPaolo Bonzini void *ptr; 59baacf047SPaolo Bonzini 60baacf047SPaolo Bonzini /* FIXME: this is not exactly optimal solution since VirtualAlloc 61baacf047SPaolo Bonzini has 64Kb granularity, but at least it guarantees us that the 62baacf047SPaolo Bonzini memory is page aligned. */ 63baacf047SPaolo Bonzini if (!size) { 64baacf047SPaolo Bonzini abort(); 65baacf047SPaolo Bonzini } 66baacf047SPaolo Bonzini ptr = qemu_oom_check(VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE)); 67baacf047SPaolo Bonzini trace_qemu_vmalloc(size, ptr); 68baacf047SPaolo Bonzini return ptr; 69baacf047SPaolo Bonzini } 70baacf047SPaolo Bonzini 71baacf047SPaolo Bonzini void qemu_vfree(void *ptr) 72baacf047SPaolo Bonzini { 73baacf047SPaolo Bonzini trace_qemu_vfree(ptr); 7494c8ff3aSMarkus Armbruster if (ptr) { 75baacf047SPaolo Bonzini VirtualFree(ptr, 0, MEM_RELEASE); 76baacf047SPaolo Bonzini } 7794c8ff3aSMarkus Armbruster } 78baacf047SPaolo Bonzini 79baacf047SPaolo Bonzini /* FIXME: add proper locking */ 80baacf047SPaolo Bonzini struct tm *gmtime_r(const time_t *timep, struct tm *result) 81baacf047SPaolo Bonzini { 82baacf047SPaolo Bonzini struct tm *p = gmtime(timep); 83baacf047SPaolo Bonzini memset(result, 0, sizeof(*result)); 84baacf047SPaolo Bonzini if (p) { 85baacf047SPaolo Bonzini *result = *p; 86baacf047SPaolo Bonzini p = result; 87baacf047SPaolo Bonzini } 88baacf047SPaolo Bonzini return p; 89baacf047SPaolo Bonzini } 90baacf047SPaolo Bonzini 91baacf047SPaolo Bonzini /* FIXME: add proper locking */ 92baacf047SPaolo Bonzini struct tm *localtime_r(const time_t *timep, struct tm *result) 93baacf047SPaolo Bonzini { 94baacf047SPaolo Bonzini struct tm *p = localtime(timep); 95baacf047SPaolo Bonzini memset(result, 0, sizeof(*result)); 96baacf047SPaolo Bonzini if (p) { 97baacf047SPaolo Bonzini *result = *p; 98baacf047SPaolo Bonzini p = result; 99baacf047SPaolo Bonzini } 100baacf047SPaolo Bonzini return p; 101baacf047SPaolo Bonzini } 102baacf047SPaolo Bonzini 103*f9e8caccSStefan Hajnoczi void qemu_set_block(int fd) 104baacf047SPaolo Bonzini { 105baacf047SPaolo Bonzini unsigned long opt = 0; 106baacf047SPaolo Bonzini WSAEventSelect(fd, NULL, 0); 107baacf047SPaolo Bonzini ioctlsocket(fd, FIONBIO, &opt); 108baacf047SPaolo Bonzini } 109baacf047SPaolo Bonzini 110*f9e8caccSStefan Hajnoczi void qemu_set_nonblock(int fd) 111baacf047SPaolo Bonzini { 112baacf047SPaolo Bonzini unsigned long opt = 1; 113baacf047SPaolo Bonzini ioctlsocket(fd, FIONBIO, &opt); 114baacf047SPaolo Bonzini qemu_fd_register(fd); 115baacf047SPaolo Bonzini } 116baacf047SPaolo Bonzini 117baacf047SPaolo Bonzini int inet_aton(const char *cp, struct in_addr *ia) 118baacf047SPaolo Bonzini { 119baacf047SPaolo Bonzini uint32_t addr = inet_addr(cp); 120baacf047SPaolo Bonzini if (addr == 0xffffffff) { 121baacf047SPaolo Bonzini return 0; 122baacf047SPaolo Bonzini } 123baacf047SPaolo Bonzini ia->s_addr = addr; 124baacf047SPaolo Bonzini return 1; 125baacf047SPaolo Bonzini } 126baacf047SPaolo Bonzini 127baacf047SPaolo Bonzini void qemu_set_cloexec(int fd) 128baacf047SPaolo Bonzini { 129baacf047SPaolo Bonzini } 130baacf047SPaolo Bonzini 131baacf047SPaolo Bonzini /* Offset between 1/1/1601 and 1/1/1970 in 100 nanosec units */ 132baacf047SPaolo Bonzini #define _W32_FT_OFFSET (116444736000000000ULL) 133baacf047SPaolo Bonzini 134baacf047SPaolo Bonzini int qemu_gettimeofday(qemu_timeval *tp) 135baacf047SPaolo Bonzini { 136baacf047SPaolo Bonzini union { 137baacf047SPaolo Bonzini unsigned long long ns100; /*time since 1 Jan 1601 in 100ns units */ 138baacf047SPaolo Bonzini FILETIME ft; 139baacf047SPaolo Bonzini } _now; 140baacf047SPaolo Bonzini 141baacf047SPaolo Bonzini if(tp) { 142baacf047SPaolo Bonzini GetSystemTimeAsFileTime (&_now.ft); 143baacf047SPaolo Bonzini tp->tv_usec=(long)((_now.ns100 / 10ULL) % 1000000ULL ); 144baacf047SPaolo Bonzini tp->tv_sec= (long)((_now.ns100 - _W32_FT_OFFSET) / 10000000ULL); 145baacf047SPaolo Bonzini } 146baacf047SPaolo Bonzini /* Always return 0 as per Open Group Base Specifications Issue 6. 147baacf047SPaolo Bonzini Do not set errno on error. */ 148baacf047SPaolo Bonzini return 0; 149baacf047SPaolo Bonzini } 150baacf047SPaolo Bonzini 151baacf047SPaolo Bonzini int qemu_get_thread_id(void) 152baacf047SPaolo Bonzini { 153baacf047SPaolo Bonzini return GetCurrentThreadId(); 154baacf047SPaolo Bonzini } 155