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