[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-block] [PULL 37/42] oslib-posix: add helpers for stack alloc and f
From: |
Kevin Wolf |
Subject: |
[Qemu-block] [PULL 37/42] oslib-posix: add helpers for stack alloc and free |
Date: |
Mon, 5 Sep 2016 20:13:49 +0200 |
From: Peter Lieven <address@hidden>
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. The memory for the guard page is deductated from
stack memory so that the usable stack size is effectively reduced by the size
of one page. This is equivalent to how the glibc stack allocation routines
behave.
Signed-off-by: Peter Lieven <address@hidden>
Reviewed-by: Richard Henderson <address@hidden>
Acked-by: Paolo Bonzini <address@hidden>
Signed-off-by: Kevin Wolf <address@hidden>
---
include/sysemu/os-posix.h | 27 ++++++++++++++++++++++++++
util/oslib-posix.c | 48 +++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 75 insertions(+)
diff --git a/include/sysemu/os-posix.h b/include/sysemu/os-posix.h
index 9c7dfdf..87e60fe 100644
--- a/include/sysemu/os-posix.h
+++ b/include/sysemu/os-posix.h
@@ -60,4 +60,31 @@ int qemu_utimens(const char *path, const qemu_timespec
*times);
bool is_daemonized(void);
+/**
+ * qemu_alloc_stack:
+ * @sz: size of required stack in bytes
+ *
+ * Allocate memory that can be used as a stack, for instance for
+ * coroutines. If the memory cannot be allocated, this function
+ * will abort (like g_malloc()). This function also inserts a
+ * guard page to catch a potential stack overflow. The memory
+ * for the guard page is deductated from stack memory so that
+ * the usable stack size is effectively sz bytes minus the size
+ * of one page.
+ *
+ * The allocated stack must be freed with qemu_free_stack().
+ *
+ * Returns: pointer to (the lowest address of) the stack memory.
+ */
+void *qemu_alloc_stack(size_t sz);
+
+/**
+ * qemu_free_stack:
+ * @stack: stack to free
+ * @sz: size of stack in bytes
+ *
+ * Free a stack allocated via qemu_alloc_stack().
+ */
+void qemu_free_stack(void *stack, size_t sz);
+
#endif
diff --git a/util/oslib-posix.c b/util/oslib-posix.c
index f2d4e9e..74dbe1c 100644
--- a/util/oslib-posix.c
+++ b/util/oslib-posix.c
@@ -499,3 +499,51 @@ pid_t qemu_fork(Error **errp)
}
return pid;
}
+
+static size_t adjust_stack_size(size_t sz, size_t pagesz)
+{
+#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);
+ return sz;
+}
+
+void *qemu_alloc_stack(size_t sz)
+{
+ void *ptr, *guardpage;
+ size_t pagesz = getpagesize();
+ sz = adjust_stack_size(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)
+{
+ size_t pagesz = getpagesize();
+ sz = adjust_stack_size(sz, pagesz);
+ munmap(stack, sz);
+}
--
1.8.3.1
- [Qemu-block] [PULL 27/42] blockdev-backup: added support for data compression, (continued)
- [Qemu-block] [PULL 27/42] blockdev-backup: added support for data compression, Kevin Wolf, 2016/09/05
- [Qemu-block] [PULL 30/42] test-coroutine: Fix coroutine pool corruption, Kevin Wolf, 2016/09/05
- [Qemu-block] [PULL 32/42] coroutine: Let CoMutex remember who holds it, Kevin Wolf, 2016/09/05
- [Qemu-block] [PULL 31/42] qcow2: fix iovec size at qcow2_co_pwritev_compressed, Kevin Wolf, 2016/09/05
- [Qemu-block] [PULL 34/42] block jobs: Improve error message for missing job ID, Kevin Wolf, 2016/09/05
- [Qemu-block] [PULL 35/42] qemu-iotests: Log QMP traffic in debug mode, Kevin Wolf, 2016/09/05
- [Qemu-block] [PULL 36/42] block: Allow node name for 'qemu-io' HMP command, Kevin Wolf, 2016/09/05
- [Qemu-block] [PULL 38/42] coroutine: add a macro for the coroutine stack size, Kevin Wolf, 2016/09/05
- [Qemu-block] [PULL 37/42] oslib-posix: add helpers for stack alloc and free,
Kevin Wolf <=
- [Qemu-block] [PULL 33/42] coroutine: Assert that no locks are held on termination, Kevin Wolf, 2016/09/05
- [Qemu-block] [PULL 40/42] coroutine-sigaltstack: use helper for allocating stack memory, Kevin Wolf, 2016/09/05
- [Qemu-block] [PULL 39/42] coroutine-ucontext: use helper for allocating stack memory, Kevin Wolf, 2016/09/05
- [Qemu-block] [PULL 41/42] oslib-posix: add a configure switch to debug stack usage, Kevin Wolf, 2016/09/05
- [Qemu-block] [PULL 42/42] coroutine: reduce stack size to 64kB, Kevin Wolf, 2016/09/05
- Re: [Qemu-block] [Qemu-devel] [PULL 00/42] Block layer patches, Peter Maydell, 2016/09/06
- Re: [Qemu-block] [Qemu-devel] [PULL 00/42] Block layer patches, Peter Maydell, 2016/09/06
- Re: [Qemu-block] [Qemu-devel] [PULL 00/42] Block layer patches, Peter Maydell, 2016/09/06