Skip to contentSkip to conversation

um: always use the internal copy of the FP registers

PATCH Superseded Benjamin Berg 2 participants 4 messages Sep 13, 2024

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 um: always use the internal copy of the FP registers

From: Benjamin Berg <[email protected]>

When switching from userspace to the kernel, all registers including the FP registers are copied into the kernel and restored later on. As such, the true source for the FP register state is actually already in the kernel and they should never be grabbed from the userspace process.

Change the various places to simply copy the data from the internal FP register storage area. Note that on i386 the format of PTRACE_GETFPREGS and PTRACE_GETFPXREGS is different enough that conversion would be needed. With this patch, -EINVAL is returned if the non-native format is requested.

The upside is, that this patchset fixes setting registers via ptrace (which simply did not work before) as well as fixing setting floating point registers using the mcontext on signal return on i386.

---

I think that ideally more changs should be done in this space. For a start, one can remove support for hosts without PTRACE_GETFPXREGS. But, more importantly, UML should probably implement the regset API, which would considerably improve both ptrace and coredump generation and should also make it easy to fix the cases where this patch returns an error.

---
 arch/um/include/shared/registers.h |  6 ---
 arch/um/kernel/process.c           | 11 ++++-
 arch/x86/um/os-Linux/registers.c   | 12 +++---
 arch/x86/um/ptrace_32.c            | 50 ++++++++++++-----------
 arch/x86/um/ptrace_64.c            | 20 ++++-----
 arch/x86/um/signal.c               | 65 ++++++++++++++----------------
 6 files changed, 80 insertions(+), 84 deletions(-)
arch/um/include/shared/registers.h +0 -6
index a0450326521c..7d81b2339a48 100644
@@ -8,12 +8,6 @@
 
 #include <sysdep/ptrace.h>
 
-extern int save_i387_registers(int pid, unsigned long *fp_regs);
-extern int restore_i387_registers(int pid, unsigned long *fp_regs);
-extern int save_fp_registers(int pid, unsigned long *fp_regs);
-extern int restore_fp_registers(int pid, unsigned long *fp_regs);
-extern int save_fpx_registers(int pid, unsigned long *fp_regs);
-extern int restore_fpx_registers(int pid, unsigned long *fp_regs);
 extern int init_pid_registers(int pid);
 extern void get_safe_registers(unsigned long *regs, unsigned long *fp_regs);
 extern int get_fp_registers(int pid, unsigned long *regs);
arch/um/kernel/process.c +9 -2
index be2856af6d4c..ad798d40f8a4 100644
@@ -290,8 +290,15 @@ unsigned long __get_wchan(struct task_struct *p)
 
 int elf_core_copy_task_fpregs(struct task_struct *t, elf_fpregset_t *fpu)
 {
-	int cpu = current_thread_info()->cpu;
+#ifdef CONFIG_X86_32
+	extern int have_fpx_regs;
 
-	return save_i387_registers(userspace_pid[cpu], (unsigned long *) fpu);
+	/* FIXME: A plain copy does not work on i386 with have_fpx_regs */
+	if (have_fpx_regs)
+		return -1;
+#endif
+	memcpy(fpu, &t->thread.regs.regs.fp, sizeof(*fpu));
+
+	return 0;
 }
 
arch/x86/um/os-Linux/registers.c +6 -6
index f3638dd09cec..f8e5516d9708 100644
@@ -19,14 +19,14 @@
 
 static int have_xstate_support;
 
-int save_i387_registers(int pid, unsigned long *fp_regs)
+static int save_i387_registers(int pid, unsigned long *fp_regs)
 {
 	if (ptrace(PTRACE_GETFPREGS, pid, 0, fp_regs) < 0)
 		return -errno;
 	return 0;
 }
 
-int save_fp_registers(int pid, unsigned long *fp_regs)
+static int save_fp_registers(int pid, unsigned long *fp_regs)
 {
 #ifdef PTRACE_GETREGSET
 	struct iovec iov;
@@ -42,14 +42,14 @@ int save_fp_registers(int pid, unsigned long *fp_regs)
 		return save_i387_registers(pid, fp_regs);
 }
 
-int restore_i387_registers(int pid, unsigned long *fp_regs)
+static int restore_i387_registers(int pid, unsigned long *fp_regs)
 {
 	if (ptrace(PTRACE_SETFPREGS, pid, 0, fp_regs) < 0)
 		return -errno;
 	return 0;
 }
 
-int restore_fp_registers(int pid, unsigned long *fp_regs)
+static int restore_fp_registers(int pid, unsigned long *fp_regs)
 {
 #ifdef PTRACE_SETREGSET
 	struct iovec iov;
@@ -66,14 +66,14 @@ int restore_fp_registers(int pid, unsigned long *fp_regs)
 
 #ifdef __i386__
 int have_fpx_regs = 1;
-int save_fpx_registers(int pid, unsigned long *fp_regs)
+static int save_fpx_registers(int pid, unsigned long *fp_regs)
 {
 	if (ptrace(PTRACE_GETFPXREGS, pid, 0, fp_regs) < 0)
 		return -errno;
 	return 0;
 }
 
-int restore_fpx_registers(int pid, unsigned long *fp_regs)
+static int restore_fpx_registers(int pid, unsigned long *fp_regs)
 {
 	if (ptrace(PTRACE_SETFPXREGS, pid, 0, fp_regs) < 0)
 		return -errno;
arch/x86/um/ptrace_32.c +27 -23

86 lines not on this page · raw message

arch/x86/um/ptrace_64.c +7 -13
index aa68d83d3f44..f8bbad29cd0b 100644
@@ -190,15 +190,10 @@ int peek_user(struct task_struct *child, long addr, long data)
 
 static int get_fpregs(struct user_i387_struct __user *buf, struct task_struct *child)
 {
-	int err, n, cpu = ((struct thread_info *) child->stack)->cpu;
-	struct user_i387_struct fpregs;
+	int n;
 
-	err = save_i387_registers(userspace_pid[cpu],
-				  (unsigned long *) &fpregs);
-	if (err)
-		return err;
-
-	n = copy_to_user(buf, &fpregs, sizeof(fpregs));
+	n = copy_to_user(buf, &child->thread.regs.regs.fp,
+			 sizeof(struct user_i387_struct));
 	if (n > 0)
 		return -EFAULT;
 
@@ -207,15 +202,14 @@ static int get_fpregs(struct user_i387_struct __user *buf, struct task_struct *c
 
 static int set_fpregs(struct user_i387_struct __user *buf, struct task_struct *child)
 {
-	int n, cpu = ((struct thread_info *) child->stack)->cpu;
-	struct user_i387_struct fpregs;
+	int n;
 
-	n = copy_from_user(&fpregs, buf, sizeof(fpregs));
+	n = copy_from_user(&child->thread.regs.regs.fp, buf,
+			   sizeof(struct user_i387_struct));
 	if (n > 0)
 		return -EFAULT;
 
-	return restore_i387_registers(userspace_pid[cpu],
-				      (unsigned long *) &fpregs);
+	return 0;
 }
 
 long subarch_ptrace(struct task_struct *child, long request,
arch/x86/um/signal.c +31 -34

99 lines not on this page · raw message

Signature · 1 line
2.46.0
#1 lore raw
Quoted 13 lines · Benjamin Berg

From: Benjamin Berg <[email protected]>

When switching from userspace to the kernel, all registers including the FP registers are copied into the kernel and restored later on. As such, the true source for the FP register state is actually already in the kernel and they should never be grabbed from the userspace process.

Change the various places to simply copy the data from the internal FP register storage area. Note that on i386 the format of PTRACE_GETFPREGS and PTRACE_GETFPXREGS is different enough that conversion would be needed. With this patch, -EINVAL is returned if the non-native format is requested.

Quoted 5 lines

The upside is, that this patchset fixes setting registers via ptrace (which simply did not work before) as well as fixing setting floating point registers using the mcontext on signal return on i386.

Hi,

I did test this mcontext restore and ptrace register setting both on i386 and x86_64 using the attached program.

Benjamin

/*
* gcc test-signal-restore.c -o test-signal-restore-amd64
* gcc -m32 -march=i686 -lm test-signal-restore.c -o test-signal-restore-i386 */

#include <math.h>
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/wait.h>
#include <errno.h>
#include <unistd.h>
#include <sys/ptrace.h>
#include <sys/user.h>
#include <asm/unistd.h>

#define ST0_EXP_ADD 10

void *scratch_page;

void sighandler(int sig, siginfo_t *info, void *p)

{
	ucontext_t *uc = p;

	printf("sighandler\n");

	uc->uc_mcontext.fpregs->_st[0].exponent += ST0_EXP_ADD;
}

int test_fp()

{
	double num = 0.5;
	long ret;

	printf("pre-signal: %g\n", num);
	/*
	 * This does kill(getpid(), SIGUSR1); with "num" being passed in AND
	 * out of the floating point stack. We can therefore modify num by
	 * changing st[0] when handling the signal.
	 */
#ifdef __i386__
	asm volatile (
		"int $0x80;"
		: "=t" (num), "=a" (ret)
		: "0" (num), "1" (__NR_kill), "b" (getpid()), "c" (SIGUSR1) : );
#else
	asm volatile (
		"syscall;"
		: "=t" (num), "=a" (ret)
		: "0" (num), "1" (__NR_kill), "D" (getpid()), "S" (SIGUSR1) : "r11", "rcx");
#endif
	printf("post-signal: %g\n", num);

	if (num != pow(2, ST0_EXP_ADD - 1)) {
		printf("floating point register was not manipulated\n");
		return 1;
	}

	return 0;
}

int test_fp_ptrace(int fpx)

{
	int pid, status, ret;

	pid = fork();
	if (pid < 0)
		return 127;

	if (pid == 0) {
		/* child */
		ptrace(PTRACE_TRACEME, 0, 0, 0);
		kill(getpid(), SIGSTOP);
		
		if (test_fp())
			exit(1);

		exit(0);
	}

	/* Wait for child to stop itself */
	do {
		ret = waitpid(pid, &status, 0);
	} while (ret < 0 && errno == EINTR);
	if (!WIFSTOPPED(status))
		return 127;

	/* Continue until SIGUSR1 to self */
	ptrace(PTRACE_CONT, pid, NULL, 0);
	do {
		ret = waitpid(pid, &status, 0);
	} while (ret < 0 && errno == EINTR);
	if (!WIFSTOPPED(status))
		return 127;

	if (fpx) {
#ifdef __i386__
		struct user_fpxregs_struct *fpstate;
		#define fp_reg_type struct _libc_fpreg
		fpstate = scratch_page + 4096 - sizeof(*fpstate);

		if (ptrace(PTRACE_GETFPXREGS, pid, NULL, fpstate)) {
			kill(pid, SIGKILL);
			if (errno == EINVAL) {
				printf("Getting FPX regs not supported\n");
				return 0;
			} else {
				printf("Error getting FPX regs: %d\n", errno);
				return 127;
			}
		}
		((fp_reg_type*)&fpstate->st_space[0])->exponent += ST0_EXP_ADD;
		if (ptrace(PTRACE_SETFPXREGS, pid, NULL, fpstate))
			return 127;
#else
		printf("No FPXREGS on x86_64\n");
		kill(pid, SIGKILL);
		return 127;
#endif
	} else {
		struct _libc_fpstate *fpstate;
		fpstate = scratch_page + 4096 - sizeof(*fpstate);

		if (ptrace(PTRACE_GETFPREGS, pid, NULL, fpstate)) {
			kill(pid, SIGKILL);
			if (errno == EINVAL) {
				printf("Getting FPX regs not supported\n");
				return 0;
			} else {
				printf("Error getting FPX regs: %d\n", errno);
				return 127;
			}
		}
		fpstate->_st[0].exponent += ST0_EXP_ADD;
		if (ptrace(PTRACE_SETFPREGS, pid, NULL, fpstate))
			return 127;
	}

	/* Run until completion (without handling the signal) */
	ptrace(PTRACE_CONT, pid, NULL, 0);
	do {
		ret = waitpid(pid, &status, 0);
	} while (ret < 0 && errno == EINTR);

	if (!WIFEXITED(status))
		return 127;

	return WEXITSTATUS(status);
}

int main()

{
	struct sigaction sa = {
		.sa_flags = SA_SIGINFO,
		.sa_handler = (void (*)(int))sighandler,
	};
	int ret;

	scratch_page = mmap(NULL, 8192, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
	munmap(scratch_page + 4096, 4096);

	sigaction(SIGUSR1, &sa, NULL);

	if (test_fp())
		return 1;

	sa.sa_handler = SIG_DFL;
	sigaction(SIGUSR1, &sa, NULL);

	printf("\nmodify using ptrace PTRACE_GETFPREGS instead of sighandler:\n");
	ret = test_fp_ptrace(0);
	if (ret)
		return ret;
#ifdef __i386__
	printf("\nmodify using ptrace PTRACE_GETFPXREGS instead of sighandler:\n");
	ret = test_fp_ptrace(1);
	if (ret)
		return ret;

#endif

	return 0;
}
#2 lore raw
In reply to Benjamin Berg
Quoted 9 lines · Benjamin Berg

From: Benjamin Berg <[email protected]>

When switching from userspace to the kernel, all registers including the FP registers are copied into the kernel and restored later on. As such, the true source for the FP register state is actually already in the kernel and they should never be grabbed from the userspace process.

Change the various places to simply copy the data from the internal FP register storage area.

Cool! I had similar thoughts, but I haven't tried to make it happen.

Quoted 56 lines

Note that on i386 the format of PTRACE_GETFPREGS
and PTRACE_GETFPXREGS is different enough that conversion would be needed. With this patch, -EINVAL is returned if the non-native format is requested.

The upside is, that this patchset fixes setting registers via ptrace (which simply did not work before) as well as fixing setting floating point registers using the mcontext on signal return on i386.

---

I think that ideally more changs should be done in this space. For a start, one can remove support for hosts without PTRACE_GETFPXREGS. But, more importantly, UML should probably implement the regset API, which would considerably improve both ptrace and coredump generation and should also make it easy to fix the cases where this patch returns an error.

---
 arch/um/include/shared/registers.h |  6 ---
 arch/um/kernel/process.c           | 11 ++++-
 arch/x86/um/os-Linux/registers.c   | 12 +++---
 arch/x86/um/ptrace_32.c            | 50 ++++++++++++-----------
 arch/x86/um/ptrace_64.c            | 20 ++++-----
 arch/x86/um/signal.c               | 65 ++++++++++++++----------------
 6 files changed, 80 insertions(+), 84 deletions(-)
arch/um/include/shared/registers.h +0 -6
index a0450326521c..7d81b2339a48 100644
@@ -8,12 +8,6 @@
 
 #include <sysdep/ptrace.h>
 
-extern int save_i387_registers(int pid, unsigned long *fp_regs);
-extern int restore_i387_registers(int pid, unsigned long *fp_regs);
-extern int save_fp_registers(int pid, unsigned long *fp_regs);
-extern int restore_fp_registers(int pid, unsigned long *fp_regs);
-extern int save_fpx_registers(int pid, unsigned long *fp_regs);
-extern int restore_fpx_registers(int pid, unsigned long *fp_regs);
 extern int init_pid_registers(int pid);
 extern void get_safe_registers(unsigned long *regs, unsigned long *fp_regs);
 extern int get_fp_registers(int pid, unsigned long *regs);
arch/um/kernel/process.c +2 -1
index be2856af6d4c..ad798d40f8a4 100644
@@ -290,8 +290,15 @@ unsigned long __get_wchan(struct task_struct *p)
 
 int elf_core_copy_task_fpregs(struct task_struct *t, elf_fpregset_t *fpu)
 {
-	int cpu = current_thread_info()->cpu;
+#ifdef CONFIG_X86_32
+	extern int have_fpx_regs;

arch/um/kernel/process.c unsigned long __get_wchan(struct task_struct *p) · line 303

Quoted 10 lines
arch/um/kernel/process.c +7 -1
 
-	return save_i387_registers(userspace_pid[cpu], (unsigned long *) fpu);
+	/* FIXME: A plain copy does not work on i386 with have_fpx_regs */
+	if (have_fpx_regs)
+		return -1;
+#endif
+	memcpy(fpu, &t->thread.regs.regs.fp, sizeof(*fpu));
+
+	return 0;
 }

This function is expected to return a boolean value which should be true on success.

https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/fs/binfmt_elf.c?h=v6.11-rc7#n1816

Regards,
Tiwei

#3 lore raw

Hi,

Quoted 19 lines · Tiwei Bie
Quoted 18 lines · Benjamin Berg

From: Benjamin Berg <[email protected]>
[SNIP]

arch/um/kernel/process.c +0 -0
index be2856af6d4c..ad798d40f8a4 100644
@@ -290,8 +290,15 @@ unsigned long __get_wchan(struct task_struct *p)


 int elf_core_copy_task_fpregs(struct task_struct *t, elf_fpregset_t *fpu) {

arch/um/kernel/process.c +2 -1
-	int cpu = current_thread_info()->cpu;
+#ifdef CONFIG_X86_32
+	extern int have_fpx_regs;

arch/um/kernel/process.c +3 -1
-	return save_i387_registers(userspace_pid[cpu], (unsigned long *) fpu);
+	/* FIXME: A plain copy does not work on i386 with have_fpx_regs */
+	if (have_fpx_regs)
+		return -1;
Quoted 8 lines
Quoted 5 lines
arch/um/kernel/process.c +4 -0
+#endif
+	memcpy(fpu, &t->thread.regs.regs.fp, sizeof(*fpu));
+
+	return 0;

 }

This function is expected to return a boolean value which should be true on success.

Hah, of course. And there I was partially doing all of this because of your patch fixing exactly that problem (and because the current code cannot work in SECCOMP mode) …

Benjamin