process.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * OpenRISC process.c
  4. *
  5. * Linux architectural port borrowing liberally from similar works of
  6. * others. All original copyrights apply as per the original source
  7. * declaration.
  8. *
  9. * Modifications for the OpenRISC architecture:
  10. * Copyright (C) 2003 Matjaz Breskvar <phoenix@bsemi.com>
  11. * Copyright (C) 2010-2011 Jonas Bonn <jonas@southpole.se>
  12. *
  13. * This file handles the architecture-dependent parts of process handling...
  14. */
  15. #define __KERNEL_SYSCALLS__
  16. #include <stdarg.h>
  17. #include <linux/errno.h>
  18. #include <linux/sched.h>
  19. #include <linux/sched/debug.h>
  20. #include <linux/sched/task.h>
  21. #include <linux/sched/task_stack.h>
  22. #include <linux/kernel.h>
  23. #include <linux/export.h>
  24. #include <linux/mm.h>
  25. #include <linux/stddef.h>
  26. #include <linux/unistd.h>
  27. #include <linux/ptrace.h>
  28. #include <linux/slab.h>
  29. #include <linux/elfcore.h>
  30. #include <linux/interrupt.h>
  31. #include <linux/delay.h>
  32. #include <linux/init_task.h>
  33. #include <linux/mqueue.h>
  34. #include <linux/fs.h>
  35. #include <linux/uaccess.h>
  36. #include <asm/io.h>
  37. #include <asm/processor.h>
  38. #include <asm/spr_defs.h>
  39. #include <linux/smp.h>
  40. /*
  41. * Pointer to Current thread info structure.
  42. *
  43. * Used at user space -> kernel transitions.
  44. */
  45. struct thread_info *current_thread_info_set[NR_CPUS] = { &init_thread_info, };
  46. void machine_restart(void)
  47. {
  48. printk(KERN_INFO "*** MACHINE RESTART ***\n");
  49. __asm__("l.nop 1");
  50. }
  51. /*
  52. * Similar to machine_power_off, but don't shut off power. Add code
  53. * here to freeze the system for e.g. post-mortem debug purpose when
  54. * possible. This halt has nothing to do with the idle halt.
  55. */
  56. void machine_halt(void)
  57. {
  58. printk(KERN_INFO "*** MACHINE HALT ***\n");
  59. __asm__("l.nop 1");
  60. }
  61. /* If or when software power-off is implemented, add code here. */
  62. void machine_power_off(void)
  63. {
  64. printk(KERN_INFO "*** MACHINE POWER OFF ***\n");
  65. __asm__("l.nop 1");
  66. }
  67. /*
  68. * Send the doze signal to the cpu if available.
  69. * Make sure, that all interrupts are enabled
  70. */
  71. void arch_cpu_idle(void)
  72. {
  73. raw_local_irq_enable();
  74. if (mfspr(SPR_UPR) & SPR_UPR_PMP)
  75. mtspr(SPR_PMR, mfspr(SPR_PMR) | SPR_PMR_DME);
  76. }
  77. void (*pm_power_off) (void) = machine_power_off;
  78. EXPORT_SYMBOL(pm_power_off);
  79. /*
  80. * When a process does an "exec", machine state like FPU and debug
  81. * registers need to be reset. This is a hook function for that.
  82. * Currently we don't have any such state to reset, so this is empty.
  83. */
  84. void flush_thread(void)
  85. {
  86. }
  87. void show_regs(struct pt_regs *regs)
  88. {
  89. extern void show_registers(struct pt_regs *regs);
  90. show_regs_print_info(KERN_DEFAULT);
  91. /* __PHX__ cleanup this mess */
  92. show_registers(regs);
  93. }
  94. void release_thread(struct task_struct *dead_task)
  95. {
  96. }
  97. /*
  98. * Copy the thread-specific (arch specific) info from the current
  99. * process to the new one p
  100. */
  101. extern asmlinkage void ret_from_fork(void);
  102. /*
  103. * copy_thread
  104. * @clone_flags: flags
  105. * @usp: user stack pointer or fn for kernel thread
  106. * @arg: arg to fn for kernel thread; always NULL for userspace thread
  107. * @p: the newly created task
  108. * @tls: the Thread Local Storage pointer for the new process
  109. *
  110. * At the top of a newly initialized kernel stack are two stacked pt_reg
  111. * structures. The first (topmost) is the userspace context of the thread.
  112. * The second is the kernelspace context of the thread.
  113. *
  114. * A kernel thread will not be returning to userspace, so the topmost pt_regs
  115. * struct can be uninitialized; it _does_ need to exist, though, because
  116. * a kernel thread can become a userspace thread by doing a kernel_execve, in
  117. * which case the topmost context will be initialized and used for 'returning'
  118. * to userspace.
  119. *
  120. * The second pt_reg struct needs to be initialized to 'return' to
  121. * ret_from_fork. A kernel thread will need to set r20 to the address of
  122. * a function to call into (with arg in r22); userspace threads need to set
  123. * r20 to NULL in which case ret_from_fork will just continue a return to
  124. * userspace.
  125. *
  126. * A kernel thread 'fn' may return; this is effectively what happens when
  127. * kernel_execve is called. In that case, the userspace pt_regs must have
  128. * been initialized (which kernel_execve takes care of, see start_thread
  129. * below); ret_from_fork will then continue its execution causing the
  130. * 'kernel thread' to return to userspace as a userspace thread.
  131. */
  132. int
  133. copy_thread(unsigned long clone_flags, unsigned long usp, unsigned long arg,
  134. struct task_struct *p, unsigned long tls)
  135. {
  136. struct pt_regs *userregs;
  137. struct pt_regs *kregs;
  138. unsigned long sp = (unsigned long)task_stack_page(p) + THREAD_SIZE;
  139. unsigned long top_of_kernel_stack;
  140. top_of_kernel_stack = sp;
  141. /* Locate userspace context on stack... */
  142. sp -= STACK_FRAME_OVERHEAD; /* redzone */
  143. sp -= sizeof(struct pt_regs);
  144. userregs = (struct pt_regs *) sp;
  145. /* ...and kernel context */
  146. sp -= STACK_FRAME_OVERHEAD; /* redzone */
  147. sp -= sizeof(struct pt_regs);
  148. kregs = (struct pt_regs *)sp;
  149. if (unlikely(p->flags & PF_KTHREAD)) {
  150. memset(kregs, 0, sizeof(struct pt_regs));
  151. kregs->gpr[20] = usp; /* fn, kernel thread */
  152. kregs->gpr[22] = arg;
  153. } else {
  154. *userregs = *current_pt_regs();
  155. if (usp)
  156. userregs->sp = usp;
  157. /*
  158. * For CLONE_SETTLS set "tp" (r10) to the TLS pointer.
  159. */
  160. if (clone_flags & CLONE_SETTLS)
  161. userregs->gpr[10] = tls;
  162. userregs->gpr[11] = 0; /* Result from fork() */
  163. kregs->gpr[20] = 0; /* Userspace thread */
  164. }
  165. /*
  166. * _switch wants the kernel stack page in pt_regs->sp so that it
  167. * can restore it to thread_info->ksp... see _switch for details.
  168. */
  169. kregs->sp = top_of_kernel_stack;
  170. kregs->gpr[9] = (unsigned long)ret_from_fork;
  171. task_thread_info(p)->ksp = (unsigned long)kregs;
  172. return 0;
  173. }
  174. /*
  175. * Set up a thread for executing a new program
  176. */
  177. void start_thread(struct pt_regs *regs, unsigned long pc, unsigned long sp)
  178. {
  179. unsigned long sr = mfspr(SPR_SR) & ~SPR_SR_SM;
  180. memset(regs, 0, sizeof(struct pt_regs));
  181. regs->pc = pc;
  182. regs->sr = sr;
  183. regs->sp = sp;
  184. }
  185. extern struct thread_info *_switch(struct thread_info *old_ti,
  186. struct thread_info *new_ti);
  187. extern int lwa_flag;
  188. struct task_struct *__switch_to(struct task_struct *old,
  189. struct task_struct *new)
  190. {
  191. struct task_struct *last;
  192. struct thread_info *new_ti, *old_ti;
  193. unsigned long flags;
  194. local_irq_save(flags);
  195. /* current_set is an array of saved current pointers
  196. * (one for each cpu). we need them at user->kernel transition,
  197. * while we save them at kernel->user transition
  198. */
  199. new_ti = new->stack;
  200. old_ti = old->stack;
  201. lwa_flag = 0;
  202. current_thread_info_set[smp_processor_id()] = new_ti;
  203. last = (_switch(old_ti, new_ti))->task;
  204. local_irq_restore(flags);
  205. return last;
  206. }
  207. /*
  208. * Write out registers in core dump format, as defined by the
  209. * struct user_regs_struct
  210. */
  211. void dump_elf_thread(elf_greg_t *dest, struct pt_regs* regs)
  212. {
  213. dest[0] = 0; /* r0 */
  214. memcpy(dest+1, regs->gpr+1, 31*sizeof(unsigned long));
  215. dest[32] = regs->pc;
  216. dest[33] = regs->sr;
  217. dest[34] = 0;
  218. dest[35] = 0;
  219. }
  220. unsigned long get_wchan(struct task_struct *p)
  221. {
  222. /* TODO */
  223. return 0;
  224. }