diff --git a/arch/risc-v/src/common/addrenv.h b/arch/risc-v/src/common/addrenv.h index 051d04758fd..2e0d7436e1d 100644 --- a/arch/risc-v/src/common/addrenv.h +++ b/arch/risc-v/src/common/addrenv.h @@ -44,6 +44,14 @@ # define ARCH_KERNEL_STACKSIZE STACK_ALIGN_UP(CONFIG_ARCH_KERNEL_STACKSIZE) #endif +/* Base address for address environment */ + +#if CONFIG_ARCH_TEXT_VBASE != 0 +# define ARCH_ADDRENV_VBASE (CONFIG_ARCH_TEXT_VBASE) +#else +# define ARCH_ADDRENV_VBASE (CONFIG_ARCH_DATA_VBASE) +#endif + /**************************************************************************** * Public Function Prototypes ****************************************************************************/ diff --git a/arch/risc-v/src/common/pgalloc.h b/arch/risc-v/src/common/pgalloc.h index db31a247aa4..9f01f839076 100644 --- a/arch/risc-v/src/common/pgalloc.h +++ b/arch/risc-v/src/common/pgalloc.h @@ -32,6 +32,8 @@ #include #include +#include "addrenv.h" + /**************************************************************************** * Pre-processor Definitions ****************************************************************************/ @@ -78,6 +80,24 @@ static inline uintptr_t riscv_pgvaddr(uintptr_t paddr) } #endif /* CONFIG_ARCH_PGPOOL_MAPPING */ +/**************************************************************************** + * Name: riscv_uservaddr + * + * Description: + * Return true if the virtual address, vaddr, lies in the user address + * space. + * + ****************************************************************************/ + +static inline bool riscv_uservaddr(uintptr_t vaddr) +{ + /* Check if this address is within the range of the virtualized .bss/.data, + * heap, or stack regions. + */ + + return vaddr >= ARCH_ADDRENV_VBASE; +} + /**************************************************************************** * Name: riscv_pgwipe * diff --git a/arch/risc-v/src/common/riscv_addrenv.c b/arch/risc-v/src/common/riscv_addrenv.c index e5074cfa5f1..44adfe3a582 100644 --- a/arch/risc-v/src/common/riscv_addrenv.c +++ b/arch/risc-v/src/common/riscv_addrenv.c @@ -68,6 +68,7 @@ #include +#include "addrenv.h" #include "pgalloc.h" #include "riscv_mmu.h" @@ -85,17 +86,9 @@ #define ENTRIES_PER_PGT (RV_MMU_PAGE_ENTRIES) -/* Base address for address environment */ - -#if CONFIG_ARCH_TEXT_VBASE != 0 -# define ADDRENV_VBASE (CONFIG_ARCH_TEXT_VBASE) -#else -# define ADDRENV_VBASE (CONFIG_ARCH_DATA_VBASE) -#endif - /* Make sure the address environment virtual address boundary is valid */ -static_assert((ADDRENV_VBASE & RV_MMU_SECTION_ALIGN) == 0, +static_assert((ARCH_ADDRENV_VBASE & RV_MMU_SECTION_ALIGN) == 0, "Addrenv start address is not aligned to section boundary"); /**************************************************************************** @@ -382,7 +375,7 @@ int up_addrenv_create(size_t textsize, size_t datasize, size_t heapsize, uintptr_t heapbase; DEBUGASSERT(addrenv); - DEBUGASSERT(MM_ISALIGNED(ADDRENV_VBASE)); + DEBUGASSERT(MM_ISALIGNED(ARCH_ADDRENV_VBASE)); /* Make sure the address environment is wiped before doing anything */ @@ -417,7 +410,7 @@ int up_addrenv_create(size_t textsize, size_t datasize, size_t heapsize, database = resvbase + MM_PGALIGNUP(resvsize); heapbase = CONFIG_ARCH_HEAP_VBASE; #else - resvbase = ADDRENV_VBASE; + resvbase = ARCH_ADDRENV_VBASE; resvsize = ARCH_DATA_RESERVE_SIZE; textbase = resvbase + MM_PGALIGNUP(resvsize); database = textbase + MM_PGALIGNUP(textsize); @@ -532,7 +525,7 @@ int up_addrenv_destroy(arch_addrenv_t *addrenv) /* Things start from the beginning of the user virtual memory */ - vaddr = ADDRENV_VBASE; + vaddr = ARCH_ADDRENV_VBASE; pgsize = mmu_get_region_size(ARCH_SPGTS); /* First destroy the allocated memory and the final level page table */