Skip to content

Commit 64236b1

Browse files
committed
initial commit
0 parents  commit 64236b1

20 files changed

+2428
-0
lines changed

.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/build/
2+
/kernel.bin
3+
/dump.txt
4+
/out.txt
5+
/docs/.obsidian/

.vscode/settings.json

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"rust-analyzer.cargo.target": "riscv64imac-unknown-none-elf",
3+
"rust-analyzer.cargo.allTargets": false
4+
}

booting.md

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
- read fdt to determine usable memory
2+
- create page allocation structure
3+
- create kernel global data? ((num of) harts, ptr to scheduling & allocation)
4+
- maybe just use registers
5+
- create scheduling structure
6+
- create memory map structures
7+
- init program setup (create descriptor, insert to scheduler, memory map)
8+
- create per-thread descriptors
9+
- set trap handlers
10+
- wake up all threads
11+
12+
13+
sloader:
14+
- read fdt
15+
- set up memory allocation (needs fdt)
16+
- set up scheduling?
17+
- set up global page
18+
- set up hart pages
19+
- set up exception and interrupt handling (needs fdt)
20+
- set up uloader device map from fdt (without kernel)
21+
- set up uloader exe and stack memory by mapping (?)
22+
23+
(hacky) act as a user prgoram and perform syscalls to set up uloader program
24+
(hacky) "become" uloader
25+
- jump to kernel code that returns to executor?

design.md

+176
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
### Per executor:
2+
- kernel scratch
3+
- executor info
4+
- scheduler entry
5+
- futex stuff??
6+
- virtual memory stuff
7+
- capabilities / handles / resources
8+
9+
### Requirements:
10+
- rv64ia
11+
- S-mode
12+
- rv39?, rv48?
13+
14+
### High Level States:
15+
- wfi / sleep / program that sleeps???
16+
- scheduling
17+
- executing user program
18+
- handling system call
19+
- running in U-mode
20+
21+
### System Calls:
22+
- yield
23+
- map memory
24+
- start executor
25+
- wake
26+
- stop executor
27+
- system info: global read-only page
28+
- terminate
29+
30+
### resources:
31+
- address space
32+
- executor
33+
- are mapped as pages into user vmem with U-mode set
34+
- address space for executor cannot be changed
35+
36+
### Procedures:
37+
- virt to phys
38+
- phys to virt
39+
- zero page
40+
- map page(s)
41+
42+
### Userspace pages (lowmem)
43+
- normal memory (RSW = 0b00) (U=1)
44+
- device memory (RSW = 0b01) (U=1)
45+
- executor page (RSW = 0b10) (U=0)
46+
- vas page (RSW = 0B11) (U=0)
47+
48+
user mode: sscratch = S satp?,
49+
supervisor mode: sscratch =
50+
51+
52+
user mode:
53+
- S-satp
54+
- P-page
55+
56+
kernel mode:
57+
- U-satp
58+
- P-page
59+
60+
61+
- set queue
62+
- park
63+
64+
- read queue
65+
- unpark?
66+
67+
park_and_op
68+
unpark
69+
70+
71+
park_and_op:
72+
1. *thread_page.parked bit = true // still have executing bit set
73+
2. op
74+
75+
unpark:
76+
1. *thread_page.parked bit = false
77+
2. if not executing, move to scheduler queue
78+
79+
scheduler queue can have multiples?
80+
81+
park_and_swap
82+
unpark
83+
84+
park_if(addr, expected):
85+
if !validate(addr):
86+
return ERR_ADDR
87+
set_parked(true)
88+
v = load_u_virt(addr)
89+
if v != expected:
90+
set_parked(false)
91+
return ERR_VAL
92+
yield()
93+
94+
unpark(id):
95+
state = load_state(id)
96+
if parked(state) && executing(state):
97+
98+
if parked(state) && !executing(state):
99+
100+
### memory maps
101+
- reference count in invalid PTE / allocator array node
102+
-
103+
104+
### page alloc and scheduling stuff on same page = Global Page
105+
106+
### scheduling
107+
- just a global FIFO for now idk
108+
- thread local spmc?
109+
110+
### page allocation
111+
- initially support only FLATMEM
112+
- maybe add support for SPARSEMEM (binary search)
113+
- see longqueue, mpmc
114+
- types of kernel pages:
115+
1. executor
116+
2. mm (root / other)
117+
3. hart-local
118+
119+
### kernel register convention
120+
- x3/gp = global kernel page
121+
- x4/tp = hart page
122+
- x8 = current executor
123+
- x3 = kernel satp
124+
- x4 = page allocator
125+
126+
### kernel hart page (x4/tp)
127+
0x000:0x008 = mcs_lock_next
128+
0x200:0x208 = mcs_lock_held
129+
130+
### global kernel page (x3/gp)
131+
0x000:0x008 = page_alloc_head
132+
0x200:0x208 = page_alloc_tail
133+
0x400:0x408 = sched_mcs_lock
134+
0x600:0x608 = sched_head
135+
0x608:0x610 = sched_tail
136+
137+
### executor state page
138+
0x00:0x08 = pc | kernel hart-local
139+
0x08:0x10 = x1
140+
0x10:0x18 = x2
141+
0x18:0x20 = x3
142+
0x20:0x28 = x4
143+
0x28:0x30 = x5
144+
0x30:0x38 = x6
145+
0x38:0x40 = x7
146+
0x40:0x48 = x8
147+
0x48:0x50 = x9
148+
0x50:0x58 = x10
149+
0x58:0x60 = x11
150+
0x60:0x68 = x12
151+
0x68:0x70 = x13
152+
0x70:0x78 = x14
153+
0x78:0x80 = x15
154+
0x80:0x88 = x16
155+
0x88:0x90 = x17
156+
0x90:0x98 = x18
157+
0x98:0xA0 = x19
158+
0xA0:0xA8 = x20
159+
0xA8:0xB0 = x21
160+
0xB0:0xB8 = x22
161+
0xB8:0xC0 = x23
162+
0xC0:0xC8 = x24
163+
0xC8:0xD0 = x25
164+
0xD0:0xD8 = x26
165+
0xD8:0xE0 = x27
166+
0xE0:0xE8 = x28
167+
0xE8:0xF0 = x29
168+
0xF8:0xF0 = x30
169+
0xF0:0x100 = x31
170+
0x100:0x108 = ptr to next to schedule
171+
0x110:0x118 = satp
172+
0x118:0x120 = ptr to vas
173+
0x120:0x128 = current hart page
174+
0x200:0x208 = state (running / terminated / waiting)
175+
0x400:0x408 = refcount
176+
0x800:0x808 = locked

docs/Kernel common.md

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
## kernel register convention
2+
- x3/gp = global kernel page
3+
- x4/tp = hart page
4+
- x8/fp = current executor
5+
- aX registers used for syscall args/res
6+
- tX registers can be used, macros can garble
7+
-- x3 = kernel satp
8+
-- x4 = page allocator
9+
10+
11+
## Kernel pages
12+
# TODO: move mcs_lock state from hart to executor (remove hart page completely?)
13+
### kernel hart page (x4/tp)
14+
`0x000:0x008` = mcs_lock_next
15+
`0x200:0x208` = mcs_lock_held
16+
17+
### global kernel page (x3/gp)
18+
`0x000:0x008` = page_alloc_head
19+
`0x200:0x208` = page_alloc_tail
20+
`0x400:0x408` = sched_mcs_lock
21+
`0x600:0x608` = sched_head
22+
`0x608:0x610` = sched_tail
23+
`0x700:0x708` = kernel_satp
24+
25+
### executor state page
26+
`0x000:0x008` = pc | kernel hart-local
27+
`0x008:0x010` = x1
28+
`0x010:0x018` = x2
29+
`0x018:0x020` = x3
30+
`0x020:0x028` = x4
31+
`0x028:0x030` = x5
32+
`0x030:0x038` = x6
33+
`0x038:0x040` = x7
34+
`0x040:0x048` = x8
35+
`0x048:0x050` = x9
36+
`0x050:0x058` = x10
37+
`0x058:0x060` = x11
38+
`0x060:0x068` = x12
39+
`0x068:0x070` = x13
40+
`0x070:0x078` = x14
41+
`0x078:0x080` = x15
42+
`0x080:0x088` = x16
43+
`0x088:0x090` = x17
44+
`0x090:0x098` = x18
45+
`0x098:0x0A0` = x19
46+
`0x0A0:0x0A8` = x20
47+
`0x0A8:0x0B0` = x21
48+
`0x0B0:0x0B8` = x22
49+
`0x0B8:0x0C0` = x23
50+
`0x0C0:0x0C8` = x24
51+
`0x0C8:0x0D0` = x25
52+
`0x0D0:0x0D8` = x26
53+
`0x0D8:0x0E0` = x27
54+
`0x0E0:0x0E8` = x28
55+
`0x0E8:0x0F0` = x29
56+
`0x0F0:0x0F8` = x30
57+
`0x0F8:0x100` = x31
58+
`0x100:0x108` = ptr to next to schedule
59+
`0x110:0x118` = satp?
60+
`0x118:0x120` = ptr to vas?
61+
`0x120:0x128` = current hart page
62+
`0x200:0x208` = state (park) (running / terminated / waiting?)
63+
`0x400:0x408` = refcount?
64+
`0x800:0x808` = locked?

0 commit comments

Comments
 (0)