aboutsummaryrefslogtreecommitdiff
path: root/pk/minit.c
blob: a040e2bd74338de80f1fcc04a7c94093cd037c9a (plain)
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include "vm.h"
#include "mtrap.h"

uintptr_t mem_size;
uint32_t num_harts;
uint32_t num_harts_booted = 1;

static void mstatus_init()
{
  if (!supports_extension('S'))
    panic("supervisor support is required");

  uintptr_t ms = 0;
  ms = INSERT_FIELD(ms, MSTATUS_PRV, PRV_M);
  ms = INSERT_FIELD(ms, MSTATUS_PRV1, PRV_S);
  ms = INSERT_FIELD(ms, MSTATUS_PRV2, PRV_U);
  ms = INSERT_FIELD(ms, MSTATUS_IE2, 1);
  ms = INSERT_FIELD(ms, MSTATUS_VM, VM_CHOICE);
  ms = INSERT_FIELD(ms, MSTATUS_FS, 3);
  ms = INSERT_FIELD(ms, MSTATUS_XS, 3);
  write_csr(mstatus, ms);
  ms = read_csr(mstatus);

  if (EXTRACT_FIELD(ms, MSTATUS_VM) != VM_CHOICE)
    have_vm = 0;

  clear_csr(mip, MIP_MSIP);
  set_csr(mie, MIP_MSIP);
}

static void memory_init()
{
  if (mem_size == 0)
    panic("could not determine memory capacity");

  if (num_harts == 0)
    panic("could not determine number of harts");
}

static void fp_init()
{
  kassert(read_csr(mstatus) & MSTATUS_FS);

#ifdef __riscv_hard_float
  if (!supports_extension('D'))
    panic("FPU not found; recompile pk with -msoft-float");
  for (int i = 0; i < 32; i++)
    init_fp_reg(i);
#else
  if (supports_extension('D'))
    panic("FPU unexpectedly found; recompile pk without -msoft-float");
#endif
}

static void hls_init(uint32_t hart_id)
{
  memset(HLS(), 0, sizeof(*HLS()));
  HLS()->hart_id = hart_id;
}

static void init_first_hart()
{
  file_init();

  struct mainvars arg_buffer;
  struct mainvars *args = parse_args(&arg_buffer);

  memory_init();
  vm_init();
  boot_loader(args);
}

static void init_other_hart()
{
  // wait until virtual memory is enabled
  while (root_page_table == NULL)
    asm volatile ("" ::: "memory");
  mb();
  write_csr(sptbr, root_page_table);

  // then make sure we're in bounds
  if (HLS()->hart_id >= num_harts)
    panic("too many harts");

  boot_other_hart();
}

void machine_init(uint32_t hart_id)
{
  hls_init(hart_id);
  mstatus_init();
  fp_init();

  if (hart_id == 0)
    init_first_hart();
  else
    init_other_hart();
}