Unverified Commit 26e7aacb authored by Alexandre Ghiti's avatar Alexandre Ghiti Committed by Palmer Dabbelt
Browse files

riscv: Allow to downgrade paging mode from the command line



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.

In KASAN kernels, we can't use the libfdt that early in the boot process
since we are not ready to execute instrumented functions. So instead of
using the "generic" libfdt, we compile our own versions of those functions
that are not instrumented and that are prefixed so that they do not
conflict with the generic ones. We also need the non-instrumented versions
of the string functions and the prefixed versions of memcpy/memmove.

This is largely inspired by commit aacd149b ("arm64: head: avoid
relocating the kernel twice for KASLR") from which I removed compilation
flags that were not relevant to RISC-V at the moment (LTO, SCS). Also
note that we have to link with -z norelro to avoid ld.lld to throw a
warning with the new .got sections, like in commit 311bea3c ("arm64:
link with -z norelro for LLD or aarch64-elf").

Signed-off-by: default avatarAlexandre Ghiti <alexghiti@rivosinc.com>
Tested-by: default avatarBjörn Töpel <bjorn@rivosinc.com>
Reviewed-by: default avatarBjörn Töpel <bjorn@rivosinc.com>
Link: https://lore.kernel.org/r/20230424092313.178699-2-alexghiti@rivosinc.com


Signed-off-by: default avatarPalmer Dabbelt <palmer@rivosinc.com>
parent d4dda690
Loading
Loading
Loading
Loading
+4 −1
Original line number Diff line number Diff line
@@ -3576,7 +3576,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 and 5-level paging modes. 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.
+2 −1
Original line number Diff line number Diff line
@@ -7,8 +7,9 @@
#

OBJCOPYFLAGS    := -O binary
LDFLAGS_vmlinux := -z norelro
ifeq ($(CONFIG_RELOCATABLE),y)
	LDFLAGS_vmlinux += -shared -Bsymbolic -z notext -z norelro --emit-relocs
	LDFLAGS_vmlinux += -shared -Bsymbolic -z notext --emit-relocs
	KBUILD_CFLAGS += -fPIE
endif
ifeq ($(CONFIG_DYNAMIC_FTRACE),y)
+2 −0
Original line number Diff line number Diff line
@@ -87,3 +87,5 @@ obj-$(CONFIG_EFI) += efi.o
obj-$(CONFIG_COMPAT)		+= compat_syscall_table.o
obj-$(CONFIG_COMPAT)		+= compat_signal.o
obj-$(CONFIG_COMPAT)		+= compat_vdso/

obj-$(CONFIG_64BIT)		+= pi/
+39 −0
Original line number Diff line number Diff line
# SPDX-License-Identifier: GPL-2.0
# This file was copied from arm64/kernel/pi/Makefile.

KBUILD_CFLAGS	:= $(subst $(CC_FLAGS_FTRACE),,$(KBUILD_CFLAGS)) -fpie \
		   -Os -DDISABLE_BRANCH_PROFILING $(DISABLE_STACKLEAK_PLUGIN) \
		   $(call cc-option,-mbranch-protection=none) \
		   -I$(srctree)/scripts/dtc/libfdt -fno-stack-protector \
		   -D__DISABLE_EXPORTS -ffreestanding \
		   -fno-asynchronous-unwind-tables -fno-unwind-tables \
		   $(call cc-option,-fno-addrsig)

KBUILD_CFLAGS	+= -mcmodel=medany

CFLAGS_cmdline_early.o += -D__NO_FORTIFY
CFLAGS_lib-fdt_ro.o += -D__NO_FORTIFY

GCOV_PROFILE	:= n
KASAN_SANITIZE	:= n
KCSAN_SANITIZE	:= n
UBSAN_SANITIZE	:= n
KCOV_INSTRUMENT	:= n

$(obj)/%.pi.o: OBJCOPYFLAGS := --prefix-symbols=__pi_ \
			       --remove-section=.note.gnu.property \
			       --prefix-alloc-sections=.init
$(obj)/%.pi.o: $(obj)/%.o FORCE
	$(call if_changed,objcopy)

$(obj)/lib-%.o: $(srctree)/lib/%.c FORCE
	$(call if_changed_rule,cc_o_c)

$(obj)/string.o: $(srctree)/lib/string.c FORCE
	$(call if_changed_rule,cc_o_c)

$(obj)/ctype.o: $(srctree)/lib/ctype.c FORCE
	$(call if_changed_rule,cc_o_c)

obj-y		:= cmdline_early.pi.o string.pi.o ctype.pi.o lib-fdt.pi.o lib-fdt_ro.pi.o
extra-y		:= $(patsubst %.pi.o,%.o,$(obj-y))
+62 −0
Original line number Diff line number Diff line
// SPDX-License-Identifier: GPL-2.0-only
#include <linux/types.h>
#include <linux/init.h>
#include <linux/libfdt.h>
#include <linux/string.h>
#include <asm/pgtable.h>
#include <asm/setup.h>

static char early_cmdline[COMMAND_LINE_SIZE];

/*
 * Declare the functions that are exported (but prefixed) here so that LLVM
 * does not complain it lacks the 'static' keyword (which, if added, makes
 * LLVM complain because the function is actually unused in this file).
 */
u64 set_satp_mode_from_cmdline(uintptr_t dtb_pa);

static char *get_early_cmdline(uintptr_t dtb_pa)
{
	const char *fdt_cmdline = NULL;
	unsigned int fdt_cmdline_size = 0;
	int chosen_node;

	if (!IS_ENABLED(CONFIG_CMDLINE_FORCE)) {
		chosen_node = fdt_path_offset((void *)dtb_pa, "/chosen");
		if (chosen_node >= 0) {
			fdt_cmdline = fdt_getprop((void *)dtb_pa, chosen_node,
						  "bootargs", NULL);
			if (fdt_cmdline) {
				fdt_cmdline_size = strlen(fdt_cmdline);
				strscpy(early_cmdline, fdt_cmdline,
					COMMAND_LINE_SIZE);
			}
		}
	}

	if (IS_ENABLED(CONFIG_CMDLINE_EXTEND) ||
	    IS_ENABLED(CONFIG_CMDLINE_FORCE) ||
	    fdt_cmdline_size == 0 /* CONFIG_CMDLINE_FALLBACK */) {
		strncat(early_cmdline, CONFIG_CMDLINE,
			COMMAND_LINE_SIZE - fdt_cmdline_size);
	}

	return early_cmdline;
}

static u64 match_noXlvl(char *cmdline)
{
	if (strstr(cmdline, "no4lvl"))
		return SATP_MODE_48;
	else if (strstr(cmdline, "no5lvl"))
		return SATP_MODE_57;

	return 0;
}

u64 set_satp_mode_from_cmdline(uintptr_t dtb_pa)
{
	char *cmdline = get_early_cmdline(dtb_pa);

	return match_noXlvl(cmdline);
}
Loading