《操作系统》的实验代码。
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

130 lignes
5.5 KiB

il y a 12 ans
il y a 12 ans
il y a 12 ans
il y a 12 ans
  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. // some constants for bios interrupt 15h AX = 0xE820
  68. #define E820MAX 20 // number of entries in E820MAP
  69. #define E820_ARM 1 // address range memory
  70. #define E820_ARR 2 // address range reserved
  71. struct e820map {
  72. int nr_map;
  73. struct {
  74. uint64_t addr;
  75. uint64_t size;
  76. uint32_t type;
  77. } __attribute__((packed)) map[E820MAX];
  78. };
  79. /* *
  80. * struct Page - Page descriptor structures. Each Page describes one
  81. * physical page. In kern/mm/pmm.h, you can find lots of useful functions
  82. * that convert Page to other data types, such as phyical address.
  83. * */
  84. struct Page {
  85. int ref; // page frame's reference counter
  86. uint32_t flags; // array of flags that describe the status of the page frame
  87. unsigned int property; // the num of free block, used in first fit pm manager
  88. list_entry_t page_link; // free list link
  89. };
  90. /* Flags describing the status of a page frame */
  91. #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
  92. #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.
  93. #define SetPageReserved(page) set_bit(PG_reserved, &((page)->flags))
  94. #define ClearPageReserved(page) clear_bit(PG_reserved, &((page)->flags))
  95. #define PageReserved(page) test_bit(PG_reserved, &((page)->flags))
  96. #define SetPageProperty(page) set_bit(PG_property, &((page)->flags))
  97. #define ClearPageProperty(page) clear_bit(PG_property, &((page)->flags))
  98. #define PageProperty(page) test_bit(PG_property, &((page)->flags))
  99. // convert list entry to page
  100. #define le2page(le, member) \
  101. to_struct((le), struct Page, member)
  102. /* free_area_t - maintains a doubly linked list to record free (unused) pages */
  103. typedef struct {
  104. list_entry_t free_list; // the list header
  105. unsigned int nr_free; // # of free pages in this free list
  106. } free_area_t;
  107. #endif /* !__ASSEMBLER__ */
  108. #endif /* !__KERN_MM_MEMLAYOUT_H__ */