• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/sched/debug.h>
28 #include <linux/sched/task.h>
29 #include <linux/sched/task_stack.h>
30 #include <linux/kernel.h>
31 #include <linux/mm.h>
32 #include <linux/stddef.h>
33 #include <linux/unistd.h>
34 #include <linux/user.h>
35 #include <linux/delay.h>
36 #include <linux/reboot.h>
37 #include <linux/interrupt.h>
38 #include <linux/kallsyms.h>
39 #include <linux/init.h>
40 #include <linux/cpu.h>
41 #include <linux/elfcore.h>
42 #include <linux/pm.h>
43 #include <linux/tick.h>
44 #include <linux/utsname.h>
45 #include <linux/uaccess.h>
46 #include <linux/random.h>
47 #include <linux/hw_breakpoint.h>
48 #include <linux/personality.h>
49 #include <linux/notifier.h>
50 #include <trace/events/power.h>
51 #include <linux/percpu.h>
52 
53 #include <asm/alternative.h>
54 #include <asm/compat.h>
55 #include <asm/cacheflush.h>
56 #include <asm/exec.h>
57 #include <asm/fpsimd.h>
58 #include <asm/mmu_context.h>
59 #include <asm/processor.h>
60 #include <asm/stacktrace.h>
61 
62 #ifdef CONFIG_CC_STACKPROTECTOR
63 #include <linux/stackprotector.h>
64 unsigned long __stack_chk_guard __read_mostly;
65 EXPORT_SYMBOL(__stack_chk_guard);
66 #endif
67 
68 /*
69  * Function pointers to optional machine specific functions
70  */
71 void (*pm_power_off)(void);
72 EXPORT_SYMBOL_GPL(pm_power_off);
73 
74 void (*arm_pm_restart)(enum reboot_mode reboot_mode, const char *cmd);
75 
76 /*
77  * This is our default idle handler.
78  */
arch_cpu_idle(void)79 void arch_cpu_idle(void)
80 {
81 	/*
82 	 * This should do all the clock switching and wait for interrupt
83 	 * tricks
84 	 */
85 	trace_cpu_idle_rcuidle(1, smp_processor_id());
86 	cpu_do_idle();
87 	local_irq_enable();
88 	trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, smp_processor_id());
89 }
90 
91 #ifdef CONFIG_HOTPLUG_CPU
arch_cpu_idle_dead(void)92 void arch_cpu_idle_dead(void)
93 {
94        cpu_die();
95 }
96 #endif
97 
98 /*
99  * Called by kexec, immediately prior to machine_kexec().
100  *
101  * This must completely disable all secondary CPUs; simply causing those CPUs
102  * to execute e.g. a RAM-based pin loop is not sufficient. This allows the
103  * kexec'd kernel to use any and all RAM as it sees fit, without having to
104  * avoid any code or data used by any SW CPU pin loop. The CPU hotplug
105  * functionality embodied in disable_nonboot_cpus() to achieve this.
106  */
machine_shutdown(void)107 void machine_shutdown(void)
108 {
109 	disable_nonboot_cpus();
110 }
111 
112 /*
113  * Halting simply requires that the secondary CPUs stop performing any
114  * activity (executing tasks, handling interrupts). smp_send_stop()
115  * achieves this.
116  */
machine_halt(void)117 void machine_halt(void)
118 {
119 	local_irq_disable();
120 	smp_send_stop();
121 	while (1);
122 }
123 
124 /*
125  * Power-off simply requires that the secondary CPUs stop performing any
126  * activity (executing tasks, handling interrupts). smp_send_stop()
127  * achieves this. When the system power is turned off, it will take all CPUs
128  * with it.
129  */
machine_power_off(void)130 void machine_power_off(void)
131 {
132 	local_irq_disable();
133 	smp_send_stop();
134 	if (pm_power_off)
135 		pm_power_off();
136 }
137 
138 /*
139  * Restart requires that the secondary CPUs stop performing any activity
140  * while the primary CPU resets the system. Systems with multiple CPUs must
141  * provide a HW restart implementation, to ensure that all CPUs reset at once.
142  * This is required so that any code running after reset on the primary CPU
143  * doesn't have to co-ordinate with other CPUs to ensure they aren't still
144  * executing pre-reset code, and using RAM that the primary CPU's code wishes
145  * to use. Implementing such co-ordination would be essentially impossible.
146  */
machine_restart(char * cmd)147 void machine_restart(char *cmd)
148 {
149 	/* Disable interrupts first */
150 	local_irq_disable();
151 	smp_send_stop();
152 
153 	/*
154 	 * UpdateCapsule() depends on the system being reset via
155 	 * ResetSystem().
156 	 */
157 	if (efi_enabled(EFI_RUNTIME_SERVICES))
158 		efi_reboot(reboot_mode, NULL);
159 
160 	/* Now call the architecture specific reboot code. */
161 	if (arm_pm_restart)
162 		arm_pm_restart(reboot_mode, cmd);
163 	else
164 		do_kernel_restart(cmd);
165 
166 	/*
167 	 * Whoops - the architecture was unable to reboot.
168 	 */
169 	printk("Reboot failed -- System halted\n");
170 	while (1);
171 }
172 
173 /*
174  * dump a block of kernel memory from around the given address
175  */
show_data(unsigned long addr,int nbytes,const char * name)176 static void show_data(unsigned long addr, int nbytes, const char *name)
177 {
178 	int	i, j;
179 	int	nlines;
180 	u32	*p;
181 
182 	/*
183 	 * don't attempt to dump non-kernel addresses or
184 	 * values that are probably just small negative numbers
185 	 */
186 	if (addr < PAGE_OFFSET || addr > -256UL)
187 		return;
188 
189 	printk("\n%s: %#lx:\n", name, addr);
190 
191 	/*
192 	 * round address down to a 32 bit boundary
193 	 * and always dump a multiple of 32 bytes
194 	 */
195 	p = (u32 *)(addr & ~(sizeof(u32) - 1));
196 	nbytes += (addr & (sizeof(u32) - 1));
197 	nlines = (nbytes + 31) / 32;
198 
199 
200 	for (i = 0; i < nlines; i++) {
201 		/*
202 		 * just display low 16 bits of address to keep
203 		 * each line of the dump < 80 characters
204 		 */
205 		printk("%04lx ", (unsigned long)p & 0xffff);
206 		for (j = 0; j < 8; j++) {
207 			u32	data;
208 			if (probe_kernel_address(p, data)) {
209 				pr_cont(" ********");
210 			} else {
211 				pr_cont(" %08x", data);
212 			}
213 			++p;
214 		}
215 		pr_cont("\n");
216 	}
217 }
218 
show_extra_register_data(struct pt_regs * regs,int nbytes)219 static void show_extra_register_data(struct pt_regs *regs, int nbytes)
220 {
221 	mm_segment_t fs;
222 	unsigned int i;
223 
224 	fs = get_fs();
225 	set_fs(KERNEL_DS);
226 	show_data(regs->pc - nbytes, nbytes * 2, "PC");
227 	show_data(regs->regs[30] - nbytes, nbytes * 2, "LR");
228 	show_data(regs->sp - nbytes, nbytes * 2, "SP");
229 	for (i = 0; i < 30; i++) {
230 		char name[4];
231 		snprintf(name, sizeof(name), "X%u", i);
232 		show_data(regs->regs[i] - nbytes, nbytes * 2, name);
233 	}
234 	set_fs(fs);
235 }
236 
__show_regs(struct pt_regs * regs)237 void __show_regs(struct pt_regs *regs)
238 {
239 	int i, top_reg;
240 	u64 lr, sp;
241 
242 	if (compat_user_mode(regs)) {
243 		lr = regs->compat_lr;
244 		sp = regs->compat_sp;
245 		top_reg = 12;
246 	} else {
247 		lr = regs->regs[30];
248 		sp = regs->sp;
249 		top_reg = 29;
250 	}
251 
252 	show_regs_print_info(KERN_DEFAULT);
253 	print_symbol("PC is at %s\n", instruction_pointer(regs));
254 	print_symbol("LR is at %s\n", lr);
255 	printk("pc : [<%016llx>] lr : [<%016llx>] pstate: %08llx\n",
256 	       regs->pc, lr, regs->pstate);
257 	printk("sp : %016llx\n", sp);
258 
259 	i = top_reg;
260 
261 	while (i >= 0) {
262 		printk("x%-2d: %016llx ", i, regs->regs[i]);
263 		i--;
264 
265 		if (i % 2 == 0) {
266 			pr_cont("x%-2d: %016llx ", i, regs->regs[i]);
267 			i--;
268 		}
269 
270 		pr_cont("\n");
271 	}
272 	if (!user_mode(regs))
273 		show_extra_register_data(regs, 128);
274 	printk("\n");
275 }
276 
show_regs(struct pt_regs * regs)277 void show_regs(struct pt_regs * regs)
278 {
279 	__show_regs(regs);
280 	dump_backtrace(regs, NULL);
281 }
282 
tls_thread_flush(void)283 static void tls_thread_flush(void)
284 {
285 	write_sysreg(0, tpidr_el0);
286 
287 	if (is_compat_task()) {
288 		current->thread.tp_value = 0;
289 
290 		/*
291 		 * We need to ensure ordering between the shadow state and the
292 		 * hardware state, so that we don't corrupt the hardware state
293 		 * with a stale shadow state during context switch.
294 		 */
295 		barrier();
296 		write_sysreg(0, tpidrro_el0);
297 	}
298 }
299 
flush_thread(void)300 void flush_thread(void)
301 {
302 	fpsimd_flush_thread();
303 	tls_thread_flush();
304 	flush_ptrace_hw_breakpoint(current);
305 }
306 
release_thread(struct task_struct * dead_task)307 void release_thread(struct task_struct *dead_task)
308 {
309 }
310 
arch_dup_task_struct(struct task_struct * dst,struct task_struct * src)311 int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
312 {
313 	if (current->mm)
314 		fpsimd_preserve_current_state();
315 	*dst = *src;
316 	return 0;
317 }
318 
319 asmlinkage void ret_from_fork(void) asm("ret_from_fork");
320 
copy_thread(unsigned long clone_flags,unsigned long stack_start,unsigned long stk_sz,struct task_struct * p)321 int copy_thread(unsigned long clone_flags, unsigned long stack_start,
322 		unsigned long stk_sz, struct task_struct *p)
323 {
324 	struct pt_regs *childregs = task_pt_regs(p);
325 
326 	memset(&p->thread.cpu_context, 0, sizeof(struct cpu_context));
327 
328 	/*
329 	 * In case p was allocated the same task_struct pointer as some
330 	 * other recently-exited task, make sure p is disassociated from
331 	 * any cpu that may have run that now-exited task recently.
332 	 * Otherwise we could erroneously skip reloading the FPSIMD
333 	 * registers for p.
334 	 */
335 	fpsimd_flush_task_state(p);
336 
337 	if (likely(!(p->flags & PF_KTHREAD))) {
338 		*childregs = *current_pt_regs();
339 		childregs->regs[0] = 0;
340 
341 		/*
342 		 * Read the current TLS pointer from tpidr_el0 as it may be
343 		 * out-of-sync with the saved value.
344 		 */
345 		*task_user_tls(p) = read_sysreg(tpidr_el0);
346 
347 		if (stack_start) {
348 			if (is_compat_thread(task_thread_info(p)))
349 				childregs->compat_sp = stack_start;
350 			else
351 				childregs->sp = stack_start;
352 		}
353 
354 		/*
355 		 * If a TLS pointer was passed to clone (4th argument), use it
356 		 * for the new thread.
357 		 */
358 		if (clone_flags & CLONE_SETTLS)
359 			p->thread.tp_value = childregs->regs[3];
360 	} else {
361 		memset(childregs, 0, sizeof(struct pt_regs));
362 		childregs->pstate = PSR_MODE_EL1h;
363 		if (IS_ENABLED(CONFIG_ARM64_UAO) &&
364 		    cpus_have_const_cap(ARM64_HAS_UAO))
365 			childregs->pstate |= PSR_UAO_BIT;
366 
367 		if (arm64_get_ssbd_state() == ARM64_SSBD_FORCE_DISABLE)
368 			set_ssbs_bit(childregs);
369 
370 		p->thread.cpu_context.x19 = stack_start;
371 		p->thread.cpu_context.x20 = stk_sz;
372 	}
373 	p->thread.cpu_context.pc = (unsigned long)ret_from_fork;
374 	p->thread.cpu_context.sp = (unsigned long)childregs;
375 
376 	ptrace_hw_copy_thread(p);
377 
378 	return 0;
379 }
380 
tls_preserve_current_state(void)381 void tls_preserve_current_state(void)
382 {
383 	*task_user_tls(current) = read_sysreg(tpidr_el0);
384 }
385 
tls_thread_switch(struct task_struct * next)386 static void tls_thread_switch(struct task_struct *next)
387 {
388 	tls_preserve_current_state();
389 
390 	if (is_compat_thread(task_thread_info(next)))
391 		write_sysreg(next->thread.tp_value, tpidrro_el0);
392 	else if (!arm64_kernel_unmapped_at_el0())
393 		write_sysreg(0, tpidrro_el0);
394 
395 	write_sysreg(*task_user_tls(next), tpidr_el0);
396 }
397 
398 /* Restore the UAO state depending on next's addr_limit */
uao_thread_switch(struct task_struct * next)399 void uao_thread_switch(struct task_struct *next)
400 {
401 	if (IS_ENABLED(CONFIG_ARM64_UAO)) {
402 		if (task_thread_info(next)->addr_limit == KERNEL_DS)
403 			asm(ALTERNATIVE("nop", SET_PSTATE_UAO(1), ARM64_HAS_UAO));
404 		else
405 			asm(ALTERNATIVE("nop", SET_PSTATE_UAO(0), ARM64_HAS_UAO));
406 	}
407 }
408 
409 /*
410  * Force SSBS state on context-switch, since it may be lost after migrating
411  * from a CPU which treats the bit as RES0 in a heterogeneous system.
412  */
ssbs_thread_switch(struct task_struct * next)413 static void ssbs_thread_switch(struct task_struct *next)
414 {
415 	struct pt_regs *regs = task_pt_regs(next);
416 
417 	/*
418 	 * Nothing to do for kernel threads, but 'regs' may be junk
419 	 * (e.g. idle task) so check the flags and bail early.
420 	 */
421 	if (unlikely(next->flags & PF_KTHREAD))
422 		return;
423 
424 	/*
425 	 * If all CPUs implement the SSBS extension, then we just need to
426 	 * context-switch the PSTATE field.
427 	 */
428 	if (cpu_have_feature(cpu_feature(SSBS)))
429 		return;
430 
431 	/* If the mitigation is enabled, then we leave SSBS clear. */
432 	if ((arm64_get_ssbd_state() == ARM64_SSBD_FORCE_ENABLE) ||
433 	    test_tsk_thread_flag(next, TIF_SSBD))
434 		return;
435 
436 	if (compat_user_mode(regs))
437 		set_compat_ssbs_bit(regs);
438 	else if (user_mode(regs))
439 		set_ssbs_bit(regs);
440 }
441 
442 /*
443  * We store our current task in sp_el0, which is clobbered by userspace. Keep a
444  * shadow copy so that we can restore this upon entry from userspace.
445  *
446  * This is *only* for exception entry from EL0, and is not valid until we
447  * __switch_to() a user task.
448  */
449 DEFINE_PER_CPU(struct task_struct *, __entry_task);
450 
entry_task_switch(struct task_struct * next)451 static void entry_task_switch(struct task_struct *next)
452 {
453 	__this_cpu_write(__entry_task, next);
454 }
455 
456 /*
457  * Thread switching.
458  */
__switch_to(struct task_struct * prev,struct task_struct * next)459 __notrace_funcgraph struct task_struct *__switch_to(struct task_struct *prev,
460 				struct task_struct *next)
461 {
462 	struct task_struct *last;
463 
464 	fpsimd_thread_switch(next);
465 	tls_thread_switch(next);
466 	hw_breakpoint_thread_switch(next);
467 	contextidr_thread_switch(next);
468 	entry_task_switch(next);
469 	uao_thread_switch(next);
470 	ssbs_thread_switch(next);
471 
472 	/*
473 	 * Complete any pending TLB or cache maintenance on this CPU in case
474 	 * the thread migrates to a different CPU.
475 	 * This full barrier is also required by the membarrier system
476 	 * call.
477 	 */
478 	dsb(ish);
479 
480 	/* the actual thread switch */
481 	last = cpu_switch_to(prev, next);
482 
483 	return last;
484 }
485 
get_wchan(struct task_struct * p)486 unsigned long get_wchan(struct task_struct *p)
487 {
488 	struct stackframe frame;
489 	unsigned long stack_page, ret = 0;
490 	int count = 0;
491 	if (!p || p == current || p->state == TASK_RUNNING)
492 		return 0;
493 
494 	stack_page = (unsigned long)try_get_task_stack(p);
495 	if (!stack_page)
496 		return 0;
497 
498 	frame.fp = thread_saved_fp(p);
499 	frame.pc = thread_saved_pc(p);
500 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
501 	frame.graph = p->curr_ret_stack;
502 #endif
503 	do {
504 		if (unwind_frame(p, &frame))
505 			goto out;
506 		if (!in_sched_functions(frame.pc)) {
507 			ret = frame.pc;
508 			goto out;
509 		}
510 	} while (count ++ < 16);
511 
512 out:
513 	put_task_stack(p);
514 	return ret;
515 }
516 
arch_align_stack(unsigned long sp)517 unsigned long arch_align_stack(unsigned long sp)
518 {
519 	if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
520 		sp -= get_random_int() & ~PAGE_MASK;
521 	return sp & ~0xf;
522 }
523 
arch_randomize_brk(struct mm_struct * mm)524 unsigned long arch_randomize_brk(struct mm_struct *mm)
525 {
526 	if (is_compat_task())
527 		return randomize_page(mm->brk, SZ_32M);
528 	else
529 		return randomize_page(mm->brk, SZ_1G);
530 }
531 
532 /*
533  * Called from setup_new_exec() after (COMPAT_)SET_PERSONALITY.
534  */
arch_setup_new_exec(void)535 void arch_setup_new_exec(void)
536 {
537 	current->mm->context.flags = is_compat_task() ? MMCF_AARCH32 : 0;
538 }
539