xref: /openbmc/linux/arch/parisc/kernel/process.c (revision 2214c0e77259b420402e279e9ab4277ef320d371)
11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *    PARISC Architecture-dependent parts of process handling
41da177e4SLinus Torvalds  *    based on the work for i386
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  *    Copyright (C) 1999-2003 Matthew Wilcox <willy at parisc-linux.org>
71da177e4SLinus Torvalds  *    Copyright (C) 2000 Martin K Petersen <mkp at mkp.net>
81da177e4SLinus Torvalds  *    Copyright (C) 2000 John Marvin <jsm at parisc-linux.org>
91da177e4SLinus Torvalds  *    Copyright (C) 2000 David Huggins-Daines <dhd with pobox.org>
101da177e4SLinus Torvalds  *    Copyright (C) 2000-2003 Paul Bame <bame at parisc-linux.org>
111da177e4SLinus Torvalds  *    Copyright (C) 2000 Philipp Rumpf <prumpf with tux.org>
121da177e4SLinus Torvalds  *    Copyright (C) 2000 David Kennedy <dkennedy with linuxcare.com>
13b2450cc1SCarlos O'Donell  *    Copyright (C) 2000 Richard Hirst <rhirst with parisc-linux.org>
141da177e4SLinus Torvalds  *    Copyright (C) 2000 Grant Grundler <grundler with parisc-linux.org>
151da177e4SLinus Torvalds  *    Copyright (C) 2001 Alan Modra <amodra at parisc-linux.org>
161da177e4SLinus Torvalds  *    Copyright (C) 2001-2002 Ryan Bradetich <rbrad at parisc-linux.org>
179dabf60dSHelge Deller  *    Copyright (C) 2001-2014 Helge Deller <deller@gmx.de>
181da177e4SLinus Torvalds  *    Copyright (C) 2002 Randolph Chung <tausq with parisc-linux.org>
191da177e4SLinus Torvalds  */
201da177e4SLinus Torvalds #include <linux/elf.h>
211da177e4SLinus Torvalds #include <linux/errno.h>
221da177e4SLinus Torvalds #include <linux/kernel.h>
231da177e4SLinus Torvalds #include <linux/mm.h>
244e950f6fSAlexey Dobriyan #include <linux/fs.h>
25310d8278SHelge Deller #include <linux/cpu.h>
261da177e4SLinus Torvalds #include <linux/module.h>
271da177e4SLinus Torvalds #include <linux/personality.h>
281da177e4SLinus Torvalds #include <linux/ptrace.h>
291da177e4SLinus Torvalds #include <linux/sched.h>
30b17b0153SIngo Molnar #include <linux/sched/debug.h>
3129930025SIngo Molnar #include <linux/sched/task.h>
3268db0cf1SIngo Molnar #include <linux/sched/task_stack.h>
335a0e3ad6STejun Heo #include <linux/slab.h>
341da177e4SLinus Torvalds #include <linux/stddef.h>
351da177e4SLinus Torvalds #include <linux/unistd.h>
361da177e4SLinus Torvalds #include <linux/kallsyms.h>
37b609308eSKyle McMartin #include <linux/uaccess.h>
38fbe75218SFrederic Weisbecker #include <linux/rcupdate.h>
399dabf60dSHelge Deller #include <linux/random.h>
4056188832SHelge Deller #include <linux/nmi.h>
411da177e4SLinus Torvalds 
421da177e4SLinus Torvalds #include <asm/io.h>
430013a854SSam Ravnborg #include <asm/asm-offsets.h>
44ff0ab8afSAl Viro #include <asm/assembly.h>
451da177e4SLinus Torvalds #include <asm/pdc.h>
461da177e4SLinus Torvalds #include <asm/pdc_chassis.h>
471da177e4SLinus Torvalds #include <asm/unwind.h>
48b609308eSKyle McMartin #include <asm/sections.h>
491da177e4SLinus Torvalds 
5010992092SKyle McMartin #define COMMAND_GLOBAL  F_EXTEND(0xfffe0030)
511da177e4SLinus Torvalds #define CMD_RESET       5       /* reset any module */
521da177e4SLinus Torvalds 
531da177e4SLinus Torvalds /*
541da177e4SLinus Torvalds ** The Wright Brothers and Gecko systems have a H/W problem
551da177e4SLinus Torvalds ** (Lasi...'nuf said) may cause a broadcast reset to lockup
561da177e4SLinus Torvalds ** the system. An HVERSION dependent PDC call was developed
571da177e4SLinus Torvalds ** to perform a "safe", platform specific broadcast reset instead
581da177e4SLinus Torvalds ** of kludging up all the code.
591da177e4SLinus Torvalds **
601da177e4SLinus Torvalds ** Older machines which do not implement PDC_BROADCAST_RESET will
611da177e4SLinus Torvalds ** return (with an error) and the regular broadcast reset can be
621da177e4SLinus Torvalds ** issued. Obviously, if the PDC does implement PDC_BROADCAST_RESET
631da177e4SLinus Torvalds ** the PDC call will not return (the system will be reset).
641da177e4SLinus Torvalds */
651da177e4SLinus Torvalds void machine_restart(char *cmd)
661da177e4SLinus Torvalds {
671da177e4SLinus Torvalds #ifdef FASTBOOT_SELFTEST_SUPPORT
681da177e4SLinus Torvalds 	/*
691da177e4SLinus Torvalds 	 ** If user has modified the Firmware Selftest Bitmap,
701da177e4SLinus Torvalds 	 ** run the tests specified in the bitmap after the
711da177e4SLinus Torvalds 	 ** system is rebooted w/PDC_DO_RESET.
721da177e4SLinus Torvalds 	 **
731da177e4SLinus Torvalds 	 ** ftc_bitmap = 0x1AUL "Skip destructive memory tests"
741da177e4SLinus Torvalds 	 **
751da177e4SLinus Torvalds 	 ** Using "directed resets" at each processor with the MEM_TOC
761da177e4SLinus Torvalds 	 ** vector cleared will also avoid running destructive
771da177e4SLinus Torvalds 	 ** memory self tests. (Not implemented yet)
781da177e4SLinus Torvalds 	 */
791da177e4SLinus Torvalds 	if (ftc_bitmap) {
801da177e4SLinus Torvalds 		pdc_do_firm_test_reset(ftc_bitmap);
811da177e4SLinus Torvalds 	}
821da177e4SLinus Torvalds #endif
831da177e4SLinus Torvalds 	/* set up a new led state on systems shipped with a LED State panel */
841da177e4SLinus Torvalds 	pdc_chassis_send_status(PDC_CHASSIS_DIRECT_SHUTDOWN);
851da177e4SLinus Torvalds 
861da177e4SLinus Torvalds 	/* "Normal" system reset */
871da177e4SLinus Torvalds 	pdc_do_reset();
881da177e4SLinus Torvalds 
891da177e4SLinus Torvalds 	/* Nope...box should reset with just CMD_RESET now */
901da177e4SLinus Torvalds 	gsc_writel(CMD_RESET, COMMAND_GLOBAL);
911da177e4SLinus Torvalds 
921da177e4SLinus Torvalds 	/* Wait for RESET to lay us to rest. */
931da177e4SLinus Torvalds 	while (1) ;
941da177e4SLinus Torvalds 
951da177e4SLinus Torvalds }
961da177e4SLinus Torvalds 
9785509c00SKyle McMartin void (*chassis_power_off)(void);
981da177e4SLinus Torvalds 
991da177e4SLinus Torvalds /*
1001da177e4SLinus Torvalds  * This routine is called from sys_reboot to actually turn off the
1011da177e4SLinus Torvalds  * machine
1021da177e4SLinus Torvalds  */
1031da177e4SLinus Torvalds void machine_power_off(void)
1041da177e4SLinus Torvalds {
1051da177e4SLinus Torvalds 	/* If there is a registered power off handler, call it. */
10685509c00SKyle McMartin 	if (chassis_power_off)
10785509c00SKyle McMartin 		chassis_power_off();
1081da177e4SLinus Torvalds 
1091da177e4SLinus Torvalds 	/* Put the soft power button back under hardware control.
1101da177e4SLinus Torvalds 	 * If the user had already pressed the power button, the
1111da177e4SLinus Torvalds 	 * following call will immediately power off. */
1121da177e4SLinus Torvalds 	pdc_soft_power_button(0);
1131da177e4SLinus Torvalds 
1141da177e4SLinus Torvalds 	pdc_chassis_send_status(PDC_CHASSIS_DIRECT_SHUTDOWN);
1151da177e4SLinus Torvalds 
11624002d59SHelge Deller 	/* ipmi_poweroff may have been installed. */
11724002d59SHelge Deller 	if (pm_power_off)
11824002d59SHelge Deller 		pm_power_off();
11924002d59SHelge Deller 
1201da177e4SLinus Torvalds 	/* It seems we have no way to power the system off via
1211da177e4SLinus Torvalds 	 * software. The user has to press the button himself. */
1221da177e4SLinus Torvalds 
1231da177e4SLinus Torvalds 	printk(KERN_EMERG "System shut down completed.\n"
124ad361c98SJoe Perches 	       "Please power this system off now.");
12573580dacSHelge Deller 
126476e75a4SHelge Deller 	/* prevent soft lockup/stalled CPU messages for endless loop. */
127476e75a4SHelge Deller 	rcu_sysrq_start();
12847bb4bafSThomas Gleixner 	lockup_detector_soft_poweroff();
12973580dacSHelge Deller 	for (;;);
1301da177e4SLinus Torvalds }
1311da177e4SLinus Torvalds 
13224002d59SHelge Deller void (*pm_power_off)(void);
13385509c00SKyle McMartin EXPORT_SYMBOL(pm_power_off);
1341da177e4SLinus Torvalds 
13567698287SHelge Deller void machine_halt(void)
13667698287SHelge Deller {
13767698287SHelge Deller 	machine_power_off();
13867698287SHelge Deller }
13967698287SHelge Deller 
1401da177e4SLinus Torvalds void flush_thread(void)
1411da177e4SLinus Torvalds {
1421da177e4SLinus Torvalds 	/* Only needs to handle fpu stuff or perf monitors.
1431da177e4SLinus Torvalds 	** REVISIT: several arches implement a "lazy fpu state".
1441da177e4SLinus Torvalds 	*/
1451da177e4SLinus Torvalds }
1461da177e4SLinus Torvalds 
1471da177e4SLinus Torvalds void release_thread(struct task_struct *dead_task)
1481da177e4SLinus Torvalds {
1491da177e4SLinus Torvalds }
1501da177e4SLinus Torvalds 
1511da177e4SLinus Torvalds /*
152310d8278SHelge Deller  * Idle thread support
153310d8278SHelge Deller  *
154310d8278SHelge Deller  * Detect when running on QEMU with SeaBIOS PDC Firmware and let
155310d8278SHelge Deller  * QEMU idle the host too.
156310d8278SHelge Deller  */
157310d8278SHelge Deller 
1587e4c65bfSHelge Deller int running_on_qemu __ro_after_init;
1593e1120f4SHelge Deller EXPORT_SYMBOL(running_on_qemu);
160310d8278SHelge Deller 
161310d8278SHelge Deller void __cpuidle arch_cpu_idle_dead(void)
162310d8278SHelge Deller {
163310d8278SHelge Deller 	/* nop on real hardware, qemu will offline CPU. */
164310d8278SHelge Deller 	asm volatile("or %%r31,%%r31,%%r31\n":::);
165310d8278SHelge Deller }
166310d8278SHelge Deller 
167310d8278SHelge Deller void __cpuidle arch_cpu_idle(void)
168310d8278SHelge Deller {
16958c644baSPeter Zijlstra 	raw_local_irq_enable();
170310d8278SHelge Deller 
171310d8278SHelge Deller 	/* nop on real hardware, qemu will idle sleep. */
172310d8278SHelge Deller 	asm volatile("or %%r10,%%r10,%%r10\n":::);
173310d8278SHelge Deller }
174310d8278SHelge Deller 
175310d8278SHelge Deller static int __init parisc_idle_init(void)
176310d8278SHelge Deller {
177310d8278SHelge Deller 	if (!running_on_qemu)
178310d8278SHelge Deller 		cpu_idle_poll_ctrl(1);
179310d8278SHelge Deller 
180310d8278SHelge Deller 	return 0;
181310d8278SHelge Deller }
182310d8278SHelge Deller arch_initcall(parisc_idle_init);
183310d8278SHelge Deller 
184310d8278SHelge Deller /*
1855fec97d0SAlex Dowad  * Copy architecture-specific thread state
1865fec97d0SAlex Dowad  */
1871da177e4SLinus Torvalds int
188714acdbdSChristian Brauner copy_thread(unsigned long clone_flags, unsigned long usp,
189d2f36c78SAmanieu d'Antras 	    unsigned long kthread_arg, struct task_struct *p, unsigned long tls)
1901da177e4SLinus Torvalds {
1911da177e4SLinus Torvalds 	struct pt_regs *cregs = &(p->thread.regs);
19240f1f0deSAl Viro 	void *stack = task_stack_page(p);
1931da177e4SLinus Torvalds 
1941da177e4SLinus Torvalds 	/* We have to use void * instead of a function pointer, because
1951da177e4SLinus Torvalds 	 * function pointers aren't a pointer to the function on 64-bit.
1961da177e4SLinus Torvalds 	 * Make them const so the compiler knows they live in .text */
1971da177e4SLinus Torvalds 	extern void * const ret_from_kernel_thread;
1981da177e4SLinus Torvalds 	extern void * const child_return;
199c78c2b7eSHelge Deller 
2000100e6bbSJens Axboe 	if (unlikely(p->flags & (PF_KTHREAD | PF_IO_WORKER))) {
2015fec97d0SAlex Dowad 		/* kernel thread */
202a44e060fSAl Viro 		memset(cregs, 0, sizeof(struct pt_regs));
203ff0ab8afSAl Viro 		if (!usp) /* idle thread */
204ff0ab8afSAl Viro 			return 0;
2051da177e4SLinus Torvalds 		/* Must exit via ret_from_kernel_thread in order
2061da177e4SLinus Torvalds 		 * to call schedule_tail()
2071da177e4SLinus Torvalds 		 */
208*2214c0e7SHelge Deller 		cregs->ksp = (unsigned long) stack + FRAME_SIZE + PT_SZ_ALGN;
2091da177e4SLinus Torvalds 		cregs->kpc = (unsigned long) &ret_from_kernel_thread;
2101da177e4SLinus Torvalds 		/*
2111da177e4SLinus Torvalds 		 * Copy function and argument to be called from
2121da177e4SLinus Torvalds 		 * ret_from_kernel_thread.
2131da177e4SLinus Torvalds 		 */
214a8f44e38SHelge Deller #ifdef CONFIG_64BIT
215a44e060fSAl Viro 		cregs->gr[27] = ((unsigned long *)usp)[3];
216a44e060fSAl Viro 		cregs->gr[26] = ((unsigned long *)usp)[2];
217a44e060fSAl Viro #else
218a44e060fSAl Viro 		cregs->gr[26] = usp;
2191da177e4SLinus Torvalds #endif
2205fec97d0SAlex Dowad 		cregs->gr[25] = kthread_arg;
2211da177e4SLinus Torvalds 	} else {
2221da177e4SLinus Torvalds 		/* user thread */
223415bfae9SAl Viro 		/* usp must be word aligned.  This also prevents users from
224415bfae9SAl Viro 		 * passing in the value 1 (which is the signal for a special
225415bfae9SAl Viro 		 * return for a kernel thread) */
226415bfae9SAl Viro 		if (usp) {
227415bfae9SAl Viro 			usp = ALIGN(usp, 4);
228415bfae9SAl Viro 			if (likely(usp))
2291da177e4SLinus Torvalds 				cregs->gr[30] = usp;
230415bfae9SAl Viro 		}
231*2214c0e7SHelge Deller 		cregs->ksp = (unsigned long) stack + FRAME_SIZE;
2321da177e4SLinus Torvalds 		cregs->kpc = (unsigned long) &child_return;
233c78c2b7eSHelge Deller 
234d2f36c78SAmanieu d'Antras 		/* Setup thread TLS area */
235b2450cc1SCarlos O'Donell 		if (clone_flags & CLONE_SETTLS)
236d2f36c78SAmanieu d'Antras 			cregs->cr27 = tls;
2371da177e4SLinus Torvalds 	}
2381da177e4SLinus Torvalds 
2391da177e4SLinus Torvalds 	return 0;
2401da177e4SLinus Torvalds }
2411da177e4SLinus Torvalds 
2421da177e4SLinus Torvalds unsigned long
2431da177e4SLinus Torvalds get_wchan(struct task_struct *p)
2441da177e4SLinus Torvalds {
2451da177e4SLinus Torvalds 	struct unwind_frame_info info;
2461da177e4SLinus Torvalds 	unsigned long ip;
2471da177e4SLinus Torvalds 	int count = 0;
248cc650a7aSKyle McMartin 
249b03fbd4fSPeter Zijlstra 	if (!p || p == current || task_is_running(p))
250cc650a7aSKyle McMartin 		return 0;
251cc650a7aSKyle McMartin 
2521da177e4SLinus Torvalds 	/*
2531da177e4SLinus Torvalds 	 * These bracket the sleeping functions..
2541da177e4SLinus Torvalds 	 */
2551da177e4SLinus Torvalds 
2561da177e4SLinus Torvalds 	unwind_frame_init_from_blocked_task(&info, p);
2571da177e4SLinus Torvalds 	do {
2581da177e4SLinus Torvalds 		if (unwind_once(&info) < 0)
2591da177e4SLinus Torvalds 			return 0;
260b03fbd4fSPeter Zijlstra 		if (task_is_running(p))
261f2863032SHelge Deller                         return 0;
2621da177e4SLinus Torvalds 		ip = info.ip;
2631da177e4SLinus Torvalds 		if (!in_sched_functions(ip))
2641da177e4SLinus Torvalds 			return ip;
265c8921d72SHelge Deller 	} while (count++ < MAX_UNWIND_ENTRIES);
2661da177e4SLinus Torvalds 	return 0;
2671da177e4SLinus Torvalds }
268b609308eSKyle McMartin 
269b609308eSKyle McMartin #ifdef CONFIG_64BIT
270b609308eSKyle McMartin void *dereference_function_descriptor(void *ptr)
271b609308eSKyle McMartin {
272b609308eSKyle McMartin 	Elf64_Fdesc *desc = ptr;
273b609308eSKyle McMartin 	void *p;
274b609308eSKyle McMartin 
2750c389d89SLinus Torvalds 	if (!get_kernel_nofault(p, (void *)&desc->addr))
276b609308eSKyle McMartin 		ptr = p;
277b609308eSKyle McMartin 	return ptr;
278b609308eSKyle McMartin }
2791705bd6aSSergey Senozhatsky 
2801705bd6aSSergey Senozhatsky void *dereference_kernel_function_descriptor(void *ptr)
2811705bd6aSSergey Senozhatsky {
2821705bd6aSSergey Senozhatsky 	if (ptr < (void *)__start_opd ||
2831705bd6aSSergey Senozhatsky 			ptr >= (void *)__end_opd)
2841705bd6aSSergey Senozhatsky 		return ptr;
2851705bd6aSSergey Senozhatsky 
2861705bd6aSSergey Senozhatsky 	return dereference_function_descriptor(ptr);
2871705bd6aSSergey Senozhatsky }
288b609308eSKyle McMartin #endif
2899dabf60dSHelge Deller 
2909dabf60dSHelge Deller static inline unsigned long brk_rnd(void)
2919dabf60dSHelge Deller {
29218d98a79SHelge Deller 	return (get_random_int() & BRK_RND_MASK) << PAGE_SHIFT;
2939dabf60dSHelge Deller }
2949dabf60dSHelge Deller 
2959dabf60dSHelge Deller unsigned long arch_randomize_brk(struct mm_struct *mm)
2969dabf60dSHelge Deller {
2979dabf60dSHelge Deller 	unsigned long ret = PAGE_ALIGN(mm->brk + brk_rnd());
2989dabf60dSHelge Deller 
2999dabf60dSHelge Deller 	if (ret < mm->brk)
3009dabf60dSHelge Deller 		return mm->brk;
3019dabf60dSHelge Deller 	return ret;
3029dabf60dSHelge Deller }
303