xref: /titanic_44/usr/src/lib/libc/amd64/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	movq	%rdi, %r8		/* flags */
62	jmp	0f
63	ENTRY_NP(vfork)
64	xorq	%r8, %r8		/* flags = 0 */
650:
66	popq	%r9			/* save return %rip in %r9 */
67	movl	$MASKSET1, %edx		/* block all signals */
68	movl	$MASKSET0, %esi
69	movl	$SIG_SETMASK, %edi
70	__SYSCALL(lwp_sigmask)
71
72	movq	%r8, %rsi		/* flags */
73	movl	$2, %edi
74	__SYSCALL(forksys)		/* vforkx(flags) */
75	jae 	1f
76
77	/* reconstruct stack before jumping to __cerror */
78	pushq	%r9
79	movq	%rax, %r8		/* save the vfork() error number */
80
81	movl	%fs:UL_SIGMASK+4, %edx	/* reinstate signals */
82	movl	%fs:UL_SIGMASK, %esi
83	movl	$SIG_SETMASK, %edi
84	__SYSCALL(lwp_sigmask)
85
86	movq	%r8, %rax		/* restore the vfork() error number */
87	jmp	__cerror
88
891:
90	/*
91	 * To determine if we are (still) a child of vfork(), the child
92	 * increments curthread->ul_vfork by one and the parent decrements
93	 * it by one.  If the result is zero, then we are not a child of
94	 * vfork(), else we are.  We do this to deal with the case of
95	 * a vfork() child calling vfork().
96	 */
97	cmpl	$0, %edx
98	jne	2f
99	movl	%fs:UL_VFORK, %edx
100	cmpl	$0, %edx		/* don't let it go negative */
101	je	3f
102	subl	$1, %edx		/* curthread->ul_vfork--; */
103	jmp	3f
1042:
105	xorl	%eax, %eax		/* zero the return value in the child */
106	movl	%fs:UL_VFORK, %edx
107	addl	$1, %edx		/* curthread->ul_vfork++; */
1083:
109	movl	%edx, %fs:UL_VFORK
110	/*
111	 * Clear the schedctl interface in both parent and child.
112	 * (The child might have modified the parent.)
113	 */
114	xorq	%rdx, %rdx
115	movq	%rdx, %fs:UL_SCHEDCTL
116	movq	%rdx, %fs:UL_SCHEDCTL_CALLED
117	movq	%rax, %r8		/* save the vfork() return value */
118
119	movl	%fs:UL_SIGMASK+4, %edx	/* reinstate signals */
120	movl	%fs:UL_SIGMASK, %esi
121	movl	$SIG_SETMASK, %edi
122	__SYSCALL(lwp_sigmask)
123
124	movq	%r8, %rax		/* restore the vfork() return value */
125	jmp	*%r9			/* jump back to the caller */
126	SET_SIZE(vfork)
127	SET_SIZE(vforkx)
128