xref: /illumos-gate/usr/src/lib/libc/amd64/sys/getcontext.S (revision e3ae4b35c024af1196582063ecee3ab79367227d)
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21
22/*
23 * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24 * Use is subject to license terms.
25 */
26
27/*
28 * Copyright 2023 Oxide Computer Company
29 */
30
31	.file	"getcontext.s"
32
33#include <sys/asm_linkage.h>
34
35	ANSI_PRAGMA_WEAK(getcontext,function)
36	ANSI_PRAGMA_WEAK(swapcontext,function)
37
38#include "SYS.h"
39#include <../assym.h>
40
41/*
42 * getcontext() is written in assembler since it has to capture the correct
43 * machine state of the calle.
44 *
45 * As swapcontext() is actually equivalent to getcontext() + setcontext(),
46 * swapcontext() shares the most code with getcontext().
47 */
48
49#define	GETCONTEXT_IMPL(offset, func)						\
50	pushq	%rdi;		/* preserve the ucontext_t pointer */	\
51	call	func;		/* call syscall */			\
52	popq	%rdx;							\
53	andl	%eax, %eax;	/* if (error_return_from_syscall) */	\
54	je	1f;							\
55	addq	$offset, %rsp;						\
56	ret;			/*	then just return */		\
571:									\
58	/*								\
59	 * fix up %rsp and %rip						\
60	 */								\
61	addq	$UC_MCONTEXT_GREGS, %rdx;				\
62				/* &ucp->uc_mcontext.gregs */		\
63	movq	offset+0(%rsp), %rax;					\
64				/* read return PC from stack */		\
65	movq	%rax, RIP_OFF (%rdx);					\
66				/* store ret PC in EIP of env var */	\
67	leaq	offset+8(%rsp), %rax;					\
68				/* get caller's sp at time of call */	\
69	movq	%rax, RSP_OFF (%rdx);					\
70				/* store the sp into UESP of env var */	\
71	xorq	%rax, %rax;	/* return 0 */				\
72	movq	%rax, RAX_OFF (%rdx);					\
73				/* getcontext returns 0 after setcontext */
74
75/*
76 * int getcontext(ucontext_t *ucp)
77 */
78
79	ENTRY(getcontext)
80	GETCONTEXT_IMPL(0, __getcontext)
81	ret
82	SET_SIZE(getcontext)
83
84/*
85 * int swapcontext(ucontext_t *oucp, const ucontext_t *ucp)
86 */
87
88	ENTRY(swapcontext)
89	pushq	%rsi			/* preserve the 2nd argument */
90
91	GETCONTEXT_IMPL(8, __getcontext)
92
93	/* call setcontext */
94	popq	%rdi
95	call	setcontext
96	ret
97	SET_SIZE(swapcontext)
98
99/*
100 * int getcontext_extd(ucontext_t *ctx, uint32_t flags)
101 */
102	ENTRY(getcontext_extd)
103	cmpl	$0, %esi
104	jne	2f
105	GETCONTEXT_IMPL(0, __getcontext_extd)
106	ret
1072:
108	movl	$EINVAL, %eax		/* errno = EINVAL */
109	jmp	__cerror		/* return (-1) */
110	SET_SIZE(getcontext_extd)
111
112/*
113 * int swapcontext_extd(ucontext_t *oucp, uint32_t flags, const ucontext_t *ucp)
114 */
115	ENTRY(swapcontext_extd)
116	cmpl	$0, %esi
117	jne	2f
118	pushq	%rdx			/* preserve the 3rd argument */
119
120	GETCONTEXT_IMPL(8, __getcontext_extd)
121
122	/* call setcontext */
123	popq	%rdi
124	call	setcontext
125	ret
1262:
127	movl	$EINVAL, %eax		/* errno = EINVAL */
128	jmp	__cerror		/* return (-1) */
129	SET_SIZE(swapcontext_extd)
130