xref: /openbmc/linux/arch/parisc/kernel/process.c (revision 76a4f7cc)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *    PARISC Architecture-dependent parts of process handling
4  *    based on the work for i386
5  *
6  *    Copyright (C) 1999-2003 Matthew Wilcox <willy at parisc-linux.org>
7  *    Copyright (C) 2000 Martin K Petersen <mkp at mkp.net>
8  *    Copyright (C) 2000 John Marvin <jsm at parisc-linux.org>
9  *    Copyright (C) 2000 David Huggins-Daines <dhd with pobox.org>
10  *    Copyright (C) 2000-2003 Paul Bame <bame at parisc-linux.org>
11  *    Copyright (C) 2000 Philipp Rumpf <prumpf with tux.org>
12  *    Copyright (C) 2000 David Kennedy <dkennedy with linuxcare.com>
13  *    Copyright (C) 2000 Richard Hirst <rhirst with parisc-linux.org>
14  *    Copyright (C) 2000 Grant Grundler <grundler with parisc-linux.org>
15  *    Copyright (C) 2001 Alan Modra <amodra at parisc-linux.org>
16  *    Copyright (C) 2001-2002 Ryan Bradetich <rbrad at parisc-linux.org>
17  *    Copyright (C) 2001-2014 Helge Deller <deller@gmx.de>
18  *    Copyright (C) 2002 Randolph Chung <tausq with parisc-linux.org>
19  */
20 #include <linux/elf.h>
21 #include <linux/errno.h>
22 #include <linux/kernel.h>
23 #include <linux/mm.h>
24 #include <linux/fs.h>
25 #include <linux/cpu.h>
26 #include <linux/module.h>
27 #include <linux/personality.h>
28 #include <linux/ptrace.h>
29 #include <linux/sched.h>
30 #include <linux/sched/debug.h>
31 #include <linux/sched/task.h>
32 #include <linux/sched/task_stack.h>
33 #include <linux/slab.h>
34 #include <linux/stddef.h>
35 #include <linux/unistd.h>
36 #include <linux/kallsyms.h>
37 #include <linux/uaccess.h>
38 #include <linux/rcupdate.h>
39 #include <linux/random.h>
40 #include <linux/nmi.h>
41 
42 #include <asm/io.h>
43 #include <asm/asm-offsets.h>
44 #include <asm/assembly.h>
45 #include <asm/pdc.h>
46 #include <asm/pdc_chassis.h>
47 #include <asm/unwind.h>
48 #include <asm/sections.h>
49 
50 #define COMMAND_GLOBAL  F_EXTEND(0xfffe0030)
51 #define CMD_RESET       5       /* reset any module */
52 
53 /*
54 ** The Wright Brothers and Gecko systems have a H/W problem
55 ** (Lasi...'nuf said) may cause a broadcast reset to lockup
56 ** the system. An HVERSION dependent PDC call was developed
57 ** to perform a "safe", platform specific broadcast reset instead
58 ** of kludging up all the code.
59 **
60 ** Older machines which do not implement PDC_BROADCAST_RESET will
61 ** return (with an error) and the regular broadcast reset can be
62 ** issued. Obviously, if the PDC does implement PDC_BROADCAST_RESET
63 ** the PDC call will not return (the system will be reset).
64 */
65 void machine_restart(char *cmd)
66 {
67 #ifdef FASTBOOT_SELFTEST_SUPPORT
68 	/*
69 	 ** If user has modified the Firmware Selftest Bitmap,
70 	 ** run the tests specified in the bitmap after the
71 	 ** system is rebooted w/PDC_DO_RESET.
72 	 **
73 	 ** ftc_bitmap = 0x1AUL "Skip destructive memory tests"
74 	 **
75 	 ** Using "directed resets" at each processor with the MEM_TOC
76 	 ** vector cleared will also avoid running destructive
77 	 ** memory self tests. (Not implemented yet)
78 	 */
79 	if (ftc_bitmap) {
80 		pdc_do_firm_test_reset(ftc_bitmap);
81 	}
82 #endif
83 	/* set up a new led state on systems shipped with a LED State panel */
84 	pdc_chassis_send_status(PDC_CHASSIS_DIRECT_SHUTDOWN);
85 
86 	/* "Normal" system reset */
87 	pdc_do_reset();
88 
89 	/* Nope...box should reset with just CMD_RESET now */
90 	gsc_writel(CMD_RESET, COMMAND_GLOBAL);
91 
92 	/* Wait for RESET to lay us to rest. */
93 	while (1) ;
94 
95 }
96 
97 void (*chassis_power_off)(void);
98 
99 /*
100  * This routine is called from sys_reboot to actually turn off the
101  * machine
102  */
103 void machine_power_off(void)
104 {
105 	/* If there is a registered power off handler, call it. */
106 	if (chassis_power_off)
107 		chassis_power_off();
108 
109 	/* Put the soft power button back under hardware control.
110 	 * If the user had already pressed the power button, the
111 	 * following call will immediately power off. */
112 	pdc_soft_power_button(0);
113 
114 	pdc_chassis_send_status(PDC_CHASSIS_DIRECT_SHUTDOWN);
115 
116 	/* ipmi_poweroff may have been installed. */
117 	if (pm_power_off)
118 		pm_power_off();
119 
120 	/* It seems we have no way to power the system off via
121 	 * software. The user has to press the button himself. */
122 
123 	printk(KERN_EMERG "System shut down completed.\n"
124 	       "Please power this system off now.");
125 
126 	/* prevent soft lockup/stalled CPU messages for endless loop. */
127 	rcu_sysrq_start();
128 	lockup_detector_soft_poweroff();
129 	for (;;);
130 }
131 
132 void (*pm_power_off)(void);
133 EXPORT_SYMBOL(pm_power_off);
134 
135 void machine_halt(void)
136 {
137 	machine_power_off();
138 }
139 
140 void flush_thread(void)
141 {
142 	/* Only needs to handle fpu stuff or perf monitors.
143 	** REVISIT: several arches implement a "lazy fpu state".
144 	*/
145 }
146 
147 void release_thread(struct task_struct *dead_task)
148 {
149 }
150 
151 /*
152  * Idle thread support
153  *
154  * Detect when running on QEMU with SeaBIOS PDC Firmware and let
155  * QEMU idle the host too.
156  */
157 
158 int running_on_qemu __ro_after_init;
159 EXPORT_SYMBOL(running_on_qemu);
160 
161 void __cpuidle arch_cpu_idle_dead(void)
162 {
163 	/* nop on real hardware, qemu will offline CPU. */
164 	asm volatile("or %%r31,%%r31,%%r31\n":::);
165 }
166 
167 void __cpuidle arch_cpu_idle(void)
168 {
169 	raw_local_irq_enable();
170 
171 	/* nop on real hardware, qemu will idle sleep. */
172 	asm volatile("or %%r10,%%r10,%%r10\n":::);
173 }
174 
175 static int __init parisc_idle_init(void)
176 {
177 	if (!running_on_qemu)
178 		cpu_idle_poll_ctrl(1);
179 
180 	return 0;
181 }
182 arch_initcall(parisc_idle_init);
183 
184 /*
185  * Copy architecture-specific thread state
186  */
187 int
188 copy_thread(unsigned long clone_flags, unsigned long usp,
189 	    unsigned long kthread_arg, struct task_struct *p, unsigned long tls)
190 {
191 	struct pt_regs *cregs = &(p->thread.regs);
192 	void *stack = task_stack_page(p);
193 
194 	/* We have to use void * instead of a function pointer, because
195 	 * function pointers aren't a pointer to the function on 64-bit.
196 	 * Make them const so the compiler knows they live in .text */
197 	extern void * const ret_from_kernel_thread;
198 	extern void * const child_return;
199 
200 	if (unlikely(p->flags & (PF_KTHREAD | PF_IO_WORKER))) {
201 		/* kernel thread */
202 		memset(cregs, 0, sizeof(struct pt_regs));
203 		if (!usp) /* idle thread */
204 			return 0;
205 		/* Must exit via ret_from_kernel_thread in order
206 		 * to call schedule_tail()
207 		 */
208 		cregs->ksp = (unsigned long)stack + THREAD_SZ_ALGN + FRAME_SIZE;
209 		cregs->kpc = (unsigned long) &ret_from_kernel_thread;
210 		/*
211 		 * Copy function and argument to be called from
212 		 * ret_from_kernel_thread.
213 		 */
214 #ifdef CONFIG_64BIT
215 		cregs->gr[27] = ((unsigned long *)usp)[3];
216 		cregs->gr[26] = ((unsigned long *)usp)[2];
217 #else
218 		cregs->gr[26] = usp;
219 #endif
220 		cregs->gr[25] = kthread_arg;
221 	} else {
222 		/* user thread */
223 		/* usp must be word aligned.  This also prevents users from
224 		 * passing in the value 1 (which is the signal for a special
225 		 * return for a kernel thread) */
226 		if (usp) {
227 			usp = ALIGN(usp, 4);
228 			if (likely(usp))
229 				cregs->gr[30] = usp;
230 		}
231 		cregs->ksp = (unsigned long)stack + THREAD_SZ_ALGN + FRAME_SIZE;
232 		cregs->kpc = (unsigned long) &child_return;
233 
234 		/* Setup thread TLS area */
235 		if (clone_flags & CLONE_SETTLS)
236 			cregs->cr27 = tls;
237 	}
238 
239 	return 0;
240 }
241 
242 unsigned long
243 get_wchan(struct task_struct *p)
244 {
245 	struct unwind_frame_info info;
246 	unsigned long ip;
247 	int count = 0;
248 
249 	if (!p || p == current || task_is_running(p))
250 		return 0;
251 
252 	/*
253 	 * These bracket the sleeping functions..
254 	 */
255 
256 	unwind_frame_init_from_blocked_task(&info, p);
257 	do {
258 		if (unwind_once(&info) < 0)
259 			return 0;
260 		if (task_is_running(p))
261                         return 0;
262 		ip = info.ip;
263 		if (!in_sched_functions(ip))
264 			return ip;
265 	} while (count++ < MAX_UNWIND_ENTRIES);
266 	return 0;
267 }
268 
269 #ifdef CONFIG_64BIT
270 void *dereference_function_descriptor(void *ptr)
271 {
272 	Elf64_Fdesc *desc = ptr;
273 	void *p;
274 
275 	if (!get_kernel_nofault(p, (void *)&desc->addr))
276 		ptr = p;
277 	return ptr;
278 }
279 
280 void *dereference_kernel_function_descriptor(void *ptr)
281 {
282 	if (ptr < (void *)__start_opd ||
283 			ptr >= (void *)__end_opd)
284 		return ptr;
285 
286 	return dereference_function_descriptor(ptr);
287 }
288 #endif
289 
290 static inline unsigned long brk_rnd(void)
291 {
292 	return (get_random_int() & BRK_RND_MASK) << PAGE_SHIFT;
293 }
294 
295 unsigned long arch_randomize_brk(struct mm_struct *mm)
296 {
297 	unsigned long ret = PAGE_ALIGN(mm->brk + brk_rnd());
298 
299 	if (ret < mm->brk)
300 		return mm->brk;
301 	return ret;
302 }
303