1/*
2 *    PARISC Architecture-dependent parts of process handling
3 *    based on the work for i386
4 *
5 *    Copyright (C) 1999-2003 Matthew Wilcox <willy at parisc-linux.org>
6 *    Copyright (C) 2000 Martin K Petersen <mkp at mkp.net>
7 *    Copyright (C) 2000 John Marvin <jsm at parisc-linux.org>
8 *    Copyright (C) 2000 David Huggins-Daines <dhd with pobox.org>
9 *    Copyright (C) 2000-2003 Paul Bame <bame at parisc-linux.org>
10 *    Copyright (C) 2000 Philipp Rumpf <prumpf with tux.org>
11 *    Copyright (C) 2000 David Kennedy <dkennedy with linuxcare.com>
12 *    Copyright (C) 2000 Richard Hirst <rhirst with parisc-linux.org>
13 *    Copyright (C) 2000 Grant Grundler <grundler with parisc-linux.org>
14 *    Copyright (C) 2001 Alan Modra <amodra at parisc-linux.org>
15 *    Copyright (C) 2001-2002 Ryan Bradetich <rbrad at parisc-linux.org>
16 *    Copyright (C) 2001-2007 Helge Deller <deller at parisc-linux.org>
17 *    Copyright (C) 2002 Randolph Chung <tausq with parisc-linux.org>
18 *
19 *
20 *    This program is free software; you can redistribute it and/or modify
21 *    it under the terms of the GNU General Public License as published by
22 *    the Free Software Foundation; either version 2 of the License, or
23 *    (at your option) any later version.
24 *
25 *    This program is distributed in the hope that it will be useful,
26 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
27 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28 *    GNU General Public License for more details.
29 *
30 *    You should have received a copy of the GNU General Public License
31 *    along with this program; if not, write to the Free Software
32 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
33 */
34
35#include <stdarg.h>
36
37#include <linux/elf.h>
38#include <linux/errno.h>
39#include <linux/kernel.h>
40#include <linux/mm.h>
41#include <linux/module.h>
42#include <linux/personality.h>
43#include <linux/ptrace.h>
44#include <linux/sched.h>
45#include <linux/stddef.h>
46#include <linux/unistd.h>
47#include <linux/kallsyms.h>
48
49#include <asm/io.h>
50#include <asm/asm-offsets.h>
51#include <asm/pdc.h>
52#include <asm/pdc_chassis.h>
53#include <asm/pgalloc.h>
54#include <asm/uaccess.h>
55#include <asm/unwind.h>
56
57/*
58 * The idle thread. There's no useful work to be
59 * done, so just try to conserve power and have a
60 * low exit latency (ie sit in a loop waiting for
61 * somebody to say that they'd like to reschedule)
62 */
63void cpu_idle(void)
64{
65	set_thread_flag(TIF_POLLING_NRFLAG);
66
67	/* endless idle loop with no priority at all */
68	while (1) {
69		while (!need_resched())
70			barrier();
71		preempt_enable_no_resched();
72		schedule();
73		preempt_disable();
74		check_pgt_cache();
75	}
76}
77
78
79#define COMMAND_GLOBAL  F_EXTEND(0xfffe0030)
80#define CMD_RESET       5       /* reset any module */
81
82/*
83** The Wright Brothers and Gecko systems have a H/W problem
84** (Lasi...'nuf said) may cause a broadcast reset to lockup
85** the system. An HVERSION dependent PDC call was developed
86** to perform a "safe", platform specific broadcast reset instead
87** of kludging up all the code.
88**
89** Older machines which do not implement PDC_BROADCAST_RESET will
90** return (with an error) and the regular broadcast reset can be
91** issued. Obviously, if the PDC does implement PDC_BROADCAST_RESET
92** the PDC call will not return (the system will be reset).
93*/
94void machine_restart(char *cmd)
95{
96#ifdef FASTBOOT_SELFTEST_SUPPORT
97	/*
98	 ** If user has modified the Firmware Selftest Bitmap,
99	 ** run the tests specified in the bitmap after the
100	 ** system is rebooted w/PDC_DO_RESET.
101	 **
102	 ** ftc_bitmap = 0x1AUL "Skip destructive memory tests"
103	 **
104	 ** Using "directed resets" at each processor with the MEM_TOC
105	 ** vector cleared will also avoid running destructive
106	 ** memory self tests. (Not implemented yet)
107	 */
108	if (ftc_bitmap) {
109		pdc_do_firm_test_reset(ftc_bitmap);
110	}
111#endif
112	/* set up a new led state on systems shipped with a LED State panel */
113	pdc_chassis_send_status(PDC_CHASSIS_DIRECT_SHUTDOWN);
114
115	/* "Normal" system reset */
116	pdc_do_reset();
117
118	/* Nope...box should reset with just CMD_RESET now */
119	gsc_writel(CMD_RESET, COMMAND_GLOBAL);
120
121	/* Wait for RESET to lay us to rest. */
122	while (1) ;
123
124}
125
126void machine_halt(void)
127{
128	/*
129	** The LED/ChassisCodes are updated by the led_halt()
130	** function, called by the reboot notifier chain.
131	*/
132}
133
134void (*chassis_power_off)(void);
135
136/*
137 * This routine is called from sys_reboot to actually turn off the
138 * machine
139 */
140void machine_power_off(void)
141{
142	/* If there is a registered power off handler, call it. */
143	if (chassis_power_off)
144		chassis_power_off();
145
146	/* Put the soft power button back under hardware control.
147	 * If the user had already pressed the power button, the
148	 * following call will immediately power off. */
149	pdc_soft_power_button(0);
150
151	pdc_chassis_send_status(PDC_CHASSIS_DIRECT_SHUTDOWN);
152
153	/* It seems we have no way to power the system off via
154	 * software. The user has to press the button himself. */
155
156	printk(KERN_EMERG "System shut down completed.\n"
157	       KERN_EMERG "Please power this system off now.");
158}
159
160void (*pm_power_off)(void) = machine_power_off;
161EXPORT_SYMBOL(pm_power_off);
162
163/*
164 * Create a kernel thread
165 */
166
167extern pid_t __kernel_thread(int (*fn)(void *), void *arg, unsigned long flags);
168pid_t kernel_thread(int (*fn)(void *), void *arg, unsigned long flags)
169{
170
171
172	return __kernel_thread(fn, arg, flags);
173}
174EXPORT_SYMBOL(kernel_thread);
175
176/*
177 * Free current thread data structures etc..
178 */
179void exit_thread(void)
180{
181}
182
183void flush_thread(void)
184{
185	/* Only needs to handle fpu stuff or perf monitors.
186	** REVISIT: several arches implement a "lazy fpu state".
187	*/
188	set_fs(USER_DS);
189}
190
191void release_thread(struct task_struct *dead_task)
192{
193}
194
195/*
196 * Fill in the FPU structure for a core dump.
197 */
198
199int dump_fpu (struct pt_regs * regs, elf_fpregset_t *r)
200{
201	if (regs == NULL)
202		return 0;
203
204	memcpy(r, regs->fr, sizeof *r);
205	return 1;
206}
207
208int dump_task_fpu (struct task_struct *tsk, elf_fpregset_t *r)
209{
210	memcpy(r, tsk->thread.regs.fr, sizeof(*r));
211	return 1;
212}
213
214/* Note that "fork()" is implemented in terms of clone, with
215   parameters (SIGCHLD, regs->gr[30], regs). */
216int
217sys_clone(unsigned long clone_flags, unsigned long usp,
218	  struct pt_regs *regs)
219{
220  	/* Arugments from userspace are:
221	   r26 = Clone flags.
222	   r25 = Child stack.
223	   r24 = parent_tidptr.
224	   r23 = Is the TLS storage descriptor
225	   r22 = child_tidptr
226
227	   However, these last 3 args are only examined
228	   if the proper flags are set. */
229	int __user *child_tidptr;
230	int __user *parent_tidptr;
231
232	/* usp must be word aligned.  This also prevents users from
233	 * passing in the value 1 (which is the signal for a special
234	 * return for a kernel thread) */
235	usp = ALIGN(usp, 4);
236
237	/* A zero value for usp means use the current stack */
238	if (usp == 0)
239	  usp = regs->gr[30];
240
241	if (clone_flags & CLONE_PARENT_SETTID)
242	  parent_tidptr = (int __user *)regs->gr[24];
243	else
244	  parent_tidptr = NULL;
245
246	if (clone_flags & (CLONE_CHILD_SETTID | CLONE_CHILD_CLEARTID))
247	  child_tidptr = (int __user *)regs->gr[22];
248	else
249	  child_tidptr = NULL;
250
251	return do_fork(clone_flags, usp, regs, 0, parent_tidptr, child_tidptr);
252}
253
254int
255sys_vfork(struct pt_regs *regs)
256{
257	return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs->gr[30], regs, 0, NULL, NULL);
258}
259
260int
261copy_thread(int nr, unsigned long clone_flags, unsigned long usp,
262	    unsigned long unused,	/* in ia64 this is "user_stack_size" */
263	    struct task_struct * p, struct pt_regs * pregs)
264{
265	struct pt_regs * cregs = &(p->thread.regs);
266	void *stack = task_stack_page(p);
267
268	/* We have to use void * instead of a function pointer, because
269	 * function pointers aren't a pointer to the function on 64-bit.
270	 * Make them const so the compiler knows they live in .text */
271	extern void * const ret_from_kernel_thread;
272	extern void * const child_return;
273#ifdef CONFIG_HPUX
274	extern void * const hpux_child_return;
275#endif
276
277	*cregs = *pregs;
278
279	/* Set the return value for the child.  Note that this is not
280           actually restored by the syscall exit path, but we put it
281           here for consistency in case of signals. */
282	cregs->gr[28] = 0; /* child */
283
284	/*
285	 * We need to differentiate between a user fork and a
286	 * kernel fork. We can't use user_mode, because the
287	 * the syscall path doesn't save iaoq. Right now
288	 * We rely on the fact that kernel_thread passes
289	 * in zero for usp.
290	 */
291	if (usp == 1) {
292		/* kernel thread */
293		cregs->ksp = (unsigned long)stack + THREAD_SZ_ALGN;
294		/* Must exit via ret_from_kernel_thread in order
295		 * to call schedule_tail()
296		 */
297		cregs->kpc = (unsigned long) &ret_from_kernel_thread;
298		/*
299		 * Copy function and argument to be called from
300		 * ret_from_kernel_thread.
301		 */
302#ifdef CONFIG_64BIT
303		cregs->gr[27] = pregs->gr[27];
304#endif
305		cregs->gr[26] = pregs->gr[26];
306		cregs->gr[25] = pregs->gr[25];
307	} else {
308		/* user thread */
309		/*
310		 * Note that the fork wrappers are responsible
311		 * for setting gr[21].
312		 */
313
314		/* Use same stack depth as parent */
315		cregs->ksp = (unsigned long)stack
316			+ (pregs->gr[21] & (THREAD_SIZE - 1));
317		cregs->gr[30] = usp;
318		if (p->personality == PER_HPUX) {
319#ifdef CONFIG_HPUX
320			cregs->kpc = (unsigned long) &hpux_child_return;
321#else
322			BUG();
323#endif
324		} else {
325			cregs->kpc = (unsigned long) &child_return;
326		}
327		/* Setup thread TLS area from the 4th parameter in clone */
328		if (clone_flags & CLONE_SETTLS)
329		  cregs->cr27 = pregs->gr[23];
330
331	}
332
333	return 0;
334}
335
336unsigned long thread_saved_pc(struct task_struct *t)
337{
338	return t->thread.regs.kpc;
339}
340
341/*
342 * sys_execve() executes a new program.
343 */
344
345asmlinkage int sys_execve(struct pt_regs *regs)
346{
347	int error;
348	char *filename;
349
350	filename = getname((const char __user *) regs->gr[26]);
351	error = PTR_ERR(filename);
352	if (IS_ERR(filename))
353		goto out;
354	error = do_execve(filename, (char __user * __user *) regs->gr[25],
355		(char __user * __user *) regs->gr[24], regs);
356	if (error == 0) {
357		task_lock(current);
358		current->ptrace &= ~PT_DTRACE;
359		task_unlock(current);
360	}
361	putname(filename);
362out:
363
364	return error;
365}
366
367extern int __execve(const char *filename, char *const argv[],
368		char *const envp[], struct task_struct *task);
369int kernel_execve(const char *filename, char *const argv[], char *const envp[])
370{
371	return __execve(filename, argv, envp, current);
372}
373
374unsigned long
375get_wchan(struct task_struct *p)
376{
377	struct unwind_frame_info info;
378	unsigned long ip;
379	int count = 0;
380
381	if (!p || p == current || p->state == TASK_RUNNING)
382		return 0;
383
384	/*
385	 * These bracket the sleeping functions..
386	 */
387
388	unwind_frame_init_from_blocked_task(&info, p);
389	do {
390		if (unwind_once(&info) < 0)
391			return 0;
392		ip = info.ip;
393		if (!in_sched_functions(ip))
394			return ip;
395	} while (count++ < 16);
396	return 0;
397}
398