Statistics
| Branch: | Revision:

root / os-win32.c @ a88790a1

History | View | Annotate | Download (6 kB)

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