mem.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #pragma once
  2. #include <types/stdint.h>
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6. // don't forget to add the initial 1m to the total
  7. struct mem_size_info {
  8. uint16_t n_1k_blks; // memory between 1m and 16m in 1k blocks
  9. uint16_t n_64k_blks; // memory above 16m in 64k blocks
  10. };
  11. extern struct mem_size_info asm_mem_size_info;
  12. // TODO: decide heap start address according
  13. // to user's memory size
  14. #define HEAP_START ((void*)0x01000000)
  15. struct mem_blk_flags {
  16. uint8_t is_free;
  17. uint8_t has_next;
  18. uint8_t _unused2;
  19. uint8_t _unused3;
  20. };
  21. struct mem_blk {
  22. size_t size;
  23. struct mem_blk_flags flags;
  24. // the first byte of the memory space
  25. // the minimal allocated space is 4 bytes
  26. uint8_t data[4];
  27. };
  28. void init_heap(void);
  29. void* k_malloc(size_t size);
  30. void k_free(void* ptr);
  31. /*
  32. * page directory entry
  33. *
  34. * p : present (1)
  35. * rw : allow write (1)
  36. * us : allow user access (1)
  37. * pwt : todo
  38. * pcd : todo
  39. * a : accessed for linear address translation (1)
  40. * d : dirty (1) (ignored)
  41. * ps : use 4MiB pages (ignored)
  42. * addr: page table address
  43. */
  44. struct page_directory_entry_in {
  45. uint32_t p : 1;
  46. uint32_t rw : 1;
  47. uint32_t us : 1;
  48. uint32_t pwt : 1;
  49. uint32_t pcd : 1;
  50. uint32_t a : 1;
  51. uint32_t d : 1;
  52. uint32_t ps : 1;
  53. uint32_t ignored : 4;
  54. uint32_t addr : 20;
  55. };
  56. typedef union page_directory_entry {
  57. uint32_t v;
  58. struct page_directory_entry_in in;
  59. } page_directory_entry;
  60. /*
  61. * page table entry
  62. *
  63. * p : present (1)
  64. * rw : allow write (1)
  65. * us : allow user access (1)
  66. * pwt : todo
  67. * pcd : todo
  68. * a : accessed for linear address translation (1)
  69. * d : dirty (1)
  70. * pat : todo (ignored)
  71. * g : used in cr4 mode (ignored)
  72. * addr: physical memory address
  73. */
  74. struct page_table_entry_in {
  75. uint32_t p : 1;
  76. uint32_t rw : 1;
  77. uint32_t us : 1;
  78. uint32_t pwt : 1;
  79. uint32_t pcd : 1;
  80. uint32_t a : 1;
  81. uint32_t d : 1;
  82. uint32_t pat : 1;
  83. uint32_t g : 1;
  84. uint32_t ignored : 3;
  85. uint32_t addr : 20;
  86. };
  87. typedef union page_table_entry {
  88. uint32_t v;
  89. struct page_table_entry_in in;
  90. } page_table_entry;
  91. #define KERNEL_PAGE_DIRECTORY_ADDR ((page_directory_entry*)0x00000000)
  92. #define KERNEL_PAGE_TABLE_START_ADDR ((page_table_entry*)0x00100000)
  93. void init_paging(void);
  94. #ifdef __cplusplus
  95. }
  96. #endif