1/* Set current context.
2
3   Copyright (C) 2009-2021 Free Software Foundation, Inc.
4
5   This file is part of the GNU C Library.
6
7   The GNU C Library is free software; you can redistribute it and/or
8   modify it under the terms of the GNU Lesser General Public License as
9   published by the Free Software Foundation; either version 2.1 of the
10   License, or (at your option) any later version.
11
12   The GNU C Library is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15   Lesser General Public License for more details.
16
17   You should have received a copy of the GNU Lesser General Public
18   License along with the GNU C Library; if not, see
19   <https://www.gnu.org/licenses/>.  */
20
21#include <sysdep.h>
22#include "ucontext_i.h"
23#include "ucontext-internal.h"
24
25/*  int __setcontext (const ucontext_t *ucp)
26
27  Restores the machine context in UCP and thereby resumes execution
28  in that context.
29
30  This implementation is intended to be used for *synchronous* context
31  switches only.  Therefore, it does not have to restore anything
32  other than the PRESERVED state.  */
33
34	.text
35
36ENTRY (__setcontext)
37	PTR_ARG (0)
38	/* Save a copy of UCP.  */
39	mov	x9, x0
40
41	/* Set the signal mask with
42	   rt_sigprocmask (SIG_SETMASK, mask, NULL, _NSIG/8).  */
43	mov	x0, #SIG_SETMASK
44	add	x1, x9, #UCONTEXT_SIGMASK
45	mov	x2, #0
46	mov	x3, #_NSIG8
47	mov	x8, SYS_ify (rt_sigprocmask)
48	svc	0
49	cbz	x0, 1f
50	b	C_SYMBOL_NAME (__syscall_error)
511:
52	/* Restore the general purpose registers.  */
53	mov	x0, x9
54	cfi_def_cfa (x0, 0)
55	cfi_offset (x18, oX0 + 18 * SZREG)
56	cfi_offset (x19, oX0 + 19 * SZREG)
57	cfi_offset (x20, oX0 + 20 * SZREG)
58	cfi_offset (x21, oX0 + 21 * SZREG)
59	cfi_offset (x22, oX0 + 22 * SZREG)
60	cfi_offset (x23, oX0 + 23 * SZREG)
61	cfi_offset (x24, oX0 + 24 * SZREG)
62	cfi_offset (x25, oX0 + 25 * SZREG)
63	cfi_offset (x26, oX0 + 26 * SZREG)
64	cfi_offset (x27, oX0 + 27 * SZREG)
65	cfi_offset (x28, oX0 + 28 * SZREG)
66	cfi_offset (x29, oX0 + 29 * SZREG)
67	cfi_offset (x30, oX0 + 30 * SZREG)
68
69	cfi_offset ( d8, oV0 + 8 * SZVREG)
70	cfi_offset ( d9, oV0 + 9 * SZVREG)
71	cfi_offset (d10, oV0 + 10 * SZVREG)
72	cfi_offset (d11, oV0 + 11 * SZVREG)
73	cfi_offset (d12, oV0 + 12 * SZVREG)
74	cfi_offset (d13, oV0 + 13 * SZVREG)
75	cfi_offset (d14, oV0 + 14 * SZVREG)
76	cfi_offset (d15, oV0 + 15 * SZVREG)
77	ldp	x18, x19, [x0, oX0 + 18 * SZREG]
78	ldp	x20, x21, [x0, oX0 + 20 * SZREG]
79	ldp	x22, x23, [x0, oX0 + 22 * SZREG]
80	ldp	x24, x25, [x0, oX0 + 24 * SZREG]
81	ldp	x26, x27, [x0, oX0 + 26 * SZREG]
82	ldp	x28, x29, [x0, oX0 + 28 * SZREG]
83	ldr     x30,      [x0, oX0 + 30 * SZREG]
84	ldr     x2, [x0, oSP]
85	mov	sp, x2
86
87	/* Check for FP SIMD context.  We don't support restoring
88	   contexts created by the kernel, so this context must have
89	   been created by getcontext.  Hence we can rely on the
90	   first extension block being the FP SIMD context.  */
91	add     x2, x0, #oEXTENSION
92
93	mov	w3, #(FPSIMD_MAGIC & 0xffff)
94	movk	w3, #(FPSIMD_MAGIC >> 16), lsl #16
95	ldr	w1, [x2, #oHEAD + oMAGIC]
96	cmp	w1, w3
97	b.ne	2f
98
99	/* Restore the FP SIMD context.  */
100	add	x3, x2, #oV0 + 8 * SZVREG
101	ldp	 q8,  q9, [x3], #2 * SZVREG
102	ldp	q10, q11, [x3], #2 * SZVREG
103	ldp	q12, q13, [x3], #2 * SZVREG
104	ldp	q14, q15, [x3], #2 * SZVREG
105
106	add	x3, x2, oFPSR
107
108	ldr	w4, [x3]
109	msr	fpsr, x4
110
111	ldr	w4, [x3, oFPCR - oFPSR]
112	msr	fpcr, x4
113
1142:
115	ldr     x16, [x0, oPC]
116	/* Restore arg registers.  */
117	ldp	x2, x3, [x0, oX0 + 2 * SZREG]
118	ldp	x4, x5, [x0, oX0 + 4 * SZREG]
119	ldp	x6, x7, [x0, oX0 + 6 * SZREG]
120	ldp	x0, x1, [x0, oX0 + 0 * SZREG]
121	/* Jump to the new pc value.  */
122	br	x16
123PSEUDO_END (__setcontext)
124weak_alias (__setcontext, setcontext)
125
126ENTRY (__startcontext)
127	mov	x0, x19
128	cbnz	x0, __setcontext
1291:	b       HIDDEN_JUMPTARGET (exit)
130END (__startcontext)
131