From patchwork Wed Mar 13 08:45:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?Q2zDqW1lbnQgTMOpZ2Vy?= X-Patchwork-Id: 13591117 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 2D71AC54791 for ; Wed, 13 Mar 2024 08:46:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:Message-ID:Date:Subject:Cc :To:From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=/ZQERprJkuxB7dnDIOQJ5DAyKVW4Pm89dYrOL0m4Ia0=; b=qujAKhzPLbY7lT 60k2VW/kyZoZORbJvLxEdopSw95D7wjS9aEPd/YgVkD0ezG3/UpAU+m2inInBsyxcS+ex6GSE1oPv u71HXWS48sIq41lLc/zgSxxff3Va/eD3swD/PIqvZ3CFMlV2A+yCSWitRsb5Tvv7KxfGA0Ac6WiqZ 52iigxlvkS+8sFMwpLvzSAPWbpJ64NZia1rnl7AnEnCiOI1IVwTg9AhllAhfGbkslroyMdUGys2Pb 8wKZzB3aQIG/n8Zx/L97SoYDAzxARJyEoYPLHQF2NPbQ0ekNheaoKfuuvgdiQLJLc3TPj1J8J40+R Uu6UosFKghvxAmqTD6Ww==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rkKFZ-00000009MkZ-2ePS; Wed, 13 Mar 2024 08:46:13 +0000 Received: from mail-wr1-x433.google.com ([2a00:1450:4864:20::433]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rkKFU-00000009Mjd-3sXC for linux-riscv@lists.infradead.org; Wed, 13 Mar 2024 08:46:11 +0000 Received: by mail-wr1-x433.google.com with SMTP id ffacd0b85a97d-33e9115d501so1163405f8f.0 for ; Wed, 13 Mar 2024 01:46:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1710319567; x=1710924367; darn=lists.infradead.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=xl9/EZGLKxGGHgqxXQrNVV9/fXW9fCPtZn84F/TZCWo=; b=TZ0/l8IGwpWNEcAiLiqZmQxjWh/58BlQ3ke4fcQzupKidhLy6THgXx6jOm711eo8AL 00saf+IYYH0x17c4lF10QVjnIjoiU8k5UafhCSke1xIa1Z4MgmaogJa4wAI38rk2sJzC 2qmSNP/god2QpD3bLAnMwBAMLi79Ppea7q3zlYEHAl3fVTM7V4BTo9dY/48urCmLwvum i//9dMMRJpf6EmjGBtlZgXROhOMvzXUAxGmkP7vRfdj9HNzjf3DeoM77ODOkE2gp7jwm 4XJ2xObdcrKCv2QX0vcNeW6KKqr8B14XLwltVe3P8jPb3LkICsEgTZC82NbcjOPX5IKw C4jA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710319567; x=1710924367; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=xl9/EZGLKxGGHgqxXQrNVV9/fXW9fCPtZn84F/TZCWo=; b=pMS3ZWxr0LHjD0/5JVQINSWPPfWOFpVvZFrt1aMwnJ+K+Yl5UpDg7vNTZMJesNjysE QyjIeBe7l3WVDdpLtpDGyxfw60J5mDkRtEFBfQbSeqkk+voGv2cH8Fr+NlCQAZgYWf1J dx5YYqROWEd8JJu/OtY/jHcXR+UhyVxHlqhJCuVddWw7iMeIYxZMen5AizpP12/AcEol Oht0qHeqduW6godq254N5PQTlgtTsJ0HQ28vwycPQW6jn7Bpt+GKPF4x8pU2pVA6t8NP XSOmko7bbXWkLEB15ApTjXlSFvSsyo819nYRYbLbOkEdJ+L2AtnUvdSgpDrJ484E71WK hCcA== X-Forwarded-Encrypted: i=1; AJvYcCX0XhmLGM2SjhS2G+mA78vzezVwSoT2HYmQzHIPlfJ+F4sWDiX3sV3EVf7ZoXzxRkaT1KN2rI9OLZD/YxY2ycVKvc7HxyutDCPGhUrWX69t X-Gm-Message-State: AOJu0YxX9ZmRGE7ndnclhAi7nZDWR5XRBPwLfuW6TzgqnQgornOHfFi/ /0fbveKGiPDjvMmBQmhFUXOU5pqhulX4RflTvJcAt2jCSqwlGiGGASVLjc57Tjc= X-Google-Smtp-Source: AGHT+IE5ezVmJ/Ll3UL9RF2ZSCycTaV8On1GZv4yRfDw6BjYLYByS2PLSa+T54t0GJaDHT8MYXbE3w== X-Received: by 2002:a05:600c:1c84:b0:413:4299:ecd7 with SMTP id k4-20020a05600c1c8400b004134299ecd7mr2825793wms.0.1710319566968; Wed, 13 Mar 2024 01:46:06 -0700 (PDT) Received: from carbon-x1.. ([2a01:e0a:999:a3a0:1a26:f18f:eb07:b76a]) by smtp.gmail.com with ESMTPSA id w9-20020a05600c474900b00412706c3ddasm1612743wmo.18.2024.03.13.01.46.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Mar 2024 01:46:06 -0700 (PDT) From: =?utf-8?b?Q2zDqW1lbnQgTMOpZ2Vy?= To: Jonathan Corbet , Paul Walmsley , Palmer Dabbelt , Albert Ou , linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org Cc: =?utf-8?b?Q2zDqW1lbnQgTMOpZ2Vy?= , Robin Ehn , Charlie Jenkins , "Stefan O'Rear" , Jessica Clarke Subject: [PATCH v4] riscv: hwprobe: export highest virtual userspace address Date: Wed, 13 Mar 2024 09:45:33 +0100 Message-ID: <20240313084554.31925-1-cleger@rivosinc.com> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240313_014609_219089_52051E22 X-CRM114-Status: GOOD ( 20.30 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org Some userspace applications (OpenJDK for instance) uses the free MSBs in pointers to insert additional information for their own logic and need to get this information from somewhere. Currently they rely on parsing /proc/cpuinfo "mmu=svxx" string to obtain the current value of virtual address usable bits [1]. Since this reflect the raw supported MMU mode, it might differ from the logical one used internally which is why arch_get_mmap_end() is used. Exporting the highest mmapable address through hwprobe will allow a more stable interface to be used. For that purpose, add a new hwprobe key named RISCV_HWPROBE_KEY_HIGHEST_VIRT_ADDRESS which will export the highest userspace virtual address. Link: https://github.com/openjdk/jdk/blob/master/src/hotspot/os_cpu/linux_riscv/vm_version_linux_riscv.cpp#L171 [1] Signed-off-by: Clément Léger Reviewed-by: Charlie Jenkins --- v4: - Added Charlie Reviewed-by - Replace a tab instead of a space - Resend with actual V4 tag in front of message which was missing - Link to v3: https://lore.kernel.org/lkml/20240301164407.475957-1-cleger@rivosinc.com/ v3: - Note: I did not added Stefan Reviewed-by since I did more modifications than just a simple respin. - Handle CONFIG_MMU=n as well and rename the key to RISCV_HWPROBE_KEY_HIGHEST_VIRT_ADDRESS - Add a user_max_virt_addr() macro to handle !MMU config - Link to v2: https://lore.kernel.org/lkml/20240220110950.871307-1-cleger@rivosinc.com/ v2: - Note: tried sysconf to export it but this is not backed by syscall and thus does not allow exporting such information easily. - Use arch_get_mmap_end() instead of VA_BITS since it reflects the maximum logical address used by the riscv port - Change hwprobe key name from RISCV_HWPROBE_KEY_VA_BITS to RISCV_HWPROBE_KEY_MAX_ADDRESS - Link to v1: https://lore.kernel.org/lkml/20240201140319.360088-1-cleger@rivosinc.com/ --- Documentation/arch/riscv/hwprobe.rst | 3 +++ arch/riscv/include/asm/hwprobe.h | 2 +- arch/riscv/include/asm/processor.h | 8 +++++++- arch/riscv/include/uapi/asm/hwprobe.h | 1 + arch/riscv/kernel/sys_hwprobe.c | 4 ++++ 5 files changed, 16 insertions(+), 2 deletions(-) diff --git a/Documentation/arch/riscv/hwprobe.rst b/Documentation/arch/riscv/hwprobe.rst index b2bcc9eed9aa..875d3122bd66 100644 --- a/Documentation/arch/riscv/hwprobe.rst +++ b/Documentation/arch/riscv/hwprobe.rst @@ -210,3 +210,6 @@ The following keys are defined: * :c:macro:`RISCV_HWPROBE_KEY_ZICBOZ_BLOCK_SIZE`: An unsigned int which represents the size of the Zicboz block in bytes. + +* :c:macro:`RISCV_HWPROBE_KEY_HIGHEST_VIRT_ADDRESS`: An unsigned long which + represent the highest userspace virtual address usable. diff --git a/arch/riscv/include/asm/hwprobe.h b/arch/riscv/include/asm/hwprobe.h index 630507dff5ea..150a9877b0af 100644 --- a/arch/riscv/include/asm/hwprobe.h +++ b/arch/riscv/include/asm/hwprobe.h @@ -8,7 +8,7 @@ #include -#define RISCV_HWPROBE_MAX_KEY 6 +#define RISCV_HWPROBE_MAX_KEY 7 static inline bool riscv_hwprobe_key_is_valid(__s64 key) { diff --git a/arch/riscv/include/asm/processor.h b/arch/riscv/include/asm/processor.h index a8509cc31ab2..8af1182f5180 100644 --- a/arch/riscv/include/asm/processor.h +++ b/arch/riscv/include/asm/processor.h @@ -14,7 +14,7 @@ #include -#ifdef CONFIG_64BIT +#if defined(CONFIG_64BIT) && defined(CONFIG_MMU) #define DEFAULT_MAP_WINDOW (UL(1) << (MMAP_VA_BITS - 1)) #define STACK_TOP_MAX TASK_SIZE @@ -58,6 +58,12 @@ #define STACK_TOP DEFAULT_MAP_WINDOW +#ifdef CONFIG_MMU +#define user_max_virt_addr() arch_get_mmap_end(ULONG_MAX, 0, 0) +#else +#define user_max_virt_addr() 0 +#endif /* CONFIG_MMU */ + /* * This decides where the kernel will search for a free chunk of vm * space during mmap's. diff --git a/arch/riscv/include/uapi/asm/hwprobe.h b/arch/riscv/include/uapi/asm/hwprobe.h index 9f2a8e3ff204..3630e3f30354 100644 --- a/arch/riscv/include/uapi/asm/hwprobe.h +++ b/arch/riscv/include/uapi/asm/hwprobe.h @@ -67,6 +67,7 @@ struct riscv_hwprobe { #define RISCV_HWPROBE_MISALIGNED_UNSUPPORTED (4 << 0) #define RISCV_HWPROBE_MISALIGNED_MASK (7 << 0) #define RISCV_HWPROBE_KEY_ZICBOZ_BLOCK_SIZE 6 +#define RISCV_HWPROBE_KEY_HIGHEST_VIRT_ADDRESS 7 /* Increase RISCV_HWPROBE_MAX_KEY when adding items. */ /* Flags */ diff --git a/arch/riscv/kernel/sys_hwprobe.c b/arch/riscv/kernel/sys_hwprobe.c index a7c56b41efd2..560ea41a716d 100644 --- a/arch/riscv/kernel/sys_hwprobe.c +++ b/arch/riscv/kernel/sys_hwprobe.c @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -202,6 +203,9 @@ static void hwprobe_one_pair(struct riscv_hwprobe *pair, if (hwprobe_ext0_has(cpus, RISCV_HWPROBE_EXT_ZICBOZ)) pair->value = riscv_cboz_block_size; break; + case RISCV_HWPROBE_KEY_HIGHEST_VIRT_ADDRESS: + pair->value = user_max_virt_addr(); + break; /* * For forward compatibility, unknown keys don't fail the whole