xref: /illumos-gate/usr/src/lib/libc/i386/sys/vforkx.S (revision 5d9d9091f564c198a760790b0bfa72c44e17912b)
1*5d9d9091SRichard Lowe/*
2*5d9d9091SRichard Lowe * CDDL HEADER START
3*5d9d9091SRichard Lowe *
4*5d9d9091SRichard Lowe * The contents of this file are subject to the terms of the
5*5d9d9091SRichard Lowe * Common Development and Distribution License (the "License").
6*5d9d9091SRichard Lowe * You may not use this file except in compliance with the License.
7*5d9d9091SRichard Lowe *
8*5d9d9091SRichard Lowe * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*5d9d9091SRichard Lowe * or http://www.opensolaris.org/os/licensing.
10*5d9d9091SRichard Lowe * See the License for the specific language governing permissions
11*5d9d9091SRichard Lowe * and limitations under the License.
12*5d9d9091SRichard Lowe *
13*5d9d9091SRichard Lowe * When distributing Covered Code, include this CDDL HEADER in each
14*5d9d9091SRichard Lowe * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*5d9d9091SRichard Lowe * If applicable, add the following below this CDDL HEADER, with the
16*5d9d9091SRichard Lowe * fields enclosed by brackets "[]" replaced with your own identifying
17*5d9d9091SRichard Lowe * information: Portions Copyright [yyyy] [name of copyright owner]
18*5d9d9091SRichard Lowe *
19*5d9d9091SRichard Lowe * CDDL HEADER END
20*5d9d9091SRichard Lowe */
21*5d9d9091SRichard Lowe
22*5d9d9091SRichard Lowe/*
23*5d9d9091SRichard Lowe * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
24*5d9d9091SRichard Lowe * Use is subject to license terms.
25*5d9d9091SRichard Lowe */
26*5d9d9091SRichard Lowe
27*5d9d9091SRichard Lowe	.file	"vforkx.s"
28*5d9d9091SRichard Lowe
29*5d9d9091SRichard Lowe#include "SYS.h"
30*5d9d9091SRichard Lowe#include <assym.h>
31*5d9d9091SRichard Lowe
32*5d9d9091SRichard Lowe/*
33*5d9d9091SRichard Lowe * pid = vforkx(flags);
34*5d9d9091SRichard Lowe * syscall trap: forksys(2, flags)
35*5d9d9091SRichard Lowe *
36*5d9d9091SRichard Lowe * pid = vfork();
37*5d9d9091SRichard Lowe * syscall trap: forksys(2, 0)
38*5d9d9091SRichard Lowe *
39*5d9d9091SRichard Lowe * From the syscall:
40*5d9d9091SRichard Lowe * %edx == 0 in parent process, %edx = 1 in child process.
41*5d9d9091SRichard Lowe * %eax == pid of child in parent, %eax == pid of parent in child.
42*5d9d9091SRichard Lowe *
43*5d9d9091SRichard Lowe * The child gets a zero return value.
44*5d9d9091SRichard Lowe * The parent gets the pid of the child.
45*5d9d9091SRichard Lowe */
46*5d9d9091SRichard Lowe
47*5d9d9091SRichard Lowe/*
48*5d9d9091SRichard Lowe * The child of vfork() will execute in the parent's address space,
49*5d9d9091SRichard Lowe * thereby changing the stack before the parent runs again.
50*5d9d9091SRichard Lowe * Therefore we have to be careful how we return from vfork().
51*5d9d9091SRichard Lowe * Pity the poor debugger developer who has to deal with this kludge.
52*5d9d9091SRichard Lowe *
53*5d9d9091SRichard Lowe * We block all blockable signals while performing the vfork() system call
54*5d9d9091SRichard Lowe * trap.  This enables us to set curthread->ul_vfork safely, so that we
55*5d9d9091SRichard Lowe * don't end up in a signal handler with curthread->ul_vfork set wrong.
56*5d9d9091SRichard Lowe */
57*5d9d9091SRichard Lowe
58*5d9d9091SRichard Lowe	ENTRY_NP(vforkx)
59*5d9d9091SRichard Lowe	movl	4(%esp), %eax		/* flags */
60*5d9d9091SRichard Lowe	jmp	0f
61*5d9d9091SRichard Lowe	ENTRY_NP(vfork)
62*5d9d9091SRichard Lowe	xorl	%eax, %eax		/* flags = 0 */
63*5d9d9091SRichard Lowe0:
64*5d9d9091SRichard Lowe	popl	%ecx			/* save return %eip in %ecx */
65*5d9d9091SRichard Lowe	pushl	%eax			/* flags */
66*5d9d9091SRichard Lowe	pushl	$MASKSET3		/* block all signals */
67*5d9d9091SRichard Lowe	pushl	$MASKSET2
68*5d9d9091SRichard Lowe	pushl	$MASKSET1
69*5d9d9091SRichard Lowe	pushl	$MASKSET0
70*5d9d9091SRichard Lowe	pushl	$SIG_SETMASK
71*5d9d9091SRichard Lowe	pushl	%ecx
72*5d9d9091SRichard Lowe	__SYSCALLINT(lwp_sigmask)
73*5d9d9091SRichard Lowe	addl	$24, %esp
74*5d9d9091SRichard Lowe
75*5d9d9091SRichard Lowe	pushl	$2
76*5d9d9091SRichard Lowe	pushl	%ecx
77*5d9d9091SRichard Lowe	__SYSCALLINT(forksys)		/* vforkx(flags) */
78*5d9d9091SRichard Lowe	jae 	1f
79*5d9d9091SRichard Lowe
80*5d9d9091SRichard Lowe	/* reconstruct stack before jumping to __cerror */
81*5d9d9091SRichard Lowe	addl	$12, %esp
82*5d9d9091SRichard Lowe	pushl	%ecx
83*5d9d9091SRichard Lowe	pushl	%eax			/* save the vfork() error number */
84*5d9d9091SRichard Lowe
85*5d9d9091SRichard Lowe	pushl	%gs:UL_SIGMASK+12	/* reinstate signals */
86*5d9d9091SRichard Lowe	pushl	%gs:UL_SIGMASK+8
87*5d9d9091SRichard Lowe	pushl	%gs:UL_SIGMASK+4
88*5d9d9091SRichard Lowe	pushl	%gs:UL_SIGMASK
89*5d9d9091SRichard Lowe	pushl	$SIG_SETMASK
90*5d9d9091SRichard Lowe	pushl	%ecx
91*5d9d9091SRichard Lowe	__SYSCALLINT(lwp_sigmask)
92*5d9d9091SRichard Lowe	addl	$24, %esp
93*5d9d9091SRichard Lowe
94*5d9d9091SRichard Lowe	popl	%eax			/* restore the vfork() error number */
95*5d9d9091SRichard Lowe	jmp	__cerror
96*5d9d9091SRichard Lowe
97*5d9d9091SRichard Lowe1:
98*5d9d9091SRichard Lowe	addl	$12, %esp
99*5d9d9091SRichard Lowe	/*
100*5d9d9091SRichard Lowe	 * To determine if we are (still) a child of vfork(), the child
101*5d9d9091SRichard Lowe	 * increments curthread->ul_vfork by one and the parent decrements
102*5d9d9091SRichard Lowe	 * it by one.  If the result is zero, then we are not a child of
103*5d9d9091SRichard Lowe	 * vfork(), else we are.  We do this to deal with the case of
104*5d9d9091SRichard Lowe	 * a vfork() child calling vfork().
105*5d9d9091SRichard Lowe	 */
106*5d9d9091SRichard Lowe	cmpl	$0, %edx
107*5d9d9091SRichard Lowe	jne	2f
108*5d9d9091SRichard Lowe	movl	%gs:UL_VFORK, %edx
109*5d9d9091SRichard Lowe	cmpl	$0, %edx		/* don't let it go negative */
110*5d9d9091SRichard Lowe	je	3f
111*5d9d9091SRichard Lowe	subl	$1, %edx		/* curthread->ul_vfork--; */
112*5d9d9091SRichard Lowe	jmp	3f
113*5d9d9091SRichard Lowe2:
114*5d9d9091SRichard Lowe	xorl	%eax, %eax		/* zero the return value in the child */
115*5d9d9091SRichard Lowe	movl	%gs:UL_VFORK, %edx
116*5d9d9091SRichard Lowe	addl	$1, %edx		/* curthread->ul_vfork++; */
117*5d9d9091SRichard Lowe3:
118*5d9d9091SRichard Lowe	movl	%edx, %gs:UL_VFORK
119*5d9d9091SRichard Lowe	/*
120*5d9d9091SRichard Lowe	 * Clear the schedctl interface in both parent and child.
121*5d9d9091SRichard Lowe	 * (The child might have modified the parent.)
122*5d9d9091SRichard Lowe	 */
123*5d9d9091SRichard Lowe	xorl	%edx, %edx
124*5d9d9091SRichard Lowe	movl	%edx, %gs:UL_SCHEDCTL
125*5d9d9091SRichard Lowe	movl	%edx, %gs:UL_SCHEDCTL_CALLED
126*5d9d9091SRichard Lowe	pushl	%eax			/* save the vfork() return value */
127*5d9d9091SRichard Lowe
128*5d9d9091SRichard Lowe	pushl	%gs:UL_SIGMASK+12	/* reinstate signals */
129*5d9d9091SRichard Lowe	pushl	%gs:UL_SIGMASK+8
130*5d9d9091SRichard Lowe	pushl	%gs:UL_SIGMASK+4
131*5d9d9091SRichard Lowe	pushl	%gs:UL_SIGMASK
132*5d9d9091SRichard Lowe	pushl	$SIG_SETMASK
133*5d9d9091SRichard Lowe	pushl	%ecx
134*5d9d9091SRichard Lowe	__SYSCALLINT(lwp_sigmask)
135*5d9d9091SRichard Lowe	addl	$24, %esp
136*5d9d9091SRichard Lowe
137*5d9d9091SRichard Lowe	popl	%eax			/* restore the vfork() return value */
138*5d9d9091SRichard Lowe	jmp	*%ecx			/* jump back to the caller */
139*5d9d9091SRichard Lowe	SET_SIZE(vfork)
140*5d9d9091SRichard Lowe	SET_SIZE(vforkx)
141