Skip to content

Commit

Permalink
refacto(kernel: memory: pmm): return pageframe address when allocating
Browse files Browse the repository at this point in the history
  • Loading branch information
d4ilyrun committed May 8, 2024
1 parent 4d06397 commit 2d9e70c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
7 changes: 5 additions & 2 deletions kernel/arch/i686/src/memory/mmu.c
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ bool mmu_map(vaddr_t virtual, vaddr_t pageframe)
return true;
}

void mmu_unmap(vaddr_t virtual)
paddr_t mmu_unmap(vaddr_t virtual)
{
u16 pde_index = virtual >> 22; // bits 31-22
u16 pte_index = (virtual >> 12) & ((1 << 10) - 1); // bits 21-12
Expand All @@ -198,14 +198,17 @@ void mmu_unmap(vaddr_t virtual)
// c.f. todo inside mmu_map

if (!kernel_page_directory[pde_index].present)
return;
return 0;

// Erase the content of the page table entry
mmu_pte_entry *page_table =
(mmu_pte_entry *)MMU_RECURSIVE_PAGE_TABLE_ADDRESS(pde_index);
paddr_t physical = page_table->page_frame << 12;
*((volatile u32 *)&page_table[pte_index]) = 0x0;

mmu_flush_tlb(virtual);

return physical;
}

static void mmu_offset_map(paddr_t start, paddr_t end, int64_t offset)
Expand Down
4 changes: 3 additions & 1 deletion kernel/include/kernel/mmu.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,10 @@ bool mmu_map(vaddr_t virt, paddr_t physical);
* the CPU to raise an exception.
*
* @param virt The virtual address
*
* @return The physical pageframe associated with the unmapped address
*/
void mmu_unmap(vaddr_t virt);
paddr_t mmu_unmap(vaddr_t virt);

/**
* @brief Perform identity mapping inside a given virtual address range
Expand Down
14 changes: 10 additions & 4 deletions kernel/include/kernel/pmm.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
/**
* @brief Physical Memory Manager
*
* @file pmm.h
*
* @defgroup PMM Physical Memory Manager
*
* The PMM is responsible for allocating and freeing new memory pages.
* These pages are then used by the Virtual Memory Manager (e.g. UNIX's malloc)
* to return (free) new mapped virtual addresses to the caller.
*
* The PMM should never interact with the virtual address space, this is the
* responsabillity of the VMM only.
*
* @file pmm.h
* @{
*/

#ifndef KERNEL_PMM_H
Expand Down Expand Up @@ -36,7 +40,7 @@
// reference value (e.g. the physical memory manager's bit map size).
#define TOTAL_PAGEFRAMES_COUNT (ADDRESS_SPACE_SIZE / PAGE_SIZE)

/// \defgroup pmm_allocation_flags
/// \defgroup Flags PMM Allocation Flags
///
/// Flags used when allocating a page frame to specify that the allocation must
/// respect certain constraints. Constraints can be specific addresses, rules,
Expand All @@ -45,8 +49,8 @@
/// @{

/// The pageframe should be located inside the kernel's physical address space.
#define PMM_MAP_KERNEL_BIT 0x1
#define PMM_MAP_KERNEL BIT(PMM_MAP_KERNEL_BIT)
#define PMM_MAP_KERNEL_BIT 0x1

/// @}

Expand Down Expand Up @@ -77,10 +81,12 @@ paddr_t pmm_allocate(int flags);
/**
* \brief Allocate a previously unused pageframe
*
* TODO: Take pagetable settings as parameter (writable, user, ...)
* @todo Take pagetable settings as parameter (writable, user, ...)
*
* @return The pageframe's **physical** address
*/
void pmm_free(paddr_t pageframe);

#endif /* KERNEL_PMM_H */

/** @} */

0 comments on commit 2d9e70c

Please sign in to comment.