-
Notifications
You must be signed in to change notification settings - Fork 9
/
schedule.go
365 lines (314 loc) · 7.45 KB
/
schedule.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
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
package main
import (
"unsafe"
)
type domain struct {
next *domain
pid uint32
numThreads uint32
nextTid uint32
Segments SegmentList
MemorySpace MemSpace
runningThreads threadList
blockedThreads threadList
programName string
}
func (d *domain) AddThread(t *thread) {
t.domain = d
t.next = nil
t.prev = nil
d.runningThreads.Enqueue(t)
t.tid = d.nextTid
d.nextTid++
d.numThreads++
}
func (d *domain) RemoveThread(t *thread) {
if t.domain != d {
return
}
if t.isBlocked {
d.blockedThreads.Dequeue(t)
} else {
d.runningThreads.Dequeue(t)
}
t.isRemoved = true
d.numThreads--
}
type stack struct {
hi uintptr
lo uintptr
}
type taskswitchbuf struct {
sp uintptr
}
type thread struct {
next *thread
prev *thread
domain *domain
tid uint32
userStack stack
kernelStack stack
isRemoved bool
// Currently ignored '^^ I don't have to do it thanks to spurious wakeups
isBlocked bool
waitAddress *uint32
// flag that shows that this thread would be handled as a new process in linux
isFork bool
// Infos to stall a thread when switching
info InterruptInfo
regs RegisterState
kernelInfo InterruptInfo
kernelRegs RegisterState
isKernelInterrupt bool
interruptedKernelEIP uintptr
interruptedKernelESP uint32
// TLS
tlsSegments [GDT_ENTRIES]GdtEntry
// fxsave and fxrstor need 512 bytes but 16 byte aligned
// need space to force alignment if array not aligned
fpOffset uintptr // should be between 0-15
fpState [512 + 16]byte
}
type threadList struct {
thread *thread
}
func (l *threadList) Next() *thread {
if l.thread == nil {
return nil
}
l.thread = l.thread.next
return l.thread
}
func (l *threadList) Enqueue(t *thread) {
if l.thread == nil {
l.thread = t
t.next = t
t.prev = t
} else {
t.next = l.thread
t.prev = l.thread.prev
l.thread.prev.next = t
l.thread.prev = t
}
}
func (l *threadList) Dequeue(t *thread) {
if t == l.thread && t.next == t {
l.thread = nil
} else if t == l.thread {
l.thread = t.next
}
t.prev.next = t.next
t.next.prev = t.prev
t.next = nil
t.prev = nil
}
type domainList struct {
head *domain
tail *domain
}
func (l *domainList) Append(domain *domain) {
if domain == nil {
return
}
if l.head == nil {
l.head = domain
l.tail = l.head
l.head.next = l.tail
} else {
domain.next = l.head
l.tail.next = domain
l.tail = domain
}
domain.pid = largestPid
largestPid++
}
func (l *domainList) Remove(d *domain) {
if d == l.head {
if d == l.tail {
l.head = nil
l.tail = nil
} else {
l.head = d.next
l.tail.next = l.head
}
return
}
for e := l.head; e.next != l.head; e = e.next {
if e.next == d {
e.next = d.next
if d == l.tail {
l.tail = e
}
break
}
}
}
var (
currentThread *thread = &scheduleThread
currentDomain *domain = nil
allDomains domainList = domainList{head: nil, tail: nil}
largestPid uint32 = 0x0
kernelHlt bool = false
scheduleThread thread = thread{}
)
func backupFpRegs(buffer uintptr)
func restoreFpRegs(buffer uintptr)
func AddDomain(d *domain) {
allDomains.Append(d)
if ENABLE_DEBUG {
kdebugln("Added new domain with pid ", d.pid)
}
if currentDomain == nil || currentThread == nil {
currentDomain = allDomains.head
currentThread = currentDomain.runningThreads.thread
}
}
func ExitDomain(d *domain) {
allDomains.Remove(d)
if allDomains.head == nil {
currentDomain = nil
}
// clean up memory
scheduleStackArg(func(dom uintptr) {
doma := (*domain)(unsafe.Pointer(dom))
cleanUpDomain(doma)
}, (uintptr)(unsafe.Pointer(d)))
}
// Execute on scheduleStack
func cleanUpDomain(d *domain) {
// Clean up threads
for cur := d.runningThreads.thread; d.runningThreads.thread != nil; cur = d.runningThreads.thread {
//kdebugln("Clean up thread ", cur.tid)
//kdebugln("t:", (uintptr)(unsafe.Pointer(cur)), " t.n:", (uintptr)(unsafe.Pointer(cur.next)), " t.p:", (uintptr)(unsafe.Pointer(cur.prev)))
d.runningThreads.Dequeue(cur)
cleanUpThread(cur)
}
for cur := d.blockedThreads.thread; d.blockedThreads.thread != nil; cur = d.blockedThreads.thread {
cleanUpThread(cur)
d.blockedThreads.Dequeue(cur)
}
// Clean allocated memory
d.MemorySpace.FreeAllPages()
// Clean up kernel resources
if ENABLE_DEBUG {
kdebugln("Allocated pages ", allocatedPages, " (out of", maxPages, ")")
}
Schedule()
FreePage((uintptr)(unsafe.Pointer(d)))
}
// Execute on scheduleStack
func cleanUpThread(t *thread) {
// TODO; Adjust when thread control block is no longer a single page
threadPtr := (uintptr)(unsafe.Pointer(t))
threadDomain := t.domain
threadDomain.MemorySpace.UnmapPage(t.kernelStack.lo)
threadDomain.MemorySpace.UnmapPage(threadPtr)
if currentThread == t {
currentThread = nil
}
}
func ExitThread(t *thread) {
if t.domain.numThreads <= 1 {
// we're last thread
ExitDomain(t.domain) // does not return
}
t.domain.RemoveThread(t)
if ENABLE_DEBUG {
kdebugln("Removing thread ", t.tid, " from domain ", t.domain.pid)
}
scheduleStackArg(func(threadPtr uintptr) {
thread := (*thread)(unsafe.Pointer(threadPtr))
cleanUpThread(thread)
Schedule()
}, (uintptr)(unsafe.Pointer(t)))
}
func BlockThread(t *thread) {
t.isBlocked = true
t.domain.runningThreads.Dequeue(t)
t.domain.blockedThreads.Enqueue(t)
PerformSchedule = true
}
func getESP() uintptr
func waitForInterrupt()
func Block() {
waitForInterrupt()
}
func ResumeThread(t *thread) {
t.isBlocked = false
t.domain.blockedThreads.Dequeue(t)
t.domain.runningThreads.Enqueue(t)
}
func Schedule() {
if currentDomain == nil {
kerrorln("No Domains to schedule")
Shutdown()
// DisableInterrupts()
// Hlt()
}
//kdebug("Scheduling in ")
//printTid(defaultLogWriter, currentThread)
nextDomain := currentDomain.next
newThread := nextDomain.runningThreads.Next()
if newThread == nil {
for newDomain := nextDomain.next; newDomain != nextDomain; newDomain = newDomain.next {
newThread = newDomain.runningThreads.Next()
if newThread != nil {
break
}
}
}
//if currentThread.next == currentThread && currentThread.tid != 0{
// kernelPanic("Why no next?")
//}
//if newThread.tid == currentThread.tid && currentThread.tid != 0 {
// kernelPanic("Why only one thread?")
//}
//kprintln("next domain: ", nextDomain.pid)
//kprintln("next thread: ", newThread.tid)
//kprintln("domain threads: ", nextDomain.numThreads)
if newThread == nil {
if kernelHlt {
// We are already stalling the kernel
return
}
// All threads blocked or no threads exist anymore.
kernelHlt = true
PerformSchedule = false
//currentThread = nil
kernelPanic("test")
return
}
kernelHlt = false
currentDomain = nextDomain
if newThread == currentThread {
return
}
switchToThread(newThread)
//kdebug("Now executing: ")
//printTid(defaultLogWriter, currentThread)
}
func switchToThread(t *thread) {
// Save state of current thread
if currentThread != nil {
addr := uintptr(unsafe.Pointer(&(currentThread.fpState)))
offset := 16 - (addr % 16)
currentThread.fpOffset = offset
backupFpRegs(addr + offset)
}
// Load next thread
//kdebugln("Switching to domain pid", currentDomain.pid, " and thread ", t.tid)
currentThread = t
addr := uintptr(unsafe.Pointer(&(currentThread.fpState)))
offset := currentThread.fpOffset
if offset != 0xffffffff {
if (addr+offset)%16 != 0 {
kprintln(addr, " ", offset)
kernelPanic("Cannot restore FP state. Not aligned. Did array move?")
}
restoreFpRegs(addr + offset)
}
// Load TLS
FlushTlsTable(t.tlsSegments[:])
}
func InitScheduling() {
}