cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

signal.c (8733B)


      1/*
      2 * Copyright (C) 2013-2014 Altera Corporation
      3 * Copyright (C) 2011-2012 Tobias Klauser <tklauser@distanz.ch>
      4 * Copyright (C) 2004 Microtronix Datacom Ltd
      5 * Copyright (C) 1991, 1992 Linus Torvalds
      6 *
      7 * This file is subject to the terms and conditions of the GNU General Public
      8 * License.  See the file COPYING in the main directory of this archive
      9 * for more details.
     10 */
     11
     12#include <linux/signal.h>
     13#include <linux/errno.h>
     14#include <linux/ptrace.h>
     15#include <linux/uaccess.h>
     16#include <linux/unistd.h>
     17#include <linux/personality.h>
     18#include <linux/resume_user_mode.h>
     19
     20#include <asm/ucontext.h>
     21#include <asm/cacheflush.h>
     22
     23#define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
     24
     25/*
     26 * Do a signal return; undo the signal stack.
     27 *
     28 * Keep the return code on the stack quadword aligned!
     29 * That makes the cache flush below easier.
     30 */
     31
     32struct rt_sigframe {
     33	struct siginfo info;
     34	struct ucontext uc;
     35};
     36
     37static inline int rt_restore_ucontext(struct pt_regs *regs,
     38					struct switch_stack *sw,
     39					struct ucontext __user *uc, int *pr2)
     40{
     41	int temp;
     42	unsigned long __user *gregs = uc->uc_mcontext.gregs;
     43	int err;
     44
     45	/* Always make any pending restarted system calls return -EINTR */
     46	current->restart_block.fn = do_no_restart_syscall;
     47
     48	err = __get_user(temp, &uc->uc_mcontext.version);
     49	if (temp != MCONTEXT_VERSION)
     50		goto badframe;
     51	/* restore passed registers */
     52	err |= __get_user(regs->r1, &gregs[0]);
     53	err |= __get_user(regs->r2, &gregs[1]);
     54	err |= __get_user(regs->r3, &gregs[2]);
     55	err |= __get_user(regs->r4, &gregs[3]);
     56	err |= __get_user(regs->r5, &gregs[4]);
     57	err |= __get_user(regs->r6, &gregs[5]);
     58	err |= __get_user(regs->r7, &gregs[6]);
     59	err |= __get_user(regs->r8, &gregs[7]);
     60	err |= __get_user(regs->r9, &gregs[8]);
     61	err |= __get_user(regs->r10, &gregs[9]);
     62	err |= __get_user(regs->r11, &gregs[10]);
     63	err |= __get_user(regs->r12, &gregs[11]);
     64	err |= __get_user(regs->r13, &gregs[12]);
     65	err |= __get_user(regs->r14, &gregs[13]);
     66	err |= __get_user(regs->r15, &gregs[14]);
     67	err |= __get_user(sw->r16, &gregs[15]);
     68	err |= __get_user(sw->r17, &gregs[16]);
     69	err |= __get_user(sw->r18, &gregs[17]);
     70	err |= __get_user(sw->r19, &gregs[18]);
     71	err |= __get_user(sw->r20, &gregs[19]);
     72	err |= __get_user(sw->r21, &gregs[20]);
     73	err |= __get_user(sw->r22, &gregs[21]);
     74	err |= __get_user(sw->r23, &gregs[22]);
     75	/* gregs[23] is handled below */
     76	err |= __get_user(sw->fp, &gregs[24]);  /* Verify, should this be
     77							settable */
     78	err |= __get_user(sw->gp, &gregs[25]);  /* Verify, should this be
     79							settable */
     80
     81	err |= __get_user(temp, &gregs[26]);  /* Not really necessary no user
     82							settable bits */
     83	err |= __get_user(regs->ea, &gregs[27]);
     84
     85	err |= __get_user(regs->ra, &gregs[23]);
     86	err |= __get_user(regs->sp, &gregs[28]);
     87
     88	regs->orig_r2 = -1;		/* disable syscall checks */
     89
     90	err |= restore_altstack(&uc->uc_stack);
     91	if (err)
     92		goto badframe;
     93
     94	*pr2 = regs->r2;
     95	return err;
     96
     97badframe:
     98	return 1;
     99}
    100
    101asmlinkage int do_rt_sigreturn(struct switch_stack *sw)
    102{
    103	struct pt_regs *regs = (struct pt_regs *)(sw + 1);
    104	/* Verify, can we follow the stack back */
    105	struct rt_sigframe __user *frame;
    106	sigset_t set;
    107	int rval;
    108
    109	frame = (struct rt_sigframe __user *) regs->sp;
    110	if (!access_ok(frame, sizeof(*frame)))
    111		goto badframe;
    112
    113	if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
    114		goto badframe;
    115
    116	set_current_blocked(&set);
    117
    118	if (rt_restore_ucontext(regs, sw, &frame->uc, &rval))
    119		goto badframe;
    120
    121	return rval;
    122
    123badframe:
    124	force_sig(SIGSEGV);
    125	return 0;
    126}
    127
    128static inline int rt_setup_ucontext(struct ucontext __user *uc, struct pt_regs *regs)
    129{
    130	struct switch_stack *sw = (struct switch_stack *)regs - 1;
    131	unsigned long __user *gregs = uc->uc_mcontext.gregs;
    132	int err = 0;
    133
    134	err |= __put_user(MCONTEXT_VERSION, &uc->uc_mcontext.version);
    135	err |= __put_user(regs->r1, &gregs[0]);
    136	err |= __put_user(regs->r2, &gregs[1]);
    137	err |= __put_user(regs->r3, &gregs[2]);
    138	err |= __put_user(regs->r4, &gregs[3]);
    139	err |= __put_user(regs->r5, &gregs[4]);
    140	err |= __put_user(regs->r6, &gregs[5]);
    141	err |= __put_user(regs->r7, &gregs[6]);
    142	err |= __put_user(regs->r8, &gregs[7]);
    143	err |= __put_user(regs->r9, &gregs[8]);
    144	err |= __put_user(regs->r10, &gregs[9]);
    145	err |= __put_user(regs->r11, &gregs[10]);
    146	err |= __put_user(regs->r12, &gregs[11]);
    147	err |= __put_user(regs->r13, &gregs[12]);
    148	err |= __put_user(regs->r14, &gregs[13]);
    149	err |= __put_user(regs->r15, &gregs[14]);
    150	err |= __put_user(sw->r16, &gregs[15]);
    151	err |= __put_user(sw->r17, &gregs[16]);
    152	err |= __put_user(sw->r18, &gregs[17]);
    153	err |= __put_user(sw->r19, &gregs[18]);
    154	err |= __put_user(sw->r20, &gregs[19]);
    155	err |= __put_user(sw->r21, &gregs[20]);
    156	err |= __put_user(sw->r22, &gregs[21]);
    157	err |= __put_user(sw->r23, &gregs[22]);
    158	err |= __put_user(regs->ra, &gregs[23]);
    159	err |= __put_user(sw->fp, &gregs[24]);
    160	err |= __put_user(sw->gp, &gregs[25]);
    161	err |= __put_user(regs->ea, &gregs[27]);
    162	err |= __put_user(regs->sp, &gregs[28]);
    163	return err;
    164}
    165
    166static inline void __user *get_sigframe(struct ksignal *ksig,
    167					struct pt_regs *regs,
    168					size_t frame_size)
    169{
    170	unsigned long usp;
    171
    172	/* Default to using normal stack.  */
    173	usp = regs->sp;
    174
    175	/* This is the X/Open sanctioned signal stack switching.  */
    176	usp = sigsp(usp, ksig);
    177
    178	/* Verify, is it 32 or 64 bit aligned */
    179	return (void __user *)((usp - frame_size) & -8UL);
    180}
    181
    182static int setup_rt_frame(struct ksignal *ksig, sigset_t *set,
    183			  struct pt_regs *regs)
    184{
    185	struct rt_sigframe __user *frame;
    186	int err = 0;
    187
    188	frame = get_sigframe(ksig, regs, sizeof(*frame));
    189
    190	if (ksig->ka.sa.sa_flags & SA_SIGINFO)
    191		err |= copy_siginfo_to_user(&frame->info, &ksig->info);
    192
    193	/* Create the ucontext.  */
    194	err |= __put_user(0, &frame->uc.uc_flags);
    195	err |= __put_user(0, &frame->uc.uc_link);
    196	err |= __save_altstack(&frame->uc.uc_stack, regs->sp);
    197	err |= rt_setup_ucontext(&frame->uc, regs);
    198	err |= copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
    199
    200	if (err)
    201		goto give_sigsegv;
    202
    203	/* Set up to return from userspace; jump to fixed address sigreturn
    204	   trampoline on kuser page.  */
    205	regs->ra = (unsigned long) (0x1044);
    206
    207	/* Set up registers for signal handler */
    208	regs->sp = (unsigned long) frame;
    209	regs->r4 = (unsigned long) ksig->sig;
    210	regs->r5 = (unsigned long) &frame->info;
    211	regs->r6 = (unsigned long) &frame->uc;
    212	regs->ea = (unsigned long) ksig->ka.sa.sa_handler;
    213	return 0;
    214
    215give_sigsegv:
    216	force_sigsegv(ksig->sig);
    217	return -EFAULT;
    218}
    219
    220/*
    221 * OK, we're invoking a handler
    222 */
    223static void handle_signal(struct ksignal *ksig, struct pt_regs *regs)
    224{
    225	int ret;
    226	sigset_t *oldset = sigmask_to_save();
    227
    228	/* set up the stack frame */
    229	ret = setup_rt_frame(ksig, oldset, regs);
    230
    231	signal_setup_done(ret, ksig, 0);
    232}
    233
    234static int do_signal(struct pt_regs *regs)
    235{
    236	unsigned int retval = 0, continue_addr = 0, restart_addr = 0;
    237	int restart = 0;
    238	struct ksignal ksig;
    239
    240	current->thread.kregs = regs;
    241
    242	/*
    243	 * If we were from a system call, check for system call restarting...
    244	 */
    245	if (regs->orig_r2 >= 0) {
    246		continue_addr = regs->ea;
    247		restart_addr = continue_addr - 4;
    248		retval = regs->r2;
    249
    250		/*
    251		 * Prepare for system call restart. We do this here so that a
    252		 * debugger will see the already changed PC.
    253		 */
    254		switch (retval) {
    255		case ERESTART_RESTARTBLOCK:
    256			restart = -2;
    257			fallthrough;
    258		case ERESTARTNOHAND:
    259		case ERESTARTSYS:
    260		case ERESTARTNOINTR:
    261			restart++;
    262			regs->r2 = regs->orig_r2;
    263			regs->r7 = regs->orig_r7;
    264			regs->ea = restart_addr;
    265			break;
    266		}
    267	}
    268
    269	if (get_signal(&ksig)) {
    270		/* handler */
    271		if (unlikely(restart && regs->ea == restart_addr)) {
    272			if (retval == ERESTARTNOHAND ||
    273			    retval == ERESTART_RESTARTBLOCK ||
    274			     (retval == ERESTARTSYS
    275				&& !(ksig.ka.sa.sa_flags & SA_RESTART))) {
    276				regs->r2 = EINTR;
    277				regs->r7 = 1;
    278				regs->ea = continue_addr;
    279			}
    280		}
    281		handle_signal(&ksig, regs);
    282		return 0;
    283	}
    284
    285	/*
    286	 * No handler present
    287	 */
    288	if (unlikely(restart) && regs->ea == restart_addr) {
    289		regs->ea = continue_addr;
    290		regs->r2 = __NR_restart_syscall;
    291	}
    292
    293	/*
    294	* If there's no signal to deliver, we just put the saved sigmask back.
    295	*/
    296	restore_saved_sigmask();
    297
    298	return restart;
    299}
    300
    301asmlinkage int do_notify_resume(struct pt_regs *regs)
    302{
    303	/*
    304	 * We want the common case to go fast, which is why we may in certain
    305	 * cases get here from kernel mode. Just return without doing anything
    306	 * if so.
    307	 */
    308	if (!user_mode(regs))
    309		return 0;
    310
    311	if (test_thread_flag(TIF_SIGPENDING) ||
    312	    test_thread_flag(TIF_NOTIFY_SIGNAL)) {
    313		int restart = do_signal(regs);
    314
    315		if (unlikely(restart)) {
    316			/*
    317			 * Restart without handlers.
    318			 * Deal with it without leaving
    319			 * the kernel space.
    320			 */
    321			return restart;
    322		}
    323	} else if (test_thread_flag(TIF_NOTIFY_RESUME))
    324		resume_user_mode_work(regs);
    325
    326	return 0;
    327}