Skip to content
Snippets Groups Projects
Commit 259f274a authored by iliya's avatar iliya
Browse files

feat: returning from permute mode as user

parent 68308105
Branches
No related tags found
No related merge requests found
......@@ -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
......
......@@ -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();
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment