Skip to content

Commit 59cbcdb

Browse files
author
Marc Zyngier
committed
ARM: KVM: Add VFP save/restore
This is almost a copy/paste of the existing version, with a couple of subtle differences: - Only write to FPEXC once on the save path - Add an isb when enabling VFP access The patch also defines a few sysreg accessors and a __vfp_enabled predicate that test the VFP trapping state. Reviewed-by: Christoffer Dall <[email protected]> Signed-off-by: Marc Zyngier <[email protected]>
1 parent c0c2cdb commit 59cbcdb

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed

arch/arm/kvm/hyp/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ obj-$(CONFIG_KVM_ARM_HOST) += tlb.o
66
obj-$(CONFIG_KVM_ARM_HOST) += cp15-sr.o
77
obj-$(CONFIG_KVM_ARM_HOST) += timer-sr.o
88
obj-$(CONFIG_KVM_ARM_HOST) += vgic-v2-sr.o
9+
obj-$(CONFIG_KVM_ARM_HOST) += vfp.o

arch/arm/kvm/hyp/hyp.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <linux/compiler.h>
2222
#include <linux/kvm_host.h>
2323
#include <asm/kvm_mmu.h>
24+
#include <asm/vfp.h>
2425

2526
#define __hyp_text __section(.hyp.text) notrace
2627

@@ -31,6 +32,8 @@
3132
"mrc", "mcr", __stringify(p15, Op1, %0, CRn, CRm, Op2), u32
3233
#define __ACCESS_CP15_64(Op1, CRm) \
3334
"mrrc", "mcrr", __stringify(p15, Op1, %Q0, %R0, CRm), u64
35+
#define __ACCESS_VFP(CRn) \
36+
"mrc", "mcr", __stringify(p10, 7, %0, CRn, cr0, 0), u32
3437

3538
#define __write_sysreg(v, r, w, c, t) asm volatile(w " " c : : "r" ((t)(v)))
3639
#define write_sysreg(v, ...) __write_sysreg(v, __VA_ARGS__)
@@ -53,6 +56,7 @@
5356
#define VMPIDR __ACCESS_CP15(c0, 4, c0, 5)
5457
#define SCTLR __ACCESS_CP15(c1, 0, c0, 0)
5558
#define CPACR __ACCESS_CP15(c1, 0, c0, 2)
59+
#define HCPTR __ACCESS_CP15(c1, 4, c1, 2)
5660
#define TTBCR __ACCESS_CP15(c2, 0, c0, 2)
5761
#define DACR __ACCESS_CP15(c3, 0, c0, 0)
5862
#define DFSR __ACCESS_CP15(c5, 0, c0, 0)
@@ -77,6 +81,8 @@
7781
#define CNTV_CTL __ACCESS_CP15(c14, 0, c3, 1)
7882
#define CNTHCTL __ACCESS_CP15(c14, 4, c1, 0)
7983

84+
#define VFP_FPEXC __ACCESS_VFP(FPEXC)
85+
8086
void __timer_save_state(struct kvm_vcpu *vcpu);
8187
void __timer_restore_state(struct kvm_vcpu *vcpu);
8288

@@ -86,4 +92,11 @@ void __vgic_v2_restore_state(struct kvm_vcpu *vcpu);
8692
void __sysreg_save_state(struct kvm_cpu_context *ctxt);
8793
void __sysreg_restore_state(struct kvm_cpu_context *ctxt);
8894

95+
void asmlinkage __vfp_save_state(struct vfp_hard_struct *vfp);
96+
void asmlinkage __vfp_restore_state(struct vfp_hard_struct *vfp);
97+
static inline bool __vfp_enabled(void)
98+
{
99+
return !(read_sysreg(HCPTR) & (HCPTR_TCP(11) | HCPTR_TCP(10)));
100+
}
101+
89102
#endif /* __ARM_KVM_HYP_H__ */

arch/arm/kvm/hyp/vfp.S

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright (C) 2012 - Virtual Open Systems and Columbia University
3+
* Author: Christoffer Dall <[email protected]>
4+
*
5+
* This program is free software; you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License version 2 as
7+
* published by the Free Software Foundation.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
#include <linux/linkage.h>
19+
#include <asm/vfpmacros.h>
20+
21+
.text
22+
.pushsection .hyp.text, "ax"
23+
24+
/* void __vfp_save_state(struct vfp_hard_struct *vfp); */
25+
ENTRY(__vfp_save_state)
26+
push {r4, r5}
27+
VFPFMRX r1, FPEXC
28+
29+
@ Make sure *really* VFP is enabled so we can touch the registers.
30+
orr r5, r1, #FPEXC_EN
31+
tst r5, #FPEXC_EX @ Check for VFP Subarchitecture
32+
bic r5, r5, #FPEXC_EX @ FPEXC_EX disable
33+
VFPFMXR FPEXC, r5
34+
isb
35+
36+
VFPFMRX r2, FPSCR
37+
beq 1f
38+
39+
@ If FPEXC_EX is 0, then FPINST/FPINST2 reads are upredictable, so
40+
@ we only need to save them if FPEXC_EX is set.
41+
VFPFMRX r3, FPINST
42+
tst r5, #FPEXC_FP2V
43+
VFPFMRX r4, FPINST2, ne @ vmrsne
44+
1:
45+
VFPFSTMIA r0, r5 @ Save VFP registers
46+
stm r0, {r1-r4} @ Save FPEXC, FPSCR, FPINST, FPINST2
47+
pop {r4, r5}
48+
bx lr
49+
ENDPROC(__vfp_save_state)
50+
51+
/* void __vfp_restore_state(struct vfp_hard_struct *vfp);
52+
* Assume FPEXC_EN is on and FPEXC_EX is off */
53+
ENTRY(__vfp_restore_state)
54+
VFPFLDMIA r0, r1 @ Load VFP registers
55+
ldm r0, {r0-r3} @ Load FPEXC, FPSCR, FPINST, FPINST2
56+
57+
VFPFMXR FPSCR, r1
58+
tst r0, #FPEXC_EX @ Check for VFP Subarchitecture
59+
beq 1f
60+
VFPFMXR FPINST, r2
61+
tst r0, #FPEXC_FP2V
62+
VFPFMXR FPINST2, r3, ne
63+
1:
64+
VFPFMXR FPEXC, r0 @ FPEXC (last, in case !EN)
65+
bx lr
66+
ENDPROC(__vfp_restore_state)
67+
68+
.popsection

0 commit comments

Comments
 (0)