arch/addrenv: Change text write enable/disable to generic mprot

Implement a generic access rights modification procedure instead
of the procedures that only do one thing (enable/disable write)
to one section (text).
This commit is contained in:
Ville Juven
2022-05-12 11:55:50 +03:00
committed by Xiang Xiao
parent 072c28fe8e
commit 47e85b68fe
4 changed files with 71 additions and 88 deletions
+14 -25
View File
@@ -25,51 +25,40 @@
#include <nuttx/config.h>
#include <nuttx/arch.h>
#include <nuttx/compiler.h>
#include <sys/mman.h>
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: up_addrenv_text_enable_write
* Name: up_addrenv_mprot
*
* Description:
* Temporarily enable write access to the .text section. This must be
* called prior to loading the process code into memory.
* Modify access rights to an address range.
*
* Input Parameters:
* addrenv - The address environment to be modified.
* addr - Base address of the region.
* len - Size of the region.
* prot - Access right flags.
*
* Returned Value:
* Zero (OK) on success; a negated errno value on failure.
*
****************************************************************************/
int up_addrenv_text_enable_write(group_addrenv_t *addrenv)
int up_addrenv_mprot(group_addrenv_t *addrenv, uintptr_t addr, size_t len,
int prot)
{
/* Nothing needs to be done */
return OK;
}
/****************************************************************************
* Name: up_addrenv_text_disable_write
*
* Description:
* Disable write access to the .text section. This must be called after the
* process code is loaded into memory.
*
* Input Parameters:
* addrenv - The address environment to be modified.
*
* Returned Value:
* Zero (OK) on success; a negated errno value on failure.
*
****************************************************************************/
int up_addrenv_text_disable_write(group_addrenv_t *addrenv)
{
/* Nothing needs to be done */
UNUSED(addrenv);
UNUSED(addr);
UNUSED(len);
UNUSED(prot);
return OK;
}
+41 -38
View File
@@ -33,15 +33,22 @@
#include <arch/barriers.h>
#include <sys/mman.h>
#include "pgalloc.h"
#include "riscv_mmu.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define CLR_MASK (PTE_R | PTE_W | PTE_X)
/****************************************************************************
* Private Functions
****************************************************************************/
static int modify_region(uintptr_t vstart, uintptr_t vend, uintptr_t setmask,
uintptr_t clrmask)
static int modify_region(uintptr_t vstart, uintptr_t vend, uintptr_t setmask)
{
uintptr_t l1vaddr;
uintptr_t lnvaddr;
@@ -78,7 +85,7 @@ static int modify_region(uintptr_t vstart, uintptr_t vend, uintptr_t setmask,
/* Get entry and modify the flags */
entry = mmu_ln_getentry(ptlevel, lnvaddr, vaddr);
entry &= ~clrmask;
entry &= ~CLR_MASK;
entry |= setmask;
/* Restore the entry */
@@ -99,53 +106,49 @@ static int modify_region(uintptr_t vstart, uintptr_t vend, uintptr_t setmask,
****************************************************************************/
/****************************************************************************
* Name: up_addrenv_text_enable_write
* Name: up_addrenv_mprot
*
* Description:
* Temporarily enable write access to the .text section. This must be
* called prior to loading the process code into memory.
* Modify access rights to an address range.
*
* Input Parameters:
* addrenv - The address environment to be modified.
* addr - Base address of the region.
* len - Size of the region.
* prot - Access right flags.
*
* Returned Value:
* Zero (OK) on success; a negated errno value on failure.
*
****************************************************************************/
int up_addrenv_text_enable_write(group_addrenv_t *addrenv)
int up_addrenv_mprot(group_addrenv_t *addrenv, uintptr_t addr, size_t len,
int prot)
{
/* Sanity checks */
uintptr_t setmask;
uintptr_t vend;
DEBUGASSERT(addrenv);
DEBUGASSERT(MM_ISALIGNED(addrenv->textvbase));
DEBUGASSERT(MM_ISALIGNED(addrenv->datavbase));
/* addrenv not needed by this implementation */
return modify_region(addrenv->textvbase, addrenv->datavbase, PTE_W, 0);
}
/****************************************************************************
* Name: up_addrenv_text_disable_write
*
* Description:
* Disable write access to the .text section. This must be called after the
* process code is loaded into memory.
*
* Input Parameters:
* addrenv - The address environment to be modified.
*
* Returned Value:
* Zero (OK) on success; a negated errno value on failure.
*
****************************************************************************/
int up_addrenv_text_disable_write(group_addrenv_t *addrenv)
{
/* Sanity checks */
DEBUGASSERT(addrenv);
DEBUGASSERT(MM_ISALIGNED(addrenv->textvbase));
DEBUGASSERT(MM_ISALIGNED(addrenv->datavbase));
return modify_region(addrenv->textvbase, addrenv->datavbase, 0, PTE_W);
UNUSED(addrenv);
setmask = 0;
vend = addr + MM_PGALIGNUP(len);
if (prot & PROT_READ)
{
setmask |= PTE_R;
}
if (prot & PROT_WRITE)
{
setmask |= PTE_W;
}
if (prot & PROT_EXEC)
{
setmask |= PTE_X;
}
return modify_region(addr, vend, setmask);
}