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 <kernel/context.hpp>
20 #include <kernel/process.hpp>
21 #include <kernel/page_allocator.hpp>
22 #include <kernel/virtual_memory.hpp>
23 using kernel::context
;
24 using kernel::kernel_context
;
25 using kernel::virtual_memory
;
28 SINGLETON_INSTANCE(kernel::kernel_context
);
31 context::context(context
& c
)
32 : m_handle(), m_heap()
34 SCOPED_LOCK(c
.m_lock
, scope
);
36 m_handle
= c
.m_handle
;
41 context::context(kernel::context_handle_t handle
,
43 : m_handle(handle
), m_heap(heap
)
49 void *context::heap_map(void *paddress
,
53 for (size_t i
= 0;i
< pages
;i
++)
55 map(adjust_pointer(paddress
, i
* virtual_memory::page_size
),
56 adjust_pointer(m_heap
, i
* virtual_memory::page_size
),
60 // Set new heap address
62 m_heap
= adjust_pointer(m_heap
, pages
* virtual_memory::page_size
);
66 void *context::heap_map(size_t pages
,
70 page_allocator
&PageAllocator
= page_allocator::instance();
71 for (size_t i
= 0;i
< pages
;i
++)
73 void *page
= PageAllocator
.allocate();
74 if (Process
!= 0)Process
->mPages
.push_back(page
);
76 adjust_pointer(m_heap
, i
* virtual_memory::page_size
),
80 // Set new heap address
82 m_heap
= adjust_pointer(m_heap
, pages
* virtual_memory::page_size
);
86 void *kernel_context::map_memory_region(void *paddress
, size_t flags
)
88 mLastKernelMemoryRegion
= adjust_pointer(mLastKernelMemoryRegion
, - virtual_memory::page_size
);
90 mLastKernelMemoryRegion
,
92 return mLastKernelMemoryRegion
;
95 void *kernel_context::allocate_memory_region(size_t pages
, size_t flags
)
97 page_allocator
&PageAllocator
= page_allocator::instance();
98 mLastKernelMemoryRegion
= adjust_pointer(mLastKernelMemoryRegion
, - pages
* virtual_memory::page_size
);
99 for (size_t i
= 0;i
< pages
;i
++)
101 void *page
= PageAllocator
.allocate();
103 adjust_pointer(mLastKernelMemoryRegion
, i
* virtual_memory::page_size
),
106 return mLastKernelMemoryRegion
;
109 void kernel_context::free_memory_region(void *address
, size_t pages
)
111 page_allocator
&PageAllocator
= page_allocator::instance();
112 for (size_t i
= 0;i
< pages
;i
++)
114 void *page
= physical_address(adjust_pointer(address
, i
* virtual_memory::page_size
));
115 PageAllocator
.free(page
);
118 if (mLastKernelMemoryRegion
== address
)
119 mLastKernelMemoryRegion
= adjust_pointer(address
, pages
* virtual_memory::page_size
);
122 kernel_context::kernel_context()
123 : context(virtual_memory::kernel_context_handle(), virtual_memory::kernel_binary_end
<void>()),
124 mLastKernelMemoryRegion(virtual_memory::kernel_memory_region_begin
<void>())
128 kernel_context::~kernel_context()