Skip to content

Commit

Permalink
Added Semihosting call to exit with an arbitrary exit code, this should
Browse files Browse the repository at this point in the history
make panic exit codes more obvious.
  • Loading branch information
Victor Roest committed Mar 8, 2020
1 parent be99ff8 commit 82c027e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
12 changes: 7 additions & 5 deletions kernel/src/common/include/interrupt.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,19 @@ enum SemihostingSWI {
BreakPoint = 0x20020,
WatchPoint = 0x20021,
StepComplete = 0x20022,
RunTimeErrorUnknown = 0x20023,
InternalError = 0x20024,
UserInterruption = 0x20025,
RunTimeErrorUnknown = 0x20023, // Qemu exits with 1
InternalError = 0x20024, // Qemu exits with 1
UserInterruption = 0x20025, // Qemu exits with 1
ApplicationExit = 0x20026, // Qemu exits with 0
StackOverflow = 0x20027,
DivisionByZero = 0x20028,
StackOverflow = 0x20027, // Qemu exits with 1
DivisionByZero = 0x20028, // Qemu exits with 1
OSSpecific = 0x20029, // Qemu exits with 1
};

void SemihostingCall(enum SemihostingSWI mode);

void SemihostingOSExit(int code) __attribute__ ((noreturn));;

typedef enum {
IRQ, // (this is bit 0x8 on the CPSR)
FIQ, // (this is bit 0x4 on the CPSR)
Expand Down
23 changes: 23 additions & 0 deletions kernel/src/common/interrupt.c
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,29 @@ void __attribute__((always_inline)) inline SemihostingCall(enum SemihostingSWI m
);
}


/// Uses the ExtendedExit Semihosting call
/// ARM Docs: https://developer.arm.com/docs/100863/0200/semihosting-operations/sys_exit_extended-0x20
void __attribute__((always_inline)) inline SemihostingOSExit(int code) {
struct {
uint32_t field1;
uint32_t field2;
} parameters;

parameters.field1 = ApplicationExit;
parameters.field2 = code;

asm volatile (
"MOV r0, #0x20\n"
"mov r1, %[in0]\n"
"svc 0x00123456\n"
::
[in0] "r" (&parameters)
);

__builtin_unreachable();
}

/* enable IRQ and/or FIQ */
void enable_interrupt(InterruptType mask) {
INFO("Enabling interrupts with mask %i", mask);
Expand Down
6 changes: 3 additions & 3 deletions kernel/src/klibc/klibc.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ void panic() {
kprintf("|_ ((_)((_|()\\ )\\ ) /((_) (_)) )(_)) )\\ |(_) )\\ \n");
kprintf("| |/ (_)) ((_)_(_/((_))| | | _ ((_)_ _(_/((_)((_) \n");
kprintf(" ' </ -_)| '_| ' \\)) -_) | | _/ _` | ' \\)) / _| \n");
kprintf(" _|\\_\\___||_| |_||_|\\___|_| |_| \\__,_|_||_||_\\__| ");
SemihostingCall(OSSpecific);
kprintf(" _|\\_\\___||_| |_||_|\\___|_| |_| \\__,_|_||_||_\\__|\n");

SemihostingOSExit(-1);
SLEEP;
__builtin_unreachable();
}

void splash() {
Expand Down

0 comments on commit 82c027e

Please sign in to comment.