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 "qemu-common.h"
36 #include "qapi/error.h"
37 #include "qemu/main-loop.h"
39 #include "qemu/sockets.h"
40 #include "qemu/cutils.h"
41 #include "qemu/error-report.h"
44 /* this must come after including "trace.h" */
47 static int get_allocation_granularity(void)
49 SYSTEM_INFO system_info
;
51 GetSystemInfo(&system_info
);
52 return system_info
.dwAllocationGranularity
;
55 void *qemu_anon_ram_alloc(size_t size
, uint64_t *align
, bool shared
,
62 * We need a MEM_COMMIT before accessing any memory in a MEM_RESERVE
63 * area; we cannot easily mimic POSIX MAP_NORESERVE semantics.
65 error_report("Skipping reservation of swap space is not supported.");
69 ptr
= VirtualAlloc(NULL
, size
, MEM_COMMIT
, PAGE_READWRITE
);
70 trace_qemu_anon_ram_alloc(size
, ptr
);
73 *align
= MAX(get_allocation_granularity(), getpagesize());
78 void qemu_anon_ram_free(void *ptr
, size_t size
)
80 trace_qemu_anon_ram_free(ptr
, size
);
82 VirtualFree(ptr
, 0, MEM_RELEASE
);
86 #ifndef _POSIX_THREAD_SAFE_FUNCTIONS
87 /* FIXME: add proper locking */
88 struct tm
*gmtime_r(const time_t *timep
, struct tm
*result
)
90 struct tm
*p
= gmtime(timep
);
91 memset(result
, 0, sizeof(*result
));
99 /* FIXME: add proper locking */
100 struct tm
*localtime_r(const time_t *timep
, struct tm
*result
)
102 struct tm
*p
= localtime(timep
);
103 memset(result
, 0, sizeof(*result
));
110 #endif /* _POSIX_THREAD_SAFE_FUNCTIONS */
112 static int socket_error(void)
114 switch (WSAGetLastError()) {
121 case WSA_INVALID_HANDLE
:
123 case WSA_NOT_ENOUGH_MEMORY
:
125 case WSA_INVALID_PARAMETER
:
127 case WSAENAMETOOLONG
:
132 /* not using EWOULDBLOCK as we don't want code to have
133 * to check both EWOULDBLOCK and EAGAIN */
141 case WSAEDESTADDRREQ
:
149 case WSAEPROTONOSUPPORT
:
150 return EPROTONOSUPPORT
;
153 case WSAEAFNOSUPPORT
:
157 case WSAEADDRNOTAVAIL
:
158 return EADDRNOTAVAIL
;
165 case WSAECONNABORTED
:
177 case WSAECONNREFUSED
:
181 case WSAEHOSTUNREACH
:
188 void qemu_set_block(int fd
)
190 unsigned long opt
= 0;
191 WSAEventSelect(fd
, NULL
, 0);
192 ioctlsocket(fd
, FIONBIO
, &opt
);
195 int qemu_try_set_nonblock(int fd
)
197 unsigned long opt
= 1;
198 if (ioctlsocket(fd
, FIONBIO
, &opt
) != NO_ERROR
) {
199 return -socket_error();
204 void qemu_set_nonblock(int fd
)
206 (void)qemu_try_set_nonblock(fd
);
209 int socket_set_fast_reuse(int fd
)
211 /* Enabling the reuse of an endpoint that was used by a socket still in
212 * TIME_WAIT state is usually performed by setting SO_REUSEADDR. On Windows
213 * fast reuse is the default and SO_REUSEADDR does strange things. So we
214 * don't have to do anything here. More info can be found at:
215 * http://msdn.microsoft.com/en-us/library/windows/desktop/ms740621.aspx */
219 int inet_aton(const char *cp
, struct in_addr
*ia
)
221 uint32_t addr
= inet_addr(cp
);
222 if (addr
== 0xffffffff) {
229 void qemu_set_cloexec(int fd
)
233 /* Offset between 1/1/1601 and 1/1/1970 in 100 nanosec units */
234 #define _W32_FT_OFFSET (116444736000000000ULL)
236 int qemu_gettimeofday(qemu_timeval
*tp
)
239 unsigned long long ns100
; /*time since 1 Jan 1601 in 100ns units */
244 GetSystemTimeAsFileTime (&_now
.ft
);
245 tp
->tv_usec
=(long)((_now
.ns100
/ 10ULL) % 1000000ULL );
246 tp
->tv_sec
= (long)((_now
.ns100
- _W32_FT_OFFSET
) / 10000000ULL);
248 /* Always return 0 as per Open Group Base Specifications Issue 6.
249 Do not set errno on error. */
253 int qemu_get_thread_id(void)
255 return GetCurrentThreadId();
259 qemu_get_local_state_pathname(const char *relative_pathname
)
262 char base_path
[MAX_PATH
+1] = "";
264 result
= SHGetFolderPath(NULL
, CSIDL_COMMON_APPDATA
, NULL
,
265 /* SHGFP_TYPE_CURRENT */ 0, base_path
);
266 if (result
!= S_OK
) {
267 /* misconfigured environment */
268 g_critical("CSIDL_COMMON_APPDATA unavailable: %ld", (long)result
);
271 return g_strdup_printf("%s" G_DIR_SEPARATOR_S
"%s", base_path
,
275 void qemu_set_tty_echo(int fd
, bool echo
)
277 HANDLE handle
= (HANDLE
)_get_osfhandle(fd
);
280 if (handle
== INVALID_HANDLE_VALUE
) {
284 GetConsoleMode(handle
, &dwMode
);
287 SetConsoleMode(handle
, dwMode
| ENABLE_ECHO_INPUT
| ENABLE_LINE_INPUT
);
289 SetConsoleMode(handle
,
290 dwMode
& ~(ENABLE_ECHO_INPUT
| ENABLE_LINE_INPUT
));
294 static const char *exec_dir
;
296 void qemu_init_exec_dir(const char *argv0
)
307 len
= GetModuleFileName(NULL
, buf
, sizeof(buf
) - 1);
314 while (p
!= buf
&& *p
!= '\\') {
318 if (access(buf
, R_OK
) == 0) {
319 exec_dir
= g_strdup(buf
);
321 exec_dir
= CONFIG_BINDIR
;
325 const char *qemu_get_exec_dir(void)
330 int getpagesize(void)
332 SYSTEM_INFO system_info
;
334 GetSystemInfo(&system_info
);
335 return system_info
.dwPageSize
;
338 void os_mem_prealloc(int fd
, char *area
, size_t memory
, int smp_cpus
,
342 size_t pagesize
= qemu_real_host_page_size
;
344 memory
= (memory
+ pagesize
- 1) & -pagesize
;
345 for (i
= 0; i
< memory
/ pagesize
; i
++) {
346 memset(area
+ pagesize
* i
, 0, 1);
350 char *qemu_get_pid_name(pid_t pid
)
352 /* XXX Implement me */
357 pid_t
qemu_fork(Error
**errp
)
360 error_setg_errno(errp
, errno
,
361 "cannot fork child process");
367 int qemu_connect_wrap(int sockfd
, const struct sockaddr
*addr
,
371 ret
= connect(sockfd
, addr
, addrlen
);
373 if (WSAGetLastError() == WSAEWOULDBLOCK
) {
376 errno
= socket_error();
384 int qemu_listen_wrap(int sockfd
, int backlog
)
387 ret
= listen(sockfd
, backlog
);
389 errno
= socket_error();
396 int qemu_bind_wrap(int sockfd
, const struct sockaddr
*addr
,
400 ret
= bind(sockfd
, addr
, addrlen
);
402 errno
= socket_error();
409 int qemu_socket_wrap(int domain
, int type
, int protocol
)
412 ret
= socket(domain
, type
, protocol
);
414 errno
= socket_error();
421 int qemu_accept_wrap(int sockfd
, struct sockaddr
*addr
,
425 ret
= accept(sockfd
, addr
, addrlen
);
427 errno
= socket_error();
434 int qemu_shutdown_wrap(int sockfd
, int how
)
437 ret
= shutdown(sockfd
, how
);
439 errno
= socket_error();
446 int qemu_ioctlsocket_wrap(int fd
, int req
, void *val
)
449 ret
= ioctlsocket(fd
, req
, val
);
451 errno
= socket_error();
458 int qemu_closesocket_wrap(int fd
)
461 ret
= closesocket(fd
);
463 errno
= socket_error();
470 int qemu_getsockopt_wrap(int sockfd
, int level
, int optname
,
471 void *optval
, socklen_t
*optlen
)
474 ret
= getsockopt(sockfd
, level
, optname
, optval
, optlen
);
476 errno
= socket_error();
483 int qemu_setsockopt_wrap(int sockfd
, int level
, int optname
,
484 const void *optval
, socklen_t optlen
)
487 ret
= setsockopt(sockfd
, level
, optname
, optval
, optlen
);
489 errno
= socket_error();
496 int qemu_getpeername_wrap(int sockfd
, struct sockaddr
*addr
,
500 ret
= getpeername(sockfd
, addr
, addrlen
);
502 errno
= socket_error();
509 int qemu_getsockname_wrap(int sockfd
, struct sockaddr
*addr
,
513 ret
= getsockname(sockfd
, addr
, addrlen
);
515 errno
= socket_error();
522 ssize_t
qemu_send_wrap(int sockfd
, const void *buf
, size_t len
, int flags
)
525 ret
= send(sockfd
, buf
, len
, flags
);
527 errno
= socket_error();
534 ssize_t
qemu_sendto_wrap(int sockfd
, const void *buf
, size_t len
, int flags
,
535 const struct sockaddr
*addr
, socklen_t addrlen
)
538 ret
= sendto(sockfd
, buf
, len
, flags
, addr
, addrlen
);
540 errno
= socket_error();
547 ssize_t
qemu_recv_wrap(int sockfd
, void *buf
, size_t len
, int flags
)
550 ret
= recv(sockfd
, buf
, len
, flags
);
552 errno
= socket_error();
559 ssize_t
qemu_recvfrom_wrap(int sockfd
, void *buf
, size_t len
, int flags
,
560 struct sockaddr
*addr
, socklen_t
*addrlen
)
563 ret
= recvfrom(sockfd
, buf
, len
, flags
, addr
, addrlen
);
565 errno
= socket_error();
570 bool qemu_write_pidfile(const char *filename
, Error
**errp
)
577 memset(&overlap
, 0, sizeof(overlap
));
579 file
= CreateFile(filename
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
580 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
582 if (file
== INVALID_HANDLE_VALUE
) {
583 error_setg(errp
, "Failed to create PID file");
586 len
= snprintf(buffer
, sizeof(buffer
), FMT_pid
"\n", (pid_t
)getpid());
587 ret
= WriteFile(file
, (LPCVOID
)buffer
, (DWORD
)len
,
591 error_setg(errp
, "Failed to write PID file");
597 char *qemu_get_host_name(Error
**errp
)
599 wchar_t tmp
[MAX_COMPUTERNAME_LENGTH
+ 1];
600 DWORD size
= G_N_ELEMENTS(tmp
);
602 if (GetComputerNameW(tmp
, &size
) == 0) {
603 error_setg_win32(errp
, GetLastError(), "failed close handle");
607 return g_utf16_to_utf8(tmp
, size
, NULL
, NULL
, NULL
);
610 size_t qemu_get_host_physmem(void)
612 MEMORYSTATUSEX statex
;
613 statex
.dwLength
= sizeof(statex
);
615 if (GlobalMemoryStatusEx(&statex
)) {
616 return statex
.ullTotalPhys
;