]>
Commit | Line | Data |
---|---|---|
c1b0b93b JS |
1 | /* |
2 | * os-posix-lib.c | |
3 | * | |
4 | * Copyright (c) 2003-2008 Fabrice Bellard | |
5 | * Copyright (c) 2010 Red Hat, Inc. | |
6 | * | |
7 | * QEMU library functions on POSIX which are shared between QEMU and | |
8 | * the QEMU tools. | |
9 | * | |
10 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
11 | * of this software and associated documentation files (the "Software"), to deal | |
12 | * in the Software without restriction, including without limitation the rights | |
13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
14 | * copies of the Software, and to permit persons to whom the Software is | |
15 | * furnished to do so, subject to the following conditions: | |
16 | * | |
17 | * The above copyright notice and this permission notice shall be included in | |
18 | * all copies or substantial portions of the Software. | |
19 | * | |
20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
23 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
26 | * THE SOFTWARE. | |
27 | */ | |
28 | ||
aafd7584 | 29 | #include "qemu/osdep.h" |
13401ba0 | 30 | #include <termios.h> |
13401ba0 | 31 | |
e2ea3515 LE |
32 | #include <glib/gprintf.h> |
33 | ||
9c17d615 | 34 | #include "sysemu/sysemu.h" |
c1b0b93b | 35 | #include "trace.h" |
da34e65c | 36 | #include "qapi/error.h" |
1de7afc9 | 37 | #include "qemu/sockets.h" |
10f5bff6 | 38 | #include <libgen.h> |
38183310 | 39 | #include <sys/signal.h> |
f348b6d1 | 40 | #include "qemu/cutils.h" |
c1b0b93b | 41 | |
cbcfa041 PB |
42 | #ifdef CONFIG_LINUX |
43 | #include <sys/syscall.h> | |
44 | #endif | |
cbcfa041 | 45 | |
41975b26 AF |
46 | #ifdef __FreeBSD__ |
47 | #include <sys/sysctl.h> | |
a7764f15 | 48 | #include <sys/user.h> |
7dc9ae43 | 49 | #include <libutil.h> |
41975b26 AF |
50 | #endif |
51 | ||
094611b4 KR |
52 | #ifdef __NetBSD__ |
53 | #include <sys/sysctl.h> | |
54 | #endif | |
55 | ||
a9c94277 | 56 | #include "qemu/mmap-alloc.h" |
794e8f30 | 57 | |
7d992e4d PL |
58 | #ifdef CONFIG_DEBUG_STACK_USAGE |
59 | #include "qemu/error-report.h" | |
60 | #endif | |
61 | ||
dfd0dcc7 | 62 | #define MAX_MEM_PREALLOC_THREAD_COUNT 16 |
1e356fc1 JK |
63 | |
64 | struct MemsetThread { | |
65 | char *addr; | |
e947d47d SW |
66 | size_t numpages; |
67 | size_t hpagesize; | |
1e356fc1 JK |
68 | QemuThread pgthread; |
69 | sigjmp_buf env; | |
70 | }; | |
71 | typedef struct MemsetThread MemsetThread; | |
72 | ||
73 | static MemsetThread *memset_thread; | |
74 | static int memset_num_threads; | |
75 | static bool memset_thread_failed; | |
76 | ||
cbcfa041 PB |
77 | int qemu_get_thread_id(void) |
78 | { | |
79 | #if defined(__linux__) | |
80 | return syscall(SYS_gettid); | |
81 | #else | |
82 | return getpid(); | |
83 | #endif | |
84 | } | |
f97742d0 AR |
85 | |
86 | int qemu_daemon(int nochdir, int noclose) | |
87 | { | |
88 | return daemon(nochdir, noclose); | |
89 | } | |
90 | ||
9e6bdef2 MAL |
91 | bool qemu_write_pidfile(const char *path, Error **errp) |
92 | { | |
93 | int fd; | |
94 | char pidstr[32]; | |
95 | ||
96 | while (1) { | |
97 | struct stat a, b; | |
98 | ||
99 | fd = qemu_open(path, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR); | |
100 | if (fd == -1) { | |
101 | error_setg_errno(errp, errno, "Cannot open pid file"); | |
102 | return false; | |
103 | } | |
104 | ||
105 | if (fstat(fd, &b) < 0) { | |
106 | error_setg_errno(errp, errno, "Cannot stat file"); | |
107 | goto fail_close; | |
108 | } | |
109 | ||
110 | if (lockf(fd, F_TLOCK, 0) < 0) { | |
111 | error_setg_errno(errp, errno, "Cannot lock pid file"); | |
112 | goto fail_close; | |
113 | } | |
114 | ||
115 | /* | |
116 | * Now make sure the path we locked is the same one that now | |
117 | * exists on the filesystem. | |
118 | */ | |
119 | if (stat(path, &a) < 0) { | |
120 | /* | |
121 | * PID file disappeared, someone else must be racing with | |
122 | * us, so try again. | |
123 | */ | |
124 | close(fd); | |
125 | continue; | |
126 | } | |
127 | ||
128 | if (a.st_ino == b.st_ino) { | |
129 | break; | |
130 | } | |
131 | ||
132 | /* | |
133 | * PID file was recreated, someone else must be racing with | |
134 | * us, so try again. | |
135 | */ | |
136 | close(fd); | |
137 | } | |
138 | ||
139 | if (ftruncate(fd, 0) < 0) { | |
140 | error_setg_errno(errp, errno, "Failed to truncate pid file"); | |
141 | goto fail_unlink; | |
142 | } | |
143 | ||
144 | snprintf(pidstr, sizeof(pidstr), FMT_pid "\n", getpid()); | |
145 | if (write(fd, pidstr, strlen(pidstr)) != strlen(pidstr)) { | |
146 | error_setg(errp, "Failed to write pid file"); | |
147 | goto fail_unlink; | |
148 | } | |
149 | ||
150 | return true; | |
151 | ||
152 | fail_unlink: | |
153 | unlink(path); | |
154 | fail_close: | |
155 | close(fd); | |
156 | return false; | |
157 | } | |
158 | ||
b152aa84 | 159 | void *qemu_oom_check(void *ptr) |
c1b0b93b JS |
160 | { |
161 | if (ptr == NULL) { | |
162 | fprintf(stderr, "Failed to allocate memory: %s\n", strerror(errno)); | |
163 | abort(); | |
164 | } | |
165 | return ptr; | |
166 | } | |
c1b0b93b | 167 | |
7d2a35cc | 168 | void *qemu_try_memalign(size_t alignment, size_t size) |
c1b0b93b JS |
169 | { |
170 | void *ptr; | |
e5354657 KW |
171 | |
172 | if (alignment < sizeof(void*)) { | |
173 | alignment = sizeof(void*); | |
174 | } | |
175 | ||
9bc5a719 | 176 | #if defined(CONFIG_POSIX_MEMALIGN) |
c1b0b93b JS |
177 | int ret; |
178 | ret = posix_memalign(&ptr, alignment, size); | |
179 | if (ret != 0) { | |
7d2a35cc KW |
180 | errno = ret; |
181 | ptr = NULL; | |
c1b0b93b JS |
182 | } |
183 | #elif defined(CONFIG_BSD) | |
7d2a35cc | 184 | ptr = valloc(size); |
c1b0b93b | 185 | #else |
7d2a35cc | 186 | ptr = memalign(alignment, size); |
c1b0b93b JS |
187 | #endif |
188 | trace_qemu_memalign(alignment, size, ptr); | |
189 | return ptr; | |
190 | } | |
191 | ||
7d2a35cc KW |
192 | void *qemu_memalign(size_t alignment, size_t size) |
193 | { | |
194 | return qemu_oom_check(qemu_try_memalign(alignment, size)); | |
195 | } | |
196 | ||
c1b0b93b | 197 | /* alloc shared memory pages */ |
06329cce | 198 | void *qemu_anon_ram_alloc(size_t size, uint64_t *alignment, bool shared) |
c1b0b93b | 199 | { |
36b58628 | 200 | size_t align = QEMU_VMALLOC_ALIGN; |
06329cce | 201 | void *ptr = qemu_ram_mmap(-1, size, align, shared); |
36b58628 | 202 | |
7dda5dc8 | 203 | if (ptr == MAP_FAILED) { |
39228250 | 204 | return NULL; |
c2a8238a | 205 | } |
c2a8238a | 206 | |
a2b257d6 IM |
207 | if (alignment) { |
208 | *alignment = align; | |
209 | } | |
c2dfc5ba | 210 | |
6eebf958 | 211 | trace_qemu_anon_ram_alloc(size, ptr); |
c7f4111a | 212 | return ptr; |
c1b0b93b JS |
213 | } |
214 | ||
215 | void qemu_vfree(void *ptr) | |
216 | { | |
217 | trace_qemu_vfree(ptr); | |
218 | free(ptr); | |
219 | } | |
9549e764 | 220 | |
e7a09b92 PB |
221 | void qemu_anon_ram_free(void *ptr, size_t size) |
222 | { | |
223 | trace_qemu_anon_ram_free(ptr, size); | |
794e8f30 | 224 | qemu_ram_munmap(ptr, size); |
e7a09b92 PB |
225 | } |
226 | ||
f9e8cacc | 227 | void qemu_set_block(int fd) |
154b9a0c PB |
228 | { |
229 | int f; | |
230 | f = fcntl(fd, F_GETFL); | |
231 | fcntl(fd, F_SETFL, f & ~O_NONBLOCK); | |
232 | } | |
233 | ||
f9e8cacc | 234 | void qemu_set_nonblock(int fd) |
9549e764 JS |
235 | { |
236 | int f; | |
237 | f = fcntl(fd, F_GETFL); | |
238 | fcntl(fd, F_SETFL, f | O_NONBLOCK); | |
239 | } | |
240 | ||
606600a1 SO |
241 | int socket_set_fast_reuse(int fd) |
242 | { | |
243 | int val = 1, ret; | |
244 | ||
245 | ret = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, | |
246 | (const char *)&val, sizeof(val)); | |
247 | ||
248 | assert(ret == 0); | |
249 | ||
250 | return ret; | |
251 | } | |
252 | ||
9549e764 JS |
253 | void qemu_set_cloexec(int fd) |
254 | { | |
255 | int f; | |
256 | f = fcntl(fd, F_GETFD); | |
7e6478e7 SS |
257 | assert(f != -1); |
258 | f = fcntl(fd, F_SETFD, f | FD_CLOEXEC); | |
259 | assert(f != -1); | |
9549e764 | 260 | } |
70e72ce4 JS |
261 | |
262 | /* | |
263 | * Creates a pipe with FD_CLOEXEC set on both file descriptors | |
264 | */ | |
265 | int qemu_pipe(int pipefd[2]) | |
266 | { | |
267 | int ret; | |
268 | ||
269 | #ifdef CONFIG_PIPE2 | |
270 | ret = pipe2(pipefd, O_CLOEXEC); | |
271 | if (ret != -1 || errno != ENOSYS) { | |
272 | return ret; | |
273 | } | |
274 | #endif | |
275 | ret = pipe(pipefd); | |
276 | if (ret == 0) { | |
277 | qemu_set_cloexec(pipefd[0]); | |
278 | qemu_set_cloexec(pipefd[1]); | |
279 | } | |
280 | ||
281 | return ret; | |
282 | } | |
38671423 | 283 | |
e2ea3515 LE |
284 | char * |
285 | qemu_get_local_state_pathname(const char *relative_pathname) | |
286 | { | |
287 | return g_strdup_printf("%s/%s", CONFIG_QEMU_LOCALSTATEDIR, | |
288 | relative_pathname); | |
289 | } | |
13401ba0 SH |
290 | |
291 | void qemu_set_tty_echo(int fd, bool echo) | |
292 | { | |
293 | struct termios tty; | |
294 | ||
295 | tcgetattr(fd, &tty); | |
296 | ||
297 | if (echo) { | |
298 | tty.c_lflag |= ECHO | ECHONL | ICANON | IEXTEN; | |
299 | } else { | |
300 | tty.c_lflag &= ~(ECHO | ECHONL | ICANON | IEXTEN); | |
301 | } | |
302 | ||
303 | tcsetattr(fd, TCSANOW, &tty); | |
304 | } | |
10f5bff6 FZ |
305 | |
306 | static char exec_dir[PATH_MAX]; | |
307 | ||
308 | void qemu_init_exec_dir(const char *argv0) | |
309 | { | |
310 | char *dir; | |
311 | char *p = NULL; | |
312 | char buf[PATH_MAX]; | |
313 | ||
314 | assert(!exec_dir[0]); | |
315 | ||
316 | #if defined(__linux__) | |
317 | { | |
318 | int len; | |
319 | len = readlink("/proc/self/exe", buf, sizeof(buf) - 1); | |
320 | if (len > 0) { | |
321 | buf[len] = 0; | |
322 | p = buf; | |
323 | } | |
324 | } | |
094611b4 KR |
325 | #elif defined(__FreeBSD__) \ |
326 | || (defined(__NetBSD__) && defined(KERN_PROC_PATHNAME)) | |
10f5bff6 | 327 | { |
094611b4 | 328 | #if defined(__FreeBSD__) |
10f5bff6 | 329 | static int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1}; |
094611b4 KR |
330 | #else |
331 | static int mib[4] = {CTL_KERN, KERN_PROC_ARGS, -1, KERN_PROC_PATHNAME}; | |
332 | #endif | |
10f5bff6 FZ |
333 | size_t len = sizeof(buf) - 1; |
334 | ||
335 | *buf = '\0'; | |
336 | if (!sysctl(mib, ARRAY_SIZE(mib), buf, &len, NULL, 0) && | |
337 | *buf) { | |
338 | buf[sizeof(buf) - 1] = '\0'; | |
339 | p = buf; | |
340 | } | |
341 | } | |
342 | #endif | |
343 | /* If we don't have any way of figuring out the actual executable | |
344 | location then try argv[0]. */ | |
345 | if (!p) { | |
346 | if (!argv0) { | |
347 | return; | |
348 | } | |
349 | p = realpath(argv0, buf); | |
350 | if (!p) { | |
351 | return; | |
352 | } | |
353 | } | |
55ad781c | 354 | dir = g_path_get_dirname(p); |
10f5bff6 FZ |
355 | |
356 | pstrcpy(exec_dir, sizeof(exec_dir), dir); | |
55ad781c WJ |
357 | |
358 | g_free(dir); | |
10f5bff6 FZ |
359 | } |
360 | ||
361 | char *qemu_get_exec_dir(void) | |
362 | { | |
363 | return g_strdup(exec_dir); | |
364 | } | |
38183310 | 365 | |
38183310 PB |
366 | static void sigbus_handler(int signal) |
367 | { | |
1e356fc1 JK |
368 | int i; |
369 | if (memset_thread) { | |
370 | for (i = 0; i < memset_num_threads; i++) { | |
371 | if (qemu_thread_is_self(&memset_thread[i].pgthread)) { | |
372 | siglongjmp(memset_thread[i].env, 1); | |
373 | } | |
374 | } | |
375 | } | |
38183310 PB |
376 | } |
377 | ||
1e356fc1 JK |
378 | static void *do_touch_pages(void *arg) |
379 | { | |
380 | MemsetThread *memset_args = (MemsetThread *)arg; | |
1e356fc1 | 381 | sigset_t set, oldset; |
1e356fc1 JK |
382 | |
383 | /* unblock SIGBUS */ | |
384 | sigemptyset(&set); | |
385 | sigaddset(&set, SIGBUS); | |
386 | pthread_sigmask(SIG_UNBLOCK, &set, &oldset); | |
387 | ||
388 | if (sigsetjmp(memset_args->env, 1)) { | |
389 | memset_thread_failed = true; | |
390 | } else { | |
e947d47d SW |
391 | char *addr = memset_args->addr; |
392 | size_t numpages = memset_args->numpages; | |
393 | size_t hpagesize = memset_args->hpagesize; | |
394 | size_t i; | |
1e356fc1 | 395 | for (i = 0; i < numpages; i++) { |
9dc44aa5 DB |
396 | /* |
397 | * Read & write back the same value, so we don't | |
398 | * corrupt existing user/app data that might be | |
399 | * stored. | |
400 | * | |
401 | * 'volatile' to stop compiler optimizing this away | |
402 | * to a no-op | |
403 | * | |
404 | * TODO: get a better solution from kernel so we | |
405 | * don't need to write at all so we don't cause | |
406 | * wear on the storage backing the region... | |
407 | */ | |
408 | *(volatile char *)addr = *addr; | |
1e356fc1 JK |
409 | addr += hpagesize; |
410 | } | |
411 | } | |
412 | pthread_sigmask(SIG_SETMASK, &oldset, NULL); | |
413 | return NULL; | |
414 | } | |
415 | ||
dfd0dcc7 JK |
416 | static inline int get_memset_num_threads(int smp_cpus) |
417 | { | |
418 | long host_procs = sysconf(_SC_NPROCESSORS_ONLN); | |
419 | int ret = 1; | |
420 | ||
421 | if (host_procs > 0) { | |
422 | ret = MIN(MIN(host_procs, MAX_MEM_PREALLOC_THREAD_COUNT), smp_cpus); | |
423 | } | |
424 | /* In case sysconf() fails, we fall back to single threaded */ | |
425 | return ret; | |
426 | } | |
427 | ||
1e356fc1 JK |
428 | static bool touch_all_pages(char *area, size_t hpagesize, size_t numpages, |
429 | int smp_cpus) | |
430 | { | |
e947d47d SW |
431 | size_t numpages_per_thread; |
432 | size_t size_per_thread; | |
1e356fc1 JK |
433 | char *addr = area; |
434 | int i = 0; | |
435 | ||
436 | memset_thread_failed = false; | |
dfd0dcc7 | 437 | memset_num_threads = get_memset_num_threads(smp_cpus); |
1e356fc1 JK |
438 | memset_thread = g_new0(MemsetThread, memset_num_threads); |
439 | numpages_per_thread = (numpages / memset_num_threads); | |
440 | size_per_thread = (hpagesize * numpages_per_thread); | |
441 | for (i = 0; i < memset_num_threads; i++) { | |
442 | memset_thread[i].addr = addr; | |
443 | memset_thread[i].numpages = (i == (memset_num_threads - 1)) ? | |
444 | numpages : numpages_per_thread; | |
445 | memset_thread[i].hpagesize = hpagesize; | |
446 | qemu_thread_create(&memset_thread[i].pgthread, "touch_pages", | |
447 | do_touch_pages, &memset_thread[i], | |
448 | QEMU_THREAD_JOINABLE); | |
449 | addr += size_per_thread; | |
450 | numpages -= numpages_per_thread; | |
451 | } | |
452 | for (i = 0; i < memset_num_threads; i++) { | |
453 | qemu_thread_join(&memset_thread[i].pgthread); | |
454 | } | |
455 | g_free(memset_thread); | |
456 | memset_thread = NULL; | |
457 | ||
458 | return memset_thread_failed; | |
459 | } | |
460 | ||
461 | void os_mem_prealloc(int fd, char *area, size_t memory, int smp_cpus, | |
462 | Error **errp) | |
38183310 | 463 | { |
b7bf8f56 | 464 | int ret; |
38183310 | 465 | struct sigaction act, oldact; |
1e356fc1 JK |
466 | size_t hpagesize = qemu_fd_getpagesize(fd); |
467 | size_t numpages = DIV_ROUND_UP(memory, hpagesize); | |
38183310 PB |
468 | |
469 | memset(&act, 0, sizeof(act)); | |
470 | act.sa_handler = &sigbus_handler; | |
471 | act.sa_flags = 0; | |
472 | ||
473 | ret = sigaction(SIGBUS, &act, &oldact); | |
474 | if (ret) { | |
056b68af IM |
475 | error_setg_errno(errp, errno, |
476 | "os_mem_prealloc: failed to install signal handler"); | |
477 | return; | |
38183310 PB |
478 | } |
479 | ||
1e356fc1 JK |
480 | /* touch pages simultaneously */ |
481 | if (touch_all_pages(area, hpagesize, numpages, smp_cpus)) { | |
056b68af | 482 | error_setg(errp, "os_mem_prealloc: Insufficient free host memory " |
462e5d50 | 483 | "pages available to allocate guest RAM"); |
056b68af | 484 | } |
38183310 | 485 | |
056b68af IM |
486 | ret = sigaction(SIGBUS, &oldact, NULL); |
487 | if (ret) { | |
488 | /* Terminate QEMU since it can't recover from error */ | |
489 | perror("os_mem_prealloc: failed to reinstall signal handler"); | |
490 | exit(1); | |
b7bf8f56 | 491 | } |
38183310 | 492 | } |
d57e4e48 DB |
493 | |
494 | ||
7dc9ae43 MP |
495 | char *qemu_get_pid_name(pid_t pid) |
496 | { | |
497 | char *name = NULL; | |
498 | ||
499 | #if defined(__FreeBSD__) | |
500 | /* BSDs don't have /proc, but they provide a nice substitute */ | |
501 | struct kinfo_proc *proc = kinfo_getproc(pid); | |
502 | ||
503 | if (proc) { | |
504 | name = g_strdup(proc->ki_comm); | |
505 | free(proc); | |
506 | } | |
507 | #else | |
508 | /* Assume a system with reasonable procfs */ | |
509 | char *pid_path; | |
510 | size_t len; | |
511 | ||
512 | pid_path = g_strdup_printf("/proc/%d/cmdline", pid); | |
513 | g_file_get_contents(pid_path, &name, &len, NULL); | |
514 | g_free(pid_path); | |
515 | #endif | |
516 | ||
517 | return name; | |
518 | } | |
519 | ||
520 | ||
57cb38b3 DB |
521 | pid_t qemu_fork(Error **errp) |
522 | { | |
523 | sigset_t oldmask, newmask; | |
524 | struct sigaction sig_action; | |
525 | int saved_errno; | |
526 | pid_t pid; | |
527 | ||
528 | /* | |
529 | * Need to block signals now, so that child process can safely | |
530 | * kill off caller's signal handlers without a race. | |
531 | */ | |
532 | sigfillset(&newmask); | |
533 | if (pthread_sigmask(SIG_SETMASK, &newmask, &oldmask) != 0) { | |
534 | error_setg_errno(errp, errno, | |
535 | "cannot block signals"); | |
536 | return -1; | |
537 | } | |
538 | ||
539 | pid = fork(); | |
540 | saved_errno = errno; | |
541 | ||
542 | if (pid < 0) { | |
543 | /* attempt to restore signal mask, but ignore failure, to | |
544 | * avoid obscuring the fork failure */ | |
545 | (void)pthread_sigmask(SIG_SETMASK, &oldmask, NULL); | |
546 | error_setg_errno(errp, saved_errno, | |
547 | "cannot fork child process"); | |
548 | errno = saved_errno; | |
549 | return -1; | |
550 | } else if (pid) { | |
551 | /* parent process */ | |
552 | ||
553 | /* Restore our original signal mask now that the child is | |
554 | * safely running. Only documented failures are EFAULT (not | |
555 | * possible, since we are using just-grabbed mask) or EINVAL | |
556 | * (not possible, since we are using correct arguments). */ | |
557 | (void)pthread_sigmask(SIG_SETMASK, &oldmask, NULL); | |
558 | } else { | |
559 | /* child process */ | |
560 | size_t i; | |
561 | ||
562 | /* Clear out all signal handlers from parent so nothing | |
563 | * unexpected can happen in our child once we unblock | |
564 | * signals */ | |
565 | sig_action.sa_handler = SIG_DFL; | |
566 | sig_action.sa_flags = 0; | |
567 | sigemptyset(&sig_action.sa_mask); | |
568 | ||
569 | for (i = 1; i < NSIG; i++) { | |
570 | /* Only possible errors are EFAULT or EINVAL The former | |
571 | * won't happen, the latter we expect, so no need to check | |
572 | * return value */ | |
573 | (void)sigaction(i, &sig_action, NULL); | |
574 | } | |
575 | ||
576 | /* Unmask all signals in child, since we've no idea what the | |
577 | * caller's done with their signal mask and don't want to | |
578 | * propagate that to children */ | |
579 | sigemptyset(&newmask); | |
580 | if (pthread_sigmask(SIG_SETMASK, &newmask, NULL) != 0) { | |
581 | Error *local_err = NULL; | |
582 | error_setg_errno(&local_err, errno, | |
583 | "cannot unblock signals"); | |
584 | error_report_err(local_err); | |
585 | _exit(1); | |
586 | } | |
587 | } | |
588 | return pid; | |
589 | } | |
8737d9e0 PL |
590 | |
591 | void *qemu_alloc_stack(size_t *sz) | |
592 | { | |
593 | void *ptr, *guardpage; | |
7d992e4d PL |
594 | #ifdef CONFIG_DEBUG_STACK_USAGE |
595 | void *ptr2; | |
596 | #endif | |
8737d9e0 PL |
597 | size_t pagesz = getpagesize(); |
598 | #ifdef _SC_THREAD_STACK_MIN | |
599 | /* avoid stacks smaller than _SC_THREAD_STACK_MIN */ | |
600 | long min_stack_sz = sysconf(_SC_THREAD_STACK_MIN); | |
601 | *sz = MAX(MAX(min_stack_sz, 0), *sz); | |
602 | #endif | |
603 | /* adjust stack size to a multiple of the page size */ | |
604 | *sz = ROUND_UP(*sz, pagesz); | |
605 | /* allocate one extra page for the guard page */ | |
606 | *sz += pagesz; | |
607 | ||
608 | ptr = mmap(NULL, *sz, PROT_READ | PROT_WRITE, | |
609 | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); | |
610 | if (ptr == MAP_FAILED) { | |
e916a6e8 | 611 | perror("failed to allocate memory for stack"); |
8737d9e0 PL |
612 | abort(); |
613 | } | |
614 | ||
615 | #if defined(HOST_IA64) | |
616 | /* separate register stack */ | |
617 | guardpage = ptr + (((*sz - pagesz) / 2) & ~pagesz); | |
618 | #elif defined(HOST_HPPA) | |
619 | /* stack grows up */ | |
620 | guardpage = ptr + *sz - pagesz; | |
621 | #else | |
622 | /* stack grows down */ | |
623 | guardpage = ptr; | |
624 | #endif | |
625 | if (mprotect(guardpage, pagesz, PROT_NONE) != 0) { | |
e916a6e8 | 626 | perror("failed to set up stack guard page"); |
8737d9e0 PL |
627 | abort(); |
628 | } | |
629 | ||
7d992e4d PL |
630 | #ifdef CONFIG_DEBUG_STACK_USAGE |
631 | for (ptr2 = ptr + pagesz; ptr2 < ptr + *sz; ptr2 += sizeof(uint32_t)) { | |
632 | *(uint32_t *)ptr2 = 0xdeadbeaf; | |
633 | } | |
634 | #endif | |
635 | ||
8737d9e0 PL |
636 | return ptr; |
637 | } | |
638 | ||
7d992e4d PL |
639 | #ifdef CONFIG_DEBUG_STACK_USAGE |
640 | static __thread unsigned int max_stack_usage; | |
641 | #endif | |
642 | ||
8737d9e0 PL |
643 | void qemu_free_stack(void *stack, size_t sz) |
644 | { | |
7d992e4d PL |
645 | #ifdef CONFIG_DEBUG_STACK_USAGE |
646 | unsigned int usage; | |
647 | void *ptr; | |
648 | ||
649 | for (ptr = stack + getpagesize(); ptr < stack + sz; | |
650 | ptr += sizeof(uint32_t)) { | |
651 | if (*(uint32_t *)ptr != 0xdeadbeaf) { | |
652 | break; | |
653 | } | |
654 | } | |
655 | usage = sz - (uintptr_t) (ptr - stack); | |
656 | if (usage > max_stack_usage) { | |
657 | error_report("thread %d max stack usage increased from %u to %u", | |
658 | qemu_get_thread_id(), max_stack_usage, usage); | |
659 | max_stack_usage = usage; | |
660 | } | |
661 | #endif | |
662 | ||
8737d9e0 PL |
663 | munmap(stack, sz); |
664 | } | |
d98d4072 PB |
665 | |
666 | void sigaction_invoke(struct sigaction *action, | |
667 | struct qemu_signalfd_siginfo *info) | |
668 | { | |
02ffa034 | 669 | siginfo_t si = {}; |
d98d4072 PB |
670 | si.si_signo = info->ssi_signo; |
671 | si.si_errno = info->ssi_errno; | |
672 | si.si_code = info->ssi_code; | |
673 | ||
674 | /* Convert the minimal set of fields defined by POSIX. | |
675 | * Positive si_code values are reserved for kernel-generated | |
676 | * signals, where the valid siginfo fields are determined by | |
677 | * the signal number. But according to POSIX, it is unspecified | |
678 | * whether SI_USER and SI_QUEUE have values less than or equal to | |
679 | * zero. | |
680 | */ | |
681 | if (info->ssi_code == SI_USER || info->ssi_code == SI_QUEUE || | |
682 | info->ssi_code <= 0) { | |
683 | /* SIGTERM, etc. */ | |
684 | si.si_pid = info->ssi_pid; | |
685 | si.si_uid = info->ssi_uid; | |
686 | } else if (info->ssi_signo == SIGILL || info->ssi_signo == SIGFPE || | |
687 | info->ssi_signo == SIGSEGV || info->ssi_signo == SIGBUS) { | |
688 | si.si_addr = (void *)(uintptr_t)info->ssi_addr; | |
689 | } else if (info->ssi_signo == SIGCHLD) { | |
690 | si.si_pid = info->ssi_pid; | |
691 | si.si_status = info->ssi_status; | |
692 | si.si_uid = info->ssi_uid; | |
d98d4072 PB |
693 | } |
694 | action->sa_sigaction(info->ssi_signo, &si, NULL); | |
695 | } |