xref: /qemu/include/sysemu/os-win32.h (revision d709bbf3)
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 __cplusplus
34 extern "C" {
35 #endif
36 
37 #if defined(_WIN64)
38 /* On w64, setjmp is implemented by _setjmp which needs a second parameter.
39  * If this parameter is NULL, longjump does no stack unwinding.
40  * That is what we need for QEMU. Passing the value of register rsp (default)
41  * lets longjmp try a stack unwinding which will crash with generated code. */
42 # undef setjmp
43 # define setjmp(env) _setjmp(env, NULL)
44 #endif
45 /* QEMU uses sigsetjmp()/siglongjmp() as the portable way to specify
46  * "longjmp and don't touch the signal masks". Since we know that the
47  * savemask parameter will always be zero we can safely define these
48  * in terms of setjmp/longjmp on Win32.
49  */
50 #define sigjmp_buf jmp_buf
51 #define sigsetjmp(env, savemask) setjmp(env)
52 #define siglongjmp(env, val) longjmp(env, val)
53 
54 /* Missing POSIX functions. Don't use MinGW-w64 macros. */
55 #ifndef _POSIX_THREAD_SAFE_FUNCTIONS
56 #undef gmtime_r
57 struct tm *gmtime_r(const time_t *timep, struct tm *result);
58 #undef localtime_r
59 struct tm *localtime_r(const time_t *timep, struct tm *result);
60 #endif /* _POSIX_THREAD_SAFE_FUNCTIONS */
61 
62 static inline void os_setup_signal_handling(void) {}
63 static inline void os_daemonize(void) {}
64 static inline void os_setup_post(void) {}
65 void os_set_line_buffering(void);
66 static inline void os_set_proc_name(const char *dummy) {}
67 
68 int getpagesize(void);
69 
70 #if !defined(EPROTONOSUPPORT)
71 # define EPROTONOSUPPORT EINVAL
72 #endif
73 
74 static inline int os_set_daemonize(bool d)
75 {
76     if (d) {
77         return -ENOTSUP;
78     }
79     return 0;
80 }
81 
82 static inline bool is_daemonized(void)
83 {
84     return false;
85 }
86 
87 static inline int os_mlock(void)
88 {
89     return -ENOSYS;
90 }
91 
92 #define fsync _commit
93 
94 #if !defined(lseek)
95 # define lseek _lseeki64
96 #endif
97 
98 int qemu_ftruncate64(int, int64_t);
99 
100 #if !defined(ftruncate)
101 # define ftruncate qemu_ftruncate64
102 #endif
103 
104 static inline char *realpath(const char *path, char *resolved_path)
105 {
106     _fullpath(resolved_path, path, _MAX_PATH);
107     return resolved_path;
108 }
109 
110 /* ??? Mingw appears to export _lock_file and _unlock_file as the functions
111  * with which to lock a stdio handle.  But something is wrong in the markup,
112  * either in the header or the library, such that we get undefined references
113  * to "_imp___lock_file" etc when linking.  Since we seem to have no other
114  * alternative, and the usage within the logging functions isn't critical,
115  * ignore FILE locking.
116  */
117 
118 static inline void qemu_flockfile(FILE *f)
119 {
120 }
121 
122 static inline void qemu_funlockfile(FILE *f)
123 {
124 }
125 
126 /* We wrap all the sockets functions so that we can
127  * set errno based on WSAGetLastError()
128  */
129 
130 #undef connect
131 #define connect qemu_connect_wrap
132 int qemu_connect_wrap(int sockfd, const struct sockaddr *addr,
133                       socklen_t addrlen);
134 
135 #undef listen
136 #define listen qemu_listen_wrap
137 int qemu_listen_wrap(int sockfd, int backlog);
138 
139 #undef bind
140 #define bind qemu_bind_wrap
141 int qemu_bind_wrap(int sockfd, const struct sockaddr *addr,
142                    socklen_t addrlen);
143 
144 #undef socket
145 #define socket qemu_socket_wrap
146 int qemu_socket_wrap(int domain, int type, int protocol);
147 
148 #undef accept
149 #define accept qemu_accept_wrap
150 int qemu_accept_wrap(int sockfd, struct sockaddr *addr,
151                      socklen_t *addrlen);
152 
153 #undef shutdown
154 #define shutdown qemu_shutdown_wrap
155 int qemu_shutdown_wrap(int sockfd, int how);
156 
157 #undef ioctlsocket
158 #define ioctlsocket qemu_ioctlsocket_wrap
159 int qemu_ioctlsocket_wrap(int fd, int req, void *val);
160 
161 #undef closesocket
162 #define closesocket qemu_closesocket_wrap
163 int qemu_closesocket_wrap(int fd);
164 
165 #undef getsockopt
166 #define getsockopt qemu_getsockopt_wrap
167 int qemu_getsockopt_wrap(int sockfd, int level, int optname,
168                          void *optval, socklen_t *optlen);
169 
170 #undef setsockopt
171 #define setsockopt qemu_setsockopt_wrap
172 int qemu_setsockopt_wrap(int sockfd, int level, int optname,
173                          const void *optval, socklen_t optlen);
174 
175 #undef getpeername
176 #define getpeername qemu_getpeername_wrap
177 int qemu_getpeername_wrap(int sockfd, struct sockaddr *addr,
178                           socklen_t *addrlen);
179 
180 #undef getsockname
181 #define getsockname qemu_getsockname_wrap
182 int qemu_getsockname_wrap(int sockfd, struct sockaddr *addr,
183                           socklen_t *addrlen);
184 
185 #undef send
186 #define send qemu_send_wrap
187 ssize_t qemu_send_wrap(int sockfd, const void *buf, size_t len, int flags);
188 
189 #undef sendto
190 #define sendto qemu_sendto_wrap
191 ssize_t qemu_sendto_wrap(int sockfd, const void *buf, size_t len, int flags,
192                          const struct sockaddr *addr, socklen_t addrlen);
193 
194 #undef recv
195 #define recv qemu_recv_wrap
196 ssize_t qemu_recv_wrap(int sockfd, void *buf, size_t len, int flags);
197 
198 #undef recvfrom
199 #define recvfrom qemu_recvfrom_wrap
200 ssize_t qemu_recvfrom_wrap(int sockfd, void *buf, size_t len, int flags,
201                            struct sockaddr *addr, socklen_t *addrlen);
202 
203 #ifdef __cplusplus
204 }
205 #endif
206 
207 #endif
208