Statistics
| Branch: | Revision:

root / osdep.c @ 151f7749

History | View | Annotate | Download (8.4 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 605686cd ths
#ifdef HOST_SOLARIS
32 605686cd ths
#include <sys/types.h>
33 605686cd ths
#include <sys/statvfs.h>
34 605686cd ths
#endif
35 ea88812f bellard
36 88ca2a59 Paul Brook
/* FIXME: This file should be target independent. However it has kqemu
37 88ca2a59 Paul Brook
   hacks, so must be built for every target.  */
38 88ca2a59 Paul Brook
39 179a2c19 blueswir1
/* Needed early for HOST_BSD etc. */
40 d40cdb10 blueswir1
#include "config-host.h"
41 d40cdb10 blueswir1
42 6e4255f6 bellard
#ifdef _WIN32
43 6e4255f6 bellard
#include <windows.h>
44 179a2c19 blueswir1
#elif defined(HOST_BSD)
45 194884dd bellard
#include <stdlib.h>
46 194884dd bellard
#else
47 49b470eb bellard
#include <malloc.h>
48 194884dd bellard
#endif
49 49b470eb bellard
50 511d2b14 blueswir1
#include "qemu-common.h"
51 511d2b14 blueswir1
#include "sysemu.h"
52 03ff3ca3 aliguori
#include "qemu_socket.h"
53 03ff3ca3 aliguori
54 6e4255f6 bellard
#if defined(_WIN32)
55 33f00271 balrog
void *qemu_memalign(size_t alignment, size_t size)
56 33f00271 balrog
{
57 33f00271 balrog
    return VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE);
58 33f00271 balrog
}
59 6e4255f6 bellard
60 6e4255f6 bellard
void *qemu_vmalloc(size_t size)
61 6e4255f6 bellard
{
62 6e4255f6 bellard
    /* FIXME: this is not exactly optimal solution since VirtualAlloc
63 6e4255f6 bellard
       has 64Kb granularity, but at least it guarantees us that the
64 6e4255f6 bellard
       memory is page aligned. */
65 6e4255f6 bellard
    return VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE);
66 6e4255f6 bellard
}
67 6e4255f6 bellard
68 6e4255f6 bellard
void qemu_vfree(void *ptr)
69 6e4255f6 bellard
{
70 6e4255f6 bellard
    VirtualFree(ptr, 0, MEM_RELEASE);
71 6e4255f6 bellard
}
72 6e4255f6 bellard
73 6cb7ee85 pbrook
#else
74 6cb7ee85 pbrook
75 640f42e4 blueswir1
#if defined(CONFIG_KQEMU)
76 49b470eb bellard
77 128ab2ff blueswir1
#ifdef __OpenBSD__
78 128ab2ff blueswir1
#include <sys/param.h>
79 128ab2ff blueswir1
#include <sys/types.h>
80 128ab2ff blueswir1
#include <sys/mount.h>
81 128ab2ff blueswir1
#else
82 5f8712aa blueswir1
#ifndef __FreeBSD__
83 6bae7ed8 bellard
#include <sys/vfs.h>
84 128ab2ff blueswir1
#endif
85 5f8712aa blueswir1
#endif
86 128ab2ff blueswir1
87 49b470eb bellard
#include <sys/mman.h>
88 49b470eb bellard
#include <fcntl.h>
89 49b470eb bellard
90 9596ebb7 pbrook
static void *kqemu_vmalloc(size_t size)
91 49b470eb bellard
{
92 49b470eb bellard
    static int phys_ram_fd = -1;
93 49b470eb bellard
    static int phys_ram_size = 0;
94 128ab2ff blueswir1
    void *ptr;
95 128ab2ff blueswir1
96 5f8712aa blueswir1
/* no need (?) for a dummy file on OpenBSD/FreeBSD */
97 c5e97233 blueswir1
#if defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__DragonFly__)
98 128ab2ff blueswir1
    int map_anon = MAP_ANON;
99 128ab2ff blueswir1
#else
100 128ab2ff blueswir1
    int map_anon = 0;
101 49b470eb bellard
    const char *tmpdir;
102 49b470eb bellard
    char phys_ram_file[1024];
103 605686cd ths
#ifdef HOST_SOLARIS
104 605686cd ths
    struct statvfs stfs;
105 605686cd ths
#else
106 6bae7ed8 bellard
    struct statfs stfs;
107 605686cd ths
#endif
108 49b470eb bellard
109 49b470eb bellard
    if (phys_ram_fd < 0) {
110 49b470eb bellard
        tmpdir = getenv("QEMU_TMPDIR");
111 49b470eb bellard
        if (!tmpdir)
112 605686cd ths
#ifdef HOST_SOLARIS
113 605686cd ths
            tmpdir = "/tmp";
114 605686cd ths
        if (statvfs(tmpdir, &stfs) == 0) {
115 605686cd ths
#else
116 49b470eb bellard
            tmpdir = "/dev/shm";
117 6bae7ed8 bellard
        if (statfs(tmpdir, &stfs) == 0) {
118 605686cd ths
#endif
119 6bae7ed8 bellard
            int64_t free_space;
120 6bae7ed8 bellard
            int ram_mb;
121 6bae7ed8 bellard
122 6bae7ed8 bellard
            free_space = (int64_t)stfs.f_bavail * stfs.f_bsize;
123 6bae7ed8 bellard
            if ((ram_size + 8192 * 1024) >= free_space) {
124 6bae7ed8 bellard
                ram_mb = (ram_size / (1024 * 1024));
125 5fafdf24 ths
                fprintf(stderr,
126 6bae7ed8 bellard
                        "You do not have enough space in '%s' for the %d MB of QEMU virtual RAM.\n",
127 6bae7ed8 bellard
                        tmpdir, ram_mb);
128 6bae7ed8 bellard
                if (strcmp(tmpdir, "/dev/shm") == 0) {
129 6bae7ed8 bellard
                    fprintf(stderr, "To have more space available provided you have enough RAM and swap, do as root:\n"
130 2520c665 aurel32
                            "mount -o remount,size=%dm /dev/shm\n",
131 6bae7ed8 bellard
                            ram_mb + 16);
132 6bae7ed8 bellard
                } else {
133 5fafdf24 ths
                    fprintf(stderr,
134 6bae7ed8 bellard
                            "Use the '-m' option of QEMU to diminish the amount of virtual RAM or use the\n"
135 6bae7ed8 bellard
                            "QEMU_TMPDIR environment variable to set another directory where the QEMU\n"
136 6bae7ed8 bellard
                            "temporary RAM file will be opened.\n");
137 6bae7ed8 bellard
                }
138 6cb7ee85 pbrook
                fprintf(stderr, "Or disable the accelerator module with -no-kqemu\n");
139 6bae7ed8 bellard
                exit(1);
140 6bae7ed8 bellard
            }
141 6bae7ed8 bellard
        }
142 5fafdf24 ths
        snprintf(phys_ram_file, sizeof(phys_ram_file), "%s/qemuXXXXXX",
143 49b470eb bellard
                 tmpdir);
144 243a273e bellard
        phys_ram_fd = mkstemp(phys_ram_file);
145 243a273e bellard
        if (phys_ram_fd < 0) {
146 5fafdf24 ths
            fprintf(stderr,
147 49b470eb bellard
                    "warning: could not create temporary file in '%s'.\n"
148 49b470eb bellard
                    "Use QEMU_TMPDIR to select a directory in a tmpfs filesystem.\n"
149 49b470eb bellard
                    "Using '/tmp' as fallback.\n",
150 49b470eb bellard
                    tmpdir);
151 5fafdf24 ths
            snprintf(phys_ram_file, sizeof(phys_ram_file), "%s/qemuXXXXXX",
152 49b470eb bellard
                     "/tmp");
153 243a273e bellard
            phys_ram_fd = mkstemp(phys_ram_file);
154 243a273e bellard
            if (phys_ram_fd < 0) {
155 5fafdf24 ths
                fprintf(stderr, "Could not create temporary memory file '%s'\n",
156 49b470eb bellard
                        phys_ram_file);
157 49b470eb bellard
                exit(1);
158 49b470eb bellard
            }
159 49b470eb bellard
        }
160 49b470eb bellard
        unlink(phys_ram_file);
161 49b470eb bellard
    }
162 49b470eb bellard
    size = (size + 4095) & ~4095;
163 49b470eb bellard
    ftruncate(phys_ram_fd, phys_ram_size + size);
164 c5e97233 blueswir1
#endif /* !(__OpenBSD__ || __FreeBSD__ || __DragonFly__) */
165 5fafdf24 ths
    ptr = mmap(NULL,
166 5fafdf24 ths
               size,
167 128ab2ff blueswir1
               PROT_WRITE | PROT_READ, map_anon | MAP_SHARED,
168 49b470eb bellard
               phys_ram_fd, phys_ram_size);
169 49b470eb bellard
    if (ptr == MAP_FAILED) {
170 49b470eb bellard
        fprintf(stderr, "Could not map physical memory\n");
171 49b470eb bellard
        exit(1);
172 49b470eb bellard
    }
173 49b470eb bellard
    phys_ram_size += size;
174 49b470eb bellard
    return ptr;
175 49b470eb bellard
}
176 49b470eb bellard
177 9596ebb7 pbrook
static void kqemu_vfree(void *ptr)
178 49b470eb bellard
{
179 49b470eb bellard
    /* may be useful some day, but currently we do not need to free */
180 49b470eb bellard
}
181 49b470eb bellard
182 6cb7ee85 pbrook
#endif
183 49b470eb bellard
184 33f00271 balrog
void *qemu_memalign(size_t alignment, size_t size)
185 33f00271 balrog
{
186 33f00271 balrog
#if defined(_POSIX_C_SOURCE)
187 33f00271 balrog
    int ret;
188 33f00271 balrog
    void *ptr;
189 33f00271 balrog
    ret = posix_memalign(&ptr, alignment, size);
190 33f00271 balrog
    if (ret != 0)
191 33f00271 balrog
        return NULL;
192 33f00271 balrog
    return ptr;
193 179a2c19 blueswir1
#elif defined(HOST_BSD)
194 33f00271 balrog
    return valloc(size);
195 33f00271 balrog
#else
196 33f00271 balrog
    return memalign(alignment, size);
197 33f00271 balrog
#endif
198 33f00271 balrog
}
199 33f00271 balrog
200 49b470eb bellard
/* alloc shared memory pages */
201 49b470eb bellard
void *qemu_vmalloc(size_t size)
202 49b470eb bellard
{
203 640f42e4 blueswir1
#if defined(CONFIG_KQEMU)
204 6cb7ee85 pbrook
    if (kqemu_allowed)
205 6cb7ee85 pbrook
        return kqemu_vmalloc(size);
206 6cb7ee85 pbrook
#endif
207 48253bd8 malc
    return qemu_memalign(getpagesize(), size);
208 49b470eb bellard
}
209 49b470eb bellard
210 49b470eb bellard
void qemu_vfree(void *ptr)
211 49b470eb bellard
{
212 640f42e4 blueswir1
#if defined(CONFIG_KQEMU)
213 6cb7ee85 pbrook
    if (kqemu_allowed)
214 6cb7ee85 pbrook
        kqemu_vfree(ptr);
215 6cb7ee85 pbrook
#endif
216 49b470eb bellard
    free(ptr);
217 49b470eb bellard
}
218 49b470eb bellard
219 49b470eb bellard
#endif
220 49b470eb bellard
221 aa26bb2d ths
int qemu_create_pidfile(const char *filename)
222 aa26bb2d ths
{
223 aa26bb2d ths
    char buffer[128];
224 aa26bb2d ths
    int len;
225 aa26bb2d ths
#ifndef _WIN32
226 aa26bb2d ths
    int fd;
227 aa26bb2d ths
228 aa26bb2d ths
    fd = open(filename, O_RDWR | O_CREAT, 0600);
229 aa26bb2d ths
    if (fd == -1)
230 aa26bb2d ths
        return -1;
231 aa26bb2d ths
232 aa26bb2d ths
    if (lockf(fd, F_TLOCK, 0) == -1)
233 aa26bb2d ths
        return -1;
234 aa26bb2d ths
235 aa26bb2d ths
    len = snprintf(buffer, sizeof(buffer), "%ld\n", (long)getpid());
236 aa26bb2d ths
    if (write(fd, buffer, len) != len)
237 aa26bb2d ths
        return -1;
238 aa26bb2d ths
#else
239 aa26bb2d ths
    HANDLE file;
240 aa26bb2d ths
    DWORD flags;
241 aa26bb2d ths
    OVERLAPPED overlap;
242 aa26bb2d ths
    BOOL ret;
243 aa26bb2d ths
244 aa26bb2d ths
    /* Open for writing with no sharing. */
245 5fafdf24 ths
    file = CreateFile(filename, GENERIC_WRITE, 0, NULL,
246 aa26bb2d ths
                      OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
247 aa26bb2d ths
248 aa26bb2d ths
    if (file == INVALID_HANDLE_VALUE)
249 aa26bb2d ths
      return -1;
250 aa26bb2d ths
251 aa26bb2d ths
    flags = LOCKFILE_EXCLUSIVE_LOCK | LOCKFILE_FAIL_IMMEDIATELY;
252 aa26bb2d ths
    overlap.hEvent = 0;
253 aa26bb2d ths
    /* Lock 1 byte. */
254 aa26bb2d ths
    ret = LockFileEx(file, flags, 0, 0, 1, &overlap);
255 aa26bb2d ths
    if (ret == 0)
256 aa26bb2d ths
      return -1;
257 aa26bb2d ths
258 aa26bb2d ths
    /* Write PID to file. */
259 aa26bb2d ths
    len = snprintf(buffer, sizeof(buffer), "%ld\n", (long)getpid());
260 5fafdf24 ths
    ret = WriteFileEx(file, (LPCVOID)buffer, (DWORD)len,
261 aa26bb2d ths
                      &overlap, NULL);
262 aa26bb2d ths
    if (ret == 0)
263 aa26bb2d ths
      return -1;
264 aa26bb2d ths
#endif
265 aa26bb2d ths
    return 0;
266 aa26bb2d ths
}
267 29b3a662 pbrook
268 29b3a662 pbrook
#ifdef _WIN32
269 29b3a662 pbrook
270 29b3a662 pbrook
/* Offset between 1/1/1601 and 1/1/1970 in 100 nanosec units */
271 29b3a662 pbrook
#define _W32_FT_OFFSET (116444736000000000ULL)
272 29b3a662 pbrook
273 29b3a662 pbrook
int qemu_gettimeofday(qemu_timeval *tp)
274 29b3a662 pbrook
{
275 29b3a662 pbrook
  union {
276 29b3a662 pbrook
    unsigned long long ns100; /*time since 1 Jan 1601 in 100ns units */
277 29b3a662 pbrook
    FILETIME ft;
278 29b3a662 pbrook
  }  _now;
279 29b3a662 pbrook
280 29b3a662 pbrook
  if(tp)
281 29b3a662 pbrook
    {
282 29b3a662 pbrook
      GetSystemTimeAsFileTime (&_now.ft);
283 29b3a662 pbrook
      tp->tv_usec=(long)((_now.ns100 / 10ULL) % 1000000ULL );
284 29b3a662 pbrook
      tp->tv_sec= (long)((_now.ns100 - _W32_FT_OFFSET) / 10000000ULL);
285 29b3a662 pbrook
    }
286 29b3a662 pbrook
  /* Always return 0 as per Open Group Base Specifications Issue 6.
287 29b3a662 pbrook
     Do not set errno on error.  */
288 29b3a662 pbrook
  return 0;
289 29b3a662 pbrook
}
290 29b3a662 pbrook
#endif /* _WIN32 */
291 03ff3ca3 aliguori
292 03ff3ca3 aliguori
293 03ff3ca3 aliguori
#ifdef _WIN32
294 03ff3ca3 aliguori
void socket_set_nonblock(int fd)
295 03ff3ca3 aliguori
{
296 03ff3ca3 aliguori
    unsigned long opt = 1;
297 03ff3ca3 aliguori
    ioctlsocket(fd, FIONBIO, &opt);
298 03ff3ca3 aliguori
}
299 03ff3ca3 aliguori
300 03ff3ca3 aliguori
int inet_aton(const char *cp, struct in_addr *ia)
301 03ff3ca3 aliguori
{
302 03ff3ca3 aliguori
    uint32_t addr = inet_addr(cp);
303 03ff3ca3 aliguori
    if (addr == 0xffffffff)
304 03ff3ca3 aliguori
        return 0;
305 03ff3ca3 aliguori
    ia->s_addr = addr;
306 03ff3ca3 aliguori
    return 1;
307 03ff3ca3 aliguori
}
308 03ff3ca3 aliguori
#else
309 03ff3ca3 aliguori
void socket_set_nonblock(int fd)
310 03ff3ca3 aliguori
{
311 03ff3ca3 aliguori
    int f;
312 03ff3ca3 aliguori
    f = fcntl(fd, F_GETFL);
313 03ff3ca3 aliguori
    fcntl(fd, F_SETFL, f | O_NONBLOCK);
314 03ff3ca3 aliguori
}
315 03ff3ca3 aliguori
#endif