xref: /minix3/minix/kernel/system/do_sigreturn.c (revision 433d6423c39e34ec4b79c950597bb2d236f886be)
1*433d6423SLionel Sambuc /* The kernel call that is implemented in this file:
2*433d6423SLionel Sambuc  *	m_type: SYS_SIGRETURN
3*433d6423SLionel Sambuc  *
4*433d6423SLionel Sambuc  * The parameters for this kernel call are:
5*433d6423SLionel Sambuc  *	m_sigcalls.endp		# process returning from handler
6*433d6423SLionel Sambuc  *	m_sigcalls.sigctx	# pointer to sigcontext structure
7*433d6423SLionel Sambuc  *
8*433d6423SLionel Sambuc  */
9*433d6423SLionel Sambuc 
10*433d6423SLionel Sambuc #include "kernel/system.h"
11*433d6423SLionel Sambuc #include <string.h>
12*433d6423SLionel Sambuc #include <machine/cpu.h>
13*433d6423SLionel Sambuc 
14*433d6423SLionel Sambuc #if USE_SIGRETURN
15*433d6423SLionel Sambuc 
16*433d6423SLionel Sambuc /*===========================================================================*
17*433d6423SLionel Sambuc  *			      do_sigreturn				     *
18*433d6423SLionel Sambuc  *===========================================================================*/
do_sigreturn(struct proc * caller,message * m_ptr)19*433d6423SLionel Sambuc int do_sigreturn(struct proc * caller, message * m_ptr)
20*433d6423SLionel Sambuc {
21*433d6423SLionel Sambuc /* POSIX style signals require sys_sigreturn to put things in order before
22*433d6423SLionel Sambuc  * the signalled process can resume execution
23*433d6423SLionel Sambuc  */
24*433d6423SLionel Sambuc   struct sigcontext sc;
25*433d6423SLionel Sambuc   register struct proc *rp;
26*433d6423SLionel Sambuc   int proc_nr, r;
27*433d6423SLionel Sambuc 
28*433d6423SLionel Sambuc   if (!isokendpt(m_ptr->m_sigcalls.endpt, &proc_nr)) return EINVAL;
29*433d6423SLionel Sambuc   if (iskerneln(proc_nr)) return EPERM;
30*433d6423SLionel Sambuc   rp = proc_addr(proc_nr);
31*433d6423SLionel Sambuc 
32*433d6423SLionel Sambuc   /* Copy in the sigcontext structure. */
33*433d6423SLionel Sambuc   if ((r = data_copy(m_ptr->m_sigcalls.endpt,
34*433d6423SLionel Sambuc 		 (vir_bytes)m_ptr->m_sigcalls.sigctx, KERNEL,
35*433d6423SLionel Sambuc 		 (vir_bytes)&sc, sizeof(struct sigcontext))) != OK)
36*433d6423SLionel Sambuc 	return r;
37*433d6423SLionel Sambuc 
38*433d6423SLionel Sambuc #if defined(__i386__)
39*433d6423SLionel Sambuc   /* Restore user bits of psw from sc, maintain system bits from proc. */
40*433d6423SLionel Sambuc   sc.sc_eflags  =  (sc.sc_eflags & X86_FLAGS_USER) |
41*433d6423SLionel Sambuc                 (rp->p_reg.psw & ~X86_FLAGS_USER);
42*433d6423SLionel Sambuc #endif
43*433d6423SLionel Sambuc 
44*433d6423SLionel Sambuc #if defined(__i386__)
45*433d6423SLionel Sambuc   /* Write back registers we allow to be restored, i.e.
46*433d6423SLionel Sambuc    * not the segment ones.
47*433d6423SLionel Sambuc    */
48*433d6423SLionel Sambuc   rp->p_reg.di = sc.sc_edi;
49*433d6423SLionel Sambuc   rp->p_reg.si = sc.sc_esi;
50*433d6423SLionel Sambuc   rp->p_reg.fp = sc.sc_ebp;
51*433d6423SLionel Sambuc   rp->p_reg.bx = sc.sc_ebx;
52*433d6423SLionel Sambuc   rp->p_reg.dx = sc.sc_edx;
53*433d6423SLionel Sambuc   rp->p_reg.cx = sc.sc_ecx;
54*433d6423SLionel Sambuc   rp->p_reg.retreg = sc.sc_eax;
55*433d6423SLionel Sambuc   rp->p_reg.pc = sc.sc_eip;
56*433d6423SLionel Sambuc   rp->p_reg.psw = sc.sc_eflags;
57*433d6423SLionel Sambuc   rp->p_reg.sp = sc.sc_esp;
58*433d6423SLionel Sambuc #endif
59*433d6423SLionel Sambuc 
60*433d6423SLionel Sambuc #if defined(__arm__)
61*433d6423SLionel Sambuc   rp->p_reg.psr = sc.sc_spsr;
62*433d6423SLionel Sambuc   rp->p_reg.retreg = sc.sc_r0;
63*433d6423SLionel Sambuc   rp->p_reg.r1 = sc.sc_r1;
64*433d6423SLionel Sambuc   rp->p_reg.r2 = sc.sc_r2;
65*433d6423SLionel Sambuc   rp->p_reg.r3 = sc.sc_r3;
66*433d6423SLionel Sambuc   rp->p_reg.r4 = sc.sc_r4;
67*433d6423SLionel Sambuc   rp->p_reg.r5 = sc.sc_r5;
68*433d6423SLionel Sambuc   rp->p_reg.r6 = sc.sc_r6;
69*433d6423SLionel Sambuc   rp->p_reg.r7 = sc.sc_r7;
70*433d6423SLionel Sambuc   rp->p_reg.r8 = sc.sc_r8;
71*433d6423SLionel Sambuc   rp->p_reg.r9 = sc.sc_r9;
72*433d6423SLionel Sambuc   rp->p_reg.r10 = sc.sc_r10;
73*433d6423SLionel Sambuc   rp->p_reg.fp = sc.sc_r11;
74*433d6423SLionel Sambuc   rp->p_reg.r12 = sc.sc_r12;
75*433d6423SLionel Sambuc   rp->p_reg.sp = sc.sc_usr_sp;
76*433d6423SLionel Sambuc   rp->p_reg.lr = sc.sc_usr_lr;
77*433d6423SLionel Sambuc   rp->p_reg.pc = sc.sc_pc;
78*433d6423SLionel Sambuc #endif
79*433d6423SLionel Sambuc 
80*433d6423SLionel Sambuc   /* Restore the registers. */
81*433d6423SLionel Sambuc   arch_proc_setcontext(rp, &rp->p_reg, 1, sc.trap_style);
82*433d6423SLionel Sambuc 
83*433d6423SLionel Sambuc   if(sc.sc_magic != SC_MAGIC) { printf("kernel sigreturn: corrupt signal context\n"); }
84*433d6423SLionel Sambuc 
85*433d6423SLionel Sambuc #if defined(__i386__)
86*433d6423SLionel Sambuc   if (sc.sc_flags & MF_FPU_INITIALIZED)
87*433d6423SLionel Sambuc   {
88*433d6423SLionel Sambuc 	memcpy(rp->p_seg.fpu_state, &sc.sc_fpu_state, FPU_XFP_SIZE);
89*433d6423SLionel Sambuc 	rp->p_misc_flags |=  MF_FPU_INITIALIZED; /* Restore math usage flag. */
90*433d6423SLionel Sambuc 	/* force reloading FPU */
91*433d6423SLionel Sambuc 	release_fpu(rp);
92*433d6423SLionel Sambuc   }
93*433d6423SLionel Sambuc #endif
94*433d6423SLionel Sambuc 
95*433d6423SLionel Sambuc   return OK;
96*433d6423SLionel Sambuc }
97*433d6423SLionel Sambuc #endif /* USE_SIGRETURN */
98*433d6423SLionel Sambuc 
99