From patchwork Tue Oct 3 23:31:38 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Warner Losh X-Patchwork-Id: 13408127 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 5F6AEE8FDC0 for ; Tue, 3 Oct 2023 23:39:49 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qnovG-00046A-Ru; Tue, 03 Oct 2023 19:35:26 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qnovD-00042e-Lp for qemu-devel@nongnu.org; Tue, 03 Oct 2023 19:35:23 -0400 Received: from mail-io1-xd2d.google.com ([2607:f8b0:4864:20::d2d]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qnovC-0006uM-16 for qemu-devel@nongnu.org; Tue, 03 Oct 2023 19:35:23 -0400 Received: by mail-io1-xd2d.google.com with SMTP id ca18e2360f4ac-79fa2dbd793so64824839f.2 for ; Tue, 03 Oct 2023 16:35:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bsdimp-com.20230601.gappssmtp.com; s=20230601; t=1696376120; x=1696980920; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=+wZeMIbIZWAaczukeU9kCYH0/v8NZZkLUqmuVnhx+go=; b=FWPY3CwqhWgXLpXpus/On1XD0gH5NJj52UzR60NxL8YFuQbtqcFB5iXM7xm6toBynu 2z7y0rivH5H3kcedc4gL32xfu2330MgloaZJD9Pv+OBP8qyw6rhviruWYAtkFXoqgjb4 H4iTF/gNXMwnBVVagYii0pFOSoYL44OYl7avm/EowlmckifyiC8PkXlxRd7XeI6uVf2G voAjuD3zMKVxOgRjA3j1c2AY8ckOGvVfySc4sYb6evZPA795otr2zaU8Q6NM5OFEDNze nYFSVYZdw7tuzns2LHzSUeKvI+pkO/N46flc1I/Ab9gVxBtQf62lC8+QutJr/8Mk1FiU W+pA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696376120; x=1696980920; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=+wZeMIbIZWAaczukeU9kCYH0/v8NZZkLUqmuVnhx+go=; b=tH7sXQrjnKnUFsQUcZBZINOMox2wey8ozkaba9oNmxX+XM4Z5QTDSKX+eFth3oDz// NSZIpgzOvrtwhncZRQTo0+o2lFX7BShR6mKr4j98STVsbqGnS8z0to10tR34w27KMsSu 9UMsh9fRjK+y1fNLYdWavoXPvuGBim8RLrZvm4DPXKZVBHKFMAANyElv78PdiGY6d/HW IVVWacb8aIwA6qpDr1zPVLv+iXHffpuf8NVDrGo2/aNXyWCZtEjJgGbYbHQpWSQdYYq+ nGex1s/M1oPJenpWZkycYRV2jpvwzX0kgIpHNFoL40jkOAL5I/gKfoxpS/KMykrDiy5f vKIQ== X-Gm-Message-State: AOJu0YzgD4kdnWPsd2xZy7NRwtGKsF20xdNhXcNtZA7K/u6Fe2N+8lFU 8tw8PsDaPyF8EkUamUYGdiODcdHDzui1S/cxk1pYaA== X-Google-Smtp-Source: AGHT+IF06LilV5Cn0XrVWSHUAxItPfBp/84dkxgYmTo6aI8/AMpUgKW8GXT7rmL/pg+EOHUs+44A6w== X-Received: by 2002:a6b:fd01:0:b0:790:c3d0:8f87 with SMTP id c1-20020a6bfd01000000b00790c3d08f87mr834956ioi.19.1696376119699; Tue, 03 Oct 2023 16:35:19 -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 w25-20020a6b4a19000000b0079fa1a7cd36sm593596iob.30.2023.10.03.16.35.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Oct 2023 16:35:19 -0700 (PDT) From: Warner Losh To: qemu-devel@nongnu.org Cc: Thomas Huth , Laurent Vivier , =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= , =?utf-8?q?Alex?= =?utf-8?q?_Benn=C3=A9e?= , Wainer dos Santos Moschetta , Beraldo Leal , Warner Losh , Kyle Evans , Stacey Son , Karim Taha , Richard Henderson Subject: [PULL 14/51] bsd-user: Implement getrlimit(2) and setrlimit(2) Date: Tue, 3 Oct 2023 17:31:38 -0600 Message-ID: <20231003233215.95557-15-imp@bsdimp.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20231003233215.95557-1-imp@bsdimp.com> References: <20231003233215.95557-1-imp@bsdimp.com> MIME-Version: 1.0 Received-SPF: none client-ip=2607:f8b0:4864:20::d2d; envelope-from=imp@bsdimp.com; helo=mail-io1-xd2d.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.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org From: Stacey Son Signed-off-by: Stacey Son Signed-off-by: Karim Taha Reviewed-by: Richard Henderson Reviewed-by: Warner Losh Message-Id: <20230925182425.3163-15-kariem.taha2.7@gmail.com> --- bsd-user/bsd-proc.h | 59 +++++++++++++++++++++++++++++++++++ bsd-user/freebsd/os-syscall.c | 8 +++++ 2 files changed, 67 insertions(+) diff --git a/bsd-user/bsd-proc.h b/bsd-user/bsd-proc.h index 133c1b0eaf8..38d1324034c 100644 --- a/bsd-user/bsd-proc.h +++ b/bsd-user/bsd-proc.h @@ -137,4 +137,63 @@ static inline abi_long do_bsd_getrusage(abi_long who, abi_ulong target_addr) return ret; } +/* getrlimit(2) */ +static inline abi_long do_bsd_getrlimit(abi_long arg1, abi_ulong arg2) +{ + abi_long ret; + int resource = target_to_host_resource(arg1); + struct target_rlimit *target_rlim; + struct rlimit rlim; + + switch (resource) { + case RLIMIT_STACK: + rlim.rlim_cur = target_dflssiz; + rlim.rlim_max = target_maxssiz; + ret = 0; + break; + + case RLIMIT_DATA: + rlim.rlim_cur = target_dfldsiz; + rlim.rlim_max = target_maxdsiz; + ret = 0; + break; + + default: + ret = get_errno(getrlimit(resource, &rlim)); + break; + } + if (!is_error(ret)) { + if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0)) { + return -TARGET_EFAULT; + } + target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur); + target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max); + unlock_user_struct(target_rlim, arg2, 1); + } + return ret; +} + +/* setrlimit(2) */ +static inline abi_long do_bsd_setrlimit(abi_long arg1, abi_ulong arg2) +{ + abi_long ret; + int resource = target_to_host_resource(arg1); + struct target_rlimit *target_rlim; + struct rlimit rlim; + + if (RLIMIT_STACK == resource) { + /* XXX We should, maybe, allow the stack size to shrink */ + ret = -TARGET_EPERM; + } else { + if (!lock_user_struct(VERIFY_READ, target_rlim, arg2, 1)) { + return -TARGET_EFAULT; + } + rlim.rlim_cur = target_to_host_rlim(target_rlim->rlim_cur); + rlim.rlim_max = target_to_host_rlim(target_rlim->rlim_max); + unlock_user_struct(target_rlim, arg2, 0); + ret = get_errno(setrlimit(resource, &rlim)); + } + return ret; +} + #endif /* !BSD_PROC_H_ */ diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c index 5d8693ed550..5cb60862303 100644 --- a/bsd-user/freebsd/os-syscall.c +++ b/bsd-user/freebsd/os-syscall.c @@ -247,6 +247,14 @@ static abi_long freebsd_syscall(void *cpu_env, int num, abi_long arg1, ret = do_bsd_getrusage(arg1, arg2); break; + case TARGET_FREEBSD_NR_getrlimit: /* getrlimit(2) */ + ret = do_bsd_getrlimit(arg1, arg2); + break; + + case TARGET_FREEBSD_NR_setrlimit: /* setrlimit(2) */ + ret = do_bsd_setrlimit(arg1, arg2); + break; + /* * File system calls.