xref: /freebsd/lib/libsys/amd64/pdrfork_thread.S (revision 9c151e1fbf1a71a4912afa9693a39d55a00db4eb)
1/*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright 2000 Peter Wemm <peter@FreeBSD.org>
5 * Copyright 2003 Alan L. Cox <alc@cs.rice.edu>
6 * Copyright 2026 The FreeBSD Foundation
7 * All rights reserved.
8 *
9 * Portions of this software were developed by
10 * Konstantin Belousov <kib@FreeBSD.org> under sponsorship from
11 * the FreeBSD Foundation.
12 */
13
14#include <machine/asm.h>
15/*
16 * With thanks to John Dyson for the original version of this.
17 */
18
19#include <SYS.h>
20
21/*
22 *                %rdi %esi     %rdx     %rcx        %r8        %r9
23 * pdrfork_thread(fdp, pdflags, rfflags, stack_addr, start_fnc, start_arg);
24 *
25 * fdp			Pointer for the resulting fd location
26 * pdflags		Flags as to pdfork.
27 * rfflags:		Flags as to rfork.
28 * stack_addr:		Top of stack for thread.
29 * start_fnc:		Address of thread function to call in child.
30 * start_arg:		Argument to pass to the thread function in child.
31 */
32
33ENTRY(pdrfork_thread)
34	pushq	%rbx
35	pushq	%r12
36	pushq	%r13
37	movq	%r8, %rbx
38	movq	%r9, %r12
39	movq	%rcx, %r13
40
41	/*
42	 * Prepare and execute the thread creation syscall
43	 */
44	_SYSCALL(pdrfork)
45	jb 	2f
46
47	/*
48	 * Check to see if we are in the parent or child
49	 */
50	cmpl	$0, %edx
51	jnz	1f
52	popq	%r13
53	popq	%r12
54	popq	%rbx
55	ret
56
57	/*
58	 * If we are in the child (new thread), then
59	 * set-up the call to the internal subroutine.  If it
60	 * returns, then call __exit.
61	 */
621:
63	movq	%r13, %rsp
64	movq	%r12, %rdi
65	call	*%rbx
66	movl	%eax, %edi
67
68	/*
69	 * Exit system call
70	 */
71	_SYSCALL(exit)
72
73	/*
74	 * Branch here if the thread creation fails:
75	 */
762:
77	popq	%r13
78	popq	%r12
79	popq	%rbx
80	jmp	HIDENAME(cerror)
81END(pdrfork_thread)
82
83	.section .note.GNU-stack,"",%progbits
84