xref: /openbsd-src/lib/libc/arch/i386/sys/tfork_thread.S (revision 83762a71f74848f4d09174ce350838b4204957c5)
1/*	$OpenBSD: tfork_thread.S,v 1.11 2023/12/10 16:45:51 deraadt Exp $ */
2/*-
3 * Copyright (c) 2000 Peter Wemm <peter@FreeBSD.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28#include <machine/asm.h>
29
30/*
31 * With thanks to John Dyson for the original version of this.
32 */
33
34#include "SYS.h"
35
36/*
37 *                8      12     16         20
38 * __tfork_thread(param, psize, start_fnc, start_arg);
39 *
40 * param:		Arguments to actual system call.
41 * psize:		Other argument to pass to the actual kernel call.
42 * start_fnc:		Address of thread function to call in child.
43 * start_arg:		Argument to pass to the thread function in child.
44 */
45
46ENTRY(__tfork_thread)
47	pushl	%ebp
48	movl	%esp, %ebp
49	pushl	%esi
50	pushl	%edi
51
52	/*
53	 * Save the thread info in esi and ebx
54	 */
55	movl	16(%ebp), %esi	# get start thread address
56	movl	20(%ebp), %edi	# get start argument
57
58	/*
59	 * Prepare and execute the thread creation syscall
60	 */
61	pushl	12(%ebp)	# push psize
62	pushl	8(%ebp)		# push param
63	pushl	$0		# slot for return address, ignored by kernel
64	movl	$SYS___tfork, %eax
6599:	int	$0x80
66	PINSYSCALL(SYS___tfork, 99b)
67	jb 	2f
68
69	/*
70	 * Check to see if we are in the parent or child
71	 */
72	cmpl	$0, %eax
73	jz	1f
74	addl	$12, %esp
75	popl	%edi
76	popl	%esi
77	movl	%ebp, %esp
78	popl	%ebp
79	ret
80	.p2align 2
81
82	/*
83	 * If we are in the child (new thread), then
84	 * set-up the call to the internal subroutine.  If it
85	 * returns, then call __threxit.
86	 */
871:
88	xorl	%ebp, %ebp	# mark outermost frame
89	subl	$4, %esp	# align stack
90	andl	$~15, %esp
91	addl	$4, %esp
92	pushl	%edi		# push start argument
93	call	*%esi
94	addl	$4, %esp
95
96	/*
97	 * Exit system call
98	 */
99	pushl	$0		# NULL pointer argument to __threxit
100	pushl	$0		# slot for return address, ignored by kernel
101	movl	$SYS___threxit, %eax
10298:	int	$0x80
103	PINSYSCALL(SYS___threxit, 98b)
104	int3
105
106	/*
107	 * Branch here if the thread creation fails:
108	 */
1092:
110	addl	$12, %esp
111	popl	%edi
112	popl	%esi
113	movl	%ebp, %esp
114	popl	%ebp
115	HANDLE_ERRNO()
116	ret
117END(__tfork_thread)
118