Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add VIRT_KERNEL_MAP memory area #240

Merged
merged 3 commits into from
Jan 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 2 additions & 20 deletions arch/x86/pagetables.c
Original file line number Diff line number Diff line change
Expand Up @@ -201,26 +201,8 @@ void *vmap(void *va, mfn_t mfn, unsigned int order,
return va;
}

void *vunmap(void *va, unsigned int order) {
return vmap(va, MFN_INVALID, order, PT_NO_FLAGS, PT_NO_FLAGS, PT_NO_FLAGS,
PT_NO_FLAGS);
}

void *kmap(mfn_t mfn, unsigned int order,
#if defined(__x86_64__)
unsigned long l4_flags,
#endif
unsigned long l3_flags, unsigned long l2_flags, unsigned long l1_flags) {
return vmap(mfn_to_virt_kern(mfn), mfn, order,
#if defined(__x86_64__)
l4_flags,
#endif
l3_flags, l2_flags, l1_flags);
}

void *kunmap(void *va, unsigned int order) {
return vmap(va, MFN_INVALID, order, PT_NO_FLAGS, PT_NO_FLAGS, PT_NO_FLAGS,
PT_NO_FLAGS);
void vunmap(void *va, unsigned int order) {
vmap(va, MFN_INVALID, order, PT_NO_FLAGS, PT_NO_FLAGS, PT_NO_FLAGS, PT_NO_FLAGS);
}

void init_pagetables(void) {
Expand Down
2 changes: 1 addition & 1 deletion common/acpi.c
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ static void acpi_table_unmap_pages(void *addr, unsigned mapped_pages) {
mfn_t mfn = virt_to_mfn(addr);

for (unsigned i = 0; i < mapped_pages; i++, mfn++) {
kunmap(mfn_to_virt_kern(mfn), PAGE_ORDER_4K);
vunmap(mfn_to_virt_kern(mfn), PAGE_ORDER_4K);
}
}

Expand Down
2 changes: 1 addition & 1 deletion drivers/acpi/acpica/osl.c
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ void AcpiOsUnmapMemory(void *LogicalAddress, ACPI_SIZE Length) {
mfn_t mfn = virt_to_mfn(LogicalAddress);

for (unsigned i = 0; i < num_pages; i++, mfn++)
kunmap(mfn_to_virt_kern(mfn), PAGE_ORDER_4K);
vunmap(mfn_to_virt_kern(mfn), PAGE_ORDER_4K);
}

/* Task management functions */
Expand Down
62 changes: 55 additions & 7 deletions include/arch/x86/page.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@
#define PADDR_MASK (~(PADDR_SIZE - 1))

#define VIRT_KERNEL_BASE _U64(0xffffffff80000000)
#define VIRT_KERNEL_MAP _U64(0xffff800000000000)
#define VIRT_USER_BASE _U64(0x0000000000400000)
#define VIRT_IDENT_BASE _U64(0x0000000000000000)

Expand All @@ -139,14 +140,8 @@ extern void *vmap(void *va, mfn_t mfn, unsigned int order,
unsigned long l4_flags,
#endif
unsigned long l3_flags, unsigned long l2_flags, unsigned long l1_flags);
extern void *vunmap(void *va, unsigned int order);

extern void *kmap(mfn_t mfn, unsigned int order,
#if defined(__x86_64__)
unsigned long l4_flags,
#endif
unsigned long l3_flags, unsigned long l2_flags, unsigned long l1_flags);
extern void *kunmap(void *va, unsigned int order);
extern void vunmap(void *va, unsigned int order);

/* Static declarations */

Expand All @@ -156,29 +151,44 @@ static inline paddr_t mfn_to_paddr(mfn_t mfn) { return (paddr_t)(mfn << PAGE_SHI
static inline void *_paddr_to_virt(paddr_t pa, unsigned long addr_space) {
return _ptr(pa + addr_space);
}

static inline void *paddr_to_virt_kern(paddr_t pa) {
return _paddr_to_virt(pa, VIRT_KERNEL_BASE);
}

static inline void *paddr_to_virt_map(paddr_t pa) {
return _paddr_to_virt(pa, VIRT_KERNEL_MAP);
}

static inline void *paddr_to_virt_user(paddr_t pa) {
return _paddr_to_virt(pa, VIRT_USER_BASE);
}

static inline void *paddr_to_virt(paddr_t pa) {
return _paddr_to_virt(pa, VIRT_IDENT_BASE);
}

static inline void *mfn_to_virt_kern(mfn_t mfn) {
return paddr_to_virt_kern(mfn << PAGE_SHIFT);
}

static inline void *mfn_to_virt_map(mfn_t mfn) {
return paddr_to_virt_map(mfn << PAGE_SHIFT);
}

static inline void *mfn_to_virt_user(mfn_t mfn) {
return paddr_to_virt_user(mfn << PAGE_SHIFT);
}

static inline void *mfn_to_virt(mfn_t mfn) { return paddr_to_virt(mfn << PAGE_SHIFT); }

static inline paddr_t virt_to_paddr(const void *va) {
paddr_t pa = (paddr_t) va;

if (IS_ADDR_SPACE_VA(va, VIRT_KERNEL_BASE))
return pa - VIRT_KERNEL_BASE;
if (IS_ADDR_SPACE_VA(va, VIRT_KERNEL_MAP))
return pa - VIRT_KERNEL_MAP;
if (IS_ADDR_SPACE_VA(va, VIRT_USER_BASE))
return pa - VIRT_USER_BASE;

Expand All @@ -189,6 +199,32 @@ static inline mfn_t virt_to_mfn(const void *va) {
return paddr_to_mfn(virt_to_paddr(va));
}

static inline void *kmap(mfn_t mfn, unsigned int order,
#if defined(__x86_64__)
unsigned long l4_flags,
#endif
unsigned long l3_flags, unsigned long l2_flags,
unsigned long l1_flags) {
return vmap(mfn_to_virt_kern(mfn), mfn, order,
#if defined(__x86_64__)
l4_flags,
#endif
l3_flags, l2_flags, l1_flags);
}

static inline void *mmap(mfn_t mfn, unsigned int order,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here
name mmap is misleading.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. Suggestions welcome.

Copy link
Contributor Author

@wipawel wipawel Jan 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With VIRT_KERNEL_MAP I hope it's now ok.

#if defined(__x86_64__)
unsigned long l4_flags,
#endif
unsigned long l3_flags, unsigned long l2_flags,
unsigned long l1_flags) {
return vmap(mfn_to_virt_map(mfn), mfn, order,
#if defined(__x86_64__)
l4_flags,
#endif
l3_flags, l2_flags, l1_flags);
}

static inline void *vmap_1g(void *va, mfn_t mfn, unsigned long l3_flags) {
return vmap(va, mfn, PAGE_ORDER_1G, L4_PROT_USER, l3_flags, PT_NO_FLAGS, PT_NO_FLAGS);
}
Expand All @@ -215,6 +251,18 @@ static inline void *kmap_4k(mfn_t mfn, unsigned long l1_flags) {
return kmap(mfn, PAGE_ORDER_4K, L4_PROT_USER, L3_PROT_USER, L2_PROT_USER, l1_flags);
}

static inline void *mmap_1g(mfn_t mfn, unsigned long l3_flags) {
return mmap(mfn, PAGE_ORDER_1G, L4_PROT_USER, l3_flags, PT_NO_FLAGS, PT_NO_FLAGS);
}

static inline void *mmap_2m(mfn_t mfn, unsigned long l2_flags) {
return mmap(mfn, PAGE_ORDER_2M, L4_PROT_USER, L3_PROT_USER, l2_flags, PT_NO_FLAGS);
}

static inline void *mmap_4k(mfn_t mfn, unsigned long l1_flags) {
return mmap(mfn, PAGE_ORDER_4K, L4_PROT_USER, L3_PROT_USER, L2_PROT_USER, l1_flags);
}

#endif /* __ASSEMBLY__ */

#endif /* KTF_PAGE_H */
1 change: 1 addition & 0 deletions include/mm/vmm.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ enum gfp_flags {
GFP_KERNEL = 0x00000001,
GFP_USER = 0x00000002,
GFP_IDENT = 0x00000004,
GFP_KERNEL_MAP = 0x00000008,
};

/* External definitions */
Expand Down
2 changes: 2 additions & 0 deletions mm/vmm.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ void *get_free_pages(unsigned int order, uint32_t flags) {
L2_PROT_USER, L1_PROT_USER);
if (flags & GFP_KERNEL)
va = kmap(mfn, order, L4_PROT, L3_PROT, L2_PROT, L1_PROT);
if (flags & GFP_KERNEL_MAP)
va = mmap(mfn, order, L4_PROT, L3_PROT, L2_PROT, L1_PROT);

return va;
}
Expand Down