diff options
author | Peter Lieven <pl@kamp.de> | 2016-09-27 11:58:40 +0200 |
---|---|---|
committer | Kevin Wolf <kwolf@redhat.com> | 2016-09-29 14:13:39 +0200 |
commit | 8737d9e0c4017aaa5ab1fcf1356c8ee4f7caf1df (patch) | |
tree | 9598ddbb6fc8d488755f71e9b9be1ea8839a25e9 /util | |
parent | 74e1ae7c0b66d85d2b04c153ffdf6294e6a40798 (diff) | |
download | qemu-8737d9e0c4017aaa5ab1fcf1356c8ee4f7caf1df.zip qemu-8737d9e0c4017aaa5ab1fcf1356c8ee4f7caf1df.tar.gz qemu-8737d9e0c4017aaa5ab1fcf1356c8ee4f7caf1df.tar.bz2 |
oslib-posix: add helpers for stack alloc and free
the allocated stack will be adjusted to the minimum supported stack size
by the OS and rounded up to be a multiple of the system pagesize.
Additionally an architecture dependent guard page is added to the stack
to catch stack overflows.
Signed-off-by: Peter Lieven <pl@kamp.de>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
Diffstat (limited to 'util')
-rw-r--r-- | util/oslib-posix.c | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/util/oslib-posix.c b/util/oslib-posix.c index f2d4e9e..d950c34 100644 --- a/util/oslib-posix.c +++ b/util/oslib-posix.c @@ -499,3 +499,45 @@ pid_t qemu_fork(Error **errp) } return pid; } + +void *qemu_alloc_stack(size_t *sz) +{ + void *ptr, *guardpage; + size_t pagesz = getpagesize(); +#ifdef _SC_THREAD_STACK_MIN + /* avoid stacks smaller than _SC_THREAD_STACK_MIN */ + long min_stack_sz = sysconf(_SC_THREAD_STACK_MIN); + *sz = MAX(MAX(min_stack_sz, 0), *sz); +#endif + /* adjust stack size to a multiple of the page size */ + *sz = ROUND_UP(*sz, pagesz); + /* allocate one extra page for the guard page */ + *sz += pagesz; + + ptr = mmap(NULL, *sz, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + if (ptr == MAP_FAILED) { + abort(); + } + +#if defined(HOST_IA64) + /* separate register stack */ + guardpage = ptr + (((*sz - pagesz) / 2) & ~pagesz); +#elif defined(HOST_HPPA) + /* stack grows up */ + guardpage = ptr + *sz - pagesz; +#else + /* stack grows down */ + guardpage = ptr; +#endif + if (mprotect(guardpage, pagesz, PROT_NONE) != 0) { + abort(); + } + + return ptr; +} + +void qemu_free_stack(void *stack, size_t sz) +{ + munmap(stack, sz); +} |