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