You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by ac...@apache.org on 2026/10/22 10:40:07 UTC

(nuttx) branch master updated (8b6495a68bc -> 3d2ee4f49b0)

This is an automated email from the ASF dual-hosted git repository.

acassis pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git


    from 8b6495a68bc Documentation: Describe the coverage of loadable modules.
     new a79f9bac76b arch/arm64: Fix the style of arm64_cache.c.
     new 0e9bb1915e7 arch/arm64: Wait for the instruction cache invalidation.
     new ca56af65d02 arch/arm64: Check the range in up_addrenv_mprot() and support PROT_NONE.
     new 96bd521662d arch/risc-v: Check the range in up_addrenv_mprot().
     new bfa56a95d99 arch/risc-v: Flush all harts in up_addrenv_mprot().
     new 07c4a9165d6 fs/mmap: Implement mprotect().
     new cf9578b75a3 arch/arm64: Keep the page permissions in fork().
     new 3d2ee4f49b0 Documentation: Update the mprotect() support.

The 8 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 Documentation/standards/posix.rst            |  18 +++--
 arch/arm64/src/common/arm64_addrenv_mmu.c    |  33 +--------
 arch/arm64/src/common/arm64_addrenv_perms.c  |  58 ++++++++++-----
 arch/arm64/src/common/arm64_cache.c          |   9 +++
 arch/risc-v/src/common/riscv_addrenv_perms.c | 102 ++++++++++++++++++++++-----
 fs/mmap/fs_mmisc.c                           |  69 +++++++++++++++++-
 6 files changed, 215 insertions(+), 74 deletions(-)


(nuttx) 08/08: Documentation: Update the mprotect() support.

Posted by ac...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

acassis pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit 3d2ee4f49b01857789b91a79e796f04741e44108
Author: Marco Casaroli <ma...@gmail.com>
AuthorDate: Mon Oct 19 21:34:40 2026 +0200

    Documentation: Update the mprotect() support.
    
    mprotect() checks its arguments in all builds.  It changes the page
    permissions with CONFIG_ARCH_ADDRENV on arm64 and RISC-V.  On other
    targets a valid request returns success and changes nothing.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <ma...@gmail.com>
---
 Documentation/standards/posix.rst | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)

diff --git a/Documentation/standards/posix.rst b/Documentation/standards/posix.rst
index 4e3d3ac034c..6d010beb7a0 100644
--- a/Documentation/standards/posix.rst
+++ b/Documentation/standards/posix.rst
@@ -193,9 +193,10 @@ POSIX.1 Option Requirements
 | `_POSIX_THREAD_PROCESS_SHARED`_ | 201712L     |                           |
 +---------------------------------+-------------+---------------------------+
 
-.. [#fn9] NuttX provides dummy implementations for ``_POSIX_MEMORY_PROTECTION``
-          which is sufficient for devices without MMU, but doesn't meet the
-          requirement for devices with MMU.
+.. [#fn9] ``mprotect()`` changes the page permissions only with
+          ``CONFIG_ARCH_ADDRENV`` on arm64 and RISC-V.  On other targets it
+          checks the arguments and changes nothing, which is sufficient for
+          devices without MMU.
 
 POSIX PSE54 - Multipurpose
 --------------------------
@@ -2923,11 +2924,16 @@ _POSIX_MEMORY_PROTECTION
 +--------------------------------+----------------+
 | API                            | Support        |
 +================================+================+
-| :c:func:`mprotect`             | Dummy [#fn17]_ |
+| :c:func:`mprotect`             | Yes [#fn17]_   |
 +--------------------------------+----------------+
 
-.. [#fn17] The function returns a success value but doesn't implement
-           the required functionality.
+.. [#fn17] All builds return ``EINVAL`` when ``addr`` is not a multiple of
+           ``sysconf(_SC_PAGESIZE)`` or ``prot`` has an unknown bit.  With
+           ``CONFIG_ARCH_ADDRENV`` on arm64 and RISC-V the function changes
+           the page permissions, and returns ``ENOMEM`` when a page of the
+           range is not mapped.  Other configurations return a success value
+           for a valid request and change nothing: without an MMU the
+           permissions cannot be enforced.
 
 _POSIX_PRIORITIZED_IO
 ---------------------


(nuttx) 07/08: arch/arm64: Keep the page permissions in fork().

Posted by ac...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

acassis pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit cf9578b75a3e9a882f326ce4e08e3c9d21bfa1af
Author: Marco Casaroli <ma...@gmail.com>
AuthorDate: Mon Oct 19 21:34:40 2026 +0200

    arch/arm64: Keep the page permissions in fork().
    
    up_addrenv_fork() mapped each copied page with the default flags: read
    and execute for .text, read and write for other pages.  Permissions
    that mprotect() changed in the parent were lost in the child.  For
    example a read-only .bss page became writable in the child.
    
    Now the child page gets the same attributes as the parent page.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <ma...@gmail.com>
---
 arch/arm64/src/common/arm64_addrenv_mmu.c | 33 +++----------------------------
 1 file changed, 3 insertions(+), 30 deletions(-)

diff --git a/arch/arm64/src/common/arm64_addrenv_mmu.c b/arch/arm64/src/common/arm64_addrenv_mmu.c
index c4f4e3c79d0..874a9eefba9 100644
--- a/arch/arm64/src/common/arm64_addrenv_mmu.c
+++ b/arch/arm64/src/common/arm64_addrenv_mmu.c
@@ -382,33 +382,6 @@ static inline bool vaddr_is_shm(uintptr_t vaddr)
 #endif
 }
 
-#ifdef CONFIG_ARCH_HAVE_FORK
-/****************************************************************************
- * Name: vaddr_is_text
- *
- * Description:
- *   Check if a vaddr is part of the .text area, which is mapped read/execute
- *   while everything else is mapped read/write.  The two arms mirror exactly
- *   the two layouts up_addrenv_create() builds.
- *
- ****************************************************************************/
-
-static inline bool vaddr_is_text(const arch_addrenv_t *addrenv,
-                                 uintptr_t vaddr)
-{
-#if (CONFIG_ARCH_TEXT_VBASE != 0x0) && (CONFIG_ARCH_HEAP_VBASE != 0x0)
-  UNUSED(addrenv);
-  return vaddr >= CONFIG_ARCH_TEXT_VBASE && vaddr < ARCH_TEXT_VEND;
-#else
-  /* Contiguous layout:  the reserve sits below .text, and .data begins where
-   * .text ends.
-   */
-
-  return vaddr >= addrenv->textvbase && vaddr < addrenv->datavbase;
-#endif
-}
-#endif /* CONFIG_ARCH_HAVE_FORK */
-
 /****************************************************************************
  * Public Functions
  ****************************************************************************/
@@ -707,10 +680,10 @@ int up_addrenv_fork(const arch_addrenv_t *src, arch_addrenv_t *dest)
           memcpy((void *)arm64_pgvaddr(destpage),
                  (const void *)arm64_pgvaddr(srcpage), MM_PGSIZE);
 
+          /* Keep the permissions, which mprotect() may have changed */
+
           mmu_ln_setentry(MMU_PGT_LEVEL_MAX, (uintptr_t)dptlast, destpage,
-                          pgvaddr,
-                          vaddr_is_text(src, pgvaddr) ? MMU_UTEXT_FLAGS
-                                                      : MMU_UDATA_FLAGS);
+                          pgvaddr, sptlast[j] & ~PTE_PADDR_MASK);
         }
     }
 


(nuttx) 05/08: arch/risc-v: Flush all harts in up_addrenv_mprot().

Posted by ac...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

acassis pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit bfa56a95d99e4a902cb9107d7dcb3dc6546702c1
Author: Marco Casaroli <ma...@gmail.com>
AuthorDate: Mon Oct 19 22:06:44 2026 +0200

    arch/risc-v: Flush all harts in up_addrenv_mprot().
    
    up_addrenv_mprot() changed the page entries and then ran sfence.vma
    and fence.i on the local hart only.  On SMP, another hart can run a
    thread of the same process: it kept the old permissions in its TLB,
    and its instruction fetch could miss code written to the pages before
    they became executable.
    
    With CONFIG_SMP, now each hart runs sfence.vma and fence.i, through
    nxsched_smp_call().  Without SMP the local fence.i stays.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <ma...@gmail.com>
---
 arch/risc-v/src/common/riscv_addrenv_perms.c | 32 ++++++++++++++++++++++++++--
 1 file changed, 30 insertions(+), 2 deletions(-)

diff --git a/arch/risc-v/src/common/riscv_addrenv_perms.c b/arch/risc-v/src/common/riscv_addrenv_perms.c
index cea004db7c8..3f40d9f195e 100644
--- a/arch/risc-v/src/common/riscv_addrenv_perms.c
+++ b/arch/risc-v/src/common/riscv_addrenv_perms.c
@@ -32,6 +32,7 @@
 #include <nuttx/addrenv.h>
 #include <nuttx/arch.h>
 #include <nuttx/pgalloc.h>
+#include <nuttx/sched.h>
 
 #include <arch/barriers.h>
 
@@ -50,6 +51,16 @@
  * Private Functions
  ****************************************************************************/
 
+#ifdef CONFIG_SMP
+static int flush_hart(FAR void *arg)
+{
+  UNUSED(arg);
+  mmu_invalidate_tlbs();
+  UP_ISB();
+  return OK;
+}
+#endif
+
 static uintptr_t get_pgtable(uintptr_t l1vaddr, uintptr_t vaddr)
 {
   uintptr_t lnvaddr;
@@ -131,10 +142,27 @@ static int modify_region(uintptr_t vstart, uintptr_t vend, uintptr_t setmask)
       mmu_ln_restore(RV_MMU_PT_LEVELS, lnvaddr, vaddr, entry);
     }
 
-  /* When all is set and done, flush the data caches */
+  /* Make the new entries and the code in the pages visible to instruction
+   * fetch: sfence.vma and fence.i act on one hart only.
+   */
 
-  UP_ISB();
   UP_DMB();
+#ifdef CONFIG_SMP
+  {
+    cpu_set_t cpus;
+    int cpu;
+
+    CPU_ZERO(&cpus);
+    for (cpu = 0; cpu < CONFIG_SMP_NCPUS; cpu++)
+      {
+        CPU_SET(cpu, &cpus);
+      }
+
+    nxsched_smp_call(cpus, flush_hart, NULL);
+  }
+#else
+  UP_ISB();
+#endif
 
   return OK;
 }


(nuttx) 03/08: arch/arm64: Check the range in up_addrenv_mprot() and support PROT_NONE.

Posted by ac...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

acassis pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit ca56af65d022192f510e2b8dfecde452181481ed
Author: Marco Casaroli <ma...@gmail.com>
AuthorDate: Mon Oct 19 21:34:40 2026 +0200

    arch/arm64: Check the range in up_addrenv_mprot() and support PROT_NONE.
    
    up_addrenv_mprot() walked the page tables for each page of the range
    and changed the last level entry.  It did not check that the address
    is a user address, it changed entries that are not valid, and it left
    the first pages changed when a later page had no page table.  It also
    always gave user read access, so PROT_NONE gave a readable page.
    
    Now the function first checks each page: a user address with a valid
    page entry.  If one page fails the check, it changes nothing and
    returns -ENOMEM.  Then it changes the entries.  PROT_NONE removes the
    user access (AP[1] clear, UXN set).
    
    The ELF loader is the only caller today, and its .text range passes
    the check.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <ma...@gmail.com>
---
 arch/arm64/src/common/arm64_addrenv_perms.c | 58 ++++++++++++++++++++---------
 1 file changed, 40 insertions(+), 18 deletions(-)

diff --git a/arch/arm64/src/common/arm64_addrenv_perms.c b/arch/arm64/src/common/arm64_addrenv_perms.c
index 91176daf604..93251d2083d 100644
--- a/arch/arm64/src/common/arm64_addrenv_perms.c
+++ b/arch/arm64/src/common/arm64_addrenv_perms.c
@@ -51,14 +51,28 @@
  * Private Functions
  ****************************************************************************/
 
+static uintptr_t get_pgtable(uintptr_t l1vaddr, uintptr_t vaddr)
+{
+  uintptr_t lnvaddr;
+  uint32_t  ptlevel;
+
+  for (ptlevel = mmu_get_base_pgt_level(), lnvaddr = l1vaddr;
+       ptlevel < MMU_PGT_LEVEL_MAX && lnvaddr != 0;
+       ptlevel++)
+    {
+      lnvaddr = arm64_pgvaddr(mmu_pte_to_paddr(
+                  mmu_ln_getentry(ptlevel, lnvaddr, vaddr)));
+    }
+
+  return lnvaddr;
+}
+
 static int modify_region(uintptr_t vstart, uintptr_t vend, uintptr_t setmask)
 {
   uintptr_t l1vaddr;
   uintptr_t lnvaddr;
   uintptr_t entry;
-  uintptr_t paddr;
   uintptr_t vaddr;
-  uint32_t  ptlevel;
 
   /* Must perform a reverse table walk */
 
@@ -71,29 +85,31 @@ static int modify_region(uintptr_t vstart, uintptr_t vend, uintptr_t setmask)
       return -EINVAL;
     }
 
+  /* Change nothing unless each page is mapped user memory */
+
   for (vaddr = vstart; vaddr < vend; vaddr += MM_PGSIZE)
     {
-      for (ptlevel = mmu_get_base_pgt_level(), lnvaddr = l1vaddr;
-           ptlevel < MMU_PGT_LEVEL_MAX;
-           ptlevel++)
+      lnvaddr = get_pgtable(l1vaddr, vaddr);
+      if (!arm64_uservaddr(vaddr) || lnvaddr == 0 ||
+          (mmu_ln_getentry(MMU_PGT_LEVEL_MAX, lnvaddr, vaddr) &
+           PTE_DESC_TYPE_MASK) != PTE_PAGE_DESC)
         {
-          paddr = mmu_pte_to_paddr(mmu_ln_getentry(ptlevel, lnvaddr, vaddr));
-          lnvaddr = arm64_pgvaddr(paddr);
-          if (!lnvaddr)
-            {
-              return -EINVAL;
-            }
+          return -ENOMEM;
         }
+    }
 
+  for (vaddr = vstart; vaddr < vend; vaddr += MM_PGSIZE)
+    {
       /* Get entry and modify the flags */
 
-      entry  = mmu_ln_getentry(ptlevel, lnvaddr, vaddr);
-      entry &= ~CLR_MASK;
-      entry |= setmask | PTE_BLOCK_DESC_AP_USER;
+      lnvaddr = get_pgtable(l1vaddr, vaddr);
+      entry   = mmu_ln_getentry(MMU_PGT_LEVEL_MAX, lnvaddr, vaddr);
+      entry  &= ~CLR_MASK;
+      entry  |= setmask;
 
       /* Restore the entry */
 
-      mmu_ln_restore(ptlevel, lnvaddr, vaddr, entry);
+      mmu_ln_restore(MMU_PGT_LEVEL_MAX, lnvaddr, vaddr, entry);
     }
 
   return OK;
@@ -129,15 +145,21 @@ int up_addrenv_mprot(arch_addrenv_t *addrenv, uintptr_t addr, size_t len,
 
   UNUSED(addrenv);
 
-  /* The access is either RO or RW, read access cannot be revoked */
+  /* PROT_NONE removes all user access.  Else the access is RO or RW, and
+   * PROT_WRITE or PROT_EXEC also give read access.
+   */
 
-  if ((prot & PROT_WRITE) == 0)
+  if (prot == PROT_NONE)
     {
       setmask = PTE_BLOCK_DESC_AP_RO;
     }
+  else if ((prot & PROT_WRITE) == 0)
+    {
+      setmask = PTE_BLOCK_DESC_AP_RO | PTE_BLOCK_DESC_AP_USER;
+    }
   else
     {
-      setmask = PTE_BLOCK_DESC_AP_RW;
+      setmask = PTE_BLOCK_DESC_AP_RW | PTE_BLOCK_DESC_AP_USER;
     }
 
   /* Execute access is explicitly revoked, implicitly granted */


(nuttx) 01/08: arch/arm64: Fix the style of arm64_cache.c.

Posted by ac...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

acassis pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit a79f9bac76b687691b8b92c9c453b61926170210
Author: Marco Casaroli <ma...@gmail.com>
AuthorDate: Tue Oct 20 14:28:12 2026 +0200

    arch/arm64: Fix the style of arm64_cache.c.
    
    Add the blank line after the declarations that nxstyle asks for.  The
    errors are older than the next change; there is no other change.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <ma...@gmail.com>
---
 arch/arm64/src/common/arm64_cache.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/arch/arm64/src/common/arm64_cache.c b/arch/arm64/src/common/arm64_cache.c
index 71cac0f274e..0ae9ba57b8a 100644
--- a/arch/arm64/src/common/arm64_cache.c
+++ b/arch/arm64/src/common/arm64_cache.c
@@ -443,6 +443,7 @@ void up_invalidate_icache(uintptr_t start, uintptr_t end)
 void up_enable_icache(void)
 {
   uint64_t value = read_sysreg(sctlr_el1);
+
   write_sysreg((value | SCTLR_I_BIT), sctlr_el1);
   UP_ISB();
 }
@@ -464,6 +465,7 @@ void up_enable_icache(void)
 void up_disable_icache(void)
 {
   uint64_t value = read_sysreg(sctlr_el1);
+
   write_sysreg((value & ~SCTLR_I_BIT), sctlr_el1);
   UP_ISB();
 }
@@ -676,6 +678,7 @@ void up_enable_dcache(void)
 void up_disable_dcache(void)
 {
   uint64_t value = read_sysreg(sctlr_el1);
+
   write_sysreg((value & ~SCTLR_C_BIT), sctlr_el1);
   UP_ISB();
 }


(nuttx) 04/08: arch/risc-v: Check the range in up_addrenv_mprot().

Posted by ac...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

acassis pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit 96bd521662d78159d2f0e2f67cf94fb0abf91f11
Author: Marco Casaroli <ma...@gmail.com>
AuthorDate: Mon Oct 19 21:34:40 2026 +0200

    arch/risc-v: Check the range in up_addrenv_mprot().
    
    up_addrenv_mprot() walked the page tables for each page of the range
    and changed the last level entry.  It did not check that the address
    is a user address, and it left the first pages changed when a later
    page had no page table.  PROT_WRITE without PROT_READ set W without R,
    which is a reserved encoding: each access to the page faults.
    
    Now the function first checks each page: a user address with a valid
    page entry.  If one page fails the check, it changes nothing and
    returns -ENOMEM.  With CONFIG_PAGING a page of .text, .data or the
    heap that is not mapped yet passes the check, because the page fault
    handler maps it at its first access.  PROT_WRITE now also sets R.
    
    The ELF loader is the only caller today, and its .text range passes
    the check.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <ma...@gmail.com>
---
 arch/risc-v/src/common/riscv_addrenv_perms.c | 70 +++++++++++++++++++++-------
 1 file changed, 54 insertions(+), 16 deletions(-)

diff --git a/arch/risc-v/src/common/riscv_addrenv_perms.c b/arch/risc-v/src/common/riscv_addrenv_perms.c
index a989a596b43..cea004db7c8 100644
--- a/arch/risc-v/src/common/riscv_addrenv_perms.c
+++ b/arch/risc-v/src/common/riscv_addrenv_perms.c
@@ -50,14 +50,44 @@
  * Private Functions
  ****************************************************************************/
 
+static uintptr_t get_pgtable(uintptr_t l1vaddr, uintptr_t vaddr)
+{
+  uintptr_t lnvaddr;
+  uint32_t  ptlevel;
+
+  for (ptlevel = 1, lnvaddr = l1vaddr;
+       ptlevel < RV_MMU_PT_LEVELS && lnvaddr != 0;
+       ptlevel++)
+    {
+      lnvaddr = riscv_pgvaddr(mmu_pte_to_paddr(
+                  mmu_ln_getentry(ptlevel, lnvaddr, vaddr)));
+    }
+
+  return lnvaddr;
+}
+
+/* With on-demand paging the page fault handler maps a page of .text, .data
+ * or the heap at its first access.
+ */
+
+static bool paged_vaddr(uintptr_t vaddr)
+{
+#ifdef CONFIG_PAGING
+  return (vaddr >= CONFIG_ARCH_TEXT_VBASE && vaddr < ARCH_TEXT_VEND) ||
+         (vaddr >= CONFIG_ARCH_DATA_VBASE && vaddr < ARCH_DATA_VEND) ||
+         (vaddr >= CONFIG_ARCH_HEAP_VBASE && vaddr < ARCH_HEAP_VEND);
+#else
+  UNUSED(vaddr);
+  return false;
+#endif
+}
+
 static int modify_region(uintptr_t vstart, uintptr_t vend, uintptr_t setmask)
 {
   uintptr_t l1vaddr;
   uintptr_t lnvaddr;
   uintptr_t entry;
-  uintptr_t paddr;
   uintptr_t vaddr;
-  uint32_t  ptlevel;
 
   /* Must perform a reverse table walk */
 
@@ -70,29 +100,35 @@ static int modify_region(uintptr_t vstart, uintptr_t vend, uintptr_t setmask)
       return -EINVAL;
     }
 
+  /* Change nothing unless each page is mapped user memory */
+
   for (vaddr = vstart; vaddr < vend; vaddr += MM_PGSIZE)
     {
-      for (ptlevel = 1, lnvaddr = l1vaddr;
-           ptlevel < RV_MMU_PT_LEVELS;
-           ptlevel++)
+      lnvaddr = get_pgtable(l1vaddr, vaddr);
+      if (!riscv_uservaddr(vaddr) || lnvaddr == 0)
         {
-          paddr = mmu_pte_to_paddr(mmu_ln_getentry(ptlevel, lnvaddr, vaddr));
-          lnvaddr = riscv_pgvaddr(paddr);
-          if (!lnvaddr)
-            {
-              return -EINVAL;
-            }
+          return -ENOMEM;
         }
 
+      if ((mmu_ln_getentry(RV_MMU_PT_LEVELS, lnvaddr, vaddr) &
+           PTE_VALID) == 0 && !paged_vaddr(vaddr))
+        {
+          return -ENOMEM;
+        }
+    }
+
+  for (vaddr = vstart; vaddr < vend; vaddr += MM_PGSIZE)
+    {
       /* Get entry and modify the flags */
 
-      entry  = mmu_ln_getentry(ptlevel, lnvaddr, vaddr);
-      entry &= ~CLR_MASK;
-      entry |= setmask;
+      lnvaddr = get_pgtable(l1vaddr, vaddr);
+      entry   = mmu_ln_getentry(RV_MMU_PT_LEVELS, lnvaddr, vaddr);
+      entry  &= ~CLR_MASK;
+      entry  |= setmask;
 
       /* Restore the entry */
 
-      mmu_ln_restore(ptlevel, lnvaddr, vaddr, entry);
+      mmu_ln_restore(RV_MMU_PT_LEVELS, lnvaddr, vaddr, entry);
     }
 
   /* When all is set and done, flush the data caches */
@@ -142,9 +178,11 @@ int up_addrenv_mprot(arch_addrenv_t *addrenv, uintptr_t addr, size_t len,
       setmask |= PTE_R;
     }
 
+  /* A writable page must be readable: W without R is reserved */
+
   if (prot & PROT_WRITE)
     {
-      setmask |= PTE_W;
+      setmask |= PTE_R | PTE_W;
     }
 
   if (prot & PROT_EXEC)


(nuttx) 02/08: arch/arm64: Wait for the instruction cache invalidation.

Posted by ac...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

acassis pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit 0e9bb1915e765a734fb4771cd6e16155e908875e
Author: Marco Casaroli <ma...@gmail.com>
AuthorDate: Tue Oct 20 14:25:31 2026 +0200

    arch/arm64: Wait for the instruction cache invalidation.
    
    up_invalidate_icache_all() ran IC IALLUIS, and up_invalidate_icache()
    ran IC IVAU for each line, without a DSB after it.  The architecture
    completes a cache maintenance instruction only at the next DSB, so the
    CPU could still fetch old instructions after the call.  For example a
    program that wrote new code into a page and made it executable with
    mprotect() sometimes ran the old code.
    
    Add DSB and ISB after the invalidation.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <ma...@gmail.com>
---
 arch/arm64/src/common/arm64_cache.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/arch/arm64/src/common/arm64_cache.c b/arch/arm64/src/common/arm64_cache.c
index 0ae9ba57b8a..25a456e945c 100644
--- a/arch/arm64/src/common/arm64_cache.c
+++ b/arch/arm64/src/common/arm64_cache.c
@@ -389,6 +389,11 @@ size_t up_get_icache_size(void)
 void up_invalidate_icache_all(void)
 {
   __ic_ialluis();
+
+  /* Wait until the invalidation is done before the next fetch */
+
+  UP_DSB();
+  UP_ISB();
 }
 
 /****************************************************************************
@@ -423,6 +428,7 @@ void up_invalidate_icache(uintptr_t start, uintptr_t end)
       start += line_size;
     }
 
+  UP_DSB();
   UP_ISB();
 }
 


(nuttx) 06/08: fs/mmap: Implement mprotect().

Posted by ac...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

acassis pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit 07c4a9165d60a549079b9a5d9df39cb80eb1527a
Author: Marco Casaroli <ma...@gmail.com>
AuthorDate: Mon Oct 19 21:34:40 2026 +0200

    fs/mmap: Implement mprotect().
    
    mprotect() returned 0 and changed nothing, also in kernel builds where
    the MMU can change the page permissions.  A program that made a page
    read-only could still write it, and a program could not make a page
    executable.  Bad arguments were not reported either.
    
    In all builds, mprotect() now returns EINVAL when addr is not a
    multiple of the page size that sysconf(_SC_PAGESIZE) reports, or when
    prot has a bit other than PROT_READ, PROT_WRITE and PROT_EXEC.  It
    returns 0 when len is 0.
    
    With CONFIG_ARCH_ADDRENV, mprotect() then calls up_addrenv_mprot() for
    the address environment of the caller, which returns ENOMEM when a
    page of the range is not mapped user memory.  The mapping list lock is
    held during the change, so that munmap() of another thread cannot
    remove the pages meanwhile.  When prot has PROT_EXEC, mprotect() also
    cleans the data cache and invalidates the instruction cache of the
    range, so that code written to the pages runs without a cache
    maintenance call in the program.
    
    PROT_WRITE | PROT_EXEC is allowed.  A W^X policy can come later as an
    option.
    
    Without CONFIG_ARCH_ADDRENV a valid request still returns 0 and
    changes nothing: without an MMU the permissions cannot be enforced.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <ma...@gmail.com>
---
 fs/mmap/fs_mmisc.c | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 67 insertions(+), 2 deletions(-)

diff --git a/fs/mmap/fs_mmisc.c b/fs/mmap/fs_mmisc.c
index 658e8cae5a5..b77858315b6 100644
--- a/fs/mmap/fs_mmisc.c
+++ b/fs/mmap/fs_mmisc.c
@@ -24,7 +24,18 @@
  * Included Files
  ****************************************************************************/
 
+#include <nuttx/config.h>
+
 #include <sys/mman.h>
+#include <errno.h>
+#include <unistd.h>
+
+#include <nuttx/addrenv.h>
+#include <nuttx/arch.h>
+#include <nuttx/cache.h>
+#include <nuttx/mm/map.h>
+#include <nuttx/pgalloc.h>
+#include <nuttx/sched.h>
 
 /****************************************************************************
  * Public Functions
@@ -52,11 +63,65 @@ int munlockall(void)
   return 0;
 }
 
-/* The empty implementation is enough since MMU/MPU mayn't exist. */
+/* Without an address environment the arguments are checked, but there are
+ * no page permissions to change.
+ */
 
 int mprotect(FAR void *addr, size_t len, int prot)
 {
-  return 0;
+#if defined(CONFIG_ARCH_ADDRENV) && defined(CONFIG_MM_PGALLOC)
+  FAR struct addrenv_s *addrenv = nxsched_self()->addrenv_own;
+#endif
+  uintptr_t start = (uintptr_t)addr;
+  long pagesize = sysconf(_SC_PAGESIZE);
+  int ret;
+
+  if ((prot & ~(PROT_READ | PROT_WRITE | PROT_EXEC)) != 0 ||
+      (pagesize > 0 && start % pagesize != 0))
+    {
+      ret = -EINVAL;
+      goto errout;
+    }
+
+  if (len == 0)
+    {
+      return OK;
+    }
+
+#if defined(CONFIG_ARCH_ADDRENV) && defined(CONFIG_MM_PGALLOC)
+  len = MM_PGALIGNUP(len);
+  if (len == 0 || start + len < start || addrenv == NULL)
+    {
+      ret = -ENOMEM;
+      goto errout;
+    }
+
+  /* Hold the mapping list so that munmap() cannot remove pages meanwhile */
+
+  ret = mm_map_lock();
+  if (ret < 0)
+    {
+      goto errout;
+    }
+
+  ret = up_addrenv_mprot(&addrenv->addrenv, start, len, prot);
+  mm_map_unlock();
+  if (ret < 0)
+    {
+      goto errout;
+    }
+
+  if ((prot & PROT_EXEC) != 0)
+    {
+      up_coherent_dcache(start, len);
+    }
+#endif
+
+  return OK;
+
+errout:
+  set_errno(-ret);
+  return ERROR;
 }
 
 /* Ignore the advice since there is no alternative strategy to select. */