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

134 lines
5.8 KiB

12 years ago
12 years ago
12 years ago
12 years ago
12 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; // the num of free block, used in first fit pm manager
  89. list_entry_t page_link; // free list link
  90. list_entry_t pra_page_link; // used for pra (page replace algorithm)
  91. uintptr_t pra_vaddr; // used for pra (page replace algorithm)
  92. };
  93. /* Flags describing the status of a page frame */
  94. #define PG_reserved 0 // if this bit=1: the Page is reserved for kernel, cannot be used in alloc/free_pages; otherwise, this bit=0
  95. #define PG_property 1 // if this bit=1: the Page is the head page of a free memory block(contains some continuous_addrress pages), and can be used in alloc_pages; if this bit=0: if the Page is the the head page of a free memory block, then this Page and the memory block is alloced. Or this Page isn't the head page.
  96. #define SetPageReserved(page) set_bit(PG_reserved, &((page)->flags))
  97. #define ClearPageReserved(page) clear_bit(PG_reserved, &((page)->flags))
  98. #define PageReserved(page) test_bit(PG_reserved, &((page)->flags))
  99. #define SetPageProperty(page) set_bit(PG_property, &((page)->flags))
  100. #define ClearPageProperty(page) clear_bit(PG_property, &((page)->flags))
  101. #define PageProperty(page) test_bit(PG_property, &((page)->flags))
  102. // convert list entry to page
  103. #define le2page(le, member) \
  104. to_struct((le), struct Page, member)
  105. /* free_area_t - maintains a doubly linked list to record free (unused) pages */
  106. typedef struct {
  107. list_entry_t free_list; // the list header
  108. unsigned int nr_free; // # of free pages in this free list
  109. } free_area_t;
  110. #endif /* !__ASSEMBLER__ */
  111. #endif /* !__KERN_MM_MEMLAYOUT_H__ */