Repository navigation
Expand file tree
/
Copy pathkernel.c
More file actions
436 lines (337 loc) · 10.3 KB
/
Copy pathkernel.c
File metadata and controls
436 lines (337 loc) · 10.3 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
/*
uOS Kernel Version: U25Q4-AArch64-Urik4
uX Version: Kernel Embed (look u_uX.h for full version name)
Expected version calendar:
- Alpha versions
U25Q4-AArch64-Urik4 <-
U26Q1-AArch64-Urik5
U26Q1-AArch64-Urik6
U26Q1-AArch64-Urik7
...
- E1 Release
U26Q2-AArch64-UrikAsit-E1
- E2 beta
U26Q2-AArch64-UrikAsit-E2-d00
...
U26Q3-AArch64-UrikAsit-E2-d10
- E2 Release
U26Q3-AArch64-UrikAsit-E2
-E3s beta (Security Enhanced)
U26Q4-AArch64-UrikAsit-E3s-d00
...
U27Q1-AArch64-UrikAsit-E3s-d10
- E3s Release (Security Enhanced)
U27Q1-AArch64-UrikAsit-E3s
*/
/*
uOS info code structure
<A:B:C:D>
A: Info type
-UIN: Info
-UWR: Warn
-UER: Error
-UFE: Fatal Error
B: Location id
-Ke: Kernel entry
-Au: Api unkown
-Ux: uX Api
C: Section of B
-S[X]: X. modified or added section
D: Section name
-SD: SD card
-UFS: uFileSystem
-UX: uX Api
*/
#include "u_gpio.h" // basic gpio
#include "u_uart.h"
#include "u_display.h" // basic display
#include "u_rand.h" // basic random
#include <memory/u_memory.h>
#include <u_SuperRH.h>
#include "u_spi_flash.h"
#include "u_cstr_util.h"
#include "u_terminal.h"
#include "u_fs.h"
#include "u_arm.h"
#include "u_thread.h"
#include "u_mmu.h"
#include "u_uX.h"
// Pixel565 *mainDisplayBuffer;
u64 elapsedMS;
time_point start, end;
size_t freeBytes;
char tmpstr[40];
void start_sd(void)
{
// Initialize the SD card
if (sd_init() != SD_OK)
{
uart_print("SD boot init fail!\n<UER:Ke:S0:SD>\n");
return;
}
// Get capacity in bytes
uint64_t size_bytes = sd_get_size();
// Get capacity in blocks (1 block = 512 bytes)
uint32_t block_count = sd_get_block_count();
// Print results
uart_print("SDsize:\n");
uart_print(" Bytes: ");
uart_print(ulltoa(size_bytes, tmpstr));
uart_print("\n");
char *tstr[3];
tstr[0] = "SDsize: ";
tstr[1] = ftoa((float)size_bytes / (1024.0f * 1024.0f * 1024.0f), 2, tmpstr);
tstr[2] = " Gb";
char *astr = append_strs((char **)&tstr, 3);
kfree(astr);
/*
if (ufs_init_sd() == UFS_FAIL)
{
uart_print("Formating sd...\n");
if (ufs_format_sd() == UFS_SUCCESS)
{
uart_print("Format OK!\n");
ut_print("Format OK!");
}
else
{
uart_print("Format ERR!\n");
ut_print("Format ERR!");
}
}
*/
}
__attribute__((optnone)) uint64_t this_must_return_input(uint64_t input)
{
return input;
}
void t1()
{
uart_print("T1 Joined!\n");
while (1)
{
delay_ms(1002);
uart_print("Im 1.002s!\n");
}
}
void t2()
{
uart_print("T2 Joined!\n");
while (1)
{
delay_ms(1001);
uart_print("Im 1.001s!\n");
}
}
// unused here
extern void el1_sync_handler();
extern void irq_vector_entry();
void main()
{
uart_init(UA_B115200);
led_init();
vbar_set(_el1_vectors_);
uart_print("kernel wake up!\n");
uart_print("uart_print addr: 0x");
uart_print_hex64((uint64_t)&uart_print);
uart_print(" mmu_enable addr: 0x");
uart_print_hex64((uint64_t)&mmu_enable);
uart_print("\n");
// some systems use this variable so pre assigning prevents too many bugs
current_thread = UOS_KERNEL_THREAD_ID;
// init page allocator
uart_print("Starting UMP system...\n");
ump_allocator_init_mem();
ump_allocator_init_vmem();
uart_print("UMP system started!\n");
// init heap allocator
uart_print("Starting UHP system...\n");
init_kernel_base_heaps();
uart_print("UHP system started!\n");
// init Super Request Handler (SRH)
SRH_init();
uart_print("Super Request Handler intiated!\n");
// init thread system
u_thread_initsys_();
uart_print("thread system intiated!\n");
// mmu is off here
// calculate kernel section sizes
size_t _kernel_text_size_ = _kernel_text_end_ - _kernel_text_start_;
size_t _kernel_rodata_size_ = _kernel_rodata_end_ - _kernel_rodata_start_;
size_t _kernel_data_size_ = _kernel_data_end_ - _kernel_data_start_;
size_t _kernel_bss_size_ = _kernel_bss_end_ - _kernel_bss_start_;
// map kernel text
mmu_map_range(_kernel_text_start_, _kernel_text_start_, _kernel_text_size_, PTE_PRESET_KERNEL_EXECUTABLE, get_mmu_ctx(UOS_KERNEL_THREAD_ID));
// map kernel bss
mmu_map_range(_kernel_bss_start_, _kernel_bss_start_, _kernel_bss_size_, PTE_PRESET_KERNEL_DATA, get_mmu_ctx(UOS_KERNEL_THREAD_ID));
// map kernel data
mmu_map_range(_kernel_data_start_, _kernel_data_start_, _kernel_data_size_, PTE_PRESET_KERNEL_DATA, get_mmu_ctx(UOS_KERNEL_THREAD_ID));
// map kernel rodata
mmu_map_range(_kernel_rodata_start_, _kernel_rodata_start_, _kernel_rodata_size_, PTE_PRESET_KERNEL_RODATA, get_mmu_ctx(UOS_KERNEL_THREAD_ID));
// map kernel stack
mmu_map_range(_kernel_stack_start_pa_ptr_, _kernel_stack_start_pa_ptr_, _kernel_stack_end_pa_ptr_ - _kernel_stack_start_pa_ptr_, PTE_PRESET_KERNEL_DATA, get_mmu_ctx(UOS_KERNEL_THREAD_ID));
// map all of pageable memory
mmu_map_range(mem, mem, mem_end - mem, PTE_PRESET_KERNEL_DATA, get_mmu_ctx(UOS_KERNEL_THREAD_ID));
// map all of pageable video memory
mmu_map_range(vmem, vmem, vmem_end - vmem, PTE_PRESET_KERNEL_DATA, get_mmu_ctx(UOS_KERNEL_THREAD_ID));
// Main peripherals (GPIO, UART, SPI, I2C, etc.)
// Base: 0xFE000000, Size: ~16MB
mmu_map_range(0xFE000000, 0xFE000000, 0x1000000, PTE_PRESET_KERNEL_MMIO, get_mmu_ctx(UOS_KERNEL_THREAD_ID));
// PCIe controller registers
// Base: 0xFD500000
mmu_map_range(0xFD500000, 0xFD500000, 0xF0000, PTE_PRESET_KERNEL_MMIO, get_mmu_ctx(UOS_KERNEL_THREAD_ID));
// GIC-400 (Interrupt Controller)
// Base: 0xFF840000
mmu_map_range(0xFF840000, 0xFF840000, 0xF0000, PTE_PRESET_KERNEL_MMIO, get_mmu_ctx(UOS_KERNEL_THREAD_ID));
// Local peripherals (ARM timer, local interrupts)
// Base: 0xFF800000
mmu_map_range(0xFF800000, 0xFF800000, 0x40000, PTE_PRESET_KERNEL_MMIO, get_mmu_ctx(UOS_KERNEL_THREAD_ID));
// also map kernel stuff to high va
// map kernel text
mmu_map_range(UOS_TTBR1_MIN + _kernel_text_start_, _kernel_text_start_, _kernel_text_size_, PTE_PRESET_KERNEL_EXECUTABLE, get_mmu_ctx(UOS_KERNEL_THREAD_ID));
// map kernel bss
mmu_map_range(UOS_TTBR1_MIN + _kernel_bss_start_, _kernel_bss_start_, _kernel_bss_size_, PTE_PRESET_KERNEL_DATA, get_mmu_ctx(UOS_KERNEL_THREAD_ID));
// map kernel data
mmu_map_range(UOS_TTBR1_MIN + _kernel_data_start_, _kernel_data_start_, _kernel_data_size_, PTE_PRESET_KERNEL_DATA, get_mmu_ctx(UOS_KERNEL_THREAD_ID));
// map kernel rodata
mmu_map_range(UOS_TTBR1_MIN + _kernel_rodata_start_, _kernel_rodata_start_, _kernel_rodata_size_, PTE_PRESET_KERNEL_RODATA, get_mmu_ctx(UOS_KERNEL_THREAD_ID));
// map kernel stack
mmu_map_range(UOS_TTBR1_MIN + _kernel_stack_start_pa_ptr_, _kernel_stack_start_pa_ptr_, _kernel_stack_end_pa_ptr_ - _kernel_stack_start_pa_ptr_, PTE_PRESET_KERNEL_DATA, get_mmu_ctx(UOS_KERNEL_THREAD_ID));
// enable mmu
uart_print("ttbr0: 0x");
uart_print_hex64(get_mmu_ctx(UOS_KERNEL_THREAD_ID)->L0_ttbr0);
uart_print(" ttbr1: 0x");
uart_print_hex64(get_mmu_ctx(UOS_KERNEL_THREAD_ID)->L0_ttbr1);
uart_print("\n");
uart_print("Enabling mmu...\n");
mmu_setup(); // setup mmu registers
mmu_switch_ttbr(get_mmu_ctx(UOS_KERNEL_THREAD_ID));
mmu_barrier();
mmu_flush_tlb();
mmu_enable();
uart_print("Mmu enabled!\n");
// try to jump to high va kernel
uint64_t high_va_offset = UOS_TTBR1_MIN;
asm volatile(
"add sp, sp, %0\n"
"add x29, x29, %0\n"
"adr x0, 1f\n"
"add x0, x0, %0\n"
"br x0\n"
"1:\n"
:
: "r"(high_va_offset)
: "x0", "memory");
// reset vbar
vbar_set(UOS_TTBR1_MIN + _high_va_el1_vectors_);
uart_print("We are in high va! \nPC: 0x");
uart_print_hex64(read_pc());
uart_print("\nSP: 0x");
uart_print_hex64(read_sp());
uart_print("\nLR: 0x");
uart_print_hex64(read_lr());
uart_print("\n");
// now we are in high va kernel
uart_print("Unmapping old low va\n");
// unmap old pa=va kernel bss/code/stack region
// unmap old kernel code & bss other data..
//mmu_unmap_range(get_mmu_ctx(UOS_KERNEL_THREAD_ID), _kernel_start_pa_ptr_, _kernel_end_pa_ptr_ - _kernel_start_pa_ptr_);
// unmap old kernel stack
// nope mmu_unmap_range(get_mmu_ctx(UOS_KERNEL_THREAD_ID), _kernel_stack_start_pa_ptr_, _kernel_stack_end_pa_ptr_ - _kernel_stack_start_pa_ptr_ - (4096));
uart_print("MMU enabled successfuly!\n");
// reinit Super Request Handler (SRH) because handler addresses are changed
SRH_init();
uart_print("Super Request Handler reintiated!\n");
uart_print("EL: ");
uart_print_dec(u_arm_current_el_());
uart_print("\n");
delay_ms(1000);
srand(0x330633);
int *a = 0ULL;
*a = 0;
while (true)
; // wait here
/*
lcd_init();
lcd_clear_screen(0xffff);
lcd_clear_screen(P565(255, 0, 255));
*/
char *usrinp = (char *)kmalloc(300);
*usrinp = '\0';
char *terinp = (char *)kmalloc(300);
*terinp = '\0';
spi_init();
// test stuff
void *ptr = NULL;
void *oPtr = ptr;
start_sd();
uart_print("kernel loop start\n");
while (1)
{
if (uart_available())
{
uart_input(terinp, 300);
if (!strcmp(terinp, "print"))
{
uart_print("OK!\n");
}
else if (!strcmp(terinp, "tTest"))
{
uart_print("Creating threads!\n");
u_thread_create(t1, 10, 1);
uart_print("OK! 1\n");
u_thread_create(t2, 10, 1);
uart_print("OK! 2\n");
uart_print("Threads created!\n");
}
else if (!strcmp(terinp, "cause_mem_exception"))
{
uart_print("Causing access exception...\n");
((char *)((uint64_t)(-1)))[0] = 'E';
}
else if (!strcmp(terinp, "ufs_format"))
{
uart_print("Formating sd...\n");
if (ufs_format_sd() == UFS_SUCCESS)
{
uart_print("Format OK!\n");
}
else
{
uart_print("Format ERR!\n");
}
uart_print("SD UFS formated!\n");
}
else if (!strcmp(terinp, "ufs_init"))
{
if (ufs_init_sd() == UFS_FAIL)
{
uart_print("UFS init error!\n");
}
else
{
uart_print("UFS init success!\n");
}
}
else if (!strcmp(terinp, "mstat"))
{
udb();
}
else if (!strcmp(terinp, "sdinit"))
{
uart_print("Disabled\n");
}
else
{
uart_print(terinp);
uart_print(" -> Unknown interrupt command!\n");
}
}
//run threads with 128 ctx switch max
//u_thread_run_threads_(128);
// process SuperRH requests
SRH_process_requests();
}
while (1)
;
}