CS350 COS
COS
Loading...
Searching...
No Matches
thread.c File Reference
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include <sys/kconfig.h>
#include <sys/kassert.h>
#include <sys/kmem.h>
#include <sys/mp.h>
#include <sys/thread.h>
#include <machine/amd64.h>
#include <machine/amd64op.h>
#include <machine/trap.h>
#include <machine/pmap.h>
Include dependency graph for thread.c:

Go to the source code of this file.

Functions

void ThreadKThreadEntry (TrapFrame *tf)
 
void switchstack (uint64_t *oldrsp, uint64_t rsp)
 
void Thread_InitArch (Thread *thr)
 
void Thread_SetupKThread (Thread *thr, void(*f)(), uintptr_t arg1, uintptr_t arg2, uintptr_t arg3)
 
static void ThreadEnterUserLevelCB (uintptr_t arg1, uintptr_t arg2, uintptr_t arg3)
 
void Thread_SetupUThread (Thread *thr, uintptr_t rip, uintptr_t arg)
 
void Thread_SwitchArch (Thread *oldthr, Thread *newthr)
 

Variables

TaskStateSegment64 TSS [MAX_CPUS]
 

Function Documentation

◆ switchstack()

void switchstack ( uint64_t oldrsp,
uint64_t  rsp 
)
Here is the caller graph for this function:

◆ Thread_InitArch()

void Thread_InitArch ( Thread thr)

Definition at line 20 of file thread.c.

21{
22 thr->arch.useFP = true;
23}
bool useFP
Definition: thread.h:21
ThreadArch arch
Definition: thread.h:32
Here is the caller graph for this function:

◆ Thread_SetupKThread()

void Thread_SetupKThread ( Thread thr,
void(*)()  f,
uintptr_t  arg1,
uintptr_t  arg2,
uintptr_t  arg3 
)

Definition at line 26 of file thread.c.

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}
uint64_t rsp
Definition: thread.h:22
uint64_t rip
Definition: thread.h:16
uint64_t rdi
Definition: thread.h:14
void ThreadKThreadEntry(TrapFrame *tf)
Definition: thread.c:314
#define RFLAGS_IF
Definition: amd64.h:180
#define SEL_KCS
Definition: amd64.h:80
#define PGSIZE
Definition: malloc.c:21
void * memset(void *dst, int c, size_t len)
Definition: string.c:164
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
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
unsigned long uint64_t
Definition: types.h:13
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Thread_SetupUThread()

void Thread_SetupUThread ( Thread thr,
uintptr_t  rip,
uintptr_t  arg 
)

Definition at line 75 of file thread.c.

76{
78 thr->ustack, arg);
79}
void Thread_SetupKThread(Thread *thr, void(*f)(), uintptr_t arg1, uintptr_t arg2, uintptr_t arg3)
Definition: thread.c:26
static void ThreadEnterUserLevelCB(uintptr_t arg1, uintptr_t arg2, uintptr_t arg3)
Definition: thread.c:58
uintptr_t ustack
Definition: thread.h:35
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Thread_SwitchArch()

void Thread_SwitchArch ( Thread oldthr,
Thread newthr 
)

Definition at line 84 of file thread.c.

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}
XSAVEArea xsa
Definition: thread.h:20
void switchstack(uint64_t *oldrsp, uint64_t rsp)
TaskStateSegment64 TSS[MAX_CPUS]
Definition: machine.c:40
uint64_t rsp0
Definition: amd64.h:108
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
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ThreadEnterUserLevelCB()

static void ThreadEnterUserLevelCB ( uintptr_t  arg1,
uintptr_t  arg2,
uintptr_t  arg3 
)
static

Definition at line 58 of file thread.c.

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}
#define SEL_UDS
Definition: amd64.h:84
#define SEL_UCS
Definition: amd64.h:83
#define MEM_USERSPACE_STKLEN
Definition: pmap.h:37
void Trap_Pop(TrapFrame *tf)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ThreadKThreadEntry()

void ThreadKThreadEntry ( TrapFrame tf)

Definition at line 314 of file thread.c.

315{
316 TSS[CPU()].rsp0 = curProc[CPU()]->kstack + 4096;
317
319
320 Trap_Pop(tf);
321}
Thread * curProc[MAX_CPUS]
Definition: sched.c:41
Spinlock schedLock
Definition: sched.c:29
void Spinlock_Unlock(Spinlock *lock) __UNLOCK_EX(*lock)
Definition: spinlock.c:109
Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ TSS

Definition at line 40 of file machine.c.