process.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /*
  2. * OpenRISC process.c
  3. *
  4. * Linux architectural port borrowing liberally from similar works of
  5. * others. All original copyrights apply as per the original source
  6. * declaration.
  7. *
  8. * Modifications for the OpenRISC architecture:
  9. * Copyright (C) 2003 Matjaz Breskvar <phoenix@bsemi.com>
  10. * Copyright (C) 2010-2011 Jonas Bonn <jonas@southpole.se>
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License
  14. * as published by the Free Software Foundation; either version
  15. * 2 of the License, or (at your option) any later version.
  16. *
  17. * This file handles the architecture-dependent parts of process handling...
  18. */
  19. #define __KERNEL_SYSCALLS__
  20. #include <stdarg.h>
  21. #include <linux/errno.h>
  22. #include <linux/sched.h>
  23. #include <linux/kernel.h>
  24. #include <linux/module.h>
  25. #include <linux/mm.h>
  26. #include <linux/stddef.h>
  27. #include <linux/unistd.h>
  28. #include <linux/ptrace.h>
  29. #include <linux/slab.h>
  30. #include <linux/elfcore.h>
  31. #include <linux/interrupt.h>
  32. #include <linux/delay.h>
  33. #include <linux/init_task.h>
  34. #include <linux/mqueue.h>
  35. #include <linux/fs.h>
  36. #include <asm/uaccess.h>
  37. #include <asm/pgtable.h>
  38. #include <asm/io.h>
  39. #include <asm/processor.h>
  40. #include <asm/spr_defs.h>
  41. #include <linux/smp.h>
  42. /*
  43. * Pointer to Current thread info structure.
  44. *
  45. * Used at user space -> kernel transitions.
  46. */
  47. struct thread_info *current_thread_info_set[NR_CPUS] = { &init_thread_info, };
  48. void machine_restart(void)
  49. {
  50. printk(KERN_INFO "*** MACHINE RESTART ***\n");
  51. __asm__("l.nop 1");
  52. }
  53. /*
  54. * Similar to machine_power_off, but don't shut off power. Add code
  55. * here to freeze the system for e.g. post-mortem debug purpose when
  56. * possible. This halt has nothing to do with the idle halt.
  57. */
  58. void machine_halt(void)
  59. {
  60. printk(KERN_INFO "*** MACHINE HALT ***\n");
  61. __asm__("l.nop 1");
  62. }
  63. /* If or when software power-off is implemented, add code here. */
  64. void machine_power_off(void)
  65. {
  66. printk(KERN_INFO "*** MACHINE POWER OFF ***\n");
  67. __asm__("l.nop 1");
  68. }
  69. void (*pm_power_off) (void) = machine_power_off;
  70. /*
  71. * When a process does an "exec", machine state like FPU and debug
  72. * registers need to be reset. This is a hook function for that.
  73. * Currently we don't have any such state to reset, so this is empty.
  74. */
  75. void flush_thread(void)
  76. {
  77. }
  78. void show_regs(struct pt_regs *regs)
  79. {
  80. extern void show_registers(struct pt_regs *regs);
  81. /* __PHX__ cleanup this mess */
  82. show_registers(regs);
  83. }
  84. unsigned long thread_saved_pc(struct task_struct *t)
  85. {
  86. return (unsigned long)user_regs(t->stack)->pc;
  87. }
  88. void release_thread(struct task_struct *dead_task)
  89. {
  90. }
  91. /*
  92. * Copy the thread-specific (arch specific) info from the current
  93. * process to the new one p
  94. */
  95. extern asmlinkage void ret_from_fork(void);
  96. int
  97. copy_thread(unsigned long clone_flags, unsigned long usp,
  98. unsigned long unused, struct task_struct *p, struct pt_regs *regs)
  99. {
  100. struct pt_regs *childregs;
  101. struct pt_regs *kregs;
  102. unsigned long sp = (unsigned long)task_stack_page(p) + THREAD_SIZE;
  103. struct thread_info *ti;
  104. unsigned long top_of_kernel_stack;
  105. top_of_kernel_stack = sp;
  106. p->set_child_tid = p->clear_child_tid = NULL;
  107. /* Copy registers */
  108. /* redzone */
  109. sp -= STACK_FRAME_OVERHEAD;
  110. sp -= sizeof(struct pt_regs);
  111. childregs = (struct pt_regs *)sp;
  112. /* Copy parent registers */
  113. *childregs = *regs;
  114. if ((childregs->sr & SPR_SR_SM) == 1) {
  115. /* for kernel thread, set `current_thread_info'
  116. * and stackptr in new task
  117. */
  118. childregs->sp = (unsigned long)task_stack_page(p) + THREAD_SIZE;
  119. childregs->gpr[10] = (unsigned long)task_thread_info(p);
  120. } else {
  121. childregs->sp = usp;
  122. }
  123. childregs->gpr[11] = 0; /* Result from fork() */
  124. /*
  125. * The way this works is that at some point in the future
  126. * some task will call _switch to switch to the new task.
  127. * That will pop off the stack frame created below and start
  128. * the new task running at ret_from_fork. The new task will
  129. * do some house keeping and then return from the fork or clone
  130. * system call, using the stack frame created above.
  131. */
  132. /* redzone */
  133. sp -= STACK_FRAME_OVERHEAD;
  134. sp -= sizeof(struct pt_regs);
  135. kregs = (struct pt_regs *)sp;
  136. ti = task_thread_info(p);
  137. ti->ksp = sp;
  138. /* kregs->sp must store the location of the 'pre-switch' kernel stack
  139. * pointer... for a newly forked process, this is simply the top of
  140. * the kernel stack.
  141. */
  142. kregs->sp = top_of_kernel_stack;
  143. kregs->gpr[3] = (unsigned long)current; /* arg to schedule_tail */
  144. kregs->gpr[10] = (unsigned long)task_thread_info(p);
  145. kregs->gpr[9] = (unsigned long)ret_from_fork;
  146. return 0;
  147. }
  148. /*
  149. * Set up a thread for executing a new program
  150. */
  151. void start_thread(struct pt_regs *regs, unsigned long pc, unsigned long sp)
  152. {
  153. unsigned long sr = regs->sr & ~SPR_SR_SM;
  154. set_fs(USER_DS);
  155. memset(regs->gpr, 0, sizeof(regs->gpr));
  156. regs->pc = pc;
  157. regs->sr = sr;
  158. regs->sp = sp;
  159. /* printk("start thread, ksp = %lx\n", current_thread_info()->ksp);*/
  160. }
  161. /* Fill in the fpu structure for a core dump. */
  162. int dump_fpu(struct pt_regs *regs, elf_fpregset_t * fpu)
  163. {
  164. /* TODO */
  165. return 0;
  166. }
  167. extern struct thread_info *_switch(struct thread_info *old_ti,
  168. struct thread_info *new_ti);
  169. struct task_struct *__switch_to(struct task_struct *old,
  170. struct task_struct *new)
  171. {
  172. struct task_struct *last;
  173. struct thread_info *new_ti, *old_ti;
  174. unsigned long flags;
  175. local_irq_save(flags);
  176. /* current_set is an array of saved current pointers
  177. * (one for each cpu). we need them at user->kernel transition,
  178. * while we save them at kernel->user transition
  179. */
  180. new_ti = new->stack;
  181. old_ti = old->stack;
  182. current_thread_info_set[smp_processor_id()] = new_ti;
  183. last = (_switch(old_ti, new_ti))->task;
  184. local_irq_restore(flags);
  185. return last;
  186. }
  187. /*
  188. * Write out registers in core dump format, as defined by the
  189. * struct user_regs_struct
  190. */
  191. void dump_elf_thread(elf_greg_t *dest, struct pt_regs* regs)
  192. {
  193. dest[0] = 0; /* r0 */
  194. memcpy(dest+1, regs->gpr+1, 31*sizeof(unsigned long));
  195. dest[32] = regs->pc;
  196. dest[33] = regs->sr;
  197. dest[34] = 0;
  198. dest[35] = 0;
  199. }
  200. extern void _kernel_thread_helper(void);
  201. void __noreturn kernel_thread_helper(int (*fn) (void *), void *arg)
  202. {
  203. do_exit(fn(arg));
  204. }
  205. /*
  206. * Create a kernel thread.
  207. */
  208. int kernel_thread(int (*fn) (void *), void *arg, unsigned long flags)
  209. {
  210. struct pt_regs regs;
  211. memset(&regs, 0, sizeof(regs));
  212. regs.gpr[20] = (unsigned long)fn;
  213. regs.gpr[22] = (unsigned long)arg;
  214. regs.sr = mfspr(SPR_SR);
  215. regs.pc = (unsigned long)_kernel_thread_helper;
  216. return do_fork(flags | CLONE_VM | CLONE_UNTRACED,
  217. 0, &regs, 0, NULL, NULL);
  218. }
  219. /*
  220. * sys_execve() executes a new program.
  221. */
  222. asmlinkage long _sys_execve(const char __user *name,
  223. const char __user * const __user *argv,
  224. const char __user * const __user *envp,
  225. struct pt_regs *regs)
  226. {
  227. int error;
  228. char *filename;
  229. filename = getname(name);
  230. error = PTR_ERR(filename);
  231. if (IS_ERR(filename))
  232. goto out;
  233. error = do_execve(filename, argv, envp, regs);
  234. putname(filename);
  235. out:
  236. return error;
  237. }
  238. unsigned long get_wchan(struct task_struct *p)
  239. {
  240. /* TODO */
  241. return 0;
  242. }
  243. int kernel_execve(const char *filename, char *const argv[], char *const envp[])
  244. {
  245. register long __res asm("r11") = __NR_execve;
  246. register long __a asm("r3") = (long)(filename);
  247. register long __b asm("r4") = (long)(argv);
  248. register long __c asm("r5") = (long)(envp);
  249. __asm__ volatile ("l.sys 1"
  250. : "=r" (__res), "=r"(__a), "=r"(__b), "=r"(__c)
  251. : "0"(__res), "1"(__a), "2"(__b), "3"(__c)
  252. : "r6", "r7", "r8", "r12", "r13", "r15",
  253. "r17", "r19", "r21", "r23", "r25", "r27",
  254. "r29", "r31");
  255. __asm__ volatile ("l.nop");
  256. return __res;
  257. }