Replace qemu_real_host_page variables with inlined functions
[qemu/ar7.git] / util / oslib-win32.c
blob3555b0235089d6c9c0788509755800f123f0445d
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 "qemu-common.h"
36 #include "qapi/error.h"
37 #include "qemu/main-loop.h"
38 #include "trace.h"
39 #include "qemu/sockets.h"
40 #include "qemu/cutils.h"
41 #include "qemu/error-report.h"
42 #include <malloc.h>
44 /* this must come after including "trace.h" */
45 #include <shlobj.h>
47 static int get_allocation_granularity(void)
49 SYSTEM_INFO system_info;
51 GetSystemInfo(&system_info);
52 return system_info.dwAllocationGranularity;
55 void *qemu_anon_ram_alloc(size_t size, uint64_t *align, bool shared,
56 bool noreserve)
58 void *ptr;
60 if (noreserve) {
62 * We need a MEM_COMMIT before accessing any memory in a MEM_RESERVE
63 * area; we cannot easily mimic POSIX MAP_NORESERVE semantics.
65 error_report("Skipping reservation of swap space is not supported.");
66 return NULL;
69 ptr = VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE);
70 trace_qemu_anon_ram_alloc(size, ptr);
72 if (ptr && align) {
73 *align = MAX(get_allocation_granularity(), getpagesize());
75 return ptr;
78 void qemu_anon_ram_free(void *ptr, size_t size)
80 trace_qemu_anon_ram_free(ptr, size);
81 if (ptr) {
82 VirtualFree(ptr, 0, MEM_RELEASE);
86 #ifndef _POSIX_THREAD_SAFE_FUNCTIONS
87 /* FIXME: add proper locking */
88 struct tm *gmtime_r(const time_t *timep, struct tm *result)
90 struct tm *p = gmtime(timep);
91 memset(result, 0, sizeof(*result));
92 if (p) {
93 *result = *p;
94 p = result;
96 return p;
99 /* FIXME: add proper locking */
100 struct tm *localtime_r(const time_t *timep, struct tm *result)
102 struct tm *p = localtime(timep);
103 memset(result, 0, sizeof(*result));
104 if (p) {
105 *result = *p;
106 p = result;
108 return p;
110 #endif /* _POSIX_THREAD_SAFE_FUNCTIONS */
112 static int socket_error(void)
114 switch (WSAGetLastError()) {
115 case 0:
116 return 0;
117 case WSAEINTR:
118 return EINTR;
119 case WSAEINVAL:
120 return EINVAL;
121 case WSA_INVALID_HANDLE:
122 return EBADF;
123 case WSA_NOT_ENOUGH_MEMORY:
124 return ENOMEM;
125 case WSA_INVALID_PARAMETER:
126 return EINVAL;
127 case WSAENAMETOOLONG:
128 return ENAMETOOLONG;
129 case WSAENOTEMPTY:
130 return ENOTEMPTY;
131 case WSAEWOULDBLOCK:
132 /* not using EWOULDBLOCK as we don't want code to have
133 * to check both EWOULDBLOCK and EAGAIN */
134 return EAGAIN;
135 case WSAEINPROGRESS:
136 return EINPROGRESS;
137 case WSAEALREADY:
138 return EALREADY;
139 case WSAENOTSOCK:
140 return ENOTSOCK;
141 case WSAEDESTADDRREQ:
142 return EDESTADDRREQ;
143 case WSAEMSGSIZE:
144 return EMSGSIZE;
145 case WSAEPROTOTYPE:
146 return EPROTOTYPE;
147 case WSAENOPROTOOPT:
148 return ENOPROTOOPT;
149 case WSAEPROTONOSUPPORT:
150 return EPROTONOSUPPORT;
151 case WSAEOPNOTSUPP:
152 return EOPNOTSUPP;
153 case WSAEAFNOSUPPORT:
154 return EAFNOSUPPORT;
155 case WSAEADDRINUSE:
156 return EADDRINUSE;
157 case WSAEADDRNOTAVAIL:
158 return EADDRNOTAVAIL;
159 case WSAENETDOWN:
160 return ENETDOWN;
161 case WSAENETUNREACH:
162 return ENETUNREACH;
163 case WSAENETRESET:
164 return ENETRESET;
165 case WSAECONNABORTED:
166 return ECONNABORTED;
167 case WSAECONNRESET:
168 return ECONNRESET;
169 case WSAENOBUFS:
170 return ENOBUFS;
171 case WSAEISCONN:
172 return EISCONN;
173 case WSAENOTCONN:
174 return ENOTCONN;
175 case WSAETIMEDOUT:
176 return ETIMEDOUT;
177 case WSAECONNREFUSED:
178 return ECONNREFUSED;
179 case WSAELOOP:
180 return ELOOP;
181 case WSAEHOSTUNREACH:
182 return EHOSTUNREACH;
183 default:
184 return EIO;
188 void qemu_set_block(int fd)
190 unsigned long opt = 0;
191 WSAEventSelect(fd, NULL, 0);
192 ioctlsocket(fd, FIONBIO, &opt);
195 int qemu_try_set_nonblock(int fd)
197 unsigned long opt = 1;
198 if (ioctlsocket(fd, FIONBIO, &opt) != NO_ERROR) {
199 return -socket_error();
201 return 0;
204 void qemu_set_nonblock(int fd)
206 (void)qemu_try_set_nonblock(fd);
209 int socket_set_fast_reuse(int fd)
211 /* Enabling the reuse of an endpoint that was used by a socket still in
212 * TIME_WAIT state is usually performed by setting SO_REUSEADDR. On Windows
213 * fast reuse is the default and SO_REUSEADDR does strange things. So we
214 * don't have to do anything here. More info can be found at:
215 * http://msdn.microsoft.com/en-us/library/windows/desktop/ms740621.aspx */
216 return 0;
219 int inet_aton(const char *cp, struct in_addr *ia)
221 uint32_t addr = inet_addr(cp);
222 if (addr == 0xffffffff) {
223 return 0;
225 ia->s_addr = addr;
226 return 1;
229 void qemu_set_cloexec(int fd)
233 int qemu_get_thread_id(void)
235 return GetCurrentThreadId();
238 char *
239 qemu_get_local_state_pathname(const char *relative_pathname)
241 HRESULT result;
242 char base_path[MAX_PATH+1] = "";
244 result = SHGetFolderPath(NULL, CSIDL_COMMON_APPDATA, NULL,
245 /* SHGFP_TYPE_CURRENT */ 0, base_path);
246 if (result != S_OK) {
247 /* misconfigured environment */
248 g_critical("CSIDL_COMMON_APPDATA unavailable: %ld", (long)result);
249 abort();
251 return g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s", base_path,
252 relative_pathname);
255 void qemu_set_tty_echo(int fd, bool echo)
257 HANDLE handle = (HANDLE)_get_osfhandle(fd);
258 DWORD dwMode = 0;
260 if (handle == INVALID_HANDLE_VALUE) {
261 return;
264 GetConsoleMode(handle, &dwMode);
266 if (echo) {
267 SetConsoleMode(handle, dwMode | ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT);
268 } else {
269 SetConsoleMode(handle,
270 dwMode & ~(ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT));
274 static const char *exec_dir;
276 void qemu_init_exec_dir(const char *argv0)
279 char *p;
280 char buf[MAX_PATH];
281 DWORD len;
283 if (exec_dir) {
284 return;
287 len = GetModuleFileName(NULL, buf, sizeof(buf) - 1);
288 if (len == 0) {
289 return;
292 buf[len] = 0;
293 p = buf + len - 1;
294 while (p != buf && *p != '\\') {
295 p--;
297 *p = 0;
298 if (access(buf, R_OK) == 0) {
299 exec_dir = g_strdup(buf);
300 } else {
301 exec_dir = CONFIG_BINDIR;
305 const char *qemu_get_exec_dir(void)
307 return exec_dir;
310 int getpagesize(void)
312 SYSTEM_INFO system_info;
314 GetSystemInfo(&system_info);
315 return system_info.dwPageSize;
318 void os_mem_prealloc(int fd, char *area, size_t memory, int smp_cpus,
319 Error **errp)
321 int i;
322 size_t pagesize = qemu_real_host_page_size();
324 memory = (memory + pagesize - 1) & -pagesize;
325 for (i = 0; i < memory / pagesize; i++) {
326 memset(area + pagesize * i, 0, 1);
330 char *qemu_get_pid_name(pid_t pid)
332 /* XXX Implement me */
333 abort();
337 pid_t qemu_fork(Error **errp)
339 errno = ENOSYS;
340 error_setg_errno(errp, errno,
341 "cannot fork child process");
342 return -1;
346 #undef connect
347 int qemu_connect_wrap(int sockfd, const struct sockaddr *addr,
348 socklen_t addrlen)
350 int ret;
351 ret = connect(sockfd, addr, addrlen);
352 if (ret < 0) {
353 if (WSAGetLastError() == WSAEWOULDBLOCK) {
354 errno = EINPROGRESS;
355 } else {
356 errno = socket_error();
359 return ret;
363 #undef listen
364 int qemu_listen_wrap(int sockfd, int backlog)
366 int ret;
367 ret = listen(sockfd, backlog);
368 if (ret < 0) {
369 errno = socket_error();
371 return ret;
375 #undef bind
376 int qemu_bind_wrap(int sockfd, const struct sockaddr *addr,
377 socklen_t addrlen)
379 int ret;
380 ret = bind(sockfd, addr, addrlen);
381 if (ret < 0) {
382 errno = socket_error();
384 return ret;
388 #undef socket
389 int qemu_socket_wrap(int domain, int type, int protocol)
391 int ret;
392 ret = socket(domain, type, protocol);
393 if (ret < 0) {
394 errno = socket_error();
396 return ret;
400 #undef accept
401 int qemu_accept_wrap(int sockfd, struct sockaddr *addr,
402 socklen_t *addrlen)
404 int ret;
405 ret = accept(sockfd, addr, addrlen);
406 if (ret < 0) {
407 errno = socket_error();
409 return ret;
413 #undef shutdown
414 int qemu_shutdown_wrap(int sockfd, int how)
416 int ret;
417 ret = shutdown(sockfd, how);
418 if (ret < 0) {
419 errno = socket_error();
421 return ret;
425 #undef ioctlsocket
426 int qemu_ioctlsocket_wrap(int fd, int req, void *val)
428 int ret;
429 ret = ioctlsocket(fd, req, val);
430 if (ret < 0) {
431 errno = socket_error();
433 return ret;
437 #undef closesocket
438 int qemu_closesocket_wrap(int fd)
440 int ret;
441 ret = closesocket(fd);
442 if (ret < 0) {
443 errno = socket_error();
445 return ret;
449 #undef getsockopt
450 int qemu_getsockopt_wrap(int sockfd, int level, int optname,
451 void *optval, socklen_t *optlen)
453 int ret;
454 ret = getsockopt(sockfd, level, optname, optval, optlen);
455 if (ret < 0) {
456 errno = socket_error();
458 return ret;
462 #undef setsockopt
463 int qemu_setsockopt_wrap(int sockfd, int level, int optname,
464 const void *optval, socklen_t optlen)
466 int ret;
467 ret = setsockopt(sockfd, level, optname, optval, optlen);
468 if (ret < 0) {
469 errno = socket_error();
471 return ret;
475 #undef getpeername
476 int qemu_getpeername_wrap(int sockfd, struct sockaddr *addr,
477 socklen_t *addrlen)
479 int ret;
480 ret = getpeername(sockfd, addr, addrlen);
481 if (ret < 0) {
482 errno = socket_error();
484 return ret;
488 #undef getsockname
489 int qemu_getsockname_wrap(int sockfd, struct sockaddr *addr,
490 socklen_t *addrlen)
492 int ret;
493 ret = getsockname(sockfd, addr, addrlen);
494 if (ret < 0) {
495 errno = socket_error();
497 return ret;
501 #undef send
502 ssize_t qemu_send_wrap(int sockfd, const void *buf, size_t len, int flags)
504 int ret;
505 ret = send(sockfd, buf, len, flags);
506 if (ret < 0) {
507 errno = socket_error();
509 return ret;
513 #undef sendto
514 ssize_t qemu_sendto_wrap(int sockfd, const void *buf, size_t len, int flags,
515 const struct sockaddr *addr, socklen_t addrlen)
517 int ret;
518 ret = sendto(sockfd, buf, len, flags, addr, addrlen);
519 if (ret < 0) {
520 errno = socket_error();
522 return ret;
526 #undef recv
527 ssize_t qemu_recv_wrap(int sockfd, void *buf, size_t len, int flags)
529 int ret;
530 ret = recv(sockfd, buf, len, flags);
531 if (ret < 0) {
532 errno = socket_error();
534 return ret;
538 #undef recvfrom
539 ssize_t qemu_recvfrom_wrap(int sockfd, void *buf, size_t len, int flags,
540 struct sockaddr *addr, socklen_t *addrlen)
542 int ret;
543 ret = recvfrom(sockfd, buf, len, flags, addr, addrlen);
544 if (ret < 0) {
545 errno = socket_error();
547 return ret;
550 bool qemu_write_pidfile(const char *filename, Error **errp)
552 char buffer[128];
553 int len;
554 HANDLE file;
555 OVERLAPPED overlap;
556 BOOL ret;
557 memset(&overlap, 0, sizeof(overlap));
559 file = CreateFile(filename, GENERIC_WRITE, FILE_SHARE_READ, NULL,
560 OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
562 if (file == INVALID_HANDLE_VALUE) {
563 error_setg(errp, "Failed to create PID file");
564 return false;
566 len = snprintf(buffer, sizeof(buffer), FMT_pid "\n", (pid_t)getpid());
567 ret = WriteFile(file, (LPCVOID)buffer, (DWORD)len,
568 NULL, &overlap);
569 CloseHandle(file);
570 if (ret == 0) {
571 error_setg(errp, "Failed to write PID file");
572 return false;
574 return true;
577 char *qemu_get_host_name(Error **errp)
579 wchar_t tmp[MAX_COMPUTERNAME_LENGTH + 1];
580 DWORD size = G_N_ELEMENTS(tmp);
582 if (GetComputerNameW(tmp, &size) == 0) {
583 error_setg_win32(errp, GetLastError(), "failed close handle");
584 return NULL;
587 return g_utf16_to_utf8(tmp, size, NULL, NULL, NULL);
590 size_t qemu_get_host_physmem(void)
592 MEMORYSTATUSEX statex;
593 statex.dwLength = sizeof(statex);
595 if (GlobalMemoryStatusEx(&statex)) {
596 return statex.ullTotalPhys;
598 return 0;