Skip to content

Commit

Permalink
Remove “null threads”
Browse files Browse the repository at this point in the history
  • Loading branch information
djphoenix committed Nov 22, 2016
1 parent 0215689 commit 776aabf
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 33 deletions.
2 changes: 0 additions & 2 deletions src/process/include/processmanager.hpp
Expand Up @@ -34,7 +34,6 @@ class ProcessManager {
ProcessManager();
QueuedThread *nextThread, *lastThread;
QueuedThread **cpuThreads;
Thread *nullThreads;
List<Process*> processes;
Mutex processSwitchMutex;
bool SwitchProcess(intcb_regs *regs);
Expand All @@ -44,7 +43,6 @@ class ProcessManager {

public:
uint64_t RegisterProcess(Process *process);
void createNullThread(uint32_t cpuid, Thread thread);
void queueThread(Process *process, Thread *thread);
void dequeueThread(Thread *thread);
static ProcessManager* getManager();
Expand Down
50 changes: 19 additions & 31 deletions src/process/processmanager.cpp
Expand Up @@ -19,13 +19,13 @@
#include "acpi.hpp"

void ProcessManager::process_loop() {
uint32_t cpuid = ACPI::getController()->getCPUID();
Thread nullThread;
asm volatile("movq %%rsp, %0":"=m"(nullThread.regs.rsp));
asm volatile("leaq _loop(%%rip), %0":"=r"(nullThread.regs.rip));
ProcessManager::getManager()->createNullThread(cpuid, nullThread);
asm volatile("_loop:");
for (;;) asm volatile("hlt");
asm volatile(
"_loop:"
"hlt;"
"jmp _loop;"
"_loop_top:"
);
for (;;) {}
}

Mutex ProcessManager::managerMutex;
Expand All @@ -48,8 +48,6 @@ ProcessManager::ProcessManager() {
cpuThreads = new QueuedThread*[cpus]();
for (uint64_t c = 0; c < cpus; c++)
cpuThreads[c] = 0;
nullThreads = new Thread[cpus]();

Interrupts::addCallback(0x20, &ProcessManager::TimerHandler);
for (int i = 0; i < 0x20; i++) {
Interrupts::addCallback(i, &ProcessManager::FaultHandler);
Expand All @@ -62,16 +60,15 @@ bool ProcessManager::FaultHandler(
uint32_t intr, uint32_t code, intcb_regs *regs) {
return getManager()->HandleFault(intr, code, regs);
}
void ProcessManager::createNullThread(uint32_t cpuid, Thread thread) {
uint64_t t = EnterCritical();
processSwitchMutex.lock();
nullThreads[cpuid] = thread;
processSwitchMutex.release();
LeaveCritical(t);
}
bool ProcessManager::SwitchProcess(intcb_regs *regs) {
processSwitchMutex.lock();
if (nullThreads[regs->cpuid].regs.rip == 0) {
uintptr_t loopbase, looptop;
asm volatile(
"lea _loop(%%rip), %0;"
"lea _loop_top(%%rip), %1":
"=r"(loopbase),"=r"(looptop));
if (regs->dpl == 0 &&
(regs->rip < loopbase || regs->rip >= looptop)) {
processSwitchMutex.release();
return false;
}
Expand Down Expand Up @@ -144,20 +141,11 @@ bool ProcessManager::HandleFault(
return true;
t = EnterCritical();
processSwitchMutex.lock();
Thread *th = &nullThreads[regs->cpuid];
uintptr_t pagetable = 0;
asm volatile("mov %%cr3, %0":"=r"(pagetable));
*regs = {
regs->cpuid, pagetable,
th->regs.rip, 0x08,
th->regs.rflags,
th->regs.rsp, 0x10,
0,
th->regs.rax, th->regs.rcx, th->regs.rdx, th->regs.rbx,
th->regs.rbp, th->regs.rsi, th->regs.rdi,
th->regs.r8, th->regs.r9, th->regs.r10, th->regs.r11,
th->regs.r12, th->regs.r13, th->regs.r14, th->regs.r15
};
asm volatile("mov %%cr3, %0":"=r"(regs->cr3));
asm volatile("lea _loop(%%rip), %0":"=r"(regs->rip));
regs->cs = 0x08;
regs->ss = 0x10;
regs->dpl = 0;
processSwitchMutex.release();
LeaveCritical(t);
return true;
Expand Down

0 comments on commit 776aabf

Please sign in to comment.