FreeNOS
IntelPaging.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2015 Niek Linnenbank
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include <SplitAllocator.h>
19#include <MemoryBlock.h>
20#include "IntelCore.h"
21#include "IntelPaging.h"
22
24 : MemoryContext(map, alloc)
25 , m_pageDirectory(0)
26 , m_pageDirectoryAddr(0)
27 , m_pageDirectoryAllocated(false)
28{
29}
30
32 : MemoryContext(map, alloc)
33 , m_pageDirectory((IntelPageDirectory *) alloc->toVirtual(pageDirectory))
34 , m_pageDirectoryAddr(pageDirectory)
35 , m_pageDirectoryAllocated(false)
36{
37}
38
46
48{
49 if (m_pageDirectoryAddr != 0)
50 {
52 }
53
54 IntelCore core;
55 Allocator::Range phys, virt;
56
57 phys.address = 0;
58 phys.size = sizeof(IntelPageDirectory);
59 phys.alignment = sizeof(IntelPageDirectory);
60
61 // Allocate page directory from low physical memory.
62 if (m_alloc->allocate(phys, virt) != Allocator::Success)
63 {
65 }
66
70
71 // Initialize the page directory
73
74 // Lookup the currently active page directory
75 IntelPageDirectory *currentDirectory =
77
78 // Inherit kernel mappings. The kernel has permanently mapped 1GB of
79 // physical memory (i.e. the "low memory" in SplitAllocator). The low
80 // memory starts at its physical base address offset (varies per core).
82 m_pageDirectory->copy(currentDirectory,
83 kdata.virt,
84 kdata.virt + kdata.size);
85
86 // Also inherit kernel private mappings, such as APIC mappings.
88 m_pageDirectory->copy(currentDirectory,
89 kdata.virt,
90 kdata.virt + kdata.size);
91
93}
94
96{
97 IntelCore core;
99 m_current = this;
100 return Success;
101}
102
104{
105 MemoryContext::Result r = m_pageDirectory->map(virt, phys, acc, m_alloc);
106
107 // Flush TLB entry
108 if (r == Success && m_current == this)
109 tlb_flush(virt);
110
111 return r;
112}
113
115{
117
118 // Flush TLB entry
119 if (r == Success && m_current == this)
120 tlb_flush(virt);
121
122 return r;
123}
124
126{
127 return m_pageDirectory->translate(virt, phys, m_alloc);
128}
129
134
136 const bool tablesOnly)
137{
138 return m_pageDirectory->releaseSection(range, m_alloc, tablesOnly);
139}
140
Intel CPU Core.
Definition IntelCore.h:258
volatile u32 readCR3() const
Read the CR3 register.
void writeCR3(u32 cr3) const
Write the CR3 register.
Intel page directory implementation.
MemoryContext::Result unmap(Address virt, SplitAllocator *alloc)
Remove virtual address mapping.
MemoryContext::Result translate(Address virt, Address *phys, SplitAllocator *alloc) const
Translate virtual address to physical address.
MemoryContext::Result map(Address virt, Address phys, Memory::Access access, SplitAllocator *alloc)
Map a virtual address to a physical address.
MemoryContext::Result access(Address virt, Memory::Access *access, SplitAllocator *alloc) const
Get Access flags for a virtual address.
MemoryContext::Result releaseRange(const Memory::Range range, SplitAllocator *alloc)
Release range of memory.
MemoryContext::Result releaseSection(const Memory::Range range, SplitAllocator *alloc, const bool tablesOnly)
Release memory sections.
MemoryContext::Result copy(IntelPageDirectory *directory, Address from, Address to)
Copy mappings from another directory.
virtual ~IntelPaging()
Destructor.
virtual Result lookup(Address virt, Address *phys) const
Translate virtual address to physical address.
Address m_pageDirectoryAddr
Physical address of the page directory.
bool m_pageDirectoryAllocated
Set to true if page directory was allocated by this class.
virtual Result map(Address virt, Address phys, Memory::Access access)
Map a physical page to a virtual address.
IntelPaging(MemoryMap *map, SplitAllocator *alloc)
Constructor.
virtual Result initialize()
Initialize the MemoryContext.
IntelPageDirectory * m_pageDirectory
Pointer to page directory in kernel's virtual memory.
virtual Result releaseSection(const Memory::Range &range, const bool tablesOnly=false)
Release memory sections.
virtual Result activate(bool initializeMMU=false)
Activate the MemoryContext.
virtual Result access(Address virt, Memory::Access *access) const
Get Access flags for a virtual address.
virtual Result unmap(Address virt)
Unmap a virtual address.
virtual Result releaseRange(Memory::Range *range)
Release range of memory.
static void * set(void *dest, int ch, unsigned count)
Fill memory with a constant byte.
Virtual memory abstract interface.
Result
Result codes.
MemoryMap * m_map
Virtual memory layout.
SplitAllocator * m_alloc
Physical memory allocator.
static MemoryContext * m_current
The currently active MemoryContext.
Describes virtual memory map layout.
Definition MemoryMap.h:39
@ KernelPrivate
< Kernel dynamic memory mappings
Definition MemoryMap.h:55
@ KernelData
< Kernel program data from libexec, e.g.
Definition MemoryMap.h:54
Memory::Range range(Region region) const
Get memory range for the given region.
Definition MemoryMap.cpp:36
Allocator which separates kernel mapped memory at virtual and physical addresses.
virtual Result allocate(Range &args)
Allocate physical memory.
Address toVirtual(const Address phys) const
Convert Address to virtual pointer.
virtual Result release(const Address addr)
Release memory page.
#define tlb_flush(addr)
Flushes the Translation Lookaside Buffers (TLB) for a single page.
Definition IntelCore.h:92
unsigned long Address
A memory address.
Definition Types.h:131
Access
Memory access flags.
Definition Memory.h:39
Describes a range of memory.
Definition Allocator.h:66
Size alignment
Alignment in bytes or ZERO for default alignment.
Definition Allocator.h:69
Address address
Starting address of the memory range.
Definition Allocator.h:67
Size size
Amount of memory in bytes.
Definition Allocator.h:68
Memory range.
Definition Memory.h:56
Size size
Size in number of bytes.
Definition Memory.h:59
Address virt
Virtual address.
Definition Memory.h:57