xref: /openbmc/linux/arch/csky/kernel/process.c (revision f8e17c17)
1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (C) 2018 Hangzhou C-SKY Microsystems co.,ltd.
3 
4 #include <linux/module.h>
5 #include <linux/version.h>
6 #include <linux/sched.h>
7 #include <linux/sched/task_stack.h>
8 #include <linux/sched/debug.h>
9 #include <linux/delay.h>
10 #include <linux/kallsyms.h>
11 #include <linux/uaccess.h>
12 #include <linux/ptrace.h>
13 
14 #include <asm/elf.h>
15 #include <abi/reg_ops.h>
16 
17 struct cpuinfo_csky cpu_data[NR_CPUS];
18 
19 #ifdef CONFIG_STACKPROTECTOR
20 #include <linux/stackprotector.h>
21 unsigned long __stack_chk_guard __read_mostly;
22 EXPORT_SYMBOL(__stack_chk_guard);
23 #endif
24 
25 asmlinkage void ret_from_fork(void);
26 asmlinkage void ret_from_kernel_thread(void);
27 
28 /*
29  * Some archs flush debug and FPU info here
30  */
31 void flush_thread(void){}
32 
33 /*
34  * Return saved PC from a blocked thread
35  */
36 unsigned long thread_saved_pc(struct task_struct *tsk)
37 {
38 	struct switch_stack *sw = (struct switch_stack *)tsk->thread.ksp;
39 
40 	return sw->r15;
41 }
42 
43 int copy_thread(unsigned long clone_flags,
44 		unsigned long usp,
45 		unsigned long kthread_arg,
46 		struct task_struct *p)
47 {
48 	struct switch_stack *childstack;
49 	struct pt_regs *childregs = task_pt_regs(p);
50 
51 #ifdef CONFIG_CPU_HAS_FPU
52 	save_to_user_fp(&p->thread.user_fp);
53 #endif
54 
55 	childstack = ((struct switch_stack *) childregs) - 1;
56 	memset(childstack, 0, sizeof(struct switch_stack));
57 
58 	/* setup ksp for switch_to !!! */
59 	p->thread.ksp = (unsigned long)childstack;
60 
61 	if (unlikely(p->flags & PF_KTHREAD)) {
62 		memset(childregs, 0, sizeof(struct pt_regs));
63 		childstack->r15 = (unsigned long) ret_from_kernel_thread;
64 		childstack->r10 = kthread_arg;
65 		childstack->r9 = usp;
66 		childregs->sr = mfcr("psr");
67 	} else {
68 		*childregs = *(current_pt_regs());
69 		if (usp)
70 			childregs->usp = usp;
71 		if (clone_flags & CLONE_SETTLS)
72 			task_thread_info(p)->tp_value = childregs->tls
73 						      = childregs->regs[0];
74 
75 		childregs->a0 = 0;
76 		childstack->r15 = (unsigned long) ret_from_fork;
77 	}
78 
79 	return 0;
80 }
81 
82 /* Fill in the fpu structure for a core dump.  */
83 int dump_fpu(struct pt_regs *regs, struct user_fp *fpu)
84 {
85 	memcpy(fpu, &current->thread.user_fp, sizeof(*fpu));
86 	return 1;
87 }
88 EXPORT_SYMBOL(dump_fpu);
89 
90 int dump_task_regs(struct task_struct *tsk, elf_gregset_t *pr_regs)
91 {
92 	struct pt_regs *regs = task_pt_regs(tsk);
93 
94 	/* NOTE: usp is error value. */
95 	ELF_CORE_COPY_REGS((*pr_regs), regs)
96 
97 	return 1;
98 }
99 
100 unsigned long get_wchan(struct task_struct *p)
101 {
102 	unsigned long lr;
103 	unsigned long *fp, *stack_start, *stack_end;
104 	int count = 0;
105 
106 	if (!p || p == current || p->state == TASK_RUNNING)
107 		return 0;
108 
109 	stack_start = (unsigned long *)end_of_stack(p);
110 	stack_end = (unsigned long *)(task_stack_page(p) + THREAD_SIZE);
111 
112 	fp = (unsigned long *) thread_saved_fp(p);
113 	do {
114 		if (fp < stack_start || fp > stack_end)
115 			return 0;
116 #ifdef CONFIG_STACKTRACE
117 		lr = fp[1];
118 		fp = (unsigned long *)fp[0];
119 #else
120 		lr = *fp++;
121 #endif
122 		if (!in_sched_functions(lr) &&
123 		    __kernel_text_address(lr))
124 			return lr;
125 	} while (count++ < 16);
126 
127 	return 0;
128 }
129 EXPORT_SYMBOL(get_wchan);
130 
131 #ifndef CONFIG_CPU_PM_NONE
132 void arch_cpu_idle(void)
133 {
134 #ifdef CONFIG_CPU_PM_WAIT
135 	asm volatile("wait\n");
136 #endif
137 
138 #ifdef CONFIG_CPU_PM_DOZE
139 	asm volatile("doze\n");
140 #endif
141 
142 #ifdef CONFIG_CPU_PM_STOP
143 	asm volatile("stop\n");
144 #endif
145 	local_irq_enable();
146 }
147 #endif
148