summaryrefslogtreecommitdiff
path: root/lib/libsys/i386/pdrfork_thread.S
blob: 92a45fc4783fbe24ed520b44610458a60f65184c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*-
 * SPDX-License-Identifier: BSD-2-Clause
 *
 * Copyright 2000 Peter Wemm <peter@FreeBSD.org>
 * Copyright 2026 The FreeBSD Foundation
 * All rights reserved.
 *
 * Portions of this software were developed by
 * Konstantin Belousov <kib@FreeBSD.org> under sponsorship from
 * the FreeBSD Foundation.
 */

#include <machine/asm.h>
/*
 * With thanks to John Dyson for the original version of this.
 */

#include <SYS.h>

/*
 *              8    12      16       20          24         28
 * rfork_thread(fdp, pdflags rfflags, stack_addr, start_fnc, start_arg);
 *
 * fdp			Pointer for the resulting fd location
 * pdflags		Flags as to pdfork.
 * rfflags:		Flags as to rfork.
 * stack_addr:		Top of stack for thread.
 * start_fnc:		Address of thread function to call in child.
 * start_arg:		Argument to pass to the thread function in child.
 */

ENTRY(pdrfork_thread)
	pushl	%ebp
	movl	%esp, %ebp
	pushl	%esi

	/*
	 * Push thread info onto the new thread's stack
	 */
	movl	20(%ebp), %esi	# get stack addr

	subl	$4, %esi
	movl	28(%ebp), %eax	# get start argument
	movl	%eax, (%esi)

	subl	$4, %esi
	movl	24(%ebp), %eax	# get start thread address
	movl	%eax, (%esi)

	/*
	 * Prepare and execute the thread creation syscall
	 */
	pushl	16(%ebp)
	pushl	12(%ebp)
	pushl	8(%ebp)
	pushl	$0
	_SYSCALL(pdrfork)
	jb 	2f

	/*
	 * Check to see if we are in the parent or child
	 */
	cmpl	$0, %edx
	jnz	1f
	addl	$16, %esp
	popl	%esi
	movl	%ebp, %esp
	popl	%ebp
	ret
	.p2align 2

	/*
	 * If we are in the child (new thread), then
	 * set-up the call to the internal subroutine.  If it
	 * returns, then call __exit.
	 */
1:
	movl	%esi,%esp
	popl	%eax
	call	*%eax
	addl	$4, %esp

	/*
	 * Exit system call
	 */
	pushl	%eax
	pushl	$0
	_SYSCALL(exit)

	/*
	 * Branch here if the thread creation fails:
	 */
2:
	addl	$16, %esp
	popl	%esi
	movl	%ebp, %esp
	popl	%ebp
	jmp	HIDENAME(cerror)
END(pdrfork_thread)

	.section .note.GNU-stack,"",%progbits