process.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. /* endless idle loop with no priority at all */
  71. while (1) {
  72. while (!need_resched()) {
  73. irq_stat[smp_processor_id()].idle_timestamp = jiffies;
  74. if (!frv_dma_inprogress && idle)
  75. idle();
  76. }
  77. schedule();
  78. }
  79. }
  80. void machine_restart(char * __unused)
  81. {
  82. unsigned long reset_addr;
  83. #ifdef CONFIG_GDBSTUB
  84. gdbstub_exit(0);
  85. #endif
  86. if (PSR_IMPLE(__get_PSR()) == PSR_IMPLE_FR551)
  87. reset_addr = 0xfefff500;
  88. else
  89. reset_addr = 0xfeff0500;
  90. /* Software reset. */
  91. asm volatile(" dcef @(gr0,gr0),1 ! membar !"
  92. " sti %1,@(%0,0) !"
  93. " nop ! nop ! nop ! nop ! nop ! "
  94. " nop ! nop ! nop ! nop ! nop ! "
  95. " nop ! nop ! nop ! nop ! nop ! "
  96. " nop ! nop ! nop ! nop ! nop ! "
  97. : : "r" (reset_addr), "r" (1) );
  98. for (;;)
  99. ;
  100. }
  101. void machine_halt(void)
  102. {
  103. #ifdef CONFIG_GDBSTUB
  104. gdbstub_exit(0);
  105. #endif
  106. for (;;);
  107. }
  108. void machine_power_off(void)
  109. {
  110. #ifdef CONFIG_GDBSTUB
  111. gdbstub_exit(0);
  112. #endif
  113. for (;;);
  114. }
  115. void flush_thread(void)
  116. {
  117. #if 0 //ndef NO_FPU
  118. unsigned long zero = 0;
  119. #endif
  120. set_fs(USER_DS);
  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. /*
  157. * This gets called before we allocate a new thread and copy
  158. * the current task into it.
  159. */
  160. void prepare_to_copy(struct task_struct *tsk)
  161. {
  162. //unlazy_fpu(tsk);
  163. } /* end prepare_to_copy() */
  164. /*****************************************************************************/
  165. /*
  166. * set up the kernel stack and exception frames for a new process
  167. */
  168. int copy_thread(int nr, unsigned long clone_flags,
  169. unsigned long usp, unsigned long topstk,
  170. struct task_struct *p, struct pt_regs *regs)
  171. {
  172. struct pt_regs *childregs0, *childregs, *regs0;
  173. regs0 = __kernel_frame0_ptr;
  174. childregs0 = (struct pt_regs *)
  175. ((unsigned long) p->thread_info + THREAD_SIZE - USER_CONTEXT_SIZE);
  176. childregs = childregs0;
  177. /* set up the userspace frame (the only place that the USP is stored) */
  178. *childregs0 = *regs0;
  179. childregs0->gr8 = 0;
  180. childregs0->sp = usp;
  181. childregs0->next_frame = NULL;
  182. /* set up the return kernel frame if called from kernel_thread() */
  183. if (regs != regs0) {
  184. childregs--;
  185. *childregs = *regs;
  186. childregs->sp = (unsigned long) childregs0;
  187. childregs->next_frame = childregs0;
  188. childregs->gr15 = (unsigned long) p->thread_info;
  189. childregs->gr29 = (unsigned long) p;
  190. }
  191. p->set_child_tid = p->clear_child_tid = NULL;
  192. p->thread.frame = childregs;
  193. p->thread.curr = p;
  194. p->thread.sp = (unsigned long) childregs;
  195. p->thread.fp = 0;
  196. p->thread.lr = 0;
  197. p->thread.pc = (unsigned long) ret_from_fork;
  198. p->thread.frame0 = childregs0;
  199. /* the new TLS pointer is passed in as arg #5 to sys_clone() */
  200. if (clone_flags & CLONE_SETTLS)
  201. childregs->gr29 = childregs->gr12;
  202. save_user_regs(p->thread.user);
  203. return 0;
  204. } /* end copy_thread() */
  205. /*
  206. * fill in the user structure for a core dump..
  207. */
  208. void dump_thread(struct pt_regs *regs, struct user *dump)
  209. {
  210. #if 0
  211. /* changed the size calculations - should hopefully work better. lbt */
  212. dump->magic = CMAGIC;
  213. dump->start_code = 0;
  214. dump->start_stack = user_stack(regs) & ~(PAGE_SIZE - 1);
  215. dump->u_tsize = ((unsigned long) current->mm->end_code) >> PAGE_SHIFT;
  216. dump->u_dsize = ((unsigned long) (current->mm->brk + (PAGE_SIZE-1))) >> PAGE_SHIFT;
  217. dump->u_dsize -= dump->u_tsize;
  218. dump->u_ssize = 0;
  219. if (dump->start_stack < TASK_SIZE)
  220. dump->u_ssize = ((unsigned long) (TASK_SIZE - dump->start_stack)) >> PAGE_SHIFT;
  221. dump->regs = *(struct user_context *) regs;
  222. #endif
  223. }
  224. /*
  225. * sys_execve() executes a new program.
  226. */
  227. asmlinkage int sys_execve(char *name, char **argv, char **envp)
  228. {
  229. int error;
  230. char * filename;
  231. lock_kernel();
  232. filename = getname(name);
  233. error = PTR_ERR(filename);
  234. if (IS_ERR(filename))
  235. goto out;
  236. error = do_execve(filename, argv, envp, __frame);
  237. putname(filename);
  238. out:
  239. unlock_kernel();
  240. return error;
  241. }
  242. unsigned long get_wchan(struct task_struct *p)
  243. {
  244. struct pt_regs *regs0;
  245. unsigned long fp, pc;
  246. unsigned long stack_limit;
  247. int count = 0;
  248. if (!p || p == current || p->state == TASK_RUNNING)
  249. return 0;
  250. stack_limit = (unsigned long) (p + 1);
  251. fp = p->thread.fp;
  252. regs0 = p->thread.frame0;
  253. do {
  254. if (fp < stack_limit || fp >= (unsigned long) regs0 || fp & 3)
  255. return 0;
  256. pc = ((unsigned long *) fp)[2];
  257. /* FIXME: This depends on the order of these functions. */
  258. if (!in_sched_functions(pc))
  259. return pc;
  260. fp = *(unsigned long *) fp;
  261. } while (count++ < 16);
  262. return 0;
  263. }
  264. unsigned long thread_saved_pc(struct task_struct *tsk)
  265. {
  266. /* Check whether the thread is blocked in resume() */
  267. if (in_sched_functions(tsk->thread.pc))
  268. return ((unsigned long *)tsk->thread.fp)[2];
  269. else
  270. return tsk->thread.pc;
  271. }
  272. int elf_check_arch(const struct elf32_hdr *hdr)
  273. {
  274. unsigned long hsr0 = __get_HSR(0);
  275. unsigned long psr = __get_PSR();
  276. if (hdr->e_machine != EM_FRV)
  277. return 0;
  278. switch (hdr->e_flags & EF_FRV_GPR_MASK) {
  279. case EF_FRV_GPR64:
  280. if ((hsr0 & HSR0_GRN) == HSR0_GRN_32)
  281. return 0;
  282. case EF_FRV_GPR32:
  283. case 0:
  284. break;
  285. default:
  286. return 0;
  287. }
  288. switch (hdr->e_flags & EF_FRV_FPR_MASK) {
  289. case EF_FRV_FPR64:
  290. if ((hsr0 & HSR0_FRN) == HSR0_FRN_32)
  291. return 0;
  292. case EF_FRV_FPR32:
  293. case EF_FRV_FPR_NONE:
  294. case 0:
  295. break;
  296. default:
  297. return 0;
  298. }
  299. if ((hdr->e_flags & EF_FRV_MULADD) == EF_FRV_MULADD)
  300. if (PSR_IMPLE(psr) != PSR_IMPLE_FR405 &&
  301. PSR_IMPLE(psr) != PSR_IMPLE_FR451)
  302. return 0;
  303. switch (hdr->e_flags & EF_FRV_CPU_MASK) {
  304. case EF_FRV_CPU_GENERIC:
  305. break;
  306. case EF_FRV_CPU_FR300:
  307. case EF_FRV_CPU_SIMPLE:
  308. case EF_FRV_CPU_TOMCAT:
  309. default:
  310. return 0;
  311. case EF_FRV_CPU_FR400:
  312. if (PSR_IMPLE(psr) != PSR_IMPLE_FR401 &&
  313. PSR_IMPLE(psr) != PSR_IMPLE_FR405 &&
  314. PSR_IMPLE(psr) != PSR_IMPLE_FR451 &&
  315. PSR_IMPLE(psr) != PSR_IMPLE_FR551)
  316. return 0;
  317. break;
  318. case EF_FRV_CPU_FR450:
  319. if (PSR_IMPLE(psr) != PSR_IMPLE_FR451)
  320. return 0;
  321. break;
  322. case EF_FRV_CPU_FR500:
  323. if (PSR_IMPLE(psr) != PSR_IMPLE_FR501)
  324. return 0;
  325. break;
  326. case EF_FRV_CPU_FR550:
  327. if (PSR_IMPLE(psr) != PSR_IMPLE_FR551)
  328. return 0;
  329. break;
  330. }
  331. return 1;
  332. }