4 * Copyright (c) 2003-2008 Fabrice Bellard
5 * Copyright (c) 2010-2016 Red Hat, Inc.
7 * QEMU library functions for win32 which are shared between QEMU and
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28 * The implementation of g_poll (functions poll_rest, g_poll) at the end of
29 * this file are based on code from GNOME glib-2 and use a different license,
30 * see the license comment there.
33 #include "qemu/osdep.h"
35 #include "qapi/error.h"
36 #include "qemu/main-loop.h"
38 #include "qemu/sockets.h"
39 #include "qemu/cutils.h"
40 #include "qemu/error-report.h"
43 static int get_allocation_granularity(void)
45 SYSTEM_INFO system_info
;
47 GetSystemInfo(&system_info
);
48 return system_info
.dwAllocationGranularity
;
51 void *qemu_anon_ram_alloc(size_t size
, uint64_t *align
, bool shared
,
58 * We need a MEM_COMMIT before accessing any memory in a MEM_RESERVE
59 * area; we cannot easily mimic POSIX MAP_NORESERVE semantics.
61 error_report("Skipping reservation of swap space is not supported.");
65 ptr
= VirtualAlloc(NULL
, size
, MEM_COMMIT
, PAGE_READWRITE
);
66 trace_qemu_anon_ram_alloc(size
, ptr
);
69 *align
= MAX(get_allocation_granularity(), getpagesize());
74 void qemu_anon_ram_free(void *ptr
, size_t size
)
76 trace_qemu_anon_ram_free(ptr
, size
);
78 VirtualFree(ptr
, 0, MEM_RELEASE
);
82 #ifndef _POSIX_THREAD_SAFE_FUNCTIONS
83 /* FIXME: add proper locking */
84 struct tm
*gmtime_r(const time_t *timep
, struct tm
*result
)
86 struct tm
*p
= gmtime(timep
);
87 memset(result
, 0, sizeof(*result
));
95 /* FIXME: add proper locking */
96 struct tm
*localtime_r(const time_t *timep
, struct tm
*result
)
98 struct tm
*p
= localtime(timep
);
99 memset(result
, 0, sizeof(*result
));
106 #endif /* _POSIX_THREAD_SAFE_FUNCTIONS */
108 static int socket_error(void)
110 switch (WSAGetLastError()) {
117 case WSA_INVALID_HANDLE
:
119 case WSA_NOT_ENOUGH_MEMORY
:
121 case WSA_INVALID_PARAMETER
:
123 case WSAENAMETOOLONG
:
128 /* not using EWOULDBLOCK as we don't want code to have
129 * to check both EWOULDBLOCK and EAGAIN */
137 case WSAEDESTADDRREQ
:
145 case WSAEPROTONOSUPPORT
:
146 return EPROTONOSUPPORT
;
149 case WSAEAFNOSUPPORT
:
153 case WSAEADDRNOTAVAIL
:
154 return EADDRNOTAVAIL
;
161 case WSAECONNABORTED
:
173 case WSAECONNREFUSED
:
177 case WSAEHOSTUNREACH
:
184 void qemu_socket_set_block(int fd
)
186 unsigned long opt
= 0;
187 WSAEventSelect(fd
, NULL
, 0);
188 ioctlsocket(fd
, FIONBIO
, &opt
);
191 int qemu_socket_try_set_nonblock(int fd
)
193 unsigned long opt
= 1;
194 if (ioctlsocket(fd
, FIONBIO
, &opt
) != NO_ERROR
) {
195 return -socket_error();
200 void qemu_socket_set_nonblock(int fd
)
202 (void)qemu_socket_try_set_nonblock(fd
);
205 int socket_set_fast_reuse(int fd
)
207 /* Enabling the reuse of an endpoint that was used by a socket still in
208 * TIME_WAIT state is usually performed by setting SO_REUSEADDR. On Windows
209 * fast reuse is the default and SO_REUSEADDR does strange things. So we
210 * don't have to do anything here. More info can be found at:
211 * http://msdn.microsoft.com/en-us/library/windows/desktop/ms740621.aspx */
215 int inet_aton(const char *cp
, struct in_addr
*ia
)
217 uint32_t addr
= inet_addr(cp
);
218 if (addr
== 0xffffffff) {
225 void qemu_set_cloexec(int fd
)
229 int qemu_get_thread_id(void)
231 return GetCurrentThreadId();
235 qemu_get_local_state_dir(void)
237 const char * const *data_dirs
= g_get_system_data_dirs();
239 g_assert(data_dirs
&& data_dirs
[0]);
241 return g_strdup(data_dirs
[0]);
244 void qemu_set_tty_echo(int fd
, bool echo
)
246 HANDLE handle
= (HANDLE
)_get_osfhandle(fd
);
249 if (handle
== INVALID_HANDLE_VALUE
) {
253 GetConsoleMode(handle
, &dwMode
);
256 SetConsoleMode(handle
, dwMode
| ENABLE_ECHO_INPUT
| ENABLE_LINE_INPUT
);
258 SetConsoleMode(handle
,
259 dwMode
& ~(ENABLE_ECHO_INPUT
| ENABLE_LINE_INPUT
));
263 int getpagesize(void)
265 SYSTEM_INFO system_info
;
267 GetSystemInfo(&system_info
);
268 return system_info
.dwPageSize
;
271 void os_mem_prealloc(int fd
, char *area
, size_t memory
, int smp_cpus
,
275 size_t pagesize
= qemu_real_host_page_size();
277 memory
= (memory
+ pagesize
- 1) & -pagesize
;
278 for (i
= 0; i
< memory
/ pagesize
; i
++) {
279 memset(area
+ pagesize
* i
, 0, 1);
283 char *qemu_get_pid_name(pid_t pid
)
285 /* XXX Implement me */
290 pid_t
qemu_fork(Error
**errp
)
293 error_setg_errno(errp
, errno
,
294 "cannot fork child process");
300 int qemu_connect_wrap(int sockfd
, const struct sockaddr
*addr
,
304 ret
= connect(sockfd
, addr
, addrlen
);
306 if (WSAGetLastError() == WSAEWOULDBLOCK
) {
309 errno
= socket_error();
317 int qemu_listen_wrap(int sockfd
, int backlog
)
320 ret
= listen(sockfd
, backlog
);
322 errno
= socket_error();
329 int qemu_bind_wrap(int sockfd
, const struct sockaddr
*addr
,
333 ret
= bind(sockfd
, addr
, addrlen
);
335 errno
= socket_error();
342 int qemu_socket_wrap(int domain
, int type
, int protocol
)
345 ret
= socket(domain
, type
, protocol
);
347 errno
= socket_error();
354 int qemu_accept_wrap(int sockfd
, struct sockaddr
*addr
,
358 ret
= accept(sockfd
, addr
, addrlen
);
360 errno
= socket_error();
367 int qemu_shutdown_wrap(int sockfd
, int how
)
370 ret
= shutdown(sockfd
, how
);
372 errno
= socket_error();
379 int qemu_ioctlsocket_wrap(int fd
, int req
, void *val
)
382 ret
= ioctlsocket(fd
, req
, val
);
384 errno
= socket_error();
391 int qemu_closesocket_wrap(int fd
)
394 ret
= closesocket(fd
);
396 errno
= socket_error();
403 int qemu_getsockopt_wrap(int sockfd
, int level
, int optname
,
404 void *optval
, socklen_t
*optlen
)
407 ret
= getsockopt(sockfd
, level
, optname
, optval
, optlen
);
409 errno
= socket_error();
416 int qemu_setsockopt_wrap(int sockfd
, int level
, int optname
,
417 const void *optval
, socklen_t optlen
)
420 ret
= setsockopt(sockfd
, level
, optname
, optval
, optlen
);
422 errno
= socket_error();
429 int qemu_getpeername_wrap(int sockfd
, struct sockaddr
*addr
,
433 ret
= getpeername(sockfd
, addr
, addrlen
);
435 errno
= socket_error();
442 int qemu_getsockname_wrap(int sockfd
, struct sockaddr
*addr
,
446 ret
= getsockname(sockfd
, addr
, addrlen
);
448 errno
= socket_error();
455 ssize_t
qemu_send_wrap(int sockfd
, const void *buf
, size_t len
, int flags
)
458 ret
= send(sockfd
, buf
, len
, flags
);
460 errno
= socket_error();
467 ssize_t
qemu_sendto_wrap(int sockfd
, const void *buf
, size_t len
, int flags
,
468 const struct sockaddr
*addr
, socklen_t addrlen
)
471 ret
= sendto(sockfd
, buf
, len
, flags
, addr
, addrlen
);
473 errno
= socket_error();
480 ssize_t
qemu_recv_wrap(int sockfd
, void *buf
, size_t len
, int flags
)
483 ret
= recv(sockfd
, buf
, len
, flags
);
485 errno
= socket_error();
492 ssize_t
qemu_recvfrom_wrap(int sockfd
, void *buf
, size_t len
, int flags
,
493 struct sockaddr
*addr
, socklen_t
*addrlen
)
496 ret
= recvfrom(sockfd
, buf
, len
, flags
, addr
, addrlen
);
498 errno
= socket_error();
503 bool qemu_write_pidfile(const char *filename
, Error
**errp
)
510 memset(&overlap
, 0, sizeof(overlap
));
512 file
= CreateFile(filename
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
513 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
515 if (file
== INVALID_HANDLE_VALUE
) {
516 error_setg(errp
, "Failed to create PID file");
519 len
= snprintf(buffer
, sizeof(buffer
), FMT_pid
"\n", (pid_t
)getpid());
520 ret
= WriteFile(file
, (LPCVOID
)buffer
, (DWORD
)len
,
524 error_setg(errp
, "Failed to write PID file");
530 size_t qemu_get_host_physmem(void)
532 MEMORYSTATUSEX statex
;
533 statex
.dwLength
= sizeof(statex
);
535 if (GlobalMemoryStatusEx(&statex
)) {
536 return statex
.ullTotalPhys
;
541 int qemu_msync(void *addr
, size_t length
, int fd
)
544 * Perform the sync based on the file descriptor
545 * The sync range will most probably be wider than the one
546 * requested - but it will still get the job done
548 return qemu_fdatasync(fd
);