qemu_ram_free: Implement it
[qemu/kraxel.git] / os-win32.c
blobd98fd77c12b04107383e10a33d73ef10897402bc
1 /*
2 * os-win32.c
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 * Copyright (c) 2010 Red Hat, Inc.
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
25 #include <windows.h>
26 #include <unistd.h>
27 #include <fcntl.h>
28 #include <signal.h>
29 #include <time.h>
30 #include <errno.h>
31 #include <sys/time.h>
32 #include "config-host.h"
33 #include "sysemu.h"
34 #include "qemu-options.h"
36 /***********************************************************/
37 /* Polling handling */
39 typedef struct PollingEntry {
40 PollingFunc *func;
41 void *opaque;
42 struct PollingEntry *next;
43 } PollingEntry;
45 static PollingEntry *first_polling_entry;
47 int qemu_add_polling_cb(PollingFunc *func, void *opaque)
49 PollingEntry **ppe, *pe;
50 pe = qemu_mallocz(sizeof(PollingEntry));
51 pe->func = func;
52 pe->opaque = opaque;
53 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next);
54 *ppe = pe;
55 return 0;
58 void qemu_del_polling_cb(PollingFunc *func, void *opaque)
60 PollingEntry **ppe, *pe;
61 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) {
62 pe = *ppe;
63 if (pe->func == func && pe->opaque == opaque) {
64 *ppe = pe->next;
65 qemu_free(pe);
66 break;
71 /***********************************************************/
72 /* Wait objects support */
73 typedef struct WaitObjects {
74 int num;
75 HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
76 WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1];
77 void *opaque[MAXIMUM_WAIT_OBJECTS + 1];
78 } WaitObjects;
80 static WaitObjects wait_objects = {0};
82 int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
84 WaitObjects *w = &wait_objects;
86 if (w->num >= MAXIMUM_WAIT_OBJECTS)
87 return -1;
88 w->events[w->num] = handle;
89 w->func[w->num] = func;
90 w->opaque[w->num] = opaque;
91 w->num++;
92 return 0;
95 void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
97 int i, found;
98 WaitObjects *w = &wait_objects;
100 found = 0;
101 for (i = 0; i < w->num; i++) {
102 if (w->events[i] == handle)
103 found = 1;
104 if (found) {
105 w->events[i] = w->events[i + 1];
106 w->func[i] = w->func[i + 1];
107 w->opaque[i] = w->opaque[i + 1];
110 if (found)
111 w->num--;
114 void os_host_main_loop_wait(int *timeout)
116 int ret, ret2, i;
117 PollingEntry *pe;
119 /* XXX: need to suppress polling by better using win32 events */
120 ret = 0;
121 for(pe = first_polling_entry; pe != NULL; pe = pe->next) {
122 ret |= pe->func(pe->opaque);
124 if (ret == 0) {
125 int err;
126 WaitObjects *w = &wait_objects;
128 ret = WaitForMultipleObjects(w->num, w->events, FALSE, *timeout);
129 if (WAIT_OBJECT_0 + 0 <= ret && ret <= WAIT_OBJECT_0 + w->num - 1) {
130 if (w->func[ret - WAIT_OBJECT_0])
131 w->func[ret - WAIT_OBJECT_0](w->opaque[ret - WAIT_OBJECT_0]);
133 /* Check for additional signaled events */
134 for(i = (ret - WAIT_OBJECT_0 + 1); i < w->num; i++) {
136 /* Check if event is signaled */
137 ret2 = WaitForSingleObject(w->events[i], 0);
138 if(ret2 == WAIT_OBJECT_0) {
139 if (w->func[i])
140 w->func[i](w->opaque[i]);
141 } else if (ret2 == WAIT_TIMEOUT) {
142 } else {
143 err = GetLastError();
144 fprintf(stderr, "WaitForSingleObject error %d %d\n", i, err);
147 } else if (ret == WAIT_TIMEOUT) {
148 } else {
149 err = GetLastError();
150 fprintf(stderr, "WaitForMultipleObjects error %d %d\n", ret, err);
154 *timeout = 0;
157 static BOOL WINAPI qemu_ctrl_handler(DWORD type)
159 exit(STATUS_CONTROL_C_EXIT);
160 return TRUE;
163 void os_setup_early_signal_handling(void)
165 /* Note: cpu_interrupt() is currently not SMP safe, so we force
166 QEMU to run on a single CPU */
167 HANDLE h;
168 DWORD mask, smask;
169 int i;
171 SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE);
173 h = GetCurrentProcess();
174 if (GetProcessAffinityMask(h, &mask, &smask)) {
175 for(i = 0; i < 32; i++) {
176 if (mask & (1 << i))
177 break;
179 if (i != 32) {
180 mask = 1 << i;
181 SetProcessAffinityMask(h, mask);
186 /* Look for support files in the same directory as the executable. */
187 char *os_find_datadir(const char *argv0)
189 char *p;
190 char buf[MAX_PATH];
191 DWORD len;
193 len = GetModuleFileName(NULL, buf, sizeof(buf) - 1);
194 if (len == 0) {
195 return NULL;
198 buf[len] = 0;
199 p = buf + len - 1;
200 while (p != buf && *p != '\\')
201 p--;
202 *p = 0;
203 if (access(buf, R_OK) == 0) {
204 return qemu_strdup(buf);
206 return NULL;
210 * Parse OS specific command line options.
211 * return 0 if option handled, -1 otherwise
213 void os_parse_cmd_args(int index, const char *optarg)
215 return;
218 void os_pidfile_error(void)
220 fprintf(stderr, "Could not acquire pid file: %s\n", strerror(errno));