process.c 7.6 KB

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