Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / arch / mn10300 / kernel / fpu.c
blob50ce7b447fed4b09d5a728a5de93879708a995f4
1 /* MN10300 FPU management
3 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public Licence
8 * as published by the Free Software Foundation; either version
9 * 2 of the Licence, or (at your option) any later version.
11 #include <linux/uaccess.h>
12 #include <linux/sched/signal.h>
14 #include <asm/fpu.h>
15 #include <asm/elf.h>
16 #include <asm/exceptions.h>
18 #ifdef CONFIG_LAZY_SAVE_FPU
19 struct task_struct *fpu_state_owner;
20 #endif
23 * error functions in FPU disabled exception
25 asmlinkage void fpu_disabled_in_kernel(struct pt_regs *regs)
27 die_if_no_fixup("An FPU Disabled exception happened in kernel space\n",
28 regs, EXCEP_FPU_DISABLED);
32 * handle an FPU operational exception
33 * - there's a possibility that if the FPU is asynchronous, the signal might
34 * be meant for a process other than the current one
36 asmlinkage void fpu_exception(struct pt_regs *regs, enum exception_code code)
38 struct task_struct *tsk = current;
39 siginfo_t info;
40 u32 fpcr;
42 if (!user_mode(regs))
43 die_if_no_fixup("An FPU Operation exception happened in"
44 " kernel space\n",
45 regs, code);
47 if (!is_using_fpu(tsk))
48 die_if_no_fixup("An FPU Operation exception happened,"
49 " but the FPU is not in use",
50 regs, code);
52 info.si_signo = SIGFPE;
53 info.si_errno = 0;
54 info.si_addr = (void *) tsk->thread.uregs->pc;
55 info.si_code = FPE_FLTINV;
57 unlazy_fpu(tsk);
59 fpcr = tsk->thread.fpu_state.fpcr;
61 if (fpcr & FPCR_EC_Z)
62 info.si_code = FPE_FLTDIV;
63 else if (fpcr & FPCR_EC_O)
64 info.si_code = FPE_FLTOVF;
65 else if (fpcr & FPCR_EC_U)
66 info.si_code = FPE_FLTUND;
67 else if (fpcr & FPCR_EC_I)
68 info.si_code = FPE_FLTRES;
70 force_sig_info(SIGFPE, &info, tsk);
74 * save the FPU state to a signal context
76 int fpu_setup_sigcontext(struct fpucontext *fpucontext)
78 struct task_struct *tsk = current;
80 if (!is_using_fpu(tsk))
81 return 0;
83 /* transfer the current FPU state to memory and cause fpu_init() to be
84 * triggered by the next attempted FPU operation by the current
85 * process.
87 preempt_disable();
89 #ifndef CONFIG_LAZY_SAVE_FPU
90 if (tsk->thread.fpu_flags & THREAD_HAS_FPU) {
91 fpu_save(&tsk->thread.fpu_state);
92 tsk->thread.uregs->epsw &= ~EPSW_FE;
93 tsk->thread.fpu_flags &= ~THREAD_HAS_FPU;
95 #else /* !CONFIG_LAZY_SAVE_FPU */
96 if (fpu_state_owner == tsk) {
97 fpu_save(&tsk->thread.fpu_state);
98 fpu_state_owner->thread.uregs->epsw &= ~EPSW_FE;
99 fpu_state_owner = NULL;
101 #endif /* !CONFIG_LAZY_SAVE_FPU */
103 preempt_enable();
105 /* we no longer have a valid current FPU state */
106 clear_using_fpu(tsk);
108 /* transfer the saved FPU state onto the userspace stack */
109 if (copy_to_user(fpucontext,
110 &tsk->thread.fpu_state,
111 min(sizeof(struct fpu_state_struct),
112 sizeof(struct fpucontext))))
113 return -1;
115 return 1;
119 * kill a process's FPU state during restoration after signal handling
121 void fpu_kill_state(struct task_struct *tsk)
123 /* disown anything left in the FPU */
124 preempt_disable();
126 #ifndef CONFIG_LAZY_SAVE_FPU
127 if (tsk->thread.fpu_flags & THREAD_HAS_FPU) {
128 tsk->thread.uregs->epsw &= ~EPSW_FE;
129 tsk->thread.fpu_flags &= ~THREAD_HAS_FPU;
131 #else /* !CONFIG_LAZY_SAVE_FPU */
132 if (fpu_state_owner == tsk) {
133 fpu_state_owner->thread.uregs->epsw &= ~EPSW_FE;
134 fpu_state_owner = NULL;
136 #endif /* !CONFIG_LAZY_SAVE_FPU */
138 preempt_enable();
140 /* we no longer have a valid current FPU state */
141 clear_using_fpu(tsk);
145 * restore the FPU state from a signal context
147 int fpu_restore_sigcontext(struct fpucontext *fpucontext)
149 struct task_struct *tsk = current;
150 int ret;
152 /* load up the old FPU state */
153 ret = copy_from_user(&tsk->thread.fpu_state, fpucontext,
154 min(sizeof(struct fpu_state_struct),
155 sizeof(struct fpucontext)));
156 if (!ret)
157 set_using_fpu(tsk);
159 return ret;
163 * fill in the FPU structure for a core dump
165 int dump_fpu(struct pt_regs *regs, elf_fpregset_t *fpreg)
167 struct task_struct *tsk = current;
168 int fpvalid;
170 fpvalid = is_using_fpu(tsk);
171 if (fpvalid) {
172 unlazy_fpu(tsk);
173 memcpy(fpreg, &tsk->thread.fpu_state, sizeof(*fpreg));
176 return fpvalid;