1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33#include "qemu/osdep.h"
34#include <windows.h>
35#include "qemu-common.h"
36#include "qapi/error.h"
37#include "sysemu/sysemu.h"
38#include "qemu/main-loop.h"
39#include "trace.h"
40#include "qemu/sockets.h"
41#include "qemu/cutils.h"
42
43
44#include <shlobj.h>
45
46void *qemu_oom_check(void *ptr)
47{
48 if (ptr == NULL) {
49 fprintf(stderr, "Failed to allocate memory: %lu\n", GetLastError());
50 abort();
51 }
52 return ptr;
53}
54
55void *qemu_try_memalign(size_t alignment, size_t size)
56{
57 void *ptr;
58
59 if (!size) {
60 abort();
61 }
62 ptr = VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE);
63 trace_qemu_memalign(alignment, size, ptr);
64 return ptr;
65}
66
67void *qemu_memalign(size_t alignment, size_t size)
68{
69 return qemu_oom_check(qemu_try_memalign(alignment, size));
70}
71
72static int get_allocation_granularity(void)
73{
74 SYSTEM_INFO system_info;
75
76 GetSystemInfo(&system_info);
77 return system_info.dwAllocationGranularity;
78}
79
80void *qemu_anon_ram_alloc(size_t size, uint64_t *align, bool shared)
81{
82 void *ptr;
83
84 ptr = VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE);
85 trace_qemu_anon_ram_alloc(size, ptr);
86
87 if (ptr && align) {
88 *align = MAX(get_allocation_granularity(), getpagesize());
89 }
90 return ptr;
91}
92
93void qemu_vfree(void *ptr)
94{
95 trace_qemu_vfree(ptr);
96 if (ptr) {
97 VirtualFree(ptr, 0, MEM_RELEASE);
98 }
99}
100
101void qemu_anon_ram_free(void *ptr, size_t size)
102{
103 trace_qemu_anon_ram_free(ptr, size);
104 if (ptr) {
105 VirtualFree(ptr, 0, MEM_RELEASE);
106 }
107}
108
109#ifndef CONFIG_LOCALTIME_R
110
111struct tm *gmtime_r(const time_t *timep, struct tm *result)
112{
113 struct tm *p = gmtime(timep);
114 memset(result, 0, sizeof(*result));
115 if (p) {
116 *result = *p;
117 p = result;
118 }
119 return p;
120}
121
122
123struct tm *localtime_r(const time_t *timep, struct tm *result)
124{
125 struct tm *p = localtime(timep);
126 memset(result, 0, sizeof(*result));
127 if (p) {
128 *result = *p;
129 p = result;
130 }
131 return p;
132}
133#endif
134
135void qemu_set_block(int fd)
136{
137 unsigned long opt = 0;
138 WSAEventSelect(fd, NULL, 0);
139 ioctlsocket(fd, FIONBIO, &opt);
140}
141
142void qemu_set_nonblock(int fd)
143{
144 unsigned long opt = 1;
145 ioctlsocket(fd, FIONBIO, &opt);
146 qemu_fd_register(fd);
147}
148
149int socket_set_fast_reuse(int fd)
150{
151
152
153
154
155
156 return 0;
157}
158
159
160static int socket_error(void)
161{
162 switch (WSAGetLastError()) {
163 case 0:
164 return 0;
165 case WSAEINTR:
166 return EINTR;
167 case WSAEINVAL:
168 return EINVAL;
169 case WSA_INVALID_HANDLE:
170 return EBADF;
171 case WSA_NOT_ENOUGH_MEMORY:
172 return ENOMEM;
173 case WSA_INVALID_PARAMETER:
174 return EINVAL;
175 case WSAENAMETOOLONG:
176 return ENAMETOOLONG;
177 case WSAENOTEMPTY:
178 return ENOTEMPTY;
179 case WSAEWOULDBLOCK:
180
181
182 return EAGAIN;
183 case WSAEINPROGRESS:
184 return EINPROGRESS;
185 case WSAEALREADY:
186 return EALREADY;
187 case WSAENOTSOCK:
188 return ENOTSOCK;
189 case WSAEDESTADDRREQ:
190 return EDESTADDRREQ;
191 case WSAEMSGSIZE:
192 return EMSGSIZE;
193 case WSAEPROTOTYPE:
194 return EPROTOTYPE;
195 case WSAENOPROTOOPT:
196 return ENOPROTOOPT;
197 case WSAEPROTONOSUPPORT:
198 return EPROTONOSUPPORT;
199 case WSAEOPNOTSUPP:
200 return EOPNOTSUPP;
201 case WSAEAFNOSUPPORT:
202 return EAFNOSUPPORT;
203 case WSAEADDRINUSE:
204 return EADDRINUSE;
205 case WSAEADDRNOTAVAIL:
206 return EADDRNOTAVAIL;
207 case WSAENETDOWN:
208 return ENETDOWN;
209 case WSAENETUNREACH:
210 return ENETUNREACH;
211 case WSAENETRESET:
212 return ENETRESET;
213 case WSAECONNABORTED:
214 return ECONNABORTED;
215 case WSAECONNRESET:
216 return ECONNRESET;
217 case WSAENOBUFS:
218 return ENOBUFS;
219 case WSAEISCONN:
220 return EISCONN;
221 case WSAENOTCONN:
222 return ENOTCONN;
223 case WSAETIMEDOUT:
224 return ETIMEDOUT;
225 case WSAECONNREFUSED:
226 return ECONNREFUSED;
227 case WSAELOOP:
228 return ELOOP;
229 case WSAEHOSTUNREACH:
230 return EHOSTUNREACH;
231 default:
232 return EIO;
233 }
234}
235
236int inet_aton(const char *cp, struct in_addr *ia)
237{
238 uint32_t addr = inet_addr(cp);
239 if (addr == 0xffffffff) {
240 return 0;
241 }
242 ia->s_addr = addr;
243 return 1;
244}
245
246void qemu_set_cloexec(int fd)
247{
248}
249
250
251#define _W32_FT_OFFSET (116444736000000000ULL)
252
253int qemu_gettimeofday(qemu_timeval *tp)
254{
255 union {
256 unsigned long long ns100;
257 FILETIME ft;
258 } _now;
259
260 if(tp) {
261 GetSystemTimeAsFileTime (&_now.ft);
262 tp->tv_usec=(long)((_now.ns100 / 10ULL) % 1000000ULL );
263 tp->tv_sec= (long)((_now.ns100 - _W32_FT_OFFSET) / 10000000ULL);
264 }
265
266
267 return 0;
268}
269
270int qemu_get_thread_id(void)
271{
272 return GetCurrentThreadId();
273}
274
275char *
276qemu_get_local_state_pathname(const char *relative_pathname)
277{
278 HRESULT result;
279 char base_path[MAX_PATH+1] = "";
280
281 result = SHGetFolderPath(NULL, CSIDL_COMMON_APPDATA, NULL,
282 0, base_path);
283 if (result != S_OK) {
284
285 g_critical("CSIDL_COMMON_APPDATA unavailable: %ld", (long)result);
286 abort();
287 }
288 return g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s", base_path,
289 relative_pathname);
290}
291
292void qemu_set_tty_echo(int fd, bool echo)
293{
294 HANDLE handle = (HANDLE)_get_osfhandle(fd);
295 DWORD dwMode = 0;
296
297 if (handle == INVALID_HANDLE_VALUE) {
298 return;
299 }
300
301 GetConsoleMode(handle, &dwMode);
302
303 if (echo) {
304 SetConsoleMode(handle, dwMode | ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT);
305 } else {
306 SetConsoleMode(handle,
307 dwMode & ~(ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT));
308 }
309}
310
311static char exec_dir[PATH_MAX];
312
313void qemu_init_exec_dir(const char *argv0)
314{
315
316 char *p;
317 char buf[MAX_PATH];
318 DWORD len;
319
320 len = GetModuleFileName(NULL, buf, sizeof(buf) - 1);
321 if (len == 0) {
322 return;
323 }
324
325 buf[len] = 0;
326 p = buf + len - 1;
327 while (p != buf && *p != '\\') {
328 p--;
329 }
330 *p = 0;
331 if (access(buf, R_OK) == 0) {
332 pstrcpy(exec_dir, sizeof(exec_dir), buf);
333 }
334}
335
336char *qemu_get_exec_dir(void)
337{
338 return g_strdup(exec_dir);
339}
340
341#if !GLIB_CHECK_VERSION(2, 50, 0)
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374static int poll_rest(gboolean poll_msgs, HANDLE *handles, gint nhandles,
375 GPollFD *fds, guint nfds, gint timeout)
376{
377 DWORD ready;
378 GPollFD *f;
379 int recursed_result;
380
381 if (poll_msgs) {
382
383
384
385 ready = MsgWaitForMultipleObjectsEx(nhandles, handles, timeout,
386 QS_ALLINPUT, MWMO_ALERTABLE);
387
388 if (ready == WAIT_FAILED) {
389 gchar *emsg = g_win32_error_message(GetLastError());
390 g_warning("MsgWaitForMultipleObjectsEx failed: %s", emsg);
391 g_free(emsg);
392 }
393 } else if (nhandles == 0) {
394
395 if (timeout == INFINITE) {
396 ready = WAIT_FAILED;
397 } else {
398 SleepEx(timeout, TRUE);
399 ready = WAIT_TIMEOUT;
400 }
401 } else {
402
403
404
405 ready =
406 WaitForMultipleObjectsEx(nhandles, handles, FALSE, timeout, TRUE);
407 if (ready == WAIT_FAILED) {
408 gchar *emsg = g_win32_error_message(GetLastError());
409 g_warning("WaitForMultipleObjectsEx failed: %s", emsg);
410 g_free(emsg);
411 }
412 }
413
414 if (ready == WAIT_FAILED) {
415 return -1;
416 } else if (ready == WAIT_TIMEOUT || ready == WAIT_IO_COMPLETION) {
417 return 0;
418 } else if (poll_msgs && ready == WAIT_OBJECT_0 + nhandles) {
419 for (f = fds; f < &fds[nfds]; ++f) {
420 if (f->fd == G_WIN32_MSG_HANDLE && f->events & G_IO_IN) {
421 f->revents |= G_IO_IN;
422 }
423 }
424
425
426
427
428 if (timeout != 0 || nhandles == 0) {
429 return 1;
430 }
431
432
433
434
435 recursed_result = poll_rest(FALSE, handles, nhandles, fds, nfds, 0);
436 return (recursed_result == -1) ? -1 : 1 + recursed_result;
437 } else if (
438
439 ready < WAIT_OBJECT_0 + nhandles) {
440 for (f = fds; f < &fds[nfds]; ++f) {
441 if ((HANDLE) f->fd == handles[ready - WAIT_OBJECT_0]) {
442 f->revents = f->events;
443 }
444 }
445
446
447
448
449 if (timeout == 0 && nhandles > 1) {
450
451 int i;
452 for (i = ready - WAIT_OBJECT_0 + 1; i < nhandles; i++) {
453 handles[i-1] = handles[i];
454 }
455 nhandles--;
456 recursed_result = poll_rest(FALSE, handles, nhandles, fds, nfds, 0);
457 return (recursed_result == -1) ? -1 : 1 + recursed_result;
458 }
459 return 1;
460 }
461
462 return 0;
463}
464
465gint g_poll(GPollFD *fds, guint nfds, gint timeout)
466{
467 HANDLE handles[MAXIMUM_WAIT_OBJECTS];
468 gboolean poll_msgs = FALSE;
469 GPollFD *f;
470 gint nhandles = 0;
471 int retval;
472
473 for (f = fds; f < &fds[nfds]; ++f) {
474 if (f->fd == G_WIN32_MSG_HANDLE && (f->events & G_IO_IN)) {
475 poll_msgs = TRUE;
476 } else if (f->fd > 0) {
477
478
479
480
481 gint i;
482
483 for (i = 0; i < nhandles; i++) {
484 if (handles[i] == (HANDLE) f->fd) {
485 break;
486 }
487 }
488
489 if (i == nhandles) {
490 if (nhandles == MAXIMUM_WAIT_OBJECTS) {
491 g_warning("Too many handles to wait for!\n");
492 break;
493 } else {
494 handles[nhandles++] = (HANDLE) f->fd;
495 }
496 }
497 }
498 }
499
500 for (f = fds; f < &fds[nfds]; ++f) {
501 f->revents = 0;
502 }
503
504 if (timeout == -1) {
505 timeout = INFINITE;
506 }
507
508
509 if (nhandles > 1 || (nhandles > 0 && poll_msgs)) {
510
511
512
513 retval = poll_rest(poll_msgs, handles, nhandles, fds, nfds, 0);
514
515
516
517
518
519
520
521
522
523
524 if (retval == 0 && (timeout == INFINITE || timeout > 0)) {
525 retval = poll_rest(poll_msgs, handles, nhandles,
526 fds, nfds, timeout);
527 }
528 } else {
529
530
531
532 retval = poll_rest(poll_msgs, handles, nhandles, fds, nfds, timeout);
533 }
534
535 if (retval == -1) {
536 for (f = fds; f < &fds[nfds]; ++f) {
537 f->revents = 0;
538 }
539 }
540
541 return retval;
542}
543#endif
544
545int getpagesize(void)
546{
547 SYSTEM_INFO system_info;
548
549 GetSystemInfo(&system_info);
550 return system_info.dwPageSize;
551}
552
553void os_mem_prealloc(int fd, char *area, size_t memory, int smp_cpus,
554 Error **errp)
555{
556 int i;
557 size_t pagesize = qemu_real_host_page_size;
558
559 memory = (memory + pagesize - 1) & -pagesize;
560 for (i = 0; i < memory / pagesize; i++) {
561 memset(area + pagesize * i, 0, 1);
562 }
563}
564
565char *qemu_get_pid_name(pid_t pid)
566{
567
568 abort();
569}
570
571
572pid_t qemu_fork(Error **errp)
573{
574 errno = ENOSYS;
575 error_setg_errno(errp, errno,
576 "cannot fork child process");
577 return -1;
578}
579
580
581#undef connect
582int qemu_connect_wrap(int sockfd, const struct sockaddr *addr,
583 socklen_t addrlen)
584{
585 int ret;
586 ret = connect(sockfd, addr, addrlen);
587 if (ret < 0) {
588 if (WSAGetLastError() == WSAEWOULDBLOCK) {
589 errno = EINPROGRESS;
590 } else {
591 errno = socket_error();
592 }
593 }
594 return ret;
595}
596
597
598#undef listen
599int qemu_listen_wrap(int sockfd, int backlog)
600{
601 int ret;
602 ret = listen(sockfd, backlog);
603 if (ret < 0) {
604 errno = socket_error();
605 }
606 return ret;
607}
608
609
610#undef bind
611int qemu_bind_wrap(int sockfd, const struct sockaddr *addr,
612 socklen_t addrlen)
613{
614 int ret;
615 ret = bind(sockfd, addr, addrlen);
616 if (ret < 0) {
617 errno = socket_error();
618 }
619 return ret;
620}
621
622
623#undef socket
624int qemu_socket_wrap(int domain, int type, int protocol)
625{
626 int ret;
627 ret = socket(domain, type, protocol);
628 if (ret < 0) {
629 errno = socket_error();
630 }
631 return ret;
632}
633
634
635#undef accept
636int qemu_accept_wrap(int sockfd, struct sockaddr *addr,
637 socklen_t *addrlen)
638{
639 int ret;
640 ret = accept(sockfd, addr, addrlen);
641 if (ret < 0) {
642 errno = socket_error();
643 }
644 return ret;
645}
646
647
648#undef shutdown
649int qemu_shutdown_wrap(int sockfd, int how)
650{
651 int ret;
652 ret = shutdown(sockfd, how);
653 if (ret < 0) {
654 errno = socket_error();
655 }
656 return ret;
657}
658
659
660#undef ioctlsocket
661int qemu_ioctlsocket_wrap(int fd, int req, void *val)
662{
663 int ret;
664 ret = ioctlsocket(fd, req, val);
665 if (ret < 0) {
666 errno = socket_error();
667 }
668 return ret;
669}
670
671
672#undef closesocket
673int qemu_closesocket_wrap(int fd)
674{
675 int ret;
676 ret = closesocket(fd);
677 if (ret < 0) {
678 errno = socket_error();
679 }
680 return ret;
681}
682
683
684#undef getsockopt
685int qemu_getsockopt_wrap(int sockfd, int level, int optname,
686 void *optval, socklen_t *optlen)
687{
688 int ret;
689 ret = getsockopt(sockfd, level, optname, optval, optlen);
690 if (ret < 0) {
691 errno = socket_error();
692 }
693 return ret;
694}
695
696
697#undef setsockopt
698int qemu_setsockopt_wrap(int sockfd, int level, int optname,
699 const void *optval, socklen_t optlen)
700{
701 int ret;
702 ret = setsockopt(sockfd, level, optname, optval, optlen);
703 if (ret < 0) {
704 errno = socket_error();
705 }
706 return ret;
707}
708
709
710#undef getpeername
711int qemu_getpeername_wrap(int sockfd, struct sockaddr *addr,
712 socklen_t *addrlen)
713{
714 int ret;
715 ret = getpeername(sockfd, addr, addrlen);
716 if (ret < 0) {
717 errno = socket_error();
718 }
719 return ret;
720}
721
722
723#undef getsockname
724int qemu_getsockname_wrap(int sockfd, struct sockaddr *addr,
725 socklen_t *addrlen)
726{
727 int ret;
728 ret = getsockname(sockfd, addr, addrlen);
729 if (ret < 0) {
730 errno = socket_error();
731 }
732 return ret;
733}
734
735
736#undef send
737ssize_t qemu_send_wrap(int sockfd, const void *buf, size_t len, int flags)
738{
739 int ret;
740 ret = send(sockfd, buf, len, flags);
741 if (ret < 0) {
742 errno = socket_error();
743 }
744 return ret;
745}
746
747
748#undef sendto
749ssize_t qemu_sendto_wrap(int sockfd, const void *buf, size_t len, int flags,
750 const struct sockaddr *addr, socklen_t addrlen)
751{
752 int ret;
753 ret = sendto(sockfd, buf, len, flags, addr, addrlen);
754 if (ret < 0) {
755 errno = socket_error();
756 }
757 return ret;
758}
759
760
761#undef recv
762ssize_t qemu_recv_wrap(int sockfd, void *buf, size_t len, int flags)
763{
764 int ret;
765 ret = recv(sockfd, buf, len, flags);
766 if (ret < 0) {
767 errno = socket_error();
768 }
769 return ret;
770}
771
772
773#undef recvfrom
774ssize_t qemu_recvfrom_wrap(int sockfd, void *buf, size_t len, int flags,
775 struct sockaddr *addr, socklen_t *addrlen)
776{
777 int ret;
778 ret = recvfrom(sockfd, buf, len, flags, addr, addrlen);
779 if (ret < 0) {
780 errno = socket_error();
781 }
782 return ret;
783}
784
785bool qemu_write_pidfile(const char *filename, Error **errp)
786{
787 char buffer[128];
788 int len;
789 HANDLE file;
790 OVERLAPPED overlap;
791 BOOL ret;
792 memset(&overlap, 0, sizeof(overlap));
793
794 file = CreateFile(filename, GENERIC_WRITE, FILE_SHARE_READ, NULL,
795 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
796
797 if (file == INVALID_HANDLE_VALUE) {
798 error_setg(errp, "Failed to create PID file");
799 return false;
800 }
801 len = snprintf(buffer, sizeof(buffer), FMT_pid "\n", (pid_t)getpid());
802 ret = WriteFile(file, (LPCVOID)buffer, (DWORD)len,
803 NULL, &overlap);
804 CloseHandle(file);
805 if (ret == 0) {
806 error_setg(errp, "Failed to write PID file");
807 return false;
808 }
809 return true;
810}
811