process.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  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/preempt.h>
  16. #include <linux/module.h>
  17. #include <linux/fs.h>
  18. #include <linux/kprobes.h>
  19. #include <linux/elfcore.h>
  20. #include <linux/tick.h>
  21. #include <linux/init.h>
  22. #include <linux/mm.h>
  23. #include <linux/compat.h>
  24. #include <linux/hardirq.h>
  25. #include <linux/syscalls.h>
  26. #include <linux/kernel.h>
  27. #include <linux/tracehook.h>
  28. #include <linux/signal.h>
  29. #include <asm/stack.h>
  30. #include <asm/homecache.h>
  31. #include <asm/syscalls.h>
  32. #include <asm/traps.h>
  33. #include <asm/setup.h>
  34. #ifdef CONFIG_HARDWALL
  35. #include <asm/hardwall.h>
  36. #endif
  37. #include <arch/chip.h>
  38. #include <arch/abi.h>
  39. #include <arch/sim_def.h>
  40. /*
  41. * Use the (x86) "idle=poll" option to prefer low latency when leaving the
  42. * idle loop over low power while in the idle loop, e.g. if we have
  43. * one thread per core and we want to get threads out of futex waits fast.
  44. */
  45. static int no_idle_nap;
  46. static int __init idle_setup(char *str)
  47. {
  48. if (!str)
  49. return -EINVAL;
  50. if (!strcmp(str, "poll")) {
  51. pr_info("using polling idle threads.\n");
  52. no_idle_nap = 1;
  53. } else if (!strcmp(str, "halt"))
  54. no_idle_nap = 0;
  55. else
  56. return -1;
  57. return 0;
  58. }
  59. early_param("idle", idle_setup);
  60. /*
  61. * The idle thread. There's no useful work to be
  62. * done, so just try to conserve power and have a
  63. * low exit latency (ie sit in a loop waiting for
  64. * somebody to say that they'd like to reschedule)
  65. */
  66. void cpu_idle(void)
  67. {
  68. int cpu = smp_processor_id();
  69. current_thread_info()->status |= TS_POLLING;
  70. if (no_idle_nap) {
  71. while (1) {
  72. while (!need_resched())
  73. cpu_relax();
  74. schedule();
  75. }
  76. }
  77. /* endless idle loop with no priority at all */
  78. while (1) {
  79. tick_nohz_idle_enter();
  80. rcu_idle_enter();
  81. while (!need_resched()) {
  82. if (cpu_is_offline(cpu))
  83. BUG(); /* no HOTPLUG_CPU */
  84. local_irq_disable();
  85. __get_cpu_var(irq_stat).idle_timestamp = jiffies;
  86. current_thread_info()->status &= ~TS_POLLING;
  87. /*
  88. * TS_POLLING-cleared state must be visible before we
  89. * test NEED_RESCHED:
  90. */
  91. smp_mb();
  92. if (!need_resched())
  93. _cpu_idle();
  94. else
  95. local_irq_enable();
  96. current_thread_info()->status |= TS_POLLING;
  97. }
  98. rcu_idle_exit();
  99. tick_nohz_idle_exit();
  100. schedule_preempt_disabled();
  101. }
  102. }
  103. struct thread_info *alloc_thread_info_node(struct task_struct *task, int node)
  104. {
  105. struct page *page;
  106. gfp_t flags = GFP_KERNEL;
  107. #ifdef CONFIG_DEBUG_STACK_USAGE
  108. flags |= __GFP_ZERO;
  109. #endif
  110. page = alloc_pages_node(node, flags, THREAD_SIZE_ORDER);
  111. if (!page)
  112. return NULL;
  113. return (struct thread_info *)page_address(page);
  114. }
  115. /*
  116. * Free a thread_info node, and all of its derivative
  117. * data structures.
  118. */
  119. void free_thread_info(struct thread_info *info)
  120. {
  121. struct single_step_state *step_state = info->step_state;
  122. #ifdef CONFIG_HARDWALL
  123. /*
  124. * We free a thread_info from the context of the task that has
  125. * been scheduled next, so the original task is already dead.
  126. * Calling deactivate here just frees up the data structures.
  127. * If the task we're freeing held the last reference to a
  128. * hardwall fd, it would have been released prior to this point
  129. * anyway via exit_files(), and "hardwall" would be NULL by now.
  130. */
  131. if (info->task->thread.hardwall)
  132. hardwall_deactivate(info->task);
  133. #endif
  134. if (step_state) {
  135. /*
  136. * FIXME: we don't munmap step_state->buffer
  137. * because the mm_struct for this process (info->task->mm)
  138. * has already been zeroed in exit_mm(). Keeping a
  139. * reference to it here seems like a bad move, so this
  140. * means we can't munmap() the buffer, and therefore if we
  141. * ptrace multiple threads in a process, we will slowly
  142. * leak user memory. (Note that as soon as the last
  143. * thread in a process dies, we will reclaim all user
  144. * memory including single-step buffers in the usual way.)
  145. * We should either assign a kernel VA to this buffer
  146. * somehow, or we should associate the buffer(s) with the
  147. * mm itself so we can clean them up that way.
  148. */
  149. kfree(step_state);
  150. }
  151. free_pages((unsigned long)info, THREAD_SIZE_ORDER);
  152. }
  153. static void save_arch_state(struct thread_struct *t);
  154. int copy_thread(unsigned long clone_flags, unsigned long sp,
  155. unsigned long stack_size,
  156. struct task_struct *p, struct pt_regs *regs)
  157. {
  158. struct pt_regs *childregs;
  159. unsigned long ksp;
  160. /*
  161. * When creating a new kernel thread we pass sp as zero.
  162. * Assign it to a reasonable value now that we have the stack.
  163. */
  164. if (sp == 0 && regs->ex1 == PL_ICS_EX1(KERNEL_PL, 0))
  165. sp = KSTK_TOP(p);
  166. /*
  167. * Do not clone step state from the parent; each thread
  168. * must make its own lazily.
  169. */
  170. task_thread_info(p)->step_state = NULL;
  171. /*
  172. * Start new thread in ret_from_fork so it schedules properly
  173. * and then return from interrupt like the parent.
  174. */
  175. p->thread.pc = (unsigned long) ret_from_fork;
  176. /* Save user stack top pointer so we can ID the stack vm area later. */
  177. p->thread.usp0 = sp;
  178. /* Record the pid of the process that created this one. */
  179. p->thread.creator_pid = current->pid;
  180. /*
  181. * Copy the registers onto the kernel stack so the
  182. * return-from-interrupt code will reload it into registers.
  183. */
  184. childregs = task_pt_regs(p);
  185. *childregs = *regs;
  186. childregs->regs[0] = 0; /* return value is zero */
  187. childregs->sp = sp; /* override with new user stack pointer */
  188. /*
  189. * If CLONE_SETTLS is set, set "tp" in the new task to "r4",
  190. * which is passed in as arg #5 to sys_clone().
  191. */
  192. if (clone_flags & CLONE_SETTLS)
  193. childregs->tp = regs->regs[4];
  194. /*
  195. * Copy the callee-saved registers from the passed pt_regs struct
  196. * into the context-switch callee-saved registers area.
  197. * This way when we start the interrupt-return sequence, the
  198. * callee-save registers will be correctly in registers, which
  199. * is how we assume the compiler leaves them as we start doing
  200. * the normal return-from-interrupt path after calling C code.
  201. * Zero out the C ABI save area to mark the top of the stack.
  202. */
  203. ksp = (unsigned long) childregs;
  204. ksp -= C_ABI_SAVE_AREA_SIZE; /* interrupt-entry save area */
  205. ((long *)ksp)[0] = ((long *)ksp)[1] = 0;
  206. ksp -= CALLEE_SAVED_REGS_COUNT * sizeof(unsigned long);
  207. memcpy((void *)ksp, &regs->regs[CALLEE_SAVED_FIRST_REG],
  208. CALLEE_SAVED_REGS_COUNT * sizeof(unsigned long));
  209. ksp -= C_ABI_SAVE_AREA_SIZE; /* __switch_to() save area */
  210. ((long *)ksp)[0] = ((long *)ksp)[1] = 0;
  211. p->thread.ksp = ksp;
  212. #if CHIP_HAS_TILE_DMA()
  213. /*
  214. * No DMA in the new thread. We model this on the fact that
  215. * fork() clears the pending signals, alarms, and aio for the child.
  216. */
  217. memset(&p->thread.tile_dma_state, 0, sizeof(struct tile_dma_state));
  218. memset(&p->thread.dma_async_tlb, 0, sizeof(struct async_tlb));
  219. #endif
  220. #if CHIP_HAS_SN_PROC()
  221. /* Likewise, the new thread is not running static processor code. */
  222. p->thread.sn_proc_running = 0;
  223. memset(&p->thread.sn_async_tlb, 0, sizeof(struct async_tlb));
  224. #endif
  225. #if CHIP_HAS_PROC_STATUS_SPR()
  226. /* New thread has its miscellaneous processor state bits clear. */
  227. p->thread.proc_status = 0;
  228. #endif
  229. #ifdef CONFIG_HARDWALL
  230. /* New thread does not own any networks. */
  231. p->thread.hardwall = NULL;
  232. #endif
  233. /*
  234. * Start the new thread with the current architecture state
  235. * (user interrupt masks, etc.).
  236. */
  237. save_arch_state(&p->thread);
  238. return 0;
  239. }
  240. /*
  241. * Return "current" if it looks plausible, or else a pointer to a dummy.
  242. * This can be helpful if we are just trying to emit a clean panic.
  243. */
  244. struct task_struct *validate_current(void)
  245. {
  246. static struct task_struct corrupt = { .comm = "<corrupt>" };
  247. struct task_struct *tsk = current;
  248. if (unlikely((unsigned long)tsk < PAGE_OFFSET ||
  249. (void *)tsk > high_memory ||
  250. ((unsigned long)tsk & (__alignof__(*tsk) - 1)) != 0)) {
  251. pr_err("Corrupt 'current' %p (sp %#lx)\n", tsk, stack_pointer);
  252. tsk = &corrupt;
  253. }
  254. return tsk;
  255. }
  256. /* Take and return the pointer to the previous task, for schedule_tail(). */
  257. struct task_struct *sim_notify_fork(struct task_struct *prev)
  258. {
  259. struct task_struct *tsk = current;
  260. __insn_mtspr(SPR_SIM_CONTROL, SIM_CONTROL_OS_FORK_PARENT |
  261. (tsk->thread.creator_pid << _SIM_CONTROL_OPERATOR_BITS));
  262. __insn_mtspr(SPR_SIM_CONTROL, SIM_CONTROL_OS_FORK |
  263. (tsk->pid << _SIM_CONTROL_OPERATOR_BITS));
  264. return prev;
  265. }
  266. int dump_task_regs(struct task_struct *tsk, elf_gregset_t *regs)
  267. {
  268. struct pt_regs *ptregs = task_pt_regs(tsk);
  269. elf_core_copy_regs(regs, ptregs);
  270. return 1;
  271. }
  272. #if CHIP_HAS_TILE_DMA()
  273. /* Allow user processes to access the DMA SPRs */
  274. void grant_dma_mpls(void)
  275. {
  276. #if CONFIG_KERNEL_PL == 2
  277. __insn_mtspr(SPR_MPL_DMA_CPL_SET_1, 1);
  278. __insn_mtspr(SPR_MPL_DMA_NOTIFY_SET_1, 1);
  279. #else
  280. __insn_mtspr(SPR_MPL_DMA_CPL_SET_0, 1);
  281. __insn_mtspr(SPR_MPL_DMA_NOTIFY_SET_0, 1);
  282. #endif
  283. }
  284. /* Forbid user processes from accessing the DMA SPRs */
  285. void restrict_dma_mpls(void)
  286. {
  287. #if CONFIG_KERNEL_PL == 2
  288. __insn_mtspr(SPR_MPL_DMA_CPL_SET_2, 1);
  289. __insn_mtspr(SPR_MPL_DMA_NOTIFY_SET_2, 1);
  290. #else
  291. __insn_mtspr(SPR_MPL_DMA_CPL_SET_1, 1);
  292. __insn_mtspr(SPR_MPL_DMA_NOTIFY_SET_1, 1);
  293. #endif
  294. }
  295. /* Pause the DMA engine, then save off its state registers. */
  296. static void save_tile_dma_state(struct tile_dma_state *dma)
  297. {
  298. unsigned long state = __insn_mfspr(SPR_DMA_USER_STATUS);
  299. unsigned long post_suspend_state;
  300. /* If we're running, suspend the engine. */
  301. if ((state & DMA_STATUS_MASK) == SPR_DMA_STATUS__RUNNING_MASK)
  302. __insn_mtspr(SPR_DMA_CTR, SPR_DMA_CTR__SUSPEND_MASK);
  303. /*
  304. * Wait for the engine to idle, then save regs. Note that we
  305. * want to record the "running" bit from before suspension,
  306. * and the "done" bit from after, so that we can properly
  307. * distinguish a case where the user suspended the engine from
  308. * the case where the kernel suspended as part of the context
  309. * swap.
  310. */
  311. do {
  312. post_suspend_state = __insn_mfspr(SPR_DMA_USER_STATUS);
  313. } while (post_suspend_state & SPR_DMA_STATUS__BUSY_MASK);
  314. dma->src = __insn_mfspr(SPR_DMA_SRC_ADDR);
  315. dma->src_chunk = __insn_mfspr(SPR_DMA_SRC_CHUNK_ADDR);
  316. dma->dest = __insn_mfspr(SPR_DMA_DST_ADDR);
  317. dma->dest_chunk = __insn_mfspr(SPR_DMA_DST_CHUNK_ADDR);
  318. dma->strides = __insn_mfspr(SPR_DMA_STRIDE);
  319. dma->chunk_size = __insn_mfspr(SPR_DMA_CHUNK_SIZE);
  320. dma->byte = __insn_mfspr(SPR_DMA_BYTE);
  321. dma->status = (state & SPR_DMA_STATUS__RUNNING_MASK) |
  322. (post_suspend_state & SPR_DMA_STATUS__DONE_MASK);
  323. }
  324. /* Restart a DMA that was running before we were context-switched out. */
  325. static void restore_tile_dma_state(struct thread_struct *t)
  326. {
  327. const struct tile_dma_state *dma = &t->tile_dma_state;
  328. /*
  329. * The only way to restore the done bit is to run a zero
  330. * length transaction.
  331. */
  332. if ((dma->status & SPR_DMA_STATUS__DONE_MASK) &&
  333. !(__insn_mfspr(SPR_DMA_USER_STATUS) & SPR_DMA_STATUS__DONE_MASK)) {
  334. __insn_mtspr(SPR_DMA_BYTE, 0);
  335. __insn_mtspr(SPR_DMA_CTR, SPR_DMA_CTR__REQUEST_MASK);
  336. while (__insn_mfspr(SPR_DMA_USER_STATUS) &
  337. SPR_DMA_STATUS__BUSY_MASK)
  338. ;
  339. }
  340. __insn_mtspr(SPR_DMA_SRC_ADDR, dma->src);
  341. __insn_mtspr(SPR_DMA_SRC_CHUNK_ADDR, dma->src_chunk);
  342. __insn_mtspr(SPR_DMA_DST_ADDR, dma->dest);
  343. __insn_mtspr(SPR_DMA_DST_CHUNK_ADDR, dma->dest_chunk);
  344. __insn_mtspr(SPR_DMA_STRIDE, dma->strides);
  345. __insn_mtspr(SPR_DMA_CHUNK_SIZE, dma->chunk_size);
  346. __insn_mtspr(SPR_DMA_BYTE, dma->byte);
  347. /*
  348. * Restart the engine if we were running and not done.
  349. * Clear a pending async DMA fault that we were waiting on return
  350. * to user space to execute, since we expect the DMA engine
  351. * to regenerate those faults for us now. Note that we don't
  352. * try to clear the TIF_ASYNC_TLB flag, since it's relatively
  353. * harmless if set, and it covers both DMA and the SN processor.
  354. */
  355. if ((dma->status & DMA_STATUS_MASK) == SPR_DMA_STATUS__RUNNING_MASK) {
  356. t->dma_async_tlb.fault_num = 0;
  357. __insn_mtspr(SPR_DMA_CTR, SPR_DMA_CTR__REQUEST_MASK);
  358. }
  359. }
  360. #endif
  361. static void save_arch_state(struct thread_struct *t)
  362. {
  363. #if CHIP_HAS_SPLIT_INTR_MASK()
  364. t->interrupt_mask = __insn_mfspr(SPR_INTERRUPT_MASK_0_0) |
  365. ((u64)__insn_mfspr(SPR_INTERRUPT_MASK_0_1) << 32);
  366. #else
  367. t->interrupt_mask = __insn_mfspr(SPR_INTERRUPT_MASK_0);
  368. #endif
  369. t->ex_context[0] = __insn_mfspr(SPR_EX_CONTEXT_0_0);
  370. t->ex_context[1] = __insn_mfspr(SPR_EX_CONTEXT_0_1);
  371. t->system_save[0] = __insn_mfspr(SPR_SYSTEM_SAVE_0_0);
  372. t->system_save[1] = __insn_mfspr(SPR_SYSTEM_SAVE_0_1);
  373. t->system_save[2] = __insn_mfspr(SPR_SYSTEM_SAVE_0_2);
  374. t->system_save[3] = __insn_mfspr(SPR_SYSTEM_SAVE_0_3);
  375. t->intctrl_0 = __insn_mfspr(SPR_INTCTRL_0_STATUS);
  376. #if CHIP_HAS_PROC_STATUS_SPR()
  377. t->proc_status = __insn_mfspr(SPR_PROC_STATUS);
  378. #endif
  379. #if !CHIP_HAS_FIXED_INTVEC_BASE()
  380. t->interrupt_vector_base = __insn_mfspr(SPR_INTERRUPT_VECTOR_BASE_0);
  381. #endif
  382. #if CHIP_HAS_TILE_RTF_HWM()
  383. t->tile_rtf_hwm = __insn_mfspr(SPR_TILE_RTF_HWM);
  384. #endif
  385. #if CHIP_HAS_DSTREAM_PF()
  386. t->dstream_pf = __insn_mfspr(SPR_DSTREAM_PF);
  387. #endif
  388. }
  389. static void restore_arch_state(const struct thread_struct *t)
  390. {
  391. #if CHIP_HAS_SPLIT_INTR_MASK()
  392. __insn_mtspr(SPR_INTERRUPT_MASK_0_0, (u32) t->interrupt_mask);
  393. __insn_mtspr(SPR_INTERRUPT_MASK_0_1, t->interrupt_mask >> 32);
  394. #else
  395. __insn_mtspr(SPR_INTERRUPT_MASK_0, t->interrupt_mask);
  396. #endif
  397. __insn_mtspr(SPR_EX_CONTEXT_0_0, t->ex_context[0]);
  398. __insn_mtspr(SPR_EX_CONTEXT_0_1, t->ex_context[1]);
  399. __insn_mtspr(SPR_SYSTEM_SAVE_0_0, t->system_save[0]);
  400. __insn_mtspr(SPR_SYSTEM_SAVE_0_1, t->system_save[1]);
  401. __insn_mtspr(SPR_SYSTEM_SAVE_0_2, t->system_save[2]);
  402. __insn_mtspr(SPR_SYSTEM_SAVE_0_3, t->system_save[3]);
  403. __insn_mtspr(SPR_INTCTRL_0_STATUS, t->intctrl_0);
  404. #if CHIP_HAS_PROC_STATUS_SPR()
  405. __insn_mtspr(SPR_PROC_STATUS, t->proc_status);
  406. #endif
  407. #if !CHIP_HAS_FIXED_INTVEC_BASE()
  408. __insn_mtspr(SPR_INTERRUPT_VECTOR_BASE_0, t->interrupt_vector_base);
  409. #endif
  410. #if CHIP_HAS_TILE_RTF_HWM()
  411. __insn_mtspr(SPR_TILE_RTF_HWM, t->tile_rtf_hwm);
  412. #endif
  413. #if CHIP_HAS_DSTREAM_PF()
  414. __insn_mtspr(SPR_DSTREAM_PF, t->dstream_pf);
  415. #endif
  416. }
  417. void _prepare_arch_switch(struct task_struct *next)
  418. {
  419. #if CHIP_HAS_SN_PROC()
  420. int snctl;
  421. #endif
  422. #if CHIP_HAS_TILE_DMA()
  423. struct tile_dma_state *dma = &current->thread.tile_dma_state;
  424. if (dma->enabled)
  425. save_tile_dma_state(dma);
  426. #endif
  427. #if CHIP_HAS_SN_PROC()
  428. /*
  429. * Suspend the static network processor if it was running.
  430. * We do not suspend the fabric itself, just like we don't
  431. * try to suspend the UDN.
  432. */
  433. snctl = __insn_mfspr(SPR_SNCTL);
  434. current->thread.sn_proc_running =
  435. (snctl & SPR_SNCTL__FRZPROC_MASK) == 0;
  436. if (current->thread.sn_proc_running)
  437. __insn_mtspr(SPR_SNCTL, snctl | SPR_SNCTL__FRZPROC_MASK);
  438. #endif
  439. }
  440. struct task_struct *__sched _switch_to(struct task_struct *prev,
  441. struct task_struct *next)
  442. {
  443. /* DMA state is already saved; save off other arch state. */
  444. save_arch_state(&prev->thread);
  445. #if CHIP_HAS_TILE_DMA()
  446. /*
  447. * Restore DMA in new task if desired.
  448. * Note that it is only safe to restart here since interrupts
  449. * are disabled, so we can't take any DMATLB miss or access
  450. * interrupts before we have finished switching stacks.
  451. */
  452. if (next->thread.tile_dma_state.enabled) {
  453. restore_tile_dma_state(&next->thread);
  454. grant_dma_mpls();
  455. } else {
  456. restrict_dma_mpls();
  457. }
  458. #endif
  459. /* Restore other arch state. */
  460. restore_arch_state(&next->thread);
  461. #if CHIP_HAS_SN_PROC()
  462. /*
  463. * Restart static network processor in the new process
  464. * if it was running before.
  465. */
  466. if (next->thread.sn_proc_running) {
  467. int snctl = __insn_mfspr(SPR_SNCTL);
  468. __insn_mtspr(SPR_SNCTL, snctl & ~SPR_SNCTL__FRZPROC_MASK);
  469. }
  470. #endif
  471. #ifdef CONFIG_HARDWALL
  472. /* Enable or disable access to the network registers appropriately. */
  473. if (prev->thread.hardwall != NULL) {
  474. if (next->thread.hardwall == NULL)
  475. restrict_network_mpls();
  476. } else if (next->thread.hardwall != NULL) {
  477. grant_network_mpls();
  478. }
  479. #endif
  480. /*
  481. * Switch kernel SP, PC, and callee-saved registers.
  482. * In the context of the new task, return the old task pointer
  483. * (i.e. the task that actually called __switch_to).
  484. * Pass the value to use for SYSTEM_SAVE_K_0 when we reset our sp.
  485. */
  486. return __switch_to(prev, next, next_current_ksp0(next));
  487. }
  488. /*
  489. * This routine is called on return from interrupt if any of the
  490. * TIF_WORK_MASK flags are set in thread_info->flags. It is
  491. * entered with interrupts disabled so we don't miss an event
  492. * that modified the thread_info flags. If any flag is set, we
  493. * handle it and return, and the calling assembly code will
  494. * re-disable interrupts, reload the thread flags, and call back
  495. * if more flags need to be handled.
  496. *
  497. * We return whether we need to check the thread_info flags again
  498. * or not. Note that we don't clear TIF_SINGLESTEP here, so it's
  499. * important that it be tested last, and then claim that we don't
  500. * need to recheck the flags.
  501. */
  502. int do_work_pending(struct pt_regs *regs, u32 thread_info_flags)
  503. {
  504. if (thread_info_flags & _TIF_NEED_RESCHED) {
  505. schedule();
  506. return 1;
  507. }
  508. #if CHIP_HAS_TILE_DMA() || CHIP_HAS_SN_PROC()
  509. if (thread_info_flags & _TIF_ASYNC_TLB) {
  510. do_async_page_fault(regs);
  511. return 1;
  512. }
  513. #endif
  514. if (thread_info_flags & _TIF_SIGPENDING) {
  515. do_signal(regs);
  516. return 1;
  517. }
  518. if (thread_info_flags & _TIF_NOTIFY_RESUME) {
  519. clear_thread_flag(TIF_NOTIFY_RESUME);
  520. tracehook_notify_resume(regs);
  521. if (current->replacement_session_keyring)
  522. key_replace_session_keyring();
  523. return 1;
  524. }
  525. if (thread_info_flags & _TIF_SINGLESTEP) {
  526. if ((regs->ex1 & SPR_EX_CONTEXT_1_1__PL_MASK) == 0)
  527. single_step_once(regs);
  528. return 0;
  529. }
  530. panic("work_pending: bad flags %#x\n", thread_info_flags);
  531. }
  532. /* Note there is an implicit fifth argument if (clone_flags & CLONE_SETTLS). */
  533. SYSCALL_DEFINE5(clone, unsigned long, clone_flags, unsigned long, newsp,
  534. void __user *, parent_tidptr, void __user *, child_tidptr,
  535. struct pt_regs *, regs)
  536. {
  537. if (!newsp)
  538. newsp = regs->sp;
  539. return do_fork(clone_flags, newsp, regs, 0,
  540. parent_tidptr, child_tidptr);
  541. }
  542. /*
  543. * sys_execve() executes a new program.
  544. */
  545. SYSCALL_DEFINE4(execve, const char __user *, path,
  546. const char __user *const __user *, argv,
  547. const char __user *const __user *, envp,
  548. struct pt_regs *, regs)
  549. {
  550. long error;
  551. char *filename;
  552. filename = getname(path);
  553. error = PTR_ERR(filename);
  554. if (IS_ERR(filename))
  555. goto out;
  556. error = do_execve(filename, argv, envp, regs);
  557. putname(filename);
  558. if (error == 0)
  559. single_step_execve();
  560. out:
  561. return error;
  562. }
  563. #ifdef CONFIG_COMPAT
  564. long compat_sys_execve(const char __user *path,
  565. compat_uptr_t __user *argv,
  566. compat_uptr_t __user *envp,
  567. struct pt_regs *regs)
  568. {
  569. long error;
  570. char *filename;
  571. filename = getname(path);
  572. error = PTR_ERR(filename);
  573. if (IS_ERR(filename))
  574. goto out;
  575. error = compat_do_execve(filename, argv, envp, regs);
  576. putname(filename);
  577. if (error == 0)
  578. single_step_execve();
  579. out:
  580. return error;
  581. }
  582. #endif
  583. unsigned long get_wchan(struct task_struct *p)
  584. {
  585. struct KBacktraceIterator kbt;
  586. if (!p || p == current || p->state == TASK_RUNNING)
  587. return 0;
  588. for (KBacktraceIterator_init(&kbt, p, NULL);
  589. !KBacktraceIterator_end(&kbt);
  590. KBacktraceIterator_next(&kbt)) {
  591. if (!in_sched_functions(kbt.it.pc))
  592. return kbt.it.pc;
  593. }
  594. return 0;
  595. }
  596. /*
  597. * We pass in lr as zero (cleared in kernel_thread) and the caller
  598. * part of the backtrace ABI on the stack also zeroed (in copy_thread)
  599. * so that backtraces will stop with this function.
  600. * Note that we don't use r0, since copy_thread() clears it.
  601. */
  602. static void start_kernel_thread(int dummy, int (*fn)(int), int arg)
  603. {
  604. do_exit(fn(arg));
  605. }
  606. /*
  607. * Create a kernel thread
  608. */
  609. int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
  610. {
  611. struct pt_regs regs;
  612. memset(&regs, 0, sizeof(regs));
  613. regs.ex1 = PL_ICS_EX1(KERNEL_PL, 0); /* run at kernel PL, no ICS */
  614. regs.pc = (long) start_kernel_thread;
  615. regs.flags = PT_FLAGS_CALLER_SAVES; /* need to restore r1 and r2 */
  616. regs.regs[1] = (long) fn; /* function pointer */
  617. regs.regs[2] = (long) arg; /* parameter register */
  618. /* Ok, create the new process.. */
  619. return do_fork(flags | CLONE_VM | CLONE_UNTRACED, 0, &regs,
  620. 0, NULL, NULL);
  621. }
  622. EXPORT_SYMBOL(kernel_thread);
  623. /* Flush thread state. */
  624. void flush_thread(void)
  625. {
  626. /* Nothing */
  627. }
  628. /*
  629. * Free current thread data structures etc..
  630. */
  631. void exit_thread(void)
  632. {
  633. /* Nothing */
  634. }
  635. void show_regs(struct pt_regs *regs)
  636. {
  637. struct task_struct *tsk = validate_current();
  638. int i;
  639. pr_err("\n");
  640. pr_err(" Pid: %d, comm: %20s, CPU: %d\n",
  641. tsk->pid, tsk->comm, smp_processor_id());
  642. #ifdef __tilegx__
  643. for (i = 0; i < 51; i += 3)
  644. pr_err(" r%-2d: "REGFMT" r%-2d: "REGFMT" r%-2d: "REGFMT"\n",
  645. i, regs->regs[i], i+1, regs->regs[i+1],
  646. i+2, regs->regs[i+2]);
  647. pr_err(" r51: "REGFMT" r52: "REGFMT" tp : "REGFMT"\n",
  648. regs->regs[51], regs->regs[52], regs->tp);
  649. pr_err(" sp : "REGFMT" lr : "REGFMT"\n", regs->sp, regs->lr);
  650. #else
  651. for (i = 0; i < 52; i += 4)
  652. pr_err(" r%-2d: "REGFMT" r%-2d: "REGFMT
  653. " r%-2d: "REGFMT" r%-2d: "REGFMT"\n",
  654. i, regs->regs[i], i+1, regs->regs[i+1],
  655. i+2, regs->regs[i+2], i+3, regs->regs[i+3]);
  656. pr_err(" r52: "REGFMT" tp : "REGFMT" sp : "REGFMT" lr : "REGFMT"\n",
  657. regs->regs[52], regs->tp, regs->sp, regs->lr);
  658. #endif
  659. pr_err(" pc : "REGFMT" ex1: %ld faultnum: %ld\n",
  660. regs->pc, regs->ex1, regs->faultnum);
  661. dump_stack_regs(regs);
  662. }