process_64.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. /*
  2. * arch/sh/kernel/process_64.c
  3. *
  4. * This file handles the architecture-dependent parts of process handling..
  5. *
  6. * Copyright (C) 2000, 2001 Paolo Alberelli
  7. * Copyright (C) 2003 - 2007 Paul Mundt
  8. * Copyright (C) 2003, 2004 Richard Curnow
  9. *
  10. * Started from SH3/4 version:
  11. * Copyright (C) 1999, 2000 Niibe Yutaka & Kaz Kojima
  12. *
  13. * In turn started from i386 version:
  14. * Copyright (C) 1995 Linus Torvalds
  15. *
  16. * This file is subject to the terms and conditions of the GNU General Public
  17. * License. See the file "COPYING" in the main directory of this archive
  18. * for more details.
  19. */
  20. #include <linux/mm.h>
  21. #include <linux/fs.h>
  22. #include <linux/ptrace.h>
  23. #include <linux/reboot.h>
  24. #include <linux/slab.h>
  25. #include <linux/init.h>
  26. #include <linux/module.h>
  27. #include <linux/io.h>
  28. #include <asm/syscalls.h>
  29. #include <asm/uaccess.h>
  30. #include <asm/pgtable.h>
  31. #include <asm/mmu_context.h>
  32. #include <asm/fpu.h>
  33. #include <asm/switch_to.h>
  34. struct task_struct *last_task_used_math = NULL;
  35. struct pt_regs fake_swapper_regs = { 0, };
  36. void show_regs(struct pt_regs *regs)
  37. {
  38. unsigned long long ah, al, bh, bl, ch, cl;
  39. printk("\n");
  40. show_regs_print_info(KERN_DEFAULT);
  41. ah = (regs->pc) >> 32;
  42. al = (regs->pc) & 0xffffffff;
  43. bh = (regs->regs[18]) >> 32;
  44. bl = (regs->regs[18]) & 0xffffffff;
  45. ch = (regs->regs[15]) >> 32;
  46. cl = (regs->regs[15]) & 0xffffffff;
  47. printk("PC : %08Lx%08Lx LINK: %08Lx%08Lx SP : %08Lx%08Lx\n",
  48. ah, al, bh, bl, ch, cl);
  49. ah = (regs->sr) >> 32;
  50. al = (regs->sr) & 0xffffffff;
  51. asm volatile ("getcon " __TEA ", %0" : "=r" (bh));
  52. asm volatile ("getcon " __TEA ", %0" : "=r" (bl));
  53. bh = (bh) >> 32;
  54. bl = (bl) & 0xffffffff;
  55. asm volatile ("getcon " __KCR0 ", %0" : "=r" (ch));
  56. asm volatile ("getcon " __KCR0 ", %0" : "=r" (cl));
  57. ch = (ch) >> 32;
  58. cl = (cl) & 0xffffffff;
  59. printk("SR : %08Lx%08Lx TEA : %08Lx%08Lx KCR0: %08Lx%08Lx\n",
  60. ah, al, bh, bl, ch, cl);
  61. ah = (regs->regs[0]) >> 32;
  62. al = (regs->regs[0]) & 0xffffffff;
  63. bh = (regs->regs[1]) >> 32;
  64. bl = (regs->regs[1]) & 0xffffffff;
  65. ch = (regs->regs[2]) >> 32;
  66. cl = (regs->regs[2]) & 0xffffffff;
  67. printk("R0 : %08Lx%08Lx R1 : %08Lx%08Lx R2 : %08Lx%08Lx\n",
  68. ah, al, bh, bl, ch, cl);
  69. ah = (regs->regs[3]) >> 32;
  70. al = (regs->regs[3]) & 0xffffffff;
  71. bh = (regs->regs[4]) >> 32;
  72. bl = (regs->regs[4]) & 0xffffffff;
  73. ch = (regs->regs[5]) >> 32;
  74. cl = (regs->regs[5]) & 0xffffffff;
  75. printk("R3 : %08Lx%08Lx R4 : %08Lx%08Lx R5 : %08Lx%08Lx\n",
  76. ah, al, bh, bl, ch, cl);
  77. ah = (regs->regs[6]) >> 32;
  78. al = (regs->regs[6]) & 0xffffffff;
  79. bh = (regs->regs[7]) >> 32;
  80. bl = (regs->regs[7]) & 0xffffffff;
  81. ch = (regs->regs[8]) >> 32;
  82. cl = (regs->regs[8]) & 0xffffffff;
  83. printk("R6 : %08Lx%08Lx R7 : %08Lx%08Lx R8 : %08Lx%08Lx\n",
  84. ah, al, bh, bl, ch, cl);
  85. ah = (regs->regs[9]) >> 32;
  86. al = (regs->regs[9]) & 0xffffffff;
  87. bh = (regs->regs[10]) >> 32;
  88. bl = (regs->regs[10]) & 0xffffffff;
  89. ch = (regs->regs[11]) >> 32;
  90. cl = (regs->regs[11]) & 0xffffffff;
  91. printk("R9 : %08Lx%08Lx R10 : %08Lx%08Lx R11 : %08Lx%08Lx\n",
  92. ah, al, bh, bl, ch, cl);
  93. ah = (regs->regs[12]) >> 32;
  94. al = (regs->regs[12]) & 0xffffffff;
  95. bh = (regs->regs[13]) >> 32;
  96. bl = (regs->regs[13]) & 0xffffffff;
  97. ch = (regs->regs[14]) >> 32;
  98. cl = (regs->regs[14]) & 0xffffffff;
  99. printk("R12 : %08Lx%08Lx R13 : %08Lx%08Lx R14 : %08Lx%08Lx\n",
  100. ah, al, bh, bl, ch, cl);
  101. ah = (regs->regs[16]) >> 32;
  102. al = (regs->regs[16]) & 0xffffffff;
  103. bh = (regs->regs[17]) >> 32;
  104. bl = (regs->regs[17]) & 0xffffffff;
  105. ch = (regs->regs[19]) >> 32;
  106. cl = (regs->regs[19]) & 0xffffffff;
  107. printk("R16 : %08Lx%08Lx R17 : %08Lx%08Lx R19 : %08Lx%08Lx\n",
  108. ah, al, bh, bl, ch, cl);
  109. ah = (regs->regs[20]) >> 32;
  110. al = (regs->regs[20]) & 0xffffffff;
  111. bh = (regs->regs[21]) >> 32;
  112. bl = (regs->regs[21]) & 0xffffffff;
  113. ch = (regs->regs[22]) >> 32;
  114. cl = (regs->regs[22]) & 0xffffffff;
  115. printk("R20 : %08Lx%08Lx R21 : %08Lx%08Lx R22 : %08Lx%08Lx\n",
  116. ah, al, bh, bl, ch, cl);
  117. ah = (regs->regs[23]) >> 32;
  118. al = (regs->regs[23]) & 0xffffffff;
  119. bh = (regs->regs[24]) >> 32;
  120. bl = (regs->regs[24]) & 0xffffffff;
  121. ch = (regs->regs[25]) >> 32;
  122. cl = (regs->regs[25]) & 0xffffffff;
  123. printk("R23 : %08Lx%08Lx R24 : %08Lx%08Lx R25 : %08Lx%08Lx\n",
  124. ah, al, bh, bl, ch, cl);
  125. ah = (regs->regs[26]) >> 32;
  126. al = (regs->regs[26]) & 0xffffffff;
  127. bh = (regs->regs[27]) >> 32;
  128. bl = (regs->regs[27]) & 0xffffffff;
  129. ch = (regs->regs[28]) >> 32;
  130. cl = (regs->regs[28]) & 0xffffffff;
  131. printk("R26 : %08Lx%08Lx R27 : %08Lx%08Lx R28 : %08Lx%08Lx\n",
  132. ah, al, bh, bl, ch, cl);
  133. ah = (regs->regs[29]) >> 32;
  134. al = (regs->regs[29]) & 0xffffffff;
  135. bh = (regs->regs[30]) >> 32;
  136. bl = (regs->regs[30]) & 0xffffffff;
  137. ch = (regs->regs[31]) >> 32;
  138. cl = (regs->regs[31]) & 0xffffffff;
  139. printk("R29 : %08Lx%08Lx R30 : %08Lx%08Lx R31 : %08Lx%08Lx\n",
  140. ah, al, bh, bl, ch, cl);
  141. ah = (regs->regs[32]) >> 32;
  142. al = (regs->regs[32]) & 0xffffffff;
  143. bh = (regs->regs[33]) >> 32;
  144. bl = (regs->regs[33]) & 0xffffffff;
  145. ch = (regs->regs[34]) >> 32;
  146. cl = (regs->regs[34]) & 0xffffffff;
  147. printk("R32 : %08Lx%08Lx R33 : %08Lx%08Lx R34 : %08Lx%08Lx\n",
  148. ah, al, bh, bl, ch, cl);
  149. ah = (regs->regs[35]) >> 32;
  150. al = (regs->regs[35]) & 0xffffffff;
  151. bh = (regs->regs[36]) >> 32;
  152. bl = (regs->regs[36]) & 0xffffffff;
  153. ch = (regs->regs[37]) >> 32;
  154. cl = (regs->regs[37]) & 0xffffffff;
  155. printk("R35 : %08Lx%08Lx R36 : %08Lx%08Lx R37 : %08Lx%08Lx\n",
  156. ah, al, bh, bl, ch, cl);
  157. ah = (regs->regs[38]) >> 32;
  158. al = (regs->regs[38]) & 0xffffffff;
  159. bh = (regs->regs[39]) >> 32;
  160. bl = (regs->regs[39]) & 0xffffffff;
  161. ch = (regs->regs[40]) >> 32;
  162. cl = (regs->regs[40]) & 0xffffffff;
  163. printk("R38 : %08Lx%08Lx R39 : %08Lx%08Lx R40 : %08Lx%08Lx\n",
  164. ah, al, bh, bl, ch, cl);
  165. ah = (regs->regs[41]) >> 32;
  166. al = (regs->regs[41]) & 0xffffffff;
  167. bh = (regs->regs[42]) >> 32;
  168. bl = (regs->regs[42]) & 0xffffffff;
  169. ch = (regs->regs[43]) >> 32;
  170. cl = (regs->regs[43]) & 0xffffffff;
  171. printk("R41 : %08Lx%08Lx R42 : %08Lx%08Lx R43 : %08Lx%08Lx\n",
  172. ah, al, bh, bl, ch, cl);
  173. ah = (regs->regs[44]) >> 32;
  174. al = (regs->regs[44]) & 0xffffffff;
  175. bh = (regs->regs[45]) >> 32;
  176. bl = (regs->regs[45]) & 0xffffffff;
  177. ch = (regs->regs[46]) >> 32;
  178. cl = (regs->regs[46]) & 0xffffffff;
  179. printk("R44 : %08Lx%08Lx R45 : %08Lx%08Lx R46 : %08Lx%08Lx\n",
  180. ah, al, bh, bl, ch, cl);
  181. ah = (regs->regs[47]) >> 32;
  182. al = (regs->regs[47]) & 0xffffffff;
  183. bh = (regs->regs[48]) >> 32;
  184. bl = (regs->regs[48]) & 0xffffffff;
  185. ch = (regs->regs[49]) >> 32;
  186. cl = (regs->regs[49]) & 0xffffffff;
  187. printk("R47 : %08Lx%08Lx R48 : %08Lx%08Lx R49 : %08Lx%08Lx\n",
  188. ah, al, bh, bl, ch, cl);
  189. ah = (regs->regs[50]) >> 32;
  190. al = (regs->regs[50]) & 0xffffffff;
  191. bh = (regs->regs[51]) >> 32;
  192. bl = (regs->regs[51]) & 0xffffffff;
  193. ch = (regs->regs[52]) >> 32;
  194. cl = (regs->regs[52]) & 0xffffffff;
  195. printk("R50 : %08Lx%08Lx R51 : %08Lx%08Lx R52 : %08Lx%08Lx\n",
  196. ah, al, bh, bl, ch, cl);
  197. ah = (regs->regs[53]) >> 32;
  198. al = (regs->regs[53]) & 0xffffffff;
  199. bh = (regs->regs[54]) >> 32;
  200. bl = (regs->regs[54]) & 0xffffffff;
  201. ch = (regs->regs[55]) >> 32;
  202. cl = (regs->regs[55]) & 0xffffffff;
  203. printk("R53 : %08Lx%08Lx R54 : %08Lx%08Lx R55 : %08Lx%08Lx\n",
  204. ah, al, bh, bl, ch, cl);
  205. ah = (regs->regs[56]) >> 32;
  206. al = (regs->regs[56]) & 0xffffffff;
  207. bh = (regs->regs[57]) >> 32;
  208. bl = (regs->regs[57]) & 0xffffffff;
  209. ch = (regs->regs[58]) >> 32;
  210. cl = (regs->regs[58]) & 0xffffffff;
  211. printk("R56 : %08Lx%08Lx R57 : %08Lx%08Lx R58 : %08Lx%08Lx\n",
  212. ah, al, bh, bl, ch, cl);
  213. ah = (regs->regs[59]) >> 32;
  214. al = (regs->regs[59]) & 0xffffffff;
  215. bh = (regs->regs[60]) >> 32;
  216. bl = (regs->regs[60]) & 0xffffffff;
  217. ch = (regs->regs[61]) >> 32;
  218. cl = (regs->regs[61]) & 0xffffffff;
  219. printk("R59 : %08Lx%08Lx R60 : %08Lx%08Lx R61 : %08Lx%08Lx\n",
  220. ah, al, bh, bl, ch, cl);
  221. ah = (regs->regs[62]) >> 32;
  222. al = (regs->regs[62]) & 0xffffffff;
  223. bh = (regs->tregs[0]) >> 32;
  224. bl = (regs->tregs[0]) & 0xffffffff;
  225. ch = (regs->tregs[1]) >> 32;
  226. cl = (regs->tregs[1]) & 0xffffffff;
  227. printk("R62 : %08Lx%08Lx T0 : %08Lx%08Lx T1 : %08Lx%08Lx\n",
  228. ah, al, bh, bl, ch, cl);
  229. ah = (regs->tregs[2]) >> 32;
  230. al = (regs->tregs[2]) & 0xffffffff;
  231. bh = (regs->tregs[3]) >> 32;
  232. bl = (regs->tregs[3]) & 0xffffffff;
  233. ch = (regs->tregs[4]) >> 32;
  234. cl = (regs->tregs[4]) & 0xffffffff;
  235. printk("T2 : %08Lx%08Lx T3 : %08Lx%08Lx T4 : %08Lx%08Lx\n",
  236. ah, al, bh, bl, ch, cl);
  237. ah = (regs->tregs[5]) >> 32;
  238. al = (regs->tregs[5]) & 0xffffffff;
  239. bh = (regs->tregs[6]) >> 32;
  240. bl = (regs->tregs[6]) & 0xffffffff;
  241. ch = (regs->tregs[7]) >> 32;
  242. cl = (regs->tregs[7]) & 0xffffffff;
  243. printk("T5 : %08Lx%08Lx T6 : %08Lx%08Lx T7 : %08Lx%08Lx\n",
  244. ah, al, bh, bl, ch, cl);
  245. /*
  246. * If we're in kernel mode, dump the stack too..
  247. */
  248. if (!user_mode(regs)) {
  249. void show_stack(struct task_struct *tsk, unsigned long *sp);
  250. unsigned long sp = regs->regs[15] & 0xffffffff;
  251. struct task_struct *tsk = get_current();
  252. tsk->thread.kregs = regs;
  253. show_stack(tsk, (unsigned long *)sp);
  254. }
  255. }
  256. /*
  257. * Free current thread data structures etc..
  258. */
  259. void exit_thread(void)
  260. {
  261. /*
  262. * See arch/sparc/kernel/process.c for the precedent for doing
  263. * this -- RPC.
  264. *
  265. * The SH-5 FPU save/restore approach relies on
  266. * last_task_used_math pointing to a live task_struct. When
  267. * another task tries to use the FPU for the 1st time, the FPUDIS
  268. * trap handling (see arch/sh/kernel/cpu/sh5/fpu.c) will save the
  269. * existing FPU state to the FP regs field within
  270. * last_task_used_math before re-loading the new task's FPU state
  271. * (or initialising it if the FPU has been used before). So if
  272. * last_task_used_math is stale, and its page has already been
  273. * re-allocated for another use, the consequences are rather
  274. * grim. Unless we null it here, there is no other path through
  275. * which it would get safely nulled.
  276. */
  277. #ifdef CONFIG_SH_FPU
  278. if (last_task_used_math == current) {
  279. last_task_used_math = NULL;
  280. }
  281. #endif
  282. }
  283. void flush_thread(void)
  284. {
  285. /* Called by fs/exec.c (setup_new_exec) to remove traces of a
  286. * previously running executable. */
  287. #ifdef CONFIG_SH_FPU
  288. if (last_task_used_math == current) {
  289. last_task_used_math = NULL;
  290. }
  291. /* Force FPU state to be reinitialised after exec */
  292. clear_used_math();
  293. #endif
  294. /* if we are a kernel thread, about to change to user thread,
  295. * update kreg
  296. */
  297. if(current->thread.kregs==&fake_swapper_regs) {
  298. current->thread.kregs =
  299. ((struct pt_regs *)(THREAD_SIZE + (unsigned long) current) - 1);
  300. current->thread.uregs = current->thread.kregs;
  301. }
  302. }
  303. void release_thread(struct task_struct *dead_task)
  304. {
  305. /* do nothing */
  306. }
  307. /* Fill in the fpu structure for a core dump.. */
  308. int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpu)
  309. {
  310. #ifdef CONFIG_SH_FPU
  311. int fpvalid;
  312. struct task_struct *tsk = current;
  313. fpvalid = !!tsk_used_math(tsk);
  314. if (fpvalid) {
  315. if (current == last_task_used_math) {
  316. enable_fpu();
  317. save_fpu(tsk);
  318. disable_fpu();
  319. last_task_used_math = 0;
  320. regs->sr |= SR_FD;
  321. }
  322. memcpy(fpu, &tsk->thread.xstate->hardfpu, sizeof(*fpu));
  323. }
  324. return fpvalid;
  325. #else
  326. return 0; /* Task didn't use the fpu at all. */
  327. #endif
  328. }
  329. EXPORT_SYMBOL(dump_fpu);
  330. asmlinkage void ret_from_fork(void);
  331. asmlinkage void ret_from_kernel_thread(void);
  332. int copy_thread(unsigned long clone_flags, unsigned long usp,
  333. unsigned long arg, struct task_struct *p)
  334. {
  335. struct pt_regs *childregs;
  336. #ifdef CONFIG_SH_FPU
  337. /* can't happen for a kernel thread */
  338. if (last_task_used_math == current) {
  339. enable_fpu();
  340. save_fpu(current);
  341. disable_fpu();
  342. last_task_used_math = NULL;
  343. current_pt_regs()->sr |= SR_FD;
  344. }
  345. #endif
  346. /* Copy from sh version */
  347. childregs = (struct pt_regs *)(THREAD_SIZE + task_stack_page(p)) - 1;
  348. p->thread.sp = (unsigned long) childregs;
  349. if (unlikely(p->flags & PF_KTHREAD)) {
  350. memset(childregs, 0, sizeof(struct pt_regs));
  351. childregs->regs[2] = (unsigned long)arg;
  352. childregs->regs[3] = (unsigned long)usp;
  353. childregs->sr = (1 << 30); /* not user_mode */
  354. childregs->sr |= SR_FD; /* Invalidate FPU flag */
  355. p->thread.pc = (unsigned long) ret_from_kernel_thread;
  356. return 0;
  357. }
  358. *childregs = *current_pt_regs();
  359. /*
  360. * Sign extend the edited stack.
  361. * Note that thread.pc and thread.pc will stay
  362. * 32-bit wide and context switch must take care
  363. * of NEFF sign extension.
  364. */
  365. if (usp)
  366. childregs->regs[15] = neff_sign_extend(usp);
  367. p->thread.uregs = childregs;
  368. childregs->regs[9] = 0; /* Set return value for child */
  369. childregs->sr |= SR_FD; /* Invalidate FPU flag */
  370. p->thread.pc = (unsigned long) ret_from_fork;
  371. return 0;
  372. }
  373. #ifdef CONFIG_FRAME_POINTER
  374. static int in_sh64_switch_to(unsigned long pc)
  375. {
  376. extern char __sh64_switch_to_end;
  377. /* For a sleeping task, the PC is somewhere in the middle of the function,
  378. so we don't have to worry about masking the LSB off */
  379. return (pc >= (unsigned long) sh64_switch_to) &&
  380. (pc < (unsigned long) &__sh64_switch_to_end);
  381. }
  382. #endif
  383. unsigned long get_wchan(struct task_struct *p)
  384. {
  385. unsigned long pc;
  386. if (!p || p == current || p->state == TASK_RUNNING)
  387. return 0;
  388. /*
  389. * The same comment as on the Alpha applies here, too ...
  390. */
  391. pc = thread_saved_pc(p);
  392. #ifdef CONFIG_FRAME_POINTER
  393. if (in_sh64_switch_to(pc)) {
  394. unsigned long schedule_fp;
  395. unsigned long sh64_switch_to_fp;
  396. unsigned long schedule_caller_pc;
  397. sh64_switch_to_fp = (long) p->thread.sp;
  398. /* r14 is saved at offset 4 in the sh64_switch_to frame */
  399. schedule_fp = *(unsigned long *) (long)(sh64_switch_to_fp + 4);
  400. /* and the caller of 'schedule' is (currently!) saved at offset 24
  401. in the frame of schedule (from disasm) */
  402. schedule_caller_pc = *(unsigned long *) (long)(schedule_fp + 24);
  403. return schedule_caller_pc;
  404. }
  405. #endif
  406. return pc;
  407. }