vfio/container: Switch to IOMMU BE set_dirty_page_tracking/query_dirty_bitmap API
[qemu/ar7.git] / util / oslib-win32.c
blob55b0189dc30424d3223f11d0775f40b939420707
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.
29 #include "qemu/osdep.h"
30 #include <windows.h>
31 #include "qapi/error.h"
32 #include "qemu/main-loop.h"
33 #include "trace.h"
34 #include "qemu/sockets.h"
35 #include "qemu/cutils.h"
36 #include "qemu/error-report.h"
37 #include <malloc.h>
39 static int get_allocation_granularity(void)
41 SYSTEM_INFO system_info;
43 GetSystemInfo(&system_info);
44 return system_info.dwAllocationGranularity;
47 void *qemu_anon_ram_alloc(size_t size, uint64_t *align, bool shared,
48 bool noreserve)
50 void *ptr;
52 if (noreserve) {
54 * We need a MEM_COMMIT before accessing any memory in a MEM_RESERVE
55 * area; we cannot easily mimic POSIX MAP_NORESERVE semantics.
57 error_report("Skipping reservation of swap space is not supported.");
58 return NULL;
61 ptr = VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE);
62 trace_qemu_anon_ram_alloc(size, ptr);
64 if (ptr && align) {
65 *align = MAX(get_allocation_granularity(), getpagesize());
67 return ptr;
70 void qemu_anon_ram_free(void *ptr, size_t size)
72 trace_qemu_anon_ram_free(ptr, size);
73 if (ptr) {
74 VirtualFree(ptr, 0, MEM_RELEASE);
78 #ifndef _POSIX_THREAD_SAFE_FUNCTIONS
79 /* FIXME: add proper locking */
80 struct tm *gmtime_r(const time_t *timep, struct tm *result)
82 struct tm *p = gmtime(timep);
83 memset(result, 0, sizeof(*result));
84 if (p) {
85 *result = *p;
86 p = result;
88 return p;
91 /* FIXME: add proper locking */
92 struct tm *localtime_r(const time_t *timep, struct tm *result)
94 struct tm *p = localtime(timep);
95 memset(result, 0, sizeof(*result));
96 if (p) {
97 *result = *p;
98 p = result;
100 return p;
102 #endif /* _POSIX_THREAD_SAFE_FUNCTIONS */
104 static int socket_error(void)
106 switch (WSAGetLastError()) {
107 case 0:
108 return 0;
109 case WSAEINTR:
110 return EINTR;
111 case WSAEINVAL:
112 return EINVAL;
113 case WSA_INVALID_HANDLE:
114 return EBADF;
115 case WSA_NOT_ENOUGH_MEMORY:
116 return ENOMEM;
117 case WSA_INVALID_PARAMETER:
118 return EINVAL;
119 case WSAENAMETOOLONG:
120 return ENAMETOOLONG;
121 case WSAENOTEMPTY:
122 return ENOTEMPTY;
123 case WSAEWOULDBLOCK:
124 /* not using EWOULDBLOCK as we don't want code to have
125 * to check both EWOULDBLOCK and EAGAIN */
126 return EAGAIN;
127 case WSAEINPROGRESS:
128 return EINPROGRESS;
129 case WSAEALREADY:
130 return EALREADY;
131 case WSAENOTSOCK:
132 return ENOTSOCK;
133 case WSAEDESTADDRREQ:
134 return EDESTADDRREQ;
135 case WSAEMSGSIZE:
136 return EMSGSIZE;
137 case WSAEPROTOTYPE:
138 return EPROTOTYPE;
139 case WSAENOPROTOOPT:
140 return ENOPROTOOPT;
141 case WSAEPROTONOSUPPORT:
142 return EPROTONOSUPPORT;
143 case WSAEOPNOTSUPP:
144 return EOPNOTSUPP;
145 case WSAEAFNOSUPPORT:
146 return EAFNOSUPPORT;
147 case WSAEADDRINUSE:
148 return EADDRINUSE;
149 case WSAEADDRNOTAVAIL:
150 return EADDRNOTAVAIL;
151 case WSAENETDOWN:
152 return ENETDOWN;
153 case WSAENETUNREACH:
154 return ENETUNREACH;
155 case WSAENETRESET:
156 return ENETRESET;
157 case WSAECONNABORTED:
158 return ECONNABORTED;
159 case WSAECONNRESET:
160 return ECONNRESET;
161 case WSAENOBUFS:
162 return ENOBUFS;
163 case WSAEISCONN:
164 return EISCONN;
165 case WSAENOTCONN:
166 return ENOTCONN;
167 case WSAETIMEDOUT:
168 return ETIMEDOUT;
169 case WSAECONNREFUSED:
170 return ECONNREFUSED;
171 case WSAELOOP:
172 return ELOOP;
173 case WSAEHOSTUNREACH:
174 return EHOSTUNREACH;
175 default:
176 return EIO;
180 void qemu_socket_set_block(int fd)
182 unsigned long opt = 0;
183 qemu_socket_unselect(fd, NULL);
184 ioctlsocket(fd, FIONBIO, &opt);
187 int qemu_socket_try_set_nonblock(int fd)
189 unsigned long opt = 1;
190 if (ioctlsocket(fd, FIONBIO, &opt) != NO_ERROR) {
191 return -socket_error();
193 return 0;
196 void qemu_socket_set_nonblock(int fd)
198 (void)qemu_socket_try_set_nonblock(fd);
201 int socket_set_fast_reuse(int fd)
203 /* Enabling the reuse of an endpoint that was used by a socket still in
204 * TIME_WAIT state is usually performed by setting SO_REUSEADDR. On Windows
205 * fast reuse is the default and SO_REUSEADDR does strange things. So we
206 * don't have to do anything here. More info can be found at:
207 * http://msdn.microsoft.com/en-us/library/windows/desktop/ms740621.aspx */
208 return 0;
211 int inet_aton(const char *cp, struct in_addr *ia)
213 uint32_t addr = inet_addr(cp);
214 if (addr == 0xffffffff) {
215 return 0;
217 ia->s_addr = addr;
218 return 1;
221 void qemu_set_cloexec(int fd)
225 int qemu_get_thread_id(void)
227 return GetCurrentThreadId();
230 char *
231 qemu_get_local_state_dir(void)
233 const char * const *data_dirs = g_get_system_data_dirs();
235 g_assert(data_dirs && data_dirs[0]);
237 return g_strdup(data_dirs[0]);
240 void qemu_set_tty_echo(int fd, bool echo)
242 HANDLE handle = (HANDLE)_get_osfhandle(fd);
243 DWORD dwMode = 0;
245 if (handle == INVALID_HANDLE_VALUE) {
246 return;
249 GetConsoleMode(handle, &dwMode);
251 if (echo) {
252 SetConsoleMode(handle, dwMode | ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT);
253 } else {
254 SetConsoleMode(handle,
255 dwMode & ~(ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT));
259 int getpagesize(void)
261 SYSTEM_INFO system_info;
263 GetSystemInfo(&system_info);
264 return system_info.dwPageSize;
267 void qemu_prealloc_mem(int fd, char *area, size_t sz, int max_threads,
268 ThreadContext *tc, Error **errp)
270 int i;
271 size_t pagesize = qemu_real_host_page_size();
273 sz = (sz + pagesize - 1) & -pagesize;
274 for (i = 0; i < sz / pagesize; i++) {
275 memset(area + pagesize * i, 0, 1);
279 char *qemu_get_pid_name(pid_t pid)
281 /* XXX Implement me */
282 abort();
286 bool qemu_socket_select(int sockfd, WSAEVENT hEventObject,
287 long lNetworkEvents, Error **errp)
289 SOCKET s = _get_osfhandle(sockfd);
291 if (errp == NULL) {
292 errp = &error_warn;
295 if (s == INVALID_SOCKET) {
296 error_setg(errp, "invalid socket fd=%d", sockfd);
297 return false;
300 if (WSAEventSelect(s, hEventObject, lNetworkEvents) != 0) {
301 error_setg_win32(errp, WSAGetLastError(), "failed to WSAEventSelect()");
302 return false;
305 return true;
308 bool qemu_socket_unselect(int sockfd, Error **errp)
310 return qemu_socket_select(sockfd, NULL, 0, errp);
313 int qemu_socketpair(int domain, int type, int protocol, int sv[2])
315 struct sockaddr_un addr = {
318 socklen_t socklen;
319 int listener = -1;
320 int client = -1;
321 int server = -1;
322 g_autofree char *path = NULL;
323 int tmpfd;
324 u_long arg;
325 int ret = -1;
327 g_return_val_if_fail(sv != NULL, -1);
329 addr.sun_family = AF_UNIX;
330 socklen = sizeof(addr);
332 tmpfd = g_file_open_tmp(NULL, &path, NULL);
333 if (tmpfd == -1 || !path) {
334 errno = EACCES;
335 goto out;
338 close(tmpfd);
340 if (strlen(path) >= sizeof(addr.sun_path)) {
341 errno = EINVAL;
342 goto out;
345 strncpy(addr.sun_path, path, sizeof(addr.sun_path) - 1);
347 listener = socket(domain, type, protocol);
348 if (listener == -1) {
349 goto out;
352 if (DeleteFile(path) == 0 && GetLastError() != ERROR_FILE_NOT_FOUND) {
353 errno = EACCES;
354 goto out;
356 g_clear_pointer(&path, g_free);
358 if (bind(listener, (struct sockaddr *)&addr, socklen) == -1) {
359 goto out;
362 if (listen(listener, 1) == -1) {
363 goto out;
366 client = socket(domain, type, protocol);
367 if (client == -1) {
368 goto out;
371 arg = 1;
372 if (ioctlsocket(client, FIONBIO, &arg) != NO_ERROR) {
373 goto out;
376 if (connect(client, (struct sockaddr *)&addr, socklen) == -1 &&
377 WSAGetLastError() != WSAEWOULDBLOCK) {
378 goto out;
381 server = accept(listener, NULL, NULL);
382 if (server == -1) {
383 goto out;
386 arg = 0;
387 if (ioctlsocket(client, FIONBIO, &arg) != NO_ERROR) {
388 goto out;
391 arg = 0;
392 if (ioctlsocket(client, SIO_AF_UNIX_GETPEERPID, &arg) != NO_ERROR) {
393 goto out;
396 if (arg != GetCurrentProcessId()) {
397 errno = EPERM;
398 goto out;
401 sv[0] = server;
402 server = -1;
403 sv[1] = client;
404 client = -1;
405 ret = 0;
407 out:
408 if (listener != -1) {
409 close(listener);
411 if (client != -1) {
412 close(client);
414 if (server != -1) {
415 close(server);
417 if (path) {
418 DeleteFile(path);
420 return ret;
423 #undef connect
424 int qemu_connect_wrap(int sockfd, const struct sockaddr *addr,
425 socklen_t addrlen)
427 int ret;
428 SOCKET s = _get_osfhandle(sockfd);
430 if (s == INVALID_SOCKET) {
431 return -1;
434 ret = connect(s, addr, addrlen);
435 if (ret < 0) {
436 if (WSAGetLastError() == WSAEWOULDBLOCK) {
437 errno = EINPROGRESS;
438 } else {
439 errno = socket_error();
442 return ret;
446 #undef listen
447 int qemu_listen_wrap(int sockfd, int backlog)
449 int ret;
450 SOCKET s = _get_osfhandle(sockfd);
452 if (s == INVALID_SOCKET) {
453 return -1;
456 ret = listen(s, backlog);
457 if (ret < 0) {
458 errno = socket_error();
460 return ret;
464 #undef bind
465 int qemu_bind_wrap(int sockfd, const struct sockaddr *addr,
466 socklen_t addrlen)
468 int ret;
469 SOCKET s = _get_osfhandle(sockfd);
471 if (s == INVALID_SOCKET) {
472 return -1;
475 ret = bind(s, addr, addrlen);
476 if (ret < 0) {
477 errno = socket_error();
479 return ret;
482 QEMU_USED EXCEPTION_DISPOSITION
483 win32_close_exception_handler(struct _EXCEPTION_RECORD *exception_record,
484 void *registration, struct _CONTEXT *context,
485 void *dispatcher)
487 return EXCEPTION_EXECUTE_HANDLER;
490 #undef close
491 int qemu_close_socket_osfhandle(int fd)
493 SOCKET s = _get_osfhandle(fd);
494 DWORD flags = 0;
497 * If we were to just call _close on the descriptor, it would close the
498 * HANDLE, but it wouldn't free any of the resources associated to the
499 * SOCKET, and we can't call _close after calling closesocket, because
500 * closesocket has already closed the HANDLE, and _close would attempt to
501 * close the HANDLE again, resulting in a double free. We can however
502 * protect the HANDLE from actually being closed long enough to close the
503 * file descriptor, then close the socket itself.
505 if (!GetHandleInformation((HANDLE)s, &flags)) {
506 errno = EACCES;
507 return -1;
510 if (!SetHandleInformation((HANDLE)s, HANDLE_FLAG_PROTECT_FROM_CLOSE, HANDLE_FLAG_PROTECT_FROM_CLOSE)) {
511 errno = EACCES;
512 return -1;
515 __try1(win32_close_exception_handler) {
517 * close() returns EBADF since we PROTECT_FROM_CLOSE the underlying
518 * handle, but the FD is actually freed
520 if (close(fd) < 0 && errno != EBADF) {
521 return -1;
524 __except1 {
527 if (!SetHandleInformation((HANDLE)s, flags, flags)) {
528 errno = EACCES;
529 return -1;
532 return 0;
535 int qemu_close_wrap(int fd)
537 SOCKET s = INVALID_SOCKET;
538 int ret = -1;
540 if (!fd_is_socket(fd)) {
541 return close(fd);
544 s = _get_osfhandle(fd);
545 qemu_close_socket_osfhandle(fd);
547 ret = closesocket(s);
548 if (ret < 0) {
549 errno = socket_error();
552 return ret;
556 #undef socket
557 int qemu_socket_wrap(int domain, int type, int protocol)
559 SOCKET s;
560 int fd;
562 s = socket(domain, type, protocol);
563 if (s == -1) {
564 errno = socket_error();
565 return -1;
568 fd = _open_osfhandle(s, _O_BINARY);
569 if (fd < 0) {
570 closesocket(s);
571 /* _open_osfhandle may not set errno, and closesocket() may override it */
572 errno = ENOMEM;
575 return fd;
579 #undef accept
580 int qemu_accept_wrap(int sockfd, struct sockaddr *addr,
581 socklen_t *addrlen)
583 int fd;
584 SOCKET s = _get_osfhandle(sockfd);
586 if (s == INVALID_SOCKET) {
587 return -1;
590 s = accept(s, addr, addrlen);
591 if (s == -1) {
592 errno = socket_error();
593 return -1;
596 fd = _open_osfhandle(s, _O_BINARY);
597 if (fd < 0) {
598 closesocket(s);
599 /* _open_osfhandle may not set errno, and closesocket() may override it */
600 errno = ENOMEM;
603 return fd;
607 #undef shutdown
608 int qemu_shutdown_wrap(int sockfd, int how)
610 int ret;
611 SOCKET s = _get_osfhandle(sockfd);
613 if (s == INVALID_SOCKET) {
614 return -1;
617 ret = shutdown(s, how);
618 if (ret < 0) {
619 errno = socket_error();
621 return ret;
625 #undef ioctlsocket
626 int qemu_ioctlsocket_wrap(int fd, int req, void *val)
628 int ret;
629 SOCKET s = _get_osfhandle(fd);
631 if (s == INVALID_SOCKET) {
632 return -1;
635 ret = ioctlsocket(s, req, val);
636 if (ret < 0) {
637 errno = socket_error();
639 return ret;
643 #undef getsockopt
644 int qemu_getsockopt_wrap(int sockfd, int level, int optname,
645 void *optval, socklen_t *optlen)
647 int ret;
648 SOCKET s = _get_osfhandle(sockfd);
650 if (s == INVALID_SOCKET) {
651 return -1;
654 ret = getsockopt(s, level, optname, optval, optlen);
655 if (ret < 0) {
656 errno = socket_error();
658 return ret;
662 #undef setsockopt
663 int qemu_setsockopt_wrap(int sockfd, int level, int optname,
664 const void *optval, socklen_t optlen)
666 int ret;
667 SOCKET s = _get_osfhandle(sockfd);
669 if (s == INVALID_SOCKET) {
670 return -1;
673 ret = setsockopt(s, level, optname, optval, optlen);
674 if (ret < 0) {
675 errno = socket_error();
677 return ret;
681 #undef getpeername
682 int qemu_getpeername_wrap(int sockfd, struct sockaddr *addr,
683 socklen_t *addrlen)
685 int ret;
686 SOCKET s = _get_osfhandle(sockfd);
688 if (s == INVALID_SOCKET) {
689 return -1;
692 ret = getpeername(s, addr, addrlen);
693 if (ret < 0) {
694 errno = socket_error();
696 return ret;
700 #undef getsockname
701 int qemu_getsockname_wrap(int sockfd, struct sockaddr *addr,
702 socklen_t *addrlen)
704 int ret;
705 SOCKET s = _get_osfhandle(sockfd);
707 if (s == INVALID_SOCKET) {
708 return -1;
711 ret = getsockname(s, addr, addrlen);
712 if (ret < 0) {
713 errno = socket_error();
715 return ret;
719 #undef send
720 ssize_t qemu_send_wrap(int sockfd, const void *buf, size_t len, int flags)
722 int ret;
723 SOCKET s = _get_osfhandle(sockfd);
725 if (s == INVALID_SOCKET) {
726 return -1;
729 ret = send(s, buf, len, flags);
730 if (ret < 0) {
731 errno = socket_error();
733 return ret;
737 #undef sendto
738 ssize_t qemu_sendto_wrap(int sockfd, const void *buf, size_t len, int flags,
739 const struct sockaddr *addr, socklen_t addrlen)
741 int ret;
742 SOCKET s = _get_osfhandle(sockfd);
744 if (s == INVALID_SOCKET) {
745 return -1;
748 ret = sendto(s, buf, len, flags, addr, addrlen);
749 if (ret < 0) {
750 errno = socket_error();
752 return ret;
756 #undef recv
757 ssize_t qemu_recv_wrap(int sockfd, void *buf, size_t len, int flags)
759 int ret;
760 SOCKET s = _get_osfhandle(sockfd);
762 if (s == INVALID_SOCKET) {
763 return -1;
766 ret = recv(s, buf, len, flags);
767 if (ret < 0) {
768 errno = socket_error();
770 return ret;
774 #undef recvfrom
775 ssize_t qemu_recvfrom_wrap(int sockfd, void *buf, size_t len, int flags,
776 struct sockaddr *addr, socklen_t *addrlen)
778 int ret;
779 SOCKET s = _get_osfhandle(sockfd);
781 if (s == INVALID_SOCKET) {
782 return -1;
785 ret = recvfrom(s, buf, len, flags, addr, addrlen);
786 if (ret < 0) {
787 errno = socket_error();
789 return ret;
792 bool qemu_write_pidfile(const char *filename, Error **errp)
794 char buffer[128];
795 int len;
796 HANDLE file;
797 OVERLAPPED overlap;
798 BOOL ret;
799 memset(&overlap, 0, sizeof(overlap));
801 file = CreateFile(filename, GENERIC_WRITE, FILE_SHARE_READ, NULL,
802 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
804 if (file == INVALID_HANDLE_VALUE) {
805 error_setg(errp, "Failed to create PID file");
806 return false;
808 len = snprintf(buffer, sizeof(buffer), FMT_pid "\n", (pid_t)getpid());
809 ret = WriteFile(file, (LPCVOID)buffer, (DWORD)len,
810 NULL, &overlap);
811 CloseHandle(file);
812 if (ret == 0) {
813 error_setg(errp, "Failed to write PID file");
814 return false;
816 return true;
819 size_t qemu_get_host_physmem(void)
821 MEMORYSTATUSEX statex;
822 statex.dwLength = sizeof(statex);
824 if (GlobalMemoryStatusEx(&statex)) {
825 return statex.ullTotalPhys;
827 return 0;
830 int qemu_msync(void *addr, size_t length, int fd)
833 * Perform the sync based on the file descriptor
834 * The sync range will most probably be wider than the one
835 * requested - but it will still get the job done
837 return qemu_fdatasync(fd);
840 void *qemu_win32_map_alloc(size_t size, HANDLE *h, Error **errp)
842 void *bits;
844 trace_win32_map_alloc(size);
846 *h = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0,
847 size, NULL);
848 if (*h == NULL) {
849 error_setg_win32(errp, GetLastError(), "Failed to CreateFileMapping");
850 return NULL;
853 bits = MapViewOfFile(*h, FILE_MAP_ALL_ACCESS, 0, 0, size);
854 if (bits == NULL) {
855 error_setg_win32(errp, GetLastError(), "Failed to MapViewOfFile");
856 CloseHandle(*h);
857 return NULL;
860 return bits;
863 void qemu_win32_map_free(void *ptr, HANDLE h, Error **errp)
865 trace_win32_map_free(ptr, h);
867 if (UnmapViewOfFile(ptr) == 0) {
868 error_setg_win32(errp, GetLastError(), "Failed to UnmapViewOfFile");
870 CloseHandle(h);