Received: by 2002:ab2:6309:0:b0:1fb:d597:ff75 with SMTP id s9csp1280649lqt; Fri, 7 Jun 2024 13:22:39 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXGjT+BZO9jgMCCEVX3SgfEz1xo+TVI6qNua7ObuHw2FRLtoVQz/DHSsSwy4pmoTasaoBz+xxXf0yFUF0wJjiu/jGkWBjWsnGEC+v/m6A== X-Google-Smtp-Source: AGHT+IEsiiaV/19SA0A4IKPbenTsQkSHuBzIKBq7AQcOp62T21cn2a3Zd0gXGbhx9B9RcZNvyrmP X-Received: by 2002:a17:906:1148:b0:a68:e335:3e62 with SMTP id a640c23a62f3a-a6cdc0e9ceemr251991566b.72.1717791759098; Fri, 07 Jun 2024 13:22:39 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1717791759; cv=pass; d=google.com; s=arc-20160816; b=V7h1eKJqFodP+7pcsJNhtzcKQ3+cU4sq8WypHy+g+EnSEhQj/htaVc5vyKnF8zJcYH yxMuhKoNor4Or14kmJ/KYW89zJbh/ElUNjPmmjejihj0wPatL6dac0JvkIUTGWVfHtcw QNkheq0eCbhuvp+v9rKTp87tNT3gL6xSaPyAliUq/eWoxqEGa59qjOOs1RPog4N2SqsX qujGQ7AHeVJyftoJWFgzXKZMC0GEfm+dpOYhKi3B7O8AaHbFAeHiDUbVUTA7zaiugPgW MG/EkxTjO6oJLuTD/w3Y/3LYn6zGoM8nVrwY0+wO4K4IgQPeGMjJdqeee2lXxx+jmKre QCJw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:to:dkim-signature:dkim-signature:from; bh=4odO1GRTROf7xvMWOl3DqpmsPPjSYUQ/pXHn+xOw3Kc=; fh=NjSy9ELbZGh6EjCqOzE+qeF+VS4OABBeZf5VHCmpp/4=; b=sr8XTv1kPqsvJo4b91L/T7W0XiNIBu+IXICJL5EBa9X5sqD1VppqyOBmcE3k0ALm0Y rtGwP208Smtvv/Di5kvPyNZo/TzpkA931utkP9I1sGTTSfsxzFoF4Xv5zHqV3jf6eJJC vvWnsixxYxDtrJEXT3A6RWtGw7tDKz5Q7enEErfOqDRuKmQWAVQ9G2QRH7vsQ4TBQqv4 nTRkfKSobdXWCo/X9NeI6+jlY3nPhyoAqCwEMOPYTRHbC4Fg2OsOQL0zzjrdcUqoxXAA afAlwFSLFNRxHnTOROBAnwLV6/6UYA9yLQPTLQuZlB1Myw//ncbZSyLhKnwjuQQUpQwh 2imA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=lAdxAtL1; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; arc=pass (i=1 spf=pass spfdomain=linutronix.de dkim=pass dkdomain=linutronix.de dmarc=pass fromdomain=linutronix.de); spf=pass (google.com: domain of linux-kernel+bounces-206694-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-206694-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id a640c23a62f3a-a6ed33c3419si64589566b.1048.2024.06.07.13.22.39 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 07 Jun 2024 13:22:39 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-206694-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=lAdxAtL1; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; arc=pass (i=1 spf=pass spfdomain=linutronix.de dkim=pass dkdomain=linutronix.de dmarc=pass fromdomain=linutronix.de); spf=pass (google.com: domain of linux-kernel+bounces-206694-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-206694-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id A14551F2310B for ; Fri, 7 Jun 2024 20:22:38 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D9BA815279C; Fri, 7 Jun 2024 20:22:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="lAdxAtL1"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="L+bpi+80" Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (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 1CE81145B1A for ; Fri, 7 Jun 2024 20:22:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717791737; cv=none; b=vBGfitE8OZ9JuOUxpEzESOIGSFl1QGeCsnOQKcWIyPQXZPqePKIee3NQQzltTXtHL2BSYqoz9ufXbdpMSsjKBabOXENRLwKxVANFG8dBkRz+iQGRxG40wcVFKNlALIs2YtrymklbViFKsYa9RApfokmoOy1LZTTrrtbHOqqFnGg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717791737; c=relaxed/simple; bh=AXLhTJOfOZbd5sWTxrT+P/JVlzoYysMbDWuTt1L3jiE=; h=From:To:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=B+Lx4V4CbTJBfGv7MzlKoNckzbp+XqmrNDKl+9MxHsXbmI9GzeqZlIfaj4CE2STkOiGM3ygTfhruYj6OaWutj60ofKqfExhOX8GZLCxjooTynTfFpmAnxsf+z7QwoWCI9YtEFTriMtZtNmdhhFi7yRkS/0C3XM1sqP0frzk4v9k= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=lAdxAtL1; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=L+bpi+80; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de From: Nam Cao DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1717791734; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=4odO1GRTROf7xvMWOl3DqpmsPPjSYUQ/pXHn+xOw3Kc=; b=lAdxAtL1WO+adL+dYcUMR8vwNa0xTJsBJmhva63AYlFQDh9iB2KmPI0nzI7rctgnpzjFGp 1+qM69qhVTlhxLRahmln8BYKQ+cwLZp8ErCCWDQkQj8FO2i88zELxECIhL4SdC/Hj0l7gt R8qEf3b85XNA9e/c3Pczr06hu/y8zjSn252U827wfpMt8+eaXa1G4ommneqv3SV+x0qbGO VNRfEBde7XqQgX8+HepNazsYzG03lw4WOIQEwZvCsxJJzBukiRkumfLWVuI7FG/ky5hfx2 RSiJVqD4cHzop2bkP4hf+GuicVGkvaa3KFZFVfuz9GASY+igGnOBEFzlJlyh3Q== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1717791734; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=4odO1GRTROf7xvMWOl3DqpmsPPjSYUQ/pXHn+xOw3Kc=; b=L+bpi+80qMhsxa1grxAhSboftzk0iStipUSm9Wd4glNMTF1wlfW63xioVkOhA8Hn8lFmca mHo+MnwYL/aHfjAw== To: Alexandre Ghiti , Paul Walmsley , Palmer Dabbelt , Albert Ou , linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 3/8] riscv: replace misleading va_kernel_pa_offset on XIP kernel Date: Fri, 7 Jun 2024 22:22:08 +0200 Message-Id: <84e5d005c1386d88d7b2531e0b6707ec5352ee54.1717789719.git.namcao@linutronix.de> In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit On XIP kernel, the name "va_kernel_pa_offset" is misleading: unlike "normal" kernel, it is not the virtual-physical address offset of kernel mapping, it is the offset of kernel mapping's first virtual address to first physical address in DRAM, which is not meaningful because the kernel's first physical address is not in DRAM. For XIP kernel, there are 2 different offsets because the read-only part of the kernel resides in ROM while the rest is in RAM. The offset to ROM is in kernel_map.va_kernel_xip_pa_offset, while the offset to RAM is not stored anywhere: it is calculated on-the-fly. Remove this confusing "va_kernel_pa_offset" and add "va_kernel_xip_data_pa_offset" as its replacement. This new variable is the offset of virtual mapping of the kernel's data portion to the corresponding physical addresses. With the introduction of this new variable, also rename va_kernel_xip_pa_offset -> va_kernel_xip_text_pa_offset to make it clear that this one is about the .text section. Signed-off-by: Nam Cao Reviewed-by: Alexandre Ghiti --- arch/riscv/include/asm/page.h | 29 +++++++++++++++++++++-------- arch/riscv/mm/init.c | 6 ++++-- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/arch/riscv/include/asm/page.h b/arch/riscv/include/asm/page.h index 115ac98b8d72..b1fcf0d733c4 100644 --- a/arch/riscv/include/asm/page.h +++ b/arch/riscv/include/asm/page.h @@ -112,11 +112,13 @@ struct kernel_mapping { /* Offset between linear mapping virtual address and kernel load address */ unsigned long va_pa_offset; /* Offset between kernel mapping virtual address and kernel load address */ - unsigned long va_kernel_pa_offset; - unsigned long va_kernel_xip_pa_offset; #ifdef CONFIG_XIP_KERNEL + unsigned long va_kernel_xip_text_pa_offset; + unsigned long va_kernel_xip_data_pa_offset; uintptr_t xiprom; uintptr_t xiprom_sz; +#else + unsigned long va_kernel_pa_offset; #endif }; @@ -134,12 +136,18 @@ extern phys_addr_t phys_ram_base; #else void *linear_mapping_pa_to_va(unsigned long x); #endif + +#ifdef CONFIG_XIP_KERNEL #define kernel_mapping_pa_to_va(y) ({ \ unsigned long _y = (unsigned long)(y); \ - (IS_ENABLED(CONFIG_XIP_KERNEL) && _y < phys_ram_base) ? \ - (void *)(_y + kernel_map.va_kernel_xip_pa_offset) : \ - (void *)(_y + kernel_map.va_kernel_pa_offset + XIP_OFFSET); \ + (_y < phys_ram_base) ? \ + (void *)(_y + kernel_map.va_kernel_xip_text_pa_offset) : \ + (void *)(_y + kernel_map.va_kernel_xip_data_pa_offset); \ }) +#else +#define kernel_mapping_pa_to_va(y) ((void *)((unsigned long)(y) + kernel_map.va_kernel_pa_offset)) +#endif + #define __pa_to_va_nodebug(x) linear_mapping_pa_to_va(x) #ifndef CONFIG_DEBUG_VIRTUAL @@ -147,12 +155,17 @@ void *linear_mapping_pa_to_va(unsigned long x); #else phys_addr_t linear_mapping_va_to_pa(unsigned long x); #endif + +#ifdef CONFIG_XIP_KERNEL #define kernel_mapping_va_to_pa(y) ({ \ unsigned long _y = (unsigned long)(y); \ - (IS_ENABLED(CONFIG_XIP_KERNEL) && _y < kernel_map.virt_addr + XIP_OFFSET) ? \ - (_y - kernel_map.va_kernel_xip_pa_offset) : \ - (_y - kernel_map.va_kernel_pa_offset - XIP_OFFSET); \ + (_y < kernel_map.virt_addr + XIP_OFFSET) ? \ + (_y - kernel_map.va_kernel_xip_text_pa_offset) : \ + (_y - kernel_map.va_kernel_xip_data_pa_offset); \ }) +#else +#define kernel_mapping_va_to_pa(y) ((unsigned long)(y) - kernel_map.va_kernel_pa_offset) +#endif #define __va_to_pa_nodebug(x) ({ \ unsigned long _x = x; \ diff --git a/arch/riscv/mm/init.c b/arch/riscv/mm/init.c index 28f9c37443c8..313459329d16 100644 --- a/arch/riscv/mm/init.c +++ b/arch/riscv/mm/init.c @@ -1092,11 +1092,14 @@ asmlinkage void __init setup_vm(uintptr_t dtb_pa) kernel_map.phys_addr = (uintptr_t)CONFIG_PHYS_RAM_BASE; kernel_map.size = (uintptr_t)(&_end) - (uintptr_t)(&_start); - kernel_map.va_kernel_xip_pa_offset = kernel_map.virt_addr - kernel_map.xiprom; + kernel_map.va_kernel_xip_text_pa_offset = kernel_map.virt_addr - kernel_map.xiprom; + kernel_map.va_kernel_xip_data_pa_offset = kernel_map.virt_addr - kernel_map.phys_addr + + (uintptr_t)&_sdata - (uintptr_t)&_start; #else kernel_map.page_offset = _AC(CONFIG_PAGE_OFFSET, UL); kernel_map.phys_addr = (uintptr_t)(&_start); kernel_map.size = (uintptr_t)(&_end) - kernel_map.phys_addr; + kernel_map.va_kernel_pa_offset = kernel_map.virt_addr - kernel_map.phys_addr; #endif #if defined(CONFIG_64BIT) && !defined(CONFIG_XIP_KERNEL) @@ -1118,7 +1121,6 @@ asmlinkage void __init setup_vm(uintptr_t dtb_pa) */ kernel_map.va_pa_offset = IS_ENABLED(CONFIG_64BIT) ? 0UL : PAGE_OFFSET - kernel_map.phys_addr; - kernel_map.va_kernel_pa_offset = kernel_map.virt_addr - kernel_map.phys_addr; /* * The default maximal physical memory size is KERN_VIRT_SIZE for 32-bit -- 2.39.2