diff options
author | Marc Zyngier <marc.zyngier@arm.com> | 2012-12-10 11:37:02 -0500 |
---|---|---|
committer | Marc Zyngier <marc.zyngier@arm.com> | 2013-06-07 09:03:39 -0400 |
commit | 2f4a07c5f9fe4a5cdb9867e1e2fcab3165846ea7 (patch) | |
tree | 35140de8ff75de10c9d5b153245727c8fcb5de8e /arch | |
parent | d7246bf3571a82834984a42db52261525bc11159 (diff) |
arm64: KVM: guest one-reg interface
Let userspace play with the guest registers.
Reviewed-by: Christopher Covington <cov@codeaurora.org>
Signed-off-by: Marc Zyngier <marc.zyngier@arm.com>
Diffstat (limited to 'arch')
-rw-r--r-- | arch/arm64/kvm/guest.c | 259 |
1 files changed, 259 insertions, 0 deletions
diff --git a/arch/arm64/kvm/guest.c b/arch/arm64/kvm/guest.c new file mode 100644 index 000000000000..3d7518a7ebaa --- /dev/null +++ b/arch/arm64/kvm/guest.c | |||
@@ -0,0 +1,259 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2012,2013 - ARM Ltd | ||
3 | * Author: Marc Zyngier <marc.zyngier@arm.com> | ||
4 | * | ||
5 | * Derived from arch/arm/kvm/guest.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/err.h> | ||
24 | #include <linux/kvm_host.h> | ||
25 | #include <linux/module.h> | ||
26 | #include <linux/vmalloc.h> | ||
27 | #include <linux/fs.h> | ||
28 | #include <asm/cputype.h> | ||
29 | #include <asm/uaccess.h> | ||
30 | #include <asm/kvm.h> | ||
31 | #include <asm/kvm_asm.h> | ||
32 | #include <asm/kvm_emulate.h> | ||
33 | #include <asm/kvm_coproc.h> | ||
34 | |||
35 | struct kvm_stats_debugfs_item debugfs_entries[] = { | ||
36 | { NULL } | ||
37 | }; | ||
38 | |||
39 | int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu) | ||
40 | { | ||
41 | vcpu->arch.hcr_el2 = HCR_GUEST_FLAGS; | ||
42 | return 0; | ||
43 | } | ||
44 | |||
45 | static u64 core_reg_offset_from_id(u64 id) | ||
46 | { | ||
47 | return id & ~(KVM_REG_ARCH_MASK | KVM_REG_SIZE_MASK | KVM_REG_ARM_CORE); | ||
48 | } | ||
49 | |||
50 | static int get_core_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg) | ||
51 | { | ||
52 | /* | ||
53 | * Because the kvm_regs structure is a mix of 32, 64 and | ||
54 | * 128bit fields, we index it as if it was a 32bit | ||
55 | * array. Hence below, nr_regs is the number of entries, and | ||
56 | * off the index in the "array". | ||
57 | */ | ||
58 | __u32 __user *uaddr = (__u32 __user *)(unsigned long)reg->addr; | ||
59 | struct kvm_regs *regs = vcpu_gp_regs(vcpu); | ||
60 | int nr_regs = sizeof(*regs) / sizeof(__u32); | ||
61 | u32 off; | ||
62 | |||
63 | /* Our ID is an index into the kvm_regs struct. */ | ||
64 | off = core_reg_offset_from_id(reg->id); | ||
65 | if (off >= nr_regs || | ||
66 | (off + (KVM_REG_SIZE(reg->id) / sizeof(__u32))) >= nr_regs) | ||
67 | return -ENOENT; | ||
68 | |||
69 | if (copy_to_user(uaddr, ((u32 *)regs) + off, KVM_REG_SIZE(reg->id))) | ||
70 | return -EFAULT; | ||
71 | |||
72 | return 0; | ||
73 | } | ||
74 | |||
75 | static int set_core_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg) | ||
76 | { | ||
77 | __u32 __user *uaddr = (__u32 __user *)(unsigned long)reg->addr; | ||
78 | struct kvm_regs *regs = vcpu_gp_regs(vcpu); | ||
79 | int nr_regs = sizeof(*regs) / sizeof(__u32); | ||
80 | __uint128_t tmp; | ||
81 | void *valp = &tmp; | ||
82 | u64 off; | ||
83 | int err = 0; | ||
84 | |||
85 | /* Our ID is an index into the kvm_regs struct. */ | ||
86 | off = core_reg_offset_from_id(reg->id); | ||
87 | if (off >= nr_regs || | ||
88 | (off + (KVM_REG_SIZE(reg->id) / sizeof(__u32))) >= nr_regs) | ||
89 | return -ENOENT; | ||
90 | |||
91 | if (KVM_REG_SIZE(reg->id) > sizeof(tmp)) | ||
92 | return -EINVAL; | ||
93 | |||
94 | if (copy_from_user(valp, uaddr, KVM_REG_SIZE(reg->id))) { | ||
95 | err = -EFAULT; | ||
96 | goto out; | ||
97 | } | ||
98 | |||
99 | if (off == KVM_REG_ARM_CORE_REG(regs.pstate)) { | ||
100 | u32 mode = (*(u32 *)valp) & COMPAT_PSR_MODE_MASK; | ||
101 | switch (mode) { | ||
102 | case PSR_MODE_EL0t: | ||
103 | case PSR_MODE_EL1t: | ||
104 | case PSR_MODE_EL1h: | ||
105 | break; | ||
106 | default: | ||
107 | err = -EINVAL; | ||
108 | goto out; | ||
109 | } | ||
110 | } | ||
111 | |||
112 | memcpy((u32 *)regs + off, valp, KVM_REG_SIZE(reg->id)); | ||
113 | out: | ||
114 | return err; | ||
115 | } | ||
116 | |||
117 | int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) | ||
118 | { | ||
119 | return -EINVAL; | ||
120 | } | ||
121 | |||
122 | int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) | ||
123 | { | ||
124 | return -EINVAL; | ||
125 | } | ||
126 | |||
127 | static unsigned long num_core_regs(void) | ||
128 | { | ||
129 | return sizeof(struct kvm_regs) / sizeof(__u32); | ||
130 | } | ||
131 | |||
132 | /** | ||
133 | * kvm_arm_num_regs - how many registers do we present via KVM_GET_ONE_REG | ||
134 | * | ||
135 | * This is for all registers. | ||
136 | */ | ||
137 | unsigned long kvm_arm_num_regs(struct kvm_vcpu *vcpu) | ||
138 | { | ||
139 | return num_core_regs() + kvm_arm_num_sys_reg_descs(vcpu); | ||
140 | } | ||
141 | |||
142 | /** | ||
143 | * kvm_arm_copy_reg_indices - get indices of all registers. | ||
144 | * | ||
145 | * We do core registers right here, then we apppend system regs. | ||
146 | */ | ||
147 | int kvm_arm_copy_reg_indices(struct kvm_vcpu *vcpu, u64 __user *uindices) | ||
148 | { | ||
149 | unsigned int i; | ||
150 | const u64 core_reg = KVM_REG_ARM64 | KVM_REG_SIZE_U64 | KVM_REG_ARM_CORE; | ||
151 | |||
152 | for (i = 0; i < sizeof(struct kvm_regs) / sizeof(__u32); i++) { | ||
153 | if (put_user(core_reg | i, uindices)) | ||
154 | return -EFAULT; | ||
155 | uindices++; | ||
156 | } | ||
157 | |||
158 | return kvm_arm_copy_sys_reg_indices(vcpu, uindices); | ||
159 | } | ||
160 | |||
161 | int kvm_arm_get_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg) | ||
162 | { | ||
163 | /* We currently use nothing arch-specific in upper 32 bits */ | ||
164 | if ((reg->id & ~KVM_REG_SIZE_MASK) >> 32 != KVM_REG_ARM64 >> 32) | ||
165 | return -EINVAL; | ||
166 | |||
167 | /* Register group 16 means we want a core register. */ | ||
168 | if ((reg->id & KVM_REG_ARM_COPROC_MASK) == KVM_REG_ARM_CORE) | ||
169 | return get_core_reg(vcpu, reg); | ||
170 | |||
171 | return kvm_arm_sys_reg_get_reg(vcpu, reg); | ||
172 | } | ||
173 | |||
174 | int kvm_arm_set_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg) | ||
175 | { | ||
176 | /* We currently use nothing arch-specific in upper 32 bits */ | ||
177 | if ((reg->id & ~KVM_REG_SIZE_MASK) >> 32 != KVM_REG_ARM64 >> 32) | ||
178 | return -EINVAL; | ||
179 | |||
180 | /* Register group 16 means we set a core register. */ | ||
181 | if ((reg->id & KVM_REG_ARM_COPROC_MASK) == KVM_REG_ARM_CORE) | ||
182 | return set_core_reg(vcpu, reg); | ||
183 | |||
184 | return kvm_arm_sys_reg_set_reg(vcpu, reg); | ||
185 | } | ||
186 | |||
187 | int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu, | ||
188 | struct kvm_sregs *sregs) | ||
189 | { | ||
190 | return -EINVAL; | ||
191 | } | ||
192 | |||
193 | int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu, | ||
194 | struct kvm_sregs *sregs) | ||
195 | { | ||
196 | return -EINVAL; | ||
197 | } | ||
198 | |||
199 | int __attribute_const__ kvm_target_cpu(void) | ||
200 | { | ||
201 | unsigned long implementor = read_cpuid_implementor(); | ||
202 | unsigned long part_number = read_cpuid_part_number(); | ||
203 | |||
204 | if (implementor != ARM_CPU_IMP_ARM) | ||
205 | return -EINVAL; | ||
206 | |||
207 | switch (part_number) { | ||
208 | case ARM_CPU_PART_AEM_V8: | ||
209 | return KVM_ARM_TARGET_AEM_V8; | ||
210 | case ARM_CPU_PART_FOUNDATION: | ||
211 | return KVM_ARM_TARGET_FOUNDATION_V8; | ||
212 | case ARM_CPU_PART_CORTEX_A57: | ||
213 | /* Currently handled by the generic backend */ | ||
214 | return KVM_ARM_TARGET_CORTEX_A57; | ||
215 | default: | ||
216 | return -EINVAL; | ||
217 | } | ||
218 | } | ||
219 | |||
220 | int kvm_vcpu_set_target(struct kvm_vcpu *vcpu, | ||
221 | const struct kvm_vcpu_init *init) | ||
222 | { | ||
223 | unsigned int i; | ||
224 | int phys_target = kvm_target_cpu(); | ||
225 | |||
226 | if (init->target != phys_target) | ||
227 | return -EINVAL; | ||
228 | |||
229 | vcpu->arch.target = phys_target; | ||
230 | bitmap_zero(vcpu->arch.features, KVM_VCPU_MAX_FEATURES); | ||
231 | |||
232 | /* -ENOENT for unknown features, -EINVAL for invalid combinations. */ | ||
233 | for (i = 0; i < sizeof(init->features) * 8; i++) { | ||
234 | if (init->features[i / 32] & (1 << (i % 32))) { | ||
235 | if (i >= KVM_VCPU_MAX_FEATURES) | ||
236 | return -ENOENT; | ||
237 | set_bit(i, vcpu->arch.features); | ||
238 | } | ||
239 | } | ||
240 | |||
241 | /* Now we know what it is, we can reset it. */ | ||
242 | return kvm_reset_vcpu(vcpu); | ||
243 | } | ||
244 | |||
245 | int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) | ||
246 | { | ||
247 | return -EINVAL; | ||
248 | } | ||
249 | |||
250 | int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) | ||
251 | { | ||
252 | return -EINVAL; | ||
253 | } | ||
254 | |||
255 | int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu, | ||
256 | struct kvm_translation *tr) | ||
257 | { | ||
258 | return -EINVAL; | ||
259 | } | ||