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 int qemu_get_thread_id(void)
235 return GetCurrentThreadId();
239 qemu_get_local_state_pathname(const char *relative_pathname
)
242 char base_path
[MAX_PATH
+1] = "";
244 result
= SHGetFolderPath(NULL
, CSIDL_COMMON_APPDATA
, NULL
,
245 /* SHGFP_TYPE_CURRENT */ 0, base_path
);
246 if (result
!= S_OK
) {
247 /* misconfigured environment */
248 g_critical("CSIDL_COMMON_APPDATA unavailable: %ld", (long)result
);
251 return g_strdup_printf("%s" G_DIR_SEPARATOR_S
"%s", base_path
,
255 void qemu_set_tty_echo(int fd
, bool echo
)
257 HANDLE handle
= (HANDLE
)_get_osfhandle(fd
);
260 if (handle
== INVALID_HANDLE_VALUE
) {
264 GetConsoleMode(handle
, &dwMode
);
267 SetConsoleMode(handle
, dwMode
| ENABLE_ECHO_INPUT
| ENABLE_LINE_INPUT
);
269 SetConsoleMode(handle
,
270 dwMode
& ~(ENABLE_ECHO_INPUT
| ENABLE_LINE_INPUT
));
274 static const char *exec_dir
;
276 void qemu_init_exec_dir(const char *argv0
)
287 len
= GetModuleFileName(NULL
, buf
, sizeof(buf
) - 1);
294 while (p
!= buf
&& *p
!= '\\') {
298 if (access(buf
, R_OK
) == 0) {
299 exec_dir
= g_strdup(buf
);
301 exec_dir
= CONFIG_BINDIR
;
305 const char *qemu_get_exec_dir(void)
310 int getpagesize(void)
312 SYSTEM_INFO system_info
;
314 GetSystemInfo(&system_info
);
315 return system_info
.dwPageSize
;
318 void os_mem_prealloc(int fd
, char *area
, size_t memory
, int smp_cpus
,
322 size_t pagesize
= qemu_real_host_page_size();
324 memory
= (memory
+ pagesize
- 1) & -pagesize
;
325 for (i
= 0; i
< memory
/ pagesize
; i
++) {
326 memset(area
+ pagesize
* i
, 0, 1);
330 char *qemu_get_pid_name(pid_t pid
)
332 /* XXX Implement me */
337 pid_t
qemu_fork(Error
**errp
)
340 error_setg_errno(errp
, errno
,
341 "cannot fork child process");
347 int qemu_connect_wrap(int sockfd
, const struct sockaddr
*addr
,
351 ret
= connect(sockfd
, addr
, addrlen
);
353 if (WSAGetLastError() == WSAEWOULDBLOCK
) {
356 errno
= socket_error();
364 int qemu_listen_wrap(int sockfd
, int backlog
)
367 ret
= listen(sockfd
, backlog
);
369 errno
= socket_error();
376 int qemu_bind_wrap(int sockfd
, const struct sockaddr
*addr
,
380 ret
= bind(sockfd
, addr
, addrlen
);
382 errno
= socket_error();
389 int qemu_socket_wrap(int domain
, int type
, int protocol
)
392 ret
= socket(domain
, type
, protocol
);
394 errno
= socket_error();
401 int qemu_accept_wrap(int sockfd
, struct sockaddr
*addr
,
405 ret
= accept(sockfd
, addr
, addrlen
);
407 errno
= socket_error();
414 int qemu_shutdown_wrap(int sockfd
, int how
)
417 ret
= shutdown(sockfd
, how
);
419 errno
= socket_error();
426 int qemu_ioctlsocket_wrap(int fd
, int req
, void *val
)
429 ret
= ioctlsocket(fd
, req
, val
);
431 errno
= socket_error();
438 int qemu_closesocket_wrap(int fd
)
441 ret
= closesocket(fd
);
443 errno
= socket_error();
450 int qemu_getsockopt_wrap(int sockfd
, int level
, int optname
,
451 void *optval
, socklen_t
*optlen
)
454 ret
= getsockopt(sockfd
, level
, optname
, optval
, optlen
);
456 errno
= socket_error();
463 int qemu_setsockopt_wrap(int sockfd
, int level
, int optname
,
464 const void *optval
, socklen_t optlen
)
467 ret
= setsockopt(sockfd
, level
, optname
, optval
, optlen
);
469 errno
= socket_error();
476 int qemu_getpeername_wrap(int sockfd
, struct sockaddr
*addr
,
480 ret
= getpeername(sockfd
, addr
, addrlen
);
482 errno
= socket_error();
489 int qemu_getsockname_wrap(int sockfd
, struct sockaddr
*addr
,
493 ret
= getsockname(sockfd
, addr
, addrlen
);
495 errno
= socket_error();
502 ssize_t
qemu_send_wrap(int sockfd
, const void *buf
, size_t len
, int flags
)
505 ret
= send(sockfd
, buf
, len
, flags
);
507 errno
= socket_error();
514 ssize_t
qemu_sendto_wrap(int sockfd
, const void *buf
, size_t len
, int flags
,
515 const struct sockaddr
*addr
, socklen_t addrlen
)
518 ret
= sendto(sockfd
, buf
, len
, flags
, addr
, addrlen
);
520 errno
= socket_error();
527 ssize_t
qemu_recv_wrap(int sockfd
, void *buf
, size_t len
, int flags
)
530 ret
= recv(sockfd
, buf
, len
, flags
);
532 errno
= socket_error();
539 ssize_t
qemu_recvfrom_wrap(int sockfd
, void *buf
, size_t len
, int flags
,
540 struct sockaddr
*addr
, socklen_t
*addrlen
)
543 ret
= recvfrom(sockfd
, buf
, len
, flags
, addr
, addrlen
);
545 errno
= socket_error();
550 bool qemu_write_pidfile(const char *filename
, Error
**errp
)
557 memset(&overlap
, 0, sizeof(overlap
));
559 file
= CreateFile(filename
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
560 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
562 if (file
== INVALID_HANDLE_VALUE
) {
563 error_setg(errp
, "Failed to create PID file");
566 len
= snprintf(buffer
, sizeof(buffer
), FMT_pid
"\n", (pid_t
)getpid());
567 ret
= WriteFile(file
, (LPCVOID
)buffer
, (DWORD
)len
,
571 error_setg(errp
, "Failed to write PID file");
577 char *qemu_get_host_name(Error
**errp
)
579 wchar_t tmp
[MAX_COMPUTERNAME_LENGTH
+ 1];
580 DWORD size
= G_N_ELEMENTS(tmp
);
582 if (GetComputerNameW(tmp
, &size
) == 0) {
583 error_setg_win32(errp
, GetLastError(), "failed close handle");
587 return g_utf16_to_utf8(tmp
, size
, NULL
, NULL
, NULL
);
590 size_t qemu_get_host_physmem(void)
592 MEMORYSTATUSEX statex
;
593 statex
.dwLength
= sizeof(statex
);
595 if (GlobalMemoryStatusEx(&statex
)) {
596 return statex
.ullTotalPhys
;