xref: /openbmc/linux/arch/riscv/kernel/process.c (revision 3dc4b6fb)
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 		/*
70 		 * Restore the initial value to the FP register
71 		 * before starting the user program.
72 		 */
73 		fstate_restore(current, regs);
74 	}
75 	regs->sepc = pc;
76 	regs->sp = sp;
77 	set_fs(USER_DS);
78 }
79 
80 void flush_thread(void)
81 {
82 #ifdef CONFIG_FPU
83 	/*
84 	 * Reset FPU state and context
85 	 *	frm: round to nearest, ties to even (IEEE default)
86 	 *	fflags: accrued exceptions cleared
87 	 */
88 	fstate_off(current, task_pt_regs(current));
89 	memset(&current->thread.fstate, 0, sizeof(current->thread.fstate));
90 #endif
91 }
92 
93 int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
94 {
95 	fstate_save(src, task_pt_regs(src));
96 	*dst = *src;
97 	return 0;
98 }
99 
100 int copy_thread(unsigned long clone_flags, unsigned long usp,
101 	unsigned long arg, struct task_struct *p)
102 {
103 	struct pt_regs *childregs = task_pt_regs(p);
104 
105 	/* p->thread holds context to be restored by __switch_to() */
106 	if (unlikely(p->flags & PF_KTHREAD)) {
107 		/* Kernel thread */
108 		const register unsigned long gp __asm__ ("gp");
109 		memset(childregs, 0, sizeof(struct pt_regs));
110 		childregs->gp = gp;
111 		childregs->sstatus = SR_SPP | SR_SPIE; /* Supervisor, irqs on */
112 
113 		p->thread.ra = (unsigned long)ret_from_kernel_thread;
114 		p->thread.s[0] = usp; /* fn */
115 		p->thread.s[1] = arg;
116 	} else {
117 		*childregs = *(current_pt_regs());
118 		if (usp) /* User fork */
119 			childregs->sp = usp;
120 		if (clone_flags & CLONE_SETTLS)
121 			childregs->tp = childregs->a5;
122 		childregs->a0 = 0; /* Return value of fork() */
123 		p->thread.ra = (unsigned long)ret_from_fork;
124 	}
125 	p->thread.sp = (unsigned long)childregs; /* kernel sp */
126 	return 0;
127 }
128