Advertisement
Guest User

Untitled

a guest
Feb 25th, 2020
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.51 KB | None | 0 0
  1. #include "stdlib.h"
  2. #include "stdio.h"
  3. #include "stdint.h"
  4. #include "stdbool.h"
  5.  
  6. void test_data_to_sections();
  7.  
  8. //секция - то, на что делится оперативная память в memory_map
  9. //страница(фрейм) - то, на что делится доступная секция
  10.  
  11. //информация о секциях в RAM определяемых с помощью GRUB memory map
  12. typedef struct memory_section_data
  13. {
  14. #define MEMORY_AVAILABLE 1
  15. #define MEMORY_UNAVAILABLE 2
  16. //адрес начала секции памяти
  17. uint32_t address;
  18. //длина секции в байтах
  19. uint32_t length;
  20. //тип секции
  21. uint8_t type;
  22. } memory_section_data_t;
  23.  
  24. //информация о доступных секциях в котором мы создаём страницы(фреймы)
  25. typedef struct memory_paging_data
  26. {
  27. //адрес начала доступной секции для разметки на страницы(фреймы)
  28. uint32_t address;
  29. //in bytes(суммарный размер страниц в этом секторе (длина доступной секции в рамках блоков, то есть небольшая часть памяти в конце(которая не вместилась в блок) отбрасывается))
  30. uint32_t length;
  31. //кол-во блоков в доступном секторе
  32. uint32_t count;
  33. } memory_paging_data_t;
  34.  
  35. typedef struct memory_page
  36. {
  37. uint32_t physical_address;
  38. bool is_busy;
  39. uint32_t owner;
  40. } memory_page_t;
  41.  
  42. uint32_t startkernel = 0x100000, endkernel = 0x102000;
  43.  
  44. //кол-во секций RAM которые мы получили после подсчёта памяти с помощью GRUB
  45. uint8_t memory_section_number = 6;
  46.  
  47. //секции RAM
  48. memory_section_data_t memory_sections[255];
  49.  
  50. //кол-во доступных секций которые мы делим на страницы(фреймы)
  51. uint8_t memory_paging_sections_number = 0;
  52.  
  53. //доступные секции которые мы разбили на страницы(фреймы)
  54. memory_paging_data_t memory_paging[255];
  55.  
  56. //все страницы
  57. //(4GB / 4096)
  58. memory_page_t memory_pages_table[1048575];
  59.  
  60. //сколько страниц(фреймов) есть в таблице(если память меньше 4GB)
  61. uint32_t memory_pages_table_count = 0;
  62.  
  63. void init_paging();
  64. void* kmalloc(uint32_t bytes);
  65. void* search_npages(uint32_t number);
  66.  
  67. int main(int argc, char** argv)
  68. {
  69. test_data_to_sections();
  70. init_paging();
  71. }
  72.  
  73. void init_paging()
  74. {
  75. printf("\n--------PAGING START--------\n");
  76. printf("\nstartkernel: 0x%x\nendkernel: 0x%x\n\n", startkernel, endkernel);
  77. for (uint8_t section_index = 0; section_index < memory_section_number; ++section_index)
  78. {
  79. if (memory_sections[section_index].type == MEMORY_AVAILABLE)
  80. {
  81. //сохраняем данные доступного сектора
  82. //вычесляем кол-во страниц(фреймов) которых можно в нём создать
  83. //вычесляем суммарный размер страниц(фреймов) в этом секторе
  84. memory_paging[memory_paging_sections_number].address = memory_sections[section_index].address;
  85. memory_paging[memory_paging_sections_number].count = memory_sections[section_index].length / 4096;
  86. memory_paging[memory_paging_sections_number].length = 4096 * memory_paging[memory_paging_sections_number].count;
  87.  
  88. printf("Avaiable section address: 0x%x\n", memory_paging[memory_paging_sections_number].address);
  89. printf("Avaiable section count: %u\n", memory_paging[memory_paging_sections_number].count);
  90. printf("Avaiable section length: %u bytes(%u kilobytes)\n\n", memory_paging[memory_paging_sections_number].length, memory_paging[memory_paging_sections_number].length / 1024);
  91.  
  92. uint32_t unavailable_page_counter = 0;
  93. //делим текущую доступную секцию на страницы(фреймы)
  94. for (uint32_t page_index = 0; page_index < memory_paging[memory_paging_sections_number].count; ++page_index)
  95. {
  96. //проверяем не заденем ли мы ядро и его память, также резервируем nullptr
  97. if ((memory_paging[memory_paging_sections_number].address + (0x1000 * page_index) + 0x1000 < startkernel || memory_paging[memory_paging_sections_number].address + (0x1000 * page_index) > endkernel) && memory_paging[memory_paging_sections_number].address + (0x1000 * page_index) > 0x0)
  98. {
  99. memory_pages_table[page_index + memory_pages_table_count].physical_address = memory_paging[memory_paging_sections_number].address + (0x1000 * page_index);
  100. }
  101. else
  102. {
  103. ++unavailable_page_counter;
  104. --memory_pages_table_count;
  105. }
  106. }
  107. memory_pages_table_count += memory_paging[memory_paging_sections_number].count;
  108. memory_paging[memory_paging_sections_number].count -= unavailable_page_counter;
  109. ++memory_paging_sections_number;
  110. }
  111. }
  112.  
  113. for (uint32_t i = 0; i < memory_pages_table_count; ++i)
  114. {
  115. printf("Physical page address: 0x%x is_busy: %u\n", memory_pages_table[i].physical_address, memory_pages_table[i].is_busy);
  116. }
  117. printf("\n--------PAGING END--------\n\n");
  118.  
  119. uint32_t address = kmalloc(1);
  120. printf("address: 0x%x\n\n", address);
  121.  
  122. address = kmalloc(1);
  123. printf("address: 0x%x\n\n", address);
  124.  
  125. address = kmalloc(1);
  126. printf("address: 0x%x\n\n", address);
  127.  
  128. address = kmalloc(1);
  129. printf("address: 0x%x\n\n", address);
  130.  
  131. address = kmalloc(1);
  132. printf("address: 0x%x\n\n", address);
  133.  
  134. memory_pages_table[1].is_busy = 0;
  135.  
  136. for (uint32_t i = 0; i < memory_pages_table_count; ++i)
  137. {
  138. printf("Physical page address: 0x%x is_busy: %u\n", memory_pages_table[i].physical_address, memory_pages_table[i].is_busy);
  139. }
  140.  
  141. address = kmalloc(7000);
  142. printf("address: 0x%x\n\n", address);
  143.  
  144. for (uint32_t i = 0; i < memory_pages_table_count; ++i)
  145. {
  146. printf("Physical page address: 0x%x is_busy: %u\n", memory_pages_table[i].physical_address, memory_pages_table[i].is_busy);
  147. }
  148. }
  149.  
  150. void* kmalloc(uint32_t bytes)
  151. {
  152. printf("kmalloc try allocate %u bytes\n", bytes);
  153. void* first_page_address = search_npages(bytes / 4096 + 1);
  154. if (first_page_address != 0x0)
  155. return first_page_address;
  156. else
  157. return 0x0;
  158. }
  159.  
  160. void* search_npages(uint32_t n)
  161. {
  162. printf("search_npages try search %u pages\n", n);
  163. //счётчик идущих друг за другом страниц(если надо выделить больше 1 страницы(фрейма))
  164. uint32_t placed_one_after_the_other = 0;
  165. void* first_page_address = 0x0;
  166. uint32_t* indexes_of_placed_after_other_for_clear = malloc(sizeof(uint32_t) * 32);
  167. for(uint32_t page_index = 0; page_index < memory_pages_table_count; ++page_index)
  168. {
  169. if (n == 1)
  170. {
  171. if (memory_pages_table[page_index].is_busy == 0)
  172. {
  173. memory_pages_table[page_index].is_busy = 1;
  174. return memory_pages_table[page_index].physical_address;
  175. }
  176. }
  177. else
  178. {
  179. if (memory_pages_table[page_index].is_busy == 0)
  180. {
  181. if (first_page_address == 0x0)
  182. {
  183. memory_pages_table[page_index].is_busy = 1;
  184. first_page_address = memory_pages_table[page_index].physical_address;
  185. indexes_of_placed_after_other_for_clear[placed_one_after_the_other] = page_index;
  186. ++placed_one_after_the_other;
  187. }
  188. else
  189. {
  190. memory_pages_table[page_index].is_busy = 1;
  191. indexes_of_placed_after_other_for_clear[placed_one_after_the_other] = page_index;
  192. ++placed_one_after_the_other;
  193. }
  194. if (n == placed_one_after_the_other)
  195. {
  196. return first_page_address;
  197. }
  198. }
  199. //если не нашли n подряд
  200. else if (first_page_address != 0x0)
  201. {
  202. for (uint32_t i = 0; i < placed_one_after_the_other; ++i)
  203. {
  204. printf("%u\n", indexes_of_placed_after_other_for_clear[placed_one_after_the_other]);
  205. memory_pages_table[indexes_of_placed_after_other_for_clear[placed_one_after_the_other]].is_busy = 0;
  206. }
  207. }
  208. }
  209. }
  210. return 0x0;
  211. }
  212.  
  213. void test_data_to_sections()
  214. {
  215. /*
  216. memory_sections[0].address = 0x0;
  217. memory_sections[0].type = MEMORY_AVAILABLE;
  218.  
  219. memory_sections[1].address = 0x9fc00;
  220. memory_sections[1].type = MEMORY_UNAVAILABLE;
  221.  
  222. memory_sections[2].address = 0xf0000;
  223. memory_sections[2].type = MEMORY_UNAVAILABLE;
  224.  
  225. memory_sections[3].address = 0x100000;
  226. memory_sections[3].type = MEMORY_AVAILABLE;
  227.  
  228. memory_sections[4].address = 0x7fe0000;
  229. memory_sections[4].type = MEMORY_UNAVAILABLE;
  230.  
  231. memory_sections[5].address = 0xfffc0000;
  232. memory_sections[5].type = MEMORY_UNAVAILABLE;
  233.  
  234. memory_sections[0].length = memory_sections[1].address - memory_sections[0].address;
  235. memory_sections[1].length = memory_sections[2].address - memory_sections[1].address;
  236. memory_sections[2].length = memory_sections[3].address - memory_sections[2].address;
  237. memory_sections[3].length = memory_sections[4].address - memory_sections[3].address;
  238. memory_sections[4].length = memory_sections[5].address - memory_sections[4].address;
  239. memory_sections[5].length = 0xffffffff - memory_sections[5].address;
  240. */
  241.  
  242. memory_sections[0].address = 0x0; //24 kilobytes
  243. memory_sections[0].type = MEMORY_AVAILABLE;
  244.  
  245. memory_sections[1].address = 0x6000;
  246. memory_sections[1].type = MEMORY_UNAVAILABLE;
  247.  
  248. memory_sections[2].address = 0xf0000;
  249. memory_sections[2].type = MEMORY_UNAVAILABLE;
  250.  
  251. memory_sections[3].address = 0x100000; //31 kilobytes
  252. memory_sections[3].type = MEMORY_AVAILABLE;
  253.  
  254. memory_sections[4].address = 0x107c00;
  255. memory_sections[4].type = MEMORY_UNAVAILABLE;
  256.  
  257. memory_sections[5].address = 0xfffc0000;
  258. memory_sections[5].type = MEMORY_UNAVAILABLE;
  259.  
  260. memory_sections[0].length = memory_sections[1].address - memory_sections[0].address;
  261. memory_sections[1].length = memory_sections[2].address - memory_sections[1].address;
  262. memory_sections[2].length = memory_sections[3].address - memory_sections[2].address;
  263. memory_sections[3].length = memory_sections[4].address - memory_sections[3].address;
  264. memory_sections[4].length = memory_sections[5].address - memory_sections[4].address;
  265. memory_sections[5].length = 0xffffffff - memory_sections[5].address;
  266.  
  267. for (uint8_t i = 0; i < memory_section_number; ++i)
  268. {
  269. if (memory_sections[i].type == MEMORY_AVAILABLE)
  270. {
  271. printf("Available RAM addr: 0x%x\n", memory_sections[i].address);
  272. printf(" Length: %u bytes(%u kilobytes)\n", memory_sections[i].length, memory_sections[i].length / 1024);
  273. }
  274. else
  275. {
  276. printf("Unavailable RAM addr: 0x%x\n", memory_sections[i].address);
  277. }
  278. }
  279. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement