riscv/addrenv: Make private function get_pgtable into a public one

The utility function can be used from other places
This commit is contained in:
Ville Juven 2022-11-16 14:32:07 +02:00 committed by Xiang Xiao
parent 192a639f8f
commit 85470adcc3
4 changed files with 116 additions and 43 deletions

View File

@ -103,4 +103,5 @@ endif
ifeq ($(CONFIG_ARCH_ADDRENV),y)
CMN_CSRCS += riscv_addrenv.c riscv_pgalloc.c riscv_addrenv_perms.c
CMN_CSRCS += riscv_addrenv_utils.c
endif

View File

@ -48,5 +48,24 @@
* Public Function Prototypes
****************************************************************************/
/****************************************************************************
* Name: riscv_get_pgtable
*
* Description:
* Get the physical address of the final level page table corresponding to
* 'vaddr'. If one does not exist, it will be allocated.
*
* Input Parameters:
* addrenv - Pointer to a structure describing the address environment
* vaddr - Virtual address to query for
*
* Returned Value:
* The physical address of the corresponding final level page table, or
* NULL if one does not exist, and there is no free memory to allocate one
*
****************************************************************************/
uintptr_t riscv_get_pgtable(group_addrenv_t *addrenv, uintptr_t vaddr);
#endif /* CONFIG_ARCH_ADDRENV */
#endif /* __ARCH_RISC_V_SRC_COMMON_ADDRENV_H */

View File

@ -0,0 +1,94 @@
/****************************************************************************
* arch/risc-v/src/common/riscv_addrenv_utils.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <nuttx/arch.h>
#include <nuttx/addrenv.h>
#include <nuttx/irq.h>
#include <nuttx/pgalloc.h>
#include <nuttx/sched.h>
#include <arch/barriers.h>
#include "pgalloc.h"
#include "riscv_mmu.h"
#ifdef CONFIG_BUILD_KERNEL
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: riscv_get_pgtable
*
* Description:
* Get the physical address of the final level page table corresponding to
* 'vaddr'. If one does not exist, it will be allocated.
*
* Input Parameters:
* addrenv - Pointer to a structure describing the address environment
* vaddr - Virtual address to query for
*
* Returned Value:
* The physical address of the corresponding final level page table, or
* NULL if one does not exist, and there is no free memory to allocate one
*
****************************************************************************/
uintptr_t riscv_get_pgtable(group_addrenv_t *addrenv, uintptr_t vaddr)
{
uintptr_t paddr;
uintptr_t ptprev;
uint32_t ptlevel;
/* Get the current level MAX_LEVELS-1 entry corresponding to this vaddr */
ptlevel = ARCH_SPGTS;
ptprev = riscv_pgvaddr(addrenv->spgtables[ARCH_SPGTS - 1]);
paddr = mmu_pte_to_paddr(mmu_ln_getentry(ptlevel, ptprev, vaddr));
if (!paddr)
{
/* No page table has been allocated... allocate one now */
paddr = mm_pgalloc(1);
if (paddr)
{
/* Wipe the page and assign it */
riscv_pgwipe(paddr);
mmu_ln_setentry(ptlevel, ptprev, paddr, vaddr, MMU_UPGT_FLAGS);
}
}
/* Flush the data cache, so the changes are committed to memory */
__DMB();
return paddr;
}
#endif /* CONFIG_BUILD_KERNEL */

View File

@ -37,6 +37,7 @@
#include <arch/barriers.h>
#include "addrenv.h"
#include "pgalloc.h"
#include "riscv_mmu.h"
@ -54,48 +55,6 @@
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: get_pgtable
*
* Description:
* Get the physical address of the last page table level corresponding to
* 'vaddr'
*
****************************************************************************/
static uintptr_t get_pgtable(group_addrenv_t *addrenv, uintptr_t vaddr)
{
uintptr_t paddr;
uintptr_t ptprev;
uint32_t ptlevel;
/* Get the current level MAX_LEVELS-1 entry corresponding to this vaddr */
ptlevel = ARCH_SPGTS;
ptprev = riscv_pgvaddr(addrenv->spgtables[ARCH_SPGTS - 1]);
paddr = mmu_pte_to_paddr(mmu_ln_getentry(ptlevel, ptprev, vaddr));
if (!paddr)
{
/* No page table has been allocated... allocate one now */
paddr = mm_pgalloc(1);
if (paddr)
{
/* Wipe the page and assign it */
riscv_pgwipe(paddr);
mmu_ln_setentry(ptlevel, ptprev, paddr, vaddr, MMU_UPGT_FLAGS);
}
}
/* Flush the data cache, so the changes are committed to memory */
__DMB();
return paddr;
}
/****************************************************************************
* Public Functions
****************************************************************************/
@ -172,7 +131,7 @@ uintptr_t pgalloc(uintptr_t brkaddr, unsigned int npages)
{
/* Get the address of the last level page table */
ptlast = riscv_pgvaddr(get_pgtable(&group->tg_addrenv, vaddr));
ptlast = riscv_pgvaddr(riscv_get_pgtable(&group->tg_addrenv, vaddr));
if (!ptlast)
{
return 0;