1 // SPDX-License-Identifier: GPL-2.0-only
3 * (not much of an) Emulation layer for 32bit guests.
5 * Copyright (C) 2012,2013 - ARM Ltd
6 * Author: Marc Zyngier <marc.zyngier@arm.com>
8 * based on arch/arm/kvm/emulate.c
9 * Copyright (C) 2012 - Virtual Open Systems and Columbia University
10 * Author: Christoffer Dall <c.dall@virtualopensystems.com>
13 #include <linux/bits.h>
14 #include <linux/kvm_host.h>
15 #include <asm/kvm_emulate.h>
16 #include <asm/kvm_hyp.h>
19 * Table taken from ARMv8 ARM DDI0487B-B, table G1-10.
21 static const u8 return_offsets[8][2] = {
22 [0] = { 0, 0 }, /* Reset, unused */
23 [1] = { 4, 2 }, /* Undefined */
24 [2] = { 0, 0 }, /* SVC, unused */
25 [3] = { 4, 4 }, /* Prefetch abort */
26 [4] = { 8, 8 }, /* Data abort */
27 [5] = { 0, 0 }, /* HVC, unused */
28 [6] = { 4, 4 }, /* IRQ, unused */
29 [7] = { 4, 4 }, /* FIQ, unused */
33 * When an exception is taken, most CPSR fields are left unchanged in the
34 * handler. However, some are explicitly overridden (e.g. M[4:0]).
36 * The SPSR/SPSR_ELx layouts differ, and the below is intended to work with
37 * either format. Note: SPSR.J bit doesn't exist in SPSR_ELx, but this bit was
38 * obsoleted by the ARMv7 virtualization extensions and is RES0.
40 * For the SPSR layout seen from AArch32, see:
41 * - ARM DDI 0406C.d, page B1-1148
42 * - ARM DDI 0487E.a, page G8-6264
44 * For the SPSR_ELx layout for AArch32 seen from AArch64, see:
45 * - ARM DDI 0487E.a, page C5-426
47 * Here we manipulate the fields in order of the AArch32 SPSR_ELx layout, from
50 static unsigned long get_except32_cpsr(struct kvm_vcpu *vcpu, u32 mode)
52 u32 sctlr = vcpu_cp15(vcpu, c1_SCTLR);
53 unsigned long old, new;
55 old = *vcpu_cpsr(vcpu);
58 new |= (old & PSR_AA32_N_BIT);
59 new |= (old & PSR_AA32_Z_BIT);
60 new |= (old & PSR_AA32_C_BIT);
61 new |= (old & PSR_AA32_V_BIT);
62 new |= (old & PSR_AA32_Q_BIT);
64 // CPSR.IT[7:0] are set to zero upon any exception
65 // See ARM DDI 0487E.a, section G1.12.3
66 // See ARM DDI 0406C.d, section B1.8.3
68 new |= (old & PSR_AA32_DIT_BIT);
70 // CPSR.SSBS is set to SCTLR.DSSBS upon any exception
71 // See ARM DDI 0487E.a, page G8-6244
73 new |= PSR_AA32_SSBS_BIT;
75 // CPSR.PAN is unchanged unless SCTLR.SPAN == 0b0
76 // SCTLR.SPAN is RES1 when ARMv8.1-PAN is not implemented
77 // See ARM DDI 0487E.a, page G8-6246
78 new |= (old & PSR_AA32_PAN_BIT);
79 if (!(sctlr & BIT(23)))
80 new |= PSR_AA32_PAN_BIT;
82 // SS does not exist in AArch32, so ignore
84 // CPSR.IL is set to zero upon any exception
85 // See ARM DDI 0487E.a, page G1-5527
87 new |= (old & PSR_AA32_GE_MASK);
89 // CPSR.IT[7:0] are set to zero upon any exception
90 // See prior comment above
92 // CPSR.E is set to SCTLR.EE upon any exception
93 // See ARM DDI 0487E.a, page G8-6245
94 // See ARM DDI 0406C.d, page B4-1701
96 new |= PSR_AA32_E_BIT;
98 // CPSR.A is unchanged upon an exception to Undefined, Supervisor
99 // CPSR.A is set upon an exception to other modes
100 // See ARM DDI 0487E.a, pages G1-5515 to G1-5516
101 // See ARM DDI 0406C.d, page B1-1182
102 new |= (old & PSR_AA32_A_BIT);
103 if (mode != PSR_AA32_MODE_UND && mode != PSR_AA32_MODE_SVC)
104 new |= PSR_AA32_A_BIT;
106 // CPSR.I is set upon any exception
107 // See ARM DDI 0487E.a, pages G1-5515 to G1-5516
108 // See ARM DDI 0406C.d, page B1-1182
109 new |= PSR_AA32_I_BIT;
111 // CPSR.F is set upon an exception to FIQ
112 // CPSR.F is unchanged upon an exception to other modes
113 // See ARM DDI 0487E.a, pages G1-5515 to G1-5516
114 // See ARM DDI 0406C.d, page B1-1182
115 new |= (old & PSR_AA32_F_BIT);
116 if (mode == PSR_AA32_MODE_FIQ)
117 new |= PSR_AA32_F_BIT;
119 // CPSR.T is set to SCTLR.TE upon any exception
120 // See ARM DDI 0487E.a, page G8-5514
121 // See ARM DDI 0406C.d, page B1-1181
123 new |= PSR_AA32_T_BIT;
130 static void prepare_fault32(struct kvm_vcpu *vcpu, u32 mode, u32 vect_offset)
132 unsigned long new_spsr_value = *vcpu_cpsr(vcpu);
133 bool is_thumb = (new_spsr_value & PSR_AA32_T_BIT);
134 u32 return_offset = return_offsets[vect_offset >> 2][is_thumb];
135 u32 sctlr = vcpu_cp15(vcpu, c1_SCTLR);
137 *vcpu_cpsr(vcpu) = get_except32_cpsr(vcpu, mode);
139 /* Note: These now point to the banked copies */
140 vcpu_write_spsr(vcpu, new_spsr_value);
141 *vcpu_reg32(vcpu, 14) = *vcpu_pc(vcpu) + return_offset;
143 /* Branch to exception vector */
144 if (sctlr & (1 << 13))
145 vect_offset += 0xffff0000;
146 else /* always have security exceptions */
147 vect_offset += vcpu_cp15(vcpu, c12_VBAR);
149 *vcpu_pc(vcpu) = vect_offset;
152 void kvm_inject_undef32(struct kvm_vcpu *vcpu)
154 prepare_fault32(vcpu, PSR_AA32_MODE_UND, 4);
158 * Modelled after TakeDataAbortException() and TakePrefetchAbortException
161 static void inject_abt32(struct kvm_vcpu *vcpu, bool is_pabt,
170 far = &vcpu_cp15(vcpu, c6_IFAR);
171 fsr = &vcpu_cp15(vcpu, c5_IFSR);
174 far = &vcpu_cp15(vcpu, c6_DFAR);
175 fsr = &vcpu_cp15(vcpu, c5_DFSR);
178 prepare_fault32(vcpu, PSR_AA32_MODE_ABT, vect_offset);
182 /* Give the guest an IMPLEMENTATION DEFINED exception */
183 is_lpae = (vcpu_cp15(vcpu, c2_TTBCR) >> 31);
185 *fsr = 1 << 9 | 0x34;
190 void kvm_inject_dabt32(struct kvm_vcpu *vcpu, unsigned long addr)
192 inject_abt32(vcpu, false, addr);
195 void kvm_inject_pabt32(struct kvm_vcpu *vcpu, unsigned long addr)
197 inject_abt32(vcpu, true, addr);