KVM: ARM: Hypervisor initialization
[linux-2.6-block.git] / arch / arm / kvm / guest.c
CommitLineData
749cf76c
CD
1/*
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 */
18
19#include <linux/errno.h>
20#include <linux/err.h>
21#include <linux/kvm_host.h>
22#include <linux/module.h>
23#include <linux/vmalloc.h>
24#include <linux/fs.h>
25#include <asm/uaccess.h>
26#include <asm/kvm.h>
27#include <asm/kvm_asm.h>
28#include <asm/kvm_emulate.h>
29
30#define VM_STAT(x) { #x, offsetof(struct kvm, stat.x), KVM_STAT_VM }
31#define VCPU_STAT(x) { #x, offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU }
32
33struct kvm_stats_debugfs_item debugfs_entries[] = {
34 { NULL }
35};
36
37int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
38{
39 return 0;
40}
41
42static u64 core_reg_offset_from_id(u64 id)
43{
44 return id & ~(KVM_REG_ARCH_MASK | KVM_REG_SIZE_MASK | KVM_REG_ARM_CORE);
45}
46
47static int get_core_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
48{
49 u32 __user *uaddr = (u32 __user *)(long)reg->addr;
50 struct kvm_regs *regs = &vcpu->arch.regs;
51 u64 off;
52
53 if (KVM_REG_SIZE(reg->id) != 4)
54 return -ENOENT;
55
56 /* Our ID is an index into the kvm_regs struct. */
57 off = core_reg_offset_from_id(reg->id);
58 if (off >= sizeof(*regs) / KVM_REG_SIZE(reg->id))
59 return -ENOENT;
60
61 return put_user(((u32 *)regs)[off], uaddr);
62}
63
64static int set_core_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
65{
66 u32 __user *uaddr = (u32 __user *)(long)reg->addr;
67 struct kvm_regs *regs = &vcpu->arch.regs;
68 u64 off, val;
69
70 if (KVM_REG_SIZE(reg->id) != 4)
71 return -ENOENT;
72
73 /* Our ID is an index into the kvm_regs struct. */
74 off = core_reg_offset_from_id(reg->id);
75 if (off >= sizeof(*regs) / KVM_REG_SIZE(reg->id))
76 return -ENOENT;
77
78 if (get_user(val, uaddr) != 0)
79 return -EFAULT;
80
81 if (off == KVM_REG_ARM_CORE_REG(usr_regs.ARM_cpsr)) {
82 unsigned long mode = val & MODE_MASK;
83 switch (mode) {
84 case USR_MODE:
85 case FIQ_MODE:
86 case IRQ_MODE:
87 case SVC_MODE:
88 case ABT_MODE:
89 case UND_MODE:
90 break;
91 default:
92 return -EINVAL;
93 }
94 }
95
96 ((u32 *)regs)[off] = val;
97 return 0;
98}
99
100int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
101{
102 return -EINVAL;
103}
104
105int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
106{
107 return -EINVAL;
108}
109
110static unsigned long num_core_regs(void)
111{
112 return sizeof(struct kvm_regs) / sizeof(u32);
113}
114
115/**
116 * kvm_arm_num_regs - how many registers do we present via KVM_GET_ONE_REG
117 *
118 * This is for all registers.
119 */
120unsigned long kvm_arm_num_regs(struct kvm_vcpu *vcpu)
121{
122 return num_core_regs();
123}
124
125/**
126 * kvm_arm_copy_reg_indices - get indices of all registers.
127 *
128 * We do core registers right here, then we apppend coproc regs.
129 */
130int kvm_arm_copy_reg_indices(struct kvm_vcpu *vcpu, u64 __user *uindices)
131{
132 unsigned int i;
133 const u64 core_reg = KVM_REG_ARM | KVM_REG_SIZE_U32 | KVM_REG_ARM_CORE;
134
135 for (i = 0; i < sizeof(struct kvm_regs)/sizeof(u32); i++) {
136 if (put_user(core_reg | i, uindices))
137 return -EFAULT;
138 uindices++;
139 }
140
141 return 0;
142}
143
144int kvm_arm_get_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
145{
146 /* We currently use nothing arch-specific in upper 32 bits */
147 if ((reg->id & ~KVM_REG_SIZE_MASK) >> 32 != KVM_REG_ARM >> 32)
148 return -EINVAL;
149
150 /* Register group 16 means we want a core register. */
151 if ((reg->id & KVM_REG_ARM_COPROC_MASK) == KVM_REG_ARM_CORE)
152 return get_core_reg(vcpu, reg);
153
154 return -EINVAL;
155}
156
157int kvm_arm_set_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
158{
159 /* We currently use nothing arch-specific in upper 32 bits */
160 if ((reg->id & ~KVM_REG_SIZE_MASK) >> 32 != KVM_REG_ARM >> 32)
161 return -EINVAL;
162
163 /* Register group 16 means we set a core register. */
164 if ((reg->id & KVM_REG_ARM_COPROC_MASK) == KVM_REG_ARM_CORE)
165 return set_core_reg(vcpu, reg);
166
167 return -EINVAL;
168}
169
170int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
171 struct kvm_sregs *sregs)
172{
173 return -EINVAL;
174}
175
176int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
177 struct kvm_sregs *sregs)
178{
179 return -EINVAL;
180}
181
182int kvm_vcpu_set_target(struct kvm_vcpu *vcpu,
183 const struct kvm_vcpu_init *init)
184{
185 unsigned int i;
186
187 /* We can only do a cortex A15 for now. */
188 if (init->target != kvm_target_cpu())
189 return -EINVAL;
190
191 vcpu->arch.target = init->target;
192 bitmap_zero(vcpu->arch.features, KVM_VCPU_MAX_FEATURES);
193
194 /* -ENOENT for unknown features, -EINVAL for invalid combinations. */
195 for (i = 0; i < sizeof(init->features) * 8; i++) {
196 if (test_bit(i, (void *)init->features)) {
197 if (i >= KVM_VCPU_MAX_FEATURES)
198 return -ENOENT;
199 set_bit(i, vcpu->arch.features);
200 }
201 }
202
203 /* Now we know what it is, we can reset it. */
204 return kvm_reset_vcpu(vcpu);
205}
206
207int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
208{
209 return -EINVAL;
210}
211
212int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
213{
214 return -EINVAL;
215}
216
217int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
218 struct kvm_translation *tr)
219{
220 return -EINVAL;
221}