diff --git a/assembleur.s b/assembleur.s index 1ac43c8ac35df98d5e24ed0091b80a54c428a9be..b17deb7842d58447b843b2debf0349d7a67e3989 100644 --- a/assembleur.s +++ b/assembleur.s @@ -6,12 +6,13 @@ #include "globals.h" .global asm_test_fault +.global reset_user_stack .global switch_to_user_mode .global switch_to_supervisor_mode .global change_ret .extern user_stack DATA // this adress can be read like: ldr Rx, =user_stack -switch_to_user_mode: +reset_user_stack: ldr r0, =user_stack add r0, r0, #8192 // 2048 * word_size msr PSP, r0 @@ -20,9 +21,16 @@ switch_to_user_mode: isb bx lr +switch_to_user_mode: + mrs r0, control + mov r0, #3 + msr control, r0 + isb + bx lr + switch_to_supervisor_mode: mrs r0, control - mov r0, #2 + mov r0, #0 msr control, r0 isb bx lr diff --git a/mpu_user_console_etu.c b/mpu_user_console_etu.c index 3208a71008972c274d32e97d344ed2260aa54b0b..a5806ea0f90bc6aa4a854f7600cf9e9ccc5b16ef 100644 --- a/mpu_user_console_etu.c +++ b/mpu_user_console_etu.c @@ -36,6 +36,7 @@ unsigned arr_err_code[BUF_SIZE] = { 0 }; volatile static unsigned idx_error; extern bool is_supervisor_mode; +void reset_user_stack(); void switch_to_user_mode(); void asm_test_fault(); void change_ret(void *addr); @@ -61,13 +62,15 @@ void test_supervisor_mode() { a = *p_out_of_mem; // not OK (out of regions) } -void svc_call(void) { - char buff[64]; - printf("Current: %s\nType 'switch' to toggle between USER and SUPERVISOR: ", is_supervisor_mode?"Supervisor":"User"); +void permute_mode(void) { + char buff[64] = { 0 }; + printf("Current: %s\nType 'switch' to toggle between USER and SUPERVISOR: ", + is_supervisor_mode ? "Supervisor" : "User"); fflush(stdin); fscanf(stdin, "%s", buff); - if (strncmp(buff, "switch", 64) == 0){ + char cmd[] = "switch"; + if (strncmp(buff, cmd, strlen(cmd)) == 0) { if (!is_supervisor_mode) { // If user mode __asm volatile ("svc 0x32"); } else { // If supervisor @@ -75,8 +78,7 @@ void svc_call(void) { } is_supervisor_mode = !is_supervisor_mode; } - printf("Current: %s\n", is_supervisor_mode?"Supervisor":"User"); - + printf("Current: %s\n", is_supervisor_mode ? "Supervisor" : "User"); } void test_user_mode() { @@ -100,7 +102,6 @@ int main(void) { MPU->RBAR = 0x00000000; MPU->RASR = RO | BTEX_NORMAL_NOT_SHAREABLE | SET_SIZE_512KB | REGION_ENABLE; - // Region 1 (SRAM1) MPU->RNR = 1; MPU->RBAR = 0x10000000; @@ -137,13 +138,13 @@ int main(void) { user_start: user_stating_address = &&user_start;// save the address of the label 'user_start' (for exercise 2) - switch_to_user_mode(); // to be implemented + reset_user_stack(); // to be implemented // testing memory accesses in user mode: //test_user_mode(); // to be removed after checking while (1) { - svc_call(); + permute_mode(); exec_user_read_write(); }