Statistics
| Branch: | Revision:

root / oslib-win32.c @ 01e26b0e

History | View | Annotate | Download (4 kB)

1 c1b0b93b Jes Sorensen
/*
2 c1b0b93b Jes Sorensen
 * os-win32.c
3 c1b0b93b Jes Sorensen
 *
4 c1b0b93b Jes Sorensen
 * Copyright (c) 2003-2008 Fabrice Bellard
5 c1b0b93b Jes Sorensen
 * Copyright (c) 2010 Red Hat, Inc.
6 c1b0b93b Jes Sorensen
 *
7 c1b0b93b Jes Sorensen
 * QEMU library functions for win32 which are shared between QEMU and
8 c1b0b93b Jes Sorensen
 * the QEMU tools.
9 c1b0b93b Jes Sorensen
 *
10 c1b0b93b Jes Sorensen
 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 c1b0b93b Jes Sorensen
 * of this software and associated documentation files (the "Software"), to deal
12 c1b0b93b Jes Sorensen
 * in the Software without restriction, including without limitation the rights
13 c1b0b93b Jes Sorensen
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 c1b0b93b Jes Sorensen
 * copies of the Software, and to permit persons to whom the Software is
15 c1b0b93b Jes Sorensen
 * furnished to do so, subject to the following conditions:
16 c1b0b93b Jes Sorensen
 *
17 c1b0b93b Jes Sorensen
 * The above copyright notice and this permission notice shall be included in
18 c1b0b93b Jes Sorensen
 * all copies or substantial portions of the Software.
19 c1b0b93b Jes Sorensen
 *
20 c1b0b93b Jes Sorensen
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 c1b0b93b Jes Sorensen
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 c1b0b93b Jes Sorensen
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 c1b0b93b Jes Sorensen
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 c1b0b93b Jes Sorensen
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 c1b0b93b Jes Sorensen
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 c1b0b93b Jes Sorensen
 * THE SOFTWARE.
27 c1b0b93b Jes Sorensen
 */
28 c1b0b93b Jes Sorensen
#include <windows.h>
29 c1b0b93b Jes Sorensen
#include "config-host.h"
30 c1b0b93b Jes Sorensen
#include "sysemu.h"
31 d3385eb4 Paolo Bonzini
#include "main-loop.h"
32 c1b0b93b Jes Sorensen
#include "trace.h"
33 9549e764 Jes Sorensen
#include "qemu_socket.h"
34 c1b0b93b Jes Sorensen
35 1f001dc7 Paolo Bonzini
static void default_qemu_fd_register(int fd)
36 1f001dc7 Paolo Bonzini
{
37 1f001dc7 Paolo Bonzini
}
38 1f001dc7 Paolo Bonzini
QEMU_WEAK_ALIAS(qemu_fd_register, default_qemu_fd_register);
39 1f001dc7 Paolo Bonzini
#define qemu_fd_register \
40 1f001dc7 Paolo Bonzini
    QEMU_WEAK_REF(qemu_fd_register, default_qemu_fd_register)
41 1f001dc7 Paolo Bonzini
42 b152aa84 Jes Sorensen
void *qemu_oom_check(void *ptr)
43 c1b0b93b Jes Sorensen
{
44 c1b0b93b Jes Sorensen
    if (ptr == NULL) {
45 c1b0b93b Jes Sorensen
        fprintf(stderr, "Failed to allocate memory: %lu\n", GetLastError());
46 c1b0b93b Jes Sorensen
        abort();
47 c1b0b93b Jes Sorensen
    }
48 c1b0b93b Jes Sorensen
    return ptr;
49 c1b0b93b Jes Sorensen
}
50 c1b0b93b Jes Sorensen
51 c1b0b93b Jes Sorensen
void *qemu_memalign(size_t alignment, size_t size)
52 c1b0b93b Jes Sorensen
{
53 c1b0b93b Jes Sorensen
    void *ptr;
54 c1b0b93b Jes Sorensen
55 c1b0b93b Jes Sorensen
    if (!size) {
56 c1b0b93b Jes Sorensen
        abort();
57 c1b0b93b Jes Sorensen
    }
58 b152aa84 Jes Sorensen
    ptr = qemu_oom_check(VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE));
59 c1b0b93b Jes Sorensen
    trace_qemu_memalign(alignment, size, ptr);
60 c1b0b93b Jes Sorensen
    return ptr;
61 c1b0b93b Jes Sorensen
}
62 c1b0b93b Jes Sorensen
63 c1b0b93b Jes Sorensen
void *qemu_vmalloc(size_t size)
64 c1b0b93b Jes Sorensen
{
65 c1b0b93b Jes Sorensen
    void *ptr;
66 c1b0b93b Jes Sorensen
67 c1b0b93b Jes Sorensen
    /* FIXME: this is not exactly optimal solution since VirtualAlloc
68 c1b0b93b Jes Sorensen
       has 64Kb granularity, but at least it guarantees us that the
69 c1b0b93b Jes Sorensen
       memory is page aligned. */
70 c1b0b93b Jes Sorensen
    if (!size) {
71 c1b0b93b Jes Sorensen
        abort();
72 c1b0b93b Jes Sorensen
    }
73 b152aa84 Jes Sorensen
    ptr = qemu_oom_check(VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE));
74 c1b0b93b Jes Sorensen
    trace_qemu_vmalloc(size, ptr);
75 c1b0b93b Jes Sorensen
    return ptr;
76 c1b0b93b Jes Sorensen
}
77 c1b0b93b Jes Sorensen
78 c1b0b93b Jes Sorensen
void qemu_vfree(void *ptr)
79 c1b0b93b Jes Sorensen
{
80 c1b0b93b Jes Sorensen
    trace_qemu_vfree(ptr);
81 c1b0b93b Jes Sorensen
    VirtualFree(ptr, 0, MEM_RELEASE);
82 c1b0b93b Jes Sorensen
}
83 9549e764 Jes Sorensen
84 d3e8f957 Stefan Weil
/* FIXME: add proper locking */
85 d3e8f957 Stefan Weil
struct tm *gmtime_r(const time_t *timep, struct tm *result)
86 d3e8f957 Stefan Weil
{
87 d3e8f957 Stefan Weil
    struct tm *p = gmtime(timep);
88 d3e8f957 Stefan Weil
    memset(result, 0, sizeof(*result));
89 d3e8f957 Stefan Weil
    if (p) {
90 d3e8f957 Stefan Weil
        *result = *p;
91 d3e8f957 Stefan Weil
        p = result;
92 d3e8f957 Stefan Weil
    }
93 d3e8f957 Stefan Weil
    return p;
94 d3e8f957 Stefan Weil
}
95 d3e8f957 Stefan Weil
96 d3e8f957 Stefan Weil
/* FIXME: add proper locking */
97 d3e8f957 Stefan Weil
struct tm *localtime_r(const time_t *timep, struct tm *result)
98 d3e8f957 Stefan Weil
{
99 d3e8f957 Stefan Weil
    struct tm *p = localtime(timep);
100 d3e8f957 Stefan Weil
    memset(result, 0, sizeof(*result));
101 d3e8f957 Stefan Weil
    if (p) {
102 d3e8f957 Stefan Weil
        *result = *p;
103 d3e8f957 Stefan Weil
        p = result;
104 d3e8f957 Stefan Weil
    }
105 d3e8f957 Stefan Weil
    return p;
106 d3e8f957 Stefan Weil
}
107 d3e8f957 Stefan Weil
108 154b9a0c Paolo Bonzini
void socket_set_block(int fd)
109 154b9a0c Paolo Bonzini
{
110 154b9a0c Paolo Bonzini
    unsigned long opt = 0;
111 d3385eb4 Paolo Bonzini
    WSAEventSelect(fd, NULL, 0);
112 154b9a0c Paolo Bonzini
    ioctlsocket(fd, FIONBIO, &opt);
113 154b9a0c Paolo Bonzini
}
114 154b9a0c Paolo Bonzini
115 9549e764 Jes Sorensen
void socket_set_nonblock(int fd)
116 9549e764 Jes Sorensen
{
117 9549e764 Jes Sorensen
    unsigned long opt = 1;
118 9549e764 Jes Sorensen
    ioctlsocket(fd, FIONBIO, &opt);
119 d3385eb4 Paolo Bonzini
    qemu_fd_register(fd);
120 9549e764 Jes Sorensen
}
121 9549e764 Jes Sorensen
122 9549e764 Jes Sorensen
int inet_aton(const char *cp, struct in_addr *ia)
123 9549e764 Jes Sorensen
{
124 9549e764 Jes Sorensen
    uint32_t addr = inet_addr(cp);
125 9549e764 Jes Sorensen
    if (addr == 0xffffffff) {
126 9549e764 Jes Sorensen
        return 0;
127 9549e764 Jes Sorensen
    }
128 9549e764 Jes Sorensen
    ia->s_addr = addr;
129 9549e764 Jes Sorensen
    return 1;
130 9549e764 Jes Sorensen
}
131 9549e764 Jes Sorensen
132 9549e764 Jes Sorensen
void qemu_set_cloexec(int fd)
133 9549e764 Jes Sorensen
{
134 9549e764 Jes Sorensen
}
135 dc786bc9 Jes Sorensen
136 dc786bc9 Jes Sorensen
/* Offset between 1/1/1601 and 1/1/1970 in 100 nanosec units */
137 dc786bc9 Jes Sorensen
#define _W32_FT_OFFSET (116444736000000000ULL)
138 dc786bc9 Jes Sorensen
139 dc786bc9 Jes Sorensen
int qemu_gettimeofday(qemu_timeval *tp)
140 dc786bc9 Jes Sorensen
{
141 dc786bc9 Jes Sorensen
  union {
142 dc786bc9 Jes Sorensen
    unsigned long long ns100; /*time since 1 Jan 1601 in 100ns units */
143 dc786bc9 Jes Sorensen
    FILETIME ft;
144 dc786bc9 Jes Sorensen
  }  _now;
145 dc786bc9 Jes Sorensen
146 dc786bc9 Jes Sorensen
  if(tp) {
147 dc786bc9 Jes Sorensen
      GetSystemTimeAsFileTime (&_now.ft);
148 dc786bc9 Jes Sorensen
      tp->tv_usec=(long)((_now.ns100 / 10ULL) % 1000000ULL );
149 dc786bc9 Jes Sorensen
      tp->tv_sec= (long)((_now.ns100 - _W32_FT_OFFSET) / 10000000ULL);
150 dc786bc9 Jes Sorensen
  }
151 dc786bc9 Jes Sorensen
  /* Always return 0 as per Open Group Base Specifications Issue 6.
152 dc786bc9 Jes Sorensen
     Do not set errno on error.  */
153 dc786bc9 Jes Sorensen
  return 0;
154 dc786bc9 Jes Sorensen
}
155 cbcfa041 Paolo Bonzini
156 cbcfa041 Paolo Bonzini
int qemu_get_thread_id(void)
157 cbcfa041 Paolo Bonzini
{
158 cbcfa041 Paolo Bonzini
    return GetCurrentThreadId();
159 cbcfa041 Paolo Bonzini
}