1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Copyright (C) 2009 Sunplus Core Technology Co., Ltd. 4 * Chen Liqin <liqin.chen@sunplusct.com> 5 * Lennox Wu <lennox.wu@sunplusct.com> 6 * Copyright (C) 2012 Regents of the University of California 7 * Copyright (C) 2017 SiFive 8 */ 9 10 #include <linux/kernel.h> 11 #include <linux/sched.h> 12 #include <linux/sched/task_stack.h> 13 #include <linux/tick.h> 14 #include <linux/ptrace.h> 15 #include <linux/uaccess.h> 16 17 #include <asm/unistd.h> 18 #include <asm/processor.h> 19 #include <asm/csr.h> 20 #include <asm/string.h> 21 #include <asm/switch_to.h> 22 23 extern asmlinkage void ret_from_fork(void); 24 extern asmlinkage void ret_from_kernel_thread(void); 25 26 void arch_cpu_idle(void) 27 { 28 wait_for_interrupt(); 29 local_irq_enable(); 30 } 31 32 void show_regs(struct pt_regs *regs) 33 { 34 show_regs_print_info(KERN_DEFAULT); 35 36 pr_cont("sepc: " REG_FMT " ra : " REG_FMT " sp : " REG_FMT "\n", 37 regs->sepc, regs->ra, regs->sp); 38 pr_cont(" gp : " REG_FMT " tp : " REG_FMT " t0 : " REG_FMT "\n", 39 regs->gp, regs->tp, regs->t0); 40 pr_cont(" t1 : " REG_FMT " t2 : " REG_FMT " s0 : " REG_FMT "\n", 41 regs->t1, regs->t2, regs->s0); 42 pr_cont(" s1 : " REG_FMT " a0 : " REG_FMT " a1 : " REG_FMT "\n", 43 regs->s1, regs->a0, regs->a1); 44 pr_cont(" a2 : " REG_FMT " a3 : " REG_FMT " a4 : " REG_FMT "\n", 45 regs->a2, regs->a3, regs->a4); 46 pr_cont(" a5 : " REG_FMT " a6 : " REG_FMT " a7 : " REG_FMT "\n", 47 regs->a5, regs->a6, regs->a7); 48 pr_cont(" s2 : " REG_FMT " s3 : " REG_FMT " s4 : " REG_FMT "\n", 49 regs->s2, regs->s3, regs->s4); 50 pr_cont(" s5 : " REG_FMT " s6 : " REG_FMT " s7 : " REG_FMT "\n", 51 regs->s5, regs->s6, regs->s7); 52 pr_cont(" s8 : " REG_FMT " s9 : " REG_FMT " s10: " REG_FMT "\n", 53 regs->s8, regs->s9, regs->s10); 54 pr_cont(" s11: " REG_FMT " t3 : " REG_FMT " t4 : " REG_FMT "\n", 55 regs->s11, regs->t3, regs->t4); 56 pr_cont(" t5 : " REG_FMT " t6 : " REG_FMT "\n", 57 regs->t5, regs->t6); 58 59 pr_cont("sstatus: " REG_FMT " sbadaddr: " REG_FMT " scause: " REG_FMT "\n", 60 regs->sstatus, regs->sbadaddr, regs->scause); 61 } 62 63 void start_thread(struct pt_regs *regs, unsigned long pc, 64 unsigned long sp) 65 { 66 regs->sstatus = SR_SPIE; 67 if (has_fpu) 68 regs->sstatus |= SR_FS_INITIAL; 69 regs->sepc = pc; 70 regs->sp = sp; 71 set_fs(USER_DS); 72 } 73 74 void flush_thread(void) 75 { 76 #ifdef CONFIG_FPU 77 /* 78 * Reset FPU context 79 * frm: round to nearest, ties to even (IEEE default) 80 * fflags: accrued exceptions cleared 81 */ 82 memset(¤t->thread.fstate, 0, sizeof(current->thread.fstate)); 83 #endif 84 } 85 86 int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src) 87 { 88 fstate_save(src, task_pt_regs(src)); 89 *dst = *src; 90 return 0; 91 } 92 93 int copy_thread(unsigned long clone_flags, unsigned long usp, 94 unsigned long arg, struct task_struct *p) 95 { 96 struct pt_regs *childregs = task_pt_regs(p); 97 98 /* p->thread holds context to be restored by __switch_to() */ 99 if (unlikely(p->flags & PF_KTHREAD)) { 100 /* Kernel thread */ 101 const register unsigned long gp __asm__ ("gp"); 102 memset(childregs, 0, sizeof(struct pt_regs)); 103 childregs->gp = gp; 104 childregs->sstatus = SR_SPP | SR_SPIE; /* Supervisor, irqs on */ 105 106 p->thread.ra = (unsigned long)ret_from_kernel_thread; 107 p->thread.s[0] = usp; /* fn */ 108 p->thread.s[1] = arg; 109 } else { 110 *childregs = *(current_pt_regs()); 111 if (usp) /* User fork */ 112 childregs->sp = usp; 113 if (clone_flags & CLONE_SETTLS) 114 childregs->tp = childregs->a5; 115 childregs->a0 = 0; /* Return value of fork() */ 116 p->thread.ra = (unsigned long)ret_from_fork; 117 } 118 p->thread.sp = (unsigned long)childregs; /* kernel sp */ 119 return 0; 120 } 121