Skip to content

Commit

Permalink
Move is address mapped in vmm_mapping.c
Browse files Browse the repository at this point in the history
  • Loading branch information
dreamos82 committed Apr 22, 2024
1 parent 30af960 commit 6762e0d
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 46 deletions.
2 changes: 2 additions & 0 deletions src/include/kernel/arch/common/mem/vmm_mapping.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ void *map_vaddress(void *address, size_t flags, uint64_t *pml4_root);
int unmap_vaddress(void *address);
int unmap_vaddress_hh(void *address, uint64_t *pml4_root);

uint8_t is_phyisical_address_mapped(uintptr_t physical_address, uintptr_t virtual_address);

#endif
45 changes: 45 additions & 0 deletions src/kernel/arch/x86_64/mem/vmm_mapping.c
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ void *map_phys_to_virt_addr(void* physical_address, void* address, size_t flags)

void *map_vaddress(void *virtual_address, size_t flags, uint64_t *pml4_root){
pretty_logf(Verbose, "address: 0x%x", virtual_address);
//TODO need to check if i can just use the phys alloc here
void *new_addr = pmm_prepare_new_pagetable();
return map_phys_to_virt_addr_hh(new_addr, virtual_address, flags, pml4_root);
}
Expand Down Expand Up @@ -265,3 +266,47 @@ int unmap_vaddress_hh(void *address, uint64_t *pml4_root) {
void identity_map_phys_address(void *physical_address, size_t flags) {
map_phys_to_virt_addr(physical_address, physical_address, flags);
}

uint8_t is_phyisical_address_mapped(uintptr_t physical_address, uintptr_t virtual_address) {
uint16_t pml4_e = PML4_ENTRY((uint64_t) virtual_address);
uint64_t *pml4_table = (uint64_t *) (SIGN_EXTENSION | ENTRIES_TO_ADDRESS(510l, 510l, 510l, 510l));
if (!(pml4_table[pml4_e] & PRESENT_BIT)) {
return PHYS_ADDRESS_NOT_MAPPED;
}

uint16_t pdpr_e = PDPR_ENTRY((uint64_t) virtual_address);
uint64_t *pdpr_table = (uint64_t *) (SIGN_EXTENSION | ENTRIES_TO_ADDRESS(510l, 510l, 510l, (uint64_t) pml4_e));
if (!(pdpr_table[pdpr_e] & PRESENT_BIT)) {
return PHYS_ADDRESS_NOT_MAPPED;
}

uint16_t pd_e = PD_ENTRY((uint64_t) virtual_address);
uint64_t *pd_table = (uint64_t*) (SIGN_EXTENSION | ENTRIES_TO_ADDRESS(510l, 510l, pml4_e, (uint64_t) pdpr_e));
if (!(pd_table[pd_e] & PRESENT_BIT)) {
return PHYS_ADDRESS_NOT_MAPPED;
}
#if SMALL_PAGES == 0
else {
if (ALIGN_PHYSADDRESS(pd_table[pd_e]) == ALIGN_PHYSADDRESS(physical_address)) {
return PHYS_ADDRESS_MAPPED;
} else {
return PHYS_ADDRESS_MISMATCH;
}
}
#endif

#if SMALL_PAGES == 1
uint16_t pt_e = PT_ENTRY((uint64_t) virtual_address);
uint64_t *pt_table = (uint64_t *) (SIGN_EXTENSION | ENTRIES_TO_ADDRESS(510l, (uint64_t) pml4_e, (uint64_t) pdpr_e, (uint64_t) pd_e));
if ( !(pt_table[pt_e] & PRESENT_BIT )) {
return PHYS_ADDRESS_NOT_MAPPED;
} else {
if (ALIGN_PHYSADDRESS(pt_table[pt_e]) == ALIGN_PHYSADDRESS(physical_address)) {
return PHYS_ADDRESS_MAPPED;
} else {
return PHYS_ADDRESS_MISMATCH;
}
}
#endif
return 0;
}
46 changes: 0 additions & 46 deletions src/kernel/mem/vmm.c
Original file line number Diff line number Diff line change
Expand Up @@ -216,52 +216,6 @@ void vmm_free(void *address) {
return;
}


// TODO: maybe thsi should be moved in the arch/mapping part and use the hhdm?
uint8_t is_phyisical_address_mapped(uintptr_t physical_address, uintptr_t virtual_address) {
uint16_t pml4_e = PML4_ENTRY((uint64_t) virtual_address);
uint64_t *pml4_table = (uint64_t *) (SIGN_EXTENSION | ENTRIES_TO_ADDRESS(510l, 510l, 510l, 510l));
if (!(pml4_table[pml4_e] & PRESENT_BIT)) {
return PHYS_ADDRESS_NOT_MAPPED;
}

uint16_t pdpr_e = PDPR_ENTRY((uint64_t) virtual_address);
uint64_t *pdpr_table = (uint64_t *) (SIGN_EXTENSION | ENTRIES_TO_ADDRESS(510l, 510l, 510l, (uint64_t) pml4_e));
if (!(pdpr_table[pdpr_e] & PRESENT_BIT)) {
return PHYS_ADDRESS_NOT_MAPPED;
}

uint16_t pd_e = PD_ENTRY((uint64_t) virtual_address);
uint64_t *pd_table = (uint64_t*) (SIGN_EXTENSION | ENTRIES_TO_ADDRESS(510l, 510l, pml4_e, (uint64_t) pdpr_e));
if (!(pd_table[pd_e] & PRESENT_BIT)) {
return PHYS_ADDRESS_NOT_MAPPED;
}
#if SMALL_PAGES == 0
else {
if (ALIGN_PHYSADDRESS(pd_table[pd_e]) == ALIGN_PHYSADDRESS(physical_address)) {
return PHYS_ADDRESS_MAPPED;
} else {
return PHYS_ADDRESS_MISMATCH;
}
}
#endif

#if SMALL_PAGES == 1
uint16_t pt_e = PT_ENTRY((uint64_t) virtual_address);
uint64_t *pt_table = (uint64_t *) (SIGN_EXTENSION | ENTRIES_TO_ADDRESS(510l, (uint64_t) pml4_e, (uint64_t) pdpr_e, (uint64_t) pd_e));
if ( !(pt_table[pt_e] & PRESENT_BIT )) {
return PHYS_ADDRESS_NOT_MAPPED;
} else {
if (ALIGN_PHYSADDRESS(pt_table[pt_e]) == ALIGN_PHYSADDRESS(physical_address)) {
return PHYS_ADDRESS_MAPPED;
} else {
return PHYS_ADDRESS_MISMATCH;
}
}
#endif
return 0;
}

//TODO implement this function or remove it
uint8_t check_virt_address_status(uint64_t virtual_address) {
(void)virtual_address;
Expand Down

0 comments on commit 6762e0d

Please sign in to comment.