qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: imp@bsdimp.com
To: qemu-devel@nongnu.org
Cc: kevans@freebsd.org,
	Richard Henderson <richard.henderson@linaro.org>,
	Warner Losh <imp@FreeBSD.org>, Warner Losh <imp@bsdimp.com>,
	Stacey Son <sson@FreeBSD.org>
Subject: [PULL 25/42] bsd-user: Add system independent stack, data and text limiting
Date: Tue,  7 Sep 2021 15:53:15 -0600	[thread overview]
Message-ID: <20210907215332.30737-26-imp@bsdimp.com> (raw)
In-Reply-To: <20210907215332.30737-1-imp@bsdimp.com>

From: Warner Losh <imp@FreeBSD.org>

Eliminate the x86 specific stack stuff in favor of more generic control
over the process size:
    target_maxtsiz  max text size
    target_dfldsiz  initial data size limit
    target_maxdsiz  max data size
    target_dflssiz  initial stack size limit
    target_maxssiz  max stack size
    target_sgrowsiz amount to grow stack
These can be set on a per-arch basis, and the stack size can be set
on the command line. Adjust the stack size parameters at startup.

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/elfload.c |  2 +-
 bsd-user/main.c    | 52 +++++++++++++++++++++++++++++++++-------------
 bsd-user/qemu.h    |  7 ++++++-
 3 files changed, 45 insertions(+), 16 deletions(-)

diff --git a/bsd-user/elfload.c b/bsd-user/elfload.c
index c28ef34143..59465b71d4 100644
--- a/bsd-user/elfload.c
+++ b/bsd-user/elfload.c
@@ -204,7 +204,7 @@ static abi_ulong setup_arg_pages(abi_ulong p, struct bsd_binprm *bprm,
     /* Create enough stack to hold everything.  If we don't use
      * it for args, we'll use it for something else...
      */
-    size = x86_stack_size;
+    size = target_dflssiz;
     if (size < MAX_ARG_PAGES * TARGET_PAGE_SIZE)
         size = MAX_ARG_PAGES * TARGET_PAGE_SIZE;
     error = target_mmap(0,
diff --git a/bsd-user/main.c b/bsd-user/main.c
index 3d4ed202a0..19bf3a09a7 100644
--- a/bsd-user/main.c
+++ b/bsd-user/main.c
@@ -18,6 +18,11 @@
  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
  */
 
+#include <sys/types.h>
+#include <sys/time.h>
+#include <sys/resource.h>
+#include <sys/sysctl.h>
+
 #include "qemu/osdep.h"
 #include "qemu-common.h"
 #include "qemu/units.h"
@@ -44,8 +49,6 @@
 #include "host-os.h"
 #include "target_arch_cpu.h"
 
-#include <sys/sysctl.h>
-
 int singlestep;
 unsigned long mmap_min_addr;
 uintptr_t guest_base;
@@ -57,12 +60,12 @@ const char *qemu_uname_release;
 enum BSDType bsd_type;
 char qemu_proc_pathname[PATH_MAX];  /* full path to exeutable */
 
-/*
- * XXX: on x86 MAP_GROWSDOWN only works if ESP <= address + 32, so
- * we allocate a bigger stack. Need a better solution, for example
- * by remapping the process stack directly at the right place
- */
-unsigned long x86_stack_size = 512 * 1024;
+unsigned long target_maxtsiz = TARGET_MAXTSIZ;   /* max text size */
+unsigned long target_dfldsiz = TARGET_DFLDSIZ;   /* initial data size limit */
+unsigned long target_maxdsiz = TARGET_MAXDSIZ;   /* max data size */
+unsigned long target_dflssiz = TARGET_DFLSSIZ;   /* initial data size limit */
+unsigned long target_maxssiz = TARGET_MAXSSIZ;   /* max stack size */
+unsigned long target_sgrowsiz = TARGET_SGROWSIZ; /* amount to grow stack */
 
 void gemu_log(const char *fmt, ...)
 {
@@ -112,7 +115,6 @@ static void usage(void)
            "-d item1[,...]    enable logging of specified items\n"
            "                  (use '-d help' for a list of log items)\n"
            "-D logfile        write logs to 'logfile' (default stderr)\n"
-           "-p pagesize       set the host page size to 'pagesize'\n"
            "-singlestep       always run in singlestep mode\n"
            "-strace           log system calls\n"
            "-trace            [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
@@ -132,7 +134,7 @@ static void usage(void)
            ,
            TARGET_NAME,
            interp_prefix,
-           x86_stack_size);
+           target_dflssiz);
     exit(1);
 }
 
@@ -161,6 +163,23 @@ void init_task_state(TaskState *ts)
     ts->sigqueue_table[i].next = NULL;
 }
 
+static void
+adjust_ssize(void)
+{
+    struct rlimit rl;
+
+    if (getrlimit(RLIMIT_STACK, &rl) != 0) {
+        return;
+    }
+
+    target_maxssiz = MIN(target_maxssiz, rl.rlim_max);
+    target_dflssiz = MIN(MAX(target_dflssiz, rl.rlim_cur), target_maxssiz);
+
+    rl.rlim_max = target_maxssiz;
+    rl.rlim_cur = target_dflssiz;
+    setrlimit(RLIMIT_STACK, &rl);
+}
+
 static void save_proc_pathname(char *argv0)
 {
     int mib[4];
@@ -197,6 +216,8 @@ int main(int argc, char **argv)
     envlist_t *envlist = NULL;
     bsd_type = HOST_DEFAULT_BSD_TYPE;
 
+    adjust_ssize();
+
     if (argc <= 1) {
         usage();
     }
@@ -257,14 +278,17 @@ int main(int argc, char **argv)
             }
         } else if (!strcmp(r, "s")) {
             r = argv[optind++];
-            rv = qemu_strtoul(r, &r, 0, &x86_stack_size);
-            if (rv < 0 || x86_stack_size <= 0) {
+            rv = qemu_strtoul(r, &r, 0, &target_dflssiz);
+            if (rv < 0 || target_dflssiz <= 0) {
                 usage();
             }
             if (*r == 'M') {
-                x86_stack_size *= MiB;
+                target_dflssiz *= 1024 * 1024;
             } else if (*r == 'k' || *r == 'K') {
-                x86_stack_size *= KiB;
+                target_dflssiz *= 1024;
+            }
+            if (target_dflssiz > target_maxssiz) {
+                usage();
             }
         } else if (!strcmp(r, "L")) {
             interp_prefix = argv[optind++];
diff --git a/bsd-user/qemu.h b/bsd-user/qemu.h
index 1b37757e06..21efa7d777 100644
--- a/bsd-user/qemu.h
+++ b/bsd-user/qemu.h
@@ -219,7 +219,12 @@ void mmap_fork_end(int child);
 
 /* main.c */
 extern char qemu_proc_pathname[];
-extern unsigned long x86_stack_size;
+extern unsigned long target_maxtsiz;
+extern unsigned long target_dfldsiz;
+extern unsigned long target_maxdsiz;
+extern unsigned long target_dflssiz;
+extern unsigned long target_maxssiz;
+extern unsigned long target_sgrowsiz;
 
 /* user access */
 
-- 
2.32.0



  parent reply	other threads:[~2021-09-07 22:08 UTC|newest]

Thread overview: 50+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-07 21:52 [PULL 00/42] bsd-user updates to run hello world imp
2021-09-07 21:52 ` [PULL 01/42] bsd-user: remove sparc and sparc64 imp
2021-09-07 21:52 ` [PULL 02/42] bsd-user: add copyright header to elfload.c imp
2021-09-07 21:52 ` [PULL 03/42] bsd-user: Add Stacey's copyright to main.c imp
2021-09-07 21:52 ` [PULL 04/42] bsd-user: add license to bsdload.c imp
2021-09-07 21:52 ` [PULL 05/42] bsd-user: style nits: bsdload.c whitespace to qemu standard imp
2021-09-07 21:52 ` [PULL 06/42] bsd-user: Remove all non-x86 code from elfload.c imp
2021-09-07 21:52 ` [PULL 07/42] bsd-user: move arch specific defines out of elfload.c imp
2021-09-07 21:52 ` [PULL 08/42] bsd-user: pass the bsd_param into loader_exec imp
2021-09-07 21:52 ` [PULL 09/42] bsd-user: Fix calculation of size to allocate imp
2021-09-07 21:53 ` [PULL 10/42] bsd-user: implement path searching imp
2021-09-07 21:53 ` [PULL 11/42] bsd-user: Eliminate elf personality imp
2021-09-07 21:53 ` [PULL 12/42] bsd-user: remove a.out support imp
2021-09-07 21:53 ` [PULL 13/42] bsd-user: TARGET_NGROUPS unused in this file, remove imp
2021-09-07 21:53 ` [PULL 14/42] bsd-user: elfload: simplify bswap a bit imp
2021-09-07 21:53 ` [PULL 15/42] bsd-user: assume pthreads and support of __thread imp
2021-09-07 21:53 ` [PULL 16/42] bsd-user: add host-os.h imp
2021-09-07 21:53 ` [PULL 17/42] bsd-user: Include host-os.h from main imp
2021-09-07 21:53 ` [PULL 18/42] bsd-user: save the path to the qemu emulator imp
2021-09-07 21:53 ` [PULL 19/42] bsd-user: start to move target CPU functions to target_arch* imp
2021-09-07 21:53 ` [PULL 20/42] bsd-user: Move per-cpu code into target_arch_cpu.h imp
2021-09-07 21:53 ` [PULL 21/42] bsd-user: pull in target_arch_thread.h update target_arch_elf.h imp
2021-09-07 21:53 ` [PULL 22/42] bsd-user: Include more things in qemu.h imp
2021-09-07 21:53 ` [PULL 23/42] bsd-user: define max args in terms of pages imp
2021-09-07 21:53 ` [PULL 24/42] bsd-user: Create target specific vmparam.h imp
2021-09-07 21:53 ` imp [this message]
2021-09-07 21:53 ` [PULL 26/42] bsd-user: *BSD specific siginfo defintions imp
2021-09-07 21:53 ` [PULL 27/42] bsd-user: Implement --seed and initialize random state imp
2021-09-07 21:53 ` [PULL 28/42] bsd-user: Move stack initializtion into a per-os file imp
2021-09-07 21:53 ` [PULL 29/42] bsd-user: Add architecture specific signal tramp code imp
2021-09-07 21:53 ` [PULL 30/42] bsd-user: elf cleanup imp
2021-09-07 21:53 ` [PULL 31/42] bsd-user: Remove dead #ifdefs from elfload.c imp
2021-09-07 21:53 ` [PULL 32/42] bsd-user: Rewrite target system call definintion glue imp
2021-09-07 21:53 ` [PULL 33/42] bsd-user: update debugging in mmap.c imp
2021-09-07 21:53 ` [PULL 34/42] bsd-user: Add target_arch_reg to describe a target's register set imp
2021-09-07 21:53 ` [PULL 35/42] bsd-user: Add target_os_user.h to capture the user/kernel structures imp
2021-09-07 21:53 ` [PULL 36/42] bsd-user: add stubbed out core dump support imp
2021-09-07 21:53 ` [PULL 37/42] bsd-user: elfload.c style catch up patch imp
2021-09-07 21:53 ` [PULL 38/42] bsd-user: Refactor load_elf_sections and is_target_elf_binary imp
2021-09-07 21:53 ` [PULL 39/42] bsd-user: move gemu_log to later in the file imp
2021-09-07 21:53 ` [PULL 40/42] bsd-user: Implement interlock for atomic operations imp
2021-09-07 21:53 ` [PULL 41/42] bsd-user: Add '-0 argv0' option to bsd-user/main.c imp
2021-09-07 21:53 ` [PULL 42/42] bsd-user: Update mapping to handle reserved and starting conditions imp
2021-09-09 15:00 ` [PULL 00/42] bsd-user updates to run hello world Peter Maydell
2021-09-09 15:12   ` Warner Losh
2021-09-09 15:21     ` Daniel P. Berrangé
2021-09-09 16:31     ` Philippe Mathieu-Daudé
2021-09-10 20:35       ` Warner Losh
2021-09-09 16:29 ` Philippe Mathieu-Daudé
2021-09-10 20:31   ` Warner Losh

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210907215332.30737-26-imp@bsdimp.com \
    --to=imp@bsdimp.com \
    --cc=imp@FreeBSD.org \
    --cc=kevans@freebsd.org \
    --cc=qemu-devel@nongnu.org \
    --cc=richard.henderson@linaro.org \
    --cc=sson@FreeBSD.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).