kernel: trap-dependent state restore, trace fixes
[minix.git] / kernel / system / do_sigsend.c
blobbbf21149de07645b62e30ea44f1c728f81884914
1 /* The kernel call that is implemented in this file:
2 * m_type: SYS_SIGSEND
4 * The parameters for this kernel call are:
5 * m2_i1: SIG_ENDPT # process to call signal handler
6 * m2_p1: SIG_CTXT_PTR # pointer to sigcontext structure
7 * m2_i3: SIG_FLAGS # flags for S_SIGRETURN call
9 */
11 #include "kernel/system.h"
12 #include <signal.h>
13 #include <string.h>
15 #if USE_SIGSEND
17 /*===========================================================================*
18 * do_sigsend *
19 *===========================================================================*/
20 int do_sigsend(struct proc * caller, message * m_ptr)
22 /* Handle sys_sigsend, POSIX-style signal handling. */
24 struct sigmsg smsg;
25 register struct proc *rp;
26 struct sigcontext sc, *scp;
27 struct sigframe fr, *frp;
28 int proc_nr, r;
30 if (!isokendpt(m_ptr->SIG_ENDPT, &proc_nr)) return(EINVAL);
31 if (iskerneln(proc_nr)) return(EPERM);
32 rp = proc_addr(proc_nr);
34 /* Get the sigmsg structure into our address space. */
35 if((r=data_copy_vmcheck(caller, caller->p_endpoint,
36 (vir_bytes) m_ptr->SIG_CTXT_PTR, KERNEL, (vir_bytes) &smsg,
37 (phys_bytes) sizeof(struct sigmsg))) != OK)
38 return r;
40 /* Compute the user stack pointer where sigcontext will be stored. */
41 smsg.sm_stkptr = arch_get_sp(rp);
42 scp = (struct sigcontext *) smsg.sm_stkptr - 1;
44 /* Copy the registers to the sigcontext structure. */
45 memcpy(&sc.sc_regs, (char *) &rp->p_reg, sizeof(sigregs));
47 #if defined(__i386__)
48 sc.trap_style = rp->p_seg.p_kern_trap_style;
50 if(sc.trap_style == KTS_NONE) {
51 printf("do_sigsend: sigsend an unsaved process\n");
52 return EINVAL;
55 if(proc_used_fpu(rp)) {
56 /* save the FPU context before saving it to the sig context */
57 save_fpu(rp);
58 memcpy(&sc.sc_fpu_state, rp->p_seg.fpu_state, FPU_XFP_SIZE);
60 #endif
62 /* Finish the sigcontext initialization. */
63 sc.sc_mask = smsg.sm_mask;
64 sc.sc_flags = rp->p_misc_flags & MF_FPU_INITIALIZED;
66 /* Copy the sigcontext structure to the user's stack. */
67 if((r=data_copy_vmcheck(caller, KERNEL, (vir_bytes) &sc, m_ptr->SIG_ENDPT,
68 (vir_bytes) scp, (vir_bytes) sizeof(struct sigcontext))) != OK)
69 return r;
71 /* Initialize the sigframe structure. */
72 frp = (struct sigframe *) scp - 1;
73 fr.sf_scpcopy = scp;
74 fr.sf_retadr2= (void (*)()) rp->p_reg.pc;
75 fr.sf_fp = rp->p_reg.fp;
76 rp->p_reg.fp = (reg_t) &frp->sf_fp;
77 fr.sf_scp = scp;
79 fpu_sigcontext(rp, &fr, &sc);
81 fr.sf_signo = smsg.sm_signo;
82 fr.sf_retadr = (void (*)()) smsg.sm_sigreturn;
84 /* Copy the sigframe structure to the user's stack. */
85 if((r=data_copy_vmcheck(caller, KERNEL, (vir_bytes) &fr,
86 m_ptr->SIG_ENDPT, (vir_bytes) frp,
87 (vir_bytes) sizeof(struct sigframe))) != OK)
88 return r;
90 /* Reset user registers to execute the signal handler. */
91 rp->p_reg.sp = (reg_t) frp;
92 rp->p_reg.pc = (reg_t) smsg.sm_sighandler;
94 /* Signal handler should get clean FPU. */
95 rp->p_misc_flags &= ~MF_FPU_INITIALIZED;
97 if(!RTS_ISSET(rp, RTS_PROC_STOP)) {
98 printf("system: warning: sigsend a running process\n");
99 printf("caller stack: ");
100 proc_stacktrace(caller);
103 return(OK);
106 #endif /* USE_SIGSEND */