process.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. /*
  145. * This gets called before we allocate a new thread and copy
  146. * the current task into it.
  147. */
  148. void prepare_to_copy(struct task_struct *tsk)
  149. {
  150. //unlazy_fpu(tsk);
  151. } /* end prepare_to_copy() */
  152. /*****************************************************************************/
  153. /*
  154. * set up the kernel stack and exception frames for a new process
  155. */
  156. int copy_thread(unsigned long clone_flags,
  157. unsigned long usp, unsigned long topstk,
  158. struct task_struct *p, struct pt_regs *regs)
  159. {
  160. struct pt_regs *childregs0, *childregs, *regs0;
  161. regs0 = __kernel_frame0_ptr;
  162. childregs0 = (struct pt_regs *)
  163. (task_stack_page(p) + THREAD_SIZE - FRV_FRAME0_SIZE);
  164. childregs = childregs0;
  165. /* set up the userspace frame (the only place that the USP is stored) */
  166. *childregs0 = *regs0;
  167. childregs0->gr8 = 0;
  168. childregs0->sp = usp;
  169. childregs0->next_frame = NULL;
  170. /* set up the return kernel frame if called from kernel_thread() */
  171. if (regs != regs0) {
  172. childregs--;
  173. *childregs = *regs;
  174. childregs->sp = (unsigned long) childregs0;
  175. childregs->next_frame = childregs0;
  176. childregs->gr15 = (unsigned long) task_thread_info(p);
  177. childregs->gr29 = (unsigned long) p;
  178. }
  179. p->set_child_tid = p->clear_child_tid = NULL;
  180. p->thread.frame = childregs;
  181. p->thread.curr = p;
  182. p->thread.sp = (unsigned long) childregs;
  183. p->thread.fp = 0;
  184. p->thread.lr = 0;
  185. p->thread.pc = (unsigned long) ret_from_fork;
  186. p->thread.frame0 = childregs0;
  187. /* the new TLS pointer is passed in as arg #5 to sys_clone() */
  188. if (clone_flags & CLONE_SETTLS)
  189. childregs->gr29 = childregs->gr12;
  190. save_user_regs(p->thread.user);
  191. return 0;
  192. } /* end copy_thread() */
  193. /*
  194. * sys_execve() executes a new program.
  195. */
  196. asmlinkage int sys_execve(const char __user *name,
  197. const char __user *const __user *argv,
  198. const char __user *const __user *envp)
  199. {
  200. int error;
  201. char * filename;
  202. filename = getname(name);
  203. error = PTR_ERR(filename);
  204. if (IS_ERR(filename))
  205. return error;
  206. error = do_execve(filename, argv, envp, __frame);
  207. putname(filename);
  208. return error;
  209. }
  210. unsigned long get_wchan(struct task_struct *p)
  211. {
  212. struct pt_regs *regs0;
  213. unsigned long fp, pc;
  214. unsigned long stack_limit;
  215. int count = 0;
  216. if (!p || p == current || p->state == TASK_RUNNING)
  217. return 0;
  218. stack_limit = (unsigned long) (p + 1);
  219. fp = p->thread.fp;
  220. regs0 = p->thread.frame0;
  221. do {
  222. if (fp < stack_limit || fp >= (unsigned long) regs0 || fp & 3)
  223. return 0;
  224. pc = ((unsigned long *) fp)[2];
  225. /* FIXME: This depends on the order of these functions. */
  226. if (!in_sched_functions(pc))
  227. return pc;
  228. fp = *(unsigned long *) fp;
  229. } while (count++ < 16);
  230. return 0;
  231. }
  232. unsigned long thread_saved_pc(struct task_struct *tsk)
  233. {
  234. /* Check whether the thread is blocked in resume() */
  235. if (in_sched_functions(tsk->thread.pc))
  236. return ((unsigned long *)tsk->thread.fp)[2];
  237. else
  238. return tsk->thread.pc;
  239. }
  240. int elf_check_arch(const struct elf32_hdr *hdr)
  241. {
  242. unsigned long hsr0 = __get_HSR(0);
  243. unsigned long psr = __get_PSR();
  244. if (hdr->e_machine != EM_FRV)
  245. return 0;
  246. switch (hdr->e_flags & EF_FRV_GPR_MASK) {
  247. case EF_FRV_GPR64:
  248. if ((hsr0 & HSR0_GRN) == HSR0_GRN_32)
  249. return 0;
  250. case EF_FRV_GPR32:
  251. case 0:
  252. break;
  253. default:
  254. return 0;
  255. }
  256. switch (hdr->e_flags & EF_FRV_FPR_MASK) {
  257. case EF_FRV_FPR64:
  258. if ((hsr0 & HSR0_FRN) == HSR0_FRN_32)
  259. return 0;
  260. case EF_FRV_FPR32:
  261. case EF_FRV_FPR_NONE:
  262. case 0:
  263. break;
  264. default:
  265. return 0;
  266. }
  267. if ((hdr->e_flags & EF_FRV_MULADD) == EF_FRV_MULADD)
  268. if (PSR_IMPLE(psr) != PSR_IMPLE_FR405 &&
  269. PSR_IMPLE(psr) != PSR_IMPLE_FR451)
  270. return 0;
  271. switch (hdr->e_flags & EF_FRV_CPU_MASK) {
  272. case EF_FRV_CPU_GENERIC:
  273. break;
  274. case EF_FRV_CPU_FR300:
  275. case EF_FRV_CPU_SIMPLE:
  276. case EF_FRV_CPU_TOMCAT:
  277. default:
  278. return 0;
  279. case EF_FRV_CPU_FR400:
  280. if (PSR_IMPLE(psr) != PSR_IMPLE_FR401 &&
  281. PSR_IMPLE(psr) != PSR_IMPLE_FR405 &&
  282. PSR_IMPLE(psr) != PSR_IMPLE_FR451 &&
  283. PSR_IMPLE(psr) != PSR_IMPLE_FR551)
  284. return 0;
  285. break;
  286. case EF_FRV_CPU_FR450:
  287. if (PSR_IMPLE(psr) != PSR_IMPLE_FR451)
  288. return 0;
  289. break;
  290. case EF_FRV_CPU_FR500:
  291. if (PSR_IMPLE(psr) != PSR_IMPLE_FR501)
  292. return 0;
  293. break;
  294. case EF_FRV_CPU_FR550:
  295. if (PSR_IMPLE(psr) != PSR_IMPLE_FR551)
  296. return 0;
  297. break;
  298. }
  299. return 1;
  300. }
  301. int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpregs)
  302. {
  303. memcpy(fpregs,
  304. &current->thread.user->f,
  305. sizeof(current->thread.user->f));
  306. return 1;
  307. }