MCPcopy Create free account
hub / github.com/LemonOSProject/LemonOS / CreateAddressSpace

Function CreateAddressSpace

Kernel/src/arch/x86_64/paging.cpp:109–155  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

107 }
108
109 address_space_t* CreateAddressSpace(){
110 address_space_t* addressSpace = (address_space_t*)kmalloc(sizeof(address_space_t));
111
112 pdpt_entry_t* pdpt = (pdpt_entry_t*)Memory::KernelAllocate4KPages(1); // PDPT;
113 uintptr_t pdptPhys = Memory::AllocatePhysicalMemoryBlock();
114 Memory::KernelMapVirtualMemory4K(pdptPhys, (uintptr_t)pdpt,1);
115 memset((pdpt_entry_t*)pdpt,0,4096);
116
117 pd_entry_t** pageDirs = (pd_entry_t**)KernelAllocate4KPages(1); // Page Dirs
118 Memory::KernelMapVirtualMemory4K(Memory::AllocatePhysicalMemoryBlock(), (uintptr_t)pageDirs,1);
119 uint64_t* pageDirsPhys = (uint64_t*)KernelAllocate4KPages(1); // Page Dirs
120 Memory::KernelMapVirtualMemory4K(Memory::AllocatePhysicalMemoryBlock(), (uintptr_t)pageDirsPhys,1);
121 page_t*** pageTables = (page_t***)KernelAllocate4KPages(1); // Page Tables
122 Memory::KernelMapVirtualMemory4K(Memory::AllocatePhysicalMemoryBlock(), (uintptr_t)pageTables,1);
123
124 pml4_entry_t* pml4 = (pml4_entry_t*)KernelAllocate4KPages(1); // Page Tables
125 uintptr_t pml4Phys = Memory::AllocatePhysicalMemoryBlock();
126 Memory::KernelMapVirtualMemory4K(pml4Phys, (uintptr_t)pml4,1);
127 memcpy(pml4, kernelPML4, 4096);
128
129 for(int i = 0; i < 512; i++){
130 pageDirs[i] = (pd_entry_t*)KernelAllocate4KPages(1);
131 pageDirsPhys[i] = Memory::AllocatePhysicalMemoryBlock();
132 KernelMapVirtualMemory4K(pageDirsPhys[i],(uintptr_t)pageDirs[i],1);
133
134 pageTables[i] = (page_t**)Memory::KernelAllocate4KPages(1);
135 KernelMapVirtualMemory4K(Memory::AllocatePhysicalMemoryBlock(),(uintptr_t)pageTables[i],1);
136
137 SetPageFrame(&(pdpt[i]),pageDirsPhys[i]);
138 pdpt[i] |= PDPT_WRITABLE | PDPT_PRESENT | PDPT_USER;
139
140 memset(pageDirs[i],0,4096);
141 memset(pageTables[i],0,4096);
142 }
143
144 addressSpace->pageDirs = pageDirs;
145 addressSpace->pageDirsPhys = pageDirsPhys;
146 addressSpace->pageTables = pageTables;
147 addressSpace->pml4 = pml4;
148 addressSpace->pdptPhys = pdptPhys;
149 addressSpace->pml4Phys = pml4Phys;
150 addressSpace->pdpt = pdpt;
151
152 pml4[0] = pdptPhys | PML4_PRESENT | PML4_WRITABLE | PAGE_USER;
153
154 return addressSpace;
155 }
156
157 void DestroyAddressSpace(address_space_t* addressSpace){
158 for(int i = 0; i < DIRS_PER_PDPT; i++){

Callers 1

Calls 7

kmallocFunction · 0.85
KernelAllocate4KPagesFunction · 0.85
KernelMapVirtualMemory4KFunction · 0.85
memsetFunction · 0.85
memcpyFunction · 0.85
SetPageFrameFunction · 0.85

Tested by

no test coverage detected