From f78500d603664f4b6a14f381016457de415383c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Lov=C3=A9n?= Date: Fri, 29 Dec 2017 21:46:00 +0100 Subject: [PATCH] PMM -- Make sure stuff works even with V2P and P2V --- src/kernel/memory/pmm.c | 2 +- src/kernel/memory/pmm.tt | 22 +++++++++++----------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/kernel/memory/pmm.c b/src/kernel/memory/pmm.c index 5f1c679..f979351 100644 --- a/src/kernel/memory/pmm.c +++ b/src/kernel/memory/pmm.c @@ -13,6 +13,6 @@ uintptr_t pmm_alloc() { uintptr_t page = first; first = page?*(uintptr_t *)page:0; - page = (uintptr_t)V2P(page); + page = (uintptr_t)(page?V2P(page):0); return page; } diff --git a/src/kernel/memory/pmm.tt b/src/kernel/memory/pmm.tt index cb9b8e1..969009b 100644 --- a/src/kernel/memory/pmm.tt +++ b/src/kernel/memory/pmm.tt @@ -3,9 +3,9 @@ #include #undef P2V -#define P2V(addr) (void *)((uintptr_t)(addr)) +#define P2V(addr) ((void *)((uintptr_t)(addr)+1)) #undef V2P -#define V2P(addr) (void *)((uintptr_t)(addr)) +#define V2P(addr) ((uintptr_t)(addr)-1) #include "pmm.c" struct { @@ -14,9 +14,9 @@ struct { TEST(alloc_returns_freed_page) { - pmm_free((uintptr_t)&mem[0]); + pmm_free(V2P(&mem[0])); uintptr_t a = pmm_alloc(); - ASSERT_EQ_PTR(a, &mem[0]); + ASSERT_EQ_PTR(a, V2P(&mem[0])); } TEST(alloc_returns_0_if_no_free_pages) { @@ -25,7 +25,7 @@ TEST(alloc_returns_0_if_no_free_pages) } TEST(alloc_zero_after_all_free_pages) { - pmm_free((uintptr_t)&mem[0]); + pmm_free(V2P(&mem[0])); pmm_alloc(); uintptr_t a = pmm_alloc(); ASSERT_EQ_PTR(a, 0); @@ -33,24 +33,24 @@ TEST(alloc_zero_after_all_free_pages) TEST(alloc_two_pages___first_page_is_not_zero) { - pmm_free((uintptr_t)&mem[0]); - pmm_free((uintptr_t)&mem[1]); + pmm_free(V2P(&mem[0])); + pmm_free(V2P(&mem[1])); uintptr_t a = pmm_alloc(); pmm_alloc(); ASSERT_NEQ_PTR(a, 0); } TEST(alloc_two_pages___second_page_is_not_zero) { - pmm_free((uintptr_t)&mem[0]); - pmm_free((uintptr_t)&mem[1]); + pmm_free(V2P(&mem[0])); + pmm_free(V2P(&mem[1])); pmm_alloc(); uintptr_t a = pmm_alloc(); ASSERT_NEQ_PTR(a, 0); } TEST(alloc_two_pages___doesnt_return_same_page_twice) { - pmm_free((uintptr_t)&mem[0]); - pmm_free((uintptr_t)&mem[1]); + pmm_free(V2P(&mem[0])); + pmm_free(V2P(&mem[1])); uintptr_t a = pmm_alloc(); uintptr_t b = pmm_alloc(); ASSERT_NEQ_PTR(a, b);