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