aboutsummaryrefslogtreecommitdiff
path: root/src/kern/priv.c
blob: 6698350e6737c8a6f178904c6cd78d5488a4a646 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include "kern/priv.h"

#include "arch.h"
#include "kern/log.h"
#include "kern/mem.h"

#ifdef ARCH_STM32L4
void set_control_register(uint32_t reg)
{
  asm volatile("msr control, %0" : "=r"(reg) :);
}

uint32_t get_control_register()
{
  uint32_t control;
  asm volatile("mrs %0, control" : "=r"(control) :);
  return control;
}

void enter_user_mode()
{
  asm volatile(
      "mov r0, #1\n\t"
      "msr control, r0\n\t");
}

void jump_to_user_mode()
{
  void* new_stack = kalloc(4096);
  new_stack += 4096;

  asm volatile(
      "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));
}
#endif