git: ecaf115434cf - main - riscv: Conditionally modify the ELF64 sysentvec for SV48

From: Mark Johnston <markj_at_FreeBSD.org>
Date: Tue, 01 Mar 2022 14:40:02 UTC
The branch main has been updated by markj:

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

commit ecaf115434cf5b427339d27860bcbee3e9f48e39
Author:     Mark Johnston <markj@FreeBSD.org>
AuthorDate: 2022-03-01 14:05:02 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2022-03-01 14:39:43 +0000

    riscv: Conditionally modify the ELF64 sysentvec for SV48
    
    A sysinit determines whether the pmap has enabled SV48 mode and modifies
    the corresponding fields which describe the user memory map.
    
    Reviewed by:    kib, jhb
    MFC after:      1 week
    Sponsored by:   The FreeBSD Foundation
    Differential Revision:  https://reviews.freebsd.org/D34274
---
 sys/riscv/riscv/elf_machdep.c | 35 +++++++++++++++++++++++++++++------
 1 file changed, 29 insertions(+), 6 deletions(-)

diff --git a/sys/riscv/riscv/elf_machdep.c b/sys/riscv/riscv/elf_machdep.c
index 117f28c941d3..c218fc76af5e 100644
--- a/sys/riscv/riscv/elf_machdep.c
+++ b/sys/riscv/riscv/elf_machdep.c
@@ -63,7 +63,7 @@ static const char *riscv_machine_arch(struct proc *p);
 
 u_long elf_hwcap;
 
-struct sysentvec elf64_freebsd_sysvec = {
+static struct sysentvec elf64_freebsd_sysvec = {
 	.sv_size	= SYS_MAXSYSCALL,
 	.sv_table	= sysent,
 	.sv_transtrap	= NULL,
@@ -79,9 +79,9 @@ struct sysentvec elf64_freebsd_sysvec = {
 	.sv_imgact_try	= NULL,
 	.sv_minsigstksz	= MINSIGSTKSZ,
 	.sv_minuser	= VM_MIN_ADDRESS,
-	.sv_maxuser	= VM_MAXUSER_ADDRESS,
-	.sv_usrstack	= USRSTACK,
-	.sv_psstrings	= PS_STRINGS,
+	.sv_maxuser	= 0,	/* Filled in during boot. */
+	.sv_usrstack	= 0,	/* Filled in during boot. */
+	.sv_psstrings	= 0,	/* Filled in during boot. */
 	.sv_psstringssz	= sizeof(struct ps_strings),
 	.sv_stackprot	= VM_PROT_READ | VM_PROT_WRITE,
 	.sv_copyout_auxargs = __elfN(freebsd_copyout_auxargs),
@@ -94,7 +94,7 @@ struct sysentvec elf64_freebsd_sysvec = {
 	.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 = 0,	/* Filled in during boot. */
 	.sv_shared_page_len = PAGE_SIZE,
 	.sv_schedtail	= NULL,
 	.sv_thread_detach = NULL,
@@ -129,10 +129,33 @@ static Elf64_Brandinfo freebsd_brand_info = {
 	.brand_note	= &elf64_freebsd_brandnote,
 	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE
 };
-
 SYSINIT(elf64, SI_SUB_EXEC, SI_ORDER_FIRST,
     (sysinit_cfunc_t)elf64_insert_brand_entry, &freebsd_brand_info);
 
+static void
+elf64_register_sysvec(void *arg)
+{
+	struct sysentvec *sv;
+
+	sv = arg;
+	switch (pmap_mode) {
+	case PMAP_MODE_SV48:
+		sv->sv_maxuser = VM_MAX_USER_ADDRESS_SV48;
+		sv->sv_usrstack = USRSTACK_SV48;
+		sv->sv_psstrings = PS_STRINGS_SV48;
+		sv->sv_shared_page_base = SHAREDPAGE_SV48;
+		break;
+	case PMAP_MODE_SV39:
+		sv->sv_maxuser = VM_MAX_USER_ADDRESS_SV39;
+		sv->sv_usrstack = USRSTACK_SV39;
+		sv->sv_psstrings = PS_STRINGS_SV39;
+		sv->sv_shared_page_base = SHAREDPAGE_SV39;
+		break;
+	}
+}
+SYSINIT(elf64_register_sysvec, SI_SUB_VM, SI_ORDER_ANY, elf64_register_sysvec,
+    &elf64_freebsd_sysvec);
+
 static bool debug_kld;
 SYSCTL_BOOL(_debug, OID_AUTO, kld_reloc, CTLFLAG_RW, &debug_kld, 0,
     "Activate debug prints in elf_reloc_internal()");