xref: /titanic_51/usr/src/lib/libc/i386/sys/vforkx.s (revision 1db96d3b343c9ab3090da5b745f1cd9de2f192ef)
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	.file	"vforkx.s"
28
29#include "SYS.h"
30#include <assym.h>
31
32/*
33 * pid = vforkx(flags);
34 * syscall trap: forksys(2, flags)
35 *
36 * pid = vfork();
37 * syscall trap: forksys(2, 0)
38 *
39 * From the syscall:
40 * %edx == 0 in parent process, %edx = 1 in child process.
41 * %eax == pid of child in parent, %eax == pid of parent in child.
42 *
43 * The child gets a zero return value.
44 * The parent gets the pid of the child.
45 */
46
47/*
48 * The child of vfork() will execute in the parent's address space,
49 * thereby changing the stack before the parent runs again.
50 * Therefore we have to be careful how we return from vfork().
51 * Pity the poor debugger developer who has to deal with this kludge.
52 *
53 * We block all blockable signals while performing the vfork() system call
54 * trap.  This enables us to set curthread->ul_vfork safely, so that we
55 * don't end up in a signal handler with curthread->ul_vfork set wrong.
56 */
57
58	ENTRY_NP(vforkx)
59	movl	4(%esp), %eax		/* flags */
60	jmp	0f
61	ENTRY_NP(vfork)
62	xorl	%eax, %eax		/* flags = 0 */
630:
64	popl	%ecx			/* save return %eip in %ecx */
65	pushl	%eax			/* flags */
66	pushl	$MASKSET1		/* block all signals */
67	pushl	$MASKSET0
68	pushl	$SIG_SETMASK
69	pushl	%ecx
70	__SYSCALLINT(lwp_sigmask)
71	addl	$16, %esp
72
73	pushl	$2
74	pushl	%ecx
75	__SYSCALLINT(forksys)		/* vforkx(flags) */
76	jae 	1f
77
78	/* reconstruct stack before jumping to __cerror */
79	addl	$12, %esp
80	pushl	%ecx
81	pushl	%eax			/* save the vfork() error number */
82
83	pushl	%gs:UL_SIGMASK+4	/* reinstate signals */
84	pushl	%gs:UL_SIGMASK
85	pushl	$SIG_SETMASK
86	pushl	%ecx
87	__SYSCALLINT(lwp_sigmask)
88	addl	$16, %esp
89
90	popl	%eax			/* restore the vfork() error number */
91	jmp	__cerror
92
931:
94	addl	$12, %esp
95	/*
96	 * To determine if we are (still) a child of vfork(), the child
97	 * increments curthread->ul_vfork by one and the parent decrements
98	 * it by one.  If the result is zero, then we are not a child of
99	 * vfork(), else we are.  We do this to deal with the case of
100	 * a vfork() child calling vfork().
101	 */
102	cmpl	$0, %edx
103	jne	2f
104	movl	%gs:UL_VFORK, %edx
105	cmpl	$0, %edx		/* don't let it go negative */
106	je	3f
107	subl	$1, %edx		/* curthread->ul_vfork--; */
108	jmp	3f
1092:
110	xorl	%eax, %eax		/* zero the return value in the child */
111	movl	%gs:UL_VFORK, %edx
112	addl	$1, %edx		/* curthread->ul_vfork++; */
1133:
114	movl	%edx, %gs:UL_VFORK
115	/*
116	 * Clear the schedctl interface in both parent and child.
117	 * (The child might have modified the parent.)
118	 */
119	xorl	%edx, %edx
120	movl	%edx, %gs:UL_SCHEDCTL
121	movl	%edx, %gs:UL_SCHEDCTL_CALLED
122	pushl	%eax			/* save the vfork() return value */
123
124	pushl	%gs:UL_SIGMASK+4	/* reinstate signals */
125	pushl	%gs:UL_SIGMASK
126	pushl	$SIG_SETMASK
127	pushl	%ecx
128	__SYSCALLINT(lwp_sigmask)
129	addl	$16, %esp
130
131	popl	%eax			/* restore the vfork() return value */
132	jmp	*%ecx			/* jump back to the caller */
133	SET_SIZE(vfork)
134	SET_SIZE(vforkx)
135