2 * Copyright (C) 2009-2011 Nathan Whitehorn
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
18 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
20 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
21 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
22 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
23 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include <sys/syscall.h>
30 #include <machine/trap.h>
31 #include <machine/param.h>
32 #include <machine/spr.h>
33 #include <machine/asm.h>
35 #define OFWSTKSZ 4096 /* 4K Open Firmware stack */
45 .space 32 /* 4 * sizeof(register_t) */
47 .llong 0, 0, 0, 0, 0 /* msr/sprg0-3 used in Open Firmware */
50 GLOBAL(openfirmware_entry)
51 .llong 0 /* Open Firmware entry point */
53 .llong 0 /* RTAS entry point */
58 TOC_ENTRY(openfirmware_entry)
60 TOC_ENTRY(rtas_regsave)
63 * Open Firmware Real-mode Entry Point. This is a huge pain.
66 ASENTRY_NOPROF(ofwcall)
72 * We need to save the following, because OF's register save/
73 * restore code assumes that the contents of registers are
74 * at most 32 bits wide: lr, cr, r2, r13-r31, the old MSR. These
75 * get placed in that order in the stack.
100 /* Record the old MSR */
103 /* read client interface handler */
104 ld %r4,TOC_REF(openfirmware_entry)(%r2)
107 /* Get OF stack pointer */
108 ld %r7,TOC_REF(ofwstk)(%r2)
109 addi %r7,%r7,OFWSTKSZ-40
112 * Set the MSR to the OF value. This has the side effect of disabling
113 * exceptions, which is important for the next few steps.
116 ld %r5,TOC_REF(ofmsr)(%r2)
122 * Set up OF stack. This needs to be accessible in real mode and
123 * use the 32-bit ABI stack frame format. The pointer to the current
124 * kernel stack is placed at the very top of the stack along with
125 * the old MSR so we can get them back later.
129 std %r5,8(%r1) /* Save real stack pointer */
130 std %r2,16(%r1) /* Save old TOC */
131 std %r6,24(%r1) /* Save old MSR */
132 std %r8,32(%r1) /* Save high 32-bits of the kernel's PC */
138 /* Finally, branch to OF */
142 /* Reload stack pointer, MSR, and reference PC from the OFW stack */
148 /* Get back to the MSR/PC we want, using the cached high bits of PC */
156 rfid /* Turn on MMU, exceptions, and 64-bit mode */
159 /* Sign-extend the return value from OF */
162 /* Restore all the non-volatile registers */
185 /* Restore the stack and link register */
192 * RTAS 32-bit Entry Point. Similar to the OF one, but simpler (no separate
195 * C prototype: int rtascall(void *callbuffer, void *rtas_privdat);
198 ASENTRY_NOPROF(rtascall)
204 * We need to save the following, because RTAS's register save/
205 * restore code assumes that the contents of registers are
206 * at most 32 bits wide: lr, cr, r2, r13-r31, the old MSR. These
207 * get placed in that order in the stack.
232 /* Record the old MSR */
235 /* Read RTAS entry and reg save area pointers */
236 ld %r5,TOC_REF(rtas_entry)(%r2)
238 ld %r8,TOC_REF(rtas_regsave)(%r2)
241 * Set the MSR to the RTAS value. This has the side effect of disabling
242 * exceptions, which is important for the next few steps.
245 ld %r7,TOC_REF(rtasmsr)(%r2)
251 * Set up RTAS register save area, so that we can get back all of
252 * our 64-bit pointers. Save our stack pointer, the TOC, and the MSR.
253 * Put this in r1, since RTAS is obliged to save it. Kernel globals
254 * are below 4 GB, so this is safe.
258 std %r7,0(%r1) /* Save 64-bit stack pointer */
259 std %r2,8(%r1) /* Save TOC */
260 std %r6,16(%r1) /* Save MSR */
261 std %r9,24(%r1) /* Save reference PC for high 32 bits */
263 /* Finally, branch to RTAS */
268 * Reload stack pointer, MSR, reg PC from the reg save area in r1. We
269 * are running in 32-bit mode at this point, so it doesn't matter if r1
270 * has become sign-extended.
278 * Get back to the right PC. We need to atomically re-enable
279 * exceptions, 64-bit mode, and the MMU. One thing that has likely
280 * happened is that, if we were running in the high-memory direct
281 * map, we no longer are as a result of LR truncation in RTAS.
282 * Fix this by copying the high-order bits of the LR at function
283 * entry onto the current PC and then jumping there while flipping
293 rfid /* Turn on MMU, exceptions, and 64-bit mode */
296 /* Sign-extend the return value from RTAS */
299 /* Restore all the non-volatile registers */
322 /* Restore the stack and link register */