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