1*0a6a1f1dSLionel Sambuc/* $NetBSD: __clone.S,v 1.6 2014/05/23 02:34:19 uebayasi Exp $ */ 22fe8fb19SBen Gras 32fe8fb19SBen Gras/* 42fe8fb19SBen Gras * Copyright (c) 2001 Wasabi Systems, Inc. 52fe8fb19SBen Gras * All rights reserved. 62fe8fb19SBen Gras * 72fe8fb19SBen Gras * Written by Frank van der Linden for Wasabi Systems, Inc. 82fe8fb19SBen Gras * 92fe8fb19SBen Gras * Redistribution and use in source and binary forms, with or without 102fe8fb19SBen Gras * modification, are permitted provided that the following conditions 112fe8fb19SBen Gras * are met: 122fe8fb19SBen Gras * 1. Redistributions of source code must retain the above copyright 132fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer. 142fe8fb19SBen Gras * 2. Redistributions in binary form must reproduce the above copyright 152fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer in the 162fe8fb19SBen Gras * documentation and/or other materials provided with the distribution. 172fe8fb19SBen Gras * 3. All advertising materials mentioning features or use of this software 182fe8fb19SBen Gras * must display the following acknowledgement: 192fe8fb19SBen Gras * This product includes software developed for the NetBSD Project by 202fe8fb19SBen Gras * Wasabi Systems, Inc. 212fe8fb19SBen Gras * 4. The name of Wasabi Systems, Inc. may not be used to endorse 222fe8fb19SBen Gras * or promote products derived from this software without specific prior 232fe8fb19SBen Gras * written permission. 242fe8fb19SBen Gras * 252fe8fb19SBen Gras * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND 262fe8fb19SBen Gras * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 272fe8fb19SBen Gras * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 282fe8fb19SBen Gras * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC 292fe8fb19SBen Gras * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 302fe8fb19SBen Gras * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 312fe8fb19SBen Gras * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 322fe8fb19SBen Gras * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 332fe8fb19SBen Gras * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 342fe8fb19SBen Gras * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 352fe8fb19SBen Gras * POSSIBILITY OF SUCH DAMAGE. 362fe8fb19SBen Gras */ 372fe8fb19SBen Gras 382fe8fb19SBen Gras#include <machine/asm.h> 392fe8fb19SBen Gras#include <sys/errno.h> 402fe8fb19SBen Gras#include "SYS.h" 412fe8fb19SBen Gras 422fe8fb19SBen Gras#ifdef WEAK_ALIAS 432fe8fb19SBen GrasWEAK_ALIAS(clone, __clone) 442fe8fb19SBen Gras#endif 452fe8fb19SBen Gras .text 462fe8fb19SBen Gras 472fe8fb19SBen Gras/* 482fe8fb19SBen Gras * int clone(int (*fn)(void *), void *stack, int flags, void *arg); 492fe8fb19SBen Gras */ 502fe8fb19SBen GrasENTRY(__clone) 512fe8fb19SBen Gras pushl %ebp 522fe8fb19SBen Gras 532fe8fb19SBen Gras /* 542fe8fb19SBen Gras * Sanity checks: func and stack may not be NULL. 552fe8fb19SBen Gras */ 562fe8fb19SBen Gras movl 8(%esp), %ebp 572fe8fb19SBen Gras cmpl $0,%ebp /* function */ 582fe8fb19SBen Gras je 3f 592fe8fb19SBen Gras movl 12(%esp),%eax /* stack */ 602fe8fb19SBen Gras cmpl $0,%eax 612fe8fb19SBen Gras je 3f 622fe8fb19SBen Gras 632fe8fb19SBen Gras /* 642fe8fb19SBen Gras * Set up the stack for the clone. 652fe8fb19SBen Gras */ 662fe8fb19SBen Gras movl 20(%esp),%ecx 672fe8fb19SBen Gras movl %ecx,-4(%eax) /* argument */ 682fe8fb19SBen Gras leal -4(%eax),%eax /* sp points to arg */ 692fe8fb19SBen Gras 702fe8fb19SBen Gras pushl %eax /* stack */ 712fe8fb19SBen Gras pushl 20(%esp) /* flags */ 722fe8fb19SBen Gras pushl $0 /* dummy return address */ 732fe8fb19SBen Gras 742fe8fb19SBen Gras SYSTRAP(__clone) 752fe8fb19SBen Gras jc 4f 762fe8fb19SBen Gras cmpl $0,%eax 772fe8fb19SBen Gras jne 2f /* we're the parent */ 782fe8fb19SBen Gras call *%ebp /* this is the clone, call the function */ 792fe8fb19SBen Gras 8084d9c625SLionel Sambuc#ifdef __PIC__ 812fe8fb19SBen Gras PIC_PROLOGUE 822fe8fb19SBen Gras pushl %eax /* clone does _exit(func(arg)); */ 832fe8fb19SBen Gras call PIC_PLT(_C_LABEL(_exit)) 842fe8fb19SBen Gras addl $4,%esp 852fe8fb19SBen Gras PIC_EPILOGUE 862fe8fb19SBen Gras#else 872fe8fb19SBen Gras pushl %eax 882fe8fb19SBen Gras call _C_LABEL(_exit) 892fe8fb19SBen Gras addl $4,%esp 902fe8fb19SBen Gras#endif 912fe8fb19SBen Gras 922fe8fb19SBen Gras2: 932fe8fb19SBen Gras addl $12,%esp 942fe8fb19SBen Gras popl %ebp 952fe8fb19SBen Gras ret 962fe8fb19SBen Gras3: 972fe8fb19SBen Gras movl $EINVAL,%eax 982fe8fb19SBen Gras jmp 5f 992fe8fb19SBen Gras4: 1002fe8fb19SBen Gras addl $12,%esp 1012fe8fb19SBen Gras5: 1022fe8fb19SBen Gras popl %ebp 1032fe8fb19SBen Gras jmp CERROR 104*0a6a1f1dSLionel SambucEND(__clone) 105