Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/lib/libc/powerpc64/gen/makecontext.c
39491 views
1
/*-
2
* SPDX-License-Identifier: BSD-2-Clause
3
*
4
* Copyright (c) 2004 Suleiman Souhlal
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
* 1. Redistributions of source code must retain the above copyright
11
* notice, this list of conditions and the following disclaimer.
12
* 2. Redistributions in binary form must reproduce the above copyright
13
* notice, this list of conditions and the following disclaimer in the
14
* documentation and/or other materials provided with the distribution.
15
*
16
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26
* SUCH DAMAGE.
27
*/
28
29
#include <sys/param.h>
30
31
#include <stdarg.h>
32
#include <stdlib.h>
33
#include <unistd.h>
34
#include <stdio.h>
35
#include <ucontext.h>
36
37
__weak_reference(__makecontext, makecontext);
38
39
void _ctx_done(ucontext_t *ucp);
40
void _ctx_start(void);
41
42
void
43
_ctx_done(ucontext_t *ucp)
44
{
45
if (ucp->uc_link == NULL)
46
exit(0);
47
else {
48
/* invalidate context */
49
ucp->uc_mcontext.mc_len = 0;
50
51
setcontext((const ucontext_t *)ucp->uc_link);
52
53
abort(); /* should never return from above call */
54
}
55
}
56
57
void
58
__makecontext(ucontext_t *ucp, void (*start)(void), int argc, ...)
59
{
60
mcontext_t *mc;
61
char *sp;
62
va_list ap;
63
int i, regargs, stackargs;
64
65
/* Sanity checks */
66
if ((ucp == NULL) || (argc < 0)
67
|| (ucp->uc_stack.ss_sp == NULL)
68
|| (ucp->uc_stack.ss_size < MINSIGSTKSZ)) {
69
/* invalidate context */
70
ucp->uc_mcontext.mc_len = 0;
71
return;
72
}
73
74
/*
75
* The stack must have space for the frame pointer, saved
76
* link register, overflow arguments, and be 16-byte
77
* aligned.
78
*/
79
stackargs = (argc > 8) ? argc - 8 : 0;
80
sp = (char *) ucp->uc_stack.ss_sp + ucp->uc_stack.ss_size
81
- sizeof(uintptr_t)*(stackargs + 6);
82
sp = (char *)((uintptr_t)sp & ~0x1f);
83
84
mc = &ucp->uc_mcontext;
85
86
/*
87
* Up to 8 register args. Assumes all args are 64-bit and
88
* integer only. Not sure how to cater for floating point.
89
*/
90
regargs = (argc > 8) ? 8 : argc;
91
va_start(ap, argc);
92
for (i = 0; i < regargs; i++)
93
mc->mc_gpr[3 + i] = va_arg(ap, uint64_t);
94
95
/*
96
* Overflow args go onto the stack
97
*/
98
if (argc > 8) {
99
uint64_t *argp;
100
101
/* Skip past frame pointer and saved LR */
102
#if !defined(_CALL_ELF) || _CALL_ELF == 1
103
argp = (uint64_t *)sp + 6;
104
#else
105
argp = (uint64_t *)sp + 4;
106
#endif
107
108
for (i = 0; i < stackargs; i++)
109
*argp++ = va_arg(ap, uint64_t);
110
}
111
va_end(ap);
112
113
/*
114
* Use caller-saved regs 14/15 to hold params that _ctx_start
115
* will use to invoke the user-supplied func
116
*/
117
#if !defined(_CALL_ELF) || _CALL_ELF == 1
118
/* Cast to ensure this is treated as a function descriptor. */
119
mc->mc_srr0 = *(uintptr_t *)_ctx_start;
120
#else
121
mc->mc_srr0 = (uintptr_t) _ctx_start;
122
mc->mc_gpr[12] = (uintptr_t) _ctx_start;/* required for prologue */
123
#endif
124
mc->mc_gpr[1] = (uintptr_t) sp; /* new stack pointer */
125
mc->mc_gpr[14] = (uintptr_t) start; /* r14 <- start */
126
mc->mc_gpr[15] = (uintptr_t) ucp; /* r15 <- ucp */
127
}
128
129