]> git.proxmox.com Git - qemu.git/blame - osdep.c
Fix USB buffer size, by Herbert Xu.
[qemu.git] / osdep.c
CommitLineData
ea88812f
FB
1/*
2 * QEMU low level functions
3 *
4 * Copyright (c) 2003 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24#include <stdlib.h>
25#include <stdio.h>
26#include <stdarg.h>
27#include <string.h>
ea88812f
FB
28#include <errno.h>
29#include <unistd.h>
30
31#include "cpu.h"
6cb7ee85
PB
32#if defined(USE_KQEMU)
33#include "vl.h"
34#endif
ea88812f 35
6e4255f6
FB
36#ifdef _WIN32
37#include <windows.h>
38#elif defined(_BSD)
194884dd
FB
39#include <stdlib.h>
40#else
49b470eb 41#include <malloc.h>
194884dd 42#endif
49b470eb 43
ea88812f
FB
44void *get_mmap_addr(unsigned long size)
45{
46 return NULL;
47}
48
49void qemu_free(void *ptr)
50{
51 free(ptr);
52}
53
54void *qemu_malloc(size_t size)
55{
56 return malloc(size);
57}
58
6e4255f6
FB
59#if defined(_WIN32)
60
61void *qemu_vmalloc(size_t size)
62{
63 /* FIXME: this is not exactly optimal solution since VirtualAlloc
64 has 64Kb granularity, but at least it guarantees us that the
65 memory is page aligned. */
66 return VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE);
67}
68
69void qemu_vfree(void *ptr)
70{
71 VirtualFree(ptr, 0, MEM_RELEASE);
72}
73
6cb7ee85
PB
74#else
75
76#if defined(USE_KQEMU)
49b470eb 77
6bae7ed8 78#include <sys/vfs.h>
49b470eb
FB
79#include <sys/mman.h>
80#include <fcntl.h>
81
6cb7ee85 82void *kqemu_vmalloc(size_t size)
49b470eb
FB
83{
84 static int phys_ram_fd = -1;
85 static int phys_ram_size = 0;
86 const char *tmpdir;
87 char phys_ram_file[1024];
88 void *ptr;
6bae7ed8 89 struct statfs stfs;
49b470eb
FB
90
91 if (phys_ram_fd < 0) {
92 tmpdir = getenv("QEMU_TMPDIR");
93 if (!tmpdir)
94 tmpdir = "/dev/shm";
6bae7ed8
FB
95 if (statfs(tmpdir, &stfs) == 0) {
96 int64_t free_space;
97 int ram_mb;
98
99 extern int ram_size;
100 free_space = (int64_t)stfs.f_bavail * stfs.f_bsize;
101 if ((ram_size + 8192 * 1024) >= free_space) {
102 ram_mb = (ram_size / (1024 * 1024));
103 fprintf(stderr,
104 "You do not have enough space in '%s' for the %d MB of QEMU virtual RAM.\n",
105 tmpdir, ram_mb);
106 if (strcmp(tmpdir, "/dev/shm") == 0) {
107 fprintf(stderr, "To have more space available provided you have enough RAM and swap, do as root:\n"
108 "umount /dev/shm\n"
109 "mount -t tmpfs -o size=%dm none /dev/shm\n",
110 ram_mb + 16);
111 } else {
112 fprintf(stderr,
113 "Use the '-m' option of QEMU to diminish the amount of virtual RAM or use the\n"
114 "QEMU_TMPDIR environment variable to set another directory where the QEMU\n"
115 "temporary RAM file will be opened.\n");
116 }
6cb7ee85 117 fprintf(stderr, "Or disable the accelerator module with -no-kqemu\n");
6bae7ed8
FB
118 exit(1);
119 }
120 }
49b470eb
FB
121 snprintf(phys_ram_file, sizeof(phys_ram_file), "%s/qemuXXXXXX",
122 tmpdir);
243a273e
FB
123 phys_ram_fd = mkstemp(phys_ram_file);
124 if (phys_ram_fd < 0) {
49b470eb
FB
125 fprintf(stderr,
126 "warning: could not create temporary file in '%s'.\n"
127 "Use QEMU_TMPDIR to select a directory in a tmpfs filesystem.\n"
128 "Using '/tmp' as fallback.\n",
129 tmpdir);
130 snprintf(phys_ram_file, sizeof(phys_ram_file), "%s/qemuXXXXXX",
131 "/tmp");
243a273e
FB
132 phys_ram_fd = mkstemp(phys_ram_file);
133 if (phys_ram_fd < 0) {
49b470eb
FB
134 fprintf(stderr, "Could not create temporary memory file '%s'\n",
135 phys_ram_file);
136 exit(1);
137 }
138 }
49b470eb
FB
139 unlink(phys_ram_file);
140 }
141 size = (size + 4095) & ~4095;
142 ftruncate(phys_ram_fd, phys_ram_size + size);
143 ptr = mmap(NULL,
144 size,
145 PROT_WRITE | PROT_READ, MAP_SHARED,
146 phys_ram_fd, phys_ram_size);
147 if (ptr == MAP_FAILED) {
148 fprintf(stderr, "Could not map physical memory\n");
149 exit(1);
150 }
151 phys_ram_size += size;
152 return ptr;
153}
154
6cb7ee85 155void kqemu_vfree(void *ptr)
49b470eb
FB
156{
157 /* may be useful some day, but currently we do not need to free */
158}
159
6cb7ee85 160#endif
49b470eb
FB
161
162/* alloc shared memory pages */
163void *qemu_vmalloc(size_t size)
164{
6cb7ee85
PB
165#if defined(USE_KQEMU)
166 if (kqemu_allowed)
167 return kqemu_vmalloc(size);
168#endif
49b470eb
FB
169#ifdef _BSD
170 return valloc(size);
171#else
172 return memalign(4096, size);
173#endif
174}
175
176void qemu_vfree(void *ptr)
177{
6cb7ee85
PB
178#if defined(USE_KQEMU)
179 if (kqemu_allowed)
180 kqemu_vfree(ptr);
181#endif
49b470eb
FB
182 free(ptr);
183}
184
185#endif
186
07d89866
FB
187void *qemu_mallocz(size_t size)
188{
189 void *ptr;
190 ptr = qemu_malloc(size);
191 if (!ptr)
192 return NULL;
193 memset(ptr, 0, size);
194 return ptr;
195}
196
2571929a
FB
197char *qemu_strdup(const char *str)
198{
199 char *ptr;
200 ptr = qemu_malloc(strlen(str) + 1);
201 if (!ptr)
202 return NULL;
203 strcpy(ptr, str);
204 return ptr;
205}