From patchwork Mon Jan 23 10:48:41 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Alexandre Ghiti X-Patchwork-Id: 13111980 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 12111C05027 for ; Mon, 23 Jan 2023 10:49:04 +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=cD89lagJlErSe0uovkDwd18AEaI2S2rMShBn+IbdYzM=; b=XenyHYmtcf4PnG VPEy6a5jDAC7yop6KnWEmWIB6sDWpLgUgWzrnLKJjjw7jN5ftle3iFMIbbsCpJrEt25NPKF0Pnzj1 4hDFVqfI6OF44MM6v/cAea7SBOUx8oghLRtlatLWKxFe5LlYIrJoRzLXibL4VTsXwFNrHx/tHf9+H bed1rhvd2bauuZE+vZXWPAbkwiZ6UMatqT+MBucns3bSlyFtcMdPgYhmBuUqwGm9IKmKCzINz5NXy ntwQJAhmgZM9jUBiivKPGmaKArqHdfeMHgj2FKy0WKy53UTdZa64G7iI/MPHFGucfxY04Yqd5mWQU sOfh+GmamXKr0nW391OQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1pJuNg-00GtGy-Mt; Mon, 23 Jan 2023 10:48:52 +0000 Received: from mail-wr1-x42c.google.com ([2a00:1450:4864:20::42c]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1pJuNd-00GtFn-9x for linux-riscv@lists.infradead.org; Mon, 23 Jan 2023 10:48:51 +0000 Received: by mail-wr1-x42c.google.com with SMTP id h12so6365110wrv.10 for ; Mon, 23 Jan 2023 02:48:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=B8g2lbRRVQxElqC+vhwxvlAd9Yl9IHH5TPADK+PZ98Q=; b=2UwllXEV1F4+37rtQa+xgzMqL7ieZGIS5R90acCmnh/NFf54gvz12Z2D7DZRryf2Kw 5JKxKy+0HL4QeXeBhvERCl4u7ebN29K1u0svY3d4dZO4jmXjDp1HqI9CnJ2qhDE5yCHY tL9uxcgeN6euhtF5uxKAzYylIY++cbUzwdhkjLjxQatr+RISnAsFA0CC4HRpJ1ZjoiZE R/CK5wbz/6o0W9L3dj7e90x7LSqWtsTh6qDw1Pdut+yByYKO6T4z94ls5LZoCYBr+AFX S1ZkPwWZeMLv+dJ4WfkbYXKdbvOsZpYYOa2ioaiChOOymuKDMNc4azdVyMiqDG/GWwxg FmiA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=B8g2lbRRVQxElqC+vhwxvlAd9Yl9IHH5TPADK+PZ98Q=; b=zRi654apRx2I3nXRNM5ZXtwkh6HgO9TPf6cHGiSR68pBialIySF/41bWrvisQQMAR0 LPqK9ZO6tjlg9uzQ7Rzkh4x+LTCJASoIC0zV/awy+MSEIk2ZyultGUuPb7RDQrkVLffc n24/4vCjyQQ1XJ1uzvMjFAlk7SzbU0vFvBaqsPRaZL0tGtz2nerrcDsNdnktwxsLHk+V WN4JKduN7TU65A80pujDbMAbHJSn6/RkgA5jPG4jgQ2cbpvJX7L78sZauPBg+B/dc5CP fSa1qP7MZcNNECm/LmpweXzG6m0n06EHWiuwqe1+iIGORgqPH2FlOq2r5PC3yQc7qIeP jEqg== X-Gm-Message-State: AFqh2ko8zYHksKNCPxiTKjiuBcgi2X3PVvTCf9ZdnSmNlsTgeVG+Aitm sgn6N0XgM3eAem8dU2zkN7CPbQ== X-Google-Smtp-Source: AMrXdXtsA5psCjwW6D5BLbiw+fn+Rog8dlvoO4malOUPAWvs7qusWFUIgGi3ZiO22gkyt4LV76DJrg== X-Received: by 2002:adf:ec8d:0:b0:2bd:d7fd:6c93 with SMTP id z13-20020adfec8d000000b002bdd7fd6c93mr20023096wrn.48.1674470927875; Mon, 23 Jan 2023 02:48:47 -0800 (PST) Received: from alex-rivos.ba.rivosinc.com (lfbn-lyo-1-450-160.w2-7.abo.wanadoo.fr. [2.7.42.160]) by smtp.gmail.com with ESMTPSA id c8-20020a5d4148000000b002bdd21f25ebsm27562327wrq.71.2023.01.23.02.48.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Jan 2023 02:48:47 -0800 (PST) From: Alexandre Ghiti To: Jonathan Corbet , Paul Walmsley , Palmer Dabbelt , Albert Ou , Conor Dooley , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-riscv@lists.infradead.org Cc: Alexandre Ghiti , =?utf-8?b?QmrDtnJuIFTDtnBlbA==?= Subject: [PATCH v3] riscv: Allow to downgrade paging mode from the command Date: Mon, 23 Jan 2023 11:48:41 +0100 Message-Id: <20230123104841.813999-1-alexghiti@rivosinc.com> X-Mailer: git-send-email 2.37.2 MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230123_024849_370469_2758EFE5 X-CRM114-Status: GOOD ( 20.73 ) 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 Add 2 early command line parameters that allow to downgrade satp mode (using the same naming as x86): - "no5lvl": use a 4-level page table (down from sv57 to sv48) - "no4lvl": use a 3-level page table (down from sv57/sv48 to sv39) Note that going through the device tree to get the kernel command line works with ACPI too since the efi stub creates a device tree anyway with the command line. Also, as those params are treated very early in the boot process and we use standard device tree functions that may be kasan instrumented, we only enable them for !KASAN configurations. Reviewed-by: Björn Töpel Signed-off-by: Alexandre Ghiti Reviewed-by: Conor Dooley --- .../admin-guide/kernel-parameters.txt | 9 ++- arch/riscv/mm/init.c | 72 +++++++++++++++++-- 2 files changed, 74 insertions(+), 7 deletions(-) diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt index 6cfa6e3996cf..fd647412ec91 100644 --- a/Documentation/admin-guide/kernel-parameters.txt +++ b/Documentation/admin-guide/kernel-parameters.txt @@ -3578,8 +3578,15 @@ emulation library even if a 387 maths coprocessor is present. - no5lvl [X86-64] Disable 5-level paging mode. Forces + no4lvl [RISCV] Disable 4-level and 5-level paging modes. Forces + kernel to use 3-level paging instead. + Note: On RISC-V, this can't be used when KASAN is + enabled. + + no5lvl [X86-64,RISCV] Disable 5-level paging mode. Forces kernel to use 4-level paging instead. + Note: On RISC-V, this can't be used when KASAN is + enabled. nofsgsbase [X86] Disables FSGSBASE instructions. diff --git a/arch/riscv/mm/init.c b/arch/riscv/mm/init.c index 478d6763a01a..10f99fa74368 100644 --- a/arch/riscv/mm/init.c +++ b/arch/riscv/mm/init.c @@ -746,17 +746,77 @@ static void __init disable_pgtable_l4(void) satp_mode = SATP_MODE_39; } +#ifndef CONFIG_KASAN +static __init bool match_noXlvl(const char *cmdline) +{ + if (strstr(cmdline, "no5lvl")) { + disable_pgtable_l5(); + } else if (strstr(cmdline, "no4lvl")) { + disable_pgtable_l5(); + disable_pgtable_l4(); + return true; + } + + return false; +} + +static int __init print_no4lvl(char *p) +{ + pr_info("Disabled 4-level and 5-level paging"); + return 0; +} +early_param("no4lvl", print_no4lvl); + +static int __init print_no5lvl(char *p) +{ + pr_info("Disabled 5-level paging"); + return 0; +} +early_param("no5lvl", print_no5lvl); +#endif + /* * There is a simple way to determine if 4-level is supported by the * underlying hardware: establish 1:1 mapping in 4-level page table mode * then read SATP to see if the configuration was taken into account * meaning sv48 is supported. */ -static __init void set_satp_mode(void) +static __init void set_satp_mode(uintptr_t dtb_pa) { u64 identity_satp, hw_satp; uintptr_t set_satp_mode_pmd = ((unsigned long)set_satp_mode) & PMD_MASK; - bool check_l4 = false; + +#ifndef CONFIG_KASAN + /* + * The below fdt functions are kasan instrumented, since at this point + * there is no mapping for the kasan shadow memory, this can't be used + * when kasan is enabled. + */ + int chosen_node; + unsigned int fdt_cmdline_size = 0; + + if (!IS_ENABLED(CONFIG_CMDLINE_FORCE)) { + chosen_node = fdt_path_offset((void *)dtb_pa, "/chosen"); + if (chosen_node >= 0) { + const char *fdt_cmdline; + + fdt_cmdline = fdt_getprop((void *)dtb_pa, chosen_node, + "bootargs", NULL); + if (fdt_cmdline) { + if (match_noXlvl(fdt_cmdline)) + return; + fdt_cmdline_size = strlen(fdt_cmdline); + } + } + } + + if (IS_ENABLED(CONFIG_CMDLINE_EXTEND) || + IS_ENABLED(CONFIG_CMDLINE_FORCE) || + fdt_cmdline_size == 0 /* CONFIG_CMDLINE_FALLBACK */) { + if (match_noXlvl(CONFIG_CMDLINE)) + return; + } +#endif create_p4d_mapping(early_p4d, set_satp_mode_pmd, (uintptr_t)early_pud, @@ -775,7 +835,8 @@ static __init void set_satp_mode(void) retry: create_pgd_mapping(early_pg_dir, set_satp_mode_pmd, - check_l4 ? (uintptr_t)early_pud : (uintptr_t)early_p4d, + pgtable_l5_enabled ? + (uintptr_t)early_p4d : (uintptr_t)early_pud, PGDIR_SIZE, PAGE_TABLE); identity_satp = PFN_DOWN((uintptr_t)&early_pg_dir) | satp_mode; @@ -786,9 +847,8 @@ static __init void set_satp_mode(void) local_flush_tlb_all(); if (hw_satp != identity_satp) { - if (!check_l4) { + if (pgtable_l5_enabled) { disable_pgtable_l5(); - check_l4 = true; memset(early_pg_dir, 0, PAGE_SIZE); goto retry; } @@ -979,7 +1039,7 @@ asmlinkage void __init setup_vm(uintptr_t dtb_pa) #endif #if defined(CONFIG_64BIT) && !defined(CONFIG_XIP_KERNEL) - set_satp_mode(); + set_satp_mode(dtb_pa); #endif kernel_map.va_pa_offset = PAGE_OFFSET - kernel_map.phys_addr;