diff options
author | Josh Rahm <joshuarahm@gmail.com> | 2020-11-25 16:53:24 -0700 |
---|---|---|
committer | Josh Rahm <joshuarahm@gmail.com> | 2020-11-25 16:53:24 -0700 |
commit | ee89199793683b3120a55f1c1887e12333c5ea7e (patch) | |
tree | 2996b7645b6ab727bd9c6ee7f7062626d3ff7023 | |
parent | 6d22b0dfc7761a605758552d5824f8039ac5a00f (diff) | |
download | stm32l4-ee89199793683b3120a55f1c1887e12333c5ea7e.tar.gz stm32l4-ee89199793683b3120a55f1c1887e12333c5ea7e.tar.bz2 stm32l4-ee89199793683b3120a55f1c1887e12333c5ea7e.zip |
Add ability to jump to a usermode init routine.
This routine will has a newly allocated stack.
I found out that when using the st-flash utility it likes to reset the
device with the IPSR in HARD FAULT mode (?) so I have to manually hit
the reset button to get it to work.
-rw-r--r-- | include/user/init.h | 6 | ||||
-rw-r--r-- | src/kern/main.c | 14 | ||||
-rw-r--r-- | src/kern/priv.c | 14 | ||||
-rw-r--r-- | src/user/init.c | 11 |
4 files changed, 30 insertions, 15 deletions
diff --git a/include/user/init.h b/include/user/init.h new file mode 100644 index 0000000..a55d5d1 --- /dev/null +++ b/include/user/init.h @@ -0,0 +1,6 @@ +#ifndef USER_INIT_H_ +#define USER_INIT_H_ + +void usermode_start(); + +#endif diff --git a/src/kern/main.c b/src/kern/main.c index 3af8beb..4fddb0a 100644 --- a/src/kern/main.c +++ b/src/kern/main.c @@ -35,19 +35,7 @@ void configure_mpu() int main() { configure_mpu(); - - klogf("Outside of usermode, I can still log stuff using klogf()\n"); - - enter_user_mode(); - - logs("Now that I'm in user mode, I have to log stuff using a system call\n"); - logs( - "because I no longer have direct accss to USART2 and cannot use\n" - "klogf()\n"); - - - for (;;) - ; + jump_to_user_mode(); } #endif diff --git a/src/kern/priv.c b/src/kern/priv.c index 3162639..2c45eca 100644 --- a/src/kern/priv.c +++ b/src/kern/priv.c @@ -2,6 +2,7 @@ #include "arch.h" #include "kern/log.h" +#include "kern/mem.h" void set_control_register(uint32_t reg) { @@ -24,7 +25,16 @@ void enter_user_mode() void jump_to_user_mode() { + void* new_stack = kalloc(4096); + new_stack += 4096; + asm volatile( - "mov r0, #1\n\t" - "msr control, r0\n\t"); + "mov r0, %0\n\t" + "msr psp, r0\n\t" + "mrs r0, control\n\t" + "orrs r0, r0, #3\n\t" + "msr control, r0\n\t" + "isb\n\t" + "dsb\n\t" + "b usermode_start\n\t" : : "r"(new_stack)); } diff --git a/src/user/init.c b/src/user/init.c new file mode 100644 index 0000000..00f1b28 --- /dev/null +++ b/src/user/init.c @@ -0,0 +1,11 @@ +#include "user/syscall.h" +#include "kern/log.h" +#include "kern/common.h" + +void usermode_start() +{ + logs("I'm in usermode!\n"); + logs("I'm still in usermode!\n"); + logs("I'm super still in usermode!\n"); + for(;;); +} |