1/* $FreeBSD$ */ 2 3/*- 4 * Copyright (C) 2010-2016 Nathan Whitehorn 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 ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 * 27 * $FreeBSD$ 28 */ 29 30#include "assym.s" 31 32#include <sys/syscall.h> 33 34#include <machine/trap.h> 35#include <machine/param.h> 36#include <machine/spr.h> 37#include <machine/asm.h> 38 39#ifdef _CALL_ELF 40.abiversion _CALL_ELF 41#endif 42 43/* Glue for linker script */ 44.globl kernbase 45.set kernbase, KERNBASE 46 47/* 48 * Globals 49 */ 50 .data 51 .align 3 52GLOBAL(__startkernel) 53 .llong begin 54GLOBAL(__endkernel) 55 .llong end 56 57 .align 4 58#define TMPSTKSZ 16384 /* 16K temporary stack */ 59GLOBAL(tmpstk) 60 .space TMPSTKSZ 61 62TOC_ENTRY(tmpstk) 63 64/* 65 * Entry point for bootloaders that do not fully implement ELF and start 66 * at the beginning of the image (kexec, notably). In its own section so 67 * that it ends up before any linker-generated call stubs and actually at 68 * the beginning of the image. kexec on some systems also enters at 69 * (start of image) + 0x60, so put a spin loop there. 70 */ 71 .section ".text.kboot", "x", @progbits 72kbootentry: 73 b __start 74. = kbootentry + 0x40 /* Magic address used in platform layer */ 75 .global smp_spin_sem 76ap_kexec_spin_sem: 77 .long -1 78. = kbootentry + 0x60 /* Entry point for kexec APs */ 79ap_kexec_start: /* At 0x60 past start, copied to 0x60 by kexec */ 80 /* r3 set to CPU ID by kexec */ 81 82 /* Invalidate icache for low-memory copy and jump there */ 83 li %r0,0x80 84 dcbst 0,%r0 85 sync 86 icbi 0,%r0 87 isync 88 ba 0x80 /* Absolute branch to next inst */ 89 90. = kbootentry + 0x80 /* Aligned to cache line */ 911: or 31,31,31 /* yield */ 92 sync 93 lwz %r1,0x40(0) /* Spin on ap_kexec_spin_sem */ 94 cmpw %r1,%r3 /* Until it equals our CPU ID */ 95 bne 1b 96 97 /* Released */ 98 or 2,2,2 /* unyield */ 99 ba EXC_RST 100 101 102/* 103 * Now start the real text section 104 */ 105 106 .text 107 .globl btext 108btext: 109 110/* 111 * Main kernel entry point. 112 * 113 * Calling convention: 114 * r3: Flattened Device Tree pointer (or zero) 115 * r4: ignored 116 * r5: OF client interface pointer (or zero) 117 * r6: Loader metadata pointer (or zero) 118 * r7: Magic cookie (0xfb5d104d) to indicate that r6 has loader metadata 119 */ 120 .text 121ASENTRY_NOPROF(__start) 122 /* Set 64-bit mode if not yet set before branching to C */ 123 mfmsr %r20 124 li %r21,1 125 insrdi %r20,%r21,1,0 126 mtmsrd %r20 127 isync 128 nop /* Make this block a multiple of 8 bytes */ 129 130 /* Set up the TOC pointer */ 131 b 0f 132 .align 3 1330: nop 134 bl 1f 135 .llong __tocbase + 0x8000 - . 1361: mflr %r2 137 ld %r1,0(%r2) 138 add %r2,%r1,%r2 139 140 /* Get load offset */ 141 ld %r31,-0x8000(%r2) /* First TOC entry is TOC base */ 142 subf %r31,%r31,%r2 /* Subtract from real TOC base to get base */ 143 144 /* Set up the stack pointer */ 145 ld %r1,TOC_REF(tmpstk)(%r2) 146 addi %r1,%r1,TMPSTKSZ-96 147 add %r1,%r1,%r31 148 149 /* Relocate kernel */ 150 std %r3,48(%r1) 151 std %r4,56(%r1) 152 std %r5,64(%r1) 153 std %r6,72(%r1) 154 std %r7,80(%r1) 155 156 bl 1f 157 .llong _DYNAMIC-. 1581: mflr %r3 159 ld %r4,0(%r3) 160 add %r3,%r4,%r3 161 mr %r4,%r31 162 bl elf_reloc_self 163 nop 164 ld %r3,48(%r1) 165 ld %r4,56(%r1) 166 ld %r5,64(%r1) 167 ld %r6,72(%r1) 168 ld %r7,80(%r1) 169 170 /* Begin CPU init */ 171 mr %r4,%r2 /* Replace ignored r4 with tocbase for trap handlers */ 172 bl powerpc_init 173 nop 174 175 /* Set stack pointer to new value and branch to mi_startup */ 176 mr %r1, %r3 177 li %r3, 0 178 std %r3, 0(%r1) 179 bl mi_startup 180 nop 181 182 /* Unreachable */ 183 b . 184 185#include <powerpc/aim/trap_subr64.S> 186