os-win32.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * win32 specific declarations
  3. *
  4. * Copyright (c) 2003-2008 Fabrice Bellard
  5. * Copyright (c) 2010 Jes Sorensen <Jes.Sorensen@redhat.com>
  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. #ifndef QEMU_OS_WIN32_H
  26. #define QEMU_OS_WIN32_H
  27. #include <winsock2.h>
  28. #include <windows.h>
  29. #include <ws2tcpip.h>
  30. #include "qemu/typedefs.h"
  31. #ifdef HAVE_AFUNIX_H
  32. #include <afunix.h>
  33. #else
  34. /*
  35. * Fallback definitions of things we need in afunix.h, if not available from
  36. * the used Windows SDK or MinGW headers.
  37. */
  38. #define UNIX_PATH_MAX 108
  39. typedef struct sockaddr_un {
  40. ADDRESS_FAMILY sun_family;
  41. char sun_path[UNIX_PATH_MAX];
  42. } SOCKADDR_UN, *PSOCKADDR_UN;
  43. #define SIO_AF_UNIX_GETPEERPID _WSAIOR(IOC_VENDOR, 256)
  44. #endif
  45. #ifdef __cplusplus
  46. extern "C" {
  47. #endif
  48. #if defined(__aarch64__)
  49. /*
  50. * On windows-arm64, setjmp is available in only one variant, and longjmp always
  51. * does stack unwinding. This crash with generated code.
  52. * Thus, we use another implementation of setjmp (not windows one), coming from
  53. * mingw, which never performs stack unwinding.
  54. */
  55. #undef setjmp
  56. #undef longjmp
  57. /*
  58. * These functions are not declared in setjmp.h because __aarch64__ defines
  59. * setjmp to _setjmpex instead. However, they are still defined in libmingwex.a,
  60. * which gets linked automatically.
  61. */
  62. int __mingw_setjmp(jmp_buf);
  63. void __attribute__((noreturn)) __mingw_longjmp(jmp_buf, int);
  64. #define setjmp(env) __mingw_setjmp(env)
  65. #define longjmp(env, val) __mingw_longjmp(env, val)
  66. #elif defined(_WIN64)
  67. /*
  68. * On windows-x64, setjmp is implemented by _setjmp which needs a second parameter.
  69. * If this parameter is NULL, longjump does no stack unwinding.
  70. * That is what we need for QEMU. Passing the value of register rsp (default)
  71. * lets longjmp try a stack unwinding which will crash with generated code.
  72. */
  73. # undef setjmp
  74. # define setjmp(env) _setjmp(env, NULL)
  75. #endif /* __aarch64__ */
  76. /* QEMU uses sigsetjmp()/siglongjmp() as the portable way to specify
  77. * "longjmp and don't touch the signal masks". Since we know that the
  78. * savemask parameter will always be zero we can safely define these
  79. * in terms of setjmp/longjmp on Win32.
  80. */
  81. #define sigjmp_buf jmp_buf
  82. #define sigsetjmp(env, savemask) setjmp(env)
  83. #define siglongjmp(env, val) longjmp(env, val)
  84. /* Missing POSIX functions. Don't use MinGW-w64 macros. */
  85. #ifndef _POSIX_THREAD_SAFE_FUNCTIONS
  86. #undef gmtime_r
  87. struct tm *gmtime_r(const time_t *timep, struct tm *result);
  88. #undef localtime_r
  89. struct tm *localtime_r(const time_t *timep, struct tm *result);
  90. #endif /* _POSIX_THREAD_SAFE_FUNCTIONS */
  91. static inline void os_setup_signal_handling(void) {}
  92. static inline void os_daemonize(void) {}
  93. static inline void os_setup_post(void) {}
  94. static inline void os_set_proc_name(const char *dummy) {}
  95. void os_set_line_buffering(void);
  96. void os_setup_early_signal_handling(void);
  97. int getpagesize(void);
  98. #if !defined(EPROTONOSUPPORT)
  99. # define EPROTONOSUPPORT EINVAL
  100. #endif
  101. static inline int os_set_daemonize(bool d)
  102. {
  103. if (d) {
  104. return -ENOTSUP;
  105. }
  106. return 0;
  107. }
  108. static inline bool is_daemonized(void)
  109. {
  110. return false;
  111. }
  112. static inline int os_mlock(bool on_fault G_GNUC_UNUSED)
  113. {
  114. return -ENOSYS;
  115. }
  116. static inline void os_setup_limits(void)
  117. {
  118. return;
  119. }
  120. #define fsync _commit
  121. #if !defined(lseek)
  122. # define lseek _lseeki64
  123. #endif
  124. int qemu_ftruncate64(int, int64_t);
  125. #if !defined(ftruncate)
  126. # define ftruncate qemu_ftruncate64
  127. #endif
  128. static inline char *realpath(const char *path, char *resolved_path)
  129. {
  130. _fullpath(resolved_path, path, _MAX_PATH);
  131. return resolved_path;
  132. }
  133. /*
  134. * Older versions of MinGW do not import _lock_file and _unlock_file properly.
  135. * This was fixed for v6.0.0 with commit b48e3ac8969d.
  136. */
  137. static inline void qemu_flockfile(FILE *f)
  138. {
  139. #ifdef HAVE__LOCK_FILE
  140. _lock_file(f);
  141. #endif
  142. }
  143. static inline void qemu_funlockfile(FILE *f)
  144. {
  145. #ifdef HAVE__LOCK_FILE
  146. _unlock_file(f);
  147. #endif
  148. }
  149. /* Helper for WSAEventSelect, to report errors */
  150. bool qemu_socket_select(int sockfd, WSAEVENT hEventObject,
  151. long lNetworkEvents, Error **errp);
  152. bool qemu_socket_unselect(int sockfd, Error **errp);
  153. /* We wrap all the sockets functions so that we can set errno based on
  154. * WSAGetLastError(), and use file-descriptors instead of SOCKET.
  155. */
  156. /*
  157. * qemu_close_socket_osfhandle:
  158. * @fd: a file descriptor associated with a SOCKET
  159. *
  160. * Close only the C run-time file descriptor, leave the SOCKET opened.
  161. *
  162. * Returns zero on success. On error, -1 is returned, and errno is set to
  163. * indicate the error.
  164. */
  165. int qemu_close_socket_osfhandle(int fd);
  166. #undef close
  167. #define close qemu_close_wrap
  168. int qemu_close_wrap(int fd);
  169. #undef connect
  170. #define connect qemu_connect_wrap
  171. int qemu_connect_wrap(int sockfd, const struct sockaddr *addr,
  172. socklen_t addrlen);
  173. #undef listen
  174. #define listen qemu_listen_wrap
  175. int qemu_listen_wrap(int sockfd, int backlog);
  176. #undef bind
  177. #define bind qemu_bind_wrap
  178. int qemu_bind_wrap(int sockfd, const struct sockaddr *addr,
  179. socklen_t addrlen);
  180. #undef socket
  181. #define socket qemu_socket_wrap
  182. int qemu_socket_wrap(int domain, int type, int protocol);
  183. #undef accept
  184. #define accept qemu_accept_wrap
  185. int qemu_accept_wrap(int sockfd, struct sockaddr *addr,
  186. socklen_t *addrlen);
  187. #undef shutdown
  188. #define shutdown qemu_shutdown_wrap
  189. int qemu_shutdown_wrap(int sockfd, int how);
  190. #undef ioctlsocket
  191. #define ioctlsocket qemu_ioctlsocket_wrap
  192. int qemu_ioctlsocket_wrap(int fd, int req, void *val);
  193. #undef getsockopt
  194. #define getsockopt qemu_getsockopt_wrap
  195. int qemu_getsockopt_wrap(int sockfd, int level, int optname,
  196. void *optval, socklen_t *optlen);
  197. #undef setsockopt
  198. #define setsockopt qemu_setsockopt_wrap
  199. int qemu_setsockopt_wrap(int sockfd, int level, int optname,
  200. const void *optval, socklen_t optlen);
  201. #undef getpeername
  202. #define getpeername qemu_getpeername_wrap
  203. int qemu_getpeername_wrap(int sockfd, struct sockaddr *addr,
  204. socklen_t *addrlen);
  205. #undef getsockname
  206. #define getsockname qemu_getsockname_wrap
  207. int qemu_getsockname_wrap(int sockfd, struct sockaddr *addr,
  208. socklen_t *addrlen);
  209. #undef send
  210. #define send qemu_send_wrap
  211. ssize_t qemu_send_wrap(int sockfd, const void *buf, size_t len, int flags);
  212. #undef sendto
  213. #define sendto qemu_sendto_wrap
  214. ssize_t qemu_sendto_wrap(int sockfd, const void *buf, size_t len, int flags,
  215. const struct sockaddr *addr, socklen_t addrlen);
  216. #undef recv
  217. #define recv qemu_recv_wrap
  218. ssize_t qemu_recv_wrap(int sockfd, void *buf, size_t len, int flags);
  219. #undef recvfrom
  220. #define recvfrom qemu_recvfrom_wrap
  221. ssize_t qemu_recvfrom_wrap(int sockfd, void *buf, size_t len, int flags,
  222. struct sockaddr *addr, socklen_t *addrlen);
  223. EXCEPTION_DISPOSITION
  224. win32_close_exception_handler(struct _EXCEPTION_RECORD*, void*,
  225. struct _CONTEXT*, void*);
  226. void *qemu_win32_map_alloc(size_t size, HANDLE *h, Error **errp);
  227. void qemu_win32_map_free(void *ptr, HANDLE h, Error **errp);
  228. #ifdef __cplusplus
  229. }
  230. #endif
  231. #endif