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 #include "opt_platform.h"
37 #define OFWSTKSZ 4096 /* 4K Open Firmware stack */
47 .space 32 /* 4 * sizeof(register_t) */
49 .llong 0, 0, 0, 0, 0 /* msr/sprg0-3 used in Open Firmware */
52 GLOBAL(openfirmware_entry)
53 .llong 0 /* Open Firmware entry point */
55 .llong 0 /* RTAS entry point */
60 TOC_ENTRY(openfirmware_entry)
62 TOC_ENTRY(rtas_regsave)
65 * Open Firmware Real-mode Entry Point. This is a huge pain.
68 ASENTRY_NOPROF(ofwcall)
74 * We need to save the following, because OF's register save/
75 * restore code assumes that the contents of registers are
76 * at most 32 bits wide: lr, cr, r2, r13-r31, the old MSR. These
77 * get placed in that order in the stack.
102 /* Record the old MSR */
105 /* read client interface handler */
106 addis %r4,%r2,TOC_REF(openfirmware_entry)@ha
107 ld %r4,TOC_REF(openfirmware_entry)@l(%r4)
110 /* Get OF stack pointer */
111 addis %r7,%r2,TOC_REF(ofwstk)@ha
112 ld %r7,TOC_REF(ofwstk)@l(%r7)
113 addi %r7,%r7,OFWSTKSZ-40
116 * Set the MSR to the OF value. This has the side effect of disabling
117 * exceptions, which is important for the next few steps.
118 * This does NOT, however, cause us to switch endianness.
121 addis %r5,%r2,TOC_REF(ofmsr)@ha
122 ld %r5,TOC_REF(ofmsr)@l(%r5)
124 #if defined(__LITTLE_ENDIAN__) && defined(QEMU)
125 /* QEMU hack: qemu does not emulate mtmsrd correctly! */
126 ori %r5,%r5,1 /* Leave PSR_LE set */
132 * Set up OF stack. This needs to be accessible in real mode and
133 * use the 32-bit ABI stack frame format. The pointer to the current
134 * kernel stack is placed at the very top of the stack along with
135 * the old MSR so we can get them back later.
139 std %r5,8(%r1) /* Save real stack pointer */
140 std %r2,16(%r1) /* Save old TOC */
141 std %r6,24(%r1) /* Save old MSR */
142 std %r8,32(%r1) /* Save high 32-bits of the kernel's PC */
148 #ifdef __LITTLE_ENDIAN__
149 /* Atomic context switch w/ endian change */
150 mtmsrd %r5, 1 /* Clear PSL_EE|PSL_RI */
151 addis %r5,%r2,TOC_REF(ofmsr)@ha
152 ld %r5,TOC_REF(ofmsr)@l(%r5)
159 addi %r5, %r5, (2f-1b)
164 RETURN_TO_NATIVE_ENDIAN
166 /* Finally, branch to OF */
171 /* Reload stack pointer, MSR, and reference PC from the OFW stack */
177 /* Get back to the MSR/PC we want, using the cached high bits of PC */
185 rfid /* Turn on MMU, exceptions, and 64-bit mode */
188 /* Sign-extend the return value from OF */
191 /* Restore all the non-volatile registers */
214 /* Restore the stack and link register */
222 * RTAS 32-bit Entry Point. Similar to the OF one, but simpler (no separate
225 * C prototype: int rtascall(void *callbuffer, void *rtas_privdat);
228 ASENTRY_NOPROF(rtascall)
234 * We need to save the following, because RTAS's register save/
235 * restore code assumes that the contents of registers are
236 * at most 32 bits wide: lr, cr, r2, r13-r31, the old MSR. These
237 * get placed in that order in the stack.
262 /* Record the old MSR */
265 /* Read RTAS entry and reg save area pointers */
266 addis %r5,%r2,TOC_REF(rtas_entry)@ha
267 ld %r5,TOC_REF(rtas_entry)@l(%r5)
269 addis %r8,%r2,TOC_REF(rtas_regsave)@ha
270 ld %r8,TOC_REF(rtas_regsave)@l(%r8)
273 * Set the MSR to the RTAS value. This has the side effect of disabling
274 * exceptions, which is important for the next few steps.
277 addis %r7,%r2,TOC_REF(rtasmsr)@ha
278 ld %r7,TOC_REF(rtasmsr)@l(%r7)
280 #ifdef __LITTLE_ENDIAN__
281 /* QEMU hack: qemu does not emulate mtmsrd correctly! */
282 ori %r7,%r7,1 /* Leave PSR_LE set */
288 * Set up RTAS register save area, so that we can get back all of
289 * our 64-bit pointers. Save our stack pointer, the TOC, and the MSR.
290 * Put this in r1, since RTAS is obliged to save it. Kernel globals
291 * are below 4 GB, so this is safe.
295 std %r7,0(%r1) /* Save 64-bit stack pointer */
296 std %r2,8(%r1) /* Save TOC */
297 std %r6,16(%r1) /* Save MSR */
298 std %r9,24(%r1) /* Save reference PC for high 32 bits */
300 #ifdef __LITTLE_ENDIAN__
301 /* Atomic context switch w/ endian change */
303 mtmsrd %r7, 1 /* Clear PSL_EE|PSL_RI */
304 addis %r7,%r2,TOC_REF(rtasmsr)@ha
305 ld %r7,TOC_REF(rtasmsr)@l(%r7)
312 addi %r5, %r5, (2f-1b)
317 RETURN_TO_NATIVE_ENDIAN
319 /* Finally, branch to RTAS */
325 * Reload stack pointer, MSR, reg PC from the reg save area in r1. We
326 * are running in 32-bit mode at this point, so it doesn't matter if r1
327 * has become sign-extended.
335 * Get back to the right PC. We need to atomically re-enable
336 * exceptions, 64-bit mode, and the MMU. One thing that has likely
337 * happened is that, if we were running in the high-memory direct
338 * map, we no longer are as a result of LR truncation in RTAS.
339 * Fix this by copying the high-order bits of the LR at function
340 * entry onto the current PC and then jumping there while flipping
350 rfid /* Turn on MMU, exceptions, and 64-bit mode */
353 /* Sign-extend the return value from RTAS */
356 /* Restore all the non-volatile registers */
379 /* Restore the stack and link register */