~eliasnaur/unik

ref: cd0f505f7c2c06bac9c3655ab38b7bbe2d1761f3 unik/kernel/thread_amd64.go -rw-r--r-- 6.4 KiB
cd0f505fElias Naur initial import 6 months ago
                                                                                
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
// SPDX-License-Identifier: Unlicense OR MIT

package kernel

import (
	"reflect"
	"time"
	"unsafe"
)

const maxThreads = 100

const (
	_IA32_KERNEL_GS_BASE = 0xc0000102
	_IA32_GS_BASE        = 0xc0000101
)

var globalThreads threads

type tid uint64

type threads struct {
	threads []thread
}

// thread represents per-thread context and bookkeeping. Must be
// 8 byte aligned so its fields must have known sizes.
type thread struct {
	self *thread
	context

	id tid

	block blockCondition
}

type blockCondition struct {
	syscall    uint32
	conditions waitConditions

	// For sleepCondition.
	sleep struct {
		monotoneTime uint64
		duration     time.Duration
	}

	// For futexCondition.
	futex uint64

	_ uint32
}

// waitConditions is a set of potential conditions that will wake up a
// thread.
type waitConditions uint32

const (
	interruptCondition waitConditions = 1 << iota
	sleepCondition
	futexCondition
	deadCondition
)

const scheduleTimeSlice = 10 * time.Millisecond

// Thread state for early initialization.
var thread0 thread

// Kernel thread for yielding.
var kernelThread thread

// context represent a thread's CPU state. The exact layout of context
// is known to the thread assembly functions.
type context struct {
	ip    uint64
	sp    uint64
	flags uint64
	bp    uint64
	ax    uint64
	bx    uint64
	cx    uint64
	dx    uint64
	si    uint64
	di    uint64
	r8    uint64
	r9    uint64
	r10   uint64
	r11   uint64
	r12   uint64
	r13   uint64
	r14   uint64
	r15   uint64

	fsbase uint64

	// fpState is space for the floating point context, including
	// alignment. FXSAVE/FXRSTOR needs 512 bytes.
	fpState [512]byte
}

//go:nosplit
func initThreads() error {
	// assembly expects the thread context after self.
	if unsafe.Offsetof(thread{}.self) != 0 {
		fatal("initThreads: invalid thread.self field alignment")
	}
	if unsafe.Offsetof(thread{}.context) != 8 {
		fatal("initThreads: invalid thread.context field alignment")
	}
	if unsafe.Offsetof(thread{}.fpState)%16 != 0 {
		fatal("initThreads: invalid thread.context field alignment")
	}
	return globalThreads.init()
}

//go:nosplit
func (ts *threads) init() error {
	size := unsafe.Sizeof(ts.threads[0]) * maxThreads
	addr, err := globalMap.mmap(0, uint64(size), pageFlagNX|pageFlagWritable)
	if err != nil {
		return err
	}
	hdr := (*reflect.SliceHeader)(unsafe.Pointer(&ts.threads))
	hdr.Data = uintptr(addr)
	hdr.Cap = int(size / unsafe.Sizeof(ts.threads[0]))
	return nil
}

//go:nosplit
func (ts *threads) newThread() (*thread, error) {
	if len(ts.threads) == cap(ts.threads) {
		return nil, kernError("newThread: too many threads")
	}
	tid := tid(len(ts.threads))
	ts.threads = ts.threads[:tid+1]
	newt := &ts.threads[tid]
	*newt = thread{
		id: tid,
	}
	newt.self = newt
	return newt, nil
}

// Schedule selects an appropriate thread to resume and makes it
// current.
//go:nosplit
func (ts *threads) schedule(t *thread) {
	for {
		updateClock()
		maxDur := 24 * time.Hour
		monotoneTime := unixClock.monotoneMillis()
		for i := 0; i < len(ts.threads); i++ {
			// Round-robin scheduling.
			tid := (int(t.id) + 1) % len(ts.threads)
			t = &ts.threads[tid]
			if dur, ok := t.runnable(monotoneTime); !ok {
				if dur > 0 && dur < maxDur {
					maxDur = dur
				}
				continue
			}
			t.block.conditions = 0
			t.makeCurrent()
			setTimer(scheduleTimeSlice)
			if t.block.syscall != 0 {
				resumeThreadFast()
			} else {
				resumeThread()
			}
			fatal("schedule: resume failed")
		}
		setTimer(maxDur)
		yield()
	}
}

//go:nosplit
func (ts *threads) futexWakeup(addr uint64, nwaiters int) {
	for i := 0; i < len(ts.threads); i++ {
		if nwaiters == 0 {
			break
		}
		t := &ts.threads[i]
		if t.block.conditions&futexCondition == 0 {
			continue
		}
		if t.block.futex != addr {
			continue
		}
		t.block.conditions = 0
		nwaiters--
	}
}

//go:nosplit
func (t *thread) runnable(monotoneTime uint64) (time.Duration, bool) {
	w := &t.block
	if w.conditions == 0 {
		return 0, true
	}
	cond := w.conditions
	if cond&interruptCondition != 0 {
		for i, intr := range pendingInterrupts {
			if intr {
				pendingInterrupts[i] = false
				t.setSyscallResult(_EOK, uint64(i))
				return 0, true
			}
		}
	}
	if cond&sleepCondition != 0 {
		dur := time.Duration(monotoneTime-t.block.sleep.monotoneTime) * time.Millisecond
		rem := t.block.sleep.duration - dur
		return rem, rem <= 0
	}
	return 0, false
}

//go:nosplit
func (t *thread) setSyscallResult(ret0, ret1 uint64) {
	t.ax = ret0
	t.dx = ret1
}

//go:nosplit
func (t *thread) makeCurrent() {
	v := uint64(uintptr(unsafe.Pointer(t)))
	wrmsr(_IA32_GS_BASE, v)
}

//go:nosplit
func (t *thread) sleepFor(duration time.Duration) {
	t.block.conditions |= sleepCondition
	t.block.sleep.monotoneTime = unixClock.monotoneMillis()
	t.block.sleep.duration = duration
}

//go:nosplit
func (t *thread) dump() {
	fields := []struct {
		field string
		value uint64
	}{
		{"addr", uint64(uintptr(unsafe.Pointer(t)))},
		{"id", uint64(t.id)},
		{"ip", t.ip},
		{"sp", t.sp},
		{"bp", t.bp},
		{"ax", t.ax},
		{"bx", t.bx},
		{"cx", t.cx},
		{"dx", t.dx},
		{"si", t.si},
		{"di", t.di},
		{"r8", t.r8},
		{"r9", t.r9},
		{"r10", t.r10},
		{"r11", t.r11},
		{"r12", t.r12},
		{"r13", t.r13},
		{"r14", t.r14},
		{"r15", t.r15},
		{"fsbase", t.fsbase},
	}
	for _, f := range fields {
		outputString(f.field)
		outputString(": ")
		outputUint64(f.value)
		outputString(" ")
	}
	nstate := len(t.fpState) / int(unsafe.Sizeof(uint64(0)))
	fpState := (*(*[1 << 30]uint64)(unsafe.Pointer(&t.fpState[0])))[:nstate:nstate]
	for i, s := range fpState {
		outputString("f")
		outputUint64(uint64(i))
		outputString(": ")
		outputUint64(s)
		outputString(" ")
	}
	outputString("\n")
}

// interruptSchedule is called to schedule a thread after a
// timer interrupt.
//go:nosplit
func interruptSchedule(t *thread) {
	t.block = blockCondition{}
	globalThreads.schedule(t)
}

// yield halts the processor until an interrupt arrives.
//go:nosplit
func yield() {
	swapgs()
	kernelThread.makeCurrent()
	swapgs()
	yield0()
}

func currentThread() *thread
func swapgs()

// saveThread stores the current thread state except IP, SP, CX, R11,
// and flags.
func saveThread()

// resumeThread restores thread context and resumes the thread. The
// context is stored in GS; see type context for the layout.
// resumeThread never returns.
func resumeThread()

// resumeThreadFast is like resumeThread, but clobbers CX and R11.
func resumeThreadFast()

// restoreThread restores thread context stored by saveThread.
func restoreThread()

func yield0()