blob: d2783494f0a0dd75dcdf604c6d49804fd0cfbf9c [file] [log] [blame]
Fuad Tabbac76466d2019-09-06 10:42:12 +01001/*
2 * Copyright 2019 The Hafnium Authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * https://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "debug_el1.h"
18
19#include "hf/check.h"
20#include "hf/dlog.h"
21#include "hf/panic.h"
22#include "hf/types.h"
23
24#include "msr.h"
25
26/**
27 * Controls traps for Trace Filter.
28 */
29#define MDCR_EL2_TTRF (1u << 19)
30
31/**
32 * Controls traps for Debug ROM.
33 */
34#define MDCR_EL2_TDRA (1u << 11)
35
36/**
37 * Controls traps for OS-Related Register Access.
38 */
39#define MDCR_EL2_TDOSA (1u << 10)
40
41/**
42 * Controls traps for remaining Debug Registers not trapped by TDRA and TDOSA.
43 */
44#define MDCR_EL2_TDA (1u << 9)
45
46/**
47 * Controls traps for all debug exceptions (e.g., breakpoints).
48 */
49#define MDCR_EL2_TDE (1u << 8)
50
51/**
52 * Controls traps for debug events, i.e., breakpoints, watchpoints, and vector.
53 * catch exceptions.
54 */
55#define MDSCR_EL1_MDE (1u << 15)
56
57/**
58 * System register are identified by op0, op2, op1, crn, crm. The ISS encoding
59 * includes also rt and direction. Exclude them, @see D13.2.37 (D13-2977).
60 */
61#define ISS_SYSREG_MASK \
62 (((1u << 22) - 1u) & /* Select the ISS bits*/ \
63 ~(0x1fu << 5) & /* exclude rt */ \
64 ~1u /* exclude direction */)
65
66#define GET_ISS_SYSREG(esr) (ISS_SYSREG_MASK & (esr))
67
68/**
69 * Op0 from the ISS encoding in the ESR.
70 */
71#define ISS_OP0_MASK 0x300000
72#define ISS_OP0_SHIFT 20
73#define GET_ISS_OP0(esr) ((ISS_OP0_MASK & (esr)) >> ISS_OP0_SHIFT)
74
75/**
76 * Op1 from the ISS encoding in the ESR.
77 */
78#define ISS_OP1_MASK 0x1c000
79#define ISS_OP1_SHIFT 14
80#define GET_ISS_OP1(esr) ((ISS_OP1_MASK & (esr)) >> ISS_OP1_SHIFT)
81
82/**
83 * Direction (i.e., read (1) or write (0), is the first bit in the ISS/ESR.
84 */
85#define ISS_DIRECTION_MASK 1u
86
87/**
88 * Gets the direction of the system register access, read (1) or write (0).
89 */
90#define GET_ISS_DIRECTION(esr) (ISS_DIRECTION_MASK & (esr))
91
92/**
93 * True if the ISS encoded in the esr indicates a read of the system register.
94 */
95#define ISS_IS_READ(esr) (ISS_DIRECTION_MASK & (esr))
96
97/**
98 * Rt, which identifies the general purpose register used for the operation.
99 */
100#define ISS_RT_MASK 0x3e0
101#define ISS_RT_SHIFT 5
102#define GET_ISS_RT(esr) ((ISS_RT_MASK & (esr)) >> ISS_RT_SHIFT)
103
104/**
105 * Definitions of read-only debug registers' ISS signatures.
106 */
107#define EL1_DEBUG_REGISTERS_READ \
108 X(MDRAR_EL1, 0x200400) \
109 X(DBGAUTHSTATUS_EL1, 0x2c1c1c) \
110 X(OSLSR_EL1, 0x280402)
111
112/**
113 * Definitions of readable and writeable debug registers' ISS signatures.
114 */
115#define EL1_DEBUG_REGISTERS_READ_WRITE \
116 X(DBGCLAIMCLR_EL1, 0x2c1c12) \
117 X(DBGCLAIMSET_EL1, 0x2c1c10) \
118 X(DBGPRCR_EL1, 0x280408) \
119 X(MDCCINT_EL1, 0x200004) \
120 X(MDSCR_EL1, 0x240004) \
121 X(OSDLR_EL1, 0x280406) \
122 X(OSDTRRX_EL1, 0x240000) \
123 X(OSDTRTX_EL1, 0x240006) \
124 X(OSECCR_EL1, 0x24000c) \
125 X(DBGBCR0_EL1, 0x2a0000) \
126 X(DBGBCR1_EL1, 0x2a0002) \
127 X(DBGBCR2_EL1, 0x2a0004) \
128 X(DBGBCR3_EL1, 0x2a0006) \
129 X(DBGBCR4_EL1, 0x2a0008) \
130 X(DBGBCR5_EL1, 0x2a000a) \
131 X(DBGBCR6_EL1, 0x2a000c) \
132 X(DBGBCR7_EL1, 0x2a000e) \
133 X(DBGBCR8_EL1, 0x2a0010) \
134 X(DBGBCR9_EL1, 0x2a0012) \
135 X(DBGBCR10_EL1, 0x2a0014) \
136 X(DBGBCR11_EL1, 0x2a0016) \
137 X(DBGBCR12_EL1, 0x2a0018) \
138 X(DBGBCR13_EL1, 0x2a001a) \
139 X(DBGBCR14_EL1, 0x2a001c) \
140 X(DBGBCR15_EL1, 0x2a001e) \
141 X(DBGBVR0_EL1, 0x280000) \
142 X(DBGBVR1_EL1, 0x280002) \
143 X(DBGBVR2_EL1, 0x280004) \
144 X(DBGBVR3_EL1, 0x280006) \
145 X(DBGBVR4_EL1, 0x280008) \
146 X(DBGBVR5_EL1, 0x28000a) \
147 X(DBGBVR6_EL1, 0x28000c) \
148 X(DBGBVR7_EL1, 0x28000e) \
149 X(DBGBVR8_EL1, 0x280010) \
150 X(DBGBVR9_EL1, 0x280012) \
151 X(DBGBVR10_EL1, 0x280014) \
152 X(DBGBVR11_EL1, 0x280016) \
153 X(DBGBVR12_EL1, 0x280018) \
154 X(DBGBVR13_EL1, 0x28001a) \
155 X(DBGBVR14_EL1, 0x28001c) \
156 X(DBGBVR15_EL1, 0x28001e) \
157 X(DBGWCR0_EL1, 0x2e0000) \
158 X(DBGWCR1_EL1, 0x2e0002) \
159 X(DBGWCR2_EL1, 0x2e0004) \
160 X(DBGWCR3_EL1, 0x2e0006) \
161 X(DBGWCR4_EL1, 0x2e0008) \
162 X(DBGWCR5_EL1, 0x2e000a) \
163 X(DBGWCR6_EL1, 0x2e000c) \
164 X(DBGWCR7_EL1, 0x2e000e) \
165 X(DBGWCR8_EL1, 0x2e0010) \
166 X(DBGWCR9_EL1, 0x2e0012) \
167 X(DBGWCR10_EL1, 0x2e0014) \
168 X(DBGWCR11_EL1, 0x2e0016) \
169 X(DBGWCR12_EL1, 0x2e0018) \
170 X(DBGWCR13_EL1, 0x2e001a) \
171 X(DBGWCR14_EL1, 0x2e001c) \
172 X(DBGWCR15_EL1, 0x2e001e) \
173 X(DBGWVR0_EL1, 0x2c0000) \
174 X(DBGWVR1_EL1, 0x2c0002) \
175 X(DBGWVR2_EL1, 0x2c0004) \
176 X(DBGWVR3_EL1, 0x2c0006) \
177 X(DBGWVR4_EL1, 0x2c0008) \
178 X(DBGWVR5_EL1, 0x2c000a) \
179 X(DBGWVR6_EL1, 0x2c000c) \
180 X(DBGWVR7_EL1, 0x2c000e) \
181 X(DBGWVR8_EL1, 0x2c0010) \
182 X(DBGWVR9_EL1, 0x2c0012) \
183 X(DBGWVR10_EL1, 0x2c0014) \
184 X(DBGWVR11_EL1, 0x2c0016) \
185 X(DBGWVR12_EL1, 0x2c0018) \
186 X(DBGWVR13_EL1, 0x2c001a) \
187 X(DBGWVR14_EL1, 0x2c001c) \
188 X(DBGWVR15_EL1, 0x2c001e)
189
190/**
191 * Definitions of all debug registers' ISS signatures.
192 */
193#define EL1_DEBUG_REGISTERS \
194 EL1_DEBUG_REGISTERS_READ \
195 EL1_DEBUG_REGISTERS_READ_WRITE
196
197/**
198 * Returns the value for mdcr_el2 for the particular VM.
199 * For now, the primary VM has one value and all secondary VMs share a value.
200 */
201uintreg_t get_mdcr_el2_value(spci_vm_id_t vm_id)
202{
203 if (vm_id == HF_PRIMARY_VM_ID) {
204 /*
205 * Trap primary VM accesses to debug registers to have fine
206 * grained control over system register accesses.
207 * Do not trap the Primary VM's debug events (!MDCR_EL2_TDE).
208 */
209 return MDCR_EL2_TTRF | MDCR_EL2_TDRA | MDCR_EL2_TDOSA |
210 MDCR_EL2_TDA;
211 }
212
213 /*
214 * Trap all secondary VM debug register accesses as well as debug
215 * event exceptions.
216 * Debug event exceptions should be disabled in secondary VMs, but trap
217 * them for additional security (MDCR_EL2_TDE).
218 */
219 return MDCR_EL2_TTRF | MDCR_EL2_TDRA | MDCR_EL2_TDOSA | MDCR_EL2_TDA |
220 MDCR_EL2_TDE;
221}
222
223/**
224 * Returns true if the ESR register shows an access to an EL1 debug register.
225 */
226bool is_debug_el1_register_access(uintreg_t esr_el2)
227{
228 /*
229 * Architecture Reference Manual D12.2: op0 == 0b10 is for debug and
230 * trace system registers. op1 = 0x1 for trace, remaining are debug.
231 */
232 return GET_ISS_OP0(esr_el2) == 0x2 && GET_ISS_OP1(esr_el2) != 0x1;
233}
234
235/**
236 * Processes an access (msr, mrs) to an EL1 debug register.
237 * Returns true if the access was allowed and performed, false otherwise.
238 */
239bool debug_el1_process_access(struct vcpu *vcpu, spci_vm_id_t vm_id,
240 uintreg_t esr_el2)
241{
242 /*
243 * For now, debug registers are not supported by secondary VMs.
244 * Disallow accesses to them.
245 */
246 if (vm_id != HF_PRIMARY_VM_ID) {
247 return false;
248 }
249
250 uintreg_t sys_register = GET_ISS_SYSREG(esr_el2);
251 uintreg_t rt_register = GET_ISS_RT(esr_el2);
252 uintreg_t value;
253
254 CHECK(rt_register < NUM_GP_REGS);
255
256 if (ISS_IS_READ(esr_el2)) {
257 switch (sys_register) {
258#define X(reg_name, reg_sig) \
259 case reg_sig: \
260 value = read_msr(reg_name); \
261 break;
262 EL1_DEBUG_REGISTERS
263#undef X
264 default:
265 value = vcpu->regs.r[rt_register];
266 dlog("Unsupported system register read 0x%x\n",
267 sys_register);
268 break;
269 }
270 vcpu->regs.r[rt_register] = value;
271
272 } else {
273 value = vcpu->regs.r[rt_register];
274 switch (sys_register) {
275#define X(reg_name, reg_sig) \
276 case reg_sig: \
277 write_msr(reg_name, value); \
278 break;
279 EL1_DEBUG_REGISTERS_READ_WRITE
280#undef X
281 default:
282 dlog("Unsupported system register write 0x%x\n",
283 sys_register);
284 break;
285 }
286 }
287
288 return true;
289}