forked from SanseroGames/LetsGo-OS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
230 lines (197 loc) · 6.66 KB
/
main.go
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
package main
import (
"io"
"path"
"unsafe"
)
var progs = [...]string{
// "/usr/cread",
// "/usr/helloc",
// "/usr/hellocxx",
// "/usr/hellogo",
// "/usr/hellorust",
// "/usr/readtest",
// "/usr/rustread",
// "/usr/shell",
// "/usr/statx",
"/usr/syscall-test",
}
var domains [len(progs)]*domain
var threads [len(progs)]*thread
func main()
const ENABLE_DEBUG = false
//go:linkname kmain main.main
func kmain(info *MultibootInfo, stackstart uintptr, stackend uintptr) {
if stackstart <= stackend {
kernelPanic("No stack")
}
text_mode_init()
InitSerialDevice()
text_mode_flush_screen()
s := "Hi and welcome to Let's-Go OS"
s2 := "An Operating System written in the GO programming language"
s3 := "It can't do much, but I hope you enjoy your stay"
kprintln(s)
kprintln(s2)
kprintln(s3)
kprintln("")
kdebugln("Starting initialization...")
InitSegments()
InitInterrupts()
SetInterruptHandler(0xd, gpfPanic, KCS_SELECTOR, PRIV_USER)
InitSyscall()
InitPIC()
InitPit()
InitKeyboard()
InitATA()
InitSerialDeviceInterrupt()
InitMultiboot(info)
//printMemMaps()
InitPaging()
InitUserMode(stackstart, stackend)
text_mode_println_col("Initilaization complete", 0x2)
kdebugln("Initialization complete")
//HdReadSector()
var err int
for i := 0; i < len(progs); i++ {
newDomainMem := AllocPage()
Memclr(newDomainMem, PAGE_SIZE)
newDomain := (*domain)(unsafe.Pointer(newDomainMem))
newThreadMem := AllocPage()
Memclr(newThreadMem, PAGE_SIZE)
newThread := (*thread)(unsafe.Pointer(newThreadMem))
err = StartProgram(progs[i], newDomain, newThread)
if err != 0 {
kernelPanic("Could not start program")
}
newDomain.MemorySpace.MapPage(newThreadMem, newThreadMem, PAGE_RW|PAGE_PERM_KERNEL)
AddDomain(newDomain)
}
kprintln("domain size: ", uint(unsafe.Sizeof(domains[0])),
" thread_size: ", uint(unsafe.Sizeof(threads[0])),
" total: ", uint(unsafe.Sizeof(domains[0])+unsafe.Sizeof(threads[0])))
kprintln("stack start: ", uintptr(scheduleThread.kernelStack.hi),
" stack end: ", uintptr(scheduleThread.kernelStack.lo))
kprintln("info: ", uint(unsafe.Sizeof(currentThread.info)),
" regs: ", uint(unsafe.Sizeof(currentThread.regs)))
if currentThread == nil {
kernelPanic("I expect AddDomain to set currentThread variable")
}
kernelThreadInit()
kernelPanic("Could not jump to user space :/")
}
func gpfPanic() {
kerrorln("\nReceived General Protection fault. Disabling Interrupts and halting")
kprintln("Errorcode: ", uintptr(currentThread.info.ExceptionCode))
panicHelper(currentThread)
}
func printFuncName(pc uintptr) {
f := runtimeFindFunc(pc)
if !f.valid() {
kprintln("func: ", pc)
return
}
s := f._Func().Name()
file, line := f._Func().FileLine(pc)
_, filename := path.Split(file)
kprintln(s, " (", filename, ":", line, ")")
}
func panicHelper(thread *thread) {
kprintln("Domain ID: ", thread.domain.pid, ", Thread ID: ", thread.tid)
kprintln("Program name: ", thread.domain.programName)
if thread.isKernelInterrupt {
kprint("In kernel function: ")
printFuncName(thread.kernelInfo.EIP)
} else {
kprintln("In user function: ", thread.info.EIP)
}
printThreadRegisters(thread, defaultScreenWriter)
printThreadRegisters(thread, defaultLogWriter)
DisableInterrupts()
Hlt()
}
// wrapper for do_kernelPanic that gets the return address
// and pushers it on the stack and then calls do_kernelPanic
func kernelPanic(msg string)
//go:nosplit
func do_kernelPanic(caller uintptr, msg string) {
kerrorln("\n", msg, " - kernel panic :(")
kprint("Called from function: ")
printFuncName(caller - 4) // account for the fact that caller points to the instruction after the call
if currentThread != nil {
panicHelper(currentThread)
} else {
kerrorln("Cannot print registers. 'currentThread' is nil")
}
DisableInterrupts()
Hlt()
// does not return
}
func printThreadRegisters(t *thread, w io.Writer) {
kFprint(w, "User regs: Kernel regs:\n")
f := runtimeFindFunc(uintptr(t.kernelInfo.EIP))
kFprint(w, "EIP: ", t.info.EIP, " ", "EIP: ", t.kernelInfo.EIP, " ", f._Func().Name(), "\n")
//rintRegisterLineInfo("EIP: ", t.info.EIP, t.kernelInfo.EIP, f._Func().Name())
printRegisterLine(w, 20, "ESP: ", t.info.ESP, t.kernelInfo.ESP)
printRegisterLine(w, 20, "EBP: ", t.regs.EBP, t.kernelRegs.EBP)
printRegisterLine(w, 20, "EAX: ", t.regs.EAX, t.kernelRegs.EAX)
printRegisterLine(w, 20, "EBX: ", t.regs.EBX, t.kernelRegs.EBX)
printRegisterLine(w, 20, "ECX: ", t.regs.ECX, t.kernelRegs.ECX)
printRegisterLine(w, 20, "EDX: ", t.regs.EDX, t.kernelRegs.EDX)
printRegisterLine(w, 20, "ESI: ", t.regs.ESI, t.kernelRegs.ESI)
printRegisterLine(w, 20, "EDI: ", t.regs.EDI, t.kernelRegs.EDI)
printRegisterLine(w, 20, "EFLAGS: ", t.info.EFLAGS, t.kernelInfo.EFLAGS)
printRegisterLine(w, 20, "Exception: ", t.info.ExceptionCode, t.kernelInfo.ExceptionCode)
printRegisterLine(w, 20, "Interrupt: ", t.info.InterruptNumber, t.kernelInfo.InterruptNumber)
printRegisterLine(w, 20, "Krn ESP: ", t.regs.KernelESP, t.kernelRegs.KernelESP)
}
func printRegisterLine(w io.Writer, tablength int, label string, userReg, kernelReg uint32) {
firstLength := len(label)
kFprint(w, label, uintptr(userReg))
// pad number
firstLength += 3 // account for the hexadecimal 0x#
for i, n := firstLength, userReg>>4; i < 20; i, n = i+1, n>>4 {
if n == 0 {
kFprint(w, " ")
}
}
kFprint(w, label, uintptr(kernelReg), "\n")
}
func printRegisters(w io.Writer, info *InterruptInfo, regs *RegisterState) {
kFprint(w, "Interrupt: ", uintptr(info.InterruptNumber), "\n")
kFprint(w, "Exception: ", uintptr(info.ExceptionCode), "\n")
kFprint(w, "EIP: ", uintptr(info.EIP), "\n")
kFprint(w, "CS: ", uintptr(info.CS), "\n")
kFprint(w, "EFLAGS: ", uintptr(info.EFLAGS), "\n")
kFprint(w, "ESP: ", uintptr(info.ESP), "\n")
kFprint(w, "SS: ", uintptr(info.SS), "\n")
kFprint(w, "-----------\n")
kFprint(w, "GS: ", uintptr(regs.GS), "\n")
kFprint(w, "FS: ", uintptr(regs.FS), "\n")
kFprint(w, "ES: ", uintptr(regs.ES), "\n")
kFprint(w, "DS: ", uintptr(regs.DS), "\n")
kFprint(w, "EBP: ", uintptr(regs.EBP), "\n")
kFprint(w, "EAX: ", uintptr(regs.EAX), "\n")
kFprint(w, "EBX: ", uintptr(regs.EBX), "\n")
kFprint(w, "ECX: ", uintptr(regs.ECX), "\n")
kFprint(w, "EDX: ", uintptr(regs.EDX), "\n")
kFprint(w, "ESI: ", uintptr(regs.ESI), "\n")
kFprint(w, "EDI: ", uintptr(regs.EDI), "\n")
kFprint(w, "KernelESP", uintptr(regs.KernelESP), "\n")
}
func printTid(w io.Writer, t *thread) {
kFprint(w, "Pid: ", t.domain.pid, ", Tid: ", t.tid, "\n")
}
func delay(v int) {
for i := 0; i < 684000; i++ {
for j := 0; j < v; j++ {
}
}
}
func cstring(ptr uintptr) string {
var n int
for p := ptr; *(*byte)(unsafe.Pointer(p)) != 0; p++ {
n++
}
return unsafe.String((*byte)(unsafe.Pointer(ptr)), n)
}