Allow calls in alternatives
From: Heiko Stuebner <heiko.stuebner@...> Date: Fri, 23 Dec 2022 22:58:52 +0100
This series is split out of my work on optimizing string functions and provides the basics to: - actually allowing calls in alternatives Function calls use auipc + jalr to reach those 32bit relative addresses but when they're compiled the offset will be wrong as alternatives live in a different section. So when the patch gets applied the address will point to the wrong location. So similar to arm64 the target addresses need to be updated. This is probably also helpful for other things needing more complex code in alternatives. For v2 I got into some sort of cleanup spree for the general instruction parsing that already existed. A number of places do their own instruction parsing and I tried consolidating some of them. Noteable, the kvm parts still do, but I had to stop somewhere :-) The series is based on commit 51094a24b85e ("Merge tag 'hardening-v6.2-rc1-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/kees/linux") from Fri Dec 23 12:00:24 2022 -0800 right now, this is the most current commit during the merge window and seems to include most or all riscv changes for the 6.2 merge-window, so the series should apply cleanly once 6.2-rc1 is out. changes since v4: - while people reporting nits for v4 mentioned that it wouldn't be necessary to respin for each, the number of them the series received waranted a respin I think - split riscv_insn_insert_utype_itype_imm parameters into separate utype + itype u32 pointer (Andrew) - use u32 instead of unsigned int in riscv_instruction_at (Jess) - rename instruction params in riscv_alternative_fix_auipc_jalr make their content obvious (Andrew) - standardize on insn for instruction-related variables (Conor) - make comment about ra register check more explicit (Conor) changes since v3: - separate allowing calls in alternatives from string work - move the immediate handling for auipc+jalr into the insn.h header This allows other parts of the kernel to reuse this, instead of duplicating the code in a number or areas - adjust the riscv_alternative_fix_auipc_jalr function to be called from a central _fix_offsets function, so that other offsets can get fixed from the same loop in the future (jal, etc) I've removed Conor's Reviewed-by: from that last patch, as it changed so much since v3. changes since v2: - add patch fixing the c.jalr funct4 value - reword some commit messages - fix position of auipc addition patch (earlier) - fix compile errors from patch-reordering gone wrong (worked at the end of v2, but compiling individual patches caused issues) - patches are now tested individually - limit Zbb variants for GNU as for now (LLVM support for .option arch is still under review) - prevent str-functions from getting optimized to builtin-variants changes since v1: - a number of generalizations/cleanups for instruction parsing - use accessor function to access instructions (Emil) - actually patch the correct location when having more than one instruction in an alternative block - string function cleanups (comments etc) (Conor) - move zbb extension above s* extensions in cpu.c lists changes since rfc: - make Zbb code actually work - drop some unneeded patches - a lot of cleanups Heiko Stuebner (12): RISC-V: fix funct4 definition for c.jalr in parse_asm.h RISC-V: add prefix to all constants/macros in parse_asm.h RISC-V: detach funct-values from their offset RISC-V: add ebreak instructions to definitions RISC-V: add auipc elements to parse_asm header RISC-V: Move riscv_insn_is_* macros into a common header RISC-V: rename parse_asm.h to insn.h RISC-V: kprobes: use central defined funct3 constants RISC-V: add U-type imm parsing to insn.h header RISC-V: add rd reg parsing to insn.h header RISC-V: add helpers for handling immediates in U-type and I-type pairs RISC-V: fix auipc-jalr addresses in patched alternatives arch/riscv/include/asm/alternative.h | 3 + arch/riscv/include/asm/insn.h | 340 +++++++++++++++++++++++ arch/riscv/include/asm/parse_asm.h | 219 --------------- arch/riscv/kernel/alternative.c | 56 ++++ arch/riscv/kernel/cpufeature.c | 5 +- arch/riscv/kernel/kgdb.c | 63 ++--- arch/riscv/kernel/probes/simulate-insn.c | 19 +- arch/riscv/kernel/probes/simulate-insn.h | 26 +- 8 files changed, 436 insertions(+), 295 deletions(-) create mode 100644 arch/riscv/include/asm/insn.h delete mode 100644 arch/riscv/include/asm/parse_asm.h -- 2.35.1
Patches in this set
- [0001] RISC-V: fix funct4 definition for c.jalr in parse_asm.h (raw)
- [0002] RISC-V: add prefix to all constants/macros in (raw)
- [0003] RISC-V: detach funct-values from their offset (raw)
- [0004] RISC-V: add ebreak instructions to definitions (raw)
- [0005] RISC-V: add auipc elements to parse_asm header (raw)
- [0006] RISC-V: Move riscv_insn_is_* macros into a common (raw)
- [0007] RISC-V: rename parse_asm.h to insn.h (raw)
- [0008] RISC-V: kprobes: use central defined funct3 constants (raw)
- [0009] RISC-V: add U-type imm parsing to insn.h header (raw)
- [0010] RISC-V: add rd reg parsing to insn.h header (raw)
- [0011] RISC-V: add helpers for handling immediates in U-type (raw)
- [0012] RISC-V: fix auipc-jalr addresses in patched (raw)
Statistics
- 12 individual patches
- 378 lines added
- 237 lines removed