process.c 18 KB

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