xref: /freebsd-14.2/lib/libc/i386/gen/rfork_thread.S (revision 1d386b48)
1/*-
2 * Copyright (c) 2000 Peter Wemm <[email protected]>
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	movl	$SYS_rfork, %eax
68	KERNCALL
69	jb 	2f
70
71	/*
72	 * Check to see if we are in the parent or child
73	 */
74	cmpl	$0, %edx
75	jnz	1f
76	addl	$8, %esp
77	popl	%esi
78	movl	%ebp, %esp
79	popl	%ebp
80	ret
81	.p2align 2
82
83	/*
84	 * If we are in the child (new thread), then
85	 * set-up the call to the internal subroutine.  If it
86	 * returns, then call __exit.
87	 */
881:
89	movl	%esi,%esp
90	popl	%eax
91	call	*%eax
92	addl	$4, %esp
93
94	/*
95	 * Exit system call
96	 */
97	pushl	%eax
98	pushl	$0
99	movl	$SYS_exit, %eax
100	KERNCALL
101
102	/*
103	 * Branch here if the thread creation fails:
104	 */
1052:
106	addl	$8, %esp
107	popl	%esi
108	movl	%ebp, %esp
109	popl	%ebp
110	jmp	HIDENAME(cerror)
111END(rfork_thread)
112
113	.section .note.GNU-stack,"",%progbits
114