⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 signal.c

📁 linux-2.6.15.6
💻 C
📖 第 1 页 / 共 2 页
字号:
 * Check whether the register-backing store is already on the signal stack. */static inline intrbs_on_sig_stack (unsigned long bsp){	return (bsp - current->sas_ss_sp < current->sas_ss_size);}static longforce_sigsegv_info (int sig, void __user *addr){	unsigned long flags;	struct siginfo si;	if (sig == SIGSEGV) {		/*		 * Acquiring siglock around the sa_handler-update is almost		 * certainly overkill, but this isn't a		 * performance-critical path and I'd rather play it safe		 * here than having to debug a nasty race if and when		 * something changes in kernel/signal.c that would make it		 * no longer safe to modify sa_handler without holding the		 * lock.		 */		spin_lock_irqsave(&current->sighand->siglock, flags);		current->sighand->action[sig - 1].sa.sa_handler = SIG_DFL;		spin_unlock_irqrestore(&current->sighand->siglock, flags);	}	si.si_signo = SIGSEGV;	si.si_errno = 0;	si.si_code = SI_KERNEL;	si.si_pid = current->pid;	si.si_uid = current->uid;	si.si_addr = addr;	force_sig_info(SIGSEGV, &si, current);	return 0;}static longsetup_frame (int sig, struct k_sigaction *ka, siginfo_t *info, sigset_t *set,	     struct sigscratch *scr){	extern char __kernel_sigtramp[];	unsigned long tramp_addr, new_rbs = 0, new_sp;	struct sigframe __user *frame;	long err;	new_sp = scr->pt.r12;	tramp_addr = (unsigned long) __kernel_sigtramp;	if ((ka->sa.sa_flags & SA_ONSTACK) && sas_ss_flags(new_sp) == 0) {		new_sp = current->sas_ss_sp + current->sas_ss_size;		/*		 * We need to check for the register stack being on the signal stack		 * separately, because it's switched separately (memory stack is switched		 * in the kernel, register stack is switched in the signal trampoline).		 */		if (!rbs_on_sig_stack(scr->pt.ar_bspstore))			new_rbs = (current->sas_ss_sp + sizeof(long) - 1) & ~(sizeof(long) - 1);	}	frame = (void __user *) ((new_sp - sizeof(*frame)) & -STACK_ALIGN);	if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))		return force_sigsegv_info(sig, frame);	err  = __put_user(sig, &frame->arg0);	err |= __put_user(&frame->info, &frame->arg1);	err |= __put_user(&frame->sc, &frame->arg2);	err |= __put_user(new_rbs, &frame->sc.sc_rbs_base);	err |= __put_user(0, &frame->sc.sc_loadrs);	/* initialize to zero */	err |= __put_user(ka->sa.sa_handler, &frame->handler);	err |= copy_siginfo_to_user(&frame->info, info);	err |= __put_user(current->sas_ss_sp, &frame->sc.sc_stack.ss_sp);	err |= __put_user(current->sas_ss_size, &frame->sc.sc_stack.ss_size);	err |= __put_user(sas_ss_flags(scr->pt.r12), &frame->sc.sc_stack.ss_flags);	err |= setup_sigcontext(&frame->sc, set, scr);	if (unlikely(err))		return force_sigsegv_info(sig, frame);	scr->pt.r12 = (unsigned long) frame - 16;	/* new stack pointer */	scr->pt.ar_fpsr = FPSR_DEFAULT;			/* reset fpsr for signal handler */	scr->pt.cr_iip = tramp_addr;	ia64_psr(&scr->pt)->ri = 0;			/* start executing in first slot */	ia64_psr(&scr->pt)->be = 0;			/* force little-endian byte-order */	/*	 * Force the interruption function mask to zero.  This has no effect when a	 * system-call got interrupted by a signal (since, in that case, scr->pt_cr_ifs is	 * ignored), but it has the desirable effect of making it possible to deliver a	 * signal with an incomplete register frame (which happens when a mandatory RSE	 * load faults).  Furthermore, it has no negative effect on the getting the user's	 * dirty partition preserved, because that's governed by scr->pt.loadrs.	 */	scr->pt.cr_ifs = (1UL << 63);	/*	 * Note: this affects only the NaT bits of the scratch regs (the ones saved in	 * pt_regs), which is exactly what we want.	 */	scr->scratch_unat = 0; /* ensure NaT bits of r12 is clear */#if DEBUG_SIG	printk("SIG deliver (%s:%d): sig=%d sp=%lx ip=%lx handler=%p\n",	       current->comm, current->pid, sig, scr->pt.r12, frame->sc.sc_ip, frame->handler);#endif	return 1;}static longhandle_signal (unsigned long sig, struct k_sigaction *ka, siginfo_t *info, sigset_t *oldset,	       struct sigscratch *scr){	if (IS_IA32_PROCESS(&scr->pt)) {		/* send signal to IA-32 process */		if (!ia32_setup_frame1(sig, ka, info, oldset, &scr->pt))			return 0;	} else		/* send signal to IA-64 process */		if (!setup_frame(sig, ka, info, oldset, scr))			return 0;	spin_lock_irq(&current->sighand->siglock);	sigorsets(&current->blocked, &current->blocked, &ka->sa.sa_mask);	if (!(ka->sa.sa_flags & SA_NODEFER))		sigaddset(&current->blocked, sig);	recalc_sigpending();	spin_unlock_irq(&current->sighand->siglock);	return 1;}/* * Note that `init' is a special process: it doesn't get signals it doesn't want to * handle.  Thus you cannot kill init even with a SIGKILL even by mistake. */longia64_do_signal (sigset_t *oldset, struct sigscratch *scr, long in_syscall){	struct k_sigaction ka;	siginfo_t info;	long restart = in_syscall;	long errno = scr->pt.r8;#	define ERR_CODE(c)	(IS_IA32_PROCESS(&scr->pt) ? -(c) : (c))	/*	 * In the ia64_leave_kernel code path, we want the common case to go fast, which	 * is why we may in certain cases get here from kernel mode. Just return without	 * doing anything if so.	 */	if (!user_mode(&scr->pt))		return 0;	if (!oldset)		oldset = &current->blocked;	/*	 * This only loops in the rare cases of handle_signal() failing, in which case we	 * need to push through a forced SIGSEGV.	 */	while (1) {		int signr = get_signal_to_deliver(&info, &ka, &scr->pt, NULL);		/*		 * get_signal_to_deliver() may have run a debugger (via notify_parent())		 * and the debugger may have modified the state (e.g., to arrange for an		 * inferior call), thus it's important to check for restarting _after_		 * get_signal_to_deliver().		 */		if (IS_IA32_PROCESS(&scr->pt)) {			if (in_syscall) {				if (errno >= 0)					restart = 0;				else					errno = -errno;			}		} else if ((long) scr->pt.r10 != -1)			/*			 * A system calls has to be restarted only if one of the error codes			 * ERESTARTNOHAND, ERESTARTSYS, or ERESTARTNOINTR is returned.  If r10			 * isn't -1 then r8 doesn't hold an error code and we don't need to			 * restart the syscall, so we can clear the "restart" flag here.			 */			restart = 0;		if (signr <= 0)			break;		if (unlikely(restart)) {			switch (errno) {			      case ERESTART_RESTARTBLOCK:			      case ERESTARTNOHAND:				scr->pt.r8 = ERR_CODE(EINTR);				/* note: scr->pt.r10 is already -1 */				break;			      case ERESTARTSYS:				if ((ka.sa.sa_flags & SA_RESTART) == 0) {					scr->pt.r8 = ERR_CODE(EINTR);					/* note: scr->pt.r10 is already -1 */					break;				}			      case ERESTARTNOINTR:				if (IS_IA32_PROCESS(&scr->pt)) {					scr->pt.r8 = scr->pt.r1;					scr->pt.cr_iip -= 2;				} else					ia64_decrement_ip(&scr->pt);				restart = 0; /* don't restart twice if handle_signal() fails... */			}		}		/*		 * Whee!  Actually deliver the signal.  If the delivery failed, we need to		 * continue to iterate in this loop so we can deliver the SIGSEGV...		 */		if (handle_signal(signr, &ka, &info, oldset, scr))			return 1;	}	/* Did we come from a system call? */	if (restart) {		/* Restart the system call - no handlers present */		if (errno == ERESTARTNOHAND || errno == ERESTARTSYS || errno == ERESTARTNOINTR		    || errno == ERESTART_RESTARTBLOCK)		{			if (IS_IA32_PROCESS(&scr->pt)) {				scr->pt.r8 = scr->pt.r1;				scr->pt.cr_iip -= 2;				if (errno == ERESTART_RESTARTBLOCK)					scr->pt.r8 = 0;	/* x86 version of __NR_restart_syscall */			} else {				/*				 * Note: the syscall number is in r15 which is saved in				 * pt_regs so all we need to do here is adjust ip so that				 * the "break" instruction gets re-executed.				 */				ia64_decrement_ip(&scr->pt);				if (errno == ERESTART_RESTARTBLOCK)					scr->pt.r15 = __NR_restart_syscall;			}		}	}	return 0;}/* Set a delayed signal that was detected in MCA/INIT/NMI/PMI context where it * could not be delivered.  It is important that the target process is not * allowed to do any more work in user space.  Possible cases for the target * process: * * - It is sleeping and will wake up soon.  Store the data in the current task, *   the signal will be sent when the current task returns from the next *   interrupt. * * - It is running in user context.  Store the data in the current task, the *   signal will be sent when the current task returns from the next interrupt. * * - It is running in kernel context on this or another cpu and will return to *   user context.  Store the data in the target task, the signal will be sent *   to itself when the target task returns to user space. * * - It is running in kernel context on this cpu and will sleep before *   returning to user context.  Because this is also the current task, the *   signal will not get delivered and the task could sleep indefinitely. *   Store the data in the idle task for this cpu, the signal will be sent *   after the idle task processes its next interrupt. * * To cover all cases, store the data in the target task, the current task and * the idle task on this cpu.  Whatever happens, the signal will be delivered * to the target task before it can do any useful user space work.  Multiple * deliveries have no unwanted side effects. * * Note: This code is executed in MCA/INIT/NMI/PMI context, with interrupts * disabled.  It must not take any locks nor use kernel structures or services * that require locks. *//* To ensure that we get the right pid, check its start time.  To avoid extra * include files in thread_info.h, convert the task start_time to unsigned long, * giving us a cycle time of > 580 years. */static inline unsigned longstart_time_ul(const struct task_struct *t){	return t->start_time.tv_sec * NSEC_PER_SEC + t->start_time.tv_nsec;}voidset_sigdelayed(pid_t pid, int signo, int code, void __user *addr){	struct task_struct *t;	unsigned long start_time =  0;	int i;	for (i = 1; i <= 3; ++i) {		switch (i) {		case 1:			t = find_task_by_pid(pid);			if (t)				start_time = start_time_ul(t);			break;		case 2:			t = current;			break;		default:			t = idle_task(smp_processor_id());			break;		}		if (!t)			return;		t->thread_info->sigdelayed.signo = signo;		t->thread_info->sigdelayed.code = code;		t->thread_info->sigdelayed.addr = addr;		t->thread_info->sigdelayed.start_time = start_time;		t->thread_info->sigdelayed.pid = pid;		wmb();		set_tsk_thread_flag(t, TIF_SIGDELAYED);	}}/* Called from entry.S when it detects TIF_SIGDELAYED, a delayed signal that * was detected in MCA/INIT/NMI/PMI context where it could not be delivered. */voiddo_sigdelayed(void){	struct siginfo siginfo;	pid_t pid;	struct task_struct *t;	clear_thread_flag(TIF_SIGDELAYED);	memset(&siginfo, 0, sizeof(siginfo));	siginfo.si_signo = current_thread_info()->sigdelayed.signo;	siginfo.si_code = current_thread_info()->sigdelayed.code;	siginfo.si_addr = current_thread_info()->sigdelayed.addr;	pid = current_thread_info()->sigdelayed.pid;	t = find_task_by_pid(pid);	if (!t)		return;	if (current_thread_info()->sigdelayed.start_time != start_time_ul(t))		return;	force_sig_info(siginfo.si_signo, &siginfo, t);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -