process.c 8.2 KB

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