root / osdep.c @ 67f0875e
History | View | Annotate | Download (4.9 kB)
1 | ea88812f | bellard | /*
|
---|---|---|---|
2 | ea88812f | bellard | * QEMU low level functions
|
3 | 5fafdf24 | ths | *
|
4 | ea88812f | bellard | * Copyright (c) 2003 Fabrice Bellard
|
5 | 5fafdf24 | ths | *
|
6 | ea88812f | bellard | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 | ea88812f | bellard | * of this software and associated documentation files (the "Software"), to deal
|
8 | ea88812f | bellard | * in the Software without restriction, including without limitation the rights
|
9 | ea88812f | bellard | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 | ea88812f | bellard | * copies of the Software, and to permit persons to whom the Software is
|
11 | ea88812f | bellard | * furnished to do so, subject to the following conditions:
|
12 | ea88812f | bellard | *
|
13 | ea88812f | bellard | * The above copyright notice and this permission notice shall be included in
|
14 | ea88812f | bellard | * all copies or substantial portions of the Software.
|
15 | ea88812f | bellard | *
|
16 | ea88812f | bellard | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 | ea88812f | bellard | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 | ea88812f | bellard | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
19 | ea88812f | bellard | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 | ea88812f | bellard | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 | ea88812f | bellard | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22 | ea88812f | bellard | * THE SOFTWARE.
|
23 | ea88812f | bellard | */
|
24 | ea88812f | bellard | #include <stdlib.h> |
25 | ea88812f | bellard | #include <stdio.h> |
26 | ea88812f | bellard | #include <stdarg.h> |
27 | ea88812f | bellard | #include <string.h> |
28 | ea88812f | bellard | #include <errno.h> |
29 | ea88812f | bellard | #include <unistd.h> |
30 | aa26bb2d | ths | #include <fcntl.h> |
31 | dfe5fff3 | Juan Quintela | #ifdef CONFIG_SOLARIS
|
32 | 605686cd | ths | #include <sys/types.h> |
33 | 605686cd | ths | #include <sys/statvfs.h> |
34 | 605686cd | ths | #endif
|
35 | ea88812f | bellard | |
36 | 71e72a19 | Juan Quintela | /* Needed early for CONFIG_BSD etc. */
|
37 | d40cdb10 | blueswir1 | #include "config-host.h" |
38 | d40cdb10 | blueswir1 | |
39 | 6e4255f6 | bellard | #ifdef _WIN32
|
40 | 6e4255f6 | bellard | #include <windows.h> |
41 | 71e72a19 | Juan Quintela | #elif defined(CONFIG_BSD)
|
42 | 194884dd | bellard | #include <stdlib.h> |
43 | 194884dd | bellard | #else
|
44 | 49b470eb | bellard | #include <malloc.h> |
45 | 194884dd | bellard | #endif
|
46 | 49b470eb | bellard | |
47 | 511d2b14 | blueswir1 | #include "qemu-common.h" |
48 | 511d2b14 | blueswir1 | #include "sysemu.h" |
49 | 03ff3ca3 | aliguori | #include "qemu_socket.h" |
50 | 03ff3ca3 | aliguori | |
51 | d644f8be | malc | #if !defined(_POSIX_C_SOURCE) || defined(_WIN32)
|
52 | d644f8be | malc | static void *oom_check(void *ptr) |
53 | d644f8be | malc | { |
54 | d644f8be | malc | if (ptr == NULL) { |
55 | d644f8be | malc | abort(); |
56 | d644f8be | malc | } |
57 | d644f8be | malc | return ptr;
|
58 | d644f8be | malc | } |
59 | d644f8be | malc | #endif
|
60 | d644f8be | malc | |
61 | 6e4255f6 | bellard | #if defined(_WIN32)
|
62 | 33f00271 | balrog | void *qemu_memalign(size_t alignment, size_t size)
|
63 | 33f00271 | balrog | { |
64 | d644f8be | malc | if (!size) {
|
65 | d644f8be | malc | abort(); |
66 | d644f8be | malc | } |
67 | d644f8be | malc | return oom_check(VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE)); |
68 | 33f00271 | balrog | } |
69 | 6e4255f6 | bellard | |
70 | 6e4255f6 | bellard | void *qemu_vmalloc(size_t size)
|
71 | 6e4255f6 | bellard | { |
72 | 6e4255f6 | bellard | /* FIXME: this is not exactly optimal solution since VirtualAlloc
|
73 | 6e4255f6 | bellard | has 64Kb granularity, but at least it guarantees us that the
|
74 | 6e4255f6 | bellard | memory is page aligned. */
|
75 | d644f8be | malc | if (!size) {
|
76 | d644f8be | malc | abort(); |
77 | d644f8be | malc | } |
78 | d644f8be | malc | return oom_check(VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE)); |
79 | 6e4255f6 | bellard | } |
80 | 6e4255f6 | bellard | |
81 | 6e4255f6 | bellard | void qemu_vfree(void *ptr) |
82 | 6e4255f6 | bellard | { |
83 | 6e4255f6 | bellard | VirtualFree(ptr, 0, MEM_RELEASE);
|
84 | 6e4255f6 | bellard | } |
85 | 6e4255f6 | bellard | |
86 | 6cb7ee85 | pbrook | #else
|
87 | 6cb7ee85 | pbrook | |
88 | 33f00271 | balrog | void *qemu_memalign(size_t alignment, size_t size)
|
89 | 33f00271 | balrog | { |
90 | 33f00271 | balrog | #if defined(_POSIX_C_SOURCE)
|
91 | 33f00271 | balrog | int ret;
|
92 | 33f00271 | balrog | void *ptr;
|
93 | 33f00271 | balrog | ret = posix_memalign(&ptr, alignment, size); |
94 | 33f00271 | balrog | if (ret != 0) |
95 | d644f8be | malc | abort(); |
96 | 33f00271 | balrog | return ptr;
|
97 | 71e72a19 | Juan Quintela | #elif defined(CONFIG_BSD)
|
98 | d644f8be | malc | return oom_check(valloc(size));
|
99 | 33f00271 | balrog | #else
|
100 | d644f8be | malc | return oom_check(memalign(alignment, size));
|
101 | 33f00271 | balrog | #endif
|
102 | 33f00271 | balrog | } |
103 | 33f00271 | balrog | |
104 | 49b470eb | bellard | /* alloc shared memory pages */
|
105 | 49b470eb | bellard | void *qemu_vmalloc(size_t size)
|
106 | 49b470eb | bellard | { |
107 | 48253bd8 | malc | return qemu_memalign(getpagesize(), size);
|
108 | 49b470eb | bellard | } |
109 | 49b470eb | bellard | |
110 | 49b470eb | bellard | void qemu_vfree(void *ptr) |
111 | 49b470eb | bellard | { |
112 | 49b470eb | bellard | free(ptr); |
113 | 49b470eb | bellard | } |
114 | 49b470eb | bellard | |
115 | 49b470eb | bellard | #endif
|
116 | 49b470eb | bellard | |
117 | aa26bb2d | ths | int qemu_create_pidfile(const char *filename) |
118 | aa26bb2d | ths | { |
119 | aa26bb2d | ths | char buffer[128]; |
120 | aa26bb2d | ths | int len;
|
121 | aa26bb2d | ths | #ifndef _WIN32
|
122 | aa26bb2d | ths | int fd;
|
123 | aa26bb2d | ths | |
124 | aa26bb2d | ths | fd = open(filename, O_RDWR | O_CREAT, 0600);
|
125 | aa26bb2d | ths | if (fd == -1) |
126 | aa26bb2d | ths | return -1; |
127 | aa26bb2d | ths | |
128 | aa26bb2d | ths | if (lockf(fd, F_TLOCK, 0) == -1) |
129 | aa26bb2d | ths | return -1; |
130 | aa26bb2d | ths | |
131 | aa26bb2d | ths | len = snprintf(buffer, sizeof(buffer), "%ld\n", (long)getpid()); |
132 | aa26bb2d | ths | if (write(fd, buffer, len) != len)
|
133 | aa26bb2d | ths | return -1; |
134 | aa26bb2d | ths | #else
|
135 | aa26bb2d | ths | HANDLE file; |
136 | aa26bb2d | ths | DWORD flags; |
137 | aa26bb2d | ths | OVERLAPPED overlap; |
138 | aa26bb2d | ths | BOOL ret; |
139 | aa26bb2d | ths | |
140 | aa26bb2d | ths | /* Open for writing with no sharing. */
|
141 | 5fafdf24 | ths | file = CreateFile(filename, GENERIC_WRITE, 0, NULL, |
142 | aa26bb2d | ths | OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
|
143 | aa26bb2d | ths | |
144 | aa26bb2d | ths | if (file == INVALID_HANDLE_VALUE)
|
145 | aa26bb2d | ths | return -1; |
146 | aa26bb2d | ths | |
147 | aa26bb2d | ths | flags = LOCKFILE_EXCLUSIVE_LOCK | LOCKFILE_FAIL_IMMEDIATELY; |
148 | aa26bb2d | ths | overlap.hEvent = 0;
|
149 | aa26bb2d | ths | /* Lock 1 byte. */
|
150 | aa26bb2d | ths | ret = LockFileEx(file, flags, 0, 0, 1, &overlap); |
151 | aa26bb2d | ths | if (ret == 0) |
152 | aa26bb2d | ths | return -1; |
153 | aa26bb2d | ths | |
154 | aa26bb2d | ths | /* Write PID to file. */
|
155 | aa26bb2d | ths | len = snprintf(buffer, sizeof(buffer), "%ld\n", (long)getpid()); |
156 | 5fafdf24 | ths | ret = WriteFileEx(file, (LPCVOID)buffer, (DWORD)len, |
157 | aa26bb2d | ths | &overlap, NULL);
|
158 | aa26bb2d | ths | if (ret == 0) |
159 | aa26bb2d | ths | return -1; |
160 | aa26bb2d | ths | #endif
|
161 | aa26bb2d | ths | return 0; |
162 | aa26bb2d | ths | } |
163 | 29b3a662 | pbrook | |
164 | 29b3a662 | pbrook | #ifdef _WIN32
|
165 | 29b3a662 | pbrook | |
166 | 29b3a662 | pbrook | /* Offset between 1/1/1601 and 1/1/1970 in 100 nanosec units */
|
167 | 29b3a662 | pbrook | #define _W32_FT_OFFSET (116444736000000000ULL) |
168 | 29b3a662 | pbrook | |
169 | 29b3a662 | pbrook | int qemu_gettimeofday(qemu_timeval *tp)
|
170 | 29b3a662 | pbrook | { |
171 | 29b3a662 | pbrook | union {
|
172 | 29b3a662 | pbrook | unsigned long long ns100; /*time since 1 Jan 1601 in 100ns units */ |
173 | 29b3a662 | pbrook | FILETIME ft; |
174 | 29b3a662 | pbrook | } _now; |
175 | 29b3a662 | pbrook | |
176 | 29b3a662 | pbrook | if(tp)
|
177 | 29b3a662 | pbrook | { |
178 | 29b3a662 | pbrook | GetSystemTimeAsFileTime (&_now.ft); |
179 | 29b3a662 | pbrook | tp->tv_usec=(long)((_now.ns100 / 10ULL) % 1000000ULL ); |
180 | 29b3a662 | pbrook | tp->tv_sec= (long)((_now.ns100 - _W32_FT_OFFSET) / 10000000ULL); |
181 | 29b3a662 | pbrook | } |
182 | 29b3a662 | pbrook | /* Always return 0 as per Open Group Base Specifications Issue 6.
|
183 | 29b3a662 | pbrook | Do not set errno on error. */
|
184 | 29b3a662 | pbrook | return 0; |
185 | 29b3a662 | pbrook | } |
186 | 29b3a662 | pbrook | #endif /* _WIN32 */ |
187 | 03ff3ca3 | aliguori | |
188 | 03ff3ca3 | aliguori | |
189 | 03ff3ca3 | aliguori | #ifdef _WIN32
|
190 | 03ff3ca3 | aliguori | void socket_set_nonblock(int fd) |
191 | 03ff3ca3 | aliguori | { |
192 | 03ff3ca3 | aliguori | unsigned long opt = 1; |
193 | 03ff3ca3 | aliguori | ioctlsocket(fd, FIONBIO, &opt); |
194 | 03ff3ca3 | aliguori | } |
195 | 03ff3ca3 | aliguori | |
196 | 03ff3ca3 | aliguori | int inet_aton(const char *cp, struct in_addr *ia) |
197 | 03ff3ca3 | aliguori | { |
198 | 03ff3ca3 | aliguori | uint32_t addr = inet_addr(cp); |
199 | 03ff3ca3 | aliguori | if (addr == 0xffffffff) |
200 | 03ff3ca3 | aliguori | return 0; |
201 | 03ff3ca3 | aliguori | ia->s_addr = addr; |
202 | 03ff3ca3 | aliguori | return 1; |
203 | 03ff3ca3 | aliguori | } |
204 | 03ff3ca3 | aliguori | #else
|
205 | 03ff3ca3 | aliguori | void socket_set_nonblock(int fd) |
206 | 03ff3ca3 | aliguori | { |
207 | 03ff3ca3 | aliguori | int f;
|
208 | 03ff3ca3 | aliguori | f = fcntl(fd, F_GETFL); |
209 | 03ff3ca3 | aliguori | fcntl(fd, F_SETFL, f | O_NONBLOCK); |
210 | 03ff3ca3 | aliguori | } |
211 | 03ff3ca3 | aliguori | #endif |