• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <linux/console.h>
36 
37 #include <asm/cacheflush.h>
38 #include <asm/idmap.h>
39 #include <asm/processor.h>
40 #include <asm/thread_notify.h>
41 #include <asm/stacktrace.h>
42 #include <asm/system_misc.h>
43 #include <asm/mach/time.h>
44 #include <asm/tls.h>
45 #include <asm/vdso.h>
46 #include "reboot.h"
47 
48 #ifdef CONFIG_CC_STACKPROTECTOR
49 #include <linux/stackprotector.h>
50 unsigned long __stack_chk_guard __read_mostly;
51 EXPORT_SYMBOL(__stack_chk_guard);
52 #endif
53 
54 static const char *processor_modes[] __maybe_unused = {
55   "USER_26", "FIQ_26" , "IRQ_26" , "SVC_26" , "UK4_26" , "UK5_26" , "UK6_26" , "UK7_26" ,
56   "UK8_26" , "UK9_26" , "UK10_26", "UK11_26", "UK12_26", "UK13_26", "UK14_26", "UK15_26",
57   "USER_32", "FIQ_32" , "IRQ_32" , "SVC_32" , "UK4_32" , "UK5_32" , "UK6_32" , "ABT_32" ,
58   "UK8_32" , "UK9_32" , "UK10_32", "UND_32" , "UK12_32", "UK13_32", "UK14_32", "SYS_32"
59 };
60 
61 static const char *isa_modes[] __maybe_unused = {
62   "ARM" , "Thumb" , "Jazelle", "ThumbEE"
63 };
64 
65 #ifdef CONFIG_SMP
arch_trigger_all_cpu_backtrace(void)66 void arch_trigger_all_cpu_backtrace(void)
67 {
68 	smp_send_all_cpu_backtrace();
69 }
70 #else
arch_trigger_all_cpu_backtrace(void)71 void arch_trigger_all_cpu_backtrace(void)
72 {
73 	dump_stack();
74 }
75 #endif
76 
77 extern void call_with_stack(void (*fn)(void *), void *arg, void *sp);
78 typedef void (*phys_reset_t)(unsigned long);
79 
80 #ifdef CONFIG_ARM_FLUSH_CONSOLE_ON_RESTART
arm_machine_flush_console(void)81 void arm_machine_flush_console(void)
82 {
83 	printk("\n");
84 	pr_emerg("Restarting %s\n", linux_banner);
85 	if (console_trylock()) {
86 		console_unlock();
87 		return;
88 	}
89 
90 	mdelay(50);
91 
92 	local_irq_disable();
93 	if (!console_trylock())
94 		pr_emerg("arm_restart: Console was locked! Busting\n");
95 	else
96 		pr_emerg("arm_restart: Console was locked!\n");
97 	console_unlock();
98 }
99 #else
arm_machine_flush_console(void)100 void arm_machine_flush_console(void)
101 {
102 }
103 #endif
104 
105 /*
106  * A temporary stack to use for CPU reset. This is static so that we
107  * don't clobber it with the identity mapping. When running with this
108  * stack, any references to the current task *will not work* so you
109  * should really do as little as possible before jumping to your reset
110  * code.
111  */
112 static u64 soft_restart_stack[16];
113 
__soft_restart(void * addr)114 static void __soft_restart(void *addr)
115 {
116 	phys_reset_t phys_reset;
117 
118 	/* Take out a flat memory mapping. */
119 	setup_mm_for_reboot();
120 
121 	/* Clean and invalidate caches */
122 	flush_cache_all();
123 
124 	/* Turn off caching */
125 	cpu_proc_fin();
126 
127 	/* Push out any further dirty data, and ensure cache is empty */
128 	flush_cache_all();
129 
130 	/* Switch to the identity mapping. */
131 	phys_reset = (phys_reset_t)(unsigned long)virt_to_phys(cpu_reset);
132 	phys_reset((unsigned long)addr);
133 
134 	/* Should never get here. */
135 	BUG();
136 }
137 
_soft_restart(unsigned long addr,bool disable_l2)138 void _soft_restart(unsigned long addr, bool disable_l2)
139 {
140 	u64 *stack = soft_restart_stack + ARRAY_SIZE(soft_restart_stack);
141 
142 	/* Disable interrupts first */
143 	raw_local_irq_disable();
144 	local_fiq_disable();
145 
146 	/* Disable the L2 if we're the last man standing. */
147 	if (disable_l2)
148 		outer_disable();
149 
150 	/* Change to the new stack and continue with the reset. */
151 	call_with_stack(__soft_restart, (void *)addr, (void *)stack);
152 
153 	/* Should never get here. */
154 	BUG();
155 }
156 
soft_restart(unsigned long addr)157 void soft_restart(unsigned long addr)
158 {
159 	_soft_restart(addr, num_online_cpus() == 1);
160 }
161 
162 /*
163  * Function pointers to optional machine specific functions
164  */
165 void (*pm_power_off)(void);
166 EXPORT_SYMBOL(pm_power_off);
167 
168 void (*arm_pm_restart)(enum reboot_mode reboot_mode, const char *cmd);
169 
170 /*
171  * This is our default idle handler.
172  */
173 
174 void (*arm_pm_idle)(void);
175 
176 /*
177  * Called from the core idle loop.
178  */
179 
arch_cpu_idle(void)180 void arch_cpu_idle(void)
181 {
182 	if (arm_pm_idle)
183 		arm_pm_idle();
184 	else
185 		cpu_do_idle();
186 	local_irq_enable();
187 }
188 
arch_cpu_idle_prepare(void)189 void arch_cpu_idle_prepare(void)
190 {
191 	local_fiq_enable();
192 }
193 
arch_cpu_idle_enter(void)194 void arch_cpu_idle_enter(void)
195 {
196 	idle_notifier_call_chain(IDLE_START);
197 	ledtrig_cpu(CPU_LED_IDLE_START);
198 #ifdef CONFIG_PL310_ERRATA_769419
199 	wmb();
200 #endif
201 }
202 
arch_cpu_idle_exit(void)203 void arch_cpu_idle_exit(void)
204 {
205 	ledtrig_cpu(CPU_LED_IDLE_END);
206 	idle_notifier_call_chain(IDLE_END);
207 }
208 
209 #ifdef CONFIG_HOTPLUG_CPU
arch_cpu_idle_dead(void)210 void arch_cpu_idle_dead(void)
211 {
212 	cpu_die();
213 }
214 #endif
215 
216 /*
217  * Called by kexec, immediately prior to machine_kexec().
218  *
219  * This must completely disable all secondary CPUs; simply causing those CPUs
220  * to execute e.g. a RAM-based pin loop is not sufficient. This allows the
221  * kexec'd kernel to use any and all RAM as it sees fit, without having to
222  * avoid any code or data used by any SW CPU pin loop. The CPU hotplug
223  * functionality embodied in disable_nonboot_cpus() to achieve this.
224  */
machine_shutdown(void)225 void machine_shutdown(void)
226 {
227 #ifdef CONFIG_SMP
228 	/*
229 	 * Disable preemption so we're guaranteed to
230 	 * run to power off or reboot and prevent
231 	 * the possibility of switching to another
232 	 * thread that might wind up blocking on
233 	 * one of the stopped CPUs.
234 	 */
235 	preempt_disable();
236 #endif
237 	disable_nonboot_cpus();
238 }
239 
240 /*
241  * Halting simply requires that the secondary CPUs stop performing any
242  * activity (executing tasks, handling interrupts). smp_send_stop()
243  * achieves this.
244  */
machine_halt(void)245 void machine_halt(void)
246 {
247 	local_irq_disable();
248 	smp_send_stop();
249 
250 	local_irq_disable();
251 	while (1);
252 }
253 
254 /*
255  * Power-off simply requires that the secondary CPUs stop performing any
256  * activity (executing tasks, handling interrupts). smp_send_stop()
257  * achieves this. When the system power is turned off, it will take all CPUs
258  * with it.
259  */
machine_power_off(void)260 void machine_power_off(void)
261 {
262 	local_irq_disable();
263 	smp_send_stop();
264 
265 	if (pm_power_off)
266 		pm_power_off();
267 }
268 
269 /*
270  * Restart requires that the secondary CPUs stop performing any activity
271  * while the primary CPU resets the system. Systems with a single CPU can
272  * use soft_restart() as their machine descriptor's .restart hook, since that
273  * will cause the only available CPU to reset. Systems with multiple CPUs must
274  * provide a HW restart implementation, to ensure that all CPUs reset at once.
275  * This is required so that any code running after reset on the primary CPU
276  * doesn't have to co-ordinate with other CPUs to ensure they aren't still
277  * executing pre-reset code, and using RAM that the primary CPU's code wishes
278  * to use. Implementing such co-ordination would be essentially impossible.
279  */
machine_restart(char * cmd)280 void machine_restart(char *cmd)
281 {
282 	local_irq_disable();
283 	smp_send_stop();
284 
285 
286 	/* Flush the console to make sure all the relevant messages make it
287 	 * out to the console drivers */
288 	arm_machine_flush_console();
289 
290 	if (arm_pm_restart)
291 		arm_pm_restart(reboot_mode, cmd);
292 	else
293 		do_kernel_restart(cmd);
294 
295 	/* Give a grace period for failure to restart of 1s */
296 	mdelay(1000);
297 
298 	/* Whoops - the platform was unable to reboot. Tell the user! */
299 	printk("Reboot failed -- System halted\n");
300 	local_irq_disable();
301 	while (1);
302 }
303 
304 /*
305  * dump a block of kernel memory from around the given address
306  */
show_data(unsigned long addr,int nbytes,const char * name)307 static void show_data(unsigned long addr, int nbytes, const char *name)
308 {
309 	int	i, j;
310 	int	nlines;
311 	u32	*p;
312 
313 	/*
314 	 * don't attempt to dump non-kernel addresses or
315 	 * values that are probably just small negative numbers
316 	 */
317 	if (addr < PAGE_OFFSET || addr > -256UL)
318 		return;
319 
320 	printk("\n%s: %#lx:\n", name, addr);
321 
322 	/*
323 	 * round address down to a 32 bit boundary
324 	 * and always dump a multiple of 32 bytes
325 	 */
326 	p = (u32 *)(addr & ~(sizeof(u32) - 1));
327 	nbytes += (addr & (sizeof(u32) - 1));
328 	nlines = (nbytes + 31) / 32;
329 
330 
331 	for (i = 0; i < nlines; i++) {
332 		/*
333 		 * just display low 16 bits of address to keep
334 		 * each line of the dump < 80 characters
335 		 */
336 		printk("%04lx ", (unsigned long)p & 0xffff);
337 		for (j = 0; j < 8; j++) {
338 			u32	data;
339 			if (probe_kernel_address(p, data)) {
340 				printk(" ********");
341 			} else {
342 				printk(" %08x", data);
343 			}
344 			++p;
345 		}
346 		printk("\n");
347 	}
348 }
349 
show_extra_register_data(struct pt_regs * regs,int nbytes)350 static void show_extra_register_data(struct pt_regs *regs, int nbytes)
351 {
352 	mm_segment_t fs;
353 
354 	fs = get_fs();
355 	set_fs(KERNEL_DS);
356 	show_data(regs->ARM_pc - nbytes, nbytes * 2, "PC");
357 	show_data(regs->ARM_lr - nbytes, nbytes * 2, "LR");
358 	show_data(regs->ARM_sp - nbytes, nbytes * 2, "SP");
359 	show_data(regs->ARM_ip - nbytes, nbytes * 2, "IP");
360 	show_data(regs->ARM_fp - nbytes, nbytes * 2, "FP");
361 	show_data(regs->ARM_r0 - nbytes, nbytes * 2, "R0");
362 	show_data(regs->ARM_r1 - nbytes, nbytes * 2, "R1");
363 	show_data(regs->ARM_r2 - nbytes, nbytes * 2, "R2");
364 	show_data(regs->ARM_r3 - nbytes, nbytes * 2, "R3");
365 	show_data(regs->ARM_r4 - nbytes, nbytes * 2, "R4");
366 	show_data(regs->ARM_r5 - nbytes, nbytes * 2, "R5");
367 	show_data(regs->ARM_r6 - nbytes, nbytes * 2, "R6");
368 	show_data(regs->ARM_r7 - nbytes, nbytes * 2, "R7");
369 	show_data(regs->ARM_r8 - nbytes, nbytes * 2, "R8");
370 	show_data(regs->ARM_r9 - nbytes, nbytes * 2, "R9");
371 	show_data(regs->ARM_r10 - nbytes, nbytes * 2, "R10");
372 	set_fs(fs);
373 }
374 
__show_regs(struct pt_regs * regs)375 void __show_regs(struct pt_regs *regs)
376 {
377 	unsigned long flags;
378 	char buf[64];
379 
380 	show_regs_print_info(KERN_DEFAULT);
381 
382 	print_symbol("PC is at %s\n", instruction_pointer(regs));
383 	print_symbol("LR is at %s\n", regs->ARM_lr);
384 	printk("pc : [<%08lx>]    lr : [<%08lx>]    psr: %08lx\n"
385 	       "sp : %08lx  ip : %08lx  fp : %08lx\n",
386 		regs->ARM_pc, regs->ARM_lr, regs->ARM_cpsr,
387 		regs->ARM_sp, regs->ARM_ip, regs->ARM_fp);
388 	printk("r10: %08lx  r9 : %08lx  r8 : %08lx\n",
389 		regs->ARM_r10, regs->ARM_r9,
390 		regs->ARM_r8);
391 	printk("r7 : %08lx  r6 : %08lx  r5 : %08lx  r4 : %08lx\n",
392 		regs->ARM_r7, regs->ARM_r6,
393 		regs->ARM_r5, regs->ARM_r4);
394 	printk("r3 : %08lx  r2 : %08lx  r1 : %08lx  r0 : %08lx\n",
395 		regs->ARM_r3, regs->ARM_r2,
396 		regs->ARM_r1, regs->ARM_r0);
397 
398 	flags = regs->ARM_cpsr;
399 	buf[0] = flags & PSR_N_BIT ? 'N' : 'n';
400 	buf[1] = flags & PSR_Z_BIT ? 'Z' : 'z';
401 	buf[2] = flags & PSR_C_BIT ? 'C' : 'c';
402 	buf[3] = flags & PSR_V_BIT ? 'V' : 'v';
403 	buf[4] = '\0';
404 
405 #ifndef CONFIG_CPU_V7M
406 	{
407 		unsigned int domain = get_domain();
408 		const char *segment;
409 
410 #ifdef CONFIG_CPU_SW_DOMAIN_PAN
411 		/*
412 		 * Get the domain register for the parent context. In user
413 		 * mode, we don't save the DACR, so lets use what it should
414 		 * be. For other modes, we place it after the pt_regs struct.
415 		 */
416 		if (user_mode(regs))
417 			domain = DACR_UACCESS_ENABLE;
418 		else
419 			domain = *(unsigned int *)(regs + 1);
420 #endif
421 
422 		if ((domain & domain_mask(DOMAIN_USER)) ==
423 		    domain_val(DOMAIN_USER, DOMAIN_NOACCESS))
424 			segment = "none";
425 		else if (get_fs() == get_ds())
426 			segment = "kernel";
427 		else
428 			segment = "user";
429 
430 		printk("Flags: %s  IRQs o%s  FIQs o%s  Mode %s  ISA %s  Segment %s\n",
431 			buf, interrupts_enabled(regs) ? "n" : "ff",
432 			fast_interrupts_enabled(regs) ? "n" : "ff",
433 			processor_modes[processor_mode(regs)],
434 			isa_modes[isa_mode(regs)], segment);
435 	}
436 #else
437 	printk("xPSR: %08lx\n", regs->ARM_cpsr);
438 #endif
439 
440 #ifdef CONFIG_CPU_CP15
441 	{
442 		unsigned int ctrl;
443 
444 		buf[0] = '\0';
445 #ifdef CONFIG_CPU_CP15_MMU
446 		{
447 			unsigned int transbase, dac = get_domain();
448 			asm("mrc p15, 0, %0, c2, c0\n\t"
449 			    : "=r" (transbase));
450 			snprintf(buf, sizeof(buf), "  Table: %08x  DAC: %08x",
451 			  	transbase, dac);
452 		}
453 #endif
454 		asm("mrc p15, 0, %0, c1, c0\n" : "=r" (ctrl));
455 
456 		printk("Control: %08x%s\n", ctrl, buf);
457 	}
458 #endif
459 
460 	show_extra_register_data(regs, 128);
461 }
462 
show_regs(struct pt_regs * regs)463 void show_regs(struct pt_regs * regs)
464 {
465 	__show_regs(regs);
466 	dump_stack();
467 }
468 
469 ATOMIC_NOTIFIER_HEAD(thread_notify_head);
470 
471 EXPORT_SYMBOL_GPL(thread_notify_head);
472 
473 /*
474  * Free current thread data structures etc..
475  */
exit_thread(struct task_struct * tsk)476 void exit_thread(struct task_struct *tsk)
477 {
478 	thread_notify(THREAD_NOTIFY_EXIT, task_thread_info(tsk));
479 }
480 
flush_thread(void)481 void flush_thread(void)
482 {
483 	struct thread_info *thread = current_thread_info();
484 	struct task_struct *tsk = current;
485 
486 	flush_ptrace_hw_breakpoint(tsk);
487 
488 	memset(thread->used_cp, 0, sizeof(thread->used_cp));
489 	memset(&tsk->thread.debug, 0, sizeof(struct debug_info));
490 	memset(&thread->fpstate, 0, sizeof(union fp_state));
491 
492 	flush_tls();
493 
494 	thread_notify(THREAD_NOTIFY_FLUSH, thread);
495 }
496 
release_thread(struct task_struct * dead_task)497 void release_thread(struct task_struct *dead_task)
498 {
499 }
500 
501 asmlinkage void ret_from_fork(void) __asm__("ret_from_fork");
502 
503 int
copy_thread(unsigned long clone_flags,unsigned long stack_start,unsigned long stk_sz,struct task_struct * p)504 copy_thread(unsigned long clone_flags, unsigned long stack_start,
505 	    unsigned long stk_sz, struct task_struct *p)
506 {
507 	struct thread_info *thread = task_thread_info(p);
508 	struct pt_regs *childregs = task_pt_regs(p);
509 
510 	memset(&thread->cpu_context, 0, sizeof(struct cpu_context_save));
511 
512 #ifdef CONFIG_CPU_USE_DOMAINS
513 	/*
514 	 * Copy the initial value of the domain access control register
515 	 * from the current thread: thread->addr_limit will have been
516 	 * copied from the current thread via setup_thread_stack() in
517 	 * kernel/fork.c
518 	 */
519 	thread->cpu_domain = get_domain();
520 #endif
521 
522 	if (likely(!(p->flags & PF_KTHREAD))) {
523 		*childregs = *current_pt_regs();
524 		childregs->ARM_r0 = 0;
525 		if (stack_start)
526 			childregs->ARM_sp = stack_start;
527 	} else {
528 		memset(childregs, 0, sizeof(struct pt_regs));
529 		thread->cpu_context.r4 = stk_sz;
530 		thread->cpu_context.r5 = stack_start;
531 		childregs->ARM_cpsr = SVC_MODE;
532 	}
533 	thread->cpu_context.pc = (unsigned long)ret_from_fork;
534 	thread->cpu_context.sp = (unsigned long)childregs;
535 
536 	clear_ptrace_hw_breakpoint(p);
537 
538 	if (clone_flags & CLONE_SETTLS)
539 		thread->tp_value[0] = childregs->ARM_r3;
540 	thread->tp_value[1] = get_tpuser();
541 
542 	thread_notify(THREAD_NOTIFY_COPY, thread);
543 
544 	return 0;
545 }
546 
547 /*
548  * Fill in the task's elfregs structure for a core dump.
549  */
dump_task_regs(struct task_struct * t,elf_gregset_t * elfregs)550 int dump_task_regs(struct task_struct *t, elf_gregset_t *elfregs)
551 {
552 	elf_core_copy_regs(elfregs, task_pt_regs(t));
553 	return 1;
554 }
555 
556 /*
557  * fill in the fpe structure for a core dump...
558  */
dump_fpu(struct pt_regs * regs,struct user_fp * fp)559 int dump_fpu (struct pt_regs *regs, struct user_fp *fp)
560 {
561 	struct thread_info *thread = current_thread_info();
562 	int used_math = thread->used_cp[1] | thread->used_cp[2];
563 
564 	if (used_math)
565 		memcpy(fp, &thread->fpstate.soft, sizeof (*fp));
566 
567 	return used_math != 0;
568 }
569 EXPORT_SYMBOL(dump_fpu);
570 
get_wchan(struct task_struct * p)571 unsigned long get_wchan(struct task_struct *p)
572 {
573 	struct stackframe frame;
574 	unsigned long stack_page;
575 	int count = 0;
576 	if (!p || p == current || p->state == TASK_RUNNING)
577 		return 0;
578 
579 	frame.fp = thread_saved_fp(p);
580 	frame.sp = thread_saved_sp(p);
581 	frame.lr = 0;			/* recovered from the stack */
582 	frame.pc = thread_saved_pc(p);
583 	stack_page = (unsigned long)task_stack_page(p);
584 	do {
585 		if (frame.sp < stack_page ||
586 		    frame.sp >= stack_page + THREAD_SIZE ||
587 		    unwind_frame(&frame) < 0)
588 			return 0;
589 		if (!in_sched_functions(frame.pc))
590 			return frame.pc;
591 	} while (count ++ < 16);
592 	return 0;
593 }
594 
arch_randomize_brk(struct mm_struct * mm)595 unsigned long arch_randomize_brk(struct mm_struct *mm)
596 {
597 	unsigned long range_end = mm->brk + 0x02000000;
598 	return randomize_range(mm->brk, range_end, 0) ? : mm->brk;
599 }
600 
601 #ifdef CONFIG_MMU
602 #ifdef CONFIG_KUSER_HELPERS
603 /*
604  * The vectors page is always readable from user space for the
605  * atomic helpers. Insert it into the gate_vma so that it is visible
606  * through ptrace and /proc/<pid>/mem.
607  */
608 static struct vm_area_struct gate_vma = {
609 	.vm_start	= 0xffff0000,
610 	.vm_end		= 0xffff0000 + PAGE_SIZE,
611 	.vm_flags	= VM_READ | VM_EXEC | VM_MAYREAD | VM_MAYEXEC,
612 };
613 
gate_vma_init(void)614 static int __init gate_vma_init(void)
615 {
616 	gate_vma.vm_page_prot = PAGE_READONLY_EXEC;
617 	return 0;
618 }
619 arch_initcall(gate_vma_init);
620 
get_gate_vma(struct mm_struct * mm)621 struct vm_area_struct *get_gate_vma(struct mm_struct *mm)
622 {
623 	return &gate_vma;
624 }
625 
in_gate_area(struct mm_struct * mm,unsigned long addr)626 int in_gate_area(struct mm_struct *mm, unsigned long addr)
627 {
628 	return (addr >= gate_vma.vm_start) && (addr < gate_vma.vm_end);
629 }
630 
in_gate_area_no_mm(unsigned long addr)631 int in_gate_area_no_mm(unsigned long addr)
632 {
633 	return in_gate_area(NULL, addr);
634 }
635 #define is_gate_vma(vma)	((vma) == &gate_vma)
636 #else
637 #define is_gate_vma(vma)	0
638 #endif
639 
arch_vma_name(struct vm_area_struct * vma)640 const char *arch_vma_name(struct vm_area_struct *vma)
641 {
642 	return is_gate_vma(vma) ? "[vectors]" : NULL;
643 }
644 
645 /* If possible, provide a placement hint at a random offset from the
646  * stack for the sigpage and vdso pages.
647  */
sigpage_addr(const struct mm_struct * mm,unsigned int npages)648 static unsigned long sigpage_addr(const struct mm_struct *mm,
649 				  unsigned int npages)
650 {
651 	unsigned long offset;
652 	unsigned long first;
653 	unsigned long last;
654 	unsigned long addr;
655 	unsigned int slots;
656 
657 	first = PAGE_ALIGN(mm->start_stack);
658 
659 	last = TASK_SIZE - (npages << PAGE_SHIFT);
660 
661 	/* No room after stack? */
662 	if (first > last)
663 		return 0;
664 
665 	/* Just enough room? */
666 	if (first == last)
667 		return first;
668 
669 	slots = ((last - first) >> PAGE_SHIFT) + 1;
670 
671 	offset = get_random_int() % slots;
672 
673 	addr = first + (offset << PAGE_SHIFT);
674 
675 	return addr;
676 }
677 
678 static struct page *signal_page;
679 extern struct page *get_signal_page(void);
680 
681 static const struct vm_special_mapping sigpage_mapping = {
682 	.name = "[sigpage]",
683 	.pages = &signal_page,
684 };
685 
arch_setup_additional_pages(struct linux_binprm * bprm,int uses_interp)686 int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp)
687 {
688 	struct mm_struct *mm = current->mm;
689 	struct vm_area_struct *vma;
690 	unsigned long npages;
691 	unsigned long addr;
692 	unsigned long hint;
693 	int ret = 0;
694 
695 	if (!signal_page)
696 		signal_page = get_signal_page();
697 	if (!signal_page)
698 		return -ENOMEM;
699 
700 	npages = 1; /* for sigpage */
701 	npages += vdso_total_pages;
702 
703 	down_write(&mm->mmap_sem);
704 	hint = sigpage_addr(mm, npages);
705 	addr = get_unmapped_area(NULL, hint, npages << PAGE_SHIFT, 0, 0);
706 	if (IS_ERR_VALUE(addr)) {
707 		ret = addr;
708 		goto up_fail;
709 	}
710 
711 	vma = _install_special_mapping(mm, addr, PAGE_SIZE,
712 		VM_READ | VM_EXEC | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC,
713 		&sigpage_mapping);
714 
715 	if (IS_ERR(vma)) {
716 		ret = PTR_ERR(vma);
717 		goto up_fail;
718 	}
719 
720 	mm->context.sigpage = addr;
721 
722 	/* Unlike the sigpage, failure to install the vdso is unlikely
723 	 * to be fatal to the process, so no error check needed
724 	 * here.
725 	 */
726 	arm_install_vdso(mm, addr + PAGE_SIZE);
727 
728  up_fail:
729 	up_write(&mm->mmap_sem);
730 	return ret;
731 }
732 #endif
733