process.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * arch/score/kernel/process.c
  3. *
  4. * Score Processor version.
  5. *
  6. * Copyright (C) 2009 Sunplus Core Technology Co., Ltd.
  7. * Chen Liqin <liqin.chen@sunplusct.com>
  8. * Lennox Wu <lennox.wu@sunplusct.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, see the file COPYING, or write
  22. * to the Free Software Foundation, Inc.,
  23. * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  24. */
  25. #include <linux/module.h>
  26. #include <linux/reboot.h>
  27. #include <linux/elfcore.h>
  28. #include <linux/pm.h>
  29. #include <linux/rcupdate.h>
  30. void (*pm_power_off)(void);
  31. EXPORT_SYMBOL(pm_power_off);
  32. /* If or when software machine-restart is implemented, add code here. */
  33. void machine_restart(char *command) {}
  34. /* If or when software machine-halt is implemented, add code here. */
  35. void machine_halt(void) {}
  36. /* If or when software machine-power-off is implemented, add code here. */
  37. void machine_power_off(void) {}
  38. /*
  39. * The idle thread. There's no useful work to be
  40. * done, so just try to conserve power and have a
  41. * low exit latency (ie sit in a loop waiting for
  42. * somebody to say that they'd like to reschedule)
  43. */
  44. void __noreturn cpu_idle(void)
  45. {
  46. /* endless idle loop with no priority at all */
  47. while (1) {
  48. rcu_idle_enter();
  49. while (!need_resched())
  50. barrier();
  51. rcu_idle_exit();
  52. schedule_preempt_disabled();
  53. }
  54. }
  55. void ret_from_fork(void);
  56. void start_thread(struct pt_regs *regs, unsigned long pc, unsigned long sp)
  57. {
  58. unsigned long status;
  59. /* New thread loses kernel privileges. */
  60. status = regs->cp0_psr & ~(KU_MASK);
  61. status |= KU_USER;
  62. regs->cp0_psr = status;
  63. regs->cp0_epc = pc;
  64. regs->regs[0] = sp;
  65. }
  66. void exit_thread(void) {}
  67. /*
  68. * When a process does an "exec", machine state like FPU and debug
  69. * registers need to be reset. This is a hook function for that.
  70. * Currently we don't have any such state to reset, so this is empty.
  71. */
  72. void flush_thread(void) {}
  73. /*
  74. * set up the kernel stack and exception frames for a new process
  75. */
  76. int copy_thread(unsigned long clone_flags, unsigned long usp,
  77. unsigned long unused,
  78. struct task_struct *p, struct pt_regs *regs)
  79. {
  80. struct thread_info *ti = task_thread_info(p);
  81. struct pt_regs *childregs = task_pt_regs(p);
  82. p->set_child_tid = NULL;
  83. p->clear_child_tid = NULL;
  84. *childregs = *regs;
  85. childregs->regs[7] = 0; /* Clear error flag */
  86. childregs->regs[4] = 0; /* Child gets zero as return value */
  87. regs->regs[4] = p->pid;
  88. if (childregs->cp0_psr & 0x8) { /* test kernel fork or user fork */
  89. childregs->regs[0] = usp; /* user fork */
  90. } else {
  91. childregs->regs[28] = (unsigned long) ti; /* kernel fork */
  92. childregs->regs[0] = (unsigned long) childregs;
  93. }
  94. p->thread.reg0 = (unsigned long) childregs;
  95. p->thread.reg3 = (unsigned long) ret_from_fork;
  96. p->thread.cp0_psr = 0;
  97. return 0;
  98. }
  99. /* Fill in the fpu structure for a core dump. */
  100. int dump_fpu(struct pt_regs *regs, elf_fpregset_t *r)
  101. {
  102. return 1;
  103. }
  104. static void __noreturn
  105. kernel_thread_helper(void *unused0, int (*fn)(void *),
  106. void *arg, void *unused1)
  107. {
  108. do_exit(fn(arg));
  109. }
  110. /*
  111. * Create a kernel thread.
  112. */
  113. long kernel_thread(int (*fn)(void *), void *arg, unsigned long flags)
  114. {
  115. struct pt_regs regs;
  116. memset(&regs, 0, sizeof(regs));
  117. regs.regs[6] = (unsigned long) arg;
  118. regs.regs[5] = (unsigned long) fn;
  119. regs.cp0_epc = (unsigned long) kernel_thread_helper;
  120. regs.cp0_psr = (regs.cp0_psr & ~(0x1|0x4|0x8)) | \
  121. ((regs.cp0_psr & 0x3) << 2);
  122. return do_fork(flags | CLONE_VM | CLONE_UNTRACED, \
  123. 0, &regs, 0, NULL, NULL);
  124. }
  125. unsigned long thread_saved_pc(struct task_struct *tsk)
  126. {
  127. return task_pt_regs(tsk)->cp0_epc;
  128. }
  129. unsigned long get_wchan(struct task_struct *task)
  130. {
  131. if (!task || task == current || task->state == TASK_RUNNING)
  132. return 0;
  133. if (!task_stack_page(task))
  134. return 0;
  135. return task_pt_regs(task)->cp0_epc;
  136. }
  137. unsigned long arch_align_stack(unsigned long sp)
  138. {
  139. return sp;
  140. }