xref: /openbsd-src/lib/libc/arch/i386/sys/tfork_thread.S (revision 6df0f8ce86b1074bf0dc0672fba71a58fdce4db9)
1/*	$OpenBSD: tfork_thread.S,v 1.2 2012/04/11 18:36:57 kettenis 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#if 0
30__FBSDID("$FreeBSD: /repoman/r/ncvs/src/lib/libc/i386/gen/rfork_thread.S,v 1.5 2003/05/07 17:23:25 jhb Exp $");
31#endif
32
33/*
34 * With thanks to John Dyson for the original version of this.
35 */
36
37#include "SYS.h"
38
39/*
40 *                8      12          16         20
41 * __tfork_thread(param, stack_addr, start_fnc, start_arg);
42 *
43 * param:		Arguments to actual system call.
44 * stack_addr:		Top of stack for thread.
45 * start_fnc:		Address of thread function to call in child.
46 * start_arg:		Argument to pass to the thread function in child.
47 */
48
49ENTRY(__tfork_thread)
50	pushl	%ebp
51	movl	%esp, %ebp
52	pushl	%esi
53
54	/*
55	 * Push thread info onto the new thread's stack
56	 */
57	movl	12(%ebp), %esi	# get stack addr
58
59	subl	$4, %esi
60	movl	20(%ebp), %eax	# get start argument
61	movl	%eax, (%esi)
62
63	subl	$4, %esi
64	movl	16(%ebp), %eax	# get start thread address
65	movl	%eax, (%esi)
66
67	/*
68	 * Prepare and execute the thread creation syscall
69	 */
70	pushl	8(%ebp)
71	pushl	$0
72	movl	$SYS___tfork, %eax
73	int	$0x80
74	jb 	2f
75
76	/*
77	 * Check to see if we are in the parent or child
78	 */
79	cmpl	$0, %edx
80	jnz	1f
81	addl	$8, %esp
82	popl	%esi
83	movl	%ebp, %esp
84	popl	%ebp
85	ret
86	.p2align 2
87
88	/*
89	 * If we are in the child (new thread), then
90	 * set-up the call to the internal subroutine.  If it
91	 * returns, then call __exit.
92	 */
931:
94	xorl	%ebp, %ebp	# mark outermost frame
95	movl	%esi, %esp
96	popl	%eax
97	call	*%eax
98	addl	$4, %esp
99
100	/*
101	 * Exit system call
102	 */
103	pushl	%eax
104	pushl	$0
105	movl	$SYS___threxit, %eax
106	int	$0x80
107
108	/*
109	 * Branch here if the thread creation fails:
110	 */
1112:
112	addl	$8, %esp
113	popl	%esi
114	movl	%ebp, %esp
115	popl	%ebp
116#ifdef PIC
117	PIC_PROLOGUE
118	movl	PIC_GOT(CERROR), %ecx
119	PIC_EPILOGUE
120	jmp	*%ecx
121#else
122	jmp	CERROR
123#endif
124