process.c 8.4 KB

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