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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
// See LICENSE for license details.
#include "pcr.h"
#include "pk.h"
#include "file.h"
#include "vm.h"
#include "frontend.h"
#include "elf.h"
#include <stdint.h>
#include <string.h>
elf_info current;
void init_tf(trapframe_t* tf, long pc, long sp, int user64)
{
memset(tf,0,sizeof(*tf));
if(sizeof(void*) != 8)
kassert(!user64);
tf->sr = (mfpcr(PCR_SR) & (SR_IM | SR_S64 | SR_VM)) | SR_S | SR_PEI;
if(user64)
tf->sr |= SR_U64;
tf->gpr[14] = sp;
tf->epc = pc;
}
static void handle_option(const char* s)
{
switch (s[1])
{
case 's':
current.t0 = rdcycle();
break;
default:
panic("unrecognized option: `%c'", s[1]);
break;
}
}
static void user_init()
{
struct args {
uint64_t argc;
uint64_t argv[];
};
const int argc_argv_size = 1024;
size_t stack_top = current.stack_top;
struct args* args = (struct args*)(stack_top - argc_argv_size);
populate_mapping(args, argc_argv_size, PROT_WRITE);
sysret_t r = frontend_syscall(SYS_getmainvars, (long)args, argc_argv_size, 0, 0);
kassert(r.result == 0);
// argv[0] is the proxy kernel itself. skip it and any flags.
unsigned a0 = 1;
for ( ; a0 < args->argc && *(char*)(uintptr_t)args->argv[a0] == '-'; a0++)
handle_option((const char*)(uintptr_t)args->argv[a0]);
args->argv[a0-1] = args->argc - a0;
args = (struct args*)&args->argv[a0-1];
stack_top = (uintptr_t)args;
// load program named by argv[0]
current.phdr_top = stack_top;
load_elf((char*)(uintptr_t)args->argv[0], ¤t);
stack_top = current.phdr;
struct {
long key;
long value;
} aux[] = {
{AT_ENTRY, current.entry},
{AT_PHNUM, current.phnum},
{AT_PHENT, current.phent},
{AT_PHDR, current.phdr},
{AT_PAGESZ, RISCV_PGSIZE},
{AT_NULL, 0}
};
// place argc, argv, envp, auxp on stack
#define PUSH_ARG(type, value) do { \
*((type*)sp) = value; \
sp += sizeof(type); \
} while (0)
#define STACK_INIT(type) do { \
unsigned naux = sizeof(aux)/sizeof(aux[0]); \
stack_top -= (1 + args->argc + 1 + 1 + 2*naux) * sizeof(type); \
stack_top &= -16; \
long sp = stack_top; \
PUSH_ARG(type, args->argc); \
for (unsigned i = 0; i < args->argc; i++) \
PUSH_ARG(type, args->argv[i]); \
PUSH_ARG(type, 0); /* argv[argc] = NULL */ \
PUSH_ARG(type, 0); /* envp[0] = NULL */ \
for (unsigned i = 0; i < naux; i++) { \
PUSH_ARG(type, aux[i].key); \
PUSH_ARG(type, aux[i].value); \
} \
} while (0)
if (current.elf64)
STACK_INIT(uint64_t);
else
STACK_INIT(uint32_t);
trapframe_t tf;
init_tf(&tf, current.entry, stack_top, current.elf64);
__clear_cache(0, 0);
pop_tf(&tf);
}
void boot()
{
memset(¤t, 0, sizeof(current));
file_init();
vm_init();
user_init();
}
|