From nobody Fri Dec 19 07:48:05 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1628373657588331.48561405861767; Sat, 7 Aug 2021 15:00:57 -0700 (PDT) Received: from localhost ([::1]:56406 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mCUNE-0001cG-Ho for importer@patchew.org; Sat, 07 Aug 2021 18:00:56 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:43562) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mCU6G-0007sh-IH for qemu-devel@nongnu.org; Sat, 07 Aug 2021 17:43:24 -0400 Received: from mail-io1-xd44.google.com ([2607:f8b0:4864:20::d44]:38705) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mCU6D-00054M-TT for qemu-devel@nongnu.org; Sat, 07 Aug 2021 17:43:24 -0400 Received: by mail-io1-xd44.google.com with SMTP id a13so19905220iol.5 for ; Sat, 07 Aug 2021 14:43:21 -0700 (PDT) Received: from dune.bsdimp.com (50-253-99-174-static.hfc.comcastbusiness.net. [50.253.99.174]) by smtp.gmail.com with ESMTPSA id a17sm8062827ios.36.2021.08.07.14.43.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 07 Aug 2021 14:43:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bsdimp-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=UrVmQqhWrbZHbGhwPoQlA+bKpudjp4hJimuUq/4iOng=; b=iky0JXKlArxpYqR4TSmIY5hWE8SerrADFkScHs+G43EV+h69F8V8nNhRJYoQNlQyC5 O7OMVmFK4PH5P+fpVHsBM4cEpGS+o2QMu7jQSvg8H1G0qC+ApMNLPvloHZAR/bpq45rv mrrmUk895Fii/PLq5Amd4Ovfkfm2+X+AT0vKZQxjwUsb0fwEW0KRvYXZ4UTzFaVTwKL1 dDQwzfSl+VFNzAX85ilyQJe4lPNGtWTejvm7Da7WXjv066JdXaoEDJrXtty12KlsjAQ3 cerNpbLkNBpY46DoLjSckHUdAqGKfvRZNmRb5WxF++qBErwqW3JEmtLlKwXmFuoWLjjr ZH9A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=UrVmQqhWrbZHbGhwPoQlA+bKpudjp4hJimuUq/4iOng=; b=kT3GfBPsAsGkGqIvuInO+Uy+TmU7RSwC/tYojSyfVjRq1IhrmIUNZvH+LEEAukmFjq U+DrKjQIuXc8uMg4LdepDt/MYewjH5sEt66+rs971Zm38fdozILQ9CULpkETJtSQTCqx 1uqiIj3DPYL9DpR37lWGV9POyjrndzdfPOD2oaMyuE+Jfck94Nfyx/2uxm31Tf/MK49J crIiduwpJmFUN82tukPpuM0tZ1K/KHdA3dQ3eHiASdjjeJAkIfvAgIgSdoHh3ux7nhgp Jw8vKIHIlHyini8ogsAnksFqU5VFzkqKxKbaL0nVjCoeVzdJ3ytXMy+aN90MTCz7OMPF ceEw== X-Gm-Message-State: AOAM530qEJz2E4NaxIxDT3S/XO2SwCla9c7SnwSgfisR4yoqDdaaPvwC lzsBoq14LDbuBcittUj2JI36XLPMkZM0Fdmm X-Google-Smtp-Source: ABdhPJxuG77OflwD+jqKALcPRrv2rB4sNOniJ94tgY0X7zpRSOAGjS9bHcdswpFHWHi74g1mywHIkQ== X-Received: by 2002:a5d:8154:: with SMTP id f20mr217386ioo.89.1628372600692; Sat, 07 Aug 2021 14:43:20 -0700 (PDT) From: Warner Losh To: qemu-devel@nongnu.org Subject: [PATCH for 6.2 29/49] bsd-user: Add system independent stack, data and text limiting Date: Sat, 7 Aug 2021 15:42:22 -0600 Message-Id: <20210807214242.82385-30-imp@bsdimp.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210807214242.82385-1-imp@bsdimp.com> References: <20210807214242.82385-1-imp@bsdimp.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: none client-ip=2607:f8b0:4864:20::d44; envelope-from=imp@bsdimp.com; helo=mail-io1-xd44.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_NONE=0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kevans@freebsd.org, Warner Losh , Warner Losh , Stacey Son Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1628373658976100001 Content-Type: text/plain; charset="utf-8" From: Warner Losh 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 Signed-off-by: Warner Losh Sponsored by: Netflix Reviewed-by: Richard Henderson --- bsd-user/elfload.c | 2 +- bsd-user/main.c | 51 +++++++++++++++++++++++++++++++++------------- bsd-user/qemu.h | 7 ++++++- 3 files changed, 44 insertions(+), 16 deletions(-) diff --git a/bsd-user/elfload.c b/bsd-user/elfload.c index 70a0f81f3d..5ceb60b1c2 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 bs= d_binprm *bprm, /* Create enough stack to hold everything. If we don't use * it for args, we'll use it for something else... */ - size =3D x86_stack_size; + size =3D target_dflssiz; if (size < MAX_ARG_PAGES * TARGET_PAGE_SIZE) size =3D MAX_ARG_PAGES * TARGET_PAGE_SIZE; error =3D target_mmap(0, diff --git a/bsd-user/main.c b/bsd-user/main.c index 91e6abb6e4..5ca1173f04 100644 --- a/bsd-user/main.c +++ b/bsd-user/main.c @@ -18,6 +18,11 @@ * along with this program; if not, see . */ =20 +#include +#include +#include +#include + #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" =20 -#include - 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 */ =20 -/* - * XXX: on x86 MAP_GROWSDOWN only works if ESP <=3D 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 =3D 512 * 1024; +unsigned long target_maxtsiz =3D TARGET_MAXTSIZ; /* max text size */ +unsigned long target_dfldsiz =3D TARGET_DFLDSIZ; /* initial data size li= mit */ +unsigned long target_maxdsiz =3D TARGET_MAXDSIZ; /* max data size */ +unsigned long target_dflssiz =3D TARGET_DFLSSIZ; /* initial data size li= mit */ +unsigned long target_maxssiz =3D TARGET_MAXSSIZ; /* max stack size */ +unsigned long target_sgrowsiz =3D TARGET_SGROWSIZ; /* amount to grow stack= */ =20 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=3D]][,events=3D][,fi= le=3D]\n" @@ -132,7 +134,7 @@ static void usage(void) , TARGET_NAME, interp_prefix, - x86_stack_size); + target_dflssiz); exit(1); } =20 @@ -161,6 +163,22 @@ void init_task_state(TaskState *ts) ts->sigqueue_table[i].next =3D NULL; } =20 +static void +adjust_ssize(void) +{ + struct rlimit rl; + + if (getrlimit(RLIMIT_STACK, &rl) !=3D 0) + return; + + target_maxssiz =3D MIN(target_maxssiz, rl.rlim_max); + target_dflssiz =3D MIN(MAX(target_dflssiz, rl.rlim_cur), target_maxssi= z); + + rl.rlim_max =3D target_maxssiz; + rl.rlim_cur =3D target_dflssiz; + setrlimit(RLIMIT_STACK, &rl); +} + static void save_proc_pathname(char *argv0) { int mib[4]; @@ -197,6 +215,8 @@ int main(int argc, char **argv) envlist_t *envlist =3D NULL; bsd_type =3D HOST_DEFAULT_BSD_TYPE; =20 + adjust_ssize(); + if (argc <=3D 1) { usage(); } @@ -257,14 +277,17 @@ int main(int argc, char **argv) } } else if (!strcmp(r, "s")) { r =3D argv[optind++]; - rv =3D qemu_strtoul(r, &r, 0, &x86_stack_size); - if (rv < 0 || x86_stack_size <=3D 0) { + rv =3D qemu_strtoul(r, &r, 0, &target_dflssiz); + if (rv < 0 || target_dflssiz <=3D 0) { usage(); } if (*r =3D=3D 'M') { - x86_stack_size *=3D MiB; + target_dflssiz *=3D 1024 * 1024; } else if (*r =3D=3D 'k' || *r =3D=3D 'K') { - x86_stack_size *=3D KiB; + target_dflssiz *=3D 1024; + } + if (target_dflssiz > target_maxssiz) { + usage(); } } else if (!strcmp(r, "L")) { interp_prefix =3D argv[optind++]; diff --git a/bsd-user/qemu.h b/bsd-user/qemu.h index c5414c7b4c..3f741027f4 100644 --- a/bsd-user/qemu.h +++ b/bsd-user/qemu.h @@ -223,7 +223,12 @@ void mmap_fork_end(int child); =20 /* 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; =20 /* user access */ =20 --=20 2.32.0