This is an automated email from the ASF dual-hosted git repository. xiaoxiang pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/incubator-nuttx.git
commit 85470adcc3283113ca35e922631e474f5ce3cda7 Author: Ville Juven <ville.ju...@unikie.com> AuthorDate: Wed Nov 16 14:32:07 2022 +0200 riscv/addrenv: Make private function get_pgtable into a public one The utility function can be used from other places --- arch/risc-v/src/common/Make.defs | 1 + arch/risc-v/src/common/addrenv.h | 19 ++++++ arch/risc-v/src/common/riscv_addrenv_utils.c | 94 ++++++++++++++++++++++++++++ arch/risc-v/src/common/riscv_pgalloc.c | 45 +------------ 4 files changed, 116 insertions(+), 43 deletions(-) diff --git a/arch/risc-v/src/common/Make.defs b/arch/risc-v/src/common/Make.defs index e7d7940e69..54fae2ae52 100644 --- a/arch/risc-v/src/common/Make.defs +++ b/arch/risc-v/src/common/Make.defs @@ -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 diff --git a/arch/risc-v/src/common/addrenv.h b/arch/risc-v/src/common/addrenv.h index fc89138984..9c709d11d4 100644 --- a/arch/risc-v/src/common/addrenv.h +++ b/arch/risc-v/src/common/addrenv.h @@ -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 */ diff --git a/arch/risc-v/src/common/riscv_addrenv_utils.c b/arch/risc-v/src/common/riscv_addrenv_utils.c new file mode 100644 index 0000000000..23d1f09f87 --- /dev/null +++ b/arch/risc-v/src/common/riscv_addrenv_utils.c @@ -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 */ diff --git a/arch/risc-v/src/common/riscv_pgalloc.c b/arch/risc-v/src/common/riscv_pgalloc.c index 643d77dcd8..e66939c8ed 100644 --- a/arch/risc-v/src/common/riscv_pgalloc.c +++ b/arch/risc-v/src/common/riscv_pgalloc.c @@ -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;