diff --git a/arch/risc-v/src/common/riscv_addrenv.c b/arch/risc-v/src/common/riscv_addrenv.c index 4f1485a6a47..539c179797f 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;