xref: /openbmc/linux/arch/parisc/kernel/process.c (revision 714acdbd1c94e7e3ab90f6b6938f1ccb27b662f0)
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 
211da177e4SLinus Torvalds #include <stdarg.h>
221da177e4SLinus Torvalds 
231da177e4SLinus Torvalds #include <linux/elf.h>
241da177e4SLinus Torvalds #include <linux/errno.h>
251da177e4SLinus Torvalds #include <linux/kernel.h>
261da177e4SLinus Torvalds #include <linux/mm.h>
274e950f6fSAlexey Dobriyan #include <linux/fs.h>
28310d8278SHelge Deller #include <linux/cpu.h>
291da177e4SLinus Torvalds #include <linux/module.h>
301da177e4SLinus Torvalds #include <linux/personality.h>
311da177e4SLinus Torvalds #include <linux/ptrace.h>
321da177e4SLinus Torvalds #include <linux/sched.h>
33b17b0153SIngo Molnar #include <linux/sched/debug.h>
3429930025SIngo Molnar #include <linux/sched/task.h>
3568db0cf1SIngo Molnar #include <linux/sched/task_stack.h>
365a0e3ad6STejun Heo #include <linux/slab.h>
371da177e4SLinus Torvalds #include <linux/stddef.h>
381da177e4SLinus Torvalds #include <linux/unistd.h>
391da177e4SLinus Torvalds #include <linux/kallsyms.h>
40b609308eSKyle McMartin #include <linux/uaccess.h>
41fbe75218SFrederic Weisbecker #include <linux/rcupdate.h>
429dabf60dSHelge Deller #include <linux/random.h>
4356188832SHelge Deller #include <linux/nmi.h>
441da177e4SLinus Torvalds 
451da177e4SLinus Torvalds #include <asm/io.h>
460013a854SSam Ravnborg #include <asm/asm-offsets.h>
47ff0ab8afSAl Viro #include <asm/assembly.h>
481da177e4SLinus Torvalds #include <asm/pdc.h>
491da177e4SLinus Torvalds #include <asm/pdc_chassis.h>
501da177e4SLinus Torvalds #include <asm/pgalloc.h>
511da177e4SLinus Torvalds #include <asm/unwind.h>
52b609308eSKyle McMartin #include <asm/sections.h>
531da177e4SLinus Torvalds 
5410992092SKyle McMartin #define COMMAND_GLOBAL  F_EXTEND(0xfffe0030)
551da177e4SLinus Torvalds #define CMD_RESET       5       /* reset any module */
561da177e4SLinus Torvalds 
571da177e4SLinus Torvalds /*
581da177e4SLinus Torvalds ** The Wright Brothers and Gecko systems have a H/W problem
591da177e4SLinus Torvalds ** (Lasi...'nuf said) may cause a broadcast reset to lockup
601da177e4SLinus Torvalds ** the system. An HVERSION dependent PDC call was developed
611da177e4SLinus Torvalds ** to perform a "safe", platform specific broadcast reset instead
621da177e4SLinus Torvalds ** of kludging up all the code.
631da177e4SLinus Torvalds **
641da177e4SLinus Torvalds ** Older machines which do not implement PDC_BROADCAST_RESET will
651da177e4SLinus Torvalds ** return (with an error) and the regular broadcast reset can be
661da177e4SLinus Torvalds ** issued. Obviously, if the PDC does implement PDC_BROADCAST_RESET
671da177e4SLinus Torvalds ** the PDC call will not return (the system will be reset).
681da177e4SLinus Torvalds */
691da177e4SLinus Torvalds void machine_restart(char *cmd)
701da177e4SLinus Torvalds {
711da177e4SLinus Torvalds #ifdef FASTBOOT_SELFTEST_SUPPORT
721da177e4SLinus Torvalds 	/*
731da177e4SLinus Torvalds 	 ** If user has modified the Firmware Selftest Bitmap,
741da177e4SLinus Torvalds 	 ** run the tests specified in the bitmap after the
751da177e4SLinus Torvalds 	 ** system is rebooted w/PDC_DO_RESET.
761da177e4SLinus Torvalds 	 **
771da177e4SLinus Torvalds 	 ** ftc_bitmap = 0x1AUL "Skip destructive memory tests"
781da177e4SLinus Torvalds 	 **
791da177e4SLinus Torvalds 	 ** Using "directed resets" at each processor with the MEM_TOC
801da177e4SLinus Torvalds 	 ** vector cleared will also avoid running destructive
811da177e4SLinus Torvalds 	 ** memory self tests. (Not implemented yet)
821da177e4SLinus Torvalds 	 */
831da177e4SLinus Torvalds 	if (ftc_bitmap) {
841da177e4SLinus Torvalds 		pdc_do_firm_test_reset(ftc_bitmap);
851da177e4SLinus Torvalds 	}
861da177e4SLinus Torvalds #endif
871da177e4SLinus Torvalds 	/* set up a new led state on systems shipped with a LED State panel */
881da177e4SLinus Torvalds 	pdc_chassis_send_status(PDC_CHASSIS_DIRECT_SHUTDOWN);
891da177e4SLinus Torvalds 
901da177e4SLinus Torvalds 	/* "Normal" system reset */
911da177e4SLinus Torvalds 	pdc_do_reset();
921da177e4SLinus Torvalds 
931da177e4SLinus Torvalds 	/* Nope...box should reset with just CMD_RESET now */
941da177e4SLinus Torvalds 	gsc_writel(CMD_RESET, COMMAND_GLOBAL);
951da177e4SLinus Torvalds 
961da177e4SLinus Torvalds 	/* Wait for RESET to lay us to rest. */
971da177e4SLinus Torvalds 	while (1) ;
981da177e4SLinus Torvalds 
991da177e4SLinus Torvalds }
1001da177e4SLinus Torvalds 
10185509c00SKyle McMartin void (*chassis_power_off)(void);
1021da177e4SLinus Torvalds 
1031da177e4SLinus Torvalds /*
1041da177e4SLinus Torvalds  * This routine is called from sys_reboot to actually turn off the
1051da177e4SLinus Torvalds  * machine
1061da177e4SLinus Torvalds  */
1071da177e4SLinus Torvalds void machine_power_off(void)
1081da177e4SLinus Torvalds {
1091da177e4SLinus Torvalds 	/* If there is a registered power off handler, call it. */
11085509c00SKyle McMartin 	if (chassis_power_off)
11185509c00SKyle McMartin 		chassis_power_off();
1121da177e4SLinus Torvalds 
1131da177e4SLinus Torvalds 	/* Put the soft power button back under hardware control.
1141da177e4SLinus Torvalds 	 * If the user had already pressed the power button, the
1151da177e4SLinus Torvalds 	 * following call will immediately power off. */
1161da177e4SLinus Torvalds 	pdc_soft_power_button(0);
1171da177e4SLinus Torvalds 
1181da177e4SLinus Torvalds 	pdc_chassis_send_status(PDC_CHASSIS_DIRECT_SHUTDOWN);
1191da177e4SLinus Torvalds 
12024002d59SHelge Deller 	/* ipmi_poweroff may have been installed. */
12124002d59SHelge Deller 	if (pm_power_off)
12224002d59SHelge Deller 		pm_power_off();
12324002d59SHelge Deller 
1241da177e4SLinus Torvalds 	/* It seems we have no way to power the system off via
1251da177e4SLinus Torvalds 	 * software. The user has to press the button himself. */
1261da177e4SLinus Torvalds 
1271da177e4SLinus Torvalds 	printk(KERN_EMERG "System shut down completed.\n"
128ad361c98SJoe Perches 	       "Please power this system off now.");
12973580dacSHelge Deller 
130476e75a4SHelge Deller 	/* prevent soft lockup/stalled CPU messages for endless loop. */
131476e75a4SHelge Deller 	rcu_sysrq_start();
13247bb4bafSThomas Gleixner 	lockup_detector_soft_poweroff();
13373580dacSHelge Deller 	for (;;);
1341da177e4SLinus Torvalds }
1351da177e4SLinus Torvalds 
13624002d59SHelge Deller void (*pm_power_off)(void);
13785509c00SKyle McMartin EXPORT_SYMBOL(pm_power_off);
1381da177e4SLinus Torvalds 
13967698287SHelge Deller void machine_halt(void)
14067698287SHelge Deller {
14167698287SHelge Deller 	machine_power_off();
14267698287SHelge Deller }
14367698287SHelge Deller 
1441da177e4SLinus Torvalds void flush_thread(void)
1451da177e4SLinus Torvalds {
1461da177e4SLinus Torvalds 	/* Only needs to handle fpu stuff or perf monitors.
1471da177e4SLinus Torvalds 	** REVISIT: several arches implement a "lazy fpu state".
1481da177e4SLinus Torvalds 	*/
1491da177e4SLinus Torvalds }
1501da177e4SLinus Torvalds 
1511da177e4SLinus Torvalds void release_thread(struct task_struct *dead_task)
1521da177e4SLinus Torvalds {
1531da177e4SLinus Torvalds }
1541da177e4SLinus Torvalds 
1551da177e4SLinus Torvalds /*
1561da177e4SLinus Torvalds  * Fill in the FPU structure for a core dump.
1571da177e4SLinus Torvalds  */
1581da177e4SLinus Torvalds 
1591da177e4SLinus Torvalds int dump_fpu (struct pt_regs * regs, elf_fpregset_t *r)
1601da177e4SLinus Torvalds {
1611da177e4SLinus Torvalds 	if (regs == NULL)
1621da177e4SLinus Torvalds 		return 0;
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds 	memcpy(r, regs->fr, sizeof *r);
1651da177e4SLinus Torvalds 	return 1;
1661da177e4SLinus Torvalds }
1671da177e4SLinus Torvalds 
1681da177e4SLinus Torvalds int dump_task_fpu (struct task_struct *tsk, elf_fpregset_t *r)
1691da177e4SLinus Torvalds {
1701da177e4SLinus Torvalds 	memcpy(r, tsk->thread.regs.fr, sizeof(*r));
1711da177e4SLinus Torvalds 	return 1;
1721da177e4SLinus Torvalds }
1731da177e4SLinus Torvalds 
1745fec97d0SAlex Dowad /*
175310d8278SHelge Deller  * Idle thread support
176310d8278SHelge Deller  *
177310d8278SHelge Deller  * Detect when running on QEMU with SeaBIOS PDC Firmware and let
178310d8278SHelge Deller  * QEMU idle the host too.
179310d8278SHelge Deller  */
180310d8278SHelge Deller 
1817e4c65bfSHelge Deller int running_on_qemu __ro_after_init;
1823e1120f4SHelge Deller EXPORT_SYMBOL(running_on_qemu);
183310d8278SHelge Deller 
184310d8278SHelge Deller void __cpuidle arch_cpu_idle_dead(void)
185310d8278SHelge Deller {
186310d8278SHelge Deller 	/* nop on real hardware, qemu will offline CPU. */
187310d8278SHelge Deller 	asm volatile("or %%r31,%%r31,%%r31\n":::);
188310d8278SHelge Deller }
189310d8278SHelge Deller 
190310d8278SHelge Deller void __cpuidle arch_cpu_idle(void)
191310d8278SHelge Deller {
192310d8278SHelge Deller 	local_irq_enable();
193310d8278SHelge Deller 
194310d8278SHelge Deller 	/* nop on real hardware, qemu will idle sleep. */
195310d8278SHelge Deller 	asm volatile("or %%r10,%%r10,%%r10\n":::);
196310d8278SHelge Deller }
197310d8278SHelge Deller 
198310d8278SHelge Deller static int __init parisc_idle_init(void)
199310d8278SHelge Deller {
200310d8278SHelge Deller 	if (!running_on_qemu)
201310d8278SHelge Deller 		cpu_idle_poll_ctrl(1);
202310d8278SHelge Deller 
203310d8278SHelge Deller 	return 0;
204310d8278SHelge Deller }
205310d8278SHelge Deller arch_initcall(parisc_idle_init);
206310d8278SHelge Deller 
207310d8278SHelge Deller /*
2085fec97d0SAlex Dowad  * Copy architecture-specific thread state
2095fec97d0SAlex Dowad  */
2101da177e4SLinus Torvalds int
211*714acdbdSChristian Brauner copy_thread(unsigned long clone_flags, unsigned long usp,
212d2f36c78SAmanieu d'Antras 	    unsigned long kthread_arg, struct task_struct *p, unsigned long tls)
2131da177e4SLinus Torvalds {
2141da177e4SLinus Torvalds 	struct pt_regs *cregs = &(p->thread.regs);
21540f1f0deSAl Viro 	void *stack = task_stack_page(p);
2161da177e4SLinus Torvalds 
2171da177e4SLinus Torvalds 	/* We have to use void * instead of a function pointer, because
2181da177e4SLinus Torvalds 	 * function pointers aren't a pointer to the function on 64-bit.
2191da177e4SLinus Torvalds 	 * Make them const so the compiler knows they live in .text */
2201da177e4SLinus Torvalds 	extern void * const ret_from_kernel_thread;
2211da177e4SLinus Torvalds 	extern void * const child_return;
222c78c2b7eSHelge Deller 
223ff0ab8afSAl Viro 	if (unlikely(p->flags & PF_KTHREAD)) {
2245fec97d0SAlex Dowad 		/* kernel thread */
225a44e060fSAl Viro 		memset(cregs, 0, sizeof(struct pt_regs));
226ff0ab8afSAl Viro 		if (!usp) /* idle thread */
227ff0ab8afSAl Viro 			return 0;
2281da177e4SLinus Torvalds 		/* Must exit via ret_from_kernel_thread in order
2291da177e4SLinus Torvalds 		 * to call schedule_tail()
2301da177e4SLinus Torvalds 		 */
231ff0ab8afSAl Viro 		cregs->ksp = (unsigned long)stack + THREAD_SZ_ALGN + FRAME_SIZE;
2321da177e4SLinus Torvalds 		cregs->kpc = (unsigned long) &ret_from_kernel_thread;
2331da177e4SLinus Torvalds 		/*
2341da177e4SLinus Torvalds 		 * Copy function and argument to be called from
2351da177e4SLinus Torvalds 		 * ret_from_kernel_thread.
2361da177e4SLinus Torvalds 		 */
237a8f44e38SHelge Deller #ifdef CONFIG_64BIT
238a44e060fSAl Viro 		cregs->gr[27] = ((unsigned long *)usp)[3];
239a44e060fSAl Viro 		cregs->gr[26] = ((unsigned long *)usp)[2];
240a44e060fSAl Viro #else
241a44e060fSAl Viro 		cregs->gr[26] = usp;
2421da177e4SLinus Torvalds #endif
2435fec97d0SAlex Dowad 		cregs->gr[25] = kthread_arg;
2441da177e4SLinus Torvalds 	} else {
2451da177e4SLinus Torvalds 		/* user thread */
246415bfae9SAl Viro 		/* usp must be word aligned.  This also prevents users from
247415bfae9SAl Viro 		 * passing in the value 1 (which is the signal for a special
248415bfae9SAl Viro 		 * return for a kernel thread) */
249415bfae9SAl Viro 		if (usp) {
250415bfae9SAl Viro 			usp = ALIGN(usp, 4);
251415bfae9SAl Viro 			if (likely(usp))
2521da177e4SLinus Torvalds 				cregs->gr[30] = usp;
253415bfae9SAl Viro 		}
254ff0ab8afSAl Viro 		cregs->ksp = (unsigned long)stack + THREAD_SZ_ALGN + FRAME_SIZE;
2551da177e4SLinus Torvalds 		cregs->kpc = (unsigned long) &child_return;
256c78c2b7eSHelge Deller 
257d2f36c78SAmanieu d'Antras 		/* Setup thread TLS area */
258b2450cc1SCarlos O'Donell 		if (clone_flags & CLONE_SETTLS)
259d2f36c78SAmanieu d'Antras 			cregs->cr27 = tls;
2601da177e4SLinus Torvalds 	}
2611da177e4SLinus Torvalds 
2621da177e4SLinus Torvalds 	return 0;
2631da177e4SLinus Torvalds }
2641da177e4SLinus Torvalds 
2651da177e4SLinus Torvalds unsigned long
2661da177e4SLinus Torvalds get_wchan(struct task_struct *p)
2671da177e4SLinus Torvalds {
2681da177e4SLinus Torvalds 	struct unwind_frame_info info;
2691da177e4SLinus Torvalds 	unsigned long ip;
2701da177e4SLinus Torvalds 	int count = 0;
271cc650a7aSKyle McMartin 
272cc650a7aSKyle McMartin 	if (!p || p == current || p->state == TASK_RUNNING)
273cc650a7aSKyle McMartin 		return 0;
274cc650a7aSKyle McMartin 
2751da177e4SLinus Torvalds 	/*
2761da177e4SLinus Torvalds 	 * These bracket the sleeping functions..
2771da177e4SLinus Torvalds 	 */
2781da177e4SLinus Torvalds 
2791da177e4SLinus Torvalds 	unwind_frame_init_from_blocked_task(&info, p);
2801da177e4SLinus Torvalds 	do {
2811da177e4SLinus Torvalds 		if (unwind_once(&info) < 0)
2821da177e4SLinus Torvalds 			return 0;
2831da177e4SLinus Torvalds 		ip = info.ip;
2841da177e4SLinus Torvalds 		if (!in_sched_functions(ip))
2851da177e4SLinus Torvalds 			return ip;
286c8921d72SHelge Deller 	} while (count++ < MAX_UNWIND_ENTRIES);
2871da177e4SLinus Torvalds 	return 0;
2881da177e4SLinus Torvalds }
289b609308eSKyle McMartin 
290b609308eSKyle McMartin #ifdef CONFIG_64BIT
291b609308eSKyle McMartin void *dereference_function_descriptor(void *ptr)
292b609308eSKyle McMartin {
293b609308eSKyle McMartin 	Elf64_Fdesc *desc = ptr;
294b609308eSKyle McMartin 	void *p;
295b609308eSKyle McMartin 
2960c389d89SLinus Torvalds 	if (!get_kernel_nofault(p, (void *)&desc->addr))
297b609308eSKyle McMartin 		ptr = p;
298b609308eSKyle McMartin 	return ptr;
299b609308eSKyle McMartin }
3001705bd6aSSergey Senozhatsky 
3011705bd6aSSergey Senozhatsky void *dereference_kernel_function_descriptor(void *ptr)
3021705bd6aSSergey Senozhatsky {
3031705bd6aSSergey Senozhatsky 	if (ptr < (void *)__start_opd ||
3041705bd6aSSergey Senozhatsky 			ptr >= (void *)__end_opd)
3051705bd6aSSergey Senozhatsky 		return ptr;
3061705bd6aSSergey Senozhatsky 
3071705bd6aSSergey Senozhatsky 	return dereference_function_descriptor(ptr);
3081705bd6aSSergey Senozhatsky }
309b609308eSKyle McMartin #endif
3109dabf60dSHelge Deller 
3119dabf60dSHelge Deller static inline unsigned long brk_rnd(void)
3129dabf60dSHelge Deller {
31318d98a79SHelge Deller 	return (get_random_int() & BRK_RND_MASK) << PAGE_SHIFT;
3149dabf60dSHelge Deller }
3159dabf60dSHelge Deller 
3169dabf60dSHelge Deller unsigned long arch_randomize_brk(struct mm_struct *mm)
3179dabf60dSHelge Deller {
3189dabf60dSHelge Deller 	unsigned long ret = PAGE_ALIGN(mm->brk + brk_rnd());
3199dabf60dSHelge Deller 
3209dabf60dSHelge Deller 	if (ret < mm->brk)
3219dabf60dSHelge Deller 		return mm->brk;
3229dabf60dSHelge Deller 	return ret;
3239dabf60dSHelge Deller }
324