^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) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include <linux/thread_info.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <asm/sigcontext.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <asm/fpumacro.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <asm/ptrace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <asm/switch_to.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include "sigutil.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) int save_fpu_state(struct pt_regs *regs, __siginfo_fpu_t __user *fpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #ifdef CONFIG_SMP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) if (test_tsk_thread_flag(current, TIF_USEDFPU)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) put_psr(get_psr() | PSR_EF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) fpsave(¤t->thread.float_regs[0], ¤t->thread.fsr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) ¤t->thread.fpqueue[0], ¤t->thread.fpqdepth);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) regs->psr &= ~(PSR_EF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) clear_tsk_thread_flag(current, TIF_USEDFPU);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) if (current == last_task_used_math) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) put_psr(get_psr() | PSR_EF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) fpsave(¤t->thread.float_regs[0], ¤t->thread.fsr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) ¤t->thread.fpqueue[0], ¤t->thread.fpqdepth);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) last_task_used_math = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) regs->psr &= ~(PSR_EF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) err |= __copy_to_user(&fpu->si_float_regs[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) ¤t->thread.float_regs[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) (sizeof(unsigned long) * 32));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) err |= __put_user(current->thread.fsr, &fpu->si_fsr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) err |= __put_user(current->thread.fpqdepth, &fpu->si_fpqdepth);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) if (current->thread.fpqdepth != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) err |= __copy_to_user(&fpu->si_fpqueue[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) ¤t->thread.fpqueue[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) ((sizeof(unsigned long) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) (sizeof(unsigned long *)))*16));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) clear_used_math();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) int restore_fpu_state(struct pt_regs *regs, __siginfo_fpu_t __user *fpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) if (((unsigned long) fpu) & 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) #ifdef CONFIG_SMP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) if (test_tsk_thread_flag(current, TIF_USEDFPU))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) regs->psr &= ~PSR_EF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) if (current == last_task_used_math) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) last_task_used_math = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) regs->psr &= ~PSR_EF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) set_used_math();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) clear_tsk_thread_flag(current, TIF_USEDFPU);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) if (!access_ok(fpu, sizeof(*fpu)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) err = __copy_from_user(¤t->thread.float_regs[0], &fpu->si_float_regs[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) (sizeof(unsigned long) * 32));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) err |= __get_user(current->thread.fsr, &fpu->si_fsr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) err |= __get_user(current->thread.fpqdepth, &fpu->si_fpqdepth);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) if (current->thread.fpqdepth != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) err |= __copy_from_user(¤t->thread.fpqueue[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) &fpu->si_fpqueue[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) ((sizeof(unsigned long) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) (sizeof(unsigned long *)))*16));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) int save_rwin_state(int wsaved, __siginfo_rwin_t __user *rwin)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) int i, err = __put_user(wsaved, &rwin->wsaved);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) for (i = 0; i < wsaved; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) struct reg_window32 *rp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) unsigned long fp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) rp = ¤t_thread_info()->reg_window[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) fp = current_thread_info()->rwbuf_stkptrs[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) err |= copy_to_user(&rwin->reg_window[i], rp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) sizeof(struct reg_window32));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) err |= __put_user(fp, &rwin->rwbuf_stkptrs[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) int restore_rwin_state(__siginfo_rwin_t __user *rp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) struct thread_info *t = current_thread_info();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) int i, wsaved, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) if (((unsigned long) rp) & 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) get_user(wsaved, &rp->wsaved);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) if (wsaved > NSWINS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) for (i = 0; i < wsaved; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) err |= copy_from_user(&t->reg_window[i],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) &rp->reg_window[i],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) sizeof(struct reg_window32));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) err |= __get_user(t->rwbuf_stkptrs[i],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) &rp->rwbuf_stkptrs[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) t->w_saved = wsaved;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) synchronize_user_stack();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) if (t->w_saved)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) }