]>
git.saurik.com Git - apple/libc.git/blob - ppc/gen/makecontext.c
2 * Copyright (c) 2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
25 * Copyright (c) 2004 Suleiman Souhlal
26 * All rights reserved.
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
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.
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
52 #include <sys/cdefs.h>
53 #include <sys/param.h>
62 void _ctx_done(ucontext_t
*ucp
);
63 void _ctx_start(void);
66 _ctx_done(ucontext_t
*ucp
)
68 if (ucp
->uc_link
== NULL
)
71 /* invalidate context */
74 setcontext((const ucontext_t
*)ucp
->uc_link
);
76 abort(); /* should never return from above call */
81 makecontext(ucontext_t
*ucp
, void (*start
)(void), int argc
, ...)
86 int i
, regargs
, stackargs
;
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 */
99 * The stack must have space for the frame pointer, saved
100 * link register, overflow arguments, and be 16-byte
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);
108 mc
= ucp
->uc_mcontext
;
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
116 regargs
= (argc
> 8) ? 8 : argc
;
118 for (i
= 0; i
< regargs
; i
++)
119 args
[i
] = va_arg(ap
, uint32_t);
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];
137 * Overflow args go onto the stack
142 /* Skip past frame pointer and saved LR */
143 argp
= (uint32_t *)sp
+ 2;
145 for (i
= 0; i
< stackargs
; i
++)
146 *argp
++ = va_arg(ap
, uint32_t);
151 * Use caller-saved regs 14/15 to hold params that _ctx_start
152 * will use to invoke the user-supplied func
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 */