1*0d34bfa2Suebayasi/* $NetBSD: __clone.S,v 1.6 2014/05/23 02:34:19 uebayasi Exp $ */ 2017c6632Sfvdl 3017c6632Sfvdl/* 4017c6632Sfvdl * Copyright (c) 2001 Wasabi Systems, Inc. 5017c6632Sfvdl * All rights reserved. 6017c6632Sfvdl * 7017c6632Sfvdl * Written by Frank van der Linden for Wasabi Systems, Inc. 8017c6632Sfvdl * 9017c6632Sfvdl * Redistribution and use in source and binary forms, with or without 10017c6632Sfvdl * modification, are permitted provided that the following conditions 11017c6632Sfvdl * are met: 12017c6632Sfvdl * 1. Redistributions of source code must retain the above copyright 13017c6632Sfvdl * notice, this list of conditions and the following disclaimer. 14017c6632Sfvdl * 2. Redistributions in binary form must reproduce the above copyright 15017c6632Sfvdl * notice, this list of conditions and the following disclaimer in the 16017c6632Sfvdl * documentation and/or other materials provided with the distribution. 17017c6632Sfvdl * 3. All advertising materials mentioning features or use of this software 18017c6632Sfvdl * must display the following acknowledgement: 19017c6632Sfvdl * This product includes software developed for the NetBSD Project by 20017c6632Sfvdl * Wasabi Systems, Inc. 21017c6632Sfvdl * 4. The name of Wasabi Systems, Inc. may not be used to endorse 22017c6632Sfvdl * or promote products derived from this software without specific prior 23017c6632Sfvdl * written permission. 24017c6632Sfvdl * 25017c6632Sfvdl * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND 26017c6632Sfvdl * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 27017c6632Sfvdl * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 28017c6632Sfvdl * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC 29017c6632Sfvdl * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 30017c6632Sfvdl * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 31017c6632Sfvdl * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 32017c6632Sfvdl * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 33017c6632Sfvdl * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 34017c6632Sfvdl * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 35017c6632Sfvdl * POSSIBILITY OF SUCH DAMAGE. 36017c6632Sfvdl */ 37017c6632Sfvdl 38017c6632Sfvdl#include <machine/asm.h> 39017c6632Sfvdl#include <sys/errno.h> 40017c6632Sfvdl#include "SYS.h" 41017c6632Sfvdl 42017c6632Sfvdl#ifdef WEAK_ALIAS 43017c6632SfvdlWEAK_ALIAS(clone, __clone) 44017c6632Sfvdl#endif 45017c6632Sfvdl .text 46017c6632Sfvdl 47da2e0bfbSthorpej/* 48da2e0bfbSthorpej * int clone(int (*fn)(void *), void *stack, int flags, void *arg); 49da2e0bfbSthorpej */ 50017c6632SfvdlENTRY(__clone) 51017c6632Sfvdl pushl %ebp 52017c6632Sfvdl 53017c6632Sfvdl /* 54017c6632Sfvdl * Sanity checks: func and stack may not be NULL. 55017c6632Sfvdl */ 56017c6632Sfvdl movl 8(%esp), %ebp 57017c6632Sfvdl cmpl $0,%ebp /* function */ 58017c6632Sfvdl je 3f 59017c6632Sfvdl movl 12(%esp),%eax /* stack */ 60017c6632Sfvdl cmpl $0,%eax 61017c6632Sfvdl je 3f 62017c6632Sfvdl 63017c6632Sfvdl /* 64017c6632Sfvdl * Set up the stack for the clone. 65017c6632Sfvdl */ 66017c6632Sfvdl movl 20(%esp),%ecx 67017c6632Sfvdl movl %ecx,-4(%eax) /* argument */ 68da2e0bfbSthorpej leal -4(%eax),%eax /* sp points to arg */ 69017c6632Sfvdl 70017c6632Sfvdl pushl %eax /* stack */ 71017c6632Sfvdl pushl 20(%esp) /* flags */ 72017c6632Sfvdl pushl $0 /* dummy return address */ 73017c6632Sfvdl 74017c6632Sfvdl SYSTRAP(__clone) 75017c6632Sfvdl jc 4f 76017c6632Sfvdl cmpl $0,%eax 77017c6632Sfvdl jne 2f /* we're the parent */ 78da2e0bfbSthorpej call *%ebp /* this is the clone, call the function */ 79da2e0bfbSthorpej 804d12bfcdSjoerg#ifdef __PIC__ 81017c6632Sfvdl PIC_PROLOGUE 82d34ad42fSfvdl pushl %eax /* clone does _exit(func(arg)); */ 83017c6632Sfvdl call PIC_PLT(_C_LABEL(_exit)) 84d34ad42fSfvdl addl $4,%esp 85017c6632Sfvdl PIC_EPILOGUE 86017c6632Sfvdl#else 87d34ad42fSfvdl pushl %eax 88d34ad42fSfvdl call _C_LABEL(_exit) 89d34ad42fSfvdl addl $4,%esp 90017c6632Sfvdl#endif 91017c6632Sfvdl 92017c6632Sfvdl2: 93017c6632Sfvdl addl $12,%esp 94017c6632Sfvdl popl %ebp 95017c6632Sfvdl ret 96017c6632Sfvdl3: 97017c6632Sfvdl movl $EINVAL,%eax 98017c6632Sfvdl jmp 5f 99017c6632Sfvdl4: 100017c6632Sfvdl addl $12,%esp 101017c6632Sfvdl5: 102017c6632Sfvdl popl %ebp 103017c6632Sfvdl jmp CERROR 104*0d34bfa2SuebayasiEND(__clone) 105