Microblaze: Convert Microblaze-pic handling to GPIOs
[qemu/cris-port.git] / target-arm / kvm64.c
blob1b7ca903746389b668e7c22a2779ceb96a33210b
1 /*
2 * ARM implementation of KVM hooks, 64 bit specific code
4 * Copyright Mian-M. Hamayun 2013, Virtual Open Systems
6 * This work is licensed under the terms of the GNU GPL, version 2 or later.
7 * See the COPYING file in the top-level directory.
9 */
11 #include <stdio.h>
12 #include <sys/types.h>
13 #include <sys/ioctl.h>
14 #include <sys/mman.h>
16 #include <linux/kvm.h>
18 #include "qemu-common.h"
19 #include "qemu/timer.h"
20 #include "sysemu/sysemu.h"
21 #include "sysemu/kvm.h"
22 #include "kvm_arm.h"
23 #include "cpu.h"
24 #include "hw/arm/arm.h"
26 static inline void set_feature(uint64_t *features, int feature)
28 *features |= 1ULL << feature;
31 bool kvm_arm_get_host_cpu_features(ARMHostCPUClass *ahcc)
33 /* Identify the feature bits corresponding to the host CPU, and
34 * fill out the ARMHostCPUClass fields accordingly. To do this
35 * we have to create a scratch VM, create a single CPU inside it,
36 * and then query that CPU for the relevant ID registers.
37 * For AArch64 we currently don't care about ID registers at
38 * all; we just want to know the CPU type.
40 int fdarray[3];
41 uint64_t features = 0;
42 /* Old kernels may not know about the PREFERRED_TARGET ioctl: however
43 * we know these will only support creating one kind of guest CPU,
44 * which is its preferred CPU type. Fortunately these old kernels
45 * support only a very limited number of CPUs.
47 static const uint32_t cpus_to_try[] = {
48 KVM_ARM_TARGET_AEM_V8,
49 KVM_ARM_TARGET_FOUNDATION_V8,
50 KVM_ARM_TARGET_CORTEX_A57,
51 QEMU_KVM_ARM_TARGET_NONE
53 struct kvm_vcpu_init init;
55 if (!kvm_arm_create_scratch_host_vcpu(cpus_to_try, fdarray, &init)) {
56 return false;
59 ahcc->target = init.target;
60 ahcc->dtb_compatible = "arm,arm-v8";
62 kvm_arm_destroy_scratch_host_vcpu(fdarray);
64 /* We can assume any KVM supporting CPU is at least a v8
65 * with VFPv4+Neon; this in turn implies most of the other
66 * feature bits.
68 set_feature(&features, ARM_FEATURE_V8);
69 set_feature(&features, ARM_FEATURE_VFP4);
70 set_feature(&features, ARM_FEATURE_NEON);
71 set_feature(&features, ARM_FEATURE_AARCH64);
73 ahcc->features = features;
75 return true;
78 int kvm_arch_init_vcpu(CPUState *cs)
80 ARMCPU *cpu = ARM_CPU(cs);
81 struct kvm_vcpu_init init;
82 int ret;
84 if (cpu->kvm_target == QEMU_KVM_ARM_TARGET_NONE ||
85 !arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) {
86 fprintf(stderr, "KVM is not supported for this guest CPU type\n");
87 return -EINVAL;
90 init.target = cpu->kvm_target;
91 memset(init.features, 0, sizeof(init.features));
92 if (cpu->start_powered_off) {
93 init.features[0] = 1 << KVM_ARM_VCPU_POWER_OFF;
95 ret = kvm_vcpu_ioctl(cs, KVM_ARM_VCPU_INIT, &init);
97 /* TODO : support for save/restore/reset of system regs via tuple list */
99 return ret;
102 #define AARCH64_CORE_REG(x) (KVM_REG_ARM64 | KVM_REG_SIZE_U64 | \
103 KVM_REG_ARM_CORE | KVM_REG_ARM_CORE_REG(x))
105 int kvm_arch_put_registers(CPUState *cs, int level)
107 struct kvm_one_reg reg;
108 uint64_t val;
109 int i;
110 int ret;
112 ARMCPU *cpu = ARM_CPU(cs);
113 CPUARMState *env = &cpu->env;
115 for (i = 0; i < 31; i++) {
116 reg.id = AARCH64_CORE_REG(regs.regs[i]);
117 reg.addr = (uintptr_t) &env->xregs[i];
118 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
119 if (ret) {
120 return ret;
124 reg.id = AARCH64_CORE_REG(regs.sp);
125 reg.addr = (uintptr_t) &env->xregs[31];
126 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
127 if (ret) {
128 return ret;
131 /* Note that KVM thinks pstate is 64 bit but we use a uint32_t */
132 val = pstate_read(env);
133 reg.id = AARCH64_CORE_REG(regs.pstate);
134 reg.addr = (uintptr_t) &val;
135 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
136 if (ret) {
137 return ret;
140 reg.id = AARCH64_CORE_REG(regs.pc);
141 reg.addr = (uintptr_t) &env->pc;
142 ret = kvm_vcpu_ioctl(cs, KVM_SET_ONE_REG, &reg);
143 if (ret) {
144 return ret;
147 /* TODO:
148 * SP_EL1
149 * ELR_EL1
150 * SPSR[]
151 * FP state
152 * system registers
154 return ret;
157 int kvm_arch_get_registers(CPUState *cs)
159 struct kvm_one_reg reg;
160 uint64_t val;
161 int i;
162 int ret;
164 ARMCPU *cpu = ARM_CPU(cs);
165 CPUARMState *env = &cpu->env;
167 for (i = 0; i < 31; i++) {
168 reg.id = AARCH64_CORE_REG(regs.regs[i]);
169 reg.addr = (uintptr_t) &env->xregs[i];
170 ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
171 if (ret) {
172 return ret;
176 reg.id = AARCH64_CORE_REG(regs.sp);
177 reg.addr = (uintptr_t) &env->xregs[31];
178 ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
179 if (ret) {
180 return ret;
183 reg.id = AARCH64_CORE_REG(regs.pstate);
184 reg.addr = (uintptr_t) &val;
185 ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
186 if (ret) {
187 return ret;
189 pstate_write(env, val);
191 reg.id = AARCH64_CORE_REG(regs.pc);
192 reg.addr = (uintptr_t) &env->pc;
193 ret = kvm_vcpu_ioctl(cs, KVM_GET_ONE_REG, &reg);
194 if (ret) {
195 return ret;
198 /* TODO: other registers */
199 return ret;
202 void kvm_arch_reset_vcpu(CPUState *cs)