git: cf0cced7eae3 - stable/14 - powerpc64: give processes a 48-bit address space by default

From: Piotr Kubaj <pkubaj_at_FreeBSD.org>
Date: Thu, 26 Nov 2026 06:16:20 UTC
The branch stable/14 has been updated by pkubaj:

URL: https://cgit.FreeBSD.org/src/commit/?id=cf0cced7eae38e3947160ac496141aca05548721

commit cf0cced7eae38e3947160ac496141aca05548721
Author:     Piotr Kubaj <pkubaj@FreeBSD.org>
AuthorDate: 2026-11-15 20:57:41 +0000
Commit:     Piotr Kubaj <pkubaj@FreeBSD.org>
CommitDate: 2026-11-25 20:35:18 +0000

    powerpc64: give processes a 48-bit address space by default
    
    Since ASLR got enabled for ELFv2, the random bases for the heap, the
    dynamic linker and PIE binaries can land anywhere in the 52-bit address
    space radix gives us. A lot of userland doesn't like that: SpiderMonkey,
    JavaScriptCore, QV4 and friends pack pointers into 47 or 48 bits and
    crash on startup (gjs, cjs, qml, ...).
    
    Do what amd64 does on LA57 hardware: keep the full range in the kernel
    and give processes a smaller map by default through a second set of
    sysvecs. The default is 48 bits (256 TiB), the same as arm64.
    
    One caveat for 48 bits: SpiderMonkey's GC allocator has no fallback
    there and relies on mmap(2) hints staying 1 MiB-aligned, which ASLR on
    FreeBSD breaks (hinted mappings move in 4 KiB steps, 64 KiB on arm64).
    The versions in ports need Mozilla's bug 1876632 (MAP_ALIGNED on
    FreeBSD, in Firefox 143) to be reliable; with it gjs runs clean.
    
    The full 52 bits stay available per process through the la52 feature
    control note (elfctl(1)) and PROC_LA_CTL (proccontrol(1)), and
    vm.pmap.prefer_uva_la48 flips the default. la52 is the powerpc64 name
    for the note bit amd64 calls la57, the procctl(2) values are the same.
    
    Fixes:          30ed27ff2556 ("sys/powerpc/powerpc/elf64_machdep.c: enable ASLR on ELFv2")
    Reviewed by:    kib
    MFC after:      3 days
    Differential Revision:  https://reviews.freebsd.org/D61331
    
    (cherry picked from commit 5f2956673c280fa3de5615dfeecdc6c92e62d307)
---
 sys/powerpc/include/proc.h          |   8 +-
 sys/powerpc/include/procctl.h       |  21 ++++
 sys/powerpc/include/vmparam.h       |  25 +++-
 sys/powerpc/powerpc/elf64_machdep.c | 225 +++++++++++++++++++++++++++++++-----
 sys/powerpc/powerpc/exec_machdep.c  |   6 +-
 sys/powerpc/powerpc/vm_machdep.c    |  88 +++++++++++++-
 sys/sys/elf_common.h                |   3 +-
 7 files changed, 336 insertions(+), 40 deletions(-)

diff --git a/sys/powerpc/include/proc.h b/sys/powerpc/include/proc.h
index c8c9b6370711..a386c7181a1c 100644
--- a/sys/powerpc/include/proc.h
+++ b/sys/powerpc/include/proc.h
@@ -45,12 +45,12 @@ struct mdthread {
 };
 
 struct mdproc {
-	/*
-	 * Avoid empty structs because they are undefined behavior.
-	 */
-	long	md_spare;
+	u_int	md_flags;		/* (c) md process flags P_MD */
 };
 
+#define	P_MD_LA48		0x00000001	/* Request LA48 after exec */
+#define	P_MD_LA52		0x00000002	/* Request LA52 after exec */
+
 #ifdef __powerpc64__
 #define	KINFO_PROC_SIZE 1088
 #define	KINFO_PROC32_SIZE 816
diff --git a/sys/powerpc/include/procctl.h b/sys/powerpc/include/procctl.h
index b340002b45ee..1e8756b24ee3 100644
--- a/sys/powerpc/include/procctl.h
+++ b/sys/powerpc/include/procctl.h
@@ -1,3 +1,24 @@
 /*-
  * This file is in the public domain.
  */
+
+#ifndef _MACHINE_PROCCTL_H_
+#define	_MACHINE_PROCCTL_H_
+
+/*
+ * Per-process user address space size, see vmparam.h: la48 is the default
+ * 48-bit (256 TiB) address space, la52 the full 52 bits of the radix MMU.
+ * The values match amd64's PROC_LA_CTL so that proccontrol(1) can share
+ * the code.
+ */
+#define	PROC_LA_CTL		(PROC_PROCCTL_MD_MIN + 2)
+#define	PROC_LA_STATUS		(PROC_PROCCTL_MD_MIN + 3)
+
+#define	PROC_LA_CTL_LA48_ON_EXEC	1
+#define	PROC_LA_CTL_LA52_ON_EXEC	2
+#define	PROC_LA_CTL_DEFAULT_ON_EXEC	3
+
+#define	PROC_LA_STATUS_LA48		0x01000000
+#define	PROC_LA_STATUS_LA52		0x02000000
+
+#endif /* !_MACHINE_PROCCTL_H_ */
diff --git a/sys/powerpc/include/vmparam.h b/sys/powerpc/include/vmparam.h
index 89982a618bc7..447b3b2c9c92 100644
--- a/sys/powerpc/include/vmparam.h
+++ b/sys/powerpc/include/vmparam.h
@@ -87,24 +87,43 @@
  * kernel map should be able to start at 0xc008000000000000 -
  * but at least the functional simulator doesn't like it
  *
- * 0x0000000000000000 - 0x000fffffffffffff   user map
+ * 0x0000000000000000 - 0x00007fffffffffff   user map (default)
+ * 0x0000000000000000 - 0x000fffffffffffff   user map (la52)
  * 0xc000000000000000 - 0xc007ffffffffffff   direct map
  * 0xc008000000000000 - 0xc00fffffffffffff   kernel map
  *
  */
 #define	VM_MIN_ADDRESS		0x0000000000000000
-#define	VM_MAXUSER_ADDRESS	0x000fffffc0000000
+/*
+ * Radix lets user space address 52 bits, but a process only gets the low
+ * 48 bits (256 TiB) by default, like on arm64: lots of userland
+ * (SpiderMonkey, JavaScriptCore, QV4, ...) packs pointers into 48 bits and
+ * crashes once ASLR puts the heap above that.  The full 52 bits are
+ * available per process through the la52 feature control note or
+ * PROC_LA_CTL, see elf64_machdep.c.
+ */
+#define	VM_MAXUSER_ADDRESS_LA48	0x0001000000000000
+#define	VM_MAXUSER_ADDRESS_LA52	0x000fffffc0000000
+#define	VM_MAXUSER_ADDRESS	VM_MAXUSER_ADDRESS_LA52
 #define	VM_MAX_ADDRESS		0xc00fffffffffffff
 #define	VM_MIN_KERNEL_ADDRESS	0xc008000000000000
 #define	VM_MAX_KERNEL_ADDRESS	0xc0080007ffffffff
 #define	VM_MAX_SAFE_KERNEL_ADDRESS	VM_MAX_KERNEL_ADDRESS
+
+#define	SHAREDPAGE_LA48		(VM_MAXUSER_ADDRESS_LA48 - PAGE_SIZE)
+#define	SHAREDPAGE_LA52		(VM_MAXUSER_ADDRESS_LA52 - PAGE_SIZE)
+#define	SHAREDPAGE		SHAREDPAGE_LA48
+#define	USRSTACK_LA48		SHAREDPAGE_LA48
+#define	USRSTACK_LA52		SHAREDPAGE_LA52
+#define	PS_STRINGS_LA48		(USRSTACK_LA48 - sizeof(struct ps_strings))
+#define	PS_STRINGS_LA52		(USRSTACK_LA52 - sizeof(struct ps_strings))
 #else
 #define	VM_MIN_ADDRESS		0
 #define	VM_MAXUSER_ADDRESS	VM_MAXUSER_ADDRESS32
 #define	VM_MAX_ADDRESS		0xffffffff
-#endif
 
 #define	SHAREDPAGE		(VM_MAXUSER_ADDRESS - PAGE_SIZE)
+#endif
 
 #define	FREEBSD32_SHAREDPAGE	(VM_MAXUSER_ADDRESS32 - PAGE_SIZE)
 #define	FREEBSD32_USRSTACK	FREEBSD32_SHAREDPAGE
diff --git a/sys/powerpc/powerpc/elf64_machdep.c b/sys/powerpc/powerpc/elf64_machdep.c
index 754104e2fe60..fefcd08eb2ed 100644
--- a/sys/powerpc/powerpc/elf64_machdep.c
+++ b/sys/powerpc/powerpc/elf64_machdep.c
@@ -41,6 +41,7 @@
 #include <sys/jail.h>
 #include <sys/smp.h>
 #include <sys/syscall.h>
+#include <sys/sysctl.h>
 #include <sys/signalvar.h>
 #include <sys/vnode.h>
 #include <sys/linker.h>
@@ -61,7 +62,7 @@
 static void exec_setregs_funcdesc(struct thread *td, struct image_params *imgp,
     uintptr_t stack);
 
-struct sysentvec elf64_freebsd_sysvec_v1 = {
+struct sysentvec elf64_freebsd_sysvec_v1_la48 = {
 	.sv_size	= SYS_MAXSYSCALL,
 	.sv_table	= sysent,
 	.sv_fixup	= __elfN(freebsd_fixup),
@@ -75,9 +76,9 @@ struct sysentvec elf64_freebsd_sysvec_v1 = {
 	.sv_elf_core_prepare_notes = __elfN(prepare_notes),
 	.sv_minsigstksz	= MINSIGSTKSZ,
 	.sv_minuser	= VM_MIN_ADDRESS,
-	.sv_maxuser	= VM_MAXUSER_ADDRESS,
-	.sv_usrstack	= USRSTACK,
-	.sv_psstrings	= PS_STRINGS,
+	.sv_maxuser	= VM_MAXUSER_ADDRESS_LA48,
+	.sv_usrstack	= USRSTACK_LA48,
+	.sv_psstrings	= PS_STRINGS_LA48,
 	.sv_psstringssz	= sizeof(struct ps_strings),
 	.sv_stackprot	= VM_PROT_ALL,
 	.sv_copyout_auxargs = __elfN(powerpc_copyout_auxargs),
@@ -90,7 +91,7 @@ struct sysentvec elf64_freebsd_sysvec_v1 = {
 	.sv_set_syscall_retval = cpu_set_syscall_retval,
 	.sv_fetch_syscall_args = cpu_fetch_syscall_args,
 	.sv_syscallnames = syscallnames,
-	.sv_shared_page_base = SHAREDPAGE,
+	.sv_shared_page_base = SHAREDPAGE_LA48,
 	.sv_shared_page_len = PAGE_SIZE,
 	.sv_schedtail	= NULL,
 	.sv_thread_detach = NULL,
@@ -103,7 +104,91 @@ struct sysentvec elf64_freebsd_sysvec_v1 = {
 	.sv_regset_end  = SET_LIMIT(__elfN(regset)),
 };
 
-struct sysentvec elf64_freebsd_sysvec_v2 = {
+struct sysentvec elf64_freebsd_sysvec_v1_la52 = {
+	.sv_size	= SYS_MAXSYSCALL,
+	.sv_table	= sysent,
+	.sv_fixup	= __elfN(freebsd_fixup),
+	.sv_sendsig	= sendsig,
+	.sv_sigcode	= sigcode64,
+	.sv_szsigcode	= &szsigcode64,
+	.sv_name	= "FreeBSD ELF64",
+	.sv_coredump	= __elfN(coredump),
+	.sv_elf_core_osabi = ELFOSABI_FREEBSD,
+	.sv_elf_core_abi_vendor = FREEBSD_ABI_VENDOR,
+	.sv_elf_core_prepare_notes = __elfN(prepare_notes),
+	.sv_minsigstksz	= MINSIGSTKSZ,
+	.sv_minuser	= VM_MIN_ADDRESS,
+	.sv_maxuser	= VM_MAXUSER_ADDRESS_LA52,
+	.sv_usrstack	= USRSTACK_LA52,
+	.sv_psstrings	= PS_STRINGS_LA52,
+	.sv_psstringssz	= sizeof(struct ps_strings),
+	.sv_stackprot	= VM_PROT_ALL,
+	.sv_copyout_auxargs = __elfN(powerpc_copyout_auxargs),
+	.sv_copyout_strings = exec_copyout_strings,
+	.sv_setregs	= exec_setregs_funcdesc,
+	.sv_fixlimit	= NULL,
+	.sv_maxssiz	= NULL,
+	.sv_flags	= SV_ABI_FREEBSD | SV_LP64 | SV_SHP | SV_ASLR |
+			    SV_TIMEKEEP | SV_RNG_SEED_VER | SV_SIGSYS,
+	.sv_set_syscall_retval = cpu_set_syscall_retval,
+	.sv_fetch_syscall_args = cpu_fetch_syscall_args,
+	.sv_syscallnames = syscallnames,
+	.sv_shared_page_base = SHAREDPAGE_LA52,
+	.sv_shared_page_len = PAGE_SIZE,
+	.sv_schedtail	= NULL,
+	.sv_thread_detach = NULL,
+	.sv_trap	= NULL,
+	.sv_hwcap	= &cpu_features,
+	.sv_hwcap2	= &cpu_features2,
+	.sv_onexec_old	= exec_onexec_old,
+	.sv_onexit	= exit_onexit,
+	.sv_regset_begin = SET_BEGIN(__elfN(regset)),
+	.sv_regset_end  = SET_LIMIT(__elfN(regset)),
+};
+
+struct sysentvec elf64_freebsd_sysvec_v2_la48 = {
+	.sv_size	= SYS_MAXSYSCALL,
+	.sv_table	= sysent,
+	.sv_fixup	= __elfN(freebsd_fixup),
+	.sv_sendsig	= sendsig,
+	.sv_sigcode	= sigcode64, /* Fixed up in ppc64_init_sysvecs(). */
+	.sv_szsigcode	= &szsigcode64,
+	.sv_name	= "FreeBSD ELF64 V2",
+	.sv_coredump	= __elfN(coredump),
+	.sv_elf_core_osabi = ELFOSABI_FREEBSD,
+	.sv_elf_core_abi_vendor = FREEBSD_ABI_VENDOR,
+	.sv_elf_core_prepare_notes = __elfN(prepare_notes),
+	.sv_minsigstksz	= MINSIGSTKSZ,
+	.sv_minuser	= VM_MIN_ADDRESS,
+	.sv_maxuser	= VM_MAXUSER_ADDRESS_LA48,
+	.sv_usrstack	= USRSTACK_LA48,
+	.sv_psstrings	= PS_STRINGS_LA48,
+	.sv_psstringssz	= sizeof(struct ps_strings),
+	.sv_stackprot	= VM_PROT_ALL,
+	.sv_copyout_auxargs = __elfN(powerpc_copyout_auxargs),
+	.sv_copyout_strings = exec_copyout_strings,
+	.sv_setregs	= exec_setregs,
+	.sv_fixlimit	= NULL,
+	.sv_maxssiz	= NULL,
+	.sv_flags	= SV_ABI_FREEBSD | SV_LP64 | SV_SHP | SV_ASLR |
+			    SV_TIMEKEEP | SV_RNG_SEED_VER | SV_SIGSYS,
+	.sv_set_syscall_retval = cpu_set_syscall_retval,
+	.sv_fetch_syscall_args = cpu_fetch_syscall_args,
+	.sv_syscallnames = syscallnames,
+	.sv_shared_page_base = SHAREDPAGE_LA48,
+	.sv_shared_page_len = PAGE_SIZE,
+	.sv_schedtail	= NULL,
+	.sv_thread_detach = NULL,
+	.sv_trap	= NULL,
+	.sv_hwcap	= &cpu_features,
+	.sv_hwcap2	= &cpu_features2,
+	.sv_onexec_old	= exec_onexec_old,
+	.sv_onexit	= exit_onexit,
+	.sv_regset_begin = SET_BEGIN(__elfN(regset)),
+	.sv_regset_end  = SET_LIMIT(__elfN(regset)),
+};
+
+struct sysentvec elf64_freebsd_sysvec_v2_la52 = {
 	.sv_size	= SYS_MAXSYSCALL,
 	.sv_table	= sysent,
 	.sv_fixup	= __elfN(freebsd_fixup),
@@ -117,9 +202,9 @@ struct sysentvec elf64_freebsd_sysvec_v2 = {
 	.sv_elf_core_prepare_notes = __elfN(prepare_notes),
 	.sv_minsigstksz	= MINSIGSTKSZ,
 	.sv_minuser	= VM_MIN_ADDRESS,
-	.sv_maxuser	= VM_MAXUSER_ADDRESS,
-	.sv_usrstack	= USRSTACK,
-	.sv_psstrings	= PS_STRINGS,
+	.sv_maxuser	= VM_MAXUSER_ADDRESS_LA52,
+	.sv_usrstack	= USRSTACK_LA52,
+	.sv_psstrings	= PS_STRINGS_LA52,
 	.sv_psstringssz	= sizeof(struct ps_strings),
 	.sv_stackprot	= VM_PROT_ALL,
 	.sv_copyout_auxargs = __elfN(powerpc_copyout_auxargs),
@@ -132,7 +217,7 @@ struct sysentvec elf64_freebsd_sysvec_v2 = {
 	.sv_set_syscall_retval = cpu_set_syscall_retval,
 	.sv_fetch_syscall_args = cpu_fetch_syscall_args,
 	.sv_syscallnames = syscallnames,
-	.sv_shared_page_base = SHAREDPAGE,
+	.sv_shared_page_base = SHAREDPAGE_LA52,
 	.sv_shared_page_len = PAGE_SIZE,
 	.sv_schedtail	= NULL,
 	.sv_thread_detach = NULL,
@@ -147,47 +232,82 @@ struct sysentvec elf64_freebsd_sysvec_v2 = {
 
 static bool ppc64_elfv1_header_match(struct image_params *params,
     int32_t *, uint32_t *);
+static bool ppc64_elfv1_la52_header_match(struct image_params *params,
+    int32_t *, uint32_t *);
 static bool ppc64_elfv2_header_match(struct image_params *params,
     int32_t *, uint32_t *);
+static bool ppc64_elfv2_la52_header_match(struct image_params *params,
+    int32_t *, uint32_t *);
 
-static Elf64_Brandinfo freebsd_brand_info_elfv1 = {
+static Elf64_Brandinfo freebsd_brand_info_elfv1_la48 = {
 	.brand		= ELFOSABI_FREEBSD,
 	.machine	= EM_PPC64,
 	.compat_3_brand	= "FreeBSD",
 	.interp_path	= "/libexec/ld-elf.so.1",
-	.sysvec		= &elf64_freebsd_sysvec_v1,
+	.sysvec		= &elf64_freebsd_sysvec_v1_la48,
 	.interp_newpath	= NULL,
 	.brand_note	= &elf64_freebsd_brandnote,
 	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE,
 	.header_supported = &ppc64_elfv1_header_match
 };
 
-SYSINIT(elf64v1, SI_SUB_EXEC, SI_ORDER_ANY,
-    (sysinit_cfunc_t) elf64_insert_brand_entry,
-    &freebsd_brand_info_elfv1);
+static Elf64_Brandinfo freebsd_brand_info_elfv1_la52 = {
+	.brand		= ELFOSABI_FREEBSD,
+	.machine	= EM_PPC64,
+	.compat_3_brand	= "FreeBSD",
+	.interp_path	= "/libexec/ld-elf.so.1",
+	.sysvec		= &elf64_freebsd_sysvec_v1_la52,
+	.interp_newpath	= NULL,
+	.brand_note	= &elf64_freebsd_brandnote,
+	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE,
+	.header_supported = &ppc64_elfv1_la52_header_match
+};
 
-static Elf64_Brandinfo freebsd_brand_info_elfv2 = {
+static Elf64_Brandinfo freebsd_brand_info_elfv2_la48 = {
 	.brand		= ELFOSABI_FREEBSD,
 	.machine	= EM_PPC64,
 	.compat_3_brand	= "FreeBSD",
 	.interp_path	= "/libexec/ld-elf.so.1",
-	.sysvec		= &elf64_freebsd_sysvec_v2,
+	.sysvec		= &elf64_freebsd_sysvec_v2_la48,
 	.interp_newpath	= NULL,
 	.brand_note	= &elf64_freebsd_brandnote,
 	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE,
 	.header_supported = &ppc64_elfv2_header_match
 };
 
-SYSINIT(elf64v2, SI_SUB_EXEC, SI_ORDER_ANY,
-    (sysinit_cfunc_t) elf64_insert_brand_entry,
-    &freebsd_brand_info_elfv2);
+static Elf64_Brandinfo freebsd_brand_info_elfv2_la52 = {
+	.brand		= ELFOSABI_FREEBSD,
+	.machine	= EM_PPC64,
+	.compat_3_brand	= "FreeBSD",
+	.interp_path	= "/libexec/ld-elf.so.1",
+	.sysvec		= &elf64_freebsd_sysvec_v2_la52,
+	.interp_newpath	= NULL,
+	.brand_note	= &elf64_freebsd_brandnote,
+	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE,
+	.header_supported = &ppc64_elfv2_la52_header_match
+};
+
+static void
+ppc64_register_brand_entries(void *arg __unused)
+{
+	/*
+	 * The la52 entries must go first so they can either claim the
+	 * image or hand it to the la48 ones.
+	 */
+	elf64_insert_brand_entry(&freebsd_brand_info_elfv2_la52);
+	elf64_insert_brand_entry(&freebsd_brand_info_elfv2_la48);
+	elf64_insert_brand_entry(&freebsd_brand_info_elfv1_la52);
+	elf64_insert_brand_entry(&freebsd_brand_info_elfv1_la48);
+}
+SYSINIT(elf64_brands, SI_SUB_EXEC, SI_ORDER_FIRST,
+    ppc64_register_brand_entries, NULL);
 
 static Elf64_Brandinfo freebsd_brand_oinfo = {
 	.brand		= ELFOSABI_FREEBSD,
 	.machine	= EM_PPC64,
 	.compat_3_brand	= "FreeBSD",
 	.interp_path	= "/usr/libexec/ld-elf.so.1",
-	.sysvec		= &elf64_freebsd_sysvec_v1,
+	.sysvec		= &elf64_freebsd_sysvec_v1_la48,
 	.interp_newpath	= NULL,
 	.brand_note	= &elf64_freebsd_brandnote,
 	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE,
@@ -203,20 +323,27 @@ void elf_reloc_self(Elf_Dyn *dynp, Elf_Addr relocbase);
 static void
 ppc64_init_sysvecs(void *arg)
 {
-	exec_sysvec_init(&elf64_freebsd_sysvec_v2);
-	exec_sysvec_init_secondary(&elf64_freebsd_sysvec_v2,
-	    &elf64_freebsd_sysvec_v1);
+	exec_sysvec_init(&elf64_freebsd_sysvec_v2_la48);
+	exec_sysvec_init_secondary(&elf64_freebsd_sysvec_v2_la48,
+	    &elf64_freebsd_sysvec_v2_la52);
+	exec_sysvec_init_secondary(&elf64_freebsd_sysvec_v2_la48,
+	    &elf64_freebsd_sysvec_v1_la48);
+	exec_sysvec_init_secondary(&elf64_freebsd_sysvec_v2_la48,
+	    &elf64_freebsd_sysvec_v1_la52);
 	/*
-	 * Adjust elfv2 sigcode after elfv1 sysvec is initialized.
+	 * Adjust elfv2 sigcode after the elfv1 sysvecs are initialized.
 	 * exec_sysvec_init_secondary() assumes secondary sysvecs use
 	 * identical signal code, and skips allocating a second copy.
 	 * Since the ELFv2 trampoline is a strict subset of the ELFv1 code,
 	 * we can work around this by adjusting the offset. This also
 	 * avoids two copies of the trampoline code being allocated!
 	 */
-	elf64_freebsd_sysvec_v2.sv_sigcode_offset +=
+	elf64_freebsd_sysvec_v2_la48.sv_sigcode_offset +=
+	    (uintptr_t)sigcode64_elfv2 - (uintptr_t)&sigcode64;
+	elf64_freebsd_sysvec_v2_la48.sv_szsigcode = &szsigcode64_elfv2;
+	elf64_freebsd_sysvec_v2_la52.sv_sigcode_offset +=
 	    (uintptr_t)sigcode64_elfv2 - (uintptr_t)&sigcode64;
-	elf64_freebsd_sysvec_v2.sv_szsigcode = &szsigcode64_elfv2;
+	elf64_freebsd_sysvec_v2_la52.sv_szsigcode = &szsigcode64_elfv2;
 }
 SYSINIT(elf64_sysvec, SI_SUB_EXEC, SI_ORDER_ANY, ppc64_init_sysvecs, NULL);
 
@@ -240,6 +367,50 @@ ppc64_elfv2_header_match(struct image_params *params, int32_t *osrel __unused,
 	return (abi == 2);
 }
 
+/*
+ * Processes get a 48-bit address space by default, see vmparam.h.  The full
+ * 52 bits are available through the la52 feature control note or
+ * PROC_LA_CTL; vm.pmap.prefer_uva_la48 flips the default like on amd64.
+ */
+static int prefer_uva_la48 = 1;
+SYSCTL_DECL(_vm_pmap);
+SYSCTL_INT(_vm_pmap, OID_AUTO, prefer_uva_la48, CTLFLAG_RDTUN,
+    &prefer_uva_la48, 0,
+    "Userspace maps are limited to LA48 unless otherwise configured");
+
+static bool
+ppc64_la52_img_compat(const struct image_params *imgp, const uint32_t *fctl0)
+{
+
+	if ((imgp->proc->p_md.md_flags & P_MD_LA52) != 0)
+		return (true);
+	if (fctl0 != NULL && (*fctl0 & NT_FREEBSD_FCTL_LA52) != 0)
+		return (true);
+	if (fctl0 == NULL || (*fctl0 & NT_FREEBSD_FCTL_LA48) != 0)
+		return (false);
+	if ((imgp->proc->p_md.md_flags & P_MD_LA48) != 0)
+		return (false);
+	return (!prefer_uva_la48);
+}
+
+static bool
+ppc64_elfv1_la52_header_match(struct image_params *params,
+    int32_t *osrel, uint32_t *fctl0)
+{
+
+	return (ppc64_elfv1_header_match(params, osrel, fctl0) &&
+	    ppc64_la52_img_compat(params, fctl0));
+}
+
+static bool
+ppc64_elfv2_la52_header_match(struct image_params *params,
+    int32_t *osrel, uint32_t *fctl0)
+{
+
+	return (ppc64_elfv2_header_match(params, osrel, fctl0) &&
+	    ppc64_la52_img_compat(params, fctl0));
+}
+
 static void  
 exec_setregs_funcdesc(struct thread *td, struct image_params *imgp,
     uintptr_t stack)
diff --git a/sys/powerpc/powerpc/exec_machdep.c b/sys/powerpc/powerpc/exec_machdep.c
index 0738bc0dec3e..7b5f1bcca8c8 100644
--- a/sys/powerpc/powerpc/exec_machdep.c
+++ b/sys/powerpc/powerpc/exec_machdep.c
@@ -128,7 +128,8 @@ static int	grab_mcontext(struct thread *, mcontext_t *, int);
 static void	cleanup_power_extras(struct thread *);
 
 #ifdef __powerpc64__
-extern struct sysentvec elf64_freebsd_sysvec_v2;
+extern struct sysentvec elf64_freebsd_sysvec_v2_la48;
+extern struct sysentvec elf64_freebsd_sysvec_v2_la52;
 #endif
 
 #ifdef __powerpc64__
@@ -1214,7 +1215,8 @@ cpu_set_upcall(struct thread *td, void (*entry)(void *), void *arg,
 		#endif
 	} else {
 	    #ifdef __powerpc64__
-		if (td->td_proc->p_sysent == &elf64_freebsd_sysvec_v2) {
+		if (td->td_proc->p_sysent == &elf64_freebsd_sysvec_v2_la48 ||
+		    td->td_proc->p_sysent == &elf64_freebsd_sysvec_v2_la52) {
 			tf->srr0 = (register_t)entry;
 			/* ELFv2 ABI requires that the global entry point be in r12. */
 			tf->fixreg[12] = (register_t)entry;
diff --git a/sys/powerpc/powerpc/vm_machdep.c b/sys/powerpc/powerpc/vm_machdep.c
index 1a31959b278b..fd35fc3aa48f 100644
--- a/sys/powerpc/powerpc/vm_machdep.c
+++ b/sys/powerpc/powerpc/vm_machdep.c
@@ -81,9 +81,11 @@
 #include <sys/vmmeter.h>
 #include <sys/kernel.h>
 #include <sys/mbuf.h>
+#include <sys/procctl.h>
 #include <sys/sysctl.h>
 #include <sys/sysent.h>
 #include <sys/unistd.h>
+#include <sys/wait.h>
 
 #include <machine/cpu.h>
 #include <machine/fpu.h>
@@ -132,6 +134,9 @@ cpu_fork(struct thread *td1, struct proc *p2, struct thread *td2, int flags)
 	/* Copy the pcb */
 	bcopy(td1->td_pcb, pcb, sizeof(struct pcb));
 
+	/* Inherit the machine-dependent process flags. */
+	bcopy(&td1->td_proc->p_md, &p2->p_md, sizeof(p2->p_md));
+
 	/*
 	 * Create a fresh stack for the new process.
 	 * Copy the trap frame for the return to user mode as if from a
@@ -232,12 +237,89 @@ cpu_exec_vmspace_reuse(struct proc *p __unused, vm_map_t map __unused)
 	return (true);
 }
 
-int
-cpu_procctl(struct thread *td __unused, int idtype __unused, id_t id __unused,
-    int com __unused, void *data __unused)
+#ifdef __powerpc64__
+static void
+cpu_procctl_la_ctl(struct proc *p, int val)
 {
 
+	switch (val) {
+	case PROC_LA_CTL_LA48_ON_EXEC:
+		p->p_md.md_flags |= P_MD_LA48;
+		p->p_md.md_flags &= ~P_MD_LA52;
+		break;
+	case PROC_LA_CTL_LA52_ON_EXEC:
+		p->p_md.md_flags &= ~P_MD_LA48;
+		p->p_md.md_flags |= P_MD_LA52;
+		break;
+	case PROC_LA_CTL_DEFAULT_ON_EXEC:
+		p->p_md.md_flags &= ~(P_MD_LA48 | P_MD_LA52);
+		break;
+	}
+}
+
+static void
+cpu_procctl_la_status(struct proc *p, int *val)
+{
+	int res;
+
+	if ((p->p_md.md_flags & P_MD_LA48) != 0)
+		res = PROC_LA_CTL_LA48_ON_EXEC;
+	else if ((p->p_md.md_flags & P_MD_LA52) != 0)
+		res = PROC_LA_CTL_LA52_ON_EXEC;
+	else
+		res = PROC_LA_CTL_DEFAULT_ON_EXEC;
+	if (p->p_sysent->sv_maxuser <= VM_MAXUSER_ADDRESS_LA48)
+		res |= PROC_LA_STATUS_LA48;
+	else
+		res |= PROC_LA_STATUS_LA52;
+	*val = res;
+}
+#endif
+
+int
+cpu_procctl(struct thread *td, int idtype, id_t id, int com, void *data)
+{
+#ifdef __powerpc64__
+	struct proc *p;
+	int error, val;
+
+	switch (com) {
+	case PROC_LA_CTL:
+	case PROC_LA_STATUS:
+		if (idtype != P_PID) {
+			error = EINVAL;
+			break;
+		}
+		if (com == PROC_LA_CTL) {
+			error = copyin(data, &val, sizeof(val));
+			if (error != 0)
+				break;
+			if (val != PROC_LA_CTL_LA48_ON_EXEC &&
+			    val != PROC_LA_CTL_LA52_ON_EXEC &&
+			    val != PROC_LA_CTL_DEFAULT_ON_EXEC) {
+				error = EINVAL;
+				break;
+			}
+		}
+		error = pget(id, PGET_CANSEE | PGET_NOTWEXIT | PGET_NOTID, &p);
+		if (error != 0)
+			break;
+		if (com == PROC_LA_CTL)
+			cpu_procctl_la_ctl(p, val);
+		else
+			cpu_procctl_la_status(p, &val);
+		PROC_UNLOCK(p);
+		if (com == PROC_LA_STATUS)
+			error = copyout(&val, data, sizeof(val));
+		break;
+	default:
+		error = EINVAL;
+		break;
+	}
+	return (error);
+#else
 	return (EINVAL);
+#endif
 }
 
 void
diff --git a/sys/sys/elf_common.h b/sys/sys/elf_common.h
index aea75a5c532c..dea73016e02a 100644
--- a/sys/sys/elf_common.h
+++ b/sys/sys/elf_common.h
@@ -854,7 +854,8 @@ typedef struct {
 #define	NT_FREEBSD_FCTL_WXNEEDED	0x00000008
 #define	NT_FREEBSD_FCTL_LA48		0x00000010
 /* was ASG_DISABLE, do not reuse	0x00000020 */
-#define	NT_FREEBSD_FCTL_LA57		0x00000040
+#define	NT_FREEBSD_FCTL_LA57		0x00000040	/* amd64 */
+#define	NT_FREEBSD_FCTL_LA52		0x00000040	/* powerpc64 */
 
 /* Values for n_type.  Used in core files. */
 #define	NT_PRSTATUS	1	/* Process status. */