optimize the menuconfig redrawing
[lunaix-os.git] / lunaix-os / arch / x86 / mm / pmm.c
1 #include <lunaix/mm/page.h>
2 #include <lunaix/mm/pagetable.h>
3
4 extern unsigned int __kexec_end[];
5
6 void
7 pmm_arch_init_pool(struct pmem* memory)
8 {
9     pmm_declare_pool(POOL_UNIFIED, 1, memory->list_len);
10 }
11
12 ptr_t
13 pmm_arch_init_remap(struct pmem* memory, struct boot_handoff* bctx)
14 {
15     size_t ppfn_total = pfn(bctx->mem.size) + 1;
16     size_t pool_size  = ppfn_total * sizeof(struct ppage);
17     
18     size_t i = 0;
19     struct boot_mmapent* ent;
20
21 restart:;
22     while (i < bctx->mem.mmap_len) {
23         ent = &bctx->mem.mmap[i++];
24         if (free_memregion(ent) && ent->size > pool_size) {
25             goto found;
26         }
27     }
28
29     // fail to find a viable free region to host pplist
30     return 0;
31
32 found:;
33     ptr_t kexec_end = to_kphysical(__kexec_end);
34     ptr_t aligned_pplist = MAX(ent->start, kexec_end);
35
36 #ifdef CONFIG_ARCH_X86_64
37     aligned_pplist = napot_upaligned(aligned_pplist, L2T_SIZE);
38 #else
39     aligned_pplist = napot_upaligned(aligned_pplist, L0T_SIZE);
40 #endif
41
42     if (aligned_pplist + pool_size > ent->start + ent->size) {
43         goto restart;
44     }
45
46     // for x86_32, the upper bound of memory requirement for pplist
47     //  is sizeof(struct ppage) * 1MiB. For simplicity (as well as 
48     //  efficiency), we limit the granule to 4M huge page, thus, 
49     //  it will take away at least 4M worth of vm address resource 
50     //  regardless the actual physical memory size
51
52     // anchor the pplist at vmap location (right after kernel)
53     memory->pplist = (struct ppage*)PMAP;
54     memory->list_len = ppfn_total;
55
56     pfn_t nhuge;
57     pte_t* ptep;
58
59     pte_t pte   = mkpte(aligned_pplist, KERNEL_DATA);
60
61 #ifdef CONFIG_ARCH_X86_64
62     nhuge = page_count(pool_size, L2T_SIZE);
63     ptep = mkl2tep_va(VMS_SELF, PMAP);
64     vmm_set_ptes_contig(ptep, pte_mkhuge(pte), L2T_SIZE, nhuge);
65 #else
66     nhuge = page_count(pool_size, L0T_SIZE);
67     ptep = mkl0tep_va(VMS_SELF, PMAP);
68     
69     // since VMAP and PMAP share same address space
70     // we need to shift VMAP to make room
71     vmap_set_start(VMAP + nhuge * L0T_SIZE);
72     vmm_set_ptes_contig(ptep, pte_mkhuge(pte), L0T_SIZE, nhuge);
73 #endif
74
75     tlb_flush_kernel(PMAP);
76     return aligned_pplist;
77 }