| 1 |
|
| 2 |
|
| 3 |
#include <program_loading.h> |
| 4 |
#include <stdint.h> |
| 5 |
#include <arch/boot.h> |
| 6 |
#include <arch/encoding.h> |
| 7 |
#include <arch/pmp.h> |
| 8 |
#include <arch/smp/atomic.h> |
| 9 |
#include <console/console.h> |
| 10 |
#include <mcall.h> |
| 11 |
#include <vm.h> |
| 12 |
|
| 13 |
|
| 14 |
void run_payload_opensbi(struct prog *prog, void *fdt, struct prog *opensbi, int payload_mode) |
| 15 |
{ |
| 16 |
int hart_id = read_csr(mhartid); |
| 17 |
uintptr_t status = read_csr(mstatus); |
| 18 |
status = INSERT_FIELD(status, MSTATUS_MPIE, 0); |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
status = INSERT_FIELD(status, MSTATUS_MPP, PRV_M); |
| 26 |
|
| 27 |
write_csr(mtvec, prog_entry(opensbi)); |
| 28 |
|
| 29 |
write_csr(mie, 0); |
| 30 |
write_csr(mstatus, status); |
| 31 |
|
| 32 |
run_opensbi(hart_id, fdt, prog_entry(opensbi), prog_entry(prog), payload_mode); |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
void run_payload(struct prog *prog, void *fdt, int payload_mode) |
| 37 |
{ |
| 38 |
void (*doit)(int hart_id, void *fdt) = prog_entry(prog); |
| 39 |
int hart_id = read_csr(mhartid); |
| 40 |
uintptr_t status = read_csr(mstatus); |
| 41 |
extern void *_text, *_estack; |
| 42 |
status = INSERT_FIELD(status, MSTATUS_MPIE, 0); |
| 43 |
|
| 44 |
switch (payload_mode) { |
| 45 |
case RISCV_PAYLOAD_MODE_S: |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
setup_pmp((u64)(uintptr_t) _text, |
| 62 |
(u64)(uintptr_t) _estack - (u64)(uintptr_t) _text, 0); |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
close_pmp(); |
| 69 |
|
| 70 |
status = INSERT_FIELD(status, MSTATUS_MPP, PRV_S); |
| 71 |
|
| 72 |
write_csr(stvec, doit); |
| 73 |
|
| 74 |
write_csr(sie, 0); |
| 75 |
|
| 76 |
write_csr(satp, 0); |
| 77 |
|
| 78 |
write_csr(mscratch, MACHINE_STACK_TOP()); |
| 79 |
break; |
| 80 |
case RISCV_PAYLOAD_MODE_M: |
| 81 |
status = INSERT_FIELD(status, MSTATUS_MPP, PRV_M); |
| 82 |
|
| 83 |
write_csr(mtvec, doit); |
| 84 |
|
| 85 |
write_csr(mie, 0); |
| 86 |
break; |
| 87 |
default: |
| 88 |
die("wrong privilege level for payload"); |
| 89 |
break; |
| 90 |
} |
| 91 |
write_csr(mstatus, status); |
| 92 |
write_csr(mepc, doit); |
| 93 |
asm volatile( |
| 94 |
"mv a0, %0\n\t" |
| 95 |
"mv a1, %1\n\t" |
| 96 |
"mret" ::"r"(hart_id), |
| 97 |
"r"(fdt) |
| 98 |
: "a0", "a1"); |
| 99 |
} |