From 1322f82802c9ab04a7cca88a6b3a5cb3a8fb34fd Mon Sep 17 00:00:00 2001 From: Ville Juven Date: Mon, 17 Jan 2022 11:31:19 +0200 Subject: [PATCH] RISC-V: Copy kernel memory mappings to userspace addrenv Copy the kernel mappings to the new (user) address environment. The copyuing is done exactly once. This relies on the fact that the kernel L1/L2 mappings will never change, as all of the kernel memory is mapped upon boot. --- arch/risc-v/src/common/riscv_addrenv.c | 48 ++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/arch/risc-v/src/common/riscv_addrenv.c b/arch/risc-v/src/common/riscv_addrenv.c index 4f1485a6a4..539c179797 100644 --- a/arch/risc-v/src/common/riscv_addrenv.c +++ b/arch/risc-v/src/common/riscv_addrenv.c @@ -88,6 +88,12 @@ #define ADDRENV_VBASE (CONFIG_ARCH_DATA_VBASE) +/**************************************************************************** + * Public Data + ****************************************************************************/ + +extern uintptr_t g_kernel_mappings; + /**************************************************************************** * Private Functions ****************************************************************************/ @@ -187,6 +193,38 @@ static int create_spgtables(group_addrenv_t *addrenv) return i; } +/**************************************************************************** + * Name: copy_kernel_mappings + * + * Description: + * Copy kernel mappings to address environment. Expects that the user page + * table does not contain any mappings yet (as they will be wiped). + * + * Input Parameters: + * addrenv - Describes the address environment. The page tables must exist + * at this point. + * + * Returned value: + * OK on success, ERROR on failure + * + ****************************************************************************/ + +static int copy_kernel_mappings(group_addrenv_t *addrenv) +{ + uintptr_t user_mappings = addrenv->spgtables[0]; + + /* Copy the L1 references */ + + if (user_mappings == 0) + { + return -EINVAL; + } + + memcpy((void *)user_mappings, (void *)g_kernel_mappings, RV_MMU_PAGE_SIZE); + + return OK; +} + /**************************************************************************** * Name: create_region * @@ -343,6 +381,16 @@ int up_addrenv_create(size_t textsize, size_t datasize, size_t heapsize, goto errout; } + /* Map the kernel memory for the user */ + + ret = copy_kernel_mappings(addrenv); + + if (ret < 0) + { + serr("ERROR: Failed to copy kernel mappings to new environment"); + goto errout; + } + /* Calculate the base addresses for convenience */ resvbase = ADDRENV_VBASE;