blob: 20e03d9695589057dd0001c39997dc38947d532a [file] [log] [blame]
Christoffer Dall749cf76c2013-01-20 18:28:06 -05001/*
2 * Copyright (C) 2012 - Virtual Open Systems and Columbia University
3 * Author: Christoffer Dall <c.dall@virtualopensystems.com>
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, write to the Free Software
16 * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
Christoffer Dall342cd0a2013-01-20 18:28:06 -050018
19#include <linux/linkage.h>
20#include <linux/const.h>
21#include <asm/unified.h>
22#include <asm/page.h>
Christoffer Dallf7ed45b2013-01-20 18:47:42 -050023#include <asm/ptrace.h>
Christoffer Dall749cf76c2013-01-20 18:28:06 -050024#include <asm/asm-offsets.h>
25#include <asm/kvm_asm.h>
Christoffer Dall342cd0a2013-01-20 18:28:06 -050026#include <asm/kvm_arm.h>
Christoffer Dallf7ed45b2013-01-20 18:47:42 -050027#include <asm/vfpmacros.h>
28#include "interrupts_head.S"
Christoffer Dall342cd0a2013-01-20 18:28:06 -050029
30 .text
31
32__kvm_hyp_code_start:
33 .globl __kvm_hyp_code_start
34
35/********************************************************************
36 * Flush per-VMID TLBs
Christoffer Dallf7ed45b2013-01-20 18:47:42 -050037 *
Marc Zyngier48762762013-01-28 15:27:00 +000038 * void __kvm_tlb_flush_vmid_ipa(struct kvm *kvm, phys_addr_t ipa);
Christoffer Dallf7ed45b2013-01-20 18:47:42 -050039 *
40 * We rely on the hardware to broadcast the TLB invalidation to all CPUs
41 * inside the inner-shareable domain (which is the case for all v7
42 * implementations). If we come across a non-IS SMP implementation, we'll
43 * have to use an IPI based mechanism. Until then, we stick to the simple
44 * hardware assisted version.
Marc Zyngier48762762013-01-28 15:27:00 +000045 *
46 * As v7 does not support flushing per IPA, just nuke the whole TLB
47 * instead, ignoring the ipa value.
Christoffer Dall342cd0a2013-01-20 18:28:06 -050048 */
Marc Zyngier48762762013-01-28 15:27:00 +000049ENTRY(__kvm_tlb_flush_vmid_ipa)
Christoffer Dallf7ed45b2013-01-20 18:47:42 -050050 push {r2, r3}
51
Marc Zyngier479c5ae2013-06-21 13:08:47 +010052 dsb ishst
Christoffer Dallf7ed45b2013-01-20 18:47:42 -050053 add r0, r0, #KVM_VTTBR
54 ldrd r2, r3, [r0]
55 mcrr p15, 6, r2, r3, c2 @ Write VTTBR
56 isb
57 mcr p15, 0, r0, c8, c3, 0 @ TLBIALLIS (rt ignored)
58 dsb
59 isb
60 mov r2, #0
61 mov r3, #0
62 mcrr p15, 6, r2, r3, c2 @ Back to VMID #0
63 isb @ Not necessary if followed by eret
64
65 pop {r2, r3}
Christoffer Dalld5d81842013-01-20 18:28:07 -050066 bx lr
Marc Zyngier48762762013-01-28 15:27:00 +000067ENDPROC(__kvm_tlb_flush_vmid_ipa)
Christoffer Dalld5d81842013-01-20 18:28:07 -050068
69/********************************************************************
Christoffer Dallf7ed45b2013-01-20 18:47:42 -050070 * Flush TLBs and instruction caches of all CPUs inside the inner-shareable
71 * domain, for all VMIDs
72 *
73 * void __kvm_flush_vm_context(void);
Christoffer Dalld5d81842013-01-20 18:28:07 -050074 */
Christoffer Dall342cd0a2013-01-20 18:28:06 -050075ENTRY(__kvm_flush_vm_context)
Christoffer Dallf7ed45b2013-01-20 18:47:42 -050076 mov r0, #0 @ rn parameter for c15 flushes is SBZ
77
78 /* Invalidate NS Non-Hyp TLB Inner Shareable (TLBIALLNSNHIS) */
79 mcr p15, 4, r0, c8, c3, 4
80 /* Invalidate instruction caches Inner Shareable (ICIALLUIS) */
81 mcr p15, 0, r0, c7, c1, 0
82 dsb
83 isb @ Not necessary if followed by eret
84
Christoffer Dall342cd0a2013-01-20 18:28:06 -050085 bx lr
86ENDPROC(__kvm_flush_vm_context)
87
Christoffer Dallf7ed45b2013-01-20 18:47:42 -050088
Christoffer Dall342cd0a2013-01-20 18:28:06 -050089/********************************************************************
90 * Hypervisor world-switch code
Christoffer Dallf7ed45b2013-01-20 18:47:42 -050091 *
92 *
93 * int __kvm_vcpu_run(struct kvm_vcpu *vcpu)
Christoffer Dall342cd0a2013-01-20 18:28:06 -050094 */
95ENTRY(__kvm_vcpu_run)
Christoffer Dallf7ed45b2013-01-20 18:47:42 -050096 @ Save the vcpu pointer
97 mcr p15, 4, vcpu, c13, c0, 2 @ HTPIDR
98
99 save_host_regs
100
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500101 restore_vgic_state
Marc Zyngier53e72402013-01-23 13:21:58 -0500102 restore_timer_state
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500103
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500104 @ Store hardware CP15 state and load guest state
105 read_cp15_state store_to_vcpu = 0
106 write_cp15_state read_from_vcpu = 1
107
108 @ If the host kernel has not been configured with VFPv3 support,
109 @ then it is safer if we deny guests from using it as well.
110#ifdef CONFIG_VFPv3
111 @ Set FPEXC_EN so the guest doesn't trap floating point instructions
112 VFPFMRX r2, FPEXC @ VMRS
113 push {r2}
114 orr r2, r2, #FPEXC_EN
115 VFPFMXR FPEXC, r2 @ VMSR
116#endif
117
118 @ Configure Hyp-role
119 configure_hyp_role vmentry
120
121 @ Trap coprocessor CRx accesses
122 set_hstr vmentry
123 set_hcptr vmentry, (HCPTR_TTA | HCPTR_TCP(10) | HCPTR_TCP(11))
124 set_hdcr vmentry
125
126 @ Write configured ID register into MIDR alias
127 ldr r1, [vcpu, #VCPU_MIDR]
128 mcr p15, 4, r1, c0, c0, 0
129
130 @ Write guest view of MPIDR into VMPIDR
131 ldr r1, [vcpu, #CP15_OFFSET(c0_MPIDR)]
132 mcr p15, 4, r1, c0, c0, 5
133
134 @ Set up guest memory translation
135 ldr r1, [vcpu, #VCPU_KVM]
136 add r1, r1, #KVM_VTTBR
137 ldrd r2, r3, [r1]
138 mcrr p15, 6, r2, r3, c2 @ Write VTTBR
139
140 @ We're all done, just restore the GPRs and go to the guest
141 restore_guest_regs
142 clrex @ Clear exclusive monitor
143 eret
144
145__kvm_vcpu_return:
146 /*
147 * return convention:
148 * guest r0, r1, r2 saved on the stack
149 * r0: vcpu pointer
150 * r1: exception code
151 */
152 save_guest_regs
153
154 @ Set VMID == 0
155 mov r2, #0
156 mov r3, #0
157 mcrr p15, 6, r2, r3, c2 @ Write VTTBR
158
159 @ Don't trap coprocessor accesses for host kernel
160 set_hstr vmexit
161 set_hdcr vmexit
162 set_hcptr vmexit, (HCPTR_TTA | HCPTR_TCP(10) | HCPTR_TCP(11))
163
164#ifdef CONFIG_VFPv3
165 @ Save floating point registers we if let guest use them.
166 tst r2, #(HCPTR_TCP(10) | HCPTR_TCP(11))
167 bne after_vfp_restore
168
169 @ Switch VFP/NEON hardware state to the host's
170 add r7, vcpu, #VCPU_VFP_GUEST
171 store_vfp_state r7
172 add r7, vcpu, #VCPU_VFP_HOST
173 ldr r7, [r7]
174 restore_vfp_state r7
175
176after_vfp_restore:
177 @ Restore FPEXC_EN which we clobbered on entry
178 pop {r2}
179 VFPFMXR FPEXC, r2
180#endif
181
182 @ Reset Hyp-role
183 configure_hyp_role vmexit
184
185 @ Let host read hardware MIDR
186 mrc p15, 0, r2, c0, c0, 0
187 mcr p15, 4, r2, c0, c0, 0
188
189 @ Back to hardware MPIDR
190 mrc p15, 0, r2, c0, c0, 5
191 mcr p15, 4, r2, c0, c0, 5
192
193 @ Store guest CP15 state and restore host state
194 read_cp15_state store_to_vcpu = 1
195 write_cp15_state read_from_vcpu = 0
196
Marc Zyngier53e72402013-01-23 13:21:58 -0500197 save_timer_state
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500198 save_vgic_state
199
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500200 restore_host_regs
201 clrex @ Clear exclusive monitor
202 mov r0, r1 @ Return the return code
203 mov r1, #0 @ Clear upper bits in return value
204 bx lr @ return to IOCTL
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500205
206/********************************************************************
207 * Call function in Hyp mode
208 *
209 *
210 * u64 kvm_call_hyp(void *hypfn, ...);
211 *
212 * This is not really a variadic function in the classic C-way and care must
213 * be taken when calling this to ensure parameters are passed in registers
214 * only, since the stack will change between the caller and the callee.
215 *
216 * Call the function with the first argument containing a pointer to the
217 * function you wish to call in Hyp mode, and subsequent arguments will be
218 * passed as r0, r1, and r2 (a maximum of 3 arguments in addition to the
219 * function pointer can be passed). The function being called must be mapped
220 * in Hyp mode (see init_hyp_mode in arch/arm/kvm/arm.c). Return values are
221 * passed in r0 and r1.
222 *
223 * The calling convention follows the standard AAPCS:
224 * r0 - r3: caller save
225 * r12: caller save
226 * rest: callee save
227 */
228ENTRY(kvm_call_hyp)
229 hvc #0
230 bx lr
231
232/********************************************************************
233 * Hypervisor exception vector and handlers
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500234 *
235 *
236 * The KVM/ARM Hypervisor ABI is defined as follows:
237 *
238 * Entry to Hyp mode from the host kernel will happen _only_ when an HVC
239 * instruction is issued since all traps are disabled when running the host
240 * kernel as per the Hyp-mode initialization at boot time.
241 *
Jonghwan Choi0b5e3ba2013-02-19 15:19:32 +0900242 * HVC instructions cause a trap to the vector page + offset 0x14 (see hyp_hvc
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500243 * below) when the HVC instruction is called from SVC mode (i.e. a guest or the
Jonghwan Choi0b5e3ba2013-02-19 15:19:32 +0900244 * host kernel) and they cause a trap to the vector page + offset 0x8 when HVC
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500245 * instructions are called from within Hyp-mode.
246 *
247 * Hyp-ABI: Calling HYP-mode functions from host (in SVC mode):
248 * Switching to Hyp mode is done through a simple HVC #0 instruction. The
249 * exception vector code will check that the HVC comes from VMID==0 and if
250 * so will push the necessary state (SPSR, lr_usr) on the Hyp stack.
251 * - r0 contains a pointer to a HYP function
252 * - r1, r2, and r3 contain arguments to the above function.
253 * - The HYP function will be called with its arguments in r0, r1 and r2.
254 * On HYP function return, we return directly to SVC.
255 *
256 * Note that the above is used to execute code in Hyp-mode from a host-kernel
257 * point of view, and is a different concept from performing a world-switch and
258 * executing guest code SVC mode (with a VMID != 0).
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500259 */
260
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500261/* Handle undef, svc, pabt, or dabt by crashing with a user notice */
262.macro bad_exception exception_code, panic_str
263 push {r0-r2}
264 mrrc p15, 6, r0, r1, c2 @ Read VTTBR
265 lsr r1, r1, #16
266 ands r1, r1, #0xff
267 beq 99f
268
269 load_vcpu @ Load VCPU pointer
270 .if \exception_code == ARM_EXCEPTION_DATA_ABORT
271 mrc p15, 4, r2, c5, c2, 0 @ HSR
272 mrc p15, 4, r1, c6, c0, 0 @ HDFAR
273 str r2, [vcpu, #VCPU_HSR]
274 str r1, [vcpu, #VCPU_HxFAR]
275 .endif
276 .if \exception_code == ARM_EXCEPTION_PREF_ABORT
277 mrc p15, 4, r2, c5, c2, 0 @ HSR
278 mrc p15, 4, r1, c6, c0, 2 @ HIFAR
279 str r2, [vcpu, #VCPU_HSR]
280 str r1, [vcpu, #VCPU_HxFAR]
281 .endif
282 mov r1, #\exception_code
283 b __kvm_vcpu_return
284
285 @ We were in the host already. Let's craft a panic-ing return to SVC.
28699: mrs r2, cpsr
287 bic r2, r2, #MODE_MASK
288 orr r2, r2, #SVC_MODE
289THUMB( orr r2, r2, #PSR_T_BIT )
290 msr spsr_cxsf, r2
291 mrs r1, ELR_hyp
292 ldr r2, =BSYM(panic)
293 msr ELR_hyp, r2
294 ldr r0, =\panic_str
295 eret
296.endm
297
298 .text
299
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500300 .align 5
301__kvm_hyp_vector:
302 .globl __kvm_hyp_vector
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500303
304 @ Hyp-mode exception vector
305 W(b) hyp_reset
306 W(b) hyp_undef
307 W(b) hyp_svc
308 W(b) hyp_pabt
309 W(b) hyp_dabt
310 W(b) hyp_hvc
311 W(b) hyp_irq
312 W(b) hyp_fiq
313
314 .align
315hyp_reset:
316 b hyp_reset
317
318 .align
319hyp_undef:
320 bad_exception ARM_EXCEPTION_UNDEFINED, und_die_str
321
322 .align
323hyp_svc:
324 bad_exception ARM_EXCEPTION_HVC, svc_die_str
325
326 .align
327hyp_pabt:
328 bad_exception ARM_EXCEPTION_PREF_ABORT, pabt_die_str
329
330 .align
331hyp_dabt:
332 bad_exception ARM_EXCEPTION_DATA_ABORT, dabt_die_str
333
334 .align
335hyp_hvc:
336 /*
337 * Getting here is either becuase of a trap from a guest or from calling
338 * HVC from the host kernel, which means "switch to Hyp mode".
339 */
340 push {r0, r1, r2}
341
342 @ Check syndrome register
343 mrc p15, 4, r1, c5, c2, 0 @ HSR
344 lsr r0, r1, #HSR_EC_SHIFT
345#ifdef CONFIG_VFPv3
346 cmp r0, #HSR_EC_CP_0_13
347 beq switch_to_guest_vfp
348#endif
349 cmp r0, #HSR_EC_HVC
350 bne guest_trap @ Not HVC instr.
351
352 /*
353 * Let's check if the HVC came from VMID 0 and allow simple
354 * switch to Hyp mode
355 */
356 mrrc p15, 6, r0, r2, c2
357 lsr r2, r2, #16
358 and r2, r2, #0xff
359 cmp r2, #0
360 bne guest_trap @ Guest called HVC
361
362host_switch_to_hyp:
363 pop {r0, r1, r2}
364
365 push {lr}
366 mrs lr, SPSR
367 push {lr}
368
369 mov lr, r0
370 mov r0, r1
371 mov r1, r2
372 mov r2, r3
373
374THUMB( orr lr, #1)
375 blx lr @ Call the HYP function
376
377 pop {lr}
378 msr SPSR_csxf, lr
379 pop {lr}
380 eret
381
382guest_trap:
383 load_vcpu @ Load VCPU pointer to r0
384 str r1, [vcpu, #VCPU_HSR]
385
386 @ Check if we need the fault information
387 lsr r1, r1, #HSR_EC_SHIFT
388 cmp r1, #HSR_EC_IABT
389 mrceq p15, 4, r2, c6, c0, 2 @ HIFAR
390 beq 2f
391 cmp r1, #HSR_EC_DABT
392 bne 1f
393 mrc p15, 4, r2, c6, c0, 0 @ HDFAR
394
3952: str r2, [vcpu, #VCPU_HxFAR]
396
397 /*
398 * B3.13.5 Reporting exceptions taken to the Non-secure PL2 mode:
399 *
400 * Abort on the stage 2 translation for a memory access from a
401 * Non-secure PL1 or PL0 mode:
402 *
403 * For any Access flag fault or Translation fault, and also for any
404 * Permission fault on the stage 2 translation of a memory access
405 * made as part of a translation table walk for a stage 1 translation,
406 * the HPFAR holds the IPA that caused the fault. Otherwise, the HPFAR
407 * is UNKNOWN.
408 */
409
410 /* Check for permission fault, and S1PTW */
411 mrc p15, 4, r1, c5, c2, 0 @ HSR
412 and r0, r1, #HSR_FSC_TYPE
413 cmp r0, #FSC_PERM
414 tsteq r1, #(1 << 7) @ S1PTW
415 mrcne p15, 4, r2, c6, c0, 4 @ HPFAR
416 bne 3f
417
Marc Zyngier6a077e42013-06-21 13:08:46 +0100418 /* Preserve PAR */
419 mrrc p15, 0, r0, r1, c7 @ PAR
420 push {r0, r1}
421
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500422 /* Resolve IPA using the xFAR */
423 mcr p15, 0, r2, c7, c8, 0 @ ATS1CPR
424 isb
425 mrrc p15, 0, r0, r1, c7 @ PAR
426 tst r0, #1
427 bne 4f @ Failed translation
428 ubfx r2, r0, #12, #20
429 lsl r2, r2, #4
430 orr r2, r2, r1, lsl #24
431
Marc Zyngier6a077e42013-06-21 13:08:46 +0100432 /* Restore PAR */
433 pop {r0, r1}
434 mcrr p15, 0, r0, r1, c7 @ PAR
435
Christoffer Dallf7ed45b2013-01-20 18:47:42 -05004363: load_vcpu @ Load VCPU pointer to r0
437 str r2, [r0, #VCPU_HPFAR]
438
4391: mov r1, #ARM_EXCEPTION_HVC
440 b __kvm_vcpu_return
441
Marc Zyngier6a077e42013-06-21 13:08:46 +01004424: pop {r0, r1} @ Failed translation, return to guest
443 mcrr p15, 0, r0, r1, c7 @ PAR
444 pop {r0, r1, r2}
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500445 eret
446
447/*
448 * If VFPv3 support is not available, then we will not switch the VFP
449 * registers; however cp10 and cp11 accesses will still trap and fallback
450 * to the regular coprocessor emulation code, which currently will
451 * inject an undefined exception to the guest.
452 */
453#ifdef CONFIG_VFPv3
454switch_to_guest_vfp:
455 load_vcpu @ Load VCPU pointer to r0
456 push {r3-r7}
457
458 @ NEON/VFP used. Turn on VFP access.
459 set_hcptr vmexit, (HCPTR_TCP(10) | HCPTR_TCP(11))
460
461 @ Switch VFP/NEON hardware state to the guest's
462 add r7, r0, #VCPU_VFP_HOST
463 ldr r7, [r7]
464 store_vfp_state r7
465 add r7, r0, #VCPU_VFP_GUEST
466 restore_vfp_state r7
467
468 pop {r3-r7}
469 pop {r0-r2}
470 eret
471#endif
472
473 .align
474hyp_irq:
475 push {r0, r1, r2}
476 mov r1, #ARM_EXCEPTION_IRQ
477 load_vcpu @ Load VCPU pointer to r0
478 b __kvm_vcpu_return
479
480 .align
481hyp_fiq:
482 b hyp_fiq
483
484 .ltorg
Christoffer Dall342cd0a2013-01-20 18:28:06 -0500485
486__kvm_hyp_code_end:
487 .globl __kvm_hyp_code_end
Christoffer Dallf7ed45b2013-01-20 18:47:42 -0500488
489 .section ".rodata"
490
491und_die_str:
492 .ascii "unexpected undefined exception in Hyp mode at: %#08x"
493pabt_die_str:
494 .ascii "unexpected prefetch abort in Hyp mode at: %#08x"
495dabt_die_str:
496 .ascii "unexpected data abort in Hyp mode at: %#08x"
497svc_die_str:
498 .ascii "unexpected HVC/SVC trap in Hyp mode at: %#08x"