]> git.proxmox.com Git - mirror_qemu.git/blame - util/oslib-posix.c
Merge tag 'misc-fixes-20231113' of https://github.com/philmd/qemu into staging
[mirror_qemu.git] / util / oslib-posix.c
CommitLineData
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"
29b838c0 37#include "qemu/error-report.h"
b85ea5fa 38#include "qemu/madvise.h"
1de7afc9 39#include "qemu/sockets.h"
db725815 40#include "qemu/thread.h"
10f5bff6 41#include <libgen.h>
f348b6d1 42#include "qemu/cutils.h"
89aec641 43#include "qemu/units.h"
e2de2c49 44#include "qemu/thread-context.h"
c1b0b93b 45
cbcfa041
PB
46#ifdef CONFIG_LINUX
47#include <sys/syscall.h>
48#endif
cbcfa041 49
41975b26 50#ifdef __FreeBSD__
9548a891 51#include <sys/thr.h>
06680b15 52#include <sys/user.h>
7dc9ae43 53#include <libutil.h>
41975b26
AF
54#endif
55
094611b4 56#ifdef __NetBSD__
9548a891 57#include <lwp.h>
094611b4
KR
58#endif
59
a9c94277 60#include "qemu/mmap-alloc.h"
794e8f30 61
dfd0dcc7 62#define MAX_MEM_PREALLOC_THREAD_COUNT 16
1e356fc1 63
dba50678
DH
64struct MemsetThread;
65
66typedef struct MemsetContext {
67 bool all_threads_created;
68 bool any_thread_failed;
69 struct MemsetThread *threads;
70 int num_threads;
71} MemsetContext;
72
1e356fc1
JK
73struct MemsetThread {
74 char *addr;
e947d47d
SW
75 size_t numpages;
76 size_t hpagesize;
1e356fc1
JK
77 QemuThread pgthread;
78 sigjmp_buf env;
dba50678 79 MemsetContext *context;
1e356fc1
JK
80};
81typedef struct MemsetThread MemsetThread;
82
dba50678
DH
83/* used by sigbus_handler() */
84static MemsetContext *sigbus_memset_context;
29b838c0 85struct sigaction sigbus_oldact;
a960d664 86static QemuMutex sigbus_mutex;
1e356fc1 87
037fb5eb 88static QemuMutex page_mutex;
89static QemuCond page_cond;
037fb5eb 90
cbcfa041
PB
91int qemu_get_thread_id(void)
92{
93#if defined(__linux__)
94 return syscall(SYS_gettid);
9548a891
DC
95#elif defined(__FreeBSD__)
96 /* thread id is up to INT_MAX */
97 long tid;
98 thr_self(&tid);
99 return (int)tid;
100#elif defined(__NetBSD__)
101 return _lwp_self();
8edbca51
DC
102#elif defined(__OpenBSD__)
103 return getthrid();
cbcfa041
PB
104#else
105 return getpid();
106#endif
107}
f97742d0
AR
108
109int qemu_daemon(int nochdir, int noclose)
110{
111 return daemon(nochdir, noclose);
112}
113
9e6bdef2
MAL
114bool qemu_write_pidfile(const char *path, Error **errp)
115{
116 int fd;
117 char pidstr[32];
118
119 while (1) {
120 struct stat a, b;
35f7f3fb
MAL
121 struct flock lock = {
122 .l_type = F_WRLCK,
123 .l_whence = SEEK_SET,
124 .l_len = 0,
125 };
9e6bdef2 126
1b34d08f 127 fd = qemu_create(path, O_WRONLY, S_IRUSR | S_IWUSR, errp);
9e6bdef2 128 if (fd == -1) {
9e6bdef2
MAL
129 return false;
130 }
131
132 if (fstat(fd, &b) < 0) {
133 error_setg_errno(errp, errno, "Cannot stat file");
134 goto fail_close;
135 }
136
35f7f3fb 137 if (fcntl(fd, F_SETLK, &lock)) {
9e6bdef2
MAL
138 error_setg_errno(errp, errno, "Cannot lock pid file");
139 goto fail_close;
140 }
141
142 /*
143 * Now make sure the path we locked is the same one that now
144 * exists on the filesystem.
145 */
146 if (stat(path, &a) < 0) {
147 /*
148 * PID file disappeared, someone else must be racing with
149 * us, so try again.
150 */
151 close(fd);
152 continue;
153 }
154
155 if (a.st_ino == b.st_ino) {
156 break;
157 }
158
159 /*
160 * PID file was recreated, someone else must be racing with
161 * us, so try again.
162 */
163 close(fd);
164 }
165
166 if (ftruncate(fd, 0) < 0) {
167 error_setg_errno(errp, errno, "Failed to truncate pid file");
168 goto fail_unlink;
169 }
170
171 snprintf(pidstr, sizeof(pidstr), FMT_pid "\n", getpid());
96eb9b2b 172 if (qemu_write_full(fd, pidstr, strlen(pidstr)) != strlen(pidstr)) {
9e6bdef2
MAL
173 error_setg(errp, "Failed to write pid file");
174 goto fail_unlink;
175 }
176
177 return true;
178
179fail_unlink:
180 unlink(path);
181fail_close:
182 close(fd);
183 return false;
184}
185
c1b0b93b 186/* alloc shared memory pages */
8dbe22c6
DH
187void *qemu_anon_ram_alloc(size_t size, uint64_t *alignment, bool shared,
188 bool noreserve)
c1b0b93b 189{
8dbe22c6
DH
190 const uint32_t qemu_map_flags = (shared ? QEMU_MAP_SHARED : 0) |
191 (noreserve ? QEMU_MAP_NORESERVE : 0);
36b58628 192 size_t align = QEMU_VMALLOC_ALIGN;
b444f5c0 193 void *ptr = qemu_ram_mmap(-1, size, align, qemu_map_flags, 0);
36b58628 194
7dda5dc8 195 if (ptr == MAP_FAILED) {
39228250 196 return NULL;
c2a8238a 197 }
c2a8238a 198
a2b257d6
IM
199 if (alignment) {
200 *alignment = align;
201 }
c2dfc5ba 202
6eebf958 203 trace_qemu_anon_ram_alloc(size, ptr);
c7f4111a 204 return ptr;
c1b0b93b
JS
205}
206
e7a09b92
PB
207void qemu_anon_ram_free(void *ptr, size_t size)
208{
209 trace_qemu_anon_ram_free(ptr, size);
53adb9d4 210 qemu_ram_munmap(-1, ptr, size);
e7a09b92
PB
211}
212
ff5927ba 213void qemu_socket_set_block(int fd)
154b9a0c 214{
22e135fc 215 g_unix_set_fd_nonblocking(fd, false, NULL);
154b9a0c
PB
216}
217
ff5927ba 218int qemu_socket_try_set_nonblock(int fd)
9549e764 219{
22e135fc 220 return g_unix_set_fd_nonblocking(fd, true, NULL) ? 0 : -errno;
894022e6
LV
221}
222
ff5927ba 223void qemu_socket_set_nonblock(int fd)
894022e6
LV
224{
225 int f;
ff5927ba 226 f = qemu_socket_try_set_nonblock(fd);
894022e6 227 assert(f == 0);
9549e764
JS
228}
229
606600a1
SO
230int socket_set_fast_reuse(int fd)
231{
232 int val = 1, ret;
233
234 ret = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
235 (const char *)&val, sizeof(val));
236
237 assert(ret == 0);
238
239 return ret;
240}
241
9549e764
JS
242void qemu_set_cloexec(int fd)
243{
244 int f;
245 f = fcntl(fd, F_GETFD);
7e6478e7
SS
246 assert(f != -1);
247 f = fcntl(fd, F_SETFD, f | FD_CLOEXEC);
248 assert(f != -1);
9549e764 249}
70e72ce4 250
3c63b4e9
GT
251int qemu_socketpair(int domain, int type, int protocol, int sv[2])
252{
253 int ret;
254
255#ifdef SOCK_CLOEXEC
256 ret = socketpair(domain, type | SOCK_CLOEXEC, protocol, sv);
257 if (ret != -1 || errno != EINVAL) {
258 return ret;
259 }
260#endif
261 ret = socketpair(domain, type, protocol, sv);;
262 if (ret == 0) {
263 qemu_set_cloexec(sv[0]);
264 qemu_set_cloexec(sv[1]);
265 }
266
267 return ret;
268}
269
e2ea3515 270char *
1fbf2665 271qemu_get_local_state_dir(void)
e2ea3515 272{
1fbf2665 273 return get_relocated_path(CONFIG_QEMU_LOCALSTATEDIR);
e2ea3515 274}
13401ba0
SH
275
276void qemu_set_tty_echo(int fd, bool echo)
277{
278 struct termios tty;
279
280 tcgetattr(fd, &tty);
281
282 if (echo) {
283 tty.c_lflag |= ECHO | ECHONL | ICANON | IEXTEN;
284 } else {
285 tty.c_lflag &= ~(ECHO | ECHONL | ICANON | IEXTEN);
286 }
287
288 tcsetattr(fd, TCSANOW, &tty);
289}
10f5bff6 290
29b838c0
DH
291#ifdef CONFIG_LINUX
292static void sigbus_handler(int signal, siginfo_t *siginfo, void *ctx)
293#else /* CONFIG_LINUX */
38183310 294static void sigbus_handler(int signal)
29b838c0 295#endif /* CONFIG_LINUX */
38183310 296{
1e356fc1 297 int i;
dba50678
DH
298
299 if (sigbus_memset_context) {
300 for (i = 0; i < sigbus_memset_context->num_threads; i++) {
301 MemsetThread *thread = &sigbus_memset_context->threads[i];
302
303 if (qemu_thread_is_self(&thread->pgthread)) {
304 siglongjmp(thread->env, 1);
1e356fc1
JK
305 }
306 }
307 }
29b838c0
DH
308
309#ifdef CONFIG_LINUX
310 /*
311 * We assume that the MCE SIGBUS handler could have been registered. We
312 * should never receive BUS_MCEERR_AO on any of our threads, but only on
313 * the main thread registered for PR_MCE_KILL_EARLY. Further, we should not
314 * receive BUS_MCEERR_AR triggered by action of other threads on one of
315 * our threads. So, no need to check for unrelated SIGBUS when seeing one
316 * for our threads.
317 *
318 * We will forward to the MCE handler, which will either handle the SIGBUS
319 * or reinstall the default SIGBUS handler and reraise the SIGBUS. The
320 * default SIGBUS handler will crash the process, so we don't care.
321 */
322 if (sigbus_oldact.sa_flags & SA_SIGINFO) {
323 sigbus_oldact.sa_sigaction(signal, siginfo, ctx);
324 return;
325 }
326#endif /* CONFIG_LINUX */
6556aadc 327 warn_report("qemu_prealloc_mem: unrelated SIGBUS detected and ignored");
38183310
PB
328}
329
1e356fc1
JK
330static void *do_touch_pages(void *arg)
331{
332 MemsetThread *memset_args = (MemsetThread *)arg;
1e356fc1 333 sigset_t set, oldset;
6c427ab9 334 int ret = 0;
1e356fc1 335
037fb5eb 336 /*
337 * On Linux, the page faults from the loop below can cause mmap_sem
338 * contention with allocation of the thread stacks. Do not start
339 * clearing until all threads have been created.
340 */
341 qemu_mutex_lock(&page_mutex);
dba50678 342 while (!memset_args->context->all_threads_created) {
037fb5eb 343 qemu_cond_wait(&page_cond, &page_mutex);
344 }
345 qemu_mutex_unlock(&page_mutex);
346
1e356fc1
JK
347 /* unblock SIGBUS */
348 sigemptyset(&set);
349 sigaddset(&set, SIGBUS);
350 pthread_sigmask(SIG_UNBLOCK, &set, &oldset);
351
352 if (sigsetjmp(memset_args->env, 1)) {
6c427ab9 353 ret = -EFAULT;
1e356fc1 354 } else {
e947d47d
SW
355 char *addr = memset_args->addr;
356 size_t numpages = memset_args->numpages;
357 size_t hpagesize = memset_args->hpagesize;
358 size_t i;
1e356fc1 359 for (i = 0; i < numpages; i++) {
9dc44aa5
DB
360 /*
361 * Read & write back the same value, so we don't
362 * corrupt existing user/app data that might be
363 * stored.
364 *
365 * 'volatile' to stop compiler optimizing this away
366 * to a no-op
9dc44aa5
DB
367 */
368 *(volatile char *)addr = *addr;
1e356fc1
JK
369 addr += hpagesize;
370 }
371 }
372 pthread_sigmask(SIG_SETMASK, &oldset, NULL);
6c427ab9 373 return (void *)(uintptr_t)ret;
1e356fc1
JK
374}
375
a384bfa3
DH
376static void *do_madv_populate_write_pages(void *arg)
377{
378 MemsetThread *memset_args = (MemsetThread *)arg;
379 const size_t size = memset_args->numpages * memset_args->hpagesize;
380 char * const addr = memset_args->addr;
381 int ret = 0;
382
383 /* See do_touch_pages(). */
384 qemu_mutex_lock(&page_mutex);
dba50678 385 while (!memset_args->context->all_threads_created) {
a384bfa3
DH
386 qemu_cond_wait(&page_cond, &page_mutex);
387 }
388 qemu_mutex_unlock(&page_mutex);
389
390 if (size && qemu_madvise(addr, size, QEMU_MADV_POPULATE_WRITE)) {
391 ret = -errno;
392 }
393 return (void *)(uintptr_t)ret;
394}
395
89aec641 396static inline int get_memset_num_threads(size_t hpagesize, size_t numpages,
6556aadc 397 int max_threads)
dfd0dcc7
JK
398{
399 long host_procs = sysconf(_SC_NPROCESSORS_ONLN);
400 int ret = 1;
401
402 if (host_procs > 0) {
6556aadc 403 ret = MIN(MIN(host_procs, MAX_MEM_PREALLOC_THREAD_COUNT), max_threads);
dfd0dcc7 404 }
89aec641
DH
405
406 /* Especially with gigantic pages, don't create more threads than pages. */
407 ret = MIN(ret, numpages);
408 /* Don't start threads to prealloc comparatively little memory. */
409 ret = MIN(ret, MAX(1, hpagesize * numpages / (64 * MiB)));
410
dfd0dcc7
JK
411 /* In case sysconf() fails, we fall back to single threaded */
412 return ret;
413}
414
6c427ab9 415static int touch_all_pages(char *area, size_t hpagesize, size_t numpages,
e04a34e5
DH
416 int max_threads, ThreadContext *tc,
417 bool use_madv_populate_write)
1e356fc1 418{
78b3f67a 419 static gsize initialized = 0;
dba50678 420 MemsetContext context = {
6556aadc 421 .num_threads = get_memset_num_threads(hpagesize, numpages, max_threads),
dba50678 422 };
037fb5eb 423 size_t numpages_per_thread, leftover;
a384bfa3 424 void *(*touch_fn)(void *);
6c427ab9 425 int ret = 0, i = 0;
1e356fc1 426 char *addr = area;
1e356fc1 427
78b3f67a
PB
428 if (g_once_init_enter(&initialized)) {
429 qemu_mutex_init(&page_mutex);
430 qemu_cond_init(&page_cond);
431 g_once_init_leave(&initialized, 1);
432 }
433
a384bfa3 434 if (use_madv_populate_write) {
ac86e5c3
DH
435 /* Avoid creating a single thread for MADV_POPULATE_WRITE */
436 if (context.num_threads == 1) {
437 if (qemu_madvise(area, hpagesize * numpages,
438 QEMU_MADV_POPULATE_WRITE)) {
439 return -errno;
440 }
441 return 0;
442 }
a384bfa3
DH
443 touch_fn = do_madv_populate_write_pages;
444 } else {
445 touch_fn = do_touch_pages;
446 }
447
dba50678
DH
448 context.threads = g_new0(MemsetThread, context.num_threads);
449 numpages_per_thread = numpages / context.num_threads;
450 leftover = numpages % context.num_threads;
451 for (i = 0; i < context.num_threads; i++) {
452 context.threads[i].addr = addr;
453 context.threads[i].numpages = numpages_per_thread + (i < leftover);
454 context.threads[i].hpagesize = hpagesize;
455 context.threads[i].context = &context;
e04a34e5
DH
456 if (tc) {
457 thread_context_create_thread(tc, &context.threads[i].pgthread,
458 "touch_pages",
459 touch_fn, &context.threads[i],
460 QEMU_THREAD_JOINABLE);
461 } else {
462 qemu_thread_create(&context.threads[i].pgthread, "touch_pages",
463 touch_fn, &context.threads[i],
464 QEMU_THREAD_JOINABLE);
465 }
dba50678
DH
466 addr += context.threads[i].numpages * hpagesize;
467 }
468
469 if (!use_madv_populate_write) {
470 sigbus_memset_context = &context;
1e356fc1 471 }
278fb162
B
472
473 qemu_mutex_lock(&page_mutex);
dba50678 474 context.all_threads_created = true;
037fb5eb 475 qemu_cond_broadcast(&page_cond);
278fb162 476 qemu_mutex_unlock(&page_mutex);
037fb5eb 477
dba50678
DH
478 for (i = 0; i < context.num_threads; i++) {
479 int tmp = (uintptr_t)qemu_thread_join(&context.threads[i].pgthread);
6c427ab9
DH
480
481 if (tmp) {
482 ret = tmp;
483 }
1e356fc1 484 }
dba50678
DH
485
486 if (!use_madv_populate_write) {
487 sigbus_memset_context = NULL;
488 }
489 g_free(context.threads);
1e356fc1 490
6c427ab9 491 return ret;
1e356fc1
JK
492}
493
a384bfa3
DH
494static bool madv_populate_write_possible(char *area, size_t pagesize)
495{
496 return !qemu_madvise(area, pagesize, QEMU_MADV_POPULATE_WRITE) ||
497 errno != EINVAL;
498}
499
6556aadc 500void qemu_prealloc_mem(int fd, char *area, size_t sz, int max_threads,
e04a34e5 501 ThreadContext *tc, Error **errp)
38183310 502{
a960d664 503 static gsize initialized;
b7bf8f56 504 int ret;
1e356fc1 505 size_t hpagesize = qemu_fd_getpagesize(fd);
6556aadc 506 size_t numpages = DIV_ROUND_UP(sz, hpagesize);
a384bfa3 507 bool use_madv_populate_write;
29b838c0 508 struct sigaction act;
38183310 509
a384bfa3
DH
510 /*
511 * Sense on every invocation, as MADV_POPULATE_WRITE cannot be used for
512 * some special mappings, such as mapping /dev/mem.
513 */
514 use_madv_populate_write = madv_populate_write_possible(area, hpagesize);
515
516 if (!use_madv_populate_write) {
a960d664
DH
517 if (g_once_init_enter(&initialized)) {
518 qemu_mutex_init(&sigbus_mutex);
519 g_once_init_leave(&initialized, 1);
520 }
521
522 qemu_mutex_lock(&sigbus_mutex);
a384bfa3 523 memset(&act, 0, sizeof(act));
29b838c0
DH
524#ifdef CONFIG_LINUX
525 act.sa_sigaction = &sigbus_handler;
526 act.sa_flags = SA_SIGINFO;
527#else /* CONFIG_LINUX */
a384bfa3
DH
528 act.sa_handler = &sigbus_handler;
529 act.sa_flags = 0;
29b838c0 530#endif /* CONFIG_LINUX */
a384bfa3 531
29b838c0 532 ret = sigaction(SIGBUS, &act, &sigbus_oldact);
a384bfa3 533 if (ret) {
dd4fc605 534 qemu_mutex_unlock(&sigbus_mutex);
a384bfa3 535 error_setg_errno(errp, errno,
6556aadc 536 "qemu_prealloc_mem: failed to install signal handler");
a384bfa3
DH
537 return;
538 }
38183310
PB
539 }
540
1e356fc1 541 /* touch pages simultaneously */
e04a34e5 542 ret = touch_all_pages(area, hpagesize, numpages, max_threads, tc,
a384bfa3 543 use_madv_populate_write);
6c427ab9
DH
544 if (ret) {
545 error_setg_errno(errp, -ret,
6556aadc 546 "qemu_prealloc_mem: preallocating memory failed");
056b68af 547 }
38183310 548
a384bfa3 549 if (!use_madv_populate_write) {
29b838c0 550 ret = sigaction(SIGBUS, &sigbus_oldact, NULL);
a384bfa3
DH
551 if (ret) {
552 /* Terminate QEMU since it can't recover from error */
6556aadc 553 perror("qemu_prealloc_mem: failed to reinstall signal handler");
a384bfa3
DH
554 exit(1);
555 }
a960d664 556 qemu_mutex_unlock(&sigbus_mutex);
b7bf8f56 557 }
38183310 558}
d57e4e48 559
7dc9ae43
MP
560char *qemu_get_pid_name(pid_t pid)
561{
562 char *name = NULL;
563
564#if defined(__FreeBSD__)
565 /* BSDs don't have /proc, but they provide a nice substitute */
566 struct kinfo_proc *proc = kinfo_getproc(pid);
567
568 if (proc) {
569 name = g_strdup(proc->ki_comm);
570 free(proc);
571 }
572#else
573 /* Assume a system with reasonable procfs */
574 char *pid_path;
575 size_t len;
576
577 pid_path = g_strdup_printf("/proc/%d/cmdline", pid);
578 g_file_get_contents(pid_path, &name, &len, NULL);
579 g_free(pid_path);
580#endif
581
582 return name;
583}
584
585
8737d9e0
PL
586void *qemu_alloc_stack(size_t *sz)
587{
a1eaa628 588 void *ptr;
fc3d1bad 589 int flags;
7d992e4d
PL
590#ifdef CONFIG_DEBUG_STACK_USAGE
591 void *ptr2;
592#endif
8e3b0cbb 593 size_t pagesz = qemu_real_host_page_size();
8737d9e0
PL
594#ifdef _SC_THREAD_STACK_MIN
595 /* avoid stacks smaller than _SC_THREAD_STACK_MIN */
596 long min_stack_sz = sysconf(_SC_THREAD_STACK_MIN);
597 *sz = MAX(MAX(min_stack_sz, 0), *sz);
598#endif
599 /* adjust stack size to a multiple of the page size */
600 *sz = ROUND_UP(*sz, pagesz);
601 /* allocate one extra page for the guard page */
602 *sz += pagesz;
603
fc3d1bad
BS
604 flags = MAP_PRIVATE | MAP_ANONYMOUS;
605#if defined(MAP_STACK) && defined(__OpenBSD__)
606 /* Only enable MAP_STACK on OpenBSD. Other OS's such as
607 * Linux/FreeBSD/NetBSD have a flag with the same name
608 * but have differing functionality. OpenBSD will SEGV
609 * if it spots execution with a stack pointer pointing
610 * at memory that was not allocated with MAP_STACK.
611 */
612 flags |= MAP_STACK;
613#endif
614
615 ptr = mmap(NULL, *sz, PROT_READ | PROT_WRITE, flags, -1, 0);
8737d9e0 616 if (ptr == MAP_FAILED) {
e916a6e8 617 perror("failed to allocate memory for stack");
8737d9e0
PL
618 abort();
619 }
620
a1eaa628
AO
621 /* Stack grows down -- guard page at the bottom. */
622 if (mprotect(ptr, pagesz, PROT_NONE) != 0) {
e916a6e8 623 perror("failed to set up stack guard page");
8737d9e0
PL
624 abort();
625 }
626
7d992e4d
PL
627#ifdef CONFIG_DEBUG_STACK_USAGE
628 for (ptr2 = ptr + pagesz; ptr2 < ptr + *sz; ptr2 += sizeof(uint32_t)) {
629 *(uint32_t *)ptr2 = 0xdeadbeaf;
630 }
631#endif
632
8737d9e0
PL
633 return ptr;
634}
635
7d992e4d
PL
636#ifdef CONFIG_DEBUG_STACK_USAGE
637static __thread unsigned int max_stack_usage;
638#endif
639
8737d9e0
PL
640void qemu_free_stack(void *stack, size_t sz)
641{
7d992e4d
PL
642#ifdef CONFIG_DEBUG_STACK_USAGE
643 unsigned int usage;
644 void *ptr;
645
8e3b0cbb 646 for (ptr = stack + qemu_real_host_page_size(); ptr < stack + sz;
7d992e4d
PL
647 ptr += sizeof(uint32_t)) {
648 if (*(uint32_t *)ptr != 0xdeadbeaf) {
649 break;
650 }
651 }
652 usage = sz - (uintptr_t) (ptr - stack);
653 if (usage > max_stack_usage) {
654 error_report("thread %d max stack usage increased from %u to %u",
655 qemu_get_thread_id(), max_stack_usage, usage);
656 max_stack_usage = usage;
657 }
658#endif
659
8737d9e0
PL
660 munmap(stack, sz);
661}
d98d4072 662
c905a368
DB
663/*
664 * Disable CFI checks.
d02d06f8 665 * We are going to call a signal handler directly. Such handler may or may not
c905a368
DB
666 * have been defined in our binary, so there's no guarantee that the pointer
667 * used to set the handler is a cfi-valid pointer. Since the handlers are
668 * stored in kernel memory, changing the handler to an attacker-defined
669 * function requires being able to call a sigaction() syscall,
670 * which is not as easy as overwriting a pointer in memory.
671 */
672QEMU_DISABLE_CFI
d98d4072
PB
673void sigaction_invoke(struct sigaction *action,
674 struct qemu_signalfd_siginfo *info)
675{
02ffa034 676 siginfo_t si = {};
d98d4072
PB
677 si.si_signo = info->ssi_signo;
678 si.si_errno = info->ssi_errno;
679 si.si_code = info->ssi_code;
680
681 /* Convert the minimal set of fields defined by POSIX.
682 * Positive si_code values are reserved for kernel-generated
683 * signals, where the valid siginfo fields are determined by
684 * the signal number. But according to POSIX, it is unspecified
685 * whether SI_USER and SI_QUEUE have values less than or equal to
686 * zero.
687 */
688 if (info->ssi_code == SI_USER || info->ssi_code == SI_QUEUE ||
689 info->ssi_code <= 0) {
690 /* SIGTERM, etc. */
691 si.si_pid = info->ssi_pid;
692 si.si_uid = info->ssi_uid;
693 } else if (info->ssi_signo == SIGILL || info->ssi_signo == SIGFPE ||
694 info->ssi_signo == SIGSEGV || info->ssi_signo == SIGBUS) {
695 si.si_addr = (void *)(uintptr_t)info->ssi_addr;
696 } else if (info->ssi_signo == SIGCHLD) {
697 si.si_pid = info->ssi_pid;
698 si.si_status = info->ssi_status;
699 si.si_uid = info->ssi_uid;
d98d4072
PB
700 }
701 action->sa_sigaction(info->ssi_signo, &si, NULL);
702}
e47f4765 703
ad06ef0e
AB
704size_t qemu_get_host_physmem(void)
705{
706#ifdef _SC_PHYS_PAGES
707 long pages = sysconf(_SC_PHYS_PAGES);
708 if (pages > 0) {
8e3b0cbb 709 if (pages > SIZE_MAX / qemu_real_host_page_size()) {
ad06ef0e
AB
710 return SIZE_MAX;
711 } else {
8e3b0cbb 712 return pages * qemu_real_host_page_size();
ad06ef0e
AB
713 }
714 }
715#endif
716 return 0;
717}
e9c4e0a8 718
73991a92
MAL
719int qemu_msync(void *addr, size_t length, int fd)
720{
721 size_t align_mask = ~(qemu_real_host_page_size() - 1);
722
723 /**
724 * There are no strict reqs as per the length of mapping
725 * to be synced. Still the length needs to follow the address
726 * alignment changes. Additionally - round the size to the multiple
727 * of PAGE_SIZE
728 */
729 length += ((uintptr_t)addr & (qemu_real_host_page_size() - 1));
730 length = (length + ~align_mask) & align_mask;
731
732 addr = (void *)((uintptr_t)addr & align_mask);
733
734 return msync(addr, length, MS_SYNC);
735}