Skip to content

Commit fbfe9c8

Browse files
inguintorvalds
authored andcommitted
um: Save FPU registers between task switches
Some time ago Jeff prepared 42daba3 ("uml: stop saving process FP state") for UML to stop saving the process FP state between task switches. The assumption was that since with SKAS0 every guest process runs inside a host process context the host OS will take care of keeping the proper FP state. Unfortunately this is not true for multi-threaded applications, where all guest threads share a single host process context yet all may use the FPU on their own. Although I haven't verified it I suspect things to be even worse in SKAS3 mode where all guest processes run inside a single host process. The patch reintroduces the saving and restoring of the FP context between task switches. [richard@nod.at: Ingo posted this patch in 2009, sadly it was never applied and got lost. Now in 2011 the problem was reported by Gunnar.] Signed-off-by: Ingo van Lil <inguin@gmx.de> Signed-off-by: Richard Weinberger <richard@nod.at> Reported-by: <gunnarlindroth@hotmail.com> Tested-by: <gunnarlindroth@hotmail.com> Cc: Stanislav Meduna <stano@meduna.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent b40997b commit fbfe9c8

7 files changed

Lines changed: 31 additions & 5 deletions

File tree

‎arch/um/include/shared/registers.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ extern int restore_fpx_registers(int pid, unsigned long *fp_regs);
1616
extern int save_registers(int pid, struct uml_pt_regs *regs);
1717
extern int restore_registers(int pid, struct uml_pt_regs *regs);
1818
extern int init_registers(int pid);
19-
extern void get_safe_registers(unsigned long *regs);
19+
extern void get_safe_registers(unsigned long *regs, unsigned long *fp_regs);
2020
extern unsigned long get_thread_reg(int reg, jmp_buf *buf);
2121
extern int get_fp_registers(int pid, unsigned long *regs);
2222
extern int put_fp_registers(int pid, unsigned long *regs);

‎arch/um/kernel/process.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ int copy_thread(unsigned long clone_flags, unsigned long sp,
202202
arch_copy_thread(&current->thread.arch, &p->thread.arch);
203203
}
204204
else {
205-
get_safe_registers(p->thread.regs.regs.gp);
205+
get_safe_registers(p->thread.regs.regs.gp, p->thread.regs.regs.fp);
206206
p->thread.request.u.thread = current->thread.request.u.thread;
207207
handler = new_thread_handler;
208208
}

‎arch/um/os-Linux/registers.c‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#include <string.h>
99
#include <sys/ptrace.h>
1010
#include "sysdep/ptrace.h"
11+
#include "sysdep/ptrace_user.h"
12+
#include "registers.h"
1113

1214
int save_registers(int pid, struct uml_pt_regs *regs)
1315
{
@@ -32,6 +34,7 @@ int restore_registers(int pid, struct uml_pt_regs *regs)
3234
/* This is set once at boot time and not changed thereafter */
3335

3436
static unsigned long exec_regs[MAX_REG_NR];
37+
static unsigned long exec_fp_regs[FP_SIZE];
3538

3639
int init_registers(int pid)
3740
{
@@ -42,10 +45,14 @@ int init_registers(int pid)
4245
return -errno;
4346

4447
arch_init_registers(pid);
48+
get_fp_registers(pid, exec_fp_regs);
4549
return 0;
4650
}
4751

48-
void get_safe_registers(unsigned long *regs)
52+
void get_safe_registers(unsigned long *regs, unsigned long *fp_regs)
4953
{
5054
memcpy(regs, exec_regs, sizeof(exec_regs));
55+
56+
if (fp_regs)
57+
memcpy(fp_regs, exec_fp_regs, sizeof(exec_fp_regs));
5158
}

‎arch/um/os-Linux/skas/mem.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ static unsigned long syscall_regs[MAX_REG_NR];
3939

4040
static int __init init_syscall_regs(void)
4141
{
42-
get_safe_registers(syscall_regs);
42+
get_safe_registers(syscall_regs, NULL);
4343
syscall_regs[REGS_IP_INDEX] = STUB_CODE +
4444
((unsigned long) &batch_syscall_stub -
4545
(unsigned long) &__syscall_stub_start);

‎arch/um/os-Linux/skas/process.c‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,9 @@ void userspace(struct uml_pt_regs *regs)
373373
if (ptrace(PTRACE_SETREGS, pid, 0, regs->gp))
374374
fatal_sigsegv();
375375

376+
if (put_fp_registers(pid, regs->fp))
377+
fatal_sigsegv();
378+
376379
/* Now we set local_using_sysemu to be used for one loop */
377380
local_using_sysemu = get_using_sysemu();
378381

@@ -399,6 +402,12 @@ void userspace(struct uml_pt_regs *regs)
399402
fatal_sigsegv();
400403
}
401404

405+
if (get_fp_registers(pid, regs->fp)) {
406+
printk(UM_KERN_ERR "userspace - get_fp_registers failed, "
407+
"errno = %d\n", errno);
408+
fatal_sigsegv();
409+
}
410+
402411
UPT_SYSCALL_NR(regs) = -1; /* Assume: It's not a syscall */
403412

404413
if (WIFSTOPPED(status)) {
@@ -457,10 +466,11 @@ void userspace(struct uml_pt_regs *regs)
457466
}
458467

459468
static unsigned long thread_regs[MAX_REG_NR];
469+
static unsigned long thread_fp_regs[FP_SIZE];
460470

461471
static int __init init_thread_regs(void)
462472
{
463-
get_safe_registers(thread_regs);
473+
get_safe_registers(thread_regs, thread_fp_regs);
464474
/* Set parent's instruction pointer to start of clone-stub */
465475
thread_regs[REGS_IP_INDEX] = STUB_CODE +
466476
(unsigned long) stub_clone_handler -
@@ -503,6 +513,13 @@ int copy_context_skas0(unsigned long new_stack, int pid)
503513
return err;
504514
}
505515

516+
err = put_fp_registers(pid, thread_fp_regs);
517+
if (err < 0) {
518+
printk(UM_KERN_ERR "copy_context_skas0 : put_fp_registers "
519+
"failed, pid = %d, err = %d\n", pid, err);
520+
return err;
521+
}
522+
506523
/* set a well known return code for detection of child write failure */
507524
child_data->err = 12345678;
508525

‎arch/um/sys-i386/shared/sysdep/ptrace.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ extern int sysemu_supported;
5353

5454
struct uml_pt_regs {
5555
unsigned long gp[MAX_REG_NR];
56+
unsigned long fp[HOST_FPX_SIZE];
5657
struct faultinfo faultinfo;
5758
long syscall;
5859
int is_user;

‎arch/um/sys-x86_64/shared/sysdep/ptrace.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585

8686
struct uml_pt_regs {
8787
unsigned long gp[MAX_REG_NR];
88+
unsigned long fp[HOST_FP_SIZE];
8889
struct faultinfo faultinfo;
8990
long syscall;
9091
int is_user;

0 commit comments

Comments
 (0)