Merge branch 'linux-linaro-lsk-v4.4' into linux-linaro-lsk-v4.4-android
[firefly-linux-kernel-4.4.55.git] / arch / arm64 / kernel / process.c
1 /*
2  * Based on arch/arm/kernel/process.c
3  *
4  * Original Copyright (C) 1995  Linus Torvalds
5  * Copyright (C) 1996-2000 Russell King - Converted to ARM.
6  * Copyright (C) 2012 ARM Ltd.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include <stdarg.h>
22
23 #include <linux/compat.h>
24 #include <linux/efi.h>
25 #include <linux/export.h>
26 #include <linux/sched.h>
27 #include <linux/kernel.h>
28 #include <linux/mm.h>
29 #include <linux/stddef.h>
30 #include <linux/unistd.h>
31 #include <linux/user.h>
32 #include <linux/delay.h>
33 #include <linux/reboot.h>
34 #include <linux/interrupt.h>
35 #include <linux/kallsyms.h>
36 #include <linux/init.h>
37 #include <linux/cpu.h>
38 #include <linux/elfcore.h>
39 #include <linux/pm.h>
40 #include <linux/tick.h>
41 #include <linux/utsname.h>
42 #include <linux/uaccess.h>
43 #include <linux/random.h>
44 #include <linux/hw_breakpoint.h>
45 #include <linux/personality.h>
46 #include <linux/notifier.h>
47 #include <trace/events/power.h>
48
49 #include <asm/compat.h>
50 #include <asm/cacheflush.h>
51 #include <asm/fpsimd.h>
52 #include <asm/mmu_context.h>
53 #include <asm/processor.h>
54 #include <asm/stacktrace.h>
55
56 #ifdef CONFIG_CC_STACKPROTECTOR
57 #include <linux/stackprotector.h>
58 unsigned long __stack_chk_guard __read_mostly;
59 EXPORT_SYMBOL(__stack_chk_guard);
60 #endif
61
62 /*
63  * Function pointers to optional machine specific functions
64  */
65 void (*pm_power_off)(void);
66 EXPORT_SYMBOL_GPL(pm_power_off);
67
68 void (*arm_pm_restart)(enum reboot_mode reboot_mode, const char *cmd);
69
70 /*
71  * This is our default idle handler.
72  */
73 void arch_cpu_idle(void)
74 {
75         /*
76          * This should do all the clock switching and wait for interrupt
77          * tricks
78          */
79         trace_cpu_idle_rcuidle(1, smp_processor_id());
80         cpu_do_idle();
81         local_irq_enable();
82         trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, smp_processor_id());
83 }
84
85 #ifdef CONFIG_HOTPLUG_CPU
86 void arch_cpu_idle_dead(void)
87 {
88        cpu_die();
89 }
90 #endif
91
92 /*
93  * Called by kexec, immediately prior to machine_kexec().
94  *
95  * This must completely disable all secondary CPUs; simply causing those CPUs
96  * to execute e.g. a RAM-based pin loop is not sufficient. This allows the
97  * kexec'd kernel to use any and all RAM as it sees fit, without having to
98  * avoid any code or data used by any SW CPU pin loop. The CPU hotplug
99  * functionality embodied in disable_nonboot_cpus() to achieve this.
100  */
101 void machine_shutdown(void)
102 {
103         disable_nonboot_cpus();
104 }
105
106 /*
107  * Halting simply requires that the secondary CPUs stop performing any
108  * activity (executing tasks, handling interrupts). smp_send_stop()
109  * achieves this.
110  */
111 void machine_halt(void)
112 {
113         local_irq_disable();
114         smp_send_stop();
115         while (1);
116 }
117
118 /*
119  * Power-off simply requires that the secondary CPUs stop performing any
120  * activity (executing tasks, handling interrupts). smp_send_stop()
121  * achieves this. When the system power is turned off, it will take all CPUs
122  * with it.
123  */
124 void machine_power_off(void)
125 {
126         local_irq_disable();
127         smp_send_stop();
128         if (pm_power_off)
129                 pm_power_off();
130 }
131
132 /*
133  * Restart requires that the secondary CPUs stop performing any activity
134  * while the primary CPU resets the system. Systems with multiple CPUs must
135  * provide a HW restart implementation, to ensure that all CPUs reset at once.
136  * This is required so that any code running after reset on the primary CPU
137  * doesn't have to co-ordinate with other CPUs to ensure they aren't still
138  * executing pre-reset code, and using RAM that the primary CPU's code wishes
139  * to use. Implementing such co-ordination would be essentially impossible.
140  */
141 void machine_restart(char *cmd)
142 {
143         /* Disable interrupts first */
144         local_irq_disable();
145         smp_send_stop();
146
147         /*
148          * UpdateCapsule() depends on the system being reset via
149          * ResetSystem().
150          */
151         if (efi_enabled(EFI_RUNTIME_SERVICES))
152                 efi_reboot(reboot_mode, NULL);
153
154         /* Now call the architecture specific reboot code. */
155         if (arm_pm_restart)
156                 arm_pm_restart(reboot_mode, cmd);
157         else
158                 do_kernel_restart(cmd);
159
160         /*
161          * Whoops - the architecture was unable to reboot.
162          */
163         printk("Reboot failed -- System halted\n");
164         while (1);
165 }
166
167 /*
168  * dump a block of kernel memory from around the given address
169  */
170 static void show_data(unsigned long addr, int nbytes, const char *name)
171 {
172         int     i, j;
173         int     nlines;
174         u32     *p;
175
176         /*
177          * don't attempt to dump non-kernel addresses or
178          * values that are probably just small negative numbers
179          */
180         if (addr < PAGE_OFFSET || addr > -256UL)
181                 return;
182
183         printk("\n%s: %#lx:\n", name, addr);
184
185         /*
186          * round address down to a 32 bit boundary
187          * and always dump a multiple of 32 bytes
188          */
189         p = (u32 *)(addr & ~(sizeof(u32) - 1));
190         nbytes += (addr & (sizeof(u32) - 1));
191         nlines = (nbytes + 31) / 32;
192
193
194         for (i = 0; i < nlines; i++) {
195                 /*
196                  * just display low 16 bits of address to keep
197                  * each line of the dump < 80 characters
198                  */
199                 printk("%04lx ", (unsigned long)p & 0xffff);
200                 for (j = 0; j < 8; j++) {
201                         u32     data;
202                         if (probe_kernel_address(p, data)) {
203                                 printk(" ********");
204                         } else {
205                                 printk(" %08x", data);
206                         }
207                         ++p;
208                 }
209                 printk("\n");
210         }
211 }
212
213 static void show_extra_register_data(struct pt_regs *regs, int nbytes)
214 {
215         mm_segment_t fs;
216         unsigned int i;
217
218         fs = get_fs();
219         set_fs(KERNEL_DS);
220         show_data(regs->pc - nbytes, nbytes * 2, "PC");
221         show_data(regs->regs[30] - nbytes, nbytes * 2, "LR");
222         show_data(regs->sp - nbytes, nbytes * 2, "SP");
223         for (i = 0; i < 30; i++) {
224                 char name[4];
225                 snprintf(name, sizeof(name), "X%u", i);
226                 show_data(regs->regs[i] - nbytes, nbytes * 2, name);
227         }
228         set_fs(fs);
229 }
230
231 void __show_regs(struct pt_regs *regs)
232 {
233         int i, top_reg;
234         u64 lr, sp;
235
236         if (compat_user_mode(regs)) {
237                 lr = regs->compat_lr;
238                 sp = regs->compat_sp;
239                 top_reg = 12;
240         } else {
241                 lr = regs->regs[30];
242                 sp = regs->sp;
243                 top_reg = 29;
244         }
245
246         show_regs_print_info(KERN_DEFAULT);
247         print_symbol("PC is at %s\n", instruction_pointer(regs));
248         print_symbol("LR is at %s\n", lr);
249         printk("pc : [<%016llx>] lr : [<%016llx>] pstate: %08llx\n",
250                regs->pc, lr, regs->pstate);
251         printk("sp : %016llx\n", sp);
252         for (i = top_reg; i >= 0; i--) {
253                 printk("x%-2d: %016llx ", i, regs->regs[i]);
254                 if (i % 2 == 0)
255                         printk("\n");
256         }
257         if (!user_mode(regs))
258                 show_extra_register_data(regs, 128);
259         printk("\n");
260 }
261
262 void show_regs(struct pt_regs * regs)
263 {
264         printk("\n");
265         __show_regs(regs);
266 }
267
268 /*
269  * Free current thread data structures etc..
270  */
271 void exit_thread(void)
272 {
273 }
274
275 static void tls_thread_flush(void)
276 {
277         asm ("msr tpidr_el0, xzr");
278
279         if (is_compat_task()) {
280                 current->thread.tp_value = 0;
281
282                 /*
283                  * We need to ensure ordering between the shadow state and the
284                  * hardware state, so that we don't corrupt the hardware state
285                  * with a stale shadow state during context switch.
286                  */
287                 barrier();
288                 asm ("msr tpidrro_el0, xzr");
289         }
290 }
291
292 void flush_thread(void)
293 {
294         fpsimd_flush_thread();
295         tls_thread_flush();
296         flush_ptrace_hw_breakpoint(current);
297 }
298
299 void release_thread(struct task_struct *dead_task)
300 {
301 }
302
303 int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
304 {
305         if (current->mm)
306                 fpsimd_preserve_current_state();
307         *dst = *src;
308         return 0;
309 }
310
311 asmlinkage void ret_from_fork(void) asm("ret_from_fork");
312
313 int copy_thread(unsigned long clone_flags, unsigned long stack_start,
314                 unsigned long stk_sz, struct task_struct *p)
315 {
316         struct pt_regs *childregs = task_pt_regs(p);
317
318         memset(&p->thread.cpu_context, 0, sizeof(struct cpu_context));
319
320         if (likely(!(p->flags & PF_KTHREAD))) {
321                 *childregs = *current_pt_regs();
322                 childregs->regs[0] = 0;
323
324                 /*
325                  * Read the current TLS pointer from tpidr_el0 as it may be
326                  * out-of-sync with the saved value.
327                  */
328                 asm("mrs %0, tpidr_el0" : "=r" (*task_user_tls(p)));
329
330                 if (stack_start) {
331                         if (is_compat_thread(task_thread_info(p)))
332                                 childregs->compat_sp = stack_start;
333                         /* 16-byte aligned stack mandatory on AArch64 */
334                         else if (stack_start & 15)
335                                 return -EINVAL;
336                         else
337                                 childregs->sp = stack_start;
338                 }
339
340                 /*
341                  * If a TLS pointer was passed to clone (4th argument), use it
342                  * for the new thread.
343                  */
344                 if (clone_flags & CLONE_SETTLS)
345                         p->thread.tp_value = childregs->regs[3];
346         } else {
347                 memset(childregs, 0, sizeof(struct pt_regs));
348                 childregs->pstate = PSR_MODE_EL1h;
349                 p->thread.cpu_context.x19 = stack_start;
350                 p->thread.cpu_context.x20 = stk_sz;
351         }
352         p->thread.cpu_context.pc = (unsigned long)ret_from_fork;
353         p->thread.cpu_context.sp = (unsigned long)childregs;
354
355         ptrace_hw_copy_thread(p);
356
357         return 0;
358 }
359
360 static void tls_thread_switch(struct task_struct *next)
361 {
362         unsigned long tpidr, tpidrro;
363
364         asm("mrs %0, tpidr_el0" : "=r" (tpidr));
365         *task_user_tls(current) = tpidr;
366
367         tpidr = *task_user_tls(next);
368         tpidrro = is_compat_thread(task_thread_info(next)) ?
369                   next->thread.tp_value : 0;
370
371         asm(
372         "       msr     tpidr_el0, %0\n"
373         "       msr     tpidrro_el0, %1"
374         : : "r" (tpidr), "r" (tpidrro));
375 }
376
377 /*
378  * Thread switching.
379  */
380 struct task_struct *__switch_to(struct task_struct *prev,
381                                 struct task_struct *next)
382 {
383         struct task_struct *last;
384
385         fpsimd_thread_switch(next);
386         tls_thread_switch(next);
387         hw_breakpoint_thread_switch(next);
388         contextidr_thread_switch(next);
389
390         /*
391          * Complete any pending TLB or cache maintenance on this CPU in case
392          * the thread migrates to a different CPU.
393          */
394         dsb(ish);
395
396         /* the actual thread switch */
397         last = cpu_switch_to(prev, next);
398
399         return last;
400 }
401
402 unsigned long get_wchan(struct task_struct *p)
403 {
404         struct stackframe frame;
405         unsigned long stack_page;
406         int count = 0;
407         if (!p || p == current || p->state == TASK_RUNNING)
408                 return 0;
409
410         frame.fp = thread_saved_fp(p);
411         frame.sp = thread_saved_sp(p);
412         frame.pc = thread_saved_pc(p);
413         stack_page = (unsigned long)task_stack_page(p);
414         do {
415                 if (frame.sp < stack_page ||
416                     frame.sp >= stack_page + THREAD_SIZE ||
417                     unwind_frame(&frame))
418                         return 0;
419                 if (!in_sched_functions(frame.pc))
420                         return frame.pc;
421         } while (count ++ < 16);
422         return 0;
423 }
424
425 unsigned long arch_align_stack(unsigned long sp)
426 {
427         if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
428                 sp -= get_random_int() & ~PAGE_MASK;
429         return sp & ~0xf;
430 }
431
432 static unsigned long randomize_base(unsigned long base)
433 {
434         unsigned long range_end = base + (STACK_RND_MASK << PAGE_SHIFT) + 1;
435         return randomize_range(base, range_end, 0) ? : base;
436 }
437
438 unsigned long arch_randomize_brk(struct mm_struct *mm)
439 {
440         return randomize_base(mm->brk);
441 }