osdep.c 8.1 KB

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