process.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  1. /*
  2. * Copyright 2010 Tilera Corporation. All Rights Reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation, version 2.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  11. * NON INFRINGEMENT. See the GNU General Public License for
  12. * more details.
  13. */
  14. #include <linux/sched.h>
  15. #include <linux/sched/debug.h>
  16. #include <linux/sched/task.h>
  17. #include <linux/sched/task_stack.h>
  18. #include <linux/preempt.h>
  19. #include <linux/module.h>
  20. #include <linux/fs.h>
  21. #include <linux/kprobes.h>
  22. #include <linux/elfcore.h>
  23. #include <linux/tick.h>
  24. #include <linux/init.h>
  25. #include <linux/mm.h>
  26. #include <linux/compat.h>
  27. #include <linux/nmi.h>
  28. #include <linux/syscalls.h>
  29. #include <linux/kernel.h>
  30. #include <linux/tracehook.h>
  31. #include <linux/signal.h>
  32. #include <linux/delay.h>
  33. #include <linux/context_tracking.h>
  34. #include <asm/stack.h>
  35. #include <asm/switch_to.h>
  36. #include <asm/homecache.h>
  37. #include <asm/syscalls.h>
  38. #include <asm/traps.h>
  39. #include <asm/setup.h>
  40. #include <linux/uaccess.h>
  41. #ifdef CONFIG_HARDWALL
  42. #include <asm/hardwall.h>
  43. #endif
  44. #include <arch/chip.h>
  45. #include <arch/abi.h>
  46. #include <arch/sim_def.h>
  47. /*
  48. * Use the (x86) "idle=poll" option to prefer low latency when leaving the
  49. * idle loop over low power while in the idle loop, e.g. if we have
  50. * one thread per core and we want to get threads out of futex waits fast.
  51. */
  52. static int __init idle_setup(char *str)
  53. {
  54. if (!str)
  55. return -EINVAL;
  56. if (!strcmp(str, "poll")) {
  57. pr_info("using polling idle threads\n");
  58. cpu_idle_poll_ctrl(true);
  59. return 0;
  60. } else if (!strcmp(str, "halt")) {
  61. return 0;
  62. }
  63. return -1;
  64. }
  65. early_param("idle", idle_setup);
  66. void arch_cpu_idle(void)
  67. {
  68. __this_cpu_write(irq_stat.idle_timestamp, jiffies);
  69. _cpu_idle();
  70. }
  71. /*
  72. * Release a thread_info structure
  73. */
  74. void arch_release_thread_stack(unsigned long *stack)
  75. {
  76. struct thread_info *info = (void *)stack;
  77. struct single_step_state *step_state = info->step_state;
  78. if (step_state) {
  79. /*
  80. * FIXME: we don't munmap step_state->buffer
  81. * because the mm_struct for this process (info->task->mm)
  82. * has already been zeroed in exit_mm(). Keeping a
  83. * reference to it here seems like a bad move, so this
  84. * means we can't munmap() the buffer, and therefore if we
  85. * ptrace multiple threads in a process, we will slowly
  86. * leak user memory. (Note that as soon as the last
  87. * thread in a process dies, we will reclaim all user
  88. * memory including single-step buffers in the usual way.)
  89. * We should either assign a kernel VA to this buffer
  90. * somehow, or we should associate the buffer(s) with the
  91. * mm itself so we can clean them up that way.
  92. */
  93. kfree(step_state);
  94. }
  95. }
  96. static void save_arch_state(struct thread_struct *t);
  97. int copy_thread(unsigned long clone_flags, unsigned long sp,
  98. unsigned long arg, struct task_struct *p)
  99. {
  100. struct pt_regs *childregs = task_pt_regs(p);
  101. unsigned long ksp;
  102. unsigned long *callee_regs;
  103. /*
  104. * Set up the stack and stack pointer appropriately for the
  105. * new child to find itself woken up in __switch_to().
  106. * The callee-saved registers must be on the stack to be read;
  107. * the new task will then jump to assembly support to handle
  108. * calling schedule_tail(), etc., and (for userspace tasks)
  109. * returning to the context set up in the pt_regs.
  110. */
  111. ksp = (unsigned long) childregs;
  112. ksp -= C_ABI_SAVE_AREA_SIZE; /* interrupt-entry save area */
  113. ((long *)ksp)[0] = ((long *)ksp)[1] = 0;
  114. ksp -= CALLEE_SAVED_REGS_COUNT * sizeof(unsigned long);
  115. callee_regs = (unsigned long *)ksp;
  116. ksp -= C_ABI_SAVE_AREA_SIZE; /* __switch_to() save area */
  117. ((long *)ksp)[0] = ((long *)ksp)[1] = 0;
  118. p->thread.ksp = ksp;
  119. /* Record the pid of the task that created this one. */
  120. p->thread.creator_pid = current->pid;
  121. if (unlikely(p->flags & PF_KTHREAD)) {
  122. /* kernel thread */
  123. memset(childregs, 0, sizeof(struct pt_regs));
  124. memset(&callee_regs[2], 0,
  125. (CALLEE_SAVED_REGS_COUNT - 2) * sizeof(unsigned long));
  126. callee_regs[0] = sp; /* r30 = function */
  127. callee_regs[1] = arg; /* r31 = arg */
  128. p->thread.pc = (unsigned long) ret_from_kernel_thread;
  129. return 0;
  130. }
  131. /*
  132. * Start new thread in ret_from_fork so it schedules properly
  133. * and then return from interrupt like the parent.
  134. */
  135. p->thread.pc = (unsigned long) ret_from_fork;
  136. /*
  137. * Do not clone step state from the parent; each thread
  138. * must make its own lazily.
  139. */
  140. task_thread_info(p)->step_state = NULL;
  141. #ifdef __tilegx__
  142. /*
  143. * Do not clone unalign jit fixup from the parent; each thread
  144. * must allocate its own on demand.
  145. */
  146. task_thread_info(p)->unalign_jit_base = NULL;
  147. #endif
  148. /*
  149. * Copy the registers onto the kernel stack so the
  150. * return-from-interrupt code will reload it into registers.
  151. */
  152. *childregs = *current_pt_regs();
  153. childregs->regs[0] = 0; /* return value is zero */
  154. if (sp)
  155. childregs->sp = sp; /* override with new user stack pointer */
  156. memcpy(callee_regs, &childregs->regs[CALLEE_SAVED_FIRST_REG],
  157. CALLEE_SAVED_REGS_COUNT * sizeof(unsigned long));
  158. /* Save user stack top pointer so we can ID the stack vm area later. */
  159. p->thread.usp0 = childregs->sp;
  160. /*
  161. * If CLONE_SETTLS is set, set "tp" in the new task to "r4",
  162. * which is passed in as arg #5 to sys_clone().
  163. */
  164. if (clone_flags & CLONE_SETTLS)
  165. childregs->tp = childregs->regs[4];
  166. #if CHIP_HAS_TILE_DMA()
  167. /*
  168. * No DMA in the new thread. We model this on the fact that
  169. * fork() clears the pending signals, alarms, and aio for the child.
  170. */
  171. memset(&p->thread.tile_dma_state, 0, sizeof(struct tile_dma_state));
  172. memset(&p->thread.dma_async_tlb, 0, sizeof(struct async_tlb));
  173. #endif
  174. /* New thread has its miscellaneous processor state bits clear. */
  175. p->thread.proc_status = 0;
  176. #ifdef CONFIG_HARDWALL
  177. /* New thread does not own any networks. */
  178. memset(&p->thread.hardwall[0], 0,
  179. sizeof(struct hardwall_task) * HARDWALL_TYPES);
  180. #endif
  181. /*
  182. * Start the new thread with the current architecture state
  183. * (user interrupt masks, etc.).
  184. */
  185. save_arch_state(&p->thread);
  186. return 0;
  187. }
  188. int set_unalign_ctl(struct task_struct *tsk, unsigned int val)
  189. {
  190. task_thread_info(tsk)->align_ctl = val;
  191. return 0;
  192. }
  193. int get_unalign_ctl(struct task_struct *tsk, unsigned long adr)
  194. {
  195. return put_user(task_thread_info(tsk)->align_ctl,
  196. (unsigned int __user *)adr);
  197. }
  198. static struct task_struct corrupt_current = { .comm = "<corrupt>" };
  199. /*
  200. * Return "current" if it looks plausible, or else a pointer to a dummy.
  201. * This can be helpful if we are just trying to emit a clean panic.
  202. */
  203. struct task_struct *validate_current(void)
  204. {
  205. struct task_struct *tsk = current;
  206. if (unlikely((unsigned long)tsk < PAGE_OFFSET ||
  207. (high_memory && (void *)tsk > high_memory) ||
  208. ((unsigned long)tsk & (__alignof__(*tsk) - 1)) != 0)) {
  209. pr_err("Corrupt 'current' %p (sp %#lx)\n", tsk, stack_pointer);
  210. tsk = &corrupt_current;
  211. }
  212. return tsk;
  213. }
  214. /* Take and return the pointer to the previous task, for schedule_tail(). */
  215. struct task_struct *sim_notify_fork(struct task_struct *prev)
  216. {
  217. struct task_struct *tsk = current;
  218. __insn_mtspr(SPR_SIM_CONTROL, SIM_CONTROL_OS_FORK_PARENT |
  219. (tsk->thread.creator_pid << _SIM_CONTROL_OPERATOR_BITS));
  220. __insn_mtspr(SPR_SIM_CONTROL, SIM_CONTROL_OS_FORK |
  221. (tsk->pid << _SIM_CONTROL_OPERATOR_BITS));
  222. return prev;
  223. }
  224. int dump_task_regs(struct task_struct *tsk, elf_gregset_t *regs)
  225. {
  226. struct pt_regs *ptregs = task_pt_regs(tsk);
  227. elf_core_copy_regs(regs, ptregs);
  228. return 1;
  229. }
  230. #if CHIP_HAS_TILE_DMA()
  231. /* Allow user processes to access the DMA SPRs */
  232. void grant_dma_mpls(void)
  233. {
  234. #if CONFIG_KERNEL_PL == 2
  235. __insn_mtspr(SPR_MPL_DMA_CPL_SET_1, 1);
  236. __insn_mtspr(SPR_MPL_DMA_NOTIFY_SET_1, 1);
  237. #else
  238. __insn_mtspr(SPR_MPL_DMA_CPL_SET_0, 1);
  239. __insn_mtspr(SPR_MPL_DMA_NOTIFY_SET_0, 1);
  240. #endif
  241. }
  242. /* Forbid user processes from accessing the DMA SPRs */
  243. void restrict_dma_mpls(void)
  244. {
  245. #if CONFIG_KERNEL_PL == 2
  246. __insn_mtspr(SPR_MPL_DMA_CPL_SET_2, 1);
  247. __insn_mtspr(SPR_MPL_DMA_NOTIFY_SET_2, 1);
  248. #else
  249. __insn_mtspr(SPR_MPL_DMA_CPL_SET_1, 1);
  250. __insn_mtspr(SPR_MPL_DMA_NOTIFY_SET_1, 1);
  251. #endif
  252. }
  253. /* Pause the DMA engine, then save off its state registers. */
  254. static void save_tile_dma_state(struct tile_dma_state *dma)
  255. {
  256. unsigned long state = __insn_mfspr(SPR_DMA_USER_STATUS);
  257. unsigned long post_suspend_state;
  258. /* If we're running, suspend the engine. */
  259. if ((state & DMA_STATUS_MASK) == SPR_DMA_STATUS__RUNNING_MASK)
  260. __insn_mtspr(SPR_DMA_CTR, SPR_DMA_CTR__SUSPEND_MASK);
  261. /*
  262. * Wait for the engine to idle, then save regs. Note that we
  263. * want to record the "running" bit from before suspension,
  264. * and the "done" bit from after, so that we can properly
  265. * distinguish a case where the user suspended the engine from
  266. * the case where the kernel suspended as part of the context
  267. * swap.
  268. */
  269. do {
  270. post_suspend_state = __insn_mfspr(SPR_DMA_USER_STATUS);
  271. } while (post_suspend_state & SPR_DMA_STATUS__BUSY_MASK);
  272. dma->src = __insn_mfspr(SPR_DMA_SRC_ADDR);
  273. dma->src_chunk = __insn_mfspr(SPR_DMA_SRC_CHUNK_ADDR);
  274. dma->dest = __insn_mfspr(SPR_DMA_DST_ADDR);
  275. dma->dest_chunk = __insn_mfspr(SPR_DMA_DST_CHUNK_ADDR);
  276. dma->strides = __insn_mfspr(SPR_DMA_STRIDE);
  277. dma->chunk_size = __insn_mfspr(SPR_DMA_CHUNK_SIZE);
  278. dma->byte = __insn_mfspr(SPR_DMA_BYTE);
  279. dma->status = (state & SPR_DMA_STATUS__RUNNING_MASK) |
  280. (post_suspend_state & SPR_DMA_STATUS__DONE_MASK);
  281. }
  282. /* Restart a DMA that was running before we were context-switched out. */
  283. static void restore_tile_dma_state(struct thread_struct *t)
  284. {
  285. const struct tile_dma_state *dma = &t->tile_dma_state;
  286. /*
  287. * The only way to restore the done bit is to run a zero
  288. * length transaction.
  289. */
  290. if ((dma->status & SPR_DMA_STATUS__DONE_MASK) &&
  291. !(__insn_mfspr(SPR_DMA_USER_STATUS) & SPR_DMA_STATUS__DONE_MASK)) {
  292. __insn_mtspr(SPR_DMA_BYTE, 0);
  293. __insn_mtspr(SPR_DMA_CTR, SPR_DMA_CTR__REQUEST_MASK);
  294. while (__insn_mfspr(SPR_DMA_USER_STATUS) &
  295. SPR_DMA_STATUS__BUSY_MASK)
  296. ;
  297. }
  298. __insn_mtspr(SPR_DMA_SRC_ADDR, dma->src);
  299. __insn_mtspr(SPR_DMA_SRC_CHUNK_ADDR, dma->src_chunk);
  300. __insn_mtspr(SPR_DMA_DST_ADDR, dma->dest);
  301. __insn_mtspr(SPR_DMA_DST_CHUNK_ADDR, dma->dest_chunk);
  302. __insn_mtspr(SPR_DMA_STRIDE, dma->strides);
  303. __insn_mtspr(SPR_DMA_CHUNK_SIZE, dma->chunk_size);
  304. __insn_mtspr(SPR_DMA_BYTE, dma->byte);
  305. /*
  306. * Restart the engine if we were running and not done.
  307. * Clear a pending async DMA fault that we were waiting on return
  308. * to user space to execute, since we expect the DMA engine
  309. * to regenerate those faults for us now. Note that we don't
  310. * try to clear the TIF_ASYNC_TLB flag, since it's relatively
  311. * harmless if set, and it covers both DMA and the SN processor.
  312. */
  313. if ((dma->status & DMA_STATUS_MASK) == SPR_DMA_STATUS__RUNNING_MASK) {
  314. t->dma_async_tlb.fault_num = 0;
  315. __insn_mtspr(SPR_DMA_CTR, SPR_DMA_CTR__REQUEST_MASK);
  316. }
  317. }
  318. #endif
  319. static void save_arch_state(struct thread_struct *t)
  320. {
  321. #if CHIP_HAS_SPLIT_INTR_MASK()
  322. t->interrupt_mask = __insn_mfspr(SPR_INTERRUPT_MASK_0_0) |
  323. ((u64)__insn_mfspr(SPR_INTERRUPT_MASK_0_1) << 32);
  324. #else
  325. t->interrupt_mask = __insn_mfspr(SPR_INTERRUPT_MASK_0);
  326. #endif
  327. t->ex_context[0] = __insn_mfspr(SPR_EX_CONTEXT_0_0);
  328. t->ex_context[1] = __insn_mfspr(SPR_EX_CONTEXT_0_1);
  329. t->system_save[0] = __insn_mfspr(SPR_SYSTEM_SAVE_0_0);
  330. t->system_save[1] = __insn_mfspr(SPR_SYSTEM_SAVE_0_1);
  331. t->system_save[2] = __insn_mfspr(SPR_SYSTEM_SAVE_0_2);
  332. t->system_save[3] = __insn_mfspr(SPR_SYSTEM_SAVE_0_3);
  333. t->intctrl_0 = __insn_mfspr(SPR_INTCTRL_0_STATUS);
  334. t->proc_status = __insn_mfspr(SPR_PROC_STATUS);
  335. #if !CHIP_HAS_FIXED_INTVEC_BASE()
  336. t->interrupt_vector_base = __insn_mfspr(SPR_INTERRUPT_VECTOR_BASE_0);
  337. #endif
  338. t->tile_rtf_hwm = __insn_mfspr(SPR_TILE_RTF_HWM);
  339. #if CHIP_HAS_DSTREAM_PF()
  340. t->dstream_pf = __insn_mfspr(SPR_DSTREAM_PF);
  341. #endif
  342. }
  343. static void restore_arch_state(const struct thread_struct *t)
  344. {
  345. #if CHIP_HAS_SPLIT_INTR_MASK()
  346. __insn_mtspr(SPR_INTERRUPT_MASK_0_0, (u32) t->interrupt_mask);
  347. __insn_mtspr(SPR_INTERRUPT_MASK_0_1, t->interrupt_mask >> 32);
  348. #else
  349. __insn_mtspr(SPR_INTERRUPT_MASK_0, t->interrupt_mask);
  350. #endif
  351. __insn_mtspr(SPR_EX_CONTEXT_0_0, t->ex_context[0]);
  352. __insn_mtspr(SPR_EX_CONTEXT_0_1, t->ex_context[1]);
  353. __insn_mtspr(SPR_SYSTEM_SAVE_0_0, t->system_save[0]);
  354. __insn_mtspr(SPR_SYSTEM_SAVE_0_1, t->system_save[1]);
  355. __insn_mtspr(SPR_SYSTEM_SAVE_0_2, t->system_save[2]);
  356. __insn_mtspr(SPR_SYSTEM_SAVE_0_3, t->system_save[3]);
  357. __insn_mtspr(SPR_INTCTRL_0_STATUS, t->intctrl_0);
  358. __insn_mtspr(SPR_PROC_STATUS, t->proc_status);
  359. #if !CHIP_HAS_FIXED_INTVEC_BASE()
  360. __insn_mtspr(SPR_INTERRUPT_VECTOR_BASE_0, t->interrupt_vector_base);
  361. #endif
  362. __insn_mtspr(SPR_TILE_RTF_HWM, t->tile_rtf_hwm);
  363. #if CHIP_HAS_DSTREAM_PF()
  364. __insn_mtspr(SPR_DSTREAM_PF, t->dstream_pf);
  365. #endif
  366. }
  367. void _prepare_arch_switch(struct task_struct *next)
  368. {
  369. #if CHIP_HAS_TILE_DMA()
  370. struct tile_dma_state *dma = &current->thread.tile_dma_state;
  371. if (dma->enabled)
  372. save_tile_dma_state(dma);
  373. #endif
  374. }
  375. struct task_struct *__sched _switch_to(struct task_struct *prev,
  376. struct task_struct *next)
  377. {
  378. /* DMA state is already saved; save off other arch state. */
  379. save_arch_state(&prev->thread);
  380. #if CHIP_HAS_TILE_DMA()
  381. /*
  382. * Restore DMA in new task if desired.
  383. * Note that it is only safe to restart here since interrupts
  384. * are disabled, so we can't take any DMATLB miss or access
  385. * interrupts before we have finished switching stacks.
  386. */
  387. if (next->thread.tile_dma_state.enabled) {
  388. restore_tile_dma_state(&next->thread);
  389. grant_dma_mpls();
  390. } else {
  391. restrict_dma_mpls();
  392. }
  393. #endif
  394. /* Restore other arch state. */
  395. restore_arch_state(&next->thread);
  396. #ifdef CONFIG_HARDWALL
  397. /* Enable or disable access to the network registers appropriately. */
  398. hardwall_switch_tasks(prev, next);
  399. #endif
  400. /* Notify the simulator of task exit. */
  401. if (unlikely(prev->state == TASK_DEAD))
  402. __insn_mtspr(SPR_SIM_CONTROL, SIM_CONTROL_OS_EXIT |
  403. (prev->pid << _SIM_CONTROL_OPERATOR_BITS));
  404. /*
  405. * Switch kernel SP, PC, and callee-saved registers.
  406. * In the context of the new task, return the old task pointer
  407. * (i.e. the task that actually called __switch_to).
  408. * Pass the value to use for SYSTEM_SAVE_K_0 when we reset our sp.
  409. */
  410. return __switch_to(prev, next, next_current_ksp0(next));
  411. }
  412. /*
  413. * This routine is called on return from interrupt if any of the
  414. * TIF_ALLWORK_MASK flags are set in thread_info->flags. It is
  415. * entered with interrupts disabled so we don't miss an event that
  416. * modified the thread_info flags. We loop until all the tested flags
  417. * are clear. Note that the function is called on certain conditions
  418. * that are not listed in the loop condition here (e.g. SINGLESTEP)
  419. * which guarantees we will do those things once, and redo them if any
  420. * of the other work items is re-done, but won't continue looping if
  421. * all the other work is done.
  422. */
  423. void prepare_exit_to_usermode(struct pt_regs *regs, u32 thread_info_flags)
  424. {
  425. if (WARN_ON(!user_mode(regs)))
  426. return;
  427. do {
  428. local_irq_enable();
  429. if (thread_info_flags & _TIF_NEED_RESCHED)
  430. schedule();
  431. #if CHIP_HAS_TILE_DMA()
  432. if (thread_info_flags & _TIF_ASYNC_TLB)
  433. do_async_page_fault(regs);
  434. #endif
  435. if (thread_info_flags & _TIF_SIGPENDING)
  436. do_signal(regs);
  437. if (thread_info_flags & _TIF_NOTIFY_RESUME) {
  438. clear_thread_flag(TIF_NOTIFY_RESUME);
  439. tracehook_notify_resume(regs);
  440. }
  441. local_irq_disable();
  442. thread_info_flags = READ_ONCE(current_thread_info()->flags);
  443. } while (thread_info_flags & _TIF_WORK_MASK);
  444. if (thread_info_flags & _TIF_SINGLESTEP) {
  445. single_step_once(regs);
  446. #ifndef __tilegx__
  447. /*
  448. * FIXME: on tilepro, since we enable interrupts in
  449. * this routine, it's possible that we miss a signal
  450. * or other asynchronous event.
  451. */
  452. local_irq_disable();
  453. #endif
  454. }
  455. user_enter();
  456. }
  457. unsigned long get_wchan(struct task_struct *p)
  458. {
  459. struct KBacktraceIterator kbt;
  460. if (!p || p == current || p->state == TASK_RUNNING)
  461. return 0;
  462. for (KBacktraceIterator_init(&kbt, p, NULL);
  463. !KBacktraceIterator_end(&kbt);
  464. KBacktraceIterator_next(&kbt)) {
  465. if (!in_sched_functions(kbt.it.pc))
  466. return kbt.it.pc;
  467. }
  468. return 0;
  469. }
  470. /* Flush thread state. */
  471. void flush_thread(void)
  472. {
  473. /* Nothing */
  474. }
  475. /*
  476. * Free current thread data structures etc..
  477. */
  478. void exit_thread(struct task_struct *tsk)
  479. {
  480. #ifdef CONFIG_HARDWALL
  481. /*
  482. * Remove the task from the list of tasks that are associated
  483. * with any live hardwalls. (If the task that is exiting held
  484. * the last reference to a hardwall fd, it would already have
  485. * been released and deactivated at this point.)
  486. */
  487. hardwall_deactivate_all(tsk);
  488. #endif
  489. }
  490. void tile_show_regs(struct pt_regs *regs)
  491. {
  492. int i;
  493. #ifdef __tilegx__
  494. for (i = 0; i < 17; i++)
  495. pr_err(" r%-2d: "REGFMT" r%-2d: "REGFMT" r%-2d: "REGFMT"\n",
  496. i, regs->regs[i], i+18, regs->regs[i+18],
  497. i+36, regs->regs[i+36]);
  498. pr_err(" r17: "REGFMT" r35: "REGFMT" tp : "REGFMT"\n",
  499. regs->regs[17], regs->regs[35], regs->tp);
  500. pr_err(" sp : "REGFMT" lr : "REGFMT"\n", regs->sp, regs->lr);
  501. #else
  502. for (i = 0; i < 13; i++)
  503. pr_err(" r%-2d: "REGFMT" r%-2d: "REGFMT
  504. " r%-2d: "REGFMT" r%-2d: "REGFMT"\n",
  505. i, regs->regs[i], i+14, regs->regs[i+14],
  506. i+27, regs->regs[i+27], i+40, regs->regs[i+40]);
  507. pr_err(" r13: "REGFMT" tp : "REGFMT" sp : "REGFMT" lr : "REGFMT"\n",
  508. regs->regs[13], regs->tp, regs->sp, regs->lr);
  509. #endif
  510. pr_err(" pc : "REGFMT" ex1: %ld faultnum: %ld flags:%s%s%s%s\n",
  511. regs->pc, regs->ex1, regs->faultnum,
  512. is_compat_task() ? " compat" : "",
  513. (regs->flags & PT_FLAGS_DISABLE_IRQ) ? " noirq" : "",
  514. !(regs->flags & PT_FLAGS_CALLER_SAVES) ? " nocallersave" : "",
  515. (regs->flags & PT_FLAGS_RESTORE_REGS) ? " restoreregs" : "");
  516. }
  517. void show_regs(struct pt_regs *regs)
  518. {
  519. struct KBacktraceIterator kbt;
  520. show_regs_print_info(KERN_DEFAULT);
  521. tile_show_regs(regs);
  522. KBacktraceIterator_init(&kbt, NULL, regs);
  523. tile_show_stack(&kbt);
  524. }
  525. #ifdef __tilegx__
  526. void nmi_raise_cpu_backtrace(struct cpumask *in_mask)
  527. {
  528. struct cpumask mask;
  529. HV_Coord tile;
  530. unsigned int timeout;
  531. int cpu;
  532. HV_NMI_Info info[NR_CPUS];
  533. /* Tentatively dump stack on remote tiles via NMI. */
  534. timeout = 100;
  535. cpumask_copy(&mask, in_mask);
  536. while (!cpumask_empty(&mask) && timeout) {
  537. for_each_cpu(cpu, &mask) {
  538. tile.x = cpu_x(cpu);
  539. tile.y = cpu_y(cpu);
  540. info[cpu] = hv_send_nmi(tile, TILE_NMI_DUMP_STACK, 0);
  541. if (info[cpu].result == HV_NMI_RESULT_OK)
  542. cpumask_clear_cpu(cpu, &mask);
  543. }
  544. mdelay(10);
  545. touch_softlockup_watchdog();
  546. timeout--;
  547. }
  548. /* Warn about cpus stuck in ICS. */
  549. if (!cpumask_empty(&mask)) {
  550. for_each_cpu(cpu, &mask) {
  551. /* Clear the bit as if nmi_cpu_backtrace() ran. */
  552. cpumask_clear_cpu(cpu, in_mask);
  553. switch (info[cpu].result) {
  554. case HV_NMI_RESULT_FAIL_ICS:
  555. pr_warn("Skipping stack dump of cpu %d in ICS at pc %#llx\n",
  556. cpu, info[cpu].pc);
  557. break;
  558. case HV_NMI_RESULT_FAIL_HV:
  559. pr_warn("Skipping stack dump of cpu %d in hypervisor\n",
  560. cpu);
  561. break;
  562. case HV_ENOSYS:
  563. WARN_ONCE(1, "Hypervisor too old to allow remote stack dumps.\n");
  564. break;
  565. default: /* should not happen */
  566. pr_warn("Skipping stack dump of cpu %d [%d,%#llx]\n",
  567. cpu, info[cpu].result, info[cpu].pc);
  568. break;
  569. }
  570. }
  571. }
  572. }
  573. void arch_trigger_cpumask_backtrace(const cpumask_t *mask, bool exclude_self)
  574. {
  575. nmi_trigger_cpumask_backtrace(mask, exclude_self,
  576. nmi_raise_cpu_backtrace);
  577. }
  578. #endif /* __tilegx_ */