- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathkernel.cpp
More file actions
Latest commit
139 lines (112 loc) · 3.9 KB
/
Copy pathkernel.cpp
File metadata and controls
139 lines (112 loc) · 3.9 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
/* Copyright (c) 2025, Rye Stahle-Smith
December 2nd, 2025 - kernel.cpp
Description: Core kernel initialization for console, scheduler, memory, traps, filesystem, and embedded user programs. Entry point after boot. */
#include<stdint.h>
#include"fat.h"
#include"shell.h"
#include"scheduler.h"
#include"embedded_user_programs.h"
externFAT fat;
extern"C"uint8_t _kernel_heap_start[];
extern"C"uint8_t _kernel_heap_end[];
boolservice_memory() {
uint8_t* start = _kernel_heap_start;
uint8_t* end = _kernel_heap_end;
// Basic sanity check: end must be > start
if (end <= start)
returnfalse;
// Write a test pattern and verify
start[0] = 0xAA;
start[1] = 0x55;
return (start[0] == 0xAA && start[1] == 0x55);
}
boolservice_traps() {
uint64_t stvec;
asmvolatile("csrr %0, stvec" : "=r"(stvec));
return stvec != 0;
}
boolservice_scheduler() { returnscheduler_init(); }
boolservice_filesystem() { return (fat.get_root() != nullptr); }
boolservice_userprog() {
// Check if we have any embedded programs
if (embedded_file_count == 0) {
returnfalse;
}
// Create user_programs directory
Directory* dir = fat.mkdir_recursive(fat.get_root(), "user_programs");
if (!dir) {
returnfalse;
}
// Create a file for each embedded program
for (unsignedint i = 0; i < embedded_file_count; i++) {
const EmbeddedFile* ef = &embedded_files[i];
// Create filename with .S extension
char filename[MAX_NAME_LEN];
int j = 0;
// Copy name
while (ef->name[j] && j < MAX_NAME_LEN - 3) {
filename[j] = ef->name[j];
j++;
}
filename[j] = '\0';
// Append .S
filename[j] = '.';
filename[j+1] = 'S';
filename[j+2] = '\0';
File* f = fat.touch(dir, filename);
if (!f) {
returnfalse;
}
// Copy assembly source code into virtual FAT file
uint32_t copy_size = ef->source_size;
if (copy_size > MAX_FILE_SIZE) {
copy_size = MAX_FILE_SIZE;
}
for (uint32_t b = 0; b < copy_size; b++) {
f->data[b] = ef->source[b];
}
// Set file size (don't add extra null terminator if source already has one)
f->size = copy_size;
f->used = true;
}
returntrue;
}
structService { constchar* name; bool (*check)(); };
Service services[] = {
{"scheduler", service_scheduler},
{"memory", service_memory},
{"traps", service_traps},
{"filesystem", service_filesystem},
{"user programs", service_userprog}
};
// Helper to print current privilege level
extern"C"voidprint_current_mode() {
uint64_t mstatus;
asmvolatile("csrr %0, mstatus" : "=r"(mstatus));
uint64_t mpp = (mstatus >> 11) & 0x3; // MPP bits
uint64_t spp = (mstatus >> 8) & 0x1; // SPP bit for sret
// This is approximate; we also know our boot sequence
// For display purposes:
if (mpp == 3) print_str("Machine Mode"); // MPP=11 -> M-mode
elseif (mpp == 1) print_str("Supervisor Mode"); // MPP=01 -> S-mode
elseprint_str("User Mode"); // fallback
}
extern"C"voidkernel_main() {
print_str("(kernel) ");
print_current_mode();
print_str(" Active. Starting RISC-V OS v1.0...\n");
print_str("(kernel) Initializing services:\n");
print_str(" • console........ OK\n");
for (int i = 0; i < sizeof(services)/sizeof(services[0]); i++) {
print_str(" • ");
print_str(services[i].name);
print_str("........ ");
print_str(services[i].check() ? "OK\n" : "FAIL\n");
}
print_str("\n(kernel) System ready. Starting scheduler...\n");
print_str("================================\n\n");
// Hand off to scheduler
scheduler_main();
// Loop forever (should never return)
while (1) asmvolatile("wfi");
}