From nobody Wed Feb 5 09:34:39 2025 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4Ynw7354GZz5mT8s; Wed, 05 Feb 2025 09:34:39 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R11" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4Ynw731Rqfz3W46; Wed, 05 Feb 2025 09:34:39 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1738748079; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=KaXYdV22E6t0i/RIEXhzRC7HigEoNxow8C4reHQe36Q=; b=uf+zygB9j/jY5QY8MASydRsofQaC1lMCK+mGgCIE+jTICBtQ0sWk+XSEVEo6lIFkFyiZfk STm74TG2VsstyDD33PhzBzxMlwV3ekeeTzNruzRQa/BLStQjIk1UhCeCzHMKS5G/kB7CBJ irkgF5ydTELO+ok3lJSqI6RkkIZuuiLbW8iJs1bbHmbZNyOFmznSwufSsYn/+k/GgjRFhm Mf9NVwrgr7snOTXxgWHwTgNLpXupox17NkNLBZG2VDsLx0/wmB0JIjAegpAon5mGwAzAQg 1TqqKJQLYNbboYZU8qvCn+LAakYdtY5ZDI0jyh6rIfjYBd2I+EEl+cJ6psKENA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1738748079; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=KaXYdV22E6t0i/RIEXhzRC7HigEoNxow8C4reHQe36Q=; b=Lf4GOvCprkwIEahimRuBRv0IPiXBv6heu38lIFqGdpsC+Mdrisdu4QO9Axj0GAvbczMP9h LHyYCq7dL/n8tY5D5SkpvOv6BDBMZC0OGkTLnUibKV3947TaTgivg1Kf2lXNjuroHE5zSW hMvaJ7KSF4Qcl1fgKtVCp00bIcYZdnCnCSJZt0f3pihuUun9mFnNZopFKZoK6Mov/7TavB BTyN1CZ1NGlEmX4cSCNn34zRTrCBxcHWXODbEwjXiqYj94gDjaOIeyK69xsGsAnjYSN0UE ri8b5l9Bhc0f2VD0k8pQoYBi3QmbbVV7lPVi6trKr6qWwZBCQW0EpFKooAA2Eg== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1738748079; a=rsa-sha256; cv=none; b=h7ES1mzINYp0YVaWSq1Aaem6KU+sltmCBjVfLhp8w/3aV04yZ5XaRev7rrkFaPAN2xHKh2 9ag2JiSdzs+VZefqqOVDZKJ4a2D3DT26I6y3f/oAST2bjAaCGmHIQ+0eDMC2TSV8yBgAJa C8zf7XCvkfuvVadCREO3pqPJWBfdYAXHJyenoiGF5BEwpqb256hQxB9tY9/hhv1bLd7iC1 VpTM1QCnqmaaXCUDxcjSfPmphFdtrhuBX8ZdTv9DWIb9+KexjHr7mRgjJ6MfZgv7B0dzNp wSrLshwMomTVQhQQnVgAeWJXSEkjjVlOz8nO9ldAgzeSahWIeedNEtatMLSxpg== ARC-Authentication-Results: i=1; mx1.freebsd.org; none Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4Ynw730tDbzmtX; Wed, 05 Feb 2025 09:34:39 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 5159YdQu025537; Wed, 5 Feb 2025 09:34:39 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 5159YdMH025534; Wed, 5 Feb 2025 09:34:39 GMT (envelope-from git) Date: Wed, 5 Feb 2025 09:34:39 GMT Message-Id: <202502050934.5159YdMH025534@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Ruslan Bukin Subject: git: 4eee13813967 - main - riscv vmm: clean up SBI code List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-main@freebsd.org Sender: owner-dev-commits-src-main@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: br X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 4eee1381396714175495b395bd6e74b263b2b16d Auto-Submitted: auto-generated The branch main has been updated by br: URL: https://cgit.FreeBSD.org/src/commit/?id=4eee1381396714175495b395bd6e74b263b2b16d commit 4eee1381396714175495b395bd6e74b263b2b16d Author: Ruslan Bukin AuthorDate: 2025-02-05 09:20:23 +0000 Commit: Ruslan Bukin CommitDate: 2025-02-05 09:21:23 +0000 riscv vmm: clean up SBI code - Use SBI standard error codes (with respect to the spec) - riscv_send_ipi() now takes cpuset_t* as an argument Differential Revision: https://reviews.freebsd.org/D48575 --- sys/riscv/vmm/riscv.h | 4 +- sys/riscv/vmm/vmm_riscv.c | 29 ++++++------- sys/riscv/vmm/vmm_sbi.c | 101 +++++++++++++++++++--------------------------- 3 files changed, 58 insertions(+), 76 deletions(-) diff --git a/sys/riscv/vmm/riscv.h b/sys/riscv/vmm/riscv.h index 793c61534cee..870d0d6c5cd1 100644 --- a/sys/riscv/vmm/riscv.h +++ b/sys/riscv/vmm/riscv.h @@ -150,9 +150,9 @@ DEFINE_VMMOPS_IFUNC(void, vmspace_free, (struct vmspace *vmspace)) struct hypctx *riscv_get_active_vcpu(void); void vmm_switch(struct hypctx *); void vmm_unpriv_trap(struct hyptrap *, uint64_t tmp); -int vmm_sbi_ecall(struct vcpu *, bool *); +bool vmm_sbi_ecall(struct vcpu *); -void riscv_send_ipi(struct hypctx *hypctx, int hart_id); +void riscv_send_ipi(struct hyp *hyp, cpuset_t *cpus); int riscv_check_ipi(struct hypctx *hypctx, bool clear); bool riscv_check_interrupts_pending(struct hypctx *hypctx); diff --git a/sys/riscv/vmm/vmm_riscv.c b/sys/riscv/vmm/vmm_riscv.c index 78250ae7c440..ca2ef50dbd24 100644 --- a/sys/riscv/vmm/vmm_riscv.c +++ b/sys/riscv/vmm/vmm_riscv.c @@ -1,7 +1,7 @@ /*- * SPDX-License-Identifier: BSD-2-Clause * - * Copyright (c) 2024 Ruslan Bukin + * Copyright (c) 2024-2025 Ruslan Bukin * * This software was developed by the University of Cambridge Computer * Laboratory (Department of Computer Science and Technology) under Innovate @@ -450,7 +450,6 @@ riscv_handle_world_switch(struct hypctx *hypctx, struct vm_exit *vme, uint64_t insn; uint64_t gpa; bool handled; - bool retu; int ret; int i; @@ -496,16 +495,12 @@ riscv_handle_world_switch(struct hypctx *hypctx, struct vm_exit *vme, handled = false; break; case SCAUSE_VIRTUAL_SUPERVISOR_ECALL: - retu = false; - vmm_sbi_ecall(hypctx->vcpu, &retu); - if (retu == false) { - handled = true; + handled = vmm_sbi_ecall(hypctx->vcpu); + if (handled == true) break; - } for (i = 0; i < nitems(vme->u.ecall.args); i++) vme->u.ecall.args[i] = hypctx->guest_regs.hyp_a[i]; vme->exitcode = VM_EXITCODE_ECALL; - handled = false; break; case SCAUSE_VIRTUAL_INSTRUCTION: insn = vme->stval; @@ -537,17 +532,23 @@ vmmops_gla2gpa(void *vcpui, struct vm_guest_paging *paging, uint64_t gla, } void -riscv_send_ipi(struct hypctx *hypctx, int hart_id) +riscv_send_ipi(struct hyp *hyp, cpuset_t *cpus) { - struct hyp *hyp; + struct hypctx *hypctx; struct vm *vm; + uint16_t maxcpus; + int i; - hyp = hypctx->hyp; vm = hyp->vm; - atomic_set_32(&hypctx->ipi_pending, 1); - - vcpu_notify_event(vm_vcpu(vm, hart_id)); + maxcpus = vm_get_maxcpus(hyp->vm); + for (i = 0; i < maxcpus; i++) { + if (!CPU_ISSET(i, cpus)) + continue; + hypctx = hyp->ctx[i]; + atomic_set_32(&hypctx->ipi_pending, 1); + vcpu_notify_event(vm_vcpu(vm, i)); + } } int diff --git a/sys/riscv/vmm/vmm_sbi.c b/sys/riscv/vmm/vmm_sbi.c index 3ba90e349b3c..426276444357 100644 --- a/sys/riscv/vmm/vmm_sbi.c +++ b/sys/riscv/vmm/vmm_sbi.c @@ -1,7 +1,7 @@ /*- * SPDX-License-Identifier: BSD-2-Clause * - * Copyright (c) 2024 Ruslan Bukin + * Copyright (c) 2024-2025 Ruslan Bukin * * This software was developed by the University of Cambridge Computer * Laboratory (Department of Computer Science and Technology) under Innovate @@ -32,29 +32,8 @@ #include #include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include #include -#include -#include -#include -#include - -#include -#include -#include -#include -#include #include #include "riscv.h" @@ -64,13 +43,13 @@ static int vmm_sbi_handle_rfnc(struct vcpu *vcpu, struct hypctx *hypctx) { struct vmm_fence fence; + cpuset_t active_cpus; uint64_t hart_mask; uint64_t hart_mask_base; uint64_t func_id; struct hyp *hyp; uint16_t maxcpus; cpuset_t cpus; - int vcpu_id; int i; func_id = hypctx->guest_regs.hyp_a[6]; @@ -94,31 +73,39 @@ vmm_sbi_handle_rfnc(struct vcpu *vcpu, struct hypctx *hypctx) fence.type = VMM_RISCV_FENCE_VMA_ASID; break; default: - return (-1); + return (SBI_ERR_NOT_SUPPORTED); } /* Construct cpuset_t from the mask supplied. */ - CPU_ZERO(&cpus); hyp = hypctx->hyp; + active_cpus = vm_active_cpus(hyp->vm); maxcpus = vm_get_maxcpus(hyp->vm); for (i = 0; i < maxcpus; i++) { vcpu = vm_vcpu(hyp->vm, i); if (vcpu == NULL) continue; - vcpu_id = vcpu_vcpuid(vcpu); if (hart_mask_base != -1UL) { - if (vcpu_id < hart_mask_base) + if (i < hart_mask_base) continue; - if (!(hart_mask & (1UL << (vcpu_id - hart_mask_base)))) + if (!(hart_mask & (1UL << (i - hart_mask_base)))) continue; } + /* + * If either hart_mask_base or at least one hartid from + * hart_mask is not valid, then return error. + */ + if (!CPU_ISSET(i, &active_cpus)) + return (SBI_ERR_INVALID_PARAM); CPU_SET(i, &cpus); } + if (CPU_EMPTY(&cpus)) + return (SBI_ERR_INVALID_PARAM); + vmm_fence_add(hyp->vm, &cpus, &fence); - return (0); + return (SBI_SUCCESS); } static int @@ -126,7 +113,6 @@ vmm_sbi_handle_time(struct vcpu *vcpu, struct hypctx *hypctx) { uint64_t func_id; uint64_t next_val; - int ret; func_id = hypctx->guest_regs.hyp_a[6]; next_val = hypctx->guest_regs.hyp_a[0]; @@ -134,31 +120,25 @@ vmm_sbi_handle_time(struct vcpu *vcpu, struct hypctx *hypctx) switch (func_id) { case SBI_TIME_SET_TIMER: vtimer_set_timer(hypctx, next_val); - ret = 0; break; default: - ret = -1; - break; + return (SBI_ERR_NOT_SUPPORTED); } - hypctx->guest_regs.hyp_a[0] = ret; - - return (0); + return (SBI_SUCCESS); } static int vmm_sbi_handle_ipi(struct vcpu *vcpu, struct hypctx *hypctx) { - struct hypctx *target_hypctx; - struct vcpu *target_vcpu __unused; cpuset_t active_cpus; struct hyp *hyp; uint64_t hart_mask; uint64_t hart_mask_base; uint64_t func_id; + cpuset_t cpus; int hart_id; int bit; - int ret; func_id = hypctx->guest_regs.hyp_a[6]; hart_mask = hypctx->guest_regs.hyp_a[0]; @@ -170,6 +150,7 @@ vmm_sbi_handle_ipi(struct vcpu *vcpu, struct hypctx *hypctx) active_cpus = vm_active_cpus(hyp->vm); + CPU_ZERO(&cpus); switch (func_id) { case SBI_IPI_SEND_IPI: while ((bit = ffs(hart_mask))) { @@ -177,30 +158,28 @@ vmm_sbi_handle_ipi(struct vcpu *vcpu, struct hypctx *hypctx) hart_mask &= ~(1u << hart_id); if (hart_mask_base != -1) hart_id += hart_mask_base; - if (CPU_ISSET(hart_id, &active_cpus)) { - /* TODO. */ - target_vcpu = vm_vcpu(hyp->vm, hart_id); - target_hypctx = hypctx->hyp->ctx[hart_id]; - riscv_send_ipi(target_hypctx, hart_id); - } + if (!CPU_ISSET(hart_id, &active_cpus)) + return (SBI_ERR_INVALID_PARAM); + CPU_SET(hart_id, &cpus); } - ret = 0; break; default: - printf("%s: unknown func %ld\n", __func__, func_id); - ret = -1; - break; + dprintf("%s: unknown func %ld\n", __func__, func_id); + return (SBI_ERR_NOT_SUPPORTED); } - hypctx->guest_regs.hyp_a[0] = ret; + if (CPU_EMPTY(&cpus)) + return (SBI_ERR_INVALID_PARAM); - return (0); + riscv_send_ipi(hyp, &cpus); + + return (SBI_SUCCESS); } -int -vmm_sbi_ecall(struct vcpu *vcpu, bool *retu) +bool +vmm_sbi_ecall(struct vcpu *vcpu) { - int sbi_extension_id __unused; + int sbi_extension_id; struct hypctx *hypctx; int error; @@ -220,18 +199,20 @@ vmm_sbi_ecall(struct vcpu *vcpu, bool *retu) switch (sbi_extension_id) { case SBI_EXT_ID_RFNC: error = vmm_sbi_handle_rfnc(vcpu, hypctx); - hypctx->guest_regs.hyp_a[0] = error; break; case SBI_EXT_ID_TIME: - vmm_sbi_handle_time(vcpu, hypctx); + error = vmm_sbi_handle_time(vcpu, hypctx); break; case SBI_EXT_ID_IPI: - vmm_sbi_handle_ipi(vcpu, hypctx); + error = vmm_sbi_handle_ipi(vcpu, hypctx); break; default: - *retu = true; - break; + /* Return to handle in userspace. */ + return (false); } - return (0); + hypctx->guest_regs.hyp_a[0] = error; + + /* Request is handled in kernel mode. */ + return (true); }