xref: /openbmc/linux/arch/hexagon/kernel/signal.c (revision 2cf1c348)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Signal support for Hexagon processor
4  *
5  * Copyright (c) 2010-2012, The Linux Foundation. All rights reserved.
6  */
7 
8 #include <linux/linkage.h>
9 #include <linux/syscalls.h>
10 #include <linux/tracehook.h>
11 #include <linux/sched/task_stack.h>
12 
13 #include <asm/registers.h>
14 #include <asm/thread_info.h>
15 #include <asm/unistd.h>
16 #include <linux/uaccess.h>
17 #include <asm/ucontext.h>
18 #include <asm/cacheflush.h>
19 #include <asm/signal.h>
20 #include <asm/vdso.h>
21 
22 struct rt_sigframe {
23 	unsigned long tramp[2];
24 	struct siginfo info;
25 	struct ucontext uc;
26 };
27 
28 static void __user *get_sigframe(struct ksignal *ksig, struct pt_regs *regs,
29 			  size_t frame_size)
30 {
31 	unsigned long sp = sigsp(regs->r29, ksig);
32 
33 	return (void __user *)((sp - frame_size) & ~(sizeof(long long) - 1));
34 }
35 
36 static int setup_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
37 {
38 	unsigned long tmp;
39 	int err = 0;
40 
41 	err |= copy_to_user(&sc->sc_regs.r0, &regs->r00,
42 			    32*sizeof(unsigned long));
43 
44 	err |= __put_user(regs->sa0, &sc->sc_regs.sa0);
45 	err |= __put_user(regs->lc0, &sc->sc_regs.lc0);
46 	err |= __put_user(regs->sa1, &sc->sc_regs.sa1);
47 	err |= __put_user(regs->lc1, &sc->sc_regs.lc1);
48 	err |= __put_user(regs->m0, &sc->sc_regs.m0);
49 	err |= __put_user(regs->m1, &sc->sc_regs.m1);
50 	err |= __put_user(regs->usr, &sc->sc_regs.usr);
51 	err |= __put_user(regs->preds, &sc->sc_regs.p3_0);
52 	err |= __put_user(regs->gp, &sc->sc_regs.gp);
53 	err |= __put_user(regs->ugp, &sc->sc_regs.ugp);
54 #if CONFIG_HEXAGON_ARCH_VERSION >= 4
55 	err |= __put_user(regs->cs0, &sc->sc_regs.cs0);
56 	err |= __put_user(regs->cs1, &sc->sc_regs.cs1);
57 #endif
58 	tmp = pt_elr(regs); err |= __put_user(tmp, &sc->sc_regs.pc);
59 	tmp = pt_cause(regs); err |= __put_user(tmp, &sc->sc_regs.cause);
60 	tmp = pt_badva(regs); err |= __put_user(tmp, &sc->sc_regs.badva);
61 
62 	return err;
63 }
64 
65 static int restore_sigcontext(struct pt_regs *regs,
66 			      struct sigcontext __user *sc)
67 {
68 	unsigned long tmp;
69 	int err = 0;
70 
71 	err |= copy_from_user(&regs->r00, &sc->sc_regs.r0,
72 			      32 * sizeof(unsigned long));
73 
74 	err |= __get_user(regs->sa0, &sc->sc_regs.sa0);
75 	err |= __get_user(regs->lc0, &sc->sc_regs.lc0);
76 	err |= __get_user(regs->sa1, &sc->sc_regs.sa1);
77 	err |= __get_user(regs->lc1, &sc->sc_regs.lc1);
78 	err |= __get_user(regs->m0, &sc->sc_regs.m0);
79 	err |= __get_user(regs->m1, &sc->sc_regs.m1);
80 	err |= __get_user(regs->usr, &sc->sc_regs.usr);
81 	err |= __get_user(regs->preds, &sc->sc_regs.p3_0);
82 	err |= __get_user(regs->gp, &sc->sc_regs.gp);
83 	err |= __get_user(regs->ugp, &sc->sc_regs.ugp);
84 #if CONFIG_HEXAGON_ARCH_VERSION >= 4
85 	err |= __get_user(regs->cs0, &sc->sc_regs.cs0);
86 	err |= __get_user(regs->cs1, &sc->sc_regs.cs1);
87 #endif
88 	err |= __get_user(tmp, &sc->sc_regs.pc); pt_set_elr(regs, tmp);
89 
90 	return err;
91 }
92 
93 /*
94  * Setup signal stack frame with siginfo structure
95  */
96 static int setup_rt_frame(struct ksignal *ksig, sigset_t *set,
97 			  struct pt_regs *regs)
98 {
99 	int err = 0;
100 	struct rt_sigframe __user *frame;
101 	struct hexagon_vdso *vdso = current->mm->context.vdso;
102 
103 	frame = get_sigframe(ksig, regs, sizeof(struct rt_sigframe));
104 
105 	if (!access_ok(frame, sizeof(struct rt_sigframe)))
106 		return -EFAULT;
107 
108 	if (copy_siginfo_to_user(&frame->info, &ksig->info))
109 		return -EFAULT;
110 
111 	/* The on-stack signal trampoline is no longer executed;
112 	 * however, the libgcc signal frame unwinding code checks for
113 	 * the presence of these two numeric magic values.
114 	 */
115 	err |= __put_user(0x7800d166, &frame->tramp[0]);
116 	err |= __put_user(0x5400c004, &frame->tramp[1]);
117 	err |= setup_sigcontext(regs, &frame->uc.uc_mcontext);
118 	err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
119 	err |= __save_altstack(&frame->uc.uc_stack, user_stack_pointer(regs));
120 	if (err)
121 		return -EFAULT;
122 
123 	/* Load r0/r1 pair with signumber/siginfo pointer... */
124 	regs->r0100 = ((unsigned long long)((unsigned long)&frame->info) << 32)
125 		| (unsigned long long)ksig->sig;
126 	regs->r02 = (unsigned long) &frame->uc;
127 	regs->r31 = (unsigned long) vdso->rt_signal_trampoline;
128 	pt_psp(regs) = (unsigned long) frame;
129 	pt_set_elr(regs, (unsigned long)ksig->ka.sa.sa_handler);
130 
131 	return 0;
132 }
133 
134 /*
135  * Setup invocation of signal handler
136  */
137 static void handle_signal(struct ksignal *ksig, struct pt_regs *regs)
138 {
139 	int ret;
140 
141 	/*
142 	 * If we're handling a signal that aborted a system call,
143 	 * set up the error return value before adding the signal
144 	 * frame to the stack.
145 	 */
146 
147 	if (regs->syscall_nr >= 0) {
148 		switch (regs->r00) {
149 		case -ERESTART_RESTARTBLOCK:
150 		case -ERESTARTNOHAND:
151 			regs->r00 = -EINTR;
152 			break;
153 		case -ERESTARTSYS:
154 			if (!(ksig->ka.sa.sa_flags & SA_RESTART)) {
155 				regs->r00 = -EINTR;
156 				break;
157 			}
158 			fallthrough;
159 		case -ERESTARTNOINTR:
160 			regs->r06 = regs->syscall_nr;
161 			pt_set_elr(regs, pt_elr(regs) - 4);
162 			regs->r00 = regs->restart_r0;
163 			break;
164 		default:
165 			break;
166 		}
167 	}
168 
169 	/*
170 	 * Set up the stack frame; not doing the SA_SIGINFO thing.  We
171 	 * only set up the rt_frame flavor.
172 	 */
173 	/* If there was an error on setup, no signal was delivered. */
174 	ret = setup_rt_frame(ksig, sigmask_to_save(), regs);
175 
176 	signal_setup_done(ret, ksig, test_thread_flag(TIF_SINGLESTEP));
177 }
178 
179 /*
180  * Called from return-from-event code.
181  */
182 void do_signal(struct pt_regs *regs)
183 {
184 	struct ksignal ksig;
185 
186 	if (!user_mode(regs))
187 		return;
188 
189 	if (get_signal(&ksig)) {
190 		handle_signal(&ksig, regs);
191 		return;
192 	}
193 
194 	/*
195 	 * No (more) signals; if we came from a system call, handle the restart.
196 	 */
197 
198 	if (regs->syscall_nr >= 0) {
199 		switch (regs->r00) {
200 		case -ERESTARTNOHAND:
201 		case -ERESTARTSYS:
202 		case -ERESTARTNOINTR:
203 			regs->r06 = regs->syscall_nr;
204 			break;
205 		case -ERESTART_RESTARTBLOCK:
206 			regs->r06 = __NR_restart_syscall;
207 			break;
208 		default:
209 			goto no_restart;
210 		}
211 		pt_set_elr(regs, pt_elr(regs) - 4);
212 		regs->r00 = regs->restart_r0;
213 	}
214 
215 no_restart:
216 	/* If there's no signal to deliver, put the saved sigmask back */
217 	restore_saved_sigmask();
218 }
219 
220 /*
221  * Architecture-specific wrappers for signal-related system calls
222  */
223 
224 asmlinkage int sys_rt_sigreturn(void)
225 {
226 	struct pt_regs *regs = current_pt_regs();
227 	struct rt_sigframe __user *frame;
228 	sigset_t blocked;
229 
230 	/* Always make any pending restarted system calls return -EINTR */
231 	current->restart_block.fn = do_no_restart_syscall;
232 
233 	frame = (struct rt_sigframe __user *)pt_psp(regs);
234 	if (!access_ok(frame, sizeof(*frame)))
235 		goto badframe;
236 	if (__copy_from_user(&blocked, &frame->uc.uc_sigmask, sizeof(blocked)))
237 		goto badframe;
238 
239 	set_current_blocked(&blocked);
240 
241 	if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
242 		goto badframe;
243 
244 	/* Restore the user's stack as well */
245 	pt_psp(regs) = regs->r29;
246 
247 	regs->syscall_nr = -1;
248 
249 	if (restore_altstack(&frame->uc.uc_stack))
250 		goto badframe;
251 
252 	return regs->r00;
253 
254 badframe:
255 	force_sig(SIGSEGV);
256 	return 0;
257 }
258