process.c 13 KB

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