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