git: de8374df28cc - main - fork: Allow ABI to specify fork return values for child.
Dmitry Chagin
dchagin at FreeBSD.org
Thu Aug 12 08:45:52 UTC 2021
The branch main has been updated by dchagin:
URL: https://cgit.FreeBSD.org/src/commit/?id=de8374df28cc23ce0d893fd96d1ad0a861414154
commit de8374df28cc23ce0d893fd96d1ad0a861414154
Author: Dmitry Chagin <dchagin at FreeBSD.org>
AuthorDate: 2021-08-12 08:45:25 +0000
Commit: Dmitry Chagin <dchagin at FreeBSD.org>
CommitDate: 2021-08-12 08:45:25 +0000
fork: Allow ABI to specify fork return values for child.
At least Linux x86 ABI's does not use carry bit and expects that the dx register
is preserved. For this add a new sv_set_fork_retval hook and call it from cpu_fork().
Add a short comment about touching dx in x86_set_fork_retval(), for more details
see phab comments from kib@ and imp at .
Reviewed by: kib
Differential revision: https://reviews.freebsd.org/D31472
MFC after: 2 weeks
---
sys/amd64/amd64/elf_machdep.c | 2 ++
sys/amd64/amd64/vm_machdep.c | 15 ++++++++++++---
sys/amd64/cloudabi32/cloudabi32_sysvec.c | 2 ++
sys/amd64/cloudabi64/cloudabi64_sysvec.c | 2 ++
sys/amd64/linux/linux_sysvec.c | 10 ++++++++++
sys/amd64/linux32/linux32_sysvec.c | 10 ++++++++++
sys/compat/ia32/ia32_sysvec.c | 1 +
sys/i386/cloudabi32/cloudabi32_sysvec.c | 2 ++
sys/i386/i386/elf_machdep.c | 1 +
sys/i386/i386/vm_machdep.c | 15 ++++++++++++---
sys/i386/linux/linux_sysvec.c | 11 +++++++++++
sys/kern/imgact_aout.c | 2 ++
sys/kern/init_main.c | 7 +++++++
sys/sys/sysent.h | 2 ++
sys/x86/include/x86_var.h | 1 +
15 files changed, 77 insertions(+), 6 deletions(-)
diff --git a/sys/amd64/amd64/elf_machdep.c b/sys/amd64/amd64/elf_machdep.c
index 80f7d1d8cefa..6535d4bd2446 100644
--- a/sys/amd64/amd64/elf_machdep.c
+++ b/sys/amd64/amd64/elf_machdep.c
@@ -87,6 +87,7 @@ struct sysentvec elf64_freebsd_sysvec_la48 = {
.sv_stackgap = elf64_stackgap,
.sv_onexec_old = exec_onexec_old,
.sv_onexit = exit_onexit,
+ .sv_set_fork_retval = x86_set_fork_retval,
};
struct sysentvec elf64_freebsd_sysvec_la57 = {
@@ -127,6 +128,7 @@ struct sysentvec elf64_freebsd_sysvec_la57 = {
.sv_stackgap = elf64_stackgap,
.sv_onexec_old = exec_onexec_old,
.sv_onexit = exit_onexit,
+ .sv_set_fork_retval= x86_set_fork_retval,
};
static void
diff --git a/sys/amd64/amd64/vm_machdep.c b/sys/amd64/amd64/vm_machdep.c
index 7d65269410e0..4567e6e0eb5d 100644
--- a/sys/amd64/amd64/vm_machdep.c
+++ b/sys/amd64/amd64/vm_machdep.c
@@ -245,9 +245,8 @@ cpu_fork(struct thread *td1, struct proc *p2, struct thread *td2, int flags)
td2->td_frame = (struct trapframe *)td2->td_md.md_stack_base - 1;
bcopy(td1->td_frame, td2->td_frame, sizeof(struct trapframe));
- td2->td_frame->tf_rax = 0; /* Child returns zero */
- td2->td_frame->tf_rflags &= ~PSL_C; /* success */
- td2->td_frame->tf_rdx = 1;
+ /* Set child return values. */
+ p2->p_sysent->sv_set_fork_retval(td2);
/*
* If the parent process has the trap bit set (i.e. a debugger
@@ -300,6 +299,16 @@ cpu_fork(struct thread *td1, struct proc *p2, struct thread *td2, int flags)
*/
}
+void
+x86_set_fork_retval(struct thread *td)
+{
+ struct trapframe *frame = td->td_frame;
+
+ frame->tf_rax = 0; /* Child returns zero */
+ frame->tf_rflags &= ~PSL_C; /* success */
+ frame->tf_rdx = 1; /* System V emulation */
+}
+
/*
* Intercept the return address from a freshly forked process that has NOT
* been scheduled yet.
diff --git a/sys/amd64/cloudabi32/cloudabi32_sysvec.c b/sys/amd64/cloudabi32/cloudabi32_sysvec.c
index 26924ed5a980..5bb4803ac715 100644
--- a/sys/amd64/cloudabi32/cloudabi32_sysvec.c
+++ b/sys/amd64/cloudabi32/cloudabi32_sysvec.c
@@ -36,6 +36,7 @@ __FBSDID("$FreeBSD$");
#include <vm/pmap.h>
#include <machine/frame.h>
+#include <machine/md_var.h>
#include <machine/pcb.h>
#include <machine/vmparam.h>
@@ -225,6 +226,7 @@ static struct sysentvec cloudabi32_elf_sysvec = {
.sv_fetch_syscall_args = cloudabi32_fetch_syscall_args,
.sv_syscallnames = cloudabi32_syscallnames,
.sv_schedtail = cloudabi32_schedtail,
+ .sv_set_fork_retval = x86_set_fork_retval,
};
INIT_SYSENTVEC(elf_sysvec, &cloudabi32_elf_sysvec);
diff --git a/sys/amd64/cloudabi64/cloudabi64_sysvec.c b/sys/amd64/cloudabi64/cloudabi64_sysvec.c
index c08d912e84d4..82c971212087 100644
--- a/sys/amd64/cloudabi64/cloudabi64_sysvec.c
+++ b/sys/amd64/cloudabi64/cloudabi64_sysvec.c
@@ -36,6 +36,7 @@ __FBSDID("$FreeBSD$");
#include <vm/pmap.h>
#include <machine/frame.h>
+#include <machine/md_var.h>
#include <machine/pcb.h>
#include <machine/vmparam.h>
@@ -212,6 +213,7 @@ static struct sysentvec cloudabi64_elf_sysvec = {
.sv_fetch_syscall_args = cloudabi64_fetch_syscall_args,
.sv_syscallnames = cloudabi64_syscallnames,
.sv_schedtail = cloudabi64_schedtail,
+ .sv_set_fork_retval = x86_set_fork_retval,
};
INIT_SYSENTVEC(elf_sysvec, &cloudabi64_elf_sysvec);
diff --git a/sys/amd64/linux/linux_sysvec.c b/sys/amd64/linux/linux_sysvec.c
index 75fb21c7d037..558dfb4e0ac0 100644
--- a/sys/amd64/linux/linux_sysvec.c
+++ b/sys/amd64/linux/linux_sysvec.c
@@ -125,6 +125,7 @@ static void linux_exec_setregs(struct thread *td, struct image_params *imgp,
static void linux_exec_sysvec_init(void *param);
static int linux_on_exec_vmspace(struct proc *p,
struct image_params *imgp);
+static void linux_set_fork_retval(struct thread *td);
static int linux_vsyscall(struct thread *td);
#define LINUX_T_UNKNOWN 255
@@ -269,6 +270,14 @@ linux_set_syscall_retval(struct thread *td, int error)
set_pcb_flags(td->td_pcb, PCB_FULL_IRET);
}
+static void
+linux_set_fork_retval(struct thread *td)
+{
+ struct trapframe *frame = td->td_frame;
+
+ frame->tf_rax = 0;
+}
+
static int
linux_copyout_auxargs(struct image_params *imgp, uintptr_t base)
{
@@ -790,6 +799,7 @@ struct sysentvec elf_linux_sysvec = {
.sv_onexit = linux_on_exit,
.sv_ontdexit = linux_thread_dtor,
.sv_setid_allowed = &linux_setid_allowed_query,
+ .sv_set_fork_retval = linux_set_fork_retval,
};
static int
diff --git a/sys/amd64/linux32/linux32_sysvec.c b/sys/amd64/linux32/linux32_sysvec.c
index 901c79fcdda8..2ec420b3024a 100644
--- a/sys/amd64/linux32/linux32_sysvec.c
+++ b/sys/amd64/linux32/linux32_sysvec.c
@@ -128,6 +128,7 @@ static bool linux32_trans_osrel(const Elf_Note *note, int32_t *osrel);
static void linux_vdso_install(const void *param);
static void linux_vdso_deinstall(const void *param);
static void linux_vdso_reloc(char *mapping, Elf_Addr offset);
+static void linux32_set_fork_retval(struct thread *td);
static void linux32_set_syscall_retval(struct thread *td, int error);
#define LINUX_T_UNKNOWN 255
@@ -703,6 +704,14 @@ linux32_set_syscall_retval(struct thread *td, int error)
}
}
+static void
+linux32_set_fork_retval(struct thread *td)
+{
+ struct trapframe *frame = td->td_frame;
+
+ frame->tf_rax = 0;
+}
+
/*
* Clear registers on exec
* XXX copied from ia32_signal.c.
@@ -956,6 +965,7 @@ struct sysentvec elf_linux_sysvec = {
.sv_onexit = linux_on_exit,
.sv_ontdexit = linux_thread_dtor,
.sv_setid_allowed = &linux_setid_allowed_query,
+ .sv_set_fork_retval = linux32_set_fork_retval,
};
static int
diff --git a/sys/compat/ia32/ia32_sysvec.c b/sys/compat/ia32/ia32_sysvec.c
index df8a36a34810..969f5645a0f4 100644
--- a/sys/compat/ia32/ia32_sysvec.c
+++ b/sys/compat/ia32/ia32_sysvec.c
@@ -133,6 +133,7 @@ struct sysentvec ia32_freebsd_sysvec = {
.sv_stackgap = elf32_stackgap,
.sv_onexec_old = exec_onexec_old,
.sv_onexit = exit_onexit,
+ .sv_set_fork_retval = x86_set_fork_retval,
};
INIT_SYSENTVEC(elf_ia32_sysvec, &ia32_freebsd_sysvec);
diff --git a/sys/i386/cloudabi32/cloudabi32_sysvec.c b/sys/i386/cloudabi32/cloudabi32_sysvec.c
index e0a50f6697a9..773b5803c873 100644
--- a/sys/i386/cloudabi32/cloudabi32_sysvec.c
+++ b/sys/i386/cloudabi32/cloudabi32_sysvec.c
@@ -36,6 +36,7 @@ __FBSDID("$FreeBSD$");
#include <vm/pmap.h>
#include <machine/frame.h>
+#include <machine/md_var.h>
#include <machine/pcb.h>
#include <machine/vmparam.h>
@@ -195,6 +196,7 @@ static struct sysentvec cloudabi32_elf_sysvec = {
.sv_fetch_syscall_args = cloudabi32_fetch_syscall_args,
.sv_syscallnames = cloudabi32_syscallnames,
.sv_schedtail = cloudabi32_schedtail,
+ .sv_set_fork_retval = x86_set_fork_retval,
};
INIT_SYSENTVEC(elf_sysvec, &cloudabi32_elf_sysvec);
diff --git a/sys/i386/i386/elf_machdep.c b/sys/i386/i386/elf_machdep.c
index 5858c8382e33..3754b36d9e33 100644
--- a/sys/i386/i386/elf_machdep.c
+++ b/sys/i386/i386/elf_machdep.c
@@ -88,6 +88,7 @@ struct sysentvec elf32_freebsd_sysvec = {
.sv_trap = NULL,
.sv_onexec_old = exec_onexec_old,
.sv_onexit = exit_onexit,
+ .sv_set_fork_retval = x86_set_fork_retval,
};
INIT_SYSENTVEC(elf32_sysvec, &elf32_freebsd_sysvec);
diff --git a/sys/i386/i386/vm_machdep.c b/sys/i386/i386/vm_machdep.c
index 57377575b9bf..5cdcdee96347 100644
--- a/sys/i386/i386/vm_machdep.c
+++ b/sys/i386/i386/vm_machdep.c
@@ -258,9 +258,8 @@ cpu_fork(struct thread *td1, struct proc *p2, struct thread *td2, int flags)
VM86_STACK_SPACE) - 1;
bcopy(td1->td_frame, td2->td_frame, sizeof(struct trapframe));
- td2->td_frame->tf_eax = 0; /* Child returns zero */
- td2->td_frame->tf_eflags &= ~PSL_C; /* success */
- td2->td_frame->tf_edx = 1;
+ /* Set child return values. */
+ p2->p_sysent->sv_set_fork_retval(td2);
/*
* If the parent process has the trap bit set (i.e. a debugger
@@ -302,6 +301,16 @@ cpu_fork(struct thread *td1, struct proc *p2, struct thread *td2, int flags)
*/
}
+void
+x86_set_fork_retval(struct thread *td)
+{
+ struct trapframe * frame = td->td_frame;
+
+ frame->tf_eax = 0; /* Child returns zero */
+ frame->tf_eflags &= ~PSL_C; /* success */
+ frame->tf_edx = 1; /* System V emulation */
+}
+
/*
* Intercept the return address from a freshly forked process that has NOT
* been scheduled yet.
diff --git a/sys/i386/linux/linux_sysvec.c b/sys/i386/linux/linux_sysvec.c
index 9261364e7a24..02d69de4d8f8 100644
--- a/sys/i386/linux/linux_sysvec.c
+++ b/sys/i386/linux/linux_sysvec.c
@@ -108,6 +108,7 @@ static int linux_on_exec_vmspace(struct proc *p,
struct image_params *imgp);
static int linux_copyout_strings(struct image_params *imgp,
uintptr_t *stack_base);
+static void linux_set_fork_retval(struct thread *td);
static bool linux_trans_osrel(const Elf_Note *note, int32_t *osrel);
static void linux_vdso_install(const void *param);
static void linux_vdso_deinstall(const void *param);
@@ -803,6 +804,14 @@ linux_set_syscall_retval(struct thread *td, int error)
}
}
+static void
+linux_set_fork_retval(struct thread *td)
+{
+ struct trapframe *frame = td->td_frame;
+
+ frame->tf_eax = 0;
+}
+
/*
* exec_setregs may initialize some registers differently than Linux
* does, thus potentially confusing Linux binaries. If necessary, we
@@ -856,6 +865,7 @@ struct sysentvec linux_sysvec = {
.sv_onexit = linux_on_exit,
.sv_ontdexit = linux_thread_dtor,
.sv_setid_allowed = &linux_setid_allowed_query,
+ .sv_set_fork_retval = linux_set_fork_retval,
};
INIT_SYSENTVEC(aout_sysvec, &linux_sysvec);
@@ -898,6 +908,7 @@ struct sysentvec elf_linux_sysvec = {
.sv_onexit = linux_on_exit,
.sv_ontdexit = linux_thread_dtor,
.sv_setid_allowed = &linux_setid_allowed_query,
+ .sv_set_fork_retval = linux_set_fork_retval,
};
static int
diff --git a/sys/kern/imgact_aout.c b/sys/kern/imgact_aout.c
index 9e7455e14e1d..7d7ba51efd84 100644
--- a/sys/kern/imgact_aout.c
+++ b/sys/kern/imgact_aout.c
@@ -103,6 +103,7 @@ struct sysentvec aout_sysvec = {
.sv_trap = NULL,
.sv_onexec_old = exec_onexec_old,
.sv_onexit = exit_onexit,
+ .sv_set_fork_retval = x86_set_fork_retval,
};
#elif defined(__amd64__)
@@ -141,6 +142,7 @@ struct sysentvec aout_sysvec = {
.sv_syscallnames = freebsd32_syscallnames,
.sv_onexec_old = exec_onexec_old,
.sv_onexit = exit_onexit,
+ .sv_set_fork_retval = x86_set_fork_retval,
};
#else
#error "Port me"
diff --git a/sys/kern/init_main.c b/sys/kern/init_main.c
index 86cc2494272f..fde07dcbf46d 100644
--- a/sys/kern/init_main.c
+++ b/sys/kern/init_main.c
@@ -402,6 +402,12 @@ null_set_syscall_retval(struct thread *td __unused, int error __unused)
panic("null_set_syscall_retval");
}
+static void
+null_set_fork_retval(struct thread *td __unused)
+{
+
+}
+
struct sysentvec null_sysvec = {
.sv_size = 0,
.sv_table = NULL,
@@ -430,6 +436,7 @@ struct sysentvec null_sysvec = {
.sv_schedtail = NULL,
.sv_thread_detach = NULL,
.sv_trap = NULL,
+ .sv_set_fork_retval = null_set_fork_retval,
};
/*
diff --git a/sys/sys/sysent.h b/sys/sys/sysent.h
index 1b43e9692153..e51a66f93f29 100644
--- a/sys/sys/sysent.h
+++ b/sys/sys/sysent.h
@@ -156,6 +156,8 @@ struct sysentvec {
void (*sv_ontdexit)(struct thread *td);
int (*sv_setid_allowed)(struct thread *td,
struct image_params *imgp);
+ void (*sv_set_fork_retval)(struct thread *);
+ /* Only used on x86 */
};
#define SV_ILP32 0x000100 /* 32-bit executable. */
diff --git a/sys/x86/include/x86_var.h b/sys/x86/include/x86_var.h
index 0fdff68638cf..ec268503634b 100644
--- a/sys/x86/include/x86_var.h
+++ b/sys/x86/include/x86_var.h
@@ -153,6 +153,7 @@ int pti_get_default(void);
int user_dbreg_trap(register_t dr6);
int minidumpsys(struct dumperinfo *);
struct pcb *get_pcb_td(struct thread *td);
+void x86_set_fork_retval(struct thread *td);
/*
* MSR ops for x86_msr_op()
More information about the dev-commits-src-main
mailing list