From nobody Thu Dec 18 13:40:37 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=1744887284; cv=none; d=zohomail.com; s=zohoarc; b=bX7CvKAEWiVQgfMFefb/W8LFhXogfTXNQ5XRnApZZUQ32FpzNOgiFUSe7WXQT+YELi9EJPB6hoQcBN2Jbxit1W5D9gdhZblZVhQwQmH5c7HujMyX1oF31RzHSWTss/pdo7KL2Mp+8PrTCS8PdhKgu8XvbZK9H1DHmilIXCFCyh4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1744887284; h=Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=1OzcF3/O35O63aAFZJRPLEZk5T/555tjIU+685rxziI=; b=BC3aji6Fa/Z8sDWxIhdrcVWVkKaYkJoEeDuiNsj3Um6SFO0AblryGbJo8iyFSFaifYMq4+WsO41axxxEWRmu9GJa0tsR+QB7ybBlTGArpDBhwHmU1rZixQ2BRAo1IN3+bKNHrtEoiMvQ6fkAHoabhJyeX3z9I8Qw19PT8k8MxJs= 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 1744887284029246.5993664970115; Thu, 17 Apr 2025 03:54:44 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1u5Ms3-0007Ko-Qu; Thu, 17 Apr 2025 06:53:27 -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 1u5Mrv-0007Hl-7D for qemu-devel@nongnu.org; Thu, 17 Apr 2025 06:53:20 -0400 Received: from mail-pl1-x62b.google.com ([2607:f8b0:4864:20::62b]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1u5Mrn-0003HB-F2 for qemu-devel@nongnu.org; Thu, 17 Apr 2025 06:53:18 -0400 Received: by mail-pl1-x62b.google.com with SMTP id d9443c01a7336-224100e9a5cso6839445ad.2 for ; Thu, 17 Apr 2025 03:53:10 -0700 (PDT) Received: from hsinchu26.internal.sifive.com ([210.176.154.34]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-73bd230ddaasm12366251b3a.138.2025.04.17.03.53.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Apr 2025 03:53:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1744887190; x=1745491990; darn=nongnu.org; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=1OzcF3/O35O63aAFZJRPLEZk5T/555tjIU+685rxziI=; b=So5Z2/cGy9UoiI0fEVC9EDDUk991Iz5oMY1VJdDr+swhyEkpChR8W7u9qwvAKOWNaW JLxKwc9EpEeIeZOca+OplIUPqaaLoo2FBrjJi/rD5zWGGn76eg710NrmG7LZw5vneHQv L5d1VuKhW0Yyq2lpmE0DNk8hFv8FTf0mu7ROaehjbPbFvpyON00lG4QYbzLNbvrnM4Hk lWOP9RNgdqpfY3qH9UEATUn3d/Ix0OhMyGwJQyyQHw2RSJFUa54bOXom39Wdzt6lC2Pc wVA3HjMI/vrZDpav3pj8A/Rg5eK3NQBfdcQ2oMmQiovqzNbKB9U3yGRiO9HWWgNSIi15 XUXA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744887190; x=1745491990; h=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=1OzcF3/O35O63aAFZJRPLEZk5T/555tjIU+685rxziI=; b=NqSVawyOjL89l7TuKBJUHVZt8OBz+FVXoKXTPzud2G9ETraSwJ/Gx4BryHwYYm/wLk kpdk8Zpvbpnt846Wx9MIzVzqzVgXF9tTcU/JOv/EhcSVaJxIbrUW1xGaJyXM91fJYs65 U/sqgBc0nB+dy0zfUHV8uy5SpNRiRybfpZyaoOpyN/HN3a/YcJioNu85/O7NUQXqJvPw Bj7udQPEVQkR0LsWEO8bN96Me9jWnjddJYaE1Yf7TaB5DDSEdhwoWF7R19cg3tSibU4t etZGwYP7nwazy6wfVIge46BnOEebZpnNd1zkviuq+jN7UNgc7jhhysinClO8KfqaijcN K4HQ== X-Gm-Message-State: AOJu0YwmPZcp3HT5Ke485hpvzeLgKHC1jPMkIwHkVE7phgE/+1++iKKg KevjqkXkTpm4gdZjgNY3KHlRsFIddC3+oUFzSXe3omXT1tS7bYKPTRG9XqK2lhkzeMhpKPekvoS CqLH/E3ILBkX0kuVF5447jm8FVU66JGGO0JaAlkXYK3aXxyb5Nn+ZgS/IzYMwGpki8usnF+hJh5 VjGkB2XxNUue3cScaUbj6r+tBzTFzqjUqGBQ== X-Gm-Gg: ASbGncs+vPCpr5GunZ0Yl7pJwJwKizXX5b5h9SMeHE+5zwF8kr5pj1C3LlvISNvPH4X Af2P3Akh2goiDP87XK8CdHvNCpT3uHV7ntgytmmUDYvxKd3lDSjemmiFme5b62D9P8zqxuLefc5 EOcI233pucqHx3MAvRAqGcAbcRUulEv9F9mI3N0FhumD5nMuI5c7tdLtfu2ciJ4EiRdBjEML1oH hWXdLfaZ1g60MZnsKTq3iKibEopzt8+uPmhz2BubgJ04YnNDcCfLgZDo/NFSmX5U1pLtVE3by1U NF26wvu4ZvSlep1/bDC7p2paSJEARJ6QwM0xmkyA/D60OYDxJ/8zpCHDPFopn7Q= X-Google-Smtp-Source: AGHT+IFB/VVpLc+3fkHQfajBQ7jQxp0K8fblft9XqyOnAuEWmcwRwAPFM/93cv/unN9ImTu7vLiK/g== X-Received: by 2002:a17:902:ef08:b0:220:bcc5:2845 with SMTP id d9443c01a7336-22c358bb4acmr75104785ad.7.1744887189383; Thu, 17 Apr 2025 03:53:09 -0700 (PDT) From: Jim Shu To: qemu-devel@nongnu.org, qemu-riscv@nongnu.org Cc: Richard Henderson , Paolo Bonzini , Palmer Dabbelt , Alistair Francis , Weiwei Li , Daniel Henrique Barboza , Liu Zhiwei , Eduardo Habkost , Marcel Apfelbaum , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , 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 , 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 v2 01/18] accel/tcg: Store section pointer in CPUTLBEntryFull Date: Thu, 17 Apr 2025 18:52:32 +0800 Message-Id: <20250417105249.18232-2-jim.shu@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20250417105249.18232-1-jim.shu@sifive.com> References: <20250417105249.18232-1-jim.shu@sifive.com> 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::62b; envelope-from=jim.shu@sifive.com; helo=mail-pl1-x62b.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: 1744887287387019100 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 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. Remove iotlb_to_section() as we directly use section pointer in the CPUTLBEntryFull (full->section). 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/exec/cputlb.h | 2 +- include/exec/exec-all.h | 15 --------------- include/hw/core/cpu.h | 12 +++++++----- system/physmem.c | 17 ----------------- 5 files changed, 23 insertions(+), 55 deletions(-) diff --git a/accel/tcg/cputlb.c b/accel/tcg/cputlb.c index fb22048876..d51bfc19c1 100644 --- a/accel/tcg/cputlb.c +++ b/accel/tcg/cputlb.c @@ -1085,7 +1085,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, @@ -1136,10 +1136,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 @@ -1149,7 +1148,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 */ @@ -1265,14 +1265,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); @@ -1331,7 +1331,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 @@ -1588,9 +1588,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 { @@ -1980,7 +1978,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(); @@ -2001,7 +1999,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(); @@ -2521,7 +2519,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(); @@ -2541,7 +2539,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/exec/cputlb.h b/include/exec/cputlb.h index 8125f6809c..fea0474cf6 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/exec/exec-all.h b/include/exec/exec-all.h index dd5c40f223..46be330d99 100644 --- a/include/exec/exec-all.h +++ b/include/exec/exec-all.h @@ -178,21 +178,6 @@ void tb_phys_invalidate(TranslationBlock *tb, tb_page_= addr_t page_addr); void tb_invalidate_phys_range(tb_page_addr_t start, tb_page_addr_t last); void tb_set_jmp_target(TranslationBlock *tb, int n, uintptr_t addr); =20 -#if !defined(CONFIG_USER_ONLY) - -/** - * 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(). - */ -struct MemoryRegionSection *iotlb_to_section(CPUState *cpu, - hwaddr index, MemTxAttrs attr= s); -#endif - /** * get_page_addr_code_hostp() * @env: CPUArchState diff --git a/include/hw/core/cpu.h b/include/hw/core/cpu.h index abd8764e83..da9011e2c5 100644 --- a/include/hw/core/cpu.h +++ b/include/hw/core/cpu.h @@ -208,15 +208,17 @@ struct CPUClass { */ 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 333a5eb94d..70a6dd1962 100644 --- a/system/physmem.c +++ b/system/physmem.c @@ -2668,23 +2668,6 @@ static uint16_t dummy_section(PhysPageMap *map, Flat= View *fv, MemoryRegion *mr) return phys_section_add(map, §ion); } =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 cpuas->memory_dispatch; - 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; -} - static void io_mem_init(void) { memory_region_init_io(&io_mem_unassigned, NULL, &unassigned_mem_ops, N= ULL, --=20 2.17.1 From nobody Thu Dec 18 13:40:37 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=1744887292; cv=none; d=zohomail.com; s=zohoarc; b=hO2uTkHkNPT5J7G3lhsnv9jGlNXcskZt7LTcBenUhlnwCaYuICuYdvIvP7EyNQve3Hd7IPyPERdo5BXEc9/o5PNmSE1nbwws9zZYBrhy/1e7n/DwIWAIZmy+BLy/hUC6LVD0G4Swr03mWSbEn1smUk2dClS/izwlW0gTgHDNV5c= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1744887292; h=Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=VWIwDHpofagJCdaNWS4Us0I4iTEd/vONX2w4rXuOZMM=; b=H6PZV1Mysm7Cm1B2uD/BobinOd/nDFPLgSbmulxCEyRgnH4XFHNtLuBr8RMkWT/uEckT5H/uYMe8/CeF2AI9SLeJlOP5SXj6cHdeyiWjhWdMFd/J47YCLYcTWw+jbqJkY+9h7Ry6vR9VD0eG8cp6fRygryQBAzvuUm20ZGhF4Qg= 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 1744887292019671.6452347808294; Thu, 17 Apr 2025 03:54:52 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1u5MsC-0007PO-0X; Thu, 17 Apr 2025 06:53:36 -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 1u5Mrv-0007Ho-U6 for qemu-devel@nongnu.org; Thu, 17 Apr 2025 06:53:20 -0400 Received: from mail-pf1-x431.google.com ([2607:f8b0:4864:20::431]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1u5Mru-0003IY-9P for qemu-devel@nongnu.org; Thu, 17 Apr 2025 06:53:19 -0400 Received: by mail-pf1-x431.google.com with SMTP id d2e1a72fcca58-7398d65476eso521507b3a.1 for ; Thu, 17 Apr 2025 03:53:17 -0700 (PDT) Received: from hsinchu26.internal.sifive.com ([210.176.154.34]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-73bd230ddaasm12366251b3a.138.2025.04.17.03.53.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Apr 2025 03:53:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1744887195; x=1745491995; darn=nongnu.org; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=VWIwDHpofagJCdaNWS4Us0I4iTEd/vONX2w4rXuOZMM=; b=cKMS5h8OmgDaqXcbo1sk6s6oUoKi7ps3MNzOgQEhWbGVOu5MbD9gbtXAKE/3nkftBj uFYNv+5sOcnC52ZBx41qFmtU1bUkZwzMuM99QT41OixUsW8u7ulaX2R1vw1WdWxgW3bC 10mq6SDbHK0EE3IJX6bbIulkAXGkAByMyE4FhVNyStUbyV8uWUTgIThJmznrhWymUVic pvz8JlOiJuCFugEV1yCIq27unrXIToSBm2aixcy3Nq+lHJAbMigFyC4uiZ6lXM+Lt8IC Nrakqk2MNl71cdblH+Q6LtkCDYcajhN2R9aZPRQkx10h1CGKRgOmJFCTMKCNRyPBIPrL f8SA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744887195; x=1745491995; h=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=VWIwDHpofagJCdaNWS4Us0I4iTEd/vONX2w4rXuOZMM=; b=nzsxeqjh9XTyj05+5QtPafTJo1rqWJizgxGm1ZTCuQPG/A1DY5zCKY+IG3avpJcxbl ri6lGUYi55vv3urABVkF24zyPuoV8MGBF85J8/pEdfoeywD7W7Xqgen9BtZ+38Xd2D02 BJIA5+kC88uD0kOuY5hAZaoERkbxRIEi2O40Uy0y1y6oo68fmz1WuVghaQqSfgU44Hhw +txcMKOiWp1e4eHrv05vxsO8u7SSlEkAH+wgQiXHtm/6Weo74+/gm75+NShos8omwC4r PBoDBc9O7r4PLuZX0WqSRJLJXwh3C6RrPwFnDJSRa24Vq3B0AT79vy7xN1cOQk5uQ++Z JHIQ== X-Gm-Message-State: AOJu0Yyjzw08O+3pYybsT5zcjoT+O/1H4riESbWVJLstZBEo1H+foJ5L UhkX4nqVRDxBnBTKIsMf0uPUk7ef5eRucN1/WSPUXtxXM5S6zSxA2N//MnOOOKwINuRUnDRUsY9 cD7oGgyN7wYzZ8B3bkcCVyNwU01cLuF1fjxMA3RvIEsKGnWf0f0vdXutRZxiSA0uPIscJvnvU35 fgPpavCbPZ1Tg7IjU8QP+GkdF6h66W8lHMig== X-Gm-Gg: ASbGncu4Xg+zEv21MJSdStpczbXqoqW/M1E6RqjNgiAQudwWVQX1lE2sVFez/Qw8CiM mTIhXVkzmZ5IWGiw7ww2XFfJd4wCxbGgn57xn2XzuXb4143ba7tQDZpoL5kdVB56e29mLuZhNyj fYdJluhIVOHktvHp+x3y61Ek9AfTfIiMQhCMcHbrgXLQVbGkPnzFlqGQLqEuLK2fqmoNusmXLwk EEPC/FPaEBZexU8KDZ0lxVZsqlp4wqMYo3m3hpItisydMNffk9L9Yex3QzE1jtCPX3qnDJtifnt qV1mIfHaFQ5gnvosBS9x6oe3VvKt8y19SABWAUKKVtIA42/eBMoTCpNoAihKjuE= X-Google-Smtp-Source: AGHT+IFVBvQPu1wQZag/jk13aUj8YdM8a9LzgzxIFNz+TdDY6I+9SybWXHGAuIid9r0OiBCVTdammQ== X-Received: by 2002:a05:6a00:3906:b0:736:4e14:8ec5 with SMTP id d2e1a72fcca58-73cf2ab9e33mr3229183b3a.11.1744887194780; Thu, 17 Apr 2025 03:53:14 -0700 (PDT) From: Jim Shu To: qemu-devel@nongnu.org, qemu-riscv@nongnu.org Cc: Richard Henderson , Paolo Bonzini , Palmer Dabbelt , Alistair Francis , Weiwei Li , Daniel Henrique Barboza , Liu Zhiwei , Eduardo Habkost , Marcel Apfelbaum , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , 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 , 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 v2 02/18] system/physmem: Remove the assertion of page-aligned section number Date: Thu, 17 Apr 2025 18:52:33 +0800 Message-Id: <20250417105249.18232-3-jim.shu@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20250417105249.18232-1-jim.shu@sifive.com> References: <20250417105249.18232-1-jim.shu@sifive.com> 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::431; envelope-from=jim.shu@sifive.com; helo=mail-pf1-x431.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: 1744887293719019000 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 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 70a6dd1962..da58d5c8ae 100644 --- a/system/physmem.c +++ b/system/physmem.c @@ -1016,12 +1016,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.17.1 From nobody Thu Dec 18 13:40:37 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=1744887360; cv=none; d=zohomail.com; s=zohoarc; b=Mb3r0s866vU2nwTG0Bi3SIc0JAcXjfwAH9egqrlMO4f+ZEU0Ic3wPec2YlLdCjdyM7C1LgwymCCXR9m83NPkXVzW+/JiNierUdvR4saCOeNpUCnGjSjFNjhbsEZdV7Zi/m6CCsZ1tj8IOFF3e2gcrzkR6T1TA+d51lPdCZ3DbAM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1744887360; h=Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=J1izik/mp/cOi9QWmmKUFGo6rJs6LGatGvCKXun7+9w=; b=hkzGEgHY7+aGfbvHir/rl3crn7NUIVhu3RwhTGWTtL4KpnodrZ79lljKPwlLrBfeZZBZgbcrpQuM86rGW4vaEMd10KoG0e4j9BbxXzTyiR1Oru8O2jkzFpy1VaTSQPMCbs/CrwgbMRdfzjvNOYIYd3GrlVDYaGS2BzmwZWm95+E= 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 174488736024696.86070191724468; Thu, 17 Apr 2025 03:56:00 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1u5MsN-0007Wc-KK; Thu, 17 Apr 2025 06:53:48 -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 1u5Ms4-0007NC-F0 for qemu-devel@nongnu.org; Thu, 17 Apr 2025 06:53:29 -0400 Received: from mail-pl1-x636.google.com ([2607:f8b0:4864:20::636]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1u5Mrz-0003Jw-EE for qemu-devel@nongnu.org; Thu, 17 Apr 2025 06:53:27 -0400 Received: by mail-pl1-x636.google.com with SMTP id d9443c01a7336-225477548e1so6154585ad.0 for ; Thu, 17 Apr 2025 03:53:22 -0700 (PDT) Received: from hsinchu26.internal.sifive.com ([210.176.154.34]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-73bd230ddaasm12366251b3a.138.2025.04.17.03.53.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Apr 2025 03:53:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1744887201; x=1745492001; darn=nongnu.org; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=J1izik/mp/cOi9QWmmKUFGo6rJs6LGatGvCKXun7+9w=; b=bM5AXfsRYmMzckfsUh9l/eBAdElkVxowQGD9ioxu7U1+8RjdgjYPnWcCxnGhxVTwqQ mQxqj0cwJE/MngR6iAQCAekDPQjPhvqpXbHcf98C3zsy1qeYPGrH7VFMKDoZTg4NTnbJ MtKoIvVPRiNf6MaQCRxfXPjs16SAlWCuBgYH41jIIuWe0r8Pa+EAU/3VW+V8A+/ZCiEa z2pCszviG6WnH4cdM15RRmmFR3yXujBfAlQXyiD8vrMV3ZFsVio+J9shp3m8AuAEqxfA TCxcZnA811YW836pFItIAxmJmw1HGyRDRa2Zo8M+GyfxSW/xOEfKcvZCU0N64q2RNvCs vOtA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744887201; x=1745492001; h=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=J1izik/mp/cOi9QWmmKUFGo6rJs6LGatGvCKXun7+9w=; b=Ayq7njLOiJxiDuvvsjttBW84FK2Fth8wn0bXHxYSuralvo878cas6hDTJIhji3lC9D 9aKbMrbbE8jHAH3//JHXx4qpm2VWZ1NrCqnEurVnST1WAWmfRZJGUvYDu1S+cXNG6ktL a3UU+CT+gun5XilPThj0DEBaZHAUexSwwwUdN+rfclF1xnKLmPKmxuLClRxeb+RQgQ21 bP+Z5QPkC2Uo6RwLtP0LLu3ePCuIQS45zfTHKE2OxRu4qvJmRLV1Qdwqczj+31hntg8V Rg74ih8oRJUV2ihrtYol8YvXJAdjU/ub0so82qUk6h9GmnraFJBvUHGlB+Ichu/JWtGI DsAg== X-Gm-Message-State: AOJu0Yz8Nbo84T1klm7VrO+UkCBchaGpDka0juT1zXnlM6nnVD2krhGS QJgpbEYUshZiuF0bbh/qYX5uVisWzh9vVNbkmsw/V1hOIfgNX94R7yQ/74Gg9Y/cL7LfatWTNQ2 20EpIlXdNBfIk1lLyl+G0mMEGx3SkXG62r7iV/IMaHxc2ms2uVcKGwOSKmcKd9/ARy+0mXgYMsR 6SpeOVuWe5xIAti6iiFtrRdIfSQ6aOSUx/WQ== X-Gm-Gg: ASbGncvOcmP6+HUpvlLwtZZM0GfHND698wYV+vhmMAwo2TsR9oatNLV65927o2Hvrp+ ysPLgvb/spogUQole2O3dxt24s3fjsl6CmBvjyweCqmYG8XuaKSG9z+dMLQ/O72HBXOhKZCf45p JL49nAefG6foa5loWb6ewECECY3znAGVGjQvR9f7KlROkFzRJmZTAL1v321Kx18nNk3Rb5yiaBr H0FSrzVXlgvqgBohIdr1rrurZEK4U6LbL/rY2WM/SMro1jr4XAJ+/RRQyp35nAIiDnbqXXYJT1P mWjtqQC1XhtxeHGiHEMZk3erqBq+IjOf+P/vIvdjiX2TYVIRRE13JcMtTYH4cZsst1/CSjLviA= = X-Google-Smtp-Source: AGHT+IGVpKLbE4JsLFjLmTiTUYHpFUFCeggAHf39EohNH3m35C24khnjnino8A1si3dmuJv/h8PhfA== X-Received: by 2002:a17:902:d552:b0:224:191d:8a87 with SMTP id d9443c01a7336-22c358e8c34mr94382175ad.26.1744887200240; Thu, 17 Apr 2025 03:53:20 -0700 (PDT) From: Jim Shu To: qemu-devel@nongnu.org, qemu-riscv@nongnu.org Cc: Richard Henderson , Paolo Bonzini , Palmer Dabbelt , Alistair Francis , Weiwei Li , Daniel Henrique Barboza , Liu Zhiwei , Eduardo Habkost , Marcel Apfelbaum , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , 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 , 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 v2 03/18] accel/tcg: memory access from CPU will pass access_type to IOMMU Date: Thu, 17 Apr 2025 18:52:34 +0800 Message-Id: <20250417105249.18232-4-jim.shu@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20250417105249.18232-1-jim.shu@sifive.com> References: <20250417105249.18232-1-jim.shu@sifive.com> 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::636; envelope-from=jim.shu@sifive.com; helo=mail-pl1-x636.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: 1744887362229019000 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 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/exec/cputlb.h | 11 +++++++---- include/exec/exec-all.h | 3 ++- 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 d51bfc19c1..9396024e75 100644 --- a/accel/tcg/cputlb.c +++ b/accel/tcg/cputlb.c @@ -1017,7 +1017,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]; @@ -1044,7 +1045,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 @@ -1180,7 +1182,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, @@ -1190,15 +1193,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 /** @@ -1240,7 +1243,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/exec/cputlb.h b/include/exec/cputlb.h index fea0474cf6..4ea67e8a96 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/include/exec/exec-all.h b/include/exec/exec-all.h index 46be330d99..63bec1446c 100644 --- a/include/exec/exec-all.h +++ b/include/exec/exec-all.h @@ -217,7 +217,8 @@ static inline tb_page_addr_t get_page_addr_code(CPUArch= State *env, MemoryRegionSection * address_space_translate_for_iotlb(CPUState *cpu, int asidx, hwaddr addr, hwaddr *xlat, hwaddr *plen, - MemTxAttrs attrs, int *prot); + MemTxAttrs attrs, int *prot, + MMUAccessType access_type); hwaddr memory_region_section_get_iotlb(CPUState *cpu, MemoryRegionSection *section); #endif diff --git a/system/physmem.c b/system/physmem.c index da58d5c8ae..fc04725096 100644 --- a/system/physmem.c +++ b/system/physmem.c @@ -681,12 +681,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 cpu->cpu_ases[asidx].memory_dispatch; @@ -703,10 +705,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 57cefcba14..e16dcde3ac 100644 --- a/target/alpha/helper.c +++ b/target/alpha/helper.c @@ -318,7 +318,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 3412312ad5..05a89b3614 100644 --- a/target/avr/helper.c +++ b/target/avr/helper.c @@ -149,7 +149,7 @@ bool avr_cpu_tlb_fill(CPUState *cs, vaddr address, int = size, } } =20 - tlb_set_page(cs, address, paddr, prot, mmu_idx, page_size); + tlb_set_page(cs, address, paddr, prot, access_type, mmu_idx, page_size= ); return true; } =20 diff --git a/target/hppa/mem_helper.c b/target/hppa/mem_helper.c index fb1d93ef1f..4f0982a2c1 100644 --- a/target/hppa/mem_helper.c +++ b/target/hppa/mem_helper.c @@ -473,7 +473,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 6876329de2..831e178927 100644 --- a/target/i386/tcg/system/excp_helper.c +++ b/target/i386/tcg/system/excp_helper.c @@ -624,7 +624,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 70d1b5cf99..b57775908d 100644 --- a/target/loongarch/tcg/tlb_helper.c +++ b/target/loongarch/tcg/tlb_helper.c @@ -522,7 +522,7 @@ bool loongarch_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); 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 0bf574830f..e652c23485 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 27fc929bee..fd8847e71f 100644 --- a/target/microblaze/helper.c +++ b/target/microblaze/helper.c @@ -53,8 +53,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 @@ -65,8 +65,8 @@ bool mb_cpu_tlb_fill(CPUState *cs, vaddr address, int siz= e, =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 df80301a41..172640aef0 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 47ac783c52..5248c168b2 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 ad9ba8294c..d0e8f20e11 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 6c4391d96b..f4c4b69a3c 100644 --- a/target/riscv/cpu_helper.c +++ b/target/riscv/cpu_helper.c @@ -1967,7 +1967,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 0ba0d55ab5..9d8afa150c 100644 --- a/target/rx/cpu.c +++ b/target/rx/cpu.c @@ -182,7 +182,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 f969850f87..ec6d4076e8 100644 --- a/target/s390x/tcg/excp_helper.c +++ b/target/s390x/tcg/excp_helper.c @@ -179,7 +179,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 7567e6c8b6..7531f6d933 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 3821cd91ec..08dd73b210 100644 --- a/target/sparc/mmu_helper.c +++ b/target/sparc/mmu_helper.c @@ -229,7 +229,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 @@ -245,7 +245,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) { @@ -769,7 +769,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 a64412e6bd..4a4688f109 100644 --- a/target/tricore/helper.c +++ b/target/tricore/helper.c @@ -84,7 +84,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 4824b97e37..5d5d69a811 100644 --- a/target/xtensa/helper.c +++ b/target/xtensa/helper.c @@ -281,7 +281,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.17.1 From nobody Thu Dec 18 13:40:37 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=1744887530; cv=none; d=zohomail.com; s=zohoarc; b=L8gPCMBejZ43cnO3o5QPKGfExFYR+lry6zKBDieypJT7D4NYb1Qhv/Gk2bIAHwA7m87c1VjIJYfaL0CYkmBN0N3ndws9Xd5Qg4hPKyCKi48EGwPSMJA/AxEKhWb/+BDXxU6zpfoqluGQiXnOoAGxRse1zF6PgLiT+B9b31/7oCw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1744887530; h=Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=cbCfnAY8k2Soyd1n/D0nC/2pTKzsneNdm2pbb2owQHc=; b=NbBhCg7Tv6EIq15KvcgPSp8X+igP6K9a7vVgk5KR1FDJ1DLCj0VDWeLmvctYfrHdVZ5fFEjIPeUa+cPbcvUxJVUHGvq56XnYdsTXrpl8mWwdGeLi9WyoHp1032ja17IlG9qWBu7+/mozmSduK077j0zUU1reAjfqtef2QagnrQU= 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 1744887530232752.4544895719816; Thu, 17 Apr 2025 03:58:50 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1u5MsR-0007c6-Ks; Thu, 17 Apr 2025 06:53:51 -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 1u5Ms7-0007OI-7O for qemu-devel@nongnu.org; Thu, 17 Apr 2025 06:53:32 -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 1u5Ms4-0003L1-9F for qemu-devel@nongnu.org; Thu, 17 Apr 2025 06:53:30 -0400 Received: by mail-pf1-x42a.google.com with SMTP id d2e1a72fcca58-736aaeed234so460092b3a.0 for ; Thu, 17 Apr 2025 03:53:27 -0700 (PDT) Received: from hsinchu26.internal.sifive.com ([210.176.154.34]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-73bd230ddaasm12366251b3a.138.2025.04.17.03.53.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Apr 2025 03:53:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1744887206; x=1745492006; darn=nongnu.org; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=cbCfnAY8k2Soyd1n/D0nC/2pTKzsneNdm2pbb2owQHc=; b=RIC7CshzBm2+A7T45AmgiQdQtOCv7iIxUeCsxbderNno4latHpIhaqmxTEEE/w9ihs LhekSRZhk9ikxA6V79c6FbIOsM3SDc2KFI1S37Y0S7FAFT0mvzR2zdlCYlpFyzBDPuaB rfik6LaEhKtmRxdLy5XDmbJ1HY1A0y2Fx5g8U+lTZGD/+X7IfmIaTLpLRu6/DHLcUS1y rtJxu1fhWqPyL7ZrnbZy+gyoulYgJrkbtVWxN3w/vcvuzelQY0P5q3t0jG/rUW6XIdkx /zXkFbwu/YNNsBjw29cxxASp4HbEBixz6+3oJ2CGdbOXf2g1JHhvvAH80KFesR7iV/HS 6+VA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744887206; x=1745492006; h=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=cbCfnAY8k2Soyd1n/D0nC/2pTKzsneNdm2pbb2owQHc=; b=YXZYNxEDyfG56BAR6qn6lSMz0k0Ot8t3Y8myK0gbJ6SnpNucNGSG2PhWDJS9J74NWS G9hs+xkyB0aJi+ABrxHd0coQYS6Yrz317FgB+kaNlZDypHWb5hh8c0Tyudva9ShTON/U YFbhJ3XuiKbaogowBV+AMCWCi/TG4U2Yv5bvdL81WbqABRoGYKOS5VZ8y23vOnpE2mCf mJLh2QGsOfX3LgX/Wy79WNGL0c7mQG4Jy4lDks4FxnmYcAOl+SBj4pnmu93FXVM9bP7F vih3vbI2NA7kYDPc/JdklLUPmij26EvqsJMXZA6JLgsJBQsUmIK6WQ7qdSBA8YPcNZSE o5JQ== X-Gm-Message-State: AOJu0YzovKf12iFQVxBxpIx5cYmIqqvjJZHsHW5Im0HZo2Hh4aDbjPTZ aQs9dlh95veErBN9oZNOTU7mqFYANlSUrD96jRHMvjAf8DcFaqAUTuEbIZywYdAl+IDeedEk3L4 N6jh8OqB5TPBVRIHu4lUYZu6u3YPh1D5Xi0F5l4eR/fK22aKgFqtAjIKQU5dYmV66tU0ZSNS33Q 6Z8H8U9OtpWxsiHjiokjdrk0Ea8nlOuMT+kw== X-Gm-Gg: ASbGnctOM6R9tRwH3oUacG4tZC9keEc2CuzdLugEkKsflaQz41eE3429NIMIqYRAyDc T7M1gqhHv2c76Ot7h6oxNLW0GJePpwhyihBCOdN5V86XdiK/n77p1Y5QuA/hEl/HY0mvahiD5gv latV8D046yw+alZMKsrhR8zMrobHhPwO2Y2Xlx0xNEZD692ddIgR1qhNCFycQNf8koUCkcf00e1 lcB+dfWVvxQi7nVAtX8an0XNgsbmH+2eBPgVL817fsyToPNCxWJHtJS0mEN1RqsgNZ+cVguiYau 9tyTsQpLWffu6vMNnuhB1VSY8ZF8ZTQgn9XGVcYG3evtOJCEDEJSZz0vV+19aAg= X-Google-Smtp-Source: AGHT+IGcw0tpnbx9LmA6EQEP0d8jXvgNFw4H5puxgT4ARXRg6weIMd+yOOKSUy/JqgxMO5Lay+zBvA== X-Received: by 2002:a05:6a00:801:b0:736:532b:7c10 with SMTP id d2e1a72fcca58-73c267fe0f0mr6994875b3a.21.1744887205746; Thu, 17 Apr 2025 03:53:25 -0700 (PDT) From: Jim Shu To: qemu-devel@nongnu.org, qemu-riscv@nongnu.org Cc: Richard Henderson , Paolo Bonzini , Palmer Dabbelt , Alistair Francis , Weiwei Li , Daniel Henrique Barboza , Liu Zhiwei , Eduardo Habkost , Marcel Apfelbaum , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , 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 , 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 v2 04/18] exec: Add RISC-V WorldGuard WID to MemTxAttrs Date: Thu, 17 Apr 2025 18:52:35 +0800 Message-Id: <20250417105249.18232-5-jim.shu@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20250417105249.18232-1-jim.shu@sifive.com> References: <20250417105249.18232-1-jim.shu@sifive.com> 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: 1744887530639019000 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 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. 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 8db1d30464..7a6866fa41 100644 --- a/include/exec/memattrs.h +++ b/include/exec/memattrs.h @@ -54,6 +54,11 @@ typedef struct MemTxAttrs { */ unsigned int pid:8; =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 @@ -63,8 +68,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.17.1 From nobody Thu Dec 18 13:40:37 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=1744887366; cv=none; d=zohomail.com; s=zohoarc; b=ZWai+ujVZRi6Qe5S73R8LrsddWdL7v7soFgG0u8VCvrliIfVOGPJ2oAzxwM+s3VzApItrBCdfLJ9iwfHJVUi6qBv4DbFuYV19CqBL3i0OKFX+AoqPd/ARzztBfD4IN8Cn+UY3dI12kEbPg7+yrahVnPX0YF37nP/mjdkML8RCM0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1744887366; h=Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=xHnyUVIUAy9EeD7Slw5HDOE0YtQ4rJTQDvRGE+rnGQs=; b=mQLYQ8YeX1ZcbnlCK6noYAjBXRnXyq3rBFuXSRk+PZg1/8C/O7LXerhETcmrQswP3Yf5TAdsZeak4otQMzrkAbSb76lJUuRiaDRoxGHINPEhocCXZgiSHmAVYFZ33u/fEEXwObwGrtPtkT4FDGBA8sJ4YCk+SO9tUvuIEyS6LhQ= 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 1744887366697470.6971224861229; Thu, 17 Apr 2025 03:56:06 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1u5MsQ-0007ZP-5n; Thu, 17 Apr 2025 06:53:50 -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 1u5MsE-0007Rl-DV for qemu-devel@nongnu.org; Thu, 17 Apr 2025 06:53: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 1u5Ms9-0003ML-Q6 for qemu-devel@nongnu.org; Thu, 17 Apr 2025 06:53:37 -0400 Received: by mail-pf1-x42b.google.com with SMTP id d2e1a72fcca58-739b3fe7ce8so485854b3a.0 for ; Thu, 17 Apr 2025 03:53:32 -0700 (PDT) Received: from hsinchu26.internal.sifive.com ([210.176.154.34]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-73bd230ddaasm12366251b3a.138.2025.04.17.03.53.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Apr 2025 03:53:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1744887211; x=1745492011; darn=nongnu.org; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=xHnyUVIUAy9EeD7Slw5HDOE0YtQ4rJTQDvRGE+rnGQs=; b=L3MeKVGO09zTMicdYvjTw+W46QBmizvXU1g62gqc/EX9bF3hDhaJ4hD7qDZpvJWdoA tLSzndBtTNqWR4CXGKOxu8zlQWqD5cUCTzyzDa2wDBENrZoI4E+BA/2q1PSqZrpfjjlE w7/T80vgzAr7WhIRExQg7LjICvWUSMtAi3Ls+UH6uMIv0ByGHsKqJl0zTC9jygOzuIEc UoBm5GgW6jIBlBjeWcv+f6Z0gwbim06tl37F3VXl/5xLWxjlzjct5109+PrVfJhapCLh 4enkBluqUK9ecM7EMzjxUQd6aLhu0I3fM9Bw+crf6SUAGBUDu3XlZHBDRIaw1/fOb8hG H2Cw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744887211; x=1745492011; h=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=xHnyUVIUAy9EeD7Slw5HDOE0YtQ4rJTQDvRGE+rnGQs=; b=TKkPP0vLwzBeOqOOunl2Brl3l0ncJ/Po5VCJYzh2g/L6CoEir0OLHueNuy6OPdnQ6w HARuuLSYE5C3vwpl/CZNi9ajuuRsHQBpRRT8vbRqmc23kfvgfoEJvCclMlnGo12o0oZE Q+QvMYesKlkv0mzS+NKGPMW4cxZHgvaMYhJIlrLBdVWRG0OKD7JFwAICpq3P2L1tLyFM JXzM3HtUmqaw4tt4hKncYM2exOEBCHW/SdT9PhKv8sNgASMIY3GtuXRSuEvsxOK6YpGx D9CUs1XAUpW++JgJQjzY3ugUcUjJyeVzI0ob+scpTCSbP6kyQni0RmCrrVZLFRjTxI+J Je1A== X-Gm-Message-State: AOJu0YwfUpQR8js5x8hNEtEiKVWRzMQkJsoUAhg6Ti6fzn+l0v2wyrn1 MtcDp1qWNsHWMyRKPxapazAhmFfc2e3pXzHUq1SZ6U1lkgH/Oi2eUK7sFJ1Aw8hbTRg3I3mWadr VgIHe5N/eAW7/wXgIqHVZQt1wxQ81L5NalVqNG4djujn21UcOasw5pPuHexSfjqo74MmqhYS8O6 av9h6yOunzx80RWxiq7j/4bkn2DNOGUAgsjw== X-Gm-Gg: ASbGncsjW8DAzSOpSw2ayLfFthuvdZW+QvRUJeS2tRUEyyKViAQqzVo9N6gL2CRRuhy cIehNr1y3mO9XoyvgRLS7uuKbdov6hNVx9erKxkLqDBTUVAbMGdLaDynJ8wuLx1LZFeqvLP7mtl R8PK+Jt5N/IlhCxLlJVhsPhc8WJnBsLvlkdQUiGmtvprHoNlvciWPp27nAcEvoBN5WgiA7yi1So Ctx9++5Tzwe/G/RDcCwJ70cV1CRUGFyy31acnI/gzVlw6IS3u1ilrpA/6DkamfNMenw8WqQI+MB lqPgP3Olhbnw119WfP/16Sz7Bk4xMS+JaTJgK/ImsW5+Xb2knOY9D5+Q2QKn5ew= X-Google-Smtp-Source: AGHT+IHEU/Z15Nqeghl+RRid4ktCJjgtInJpJwdFp+mC4T5BOF+V1EaTrnihixPDioE4+GiFqo4TbA== X-Received: by 2002:a05:6a21:8cca:b0:1f5:7ea8:a791 with SMTP id adf61e73a8af0-203b3e9ea8dmr7556355637.10.1744887211132; Thu, 17 Apr 2025 03:53:31 -0700 (PDT) From: Jim Shu To: qemu-devel@nongnu.org, qemu-riscv@nongnu.org Cc: Richard Henderson , Paolo Bonzini , Palmer Dabbelt , Alistair Francis , Weiwei Li , Daniel Henrique Barboza , Liu Zhiwei , Eduardo Habkost , Marcel Apfelbaum , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , 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 , 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 v2 05/18] hw/misc: riscv_worldguard: Add RISC-V WorldGuard global config Date: Thu, 17 Apr 2025 18:52:36 +0800 Message-Id: <20250417105249.18232-6-jim.shu@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20250417105249.18232-1-jim.shu@sifive.com> References: <20250417105249.18232-1-jim.shu@sifive.com> 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: 1744887368232019000 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 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 | 55 +++++++++ 4 files changed, 241 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 ec0fa5aa9f..7677c0e962 100644 --- a/hw/misc/Kconfig +++ b/hw/misc/Kconfig @@ -222,4 +222,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 6d47de482c..3d2f4bb6a3 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..b02bd28d02 --- /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 =3D NULL; + +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 */ +static 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, void *data) +{ + 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..8a533a0517 --- /dev/null +++ b/include/hw/misc/riscv_worldguard.h @@ -0,0 +1,55 @@ +/* + * 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); + +uint32_t mem_attrs_to_wid(MemTxAttrs attrs); +bool could_access_wgblocks(MemTxAttrs attrs, const char *wgblock); + +#endif --=20 2.17.1 From nobody Thu Dec 18 13:40:37 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=1744887359; cv=none; d=zohomail.com; s=zohoarc; b=XtQQWSBoCUbja2+MHGg2M1cpMJ9owCI2jZxknYFAV1AbOZVeOXeE/5Kcv+asOxZWgjdLYFNLN9EfwrzUo0dVslYWBX/G+QKsNha5e2C/9R4MHNbWxmZq8WdiNaTvyOjwykoAAWul6c4nsfyyuIKkbKhQOyMXSs3VMXo0mJ5JyEc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1744887359; h=Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=Q73Ra6NhBnNXysABi6967YWRbV9gsBItuZ0tiWgYmXM=; b=YUpxYgOta2wJNR1FFTTUtAhxBy5FFhV8IbWghgMNpKCjXQPUhHgXT5fa9eMEjvLHuR3R9MRqLtYerNaAY6dnwwSCZpk4S3JFXEfVctshYPb+e38e7jRMLihnyV6LvocRQ3h6PyjieArqlStBWF6dkYmUvD5zR2GjmpZ9pH3QAng= 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 174488735921373.25833783091355; Thu, 17 Apr 2025 03:55:59 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1u5MsS-0007cz-4t; Thu, 17 Apr 2025 06:53: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 1u5MsH-0007TZ-99 for qemu-devel@nongnu.org; Thu, 17 Apr 2025 06:53:42 -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 1u5MsE-0003NR-Sk for qemu-devel@nongnu.org; Thu, 17 Apr 2025 06:53:41 -0400 Received: by mail-pf1-x435.google.com with SMTP id d2e1a72fcca58-7399838db7fso599504b3a.0 for ; Thu, 17 Apr 2025 03:53:38 -0700 (PDT) Received: from hsinchu26.internal.sifive.com ([210.176.154.34]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-73bd230ddaasm12366251b3a.138.2025.04.17.03.53.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Apr 2025 03:53:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1744887217; x=1745492017; darn=nongnu.org; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=Q73Ra6NhBnNXysABi6967YWRbV9gsBItuZ0tiWgYmXM=; b=MxQibbiCnjGq9Ic9H23T5yiZgGbiBswbIEMy/FdLrya3GSmYaMBiZnc40fMC+BIV2G QLFMt49EprBeHP6vWPRj9uyrbC+yzodT8ydQJ8MQOVjW1Mou+sWrUAlWXHcF7UeVRIcR 1UgWAAf5XSQnegoqEuhGAAnaobvoqo6hSayu0fQbW9nJeTDRvPPiTAfvMfBOcyxa23N+ /A+92NiXMU9HnX/3Bymz8yvaLl+jH6ExboWBXn+SL/48yr9Nra37aXsWXHTjITnjA5D5 iuzjwL1N/ZSJRuoW72yOFZppbd6fxDLlqVXHq7i9/OHE5A9efbmgBVxL/q4m4R/4k6aL 1ChA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744887217; x=1745492017; h=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=Q73Ra6NhBnNXysABi6967YWRbV9gsBItuZ0tiWgYmXM=; b=A4TDK90PWZktwamFrSSJHJoN297oRPGaLWaxF8z4BIxwfeaOAoziW+CtqCqiGJLmah fthAEpgfy50RW7Mo05dfy42wqtlUo4F6DYp1rgKwkeu5+bOnR1p6AjvHdRrh8KVBhzNA a4FT5KlXQzFlszx8Sqnq2TCxridiYX/KeTrkR6FnH9mBga1UCjrUZc6Jw7D9+Osp9qLT OqPy0S89eS3USPfIjhLvwVygfaeZYCustVJ5ghKig7e9qBIB7AvVyrXV6dYrIBvldKFc QICBLdVW1p8Tx3bJeuwhufsbc6iL5g08O/VZoPzK+1azhF04Rlqo0ryEGAMBVow9trGE ZefQ== X-Gm-Message-State: AOJu0YyFe8MvB0sAsqCxiHUjfyAQGv4q1ZHSPntlBMcJFVH9LdS3tC4h WKuDz3rkGbr/b07RCfOn2AkT89Ev3gRwLEAkMDUHoeU6MOmy6onGqEbbWV6dI04RzKKk49Q3+AT WWdJpDlji/WXgTGvLhGzS/v0odv1IoMDdWjoZCGQQT7K0pWFoJQu5qcDltZyg5bJb9vDPOqputA JO/pssLpk8mrmCZG/DGnOzdUdSLKatfx3MqQ== X-Gm-Gg: ASbGnctpH82gCF4E27XANDRkkZjE7TBZyc6RNMrdnGnP1wqUmSP5rbu6NGzfT+4sSHM WOtWMdhq6ZxxEQdN6Eg0H1KWQQUktRhl/Wq9WHULMuB2rmCYVRX8pmO+V/XHdiYpEK/gF61ODri f1YweQDve++TB8D1VWH5SV6U4xjAvUDbL8i9GcdCoq54U3WlhQJ++wcyNTkTthNNWK6Ojs1vx9Y brj9MOm3IQ2O9ARxntwYKPAD/KiXw7Z3iYk/666Re7zADaz9JQ5JCj5lZBTqbXzUrOZmrTkOUvb dXW7nd4BBPD0LNRfd4EgJq7lDp9CJeBRvIsoOjdJ7vO0j0ewlFD0IGpkyiXrjmg= X-Google-Smtp-Source: AGHT+IG9lIi4y81GtUAS/p2kFe8CuUnY6PtZhntDyHe9dS07v84VZAseI8Cz6apbvILdy6H2Wt/8XQ== X-Received: by 2002:a05:6a00:26dd:b0:734:26c6:26d3 with SMTP id d2e1a72fcca58-73cdbf142f5mr3803664b3a.5.1744887216489; Thu, 17 Apr 2025 03:53:36 -0700 (PDT) From: Jim Shu To: qemu-devel@nongnu.org, qemu-riscv@nongnu.org Cc: Richard Henderson , Paolo Bonzini , Palmer Dabbelt , Alistair Francis , Weiwei Li , Daniel Henrique Barboza , Liu Zhiwei , Eduardo Habkost , Marcel Apfelbaum , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , 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 , 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 v2 06/18] target/riscv: Add CPU options of WorldGuard CPU extension Date: Thu, 17 Apr 2025 18:52:37 +0800 Message-Id: <20250417105249.18232-7-jim.shu@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20250417105249.18232-1-jim.shu@sifive.com> References: <20250417105249.18232-1-jim.shu@sifive.com> 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: 1744887361404019100 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 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.h | 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 09ded6829a..a182e8c61f 100644 --- a/target/riscv/cpu.c +++ b/target/riscv/cpu.c @@ -236,6 +236,9 @@ const RISCVIsaExtData isa_edata_arr[] =3D { ISA_EXT_DATA_ENTRY(xtheadmempair, PRIV_VERSION_1_11_0, ext_xtheadmempa= ir), ISA_EXT_DATA_ENTRY(xtheadsync, PRIV_VERSION_1_11_0, ext_xtheadsync), ISA_EXT_DATA_ENTRY(xventanacondops, PRIV_VERSION_1_12_0, ext_XVentanaC= ondOps), + 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(sswg, PRIV_VERSION_1_12_0, ext_sswg), =20 { }, }; diff --git a/target/riscv/cpu_cfg.h b/target/riscv/cpu_cfg.h index 8a843482cc..a5b045aa2f 100644 --- a/target/riscv/cpu_cfg.h +++ b/target/riscv/cpu_cfg.h @@ -143,6 +143,9 @@ struct RISCVCPUConfig { bool ext_smmpm; bool ext_sspm; bool ext_supm; + bool ext_smwg; + bool ext_smwgd; + bool ext_sswg; bool rvv_ta_all_1s; bool rvv_ma_all_1s; bool rvv_vl_half_avl; diff --git a/target/riscv/tcg/tcg-cpu.c b/target/riscv/tcg/tcg-cpu.c index 5aef9eef36..343e46e698 100644 --- a/target/riscv/tcg/tcg-cpu.c +++ b/target/riscv/tcg/tcg-cpu.c @@ -694,6 +694,17 @@ void riscv_cpu_validate_set_extensions(RISCVCPU *cpu, = Error **errp) cpu->cfg.ext_ssctr =3D false; } =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.17.1 From nobody Thu Dec 18 13:40:37 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=1744887314; cv=none; d=zohomail.com; s=zohoarc; b=Wz7Oufx8rPGJSZRjV/XlckFtHghMZJM3C+O3RrshFVXk0dsOVh4yULxBk+PFQtT7Jq7QZXCdzwGHCXgmmywUzaMHeICf1mETqrc1NKFtWmkyYnwDejUM7WyE5Ps/1RTtS5fL4O2pYMJ1qugz1zojA1qNo3COTHdO9iuV2zdkzhk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1744887314; h=Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=nwKYDLJebV8nfrFMI0a6Dc+oBkaaAbOXeXeNO1BXXr4=; b=k+urmISc4gEsmaSkCiBgKZwJAZlumDXNSniOIFhC5tSfzGsfCNgweiGh1DgirAhhAPJj/Ru0crzoH3CZeGi9c2MY5Wf76lk3+kgLaQcAOF3qf04RVfSdaeajhCjph3SThXYWLBqKm8cPqkeXfsXf6D1YZUkgrTEpi/uzFkqlCdE= 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 1744887314257178.61401972421822; Thu, 17 Apr 2025 03:55:14 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1u5MsS-0007cw-3U; Thu, 17 Apr 2025 06:53: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 1u5MsM-0007X2-M3 for qemu-devel@nongnu.org; Thu, 17 Apr 2025 06:53:47 -0400 Received: from mail-pf1-x434.google.com ([2607:f8b0:4864:20::434]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1u5MsK-0003Oo-5b for qemu-devel@nongnu.org; Thu, 17 Apr 2025 06:53:46 -0400 Received: by mail-pf1-x434.google.com with SMTP id d2e1a72fcca58-7376dd56f60so414698b3a.3 for ; Thu, 17 Apr 2025 03:53:43 -0700 (PDT) Received: from hsinchu26.internal.sifive.com ([210.176.154.34]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-73bd230ddaasm12366251b3a.138.2025.04.17.03.53.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Apr 2025 03:53:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1744887222; x=1745492022; darn=nongnu.org; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=nwKYDLJebV8nfrFMI0a6Dc+oBkaaAbOXeXeNO1BXXr4=; b=IuVNSOsKQ6zMRtyTx8/XKs+2edbSXgt++x/xSJZR15pqzdAqh0IBguK7AT93tjrI9N 7NGWV33JlCcIADUNLeuYn1rPqNNyV+uyRcN1DbyyNWSScJVdWuK1hcB6K/IYOkRjFgaJ xgzmkQRTIFsNijS6TxUTSUhqgsOT7pYrNXWDIQKf+6iVj6PH3erb49vulv0msuOTgaoB xSuNTV1NJDWt1IHKSp4GndzayiROXW6iP+ooPIUzcajjvhYqmitFznMKL9hoOT3z8BaS peCQDtQ/Mfa5mRg8OF5I4oXkguf4vtyi+c/SmS+CVWu4zwqvdZkTqJhcncTdo87Lj2QJ Swuw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744887222; x=1745492022; h=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=nwKYDLJebV8nfrFMI0a6Dc+oBkaaAbOXeXeNO1BXXr4=; b=efAJuvG7mFjHhy+OcVv7pkdWv1ZTv07qCGcw7RSH18+dUvLHk9SEXHYJyPmVIqtkDw jHYHN88l0rK1yvqyAOD2sa5tN83hS0f0loBkouQSw081xpLqS+Rmu2mOD9jPnRtp0NCn sUJpxjydgWR+32AAsaZn8Ks6lzvpw7TAoWUYmBcEn7D336lHHg/43gBxEZAKxpr8NqoN 4LjQ3lDuUhtQcprnpg1sNY5f8xfDgOfaeXQvPeuiOuNpHHe2kk7Nr5mc5DC+RbJJiEjr fUyHOjv0sLI8bWpdPqw0buCLJE7FgD+Cgx++Bfy5hIxUcL7+ZsKPuLhawoja0BLr1zAB lnDg== X-Gm-Message-State: AOJu0YxDnv/QM3IyvYAv9gpnxWD25+NZkIezLGkhHTLDOXMH8GD0nnG8 0VIEhJ3CWvvdwnwsgVkOq7KQLGeHvXyH2V4fI4HEiRs6pVL/jDghuZCBgfhuXaVpSW1VltxM7Bo n/X8pXx2+zyJ9EgWZ1Ub/7M1kg+DUHq2oVH3PBFomk9ivO45puWzJOd13nngwD39QkNc9kkYGe+ nQ8/mK/7Id+fRRv2bmUJLaY9pox2giaDWj/g== X-Gm-Gg: ASbGnct3DwJRQIIsmva2xdbaIFhSkHs+2/Q15B4TQmGNR7Ay6DesBc/oos9Q9TcOr4K WU7M4gK4jrJ1ATB6lmJulhj48Q88uxwFTCjXS1fD/Jr1wEswbaam7qS14MjSvPLHku+8cVnozjC Tk9BBemoPwhegTCa78pZAWl0mxubxjjfRrDwEuN5qahiXDRO9eYFd5dSs7j5SH3LJMPEju2q0I/ If5n9J1SfK+OC0KTYHOdnW0XUa2/+z02IptylRpOK5cEFxXmF1ZfqmkMj8byep/8KQnh90ODJIE eeUtn3+M3jPmNTCVy7Tr6Y75LITIO0/pujL34sH/VeQM/DLRLMvowPi3I9LVu68= X-Google-Smtp-Source: AGHT+IEtAT+0wGCf8mT+gDNa+Ynb8Ljy3iul0+oHiooH2s8FGiapW9pti1/nUioVpzgnp2NNFNq2IQ== X-Received: by 2002:a05:6a00:4501:b0:737:6d4b:f5f8 with SMTP id d2e1a72fcca58-73c267d36bdmr7470882b3a.17.1744887221840; Thu, 17 Apr 2025 03:53:41 -0700 (PDT) From: Jim Shu To: qemu-devel@nongnu.org, qemu-riscv@nongnu.org Cc: Richard Henderson , Paolo Bonzini , Palmer Dabbelt , Alistair Francis , Weiwei Li , Daniel Henrique Barboza , Liu Zhiwei , Eduardo Habkost , Marcel Apfelbaum , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , 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 , 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 v2 07/18] target/riscv: Add hard-coded CPU state of WG extension Date: Thu, 17 Apr 2025 18:52:38 +0800 Message-Id: <20250417105249.18232-8-jim.shu@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20250417105249.18232-1-jim.shu@sifive.com> References: <20250417105249.18232-1-jim.shu@sifive.com> 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::434; envelope-from=jim.shu@sifive.com; helo=mail-pf1-x434.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: 1744887315196019100 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 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.h | 2 ++ target/riscv/cpu_helper.c | 18 ++++++++++++++++++ 3 files changed, 22 insertions(+) diff --git a/target/riscv/cpu.h b/target/riscv/cpu.h index 51e49e03de..ac50928b57 100644 --- a/target/riscv/cpu.h +++ b/target/riscv/cpu.h @@ -613,6 +613,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.h b/target/riscv/cpu_cfg.h index a5b045aa2f..86b6240989 100644 --- a/target/riscv/cpu_cfg.h +++ b/target/riscv/cpu_cfg.h @@ -196,6 +196,8 @@ struct RISCVCPUConfig { bool pmp; bool debug; bool misa_w; + uint32_t mwid; + uint32_t mwidlist; =20 bool short_isa_string; =20 diff --git a/target/riscv/cpu_helper.c b/target/riscv/cpu_helper.c index f4c4b69a3c..2c3a0d903b 100644 --- a/target/riscv/cpu_helper.c +++ b/target/riscv/cpu_helper.c @@ -231,6 +231,24 @@ void cpu_get_tb_cpu_state(CPURISCVState *env, vaddr *p= c, *pflags =3D flags; } =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.17.1 From nobody Thu Dec 18 13:40:37 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=1744887441; cv=none; d=zohomail.com; s=zohoarc; b=On/Uu0yvasVraha9AFDPFsovaGrR1GkfgXaePAX727pgtxWSm+kcAO09SQusf6UriGvUMXjDkcjqz+f5Hv6wF6sAj/MjX2vAw+GLxwjnmLI/2aGLo4DbuhkTy6PWpC5vL8Yr1lfuFIVgjSLB3P8l/d6rlF7VaFWR/2KPtNGY3/Q= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1744887441; h=Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=31sA8ZoJNIVGckIUDC4jt2xxe297otB918AQ9sayA4k=; b=klge7I+LivtRQcfp7AcJJVRe2jH4qRzXdN/ayCbv1DSbYeCwiSkvVFTyEWs0WzKbOU+tazbQAsnPg1XwkdiYAmGOB7NSN5Ox3PzYE54NPLR6MBo/DtlB39ABbOeEoXwMvUwvXobaYoZN5Ke8JoguyrWAV3oZv+7hOSk9LwZ0a5A= 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 1744887440880467.3845594456926; Thu, 17 Apr 2025 03:57:20 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1u5Msf-0007fi-BP; Thu, 17 Apr 2025 06:54:05 -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 1u5MsR-0007cD-Nw for qemu-devel@nongnu.org; Thu, 17 Apr 2025 06:53:51 -0400 Received: from mail-pf1-x432.google.com ([2607:f8b0:4864:20::432]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1u5MsP-0003QJ-8o for qemu-devel@nongnu.org; Thu, 17 Apr 2025 06:53:51 -0400 Received: by mail-pf1-x432.google.com with SMTP id d2e1a72fcca58-7370a2d1981so427228b3a.2 for ; Thu, 17 Apr 2025 03:53:48 -0700 (PDT) Received: from hsinchu26.internal.sifive.com ([210.176.154.34]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-73bd230ddaasm12366251b3a.138.2025.04.17.03.53.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Apr 2025 03:53:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1744887227; x=1745492027; darn=nongnu.org; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=31sA8ZoJNIVGckIUDC4jt2xxe297otB918AQ9sayA4k=; b=nmUMWWfLY4qyOfwB3TFEJCNXlqN1f+Qrqh2E0d5mVZ05z4JX0FSVYXgfZTPoJZx7DR MfslXuyLyuHl3Myg0KnHQu2CNxlwOA+SuhvwpRdsrBF3+g85k+//J8qoOslMJfMI1I+u y0sh/CzjvdKGBS0nAOqkLXQkP6Ry3yTs8B82dokD/zMs1o1oYXMDnTR2um4KsM4Jw9KU xRo6aCLmhbpch85nTC8uOSwfFwNDwBroagu/uPEvxcdmDdPra1H2msHauR2XTeUNgdpw DC24I2SVeO3PDpnhrUaGFqRpsJBG2NCffUmlg4JGh+N8oJcTNSkfH7DjfFB5NAeFZqaN fhZw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744887227; x=1745492027; h=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=31sA8ZoJNIVGckIUDC4jt2xxe297otB918AQ9sayA4k=; b=nmgXAcZnwwlANqikHM+xxrH6Z7q8IwwO9q5KyI6mFFP+RU9OYQdVZXaSYXXuVzRd+K y3juGETcceqraehqJ8UbkZjibj1C26RuJ08Cqsrgql4Put4blFFq5iwvsD++yWb+RAqY dXmdwFgTqE07qTlQA2WR89WaAYAeMBh1flQVKsIz5ctrom77tEx6p3kjiOGXx+tCKCNt 1UFnoNvLHxljurvAJkLOXpuM9J7ab395nyjF7opwULwfJKOC4Nw8j4OCtljAKB+JcAyi E7EJFrj5bIOXpIg3PuwWm1iKZDnGff9wzylcWfoAtwGx8yEQ56qdWOh7O3bdDNibSvxk Jhow== X-Gm-Message-State: AOJu0Yx8kA/k3fPtNEKVy8IrNxhMd7Wjut0mUXulJqRTSs4cF8hVuy+o YWoAcuL/5y/Pg+oYoKeOz6Fe783fIp4NRLj7HLHzf+tERNYIgrK/ftN5RPgYmIR3LoDkKUdqSov n6Y8I4wk6WN0cTEzJTUJacfrUo37LqXD7N4BcpchHcSAfWbvomfmnCqwdGe/VzY+esoOwMw2SwE UE0hDGyKJkpd8dx/61D5PREIXkrMSUVQrXyw== X-Gm-Gg: ASbGncvas07tRjEwELJ5GSnnVeZaWj5epbO5Hddt2xqBDzLSL7P70nbSWK/oq7iIwLk 2d6wzCJcAQcco2InRsN9y3bdMP2wQH2m1KoWER8EsZ0XZgKJxRATFougyZm77E5szFnyn6KTgFQ oy5YOT8baQZF6loQ82P9zarOkCHNVViT20Osw6OSRkFb8DSfuUoev5LeKOUgPk/mH3yZVWnYV5E Gou0Lo0+LvjiGAv860lhNKfJWEKFxc7lYHWGQmJb6f8SbIo+Mw6sRa5vMD+t932oxkzaEYXDdKJ peg61Nn/WFL4s3nysGcxeqF49Ribs9EMlFoeBvwGcPIFg/1gZR+rQzP+FpXHpqHJRRrFaoYS8A= = X-Google-Smtp-Source: AGHT+IHdsLrCbuW1aeatAFe3irR5/r1fGLXPAu4NMLYEYp6v5RXrAbLkqQgEtPEohMi9Kv46uYpboA== X-Received: by 2002:a05:6a00:240c:b0:736:5725:59b4 with SMTP id d2e1a72fcca58-73c266b5fcfmr8696901b3a.3.1744887227222; Thu, 17 Apr 2025 03:53:47 -0700 (PDT) From: Jim Shu To: qemu-devel@nongnu.org, qemu-riscv@nongnu.org Cc: Richard Henderson , Paolo Bonzini , Palmer Dabbelt , Alistair Francis , Weiwei Li , Daniel Henrique Barboza , Liu Zhiwei , Eduardo Habkost , Marcel Apfelbaum , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , 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 , 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 v2 08/18] target/riscv: Add defines for WorldGuard CSRs Date: Thu, 17 Apr 2025 18:52:39 +0800 Message-Id: <20250417105249.18232-9-jim.shu@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20250417105249.18232-1-jim.shu@sifive.com> References: <20250417105249.18232-1-jim.shu@sifive.com> 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::432; envelope-from=jim.shu@sifive.com; helo=mail-pf1-x432.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: 1744887443607019100 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 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 a30317c617..7705c6995e 100644 --- a/target/riscv/cpu_bits.h +++ b/target/riscv/cpu_bits.h @@ -408,6 +408,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.17.1 From nobody Thu Dec 18 13:40:37 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=1744887508; cv=none; d=zohomail.com; s=zohoarc; b=Et5zO8HOYD/B3uHv2R+UUVamRt3g95XvOSunoNHF1SjH6ZqCpbPxTdOYmAGVwWooG6SwgFtqY7xufvCCbVK9aTDTgXkNYuPJ378S9ozfuV4PTn5tASDjfTH9KpHxKyAgeI/oqszwb0/4H0ts7kcJU0iZk225c9Jhkp2aM8Jy6Fo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1744887508; h=Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=Vz+7rJLKk52J8xcxBS8V6jPxUUQa+S+ay0i6P14+Mmc=; b=h+aki1ygyTGDF0RncwqdsH7I3x7wjQ5Q83TK49n3ZriotVmQU+SYRm01o7RKfyboWMzM+4JpCMvQ7vPBcWxyST/W9kdYQJoYLkdjXSTYP60pFqz+bontQzHoFanqUW4OZhRIlaPc5CmXP+Nzj1Yie+aCW1fvhhW79ULvs1NTWA4= 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 1744887508831484.9687811313519; Thu, 17 Apr 2025 03:58:28 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1u5Mst-00088C-KU; Thu, 17 Apr 2025 06:54:20 -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 1u5MsY-0007f7-2P for qemu-devel@nongnu.org; Thu, 17 Apr 2025 06:53:59 -0400 Received: from mail-pl1-x62d.google.com ([2607:f8b0:4864:20::62d]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1u5MsV-0003S2-0Q for qemu-devel@nongnu.org; Thu, 17 Apr 2025 06:53:57 -0400 Received: by mail-pl1-x62d.google.com with SMTP id d9443c01a7336-2243803b776so9597585ad.0 for ; Thu, 17 Apr 2025 03:53:54 -0700 (PDT) Received: from hsinchu26.internal.sifive.com ([210.176.154.34]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-73bd230ddaasm12366251b3a.138.2025.04.17.03.53.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Apr 2025 03:53:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1744887233; x=1745492033; darn=nongnu.org; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=Vz+7rJLKk52J8xcxBS8V6jPxUUQa+S+ay0i6P14+Mmc=; b=RlGEw3A7mt8SrbKIYvda6F/VuXxnZaXh1AGvVEMFHkrvMPW8Nxb05wOLMOfeAhyegk ID7AXxqbX2fVqIlf7C7Sgvxhxz+MBco00mXsRaqmbtKR2GX+m6mvYz3KYILd8JERVu12 1oq/qHhJ9uqE3gk+b1ZFr6XlFBjltNbG+q1Bu3zsYgih+Je2Mh7/GsOE1kZ265vI7DE9 e4vA4xTeIRE7hTBFhZohDQ6c7PwXCwXvfTobmZIQTVxlGD1PEevyUf0zuwPVH0VDmc/Y biyg6NKLSZnpAyUPUeCoYg3Ntk9JKdfMeBnE46Wt4Oj5QGR7XXeLieacp5BWnvfTYmEc egaA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744887233; x=1745492033; h=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=Vz+7rJLKk52J8xcxBS8V6jPxUUQa+S+ay0i6P14+Mmc=; b=F8H4k6akdKECBPk9BU9N7dBflPlTg4BQtPTI7UhrQtr4C+cYhjtobYAzxz3l8mFrZE +flEosLjek9b3jYuYm40hxAWK35nP7wR9mmiJbL3YC/vsKdBfeZmDRqI3u7eQXcWtvjp XzIymBtGPgo2iyUJEem+5RM7iSrExV6O0zpVfSTkF4VUTyYlFaqDazXAk/SYhM99olrV 9tiHPChHyp542oGG+/AJhKLeUQJPdUIYHgQaFs7lCpNs+xQlKfAuJxEJ12F0tEgc8WXx 5yYP0uE7Hh5pBggZWLM+AFddOkj/Ti2hAAJOWnFr71al4UwIFLApgAXPKt4Nxt6nEcci I8mw== X-Gm-Message-State: AOJu0Yznw6xYodh4C51KJUB34DjWvz5KHwR4yeTlNCALZsF26gja+H7w cxTtmv0yaN3cPmxk68xlQZ6yEDeZg+/qAuhaIFmRgpeq8WV47ZVOcyr3EM+G9+wFO6ngdoJraok GxrnANm+5fGb2orr1E8HaCTHM/KaEnNdQrHQibgxYVKoI1NAtm+i/G92PZwvYlq6i0ctOgwRwnZ 3z4Ir14iX2lVykFrAlyD/rN8We4pc3kfhKBA== X-Gm-Gg: ASbGncttRjxayQjBnrtOA8OAvibS8lCPlRjMbMXQbxewHfuUUhz4POm1tWh7NEw4Xe3 Wn4EGD2SPHz7piF/DCLC0xkrE6/qGGfBbdaLRSPSZd0ujC8p9XG/A1MFHyviAP+jOKRCxSmJ6g5 +QQEoEDtKel7iSJhh6buQaixIa2WPInOeGlscyfgnrWbDdsMTsg0scipBOxK1h91PpJMQfgoTLd cRT2PXrvl8eMf1iR7tKfrJeiBeQBsiNpyllZZWNEeNPgxfGgpn05Yck7kFwgJshf+7wzNSlrOfd W6FOo9dEPoNaqx0Zww3EP/aAwrS9r2wwMjhOkC1N1UQ/Db8mrs/UOo/vMjJinnI= X-Google-Smtp-Source: AGHT+IGzltfdtJbSZf8YTPoY/XTmtC7TK3XCLZHrCC3bsXH7eK1800buUunBw9IuipQn8mMusQMVOg== X-Received: by 2002:a17:903:11d0:b0:223:635d:3e38 with SMTP id d9443c01a7336-22c358db3f4mr77653625ad.15.1744887232614; Thu, 17 Apr 2025 03:53:52 -0700 (PDT) From: Jim Shu To: qemu-devel@nongnu.org, qemu-riscv@nongnu.org Cc: Richard Henderson , Paolo Bonzini , Palmer Dabbelt , Alistair Francis , Weiwei Li , Daniel Henrique Barboza , Liu Zhiwei , Eduardo Habkost , Marcel Apfelbaum , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , 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 , 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 v2 09/18] target/riscv: Allow global WG config to set WG CPU callbacks Date: Thu, 17 Apr 2025 18:52:40 +0800 Message-Id: <20250417105249.18232-10-jim.shu@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20250417105249.18232-1-jim.shu@sifive.com> References: <20250417105249.18232-1-jim.shu@sifive.com> 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::62d; envelope-from=jim.shu@sifive.com; helo=mail-pl1-x62d.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: 1744887510618019000 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 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 ac50928b57..6dfc260a07 100644 --- a/target/riscv/cpu.h +++ b/target/riscv/cpu.h @@ -500,6 +500,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.17.1 From nobody Thu Dec 18 13:40:37 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=1744887305; cv=none; d=zohomail.com; s=zohoarc; b=CxcCrEc+r0F6mZN1yibfNI9ORi04w2/KHm5wsS4pjsnt7rFfGecfrMYxm4BhAaMDMBTsptbx/L7wSmgNoRcc+gCQoL9XJgtPJcdyR/7eimEvPRpggbmeL3+KwfByVg+J9efDXMQ5Ck1mpS33xk6OrNPMBzJQqfHeK/7PwiV3UOA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1744887305; h=Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=2ynf1dYmwbYGI8KiYXQBOC7joESopxPzlk7K7/NwSsw=; b=MyIdxI1gyTYvJaYih0YcBJ1RV8PLUwV72tPK+rZ8JZ+oA5F+FOs5T3itrOC+7ShZEZpiggllvUKHod4zsUMRDN+Y2Tk7dTRN595CTzBGTpzg5IGgI75BSrNVQhW5dsEXfzD2tzl0r5RqwzdzQlK+t4VBKbe53wX1ezmsQm0AbxY= 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 1744887305680816.859021403597; Thu, 17 Apr 2025 03:55:05 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1u5Msw-0008LE-Um; Thu, 17 Apr 2025 06:54:23 -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 1u5Msd-0007gg-TH for qemu-devel@nongnu.org; Thu, 17 Apr 2025 06:54:05 -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 1u5Msb-0003T7-HP for qemu-devel@nongnu.org; Thu, 17 Apr 2025 06:54:03 -0400 Received: by mail-pf1-x435.google.com with SMTP id d2e1a72fcca58-736c3e7b390so449828b3a.2 for ; Thu, 17 Apr 2025 03:54:00 -0700 (PDT) Received: from hsinchu26.internal.sifive.com ([210.176.154.34]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-73bd230ddaasm12366251b3a.138.2025.04.17.03.53.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Apr 2025 03:53:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1744887238; x=1745492038; darn=nongnu.org; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=2ynf1dYmwbYGI8KiYXQBOC7joESopxPzlk7K7/NwSsw=; b=hJgxRkIVUoPyncr1oKDfuhJCrbGsWYdhS5iaeh4LLhRnp9SSX1cPovDHPSrtsw0H5R ecl0zA3wZE5G1TXTgeIgnQ53gkBtYIqRw5rvNXppO1DP7igh+sFiCxE58v8wgQEW/b1Q S2ipnTTqOV5MC0blZ6QewcnHz+5L4/1Udf7PIXe16zrE4Y00o7a5IKS/jfKa9EuRRc+C szBEAqvLMoSBBOxAvkXbpo1oqx5DgjzEx5o7ljAan1QwDhFXPooS6Oewq/F04gW/o54+ HAwHwicEZrG9Kz3u/A1YNnaLvPT7PZ8WCp8dyryro1/zaZe1l87oUQPGq4CF3BYkDhsG G/vA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744887238; x=1745492038; h=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=2ynf1dYmwbYGI8KiYXQBOC7joESopxPzlk7K7/NwSsw=; b=gdmGDIVeX/DZihxTGbkw7skMXQLdy0UfBdsXo4s1doOdPl7te5/1nYuJ16z/3M2s2i 0yQs8MenropTk6Gduf2o2EcshwMUvszD54j9F1yBb2BsiC0tf9/q/J5xO0JdXZMjZ/J/ NUCTfJV9h3mFi98eVq9SGq0ZYVbvLhFewDzAKuTHTe4KNxw+4S7mB+edRsFEBZ2HKo+b PJTn6LlbTfx2SXvm/+VbszKhGPMx/zitiKoY65cZXTMEockFsubXRppn44PMeA3z8kJd FpytcVlrKw6SFgvBH3Trrz7ey5K7YBnQRJ8uxQFFAvQi/ZMGetz7pLiL0ABS67sQjS3U LeWQ== X-Gm-Message-State: AOJu0YzXuijX8xVjAech+n1FCXfylQ6lhLwmP+tsSWiwEUVmqHtULZZ6 UlKFF9DEB6WsLqPz0NIP9CCE7K71U3cvtpUWwEQ5Vy3moWzFG2Op+X6/7P8L5vIHdsTGw86tb4h 59ygAiKqPoFdWS89stHMB6XMTROVz1UNX1Guo9Wy/TeulGoBKfmuRnd75anZ9gNhO0uu3kp+TaP zuak693bcaHbhIrLjYAPIyJAhxt8rpT5uDiA== X-Gm-Gg: ASbGncslUZNug2lE/3y7o8LwIBAPipf94B8zBiW54yRSiirHLqmmpT/NTr4HtAd19hl 2ciCder/y2E0mTML7zVcT9dE4eqfayYbeqW9Py9KSDnY4qS1Ts+YsJ8RIgDOv/c7IWM9dSdnW5D vdrl3VJAbuSIV3CVP5XEbDy0o9IkAahkTVHK9BvTuCBNU7xVrsQ/NPYukKTlnBuuAhVtrz2IBxk ViIVpcyq8KKlda/k52Shw0+mjlWLe9jhl/5qfC7Qh/YnMdtKY6/OfVWMsm9i7jhKG8BtJ7L3yxj EKLnA+uXzQyK3Svb7siY36yxQ9eHWFCjKpRYzOQhF7oZTI/gO/ibDPW/+d3Bfm8= X-Google-Smtp-Source: AGHT+IHgbOzKff8rpGr7vAShyE/TVFOAk9a78CZeOSMl2RxNdZiEp9IeyuiDmv2prSWkA1bYr5u2WQ== X-Received: by 2002:a05:6a00:1145:b0:736:4a5b:7f08 with SMTP id d2e1a72fcca58-73c267fe1dbmr8043631b3a.22.1744887238063; Thu, 17 Apr 2025 03:53:58 -0700 (PDT) From: Jim Shu To: qemu-devel@nongnu.org, qemu-riscv@nongnu.org Cc: Richard Henderson , Paolo Bonzini , Palmer Dabbelt , Alistair Francis , Weiwei Li , Daniel Henrique Barboza , Liu Zhiwei , Eduardo Habkost , Marcel Apfelbaum , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , 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 , 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 v2 10/18] target/riscv: Implement WorldGuard CSRs Date: Thu, 17 Apr 2025 18:52:41 +0800 Message-Id: <20250417105249.18232-11-jim.shu@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20250417105249.18232-1-jim.shu@sifive.com> References: <20250417105249.18232-1-jim.shu@sifive.com> 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: 1744887307759019000 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 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 a182e8c61f..1dbeac0509 100644 --- a/target/riscv/cpu.c +++ b/target/riscv/cpu.c @@ -1141,6 +1141,10 @@ static void riscv_cpu_reset_hold(Object *obj, ResetT= ype 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 6dfc260a07..7bffe62f70 100644 --- a/target/riscv/cpu.h +++ b/target/riscv/cpu.h @@ -501,6 +501,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 7948188356..614df37d00 100644 --- a/target/riscv/csr.c +++ b/target/riscv/csr.c @@ -5388,6 +5388,109 @@ static int write_mnstatus(CPURISCVState *env, int c= srno, target_ulong val) 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 */ @@ -6465,5 +6568,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.17.1 From nobody Thu Dec 18 13:40:37 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=1744887286; cv=none; d=zohomail.com; s=zohoarc; b=MrtmCgYyrd6tglMlmVuBY91J7BtXYqluNCCHcZSBK/vIsBhR/Q7VpijqCZDzHkTGDR3Jl4Zo6n1opTx6oYvAgrgXXMtIzbflryGbAJoH3pSeItB+i6F00+Xvy9JruRcybBZzTSlJk119NRNMz7kGgemQkKav92PoARQEETxiDuw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1744887286; h=Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=byOo/KqqWXeR72ANJUzCRKebW2bLJp2ySZr8t52WqcA=; b=Hae8BJot0G6LP1EcjGJC7C52eG46svsrMdIQYIh6LfyX43w4QrtKM6ch4jcKR2aLhGf6wKuEHdZyIIOffYyjARAsEt70Z9n96ZHkS050GI/wvUp/2N1qUPye5bmx3JRePdNStUY0NbZNSDkmdWaLbqVDs/Y86fGrfIEcgkWKL90= 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 1744887286663797.0805500658735; Thu, 17 Apr 2025 03:54:46 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1u5Msw-0008Kj-KN; Thu, 17 Apr 2025 06:54:22 -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 1u5Msk-0007jl-Au for qemu-devel@nongnu.org; Thu, 17 Apr 2025 06:54:13 -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 1u5Msf-0003US-8R for qemu-devel@nongnu.org; Thu, 17 Apr 2025 06:54:08 -0400 Received: by mail-pf1-x42b.google.com with SMTP id d2e1a72fcca58-7394945d37eso446400b3a.3 for ; Thu, 17 Apr 2025 03:54:04 -0700 (PDT) Received: from hsinchu26.internal.sifive.com ([210.176.154.34]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-73bd230ddaasm12366251b3a.138.2025.04.17.03.53.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Apr 2025 03:54:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1744887244; x=1745492044; darn=nongnu.org; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=byOo/KqqWXeR72ANJUzCRKebW2bLJp2ySZr8t52WqcA=; b=Wui/iPjQNqkeBTH46WqcPSha8S8S/Kg9Usj/hRPoJi2Itt3NFaWvnfDBKQ1le02IpD zQipbObLgYvqyiMrL/YK5AZ/4h4Un2Nv3Df4XbfNe+7x+w3dRaUhNxkXYE7ZVAhjtOzC 0jlEvZKCmTlC1SxNX0AhDRqxwzSYbh4jdSxQGI8wKJn9ZFE+WCrpr80y5aVbkWqJovs0 y4CO7FY4+hlBF4Hm4SxcwT+us52ervpx0egLTbTqNbvZ6r2ZC9NJstRUimZLbHNVSI5i jpdKlBd+gqbYzoyh9QzDzzHBE+iL526wI2XcjaapVUbbh0J5qzp8ZahT5/It+OTddfvu M9GQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744887244; x=1745492044; h=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=byOo/KqqWXeR72ANJUzCRKebW2bLJp2ySZr8t52WqcA=; b=eETBNxgqnp0Es1qUzx1Pik+cWdgiTo1tc2ErBGbiP+DsqIVEzk4/83Z4uVCBOq2uds UxpEduGIxt/nw2tZR85lq1I5vdaurP1IB50HSt+v9V/yfAxZsR2WCXCgAuypBq9/Ib81 2i0kl4iDmatpWqJV2xcw8PyrK8sWkWbV9fcuioPB66TZGJcSmUcgPJ9xLhoy/xEhePDy yzfji9gD7sWsEIlL1XIx9umzB4qdQrqWDG/4QPQw+aWsEPKKilq/s97ncEj9a2MSp1Sl WLX/zobiB0nwS+10yypEidP/rUcbVSEXg/BjZIgO4sgP/NqDe7tJOQhEiks/2TYvaMkp Z7og== X-Gm-Message-State: AOJu0Yx3Wb5khgbXl0FsL1W2YoOZ9mfuohR2etRkjK8cCNm+omMB15SM NDJMeSHaWEYllQZG1Wrr1rRQ4ddYLOALcNbYMUhvltqVAJY0S85GjOmmDyoyi8KYhFgxV7GIhAC Pc5yVR7cgTKXZW7YpjyLQK4K3pB37+LbzQSWaJ0LdllZpdv//xsaOaGLm7n/ReFb33sbGm2PRdv ygAr35tyCUUmUjk/GdIPjc8iaeoas07XZk6g== X-Gm-Gg: ASbGnct9dbGBoZDcWpjfJJzDwuEWZrB+WFUohevjqeKO9s24ST4kp/2hu1nFcFmwYJC MiFw4gcc/4Uhgl4kquUlSCnqRf2vHSQizRRDBwGfZ3SyivWoOX8iw1khlbycefmmMzbfrYlWbvx 1h/G51Q8iebHEd6d4ZMOwO2PVHl5RXYDxW3liUyycGzAWOC4BUwezAktZV9yNMp7FTZVeFR/2sA xb3je3Kl80ar1nwDB8T+f9N0T8IU+uXKEsYBFV3WgWqY58cQ/Jwps8VnGB7yiLlzNS7rmGqWCyW dRZrokBMH+lGzfRNIeKn3OmNYCYUqBc6wiFuug2w32t9Nk5PEfLG0wir42igE8KSnzYrwla6PQ= = X-Google-Smtp-Source: AGHT+IEBfmhmq1YHPMJByKurQOo2En1pY4GxzZObr4BYoP3yevnGlsYQpo9lA6t2Ux0hX5nMOmOdjw== X-Received: by 2002:a05:6a20:e18a:b0:1f5:7ba7:69d3 with SMTP id adf61e73a8af0-203b3e92326mr8230124637.15.1744887243452; Thu, 17 Apr 2025 03:54:03 -0700 (PDT) From: Jim Shu To: qemu-devel@nongnu.org, qemu-riscv@nongnu.org Cc: Richard Henderson , Paolo Bonzini , Palmer Dabbelt , Alistair Francis , Weiwei Li , Daniel Henrique Barboza , Liu Zhiwei , Eduardo Habkost , Marcel Apfelbaum , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , 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 , 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 v2 11/18] target/riscv: Add WID to MemTxAttrs of CPU memory transactions Date: Thu, 17 Apr 2025 18:52:42 +0800 Message-Id: <20250417105249.18232-12-jim.shu@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20250417105249.18232-1-jim.shu@sifive.com> References: <20250417105249.18232-1-jim.shu@sifive.com> 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: 1744887289085019000 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 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 1dbeac0509..1aba6dd853 100644 --- a/target/riscv/cpu.c +++ b/target/riscv/cpu.c @@ -3035,7 +3035,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 7bffe62f70..1fdeee7708 100644 --- a/target/riscv/cpu.h +++ b/target/riscv/cpu.h @@ -584,6 +584,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 2c3a0d903b..944a5b7ee1 100644 --- a/target/riscv/cpu_helper.c +++ b/target/riscv/cpu_helper.c @@ -232,6 +232,34 @@ void cpu_get_tb_cpu_state(CPURISCVState *env, vaddr *p= c, } =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); @@ -1768,13 +1796,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)) { @@ -1886,12 +1923,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 */ @@ -1984,8 +2029,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.17.1 From nobody Thu Dec 18 13:40:37 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=1744887413; cv=none; d=zohomail.com; s=zohoarc; b=jgByXHhDJg2TzwudMoxa92+XeFTVQOAKOdubzBx7GqmrG5QZ26NP2lTy6kSdVKzZGk+dbDxq/eV5xIRJKB9IEpmSiZS6F0oeqnqSYWNMhosHEYkCiCuVVbs6+/b3O1nTpNRTNMXLICL1bBAZMDDKrU2pi8dZLfQMMlog1fikAPg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1744887413; h=Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=vQTsKrAn76+UasM8uqjd/nLkuCgMzLsX/xbV+gpdXKA=; b=hDoWsp6O2BroRIEJBj3QK2G+a57q/9nh9bhpAzyURqVD7SP8b78aq8Zu7jDs3yiB9Ar7oQvlut5UxsSwUDvM4jNnHJr7U1cf/BYIHO8VuOBMYA3VBm14ZbTR1CJOeQXoiQgtxcy1XKznDGFdXhGFTODp+GNP4Xt1iN4lUhMLzl4= 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 1744887413686163.93995778456792; Thu, 17 Apr 2025 03:56:53 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1u5Msy-0008RQ-SZ; Thu, 17 Apr 2025 06:54:24 -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 1u5Mso-0007sU-SC for qemu-devel@nongnu.org; Thu, 17 Apr 2025 06:54:14 -0400 Received: from mail-pf1-x431.google.com ([2607:f8b0:4864:20::431]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1u5Msm-0003WG-Gn for qemu-devel@nongnu.org; Thu, 17 Apr 2025 06:54:13 -0400 Received: by mail-pf1-x431.google.com with SMTP id d2e1a72fcca58-73bf1cef6ceso511960b3a.0 for ; Thu, 17 Apr 2025 03:54:10 -0700 (PDT) Received: from hsinchu26.internal.sifive.com ([210.176.154.34]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-73bd230ddaasm12366251b3a.138.2025.04.17.03.54.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Apr 2025 03:54:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1744887249; x=1745492049; darn=nongnu.org; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=vQTsKrAn76+UasM8uqjd/nLkuCgMzLsX/xbV+gpdXKA=; b=I9+Q53nHVW8rYoQtt/fhjN3DE5MD6jM5TYEzDWKQGVWTDhANcxPTXjLiqumgdMo7Dx M/fPW0HrtdjOO8JoJuHLUEciAUYGPPIv1J09Vcj6oJNiPQKK57tgfSJ3ad9hEIRi3Yxk SfAVFCcpoHuakta427pMKn+Le2DhJjAX/UpSNo90qohO4ekf40ofyvN8dRPRcxYgqedV qhHjopERl0LlKh9cXraIj6o8TgjXz0Oz34cH0TdRJKdEdZRgC5q1gEY77qSYUvrxUWMf lXJqXf15lkXJNJl5AeVigSSv1nISzSIw3ZV0Vxfj9GGUtJD3SGkxHfahhVid4HIcQ5jz nadw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744887249; x=1745492049; h=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=vQTsKrAn76+UasM8uqjd/nLkuCgMzLsX/xbV+gpdXKA=; b=hJqRhDiGqdeEYCCd/Wx9sjMlEqzi3eYFqzFqGaCLBpBPcD2s+MdUZHd90BSxJhjtYo OF/HdvLs9g5mHQ/basvEHehBc63VequHXbIRakJMag/sLhIX9pr5CbfxoU+Vx2njbZXS Egu1F9ChbSDhEdQIarHXnRJU+YgfB2RzXqAobOkOJ6v04TNhKQErcyUz7KfRoF2VQ0sL KLfh/bGG2rJqbxMEsxLt0IVBbW72/TTY5qE2EUBqrrU+cTuKxBdx7gwDprQp7+Vsl5sk FgkNc45sPRYd94aoURC0Sm4chfZJzMzA8aEVKWWAqxvmh/Seu7/ln2QpDfXv4uVB4V4i dRaw== X-Gm-Message-State: AOJu0Yzn4JPM36Kvxd5aXsEbfMGrPEpPaCKN7VlfjC5y714oZkgvj3Yk WtZjehckEMOiXGwYFvSUHlCIEzbb7ED3SBSK8aAAsnXQ8nwku/7+uE28fgYkPNRn1me1i512qHi ZBxVik6d2wR1X1Q+FJb5jckV9P8Rx6Ktfw/SG2daiNd7kb+K6KiPUlknYUad+b60IsD8ESowQFl 8K/WzEkgA5gdTxP3YDSnFe/sJWncg02MG9VQ== X-Gm-Gg: ASbGnctu2y8zapclrvAIWNuusxYxJiY7xP3J3+JLknHIzUZ2bf9/mjFBiOUqJmjDn0P fc4PmHxddjmcgrFLi78rnpudauue49yevM9Vow1bNoZQzGLnKUFmyf6xiatkk9EBmhc8RLFr//z gMrUmBKzPArU6a/uaAbLZNCbErCwnZ1a0L5l0se8ylM9GJwEWAhwpUtqWAtv4nHzXXEScdfuLNd rKtvs3pFMcYcFE/0fhqmPi6g/8ECs5tPWw9JoDZgXwuesX7BFpbMN4twpQT5DKlmRcfdpEuAhSH SN339KHcIRNy6qgkpbydLHjaAu8logMdenSCrvMjV/eBIFfW2y0LtSiL/8Xv95s= X-Google-Smtp-Source: AGHT+IEekOeXlJxQgqE9ZWPX/+ewPT7W/wX1P7lgDnVNwXejZ1pv1ga4HdqDy4pgWILsWxLishEcHg== X-Received: by 2002:a05:6a21:150b:b0:1f5:97c3:41b9 with SMTP id adf61e73a8af0-203b3e4ffe2mr7406779637.5.1744887248819; Thu, 17 Apr 2025 03:54:08 -0700 (PDT) From: Jim Shu To: qemu-devel@nongnu.org, qemu-riscv@nongnu.org Cc: Richard Henderson , Paolo Bonzini , Palmer Dabbelt , Alistair Francis , Weiwei Li , Daniel Henrique Barboza , Liu Zhiwei , Eduardo Habkost , Marcel Apfelbaum , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , 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 , 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 v2 12/18] target/riscv: Expose CPU options of WorldGuard Date: Thu, 17 Apr 2025 18:52:43 +0800 Message-Id: <20250417105249.18232-13-jim.shu@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20250417105249.18232-1-jim.shu@sifive.com> References: <20250417105249.18232-1-jim.shu@sifive.com> 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::431; envelope-from=jim.shu@sifive.com; helo=mail-pf1-x431.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=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @sifive.com) X-ZM-MESSAGEID: 1744887416775019100 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 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 1aba6dd853..46df970fe3 100644 --- a/target/riscv/cpu.c +++ b/target/riscv/cpu.c @@ -1742,6 +1742,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 @@ -2975,6 +2980,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 #if defined(TARGET_RISCV64) --=20 2.17.1 From nobody Thu Dec 18 13:40:37 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=1744887282; cv=none; d=zohomail.com; s=zohoarc; b=carbdCvRo76Cl+Kjylo9KRfFsndpi9AHns5WVpfA0GWfvcUeYiwmrjAM6ZHrGzerBEFy6IbPmJtCmx0jfHcG5YhS72dq9jox0v0uNi92Ia2zLpmTAGOxxfmSVjGLZgdsYCC/BDO7Q5u8dR3Pq54RPR1ENbK9HJ3vLqRCZAnc+4A= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1744887282; h=Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=wmWgl0GHsRaxiJyu5Hk62jxV2+HXDxpJgBZBEMvOZO8=; b=j/iC5J7KpJzWFD7ZqALxJ42HLPDXOOH+FFimuAy+ab+ptlztGvBcmc4l0Lsx71li7lMQu/QI0QqGZ1uokPASYD+aa3GCiw3yhALG7his2dVEQzh/QW/5Spky7lR8JBLv95jDJz8jHeUWDDrvdMS7082Mc22pYuRw0NAW/kMUdcE= 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 1744887282561409.26893061689873; Thu, 17 Apr 2025 03:54:42 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1u5Mt0-0008UJ-JJ; Thu, 17 Apr 2025 06:54:26 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1u5Mst-000891-2u for qemu-devel@nongnu.org; Thu, 17 Apr 2025 06:54:19 -0400 Received: from mail-pf1-x433.google.com ([2607:f8b0:4864:20::433]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1u5Msq-0003Yt-7j for qemu-devel@nongnu.org; Thu, 17 Apr 2025 06:54:18 -0400 Received: by mail-pf1-x433.google.com with SMTP id d2e1a72fcca58-736c3e7b390so449979b3a.2 for ; Thu, 17 Apr 2025 03:54:15 -0700 (PDT) Received: from hsinchu26.internal.sifive.com ([210.176.154.34]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-73bd230ddaasm12366251b3a.138.2025.04.17.03.54.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Apr 2025 03:54:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1744887254; x=1745492054; darn=nongnu.org; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=wmWgl0GHsRaxiJyu5Hk62jxV2+HXDxpJgBZBEMvOZO8=; b=Dsh5o9sZ+zYV684ovMrynO4TdXNa5wE+VXlIt/uSoiA/fIXDkSTLj84yBDrI+F5Yvf ghAUUsyb8am4uwUj+oRxh3DpcBUymbPjWMyNKF10T44r4sHyc81/+GuaYYgEmV6DtrlS Wt5L/3E/HbTekpkxvUEVlc7DGUnK1iPnZptgqjP+ehMHdZBLCLP9l38K2PkmiFVzceM8 sWDban404v9GS5Dj1vxQRSS9X5YBbIGO/vq9OL3ivTogdjWAvOZSvtMX5OiFsWzxWmRx /eRRm3fXnkp926PTfIa7qsqmQ7q/v6ImfpNn9/h9hxtXJKgnCnJdhDFvqvyNUtgyeMsX 501Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744887254; x=1745492054; h=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=wmWgl0GHsRaxiJyu5Hk62jxV2+HXDxpJgBZBEMvOZO8=; b=iD84h1lnFTPt9QqGzDZ3RNTrS7zuNGCrxnZp71SWo1A1suAPamX6zXh9K3Ybjkfcxt fL3XA/nfLhx0okfhAJkUUEiYHFQ6dnKbMEzs6fkeJYkAuVTFTnQi+/XS8oO+sN0cq3vE C7R8oDfGsHW1gsUz/zIHfsNm84xKGGxjKhrKEuW80lidZ3lKoBPD01NSZnWwLRFyz7LN TYScaR9UgbpyPPuu2fQhnbd4YSRsLB+n8LWpmFUKa5EG9w3C3htRFL3LgjkVi+xfYpjv V3OERNmrjTDbXQpPMjGhHmJYdyUjj9w10qYyge40rvLv+0vgy9UcFZ6LQhK+MHv7VzvL oTsA== X-Gm-Message-State: AOJu0YwsI6vZoHZqTtfHwY1EUB5UQhzfNDUeKm/hktSF7aVU5Zqy/nsQ LeJAHUEYAc/hKMOnb7m2PPqcxiZ0IpFyQ0ELd9/ItcQi1gwfYhQ7aMr/Vc/4MChHZ2LTz1W9zx2 F42IPNxMFacPYspwluQhF2uFLbPUeQaFWQXw012QWTxwxO/AqZW0xh7YfgFI77bQKZWDnpXpOpU viQ9n+IRcvmQXSTASdPXjuCWUN1RcNyxs0wA== X-Gm-Gg: ASbGnctU0jI6rRcNg9TqMrasIbqlrzONqzAMEt3Jod8PB6iF3H/eN1Bs/ZK7BZ6/kl+ fBcp51KwCb/5IvGGrd9l/qmi1uQ2I5LM9JUP3ILDr9qjVjgk2hNdBY//b5LYxWIsJIYQYnuighh 7ReiMB/DgMgQ03/6PStgCJLWqkJoIy1jMglpbFVE7x9EooddZ5cgqRAytMZNPOU44ICXEl0otVU QI1TOEMXc/lX9lr0bPNy1CqlMoxP1RF2ppt1acLxn0NUj72vmczAQNPXwvDpdAYwb1aeqOJcTRy JlFS73lfTY7Jw7kvpFPD+bjXqhiO5spYf2jANky/+GkpkDFC2jTCtKbg8ZBB11Q= X-Google-Smtp-Source: AGHT+IECc63NCIK987B/j/uELTkz7L/JtCbP40ku5xPL7tfpLlmeUFI8Jsywa0uCscADRXmakFVT+Q== X-Received: by 2002:a05:6a21:99a6:b0:1f5:86ce:126a with SMTP id adf61e73a8af0-203b3ff78f4mr8932278637.40.1744887254253; Thu, 17 Apr 2025 03:54:14 -0700 (PDT) From: Jim Shu To: qemu-devel@nongnu.org, qemu-riscv@nongnu.org Cc: Richard Henderson , Paolo Bonzini , Palmer Dabbelt , Alistair Francis , Weiwei Li , Daniel Henrique Barboza , Liu Zhiwei , Eduardo Habkost , Marcel Apfelbaum , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , 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 , 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 v2 13/18] hw/misc: riscv_worldguard: Add API to enable WG extension of CPU Date: Thu, 17 Apr 2025 18:52:44 +0800 Message-Id: <20250417105249.18232-14-jim.shu@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20250417105249.18232-1-jim.shu@sifive.com> References: <20250417105249.18232-1-jim.shu@sifive.com> 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::433; envelope-from=jim.shu@sifive.com; helo=mail-pf1-x433.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: 1744887284226019000 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 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 | 87 ++++++++++++++++++++++++++++++ include/hw/misc/riscv_worldguard.h | 1 + 2 files changed, 88 insertions(+) diff --git a/hw/misc/riscv_worldguard.c b/hw/misc/riscv_worldguard.c index b02bd28d02..1a910f4cf3 100644 --- a/hw/misc/riscv_worldguard.c +++ b/hw/misc/riscv_worldguard.c @@ -92,6 +92,93 @@ 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) +{ + /* WG global config should exist */ + g_assert(worldguard_config); + + CPUState *cpu =3D qemu_get_cpu(hartid); + RISCVCPU *rcpu =3D RISCV_CPU(cpu); + CPURISCVState *env =3D cpu ? cpu_env(cpu) : NULL; + + 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 8a533a0517..211a72e438 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 uint32_t mem_attrs_to_wid(MemTxAttrs attrs); bool could_access_wgblocks(MemTxAttrs attrs, const char *wgblock); --=20 2.17.1 From nobody Thu Dec 18 13:40:37 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=1744887446; cv=none; d=zohomail.com; s=zohoarc; b=JMo/AzLcxNfgHgvMrbEX//bMR2bjK8cBdwzvHT1/MpJBCvdLYmJHWrQ1zPUcJvR8Hh3ZWWCCHMgR3Gt58k6CCft+6chs2eScK0wfPz8nnR/gIczcjjlLuDRw+KVpZv/spxea/Oyv0SZDZL6Rsz+bbhzzvO4X5G3yaer0vf1HVN0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1744887446; h=Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=tIJ3izDwhM7rqWWbObHdV4Xt43nkxUxukpA0UtZRKQU=; b=IAQTY5c6+iPggMyGW78K2g1YpC0SBbE7++z+80VageA+viJCPsyGUdG2iSkkeHb1CpFT8ioVHW8VDwIGii20xpJVE6yoX+iC0TJGpj00MLLReLkxbbHRUq2SXz7wXZ5BEDI/cTwDd3wGMBdJuY66RkJwyht5gNtv9jCx1bSqrSY= 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 1744887446532906.8322567395471; Thu, 17 Apr 2025 03:57:26 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1u5Mt2-0008WL-SN; Thu, 17 Apr 2025 06:54:29 -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 1u5Mt1-0008VW-Qu for qemu-devel@nongnu.org; Thu, 17 Apr 2025 06:54:27 -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 1u5Msw-0003cO-1Y for qemu-devel@nongnu.org; Thu, 17 Apr 2025 06:54:27 -0400 Received: by mail-pf1-x429.google.com with SMTP id d2e1a72fcca58-736c1cf75e4so436866b3a.2 for ; Thu, 17 Apr 2025 03:54:21 -0700 (PDT) Received: from hsinchu26.internal.sifive.com ([210.176.154.34]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-73bd230ddaasm12366251b3a.138.2025.04.17.03.54.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Apr 2025 03:54:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1744887260; x=1745492060; darn=nongnu.org; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=tIJ3izDwhM7rqWWbObHdV4Xt43nkxUxukpA0UtZRKQU=; b=J7aePoEXsb/1ygPQzk0YKBoD4cs8t7T2hTCC6XQggA31kajZO1y8hkOV5Mipu3fNAo J9mK0fRjQeoLnIkgqOtSIRuub6eLbFGRTjLb2YQC2BMhjnZDFvdaDlnIM6aU4I9dIgmW bhrlgIN7l+rBmMe/wHd4I26FVQd9x/GIPc84lttE2rSeY2UGphBbCIiU5isIjr+SreEL ijvEJK/MBdN4AMvH84Qn3vtFd3O0pxgnFv6ppA8AErXmSPlBO0B323fDsqt9HG6NuQ04 tWzcQX3wWX7Opbby1PyMAsJmpmbCV+QwhwY/vpEuBluOXWWCAEQW/xxhSkR1U7bYzsks 3QBw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744887260; x=1745492060; h=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=tIJ3izDwhM7rqWWbObHdV4Xt43nkxUxukpA0UtZRKQU=; b=cfHykV3qWa/koSS4fZbt4ycgWM1JUwfJvg4bMT/AjcEOkaktg+kzwCTm+QajMV6LHC XUSl6fpA+w7jEP3UHzpmOeOnKJXHEnUngD0zCTfSgWOcohliF4br0z5o3J4u6BugUjgb 4h5VNN1YwY0qnFkZDX+KSaVe87EatUExP9ETOmUw8s5OMWmSUfNS8ujlc7Eur8sa70AQ aiHbf2sfoY84xQN8LtMlKCDWA0B4APOD1bOhP4ezPtfurV+wnFoWwJIADbfu2GMLkNkP M/ba7YURA72dLyqwpGbE5s4VSXJmV1Szi4iXyYAz0czKYbdOcbV7xdulaQj60ttf08Mu 3SPA== X-Gm-Message-State: AOJu0YwuEooRWpg9//3rGHdymqOWprlhstr+Me34leEqIVYJq0zccxQ/ f50Fg8WfuYLI/N0/GONCkeqeY/CPiMICnDAGBDtOBm8S9GMTIbzO7f0uUzqMSbtUTQO6U8tKBuz +ds/tF2vrXyr6C0ShmdyvQibykw0qtXTvjwQXHDVR/8lP8ypE/dTmdqKnEUmEAzhdukqI2HQNsl ETnXtgEjOWCgrgTC8uY+kDcNz7k1Fsjaw0tg== X-Gm-Gg: ASbGnctMyMXoqSl8AOGZ47gDsO1TejkN99nenc9drXB1lnLHTsZl1IbuM/eY0I5XPfx 9OvTDvGBu5CumNL59LAauAC6czVBBW4wdhQ3fmhlF8vzPAFCYWQlDbexNTdRKQCisPIhGBC/7Ma k3WjfGr7D8GpBw44Iweyj2yg67OxVupyXnVEfvky3dasREeWrm3+UZJXt4OjKpDce+yRL/H1RnX noJ9DC4oazG4tfPzQ3ttGX9Hz2BkyQnStIYYwhKy+SDN63uvw3pRyT+iqK7aJ20YxuiNKDwkwjq Lfr0J8AqSmGs4T9awvdISSWeZAOgDcVVIdCjDxinWdFRURusUzXAFZgFhbr1QEpVp5ONvgZitg= = X-Google-Smtp-Source: AGHT+IF40f822bix5T7GlrTTNXiZpe0a+pnE6+4c9T/1uqdysjEy2VfHjvCtsnqbFP4gPuZJWv+TZw== X-Received: by 2002:a05:6a00:888:b0:72f:590f:2859 with SMTP id d2e1a72fcca58-73c267174d7mr7807724b3a.13.1744887259678; Thu, 17 Apr 2025 03:54:19 -0700 (PDT) From: Jim Shu To: qemu-devel@nongnu.org, qemu-riscv@nongnu.org Cc: Richard Henderson , Paolo Bonzini , Palmer Dabbelt , Alistair Francis , Weiwei Li , Daniel Henrique Barboza , Liu Zhiwei , Eduardo Habkost , Marcel Apfelbaum , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , 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 , 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 v2 14/18] hw/misc: riscv_wgchecker: Implement RISC-V WorldGuard Checker Date: Thu, 17 Apr 2025 18:52:45 +0800 Message-Id: <20250417105249.18232-15-jim.shu@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20250417105249.18232-1-jim.shu@sifive.com> References: <20250417105249.18232-1-jim.shu@sifive.com> 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: 1744887447843019100 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 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 Reviewed-by: Daniel Henrique Barboza --- hw/misc/meson.build | 2 +- hw/misc/riscv_wgchecker.c | 603 +++++++++++++++++++++++++++++ hw/misc/trace-events | 8 + include/hw/misc/riscv_worldguard.h | 63 +++ 4 files changed, 675 insertions(+), 1 deletion(-) create mode 100644 hw/misc/riscv_wgchecker.c diff --git a/hw/misc/meson.build b/hw/misc/meson.build index 3d2f4bb6a3..73c11bc7c9 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..ea50f4f53a --- /dev/null +++ b/hw/misc/riscv_wgchecker.c @@ -0,0 +1,603 @@ +/* + * 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 "exec/exec-all.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); + + trace_riscv_wgc_mem_blocked_read(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); + + trace_riscv_wgc_mem_blocked_write(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_wgc_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 wgChe= cker\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, + 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 16 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_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 at 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=3D0; imem_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, void *data) +{ + 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=3D0; i*/ + 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.17.1 From nobody Thu Dec 18 13:40:37 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=1744887365; cv=none; d=zohomail.com; s=zohoarc; b=KOuH447rLTJDaTtUxdy4Viyohj7gVNn5xrdiQyr3ty34NQvTMKQGYPg1AQB3dMP30rApj1Rp2G5NUk99pvbkst9eDweh9ahcJwEZikTAcQ5K3BdxjxCu/7K55i4N1xbsiQC9t1c3n03PTwkBeJxMyNw03arOnCEvyeeHi28EIMk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1744887365; h=Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=PalTCHeL3ElxHt3CezuVo/2+iw/lympwyWxiua7uelw=; b=PRFTA5MU2MX+xiHZV9zGDfKGZhDlo1w6bZia+v+JMt3sr92WYDdCYe1ajfHKQHjEUExeY2uzSXFP94vLh4l1weWI3BpIPHmMxYg6I6IDqKIUuMmoHVwOiDM5vdPnTNu75+FBNX495Qw4dJZzXELj6LC7PWnpy8leftIKKvf1Omg= 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 1744887365350227.1794746957903; Thu, 17 Apr 2025 03:56:05 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1u5Mt6-0000BU-U3; Thu, 17 Apr 2025 06:54: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 1u5Mt5-00009U-Ac for qemu-devel@nongnu.org; Thu, 17 Apr 2025 06:54:31 -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 1u5Mt2-0003f8-3B for qemu-devel@nongnu.org; Thu, 17 Apr 2025 06:54:30 -0400 Received: by mail-pf1-x42b.google.com with SMTP id d2e1a72fcca58-736b350a22cso463082b3a.1 for ; Thu, 17 Apr 2025 03:54:27 -0700 (PDT) Received: from hsinchu26.internal.sifive.com ([210.176.154.34]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-73bd230ddaasm12366251b3a.138.2025.04.17.03.54.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Apr 2025 03:54:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1744887265; x=1745492065; darn=nongnu.org; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=PalTCHeL3ElxHt3CezuVo/2+iw/lympwyWxiua7uelw=; b=Q/2UjNj5KfJzrJHG0tOuvwGS/g8AZFg1a67CiDqvbeTeWy6h7p464+h4n5Mh/CsL5y v92f9Gv7d8wNnfz43H1E4IcTiDbQGALKS3Wu9RJQ+SGOpcX1nkGh+czskOnKCagpfwSB eJXGYFooE+MPnZhV1wWHVA2KDGKx5LaUsSqJgBoA9CzBphEeshtN6gT2bBPLsBaYubaw vciIke4s6FypxGiazd7l5cTBayxDyg09XwBNymbwMUoRGIAKVWwgtnWEEoVW9Mi8logi DwdjIdNgsA+5X9E2hVwJsp91ej6BZRjTVlDQZ/niE1z6Ic0lcMSLXZGkqkeEMBGaC5LE IXLw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744887265; x=1745492065; h=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=PalTCHeL3ElxHt3CezuVo/2+iw/lympwyWxiua7uelw=; b=TrHfo6g1PUNqaF/5VSEleNhdzm0j/NLkEAI2afMypq6c2CcAOSPIn4DhtFMJ0HFjiW V+4+E5GhF7tjg7EBdTnTRjVWTMZkclybRgkr9QYeJsE1ntx8LAbAPmqzKuY56v3A/SfP 5cypvDumMw1ADSs5N84NJLLs54IMFgg7HgvRr69wCw7CWVzHGj2rxlY9+EGPn/XH1cUG u6b98xalLfTzumVtIIwmF0lc3uHiDkdO4OhIXTxwAFlY/gCvvV1nSfsN0kFPE0li+AeK uZcu0XSOHS4PJpd+40kaxMXTf3lcs5jiUsep9X8rUkcJ44wrAUJJRFnyrt6DwokvjZ1V 4txA== X-Gm-Message-State: AOJu0YyJ7sl5JZBmB+z+KOwm3Pm97TRfy8Y0JENqt8Hs5+bn8tpoBG1q YnnT7XwkyPIMfgPigDM+9ZVQAyO8lDyB8R9bhOWzSp7fx6LmpY31088lqpn/FtPteYmx3wMenv9 qYMZrRVmWxTmXkZ4oQ78zmhTEcsZgX4l96owiDkG9JkVOPsXlghtjnfrspbT5N4ny79Xsv5fULR 62lFxhha1L0OMQ1zHjyldtzoJF8SH48tqi5Q== X-Gm-Gg: ASbGncvrCoSQHLUhAUA5c6G4u8aHGGW+Cp9RAIEQaSKn+DjgpPi0Rb65ceQZ4Orm5YJ F/nTvrsL3hG/CCm1hY8SP1svJ2sQadZAehe+qPHp9NrC5j8vb3mHvFVfrEe9mOPt6dqEB5DYcUo PrCDY99QJUJBs/qItysn1N/da5gTW4F3rQ0NzEexJlmZkuBFBRpOVOfUZdA6OXKS8rWUR3SjNWp 5Yt7UVhjwe4roVw7O1vzFqANHmhlmrBizg+4vDcES9eIgxWzLHZPMmb4ZN/LMNag6b7Nu8PCkSb G8jQm3CJCCuWmaHhVlRLxNVoDUkJiqWSeZD0DsTMFeNcWG9lc/FgXtT/yjOBbeY= X-Google-Smtp-Source: AGHT+IFb9NFOkDuZrltKII+EmDLWNKkqGGj9dnre7XEf8OCLAjzcROnJy/+vMybnUVS5/ETTgkkJbg== X-Received: by 2002:a05:6a00:1252:b0:737:9b:582a with SMTP id d2e1a72fcca58-73c267fa0dbmr8516319b3a.24.1744887265068; Thu, 17 Apr 2025 03:54:25 -0700 (PDT) From: Jim Shu To: qemu-devel@nongnu.org, qemu-riscv@nongnu.org Cc: Richard Henderson , Paolo Bonzini , Palmer Dabbelt , Alistair Francis , Weiwei Li , Daniel Henrique Barboza , Liu Zhiwei , Eduardo Habkost , Marcel Apfelbaum , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , 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 , 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 v2 15/18] hw/misc: riscv_wgchecker: Implement wgchecker slot registers Date: Thu, 17 Apr 2025 18:52:46 +0800 Message-Id: <20250417105249.18232-16-jim.shu@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20250417105249.18232-1-jim.shu@sifive.com> References: <20250417105249.18232-1-jim.shu@sifive.com> 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: 1744887367480019100 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 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 | 330 +++++++++++++++++++++++++++++ hw/misc/riscv_worldguard.c | 3 + include/hw/misc/riscv_worldguard.h | 4 + 3 files changed, 337 insertions(+) diff --git a/hw/misc/riscv_wgchecker.c b/hw/misc/riscv_wgchecker.c index ea50f4f53a..8839d898c9 100644 --- a/hw/misc/riscv_wgchecker.c +++ b/hw/misc/riscv_wgchecker.c @@ -53,6 +53,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 @@ -146,6 +192,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; @@ -171,6 +239,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; @@ -228,11 +327,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: Unexpected memory access to (0x%" HWADDR_PR= IX ", %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; @@ -253,6 +462,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); @@ -460,6 +744,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); @@ -523,6 +809,37 @@ 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; + } } =20 static void riscv_wgchecker_class_init(ObjectClass *klass, void *data) @@ -596,6 +913,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 1a910f4cf3..59ca6d16e7 100644 --- a/hw/misc/riscv_worldguard.c +++ b/hw/misc/riscv_worldguard.c @@ -38,6 +38,9 @@ */ struct RISCVWorldGuardState *worldguard_config =3D NULL; =20 +/* perm field bitmask of wgChecker slot, it's depends on NWorld. */ +uint64_t wgc_slot_perm_mask =3D 0; + 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 7b5aae866a..f53202524c 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); @@ -86,9 +87,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.17.1 From nobody Thu Dec 18 13:40:37 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=1744887379; cv=none; d=zohomail.com; s=zohoarc; b=erckeag0+y4MQ36l1SQyrgb8vBfRkY+s5EhslrNkU3/r4w+u5Jt3HYX838fsZb0B2xRzyviKQUoIy8e6n+F7eiuLtdUmEFE/QeEWaD9z6P7c1/VQyJ7+2QD+3eso6HU38uSg7nQragzTQVnT0L7lqWeIxrlQdvDyBU6OH5+SKf0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1744887379; h=Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=r8MkLirfRd+1eYBUne8MUcSIMDu4+B+icN4ZLFGI96k=; b=oLj9x17qcw8RsEqy//gjxtIR3Wwh9MVrx7LCs6YtFgdHKnws1ZK6FAs+SLnGGEkofnNAgEQo8iDWl/WHs6M4/tBsGI+MvQkVaszY8dYRSkkZg3j+/3p0kSFPkOTeTHDMb9/AGgSi2p0BqTx/DJgcyKeDIs05cg9jldH56/9Rgt0= 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 1744887379021458.78508767881; Thu, 17 Apr 2025 03:56:19 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1u5MtC-0000OY-Di; Thu, 17 Apr 2025 06:54:38 -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 1u5Mt9-0000M0-Pt for qemu-devel@nongnu.org; Thu, 17 Apr 2025 06:54:36 -0400 Received: from mail-pf1-x42d.google.com ([2607:f8b0:4864:20::42d]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1u5Mt6-0003iE-Ll for qemu-devel@nongnu.org; Thu, 17 Apr 2025 06:54:35 -0400 Received: by mail-pf1-x42d.google.com with SMTP id d2e1a72fcca58-736b98acaadso559613b3a.1 for ; Thu, 17 Apr 2025 03:54:32 -0700 (PDT) Received: from hsinchu26.internal.sifive.com ([210.176.154.34]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-73bd230ddaasm12366251b3a.138.2025.04.17.03.54.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Apr 2025 03:54:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1744887271; x=1745492071; darn=nongnu.org; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=r8MkLirfRd+1eYBUne8MUcSIMDu4+B+icN4ZLFGI96k=; b=C48xIXmFsBxF7mHQ+Z+GUp+Q2RY9bSxUdgRbdLmZI9Q8iMdsABWtU7IpGy2iXdIVJG reZVBQlPqpCDRmdypGM6a1+3IzA9+/Huusm1x1kqyocvHsRFaCo4yskQahjijy2gsEgy 8Ik7OTl7xx8gHRd+W/NKPsaAtwliFrlj2L6qgtLSh1FxaILslAgXlnrEUiHztWFkXV83 DI+yj6tSl37Xz5V4x1b1k5bn+KzL670yTkNE4mI7HMKYrkfW9+cFkqZKBam6mPx23shG w7lLNWdMe1ivKPZJp1Zkm2S3NUtutpNvmU5RbqtyE1+dHoLW+F2XaqZBdw95li8pJR0Z 8ICA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744887271; x=1745492071; h=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=r8MkLirfRd+1eYBUne8MUcSIMDu4+B+icN4ZLFGI96k=; b=cQWeubfNNNP0hyG57w+EdsUsvc+ZTHFYbMLoFdY5rrKNuypa7LW1SFmWQgxLsdSHRD bZ3ESlPxIE/UXqrfozLRXV5VOxdI+NgAip2ZW/32eQzYe5OdSRKdX0sv5sx+J5Vj/Wce JYToHAkkgeTYEyFYRNR90vYGay/ti/z6Yq9XpxjH9ljOPKoe57x5prTZG7bGipdh1iYM Cgu0TL58UnlL93WoAbYofqOMA8fA70jtqWl9q7OhGqNIv6Yrfu0YgHbyEWSF41IeIeW5 3qJ5Ts/2tEbz2XJcquFSYMlJcjomNXXuE+IYoVTHzA7+0mdIqzRCQS52WlUFtgbUUuo0 XuBQ== X-Gm-Message-State: AOJu0YwhljlMQDtNE7Kw7jaQo/9R8dcsut2yeXkIoxhRjokLYZTJv9MU OGKDhB9jiGpIDb0gvZYdMV9zwrCAqAHt/THQEAxObluyKN7rS1raM8anjov8WOLF1Lu5ihT2xuF PF293uApvJFblPuSo/Dems0f70P4UIRjTSgONngHe64PkprFPRHrDxix7V4s/YOlv+LWPWIljj2 SpFH2J9WHN1yM6PDlVYAyNyGj2GVYa+DxJjg== X-Gm-Gg: ASbGncuHjJuEM5lG1sQEyOpP4eqUZAEk9Iv249Tfmt04q6IQD7+lHFPxV5MQ3FdABys /koDB5vgApS0Qg94V4mw1+S1FibemAhdJpYe4BcopZx+ou3U5IRY+/i76gRVzRYHkARs4NvKamj ps2pCEb9OXscXz4Xk04gEXKcBvSHFUAkhZNfT6H17wV5luutLxh8iTqLIPN7TW44gbkU8H8lxHM ZKbnIZW97qSB9a258462G1K6qivCePDP4skgpPiZBxP3N7OhIEw9Oh+MsKOsWdstdbVc6pOsZiz YYPNwsfMzl1FP/xXtcmOEsPeK7u1Ps1tR3N6KJxukuaW0JDMX+wN0ZOs3rS/WW0= X-Google-Smtp-Source: AGHT+IElia6RPXyh3KzKaXLcl6TnKXDgngxoI7YijmJNyiQlaxFIOp7ANOpA1cM15IoLDVGO9WH/AA== X-Received: by 2002:a05:6a20:d709:b0:1f5:59e5:8ad2 with SMTP id adf61e73a8af0-203b3ee9a1dmr8408810637.24.1744887270404; Thu, 17 Apr 2025 03:54:30 -0700 (PDT) From: Jim Shu To: qemu-devel@nongnu.org, qemu-riscv@nongnu.org Cc: Richard Henderson , Paolo Bonzini , Palmer Dabbelt , Alistair Francis , Weiwei Li , Daniel Henrique Barboza , Liu Zhiwei , Eduardo Habkost , Marcel Apfelbaum , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , 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 , 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 v2 16/18] hw/misc: riscv_wgchecker: Implement correct block-access behavior Date: Thu, 17 Apr 2025 18:52:47 +0800 Message-Id: <20250417105249.18232-17-jim.shu@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20250417105249.18232-1-jim.shu@sifive.com> References: <20250417105249.18232-1-jim.shu@sifive.com> 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::42d; envelope-from=jim.shu@sifive.com; helo=mail-pf1-x42d.google.com 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: 1744887381492019100 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 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 Signed-off-by: Jim Shu --- hw/misc/riscv_wgchecker.c | 169 +++++++++++++++++++++++++++++++++++++- 1 file changed, 167 insertions(+), 2 deletions(-) diff --git a/hw/misc/riscv_wgchecker.c b/hw/misc/riscv_wgchecker.c index 8839d898c9..5d2af7946f 100644 --- a/hw/misc/riscv_wgchecker.c +++ b/hw/misc/riscv_wgchecker.c @@ -100,6 +100,169 @@ 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 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); + qemu_irq_raise(s->irq); + } + + 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 @@ -109,23 +272,25 @@ 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 trace_riscv_wgc_mem_blocked_read(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 trace_riscv_wgc_mem_blocked_write(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 { --=20 2.17.1 From nobody Thu Dec 18 13:40:37 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=1744887362; cv=none; d=zohomail.com; s=zohoarc; b=Lsk2a23zo7kLqEFMnAzAL3iSrvI7d4Mk9rCVAR3M4fC6K4/2uGKBQk6XfyjiZMBJPwX6mISQfHVzdqXBCLldgem9uT0NaE/LKqKE0mxcujon8NzB4qOVmF+ndNBmk4aQfExpTuMfgjFQegp5Z/FuXafAD0Pi4EFoxxoVFO2UJRw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1744887362; h=Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=zQPqjXYVy2MZpkzlsyhoG+MQg9fKVsZKI/VvPlBGP+g=; b=Z3TKHUxBgeQ8kwqeSxB+mlW+yzz8BphLU1IDgUP/0lUPs8rVkzdPctw79gGH+HkTBYivDUWen4FVa6QWJXGSJ9/NrLEJQhUL771xNSv+Fi9UaFlAvZVMSAclBJe2LYXtIi+znQscP4BHkz782DPVRnsHNB3J2B9uhR8+q2Re9NI= 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 1744887362223756.0640267749448; Thu, 17 Apr 2025 03:56:02 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1u5MtJ-0000aj-Pu; Thu, 17 Apr 2025 06:54:45 -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 1u5MtH-0000XD-EH for qemu-devel@nongnu.org; Thu, 17 Apr 2025 06:54:43 -0400 Received: from mail-pl1-x634.google.com ([2607:f8b0:4864:20::634]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1u5MtB-0003nq-SC for qemu-devel@nongnu.org; Thu, 17 Apr 2025 06:54:43 -0400 Received: by mail-pl1-x634.google.com with SMTP id d9443c01a7336-2279915e06eso6755485ad.1 for ; Thu, 17 Apr 2025 03:54:37 -0700 (PDT) Received: from hsinchu26.internal.sifive.com ([210.176.154.34]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-73bd230ddaasm12366251b3a.138.2025.04.17.03.54.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Apr 2025 03:54:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1744887276; x=1745492076; darn=nongnu.org; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=zQPqjXYVy2MZpkzlsyhoG+MQg9fKVsZKI/VvPlBGP+g=; b=KRK9X5QTdQjpJLo6yOINvlj9KNuSEZCmQYkhb84enEeg73Il+kiDaRGsfFQARZ7z3V uSMVCBCJXXYAyreSozkODU23DUq47ydms9WvkI7O/VYTryY4zukxrAgTYPxOrSKAN6Lj n1fxkQn05d+BMC0QGpfRMpygV7vLlE1psUifyWyUZUk2VEdU/rGlT7WRLHkjI/AmUclB X8bl8hHNSPdYEg43CdEueMNFWor2ibYzws5kaV4Frd1uFLlaBs9cFEAFTeKs84Uy5HgV WtYxPzTgO/EpZLdVVkxzgxdJ56JTlVUgJrDgt/gb4LUdiU1TDTAAYWJK1HXWnx/I6r4P z3Jw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744887276; x=1745492076; h=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=zQPqjXYVy2MZpkzlsyhoG+MQg9fKVsZKI/VvPlBGP+g=; b=qQmc/BYU3q65njs1cEqZUqtB9OYQ7WhD6du/538Od0/xsizUDMjD5taE2O9xvNlxUT BgZ8o/UurHixQvYupZ5rSGzlOIqIZsBBeKXt45e7jiiCeXqFQ5Nmmayj+vZhV7Yl/3NU rwWnXMRrYjPa5Chv6dUQ4ZxSRKlTk6pqUlrjqMxkFrq8XeyY7NwpJELI2A1vWfF0oCFQ 1J8mjX30TYKJ2QYltUeLIL/caVPbbY3CSqtqIr1PJrIaTVViaYjkgnFsniCxYyZnfV+d 5j4Ybe+zkt75XQXHayn4AFhlhMkkjetYJOu+Cj3OkcLOWT0MDFE0EvxZE96DCFV0emAf OOzw== X-Gm-Message-State: AOJu0YyQsi5Do6P9hYpDYN6zqFR/iE/SBAn8QRK0WYZaXnGEWgCdEmlN W0dm02jGxz3Q+1vJ2OPtpc1aPA9bmWzQrvb6IGxOBb45IasF6ubipPInSrAzox8oA90IK/z3mux klxwMwZZPPzVNw8paDNl6aV/yrGN4gIMbq4OGgbVm98VwYMtD25jl3YLRzksPgObJlTZ9my+Fal +hChFDaB3Eroe0wdr8uqahTq5xftwJxyBiTg== X-Gm-Gg: ASbGncvDAWSS8/fali/KVjUnyE9nFqd7TwLO6r3/0xfWPETdLqhgcsWcFVkDIIO94iu B3Lr/XTqha669Kfxn52qXGGWCfuKUAyhqOGo1j078HR9gPumEZ1jtAEhy7akvs8RJJIiK34O7R/ r7IIwCwtP7fwcSpmGxdut2l9YSAfrQ5z7sRzg4smr0iiOFgubMCFQp9RjBsv9qplzjVXvzfUfQn XRpzXlUHKj8nB6jamGEnJIc0CGZuzBquXziiVQdF1eN8eZGJbhNuAK2OwihMi2xZbSxHhoTfqMz 4U++/AN51sGufyvTL8ebecGOozPYN4HvSHaW/bkfF9FJ6IIUGrLNeAd2aCXidr4= X-Google-Smtp-Source: AGHT+IE4m/QZrr2Q6Luap6Pg7J62BHPXXU9yOa9/DWDaGYQ/X6pxr3b7Xqvv907SRhkPiLLuthmhAA== X-Received: by 2002:a17:902:ea12:b0:223:5241:f5ca with SMTP id d9443c01a7336-22c358db91bmr60556795ad.20.1744887275787; Thu, 17 Apr 2025 03:54:35 -0700 (PDT) From: Jim Shu To: qemu-devel@nongnu.org, qemu-riscv@nongnu.org Cc: Richard Henderson , Paolo Bonzini , Palmer Dabbelt , Alistair Francis , Weiwei Li , Daniel Henrique Barboza , Liu Zhiwei , Eduardo Habkost , Marcel Apfelbaum , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , 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 , 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 v2 17/18] hw/misc: riscv_wgchecker: Check the slot settings in translate Date: Thu, 17 Apr 2025 18:52:48 +0800 Message-Id: <20250417105249.18232-18-jim.shu@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20250417105249.18232-1-jim.shu@sifive.com> References: <20250417105249.18232-1-jim.shu@sifive.com> 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::634; envelope-from=jim.shu@sifive.com; helo=mail-pl1-x634.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: 1744887364114019000 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 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 5d2af7946f..5a70231837 100644 --- a/hw/misc/riscv_wgchecker.c +++ b/hw/misc/riscv_wgchecker.c @@ -100,6 +100,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_wgc_iommu_notify_all(); + + for (int i=3D0; imem_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=3D0; widupstream, wid, event); + } + } +} + static void decode_napot(hwaddr a, hwaddr *sa, hwaddr *ea) { /* @@ -309,6 +355,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 @@ -327,18 +376,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_wgc_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 @@ -604,6 +660,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 @@ -699,6 +758,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 b1d8538220..54dfcd50a1 100644 --- a/hw/misc/trace-events +++ b/hw/misc/trace-events @@ -403,3 +403,4 @@ riscv_wgchecker_mmio_write(uint64_t base, uint64_t offs= et, unsigned int size, ui riscv_wgc_mem_blocked_read(uint64_t addr, unsigned size, uint32_t wid) "wg= Checker blocked read: offset 0x%" PRIx64 " size %u wid %" PRIu32 riscv_wgc_mem_blocked_write(uint64_t addr, uint64_t data, unsigned size, u= int32_t wid) "wgChecker blocked write: offset 0x%" PRIx64 " data 0x%" PRIx6= 4 " size %u wid %" PRIu32 riscv_wgc_translate(uint64_t addr, int flags, int wid, const char *res) "w= gChecker translate: addr 0x%016" PRIx64 " flags 0x%x wid %d: %s" +riscv_wgc_iommu_notify_all(void) "wgChecker iommu: notifying UNMAP for all" --=20 2.17.1 From nobody Thu Dec 18 13:40:37 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=1744887372; cv=none; d=zohomail.com; s=zohoarc; b=JFEZQ2MtiU+CqWohF86/3PoCgIXySGQCSkFcvtcXykecHKUJXewjXfIbjUrA2GEXwYDzENSEvS6FT3aM6dECKfOYvwwQMChsSjIxiKEYXpfDV6qktQMpjMUdPNvybCWLV3f2t3CKAFhxMZ9NG35mOu/MK/iujllAU8+p9E4I354= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1744887372; h=Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=blq7hMk0Njvz2l144/h84muIi7pqw4eOWM3fFtNPamQ=; b=YNUr9wmgOd76/QOdqWmUsm5wtM7+ezq+t8v6RTpJ6homBvBNTSoUiWHC4vX+w/Q9wwwcr+MomqmBMr+43Mthc+0aFyk/WgT2M8q6JpoTYPbMN/pPcx2USRz8cK7Vemu++3USl7uSSNYpMsBN7aGb6w52Tfw6xMt1fWwdTl48LJ0= 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 1744887372156543.6860630022172; Thu, 17 Apr 2025 03:56:12 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1u5MtN-0000hp-R4; Thu, 17 Apr 2025 06:54: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 1u5MtL-0000fm-Fh for qemu-devel@nongnu.org; Thu, 17 Apr 2025 06:54:47 -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 1u5MtH-0003rS-MP for qemu-devel@nongnu.org; Thu, 17 Apr 2025 06:54:47 -0400 Received: by mail-pf1-x436.google.com with SMTP id d2e1a72fcca58-736bfa487c3so468530b3a.1 for ; Thu, 17 Apr 2025 03:54:43 -0700 (PDT) Received: from hsinchu26.internal.sifive.com ([210.176.154.34]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-73bd230ddaasm12366251b3a.138.2025.04.17.03.54.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Apr 2025 03:54:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1744887281; x=1745492081; darn=nongnu.org; h=references:in-reply-to:message-id:date:subject:cc:to:from:from:to :cc:subject:date:message-id:reply-to; bh=blq7hMk0Njvz2l144/h84muIi7pqw4eOWM3fFtNPamQ=; b=eltds1CYVnX+9rsymdxuecPW0pvFrpGxa1OURAfp58bTluYNvm+YRhH8kwdrtiiBh9 0DpogkASeSyh4LCKbyRaOsM98X1WlG5vbX/0ZfvzHN6FWgBps+VOMHjmarUVgCKJ5zHG 6FKp6y1LOi+TRhNaAh1yutOY6anM3osjxMLuuxrIcCPvPYF2Nf0PLwm3HKCPIYveNAHK Ht0s8iKu1614mP1p5FYHK1U/7D7+f5pSK/wPqnKR2QlQBPVlK9KBbdxyjncdNUDnfMq/ Sxo8p97ZCQ8+zP+DmckzVkNFp1UsqGrJJQIc8rgtpToMOy2X1tCfZLEzyYBhhEDWTbST FOCg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744887281; x=1745492081; h=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=blq7hMk0Njvz2l144/h84muIi7pqw4eOWM3fFtNPamQ=; b=ardeabn4u+LDlhQTV+nKHPMrS7Lo/U0So6bmh9SpfhpvncjEdGdP/HGaxW6TwGscFp DVHk2kbCLyvqFpYfb6eJ0gfSCcKoyAbchQGEAYGm5Rh2F2D0h3BFD5Vxnj6r6HMRPMUc ApAqCDVOiaBZQEQBt/HqvFkWkIMnQd1qIpvkxqAvUebcc67xTj9DoyXZIIaCRvv31PH1 t2n18mAW3Lt7vpdQslpjEb6Dj7anyj+wj9DnGJ8Hf5bzGHAfjExUZ3EpBxOKOXBPIQtg sRJVMjo5HSFA50zCqyYecuyJcw44gWrjt2371riHuzPDRd6TwW0+jqxqTQJufEly/Cnm eScA== X-Gm-Message-State: AOJu0YwzUnpNeFgE9DyFOcQAY1ULYMTHOA7gktq7XjOIBcMWxg6NKoyE 0cOM692/HiqDYgqsP5fFSf7mA6HGqqftXKn6Y5M7WwSjwZs7yDi6/tP+kJZfdlGA+jMjceikh3K /XNxTVg5EQNCKN151VKJGrSipI1RVkEK4JrRCyt2kqRPqHJl89wztw42+x22R/Mw9fieKxzj+N5 CFk1SwTZ3rOaxJcjK0KWQA9PL20HSPoHfiNA== X-Gm-Gg: ASbGnctqvZEiVIgWKQv4gfJJV9BpGzSgdJWHeixu5tL8ISUdYXIjgjmOS9qgyCgwAVI gmM/y4A2TQUcC6xtYw9bR5FqTDlNiNr3E+S2tDAlOK+itteQ1xuFKn1h7FAxp8MKMMJtXtR+0yS sBeWBglj7NwQXqcESa8a4TCEdbsdMpuh0S8wl8Q7C+tKvBUEHHhbaSPVqW/AHg+9TPPpehUcfP5 jOspcNCWWEVGoNW9PiBwMerUTLBabs/HcPD684R20ro3Y7ilmMZWjMl9wQpS9Nta5M34ywsWWQk FAQWDDYKE9bMfYrHhQ7ESujTVuRIeI/sTD+iyNO+OUa98EODE3NORqOYc64OJpEt3yW9Mit1Yg= = X-Google-Smtp-Source: AGHT+IHdTF5ohfBbRKWRzDzcXt1tTlqP04POyexqyc8brw0bW86CIUg/06AuS+AcpAWq8f/WkYHvEw== X-Received: by 2002:a05:6a00:1d81:b0:736:a682:deb8 with SMTP id d2e1a72fcca58-73c266f94aemr9882782b3a.8.1744887281192; Thu, 17 Apr 2025 03:54:41 -0700 (PDT) From: Jim Shu To: qemu-devel@nongnu.org, qemu-riscv@nongnu.org Cc: Richard Henderson , Paolo Bonzini , Palmer Dabbelt , Alistair Francis , Weiwei Li , Daniel Henrique Barboza , Liu Zhiwei , Eduardo Habkost , Marcel Apfelbaum , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , 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 , 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 v2 18/18] hw/riscv: virt: Add WorldGuard support Date: Thu, 17 Apr 2025 18:52:49 +0800 Message-Id: <20250417105249.18232-19-jim.shu@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20250417105249.18232-1-jim.shu@sifive.com> References: <20250417105249.18232-1-jim.shu@sifive.com> 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: 1744887374198019000 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 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 | 163 ++++++++++++++++++++++++++++++++++++- include/hw/riscv/virt.h | 15 +++- 4 files changed, 195 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 e6a0ac1fa1..5c3e7b3479 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 e517002fdf..da873bc8b8 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 virt_memmap[VIRT_FLASH].size / 2; hwaddr flashbase =3D virt_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, @@ -1426,6 +1472,71 @@ 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=3D0; inum_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=3D0; inum_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], upst= ream_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=3D0; ihave_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++) { @@ -1673,6 +1791,10 @@ static void virt_machine_init(MachineState *machine) memory_region_add_subregion(system_memory, memmap[VIRT_DRAM].base, machine->ram); =20 + if (tcg_enabled() && s->have_wg) { + wgc_append_child(&wgcinfo[WGC_DRAM], machine->ram, memmap[VIRT_DRA= M].base); + } + /* boot rom */ memory_region_init_rom(mask_rom, NULL, "riscv_virt_board.mrom", memmap[VIRT_MROM].size, &error_fatal); @@ -1700,10 +1822,16 @@ static void virt_machine_init(MachineState *machine) =20 create_platform_bus(s, mmio_irqchip); =20 - serial_mm_init(system_memory, memmap[VIRT_UART0].base, + uart =3D serial_mm_init(system_memory, memmap[VIRT_UART0].base, 0, qdev_get_gpio_in(mmio_irqchip, UART0_IRQ), 399193, serial_hd(0), DEVICE_LITTLE_ENDIAN); =20 + if (tcg_enabled() && s->have_wg) { + wgc_append_child(&wgcinfo[WGC_UART], + sysbus_mmio_get_region(SYS_BUS_DEVICE(uart), 0), + memmap[VIRT_UART0].base); + } + sysbus_create_simple("goldfish_rtc", memmap[VIRT_RTC].base, qdev_get_gpio_in(mmio_irqchip, RTC_IRQ)); =20 @@ -1712,7 +1840,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 (tcg_enabled() && s->have_wg) { + virt_flash_map(s, system_memory, &wgcinfo[WGC_FLASH]); + } else { + virt_flash_map(s, system_memory, NULL); + } + + if (tcg_enabled() && s->have_wg) { + virt_create_worldguard(wgcinfo, WGC_NUM, mmio_irqchip); + } =20 /* load/create device tree */ if (machine->dtb) { @@ -1757,6 +1894,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 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); @@ -1977,6 +2128,12 @@ static void virt_machine_class_init(ObjectClass *oc,= 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 48a14bea2e..3a631a6a23 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; @@ -87,11 +88,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 */ @@ -102,7 +109,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) @@ -158,4 +165,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.17.1