xref: /freebsd/lib/libc/arm/gen/getcontextx.c (revision 22cf89c938886d14f5796fc49f9f020c23ea8eaf)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2017 Michal Meloun <mmel@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/cdefs.h>
30 #include <sys/types.h>
31 #include <sys/ucontext.h>
32 #include <errno.h>
33 #include <stdlib.h>
34 #include <machine/sysarch.h>
35 
36 struct ucontextx {
37 	ucontext_t	ucontext;
38 	mcontext_vfp_t	mcontext_vfp;
39 };
40 
41 int
42 __getcontextx_size(void)
43 {
44 
45 	return (sizeof(struct ucontextx));
46 }
47 
48 int
49 __fillcontextx2(char *ctx)
50 {
51 	struct ucontextx *ucxp;
52 	ucontext_t	 *ucp;
53 	mcontext_vfp_t	 *mvp;
54 	struct arm_get_vfpstate_args vfp_arg;
55 
56 	ucxp = (struct ucontextx *)ctx;
57 	ucp = &ucxp->ucontext;
58 	mvp = &ucxp->mcontext_vfp;
59 
60 	vfp_arg.mc_vfp_size = sizeof(mcontext_vfp_t);
61 	vfp_arg.mc_vfp = mvp;
62 	if (sysarch(ARM_GET_VFPSTATE, &vfp_arg) == -1)
63 			return (-1);
64 	ucp->uc_mcontext.mc_vfp_size = sizeof(mcontext_vfp_t);
65 	ucp->uc_mcontext.mc_vfp_ptr = mvp;
66 	return (0);
67 }
68 
69 int
70 __fillcontextx(char *ctx)
71 {
72 	struct ucontextx *ucxp;
73 
74 	ucxp = (struct ucontextx *)ctx;
75 	if (getcontext(&ucxp->ucontext) == -1)
76 		return (-1);
77 	__fillcontextx2(ctx);
78 	return (0);
79 }
80 
81 __weak_reference(__getcontextx, getcontextx);
82 
83 ucontext_t *
84 __getcontextx(void)
85 {
86 	char *ctx;
87 	int error;
88 
89 	ctx = malloc(__getcontextx_size());
90 	if (ctx == NULL)
91 		return (NULL);
92 	if (__fillcontextx(ctx) == -1) {
93 		error = errno;
94 		free(ctx);
95 		errno = error;
96 		return (NULL);
97 	}
98 	return ((ucontext_t *)ctx);
99 }
100