git: 01c77a436e35 - main - Pass vdso address to userspace

From: Konstantin Belousov <kib_at_FreeBSD.org>
Date: Mon, 06 Dec 2021 18:48:19 UTC
The branch main has been updated by kib:

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

commit 01c77a436e35d6dc9660ec7bac477946f563c99d
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2021-11-11 17:29:24 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2021-12-06 18:46:49 +0000

    Pass vdso address to userspace
    
    Reviewed by:    emaste
    Discussed with: jrtc27
    Tested by:      pho
    Sponsored by:   The FreeBSD Foundation
    MFC after:      1 month
    Differential revision:  https://reviews.freebsd.org/D32960
---
 libexec/rtld-elf/rtld.c          | 1 +
 sys/kern/imgact_elf.c            | 2 ++
 sys/kern/kern_sharedpage.c       | 7 +++++++
 sys/sys/elf_common.h             | 3 ++-
 sys/sys/sysent.h                 | 1 +
 usr.bin/procstat/procstat_auxv.c | 6 ++++++
 6 files changed, 19 insertions(+), 1 deletion(-)

diff --git a/libexec/rtld-elf/rtld.c b/libexec/rtld-elf/rtld.c
index d5c3d2893582..af29d3ead390 100644
--- a/libexec/rtld-elf/rtld.c
+++ b/libexec/rtld-elf/rtld.c
@@ -6103,6 +6103,7 @@ static const struct auxfmt {
 	AUXFMT(AT_ENVV, "%p"),
 	AUXFMT(AT_PS_STRINGS, "%p"),
 	AUXFMT(AT_FXRNG, "%p"),
+	AUXFMT(AT_KPRELOAD, "%p"),
 };
 
 static bool
diff --git a/sys/kern/imgact_elf.c b/sys/kern/imgact_elf.c
index 7e970c0215b6..4c9a7a40b3b5 100644
--- a/sys/kern/imgact_elf.c
+++ b/sys/kern/imgact_elf.c
@@ -1414,6 +1414,8 @@ __elfN(freebsd_copyout_auxargs)(struct image_params *imgp, uintptr_t base)
 	AUXARGS_ENTRY_PTR(pos, AT_PS_STRINGS, imgp->ps_strings);
 	if (imgp->sysent->sv_fxrng_gen_base != 0)
 		AUXARGS_ENTRY(pos, AT_FXRNG, imgp->sysent->sv_fxrng_gen_base);
+	if (imgp->sysent->sv_vdso_base != 0)
+		AUXARGS_ENTRY(pos, AT_KPRELOAD, imgp->sysent->sv_vdso_base);
 	AUXARGS_ENTRY(pos, AT_NULL, 0);
 
 	free(imgp->auxargs, M_TEMP);
diff --git a/sys/kern/kern_sharedpage.c b/sys/kern/kern_sharedpage.c
index 98f7b619e0b7..9b36676decd5 100644
--- a/sys/kern/kern_sharedpage.c
+++ b/sys/kern/kern_sharedpage.c
@@ -328,6 +328,7 @@ exec_sysvec_init(void *param)
 			if (res == -1)
 				panic("copying sigtramp to shared page");
 			sb += res;
+			sv->sv_vdso_base = sb;
 			sb += sv->sv_sigcodeoff;
 			sv->sv_sigcode_base = sb;
 		} else {
@@ -386,12 +387,18 @@ exec_sysvec_init_secondary(struct sysentvec *sv, struct sysentvec *sv2)
 	MPASS((sv2->sv_flags & SV_ABI_MASK) == (sv->sv_flags & SV_ABI_MASK));
 	MPASS((sv2->sv_flags & SV_TIMEKEEP) == (sv->sv_flags & SV_TIMEKEEP));
 	MPASS((sv2->sv_flags & SV_SHP) != 0 && (sv->sv_flags & SV_SHP) != 0);
+	MPASS((sv2->sv_flags & SV_DSO_SIG) != 0 &&
+	    (sv->sv_flags & SV_DSO_SIG) != 0);
 	MPASS((sv2->sv_flags & SV_RNG_SEED_VER) ==
 	    (sv->sv_flags & SV_RNG_SEED_VER));
 
 	sv2->sv_shared_page_obj = sv->sv_shared_page_obj;
 	sv2->sv_sigcode_base = sv2->sv_shared_page_base +
 	    (sv->sv_sigcode_base - sv->sv_shared_page_base);
+	if ((sv2->sv_flags & SV_DSO_SIG) != 0) {
+		sv2->sv_vdso_base = sv2->sv_shared_page_base +
+		    (sv->sv_vdso_base - sv->sv_shared_page_base);
+	}
 	if ((sv2->sv_flags & SV_ABI_MASK) != SV_ABI_FREEBSD)
 		return;
 	if ((sv2->sv_flags & SV_TIMEKEEP) != 0) {
diff --git a/sys/sys/elf_common.h b/sys/sys/elf_common.h
index e8eda4d91260..de09a2172f81 100644
--- a/sys/sys/elf_common.h
+++ b/sys/sys/elf_common.h
@@ -981,8 +981,9 @@ typedef struct {
 #define	AT_ENVV		31	/* Environment vector */
 #define	AT_PS_STRINGS	32	/* struct ps_strings */
 #define	AT_FXRNG	33	/* Pointer to root RNG seed version. */
+#define	AT_KPRELOAD	34	/* Base of vdso, preloaded by rtld */
 
-#define	AT_COUNT	34	/* Count of defined aux entry types. */
+#define	AT_COUNT	35	/* Count of defined aux entry types. */
 
 /*
  * Relocation types.
diff --git a/sys/sys/sysent.h b/sys/sys/sysent.h
index 81bd1db108f9..84269f233a3a 100644
--- a/sys/sys/sysent.h
+++ b/sys/sys/sysent.h
@@ -144,6 +144,7 @@ struct sysentvec {
 	vm_offset_t	sv_shared_page_len;
 	vm_offset_t	sv_sigcode_base;
 	void		*sv_shared_page_obj;
+	vm_offset_t	sv_vdso_base;
 	void		(*sv_schedtail)(struct thread *);
 	void		(*sv_thread_detach)(struct thread *);
 	int		(*sv_trap)(struct thread *);
diff --git a/usr.bin/procstat/procstat_auxv.c b/usr.bin/procstat/procstat_auxv.c
index f868b7ed2381..d62ed6028569 100644
--- a/usr.bin/procstat/procstat_auxv.c
+++ b/usr.bin/procstat/procstat_auxv.c
@@ -239,6 +239,12 @@ procstat_auxv(struct procstat *procstat, struct kinfo_proc *kipp)
 			xo_emit("{dw:/%s}{Lw:/%-16s/%s}{:AT_FXRNG/%p}\n",
 			    prefix, "AT_FXRNG", auxv[i].a_un.a_ptr);
 			break;
+#endif
+#ifdef AT_KPRELOAD
+		case AT_KPRELOAD:
+			xo_emit("{dw:/%s}{Lw:/%-16s/%s}{:AT_KPRELOAD/%p}\n",
+			    prefix, "AT_KPRELOAD", auxv[i].a_un.a_ptr);
+			break;
 #endif
 		default:
 			xo_emit("{dw:/%s}{Lw:/%16ld/%ld}{:UNKNOWN/%#lx}\n",