blob: 7be24f2b18dbd788058a2a0a34467f14e8145d80 [file] [log] [blame]
Marc Zyngierf4672752012-12-10 16:23:59 +00001/*
2 * Copyright (C) 2012,2013 - ARM Ltd
3 * Author: Marc Zyngier <marc.zyngier@arm.com>
4 *
5 * Derived from arch/arm/kvm/reset.c
6 * Copyright (C) 2012 - Virtual Open Systems and Columbia University
7 * Author: Christoffer Dall <c.dall@virtualopensystems.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License, version 2, as
11 * published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#include <linux/errno.h>
23#include <linux/kvm_host.h>
24#include <linux/kvm.h>
Alex Bennée834bf882015-07-07 17:30:02 +010025#include <linux/hw_breakpoint.h>
Marc Zyngierf4672752012-12-10 16:23:59 +000026
Marc Zyngier003300d2012-12-07 17:52:03 +000027#include <kvm/arm_arch_timer.h>
28
Marc Zyngierf4672752012-12-10 16:23:59 +000029#include <asm/cputype.h>
30#include <asm/ptrace.h>
31#include <asm/kvm_arm.h>
AKASHI Takahiro67f69192016-04-27 17:47:05 +010032#include <asm/kvm_asm.h>
Marc Zyngierf4672752012-12-10 16:23:59 +000033#include <asm/kvm_coproc.h>
AKASHI Takahiro67f69192016-04-27 17:47:05 +010034#include <asm/kvm_mmu.h>
Marc Zyngierf4672752012-12-10 16:23:59 +000035
36/*
37 * ARMv8 Reset Values
38 */
39static const struct kvm_regs default_regs_reset = {
40 .regs.pstate = (PSR_MODE_EL1h | PSR_A_BIT | PSR_I_BIT |
41 PSR_F_BIT | PSR_D_BIT),
42};
43
Marc Zyngier0d854a62013-02-07 10:46:46 +000044static const struct kvm_regs default_regs_reset32 = {
45 .regs.pstate = (COMPAT_PSR_MODE_SVC | COMPAT_PSR_A_BIT |
46 COMPAT_PSR_I_BIT | COMPAT_PSR_F_BIT),
47};
48
Marc Zyngier003300d2012-12-07 17:52:03 +000049static const struct kvm_irq_level default_vtimer_irq = {
50 .irq = 27,
51 .level = 1,
52};
53
Marc Zyngier0d854a62013-02-07 10:46:46 +000054static bool cpu_has_32bit_el1(void)
55{
56 u64 pfr0;
57
Suzuki K. Poulose4db8e5e2015-10-19 14:24:55 +010058 pfr0 = read_system_reg(SYS_ID_AA64PFR0_EL1);
Marc Zyngier0d854a62013-02-07 10:46:46 +000059 return !!(pfr0 & 0x20);
60}
61
Alex Bennée834bf882015-07-07 17:30:02 +010062/**
63 * kvm_arch_dev_ioctl_check_extension
64 *
65 * We currently assume that the number of HW registers is uniform
66 * across all CPUs (see cpuinfo_sanity_check).
67 */
Marc Zyngierf4672752012-12-10 16:23:59 +000068int kvm_arch_dev_ioctl_check_extension(long ext)
69{
70 int r;
71
72 switch (ext) {
Marc Zyngier0d854a62013-02-07 10:46:46 +000073 case KVM_CAP_ARM_EL1_32BIT:
74 r = cpu_has_32bit_el1();
75 break;
Alex Bennée834bf882015-07-07 17:30:02 +010076 case KVM_CAP_GUEST_DEBUG_HW_BPS:
77 r = get_num_brps();
78 break;
79 case KVM_CAP_GUEST_DEBUG_HW_WPS:
80 r = get_num_wrps();
81 break;
Shannon Zhao808e7382016-01-11 22:46:15 +080082 case KVM_CAP_ARM_PMU_V3:
83 r = kvm_arm_support_pmu_v3();
84 break;
Alex Bennée834bf882015-07-07 17:30:02 +010085 case KVM_CAP_SET_GUEST_DEBUG:
Shannon Zhaof577f6c2016-01-11 20:56:17 +080086 case KVM_CAP_VCPU_ATTRIBUTES:
Alex Bennée834bf882015-07-07 17:30:02 +010087 r = 1;
88 break;
Marc Zyngierf4672752012-12-10 16:23:59 +000089 default:
90 r = 0;
91 }
92
93 return r;
94}
95
96/**
97 * kvm_reset_vcpu - sets core registers and sys_regs to reset value
98 * @vcpu: The VCPU pointer
99 *
100 * This function finds the right table above and sets the registers on
Andrea Gelminiedce2292016-05-21 13:53:14 +0200101 * the virtual CPU struct to their architecturally defined reset
Marc Zyngierf4672752012-12-10 16:23:59 +0000102 * values.
103 */
104int kvm_reset_vcpu(struct kvm_vcpu *vcpu)
105{
Marc Zyngier003300d2012-12-07 17:52:03 +0000106 const struct kvm_irq_level *cpu_vtimer_irq;
Marc Zyngierf4672752012-12-10 16:23:59 +0000107 const struct kvm_regs *cpu_reset;
108
109 switch (vcpu->arch.target) {
110 default:
Marc Zyngier0d854a62013-02-07 10:46:46 +0000111 if (test_bit(KVM_ARM_VCPU_EL1_32BIT, vcpu->arch.features)) {
112 if (!cpu_has_32bit_el1())
113 return -EINVAL;
114 cpu_reset = &default_regs_reset32;
Marc Zyngier0d854a62013-02-07 10:46:46 +0000115 } else {
116 cpu_reset = &default_regs_reset;
117 }
118
Marc Zyngier003300d2012-12-07 17:52:03 +0000119 cpu_vtimer_irq = &default_vtimer_irq;
Marc Zyngierf4672752012-12-10 16:23:59 +0000120 break;
121 }
122
123 /* Reset core registers */
124 memcpy(vcpu_gp_regs(vcpu), cpu_reset, sizeof(*cpu_reset));
125
126 /* Reset system registers */
127 kvm_reset_sys_regs(vcpu);
128
Shannon Zhao2aa36e92015-09-11 11:30:22 +0800129 /* Reset PMU */
130 kvm_pmu_vcpu_reset(vcpu);
131
Marc Zyngier003300d2012-12-07 17:52:03 +0000132 /* Reset timer */
Marc Zyngierf120cd62014-06-23 13:59:13 +0100133 return kvm_timer_vcpu_reset(vcpu, cpu_vtimer_irq);
Marc Zyngierf4672752012-12-10 16:23:59 +0000134}
AKASHI Takahiro67f69192016-04-27 17:47:05 +0100135
136extern char __hyp_idmap_text_start[];
137
James Morsec6125052016-04-29 18:27:03 +0100138unsigned long kvm_hyp_reset_entry(void)
AKASHI Takahiro67f69192016-04-27 17:47:05 +0100139{
James Morsec6125052016-04-29 18:27:03 +0100140 if (!__kvm_cpu_uses_extended_idmap()) {
141 unsigned long offset;
AKASHI Takahiro67f69192016-04-27 17:47:05 +0100142
James Morsec6125052016-04-29 18:27:03 +0100143 /*
144 * Find the address of __kvm_hyp_reset() in the trampoline page.
145 * This is present in the running page tables, and the boot page
146 * tables, so we call the code here to start the trampoline
147 * dance in reverse.
148 */
149 offset = (unsigned long)__kvm_hyp_reset
150 - ((unsigned long)__hyp_idmap_text_start & PAGE_MASK);
AKASHI Takahiro67f69192016-04-27 17:47:05 +0100151
James Morsec6125052016-04-29 18:27:03 +0100152 return TRAMPOLINE_VA + offset;
153 } else {
154 /*
155 * KVM is running with merged page tables, which don't have the
156 * trampoline page mapped. We know the idmap is still mapped,
157 * but can't be called into directly. Use
158 * __extended_idmap_trampoline to do the call.
159 */
160 return (unsigned long)kvm_ksym_ref(__extended_idmap_trampoline);
161 }
AKASHI Takahiro67f69192016-04-27 17:47:05 +0100162}