1*0a6a1f1dSLionel Sambuc/* $NetBSD: __clone.S,v 1.5 2014/05/22 15:01:57 uebayasi Exp $ */ 22fe8fb19SBen Gras 32fe8fb19SBen Gras/* 42fe8fb19SBen Gras * Copyright (c) 2002 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 pushq %r12 522fe8fb19SBen Gras pushq %r13 532fe8fb19SBen Gras /* 542fe8fb19SBen Gras * Sanity checks: func and stack may not be NULL. 552fe8fb19SBen Gras */ 562fe8fb19SBen Gras testq %rdi,%rdi 572fe8fb19SBen Gras je 3f 582fe8fb19SBen Gras testq %rsi,%rsi 592fe8fb19SBen Gras je 3f 602fe8fb19SBen Gras 612fe8fb19SBen Gras 622fe8fb19SBen Gras movq %rdi,%r12 632fe8fb19SBen Gras movq %rcx,%r13 642fe8fb19SBen Gras 652fe8fb19SBen Gras movq %rdx,%rdi 662fe8fb19SBen Gras 672fe8fb19SBen Gras pushq $0 /* dummy return address */ 682fe8fb19SBen Gras 692fe8fb19SBen Gras SYSTRAP(__clone) 702fe8fb19SBen Gras jc 4f 712fe8fb19SBen Gras cmpl $0,%eax 722fe8fb19SBen Gras jne 2f /* we're the parent */ 732fe8fb19SBen Gras movq %r13,%rdi /* restore argument */ 742fe8fb19SBen Gras call *%r12 /* this is the clone, call the function */ 752fe8fb19SBen Gras 762fe8fb19SBen Gras movq %rax,%rdi 7784d9c625SLionel Sambuc#ifdef __PIC__ 782fe8fb19SBen Gras call PIC_PLT(_C_LABEL(_exit)) 792fe8fb19SBen Gras#else 802fe8fb19SBen Gras call _C_LABEL(_exit) 812fe8fb19SBen Gras#endif 822fe8fb19SBen Gras 832fe8fb19SBen Gras2: 842fe8fb19SBen Gras addq $8,%rsp 852fe8fb19SBen Gras popq %r13 862fe8fb19SBen Gras popq %r12 872fe8fb19SBen Gras ret 882fe8fb19SBen Gras3: 892fe8fb19SBen Gras movl $EINVAL,%eax 902fe8fb19SBen Gras jmp 5f 912fe8fb19SBen Gras4: 922fe8fb19SBen Gras addq $8,%rsp 932fe8fb19SBen Gras5: 942fe8fb19SBen Gras popq %r13 952fe8fb19SBen Gras popq %r12 962fe8fb19SBen Gras jmp CERROR 97*0a6a1f1dSLionel SambucEND(__clone) 98