Marc Zyngier | f467275 | 2012-12-10 16:23:59 +0000 | [diff] [blame] | 1 | /* |
| 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> |
| 25 | |
Marc Zyngier | 003300d | 2012-12-07 17:52:03 +0000 | [diff] [blame] | 26 | #include <kvm/arm_arch_timer.h> |
| 27 | |
Marc Zyngier | f467275 | 2012-12-10 16:23:59 +0000 | [diff] [blame] | 28 | #include <asm/cputype.h> |
| 29 | #include <asm/ptrace.h> |
| 30 | #include <asm/kvm_arm.h> |
AKASHI Takahiro | a7611ce | 2016-04-27 17:47:05 +0100 | [diff] [blame^] | 31 | #include <asm/kvm_asm.h> |
Marc Zyngier | f467275 | 2012-12-10 16:23:59 +0000 | [diff] [blame] | 32 | #include <asm/kvm_coproc.h> |
AKASHI Takahiro | a7611ce | 2016-04-27 17:47:05 +0100 | [diff] [blame^] | 33 | #include <asm/kvm_mmu.h> |
Marc Zyngier | f467275 | 2012-12-10 16:23:59 +0000 | [diff] [blame] | 34 | |
| 35 | /* |
| 36 | * ARMv8 Reset Values |
| 37 | */ |
| 38 | static const struct kvm_regs default_regs_reset = { |
| 39 | .regs.pstate = (PSR_MODE_EL1h | PSR_A_BIT | PSR_I_BIT | |
| 40 | PSR_F_BIT | PSR_D_BIT), |
| 41 | }; |
| 42 | |
Marc Zyngier | 0d854a6 | 2013-02-07 10:46:46 +0000 | [diff] [blame] | 43 | static const struct kvm_regs default_regs_reset32 = { |
| 44 | .regs.pstate = (COMPAT_PSR_MODE_SVC | COMPAT_PSR_A_BIT | |
| 45 | COMPAT_PSR_I_BIT | COMPAT_PSR_F_BIT), |
| 46 | }; |
| 47 | |
Marc Zyngier | 003300d | 2012-12-07 17:52:03 +0000 | [diff] [blame] | 48 | static const struct kvm_irq_level default_vtimer_irq = { |
| 49 | .irq = 27, |
| 50 | .level = 1, |
| 51 | }; |
| 52 | |
Marc Zyngier | 0d854a6 | 2013-02-07 10:46:46 +0000 | [diff] [blame] | 53 | static bool cpu_has_32bit_el1(void) |
| 54 | { |
| 55 | u64 pfr0; |
| 56 | |
| 57 | pfr0 = read_cpuid(ID_AA64PFR0_EL1); |
| 58 | return !!(pfr0 & 0x20); |
| 59 | } |
| 60 | |
Marc Zyngier | f467275 | 2012-12-10 16:23:59 +0000 | [diff] [blame] | 61 | int kvm_arch_dev_ioctl_check_extension(long ext) |
| 62 | { |
| 63 | int r; |
| 64 | |
| 65 | switch (ext) { |
Marc Zyngier | 0d854a6 | 2013-02-07 10:46:46 +0000 | [diff] [blame] | 66 | case KVM_CAP_ARM_EL1_32BIT: |
| 67 | r = cpu_has_32bit_el1(); |
| 68 | break; |
Marc Zyngier | f467275 | 2012-12-10 16:23:59 +0000 | [diff] [blame] | 69 | default: |
| 70 | r = 0; |
| 71 | } |
| 72 | |
| 73 | return r; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * kvm_reset_vcpu - sets core registers and sys_regs to reset value |
| 78 | * @vcpu: The VCPU pointer |
| 79 | * |
| 80 | * This function finds the right table above and sets the registers on |
| 81 | * the virtual CPU struct to their architectually defined reset |
| 82 | * values. |
| 83 | */ |
| 84 | int kvm_reset_vcpu(struct kvm_vcpu *vcpu) |
| 85 | { |
Marc Zyngier | 003300d | 2012-12-07 17:52:03 +0000 | [diff] [blame] | 86 | const struct kvm_irq_level *cpu_vtimer_irq; |
Marc Zyngier | f467275 | 2012-12-10 16:23:59 +0000 | [diff] [blame] | 87 | const struct kvm_regs *cpu_reset; |
| 88 | |
| 89 | switch (vcpu->arch.target) { |
| 90 | default: |
Marc Zyngier | 0d854a6 | 2013-02-07 10:46:46 +0000 | [diff] [blame] | 91 | if (test_bit(KVM_ARM_VCPU_EL1_32BIT, vcpu->arch.features)) { |
| 92 | if (!cpu_has_32bit_el1()) |
| 93 | return -EINVAL; |
| 94 | cpu_reset = &default_regs_reset32; |
Marc Zyngier | 0d854a6 | 2013-02-07 10:46:46 +0000 | [diff] [blame] | 95 | } else { |
| 96 | cpu_reset = &default_regs_reset; |
| 97 | } |
| 98 | |
Marc Zyngier | 003300d | 2012-12-07 17:52:03 +0000 | [diff] [blame] | 99 | cpu_vtimer_irq = &default_vtimer_irq; |
Marc Zyngier | f467275 | 2012-12-10 16:23:59 +0000 | [diff] [blame] | 100 | break; |
| 101 | } |
| 102 | |
| 103 | /* Reset core registers */ |
| 104 | memcpy(vcpu_gp_regs(vcpu), cpu_reset, sizeof(*cpu_reset)); |
| 105 | |
| 106 | /* Reset system registers */ |
| 107 | kvm_reset_sys_regs(vcpu); |
| 108 | |
Marc Zyngier | 003300d | 2012-12-07 17:52:03 +0000 | [diff] [blame] | 109 | /* Reset timer */ |
| 110 | kvm_timer_vcpu_reset(vcpu, cpu_vtimer_irq); |
| 111 | |
Marc Zyngier | f467275 | 2012-12-10 16:23:59 +0000 | [diff] [blame] | 112 | return 0; |
| 113 | } |
AKASHI Takahiro | a7611ce | 2016-04-27 17:47:05 +0100 | [diff] [blame^] | 114 | |
| 115 | extern char __hyp_idmap_text_start[]; |
| 116 | |
| 117 | phys_addr_t kvm_hyp_reset_entry(void) |
| 118 | { |
| 119 | unsigned long offset; |
| 120 | |
| 121 | offset = (unsigned long)__kvm_hyp_reset |
| 122 | - ((unsigned long)__hyp_idmap_text_start & PAGE_MASK); |
| 123 | |
| 124 | return TRAMPOLINE_VA + offset; |
| 125 | } |