Statistics
| Branch: | Revision:

root / osdep.c @ ab19b0ec

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