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 
40 {
42  {
44  }
45 }
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 
131 {
132  return m_pageDirectory->access(virt, access, m_alloc);
133 }
134 
136  const bool tablesOnly)
137 {
138  return m_pageDirectory->releaseSection(range, m_alloc, tablesOnly);
139 }
140 
142 {
143  return m_pageDirectory->releaseRange(*range, m_alloc);
144 }
SplitAllocator::allocate
virtual Result allocate(Range &args)
Allocate physical memory.
Definition: SplitAllocator.cpp:35
IntelPageDirectory::releaseSection
MemoryContext::Result releaseSection(const Memory::Range range, SplitAllocator *alloc, const bool tablesOnly)
Release memory sections.
Definition: IntelPageDirectory.cpp:189
IntelPaging::access
virtual Result access(Address virt, Memory::Access *access) const
Get Access flags for a virtual address.
Definition: IntelPaging.cpp:130
Memory::Range
Memory range.
Definition: Memory.h:55
SplitAllocator.h
IntelPaging::releaseSection
virtual Result releaseSection(const Memory::Range &range, const bool tablesOnly=false)
Release memory sections.
Definition: IntelPaging.cpp:135
MemoryContext
Virtual memory abstract interface.
Definition: MemoryContext.h:42
IntelCore::writeCR3
void writeCR3(u32 cr3) const
Write the CR3 register.
Definition: IntelCore.cpp:158
MemoryBlock::set
static void * set(void *dest, int ch, unsigned count)
Fill memory with a constant byte.
Definition: MemoryBlock.cpp:25
IntelPageDirectory::unmap
MemoryContext::Result unmap(Address virt, SplitAllocator *alloc)
Remove virtual address mapping.
Definition: IntelPageDirectory.cpp:92
Address
unsigned long Address
A memory address.
Definition: Types.h:131
Allocator::Range::address
Address address
Starting address of the memory range.
Definition: Allocator.h:67
IntelPageDirectory::copy
MemoryContext::Result copy(IntelPageDirectory *directory, Address from, Address to)
Copy mappings from another directory.
Definition: IntelPageDirectory.cpp:52
MemoryBlock.h
IntelPaging.h
Allocator::Range::alignment
Size alignment
Alignment in bytes or ZERO for default alignment.
Definition: Allocator.h:69
IntelCore::readCR3
volatile u32 readCR3() const
Read the CR3 register.
Definition: IntelCore.cpp:150
IntelPaging::m_pageDirectoryAllocated
bool m_pageDirectoryAllocated
Set to true if page directory was allocated by this class.
Definition: IntelPaging.h:168
IntelPageDirectory::translate
MemoryContext::Result translate(Address virt, Address *phys, SplitAllocator *alloc) const
Translate virtual address to physical address.
Definition: IntelPageDirectory.cpp:101
Allocator::Range::size
Size size
Amount of memory in bytes.
Definition: Allocator.h:68
SplitAllocator
Allocator which separates kernel mapped memory at virtual and physical addresses.
Definition: SplitAllocator.h:37
IntelPaging::m_pageDirectoryAddr
Address m_pageDirectoryAddr
Physical address of the page directory.
Definition: IntelPaging.h:165
IntelPaging::m_pageDirectory
IntelPageDirectory * m_pageDirectory
Pointer to page directory in kernel's virtual memory.
Definition: IntelPaging.h:162
IntelPaging::unmap
virtual Result unmap(Address virt)
Unmap a virtual address.
Definition: IntelPaging.cpp:114
IntelPaging::releaseRange
virtual Result releaseRange(Memory::Range *range)
Release range of memory.
Definition: IntelPaging.cpp:141
Allocator::Success
@ Success
Definition: Allocator.h:55
MemoryContext::Result
Result
Result codes.
Definition: MemoryContext.h:49
IntelPaging::map
virtual Result map(Address virt, Address phys, Memory::Access access)
Map a physical page to a virtual address.
Definition: IntelPaging.cpp:103
MemoryMap::range
Memory::Range range(Region region) const
Get memory range for the given region.
Definition: MemoryMap.cpp:36
MemoryContext::Success
@ Success
Definition: MemoryContext.h:51
MemoryMap
Describes virtual memory map layout.
Definition: MemoryMap.h:38
IntelPageDirectory::access
MemoryContext::Result access(Address virt, Memory::Access *access, SplitAllocator *alloc) const
Get Access flags for a virtual address.
Definition: IntelPageDirectory.cpp:122
MemoryMap::KernelPrivate
@ KernelPrivate
< Kernel dynamic memory mappings
Definition: MemoryMap.h:55
Memory::Access
Access
Memory access flags.
Definition: Memory.h:38
tlb_flush
#define tlb_flush(addr)
Flushes the Translation Lookaside Buffers (TLB) for a single page.
Definition: IntelCore.h:92
IntelPaging::~IntelPaging
virtual ~IntelPaging()
Destructor.
Definition: IntelPaging.cpp:39
MemoryContext::OutOfMemory
@ OutOfMemory
Definition: MemoryContext.h:55
IntelCore.h
Allocator::Range
Describes a range of memory.
Definition: Allocator.h:65
IntelPageDirectory
Intel page directory implementation.
Definition: IntelPageDirectory.h:42
IntelPaging::initialize
virtual Result initialize()
Initialize the MemoryContext.
Definition: IntelPaging.cpp:47
SplitAllocator::release
virtual Result release(const Address addr)
Release memory page.
Definition: SplitAllocator.cpp:89
MemoryContext::m_current
static MemoryContext * m_current
The currently active MemoryContext.
Definition: MemoryContext.h:233
IntelPaging::activate
virtual Result activate(bool initializeMMU=false)
Activate the MemoryContext.
Definition: IntelPaging.cpp:95
Memory::Range::virt
Address virt
Virtual address.
Definition: Memory.h:57
IntelPageDirectory::releaseRange
MemoryContext::Result releaseRange(const Memory::Range range, SplitAllocator *alloc)
Release range of memory.
Definition: IntelPageDirectory.cpp:163
IntelPageDirectory::map
MemoryContext::Result map(Address virt, Address phys, Memory::Access access, SplitAllocator *alloc)
Map a virtual address to a physical address.
Definition: IntelPageDirectory.cpp:64
Memory::Range::size
Size size
Size in number of bytes.
Definition: Memory.h:59
IntelCore
Intel CPU Core.
Definition: IntelCore.h:257
IntelPaging::lookup
virtual Result lookup(Address virt, Address *phys) const
Translate virtual address to physical address.
Definition: IntelPaging.cpp:125
IntelPaging::IntelPaging
IntelPaging(MemoryMap *map, SplitAllocator *alloc)
Constructor.
Definition: IntelPaging.cpp:23
MemoryMap::KernelData
@ KernelData
< Kernel program data from libexec, e.g.
Definition: MemoryMap.h:54
SplitAllocator::toVirtual
Address toVirtual(const Address phys) const
Convert Address to virtual pointer.
Definition: SplitAllocator.cpp:94
MemoryContext::m_alloc
SplitAllocator * m_alloc
Physical memory allocator.
Definition: MemoryContext.h:227
MemoryContext::m_map
MemoryMap * m_map
Virtual memory layout.
Definition: MemoryContext.h:230