From b74218f2aeeda3f7ec18a54c9f2e6bee9a4a6c94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Povi=C5=A1er?= Date: Wed, 22 Feb 2023 15:24:21 +0100 Subject: [PATCH] display: Factor out top of memory allocation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Move it into utils.c before we reuse it for SIO data. Signed-off-by: Martin PoviĊĦer --- src/display.c | 7 +------ src/utils.c | 20 ++++++++++++++++++++ src/utils.h | 1 + 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/display.c b/src/display.c index 3dbf49ea..21bf41f3 100644 --- a/src/display.c +++ b/src/display.c @@ -369,12 +369,7 @@ int display_configure(const char *config) return -1; } - cur_boot_args.mem_size -= size; - fb_pa = cur_boot_args.phys_base + cur_boot_args.mem_size; - /* add guard page between RAM and framebuffer */ - // TODO: update mapping? - cur_boot_args.mem_size -= SZ_16K; - + fb_pa = top_of_memory_alloc(size); memset((void *)fb_pa, 0, size); tmp_dva = iova_alloc(dcp->iovad_dcp, size); diff --git a/src/utils.c b/src/utils.c index 23434762..ff8dd84b 100644 --- a/src/utils.c +++ b/src/utils.c @@ -180,3 +180,23 @@ bool is_heap(void *addr) return p > top_of_kernel_data && p < top_of_ram; } + +// TODO: update mapping? +u64 top_of_memory_alloc(size_t size) +{ + static bool guard_page_inserted = false; + cur_boot_args.mem_size -= ALIGN_UP(size, SZ_16K); + u64 ret = cur_boot_args.phys_base + cur_boot_args.mem_size; + + if (!guard_page_inserted) { + cur_boot_args.mem_size -= SZ_16K; + guard_page_inserted = true; + } else { + // If the guard page was already there, move it down and allocate + // above it -- this is accomplished by simply shifting the allocated + // region by one page up. + ret += SZ_16K; + } + + return ret; +} diff --git a/src/utils.h b/src/utils.h index 1d053d2e..0e76221d 100644 --- a/src/utils.h +++ b/src/utils.h @@ -440,5 +440,6 @@ extern struct vector_args next_stage; void deep_wfi(void); bool is_heap(void *addr); +u64 top_of_memory_alloc(size_t size); #endif