《操作系统》的实验代码。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

665 lines
22 KiB

  1. #include <defs.h>
  2. #include <x86.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <mmu.h>
  6. #include <memlayout.h>
  7. #include <pmm.h>
  8. #include <default_pmm.h>
  9. #include <sync.h>
  10. #include <error.h>
  11. //#include <swap.h>
  12. //#include <vmm.h>
  13. #include <kmalloc.h>
  14. /* *
  15. * Task State Segment:
  16. *
  17. * The TSS may reside anywhere in memory. A special segment register called
  18. * the Task Register (TR) holds a segment selector that points a valid TSS
  19. * segment descriptor which resides in the GDT. Therefore, to use a TSS
  20. * the following must be done in function gdt_init:
  21. * - create a TSS descriptor entry in GDT
  22. * - add enough information to the TSS in memory as needed
  23. * - load the TR register with a segment selector for that segment
  24. *
  25. * There are several fileds in TSS for specifying the new stack pointer when a
  26. * privilege level change happens. But only the fields SS0 and ESP0 are useful
  27. * in our os kernel.
  28. *
  29. * The field SS0 contains the stack segment selector for CPL = 0, and the ESP0
  30. * contains the new ESP value for CPL = 0. When an interrupt happens in protected
  31. * mode, the x86 CPU will look in the TSS for SS0 and ESP0 and load their value
  32. * into SS and ESP respectively.
  33. * */
  34. static struct taskstate ts = {0};
  35. // virtual address of physicall page array
  36. struct Page *pages;
  37. // amount of physical memory (in pages)
  38. size_t npage = 0;
  39. // virtual address of boot-time page directory
  40. pde_t *boot_pgdir = NULL;
  41. // physical address of boot-time page directory
  42. uintptr_t boot_cr3;
  43. // physical memory management
  44. const struct pmm_manager *pmm_manager;
  45. /* *
  46. * The page directory entry corresponding to the virtual address range
  47. * [VPT, VPT + PTSIZE) points to the page directory itself. Thus, the page
  48. * directory is treated as a page table as well as a page directory.
  49. *
  50. * One result of treating the page directory as a page table is that all PTEs
  51. * can be accessed though a "virtual page table" at virtual address VPT. And the
  52. * PTE for number n is stored in vpt[n].
  53. *
  54. * A second consequence is that the contents of the current page directory will
  55. * always available at virtual address PGADDR(PDX(VPT), PDX(VPT), 0), to which
  56. * vpd is set bellow.
  57. * */
  58. pte_t * const vpt = (pte_t *)VPT;
  59. pde_t * const vpd = (pde_t *)PGADDR(PDX(VPT), PDX(VPT), 0);
  60. /* *
  61. * Global Descriptor Table:
  62. *
  63. * The kernel and user segments are identical (except for the DPL). To load
  64. * the %ss register, the CPL must equal the DPL. Thus, we must duplicate the
  65. * segments for the user and the kernel. Defined as follows:
  66. * - 0x0 : unused (always faults -- for trapping NULL far pointers)
  67. * - 0x8 : kernel code segment
  68. * - 0x10: kernel data segment
  69. * - 0x18: user code segment
  70. * - 0x20: user data segment
  71. * - 0x28: defined for tss, initialized in gdt_init
  72. * */
  73. static struct segdesc gdt[] = {
  74. SEG_NULL,
  75. [SEG_KTEXT] = SEG(STA_X | STA_R, 0x0, 0xFFFFFFFF, DPL_KERNEL),
  76. [SEG_KDATA] = SEG(STA_W, 0x0, 0xFFFFFFFF, DPL_KERNEL),
  77. [SEG_UTEXT] = SEG(STA_X | STA_R, 0x0, 0xFFFFFFFF, DPL_USER),
  78. [SEG_UDATA] = SEG(STA_W, 0x0, 0xFFFFFFFF, DPL_USER),
  79. [SEG_TSS] = SEG_NULL,
  80. };
  81. static struct pseudodesc gdt_pd = {
  82. sizeof(gdt) - 1, (uintptr_t)gdt
  83. };
  84. static void check_alloc_page(void);
  85. static void check_pgdir(void);
  86. static void check_boot_pgdir(void);
  87. /* *
  88. * lgdt - load the global descriptor table register and reset the
  89. * data/code segement registers for kernel.
  90. * */
  91. static inline void
  92. lgdt(struct pseudodesc *pd) {
  93. asm volatile ("lgdt (%0)" :: "r" (pd));
  94. asm volatile ("movw %%ax, %%gs" :: "a" (USER_DS));
  95. asm volatile ("movw %%ax, %%fs" :: "a" (USER_DS));
  96. asm volatile ("movw %%ax, %%es" :: "a" (KERNEL_DS));
  97. asm volatile ("movw %%ax, %%ds" :: "a" (KERNEL_DS));
  98. asm volatile ("movw %%ax, %%ss" :: "a" (KERNEL_DS));
  99. // reload cs
  100. asm volatile ("ljmp %0, $1f\n 1:\n" :: "i" (KERNEL_CS));
  101. }
  102. /* *
  103. * load_esp0 - change the ESP0 in default task state segment,
  104. * so that we can use different kernel stack when we trap frame
  105. * user to kernel.
  106. * */
  107. void
  108. load_esp0(uintptr_t esp0) {
  109. ts.ts_esp0 = esp0;
  110. }
  111. /* gdt_init - initialize the default GDT and TSS */
  112. static void
  113. gdt_init(void) {
  114. // set boot kernel stack and default SS0
  115. load_esp0((uintptr_t)bootstacktop);
  116. ts.ts_ss0 = KERNEL_DS;
  117. // initialize the TSS filed of the gdt
  118. gdt[SEG_TSS] = SEGTSS(STS_T32A, (uintptr_t)&ts, sizeof(ts), DPL_KERNEL);
  119. // reload all segment registers
  120. lgdt(&gdt_pd);
  121. // load the TSS
  122. ltr(GD_TSS);
  123. }
  124. //init_pmm_manager - initialize a pmm_manager instance
  125. static void
  126. init_pmm_manager(void) {
  127. pmm_manager = &default_pmm_manager;
  128. cprintf("memory management: %s\n", pmm_manager->name);
  129. pmm_manager->init();
  130. }
  131. //init_memmap - call pmm->init_memmap to build Page struct for free memory
  132. static void
  133. init_memmap(struct Page *base, size_t n) {
  134. pmm_manager->init_memmap(base, n);
  135. }
  136. //alloc_pages - call pmm->alloc_pages to allocate a continuous n*PAGESIZE memory
  137. struct Page *
  138. alloc_pages(size_t n) {
  139. struct Page *page=NULL;
  140. bool intr_flag;
  141. local_intr_save(intr_flag);
  142. page = pmm_manager->alloc_pages(n);
  143. local_intr_restore(intr_flag);
  144. if (page == NULL )
  145. panic("alloc_pages: NO FREE PAGES!!!\n");
  146. //cprintf("n %d,get page %x, No %d in alloc_pages\n",n,page,(page-pages));
  147. return page;
  148. }
  149. //free_pages - call pmm->free_pages to free a continuous n*PAGESIZE memory
  150. void
  151. free_pages(struct Page *base, size_t n) {
  152. bool intr_flag;
  153. local_intr_save(intr_flag);
  154. {
  155. pmm_manager->free_pages(base, n);
  156. }
  157. local_intr_restore(intr_flag);
  158. }
  159. //nr_free_pages - call pmm->nr_free_pages to get the size (nr*PAGESIZE)
  160. //of current free memory
  161. size_t
  162. nr_free_pages(void) {
  163. size_t ret;
  164. bool intr_flag;
  165. local_intr_save(intr_flag);
  166. {
  167. ret = pmm_manager->nr_free_pages();
  168. }
  169. local_intr_restore(intr_flag);
  170. return ret;
  171. }
  172. /* pmm_init - initialize the physical memory management */
  173. static void
  174. page_init(void) {
  175. struct e820map *memmap = (struct e820map *)(0x8000 + KERNBASE);
  176. uint64_t maxpa = 0;
  177. cprintf("e820map:\n");
  178. int i;
  179. for (i = 0; i < memmap->nr_map; i ++) {
  180. uint64_t begin = memmap->map[i].addr, end = begin + memmap->map[i].size;
  181. cprintf(" memory: %08llx, [%08llx, %08llx], type = %d.\n",
  182. memmap->map[i].size, begin, end - 1, memmap->map[i].type);
  183. if (memmap->map[i].type == E820_ARM) {
  184. if (maxpa < end && begin < KMEMSIZE) {
  185. maxpa = end;
  186. }
  187. }
  188. }
  189. if (maxpa > KMEMSIZE) {
  190. maxpa = KMEMSIZE;
  191. }
  192. extern char end[];
  193. npage = maxpa / PGSIZE;
  194. pages = (struct Page *)ROUNDUP((void *)end, PGSIZE);
  195. for (i = 0; i < npage; i ++) {
  196. SetPageReserved(pages + i);
  197. }
  198. uintptr_t freemem = PADDR((uintptr_t)pages + sizeof(struct Page) * npage);
  199. for (i = 0; i < memmap->nr_map; i ++) {
  200. uint64_t begin = memmap->map[i].addr, end = begin + memmap->map[i].size;
  201. if (memmap->map[i].type == E820_ARM) {
  202. if (begin < freemem) {
  203. begin = freemem;
  204. }
  205. if (end > KMEMSIZE) {
  206. end = KMEMSIZE;
  207. }
  208. if (begin < end) {
  209. begin = ROUNDUP(begin, PGSIZE);
  210. end = ROUNDDOWN(end, PGSIZE);
  211. if (begin < end) {
  212. init_memmap(pa2page(begin), (end - begin) / PGSIZE);
  213. }
  214. }
  215. }
  216. }
  217. }
  218. static void
  219. enable_paging(void) {
  220. lcr3(boot_cr3);
  221. // turn on paging
  222. uint32_t cr0 = rcr0();
  223. cr0 |= CR0_PE | CR0_PG | CR0_AM | CR0_WP | CR0_NE | CR0_TS | CR0_EM | CR0_MP;
  224. cr0 &= ~(CR0_TS | CR0_EM);
  225. lcr0(cr0);
  226. }
  227. //boot_map_segment - setup&enable the paging mechanism
  228. // parameters
  229. // la: linear address of this memory need to map (after x86 segment map)
  230. // size: memory size
  231. // pa: physical address of this memory
  232. // perm: permission of this memory
  233. static void
  234. boot_map_segment(pde_t *pgdir, uintptr_t la, size_t size, uintptr_t pa, uint32_t perm) {
  235. assert(PGOFF(la) == PGOFF(pa));
  236. size_t n = ROUNDUP(size + PGOFF(la), PGSIZE) / PGSIZE;
  237. la = ROUNDDOWN(la, PGSIZE);
  238. pa = ROUNDDOWN(pa, PGSIZE);
  239. for (; n > 0; n --, la += PGSIZE, pa += PGSIZE) {
  240. pte_t *ptep = get_pte(pgdir, la, 1);
  241. assert(ptep != NULL);
  242. *ptep = pa | PTE_P | perm;
  243. }
  244. }
  245. //boot_alloc_page - allocate one page using pmm->alloc_pages(1)
  246. // return value: the kernel virtual address of this allocated page
  247. //note: this function is used to get the memory for PDT(Page Directory Table)&PT(Page Table)
  248. static void *
  249. boot_alloc_page(void) {
  250. struct Page *p = alloc_page();
  251. if (p == NULL) {
  252. panic("boot_alloc_page failed.\n");
  253. }
  254. return page2kva(p);
  255. }
  256. //pmm_init - setup a pmm to manage physical memory, build PDT&PT to setup paging mechanism
  257. // - check the correctness of pmm & paging mechanism, print PDT&PT
  258. void
  259. pmm_init(void) {
  260. //We need to alloc/free the physical memory (granularity is 4KB or other size).
  261. //So a framework of physical memory manager (struct pmm_manager)is defined in pmm.h
  262. //First we should init a physical memory manager(pmm) based on the framework.
  263. //Then pmm can alloc/free the physical memory.
  264. //Now the first_fit/best_fit/worst_fit/buddy_system pmm are available.
  265. init_pmm_manager();
  266. // detect physical memory space, reserve already used memory,
  267. // then use pmm->init_memmap to create free page list
  268. page_init();
  269. //use pmm->check to verify the correctness of the alloc/free function in a pmm
  270. check_alloc_page();
  271. // create boot_pgdir, an initial page directory(Page Directory Table, PDT)
  272. boot_pgdir = boot_alloc_page();
  273. memset(boot_pgdir, 0, PGSIZE);
  274. boot_cr3 = PADDR(boot_pgdir);
  275. check_pgdir();
  276. static_assert(KERNBASE % PTSIZE == 0 && KERNTOP % PTSIZE == 0);
  277. // recursively insert boot_pgdir in itself
  278. // to form a virtual page table at virtual address VPT
  279. boot_pgdir[PDX(VPT)] = PADDR(boot_pgdir) | PTE_P | PTE_W;
  280. // map all physical memory to linear memory with base linear addr KERNBASE
  281. //linear_addr KERNBASE~KERNBASE+KMEMSIZE = phy_addr 0~KMEMSIZE
  282. //But shouldn't use this map until enable_paging() & gdt_init() finished.
  283. boot_map_segment(boot_pgdir, KERNBASE, KMEMSIZE, 0, PTE_W);
  284. //temporary map:
  285. //virtual_addr 3G~3G+4M = linear_addr 0~4M = linear_addr 3G~3G+4M = phy_addr 0~4M
  286. boot_pgdir[0] = boot_pgdir[PDX(KERNBASE)];
  287. enable_paging();
  288. //reload gdt(third time,the last time) to map all physical memory
  289. //virtual_addr 0~4G=liear_addr 0~4G
  290. //then set kernel stack(ss:esp) in TSS, setup TSS in gdt, load TSS
  291. gdt_init();
  292. //disable the map of virtual_addr 0~4M
  293. boot_pgdir[0] = 0;
  294. //now the basic virtual memory map(see memalyout.h) is established.
  295. //check the correctness of the basic virtual memory map.
  296. check_boot_pgdir();
  297. print_pgdir();
  298. kmalloc_init();
  299. }
  300. //get_pte - get pte and return the kernel virtual address of this pte for la
  301. // - if the PT contians this pte didn't exist, alloc a page for PT
  302. // parameter:
  303. // pgdir: the kernel virtual base address of PDT
  304. // la: the linear address need to map
  305. // create: a logical value to decide if alloc a page for PT
  306. // return vaule: the kernel virtual address of this pte
  307. pte_t *
  308. get_pte(pde_t *pgdir, uintptr_t la, bool create) {
  309. /* LAB2 EXERCISE 2: 2012011346
  310. *
  311. * If you need to visit a physical address, please use KADDR()
  312. * please read pmm.h for useful macros
  313. *
  314. * Maybe you want help comment, BELOW comments can help you finish the code
  315. *
  316. * Some Useful MACROs and DEFINEs, you can use them in below implementation.
  317. * MACROs or Functions:
  318. * PDX(la) = the index of page directory entry of VIRTUAL ADDRESS la.
  319. * KADDR(pa) : takes a physical address and returns the corresponding kernel virtual address.
  320. * set_page_ref(page,1) : means the page be referenced by one time
  321. * page2pa(page): get the physical address of memory which this (struct Page *) page manages
  322. * struct Page * alloc_page() : allocation a page
  323. * memset(void *s, char c, size_t n) : sets the first n bytes of the memory area pointed by s
  324. * to the specified value c.
  325. * DEFINEs:
  326. * PTE_P 0x001 // page table/directory entry flags bit : Present
  327. * PTE_W 0x002 // page table/directory entry flags bit : Writeable
  328. * PTE_U 0x004 // page table/directory entry flags bit : User can access
  329. */
  330. // (1) find page directory entry
  331. pde_t *pdep = pgdir + PDX(la);
  332. pte_t *ret = NULL;
  333. // (2) check if entry is not present
  334. if (!(*pdep & PTE_P)) {
  335. // (3) check if creating is needed, then alloc page for page table
  336. if (!create)
  337. return NULL;
  338. // CAUTION: this page is used for page table, not for common data page
  339. struct Page *page = alloc_page();
  340. // (4) set page reference
  341. set_page_ref(page, 1);
  342. // (5) get linear address of page
  343. uintptr_t pa = page2pa(page); //physical
  344. // (6) clear page content using memset
  345. memset((void*)KADDR(pa), 0, PGSIZE);
  346. // (7) set page directory entry's permission
  347. assert(!(pa & 0xFFF));
  348. *pdep = pa | PTE_U | PTE_W | PTE_P;
  349. }
  350. ret = KADDR((pte_t *)(*pdep & ~0xFFF) + PTX(la));
  351. return ret; // (8) return page table entry
  352. }
  353. //get_page - get related Page struct for linear address la using PDT pgdir
  354. struct Page *
  355. get_page(pde_t *pgdir, uintptr_t la, pte_t **ptep_store) {
  356. pte_t *ptep = get_pte(pgdir, la, 0);
  357. if (ptep_store != NULL) {
  358. *ptep_store = ptep;
  359. }
  360. if (ptep != NULL && *ptep & PTE_P) {
  361. return pa2page(*ptep);
  362. }
  363. return NULL;
  364. }
  365. //page_remove_pte - free an Page sturct which is related linear address la
  366. // - and clean(invalidate) pte which is related linear address la
  367. //note: PT is changed, so the TLB need to be invalidate
  368. static inline void
  369. page_remove_pte(pde_t *pgdir, uintptr_t la, pte_t *ptep) {
  370. /* LAB2 EXERCISE 3: 2012011346
  371. *
  372. * Please check if ptep is valid, and tlb must be manually updated if mapping is updated
  373. *
  374. * Maybe you want help comment, BELOW comments can help you finish the code
  375. *
  376. * Some Useful MACROs and DEFINEs, you can use them in below implementation.
  377. * MACROs or Functions:
  378. * struct Page *page pte2page(*ptep): get the according page from the value of a ptep
  379. * free_page : free a page
  380. * page_ref_dec(page) : decrease page->ref. NOTICE: ff page->ref == 0 , then this page should be free.
  381. * tlb_invalidate(pde_t *pgdir, uintptr_t la) : Invalidate a TLB entry, but only if the page tables being
  382. * edited are the ones currently in use by the processor.
  383. * DEFINEs:
  384. * PTE_P 0x001 // page table/directory entry flags bit : Present
  385. */
  386. //(1) check if this page table entry is present
  387. if (*ptep & PTE_P) {
  388. //(2) find corresponding page to pte
  389. struct Page *page = pte2page(*ptep);
  390. //(3) decrease page reference
  391. assert(page->ref > 0);
  392. if (!page_ref_dec(page)) {
  393. //(4) and free this page when page reference reachs 0
  394. free_page(page);
  395. }
  396. //(5) clear second page table entry
  397. *ptep = 0;
  398. //(6) flush tlb
  399. }
  400. tlb_invalidate(pgdir, la);
  401. }
  402. //page_remove - free an Page which is related linear address la and has an validated pte
  403. void
  404. page_remove(pde_t *pgdir, uintptr_t la) {
  405. pte_t *ptep = get_pte(pgdir, la, 0);
  406. if (ptep != NULL) {
  407. page_remove_pte(pgdir, la, ptep);
  408. }
  409. }
  410. //page_insert - build the map of phy addr of an Page with the linear addr la
  411. // paramemters:
  412. // pgdir: the kernel virtual base address of PDT
  413. // page: the Page which need to map
  414. // la: the linear address need to map
  415. // perm: the permission of this Page which is setted in related pte
  416. // return value: always 0
  417. //note: PT is changed, so the TLB need to be invalidate
  418. int
  419. page_insert(pde_t *pgdir, struct Page *page, uintptr_t la, uint32_t perm) {
  420. pte_t *ptep = get_pte(pgdir, la, 1);
  421. if (ptep == NULL) {
  422. return -E_NO_MEM;
  423. }
  424. page_ref_inc(page);
  425. if (*ptep & PTE_P) {
  426. struct Page *p = pte2page(*ptep);
  427. if (p == page) {
  428. page_ref_dec(page);
  429. }
  430. else {
  431. page_remove_pte(pgdir, la, ptep);
  432. }
  433. }
  434. *ptep = page2pa(page) | PTE_P | perm;
  435. tlb_invalidate(pgdir, la);
  436. return 0;
  437. }
  438. // invalidate a TLB entry, but only if the page tables being
  439. // edited are the ones currently in use by the processor.
  440. void
  441. tlb_invalidate(pde_t *pgdir, uintptr_t la) {
  442. if (rcr3() == PADDR(pgdir)) {
  443. invlpg((void *)la);
  444. }
  445. }
  446. // pgdir_alloc_page - call alloc_page & page_insert functions to
  447. // - allocate a page size memory & setup an addr map
  448. // - pa<->la with linear address la and the PDT pgdir
  449. struct Page *
  450. pgdir_alloc_page(pde_t *pgdir, uintptr_t la, uint32_t perm) {
  451. struct Page *page = alloc_page();
  452. if (page != NULL) {
  453. if (page_insert(pgdir, page, la, perm) ==-E_NO_MEM) {
  454. free_page(page);
  455. panic("pgdir_alloc_page:NO FREE PAGES1!!");
  456. }
  457. }
  458. return page;
  459. }
  460. static void
  461. check_alloc_page(void) {
  462. pmm_manager->check();
  463. cprintf("check_alloc_page() succeeded!\n");
  464. }
  465. static void
  466. check_pgdir(void) {
  467. assert(npage <= KMEMSIZE / PGSIZE);
  468. assert(boot_pgdir != NULL && (uint32_t)PGOFF(boot_pgdir) == 0);
  469. assert(get_page(boot_pgdir, 0x0, NULL) == NULL);
  470. struct Page *p1, *p2;
  471. p1 = alloc_page();
  472. assert(page_insert(boot_pgdir, p1, 0x0, 0) == 0);
  473. pte_t *ptep;
  474. assert((ptep = get_pte(boot_pgdir, 0x0, 0)) != NULL);
  475. assert(pa2page(*ptep) == p1);
  476. assert(page_ref(p1) == 1);
  477. ptep = &((pte_t *)KADDR(PDE_ADDR(boot_pgdir[0])))[1];
  478. assert(get_pte(boot_pgdir, PGSIZE, 0) == ptep);
  479. p2 = alloc_page();
  480. assert(page_insert(boot_pgdir, p2, PGSIZE, PTE_U | PTE_W) == 0);
  481. assert((ptep = get_pte(boot_pgdir, PGSIZE, 0)) != NULL);
  482. assert(*ptep & PTE_U);
  483. assert(*ptep & PTE_W);
  484. assert(boot_pgdir[0] & PTE_U);
  485. assert(page_ref(p2) == 1);
  486. assert(page_insert(boot_pgdir, p1, PGSIZE, 0) == 0);
  487. assert(page_ref(p1) == 2);
  488. assert(page_ref(p2) == 0);
  489. assert((ptep = get_pte(boot_pgdir, PGSIZE, 0)) != NULL);
  490. assert(pa2page(*ptep) == p1);
  491. assert((*ptep & PTE_U) == 0);
  492. page_remove(boot_pgdir, 0x0);
  493. assert(page_ref(p1) == 1);
  494. assert(page_ref(p2) == 0);
  495. page_remove(boot_pgdir, PGSIZE);
  496. assert(page_ref(p1) == 0);
  497. assert(page_ref(p2) == 0);
  498. assert(page_ref(pa2page(boot_pgdir[0])) == 1);
  499. free_page(pa2page(boot_pgdir[0]));
  500. boot_pgdir[0] = 0;
  501. cprintf("check_pgdir() succeeded!\n");
  502. }
  503. static void
  504. check_boot_pgdir(void) {
  505. pte_t *ptep;
  506. int i;
  507. for (i = 0; i < npage; i += PGSIZE) {
  508. assert((ptep = get_pte(boot_pgdir, (uintptr_t)KADDR(i), 0)) != NULL);
  509. assert(PTE_ADDR(*ptep) == i);
  510. }
  511. assert(PDE_ADDR(boot_pgdir[PDX(VPT)]) == PADDR(boot_pgdir));
  512. assert(boot_pgdir[0] == 0);
  513. struct Page *p;
  514. p = alloc_page();
  515. assert(page_insert(boot_pgdir, p, 0x100, PTE_W) == 0);
  516. assert(page_ref(p) == 1);
  517. assert(page_insert(boot_pgdir, p, 0x100 + PGSIZE, PTE_W) == 0);
  518. assert(page_ref(p) == 2);
  519. const char *str = "ucore: Hello world!!";
  520. strcpy((void *)0x100, str);
  521. assert(strcmp((void *)0x100, (void *)(0x100 + PGSIZE)) == 0);
  522. *(char *)(page2kva(p) + 0x100) = '\0';
  523. assert(strlen((const char *)0x100) == 0);
  524. free_page(p);
  525. free_page(pa2page(PDE_ADDR(boot_pgdir[0])));
  526. boot_pgdir[0] = 0;
  527. cprintf("check_boot_pgdir() succeeded!\n");
  528. }
  529. //perm2str - use string 'u,r,w,-' to present the permission
  530. static const char *
  531. perm2str(int perm) {
  532. static char str[4];
  533. str[0] = (perm & PTE_U) ? 'u' : '-';
  534. str[1] = 'r';
  535. str[2] = (perm & PTE_W) ? 'w' : '-';
  536. str[3] = '\0';
  537. return str;
  538. }
  539. //get_pgtable_items - In [left, right] range of PDT or PT, find a continuous linear addr space
  540. // - (left_store*X_SIZE~right_store*X_SIZE) for PDT or PT
  541. // - X_SIZE=PTSIZE=4M, if PDT; X_SIZE=PGSIZE=4K, if PT
  542. // paramemters:
  543. // left: no use ???
  544. // right: the high side of table's range
  545. // start: the low side of table's range
  546. // table: the beginning addr of table
  547. // left_store: the pointer of the high side of table's next range
  548. // right_store: the pointer of the low side of table's next range
  549. // return value: 0 - not a invalid item range, perm - a valid item range with perm permission
  550. static int
  551. get_pgtable_items(size_t left, size_t right, size_t start, uintptr_t *table, size_t *left_store, size_t *right_store) {
  552. if (start >= right) {
  553. return 0;
  554. }
  555. while (start < right && !(table[start] & PTE_P)) {
  556. start ++;
  557. }
  558. if (start < right) {
  559. if (left_store != NULL) {
  560. *left_store = start;
  561. }
  562. int perm = (table[start ++] & PTE_USER);
  563. while (start < right && (table[start] & PTE_USER) == perm) {
  564. start ++;
  565. }
  566. if (right_store != NULL) {
  567. *right_store = start;
  568. }
  569. return perm;
  570. }
  571. return 0;
  572. }
  573. //print_pgdir - print the PDT&PT
  574. void
  575. print_pgdir(void) {
  576. cprintf("-------------------- BEGIN --------------------\n");
  577. size_t left, right = 0, perm;
  578. while ((perm = get_pgtable_items(0, NPDEENTRY, right, vpd, &left, &right)) != 0) {
  579. cprintf("PDE(%03x) %08x-%08x %08x %s\n", right - left,
  580. left * PTSIZE, right * PTSIZE, (right - left) * PTSIZE, perm2str(perm));
  581. size_t l, r = left * NPTEENTRY;
  582. while ((perm = get_pgtable_items(left * NPTEENTRY, right * NPTEENTRY, r, vpt, &l, &r)) != 0) {
  583. cprintf(" |-- PTE(%05x) %08x-%08x %08x %s\n", r - l,
  584. l * PGSIZE, r * PGSIZE, (r - l) * PGSIZE, perm2str(perm));
  585. }
  586. }
  587. cprintf("--------------------- END ---------------------\n");
  588. }