]>
Commit | Line | Data |
---|---|---|
522e6ee3 | 1 | /* clone() implementation for Nios II. |
f7a9f785 | 2 | Copyright (C) 2008-2016 Free Software Foundation, Inc. |
522e6ee3 CLT |
3 | This file is part of the GNU C Library. |
4 | Contributed by Andrew Jenner <andrew@codesourcery.com>, 2008. | |
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, see | |
18 | <http://www.gnu.org/licenses/>. */ | |
19 | ||
20 | /* clone() is even more special than fork() as it mucks with stacks | |
21 | and invokes a function in the right context after its all over. */ | |
22 | ||
23 | #include <sysdep.h> | |
24 | #define _ERRNO_H 1 | |
25 | #include <bits/errno.h> | |
26 | #include <tcb-offsets.h> | |
27 | ||
28 | #define CLONE_VM 0x00000100 | |
29 | #define CLONE_THREAD 0x00010000 | |
30 | ||
31 | /* int clone(int (*fn)(void *arg), void *child_stack, int flags, void *arg, | |
32 | void *parent_tidptr, void *tls, void *child_tidptr) */ | |
33 | ||
34 | .text | |
35 | ENTRY(__clone) | |
36 | /* Sanity check arguments. */ | |
37 | movi r2, EINVAL | |
38 | /* No NULL function pointers. */ | |
39 | beq r4, zero, SYSCALL_ERROR_LABEL | |
40 | /* No NULL stack pointers. */ | |
41 | beq r5, zero, SYSCALL_ERROR_LABEL | |
42 | ||
43 | subi r5, r5, 8 /* Reserve argument save space. */ | |
44 | stw r4, 4(r5) /* Save function pointer. */ | |
45 | stw r7, 0(r5) /* Save argument pointer. */ | |
46 | ||
47 | /* Load arguments. */ | |
48 | mov r4, r6 | |
49 | ldw r6, 0(sp) | |
50 | ldw r7, 8(sp) | |
51 | ldw r8, 4(sp) | |
52 | ||
53 | /* Do the system call. */ | |
54 | movi r2, SYS_ify (clone) | |
55 | ||
56 | /* End FDE now, because in the child the unwind info will be | |
57 | wrong. */ | |
58 | cfi_endproc | |
59 | trap | |
60 | ||
61 | /* Check for errors. */ | |
62 | bne r7, zero, SYSCALL_ERROR_LABEL | |
63 | /* See if we're on the newly created thread. */ | |
64 | beq r2, zero, thread_start | |
65 | /* Successful return from the parent */ | |
66 | ret | |
67 | ||
68 | thread_start: | |
69 | cfi_startproc | |
70 | cfi_undefined (ra) | |
71 | ||
72 | /* We expect the argument registers to be preserved across system | |
73 | calls and across task cloning, so flags should be in r4 here. */ | |
74 | andhi r2, r4, %hi(CLONE_THREAD) | |
75 | bne r2, zero, 2f | |
76 | andi r3, r4, CLONE_VM | |
77 | movi r2, -1 | |
78 | bne r3, zero, 3f | |
79 | DO_CALL (getpid, 0) | |
80 | 3: | |
81 | stw r2, PID_OFFSET(r23) | |
82 | stw r2, TID_OFFSET(r23) | |
83 | 2: | |
84 | ldw r5, 4(sp) /* Function pointer. */ | |
85 | ldw r4, 0(sp) /* Argument pointer. */ | |
86 | addi sp, sp, 8 | |
87 | ||
88 | /* Call the user's function. */ | |
89 | callr r5 | |
90 | ||
91 | /* _exit with the result. */ | |
92 | mov r4, r2 | |
93 | #ifdef PIC | |
94 | nextpc r22 | |
95 | 1: movhi r8, %hiadj(_gp_got - 1b) | |
96 | addi r8, r8, %lo(_gp_got - 1b) | |
97 | add r22, r22, r8 | |
98 | ldw r8, %call(HIDDEN_JUMPTARGET(_exit))(r22) | |
99 | jmp r8 | |
100 | #else | |
101 | jmpi _exit | |
102 | #endif | |
103 | cfi_endproc | |
104 | ||
105 | cfi_startproc | |
106 | PSEUDO_END (__clone) | |
107 | weak_alias (__clone, clone) |