process.c 21 KB

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