xref: /freebsd/lib/libc/aarch64/gen/getcontextx.c (revision 5202ca77aaf552310bcbaccba750ee9f0888d0d8)
1fd6b1cc3SAndrew Turner /*
2fd6b1cc3SAndrew Turner  * Copyright (c) 2011 Konstantin Belousov <kib@FreeBSD.org>
3fd6b1cc3SAndrew Turner  * All rights reserved.
4fd6b1cc3SAndrew Turner  *
5fd6b1cc3SAndrew Turner  * Redistribution and use in source and binary forms, with or without
6fd6b1cc3SAndrew Turner  * modification, are permitted provided that the following conditions
7fd6b1cc3SAndrew Turner  * are met:
8fd6b1cc3SAndrew Turner  *
9fd6b1cc3SAndrew Turner  * 1. Redistributions of source code must retain the above copyright
10fd6b1cc3SAndrew Turner  *    notice, this list of conditions and the following disclaimer.
11fd6b1cc3SAndrew Turner  * 2. Redistributions in binary form must reproduce the above copyright
12fd6b1cc3SAndrew Turner  *    notice, this list of conditions and the following disclaimer in the
13fd6b1cc3SAndrew Turner  *    documentation and/or other materials provided with the distribution.
14fd6b1cc3SAndrew Turner  *
15fd6b1cc3SAndrew Turner  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16fd6b1cc3SAndrew Turner  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17fd6b1cc3SAndrew Turner  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18fd6b1cc3SAndrew Turner  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19fd6b1cc3SAndrew Turner  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20fd6b1cc3SAndrew Turner  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21fd6b1cc3SAndrew Turner  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22fd6b1cc3SAndrew Turner  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23fd6b1cc3SAndrew Turner  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24fd6b1cc3SAndrew Turner  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25fd6b1cc3SAndrew Turner  */
26fd6b1cc3SAndrew Turner 
27fd6b1cc3SAndrew Turner #include <sys/types.h>
28fd6b1cc3SAndrew Turner #include <sys/ucontext.h>
29fd6b1cc3SAndrew Turner #include <errno.h>
30fd6b1cc3SAndrew Turner #include <stdlib.h>
31fd6b1cc3SAndrew Turner 
32fd6b1cc3SAndrew Turner int
__getcontextx_size(void)33fd6b1cc3SAndrew Turner __getcontextx_size(void)
34fd6b1cc3SAndrew Turner {
35*5202ca77SAndrew Turner 	size_t size;
36fd6b1cc3SAndrew Turner 
37*5202ca77SAndrew Turner 	size = sizeof(ucontext_t);
38*5202ca77SAndrew Turner 	size += sizeof(struct arm64_reg_context); /* Space for ARM64_CTX_END */
39*5202ca77SAndrew Turner 
40*5202ca77SAndrew Turner 	return (size);
41fd6b1cc3SAndrew Turner }
42fd6b1cc3SAndrew Turner 
43fd6b1cc3SAndrew Turner int
__fillcontextx2(char * ctx)44fd6b1cc3SAndrew Turner __fillcontextx2(char *ctx)
45fd6b1cc3SAndrew Turner {
46*5202ca77SAndrew Turner 	struct arm64_reg_context *reg_ctx;
47*5202ca77SAndrew Turner 	ucontext_t *ucp;
48*5202ca77SAndrew Turner 
49*5202ca77SAndrew Turner 	ucp = (ucontext_t *)ctx;
50*5202ca77SAndrew Turner 	ucp->uc_mcontext.mc_ptr = (uint64_t)(ucp + 1);
51*5202ca77SAndrew Turner 
52*5202ca77SAndrew Turner 	reg_ctx = (struct arm64_reg_context *)ucp->uc_mcontext.mc_ptr;
53*5202ca77SAndrew Turner 	reg_ctx->ctx_id = ARM64_CTX_END;
54*5202ca77SAndrew Turner 	reg_ctx->ctx_size = sizeof(struct arm64_reg_context);
55fd6b1cc3SAndrew Turner 
56fd6b1cc3SAndrew Turner 	return (0);
57fd6b1cc3SAndrew Turner }
58fd6b1cc3SAndrew Turner 
59fd6b1cc3SAndrew Turner int
__fillcontextx(char * ctx)60fd6b1cc3SAndrew Turner __fillcontextx(char *ctx)
61fd6b1cc3SAndrew Turner {
62fd6b1cc3SAndrew Turner 	ucontext_t *ucp;
63fd6b1cc3SAndrew Turner 
64fd6b1cc3SAndrew Turner 	ucp = (ucontext_t *)ctx;
65*5202ca77SAndrew Turner 	if (getcontext(ucp) == -1)
66*5202ca77SAndrew Turner 		return (-1);
67*5202ca77SAndrew Turner 	__fillcontextx2(ctx);
68*5202ca77SAndrew Turner 	return (0);
69fd6b1cc3SAndrew Turner }
70fd6b1cc3SAndrew Turner 
71fd6b1cc3SAndrew Turner __weak_reference(__getcontextx, getcontextx);
72fd6b1cc3SAndrew Turner 
73fd6b1cc3SAndrew Turner ucontext_t *
__getcontextx(void)74fd6b1cc3SAndrew Turner __getcontextx(void)
75fd6b1cc3SAndrew Turner {
76fd6b1cc3SAndrew Turner 	char *ctx;
77fd6b1cc3SAndrew Turner 	int error;
78fd6b1cc3SAndrew Turner 
79fd6b1cc3SAndrew Turner 	ctx = malloc(__getcontextx_size());
80fd6b1cc3SAndrew Turner 	if (ctx == NULL)
81fd6b1cc3SAndrew Turner 		return (NULL);
82fd6b1cc3SAndrew Turner 	if (__fillcontextx(ctx) == -1) {
83fd6b1cc3SAndrew Turner 		error = errno;
84fd6b1cc3SAndrew Turner 		free(ctx);
85fd6b1cc3SAndrew Turner 		errno = error;
86fd6b1cc3SAndrew Turner 		return (NULL);
87fd6b1cc3SAndrew Turner 	}
88fd6b1cc3SAndrew Turner 	return ((ucontext_t *)ctx);
89fd6b1cc3SAndrew Turner }
90