process.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  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/config.h>
  10. #include <linux/errno.h>
  11. #include <linux/module.h>
  12. #include <linux/sched.h>
  13. #include <linux/kernel.h>
  14. #include <linux/mm.h>
  15. #include <linux/smp.h>
  16. #include <linux/smp_lock.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/mman.h>
  28. #include <linux/elfcore.h>
  29. #include <linux/reboot.h>
  30. #include <linux/tty.h>
  31. #include <linux/console.h>
  32. #include <asm/reg.h>
  33. #include <asm/uaccess.h>
  34. #include <asm/system.h>
  35. #include <asm/io.h>
  36. #include <asm/pgtable.h>
  37. #include <asm/hwrpb.h>
  38. #include <asm/fpu.h>
  39. #include "proto.h"
  40. #include "pci_impl.h"
  41. void default_idle(void)
  42. {
  43. barrier();
  44. }
  45. void
  46. cpu_idle(void)
  47. {
  48. while (1) {
  49. void (*idle)(void) = default_idle;
  50. /* FIXME -- EV6 and LCA45 know how to power down
  51. the CPU. */
  52. while (!need_resched())
  53. idle();
  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. clear_bit(cpuid, &cpu_present_mask);
  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_possible_map);
  106. while (cpus_weight(cpu_possible_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. /* This has the effect of resetting the VGA video origin. */
  113. take_over_console(&dummy_con, 0, MAX_NR_CONSOLES-1, 1);
  114. #endif
  115. pci_restore_srm_config();
  116. set_hae(srm_hae);
  117. }
  118. if (alpha_mv.kill_arch)
  119. alpha_mv.kill_arch(how->mode);
  120. if (! alpha_using_srm && how->mode != LINUX_REBOOT_CMD_RESTART) {
  121. /* Unfortunately, since MILO doesn't currently understand
  122. the hwrpb bits above, we can't reliably halt the
  123. processor and keep it halted. So just loop. */
  124. return;
  125. }
  126. if (alpha_using_srm)
  127. srm_paging_stop();
  128. halt();
  129. }
  130. static void
  131. common_shutdown(int mode, char *restart_cmd)
  132. {
  133. struct halt_info args;
  134. args.mode = mode;
  135. args.restart_cmd = restart_cmd;
  136. on_each_cpu(common_shutdown_1, &args, 1, 0);
  137. }
  138. void
  139. machine_restart(char *restart_cmd)
  140. {
  141. common_shutdown(LINUX_REBOOT_CMD_RESTART, restart_cmd);
  142. }
  143. void
  144. machine_halt(void)
  145. {
  146. common_shutdown(LINUX_REBOOT_CMD_HALT, NULL);
  147. }
  148. void
  149. machine_power_off(void)
  150. {
  151. common_shutdown(LINUX_REBOOT_CMD_POWER_OFF, NULL);
  152. }
  153. /* Used by sysrq-p, among others. I don't believe r9-r15 are ever
  154. saved in the context it's used. */
  155. void
  156. show_regs(struct pt_regs *regs)
  157. {
  158. dik_show_regs(regs, NULL);
  159. }
  160. /*
  161. * Re-start a thread when doing execve()
  162. */
  163. void
  164. start_thread(struct pt_regs * regs, unsigned long pc, unsigned long sp)
  165. {
  166. set_fs(USER_DS);
  167. regs->pc = pc;
  168. regs->ps = 8;
  169. wrusp(sp);
  170. }
  171. /*
  172. * Free current thread data structures etc..
  173. */
  174. void
  175. exit_thread(void)
  176. {
  177. }
  178. void
  179. flush_thread(void)
  180. {
  181. /* Arrange for each exec'ed process to start off with a clean slate
  182. with respect to the FPU. This is all exceptions disabled. */
  183. current_thread_info()->ieee_state = 0;
  184. wrfpcr(FPCR_DYN_NORMAL | ieee_swcr_to_fpcr(0));
  185. /* Clean slate for TLS. */
  186. current_thread_info()->pcb.unique = 0;
  187. }
  188. void
  189. release_thread(struct task_struct *dead_task)
  190. {
  191. }
  192. /*
  193. * "alpha_clone()".. By the time we get here, the
  194. * non-volatile registers have also been saved on the
  195. * stack. We do some ugly pointer stuff here.. (see
  196. * also copy_thread)
  197. *
  198. * Notice that "fork()" is implemented in terms of clone,
  199. * with parameters (SIGCHLD, 0).
  200. */
  201. int
  202. alpha_clone(unsigned long clone_flags, unsigned long usp,
  203. int __user *parent_tid, int __user *child_tid,
  204. unsigned long tls_value, struct pt_regs *regs)
  205. {
  206. if (!usp)
  207. usp = rdusp();
  208. return do_fork(clone_flags, usp, regs, 0, parent_tid, child_tid);
  209. }
  210. int
  211. alpha_vfork(struct pt_regs *regs)
  212. {
  213. return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, rdusp(),
  214. regs, 0, NULL, NULL);
  215. }
  216. /*
  217. * Copy an alpha thread..
  218. *
  219. * Note the "stack_offset" stuff: when returning to kernel mode, we need
  220. * to have some extra stack-space for the kernel stack that still exists
  221. * after the "ret_from_fork". When returning to user mode, we only want
  222. * the space needed by the syscall stack frame (ie "struct pt_regs").
  223. * Use the passed "regs" pointer to determine how much space we need
  224. * for a kernel fork().
  225. */
  226. int
  227. copy_thread(int nr, unsigned long clone_flags, unsigned long usp,
  228. unsigned long unused,
  229. struct task_struct * p, struct pt_regs * regs)
  230. {
  231. extern void ret_from_fork(void);
  232. struct thread_info *childti = p->thread_info;
  233. struct pt_regs * childregs;
  234. struct switch_stack * childstack, *stack;
  235. unsigned long stack_offset, settls;
  236. stack_offset = PAGE_SIZE - sizeof(struct pt_regs);
  237. if (!(regs->ps & 8))
  238. stack_offset = (PAGE_SIZE-1) & (unsigned long) regs;
  239. childregs = (struct pt_regs *)
  240. (stack_offset + PAGE_SIZE + (long) childti);
  241. *childregs = *regs;
  242. settls = regs->r20;
  243. childregs->r0 = 0;
  244. childregs->r19 = 0;
  245. childregs->r20 = 1; /* OSF/1 has some strange fork() semantics. */
  246. regs->r20 = 0;
  247. stack = ((struct switch_stack *) regs) - 1;
  248. childstack = ((struct switch_stack *) childregs) - 1;
  249. *childstack = *stack;
  250. childstack->r26 = (unsigned long) ret_from_fork;
  251. childti->pcb.usp = usp;
  252. childti->pcb.ksp = (unsigned long) childstack;
  253. childti->pcb.flags = 1; /* set FEN, clear everything else */
  254. /* Set a new TLS for the child thread? Peek back into the
  255. syscall arguments that we saved on syscall entry. Oops,
  256. except we'd have clobbered it with the parent/child set
  257. of r20. Read the saved copy. */
  258. /* Note: if CLONE_SETTLS is not set, then we must inherit the
  259. value from the parent, which will have been set by the block
  260. copy in dup_task_struct. This is non-intuitive, but is
  261. required for proper operation in the case of a threaded
  262. application calling fork. */
  263. if (clone_flags & CLONE_SETTLS)
  264. childti->pcb.unique = settls;
  265. return 0;
  266. }
  267. /*
  268. * Fill in the user structure for an ECOFF core dump.
  269. */
  270. void
  271. dump_thread(struct pt_regs * pt, struct user * dump)
  272. {
  273. /* switch stack follows right below pt_regs: */
  274. struct switch_stack * sw = ((struct switch_stack *) pt) - 1;
  275. dump->magic = CMAGIC;
  276. dump->start_code = current->mm->start_code;
  277. dump->start_data = current->mm->start_data;
  278. dump->start_stack = rdusp() & ~(PAGE_SIZE - 1);
  279. dump->u_tsize = ((current->mm->end_code - dump->start_code)
  280. >> PAGE_SHIFT);
  281. dump->u_dsize = ((current->mm->brk + PAGE_SIZE-1 - dump->start_data)
  282. >> PAGE_SHIFT);
  283. dump->u_ssize = (current->mm->start_stack - dump->start_stack
  284. + PAGE_SIZE-1) >> PAGE_SHIFT;
  285. /*
  286. * We store the registers in an order/format that is
  287. * compatible with DEC Unix/OSF/1 as this makes life easier
  288. * for gdb.
  289. */
  290. dump->regs[EF_V0] = pt->r0;
  291. dump->regs[EF_T0] = pt->r1;
  292. dump->regs[EF_T1] = pt->r2;
  293. dump->regs[EF_T2] = pt->r3;
  294. dump->regs[EF_T3] = pt->r4;
  295. dump->regs[EF_T4] = pt->r5;
  296. dump->regs[EF_T5] = pt->r6;
  297. dump->regs[EF_T6] = pt->r7;
  298. dump->regs[EF_T7] = pt->r8;
  299. dump->regs[EF_S0] = sw->r9;
  300. dump->regs[EF_S1] = sw->r10;
  301. dump->regs[EF_S2] = sw->r11;
  302. dump->regs[EF_S3] = sw->r12;
  303. dump->regs[EF_S4] = sw->r13;
  304. dump->regs[EF_S5] = sw->r14;
  305. dump->regs[EF_S6] = sw->r15;
  306. dump->regs[EF_A3] = pt->r19;
  307. dump->regs[EF_A4] = pt->r20;
  308. dump->regs[EF_A5] = pt->r21;
  309. dump->regs[EF_T8] = pt->r22;
  310. dump->regs[EF_T9] = pt->r23;
  311. dump->regs[EF_T10] = pt->r24;
  312. dump->regs[EF_T11] = pt->r25;
  313. dump->regs[EF_RA] = pt->r26;
  314. dump->regs[EF_T12] = pt->r27;
  315. dump->regs[EF_AT] = pt->r28;
  316. dump->regs[EF_SP] = rdusp();
  317. dump->regs[EF_PS] = pt->ps;
  318. dump->regs[EF_PC] = pt->pc;
  319. dump->regs[EF_GP] = pt->gp;
  320. dump->regs[EF_A0] = pt->r16;
  321. dump->regs[EF_A1] = pt->r17;
  322. dump->regs[EF_A2] = pt->r18;
  323. memcpy((char *)dump->regs + EF_SIZE, sw->fp, 32 * 8);
  324. }
  325. /*
  326. * Fill in the user structure for a ELF core dump.
  327. */
  328. void
  329. dump_elf_thread(elf_greg_t *dest, struct pt_regs *pt, struct thread_info *ti)
  330. {
  331. /* switch stack follows right below pt_regs: */
  332. struct switch_stack * sw = ((struct switch_stack *) pt) - 1;
  333. dest[ 0] = pt->r0;
  334. dest[ 1] = pt->r1;
  335. dest[ 2] = pt->r2;
  336. dest[ 3] = pt->r3;
  337. dest[ 4] = pt->r4;
  338. dest[ 5] = pt->r5;
  339. dest[ 6] = pt->r6;
  340. dest[ 7] = pt->r7;
  341. dest[ 8] = pt->r8;
  342. dest[ 9] = sw->r9;
  343. dest[10] = sw->r10;
  344. dest[11] = sw->r11;
  345. dest[12] = sw->r12;
  346. dest[13] = sw->r13;
  347. dest[14] = sw->r14;
  348. dest[15] = sw->r15;
  349. dest[16] = pt->r16;
  350. dest[17] = pt->r17;
  351. dest[18] = pt->r18;
  352. dest[19] = pt->r19;
  353. dest[20] = pt->r20;
  354. dest[21] = pt->r21;
  355. dest[22] = pt->r22;
  356. dest[23] = pt->r23;
  357. dest[24] = pt->r24;
  358. dest[25] = pt->r25;
  359. dest[26] = pt->r26;
  360. dest[27] = pt->r27;
  361. dest[28] = pt->r28;
  362. dest[29] = pt->gp;
  363. dest[30] = rdusp();
  364. dest[31] = pt->pc;
  365. /* Once upon a time this was the PS value. Which is stupid
  366. since that is always 8 for usermode. Usurped for the more
  367. useful value of the thread's UNIQUE field. */
  368. dest[32] = ti->pcb.unique;
  369. }
  370. int
  371. dump_elf_task(elf_greg_t *dest, struct task_struct *task)
  372. {
  373. struct thread_info *ti;
  374. struct pt_regs *pt;
  375. ti = task->thread_info;
  376. pt = (struct pt_regs *)((unsigned long)ti + 2*PAGE_SIZE) - 1;
  377. dump_elf_thread(dest, pt, ti);
  378. return 1;
  379. }
  380. int
  381. dump_elf_task_fp(elf_fpreg_t *dest, struct task_struct *task)
  382. {
  383. struct thread_info *ti;
  384. struct pt_regs *pt;
  385. struct switch_stack *sw;
  386. ti = task->thread_info;
  387. pt = (struct pt_regs *)((unsigned long)ti + 2*PAGE_SIZE) - 1;
  388. sw = (struct switch_stack *)pt - 1;
  389. memcpy(dest, sw->fp, 32 * 8);
  390. return 1;
  391. }
  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(task_t *t)
  425. {
  426. unsigned long base = (unsigned long)t->thread_info;
  427. unsigned long fp, sp = t->thread_info->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 *)p->thread_info->pcb.ksp)[6];
  454. return ((unsigned long *)schedule_frame)[12];
  455. }
  456. return pc;
  457. }