/
processmanager.cpp
161 lines (150 loc) · 4.86 KB
/
processmanager.cpp
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
// PhoeniX OS Process Manager subsystem
// Copyright © 2017 Yury Popov a.k.a. PhoeniX
#include "processmanager.hpp"
#include "acpi.hpp"
void __attribute((naked)) ProcessManager::process_loop() {
asm volatile(
"1: hlt; jmp 1b;"
"process_loop_top:"
);
}
Mutex ProcessManager::managerMutex;
volatile ProcessManager* ProcessManager::manager = nullptr;
ProcessManager* ProcessManager::getManager() {
if (manager) return const_cast<ProcessManager*>(manager);
Mutex::CriticalLock lock(managerMutex);
if (!manager) manager = new ProcessManager();
return const_cast<ProcessManager*>(manager);
}
ProcessManager::ProcessManager() {
nextThread = lastThread = nullptr;
uint64_t cpus = ACPI::getController()->getCPUCount();
cpuThreads = new QueuedThread*[cpus]();
Interrupts::addCallback(0x20, &ProcessManager::TimerHandler);
for (uint8_t i = 0; i < 0x20; i++) {
Interrupts::addCallback(i, &ProcessManager::FaultHandler);
}
}
bool ProcessManager::TimerHandler(uint8_t, uint32_t, Interrupts::CallbackRegs *regs) {
return getManager()->SwitchProcess(regs);
}
bool ProcessManager::FaultHandler(uint8_t intr, uint32_t code, Interrupts::CallbackRegs *regs) {
return getManager()->HandleFault(intr, code, regs);
}
bool ProcessManager::SwitchProcess(Interrupts::CallbackRegs *regs) {
uintptr_t loopbase = uintptr_t(&process_loop), looptop;
asm volatile("lea process_loop_top(%%rip), %q0":"=r"(looptop));
Mutex::Lock lock(processSwitchMutex);
if (regs->dpl == 0 && (regs->rip < loopbase || regs->rip >= looptop)) return false;
QueuedThread *thread = nextThread;
if (thread == nullptr) return false;
nextThread = thread->next;
if (thread == lastThread)
lastThread = nullptr;
if (cpuThreads[regs->cpuid] != nullptr) {
QueuedThread *cth = cpuThreads[regs->cpuid];
Thread *th = cth->thread;
th->regs = {
regs->rip, regs->rflags,
regs->rsi, regs->rdi, regs->rbp, regs->rsp,
regs->rax, regs->rcx, regs->rdx, regs->rbx,
regs->r8, regs->r9, regs->r10, regs->r11,
regs->r12, regs->r13, regs->r14, regs->r15
};
if (lastThread != nullptr) {
lastThread->next = cth;
lastThread = cth;
} else {
nextThread = lastThread = cth;
}
}
cpuThreads[regs->cpuid] = thread;
Thread *th = thread->thread;
*regs = {
regs->cpuid, uintptr_t(thread->process->pagetable),
th->regs.rip, 0x20,
th->regs.rflags,
th->regs.rsp, 0x18,
3,
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
};
return true;
}
bool ProcessManager::HandleFault(uint8_t intr, uint32_t code, Interrupts::CallbackRegs *regs) {
if (regs->dpl == 0) return false;
QueuedThread *thread;
{
Mutex::CriticalLock lock(processSwitchMutex);
thread = cpuThreads[regs->cpuid];
cpuThreads[regs->cpuid] = nullptr;
}
if (!thread) return false;
Interrupts::print(intr, regs, code, thread->process);
delete thread->process;
delete thread;
if (SwitchProcess(regs)) return true;
{
Mutex::CriticalLock lock(processSwitchMutex);
asm volatile("mov %%cr3, %0":"=r"(regs->cr3));
regs->rip = uintptr_t(&process_loop);
regs->cs = 0x08;
regs->ss = 0x10;
regs->dpl = 0;
}
return true;
}
uint64_t ProcessManager::RegisterProcess(Process *process) {
Mutex::CriticalLock lock(processSwitchMutex);
uint64_t pid = 1;
for (size_t i = 0; i < processes.getCount(); i++) {
pid = klib::max(pid, processes[i]->getId() + 1);
}
processes.add(process);
return pid;
}
void ProcessManager::queueThread(Process *process, Thread *thread) {
QueuedThread *q = new QueuedThread();
q->process = process;
q->thread = thread;
q->next = nullptr;
Mutex::CriticalLock lock(processSwitchMutex);
if (lastThread) {
lastThread = (lastThread->next = q);
} else {
lastThread = nextThread = q;
}
}
void ProcessManager::dequeueThread(Thread *thread) {
Mutex::CriticalLock lock(processSwitchMutex);
QueuedThread *next = nextThread, *prev = nullptr;
while (next != nullptr) {
if (next->thread != thread) {
prev = next;
next = next->next;
continue;
}
if (prev == nullptr)
nextThread = next->next;
else
prev->next = next->next;
if (lastThread == next)
lastThread = prev;
delete next;
next = prev ? prev->next : nextThread;
}
for (size_t cpu = 0; cpu < ACPI::getController()->getCPUCount(); cpu++) {
if (!cpuThreads[cpu]) continue;
if (cpuThreads[cpu]->thread != thread) continue;
delete cpuThreads[cpu];
cpuThreads[cpu] = nullptr;
}
}
Process *ProcessManager::currentProcess() {
uint64_t cpuid = ACPI::getController()->getCPUID();
Mutex::CriticalLock lock(processSwitchMutex);
QueuedThread *curr = cpuThreads[cpuid];
return curr ? curr->process : nullptr;
}