process.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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/kernel.h>
  16. #include <linux/mm.h>
  17. #include <linux/stddef.h>
  18. #include <linux/unistd.h>
  19. #include <linux/delay.h>
  20. #include <linux/reboot.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/kallsyms.h>
  23. #include <linux/init.h>
  24. #include <linux/cpu.h>
  25. #include <linux/elfcore.h>
  26. #include <linux/pm.h>
  27. #include <linux/tick.h>
  28. #include <linux/utsname.h>
  29. #include <linux/uaccess.h>
  30. #include <linux/random.h>
  31. #include <linux/gpio.h>
  32. #include <linux/stacktrace.h>
  33. #include <asm/cacheflush.h>
  34. #include <asm/processor.h>
  35. #include <asm/system.h>
  36. #include <asm/stacktrace.h>
  37. #include "setup.h"
  38. static const char * const processor_modes[] = {
  39. "UK00", "UK01", "UK02", "UK03", "UK04", "UK05", "UK06", "UK07",
  40. "UK08", "UK09", "UK0A", "UK0B", "UK0C", "UK0D", "UK0E", "UK0F",
  41. "USER", "REAL", "INTR", "PRIV", "UK14", "UK15", "UK16", "ABRT",
  42. "UK18", "UK19", "UK1A", "EXTN", "UK1C", "UK1D", "UK1E", "SUSR"
  43. };
  44. /*
  45. * The idle thread, has rather strange semantics for calling pm_idle,
  46. * but this is what x86 does and we need to do the same, so that
  47. * things like cpuidle get called in the same way.
  48. */
  49. void cpu_idle(void)
  50. {
  51. /* endless idle loop with no priority at all */
  52. while (1) {
  53. tick_nohz_idle_enter();
  54. rcu_idle_enter();
  55. while (!need_resched()) {
  56. local_irq_disable();
  57. stop_critical_timings();
  58. cpu_do_idle();
  59. local_irq_enable();
  60. start_critical_timings();
  61. }
  62. rcu_idle_exit();
  63. tick_nohz_idle_exit();
  64. preempt_enable_no_resched();
  65. schedule();
  66. preempt_disable();
  67. }
  68. }
  69. static char reboot_mode = 'h';
  70. int __init reboot_setup(char *str)
  71. {
  72. reboot_mode = str[0];
  73. return 1;
  74. }
  75. __setup("reboot=", reboot_setup);
  76. void machine_halt(void)
  77. {
  78. gpio_set_value(GPO_SOFT_OFF, 0);
  79. }
  80. /*
  81. * Function pointers to optional machine specific functions
  82. */
  83. void (*pm_power_off)(void) = NULL;
  84. void machine_power_off(void)
  85. {
  86. if (pm_power_off)
  87. pm_power_off();
  88. machine_halt();
  89. }
  90. void machine_restart(char *cmd)
  91. {
  92. /* Disable interrupts first */
  93. local_irq_disable();
  94. /*
  95. * Tell the mm system that we are going to reboot -
  96. * we may need it to insert some 1:1 mappings so that
  97. * soft boot works.
  98. */
  99. setup_mm_for_reboot(reboot_mode);
  100. /* Clean and invalidate caches */
  101. flush_cache_all();
  102. /* Turn off caching */
  103. cpu_proc_fin();
  104. /* Push out any further dirty data, and ensure cache is empty */
  105. flush_cache_all();
  106. /*
  107. * Now handle reboot code.
  108. */
  109. if (reboot_mode == 's') {
  110. /* Jump into ROM at address 0xffff0000 */
  111. cpu_reset(VECTORS_BASE);
  112. } else {
  113. writel(0x00002001, PM_PLLSYSCFG); /* cpu clk = 250M */
  114. writel(0x00100800, PM_PLLDDRCFG); /* ddr clk = 44M */
  115. writel(0x00002001, PM_PLLVGACFG); /* vga clk = 250M */
  116. /* Use on-chip reset capability */
  117. /* following instructions must be in one icache line */
  118. __asm__ __volatile__(
  119. " .align 5\n\t"
  120. " stw %1, [%0]\n\t"
  121. "201: ldw r0, [%0]\n\t"
  122. " cmpsub.a r0, #0\n\t"
  123. " bne 201b\n\t"
  124. " stw %3, [%2]\n\t"
  125. " nop; nop; nop\n\t"
  126. /* prefetch 3 instructions at most */
  127. :
  128. : "r" (PM_PMCR),
  129. "r" (PM_PMCR_CFBSYS | PM_PMCR_CFBDDR
  130. | PM_PMCR_CFBVGA),
  131. "r" (RESETC_SWRR),
  132. "r" (RESETC_SWRR_SRB)
  133. : "r0", "memory");
  134. }
  135. /*
  136. * Whoops - the architecture was unable to reboot.
  137. * Tell the user!
  138. */
  139. mdelay(1000);
  140. printk(KERN_EMERG "Reboot failed -- System halted\n");
  141. do { } while (1);
  142. }
  143. void __show_regs(struct pt_regs *regs)
  144. {
  145. unsigned long flags;
  146. char buf[64];
  147. printk(KERN_DEFAULT "CPU: %d %s (%s %.*s)\n",
  148. raw_smp_processor_id(), print_tainted(),
  149. init_utsname()->release,
  150. (int)strcspn(init_utsname()->version, " "),
  151. init_utsname()->version);
  152. print_symbol("PC is at %s\n", instruction_pointer(regs));
  153. print_symbol("LR is at %s\n", regs->UCreg_lr);
  154. printk(KERN_DEFAULT "pc : [<%08lx>] lr : [<%08lx>] psr: %08lx\n"
  155. "sp : %08lx ip : %08lx fp : %08lx\n",
  156. regs->UCreg_pc, regs->UCreg_lr, regs->UCreg_asr,
  157. regs->UCreg_sp, regs->UCreg_ip, regs->UCreg_fp);
  158. printk(KERN_DEFAULT "r26: %08lx r25: %08lx r24: %08lx\n",
  159. regs->UCreg_26, regs->UCreg_25,
  160. regs->UCreg_24);
  161. printk(KERN_DEFAULT "r23: %08lx r22: %08lx r21: %08lx r20: %08lx\n",
  162. regs->UCreg_23, regs->UCreg_22,
  163. regs->UCreg_21, regs->UCreg_20);
  164. printk(KERN_DEFAULT "r19: %08lx r18: %08lx r17: %08lx r16: %08lx\n",
  165. regs->UCreg_19, regs->UCreg_18,
  166. regs->UCreg_17, regs->UCreg_16);
  167. printk(KERN_DEFAULT "r15: %08lx r14: %08lx r13: %08lx r12: %08lx\n",
  168. regs->UCreg_15, regs->UCreg_14,
  169. regs->UCreg_13, regs->UCreg_12);
  170. printk(KERN_DEFAULT "r11: %08lx r10: %08lx r9 : %08lx r8 : %08lx\n",
  171. regs->UCreg_11, regs->UCreg_10,
  172. regs->UCreg_09, regs->UCreg_08);
  173. printk(KERN_DEFAULT "r7 : %08lx r6 : %08lx r5 : %08lx r4 : %08lx\n",
  174. regs->UCreg_07, regs->UCreg_06,
  175. regs->UCreg_05, regs->UCreg_04);
  176. printk(KERN_DEFAULT "r3 : %08lx r2 : %08lx r1 : %08lx r0 : %08lx\n",
  177. regs->UCreg_03, regs->UCreg_02,
  178. regs->UCreg_01, regs->UCreg_00);
  179. flags = regs->UCreg_asr;
  180. buf[0] = flags & PSR_S_BIT ? 'S' : 's';
  181. buf[1] = flags & PSR_Z_BIT ? 'Z' : 'z';
  182. buf[2] = flags & PSR_C_BIT ? 'C' : 'c';
  183. buf[3] = flags & PSR_V_BIT ? 'V' : 'v';
  184. buf[4] = '\0';
  185. printk(KERN_DEFAULT "Flags: %s INTR o%s REAL o%s Mode %s Segment %s\n",
  186. buf, interrupts_enabled(regs) ? "n" : "ff",
  187. fast_interrupts_enabled(regs) ? "n" : "ff",
  188. processor_modes[processor_mode(regs)],
  189. segment_eq(get_fs(), get_ds()) ? "kernel" : "user");
  190. {
  191. unsigned int ctrl;
  192. buf[0] = '\0';
  193. {
  194. unsigned int transbase;
  195. asm("movc %0, p0.c2, #0\n"
  196. : "=r" (transbase));
  197. snprintf(buf, sizeof(buf), " Table: %08x", transbase);
  198. }
  199. asm("movc %0, p0.c1, #0\n" : "=r" (ctrl));
  200. printk(KERN_DEFAULT "Control: %08x%s\n", ctrl, buf);
  201. }
  202. }
  203. void show_regs(struct pt_regs *regs)
  204. {
  205. printk(KERN_DEFAULT "\n");
  206. printk(KERN_DEFAULT "Pid: %d, comm: %20s\n",
  207. task_pid_nr(current), current->comm);
  208. __show_regs(regs);
  209. __backtrace();
  210. }
  211. /*
  212. * Free current thread data structures etc..
  213. */
  214. void exit_thread(void)
  215. {
  216. }
  217. void flush_thread(void)
  218. {
  219. struct thread_info *thread = current_thread_info();
  220. struct task_struct *tsk = current;
  221. memset(thread->used_cp, 0, sizeof(thread->used_cp));
  222. memset(&tsk->thread.debug, 0, sizeof(struct debug_info));
  223. #ifdef CONFIG_UNICORE_FPU_F64
  224. memset(&thread->fpstate, 0, sizeof(struct fp_state));
  225. #endif
  226. }
  227. void release_thread(struct task_struct *dead_task)
  228. {
  229. }
  230. asmlinkage void ret_from_fork(void) __asm__("ret_from_fork");
  231. int
  232. copy_thread(unsigned long clone_flags, unsigned long stack_start,
  233. unsigned long stk_sz, struct task_struct *p, struct pt_regs *regs)
  234. {
  235. struct thread_info *thread = task_thread_info(p);
  236. struct pt_regs *childregs = task_pt_regs(p);
  237. *childregs = *regs;
  238. childregs->UCreg_00 = 0;
  239. childregs->UCreg_sp = stack_start;
  240. memset(&thread->cpu_context, 0, sizeof(struct cpu_context_save));
  241. thread->cpu_context.sp = (unsigned long)childregs;
  242. thread->cpu_context.pc = (unsigned long)ret_from_fork;
  243. if (clone_flags & CLONE_SETTLS)
  244. childregs->UCreg_16 = regs->UCreg_03;
  245. return 0;
  246. }
  247. /*
  248. * Fill in the task's elfregs structure for a core dump.
  249. */
  250. int dump_task_regs(struct task_struct *t, elf_gregset_t *elfregs)
  251. {
  252. elf_core_copy_regs(elfregs, task_pt_regs(t));
  253. return 1;
  254. }
  255. /*
  256. * fill in the fpe structure for a core dump...
  257. */
  258. int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fp)
  259. {
  260. struct thread_info *thread = current_thread_info();
  261. int used_math = thread->used_cp[1] | thread->used_cp[2];
  262. #ifdef CONFIG_UNICORE_FPU_F64
  263. if (used_math)
  264. memcpy(fp, &thread->fpstate, sizeof(*fp));
  265. #endif
  266. return used_math != 0;
  267. }
  268. EXPORT_SYMBOL(dump_fpu);
  269. /*
  270. * Shuffle the argument into the correct register before calling the
  271. * thread function. r1 is the thread argument, r2 is the pointer to
  272. * the thread function, and r3 points to the exit function.
  273. */
  274. asm(".pushsection .text\n"
  275. " .align\n"
  276. " .type kernel_thread_helper, #function\n"
  277. "kernel_thread_helper:\n"
  278. " mov.a asr, r7\n"
  279. " mov r0, r4\n"
  280. " mov lr, r6\n"
  281. " mov pc, r5\n"
  282. " .size kernel_thread_helper, . - kernel_thread_helper\n"
  283. " .popsection");
  284. /*
  285. * Create a kernel thread.
  286. */
  287. pid_t kernel_thread(int (*fn)(void *), void *arg, unsigned long flags)
  288. {
  289. struct pt_regs regs;
  290. memset(&regs, 0, sizeof(regs));
  291. regs.UCreg_04 = (unsigned long)arg;
  292. regs.UCreg_05 = (unsigned long)fn;
  293. regs.UCreg_06 = (unsigned long)do_exit;
  294. regs.UCreg_07 = PRIV_MODE;
  295. regs.UCreg_pc = (unsigned long)kernel_thread_helper;
  296. regs.UCreg_asr = regs.UCreg_07 | PSR_I_BIT;
  297. return do_fork(flags|CLONE_VM|CLONE_UNTRACED, 0, &regs, 0, NULL, NULL);
  298. }
  299. EXPORT_SYMBOL(kernel_thread);
  300. unsigned long get_wchan(struct task_struct *p)
  301. {
  302. struct stackframe frame;
  303. int count = 0;
  304. if (!p || p == current || p->state == TASK_RUNNING)
  305. return 0;
  306. frame.fp = thread_saved_fp(p);
  307. frame.sp = thread_saved_sp(p);
  308. frame.lr = 0; /* recovered from the stack */
  309. frame.pc = thread_saved_pc(p);
  310. do {
  311. int ret = unwind_frame(&frame);
  312. if (ret < 0)
  313. return 0;
  314. if (!in_sched_functions(frame.pc))
  315. return frame.pc;
  316. } while ((count++) < 16);
  317. return 0;
  318. }
  319. unsigned long arch_randomize_brk(struct mm_struct *mm)
  320. {
  321. unsigned long range_end = mm->brk + 0x02000000;
  322. return randomize_range(mm->brk, range_end, 0) ? : mm->brk;
  323. }
  324. /*
  325. * The vectors page is always readable from user space for the
  326. * atomic helpers and the signal restart code. Let's declare a mapping
  327. * for it so it is visible through ptrace and /proc/<pid>/mem.
  328. */
  329. int vectors_user_mapping(void)
  330. {
  331. struct mm_struct *mm = current->mm;
  332. return install_special_mapping(mm, 0xffff0000, PAGE_SIZE,
  333. VM_READ | VM_EXEC |
  334. VM_MAYREAD | VM_MAYEXEC |
  335. VM_ALWAYSDUMP | VM_RESERVED,
  336. NULL);
  337. }
  338. const char *arch_vma_name(struct vm_area_struct *vma)
  339. {
  340. return (vma->vm_start == 0xffff0000) ? "[vectors]" : NULL;
  341. }