riscv: xip: support runtime trap patching
PATCH 1/1 riscv: xip: support runtime trap patching
RISCV_ERRATA_ALTERNATIVE patches text at runtime which is currently not possible when the kernel is executed from the flash in XIP mode. Since runtime patching concerns only traps at the moment, let's just have all the traps reside in RAM anyway if RISCV_ERRATA_ALTERNATIVE is set. Thus, these functions will be patch-able even when the .text section is in flash.
--- arch/riscv/kernel/traps.c | 13 +++++++++---- arch/riscv/kernel/vmlinux-xip.lds.S | 15 ++++++++++++++- 2 files changed, 23 insertions(+), 5 deletions(-)
arch/riscv/kernel/traps.c +9 -4
@@ -86,8 +86,13 @@ static void do_trap_error(struct pt_regs *regs, int signo, int code, } } +#if defined (CONFIG_XIP_KERNEL ) && defined (CONFIG_RISCV_ERRATA_ALTERNATIVE ) +#define __trap_section __section(".xip.traps") +#else +#define __trap_section +#endif #define DO_ERROR_INFO (name, signo, code, str) \ -asmlinkage __visible void name(struct pt_regs *regs) \ +asmlinkage __visible __trap_section void name(struct pt_regs *regs) \ { \ do_trap_error(regs, signo, code, regs->epc, "Oops - " str); \ }
@@ -111,7 +116,7 @@ DO_ERROR_INFO(do_trap_store_misaligned,int handle_misaligned_load (struct pt_regs *regs);int handle_misaligned_store (struct pt_regs *regs); -asmlinkage void do_trap_load_misaligned(struct pt_regs *regs) +asmlinkage void __trap_section do_trap_load_misaligned(struct pt_regs *regs) {if (!handle_misaligned_load (regs))return ;
@@ -119,7 +124,7 @@ asmlinkage void do_trap_load_misaligned(struct pt_regs *regs) "Oops - load address misaligned"); } -asmlinkage void do_trap_store_misaligned(struct pt_regs *regs) +asmlinkage void __trap_section do_trap_store_misaligned(struct pt_regs *regs) {if (!handle_misaligned_store (regs))return ;
@@ -146,7 +151,7 @@ static inline unsigned long get_break_insn_length(unsigned long pc)return GET_INSN_LENGTH (insn); } -asmlinkage __visible void do_trap_break(struct pt_regs *regs) +asmlinkage __visible __trap_section void do_trap_break(struct pt_regs *regs) {#ifdef CONFIG_KPROBES if (kprobe_single_step_handler (regs))
arch/riscv/kernel/vmlinux-xip.lds.S +14 -1
@@ -99,9 +99,22 @@ SECTIONS } PERCPU_SECTION(L1_CACHE_BYTES) - . = ALIGN(PAGE_SIZE); + . = ALIGN(8); + .alternative : { + __alt_start = .; + *(.alternative) + __alt_end = .; + } __init_end = .; + . = ALIGN(16); + .xip.traps : { + __xip_traps_start = .; + *(.xip.traps) + __xip_traps_end = .; + } + + . = ALIGN(PAGE_SIZE); .sdata : { __global_pointer$ = . + 0x800; *(.sdata*)
Signature · 1 line
2.29.2
Alex Ghiti
reviewerHi Vitaly,
Le 31/05/2021 à 10:53, Vitaly Wool a écrit :
Quoted 7 lines
RISCV_ERRATA_ALTERNATIVEpatches text at runtime which is currently not possible when the kernel is executed from the flash in XIP mode. Since runtime patching concerns only traps at the moment, let's just have all the traps reside in RAM anyway ifRISCV_ERRATA_ALTERNATIVEis set. Thus, these functions will be patch-able even when the .text section is in flash.
This sounds like a good fix for sifive platforms to work with XIP kernel in 5.13: did you test that it actually works on HW?
Quoted 11 lines
Signed-off-by Vitaly Wool <[email protected]>--- arch/riscv/kernel/traps.c | 13 +++++++++---- arch/riscv/kernel/vmlinux-xip.lds.S | 15 ++++++++++++++- 2 files changed, 23 insertions(+), 5 deletions(-)arch/riscv/kernel/traps.c +0
-0
arch/riscv/kernel/traps.c static void do_trap_error(struct pt_regs *regs, int signo, int code, · line 93
Quoted 8 lines
arch/riscv/kernel/traps.c +5
-0@@ -86,8 +86,13 @@ static void do_trap_error(struct pt_regs *regs, int signo, int code, } } +#if defined (CONFIG_XIP_KERNEL ) && defined (CONFIG_RISCV_ERRATA_ALTERNATIVE ) +#define __trap_section __section(".xip.traps") +#else +#define __trap_section +#endif
Maybe we could do something more generic. At the moment, only traps are subject to alternatives but that will likely expand: what about rather defining a section called __alternative_section?
Quoted 41 lines
arch/riscv/kernel/traps.c +4
-4#define DO_ERROR_INFO (name, signo, code, str) \ -asmlinkage __visible void name(struct pt_regs *regs) \ +asmlinkage __visible __trap_section void name(struct pt_regs *regs) \ { \ do_trap_error(regs, signo, code, regs->epc, "Oops - " str); \ }@@ -111,7 +116,7 @@ DO_ERROR_INFO(do_trap_store_misaligned,int handle_misaligned_load (struct pt_regs *regs);int handle_misaligned_store (struct pt_regs *regs); -asmlinkage void do_trap_load_misaligned(struct pt_regs *regs) +asmlinkage void __trap_section do_trap_load_misaligned(struct pt_regs *regs) {if (!handle_misaligned_load (regs))return ;@@ -119,7 +124,7 @@ asmlinkage void do_trap_load_misaligned(struct pt_regs *regs) "Oops - load address misaligned"); } -asmlinkage void do_trap_store_misaligned(struct pt_regs *regs) +asmlinkage void __trap_section do_trap_store_misaligned(struct pt_regs *regs) {if (!handle_misaligned_store (regs))return ;@@ -146,7 +151,7 @@ static inline unsigned long get_break_insn_length(unsigned long pc)return GET_INSN_LENGTH (insn); } -asmlinkage __visible void do_trap_break(struct pt_regs *regs) +asmlinkage __visible __trap_section void do_trap_break(struct pt_regs *regs) {#ifdef CONFIG_KPROBES if (kprobe_single_step_handler (regs))arch/riscv/kernel/vmlinux-xip.lds.S +0
-0@@ -99,9 +99,22 @@ SECTIONS } PERCPU_SECTION(L1_CACHE_BYTES)
arch/riscv/kernel/vmlinux-xip.lds.S SECTIONS · line 110
Quoted 10 lines
arch/riscv/kernel/vmlinux-xip.lds.S +7
-1- . = ALIGN(PAGE_SIZE); + . = ALIGN(8); + .alternative : { + __alt_start = .; + *(.alternative) + __alt_end = .; + } __init_end = .; + . = ALIGN(16);
Why 16 here?
arch/riscv/kernel/vmlinux-xip.lds.S SECTIONS · line 120
Quoted 11 lines
arch/riscv/kernel/vmlinux-xip.lds.S +7
-0+ .xip.traps : { + __xip_traps_start = .; + *(.xip.traps) + __xip_traps_end = .; + } + + . = ALIGN(PAGE_SIZE); .sdata : { __global_pointer$ = . + 0x800; *(.sdata*)
Thanks,
Alex
_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
Quoted 12 lines · Alex Ghiti
Hi Vitaly,
Quoted 7 lines · Vitaly Wool
RISCV_ERRATA_ALTERNATIVEpatches text at runtime which is currently not possible when the kernel is executed from the flash in XIP mode. Since runtime patching concerns only traps at the moment, let's just have all the traps reside in RAM anyway ifRISCV_ERRATA_ALTERNATIVEis set. Thus, these functions will be patch-able even when the .text section is in flash.
Quoted 2 lines
This sounds like a good fix for sifive platforms to work with XIP kernel in 5.13: did you test that it actually works on HW?
I don't have any SiFive HW so strictly speaking, not quite. However, I created a test configuration for Polarfire with a basically no-op runtime patching and it apparently worked. Traps end up in RAM and are changeable.
Quoted 74 lines
Quoted 11 lines
Signed-off-by Vitaly Wool <[email protected]>--- arch/riscv/kernel/traps.c | 13 +++++++++---- arch/riscv/kernel/vmlinux-xip.lds.S | 15 ++++++++++++++- 2 files changed, 23 insertions(+), 5 deletions(-)arch/riscv/kernel/traps.c +0
-0arch/riscv/kernel/traps.c static void do_trap_error(struct pt_regs *regs, int signo, int code, · line 93
Quoted 8 lines
arch/riscv/kernel/traps.c +5
-0@@ -86,8 +86,13 @@ static void do_trap_error(struct pt_regs *regs, int signo, int code, } } +#if defined (CONFIG_XIP_KERNEL ) && defined (CONFIG_RISCV_ERRATA_ALTERNATIVE ) +#define __trap_section __section(".xip.traps") +#else +#define __trap_section +#endif Maybe we could do something more generic. At the moment, only traps are subject to alternatives but that will likely expand: what about rather defining a section called
__alternative_section?Quoted 50 lines
arch/riscv/kernel/traps.c +4
-4#define DO_ERROR_INFO (name, signo, code, str) \ -asmlinkage __visible void name(struct pt_regs *regs) \ +asmlinkage __visible __trap_section void name(struct pt_regs *regs) \ { \ do_trap_error(regs, signo, code, regs->epc, "Oops - " str); \ }@@ -111,7 +116,7 @@ DO_ERROR_INFO(do_trap_store_misaligned,int handle_misaligned_load (struct pt_regs *regs);int handle_misaligned_store (struct pt_regs *regs); -asmlinkage void do_trap_load_misaligned(struct pt_regs *regs) +asmlinkage void __trap_section do_trap_load_misaligned(struct pt_regs *regs) {if (!handle_misaligned_load (regs))return ;@@ -119,7 +124,7 @@ asmlinkage void do_trap_load_misaligned(struct pt_regs *regs) "Oops - load address misaligned"); } -asmlinkage void do_trap_store_misaligned(struct pt_regs *regs) +asmlinkage void __trap_section do_trap_store_misaligned(struct pt_regs *regs) {if (!handle_misaligned_store (regs))return ;@@ -146,7 +151,7 @@ static inline unsigned long get_break_insn_length(unsigned long pc)return GET_INSN_LENGTH (insn); } -asmlinkage __visible void do_trap_break(struct pt_regs *regs) +asmlinkage __visible __trap_section void do_trap_break(struct pt_regs *regs) {#ifdef CONFIG_KPROBES if (kprobe_single_step_handler (regs))arch/riscv/kernel/vmlinux-xip.lds.S +6
-1@@ -99,9 +99,22 @@ SECTIONS } PERCPU_SECTION(L1_CACHE_BYTES) - . = ALIGN(PAGE_SIZE); + . = ALIGN(8); + .alternative : { + __alt_start = .; + *(.alternative) + __alt_end = .; + } __init_end = .;
Quoted 3 lines
arch/riscv/kernel/vmlinux-xip.lds.S SECTIONS · line 110
Quoted 1 line
arch/riscv/kernel/vmlinux-xip.lds.S +1
-0+ . = ALIGN(16);Why 16 here?
Honestly, I don't remember. I believe it should have been 8 but didn't bother to check, thought it was not a big deal anyway :)
Best regards,
Vitaly
Quoted 15 lines
arch/riscv/kernel/vmlinux-xip.lds.S SECTIONS · line 120
Quoted 11 lines
arch/riscv/kernel/vmlinux-xip.lds.S +7
-0+ .xip.traps : { + __xip_traps_start = .; + *(.xip.traps) + __xip_traps_end = .; + } + + . = ALIGN(PAGE_SIZE); .sdata : { __global_pointer$ = . + 0x800; *(.sdata*)Thanks,
Alex
Alex Ghiti
reviewerLe 31/05/2021 à 17:17, Alex Ghiti a écrit :
Quoted 19 lines
Hi Vitaly,
Le 31/05/2021 à 10:53, Vitaly Wool a écrit :
Quoted 7 lines
RISCV_ERRATA_ALTERNATIVEpatches text at runtime which is currently not possible when the kernel is executed from the flash in XIP mode. Since runtime patching concerns only traps at the moment, let's just have all the traps reside in RAM anyway ifRISCV_ERRATA_ALTERNATIVEis set. Thus, these functions will be patch-able even when the .text section is in flash.This sounds like a good fix for sifive platforms to work with XIP kernel in 5.13: did you test that it actually works on HW?
Quoted 6 lines
Signed-off-by Vitaly Wool <[email protected]>---
arch/riscv/kernel/traps.c | 13 +++++++++----arch/riscv/kernel/vmlinux-xip.lds.S| 15 ++++++++++++++-2 files changed, 23 insertions(+), 5 deletions(-)
Quoted 16 lines
Quoted 5 lines
arch/riscv/kernel/traps.c +0
-0arch/riscv/kernel/traps.c static void do_trap_error(struct pt_regs *regs, int · line 94
Quoted 9 lines
arch/riscv/kernel/traps.c +5
-0@@ -86,8 +86,13 @@ static void do_trap_error(struct pt_regs *regs, int signo, int code, } } +#if defined (CONFIG_XIP_KERNEL ) && defined (CONFIG_RISCV_ERRATA_ALTERNATIVE ) +#define __trap_section __section(".xip.traps") +#else +#define __trap_section +#endif Maybe we could do something more generic. At the moment, only traps are subject to alternatives but that will likely expand: what about rather defining a section called
__alternative_section?
Any thoughts about that?
Thanks,
Alex
Quoted 75 lines
Quoted 42 lines
arch/riscv/kernel/traps.c +1
-1#define DO_ERROR_INFO(name, signo, code, str) \ -asmlinkage __visible void name(struct pt_regs *regs) \ +asmlinkage __visible __trap_section void name(struct pt_regs *regs) \ { \ do_trap_error(regs, signo, code, regs->epc, "Oops - " str); \}
arch/riscv/kernel/traps.c +2
-2@@ -111,7 +116,7 @@ DO_ERROR_INFO(do_trap_store_misaligned, int handle_misaligned_load(struct pt_regs *regs); int handle_misaligned_store(struct pt_regs *regs); -asmlinkage void do_trap_load_misaligned(struct pt_regs *regs) +asmlinkage void __trap_section do_trap_load_misaligned(struct pt_regs *regs) { if (!handle_misaligned_load(regs)) return;@@ -119,7 +124,7 @@ asmlinkage void do_trap_load_misaligned(struct pt_regs *regs) "Oops - load address misaligned"); } -asmlinkage void do_trap_store_misaligned(struct pt_regs *regs) +asmlinkage void __trap_section do_trap_store_misaligned(structpt_regs *regs) { if (!handle_misaligned_store(regs))return;
arch/riscv/kernel/traps.c +1
-1@@ -146,7 +151,7 @@ static inline unsigned longget_break_insn_length (unsigned long pc ) return GET_INSN_LENGTH(insn); } -asmlinkage __visible void do_trap_break(struct pt_regs *regs) +asmlinkage __visible __trap_section void do_trap_break(struct pt_regs *regs) { #ifdef CONFIG_KPROBESif (
kprobe_single_step_handler(regs))arch/riscv/kernel/vmlinux-xip.lds.S +0
-0@@ -99,9 +99,22 @@ SECTIONS }arch/riscv/kernel/vmlinux-xip.lds.S SECTIONS · line 108
Quoted 10 lines
arch/riscv/kernel/vmlinux-xip.lds.S +7
-1PERCPU_SECTION(L1_CACHE_BYTES) - . = ALIGN(PAGE_SIZE); + . = ALIGN(8); + .alternative : { + __alt_start = .; + *(.alternative) + __alt_end = .; + } __init_end = .; + . = ALIGN(16);Why 16 here?
arch/riscv/kernel/vmlinux-xip.lds.S SECTIONS
Quoted 11 lines
arch/riscv/kernel/vmlinux-xip.lds.S +7
-0+ .xip.traps : { + __xip_traps_start = .; + *(.xip.traps) + __xip_traps_end = .; + } + + . = ALIGN(PAGE_SIZE); .sdata : { __global_pointer$ = . + 0x800; *(.sdata*)Thanks,
Alex
_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
Hi Alex,
Quoted 41 lines · Alex Ghiti
Quoted 30 lines · Alex Ghiti
Hi Vitaly,
Quoted 7 lines · Vitaly Wool
RISCV_ERRATA_ALTERNATIVEpatches text at runtime which is currently not possible when the kernel is executed from the flash in XIP mode. Since runtime patching concerns only traps at the moment, let's just have all the traps reside in RAM anyway ifRISCV_ERRATA_ALTERNATIVEis set. Thus, these functions will be patch-able even when the .text section is in flash.This sounds like a good fix for sifive platforms to work with XIP kernel in 5.13: did you test that it actually works on HW?
Quoted 16 lines
Signed-off-by Vitaly Wool <[email protected]>--- arch/riscv/kernel/traps.c | 13 +++++++++---- arch/riscv/kernel/vmlinux-xip.lds.S | 15 ++++++++++++++- 2 files changed, 23 insertions(+), 5 deletions(-)arch/riscv/kernel/traps.c +1
-0@@ -86,8 +86,13 @@ static void do_trap_error(struct pt_regs *regs, int signo, int code, } } +#if defined (CONFIG_XIP_KERNEL ) && defined (CONFIG_RISCV_ERRATA_ALTERNATIVE )Quoted 8 lines
arch/riscv/kernel/traps.c static void do_trap_error(struct pt_regs *regs, int signo, int code, · line 93
Quoted 4 lines
arch/riscv/kernel/traps.c +4
-0+#define __trap_section __section(".xip.traps") +#else +#define __trap_section +#endif Maybe we could do something more generic. At the moment, only traps are subject to alternatives but that will likely expand: what about rather defining a section called
__alternative_section?
Quoted 1 line
Any thoughts about that?
Oh sorry, I have overlooked this. I do agree this is the way to go long term, I just wanted to do a reasonably good quick fix since the default SiFive configuration won't even build with CONFIG_XIP enabled. So I would probably just suggest to take the existing solution and then think over the details (e. g. should we just use one "alternative section" for both the functions and the table or have separate ones?) for the generic solution.
Best regards,
Vitaly
Quoted 80 lines
Thanks,
Alex
Quoted 76 lines
Quoted 43 lines
arch/riscv/kernel/traps.c +1
-1#define DO_ERROR_INFO (name, signo, code, str) \ -asmlinkage __visible void name(struct pt_regs *regs) \ +asmlinkage __visible __trap_section void name(struct pt_regs *regs) \ { \ do_trap_error(regs, signo, code, regs->epc, "Oops - " str); \}arch/riscv/kernel/traps.c +2
-2@@ -111,7 +116,7 @@ DO_ERROR_INFO(do_trap_store_misaligned,int handle_misaligned_load (struct pt_regs *regs);int handle_misaligned_store (struct pt_regs *regs); -asmlinkage void do_trap_load_misaligned(struct pt_regs *regs) +asmlinkage void __trap_section do_trap_load_misaligned(struct pt_regs *regs) {if (!handle_misaligned_load (regs))return ;@@ -119,7 +124,7 @@ asmlinkage void do_trap_load_misaligned(struct pt_regs *regs) "Oops - load address misaligned"); } -asmlinkage void do_trap_store_misaligned(struct pt_regs *regs) +asmlinkage void __trap_section do_trap_store_misaligned(structpt_regs *regs) {if (!handle_misaligned_store (regs))return;arch/riscv/kernel/traps.c +1
-1@@ -146,7 +151,7 @@ static inline unsigned longget_break_insn_length (unsigned long pc ) return GET_INSN_LENGTH(insn); } -asmlinkage __visible void do_trap_break(struct pt_regs *regs) +asmlinkage __visible __trap_section void do_trap_break(struct pt_regs *regs) {#ifdef CONFIG_KPROBES if (
kprobe_single_step_handler(regs))arch/riscv/kernel/vmlinux-xip.lds.S +0
-0@@ -99,9 +99,22 @@ SECTIONS }arch/riscv/kernel/vmlinux-xip.lds.S SECTIONS · line 108
Quoted 10 lines
arch/riscv/kernel/vmlinux-xip.lds.S +7
-1PERCPU_SECTION(L1_CACHE_BYTES) - . = ALIGN(PAGE_SIZE); + . = ALIGN(8); + .alternative : { + __alt_start = .; + *(.alternative) + __alt_end = .; + } __init_end = .; + . = ALIGN(16);Why 16 here?
arch/riscv/kernel/vmlinux-xip.lds.S SECTIONS
Quoted 11 lines
arch/riscv/kernel/vmlinux-xip.lds.S +7
-0+ .xip.traps : { + __xip_traps_start = .; + *(.xip.traps) + __xip_traps_end = .; + } + + . = ALIGN(PAGE_SIZE); .sdata : { __global_pointer$ = . + 0x800; *(.sdata*)Thanks,
Alex
_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
Palmer Dabbelt
maintainerQuoted 46 lines · On Fri, 04 Jun 2021 02:52:40 PDT (-0700), [email protected] wrote:
Hi Alex,
Quoted 41 lines · Alex Ghiti
Quoted 30 lines · Alex Ghiti
Hi Vitaly,
Quoted 7 lines · Vitaly Wool
RISCV_ERRATA_ALTERNATIVEpatches text at runtime which is currently not possible when the kernel is executed from the flash in XIP mode. Since runtime patching concerns only traps at the moment, let's just have all the traps reside in RAM anyway ifRISCV_ERRATA_ALTERNATIVEis set. Thus, these functions will be patch-able even when the .text section is in flash.This sounds like a good fix for sifive platforms to work with XIP kernel in 5.13: did you test that it actually works on HW?
Quoted 16 lines
Signed-off-by Vitaly Wool <[email protected]>--- arch/riscv/kernel/traps.c | 13 +++++++++---- arch/riscv/kernel/vmlinux-xip.lds.S | 15 ++++++++++++++- 2 files changed, 23 insertions(+), 5 deletions(-)arch/riscv/kernel/traps.c +1
-0@@ -86,8 +86,13 @@ static void do_trap_error(struct pt_regs *regs, int signo, int code, } } +#if defined (CONFIG_XIP_KERNEL ) && defined (CONFIG_RISCV_ERRATA_ALTERNATIVE )Quoted 8 lines
arch/riscv/kernel/traps.c static void do_trap_error(struct pt_regs *regs, int signo, int code, · line 93
Quoted 4 lines
arch/riscv/kernel/traps.c +4
-0+#define __trap_section __section(".xip.traps") +#else +#define __trap_section +#endif Maybe we could do something more generic. At the moment, only traps are subject to alternatives but that will likely expand: what about rather defining a section called
__alternative_section?Quoted 1 line
Any thoughts about that?
Quoted 7 lines
Oh sorry, I have overlooked this. I do agree this is the way to go long term, I just wanted to do a reasonably good quick fix since the default SiFive configuration won't even build with
CONFIG_XIPenabled. So I would probably just suggest to take the existing solution and then think over the details (e. g. should we just use one "alternative section" for both the functions and the table or have separate ones?) for the generic solution.
I've taken this on fixes. It's fixing a bug and should be pretty safe, since it only has any effect on XIP kernels.