summaryrefslogtreecommitdiff
path: root/Source/Kernel/TaskManager/Thread.class.cpp
blob: 6d62474a4d5e99329ea27ff578ca431f9c5d5904 (plain) (blame)
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
#include "Thread.class.h"
#include <TaskManager/Task.ns.h>
#include <MemoryManager/PageAlloc.ns.h>
#include <DeviceManager/Time.ns.h>

void runThread(Thread* thread, void* data, thread_entry_t entry_point) {
	asm volatile("sti");
	u32int ret = entry_point(data);	//Run !
	asm volatile("mov %0, %%eax; int $66;" : : "r"(ret));	//Syscall for thread ending
}

Thread::Thread() {	//Private constructor, does nothing
}

Thread::Thread(thread_entry_t entry_point, void* data, bool iskernel) {
	if (iskernel) {
		setup(Task::getKernelProcess(), entry_point, data, true);
	} else {
		setup(Task::currProcess(), entry_point, data, false);
	}
}

Thread::Thread(Process* process, thread_entry_t entry_point, void* data) {
	setup(process, entry_point, data, false);
}

Thread::~Thread() {
	Task::unregisterThread(this);
	Mem::kfree(m_kernelStack.addr);
	if (!m_isKernel) m_process->heap().free(m_userStack.addr);
	//Don't unregister thread in process, it has probably already been done
}

void Thread::setup(Process* process, thread_entry_t entry_point, void* data, bool isKernel) {
	DEBUG("new Thread :: setup");
	m_isKernel = isKernel;
	m_process = process;
	m_kernelStack.addr = Mem::kalloc(STACKSIZE);
	m_kernelStack.size = STACKSIZE;

	if (m_isKernel) {
		m_userStack.size = 0;
		m_userStack.addr = 0;
	} else {
		m_userStack.addr = m_process->heap().alloc(STACKSIZE);
		m_userStack.size = STACKSIZE;
	}
	u32int* stack = (u32int*)((u32int)(m_kernelStack.addr) + m_kernelStack.size);

	//Pass function parameters for runThread()
	stack--;
	*stack = (u32int)entry_point;	//Push entry point (function parameter)
	stack--;
	*stack = (u32int)data;	//Push data
	stack--;
	*stack = (u32int)this;	//Push object pointer
	stack--;
	*stack = 0;	//Null return address
	m_esp = (u32int)stack;
	m_ebp = m_esp + 8;
	m_eip = (u32int)runThread;

	m_state = T_RUNNING;
	m_process->registerThread(this);
	Task::registerThread(this);
}

void Thread::finish(u32int errcode) {
	if (errcode == E_PAGEFAULT and m_isKernel) {
		PANIC("Page fault in kernel thread !");
	}
	if (errcode == E_UNHANDLED_EXCEPTION and m_isKernel) {
		PANIC("Unhandled exception in kernel thread !");
	}
	//Needs not set m_state to a finished state, either :
	// - thread is unregistered from process and everywhere
	// - errcode is an exception or this is main thread, process exits
	m_process->threadFinishes(this, errcode);
} 

void Thread::handleException(registers_t regs, int no) {
	char* exceptions[] = {
		"Division by zero", "Debug exception", "Non maskable interrupt", 
		"Breakpoint exception", "'Into detected overflow'", "Out of bounds exception",
		"Invalid opcode exception", "No coprocessor exception", "Double fault",
		"Coprocessor segment overrun", "Bad TSS", "Segment not present",
		"Stack fault", "General protection fault", "Page fault",
		"Unknown", "Coprocessor fault", "Alignement check exception",
		"Machine check exception", "", "",
		"", "", "",
		"", "", "",
		"", "", "",
		"", ""};

	*(m_process->m_vt) << "\nUnhandled exception " << (s32int)no << " at " << (u32int)regs.cs << ":" <<
		(u32int)regs.eip << "\n:: " << exceptions[no];
	if (m_isKernel) PANIC_DUMP("Exception in kernel thread", &regs);

	if (no == 14) {	//Page fault
		int present = !(regs.err_code & 0x1);
		int rw = regs.err_code & 0x2;
		int us = regs.err_code & 0x4;
		int rsvd = regs.err_code & 0x8;
		u32int faddr;
		asm volatile("mov %%cr2, %0" : "=r"(faddr));
		*(m_process->m_vt) << "\n   ";
		if (present) *(m_process->m_vt) << "Present ";
		if (rw) *(m_process->m_vt) << "R/W ";
		if (us) *(m_process->m_vt) << "User ";
		if (rsvd) *(m_process->m_vt) << "Rsvd ";
		*(m_process->m_vt) << "At:" << (u32int)faddr;

		*(m_process->m_vt) << "\nThread finishing.\n";
		finish(E_PAGEFAULT);
		return;
	}
	*(m_process->m_vt) << "\nThread finishing.\n";
	finish(E_UNHANDLED_EXCEPTION);
}

void Thread::setState(u32int esp, u32int ebp, u32int eip) {
	m_esp = esp;
	m_ebp = ebp;
	m_eip = eip;
}

u32int Thread::getEsp() { return m_esp; }
u32int Thread::getEbp() { return m_ebp; }
u32int Thread::getEip() { return m_eip; }

Process* Thread::getProcess() { return m_process; }

void Thread::sleep(u32int msecs) {
	m_state = T_SLEEPING;
	waitfor.m_time = Time::time() + msecs;

	Task::triggerSwitch();
}

void Thread::waitIRQ(u8int irq) {
	m_state = T_IRQWAIT;
	waitfor.m_irq = irq;

	Task::triggerSwitch();
}

bool Thread::runnable() {
	if (m_state == T_RUNNING) return true;
	if (m_state == T_SLEEPING and Time::time() >= waitfor.m_time) {
		m_state = T_RUNNING;
		return true;
	}
	return false;
}