signal.c 7.5 KB
Newer Older
R
Richard Kuo 已提交
1 2 3
/*
 * Signal support for Hexagon processor
 *
R
Richard Kuo 已提交
4
 * Copyright (c) 2010-2012, The Linux Foundation. All rights reserved.
R
Richard Kuo 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 and
 * only version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */

#include <linux/linkage.h>
#include <linux/syscalls.h>
#include <linux/tracehook.h>
#include <asm/registers.h>
#include <asm/thread_info.h>
#include <asm/unistd.h>
#include <asm/uaccess.h>
#include <asm/ucontext.h>
#include <asm/cacheflush.h>
#include <asm/signal.h>
#include <asm/vdso.h>

struct rt_sigframe {
	unsigned long tramp[2];
	struct siginfo info;
	struct ucontext uc;
};

static void __user *get_sigframe(struct k_sigaction *ka, struct pt_regs *regs,
			  size_t frame_size)
{
	unsigned long sp = regs->r29;

44 45 46 47
	/* check if we would overflow the alt stack */
	if (on_sig_stack(sp) && !likely(on_sig_stack(sp - frame_size)))
		return (void __user __force *)-1UL;

R
Richard Kuo 已提交
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
	/* Switch to signal stack if appropriate */
	if ((ka->sa.sa_flags & SA_ONSTACK) && (sas_ss_flags(sp) == 0))
		sp = current->sas_ss_sp + current->sas_ss_size;

	return (void __user *)((sp - frame_size) & ~(sizeof(long long) - 1));
}

static int setup_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc)
{
	unsigned long tmp;
	int err = 0;

	err |= copy_to_user(&sc->sc_regs.r0, &regs->r00,
			    32*sizeof(unsigned long));

	err |= __put_user(regs->sa0, &sc->sc_regs.sa0);
	err |= __put_user(regs->lc0, &sc->sc_regs.lc0);
	err |= __put_user(regs->sa1, &sc->sc_regs.sa1);
	err |= __put_user(regs->lc1, &sc->sc_regs.lc1);
	err |= __put_user(regs->m0, &sc->sc_regs.m0);
	err |= __put_user(regs->m1, &sc->sc_regs.m1);
	err |= __put_user(regs->usr, &sc->sc_regs.usr);
	err |= __put_user(regs->preds, &sc->sc_regs.p3_0);
	err |= __put_user(regs->gp, &sc->sc_regs.gp);
	err |= __put_user(regs->ugp, &sc->sc_regs.ugp);
73 74 75 76
#if CONFIG_HEXAGON_ARCH_VERSION >= 4
	err |= __put_user(regs->cs0, &sc->sc_regs.cs0);
	err |= __put_user(regs->cs1, &sc->sc_regs.cs1);
#endif
R
Richard Kuo 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
	tmp = pt_elr(regs); err |= __put_user(tmp, &sc->sc_regs.pc);
	tmp = pt_cause(regs); err |= __put_user(tmp, &sc->sc_regs.cause);
	tmp = pt_badva(regs); err |= __put_user(tmp, &sc->sc_regs.badva);

	return err;
}

static int restore_sigcontext(struct pt_regs *regs,
			      struct sigcontext __user *sc)
{
	unsigned long tmp;
	int err = 0;

	err |= copy_from_user(&regs->r00, &sc->sc_regs.r0,
			      32 * sizeof(unsigned long));

	err |= __get_user(regs->sa0, &sc->sc_regs.sa0);
	err |= __get_user(regs->lc0, &sc->sc_regs.lc0);
	err |= __get_user(regs->sa1, &sc->sc_regs.sa1);
	err |= __get_user(regs->lc1, &sc->sc_regs.lc1);
	err |= __get_user(regs->m0, &sc->sc_regs.m0);
	err |= __get_user(regs->m1, &sc->sc_regs.m1);
	err |= __get_user(regs->usr, &sc->sc_regs.usr);
	err |= __get_user(regs->preds, &sc->sc_regs.p3_0);
	err |= __get_user(regs->gp, &sc->sc_regs.gp);
	err |= __get_user(regs->ugp, &sc->sc_regs.ugp);
103 104 105 106
#if CONFIG_HEXAGON_ARCH_VERSION >= 4
	err |= __get_user(regs->cs0, &sc->sc_regs.cs0);
	err |= __get_user(regs->cs1, &sc->sc_regs.cs1);
#endif
R
Richard Kuo 已提交
107 108 109 110 111 112 113 114
	err |= __get_user(tmp, &sc->sc_regs.pc); pt_set_elr(regs, tmp);

	return err;
}

/*
 * Setup signal stack frame with siginfo structure
 */
115 116
static int setup_rt_frame(struct ksignal *ksig, sigset_t *set,
			  struct pt_regs *regs)
R
Richard Kuo 已提交
117 118 119 120 121
{
	int err = 0;
	struct rt_sigframe __user *frame;
	struct hexagon_vdso *vdso = current->mm->context.vdso;

122
	frame = get_sigframe(&ksig->ka, regs, sizeof(struct rt_sigframe));
R
Richard Kuo 已提交
123 124

	if (!access_ok(VERIFY_WRITE, frame, sizeof(struct rt_sigframe)))
125
		return -EFAULT;
R
Richard Kuo 已提交
126

127 128
	if (copy_siginfo_to_user(&frame->info, &ksig->info))
		return -EFAULT;
R
Richard Kuo 已提交
129 130 131 132 133 134 135 136 137

	/* The on-stack signal trampoline is no longer executed;
	 * however, the libgcc signal frame unwinding code checks for
	 * the presence of these two numeric magic values.
	 */
	err |= __put_user(0x7800d166, &frame->tramp[0]);
	err |= __put_user(0x5400c004, &frame->tramp[1]);
	err |= setup_sigcontext(regs, &frame->uc.uc_mcontext);
	err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
138
	err |= __save_altstack(&frame->uc.uc_stack, user_stack_pointer(regs));
R
Richard Kuo 已提交
139
	if (err)
140
		return -EFAULT;
R
Richard Kuo 已提交
141 142 143

	/* Load r0/r1 pair with signumber/siginfo pointer... */
	regs->r0100 = ((unsigned long long)((unsigned long)&frame->info) << 32)
144
		| (unsigned long long)ksig->sig;
R
Richard Kuo 已提交
145 146 147
	regs->r02 = (unsigned long) &frame->uc;
	regs->r31 = (unsigned long) vdso->rt_signal_trampoline;
	pt_psp(regs) = (unsigned long) frame;
148
	pt_set_elr(regs, (unsigned long)ksig->ka.sa.sa_handler);
R
Richard Kuo 已提交
149 150 151 152 153 154 155

	return 0;
}

/*
 * Setup invocation of signal handler
 */
156
static void handle_signal(struct ksignal *ksig, struct pt_regs *regs)
R
Richard Kuo 已提交
157
{
158 159
	int ret;

R
Richard Kuo 已提交
160 161 162 163 164 165 166 167 168 169 170 171 172
	/*
	 * If we're handling a signal that aborted a system call,
	 * set up the error return value before adding the signal
	 * frame to the stack.
	 */

	if (regs->syscall_nr >= 0) {
		switch (regs->r00) {
		case -ERESTART_RESTARTBLOCK:
		case -ERESTARTNOHAND:
			regs->r00 = -EINTR;
			break;
		case -ERESTARTSYS:
173
			if (!(ksig->ka.sa.sa_flags & SA_RESTART)) {
R
Richard Kuo 已提交
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
				regs->r00 = -EINTR;
				break;
			}
			/* Fall through */
		case -ERESTARTNOINTR:
			regs->r06 = regs->syscall_nr;
			pt_set_elr(regs, pt_elr(regs) - 4);
			regs->r00 = regs->restart_r0;
			break;
		default:
			break;
		}
	}

	/*
	 * Set up the stack frame; not doing the SA_SIGINFO thing.  We
	 * only set up the rt_frame flavor.
	 */
	/* If there was an error on setup, no signal was delivered. */
193
	ret = setup_rt_frame(ksig, sigmask_to_save(), regs);
R
Richard Kuo 已提交
194

195
	signal_setup_done(ret, ksig, test_thread_flag(TIF_SINGLESTEP));
R
Richard Kuo 已提交
196 197 198 199 200
}

/*
 * Called from return-from-event code.
 */
201
void do_signal(struct pt_regs *regs)
R
Richard Kuo 已提交
202
{
203
	struct ksignal ksig;
R
Richard Kuo 已提交
204 205 206 207

	if (!user_mode(regs))
		return;

208 209
	if (get_signal(&ksig)) {
		handle_signal(&ksig, regs);
R
Richard Kuo 已提交
210 211 212 213
		return;
	}

	/*
214
	 * No (more) signals; if we came from a system call, handle the restart.
R
Richard Kuo 已提交
215
	 */
216

R
Richard Kuo 已提交
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
	if (regs->syscall_nr >= 0) {
		switch (regs->r00) {
		case -ERESTARTNOHAND:
		case -ERESTARTSYS:
		case -ERESTARTNOINTR:
			regs->r06 = regs->syscall_nr;
			break;
		case -ERESTART_RESTARTBLOCK:
			regs->r06 = __NR_restart_syscall;
			break;
		default:
			goto no_restart;
		}
		pt_set_elr(regs, pt_elr(regs) - 4);
		regs->r00 = regs->restart_r0;
	}

no_restart:
	/* If there's no signal to deliver, put the saved sigmask back */
A
Al Viro 已提交
236
	restore_saved_sigmask();
R
Richard Kuo 已提交
237 238 239 240 241 242 243 244
}

/*
 * Architecture-specific wrappers for signal-related system calls
 */

asmlinkage int sys_rt_sigreturn(void)
{
245
	struct pt_regs *regs = current_pt_regs();
R
Richard Kuo 已提交
246 247 248
	struct rt_sigframe __user *frame;
	sigset_t blocked;

249 250 251
	/* Always make any pending restarted system calls return -EINTR */
	current_thread_info()->restart_block.fn = do_no_restart_syscall;

R
Richard Kuo 已提交
252 253 254 255 256 257
	frame = (struct rt_sigframe __user *)pt_psp(regs);
	if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
		goto badframe;
	if (__copy_from_user(&blocked, &frame->uc.uc_sigmask, sizeof(blocked)))
		goto badframe;

258
	set_current_blocked(&blocked);
R
Richard Kuo 已提交
259 260 261 262 263 264 265

	if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
		goto badframe;

	/* Restore the user's stack as well */
	pt_psp(regs) = regs->r29;

266
	regs->syscall_nr = -1;
R
Richard Kuo 已提交
267

A
Al Viro 已提交
268
	if (restore_altstack(&frame->uc.uc_stack))
R
Richard Kuo 已提交
269 270
		goto badframe;

271
	return regs->r00;
R
Richard Kuo 已提交
272 273 274 275 276

badframe:
	force_sig(SIGSEGV, current);
	return 0;
}