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