process.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /* process.c: FRV specific parts of process handling
  2. *
  3. * Copyright (C) 2003-5 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. * - Derived from arch/m68k/kernel/process.c
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the License, or (at your option) any later version.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/errno.h>
  14. #include <linux/sched.h>
  15. #include <linux/kernel.h>
  16. #include <linux/mm.h>
  17. #include <linux/smp.h>
  18. #include <linux/stddef.h>
  19. #include <linux/unistd.h>
  20. #include <linux/ptrace.h>
  21. #include <linux/slab.h>
  22. #include <linux/user.h>
  23. #include <linux/elf.h>
  24. #include <linux/reboot.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/pagemap.h>
  27. #include <linux/rcupdate.h>
  28. #include <asm/asm-offsets.h>
  29. #include <asm/uaccess.h>
  30. #include <asm/setup.h>
  31. #include <asm/pgtable.h>
  32. #include <asm/tlb.h>
  33. #include <asm/gdb-stub.h>
  34. #include <asm/mb-regs.h>
  35. #include "local.h"
  36. asmlinkage void ret_from_fork(void);
  37. asmlinkage void ret_from_kernel_thread(void);
  38. #include <asm/pgalloc.h>
  39. void (*pm_power_off)(void);
  40. EXPORT_SYMBOL(pm_power_off);
  41. static void core_sleep_idle(void)
  42. {
  43. #ifdef LED_DEBUG_SLEEP
  44. /* Show that we're sleeping... */
  45. __set_LEDS(0x55aa);
  46. #endif
  47. frv_cpu_core_sleep();
  48. #ifdef LED_DEBUG_SLEEP
  49. /* ... and that we woke up */
  50. __set_LEDS(0);
  51. #endif
  52. mb();
  53. }
  54. void (*idle)(void) = core_sleep_idle;
  55. /*
  56. * The idle thread. There's no useful work to be
  57. * done, so just try to conserve power and have a
  58. * low exit latency (ie sit in a loop waiting for
  59. * somebody to say that they'd like to reschedule)
  60. */
  61. void cpu_idle(void)
  62. {
  63. /* endless idle loop with no priority at all */
  64. while (1) {
  65. rcu_idle_enter();
  66. while (!need_resched()) {
  67. check_pgt_cache();
  68. if (!frv_dma_inprogress && idle)
  69. idle();
  70. }
  71. rcu_idle_exit();
  72. schedule_preempt_disabled();
  73. }
  74. }
  75. void machine_restart(char * __unused)
  76. {
  77. unsigned long reset_addr;
  78. #ifdef CONFIG_GDBSTUB
  79. gdbstub_exit(0);
  80. #endif
  81. if (PSR_IMPLE(__get_PSR()) == PSR_IMPLE_FR551)
  82. reset_addr = 0xfefff500;
  83. else
  84. reset_addr = 0xfeff0500;
  85. /* Software reset. */
  86. asm volatile(" dcef @(gr0,gr0),1 ! membar !"
  87. " sti %1,@(%0,0) !"
  88. " nop ! nop ! nop ! nop ! nop ! "
  89. " nop ! nop ! nop ! nop ! nop ! "
  90. " nop ! nop ! nop ! nop ! nop ! "
  91. " nop ! nop ! nop ! nop ! nop ! "
  92. : : "r" (reset_addr), "r" (1) );
  93. for (;;)
  94. ;
  95. }
  96. void machine_halt(void)
  97. {
  98. #ifdef CONFIG_GDBSTUB
  99. gdbstub_exit(0);
  100. #endif
  101. for (;;);
  102. }
  103. void machine_power_off(void)
  104. {
  105. #ifdef CONFIG_GDBSTUB
  106. gdbstub_exit(0);
  107. #endif
  108. for (;;);
  109. }
  110. void flush_thread(void)
  111. {
  112. /* nothing */
  113. }
  114. inline unsigned long user_stack(const struct pt_regs *regs)
  115. {
  116. while (regs->next_frame)
  117. regs = regs->next_frame;
  118. return user_mode(regs) ? regs->sp : 0;
  119. }
  120. asmlinkage int sys_fork(void)
  121. {
  122. #ifndef CONFIG_MMU
  123. /* fork almost works, enough to trick you into looking elsewhere:-( */
  124. return -EINVAL;
  125. #else
  126. return do_fork(SIGCHLD, user_stack(__frame), __frame, 0, NULL, NULL);
  127. #endif
  128. }
  129. asmlinkage int sys_vfork(void)
  130. {
  131. return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, user_stack(__frame), __frame, 0,
  132. NULL, NULL);
  133. }
  134. /*****************************************************************************/
  135. /*
  136. * clone a process
  137. * - tlsptr is retrieved by copy_thread()
  138. */
  139. asmlinkage int sys_clone(unsigned long clone_flags, unsigned long newsp,
  140. int __user *parent_tidptr, int __user *child_tidptr,
  141. int __user *tlsptr)
  142. {
  143. if (!newsp)
  144. newsp = user_stack(__frame);
  145. return do_fork(clone_flags, newsp, __frame, 0, parent_tidptr, child_tidptr);
  146. } /* end sys_clone() */
  147. /*
  148. * set up the kernel stack and exception frames for a new process
  149. */
  150. int copy_thread(unsigned long clone_flags,
  151. unsigned long usp, unsigned long arg,
  152. struct task_struct *p, struct pt_regs *regs)
  153. {
  154. struct pt_regs *childregs;
  155. childregs = (struct pt_regs *)
  156. (task_stack_page(p) + THREAD_SIZE - FRV_FRAME0_SIZE);
  157. /* set up the userspace frame (the only place that the USP is stored) */
  158. *childregs = *__kernel_frame0_ptr;
  159. p->set_child_tid = p->clear_child_tid = NULL;
  160. p->thread.frame = childregs;
  161. p->thread.curr = p;
  162. p->thread.sp = (unsigned long) childregs;
  163. p->thread.fp = 0;
  164. p->thread.lr = 0;
  165. p->thread.frame0 = childregs;
  166. if (unlikely(!regs)) {
  167. childregs->gr9 = usp; /* function */
  168. childregs->gr8 = arg;
  169. p->thread.pc = (unsigned long) ret_from_kernel_thread;
  170. save_user_regs(p->thread.user);
  171. return 0;
  172. }
  173. /* set up the userspace frame (the only place that the USP is stored) */
  174. *childregs = *regs;
  175. childregs->sp = usp;
  176. childregs->next_frame = NULL;
  177. p->thread.pc = (unsigned long) ret_from_fork;
  178. /* the new TLS pointer is passed in as arg #5 to sys_clone() */
  179. if (clone_flags & CLONE_SETTLS)
  180. childregs->gr29 = childregs->gr12;
  181. save_user_regs(p->thread.user);
  182. return 0;
  183. } /* end copy_thread() */
  184. unsigned long get_wchan(struct task_struct *p)
  185. {
  186. struct pt_regs *regs0;
  187. unsigned long fp, pc;
  188. unsigned long stack_limit;
  189. int count = 0;
  190. if (!p || p == current || p->state == TASK_RUNNING)
  191. return 0;
  192. stack_limit = (unsigned long) (p + 1);
  193. fp = p->thread.fp;
  194. regs0 = p->thread.frame0;
  195. do {
  196. if (fp < stack_limit || fp >= (unsigned long) regs0 || fp & 3)
  197. return 0;
  198. pc = ((unsigned long *) fp)[2];
  199. /* FIXME: This depends on the order of these functions. */
  200. if (!in_sched_functions(pc))
  201. return pc;
  202. fp = *(unsigned long *) fp;
  203. } while (count++ < 16);
  204. return 0;
  205. }
  206. unsigned long thread_saved_pc(struct task_struct *tsk)
  207. {
  208. /* Check whether the thread is blocked in resume() */
  209. if (in_sched_functions(tsk->thread.pc))
  210. return ((unsigned long *)tsk->thread.fp)[2];
  211. else
  212. return tsk->thread.pc;
  213. }
  214. int elf_check_arch(const struct elf32_hdr *hdr)
  215. {
  216. unsigned long hsr0 = __get_HSR(0);
  217. unsigned long psr = __get_PSR();
  218. if (hdr->e_machine != EM_FRV)
  219. return 0;
  220. switch (hdr->e_flags & EF_FRV_GPR_MASK) {
  221. case EF_FRV_GPR64:
  222. if ((hsr0 & HSR0_GRN) == HSR0_GRN_32)
  223. return 0;
  224. case EF_FRV_GPR32:
  225. case 0:
  226. break;
  227. default:
  228. return 0;
  229. }
  230. switch (hdr->e_flags & EF_FRV_FPR_MASK) {
  231. case EF_FRV_FPR64:
  232. if ((hsr0 & HSR0_FRN) == HSR0_FRN_32)
  233. return 0;
  234. case EF_FRV_FPR32:
  235. case EF_FRV_FPR_NONE:
  236. case 0:
  237. break;
  238. default:
  239. return 0;
  240. }
  241. if ((hdr->e_flags & EF_FRV_MULADD) == EF_FRV_MULADD)
  242. if (PSR_IMPLE(psr) != PSR_IMPLE_FR405 &&
  243. PSR_IMPLE(psr) != PSR_IMPLE_FR451)
  244. return 0;
  245. switch (hdr->e_flags & EF_FRV_CPU_MASK) {
  246. case EF_FRV_CPU_GENERIC:
  247. break;
  248. case EF_FRV_CPU_FR300:
  249. case EF_FRV_CPU_SIMPLE:
  250. case EF_FRV_CPU_TOMCAT:
  251. default:
  252. return 0;
  253. case EF_FRV_CPU_FR400:
  254. if (PSR_IMPLE(psr) != PSR_IMPLE_FR401 &&
  255. PSR_IMPLE(psr) != PSR_IMPLE_FR405 &&
  256. PSR_IMPLE(psr) != PSR_IMPLE_FR451 &&
  257. PSR_IMPLE(psr) != PSR_IMPLE_FR551)
  258. return 0;
  259. break;
  260. case EF_FRV_CPU_FR450:
  261. if (PSR_IMPLE(psr) != PSR_IMPLE_FR451)
  262. return 0;
  263. break;
  264. case EF_FRV_CPU_FR500:
  265. if (PSR_IMPLE(psr) != PSR_IMPLE_FR501)
  266. return 0;
  267. break;
  268. case EF_FRV_CPU_FR550:
  269. if (PSR_IMPLE(psr) != PSR_IMPLE_FR551)
  270. return 0;
  271. break;
  272. }
  273. return 1;
  274. }
  275. int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpregs)
  276. {
  277. memcpy(fpregs,
  278. &current->thread.user->f,
  279. sizeof(current->thread.user->f));
  280. return 1;
  281. }