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