Fix TSS kernel stack pointer switching

This commit is contained in:
Ali Mashtizadeh 2014-11-29 15:46:17 -08:00
parent 796a7f0cbb
commit 44195fd034
9 changed files with 49 additions and 9 deletions

View File

@ -4,12 +4,13 @@
int int
main(int argc, const char *argv[]) main(int argc, const char *argv[])
{ {
int i;
char buf[256]; char buf[256];
fputs("Shell\n", stdout); fputs("Shell\n", stdout);
while (1) { while (1) {
fputs("> ", stdout); fputs("> ", stdout);
fgets(buf, sizeof(buf), stdin); //fgets(buf, sizeof(buf), stdin);
} }
} }

View File

@ -19,5 +19,8 @@ int SystemFlush(uint64_t fd);
uint64_t SystemOpen(const char *path, uint64_t flags); uint64_t SystemOpen(const char *path, uint64_t flags);
int SystemClose(uint64_t fd); int SystemClose(uint64_t fd);
// Threads
int SystemThreadSleep(uint64_t time);
#endif /* __SYSCALL_H__ */ #endif /* __SYSCALL_H__ */

View File

@ -79,3 +79,9 @@ SystemClose(uint64_t fd)
return syscall(SYSCALL_CLOSE, fd); return syscall(SYSCALL_CLOSE, fd);
} }
int
SystemThreadSleep(uint64_t time)
{
return syscall(SYSCALL_THREADSLEEP, time);
}

View File

@ -8,10 +8,8 @@ main(int argc, const char *argv[])
SystemSpawn("/bin/shell"); SystemSpawn("/bin/shell");
//while (1) { }
while (1) { while (1) {
asm volatile("int3"); SystemThreadSleep(0);
} }
} }

View File

@ -34,7 +34,7 @@ extern void Loader_LoadInit();
static SegmentDescriptor GDT[MAX_CPUS][GDT_MAX]; static SegmentDescriptor GDT[MAX_CPUS][GDT_MAX];
static PseudoDescriptor GDTDescriptor[MAX_CPUS]; static PseudoDescriptor GDTDescriptor[MAX_CPUS];
static TaskStateSegment64 TSS[MAX_CPUS]; TaskStateSegment64 TSS[MAX_CPUS];
static char df_stack[4096]; static char df_stack[4096];

View File

@ -3,8 +3,10 @@
#include <stdint.h> #include <stdint.h>
#include <string.h> #include <string.h>
#include <sys/kconfig.h>
#include <sys/kassert.h> #include <sys/kassert.h>
#include <sys/kmem.h> #include <sys/kmem.h>
#include <sys/mp.h>
#include <sys/thread.h> #include <sys/thread.h>
#include <machine/amd64.h> #include <machine/amd64.h>
#include <machine/amd64op.h> #include <machine/amd64op.h>
@ -48,6 +50,8 @@ Thread_SetupKThread(Thread *thr, void (*f)(void *), void *arg)
tf->rflags = RFLAGS_IF; tf->rflags = RFLAGS_IF;
} }
extern TaskStateSegment64 TSS[MAX_CPUS];
void void
Thread_SwitchArch(Thread *oldthr, Thread *newthr) Thread_SwitchArch(Thread *oldthr, Thread *newthr)
{ {
@ -63,5 +67,8 @@ Thread_SwitchArch(Thread *oldthr, Thread *newthr)
// Jump to trapframe // Jump to trapframe
switchstack(&oldthr->arch.rsp, newthr->arch.rsp); switchstack(&oldthr->arch.rsp, newthr->arch.rsp);
// Set new RSP0
TSS[CPU()].rsp0 = oldthr->kstack + 4096;
} }

View File

@ -147,6 +147,7 @@ void
Loader_EnterUserLevel(void *arg) Loader_EnterUserLevel(void *arg)
{ {
TrapFrame tf; TrapFrame tf;
memset(&tf, 0, sizeof(tf)); memset(&tf, 0, sizeof(tf));
tf.ds = SEL_UDS | 3; tf.ds = SEL_UDS | 3;
tf.rip = (uint64_t)arg; tf.rip = (uint64_t)arg;
@ -154,6 +155,7 @@ Loader_EnterUserLevel(void *arg)
tf.rsp = 0x70000000 + PGSIZE; tf.rsp = 0x70000000 + PGSIZE;
tf.ss = SEL_UDS | 3; tf.ss = SEL_UDS | 3;
tf.rflags = RFLAGS_IF; tf.rflags = RFLAGS_IF;
Trap_Pop(&tf); Trap_Pop(&tf);
} }

View File

@ -86,11 +86,11 @@ Syscall_Spawn(uint64_t user_path)
handle = Console_OpenHandle(); handle = Console_OpenHandle();
Handle_Add(thr, handle); Handle_Add(thr, handle);
//Loader_Load(thr, file, pg, 1024); Loader_Load(thr, file, pg, 1024);
VFS_Close(file); VFS_Close(file);
//Thread_SetRunnable(thr); Thread_SetRunnable(thr);
return 0; return 0;
} }
@ -216,6 +216,15 @@ Syscall_Close(uint64_t fd)
return (handle->close)(handle); return (handle->close)(handle);
} }
uint64_t
Syscall_ThreadSleep(uint64_t time)
{
// XXX: Support sleeping
Thread_Scheduler();
return 0;
}
uint64_t uint64_t
Syscall_Entry(uint64_t syscall, uint64_t a1, uint64_t a2, Syscall_Entry(uint64_t syscall, uint64_t a1, uint64_t a2,
uint64_t a3, uint64_t a4, uint64_t a5) uint64_t a3, uint64_t a4, uint64_t a5)
@ -249,6 +258,8 @@ Syscall_Entry(uint64_t syscall, uint64_t a1, uint64_t a2,
return Syscall_Open(a1, a2); return Syscall_Open(a1, a2);
case SYSCALL_CLOSE: case SYSCALL_CLOSE:
return Syscall_Close(a1); return Syscall_Close(a1);
case SYSCALL_THREADSLEEP:
return Syscall_ThreadSleep(a1);
default: default:
return (uint64_t)-1; return (uint64_t)-1;
} }

View File

@ -7,6 +7,7 @@
#include <sys/kconfig.h> #include <sys/kconfig.h>
#include <sys/kdebug.h> #include <sys/kdebug.h>
#include <sys/kmem.h> #include <sys/kmem.h>
#include <sys/mp.h>
#include <sys/spinlock.h> #include <sys/spinlock.h>
#include <sys/thread.h> #include <sys/thread.h>
@ -161,9 +162,13 @@ Thread_Scheduler()
Spinlock_Unlock(&threadLock); Spinlock_Unlock(&threadLock);
} }
extern TaskStateSegment64 TSS[MAX_CPUS];
void void
ThreadKThreadEntry(TrapFrame *tf) ThreadKThreadEntry(TrapFrame *tf)
{ {
TSS[CPU()].rsp0 = curProc->kstack + 4096;
Spinlock_Unlock(&threadLock); Spinlock_Unlock(&threadLock);
Trap_Pop(tf); Trap_Pop(tf);
@ -174,7 +179,7 @@ Debug_Threads(int argc, const char *argv[])
{ {
Thread *thr; Thread *thr;
Spinlock_Lock(&threadLock); //Spinlock_Lock(&threadLock);
kprintf("Current: %016llx %d\n", curProc, curProc->ctxSwitches); kprintf("Current: %016llx %d\n", curProc, curProc->ctxSwitches);
TAILQ_FOREACH(thr, &threadQueue, schedQueue) TAILQ_FOREACH(thr, &threadQueue, schedQueue)
@ -182,8 +187,15 @@ Debug_Threads(int argc, const char *argv[])
kprintf("Thread: %016llx %d\n", thr, thr->ctxSwitches); kprintf("Thread: %016llx %d\n", thr, thr->ctxSwitches);
} }
Spinlock_Unlock(&threadLock); //Spinlock_Unlock(&threadLock);
} }
REGISTER_DBGCMD(threads, "Display list of threads", Debug_Threads); REGISTER_DBGCMD(threads, "Display list of threads", Debug_Threads);
void
Debug_ThreadInfo(int argc, const char *argv[])
{
}
REGISTER_DBGCMD(threadinfo, "Display thread state", Debug_ThreadInfo);