process.c 7.7 KB

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