os-win32.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * os-win32.c
  3. *
  4. * Copyright (c) 2003-2008 Fabrice Bellard
  5. * Copyright (c) 2010 Red Hat, Inc.
  6. *
  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:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  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.
  24. */
  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"
  35. /***********************************************************/
  36. /* Functions missing in mingw */
  37. int setenv(const char *name, const char *value, int overwrite)
  38. {
  39. int result = 0;
  40. if (overwrite || !getenv(name)) {
  41. size_t length = strlen(name) + strlen(value) + 2;
  42. char *string = qemu_malloc(length);
  43. snprintf(string, length, "%s=%s", name, value);
  44. result = putenv(string);
  45. }
  46. return result;
  47. }
  48. /***********************************************************/
  49. /* Polling handling */
  50. typedef struct PollingEntry {
  51. PollingFunc *func;
  52. void *opaque;
  53. struct PollingEntry *next;
  54. } PollingEntry;
  55. static PollingEntry *first_polling_entry;
  56. int qemu_add_polling_cb(PollingFunc *func, void *opaque)
  57. {
  58. PollingEntry **ppe, *pe;
  59. pe = qemu_mallocz(sizeof(PollingEntry));
  60. pe->func = func;
  61. pe->opaque = opaque;
  62. for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next);
  63. *ppe = pe;
  64. return 0;
  65. }
  66. void qemu_del_polling_cb(PollingFunc *func, void *opaque)
  67. {
  68. PollingEntry **ppe, *pe;
  69. for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) {
  70. pe = *ppe;
  71. if (pe->func == func && pe->opaque == opaque) {
  72. *ppe = pe->next;
  73. qemu_free(pe);
  74. break;
  75. }
  76. }
  77. }
  78. /***********************************************************/
  79. /* Wait objects support */
  80. typedef struct WaitObjects {
  81. int num;
  82. HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
  83. WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1];
  84. void *opaque[MAXIMUM_WAIT_OBJECTS + 1];
  85. } WaitObjects;
  86. static WaitObjects wait_objects = {0};
  87. int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
  88. {
  89. WaitObjects *w = &wait_objects;
  90. if (w->num >= MAXIMUM_WAIT_OBJECTS)
  91. return -1;
  92. w->events[w->num] = handle;
  93. w->func[w->num] = func;
  94. w->opaque[w->num] = opaque;
  95. w->num++;
  96. return 0;
  97. }
  98. void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
  99. {
  100. int i, found;
  101. WaitObjects *w = &wait_objects;
  102. found = 0;
  103. for (i = 0; i < w->num; i++) {
  104. if (w->events[i] == handle)
  105. found = 1;
  106. if (found) {
  107. w->events[i] = w->events[i + 1];
  108. w->func[i] = w->func[i + 1];
  109. w->opaque[i] = w->opaque[i + 1];
  110. }
  111. }
  112. if (found)
  113. w->num--;
  114. }
  115. void os_host_main_loop_wait(int *timeout)
  116. {
  117. int ret, ret2, i;
  118. 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);
  123. }
  124. if (ret == 0) {
  125. int err;
  126. WaitObjects *w = &wait_objects;
  127. qemu_mutex_unlock_iothread();
  128. ret = WaitForMultipleObjects(w->num, w->events, FALSE, *timeout);
  129. qemu_mutex_lock_iothread();
  130. if (WAIT_OBJECT_0 + 0 <= ret && ret <= WAIT_OBJECT_0 + w->num - 1) {
  131. if (w->func[ret - WAIT_OBJECT_0])
  132. 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++) {
  135. /* Check if event is signaled */
  136. ret2 = WaitForSingleObject(w->events[i], 0);
  137. if(ret2 == WAIT_OBJECT_0) {
  138. if (w->func[i])
  139. w->func[i](w->opaque[i]);
  140. } else if (ret2 == WAIT_TIMEOUT) {
  141. } else {
  142. err = GetLastError();
  143. fprintf(stderr, "WaitForSingleObject error %d %d\n", i, err);
  144. }
  145. }
  146. } else if (ret == WAIT_TIMEOUT) {
  147. } else {
  148. err = GetLastError();
  149. fprintf(stderr, "WaitForMultipleObjects error %d %d\n", ret, err);
  150. }
  151. }
  152. *timeout = 0;
  153. }
  154. static BOOL WINAPI qemu_ctrl_handler(DWORD type)
  155. {
  156. exit(STATUS_CONTROL_C_EXIT);
  157. return TRUE;
  158. }
  159. void os_setup_early_signal_handling(void)
  160. {
  161. /* Note: cpu_interrupt() is currently not SMP safe, so we force
  162. QEMU to run on a single CPU */
  163. HANDLE h;
  164. DWORD_PTR mask, smask;
  165. int i;
  166. SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE);
  167. h = GetCurrentProcess();
  168. if (GetProcessAffinityMask(h, &mask, &smask)) {
  169. for(i = 0; i < 32; i++) {
  170. if (mask & (1 << i))
  171. break;
  172. }
  173. if (i != 32) {
  174. mask = 1 << i;
  175. SetProcessAffinityMask(h, mask);
  176. }
  177. }
  178. }
  179. /* Look for support files in the same directory as the executable. */
  180. char *os_find_datadir(const char *argv0)
  181. {
  182. char *p;
  183. char buf[MAX_PATH];
  184. DWORD len;
  185. len = GetModuleFileName(NULL, buf, sizeof(buf) - 1);
  186. if (len == 0) {
  187. return NULL;
  188. }
  189. buf[len] = 0;
  190. p = buf + len - 1;
  191. while (p != buf && *p != '\\')
  192. p--;
  193. *p = 0;
  194. if (access(buf, R_OK) == 0) {
  195. return qemu_strdup(buf);
  196. }
  197. return NULL;
  198. }
  199. void os_set_line_buffering(void)
  200. {
  201. setbuf(stdout, NULL);
  202. setbuf(stderr, NULL);
  203. }
  204. /*
  205. * Parse OS specific command line options.
  206. * return 0 if option handled, -1 otherwise
  207. */
  208. void os_parse_cmd_args(int index, const char *optarg)
  209. {
  210. return;
  211. }
  212. void os_pidfile_error(void)
  213. {
  214. fprintf(stderr, "Could not acquire pid file: %s\n", strerror(errno));
  215. }
  216. int qemu_create_pidfile(const char *filename)
  217. {
  218. char buffer[128];
  219. int len;
  220. HANDLE file;
  221. OVERLAPPED overlap;
  222. BOOL ret;
  223. memset(&overlap, 0, sizeof(overlap));
  224. file = CreateFile(filename, GENERIC_WRITE, FILE_SHARE_READ, NULL,
  225. OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  226. if (file == INVALID_HANDLE_VALUE) {
  227. return -1;
  228. }
  229. len = snprintf(buffer, sizeof(buffer), FMT_pid "\n", getpid());
  230. ret = WriteFileEx(file, (LPCVOID)buffer, (DWORD)len,
  231. &overlap, NULL);
  232. if (ret == 0) {
  233. return -1;
  234. }
  235. return 0;
  236. }
  237. int qemu_get_thread_id(void)
  238. {
  239. return GetCurrentThreadId();
  240. }