1/* $OpenBSD: tfork_thread.S,v 1.12 2023/12/10 16:45:51 deraadt Exp $ */ 2/*- 3 * Copyright (c) 2000 Peter Wemm <peter@FreeBSD.org> 4 * Copyright (c) 2003 Alan L. Cox <alc@cs.rice.edu> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29#include <machine/asm.h> 30 31/* 32 * With thanks to John Dyson for the original version of this. 33 */ 34 35#include "SYS.h" 36 37/* 38 * %rdi %rsi %rdx %rcx 39 * __tfork_thread(param, psize, start_fnc, start_arg); 40 * 41 * param: Argument to pass to the actual kernel call. 42 * psize: Other argument to pass to the actual kernel call. 43 * start_fnc: Address of thread function to call in child. 44 * start_arg: Argument to pass to the thread function in child. 45 */ 46 47ENTRY(__tfork_thread) 48 .cfi_startproc 49 RETGUARD_SETUP(__tfork_thread, r11); 50 RETGUARD_PUSH(r11); 51 movq %rdx, %r8 52 movq %rcx, %r9 53 54 /* 55 * Prepare and execute the thread creation syscall 56 */ 57 movl $SYS___tfork, %eax 58 .cfi_endproc 5999: syscall 60 PINSYSCALL(SYS___tfork, 99b) 61 jb 2f 62 63 /* 64 * Check to see if we are in the parent or child 65 */ 66 cmpl $0, %eax 67 jz 1f 68 jmp 3f 69 70 /* the retpoline we'll use to call the child's main */ 71 _ALIGN_TRAPS 72 .cfi_startproc 730: JMP_RETPOLINE(r8) 74 75 /* 76 * If we are in the child (new thread), then 77 * set-up the call to the internal subroutine. If it 78 * returns, then call __threxit. 79 */ 80 _ALIGN_TRAPS 811: 82 /* Mark top frame of new thread in CFI and with zero FP */ 83 .cfi_undefined rip 84 .cfi_undefined rsp 85 xorl %ebp, %ebp 86 movq %r9, %rdi 87 call 0b 88 89 /* 90 * Thread exit system call 91 */ 92 movl $SYS___threxit, %eax 93 xorl %edi, %edi 9498: syscall 95 PINSYSCALL(SYS___threxit, 98b) 96 int3 97 98 /* 99 * Branch here if the thread creation fails: 100 */ 1012: 102 SET_ERRNO 1033: 104 RETGUARD_POP(r11); 105 RETGUARD_CHECK(__tfork_thread, r11); 106 ret 107 .cfi_endproc 108END(__tfork_thread) 109