process.c 8.7 KB

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