machine: add mem compound property
[qemu.git] / util / oslib-win32.c
blobdafef4f157339b1ec1161e2a13345f8f2528d75c
1 /*
2 * os-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
8 * the QEMU tools.
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
26 * THE SOFTWARE.
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"
34 #include <windows.h>
35 #include "qapi/error.h"
36 #include "qemu/main-loop.h"
37 #include "trace.h"
38 #include "qemu/sockets.h"
39 #include "qemu/cutils.h"
40 #include "qemu/error-report.h"
41 #include <malloc.h>
43 /* this must come after including "trace.h" */
44 #include <shlobj.h>
46 static int get_allocation_granularity(void)
48 SYSTEM_INFO system_info;
50 GetSystemInfo(&system_info);
51 return system_info.dwAllocationGranularity;
54 void *qemu_anon_ram_alloc(size_t size, uint64_t *align, bool shared,
55 bool noreserve)
57 void *ptr;
59 if (noreserve) {
61 * We need a MEM_COMMIT before accessing any memory in a MEM_RESERVE
62 * area; we cannot easily mimic POSIX MAP_NORESERVE semantics.
64 error_report("Skipping reservation of swap space is not supported.");
65 return NULL;
68 ptr = VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE);
69 trace_qemu_anon_ram_alloc(size, ptr);
71 if (ptr && align) {
72 *align = MAX(get_allocation_granularity(), getpagesize());
74 return ptr;
77 void qemu_anon_ram_free(void *ptr, size_t size)
79 trace_qemu_anon_ram_free(ptr, size);
80 if (ptr) {
81 VirtualFree(ptr, 0, MEM_RELEASE);
85 #ifndef _POSIX_THREAD_SAFE_FUNCTIONS
86 /* FIXME: add proper locking */
87 struct tm *gmtime_r(const time_t *timep, struct tm *result)
89 struct tm *p = gmtime(timep);
90 memset(result, 0, sizeof(*result));
91 if (p) {
92 *result = *p;
93 p = result;
95 return p;
98 /* FIXME: add proper locking */
99 struct tm *localtime_r(const time_t *timep, struct tm *result)
101 struct tm *p = localtime(timep);
102 memset(result, 0, sizeof(*result));
103 if (p) {
104 *result = *p;
105 p = result;
107 return p;
109 #endif /* _POSIX_THREAD_SAFE_FUNCTIONS */
111 static int socket_error(void)
113 switch (WSAGetLastError()) {
114 case 0:
115 return 0;
116 case WSAEINTR:
117 return EINTR;
118 case WSAEINVAL:
119 return EINVAL;
120 case WSA_INVALID_HANDLE:
121 return EBADF;
122 case WSA_NOT_ENOUGH_MEMORY:
123 return ENOMEM;
124 case WSA_INVALID_PARAMETER:
125 return EINVAL;
126 case WSAENAMETOOLONG:
127 return ENAMETOOLONG;
128 case WSAENOTEMPTY:
129 return ENOTEMPTY;
130 case WSAEWOULDBLOCK:
131 /* not using EWOULDBLOCK as we don't want code to have
132 * to check both EWOULDBLOCK and EAGAIN */
133 return EAGAIN;
134 case WSAEINPROGRESS:
135 return EINPROGRESS;
136 case WSAEALREADY:
137 return EALREADY;
138 case WSAENOTSOCK:
139 return ENOTSOCK;
140 case WSAEDESTADDRREQ:
141 return EDESTADDRREQ;
142 case WSAEMSGSIZE:
143 return EMSGSIZE;
144 case WSAEPROTOTYPE:
145 return EPROTOTYPE;
146 case WSAENOPROTOOPT:
147 return ENOPROTOOPT;
148 case WSAEPROTONOSUPPORT:
149 return EPROTONOSUPPORT;
150 case WSAEOPNOTSUPP:
151 return EOPNOTSUPP;
152 case WSAEAFNOSUPPORT:
153 return EAFNOSUPPORT;
154 case WSAEADDRINUSE:
155 return EADDRINUSE;
156 case WSAEADDRNOTAVAIL:
157 return EADDRNOTAVAIL;
158 case WSAENETDOWN:
159 return ENETDOWN;
160 case WSAENETUNREACH:
161 return ENETUNREACH;
162 case WSAENETRESET:
163 return ENETRESET;
164 case WSAECONNABORTED:
165 return ECONNABORTED;
166 case WSAECONNRESET:
167 return ECONNRESET;
168 case WSAENOBUFS:
169 return ENOBUFS;
170 case WSAEISCONN:
171 return EISCONN;
172 case WSAENOTCONN:
173 return ENOTCONN;
174 case WSAETIMEDOUT:
175 return ETIMEDOUT;
176 case WSAECONNREFUSED:
177 return ECONNREFUSED;
178 case WSAELOOP:
179 return ELOOP;
180 case WSAEHOSTUNREACH:
181 return EHOSTUNREACH;
182 default:
183 return EIO;
187 void qemu_socket_set_block(int fd)
189 unsigned long opt = 0;
190 WSAEventSelect(fd, NULL, 0);
191 ioctlsocket(fd, FIONBIO, &opt);
194 int qemu_socket_try_set_nonblock(int fd)
196 unsigned long opt = 1;
197 if (ioctlsocket(fd, FIONBIO, &opt) != NO_ERROR) {
198 return -socket_error();
200 return 0;
203 void qemu_socket_set_nonblock(int fd)
205 (void)qemu_socket_try_set_nonblock(fd);
208 int socket_set_fast_reuse(int fd)
210 /* Enabling the reuse of an endpoint that was used by a socket still in
211 * TIME_WAIT state is usually performed by setting SO_REUSEADDR. On Windows
212 * fast reuse is the default and SO_REUSEADDR does strange things. So we
213 * don't have to do anything here. More info can be found at:
214 * http://msdn.microsoft.com/en-us/library/windows/desktop/ms740621.aspx */
215 return 0;
218 int inet_aton(const char *cp, struct in_addr *ia)
220 uint32_t addr = inet_addr(cp);
221 if (addr == 0xffffffff) {
222 return 0;
224 ia->s_addr = addr;
225 return 1;
228 void qemu_set_cloexec(int fd)
232 int qemu_get_thread_id(void)
234 return GetCurrentThreadId();
237 char *
238 qemu_get_local_state_dir(void)
240 HRESULT result;
241 char base_path[MAX_PATH+1] = "";
243 result = SHGetFolderPath(NULL, CSIDL_COMMON_APPDATA, NULL,
244 /* SHGFP_TYPE_CURRENT */ 0, base_path);
245 if (result != S_OK) {
246 /* misconfigured environment */
247 g_critical("CSIDL_COMMON_APPDATA unavailable: %ld", (long)result);
248 abort();
250 return g_strdup(base_path);
253 void qemu_set_tty_echo(int fd, bool echo)
255 HANDLE handle = (HANDLE)_get_osfhandle(fd);
256 DWORD dwMode = 0;
258 if (handle == INVALID_HANDLE_VALUE) {
259 return;
262 GetConsoleMode(handle, &dwMode);
264 if (echo) {
265 SetConsoleMode(handle, dwMode | ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT);
266 } else {
267 SetConsoleMode(handle,
268 dwMode & ~(ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT));
272 static const char *exec_dir;
274 void qemu_init_exec_dir(const char *argv0)
277 char *p;
278 char buf[MAX_PATH];
279 DWORD len;
281 if (exec_dir) {
282 return;
285 len = GetModuleFileName(NULL, buf, sizeof(buf) - 1);
286 if (len == 0) {
287 return;
290 buf[len] = 0;
291 p = buf + len - 1;
292 while (p != buf && *p != '\\') {
293 p--;
295 *p = 0;
296 if (access(buf, R_OK) == 0) {
297 exec_dir = g_strdup(buf);
298 } else {
299 exec_dir = CONFIG_BINDIR;
303 const char *qemu_get_exec_dir(void)
305 return exec_dir;
308 int getpagesize(void)
310 SYSTEM_INFO system_info;
312 GetSystemInfo(&system_info);
313 return system_info.dwPageSize;
316 void os_mem_prealloc(int fd, char *area, size_t memory, int smp_cpus,
317 Error **errp)
319 int i;
320 size_t pagesize = qemu_real_host_page_size();
322 memory = (memory + pagesize - 1) & -pagesize;
323 for (i = 0; i < memory / pagesize; i++) {
324 memset(area + pagesize * i, 0, 1);
328 char *qemu_get_pid_name(pid_t pid)
330 /* XXX Implement me */
331 abort();
335 pid_t qemu_fork(Error **errp)
337 errno = ENOSYS;
338 error_setg_errno(errp, errno,
339 "cannot fork child process");
340 return -1;
344 #undef connect
345 int qemu_connect_wrap(int sockfd, const struct sockaddr *addr,
346 socklen_t addrlen)
348 int ret;
349 ret = connect(sockfd, addr, addrlen);
350 if (ret < 0) {
351 if (WSAGetLastError() == WSAEWOULDBLOCK) {
352 errno = EINPROGRESS;
353 } else {
354 errno = socket_error();
357 return ret;
361 #undef listen
362 int qemu_listen_wrap(int sockfd, int backlog)
364 int ret;
365 ret = listen(sockfd, backlog);
366 if (ret < 0) {
367 errno = socket_error();
369 return ret;
373 #undef bind
374 int qemu_bind_wrap(int sockfd, const struct sockaddr *addr,
375 socklen_t addrlen)
377 int ret;
378 ret = bind(sockfd, addr, addrlen);
379 if (ret < 0) {
380 errno = socket_error();
382 return ret;
386 #undef socket
387 int qemu_socket_wrap(int domain, int type, int protocol)
389 int ret;
390 ret = socket(domain, type, protocol);
391 if (ret < 0) {
392 errno = socket_error();
394 return ret;
398 #undef accept
399 int qemu_accept_wrap(int sockfd, struct sockaddr *addr,
400 socklen_t *addrlen)
402 int ret;
403 ret = accept(sockfd, addr, addrlen);
404 if (ret < 0) {
405 errno = socket_error();
407 return ret;
411 #undef shutdown
412 int qemu_shutdown_wrap(int sockfd, int how)
414 int ret;
415 ret = shutdown(sockfd, how);
416 if (ret < 0) {
417 errno = socket_error();
419 return ret;
423 #undef ioctlsocket
424 int qemu_ioctlsocket_wrap(int fd, int req, void *val)
426 int ret;
427 ret = ioctlsocket(fd, req, val);
428 if (ret < 0) {
429 errno = socket_error();
431 return ret;
435 #undef closesocket
436 int qemu_closesocket_wrap(int fd)
438 int ret;
439 ret = closesocket(fd);
440 if (ret < 0) {
441 errno = socket_error();
443 return ret;
447 #undef getsockopt
448 int qemu_getsockopt_wrap(int sockfd, int level, int optname,
449 void *optval, socklen_t *optlen)
451 int ret;
452 ret = getsockopt(sockfd, level, optname, optval, optlen);
453 if (ret < 0) {
454 errno = socket_error();
456 return ret;
460 #undef setsockopt
461 int qemu_setsockopt_wrap(int sockfd, int level, int optname,
462 const void *optval, socklen_t optlen)
464 int ret;
465 ret = setsockopt(sockfd, level, optname, optval, optlen);
466 if (ret < 0) {
467 errno = socket_error();
469 return ret;
473 #undef getpeername
474 int qemu_getpeername_wrap(int sockfd, struct sockaddr *addr,
475 socklen_t *addrlen)
477 int ret;
478 ret = getpeername(sockfd, addr, addrlen);
479 if (ret < 0) {
480 errno = socket_error();
482 return ret;
486 #undef getsockname
487 int qemu_getsockname_wrap(int sockfd, struct sockaddr *addr,
488 socklen_t *addrlen)
490 int ret;
491 ret = getsockname(sockfd, addr, addrlen);
492 if (ret < 0) {
493 errno = socket_error();
495 return ret;
499 #undef send
500 ssize_t qemu_send_wrap(int sockfd, const void *buf, size_t len, int flags)
502 int ret;
503 ret = send(sockfd, buf, len, flags);
504 if (ret < 0) {
505 errno = socket_error();
507 return ret;
511 #undef sendto
512 ssize_t qemu_sendto_wrap(int sockfd, const void *buf, size_t len, int flags,
513 const struct sockaddr *addr, socklen_t addrlen)
515 int ret;
516 ret = sendto(sockfd, buf, len, flags, addr, addrlen);
517 if (ret < 0) {
518 errno = socket_error();
520 return ret;
524 #undef recv
525 ssize_t qemu_recv_wrap(int sockfd, void *buf, size_t len, int flags)
527 int ret;
528 ret = recv(sockfd, buf, len, flags);
529 if (ret < 0) {
530 errno = socket_error();
532 return ret;
536 #undef recvfrom
537 ssize_t qemu_recvfrom_wrap(int sockfd, void *buf, size_t len, int flags,
538 struct sockaddr *addr, socklen_t *addrlen)
540 int ret;
541 ret = recvfrom(sockfd, buf, len, flags, addr, addrlen);
542 if (ret < 0) {
543 errno = socket_error();
545 return ret;
548 bool qemu_write_pidfile(const char *filename, Error **errp)
550 char buffer[128];
551 int len;
552 HANDLE file;
553 OVERLAPPED overlap;
554 BOOL ret;
555 memset(&overlap, 0, sizeof(overlap));
557 file = CreateFile(filename, GENERIC_WRITE, FILE_SHARE_READ, NULL,
558 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
560 if (file == INVALID_HANDLE_VALUE) {
561 error_setg(errp, "Failed to create PID file");
562 return false;
564 len = snprintf(buffer, sizeof(buffer), FMT_pid "\n", (pid_t)getpid());
565 ret = WriteFile(file, (LPCVOID)buffer, (DWORD)len,
566 NULL, &overlap);
567 CloseHandle(file);
568 if (ret == 0) {
569 error_setg(errp, "Failed to write PID file");
570 return false;
572 return true;
575 size_t qemu_get_host_physmem(void)
577 MEMORYSTATUSEX statex;
578 statex.dwLength = sizeof(statex);
580 if (GlobalMemoryStatusEx(&statex)) {
581 return statex.ullTotalPhys;
583 return 0;
586 int qemu_msync(void *addr, size_t length, int fd)
589 * Perform the sync based on the file descriptor
590 * The sync range will most probably be wider than the one
591 * requested - but it will still get the job done
593 return qemu_fdatasync(fd);