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

140 lines
5.9 KiB

10 years ago
  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. * | |
  40. * | |
  41. * | |
  42. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  43. * (*) Note: The kernel ensures that "Invalid Memory" is *never* mapped.
  44. * "Empty Memory" is normally unmapped, but user programs may map pages
  45. * there if desired.
  46. *
  47. * */
  48. /* All physical memory mapped at this address */
  49. #define KERNBASE 0xC0000000
  50. #define KMEMSIZE 0x38000000 // the maximum amount of physical memory
  51. #define KERNTOP (KERNBASE + KMEMSIZE)
  52. /* *
  53. * Virtual page table. Entry PDX[VPT] in the PD (Page Directory) contains
  54. * a pointer to the page directory itself, thereby turning the PD into a page
  55. * table, which maps all the PTEs (Page Table Entry) containing the page mappings
  56. * for the entire virtual address space into that 4 Meg region starting at VPT.
  57. * */
  58. #define VPT 0xFAC00000
  59. #define KSTACKPAGE 2 // # of pages in kernel stack
  60. #define KSTACKSIZE (KSTACKPAGE * PGSIZE) // sizeof kernel stack
  61. #ifndef __ASSEMBLER__
  62. #include <defs.h>
  63. #include <atomic.h>
  64. #include <list.h>
  65. typedef uintptr_t pte_t;
  66. typedef uintptr_t pde_t;
  67. typedef pte_t swap_entry_t; //the pte can also be a swap entry
  68. // some constants for bios interrupt 15h AX = 0xE820
  69. #define E820MAX 20 // number of entries in E820MAP
  70. #define E820_ARM 1 // address range memory
  71. #define E820_ARR 2 // address range reserved
  72. struct e820map {
  73. int nr_map;
  74. struct {
  75. uint64_t addr;
  76. uint64_t size;
  77. uint32_t type;
  78. } __attribute__((packed)) map[E820MAX];
  79. };
  80. /* *
  81. * struct Page - Page descriptor structures. Each Page describes one
  82. * physical page. In kern/mm/pmm.h, you can find lots of useful functions
  83. * that convert Page to other data types, such as phyical address.
  84. * */
  85. struct Page {
  86. int ref; // page frame's reference counter
  87. uint32_t flags; // array of flags that describe the status of the page frame
  88. unsigned int property; // used in buddy system, stores the order (the X in 2^X) of the continuous memory block
  89. int zone_num; // used in buddy system, the No. of zone which the page belongs to
  90. list_entry_t page_link; // free list link
  91. list_entry_t pra_page_link; // used for pra (page replace algorithm)
  92. uintptr_t pra_vaddr; // used for pra (page replace algorithm)
  93. };
  94. /* Flags describing the status of a page frame */
  95. #define PG_reserved 0 // the page descriptor is reserved for kernel or unusable
  96. #define PG_property 1 // the member 'property' is valid
  97. #define SetPageReserved(page) set_bit(PG_reserved, &((page)->flags))
  98. #define ClearPageReserved(page) clear_bit(PG_reserved, &((page)->flags))
  99. #define PageReserved(page) test_bit(PG_reserved, &((page)->flags))
  100. #define SetPageProperty(page) set_bit(PG_property, &((page)->flags))
  101. #define ClearPageProperty(page) clear_bit(PG_property, &((page)->flags))
  102. #define PageProperty(page) test_bit(PG_property, &((page)->flags))
  103. // convert list entry to page
  104. #define le2page(le, member) \
  105. to_struct((le), struct Page, member)
  106. /* free_area_t - maintains a doubly linked list to record free (unused) pages */
  107. typedef struct {
  108. list_entry_t free_list; // the list header
  109. unsigned int nr_free; // # of free pages in this free list
  110. } free_area_t;
  111. /* for slab style kmalloc */
  112. #define PG_slab 2 // page frame is included in a slab
  113. #define SetPageSlab(page) set_bit(PG_slab, &((page)->flags))
  114. #define ClearPageSlab(page) clear_bit(PG_slab, &((page)->flags))
  115. #define PageSlab(page) test_bit(PG_slab, &((page)->flags))
  116. #endif /* !__ASSEMBLER__ */
  117. #endif /* !__KERN_MM_MEMLAYOUT_H__ */