Statistics
| Branch: | Revision:

root / osdep.c @ 4d3b6f6e

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