CPU architecture:ARM920T
Author:ce123(http://blog.csdn.net/ce123)
当进程被调度时,会调用do_notify_resume()来处理信号队列中的信号。信号处理主要就是调用sighand_struct结构中对应的信号处理函数。do_notify_resume()(arch/arm/kernel/signal.c)函数的定义如下:
- asmlinkage void
- do_notify_resume(struct pt_regs *regs, unsigned int thread_flags, int syscall)
- {
- if (thread_flags & _TIF_SIGPENDING)
- do_signal(¤t->blocked, regs, syscall);
- }
- /*
- * 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.
- *
- * Note that we go through the signals twice: once to check the signals that
- * the kernel can handle, and then we build all the user-level signal handling
- * stack-frames in one go after that.
- */
- static int do_signal(sigset_t *oldset, struct pt_regs *regs, int syscall)
- {
- struct k_sigaction ka;
- siginfo_t info;
- int signr;
- /*
- * 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(regs))//regs保存的是进入内核态之前的寄存器现场,必须为用户模式,否则直接返回
- return 0;
- if (try_to_freeze())
- goto no_signal;
- if (current->ptrace & PT_SINGLESTEP)
- ptrace_cancel_bpt(current);//和调试相关,我们在后面的文章中会具体分析
- signr = get_signal_to_deliver(&info, &ka, regs, NULL);//取出等待处理的信号
- if (signr > 0) {
- handle_signal(signr, &ka, &info, oldset, regs, syscall);//处理信号
- if (current->ptrace & PT_SINGLESTEP)
- ptrace_set_bpt(current);
- return 1;
- }
- no_signal:
- /*
- * No signal to deliver to the process - restart the syscall.
- */
- if (syscall) {
- if (regs->ARM_r0 == -ERESTART_RESTARTBLOCK) {
- if (thumb_mode(regs)) {
- regs->ARM_r7 = __NR_restart_syscall;
- regs->ARM_pc -= 2;
- } else {
- u32 __user *usp;
- regs->ARM_sp -= 12;
- usp = (u32 __user *)regs->ARM_sp;
- put_user(regs->ARM_pc, &usp[0]);
- /* swi __NR_restart_syscall */
- put_user(0xef000000 | __NR_restart_syscall, &usp[1]);
- /* ldr pc, [sp], #12 */
- put_user(0xe49df00c, &usp[2]);
- flush_icache_range((unsigned long)usp,
- (unsigned long)(usp + 3));
- regs->ARM_pc = regs->ARM_sp + 4;
- }
- }
- if (regs->ARM_r0 == -ERESTARTNOHAND ||
- regs->ARM_r0 == -ERESTARTSYS ||
- regs->ARM_r0 == -ERESTARTNOINTR) {
- restart_syscall(regs);
- }
- }
- if (current->ptrace & PT_SINGLESTEP)
- ptrace_set_bpt(current);
- return 0;
- }
执行do_signal()函数时,进程一定处于内核空间,通常进程只有通过中断或者系统调用才能进入内核空间,regs保存着系统调用或者中断时的现场。user_mode()根据regs中的cpsr寄存器来判断是中断现场环境还是用户态环境。如果不是用户态环境,就不对信号进行任何处理,直接从do_signal()函数返回。
如果user_mode()函数发现regs的现场是内核态,那就意味着这不是一次系统调用的返回,也不是一次普通的中断返回,而是一次嵌套中断返回(或者在系统调用过程中发生了中断)。此时大概的执行路径应该是这样的:假设进场现在运行在用户态,此时发生一次中断,进场进入内核态(此时user_mode(regs)返回1,意味着中断现场是用户态。),此后在中断返回前,发生了一个更高优先级的中断,于是CPU开始执行高优先级的处理函数(此时user_mode(regs)返回0,意味着中断现场是在内核态)。当高优先级中断处理结束后,在它返回时,是不应该处理信号的,因为信号的优先级比中断的优先级低。在这种情况下,对信号的处理将会延迟到低优先级中断处理结束之后。相对于Windows内核来说,尽管linux内核中没有一组显式的操作函数来实现这一系列的优先级管理方案,但是linux内核和Windows内核都使用了同样的机制,优先级关系为:高优先级中断->低优先级中断->软中断(类似Windows内中的DPC)->信号(类似Windows内核中的APC)->进程运行。
如果user_mode(regs)返回1,接下来会执行(中间略去一下和本文关系不大的代码)get_signal_to_deliver(),这个函数从当前进程的信号队列(保存Private Signal Queue和Shared Signal Queue)取出等待处理的信号(调用dequeue_signal()函数),然后根据信号定位到对应的signal_struct结构,如果信号的处理函数sa_handler为SIG_IGN,就忽略该信号,继续取下一个信号;如果信号的处理函数sa_handler为SIG_DFL,意味着按照信号默认的处理方式对待就可以了(例如直接调用do_coredump()等)。
如果get_signal_to_deliver()函数返回值大于0,说明这个信号的处理函数是在用户态空间(通过signal()和sigaction()等函数设置的自定义信号处理函数。),将调用handle_signal()函数进行处理。handle_signal()函数的定义如下:
- /*
- * OK, we're invoking a handler
- */
- static void
- handle_signal(unsigned long sig, struct k_sigaction *ka,
- siginfo_t *info, sigset_t *oldset,
- struct pt_regs * regs, int syscall)
- {
- struct thread_info *thread = current_thread_info();
- struct task_struct *tsk = current;
- int usig = sig;
- int ret;
- /*
- * If we were from a system call, check for system call restarting...
- */
- if (syscall) {
- switch (regs->ARM_r0) {
- case -ERESTART_RESTARTBLOCK:
- case -ERESTARTNOHAND:
- regs->ARM_r0 = -EINTR;
- break;
- case -ERESTARTSYS:
- if (!(ka->sa.sa_flags & SA_RESTART)) {
- regs->ARM_r0 = -EINTR;
- break;
- }
- /* fallthrough */
- case -ERESTARTNOINTR:
- restart_syscall(regs);
- }
- }
- /*
- * translate the signal
- */
- if (usig < 32 && thread->exec_domain && thread->exec_domain->signal_invmap)
- usig = thread->exec_domain->signal_invmap[usig];
- /*
- * Set up the stack frame//设置栈帧
- */
- if (ka->sa.sa_flags & SA_SIGINFO)
- ret = setup_rt_frame(usig, ka, info, oldset, regs);
- else
- ret = setup_frame(usig, ka, oldset, regs);
- /*
- * Check that the resulting registers are actually sane.
- */
- ret |= !valid_user_regs(regs);
- /*
- * Block the signal if we were unsuccessful.
- */
- if (ret != 0) {
- spin_lock_irq(&tsk->sighand->siglock);
- sigorsets(&tsk->blocked, &tsk->blocked,
- &ka->sa.sa_mask);
- if (!(ka->sa.sa_flags & SA_NODEFER))
- sigaddset(&tsk->blocked, sig);
- recalc_sigpending();
- spin_unlock_irq(&tsk->sighand->siglock);
- }
- if (ret == 0)
- return;
- force_sigsegv(sig, tsk);
- }
1.临时的用户态堆栈在哪里呢?这个很好解决,因为可以直接使用进程现有的用户态堆栈,这里要保证的是:使用结束后这个堆栈必须和使用前是一模一样的。
2.临时堆栈解决后,需要确定的是通过什么方法来保证返回到用户态后,进程执行的是信号的处理函数。我们知道在进入内核态时,内核态堆栈中保存了一个中断现场,也就是一个pt_regs结构,中断返回地址就保存在pt_regts中的pc中,因此我们这里只要把当前进程的pt_regs中pc设置为sa_handler,然后返回到用户态就开始从sa_handler处开始执行了。
- unsigned long handler = (unsigned long)ka->sa.sa_handler;
- regs->ARM_pc = handler;
3.当信号的用户态处理函数执行结束时,需要再次进入内核态,还原用户态堆栈,并且修改pt_regs中的pc,保证将来能够按照正常的方式返回用户态。我们知道进程要主动进入内核态只有通过系统调用,出发异常等方法,为此内核专门提供了一个系统调用sys_sigreturn()(还有一个sys_rt_sigreturn()),但是如何调用sys_sigreturn()呢?强制安装信号处理函数最后必须调用一个sigreturn()不是一个好办法,因为不了解内核的程序员会对这个限制感到不解,为此程序员常常忘记在它们的信号处理函数的末尾调用sigreturn(),如果真是这样,arm-linux-gcc也检测不出这个错误。为此,内核修改regs的ARM_lr值,:
- regs->ARM_lr = retcode;
当用户态信号处理函数运行结束时,会从lr取出返回地址,因此内核在构建临时regs时,会把上面这段代码的入口地址保存在lr,这样当信号处理完成后,就会顺利的通过系统调用sys_sigreturn()进入内核。
4.当通过构造的sys_sigreturn()返回到内核态之后,内核需要顺利的返回到用户态执行原来的代码(信号处理前应该返回的用户空间状态),但是此时进入内核空间的pt_regs上下文是通过sys_sigreturn()构造的,而最初的内核堆栈中的pt_regs上下文在第一次返回用户空间执行信号处理函数时,就已经被“销毁”了(内核态堆栈的pt_regs上下文在中断返回后就不复存在了)。而现在必须通过最初的pt_regs上下文返回用户态,为此,在构建临时堆栈环境时,内核会把最初的pt_regs上下文备份到临时堆栈中(位于用户态堆栈),当通过系统调用sys_sigreturn()再次进入内核时,内核从用户态空间还原出原始的pt_regs。最后正常返回。
通过上面的讨论,我们知道在这个迂回的处理过程中,关键在于用户态的临时堆栈环境的建立,这是一个sigframe结构:
- /*
- * Do a signal return; undo the signal stack. These are aligned to 64-bit.
- */
- struct sigframe {
- struct sigcontext sc;//保存一组寄存器上下文
- unsigned long extramask[_NSIG_WORDS-1];
- unsigned long retcode;//保存返回地址
- struct aux_sigframe aux __attribute__((aligned(8)));
- };
- struct rt_sigframe {
- struct siginfo __user *pinfo;
- void __user *puc;
- struct siginfo info;
- struct ucontext uc;
- unsigned long retcode;
- struct aux_sigframe aux __attribute__((aligned(8)));
- };
- static inline void __user *
- get_sigframe(struct k_sigaction *ka, struct pt_regs *regs, int framesize)
- {
- unsigned long sp = regs->ARM_sp;
- void __user *frame;
- /*
- * This is the X/Open sanctioned signal stack switching.
- */
- if ((ka->sa.sa_flags & SA_ONSTACK) && !sas_ss_flags(sp))
- sp = current->sas_ss_sp + current->sas_ss_size;
- /*
- * ATPCS B01 mandates 8-byte alignment
- */
- frame = (void __user *)((sp - framesize) & ~7);
- /*
- * Check that we can actually write to the signal frame.
- */
- if (!access_ok(VERIFY_WRITE, frame, framesize))
- frame = NULL;
- return frame;
- }
通过上面的讨论,我们再回到do_signal()中来,如果有用户态的信号处理函数,do_signal()会调用handle_signal(),handle_signal()将调用setup_frame()或者setup_rt_frame()来完成实际的工作,这里我们以setup_frame()为例进行进一步讨论。
- static int
- setup_frame(int usig, struct k_sigaction *ka, sigset_t *set, struct pt_regs *regs)
- {
- //在用户态堆栈上分配一个sigframe结构
- struct sigframe __user *frame = get_sigframe(ka, regs, sizeof(*frame));
- int err = 0;
- if (!frame)
- return 1;
- //把相关信息从内核态备份到用户态堆栈的sigframe结构中
- err |= setup_sigcontext(&frame->sc, &frame->aux, regs, set->sig[0]);
- if (_NSIG_WORDS > 1) {
- err |= __copy_to_user(frame->extramask, &set->sig[1],
- sizeof(frame->extramask));
- }
- if (err == 0)
- err = setup_return(regs, ka, &frame->retcode, frame, usig);
- return err;
- }
- static int
- setup_return(struct pt_regs *regs, struct k_sigaction *ka,
- unsigned long __user *rc, void __user *frame, int usig)
- {
- unsigned long handler = (unsigned long)ka->sa.sa_handler;
- unsigned long retcode;
- int thumb = 0;
- unsigned long cpsr = regs->ARM_cpsr & ~PSR_f;
- /*
- * Maybe we need to deliver a 32-bit signal to a 26-bit task.
- */
- if (ka->sa.sa_flags & SA_THIRTYTWO)
- cpsr = (cpsr & ~MODE_MASK) | USR_MODE;
- #ifdef CONFIG_ARM_THUMB
- if (elf_hwcap & HWCAP_THUMB) {
- /*
- * The LSB of the handler determines if we're going to
- * be using THUMB or ARM mode for this signal handler.
- */
- thumb = handler & 1;
- if (thumb)
- cpsr |= PSR_T_BIT;
- else
- cpsr &= ~PSR_T_BIT;
- }
- #endif
- //这里的retcode就是保存手工构造的sigreturn()代码
- if (ka->sa.sa_flags & SA_RESTORER) {
- retcode = (unsigned long)ka->sa.sa_restorer;
- } else {
- unsigned int idx = thumb;
- if (ka->sa.sa_flags & SA_SIGINFO)
- idx += 2;
- if (__put_user(sigreturn_codes[idx], rc))
- return 1;
- if (cpsr & MODE32_BIT) {
- /*
- * 32-bit code can use the new high-page
- * signal return code support.
- */
- retcode = KERN_SIGRETURN_CODE + (idx << 2) + thumb;
- } else {
- /*
- * Ensure that the instruction cache sees
- * the return code written onto the stack.
- */
- flush_icache_range((unsigned long)rc,
- (unsigned long)(rc + 1));
- retcode = ((unsigned long)rc) + thumb;
- }
- }
- regs->ARM_r0 = usig;
- regs->ARM_sp = (unsigned long)frame;//堆栈
- regs->ARM_lr = retcode;//返回地址,当用户态信号处理函数结束时,就会把这个地址作为返回地址
- regs->ARM_pc = handler;//信号处理函数
- regs->ARM_cpsr = cpsr;
- return 0;
- }
当setup_frame()返回后,一切准备就绪,因此可以从内核态返回了,这样就顺利过渡到用户态的信号处理函数。当这个函数处理结束后,会通过retcode再次进入内核态,现在我们看看retcode是如何处理的,下面的代码选自glibc(2.3.2):
- #include
- /* If no SA_RESTORER function was specified by the application we use
- one of these. This avoids the need for the kernel to synthesise a return
- instruction on the stack, which would involve expensive cache flushes. */
- ENTRY(__default_sa_restorer)
- swi SYS_ify(sigreturn)
- #ifdef __NR_rt_sigreturn
- ENTRY(__default_rt_sa_restorer)
- swi SYS_ify(rt_sigreturn)
- #define SYS_ify(syscall_name) (__NR_##syscall_name)
下面具体看看sys_sigreturn()的定义:
- asmlinkage int sys_sigreturn(struct pt_regs *regs)
- {
- struct sigframe __user *frame;
- sigset_t set;
- /* Always make any pending restarted system calls return -EINTR */
- current_thread_info()->restart_block.fn = do_no_restart_syscall;
- /*
- * Since we stacked the signal on a 64-bit boundary,
- * then 'sp' should be word aligned here. If it's
- * not, then the user is trying to mess with us.
- */
- if (regs->ARM_sp & 7)
- goto badframe;
- frame = (struct sigframe __user *)regs->ARM_sp;
- if (!access_ok(VERIFY_READ, frame, sizeof (*frame)))
- goto badframe;
- if (__get_user(set.sig[0], &frame->sc.oldmask)
- || (_NSIG_WORDS > 1
- && __copy_from_user(&set.sig[1], &frame->extramask,
- sizeof(frame->extramask))))
- goto badframe;
- sigdelsetmask(&set, ~_BLOCKABLE);
- spin_lock_irq(¤t->sighand->siglock);
- current->blocked = set;
- recalc_sigpending();
- spin_unlock_irq(¤tt->sighand->siglock);
- if (restore_sigcontext(regs, &frame->sc, &frame->aux))
- goto badframe;
- /* Send SIGTRAP if we're single-stepping */
- if (current->ptrace & PT_SINGLESTEP) {
- ptrace_cancel_bpt(current);
- send_sig(SIGTRAP, current, 1);
- }
- return regs->ARM_r0;
- badframe:
- force_sig(SIGSEGV, current);
- return 0;
- }
- static int
- restore_sigcontext(struct pt_regs *regs, struct sigcontext __user *sc,
- struct aux_sigframe __user *aux)
- {
- int err = 0;
- __get_user_error(regs->ARM_r0, &sc->arm_r0, err);
- __get_user_error(regs->ARM_r1, &sc->arm_r1, err);
- __get_user_error(regs->ARM_r2, &sc->arm_r2, err);
- __get_user_error(regs->ARM_r3, &sc->arm_r3, err);
- __get_user_error(regs->ARM_r4, &sc->arm_r4, err);
- __get_user_error(regs->ARM_r5, &sc->arm_r5, err);
- __get_user_error(regs->ARM_r6, &sc->arm_r6, err);
- __get_user_error(regs->ARM_r7, &sc->arm_r7, err);
- __get_user_error(regs->ARM_r8, &sc->arm_r8, err);
- __get_user_error(regs->ARM_r9, &sc->arm_r9, err);
- __get_user_error(regs->ARM_r10, &sc->arm_r10, err);
- __get_user_error(regs->ARM_fp, &sc->arm_fp, err);
- __get_user_error(regs->ARM_ip, &sc->arm_ip, err);
- __get_user_error(regs->ARM_sp, &sc->arm_sp, err);
- __get_user_error(regs->ARM_lr, &sc->arm_lr, err);
- __get_user_error(regs->ARM_pc, &sc->arm_pc, err);
- __get_user_error(regs->ARM_cpsr, &sc->arm_cpsr, err);
- err |= !valid_user_regs(regs);
- #ifdef CONFIG_IWMMXT
- if (err == 0 && test_thread_flag(TIF_USING_IWMMXT))
- err |= restore_iwmmxt_context(&aux->iwmmxt);
- #endif
- #ifdef CONFIG_VFP
- // if (err == 0)
- // err |= vfp_restore_state(&aux->vfp);
- #endif
- return err;
- }
上一篇:linux内核中的信号机制--从用户层到内核层
下一篇:linux内核中的信号机制--信号发送
推荐阅读最新更新时间:2024-03-16 14:45
设计资源 培训 开发板 精华推荐
- 柔灵科技陈涵:将小型、柔性的脑机接口睡眠设备,做到千家万户
- 微灵医疗李骁健:脑机接口技术正在开启意识与AI融合的新纪元
- USB Type-C® 和 USB Power Delivery:专为扩展功率范围和电池供电型系统而设计
- 景昱医疗耿东:脑机接口DBS治疗技术已实现国产替代
- 首都医科大学王长明:针对癫痫的数字疗法已进入使用阶段
- 非常见问题解答第223期:如何在没有软启动方程的情况下测量和确定软启动时序?
- 兆易创新GD25/55全系列车规级SPI NOR Flash荣获ISO 26262 ASIL D功能安全认证证书
- 新型IsoVu™ 隔离电流探头:为电流测量带来全新维度
- 英飞凌推出简化电机控制开发的ModusToolbox™电机套件
- 意法半导体IO-Link执行器电路板为工业监控和设备厂商带来一站式参考设计