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