process.c 7.9 KB

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