From patchwork Thu Dec 15 08:19:48 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Alexandre Ghiti X-Patchwork-Id: 13073970 X-Patchwork-Delegate: palmer@dabbelt.com 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 AFF7AC4332F for ; Thu, 15 Dec 2022 08:20:06 +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=yKQfsWUKjnm1T5LtEkrzfKcBuhF/p5Abs0c3IilSyFQ=; b=Gk7cb8S/6becg0 ma40MZ8ycOuMF89KZLesg6qpVqDBVleh2BkNNmJx2dpICDm4E4dAhNcmlycFcgvKhhAbJwlxCDbef 9jARnYFVxtobv4BpqYag+IjnbDHYlYi8pf9s7Jf5MFaa3KoGYQ/1LH5js6iJBFkVuWh+F+vZAVsLQ B4sGUhPfXnTA2SYFOYqYD0xiFALhYO66WTAjiZrxZ6QJV7Z/o+ZQAibcv/OtFowav4OUUwz986lwD sQGkpBi/5qmszZXWN8vp4G3UOgmRYeXPROD+7tQmJd7HQNao6AiOCfMtdpcZRKzYxLZhXVkxBxWuV x58CvD5CTTECNfxyavZw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1p5jTB-007cnK-K9; Thu, 15 Dec 2022 08:19:57 +0000 Received: from mail-wm1-x344.google.com ([2a00:1450:4864:20::344]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1p5jT8-007ckY-KO for linux-riscv@lists.infradead.org; Thu, 15 Dec 2022 08:19:56 +0000 Received: by mail-wm1-x344.google.com with SMTP id bg10so13166261wmb.1 for ; Thu, 15 Dec 2022 00:19:53 -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=P5JatZUrB4wn6RHIEmLRs1De+PDGwMqcpJtj1hMpn28=; b=JLzjunRNOInwlypF9TQNBJoNGaay7JWcNf7YoQGqGXE60pbfIWK4g6E87kqaTlNEjX QYWw/I0p6xgHoTGol6FiQPq9nTnuTvFo2YLnBTUg5Ueb5OYw4a8ptZQQTNH7k2Fp3YhD npDLW5SBQ5HkMKkUA8u2VTSA1gi+lAkedt2CugVww2Eze30/Pmfi+1tCa5dpR610Yter uauMBuCIAdvh+JMFerv+vRM5FDaeSnHgU/XEyIR0kuVkuCIsqJi+Smi0sL2Wb8m6GZ/b Uq44xe1xMFDjbmCpGCYxjhjsgdPl3K5A7Kfkec34CCBAFu4a0SBQKbD4NBulirE5Hduj A9TA== 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=P5JatZUrB4wn6RHIEmLRs1De+PDGwMqcpJtj1hMpn28=; b=Ru2I0uEDbuV82m72xXKYVe1lzRgUHx65uaB6xX9E3GqsyisIQg4McjvSsk686j72wE dAe2ooMQJnSROFJP2BThD3OeWVfqBannv2U24bMOYEH9WZ5hOtcVho0x/iUWnFjahDaL cqqqVWARS2XFpuO/iogj9dlFWrCPbApvQrqKh+LGlrF6VL6VgxP6dx2CPn567KwpUWFL hyvZZGCV3ZX7iwr072P3B71EUXEQZbRknZrGJ/jLqnwZdcNjjN6kmXEnD0m94yW1pXfR 5pQZwVl489TV5abFYQUBeYjUE4oxbZrMk5Dezj5XOMhoD55JmQQszLngJZQmlUQbjTaM wPvw== X-Gm-Message-State: ANoB5pm8X+L7jlnNx0YX0CEDOPbTAoGDujJKiu6a71IvcQ3X2o1CCCDs o1OVgUMln8pPapEnHX6z9mm2Mw== X-Google-Smtp-Source: AA0mqf5WjGshX/H4Sy5tMFHvg73SLxcJk240JuKos8U0A2iaj5qoJywpDAzH4oFnYFIYMozh6bDxPQ== X-Received: by 2002:a7b:ce89:0:b0:3d2:3be4:2d9a with SMTP id q9-20020a7bce89000000b003d23be42d9amr3967121wmj.20.1671092392702; Thu, 15 Dec 2022 00:19:52 -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 f9-20020a05600c154900b003b49bd61b19sm1615436wmg.15.2022.12.15.00.19.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 15 Dec 2022 00:19:52 -0800 (PST) From: Alexandre Ghiti To: Jonathan Corbet , Paul Walmsley , Palmer Dabbelt , Albert Ou , =?utf-8?b?QmrDtnJuIFTDtnBlbA==?= , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-riscv@lists.infradead.org Cc: Alexandre Ghiti Subject: [PATCH v2] riscv: Allow to downgrade paging mode from the command line Date: Thu, 15 Dec 2022 09:19:48 +0100 Message-Id: <20221215081948.1256610-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-20221215_001954_914511_F60E1D56 X-CRM114-Status: GOOD ( 20.98 ) 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 called "no5lvl" and "no4lvl" (using the same naming as x86) to allow a user to downgrade from sv57 (the default mode if the hardware supports it) to sv48 or 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. Signed-off-by: Alexandre Ghiti Reviewed-by: Björn Töpel --- v2: - Honor CMDLINE_EXTEND and CMDLINE_FORCE as noticed by Björn .../admin-guide/kernel-parameters.txt | 5 +- arch/riscv/mm/init.c | 72 +++++++++++++++++-- 2 files changed, 70 insertions(+), 7 deletions(-) diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt index a465d5242774..6741524aa980 100644 --- a/Documentation/admin-guide/kernel-parameters.txt +++ b/Documentation/admin-guide/kernel-parameters.txt @@ -3548,7 +3548,10 @@ emulation library even if a 387 maths coprocessor is present. - no5lvl [X86-64] Disable 5-level paging mode. Forces + no4lvl [RISCV] Disable 4-level paging mode. Forces + kernel to use 3-level paging instead. + + no5lvl [X86-64,RISCV] Disable 5-level paging mode. Forces kernel to use 4-level paging instead. nofsgsbase [X86] Disables FSGSBASE instructions. diff --git a/arch/riscv/mm/init.c b/arch/riscv/mm/init.c index b56a0a75533f..d90fbe9ad494 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;