From nobody Fri Nov 14 17:01:12 2025 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; dmarc=pass(p=reject dis=none) header.from=sifive.com ARC-Seal: i=1; a=rsa-sha256; t=1761062258; cv=none; d=zohomail.com; s=zohoarc; b=VYbCZm5486A7B5FkjU2l8IrG1sB5T7zIFuz4OrRLfEPUOvw+nx6CJJ4CQryvD/99ESSGYYqUNraFyV7DhGQbAzwHODCrBReBTzfSgsbtig2iy81dBvNT0biQsZQA1n9zz+E3ggwjHwo/zKY2aPhYPdyvtLGUYQ8c0jkdHj6iCE0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1761062258; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=ZVkMXGiRK6D7lGy5hWttf9DjSzSeLTnX/sMNxqBSunI=; b=m2sjpCJW8lNXibB6ygrO5g/wi75drCSAQdN53o/k4sDNU7RdqqwtBWM6nxh/9OnHxSOpIGH0Udi1rZEZTl5AOLVBqt8ztkuZ4a9d8NahFholJibL3RMV+7tV9RZ9zbQvmsQyoP5FahjvchZ+qy3emn5vKVQsozMA050ElumWFmI= 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; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1761062258141207.23038110573043; Tue, 21 Oct 2025 08:57:38 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1vBEib-0000pE-Aa; Tue, 21 Oct 2025 11:56:16 -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 1vBEiX-0000o9-Oe for qemu-devel@nongnu.org; Tue, 21 Oct 2025 11:56:09 -0400 Received: from mail-pl1-x633.google.com ([2607:f8b0:4864:20::633]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1vBEiU-0007jt-MI for qemu-devel@nongnu.org; Tue, 21 Oct 2025 11:56:09 -0400 Received: by mail-pl1-x633.google.com with SMTP id d9443c01a7336-273a0aeed57so139565ad.1 for ; Tue, 21 Oct 2025 08:56:06 -0700 (PDT) Received: from hsinchu26.internal.sifive.com ([210.176.154.34]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-33d5de11392sm11490177a91.7.2025.10.21.08.55.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Oct 2025 08:56:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1761062165; x=1761666965; 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=ZVkMXGiRK6D7lGy5hWttf9DjSzSeLTnX/sMNxqBSunI=; b=CFScIcshrvv556Y6Nu0ak1/nc9gcNTzzi9AafIulDd+A/IDyv1eErdnPzfuYBf5EEz kOUd9FSN6E/fQWHNgmxzV2Syw8OW6JZUxmGbXc8tOWHcWcbhxH5UwUVMbTTW0hSpav/k VyZ9nl8iNExne3pWKC2FMe1ZXBpRqJ1y7plc5v52u7waztMagAoLrHeWfS799wApplWd FG6rn2WoszGf4ReuMJoPu+ge33khKhbSSU028C5x0Qav5XRZbC8hd/p8227b0GRji70b Hdqg9p7ikZtshhpdx2D10F2i+072FzYw07Gw9imvRodd4fjJ37zT2rV65tdO3pf9peYo Yriw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1761062165; x=1761666965; 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=ZVkMXGiRK6D7lGy5hWttf9DjSzSeLTnX/sMNxqBSunI=; b=eg+kpkwkwbQUPT6+z//HffG9+me2ldUaFnntCgAsISdLkB+4S+Bys40Dh4v7uk2QWW goXApAe65rgSPYdCi9e4lH2/WOJnSHppcJXHtnZrLqNrVhmlB2Z3D5Rb0EqPvt0BbCrQ 3l6LphyIzlq+DPjqOFuVRVs6y0W+D0PAjiQ5YXXLSPnex8mkFz15wacrU3xZmZNN3pte HNOE/Jw9sDWPDV+Bn0eeCS81W5wJn/bGqNQvOu1MBnJwWVYUAR7ZH9UL1nwnwvrfy0R7 a7Z8lCjx0laJ+EFWonJYD3L9A6vlcv9u+YAzeDjFnCHQsZEd34VTAFvkp/5GDoyITvCa P8HQ== X-Gm-Message-State: AOJu0YxOCjP5Z5/LNP3fEnIyjzF/wq3guVvK+qWIOedFBAfgfZD+EqF7 B2H0rYkFTyruELUHFpRy1u330wKsmCPyZo+NqLOeb2EP3QKXOplENdo9eBn/fNF/ZAvoKd6FZ5X /aHjpmx8AwQfmqyZ0yias4P1tRKQtIt555ZAkvrN8GZuz/2S1MkimWM0LpwhIsfuyY0qCJrcsum 22AQ6bHKLFqDcjMpMVr+KIw726Mxw6227CfWtuCA== X-Gm-Gg: ASbGncvn6ZvY4w2RNxcKU8V1F3kDx7bVcJO8kSVr33V6HYY/5SXN0vGzsBVJzCHzusc x91kJI1O9iLxBR3Y7+ZIO1hooqwoxtNVf6RDzuIjSkwe9nTCkLb9skRw5UEQCgVg0e9ke3J4P3K ZcKWc6CoVCLW42YxHcjSENelD1COSvWSY3K06mnKfcAm9OZb2bKa24mAWw/+lIfknNd7aQQ3zlf bR7nJzgkro44LpC3kYeBgEhNGR60L1cJxhHCCSHQt+b5maxKhy1CyNjHAx0TTGZ/f4eiYVNJED5 FbpOzIMnGdiFuJL+0ui7i1WfzG14QwK4ZXA5GmjuPG7kHcvYOuQz34Pyfk1ywoVn1GBHHfuBkzb 3pVc4AD80mVoI4IOPC1uHaSYttgrEHz37iHIhW//PCfxBPos88T96UeU1AWUXuNsreExXhXlUnm gEanxxZEsqI7aChKGRb3QH0HW+UWAEnWuZ X-Google-Smtp-Source: AGHT+IGEYAekfWzfzI91SijeLYYBI9MEWoiPZYFokqJmySWO/bQ/8JmTOUXjTlc5Ar+ACZ1mlIrj2g== X-Received: by 2002:a17:902:f789:b0:266:57f7:25f5 with SMTP id d9443c01a7336-292ff8169c9mr3182655ad.7.1761062164201; Tue, 21 Oct 2025 08:56:04 -0700 (PDT) From: Jim Shu To: qemu-devel@nongnu.org, qemu-riscv@nongnu.org Cc: Daniel Henrique Barboza , Richard Henderson , Paolo Bonzini , Palmer Dabbelt , Alistair Francis , Weiwei Li , Liu Zhiwei , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Eduardo Habkost , Marcel Apfelbaum , Yanan Wang , Zhao Liu , Peter Xu , David Hildenbrand , Michael Rolnik , Helge Deller , Song Gao , Laurent Vivier , "Edgar E. Iglesias" , Aurelien Jarno , Jiaxun Yang , Aleksandar Rikalo , Stafford Horne , Nicholas Piggin , Chinmay Rath , Yoshinori Sato , Ilya Leoshkevich , Thomas Huth , Mark Cave-Ayland , Artyom Tarasenko , Bastian Koppelmann , Max Filippov , qemu-ppc@nongnu.org (open list:PowerPC TCG CPUs), qemu-s390x@nongnu.org (open list:S390 TCG CPUs), Jim Shu Subject: [PATCH v3 01/18] accel/tcg: Store section pointer in CPUTLBEntryFull Date: Tue, 21 Oct 2025 23:55:31 +0800 Message-ID: <20251021155548.584543-2-jim.shu@sifive.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20251021155548.584543-1-jim.shu@sifive.com> References: <20251021155548.584543-1-jim.shu@sifive.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: pass client-ip=2607:f8b0:4864:20::633; envelope-from=jim.shu@sifive.com; helo=mail-pl1-x633.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable 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 @sifive.com) X-ZM-MESSAGEID: 1761062261024154100 Content-Type: text/plain; charset="utf-8" To fix the bug of iotlb_to_section(), store complete MemoryRegionSection pointer in CPUTLBEntryFull to replace the section_index in xlat_section. Rename 'xlat_section' to 'xlat' as we remove last 12 bits section_index inside. Also, since we directly use section pointer in the CPUTLBEntryFull (full->section), we can remove the unused functions: iotlb_to_section(), memory_region_section_get_iotlb(). iotlb_to_section() bug description: 'CPUTLBEntryFull.xlat_section' stores section_index in last 12 bits to find the correct section when CPU access the IO region over the IOTLB. However, section_index is only unique inside single AddressSpace. If address space translation is over IOMMUMemoryRegion, it could return section from other AddressSpace. 'iotlb_to_section()' API only finds the sections from CPU's AddressSpace so that it couldn't find section in other AddressSpace. Thus, using 'iotlb_to_section()' API will find the wrong section and QEMU will have wrong load/store access. This bug occurs only when (1) IOMMUMemoryRegion is in the path of CPU access. (2) IOMMUMemoryRegion returns different target_as and the section is in the IO region. Common IOMMU devices don't have this issue since they are only in the path of DMA access. Currently, the bug only occurs when ARM MPC device (hw/misc/tz-mpc.c) returns 'blocked_io_as' to emulate blocked access handling. Upcoming RISC-V wgChecker device is also affected by this bug. Signed-off-by: Jim Shu --- accel/tcg/cputlb.c | 32 +++++++++++++++----------------- include/accel/tcg/iommu.h | 15 --------------- include/exec/cputlb.h | 2 +- include/hw/core/cpu.h | 12 +++++++----- system/physmem.c | 25 ------------------------- 5 files changed, 23 insertions(+), 63 deletions(-) diff --git a/accel/tcg/cputlb.c b/accel/tcg/cputlb.c index 631f1fe135..b3ed5d2b9c 100644 --- a/accel/tcg/cputlb.c +++ b/accel/tcg/cputlb.c @@ -1089,7 +1089,7 @@ void tlb_set_page_full(CPUState *cpu, int mmu_idx, } } else { /* I/O or ROMD */ - iotlb =3D memory_region_section_get_iotlb(cpu, section) + xlat; + iotlb =3D xlat; /* * Writes to romd devices must go through MMIO to enable write. * Reads to romd devices go through the ram_ptr found above, @@ -1140,10 +1140,9 @@ void tlb_set_page_full(CPUState *cpu, int mmu_idx, /* * When memory region is ram, iotlb contains a TARGET_PAGE_BITS * aligned ram_addr_t of the page base of the target RAM. - * Otherwise, iotlb contains - * - a physical section number in the lower TARGET_PAGE_BITS - * - the offset within section->mr of the page base (I/O, ROMD) with = the - * TARGET_PAGE_BITS masked off. + * Otherwise, iotlb contains a TARGET_PAGE_BITS aligned + * offset within section->mr of the page base (I/O, ROMD) + * * We subtract addr_page (which is page aligned and thus won't * disturb the low bits) to give an offset which can be added to the * (non-page-aligned) vaddr of the eventual memory access to get @@ -1153,7 +1152,8 @@ void tlb_set_page_full(CPUState *cpu, int mmu_idx, */ desc->fulltlb[index] =3D *full; full =3D &desc->fulltlb[index]; - full->xlat_section =3D iotlb - addr_page; + full->xlat =3D iotlb - addr_page; + full->section =3D section; full->phys_addr =3D paddr_page; =20 /* Now calculate the new entry */ @@ -1269,14 +1269,14 @@ static inline void cpu_unaligned_access(CPUState *c= pu, vaddr addr, } =20 static MemoryRegionSection * -io_prepare(hwaddr *out_offset, CPUState *cpu, hwaddr xlat, +io_prepare(hwaddr *out_offset, CPUState *cpu, CPUTLBEntryFull *full, MemTxAttrs attrs, vaddr addr, uintptr_t retaddr) { MemoryRegionSection *section; hwaddr mr_offset; =20 - section =3D iotlb_to_section(cpu, xlat, attrs); - mr_offset =3D (xlat & TARGET_PAGE_MASK) + addr; + section =3D full->section; + mr_offset =3D full->xlat + addr; cpu->mem_io_pc =3D retaddr; if (!cpu->neg.can_do_io) { cpu_io_recompile(cpu, retaddr); @@ -1335,7 +1335,7 @@ static bool victim_tlb_hit(CPUState *cpu, size_t mmu_= idx, size_t index, static void notdirty_write(CPUState *cpu, vaddr mem_vaddr, unsigned size, CPUTLBEntryFull *full, uintptr_t retaddr) { - ram_addr_t ram_addr =3D mem_vaddr + full->xlat_section; + ram_addr_t ram_addr =3D mem_vaddr + full->xlat; =20 trace_memory_notdirty_write_access(mem_vaddr, ram_addr, size); =20 @@ -1592,9 +1592,7 @@ bool tlb_plugin_lookup(CPUState *cpu, vaddr addr, int= mmu_idx, =20 /* We must have an iotlb entry for MMIO */ if (tlb_addr & TLB_MMIO) { - MemoryRegionSection *section =3D - iotlb_to_section(cpu, full->xlat_section & ~TARGET_PAGE_MASK, - full->attrs); + MemoryRegionSection *section =3D full->section; data->is_io =3D true; data->mr =3D section->mr; } else { @@ -1991,7 +1989,7 @@ static uint64_t do_ld_mmio_beN(CPUState *cpu, CPUTLBE= ntryFull *full, tcg_debug_assert(size > 0 && size <=3D 8); =20 attrs =3D full->attrs; - section =3D io_prepare(&mr_offset, cpu, full->xlat_section, attrs, add= r, ra); + section =3D io_prepare(&mr_offset, cpu, full, attrs, addr, ra); mr =3D section->mr; =20 BQL_LOCK_GUARD(); @@ -2012,7 +2010,7 @@ static Int128 do_ld16_mmio_beN(CPUState *cpu, CPUTLBE= ntryFull *full, tcg_debug_assert(size > 8 && size <=3D 16); =20 attrs =3D full->attrs; - section =3D io_prepare(&mr_offset, cpu, full->xlat_section, attrs, add= r, ra); + section =3D io_prepare(&mr_offset, cpu, full, attrs, addr, ra); mr =3D section->mr; =20 BQL_LOCK_GUARD(); @@ -2532,7 +2530,7 @@ static uint64_t do_st_mmio_leN(CPUState *cpu, CPUTLBE= ntryFull *full, tcg_debug_assert(size > 0 && size <=3D 8); =20 attrs =3D full->attrs; - section =3D io_prepare(&mr_offset, cpu, full->xlat_section, attrs, add= r, ra); + section =3D io_prepare(&mr_offset, cpu, full, attrs, addr, ra); mr =3D section->mr; =20 BQL_LOCK_GUARD(); @@ -2552,7 +2550,7 @@ static uint64_t do_st16_mmio_leN(CPUState *cpu, CPUTL= BEntryFull *full, tcg_debug_assert(size > 8 && size <=3D 16); =20 attrs =3D full->attrs; - section =3D io_prepare(&mr_offset, cpu, full->xlat_section, attrs, add= r, ra); + section =3D io_prepare(&mr_offset, cpu, full, attrs, addr, ra); mr =3D section->mr; =20 BQL_LOCK_GUARD(); diff --git a/include/accel/tcg/iommu.h b/include/accel/tcg/iommu.h index 90cfd6c0ed..547f8ea0ef 100644 --- a/include/accel/tcg/iommu.h +++ b/include/accel/tcg/iommu.h @@ -14,18 +14,6 @@ #include "exec/hwaddr.h" #include "exec/memattrs.h" =20 -/** - * iotlb_to_section: - * @cpu: CPU performing the access - * @index: TCG CPU IOTLB entry - * - * Given a TCG CPU IOTLB entry, return the MemoryRegionSection that - * it refers to. @index will have been initially created and returned - * by memory_region_section_get_iotlb(). - */ -MemoryRegionSection *iotlb_to_section(CPUState *cpu, - hwaddr index, MemTxAttrs attrs); - MemoryRegionSection *address_space_translate_for_iotlb(CPUState *cpu, int asidx, hwaddr addr, @@ -34,8 +22,5 @@ MemoryRegionSection *address_space_translate_for_iotlb(CP= UState *cpu, MemTxAttrs attrs, int *prot); =20 -hwaddr memory_region_section_get_iotlb(CPUState *cpu, - MemoryRegionSection *section); - #endif =20 diff --git a/include/exec/cputlb.h b/include/exec/cputlb.h index 9bec0e7890..c0ca7ad77b 100644 --- a/include/exec/cputlb.h +++ b/include/exec/cputlb.h @@ -43,7 +43,7 @@ void tlb_reset_dirty_range_all(ram_addr_t start, ram_addr= _t length); * @full: the details of the tlb entry * * Add an entry to @cpu tlb index @mmu_idx. All of the fields of - * @full must be filled, except for xlat_section, and constitute + * @full must be filled, except for xlat, and constitute * the complete description of the translated page. * * This is generally called by the target tlb_fill function after diff --git a/include/hw/core/cpu.h b/include/hw/core/cpu.h index e79e8e0a8e..cce3f16198 100644 --- a/include/hw/core/cpu.h +++ b/include/hw/core/cpu.h @@ -214,15 +214,17 @@ typedef uint32_t MMUIdxMap; */ struct CPUTLBEntryFull { /* - * @xlat_section contains: - * - in the lower TARGET_PAGE_BITS, a physical section number - * - with the lower TARGET_PAGE_BITS masked off, an offset which - * must be added to the virtual address to obtain: + * @xlat contains: + * - a TARGET_PAGE_BITS aligned offset which must be added to + * the virtual address to obtain: * + the ram_addr_t of the target RAM (if the physical section * number is PHYS_SECTION_NOTDIRTY or PHYS_SECTION_ROM) * + the offset within the target MemoryRegion (otherwise) */ - hwaddr xlat_section; + hwaddr xlat; + + /* @section contains physical section. */ + MemoryRegionSection *section; =20 /* * @phys_addr contains the physical address in the address space diff --git a/system/physmem.c b/system/physmem.c index a340ca3e61..c163d6b856 100644 --- a/system/physmem.c +++ b/system/physmem.c @@ -748,31 +748,6 @@ translate_fail: return &d->map.sections[PHYS_SECTION_UNASSIGNED]; } =20 -MemoryRegionSection *iotlb_to_section(CPUState *cpu, - hwaddr index, MemTxAttrs attrs) -{ - int asidx =3D cpu_asidx_from_attrs(cpu, attrs); - CPUAddressSpace *cpuas =3D &cpu->cpu_ases[asidx]; - AddressSpaceDispatch *d =3D address_space_to_dispatch(cpuas->as); - int section_index =3D index & ~TARGET_PAGE_MASK; - MemoryRegionSection *ret; - - assert(section_index < d->map.sections_nb); - ret =3D d->map.sections + section_index; - assert(ret->mr); - assert(ret->mr->ops); - - return ret; -} - -/* Called from RCU critical section */ -hwaddr memory_region_section_get_iotlb(CPUState *cpu, - MemoryRegionSection *section) -{ - AddressSpaceDispatch *d =3D flatview_to_dispatch(section->fv); - return section - d->map.sections; -} - #endif /* CONFIG_TCG */ =20 void cpu_address_space_init(CPUState *cpu, int asidx, --=20 2.43.0 From nobody Fri Nov 14 17:01:12 2025 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; dmarc=pass(p=reject dis=none) header.from=sifive.com ARC-Seal: i=1; a=rsa-sha256; t=1761062435; cv=none; d=zohomail.com; s=zohoarc; b=X0uFPgXEnmWjMKVO53XHNfTIlyNYmK5Z/YyJbVPevIukBzM8Bxo3oRIwQGY65SWdNGp0t95/ZMBpclrzH50vPgwGG/A/gU4bmZKu6c0wHtxQO1tDt+Da8mVqTVkl4IXBIXsPBnQTawoC15hT7NHWC2Ws3vCZwFq6NZCtxA/NLaU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1761062435; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=ItEDascWURdtiJBIFOxPqFxdsposkPVgAOSmqvZV7F4=; b=KdJiKfGMMUd7wjGWJraK3csaTprJoLVspTsAh84woB2PWrKM3Yyl8KG+ezYEGKlY+HVCabodaPhWNZRKUmiK2yFcaVi3dXnTxGpOMVBjpqBwn9W+D4rCCsGM4P3L1TjcZiV3J5AZYMOQa0aWUnEKA0dbO4wxRfWvDWxrpYAIkJ0= 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; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 17610624356981002.3382932111931; Tue, 21 Oct 2025 09:00:35 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1vBEin-0000vR-Ao; Tue, 21 Oct 2025 11:56:25 -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 1vBEid-0000pm-QA for qemu-devel@nongnu.org; Tue, 21 Oct 2025 11:56:17 -0400 Received: from mail-pf1-x435.google.com ([2607:f8b0:4864:20::435]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1vBEia-0007l7-9W for qemu-devel@nongnu.org; Tue, 21 Oct 2025 11:56:15 -0400 Received: by mail-pf1-x435.google.com with SMTP id d2e1a72fcca58-77f67ba775aso7486383b3a.3 for ; Tue, 21 Oct 2025 08:56:11 -0700 (PDT) Received: from hsinchu26.internal.sifive.com ([210.176.154.34]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-33d5de11392sm11490177a91.7.2025.10.21.08.56.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Oct 2025 08:56:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1761062170; x=1761666970; 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=ItEDascWURdtiJBIFOxPqFxdsposkPVgAOSmqvZV7F4=; b=VZGkaLV7jqRVLYo7QXIMCTZ34wg0JdWFhmfdkcVggYH6gDbZXau1MDyIXdBNo3W9/S lf/vKTnunIWw5jkVsqgmaWGhJutaEEc6p9PGrpoCqZIItNKMklYbXVtGiAwoOYv12M4i N52tZLNAvg8Xby8zj8EUARr5Uf3WaYmCwJ+7lCp/b3+EGtn+syQCjo0KX/SDbZyJ2Vkh hc3X8ZMOq2/EzAVXF9oYDoc7Nlfs0PrVSaN9YCRa7AZn9YI6+Z7c9HeV4I9E92cfcyo+ V1qpB/SESY9G10x7Z8VqCgVPNCIXn5rd6ALWaGzBHzFIw85vxqfpjvM/tN2nBF+iWhip WaTQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1761062170; x=1761666970; 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=ItEDascWURdtiJBIFOxPqFxdsposkPVgAOSmqvZV7F4=; b=JyIBt9NFIcgaxYZ02Qg+xa2bwtLoNuBFwPAZeic9sdybk97yxyrShsPACEAV4LSGKB bz/tOFHmRw2lKOO1tYDJF5yWJ7ZWtggUTxpF0pR/7wGiRm+/Ei/R+IF1WP18AiO1p1Jm 8mZZwnzPxrNQxPwGazu6IDTHDTjB2zjpiy4dpUkBenmJSq0Ir1j5UtbqyIfEimiD4w3U P9IptpjNvNxmDtICM9lDO2Bf95lGW2pW+FwhgTFFV+FrK2xceXG8sPxstW7XBqjRecbg A2E07pBFzdxVbabEfDVDI0PEeSQxmQ2hKglVJavVvBt/1pz2QeAT1a+9nzd1DLsEo1gK B9lQ== X-Gm-Message-State: AOJu0YzxUwNj/zkNs9sehS8dINZLN+Gs8Id2frscQcKNnD5wVO3GnQLP 48w2NmFte087rMPr34lcZ68qW2/0UshjifAeEXlijKA/0BVH/QA8sdlC4YMqVyyrtAYAGD19qt/ okeCHldVZgxcIM8kd83JdAW9H3AoRaTefS9fukm/Gy4gB0SuIkRC5+O+tfvcaDY5wlXqiD72xam 96WwjSNC/UZMiPQz0FphgRpjs5ZLR1sCdlfX+yhg== X-Gm-Gg: ASbGncsFo6QlBcLIjzMUweLp8QkKyNSiaIT54PU2bZ2elXx4w63oq2hot8eGedIsGLl mn7qOpYrKLww9yNTBIllV7tkWrYjtggSDktq/nmtknGbdaK2YbqTrrvSL7TMg/hFE0P3/Wnj+BI v/V1G3l3XYCh0kSJYwa4Cvt4kFBcO+QDJXb/0rkp7HSAOkkT0Ab9dMSQPHrPnDpNLcz59hElQa0 8P6dxcrzBIdQjpYOEjeQmp/H9fx5S3+kK2tr0lFmtCuqUFfIPvKwlbXYT2I9odDROpohadZpjlH k+UsGf61KIZ5zwif4/Ja4uT2vCxoZNhu0Ou0UP29KvJ9+aX3Ome9jetQXHqBVoiKdlWM5IIHeIC lFzDC84KtGQtGwrdrqJFBFB7ZaxdZhadlnWG1tz96pZfH2PUu+Mp0mnE6BdHzzsSzlTJLlCu/pk JKX6Qpe7yu7WFA9XSOHUJTjOCx/VsfA2k6 X-Google-Smtp-Source: AGHT+IEn0Qt6Oo+4e0ngsjIqW8GIuRuTZTtXWnxt3lGQr7smV5xgsnGddwfD17jHYxwNbaGUbIg5Ag== X-Received: by 2002:a05:6a20:158a:b0:2e5:655c:7f93 with SMTP id adf61e73a8af0-334a8616c19mr24469072637.33.1761062169916; Tue, 21 Oct 2025 08:56:09 -0700 (PDT) From: Jim Shu To: qemu-devel@nongnu.org, qemu-riscv@nongnu.org Cc: Daniel Henrique Barboza , Richard Henderson , Paolo Bonzini , Palmer Dabbelt , Alistair Francis , Weiwei Li , Liu Zhiwei , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Eduardo Habkost , Marcel Apfelbaum , Yanan Wang , Zhao Liu , Peter Xu , David Hildenbrand , Michael Rolnik , Helge Deller , Song Gao , Laurent Vivier , "Edgar E. Iglesias" , Aurelien Jarno , Jiaxun Yang , Aleksandar Rikalo , Stafford Horne , Nicholas Piggin , Chinmay Rath , Yoshinori Sato , Ilya Leoshkevich , Thomas Huth , Mark Cave-Ayland , Artyom Tarasenko , Bastian Koppelmann , Max Filippov , qemu-ppc@nongnu.org (open list:PowerPC TCG CPUs), qemu-s390x@nongnu.org (open list:S390 TCG CPUs), Jim Shu Subject: [PATCH v3 02/18] system/physmem: Remove the assertion of page-aligned section number Date: Tue, 21 Oct 2025 23:55:32 +0800 Message-ID: <20251021155548.584543-3-jim.shu@sifive.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20251021155548.584543-1-jim.shu@sifive.com> References: <20251021155548.584543-1-jim.shu@sifive.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: pass client-ip=2607:f8b0:4864:20::435; envelope-from=jim.shu@sifive.com; helo=mail-pf1-x435.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable 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 @sifive.com) X-ZM-MESSAGEID: 1761062438015154100 Content-Type: text/plain; charset="utf-8" The physical section number is no longer ORed into the IOTLB entries together with a page-aligned pointer, so it no longer needs to be page-aligned. Signed-off-by: Jim Shu --- system/physmem.c | 6 ------ 1 file changed, 6 deletions(-) diff --git a/system/physmem.c b/system/physmem.c index c163d6b856..23d9b92954 100644 --- a/system/physmem.c +++ b/system/physmem.c @@ -1305,12 +1305,6 @@ static subpage_t *subpage_init(FlatView *fv, hwaddr = base); static uint16_t phys_section_add(PhysPageMap *map, MemoryRegionSection *section) { - /* The physical section number is ORed with a page-aligned - * pointer to produce the iotlb entries. Thus it should - * never overflow into the page-aligned value. - */ - assert(map->sections_nb < TARGET_PAGE_SIZE); - if (map->sections_nb =3D=3D map->sections_nb_alloc) { map->sections_nb_alloc =3D MAX(map->sections_nb_alloc * 2, 16); map->sections =3D g_renew(MemoryRegionSection, map->sections, --=20 2.43.0 From nobody Fri Nov 14 17:01:12 2025 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; dmarc=pass(p=reject dis=none) header.from=sifive.com ARC-Seal: i=1; a=rsa-sha256; t=1761062282; cv=none; d=zohomail.com; s=zohoarc; b=K9osBa2RWEXYS7viZn94ZzC33RXadK/ZVNcd8HA6BGtk8HxC+mw4S+2/6vqlhZ+olcq1peYmTEL2/8+X4nRny2CK+kzX5uIFkaPNH4Tu29f1FQO45aBv7wU0qKX9FnDcE+AOfSqkpWy0GG6qqafbu/soLJFbI5Ngm5paX9gC4rk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1761062282; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=p2IL9rBkWrTjdCHMqqTwPGOmEmX+DIgC22nY+xhrzm0=; b=ALTks+txk8JNEoHqKJXFNaWeh/iqwjEnH7G8QccMk8pLbYm43gEowBcmc8a8YsBhEmBUoFmVsJcFqhoncA2XEwBLCW3flmY0zmNAZ/f+hP493001P0ik2N5ib3amJuFElpSLV47sooRGcb8qk4ep2rm+eKXwvtWJrfakwEJdJD8= 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; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1761062282868643.7519520813446; Tue, 21 Oct 2025 08:58:02 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1vBEiv-000107-4e; Tue, 21 Oct 2025 11:56:33 -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 1vBEio-0000xC-Dj for qemu-devel@nongnu.org; Tue, 21 Oct 2025 11:56:27 -0400 Received: from mail-pf1-x42c.google.com ([2607:f8b0:4864:20::42c]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1vBEig-0007m6-SS for qemu-devel@nongnu.org; Tue, 21 Oct 2025 11:56:26 -0400 Received: by mail-pf1-x42c.google.com with SMTP id d2e1a72fcca58-7841da939deso5436503b3a.2 for ; Tue, 21 Oct 2025 08:56:17 -0700 (PDT) Received: from hsinchu26.internal.sifive.com ([210.176.154.34]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-33d5de11392sm11490177a91.7.2025.10.21.08.56.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Oct 2025 08:56:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1761062176; x=1761666976; 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=p2IL9rBkWrTjdCHMqqTwPGOmEmX+DIgC22nY+xhrzm0=; b=goABQsk6GIHV3EWALGGfzAHGjmioarlrJ4qGligtUSLycVMQU3Kx/6UdphkTmov18r 9mIKh0n5QXwS3rCBWn3xWoKVR9orhquHbbnjJCVecA/1QHy406DgyUKlrxkhWuXCMK/f bGsQZlvtzrV63dR9rqDgj4mqPwSP0D88xS/LNzTHgtOtVkP/HZAg3DquM0yBJ4SMkyWv nPYsqu4SA7LQ+vUG4omi+feDUMa+zKZiuBCoiSF8zurGww3BgaTHj3UJ0BmPLgCNAI55 q/BVtVoLXpgWikVH1EoBEYFBukGmEGzE7v7UPDzCKl54DHKYZ8b13nA42crbq8VULvoU cdrA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1761062176; x=1761666976; 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=p2IL9rBkWrTjdCHMqqTwPGOmEmX+DIgC22nY+xhrzm0=; b=oPj4w1iMf9luCNpoFC1O5UfBT0yxWlmHwYmy8W2/b3sV1lGcsyvTz3RDmlNzUzftY4 aoA/P4kHVhHy9LEF+IVEtnqk9ENZCGCYZA+VP/ffxADKNuPqB56ZaxaB5qLWu/RUuLy+ z1RVP9x+wQvxJDICRQYJqOdfGr0PnhEb1rW6tYzpFjHMW5yGihAHkdXGwU6D+EKeIbqx 4M4AZQ5nBS89BqYt1uiFwNEBAmH3pUFp9RNk8DAW4D1wvYXm0mCecVfRwvnBo5P9UWQG 8snO3jWKyADvk7jFmbkWPB1+4CV2If2EHiQ+TkjDsYQL9bPtmG76DuHeqtxbBXRIcbbw hOlg== X-Gm-Message-State: AOJu0Yxw5EYtSSaq2buEKK+xfhO7nF+vDVByXqCNvIPVZkx4e6dI9fxF jd1WgmTHph77OFtCdINRawaRVwLStQU+KScV3QvycZsYyseirwx02PYdbM9SYtUioNRDKvuFk0U uBjcWDiDEVZ4Ulc9RqOgosKXZnm6F2tEchBNFw+rOhpFjvrGngpkREDsl5mLUXndquD8WGjRlUx evf6hM4NtoUGRgLiunqXZF8fT0z+09y+aS8wbdLg== X-Gm-Gg: ASbGncuk5zXsy7PYLGRftMXaS0JT/F8PsUXNWlcjfPpMJpmaGPl+NjBtUuzL3ubDKAT SN5wTYgowWQBhF8j8pdElx6Ilu5sUqer6ct37+8TybjxYz7eVAnIwJZ1kU101FkPKWD8675xZUr rH9CsOjOpjMrRxLNP7hNtnEWb6KkNo4UoWMw+Rp6zl2b3Yx7bVpJ5Ia6E/WiPe/49m67TBbZGAC MDmAbNj7KClENRE+q8hu3lH13qxi+QQU48odM4bi+Xe59RJM0LN/EJMxWEBBSGxHT6HYTWi+7nS rCTd/klUYhXhjW39C3DEhnopE2IgAKSejDQcWvnXVEWA0njP6ZjSpJ+pJ7jVlxQe0nt9lp0MPe9 Gk1+PqZjDKLSw9r2ybqHMeU/jWSpvp+k3t2CHh+Wqh0E+iNx+WF5qyemwulmIJtmdWTCBFYZRW2 9qfT3nREGRQK8Co/sRz1dKBg== X-Google-Smtp-Source: AGHT+IEOHRAHi/kThU8XcZm++uy21KLqveTvhsLOCWDtxbRW0wTlgzGaCXCfsmK8fZg7ypHpHFvltQ== X-Received: by 2002:a05:6a20:42a3:b0:334:8f45:8e99 with SMTP id adf61e73a8af0-334a85fd3b4mr22173301637.35.1761062175552; Tue, 21 Oct 2025 08:56:15 -0700 (PDT) From: Jim Shu To: qemu-devel@nongnu.org, qemu-riscv@nongnu.org Cc: Daniel Henrique Barboza , Richard Henderson , Paolo Bonzini , Palmer Dabbelt , Alistair Francis , Weiwei Li , Liu Zhiwei , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Eduardo Habkost , Marcel Apfelbaum , Yanan Wang , Zhao Liu , Peter Xu , David Hildenbrand , Michael Rolnik , Helge Deller , Song Gao , Laurent Vivier , "Edgar E. Iglesias" , Aurelien Jarno , Jiaxun Yang , Aleksandar Rikalo , Stafford Horne , Nicholas Piggin , Chinmay Rath , Yoshinori Sato , Ilya Leoshkevich , Thomas Huth , Mark Cave-Ayland , Artyom Tarasenko , Bastian Koppelmann , Max Filippov , qemu-ppc@nongnu.org (open list:PowerPC TCG CPUs), qemu-s390x@nongnu.org (open list:S390 TCG CPUs), Jim Shu Subject: [PATCH v3 03/18] accel/tcg: memory access from CPU will pass access_type to IOMMU Date: Tue, 21 Oct 2025 23:55:33 +0800 Message-ID: <20251021155548.584543-4-jim.shu@sifive.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20251021155548.584543-1-jim.shu@sifive.com> References: <20251021155548.584543-1-jim.shu@sifive.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: pass client-ip=2607:f8b0:4864:20::42c; envelope-from=jim.shu@sifive.com; helo=mail-pf1-x42c.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable 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 @sifive.com) X-ZM-MESSAGEID: 1761062286820154100 Content-Type: text/plain; charset="utf-8" It is the preparation patch for upcoming RISC-V wgChecker device. Since RISC-V wgChecker could permit access in RO/WO permission, the IOMMUMemoryRegion could return different section for read & write access. The memory access from CPU should also pass the access_type to IOMMU translate function so that IOMMU could return the correct section of specified access_type. Signed-off-by: Jim Shu --- accel/tcg/cputlb.c | 17 ++++++++++------- include/accel/tcg/iommu.h | 3 ++- include/exec/cputlb.h | 11 +++++++---- system/physmem.c | 16 +++++++++++----- target/alpha/helper.c | 2 +- target/avr/helper.c | 2 +- target/hppa/mem_helper.c | 1 - target/i386/tcg/system/excp_helper.c | 3 ++- target/loongarch/tcg/tlb_helper.c | 2 +- target/m68k/helper.c | 10 +++++++--- target/microblaze/helper.c | 8 ++++---- target/mips/tcg/system/tlb_helper.c | 4 ++-- target/openrisc/mmu.c | 2 +- target/ppc/mmu_helper.c | 2 +- target/riscv/cpu_helper.c | 2 +- target/rx/cpu.c | 3 ++- target/s390x/tcg/excp_helper.c | 2 +- target/sh4/helper.c | 2 +- target/sparc/mmu_helper.c | 6 +++--- target/tricore/helper.c | 2 +- target/xtensa/helper.c | 3 ++- 21 files changed, 61 insertions(+), 42 deletions(-) diff --git a/accel/tcg/cputlb.c b/accel/tcg/cputlb.c index b3ed5d2b9c..a906241b1b 100644 --- a/accel/tcg/cputlb.c +++ b/accel/tcg/cputlb.c @@ -1021,7 +1021,8 @@ static inline void tlb_set_compare(CPUTLBEntryFull *f= ull, CPUTLBEntry *ent, * critical section. */ void tlb_set_page_full(CPUState *cpu, int mmu_idx, - vaddr addr, CPUTLBEntryFull *full) + vaddr addr, MMUAccessType access_type, + CPUTLBEntryFull *full) { CPUTLB *tlb =3D &cpu->neg.tlb; CPUTLBDesc *desc =3D &tlb->d[mmu_idx]; @@ -1048,7 +1049,8 @@ void tlb_set_page_full(CPUState *cpu, int mmu_idx, prot =3D full->prot; asidx =3D cpu_asidx_from_attrs(cpu, full->attrs); section =3D address_space_translate_for_iotlb(cpu, asidx, paddr_page, - &xlat, &sz, full->attrs, &= prot); + &xlat, &sz, full->attrs, &= prot, + access_type); assert(sz >=3D TARGET_PAGE_SIZE); =20 tlb_debug("vaddr=3D%016" VADDR_PRIx " paddr=3D0x" HWADDR_FMT_plx @@ -1184,7 +1186,8 @@ void tlb_set_page_full(CPUState *cpu, int mmu_idx, =20 void tlb_set_page_with_attrs(CPUState *cpu, vaddr addr, hwaddr paddr, MemTxAttrs attrs, int prot, - int mmu_idx, vaddr size) + MMUAccessType access_type, int mmu_idx, + vaddr size) { CPUTLBEntryFull full =3D { .phys_addr =3D paddr, @@ -1194,15 +1197,15 @@ void tlb_set_page_with_attrs(CPUState *cpu, vaddr a= ddr, }; =20 assert(is_power_of_2(size)); - tlb_set_page_full(cpu, mmu_idx, addr, &full); + tlb_set_page_full(cpu, mmu_idx, addr, access_type, &full); } =20 void tlb_set_page(CPUState *cpu, vaddr addr, - hwaddr paddr, int prot, + hwaddr paddr, int prot, MMUAccessType access_type, int mmu_idx, vaddr size) { tlb_set_page_with_attrs(cpu, addr, paddr, MEMTXATTRS_UNSPECIFIED, - prot, mmu_idx, size); + prot, access_type, mmu_idx, size); } =20 /** @@ -1244,7 +1247,7 @@ static bool tlb_fill_align(CPUState *cpu, vaddr addr,= MMUAccessType type, if (ops->tlb_fill_align) { if (ops->tlb_fill_align(cpu, &full, addr, type, mmu_idx, memop, size, probe, ra)) { - tlb_set_page_full(cpu, mmu_idx, addr, &full); + tlb_set_page_full(cpu, mmu_idx, addr, type, &full); return true; } } else { diff --git a/include/accel/tcg/iommu.h b/include/accel/tcg/iommu.h index 547f8ea0ef..2a79f85983 100644 --- a/include/accel/tcg/iommu.h +++ b/include/accel/tcg/iommu.h @@ -20,7 +20,8 @@ MemoryRegionSection *address_space_translate_for_iotlb(CP= UState *cpu, hwaddr *xlat, hwaddr *plen, MemTxAttrs attrs, - int *prot); + int *prot, + MMUAccessType acces= s_type); =20 #endif =20 diff --git a/include/exec/cputlb.h b/include/exec/cputlb.h index c0ca7ad77b..b5230e70b9 100644 --- a/include/exec/cputlb.h +++ b/include/exec/cputlb.h @@ -40,6 +40,7 @@ void tlb_reset_dirty_range_all(ram_addr_t start, ram_addr= _t length); * @cpu: CPU context * @mmu_idx: mmu index of the tlb to modify * @addr: virtual address of the entry to add + * @access_type: access was read/write/execute * @full: the details of the tlb entry * * Add an entry to @cpu tlb index @mmu_idx. All of the fields of @@ -55,6 +56,7 @@ void tlb_reset_dirty_range_all(ram_addr_t start, ram_addr= _t length); * used by tlb_flush_page. */ void tlb_set_page_full(CPUState *cpu, int mmu_idx, vaddr addr, + MMUAccessType access_type, CPUTLBEntryFull *full); =20 /** @@ -64,6 +66,7 @@ void tlb_set_page_full(CPUState *cpu, int mmu_idx, vaddr = addr, * @paddr: physical address of the page * @attrs: memory transaction attributes * @prot: access permissions (PAGE_READ/PAGE_WRITE/PAGE_EXEC bits) + * @access_type: access was read/write/execute * @mmu_idx: MMU index to insert TLB entry for * @size: size of the page in bytes * @@ -80,9 +83,9 @@ void tlb_set_page_full(CPUState *cpu, int mmu_idx, vaddr = addr, * used by tlb_flush_page. */ void tlb_set_page_with_attrs(CPUState *cpu, vaddr addr, - hwaddr paddr, MemTxAttrs attrs, - int prot, int mmu_idx, vaddr size); - + hwaddr paddr, MemTxAttrs attrs, int prot, + MMUAccessType access_type, int mmu_idx, + vaddr size); /** * tlb_set_page: * @@ -91,7 +94,7 @@ void tlb_set_page_with_attrs(CPUState *cpu, vaddr addr, * as a convenience for CPUs which don't use memory transaction attributes. */ void tlb_set_page(CPUState *cpu, vaddr addr, - hwaddr paddr, int prot, + hwaddr paddr, int prot, MMUAccessType access_type, int mmu_idx, vaddr size); =20 #if defined(CONFIG_TCG) && !defined(CONFIG_USER_ONLY) diff --git a/system/physmem.c b/system/physmem.c index 23d9b92954..f57e1a97d5 100644 --- a/system/physmem.c +++ b/system/physmem.c @@ -684,12 +684,14 @@ void tcg_iommu_init_notifier_list(CPUState *cpu) MemoryRegionSection * address_space_translate_for_iotlb(CPUState *cpu, int asidx, hwaddr orig_ad= dr, hwaddr *xlat, hwaddr *plen, - MemTxAttrs attrs, int *prot) + MemTxAttrs attrs, int *prot, + MMUAccessType access_type) { MemoryRegionSection *section; IOMMUMemoryRegion *iommu_mr; IOMMUMemoryRegionClass *imrc; IOMMUTLBEntry iotlb; + IOMMUAccessFlags iommu_flags; int iommu_idx; hwaddr addr =3D orig_addr; AddressSpaceDispatch *d =3D address_space_to_dispatch(cpu->cpu_ases[as= idx].as); @@ -706,10 +708,14 @@ address_space_translate_for_iotlb(CPUState *cpu, int = asidx, hwaddr orig_addr, =20 iommu_idx =3D imrc->attrs_to_index(iommu_mr, attrs); tcg_register_iommu_notifier(cpu, iommu_mr, iommu_idx); - /* We need all the permissions, so pass IOMMU_NONE so the IOMMU - * doesn't short-cut its translation table walk. - */ - iotlb =3D imrc->translate(iommu_mr, addr, IOMMU_NONE, iommu_idx); + + if (access_type =3D=3D MMU_DATA_STORE) { + iommu_flags =3D IOMMU_WO; + } else { + iommu_flags =3D IOMMU_RO; + } + + iotlb =3D imrc->translate(iommu_mr, addr, iommu_flags, iommu_idx); addr =3D ((iotlb.translated_addr & ~iotlb.addr_mask) | (addr & iotlb.addr_mask)); /* Update the caller's prot bits to remove permissions the IOMMU diff --git a/target/alpha/helper.c b/target/alpha/helper.c index 096eac3445..dd5cba740b 100644 --- a/target/alpha/helper.c +++ b/target/alpha/helper.c @@ -320,7 +320,7 @@ bool alpha_cpu_tlb_fill(CPUState *cs, vaddr addr, int s= ize, } =20 tlb_set_page(cs, addr & TARGET_PAGE_MASK, phys & TARGET_PAGE_MASK, - prot, mmu_idx, TARGET_PAGE_SIZE); + prot, access_type, mmu_idx, TARGET_PAGE_SIZE); return true; } =20 diff --git a/target/avr/helper.c b/target/avr/helper.c index 4b29ab3526..fd147e00e3 100644 --- a/target/avr/helper.c +++ b/target/avr/helper.c @@ -140,7 +140,7 @@ bool avr_cpu_tlb_fill(CPUState *cs, vaddr address, int = size, prot =3D PAGE_READ | PAGE_WRITE; } =20 - tlb_set_page(cs, address, paddr, prot, mmu_idx, TARGET_PAGE_SIZE); + tlb_set_page(cs, address, paddr, prot, access_type, mmu_idx, TARGET_PA= GE_SIZE); return true; } =20 diff --git a/target/hppa/mem_helper.c b/target/hppa/mem_helper.c index cce82e6599..710259b3cd 100644 --- a/target/hppa/mem_helper.c +++ b/target/hppa/mem_helper.c @@ -475,7 +475,6 @@ bool hppa_cpu_tlb_fill_align(CPUState *cs, CPUTLBEntryF= ull *out, vaddr addr, out->prot =3D prot; out->attrs =3D MEMTXATTRS_UNSPECIFIED; out->lg_page_size =3D TARGET_PAGE_BITS; - return true; } =20 diff --git a/target/i386/tcg/system/excp_helper.c b/target/i386/tcg/system/= excp_helper.c index f622b5d588..ac24c1ca98 100644 --- a/target/i386/tcg/system/excp_helper.c +++ b/target/i386/tcg/system/excp_helper.c @@ -628,7 +628,8 @@ bool x86_cpu_tlb_fill(CPUState *cs, vaddr addr, int siz= e, tlb_set_page_with_attrs(cs, addr & TARGET_PAGE_MASK, out.paddr & TARGET_PAGE_MASK, cpu_get_mem_attrs(env), - out.prot, mmu_idx, out.page_size); + out.prot, access_type, mmu_idx, + out.page_size); return true; } =20 diff --git a/target/loongarch/tcg/tlb_helper.c b/target/loongarch/tcg/tlb_h= elper.c index e119f78d92..09d1f6f60e 100644 --- a/target/loongarch/tcg/tlb_helper.c +++ b/target/loongarch/tcg/tlb_helper.c @@ -581,7 +581,7 @@ bool loongarch_cpu_tlb_fill(CPUState *cs, vaddr address= , int size, prot =3D context.prot; tlb_set_page(cs, address & TARGET_PAGE_MASK, physical & TARGET_PAGE_MASK, prot, - mmu_idx, TARGET_PAGE_SIZE); + access_type, mmu_idx, TARGET_PAGE_SIZE); qemu_log_mask(CPU_LOG_MMU, "%s address=3D%" VADDR_PRIx " physical " HWADDR_FMT_= plx " prot %d\n", __func__, address, physical, prot); diff --git a/target/m68k/helper.c b/target/m68k/helper.c index 15f110fa7a..aab2ea8d19 100644 --- a/target/m68k/helper.c +++ b/target/m68k/helper.c @@ -969,7 +969,7 @@ bool m68k_cpu_tlb_fill(CPUState *cs, vaddr address, int= size, tlb_set_page(cs, address & TARGET_PAGE_MASK, address & TARGET_PAGE_MASK, PAGE_READ | PAGE_WRITE | PAGE_EXEC, - mmu_idx, TARGET_PAGE_SIZE); + qemu_access_type, mmu_idx, TARGET_PAGE_SIZE); return true; } =20 @@ -989,7 +989,8 @@ bool m68k_cpu_tlb_fill(CPUState *cs, vaddr address, int= size, address, access_type, &page_size); if (likely(ret =3D=3D 0)) { tlb_set_page(cs, address & TARGET_PAGE_MASK, - physical & TARGET_PAGE_MASK, prot, mmu_idx, page_size= ); + physical & TARGET_PAGE_MASK, prot, qemu_access_type, + mmu_idx, page_size); return true; } =20 @@ -1461,6 +1462,7 @@ void HELPER(ptest)(CPUM68KState *env, uint32_t addr, = uint32_t is_read) int prot; int ret; target_ulong page_size; + MMUAccessType qemu_access_type; =20 access_type =3D ACCESS_PTEST; if (env->dfc & 4) { @@ -1468,9 +1470,11 @@ void HELPER(ptest)(CPUM68KState *env, uint32_t addr,= uint32_t is_read) } if ((env->dfc & 3) =3D=3D 2) { access_type |=3D ACCESS_CODE; + qemu_access_type =3D MMU_INST_FETCH; } if (!is_read) { access_type |=3D ACCESS_STORE; + qemu_access_type =3D MMU_DATA_STORE; } =20 env->mmu.mmusr =3D 0; @@ -1480,7 +1484,7 @@ void HELPER(ptest)(CPUM68KState *env, uint32_t addr, = uint32_t is_read) if (ret =3D=3D 0) { tlb_set_page(env_cpu(env), addr & TARGET_PAGE_MASK, physical & TARGET_PAGE_MASK, - prot, access_type & ACCESS_SUPER ? + prot, qemu_access_type, access_type & ACCESS_SUPER ? MMU_KERNEL_IDX : MMU_USER_IDX, page_size); } } diff --git a/target/microblaze/helper.c b/target/microblaze/helper.c index cf577a7226..6d0ec55c13 100644 --- a/target/microblaze/helper.c +++ b/target/microblaze/helper.c @@ -98,8 +98,8 @@ bool mb_cpu_tlb_fill(CPUState *cs, vaddr address, int siz= e, /* MMU disabled or not available. */ address &=3D TARGET_PAGE_MASK; prot =3D PAGE_RWX; - tlb_set_page_with_attrs(cs, address, address, attrs, prot, mmu_idx, - TARGET_PAGE_SIZE); + tlb_set_page_with_attrs(cs, address, address, attrs, prot, access_= type, + mmu_idx, TARGET_PAGE_SIZE); return true; } =20 @@ -110,8 +110,8 @@ bool mb_cpu_tlb_fill(CPUState *cs, vaddr address, int s= ize, =20 qemu_log_mask(CPU_LOG_MMU, "MMU map mmu=3D%d v=3D%x p=3D%x prot=3D= %x\n", mmu_idx, vaddr, paddr, lu.prot); - tlb_set_page_with_attrs(cs, vaddr, paddr, attrs, lu.prot, mmu_idx, - TARGET_PAGE_SIZE); + tlb_set_page_with_attrs(cs, vaddr, paddr, attrs, lu.prot, access_t= ype, + mmu_idx, TARGET_PAGE_SIZE); return true; } =20 diff --git a/target/mips/tcg/system/tlb_helper.c b/target/mips/tcg/system/t= lb_helper.c index 1e8901556d..0d087aadf6 100644 --- a/target/mips/tcg/system/tlb_helper.c +++ b/target/mips/tcg/system/tlb_helper.c @@ -933,7 +933,7 @@ bool mips_cpu_tlb_fill(CPUState *cs, vaddr address, int= size, if (ret =3D=3D TLBRET_MATCH) { tlb_set_page(cs, address & TARGET_PAGE_MASK, physical & TARGET_PAGE_MASK, prot, - mmu_idx, TARGET_PAGE_SIZE); + access_type, mmu_idx, TARGET_PAGE_SIZE); return true; } #if !defined(TARGET_MIPS64) @@ -951,7 +951,7 @@ bool mips_cpu_tlb_fill(CPUState *cs, vaddr address, int= size, if (ret =3D=3D TLBRET_MATCH) { tlb_set_page(cs, address & TARGET_PAGE_MASK, physical & TARGET_PAGE_MASK, prot, - mmu_idx, TARGET_PAGE_SIZE); + access_type, mmu_idx, TARGET_PAGE_SIZE); return true; } } diff --git a/target/openrisc/mmu.c b/target/openrisc/mmu.c index ffb732e0d1..8c43d8350f 100644 --- a/target/openrisc/mmu.c +++ b/target/openrisc/mmu.c @@ -128,7 +128,7 @@ bool openrisc_cpu_tlb_fill(CPUState *cs, vaddr addr, in= t size, if (likely(excp =3D=3D 0)) { tlb_set_page(cs, addr & TARGET_PAGE_MASK, phys_addr & TARGET_PAGE_MASK, prot, - mmu_idx, TARGET_PAGE_SIZE); + access_type, mmu_idx, TARGET_PAGE_SIZE); return true; } if (probe) { diff --git a/target/ppc/mmu_helper.c b/target/ppc/mmu_helper.c index ac60705402..8b55a9e4dd 100644 --- a/target/ppc/mmu_helper.c +++ b/target/ppc/mmu_helper.c @@ -1369,7 +1369,7 @@ bool ppc_cpu_tlb_fill(CPUState *cs, vaddr eaddr, int = size, if (ppc_xlate(cpu, eaddr, access_type, &raddr, &page_size, &prot, mmu_idx, !probe)) { tlb_set_page(cs, eaddr & TARGET_PAGE_MASK, raddr & TARGET_PAGE_MAS= K, - prot, mmu_idx, 1UL << page_size); + prot, access_type, mmu_idx, 1UL << page_size); return true; } if (probe) { diff --git a/target/riscv/cpu_helper.c b/target/riscv/cpu_helper.c index 3479a62cc7..63acd70543 100644 --- a/target/riscv/cpu_helper.c +++ b/target/riscv/cpu_helper.c @@ -1874,7 +1874,7 @@ bool riscv_cpu_tlb_fill(CPUState *cs, vaddr address, = int size, =20 if (ret =3D=3D TRANSLATE_SUCCESS) { tlb_set_page(cs, address & ~(tlb_size - 1), pa & ~(tlb_size - 1), - prot, mmu_idx, tlb_size); + prot, access_type, mmu_idx, tlb_size); return true; } else if (probe) { return false; diff --git a/target/rx/cpu.c b/target/rx/cpu.c index da02ae7bf8..ee2594aa1e 100644 --- a/target/rx/cpu.c +++ b/target/rx/cpu.c @@ -194,7 +194,8 @@ static bool rx_cpu_tlb_fill(CPUState *cs, vaddr addr, i= nt size, /* Linear mapping */ address =3D physical =3D addr & TARGET_PAGE_MASK; prot =3D PAGE_READ | PAGE_WRITE | PAGE_EXEC; - tlb_set_page(cs, address, physical, prot, mmu_idx, TARGET_PAGE_SIZE); + tlb_set_page(cs, address, physical, prot, access_type, + mmu_idx, TARGET_PAGE_SIZE); return true; } =20 diff --git a/target/s390x/tcg/excp_helper.c b/target/s390x/tcg/excp_helper.c index 0ae4e26606..81e180f9f6 100644 --- a/target/s390x/tcg/excp_helper.c +++ b/target/s390x/tcg/excp_helper.c @@ -181,7 +181,7 @@ bool s390_cpu_tlb_fill(CPUState *cs, vaddr address, int= size, "%s: set tlb %" PRIx64 " -> %" PRIx64 " (%x)\n", __func__, (uint64_t)vaddr, (uint64_t)raddr, prot); tlb_set_page(cs, address & TARGET_PAGE_MASK, raddr, prot, - mmu_idx, TARGET_PAGE_SIZE); + access_type, mmu_idx, TARGET_PAGE_SIZE); return true; } if (probe) { diff --git a/target/sh4/helper.c b/target/sh4/helper.c index 3b18a320b8..1a908ce7d5 100644 --- a/target/sh4/helper.c +++ b/target/sh4/helper.c @@ -808,7 +808,7 @@ bool superh_cpu_tlb_fill(CPUState *cs, vaddr address, i= nt size, if (ret =3D=3D MMU_OK) { address &=3D TARGET_PAGE_MASK; physical &=3D TARGET_PAGE_MASK; - tlb_set_page(cs, address, physical, prot, mmu_idx, TARGET_PAGE_SIZ= E); + tlb_set_page(cs, address, physical, prot, access_type, mmu_idx, TA= RGET_PAGE_SIZE); return true; } if (probe) { diff --git a/target/sparc/mmu_helper.c b/target/sparc/mmu_helper.c index 217580a4d8..a4a47e2ca7 100644 --- a/target/sparc/mmu_helper.c +++ b/target/sparc/mmu_helper.c @@ -233,7 +233,7 @@ bool sparc_cpu_tlb_fill(CPUState *cs, vaddr address, in= t size, "Translate at %" VADDR_PRIx " -> " HWADDR_FMT_plx ", vaddr " TARGET_FMT_lx "\n", address, full.phys_addr, vaddr); - tlb_set_page_full(cs, mmu_idx, vaddr, &full); + tlb_set_page_full(cs, mmu_idx, vaddr, access_type, &full); return true; } =20 @@ -249,7 +249,7 @@ bool sparc_cpu_tlb_fill(CPUState *cs, vaddr address, in= t size, neverland. Fake/overridden mappings will be flushed when switching to normal mode. */ full.prot =3D PAGE_READ | PAGE_WRITE | PAGE_EXEC; - tlb_set_page_full(cs, mmu_idx, vaddr, &full); + tlb_set_page_full(cs, mmu_idx, vaddr, access_type, &full); return true; } else { if (access_type =3D=3D MMU_INST_FETCH) { @@ -773,7 +773,7 @@ bool sparc_cpu_tlb_fill(CPUState *cs, vaddr address, in= t size, trace_mmu_helper_mmu_fault(address, full.phys_addr, mmu_idx, env->= tl, env->dmmu.mmu_primary_context, env->dmmu.mmu_secondary_context); - tlb_set_page_full(cs, mmu_idx, address, &full); + tlb_set_page_full(cs, mmu_idx, address, access_type, &full); return true; } if (probe) { diff --git a/target/tricore/helper.c b/target/tricore/helper.c index 7574111c87..ad0c8b11d3 100644 --- a/target/tricore/helper.c +++ b/target/tricore/helper.c @@ -86,7 +86,7 @@ bool tricore_cpu_tlb_fill(CPUState *cs, vaddr address, in= t size, if (ret =3D=3D TLBRET_MATCH) { tlb_set_page(cs, address & TARGET_PAGE_MASK, physical & TARGET_PAGE_MASK, prot | PAGE_EXEC, - mmu_idx, TARGET_PAGE_SIZE); + rw, mmu_idx, TARGET_PAGE_SIZE); return true; } else { assert(ret < 0); diff --git a/target/xtensa/helper.c b/target/xtensa/helper.c index 2d93b45036..2cd51ba0cb 100644 --- a/target/xtensa/helper.c +++ b/target/xtensa/helper.c @@ -282,7 +282,8 @@ bool xtensa_cpu_tlb_fill(CPUState *cs, vaddr address, i= nt size, tlb_set_page(cs, address & TARGET_PAGE_MASK, paddr & TARGET_PAGE_MASK, - access, mmu_idx, page_size); + access, access_type, mmu_idx, + page_size); return true; } else if (probe) { return false; --=20 2.43.0 From nobody Fri Nov 14 17:01:12 2025 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; dmarc=pass(p=reject dis=none) header.from=sifive.com ARC-Seal: i=1; a=rsa-sha256; t=1761062366; cv=none; d=zohomail.com; s=zohoarc; b=RlZYYeiJ9rqTAzxAu8quyWjGmEJbDTSwAQwOML+KXlZRvDYA0fGIvWJi8BTaDOKC2IF9qF2iNjaJWZQQwL9t/e5EGToEe2+pshiRGB0DfqAEmy6C4HUGfEDG/4Pofk64BmcNuomL1nj2QYc8KRDS6bMhvzWQL89kuM2f5O/aQuM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1761062366; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=nAFt0XM3JNUMXpzfgz5nUHYjB7++JZQOscrYhRrmG8U=; b=HzmasBDUFcAji9mORRY8DkPQy7bdsh/PBTIO9tyG+mAPg5fTOEKaQEUm3XoIA4z4uY9UKxmB9s9/w4SrjwkKKQj34h5clmt2BgUUxWXeZy9dPTmg5L917FLvrR6hjiX30q5f8fjqK7X2Ckl4iJE14ziikQ6fXHgpUWw2UsTUPYw= 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; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 176106236621476.43918238846322; Tue, 21 Oct 2025 08:59:26 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1vBEj3-00013k-1x; Tue, 21 Oct 2025 11:56:41 -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 1vBEiq-0000xs-3J for qemu-devel@nongnu.org; Tue, 21 Oct 2025 11:56:29 -0400 Received: from mail-pf1-x42c.google.com ([2607:f8b0:4864:20::42c]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1vBEin-0007mr-7r for qemu-devel@nongnu.org; Tue, 21 Oct 2025 11:56:27 -0400 Received: by mail-pf1-x42c.google.com with SMTP id d2e1a72fcca58-782a77b5ec7so5346473b3a.1 for ; Tue, 21 Oct 2025 08:56:22 -0700 (PDT) Received: from hsinchu26.internal.sifive.com ([210.176.154.34]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-33d5de11392sm11490177a91.7.2025.10.21.08.56.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Oct 2025 08:56:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1761062181; x=1761666981; 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=nAFt0XM3JNUMXpzfgz5nUHYjB7++JZQOscrYhRrmG8U=; b=L+qNtrM2Orv3H6mWBbuZxCn0OM60ElmUZ9y0VDJMW4oxAJYhpqIQGUy6g4R1FMR3Zu E7Jx6B5NKyYW18W8rtNF+iWm4NKt9+yqfq912HMPNH5PGfOinQ9ElCGaAhQNJsbfQwX/ qSJiLbpasCS/2Zt6lnLQ+gfBZy+lPc/kjJaH6G4fIutSCL3DahTTt6UdGdd7qS2cFzak /2/6+6JYQReLobqcEF8mh+L5lt/SBK9cLH8V5eTI8eMEl7ikqSfHm2JV35T9S9WB/X7Q Xe8gFB/btfdtiPvV/r11AkrSfJEEGHWWujTpSD8aUykHVejiWastG/Uo0T7ZsLKpNCIL mCyw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1761062181; x=1761666981; 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=nAFt0XM3JNUMXpzfgz5nUHYjB7++JZQOscrYhRrmG8U=; b=FTx21lDG5s4dIwqORALcrqwVkGZZUM91xekEuwaBNOfxyMb2npvbZe7lIBtFGAD38n zYyzdbL1pVaaw5VraqLGbBFXnzc7oCBVQ07xuxGlwxHkTk0s+IAIDtNV7I6sZK5mviCL UiciTnstfr1Ikfcdq8uY7TgctthRld5hMZUhJwKafbnE6XPidNpZqc7jrZHZ/kXw9GZu sBV0G3RhMqd3MJwrOYfP77myGNpcH7HXV9lvKIT5uzsy8+LT5R6kKkVSVlH0Azt4Vx19 VWlHjXzwri3AQo7grXq3vUv5EviGGBoUxCTIXkQHRTo08e/nLPXSjfgsaaE46DfSMs9b ehiA== X-Gm-Message-State: AOJu0YzwT/L0/TO5yH5fqCwDhpnLt5vPH12NW+Yxlw+jS5arZUxtHty/ ehzs9Qdqky4VXDwRjD36zMMZ/64hwRlpX51WPjv0/7+vBChA4dlFc4OKtvV/MvhBO6/ScsHegVO Hd7k7zFlX8Sv7IUEk3Kc3OktRNFflfJyL7J0y19dym2ukKfRZs01+K8zktjU/AixFzvDld1HZa+ IlZsDpZdky1KGWVpkAgo6NGzbp5AVnsH6dPZxaLw== X-Gm-Gg: ASbGncuxnU0N+WSVif5qp3pCm6+y8CJRFkE6/qqO1fZLHutXZP0yElEbS39l2WwrVc7 q0WaZAix8Cs10Z+kK9X0cfnUts0IqEr+9j0jd8VDSFRJw/tGsfDcOzAaDduQVt4xxoFLyGtQR3O 28Jflag+Wsjl8NQTMUovufiieK/YdI4nI9blhJ9j677RSlrqS7vu0zBHJF0/7bETWo9Guj0hHSx HC+izJ0ZyJUzxRlVjKVPLwp/VtuVDuOhD4Ibv7gGmrdXsaORsiDVmfWjevOWZIi+ssLF7RBymRJ c4294l7/1vNRXzUiLb0zxiz1/9aowgPsjLv6t4E0S0DjUnGubjrOxM7QNikbuBzRQgIXQ+MsCVd nSZs915VnH/DRexOwBX/M83t2QPPkPgrOna78IU4SHAKHpH83ylM1OD0X55dnZYfZWlVsdl1qq1 7CPXFnGKoYfB8tungjg3WMPRlXVYfVL7yT X-Google-Smtp-Source: AGHT+IG/Ymu0La9V/H/4kILD0yf/r9Yxm6kvaeqrFGmFr0ifIN+gAOunrqLzWFFlHpmxgvl8/uHWHw== X-Received: by 2002:a17:90b:2d8f:b0:339:f09b:d36f with SMTP id 98e67ed59e1d1-33bcf8f8ca4mr21616673a91.28.1761062181172; Tue, 21 Oct 2025 08:56:21 -0700 (PDT) From: Jim Shu To: qemu-devel@nongnu.org, qemu-riscv@nongnu.org Cc: Daniel Henrique Barboza , Richard Henderson , Paolo Bonzini , Palmer Dabbelt , Alistair Francis , Weiwei Li , Liu Zhiwei , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Eduardo Habkost , Marcel Apfelbaum , Yanan Wang , Zhao Liu , Peter Xu , David Hildenbrand , Michael Rolnik , Helge Deller , Song Gao , Laurent Vivier , "Edgar E. Iglesias" , Aurelien Jarno , Jiaxun Yang , Aleksandar Rikalo , Stafford Horne , Nicholas Piggin , Chinmay Rath , Yoshinori Sato , Ilya Leoshkevich , Thomas Huth , Mark Cave-Ayland , Artyom Tarasenko , Bastian Koppelmann , Max Filippov , qemu-ppc@nongnu.org (open list:PowerPC TCG CPUs), qemu-s390x@nongnu.org (open list:S390 TCG CPUs), Jim Shu Subject: [PATCH v3 04/18] exec: Add RISC-V WorldGuard WID to MemTxAttrs Date: Tue, 21 Oct 2025 23:55:34 +0800 Message-ID: <20251021155548.584543-5-jim.shu@sifive.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20251021155548.584543-1-jim.shu@sifive.com> References: <20251021155548.584543-1-jim.shu@sifive.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: pass client-ip=2607:f8b0:4864:20::42c; envelope-from=jim.shu@sifive.com; helo=mail-pf1-x42c.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable 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 @sifive.com) X-ZM-MESSAGEID: 1761062366728158500 Content-Type: text/plain; charset="utf-8" RISC-V WorldGuard will add 5-bit world_id (WID) to the each memory transaction on the bus. The wgChecker in front of RAM or peripherals MMIO could do the access control based on the WID. It is similar to ARM TrustZone NS bit, but the WID is 5-bit. The common implementation of WID is AXI4 AxUSER signal. Since the '_reserved*' fields in MemTxAttr are for padding the struct to be 8-byte [1], we change the reserved fields from 3 to 2 bytes when adding 5-bit world_id field. [1] Commit 5014e33b1e00d330f13df33c09a3932ac88f8d94 Link: https://lore.kernel.org/r/20250121151322.171832-2-zhao1.liu@intel= .com Signed-off-by: Jim Shu --- include/exec/memattrs.h | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/include/exec/memattrs.h b/include/exec/memattrs.h index 52ee955249..6bde90d482 100644 --- a/include/exec/memattrs.h +++ b/include/exec/memattrs.h @@ -57,6 +57,11 @@ typedef struct MemTxAttrs { /* PCI - IOMMU operations, see PCIAddressType */ unsigned int address_type:1; =20 + /* + * RISC-V WorldGuard: the 5-bit WID field of memory access. + */ + unsigned int world_id:5; + /* * Bus masters which don't specify any attributes will get this * (via the MEMTXATTRS_UNSPECIFIED constant), so that we can @@ -66,8 +71,7 @@ typedef struct MemTxAttrs { */ bool unspecified; =20 - uint8_t _reserved1; - uint16_t _reserved2; + uint16_t _reserved1; } MemTxAttrs; =20 QEMU_BUILD_BUG_ON(sizeof(MemTxAttrs) > 8); --=20 2.43.0 From nobody Fri Nov 14 17:01:12 2025 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; dmarc=pass(p=reject dis=none) header.from=sifive.com ARC-Seal: i=1; a=rsa-sha256; t=1761062303; cv=none; d=zohomail.com; s=zohoarc; b=CqYs0bUV9Q3XehT9kccFQ6TKkw6GMei2iuxL+zgdcnfrR55YozY45/wZPsjoDxq85MjtEwBLm3mYcS9/5od1X4mdeAc6BQj6TFsXeJrXbRvzAd+ApuabM10I/4NgD2ZHzrmtQzq8Fsa1on05OSzBpccxjAwLnCuMOXjPlnAxfkg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1761062303; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=GY9PGMdFzEQRlghTOzCeBl7G03PRuXs1m7DET48CoN0=; b=jDgpI4ib0e9TwGIMNTnZooTucVWkav7jaUnOOmAQeYeJpiv2XKB/gK0ryl93sZu0duqHhKw2fICl40LKQRtfOmSGy6gJdmvgU49aEuGEuoPe/eJnMZClTjOnNmgrk5AyZih4g4O7rc8P27QEdKXwbiHZoR8XhvXkCIvJaUZ89sU= 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; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1761062303195858.3498042178555; Tue, 21 Oct 2025 08:58:23 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1vBEj4-000148-1S; Tue, 21 Oct 2025 11:56:42 -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 1vBEiw-00010v-Jo for qemu-devel@nongnu.org; Tue, 21 Oct 2025 11:56:34 -0400 Received: from mail-pj1-x1032.google.com ([2607:f8b0:4864:20::1032]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1vBEir-0007nx-Dm for qemu-devel@nongnu.org; Tue, 21 Oct 2025 11:56:33 -0400 Received: by mail-pj1-x1032.google.com with SMTP id 98e67ed59e1d1-339e71ccf48so7813078a91.3 for ; Tue, 21 Oct 2025 08:56:29 -0700 (PDT) Received: from hsinchu26.internal.sifive.com ([210.176.154.34]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-33d5de11392sm11490177a91.7.2025.10.21.08.56.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Oct 2025 08:56:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1761062187; x=1761666987; 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=GY9PGMdFzEQRlghTOzCeBl7G03PRuXs1m7DET48CoN0=; b=KP224jKXNV1VWNgHzTXph3qaIzTHYR1seBF5C1IHX2C3nHGRPs2z5pH90dnhCAhuLI q0lsuGOQKT6N2q4GT3zBbkWSI5rdl9so8Lwn7nA+Adnc/4Ymj6JbwQyNOTtbI5aPgNpK XqqfASZeX0TyZ16KODsrI5orV+Y+aycDVAuEE/DZ3OTx2zCblpidbM7hkxiS1iljukp7 8S29gAq4IQWpdfyFkdAEejEHsLwF6NUXPgZoQBX+Qh7jgLmdPdI7jqVbT2qUNEGG9ohw kqp/ypuuNe3aV2poWhSP1dbMbxF9pctIBp8cimieyCrj2A4hzu9SzPQ1iq37S1UgROTA OCSg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1761062187; x=1761666987; 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=GY9PGMdFzEQRlghTOzCeBl7G03PRuXs1m7DET48CoN0=; b=R8M28UhaZJWtbUHzNtsrgZ+q+++3vkNrYPJGGmh8gAI8XGDNNDkOCj8zST+IyV8E32 aoiESpw2g8w4EEnn3O0oWZxTK2+3i/2/lOGYv3muMLjf0IyXC9M4memAUCRUUPpTZ2Ww dK2r2BmiRH167wg0VsFM3JYf3cS4WDa4Eo60UTnPaNR6vMYE7JLNFdoSrmStFOv01H8p rMlqhzjfeHeORxF/gcCyUtgyclj17OKaogJOhVUeAgIOH09GJe7JijUm7QzuB0GrpqVn iCZtpq/4NihA1qvthm6ayXut6WLphjtqJKvxURJ9Wbx1cZ2LQXyZvjQ622SvmYZx6Q3+ LdHw== X-Gm-Message-State: AOJu0Yy8mg6Tc33MKIF4uMmGrrJE8+8bYQuIdjo3xZ0azb02Guc5Ks89 1oZ2RM2PETqyhTKfjlRkxBvmHO5FaSyGYcOyPNymiy8D2gZUHE9WnONNOfufi1Y86/y7g0eCwpg 5R0gI9EFGesue1aY8vpmr2AwbYbn/4t24zU6QkxY921W9Tn3eVFtJiZUoJWOdHdolUA3HzbHOrP 9KgAXPAQOQjNiFyF3qFo4BXhZeXolpN8DRJqeCEw== X-Gm-Gg: ASbGncugVpkSrozFf4UWN8NGUmuFUpYU40MejGS1dz/2LhZFWKJnPunBadqa2XtsREC TRK0puH5A2WoOVTKLoCVO0KyLfYVhtsaFmXlF7AloaDSPrzfAbu/d3iJNeKB0TZJYRcz1Nx3GTL +wPSEogBb5/VL91wf0JVfHnDQFAMvqEjYM4H0AWWv7V9VoPRuuSJMJeUDUdLr9BL0ADHRL/aSca MY3NeiTh3mmPVvNgEw56QLOyd/IPS9L5CbbGmEvFJQn/4PC740lfaI7u+FKGSz9EnV0WlZFUowO pY1+flKJRZnPLnGwH78MShcOOshtrgPK3b32EK1snb9vIL+u5CPAkin1QDbq3fCAMXrKzPDE/dk 6uLVz7Y9/uYLJKr41YXLlyMrNbYM3s6IdoOlS0L26lgs+wNU4k8xTYPQrxGjgRIxdWMmXLGt9zW 5d7MmT8x5DqMw31Kvw73RF9tDaH4Lrw1eY X-Google-Smtp-Source: AGHT+IFvxjuyQ6KypntX7BYav7ZYE0DcCPs/GqeUQUAFmqaHWW8T+XrZaFKcZZDsOY83GxRm9PjDAw== X-Received: by 2002:a17:90b:4b09:b0:32e:d600:4fe9 with SMTP id 98e67ed59e1d1-33bcf85ceacmr25366723a91.4.1761062186890; Tue, 21 Oct 2025 08:56:26 -0700 (PDT) From: Jim Shu To: qemu-devel@nongnu.org, qemu-riscv@nongnu.org Cc: Daniel Henrique Barboza , Richard Henderson , Paolo Bonzini , Palmer Dabbelt , Alistair Francis , Weiwei Li , Liu Zhiwei , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Eduardo Habkost , Marcel Apfelbaum , Yanan Wang , Zhao Liu , Peter Xu , David Hildenbrand , Michael Rolnik , Helge Deller , Song Gao , Laurent Vivier , "Edgar E. Iglesias" , Aurelien Jarno , Jiaxun Yang , Aleksandar Rikalo , Stafford Horne , Nicholas Piggin , Chinmay Rath , Yoshinori Sato , Ilya Leoshkevich , Thomas Huth , Mark Cave-Ayland , Artyom Tarasenko , Bastian Koppelmann , Max Filippov , qemu-ppc@nongnu.org (open list:PowerPC TCG CPUs), qemu-s390x@nongnu.org (open list:S390 TCG CPUs), Jim Shu Subject: [PATCH v3 05/18] hw/misc: riscv_worldguard: Add RISC-V WorldGuard global config Date: Tue, 21 Oct 2025 23:55:35 +0800 Message-ID: <20251021155548.584543-6-jim.shu@sifive.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20251021155548.584543-1-jim.shu@sifive.com> References: <20251021155548.584543-1-jim.shu@sifive.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: pass client-ip=2607:f8b0:4864:20::1032; envelope-from=jim.shu@sifive.com; helo=mail-pj1-x1032.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable 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 @sifive.com) X-ZM-MESSAGEID: 1761062308321158500 Content-Type: text/plain; charset="utf-8" Add a device for RISCV WG global config, which contains the number of worlds, reset value, and trusted WID ... etc. This global config is used by both CPU WG extension and wgChecker devices. Signed-off-by: Jim Shu Reviewed-by: Daniel Henrique Barboza --- hw/misc/Kconfig | 3 + hw/misc/meson.build | 1 + hw/misc/riscv_worldguard.c | 182 +++++++++++++++++++++++++++++ include/hw/misc/riscv_worldguard.h | 56 +++++++++ 4 files changed, 242 insertions(+) create mode 100644 hw/misc/riscv_worldguard.c create mode 100644 include/hw/misc/riscv_worldguard.h diff --git a/hw/misc/Kconfig b/hw/misc/Kconfig index 4e35657468..bee8824868 100644 --- a/hw/misc/Kconfig +++ b/hw/misc/Kconfig @@ -235,4 +235,7 @@ config IOSB config XLNX_VERSAL_TRNG bool =20 +config RISCV_WORLDGUARD + bool + source macio/Kconfig diff --git a/hw/misc/meson.build b/hw/misc/meson.build index b1d8d8e5d2..200ccc96c0 100644 --- a/hw/misc/meson.build +++ b/hw/misc/meson.build @@ -34,6 +34,7 @@ system_ss.add(when: 'CONFIG_SIFIVE_E_PRCI', if_true: file= s('sifive_e_prci.c')) system_ss.add(when: 'CONFIG_SIFIVE_E_AON', if_true: files('sifive_e_aon.c'= )) system_ss.add(when: 'CONFIG_SIFIVE_U_OTP', if_true: files('sifive_u_otp.c'= )) system_ss.add(when: 'CONFIG_SIFIVE_U_PRCI', if_true: files('sifive_u_prci.= c')) +specific_ss.add(when: 'CONFIG_RISCV_WORLDGUARD', if_true: files('riscv_wor= ldguard.c')) =20 subdir('macio') =20 diff --git a/hw/misc/riscv_worldguard.c b/hw/misc/riscv_worldguard.c new file mode 100644 index 0000000000..588c16ae9a --- /dev/null +++ b/hw/misc/riscv_worldguard.c @@ -0,0 +1,182 @@ +/* + * RISC-V WorldGuard Device + * + * Copyright (c) 2022 SiFive, Inc. + * + * This provides WorldGuard global config. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2 or later, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License f= or + * more details. + * + * You should have received a copy of the GNU General Public License along= with + * this program. If not, see . + */ + +#include "qemu/osdep.h" +#include "qapi/error.h" +#include "qemu/log.h" +#include "exec/hwaddr.h" +#include "hw/registerfields.h" +#include "hw/sysbus.h" +#include "hw/hw.h" +#include "hw/qdev-properties.h" +#include "hw/misc/riscv_worldguard.h" +#include "hw/core/cpu.h" +#include "target/riscv/cpu.h" +#include "trace.h" + +/* + * WorldGuard global config: + * List the global setting of WG, like num-of-worlds. It is unique in the = machine. + * All CPUs with WG extension and wgChecker devices will use it. + */ +struct RISCVWorldGuardState *worldguard_config; + +static Property riscv_worldguard_properties[] =3D { + DEFINE_PROP_UINT32("nworlds", RISCVWorldGuardState, nworlds, 0), + + /* Only Trusted WID could access wgCheckers if it is enabled. */ + DEFINE_PROP_UINT32("trustedwid", RISCVWorldGuardState, trustedwid, NO_= TRUSTEDWID), + + /* + * WG reset value is bypass mode in HW. All WG permission checkings are + * pass by default, so SW could correctly run on the machine w/o any WG + * programming. + */ + DEFINE_PROP_BOOL("hw-bypass", RISCVWorldGuardState, hw_bypass, false), + + /* + * TrustZone compatible mode: + * This mode is only supported in 2 worlds system. It converts WorldGu= ard + * WID to TZ NS signal on the bus so WG could be cooperated with + * TZ components. In QEMU, it converts WID to 'MemTxAttrs.secure' bit = used + * by TZ. + */ + DEFINE_PROP_BOOL("tz-compat", RISCVWorldGuardState, tz_compat, false), +}; + +/* WID to MemTxAttrs converter */ +void wid_to_mem_attrs(MemTxAttrs *attrs, uint32_t wid) +{ + g_assert(wid < worldguard_config->nworlds); + + attrs->unspecified =3D 0; + if (worldguard_config->tz_compat) { + attrs->secure =3D wid; + } else { + attrs->world_id =3D wid; + } +} + +/* MemTxAttrs to WID converter */ +uint32_t mem_attrs_to_wid(MemTxAttrs attrs) +{ + if (attrs.unspecified) { + if (worldguard_config->trustedwid !=3D NO_TRUSTEDWID) { + return worldguard_config->trustedwid; + } else { + return worldguard_config->nworlds - 1; + } + } + + if (worldguard_config->tz_compat) { + return attrs.secure; + } else { + return attrs.world_id; + } +} + +bool could_access_wgblocks(MemTxAttrs attrs, const char *wgblock) +{ + uint32_t wid =3D mem_attrs_to_wid(attrs); + uint32_t trustedwid =3D worldguard_config->trustedwid; + + if ((trustedwid =3D=3D NO_TRUSTEDWID) || (wid =3D=3D trustedwid)) { + return true; + } else { + /* + * Only Trusted WID could access WG blocks if having it. + * Access them from other WIDs will get failed. + */ + qemu_log_mask(LOG_GUEST_ERROR, + "%s: Invalid access to %s from non-trusted WID %d\n", + __func__, wgblock, wid); + + return false; + } +} + +static void riscv_worldguard_realize(DeviceState *dev, Error **errp) +{ + RISCVWorldGuardState *s =3D RISCV_WORLDGUARD(dev); + + if (worldguard_config !=3D NULL) { + error_setg(errp, "Couldn't realize multiple global WorldGuard conf= igs."); + return; + } + + if ((s->nworlds) & (s->nworlds - 1)) { + error_setg(errp, "Current implementation only support power-of-2 N= World."); + return; + } + + if ((s->trustedwid !=3D NO_TRUSTEDWID) && (s->trustedwid >=3D s->nworl= ds)) { + error_setg(errp, "Trusted WID must be less than the number of worl= d."); + return; + } + + if ((s->nworlds !=3D 2) && (s->tz_compat)) { + error_setg(errp, "Only 2 worlds system could use TrustZone compati= ble mode."); + return; + } + + /* Register WG global config */ + worldguard_config =3D s; + + /* Initialize global data for wgChecker */ + wgc_slot_perm_mask =3D MAKE_64BIT_MASK(0, 2 * worldguard_config->nworl= ds); +} + +static void riscv_worldguard_class_init(ObjectClass *klass, const void *da= ta) +{ + DeviceClass *dc =3D DEVICE_CLASS(klass); + + device_class_set_props(dc, riscv_worldguard_properties); + dc->user_creatable =3D true; + dc->realize =3D riscv_worldguard_realize; +} + +static const TypeInfo riscv_worldguard_info =3D { + .name =3D TYPE_RISCV_WORLDGUARD, + .parent =3D TYPE_DEVICE, + .instance_size =3D sizeof(RISCVWorldGuardState), + .class_init =3D riscv_worldguard_class_init, +}; + +/* + * Create WorldGuard global config + */ +DeviceState *riscv_worldguard_create(uint32_t nworlds, uint32_t trustedwid, + bool hw_bypass, bool tz_compat) +{ + DeviceState *dev =3D qdev_new(TYPE_RISCV_WORLDGUARD); + qdev_prop_set_uint32(dev, "nworlds", nworlds); + qdev_prop_set_uint32(dev, "trustedwid", trustedwid); + qdev_prop_set_bit(dev, "hw-bypass", hw_bypass); + qdev_prop_set_bit(dev, "tz-compat", tz_compat); + qdev_realize(DEVICE(dev), NULL, &error_fatal); + return dev; +} + +static void riscv_worldguard_register_types(void) +{ + type_register_static(&riscv_worldguard_info); +} + +type_init(riscv_worldguard_register_types) diff --git a/include/hw/misc/riscv_worldguard.h b/include/hw/misc/riscv_wor= ldguard.h new file mode 100644 index 0000000000..bb276e59b8 --- /dev/null +++ b/include/hw/misc/riscv_worldguard.h @@ -0,0 +1,56 @@ +/* + * RISC-V WorldGuard Devices + * + * Copyright (c) 2022 RISCV, Inc. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2 or later, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License f= or + * more details. + * + * You should have received a copy of the GNU General Public License along= with + * this program. If not, see . + */ + +#ifndef HW_RISCV_WORLDGUARD_H +#define HW_RISCV_WORLDGUARD_H + +#include "qom/object.h" +#include "hw/sysbus.h" +#include "exec/hwaddr.h" + +#define TYPE_RISCV_WORLDGUARD "riscv.worldguard" + +#define NO_TRUSTEDWID UINT32_MAX + +typedef struct RISCVWorldGuardState RISCVWorldGuardState; +DECLARE_INSTANCE_CHECKER(RISCVWorldGuardState, RISCV_WORLDGUARD, + TYPE_RISCV_WORLDGUARD) + +struct RISCVWorldGuardState { + /*< private >*/ + DeviceState parent_obj; + + /*< public >*/ + + /* Property */ + uint32_t nworlds; + uint32_t trustedwid; + bool hw_bypass; + bool tz_compat; +}; + +extern struct RISCVWorldGuardState *worldguard_config; + +DeviceState *riscv_worldguard_create(uint32_t nworlds, uint32_t trustedwid, + bool hw_bypass, bool tz_compat); + +void wid_to_mem_attrs(MemTxAttrs *attrs, uint32_t wid); +uint32_t mem_attrs_to_wid(MemTxAttrs attrs); +bool could_access_wgblocks(MemTxAttrs attrs, const char *wgblock); + +#endif --=20 2.43.0 From nobody Fri Nov 14 17:01:12 2025 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; dmarc=pass(p=reject dis=none) header.from=sifive.com ARC-Seal: i=1; a=rsa-sha256; t=1761062348; cv=none; d=zohomail.com; s=zohoarc; b=RDEv0BwATAn9BEPpqPORV5Eq73vaV9vWKQBBwaLpaxyOevSioqq8ufLOGlqSI5+K3j7TYKRp+v0urWsx/3aSaufGmCrsLZerZkwyWJMtVd//BbA6LEOwIHyhJRf8exYHaBoXztmAi/tqy9JAk7Eq1eXh7/13EuVRM0sbwPWhmig= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1761062348; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=1YPyDNhfux04WDvQPtw33/XTRtPeCUZGQEMXAPkmtXk=; b=NUaF8vxo0yftuANhzj0HC1eCGiHX151HImiis81QAEwDFt0Vn238Eo9la/KE9V6aa2HiNT9J+yg1Oq+dvetI5DUINOLlw+7pqXtLmgTuR7v4RF5LSPCZTi4fe8ZiZSYNqU1g0p3+J9auakdx5zuCPu49ShxblRacEp+W27kYeZ4= 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; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1761062348658176.9147381124966; Tue, 21 Oct 2025 08:59:08 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1vBEj4-00014c-JK; Tue, 21 Oct 2025 11:56:42 -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 1vBEiz-00012j-Rp for qemu-devel@nongnu.org; Tue, 21 Oct 2025 11:56:38 -0400 Received: from mail-pj1-x102e.google.com ([2607:f8b0:4864:20::102e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1vBEix-0007oa-0D for qemu-devel@nongnu.org; Tue, 21 Oct 2025 11:56:37 -0400 Received: by mail-pj1-x102e.google.com with SMTP id 98e67ed59e1d1-33226dc4fc9so5360609a91.1 for ; Tue, 21 Oct 2025 08:56:34 -0700 (PDT) Received: from hsinchu26.internal.sifive.com ([210.176.154.34]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-33d5de11392sm11490177a91.7.2025.10.21.08.56.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Oct 2025 08:56:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1761062193; x=1761666993; 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=1YPyDNhfux04WDvQPtw33/XTRtPeCUZGQEMXAPkmtXk=; b=A+9/0P2g0ose+C73yqes+aoYPClWlYRoZLe8ZXOxWZKcfpP/C2GbOzBUgxzzWG5+lj S4SUQtJG5ql78wolLzSD68jLM95URg0ghVtWXPBAQoMALz9Q/iBVYlfI2qI1I4v26Xd/ 3GcG1wclrXbY1w/JtQSw74YCwkHLWch78q5LJVwE8QFhbCHQncLKWKqsW3ptWlqgKvD+ 9xkm9XVGwJWcS23TWjDhJRM+qBinCDIakvdT/SEwz08A9sNXcwyKNrgzAFVs9fvYHJ7B swYUxICGO2rj+W1NI1vlzgas439FNncWh4hc6zNTAA6DQR/5vi/fEAsKDZRm4EEvgTe+ KLvQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1761062193; x=1761666993; 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=1YPyDNhfux04WDvQPtw33/XTRtPeCUZGQEMXAPkmtXk=; b=vTLzifkmH7EK8228d1fg+982BMxvAShzsUpOrMv7kjvc6e6+9+JZh6Y0EQ0XOjfsQJ dgGrARUtl6HpHVzrCFO9bjuijEN7BuNEHwrmo9Q8swDl/WmJyr2htDJmFiYCf3gfT0ep JGg7PYdYK95ruFJtlL0AkR/4Eop2e+NaoCxUhlo9+LcZWKuybYQMveWcXUZAhtF7XY2O 3bdUPxwmlduXwnJ/fRIQ/VGMRkl+aDG2P0HGTCVq4VCC9Awwc9H5dtezwEngkUvj814s EA2xHASs2vQi8BVXi+O6iQ79osFO6qTPFzh2quiStSfwD++8wC0hb+XvXtK+RI58NnE0 el1g== X-Gm-Message-State: AOJu0YwW3j8MMGcvW8jKl7ZfE8QzhmlpayCoz9QLJ4L+kJX7fLqS8i35 NsACzDLZqCVnrkup0gMWsIOhThTn5efI3LmVZx1hD5fBNbZybhPIlBSHQ300tg6PO+JgLN3FCeh IJMRuDT/MMaTaoRl6wtBm2qpfSvqs2c4ZhC0N0KJhKuxGnpRHmJ6XBKgS3fAHqLh0RMsDToPoZM 1YUMboSBswFb0UKo0aGAPmFsPl6KzYBkpp1WcuQQ== X-Gm-Gg: ASbGncsle5KdciuzrqnFBUdpc/Rr7BZY9UPGh4OLwGbgOVP8jNkN+2SkIDrypHpht8V 1ksD4i90+XkaHfBC/PumJyY5cFHe1uEkhl4GsGVhcNbB3XcMvz/OsyxF/I6v3NoYtLYxt/0Q8/C fCQg24OCYQrsI1YRhzgsWg7C9QDjOfa2ibQoNMz1YygA/l9Cox9km28JRKLcuR50aj69zr4dDis QAJGB6nZ9ERJ6Zv1kHJURQUf6fzDAURsAq6TE74a5aSCT4HUTgZV2HxnhLUTIHHAH9UkSHk9quf KCEI+zgNN47t/EI8ov+tC9DEyb7fVNnHVTRV2Qs5UwLaZxpL9M/ssbEWUErdQHyg2xlU2atgEm7 LrU2UWi/MKKr66TT+vtBnN1wEkgpHnizHwjNP+6tjYVDHsq8piI3rQ+/tVGO3CX8Qqd+uKMnbr1 JyM1Tv5N5P04PfOhkKtDzqeA== X-Google-Smtp-Source: AGHT+IEsTGfGISdwoHtefwuOV4DrhpdRTrH6sNO0Biy/c/78idzNfMmzW7N8QYwaPO0rl9vfdaQYUQ== X-Received: by 2002:a17:90b:1e07:b0:33b:ba40:c3f with SMTP id 98e67ed59e1d1-33bcf85a834mr22079560a91.5.1761062192678; Tue, 21 Oct 2025 08:56:32 -0700 (PDT) From: Jim Shu To: qemu-devel@nongnu.org, qemu-riscv@nongnu.org Cc: Daniel Henrique Barboza , Richard Henderson , Paolo Bonzini , Palmer Dabbelt , Alistair Francis , Weiwei Li , Liu Zhiwei , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Eduardo Habkost , Marcel Apfelbaum , Yanan Wang , Zhao Liu , Peter Xu , David Hildenbrand , Michael Rolnik , Helge Deller , Song Gao , Laurent Vivier , "Edgar E. Iglesias" , Aurelien Jarno , Jiaxun Yang , Aleksandar Rikalo , Stafford Horne , Nicholas Piggin , Chinmay Rath , Yoshinori Sato , Ilya Leoshkevich , Thomas Huth , Mark Cave-Ayland , Artyom Tarasenko , Bastian Koppelmann , Max Filippov , qemu-ppc@nongnu.org (open list:PowerPC TCG CPUs), qemu-s390x@nongnu.org (open list:S390 TCG CPUs), Jim Shu Subject: [PATCH v3 06/18] target/riscv: Add CPU options of WorldGuard CPU extension Date: Tue, 21 Oct 2025 23:55:36 +0800 Message-ID: <20251021155548.584543-7-jim.shu@sifive.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20251021155548.584543-1-jim.shu@sifive.com> References: <20251021155548.584543-1-jim.shu@sifive.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: pass client-ip=2607:f8b0:4864:20::102e; envelope-from=jim.shu@sifive.com; helo=mail-pj1-x102e.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable 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 @sifive.com) X-ZM-MESSAGEID: 1761062351220154100 Content-Type: text/plain; charset="utf-8" We define CPU options for WG CSR support in RISC-V CPUs which can be set by machine/device emulation. The RISC-V CSR emulation will also check this feature for emulating WG CSRs. Signed-off-by: Jim Shu --- target/riscv/cpu.c | 3 +++ target/riscv/cpu_cfg_fields.h.inc | 3 +++ target/riscv/tcg/tcg-cpu.c | 11 +++++++++++ 3 files changed, 17 insertions(+) diff --git a/target/riscv/cpu.c b/target/riscv/cpu.c index a877018ab0..03946439f2 100644 --- a/target/riscv/cpu.c +++ b/target/riscv/cpu.c @@ -207,6 +207,8 @@ const RISCVIsaExtData isa_edata_arr[] =3D { ISA_EXT_DATA_ENTRY(smmpm, PRIV_VERSION_1_13_0, ext_smmpm), ISA_EXT_DATA_ENTRY(smnpm, PRIV_VERSION_1_13_0, ext_smnpm), ISA_EXT_DATA_ENTRY(smstateen, PRIV_VERSION_1_12_0, ext_smstateen), + ISA_EXT_DATA_ENTRY(smwg, PRIV_VERSION_1_12_0, ext_smwg), + ISA_EXT_DATA_ENTRY(smwgd, PRIV_VERSION_1_12_0, ext_smwgd), ISA_EXT_DATA_ENTRY(ssaia, PRIV_VERSION_1_12_0, ext_ssaia), ISA_EXT_DATA_ENTRY(ssccfg, PRIV_VERSION_1_13_0, ext_ssccfg), ISA_EXT_DATA_ENTRY(ssccptr, PRIV_VERSION_1_11_0, has_priv_1_11), @@ -222,6 +224,7 @@ const RISCVIsaExtData isa_edata_arr[] =3D { ISA_EXT_DATA_ENTRY(sstvala, PRIV_VERSION_1_12_0, has_priv_1_12), ISA_EXT_DATA_ENTRY(sstvecd, PRIV_VERSION_1_12_0, has_priv_1_12), ISA_EXT_DATA_ENTRY(ssu64xl, PRIV_VERSION_1_12_0, has_priv_1_12), + ISA_EXT_DATA_ENTRY(sswg, PRIV_VERSION_1_12_0, ext_sswg), ISA_EXT_DATA_ENTRY(supm, PRIV_VERSION_1_13_0, ext_supm), ISA_EXT_DATA_ENTRY(svade, PRIV_VERSION_1_11_0, ext_svade), ISA_EXT_DATA_ENTRY(smctr, PRIV_VERSION_1_12_0, ext_smctr), diff --git a/target/riscv/cpu_cfg_fields.h.inc b/target/riscv/cpu_cfg_field= s.h.inc index e2d116f0df..44387a8db3 100644 --- a/target/riscv/cpu_cfg_fields.h.inc +++ b/target/riscv/cpu_cfg_fields.h.inc @@ -112,6 +112,9 @@ BOOL_FIELD(ext_smnpm) BOOL_FIELD(ext_smmpm) BOOL_FIELD(ext_sspm) BOOL_FIELD(ext_supm) +BOOL_FIELD(ext_smwg) +BOOL_FIELD(ext_smwgd) +BOOL_FIELD(ext_sswg) BOOL_FIELD(rvv_ta_all_1s) BOOL_FIELD(rvv_ma_all_1s) BOOL_FIELD(rvv_vl_half_avl) diff --git a/target/riscv/tcg/tcg-cpu.c b/target/riscv/tcg/tcg-cpu.c index 1150bd1469..63e0b05f7d 100644 --- a/target/riscv/tcg/tcg-cpu.c +++ b/target/riscv/tcg/tcg-cpu.c @@ -861,6 +861,17 @@ void riscv_cpu_validate_set_extensions(RISCVCPU *cpu, = Error **errp) return; } =20 + /* RISC-V WorldGuard */ + if (cpu->cfg.ext_sswg && !cpu->cfg.ext_smwg) { + error_setg(errp, "Sswg extension requires Smwg extension"); + return; + } + + if (cpu->cfg.ext_smwgd !=3D cpu->cfg.ext_sswg) { + error_setg(errp, "Smwgd/Sswg extensions should be enabled together= "); + return; + } + /* * Disable isa extensions based on priv spec after we * validated and set everything we need. --=20 2.43.0 From nobody Fri Nov 14 17:01:12 2025 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; dmarc=pass(p=reject dis=none) header.from=sifive.com ARC-Seal: i=1; a=rsa-sha256; t=1761062301; cv=none; d=zohomail.com; s=zohoarc; b=IObMeM3pDMnpQGeDbCyXBdsXNW4NMfUYh/M8jgXrKQzOjDcax+ZQedAh6J9ivnNplThavebgu7vgL5BBiuJjSh4Fo6PA4FeZILOzQDWSAUJPkKzm/bh31UO6KPliv2Sijcph3K/7eGPjdObAHHZpSLqrS4/0qSM0VxHuG0PNwJQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1761062301; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=f5rIQfR0LdOWH1gZfogqGGKNzbK2Da+vWsJhiDZXltU=; b=CC3FBpglWUgnOFzQqgESqcejVkQ+05IRvwsWWgHQ4UaUE9Z1qELm+xitcR2LXH39wUvR07JFQLDgKTdOVu8FiYNudfYumZn+hDv1ufC2trduWNqrO6Ny3w2hFjO3sGp+KpQMwMPQJVNF4VaGzdw7QuxCwSOc3NDikOwKH1KvWkA= 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; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1761062301311156.25707816591182; Tue, 21 Oct 2025 08:58:21 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1vBEj8-00017M-Cc; Tue, 21 Oct 2025 11:56:46 -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 1vBEj5-00016O-Ux for qemu-devel@nongnu.org; Tue, 21 Oct 2025 11:56:44 -0400 Received: from mail-pj1-x1033.google.com ([2607:f8b0:4864:20::1033]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1vBEj2-0007pp-Ma for qemu-devel@nongnu.org; Tue, 21 Oct 2025 11:56:43 -0400 Received: by mail-pj1-x1033.google.com with SMTP id 98e67ed59e1d1-33db8fde85cso2402224a91.0 for ; Tue, 21 Oct 2025 08:56:40 -0700 (PDT) Received: from hsinchu26.internal.sifive.com ([210.176.154.34]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-33d5de11392sm11490177a91.7.2025.10.21.08.56.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Oct 2025 08:56:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1761062198; x=1761666998; 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=f5rIQfR0LdOWH1gZfogqGGKNzbK2Da+vWsJhiDZXltU=; b=ettL4Yk386GxmCiHsIok/lq9fRLGJyfMNTGpUdWsdZJnZt9UkXgJQj4aQ+UbCEN6Kw sfbnlcqLc8Wo3RyJx15q//eBipexm0TVrttdbXWc8BQLWqdWARFqDSHS68NzF/I/2WJD fBW7RvQN/u6W+X2jSCGORTfglLf/EFrJP5iZAmdnzUj7JhShNAkOY8hCky9q/lp8BWDD gax///jWDHlFXpehojQPZYqSkIhHVXudV6oZA2VgQSdIFYpoSzXcoJc/BRK2AKG3M+wO 7yS8HuOLTXP/P7xzV9fBxb/mYXM3IWnkZM8zWgf/KST1wxqDWefcuuR6/VpWYhn6G8eP UlKA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1761062198; x=1761666998; 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=f5rIQfR0LdOWH1gZfogqGGKNzbK2Da+vWsJhiDZXltU=; b=qiB0fP8exmcVXBJO+R3aYRjE5MHWx7J1wU7SViVq5gn/AmzsbBy28KYdbiJnUzxbb/ 4l3jBgYCEOcj5LxWEFVWmQFESGpuz2/eHLERlpMCApFw4NiBqWJNOl+Kro41CnMvoiB6 jAya4ZWvC2Ev+001JgZvaR+kigIMOSXQxisJHv/fklh+1svgBE9GB6HsFpFj1bQnY8AK 759Z8TLDyKOg7eOPX9SDQHGDO9ZFeV4NsS7FuCccQNRlniLdvpmA46XnnG43PDCWAo/9 91+37bS8pA1crGQSXEFtwSi7SpJprj60wx2iUdLctJlHodcWdK9VpFzS6f/MPSdTbxA1 Bbeg== X-Gm-Message-State: AOJu0YyeUC5m8yCgOHMCiGGPA/TGbi1BbIfepu4HqGmib+Oao1VVTCEE oqAsE4W/C7MY7okUQj+d1m38/8G9RZzVNleFfkV4WbCuXU6ZYkmdA4kJ2XjuSyYd1/M26RSjBir Xokb6JNDthQCMC+ROXC3poSGbAUHaaK38c0RMcFo5alyhaKC3TQUDNauaLndzglnlaHreDkiuQN NHrRMaWJSAjlIzDChoQLTNLgj4lPrLLzzT1t1gdQ== X-Gm-Gg: ASbGncsf7rPeukMGB2KZpyulX2xmVazAe2+VaIAZCQJsOYoua61V7LD04/r5+TUmPAw 7+1MMXTDNG5JuQt9ilzR1JSwRPT0w6XA2GWiGXQZ9R25wuhgfCxsc3B3YYF/w5FHr+4rGgqGbLV R22SyCs9GIvUP4/t6eEVYwUCxFm086zRPgOnLeB4b2spm9sMWNuGLi5Tl2cZCblQDHIUhDYOLPK 5fo9ku1q/MLEaaKgW9Z21eyV7G/80QgcyeGHcAqWGvv9O0wh86OSrU8qMrvTL/gvIPw17Y2j4nJ T6sC1BToNBSjCA5OVD33g55A2uDXVdT4vbGsyr/uKeqDEjJrsh+v0hxDizxzivUyR3ybbl5ynox cAg/CJ6Cfo2FftajecZEpz/jkyT7klAYG2KUlfKh5D7EBGfOYAc86AZJf1b7l84msFmwQS4EbIz g56oy2IZmVdqZ9LOvMiJvtvK2PhphsWWap X-Google-Smtp-Source: AGHT+IE3tDh4RrsD3PH84nIuLTMEa7pF2YbSrTFf2kInDR0bGp+RFzhwLSvuVo5UCnBak6GN00VnHA== X-Received: by 2002:a17:90b:4c92:b0:32e:32f8:bf9f with SMTP id 98e67ed59e1d1-33bcf8f9960mr20847794a91.30.1761062198330; Tue, 21 Oct 2025 08:56:38 -0700 (PDT) From: Jim Shu To: qemu-devel@nongnu.org, qemu-riscv@nongnu.org Cc: Daniel Henrique Barboza , Richard Henderson , Paolo Bonzini , Palmer Dabbelt , Alistair Francis , Weiwei Li , Liu Zhiwei , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Eduardo Habkost , Marcel Apfelbaum , Yanan Wang , Zhao Liu , Peter Xu , David Hildenbrand , Michael Rolnik , Helge Deller , Song Gao , Laurent Vivier , "Edgar E. Iglesias" , Aurelien Jarno , Jiaxun Yang , Aleksandar Rikalo , Stafford Horne , Nicholas Piggin , Chinmay Rath , Yoshinori Sato , Ilya Leoshkevich , Thomas Huth , Mark Cave-Ayland , Artyom Tarasenko , Bastian Koppelmann , Max Filippov , qemu-ppc@nongnu.org (open list:PowerPC TCG CPUs), qemu-s390x@nongnu.org (open list:S390 TCG CPUs), Jim Shu Subject: [PATCH v3 07/18] target/riscv: Add hard-coded CPU state of WG extension Date: Tue, 21 Oct 2025 23:55:37 +0800 Message-ID: <20251021155548.584543-8-jim.shu@sifive.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20251021155548.584543-1-jim.shu@sifive.com> References: <20251021155548.584543-1-jim.shu@sifive.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: pass client-ip=2607:f8b0:4864:20::1033; envelope-from=jim.shu@sifive.com; helo=mail-pj1-x1033.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable 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 @sifive.com) X-ZM-MESSAGEID: 1761062304473154100 Content-Type: text/plain; charset="utf-8" Add hard-coded state of WG extension. 'mwid' is the M-mode WID of CPU. 'mwidlist' is the list of allowed WID value of 'mlwid' CSR. These CPU states can be set by CPU option, or can be set by machine code via newly added APIs. If we want different WG configs of CPUs, we should set it by machine code. Signed-off-by: Jim Shu --- target/riscv/cpu.h | 2 ++ target/riscv/cpu_cfg_fields.h.inc | 3 +++ target/riscv/cpu_helper.c | 18 ++++++++++++++++++ 3 files changed, 23 insertions(+) diff --git a/target/riscv/cpu.h b/target/riscv/cpu.h index 4c13012442..b121e3bca9 100644 --- a/target/riscv/cpu.h +++ b/target/riscv/cpu.h @@ -654,6 +654,8 @@ void riscv_cpu_set_aia_ireg_rmw_fn(CPURISCVState *env, = uint32_t priv, void *rmw_fn_arg); =20 RISCVException smstateen_acc_ok(CPURISCVState *env, int index, uint64_t bi= t); +void riscv_cpu_set_wg_mwid(CPURISCVState *env, uint32_t mwid); +void riscv_cpu_set_wg_mwidlist(CPURISCVState *env, uint32_t mwidlist); #endif /* !CONFIG_USER_ONLY */ =20 void riscv_cpu_set_mode(CPURISCVState *env, target_ulong newpriv, bool vir= t_en); diff --git a/target/riscv/cpu_cfg_fields.h.inc b/target/riscv/cpu_cfg_field= s.h.inc index 44387a8db3..ba309d9c32 100644 --- a/target/riscv/cpu_cfg_fields.h.inc +++ b/target/riscv/cpu_cfg_fields.h.inc @@ -172,5 +172,8 @@ TYPED_FIELD(uint8_t, pmp_regions, 0) =20 TYPED_FIELD(int8_t, max_satp_mode, -1) =20 +TYPED_FIELD(uint32_t, mwid, 0) +TYPED_FIELD(uint32_t, mwidlist, 0) + #undef BOOL_FIELD #undef TYPED_FIELD diff --git a/target/riscv/cpu_helper.c b/target/riscv/cpu_helper.c index 63acd70543..d2be660d58 100644 --- a/target/riscv/cpu_helper.c +++ b/target/riscv/cpu_helper.c @@ -135,6 +135,24 @@ bool riscv_env_smode_dbltrp_enabled(CPURISCVState *env= , bool virt) #endif } =20 +#ifndef CONFIG_USER_ONLY +void riscv_cpu_set_wg_mwid(CPURISCVState *env, uint32_t mwid) +{ + CPUState *cs =3D env_cpu(env); + RISCVCPU *cpu =3D RISCV_CPU(cs); + + cpu->cfg.mwid =3D mwid; +} + +void riscv_cpu_set_wg_mwidlist(CPURISCVState *env, uint32_t mwidlist) +{ + CPUState *cs =3D env_cpu(env); + RISCVCPU *cpu =3D RISCV_CPU(cs); + + cpu->cfg.mwidlist =3D mwidlist; +} +#endif /* CONFIG_USER_ONLY */ + RISCVPmPmm riscv_pm_get_pmm(CPURISCVState *env) { #ifndef CONFIG_USER_ONLY --=20 2.43.0 From nobody Fri Nov 14 17:01:12 2025 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; dmarc=pass(p=reject dis=none) header.from=sifive.com ARC-Seal: i=1; a=rsa-sha256; t=1761063291; cv=none; d=zohomail.com; s=zohoarc; b=RgymsT6u90wkT/3A9sCBqxzd89GQnd5kcAHiBaLwfKOpVZv37i9N9G6DPY/WdrOPZyGuNFLDzyMHrqBe3fCJy8XPTHyECGyIfojjUuHov/FhEIWQksc0/835Sur8PnTiD33ygH+1JrTHuRgj2E3oKmKAaZn61GHJ7P+Szd+LRYY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1761063291; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=4Y13V29TyEhcjAwL5nFeY1ayHUoKW0GaN/z1B8E1GJk=; b=AMOOAcByu3YCyu6Nt/kNhRfAFcxKHuXOetpovnBUcoXBQxRZIYdrxeMN1kvPYlDohLS1fXHlhUykTr4gJ0zQFREbsvFz6blpZ+KVzVdyKweeHrZVHZV0xsWFoq2WDWQ1HeWgVOHx47MhxwabTFFk2TXgpxgUatO5NoDpPUYpzk8= 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; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1761063291034917.4480888144433; Tue, 21 Oct 2025 09:14:51 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1vBEzg-0004GT-DV; Tue, 21 Oct 2025 12:13:52 -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 1vBEzW-0004D7-UX for qemu-devel@nongnu.org; Tue, 21 Oct 2025 12:13:45 -0400 Received: from mail-pf1-x430.google.com ([2607:f8b0:4864:20::430]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1vBEzU-0001aV-Np for qemu-devel@nongnu.org; Tue, 21 Oct 2025 12:13:42 -0400 Received: by mail-pf1-x430.google.com with SMTP id d2e1a72fcca58-7800ff158d5so5214175b3a.1 for ; Tue, 21 Oct 2025 09:13:36 -0700 (PDT) Received: from hsinchu26.internal.sifive.com ([210.176.154.34]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7a23010f7c7sm11731157b3a.54.2025.10.21.09.13.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Oct 2025 09:13:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1761063215; x=1761668015; 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=4Y13V29TyEhcjAwL5nFeY1ayHUoKW0GaN/z1B8E1GJk=; b=AklSlcxxHGTWnz6+yiR5QYj8IneBSb5AiHH49OkNZCmAx48cyuCkP1Xf5KH1vhkTrI pE8RBP3qy05XpkNxbk+70UmOqTqknuIx+45nwBj3hXFlx+//Fz+5U2DsBldenOx/yuP3 hpCaaPhXi/Yizn4cduifL1lD51kLMxnyeL1FL83aJ3N+GDUQERi5KG9A7d/267BdwLXG VXVNWv6/5UwJ3+k4EBTpc38M9jH+acdyhW+1ZsPD24QoGcX5MM/scb14v/x34pi23Vtl O6EnO1bAcVWW0YCgOGwrPjy1RFayJU2zYAWg4SsfIVuZ9GE3tNt3pV6FCUqKScMcSmAS t0zw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1761063215; x=1761668015; 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=4Y13V29TyEhcjAwL5nFeY1ayHUoKW0GaN/z1B8E1GJk=; b=vOq71aMiJBaYO9PUcVr1zrW3P7yEXq/SoTfoXkLvApZ1G8GFG1urH6xDX5pcOABITV 5QCjz1jVNOCX6YfDlkKShIQjxJYYFwtCUJSJZIxo4UHTFF1TWqSZvbk2Qa7nZmUT9Zns NApFakNRrPTXpq66L7fegQTzuEu/Dzzw0PAKhBYCAohclNDTDRx/quPOEKl4sd0HoaZL uw5k2rQn+rzNfFe0JCBxUFOeKp/7ms+OrBb+Fk5eNVt8QjvVPPt+q0ySH/Csgg48aeLN V3W05E9AeiJURfy0MsmnsAMVCE5LHAfp2g7bBYQnbyqv1yvtCYysZSdpqVUe517WwwoK A0YA== X-Gm-Message-State: AOJu0YyDs/7fgSClZlAmBkga0wg7uro48FVO6Oha2b7x+m8BsYWCoPky lW6//AD79gIG39vh1dfrS58cEpQMQ3GWFm1KEGTtd5QeeT6wB2vwZgDBJ5UWv/Oz4cmnuUrWHOG TJCOwxYI/lvcoIynHHh4ygV40sCDfUoCTkwrkWu4M+lpTvDDwn4pOaTbdakaf3zmFh9gqoDE+0Y 7L2g0wavgB4awgfEFQbqNknvLZ0MBxhkWigEuVqA== X-Gm-Gg: ASbGnctRx3Tzmei4227eMk55D+IL1VXiavdIEZuXbenE40L9fSDVzbb/A38aqAbz2/c o90HKJ1AFJPb4y2g2veoYNbQtbHvTBfH4FyHBI2PrmiFxlr+T7BwYwL+O275t/dVbSFv7RNizmO 65i1mn5hBWOcwHHepzhXUF+t6NlAv9p/JsELOn8nYmJNX1Xn312kZImqGp1fe6lRL7NOSoIYQsx VPbmi4qGpNiznKxgzgKbb6wAZbGAJx5OE7P11oeNQXmJZ8xgaQkeFG636d145IymWOrfAWebtOO SL+ishF9zkugatSs+zs2rDAcAjU5hdtkwOVNkUL/gsfMx5gKTWtwXnkQ5chzUXUJbbCQdmQ9aCj mY6hUOT2dk12wg7q6Bx9oMHExYju5N8Y4aHb2N6Wae7MyCl+APTQU3rXP1VIjcv+Zv7VmRPXKiM CJYn/EMKZ3rtlvt4rWs7/OGg== X-Google-Smtp-Source: AGHT+IESq+Wree+qpeUxnR7Xwrl5wv4lQYZ/VsJBAAZNIYY2fKNL9WpYZZTIkByfOojT6r4EGL3dvw== X-Received: by 2002:a05:6a00:94c2:b0:79a:f5eb:3dce with SMTP id d2e1a72fcca58-7a220d5792amr20550258b3a.31.1761063214459; Tue, 21 Oct 2025 09:13:34 -0700 (PDT) From: Jim Shu To: qemu-devel@nongnu.org, qemu-riscv@nongnu.org Cc: Daniel Henrique Barboza , Richard Henderson , Paolo Bonzini , Palmer Dabbelt , Alistair Francis , Weiwei Li , Liu Zhiwei , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Eduardo Habkost , Marcel Apfelbaum , Yanan Wang , Zhao Liu , Peter Xu , David Hildenbrand , Michael Rolnik , Helge Deller , Song Gao , Laurent Vivier , "Edgar E. Iglesias" , Aurelien Jarno , Jiaxun Yang , Aleksandar Rikalo , Stafford Horne , Nicholas Piggin , Chinmay Rath , Yoshinori Sato , Ilya Leoshkevich , Thomas Huth , Mark Cave-Ayland , Artyom Tarasenko , Bastian Koppelmann , Max Filippov , qemu-ppc@nongnu.org (open list:PowerPC TCG CPUs), qemu-s390x@nongnu.org (open list:S390 TCG CPUs), Jim Shu Subject: [PATCH v3 08/18] target/riscv: Add defines for WorldGuard CSRs Date: Wed, 22 Oct 2025 00:13:14 +0800 Message-ID: <20251021161325.585278-1-jim.shu@sifive.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20251021155548.584543-1-jim.shu@sifive.com> References: <20251021155548.584543-1-jim.shu@sifive.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: pass client-ip=2607:f8b0:4864:20::430; envelope-from=jim.shu@sifive.com; helo=mail-pf1-x430.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable 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 @sifive.com) X-ZM-MESSAGEID: 1761063291909154100 Content-Type: text/plain; charset="utf-8" Add CSRs for 3 WG extensions: Smwg, Smwgd, and Sswg. Signed-off-by: Jim Shu Reviewed-by: Daniel Henrique Barboza --- target/riscv/cpu_bits.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/target/riscv/cpu_bits.h b/target/riscv/cpu_bits.h index b62dd82fe7..a546545ff3 100644 --- a/target/riscv/cpu_bits.h +++ b/target/riscv/cpu_bits.h @@ -468,6 +468,11 @@ #define CSR_DPC 0x7b1 #define CSR_DSCRATCH 0x7b2 =20 +/* RISC-V WorldGuard */ +#define CSR_MLWID 0x390 +#define CSR_SLWID 0x190 +#define CSR_MWIDDELEG 0x748 + /* Performance Counters */ #define CSR_MHPMCOUNTER3 0xb03 #define CSR_MHPMCOUNTER4 0xb04 --=20 2.43.0 From nobody Fri Nov 14 17:01:12 2025 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; dmarc=pass(p=reject dis=none) header.from=sifive.com ARC-Seal: i=1; a=rsa-sha256; t=1761063410; cv=none; d=zohomail.com; s=zohoarc; b=grNXqNM5fFkg6AUGBora92duIM6CEhnoQqfMOact4qevwoxrSiswOG4xEvuaJO0yjqMjrioYb7ywi07g0hJXM1hohWjhg3Mu8TFonhz6yv/l1YuaMkfC0oDzeU9vIr+1ixAZTA1w8R1LPLoS9V9PLspBb8jkEC+HMrZ9CKCp37Y= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1761063410; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=27TC79UHh7APf1kYdEu3SyfuDzXTlSkrNQqbdUNBRwU=; b=DRNKnO3vcPovBhak+3xnFoZdMbvC2vXbBPfkGokvy2njhM3efPIPE0pPtIYKDKuHxtJ6dEBLlENwfxidhuWI3npRdCV8CvqFNDa2r/QEnPQBynLk6+C9ThoDd6aYHIkXpxIVhKLheXCY0eZ6lafg0etY3wVyrGlfduyJq529ilg= 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; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1761063410761327.3465216123924; Tue, 21 Oct 2025 09:16:50 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1vBEzh-0004Ik-Vb; Tue, 21 Oct 2025 12:13:54 -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 1vBEzd-0004ES-NJ for qemu-devel@nongnu.org; Tue, 21 Oct 2025 12:13:51 -0400 Received: from mail-pf1-x42a.google.com ([2607:f8b0:4864:20::42a]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1vBEzW-0001at-OA for qemu-devel@nongnu.org; Tue, 21 Oct 2025 12:13:48 -0400 Received: by mail-pf1-x42a.google.com with SMTP id d2e1a72fcca58-77f343231fcso3979379b3a.3 for ; Tue, 21 Oct 2025 09:13:42 -0700 (PDT) Received: from hsinchu26.internal.sifive.com ([210.176.154.34]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7a23010f7c7sm11731157b3a.54.2025.10.21.09.13.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Oct 2025 09:13:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1761063220; x=1761668020; 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=27TC79UHh7APf1kYdEu3SyfuDzXTlSkrNQqbdUNBRwU=; b=EFv3JipMcTxiZuEB7b541OD7aenJEphRSygWyuDqyjnLJvaPT9MQWevA/J6camucTb gpwjl0lju3tIDleq9f6/7IT8heL+js2g2FXowc7Je7df2N09e7xG+qfAxGAOl4Cz3Gxk mfqFaBE7CCoSXA3HFpofh9V1hVv/maeP1HEYfgKHoZ310BIyVwudumhvOrMyBsqzgnvR XD7zxVGq4RE/hC3YgAKA8Cp0fftgmE2xDZxJKQaq28WdZwfpXnMKDslhMYHcdV+PGc/W HloS/CfL1+B+5MAnR5WfaiTcoP0kWlvqG2MiMcH0emf2IUZXimsy5kaSxibnHgIUPL2Z vUEQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1761063220; x=1761668020; 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=27TC79UHh7APf1kYdEu3SyfuDzXTlSkrNQqbdUNBRwU=; b=FzVJ853y+1ZZtydhCNr8yDJm34VtBF6Lei6nnMtpybM9VCt4BRVML7VKBZst2/qDLA U8gyqxPCvbuVrYL6lQKtytDvcKk8l39A7ZpAqXnkstNDuknEkWadIAgpcHaUrkH8xn0B 9M9Y0y4xBoJur9iCC9ibyZ46PX2FYKHDbHgv7hIE2nZm/K84b/N0+IKo+xJ+KQLZWVnr BoQMHR8setJd7Y7aX9tciJTQcI00d/F6uAjUtO03dAEt8mjZlXCUiNIEGv9o/kll/lH/ guBG7Qo2hYVmedS9Jtwqkr/4t2Ceo1ElpaZH1Ke1o1lnM1oKjOTKALWEzcuP+P3+M61o qceA== X-Gm-Message-State: AOJu0YyhmiFJVUSvTKTmTx5dd+FNGy5kAzUNhSApgflYHgzehlLBUQu9 WmcxzLSusEznDbwiTVXg/OoRNFm1hk7OxNYWaECx74lmWoD56+jznl/fIxY7z8V+JOF/+P/qYed KLFyBWaeZqZs9XuvV7mFV79g5e80eTsaEvBXTnd6JvrtUlilnWg0t9ucDLBUkEKTN+yIXtWsVFW GG5qnlacxwHAc0rRPnd1vZQjlX3eVy3D/IWGD/HA== X-Gm-Gg: ASbGncuIf8l1Uf4scAr5TwRwZG4N4MZF8gFTgOfJ7UiB22aNdNhxR6gX1JjW34a+WWc yfvCp77twkhbjB07mf3wywpInQH9eNoLKiRcZxl0Rm+iMRCI0BMVlrVQ87ZcaPbauVd6VeFWydo jxYdqdHUQvcKRzaQnAv0Sa9zGCkixY7lWbCuf0CsmvfLHjg1qyFvw2plEvZ9KwfCZx2lqi4G8Fe 92elBnct1Kza92ubRH8TBBt1vbXHHq9E0spQqocuPAq2trRop0BsqBrekHLUTU6tuWr2KhT476E w40SELs47K2dmb9yBMSGQE47LnisTmZxBU7puP+Oo3BC1j3Bfqj9RuD486Xx7q6KOEXlhNlcpcW x0UDv09klEav/BJ/SXl5x34bpPG7LnljV5bVqtTQi1JuJqxP2ha04wLkKaB/8ELXYi8Z3NC4+D5 8UW6wll9+pEBMzihKnI0WJjJDwGWQlloZt X-Google-Smtp-Source: AGHT+IEvKEZ/oorCPRUE3+2drUtMz/WjilkHhFd+68pw51VUwa/jNnxVq8UgoGwKNSE66uhNYDRydA== X-Received: by 2002:a05:6a00:4b0f:b0:777:797b:7318 with SMTP id d2e1a72fcca58-7a220a5e685mr19877401b3a.7.1761063220131; Tue, 21 Oct 2025 09:13:40 -0700 (PDT) From: Jim Shu To: qemu-devel@nongnu.org, qemu-riscv@nongnu.org Cc: Daniel Henrique Barboza , Richard Henderson , Paolo Bonzini , Palmer Dabbelt , Alistair Francis , Weiwei Li , Liu Zhiwei , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Eduardo Habkost , Marcel Apfelbaum , Yanan Wang , Zhao Liu , Peter Xu , David Hildenbrand , Michael Rolnik , Helge Deller , Song Gao , Laurent Vivier , "Edgar E. Iglesias" , Aurelien Jarno , Jiaxun Yang , Aleksandar Rikalo , Stafford Horne , Nicholas Piggin , Chinmay Rath , Yoshinori Sato , Ilya Leoshkevich , Thomas Huth , Mark Cave-Ayland , Artyom Tarasenko , Bastian Koppelmann , Max Filippov , qemu-ppc@nongnu.org (open list:PowerPC TCG CPUs), qemu-s390x@nongnu.org (open list:S390 TCG CPUs), Jim Shu Subject: [PATCH v3 09/18] target/riscv: Allow global WG config to set WG CPU callbacks Date: Wed, 22 Oct 2025 00:13:15 +0800 Message-ID: <20251021161325.585278-2-jim.shu@sifive.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20251021155548.584543-1-jim.shu@sifive.com> References: <20251021155548.584543-1-jim.shu@sifive.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: pass client-ip=2607:f8b0:4864:20::42a; envelope-from=jim.shu@sifive.com; helo=mail-pf1-x42a.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable 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 @sifive.com) X-ZM-MESSAGEID: 1761063414467158500 Content-Type: text/plain; charset="utf-8" Some WG CPU functions depend on global WG config (like num-of-world), so we let the global WG config device to set callbacks of a RISC-V HART. Signed-off-by: Jim Shu Reviewed-by: Daniel Henrique Barboza --- target/riscv/cpu.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/target/riscv/cpu.h b/target/riscv/cpu.h index b121e3bca9..8e18b5f13e 100644 --- a/target/riscv/cpu.h +++ b/target/riscv/cpu.h @@ -513,6 +513,10 @@ struct CPUArchState { target_ulong rnmip; uint64_t rnmi_irqvec; uint64_t rnmi_excpvec; + + /* machine specific WorldGuard callback */ + void (*wg_reset)(CPURISCVState *env); + void (*wid_to_mem_attrs)(MemTxAttrs *attrs, uint32_t wid); }; =20 /* --=20 2.43.0 From nobody Fri Nov 14 17:01:12 2025 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; dmarc=pass(p=reject dis=none) header.from=sifive.com ARC-Seal: i=1; a=rsa-sha256; t=1761063460; cv=none; d=zohomail.com; s=zohoarc; b=WAaWmk46Ed4WmKNaMNPHeL6lFYIeNTIP4XOTrmJhJsD5Mk5axmOCwwnWNi6MfghrCRt9pT5+p3RabOu7s1VWEVIZYgLbRr/OVIJ0PqL8KQtnXo8fbuBKvobXmRZ1XXUEeYLsd8SN8pZdJdkd1EGRB/dIPMgprGzHjkfZvNS38MY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1761063460; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=IPid8O/40LFlUrPqK0SELP2UT4eulj7CVPfrAr1p+ws=; b=L+GvRqihyNYCLNo6Sukxf6nl5A5SMKT5wV9O87mf5k0zmIjp87Ywz+bo4sysMDeJmK1yrvurUZNfbkJN54AthLcu7gd2M4a82mJjWh1cBk7pcCPfNqUdjXPHDSZAk8C2nGUm7lvVN1r2mrb1FlzUcP0cGA+G5Uc4C/pr2BUMlGc= 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; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1761063459973130.67737360538945; Tue, 21 Oct 2025 09:17:39 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1vBEzj-0004Je-8a; Tue, 21 Oct 2025 12:13:55 -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 1vBEzh-0004IR-CG for qemu-devel@nongnu.org; Tue, 21 Oct 2025 12:13:53 -0400 Received: from mail-pf1-x430.google.com ([2607:f8b0:4864:20::430]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1vBEzd-0001bi-H7 for qemu-devel@nongnu.org; Tue, 21 Oct 2025 12:13:53 -0400 Received: by mail-pf1-x430.google.com with SMTP id d2e1a72fcca58-7a1603a098eso3602107b3a.1 for ; Tue, 21 Oct 2025 09:13:47 -0700 (PDT) Received: from hsinchu26.internal.sifive.com ([210.176.154.34]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7a23010f7c7sm11731157b3a.54.2025.10.21.09.13.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Oct 2025 09:13:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1761063226; x=1761668026; 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=IPid8O/40LFlUrPqK0SELP2UT4eulj7CVPfrAr1p+ws=; b=WOJUrlxyQXIn+XM+I1rhxKaCMBtpB4mHqTguBc55vaP1rPUJnMWyC9yT1IEuBVsKYu zgQeTVgFwDuYIzHpgoKHA76TC0myWFqB6YorHJLEx9sXa60IWjzOrot3bD1y+DYYnfpJ edfDAT1vCu+fqzPI/lyBvlYJkVf9Tsm7cvDFbMExENJ3DoJmWzofPCzar+E2WXAJqjwU swLcj7xJ7mkN4YAcpnkZyRULi/I4GMQ5G4fWI+tKwwkVqtpXw1q8JznhuxYe5kAv3dSl UWq5PKpV5zZU+h79hr4pGw2eENGlhpoXUm38GMbUoBOSuHVfKB7NuG9rV89J/OM0xt/r pdAw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1761063226; x=1761668026; 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=IPid8O/40LFlUrPqK0SELP2UT4eulj7CVPfrAr1p+ws=; b=Tw8ZXa+LIcA07M9sRrrVW6iLUVHsVQVCwzc8VSLb4oU1jo6WIEao0OMC9GXbu3nCT5 MUxC1B0vQIA0i3s6sVG0w9pI9rhElYo3OtlCii5uAf6IUaVqNtbV37uo4H6AFb5s7kcd aQIoIN1KI95doYEZRNLwk+o/eq9nZuSBS7eva3gMOgVy8rfTERJgSv3mNZYU8n8Ymu0U g33kGVx2vGsevKFQ9oLnzn64Jx5z3CfCfJJzAmKF3+8rv1PFPDDi9eqBMH+vTI8up65T yl641us/PZf1xTWjDVsiVgGbUbuVv1WxiLSvZChmvJRZzxlfSNpCzr1SQgcrHyPi1DdH 84QQ== X-Gm-Message-State: AOJu0YxwEXDZk2LibEx4MXoD24hiMuGUkTbF1ve5bAE1qnYCjOKtOVl5 a8drjoUqhLo1Pu3W1lj4kgnp2/DuQwYUBRCfeDjM6RNva5pyThVV9A9aS2ng5mPXT/ssyAu0AuT Qry/wY3gCfFQyPCSix9q5oc0xKndR4weo1ZMMwPsqUu5+IN+5ptHZlnnnrawh6X5N+WK+4VrrZZ eaP0kcYf3mU3p4i+jCQPUsvf8V6Zvekk2yom0UzQ== X-Gm-Gg: ASbGncv5LnIbamUOtcrbYPCZ8G1h9oDWSiO9hxfInM9oLCM9GX468iLlpYrvnyXLm6/ Y6W2G1zq/XOa3bhcG/nvhFsfMVHL4e88QF0aO1KxeTzsH168jMs3d/my1c28iKingUOqB39IKs3 qSkxyhVcZ+cA2zviFoP+DoJXhcrBmYZ/HLVS2+5d9KsmnGDdYbO6b8kLqOm1U8CQ9x63uMTuUcb CSkf+syDB78FHBlh/O//P8yCZdYbLMF4jep5+PmzGCfzH6jBQkPjQN6D+yUME26aii4D12kayUr xG3CWLjJhLIjnb0sVwf9s2M0LDD7fJPu8Wk7fRSTKc2bSh7xSwwHKc8/ES63Oeehu+Ylw8BPRtF XXk0dkbWULvTOHHLwdBPKg/crt/DSLcQ35cbxLuulIx28dqRZnII5Vh/4eSYHqxchO0vT1kLpDM rCf7CEH+ciyHGPCR/xme1dTg== X-Google-Smtp-Source: AGHT+IHRpNcOULa6siOygj7bHDL86wtI9uZwhVGxYVuwKJq4fP0bxUJjSmKp+5DHkiIWGu4RHtkblw== X-Received: by 2002:a05:6a00:886:b0:77d:b0cf:ca14 with SMTP id d2e1a72fcca58-7a220af0525mr20092587b3a.22.1761063225839; Tue, 21 Oct 2025 09:13:45 -0700 (PDT) From: Jim Shu To: qemu-devel@nongnu.org, qemu-riscv@nongnu.org Cc: Daniel Henrique Barboza , Richard Henderson , Paolo Bonzini , Palmer Dabbelt , Alistair Francis , Weiwei Li , Liu Zhiwei , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Eduardo Habkost , Marcel Apfelbaum , Yanan Wang , Zhao Liu , Peter Xu , David Hildenbrand , Michael Rolnik , Helge Deller , Song Gao , Laurent Vivier , "Edgar E. Iglesias" , Aurelien Jarno , Jiaxun Yang , Aleksandar Rikalo , Stafford Horne , Nicholas Piggin , Chinmay Rath , Yoshinori Sato , Ilya Leoshkevich , Thomas Huth , Mark Cave-Ayland , Artyom Tarasenko , Bastian Koppelmann , Max Filippov , qemu-ppc@nongnu.org (open list:PowerPC TCG CPUs), qemu-s390x@nongnu.org (open list:S390 TCG CPUs), Jim Shu Subject: [PATCH v3 10/18] target/riscv: Implement WorldGuard CSRs Date: Wed, 22 Oct 2025 00:13:16 +0800 Message-ID: <20251021161325.585278-3-jim.shu@sifive.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20251021155548.584543-1-jim.shu@sifive.com> References: <20251021155548.584543-1-jim.shu@sifive.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: pass client-ip=2607:f8b0:4864:20::430; envelope-from=jim.shu@sifive.com; helo=mail-pf1-x430.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable 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 @sifive.com) X-ZM-MESSAGEID: 1761063463007158500 Content-Type: text/plain; charset="utf-8" The WG v0.4 specification adds 3 CSRs to configure S/U/HS/VS-mode WIDs of CPUs in the higher privileged modes. The Smwg extension at least requires a RISC-V HART to have M/U-mode, and the Sswg/Smwgd extension at least requires a RISC-V HART to have M/S/U-mode. Signed-off-by: Jim Shu Reviewed-by: Daniel Henrique Barboza --- target/riscv/cpu.c | 4 ++ target/riscv/cpu.h | 5 +++ target/riscv/csr.c | 107 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 116 insertions(+) diff --git a/target/riscv/cpu.c b/target/riscv/cpu.c index 03946439f2..22b4070476 100644 --- a/target/riscv/cpu.c +++ b/target/riscv/cpu.c @@ -795,6 +795,10 @@ static void riscv_cpu_reset_hold(Object *obj, ResetTyp= e type) env->mnstatus =3D set_field(env->mnstatus, MNSTATUS_NMIE, false); } =20 + if (riscv_cpu_cfg(env)->ext_smwg && env->wg_reset) { + env->wg_reset(env); + } + if (kvm_enabled()) { kvm_riscv_reset_vcpu(cpu); } diff --git a/target/riscv/cpu.h b/target/riscv/cpu.h index 8e18b5f13e..e99aa11140 100644 --- a/target/riscv/cpu.h +++ b/target/riscv/cpu.h @@ -514,6 +514,11 @@ struct CPUArchState { uint64_t rnmi_irqvec; uint64_t rnmi_excpvec; =20 + /* RISC-V WorldGuard */ + target_ulong mlwid; + target_ulong slwid; + target_ulong mwiddeleg; + /* machine specific WorldGuard callback */ void (*wg_reset)(CPURISCVState *env); void (*wid_to_mem_attrs)(MemTxAttrs *attrs, uint32_t wid); diff --git a/target/riscv/csr.c b/target/riscv/csr.c index 5c91658c3d..be62a30953 100644 --- a/target/riscv/csr.c +++ b/target/riscv/csr.c @@ -5470,6 +5470,109 @@ static RISCVException write_mnstatus(CPURISCVState = *env, int csrno, return RISCV_EXCP_NONE; } =20 +/* RISC-V Worldguard */ +static RISCVException worldguard_umode(CPURISCVState *env, int csrno) +{ + if (!riscv_cpu_cfg(env)->ext_smwg) { + return RISCV_EXCP_ILLEGAL_INST; + } + + return umode(env, csrno); +} + +static RISCVException worldguard_sumode(CPURISCVState *env, int csrno) +{ + RISCVException ret; + + if (!riscv_cpu_cfg(env)->ext_sswg) { + return RISCV_EXCP_ILLEGAL_INST; + } + + ret =3D smode(env, csrno); + + if (ret !=3D RISCV_EXCP_NONE) { + return ret; + } + + return umode(env, csrno); +} + +static RISCVException rmw_mlwid(CPURISCVState *env, int csrno, + target_ulong *ret_val, + target_ulong new_val, target_ulong wr_mask) +{ + CPUState *cs =3D env_cpu(env); + RISCVCPU *cpu =3D RISCV_CPU(cs); + target_ulong new_mlwid =3D (env->mlwid & ~wr_mask) | (new_val & wr_mas= k); + + if (ret_val) { + *ret_val =3D env->mlwid; + } + + g_assert(cpu->cfg.mwidlist); + if (!(BIT(new_mlwid) & cpu->cfg.mwidlist)) { + /* Set WID to lowest legal value if writing illegal value (WARL) */ + new_mlwid =3D find_first_bit((unsigned long *)&cpu->cfg.mwidlist, = 32); + } + + if (env->mlwid !=3D new_mlwid) { + env->mlwid =3D new_mlwid; + tlb_flush(cs); + } + + return RISCV_EXCP_NONE; +} + +static RISCVException rmw_slwid(CPURISCVState *env, int csrno, + target_ulong *ret_val, + target_ulong new_val, target_ulong wr_mask) +{ + target_ulong new_slwid =3D (env->slwid & ~wr_mask) | (new_val & wr_mas= k); + + if (!env->mwiddeleg) { + /* + * When mwiddeleg CSR is zero, access to slwid raises an illegal + * instruction exception. + */ + return RISCV_EXCP_ILLEGAL_INST; + } + + if (ret_val) { + *ret_val =3D env->slwid; + } + + if (!(BIT(new_slwid) & env->mwiddeleg)) { + /* Set WID to lowest legal value if writing illegal value (WARL) */ + new_slwid =3D find_first_bit( + (unsigned long *)&env->mwiddeleg, TARGET_LONG_BITS); + } + + if (env->slwid !=3D new_slwid) { + env->slwid =3D new_slwid; + tlb_flush(env_cpu(env)); + } + + return RISCV_EXCP_NONE; +} + +static RISCVException rmw_mwiddeleg(CPURISCVState *env, int csrno, + target_ulong *ret_val, + target_ulong new_val, target_ulong wr_= mask) +{ + CPUState *cs =3D env_cpu(env); + RISCVCPU *cpu =3D RISCV_CPU(cs); + + if (ret_val) { + *ret_val =3D env->mwiddeleg; + } + + env->mwiddeleg =3D (env->mwiddeleg & ~wr_mask) | (new_val & wr_mask); + + /* Core wgMarker can only have WID value in mwidlist. */ + env->mwiddeleg &=3D cpu->cfg.mwidlist; + + return RISCV_EXCP_NONE; +} #endif =20 /* Crypto Extension */ @@ -6667,5 +6770,9 @@ riscv_csr_operations csr_ops[CSR_TABLE_SIZE] =3D { [CSR_SCOUNTOVF] =3D { "scountovf", sscofpmf, read_scountovf, .min_priv_ver =3D PRIV_VERSION_1_12_0 }, =20 + /* RISC-V WorldGuard */ + [CSR_MLWID] =3D { "mlwid", worldguard_umode, NULL, NULL, rmw_= mlwid }, + [CSR_SLWID] =3D { "slwid", worldguard_sumode, NULL, NULL, rmw_= slwid }, + [CSR_MWIDDELEG] =3D { "mwiddeleg", worldguard_sumode, NULL, NULL, rmw_= mwiddeleg }, #endif /* !CONFIG_USER_ONLY */ }; --=20 2.43.0 From nobody Fri Nov 14 17:01:12 2025 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; dmarc=pass(p=reject dis=none) header.from=sifive.com ARC-Seal: i=1; a=rsa-sha256; t=1761063276; cv=none; d=zohomail.com; s=zohoarc; b=ZHp6xd7+3A7dGGftYvg/qfZQzjlLtt02hmjwtyW2SsDquUbFbEjuWDDY+W8dCIYfpmNwihhLUyEn5mgjGy4UyempOQulXEXoJco89K2187/JLVVok7BuqD6SNZcrAdtzV22lFfIB6HNpgZsgWxs/naG6wbUJccyiU87eR4xAjyU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1761063276; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=KBqwCM6wM6yghlxF77/Wnstb6LPR3AQsGDjym+XGAI8=; b=NQ2px/5h0g6/ndKCWrKCfq3VjMhJtYlPQOUynMGtL7h0vQ7/xq3xJX++wiP5ECPxEEYflmGITDHSItxitq3FbNSypjlieRjhaS2tEGjzq/grt0md9FQlLlgxKgvwXX6Jr8i/V865j+8zBd6wASPgC+Pqs0jNXuKumXW73c5zbGs= 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; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1761063276211679.9032397068478; Tue, 21 Oct 2025 09:14:36 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1vBEzl-0004Lj-Vb; Tue, 21 Oct 2025 12:13:58 -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 1vBEzk-0004KJ-AH for qemu-devel@nongnu.org; Tue, 21 Oct 2025 12:13:56 -0400 Received: from mail-pf1-x42a.google.com ([2607:f8b0:4864:20::42a]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1vBEzi-0001cT-8A for qemu-devel@nongnu.org; Tue, 21 Oct 2025 12:13:56 -0400 Received: by mail-pf1-x42a.google.com with SMTP id d2e1a72fcca58-7930132f59aso7741338b3a.0 for ; Tue, 21 Oct 2025 09:13:53 -0700 (PDT) Received: from hsinchu26.internal.sifive.com ([210.176.154.34]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7a23010f7c7sm11731157b3a.54.2025.10.21.09.13.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Oct 2025 09:13:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1761063232; x=1761668032; 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=KBqwCM6wM6yghlxF77/Wnstb6LPR3AQsGDjym+XGAI8=; b=SnTBS1ji8aNOO09GrgeqvewVZOaK8gKKUWxXq81xfOK6zg/K3CaxAFjUtQuO7Y1xBd UJtnCZazcoPkjMjkcNEt9erm0rAg0SD4InIdif6UqvnP2I2hLOer2SxnaI1uhM0gM1z6 mib0BrNQBxNYGwudTMMaGEfWIbbFGxnXg7+NTRRuL93MkTU5vl3xMgo4mw7qAeqFjiTn /LSLNp8fg195pr6wH3XVPbgwuCLePWq7Jj1s+1xBnWihE2d8hRvKRj+OqJwRDvcG6byS gQDPvcHquJsBX/TPnSnk1J21RUGMYHeDhnsS1TvTT0+LgQvUSodu9O3XEy7ElJx4URc6 Pc2A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1761063232; x=1761668032; 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=KBqwCM6wM6yghlxF77/Wnstb6LPR3AQsGDjym+XGAI8=; b=mKLudReeIZBaxWshAT5wcideSOyn5tg66ywRLrZ+nRMt+y4jDUFHKUTsVevpnXXMYt Oj32fBm1zIqRxP/Nv3VzD1WErcVEs29SGFEGhr6OcrgUyJMqpxgmxo6y/nACBw/apN6l QpcY7fKW1RDB4nDURqt8OMyU/GUWmxDQiJi8m8owbPWQQUezRYEALKDlVFky7myD746V hXD838m6i+HYb3CvIBL9CdclUATKJyd2V5si7M+SC1AXBztJQdGoJ2mk7o7+NHgVOySA CHzCw775X45cGvrcspy36SqkJv3CfAlXXwRZ0j7okS+Hr4DHRnAEM8FJz020uVQjjFsB c8QQ== X-Gm-Message-State: AOJu0YyPRkoCWk/7kJ60RcjZL0P/rvZpTa7pLkPoFctAyqiQl70Qb65J +kXsv0rM2y02bPx4m4ceLbD0eijlgOrq1QXZj3ePkxRQhcbtAFcX/F8FhtNrYzkWoPfey1IwpNR 5j3duqKM4XidOuBKnnCVuRIcuYn0F1KoWuifXyfykWEAvMxr87hM14WW1Yib5j4r8MDs2RzsVH+ I8Xy1N6u6FafVK9kKxj3ckiK03KYjMQ+OEHYaJzg== X-Gm-Gg: ASbGncvwe8gxnbsdDB3O3qOSf6prNAohrrs3rfVt1hicYqeVn8FW3G90qzMJ51tOYL1 3EVmwP+bAcizU2dAVBXsFxv9d8XLS0IkzKHGg/Gnw8O9Tp7agXkS0vdY22W7WsQakkt0f8uIUq/ 7mylrxrOw+7UMV+J0DEBqQydabigYkQsZIuKeizpUSbq8nFkjLaG6V2qnLWnZTRC9H+/Xxje6zh eLRFfUiWc7wGcqny6LL9zBfL77ck6kXy1szRIbICoQq5j5BOcmj1Rnch8xv3HICvpA/jzzfxzlY QV7buIs3hE75af81KBB9Rzxiyn/eOukbOShJbQ6h7252EQvbZpCsyqwSITbMgxgH3YufpEqqVFw z1AbkbKCjyHsO403/hn58ZxQ8vpZ4fDC0ZX/qGt01ECQ0Mw3p2PczzTSzO2v0vD+nHXwmV9jGdz AqiDue64jj7ZnNjS2j+ha1Jg== X-Google-Smtp-Source: AGHT+IFQhd6ho7BqlWu0WB0x8T6aRoHrh9WS3PUXQqbREbT20oiydj94YL7jqKF36KXQ2ehaCmreTw== X-Received: by 2002:a05:6a00:1894:b0:781:1dc6:4e69 with SMTP id d2e1a72fcca58-7a220aabb68mr22685131b3a.14.1761063231605; Tue, 21 Oct 2025 09:13:51 -0700 (PDT) From: Jim Shu To: qemu-devel@nongnu.org, qemu-riscv@nongnu.org Cc: Daniel Henrique Barboza , Richard Henderson , Paolo Bonzini , Palmer Dabbelt , Alistair Francis , Weiwei Li , Liu Zhiwei , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Eduardo Habkost , Marcel Apfelbaum , Yanan Wang , Zhao Liu , Peter Xu , David Hildenbrand , Michael Rolnik , Helge Deller , Song Gao , Laurent Vivier , "Edgar E. Iglesias" , Aurelien Jarno , Jiaxun Yang , Aleksandar Rikalo , Stafford Horne , Nicholas Piggin , Chinmay Rath , Yoshinori Sato , Ilya Leoshkevich , Thomas Huth , Mark Cave-Ayland , Artyom Tarasenko , Bastian Koppelmann , Max Filippov , qemu-ppc@nongnu.org (open list:PowerPC TCG CPUs), qemu-s390x@nongnu.org (open list:S390 TCG CPUs), Jim Shu Subject: [PATCH v3 11/18] target/riscv: Add WID to MemTxAttrs of CPU memory transactions Date: Wed, 22 Oct 2025 00:13:17 +0800 Message-ID: <20251021161325.585278-4-jim.shu@sifive.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20251021155548.584543-1-jim.shu@sifive.com> References: <20251021155548.584543-1-jim.shu@sifive.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: pass client-ip=2607:f8b0:4864:20::42a; envelope-from=jim.shu@sifive.com; helo=mail-pf1-x42a.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable 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 @sifive.com) X-ZM-MESSAGEID: 1761063280096154100 Content-Type: text/plain; charset="utf-8" When a RISC-V HART has WG extension, their memory transactions will contain WID. Support MemTxAttrs in RISC-V target and add WID inside if a HART has WG extension. Signed-off-by: Jim Shu --- target/riscv/cpu.c | 2 +- target/riscv/cpu.h | 1 + target/riscv/cpu_helper.c | 51 ++++++++++++++++++++++++++++++++++++--- 3 files changed, 50 insertions(+), 4 deletions(-) diff --git a/target/riscv/cpu.c b/target/riscv/cpu.c index 22b4070476..32a9f8a6a4 100644 --- a/target/riscv/cpu.c +++ b/target/riscv/cpu.c @@ -2678,7 +2678,7 @@ static int64_t riscv_get_arch_id(CPUState *cs) =20 static const struct SysemuCPUOps riscv_sysemu_ops =3D { .has_work =3D riscv_cpu_has_work, - .get_phys_page_debug =3D riscv_cpu_get_phys_page_debug, + .get_phys_page_attrs_debug =3D riscv_cpu_get_phys_page_attrs_debug, .write_elf64_note =3D riscv_cpu_write_elf64_note, .write_elf32_note =3D riscv_cpu_write_elf32_note, .legacy_vmsd =3D &vmstate_riscv_cpu, diff --git a/target/riscv/cpu.h b/target/riscv/cpu.h index e99aa11140..e2436fdcd8 100644 --- a/target/riscv/cpu.h +++ b/target/riscv/cpu.h @@ -625,6 +625,7 @@ int riscv_env_mmu_index(CPURISCVState *env, bool ifetch= ); bool cpu_get_fcfien(CPURISCVState *env); bool cpu_get_bcfien(CPURISCVState *env); bool riscv_env_smode_dbltrp_enabled(CPURISCVState *env, bool virt); +hwaddr riscv_cpu_get_phys_page_attrs_debug(CPUState *cpu, vaddr addr, MemT= xAttrs *attrs); G_NORETURN void riscv_cpu_do_unaligned_access(CPUState *cs, vaddr addr, MMUAccessType access_type, int mmu_idx, uintptr_t reta= ddr); diff --git a/target/riscv/cpu_helper.c b/target/riscv/cpu_helper.c index d2be660d58..64e5557f0a 100644 --- a/target/riscv/cpu_helper.c +++ b/target/riscv/cpu_helper.c @@ -136,6 +136,34 @@ bool riscv_env_smode_dbltrp_enabled(CPURISCVState *env= , bool virt) } =20 #ifndef CONFIG_USER_ONLY +static uint32_t riscv_cpu_wg_get_wid(CPURISCVState *env, int mode) +{ + CPUState *cs =3D env_cpu(env); + RISCVCPU *cpu =3D RISCV_CPU(cs); + bool virt =3D env->virt_enabled; + + if (mode =3D=3D PRV_M) { + return cpu->cfg.mwid; + } else if (mode =3D=3D PRV_S) { + if (!virt || !env->mwiddeleg) { + /* HS-mode, S-mode w/o RVH, or VS-mode but mwiddeleg =3D 0 */ + return env->mlwid; + } else { + /* VS-mode */ + return env->slwid; + } + } else if (mode =3D=3D PRV_U) { + if (!riscv_has_ext(env, RVS) || !env->mwiddeleg) { + /* M/U mode CPU or mwiddeleg =3D 0 */ + return env->mlwid; + } else { + return env->slwid; + } + } + + return cpu->cfg.mwid; +} + void riscv_cpu_set_wg_mwid(CPURISCVState *env, uint32_t mwid) { CPUState *cs =3D env_cpu(env); @@ -1675,13 +1703,22 @@ static void raise_mmu_exception(CPURISCVState *env,= target_ulong address, env->two_stage_indirect_lookup =3D two_stage_indirect; } =20 -hwaddr riscv_cpu_get_phys_page_debug(CPUState *cs, vaddr addr) +hwaddr riscv_cpu_get_phys_page_attrs_debug(CPUState *cs, vaddr addr, MemTx= Attrs *attrs) { RISCVCPU *cpu =3D RISCV_CPU(cs); CPURISCVState *env =3D &cpu->env; hwaddr phys_addr; int prot; int mmu_idx =3D riscv_env_mmu_index(&cpu->env, false); + int mode =3D mmuidx_priv(mmu_idx); + uint32_t wid; + + if (riscv_cpu_cfg(env)->ext_smwg && env->wid_to_mem_attrs) { + wid =3D riscv_cpu_wg_get_wid(env, mode); + env->wid_to_mem_attrs(attrs, wid); + } else { + *attrs =3D MEMTXATTRS_UNSPECIFIED; + } =20 if (get_physical_address(env, &phys_addr, &prot, addr, NULL, 0, mmu_id= x, true, env->virt_enabled, true, false)) { @@ -1793,12 +1830,20 @@ bool riscv_cpu_tlb_fill(CPUState *cs, vaddr address= , int size, int mode =3D mmuidx_priv(mmu_idx); /* default TLB page size */ hwaddr tlb_size =3D TARGET_PAGE_SIZE; + uint32_t wid; + MemTxAttrs attrs =3D MEMTXATTRS_UNSPECIFIED; =20 env->guest_phys_fault_addr =3D 0; =20 qemu_log_mask(CPU_LOG_MMU, "%s ad %" VADDR_PRIx " rw %d mmu_idx %d\n", __func__, address, access_type, mmu_idx); =20 + if (riscv_cpu_cfg(env)->ext_smwg && env->wid_to_mem_attrs) { + mode =3D mmuidx_priv(mmu_idx); + wid =3D riscv_cpu_wg_get_wid(env, mode); + env->wid_to_mem_attrs(&attrs, wid); + } + pmu_tlb_fill_incr_ctr(cpu, access_type); if (two_stage_lookup) { /* Two stage lookup */ @@ -1891,8 +1936,8 @@ bool riscv_cpu_tlb_fill(CPUState *cs, vaddr address, = int size, } =20 if (ret =3D=3D TRANSLATE_SUCCESS) { - tlb_set_page(cs, address & ~(tlb_size - 1), pa & ~(tlb_size - 1), - prot, access_type, mmu_idx, tlb_size); + tlb_set_page_with_attrs(cs, address & ~(tlb_size - 1), pa & ~(tlb_= size - 1), + attrs, prot, access_type, mmu_idx, tlb_siz= e); return true; } else if (probe) { return false; --=20 2.43.0 From nobody Fri Nov 14 17:01:12 2025 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; dmarc=pass(p=reject dis=none) header.from=sifive.com ARC-Seal: i=1; a=rsa-sha256; t=1761063293; cv=none; d=zohomail.com; s=zohoarc; b=df8yjTkK04TtEbzx2AQW2Cf0aKcI89KjYvRdQdhQuTzBOg6y5d6xmYZizcRto+z0ry4Wm3bxc0RTsNCDD+P6KRMMoAmDoGotVNRpbt3m+yoyczOIgl0Z0jSZHLLbUpxUG8XmRbyLcRtS4oB/xJs91XIlkLsQgSHjKhER/1qM5aA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1761063293; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=2TCE8SvaITfs8coL27qj39ul3rERe6+OoO3JJ8MTgYA=; b=MBDtzfYDCCpnKiNXCyHcf09gEcurydMQ5VvMBMiI0J8/bApfQSpX/YEGH/zSP4hNjAkXfnCuEOFkrS3P46qu0wAQChzzf0XRyg1HKtNSEITppN3uZZMOsiyGeuXpR8JEbnqCtHYzoN5TZyCWIo6b3cLER/wiWTSE9yILWi3E89w= 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; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1761063293010672.315498640231; Tue, 21 Oct 2025 09:14:53 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1vBEzs-0004Of-6k; Tue, 21 Oct 2025 12:14:04 -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 1vBEzq-0004Nc-Nq for qemu-devel@nongnu.org; Tue, 21 Oct 2025 12:14:02 -0400 Received: from mail-pf1-x42e.google.com ([2607:f8b0:4864:20::42e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1vBEzn-0001dS-H6 for qemu-devel@nongnu.org; Tue, 21 Oct 2025 12:14:02 -0400 Received: by mail-pf1-x42e.google.com with SMTP id d2e1a72fcca58-782a77b5ec7so5370626b3a.1 for ; Tue, 21 Oct 2025 09:13:59 -0700 (PDT) Received: from hsinchu26.internal.sifive.com ([210.176.154.34]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7a23010f7c7sm11731157b3a.54.2025.10.21.09.13.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Oct 2025 09:13:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1761063238; x=1761668038; 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=2TCE8SvaITfs8coL27qj39ul3rERe6+OoO3JJ8MTgYA=; b=Mf0SpKW+NgKLKFwOUrqxSp81iM7H0HyZ7DJILfjfZJtr6Sy/Ux/EXFy7pXKhPgHYuZ U1B1NiSmXAOR+o1hIUFT3E9a7A1YKgG9JrvHLVixp9usYB7AaKEtg8JAUqkNZy8zI+Cw r0W8ksnB0FDOviDTZD6cnQ1JpfcUT8hPxoxYFddvrBsQhw47m8dvmSgfbK5M83eflVpT N04IkrZdXkYE5lqIiSiXuqf6Vz+QZPnzJ/RFt5pSAKAp/qQTqWrUNQ4jQRXhehqTHfvh Ll1ooyxHTb++jZb2XReNaGHRnf2serfvP0nu1DlC+BtuQy1HW3pg9ICWFVvmNRdN/BiN h9TA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1761063238; x=1761668038; 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=2TCE8SvaITfs8coL27qj39ul3rERe6+OoO3JJ8MTgYA=; b=Cai0VAKMvToH71PyYUcxof3ViF/gKWJ7St+bL5pntUG0p9c4blV7c6kBzuo4cEbYJW Ocz3XS5Gj6KCOQhs+H63a4yc+naVaSKGSHbJeL4RBNTF0FYT6PLaw01R1hBR0iKZFFex j6Lcr7yjIk+IiCAl7uoa7z51LsZAaBxme+titFp+SM4mlS9z60uU0b9XYE69XTaFhbLI Qq7/tzTccvSlWMXAK6S8oP9VaGD+OEAxoZ0ZuA+91jN3X34jjRzdH0LL66L6LTWSfaaI zs62HYgM/xhwSwyJGs2my/alEArhAIqaZGX/tMRhX22J5zSs1s5z7go+cien8iAYv6rc nLfg== X-Gm-Message-State: AOJu0YzMOM8HBrtEImlswt6AhfzHN7fBHiVBOiX7w1pkr8gRDeBTcm4c IbxM+w8/r08ARzJa0hCMU1JYXE8lnMRz937lenOM5nW/7klzBtns1KuPTGMEzwrfKtTxgkQjgUy 76s2sm7Wu1sa/prLLoBli5y+bAKSZwMZ9T4ZbKtjl2tzlkxHn83IEmP2knx9uuNf/JWwyh2gooI i5Cgi6HDfA26yZthY/Oq6HwY+t12WYppDyC7X1Gw== X-Gm-Gg: ASbGncsh49T9UF/mO9/glsKGVJIQxRVzUtKi8lN11SfkkAqySxetiJj405MJWhhhUEn YbhUE9L8ATiamBTz+/Q//OAQlGKB44RWkrdrVvtH7zhmFXS0gzTuakooz/yAQTEC+upC+KLy1iU f6gK2usN4dMae06VVEczSH/S1rD+8jFUqyDLTBDbV4Mih5gUN3qhLPX1e2L6bmAPCLGH73ZiCim /mu3yucUv6i6J3Zpha+xu6EOt+6dmaTGxMNlWlCbBx/iqTNo6QLh15k1ER/t2kW6x2S8/5YPhKR 5Wb0PzxAhg4ikav9iCp+WBi3clRoung0wJf7H6Vb/DWy8nR1EXaxQW46IINfSx6aSNGxOjWGfT5 l7g/vcNHHzcM7YBYfZqKqmA+vj9d4NNywppabLteAd+JvrZnJ5G1l8lkypLPsPp8gx6QVlBPi3j f+FjtaVjuFmwLW91fuvuSl3UauBhxz1YaI X-Google-Smtp-Source: AGHT+IEA5R+f82XyVar/ubEBqZZMWM8CYNx9jQMOmBq14yxF9TVlfPiAIb0+PERnaH4eGAVT1rGM+g== X-Received: by 2002:a05:6a20:7351:b0:334:912f:acda with SMTP id adf61e73a8af0-334a8525d01mr23518065637.23.1761063237325; Tue, 21 Oct 2025 09:13:57 -0700 (PDT) From: Jim Shu To: qemu-devel@nongnu.org, qemu-riscv@nongnu.org Cc: Daniel Henrique Barboza , Richard Henderson , Paolo Bonzini , Palmer Dabbelt , Alistair Francis , Weiwei Li , Liu Zhiwei , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Eduardo Habkost , Marcel Apfelbaum , Yanan Wang , Zhao Liu , Peter Xu , David Hildenbrand , Michael Rolnik , Helge Deller , Song Gao , Laurent Vivier , "Edgar E. Iglesias" , Aurelien Jarno , Jiaxun Yang , Aleksandar Rikalo , Stafford Horne , Nicholas Piggin , Chinmay Rath , Yoshinori Sato , Ilya Leoshkevich , Thomas Huth , Mark Cave-Ayland , Artyom Tarasenko , Bastian Koppelmann , Max Filippov , qemu-ppc@nongnu.org (open list:PowerPC TCG CPUs), qemu-s390x@nongnu.org (open list:S390 TCG CPUs), Jim Shu Subject: [PATCH v3 12/18] target/riscv: Expose CPU options of WorldGuard Date: Wed, 22 Oct 2025 00:13:18 +0800 Message-ID: <20251021161325.585278-5-jim.shu@sifive.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20251021155548.584543-1-jim.shu@sifive.com> References: <20251021155548.584543-1-jim.shu@sifive.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: pass client-ip=2607:f8b0:4864:20::42e; envelope-from=jim.shu@sifive.com; helo=mail-pf1-x42e.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable 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 @sifive.com) X-ZM-MESSAGEID: 1761063299447158500 Content-Type: text/plain; charset="utf-8" Expose WG CPU extensions (Smwg, Sswg, Smwgd) and WG CPU configs (mwid, mwidlist). Signed-off-by: Jim Shu Reviewed-by: Daniel Henrique Barboza --- target/riscv/cpu.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/target/riscv/cpu.c b/target/riscv/cpu.c index 32a9f8a6a4..b8704e7e88 100644 --- a/target/riscv/cpu.c +++ b/target/riscv/cpu.c @@ -1380,6 +1380,11 @@ const RISCVCPUMultiExtConfig riscv_cpu_vendor_exts[]= =3D { const RISCVCPUMultiExtConfig riscv_cpu_experimental_exts[] =3D { MULTI_EXT_CFG_BOOL("x-svukte", ext_svukte, false), =20 + /* RISC-V WorldGuard v0.4 */ + MULTI_EXT_CFG_BOOL("x-smwg", ext_smwg, false), + MULTI_EXT_CFG_BOOL("x-smwgd", ext_smwgd, false), + MULTI_EXT_CFG_BOOL("x-sswg", ext_sswg, false), + { }, }; =20 @@ -2648,6 +2653,9 @@ static const Property riscv_cpu_properties[] =3D { * it with -x and default to 'false'. */ DEFINE_PROP_BOOL("x-misa-w", RISCVCPU, cfg.misa_w, false), + + DEFINE_PROP_UINT32("x-mwid", RISCVCPU, cfg.mwid, UINT32_MAX), + DEFINE_PROP_UINT32("x-mwidlist", RISCVCPU, cfg.mwidlist, UINT32_MAX), }; =20 static const gchar *riscv_gdb_arch_name(CPUState *cs) --=20 2.43.0 From nobody Fri Nov 14 17:01:12 2025 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; dmarc=pass(p=reject dis=none) header.from=sifive.com ARC-Seal: i=1; a=rsa-sha256; t=1761063363; cv=none; d=zohomail.com; s=zohoarc; b=j0Jxd4K++rJT8bBYmGSScDjQC4MYxgUBAxyxiSyW/tWSKUASIUJ1sgaOecUdjsEEtXyVFUj3ve9hRL1Xj/4zrHlXeaUjvDhxroOOm8LuISiRh5rGC0DMjYTl8P2brpbUBWaZLmK5n0PgRIR57Ew6/E41WyAPqC2Cd8qa4yD4TV8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1761063363; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=mCgTD8dkwQgMMt8MeCyjtFh4SBtoRLRCufrZusreNxI=; b=GFbf0bG7TTBM8xoL/pDBi91kkgmsAroexKztDXdylxOOpMhkBq73TXAeuA4S53nXqb92xsLLtFFuzOpAASadgph21UgNEsGTRxuKxQppe4udp/Q7b+NWHyL/PfPHgXlRoliRujcHYSCGDOFSdfjnii5IvDe3EAhJnqDtrSoZ10k= 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; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1761063362994519.7717966771268; Tue, 21 Oct 2025 09:16:02 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1vBEzy-0004Rq-4m; Tue, 21 Oct 2025 12:14:10 -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 1vBEzw-0004Qx-SO for qemu-devel@nongnu.org; Tue, 21 Oct 2025 12:14:08 -0400 Received: from mail-pf1-x436.google.com ([2607:f8b0:4864:20::436]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1vBEzt-0001eG-E3 for qemu-devel@nongnu.org; Tue, 21 Oct 2025 12:14:08 -0400 Received: by mail-pf1-x436.google.com with SMTP id d2e1a72fcca58-781251eec51so4581861b3a.3 for ; Tue, 21 Oct 2025 09:14:05 -0700 (PDT) Received: from hsinchu26.internal.sifive.com ([210.176.154.34]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7a23010f7c7sm11731157b3a.54.2025.10.21.09.13.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Oct 2025 09:14:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1761063243; x=1761668043; 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=mCgTD8dkwQgMMt8MeCyjtFh4SBtoRLRCufrZusreNxI=; b=QyymTXe8LhyLGvK6G/cxpvema7MvmvTxU+GU6CrI43STQ8do+0pcOrZ3tyjfPKKNBJ PQU240p6Cn4prBx2graLPEp8MhLHZGBIlnbMqup9NMZ2OKqTfZ2t9C7rCPVOmN6zOtPf iKdVmL8tUiZ2dte706HbITUr0Wl8jqndFKPFLNKq10jdLSBzAyh6FUE8MazV9Z7ikYta +qQkY4Qk47KFvccxjtiH13mVmAoEuA3sEQpO+C7tU0HihQl1k8TMFHPviqEJQJcx7DRU ozd7C9ohZn4gf+C6mGXdOyRMSis7l/8u5JVUoLNqEwJ+1sET84QspePqF0SJUPR8o+0n F1aQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1761063243; x=1761668043; 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=mCgTD8dkwQgMMt8MeCyjtFh4SBtoRLRCufrZusreNxI=; b=o9UgHqIIfPZX65VQxPxOppD9X/qSYoG6UL4++BjfMA+L+8KPRPybko7f1W4KuOVxsp ntjVEpU/2olno08hYrTIoK8uq7LHkQVNIKkcr3X/MBLw38V+hKeAue9mSsa3UK8Q+Iyk Sst1SSM2haHHrO2mVRy0+pe2uzOuBE9u2BdwKm6EdSvJ5aQCzqxVoCj/1tuLqtJgAXia e8JXXf+jmfL+uIpzS1u8JBFggicR1IInv6KisWvRgqXSXhdLHmJMPf0EIKDAOl4FQPIT 0d6Zxz8xJ7wCpY1578YASZvHOai+o8wDWAW3QQlQ+2LfWF/wqW7Dd1YsF4u+RKUJd97T LWOw== X-Gm-Message-State: AOJu0YzK1+gECZOLqfLxaJgiNGc0WasjWsrHaoMMIxobCiei3yglMjTV wQ0YhI7zougHstDurNCPXnYuYinwpcijKYsGCDnTzqPixY42H0rcyTmQ2/6ZVGiTYVyOvSaAu2B qAdymBC5JMzeZEHnafU200p5v5oIQtRP2Z/3mN2s9e9FnpkKOw1VQRLFWIkd8Pdt61zzXliAl5Z ytJ4DbVIw3cR9xMQRZ0FyNBGzW67xZZH8pYaDwnw== X-Gm-Gg: ASbGnct5qH6gj+0663t7kIDf5GEtFOVeEzUi+/gyVJLLXkg5E4iy9kSHKM+zD+Nbe5Q N6a/kJYr5Mhn4fyLsbG7ampVBwmBTT3GhqOXR1n2wFWkuzKpprWwV/0RKjGW2GlA1XSbQO4i8qM vP5XQVJ1Zpqp7ILoTRJ3AqhG7uOlYavpvCueJfYmjjetSIk/XDUpC2OToYx2octI3zQGo/9ujhI cQl8tRCrw9xIcYNFFJUiKKtMYiRokRb++q/j68a7MM4xFDVrVXjRbqsBoy1SRxx39moU9UtRiPn O29qJYtPWWWpf10BbkTqzJjICu0T79in/J3DVZiGd3T2PP2jazzH3hyhEnToHqsm9SYaWreHuke OLavlA7GJ6rkhcRJxUdoXlsk7kMUCZ4GI2ExkolpLgTw4s8cgmn7Jpl/IuOzx5T3HpoarRez/Cc KYoSVbn40pl9xdrHJtsxe40w== X-Google-Smtp-Source: AGHT+IG1N5YhVFO62QR4+4v3Tn9KmpHp9epP+wwMroJLP+wisiu2TQIWQ3DE49usvcvmEENmH6dXDw== X-Received: by 2002:a05:6a00:b8c:b0:77f:4641:e5ac with SMTP id d2e1a72fcca58-7a220a59afcmr20764351b3a.6.1761063243025; Tue, 21 Oct 2025 09:14:03 -0700 (PDT) From: Jim Shu To: qemu-devel@nongnu.org, qemu-riscv@nongnu.org Cc: Daniel Henrique Barboza , Richard Henderson , Paolo Bonzini , Palmer Dabbelt , Alistair Francis , Weiwei Li , Liu Zhiwei , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Eduardo Habkost , Marcel Apfelbaum , Yanan Wang , Zhao Liu , Peter Xu , David Hildenbrand , Michael Rolnik , Helge Deller , Song Gao , Laurent Vivier , "Edgar E. Iglesias" , Aurelien Jarno , Jiaxun Yang , Aleksandar Rikalo , Stafford Horne , Nicholas Piggin , Chinmay Rath , Yoshinori Sato , Ilya Leoshkevich , Thomas Huth , Mark Cave-Ayland , Artyom Tarasenko , Bastian Koppelmann , Max Filippov , qemu-ppc@nongnu.org (open list:PowerPC TCG CPUs), qemu-s390x@nongnu.org (open list:S390 TCG CPUs), Jim Shu Subject: [PATCH v3 13/18] hw/misc: riscv_worldguard: Add API to enable WG extension of CPU Date: Wed, 22 Oct 2025 00:13:19 +0800 Message-ID: <20251021161325.585278-6-jim.shu@sifive.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20251021155548.584543-1-jim.shu@sifive.com> References: <20251021155548.584543-1-jim.shu@sifive.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: pass client-ip=2607:f8b0:4864:20::436; envelope-from=jim.shu@sifive.com; helo=mail-pf1-x436.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable 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 @sifive.com) X-ZM-MESSAGEID: 1761063366306158500 Content-Type: text/plain; charset="utf-8" riscv_worldguard_apply_cpu() could enable WG CPU extension and set WG callback to CPUs. It is used by machine code after realizing global WG device. Signed-off-by: Jim Shu --- hw/misc/riscv_worldguard.c | 92 ++++++++++++++++++++++++++++++ include/hw/misc/riscv_worldguard.h | 1 + 2 files changed, 93 insertions(+) diff --git a/hw/misc/riscv_worldguard.c b/hw/misc/riscv_worldguard.c index 588c16ae9a..73fa8190cd 100644 --- a/hw/misc/riscv_worldguard.c +++ b/hw/misc/riscv_worldguard.c @@ -92,6 +92,98 @@ uint32_t mem_attrs_to_wid(MemTxAttrs attrs) } } =20 +static void riscv_cpu_wg_reset(CPURISCVState *env) +{ + CPUState *cs =3D env_cpu(env); + RISCVCPU *cpu =3D RISCV_CPU(cs); + uint32_t mlwid, slwid, mwiddeleg; + uint32_t trustedwid; + + if (!riscv_cpu_cfg(env)->ext_smwg) { + return; + } + + if (worldguard_config =3D=3D NULL) { + /* + * Note: This reset is dummy now and WG CSRs will be reset again + * after worldguard_config is realized. + */ + return; + } + + trustedwid =3D worldguard_config->trustedwid; + if (trustedwid =3D=3D NO_TRUSTEDWID) { + trustedwid =3D worldguard_config->nworlds - 1; + } + + /* Reset mlwid, slwid, mwiddeleg CSRs */ + if (worldguard_config->hw_bypass) { + /* HW bypass mode */ + mlwid =3D trustedwid; + } else { + mlwid =3D 0; + } + slwid =3D 0; + mwiddeleg =3D 0; + + env->mlwid =3D mlwid; + if (riscv_cpu_cfg(env)->ext_sswg) { + env->slwid =3D slwid; + env->mwiddeleg =3D mwiddeleg; + } + + /* Check mwid, mwidlist config */ + if (worldguard_config !=3D NULL) { + uint32_t valid_widlist =3D MAKE_64BIT_MASK(0, worldguard_config->n= worlds); + + /* CPU use default mwid / mwidlist config if not set */ + if (cpu->cfg.mwidlist =3D=3D UINT32_MAX) { + /* mwidlist contains all WIDs */ + cpu->cfg.mwidlist =3D valid_widlist; + } + if (cpu->cfg.mwid =3D=3D UINT32_MAX) { + cpu->cfg.mwid =3D trustedwid; + } + + /* Check if mwid/mwidlist HW config is valid in NWorld. */ + g_assert((cpu->cfg.mwidlist & ~valid_widlist) =3D=3D 0); + g_assert(cpu->cfg.mwid < worldguard_config->nworlds); + } +} + +/* + * riscv_worldguard_apply_cpu - Enable WG extension of CPU + * + * Note: This API should be used after global WG device is created + * (riscv_worldguard_realize()). + */ +void riscv_worldguard_apply_cpu(uint32_t hartid) +{ + CPUState *cpu =3D cpu_by_arch_id(hartid); + RISCVCPU *rcpu =3D RISCV_CPU(cpu); + CPURISCVState *env =3D cpu ? cpu_env(cpu) : NULL; + + /* WG global config should exist */ + g_assert(worldguard_config); + + /* If the CPU with this hartid doesn't exist */ + if (env =3D=3D NULL) { + return; + } + + rcpu->cfg.ext_smwg =3D true; + if (riscv_has_ext(env, RVS) && riscv_has_ext(env, RVU)) { + rcpu->cfg.ext_sswg =3D true; + } + + /* Set machine specific WorldGuard callback */ + env->wg_reset =3D riscv_cpu_wg_reset; + env->wid_to_mem_attrs =3D wid_to_mem_attrs; + + /* Reset WG CSRs in CPU */ + env->wg_reset(env); +} + bool could_access_wgblocks(MemTxAttrs attrs, const char *wgblock) { uint32_t wid =3D mem_attrs_to_wid(attrs); diff --git a/include/hw/misc/riscv_worldguard.h b/include/hw/misc/riscv_wor= ldguard.h index bb276e59b8..fb08c92f46 100644 --- a/include/hw/misc/riscv_worldguard.h +++ b/include/hw/misc/riscv_worldguard.h @@ -48,6 +48,7 @@ extern struct RISCVWorldGuardState *worldguard_config; =20 DeviceState *riscv_worldguard_create(uint32_t nworlds, uint32_t trustedwid, bool hw_bypass, bool tz_compat); +void riscv_worldguard_apply_cpu(uint32_t hartid); =20 void wid_to_mem_attrs(MemTxAttrs *attrs, uint32_t wid); uint32_t mem_attrs_to_wid(MemTxAttrs attrs); --=20 2.43.0 From nobody Fri Nov 14 17:01:12 2025 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; dmarc=pass(p=reject dis=none) header.from=sifive.com ARC-Seal: i=1; a=rsa-sha256; t=1761063376; cv=none; d=zohomail.com; s=zohoarc; b=CXjvo9v0sWdkNWi/krSBG0r9l3fxlm+o7qJQGRx/rrHO3XJ//3ib5+arsAQuL6esSN5I6sMomfT8vX6bh+rVg4mzFlMRtrQFy4faBT9KFelMR4qC0NVLnj1WisLeTEHyOEUwUokc8KjKf6XLfUIhqmT8cLv8yyoCrKYuZTTLCLQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1761063376; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=9sSUm3c0npvdWUzDrwKmMIiHjYGtJNeB7WDV0+dZDj8=; b=TRTWA18b4YGL7jyKxlB+lCGCXY2khjZ6yL+BgWl1OuFih1RpdHfz30qQbBgCKkYRvb+LXl84hj4w9ZNr610tDV7mlFdoLL/RH8EKCP2Blw/bgnX14UITUDcOAHtstgPsmf2QWmSveDkSHusG6Ik1meS4CtPTLD/jXAr9/XZCkQ8= 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; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1761063376264933.7757490747543; Tue, 21 Oct 2025 09:16:16 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1vBF06-0004V9-Ej; Tue, 21 Oct 2025 12:14:18 -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 1vBF05-0004UR-0O for qemu-devel@nongnu.org; Tue, 21 Oct 2025 12:14:17 -0400 Received: from mail-pf1-x429.google.com ([2607:f8b0:4864:20::429]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1vBEzz-0001f9-9P for qemu-devel@nongnu.org; Tue, 21 Oct 2025 12:14:16 -0400 Received: by mail-pf1-x429.google.com with SMTP id d2e1a72fcca58-7835321bc98so5141303b3a.2 for ; Tue, 21 Oct 2025 09:14:10 -0700 (PDT) Received: from hsinchu26.internal.sifive.com ([210.176.154.34]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7a23010f7c7sm11731157b3a.54.2025.10.21.09.14.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Oct 2025 09:14:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1761063249; x=1761668049; 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=9sSUm3c0npvdWUzDrwKmMIiHjYGtJNeB7WDV0+dZDj8=; b=mhbHjzKa4Rera0H7CH0n56doZY4cmibGs7k94YOt2z8NOSd5pDcVdm5uLLnIuxiq27 yv8quNX58ju6RoM/dG39NJZ/Wbr779zOLBAtp8VtnldR0uhIi91gB6FwztGHEVL66tjf z6/ZETQhS3854qr/cJXYkMXEF3ogX+iFNyBunGFx0perOK4dnNpkkgyDJrPPao8GXSn+ njfb3ypVGkXEuMuv++SwabB/eetRIstfyzo8aFx1vhilrof2H6u9OvuEQNQaXtGUNQbv 2v1B5UYW4rTB1QRPTEn76q7x2ARqyo4H44FmgsCyNEuO50jeIzGiD/uOjWw/JS0gImuc eQRA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1761063249; x=1761668049; 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=9sSUm3c0npvdWUzDrwKmMIiHjYGtJNeB7WDV0+dZDj8=; b=Mr8AsFQvk2poIUDxCKpyHGcmgQrFByq3soLsIPHCQq+JD9KJ1WtqRU0tGlJd7s1H/Y 7KluXyiVEiZOChRH7WbfLvIY1rUYO5pINKBDlEi3US9MGbmPNEyzmO7oR3dnm6BZrzC0 /unvQvNAA3OiK8x8yZAnorFkR6s/SNAqBIUNJONmxZDiQsFjFzy/2KBp1T7zTYnNPVci rV9glhnDw9iRlhh1cjbDafM0QgBF+WxwOnFJ3cgVSldJX6AkR4kNJjNSAglmNL7M5kiY w1eXmhy/8jlS9WR9lvLupjg5zGIIWlNXcdNdPL3uxbcxcUzQJp2HQPE16Rgn1hJELMIL sZGA== X-Gm-Message-State: AOJu0Yy6klrPVbo4vAs4ywBWQ85O2mJgKn8AuEoE0rr38GpwQkbR4221 iXMvh8UAKnxDbgq6h4iMKtH7aRBetRw8RCchtDSyo4wRsqWIKS1727QbIdkt04s8d3ERcrxyYCQ ND0HZTSGcB7g+hEdPJYXI++0o3LdVATFm0oWvbfF1Q0UisIYcMfrH2ZfzhtoBpRArZ5waAj2jik ToasoXWqd5gg1vnJud9OmGYfAwAZYiFU+/80bPdw== X-Gm-Gg: ASbGncugCLa3nNdn/NqB5qTkWtCkDmJXNDFbwYPu2ZcHN9ErsTZpNxkLNWtkzEpPRQ7 aI/Mi+Xs/TEUh8abxbKZaTXOu7Qja5yBzRUcmGPX8Ntay0Lqx8cjrKOXCJrYP7pK/ij/xfxu8rq 6JsbmW3+E82B1O98ZF+lwPTtp04rtnL8FMB05bIfrDdDsJ0Dj+aRL+v7MIZUXpP3B1F2J8Gm8JQ qXaQM0pnP6g1iz2RNnol9qTTQXX76yEs5XlDHldktGm7n0CEK9USScTCFOGr8ucuCxV7u7s6OIV AqrUF8ge0V2Wl3zTBgItl+ZE9L/Ga7p+3Oear9LjaSafAg3no7zEVjaXtYoMTEp2cy9yX46tBL1 pZHnKJBG0akQ/Bz6DagPOFTeNsXXFevuvUVOe5vxu/AVqiVn5/F27aVNbMFCENVnXKAtUcW62aL S6ESi3gspXPpHth2ELqEK4nPVnXUjrb7VU X-Google-Smtp-Source: AGHT+IHJR/6yWV0d0ZvEbZg+3NeosyWz8NUPEp3iGHKpIjQznMFNSLy7H3XDsa5TEEAgXqgo6A7SnQ== X-Received: by 2002:a05:6a20:3943:b0:24d:7926:bb29 with SMTP id adf61e73a8af0-334a85151bfmr22454136637.5.1761063248959; Tue, 21 Oct 2025 09:14:08 -0700 (PDT) From: Jim Shu To: qemu-devel@nongnu.org, qemu-riscv@nongnu.org Cc: Daniel Henrique Barboza , Richard Henderson , Paolo Bonzini , Palmer Dabbelt , Alistair Francis , Weiwei Li , Liu Zhiwei , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Eduardo Habkost , Marcel Apfelbaum , Yanan Wang , Zhao Liu , Peter Xu , David Hildenbrand , Michael Rolnik , Helge Deller , Song Gao , Laurent Vivier , "Edgar E. Iglesias" , Aurelien Jarno , Jiaxun Yang , Aleksandar Rikalo , Stafford Horne , Nicholas Piggin , Chinmay Rath , Yoshinori Sato , Ilya Leoshkevich , Thomas Huth , Mark Cave-Ayland , Artyom Tarasenko , Bastian Koppelmann , Max Filippov , qemu-ppc@nongnu.org (open list:PowerPC TCG CPUs), qemu-s390x@nongnu.org (open list:S390 TCG CPUs), Jim Shu , "Fea . Wang" Subject: [PATCH v3 14/18] hw/misc: riscv_wgchecker: Implement RISC-V WorldGuard Checker Date: Wed, 22 Oct 2025 00:13:20 +0800 Message-ID: <20251021161325.585278-7-jim.shu@sifive.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20251021155548.584543-1-jim.shu@sifive.com> References: <20251021155548.584543-1-jim.shu@sifive.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: pass client-ip=2607:f8b0:4864:20::429; envelope-from=jim.shu@sifive.com; helo=mail-pf1-x429.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable 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 @sifive.com) X-ZM-MESSAGEID: 1761063379249154100 Content-Type: text/plain; charset="utf-8" Implement the RISC-V WorldGuard Checker, which sits in front of RAM or device MMIO and allow software to configure it to either pass through or reject transactions. We implement the wgChecker as a QEMU IOMMU, which will direct transactions either through to the devices and memory behind it or to a special "never works" AddressSpace if they are blocked. This initial commit implements the skeleton of the device: * it always permits accesses * it doesn't implement wgChecker's slot registers * it doesn't implement the interrupt or other behaviour for blocked transactions Signed-off-by: Jim Shu Signed-off-by: Fea.Wang Reviewed-by: Daniel Henrique Barboza --- hw/misc/meson.build | 2 +- hw/misc/riscv_wgchecker.c | 618 +++++++++++++++++++++++++++++ hw/misc/trace-events | 8 + include/hw/misc/riscv_worldguard.h | 63 +++ 4 files changed, 690 insertions(+), 1 deletion(-) create mode 100644 hw/misc/riscv_wgchecker.c diff --git a/hw/misc/meson.build b/hw/misc/meson.build index 200ccc96c0..019afa0fad 100644 --- a/hw/misc/meson.build +++ b/hw/misc/meson.build @@ -34,7 +34,7 @@ system_ss.add(when: 'CONFIG_SIFIVE_E_PRCI', if_true: file= s('sifive_e_prci.c')) system_ss.add(when: 'CONFIG_SIFIVE_E_AON', if_true: files('sifive_e_aon.c'= )) system_ss.add(when: 'CONFIG_SIFIVE_U_OTP', if_true: files('sifive_u_otp.c'= )) system_ss.add(when: 'CONFIG_SIFIVE_U_PRCI', if_true: files('sifive_u_prci.= c')) -specific_ss.add(when: 'CONFIG_RISCV_WORLDGUARD', if_true: files('riscv_wor= ldguard.c')) +specific_ss.add(when: 'CONFIG_RISCV_WORLDGUARD', if_true: files('riscv_wor= ldguard.c', 'riscv_wgchecker.c')) =20 subdir('macio') =20 diff --git a/hw/misc/riscv_wgchecker.c b/hw/misc/riscv_wgchecker.c new file mode 100644 index 0000000000..fed6a14fbd --- /dev/null +++ b/hw/misc/riscv_wgchecker.c @@ -0,0 +1,618 @@ +/* + * RISC-V WorldGuard Checker Device + * + * Copyright (c) 2022 SiFive, Inc. + * + * This provides WorldGuard Checker model. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2 or later, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License f= or + * more details. + * + * You should have received a copy of the GNU General Public License along= with + * this program. If not, see . + */ + +#include "qemu/osdep.h" +#include "qapi/error.h" +#include "qemu/log.h" +#include "exec/hwaddr.h" +#include "hw/irq.h" +#include "hw/registerfields.h" +#include "hw/sysbus.h" +#include "hw/hw.h" +#include "hw/qdev-properties.h" +#include "hw/misc/riscv_worldguard.h" +#include "target/riscv/cpu.h" +#include "trace.h" + +/* Common */ +REG32(VENDOR, 0x000) +REG32(IMPID, 0x004) + +/* wgChecker */ +REG32(NSLOTS, 0x008) +REG64(ERRCAUSE, 0x010) + FIELD(ERRCAUSE, WID, 0, 8) + FIELD(ERRCAUSE, R, 8, 1) + FIELD(ERRCAUSE, W, 9, 1) + FIELD(ERRCAUSE, BE, 62, 1) + FIELD(ERRCAUSE, IP, 63, 1) + +#define ERRCAUSE_MASK \ + (R_ERRCAUSE_WID_MASK | \ + R_ERRCAUSE_R_MASK | \ + R_ERRCAUSE_W_MASK | \ + R_ERRCAUSE_BE_MASK | \ + R_ERRCAUSE_IP_MASK) + +REG64(ERRADDR, 0x018) + +/* + * Accesses only reach these read and write functions if the wgChecker + * is blocking them; non-blocked accesses go directly to the downstream + * memory region without passing through this code. + */ +static MemTxResult riscv_wgc_mem_blocked_read(void *opaque, hwaddr addr, + uint64_t *pdata, + unsigned size, MemTxAttrs a= ttrs) +{ + uint32_t wid =3D mem_attrs_to_wid(attrs); + + hwaddr phys_addr =3D addr + region->region_offset; + trace_riscv_wgchecker_mem_blocked_read(phys_addr, size, wid); + + *pdata =3D 0; + return MEMTX_OK; +} + +static MemTxResult riscv_wgc_mem_blocked_write(void *opaque, hwaddr addr, + uint64_t value, + unsigned size, MemTxAttrs a= ttrs) +{ + uint32_t wid =3D mem_attrs_to_wid(attrs); + + hwaddr phys_addr =3D addr + region->region_offset; + trace_riscv_wgchecker_mem_blocked_write(phys_addr, value, size, wid); + + return MEMTX_OK; +} + +static const MemoryRegionOps riscv_wgc_mem_blocked_ops =3D { + .read_with_attrs =3D riscv_wgc_mem_blocked_read, + .write_with_attrs =3D riscv_wgc_mem_blocked_write, + .endianness =3D DEVICE_LITTLE_ENDIAN, + .valid.min_access_size =3D 1, + .valid.max_access_size =3D 8, + .impl.min_access_size =3D 1, + .impl.max_access_size =3D 8, +}; + +static IOMMUTLBEntry riscv_wgc_translate(IOMMUMemoryRegion *iommu, + hwaddr addr, IOMMUAccessFlags fl= ags, + int iommu_idx) +{ + WgCheckerRegion *region =3D container_of(iommu, WgCheckerRegion, upstr= eam); + RISCVWgCheckerState *s =3D RISCV_WGCHECKER(region->wgchecker); + hwaddr phys_addr; + uint64_t region_size; + + IOMMUTLBEntry ret =3D { + .iova =3D addr & ~WG_ALIGNED_MASK, + .translated_addr =3D addr & ~WG_ALIGNED_MASK, + .addr_mask =3D WG_ALIGNED_MASK, + .perm =3D IOMMU_RW, + }; + + /* addr shouldn't exceed region size of down/upstream. */ + region_size =3D memory_region_size(region->downstream); + g_assert(addr < region_size); + + /* + * Look at the wgChecker configuration for this address, and + * return a TLB entry directing the transaction at either + * downstream_as or blocked_io_as, as appropriate. + * For the moment, always permit accesses. + */ + + /* Use physical address instead of offset */ + phys_addr =3D addr + region->region_offset; + + is_success =3D true; + + trace_riscv_wgchecker_translate(phys_addr, flags, + iommu_idx, is_success ? "pass" : "block"); + + ret.target_as =3D is_success ? ®ion->downstream_as : ®ion->block= ed_io_as; + return ret; +} + +static int riscv_wgc_attrs_to_index(IOMMUMemoryRegion *iommu, MemTxAttrs a= ttrs) +{ + return mem_attrs_to_wid(attrs); +} + +static int riscv_wgc_num_indexes(IOMMUMemoryRegion *iommu) +{ + return worldguard_config->nworlds; +} + +static uint64_t riscv_wgchecker_readq(void *opaque, hwaddr addr) +{ + RISCVWgCheckerState *s =3D RISCV_WGCHECKER(opaque); + uint64_t val =3D 0; + + switch (addr) { + case A_ERRCAUSE: + val =3D s->errcause & ERRCAUSE_MASK; + break; + case A_ERRADDR: + val =3D s->erraddr; + break; + case A_NSLOTS: + val =3D s->slot_count; + break; + default: + qemu_log_mask(LOG_GUEST_ERROR, + "%s: Unexpected memory access to (0x%" HWADDR_PRIX + ", %u)\n", __func__, addr, 8); + break; + } + + return val; +} + +static uint64_t riscv_wgchecker_readl(void *opaque, hwaddr addr) +{ + RISCVWgCheckerState *s =3D RISCV_WGCHECKER(opaque); + uint64_t val =3D 0; + + switch (addr) { + case A_VENDOR: + val =3D 0; + break; + case A_IMPID: + val =3D 0; + break; + case A_NSLOTS: + val =3D extract64(s->slot_count, 0, 32); + break; + case A_NSLOTS + 4: + val =3D extract64(s->slot_count, 0, 32); + break; + case A_ERRCAUSE: + val =3D s->errcause & ERRCAUSE_MASK; + val =3D extract64(val, 0, 32); + break; + case A_ERRCAUSE + 4: + val =3D s->errcause & ERRCAUSE_MASK; + val =3D extract64(val, 32, 32); + break; + case A_ERRADDR: + val =3D extract64(s->erraddr, 0, 32); + break; + case A_ERRADDR + 4: + val =3D extract64(s->erraddr, 32, 32); + break; + default: + qemu_log_mask(LOG_GUEST_ERROR, + "%s: Unexpected memory access to (0x%" HWADDR_PRIX + ", %u)\n", __func__, addr, 4); + break; + } + + return val; +} + +static uint64_t riscv_wgchecker_read(void *opaque, hwaddr addr, unsigned s= ize) +{ + uint64_t val =3D 0; + + switch (size) { + case 8: + val =3D riscv_wgchecker_readq(opaque, addr); + break; + case 4: + val =3D riscv_wgchecker_readl(opaque, addr); + break; + default: + qemu_log_mask(LOG_GUEST_ERROR, "%s: Invalid read size %u to wgChec= ker\n", + __func__, size); + return 0; + } + + return val; +} + +static void riscv_wgchecker_writeq(void *opaque, hwaddr addr, + uint64_t value) +{ + RISCVWgCheckerState *s =3D RISCV_WGCHECKER(opaque); + + switch (addr) { + case A_ERRCAUSE: + s->errcause =3D value & ERRCAUSE_MASK; + break; + case A_ERRADDR: + s->erraddr =3D value; + break; + default: + qemu_log_mask(LOG_GUEST_ERROR, + "%s: Unexpected memory access to (0x%" HWADDR_PRIX + ", %u)\n", __func__, addr, 8); + break; + } +} + +static void riscv_wgchecker_writel(void *opaque, hwaddr addr, + uint64_t value) +{ + RISCVWgCheckerState *s =3D RISCV_WGCHECKER(opaque); + + switch (addr) { + case A_ERRCAUSE: + value &=3D extract64(ERRCAUSE_MASK, 0, 32); + s->errcause =3D deposit64(s->errcause, 0, 32, value); + break; + case A_ERRCAUSE + 4: + value &=3D extract64(ERRCAUSE_MASK, 32, 32); + s->errcause =3D deposit64(s->errcause, 32, 32, value); + break; + case A_ERRADDR: + s->erraddr =3D deposit64(s->erraddr, 0, 32, value); + break; + case A_ERRADDR + 4: + s->erraddr =3D deposit64(s->erraddr, 32, 32, value); + break; + default: + qemu_log_mask(LOG_GUEST_ERROR, + "%s: Unexpected memory access to (0x%" HWADDR_PRIX + ", %u)\n", __func__, addr, 4); + break; + } +} + +static void riscv_wgchecker_write(void *opaque, hwaddr addr, + uint64_t value, unsigned size) +{ + switch (size) { + case 8: + riscv_wgchecker_writeq(opaque, addr, value); + break; + case 4: + riscv_wgchecker_writel(opaque, addr, value); + break; + default: + qemu_log_mask(LOG_GUEST_ERROR, "%s: Invalid write size %u to " + "wgChecker\n", __func__, size); + break; + } +} + +static MemTxResult riscv_wgchecker_read_with_attrs( + void *opaque, hwaddr addr, uint64_t *pdata, unsigned size, + MemTxAttrs attrs) +{ + SysBusDevice *dev =3D SYS_BUS_DEVICE(opaque); + + trace_riscv_wgchecker_mmio_read(dev->mmio[0].addr, addr, size); + + *pdata =3D 0; + if (could_access_wgblocks(attrs, "wgChecker")) { + *pdata =3D riscv_wgchecker_read(opaque, addr, size); + } + + return MEMTX_OK; +} + +static MemTxResult riscv_wgchecker_write_with_attrs( + void *opaque, hwaddr addr, uint64_t data, unsigned size, + MemTxAttrs attrs) +{ + SysBusDevice *dev =3D SYS_BUS_DEVICE(opaque); + + trace_riscv_wgchecker_mmio_write(dev->mmio[0].addr, addr, size, data); + + if (could_access_wgblocks(attrs, "wgChecker")) { + riscv_wgchecker_write(opaque, addr, data, size); + } + + return MEMTX_OK; +} + +static const MemoryRegionOps riscv_wgchecker_ops =3D { + .read_with_attrs =3D riscv_wgchecker_read_with_attrs, + .write_with_attrs =3D riscv_wgchecker_write_with_attrs, + .endianness =3D DEVICE_NATIVE_ENDIAN, + .valid =3D { + .min_access_size =3D 4, + .max_access_size =3D 8 + }, + .impl =3D { + .min_access_size =3D 4, + .max_access_size =3D 8 + } +}; + +static void riscv_wgc_iommu_memory_region_class_init(ObjectClass *klass, + const void *data) +{ + IOMMUMemoryRegionClass *imrc =3D IOMMU_MEMORY_REGION_CLASS(klass); + + imrc->translate =3D riscv_wgc_translate; + imrc->attrs_to_index =3D riscv_wgc_attrs_to_index; + imrc->num_indexes =3D riscv_wgc_num_indexes; +} + +static const TypeInfo riscv_wgc_iommu_memory_region_info =3D { + .name =3D TYPE_RISCV_WGC_IOMMU_MEMORY_REGION, + .parent =3D TYPE_IOMMU_MEMORY_REGION, + .class_init =3D riscv_wgc_iommu_memory_region_class_init, +}; + + +#define DEFINE_REGION(N) \ + DEFINE_PROP_LINK("downstream-mr[" #N "]", RISCVWgCheckerState, \ + mem_regions[N].downstream, \ + TYPE_MEMORY_REGION, MemoryRegion *), \ + DEFINE_PROP_UINT64("region-offset[" #N "]", RISCVWgCheckerState, \ + mem_regions[N].region_offset, 0) \ + +static Property riscv_wgchecker_properties[] =3D { + DEFINE_PROP_UINT32("slot-count", RISCVWgCheckerState, slot_count, 0x1), + DEFINE_PROP_UINT32("mmio-size", RISCVWgCheckerState, mmio_size, 0x1000= ), + + /* Assume 1 wgChecker has 64 regions at maximum (WGC_NUM_REGIONS). */ + DEFINE_REGION(0), DEFINE_REGION(1), DEFINE_REGION(2), DEFINE_REGION(3), + DEFINE_REGION(4), DEFINE_REGION(5), DEFINE_REGION(6), DEFINE_REGION(7), + DEFINE_REGION(8), DEFINE_REGION(9), DEFINE_REGION(10), DEFINE_REGION(1= 1), + DEFINE_REGION(12), DEFINE_REGION(13), DEFINE_REGION(14), DEFINE_REGION= (15), + DEFINE_REGION(16), DEFINE_REGION(17), DEFINE_REGION(18), DEFINE_REGION= (19), + DEFINE_REGION(20), DEFINE_REGION(21), DEFINE_REGION(22), DEFINE_REGION= (23), + DEFINE_REGION(24), DEFINE_REGION(25), DEFINE_REGION(26), DEFINE_REGION= (27), + DEFINE_REGION(28), DEFINE_REGION(29), DEFINE_REGION(30), DEFINE_REGION= (31), + DEFINE_REGION(32), DEFINE_REGION(33), DEFINE_REGION(34), DEFINE_REGION= (35), + DEFINE_REGION(36), DEFINE_REGION(37), DEFINE_REGION(38), DEFINE_REGION= (39), + DEFINE_REGION(40), DEFINE_REGION(41), DEFINE_REGION(42), DEFINE_REGION= (43), + DEFINE_REGION(44), DEFINE_REGION(45), DEFINE_REGION(46), DEFINE_REGION= (47), + DEFINE_REGION(48), DEFINE_REGION(49), DEFINE_REGION(50), DEFINE_REGION= (51), + DEFINE_REGION(52), DEFINE_REGION(53), DEFINE_REGION(54), DEFINE_REGION= (55), + DEFINE_REGION(56), DEFINE_REGION(57), DEFINE_REGION(58), DEFINE_REGION= (59), + DEFINE_REGION(60), DEFINE_REGION(61), DEFINE_REGION(62), DEFINE_REGION= (63), + + DEFINE_PROP_UINT64("addr-range-start", RISCVWgCheckerState, + addr_range_start, 0), + DEFINE_PROP_UINT64("addr-range-size", RISCVWgCheckerState, + addr_range_size, UINT64_MAX), + + /* + * We could only set individual wgChecker to hw-bypass mode. It is + * usually used in wgChecker of BootROM, since SW has no way to enable + * the permission of it. + */ + DEFINE_PROP_BOOL("hw-bypass", RISCVWgCheckerState, hw_bypass, false), +}; + +static int int_log2_down(int n) +{ + int i =3D 0; + + n >>=3D 1; + + while (n) { + i++; + n >>=3D 1; + } + + return i; +} + +static int int_log2_up(int n) +{ + return int_log2_down(n - 1) + 1; +} + +/* + * Change the address range to be NAPOT alignment. + * + * New address range should totally cover the origin range, but new range + * should be configured by 1 NAPOT region (slot). + */ +static void address_range_align_napot(RISCVWgCheckerState *s) +{ + uint64_t start, end, size, new_size; + + start =3D s->addr_range_start; + end =3D s->addr_range_start + s->addr_range_size; + size =3D s->addr_range_size; + + if (size =3D=3D UINT64_MAX) { + /* Full address range. No need of NAPOT alignment. */ + return; + } + + /* Size is the next power-of-2 number. */ + size =3D 1 << (int_log2_up(size)); + start =3D QEMU_ALIGN_DOWN(start, size); + end =3D QEMU_ALIGN_UP(end, size); + new_size =3D end - start; + + /* + * If base is not aligned to region size (new_size), + * double the region size and try it again. + */ + while ((new_size !=3D size) && (size !=3D 1ULL << 63)) { + size *=3D 2; + start =3D QEMU_ALIGN_DOWN(start, size); + end =3D QEMU_ALIGN_UP(end, size); + new_size =3D end - start; + } + + s->addr_range_start =3D start; + s->addr_range_size =3D size; +} + +static void riscv_wgchecker_realize(DeviceState *dev, Error **errp) +{ + Object *obj =3D OBJECT(dev); + RISCVWgCheckerState *s =3D RISCV_WGCHECKER(dev); + uint64_t size; + + if (worldguard_config =3D=3D NULL) { + error_setg(errp, "Couldn't find global WorldGuard configs. " + "Please realize %s device first.", + TYPE_RISCV_WORLDGUARD); + return; + } + + if (s->slot_count =3D=3D 0) { + error_setg(errp, "wgChecker slot-count couldn't be zero."); + return; + } + + memory_region_init_io(&s->mmio, OBJECT(dev), &riscv_wgchecker_ops, s, + TYPE_RISCV_WGCHECKER, s->mmio_size); + sysbus_init_mmio(SYS_BUS_DEVICE(dev), &s->mmio); + sysbus_init_irq(SYS_BUS_DEVICE(dev), &s->irq); + + /* Address range should be NAPOT alignment */ + address_range_align_napot(s); + + for (int i =3D 0; i < WGC_NUM_REGIONS; i++) { + WgCheckerRegion *region =3D &s->mem_regions[i]; + + if (!region->downstream) { + continue; + } + region->wgchecker =3D s; + + const char *upstream_name =3D g_strdup_printf( + "wgchecker-upstream-%"HWADDR_PRIx, region->region_offset); + const char *downstream_name =3D g_strdup_printf( + "wgchecker-downstream-%"HWADDR_PRIx, region->region_offset); + + size =3D memory_region_size(region->downstream); + memory_region_init_iommu(®ion->upstream, sizeof(region->upstrea= m), + TYPE_RISCV_WGC_IOMMU_MEMORY_REGION, + obj, upstream_name, size); + + /* upstream MRs are 2nd ~ (n+1)th MemoryRegion. */ + sysbus_init_mmio(SYS_BUS_DEVICE(dev), MEMORY_REGION(®ion->upstr= eam)); + + /* + * This memory region is not exposed to users of this device as a + * sysbus MMIO region, but is instead used internally as something + * that our IOMMU translate function might direct accesses to. + */ + memory_region_init_io(®ion->blocked_io, obj, &riscv_wgc_mem_blo= cked_ops, + region, "wgchecker-blocked-io", size); + + address_space_init(®ion->downstream_as, region->downstream, + downstream_name); + address_space_init(®ion->blocked_io_as, ®ion->blocked_io, + "wgchecker-blocked-io"); + } +} + +static void riscv_wgchecker_unrealize(DeviceState *dev) +{ + RISCVWgCheckerState *s =3D RISCV_WGCHECKER(dev); + + g_free(s->slots); + if (s->num_default_slots && s->default_slots) { + g_free(s->default_slots); + } +} + +static void riscv_wgchecker_reset_enter(Object *obj, ResetType type) +{ + RISCVWgCheckerState *s =3D RISCV_WGCHECKER(obj); + uint64_t start =3D s->addr_range_start; + uint64_t end =3D s->addr_range_start + s->addr_range_size; + int nslots =3D s->slot_count; + + s->errcause =3D 0; + s->erraddr =3D 0; +} + +static void riscv_wgchecker_class_init(ObjectClass *klass, const void *dat= a) +{ + DeviceClass *dc =3D DEVICE_CLASS(klass); + + device_class_set_props(dc, riscv_wgchecker_properties); + dc->user_creatable =3D true; + dc->realize =3D riscv_wgchecker_realize; + dc->unrealize =3D riscv_wgchecker_unrealize; + ResettableClass *rc =3D RESETTABLE_CLASS(klass); + rc->phases.enter =3D riscv_wgchecker_reset_enter; +} + +static void riscv_wgchecker_instance_init(Object *obj) +{ + RISCVWgCheckerState *s =3D RISCV_WGCHECKER(obj); + + s->num_default_slots =3D 0; +} + +static const TypeInfo riscv_wgchecker_info =3D { + .name =3D TYPE_RISCV_WGCHECKER, + .parent =3D TYPE_SYS_BUS_DEVICE, + .instance_init =3D riscv_wgchecker_instance_init, + .instance_size =3D sizeof(RISCVWgCheckerState), + .class_init =3D riscv_wgchecker_class_init, +}; + +static void riscv_wgchecker_register_types(void) +{ + type_register_static(&riscv_wgchecker_info); + type_register_static(&riscv_wgc_iommu_memory_region_info); +} + +type_init(riscv_wgchecker_register_types) + +/* + * Create WgChecker device + */ +DeviceState *riscv_wgchecker_create(hwaddr addr, uint32_t size, + qemu_irq irq, uint32_t slot_count, + uint64_t addr_range_start, + uint64_t addr_range_size, + uint32_t num_of_region, + MemoryRegion **downstream, + uint64_t *region_offset, + uint32_t num_default_slots, + WgCheckerSlot *default_slots) +{ + DeviceState *dev =3D qdev_new(TYPE_RISCV_WGCHECKER); + RISCVWgCheckerState *s =3D RISCV_WGCHECKER(dev); + char name_mr[32]; + char name_offset[32]; + int i; + + qdev_prop_set_uint32(dev, "slot-count", slot_count); + qdev_prop_set_uint32(dev, "mmio-size", size); + qdev_prop_set_uint64(dev, "addr-range-start", addr_range_start); + if (addr_range_size) { + qdev_prop_set_uint64(dev, "addr-range-size", addr_range_size); + } + + g_assert(num_of_region <=3D WGC_NUM_REGIONS); + for (i =3D 0; i < num_of_region; i++) { + snprintf(name_mr, 32, "downstream-mr[%d]", i); + snprintf(name_offset, 32, "region-offset[%d]", i); + + object_property_set_link(OBJECT(dev), name_mr, + OBJECT(downstream[i]), &error_fatal); + qdev_prop_set_uint64(dev, name_offset, region_offset[i]); + } + + sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal); + sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, addr); + sysbus_connect_irq(SYS_BUS_DEVICE(dev), 0, irq); + return dev; +} diff --git a/hw/misc/trace-events b/hw/misc/trace-events index eeb9243898..7617b8843f 100644 --- a/hw/misc/trace-events +++ b/hw/misc/trace-events @@ -409,3 +409,11 @@ ivshmem_flat_interrupt_peer(uint16_t peer_id, uint16_t= vector_id) "Interrupting i2c_echo_event(const char *id, const char *event) "%s: %s" i2c_echo_recv(const char *id, uint8_t data) "%s: recv 0x%02" PRIx8 i2c_echo_send(const char *id, uint8_t data) "%s: send 0x%02" PRIx8 + +# riscv_worldguard.c +riscv_wgchecker_mmio_read(uint64_t base, uint64_t offset, unsigned int siz= e) "base =3D 0x%" PRIx64 ", offset =3D 0x%" PRIx64 ", size =3D 0x%x" +riscv_wgchecker_mmio_write(uint64_t base, uint64_t offset, unsigned int si= ze, uint64_t val) "base =3D 0x%" PRIx64 ", offset =3D 0x%" PRIx64 ", size = =3D 0x%x, val =3D 0x%" PRIx64 + +riscv_wgchecker_mem_blocked_read(uint64_t phys_addr, unsigned size, uint32= _t wid) "phys_addr =3D 0x%" PRIx64 ", size =3D %u, wid =3D %" PRIu32 +riscv_wgchecker_mem_blocked_write(uint64_t phys_addr, uint64_t data, unsig= ned size, uint32_t wid) "phys_addr =3D 0x%" PRIx64 ", data =3D 0x%" PRIx64 = ", size =3D %u, wid =3D %" PRIu32 +riscv_wgchecker_translate(uint64_t addr, int flags, int wid, const char *r= es) "addr =3D 0x%016" PRIx64 ", flags =3D 0x%x, wid =3D %d: %s" diff --git a/include/hw/misc/riscv_worldguard.h b/include/hw/misc/riscv_wor= ldguard.h index fb08c92f46..bb61d372f0 100644 --- a/include/hw/misc/riscv_worldguard.h +++ b/include/hw/misc/riscv_worldguard.h @@ -54,4 +54,67 @@ void wid_to_mem_attrs(MemTxAttrs *attrs, uint32_t wid); uint32_t mem_attrs_to_wid(MemTxAttrs attrs); bool could_access_wgblocks(MemTxAttrs attrs, const char *wgblock); =20 +#define TYPE_RISCV_WGCHECKER "riscv.wgchecker" + +typedef struct RISCVWgCheckerState RISCVWgCheckerState; +DECLARE_INSTANCE_CHECKER(RISCVWgCheckerState, RISCV_WGCHECKER, + TYPE_RISCV_WGCHECKER) + +#define TYPE_RISCV_WGC_IOMMU_MEMORY_REGION "riscv-wgc-iommu-memory-regi= on" + +typedef struct WgCheckerSlot WgCheckerSlot; +struct WgCheckerSlot { + uint64_t addr; + uint64_t perm; + uint32_t cfg; +}; + +typedef struct WgCheckerRegion WgCheckerRegion; +struct WgCheckerRegion { + MemoryRegion *downstream; + uint64_t region_offset; + + IOMMUMemoryRegion upstream; + MemoryRegion blocked_io; + AddressSpace downstream_as; + AddressSpace blocked_io_as; + + RISCVWgCheckerState *wgchecker; +}; + +#define WGC_NUM_REGIONS 64 + +struct RISCVWgCheckerState { + /*< private >*/ + SysBusDevice parent_obj; + + /*< public >*/ + MemoryRegion mmio; + qemu_irq irq; + + /* error reg */ + uint64_t errcause; + uint64_t erraddr; + + /* Memory regions protected by wgChecker */ + WgCheckerRegion mem_regions[WGC_NUM_REGIONS]; + + /* Property */ + uint32_t slot_count; /* nslots */ + uint32_t mmio_size; + uint64_t addr_range_start; + uint64_t addr_range_size; + bool hw_bypass; +}; + +DeviceState *riscv_wgchecker_create(hwaddr addr, uint32_t size, + qemu_irq irq, uint32_t slot_count, + uint64_t addr_range_start, + uint64_t addr_range_size, + uint32_t num_of_region, + MemoryRegion **downstream, + uint64_t *region_offset, + uint32_t num_default_slots, + WgCheckerSlot *default_slots); + #endif --=20 2.43.0 From nobody Fri Nov 14 17:01:12 2025 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; dmarc=pass(p=reject dis=none) header.from=sifive.com ARC-Seal: i=1; a=rsa-sha256; t=1761063789; cv=none; d=zohomail.com; s=zohoarc; b=HRT1TMUmPBkfjKH9DURlZppV6zl3BVgYHm6/u+v/DrJ590Vkw4QcVtSoRxRwnkPviUsD1348sz4RYp/hLrro2eg/by0ChbHcpO7WgaLA7t5owxzlnQxmP5cYsjch31njxhSBErTpdthVwXAnuvymrJYz2ARyhWsDrDri7vQbmF4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1761063789; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=QExG5RYdnkMTLteuWV+itmwS5ytsioDmS+hzud2jQvE=; b=DTl+5wTrUQHxtXYmWkqIq3GrjlH2f6cBcNv0/8O2K/GiiGFj2WCxr0y4a21ZpLHfOl4rK4kP5mKZZwbjoybtuGZZD8H+dv4exr31J8PMQ+i8D1QU9Gl7SmmNMjBVvFgZlH96FzkVmPEkDT7YPECtyPKpkJY2QGueR1uOqUQSaz8= 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; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1761063789635354.3756375838228; Tue, 21 Oct 2025 09:23:09 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1vBF7L-0004PE-T6; Tue, 21 Oct 2025 12:21:49 -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 1vBF7A-0004MZ-Fk for qemu-devel@nongnu.org; Tue, 21 Oct 2025 12:21:38 -0400 Received: from mail-pf1-x42b.google.com ([2607:f8b0:4864:20::42b]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1vBF6v-0002Xw-LZ for qemu-devel@nongnu.org; Tue, 21 Oct 2025 12:21:26 -0400 Received: by mail-pf1-x42b.google.com with SMTP id d2e1a72fcca58-781251eec51so4588197b3a.3 for ; Tue, 21 Oct 2025 09:21:20 -0700 (PDT) Received: from hsinchu26.internal.sifive.com ([210.176.154.34]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7a23011088fsm11720280b3a.65.2025.10.21.09.21.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Oct 2025 09:21:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1761063678; x=1761668478; 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=QExG5RYdnkMTLteuWV+itmwS5ytsioDmS+hzud2jQvE=; b=FY/FFndEVDH87QnBPb6eZAHKrO8Cw7U3LAcJQcqVIEMkavFCcyiZz0bXm1+Qz/HDR1 rs1P5v4nOvMdkHicfUCjHcyqNmMFuT6rw6XBsk92t0HixsZzWkzymxXBjJvMWV5I7FFT duMmuUKFHs87mMYddFea7z/WbjczWXzNpWzXr/DVY0s0OgIQv7kj/bfKuc64pXKpZMuG 67MBaZEJTEUBoW70+BOckaJYI60108li819s89t8IoHHUg69aiktbw6v0m1fw2By1OsZ jps6f86nDEV1AW1uiHThkGYS4ugx2qA683IQQxA7xoVv+dhMMZjxeylWjwqCBODZTwbN UFBA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1761063678; x=1761668478; 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=QExG5RYdnkMTLteuWV+itmwS5ytsioDmS+hzud2jQvE=; b=DVyGyetW9cotxEznLr6ZB9nxCd94RXvrfNVomfWSKYUVc1lCItr7OUKchQ6SXypZ8A kWqjDgc6XUEFkYjkdKIN6Mzg869EaD4chG5OtJMabaCdaUwEE4i8Ys0V+LXdNAGJEzWG 27njTGZkslZjahWs/vCSJIP9Kq4eC57FqptiFiobxk8x2pgG0tJ4S9wAxxv0rXvUVUTW 8ut2TmNlXSSiGExwatx33tdZ/lyLh6ijHQvm08w4b+uLz2VHXRJdVALc614f0lTnH+EI Va1MBVMgqefmF4Qibvk221RSOtsZZdvRmLeXBY7gUKJPBhYpv2HFmJKhJwrxSljMeDTo SPIw== X-Gm-Message-State: AOJu0Yxwu0P3mVBtviF4Br8t6l4sLCZxLJhaFobfvnv0cFIYpeCA3axo H28LbyVc2gCXDPuCmNC6goBar+jNl5dfnTKM7hkMJbyoTJDVFvN2TbFuOZANzCsW1zVb85Gqfjj 4E7EPljarWnkGnCQPvOrB43m/LzcrSQHRtDDwwFe411lo7mZngXkzT+e38wrfvY9Uq6dx4gPJQ7 sDy5KWn4b1DVJkStc+XuOB/gIHBlItUwsAvm+Eew== X-Gm-Gg: ASbGnctClD2If2SevdaFnU8DV55Pt+KVzcunMSznNi5jjvyhBDlGNdGKiTs1ifY6fr4 vX8lRGlrECRM72E4ewoy/vIVTboE8G+AL8E/wyS2EA3mujRWGJB1vS7+uHk54xO1DIqqefzkZN/ ex7Bzm6PDxNbRawaSzwz9rxE9IqlWCesL8xaGeF4AIAAMP6/cKbGpd+S3wxhwqMuLV8jk9HFgNz Z0F8h5mHgRmTMtWLWzBwK7Lu8YVG3edYTHOPBmJUeQn09BGVH5zGrSv0G7WpWgfClq/UfUHX5cR saxbklUrfW8JWMjTSbbs6p2+3FnbcFEYhsIV0DC43n8r10qePc/KLmlB2gMv9QoMZ/8OTKr1Kr1 ZML4F6rIq9+U9rOpOuPF2aEOC1WxHQFfWSTYJWlh5RgaMVwaVKfSVS/YsxnSmT37ZYcjLQIH981 7otAYqlJCSAK28F9Q48rx4Ueta83xjeWu6 X-Google-Smtp-Source: AGHT+IF7StEAPlU32UATkPOsgxdPNgCioRqkHyTr0rdsJCLkYDyFURYSV64IOK4x90ICugdCUZxpiQ== X-Received: by 2002:a05:6a00:2d8b:b0:772:3aa4:226e with SMTP id d2e1a72fcca58-7a220b10796mr19937408b3a.19.1761063677554; Tue, 21 Oct 2025 09:21:17 -0700 (PDT) From: Jim Shu To: qemu-devel@nongnu.org, qemu-riscv@nongnu.org Cc: Daniel Henrique Barboza , Paolo Bonzini , Palmer Dabbelt , Alistair Francis , Weiwei Li , Liu Zhiwei , Jim Shu Subject: [PATCH v3 15/18] hw/misc: riscv_wgchecker: Implement wgchecker slot registers Date: Wed, 22 Oct 2025 00:21:05 +0800 Message-ID: <20251021162108.585468-1-jim.shu@sifive.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20251021155548.584543-1-jim.shu@sifive.com> References: <20251021155548.584543-1-jim.shu@sifive.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: pass client-ip=2607:f8b0:4864:20::42b; envelope-from=jim.shu@sifive.com; helo=mail-pf1-x42b.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable 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 @sifive.com) X-ZM-MESSAGEID: 1761063791555158500 Content-Type: text/plain; charset="utf-8" wgChecker slot is similar to PMP region. SW could program each slot to configure the permission of address range. Signed-off-by: Jim Shu Reviewed-by: Daniel Henrique Barboza --- hw/misc/riscv_wgchecker.c | 337 +++++++++++++++++++++++++++++ hw/misc/riscv_worldguard.c | 3 + include/hw/misc/riscv_worldguard.h | 4 + 3 files changed, 344 insertions(+) diff --git a/hw/misc/riscv_wgchecker.c b/hw/misc/riscv_wgchecker.c index fed6a14fbd..99439a1c68 100644 --- a/hw/misc/riscv_wgchecker.c +++ b/hw/misc/riscv_wgchecker.c @@ -52,6 +52,52 @@ REG64(ERRCAUSE, 0x010) R_ERRCAUSE_IP_MASK) =20 REG64(ERRADDR, 0x018) +REG64(WGC_SLOT, 0x020) + +/* wgChecker slots */ +REG64(SLOT_ADDR, 0x000) +REG64(SLOT_PERM, 0x008) +REG32(SLOT_CFG, 0x010) + FIELD(SLOT_CFG, A, 0, 2) + FIELD(SLOT_CFG, ER, 8, 1) + FIELD(SLOT_CFG, EW, 9, 1) + FIELD(SLOT_CFG, IR, 10, 1) + FIELD(SLOT_CFG, IW, 11, 1) + FIELD(SLOT_CFG, LOCK, 31, 1) + +#define SLOT_SIZE 0x020 + +#define SLOT0_CFG_MASK \ + (R_SLOT_CFG_ER_MASK | \ + R_SLOT_CFG_EW_MASK | \ + R_SLOT_CFG_IR_MASK | \ + R_SLOT_CFG_IW_MASK | \ + R_SLOT_CFG_LOCK_MASK) + +#define SLOT_CFG_MASK \ + (R_SLOT_CFG_A_MASK | (SLOT0_CFG_MASK)) + +#define WGC_SLOT_END(nslots) \ + (A_WGC_SLOT + SLOT_SIZE * (nslots + 1)) + +/* wgChecker slot is 4K alignment */ +#define WG_ALIGNED_SIZE (1 << 12) +#define WG_ALIGNED_MASK MAKE_64BIT_MASK(0, 12) + +/* wgChecker slot address is (addr / 4). */ +#define TO_SLOT_ADDR(addr) ((addr) >> 2) +#define FROM_SLOT_ADDR(addr) ((addr) << 2) + +/* wgChecker slot cfg.A[1:0] */ +#define A_OFF 0 +#define A_TOR 1 +#define A_NA4 2 +#define A_NAPOT 3 + +/* wgChecker slot perm */ +#define WGC_PERM(wid, perm) ((uint64_t)(perm) << (2 * (wid))) +#define P_READ (1 << 0) +#define P_WRITE (1 << 1) =20 /* * Accesses only reach these read and write functions if the wgChecker @@ -147,6 +193,28 @@ static uint64_t riscv_wgchecker_readq(void *opaque, hw= addr addr) RISCVWgCheckerState *s =3D RISCV_WGCHECKER(opaque); uint64_t val =3D 0; =20 + if ((addr >=3D A_WGC_SLOT) && (addr < WGC_SLOT_END(s->slot_count))) { + /* Read from WGC slot */ + int slot_id =3D (addr - A_WGC_SLOT) / SLOT_SIZE; + int slot_offset =3D (addr - A_WGC_SLOT) % SLOT_SIZE; + + switch (slot_offset) { + case A_SLOT_ADDR: + val =3D s->slots[slot_id].addr; + break; + case A_SLOT_PERM: + val =3D s->slots[slot_id].perm; + break; + default: + qemu_log_mask(LOG_GUEST_ERROR, + "%s: Unexpected memory access to (0x%" HWADDR_PR= IX + ", %u)\n", __func__, addr, 8); + break; + } + + return val; + } + switch (addr) { case A_ERRCAUSE: val =3D s->errcause & ERRCAUSE_MASK; @@ -172,6 +240,37 @@ static uint64_t riscv_wgchecker_readl(void *opaque, hw= addr addr) RISCVWgCheckerState *s =3D RISCV_WGCHECKER(opaque); uint64_t val =3D 0; =20 + if ((addr >=3D A_WGC_SLOT) && (addr < WGC_SLOT_END(s->slot_count))) { + /* Read from WGC slot */ + int slot_id =3D (addr - A_WGC_SLOT) / SLOT_SIZE; + int slot_offset =3D (addr - A_WGC_SLOT) % SLOT_SIZE; + + switch (slot_offset) { + case A_SLOT_ADDR: + val =3D extract64(s->slots[slot_id].addr, 0, 32); + break; + case A_SLOT_ADDR + 4: + val =3D extract64(s->slots[slot_id].addr, 32, 32); + break; + case A_SLOT_PERM: + val =3D extract64(s->slots[slot_id].perm, 0, 32); + break; + case A_SLOT_PERM + 4: + val =3D extract64(s->slots[slot_id].perm, 32, 32); + break; + case A_SLOT_CFG: + val =3D s->slots[slot_id].cfg & SLOT_CFG_MASK; + break; + default: + qemu_log_mask(LOG_GUEST_ERROR, + "%s: Unexpected memory access to (0x%" HWADDR_PR= IX + ", %u)\n", __func__, addr, 4); + break; + } + + return val; + } + switch (addr) { case A_VENDOR: val =3D 0; @@ -229,11 +328,121 @@ static uint64_t riscv_wgchecker_read(void *opaque, h= waddr addr, unsigned size) return val; } =20 +/* + * Validate the WGC slot address is between address range. + * + * Fix the slot address to the start address if it's not within the addres= s range. + * We need validation when changing "slot address" or "TOR/NAPOT mode (cfg= .A)" + */ +static void validate_slot_address(void *opaque, int slot_id) +{ + RISCVWgCheckerState *s =3D RISCV_WGCHECKER(opaque); + uint64_t start =3D TO_SLOT_ADDR(s->addr_range_start); + uint64_t end =3D TO_SLOT_ADDR(s->addr_range_start + s->addr_range_size= ); + uint32_t cfg_a =3D FIELD_EX32(s->slots[slot_id].cfg, SLOT_CFG, A); + + /* First and last slot address are hard-coded. */ + if ((slot_id =3D=3D 0) || (slot_id =3D=3D s->slot_count)) { + return; + } + + /* Check WGC slot address is between address range. */ + if ((s->slots[slot_id].addr < start) || (s->slots[slot_id].addr >=3D e= nd)) { + s->slots[slot_id].addr =3D start; + } + + /* Check WGC slot is 4k-aligned. */ + if (cfg_a =3D=3D A_TOR) { + s->slots[slot_id].addr &=3D ~TO_SLOT_ADDR(WG_ALIGNED_MASK); + } else if (cfg_a =3D=3D A_NAPOT) { + s->slots[slot_id].addr |=3D TO_SLOT_ADDR(WG_ALIGNED_MASK >> 1); + } else if (cfg_a =3D=3D A_NA4) { + /* Forcely replace NA4 slot with 4K-aligned NAPOT slot. */ + FIELD_DP32(s->slots[slot_id].cfg, SLOT_CFG, A, A_NAPOT); + s->slots[slot_id].addr |=3D TO_SLOT_ADDR(WG_ALIGNED_MASK >> 1); + } +} + +static bool slots_reg_is_ro(int slot_id, int slot_offset, uint32_t nslots) +{ + /* + * Special slots: + * - slot[0]: + * - addr is RO + * - perm is RO + * - cfg.A is OFF + * + * - slot[nslots]: + * - addr is RO + * - cfg.A is OFF or TOR + */ + if (slot_id =3D=3D 0) { + switch (slot_offset) { + case A_SLOT_ADDR: + case A_SLOT_ADDR + 4: + case A_SLOT_PERM: + case A_SLOT_PERM + 4: + return true; + default: + break; + } + } else if (slot_id =3D=3D nslots) { + switch (slot_offset) { + case A_SLOT_ADDR: + case A_SLOT_ADDR + 4: + return true; + default: + break; + } + } + + return false; +} + static void riscv_wgchecker_writeq(void *opaque, hwaddr addr, uint64_t value) { RISCVWgCheckerState *s =3D RISCV_WGCHECKER(opaque); =20 + if ((addr >=3D A_WGC_SLOT) && (addr < WGC_SLOT_END(s->slot_count))) { + /* Read from WGC slot */ + int slot_id =3D (addr - A_WGC_SLOT) / SLOT_SIZE; + int slot_offset =3D (addr - A_WGC_SLOT) % SLOT_SIZE; + bool locked =3D FIELD_EX32(s->slots[slot_id].cfg, SLOT_CFG, LOCK); + + if (locked) { + qemu_log_mask(LOG_GUEST_ERROR, + "%s: Couldn't write access to locked wgChecker S= lot: " + "slot =3D %d, offset =3D %d\n", __func__, slot_i= d, + slot_offset); + return; + } + + if (slots_reg_is_ro(slot_id, slot_offset, s->slot_count)) { + qemu_log_mask(LOG_GUEST_ERROR, + "%s: Couldn't write access to RO reg (0x%" + HWADDR_PRIX ", %u)n", __func__, addr, 8); + } + + switch (slot_offset) { + case A_SLOT_ADDR: + s->slots[slot_id].addr =3D value; + validate_slot_address(s, slot_id); + break; + case A_SLOT_PERM: + value &=3D wgc_slot_perm_mask; + s->slots[slot_id].perm =3D value; + break; + default: + qemu_log_mask(LOG_GUEST_ERROR, + "%s: Unexpected memory access to (0x%" HWADDR_PR= IX + ", %u)\n", __func__, addr, 8); + break; + } + + return; + } + switch (addr) { case A_ERRCAUSE: s->errcause =3D value & ERRCAUSE_MASK; @@ -254,6 +463,81 @@ static void riscv_wgchecker_writel(void *opaque, hwadd= r addr, { RISCVWgCheckerState *s =3D RISCV_WGCHECKER(opaque); =20 + if ((addr >=3D A_WGC_SLOT) && (addr < WGC_SLOT_END(s->slot_count))) { + /* Write to WGC slot */ + int slot_id =3D (addr - A_WGC_SLOT) / SLOT_SIZE; + int slot_offset =3D (addr - A_WGC_SLOT) % SLOT_SIZE; + bool locked =3D FIELD_EX32(s->slots[slot_id].cfg, SLOT_CFG, LOCK); + int cfg_a, old_cfg_a; + + if (locked) { + qemu_log_mask(LOG_GUEST_ERROR, + "%s: Couldn't write access to locked wgChecker S= lot: " + "slot =3D %d, offset =3D %d\n", __func__, slot_i= d, + slot_offset); + return; + } + + if (slots_reg_is_ro(slot_id, slot_offset, s->slot_count)) { + qemu_log_mask(LOG_GUEST_ERROR, + "%s: Unexpected memory access to (0x%" HWADDR_PR= IX + ", %u)\n", __func__, addr, 4); + } + + switch (slot_offset) { + case A_SLOT_ADDR: + s->slots[slot_id].addr =3D deposit64( + s->slots[slot_id].addr, 0, 32, value); + validate_slot_address(s, slot_id); + break; + case A_SLOT_ADDR + 4: + s->slots[slot_id].addr =3D deposit64( + s->slots[slot_id].addr, 32, 32, value); + validate_slot_address(s, slot_id); + break; + case A_SLOT_PERM: + value &=3D wgc_slot_perm_mask; + s->slots[slot_id].perm =3D deposit64( + s->slots[slot_id].perm, 0, 32, value); + break; + case A_SLOT_PERM + 4: + value &=3D extract64(wgc_slot_perm_mask, 32, 32); + s->slots[slot_id].perm =3D deposit64( + s->slots[slot_id].perm, 32, 32, value); + break; + case A_SLOT_CFG: + if (slot_id =3D=3D 0) { + value &=3D SLOT0_CFG_MASK; + s->slots[0].cfg =3D value; + } else if (slot_id =3D=3D s->slot_count) { + old_cfg_a =3D FIELD_EX32(s->slots[s->slot_count].cfg, SLOT= _CFG, A); + cfg_a =3D FIELD_EX32(value, SLOT_CFG, A); + + value &=3D SLOT0_CFG_MASK; + if ((cfg_a =3D=3D A_OFF) || (cfg_a =3D=3D A_TOR)) { + value |=3D cfg_a; + } else { + /* slot[nslots] could only use OFF or TOR config. */ + value |=3D old_cfg_a; + } + s->slots[s->slot_count].cfg =3D value; + + validate_slot_address(s, slot_id); + } else { + value &=3D SLOT_CFG_MASK; + s->slots[slot_id].cfg =3D value; + } + break; + default: + qemu_log_mask(LOG_GUEST_ERROR, + "%s: Unexpected memory access to (0x%" HWADDR_PR= IX + ", %u)\n", __func__, addr, 4); + break; + } + + return; + } + switch (addr) { case A_ERRCAUSE: value &=3D extract64(ERRCAUSE_MASK, 0, 32); @@ -475,6 +759,8 @@ static void riscv_wgchecker_realize(DeviceState *dev, E= rror **errp) return; } =20 + s->slots =3D g_new0(WgCheckerSlot, s->slot_count + 1); + memory_region_init_io(&s->mmio, OBJECT(dev), &riscv_wgchecker_ops, s, TYPE_RISCV_WGCHECKER, s->mmio_size); sysbus_init_mmio(SYS_BUS_DEVICE(dev), &s->mmio); @@ -538,6 +824,44 @@ static void riscv_wgchecker_reset_enter(Object *obj, R= esetType type) =20 s->errcause =3D 0; s->erraddr =3D 0; + + for (int i =3D 0; i < nslots; i++) { + s->slots[i].addr =3D TO_SLOT_ADDR(start); + s->slots[i].perm =3D 0; + s->slots[i].cfg =3D 0; + } + s->slots[nslots].addr =3D TO_SLOT_ADDR(end); + s->slots[nslots].perm =3D 0; + s->slots[nslots].cfg =3D 0; + + if (s->num_default_slots !=3D 0) { + /* + * Use default slots: + * slot[0] is hard-coded to start address, so the default slots + * start from slot[1]. + */ + memcpy(&s->slots[1], s->default_slots, + sizeof(WgCheckerSlot) * s->num_default_slots); + } else if ((s->hw_bypass) || + ((worldguard_config !=3D NULL) && worldguard_config->hw_byp= ass)) { + /* HW bypass mode */ + uint32_t trustedwid =3D worldguard_config->trustedwid; + + if (trustedwid =3D=3D NO_TRUSTEDWID) { + trustedwid =3D worldguard_config->nworlds - 1; + } + + s->slots[nslots].perm =3D WGC_PERM(trustedwid, P_READ | P_WRITE); + s->slots[nslots].perm &=3D wgc_slot_perm_mask; + s->slots[nslots].cfg =3D A_TOR; + } + + /* + * As reset function modify the wgC slot, we need to flush existing + * softmmu TLB. It is required since power-gating will reset wgC after + * running some workload and using TLB. + */ + wgchecker_iommu_notify_all(s); } =20 static void riscv_wgchecker_class_init(ObjectClass *klass, const void *dat= a) @@ -611,6 +935,19 @@ DeviceState *riscv_wgchecker_create(hwaddr addr, uint3= 2_t size, qdev_prop_set_uint64(dev, name_offset, region_offset[i]); } =20 + if (num_default_slots > slot_count) { + num_default_slots =3D slot_count; + } + + s->num_default_slots =3D num_default_slots; + if (s->num_default_slots) { + s->default_slots =3D g_new0(WgCheckerSlot, s->num_default_slots); + memcpy(s->default_slots, default_slots, + sizeof(WgCheckerSlot) * s->num_default_slots); + } else { + s->default_slots =3D NULL; + } + sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal); sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, addr); sysbus_connect_irq(SYS_BUS_DEVICE(dev), 0, irq); diff --git a/hw/misc/riscv_worldguard.c b/hw/misc/riscv_worldguard.c index 73fa8190cd..e17f94dbf3 100644 --- a/hw/misc/riscv_worldguard.c +++ b/hw/misc/riscv_worldguard.c @@ -38,6 +38,9 @@ */ struct RISCVWorldGuardState *worldguard_config; =20 +/* perm field bitmask of wgChecker slot, it's depends on NWorld. */ +uint64_t wgc_slot_perm_mask; + static Property riscv_worldguard_properties[] =3D { DEFINE_PROP_UINT32("nworlds", RISCVWorldGuardState, nworlds, 0), =20 diff --git a/include/hw/misc/riscv_worldguard.h b/include/hw/misc/riscv_wor= ldguard.h index bb61d372f0..e83aaa00ff 100644 --- a/include/hw/misc/riscv_worldguard.h +++ b/include/hw/misc/riscv_worldguard.h @@ -45,6 +45,7 @@ struct RISCVWorldGuardState { }; =20 extern struct RISCVWorldGuardState *worldguard_config; +extern uint64_t wgc_slot_perm_mask; =20 DeviceState *riscv_worldguard_create(uint32_t nworlds, uint32_t trustedwid, bool hw_bypass, bool tz_compat); @@ -87,9 +88,12 @@ struct WgCheckerRegion { struct RISCVWgCheckerState { /*< private >*/ SysBusDevice parent_obj; + uint32_t num_default_slots; + WgCheckerSlot *default_slots; =20 /*< public >*/ MemoryRegion mmio; + WgCheckerSlot *slots; qemu_irq irq; =20 /* error reg */ --=20 2.43.0 From nobody Fri Nov 14 17:01:12 2025 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; dmarc=pass(p=reject dis=none) header.from=sifive.com ARC-Seal: i=1; a=rsa-sha256; t=1761063791; cv=none; d=zohomail.com; s=zohoarc; b=mh1wVSS0/6iViAmleW1JB2fJCf4Sw12aM5YCOfbyuJJvXR0UKrN+iXICLNNU+ecaQIEsU/mbhVE5cp9JqsiyI2RW8NN3Y0pXaZneEF9yuNNRyUMfGARniiJM5yQdWZDyX4i4eZ2iH9L82CO9odSXZofJnuVYMHPDgV6w1xwRU70= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1761063791; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=V7OWg3xhf8hcd2KugK7381OPAfVuAeutnPeAuNj80lI=; b=GrSihgStAeaUZf8ClzPbMn4/py80D2zzRluRwwJMiboU1kuw15gFLmYGtj3yQW8N+ns0CE/mg9VsRS6brYqwDCmvk+bOe/s220/ez9PtK8Sm3Jpqg6CUZuOuzg4p1nVJWmKNPgAArHSJQuom3TBX3NJclsgL4kd0QJhjjEu417E= 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; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1761063791239661.2663886078503; Tue, 21 Oct 2025 09:23:11 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1vBF7U-0004SE-79; Tue, 21 Oct 2025 12:21:56 -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 1vBF7A-0004Ld-6L for qemu-devel@nongnu.org; Tue, 21 Oct 2025 12:21:38 -0400 Received: from mail-pf1-x42b.google.com ([2607:f8b0:4864:20::42b]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1vBF6w-0002Y7-Cw for qemu-devel@nongnu.org; Tue, 21 Oct 2025 12:21:26 -0400 Received: by mail-pf1-x42b.google.com with SMTP id d2e1a72fcca58-781ea2cee3fso5200030b3a.0 for ; Tue, 21 Oct 2025 09:21:21 -0700 (PDT) Received: from hsinchu26.internal.sifive.com ([210.176.154.34]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7a23011088fsm11720280b3a.65.2025.10.21.09.21.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Oct 2025 09:21:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1761063680; x=1761668480; 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=V7OWg3xhf8hcd2KugK7381OPAfVuAeutnPeAuNj80lI=; b=fAsNY/nxWapNdbE12fowqC6AX87uCmLnqRBOBp1IW+HLpHxg2ZE4Ke7dmppCXnzsye 99jOVzGp+heGqdoiHiJJwxjnGj2I4bQjaYwrZEpqbTmvIqGQoWMI4LPnoZLNfJTqAZSz VLhF7YaFefAdVrw9We8u4gjHNA8yESi7suj9A2Exxxo10SKzvggP1HrpjdcwYpRY/Ssa Dzs349d6PQivscj3fOyGQm961m6IX7xWy/QiOUdhmrJtHMkNiN2rKlx+N6w3h8+nAco2 +jovlx80DFI42NMHOZjNGydOthEAjge5qBjtYyqwWhCHRCqp8G52E5pB51+5T/gwgDsQ ryxA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1761063680; x=1761668480; 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=V7OWg3xhf8hcd2KugK7381OPAfVuAeutnPeAuNj80lI=; b=rtE3MF8Y2FeDoaMAYnhSVZPZPGEXl5ZGAh1QlpotNkywohov03TB6IzQQl7OI4cIoo W6fsbgev2GVl2khE1muwW9Har0aTnYD2Tj059JoBEG0bydNVhb2AfmnIAftBOb5HiCS5 NOQpAiCpBRUAloS7HYaj2UFK8nlaecn+gSbi9ALnd3RgTdTAwcLM7ESsJ/9JfibI1Acj H4DQ0h/6UMDee5LCuFBkT6H6x1mMz+N5j8sP82d+BYqqSRwnwtJKnnmCl5vkLsKcDL4N FXD3iOiY72ImU4gnvklW+Ux9+zyk+M0XjgIkFUJL7OzpY/nHPLZ65nr6CSqpkT/ps/BK TBZA== X-Gm-Message-State: AOJu0YyfPzwWmVc67tyYqLyEUtr7M+6XKVTgBoqRJZB6EHy1SgR9gYy+ OC7s2XMkgS2MzNf3mDfSeqqLeq+GR/7wV2l4Ttg9Gh7tGVV1grL/+l4GkmL6G+PtUyXIIu+Zum+ uZUhu/41zNJ0k9WczB6gLGN9rW/J5fhnLDc9HfT5tj6smatcK6Pj1cXhWSfSjhC48WAqEz7oNC8 Riz2+JQuGIP52PExIW1SMzu4p1cJnkvQLXKmuzcg== X-Gm-Gg: ASbGncvAw3rtXRV5N4GlznXwNfkBgEtg9yz5uzckPPMUys6RpTdJxVPl7rRaJNzn0Fn DX17shra96kpaokmdlW0+1CX1Ti+vGV5TkyHo9hdt9jGyvYy5b2xq9vMy6hS5Ccf2P605s81b6J 2PCUSvJD+dJ3slrPctYYx/5N2UIkP4fkld319u1eCjCA2JWhp4cWOluwrGx6hkDqKC6AJgr8ogz 6jWtvrDJ1i0ye+A1uy6M6lyklVAN2IleAB5EUZXjSqy70svhgH3Yo+xHS3AbLRMkVMU1UIGYimQ mTxBpLpNMdeXob8g0GrO6QVltRUIqnY3BkTVscrYBM4prn0uB6LKRpIickhwakCVlZzKWA39Orw M/tcHssOa2ahabdxrkZcGSHT7tMj1onkxzhg/A1XOnfr63EPB66nii4gFZEJc75ruv9dc2ikoQO hwfTQWfNs479/YVXcSMdDl6Q== X-Google-Smtp-Source: AGHT+IGggbcFtpUPpmjce8nnOZM0nUwued5cfZ0Tf3WDatbH77nY5U36b5TuuRnHiqw5qbHZaZL59Q== X-Received: by 2002:a05:6a00:1705:b0:77f:3149:372f with SMTP id d2e1a72fcca58-7a220a3bf85mr21621616b3a.8.1761063679839; Tue, 21 Oct 2025 09:21:19 -0700 (PDT) From: Jim Shu To: qemu-devel@nongnu.org, qemu-riscv@nongnu.org Cc: Daniel Henrique Barboza , Paolo Bonzini , Palmer Dabbelt , Alistair Francis , Weiwei Li , Liu Zhiwei , Jim Shu , "Fea . Wang" Subject: [PATCH v3 16/18] hw/misc: riscv_wgchecker: Implement correct block-access behavior Date: Wed, 22 Oct 2025 00:21:06 +0800 Message-ID: <20251021162108.585468-2-jim.shu@sifive.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20251021155548.584543-1-jim.shu@sifive.com> References: <20251021155548.584543-1-jim.shu@sifive.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: pass client-ip=2607:f8b0:4864:20::42b; envelope-from=jim.shu@sifive.com; helo=mail-pf1-x42b.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable 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 @sifive.com) X-ZM-MESSAGEID: 1761063793261158500 Content-Type: text/plain; charset="utf-8" The wgChecker is configurable for whether blocked accesses: * should cause a bus error or just read return zero and write ignore * should generate the interrupt or not Also, when updating error-cause register in world-guard checker, it should also update the interrupt status. Signed-off-by: Jim Shu Signed-off-by: Fea.Wang --- hw/misc/riscv_wgchecker.c | 179 +++++++++++++++++++++++++++++++++++++- 1 file changed, 177 insertions(+), 2 deletions(-) diff --git a/hw/misc/riscv_wgchecker.c b/hw/misc/riscv_wgchecker.c index 99439a1c68..b4acdaa294 100644 --- a/hw/misc/riscv_wgchecker.c +++ b/hw/misc/riscv_wgchecker.c @@ -99,6 +99,176 @@ REG32(SLOT_CFG, 0x010) #define P_READ (1 << 0) #define P_WRITE (1 << 1) =20 +static void decode_napot(hwaddr a, hwaddr *sa, hwaddr *ea) +{ + /* + * aaaa...aaa0 8-byte NAPOT range + * aaaa...aa01 16-byte NAPOT range + * aaaa...a011 32-byte NAPOT range + * ... + * aa01...1111 2^XLEN-byte NAPOT range + * a011...1111 2^(XLEN+1)-byte NAPOT range + * 0111...1111 2^(XLEN+2)-byte NAPOT range + * 1111...1111 Reserved + */ + + a =3D FROM_SLOT_ADDR(a) | 0x3; + + if (sa) { + *sa =3D a & (a + 1); + } + if (ea) { + *ea =3D a | (a + 1); + } +} + +typedef struct WgAccessResult WgAccessResult; +struct WgAccessResult { + bool is_success; + bool has_bus_error; + bool has_interrupt; + uint8_t perm:2; +}; + +static WgAccessResult wgc_check_access( + RISCVWgCheckerState *s, hwaddr phys_addr, uint32_t wid, bool is_write) +{ + WgCheckerSlot *slot, *prev_slot; + uint32_t cfg_a, prev_cfg_a; + uint64_t start, end; + int slot_id, wgc_perm =3D 0; + WgAccessResult result =3D { 0 }; + + bool is_matching =3D false; + bool slot0_be, slot0_ip; + bool matched_slot_be =3D false, matched_slot_ip =3D false; + + for (slot_id =3D 0; slot_id < s->slot_count; slot_id++) { + slot =3D &s->slots[slot_id + 1]; + cfg_a =3D FIELD_EX32(slot->cfg, SLOT_CFG, A); + + if (cfg_a =3D=3D A_TOR) { + prev_slot =3D &s->slots[slot_id]; + + prev_cfg_a =3D FIELD_EX32(prev_slot->cfg, SLOT_CFG, A); + if (prev_cfg_a =3D=3D A_NA4) { + start =3D FROM_SLOT_ADDR(prev_slot->addr) + 4; + } else if (prev_cfg_a =3D=3D A_NAPOT) { + decode_napot(prev_slot->addr, NULL, &start); + start +=3D 1; + } else { /* A_TOR or A_OFF */ + start =3D FROM_SLOT_ADDR(prev_slot->addr); + } + end =3D FROM_SLOT_ADDR(slot->addr); + } else if (cfg_a =3D=3D A_NA4) { + start =3D FROM_SLOT_ADDR(slot->addr); + end =3D start + 4; + } else if (cfg_a =3D=3D A_NAPOT) { + decode_napot(slot->addr, &start, &end); + end +=3D 1; + } else { + /* A_OFF: not in slot range. */ + continue; + } + + /* wgChecker slot range is between start to (end - 1). */ + if ((start <=3D phys_addr) && (phys_addr < end)) { + /* Match the wgC slot */ + int perm =3D ((slot->perm >> (wid * 2)) & 0x3); + + /* If any matching rule permits access, the access is permitte= d. */ + wgc_perm |=3D perm; + + /* + * If any matching rule wants to report error (IRQ or Bus Erro= r), + * the denied access should report error. + */ + is_matching =3D true; + if (is_write) { + matched_slot_be |=3D FIELD_EX64(slot->cfg, SLOT_CFG, EW); + matched_slot_ip |=3D FIELD_EX64(slot->cfg, SLOT_CFG, IW); + } else { + matched_slot_be |=3D FIELD_EX64(slot->cfg, SLOT_CFG, ER); + matched_slot_ip |=3D FIELD_EX64(slot->cfg, SLOT_CFG, IR); + } + } + } + + /* If no matching rule, error reporting depends on the slot0's config.= */ + if (is_write) { + slot0_be =3D FIELD_EX64(s->slots[0].cfg, SLOT_CFG, EW); + slot0_ip =3D FIELD_EX64(s->slots[0].cfg, SLOT_CFG, IW); + } else { + slot0_be =3D FIELD_EX64(s->slots[0].cfg, SLOT_CFG, ER); + slot0_ip =3D FIELD_EX64(s->slots[0].cfg, SLOT_CFG, IR); + } + + result.is_success =3D is_write ? (wgc_perm & P_WRITE) : (wgc_perm & P_= READ); + result.perm =3D wgc_perm; + if (!result.is_success) { + if (is_matching) { + result.has_bus_error =3D matched_slot_be; + result.has_interrupt =3D matched_slot_ip; + } else { + result.has_bus_error =3D slot0_be; + result.has_interrupt =3D slot0_ip; + } + } + + return result; +} + +static bool riscv_wgc_irq_update(RISCVWgCheckerState *s) +{ + bool ip =3D FIELD_EX64(s->errcause, ERRCAUSE, IP); + qemu_set_irq(s->irq, ip); + return ip; +} + +static MemTxResult riscv_wgc_handle_blocked_access( + WgCheckerRegion *region, hwaddr addr, uint32_t wid, bool is_write) +{ + RISCVWgCheckerState *s =3D RISCV_WGCHECKER(region->wgchecker); + bool be, ip; + WgAccessResult result; + hwaddr phys_addr; + + be =3D FIELD_EX64(s->errcause, ERRCAUSE, BE); + ip =3D FIELD_EX64(s->errcause, ERRCAUSE, IP); + phys_addr =3D addr + region->region_offset; + + /* + * Check if this blocked access trigger IRQ (Bus Error) or not. + * It depends on wgChecker slot config (cfg.IR/IW/ER/EW bits). + */ + result =3D wgc_check_access(s, phys_addr, wid, is_write); + + if (!be && !ip) { + /* + * With either of the be or ip bits is set, further violations do = not + * update the errcause or erraddr registers. Also, new interrupts + * cannot be generated until the be and ip fields are cleared. + */ + if (result.has_interrupt || result.has_bus_error) { + s->errcause =3D FIELD_DP64(s->errcause, ERRCAUSE, WID, wid); + s->errcause =3D FIELD_DP64(s->errcause, ERRCAUSE, R, !is_write= ); + s->errcause =3D FIELD_DP64(s->errcause, ERRCAUSE, W, is_write); + s->erraddr =3D TO_SLOT_ADDR(phys_addr); + } + + if (result.has_interrupt) { + s->errcause =3D FIELD_DP64(s->errcause, ERRCAUSE, IP, 1); + riscv_wgc_irq_update(s); + } + + if (result.has_bus_error) { + s->errcause =3D FIELD_DP64(s->errcause, ERRCAUSE, BE, 1); + } + } + + return result.has_bus_error ? MEMTX_ERROR : MEMTX_OK; +} + /* * Accesses only reach these read and write functions if the wgChecker * is blocking them; non-blocked accesses go directly to the downstream @@ -108,25 +278,27 @@ static MemTxResult riscv_wgc_mem_blocked_read(void *o= paque, hwaddr addr, uint64_t *pdata, unsigned size, MemTxAttrs a= ttrs) { + WgCheckerRegion *region =3D opaque; uint32_t wid =3D mem_attrs_to_wid(attrs); =20 hwaddr phys_addr =3D addr + region->region_offset; trace_riscv_wgchecker_mem_blocked_read(phys_addr, size, wid); =20 *pdata =3D 0; - return MEMTX_OK; + return riscv_wgc_handle_blocked_access(region, addr, wid, false); } =20 static MemTxResult riscv_wgc_mem_blocked_write(void *opaque, hwaddr addr, uint64_t value, unsigned size, MemTxAttrs a= ttrs) { + WgCheckerRegion *region =3D opaque; uint32_t wid =3D mem_attrs_to_wid(attrs); =20 hwaddr phys_addr =3D addr + region->region_offset; trace_riscv_wgchecker_mem_blocked_write(phys_addr, value, size, wid); =20 - return MEMTX_OK; + return riscv_wgc_handle_blocked_access(region, addr, wid, true); } =20 static const MemoryRegionOps riscv_wgc_mem_blocked_ops =3D { @@ -446,6 +618,7 @@ static void riscv_wgchecker_writeq(void *opaque, hwaddr= addr, switch (addr) { case A_ERRCAUSE: s->errcause =3D value & ERRCAUSE_MASK; + riscv_wgc_irq_update(s); break; case A_ERRADDR: s->erraddr =3D value; @@ -546,6 +719,7 @@ static void riscv_wgchecker_writel(void *opaque, hwaddr= addr, case A_ERRCAUSE + 4: value &=3D extract64(ERRCAUSE_MASK, 32, 32); s->errcause =3D deposit64(s->errcause, 32, 32, value); + riscv_wgc_irq_update(s); break; case A_ERRADDR: s->erraddr =3D deposit64(s->erraddr, 0, 32, value); @@ -824,6 +998,7 @@ static void riscv_wgchecker_reset_enter(Object *obj, Re= setType type) =20 s->errcause =3D 0; s->erraddr =3D 0; + riscv_wgc_irq_update(s); =20 for (int i =3D 0; i < nslots; i++) { s->slots[i].addr =3D TO_SLOT_ADDR(start); --=20 2.43.0 From nobody Fri Nov 14 17:01:12 2025 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; dmarc=pass(p=reject dis=none) header.from=sifive.com ARC-Seal: i=1; a=rsa-sha256; t=1761063789; cv=none; d=zohomail.com; s=zohoarc; b=JKtorgXcAP55PAONziyv3GuS7UouaTg7FobdCKhaxQrIwuJzkNGgWhkMdBXnqcIRe8GooFjNkkGos0nbhPQuzlLcYXDO9Q/DPVE14WjFh11YRtzHoLkcBkpP80gauLsKRYp37ynm2en21h1AevuCKHv955BSd0eCioI7ASNaTf8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1761063789; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=eC9YC8UsMk4PwW0SP0X/PECmIUNXr4CBp0Q0dpINKAA=; b=E/avISjMRk8S2gVBU2dCjG0xMpXSMlYaPNiHN/TXPYrERlU9LLCzCp5G9y2iq6l1NNlltbjsPw/ZamkoOnJlrPF7r2RdRYf+pxdOL76QePSkwzC3BS5aiK1AI62FB7oGXZ1HEGE3uOruqChWfYpLX2OtFfkmagt4eqL8979Kq4A= 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; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1761063789610676.0169742129328; Tue, 21 Oct 2025 09:23:09 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1vBF7R-0004Qp-RM; Tue, 21 Oct 2025 12:21:53 -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 1vBF7A-0004Mc-Ix for qemu-devel@nongnu.org; Tue, 21 Oct 2025 12:21:38 -0400 Received: from mail-pf1-x42f.google.com ([2607:f8b0:4864:20::42f]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1vBF6z-0002YP-Ef for qemu-devel@nongnu.org; Tue, 21 Oct 2025 12:21:27 -0400 Received: by mail-pf1-x42f.google.com with SMTP id d2e1a72fcca58-7811a02316bso4038370b3a.3 for ; Tue, 21 Oct 2025 09:21:24 -0700 (PDT) Received: from hsinchu26.internal.sifive.com ([210.176.154.34]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7a23011088fsm11720280b3a.65.2025.10.21.09.21.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Oct 2025 09:21:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1761063682; x=1761668482; 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=eC9YC8UsMk4PwW0SP0X/PECmIUNXr4CBp0Q0dpINKAA=; b=ItDYrTg5bDPnFOOQMrCzQ6TXmBmAlfygfNDlXvm1isxRFm+q/dYwJIOl4yT3wL1Jq9 akmcN4JcgdpB+ygRUvrvlLiSQdfkbAtZ6ZjThucOfRXknv/0Vth+H44slQDAO2ihRwsY gjsNsdGbkh+5M1zYgXd0j+GAs8qXImdeWG+RZGbslTt3JnTUwOlD5Evbn5Vc9NkpAMVq hPBwXmtAEE1Dmk1/AtWvdq9CwpNDlJYbdy9r2hFc5zeZheb93Ym56vewkMW/9ckux2xq G1jNSwkJS/3q/I8fxuhJBrQrvhJDmwY35JolyjH58LcE9+K4LXs/ybZRLe0D+vBwtS/g gYMA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1761063682; x=1761668482; 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=eC9YC8UsMk4PwW0SP0X/PECmIUNXr4CBp0Q0dpINKAA=; b=DWnt1ZEa+rGdHIGvhcsSVsD+BxqNYK8aU6EGW142+YVCLpKJjBJHiKxFz7WqZRJa1/ fLiUnuegmH7vSUG+alwgFXUcYWqgk0Ehlwy9jCJXUXXfLJ3bIyKK4HP6hwwriB7vkNNV gNs2Ob6mYQ5IXovfngwZ9fiH1idjzmgXBooTESPgdzrQNXIWSMyDx0+QJslnXe6y6shq OlcZJ34vdLeJGcLvPim6fSf1MK14W5dzZepuNZXCdqP/vzRjCyU/22QvCa6LV1GI7PRc rNLwp+O7H1Vm8KkpJBDPtY4ZXHJef1JtW1CoeU3LZELFqj2Xhi7ZDpqsg39vT/QMlidh f7AA== X-Gm-Message-State: AOJu0YxI9q8QNxSC3G3i954hyBWGiHpWDL6RjRPw8EXwJUep0jNON4wS WgkwBc4QrdJdyfdNwxbhY/bmHjzVTkRZ1Y5CiTMmGy1k8srei5CKnedc8B5x4ssbn8xqMsNfrtC G7E3JnwBhxytJW76qBV2QL0+fU9co0GFZ9kz6w5l1rGsT2C0F+T/QGFFzf3P6tL8j40XpdSH2p+ QHQAMYynzUlDQKH4yZrDtI3OaG+5k6lNePtlv4mA== X-Gm-Gg: ASbGncvG0eYL8LVKm7SlET1jZEjnzNhYq3xnvdnlltWC/iJyuj2uaJy+azblJgcIPAH Zzz6syGhbvbqbiMnNA+bAUtkovw9jlsUH5rR9gxWteJNeA+bTXTeWu57ECnIsLt4K9PoolKXtfV JvQPJubVi/ETR5FqahPQW3xEGiHfW4GFu1V7SogVdqWQAMCmIb1SJ2FV/zL1MGNj09yw/aFlQJ4 GylmKCCEtok8TYHhi/GRZK5kCq5Nv8QtZFYKnCHVq/95O5QiFXLceRAqOo6ktFf3IyXm23ySSPy fx0Q0Fg0tmvD4Pok54DYAl/m+xjzkkBRPBWh58irRON9pymfXYgxEJkyilWT2cTIqV8PN/JpaZK UOfve/XrDHuIdM9b5+OKh+55GW1wAPPdsQevwxBfqqDs44bgMAYGZMG57BBNCa/StZh23L62AXF dFBdtxQL2eS+yAnYDTlvZQ4A== X-Google-Smtp-Source: AGHT+IH+zFQ5lzpqAqaMbs0TXG30X3bygXGYGISXE3B3C0r5eD94gFVNBIlKnzB9yExBvMUtp1+YNw== X-Received: by 2002:a05:6a21:3290:b0:32d:a6d1:22b2 with SMTP id adf61e73a8af0-334a85047a0mr24997564637.10.1761063682008; Tue, 21 Oct 2025 09:21:22 -0700 (PDT) From: Jim Shu To: qemu-devel@nongnu.org, qemu-riscv@nongnu.org Cc: Daniel Henrique Barboza , Paolo Bonzini , Palmer Dabbelt , Alistair Francis , Weiwei Li , Liu Zhiwei , Jim Shu Subject: [PATCH v3 17/18] hw/misc: riscv_wgchecker: Check the slot settings in translate Date: Wed, 22 Oct 2025 00:21:07 +0800 Message-ID: <20251021162108.585468-3-jim.shu@sifive.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20251021155548.584543-1-jim.shu@sifive.com> References: <20251021155548.584543-1-jim.shu@sifive.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: pass client-ip=2607:f8b0:4864:20::42f; envelope-from=jim.shu@sifive.com; helo=mail-pf1-x42f.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable 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 @sifive.com) X-ZM-MESSAGEID: 1761063793595154100 Content-Type: text/plain; charset="utf-8" The final part of wgChecker we need to implement is actually using the wgChecker slots programmed by guest to determine whether to block the transaction or not. Since this means we now change transaction mappings when the guest writes to wgChecker slots, we must also call the IOMMU notifiers at that point. One tricky part here is that the perm of 'blocked_io_as' is the condition of deny access. For example, if wgChecker only permits RO access, the perm of 'downstream_as' will be IOMMU_RO and the perm of 'blocked_io_as' will be IOMMU_WO. Signed-off-by: Jim Shu Reviewed-by: Daniel Henrique Barboza --- hw/misc/riscv_wgchecker.c | 70 ++++++++++++++++++++++++++++++++++++--- hw/misc/trace-events | 1 + 2 files changed, 67 insertions(+), 4 deletions(-) diff --git a/hw/misc/riscv_wgchecker.c b/hw/misc/riscv_wgchecker.c index b4acdaa294..c23b076b1c 100644 --- a/hw/misc/riscv_wgchecker.c +++ b/hw/misc/riscv_wgchecker.c @@ -99,6 +99,52 @@ REG32(SLOT_CFG, 0x010) #define P_READ (1 << 0) #define P_WRITE (1 << 1) =20 +static IOMMUAccessFlags wgc_perm_to_iommu_flags(int wgc_perm) +{ + if (wgc_perm =3D=3D (P_READ | P_WRITE)) { + return IOMMU_RW; + } else if (wgc_perm & P_WRITE) { + return IOMMU_WO; + } else if (wgc_perm & P_READ) { + return IOMMU_RO; + } else { + return IOMMU_NONE; + } +} + +static void wgchecker_iommu_notify_all(RISCVWgCheckerState *s) +{ + /* + * Do tlb_flush() to whole address space via memory_region_notify_iomm= u() + * when wgChecker changes it's config. + */ + + IOMMUTLBEvent event =3D { + .entry =3D { + .addr_mask =3D -1ULL, + } + }; + + trace_riscv_wgchecker_iommu_notify_all(); + + for (int i =3D 0; i < WGC_NUM_REGIONS; i++) { + WgCheckerRegion *region =3D &s->mem_regions[i]; + uint32_t nworlds =3D worldguard_config->nworlds; + + if (!region->downstream) { + continue; + } + event.entry.iova =3D 0; + event.entry.translated_addr =3D 0; + event.type =3D IOMMU_NOTIFIER_UNMAP; + event.entry.perm =3D IOMMU_NONE; + + for (int wid =3D 0; wid < nworlds; wid++) { + memory_region_notify_iommu(®ion->upstream, wid, event); + } + } +} + static void decode_napot(hwaddr a, hwaddr *sa, hwaddr *ea) { /* @@ -317,6 +363,9 @@ static IOMMUTLBEntry riscv_wgc_translate(IOMMUMemoryReg= ion *iommu, { WgCheckerRegion *region =3D container_of(iommu, WgCheckerRegion, upstr= eam); RISCVWgCheckerState *s =3D RISCV_WGCHECKER(region->wgchecker); + bool is_write; + WgAccessResult result; + int wgc_perm; hwaddr phys_addr; uint64_t region_size; =20 @@ -335,18 +384,25 @@ static IOMMUTLBEntry riscv_wgc_translate(IOMMUMemoryR= egion *iommu, * Look at the wgChecker configuration for this address, and * return a TLB entry directing the transaction at either * downstream_as or blocked_io_as, as appropriate. - * For the moment, always permit accesses. */ =20 /* Use physical address instead of offset */ phys_addr =3D addr + region->region_offset; + is_write =3D (flags =3D=3D IOMMU_WO); =20 - is_success =3D true; + result =3D wgc_check_access(s, phys_addr, iommu_idx, is_write); =20 trace_riscv_wgchecker_translate(phys_addr, flags, - iommu_idx, is_success ? "pass" : "block"); + iommu_idx, result.is_success ? "pass" : "block"); =20 - ret.target_as =3D is_success ? ®ion->downstream_as : ®ion->block= ed_io_as; + wgc_perm =3D result.perm; + if (!result.is_success) { + /* if target_as is blocked_io_as, the perm is the condition of den= y access. */ + wgc_perm ^=3D (P_READ | P_WRITE); + } + ret.perm =3D wgc_perm_to_iommu_flags(wgc_perm); + + ret.target_as =3D result.is_success ? ®ion->downstream_as : ®ion= ->blocked_io_as; return ret; } =20 @@ -612,6 +668,9 @@ static void riscv_wgchecker_writeq(void *opaque, hwaddr= addr, break; } =20 + /* Flush softmmu TLB when wgChecker changes config. */ + wgchecker_iommu_notify_all(s); + return; } =20 @@ -708,6 +767,9 @@ static void riscv_wgchecker_writel(void *opaque, hwaddr= addr, break; } =20 + /* Flush softmmu TLB when wgChecker changes config. */ + wgchecker_iommu_notify_all(s); + return; } =20 diff --git a/hw/misc/trace-events b/hw/misc/trace-events index 7617b8843f..8505a9f964 100644 --- a/hw/misc/trace-events +++ b/hw/misc/trace-events @@ -417,3 +417,4 @@ riscv_wgchecker_mmio_write(uint64_t base, uint64_t offs= et, unsigned int size, ui riscv_wgchecker_mem_blocked_read(uint64_t phys_addr, unsigned size, uint32= _t wid) "phys_addr =3D 0x%" PRIx64 ", size =3D %u, wid =3D %" PRIu32 riscv_wgchecker_mem_blocked_write(uint64_t phys_addr, uint64_t data, unsig= ned size, uint32_t wid) "phys_addr =3D 0x%" PRIx64 ", data =3D 0x%" PRIx64 = ", size =3D %u, wid =3D %" PRIu32 riscv_wgchecker_translate(uint64_t addr, int flags, int wid, const char *r= es) "addr =3D 0x%016" PRIx64 ", flags =3D 0x%x, wid =3D %d: %s" +riscv_wgchecker_iommu_notify_all(void) "notifying UNMAP for all" --=20 2.43.0 From nobody Fri Nov 14 17:01:12 2025 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; dmarc=pass(p=reject dis=none) header.from=sifive.com ARC-Seal: i=1; a=rsa-sha256; t=1761063790; cv=none; d=zohomail.com; s=zohoarc; b=lR6xG0B27bkccL+njkK/KBifIYi+kJSuxr/S6TB/TffRt27q5XtvmKzuDDTp0T5uJaxHoE5XK8ij5cdEP6kyGMB2bsKnAGKF3kXAQMyFkROubjQb7HzTY38ebufDTQMUuCOVNi8lb9S+dHyw2s1Wb4Trr5hAB944ptsF/H/lwlQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1761063790; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=huFZTWwE5EwrZvuArHryWgwrJYSVhUlfOJjgqhChV0Q=; b=efbzbEuGpOPIN/f4mY1bzkpfdgY6r8i7FvT/crp8d3YVqNyskVo/SWaJ0kaPMbQUs3Wf9UhwAwEXu12BbdasZCS5eDsnp1BdTUy6OT1gzvUTfMQ428T8fNwXqpBiLgCjjnSjw80OGFPwm1P+YuezsG1TrLZbIrYF369iecvv2G0= 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; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1761063790100304.256372138261; Tue, 21 Oct 2025 09:23:10 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1vBF7S-0004RB-S1; Tue, 21 Oct 2025 12:21:54 -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 1vBF7C-0004Mw-TA for qemu-devel@nongnu.org; Tue, 21 Oct 2025 12:21:39 -0400 Received: from mail-pf1-x42c.google.com ([2607:f8b0:4864:20::42c]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1vBF7A-0002Yp-CD for qemu-devel@nongnu.org; Tue, 21 Oct 2025 12:21:38 -0400 Received: by mail-pf1-x42c.google.com with SMTP id d2e1a72fcca58-7a226a0798cso3739593b3a.2 for ; Tue, 21 Oct 2025 09:21:26 -0700 (PDT) Received: from hsinchu26.internal.sifive.com ([210.176.154.34]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7a23011088fsm11720280b3a.65.2025.10.21.09.21.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Oct 2025 09:21:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1761063684; x=1761668484; 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=huFZTWwE5EwrZvuArHryWgwrJYSVhUlfOJjgqhChV0Q=; b=S2eO2FO9UAWNYZoFWrPp8lAnPxiSpcazWzVqDy6KNP8g3kBUBChlqbwzNSSINNeSgh ZW1j1Cbi23H0pbyYjOvxtp2hdgpCpeV1yHsABlDrs59gBxmIMtuLC1ZYkUethEyjuEgT oPpEajvDJJ7IsgCS/54osVFNu6hIyur/Q81VrfHvNu19ciX/Rb0S3AWPMX5ONT7kr6z4 d9F9v5ku/MTUU2LLMqc1FDrdSK8QU/bO6w7SY8gqjLrkb7vRT8nUxjasKs8AY/zS8IOo o34N0m/6duvr4lf1p6MR7O/Byno+NYgZzvwrQO9FdvezaRvJU00NFcObPPySTkL7+UgN ip0Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1761063684; x=1761668484; 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=huFZTWwE5EwrZvuArHryWgwrJYSVhUlfOJjgqhChV0Q=; b=wF5fw6tZKi9TKIi6SaHZSulSzg1dfLKsi8YTA2Ie3kmewHKLzluxJ2Ny1CBrD8uvc2 GfiDwxnCmDR2Qt5BV9iqGk2qpve6eFeIelCybKKIShK519wutZtGEZnUkYBuZViOngzj aShCLz5QwO+s6fhS6bpQ/BdntgjBFELFsrFF8d6RQjz6HQSdZiuL3Dx/Bn01C/S4CUj7 uXEX5Iuc1S642Gk5N9JGy0XoH2XT2h+O9dp1oLbwyWvVmOzY6kWxj0mzE0AtJR06EvYS caOuXUjRu511HAPFypuvaRYuK2RbahBIxbgqRhpZ6gWdLwo24m6PUBx664yMxgV2dh0Z v6MA== X-Gm-Message-State: AOJu0Yw2tbkjFEbLAqq9VT1A4S52EOdrCsRcZzYaIykrn7n+nQUyterq LDIkcs+NdavXXUvhcQAdcpHp4q+BP9UdlIrY0p+7i6VIiuYPYMaFvgrUyUzF6J2CRenLTQIkQ8u QdW0ml5AioYwgWih4NK7of0N/YkY7mOjSwRw9WxgJ7SYCgzQ5nV/J6v3mq4jNO4p6MH4PuaDbWQ 4NLHsgYXmIq9B9Ez9Lm5VvWLkaMVtwllXAabE/kA== X-Gm-Gg: ASbGncvCGYO/jx1hbr5vakipWczm7ZKq+ya8GGPD1LorPMnZr9ByCRygeVwBq7AXSn7 QkL/05L0aeWVAkBX4l3MIczzJxh9poO2nTHxmjwszyez0+WoFoj+hT2TLPdJMQnZITDYbiyjUSw h4Fqx4k2wVt4tmT+5GPTMfNhdgml3axKbrB91esQVP9ca/FxPDSVih2ORvjN1z0T9lPvqIya30N gUv3N66jUWHCYG9k0TfGl13OPMA8NXbpKcnQ/oL28ytha6aHfvjmSTy3JMABkVglBhktGsp4msB 4x+PR+D7pQW8cjWyKBbFdpB+L/gHbnpPiHxRqnBoDhP6Z6Yo1xHCx0eKFasvC124KR/t1MtVpGp iyA06ElAAZNVvZ2wGCZJqgWCxOPN62IZ2s3nAHzQ/lRN0i6+VtVT0YBmf8JETsSFSz5HkEbnmJl Xo06LDbtErR58hQhoPpT3cQm0HxzFEQJT9 X-Google-Smtp-Source: AGHT+IHhFy4VzDWfZbtCM8KJZ28J/tgadcBeZJVWAvN3f7fNgI/ocOd1dPXKv27sG96lzMYFu3e6jA== X-Received: by 2002:a05:6a20:3945:b0:334:a15f:8d2d with SMTP id adf61e73a8af0-334a8629db6mr21085726637.44.1761063684189; Tue, 21 Oct 2025 09:21:24 -0700 (PDT) From: Jim Shu To: qemu-devel@nongnu.org, qemu-riscv@nongnu.org Cc: Daniel Henrique Barboza , Paolo Bonzini , Palmer Dabbelt , Alistair Francis , Weiwei Li , Liu Zhiwei , Jim Shu Subject: [PATCH v3 18/18] hw/riscv: virt: Add WorldGuard support Date: Wed, 22 Oct 2025 00:21:08 +0800 Message-ID: <20251021162108.585468-4-jim.shu@sifive.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20251021155548.584543-1-jim.shu@sifive.com> References: <20251021155548.584543-1-jim.shu@sifive.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: pass client-ip=2607:f8b0:4864:20::42c; envelope-from=jim.shu@sifive.com; helo=mail-pf1-x42c.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable 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 @sifive.com) X-ZM-MESSAGEID: 1761063793399154100 Content-Type: text/plain; charset="utf-8" * Add 'wg=3Don' option to enable RISC-V WorldGuard * Add wgChecker to protect several resources: DRAM, FLASH, UART. Signed-off-by: Jim Shu --- docs/system/riscv/virt.rst | 20 +++++ hw/riscv/Kconfig | 1 + hw/riscv/virt.c | 165 ++++++++++++++++++++++++++++++++++++- include/hw/riscv/virt.h | 15 +++- 4 files changed, 197 insertions(+), 4 deletions(-) diff --git a/docs/system/riscv/virt.rst b/docs/system/riscv/virt.rst index 60850970ce..eef1233350 100644 --- a/docs/system/riscv/virt.rst +++ b/docs/system/riscv/virt.rst @@ -146,6 +146,26 @@ The following machine-specific options are supported: =20 Enables the riscv-iommu-sys platform device. Defaults to 'off'. =20 +- wg=3D[on|off] + + When this option is "on", RISC-V WorldGuard will be enabled in the system + to provide the isolation of multiple worlds. RISC-V HARTs will enable WG + extensions to have WID in memory transaction. wgCheckers in front of RAMs + and device MMIO will be enabled to provide the access control of resourc= es + if the transaction contains WID. When not specified, this option is assu= med + to be "off". + + The WG configuration of virt machine includes 4 worlds. For WG configura= tion + of CPUs, the M-mode WID of CPU (``mwid``) is set to the largest WID numb= er, + and the authorized WID list of CPU (``mwidlist``) includes all WIDs. We = can + modify the configuration of all CPUs via ``x-mwid`` and ``x-mwidlist`` + CPU options. There are 3 wgCheckers in the virt machine, which separately + protects DRAM, FLASH, and UART. Default WG configuration on the virt mac= hine + is enough to run the demo of dual OSes in the different worlds. For exam= ple, + running both Linux kernel and Secure OS (e.g. OP-TEE) in it's own world. + + This option is restricted to the TCG accelerator. + Running Linux kernel -------------------- =20 diff --git a/hw/riscv/Kconfig b/hw/riscv/Kconfig index fc9c35bd98..d47e347b0f 100644 --- a/hw/riscv/Kconfig +++ b/hw/riscv/Kconfig @@ -68,6 +68,7 @@ config RISCV_VIRT select PLATFORM_BUS select ACPI select ACPI_PCI + select RISCV_WORLDGUARD =20 config SHAKTI_C bool diff --git a/hw/riscv/virt.c b/hw/riscv/virt.c index 47e573f85a..25dfa8a55e 100644 --- a/hw/riscv/virt.c +++ b/hw/riscv/virt.c @@ -58,6 +58,7 @@ #include "qapi/qapi-visit-common.h" #include "hw/virtio/virtio-iommu.h" #include "hw/uefi/var-service-api.h" +#include "hw/misc/riscv_worldguard.h" =20 /* KVM AIA only supports APLIC MSI. APLIC Wired is always emulated by QEMU= . */ static bool virt_use_kvm_aia_aplic_imsic(RISCVVirtAIAType aia_type) @@ -89,6 +90,9 @@ static const MemMapEntry virt_memmap[] =3D { [VIRT_PCIE_PIO] =3D { 0x3000000, 0x10000 }, [VIRT_IOMMU_SYS] =3D { 0x3010000, 0x1000 }, [VIRT_PLATFORM_BUS] =3D { 0x4000000, 0x2000000 }, + [VIRT_WGC_DRAM] =3D { 0x6000000, 0x1000 }, + [VIRT_WGC_FLASH] =3D { 0x6001000, 0x1000 }, + [VIRT_WGC_UART] =3D { 0x6002000, 0x1000 }, [VIRT_PLIC] =3D { 0xc000000, VIRT_PLIC_SIZE(VIRT_CPUS_MAX * 2= ) }, [VIRT_APLIC_M] =3D { 0xc000000, APLIC_SIZE(VIRT_CPUS_MAX) }, [VIRT_APLIC_S] =3D { 0xd000000, APLIC_SIZE(VIRT_CPUS_MAX) }, @@ -114,6 +118,38 @@ static MemMapEntry virt_high_pcie_memmap; =20 #define VIRT_FLASH_SECTOR_SIZE (256 * KiB) =20 +/* wgChecker helpers */ +typedef struct WGCInfo { + int memmap_idx; + uint32_t irq_num; + uint32_t slot_count; + + int num_of_child; + MemoryRegion *c_region[WGC_NUM_REGIONS]; + uint64_t c_offset[WGC_NUM_REGIONS]; +} WGCInfo; + +enum { + WGC_DRAM, + WGC_FLASH, + WGC_UART, + WGC_NUM, +}; + +static WGCInfo virt_wgcinfo[] =3D { + [WGC_DRAM] =3D { VIRT_WGC_DRAM, WGC_DRAM_IRQ, 16 }, + [WGC_FLASH] =3D { VIRT_WGC_FLASH, WGC_FLASH_IRQ, 16 }, + [WGC_UART] =3D { VIRT_WGC_UART, WGC_UART_IRQ, 1 }, +}; + +static void wgc_append_child(WGCInfo *info, MemoryRegion *region, + uint64_t offset) +{ + info->c_region[info->num_of_child] =3D region; + info->c_offset[info->num_of_child] =3D offset; + info->num_of_child +=3D 1; +} + static PFlashCFI01 *virt_flash_create1(RISCVVirtState *s, const char *name, const char *alias_prop_name) @@ -164,7 +200,8 @@ static void virt_flash_map1(PFlashCFI01 *flash, } =20 static void virt_flash_map(RISCVVirtState *s, - MemoryRegion *sysmem) + MemoryRegion *sysmem, + WGCInfo *info) { hwaddr flashsize =3D s->memmap[VIRT_FLASH].size / 2; hwaddr flashbase =3D s->memmap[VIRT_FLASH].base; @@ -173,6 +210,15 @@ static void virt_flash_map(RISCVVirtState *s, sysmem); virt_flash_map1(s->flash[1], flashbase + flashsize, flashsize, sysmem); + + if (info) { + wgc_append_child(info, + sysbus_mmio_get_region(SYS_BUS_DEVICE(s->flash[0]= ), 0), + flashbase); + wgc_append_child(info, + sysbus_mmio_get_region(SYS_BUS_DEVICE(s->flash[1]= ), 0), + flashbase + flashsize); + } } =20 static void create_pcie_irq_map(RISCVVirtState *s, void *fdt, char *nodena= me, @@ -1428,6 +1474,72 @@ static void virt_build_smbios(RISCVVirtState *s) } } =20 +static DeviceState *create_wgc(WGCInfo *info, DeviceState *irqchip) +{ + MemoryRegion *system_memory =3D get_system_memory(); + DeviceState *wgc; + MemoryRegion *upstream_mr, *downstream_mr; + qemu_irq irq =3D qdev_get_gpio_in(irqchip, info->irq_num); + hwaddr base, size; + + /* Unmap downstream_mr from system_memory if it is already mapped. */ + for (int i =3D 0; i < info->num_of_child; i++) { + downstream_mr =3D info->c_region[i]; + + g_assert(downstream_mr); + if (downstream_mr->container =3D=3D system_memory) { + memory_region_del_subregion(system_memory, downstream_mr); + } + + /* + * Clear the offset of downstream_mr, so we could correctly do + * address_space_init() to it in wgchecker. + */ + memory_region_set_address(downstream_mr, 0); + } + + base =3D virt_memmap[info->memmap_idx].base; + size =3D virt_memmap[info->memmap_idx].size; + + wgc =3D riscv_wgchecker_create( + base, size, irq, info->slot_count, 0, 0, + info->num_of_child, info->c_region, info->c_offset, 0, NULL); + + /* Map upstream_mr to system_memory */ + for (int i =3D 0; i < info->num_of_child; i++) { + upstream_mr =3D sysbus_mmio_get_region(SYS_BUS_DEVICE(wgc), i + 1); + g_assert(upstream_mr); + memory_region_add_subregion(system_memory, info->c_offset[i], + upstream_mr); + } + + return wgc; +} + +static void virt_create_worldguard(WGCInfo *wgcinfo, int wgc_num, + DeviceState *irqchip) +{ + CPUState *cpu; + + /* Global WG config */ + riscv_worldguard_create(VIRT_WG_NWORLDS, + VIRT_WG_TRUSTEDWID, + VIRT_WG_HWBYPASS, + VIRT_WG_TZCOMPAT); + + /* Enable WG extension of each CPU */ + CPU_FOREACH(cpu) { + CPURISCVState *env =3D cpu ? cpu_env(cpu) : NULL; + + riscv_worldguard_apply_cpu(env->mhartid); + } + + /* Create all wgChecker devices */ + for (int i =3D 0; i < wgc_num; i++) { + create_wgc(&wgcinfo[i], DEVICE(irqchip)); + } +} + static void virt_machine_done(Notifier *notifier, void *data) { RISCVVirtState *s =3D container_of(notifier, RISCVVirtState, @@ -1527,10 +1639,12 @@ static void virt_machine_done(Notifier *notifier, v= oid *data) =20 static void virt_machine_init(MachineState *machine) { + WGCInfo *wgcinfo =3D virt_wgcinfo; RISCVVirtState *s =3D RISCV_VIRT_MACHINE(machine); MemoryRegion *system_memory =3D get_system_memory(); MemoryRegion *mask_rom =3D g_new(MemoryRegion, 1); DeviceState *mmio_irqchip, *virtio_irqchip, *pcie_irqchip; + SerialMM *uart; int i, base_hartid, hart_count; int socket_count =3D riscv_socket_count(machine); =20 @@ -1548,6 +1662,11 @@ static void virt_machine_init(MachineState *machine) exit(1); } =20 + if (!tcg_enabled() && s->have_wg) { + error_report("'wg' is only available with TCG acceleration"); + exit(1); + } + /* Initialize sockets */ mmio_irqchip =3D virtio_irqchip =3D pcie_irqchip =3D NULL; for (i =3D 0; i < socket_count; i++) { @@ -1674,6 +1793,11 @@ static void virt_machine_init(MachineState *machine) memory_region_add_subregion(system_memory, s->memmap[VIRT_DRAM].base, machine->ram); =20 + if (object_property_get_bool(OBJECT(s), "wg", NULL)) { + wgc_append_child(&wgcinfo[WGC_DRAM], machine->ram, + s->memmap[VIRT_DRAM].base); + } + /* boot rom */ memory_region_init_rom(mask_rom, NULL, "riscv_virt_board.mrom", s->memmap[VIRT_MROM].size, &error_fatal); @@ -1701,10 +1825,16 @@ static void virt_machine_init(MachineState *machine) =20 create_platform_bus(s, mmio_irqchip); =20 - serial_mm_init(system_memory, s->memmap[VIRT_UART0].base, + uart =3D serial_mm_init(system_memory, s->memmap[VIRT_UART0].base, 0, qdev_get_gpio_in(mmio_irqchip, UART0_IRQ), 399193, serial_hd(0), DEVICE_LITTLE_ENDIAN); =20 + if (object_property_get_bool(OBJECT(s), "wg", NULL)) { + wgc_append_child(&wgcinfo[WGC_UART], + sysbus_mmio_get_region(SYS_BUS_DEVICE(uart), 0), + s->memmap[VIRT_UART0].base); + } + sysbus_create_simple("goldfish_rtc", s->memmap[VIRT_RTC].base, qdev_get_gpio_in(mmio_irqchip, RTC_IRQ)); =20 @@ -1713,7 +1843,16 @@ static void virt_machine_init(MachineState *machine) pflash_cfi01_legacy_drive(s->flash[i], drive_get(IF_PFLASH, 0, i)); } - virt_flash_map(s, system_memory); + + if (object_property_get_bool(OBJECT(s), "wg", NULL)) { + virt_flash_map(s, system_memory, &wgcinfo[WGC_FLASH]); + } else { + virt_flash_map(s, system_memory, NULL); + } + + if (object_property_get_bool(OBJECT(s), "wg", NULL)) { + virt_create_worldguard(wgcinfo, WGC_NUM, mmio_irqchip); + } =20 /* load/create device tree */ if (machine->dtb) { @@ -1758,6 +1897,20 @@ static void virt_machine_instance_init(Object *obj) s->iommu_sys =3D ON_OFF_AUTO_AUTO; } =20 +static bool virt_get_wg(Object *obj, Error **errp) +{ + RISCVVirtState *s =3D RISCV_VIRT_MACHINE(obj); + + return tcg_enabled() && s->have_wg; +} + +static void virt_set_wg(Object *obj, bool value, Error **errp) +{ + RISCVVirtState *s =3D RISCV_VIRT_MACHINE(obj); + + s->have_wg =3D value; +} + static char *virt_get_aia_guests(Object *obj, Error **errp) { RISCVVirtState *s =3D RISCV_VIRT_MACHINE(obj); @@ -1978,6 +2131,12 @@ static void virt_machine_class_init(ObjectClass *oc,= const void *data) NULL, NULL); object_class_property_set_description(oc, "iommu-sys", "Enable IOMMU platform device"); + + object_class_property_add_bool(oc, "wg", virt_get_wg, + virt_set_wg); + object_class_property_set_description(oc, "wg", + "Set on/off to enable/disabl= e the " + "RISC-V WorldGuard."); } =20 static const TypeInfo virt_machine_typeinfo =3D { diff --git a/include/hw/riscv/virt.h b/include/hw/riscv/virt.h index 7b4c2c8b7d..63be60b8a2 100644 --- a/include/hw/riscv/virt.h +++ b/include/hw/riscv/virt.h @@ -57,6 +57,7 @@ struct RISCVVirtState { bool have_aclint; RISCVVirtAIAType aia_type; int aia_guests; + bool have_wg; char *oem_id; char *oem_table_id; OnOffAuto acpi; @@ -88,11 +89,17 @@ enum { VIRT_PLATFORM_BUS, VIRT_PCIE_ECAM, VIRT_IOMMU_SYS, + VIRT_WGC_DRAM, + VIRT_WGC_FLASH, + VIRT_WGC_UART }; =20 enum { UART0_IRQ =3D 10, RTC_IRQ =3D 11, + WGC_DRAM_IRQ =3D 15, + WGC_FLASH_IRQ =3D 16, + WGC_UART_IRQ =3D 17, VIRTIO_IRQ =3D 1, /* 1 to 8 */ VIRTIO_COUNT =3D 8, PCIE_IRQ =3D 0x20, /* 32 to 35 */ @@ -103,7 +110,7 @@ enum { #define VIRT_PLATFORM_BUS_NUM_IRQS 32 =20 #define VIRT_IRQCHIP_NUM_MSIS 255 -#define VIRT_IRQCHIP_NUM_SOURCES 96 +#define VIRT_IRQCHIP_NUM_SOURCES 128 #define VIRT_IRQCHIP_NUM_PRIO_BITS 3 #define VIRT_IRQCHIP_MAX_GUESTS_BITS 3 #define VIRT_IRQCHIP_MAX_GUESTS ((1U << VIRT_IRQCHIP_MAX_GUESTS_BITS) - 1U) @@ -159,4 +166,10 @@ uint32_t imsic_num_bits(uint32_t count); #error "Can't accommodate all IMSIC groups in address space" #endif =20 +/* WorldGuard */ +#define VIRT_WG_NWORLDS 4 +#define VIRT_WG_TRUSTEDWID 3 +#define VIRT_WG_HWBYPASS true +#define VIRT_WG_TZCOMPAT false + #endif --=20 2.43.0