ARM: introduce sjlj structured exception handling
cover ARM: introduce sjlj structured exception handling
We are already using data_abort_mask at a hand full of places to integrate Hardware exceptions with normal flow control mechanisms.
While cumbersome to use, these few code blocks have a terrific track record in stability, having so far never crashed due to NULL pointer exceptions.
Having something like this everywhere in barebox would have been great for software stability[1][2][3][4][5] in the past.
This patch takes this idea a bit further, by introducing new TRY/CATCH primitives, inspired by Microsoft Visual C Structured Exception Handling (SEH), into barebox. These can now be wielded to beat any and all error conditions into total and utter submission:
u32 cycles;
TRY {
u32 div = readl(NULL);
cycles = ticks / div;
cycles = ((void (*)())cycles)();
if (cycles == 0)
THROW(RuntimeError);
} CATCH_3(DataAbortException, UndefinedInstructionException, RuntimerError) {
cycles = 42;
}
ENDTRY;As a first step, this patch series is introduced as a RFC for ARM. Other platforms can follow when setjmp/longjump (sjlj) is implemented for them. This will allow all barebox hackers to benefit from state-of-the-art tooling in building the same highly robust and crash resistant software that has been possible since SEH's introduction with Windows 95.
This is of course not only limited to hardware faults, as the example above shows.
By blurring the line between hardware fault handling and software error propagation, it also allows a more natural control flow than having to deal with arcane UNIX signals running in restricted contexts or *shudder* check against NULL before using a pointer.
Hoping you are as hyped as I am.
[1]: dc5100e6b ("state: backend_storage: deal
gracefully with runtime bucket corruption")
[2]: 09d11c546 ("video/ssd1307fb: fix NULL pointer dereference in probe") [3]: 671a7d5df ("driver: bail out, don't crash, if drv->name is not set") [4]: 8a774977b ("mci: dove: fix dereference of nullable pointer") [5]: bb414a64b ("drivers: video: Fix parsing oftree timings")
Cheers,
Ahmad (4):
ARM: implement sjlj-based TRY/CATCH exception handling
startup: wrap barebox startup in TRY/CATCH
ARM: rethrow CPU exceptions as sjlj-exceptions
commands: implement except test command
arch/arm/cpu/interrupts.c | 32 +-- commands/Kconfig | 5 + commands/Makefile | 1 + commands/except.c | 130 ++++++++++++ common/Makefile | 3 + common/except.c | 282 +++++++++++++++++++++++++ common/startup.c | 70 +++++-- include/except.h | 156 ++++++++++++++ include/exceptions.h | 419 ++++++++++++++++++++++++++++++++++++++ 9 files changed, 1058 insertions(+), 40 deletions(-) create mode 100644 commands/except.c create mode 100644 common/except.c create mode 100644 include/except.h create mode 100644 include/exceptions.h
Signature · 7 lines
2.20.1 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
RFC PATCH 1/4 ARM: implement sjlj-based TRY/CATCH exception handling
Now with setjmp supported in barebox, we don't need to limit ourselves to outdated of return-value-based error propagation.
Instead, embrace the state-of-the-art and port kazlib's ANSI C exceptions to barebox.
--- common/Makefile | 3 + common/except.c | 282 +++++++++++++++++++++++++++++ include/except.h | 156 ++++++++++++++++ include/exceptions.h | 419 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 860 insertions(+) create mode 100644 common/except.c create mode 100644 include/except.h create mode 100644 include/exceptions.h
common/Makefile +3 -0
@@ -69,6 +69,9 @@ obj-$(CONFIG_SERIAL_DEV_BUS) += serdev.o obj-$(CONFIG_USBGADGET_START) += usbgadget.o pbl-$(CONFIG_PBL_OPTEE) += optee.o obj-$(CONFIG_BOOTM_OPTEE) += optee.o +obj-y += except.o + +UBSAN_SANITIZE_except.o := n ifdef CONFIG_PASSWORD
common/except.c new file +282 -0
282 lines not on this page · raw message
include/except.h new file +156 -0
156 lines not on this page · raw message
include/exceptions.h new file +419 -0
419 lines not on this page · raw message
Signature · 7 lines
2.20.1 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
Re: [RFC PATCH 1/4] ARM: implement sjlj-based TRY/CATCH exception handling
Quoted 6 lines · Ahmad Fatoum
Now with setjmp supported in barebox, we don't need to limit ourselves to outdated of return-value-based error propagation.
Instead, embrace the state-of-the-art and port kazlib's ANSI C exceptions to barebox.Reviewed-by Ahmad Fatoum <[email protected]>
Self-review …? Or supposed to be a S-o-b?
- Roland
Signature · 9 lines
Roland Hieber, Pengutronix e.K. | [email protected] | Steuerwalder Str. 21 | https://www.pengutronix.de/ | 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 | _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
Re: [RFC PATCH 1/4] ARM: implement sjlj-based TRY/CATCH exception handling
Quoted 9 lines · Roland Hieber
Quoted 6 lines · Ahmad Fatoum
Now with setjmp supported in barebox, we don't need to limit ourselves to outdated of return-value-based error propagation.
Instead, embrace the state-of-the-art and port kazlib's ANSI C exceptions to barebox.Reviewed-by Ahmad Fatoum <[email protected]>Self-review …? Or supposed to be a S-o-b?
Too humbled to claim co-authorship of such intricate beauty. This is not made, but discovered, and as such transcends the DCO...
Cheers
Ahmad
Quoted 3 lines
- Roland
Signature · 9 lines
Pengutronix e.K. | | Steuerwalder Str. 21 | http://www.pengutronix.de/ | 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 | _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
RFC PATCH 2/4 startup: wrap barebox startup in TRY/CATCH
In reply to Ahmad FatoumInstall a top level exception handler around barebox_main, so we may catch THROWs that aren't contained in a TRY { } block.
--- common/startup.c | 42 +++++++++++++++++++++++++----------------- 1 file changed, 25 insertions(+), 17 deletions(-)
common/startup.c +25 -17
@@ -348,27 +348,35 @@ void __noreturn start_barebox(void)if (!IS_ENABLED (CONFIG_SHELL_NONE ) &&IS_ENABLED (CONFIG_COMMAND_SUPPORT )) barebox_main = run_init; -for (initcall = __barebox_initcalls_start; - initcall < __barebox_initcalls_end; initcall++) { -pr_debug ("initcall-> %pS\n" , *initcall); - result = (*initcall)(); -if (result) -pr_err ("initcall %pS failed: %s\n" , *initcall, -strerror (-result)); - } + TRY { +for (initcall = __barebox_initcalls_start; + initcall < __barebox_initcalls_end; initcall++) { +pr_debug ("initcall-> %pS\n" , *initcall); + result = (*initcall)(); +if (result) +pr_err ("initcall %pS failed: %s\n" , *initcall, +strerror (-result)); + } -pr_debug ("initcalls done\n" ); +pr_debug ("initcalls done\n" ); -if (barebox_main) -barebox_main (); +if (barebox_main) +barebox_main (); -if (IS_ENABLED (CONFIG_SHELL_NONE )) { -pr_err ("Nothing left to do\n" ); -hang (); - }else { -while (1 ) -run_shell (); +if (IS_ENABLED (CONFIG_SHELL_NONE )) { +pr_err ("Nothing left to do\n" ); +hang (); + }else { +while (1 ) +run_shell (); + } + } CATCH_ALL { +panic ("Unhandled exception" ); } + + +ENDTRY ; + }void __noreturn hang (void )
Signature · 7 lines
2.20.1 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
RFC PATCH 3/4 ARM: rethrow CPU exceptions as sjlj-exceptions
In reply to Ahmad FatoumHaving to differentiate between hardware and software exceptions is confusing, especially for newcomers. Be more welcoming by just treating them the same.
--- arch/arm/cpu/interrupts.c | 32 +++++++++----------------------- common/startup.c | 32 ++++++++++++++++++++++++++++++-- 2 files changed, 39 insertions(+), 25 deletions(-)
arch/arm/cpu/interrupts.c +9 -23
@@ -27,6 +27,7 @@#include <asm/ptrace.h> #include <asm/unwind.h> #include <init.h> +#include "exceptions.h" /* Avoid missing prototype warning, called from assembly */ void do_undefined_instruction (struct pt_regs *pt_regs);
@@ -80,11 +81,9 @@ void show_regs (struct pt_regs *regs)#endif } -static void __noreturn do_exception (struct pt_regs *pt_regs) +static void __noreturn do_exception (int exception,struct pt_regs *pt_regs) { -show_regs (pt_regs); - -panic ("" ); +THROW_DATA (exception,NULL ,XCEPT_DATA_NONDYN (pt_regs)); }/**
@@ -93,8 +92,7 @@ static void __noreturn do_exception(struct pt_regs *pt_regs)*/ void do_undefined_instruction (struct pt_regs *pt_regs) { -printf ("undefined instruction\n" ); -do_exception (pt_regs); +do_exception (UndefinedInstructionException, pt_regs); }/**
@@ -106,8 +104,7 @@ void do_undefined_instruction (struct pt_regs *pt_regs)*/ void do_software_interrupt (struct pt_regs *pt_regs) { -printf ("software interrupt\n" ); -do_exception (pt_regs); +do_exception (SoftwareInterruptException, pt_regs); }/**
@@ -118,8 +115,7 @@ void do_software_interrupt (struct pt_regs *pt_regs)*/ void do_prefetch_abort (struct pt_regs *pt_regs) { -printf ("prefetch abort\n" ); -do_exception (pt_regs); +do_exception (PrefetchAbortException, pt_regs); }/**
@@ -130,15 +126,7 @@ void do_prefetch_abort (struct pt_regs *pt_regs)*/ void do_data_abort (struct pt_regs *pt_regs) { -u32 far; - - asmvolatile ("mrc p15, 0, %0, c6, c0, 0" :"=r" (far) : :"cc" ); - -printf ("unable to handle %s at address 0x%08x\n" , - far <PAGE_SIZE ?"NULL pointer dereference" : -"paging request" , far); - -do_exception (pt_regs); +do_exception (DataAbortException, pt_regs); }/**
@@ -149,8 +137,7 @@ void do_data_abort (struct pt_regs *pt_regs)*/ void do_fiq (struct pt_regs *pt_regs) { -printf ("fast interrupt request\n" ); -do_exception (pt_regs); +do_exception (FiqException, pt_regs); }/**
@@ -161,8 +148,7 @@ void do_fiq (struct pt_regs *pt_regs)*/ void do_irq (struct pt_regs *pt_regs) { -printf ("interrupt request\n" ); -do_exception (pt_regs); +do_exception (IrqException, pt_regs); }extern volatile int arm_ignore_data_abort;
common/startup.c +30 -2
@@ -45,6 +45,8 @@#include <console_countdown.h> #include <environment.h> #include <linux/ctype.h> +#include <asm/unwind.h> +#include <exceptions.h> extern initcall_t __barebox_initcalls_start[], __barebox_early_initcalls_end[], __barebox_initcalls_end[];
@@ -370,13 +372,39 @@ void __noreturn start_barebox(void)while (1 )run_shell (); } - } CATCH_ALL { -panic ("Unhandled exception" ); + } CATCH_ERRORS { +unwind_backtrace (GET_DATA ); + }CATCH (UndefinedInstructionException) { +printf ("undefined instruction\n" ); +show_regs (GET_DATA ); + }CATCH (SoftwareInterruptException) { +printf ("software interrupt\n" ); +show_regs (GET_DATA ); + }CATCH (PrefetchAbortException) { +printf ("prefetch abort\n" ); +show_regs (GET_DATA ); + }CATCH (DataAbortException) { +u32 far; + + asmvolatile ("mrc p15, 0, %0, c6, c0, 0" :"=r" (far) : :"cc" ); + +printf ("unable to handle %s at address 0x%08x\n" , + far <PAGE_SIZE ?"NULL pointer dereference" : +"paging request" , far); + +show_regs ((void *)((unsigned long )GET_DATA & ~1U )); + }CATCH (FiqException) { +printf ("fast interrupt request\n" ); +show_regs (GET_DATA ); + }CATCH (IrqException) { +printf ("interrupt request\n" ); +show_regs (GET_DATA ); }ENDTRY ; +panic ("Unhandled exception" ); }void __noreturn hang (void )
Signature · 7 lines
2.20.1 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
RFC PATCH 4/4 commands: implement except test command
In reply to Ahmad FatoumThis command is not meant for production, but is a useful tool to test software and hardware exception handling in the works, especially when porting to new platforms.
--- commands/Kconfig | 5 ++ commands/Makefile | 1 + commands/except.c | 130 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 136 insertions(+) create mode 100644 commands/except.c
commands/Kconfig +5 -0
@@ -2196,6 +2196,11 @@ config CMD_UBSAN This is a test command for the undefined behavior sanitizer. It triggers various undefined behavior, and detect it. +config CMD_EXCEPT + tristate "except" + help + This is a test command for the barebox exceptions facility. + # end Miscellaneous commands endmenu
commands/Makefile +1 -0
@@ -128,5 +128,6 @@ obj-$(CONFIG_CMD_NAND_BITFLIP) += nand-bitflip.o obj-$(CONFIG_CMD_SEED) += seed.o obj-$(CONFIG_CMD_IP_ROUTE_GET) += ip-route-get.o obj-$(CONFIG_CMD_UBSAN) += ubsan.o +obj-$(CONFIG_CMD_EXCEPT) += except.o UBSAN_SANITIZE_ubsan.o := y
commands/except.c new file +130 -0
130 lines not on this page · raw message
Signature · 7 lines
2.20.1 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox