process.c 8.2 KB

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