xref: /openbmc/qemu/include/sysemu/os-win32.h (revision 197a1372)
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 
33 #ifdef HAVE_AFUNIX_H
34 #include <afunix.h>
35 #else
36 /*
37  * Fallback definitions of things we need in afunix.h, if not available from
38  * the used Windows SDK or MinGW headers.
39  */
40 #define UNIX_PATH_MAX 108
41 
42 typedef struct sockaddr_un {
43     ADDRESS_FAMILY sun_family;
44     char sun_path[UNIX_PATH_MAX];
45 } SOCKADDR_UN, *PSOCKADDR_UN;
46 
47 #define SIO_AF_UNIX_GETPEERPID _WSAIOR(IOC_VENDOR, 256)
48 #endif
49 
50 #ifdef __cplusplus
51 extern "C" {
52 #endif
53 
54 #if defined(__aarch64__)
55 /*
56  * On windows-arm64, setjmp is available in only one variant, and longjmp always
57  * does stack unwinding. This crash with generated code.
58  * Thus, we use another implementation of setjmp (not windows one), coming from
59  * mingw, which never performs stack unwinding.
60  */
61 #undef setjmp
62 #undef longjmp
63 /*
64  * These functions are not declared in setjmp.h because __aarch64__ defines
65  * setjmp to _setjmpex instead. However, they are still defined in libmingwex.a,
66  * which gets linked automatically.
67  */
68 extern int __mingw_setjmp(jmp_buf);
69 extern void __attribute__((noreturn)) __mingw_longjmp(jmp_buf, int);
70 #define setjmp(env) __mingw_setjmp(env)
71 #define longjmp(env, val) __mingw_longjmp(env, val)
72 #elif defined(_WIN64)
73 /*
74  * On windows-x64, setjmp is implemented by _setjmp which needs a second parameter.
75  * If this parameter is NULL, longjump does no stack unwinding.
76  * That is what we need for QEMU. Passing the value of register rsp (default)
77  * lets longjmp try a stack unwinding which will crash with generated code.
78  */
79 # undef setjmp
80 # define setjmp(env) _setjmp(env, NULL)
81 #endif /* __aarch64__ */
82 /* QEMU uses sigsetjmp()/siglongjmp() as the portable way to specify
83  * "longjmp and don't touch the signal masks". Since we know that the
84  * savemask parameter will always be zero we can safely define these
85  * in terms of setjmp/longjmp on Win32.
86  */
87 #define sigjmp_buf jmp_buf
88 #define sigsetjmp(env, savemask) setjmp(env)
89 #define siglongjmp(env, val) longjmp(env, val)
90 
91 /* Missing POSIX functions. Don't use MinGW-w64 macros. */
92 #ifndef _POSIX_THREAD_SAFE_FUNCTIONS
93 #undef gmtime_r
94 struct tm *gmtime_r(const time_t *timep, struct tm *result);
95 #undef localtime_r
96 struct tm *localtime_r(const time_t *timep, struct tm *result);
97 #endif /* _POSIX_THREAD_SAFE_FUNCTIONS */
98 
99 static inline void os_setup_signal_handling(void) {}
100 static inline void os_daemonize(void) {}
101 static inline void os_setup_post(void) {}
102 static inline void os_set_proc_name(const char *dummy) {}
103 static inline int os_parse_cmd_args(int index, const char *optarg) { return -1; }
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(void)
127 {
128     return -ENOSYS;
129 }
130 
131 #define fsync _commit
132 
133 #if !defined(lseek)
134 # define lseek _lseeki64
135 #endif
136 
137 int qemu_ftruncate64(int, int64_t);
138 
139 #if !defined(ftruncate)
140 # define ftruncate qemu_ftruncate64
141 #endif
142 
143 static inline char *realpath(const char *path, char *resolved_path)
144 {
145     _fullpath(resolved_path, path, _MAX_PATH);
146     return resolved_path;
147 }
148 
149 /*
150  * Older versions of MinGW do not import _lock_file and _unlock_file properly.
151  * This was fixed for v6.0.0 with commit b48e3ac8969d.
152  */
153 static inline void qemu_flockfile(FILE *f)
154 {
155 #ifdef HAVE__LOCK_FILE
156     _lock_file(f);
157 #endif
158 }
159 
160 static inline void qemu_funlockfile(FILE *f)
161 {
162 #ifdef HAVE__LOCK_FILE
163     _unlock_file(f);
164 #endif
165 }
166 
167 /* We wrap all the sockets functions so that we can
168  * set errno based on WSAGetLastError()
169  */
170 
171 #undef connect
172 #define connect qemu_connect_wrap
173 int qemu_connect_wrap(int sockfd, const struct sockaddr *addr,
174                       socklen_t addrlen);
175 
176 #undef listen
177 #define listen qemu_listen_wrap
178 int qemu_listen_wrap(int sockfd, int backlog);
179 
180 #undef bind
181 #define bind qemu_bind_wrap
182 int qemu_bind_wrap(int sockfd, const struct sockaddr *addr,
183                    socklen_t addrlen);
184 
185 #undef socket
186 #define socket qemu_socket_wrap
187 int qemu_socket_wrap(int domain, int type, int protocol);
188 
189 #undef accept
190 #define accept qemu_accept_wrap
191 int qemu_accept_wrap(int sockfd, struct sockaddr *addr,
192                      socklen_t *addrlen);
193 
194 #undef shutdown
195 #define shutdown qemu_shutdown_wrap
196 int qemu_shutdown_wrap(int sockfd, int how);
197 
198 #undef ioctlsocket
199 #define ioctlsocket qemu_ioctlsocket_wrap
200 int qemu_ioctlsocket_wrap(int fd, int req, void *val);
201 
202 #undef closesocket
203 #define closesocket qemu_closesocket_wrap
204 int qemu_closesocket_wrap(int fd);
205 
206 #undef getsockopt
207 #define getsockopt qemu_getsockopt_wrap
208 int qemu_getsockopt_wrap(int sockfd, int level, int optname,
209                          void *optval, socklen_t *optlen);
210 
211 #undef setsockopt
212 #define setsockopt qemu_setsockopt_wrap
213 int qemu_setsockopt_wrap(int sockfd, int level, int optname,
214                          const void *optval, socklen_t optlen);
215 
216 #undef getpeername
217 #define getpeername qemu_getpeername_wrap
218 int qemu_getpeername_wrap(int sockfd, struct sockaddr *addr,
219                           socklen_t *addrlen);
220 
221 #undef getsockname
222 #define getsockname qemu_getsockname_wrap
223 int qemu_getsockname_wrap(int sockfd, struct sockaddr *addr,
224                           socklen_t *addrlen);
225 
226 #undef send
227 #define send qemu_send_wrap
228 ssize_t qemu_send_wrap(int sockfd, const void *buf, size_t len, int flags);
229 
230 #undef sendto
231 #define sendto qemu_sendto_wrap
232 ssize_t qemu_sendto_wrap(int sockfd, const void *buf, size_t len, int flags,
233                          const struct sockaddr *addr, socklen_t addrlen);
234 
235 #undef recv
236 #define recv qemu_recv_wrap
237 ssize_t qemu_recv_wrap(int sockfd, void *buf, size_t len, int flags);
238 
239 #undef recvfrom
240 #define recvfrom qemu_recvfrom_wrap
241 ssize_t qemu_recvfrom_wrap(int sockfd, void *buf, size_t len, int flags,
242                            struct sockaddr *addr, socklen_t *addrlen);
243 
244 #ifdef __cplusplus
245 }
246 #endif
247 
248 #endif
249