| 1 | |
| 2 | |
| 3 | * Author: Huacai Chen <[email protected]>
|
| 4 | * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
|
| 5 | *
|
| 6 | * Derived from MIPS:
|
| 7 | * Copyright (C) 1994 - 1999, 2000 by Ralf Baechle and others.
|
| 8 | * Copyright (C) 2005, 2006 by Ralf Baechle ([email protected])
|
| 9 | * Copyright (C) 1999, 2000 Silicon Graphics, Inc.
|
| 10 | * Copyright (C) 2004 Thiemo Seufer
|
| 11 | * Copyright (C) 2013 Imagination Technologies Ltd.
|
| 12 |
|
| 13 | #include <linux/cpu.h>
|
| 14 | #include <linux/init.h>
|
| 15 | #include <linux/kernel.h>
|
| 16 | #include <linux/entry-common.h>
|
| 17 | #include <linux/errno.h>
|
| 18 | #include <linux/sched.h>
|
| 19 | #include <linux/sched/debug.h>
|
| 20 | #include <linux/sched/task.h>
|
| 21 | #include <linux/sched/task_stack.h>
|
| 22 | #include <linux/hw_breakpoint.h>
|
| 23 | #include <linux/mm.h>
|
| 24 | #include <linux/stddef.h>
|
| 25 | #include <linux/unistd.h>
|
| 26 | #include <linux/export.h>
|
| 27 | #include <linux/ptrace.h>
|
| 28 | #include <linux/mman.h>
|
| 29 | #include <linux/personality.h>
|
| 30 | #include <linux/sys.h>
|
| 31 | #include <linux/completion.h>
|
| 32 | #include <linux/kallsyms.h>
|
| 33 | #include <linux/random.h>
|
| 34 | #include <linux/prctl.h>
|
| 35 | #include <linux/nmi.h>
|
| 36 |
|
| 37 | #include <asm/asm.h>
|
| 38 | #include <asm/asm-prototypes.h>
|
| 39 | #include <asm/bootinfo.h>
|
| 40 | #include <asm/cpu.h>
|
| 41 | #include <asm/elf.h>
|
| 42 | #include <asm/exec.h>
|
| 43 | #include <asm/fpu.h>
|
| 44 | #include <asm/lbt.h>
|
| 45 | #include <asm/io.h>
|
| 46 | #include <asm/irq.h>
|
| 47 | #include <asm/irq_regs.h>
|
| 48 | #include <asm/loongarch.h>
|
| 49 | #include <asm/pgtable.h>
|
| 50 | #include <asm/processor.h>
|
| 51 | #include <asm/reg.h>
|
| 52 | #include <asm/switch_to.h>
|
| 53 | #include <asm/unwind.h>
|
| 54 | #include <asm/vdso.h>
|
| 55 | #include <asm/vdso/vdso.h>
|
| 56 |
|
| 57 | #ifdef CONFIG_STACKPROTECTOR
|
| 58 | #include <linux/stackprotector.h>
|
| 59 | unsigned long __stack_chk_guard __read_mostly;
|
| 60 | EXPORT_SYMBOL(__stack_chk_guard);
|
| 61 | #endif
|
| 62 |
|
| 63 | DEFINE_PER_CPU(struct task_struct *, cpu_tasks);
|
| 64 |
|
| 65 | |
| 66 | * Idle related variables and functions
|
| 67 |
|
| 68 |
|
| 69 | unsigned long boot_option_idle_override = IDLE_NO_OVERRIDE;
|
| 70 | EXPORT_SYMBOL(boot_option_idle_override);
|
| 71 |
|
| 72 | asmlinkage void restore_and_ret(void);
|
| 73 | asmlinkage void ret_from_fork_asm(void);
|
| 74 | asmlinkage void ret_from_kernel_thread_asm(void);
|
| 75 |
|
| 76 | void start_thread(struct pt_regs *regs, unsigned long pc, unsigned long sp)
|
| 77 | {
|
| 78 | unsigned long crmd;
|
| 79 | unsigned long prmd;
|
| 80 | unsigned long euen;
|
| 81 |
|
| 82 |
|
| 83 | crmd = regs->csr_crmd & ~(PLV_MASK);
|
| 84 | crmd |= PLV_USER;
|
| 85 | regs->csr_crmd = crmd;
|
| 86 |
|
| 87 | prmd = regs->csr_prmd & ~(PLV_MASK);
|
| 88 | prmd |= PLV_USER;
|
| 89 | regs->csr_prmd = prmd;
|
| 90 |
|
| 91 | euen = regs->csr_euen & ~(CSR_EUEN_FPEN);
|
| 92 | regs->csr_euen = euen;
|
| 93 | lose_fpu(0);
|
| 94 | lose_lbt(0);
|
| 95 | current->thread.fpu.fcsr = boot_cpu_data.fpu_csr0;
|
| 96 |
|
| 97 | clear_thread_flag(TIF_LSX_CTX_LIVE);
|
| 98 | clear_thread_flag(TIF_LASX_CTX_LIVE);
|
| 99 | clear_thread_flag(TIF_LBT_CTX_LIVE);
|
| 100 | clear_used_math();
|
| 101 | regs->csr_era = pc;
|
| 102 | regs->regs[3] = sp;
|
| 103 | }
|
| 104 |
|
| 105 | void flush_thread(void)
|
| 106 | {
|
| 107 | flush_ptrace_hw_breakpoint(current);
|
| 108 | }
|
| 109 |
|
| 110 | void exit_thread(struct task_struct *tsk)
|
| 111 | {
|
| 112 | }
|
| 113 |
|
| 114 | int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
|
| 115 | {
|
| 116 | |
| 117 | * Save any process state which is live in hardware registers to the
|
| 118 | * parent context prior to duplication. This prevents the new child
|
| 119 | * state becoming stale if the parent is preempted before copy_thread()
|
| 120 | * gets a chance to save the parent's live hardware registers to the
|
| 121 | * child context.
|
| 122 |
|
| 123 | preempt_disable();
|
| 124 |
|
| 125 | if (is_fpu_owner()) {
|
| 126 | if (is_lasx_enabled())
|
| 127 | save_lasx(current);
|
| 128 | else if (is_lsx_enabled())
|
| 129 | save_lsx(current);
|
| 130 | else
|
| 131 | save_fp(current);
|
| 132 | }
|
| 133 |
|
| 134 | preempt_enable();
|
| 135 |
|
| 136 | if (IS_ENABLED(CONFIG_RANDSTRUCT)) {
|
| 137 | memcpy(dst, src, sizeof(struct task_struct));
|
| 138 | return 0;
|
| 139 | }
|
| 140 |
|
| 141 | dst->thread.fpu.fcsr = src->thread.fpu.fcsr;
|
| 142 |
|
| 143 | if (!used_math())
|
| 144 | memcpy(dst, src, offsetof(struct task_struct, thread.fpu.fpr));
|
| 145 | else
|
| 146 | memcpy(dst, src, offsetof(struct task_struct, thread.lbt.scr0));
|
| 147 |
|
| 148 | #ifdef CONFIG_CPU_HAS_LBT
|
| 149 | memcpy(&dst->thread.lbt, &src->thread.lbt, sizeof(struct loongarch_lbt));
|
| 150 | #endif
|
| 151 |
|
| 152 | return 0;
|
| 153 | }
|
| 154 |
|
| 155 | asmlinkage void noinstr __no_stack_protector ret_from_fork(struct task_struct *prev,
|
| 156 | struct pt_regs *regs)
|
| 157 | {
|
| 158 | schedule_tail(prev);
|
| 159 | syscall_exit_to_user_mode(regs);
|
| 160 | }
|
| 161 |
|
| 162 | asmlinkage void noinstr __no_stack_protector ret_from_kernel_thread(struct task_struct *prev,
|
| 163 | struct pt_regs *regs,
|
| 164 | int (*fn)(void *),
|
| 165 | void *fn_arg)
|
| 166 | {
|
| 167 | schedule_tail(prev);
|
| 168 | fn(fn_arg);
|
| 169 | syscall_exit_to_user_mode(regs);
|
| 170 | }
|
| 171 |
|
| 172 | |
| 173 | * Copy architecture-specific thread state
|
| 174 |
|
| 175 | int copy_thread(struct task_struct *p, const struct kernel_clone_args *args)
|
| 176 | {
|
| 177 | unsigned long childksp;
|
| 178 | unsigned long tls = args->tls;
|
| 179 | unsigned long usp = args->stack;
|
| 180 | u64 clone_flags = args->flags;
|
| 181 | struct pt_regs *childregs, *regs = current_pt_regs();
|
| 182 |
|
| 183 | childksp = (unsigned long)task_stack_page(p) + THREAD_SIZE;
|
| 184 |
|
| 185 |
|
| 186 | childregs = (struct pt_regs *) childksp - 1;
|
| 187 |
|
| 188 | childksp = (unsigned long) childregs;
|
| 189 | p->thread.sched_cfa = 0;
|
| 190 | p->thread.csr_euen = 0;
|
| 191 | p->thread.csr_crmd = csr_read32(LOONGARCH_CSR_CRMD);
|
| 192 | p->thread.csr_prmd = csr_read32(LOONGARCH_CSR_PRMD);
|
| 193 | p->thread.csr_ecfg = csr_read32(LOONGARCH_CSR_ECFG);
|
| 194 | if (unlikely(args->fn)) {
|
| 195 |
|
| 196 | p->thread.reg03 = childksp;
|
| 197 | p->thread.reg23 = (unsigned long)args->fn;
|
| 198 | p->thread.reg24 = (unsigned long)args->fn_arg;
|
| 199 | p->thread.reg01 = (unsigned long)ret_from_kernel_thread_asm;
|
| 200 | p->thread.sched_ra = (unsigned long)ret_from_kernel_thread_asm;
|
| 201 | memset(childregs, 0, sizeof(struct pt_regs));
|
| 202 | childregs->csr_euen = p->thread.csr_euen;
|
| 203 | childregs->csr_crmd = p->thread.csr_crmd;
|
| 204 | childregs->csr_prmd = p->thread.csr_prmd;
|
| 205 | childregs->csr_ecfg = p->thread.csr_ecfg;
|
| 206 | goto out;
|
| 207 | }
|
| 208 |
|
| 209 |
|
| 210 | *childregs = *regs;
|
| 211 | childregs->regs[4] = 0;
|
| 212 | if (usp)
|
| 213 | childregs->regs[3] = usp;
|
| 214 |
|
| 215 | p->thread.reg03 = (unsigned long) childregs;
|
| 216 | p->thread.reg01 = (unsigned long) ret_from_fork_asm;
|
| 217 | p->thread.sched_ra = (unsigned long) ret_from_fork_asm;
|
| 218 |
|
| 219 | |
| 220 | * New tasks lose permission to use the fpu. This accelerates context
|
| 221 | * switching for most programs since they don't use the fpu.
|
| 222 |
|
| 223 | childregs->csr_euen = 0;
|
| 224 |
|
| 225 | if (clone_flags & CLONE_SETTLS)
|
| 226 | childregs->regs[2] = tls;
|
| 227 |
|
| 228 | out:
|
| 229 | ptrace_hw_copy_thread(p);
|
| 230 | clear_tsk_thread_flag(p, TIF_USEDFPU);
|
| 231 | clear_tsk_thread_flag(p, TIF_USEDSIMD);
|
| 232 | clear_tsk_thread_flag(p, TIF_USEDLBT);
|
| 233 | clear_tsk_thread_flag(p, TIF_LSX_CTX_LIVE);
|
| 234 | clear_tsk_thread_flag(p, TIF_LASX_CTX_LIVE);
|
| 235 | clear_tsk_thread_flag(p, TIF_LBT_CTX_LIVE);
|
| 236 |
|
| 237 | return 0;
|
| 238 | }
|
| 239 |
|
| 240 | unsigned long __get_wchan(struct task_struct *task)
|
| 241 | {
|
| 242 | unsigned long pc = 0;
|
| 243 | struct unwind_state state;
|
| 244 |
|
| 245 | if (!try_get_task_stack(task))
|
| 246 | return 0;
|
| 247 |
|
| 248 | for (unwind_start(&state, task, NULL);
|
| 249 | !unwind_done(&state); unwind_next_frame(&state)) {
|
| 250 | pc = unwind_get_return_address(&state);
|
| 251 | if (!pc)
|
| 252 | break;
|
| 253 | if (in_sched_functions(pc))
|
| 254 | continue;
|
| 255 | break;
|
| 256 | }
|
| 257 |
|
| 258 | put_task_stack(task);
|
| 259 |
|
| 260 | return pc;
|
| 261 | }
|
| 262 |
|
| 263 | bool in_irq_stack(unsigned long stack, struct stack_info *info)
|
| 264 | {
|
| 265 | unsigned long nextsp;
|
| 266 | unsigned long begin = (unsigned long)this_cpu_read(irq_stack);
|
| 267 | unsigned long end = begin + IRQ_STACK_START;
|
| 268 |
|
| 269 | if (stack < begin || stack >= end)
|
| 270 | return false;
|
| 271 |
|
| 272 | nextsp = *(unsigned long *)end;
|
| 273 | if (nextsp & (SZREG - 1))
|
| 274 | return false;
|
| 275 |
|
| 276 | info->begin = begin;
|
| 277 | info->end = end;
|
| 278 | info->next_sp = nextsp;
|
| 279 | info->type = STACK_TYPE_IRQ;
|
| 280 |
|
| 281 | return true;
|
| 282 | }
|
| 283 |
|
| 284 | bool in_task_stack(unsigned long stack, struct task_struct *task,
|
| 285 | struct stack_info *info)
|
| 286 | {
|
| 287 | unsigned long begin = (unsigned long)task_stack_page(task);
|
| 288 | unsigned long end = begin + THREAD_SIZE;
|
| 289 |
|
| 290 | if (stack < begin || stack >= end)
|
| 291 | return false;
|
| 292 |
|
| 293 | info->begin = begin;
|
| 294 | info->end = end;
|
| 295 | info->next_sp = 0;
|
| 296 | info->type = STACK_TYPE_TASK;
|
| 297 |
|
| 298 | return true;
|
| 299 | }
|
| 300 |
|
| 301 | int get_stack_info(unsigned long stack, struct task_struct *task,
|
| 302 | struct stack_info *info)
|
| 303 | {
|
| 304 | task = task ? : current;
|
| 305 |
|
| 306 | if (!stack || stack & (SZREG - 1))
|
| 307 | goto unknown;
|
| 308 |
|
| 309 | if (in_task_stack(stack, task, info))
|
| 310 | return 0;
|
| 311 |
|
| 312 | if (task != current)
|
| 313 | goto unknown;
|
| 314 |
|
| 315 | if (in_irq_stack(stack, info))
|
| 316 | return 0;
|
| 317 |
|
| 318 | unknown:
|
| 319 | info->type = STACK_TYPE_UNKNOWN;
|
| 320 | return -EINVAL;
|
| 321 | }
|
| 322 |
|
| 323 | unsigned long stack_top(void)
|
| 324 | {
|
| 325 | unsigned long top = TASK_SIZE & PAGE_MASK;
|
| 326 |
|
| 327 | if (current->thread.vdso) {
|
| 328 |
|
| 329 | top -= PAGE_ALIGN(current->thread.vdso->size);
|
| 330 | top -= VVAR_SIZE;
|
| 331 |
|
| 332 |
|
| 333 | if (current->flags & PF_RANDOMIZE)
|
| 334 | top -= VDSO_RANDOMIZE_SIZE;
|
| 335 | }
|
| 336 |
|
| 337 | return top;
|
| 338 | }
|
| 339 |
|
| 340 | |
| 341 | * Don't forget that the stack pointer must be aligned on a 8 bytes
|
| 342 | * boundary for 32-bits ABI and 16 bytes for 64-bits ABI.
|
| 343 |
|
| 344 | unsigned long arch_align_stack(unsigned long sp)
|
| 345 | {
|
| 346 | if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
|
| 347 | sp -= get_random_u32_below(PAGE_SIZE);
|
| 348 |
|
| 349 | return sp & STACK_ALIGN;
|
| 350 | }
|
| 351 |
|
| 352 | static DEFINE_PER_CPU(call_single_data_t, backtrace_csd);
|
| 353 | static struct cpumask backtrace_csd_busy;
|
| 354 |
|
| 355 | static void handle_backtrace(void *info)
|
| 356 | {
|
| 357 | nmi_cpu_backtrace(get_irq_regs());
|
| 358 | cpumask_clear_cpu(smp_processor_id(), &backtrace_csd_busy);
|
| 359 | }
|
| 360 |
|
| 361 | static void raise_backtrace(cpumask_t *mask)
|
| 362 | {
|
| 363 | call_single_data_t *csd;
|
| 364 | int cpu;
|
| 365 |
|
| 366 | for_each_cpu(cpu, mask) {
|
| 367 | |
| 368 | * If we previously sent an IPI to the target CPU & it hasn't
|
| 369 | * cleared its bit in the busy cpumask then it didn't handle
|
| 370 | * our previous IPI & it's not safe for us to reuse the
|
| 371 | * call_single_data_t.
|
| 372 |
|
| 373 | if (cpumask_test_and_set_cpu(cpu, &backtrace_csd_busy)) {
|
| 374 | pr_warn("Unable to send backtrace IPI to CPU%u - perhaps it hung?\n",
|
| 375 | cpu);
|
| 376 | continue;
|
| 377 | }
|
| 378 |
|
| 379 | csd = &per_cpu(backtrace_csd, cpu);
|
| 380 | csd->func = handle_backtrace;
|
| 381 | smp_call_function_single_async(cpu, csd);
|
| 382 | }
|
| 383 | }
|
| 384 |
|
| 385 | void arch_trigger_cpumask_backtrace(const cpumask_t *mask, int exclude_cpu)
|
| 386 | {
|
| 387 | nmi_trigger_cpumask_backtrace(mask, exclude_cpu, raise_backtrace);
|
| 388 | }
|
| 389 |
|
| 390 | #ifdef CONFIG_32BIT
|
| 391 | void loongarch_dump_regs32(u32 *uregs, const struct pt_regs *regs)
|
| 392 | #else
|
| 393 | void loongarch_dump_regs64(u64 *uregs, const struct pt_regs *regs)
|
| 394 | #endif
|
| 395 | {
|
| 396 | unsigned int i;
|
| 397 |
|
| 398 | for (i = LOONGARCH_EF_R1; i <= LOONGARCH_EF_R31; i++) {
|
| 399 | uregs[i] = regs->regs[i - LOONGARCH_EF_R0];
|
| 400 | }
|
| 401 |
|
| 402 | uregs[LOONGARCH_EF_ORIG_A0] = regs->orig_a0;
|
| 403 | uregs[LOONGARCH_EF_CSR_ERA] = regs->csr_era;
|
| 404 | uregs[LOONGARCH_EF_CSR_BADV] = regs->csr_badvaddr;
|
| 405 | uregs[LOONGARCH_EF_CSR_CRMD] = regs->csr_crmd;
|
| 406 | uregs[LOONGARCH_EF_CSR_PRMD] = regs->csr_prmd;
|
| 407 | uregs[LOONGARCH_EF_CSR_EUEN] = regs->csr_euen;
|
| 408 | uregs[LOONGARCH_EF_CSR_ECFG] = regs->csr_ecfg;
|
| 409 | uregs[LOONGARCH_EF_CSR_ESTAT] = regs->csr_estat;
|
| 410 | }
|