/arch/alpha/kernel/process.c

https://bitbucket.org/evzijst/gittest · C · 528 lines · 354 code · 67 blank · 107 comment · 30 complexity · d387c389f8cc7246eeb6b6f61cdd3fbd MD5 · raw file

  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. EXPORT_SYMBOL(machine_restart);
  144. void
  145. machine_halt(void)
  146. {
  147. common_shutdown(LINUX_REBOOT_CMD_HALT, NULL);
  148. }
  149. EXPORT_SYMBOL(machine_halt);
  150. void
  151. machine_power_off(void)
  152. {
  153. common_shutdown(LINUX_REBOOT_CMD_POWER_OFF, NULL);
  154. }
  155. EXPORT_SYMBOL(machine_power_off);
  156. /* Used by sysrq-p, among others. I don't believe r9-r15 are ever
  157. saved in the context it's used. */
  158. void
  159. show_regs(struct pt_regs *regs)
  160. {
  161. dik_show_regs(regs, NULL);
  162. }
  163. /*
  164. * Re-start a thread when doing execve()
  165. */
  166. void
  167. start_thread(struct pt_regs * regs, unsigned long pc, unsigned long sp)
  168. {
  169. set_fs(USER_DS);
  170. regs->pc = pc;
  171. regs->ps = 8;
  172. wrusp(sp);
  173. }
  174. /*
  175. * Free current thread data structures etc..
  176. */
  177. void
  178. exit_thread(void)
  179. {
  180. }
  181. void
  182. flush_thread(void)
  183. {
  184. /* Arrange for each exec'ed process to start off with a clean slate
  185. with respect to the FPU. This is all exceptions disabled. */
  186. current_thread_info()->ieee_state = 0;
  187. wrfpcr(FPCR_DYN_NORMAL | ieee_swcr_to_fpcr(0));
  188. /* Clean slate for TLS. */
  189. current_thread_info()->pcb.unique = 0;
  190. }
  191. void
  192. release_thread(struct task_struct *dead_task)
  193. {
  194. }
  195. /*
  196. * "alpha_clone()".. By the time we get here, the
  197. * non-volatile registers have also been saved on the
  198. * stack. We do some ugly pointer stuff here.. (see
  199. * also copy_thread)
  200. *
  201. * Notice that "fork()" is implemented in terms of clone,
  202. * with parameters (SIGCHLD, 0).
  203. */
  204. int
  205. alpha_clone(unsigned long clone_flags, unsigned long usp,
  206. int __user *parent_tid, int __user *child_tid,
  207. unsigned long tls_value, struct pt_regs *regs)
  208. {
  209. if (!usp)
  210. usp = rdusp();
  211. return do_fork(clone_flags, usp, regs, 0, parent_tid, child_tid);
  212. }
  213. int
  214. alpha_vfork(struct pt_regs *regs)
  215. {
  216. return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, rdusp(),
  217. regs, 0, NULL, NULL);
  218. }
  219. /*
  220. * Copy an alpha thread..
  221. *
  222. * Note the "stack_offset" stuff: when returning to kernel mode, we need
  223. * to have some extra stack-space for the kernel stack that still exists
  224. * after the "ret_from_fork". When returning to user mode, we only want
  225. * the space needed by the syscall stack frame (ie "struct pt_regs").
  226. * Use the passed "regs" pointer to determine how much space we need
  227. * for a kernel fork().
  228. */
  229. int
  230. copy_thread(int nr, unsigned long clone_flags, unsigned long usp,
  231. unsigned long unused,
  232. struct task_struct * p, struct pt_regs * regs)
  233. {
  234. extern void ret_from_fork(void);
  235. struct thread_info *childti = p->thread_info;
  236. struct pt_regs * childregs;
  237. struct switch_stack * childstack, *stack;
  238. unsigned long stack_offset, settls;
  239. stack_offset = PAGE_SIZE - sizeof(struct pt_regs);
  240. if (!(regs->ps & 8))
  241. stack_offset = (PAGE_SIZE-1) & (unsigned long) regs;
  242. childregs = (struct pt_regs *)
  243. (stack_offset + PAGE_SIZE + (long) childti);
  244. *childregs = *regs;
  245. settls = regs->r20;
  246. childregs->r0 = 0;
  247. childregs->r19 = 0;
  248. childregs->r20 = 1; /* OSF/1 has some strange fork() semantics. */
  249. regs->r20 = 0;
  250. stack = ((struct switch_stack *) regs) - 1;
  251. childstack = ((struct switch_stack *) childregs) - 1;
  252. *childstack = *stack;
  253. childstack->r26 = (unsigned long) ret_from_fork;
  254. childti->pcb.usp = usp;
  255. childti->pcb.ksp = (unsigned long) childstack;
  256. childti->pcb.flags = 1; /* set FEN, clear everything else */
  257. /* Set a new TLS for the child thread? Peek back into the
  258. syscall arguments that we saved on syscall entry. Oops,
  259. except we'd have clobbered it with the parent/child set
  260. of r20. Read the saved copy. */
  261. /* Note: if CLONE_SETTLS is not set, then we must inherit the
  262. value from the parent, which will have been set by the block
  263. copy in dup_task_struct. This is non-intuitive, but is
  264. required for proper operation in the case of a threaded
  265. application calling fork. */
  266. if (clone_flags & CLONE_SETTLS)
  267. childti->pcb.unique = settls;
  268. return 0;
  269. }
  270. /*
  271. * Fill in the user structure for an ECOFF core dump.
  272. */
  273. void
  274. dump_thread(struct pt_regs * pt, struct user * dump)
  275. {
  276. /* switch stack follows right below pt_regs: */
  277. struct switch_stack * sw = ((struct switch_stack *) pt) - 1;
  278. dump->magic = CMAGIC;
  279. dump->start_code = current->mm->start_code;
  280. dump->start_data = current->mm->start_data;
  281. dump->start_stack = rdusp() & ~(PAGE_SIZE - 1);
  282. dump->u_tsize = ((current->mm->end_code - dump->start_code)
  283. >> PAGE_SHIFT);
  284. dump->u_dsize = ((current->mm->brk + PAGE_SIZE-1 - dump->start_data)
  285. >> PAGE_SHIFT);
  286. dump->u_ssize = (current->mm->start_stack - dump->start_stack
  287. + PAGE_SIZE-1) >> PAGE_SHIFT;
  288. /*
  289. * We store the registers in an order/format that is
  290. * compatible with DEC Unix/OSF/1 as this makes life easier
  291. * for gdb.
  292. */
  293. dump->regs[EF_V0] = pt->r0;
  294. dump->regs[EF_T0] = pt->r1;
  295. dump->regs[EF_T1] = pt->r2;
  296. dump->regs[EF_T2] = pt->r3;
  297. dump->regs[EF_T3] = pt->r4;
  298. dump->regs[EF_T4] = pt->r5;
  299. dump->regs[EF_T5] = pt->r6;
  300. dump->regs[EF_T6] = pt->r7;
  301. dump->regs[EF_T7] = pt->r8;
  302. dump->regs[EF_S0] = sw->r9;
  303. dump->regs[EF_S1] = sw->r10;
  304. dump->regs[EF_S2] = sw->r11;
  305. dump->regs[EF_S3] = sw->r12;
  306. dump->regs[EF_S4] = sw->r13;
  307. dump->regs[EF_S5] = sw->r14;
  308. dump->regs[EF_S6] = sw->r15;
  309. dump->regs[EF_A3] = pt->r19;
  310. dump->regs[EF_A4] = pt->r20;
  311. dump->regs[EF_A5] = pt->r21;
  312. dump->regs[EF_T8] = pt->r22;
  313. dump->regs[EF_T9] = pt->r23;
  314. dump->regs[EF_T10] = pt->r24;
  315. dump->regs[EF_T11] = pt->r25;
  316. dump->regs[EF_RA] = pt->r26;
  317. dump->regs[EF_T12] = pt->r27;
  318. dump->regs[EF_AT] = pt->r28;
  319. dump->regs[EF_SP] = rdusp();
  320. dump->regs[EF_PS] = pt->ps;
  321. dump->regs[EF_PC] = pt->pc;
  322. dump->regs[EF_GP] = pt->gp;
  323. dump->regs[EF_A0] = pt->r16;
  324. dump->regs[EF_A1] = pt->r17;
  325. dump->regs[EF_A2] = pt->r18;
  326. memcpy((char *)dump->regs + EF_SIZE, sw->fp, 32 * 8);
  327. }
  328. /*
  329. * Fill in the user structure for a ELF core dump.
  330. */
  331. void
  332. dump_elf_thread(elf_greg_t *dest, struct pt_regs *pt, struct thread_info *ti)
  333. {
  334. /* switch stack follows right below pt_regs: */
  335. struct switch_stack * sw = ((struct switch_stack *) pt) - 1;
  336. dest[ 0] = pt->r0;
  337. dest[ 1] = pt->r1;
  338. dest[ 2] = pt->r2;
  339. dest[ 3] = pt->r3;
  340. dest[ 4] = pt->r4;
  341. dest[ 5] = pt->r5;
  342. dest[ 6] = pt->r6;
  343. dest[ 7] = pt->r7;
  344. dest[ 8] = pt->r8;
  345. dest[ 9] = sw->r9;
  346. dest[10] = sw->r10;
  347. dest[11] = sw->r11;
  348. dest[12] = sw->r12;
  349. dest[13] = sw->r13;
  350. dest[14] = sw->r14;
  351. dest[15] = sw->r15;
  352. dest[16] = pt->r16;
  353. dest[17] = pt->r17;
  354. dest[18] = pt->r18;
  355. dest[19] = pt->r19;
  356. dest[20] = pt->r20;
  357. dest[21] = pt->r21;
  358. dest[22] = pt->r22;
  359. dest[23] = pt->r23;
  360. dest[24] = pt->r24;
  361. dest[25] = pt->r25;
  362. dest[26] = pt->r26;
  363. dest[27] = pt->r27;
  364. dest[28] = pt->r28;
  365. dest[29] = pt->gp;
  366. dest[30] = rdusp();
  367. dest[31] = pt->pc;
  368. /* Once upon a time this was the PS value. Which is stupid
  369. since that is always 8 for usermode. Usurped for the more
  370. useful value of the thread's UNIQUE field. */
  371. dest[32] = ti->pcb.unique;
  372. }
  373. int
  374. dump_elf_task(elf_greg_t *dest, struct task_struct *task)
  375. {
  376. struct thread_info *ti;
  377. struct pt_regs *pt;
  378. ti = task->thread_info;
  379. pt = (struct pt_regs *)((unsigned long)ti + 2*PAGE_SIZE) - 1;
  380. dump_elf_thread(dest, pt, ti);
  381. return 1;
  382. }
  383. int
  384. dump_elf_task_fp(elf_fpreg_t *dest, struct task_struct *task)
  385. {
  386. struct thread_info *ti;
  387. struct pt_regs *pt;
  388. struct switch_stack *sw;
  389. ti = task->thread_info;
  390. pt = (struct pt_regs *)((unsigned long)ti + 2*PAGE_SIZE) - 1;
  391. sw = (struct switch_stack *)pt - 1;
  392. memcpy(dest, sw->fp, 32 * 8);
  393. return 1;
  394. }
  395. /*
  396. * sys_execve() executes a new program.
  397. */
  398. asmlinkage int
  399. do_sys_execve(char __user *ufilename, char __user * __user *argv,
  400. char __user * __user *envp, struct pt_regs *regs)
  401. {
  402. int error;
  403. char *filename;
  404. filename = getname(ufilename);
  405. error = PTR_ERR(filename);
  406. if (IS_ERR(filename))
  407. goto out;
  408. error = do_execve(filename, argv, envp, regs);
  409. putname(filename);
  410. out:
  411. return error;
  412. }
  413. /*
  414. * Return saved PC of a blocked thread. This assumes the frame
  415. * pointer is the 6th saved long on the kernel stack and that the
  416. * saved return address is the first long in the frame. This all
  417. * holds provided the thread blocked through a call to schedule() ($15
  418. * is the frame pointer in schedule() and $15 is saved at offset 48 by
  419. * entry.S:do_switch_stack).
  420. *
  421. * Under heavy swap load I've seen this lose in an ugly way. So do
  422. * some extra sanity checking on the ranges we expect these pointers
  423. * to be in so that we can fail gracefully. This is just for ps after
  424. * all. -- r~
  425. */
  426. unsigned long
  427. thread_saved_pc(task_t *t)
  428. {
  429. unsigned long base = (unsigned long)t->thread_info;
  430. unsigned long fp, sp = t->thread_info->pcb.ksp;
  431. if (sp > base && sp+6*8 < base + 16*1024) {
  432. fp = ((unsigned long*)sp)[6];
  433. if (fp > sp && fp < base + 16*1024)
  434. return *(unsigned long *)fp;
  435. }
  436. return 0;
  437. }
  438. unsigned long
  439. get_wchan(struct task_struct *p)
  440. {
  441. unsigned long schedule_frame;
  442. unsigned long pc;
  443. if (!p || p == current || p->state == TASK_RUNNING)
  444. return 0;
  445. /*
  446. * This one depends on the frame size of schedule(). Do a
  447. * "disass schedule" in gdb to find the frame size. Also, the
  448. * code assumes that sleep_on() follows immediately after
  449. * interruptible_sleep_on() and that add_timer() follows
  450. * immediately after interruptible_sleep(). Ugly, isn't it?
  451. * Maybe adding a wchan field to task_struct would be better,
  452. * after all...
  453. */
  454. pc = thread_saved_pc(p);
  455. if (in_sched_functions(pc)) {
  456. schedule_frame = ((unsigned long *)p->thread_info->pcb.ksp)[6];
  457. return ((unsigned long *)schedule_frame)[12];
  458. }
  459. return pc;
  460. }