CS350 COS
COS
Loading...
Searching...
No Matches
thread.c
Go to the documentation of this file.
1
2#include <stdbool.h>
3#include <stdint.h>
4#include <string.h>
5
6#include <sys/kconfig.h>
7#include <sys/kassert.h>
8#include <sys/kmem.h>
9#include <sys/mp.h>
10#include <sys/thread.h>
11#include <machine/amd64.h>
12#include <machine/amd64op.h>
13#include <machine/trap.h>
14#include <machine/pmap.h>
15
16extern void ThreadKThreadEntry(TrapFrame *tf);
17extern void switchstack(uint64_t *oldrsp, uint64_t rsp);
18
19void
21{
22 thr->arch.useFP = true;
23}
24
25void
26Thread_SetupKThread(Thread *thr, void (*f)(),
27 uintptr_t arg1, uintptr_t arg2, uintptr_t arg3)
28{
29 // Initialize stack
30 uint64_t stacktop = thr->kstack + PGSIZE;
32 TrapFrame *tf;
33
34 tf = (TrapFrame *)(stacktop - sizeof(*tf));
35 sf = (ThreadArchStackFrame *)(stacktop - sizeof(*tf) - sizeof(*sf));
36 thr->arch.rsp = (uint64_t)sf;
37
38 memset(tf, 0, sizeof(*tf));
39 memset(sf, 0, sizeof(*sf));
40
41 // Setup thread exit function on stack
42
44 sf->rdi = (uint64_t)tf;
45
46 tf->ds = 0;
47 tf->ss = 0; //SEL_KDS;
48 tf->rsp = stacktop;
49 tf->cs = SEL_KCS;
50 tf->rip = (uint64_t)f;
51 tf->rdi = (uint64_t)arg1;
52 tf->rsi = (uint64_t)arg2;
53 tf->rdx = (uint64_t)arg3;
54 tf->rflags = RFLAGS_IF;
55}
56
57static void
59{
60 TrapFrame tf;
61
62 memset(&tf, 0, sizeof(tf));
63 tf.ds = SEL_UDS | 3;
64 tf.rip = (uint64_t)arg1;
65 tf.cs = SEL_UCS | 3;
67 tf.ss = SEL_UDS | 3;
68 tf.rflags = RFLAGS_IF;
69 tf.rdi = (uint64_t)arg3; /* Userspace Argument */
70
71 Trap_Pop(&tf);
72}
73
74void
76{
78 thr->ustack, arg);
79}
80
82
83void
85{
86 /*
87 * Save and restore floating point and vector CPU state using the fxsave
88 * and fxrstor instructions.
89 */
90 if (oldthr->arch.useFP)
91 {
92 fxsave(&oldthr->arch.xsa);
93 }
94
95 if (newthr->arch.useFP)
96 {
97 fxrstor(&newthr->arch.xsa);
98 }
99
100 clts();
101
102 // Jump to trapframe
103 switchstack(&oldthr->arch.rsp, newthr->arch.rsp);
104
105 // Set new RSP0
106 TSS[CPU()].rsp0 = oldthr->kstack + 4096;
107}
108
XSAVEArea xsa
Definition: thread.h:20
uint64_t rsp
Definition: thread.h:22
bool useFP
Definition: thread.h:21
uint64_t rip
Definition: thread.h:16
uint64_t rdi
Definition: thread.h:14
void switchstack(uint64_t *oldrsp, uint64_t rsp)
void Thread_SetupUThread(Thread *thr, uintptr_t rip, uintptr_t arg)
Definition: thread.c:75
void Thread_SetupKThread(Thread *thr, void(*f)(), uintptr_t arg1, uintptr_t arg2, uintptr_t arg3)
Definition: thread.c:26
void Thread_SwitchArch(Thread *oldthr, Thread *newthr)
Definition: thread.c:84
TaskStateSegment64 TSS[MAX_CPUS]
Definition: machine.c:40
static void ThreadEnterUserLevelCB(uintptr_t arg1, uintptr_t arg2, uintptr_t arg3)
Definition: thread.c:58
void ThreadKThreadEntry(TrapFrame *tf)
Definition: thread.c:314
void Thread_InitArch(Thread *thr)
Definition: thread.c:20
uint64_t rsp0
Definition: amd64.h:108
#define RFLAGS_IF
Definition: amd64.h:180
#define SEL_KCS
Definition: amd64.h:80
#define SEL_UDS
Definition: amd64.h:84
#define SEL_UCS
Definition: amd64.h:83
static INLINE void fxrstor(struct XSAVEArea *xsa)
Definition: amd64op.h:386
static INLINE void clts()
Definition: amd64op.h:372
static INLINE void fxsave(struct XSAVEArea *xsa)
Definition: amd64op.h:377
#define CPU
Definition: mp.h:7
#define MAX_CPUS
Definition: kconfig.h:8
#define PGSIZE
Definition: malloc.c:21
#define MEM_USERSPACE_STKLEN
Definition: pmap.h:37
void * memset(void *dst, int c, size_t len)
Definition: string.c:164
Definition: thread.h:31
uintptr_t ustack
Definition: thread.h:35
ThreadArch arch
Definition: thread.h:32
uintptr_t kstack
Definition: thread.h:34
uint16_t cs
Definition: trap.h:73
uint64_t rsi
Definition: trap.h:62
uint16_t ss
Definition: trap.h:79
uint64_t rsp
Definition: trap.h:78
uint64_t rip
Definition: trap.h:72
uint64_t rdi
Definition: trap.h:61
void Trap_Pop(TrapFrame *tf)
uint64_t rflags
Definition: trap.h:77
uint64_t ds
Definition: trap.h:66
uint64_t rdx
Definition: trap.h:63
Definition: trap.h:51
uint64_t uintptr_t
Definition: types.h:16
unsigned long uint64_t
Definition: types.h:13