yuezonghe | 824eb0c | 2024-06-27 02:32:26 -0700 | [diff] [blame^] | 1 | /* Copyright (C) 1996, 1997, 2000 Free Software Foundation, Inc. |
| 2 | This file is part of the GNU C Library. |
| 3 | Contributed by David Huggins-Daines <dhd@debian.org>, 2000. |
| 4 | Based on the Alpha version by Richard Henderson <rth@tamu.edu>, 1996. |
| 5 | |
| 6 | The GNU C Library is free software; you can redistribute it and/or |
| 7 | modify it under the terms of the GNU Lesser General Public |
| 8 | License as published by the Free Software Foundation; either |
| 9 | version 2.1 of the License, or (at your option) any later version. |
| 10 | |
| 11 | The GNU C Library is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | Lesser General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU Lesser General Public |
| 17 | License along with the GNU C Library; if not, write to the Free |
| 18 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 19 | 02111-1307 USA. */ |
| 20 | |
| 21 | /* clone() is even more special than fork() as it mucks with stacks |
| 22 | and invokes a function in the right context after its all over. */ |
| 23 | |
| 24 | #include <asm/unistd.h> |
| 25 | #define _ERRNO_H 1 |
| 26 | #include <bits/errno.h> |
| 27 | #include <sys/syscall.h> |
| 28 | |
| 29 | /* Non-thread code calls __clone with the following parameters: |
| 30 | int clone(int (*fn)(void *arg), void *child_stack, int flags, void *arg) |
| 31 | |
| 32 | NPTL Code will call __clone with the following parameters: |
| 33 | int clone(int (*fn)(void *arg), void *child_stack, int flags, void *arg, |
| 34 | int *parent_tidptr, struct user_desc *newtls, int *child_pidptr) |
| 35 | |
| 36 | The code should not mangle the extra input registers. |
| 37 | Syscall expects: Input to __clone: |
| 38 | 4(r25) - function pointer (r26, arg0) |
| 39 | 0(r25) - argument (r23, arg3) |
| 40 | r26 - clone flags. (r24, arg2) |
| 41 | r25+64 - user stack pointer. (r25, arg1) |
| 42 | r24 - parent tid pointer. (stack - 52) |
| 43 | r23 - struct user_desc newtls pointer. (stack - 56) |
| 44 | r22 - child tid pointer. (stack - 60) |
| 45 | r20 - clone syscall number (constant) |
| 46 | */ |
| 47 | |
| 48 | .text |
| 49 | .global __clone |
| 50 | .type __clone,%function |
| 51 | __clone: |
| 52 | |
| 53 | /* Sanity check arguments. */ |
| 54 | ldi -EINVAL,%ret0 |
| 55 | comib,=,n 0,%arg0,.Lerror /* no NULL function pointers */ |
| 56 | comib,=,n 0,%arg1,.Lerror /* no NULL stack pointers */ |
| 57 | |
| 58 | /* Save the fn ptr and arg on the new stack. */ |
| 59 | stwm %r26,64(%r25) |
| 60 | stw %r23,-60(%r25) |
| 61 | /* Clone arguments are (int flags, void * child_stack) */ |
| 62 | copy %r24,%r26 /* flags are first */ |
| 63 | /* User stack pointer is in the correct register already */ |
| 64 | |
| 65 | /* Load args from stack... */ |
| 66 | ldw -52(%sp), %r24 /* Load parent_tidptr */ |
| 67 | ldw -56(%sp), %r23 /* Load newtls */ |
| 68 | ldw -60(%sp), %r22 /* Load child_tidptr */ |
| 69 | |
| 70 | /* Create frame to get r3 free */ |
| 71 | copy %sp, %r21 |
| 72 | stwm %r3, 64(%sp) |
| 73 | stw %r21, -4(%sp) |
| 74 | |
| 75 | /* Save the PIC register. */ |
| 76 | #ifdef __PIC__ |
| 77 | copy %r19, %r3 /* parent */ |
| 78 | #endif |
| 79 | |
| 80 | /* Do the system call */ |
| 81 | ble 0x100(%sr2,%r0) |
| 82 | ldi __NR_clone,%r20 |
| 83 | |
| 84 | ldi -4096,%r1 |
| 85 | comclr,>>= %r1,%ret0,%r0 /* Note: unsigned compare. */ |
| 86 | b,n .LerrorRest |
| 87 | |
| 88 | comib,=,n 0,%ret0,thread_start |
| 89 | |
| 90 | /* Successful return from the parent |
| 91 | No need to restore the PIC register, |
| 92 | since we return immediately. */ |
| 93 | |
| 94 | bv %r0(%rp) |
| 95 | ldwm -64(%sp), %r3 |
| 96 | |
| 97 | .LerrorRest: |
| 98 | /* Restore the PIC register on error */ |
| 99 | #ifdef __PIC__ |
| 100 | copy %r3, %r19 /* parent */ |
| 101 | #endif |
| 102 | |
| 103 | /* Something bad happened -- no child created */ |
| 104 | .Lerror: |
| 105 | |
| 106 | /* Set errno, save ret0 so we return with that value. */ |
| 107 | copy %ret0, %r3 |
| 108 | b __syscall_error |
| 109 | sub %r0,%ret0,%arg0 |
| 110 | copy %r3, %ret0 |
| 111 | /* Return after setting errno, and restoring ret0 */ |
| 112 | bv %r0(%rp) |
| 113 | ldwm -64(%sp), %r3 |
| 114 | |
| 115 | thread_start: |
| 116 | |
| 117 | /* Load up the arguments. */ |
| 118 | ldw -60(%sr0, %sp),%arg0 |
| 119 | ldw -64(%sr0, %sp),%r22 |
| 120 | |
| 121 | /* $$dyncall fixes childs PIC register */ |
| 122 | |
| 123 | /* Call the user's function */ |
| 124 | bl $$dyncall,%r31 |
| 125 | copy %r31,%rp |
| 126 | |
| 127 | bl HIDDEN_JUMPTARGET(_exit),%rp |
| 128 | copy %ret0,%arg0 |
| 129 | |
| 130 | /* Die horribly. */ |
| 131 | iitlbp %r0,(%sr0,%r0) |
| 132 | |
| 133 | .size clone,.-clone |
| 134 | |
| 135 | weak_alias (__clone, clone) |