1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Process creation support for Hexagon 4 * 5 * Copyright (c) 2010-2012, The Linux Foundation. All rights reserved. 6 */ 7 8 #include <linux/sched.h> 9 #include <linux/sched/debug.h> 10 #include <linux/sched/task.h> 11 #include <linux/sched/task_stack.h> 12 #include <linux/types.h> 13 #include <linux/module.h> 14 #include <linux/tick.h> 15 #include <linux/uaccess.h> 16 #include <linux/slab.h> 17 #include <linux/tracehook.h> 18 19 /* 20 * Program thread launch. Often defined as a macro in processor.h, 21 * but we're shooting for a small footprint and it's not an inner-loop 22 * performance-critical operation. 23 * 24 * The Hexagon ABI specifies that R28 is zero'ed before program launch, 25 * so that gets automatically done here. If we ever stop doing that here, 26 * we'll probably want to define the ELF_PLAT_INIT macro. 27 */ 28 void start_thread(struct pt_regs *regs, unsigned long pc, unsigned long sp) 29 { 30 /* We want to zero all data-containing registers. Is this overkill? */ 31 memset(regs, 0, sizeof(*regs)); 32 /* We might want to also zero all Processor registers here */ 33 pt_set_usermode(regs); 34 pt_set_elr(regs, pc); 35 pt_set_rte_sp(regs, sp); 36 } 37 38 /* 39 * Spin, or better still, do a hardware or VM wait instruction 40 * If hardware or VM offer wait termination even though interrupts 41 * are disabled. 42 */ 43 void arch_cpu_idle(void) 44 { 45 __vmwait(); 46 /* interrupts wake us up, but irqs are still disabled */ 47 raw_local_irq_enable(); 48 } 49 50 /* 51 * Copy architecture-specific thread state 52 */ 53 int copy_thread(unsigned long clone_flags, unsigned long usp, unsigned long arg, 54 struct task_struct *p, unsigned long tls) 55 { 56 struct thread_info *ti = task_thread_info(p); 57 struct hexagon_switch_stack *ss; 58 struct pt_regs *childregs; 59 asmlinkage void ret_from_fork(void); 60 61 childregs = (struct pt_regs *) (((unsigned long) ti + THREAD_SIZE) - 62 sizeof(*childregs)); 63 64 ti->regs = childregs; 65 66 /* 67 * Establish kernel stack pointer and initial PC for new thread 68 * Note that unlike the usual situation, we do not copy the 69 * parent's callee-saved here; those are in pt_regs and whatever 70 * we leave here will be overridden on return to userland. 71 */ 72 ss = (struct hexagon_switch_stack *) ((unsigned long) childregs - 73 sizeof(*ss)); 74 ss->lr = (unsigned long)ret_from_fork; 75 p->thread.switch_sp = ss; 76 if (unlikely(p->flags & (PF_KTHREAD | PF_IO_WORKER))) { 77 memset(childregs, 0, sizeof(struct pt_regs)); 78 /* r24 <- fn, r25 <- arg */ 79 ss->r24 = usp; 80 ss->r25 = arg; 81 pt_set_kmode(childregs); 82 return 0; 83 } 84 memcpy(childregs, current_pt_regs(), sizeof(*childregs)); 85 ss->r2524 = 0; 86 87 if (usp) 88 pt_set_rte_sp(childregs, usp); 89 90 /* Child sees zero return value */ 91 childregs->r00 = 0; 92 93 /* 94 * The clone syscall has the C signature: 95 * int [r0] clone(int flags [r0], 96 * void *child_frame [r1], 97 * void *parent_tid [r2], 98 * void *child_tid [r3], 99 * void *thread_control_block [r4]); 100 * ugp is used to provide TLS support. 101 */ 102 if (clone_flags & CLONE_SETTLS) 103 childregs->ugp = tls; 104 105 /* 106 * Parent sees new pid -- not necessary, not even possible at 107 * this point in the fork process 108 * Might also want to set things like ti->addr_limit 109 */ 110 111 return 0; 112 } 113 114 /* 115 * Release any architecture-specific resources locked by thread 116 */ 117 void release_thread(struct task_struct *dead_task) 118 { 119 } 120 121 /* 122 * Some archs flush debug and FPU info here 123 */ 124 void flush_thread(void) 125 { 126 } 127 128 /* 129 * The "wait channel" terminology is archaic, but what we want 130 * is an identification of the point at which the scheduler 131 * was invoked by a blocked thread. 132 */ 133 unsigned long __get_wchan(struct task_struct *p) 134 { 135 unsigned long fp, pc; 136 unsigned long stack_page; 137 int count = 0; 138 139 stack_page = (unsigned long)task_stack_page(p); 140 fp = ((struct hexagon_switch_stack *)p->thread.switch_sp)->fp; 141 do { 142 if (fp < (stack_page + sizeof(struct thread_info)) || 143 fp >= (THREAD_SIZE - 8 + stack_page)) 144 return 0; 145 pc = ((unsigned long *)fp)[1]; 146 if (!in_sched_functions(pc)) 147 return pc; 148 fp = *(unsigned long *) fp; 149 } while (count++ < 16); 150 151 return 0; 152 } 153 154 /* 155 * Called on the exit path of event entry; see vm_entry.S 156 * 157 * Interrupts will already be disabled. 158 * 159 * Returns 0 if there's no need to re-check for more work. 160 */ 161 162 int do_work_pending(struct pt_regs *regs, u32 thread_info_flags) 163 { 164 if (!(thread_info_flags & _TIF_WORK_MASK)) { 165 return 0; 166 } /* shortcut -- no work to be done */ 167 168 local_irq_enable(); 169 170 if (thread_info_flags & _TIF_NEED_RESCHED) { 171 schedule(); 172 return 1; 173 } 174 175 if (thread_info_flags & (_TIF_SIGPENDING | _TIF_NOTIFY_SIGNAL)) { 176 do_signal(regs); 177 return 1; 178 } 179 180 if (thread_info_flags & _TIF_NOTIFY_RESUME) { 181 tracehook_notify_resume(regs); 182 return 1; 183 } 184 185 /* Should not even reach here */ 186 panic("%s: bad thread_info flags 0x%08x\n", __func__, 187 thread_info_flags); 188 } 189