process.c 21 KB

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