xref: /freebsd/sys/x86/linux/linux_x86_sigframe.h (revision f126890ac5386406dadf7c4cfa9566cbb56537c5)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2004 Tim J. Robbins
5  * Copyright (c) 2001 Doug Rabson
6  * Copyright (c) 1994-1996 Søren Schmidt
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer
14  *    in this position and unchanged.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #ifndef _X86_LINUX_SIGFRAME_H_
34 #define	_X86_LINUX_SIGFRAME_H_
35 
36 #define	LINUX_UC_FP_XSTATE		0x1
37 
38 #define	LINUX_FP_XSTATE_MAGIC1		0x46505853U
39 #define	LINUX_FP_XSTATE_MAGIC2		0x46505845U
40 #define	LINUX_FP_XSTATE_MAGIC2_SIZE	sizeof(uint32_t)
41 
42 struct l_fpx_sw_bytes {
43 	uint32_t	magic1;
44 	uint32_t	extended_size;
45 	uint64_t	xfeatures;
46 	uint32_t	xstate_size;
47 	uint32_t	padding[7];
48 };
49 
50 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
51 
52 /* The Linux sigcontext, pretty much a standard 386 trapframe. */
53 struct l_sigcontext {
54 	l_uint		sc_gs;
55 	l_uint		sc_fs;
56 	l_uint		sc_es;
57 	l_uint		sc_ds;
58 	l_uint		sc_edi;
59 	l_uint		sc_esi;
60 	l_uint		sc_ebp;
61 	l_uint		sc_esp;
62 	l_uint		sc_ebx;
63 	l_uint		sc_edx;
64 	l_uint		sc_ecx;
65 	l_uint		sc_eax;
66 	l_uint		sc_trapno;
67 	l_uint		sc_err;
68 	l_uint		sc_eip;
69 	l_uint		sc_cs;
70 	l_uint		sc_eflags;
71 	l_uint		sc_esp_at_signal;
72 	l_uint		sc_ss;
73 	l_uint		sc_387;
74 	l_uint		sc_mask;
75 	l_uint		sc_cr2;
76 };
77 
78 struct l_ucontext {
79 	l_ulong		uc_flags;
80 	l_uintptr_t	uc_link;
81 	l_stack_t	uc_stack;
82 	struct l_sigcontext	uc_mcontext;
83 	l_sigset_t	uc_sigmask;
84 } __packed;
85 
86 struct l_fpreg {
87 	u_int16_t	significand[4];
88 	u_int16_t	exponent;
89 };
90 
91 struct l_fpxreg {
92 	u_int16_t	significand[4];
93 	u_int16_t	exponent;
94 	u_int16_t	padding[3];
95 };
96 
97 struct l_xmmreg {
98 	u_int32_t	element[4];
99 };
100 
101 struct l_fpstate {
102 	/* Regular FPU environment */
103 	u_int32_t		cw;
104 	u_int32_t		sw;
105 	u_int32_t		tag;
106 	u_int32_t		ipoff;
107 	u_int32_t		cssel;
108 	u_int32_t		dataoff;
109 	u_int32_t		datasel;
110 	struct l_fpreg		_st[8];
111 	u_int16_t		status;
112 	u_int16_t		magic;		/* 0xffff = regular FPU data */
113 
114 	/* FXSR FPU environment */
115 	u_int32_t		_fxsr_env[6];	/* env is ignored. */
116 	u_int32_t		mxcsr;
117 	u_int32_t		reserved;
118 	struct l_fpxreg		_fxsr_st[8];	/* reg data is ignored. */
119 	struct l_xmmreg		_xmm[8];
120 	u_int32_t		padding[56];
121 };
122 
123 /*
124  * We make the stack look like Linux expects it when calling a signal
125  * handler, but use the BSD way of calling the handler and sigreturn().
126  */
127 struct l_sigframe {
128 	l_int			sf_sig;
129 	struct l_sigcontext	sf_sc;
130 	struct l_fpstate	sf_fpstate;
131 	sigset_t		sf_sigmask;
132 };
133 
134 struct l_rt_sigframe {
135 	l_int			sf_sig;
136 	l_uintptr_t		sf_siginfo;
137 	l_uintptr_t		sf_ucontext;
138 	l_siginfo_t		sf_si;
139 	struct l_ucontext	sf_uc;
140 };
141 
142 #else
143 
144 struct l_fpstate {
145 	u_int16_t cwd;
146 	u_int16_t swd;
147 	u_int16_t twd;
148 	u_int16_t fop;
149 	u_int64_t rip;
150 	u_int64_t rdp;
151 	u_int32_t mxcsr;
152 	u_int32_t mxcsr_mask;
153 	u_int8_t st[8][16];
154 	u_int8_t xmm[16][16];
155 	u_int32_t reserved2[12];
156 	union {
157 		u_int32_t		reserved3[12];
158 		struct l_fpx_sw_bytes	sw_reserved;
159 	};
160 } __aligned(16);
161 
162 struct l_sigcontext {
163 	l_ulong		sc_r8;
164 	l_ulong		sc_r9;
165 	l_ulong		sc_r10;
166 	l_ulong		sc_r11;
167 	l_ulong		sc_r12;
168 	l_ulong		sc_r13;
169 	l_ulong		sc_r14;
170 	l_ulong		sc_r15;
171 	l_ulong		sc_rdi;
172 	l_ulong		sc_rsi;
173 	l_ulong		sc_rbp;
174 	l_ulong		sc_rbx;
175 	l_ulong		sc_rdx;
176 	l_ulong		sc_rax;
177 	l_ulong		sc_rcx;
178 	l_ulong		sc_rsp;
179 	l_ulong		sc_rip;
180 	l_ulong		sc_rflags;
181 	l_ushort	sc_cs;
182 	l_ushort	sc_gs;
183 	l_ushort	sc_fs;
184 	l_ushort	sc___pad0;
185 	l_ulong		sc_err;
186 	l_ulong		sc_trapno;
187 	l_sigset_t	sc_mask;
188 	l_ulong		sc_cr2;
189 	/*
190 	 * On Linux sc_fpstate is (struct l_fpstate *) or (struct l_xstate *)
191 	 * depending on the FP_XSTATE_MAGIC1 encoded in the sw_reserved
192 	 * bytes of (struct l_fpstate) and FP_XSTATE_MAGIC2 present at the end
193 	 * of extended memory layout.
194 	 */
195 	l_uintptr_t	sc_fpstate;
196 	l_ulong		sc_reserved1[8];
197 };
198 
199 struct l_ucontext {
200 	l_ulong		uc_flags;
201 	l_uintptr_t	uc_link;
202 	l_stack_t	uc_stack;
203 	struct l_sigcontext	uc_mcontext;
204 	l_sigset_t	uc_sigmask;
205 };
206 
207 /*
208  * We make the stack look like Linux expects it when calling a signal
209  * handler, but use the BSD way of calling the handler and sigreturn().
210  */
211 struct l_rt_sigframe {
212 	struct l_ucontext	sf_uc;
213 	struct l_siginfo	sf_si;
214 };
215 
216 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
217 
218 #endif /* !_X86_LINUX_SIGFRAME_H_ */
219