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
32 #include "config-host.h"
34 #include "qemu-options.h"
36 /***********************************************************/
37 /* Functions missing in mingw */
39 int setenv(const char *name
, const char *value
, int overwrite
)
42 if (overwrite
|| !getenv(name
)) {
43 size_t length
= strlen(name
) + strlen(value
) + 2;
44 char *string
= qemu_malloc(length
);
45 snprintf(string
, length
, "%s=%s", name
, value
);
46 result
= putenv(string
);
51 /***********************************************************/
52 /* Polling handling */
54 typedef struct PollingEntry
{
57 struct PollingEntry
*next
;
60 static PollingEntry
*first_polling_entry
;
62 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
64 PollingEntry
**ppe
, *pe
;
65 pe
= qemu_mallocz(sizeof(PollingEntry
));
68 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
73 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
75 PollingEntry
**ppe
, *pe
;
76 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
78 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
86 /***********************************************************/
87 /* Wait objects support */
88 typedef struct WaitObjects
{
90 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
91 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
92 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
95 static WaitObjects wait_objects
= {0};
97 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
99 WaitObjects
*w
= &wait_objects
;
101 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
103 w
->events
[w
->num
] = handle
;
104 w
->func
[w
->num
] = func
;
105 w
->opaque
[w
->num
] = opaque
;
110 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
113 WaitObjects
*w
= &wait_objects
;
116 for (i
= 0; i
< w
->num
; i
++) {
117 if (w
->events
[i
] == handle
)
120 w
->events
[i
] = w
->events
[i
+ 1];
121 w
->func
[i
] = w
->func
[i
+ 1];
122 w
->opaque
[i
] = w
->opaque
[i
+ 1];
129 void os_host_main_loop_wait(int *timeout
)
134 /* XXX: need to suppress polling by better using win32 events */
136 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
137 ret
|= pe
->func(pe
->opaque
);
141 WaitObjects
*w
= &wait_objects
;
143 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, *timeout
);
144 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
145 if (w
->func
[ret
- WAIT_OBJECT_0
])
146 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
148 /* Check for additional signaled events */
149 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
151 /* Check if event is signaled */
152 ret2
= WaitForSingleObject(w
->events
[i
], 0);
153 if(ret2
== WAIT_OBJECT_0
) {
155 w
->func
[i
](w
->opaque
[i
]);
156 } else if (ret2
== WAIT_TIMEOUT
) {
158 err
= GetLastError();
159 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
162 } else if (ret
== WAIT_TIMEOUT
) {
164 err
= GetLastError();
165 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
172 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
174 exit(STATUS_CONTROL_C_EXIT
);
178 void os_setup_early_signal_handling(void)
180 /* Note: cpu_interrupt() is currently not SMP safe, so we force
181 QEMU to run on a single CPU */
186 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
188 h
= GetCurrentProcess();
189 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
190 for(i
= 0; i
< 32; i
++) {
196 SetProcessAffinityMask(h
, mask
);
201 /* Look for support files in the same directory as the executable. */
202 char *os_find_datadir(const char *argv0
)
208 len
= GetModuleFileName(NULL
, buf
, sizeof(buf
) - 1);
215 while (p
!= buf
&& *p
!= '\\')
218 if (access(buf
, R_OK
) == 0) {
219 return qemu_strdup(buf
);
225 * Parse OS specific command line options.
226 * return 0 if option handled, -1 otherwise
228 void os_parse_cmd_args(int index
, const char *optarg
)
233 void os_pidfile_error(void)
235 fprintf(stderr
, "Could not acquire pid file: %s\n", strerror(errno
));