From mboxrd@z Thu Jan 1 00:00:00 1970 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 9276DC77B73 for ; Tue, 11 Apr 2023 17:12:53 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pmHWR-00044r-8H; Tue, 11 Apr 2023 13:11:11 -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 1pmHVu-0003rF-AX for qemu-devel@nongnu.org; Tue, 11 Apr 2023 13:10:39 -0400 Received: from mail-io1-xd32.google.com ([2607:f8b0:4864:20::d32]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1pmHVr-00068w-Vw for qemu-devel@nongnu.org; Tue, 11 Apr 2023 13:10:38 -0400 Received: by mail-io1-xd32.google.com with SMTP id b25so6172ioc.7 for ; Tue, 11 Apr 2023 10:10:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bsdimp-com.20210112.gappssmtp.com; s=20210112; t=1681233032; 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=dLPGsKZ9qU3wG8gg3c0pvYiD+HZ1kzhXINDhxubxQNw=; b=Pw1b2G2fP3kaYcAm8o2MR2cB4Wr66O4u5lPRaMqMbNQ6YvpNm+XC/c6KEZTQR9Z1jJ BRzsHJRqWfbPUkUjuoqfhmzsmRvEwIX+gdn46J4/sjJNbkpknxDluBsstiEQ90yM1LKj B79xs84PDcPVVhTtS6keBYI5p1R1NItbO92c8qAKYeU/F55h25dOftpkkGISkQ94pMtv Fv2U1ecYuwqK1F/uXaJ+9zf04qbjcyV6XJY2yRqtLYYB0zrksmFzA+/dlITSQQdl1bgZ /SNlrDkHyGP3gWLz0tdRhHoLCRmziJorSN6OKDQWc3tPbfEvm33+/LPzC8vrdtZfaADk 48eA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1681233032; 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=dLPGsKZ9qU3wG8gg3c0pvYiD+HZ1kzhXINDhxubxQNw=; b=ZkTIJ1t05graTbci7cbYYlfUswa2wKckm/56qjKB9uooNej7X+Fcb51Mh26cM6N9j+ l93HWdy4q+Y3l73FDVlvwlkX3ZhkfdhrBA2/fmNeyxMXxrvK8zuBV1XsSoIkW98Hltuy gNA8sT9JG2TygbaYYvLd0Ow1lApBd1WV0Zq1siHsZBWO74ueCQRTEVO408BOkhsSDNcS 6DqBbrzWcTJ4rXvUGKKBkoueTq41uzDmFrNv4ID4xsKayxEe+FyJXU4p62klderc4lWi 04BenaT9P6KbpHneQ3Nm2lNT5eLsT+FJ/5QkhUuiA28pQR3AeVuKQmfvV1KG7hxn7DTM DuGg== X-Gm-Message-State: AAQBX9fdQIjnHcPPPKsDC41XImONJIIkOXyprRkC9yRB+f4izV3SaliQ cHUpHCMnRRt0gK0jSdsfl3isVRaYC/TUvAUuIEo= X-Google-Smtp-Source: AKy350YiWanQyNVBH2FlfyDpLtxv+V4ncYNeQxa51fB0tQVP5gPIjRnH+TyxKONJeJgkqxBQZ6IOzA== X-Received: by 2002:a5e:d515:0:b0:760:8476:8717 with SMTP id e21-20020a5ed515000000b0076084768717mr3618430iom.0.1681233032525; Tue, 11 Apr 2023 10:10:32 -0700 (PDT) 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 d36-20020a0285a7000000b0040b11b7ef54sm4140942jai.39.2023.04.11.10.10.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 Apr 2023 10:10:32 -0700 (PDT) From: Warner Losh To: qemu-devel@nongnu.org Cc: ryoon@netbsd.org, kevans@freebsd.org, =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Thomas Huth , riastradh@netbsd.org, brad@comstyle.com, reinoud@netbsd.org, jrtc27@jrtc27.com, =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , Paolo Bonzini , Warner Losh , Stacey Son , Richard Henderson Subject: [PATCH v3 10/20] bsd-user: Implement do_sysctl_kern_getprocs Date: Tue, 11 Apr 2023 11:09:45 -0600 Message-Id: <20230411170955.17358-11-imp@bsdimp.com> X-Mailer: git-send-email 2.40.0 In-Reply-To: <20230411170955.17358-1-imp@bsdimp.com> References: <20230411170955.17358-1-imp@bsdimp.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Received-SPF: none client-ip=2607:f8b0:4864:20::d32; envelope-from=imp@bsdimp.com; helo=mail-io1-xd32.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 Implement do_sysctl_kern_getprocs to retrieve proc info from the kernel. Signed-off-by: Stacey Son Signed-off-by: Warner Losh Reviewed-by: Richard Henderson --- bsd-user/freebsd/os-sys.c | 165 +++++++++++++++++++++++++++++++++++++- bsd-user/qemu.h | 3 + 2 files changed, 167 insertions(+), 1 deletion(-) diff --git a/bsd-user/freebsd/os-sys.c b/bsd-user/freebsd/os-sys.c index df317065587..d4a6dcc6c2b 100644 --- a/bsd-user/freebsd/os-sys.c +++ b/bsd-user/freebsd/os-sys.c @@ -19,9 +19,14 @@ #include "qemu/osdep.h" #include "qemu.h" +#include "qemu-bsd.h" #include "target_arch_sysarch.h" - +#include "signal-common.h" +#include #include +#include /* For struct kinfo_* */ + +#include "target_os_user.h" /* * Length for the fixed length types. @@ -107,6 +112,164 @@ static abi_ulong h2g_ulong_sat(u_long ul) */ #define bsd_get_ncpu() 1 +static void +host_to_target_kinfo_proc(struct target_kinfo_proc *tki, struct kinfo_proc *hki) +{ + int i; + + __put_user(sizeof(struct target_kinfo_proc), &tki->ki_structsize); + __put_user(hki->ki_layout, &tki->ki_layout); + + /* Some of these are used as flags (e.g. ki_fd == NULL in procstat). */ + tki->ki_args = tswapal((abi_ulong)(uintptr_t)hki->ki_args); + tki->ki_paddr = tswapal((abi_ulong)(uintptr_t)hki->ki_paddr); + tki->ki_addr = tswapal((abi_ulong)(uintptr_t)hki->ki_addr); + tki->ki_tracep = tswapal((abi_ulong)(uintptr_t)hki->ki_tracep); + tki->ki_textvp = tswapal((abi_ulong)(uintptr_t)hki->ki_textvp); + tki->ki_fd = tswapal((abi_ulong)(uintptr_t)hki->ki_fd); + tki->ki_vmspace = tswapal((abi_ulong)(uintptr_t)hki->ki_vmspace); + tki->ki_wchan = tswapal((abi_ulong)(uintptr_t)hki->ki_wchan); + + __put_user(hki->ki_pid, &tki->ki_pid); + __put_user(hki->ki_ppid, &tki->ki_ppid); + __put_user(hki->ki_pgid, &tki->ki_pgid); + __put_user(hki->ki_tpgid, &tki->ki_tpgid); + __put_user(hki->ki_sid, &tki->ki_sid); + __put_user(hki->ki_tsid, &tki->ki_tsid); + __put_user(hki->ki_jobc, &tki->ki_jobc); + __put_user(hki->ki_tdev, &tki->ki_tdev); + + host_to_target_sigset(&tki->ki_siglist, &hki->ki_siglist); + host_to_target_sigset(&tki->ki_sigmask, &hki->ki_sigmask); + host_to_target_sigset(&tki->ki_sigignore, &hki->ki_sigignore); + host_to_target_sigset(&tki->ki_sigcatch, &hki->ki_sigcatch); + + __put_user(hki->ki_uid, &tki->ki_uid); + __put_user(hki->ki_ruid, &tki->ki_ruid); + __put_user(hki->ki_svuid, &tki->ki_svuid); + __put_user(hki->ki_rgid, &tki->ki_rgid); + __put_user(hki->ki_svgid, &tki->ki_svgid); + __put_user(hki->ki_ngroups, &tki->ki_ngroups); + + for (i=0; i < TARGET_KI_NGROUPS; i++) + __put_user(hki->ki_groups[i], &tki->ki_groups[i]); + + __put_user(hki->ki_size, &tki->ki_size); + + __put_user(hki->ki_rssize, &tki->ki_rssize); + __put_user(hki->ki_swrss, &tki->ki_swrss); + __put_user(hki->ki_tsize, &tki->ki_tsize); + __put_user(hki->ki_dsize, &tki->ki_dsize); + __put_user(hki->ki_ssize, &tki->ki_ssize); + + __put_user(hki->ki_xstat, &tki->ki_xstat); + __put_user(hki->ki_acflag, &tki->ki_acflag); + + __put_user(hki->ki_pctcpu, &tki->ki_pctcpu); + + __put_user(hki->ki_estcpu, &tki->ki_estcpu); + __put_user(hki->ki_slptime, &tki->ki_slptime); + __put_user(hki->ki_swtime, &tki->ki_swtime); + __put_user(hki->ki_cow, &tki->ki_cow); + __put_user(hki->ki_runtime, &tki->ki_runtime); + + __put_user(hki->ki_start.tv_sec, &tki->ki_start.tv_sec); + __put_user(hki->ki_start.tv_usec, &tki->ki_start.tv_usec); + __put_user(hki->ki_childtime.tv_sec, &tki->ki_childtime.tv_sec); + __put_user(hki->ki_childtime.tv_usec, &tki->ki_childtime.tv_usec); + + __put_user(hki->ki_flag, &tki->ki_flag); + __put_user(hki->ki_kiflag, &tki->ki_kiflag); + + __put_user(hki->ki_traceflag, &tki->ki_traceflag); + __put_user(hki->ki_stat, &tki->ki_stat); + __put_user(hki->ki_nice, &tki->ki_nice); + __put_user(hki->ki_lock, &tki->ki_lock); + __put_user(hki->ki_rqindex, &tki->ki_rqindex); + __put_user(hki->ki_oncpu_old, &tki->ki_oncpu_old); + __put_user(hki->ki_lastcpu_old, &tki->ki_lastcpu_old); + + strncpy(tki->ki_tdname, hki->ki_tdname, TARGET_TDNAMLEN+1); + strncpy(tki->ki_wmesg, hki->ki_wmesg, TARGET_WMESGLEN+1); + strncpy(tki->ki_login, hki->ki_login, TARGET_LOGNAMELEN+1); + strncpy(tki->ki_lockname, hki->ki_lockname, TARGET_LOCKNAMELEN+1); + strncpy(tki->ki_comm, hki->ki_comm, TARGET_COMMLEN+1); + strncpy(tki->ki_emul, hki->ki_emul, TARGET_KI_EMULNAMELEN+1); + strncpy(tki->ki_loginclass, hki->ki_loginclass, TARGET_LOGINCLASSLEN+1); + + __put_user(hki->ki_oncpu, &tki->ki_oncpu); + __put_user(hki->ki_lastcpu, &tki->ki_lastcpu); + __put_user(hki->ki_tracer, &tki->ki_tracer); + __put_user(hki->ki_flag2, &tki->ki_flag2); + __put_user(hki->ki_fibnum, &tki->ki_fibnum); + __put_user(hki->ki_cr_flags, &tki->ki_cr_flags); + __put_user(hki->ki_jid, &tki->ki_jid); + __put_user(hki->ki_numthreads, &tki->ki_numthreads); + __put_user(hki->ki_tid, &tki->ki_tid); + + memcpy(&tki->ki_pri, &hki->ki_pri, sizeof(struct target_priority)); + + h2g_rusage(&hki->ki_rusage, &tki->ki_rusage); + h2g_rusage(&hki->ki_rusage_ch, &tki->ki_rusage_ch); + + __put_user(((uintptr_t)hki->ki_pcb), &tki->ki_pcb); + __put_user(((uintptr_t)hki->ki_kstack), &tki->ki_kstack); + __put_user(((uintptr_t)hki->ki_udata), &tki->ki_udata); + __put_user(((uintptr_t)hki->ki_tdaddr), &tki->ki_tdaddr); + + __put_user(hki->ki_sflag, &tki->ki_sflag); + __put_user(hki->ki_tdflags, &tki->ki_tdflags); +} + +abi_long +do_sysctl_kern_getprocs(int op, int arg, size_t olen, + struct target_kinfo_proc *tki, size_t *tlen) +{ + abi_long ret; + struct kinfo_proc *kipp; + int mib[4], num, i, miblen; + size_t len; + + if (tlen == NULL) + return -TARGET_EINVAL; + + mib[0] = CTL_KERN; + mib[1] = KERN_PROC; + mib[2] = op; + mib[3] = arg; + + miblen = (op == KERN_PROC_ALL || op == KERN_PROC_PROC) ? 3 : 4; + + len = 0; + ret = get_errno(sysctl(mib, miblen, NULL, &len, NULL, 0)); + if (is_error(ret)) + return ret; + + num = len / sizeof(*kipp); + *tlen = num * sizeof(struct target_kinfo_proc); + if (tki == NULL) + return ret; + + if (olen < *tlen) + return -TARGET_EINVAL; + + kipp = g_malloc(len); + if (kipp == NULL) + return -TARGET_ENOMEM; + ret = get_errno(sysctl(mib, miblen, kipp, &len, NULL, 0)); + num = len / sizeof(*kipp); + *tlen = num * sizeof(struct target_kinfo_proc); + if (len % sizeof(*kipp) != 0 || kipp->ki_structsize != sizeof(*kipp)) { + ret = -TARGET_EINVAL; /* XXX */ + } else if (!is_error(ret)) { + for(i=0; i < num; i++) + host_to_target_kinfo_proc(&tki[i], &kipp[i]); + } + + g_free(kipp); + return ret; +} + /* * This uses the undocumented oidfmt interface to find the kind of a requested * sysctl, see /sys/kern/kern_sysctl.c:sysctl_sysctl_oidfmt() (compare to diff --git a/bsd-user/qemu.h b/bsd-user/qemu.h index 49468734d44..fcaf794ad6e 100644 --- a/bsd-user/qemu.h +++ b/bsd-user/qemu.h @@ -258,6 +258,9 @@ bool is_error(abi_long ret); int host_to_target_errno(int err); /* os-sys.c */ +struct target_kinfo_proc; +abi_long do_sysctl_kern_getprocs(int op, int arg, size_t olen, + struct target_kinfo_proc *tki, size_t *tlen); abi_long do_freebsd_sysctl(CPUArchState *env, abi_ulong namep, int32_t namelen, abi_ulong oldp, abi_ulong oldlenp, abi_ulong newp, abi_ulong newlen); abi_long do_freebsd_sysctlbyname(CPUArchState *env, abi_ulong namep, -- 2.40.0