xref: /openbmc/linux/arch/parisc/kernel/process.c (revision 24002d59376e2d19a2a523fe88d5b32e691a43af)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *    PARISC Architecture-dependent parts of process handling
31da177e4SLinus Torvalds  *    based on the work for i386
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *    Copyright (C) 1999-2003 Matthew Wilcox <willy at parisc-linux.org>
61da177e4SLinus Torvalds  *    Copyright (C) 2000 Martin K Petersen <mkp at mkp.net>
71da177e4SLinus Torvalds  *    Copyright (C) 2000 John Marvin <jsm at parisc-linux.org>
81da177e4SLinus Torvalds  *    Copyright (C) 2000 David Huggins-Daines <dhd with pobox.org>
91da177e4SLinus Torvalds  *    Copyright (C) 2000-2003 Paul Bame <bame at parisc-linux.org>
101da177e4SLinus Torvalds  *    Copyright (C) 2000 Philipp Rumpf <prumpf with tux.org>
111da177e4SLinus Torvalds  *    Copyright (C) 2000 David Kennedy <dkennedy with linuxcare.com>
12b2450cc1SCarlos O'Donell  *    Copyright (C) 2000 Richard Hirst <rhirst with parisc-linux.org>
131da177e4SLinus Torvalds  *    Copyright (C) 2000 Grant Grundler <grundler with parisc-linux.org>
141da177e4SLinus Torvalds  *    Copyright (C) 2001 Alan Modra <amodra at parisc-linux.org>
151da177e4SLinus Torvalds  *    Copyright (C) 2001-2002 Ryan Bradetich <rbrad at parisc-linux.org>
169dabf60dSHelge Deller  *    Copyright (C) 2001-2014 Helge Deller <deller@gmx.de>
171da177e4SLinus Torvalds  *    Copyright (C) 2002 Randolph Chung <tausq with parisc-linux.org>
181da177e4SLinus Torvalds  *
191da177e4SLinus Torvalds  *
201da177e4SLinus Torvalds  *    This program is free software; you can redistribute it and/or modify
211da177e4SLinus Torvalds  *    it under the terms of the GNU General Public License as published by
221da177e4SLinus Torvalds  *    the Free Software Foundation; either version 2 of the License, or
231da177e4SLinus Torvalds  *    (at your option) any later version.
241da177e4SLinus Torvalds  *
251da177e4SLinus Torvalds  *    This program is distributed in the hope that it will be useful,
261da177e4SLinus Torvalds  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
271da177e4SLinus Torvalds  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
281da177e4SLinus Torvalds  *    GNU General Public License for more details.
291da177e4SLinus Torvalds  *
301da177e4SLinus Torvalds  *    You should have received a copy of the GNU General Public License
311da177e4SLinus Torvalds  *    along with this program; if not, write to the Free Software
321da177e4SLinus Torvalds  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
331da177e4SLinus Torvalds  */
341da177e4SLinus Torvalds 
351da177e4SLinus Torvalds #include <stdarg.h>
361da177e4SLinus Torvalds 
371da177e4SLinus Torvalds #include <linux/elf.h>
381da177e4SLinus Torvalds #include <linux/errno.h>
391da177e4SLinus Torvalds #include <linux/kernel.h>
401da177e4SLinus Torvalds #include <linux/mm.h>
414e950f6fSAlexey Dobriyan #include <linux/fs.h>
42310d8278SHelge Deller #include <linux/cpu.h>
431da177e4SLinus Torvalds #include <linux/module.h>
441da177e4SLinus Torvalds #include <linux/personality.h>
451da177e4SLinus Torvalds #include <linux/ptrace.h>
461da177e4SLinus Torvalds #include <linux/sched.h>
47b17b0153SIngo Molnar #include <linux/sched/debug.h>
4829930025SIngo Molnar #include <linux/sched/task.h>
4968db0cf1SIngo Molnar #include <linux/sched/task_stack.h>
505a0e3ad6STejun Heo #include <linux/slab.h>
511da177e4SLinus Torvalds #include <linux/stddef.h>
521da177e4SLinus Torvalds #include <linux/unistd.h>
531da177e4SLinus Torvalds #include <linux/kallsyms.h>
54b609308eSKyle McMartin #include <linux/uaccess.h>
55fbe75218SFrederic Weisbecker #include <linux/rcupdate.h>
569dabf60dSHelge Deller #include <linux/random.h>
5756188832SHelge Deller #include <linux/nmi.h>
581da177e4SLinus Torvalds 
591da177e4SLinus Torvalds #include <asm/io.h>
600013a854SSam Ravnborg #include <asm/asm-offsets.h>
61ff0ab8afSAl Viro #include <asm/assembly.h>
621da177e4SLinus Torvalds #include <asm/pdc.h>
631da177e4SLinus Torvalds #include <asm/pdc_chassis.h>
641da177e4SLinus Torvalds #include <asm/pgalloc.h>
651da177e4SLinus Torvalds #include <asm/unwind.h>
66b609308eSKyle McMartin #include <asm/sections.h>
671da177e4SLinus Torvalds 
6810992092SKyle McMartin #define COMMAND_GLOBAL  F_EXTEND(0xfffe0030)
691da177e4SLinus Torvalds #define CMD_RESET       5       /* reset any module */
701da177e4SLinus Torvalds 
711da177e4SLinus Torvalds /*
721da177e4SLinus Torvalds ** The Wright Brothers and Gecko systems have a H/W problem
731da177e4SLinus Torvalds ** (Lasi...'nuf said) may cause a broadcast reset to lockup
741da177e4SLinus Torvalds ** the system. An HVERSION dependent PDC call was developed
751da177e4SLinus Torvalds ** to perform a "safe", platform specific broadcast reset instead
761da177e4SLinus Torvalds ** of kludging up all the code.
771da177e4SLinus Torvalds **
781da177e4SLinus Torvalds ** Older machines which do not implement PDC_BROADCAST_RESET will
791da177e4SLinus Torvalds ** return (with an error) and the regular broadcast reset can be
801da177e4SLinus Torvalds ** issued. Obviously, if the PDC does implement PDC_BROADCAST_RESET
811da177e4SLinus Torvalds ** the PDC call will not return (the system will be reset).
821da177e4SLinus Torvalds */
831da177e4SLinus Torvalds void machine_restart(char *cmd)
841da177e4SLinus Torvalds {
851da177e4SLinus Torvalds #ifdef FASTBOOT_SELFTEST_SUPPORT
861da177e4SLinus Torvalds 	/*
871da177e4SLinus Torvalds 	 ** If user has modified the Firmware Selftest Bitmap,
881da177e4SLinus Torvalds 	 ** run the tests specified in the bitmap after the
891da177e4SLinus Torvalds 	 ** system is rebooted w/PDC_DO_RESET.
901da177e4SLinus Torvalds 	 **
911da177e4SLinus Torvalds 	 ** ftc_bitmap = 0x1AUL "Skip destructive memory tests"
921da177e4SLinus Torvalds 	 **
931da177e4SLinus Torvalds 	 ** Using "directed resets" at each processor with the MEM_TOC
941da177e4SLinus Torvalds 	 ** vector cleared will also avoid running destructive
951da177e4SLinus Torvalds 	 ** memory self tests. (Not implemented yet)
961da177e4SLinus Torvalds 	 */
971da177e4SLinus Torvalds 	if (ftc_bitmap) {
981da177e4SLinus Torvalds 		pdc_do_firm_test_reset(ftc_bitmap);
991da177e4SLinus Torvalds 	}
1001da177e4SLinus Torvalds #endif
1011da177e4SLinus Torvalds 	/* set up a new led state on systems shipped with a LED State panel */
1021da177e4SLinus Torvalds 	pdc_chassis_send_status(PDC_CHASSIS_DIRECT_SHUTDOWN);
1031da177e4SLinus Torvalds 
1041da177e4SLinus Torvalds 	/* "Normal" system reset */
1051da177e4SLinus Torvalds 	pdc_do_reset();
1061da177e4SLinus Torvalds 
1071da177e4SLinus Torvalds 	/* Nope...box should reset with just CMD_RESET now */
1081da177e4SLinus Torvalds 	gsc_writel(CMD_RESET, COMMAND_GLOBAL);
1091da177e4SLinus Torvalds 
1101da177e4SLinus Torvalds 	/* Wait for RESET to lay us to rest. */
1111da177e4SLinus Torvalds 	while (1) ;
1121da177e4SLinus Torvalds 
1131da177e4SLinus Torvalds }
1141da177e4SLinus Torvalds 
1151da177e4SLinus Torvalds void machine_halt(void)
1161da177e4SLinus Torvalds {
1171da177e4SLinus Torvalds 	/*
1181da177e4SLinus Torvalds 	** The LED/ChassisCodes are updated by the led_halt()
1191da177e4SLinus Torvalds 	** function, called by the reboot notifier chain.
1201da177e4SLinus Torvalds 	*/
1211da177e4SLinus Torvalds }
1221da177e4SLinus Torvalds 
12385509c00SKyle McMartin void (*chassis_power_off)(void);
1241da177e4SLinus Torvalds 
1251da177e4SLinus Torvalds /*
1261da177e4SLinus Torvalds  * This routine is called from sys_reboot to actually turn off the
1271da177e4SLinus Torvalds  * machine
1281da177e4SLinus Torvalds  */
1291da177e4SLinus Torvalds void machine_power_off(void)
1301da177e4SLinus Torvalds {
1311da177e4SLinus Torvalds 	/* If there is a registered power off handler, call it. */
13285509c00SKyle McMartin 	if (chassis_power_off)
13385509c00SKyle McMartin 		chassis_power_off();
1341da177e4SLinus Torvalds 
1351da177e4SLinus Torvalds 	/* Put the soft power button back under hardware control.
1361da177e4SLinus Torvalds 	 * If the user had already pressed the power button, the
1371da177e4SLinus Torvalds 	 * following call will immediately power off. */
1381da177e4SLinus Torvalds 	pdc_soft_power_button(0);
1391da177e4SLinus Torvalds 
1401da177e4SLinus Torvalds 	pdc_chassis_send_status(PDC_CHASSIS_DIRECT_SHUTDOWN);
1411da177e4SLinus Torvalds 
142*24002d59SHelge Deller 	/* ipmi_poweroff may have been installed. */
143*24002d59SHelge Deller 	if (pm_power_off)
144*24002d59SHelge Deller 		pm_power_off();
145*24002d59SHelge Deller 
1461da177e4SLinus Torvalds 	/* It seems we have no way to power the system off via
1471da177e4SLinus Torvalds 	 * software. The user has to press the button himself. */
1481da177e4SLinus Torvalds 
1491da177e4SLinus Torvalds 	printk(KERN_EMERG "System shut down completed.\n"
150ad361c98SJoe Perches 	       "Please power this system off now.");
15173580dacSHelge Deller 
152476e75a4SHelge Deller 	/* prevent soft lockup/stalled CPU messages for endless loop. */
153476e75a4SHelge Deller 	rcu_sysrq_start();
15447bb4bafSThomas Gleixner 	lockup_detector_soft_poweroff();
15573580dacSHelge Deller 	for (;;);
1561da177e4SLinus Torvalds }
1571da177e4SLinus Torvalds 
158*24002d59SHelge Deller void (*pm_power_off)(void);
15985509c00SKyle McMartin EXPORT_SYMBOL(pm_power_off);
1601da177e4SLinus Torvalds 
1611da177e4SLinus Torvalds void flush_thread(void)
1621da177e4SLinus Torvalds {
1631da177e4SLinus Torvalds 	/* Only needs to handle fpu stuff or perf monitors.
1641da177e4SLinus Torvalds 	** REVISIT: several arches implement a "lazy fpu state".
1651da177e4SLinus Torvalds 	*/
1661da177e4SLinus Torvalds }
1671da177e4SLinus Torvalds 
1681da177e4SLinus Torvalds void release_thread(struct task_struct *dead_task)
1691da177e4SLinus Torvalds {
1701da177e4SLinus Torvalds }
1711da177e4SLinus Torvalds 
1721da177e4SLinus Torvalds /*
1731da177e4SLinus Torvalds  * Fill in the FPU structure for a core dump.
1741da177e4SLinus Torvalds  */
1751da177e4SLinus Torvalds 
1761da177e4SLinus Torvalds int dump_fpu (struct pt_regs * regs, elf_fpregset_t *r)
1771da177e4SLinus Torvalds {
1781da177e4SLinus Torvalds 	if (regs == NULL)
1791da177e4SLinus Torvalds 		return 0;
1801da177e4SLinus Torvalds 
1811da177e4SLinus Torvalds 	memcpy(r, regs->fr, sizeof *r);
1821da177e4SLinus Torvalds 	return 1;
1831da177e4SLinus Torvalds }
1841da177e4SLinus Torvalds 
1851da177e4SLinus Torvalds int dump_task_fpu (struct task_struct *tsk, elf_fpregset_t *r)
1861da177e4SLinus Torvalds {
1871da177e4SLinus Torvalds 	memcpy(r, tsk->thread.regs.fr, sizeof(*r));
1881da177e4SLinus Torvalds 	return 1;
1891da177e4SLinus Torvalds }
1901da177e4SLinus Torvalds 
1915fec97d0SAlex Dowad /*
192310d8278SHelge Deller  * Idle thread support
193310d8278SHelge Deller  *
194310d8278SHelge Deller  * Detect when running on QEMU with SeaBIOS PDC Firmware and let
195310d8278SHelge Deller  * QEMU idle the host too.
196310d8278SHelge Deller  */
197310d8278SHelge Deller 
198310d8278SHelge Deller int running_on_qemu __read_mostly;
199310d8278SHelge Deller 
200310d8278SHelge Deller void __cpuidle arch_cpu_idle_dead(void)
201310d8278SHelge Deller {
202310d8278SHelge Deller 	/* nop on real hardware, qemu will offline CPU. */
203310d8278SHelge Deller 	asm volatile("or %%r31,%%r31,%%r31\n":::);
204310d8278SHelge Deller }
205310d8278SHelge Deller 
206310d8278SHelge Deller void __cpuidle arch_cpu_idle(void)
207310d8278SHelge Deller {
208310d8278SHelge Deller 	local_irq_enable();
209310d8278SHelge Deller 
210310d8278SHelge Deller 	/* nop on real hardware, qemu will idle sleep. */
211310d8278SHelge Deller 	asm volatile("or %%r10,%%r10,%%r10\n":::);
212310d8278SHelge Deller }
213310d8278SHelge Deller 
214310d8278SHelge Deller static int __init parisc_idle_init(void)
215310d8278SHelge Deller {
216310d8278SHelge Deller 	const char *marker;
217310d8278SHelge Deller 
218310d8278SHelge Deller 	/* check QEMU/SeaBIOS marker in PAGE0 */
219310d8278SHelge Deller 	marker = (char *) &PAGE0->pad0;
220310d8278SHelge Deller 	running_on_qemu = (memcmp(marker, "SeaBIOS", 8) == 0);
221310d8278SHelge Deller 
222310d8278SHelge Deller 	if (!running_on_qemu)
223310d8278SHelge Deller 		cpu_idle_poll_ctrl(1);
224310d8278SHelge Deller 
225310d8278SHelge Deller 	return 0;
226310d8278SHelge Deller }
227310d8278SHelge Deller arch_initcall(parisc_idle_init);
228310d8278SHelge Deller 
229310d8278SHelge Deller /*
2305fec97d0SAlex Dowad  * Copy architecture-specific thread state
2315fec97d0SAlex Dowad  */
2321da177e4SLinus Torvalds int
2336f2c55b8SAlexey Dobriyan copy_thread(unsigned long clone_flags, unsigned long usp,
2345fec97d0SAlex Dowad 	    unsigned long kthread_arg, struct task_struct *p)
2351da177e4SLinus Torvalds {
2361da177e4SLinus Torvalds 	struct pt_regs *cregs = &(p->thread.regs);
23740f1f0deSAl Viro 	void *stack = task_stack_page(p);
2381da177e4SLinus Torvalds 
2391da177e4SLinus Torvalds 	/* We have to use void * instead of a function pointer, because
2401da177e4SLinus Torvalds 	 * function pointers aren't a pointer to the function on 64-bit.
2411da177e4SLinus Torvalds 	 * Make them const so the compiler knows they live in .text */
2421da177e4SLinus Torvalds 	extern void * const ret_from_kernel_thread;
2431da177e4SLinus Torvalds 	extern void * const child_return;
244c78c2b7eSHelge Deller 
245ff0ab8afSAl Viro 	if (unlikely(p->flags & PF_KTHREAD)) {
2465fec97d0SAlex Dowad 		/* kernel thread */
247a44e060fSAl Viro 		memset(cregs, 0, sizeof(struct pt_regs));
248ff0ab8afSAl Viro 		if (!usp) /* idle thread */
249ff0ab8afSAl Viro 			return 0;
2501da177e4SLinus Torvalds 		/* Must exit via ret_from_kernel_thread in order
2511da177e4SLinus Torvalds 		 * to call schedule_tail()
2521da177e4SLinus Torvalds 		 */
253ff0ab8afSAl Viro 		cregs->ksp = (unsigned long)stack + THREAD_SZ_ALGN + FRAME_SIZE;
2541da177e4SLinus Torvalds 		cregs->kpc = (unsigned long) &ret_from_kernel_thread;
2551da177e4SLinus Torvalds 		/*
2561da177e4SLinus Torvalds 		 * Copy function and argument to be called from
2571da177e4SLinus Torvalds 		 * ret_from_kernel_thread.
2581da177e4SLinus Torvalds 		 */
259a8f44e38SHelge Deller #ifdef CONFIG_64BIT
260a44e060fSAl Viro 		cregs->gr[27] = ((unsigned long *)usp)[3];
261a44e060fSAl Viro 		cregs->gr[26] = ((unsigned long *)usp)[2];
262a44e060fSAl Viro #else
263a44e060fSAl Viro 		cregs->gr[26] = usp;
2641da177e4SLinus Torvalds #endif
2655fec97d0SAlex Dowad 		cregs->gr[25] = kthread_arg;
2661da177e4SLinus Torvalds 	} else {
2671da177e4SLinus Torvalds 		/* user thread */
268415bfae9SAl Viro 		/* usp must be word aligned.  This also prevents users from
269415bfae9SAl Viro 		 * passing in the value 1 (which is the signal for a special
270415bfae9SAl Viro 		 * return for a kernel thread) */
271415bfae9SAl Viro 		if (usp) {
272415bfae9SAl Viro 			usp = ALIGN(usp, 4);
273415bfae9SAl Viro 			if (likely(usp))
2741da177e4SLinus Torvalds 				cregs->gr[30] = usp;
275415bfae9SAl Viro 		}
276ff0ab8afSAl Viro 		cregs->ksp = (unsigned long)stack + THREAD_SZ_ALGN + FRAME_SIZE;
2771da177e4SLinus Torvalds 		cregs->kpc = (unsigned long) &child_return;
278c78c2b7eSHelge Deller 
279b2450cc1SCarlos O'Donell 		/* Setup thread TLS area from the 4th parameter in clone */
280b2450cc1SCarlos O'Donell 		if (clone_flags & CLONE_SETTLS)
281415bfae9SAl Viro 			cregs->cr27 = cregs->gr[23];
2821da177e4SLinus Torvalds 	}
2831da177e4SLinus Torvalds 
2841da177e4SLinus Torvalds 	return 0;
2851da177e4SLinus Torvalds }
2861da177e4SLinus Torvalds 
2871da177e4SLinus Torvalds unsigned long
2881da177e4SLinus Torvalds get_wchan(struct task_struct *p)
2891da177e4SLinus Torvalds {
2901da177e4SLinus Torvalds 	struct unwind_frame_info info;
2911da177e4SLinus Torvalds 	unsigned long ip;
2921da177e4SLinus Torvalds 	int count = 0;
293cc650a7aSKyle McMartin 
294cc650a7aSKyle McMartin 	if (!p || p == current || p->state == TASK_RUNNING)
295cc650a7aSKyle McMartin 		return 0;
296cc650a7aSKyle McMartin 
2971da177e4SLinus Torvalds 	/*
2981da177e4SLinus Torvalds 	 * These bracket the sleeping functions..
2991da177e4SLinus Torvalds 	 */
3001da177e4SLinus Torvalds 
3011da177e4SLinus Torvalds 	unwind_frame_init_from_blocked_task(&info, p);
3021da177e4SLinus Torvalds 	do {
3031da177e4SLinus Torvalds 		if (unwind_once(&info) < 0)
3041da177e4SLinus Torvalds 			return 0;
3051da177e4SLinus Torvalds 		ip = info.ip;
3061da177e4SLinus Torvalds 		if (!in_sched_functions(ip))
3071da177e4SLinus Torvalds 			return ip;
3081da177e4SLinus Torvalds 	} while (count++ < 16);
3091da177e4SLinus Torvalds 	return 0;
3101da177e4SLinus Torvalds }
311b609308eSKyle McMartin 
312b609308eSKyle McMartin #ifdef CONFIG_64BIT
313b609308eSKyle McMartin void *dereference_function_descriptor(void *ptr)
314b609308eSKyle McMartin {
315b609308eSKyle McMartin 	Elf64_Fdesc *desc = ptr;
316b609308eSKyle McMartin 	void *p;
317b609308eSKyle McMartin 
318b609308eSKyle McMartin 	if (!probe_kernel_address(&desc->addr, p))
319b609308eSKyle McMartin 		ptr = p;
320b609308eSKyle McMartin 	return ptr;
321b609308eSKyle McMartin }
3221705bd6aSSergey Senozhatsky 
3231705bd6aSSergey Senozhatsky void *dereference_kernel_function_descriptor(void *ptr)
3241705bd6aSSergey Senozhatsky {
3251705bd6aSSergey Senozhatsky 	if (ptr < (void *)__start_opd ||
3261705bd6aSSergey Senozhatsky 			ptr >= (void *)__end_opd)
3271705bd6aSSergey Senozhatsky 		return ptr;
3281705bd6aSSergey Senozhatsky 
3291705bd6aSSergey Senozhatsky 	return dereference_function_descriptor(ptr);
3301705bd6aSSergey Senozhatsky }
331b609308eSKyle McMartin #endif
3329dabf60dSHelge Deller 
3339dabf60dSHelge Deller static inline unsigned long brk_rnd(void)
3349dabf60dSHelge Deller {
33518d98a79SHelge Deller 	return (get_random_int() & BRK_RND_MASK) << PAGE_SHIFT;
3369dabf60dSHelge Deller }
3379dabf60dSHelge Deller 
3389dabf60dSHelge Deller unsigned long arch_randomize_brk(struct mm_struct *mm)
3399dabf60dSHelge Deller {
3409dabf60dSHelge Deller 	unsigned long ret = PAGE_ALIGN(mm->brk + brk_rnd());
3419dabf60dSHelge Deller 
3429dabf60dSHelge Deller 	if (ret < mm->brk)
3439dabf60dSHelge Deller 		return mm->brk;
3449dabf60dSHelge Deller 	return ret;
3459dabf60dSHelge Deller }
346