process.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (C) 2018 Hangzhou C-SKY Microsystems co.,ltd.
  3. #include <linux/module.h>
  4. #include <linux/version.h>
  5. #include <linux/sched.h>
  6. #include <linux/sched/task_stack.h>
  7. #include <linux/sched/debug.h>
  8. #include <linux/delay.h>
  9. #include <linux/kallsyms.h>
  10. #include <linux/uaccess.h>
  11. #include <linux/ptrace.h>
  12. #include <asm/elf.h>
  13. #include <abi/reg_ops.h>
  14. struct cpuinfo_csky cpu_data[NR_CPUS];
  15. asmlinkage void ret_from_fork(void);
  16. asmlinkage void ret_from_kernel_thread(void);
  17. /*
  18. * Some archs flush debug and FPU info here
  19. */
  20. void flush_thread(void){}
  21. /*
  22. * Return saved PC from a blocked thread
  23. */
  24. unsigned long thread_saved_pc(struct task_struct *tsk)
  25. {
  26. struct switch_stack *sw = (struct switch_stack *)tsk->thread.ksp;
  27. return sw->r15;
  28. }
  29. int copy_thread(unsigned long clone_flags,
  30. unsigned long usp,
  31. unsigned long kthread_arg,
  32. struct task_struct *p)
  33. {
  34. struct switch_stack *childstack;
  35. struct pt_regs *childregs = task_pt_regs(p);
  36. #ifdef CONFIG_CPU_HAS_FPU
  37. save_to_user_fp(&p->thread.user_fp);
  38. #endif
  39. childstack = ((struct switch_stack *) childregs) - 1;
  40. memset(childstack, 0, sizeof(struct switch_stack));
  41. /* setup ksp for switch_to !!! */
  42. p->thread.ksp = (unsigned long)childstack;
  43. if (unlikely(p->flags & PF_KTHREAD)) {
  44. memset(childregs, 0, sizeof(struct pt_regs));
  45. childstack->r15 = (unsigned long) ret_from_kernel_thread;
  46. childstack->r8 = kthread_arg;
  47. childstack->r9 = usp;
  48. childregs->sr = mfcr("psr");
  49. } else {
  50. *childregs = *(current_pt_regs());
  51. if (usp)
  52. childregs->usp = usp;
  53. if (clone_flags & CLONE_SETTLS)
  54. task_thread_info(p)->tp_value = childregs->tls
  55. = childregs->regs[0];
  56. childregs->a0 = 0;
  57. childstack->r15 = (unsigned long) ret_from_fork;
  58. }
  59. return 0;
  60. }
  61. /* Fill in the fpu structure for a core dump. */
  62. int dump_fpu(struct pt_regs *regs, struct user_fp *fpu)
  63. {
  64. memcpy(fpu, &current->thread.user_fp, sizeof(*fpu));
  65. return 1;
  66. }
  67. EXPORT_SYMBOL(dump_fpu);
  68. int dump_task_regs(struct task_struct *tsk, elf_gregset_t *pr_regs)
  69. {
  70. struct pt_regs *regs = task_pt_regs(tsk);
  71. /* NOTE: usp is error value. */
  72. ELF_CORE_COPY_REGS((*pr_regs), regs)
  73. return 1;
  74. }
  75. unsigned long get_wchan(struct task_struct *p)
  76. {
  77. unsigned long esp, pc;
  78. unsigned long stack_page;
  79. int count = 0;
  80. if (!p || p == current || p->state == TASK_RUNNING)
  81. return 0;
  82. stack_page = (unsigned long)p;
  83. esp = p->thread.esp0;
  84. do {
  85. if (esp < stack_page+sizeof(struct task_struct) ||
  86. esp >= 8184+stack_page)
  87. return 0;
  88. /*FIXME: There's may be error here!*/
  89. pc = ((unsigned long *)esp)[1];
  90. /* FIXME: This depends on the order of these functions. */
  91. if (!in_sched_functions(pc))
  92. return pc;
  93. esp = *(unsigned long *) esp;
  94. } while (count++ < 16);
  95. return 0;
  96. }
  97. EXPORT_SYMBOL(get_wchan);
  98. #ifndef CONFIG_CPU_PM_NONE
  99. void arch_cpu_idle(void)
  100. {
  101. #ifdef CONFIG_CPU_PM_WAIT
  102. asm volatile("wait\n");
  103. #endif
  104. #ifdef CONFIG_CPU_PM_DOZE
  105. asm volatile("doze\n");
  106. #endif
  107. #ifdef CONFIG_CPU_PM_STOP
  108. asm volatile("stop\n");
  109. #endif
  110. local_irq_enable();
  111. }
  112. #endif