blob: 03359616808c4ee2d25d1ddc7c1306ccd122adf2 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001/*
2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
4 * for more details.
5 *
6 * Copyright (C) 1994 - 1999, 2000 by Ralf Baechle and others.
7 * Copyright (C) 2005, 2006 by Ralf Baechle (ralf@linux-mips.org)
8 * Copyright (C) 1999, 2000 Silicon Graphics, Inc.
9 * Copyright (C) 2004 Thiemo Seufer
10 * Copyright (C) 2013 Imagination Technologies Ltd.
11 */
12#include <linux/errno.h>
13#include <linux/sched.h>
14#include <linux/sched/debug.h>
15#include <linux/sched/task.h>
16#include <linux/sched/task_stack.h>
17#include <linux/tick.h>
18#include <linux/kernel.h>
19#include <linux/mm.h>
20#include <linux/stddef.h>
21#include <linux/unistd.h>
22#include <linux/export.h>
23#include <linux/ptrace.h>
24#include <linux/mman.h>
25#include <linux/personality.h>
26#include <linux/sys.h>
27#include <linux/init.h>
28#include <linux/completion.h>
29#include <linux/kallsyms.h>
30#include <linux/random.h>
31#include <linux/prctl.h>
32#include <linux/nmi.h>
33#include <linux/cpu.h>
34
35#include <asm/abi.h>
36#include <asm/asm.h>
37#include <asm/bootinfo.h>
38#include <asm/cpu.h>
39#include <asm/dsemul.h>
40#include <asm/dsp.h>
41#include <asm/fpu.h>
42#include <asm/irq.h>
43#include <asm/mips-cps.h>
44#include <asm/msa.h>
45#include <asm/pgtable.h>
46#include <asm/mipsregs.h>
47#include <asm/processor.h>
48#include <asm/reg.h>
49#include <linux/uaccess.h>
50#include <asm/io.h>
51#include <asm/elf.h>
52#include <asm/isadep.h>
53#include <asm/inst.h>
54#include <asm/stacktrace.h>
55#include <asm/irq_regs.h>
56
57#ifdef CONFIG_HOTPLUG_CPU
58void arch_cpu_idle_dead(void)
59{
60 play_dead();
61}
62#endif
63
64asmlinkage void ret_from_fork(void);
65asmlinkage void ret_from_kernel_thread(void);
66
67void start_thread(struct pt_regs * regs, unsigned long pc, unsigned long sp)
68{
69 unsigned long status;
70
71 /* New thread loses kernel privileges. */
72 status = regs->cp0_status & ~(ST0_CU0|ST0_CU1|ST0_FR|KU_MASK);
73 status |= KU_USER;
74 regs->cp0_status = status;
75 lose_fpu(0);
76 clear_thread_flag(TIF_MSA_CTX_LIVE);
77 clear_used_math();
78#ifdef CONFIG_MIPS_FP_SUPPORT
79 atomic_set(&current->thread.bd_emu_frame, BD_EMUFRAME_NONE);
80#endif
81 init_dsp();
82 regs->cp0_epc = pc;
83 regs->regs[29] = sp;
84}
85
86void exit_thread(struct task_struct *tsk)
87{
88 /*
89 * User threads may have allocated a delay slot emulation frame.
90 * If so, clean up that allocation.
91 */
92 if (!(current->flags & PF_KTHREAD))
93 dsemul_thread_cleanup(tsk);
94}
95
96int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
97{
98 /*
99 * Save any process state which is live in hardware registers to the
100 * parent context prior to duplication. This prevents the new child
101 * state becoming stale if the parent is preempted before copy_thread()
102 * gets a chance to save the parent's live hardware registers to the
103 * child context.
104 */
105 preempt_disable();
106
107 if (is_msa_enabled())
108 save_msa(current);
109 else if (is_fpu_owner())
110 _save_fp(current);
111
112 save_dsp(current);
113
114 preempt_enable();
115
116 *dst = *src;
117 return 0;
118}
119
120/*
121 * Copy architecture-specific thread state
122 */
123int copy_thread_tls(unsigned long clone_flags, unsigned long usp,
124 unsigned long kthread_arg, struct task_struct *p, unsigned long tls)
125{
126 struct thread_info *ti = task_thread_info(p);
127 struct pt_regs *childregs, *regs = current_pt_regs();
128 unsigned long childksp;
129
130 childksp = (unsigned long)task_stack_page(p) + THREAD_SIZE - 32;
131
132 /* set up new TSS. */
133 childregs = (struct pt_regs *) childksp - 1;
134 /* Put the stack after the struct pt_regs. */
135 childksp = (unsigned long) childregs;
136 p->thread.cp0_status = read_c0_status() & ~(ST0_CU2|ST0_CU1);
137 if (unlikely(p->flags & PF_KTHREAD)) {
138 /* kernel thread */
139 unsigned long status = p->thread.cp0_status;
140 memset(childregs, 0, sizeof(struct pt_regs));
141 ti->addr_limit = KERNEL_DS;
142 p->thread.reg16 = usp; /* fn */
143 p->thread.reg17 = kthread_arg;
144 p->thread.reg29 = childksp;
145 p->thread.reg31 = (unsigned long) ret_from_kernel_thread;
146#if defined(CONFIG_CPU_R3000) || defined(CONFIG_CPU_TX39XX)
147 status = (status & ~(ST0_KUP | ST0_IEP | ST0_IEC)) |
148 ((status & (ST0_KUC | ST0_IEC)) << 2);
149#else
150 status |= ST0_EXL;
151#endif
152 childregs->cp0_status = status;
153 return 0;
154 }
155
156 /* user thread */
157 *childregs = *regs;
158 childregs->regs[7] = 0; /* Clear error flag */
159 childregs->regs[2] = 0; /* Child gets zero as return value */
160 if (usp)
161 childregs->regs[29] = usp;
162 ti->addr_limit = USER_DS;
163
164 p->thread.reg29 = (unsigned long) childregs;
165 p->thread.reg31 = (unsigned long) ret_from_fork;
166
167 /*
168 * New tasks lose permission to use the fpu. This accelerates context
169 * switching for most programs since they don't use the fpu.
170 */
171 childregs->cp0_status &= ~(ST0_CU2|ST0_CU1);
172
173 clear_tsk_thread_flag(p, TIF_USEDFPU);
174 clear_tsk_thread_flag(p, TIF_USEDMSA);
175 clear_tsk_thread_flag(p, TIF_MSA_CTX_LIVE);
176
177#ifdef CONFIG_MIPS_MT_FPAFF
178 clear_tsk_thread_flag(p, TIF_FPUBOUND);
179#endif /* CONFIG_MIPS_MT_FPAFF */
180
181#ifdef CONFIG_MIPS_FP_SUPPORT
182 atomic_set(&p->thread.bd_emu_frame, BD_EMUFRAME_NONE);
183#endif
184
185 if (clone_flags & CLONE_SETTLS)
186 ti->tp_value = tls;
187
188 return 0;
189}
190
191#ifdef CONFIG_STACKPROTECTOR
192#include <linux/stackprotector.h>
193unsigned long __stack_chk_guard __read_mostly;
194EXPORT_SYMBOL(__stack_chk_guard);
195#endif
196
197struct mips_frame_info {
198 void *func;
199 unsigned long func_size;
200 int frame_size;
201 int pc_offset;
202};
203
204#define J_TARGET(pc,target) \
205 (((unsigned long)(pc) & 0xf0000000) | ((target) << 2))
206
207static inline int is_ra_save_ins(union mips_instruction *ip, int *poff)
208{
209#ifdef CONFIG_CPU_MICROMIPS
210 /*
211 * swsp ra,offset
212 * swm16 reglist,offset(sp)
213 * swm32 reglist,offset(sp)
214 * sw32 ra,offset(sp)
215 * jradiussp - NOT SUPPORTED
216 *
217 * microMIPS is way more fun...
218 */
219 if (mm_insn_16bit(ip->word >> 16)) {
220 switch (ip->mm16_r5_format.opcode) {
221 case mm_swsp16_op:
222 if (ip->mm16_r5_format.rt != 31)
223 return 0;
224
225 *poff = ip->mm16_r5_format.imm;
226 *poff = (*poff << 2) / sizeof(ulong);
227 return 1;
228
229 case mm_pool16c_op:
230 switch (ip->mm16_m_format.func) {
231 case mm_swm16_op:
232 *poff = ip->mm16_m_format.imm;
233 *poff += 1 + ip->mm16_m_format.rlist;
234 *poff = (*poff << 2) / sizeof(ulong);
235 return 1;
236
237 default:
238 return 0;
239 }
240
241 default:
242 return 0;
243 }
244 }
245
246 switch (ip->i_format.opcode) {
247 case mm_sw32_op:
248 if (ip->i_format.rs != 29)
249 return 0;
250 if (ip->i_format.rt != 31)
251 return 0;
252
253 *poff = ip->i_format.simmediate / sizeof(ulong);
254 return 1;
255
256 case mm_pool32b_op:
257 switch (ip->mm_m_format.func) {
258 case mm_swm32_func:
259 if (ip->mm_m_format.rd < 0x10)
260 return 0;
261 if (ip->mm_m_format.base != 29)
262 return 0;
263
264 *poff = ip->mm_m_format.simmediate;
265 *poff += (ip->mm_m_format.rd & 0xf) * sizeof(u32);
266 *poff /= sizeof(ulong);
267 return 1;
268 default:
269 return 0;
270 }
271
272 default:
273 return 0;
274 }
275#else
276 /* sw / sd $ra, offset($sp) */
277 if ((ip->i_format.opcode == sw_op || ip->i_format.opcode == sd_op) &&
278 ip->i_format.rs == 29 && ip->i_format.rt == 31) {
279 *poff = ip->i_format.simmediate / sizeof(ulong);
280 return 1;
281 }
282
283 return 0;
284#endif
285}
286
287static inline int is_jump_ins(union mips_instruction *ip)
288{
289#ifdef CONFIG_CPU_MICROMIPS
290 /*
291 * jr16,jrc,jalr16,jalr16
292 * jal
293 * jalr/jr,jalr.hb/jr.hb,jalrs,jalrs.hb
294 * jraddiusp - NOT SUPPORTED
295 *
296 * microMIPS is kind of more fun...
297 */
298 if (mm_insn_16bit(ip->word >> 16)) {
299 if ((ip->mm16_r5_format.opcode == mm_pool16c_op &&
300 (ip->mm16_r5_format.rt & mm_jr16_op) == mm_jr16_op))
301 return 1;
302 return 0;
303 }
304
305 if (ip->j_format.opcode == mm_j32_op)
306 return 1;
307 if (ip->j_format.opcode == mm_jal32_op)
308 return 1;
309 if (ip->r_format.opcode != mm_pool32a_op ||
310 ip->r_format.func != mm_pool32axf_op)
311 return 0;
312 return ((ip->u_format.uimmediate >> 6) & mm_jalr_op) == mm_jalr_op;
313#else
314 if (ip->j_format.opcode == j_op)
315 return 1;
316 if (ip->j_format.opcode == jal_op)
317 return 1;
318 if (ip->r_format.opcode != spec_op)
319 return 0;
320 return ip->r_format.func == jalr_op || ip->r_format.func == jr_op;
321#endif
322}
323
324static inline int is_sp_move_ins(union mips_instruction *ip, int *frame_size)
325{
326#ifdef CONFIG_CPU_MICROMIPS
327 unsigned short tmp;
328
329 /*
330 * addiusp -imm
331 * addius5 sp,-imm
332 * addiu32 sp,sp,-imm
333 * jradiussp - NOT SUPPORTED
334 *
335 * microMIPS is not more fun...
336 */
337 if (mm_insn_16bit(ip->word >> 16)) {
338 if (ip->mm16_r3_format.opcode == mm_pool16d_op &&
339 ip->mm16_r3_format.simmediate & mm_addiusp_func) {
340 tmp = ip->mm_b0_format.simmediate >> 1;
341 tmp = ((tmp & 0x1ff) ^ 0x100) - 0x100;
342 if ((tmp + 2) < 4) /* 0x0,0x1,0x1fe,0x1ff are special */
343 tmp ^= 0x100;
344 *frame_size = -(signed short)(tmp << 2);
345 return 1;
346 }
347 if (ip->mm16_r5_format.opcode == mm_pool16d_op &&
348 ip->mm16_r5_format.rt == 29) {
349 tmp = ip->mm16_r5_format.imm >> 1;
350 *frame_size = -(signed short)(tmp & 0xf);
351 return 1;
352 }
353 return 0;
354 }
355
356 if (ip->mm_i_format.opcode == mm_addiu32_op &&
357 ip->mm_i_format.rt == 29 && ip->mm_i_format.rs == 29) {
358 *frame_size = -ip->i_format.simmediate;
359 return 1;
360 }
361#else
362 /* addiu/daddiu sp,sp,-imm */
363 if (ip->i_format.rs != 29 || ip->i_format.rt != 29)
364 return 0;
365
366 if (ip->i_format.opcode == addiu_op ||
367 ip->i_format.opcode == daddiu_op) {
368 if (ip->i_format.simmediate > 0)
369 return 0;
370 *frame_size = -ip->i_format.simmediate;
371 return 1;
372 }
373#endif
374 return 0;
375}
376
377static int get_frame_info(struct mips_frame_info *info)
378{
379 bool is_mmips = IS_ENABLED(CONFIG_CPU_MICROMIPS);
380 union mips_instruction insn, *ip;
381 const unsigned int max_insns = 128;
382 unsigned int last_insn_size = 0;
383 unsigned int i;
384 bool saw_jump = false;
385
386 info->pc_offset = -1;
387 info->frame_size = 0;
388
389 ip = (void *)msk_isa16_mode((ulong)info->func);
390 if (!ip)
391 goto err;
392
393 for (i = 0; i < max_insns; i++) {
394 ip = (void *)ip + last_insn_size;
395
396 if (is_mmips && mm_insn_16bit(ip->halfword[0])) {
397 insn.word = ip->halfword[0] << 16;
398 last_insn_size = 2;
399 } else if (is_mmips) {
400 insn.word = ip->halfword[0] << 16 | ip->halfword[1];
401 last_insn_size = 4;
402 } else {
403 insn.word = ip->word;
404 last_insn_size = 4;
405 }
406
407 if (!info->frame_size) {
408 is_sp_move_ins(&insn, &info->frame_size);
409 continue;
410 } else if (!saw_jump && is_jump_ins(ip)) {
411 /*
412 * If we see a jump instruction, we are finished
413 * with the frame save.
414 *
415 * Some functions can have a shortcut return at
416 * the beginning of the function, so don't start
417 * looking for jump instruction until we see the
418 * frame setup.
419 *
420 * The RA save instruction can get put into the
421 * delay slot of the jump instruction, so look
422 * at the next instruction, too.
423 */
424 saw_jump = true;
425 continue;
426 }
427 if (info->pc_offset == -1 &&
428 is_ra_save_ins(&insn, &info->pc_offset))
429 break;
430 if (saw_jump)
431 break;
432 }
433 if (info->frame_size && info->pc_offset >= 0) /* nested */
434 return 0;
435 if (info->pc_offset < 0) /* leaf */
436 return 1;
437 /* prologue seems bogus... */
438err:
439 return -1;
440}
441
442static struct mips_frame_info schedule_mfi __read_mostly;
443
444#ifdef CONFIG_KALLSYMS
445static unsigned long get___schedule_addr(void)
446{
447 return kallsyms_lookup_name("__schedule");
448}
449#else
450static unsigned long get___schedule_addr(void)
451{
452 union mips_instruction *ip = (void *)schedule;
453 int max_insns = 8;
454 int i;
455
456 for (i = 0; i < max_insns; i++, ip++) {
457 if (ip->j_format.opcode == j_op)
458 return J_TARGET(ip, ip->j_format.target);
459 }
460 return 0;
461}
462#endif
463
464static int __init frame_info_init(void)
465{
466 unsigned long size = 0;
467#ifdef CONFIG_KALLSYMS
468 unsigned long ofs;
469#endif
470 unsigned long addr;
471
472 addr = get___schedule_addr();
473 if (!addr)
474 addr = (unsigned long)schedule;
475
476#ifdef CONFIG_KALLSYMS
477 kallsyms_lookup_size_offset(addr, &size, &ofs);
478#endif
479 schedule_mfi.func = (void *)addr;
480 schedule_mfi.func_size = size;
481
482 get_frame_info(&schedule_mfi);
483
484 /*
485 * Without schedule() frame info, result given by
486 * thread_saved_pc() and get_wchan() are not reliable.
487 */
488 if (schedule_mfi.pc_offset < 0)
489 printk("Can't analyze schedule() prologue at %p\n", schedule);
490
491 return 0;
492}
493
494arch_initcall(frame_info_init);
495
496/*
497 * Return saved PC of a blocked thread.
498 */
499static unsigned long thread_saved_pc(struct task_struct *tsk)
500{
501 struct thread_struct *t = &tsk->thread;
502
503 /* New born processes are a special case */
504 if (t->reg31 == (unsigned long) ret_from_fork)
505 return t->reg31;
506 if (schedule_mfi.pc_offset < 0)
507 return 0;
508 return ((unsigned long *)t->reg29)[schedule_mfi.pc_offset];
509}
510
511
512#ifdef CONFIG_KALLSYMS
513/* generic stack unwinding function */
514unsigned long notrace unwind_stack_by_address(unsigned long stack_page,
515 unsigned long *sp,
516 unsigned long pc,
517 unsigned long *ra)
518{
519 unsigned long low, high, irq_stack_high;
520 struct mips_frame_info info;
521 unsigned long size, ofs;
522 struct pt_regs *regs;
523 int leaf;
524
525 if (!stack_page)
526 return 0;
527
528 /*
529 * IRQ stacks start at IRQ_STACK_START
530 * task stacks at THREAD_SIZE - 32
531 */
532 low = stack_page;
533 if (!preemptible() && on_irq_stack(raw_smp_processor_id(), *sp)) {
534 high = stack_page + IRQ_STACK_START;
535 irq_stack_high = high;
536 } else {
537 high = stack_page + THREAD_SIZE - 32;
538 irq_stack_high = 0;
539 }
540
541 /*
542 * If we reached the top of the interrupt stack, start unwinding
543 * the interrupted task stack.
544 */
545 if (unlikely(*sp == irq_stack_high)) {
546 unsigned long task_sp = *(unsigned long *)*sp;
547
548 /*
549 * Check that the pointer saved in the IRQ stack head points to
550 * something within the stack of the current task
551 */
552 if (!object_is_on_stack((void *)task_sp))
553 return 0;
554
555 /*
556 * Follow pointer to tasks kernel stack frame where interrupted
557 * state was saved.
558 */
559 regs = (struct pt_regs *)task_sp;
560 pc = regs->cp0_epc;
561 if (!user_mode(regs) && __kernel_text_address(pc)) {
562 *sp = regs->regs[29];
563 *ra = regs->regs[31];
564 return pc;
565 }
566 return 0;
567 }
568 if (!kallsyms_lookup_size_offset(pc, &size, &ofs))
569 return 0;
570 /*
571 * Return ra if an exception occurred at the first instruction
572 */
573 if (unlikely(ofs == 0)) {
574 pc = *ra;
575 *ra = 0;
576 return pc;
577 }
578
579 info.func = (void *)(pc - ofs);
580 info.func_size = ofs; /* analyze from start to ofs */
581 leaf = get_frame_info(&info);
582 if (leaf < 0)
583 return 0;
584
585 if (*sp < low || *sp + info.frame_size > high)
586 return 0;
587
588 if (leaf)
589 /*
590 * For some extreme cases, get_frame_info() can
591 * consider wrongly a nested function as a leaf
592 * one. In that cases avoid to return always the
593 * same value.
594 */
595 pc = pc != *ra ? *ra : 0;
596 else
597 pc = ((unsigned long *)(*sp))[info.pc_offset];
598
599 *sp += info.frame_size;
600 *ra = 0;
601 return __kernel_text_address(pc) ? pc : 0;
602}
603EXPORT_SYMBOL(unwind_stack_by_address);
604
605/* used by show_backtrace() */
606unsigned long unwind_stack(struct task_struct *task, unsigned long *sp,
607 unsigned long pc, unsigned long *ra)
608{
609 unsigned long stack_page = 0;
610 int cpu;
611
612 for_each_possible_cpu(cpu) {
613 if (on_irq_stack(cpu, *sp)) {
614 stack_page = (unsigned long)irq_stack[cpu];
615 break;
616 }
617 }
618
619 if (!stack_page)
620 stack_page = (unsigned long)task_stack_page(task);
621
622 return unwind_stack_by_address(stack_page, sp, pc, ra);
623}
624#endif
625
626/*
627 * get_wchan - a maintenance nightmare^W^Wpain in the ass ...
628 */
629unsigned long get_wchan(struct task_struct *task)
630{
631 unsigned long pc = 0;
632#ifdef CONFIG_KALLSYMS
633 unsigned long sp;
634 unsigned long ra = 0;
635#endif
636
637 if (!task || task == current || task->state == TASK_RUNNING)
638 goto out;
639 if (!task_stack_page(task))
640 goto out;
641
642 pc = thread_saved_pc(task);
643
644#ifdef CONFIG_KALLSYMS
645 sp = task->thread.reg29 + schedule_mfi.frame_size;
646
647 while (in_sched_functions(pc))
648 pc = unwind_stack(task, &sp, pc, &ra);
649#endif
650
651out:
652 return pc;
653}
654
655unsigned long mips_stack_top(void)
656{
657 unsigned long top = TASK_SIZE & PAGE_MASK;
658
659 if (IS_ENABLED(CONFIG_MIPS_FP_SUPPORT)) {
660 /* One page for branch delay slot "emulation" */
661 top -= PAGE_SIZE;
662 }
663
664 /* Space for the VDSO, data page & GIC user page */
665 top -= PAGE_ALIGN(current->thread.abi->vdso->size);
666 top -= PAGE_SIZE;
667 top -= mips_gic_present() ? PAGE_SIZE : 0;
668
669 /* Space for cache colour alignment */
670 if (cpu_has_dc_aliases)
671 top -= shm_align_mask + 1;
672
673 /* Space to randomize the VDSO base */
674 if (current->flags & PF_RANDOMIZE)
675 top -= VDSO_RANDOMIZE_SIZE;
676
677 return top;
678}
679
680/*
681 * Don't forget that the stack pointer must be aligned on a 8 bytes
682 * boundary for 32-bits ABI and 16 bytes for 64-bits ABI.
683 */
684unsigned long arch_align_stack(unsigned long sp)
685{
686 if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
687 sp -= get_random_int() & ~PAGE_MASK;
688
689 return sp & ALMASK;
690}
691
692static DEFINE_PER_CPU(call_single_data_t, backtrace_csd);
693static struct cpumask backtrace_csd_busy;
694
695static void handle_backtrace(void *info)
696{
697 nmi_cpu_backtrace(get_irq_regs());
698 cpumask_clear_cpu(smp_processor_id(), &backtrace_csd_busy);
699}
700
701static void raise_backtrace(cpumask_t *mask)
702{
703 call_single_data_t *csd;
704 int cpu;
705
706 for_each_cpu(cpu, mask) {
707 /*
708 * If we previously sent an IPI to the target CPU & it hasn't
709 * cleared its bit in the busy cpumask then it didn't handle
710 * our previous IPI & it's not safe for us to reuse the
711 * call_single_data_t.
712 */
713 if (cpumask_test_and_set_cpu(cpu, &backtrace_csd_busy)) {
714 pr_warn("Unable to send backtrace IPI to CPU%u - perhaps it hung?\n",
715 cpu);
716 continue;
717 }
718
719 csd = &per_cpu(backtrace_csd, cpu);
720 csd->func = handle_backtrace;
721 smp_call_function_single_async(cpu, csd);
722 }
723}
724
725void arch_trigger_cpumask_backtrace(const cpumask_t *mask, bool exclude_self)
726{
727 nmi_trigger_cpumask_backtrace(mask, exclude_self, raise_backtrace);
728}
729
730int mips_get_process_fp_mode(struct task_struct *task)
731{
732 int value = 0;
733
734 if (!test_tsk_thread_flag(task, TIF_32BIT_FPREGS))
735 value |= PR_FP_MODE_FR;
736 if (test_tsk_thread_flag(task, TIF_HYBRID_FPREGS))
737 value |= PR_FP_MODE_FRE;
738
739 return value;
740}
741
742static long prepare_for_fp_mode_switch(void *unused)
743{
744 /*
745 * This is icky, but we use this to simply ensure that all CPUs have
746 * context switched, regardless of whether they were previously running
747 * kernel or user code. This ensures that no CPU that a mode-switching
748 * program may execute on keeps its FPU enabled (& in the old mode)
749 * throughout the mode switch.
750 */
751 return 0;
752}
753
754int mips_set_process_fp_mode(struct task_struct *task, unsigned int value)
755{
756 const unsigned int known_bits = PR_FP_MODE_FR | PR_FP_MODE_FRE;
757 struct task_struct *t;
758 struct cpumask process_cpus;
759 int cpu;
760
761 /* If nothing to change, return right away, successfully. */
762 if (value == mips_get_process_fp_mode(task))
763 return 0;
764
765 /* Only accept a mode change if 64-bit FP enabled for o32. */
766 if (!IS_ENABLED(CONFIG_MIPS_O32_FP64_SUPPORT))
767 return -EOPNOTSUPP;
768
769 /* And only for o32 tasks. */
770 if (IS_ENABLED(CONFIG_64BIT) && !test_thread_flag(TIF_32BIT_REGS))
771 return -EOPNOTSUPP;
772
773 /* Check the value is valid */
774 if (value & ~known_bits)
775 return -EOPNOTSUPP;
776
777 /* Setting FRE without FR is not supported. */
778 if ((value & (PR_FP_MODE_FR | PR_FP_MODE_FRE)) == PR_FP_MODE_FRE)
779 return -EOPNOTSUPP;
780
781 /* Avoid inadvertently triggering emulation */
782 if ((value & PR_FP_MODE_FR) && raw_cpu_has_fpu &&
783 !(raw_current_cpu_data.fpu_id & MIPS_FPIR_F64))
784 return -EOPNOTSUPP;
785 if ((value & PR_FP_MODE_FRE) && raw_cpu_has_fpu && !cpu_has_fre)
786 return -EOPNOTSUPP;
787
788 /* FR = 0 not supported in MIPS R6 */
789 if (!(value & PR_FP_MODE_FR) && raw_cpu_has_fpu && cpu_has_mips_r6)
790 return -EOPNOTSUPP;
791
792 /* Indicate the new FP mode in each thread */
793 for_each_thread(task, t) {
794 /* Update desired FP register width */
795 if (value & PR_FP_MODE_FR) {
796 clear_tsk_thread_flag(t, TIF_32BIT_FPREGS);
797 } else {
798 set_tsk_thread_flag(t, TIF_32BIT_FPREGS);
799 clear_tsk_thread_flag(t, TIF_MSA_CTX_LIVE);
800 }
801
802 /* Update desired FP single layout */
803 if (value & PR_FP_MODE_FRE)
804 set_tsk_thread_flag(t, TIF_HYBRID_FPREGS);
805 else
806 clear_tsk_thread_flag(t, TIF_HYBRID_FPREGS);
807 }
808
809 /*
810 * We need to ensure that all threads in the process have switched mode
811 * before returning, in order to allow userland to not worry about
812 * races. We can do this by forcing all CPUs that any thread in the
813 * process may be running on to schedule something else - in this case
814 * prepare_for_fp_mode_switch().
815 *
816 * We begin by generating a mask of all CPUs that any thread in the
817 * process may be running on.
818 */
819 cpumask_clear(&process_cpus);
820 for_each_thread(task, t)
821 cpumask_set_cpu(task_cpu(t), &process_cpus);
822
823 /*
824 * Now we schedule prepare_for_fp_mode_switch() on each of those CPUs.
825 *
826 * The CPUs may have rescheduled already since we switched mode or
827 * generated the cpumask, but that doesn't matter. If the task in this
828 * process is scheduled out then our scheduling
829 * prepare_for_fp_mode_switch() will simply be redundant. If it's
830 * scheduled in then it will already have picked up the new FP mode
831 * whilst doing so.
832 */
833 get_online_cpus();
834 for_each_cpu_and(cpu, &process_cpus, cpu_online_mask)
835 work_on_cpu(cpu, prepare_for_fp_mode_switch, NULL);
836 put_online_cpus();
837
838 return 0;
839}
840
841#if defined(CONFIG_32BIT) || defined(CONFIG_MIPS32_O32)
842void mips_dump_regs32(u32 *uregs, const struct pt_regs *regs)
843{
844 unsigned int i;
845
846 for (i = MIPS32_EF_R1; i <= MIPS32_EF_R31; i++) {
847 /* k0/k1 are copied as zero. */
848 if (i == MIPS32_EF_R26 || i == MIPS32_EF_R27)
849 uregs[i] = 0;
850 else
851 uregs[i] = regs->regs[i - MIPS32_EF_R0];
852 }
853
854 uregs[MIPS32_EF_LO] = regs->lo;
855 uregs[MIPS32_EF_HI] = regs->hi;
856 uregs[MIPS32_EF_CP0_EPC] = regs->cp0_epc;
857 uregs[MIPS32_EF_CP0_BADVADDR] = regs->cp0_badvaddr;
858 uregs[MIPS32_EF_CP0_STATUS] = regs->cp0_status;
859 uregs[MIPS32_EF_CP0_CAUSE] = regs->cp0_cause;
860}
861#endif /* CONFIG_32BIT || CONFIG_MIPS32_O32 */
862
863#ifdef CONFIG_64BIT
864void mips_dump_regs64(u64 *uregs, const struct pt_regs *regs)
865{
866 unsigned int i;
867
868 for (i = MIPS64_EF_R1; i <= MIPS64_EF_R31; i++) {
869 /* k0/k1 are copied as zero. */
870 if (i == MIPS64_EF_R26 || i == MIPS64_EF_R27)
871 uregs[i] = 0;
872 else
873 uregs[i] = regs->regs[i - MIPS64_EF_R0];
874 }
875
876 uregs[MIPS64_EF_LO] = regs->lo;
877 uregs[MIPS64_EF_HI] = regs->hi;
878 uregs[MIPS64_EF_CP0_EPC] = regs->cp0_epc;
879 uregs[MIPS64_EF_CP0_BADVADDR] = regs->cp0_badvaddr;
880 uregs[MIPS64_EF_CP0_STATUS] = regs->cp0_status;
881 uregs[MIPS64_EF_CP0_CAUSE] = regs->cp0_cause;
882}
883#endif /* CONFIG_64BIT */