]>
git.proxmox.com Git - mirror_qemu.git/blob - util/oslib-win32.c
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 void *qemu_try_memalign(size_t alignment
, size_t size
)
52 if (alignment
< sizeof(void *)) {
53 alignment
= sizeof(void *);
55 g_assert(is_power_of_2(alignment
));
57 ptr
= _aligned_malloc(size
, alignment
);
58 trace_qemu_memalign(alignment
, size
, ptr
);
62 static int get_allocation_granularity(void)
64 SYSTEM_INFO system_info
;
66 GetSystemInfo(&system_info
);
67 return system_info
.dwAllocationGranularity
;
70 void *qemu_anon_ram_alloc(size_t size
, uint64_t *align
, bool shared
,
77 * We need a MEM_COMMIT before accessing any memory in a MEM_RESERVE
78 * area; we cannot easily mimic POSIX MAP_NORESERVE semantics.
80 error_report("Skipping reservation of swap space is not supported.");
84 ptr
= VirtualAlloc(NULL
, size
, MEM_COMMIT
, PAGE_READWRITE
);
85 trace_qemu_anon_ram_alloc(size
, ptr
);
88 *align
= MAX(get_allocation_granularity(), getpagesize());
93 void qemu_vfree(void *ptr
)
95 trace_qemu_vfree(ptr
);
99 void qemu_anon_ram_free(void *ptr
, size_t size
)
101 trace_qemu_anon_ram_free(ptr
, size
);
103 VirtualFree(ptr
, 0, MEM_RELEASE
);
107 #ifndef _POSIX_THREAD_SAFE_FUNCTIONS
108 /* FIXME: add proper locking */
109 struct tm
*gmtime_r(const time_t *timep
, struct tm
*result
)
111 struct tm
*p
= gmtime(timep
);
112 memset(result
, 0, sizeof(*result
));
120 /* FIXME: add proper locking */
121 struct tm
*localtime_r(const time_t *timep
, struct tm
*result
)
123 struct tm
*p
= localtime(timep
);
124 memset(result
, 0, sizeof(*result
));
131 #endif /* _POSIX_THREAD_SAFE_FUNCTIONS */
133 static int socket_error(void)
135 switch (WSAGetLastError()) {
142 case WSA_INVALID_HANDLE
:
144 case WSA_NOT_ENOUGH_MEMORY
:
146 case WSA_INVALID_PARAMETER
:
148 case WSAENAMETOOLONG
:
153 /* not using EWOULDBLOCK as we don't want code to have
154 * to check both EWOULDBLOCK and EAGAIN */
162 case WSAEDESTADDRREQ
:
170 case WSAEPROTONOSUPPORT
:
171 return EPROTONOSUPPORT
;
174 case WSAEAFNOSUPPORT
:
178 case WSAEADDRNOTAVAIL
:
179 return EADDRNOTAVAIL
;
186 case WSAECONNABORTED
:
198 case WSAECONNREFUSED
:
202 case WSAEHOSTUNREACH
:
209 void qemu_set_block(int fd
)
211 unsigned long opt
= 0;
212 WSAEventSelect(fd
, NULL
, 0);
213 ioctlsocket(fd
, FIONBIO
, &opt
);
216 int qemu_try_set_nonblock(int fd
)
218 unsigned long opt
= 1;
219 if (ioctlsocket(fd
, FIONBIO
, &opt
) != NO_ERROR
) {
220 return -socket_error();
225 void qemu_set_nonblock(int fd
)
227 (void)qemu_try_set_nonblock(fd
);
230 int socket_set_fast_reuse(int fd
)
232 /* Enabling the reuse of an endpoint that was used by a socket still in
233 * TIME_WAIT state is usually performed by setting SO_REUSEADDR. On Windows
234 * fast reuse is the default and SO_REUSEADDR does strange things. So we
235 * don't have to do anything here. More info can be found at:
236 * http://msdn.microsoft.com/en-us/library/windows/desktop/ms740621.aspx */
240 int inet_aton(const char *cp
, struct in_addr
*ia
)
242 uint32_t addr
= inet_addr(cp
);
243 if (addr
== 0xffffffff) {
250 void qemu_set_cloexec(int fd
)
254 /* Offset between 1/1/1601 and 1/1/1970 in 100 nanosec units */
255 #define _W32_FT_OFFSET (116444736000000000ULL)
257 int qemu_gettimeofday(qemu_timeval
*tp
)
260 unsigned long long ns100
; /*time since 1 Jan 1601 in 100ns units */
265 GetSystemTimeAsFileTime (&_now
.ft
);
266 tp
->tv_usec
=(long)((_now
.ns100
/ 10ULL) % 1000000ULL );
267 tp
->tv_sec
= (long)((_now
.ns100
- _W32_FT_OFFSET
) / 10000000ULL);
269 /* Always return 0 as per Open Group Base Specifications Issue 6.
270 Do not set errno on error. */
274 int qemu_get_thread_id(void)
276 return GetCurrentThreadId();
280 qemu_get_local_state_pathname(const char *relative_pathname
)
283 char base_path
[MAX_PATH
+1] = "";
285 result
= SHGetFolderPath(NULL
, CSIDL_COMMON_APPDATA
, NULL
,
286 /* SHGFP_TYPE_CURRENT */ 0, base_path
);
287 if (result
!= S_OK
) {
288 /* misconfigured environment */
289 g_critical("CSIDL_COMMON_APPDATA unavailable: %ld", (long)result
);
292 return g_strdup_printf("%s" G_DIR_SEPARATOR_S
"%s", base_path
,
296 void qemu_set_tty_echo(int fd
, bool echo
)
298 HANDLE handle
= (HANDLE
)_get_osfhandle(fd
);
301 if (handle
== INVALID_HANDLE_VALUE
) {
305 GetConsoleMode(handle
, &dwMode
);
308 SetConsoleMode(handle
, dwMode
| ENABLE_ECHO_INPUT
| ENABLE_LINE_INPUT
);
310 SetConsoleMode(handle
,
311 dwMode
& ~(ENABLE_ECHO_INPUT
| ENABLE_LINE_INPUT
));
315 static const char *exec_dir
;
317 void qemu_init_exec_dir(const char *argv0
)
328 len
= GetModuleFileName(NULL
, buf
, sizeof(buf
) - 1);
335 while (p
!= buf
&& *p
!= '\\') {
339 if (access(buf
, R_OK
) == 0) {
340 exec_dir
= g_strdup(buf
);
342 exec_dir
= CONFIG_BINDIR
;
346 const char *qemu_get_exec_dir(void)
351 int getpagesize(void)
353 SYSTEM_INFO system_info
;
355 GetSystemInfo(&system_info
);
356 return system_info
.dwPageSize
;
359 void os_mem_prealloc(int fd
, char *area
, size_t memory
, int smp_cpus
,
363 size_t pagesize
= qemu_real_host_page_size
;
365 memory
= (memory
+ pagesize
- 1) & -pagesize
;
366 for (i
= 0; i
< memory
/ pagesize
; i
++) {
367 memset(area
+ pagesize
* i
, 0, 1);
371 char *qemu_get_pid_name(pid_t pid
)
373 /* XXX Implement me */
378 pid_t
qemu_fork(Error
**errp
)
381 error_setg_errno(errp
, errno
,
382 "cannot fork child process");
388 int qemu_connect_wrap(int sockfd
, const struct sockaddr
*addr
,
392 ret
= connect(sockfd
, addr
, addrlen
);
394 if (WSAGetLastError() == WSAEWOULDBLOCK
) {
397 errno
= socket_error();
405 int qemu_listen_wrap(int sockfd
, int backlog
)
408 ret
= listen(sockfd
, backlog
);
410 errno
= socket_error();
417 int qemu_bind_wrap(int sockfd
, const struct sockaddr
*addr
,
421 ret
= bind(sockfd
, addr
, addrlen
);
423 errno
= socket_error();
430 int qemu_socket_wrap(int domain
, int type
, int protocol
)
433 ret
= socket(domain
, type
, protocol
);
435 errno
= socket_error();
442 int qemu_accept_wrap(int sockfd
, struct sockaddr
*addr
,
446 ret
= accept(sockfd
, addr
, addrlen
);
448 errno
= socket_error();
455 int qemu_shutdown_wrap(int sockfd
, int how
)
458 ret
= shutdown(sockfd
, how
);
460 errno
= socket_error();
467 int qemu_ioctlsocket_wrap(int fd
, int req
, void *val
)
470 ret
= ioctlsocket(fd
, req
, val
);
472 errno
= socket_error();
479 int qemu_closesocket_wrap(int fd
)
482 ret
= closesocket(fd
);
484 errno
= socket_error();
491 int qemu_getsockopt_wrap(int sockfd
, int level
, int optname
,
492 void *optval
, socklen_t
*optlen
)
495 ret
= getsockopt(sockfd
, level
, optname
, optval
, optlen
);
497 errno
= socket_error();
504 int qemu_setsockopt_wrap(int sockfd
, int level
, int optname
,
505 const void *optval
, socklen_t optlen
)
508 ret
= setsockopt(sockfd
, level
, optname
, optval
, optlen
);
510 errno
= socket_error();
517 int qemu_getpeername_wrap(int sockfd
, struct sockaddr
*addr
,
521 ret
= getpeername(sockfd
, addr
, addrlen
);
523 errno
= socket_error();
530 int qemu_getsockname_wrap(int sockfd
, struct sockaddr
*addr
,
534 ret
= getsockname(sockfd
, addr
, addrlen
);
536 errno
= socket_error();
543 ssize_t
qemu_send_wrap(int sockfd
, const void *buf
, size_t len
, int flags
)
546 ret
= send(sockfd
, buf
, len
, flags
);
548 errno
= socket_error();
555 ssize_t
qemu_sendto_wrap(int sockfd
, const void *buf
, size_t len
, int flags
,
556 const struct sockaddr
*addr
, socklen_t addrlen
)
559 ret
= sendto(sockfd
, buf
, len
, flags
, addr
, addrlen
);
561 errno
= socket_error();
568 ssize_t
qemu_recv_wrap(int sockfd
, void *buf
, size_t len
, int flags
)
571 ret
= recv(sockfd
, buf
, len
, flags
);
573 errno
= socket_error();
580 ssize_t
qemu_recvfrom_wrap(int sockfd
, void *buf
, size_t len
, int flags
,
581 struct sockaddr
*addr
, socklen_t
*addrlen
)
584 ret
= recvfrom(sockfd
, buf
, len
, flags
, addr
, addrlen
);
586 errno
= socket_error();
591 bool qemu_write_pidfile(const char *filename
, Error
**errp
)
598 memset(&overlap
, 0, sizeof(overlap
));
600 file
= CreateFile(filename
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
601 OPEN_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
603 if (file
== INVALID_HANDLE_VALUE
) {
604 error_setg(errp
, "Failed to create PID file");
607 len
= snprintf(buffer
, sizeof(buffer
), FMT_pid
"\n", (pid_t
)getpid());
608 ret
= WriteFile(file
, (LPCVOID
)buffer
, (DWORD
)len
,
612 error_setg(errp
, "Failed to write PID file");
618 char *qemu_get_host_name(Error
**errp
)
620 wchar_t tmp
[MAX_COMPUTERNAME_LENGTH
+ 1];
621 DWORD size
= G_N_ELEMENTS(tmp
);
623 if (GetComputerNameW(tmp
, &size
) == 0) {
624 error_setg_win32(errp
, GetLastError(), "failed close handle");
628 return g_utf16_to_utf8(tmp
, size
, NULL
, NULL
, NULL
);
631 size_t qemu_get_host_physmem(void)
633 MEMORYSTATUSEX statex
;
634 statex
.dwLength
= sizeof(statex
);
636 if (GlobalMemoryStatusEx(&statex
)) {
637 return statex
.ullTotalPhys
;