- kdevelop3 project files
[lightOS.git] / kernel / thread.cpp
bloba70343d19abc37311c8973cd660002ffee584071
1 /*
2 lightOS kernel
3 Copyright (C) 2006-2009 Jörg Pfähler
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 #include <cstddef>
20 #include <kernel/thread.hpp>
21 #include <kernel/scheduler.hpp>
22 #include <lightOS/c++utils.hpp>
23 #include <kernel/processor.hpp>
24 #include <kernel/page_allocator.hpp>
26 #include <kernel/virtual_memory.hpp>
27 using namespace std;
28 using kernel::thread;
29 using kernel::virtual_memory;
31 bool thread::handle_page_fault(void *address)
33 page_allocator &PageAllocator = page_allocator::instance();
34 uintptr_t pfAddress = reinterpret_cast<uintptr_t>(address);
35 if (pfAddress < (reinterpret_cast<uintptr_t>(threadStack) - threadStackSize) &&
36 pfAddress > (reinterpret_cast<uintptr_t>(threadStack) - 1024 * 1024))
38 size_t pages = (reinterpret_cast<uintptr_t>(threadStack) + threadStackSize - pfAddress + virtual_memory::page_size - 1) / virtual_memory::page_size;
40 for (size_t i = 0;i < pages;i++)
42 context &Context = Process.getContext();
43 void *page = PageAllocator.allocate();
44 Process.mPages.push_back(page);
45 Context.map(page,
46 adjust_pointer(threadStack, - threadStackSize - virtual_memory::page_size),
47 lightOS::context::write | lightOS::context::user);
48 threadStackSize += virtual_memory::page_size;
50 return true;
52 return false;
54 void thread::block(state reason)
56 if (reason == run)return;
57 if (State == run)
59 scheduler::remove(this);
61 State = reason;
63 void thread::unblock()
65 if (State == run)return;
66 State = run;
68 scheduler::add(this);
70 thread::~thread()
72 processor::free_fpu_state(this);
74 scheduler::remove(this);