process.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. /* $Id: process.c,v 1.28 2004/05/05 16:54:23 lethal Exp $
  2. *
  3. * linux/arch/sh/kernel/process.c
  4. *
  5. * Copyright (C) 1995 Linus Torvalds
  6. *
  7. * SuperH version: Copyright (C) 1999, 2000 Niibe Yutaka & Kaz Kojima
  8. * Copyright (C) 2006 Lineo Solutions Inc. support SH4A UBC
  9. */
  10. /*
  11. * This file handles the architecture-dependent parts of process handling..
  12. */
  13. #include <linux/module.h>
  14. #include <linux/unistd.h>
  15. #include <linux/mm.h>
  16. #include <linux/elfcore.h>
  17. #include <linux/a.out.h>
  18. #include <linux/slab.h>
  19. #include <linux/pm.h>
  20. #include <linux/ptrace.h>
  21. #include <linux/kallsyms.h>
  22. #include <linux/kexec.h>
  23. #include <asm/io.h>
  24. #include <asm/uaccess.h>
  25. #include <asm/mmu_context.h>
  26. #include <asm/elf.h>
  27. #include <asm/ubc.h>
  28. static int hlt_counter=0;
  29. int ubc_usercnt = 0;
  30. #define HARD_IDLE_TIMEOUT (HZ / 3)
  31. void (*pm_idle)(void);
  32. void (*pm_power_off)(void);
  33. EXPORT_SYMBOL(pm_power_off);
  34. void disable_hlt(void)
  35. {
  36. hlt_counter++;
  37. }
  38. EXPORT_SYMBOL(disable_hlt);
  39. void enable_hlt(void)
  40. {
  41. hlt_counter--;
  42. }
  43. EXPORT_SYMBOL(enable_hlt);
  44. void default_idle(void)
  45. {
  46. if (!hlt_counter)
  47. cpu_sleep();
  48. else
  49. cpu_relax();
  50. }
  51. void cpu_idle(void)
  52. {
  53. /* endless idle loop with no priority at all */
  54. while (1) {
  55. void (*idle)(void) = pm_idle;
  56. if (!idle)
  57. idle = default_idle;
  58. while (!need_resched())
  59. idle();
  60. preempt_enable_no_resched();
  61. schedule();
  62. preempt_disable();
  63. }
  64. }
  65. void machine_restart(char * __unused)
  66. {
  67. /* SR.BL=1 and invoke address error to let CPU reset (manual reset) */
  68. asm volatile("ldc %0, sr\n\t"
  69. "mov.l @%1, %0" : : "r" (0x10000000), "r" (0x80000001));
  70. }
  71. void machine_halt(void)
  72. {
  73. local_irq_disable();
  74. while (1)
  75. cpu_sleep();
  76. }
  77. void machine_power_off(void)
  78. {
  79. if (pm_power_off)
  80. pm_power_off();
  81. }
  82. void show_regs(struct pt_regs * regs)
  83. {
  84. printk("\n");
  85. printk("Pid : %d, Comm: %20s\n", current->pid, current->comm);
  86. print_symbol("PC is at %s\n", instruction_pointer(regs));
  87. printk("PC : %08lx SP : %08lx SR : %08lx ",
  88. regs->pc, regs->regs[15], regs->sr);
  89. #ifdef CONFIG_MMU
  90. printk("TEA : %08x ", ctrl_inl(MMU_TEA));
  91. #else
  92. printk(" ");
  93. #endif
  94. printk("%s\n", print_tainted());
  95. printk("R0 : %08lx R1 : %08lx R2 : %08lx R3 : %08lx\n",
  96. regs->regs[0],regs->regs[1],
  97. regs->regs[2],regs->regs[3]);
  98. printk("R4 : %08lx R5 : %08lx R6 : %08lx R7 : %08lx\n",
  99. regs->regs[4],regs->regs[5],
  100. regs->regs[6],regs->regs[7]);
  101. printk("R8 : %08lx R9 : %08lx R10 : %08lx R11 : %08lx\n",
  102. regs->regs[8],regs->regs[9],
  103. regs->regs[10],regs->regs[11]);
  104. printk("R12 : %08lx R13 : %08lx R14 : %08lx\n",
  105. regs->regs[12],regs->regs[13],
  106. regs->regs[14]);
  107. printk("MACH: %08lx MACL: %08lx GBR : %08lx PR : %08lx\n",
  108. regs->mach, regs->macl, regs->gbr, regs->pr);
  109. show_trace(NULL, (unsigned long *)regs->regs[15], regs);
  110. }
  111. /*
  112. * Create a kernel thread
  113. */
  114. /*
  115. * This is the mechanism for creating a new kernel thread.
  116. *
  117. */
  118. extern void kernel_thread_helper(void);
  119. __asm__(".align 5\n"
  120. "kernel_thread_helper:\n\t"
  121. "jsr @r5\n\t"
  122. " nop\n\t"
  123. "mov.l 1f, r1\n\t"
  124. "jsr @r1\n\t"
  125. " mov r0, r4\n\t"
  126. ".align 2\n\t"
  127. "1:.long do_exit");
  128. int kernel_thread(int (*fn)(void *), void * arg, unsigned long flags)
  129. { /* Don't use this in BL=1(cli). Or else, CPU resets! */
  130. struct pt_regs regs;
  131. memset(&regs, 0, sizeof(regs));
  132. regs.regs[4] = (unsigned long) arg;
  133. regs.regs[5] = (unsigned long) fn;
  134. regs.pc = (unsigned long) kernel_thread_helper;
  135. regs.sr = (1 << 30);
  136. /* Ok, create the new process.. */
  137. return do_fork(flags | CLONE_VM | CLONE_UNTRACED, 0, &regs, 0, NULL, NULL);
  138. }
  139. /*
  140. * Free current thread data structures etc..
  141. */
  142. void exit_thread(void)
  143. {
  144. if (current->thread.ubc_pc) {
  145. current->thread.ubc_pc = 0;
  146. ubc_usercnt -= 1;
  147. }
  148. }
  149. void flush_thread(void)
  150. {
  151. #if defined(CONFIG_SH_FPU)
  152. struct task_struct *tsk = current;
  153. /* Forget lazy FPU state */
  154. clear_fpu(tsk, task_pt_regs(tsk));
  155. clear_used_math();
  156. #endif
  157. }
  158. void release_thread(struct task_struct *dead_task)
  159. {
  160. /* do nothing */
  161. }
  162. /* Fill in the fpu structure for a core dump.. */
  163. int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpu)
  164. {
  165. int fpvalid = 0;
  166. #if defined(CONFIG_SH_FPU)
  167. struct task_struct *tsk = current;
  168. fpvalid = !!tsk_used_math(tsk);
  169. if (fpvalid) {
  170. unlazy_fpu(tsk, regs);
  171. memcpy(fpu, &tsk->thread.fpu.hard, sizeof(*fpu));
  172. }
  173. #endif
  174. return fpvalid;
  175. }
  176. /*
  177. * Capture the user space registers if the task is not running (in user space)
  178. */
  179. int dump_task_regs(struct task_struct *tsk, elf_gregset_t *regs)
  180. {
  181. struct pt_regs ptregs;
  182. ptregs = *task_pt_regs(tsk);
  183. elf_core_copy_regs(regs, &ptregs);
  184. return 1;
  185. }
  186. int
  187. dump_task_fpu (struct task_struct *tsk, elf_fpregset_t *fpu)
  188. {
  189. int fpvalid = 0;
  190. #if defined(CONFIG_SH_FPU)
  191. fpvalid = !!tsk_used_math(tsk);
  192. if (fpvalid) {
  193. unlazy_fpu(tsk, task_pt_regs(tsk));
  194. memcpy(fpu, &tsk->thread.fpu.hard, sizeof(*fpu));
  195. }
  196. #endif
  197. return fpvalid;
  198. }
  199. asmlinkage void ret_from_fork(void);
  200. int copy_thread(int nr, unsigned long clone_flags, unsigned long usp,
  201. unsigned long unused,
  202. struct task_struct *p, struct pt_regs *regs)
  203. {
  204. struct thread_info *ti = task_thread_info(p);
  205. struct pt_regs *childregs;
  206. #if defined(CONFIG_SH_FPU)
  207. struct task_struct *tsk = current;
  208. unlazy_fpu(tsk, regs);
  209. p->thread.fpu = tsk->thread.fpu;
  210. copy_to_stopped_child_used_math(p);
  211. #endif
  212. childregs = task_pt_regs(p);
  213. *childregs = *regs;
  214. if (user_mode(regs)) {
  215. childregs->regs[15] = usp;
  216. ti->addr_limit = USER_DS;
  217. } else {
  218. childregs->regs[15] = (unsigned long)task_stack_page(p) + THREAD_SIZE;
  219. ti->addr_limit = KERNEL_DS;
  220. }
  221. if (clone_flags & CLONE_SETTLS) {
  222. childregs->gbr = childregs->regs[0];
  223. }
  224. childregs->regs[0] = 0; /* Set return value for child */
  225. p->thread.sp = (unsigned long) childregs;
  226. p->thread.pc = (unsigned long) ret_from_fork;
  227. p->thread.ubc_pc = 0;
  228. return 0;
  229. }
  230. /* Tracing by user break controller. */
  231. static void
  232. ubc_set_tracing(int asid, unsigned long pc)
  233. {
  234. #if defined(CONFIG_CPU_SH4A)
  235. unsigned long val;
  236. val = (UBC_CBR_ID_INST | UBC_CBR_RW_READ | UBC_CBR_CE);
  237. val |= (UBC_CBR_AIE | UBC_CBR_AIV_SET(asid));
  238. ctrl_outl(val, UBC_CBR0);
  239. ctrl_outl(pc, UBC_CAR0);
  240. ctrl_outl(0x0, UBC_CAMR0);
  241. ctrl_outl(0x0, UBC_CBCR);
  242. val = (UBC_CRR_RES | UBC_CRR_PCB | UBC_CRR_BIE);
  243. ctrl_outl(val, UBC_CRR0);
  244. /* Read UBC register that we writed last. For chekking UBC Register changed */
  245. val = ctrl_inl(UBC_CRR0);
  246. #else /* CONFIG_CPU_SH4A */
  247. ctrl_outl(pc, UBC_BARA);
  248. #ifdef CONFIG_MMU
  249. /* We don't have any ASID settings for the SH-2! */
  250. if (cpu_data->type != CPU_SH7604)
  251. ctrl_outb(asid, UBC_BASRA);
  252. #endif
  253. ctrl_outl(0, UBC_BAMRA);
  254. if (cpu_data->type == CPU_SH7729 || cpu_data->type == CPU_SH7710) {
  255. ctrl_outw(BBR_INST | BBR_READ | BBR_CPU, UBC_BBRA);
  256. ctrl_outl(BRCR_PCBA | BRCR_PCTE, UBC_BRCR);
  257. } else {
  258. ctrl_outw(BBR_INST | BBR_READ, UBC_BBRA);
  259. ctrl_outw(BRCR_PCBA, UBC_BRCR);
  260. }
  261. #endif /* CONFIG_CPU_SH4A */
  262. }
  263. /*
  264. * switch_to(x,y) should switch tasks from x to y.
  265. *
  266. */
  267. struct task_struct *__switch_to(struct task_struct *prev, struct task_struct *next)
  268. {
  269. #if defined(CONFIG_SH_FPU)
  270. unlazy_fpu(prev, task_pt_regs(prev));
  271. #endif
  272. #ifdef CONFIG_PREEMPT
  273. {
  274. unsigned long flags;
  275. struct pt_regs *regs;
  276. local_irq_save(flags);
  277. regs = task_pt_regs(prev);
  278. if (user_mode(regs) && regs->regs[15] >= 0xc0000000) {
  279. int offset = (int)regs->regs[15];
  280. /* Reset stack pointer: clear critical region mark */
  281. regs->regs[15] = regs->regs[1];
  282. if (regs->pc < regs->regs[0])
  283. /* Go to rewind point */
  284. regs->pc = regs->regs[0] + offset;
  285. }
  286. local_irq_restore(flags);
  287. }
  288. #endif
  289. #ifdef CONFIG_MMU
  290. /*
  291. * Restore the kernel mode register
  292. * k7 (r7_bank1)
  293. */
  294. asm volatile("ldc %0, r7_bank"
  295. : /* no output */
  296. : "r" (task_thread_info(next)));
  297. #endif
  298. /* If no tasks are using the UBC, we're done */
  299. if (ubc_usercnt == 0)
  300. /* If no tasks are using the UBC, we're done */;
  301. else if (next->thread.ubc_pc && next->mm) {
  302. int asid = 0;
  303. #ifdef CONFIG_MMU
  304. asid |= next->mm->context.id & MMU_CONTEXT_ASID_MASK;
  305. #endif
  306. ubc_set_tracing(asid, next->thread.ubc_pc);
  307. } else {
  308. #if defined(CONFIG_CPU_SH4A)
  309. ctrl_outl(UBC_CBR_INIT, UBC_CBR0);
  310. ctrl_outl(UBC_CRR_INIT, UBC_CRR0);
  311. #else
  312. ctrl_outw(0, UBC_BBRA);
  313. ctrl_outw(0, UBC_BBRB);
  314. #endif
  315. }
  316. return prev;
  317. }
  318. asmlinkage int sys_fork(unsigned long r4, unsigned long r5,
  319. unsigned long r6, unsigned long r7,
  320. struct pt_regs __regs)
  321. {
  322. struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
  323. #ifdef CONFIG_MMU
  324. return do_fork(SIGCHLD, regs->regs[15], regs, 0, NULL, NULL);
  325. #else
  326. /* fork almost works, enough to trick you into looking elsewhere :-( */
  327. return -EINVAL;
  328. #endif
  329. }
  330. asmlinkage int sys_clone(unsigned long clone_flags, unsigned long newsp,
  331. unsigned long parent_tidptr,
  332. unsigned long child_tidptr,
  333. struct pt_regs __regs)
  334. {
  335. struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
  336. if (!newsp)
  337. newsp = regs->regs[15];
  338. return do_fork(clone_flags, newsp, regs, 0,
  339. (int __user *)parent_tidptr, (int __user *)child_tidptr);
  340. }
  341. /*
  342. * This is trivial, and on the face of it looks like it
  343. * could equally well be done in user mode.
  344. *
  345. * Not so, for quite unobvious reasons - register pressure.
  346. * In user mode vfork() cannot have a stack frame, and if
  347. * done by calling the "clone()" system call directly, you
  348. * do not have enough call-clobbered registers to hold all
  349. * the information you need.
  350. */
  351. asmlinkage int sys_vfork(unsigned long r4, unsigned long r5,
  352. unsigned long r6, unsigned long r7,
  353. struct pt_regs __regs)
  354. {
  355. struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
  356. return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs->regs[15], regs,
  357. 0, NULL, NULL);
  358. }
  359. /*
  360. * sys_execve() executes a new program.
  361. */
  362. asmlinkage int sys_execve(char *ufilename, char **uargv,
  363. char **uenvp, unsigned long r7,
  364. struct pt_regs __regs)
  365. {
  366. struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
  367. int error;
  368. char *filename;
  369. filename = getname((char __user *)ufilename);
  370. error = PTR_ERR(filename);
  371. if (IS_ERR(filename))
  372. goto out;
  373. error = do_execve(filename,
  374. (char __user * __user *)uargv,
  375. (char __user * __user *)uenvp,
  376. regs);
  377. if (error == 0) {
  378. task_lock(current);
  379. current->ptrace &= ~PT_DTRACE;
  380. task_unlock(current);
  381. }
  382. putname(filename);
  383. out:
  384. return error;
  385. }
  386. unsigned long get_wchan(struct task_struct *p)
  387. {
  388. unsigned long schedule_frame;
  389. unsigned long pc;
  390. if (!p || p == current || p->state == TASK_RUNNING)
  391. return 0;
  392. /*
  393. * The same comment as on the Alpha applies here, too ...
  394. */
  395. pc = thread_saved_pc(p);
  396. if (in_sched_functions(pc)) {
  397. schedule_frame = (unsigned long)p->thread.sp;
  398. return ((unsigned long *)schedule_frame)[21];
  399. }
  400. return pc;
  401. }
  402. asmlinkage void break_point_trap(void)
  403. {
  404. /* Clear tracing. */
  405. #if defined(CONFIG_CPU_SH4A)
  406. ctrl_outl(UBC_CBR_INIT, UBC_CBR0);
  407. ctrl_outl(UBC_CRR_INIT, UBC_CRR0);
  408. #else
  409. ctrl_outw(0, UBC_BBRA);
  410. ctrl_outw(0, UBC_BBRB);
  411. #endif
  412. current->thread.ubc_pc = 0;
  413. ubc_usercnt -= 1;
  414. force_sig(SIGTRAP, current);
  415. }
  416. asmlinkage void break_point_trap_software(unsigned long r4, unsigned long r5,
  417. unsigned long r6, unsigned long r7,
  418. struct pt_regs __regs)
  419. {
  420. struct pt_regs *regs = RELOC_HIDE(&__regs, 0);
  421. /* Rewind */
  422. regs->pc -= 2;
  423. #ifdef CONFIG_BUG
  424. if (__kernel_text_address(instruction_pointer(regs))) {
  425. u16 insn = *(u16 *)instruction_pointer(regs);
  426. if (insn == TRAPA_BUG_OPCODE)
  427. handle_BUG(regs);
  428. }
  429. #endif
  430. force_sig(SIGTRAP, current);
  431. }