blob: a4dc79ba030fab68ec004ead9ae84d3b1c70bd23 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/stddef.h>
18#include <linux/unistd.h>
19#include <linux/ptrace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/user.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/time.h>
22#include <linux/major.h>
23#include <linux/stat.h>
Jon Smirla8f340e2006-07-10 04:44:12 -070024#include <linux/vt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/mman.h>
26#include <linux/elfcore.h>
27#include <linux/reboot.h>
28#include <linux/tty.h>
29#include <linux/console.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Frederic Weisbecker4c94cad2012-08-22 17:27:34 +020031#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33#include <asm/reg.h>
34#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <asm/io.h>
36#include <asm/pgtable.h>
37#include <asm/hwrpb.h>
38#include <asm/fpu.h>
39
40#include "proto.h"
41#include "pci_impl.h"
42
Eric W. Biederman5e382912006-01-08 01:03:46 -080043/*
44 * Power off function, if any
45 */
46void (*pm_power_off)(void) = machine_power_off;
Al Viro89eb1692007-01-30 13:23:25 +000047EXPORT_SYMBOL(pm_power_off);
Eric W. Biederman5e382912006-01-08 01:03:46 -080048
Linus Torvalds1da177e2005-04-16 15:20:36 -070049void
50cpu_idle(void)
51{
Al Viro3185bd22012-10-20 15:52:23 +010052 current_thread_info()->status |= TS_POLLING;
Nick Piggin64c7c8f2005-11-08 21:39:04 -080053
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 /* FIXME -- EV6 and LCA45 know how to power down
56 the CPU. */
57
Frederic Weisbecker4c94cad2012-08-22 17:27:34 +020058 rcu_idle_enter();
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 while (!need_resched())
Nick Piggin64c7c8f2005-11-08 21:39:04 -080060 cpu_relax();
Frederic Weisbecker6a6c0272012-08-23 14:11:25 +020061
Frederic Weisbecker4c94cad2012-08-22 17:27:34 +020062 rcu_idle_exit();
Frederic Weisbecker6a6c0272012-08-23 14:11:25 +020063 schedule_preempt_disabled();
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 }
65}
66
67
68struct halt_info {
69 int mode;
70 char *restart_cmd;
71};
72
73static void
74common_shutdown_1(void *generic_ptr)
75{
76 struct halt_info *how = (struct halt_info *)generic_ptr;
77 struct percpu_struct *cpup;
78 unsigned long *pflags, flags;
79 int cpuid = smp_processor_id();
80
81 /* No point in taking interrupts anymore. */
82 local_irq_disable();
83
84 cpup = (struct percpu_struct *)
85 ((unsigned long)hwrpb + hwrpb->processor_offset
86 + hwrpb->processor_size * cpuid);
87 pflags = &cpup->flags;
88 flags = *pflags;
89
90 /* Clear reason to "default"; clear "bootstrap in progress". */
91 flags &= ~0x00ff0001UL;
92
93#ifdef CONFIG_SMP
94 /* Secondaries halt here. */
95 if (cpuid != boot_cpuid) {
96 flags |= 0x00040000UL; /* "remain halted" */
97 *pflags = flags;
Rusty Russell1371be02009-02-16 17:31:59 -060098 set_cpu_present(cpuid, false);
99 set_cpu_possible(cpuid, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 halt();
101 }
102#endif
103
104 if (how->mode == LINUX_REBOOT_CMD_RESTART) {
105 if (!how->restart_cmd) {
106 flags |= 0x00020000UL; /* "cold bootstrap" */
107 } else {
108 /* For SRM, we could probably set environment
109 variables to get this to work. We'd have to
110 delay this until after srm_paging_stop unless
111 we ever got srm_fixup working.
112
113 At the moment, SRM will use the last boot device,
114 but the file and flags will be the defaults, when
115 doing a "warm" bootstrap. */
116 flags |= 0x00030000UL; /* "warm bootstrap" */
117 }
118 } else {
119 flags |= 0x00040000UL; /* "remain halted" */
120 }
121 *pflags = flags;
122
123#ifdef CONFIG_SMP
124 /* Wait for the secondaries to halt. */
Rusty Russell1371be02009-02-16 17:31:59 -0600125 set_cpu_present(boot_cpuid, false);
126 set_cpu_possible(boot_cpuid, false);
KOSAKI Motohiro81740fc2011-05-24 17:12:56 -0700127 while (cpumask_weight(cpu_present_mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 barrier();
129#endif
130
131 /* If booted from SRM, reset some of the original environment. */
132 if (alpha_using_srm) {
133#ifdef CONFIG_DUMMY_CONSOLE
Ivan Kokshaysky4b3c86a2005-09-22 21:43:57 -0700134 /* If we've gotten here after SysRq-b, leave interrupt
135 context before taking over the console. */
136 if (in_interrupt())
137 irq_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 /* This has the effect of resetting the VGA video origin. */
139 take_over_console(&dummy_con, 0, MAX_NR_CONSOLES-1, 1);
140#endif
141 pci_restore_srm_config();
142 set_hae(srm_hae);
143 }
144
145 if (alpha_mv.kill_arch)
146 alpha_mv.kill_arch(how->mode);
147
148 if (! alpha_using_srm && how->mode != LINUX_REBOOT_CMD_RESTART) {
149 /* Unfortunately, since MILO doesn't currently understand
150 the hwrpb bits above, we can't reliably halt the
151 processor and keep it halted. So just loop. */
152 return;
153 }
154
155 if (alpha_using_srm)
156 srm_paging_stop();
157
158 halt();
159}
160
161static void
162common_shutdown(int mode, char *restart_cmd)
163{
164 struct halt_info args;
165 args.mode = mode;
166 args.restart_cmd = restart_cmd;
Jens Axboe15c8b6c2008-05-09 09:39:44 +0200167 on_each_cpu(common_shutdown_1, &args, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168}
169
170void
171machine_restart(char *restart_cmd)
172{
173 common_shutdown(LINUX_REBOOT_CMD_RESTART, restart_cmd);
174}
175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177void
178machine_halt(void)
179{
180 common_shutdown(LINUX_REBOOT_CMD_HALT, NULL);
181}
182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
184void
185machine_power_off(void)
186{
187 common_shutdown(LINUX_REBOOT_CMD_POWER_OFF, NULL);
188}
189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
191/* Used by sysrq-p, among others. I don't believe r9-r15 are ever
192 saved in the context it's used. */
193
194void
195show_regs(struct pt_regs *regs)
196{
197 dik_show_regs(regs, NULL);
198}
199
200/*
201 * Re-start a thread when doing execve()
202 */
203void
204start_thread(struct pt_regs * regs, unsigned long pc, unsigned long sp)
205{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 regs->pc = pc;
207 regs->ps = 8;
208 wrusp(sp);
209}
Al Virocff52da2006-10-11 17:40:22 +0100210EXPORT_SYMBOL(start_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
212/*
213 * Free current thread data structures etc..
214 */
215void
216exit_thread(void)
217{
218}
219
220void
221flush_thread(void)
222{
223 /* Arrange for each exec'ed process to start off with a clean slate
224 with respect to the FPU. This is all exceptions disabled. */
225 current_thread_info()->ieee_state = 0;
226 wrfpcr(FPCR_DYN_NORMAL | ieee_swcr_to_fpcr(0));
227
228 /* Clean slate for TLS. */
229 current_thread_info()->pcb.unique = 0;
230}
231
232void
233release_thread(struct task_struct *dead_task)
234{
235}
236
237/*
238 * "alpha_clone()".. By the time we get here, the
239 * non-volatile registers have also been saved on the
240 * stack. We do some ugly pointer stuff here.. (see
241 * also copy_thread)
242 *
243 * Notice that "fork()" is implemented in terms of clone,
244 * with parameters (SIGCHLD, 0).
245 */
246int
247alpha_clone(unsigned long clone_flags, unsigned long usp,
248 int __user *parent_tid, int __user *child_tid,
Al Viroe0e431a2012-10-18 01:11:58 -0400249 unsigned long tls_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
Al Viroe0e431a2012-10-18 01:11:58 -0400251 return do_fork(clone_flags, usp, current_pt_regs(), 0,
252 parent_tid, child_tid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253}
254
255int
Al Viroe0e431a2012-10-18 01:11:58 -0400256alpha_vfork(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
Al Viroe0e431a2012-10-18 01:11:58 -0400258 return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, 0,
259 current_pt_regs(), 0, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260}
261
262/*
263 * Copy an alpha thread..
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 */
265
266int
Alexey Dobriyan6f2c55b2009-04-02 16:56:59 -0700267copy_thread(unsigned long clone_flags, unsigned long usp,
Al Virocba1ec72012-09-09 22:03:42 -0400268 unsigned long arg,
Al Viro25906732012-10-21 15:52:04 -0400269 struct task_struct *p, struct pt_regs *wontuse)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
271 extern void ret_from_fork(void);
Al Virocba1ec72012-09-09 22:03:42 -0400272 extern void ret_from_kernel_thread(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Al Viro27f45132006-01-12 01:05:36 -0800274 struct thread_info *childti = task_thread_info(p);
Al Virocba1ec72012-09-09 22:03:42 -0400275 struct pt_regs *childregs = task_pt_regs(p);
Al Viro25906732012-10-21 15:52:04 -0400276 struct pt_regs *regs = current_pt_regs();
Al Virocba1ec72012-09-09 22:03:42 -0400277 struct switch_stack *childstack, *stack;
278 unsigned long settls;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Al Virocba1ec72012-09-09 22:03:42 -0400280 childstack = ((struct switch_stack *) childregs) - 1;
Al Viro25906732012-10-21 15:52:04 -0400281 childti->pcb.ksp = (unsigned long) childstack;
282 childti->pcb.flags = 1; /* set FEN, clear everything else */
283
284 if (unlikely(p->flags & PF_KTHREAD)) {
Al Virocba1ec72012-09-09 22:03:42 -0400285 /* kernel thread */
286 memset(childstack, 0,
287 sizeof(struct switch_stack) + sizeof(struct pt_regs));
288 childstack->r26 = (unsigned long) ret_from_kernel_thread;
289 childstack->r9 = usp; /* function */
290 childstack->r10 = arg;
291 childregs->hae = alpha_mv.hae_cache,
292 childti->pcb.usp = 0;
Al Virocba1ec72012-09-09 22:03:42 -0400293 return 0;
294 }
Al Viro25906732012-10-21 15:52:04 -0400295 /* Note: if CLONE_SETTLS is not set, then we must inherit the
296 value from the parent, which will have been set by the block
297 copy in dup_task_struct. This is non-intuitive, but is
298 required for proper operation in the case of a threaded
299 application calling fork. */
300 if (clone_flags & CLONE_SETTLS)
301 childti->pcb.unique = regs->r20;
302 childti->pcb.usp = usp ?: rdusp();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 *childregs = *regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 childregs->r0 = 0;
305 childregs->r19 = 0;
306 childregs->r20 = 1; /* OSF/1 has some strange fork() semantics. */
307 regs->r20 = 0;
308 stack = ((struct switch_stack *) regs) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 *childstack = *stack;
310 childstack->r26 = (unsigned long) ret_from_fork;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 return 0;
312}
313
314/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 * Fill in the user structure for a ELF core dump.
316 */
317void
318dump_elf_thread(elf_greg_t *dest, struct pt_regs *pt, struct thread_info *ti)
319{
320 /* switch stack follows right below pt_regs: */
321 struct switch_stack * sw = ((struct switch_stack *) pt) - 1;
322
323 dest[ 0] = pt->r0;
324 dest[ 1] = pt->r1;
325 dest[ 2] = pt->r2;
326 dest[ 3] = pt->r3;
327 dest[ 4] = pt->r4;
328 dest[ 5] = pt->r5;
329 dest[ 6] = pt->r6;
330 dest[ 7] = pt->r7;
331 dest[ 8] = pt->r8;
332 dest[ 9] = sw->r9;
333 dest[10] = sw->r10;
334 dest[11] = sw->r11;
335 dest[12] = sw->r12;
336 dest[13] = sw->r13;
337 dest[14] = sw->r14;
338 dest[15] = sw->r15;
339 dest[16] = pt->r16;
340 dest[17] = pt->r17;
341 dest[18] = pt->r18;
342 dest[19] = pt->r19;
343 dest[20] = pt->r20;
344 dest[21] = pt->r21;
345 dest[22] = pt->r22;
346 dest[23] = pt->r23;
347 dest[24] = pt->r24;
348 dest[25] = pt->r25;
349 dest[26] = pt->r26;
350 dest[27] = pt->r27;
351 dest[28] = pt->r28;
352 dest[29] = pt->gp;
Al Viro32163f42010-09-25 21:07:51 +0100353 dest[30] = ti == current_thread_info() ? rdusp() : ti->pcb.usp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 dest[31] = pt->pc;
355
356 /* Once upon a time this was the PS value. Which is stupid
357 since that is always 8 for usermode. Usurped for the more
358 useful value of the thread's UNIQUE field. */
359 dest[32] = ti->pcb.unique;
360}
Al Virocff52da2006-10-11 17:40:22 +0100361EXPORT_SYMBOL(dump_elf_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
363int
364dump_elf_task(elf_greg_t *dest, struct task_struct *task)
365{
akpm@osdl.orge52f4ca2006-01-12 01:05:37 -0800366 dump_elf_thread(dest, task_pt_regs(task), task_thread_info(task));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 return 1;
368}
Al Virocff52da2006-10-11 17:40:22 +0100369EXPORT_SYMBOL(dump_elf_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
371int
372dump_elf_task_fp(elf_fpreg_t *dest, struct task_struct *task)
373{
akpm@osdl.orge52f4ca2006-01-12 01:05:37 -0800374 struct switch_stack *sw = (struct switch_stack *)task_pt_regs(task) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 memcpy(dest, sw->fp, 32 * 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 return 1;
377}
Al Virocff52da2006-10-11 17:40:22 +0100378EXPORT_SYMBOL(dump_elf_task_fp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
380/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 * Return saved PC of a blocked thread. This assumes the frame
382 * pointer is the 6th saved long on the kernel stack and that the
383 * saved return address is the first long in the frame. This all
384 * holds provided the thread blocked through a call to schedule() ($15
385 * is the frame pointer in schedule() and $15 is saved at offset 48 by
386 * entry.S:do_switch_stack).
387 *
388 * Under heavy swap load I've seen this lose in an ugly way. So do
389 * some extra sanity checking on the ranges we expect these pointers
390 * to be in so that we can fail gracefully. This is just for ps after
391 * all. -- r~
392 */
393
394unsigned long
Ingo Molnar36c8b582006-07-03 00:25:41 -0700395thread_saved_pc(struct task_struct *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396{
Al Viro27f45132006-01-12 01:05:36 -0800397 unsigned long base = (unsigned long)task_stack_page(t);
Al Viro37bfbaf2006-01-12 01:05:36 -0800398 unsigned long fp, sp = task_thread_info(t)->pcb.ksp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400 if (sp > base && sp+6*8 < base + 16*1024) {
401 fp = ((unsigned long*)sp)[6];
402 if (fp > sp && fp < base + 16*1024)
403 return *(unsigned long *)fp;
404 }
405
406 return 0;
407}
408
409unsigned long
410get_wchan(struct task_struct *p)
411{
412 unsigned long schedule_frame;
413 unsigned long pc;
414 if (!p || p == current || p->state == TASK_RUNNING)
415 return 0;
416 /*
417 * This one depends on the frame size of schedule(). Do a
418 * "disass schedule" in gdb to find the frame size. Also, the
419 * code assumes that sleep_on() follows immediately after
420 * interruptible_sleep_on() and that add_timer() follows
421 * immediately after interruptible_sleep(). Ugly, isn't it?
422 * Maybe adding a wchan field to task_struct would be better,
423 * after all...
424 */
425
426 pc = thread_saved_pc(p);
427 if (in_sched_functions(pc)) {
Al Viro37bfbaf2006-01-12 01:05:36 -0800428 schedule_frame = ((unsigned long *)task_thread_info(p)->pcb.ksp)[6];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 return ((unsigned long *)schedule_frame)[12];
430 }
431 return pc;
432}