process.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*
  2. * linux/arch/unicore32/kernel/process.c
  3. *
  4. * Code specific to PKUnity SoC and UniCore ISA
  5. *
  6. * Copyright (C) 2001-2010 GUAN Xue-tao
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <stdarg.h>
  13. #include <linux/module.h>
  14. #include <linux/sched.h>
  15. #include <linux/sched/debug.h>
  16. #include <linux/sched/task.h>
  17. #include <linux/sched/task_stack.h>
  18. #include <linux/kernel.h>
  19. #include <linux/mm.h>
  20. #include <linux/stddef.h>
  21. #include <linux/unistd.h>
  22. #include <linux/delay.h>
  23. #include <linux/reboot.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/init.h>
  26. #include <linux/cpu.h>
  27. #include <linux/elfcore.h>
  28. #include <linux/pm.h>
  29. #include <linux/tick.h>
  30. #include <linux/utsname.h>
  31. #include <linux/uaccess.h>
  32. #include <linux/random.h>
  33. #include <linux/gpio.h>
  34. #include <linux/stacktrace.h>
  35. #include <asm/cacheflush.h>
  36. #include <asm/processor.h>
  37. #include <asm/stacktrace.h>
  38. #include "setup.h"
  39. static const char * const processor_modes[] = {
  40. "UK00", "UK01", "UK02", "UK03", "UK04", "UK05", "UK06", "UK07",
  41. "UK08", "UK09", "UK0A", "UK0B", "UK0C", "UK0D", "UK0E", "UK0F",
  42. "USER", "REAL", "INTR", "PRIV", "UK14", "UK15", "UK16", "ABRT",
  43. "UK18", "UK19", "UK1A", "EXTN", "UK1C", "UK1D", "UK1E", "SUSR"
  44. };
  45. void arch_cpu_idle(void)
  46. {
  47. cpu_do_idle();
  48. local_irq_enable();
  49. }
  50. void machine_halt(void)
  51. {
  52. gpio_set_value(GPO_SOFT_OFF, 0);
  53. }
  54. /*
  55. * Function pointers to optional machine specific functions
  56. */
  57. void (*pm_power_off)(void) = NULL;
  58. EXPORT_SYMBOL(pm_power_off);
  59. void machine_power_off(void)
  60. {
  61. if (pm_power_off)
  62. pm_power_off();
  63. machine_halt();
  64. }
  65. void machine_restart(char *cmd)
  66. {
  67. /* Disable interrupts first */
  68. local_irq_disable();
  69. /*
  70. * Tell the mm system that we are going to reboot -
  71. * we may need it to insert some 1:1 mappings so that
  72. * soft boot works.
  73. */
  74. setup_mm_for_reboot();
  75. /* Clean and invalidate caches */
  76. flush_cache_all();
  77. /* Turn off caching */
  78. cpu_proc_fin();
  79. /* Push out any further dirty data, and ensure cache is empty */
  80. flush_cache_all();
  81. /*
  82. * Now handle reboot code.
  83. */
  84. if (reboot_mode == REBOOT_SOFT) {
  85. /* Jump into ROM at address 0xffff0000 */
  86. cpu_reset(VECTORS_BASE);
  87. } else {
  88. writel(0x00002001, PM_PLLSYSCFG); /* cpu clk = 250M */
  89. writel(0x00100800, PM_PLLDDRCFG); /* ddr clk = 44M */
  90. writel(0x00002001, PM_PLLVGACFG); /* vga clk = 250M */
  91. /* Use on-chip reset capability */
  92. /* following instructions must be in one icache line */
  93. __asm__ __volatile__(
  94. " .align 5\n\t"
  95. " stw %1, [%0]\n\t"
  96. "201: ldw r0, [%0]\n\t"
  97. " cmpsub.a r0, #0\n\t"
  98. " bne 201b\n\t"
  99. " stw %3, [%2]\n\t"
  100. " nop; nop; nop\n\t"
  101. /* prefetch 3 instructions at most */
  102. :
  103. : "r" (PM_PMCR),
  104. "r" (PM_PMCR_CFBSYS | PM_PMCR_CFBDDR
  105. | PM_PMCR_CFBVGA),
  106. "r" (RESETC_SWRR),
  107. "r" (RESETC_SWRR_SRB)
  108. : "r0", "memory");
  109. }
  110. /*
  111. * Whoops - the architecture was unable to reboot.
  112. * Tell the user!
  113. */
  114. mdelay(1000);
  115. printk(KERN_EMERG "Reboot failed -- System halted\n");
  116. do { } while (1);
  117. }
  118. void __show_regs(struct pt_regs *regs)
  119. {
  120. unsigned long flags;
  121. char buf[64];
  122. show_regs_print_info(KERN_DEFAULT);
  123. printk("PC is at %pS\n", (void *)instruction_pointer(regs));
  124. printk("LR is at %pS\n", (void *)regs->UCreg_lr);
  125. printk(KERN_DEFAULT "pc : [<%08lx>] lr : [<%08lx>] psr: %08lx\n"
  126. "sp : %08lx ip : %08lx fp : %08lx\n",
  127. regs->UCreg_pc, regs->UCreg_lr, regs->UCreg_asr,
  128. regs->UCreg_sp, regs->UCreg_ip, regs->UCreg_fp);
  129. printk(KERN_DEFAULT "r26: %08lx r25: %08lx r24: %08lx\n",
  130. regs->UCreg_26, regs->UCreg_25,
  131. regs->UCreg_24);
  132. printk(KERN_DEFAULT "r23: %08lx r22: %08lx r21: %08lx r20: %08lx\n",
  133. regs->UCreg_23, regs->UCreg_22,
  134. regs->UCreg_21, regs->UCreg_20);
  135. printk(KERN_DEFAULT "r19: %08lx r18: %08lx r17: %08lx r16: %08lx\n",
  136. regs->UCreg_19, regs->UCreg_18,
  137. regs->UCreg_17, regs->UCreg_16);
  138. printk(KERN_DEFAULT "r15: %08lx r14: %08lx r13: %08lx r12: %08lx\n",
  139. regs->UCreg_15, regs->UCreg_14,
  140. regs->UCreg_13, regs->UCreg_12);
  141. printk(KERN_DEFAULT "r11: %08lx r10: %08lx r9 : %08lx r8 : %08lx\n",
  142. regs->UCreg_11, regs->UCreg_10,
  143. regs->UCreg_09, regs->UCreg_08);
  144. printk(KERN_DEFAULT "r7 : %08lx r6 : %08lx r5 : %08lx r4 : %08lx\n",
  145. regs->UCreg_07, regs->UCreg_06,
  146. regs->UCreg_05, regs->UCreg_04);
  147. printk(KERN_DEFAULT "r3 : %08lx r2 : %08lx r1 : %08lx r0 : %08lx\n",
  148. regs->UCreg_03, regs->UCreg_02,
  149. regs->UCreg_01, regs->UCreg_00);
  150. flags = regs->UCreg_asr;
  151. buf[0] = flags & PSR_S_BIT ? 'S' : 's';
  152. buf[1] = flags & PSR_Z_BIT ? 'Z' : 'z';
  153. buf[2] = flags & PSR_C_BIT ? 'C' : 'c';
  154. buf[3] = flags & PSR_V_BIT ? 'V' : 'v';
  155. buf[4] = '\0';
  156. printk(KERN_DEFAULT "Flags: %s INTR o%s REAL o%s Mode %s Segment %s\n",
  157. buf, interrupts_enabled(regs) ? "n" : "ff",
  158. fast_interrupts_enabled(regs) ? "n" : "ff",
  159. processor_modes[processor_mode(regs)],
  160. uaccess_kernel() ? "kernel" : "user");
  161. {
  162. unsigned int ctrl;
  163. buf[0] = '\0';
  164. {
  165. unsigned int transbase;
  166. asm("movc %0, p0.c2, #0\n"
  167. : "=r" (transbase));
  168. snprintf(buf, sizeof(buf), " Table: %08x", transbase);
  169. }
  170. asm("movc %0, p0.c1, #0\n" : "=r" (ctrl));
  171. printk(KERN_DEFAULT "Control: %08x%s\n", ctrl, buf);
  172. }
  173. }
  174. void show_regs(struct pt_regs *regs)
  175. {
  176. printk(KERN_DEFAULT "\n");
  177. printk(KERN_DEFAULT "Pid: %d, comm: %20s\n",
  178. task_pid_nr(current), current->comm);
  179. __show_regs(regs);
  180. __backtrace();
  181. }
  182. void flush_thread(void)
  183. {
  184. struct thread_info *thread = current_thread_info();
  185. struct task_struct *tsk = current;
  186. memset(thread->used_cp, 0, sizeof(thread->used_cp));
  187. memset(&tsk->thread.debug, 0, sizeof(struct debug_info));
  188. #ifdef CONFIG_UNICORE_FPU_F64
  189. memset(&thread->fpstate, 0, sizeof(struct fp_state));
  190. #endif
  191. }
  192. void release_thread(struct task_struct *dead_task)
  193. {
  194. }
  195. asmlinkage void ret_from_fork(void) __asm__("ret_from_fork");
  196. asmlinkage void ret_from_kernel_thread(void) __asm__("ret_from_kernel_thread");
  197. int
  198. copy_thread(unsigned long clone_flags, unsigned long stack_start,
  199. unsigned long stk_sz, struct task_struct *p)
  200. {
  201. struct thread_info *thread = task_thread_info(p);
  202. struct pt_regs *childregs = task_pt_regs(p);
  203. memset(&thread->cpu_context, 0, sizeof(struct cpu_context_save));
  204. thread->cpu_context.sp = (unsigned long)childregs;
  205. if (unlikely(p->flags & PF_KTHREAD)) {
  206. thread->cpu_context.pc = (unsigned long)ret_from_kernel_thread;
  207. thread->cpu_context.r4 = stack_start;
  208. thread->cpu_context.r5 = stk_sz;
  209. memset(childregs, 0, sizeof(struct pt_regs));
  210. } else {
  211. thread->cpu_context.pc = (unsigned long)ret_from_fork;
  212. *childregs = *current_pt_regs();
  213. childregs->UCreg_00 = 0;
  214. if (stack_start)
  215. childregs->UCreg_sp = stack_start;
  216. if (clone_flags & CLONE_SETTLS)
  217. childregs->UCreg_16 = childregs->UCreg_03;
  218. }
  219. return 0;
  220. }
  221. /*
  222. * Fill in the task's elfregs structure for a core dump.
  223. */
  224. int dump_task_regs(struct task_struct *t, elf_gregset_t *elfregs)
  225. {
  226. elf_core_copy_regs(elfregs, task_pt_regs(t));
  227. return 1;
  228. }
  229. /*
  230. * fill in the fpe structure for a core dump...
  231. */
  232. int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fp)
  233. {
  234. struct thread_info *thread = current_thread_info();
  235. int used_math = thread->used_cp[1] | thread->used_cp[2];
  236. #ifdef CONFIG_UNICORE_FPU_F64
  237. if (used_math)
  238. memcpy(fp, &thread->fpstate, sizeof(*fp));
  239. #endif
  240. return used_math != 0;
  241. }
  242. EXPORT_SYMBOL(dump_fpu);
  243. unsigned long get_wchan(struct task_struct *p)
  244. {
  245. struct stackframe frame;
  246. int count = 0;
  247. if (!p || p == current || p->state == TASK_RUNNING)
  248. return 0;
  249. frame.fp = thread_saved_fp(p);
  250. frame.sp = thread_saved_sp(p);
  251. frame.lr = 0; /* recovered from the stack */
  252. frame.pc = thread_saved_pc(p);
  253. do {
  254. int ret = unwind_frame(&frame);
  255. if (ret < 0)
  256. return 0;
  257. if (!in_sched_functions(frame.pc))
  258. return frame.pc;
  259. } while ((count++) < 16);
  260. return 0;
  261. }
  262. unsigned long arch_randomize_brk(struct mm_struct *mm)
  263. {
  264. return randomize_page(mm->brk, 0x02000000);
  265. }
  266. /*
  267. * The vectors page is always readable from user space for the
  268. * atomic helpers and the signal restart code. Let's declare a mapping
  269. * for it so it is visible through ptrace and /proc/<pid>/mem.
  270. */
  271. int vectors_user_mapping(void)
  272. {
  273. struct mm_struct *mm = current->mm;
  274. return install_special_mapping(mm, 0xffff0000, PAGE_SIZE,
  275. VM_READ | VM_EXEC |
  276. VM_MAYREAD | VM_MAYEXEC |
  277. VM_DONTEXPAND | VM_DONTDUMP,
  278. NULL);
  279. }
  280. const char *arch_vma_name(struct vm_area_struct *vma)
  281. {
  282. return (vma->vm_start == 0xffff0000) ? "[vectors]" : NULL;
  283. }