xref: /openbsd/lib/libc/arch/i386/sys/tfork_thread.S (revision f6aab3d8)
1/*	$OpenBSD: tfork_thread.S,v 1.10 2020/10/18 14:28:17 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
65	int	$0x80
66	jb 	2f
67
68	/*
69	 * Check to see if we are in the parent or child
70	 */
71	cmpl	$0, %eax
72	jz	1f
73	addl	$12, %esp
74	popl	%edi
75	popl	%esi
76	movl	%ebp, %esp
77	popl	%ebp
78	ret
79	.p2align 2
80
81	/*
82	 * If we are in the child (new thread), then
83	 * set-up the call to the internal subroutine.  If it
84	 * returns, then call __threxit.
85	 */
861:
87	xorl	%ebp, %ebp	# mark outermost frame
88	subl	$4, %esp	# align stack
89	andl	$~15, %esp
90	addl	$4, %esp
91	pushl	%edi		# push start argument
92	call	*%esi
93	addl	$4, %esp
94
95	/*
96	 * Exit system call
97	 */
98	pushl	$0		# NULL pointer argument to __threxit
99	pushl	$0		# slot for return address, ignored by kernel
100	movl	$SYS___threxit, %eax
101	int	$0x80
102	int3
103
104	/*
105	 * Branch here if the thread creation fails:
106	 */
1072:
108	addl	$12, %esp
109	popl	%edi
110	popl	%esi
111	movl	%ebp, %esp
112	popl	%ebp
113	HANDLE_ERRNO()
114	ret
115END(__tfork_thread)
116