1/* $OpenBSD: tfork_thread.S,v 1.6 2015/04/21 01:24:28 guenther Exp $ */ 2/*- 3 * Copyright (c) 2000 Peter Wemm <peter@FreeBSD.org> 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28#include <machine/asm.h> 29#if 0 30__FBSDID("$FreeBSD: /repoman/r/ncvs/src/lib/libc/i386/gen/rfork_thread.S,v 1.5 2003/05/07 17:23:25 jhb Exp $"); 31#endif 32 33/* 34 * With thanks to John Dyson for the original version of this. 35 */ 36 37#include "SYS.h" 38 39/* 40 * 8 12 16 20 41 * __tfork_thread(param, psize, start_fnc, start_arg); 42 * 43 * param: Arguments to actual system call. 44 * psize: Other argument to pass to the actual kernel call. 45 * start_fnc: Address of thread function to call in child. 46 * start_arg: Argument to pass to the thread function in child. 47 */ 48 49ENTRY(__tfork_thread) 50 pushl %ebp 51 movl %esp, %ebp 52 pushl %esi 53 pushl %edi 54 55 /* 56 * Save the thread info in esi and ebx 57 */ 58 movl 16(%ebp), %esi # get start thread address 59 movl 20(%ebp), %edi # get start argument 60 61 /* 62 * Prepare and execute the thread creation syscall 63 */ 64 pushl 12(%ebp) # push psize 65 pushl 8(%ebp) # push param 66 pushl $0 # slot for return address, ignored by kernel 67 movl $SYS___tfork, %eax 68 int $0x80 69 jb 2f 70 71 /* 72 * Check to see if we are in the parent or child 73 */ 74 cmpl $0, %eax 75 jz 1f 76 addl $12, %esp 77 popl %edi 78 popl %esi 79 movl %ebp, %esp 80 popl %ebp 81 ret 82 .p2align 2 83 84 /* 85 * If we are in the child (new thread), then 86 * set-up the call to the internal subroutine. If it 87 * returns, then call __threxit. 88 */ 891: 90 xorl %ebp, %ebp # mark outermost frame 91 subl $4, %esp # align stack 92 andl $~15, %esp 93 addl $4, %esp 94 pushl %edi # push start argument 95 call *%esi 96 addl $4, %esp 97 98 /* 99 * Exit system call 100 */ 101 pushl $0 # NULL pointer argument to __threxit 102 pushl $0 # slot for return address, ignored by kernel 103 movl $SYS___threxit, %eax 104 int $0x80 105 106 /* 107 * Branch here if the thread creation fails: 108 */ 1092: 110 addl $12, %esp 111 popl %edi 112 popl %esi 113 movl %ebp, %esp 114 popl %ebp 115#ifdef __PIC__ 116 PIC_PROLOGUE 117 movl PIC_GOT(CERROR), %ecx 118 PIC_EPILOGUE 119 jmp *%ecx 120#else 121 jmp CERROR 122#endif 123