1 #include "mm.h"
2 #include "mm-types.h"
3 #include "mmio.h"
4 #include "slab.h"
5 #include <common/printk.h>
6 #include <common/kprint.h>
7 #include <driver/multiboot2/multiboot2.h>
8 #include <process/process.h>
9 #include <common/compiler.h>
10 #include <common/errno.h>
11 #include <debug/traceback/traceback.h>
12
13 uint64_t mm_Total_Memory = 0;
14 uint64_t mm_total_2M_pages = 0;
15 struct mm_struct initial_mm = {0};
16
17 struct memory_desc memory_management_struct = {{0}, 0};
18
19 /**
20 * @brief 从页表中获取pdt页表项的内容
21 *
22 * @param proc_page_table_addr 页表的地址
23 * @param is_phys 页表地址是否为物理地址
24 * @param virt_addr_start 要清除的虚拟地址的起始地址
25 * @param length 要清除的区域的长度
26 * @param clear 是否清除标志位
27 */
28 uint64_t mm_get_PDE(ul proc_page_table_addr, bool is_phys, ul virt_addr, bool clear);
29
30 /**
31 * @brief 检查页表是否存在不为0的页表项
32 *
33 * @param ptr 页表基指针
34 * @return int8_t 存在 -> 1
35 * 不存在 -> 0
36 */
mm_check_page_table(uint64_t * ptr)37 int8_t mm_check_page_table(uint64_t *ptr)
38 {
39 for (int i = 0; i < 512; ++i, ++ptr)
40 {
41 if (*ptr != 0)
42 return 1;
43 }
44 return 0;
45 }
46
mm_init()47 void mm_init()
48 {
49 kinfo("Initializing memory management unit...");
50 // 设置内核程序不同部分的起止地址
51 memory_management_struct.kernel_code_start = (ul)&_text;
52 memory_management_struct.kernel_code_end = (ul)&_etext;
53 memory_management_struct.kernel_data_end = (ul)&_edata;
54 memory_management_struct.rodata_end = (ul)&_erodata;
55 memory_management_struct.start_brk = (ul)&_end;
56
57 struct multiboot_mmap_entry_t mb2_mem_info[512];
58 int count;
59
60 multiboot2_iter(multiboot2_get_memory, mb2_mem_info, &count);
61 io_mfence();
62 for (int i = 0; i < count; ++i)
63 {
64 io_mfence();
65 // 可用的内存
66 if (mb2_mem_info->type == 1)
67 mm_Total_Memory += mb2_mem_info->len;
68
69 // kdebug("[i=%d] mb2_mem_info[i].type=%d, mb2_mem_info[i].addr=%#018lx", i, mb2_mem_info[i].type, mb2_mem_info[i].addr);
70 // 保存信息到mms
71 memory_management_struct.e820[i].BaseAddr = mb2_mem_info[i].addr;
72 memory_management_struct.e820[i].Length = mb2_mem_info[i].len;
73 memory_management_struct.e820[i].type = mb2_mem_info[i].type;
74 memory_management_struct.len_e820 = i;
75
76 // 脏数据
77 if (mb2_mem_info[i].type > 4 || mb2_mem_info[i].len == 0 || mb2_mem_info[i].type < 1)
78 break;
79 }
80 printk("[ INFO ] Total amounts of RAM : %ld bytes\n", mm_Total_Memory);
81
82 // 计算有效内存页数
83 io_mfence();
84 for (int i = 0; i < memory_management_struct.len_e820; ++i)
85 {
86 if (memory_management_struct.e820[i].type != 1)
87 continue;
88 io_mfence();
89 // 将内存段的起始物理地址按照2M进行对齐
90 ul addr_start = PAGE_2M_ALIGN(memory_management_struct.e820[i].BaseAddr);
91 // 将内存段的终止物理地址的低2M区域清空,以实现对齐
92 ul addr_end = ((memory_management_struct.e820[i].BaseAddr + memory_management_struct.e820[i].Length) & PAGE_2M_MASK);
93
94 // 内存段不可用
95 if (addr_end <= addr_start)
96 continue;
97 io_mfence();
98 mm_total_2M_pages += ((addr_end - addr_start) >> PAGE_2M_SHIFT);
99 }
100 kinfo("Total amounts of 2M pages : %ld.", mm_total_2M_pages);
101
102 // 物理地址空间的最大地址(包含了物理内存、内存空洞、ROM等)
103 ul max_addr = memory_management_struct.e820[memory_management_struct.len_e820].BaseAddr + memory_management_struct.e820[memory_management_struct.len_e820].Length;
104 // 初始化mms的bitmap
105 // bmp的指针指向截止位置的4k对齐的上边界(防止修改了别的数据)
106 io_mfence();
107 memory_management_struct.bmp = (unsigned long *)((memory_management_struct.start_brk + PAGE_4K_SIZE - 1) & PAGE_4K_MASK);
108 memory_management_struct.bits_size = max_addr >> PAGE_2M_SHIFT; // 物理地址空间的最大页面数
109 memory_management_struct.bmp_len = (((unsigned long)(max_addr >> PAGE_2M_SHIFT) + sizeof(unsigned long) * 8 - 1) / 8) & (~(sizeof(unsigned long) - 1)); // bmp由多少个unsigned long变量组成
110 io_mfence();
111
112 // 初始化bitmap, 先将整个bmp空间全部置位。稍后再将可用物理内存页复位。
113 memset(memory_management_struct.bmp, 0xff, memory_management_struct.bmp_len);
114 io_mfence();
115 // 初始化内存页结构
116 // 将页结构映射于bmp之后
117 memory_management_struct.pages_struct = (struct Page *)(((unsigned long)memory_management_struct.bmp + memory_management_struct.bmp_len + PAGE_4K_SIZE - 1) & PAGE_4K_MASK);
118
119 memory_management_struct.count_pages = max_addr >> PAGE_2M_SHIFT;
120 memory_management_struct.pages_struct_len = ((max_addr >> PAGE_2M_SHIFT) * sizeof(struct Page) + sizeof(long) - 1) & (~(sizeof(long) - 1));
121 // 将pages_struct全部清空,以备后续初始化
122 memset(memory_management_struct.pages_struct, 0x00, memory_management_struct.pages_struct_len); // init pages memory
123
124 io_mfence();
125 // 初始化内存区域
126 memory_management_struct.zones_struct = (struct Zone *)(((ul)memory_management_struct.pages_struct + memory_management_struct.pages_struct_len + PAGE_4K_SIZE - 1) & PAGE_4K_MASK);
127 io_mfence();
128 // 由于暂时无法计算zone结构体的数量,因此先将其设为0
129 memory_management_struct.count_zones = 0;
130 io_mfence();
131 // zones-struct 成员变量暂时按照5个来计算
132 memory_management_struct.zones_struct_len = (10 * sizeof(struct Zone) + sizeof(ul) - 1) & (~(sizeof(ul) - 1));
133 io_mfence();
134 memset(memory_management_struct.zones_struct, 0x00, memory_management_struct.zones_struct_len);
135
136 // ==== 遍历e820数组,完成成员变量初始化工作 ===
137
138 for (int i = 0; i < memory_management_struct.len_e820; ++i)
139 {
140 io_mfence();
141 if (memory_management_struct.e820[i].type != 1) // 不是操作系统可以使用的物理内存
142 continue;
143 ul addr_start = PAGE_2M_ALIGN(memory_management_struct.e820[i].BaseAddr);
144 ul addr_end = (memory_management_struct.e820[i].BaseAddr + memory_management_struct.e820[i].Length) & PAGE_2M_MASK;
145
146 if (addr_end <= addr_start)
147 continue;
148
149 // zone init
150 struct Zone *z = memory_management_struct.zones_struct + memory_management_struct.count_zones;
151 ++memory_management_struct.count_zones;
152
153 z->zone_addr_start = addr_start;
154 z->zone_addr_end = addr_end;
155 z->zone_length = addr_end - addr_start;
156
157 z->count_pages_using = 0;
158 z->count_pages_free = (addr_end - addr_start) >> PAGE_2M_SHIFT;
159 z->total_pages_link = 0;
160
161 z->attr = 0;
162 z->gmd_struct = &memory_management_struct;
163
164 z->count_pages = (addr_end - addr_start) >> PAGE_2M_SHIFT;
165 z->pages_group = (struct Page *)(memory_management_struct.pages_struct + (addr_start >> PAGE_2M_SHIFT));
166
167 // 初始化页
168 struct Page *p = z->pages_group;
169
170 for (int j = 0; j < z->count_pages; ++j, ++p)
171 {
172 p->zone = z;
173 p->addr_phys = addr_start + PAGE_2M_SIZE * j;
174 p->attr = 0;
175
176 p->ref_counts = 0;
177 p->age = 0;
178
179 // 将bmp中对应的位 复位
180 *(memory_management_struct.bmp + ((p->addr_phys >> PAGE_2M_SHIFT) >> 6)) ^= (1UL << ((p->addr_phys >> PAGE_2M_SHIFT) % 64));
181 }
182 }
183
184 // 初始化0~2MB的物理页
185 // 由于这个区间的内存由多个内存段组成,因此不会被以上代码初始化,需要我们手动配置page[0]。
186 io_mfence();
187 memory_management_struct.pages_struct->zone = memory_management_struct.zones_struct;
188 memory_management_struct.pages_struct->addr_phys = 0UL;
189 set_page_attr(memory_management_struct.pages_struct, PAGE_PGT_MAPPED | PAGE_KERNEL_INIT | PAGE_KERNEL);
190 memory_management_struct.pages_struct->ref_counts = 1;
191 memory_management_struct.pages_struct->age = 0;
192 // 将第0页的标志位给置上
193 //*(memory_management_struct.bmp) |= 1UL;
194
195 // 计算zone结构体的总长度(按照64位对齐)
196 memory_management_struct.zones_struct_len = (memory_management_struct.count_zones * sizeof(struct Zone) + sizeof(ul) - 1) & (~(sizeof(ul) - 1));
197
198 ZONE_DMA_INDEX = 0;
199 ZONE_NORMAL_INDEX = memory_management_struct.count_zones ;
200 ZONE_UNMAPPED_INDEX = 0;
201
202 // kdebug("ZONE_DMA_INDEX=%d\tZONE_NORMAL_INDEX=%d\tZONE_UNMAPPED_INDEX=%d", ZONE_DMA_INDEX, ZONE_NORMAL_INDEX, ZONE_UNMAPPED_INDEX);
203 // 设置内存页管理结构的地址,预留了一段空间,防止内存越界。
204 memory_management_struct.end_of_struct = (ul)((ul)memory_management_struct.zones_struct + memory_management_struct.zones_struct_len + sizeof(long) * 32) & (~(sizeof(long) - 1));
205
206 // 初始化内存管理单元结构所占的物理页的结构体
207 ul mms_max_page = (virt_2_phys(memory_management_struct.end_of_struct) >> PAGE_2M_SHIFT); // 内存管理单元所占据的序号最大的物理页
208 // kdebug("mms_max_page=%ld", mms_max_page);
209
210 struct Page *tmp_page = NULL;
211 ul page_num;
212 // 第0个page已经在上方配置
213 for (ul j = 1; j <= mms_max_page; ++j)
214 {
215 barrier();
216 tmp_page = memory_management_struct.pages_struct + j;
217 page_init(tmp_page, PAGE_PGT_MAPPED | PAGE_KERNEL | PAGE_KERNEL_INIT);
218 barrier();
219 page_num = tmp_page->addr_phys >> PAGE_2M_SHIFT;
220 *(memory_management_struct.bmp + (page_num >> 6)) |= (1UL << (page_num % 64));
221 ++tmp_page->zone->count_pages_using;
222 --tmp_page->zone->count_pages_free;
223 }
224
225 kinfo("Memory management unit initialize complete!");
226
227 flush_tlb();
228 // todo: 在这里增加代码,暂时停止视频输出,否则可能会导致图像数据写入slab的区域,从而造成异常
229 // 初始化slab内存池
230 slab_init();
231 page_table_init();
232
233 initial_mm.pgd = (pml4t_t *)get_CR3();
234
235 initial_mm.code_addr_start = memory_management_struct.kernel_code_start;
236 initial_mm.code_addr_end = memory_management_struct.kernel_code_end;
237
238 initial_mm.data_addr_start = (ul)&_data;
239 initial_mm.data_addr_end = memory_management_struct.kernel_data_end;
240
241 initial_mm.rodata_addr_start = (ul)&_rodata;
242 initial_mm.rodata_addr_end = (ul)&_erodata;
243 initial_mm.bss_start = (uint64_t)&_bss;
244 initial_mm.bss_end = (uint64_t)&_ebss;
245
246 initial_mm.brk_start = memory_management_struct.start_brk;
247 initial_mm.brk_end = current_pcb->addr_limit;
248
249 initial_mm.stack_start = _stack_start;
250 initial_mm.vmas = NULL;
251
252
253
254 mmio_init();
255 }
256
257 /**
258 * @brief 初始化内存页
259 *
260 * @param page 内存页结构体
261 * @param flags 标志位
262 * 本函数只负责初始化内存页,允许对同一页面进行多次初始化
263 * 而维护计数器及置位bmp标志位的功能,应当在分配页面的时候手动完成
264 * @return unsigned long
265 */
page_init(struct Page * page,ul flags)266 unsigned long page_init(struct Page *page, ul flags)
267 {
268 page->attr |= flags;
269 // 若页面的引用计数为0或是共享页,增加引用计数
270 if ((!page->ref_counts) || (page->attr & PAGE_SHARED))
271 {
272 ++page->ref_counts;
273 barrier();
274 if (page->zone)
275 ++page->zone->total_pages_link;
276 }
277 page->anon_vma = NULL;
278 spin_init(&(page->op_lock));
279 return 0;
280 }
281
282 /**
283 * @brief 从已初始化的页结构中搜索符合申请条件的、连续num个struct page
284 *
285 * @param zone_select 选择内存区域, 可选项:dma, mapped in pgt(normal), unmapped in pgt
286 * @param num 需要申请的连续内存页的数量 num<64
287 * @param flags 将页面属性设置成flag
288 * @return struct Page*
289 */
alloc_pages(unsigned int zone_select,int num,ul flags)290 struct Page *alloc_pages(unsigned int zone_select, int num, ul flags)
291 {
292 ul zone_start = 0, zone_end = 0;
293 if (num >= 64 && num <= 0)
294 {
295 kerror("alloc_pages(): num is invalid.");
296 return NULL;
297 }
298
299 ul attr = flags;
300 switch (zone_select)
301 {
302 case ZONE_DMA:
303 // DMA区域
304 zone_start = 0;
305 zone_end = ZONE_DMA_INDEX;
306 attr |= PAGE_PGT_MAPPED;
307 break;
308 case ZONE_NORMAL:
309 zone_start = ZONE_DMA_INDEX;
310 zone_end = ZONE_NORMAL_INDEX;
311 attr |= PAGE_PGT_MAPPED;
312 break;
313 case ZONE_UNMAPPED_IN_PGT:
314 zone_start = ZONE_NORMAL_INDEX;
315 zone_end = ZONE_UNMAPPED_INDEX;
316 attr = 0;
317 break;
318
319 default:
320 kerror("In alloc_pages: param: zone_select incorrect.");
321 // 返回空
322 return NULL;
323 break;
324 }
325
326 for (int i = zone_start; i < zone_end; ++i)
327 {
328 if ((memory_management_struct.zones_struct + i)->count_pages_free < num)
329 continue;
330
331 struct Zone *z = memory_management_struct.zones_struct + i;
332 // 区域对应的起止页号
333 ul page_start = (z->zone_addr_start >> PAGE_2M_SHIFT);
334 ul page_end = (z->zone_addr_end >> PAGE_2M_SHIFT);
335
336 ul tmp = 64 - page_start % 64;
337 for (ul j = page_start; j < page_end; j += ((j % 64) ? tmp : 64))
338 {
339 // 按照bmp中的每一个元素进行查找
340 // 先将p定位到bmp的起始元素
341 ul *p = memory_management_struct.bmp + (j >> 6);
342
343 ul shift = j % 64;
344 ul tmp_num = ((1UL << num) - 1);
345 for (ul k = shift; k < 64; ++k)
346 {
347 // 寻找连续num个空页
348 if (!((k ? ((*p >> k) | (*(p + 1) << (64 - k))) : *p) & tmp_num))
349
350 {
351 ul start_page_num = j + k - shift; // 计算得到要开始获取的内存页的页号
352 for (ul l = 0; l < num; ++l)
353 {
354 struct Page *x = memory_management_struct.pages_struct + start_page_num + l;
355
356 // 分配页面,手动配置属性及计数器
357 // 置位bmp
358 *(memory_management_struct.bmp + ((x->addr_phys >> PAGE_2M_SHIFT) >> 6)) |= (1UL << (x->addr_phys >> PAGE_2M_SHIFT) % 64);
359 ++(z->count_pages_using);
360 --(z->count_pages_free);
361 page_init(x, attr);
362 }
363 // 成功分配了页面,返回第一个页面的指针
364 // kwarn("start page num=%d\n", start_page_num);
365 return (struct Page *)(memory_management_struct.pages_struct + start_page_num);
366 }
367 }
368 }
369 }
370 kBUG("Cannot alloc page, ZONE=%d\tnums=%d, mm_total_2M_pages=%d", zone_select, num, mm_total_2M_pages);
371 return NULL;
372 }
373
374 /**
375 * @brief 清除页面的引用计数, 计数为0时清空除页表已映射以外的所有属性
376 *
377 * @param p 物理页结构体
378 * @return unsigned long
379 */
page_clean(struct Page * p)380 unsigned long page_clean(struct Page *p)
381 {
382 --p->ref_counts;
383 --p->zone->total_pages_link;
384
385 // 若引用计数为空,则清空除PAGE_PGT_MAPPED以外的所有属性
386 if (!p->ref_counts)
387 {
388 p->attr &= PAGE_PGT_MAPPED;
389 }
390 return 0;
391 }
392
393 /**
394 * @brief Get the page's attr
395 *
396 * @param page 内存页结构体
397 * @return ul 属性
398 */
get_page_attr(struct Page * page)399 ul get_page_attr(struct Page *page)
400 {
401 if (page == NULL)
402 {
403 kBUG("get_page_attr(): page == NULL");
404 return EPAGE_NULL;
405 }
406 else
407 return page->attr;
408 }
409
410 /**
411 * @brief Set the page's attr
412 *
413 * @param page 内存页结构体
414 * @param flags 属性
415 * @return ul 错误码
416 */
set_page_attr(struct Page * page,ul flags)417 ul set_page_attr(struct Page *page, ul flags)
418 {
419 if (page == NULL)
420 {
421 kBUG("get_page_attr(): page == NULL");
422 return EPAGE_NULL;
423 }
424 else
425 {
426 page->attr = flags;
427 return 0;
428 }
429 }
430 /**
431 * @brief 释放连续number个内存页
432 *
433 * @param page 第一个要被释放的页面的结构体
434 * @param number 要释放的内存页数量 number<64
435 */
436
free_pages(struct Page * page,int number)437 void free_pages(struct Page *page, int number)
438 {
439 if (page == NULL)
440 {
441 kerror("free_pages() page is invalid.");
442 return;
443 }
444
445 if (number >= 64 || number <= 0)
446 {
447 kerror("free_pages(): number %d is invalid.", number);
448 return;
449 }
450
451 ul page_num;
452 for (int i = 0; i < number; ++i, ++page)
453 {
454 page_num = page->addr_phys >> PAGE_2M_SHIFT;
455 // 复位bmp
456 *(memory_management_struct.bmp + (page_num >> 6)) &= ~(1UL << (page_num % 64));
457 // 更新计数器
458 --page->zone->count_pages_using;
459 ++page->zone->count_pages_free;
460 page->attr = 0;
461 }
462
463 return;
464 }
465
466 /**
467 * @brief 重新初始化页表的函数
468 * 将所有物理页映射到线性地址空间
469 */
page_table_init()470 void page_table_init()
471 {
472 kinfo("Re-Initializing page table...");
473 ul *global_CR3 = get_CR3();
474
475 int js = 0;
476 ul *tmp_addr;
477 for (int i = 0; i < memory_management_struct.count_zones; ++i)
478 {
479 struct Zone *z = memory_management_struct.zones_struct + i;
480 struct Page *p = z->pages_group;
481
482 if (i == ZONE_UNMAPPED_INDEX && ZONE_UNMAPPED_INDEX != 0)
483 break;
484
485 for (int j = 0; j < z->count_pages; ++j)
486 {
487 mm_map_proc_page_table((uint64_t)get_CR3(), true, (ul)phys_2_virt(p->addr_phys), p->addr_phys, PAGE_2M_SIZE, PAGE_KERNEL_PAGE, false, true, false);
488
489 ++p;
490 ++js;
491 }
492 }
493
494
495 barrier();
496 // ========= 在IDLE进程的顶层页表中添加对内核地址空间的映射 =====================
497
498 // 由于IDLE进程的顶层页表的高地址部分会被后续进程所复制,为了使所有进程能够共享相同的内核空间,
499 // 因此需要先在IDLE进程的顶层页表内映射二级页表
500
501 uint64_t *idle_pml4t_vaddr = (uint64_t *)phys_2_virt((uint64_t)get_CR3() & (~0xfffUL));
502
503 for (int i = 256; i < 512; ++i)
504 {
505 uint64_t *tmp = idle_pml4t_vaddr + i;
506 barrier();
507 if (*tmp == 0)
508 {
509 void *pdpt = kmalloc(PAGE_4K_SIZE, 0);
510 barrier();
511 memset(pdpt, 0, PAGE_4K_SIZE);
512 barrier();
513 set_pml4t(tmp, mk_pml4t(virt_2_phys(pdpt), PAGE_KERNEL_PGT));
514 }
515 }
516 barrier();
517 flush_tlb();
518 kinfo("Page table Initialized. Affects:%d", js);
519 }
520
521 /**
522 * @brief 从页表中获取pdt页表项的内容
523 *
524 * @param proc_page_table_addr 页表的地址
525 * @param is_phys 页表地址是否为物理地址
526 * @param virt_addr_start 要清除的虚拟地址的起始地址
527 * @param length 要清除的区域的长度
528 * @param clear 是否清除标志位
529 */
mm_get_PDE(ul proc_page_table_addr,bool is_phys,ul virt_addr,bool clear)530 uint64_t mm_get_PDE(ul proc_page_table_addr, bool is_phys, ul virt_addr, bool clear)
531 {
532 ul *tmp;
533 if (is_phys)
534 tmp = phys_2_virt((ul *)((ul)proc_page_table_addr & (~0xfffUL)) + ((virt_addr >> PAGE_GDT_SHIFT) & 0x1ff));
535 else
536 tmp = (ul *)((ul)proc_page_table_addr & (~0xfffUL)) + ((virt_addr >> PAGE_GDT_SHIFT) & 0x1ff);
537
538 // pml4页表项为0
539 if (*tmp == 0)
540 return 0;
541
542 tmp = phys_2_virt((ul *)(*tmp & (~0xfffUL)) + ((virt_addr >> PAGE_1G_SHIFT) & 0x1ff));
543
544 // pdpt页表项为0
545 if (*tmp == 0)
546 return 0;
547
548 // 读取pdt页表项
549 tmp = phys_2_virt(((ul *)(*tmp & (~0xfffUL)) + (((ul)(virt_addr) >> PAGE_2M_SHIFT) & 0x1ff)));
550
551 if (clear) // 清除页表项的标志位
552 return *tmp & (~0x1fff);
553 else
554 return *tmp;
555 }
556
557 /**
558 * @brief 从mms中寻找Page结构体
559 *
560 * @param phys_addr
561 * @return struct Page*
562 */
mm_find_page(uint64_t phys_addr,uint32_t zone_select)563 static struct Page *mm_find_page(uint64_t phys_addr, uint32_t zone_select)
564 {
565 uint32_t zone_start, zone_end;
566 switch (zone_select)
567 {
568 case ZONE_DMA:
569 // DMA区域
570 zone_start = 0;
571 zone_end = ZONE_DMA_INDEX;
572 break;
573 case ZONE_NORMAL:
574 zone_start = ZONE_DMA_INDEX;
575 zone_end = ZONE_NORMAL_INDEX;
576 break;
577 case ZONE_UNMAPPED_IN_PGT:
578 zone_start = ZONE_NORMAL_INDEX;
579 zone_end = ZONE_UNMAPPED_INDEX;
580 break;
581
582 default:
583 kerror("In mm_find_page: param: zone_select incorrect.");
584 // 返回空
585 return NULL;
586 break;
587 }
588
589 for (int i = zone_start; i <= zone_end; ++i)
590 {
591 if ((memory_management_struct.zones_struct + i)->count_pages_using == 0)
592 continue;
593
594 struct Zone *z = memory_management_struct.zones_struct + i;
595
596 // 区域对应的起止页号
597 ul page_start = (z->zone_addr_start >> PAGE_2M_SHIFT);
598 ul page_end = (z->zone_addr_end >> PAGE_2M_SHIFT);
599
600 ul tmp = 64 - page_start % 64;
601 for (ul j = page_start; j < page_end; j += ((j % 64) ? tmp : 64))
602 {
603 // 按照bmp中的每一个元素进行查找
604 // 先将p定位到bmp的起始元素
605 ul *p = memory_management_struct.bmp + (j >> 6);
606
607 ul shift = j % 64;
608 for (ul k = shift; k < 64; ++k)
609 {
610 if ((*p >> k) & 1) // 若当前页已分配
611 {
612 uint64_t page_num = j + k - shift;
613 struct Page *x = memory_management_struct.pages_struct + page_num;
614
615 if (x->addr_phys == phys_addr) // 找到对应的页
616 return x;
617 }
618 }
619 }
620 }
621 return NULL;
622 }
623
624 /**
625 * @brief 调整堆区域的大小(暂时只能增加堆区域)
626 *
627 * @todo 缩小堆区域
628 * @param old_brk_end_addr 原本的堆内存区域的结束地址
629 * @param offset 新的地址相对于原地址的偏移量
630 * @return uint64_t
631 */
mm_do_brk(uint64_t old_brk_end_addr,int64_t offset)632 uint64_t mm_do_brk(uint64_t old_brk_end_addr, int64_t offset)
633 {
634
635 uint64_t end_addr = PAGE_2M_ALIGN(old_brk_end_addr + offset);
636 if (offset >= 0)
637 {
638 for (uint64_t i = old_brk_end_addr; i < end_addr; i += PAGE_2M_SIZE)
639 {
640 struct vm_area_struct *vma = NULL;
641 mm_create_vma(current_pcb->mm, i, PAGE_2M_SIZE, VM_USER | VM_ACCESS_FLAGS, NULL, &vma);
642 mm_map(current_pcb->mm, i, PAGE_2M_SIZE, alloc_pages(ZONE_NORMAL, 1, PAGE_PGT_MAPPED)->addr_phys);
643 // mm_map_vma(vma, alloc_pages(ZONE_NORMAL, 1, PAGE_PGT_MAPPED)->addr_phys, 0, PAGE_2M_SIZE);
644 }
645 current_pcb->mm->brk_end = end_addr;
646 }
647 else
648 {
649
650 // 释放堆内存
651 for (uint64_t i = end_addr; i < old_brk_end_addr; i += PAGE_2M_SIZE)
652 {
653 uint64_t phys = mm_get_PDE((uint64_t)phys_2_virt((uint64_t)current_pcb->mm->pgd), false, i, true);
654
655 // 找到对应的页
656 struct Page *p = mm_find_page(phys, ZONE_NORMAL);
657 if (p == NULL)
658 {
659 kerror("cannot find page addr=%#018lx", phys);
660 return end_addr;
661 }
662
663 free_pages(p, 1);
664 }
665
666 mm_unmap_proc_table((uint64_t)phys_2_virt((uint64_t)current_pcb->mm->pgd), false, end_addr, PAGE_2M_ALIGN(ABS(offset)));
667 // 在页表中取消映射
668 }
669 return end_addr;
670 }
671
672 /**
673 * @brief 创建mmio对应的页结构体
674 *
675 * @param paddr 物理地址
676 * @return struct Page* 创建成功的page
677 */
__create_mmio_page_struct(uint64_t paddr)678 struct Page *__create_mmio_page_struct(uint64_t paddr)
679 {
680 struct Page *p = (struct Page *)kzalloc(sizeof(struct Page), 0);
681 if (p == NULL)
682 return NULL;
683 p->addr_phys = paddr;
684 page_init(p, PAGE_DEVICE);
685 return p;
686 }