getcontextx.c revision 325307
1/*
2 * Copyright (c) 2017 Michal Meloun <mmel@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: stable/11/lib/libc/arm/gen/getcontextx.c 325307 2017-11-02 07:08:13Z mmel $");
29
30#include <sys/types.h>
31#include <sys/ucontext.h>
32#include <errno.h>
33#include <stdlib.h>
34#include <machine/sysarch.h>
35
36struct ucontextx {
37	ucontext_t	ucontext;
38	mcontext_vfp_t	mcontext_vfp;
39};
40
41int
42__getcontextx_size(void)
43{
44
45	return (sizeof(struct ucontextx));
46}
47
48int
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
69int
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
83ucontext_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