]>
Commit | Line | Data |
---|---|---|
224c7076 A |
1 | /* |
2 | * Copyright (c) 2004 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
6 | * This file contains Original Code and/or Modifications of Original Code | |
7 | * as defined in and that are subject to the Apple Public Source License | |
8 | * Version 2.0 (the 'License'). You may not use this file except in | |
9 | * compliance with the License. Please obtain a copy of the License at | |
10 | * http://www.opensource.apple.com/apsl/ and read it before using this | |
11 | * file. | |
12 | * | |
13 | * The Original Code and all software distributed under the License are | |
14 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
15 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, | |
16 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
17 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. | |
18 | * Please see the License for the specific language governing rights and | |
19 | * limitations under the License. | |
20 | * | |
21 | * @APPLE_LICENSE_HEADER_END@ | |
22 | */ | |
23 | ||
24 | /* | |
25 | * Copyright (c) 2004 Suleiman Souhlal | |
26 | * All rights reserved. | |
27 | * | |
28 | * Redistribution and use in source and binary forms, with or without | |
29 | * modification, are permitted provided that the following conditions | |
30 | * are met: | |
31 | * 1. Redistributions of source code must retain the above copyright | |
32 | * notice, this list of conditions and the following disclaimer. | |
33 | * 2. Redistributions in binary form must reproduce the above copyright | |
34 | * notice, this list of conditions and the following disclaimer in the | |
35 | * documentation and/or other materials provided with the distribution. | |
36 | * | |
37 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND | |
38 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
39 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
40 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | |
41 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
42 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
43 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
44 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
45 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
46 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
47 | * SUCH DAMAGE. | |
48 | */ | |
49 | ||
50 | #if defined(__ppc__) | |
51 | ||
52 | #include <sys/cdefs.h> | |
53 | #include <sys/param.h> | |
54 | ||
55 | #include <stdarg.h> | |
56 | #include <stdlib.h> | |
57 | #include <stdint.h> | |
58 | #include <stdio.h> | |
59 | #include <ucontext.h> | |
60 | #include <unistd.h> | |
61 | ||
62 | void _ctx_done(ucontext_t *ucp); | |
63 | void _ctx_start(void); | |
64 | ||
65 | void | |
66 | _ctx_done(ucontext_t *ucp) | |
67 | { | |
68 | if (ucp->uc_link == NULL) | |
69 | exit(0); | |
70 | else { | |
71 | /* invalidate context */ | |
72 | ucp->uc_mcsize = 0; | |
73 | ||
74 | setcontext((const ucontext_t *)ucp->uc_link); | |
75 | ||
76 | abort(); /* should never return from above call */ | |
77 | } | |
78 | } | |
79 | ||
80 | void | |
81 | makecontext(ucontext_t *ucp, void (*start)(void), int argc, ...) | |
82 | { | |
83 | mcontext_t mc; | |
84 | char *sp; | |
85 | va_list ap; | |
86 | int i, regargs, stackargs; | |
87 | uint32_t args[8]; | |
88 | ||
89 | /* Sanity checks */ | |
90 | if ((ucp == NULL) || (argc < 0) || (argc > NCARGS) | |
91 | || (ucp->uc_stack.ss_sp == NULL) | |
92 | || (ucp->uc_stack.ss_size < 8192)) { | |
93 | /* invalidate context */ | |
94 | ucp->uc_mcsize = 0; | |
95 | return; | |
96 | } | |
97 | ||
98 | /* | |
99 | * The stack must have space for the frame pointer, saved | |
100 | * link register, overflow arguments, and be 16-byte | |
101 | * aligned. | |
102 | */ | |
103 | stackargs = (argc > 8) ? argc - 8 : 0; | |
104 | sp = (char *) ucp->uc_stack.ss_sp + ucp->uc_stack.ss_size | |
105 | - sizeof(uint32_t)*(stackargs + 2); | |
106 | sp = (char *)((uint32_t)sp & ~0x1f); | |
107 | ||
108 | mc = ucp->uc_mcontext; | |
109 | ||
110 | /* | |
111 | * Up to 8 register args. Assumes all args are 32-bit and | |
112 | * integer only. Not sure how to cater for floating point, | |
113 | * although 64-bit args will work if aligned correctly | |
114 | * in the arg list. | |
115 | */ | |
116 | regargs = (argc > 8) ? 8 : argc; | |
117 | va_start(ap, argc); | |
118 | for (i = 0; i < regargs; i++) | |
119 | args[i] = va_arg(ap, uint32_t); | |
120 | ||
121 | switch (regargs) { | |
122 | /* | |
123 | * Hi Tom! | |
124 | */ | |
125 | case 8 : mc->ss.r10 = args[7]; | |
126 | case 7 : mc->ss.r9 = args[6]; | |
127 | case 6 : mc->ss.r8 = args[5]; | |
128 | case 5 : mc->ss.r7 = args[4]; | |
129 | case 4 : mc->ss.r6 = args[3]; | |
130 | case 3 : mc->ss.r5 = args[2]; | |
131 | case 2 : mc->ss.r4 = args[1]; | |
132 | case 1 : mc->ss.r3 = args[0]; | |
133 | default: break; | |
134 | } | |
135 | ||
136 | /* | |
137 | * Overflow args go onto the stack | |
138 | */ | |
139 | if (argc > 8) { | |
140 | uint32_t *argp; | |
141 | ||
142 | /* Skip past frame pointer and saved LR */ | |
143 | argp = (uint32_t *)sp + 2; | |
144 | ||
145 | for (i = 0; i < stackargs; i++) | |
146 | *argp++ = va_arg(ap, uint32_t); | |
147 | } | |
148 | va_end(ap); | |
149 | ||
150 | /* | |
151 | * Use caller-saved regs 14/15 to hold params that _ctx_start | |
152 | * will use to invoke the user-supplied func | |
153 | */ | |
154 | mc->ss.srr0 = (uint32_t) _ctx_start; | |
155 | mc->ss.r1 = (uint32_t) sp; /* new stack pointer */ | |
156 | mc->ss.r14 = (uint32_t) start; /* r14 <- start */ | |
157 | mc->ss.r15 = (uint32_t) ucp; /* r15 <- ucp */ | |
158 | } | |
159 | ||
160 | #endif /* __ppc__ */ |