1 /*- 2 * Copyright (C) 2006-2012 Semihalf 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 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. 13 * 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. IN 17 * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 18 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 19 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 21 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 22 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 23 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 /*- 26 * Copyright (C) 2001 Benno Rice 27 * All rights reserved. 28 * 29 * Redistribution and use in source and binary forms, with or without 30 * modification, are permitted provided that the following conditions 31 * are met: 32 * 1. Redistributions of source code must retain the above copyright 33 * notice, this list of conditions and the following disclaimer. 34 * 2. Redistributions in binary form must reproduce the above copyright 35 * notice, this list of conditions and the following disclaimer in the 36 * documentation and/or other materials provided with the distribution. 37 * 38 * THIS SOFTWARE IS PROVIDED BY Benno Rice ``AS IS'' AND ANY EXPRESS OR 39 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 40 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 41 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 43 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 44 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 45 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 46 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 47 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 48 * $NetBSD: machdep.c,v 1.74.2.1 2000/11/01 16:13:48 tv Exp $ 49 */ 50 /*- 51 * Copyright (C) 1995, 1996 Wolfgang Solfrank. 52 * Copyright (C) 1995, 1996 TooLs GmbH. 53 * All rights reserved. 54 * 55 * Redistribution and use in source and binary forms, with or without 56 * modification, are permitted provided that the following conditions 57 * are met: 58 * 1. Redistributions of source code must retain the above copyright 59 * notice, this list of conditions and the following disclaimer. 60 * 2. Redistributions in binary form must reproduce the above copyright 61 * notice, this list of conditions and the following disclaimer in the 62 * documentation and/or other materials provided with the distribution. 63 * 3. All advertising materials mentioning features or use of this software 64 * must display the following acknowledgement: 65 * This product includes software developed by TooLs GmbH. 66 * 4. The name of TooLs GmbH may not be used to endorse or promote products 67 * derived from this software without specific prior written permission. 68 * 69 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR 70 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 71 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 72 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 73 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 74 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 75 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 76 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 77 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 78 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 79 */ 80 81 #include <sys/cdefs.h> 82 #include "opt_ddb.h" 83 #include "opt_hwpmc_hooks.h" 84 #include "opt_kstack_pages.h" 85 #include "opt_platform.h" 86 87 #include <sys/cdefs.h> 88 #include <sys/types.h> 89 #include <sys/param.h> 90 #include <sys/proc.h> 91 #include <sys/systm.h> 92 #include <sys/time.h> 93 #include <sys/bio.h> 94 #include <sys/buf.h> 95 #include <sys/bus.h> 96 #include <sys/cons.h> 97 #include <sys/cpu.h> 98 #include <sys/kdb.h> 99 #include <sys/kernel.h> 100 #include <sys/lock.h> 101 #include <sys/mutex.h> 102 #include <sys/rwlock.h> 103 #include <sys/sysctl.h> 104 #include <sys/exec.h> 105 #include <sys/ktr.h> 106 #include <sys/syscallsubr.h> 107 #include <sys/sysproto.h> 108 #include <sys/signalvar.h> 109 #include <sys/sysent.h> 110 #include <sys/imgact.h> 111 #include <sys/msgbuf.h> 112 #include <sys/ptrace.h> 113 114 #include <vm/vm.h> 115 #include <vm/pmap.h> 116 #include <vm/vm_extern.h> 117 #include <vm/vm_page.h> 118 #include <vm/vm_object.h> 119 #include <vm/vm_pager.h> 120 121 #include <machine/cpu.h> 122 #include <machine/kdb.h> 123 #include <machine/vmparam.h> 124 #include <machine/spr.h> 125 #include <machine/hid.h> 126 #include <machine/psl.h> 127 #include <machine/trap.h> 128 #include <machine/md_var.h> 129 #include <machine/mmuvar.h> 130 #include <machine/sigframe.h> 131 #include <machine/machdep.h> 132 #include <machine/metadata.h> 133 #include <machine/platform.h> 134 135 #include <sys/linker.h> 136 #include <sys/reboot.h> 137 138 #include <contrib/libfdt/libfdt.h> 139 #include <dev/fdt/fdt_common.h> 140 #include <dev/ofw/openfirm.h> 141 142 #ifdef DDB 143 #include <ddb/ddb.h> 144 #endif 145 146 #ifdef DEBUG 147 #define debugf(fmt, args...) printf(fmt, ##args) 148 #else 149 #define debugf(fmt, args...) 150 #endif 151 152 extern unsigned char _etext[]; 153 extern unsigned char _edata[]; 154 extern unsigned char __bss_start[]; 155 extern unsigned char __sbss_start[]; 156 extern unsigned char __sbss_end[]; 157 extern unsigned char _end[]; 158 extern vm_offset_t __endkernel; 159 extern vm_paddr_t kernload; 160 161 /* 162 * Bootinfo is passed to us by legacy loaders. Save the address of the 163 * structure to handle backward compatibility. 164 */ 165 uint32_t *bootinfo; 166 167 void print_kernel_section_addr(void); 168 void print_kenv(void); 169 uintptr_t booke_init(u_long, u_long); 170 void ivor_setup(void); 171 172 extern void *interrupt_vector_base; 173 extern void *int_critical_input; 174 extern void *int_machine_check; 175 extern void *int_data_storage; 176 extern void *int_instr_storage; 177 extern void *int_external_input; 178 extern void *int_alignment; 179 extern void *int_fpu; 180 extern void *int_program; 181 extern void *int_syscall; 182 extern void *int_decrementer; 183 extern void *int_fixed_interval_timer; 184 extern void *int_watchdog; 185 extern void *int_data_tlb_error; 186 extern void *int_inst_tlb_error; 187 extern void *int_debug; 188 extern void *int_debug_ed; 189 extern void *int_vec; 190 extern void *int_vecast; 191 #ifdef __SPE__ 192 extern void *int_spe_fpdata; 193 extern void *int_spe_fpround; 194 #endif 195 #ifdef HWPMC_HOOKS 196 extern void *int_performance_counter; 197 #endif 198 199 #define SET_TRAP(ivor, handler) \ 200 KASSERT(((uintptr_t)(&handler) & ~0xffffUL) == \ 201 ((uintptr_t)(&interrupt_vector_base) & ~0xffffUL), \ 202 ("Handler " #handler " too far from interrupt vector base")); \ 203 mtspr(ivor, (uintptr_t)(&handler) & 0xffffUL); 204 205 uintptr_t powerpc_init(vm_offset_t fdt, vm_offset_t, vm_offset_t, void *mdp, 206 uint32_t mdp_cookie); 207 void booke_cpu_init(void); 208 209 void 210 booke_cpu_init(void) 211 { 212 213 cpu_features |= PPC_FEATURE_BOOKE; 214 215 psl_kernset = PSL_CE | PSL_ME | PSL_EE; 216 #ifdef __powerpc64__ 217 psl_kernset |= PSL_CM; 218 #endif 219 psl_userset = psl_kernset | PSL_PR; 220 #ifdef __powerpc64__ 221 psl_userset32 = psl_userset & ~PSL_CM; 222 #endif 223 /* 224 * Zeroed bits in this variable signify that the value of the bit 225 * in its position is allowed to vary between userspace contexts. 226 * 227 * All other bits are required to be identical for every userspace 228 * context. The actual *value* of the bit is determined by 229 * psl_userset and/or psl_userset32, and is not allowed to change. 230 * 231 * Remember to update this set when implementing support for 232 * *conditionally* enabling a processor facility. Failing to do 233 * this will cause swapcontext() in userspace to break when a 234 * process uses a conditionally-enabled facility. 235 * 236 * When *unconditionally* implementing support for a processor 237 * facility, update psl_userset / psl_userset32 instead. 238 * 239 * See the access control check in set_mcontext(). 240 */ 241 psl_userstatic = ~(PSL_VEC | PSL_FP | PSL_FE0 | PSL_FE1); 242 243 pmap_mmu_install(MMU_TYPE_BOOKE, BUS_PROBE_GENERIC); 244 } 245 246 void 247 ivor_setup(void) 248 { 249 250 mtspr(SPR_IVPR, ((uintptr_t)&interrupt_vector_base) & ~0xffffUL); 251 252 SET_TRAP(SPR_IVOR0, int_critical_input); 253 SET_TRAP(SPR_IVOR1, int_machine_check); 254 SET_TRAP(SPR_IVOR2, int_data_storage); 255 SET_TRAP(SPR_IVOR3, int_instr_storage); 256 SET_TRAP(SPR_IVOR4, int_external_input); 257 SET_TRAP(SPR_IVOR5, int_alignment); 258 SET_TRAP(SPR_IVOR6, int_program); 259 SET_TRAP(SPR_IVOR8, int_syscall); 260 SET_TRAP(SPR_IVOR10, int_decrementer); 261 SET_TRAP(SPR_IVOR11, int_fixed_interval_timer); 262 SET_TRAP(SPR_IVOR12, int_watchdog); 263 SET_TRAP(SPR_IVOR13, int_data_tlb_error); 264 SET_TRAP(SPR_IVOR14, int_inst_tlb_error); 265 SET_TRAP(SPR_IVOR15, int_debug); 266 #ifdef HWPMC_HOOKS 267 SET_TRAP(SPR_IVOR35, int_performance_counter); 268 #endif 269 switch ((mfpvr() >> 16) & 0xffff) { 270 case FSL_E6500: 271 SET_TRAP(SPR_IVOR32, int_vec); 272 SET_TRAP(SPR_IVOR33, int_vecast); 273 /* FALLTHROUGH */ 274 case FSL_E500mc: 275 case FSL_E5500: 276 SET_TRAP(SPR_IVOR7, int_fpu); 277 SET_TRAP(SPR_IVOR15, int_debug_ed); 278 break; 279 case FSL_E500v1: 280 case FSL_E500v2: 281 SET_TRAP(SPR_IVOR32, int_vec); 282 #ifdef __SPE__ 283 SET_TRAP(SPR_IVOR33, int_spe_fpdata); 284 SET_TRAP(SPR_IVOR34, int_spe_fpround); 285 #endif 286 break; 287 } 288 289 #ifdef __powerpc64__ 290 /* Set 64-bit interrupt mode. */ 291 mtspr(SPR_EPCR, mfspr(SPR_EPCR) | EPCR_ICM); 292 #endif 293 } 294 295 static int 296 booke_check_for_fdt(uint32_t arg1, vm_offset_t *dtbp) 297 { 298 void *ptr; 299 int fdt_size; 300 301 if (arg1 % 8 != 0) 302 return (-1); 303 304 ptr = (void *)pmap_early_io_map(arg1, PAGE_SIZE); 305 if (fdt_check_header(ptr) != 0) 306 return (-1); 307 308 /* 309 * Read FDT total size from the header of FDT. 310 * This for sure hits within first page which is 311 * already mapped. 312 */ 313 fdt_size = fdt_totalsize((void *)ptr); 314 315 /* 316 * Ok, arg1 points to FDT, so we need to map it in. 317 * First, unmap this page and then map FDT again with full size 318 */ 319 pmap_early_io_unmap((vm_offset_t)ptr, PAGE_SIZE); 320 ptr = (void *)pmap_early_io_map(arg1, fdt_size); 321 *dtbp = (vm_offset_t)ptr; 322 323 return (0); 324 } 325 326 uintptr_t 327 booke_init(u_long arg1, u_long arg2) 328 { 329 uintptr_t ret; 330 void *mdp; 331 vm_offset_t dtbp, end; 332 333 end = (uintptr_t)_end; 334 dtbp = (vm_offset_t)NULL; 335 336 /* Set up TLB initially */ 337 bootinfo = NULL; 338 bzero(__sbss_start, __sbss_end - __sbss_start); 339 bzero(__bss_start, _end - __bss_start); 340 tlb1_init(); 341 342 /* 343 * Handle the various ways we can get loaded and started: 344 * - FreeBSD's loader passes the pointer to the metadata 345 * in arg1, with arg2 undefined. arg1 has a value that's 346 * relative to the kernel's link address (i.e. larger 347 * than 0xc0000000). 348 * - Juniper's loader passes the metadata pointer in arg2 349 * and sets arg1 to zero. This is to signal that the 350 * loader maps the kernel and starts it at its link 351 * address (unlike the FreeBSD loader). 352 * - U-Boot passes the standard argc and argv parameters 353 * in arg1 and arg2 (resp). arg1 is between 1 and some 354 * relatively small number, such as 64K. arg2 is the 355 * physical address of the argv vector. 356 * - ePAPR loaders pass an FDT blob in r3 (arg1) and the magic hex 357 * string 0x45504150 ('EPAP') in r6 (which has been lost by now). 358 * r4 (arg2) is supposed to be set to zero, but is not always. 359 */ 360 361 if (arg1 == 0) /* Juniper loader */ 362 mdp = (void *)arg2; 363 else if (booke_check_for_fdt(arg1, &dtbp) == 0) { /* ePAPR */ 364 end = roundup(end, 8); 365 memmove((void *)end, (void *)dtbp, fdt_totalsize((void *)dtbp)); 366 dtbp = end; 367 end += fdt_totalsize((void *)dtbp); 368 __endkernel = end; 369 mdp = NULL; 370 } else if (arg1 > (uintptr_t)kernload) /* FreeBSD loader */ 371 mdp = (void *)arg1; 372 else /* U-Boot */ 373 mdp = NULL; 374 375 /* Default to 32 byte cache line size. */ 376 switch ((mfpvr()) >> 16) { 377 case FSL_E500mc: 378 case FSL_E5500: 379 case FSL_E6500: 380 cacheline_size = 64; 381 break; 382 } 383 384 /* 385 * Last element is a magic cookie that indicates that the metadata 386 * pointer is meaningful. 387 */ 388 ret = powerpc_init(dtbp, 0, 0, mdp, (mdp == NULL) ? 0 : 0xfb5d104d); 389 390 /* Enable caches */ 391 booke_enable_l1_cache(); 392 booke_enable_l2_cache(); 393 394 booke_enable_bpred(); 395 396 return (ret); 397 } 398 399 #define RES_GRANULE cacheline_size 400 extern uintptr_t tlb0_miss_locks[]; 401 402 /* Initialise a struct pcpu. */ 403 void 404 cpu_pcpu_init(struct pcpu *pcpu, int cpuid, size_t sz) 405 { 406 407 pcpu->pc_booke.tid_next = TID_MIN; 408 409 #ifdef SMP 410 uintptr_t *ptr; 411 int words_per_gran = RES_GRANULE / sizeof(uintptr_t); 412 413 ptr = &tlb0_miss_locks[cpuid * words_per_gran]; 414 pcpu->pc_booke.tlb_lock = ptr; 415 *ptr = TLB_UNLOCKED; 416 *(ptr + 1) = 0; /* recurse counter */ 417 #endif 418 } 419 420 /* Shutdown the CPU as much as possible. */ 421 void 422 cpu_halt(void) 423 { 424 425 mtmsr(mfmsr() & ~(PSL_CE | PSL_EE | PSL_ME | PSL_DE)); 426 while (1) 427 ; 428 } 429 430 int 431 ptrace_single_step(struct thread *td) 432 { 433 struct trapframe *tf; 434 435 tf = td->td_frame; 436 tf->srr1 |= PSL_DE; 437 tf->cpu.booke.dbcr0 |= (DBCR0_IDM | DBCR0_IC); 438 return (0); 439 } 440 441 int 442 ptrace_clear_single_step(struct thread *td) 443 { 444 struct trapframe *tf; 445 446 tf = td->td_frame; 447 tf->srr1 &= ~PSL_DE; 448 tf->cpu.booke.dbcr0 &= ~(DBCR0_IDM | DBCR0_IC); 449 return (0); 450 } 451 452 void 453 kdb_cpu_clear_singlestep(void) 454 { 455 register_t r; 456 457 r = mfspr(SPR_DBCR0); 458 mtspr(SPR_DBCR0, r & ~DBCR0_IC); 459 kdb_frame->srr1 &= ~PSL_DE; 460 } 461 462 void 463 kdb_cpu_set_singlestep(void) 464 { 465 register_t r; 466 467 r = mfspr(SPR_DBCR0); 468 mtspr(SPR_DBCR0, r | DBCR0_IC | DBCR0_IDM); 469 kdb_frame->srr1 |= PSL_DE; 470 } 471