[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v2 27/43] bsd-user: Move stack initializtion into a per-os file.
From: |
imp |
Subject: |
[PATCH v2 27/43] bsd-user: Move stack initializtion into a per-os file. |
Date: |
Thu, 26 Aug 2021 15:11:45 -0600 |
From: Warner Losh <imp@FreeBSD.org>
Move all of the stack initialization into target_os_stack.h. Each BSD
sets up processes a little differently.
Signed-off-by: Stacey Son <sson@FreeBSD.org>
Signed-off-by: Warner Losh <imp@bsdimp.com>
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
---
bsd-user/freebsd/target_os_stack.h | 180 +++++++++++++++++++++++++++++
bsd-user/netbsd/target_os_stack.h | 56 +++++++++
bsd-user/openbsd/target_os_stack.h | 56 +++++++++
3 files changed, 292 insertions(+)
create mode 100644 bsd-user/freebsd/target_os_stack.h
create mode 100644 bsd-user/netbsd/target_os_stack.h
create mode 100644 bsd-user/openbsd/target_os_stack.h
diff --git a/bsd-user/freebsd/target_os_stack.h
b/bsd-user/freebsd/target_os_stack.h
new file mode 100644
index 0000000000..93e353ca61
--- /dev/null
+++ b/bsd-user/freebsd/target_os_stack.h
@@ -0,0 +1,180 @@
+/*
+ * FreeBSD setup_initial_stack() implementation.
+ *
+ * Copyright (c) 2013-14 Stacey D. Son
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef _TARGET_OS_STACK_H_
+#define _TARGET_OS_STACK_H_
+
+#include <sys/param.h>
+#include "target_arch_sigtramp.h"
+#include "qemu/guest-random.h"
+
+/*
+ * The inital FreeBSD stack is as follows:
+ * (see kern/kern_exec.c exec_copyout_strings() )
+ *
+ * Hi Address -> char **ps_argvstr (struct ps_strings for ps, w, etc.)
+ * unsigned ps_nargvstr
+ * char **ps_envstr
+ * PS_STRINGS -> unsigned ps_nenvstr
+ *
+ * machine dependent sigcode (sv_sigcode of size
+ * sv_szsigcode)
+ *
+ * execpath (absolute image path for rtld)
+ *
+ * SSP Canary (sizeof(long) * 8)
+ *
+ * page sizes array (usually sizeof(u_long) )
+ *
+ * "destp" -> argv, env strings (up to 262144 bytes)
+ */
+static inline int setup_initial_stack(struct bsd_binprm *bprm,
+ abi_ulong *ret_addr, abi_ulong *stringp)
+{
+ int i;
+ abi_ulong stack_hi_addr;
+ size_t execpath_len, stringspace;
+ abi_ulong destp, argvp, envp, p;
+ struct target_ps_strings ps_strs;
+ char canary[sizeof(abi_long) * 8];
+
+ stack_hi_addr = p = target_stkbas + target_stksiz;
+
+ /* Save some space for ps_strings. */
+ p -= sizeof(struct target_ps_strings);
+
+ /* Add machine depedent sigcode. */
+ p -= TARGET_SZSIGCODE;
+ if (setup_sigtramp(p, (unsigned)offsetof(struct target_sigframe, sf_uc),
+ TARGET_FREEBSD_NR_sigreturn)) {
+ errno = EFAULT;
+ return -1;
+ }
+ if (bprm->fullpath) {
+ execpath_len = strlen(bprm->fullpath) + 1;
+ p -= roundup(execpath_len, sizeof(abi_ulong));
+ if (memcpy_to_target(p, bprm->fullpath, execpath_len)) {
+ errno = EFAULT;
+ return -1;
+ }
+ }
+ /* Add canary for SSP. */
+ qemu_guest_getrandom_nofail(canary, sizeof(canary));
+ p -= roundup(sizeof(canary), sizeof(abi_ulong));
+ if (memcpy_to_target(p, canary, sizeof(canary))) {
+ errno = EFAULT;
+ return -1;
+ }
+ /* Add page sizes array. */
+ p -= sizeof(abi_ulong);
+ if (put_user_ual(TARGET_PAGE_SIZE, p)) {
+ errno = EFAULT;
+ return -1;
+ }
+ /*
+ * Deviate from FreeBSD stack layout: force stack to new page here
+ * so that signal trampoline is not sharing the page with user stack
+ * frames. This is actively harmful in qemu as it marks pages with
+ * code it translated as read-only, which is somewhat problematic
+ * for user trying to use the stack as intended.
+ */
+ p = rounddown(p, TARGET_PAGE_SIZE);
+
+ /* Calculate the string space needed */
+ stringspace = 0;
+ for (i = 0; i < bprm->argc; ++i) {
+ stringspace += strlen(bprm->argv[i]) + 1;
+ }
+ for (i = 0; i < bprm->envc; ++i) {
+ stringspace += strlen(bprm->envp[i]) + 1;
+ }
+ if (stringspace > TARGET_ARG_MAX) {
+ errno = ENOMEM;
+ return -1;
+ }
+ /* Make room for the argv and envp strings */
+ destp = rounddown(p - stringspace, sizeof(abi_ulong));
+ p = argvp = destp - (bprm->argc + bprm->envc + 2) * sizeof(abi_ulong);
+ /* Remember the strings pointer */
+ if (stringp)
+ *stringp = destp;
+ /*
+ * Add argv strings. Note that the argv[] vectors are added by
+ * loader_build_argptr()
+ */
+ /* XXX need to make room for auxargs */
+ ps_strs.ps_argvstr = tswapl(argvp);
+ ps_strs.ps_nargvstr = tswap32(bprm->argc);
+ for (i = 0; i < bprm->argc; ++i) {
+ size_t len = strlen(bprm->argv[i]) + 1;
+
+ if (memcpy_to_target(destp, bprm->argv[i], len)) {
+ errno = EFAULT;
+ return -1;
+ }
+ if (put_user_ual(destp, argvp)) {
+ errno = EFAULT;
+ return -1;
+ }
+ argvp += sizeof(abi_ulong);
+ destp += len;
+ }
+ if (put_user_ual(0, argvp)) {
+ errno = EFAULT;
+ return -1;
+ }
+ /*
+ * Add env strings. Note that the envp[] vectors are added by
+ * loader_build_argptr().
+ */
+ envp = argvp + sizeof(abi_ulong);
+ ps_strs.ps_envstr = tswapl(envp);
+ ps_strs.ps_nenvstr = tswap32(bprm->envc);
+ for (i = 0; i < bprm->envc; ++i) {
+ size_t len = strlen(bprm->envp[i]) + 1;
+
+ if (memcpy_to_target(destp, bprm->envp[i], len)) {
+ errno = EFAULT;
+ return -1;
+ }
+ if (put_user_ual(destp, envp)) {
+ errno = EFAULT;
+ return -1;
+ }
+ envp += sizeof(abi_ulong);
+ destp += len;
+ }
+ if (put_user_ual(0, envp)) {
+ errno = EFAULT;
+ return -1;
+ }
+ if (memcpy_to_target(stack_hi_addr - sizeof(ps_strs), &ps_strs,
+ sizeof(ps_strs))) {
+ errno = EFAULT;
+ return -1;
+ }
+
+ if (ret_addr) {
+ *ret_addr = p;
+ }
+
+ return 0;
+ }
+
+#endif /* !_TARGET_OS_STACK_H_ */
diff --git a/bsd-user/netbsd/target_os_stack.h
b/bsd-user/netbsd/target_os_stack.h
new file mode 100644
index 0000000000..503279c1a9
--- /dev/null
+++ b/bsd-user/netbsd/target_os_stack.h
@@ -0,0 +1,56 @@
+/*
+ * NetBSD setup_initial_stack() implementation.
+ *
+ * Copyright (c) 2013-14 Stacey D. Son
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef _TARGET_OS_STACK_H_
+#define _TARGET_OS_STACK_H_
+
+#include "target_arch_sigtramp.h"
+
+static inline int setup_initial_stack(struct bsd_binprm *bprm, abi_ulong *p,
+ abi_ulong *stringp)
+{
+ int i;
+ abi_ulong stack_base;
+
+ stack_base = (target_stkbas + target_stksiz) -
+ MAX_ARG_PAGES * TARGET_PAGE_SIZE;
+ if (p) {
+ *p = stack_base;
+ }
+ if (stringp) {
+ *stringp = stack_base;
+ }
+
+ for (i = 0; i < MAX_ARG_PAGES; i++) {
+ if (bprm->page[i]) {
+ info->rss++;
+ if (!memcpy_to_target(stack_base, bprm->page[i],
+ TARGET_PAGE_SIZE)) {
+ errno = EFAULT;
+ return -1;
+ }
+ g_free(bprm->page[i]);
+ }
+ stack_base += TARGET_PAGE_SIZE;
+ }
+
+ return 0;
+}
+
+#endif /* !_TARGET_OS_STACK_H_ */
diff --git a/bsd-user/openbsd/target_os_stack.h
b/bsd-user/openbsd/target_os_stack.h
new file mode 100644
index 0000000000..4b37955d3b
--- /dev/null
+++ b/bsd-user/openbsd/target_os_stack.h
@@ -0,0 +1,56 @@
+/*
+ * OpenBSD setup_initial_stack() implementation.
+ *
+ * Copyright (c) 2013-14 Stacey D. Son
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef _TARGET_OS_STACK_H_
+#define _TARGET_OS_STACK_H_
+
+#include "target_arch_sigtramp.h"
+
+static inline int setup_initial_stack(struct bsd_binprm *bprm, abi_ulong *p,
+ abi_ulong *stringp)
+{
+ int i;
+ abi_ulong stack_base;
+
+ stack_base = (target_stkbas + target_stksiz) -
+ MAX_ARG_PAGES * TARGET_PAGE_SIZE;
+ if (p) {
+ *p = stack_base;
+ }
+ if (stringp) {
+ *stringp = stack_base;
+ }
+
+ for (i = 0; i < MAX_ARG_PAGES; i++) {
+ if (bprm->page[i]) {
+ info->rss++;
+ if (!memcpy_to_target(stack_base, bprm->page[i],
+ TARGET_PAGE_SIZE)) {
+ errno = EFAULT;
+ return -1;
+ }
+ g_free(bprm->page[i]);
+ }
+ stack_base += TARGET_PAGE_SIZE;
+ }
+
+ return 0;
+}
+
+#endif /* !_TARGET_OS_STACK_H_ */
--
2.32.0
- [PATCH v2 20/43] bsd-user: Move per-cpu code into target_arch_cpu.h, (continued)
- [PATCH v2 20/43] bsd-user: Move per-cpu code into target_arch_cpu.h, imp, 2021/08/26
- [PATCH v2 24/43] bsd-user: Create target specific vmparam.h, imp, 2021/08/26
- [PATCH v2 26/43] bsd-user: *BSD specific siginfo defintions, imp, 2021/08/26
- [PATCH v2 21/43] bsd-user: pull in target_arch_thread.h update target_arch_elf.h, imp, 2021/08/26
- [PATCH v2 32/43] bsd-user: Make cpu_model and cpu_type visible to all of main.c, imp, 2021/08/26
- [PATCH v2 33/43] bsd-user: update debugging in mmap.c, imp, 2021/08/26
- [PATCH v2 39/43] bsd-user: move qemu_log to later in the file, imp, 2021/08/26
- [PATCH v2 29/43] bsd-user: elf cleanup, imp, 2021/08/26
- [PATCH v2 31/43] bsd-user: Rewrite target system call definintion glue, imp, 2021/08/26
- [PATCH v2 25/43] bsd-user: Add architecture specific signal tramp code, imp, 2021/08/26
- [PATCH v2 27/43] bsd-user: Move stack initializtion into a per-os file.,
imp <=
- [PATCH v2 34/43] bsd-user: Add target_arch_reg to describe a target's register set, imp, 2021/08/26
- [PATCH v2 30/43] bsd-user: Remove dead #ifdefs from elfload.c, imp, 2021/08/26
[PATCH v2 36/43] bsd-user: add stubbed out core dump support, imp, 2021/08/26
[PATCH v2 38/43] bsd-user: Refactor load_elf_sections and is_target_elf_binary, imp, 2021/08/26
[PATCH v2 42/43] bsd-user: Add '-0 argv0' option to bsd-user/main.c, imp, 2021/08/26