1/* Install given context.
2   Copyright (C) 2001-2022 Free Software Foundation, Inc.
3   This file is part of the GNU C Library.
4
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.
9
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.
14
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   <https://www.gnu.org/licenses/>.  */
18
19#include <sysdep.h>
20
21#include "ucontext_i.h"
22
23
24ENTRY(__setcontext)
25	/* Load address of the context data structure.  */
26	movl	4(%esp), %eax
27
28	/* Get the current signal mask.  */
29	subl	$12, %esp
30	cfi_adjust_cfa_offset (12)
31	movl	$0, 8(%esp)
32	leal	oSIGMASK(%eax), %eax
33	movl	%eax, 4(%esp)
34	movl	$SIG_SETMASK, (%esp)
35	call	HIDDEN_JUMPTARGET (__sigprocmask)
36	addl	$12, %esp
37	cfi_adjust_cfa_offset (-12)
38	testl	%eax, %eax
39	jne	L(pseudo_end)
40
41	/* EAX was modified, reload it.  */
42	movl	4(%esp), %eax
43
44	/* Restore the floating-point context.  Not the registers, only the
45	   rest.  */
46	leal	oFPREGS(%eax), %ecx
47	fldenv	(%ecx)
48
49	/* Restore the FS segment register.  We don't touch the GS register
50	   since it is used for threads.  */
51	movl	oFS(%eax), %ecx
52	movw	%cx, %fs
53
54	/* Fetch the address to return to.  */
55	movl	oEIP(%eax), %ecx
56
57	/* Load the new stack pointer.  */
58	cfi_def_cfa (eax, 0)
59	cfi_offset (edi, oEDI)
60	cfi_offset (esi, oESI)
61	cfi_offset (ebp, oEBP)
62	cfi_offset (ebx, oEBX)
63	cfi_offset (edx, oEDX)
64	cfi_offset (ecx, oECX)
65	movl	oESP(%eax), %esp
66
67	/* Push the return address on the new stack so we can return there.  */
68	pushl	%ecx
69
70	/* Load the values of all the 32-bit registers (except ESP).
71	   Since we are loading from EAX, it must be last.  */
72	movl	oEDI(%eax), %edi
73	movl	oESI(%eax), %esi
74	movl	oEBP(%eax), %ebp
75	movl	oEBX(%eax), %ebx
76	movl	oEDX(%eax), %edx
77	movl	oECX(%eax), %ecx
78	movl	oEAX(%eax), %eax
79
80	/* End FDE here, we fall into another context.  */
81	cfi_endproc
82	cfi_startproc
83
84	/* The following 'ret' will pop the address of the code and jump
85	   to it.  */
86
87L(pseudo_end):
88	ret
89PSEUDO_END(__setcontext)
90libc_hidden_def (__setcontext)
91
92weak_alias (__setcontext, setcontext)
93