Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * arch/sh/kernel/process.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * This file handles the architecture-dependent parts of process handling..
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  *  Copyright (C) 1995  Linus Torvalds
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  *  SuperH version:  Copyright (C) 1999, 2000  Niibe Yutaka & Kaz Kojima
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  *		     Copyright (C) 2006 Lineo Solutions Inc. support SH4A UBC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  *		     Copyright (C) 2002 - 2008  Paul Mundt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/sched/debug.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/sched/task.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/sched/task_stack.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/elfcore.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <linux/ftrace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <linux/hw_breakpoint.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include <linux/prefetch.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #include <linux/stackprotector.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #include <asm/mmu_context.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #include <asm/fpu.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #include <asm/syscalls.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #include <asm/switch_to.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) void show_regs(struct pt_regs * regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	pr_info("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	show_regs_print_info(KERN_DEFAULT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	pr_info("PC is at %pS\n", (void *)instruction_pointer(regs));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	pr_info("PR is at %pS\n", (void *)regs->pr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	pr_info("PC  : %08lx SP  : %08lx SR  : %08lx ", regs->pc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 		regs->regs[15], regs->sr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) #ifdef CONFIG_MMU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	pr_cont("TEA : %08x\n", __raw_readl(MMU_TEA));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	pr_cont("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	pr_info("R0  : %08lx R1  : %08lx R2  : %08lx R3  : %08lx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 		regs->regs[0], regs->regs[1], regs->regs[2], regs->regs[3]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	pr_info("R4  : %08lx R5  : %08lx R6  : %08lx R7  : %08lx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 		regs->regs[4], regs->regs[5], regs->regs[6], regs->regs[7]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	pr_info("R8  : %08lx R9  : %08lx R10 : %08lx R11 : %08lx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 		regs->regs[8], regs->regs[9], regs->regs[10], regs->regs[11]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	pr_info("R12 : %08lx R13 : %08lx R14 : %08lx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 		regs->regs[12], regs->regs[13], regs->regs[14]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	pr_info("MACH: %08lx MACL: %08lx GBR : %08lx PR  : %08lx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 		regs->mach, regs->macl, regs->gbr, regs->pr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	show_trace(NULL, (unsigned long *)regs->regs[15], regs, KERN_DEFAULT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	show_code(regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) void start_thread(struct pt_regs *regs, unsigned long new_pc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 		  unsigned long new_sp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	regs->pr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	regs->sr = SR_FD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	regs->pc = new_pc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	regs->regs[15] = new_sp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	free_thread_xstate(current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) EXPORT_SYMBOL(start_thread);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) void flush_thread(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	struct task_struct *tsk = current;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	flush_ptrace_hw_breakpoint(tsk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) #if defined(CONFIG_SH_FPU)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	/* Forget lazy FPU state */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	clear_fpu(tsk, task_pt_regs(tsk));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	clear_used_math();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) void release_thread(struct task_struct *dead_task)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	/* do nothing */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) asmlinkage void ret_from_fork(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) asmlinkage void ret_from_kernel_thread(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) int copy_thread(unsigned long clone_flags, unsigned long usp, unsigned long arg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 		struct task_struct *p, unsigned long tls)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	struct thread_info *ti = task_thread_info(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	struct pt_regs *childregs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) #if defined(CONFIG_SH_DSP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	struct task_struct *tsk = current;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	if (is_dsp_enabled(tsk)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		/* We can use the __save_dsp or just copy the struct:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 		 * __save_dsp(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 		 * p->thread.dsp_status.status |= SR_DSP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 		p->thread.dsp_status = tsk->thread.dsp_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	memset(p->thread.ptrace_bps, 0, sizeof(p->thread.ptrace_bps));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	childregs = task_pt_regs(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	p->thread.sp = (unsigned long) childregs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	if (unlikely(p->flags & PF_KTHREAD)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		memset(childregs, 0, sizeof(struct pt_regs));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 		p->thread.pc = (unsigned long) ret_from_kernel_thread;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 		childregs->regs[4] = arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 		childregs->regs[5] = usp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 		childregs->sr = SR_MD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) #if defined(CONFIG_SH_FPU)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 		childregs->sr |= SR_FD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 		ti->addr_limit = KERNEL_DS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		ti->status &= ~TS_USEDFPU;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		p->thread.fpu_counter = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	*childregs = *current_pt_regs();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	if (usp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 		childregs->regs[15] = usp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	ti->addr_limit = USER_DS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	if (clone_flags & CLONE_SETTLS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 		childregs->gbr = tls;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	childregs->regs[0] = 0; /* Set return value for child */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	p->thread.pc = (unsigned long) ret_from_fork;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)  *	switch_to(x,y) should switch tasks from x to y.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) __notrace_funcgraph struct task_struct *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) __switch_to(struct task_struct *prev, struct task_struct *next)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	struct thread_struct *next_t = &next->thread;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) #if defined(CONFIG_STACKPROTECTOR) && !defined(CONFIG_SMP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	__stack_chk_guard = next->stack_canary;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	unlazy_fpu(prev, task_pt_regs(prev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	/* we're going to use this soon, after a few expensive things */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	if (next->thread.fpu_counter > 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 		prefetch(next_t->xstate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) #ifdef CONFIG_MMU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	 * Restore the kernel mode register
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	 *	k7 (r7_bank1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	asm volatile("ldc	%0, r7_bank"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 		     : /* no output */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 		     : "r" (task_thread_info(next)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	 * If the task has used fpu the last 5 timeslices, just do a full
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	 * restore of the math state immediately to avoid the trap; the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	 * chances of needing FPU soon are obviously high now
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	if (next->thread.fpu_counter > 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 		__fpu_state_restore();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	return prev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) unsigned long get_wchan(struct task_struct *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	unsigned long pc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	if (!p || p == current || p->state == TASK_RUNNING)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	 * The same comment as on the Alpha applies here, too ...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	pc = thread_saved_pc(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) #ifdef CONFIG_FRAME_POINTER
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	if (in_sched_functions(pc)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 		unsigned long schedule_frame = (unsigned long)p->thread.sp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 		return ((unsigned long *)schedule_frame)[21];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	return pc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) }