blob: bb3c631b808eeba2218d03657b107b9814f80259 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070012static inline int
Atsushi Nemoto9bbf28a32006-02-01 01:41:09 +090013setup_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -070014{
15 int err = 0;
Franck Bui-Huua007b1f2007-02-05 15:24:19 +010016 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
18 err |= __put_user(regs->cp0_epc, &sc->sc_pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
Franck Bui-Huua007b1f2007-02-05 15:24:19 +010020 err |= __put_user(0, &sc->sc_regs[0]);
21 for (i = 1; i < 32; i++)
22 err |= __put_user(regs->regs[i], &sc->sc_regs[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24 err |= __put_user(regs->hi, &sc->sc_mdhi);
25 err |= __put_user(regs->lo, &sc->sc_mdlo);
Ralf Baechlee50c0a82005-05-31 11:49:19 +000026 if (cpu_has_dsp) {
27 err |= __put_user(mfhi1(), &sc->sc_hi1);
28 err |= __put_user(mflo1(), &sc->sc_lo1);
29 err |= __put_user(mfhi2(), &sc->sc_hi2);
30 err |= __put_user(mflo2(), &sc->sc_lo2);
31 err |= __put_user(mfhi3(), &sc->sc_hi3);
32 err |= __put_user(mflo3(), &sc->sc_lo3);
33 err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
34 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36 err |= __put_user(!!used_math(), &sc->sc_used_math);
37
Franck Bui-Huua007b1f2007-02-05 15:24:19 +010038 if (used_math()) {
39 /*
40 * Save FPU state to signal context. Signal handler
41 * will "inherit" current FPU state.
42 */
43 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Franck Bui-Huua007b1f2007-02-05 15:24:19 +010045 if (!is_fpu_owner()) {
46 own_fpu();
47 restore_fp(current);
48 }
49 err |= save_fp_context(sc);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Franck Bui-Huua007b1f2007-02-05 15:24:19 +010051 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 return err;
54}
55
56static inline int
Atsushi Nemoto9bbf28a32006-02-01 01:41:09 +090057restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058{
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 unsigned int used_math;
Ralf Baechlee50c0a82005-05-31 11:49:19 +000060 unsigned long treg;
61 int err = 0;
Franck Bui-Huua007b1f2007-02-05 15:24:19 +010062 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64 /* Always make any pending restarted system calls return -EINTR */
65 current_thread_info()->restart_block.fn = do_no_restart_syscall;
66
67 err |= __get_user(regs->cp0_epc, &sc->sc_pc);
68 err |= __get_user(regs->hi, &sc->sc_mdhi);
69 err |= __get_user(regs->lo, &sc->sc_mdlo);
Ralf Baechlee50c0a82005-05-31 11:49:19 +000070 if (cpu_has_dsp) {
71 err |= __get_user(treg, &sc->sc_hi1); mthi1(treg);
72 err |= __get_user(treg, &sc->sc_lo1); mtlo1(treg);
73 err |= __get_user(treg, &sc->sc_hi2); mthi2(treg);
74 err |= __get_user(treg, &sc->sc_lo2); mtlo2(treg);
75 err |= __get_user(treg, &sc->sc_hi3); mthi3(treg);
76 err |= __get_user(treg, &sc->sc_lo3); mtlo3(treg);
77 err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
78 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Franck Bui-Huua007b1f2007-02-05 15:24:19 +010080 for (i = 1; i < 32; i++)
81 err |= __get_user(regs->regs[i], &sc->sc_regs[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83 err |= __get_user(used_math, &sc->sc_used_math);
84 conditional_used_math(used_math);
85
86 preempt_disable();
87
88 if (used_math()) {
89 /* restore fpu context if we have used it before */
90 own_fpu();
91 err |= restore_fp_context(sc);
92 } else {
93 /* signal handler may have used FPU. Give it up. */
94 lose_fpu();
95 }
96
97 preempt_enable();
98
99 return err;
100}
101
102/*
103 * Determine which stack to use..
104 */
Atsushi Nemoto9bbf28a32006-02-01 01:41:09 +0900105static inline void __user *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, size_t frame_size)
107{
Ralf Baechle02416dc2005-06-15 13:00:12 +0000108 unsigned long sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110 /* Default to using normal stack */
111 sp = regs->regs[29];
112
113 /*
Ralf Baechlea3dddd52006-03-11 08:18:41 +0000114 * FPU emulator may have it's own trampoline active just
115 * above the user stack, 16-bytes before the next lowest
116 * 16 byte boundary. Try to avoid trashing it.
117 */
118 sp -= 32;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120 /* This is the X/Open sanctioned signal stack switching. */
121 if ((ka->sa.sa_flags & SA_ONSTACK) && (sas_ss_flags (sp) == 0))
122 sp = current->sas_ss_sp + current->sas_ss_size;
123
Thomas Koeller387a1542006-02-10 17:36:27 +0100124 return (void __user *)((sp - frame_size) & (ICACHE_REFILLS_WORKAROUND_WAR ? ~(cpu_icache_line_size()-1) : ALMASK));
Ralf Baechle02416dc2005-06-15 13:00:12 +0000125}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Ralf Baechle02416dc2005-06-15 13:00:12 +0000127static inline int install_sigtramp(unsigned int __user *tramp,
128 unsigned int syscall)
129{
130 int err;
131
132 /*
133 * Set up the return code ...
134 *
135 * li v0, __NR__foo_sigreturn
136 * syscall
137 */
138
139 err = __put_user(0x24020000 + syscall, tramp + 0);
140 err |= __put_user(0x0000000c , tramp + 1);
141 if (ICACHE_REFILLS_WORKAROUND_WAR) {
142 err |= __put_user(0, tramp + 2);
143 err |= __put_user(0, tramp + 3);
144 err |= __put_user(0, tramp + 4);
145 err |= __put_user(0, tramp + 5);
146 err |= __put_user(0, tramp + 6);
147 err |= __put_user(0, tramp + 7);
148 }
149 flush_cache_sigtramp((unsigned long) tramp);
150
151 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152}