MemoryManager.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. #include "MemoryManager.h"
  2. #include <AK/Assertions.h>
  3. #include <AK/kstdio.h>
  4. #include <AK/kmalloc.h>
  5. #include "i386.h"
  6. #include "StdLib.h"
  7. #include "Process.h"
  8. //#define MM_DEBUG
  9. #define SCRUB_DEALLOCATED_PAGE_TABLES
  10. static MemoryManager* s_the;
  11. MemoryManager& MM
  12. {
  13. return *s_the;
  14. }
  15. MemoryManager::MemoryManager()
  16. {
  17. m_kernel_page_directory = (PageDirectory*)0x4000;
  18. m_pageTableZero = (dword*)0x6000;
  19. m_pageTableOne = (dword*)0x7000;
  20. m_next_laddr.set(0xd0000000);
  21. initializePaging();
  22. }
  23. MemoryManager::~MemoryManager()
  24. {
  25. }
  26. void MemoryManager::populate_page_directory(Process& process)
  27. {
  28. memset(process.m_page_directory, 0, sizeof(PageDirectory));
  29. process.m_page_directory->entries[0] = m_kernel_page_directory->entries[0];
  30. process.m_page_directory->entries[1] = m_kernel_page_directory->entries[1];
  31. }
  32. void MemoryManager::release_page_directory(Process& process)
  33. {
  34. ASSERT_INTERRUPTS_DISABLED();
  35. #ifdef MM_DEBUG
  36. dbgprintf("MM: release_page_directory for pid %d, PD K%x\n", process.pid(), process.m_page_directory);
  37. #endif
  38. for (size_t i = 0; i < 1024; ++i) {
  39. auto page_table = process.m_page_directory->physical_addresses[i];
  40. if (!page_table.is_null()) {
  41. #ifdef MM_DEBUG
  42. dbgprintf("MM: deallocating process page table [%u] P%x @ %p\n", i, page_table.get(), &process.m_page_directory->physical_addresses[i]);
  43. #endif
  44. deallocate_page_table(page_table);
  45. }
  46. }
  47. #ifdef SCRUB_DEALLOCATED_PAGE_TABLES
  48. memset(process.m_page_directory, 0xc9, sizeof(PageDirectory));
  49. #endif
  50. }
  51. void MemoryManager::initializePaging()
  52. {
  53. static_assert(sizeof(MemoryManager::PageDirectoryEntry) == 4);
  54. static_assert(sizeof(MemoryManager::PageTableEntry) == 4);
  55. memset(m_pageTableZero, 0, PAGE_SIZE);
  56. memset(m_pageTableOne, 0, PAGE_SIZE);
  57. memset(m_kernel_page_directory, 0, sizeof(PageDirectory));
  58. #ifdef MM_DEBUG
  59. kprintf("MM: Kernel page directory @ %p\n", m_kernel_page_directory);
  60. #endif
  61. // Make null dereferences crash.
  62. protectMap(LinearAddress(0), PAGE_SIZE);
  63. // The bottom 4 MB are identity mapped & supervisor only. Every process shares these mappings.
  64. create_identity_mapping(LinearAddress(PAGE_SIZE), 4 * MB);
  65. // The physical pages 4 MB through 8 MB are available for Zone allocation.
  66. for (size_t i = (4 * MB) + PAGE_SIZE; i < (8 * MB); i += PAGE_SIZE)
  67. m_freePages.append(PhysicalAddress(i));
  68. asm volatile("movl %%eax, %%cr3"::"a"(m_kernel_page_directory));
  69. asm volatile(
  70. "movl %cr0, %eax\n"
  71. "orl $0x80000001, %eax\n"
  72. "movl %eax, %cr0\n"
  73. );
  74. }
  75. PhysicalAddress MemoryManager::allocate_page_table()
  76. {
  77. auto ppages = allocatePhysicalPages(1);
  78. dword address = ppages[0].get();
  79. create_identity_mapping(LinearAddress(address), PAGE_SIZE);
  80. memset((void*)address, 0, PAGE_SIZE);
  81. return PhysicalAddress(address);
  82. }
  83. void MemoryManager::deallocate_page_table(PhysicalAddress paddr)
  84. {
  85. ASSERT(!m_freePages.contains_slow(paddr));
  86. remove_identity_mapping(LinearAddress(paddr.get()), PAGE_SIZE);
  87. m_freePages.append(paddr);
  88. }
  89. void MemoryManager::remove_identity_mapping(LinearAddress laddr, size_t size)
  90. {
  91. InterruptDisabler disabler;
  92. // FIXME: ASSERT(laddr is 4KB aligned);
  93. for (dword offset = 0; offset < size; offset += PAGE_SIZE) {
  94. auto pte_address = laddr.offset(offset);
  95. auto pte = ensurePTE(m_kernel_page_directory, pte_address);
  96. pte.setPhysicalPageBase(0);
  97. pte.setUserAllowed(false);
  98. pte.setPresent(true);
  99. pte.setWritable(true);
  100. flushTLB(pte_address);
  101. }
  102. }
  103. auto MemoryManager::ensurePTE(PageDirectory* page_directory, LinearAddress laddr) -> PageTableEntry
  104. {
  105. ASSERT_INTERRUPTS_DISABLED();
  106. dword page_directory_index = (laddr.get() >> 22) & 0x3ff;
  107. dword page_table_index = (laddr.get() >> 12) & 0x3ff;
  108. PageDirectoryEntry pde = PageDirectoryEntry(&page_directory->entries[page_directory_index]);
  109. if (!pde.isPresent()) {
  110. #ifdef MM_DEBUG
  111. dbgprintf("MM: PDE %u not present, allocating\n", page_directory_index);
  112. #endif
  113. if (page_directory_index == 0) {
  114. ASSERT(page_directory == m_kernel_page_directory);
  115. pde.setPageTableBase((dword)m_pageTableZero);
  116. pde.setUserAllowed(false);
  117. pde.setPresent(true);
  118. pde.setWritable(true);
  119. } else if (page_directory_index == 1) {
  120. ASSERT(page_directory == m_kernel_page_directory);
  121. pde.setPageTableBase((dword)m_pageTableOne);
  122. pde.setUserAllowed(false);
  123. pde.setPresent(true);
  124. pde.setWritable(true);
  125. } else {
  126. auto page_table = allocate_page_table();
  127. #ifdef MM_DEBUG
  128. dbgprintf("MM: PD K%x (%s) allocated page table #%u (for L%x) at P%x\n",
  129. page_directory,
  130. page_directory == m_kernel_page_directory ? "Kernel" : "User",
  131. page_directory_index,
  132. laddr.get(),
  133. page_table);
  134. #endif
  135. if (page_table.get() == 0x71d000)
  136. ASSERT(page_directory == m_kernel_page_directory);
  137. page_directory->physical_addresses[page_directory_index] = page_table;
  138. pde.setPageTableBase(page_table.get());
  139. pde.setUserAllowed(true);
  140. pde.setPresent(true);
  141. pde.setWritable(true);
  142. }
  143. }
  144. return PageTableEntry(&pde.pageTableBase()[page_table_index]);
  145. }
  146. void MemoryManager::protectMap(LinearAddress linearAddress, size_t length)
  147. {
  148. InterruptDisabler disabler;
  149. // FIXME: ASSERT(linearAddress is 4KB aligned);
  150. for (dword offset = 0; offset < length; offset += PAGE_SIZE) {
  151. auto pteAddress = linearAddress.offset(offset);
  152. auto pte = ensurePTE(m_kernel_page_directory, pteAddress);
  153. pte.setPhysicalPageBase(pteAddress.get());
  154. pte.setUserAllowed(false);
  155. pte.setPresent(false);
  156. pte.setWritable(false);
  157. flushTLB(pteAddress);
  158. }
  159. }
  160. void MemoryManager::create_identity_mapping(LinearAddress laddr, size_t size)
  161. {
  162. InterruptDisabler disabler;
  163. // FIXME: ASSERT(laddr is 4KB aligned);
  164. for (dword offset = 0; offset < size; offset += PAGE_SIZE) {
  165. auto pteAddress = laddr.offset(offset);
  166. auto pte = ensurePTE(m_kernel_page_directory, pteAddress);
  167. pte.setPhysicalPageBase(pteAddress.get());
  168. pte.setUserAllowed(false);
  169. pte.setPresent(true);
  170. pte.setWritable(true);
  171. flushTLB(pteAddress);
  172. }
  173. }
  174. void MemoryManager::initialize()
  175. {
  176. s_the = new MemoryManager;
  177. }
  178. PageFaultResponse MemoryManager::handlePageFault(const PageFault& fault)
  179. {
  180. ASSERT_INTERRUPTS_DISABLED();
  181. kprintf("MM: handlePageFault(%w) at L%x\n", fault.code(), fault.address().get());
  182. if (fault.isNotPresent()) {
  183. kprintf(" >> NP fault!\n");
  184. } else if (fault.isProtectionViolation()) {
  185. kprintf(" >> PV fault!\n");
  186. }
  187. return PageFaultResponse::ShouldCrash;
  188. }
  189. void MemoryManager::registerZone(Zone& zone)
  190. {
  191. ASSERT_INTERRUPTS_DISABLED();
  192. m_zones.set(&zone);
  193. #ifdef MM_DEBUG
  194. for (size_t i = 0; i < zone.m_pages.size(); ++i)
  195. dbgprintf("MM: allocated to zone: P%x\n", zone.m_pages[i].get());
  196. #endif
  197. }
  198. void MemoryManager::unregisterZone(Zone& zone)
  199. {
  200. ASSERT_INTERRUPTS_DISABLED();
  201. #ifdef MM_DEBUG
  202. for (size_t i = 0; i < zone.m_pages.size(); ++i)
  203. dbgprintf("MM: deallocated from zone: P%x\n", zone.m_pages[i].get());
  204. #endif
  205. m_zones.remove(&zone);
  206. m_freePages.append(move(zone.m_pages));
  207. }
  208. Zone::Zone(Vector<PhysicalAddress>&& pages)
  209. : m_pages(move(pages))
  210. {
  211. MM.registerZone(*this);
  212. }
  213. Zone::~Zone()
  214. {
  215. MM.unregisterZone(*this);
  216. }
  217. RetainPtr<Zone> MemoryManager::createZone(size_t size)
  218. {
  219. InterruptDisabler disabler;
  220. auto pages = allocatePhysicalPages(ceilDiv(size, PAGE_SIZE));
  221. if (pages.isEmpty()) {
  222. kprintf("MM: createZone: no physical pages for size %u\n", size);
  223. return nullptr;
  224. }
  225. return adopt(*new Zone(move(pages)));
  226. }
  227. Vector<PhysicalAddress> MemoryManager::allocatePhysicalPages(size_t count)
  228. {
  229. InterruptDisabler disabler;
  230. if (count > m_freePages.size())
  231. return { };
  232. Vector<PhysicalAddress> pages;
  233. pages.ensureCapacity(count);
  234. for (size_t i = 0; i < count; ++i) {
  235. pages.append(m_freePages.takeLast());
  236. #ifdef MM_DEBUG
  237. dbgprintf("MM: allocate_physical_pages vending P%x\n", pages.last());
  238. #endif
  239. }
  240. return pages;
  241. }
  242. void MemoryManager::enter_kernel_paging_scope()
  243. {
  244. InterruptDisabler disabler;
  245. current->m_tss.cr3 = (dword)m_kernel_page_directory;
  246. asm volatile("movl %%eax, %%cr3"::"a"(m_kernel_page_directory):"memory");
  247. }
  248. void MemoryManager::enter_process_paging_scope(Process& process)
  249. {
  250. InterruptDisabler disabler;
  251. current->m_tss.cr3 = (dword)process.m_page_directory;
  252. asm volatile("movl %%eax, %%cr3"::"a"(process.m_page_directory):"memory");
  253. }
  254. void MemoryManager::flushEntireTLB()
  255. {
  256. asm volatile(
  257. "mov %cr3, %eax\n"
  258. "mov %eax, %cr3\n"
  259. );
  260. }
  261. void MemoryManager::flushTLB(LinearAddress laddr)
  262. {
  263. asm volatile("invlpg %0": :"m" (*(char*)laddr.get()) : "memory");
  264. }
  265. void MemoryManager::map_region_at_address(PageDirectory* page_directory, Region& region, LinearAddress laddr, bool user_allowed)
  266. {
  267. InterruptDisabler disabler;
  268. auto& zone = *region.zone;
  269. for (size_t i = 0; i < zone.m_pages.size(); ++i) {
  270. auto page_laddr = laddr.offset(i * PAGE_SIZE);
  271. auto pte = ensurePTE(page_directory, page_laddr);
  272. pte.setPhysicalPageBase(zone.m_pages[i].get());
  273. pte.setPresent(true);
  274. pte.setWritable(true);
  275. pte.setUserAllowed(user_allowed);
  276. flushTLB(page_laddr);
  277. #ifdef MM_DEBUG
  278. dbgprintf("MM: >> map_region_at_address (PD=%x) L%x => P%x\n", page_directory, page_laddr, zone.m_pages[i].get());
  279. #endif
  280. }
  281. }
  282. void MemoryManager::unmap_range(PageDirectory* page_directory, LinearAddress laddr, size_t size)
  283. {
  284. ASSERT((size % PAGE_SIZE) == 0);
  285. InterruptDisabler disabler;
  286. size_t numPages = size / PAGE_SIZE;
  287. for (size_t i = 0; i < numPages; ++i) {
  288. auto page_laddr = laddr.offset(i * PAGE_SIZE);
  289. auto pte = ensurePTE(page_directory, page_laddr);
  290. pte.setPhysicalPageBase(0);
  291. pte.setPresent(false);
  292. pte.setWritable(false);
  293. pte.setUserAllowed(false);
  294. flushTLB(page_laddr);
  295. #ifdef MM_DEBUG
  296. dbgprintf("MM: << unmap_range L%x =/> 0\n", page_laddr);
  297. #endif
  298. }
  299. }
  300. LinearAddress MemoryManager::allocate_linear_address_range(size_t size)
  301. {
  302. ASSERT((size % PAGE_SIZE) == 0);
  303. // FIXME: Recycle ranges!
  304. auto laddr = m_next_laddr;
  305. m_next_laddr.set(m_next_laddr.get() + size);
  306. return laddr;
  307. }
  308. byte* MemoryManager::create_kernel_alias_for_region(Region& region)
  309. {
  310. InterruptDisabler disabler;
  311. #ifdef MM_DEBUG
  312. dbgprintf("MM: create_kernel_alias_for_region region=%p (L%x size=%u)\n", &region, region.linearAddress.get(), region.size);
  313. #endif
  314. auto laddr = allocate_linear_address_range(region.size);
  315. map_region_at_address(m_kernel_page_directory, region, laddr, false);
  316. #ifdef MM_DEBUG
  317. dbgprintf("MM: Created alias L%x for L%x\n", laddr.get(), region.linearAddress.get());
  318. #endif
  319. return laddr.asPtr();
  320. }
  321. void MemoryManager::remove_kernel_alias_for_region(Region& region, byte* addr)
  322. {
  323. #ifdef MM_DEBUG
  324. dbgprintf("remove_kernel_alias_for_region region=%p, addr=L%x\n", &region, addr);
  325. #endif
  326. unmap_range(m_kernel_page_directory, LinearAddress((dword)addr), region.size);
  327. }
  328. bool MemoryManager::unmapRegion(Process& process, Region& region)
  329. {
  330. InterruptDisabler disabler;
  331. auto& zone = *region.zone;
  332. for (size_t i = 0; i < zone.m_pages.size(); ++i) {
  333. auto laddr = region.linearAddress.offset(i * PAGE_SIZE);
  334. auto pte = ensurePTE(process.m_page_directory, laddr);
  335. pte.setPhysicalPageBase(0);
  336. pte.setPresent(false);
  337. pte.setWritable(false);
  338. pte.setUserAllowed(false);
  339. flushTLB(laddr);
  340. #ifdef MM_DEBUG
  341. //dbgprintf("MM: >> Unmapped L%x => P%x <<\n", laddr, zone.m_pages[i].get());
  342. #endif
  343. }
  344. return true;
  345. }
  346. bool MemoryManager::unmapSubregion(Process& process, Subregion& subregion)
  347. {
  348. InterruptDisabler disabler;
  349. size_t numPages = subregion.size / PAGE_SIZE;
  350. ASSERT(numPages);
  351. for (size_t i = 0; i < numPages; ++i) {
  352. auto laddr = subregion.linearAddress.offset(i * PAGE_SIZE);
  353. auto pte = ensurePTE(process.m_page_directory, laddr);
  354. pte.setPhysicalPageBase(0);
  355. pte.setPresent(false);
  356. pte.setWritable(false);
  357. pte.setUserAllowed(false);
  358. flushTLB(laddr);
  359. #ifdef MM_DEBUG
  360. //dbgprintf("MM: >> Unmapped subregion %s L%x => P%x <<\n", subregion.name.characters(), laddr, zone.m_pages[i].get());
  361. #endif
  362. }
  363. return true;
  364. }
  365. bool MemoryManager::mapSubregion(Process& process, Subregion& subregion)
  366. {
  367. InterruptDisabler disabler;
  368. auto& region = *subregion.region;
  369. auto& zone = *region.zone;
  370. size_t firstPage = subregion.offset / PAGE_SIZE;
  371. size_t numPages = subregion.size / PAGE_SIZE;
  372. ASSERT(numPages);
  373. for (size_t i = 0; i < numPages; ++i) {
  374. auto laddr = subregion.linearAddress.offset(i * PAGE_SIZE);
  375. auto pte = ensurePTE(process.m_page_directory, laddr);
  376. pte.setPhysicalPageBase(zone.m_pages[firstPage + i].get());
  377. pte.setPresent(true);
  378. pte.setWritable(true);
  379. pte.setUserAllowed(true);
  380. flushTLB(laddr);
  381. #ifdef MM_DEBUG
  382. //dbgprintf("MM: >> Mapped subregion %s L%x => P%x (%u into region)\n", subregion.name.characters(), laddr, zone.m_pages[firstPage + i].get(), subregion.offset);
  383. #endif
  384. }
  385. return true;
  386. }
  387. bool MemoryManager::mapRegion(Process& process, Region& region)
  388. {
  389. map_region_at_address(process.m_page_directory, region, region.linearAddress, true);
  390. return true;
  391. }
  392. bool MemoryManager::validate_user_read(const Process& process, LinearAddress laddr) const
  393. {
  394. dword pageDirectoryIndex = (laddr.get() >> 22) & 0x3ff;
  395. dword pageTableIndex = (laddr.get() >> 12) & 0x3ff;
  396. auto pde = PageDirectoryEntry(&process.m_page_directory->entries[pageDirectoryIndex]);
  397. if (!pde.isPresent())
  398. return false;
  399. auto pte = PageTableEntry(&pde.pageTableBase()[pageTableIndex]);
  400. if (!pte.isPresent())
  401. return false;
  402. if (!pte.isUserAllowed())
  403. return false;
  404. return true;
  405. }
  406. bool MemoryManager::validate_user_write(const Process& process, LinearAddress laddr) const
  407. {
  408. dword pageDirectoryIndex = (laddr.get() >> 22) & 0x3ff;
  409. dword pageTableIndex = (laddr.get() >> 12) & 0x3ff;
  410. auto pde = PageDirectoryEntry(&process.m_page_directory->entries[pageDirectoryIndex]);
  411. if (!pde.isPresent())
  412. return false;
  413. auto pte = PageTableEntry(&pde.pageTableBase()[pageTableIndex]);
  414. if (!pte.isPresent())
  415. return false;
  416. if (!pte.isUserAllowed())
  417. return false;
  418. if (!pte.isWritable())
  419. return false;
  420. return true;
  421. }
  422. RetainPtr<Region> Region::clone()
  423. {
  424. InterruptDisabler disabler;
  425. KernelPagingScope pagingScope;
  426. // FIXME: Implement COW regions.
  427. auto clone_zone = MM.createZone(zone->size());
  428. auto clone_region = adopt(*new Region(linearAddress, size, move(clone_zone), String(name)));
  429. // FIXME: It would be cool to make the src_alias a read-only mapping.
  430. byte* src_alias = MM.create_kernel_alias_for_region(*this);
  431. byte* dest_alias = MM.create_kernel_alias_for_region(*clone_region);
  432. memcpy(dest_alias, src_alias, size);
  433. MM.remove_kernel_alias_for_region(*clone_region, dest_alias);
  434. MM.remove_kernel_alias_for_region(*this, src_alias);
  435. return clone_region;
  436. }