process.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Process creation support for Hexagon
  4. *
  5. * Copyright (c) 2010-2012, The Linux Foundation. All rights reserved.
  6. */
  7. #include <linux/sched.h>
  8. #include <linux/sched/debug.h>
  9. #include <linux/sched/task.h>
  10. #include <linux/sched/task_stack.h>
  11. #include <linux/types.h>
  12. #include <linux/module.h>
  13. #include <linux/tick.h>
  14. #include <linux/uaccess.h>
  15. #include <linux/slab.h>
  16. #include <linux/resume_user_mode.h>
  17. /*
  18. * Program thread launch. Often defined as a macro in processor.h,
  19. * but we're shooting for a small footprint and it's not an inner-loop
  20. * performance-critical operation.
  21. *
  22. * The Hexagon ABI specifies that R28 is zero'ed before program launch,
  23. * so that gets automatically done here. If we ever stop doing that here,
  24. * we'll probably want to define the ELF_PLAT_INIT macro.
  25. */
  26. void start_thread(struct pt_regs *regs, unsigned long pc, unsigned long sp)
  27. {
  28. /* We want to zero all data-containing registers. Is this overkill? */
  29. memset(regs, 0, sizeof(*regs));
  30. /* We might want to also zero all Processor registers here */
  31. pt_set_usermode(regs);
  32. pt_set_elr(regs, pc);
  33. pt_set_rte_sp(regs, sp);
  34. }
  35. /*
  36. * Spin, or better still, do a hardware or VM wait instruction
  37. * If hardware or VM offer wait termination even though interrupts
  38. * are disabled.
  39. */
  40. void arch_cpu_idle(void)
  41. {
  42. __vmwait();
  43. /* interrupts wake us up, but irqs are still disabled */
  44. raw_local_irq_enable();
  45. }
  46. /*
  47. * Copy architecture-specific thread state
  48. */
  49. int copy_thread(struct task_struct *p, const struct kernel_clone_args *args)
  50. {
  51. unsigned long clone_flags = args->flags;
  52. unsigned long usp = args->stack;
  53. unsigned long tls = args->tls;
  54. struct thread_info *ti = task_thread_info(p);
  55. struct hexagon_switch_stack *ss;
  56. struct pt_regs *childregs;
  57. asmlinkage void ret_from_fork(void);
  58. childregs = (struct pt_regs *) (((unsigned long) ti + THREAD_SIZE) -
  59. sizeof(*childregs));
  60. ti->regs = childregs;
  61. /*
  62. * Establish kernel stack pointer and initial PC for new thread
  63. * Note that unlike the usual situation, we do not copy the
  64. * parent's callee-saved here; those are in pt_regs and whatever
  65. * we leave here will be overridden on return to userland.
  66. */
  67. ss = (struct hexagon_switch_stack *) ((unsigned long) childregs -
  68. sizeof(*ss));
  69. ss->lr = (unsigned long)ret_from_fork;
  70. p->thread.switch_sp = ss;
  71. if (unlikely(args->fn)) {
  72. memset(childregs, 0, sizeof(struct pt_regs));
  73. /* r24 <- fn, r25 <- arg */
  74. ss->r24 = (unsigned long)args->fn;
  75. ss->r25 = (unsigned long)args->fn_arg;
  76. pt_set_kmode(childregs);
  77. return 0;
  78. }
  79. memcpy(childregs, current_pt_regs(), sizeof(*childregs));
  80. ss->r2524 = 0;
  81. if (usp)
  82. pt_set_rte_sp(childregs, usp);
  83. /* Child sees zero return value */
  84. childregs->r00 = 0;
  85. /*
  86. * The clone syscall has the C signature:
  87. * int [r0] clone(int flags [r0],
  88. * void *child_frame [r1],
  89. * void *parent_tid [r2],
  90. * void *child_tid [r3],
  91. * void *thread_control_block [r4]);
  92. * ugp is used to provide TLS support.
  93. */
  94. if (clone_flags & CLONE_SETTLS)
  95. childregs->ugp = tls;
  96. /*
  97. * Parent sees new pid -- not necessary, not even possible at
  98. * this point in the fork process
  99. */
  100. return 0;
  101. }
  102. /*
  103. * Some archs flush debug and FPU info here
  104. */
  105. void flush_thread(void)
  106. {
  107. }
  108. /*
  109. * The "wait channel" terminology is archaic, but what we want
  110. * is an identification of the point at which the scheduler
  111. * was invoked by a blocked thread.
  112. */
  113. unsigned long __get_wchan(struct task_struct *p)
  114. {
  115. unsigned long fp, pc;
  116. unsigned long stack_page;
  117. int count = 0;
  118. stack_page = (unsigned long)task_stack_page(p);
  119. fp = ((struct hexagon_switch_stack *)p->thread.switch_sp)->fp;
  120. do {
  121. if (fp < (stack_page + sizeof(struct thread_info)) ||
  122. fp >= (THREAD_SIZE - 8 + stack_page))
  123. return 0;
  124. pc = ((unsigned long *)fp)[1];
  125. if (!in_sched_functions(pc))
  126. return pc;
  127. fp = *(unsigned long *) fp;
  128. } while (count++ < 16);
  129. return 0;
  130. }
  131. /*
  132. * Called on the exit path of event entry; see vm_entry.S
  133. *
  134. * Interrupts will already be disabled.
  135. *
  136. * Returns 0 if there's no need to re-check for more work.
  137. */
  138. int do_work_pending(struct pt_regs *regs, u32 thread_info_flags)
  139. {
  140. if (!(thread_info_flags & _TIF_WORK_MASK)) {
  141. return 0;
  142. } /* shortcut -- no work to be done */
  143. local_irq_enable();
  144. if (thread_info_flags & _TIF_NEED_RESCHED) {
  145. schedule();
  146. return 1;
  147. }
  148. if (thread_info_flags & (_TIF_SIGPENDING | _TIF_NOTIFY_SIGNAL)) {
  149. do_signal(regs);
  150. return 1;
  151. }
  152. if (thread_info_flags & _TIF_NOTIFY_RESUME) {
  153. resume_user_mode_work(regs);
  154. return 1;
  155. }
  156. /* Should not even reach here */
  157. panic("%s: bad thread_info flags 0x%08x\n", __func__,
  158. thread_info_flags);
  159. }