1*b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 25598473aSDavid S. Miller #include <linux/kernel.h> 35598473aSDavid S. Miller #include <linux/types.h> 45598473aSDavid S. Miller #include <linux/thread_info.h> 55598473aSDavid S. Miller #include <linux/uaccess.h> 65598473aSDavid S. Miller #include <linux/sched.h> 75598473aSDavid S. Miller 85598473aSDavid S. Miller #include <asm/sigcontext.h> 95598473aSDavid S. Miller #include <asm/fpumacro.h> 105598473aSDavid S. Miller #include <asm/ptrace.h> 11d550bbd4SDavid Howells #include <asm/switch_to.h> 125598473aSDavid S. Miller 135598473aSDavid S. Miller #include "sigutil.h" 145598473aSDavid S. Miller 155598473aSDavid S. Miller int save_fpu_state(struct pt_regs *regs, __siginfo_fpu_t __user *fpu) 165598473aSDavid S. Miller { 175598473aSDavid S. Miller int err = 0; 185598473aSDavid S. Miller #ifdef CONFIG_SMP 195598473aSDavid S. Miller if (test_tsk_thread_flag(current, TIF_USEDFPU)) { 205598473aSDavid S. Miller put_psr(get_psr() | PSR_EF); 215598473aSDavid S. Miller fpsave(¤t->thread.float_regs[0], ¤t->thread.fsr, 225598473aSDavid S. Miller ¤t->thread.fpqueue[0], ¤t->thread.fpqdepth); 235598473aSDavid S. Miller regs->psr &= ~(PSR_EF); 245598473aSDavid S. Miller clear_tsk_thread_flag(current, TIF_USEDFPU); 255598473aSDavid S. Miller } 265598473aSDavid S. Miller #else 275598473aSDavid S. Miller if (current == last_task_used_math) { 285598473aSDavid S. Miller put_psr(get_psr() | PSR_EF); 295598473aSDavid S. Miller fpsave(¤t->thread.float_regs[0], ¤t->thread.fsr, 305598473aSDavid S. Miller ¤t->thread.fpqueue[0], ¤t->thread.fpqdepth); 315598473aSDavid S. Miller last_task_used_math = NULL; 325598473aSDavid S. Miller regs->psr &= ~(PSR_EF); 335598473aSDavid S. Miller } 345598473aSDavid S. Miller #endif 355598473aSDavid S. Miller err |= __copy_to_user(&fpu->si_float_regs[0], 365598473aSDavid S. Miller ¤t->thread.float_regs[0], 375598473aSDavid S. Miller (sizeof(unsigned long) * 32)); 385598473aSDavid S. Miller err |= __put_user(current->thread.fsr, &fpu->si_fsr); 395598473aSDavid S. Miller err |= __put_user(current->thread.fpqdepth, &fpu->si_fpqdepth); 405598473aSDavid S. Miller if (current->thread.fpqdepth != 0) 415598473aSDavid S. Miller err |= __copy_to_user(&fpu->si_fpqueue[0], 425598473aSDavid S. Miller ¤t->thread.fpqueue[0], 435598473aSDavid S. Miller ((sizeof(unsigned long) + 445598473aSDavid S. Miller (sizeof(unsigned long *)))*16)); 455598473aSDavid S. Miller clear_used_math(); 465598473aSDavid S. Miller return err; 475598473aSDavid S. Miller } 485598473aSDavid S. Miller 495598473aSDavid S. Miller int restore_fpu_state(struct pt_regs *regs, __siginfo_fpu_t __user *fpu) 505598473aSDavid S. Miller { 515598473aSDavid S. Miller int err; 52d11c2a0dSDavid S. Miller 53d11c2a0dSDavid S. Miller if (((unsigned long) fpu) & 3) 54d11c2a0dSDavid S. Miller return -EFAULT; 55d11c2a0dSDavid S. Miller 565598473aSDavid S. Miller #ifdef CONFIG_SMP 575598473aSDavid S. Miller if (test_tsk_thread_flag(current, TIF_USEDFPU)) 585598473aSDavid S. Miller regs->psr &= ~PSR_EF; 595598473aSDavid S. Miller #else 605598473aSDavid S. Miller if (current == last_task_used_math) { 615598473aSDavid S. Miller last_task_used_math = NULL; 625598473aSDavid S. Miller regs->psr &= ~PSR_EF; 635598473aSDavid S. Miller } 645598473aSDavid S. Miller #endif 655598473aSDavid S. Miller set_used_math(); 665598473aSDavid S. Miller clear_tsk_thread_flag(current, TIF_USEDFPU); 675598473aSDavid S. Miller 685598473aSDavid S. Miller if (!access_ok(VERIFY_READ, fpu, sizeof(*fpu))) 695598473aSDavid S. Miller return -EFAULT; 705598473aSDavid S. Miller 715598473aSDavid S. Miller err = __copy_from_user(¤t->thread.float_regs[0], &fpu->si_float_regs[0], 725598473aSDavid S. Miller (sizeof(unsigned long) * 32)); 735598473aSDavid S. Miller err |= __get_user(current->thread.fsr, &fpu->si_fsr); 745598473aSDavid S. Miller err |= __get_user(current->thread.fpqdepth, &fpu->si_fpqdepth); 755598473aSDavid S. Miller if (current->thread.fpqdepth != 0) 765598473aSDavid S. Miller err |= __copy_from_user(¤t->thread.fpqueue[0], 775598473aSDavid S. Miller &fpu->si_fpqueue[0], 785598473aSDavid S. Miller ((sizeof(unsigned long) + 795598473aSDavid S. Miller (sizeof(unsigned long *)))*16)); 805598473aSDavid S. Miller return err; 815598473aSDavid S. Miller } 825598473aSDavid S. Miller 835598473aSDavid S. Miller int save_rwin_state(int wsaved, __siginfo_rwin_t __user *rwin) 845598473aSDavid S. Miller { 855598473aSDavid S. Miller int i, err = __put_user(wsaved, &rwin->wsaved); 865598473aSDavid S. Miller 875598473aSDavid S. Miller for (i = 0; i < wsaved; i++) { 885598473aSDavid S. Miller struct reg_window32 *rp; 895598473aSDavid S. Miller unsigned long fp; 905598473aSDavid S. Miller 915598473aSDavid S. Miller rp = ¤t_thread_info()->reg_window[i]; 925598473aSDavid S. Miller fp = current_thread_info()->rwbuf_stkptrs[i]; 935598473aSDavid S. Miller err |= copy_to_user(&rwin->reg_window[i], rp, 945598473aSDavid S. Miller sizeof(struct reg_window32)); 955598473aSDavid S. Miller err |= __put_user(fp, &rwin->rwbuf_stkptrs[i]); 965598473aSDavid S. Miller } 975598473aSDavid S. Miller return err; 985598473aSDavid S. Miller } 995598473aSDavid S. Miller 1005598473aSDavid S. Miller int restore_rwin_state(__siginfo_rwin_t __user *rp) 1015598473aSDavid S. Miller { 1025598473aSDavid S. Miller struct thread_info *t = current_thread_info(); 1035598473aSDavid S. Miller int i, wsaved, err; 1045598473aSDavid S. Miller 105d11c2a0dSDavid S. Miller if (((unsigned long) rp) & 3) 106d11c2a0dSDavid S. Miller return -EFAULT; 107d11c2a0dSDavid S. Miller 108d11c2a0dSDavid S. Miller get_user(wsaved, &rp->wsaved); 1095598473aSDavid S. Miller if (wsaved > NSWINS) 1105598473aSDavid S. Miller return -EFAULT; 1115598473aSDavid S. Miller 1125598473aSDavid S. Miller err = 0; 1135598473aSDavid S. Miller for (i = 0; i < wsaved; i++) { 1145598473aSDavid S. Miller err |= copy_from_user(&t->reg_window[i], 1155598473aSDavid S. Miller &rp->reg_window[i], 1165598473aSDavid S. Miller sizeof(struct reg_window32)); 1175598473aSDavid S. Miller err |= __get_user(t->rwbuf_stkptrs[i], 1185598473aSDavid S. Miller &rp->rwbuf_stkptrs[i]); 1195598473aSDavid S. Miller } 1205598473aSDavid S. Miller if (err) 1215598473aSDavid S. Miller return err; 1225598473aSDavid S. Miller 1235598473aSDavid S. Miller t->w_saved = wsaved; 1245598473aSDavid S. Miller synchronize_user_stack(); 1255598473aSDavid S. Miller if (t->w_saved) 1265598473aSDavid S. Miller return -EFAULT; 1275598473aSDavid S. Miller return 0; 1285598473aSDavid S. Miller 1295598473aSDavid S. Miller } 130