xref: /freebsd/lib/libc/x86/gen/getcontextx.c (revision 61e21613)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2011 Konstantin Belousov <kib@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/types.h>
30 #include <sys/ucontext.h>
31 #include <errno.h>
32 #include <stdarg.h>
33 #include <stdlib.h>
34 #include <machine/cpufunc.h>
35 #include <machine/specialreg.h>
36 #include <machine/sysarch.h>
37 #include <x86/ifunc.h>
38 #include <x86/fpu.h>
39 
40 #if defined __i386__
41 #define	X86_GET_XFPUSTATE	I386_GET_XFPUSTATE
42 typedef struct savexmm savex86_t ;
43 typedef struct i386_get_xfpustate x86_get_xfpustate_t;
44 #elif defined __amd64__
45 #define	X86_GET_XFPUSTATE	AMD64_GET_XFPUSTATE
46 typedef struct savefpu savex86_t;
47 typedef struct amd64_get_xfpustate x86_get_xfpustate_t;
48 #else
49 #error "Wrong arch"
50 #endif
51 
52 static int xstate_sz = 0;
53 
54 static int
55 __getcontextx_size_xfpu(void)
56 {
57 
58 	return (sizeof(ucontext_t) + xstate_sz);
59 }
60 
61 DEFINE_UIFUNC(, int, __getcontextx_size, (void))
62 {
63 	u_int p[4];
64 
65 	if ((cpu_feature2 & CPUID2_OSXSAVE) != 0) {
66 		cpuid_count(0xd, 0x0, p);
67 		xstate_sz = p[1] - sizeof(savex86_t);
68 	}
69 	return (__getcontextx_size_xfpu);
70 }
71 
72 static int
73 __fillcontextx2_xfpu(char *ctx)
74 {
75 	x86_get_xfpustate_t xfpu;
76 	ucontext_t *ucp;
77 
78 	ucp = (ucontext_t *)ctx;
79 	xfpu.addr = (char *)(ucp + 1);
80 	xfpu.len = xstate_sz;
81 	if (sysarch(X86_GET_XFPUSTATE, &xfpu) == -1)
82 		return (-1);
83 	ucp->uc_mcontext.mc_xfpustate = (__register_t)xfpu.addr;
84 	ucp->uc_mcontext.mc_xfpustate_len = xstate_sz;
85 	ucp->uc_mcontext.mc_flags |= _MC_HASFPXSTATE;
86 	return (0);
87 }
88 
89 static int
90 __fillcontextx2_noxfpu(char *ctx)
91 {
92 	ucontext_t *ucp;
93 
94 	ucp = (ucontext_t *)ctx;
95 	ucp->uc_mcontext.mc_xfpustate = 0;
96 	ucp->uc_mcontext.mc_xfpustate_len = 0;
97 	return (0);
98 }
99 
100 DEFINE_UIFUNC(, int, __fillcontextx2, (char *))
101 {
102 
103 	return ((cpu_feature2 & CPUID2_OSXSAVE) != 0 ? __fillcontextx2_xfpu :
104 	    __fillcontextx2_noxfpu);
105 }
106 
107 int
108 __fillcontextx(char *ctx)
109 {
110 	ucontext_t *ucp;
111 
112 	ucp = (ucontext_t *)ctx;
113 	if (getcontext(ucp) == -1)
114 		return (-1);
115 	__fillcontextx2(ctx);
116 	return (0);
117 }
118 
119 __weak_reference(__getcontextx, getcontextx);
120 
121 ucontext_t *
122 __getcontextx(void)
123 {
124 	char *ctx;
125 	int error;
126 
127 	ctx = malloc(__getcontextx_size());
128 	if (ctx == NULL)
129 		return (NULL);
130 	if (__fillcontextx(ctx) == -1) {
131 		error = errno;
132 		free(ctx);
133 		errno = error;
134 		return (NULL);
135 	}
136 	return ((ucontext_t *)ctx);
137 }
138