🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: muc.lists.freebsd.bugs
2 messages
2 total messages Started by bugzilla-noreply Fri, 27 Nov 2026 08:48
[Bug 301179] arm64 kexec: kexec_load_md() builds a malformed and incomplete identity map; the kexec jump faults in kexec_reboot_...
#779
Author: bugzilla-noreply
Date: Fri, 27 Nov 2026 08:48
201 lines
8772 bytes
https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=301179

            Bug ID: 301179
           Summary: arm64 kexec: kexec_load_md() builds a malformed and
                    incomplete identity map; the kexec jump faults in
                    kexec_reboot_bottom()
           Product: Base System
           Version: 16.0-CURRENT
          Hardware: arm64
                OS: Any
            Status: New
          Severity: Affects Only Me
          Priority: ---
         Component: kern
          Assignee: bugs@FreeBSD.org
          Reporter: dmilith@me.com

Affects: sys/arm64/arm64/kexec_support.c (unchanged in main up to f129398f6454,
2026-11-26)

Seen on: FreeBSD 16.0-CURRENT main-n291235-611046b15dbb (arm64 GENERIC,
snapshot 20261117),
in a VM on Apple silicon (Hypervisor.framework, QEMU "virt"-like machine,
GICv3, U-Boot ->
loader.efi, 2 vCPUs, 2 GiB).

Any image reproduces it: the fault happens before the staged image is entered.

  1. kexec_load(2) any image (one segment holding "b ." is enough).
  2. reboot(RB_AUTOBOOT | RB_KEXEC)

The whole shutdown runs, "Starting kexec reboot" is printed, then the CPU takes
an instruction
abort at the physical address of kexec_reboot_bottom() and the machine hangs.

Cause
-----
kexec_reboot_md() switches TTBR0 to locore's bootstrap tables
(pagetable_l0_ttbr0_bootstrap,
one L1 entry, one L2 table) and enters kexec_reboot_bottom() at its physical
address.
kexec_reboot_bottom() runs from that identity map with the MMU on until it
writes SCTLR_EL1.
kexec_load_md() adds three L2 block entries for it:

    tmp = pmap_kextract((vm_offset_t)kexec_reboot_bottom);
    pte[pmap_l2_index(tmp)] = (tmp | L2_BLOCK | ATTR_AF | ATTR_S1_UXN);

These entries have two problems, and the identity map is also incomplete.

1. The output address is the symbol's exact physical address, not its 2 MiB
block
   (tmp & ~L2_OFFSET). The symbol's offset within the block therefore lands in
descriptor
   bits [20:2]:
   - bits [20:12]: RES0, nT, and OA[51:48] with FEAT_LPA;
   - bits [11:2]: nG, AF, SH, AP, NS and AttrIndx.
   With the kernel at PA 0xb5600000, kexec_reboot_bottom is at PA 0xb5f49390
and gets
       0x00400000b5f49791
   That is AttrIndx 4 (VM_MEMATTR_DEVICE_nGnRE), AP 0b10 (read-only), SH 3 and
nG 0, with
   0x149 in bits [20:12]. locore's entry for that block would be
       0x00440000b5e00f09
   (ATTR_S1_nG | ATTR_S1_IDX(VM_MEMATTR_WRITE_BACK) | pmap_sh_attr |
pmap_gp_attr | AF | UXN).

2. No memory attribute index, no nG and no shareability are set, unlike the
entries
   build_l2_block_pagetable writes into the same table.

3. Three blocks are not enough. Before the MMU goes off, kexec_reboot_bottom()
calls
   pmap_kextract() and, through VM_RADIX_FORALL, pctrie_iter_lookup() and
pctrie_iter_next().
   These are PC-relative "bl" calls, so they run from the identity map too. In
the snapshot
   kernel:
       kexec_reboot_bottom   KERNBASE + 0x949390
       pmap_kextract         KERNBASE + 0x951320   (same 2 MiB block)
       pctrie_iter_lookup    KERNBASE + 0x59bc6c   (a block none of the three
entries maps)
       pctrie_iter_next      KERNBASE + 0x59c33c
   Its stack (initstack_end) and the globals it reads with adrp (vm_page_array,
and what the
   callees read, e.g. dmap_phys_base) must be identity-mapped as well.

Fix
---
Everything kexec_reboot_bottom() touches with the MMU on lies in the kernel
image. Give every
2 MiB block of [_start, _end) the entry locore builds for _start's block. The
bootstrap L1 table
has one entry, so the image must lie in _start's 1 GiB. Patch attached
(1-kexec_support.c.diff). It is not compile-tested. A tested equivalent runs
from userland:
after kexec_load(2) we write exactly these entries through /dev/kmem, using
kldsym(2) for
_start, _end, pagetable_l0_ttbr0_bootstrap, pmap_sh_attr and pmap_gp_attr. With
that, kexec
reboots complete on the same VM: ZFS root, both CPUs, multi-user, and a second
kexec from the
kexec'd system.


Patch:

--- a/sys/arm64/arm64/kexec_support.c
+++ b/sys/arm64/arm64/kexec_support.c
@@ -56,6 +56,8 @@

 extern pt_entry_t pagetable_l0_ttbr0_bootstrap[];
 extern unsigned long initstack_end[];
+extern pt_entry_t pmap_gp_attr;
+extern char _start[], _end[];
 void switch_stack(void *, void (*)(void *, void *, struct kexec_image *), void
*);

 #define        SCTLR_EL1_NO_MMU        (SCTLR_RES1 | SCTLR_LSMAOE |
SCTLR_nTLSMD | \
@@ -160,8 +162,8 @@
 int
 kexec_load_md(struct kexec_image *image)
 {
-       vm_paddr_t tmp;
-       pt_entry_t *pte;
+       vm_paddr_t pa, last;
+       pt_entry_t *pte, attr;

        /* Create L2 page blocks for the trampoline. L0/L1 are from the
startup. */

@@ -173,16 +175,23 @@
        pte -= (Ln_ENTRIES * 2);        /* move to start of L2 pages */

        /*
-        * Populate the identity map with symbols we know we'll need before we
-        * turn off the MMU.
+        * kexec_reboot_bottom() runs from the identity map with the MMU on
until
+        * it disables it: its code, its stack (initstack), the functions it
calls
+        * (pmap_kextract(), pctrie_iter_lookup(), pctrie_iter_next()) and the
+        * globals it reads (vm_page_array) must all be mapped.  They all lie
in
+        * the kernel image, so identity-map all of it, with the entries
locore's
+        * build_l2_block_pagetable writes for _start's block.  The bootstrap
L1
+        * table has a single entry: the image must lie in _start's 1 GiB.
         */
-       tmp = pmap_kextract((vm_offset_t)kexec_reboot_bottom);
-       pte[pmap_l2_index(tmp)] = (tmp | L2_BLOCK | ATTR_AF | ATTR_S1_UXN);
-       tmp = pmap_kextract((vm_offset_t)initstack_end);
-       pte[pmap_l2_index(tmp)] = (tmp | L2_BLOCK | ATTR_AF | ATTR_S1_UXN);
-       /* We'll need vm_page_array for doing offset calculations. */
-       tmp = pmap_kextract((vm_offset_t)&vm_page_array);
-       pte[pmap_l2_index(tmp)] = (tmp | L2_BLOCK | ATTR_AF | ATTR_S1_UXN);
+       attr = L2_BLOCK | ATTR_AF | ATTR_S1_UXN | ATTR_S1_nG |
+           ATTR_S1_IDX(VM_MEMATTR_WRITE_BACK) | pmap_sh_attr | pmap_gp_attr;
+       pa = pmap_kextract((vm_offset_t)_start) & ~L2_OFFSET;
+       last = pmap_kextract((vm_offset_t)_end - 1) & ~L2_OFFSET;
+       if ((pa >> L1_SHIFT) != (last >> L1_SHIFT))
+               return (EINVAL);
+       for (; pa <= last; pa += L2_SIZE)
+               pte[pmap_l2_index(pa)] = PHYS_TO_PTE(pa) | attr;
+       dsb(ishst);

        return (0);
 }

-- 
You are receiving this mail because:
You are the assignee for the bug.

--
Posted automagically by a mail2news gateway at muc.de e.V.
Please direct questions, flames, donations, etc. to news-admin@muc.de
[Bug 301179] arm64 kexec: kexec_load_md() builds a malformed and incomplete identity map; the kexec jump faults in kexec_reboot_...
#782
Author: bugzilla-noreply
Date: Fri, 27 Nov 2026 09:03
16 lines
2561 bytes
https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=301179

Mark Linimon <linimon@FreeBSD.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |crash

-- 
You are receiving this mail because:
You are the assignee for the bug.

--
Posted automagically by a mail2news gateway at muc.de e.V.
Please direct questions, flames, donations, etc. to news-admin@muc.de
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads