xref: /openbmc/qemu/util/oslib-win32.c (revision 861bbc88)
1 /*
2  * os-win32.c
3  *
4  * Copyright (c) 2003-2008 Fabrice Bellard
5  * Copyright (c) 2010-2016 Red Hat, Inc.
6  *
7  * QEMU library functions for win32 which are shared between QEMU and
8  * the QEMU tools.
9  *
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:
16  *
17  * The above copyright notice and this permission notice shall be included in
18  * all copies or substantial portions of the Software.
19  *
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.
27  */
28 
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>
38 
39 static int get_allocation_granularity(void)
40 {
41     SYSTEM_INFO system_info;
42 
43     GetSystemInfo(&system_info);
44     return system_info.dwAllocationGranularity;
45 }
46 
47 void *qemu_anon_ram_alloc(size_t size, uint64_t *align, bool shared,
48                           bool noreserve)
49 {
50     void *ptr;
51 
52     if (noreserve) {
53         /*
54          * We need a MEM_COMMIT before accessing any memory in a MEM_RESERVE
55          * area; we cannot easily mimic POSIX MAP_NORESERVE semantics.
56          */
57         error_report("Skipping reservation of swap space is not supported.");
58         return NULL;
59     }
60 
61     ptr = VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE);
62     trace_qemu_anon_ram_alloc(size, ptr);
63 
64     if (ptr && align) {
65         *align = MAX(get_allocation_granularity(), getpagesize());
66     }
67     return ptr;
68 }
69 
70 void qemu_anon_ram_free(void *ptr, size_t size)
71 {
72     trace_qemu_anon_ram_free(ptr, size);
73     if (ptr) {
74         VirtualFree(ptr, 0, MEM_RELEASE);
75     }
76 }
77 
78 #ifndef _POSIX_THREAD_SAFE_FUNCTIONS
79 /* FIXME: add proper locking */
80 struct tm *gmtime_r(const time_t *timep, struct tm *result)
81 {
82     struct tm *p = gmtime(timep);
83     memset(result, 0, sizeof(*result));
84     if (p) {
85         *result = *p;
86         p = result;
87     }
88     return p;
89 }
90 
91 /* FIXME: add proper locking */
92 struct tm *localtime_r(const time_t *timep, struct tm *result)
93 {
94     struct tm *p = localtime(timep);
95     memset(result, 0, sizeof(*result));
96     if (p) {
97         *result = *p;
98         p = result;
99     }
100     return p;
101 }
102 #endif /* _POSIX_THREAD_SAFE_FUNCTIONS */
103 
104 static int socket_error(void)
105 {
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;
177     }
178 }
179 
180 void qemu_socket_set_block(int fd)
181 {
182     unsigned long opt = 0;
183     qemu_socket_unselect(fd, NULL);
184     ioctlsocket(fd, FIONBIO, &opt);
185 }
186 
187 int qemu_socket_try_set_nonblock(int fd)
188 {
189     unsigned long opt = 1;
190     if (ioctlsocket(fd, FIONBIO, &opt) != NO_ERROR) {
191         return -socket_error();
192     }
193     return 0;
194 }
195 
196 void qemu_socket_set_nonblock(int fd)
197 {
198     (void)qemu_socket_try_set_nonblock(fd);
199 }
200 
201 int socket_set_fast_reuse(int fd)
202 {
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;
209 }
210 
211 int inet_aton(const char *cp, struct in_addr *ia)
212 {
213     uint32_t addr = inet_addr(cp);
214     if (addr == 0xffffffff) {
215         return 0;
216     }
217     ia->s_addr = addr;
218     return 1;
219 }
220 
221 void qemu_set_cloexec(int fd)
222 {
223 }
224 
225 int qemu_get_thread_id(void)
226 {
227     return GetCurrentThreadId();
228 }
229 
230 char *
231 qemu_get_local_state_dir(void)
232 {
233     const char * const *data_dirs = g_get_system_data_dirs();
234 
235     g_assert(data_dirs && data_dirs[0]);
236 
237     return g_strdup(data_dirs[0]);
238 }
239 
240 void qemu_set_tty_echo(int fd, bool echo)
241 {
242     HANDLE handle = (HANDLE)_get_osfhandle(fd);
243     DWORD dwMode = 0;
244 
245     if (handle == INVALID_HANDLE_VALUE) {
246         return;
247     }
248 
249     GetConsoleMode(handle, &dwMode);
250 
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));
256     }
257 }
258 
259 int getpagesize(void)
260 {
261     SYSTEM_INFO system_info;
262 
263     GetSystemInfo(&system_info);
264     return system_info.dwPageSize;
265 }
266 
267 bool qemu_prealloc_mem(int fd, char *area, size_t sz, int max_threads,
268                        ThreadContext *tc, Error **errp)
269 {
270     int i;
271     size_t pagesize = qemu_real_host_page_size();
272 
273     sz = (sz + pagesize - 1) & -pagesize;
274     for (i = 0; i < sz / pagesize; i++) {
275         memset(area + pagesize * i, 0, 1);
276     }
277 
278     return true;
279 }
280 
281 char *qemu_get_pid_name(pid_t pid)
282 {
283     /* XXX Implement me */
284     abort();
285 }
286 
287 
288 bool qemu_socket_select(int sockfd, WSAEVENT hEventObject,
289                         long lNetworkEvents, Error **errp)
290 {
291     SOCKET s = _get_osfhandle(sockfd);
292 
293     if (errp == NULL) {
294         errp = &error_warn;
295     }
296 
297     if (s == INVALID_SOCKET) {
298         error_setg(errp, "invalid socket fd=%d", sockfd);
299         return false;
300     }
301 
302     if (WSAEventSelect(s, hEventObject, lNetworkEvents) != 0) {
303         error_setg_win32(errp, WSAGetLastError(), "failed to WSAEventSelect()");
304         return false;
305     }
306 
307     return true;
308 }
309 
310 bool qemu_socket_unselect(int sockfd, Error **errp)
311 {
312     return qemu_socket_select(sockfd, NULL, 0, errp);
313 }
314 
315 int qemu_socketpair(int domain, int type, int protocol, int sv[2])
316 {
317     struct sockaddr_un addr = {
318         0,
319     };
320     socklen_t socklen;
321     int listener = -1;
322     int client = -1;
323     int server = -1;
324     g_autofree char *path = NULL;
325     int tmpfd;
326     u_long arg;
327     int ret = -1;
328 
329     g_return_val_if_fail(sv != NULL, -1);
330 
331     addr.sun_family = AF_UNIX;
332     socklen = sizeof(addr);
333 
334     tmpfd = g_file_open_tmp(NULL, &path, NULL);
335     if (tmpfd == -1 || !path) {
336         errno = EACCES;
337         goto out;
338     }
339 
340     close(tmpfd);
341 
342     if (strlen(path) >= sizeof(addr.sun_path)) {
343         errno = EINVAL;
344         goto out;
345     }
346 
347     strncpy(addr.sun_path, path, sizeof(addr.sun_path) - 1);
348 
349     listener = socket(domain, type, protocol);
350     if (listener == -1) {
351         goto out;
352     }
353 
354     if (DeleteFile(path) == 0 && GetLastError() != ERROR_FILE_NOT_FOUND) {
355         errno = EACCES;
356         goto out;
357     }
358     g_clear_pointer(&path, g_free);
359 
360     if (bind(listener, (struct sockaddr *)&addr, socklen) == -1) {
361         goto out;
362     }
363 
364     if (listen(listener, 1) == -1) {
365         goto out;
366     }
367 
368     client = socket(domain, type, protocol);
369     if (client == -1) {
370         goto out;
371     }
372 
373     arg = 1;
374     if (ioctlsocket(client, FIONBIO, &arg) != NO_ERROR) {
375         goto out;
376     }
377 
378     if (connect(client, (struct sockaddr *)&addr, socklen) == -1 &&
379         WSAGetLastError() != WSAEWOULDBLOCK) {
380         goto out;
381     }
382 
383     server = accept(listener, NULL, NULL);
384     if (server == -1) {
385         goto out;
386     }
387 
388     arg = 0;
389     if (ioctlsocket(client, FIONBIO, &arg) != NO_ERROR) {
390         goto out;
391     }
392 
393     arg = 0;
394     if (ioctlsocket(client, SIO_AF_UNIX_GETPEERPID, &arg) != NO_ERROR) {
395         goto out;
396     }
397 
398     if (arg != GetCurrentProcessId()) {
399         errno = EPERM;
400         goto out;
401     }
402 
403     sv[0] = server;
404     server = -1;
405     sv[1] = client;
406     client = -1;
407     ret = 0;
408 
409 out:
410     if (listener != -1) {
411         close(listener);
412     }
413     if (client != -1) {
414         close(client);
415     }
416     if (server != -1) {
417         close(server);
418     }
419     if (path) {
420         DeleteFile(path);
421     }
422     return ret;
423 }
424 
425 #undef connect
426 int qemu_connect_wrap(int sockfd, const struct sockaddr *addr,
427                       socklen_t addrlen)
428 {
429     int ret;
430     SOCKET s = _get_osfhandle(sockfd);
431 
432     if (s == INVALID_SOCKET) {
433         return -1;
434     }
435 
436     ret = connect(s, addr, addrlen);
437     if (ret < 0) {
438         if (WSAGetLastError() == WSAEWOULDBLOCK) {
439             errno = EINPROGRESS;
440         } else {
441             errno = socket_error();
442         }
443     }
444     return ret;
445 }
446 
447 
448 #undef listen
449 int qemu_listen_wrap(int sockfd, int backlog)
450 {
451     int ret;
452     SOCKET s = _get_osfhandle(sockfd);
453 
454     if (s == INVALID_SOCKET) {
455         return -1;
456     }
457 
458     ret = listen(s, backlog);
459     if (ret < 0) {
460         errno = socket_error();
461     }
462     return ret;
463 }
464 
465 
466 #undef bind
467 int qemu_bind_wrap(int sockfd, const struct sockaddr *addr,
468                    socklen_t addrlen)
469 {
470     int ret;
471     SOCKET s = _get_osfhandle(sockfd);
472 
473     if (s == INVALID_SOCKET) {
474         return -1;
475     }
476 
477     ret = bind(s, addr, addrlen);
478     if (ret < 0) {
479         errno = socket_error();
480     }
481     return ret;
482 }
483 
484 QEMU_USED EXCEPTION_DISPOSITION
485 win32_close_exception_handler(struct _EXCEPTION_RECORD *exception_record,
486                               void *registration, struct _CONTEXT *context,
487                               void *dispatcher)
488 {
489     return EXCEPTION_EXECUTE_HANDLER;
490 }
491 
492 #undef close
493 int qemu_close_socket_osfhandle(int fd)
494 {
495     SOCKET s = _get_osfhandle(fd);
496     DWORD flags = 0;
497 
498     /*
499      * If we were to just call _close on the descriptor, it would close the
500      * HANDLE, but it wouldn't free any of the resources associated to the
501      * SOCKET, and we can't call _close after calling closesocket, because
502      * closesocket has already closed the HANDLE, and _close would attempt to
503      * close the HANDLE again, resulting in a double free. We can however
504      * protect the HANDLE from actually being closed long enough to close the
505      * file descriptor, then close the socket itself.
506      */
507     if (!GetHandleInformation((HANDLE)s, &flags)) {
508         errno = EACCES;
509         return -1;
510     }
511 
512     if (!SetHandleInformation((HANDLE)s, HANDLE_FLAG_PROTECT_FROM_CLOSE, HANDLE_FLAG_PROTECT_FROM_CLOSE)) {
513         errno = EACCES;
514         return -1;
515     }
516 
517     __try1(win32_close_exception_handler) {
518         /*
519          * close() returns EBADF since we PROTECT_FROM_CLOSE the underlying
520          * handle, but the FD is actually freed
521          */
522         if (close(fd) < 0 && errno != EBADF) {
523             return -1;
524         }
525     }
526     __except1 {
527     }
528 
529     if (!SetHandleInformation((HANDLE)s, flags, flags)) {
530         errno = EACCES;
531         return -1;
532     }
533 
534     return 0;
535 }
536 
537 int qemu_close_wrap(int fd)
538 {
539     SOCKET s = INVALID_SOCKET;
540     int ret = -1;
541 
542     if (!fd_is_socket(fd)) {
543         return close(fd);
544     }
545 
546     s = _get_osfhandle(fd);
547     qemu_close_socket_osfhandle(fd);
548 
549     ret = closesocket(s);
550     if (ret < 0) {
551         errno = socket_error();
552     }
553 
554     return ret;
555 }
556 
557 
558 #undef socket
559 int qemu_socket_wrap(int domain, int type, int protocol)
560 {
561     SOCKET s;
562     int fd;
563 
564     s = socket(domain, type, protocol);
565     if (s == -1) {
566         errno = socket_error();
567         return -1;
568     }
569 
570     fd = _open_osfhandle(s, _O_BINARY);
571     if (fd < 0) {
572         closesocket(s);
573         /* _open_osfhandle may not set errno, and closesocket() may override it */
574         errno = ENOMEM;
575     }
576 
577     return fd;
578 }
579 
580 
581 #undef accept
582 int qemu_accept_wrap(int sockfd, struct sockaddr *addr,
583                      socklen_t *addrlen)
584 {
585     int fd;
586     SOCKET s = _get_osfhandle(sockfd);
587 
588     if (s == INVALID_SOCKET) {
589         return -1;
590     }
591 
592     s = accept(s, addr, addrlen);
593     if (s == -1) {
594         errno = socket_error();
595         return -1;
596     }
597 
598     fd = _open_osfhandle(s, _O_BINARY);
599     if (fd < 0) {
600         closesocket(s);
601         /* _open_osfhandle may not set errno, and closesocket() may override it */
602         errno = ENOMEM;
603     }
604 
605     return fd;
606 }
607 
608 
609 #undef shutdown
610 int qemu_shutdown_wrap(int sockfd, int how)
611 {
612     int ret;
613     SOCKET s = _get_osfhandle(sockfd);
614 
615     if (s == INVALID_SOCKET) {
616         return -1;
617     }
618 
619     ret = shutdown(s, how);
620     if (ret < 0) {
621         errno = socket_error();
622     }
623     return ret;
624 }
625 
626 
627 #undef ioctlsocket
628 int qemu_ioctlsocket_wrap(int fd, int req, void *val)
629 {
630     int ret;
631     SOCKET s = _get_osfhandle(fd);
632 
633     if (s == INVALID_SOCKET) {
634         return -1;
635     }
636 
637     ret = ioctlsocket(s, req, val);
638     if (ret < 0) {
639         errno = socket_error();
640     }
641     return ret;
642 }
643 
644 
645 #undef getsockopt
646 int qemu_getsockopt_wrap(int sockfd, int level, int optname,
647                          void *optval, socklen_t *optlen)
648 {
649     int ret;
650     SOCKET s = _get_osfhandle(sockfd);
651 
652     if (s == INVALID_SOCKET) {
653         return -1;
654     }
655 
656     ret = getsockopt(s, level, optname, optval, optlen);
657     if (ret < 0) {
658         errno = socket_error();
659     }
660     return ret;
661 }
662 
663 
664 #undef setsockopt
665 int qemu_setsockopt_wrap(int sockfd, int level, int optname,
666                          const void *optval, socklen_t optlen)
667 {
668     int ret;
669     SOCKET s = _get_osfhandle(sockfd);
670 
671     if (s == INVALID_SOCKET) {
672         return -1;
673     }
674 
675     ret = setsockopt(s, level, optname, optval, optlen);
676     if (ret < 0) {
677         errno = socket_error();
678     }
679     return ret;
680 }
681 
682 
683 #undef getpeername
684 int qemu_getpeername_wrap(int sockfd, struct sockaddr *addr,
685                           socklen_t *addrlen)
686 {
687     int ret;
688     SOCKET s = _get_osfhandle(sockfd);
689 
690     if (s == INVALID_SOCKET) {
691         return -1;
692     }
693 
694     ret = getpeername(s, addr, addrlen);
695     if (ret < 0) {
696         errno = socket_error();
697     }
698     return ret;
699 }
700 
701 
702 #undef getsockname
703 int qemu_getsockname_wrap(int sockfd, struct sockaddr *addr,
704                           socklen_t *addrlen)
705 {
706     int ret;
707     SOCKET s = _get_osfhandle(sockfd);
708 
709     if (s == INVALID_SOCKET) {
710         return -1;
711     }
712 
713     ret = getsockname(s, addr, addrlen);
714     if (ret < 0) {
715         errno = socket_error();
716     }
717     return ret;
718 }
719 
720 
721 #undef send
722 ssize_t qemu_send_wrap(int sockfd, const void *buf, size_t len, int flags)
723 {
724     int ret;
725     SOCKET s = _get_osfhandle(sockfd);
726 
727     if (s == INVALID_SOCKET) {
728         return -1;
729     }
730 
731     ret = send(s, buf, len, flags);
732     if (ret < 0) {
733         errno = socket_error();
734     }
735     return ret;
736 }
737 
738 
739 #undef sendto
740 ssize_t qemu_sendto_wrap(int sockfd, const void *buf, size_t len, int flags,
741                          const struct sockaddr *addr, socklen_t addrlen)
742 {
743     int ret;
744     SOCKET s = _get_osfhandle(sockfd);
745 
746     if (s == INVALID_SOCKET) {
747         return -1;
748     }
749 
750     ret = sendto(s, buf, len, flags, addr, addrlen);
751     if (ret < 0) {
752         errno = socket_error();
753     }
754     return ret;
755 }
756 
757 
758 #undef recv
759 ssize_t qemu_recv_wrap(int sockfd, void *buf, size_t len, int flags)
760 {
761     int ret;
762     SOCKET s = _get_osfhandle(sockfd);
763 
764     if (s == INVALID_SOCKET) {
765         return -1;
766     }
767 
768     ret = recv(s, buf, len, flags);
769     if (ret < 0) {
770         errno = socket_error();
771     }
772     return ret;
773 }
774 
775 
776 #undef recvfrom
777 ssize_t qemu_recvfrom_wrap(int sockfd, void *buf, size_t len, int flags,
778                            struct sockaddr *addr, socklen_t *addrlen)
779 {
780     int ret;
781     SOCKET s = _get_osfhandle(sockfd);
782 
783     if (s == INVALID_SOCKET) {
784         return -1;
785     }
786 
787     ret = recvfrom(s, buf, len, flags, addr, addrlen);
788     if (ret < 0) {
789         errno = socket_error();
790     }
791     return ret;
792 }
793 
794 bool qemu_write_pidfile(const char *filename, Error **errp)
795 {
796     char buffer[128];
797     int len;
798     HANDLE file;
799     OVERLAPPED overlap;
800     BOOL ret;
801     memset(&overlap, 0, sizeof(overlap));
802 
803     file = CreateFile(filename, GENERIC_WRITE, FILE_SHARE_READ, NULL,
804                       OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
805 
806     if (file == INVALID_HANDLE_VALUE) {
807         error_setg(errp, "Failed to create PID file");
808         return false;
809     }
810     len = snprintf(buffer, sizeof(buffer), FMT_pid "\n", (pid_t)getpid());
811     ret = WriteFile(file, (LPCVOID)buffer, (DWORD)len,
812                     NULL, &overlap);
813     CloseHandle(file);
814     if (ret == 0) {
815         error_setg(errp, "Failed to write PID file");
816         return false;
817     }
818     return true;
819 }
820 
821 size_t qemu_get_host_physmem(void)
822 {
823     MEMORYSTATUSEX statex;
824     statex.dwLength = sizeof(statex);
825 
826     if (GlobalMemoryStatusEx(&statex)) {
827         return statex.ullTotalPhys;
828     }
829     return 0;
830 }
831 
832 int qemu_msync(void *addr, size_t length, int fd)
833 {
834     /**
835      * Perform the sync based on the file descriptor
836      * The sync range will most probably be wider than the one
837      * requested - but it will still get the job done
838      */
839     return qemu_fdatasync(fd);
840 }
841 
842 void *qemu_win32_map_alloc(size_t size, HANDLE *h, Error **errp)
843 {
844     void *bits;
845 
846     trace_win32_map_alloc(size);
847 
848     *h = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0,
849                           size, NULL);
850     if (*h == NULL) {
851         error_setg_win32(errp, GetLastError(), "Failed to CreateFileMapping");
852         return NULL;
853     }
854 
855     bits = MapViewOfFile(*h, FILE_MAP_ALL_ACCESS, 0, 0, size);
856     if (bits == NULL) {
857         error_setg_win32(errp, GetLastError(), "Failed to MapViewOfFile");
858         CloseHandle(*h);
859         return NULL;
860     }
861 
862     return bits;
863 }
864 
865 void qemu_win32_map_free(void *ptr, HANDLE h, Error **errp)
866 {
867     trace_win32_map_free(ptr, h);
868 
869     if (UnmapViewOfFile(ptr) == 0) {
870         error_setg_win32(errp, GetLastError(), "Failed to UnmapViewOfFile");
871     }
872     CloseHandle(h);
873 }
874