《操作系统》的实验代码。
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.

169 lines
7.6 KiB

  1. #ifndef __KERN_MM_MEMLAYOUT_H__
  2. #define __KERN_MM_MEMLAYOUT_H__
  3. /* This file contains the definitions for memory management in our OS. */
  4. /* global segment number */
  5. #define SEG_KTEXT 1
  6. #define SEG_KDATA 2
  7. #define SEG_UTEXT 3
  8. #define SEG_UDATA 4
  9. #define SEG_TSS 5
  10. /* global descrptor numbers */
  11. #define GD_KTEXT ((SEG_KTEXT) << 3) // kernel text
  12. #define GD_KDATA ((SEG_KDATA) << 3) // kernel data
  13. #define GD_UTEXT ((SEG_UTEXT) << 3) // user text
  14. #define GD_UDATA ((SEG_UDATA) << 3) // user data
  15. #define GD_TSS ((SEG_TSS) << 3) // task segment selector
  16. #define DPL_KERNEL (0)
  17. #define DPL_USER (3)
  18. #define KERNEL_CS ((GD_KTEXT) | DPL_KERNEL)
  19. #define KERNEL_DS ((GD_KDATA) | DPL_KERNEL)
  20. #define USER_CS ((GD_UTEXT) | DPL_USER)
  21. #define USER_DS ((GD_UDATA) | DPL_USER)
  22. /* *
  23. * Virtual memory map: Permissions
  24. * kernel/user
  25. *
  26. * 4G ------------------> +---------------------------------+
  27. * | |
  28. * | Empty Memory (*) |
  29. * | |
  30. * +---------------------------------+ 0xFB000000
  31. * | Cur. Page Table (Kern, RW) | RW/-- PTSIZE
  32. * VPT -----------------> +---------------------------------+ 0xFAC00000
  33. * | Invalid Memory (*) | --/--
  34. * KERNTOP -------------> +---------------------------------+ 0xF8000000
  35. * | |
  36. * | Remapped Physical Memory | RW/-- KMEMSIZE
  37. * | |
  38. * KERNBASE ------------> +---------------------------------+ 0xC0000000
  39. * | Invalid Memory (*) | --/--
  40. * USERTOP -------------> +---------------------------------+ 0xB0000000
  41. * | User stack |
  42. * +---------------------------------+
  43. * | |
  44. * : :
  45. * | ~~~~~~~~~~~~~~~~ |
  46. * : :
  47. * | |
  48. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  49. * | User Program & Heap |
  50. * UTEXT ---------------> +---------------------------------+ 0x00800000
  51. * | Invalid Memory (*) | --/--
  52. * | - - - - - - - - - - - - - - - |
  53. * | User STAB Data (optional) |
  54. * USERBASE, USTAB------> +---------------------------------+ 0x00200000
  55. * | Invalid Memory (*) | --/--
  56. * 0 -------------------> +---------------------------------+ 0x00000000
  57. * (*) Note: The kernel ensures that "Invalid Memory" is *never* mapped.
  58. * "Empty Memory" is normally unmapped, but user programs may map pages
  59. * there if desired.
  60. *
  61. * */
  62. /* All physical memory mapped at this address */
  63. #define KERNBASE 0xC0000000
  64. #define KMEMSIZE 0x38000000 // the maximum amount of physical memory
  65. #define KERNTOP (KERNBASE + KMEMSIZE)
  66. /* *
  67. * Virtual page table. Entry PDX[VPT] in the PD (Page Directory) contains
  68. * a pointer to the page directory itself, thereby turning the PD into a page
  69. * table, which maps all the PTEs (Page Table Entry) containing the page mappings
  70. * for the entire virtual address space into that 4 Meg region starting at VPT.
  71. * */
  72. #define VPT 0xFAC00000
  73. #define KSTACKPAGE 2 // # of pages in kernel stack
  74. #define KSTACKSIZE (KSTACKPAGE * PGSIZE) // sizeof kernel stack
  75. #define USERTOP 0xB0000000
  76. #define USTACKTOP USERTOP
  77. #define USTACKPAGE 256 // # of pages in user stack
  78. #define USTACKSIZE (USTACKPAGE * PGSIZE) // sizeof user stack
  79. #define USERBASE 0x00200000
  80. #define UTEXT 0x00800000 // where user programs generally begin
  81. #define USTAB USERBASE // the location of the user STABS data structure
  82. #define USER_ACCESS(start, end) \
  83. (USERBASE <= (start) && (start) < (end) && (end) <= USERTOP)
  84. #define KERN_ACCESS(start, end) \
  85. (KERNBASE <= (start) && (start) < (end) && (end) <= KERNTOP)
  86. #ifndef __ASSEMBLER__
  87. #include <defs.h>
  88. #include <atomic.h>
  89. #include <list.h>
  90. typedef uintptr_t pte_t;
  91. typedef uintptr_t pde_t;
  92. typedef pte_t swap_entry_t; //the pte can also be a swap entry
  93. // some constants for bios interrupt 15h AX = 0xE820
  94. #define E820MAX 20 // number of entries in E820MAP
  95. #define E820_ARM 1 // address range memory
  96. #define E820_ARR 2 // address range reserved
  97. struct e820map {
  98. int nr_map;
  99. struct {
  100. uint64_t addr;
  101. uint64_t size;
  102. uint32_t type;
  103. } __attribute__((packed)) map[E820MAX];
  104. };
  105. /* *
  106. * struct Page - Page descriptor structures. Each Page describes one
  107. * physical page. In kern/mm/pmm.h, you can find lots of useful functions
  108. * that convert Page to other data types, such as phyical address.
  109. * */
  110. struct Page {
  111. int ref; // page frame's reference counter
  112. uint32_t flags; // array of flags that describe the status of the page frame
  113. unsigned int property; // used in buddy system, stores the order (the X in 2^X) of the continuous memory block
  114. int zone_num; // used in buddy system, the No. of zone which the page belongs to
  115. list_entry_t page_link; // free list link
  116. list_entry_t pra_page_link; // used for pra (page replace algorithm)
  117. uintptr_t pra_vaddr; // used for pra (page replace algorithm)
  118. };
  119. /* Flags describing the status of a page frame */
  120. #define PG_reserved 0 // the page descriptor is reserved for kernel or unusable
  121. #define PG_property 1 // the member 'property' is valid
  122. #define SetPageReserved(page) set_bit(PG_reserved, &((page)->flags))
  123. #define ClearPageReserved(page) clear_bit(PG_reserved, &((page)->flags))
  124. #define PageReserved(page) test_bit(PG_reserved, &((page)->flags))
  125. #define SetPageProperty(page) set_bit(PG_property, &((page)->flags))
  126. #define ClearPageProperty(page) clear_bit(PG_property, &((page)->flags))
  127. #define PageProperty(page) test_bit(PG_property, &((page)->flags))
  128. // convert list entry to page
  129. #define le2page(le, member) \
  130. to_struct((le), struct Page, member)
  131. /* free_area_t - maintains a doubly linked list to record free (unused) pages */
  132. typedef struct {
  133. list_entry_t free_list; // the list header
  134. unsigned int nr_free; // # of free pages in this free list
  135. } free_area_t;
  136. /* for slab style kmalloc */
  137. #define PG_slab 2 // page frame is included in a slab
  138. #define SetPageSlab(page) set_bit(PG_slab, &((page)->flags))
  139. #define ClearPageSlab(page) clear_bit(PG_slab, &((page)->flags))
  140. #define PageSlab(page) test_bit(PG_slab, &((page)->flags))
  141. #endif /* !__ASSEMBLER__ */
  142. #endif /* !__KERN_MM_MEMLAYOUT_H__ */