posix: Fix posix_spawn invalid memory access
[glibc.git] / sysdeps / unix / sysv / linux / nios2 / setcontext.S
blobf10e8ffc0ff34a69006d4bb694877bbb9913fe36
1 /* Set current context.
2    Copyright (C) 2015-2016 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, see
17    <http://www.gnu.org/licenses/>.  */
19 #include <sysdep.h>
20 #include "ucontext_i.h"
22 /* int setcontext (const ucontext_t *ucp) */
23         .text
24 ENTRY(__setcontext)
25         ldw     r5, UCONTEXT_FLAGS(r4)
26         movi    r6, 1
27         bne     r5, r6, .Lsigreturn
29         mov     r10, r4
31         /* Restore signal mask.  */
32         /* rt_sigprocmask (SIG_SETMASK, &ucp->uc_sigmask, NULL, _NSIG8) */
33         movi    r7, _NSIG8
34         addi    r5, r4, UCONTEXT_SIGMASK
35         mov     r6, zero
36         movi    r4, SIG_SETMASK
37         movi    r2, SYS_ify (rt_sigprocmask)
38         trap
39         bne     r7, zero, SYSCALL_ERROR_LABEL
41         /* Restore argument registers, for the makecontext() case.  */
42         ldw     r4,   (UCONTEXT_MCONTEXT + 4*4)(r10)
43         ldw     r5,   (UCONTEXT_MCONTEXT + 5*4)(r10)
44         ldw     r6,   (UCONTEXT_MCONTEXT + 6*4)(r10)
45         ldw     r7,   (UCONTEXT_MCONTEXT + 7*4)(r10)
47         ldw     r16, (UCONTEXT_MCONTEXT + 16*4)(r10)
48         ldw     r17, (UCONTEXT_MCONTEXT + 17*4)(r10)
49         ldw     r18, (UCONTEXT_MCONTEXT + 18*4)(r10)
50         ldw     r19, (UCONTEXT_MCONTEXT + 19*4)(r10)
51         ldw     r20, (UCONTEXT_MCONTEXT + 20*4)(r10)
52         ldw     r21, (UCONTEXT_MCONTEXT + 21*4)(r10)
53         ldw     r22, (UCONTEXT_MCONTEXT + 22*4)(r10)
54         ldw     ra,  (UCONTEXT_MCONTEXT + 24*4)(r10)
55         ldw     fp,  (UCONTEXT_MCONTEXT + 25*4)(r10)
56         ldw     gp,  (UCONTEXT_MCONTEXT + 26*4)(r10)
57         /* Load address to continue execution.  */
58         ldw     r3,  (UCONTEXT_MCONTEXT + 28*4)(r10)
59         ldw     sp,  (UCONTEXT_MCONTEXT + 29*4)(r10)
61         mov     r2, zero
62         jmp     r3
64 .Lsigreturn:
65         addi    sp, sp, -RT_SIGFRAME_SIZE
66         cfi_adjust_cfa_offset (RT_SIGFRAME_SIZE)
68         addi    r2, sp, RT_SIGFRAME_UCONTEXT
69         movi    r3, UCONTEXT_SIZE-4
71         add     r6, r4, r3
72         ldw     r5, 0(r6)
73         add     r7, r2, r3
74         addi    r3, r3, -4
75         stw     r5, 0(r7)
76         bgt     r3, zero, 1b
78         movi    r2, SYS_ify (rt_sigreturn)
79         trap
81         addi    sp, sp, RT_SIGFRAME_SIZE
82         cfi_adjust_cfa_offset (-RT_SIGFRAME_SIZE)
83         br      SYSCALL_ERROR_LABEL
85 PSEUDO_END (__setcontext)
86 weak_alias (__setcontext, setcontext)
88 ENTRY(__startcontext)
89         mov     r4, r16
90         bne     r4, zero, __setcontext
92         /* If uc_link == zero, call exit.  */
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 END(__startcontext)