1/*
2 *  linux/arch/alpha/kernel/process.c
3 *
4 *  Copyright (C) 1995  Linus Torvalds
5 */
6
7/*
8 * This file handles the architecture-dependent parts of process handling.
9 */
10
11#include <linux/errno.h>
12#include <linux/module.h>
13#include <linux/sched.h>
14#include <linux/kernel.h>
15#include <linux/mm.h>
16#include <linux/smp.h>
17#include <linux/stddef.h>
18#include <linux/unistd.h>
19#include <linux/ptrace.h>
20#include <linux/slab.h>
21#include <linux/user.h>
22#include <linux/a.out.h>
23#include <linux/utsname.h>
24#include <linux/time.h>
25#include <linux/major.h>
26#include <linux/stat.h>
27#include <linux/vt.h>
28#include <linux/mman.h>
29#include <linux/elfcore.h>
30#include <linux/reboot.h>
31#include <linux/tty.h>
32#include <linux/console.h>
33
34#include <asm/reg.h>
35#include <asm/uaccess.h>
36#include <asm/system.h>
37#include <asm/io.h>
38#include <asm/pgtable.h>
39#include <asm/hwrpb.h>
40#include <asm/fpu.h>
41
42#include "proto.h"
43#include "pci_impl.h"
44
45/*
46 * Power off function, if any
47 */
48void (*pm_power_off)(void) = machine_power_off;
49EXPORT_SYMBOL(pm_power_off);
50
51void
52cpu_idle(void)
53{
54	set_thread_flag(TIF_POLLING_NRFLAG);
55
56	while (1) {
57
58		while (!need_resched())
59			cpu_relax();
60		schedule();
61	}
62}
63
64
65struct halt_info {
66	int mode;
67	char *restart_cmd;
68};
69
70static void
71common_shutdown_1(void *generic_ptr)
72{
73	struct halt_info *how = (struct halt_info *)generic_ptr;
74	struct percpu_struct *cpup;
75	unsigned long *pflags, flags;
76	int cpuid = smp_processor_id();
77
78	/* No point in taking interrupts anymore. */
79	local_irq_disable();
80
81	cpup = (struct percpu_struct *)
82			((unsigned long)hwrpb + hwrpb->processor_offset
83			 + hwrpb->processor_size * cpuid);
84	pflags = &cpup->flags;
85	flags = *pflags;
86
87	/* Clear reason to "default"; clear "bootstrap in progress". */
88	flags &= ~0x00ff0001UL;
89
90#ifdef CONFIG_SMP
91	/* Secondaries halt here. */
92	if (cpuid != boot_cpuid) {
93		flags |= 0x00040000UL; /* "remain halted" */
94		*pflags = flags;
95		cpu_clear(cpuid, cpu_present_map);
96		halt();
97	}
98#endif
99
100	if (how->mode == LINUX_REBOOT_CMD_RESTART) {
101		if (!how->restart_cmd) {
102			flags |= 0x00020000UL; /* "cold bootstrap" */
103		} else {
104			/* For SRM, we could probably set environment
105			   variables to get this to work.  We'd have to
106			   delay this until after srm_paging_stop unless
107			   we ever got srm_fixup working.
108
109			   At the moment, SRM will use the last boot device,
110			   but the file and flags will be the defaults, when
111			   doing a "warm" bootstrap.  */
112			flags |= 0x00030000UL; /* "warm bootstrap" */
113		}
114	} else {
115		flags |= 0x00040000UL; /* "remain halted" */
116	}
117	*pflags = flags;
118
119#ifdef CONFIG_SMP
120	/* Wait for the secondaries to halt. */
121	cpu_clear(boot_cpuid, cpu_present_map);
122	while (cpus_weight(cpu_present_map))
123		barrier();
124#endif
125
126	/* If booted from SRM, reset some of the original environment. */
127	if (alpha_using_srm) {
128#ifdef CONFIG_DUMMY_CONSOLE
129		/* If we've gotten here after SysRq-b, leave interrupt
130		   context before taking over the console. */
131		if (in_interrupt())
132			irq_exit();
133		/* This has the effect of resetting the VGA video origin.  */
134		take_over_console(&dummy_con, 0, MAX_NR_CONSOLES-1, 1);
135#endif
136		pci_restore_srm_config();
137		set_hae(srm_hae);
138	}
139
140	if (alpha_mv.kill_arch)
141		alpha_mv.kill_arch(how->mode);
142
143	if (! alpha_using_srm && how->mode != LINUX_REBOOT_CMD_RESTART) {
144		/* Unfortunately, since MILO doesn't currently understand
145		   the hwrpb bits above, we can't reliably halt the
146		   processor and keep it halted.  So just loop.  */
147		return;
148	}
149
150	if (alpha_using_srm)
151		srm_paging_stop();
152
153	halt();
154}
155
156static void
157common_shutdown(int mode, char *restart_cmd)
158{
159	struct halt_info args;
160	args.mode = mode;
161	args.restart_cmd = restart_cmd;
162	on_each_cpu(common_shutdown_1, &args, 1, 0);
163}
164
165void
166machine_restart(char *restart_cmd)
167{
168	common_shutdown(LINUX_REBOOT_CMD_RESTART, restart_cmd);
169}
170
171
172void
173machine_halt(void)
174{
175	common_shutdown(LINUX_REBOOT_CMD_HALT, NULL);
176}
177
178
179void
180machine_power_off(void)
181{
182	common_shutdown(LINUX_REBOOT_CMD_POWER_OFF, NULL);
183}
184
185
186/* Used by sysrq-p, among others.  I don't believe r9-r15 are ever
187   saved in the context it's used.  */
188
189void
190show_regs(struct pt_regs *regs)
191{
192	dik_show_regs(regs, NULL);
193}
194
195/*
196 * Re-start a thread when doing execve()
197 */
198void
199start_thread(struct pt_regs * regs, unsigned long pc, unsigned long sp)
200{
201	set_fs(USER_DS);
202	regs->pc = pc;
203	regs->ps = 8;
204	wrusp(sp);
205}
206EXPORT_SYMBOL(start_thread);
207
208/*
209 * Free current thread data structures etc..
210 */
211void
212exit_thread(void)
213{
214}
215
216void
217flush_thread(void)
218{
219	/* Arrange for each exec'ed process to start off with a clean slate
220	   with respect to the FPU.  This is all exceptions disabled.  */
221	current_thread_info()->ieee_state = 0;
222	wrfpcr(FPCR_DYN_NORMAL | ieee_swcr_to_fpcr(0));
223
224	/* Clean slate for TLS.  */
225	current_thread_info()->pcb.unique = 0;
226}
227
228void
229release_thread(struct task_struct *dead_task)
230{
231}
232
233/*
234 * "alpha_clone()".. By the time we get here, the
235 * non-volatile registers have also been saved on the
236 * stack. We do some ugly pointer stuff here.. (see
237 * also copy_thread)
238 *
239 * Notice that "fork()" is implemented in terms of clone,
240 * with parameters (SIGCHLD, 0).
241 */
242int
243alpha_clone(unsigned long clone_flags, unsigned long usp,
244	    int __user *parent_tid, int __user *child_tid,
245	    unsigned long tls_value, struct pt_regs *regs)
246{
247	if (!usp)
248		usp = rdusp();
249
250	return do_fork(clone_flags, usp, regs, 0, parent_tid, child_tid);
251}
252
253int
254alpha_vfork(struct pt_regs *regs)
255{
256	return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, rdusp(),
257		       regs, 0, NULL, NULL);
258}
259
260/*
261 * Copy an alpha thread..
262 *
263 * Note the "stack_offset" stuff: when returning to kernel mode, we need
264 * to have some extra stack-space for the kernel stack that still exists
265 * after the "ret_from_fork".  When returning to user mode, we only want
266 * the space needed by the syscall stack frame (ie "struct pt_regs").
267 * Use the passed "regs" pointer to determine how much space we need
268 * for a kernel fork().
269 */
270
271int
272copy_thread(int nr, unsigned long clone_flags, unsigned long usp,
273	    unsigned long unused,
274	    struct task_struct * p, struct pt_regs * regs)
275{
276	extern void ret_from_fork(void);
277
278	struct thread_info *childti = task_thread_info(p);
279	struct pt_regs * childregs;
280	struct switch_stack * childstack, *stack;
281	unsigned long stack_offset, settls;
282
283	stack_offset = PAGE_SIZE - sizeof(struct pt_regs);
284	if (!(regs->ps & 8))
285		stack_offset = (PAGE_SIZE-1) & (unsigned long) regs;
286	childregs = (struct pt_regs *)
287	  (stack_offset + PAGE_SIZE + task_stack_page(p));
288
289	*childregs = *regs;
290	settls = regs->r20;
291	childregs->r0 = 0;
292	childregs->r19 = 0;
293	childregs->r20 = 1;	/* OSF/1 has some strange fork() semantics.  */
294	regs->r20 = 0;
295	stack = ((struct switch_stack *) regs) - 1;
296	childstack = ((struct switch_stack *) childregs) - 1;
297	*childstack = *stack;
298	childstack->r26 = (unsigned long) ret_from_fork;
299	childti->pcb.usp = usp;
300	childti->pcb.ksp = (unsigned long) childstack;
301	childti->pcb.flags = 1;	/* set FEN, clear everything else */
302
303	/* Set a new TLS for the child thread?  Peek back into the
304	   syscall arguments that we saved on syscall entry.  Oops,
305	   except we'd have clobbered it with the parent/child set
306	   of r20.  Read the saved copy.  */
307	/* Note: if CLONE_SETTLS is not set, then we must inherit the
308	   value from the parent, which will have been set by the block
309	   copy in dup_task_struct.  This is non-intuitive, but is
310	   required for proper operation in the case of a threaded
311	   application calling fork.  */
312	if (clone_flags & CLONE_SETTLS)
313		childti->pcb.unique = settls;
314
315	return 0;
316}
317
318/*
319 * Fill in the user structure for an ECOFF core dump.
320 */
321void
322dump_thread(struct pt_regs * pt, struct user * dump)
323{
324	/* switch stack follows right below pt_regs: */
325	struct switch_stack * sw = ((struct switch_stack *) pt) - 1;
326
327	dump->magic = CMAGIC;
328	dump->start_code  = current->mm->start_code;
329	dump->start_data  = current->mm->start_data;
330	dump->start_stack = rdusp() & ~(PAGE_SIZE - 1);
331	dump->u_tsize = ((current->mm->end_code - dump->start_code)
332			 >> PAGE_SHIFT);
333	dump->u_dsize = ((current->mm->brk + PAGE_SIZE-1 - dump->start_data)
334			 >> PAGE_SHIFT);
335	dump->u_ssize = (current->mm->start_stack - dump->start_stack
336			 + PAGE_SIZE-1) >> PAGE_SHIFT;
337
338	/*
339	 * We store the registers in an order/format that is
340	 * compatible with DEC Unix/OSF/1 as this makes life easier
341	 * for gdb.
342	 */
343	dump->regs[EF_V0]  = pt->r0;
344	dump->regs[EF_T0]  = pt->r1;
345	dump->regs[EF_T1]  = pt->r2;
346	dump->regs[EF_T2]  = pt->r3;
347	dump->regs[EF_T3]  = pt->r4;
348	dump->regs[EF_T4]  = pt->r5;
349	dump->regs[EF_T5]  = pt->r6;
350	dump->regs[EF_T6]  = pt->r7;
351	dump->regs[EF_T7]  = pt->r8;
352	dump->regs[EF_S0]  = sw->r9;
353	dump->regs[EF_S1]  = sw->r10;
354	dump->regs[EF_S2]  = sw->r11;
355	dump->regs[EF_S3]  = sw->r12;
356	dump->regs[EF_S4]  = sw->r13;
357	dump->regs[EF_S5]  = sw->r14;
358	dump->regs[EF_S6]  = sw->r15;
359	dump->regs[EF_A3]  = pt->r19;
360	dump->regs[EF_A4]  = pt->r20;
361	dump->regs[EF_A5]  = pt->r21;
362	dump->regs[EF_T8]  = pt->r22;
363	dump->regs[EF_T9]  = pt->r23;
364	dump->regs[EF_T10] = pt->r24;
365	dump->regs[EF_T11] = pt->r25;
366	dump->regs[EF_RA]  = pt->r26;
367	dump->regs[EF_T12] = pt->r27;
368	dump->regs[EF_AT]  = pt->r28;
369	dump->regs[EF_SP]  = rdusp();
370	dump->regs[EF_PS]  = pt->ps;
371	dump->regs[EF_PC]  = pt->pc;
372	dump->regs[EF_GP]  = pt->gp;
373	dump->regs[EF_A0]  = pt->r16;
374	dump->regs[EF_A1]  = pt->r17;
375	dump->regs[EF_A2]  = pt->r18;
376	memcpy((char *)dump->regs + EF_SIZE, sw->fp, 32 * 8);
377}
378EXPORT_SYMBOL(dump_thread);
379
380/*
381 * Fill in the user structure for a ELF core dump.
382 */
383void
384dump_elf_thread(elf_greg_t *dest, struct pt_regs *pt, struct thread_info *ti)
385{
386	/* switch stack follows right below pt_regs: */
387	struct switch_stack * sw = ((struct switch_stack *) pt) - 1;
388
389	dest[ 0] = pt->r0;
390	dest[ 1] = pt->r1;
391	dest[ 2] = pt->r2;
392	dest[ 3] = pt->r3;
393	dest[ 4] = pt->r4;
394	dest[ 5] = pt->r5;
395	dest[ 6] = pt->r6;
396	dest[ 7] = pt->r7;
397	dest[ 8] = pt->r8;
398	dest[ 9] = sw->r9;
399	dest[10] = sw->r10;
400	dest[11] = sw->r11;
401	dest[12] = sw->r12;
402	dest[13] = sw->r13;
403	dest[14] = sw->r14;
404	dest[15] = sw->r15;
405	dest[16] = pt->r16;
406	dest[17] = pt->r17;
407	dest[18] = pt->r18;
408	dest[19] = pt->r19;
409	dest[20] = pt->r20;
410	dest[21] = pt->r21;
411	dest[22] = pt->r22;
412	dest[23] = pt->r23;
413	dest[24] = pt->r24;
414	dest[25] = pt->r25;
415	dest[26] = pt->r26;
416	dest[27] = pt->r27;
417	dest[28] = pt->r28;
418	dest[29] = pt->gp;
419	dest[30] = rdusp();
420	dest[31] = pt->pc;
421
422	/* Once upon a time this was the PS value.  Which is stupid
423	   since that is always 8 for usermode.  Usurped for the more
424	   useful value of the thread's UNIQUE field.  */
425	dest[32] = ti->pcb.unique;
426}
427EXPORT_SYMBOL(dump_elf_thread);
428
429int
430dump_elf_task(elf_greg_t *dest, struct task_struct *task)
431{
432	dump_elf_thread(dest, task_pt_regs(task), task_thread_info(task));
433	return 1;
434}
435EXPORT_SYMBOL(dump_elf_task);
436
437int
438dump_elf_task_fp(elf_fpreg_t *dest, struct task_struct *task)
439{
440	struct switch_stack *sw = (struct switch_stack *)task_pt_regs(task) - 1;
441	memcpy(dest, sw->fp, 32 * 8);
442	return 1;
443}
444EXPORT_SYMBOL(dump_elf_task_fp);
445
446/*
447 * sys_execve() executes a new program.
448 */
449asmlinkage int
450do_sys_execve(char __user *ufilename, char __user * __user *argv,
451	      char __user * __user *envp, struct pt_regs *regs)
452{
453	int error;
454	char *filename;
455
456	filename = getname(ufilename);
457	error = PTR_ERR(filename);
458	if (IS_ERR(filename))
459		goto out;
460	error = do_execve(filename, argv, envp, regs);
461	putname(filename);
462out:
463	return error;
464}
465
466/*
467 * Return saved PC of a blocked thread.  This assumes the frame
468 * pointer is the 6th saved long on the kernel stack and that the
469 * saved return address is the first long in the frame.  This all
470 * holds provided the thread blocked through a call to schedule() ($15
471 * is the frame pointer in schedule() and $15 is saved at offset 48 by
472 * entry.S:do_switch_stack).
473 *
474 * Under heavy swap load I've seen this lose in an ugly way.  So do
475 * some extra sanity checking on the ranges we expect these pointers
476 * to be in so that we can fail gracefully.  This is just for ps after
477 * all.  -- r~
478 */
479
480unsigned long
481thread_saved_pc(struct task_struct *t)
482{
483	unsigned long base = (unsigned long)task_stack_page(t);
484	unsigned long fp, sp = task_thread_info(t)->pcb.ksp;
485
486	if (sp > base && sp+6*8 < base + 16*1024) {
487		fp = ((unsigned long*)sp)[6];
488		if (fp > sp && fp < base + 16*1024)
489			return *(unsigned long *)fp;
490	}
491
492	return 0;
493}
494
495unsigned long
496get_wchan(struct task_struct *p)
497{
498	unsigned long schedule_frame;
499	unsigned long pc;
500	if (!p || p == current || p->state == TASK_RUNNING)
501		return 0;
502	/*
503	 * This one depends on the frame size of schedule().  Do a
504	 * "disass schedule" in gdb to find the frame size.  Also, the
505	 * code assumes that sleep_on() follows immediately after
506	 * interruptible_sleep_on() and that add_timer() follows
507	 * immediately after interruptible_sleep().  Ugly, isn't it?
508	 * Maybe adding a wchan field to task_struct would be better,
509	 * after all...
510	 */
511
512	pc = thread_saved_pc(p);
513	if (in_sched_functions(pc)) {
514		schedule_frame = ((unsigned long *)task_thread_info(p)->pcb.ksp)[6];
515		return ((unsigned long *)schedule_frame)[12];
516	}
517	return pc;
518}
519