From patchwork Wed Aug 7 15:16:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Jones X-Patchwork-Id: 13756424 Received: from out-181.mta1.migadu.com (out-181.mta1.migadu.com [95.215.58.181]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BBCA078291 for ; Wed, 7 Aug 2024 15:16:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723043807; cv=none; b=VheG+P1cduALYxiAHAXXdvTzG8YqEa8BC4Yck847JEop8uys1UJYDRnmiBetUc/7yeFl6v59XgX3V0pJPmyMMu8im70Gwolwp6N4QNw7mI1AjnAY32HdSs6joe5HGWZMGvw/zjzH82f58vvDY4VhrLzRiAK9RKiIZ0waw1rYquM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723043807; c=relaxed/simple; bh=yauA6eDczoyHsEMvB/ScoIlvpidBjdOd0bYLDrs8kVQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Q3mrwH/Ujbs9qRCr7LHEtXwMEKAl4WTvP4sf8cjLUNNom+OTS0y4PAmNeKJXkKtn34osQVugwMfsZ489vhP7TCriae/xadf5Tikc8f1kz+cC2Tx7SciB3ysj2Rr/DFSd73eI9a434gMhMl+CGhPh9HSW6zcfIqGcL00h5sE1AZQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=Fg5NgsoJ; arc=none smtp.client-ip=95.215.58.181 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="Fg5NgsoJ" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1723043803; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=0JnWydVRlcUj9MsWlBjRPKAbHtiO4c0GMGL7PGlxT7Y=; b=Fg5NgsoJGBQLwal8r4tS97dNmfiatXZJdjpoo1nO5VjIz7WBTp5KVLkLOD4Jx2Wi7BKaV7 uwFAFvZsI6c869zXzNpZWMkqSLvy7p8ht12cyW0sag+FPBq46gyrdXZ6Dh97MonrJsFEfh 86JpRJ9yCUQ+OdKobsJtZxuwk7SB3zo= From: Andrew Jones To: kvm@vger.kernel.org, kvm-riscv@lists.infradead.org Cc: atishp@rivosinc.com, cade.richard@berkeley.edu, jamestiotio@gmail.com Subject: [kvm-unit-tests PATCH 1/3] riscv: Fix virt_to_phys again Date: Wed, 7 Aug 2024 17:16:31 +0200 Message-ID: <20240807151629.144168-6-andrew.jones@linux.dev> In-Reply-To: <20240807151629.144168-5-andrew.jones@linux.dev> References: <20240807151629.144168-5-andrew.jones@linux.dev> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT The last fix was a bit hasty since we didn't double check that virt_to_phys() was the right place for the fix, rather than virt_to_pte_phys(), and of course it was the latter... All architectures add on the offset in virt_to_pte_phys() and then simply wrap virt_to_pte_phys() with virt_to_phys(), if they implement virt_to_phys() at all. RISCV shouldn't be different. Fixes: e1dd4ea76894 ("riscv: Fix virt_to_phys") Fixes: 23100d972705 ("riscv: Enable vmalloc") Signed-off-by: Andrew Jones --- lib/riscv/mmu.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/riscv/mmu.c b/lib/riscv/mmu.c index 165a7034bc69..2c9c4f376ac9 100644 --- a/lib/riscv/mmu.c +++ b/lib/riscv/mmu.c @@ -179,7 +179,7 @@ phys_addr_t virt_to_pte_phys(pgd_t *pgtable, void *virt) if (!pte_val(*ptep)) return 0; - return __pa(pteval_to_ptep(pte_val(*ptep))); + return __pa(pteval_to_ptep(pte_val(*ptep))) | offset_in_page(virt); } unsigned long virt_to_phys(volatile void *address) @@ -194,7 +194,7 @@ unsigned long virt_to_phys(volatile void *address) paddr = virt_to_pte_phys(pgtable, (void *)address); assert(sizeof(long) == 8 || !(paddr >> 32)); - return (unsigned long)paddr | offset_in_page(address); + return (unsigned long)paddr; } void *phys_to_virt(unsigned long address) From patchwork Wed Aug 7 15:16:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Jones X-Patchwork-Id: 13756425 Received: from out-170.mta1.migadu.com (out-170.mta1.migadu.com [95.215.58.170]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 516BD84D12 for ; Wed, 7 Aug 2024 15:16:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.170 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723043811; cv=none; b=MPRE7Bopnice2ZSVIo1hTjJZ/n9LagGXl8cK4cj/9kJ3Z5PWf1cFPW+KtHpAGsPzm+MRFHF5Ue1jNnLSMUFAYEycGbaEg3obhQwXo8IhNJ9tqxC141L2x3EHD/ehy8tcR+hnQ96kkWLxQpwnGNhDDwX7bQElUk7PqYwqnEmMUN4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723043811; c=relaxed/simple; bh=xMMD6lbF1Q8SU34AXHQ+A7gAtY/+5St/83LVg8jlPAs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ozLMdho4Ce5y3wnT7u9TPmGFIzKgHNpysKhD+jDXUklkYmlwxhkMP7HFqL7NRKK03L91BDhLloHXT3WmbntgabPQmlOp7TK4CvlSilgWpIWSWlUvzWUq8gr1MAIqTYSbscUe6QvHz2LYlPaY3GuRhMHL+FbBhzyEiFlzJlfQwXg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=G9lpFs7W; arc=none smtp.client-ip=95.215.58.170 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="G9lpFs7W" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1723043807; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=3M9iRKgv8aV/QZsAf7pAIKygkuBgiAEBkmW/A2oWcjU=; b=G9lpFs7WQxJkKeHOXcwA5oebDhhNL1I0JIyvp9EL9sJ7Twn+lfK0kXc5xuaBikT+BmkCmh PQZOrlLV4ClFc/Fnu9rpqD+8rLvzIC1RcYuemQ3BpuG6lOYWS0ZcN3g0oLJyT3X3VzzFaQ rOfGNLf8Z7sy6gMMmlDS/osutI8Hi6g= From: Andrew Jones To: kvm@vger.kernel.org, kvm-riscv@lists.infradead.org Cc: atishp@rivosinc.com, cade.richard@berkeley.edu, jamestiotio@gmail.com Subject: [kvm-unit-tests PATCH 2/3] riscv: setup: Apply VA_BASE check to rv64 Date: Wed, 7 Aug 2024 17:16:32 +0200 Message-ID: <20240807151629.144168-7-andrew.jones@linux.dev> In-Reply-To: <20240807151629.144168-5-andrew.jones@linux.dev> References: <20240807151629.144168-5-andrew.jones@linux.dev> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT The VA_BASE check in setup() also applies to rv64, as is clear from the later VA_BASE check in mem_allocator_init(), which ensures freemem_start < freemem_end < VA_BASE. Fixes: 6895ce6dc618 ("riscv: Populate memregions and switch to page allocator") Signed-off-by: Andrew Jones --- lib/riscv/setup.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/riscv/setup.c b/lib/riscv/setup.c index e0b5f6f7daf5..2c7792a5b0bd 100644 --- a/lib/riscv/setup.c +++ b/lib/riscv/setup.c @@ -193,7 +193,7 @@ void setup(const void *fdt, phys_addr_t freemem_start) const char *bootargs; int ret; - assert(sizeof(long) == 8 || freemem_start < VA_BASE); + assert(freemem_start < VA_BASE); freemem = __va(freemem_start); freemem_push_fdt(&freemem, fdt); From patchwork Wed Aug 7 15:16:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Jones X-Patchwork-Id: 13756426 Received: from out-170.mta0.migadu.com (out-170.mta0.migadu.com [91.218.175.170]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6DB218172A for ; Wed, 7 Aug 2024 15:16:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.170 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723043816; cv=none; b=JURRXgWYF/YDaMNkM6sv1kNBGQh5rgx9+Y7TS0oMQrRlonwZgV51w4aQu+23/V4xGRA/eRX8muFpnq2wnKisvpaB8/F2c9AE1FPQxt3KQliijUe7DLLHrwrxLo5k4BZ1xcX8pN9a1HSx5B6Q1aKNyYsOfcD5Hn4XPtWQQ6H2jYY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723043816; c=relaxed/simple; bh=TTqgKyZF6Fv0E3hsBiQtvgHzmv1AYaaVJLklslQXPmc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=pdOYYNBHHvk3CsZSwwU6nmxyIc7VOHL2JMpksYWD7uVTvggZdwKAcN9dERwUF+4UtRC0QrYgfeUmefw6m18i3SIX5EL8FwePhFKLVb9EC2jRqAMSUSLavRe3sTYT0RqBJxvQBREOb8jJP7p8mEDugJJzuM+tmcH7JkYavnRvyy0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=Kdjtgch6; arc=none smtp.client-ip=91.218.175.170 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="Kdjtgch6" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1723043811; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=emQhgwKma5wMWcnZ3rromGvCA08bQy1hB1bcQU8+stY=; b=Kdjtgch6MkNPlg6yb4HfVP3RaE9UWGXaJx+LRpqTpZopg/7ifvmBN8GoW6SOyx0Ln4Z6XA NN4wjVLkrUJyeKuI7LiSZO35Iap+Hb07YZYUqAAv7Z5jkmRoko97y1RPLUYVNwcHeC9ZG2 kiv0IRRU9MaS7ffoCEMSgEgpdeqqa9g= From: Andrew Jones To: kvm@vger.kernel.org, kvm-riscv@lists.infradead.org Cc: atishp@rivosinc.com, cade.richard@berkeley.edu, jamestiotio@gmail.com Subject: [kvm-unit-tests PATCH 3/3] riscv: Support up to 34-bit physical addresses on rv32, sort of Date: Wed, 7 Aug 2024 17:16:33 +0200 Message-ID: <20240807151629.144168-8-andrew.jones@linux.dev> In-Reply-To: <20240807151629.144168-5-andrew.jones@linux.dev> References: <20240807151629.144168-5-andrew.jones@linux.dev> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT Change virt_to_phys() and phys_to_virt() to use phys_addr_t instead of unsigned long. This allows 32-bit builds to use physical addresses over 32 bits wide (the spec allows up to 34 bits). But, to keep things simple, we don't expect physical addresses wider than 32 bits in most the library code (and that's ensured by sprinkling around some asserts). IOW, the support is really only for unit tests which want to test with an additional high memory region. Signed-off-by: Andrew Jones --- lib/riscv/asm/io.h | 4 ++-- lib/riscv/mmu.c | 32 ++++++++++++++++++++------------ lib/riscv/smp.c | 7 ++++++- 3 files changed, 28 insertions(+), 15 deletions(-) diff --git a/lib/riscv/asm/io.h b/lib/riscv/asm/io.h index 37a130e533c9..a48a9aa654dd 100644 --- a/lib/riscv/asm/io.h +++ b/lib/riscv/asm/io.h @@ -77,10 +77,10 @@ static inline u64 __raw_readq(const volatile void __iomem *addr) void __iomem *ioremap(phys_addr_t phys_addr, size_t size); #define virt_to_phys virt_to_phys -unsigned long virt_to_phys(volatile void *address); +phys_addr_t virt_to_phys(volatile void *address); #define phys_to_virt phys_to_virt -void *phys_to_virt(unsigned long address); +void *phys_to_virt(phys_addr_t address); #include diff --git a/lib/riscv/mmu.c b/lib/riscv/mmu.c index 2c9c4f376ac9..664e0aded404 100644 --- a/lib/riscv/mmu.c +++ b/lib/riscv/mmu.c @@ -18,9 +18,16 @@ static int pte_index(uintptr_t vaddr, int level) return (vaddr >> (PGDIR_BITS * level + PAGE_SHIFT)) & PGDIR_MASK; } +static phys_addr_t pteval_to_phys_addr(pteval_t pteval) +{ + return ((pteval & PTE_PPN) >> PPN_SHIFT) << PAGE_SHIFT; +} + static pte_t *pteval_to_ptep(pteval_t pteval) { - return (pte_t *)(((pteval & PTE_PPN) >> PPN_SHIFT) << PAGE_SHIFT); + phys_addr_t paddr = pteval_to_phys_addr(pteval); + assert(paddr == __pa(paddr)); + return (pte_t *)__pa(paddr); } static pteval_t ptep_to_pteval(pte_t *ptep) @@ -106,7 +113,7 @@ void __mmu_enable(unsigned long satp) void mmu_enable(unsigned long mode, pgd_t *pgtable) { - unsigned long ppn = (unsigned long)pgtable >> PAGE_SHIFT; + unsigned long ppn = __pa(pgtable) >> PAGE_SHIFT; unsigned long satp = mode | ppn; assert(!(ppn & ~SATP_PPN)); @@ -118,6 +125,9 @@ void *setup_mmu(phys_addr_t top, void *opaque) struct mem_region *r; pgd_t *pgtable; + /* The initial page table uses an identity mapping. */ + assert(sizeof(long) == 8 || !(top >> 32)); + if (!__initial_pgtable) __initial_pgtable = alloc_page(); pgtable = __initial_pgtable; @@ -146,7 +156,8 @@ void __iomem *ioremap(phys_addr_t phys_addr, size_t size) pgd_t *pgtable = current_pgtable(); bool flush = true; - assert(sizeof(long) == 8 || !(phys_addr >> 32)); + /* I/O is always identity mapped. */ + assert(sizeof(long) == 8 || !(end >> 32)); if (!pgtable) { if (!__initial_pgtable) @@ -158,7 +169,7 @@ void __iomem *ioremap(phys_addr_t phys_addr, size_t size) mmu_set_range_ptes(pgtable, start, start, end, __pgprot(_PAGE_READ | _PAGE_WRITE), flush); - return (void __iomem *)(unsigned long)phys_addr; + return (void __iomem *)__pa(phys_addr); } phys_addr_t virt_to_pte_phys(pgd_t *pgtable, void *virt) @@ -179,27 +190,24 @@ phys_addr_t virt_to_pte_phys(pgd_t *pgtable, void *virt) if (!pte_val(*ptep)) return 0; - return __pa(pteval_to_ptep(pte_val(*ptep))) | offset_in_page(virt); + return pteval_to_phys_addr(pte_val(*ptep)) | offset_in_page(virt); } -unsigned long virt_to_phys(volatile void *address) +phys_addr_t virt_to_phys(volatile void *address) { unsigned long satp = csr_read(CSR_SATP); pgd_t *pgtable = (pgd_t *)((satp & SATP_PPN) << PAGE_SHIFT); - phys_addr_t paddr; if ((satp >> SATP_MODE_SHIFT) == 0) return __pa(address); - paddr = virt_to_pte_phys(pgtable, (void *)address); - assert(sizeof(long) == 8 || !(paddr >> 32)); - - return (unsigned long)paddr; + return virt_to_pte_phys(pgtable, (void *)address); } -void *phys_to_virt(unsigned long address) +void *phys_to_virt(phys_addr_t address) { /* @address must have an identity mapping for this to work. */ + assert(address == __pa(address)); assert(virt_to_phys(__va(address)) == address); return __va(address); } diff --git a/lib/riscv/smp.c b/lib/riscv/smp.c index 7e4bb5b76903..4d373e0a29a8 100644 --- a/lib/riscv/smp.c +++ b/lib/riscv/smp.c @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -36,6 +37,7 @@ secondary_func_t secondary_cinit(struct secondary_data *data) static void __smp_boot_secondary(int cpu, secondary_func_t func) { struct secondary_data *sp = alloc_pages(1) + SZ_8K - 16; + phys_addr_t sp_phys; struct sbiret ret; sp -= sizeof(struct secondary_data); @@ -43,7 +45,10 @@ static void __smp_boot_secondary(int cpu, secondary_func_t func) sp->stvec = csr_read(CSR_STVEC); sp->func = func; - ret = sbi_hart_start(cpus[cpu].hartid, (unsigned long)&secondary_entry, __pa(sp)); + sp_phys = virt_to_phys(sp); + assert(sp_phys == __pa(sp_phys)); + + ret = sbi_hart_start(cpus[cpu].hartid, (unsigned long)&secondary_entry, __pa(sp_phys)); assert(ret.error == SBI_SUCCESS); }