#include <stdbool.h>
#include <stdint.h>
#include <sys/kconfig.h>
#include <sys/kassert.h>
#include <sys/kdebug.h>
#include <sys/kmem.h>
#include <machine/amd64.h>
#include <machine/amd64op.h>
#include <machine/mp.h>
#include <machine/pmap.h>
Go to the source code of this file.
|
void | PMap_Init () |
|
void | PMap_InitAP () |
|
AS * | PMap_NewAS () |
|
void | PMap_DestroyAS (AS *space) |
|
AS * | PMap_CurrentAS () |
|
void | PMap_LoadAS (AS *space) |
|
static PageTable * | PMapAllocPageTable () |
|
uintptr_t | PMap_Translate (AS *space, uintptr_t va) |
|
static void | PMapLookupEntry (AS *space, uint64_t va, PageEntry **entry, int size) |
|
bool | PMap_Map (AS *as, uint64_t phys, uint64_t virt, uint64_t pages, uint64_t flags) |
|
bool | PMap_Unmap (AS *as, uint64_t va, uint64_t pages) |
|
bool | PMap_AllocMap (AS *as, uint64_t virt, uint64_t len, uint64_t flags) |
|
void | PMap_SystemLookup (uint64_t va, PageEntry **entry, int size) |
|
bool | PMap_SystemLMap (uint64_t phys, uint64_t virt, uint64_t lpages, uint64_t flags) |
|
bool | PMap_SystemMap (uint64_t phys, uint64_t virt, uint64_t pages, uint64_t flags) |
|
bool | PMap_SystemUnmap (uint64_t virt, uint64_t pages) |
|
static uint64_t | AddrFromIJKL (uint64_t i, uint64_t j, uint64_t k, uint64_t l) |
|
void | PMap_DumpFull (AS *space) |
|
static void | Debug_PMapDumpFull (int argc, const char *argv[]) |
|
| REGISTER_DBGCMD (pmapdumpfull, "Dump memory mappings", Debug_PMapDumpFull) |
|
void | PMap_Dump (AS *space) |
|
static void | Debug_PMapDump (int argc, const char *argv[]) |
|
| REGISTER_DBGCMD (pmapdump, "Dump memory mappings", Debug_PMapDump) |
|
◆ AddrFromIJKL()
◆ Debug_PMapDump()
static void Debug_PMapDump |
( |
int |
argc, |
|
|
const char * |
argv[] |
|
) |
| |
|
static |
Definition at line 669 of file pmap.c.
670{
672}
void PMap_Dump(AS *space)
◆ Debug_PMapDumpFull()
static void Debug_PMapDumpFull |
( |
int |
argc, |
|
|
const char * |
argv[] |
|
) |
| |
|
static |
Definition at line 608 of file pmap.c.
609{
611}
void PMap_DumpFull(AS *space)
◆ PMap_AllocMap()
PMap_AllocMap –
Map a virtual mapping in an address space and back it by newly allocated memory.
- Parameters
-
[in] | as | Address space. |
[in] | virt | Virtual address. |
[in] | pages | Pages to map in. |
[in] | flags | Flags to apply to the mapping. |
- Return values
-
true | On success |
false | On failure |
Definition at line 423 of file pmap.c.
424{
425 int i;
428
430
431 for (i = 0; i < pages; i++) {
434 if (!entry) {
435 kprintf(
"Map failed to allocate memory!\n");
436 return false;
437 }
438
442 }
443 }
444
445 return true;
446}
int kprintf(const char *fmt,...)
void * PAlloc_AllocPage()
static void PMapLookupEntry(AS *space, uint64_t va, PageEntry **entry, int size)
◆ PMap_CurrentAS()
PMap_CurrentAS –
Get the current address space on this CPU.
- Returns
- Current address space.
Definition at line 168 of file pmap.c.
◆ PMap_DestroyAS()
void PMap_DestroyAS |
( |
AS * |
space | ) |
|
PMap_DestroyAS –
Destroys an address space and releases the physical pages.
- Parameters
-
[in] | space | Address space to destroy. |
Definition at line 116 of file pmap.c.
117{
118
120 {
123
137
139 }
140 }
141
142
144 }
145 }
146
147
149 }
150 }
151
152
154 }
155 }
156
158}
PageEntry entries[PAGETABLE_ENTRIES]
#define PAGETABLE_ENTRIES
void PAlloc_Release(void *pg)
◆ PMap_Dump()
void PMap_Dump |
( |
AS * |
space | ) |
|
Definition at line 616 of file pmap.c.
617{
618 int i = 0;
619 int j = 0;
620 int k = 0;
621 int l = 0;
623
624 kprintf(
"%-18s %-18s %-5s\n",
"Virtual",
"Physical PTE",
"Flags");
628
630 continue;
631
635
637 continue;
638
642
644 continue;
645
646 if ((pte3 &
PTE_PS) == 0) {
649
651 kprintf(
"0x%016llx: 0x%016llx P%c%c%c%c%c\n",
654 (pte4 &
PTE_W) ?
'W' :
' ',
655 (pte4 &
PTE_NX) ?
' ' :
'X',
656 (pte4 &
PTE_U) ?
'U' :
' ',
657 (pte4 &
PTE_A) ?
'A' :
' ',
658 (pte4 &
PTE_D) ?
'D' :
' ');
659 }
660 }
661 }
662 }
663 }
664
665 return;
666}
static uint64_t AddrFromIJKL(uint64_t i, uint64_t j, uint64_t k, uint64_t l)
◆ PMap_DumpFull()
void PMap_DumpFull |
( |
AS * |
space | ) |
|
Definition at line 552 of file pmap.c.
553{
554 int i = 0;
555 int j = 0;
556 int k = 0;
557 int l = 0;
559
561
565
567 continue;
568
570
574
576 continue;
577
579
583
585 continue;
586
587 kprintf(
"Level 3: %016llx:%016llx\n",
590
591 if ((pte3 &
PTE_PS) == 0) {
594
595 kprintf(
"Level 4: %016llx:%016llx\n",
598 }
599 }
600 }
601 }
602 }
603
604 return;
605}
◆ PMap_Init()
Definition at line 19 of file pmap.c.
20{
21 int i, j;
22
23 kprintf(
"Initializing PMAP ... ");
24
25
28 }
29
30
35 PANIC(
"Cannot allocate system page table");
36
39
43 if (!pgtbl)
44 PANIC(
"Not enough memory!");
45
47
50 }
51 }
52
53
55 3*512, 0);
59 60*512, 0);
60
62
64}
bool PMap_SystemLMap(uint64_t phys, uint64_t virt, uint64_t lpages, uint64_t flags)
void PMap_LoadAS(AS *space)
#define MEM_DIRECTMAP_BASE
◆ PMap_InitAP()
◆ PMap_LoadAS()
void PMap_LoadAS |
( |
AS * |
space | ) |
|
PMap_LoadAS –
Load an address space into the CPU. Reloads the CR3 register in x86-64 that points the physical page tables and flushes the TLB entries.
- Parameters
-
[in] | space | Address space to load. |
Definition at line 182 of file pmap.c.
183{
186}
static INLINE void write_cr3(uint64_t val)
◆ PMap_Map()
PMap_Map –
Map a physical to virtual mapping in an address space.
- Parameters
-
[in] | as | Address space. |
[in] | phys | Physical address. |
[in] | virt | Virtual address. |
[in] | pages | Pages to map in. |
[in] | flags | Flags to apply to the mapping. |
- Return values
-
true | On success |
false | On failure |
Definition at line 355 of file pmap.c.
356{
357 int i;
359
360 for (i = 0; i < pages; i++) {
363 if (!entry) {
364 kprintf(
"Map failed to allocate memory!\n");
365 return false;
366 }
367
369 }
370
371 return true;
372}
◆ PMap_NewAS()
PMap_NewAS –
Create a new address space.
- Returns
- Newly created address space.
Definition at line 80 of file pmap.c.
81{
82 int i;
84
85 if (!as)
87
91
95 }
96
98 {
100 }
103 }
104
105 return as;
106}
◆ PMap_SystemLMap()
PMap_SystemLMap –
Map a range of large (2MB) physical pages to virtual pages in the kernel address space that is shared by all processes.
- Parameters
-
[in] | phys | Physical address. |
[in] | virt | Virtual address. |
[in] | lpages | Large pages to map in. |
[in] | flags | Flags to apply to the mapping. |
- Return values
-
true | On success |
false | On failure |
Definition at line 480 of file pmap.c.
481{
482 int i;
484
485 for (i = 0; i < lpages; i++) {
488 if (!entry) {
489 kprintf(
"SystemLMap failed to allocate memory!\n");
490 return false;
491 }
492
494 }
495
496 return true;
497}
◆ PMap_SystemLookup()
PMap_SystemLookup –
Lookup a kernel virtual address in a page table and return a pointer to the page entry. This function allocates page tables as necessary to fill in the 4-level heirarchy.
- Parameters
-
[in] | va | Virtual address to lookup. |
[out] | entry | Pointer will point to the PageEntry. |
[in] | size | Page size we want to use. |
Definition at line 460 of file pmap.c.
◆ PMap_SystemMap()
PMap_SystemLMap –
Map a range of physical pages to virtual pages in the kernel address space that is shared by all processes.
- Parameters
-
[in] | phys | Physical address. |
[in] | virt | Virtual address. |
[in] | pages | Pages to map in. |
[in] | flags | Flags to apply to the mapping. |
- Return values
-
true | On success |
false | On failure |
Definition at line 514 of file pmap.c.
515{
516 int i;
518
519 for (i = 0; i < pages; i++) {
522 if (!entry) {
523 kprintf(
"SystemMap failed to allocate memory!\n");
524 return false;
525 }
526
528 }
529
530 return true;
531}
◆ PMap_SystemUnmap()
PMap_SystemUnmap –
We do not currently use this!
Definition at line 539 of file pmap.c.
540{
542 return false;
543}
#define NOT_IMPLEMENTED()
◆ PMap_Translate()
PMap_Translate –
Translates a virtual address to physical address for a given address space.
- Parameters
-
[in] | space | Address space we wish to lookup a mapping in. |
[in] | va | Virtual address we wish to translate. |
Definition at line 220 of file pmap.c.
221{
222 int i,j,k,l;
226
231
233 if (pte == 0) {
235 return 0;
236 }
238
240
241 if (pte == 0) {
243 return 0;
244 }
246
249
252 }
253 if (pte == 0) {
255 return 0;
256 }
258
259
261
263}
◆ PMap_Unmap()
PMap_Unmap –
Unmap a range of addresses.
- Parameters
-
[in] | as | Address space. |
[in] | va | Virtual address. |
[in] | pages | Pages to map in. |
- Return values
-
true | On success |
false | On failure |
Definition at line 387 of file pmap.c.
388{
389 int i;
391
392 for (i = 0; i < pages; i++) {
395 if (!entry) {
396 kprintf(
"Unmap tried to allocate memory!\n");
397 return false;
398 }
399
401
402 *entry = 0;
403 }
404
405 return true;
406}
◆ PMapAllocPageTable()
PMapAllocPageTable –
Allocates and initializes a page table.
- Returns
- Newly created PageTable.
Definition at line 196 of file pmap.c.
197{
198 int i;
200
201 if (!pgtbl)
202 return 0;
203
206 }
207
208 return pgtbl;
209}
◆ PMapLookupEntry()
PMapLookupEntry –
Lookup a virtual address in a page table and return a pointer to the page entry. This function allocates page tables as necessary to fill in the 4-level heirarchy.
- Parameters
-
[in] | space | Address space to search. |
[in] | va | Virtual address to lookup. |
[out] | entry | Pointer will point to the PageEntry. |
[in] | size | Page size we want to use. |
Definition at line 278 of file pmap.c.
279{
280 int i,j,k,l;
283
288
290
292 if (pte == 0) {
294 if (!newtable)
295 return;
296
299 }
301
304
306 return;
307 }
308 if (pte == 0) {
310 if (!newtable)
311 return;
312
315 }
317
320
322 return;
323 }
324 if (pte == 0) {
326 if (!newtable)
327 return;
328
331 }
333
334
337 return;
338}
static PageTable * PMapAllocPageTable()
◆ REGISTER_DBGCMD() [1/2]
REGISTER_DBGCMD |
( |
pmapdump |
, |
|
|
"Dump memory mappings" |
, |
|
|
Debug_PMapDump |
|
|
) |
| |
◆ REGISTER_DBGCMD() [2/2]
◆ currentAS
◆ systemAS