process.c 7.2 KB

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