blob: 3208ff528cd26bca5199ab0444c483714035e7c8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
4 * for more details.
5 *
6 * Copyright (C) 1991, 1992 Linus Torvalds
7 * Copyright (C) 1994 - 2000 Ralf Baechle
8 * Copyright (C) 1999, 2000 Silicon Graphics, Inc.
9 */
10
Ralf Baechlee50c0a82005-05-31 11:49:19 +000011#include <linux/config.h>
12
Linus Torvalds1da177e2005-04-16 15:20:36 -070013static inline int
14setup_sigcontext(struct pt_regs *regs, struct sigcontext *sc)
15{
16 int err = 0;
17
18 err |= __put_user(regs->cp0_epc, &sc->sc_pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
20#define save_gp_reg(i) do { \
21 err |= __put_user(regs->regs[i], &sc->sc_regs[i]); \
22} while(0)
23 __put_user(0, &sc->sc_regs[0]); save_gp_reg(1); save_gp_reg(2);
24 save_gp_reg(3); save_gp_reg(4); save_gp_reg(5); save_gp_reg(6);
25 save_gp_reg(7); save_gp_reg(8); save_gp_reg(9); save_gp_reg(10);
26 save_gp_reg(11); save_gp_reg(12); save_gp_reg(13); save_gp_reg(14);
27 save_gp_reg(15); save_gp_reg(16); save_gp_reg(17); save_gp_reg(18);
28 save_gp_reg(19); save_gp_reg(20); save_gp_reg(21); save_gp_reg(22);
29 save_gp_reg(23); save_gp_reg(24); save_gp_reg(25); save_gp_reg(26);
30 save_gp_reg(27); save_gp_reg(28); save_gp_reg(29); save_gp_reg(30);
31 save_gp_reg(31);
32#undef save_gp_reg
33
Ralf Baechlee50c0a82005-05-31 11:49:19 +000034#ifdef CONFIG_32BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 err |= __put_user(regs->hi, &sc->sc_mdhi);
36 err |= __put_user(regs->lo, &sc->sc_mdlo);
Ralf Baechlee50c0a82005-05-31 11:49:19 +000037 if (cpu_has_dsp) {
38 err |= __put_user(mfhi1(), &sc->sc_hi1);
39 err |= __put_user(mflo1(), &sc->sc_lo1);
40 err |= __put_user(mfhi2(), &sc->sc_hi2);
41 err |= __put_user(mflo2(), &sc->sc_lo2);
42 err |= __put_user(mfhi3(), &sc->sc_hi3);
43 err |= __put_user(mflo3(), &sc->sc_lo3);
44 err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
45 }
46#endif
47#ifdef CONFIG_64BIT
48 err |= __put_user(regs->hi, &sc->sc_hi[0]);
49 err |= __put_user(regs->lo, &sc->sc_lo[0]);
50 if (cpu_has_dsp) {
51 err |= __put_user(mfhi1(), &sc->sc_hi[1]);
52 err |= __put_user(mflo1(), &sc->sc_lo[1]);
53 err |= __put_user(mfhi2(), &sc->sc_hi[2]);
54 err |= __put_user(mflo2(), &sc->sc_lo[2]);
55 err |= __put_user(mfhi3(), &sc->sc_hi[3]);
56 err |= __put_user(mflo3(), &sc->sc_lo[3]);
57 err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
58 }
59#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61 err |= __put_user(!!used_math(), &sc->sc_used_math);
62
63 if (!used_math())
64 goto out;
65
66 /*
67 * Save FPU state to signal context. Signal handler will "inherit"
68 * current FPU state.
69 */
70 preempt_disable();
71
72 if (!is_fpu_owner()) {
73 own_fpu();
74 restore_fp(current);
75 }
76 err |= save_fp_context(sc);
77
78 preempt_enable();
79
80out:
81 return err;
82}
83
84static inline int
85restore_sigcontext(struct pt_regs *regs, struct sigcontext *sc)
86{
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 unsigned int used_math;
Ralf Baechlee50c0a82005-05-31 11:49:19 +000088 unsigned long treg;
89 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91 /* Always make any pending restarted system calls return -EINTR */
92 current_thread_info()->restart_block.fn = do_no_restart_syscall;
93
94 err |= __get_user(regs->cp0_epc, &sc->sc_pc);
Ralf Baechlee50c0a82005-05-31 11:49:19 +000095#ifdef CONFIG_32BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 err |= __get_user(regs->hi, &sc->sc_mdhi);
97 err |= __get_user(regs->lo, &sc->sc_mdlo);
Ralf Baechlee50c0a82005-05-31 11:49:19 +000098 if (cpu_has_dsp) {
99 err |= __get_user(treg, &sc->sc_hi1); mthi1(treg);
100 err |= __get_user(treg, &sc->sc_lo1); mtlo1(treg);
101 err |= __get_user(treg, &sc->sc_hi2); mthi2(treg);
102 err |= __get_user(treg, &sc->sc_lo2); mtlo2(treg);
103 err |= __get_user(treg, &sc->sc_hi3); mthi3(treg);
104 err |= __get_user(treg, &sc->sc_lo3); mtlo3(treg);
105 err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
106 }
107#endif
108#ifdef CONFIG_64BIT
109 err |= __get_user(regs->hi, &sc->sc_hi[0]);
110 err |= __get_user(regs->lo, &sc->sc_lo[0]);
111 if (cpu_has_dsp) {
112 err |= __get_user(treg, &sc->sc_hi[1]); mthi1(treg);
113 err |= __get_user(treg, &sc->sc_lo[1]); mthi1(treg);
114 err |= __get_user(treg, &sc->sc_hi[2]); mthi2(treg);
115 err |= __get_user(treg, &sc->sc_lo[2]); mthi2(treg);
116 err |= __get_user(treg, &sc->sc_hi[3]); mthi3(treg);
117 err |= __get_user(treg, &sc->sc_lo[3]); mthi3(treg);
118 err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
119 }
120#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122#define restore_gp_reg(i) do { \
123 err |= __get_user(regs->regs[i], &sc->sc_regs[i]); \
124} while(0)
125 restore_gp_reg( 1); restore_gp_reg( 2); restore_gp_reg( 3);
126 restore_gp_reg( 4); restore_gp_reg( 5); restore_gp_reg( 6);
127 restore_gp_reg( 7); restore_gp_reg( 8); restore_gp_reg( 9);
128 restore_gp_reg(10); restore_gp_reg(11); restore_gp_reg(12);
129 restore_gp_reg(13); restore_gp_reg(14); restore_gp_reg(15);
130 restore_gp_reg(16); restore_gp_reg(17); restore_gp_reg(18);
131 restore_gp_reg(19); restore_gp_reg(20); restore_gp_reg(21);
132 restore_gp_reg(22); restore_gp_reg(23); restore_gp_reg(24);
133 restore_gp_reg(25); restore_gp_reg(26); restore_gp_reg(27);
134 restore_gp_reg(28); restore_gp_reg(29); restore_gp_reg(30);
135 restore_gp_reg(31);
136#undef restore_gp_reg
137
138 err |= __get_user(used_math, &sc->sc_used_math);
139 conditional_used_math(used_math);
140
141 preempt_disable();
142
143 if (used_math()) {
144 /* restore fpu context if we have used it before */
145 own_fpu();
146 err |= restore_fp_context(sc);
147 } else {
148 /* signal handler may have used FPU. Give it up. */
149 lose_fpu();
150 }
151
152 preempt_enable();
153
154 return err;
155}
156
157/*
158 * Determine which stack to use..
159 */
160static inline void *
161get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size)
162{
163 unsigned long sp, almask;
164
165 /* Default to using normal stack */
166 sp = regs->regs[29];
167
168 /*
169 * FPU emulator may have it's own trampoline active just
170 * above the user stack, 16-bytes before the next lowest
171 * 16 byte boundary. Try to avoid trashing it.
172 */
173 sp -= 32;
174
175 /* This is the X/Open sanctioned signal stack switching. */
176 if ((ka->sa.sa_flags & SA_ONSTACK) && (sas_ss_flags (sp) == 0))
177 sp = current->sas_ss_sp + current->sas_ss_size;
178
179 if (PLAT_TRAMPOLINE_STUFF_LINE)
180 almask = ~(PLAT_TRAMPOLINE_STUFF_LINE - 1);
181 else
182 almask = ALMASK;
183
184 return (void *)((sp - frame_size) & almask);
185}