pussuw commented on code in PR #6069:
URL: https://github.com/apache/incubator-nuttx/pull/6069#discussion_r859608473


##########
arch/risc-v/src/common/riscv_addrenv.c:
##########
@@ -0,0 +1,788 @@
+/****************************************************************************
+ * arch/risc-v/src/common/riscv_addrenv.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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Address Environment Interfaces
+ *
+ * Low-level interfaces used in binfmt/ to instantiate tasks with address
+ * environments.  These interfaces all operate on type group_addrenv_t which
+ * is an abstract representation of a task group's address environment and
+ * must be defined in arch/arch.h if CONFIG_ARCH_ADDRENV is defined.
+ *
+ *   up_addrenv_create   - Create an address environment
+ *   up_addrenv_destroy  - Destroy an address environment.
+ *   up_addrenv_vtext    - Returns the virtual base address of the .text
+ *                         address environment
+ *   up_addrenv_vdata    - Returns the virtual base address of the .bss/.data
+ *                         address environment
+ *   up_addrenv_heapsize - Returns the size of the initial heap allocation.
+ *   up_addrenv_select   - Instantiate an address environment
+ *   up_addrenv_restore  - Restore an address environment
+ *   up_addrenv_clone    - Copy an address environment from one location to
+ *                        another.
+ *
+ * Higher-level interfaces used by the tasking logic.  These interfaces are
+ * used by the functions in sched/ and all operate on the thread which whose
+ * group been assigned an address environment by up_addrenv_clone().
+ *
+ *   up_addrenv_attach   - Clone the address environment assigned to one TCB
+ *                         to another.  This operation is done when a pthread
+ *                         is created that share's the same address
+ *                         environment.
+ *   up_addrenv_detach   - Release the threads reference to an address
+ *                         environment when a task/thread exits.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <errno.h>
+#include <assert.h>
+#include <debug.h>
+
+#include <nuttx/addrenv.h>
+#include <nuttx/arch.h>
+#include <nuttx/irq.h>
+#include <nuttx/pgalloc.h>
+
+#include <arch/barriers.h>
+
+#include "pgalloc.h"
+#include "riscv_mmu.h"
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/* Only CONFIG_BUILD_KERNEL is supported (i.e. tested) */
+
+#ifndef CONFIG_BUILD_KERNEL
+#  error "This module is intended to be used with CONFIG_BUILD_KERNEL"
+#endif
+
+/* Entries per PGT */
+
+#define ENTRIES_PER_PGT     (RV_MMU_PAGE_ENTRIES)
+
+/* Base address for address environment */
+
+#define ADDRENV_VBASE       (CONFIG_ARCH_DATA_VBASE)
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+extern uintptr_t            g_kernel_mappings;
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: map_spgtables
+ *
+ * Description:
+ *   Map vaddr to the static page tables.
+ *
+ * Input Parameters:
+ *   addrenv - Describes the address environment
+ *
+ ****************************************************************************/
+
+static void map_spgtables(group_addrenv_t *addrenv, uintptr_t vaddr)
+{
+  int       i;
+  uintptr_t prev;
+
+  /* Start from L1, and connect until max level - 1 */
+
+  prev = riscv_pgvaddr(addrenv->spgtables[0]);
+
+  /* Check if the mapping already exists */
+
+  if (mmu_ln_getentry(1, prev, vaddr) != 0)
+    {
+      return;
+    }
+
+  /* No mapping yet, create it */
+
+  for (i = 0; i < (ARCH_SPGTS - 1); i++)
+    {
+      uintptr_t next = riscv_pgvaddr(addrenv->spgtables[i + 1]);
+      mmu_ln_setentry(i + 1, prev, next, vaddr, MMU_UPGT_FLAGS);
+      prev = next;
+    }
+}
+
+/****************************************************************************
+ * Name: create_spgtables
+ *
+ * Description:
+ *   Create the static page tables. Allocate memory for them and connect them
+ *   together.
+ *
+ * Input Parameters:
+ *   addrenv - Describes the address environment
+ *
+ * Returned value:
+ *   Amount of pages created on success; a negated errno value on failure
+ *
+ ****************************************************************************/
+
+static int create_spgtables(group_addrenv_t *addrenv)
+{
+  int       i;
+  uintptr_t paddr;
+
+  for (i = 0; i < ARCH_SPGTS; i++)
+    {
+      paddr = mm_pgalloc(1);
+      if (!paddr)
+        {
+          return -ENOMEM;
+        }
+
+      /* Wipe the memory and assign it */
+
+      riscv_pgwipe(paddr);
+      addrenv->spgtables[i] = paddr;
+    }
+
+  /* Flush the data cache, so the changes are committed to memory */
+
+  __DMB();
+
+  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 = riscv_pgvaddr(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
+ *
+ * Description:
+ *   Map a single region of memory to MMU. Assumes that the static page
+ *   tables exist. Allocates the final level page tables and commits the
+ *   region memory to physical memory.
+ *
+ * Input Parameters:
+ *   addrenv - Describes the address environment
+ *   vaddr - Base virtual address for the mapping
+ *   size - Size of the region in bytes
+ *   mmuflags - MMU flags to use
+ *
+ * Returned value:
+ *   Amount of pages created on success; a negated errno value on failure
+ *
+ ****************************************************************************/
+
+static int create_region(group_addrenv_t *addrenv, uintptr_t vaddr,
+                         size_t size, uint32_t mmuflags)
+{
+  uintptr_t ptlast;
+  uintptr_t ptprev;
+  uintptr_t paddr;
+  uint32_t  ptlevel;
+  int       npages;
+  int       nmapped;
+  int       i;
+  int       j;
+
+  nmapped   = 0;
+  npages    = MM_NPAGES(size);
+  ptprev    = riscv_pgvaddr(addrenv->spgtables[ARCH_SPGTS - 1]);
+  ptlevel   = ARCH_SPGTS;
+
+  /* Create mappings for the lower level tables */
+
+  map_spgtables(addrenv, vaddr);
+
+  /* Begin allocating memory for the page tables */
+
+  for (i = 0; i < npages; i += ENTRIES_PER_PGT)
+    {
+      /* Get the current final level entry corresponding to this vaddr */
+
+      paddr = mmu_pte_to_paddr(mmu_ln_getentry(ptlevel, ptprev, vaddr));
+
+      if (paddr == 0)
+        {
+          /* Nothing yet, allocate one page for final level page table */
+
+          paddr = mm_pgalloc(1);
+          if (!paddr)
+            {
+              return -ENOMEM;
+            }
+
+          /* Map the page table to the prior level */
+
+          mmu_ln_setentry(ptlevel, ptprev, paddr, vaddr, MMU_UPGT_FLAGS);
+
+          /* This is then used to map the final level */
+
+          riscv_pgwipe(paddr);
+        }
+
+      ptlast = riscv_pgvaddr(paddr);
+
+      /* Then allocate memory for the region data */
+
+      for (j = 0; j < ENTRIES_PER_PGT && nmapped < size; j++)
+        {
+          paddr = mm_pgalloc(1);
+          if (!paddr)
+            {
+              return -ENOMEM;
+            }
+
+          /* Wipe the physical page memory */
+
+          riscv_pgwipe(paddr);
+
+          /* Then map the virtual address to the physical address */
+
+          mmu_ln_setentry(ptlevel + 1, ptlast, paddr, vaddr, mmuflags);
+          nmapped   += MM_PGSIZE;
+          vaddr     += MM_PGSIZE;

Review Comment:
   Sure



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to