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/config.h>
12 #include <linux/errno.h>
13 #include <linux/sched.h>
14 #include <linux/kernel.h>
15 #include <linux/mm.h>
16 #include <linux/smp.h>
17 #include <linux/smp_lock.h>
18 #include <linux/stddef.h>
19 #include <linux/unistd.h>
20 #include <linux/ptrace.h>
21 #include <linux/slab.h>
22 #include <linux/user.h>
23 #include <linux/a.out.h>
24 #include <linux/utsname.h>
25 #include <linux/time.h>
26 #include <linux/major.h>
27 #include <linux/stat.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 * Initial task structure. Make this a per-architecture thing,
47 * because different architectures tend to have different
48 * alignment requirements and potentially different initial
49 * setup.
50 */
51
52 unsigned long init_user_stack[1024] = { STACK_MAGIC, };
53 static struct fs_struct init_fs = INIT_FS;
54 static struct files_struct init_files = INIT_FILES;
55 static struct signal_struct init_signals = INIT_SIGNALS;
56 struct mm_struct init_mm = INIT_MM(init_mm);
57
58 union task_union init_task_union __attribute__((section("init_task")))
59 = { task: INIT_TASK(init_task_union.task) };
60
61 /*
62 * No need to acquire the kernel lock, we're entirely local..
63 */
64 asmlinkage int
sys_sethae(unsigned long hae,unsigned long a1,unsigned long a2,unsigned long a3,unsigned long a4,unsigned long a5,struct pt_regs regs)65 sys_sethae(unsigned long hae, unsigned long a1, unsigned long a2,
66 unsigned long a3, unsigned long a4, unsigned long a5,
67 struct pt_regs regs)
68 {
69 (®s)->hae = hae;
70 return 0;
71 }
72
73 void
cpu_idle(void)74 cpu_idle(void)
75 {
76 /* An endless idle loop with no priority at all. */
77 current->nice = 20;
78 current->counter = -100;
79
80 while (1) {
81 /* FIXME -- EV6 and LCA45 know how to power down
82 the CPU. */
83
84 /* Although we are an idle CPU, we do not want to
85 get into the scheduler unnecessarily. */
86 long oldval = xchg(¤t->need_resched, -1UL);
87 if (!oldval)
88 while (current->need_resched < 0);
89 schedule();
90 check_pgt_cache();
91 }
92 }
93
94
95 struct halt_info {
96 int mode;
97 char *restart_cmd;
98 };
99
100 static void
common_shutdown_1(void * generic_ptr)101 common_shutdown_1(void *generic_ptr)
102 {
103 struct halt_info *how = (struct halt_info *)generic_ptr;
104 struct percpu_struct *cpup;
105 unsigned long *pflags, flags;
106 int cpuid = smp_processor_id();
107
108 /* No point in taking interrupts anymore. */
109 __cli();
110
111 cpup = (struct percpu_struct *)
112 ((unsigned long)hwrpb + hwrpb->processor_offset
113 + hwrpb->processor_size * cpuid);
114 pflags = &cpup->flags;
115 flags = *pflags;
116
117 /* Clear reason to "default"; clear "bootstrap in progress". */
118 flags &= ~0x00ff0001UL;
119
120 #ifdef CONFIG_SMP
121 /* Secondaries halt here. */
122 if (cpuid != boot_cpuid) {
123 flags |= 0x00040000UL; /* "remain halted" */
124 *pflags = flags;
125 clear_bit(cpuid, &cpu_present_mask);
126 halt();
127 }
128 #endif
129
130 if (how->mode == LINUX_REBOOT_CMD_RESTART) {
131 if (!how->restart_cmd) {
132 flags |= 0x00020000UL; /* "cold bootstrap" */
133 } else {
134 /* For SRM, we could probably set environment
135 variables to get this to work. We'd have to
136 delay this until after srm_paging_stop unless
137 we ever got srm_fixup working.
138
139 At the moment, SRM will use the last boot device,
140 but the file and flags will be the defaults, when
141 doing a "warm" bootstrap. */
142 flags |= 0x00030000UL; /* "warm bootstrap" */
143 }
144 } else {
145 flags |= 0x00040000UL; /* "remain halted" */
146 }
147 *pflags = flags;
148
149 #ifdef CONFIG_SMP
150 /* Wait for the secondaries to halt. */
151 clear_bit(boot_cpuid, &cpu_present_mask);
152 while (cpu_present_mask)
153 barrier();
154 #endif
155
156 /* If booted from SRM, reset some of the original environment. */
157 if (alpha_using_srm) {
158 #ifdef CONFIG_DUMMY_CONSOLE
159 /* This has the effect of resetting the VGA video origin. */
160 take_over_console(&dummy_con, 0, MAX_NR_CONSOLES-1, 1);
161 #endif
162 pci_restore_srm_config();
163 set_hae(srm_hae);
164 }
165
166 if (alpha_mv.kill_arch)
167 alpha_mv.kill_arch(how->mode);
168
169 if (! alpha_using_srm && how->mode != LINUX_REBOOT_CMD_RESTART) {
170 /* Unfortunately, since MILO doesn't currently understand
171 the hwrpb bits above, we can't reliably halt the
172 processor and keep it halted. So just loop. */
173 return;
174 }
175
176 if (alpha_using_srm)
177 srm_paging_stop();
178
179 halt();
180 }
181
182 static void
common_shutdown(int mode,char * restart_cmd)183 common_shutdown(int mode, char *restart_cmd)
184 {
185 struct halt_info args;
186 args.mode = mode;
187 args.restart_cmd = restart_cmd;
188 #ifdef CONFIG_SMP
189 smp_call_function(common_shutdown_1, &args, 1, 0);
190 #endif
191 common_shutdown_1(&args);
192 }
193
194 void
machine_restart(char * restart_cmd)195 machine_restart(char *restart_cmd)
196 {
197 common_shutdown(LINUX_REBOOT_CMD_RESTART, restart_cmd);
198 }
199
200 void
machine_halt(void)201 machine_halt(void)
202 {
203 common_shutdown(LINUX_REBOOT_CMD_HALT, NULL);
204 }
205
206 void
machine_power_off(void)207 machine_power_off(void)
208 {
209 common_shutdown(LINUX_REBOOT_CMD_POWER_OFF, NULL);
210 }
211
212 void
show_regs(struct pt_regs * regs)213 show_regs(struct pt_regs * regs)
214 {
215 printk("\n");
216 printk("Pid: %d, comm: %20s\n", current->pid, current->comm);
217 printk("ps: %04lx pc: [<%016lx>] CPU %d %s\n",
218 regs->ps, regs->pc, smp_processor_id(), print_tainted());
219 printk("rp: [<%016lx>] sp: %p\n", regs->r26, regs+1);
220 printk(" r0: %016lx r1: %016lx r2: %016lx r3: %016lx\n",
221 regs->r0, regs->r1, regs->r2, regs->r3);
222 printk(" r4: %016lx r5: %016lx r6: %016lx r7: %016lx\n",
223 regs->r4, regs->r5, regs->r6, regs->r7);
224 printk(" r8: %016lx r16: %016lx r17: %016lx r18: %016lx\n",
225 regs->r8, regs->r16, regs->r17, regs->r18);
226 printk("r19: %016lx r20: %016lx r21: %016lx r22: %016lx\n",
227 regs->r19, regs->r20, regs->r21, regs->r22);
228 printk("r23: %016lx r24: %016lx r25: %016lx r26: %016lx\n",
229 regs->r23, regs->r24, regs->r25, regs->r26);
230 printk("r27: %016lx r28: %016lx r29: %016lx hae: %016lx\n",
231 regs->r27, regs->r28, regs->gp, regs->hae);
232 }
233
234 /*
235 * Re-start a thread when doing execve()
236 */
237 void
start_thread(struct pt_regs * regs,unsigned long pc,unsigned long sp)238 start_thread(struct pt_regs * regs, unsigned long pc, unsigned long sp)
239 {
240 set_fs(USER_DS);
241 regs->pc = pc;
242 regs->ps = 8;
243 wrusp(sp);
244 }
245
246 /*
247 * Free current thread data structures etc..
248 */
249 void
exit_thread(void)250 exit_thread(void)
251 {
252 }
253
254 void
flush_thread(void)255 flush_thread(void)
256 {
257 /* Arrange for each exec'ed process to start off with a clean slate
258 with respect to the FPU. This is all exceptions disabled. */
259 current->thread.flags &= ~IEEE_SW_MASK;
260 wrfpcr(FPCR_DYN_NORMAL | ieee_swcr_to_fpcr(0));
261 }
262
263 void
release_thread(struct task_struct * dead_task)264 release_thread(struct task_struct *dead_task)
265 {
266 }
267
268 /*
269 * "alpha_clone()".. By the time we get here, the
270 * non-volatile registers have also been saved on the
271 * stack. We do some ugly pointer stuff here.. (see
272 * also copy_thread)
273 *
274 * Notice that "fork()" is implemented in terms of clone,
275 * with parameters (SIGCHLD, 0).
276 */
277 int
alpha_clone(unsigned long clone_flags,unsigned long usp,struct switch_stack * swstack)278 alpha_clone(unsigned long clone_flags, unsigned long usp,
279 struct switch_stack * swstack)
280 {
281 if (!usp)
282 usp = rdusp();
283 return do_fork(clone_flags, usp, (struct pt_regs *) (swstack+1), 0);
284 }
285
286 int
alpha_vfork(struct switch_stack * swstack)287 alpha_vfork(struct switch_stack * swstack)
288 {
289 return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, rdusp(),
290 (struct pt_regs *) (swstack+1), 0);
291 }
292
293 /*
294 * Copy an alpha thread..
295 *
296 * Note the "stack_offset" stuff: when returning to kernel mode, we need
297 * to have some extra stack-space for the kernel stack that still exists
298 * after the "ret_from_sys_call". When returning to user mode, we only
299 * want the space needed by the syscall stack frame (ie "struct pt_regs").
300 * Use the passed "regs" pointer to determine how much space we need
301 * for a kernel fork().
302 */
303
304 int
copy_thread(int nr,unsigned long clone_flags,unsigned long usp,unsigned long unused,struct task_struct * p,struct pt_regs * regs)305 copy_thread(int nr, unsigned long clone_flags, unsigned long usp,
306 unsigned long unused,
307 struct task_struct * p, struct pt_regs * regs)
308 {
309 extern void ret_from_sys_call(void);
310 extern void ret_from_fork(void);
311
312 struct pt_regs * childregs;
313 struct switch_stack * childstack, *stack;
314 unsigned long stack_offset;
315
316 stack_offset = PAGE_SIZE - sizeof(struct pt_regs);
317 if (!(regs->ps & 8))
318 stack_offset = (PAGE_SIZE-1) & (unsigned long) regs;
319 childregs = (struct pt_regs *) (stack_offset + PAGE_SIZE + (long)p);
320
321 *childregs = *regs;
322 childregs->r0 = 0;
323 childregs->r19 = 0;
324 childregs->r20 = 1; /* OSF/1 has some strange fork() semantics. */
325 regs->r20 = 0;
326 stack = ((struct switch_stack *) regs) - 1;
327 childstack = ((struct switch_stack *) childregs) - 1;
328 *childstack = *stack;
329 childstack->r26 = (unsigned long) ret_from_fork;
330 p->thread.usp = usp;
331 p->thread.ksp = (unsigned long) childstack;
332 p->thread.pal_flags = 1; /* set FEN, clear everything else */
333 p->thread.flags = current->thread.flags;
334
335 return 0;
336 }
337
338 /*
339 * Fill in the user structure for an ECOFF core dump.
340 */
341 void
dump_thread(struct pt_regs * pt,struct user * dump)342 dump_thread(struct pt_regs * pt, struct user * dump)
343 {
344 /* switch stack follows right below pt_regs: */
345 struct switch_stack * sw = ((struct switch_stack *) pt) - 1;
346
347 dump->magic = CMAGIC;
348 dump->start_code = current->mm->start_code;
349 dump->start_data = current->mm->start_data;
350 dump->start_stack = rdusp() & ~(PAGE_SIZE - 1);
351 dump->u_tsize = ((current->mm->end_code - dump->start_code)
352 >> PAGE_SHIFT);
353 dump->u_dsize = ((current->mm->brk + PAGE_SIZE-1 - dump->start_data)
354 >> PAGE_SHIFT);
355 dump->u_ssize = (current->mm->start_stack - dump->start_stack
356 + PAGE_SIZE-1) >> PAGE_SHIFT;
357
358 /*
359 * We store the registers in an order/format that is
360 * compatible with DEC Unix/OSF/1 as this makes life easier
361 * for gdb.
362 */
363 dump->regs[EF_V0] = pt->r0;
364 dump->regs[EF_T0] = pt->r1;
365 dump->regs[EF_T1] = pt->r2;
366 dump->regs[EF_T2] = pt->r3;
367 dump->regs[EF_T3] = pt->r4;
368 dump->regs[EF_T4] = pt->r5;
369 dump->regs[EF_T5] = pt->r6;
370 dump->regs[EF_T6] = pt->r7;
371 dump->regs[EF_T7] = pt->r8;
372 dump->regs[EF_S0] = sw->r9;
373 dump->regs[EF_S1] = sw->r10;
374 dump->regs[EF_S2] = sw->r11;
375 dump->regs[EF_S3] = sw->r12;
376 dump->regs[EF_S4] = sw->r13;
377 dump->regs[EF_S5] = sw->r14;
378 dump->regs[EF_S6] = sw->r15;
379 dump->regs[EF_A3] = pt->r19;
380 dump->regs[EF_A4] = pt->r20;
381 dump->regs[EF_A5] = pt->r21;
382 dump->regs[EF_T8] = pt->r22;
383 dump->regs[EF_T9] = pt->r23;
384 dump->regs[EF_T10] = pt->r24;
385 dump->regs[EF_T11] = pt->r25;
386 dump->regs[EF_RA] = pt->r26;
387 dump->regs[EF_T12] = pt->r27;
388 dump->regs[EF_AT] = pt->r28;
389 dump->regs[EF_SP] = rdusp();
390 dump->regs[EF_PS] = pt->ps;
391 dump->regs[EF_PC] = pt->pc;
392 dump->regs[EF_GP] = pt->gp;
393 dump->regs[EF_A0] = pt->r16;
394 dump->regs[EF_A1] = pt->r17;
395 dump->regs[EF_A2] = pt->r18;
396 memcpy((char *)dump->regs + EF_SIZE, sw->fp, 32 * 8);
397 }
398
399 /*
400 * Fill in the user structure for a ELF core dump.
401 */
402 void
dump_elf_thread(elf_gregset_t dest,struct pt_regs * pt,struct task_struct * task)403 dump_elf_thread(elf_gregset_t dest, struct pt_regs *pt,
404 struct task_struct *task)
405 {
406 /* switch stack follows right below pt_regs: */
407 struct switch_stack * sw = ((struct switch_stack *) pt) - 1;
408
409 dest[ 0] = pt->r0;
410 dest[ 1] = pt->r1;
411 dest[ 2] = pt->r2;
412 dest[ 3] = pt->r3;
413 dest[ 4] = pt->r4;
414 dest[ 5] = pt->r5;
415 dest[ 6] = pt->r6;
416 dest[ 7] = pt->r7;
417 dest[ 8] = pt->r8;
418 dest[ 9] = sw->r9;
419 dest[10] = sw->r10;
420 dest[11] = sw->r11;
421 dest[12] = sw->r12;
422 dest[13] = sw->r13;
423 dest[14] = sw->r14;
424 dest[15] = sw->r15;
425 dest[16] = pt->r16;
426 dest[17] = pt->r17;
427 dest[18] = pt->r18;
428 dest[19] = pt->r19;
429 dest[20] = pt->r20;
430 dest[21] = pt->r21;
431 dest[22] = pt->r22;
432 dest[23] = pt->r23;
433 dest[24] = pt->r24;
434 dest[25] = pt->r25;
435 dest[26] = pt->r26;
436 dest[27] = pt->r27;
437 dest[28] = pt->r28;
438 dest[29] = pt->gp;
439 dest[30] = rdusp();
440 dest[31] = pt->pc;
441
442 /* Once upon a time this was the PS value. Which is stupid
443 since that is always 8 for usermode. Usurped for the more
444 useful value of the thread's UNIQUE field. */
445 dest[32] = task->thread.unique;
446 }
447
448 int
dump_fpu(struct pt_regs * regs,elf_fpregset_t * r)449 dump_fpu(struct pt_regs * regs, elf_fpregset_t *r)
450 {
451 /* switch stack follows right below pt_regs: */
452 struct switch_stack * sw = ((struct switch_stack *) regs) - 1;
453 memcpy(r, sw->fp, 32 * 8);
454 return 1;
455 }
456
457 /*
458 * sys_execve() executes a new program.
459 *
460 * This works due to the alpha calling sequence: the first 6 args
461 * are gotten from registers, while the rest is on the stack, so
462 * we get a0-a5 for free, and then magically find "struct pt_regs"
463 * on the stack for us..
464 *
465 * Don't do this at home.
466 */
467 asmlinkage int
sys_execve(char * ufilename,char ** argv,char ** envp,unsigned long a3,unsigned long a4,unsigned long a5,struct pt_regs regs)468 sys_execve(char *ufilename, char **argv, char **envp,
469 unsigned long a3, unsigned long a4, unsigned long a5,
470 struct pt_regs regs)
471 {
472 int error;
473 char *filename;
474
475 filename = getname(ufilename);
476 error = PTR_ERR(filename);
477 if (IS_ERR(filename))
478 goto out;
479 error = do_execve(filename, argv, envp, ®s);
480 putname(filename);
481 out:
482 return error;
483 }
484
485 /*
486 * These bracket the sleeping functions..
487 */
488 extern void scheduling_functions_start_here(void);
489 extern void scheduling_functions_end_here(void);
490 #define first_sched ((unsigned long) scheduling_functions_start_here)
491 #define last_sched ((unsigned long) scheduling_functions_end_here)
492
493 unsigned long
get_wchan(struct task_struct * p)494 get_wchan(struct task_struct *p)
495 {
496 unsigned long schedule_frame;
497 unsigned long pc;
498 if (!p || p == current || p->state == TASK_RUNNING)
499 return 0;
500 /*
501 * This one depends on the frame size of schedule(). Do a
502 * "disass schedule" in gdb to find the frame size. Also, the
503 * code assumes that sleep_on() follows immediately after
504 * interruptible_sleep_on() and that add_timer() follows
505 * immediately after interruptible_sleep(). Ugly, isn't it?
506 * Maybe adding a wchan field to task_struct would be better,
507 * after all...
508 */
509
510 pc = thread_saved_pc(&p->thread);
511 if (pc >= first_sched && pc < last_sched) {
512 schedule_frame = ((unsigned long *)p->thread.ksp)[6];
513 return ((unsigned long *)schedule_frame)[12];
514 }
515 return pc;
516 }
517