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
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>
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
);
46 adjust_pointer(threadStack
, - threadStackSize
- virtual_memory::page_size
),
47 lightOS::context::write
| lightOS::context::user
);
48 threadStackSize
+= virtual_memory::page_size
;
54 void thread::block(state reason
)
56 if (reason
== run
)return;
59 scheduler::remove(this);
63 void thread::unblock()
65 if (State
== run
)return;
72 processor::free_fpu_state(this);
74 scheduler::remove(this);