blk: rq_data_dir() should not return a boolean
[cris-mirror.git] / arch / arm64 / kvm / guest.c
blobd250160d32bc68ae636c804b9cdfe2499bdddcb9
1 /*
2 * Copyright (C) 2012,2013 - ARM Ltd
3 * Author: Marc Zyngier <marc.zyngier@arm.com>
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>
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.
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.
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/>.
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>
35 #include "trace.h"
37 struct kvm_stats_debugfs_item debugfs_entries[] = {
38 { NULL }
41 int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
43 return 0;
46 static u64 core_reg_offset_from_id(u64 id)
48 return id & ~(KVM_REG_ARCH_MASK | KVM_REG_SIZE_MASK | KVM_REG_ARM_CORE);
51 static int get_core_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
54 * Because the kvm_regs structure is a mix of 32, 64 and
55 * 128bit fields, we index it as if it was a 32bit
56 * array. Hence below, nr_regs is the number of entries, and
57 * off the index in the "array".
59 __u32 __user *uaddr = (__u32 __user *)(unsigned long)reg->addr;
60 struct kvm_regs *regs = vcpu_gp_regs(vcpu);
61 int nr_regs = sizeof(*regs) / sizeof(__u32);
62 u32 off;
64 /* Our ID is an index into the kvm_regs struct. */
65 off = core_reg_offset_from_id(reg->id);
66 if (off >= nr_regs ||
67 (off + (KVM_REG_SIZE(reg->id) / sizeof(__u32))) >= nr_regs)
68 return -ENOENT;
70 if (copy_to_user(uaddr, ((u32 *)regs) + off, KVM_REG_SIZE(reg->id)))
71 return -EFAULT;
73 return 0;
76 static int set_core_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
78 __u32 __user *uaddr = (__u32 __user *)(unsigned long)reg->addr;
79 struct kvm_regs *regs = vcpu_gp_regs(vcpu);
80 int nr_regs = sizeof(*regs) / sizeof(__u32);
81 __uint128_t tmp;
82 void *valp = &tmp;
83 u64 off;
84 int err = 0;
86 /* Our ID is an index into the kvm_regs struct. */
87 off = core_reg_offset_from_id(reg->id);
88 if (off >= nr_regs ||
89 (off + (KVM_REG_SIZE(reg->id) / sizeof(__u32))) >= nr_regs)
90 return -ENOENT;
92 if (KVM_REG_SIZE(reg->id) > sizeof(tmp))
93 return -EINVAL;
95 if (copy_from_user(valp, uaddr, KVM_REG_SIZE(reg->id))) {
96 err = -EFAULT;
97 goto out;
100 if (off == KVM_REG_ARM_CORE_REG(regs.pstate)) {
101 u32 mode = (*(u32 *)valp) & COMPAT_PSR_MODE_MASK;
102 switch (mode) {
103 case COMPAT_PSR_MODE_USR:
104 case COMPAT_PSR_MODE_FIQ:
105 case COMPAT_PSR_MODE_IRQ:
106 case COMPAT_PSR_MODE_SVC:
107 case COMPAT_PSR_MODE_ABT:
108 case COMPAT_PSR_MODE_UND:
109 case PSR_MODE_EL0t:
110 case PSR_MODE_EL1t:
111 case PSR_MODE_EL1h:
112 break;
113 default:
114 err = -EINVAL;
115 goto out;
119 memcpy((u32 *)regs + off, valp, KVM_REG_SIZE(reg->id));
120 out:
121 return err;
124 int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
126 return -EINVAL;
129 int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
131 return -EINVAL;
134 static unsigned long num_core_regs(void)
136 return sizeof(struct kvm_regs) / sizeof(__u32);
140 * ARM64 versions of the TIMER registers, always available on arm64
143 #define NUM_TIMER_REGS 3
145 static bool is_timer_reg(u64 index)
147 switch (index) {
148 case KVM_REG_ARM_TIMER_CTL:
149 case KVM_REG_ARM_TIMER_CNT:
150 case KVM_REG_ARM_TIMER_CVAL:
151 return true;
153 return false;
156 static int copy_timer_indices(struct kvm_vcpu *vcpu, u64 __user *uindices)
158 if (put_user(KVM_REG_ARM_TIMER_CTL, uindices))
159 return -EFAULT;
160 uindices++;
161 if (put_user(KVM_REG_ARM_TIMER_CNT, uindices))
162 return -EFAULT;
163 uindices++;
164 if (put_user(KVM_REG_ARM_TIMER_CVAL, uindices))
165 return -EFAULT;
167 return 0;
170 static int set_timer_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
172 void __user *uaddr = (void __user *)(long)reg->addr;
173 u64 val;
174 int ret;
176 ret = copy_from_user(&val, uaddr, KVM_REG_SIZE(reg->id));
177 if (ret != 0)
178 return -EFAULT;
180 return kvm_arm_timer_set_reg(vcpu, reg->id, val);
183 static int get_timer_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
185 void __user *uaddr = (void __user *)(long)reg->addr;
186 u64 val;
188 val = kvm_arm_timer_get_reg(vcpu, reg->id);
189 return copy_to_user(uaddr, &val, KVM_REG_SIZE(reg->id));
193 * kvm_arm_num_regs - how many registers do we present via KVM_GET_ONE_REG
195 * This is for all registers.
197 unsigned long kvm_arm_num_regs(struct kvm_vcpu *vcpu)
199 return num_core_regs() + kvm_arm_num_sys_reg_descs(vcpu)
200 + NUM_TIMER_REGS;
204 * kvm_arm_copy_reg_indices - get indices of all registers.
206 * We do core registers right here, then we apppend system regs.
208 int kvm_arm_copy_reg_indices(struct kvm_vcpu *vcpu, u64 __user *uindices)
210 unsigned int i;
211 const u64 core_reg = KVM_REG_ARM64 | KVM_REG_SIZE_U64 | KVM_REG_ARM_CORE;
212 int ret;
214 for (i = 0; i < sizeof(struct kvm_regs) / sizeof(__u32); i++) {
215 if (put_user(core_reg | i, uindices))
216 return -EFAULT;
217 uindices++;
220 ret = copy_timer_indices(vcpu, uindices);
221 if (ret)
222 return ret;
223 uindices += NUM_TIMER_REGS;
225 return kvm_arm_copy_sys_reg_indices(vcpu, uindices);
228 int kvm_arm_get_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
230 /* We currently use nothing arch-specific in upper 32 bits */
231 if ((reg->id & ~KVM_REG_SIZE_MASK) >> 32 != KVM_REG_ARM64 >> 32)
232 return -EINVAL;
234 /* Register group 16 means we want a core register. */
235 if ((reg->id & KVM_REG_ARM_COPROC_MASK) == KVM_REG_ARM_CORE)
236 return get_core_reg(vcpu, reg);
238 if (is_timer_reg(reg->id))
239 return get_timer_reg(vcpu, reg);
241 return kvm_arm_sys_reg_get_reg(vcpu, reg);
244 int kvm_arm_set_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
246 /* We currently use nothing arch-specific in upper 32 bits */
247 if ((reg->id & ~KVM_REG_SIZE_MASK) >> 32 != KVM_REG_ARM64 >> 32)
248 return -EINVAL;
250 /* Register group 16 means we set a core register. */
251 if ((reg->id & KVM_REG_ARM_COPROC_MASK) == KVM_REG_ARM_CORE)
252 return set_core_reg(vcpu, reg);
254 if (is_timer_reg(reg->id))
255 return set_timer_reg(vcpu, reg);
257 return kvm_arm_sys_reg_set_reg(vcpu, reg);
260 int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
261 struct kvm_sregs *sregs)
263 return -EINVAL;
266 int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
267 struct kvm_sregs *sregs)
269 return -EINVAL;
272 int __attribute_const__ kvm_target_cpu(void)
274 unsigned long implementor = read_cpuid_implementor();
275 unsigned long part_number = read_cpuid_part_number();
277 switch (implementor) {
278 case ARM_CPU_IMP_ARM:
279 switch (part_number) {
280 case ARM_CPU_PART_AEM_V8:
281 return KVM_ARM_TARGET_AEM_V8;
282 case ARM_CPU_PART_FOUNDATION:
283 return KVM_ARM_TARGET_FOUNDATION_V8;
284 case ARM_CPU_PART_CORTEX_A53:
285 return KVM_ARM_TARGET_CORTEX_A53;
286 case ARM_CPU_PART_CORTEX_A57:
287 return KVM_ARM_TARGET_CORTEX_A57;
289 break;
290 case ARM_CPU_IMP_APM:
291 switch (part_number) {
292 case APM_CPU_PART_POTENZA:
293 return KVM_ARM_TARGET_XGENE_POTENZA;
295 break;
298 /* Return a default generic target */
299 return KVM_ARM_TARGET_GENERIC_V8;
302 int kvm_vcpu_preferred_target(struct kvm_vcpu_init *init)
304 int target = kvm_target_cpu();
306 if (target < 0)
307 return -ENODEV;
309 memset(init, 0, sizeof(*init));
312 * For now, we don't return any features.
313 * In future, we might use features to return target
314 * specific features available for the preferred
315 * target type.
317 init->target = (__u32)target;
319 return 0;
322 int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
324 return -EINVAL;
327 int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
329 return -EINVAL;
332 int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
333 struct kvm_translation *tr)
335 return -EINVAL;
338 #define KVM_GUESTDBG_VALID_MASK (KVM_GUESTDBG_ENABLE | \
339 KVM_GUESTDBG_USE_SW_BP | \
340 KVM_GUESTDBG_USE_HW | \
341 KVM_GUESTDBG_SINGLESTEP)
344 * kvm_arch_vcpu_ioctl_set_guest_debug - set up guest debugging
345 * @kvm: pointer to the KVM struct
346 * @kvm_guest_debug: the ioctl data buffer
348 * This sets up and enables the VM for guest debugging. Userspace
349 * passes in a control flag to enable different debug types and
350 * potentially other architecture specific information in the rest of
351 * the structure.
353 int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
354 struct kvm_guest_debug *dbg)
356 trace_kvm_set_guest_debug(vcpu, dbg->control);
358 if (dbg->control & ~KVM_GUESTDBG_VALID_MASK)
359 return -EINVAL;
361 if (dbg->control & KVM_GUESTDBG_ENABLE) {
362 vcpu->guest_debug = dbg->control;
364 /* Hardware assisted Break and Watch points */
365 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW) {
366 vcpu->arch.external_debug_state = dbg->arch;
369 } else {
370 /* If not enabled clear all flags */
371 vcpu->guest_debug = 0;
373 return 0;