xref: /freebsd/lib/libsys/i386/rfork_thread.S (revision 6580f5c38dd5b01aeeaed16b370f1a12423437f0)
1/*-
2 * Copyright (c) 2000 Peter Wemm <peter@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#include <machine/asm.h>
28/*
29 * With thanks to John Dyson for the original version of this.
30 */
31
32#include <SYS.h>
33
34/*
35 *                8      12          16         20
36 * rfork_thread(flags, stack_addr, start_fnc, start_arg);
37 *
38 * flags:		Flags to rfork system call.  See rfork(2).
39 * stack_addr:		Top of stack for thread.
40 * start_fnc:		Address of thread function to call in child.
41 * start_arg:		Argument to pass to the thread function in child.
42 */
43
44ENTRY(rfork_thread)
45	pushl	%ebp
46	movl	%esp, %ebp
47	pushl	%esi
48
49	/*
50	 * Push thread info onto the new thread's stack
51	 */
52	movl	12(%ebp), %esi	# get stack addr
53
54	subl	$4, %esi
55	movl	20(%ebp), %eax	# get start argument
56	movl	%eax, (%esi)
57
58	subl	$4, %esi
59	movl	16(%ebp), %eax	# get start thread address
60	movl	%eax, (%esi)
61
62	/*
63	 * Prepare and execute the thread creation syscall
64	 */
65	pushl	8(%ebp)
66	pushl	$0
67	_SYSCALL(rfork)
68	jb 	2f
69
70	/*
71	 * Check to see if we are in the parent or child
72	 */
73	cmpl	$0, %edx
74	jnz	1f
75	addl	$8, %esp
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 __exit.
86	 */
871:
88	movl	%esi,%esp
89	popl	%eax
90	call	*%eax
91	addl	$4, %esp
92
93	/*
94	 * Exit system call
95	 */
96	pushl	%eax
97	pushl	$0
98	_SYSCALL(exit)
99
100	/*
101	 * Branch here if the thread creation fails:
102	 */
1032:
104	addl	$8, %esp
105	popl	%esi
106	movl	%ebp, %esp
107	popl	%ebp
108	jmp	HIDENAME(cerror)
109END(rfork_thread)
110
111	.section .note.GNU-stack,"",%progbits
112