From nobody Mon Feb 9 11:28:43 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; 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 ARC-Seal: i=1; a=rsa-sha256; t=1677695137; cv=none; d=zohomail.com; s=zohoarc; b=m7fshrziOcLGWy/wKkGEFMsmBo9/cZPihQJtBjVMDKWVajKHUbNX5CO5r7m99kffL40ogzdyDcyYAWInHNoZd5EwFv1D5JY6OE3cS3GWj3yQmYnbr+M3m7Vfh4i+hLIghFRXLKJX4SkE7yh80OBRAwIO/bRXZ+gV8lQ/iwKp+bQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1677695137; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=izUUSmVMMLad6kw3jAaqzTh4DzadBB2bLLAowF8XXvA=; b=KrV7RUB85zb5Sj1CoDQqrXPW4UTIGzxYs7XilePNwO3VFgMDSQBHWM1AfqslQw3eejSo/q5lMTZgHa7bPdPiIjAgjTJVyuDIiR7ZD4Ilb4q3bo0fJPPq5jUE/pRZKh3+TYrrBdSuY9EmfM8Vyn7OYf+M/RPydXOZWQ375noD73k= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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 1677695137250801.5513924241183; Wed, 1 Mar 2023 10:25:37 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pXR7j-0005ZJ-R1; Wed, 01 Mar 2023 13:24:19 -0500 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 1pXR7i-0005Xf-NK for qemu-devel@nongnu.org; Wed, 01 Mar 2023 13:24:18 -0500 Received: from mail-io1-xd2a.google.com ([2607:f8b0:4864:20::d2a]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1pXR7h-0006pV-4c for qemu-devel@nongnu.org; Wed, 01 Mar 2023 13:24:18 -0500 Received: by mail-io1-xd2a.google.com with SMTP id v10so5743765iox.8 for ; Wed, 01 Mar 2023 10:24:16 -0800 (PST) Received: from dune.bsdimp.com (c-71-237-47-177.hsd1.co.comcast.net. [71.237.47.177]) by smtp.gmail.com with ESMTPSA id l5-20020a6b7b05000000b0073f8a470bacsm4169316iop.16.2023.03.01.10.24.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Mar 2023 10:24:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bsdimp-com.20210112.gappssmtp.com; s=20210112; t=1677695056; 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=izUUSmVMMLad6kw3jAaqzTh4DzadBB2bLLAowF8XXvA=; b=Cx4Pm+hQ+2KR5pMe2EXrKniIOU2q0A9yYEuoBdqNkElyrF7bKREJn32gN6lH4A4upi 5Av7JaWb3F1HdEx9WuEjjNHf8FggEtVP9IIn3hYvWRPaIImQOtaFgQwInh6qRxwS2kqA 5TbXC8NNanyyqg1paQ/JngH1lPuM0WXt1e5QeAfVrk3hYsO7avzU7Wxz0ji1u+JcCDSK CHN77Qr6deyBwfSjJHsmyJ9f98yU2qwNKJ4IRGe9lLp4lfcPlJUgmrOW5F4sWKMKdHL3 GyX2OHPH3m1CAaxdfFfMJZCB9GHLxDVKhrsWf6zd3QhDJueKA18c2i1pwDbtTaafZWBv M52Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1677695056; 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=izUUSmVMMLad6kw3jAaqzTh4DzadBB2bLLAowF8XXvA=; b=Bhzmc+lQDUPTTbltdw/JzQ8zRMeMArwRWD54ONjxofh8YprLjqcuksUImKmT1jIryX R/e6GNEzfP3LEKrmPlICyHq0L2Z1nFHUeVY91iu5tWiFelZ82rwbH5cAy+Vc/BlsQzfY OSQN2MvS1Zb9BBJJDr2qu4NXLX/IE/mxM20VEwxmgJcs7RVWSD/Z2qDISbLIULLoRrTS IDjXQZcv9tXn6tUqmPISjg8TxjGgggkxxg3u+kP+Axrd4PfLBwsXms8bBcdcwh3zL3u2 n9+s77Tc1I5dERoSFGmkdek1asP6fGT8UjdlG/0FWbFV7rdADRI2JuRPPh1eFgwdwalk PpEQ== X-Gm-Message-State: AO0yUKWLl5vKSPbExpLh+x6HSmr3NfN0Rai8+SauIPzmnzXj0wUzoT6A hMEnXqzjm51AmyH3CaoK4nAYbvgA391RLbc/ X-Google-Smtp-Source: AK7set//HoG/rzR6f39nmr8PpbuSH7YwchLIwPbdfb5TaIE97JtrA6HQTs/JQUwHHFGtOHg2HH1oXA== X-Received: by 2002:a05:6602:22c5:b0:74c:8b1c:239c with SMTP id e5-20020a05660222c500b0074c8b1c239cmr4472109ioe.10.1677695055613; Wed, 01 Mar 2023 10:24:15 -0800 (PST) From: Warner Losh To: qemu-devel@nongnu.org Cc: Kyle Evans , Warner Losh , Thomas Huth , =?UTF-8?q?Alex=20Benn=C3=A9e?= , richard.henderson@linaro.org, f4bug@amsat.org, Paolo Bonzini , Kyle Evans Subject: [PULL 10/10] bsd-user: implement sysctlbyname(2) Date: Wed, 1 Mar 2023 11:23:53 -0700 Message-Id: <20230301182353.21559-11-imp@bsdimp.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230301182353.21559-1-imp@bsdimp.com> References: <20230301182353.21559-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::d2a; envelope-from=imp@bsdimp.com; helo=mail-io1-xd2a.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+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @bsdimp-com.20210112.gappssmtp.com) X-ZM-MESSAGEID: 1677695138180100017 Content-Type: text/plain; charset="utf-8" From: Kyle Evans do_freebsd_sysctlbyname needs to translate the 'name' back down to a OID so we can intercept the special ones. Do that and call the common wrapper do_freebsd_sysctl_oid. Signed-off-by: Kyle Evans Reviewed-by: Warner Losh Signed-off-by: Warner Losh Reviewed-by: Richard Henderson --- bsd-user/freebsd/os-sys.c | 67 +++++++++++++++++++++++++++++++++++ bsd-user/freebsd/os-syscall.c | 4 +++ bsd-user/qemu.h | 3 ++ 3 files changed, 74 insertions(+) diff --git a/bsd-user/freebsd/os-sys.c b/bsd-user/freebsd/os-sys.c index 646b8e3f74d..df317065587 100644 --- a/bsd-user/freebsd/os-sys.c +++ b/bsd-user/freebsd/os-sys.c @@ -471,6 +471,73 @@ out: return ret; } =20 +/* + * This syscall was created to make sysctlbyname(3) more efficient, but we= can't + * really provide it in bsd-user. Notably, we must always translate the n= ames + * independently since some sysctl values have to be faked for the target + * environment, so it still has to break down to two syscalls for the unde= rlying + * implementation. + */ +abi_long do_freebsd_sysctlbyname(CPUArchState *env, abi_ulong namep, + int32_t namelen, abi_ulong oldp, abi_ulong oldlenp, abi_ulong newp, + abi_ulong newlen) +{ + abi_long ret =3D -TARGET_EFAULT; + void *holdp =3D NULL, *hnewp =3D NULL; + char *snamep =3D NULL; + int oid[CTL_MAXNAME + 2]; + size_t holdlen, oidplen; + abi_ulong oldlen =3D 0; + + /* oldlenp is read/write, pre-check here for write */ + if (oldlenp) { + if (!access_ok(VERIFY_WRITE, oldlenp, sizeof(abi_ulong)) || + get_user_ual(oldlen, oldlenp)) { + goto out; + } + } + snamep =3D lock_user_string(namep); + if (snamep =3D=3D NULL) { + goto out; + } + if (newp) { + hnewp =3D lock_user(VERIFY_READ, newp, newlen, 1); + if (hnewp =3D=3D NULL) { + goto out; + } + } + if (oldp) { + holdp =3D lock_user(VERIFY_WRITE, oldp, oldlen, 0); + if (holdp =3D=3D NULL) { + goto out; + } + } + holdlen =3D oldlen; + + oidplen =3D ARRAY_SIZE(oid); + if (sysctlnametomib(snamep, oid, &oidplen) !=3D 0) { + ret =3D -TARGET_EINVAL; + goto out; + } + + ret =3D do_freebsd_sysctl_oid(env, oid, oidplen, holdp, &holdlen, hnew= p, + newlen); + + /* + * writeability pre-checked above. __sysctl(2) returns ENOMEM and upda= tes + * oldlenp for the proper size to use. + */ + if (oldlenp && (ret =3D=3D 0 || ret =3D=3D -TARGET_ENOMEM)) { + put_user_ual(holdlen, oldlenp); + } +out: + unlock_user(snamep, namep, 0); + unlock_user(holdp, oldp, ret =3D=3D 0 ? holdlen : 0); + unlock_user(hnewp, newp, 0); + + return ret; +} + abi_long do_freebsd_sysctl(CPUArchState *env, abi_ulong namep, int32_t nam= elen, abi_ulong oldp, abi_ulong oldlenp, abi_ulong newp, abi_ulong newle= n) { diff --git a/bsd-user/freebsd/os-syscall.c b/bsd-user/freebsd/os-syscall.c index 20ab3d4d9a1..179a20c304b 100644 --- a/bsd-user/freebsd/os-syscall.c +++ b/bsd-user/freebsd/os-syscall.c @@ -498,6 +498,10 @@ static abi_long freebsd_syscall(void *cpu_env, int num= , abi_long arg1, ret =3D do_freebsd_sysctl(cpu_env, arg1, arg2, arg3, arg4, arg5, a= rg6); break; =20 + case TARGET_FREEBSD_NR___sysctlbyname: /* sysctlbyname(2) */ + ret =3D do_freebsd_sysctlbyname(cpu_env, arg1, arg2, arg3, arg4, a= rg5, arg6); + break; + case TARGET_FREEBSD_NR_sysarch: /* sysarch(2) */ ret =3D do_freebsd_sysarch(cpu_env, arg1, arg2); break; diff --git a/bsd-user/qemu.h b/bsd-user/qemu.h index 208772d4ed6..41d84e0b81b 100644 --- a/bsd-user/qemu.h +++ b/bsd-user/qemu.h @@ -255,6 +255,9 @@ int host_to_target_errno(int err); /* os-sys.c */ abi_long do_freebsd_sysctl(CPUArchState *env, abi_ulong namep, int32_t nam= elen, abi_ulong oldp, abi_ulong oldlenp, abi_ulong newp, abi_ulong newle= n); +abi_long do_freebsd_sysctlbyname(CPUArchState *env, abi_ulong namep, + int32_t namelen, abi_ulong oldp, abi_ulong oldlenp, abi_ulong newp, + abi_ulong newlen); abi_long do_freebsd_sysarch(void *cpu_env, abi_long arg1, abi_long arg2); =20 /* user access */ --=20 2.39.1