1 /*
2  *  linux/arch/x86_64/kernel/i387.c
3  *
4  *  Copyright (C) 1994 Linus Torvalds
5  *  Copyright (C) 2002 Andi Kleen, SuSE Labs
6  *
7  *  Pentium III FXSR, SSE support
8  *  General FPU state handling cleanups
9  *	Gareth Hughes <gareth@valinux.com>, May 2000
10  *
11  *  x86-64 rework 2002 Andi Kleen.
12  *  Does direct fxsave in and out of user space now for signal handlers.
13  *  All the FSAVE<->FXSAVE conversion code has been moved to the 32bit emulation,
14  *  the 64bit user space sees a FXSAVE frame directly.
15  */
16 
17 #include <linux/config.h>
18 #include <linux/sched.h>
19 #include <asm/processor.h>
20 #include <asm/i387.h>
21 #include <asm/sigcontext.h>
22 #include <asm/user.h>
23 #include <asm/ptrace.h>
24 #include <asm/uaccess.h>
25 
26 extern int exception_trace;
27 
init_fpu(struct task_struct * child)28 void init_fpu(struct task_struct *child)
29 {
30 	if (child->used_math) {
31 		unlazy_fpu(child);
32 		return;
33 	}
34 	memset(&child->thread.i387.fxsave, 0, sizeof(struct i387_fxsave_struct));
35 	child->thread.i387.fxsave.cwd = 0x37f;
36 	child->thread.i387.fxsave.mxcsr = 0x1f80;
37 	child->used_math = 1;
38 }
39 
40 /*
41  * Signal frame handlers.
42  */
43 
save_i387(struct _fpstate * buf)44 int save_i387(struct _fpstate *buf)
45 {
46 	struct task_struct *tsk = current;
47 	int err = 0;
48 
49 	{
50 		extern void bad_user_i387_struct(void);
51 		if (sizeof(struct user_i387_struct) != sizeof(tsk->thread.i387.fxsave))
52 			bad_user_i387_struct();
53 	}
54 
55 	if (!tsk->used_math)
56 		return 0;
57 	tsk->used_math = 0; /* trigger finit */
58 	if (tsk->flags & PF_USEDFPU) {
59 		err = save_i387_checking((struct i387_fxsave_struct *)buf);
60 		if (err) {
61 			if (exception_trace)
62 				printk("%s[%d] unaligned signal floating point context %p\n",
63 					tsk->comm, tsk->pid, buf);
64 			return err;
65 		}
66 		stts();
67 	} else {
68 		if (__copy_to_user(buf, &tsk->thread.i387.fxsave,
69 				   sizeof(struct i387_fxsave_struct)))
70 			return -1;
71 	}
72 	return 1;
73 }
74 
75 /*
76  * ptrace request handlers.
77  */
78 
get_fpregs(struct user_i387_struct * buf,struct task_struct * tsk)79 int get_fpregs(struct user_i387_struct *buf, struct task_struct *tsk)
80 {
81 	init_fpu(tsk);
82 	return __copy_to_user((void *)buf, &tsk->thread.i387.fxsave,
83 			       sizeof(struct user_i387_struct)) ? -EFAULT : 0;
84 }
85 
set_fpregs(struct task_struct * tsk,struct user_i387_struct * buf)86 int set_fpregs(struct task_struct *tsk, struct user_i387_struct *buf)
87 {
88 	if (__copy_from_user(&tsk->thread.i387.fxsave, buf,
89 			     sizeof(struct user_i387_struct)))
90 		return -EFAULT;
91 	/* mxcsr bit 6 and 31-16 must be zero for security reasons. */
92 	tsk->thread.i387.fxsave.mxcsr &= 0xffbf;
93 	return 0;
94 }
95 
96 /*
97  * FPU state for core dumps.
98  */
99 
dump_fpu(struct pt_regs * regs,struct user_i387_struct * fpu)100 int dump_fpu( struct pt_regs *regs, struct user_i387_struct *fpu )
101 {
102 	struct task_struct *tsk = current;
103 
104 	if (!tsk->used_math)
105 		return 0;
106 	unlazy_fpu(tsk);
107 
108 	memcpy(fpu, &tsk->thread.i387.fxsave, sizeof(struct user_i387_struct));
109 	return 1;
110 }
111