1 /* 2 * linux/arch/arm/kernel/process.c 3 * 4 * Copyright (C) 1996-2000 Russell King - Converted to ARM. 5 * Original Copyright (C) 1995 Linus Torvalds 6 * 7 * This program is free software; you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License version 2 as 9 * published by the Free Software Foundation. 10 */ 11 #include <stdarg.h> 12 13 #include <linux/export.h> 14 #include <linux/sched.h> 15 #include <linux/kernel.h> 16 #include <linux/mm.h> 17 #include <linux/stddef.h> 18 #include <linux/unistd.h> 19 #include <linux/user.h> 20 #include <linux/delay.h> 21 #include <linux/reboot.h> 22 #include <linux/interrupt.h> 23 #include <linux/kallsyms.h> 24 #include <linux/init.h> 25 #include <linux/cpu.h> 26 #include <linux/elfcore.h> 27 #include <linux/pm.h> 28 #include <linux/tick.h> 29 #include <linux/utsname.h> 30 #include <linux/uaccess.h> 31 #include <linux/random.h> 32 #include <linux/hw_breakpoint.h> 33 #include <linux/leds.h> 34 #include <linux/reboot.h> 35 36 #include <asm/cacheflush.h> 37 #include <asm/idmap.h> 38 #include <asm/processor.h> 39 #include <asm/thread_notify.h> 40 #include <asm/stacktrace.h> 41 #include <asm/system_misc.h> 42 #include <asm/mach/time.h> 43 #include <asm/tls.h> 44 #include "reboot.h" 45 46 #ifdef CONFIG_CC_STACKPROTECTOR 47 #include <linux/stackprotector.h> 48 unsigned long __stack_chk_guard __read_mostly; 49 EXPORT_SYMBOL(__stack_chk_guard); 50 #endif 51 52 static const char *processor_modes[] __maybe_unused = { 53 "USER_26", "FIQ_26" , "IRQ_26" , "SVC_26" , "UK4_26" , "UK5_26" , "UK6_26" , "UK7_26" , 54 "UK8_26" , "UK9_26" , "UK10_26", "UK11_26", "UK12_26", "UK13_26", "UK14_26", "UK15_26", 55 "USER_32", "FIQ_32" , "IRQ_32" , "SVC_32" , "UK4_32" , "UK5_32" , "MON_32" , "ABT_32" , 56 "UK8_32" , "UK9_32" , "HYP_32", "UND_32" , "UK12_32", "UK13_32", "UK14_32", "SYS_32" 57 }; 58 59 static const char *isa_modes[] __maybe_unused = { 60 "ARM" , "Thumb" , "Jazelle", "ThumbEE" 61 }; 62 63 extern void call_with_stack(void (*fn)(void *), void *arg, void *sp); 64 typedef void (*phys_reset_t)(unsigned long); 65 66 /* 67 * A temporary stack to use for CPU reset. This is static so that we 68 * don't clobber it with the identity mapping. When running with this 69 * stack, any references to the current task *will not work* so you 70 * should really do as little as possible before jumping to your reset 71 * code. 72 */ 73 static u64 soft_restart_stack[16]; 74 75 static void __soft_restart(void *addr) 76 { 77 phys_reset_t phys_reset; 78 79 /* Take out a flat memory mapping. */ 80 setup_mm_for_reboot(); 81 82 /* Clean and invalidate caches */ 83 flush_cache_all(); 84 85 /* Turn off caching */ 86 cpu_proc_fin(); 87 88 /* Push out any further dirty data, and ensure cache is empty */ 89 flush_cache_all(); 90 91 /* Switch to the identity mapping. */ 92 phys_reset = (phys_reset_t)(unsigned long)virt_to_phys(cpu_reset); 93 phys_reset((unsigned long)addr); 94 95 /* Should never get here. */ 96 BUG(); 97 } 98 99 void _soft_restart(unsigned long addr, bool disable_l2) 100 { 101 u64 *stack = soft_restart_stack + ARRAY_SIZE(soft_restart_stack); 102 103 /* Disable interrupts first */ 104 raw_local_irq_disable(); 105 local_fiq_disable(); 106 107 /* Disable the L2 if we're the last man standing. */ 108 if (disable_l2) 109 outer_disable(); 110 111 /* Change to the new stack and continue with the reset. */ 112 call_with_stack(__soft_restart, (void *)addr, (void *)stack); 113 114 /* Should never get here. */ 115 BUG(); 116 } 117 118 void soft_restart(unsigned long addr) 119 { 120 _soft_restart(addr, num_online_cpus() == 1); 121 } 122 123 /* 124 * Function pointers to optional machine specific functions 125 */ 126 void (*pm_power_off)(void); 127 EXPORT_SYMBOL(pm_power_off); 128 129 void (*arm_pm_restart)(enum reboot_mode reboot_mode, const char *cmd); 130 131 /* 132 * This is our default idle handler. 133 */ 134 135 void (*arm_pm_idle)(void); 136 137 /* 138 * Called from the core idle loop. 139 */ 140 141 void arch_cpu_idle(void) 142 { 143 if (arm_pm_idle) 144 arm_pm_idle(); 145 else 146 cpu_do_idle(); 147 local_irq_enable(); 148 } 149 150 void arch_cpu_idle_prepare(void) 151 { 152 local_fiq_enable(); 153 } 154 155 void arch_cpu_idle_enter(void) 156 { 157 ledtrig_cpu(CPU_LED_IDLE_START); 158 #ifdef CONFIG_PL310_ERRATA_769419 159 wmb(); 160 #endif 161 } 162 163 void arch_cpu_idle_exit(void) 164 { 165 ledtrig_cpu(CPU_LED_IDLE_END); 166 } 167 168 #ifdef CONFIG_HOTPLUG_CPU 169 void arch_cpu_idle_dead(void) 170 { 171 cpu_die(); 172 } 173 #endif 174 175 /* 176 * Called by kexec, immediately prior to machine_kexec(). 177 * 178 * This must completely disable all secondary CPUs; simply causing those CPUs 179 * to execute e.g. a RAM-based pin loop is not sufficient. This allows the 180 * kexec'd kernel to use any and all RAM as it sees fit, without having to 181 * avoid any code or data used by any SW CPU pin loop. The CPU hotplug 182 * functionality embodied in disable_nonboot_cpus() to achieve this. 183 */ 184 void machine_shutdown(void) 185 { 186 disable_nonboot_cpus(); 187 } 188 189 /* 190 * Halting simply requires that the secondary CPUs stop performing any 191 * activity (executing tasks, handling interrupts). smp_send_stop() 192 * achieves this. 193 */ 194 void machine_halt(void) 195 { 196 local_irq_disable(); 197 smp_send_stop(); 198 199 local_irq_disable(); 200 while (1); 201 } 202 203 /* 204 * Power-off simply requires that the secondary CPUs stop performing any 205 * activity (executing tasks, handling interrupts). smp_send_stop() 206 * achieves this. When the system power is turned off, it will take all CPUs 207 * with it. 208 */ 209 void machine_power_off(void) 210 { 211 local_irq_disable(); 212 smp_send_stop(); 213 214 if (pm_power_off) 215 pm_power_off(); 216 } 217 218 /* 219 * Restart requires that the secondary CPUs stop performing any activity 220 * while the primary CPU resets the system. Systems with a single CPU can 221 * use soft_restart() as their machine descriptor's .restart hook, since that 222 * will cause the only available CPU to reset. Systems with multiple CPUs must 223 * provide a HW restart implementation, to ensure that all CPUs reset at once. 224 * This is required so that any code running after reset on the primary CPU 225 * doesn't have to co-ordinate with other CPUs to ensure they aren't still 226 * executing pre-reset code, and using RAM that the primary CPU's code wishes 227 * to use. Implementing such co-ordination would be essentially impossible. 228 */ 229 void machine_restart(char *cmd) 230 { 231 local_irq_disable(); 232 smp_send_stop(); 233 234 if (arm_pm_restart) 235 arm_pm_restart(reboot_mode, cmd); 236 else 237 do_kernel_restart(cmd); 238 239 /* Give a grace period for failure to restart of 1s */ 240 mdelay(1000); 241 242 /* Whoops - the platform was unable to reboot. Tell the user! */ 243 printk("Reboot failed -- System halted\n"); 244 local_irq_disable(); 245 while (1); 246 } 247 248 void __show_regs(struct pt_regs *regs) 249 { 250 unsigned long flags; 251 char buf[64]; 252 253 show_regs_print_info(KERN_DEFAULT); 254 255 print_symbol("PC is at %s\n", instruction_pointer(regs)); 256 print_symbol("LR is at %s\n", regs->ARM_lr); 257 printk("pc : [<%08lx>] lr : [<%08lx>] psr: %08lx\n" 258 "sp : %08lx ip : %08lx fp : %08lx\n", 259 regs->ARM_pc, regs->ARM_lr, regs->ARM_cpsr, 260 regs->ARM_sp, regs->ARM_ip, regs->ARM_fp); 261 printk("r10: %08lx r9 : %08lx r8 : %08lx\n", 262 regs->ARM_r10, regs->ARM_r9, 263 regs->ARM_r8); 264 printk("r7 : %08lx r6 : %08lx r5 : %08lx r4 : %08lx\n", 265 regs->ARM_r7, regs->ARM_r6, 266 regs->ARM_r5, regs->ARM_r4); 267 printk("r3 : %08lx r2 : %08lx r1 : %08lx r0 : %08lx\n", 268 regs->ARM_r3, regs->ARM_r2, 269 regs->ARM_r1, regs->ARM_r0); 270 271 flags = regs->ARM_cpsr; 272 buf[0] = flags & PSR_N_BIT ? 'N' : 'n'; 273 buf[1] = flags & PSR_Z_BIT ? 'Z' : 'z'; 274 buf[2] = flags & PSR_C_BIT ? 'C' : 'c'; 275 buf[3] = flags & PSR_V_BIT ? 'V' : 'v'; 276 buf[4] = '\0'; 277 278 #ifndef CONFIG_CPU_V7M 279 printk("Flags: %s IRQs o%s FIQs o%s Mode %s ISA %s Segment %s\n", 280 buf, interrupts_enabled(regs) ? "n" : "ff", 281 fast_interrupts_enabled(regs) ? "n" : "ff", 282 processor_modes[processor_mode(regs)], 283 isa_modes[isa_mode(regs)], 284 get_fs() == get_ds() ? "kernel" : "user"); 285 #else 286 printk("xPSR: %08lx\n", regs->ARM_cpsr); 287 #endif 288 289 #ifdef CONFIG_CPU_CP15 290 { 291 unsigned int ctrl; 292 293 buf[0] = '\0'; 294 #ifdef CONFIG_CPU_CP15_MMU 295 { 296 unsigned int transbase, dac; 297 asm("mrc p15, 0, %0, c2, c0\n\t" 298 "mrc p15, 0, %1, c3, c0\n" 299 : "=r" (transbase), "=r" (dac)); 300 snprintf(buf, sizeof(buf), " Table: %08x DAC: %08x", 301 transbase, dac); 302 } 303 #endif 304 asm("mrc p15, 0, %0, c1, c0\n" : "=r" (ctrl)); 305 306 printk("Control: %08x%s\n", ctrl, buf); 307 } 308 #endif 309 } 310 311 void show_regs(struct pt_regs * regs) 312 { 313 __show_regs(regs); 314 dump_stack(); 315 } 316 317 ATOMIC_NOTIFIER_HEAD(thread_notify_head); 318 319 EXPORT_SYMBOL_GPL(thread_notify_head); 320 321 /* 322 * Free current thread data structures etc.. 323 */ 324 void exit_thread(void) 325 { 326 thread_notify(THREAD_NOTIFY_EXIT, current_thread_info()); 327 } 328 329 void flush_thread(void) 330 { 331 struct thread_info *thread = current_thread_info(); 332 struct task_struct *tsk = current; 333 334 flush_ptrace_hw_breakpoint(tsk); 335 336 memset(thread->used_cp, 0, sizeof(thread->used_cp)); 337 memset(&tsk->thread.debug, 0, sizeof(struct debug_info)); 338 memset(&thread->fpstate, 0, sizeof(union fp_state)); 339 340 flush_tls(); 341 342 thread_notify(THREAD_NOTIFY_FLUSH, thread); 343 } 344 345 void release_thread(struct task_struct *dead_task) 346 { 347 } 348 349 asmlinkage void ret_from_fork(void) __asm__("ret_from_fork"); 350 351 int 352 copy_thread(unsigned long clone_flags, unsigned long stack_start, 353 unsigned long stk_sz, struct task_struct *p) 354 { 355 struct thread_info *thread = task_thread_info(p); 356 struct pt_regs *childregs = task_pt_regs(p); 357 358 memset(&thread->cpu_context, 0, sizeof(struct cpu_context_save)); 359 360 if (likely(!(p->flags & PF_KTHREAD))) { 361 *childregs = *current_pt_regs(); 362 childregs->ARM_r0 = 0; 363 if (stack_start) 364 childregs->ARM_sp = stack_start; 365 } else { 366 memset(childregs, 0, sizeof(struct pt_regs)); 367 thread->cpu_context.r4 = stk_sz; 368 thread->cpu_context.r5 = stack_start; 369 childregs->ARM_cpsr = SVC_MODE; 370 } 371 thread->cpu_context.pc = (unsigned long)ret_from_fork; 372 thread->cpu_context.sp = (unsigned long)childregs; 373 374 clear_ptrace_hw_breakpoint(p); 375 376 if (clone_flags & CLONE_SETTLS) 377 thread->tp_value[0] = childregs->ARM_r3; 378 thread->tp_value[1] = get_tpuser(); 379 380 thread_notify(THREAD_NOTIFY_COPY, thread); 381 382 return 0; 383 } 384 385 /* 386 * Fill in the task's elfregs structure for a core dump. 387 */ 388 int dump_task_regs(struct task_struct *t, elf_gregset_t *elfregs) 389 { 390 elf_core_copy_regs(elfregs, task_pt_regs(t)); 391 return 1; 392 } 393 394 /* 395 * fill in the fpe structure for a core dump... 396 */ 397 int dump_fpu (struct pt_regs *regs, struct user_fp *fp) 398 { 399 struct thread_info *thread = current_thread_info(); 400 int used_math = thread->used_cp[1] | thread->used_cp[2]; 401 402 if (used_math) 403 memcpy(fp, &thread->fpstate.soft, sizeof (*fp)); 404 405 return used_math != 0; 406 } 407 EXPORT_SYMBOL(dump_fpu); 408 409 unsigned long get_wchan(struct task_struct *p) 410 { 411 struct stackframe frame; 412 unsigned long stack_page; 413 int count = 0; 414 if (!p || p == current || p->state == TASK_RUNNING) 415 return 0; 416 417 frame.fp = thread_saved_fp(p); 418 frame.sp = thread_saved_sp(p); 419 frame.lr = 0; /* recovered from the stack */ 420 frame.pc = thread_saved_pc(p); 421 stack_page = (unsigned long)task_stack_page(p); 422 do { 423 if (frame.sp < stack_page || 424 frame.sp >= stack_page + THREAD_SIZE || 425 unwind_frame(&frame) < 0) 426 return 0; 427 if (!in_sched_functions(frame.pc)) 428 return frame.pc; 429 } while (count ++ < 16); 430 return 0; 431 } 432 433 unsigned long arch_randomize_brk(struct mm_struct *mm) 434 { 435 unsigned long range_end = mm->brk + 0x02000000; 436 return randomize_range(mm->brk, range_end, 0) ? : mm->brk; 437 } 438 439 #ifdef CONFIG_MMU 440 #ifdef CONFIG_KUSER_HELPERS 441 /* 442 * The vectors page is always readable from user space for the 443 * atomic helpers. Insert it into the gate_vma so that it is visible 444 * through ptrace and /proc/<pid>/mem. 445 */ 446 static struct vm_area_struct gate_vma = { 447 .vm_start = 0xffff0000, 448 .vm_end = 0xffff0000 + PAGE_SIZE, 449 .vm_flags = VM_READ | VM_EXEC | VM_MAYREAD | VM_MAYEXEC, 450 }; 451 452 static int __init gate_vma_init(void) 453 { 454 gate_vma.vm_page_prot = PAGE_READONLY_EXEC; 455 return 0; 456 } 457 arch_initcall(gate_vma_init); 458 459 struct vm_area_struct *get_gate_vma(struct mm_struct *mm) 460 { 461 return &gate_vma; 462 } 463 464 int in_gate_area(struct mm_struct *mm, unsigned long addr) 465 { 466 return (addr >= gate_vma.vm_start) && (addr < gate_vma.vm_end); 467 } 468 469 int in_gate_area_no_mm(unsigned long addr) 470 { 471 return in_gate_area(NULL, addr); 472 } 473 #define is_gate_vma(vma) ((vma) == &gate_vma) 474 #else 475 #define is_gate_vma(vma) 0 476 #endif 477 478 const char *arch_vma_name(struct vm_area_struct *vma) 479 { 480 return is_gate_vma(vma) ? "[vectors]" : NULL; 481 } 482 483 /* If possible, provide a placement hint at a random offset from the 484 * stack for the signal page. 485 */ 486 static unsigned long sigpage_addr(const struct mm_struct *mm, 487 unsigned int npages) 488 { 489 unsigned long offset; 490 unsigned long first; 491 unsigned long last; 492 unsigned long addr; 493 unsigned int slots; 494 495 first = PAGE_ALIGN(mm->start_stack); 496 497 last = TASK_SIZE - (npages << PAGE_SHIFT); 498 499 /* No room after stack? */ 500 if (first > last) 501 return 0; 502 503 /* Just enough room? */ 504 if (first == last) 505 return first; 506 507 slots = ((last - first) >> PAGE_SHIFT) + 1; 508 509 offset = get_random_int() % slots; 510 511 addr = first + (offset << PAGE_SHIFT); 512 513 return addr; 514 } 515 516 static struct page *signal_page; 517 extern struct page *get_signal_page(void); 518 519 static const struct vm_special_mapping sigpage_mapping = { 520 .name = "[sigpage]", 521 .pages = &signal_page, 522 }; 523 524 int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp) 525 { 526 struct mm_struct *mm = current->mm; 527 struct vm_area_struct *vma; 528 unsigned long addr; 529 unsigned long hint; 530 int ret = 0; 531 532 if (!signal_page) 533 signal_page = get_signal_page(); 534 if (!signal_page) 535 return -ENOMEM; 536 537 down_write(&mm->mmap_sem); 538 hint = sigpage_addr(mm, 1); 539 addr = get_unmapped_area(NULL, hint, PAGE_SIZE, 0, 0); 540 if (IS_ERR_VALUE(addr)) { 541 ret = addr; 542 goto up_fail; 543 } 544 545 vma = _install_special_mapping(mm, addr, PAGE_SIZE, 546 VM_READ | VM_EXEC | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC, 547 &sigpage_mapping); 548 549 if (IS_ERR(vma)) { 550 ret = PTR_ERR(vma); 551 goto up_fail; 552 } 553 554 mm->context.sigpage = addr; 555 556 up_fail: 557 up_write(&mm->mmap_sem); 558 return ret; 559 } 560 #endif 561