xref: /openbmc/linux/arch/sparc/kernel/sigutil_32.c (revision 171f1bc7)
1 #include <linux/kernel.h>
2 #include <linux/types.h>
3 #include <linux/thread_info.h>
4 #include <linux/uaccess.h>
5 #include <linux/sched.h>
6 
7 #include <asm/sigcontext.h>
8 #include <asm/fpumacro.h>
9 #include <asm/ptrace.h>
10 
11 #include "sigutil.h"
12 
13 int save_fpu_state(struct pt_regs *regs, __siginfo_fpu_t __user *fpu)
14 {
15 	int err = 0;
16 #ifdef CONFIG_SMP
17 	if (test_tsk_thread_flag(current, TIF_USEDFPU)) {
18 		put_psr(get_psr() | PSR_EF);
19 		fpsave(&current->thread.float_regs[0], &current->thread.fsr,
20 		       &current->thread.fpqueue[0], &current->thread.fpqdepth);
21 		regs->psr &= ~(PSR_EF);
22 		clear_tsk_thread_flag(current, TIF_USEDFPU);
23 	}
24 #else
25 	if (current == last_task_used_math) {
26 		put_psr(get_psr() | PSR_EF);
27 		fpsave(&current->thread.float_regs[0], &current->thread.fsr,
28 		       &current->thread.fpqueue[0], &current->thread.fpqdepth);
29 		last_task_used_math = NULL;
30 		regs->psr &= ~(PSR_EF);
31 	}
32 #endif
33 	err |= __copy_to_user(&fpu->si_float_regs[0],
34 			      &current->thread.float_regs[0],
35 			      (sizeof(unsigned long) * 32));
36 	err |= __put_user(current->thread.fsr, &fpu->si_fsr);
37 	err |= __put_user(current->thread.fpqdepth, &fpu->si_fpqdepth);
38 	if (current->thread.fpqdepth != 0)
39 		err |= __copy_to_user(&fpu->si_fpqueue[0],
40 				      &current->thread.fpqueue[0],
41 				      ((sizeof(unsigned long) +
42 				      (sizeof(unsigned long *)))*16));
43 	clear_used_math();
44 	return err;
45 }
46 
47 int restore_fpu_state(struct pt_regs *regs, __siginfo_fpu_t __user *fpu)
48 {
49 	int err;
50 #ifdef CONFIG_SMP
51 	if (test_tsk_thread_flag(current, TIF_USEDFPU))
52 		regs->psr &= ~PSR_EF;
53 #else
54 	if (current == last_task_used_math) {
55 		last_task_used_math = NULL;
56 		regs->psr &= ~PSR_EF;
57 	}
58 #endif
59 	set_used_math();
60 	clear_tsk_thread_flag(current, TIF_USEDFPU);
61 
62 	if (!access_ok(VERIFY_READ, fpu, sizeof(*fpu)))
63 		return -EFAULT;
64 
65 	err = __copy_from_user(&current->thread.float_regs[0], &fpu->si_float_regs[0],
66 			       (sizeof(unsigned long) * 32));
67 	err |= __get_user(current->thread.fsr, &fpu->si_fsr);
68 	err |= __get_user(current->thread.fpqdepth, &fpu->si_fpqdepth);
69 	if (current->thread.fpqdepth != 0)
70 		err |= __copy_from_user(&current->thread.fpqueue[0],
71 					&fpu->si_fpqueue[0],
72 					((sizeof(unsigned long) +
73 					(sizeof(unsigned long *)))*16));
74 	return err;
75 }
76 
77 int save_rwin_state(int wsaved, __siginfo_rwin_t __user *rwin)
78 {
79 	int i, err = __put_user(wsaved, &rwin->wsaved);
80 
81 	for (i = 0; i < wsaved; i++) {
82 		struct reg_window32 *rp;
83 		unsigned long fp;
84 
85 		rp = &current_thread_info()->reg_window[i];
86 		fp = current_thread_info()->rwbuf_stkptrs[i];
87 		err |= copy_to_user(&rwin->reg_window[i], rp,
88 				    sizeof(struct reg_window32));
89 		err |= __put_user(fp, &rwin->rwbuf_stkptrs[i]);
90 	}
91 	return err;
92 }
93 
94 int restore_rwin_state(__siginfo_rwin_t __user *rp)
95 {
96 	struct thread_info *t = current_thread_info();
97 	int i, wsaved, err;
98 
99 	__get_user(wsaved, &rp->wsaved);
100 	if (wsaved > NSWINS)
101 		return -EFAULT;
102 
103 	err = 0;
104 	for (i = 0; i < wsaved; i++) {
105 		err |= copy_from_user(&t->reg_window[i],
106 				      &rp->reg_window[i],
107 				      sizeof(struct reg_window32));
108 		err |= __get_user(t->rwbuf_stkptrs[i],
109 				  &rp->rwbuf_stkptrs[i]);
110 	}
111 	if (err)
112 		return err;
113 
114 	t->w_saved = wsaved;
115 	synchronize_user_stack();
116 	if (t->w_saved)
117 		return -EFAULT;
118 	return 0;
119 
120 }
121