aurel32 | d76d165 | 2008-12-16 10:43:58 +0000 | [diff] [blame] | 1 | /* |
| 2 | * PowerPC implementation of KVM hooks |
| 3 | * |
| 4 | * Copyright IBM Corp. 2007 |
| 5 | * |
| 6 | * Authors: |
| 7 | * Jerone Young <jyoung5@us.ibm.com> |
| 8 | * Christian Ehrhardt <ehrhardt@linux.vnet.ibm.com> |
| 9 | * Hollis Blanchard <hollisb@us.ibm.com> |
| 10 | * |
| 11 | * This work is licensed under the terms of the GNU GPL, version 2 or later. |
| 12 | * See the COPYING file in the top-level directory. |
| 13 | * |
| 14 | */ |
| 15 | |
| 16 | #include <sys/types.h> |
| 17 | #include <sys/ioctl.h> |
| 18 | #include <sys/mman.h> |
| 19 | |
| 20 | #include <linux/kvm.h> |
| 21 | |
| 22 | #include "qemu-common.h" |
| 23 | #include "qemu-timer.h" |
| 24 | #include "sysemu.h" |
| 25 | #include "kvm.h" |
| 26 | #include "kvm_ppc.h" |
| 27 | #include "cpu.h" |
| 28 | #include "device_tree.h" |
| 29 | |
| 30 | //#define DEBUG_KVM |
| 31 | |
| 32 | #ifdef DEBUG_KVM |
| 33 | #define dprintf(fmt, ...) \ |
| 34 | do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0) |
| 35 | #else |
| 36 | #define dprintf(fmt, ...) \ |
| 37 | do { } while (0) |
| 38 | #endif |
| 39 | |
Jan Kiszka | 94a8d39 | 2011-01-21 21:48:17 +0100 | [diff] [blame] | 40 | const KVMCapabilityInfo kvm_arch_required_capabilities[] = { |
| 41 | KVM_CAP_LAST_INFO |
| 42 | }; |
| 43 | |
Alexander Graf | fc87e18 | 2010-08-30 13:49:15 +0200 | [diff] [blame] | 44 | static int cap_interrupt_unset = false; |
| 45 | static int cap_interrupt_level = false; |
| 46 | |
Alexander Graf | c821c2b | 2010-04-18 23:10:17 +0200 | [diff] [blame] | 47 | /* XXX We have a race condition where we actually have a level triggered |
| 48 | * interrupt, but the infrastructure can't expose that yet, so the guest |
| 49 | * takes but ignores it, goes to sleep and never gets notified that there's |
| 50 | * still an interrupt pending. |
Alexander Graf | c6a94ba | 2010-02-09 17:37:10 +0100 | [diff] [blame] | 51 | * |
Alexander Graf | c821c2b | 2010-04-18 23:10:17 +0200 | [diff] [blame] | 52 | * As a quick workaround, let's just wake up again 20 ms after we injected |
| 53 | * an interrupt. That way we can assure that we're always reinjecting |
| 54 | * interrupts in case the guest swallowed them. |
Alexander Graf | c6a94ba | 2010-02-09 17:37:10 +0100 | [diff] [blame] | 55 | */ |
| 56 | static QEMUTimer *idle_timer; |
| 57 | |
Alexander Graf | c821c2b | 2010-04-18 23:10:17 +0200 | [diff] [blame] | 58 | static void kvm_kick_env(void *env) |
Alexander Graf | c6a94ba | 2010-02-09 17:37:10 +0100 | [diff] [blame] | 59 | { |
Alexander Graf | c821c2b | 2010-04-18 23:10:17 +0200 | [diff] [blame] | 60 | qemu_cpu_kick(env); |
Alexander Graf | c6a94ba | 2010-02-09 17:37:10 +0100 | [diff] [blame] | 61 | } |
| 62 | |
Jan Kiszka | cad1e28 | 2011-01-21 21:48:16 +0100 | [diff] [blame] | 63 | int kvm_arch_init(KVMState *s) |
aurel32 | d76d165 | 2008-12-16 10:43:58 +0000 | [diff] [blame] | 64 | { |
Alexander Graf | fc87e18 | 2010-08-30 13:49:15 +0200 | [diff] [blame] | 65 | #ifdef KVM_CAP_PPC_UNSET_IRQ |
| 66 | cap_interrupt_unset = kvm_check_extension(s, KVM_CAP_PPC_UNSET_IRQ); |
| 67 | #endif |
| 68 | #ifdef KVM_CAP_PPC_IRQ_LEVEL |
| 69 | cap_interrupt_level = kvm_check_extension(s, KVM_CAP_PPC_IRQ_LEVEL); |
| 70 | #endif |
| 71 | |
| 72 | if (!cap_interrupt_level) { |
| 73 | fprintf(stderr, "KVM: Couldn't find level irq capability. Expect the " |
| 74 | "VM to stall at times!\n"); |
| 75 | } |
| 76 | |
aurel32 | d76d165 | 2008-12-16 10:43:58 +0000 | [diff] [blame] | 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | int kvm_arch_init_vcpu(CPUState *cenv) |
| 81 | { |
Alexander Graf | 861bbc80 | 2009-07-17 13:51:43 +0200 | [diff] [blame] | 82 | int ret = 0; |
| 83 | struct kvm_sregs sregs; |
| 84 | |
| 85 | sregs.pvr = cenv->spr[SPR_PVR]; |
| 86 | ret = kvm_vcpu_ioctl(cenv, KVM_SET_SREGS, &sregs); |
| 87 | |
Alexander Graf | c821c2b | 2010-04-18 23:10:17 +0200 | [diff] [blame] | 88 | idle_timer = qemu_new_timer(vm_clock, kvm_kick_env, cenv); |
| 89 | |
Alexander Graf | 861bbc80 | 2009-07-17 13:51:43 +0200 | [diff] [blame] | 90 | return ret; |
aurel32 | d76d165 | 2008-12-16 10:43:58 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Jan Kiszka | caa5af0 | 2009-11-06 19:39:24 +0100 | [diff] [blame] | 93 | void kvm_arch_reset_vcpu(CPUState *env) |
| 94 | { |
| 95 | } |
| 96 | |
Jan Kiszka | ea375f9 | 2010-03-01 19:10:30 +0100 | [diff] [blame] | 97 | int kvm_arch_put_registers(CPUState *env, int level) |
aurel32 | d76d165 | 2008-12-16 10:43:58 +0000 | [diff] [blame] | 98 | { |
| 99 | struct kvm_regs regs; |
| 100 | int ret; |
| 101 | int i; |
| 102 | |
| 103 | ret = kvm_vcpu_ioctl(env, KVM_GET_REGS, ®s); |
| 104 | if (ret < 0) |
| 105 | return ret; |
| 106 | |
| 107 | regs.ctr = env->ctr; |
| 108 | regs.lr = env->lr; |
| 109 | regs.xer = env->xer; |
| 110 | regs.msr = env->msr; |
| 111 | regs.pc = env->nip; |
| 112 | |
| 113 | regs.srr0 = env->spr[SPR_SRR0]; |
| 114 | regs.srr1 = env->spr[SPR_SRR1]; |
| 115 | |
| 116 | regs.sprg0 = env->spr[SPR_SPRG0]; |
| 117 | regs.sprg1 = env->spr[SPR_SPRG1]; |
| 118 | regs.sprg2 = env->spr[SPR_SPRG2]; |
| 119 | regs.sprg3 = env->spr[SPR_SPRG3]; |
| 120 | regs.sprg4 = env->spr[SPR_SPRG4]; |
| 121 | regs.sprg5 = env->spr[SPR_SPRG5]; |
| 122 | regs.sprg6 = env->spr[SPR_SPRG6]; |
| 123 | regs.sprg7 = env->spr[SPR_SPRG7]; |
| 124 | |
| 125 | for (i = 0;i < 32; i++) |
| 126 | regs.gpr[i] = env->gpr[i]; |
| 127 | |
| 128 | ret = kvm_vcpu_ioctl(env, KVM_SET_REGS, ®s); |
| 129 | if (ret < 0) |
| 130 | return ret; |
| 131 | |
| 132 | return ret; |
| 133 | } |
| 134 | |
| 135 | int kvm_arch_get_registers(CPUState *env) |
| 136 | { |
| 137 | struct kvm_regs regs; |
Alexander Graf | ba5e509 | 2009-12-02 23:19:47 +0100 | [diff] [blame] | 138 | struct kvm_sregs sregs; |
Alexander Graf | 138b38b | 2010-11-25 08:20:46 +0100 | [diff] [blame] | 139 | int i, ret; |
aurel32 | d76d165 | 2008-12-16 10:43:58 +0000 | [diff] [blame] | 140 | |
| 141 | ret = kvm_vcpu_ioctl(env, KVM_GET_REGS, ®s); |
| 142 | if (ret < 0) |
| 143 | return ret; |
| 144 | |
Alexander Graf | ba5e509 | 2009-12-02 23:19:47 +0100 | [diff] [blame] | 145 | ret = kvm_vcpu_ioctl(env, KVM_GET_SREGS, &sregs); |
| 146 | if (ret < 0) |
| 147 | return ret; |
| 148 | |
aurel32 | d76d165 | 2008-12-16 10:43:58 +0000 | [diff] [blame] | 149 | env->ctr = regs.ctr; |
| 150 | env->lr = regs.lr; |
| 151 | env->xer = regs.xer; |
| 152 | env->msr = regs.msr; |
| 153 | env->nip = regs.pc; |
| 154 | |
| 155 | env->spr[SPR_SRR0] = regs.srr0; |
| 156 | env->spr[SPR_SRR1] = regs.srr1; |
| 157 | |
| 158 | env->spr[SPR_SPRG0] = regs.sprg0; |
| 159 | env->spr[SPR_SPRG1] = regs.sprg1; |
| 160 | env->spr[SPR_SPRG2] = regs.sprg2; |
| 161 | env->spr[SPR_SPRG3] = regs.sprg3; |
| 162 | env->spr[SPR_SPRG4] = regs.sprg4; |
| 163 | env->spr[SPR_SPRG5] = regs.sprg5; |
| 164 | env->spr[SPR_SPRG6] = regs.sprg6; |
| 165 | env->spr[SPR_SPRG7] = regs.sprg7; |
| 166 | |
| 167 | for (i = 0;i < 32; i++) |
| 168 | env->gpr[i] = regs.gpr[i]; |
| 169 | |
Alexander Graf | ba5e509 | 2009-12-02 23:19:47 +0100 | [diff] [blame] | 170 | #ifdef KVM_CAP_PPC_SEGSTATE |
| 171 | if (kvm_check_extension(env->kvm_state, KVM_CAP_PPC_SEGSTATE)) { |
| 172 | env->sdr1 = sregs.u.s.sdr1; |
| 173 | |
| 174 | /* Sync SLB */ |
Alexander Graf | 82c09f2 | 2009-12-19 01:58:59 +0100 | [diff] [blame] | 175 | #ifdef TARGET_PPC64 |
Alexander Graf | ba5e509 | 2009-12-02 23:19:47 +0100 | [diff] [blame] | 176 | for (i = 0; i < 64; i++) { |
| 177 | ppc_store_slb(env, sregs.u.s.ppc64.slb[i].slbe, |
| 178 | sregs.u.s.ppc64.slb[i].slbv); |
| 179 | } |
Alexander Graf | 82c09f2 | 2009-12-19 01:58:59 +0100 | [diff] [blame] | 180 | #endif |
Alexander Graf | ba5e509 | 2009-12-02 23:19:47 +0100 | [diff] [blame] | 181 | |
| 182 | /* Sync SRs */ |
| 183 | for (i = 0; i < 16; i++) { |
| 184 | env->sr[i] = sregs.u.s.ppc32.sr[i]; |
| 185 | } |
| 186 | |
| 187 | /* Sync BATs */ |
| 188 | for (i = 0; i < 8; i++) { |
| 189 | env->DBAT[0][i] = sregs.u.s.ppc32.dbat[i] & 0xffffffff; |
| 190 | env->DBAT[1][i] = sregs.u.s.ppc32.dbat[i] >> 32; |
| 191 | env->IBAT[0][i] = sregs.u.s.ppc32.ibat[i] & 0xffffffff; |
| 192 | env->IBAT[1][i] = sregs.u.s.ppc32.ibat[i] >> 32; |
| 193 | } |
| 194 | } |
| 195 | #endif |
| 196 | |
aurel32 | d76d165 | 2008-12-16 10:43:58 +0000 | [diff] [blame] | 197 | return 0; |
| 198 | } |
| 199 | |
Alexander Graf | fc87e18 | 2010-08-30 13:49:15 +0200 | [diff] [blame] | 200 | int kvmppc_set_interrupt(CPUState *env, int irq, int level) |
| 201 | { |
| 202 | unsigned virq = level ? KVM_INTERRUPT_SET_LEVEL : KVM_INTERRUPT_UNSET; |
| 203 | |
| 204 | if (irq != PPC_INTERRUPT_EXT) { |
| 205 | return 0; |
| 206 | } |
| 207 | |
| 208 | if (!kvm_enabled() || !cap_interrupt_unset || !cap_interrupt_level) { |
| 209 | return 0; |
| 210 | } |
| 211 | |
| 212 | kvm_vcpu_ioctl(env, KVM_INTERRUPT, &virq); |
| 213 | |
| 214 | return 0; |
| 215 | } |
| 216 | |
Alexander Graf | 1641533 | 2009-07-17 13:51:46 +0200 | [diff] [blame] | 217 | #if defined(TARGET_PPCEMB) |
| 218 | #define PPC_INPUT_INT PPC40x_INPUT_INT |
| 219 | #elif defined(TARGET_PPC64) |
| 220 | #define PPC_INPUT_INT PPC970_INPUT_INT |
| 221 | #else |
| 222 | #define PPC_INPUT_INT PPC6xx_INPUT_INT |
| 223 | #endif |
| 224 | |
aurel32 | d76d165 | 2008-12-16 10:43:58 +0000 | [diff] [blame] | 225 | int kvm_arch_pre_run(CPUState *env, struct kvm_run *run) |
| 226 | { |
| 227 | int r; |
| 228 | unsigned irq; |
| 229 | |
| 230 | /* PowerPC Qemu tracks the various core input pins (interrupt, critical |
| 231 | * interrupt, reset, etc) in PPC-specific env->irq_input_state. */ |
Alexander Graf | fc87e18 | 2010-08-30 13:49:15 +0200 | [diff] [blame] | 232 | if (!cap_interrupt_level && |
| 233 | run->ready_for_interrupt_injection && |
aurel32 | d76d165 | 2008-12-16 10:43:58 +0000 | [diff] [blame] | 234 | (env->interrupt_request & CPU_INTERRUPT_HARD) && |
Alexander Graf | 1641533 | 2009-07-17 13:51:46 +0200 | [diff] [blame] | 235 | (env->irq_input_state & (1<<PPC_INPUT_INT))) |
aurel32 | d76d165 | 2008-12-16 10:43:58 +0000 | [diff] [blame] | 236 | { |
| 237 | /* For now KVM disregards the 'irq' argument. However, in the |
| 238 | * future KVM could cache it in-kernel to avoid a heavyweight exit |
| 239 | * when reading the UIC. |
| 240 | */ |
Alexander Graf | fc87e18 | 2010-08-30 13:49:15 +0200 | [diff] [blame] | 241 | irq = KVM_INTERRUPT_SET; |
aurel32 | d76d165 | 2008-12-16 10:43:58 +0000 | [diff] [blame] | 242 | |
| 243 | dprintf("injected interrupt %d\n", irq); |
| 244 | r = kvm_vcpu_ioctl(env, KVM_INTERRUPT, &irq); |
| 245 | if (r < 0) |
| 246 | printf("cpu %d fail inject %x\n", env->cpu_index, irq); |
Alexander Graf | c821c2b | 2010-04-18 23:10:17 +0200 | [diff] [blame] | 247 | |
| 248 | /* Always wake up soon in case the interrupt was level based */ |
| 249 | qemu_mod_timer(idle_timer, qemu_get_clock(vm_clock) + |
| 250 | (get_ticks_per_sec() / 50)); |
aurel32 | d76d165 | 2008-12-16 10:43:58 +0000 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | /* We don't know if there are more interrupts pending after this. However, |
| 254 | * the guest will return to userspace in the course of handling this one |
| 255 | * anyways, so we will get a chance to deliver the rest. */ |
| 256 | return 0; |
| 257 | } |
| 258 | |
Jan Kiszka | 7a39fe5 | 2011-02-07 12:19:20 +0100 | [diff] [blame] | 259 | void kvm_arch_post_run(CPUState *env, struct kvm_run *run) |
aurel32 | d76d165 | 2008-12-16 10:43:58 +0000 | [diff] [blame] | 260 | { |
aurel32 | d76d165 | 2008-12-16 10:43:58 +0000 | [diff] [blame] | 261 | } |
| 262 | |
Jan Kiszka | 7a39fe5 | 2011-02-07 12:19:20 +0100 | [diff] [blame] | 263 | void kvm_arch_process_irqchip_events(CPUState *env) |
Marcelo Tosatti | 0af691d | 2010-05-04 09:45:27 -0300 | [diff] [blame] | 264 | { |
Marcelo Tosatti | 0af691d | 2010-05-04 09:45:27 -0300 | [diff] [blame] | 265 | } |
| 266 | |
aurel32 | d76d165 | 2008-12-16 10:43:58 +0000 | [diff] [blame] | 267 | static int kvmppc_handle_halt(CPUState *env) |
| 268 | { |
| 269 | if (!(env->interrupt_request & CPU_INTERRUPT_HARD) && (msr_ee)) { |
| 270 | env->halted = 1; |
| 271 | env->exception_index = EXCP_HLT; |
| 272 | } |
| 273 | |
| 274 | return 1; |
| 275 | } |
| 276 | |
| 277 | /* map dcr access to existing qemu dcr emulation */ |
| 278 | static int kvmppc_handle_dcr_read(CPUState *env, uint32_t dcrn, uint32_t *data) |
| 279 | { |
| 280 | if (ppc_dcr_read(env->dcr_env, dcrn, data) < 0) |
| 281 | fprintf(stderr, "Read to unhandled DCR (0x%x)\n", dcrn); |
| 282 | |
| 283 | return 1; |
| 284 | } |
| 285 | |
| 286 | static int kvmppc_handle_dcr_write(CPUState *env, uint32_t dcrn, uint32_t data) |
| 287 | { |
| 288 | if (ppc_dcr_write(env->dcr_env, dcrn, data) < 0) |
| 289 | fprintf(stderr, "Write to unhandled DCR (0x%x)\n", dcrn); |
| 290 | |
| 291 | return 1; |
| 292 | } |
| 293 | |
| 294 | int kvm_arch_handle_exit(CPUState *env, struct kvm_run *run) |
| 295 | { |
| 296 | int ret = 0; |
| 297 | |
| 298 | switch (run->exit_reason) { |
| 299 | case KVM_EXIT_DCR: |
| 300 | if (run->dcr.is_write) { |
| 301 | dprintf("handle dcr write\n"); |
| 302 | ret = kvmppc_handle_dcr_write(env, run->dcr.dcrn, run->dcr.data); |
| 303 | } else { |
| 304 | dprintf("handle dcr read\n"); |
| 305 | ret = kvmppc_handle_dcr_read(env, run->dcr.dcrn, &run->dcr.data); |
| 306 | } |
| 307 | break; |
| 308 | case KVM_EXIT_HLT: |
| 309 | dprintf("handle halt\n"); |
| 310 | ret = kvmppc_handle_halt(env); |
| 311 | break; |
Jan Kiszka | 73aaec4 | 2011-01-21 21:48:06 +0100 | [diff] [blame] | 312 | default: |
| 313 | fprintf(stderr, "KVM: unknown exit reason %d\n", run->exit_reason); |
| 314 | ret = -1; |
| 315 | break; |
aurel32 | d76d165 | 2008-12-16 10:43:58 +0000 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | return ret; |
| 319 | } |
| 320 | |
Alexander Graf | dc333cd | 2010-02-09 17:37:05 +0100 | [diff] [blame] | 321 | static int read_cpuinfo(const char *field, char *value, int len) |
| 322 | { |
| 323 | FILE *f; |
| 324 | int ret = -1; |
| 325 | int field_len = strlen(field); |
| 326 | char line[512]; |
| 327 | |
| 328 | f = fopen("/proc/cpuinfo", "r"); |
| 329 | if (!f) { |
| 330 | return -1; |
| 331 | } |
| 332 | |
| 333 | do { |
| 334 | if(!fgets(line, sizeof(line), f)) { |
| 335 | break; |
| 336 | } |
| 337 | if (!strncmp(line, field, field_len)) { |
| 338 | strncpy(value, line, len); |
| 339 | ret = 0; |
| 340 | break; |
| 341 | } |
| 342 | } while(*line); |
| 343 | |
| 344 | fclose(f); |
| 345 | |
| 346 | return ret; |
| 347 | } |
| 348 | |
| 349 | uint32_t kvmppc_get_tbfreq(void) |
| 350 | { |
| 351 | char line[512]; |
| 352 | char *ns; |
| 353 | uint32_t retval = get_ticks_per_sec(); |
| 354 | |
| 355 | if (read_cpuinfo("timebase", line, sizeof(line))) { |
| 356 | return retval; |
| 357 | } |
| 358 | |
| 359 | if (!(ns = strchr(line, ':'))) { |
| 360 | return retval; |
| 361 | } |
| 362 | |
| 363 | ns++; |
| 364 | |
| 365 | retval = atoi(ns); |
| 366 | return retval; |
| 367 | } |
Gleb Natapov | 4513d92 | 2010-05-10 11:21:34 +0300 | [diff] [blame] | 368 | |
Alexander Graf | 45024f0 | 2010-08-03 15:22:42 +0200 | [diff] [blame] | 369 | int kvmppc_get_hypercall(CPUState *env, uint8_t *buf, int buf_len) |
| 370 | { |
| 371 | uint32_t *hc = (uint32_t*)buf; |
| 372 | |
| 373 | #ifdef KVM_CAP_PPC_GET_PVINFO |
| 374 | struct kvm_ppc_pvinfo pvinfo; |
| 375 | |
| 376 | if (kvm_check_extension(env->kvm_state, KVM_CAP_PPC_GET_PVINFO) && |
| 377 | !kvm_vm_ioctl(env->kvm_state, KVM_PPC_GET_PVINFO, &pvinfo)) { |
| 378 | memcpy(buf, pvinfo.hcall, buf_len); |
| 379 | |
| 380 | return 0; |
| 381 | } |
| 382 | #endif |
| 383 | |
| 384 | /* |
| 385 | * Fallback to always fail hypercalls: |
| 386 | * |
| 387 | * li r3, -1 |
| 388 | * nop |
| 389 | * nop |
| 390 | * nop |
| 391 | */ |
| 392 | |
| 393 | hc[0] = 0x3860ffff; |
| 394 | hc[1] = 0x60000000; |
| 395 | hc[2] = 0x60000000; |
| 396 | hc[3] = 0x60000000; |
| 397 | |
| 398 | return 0; |
| 399 | } |
| 400 | |
Gleb Natapov | 4513d92 | 2010-05-10 11:21:34 +0300 | [diff] [blame] | 401 | bool kvm_arch_stop_on_emulation_error(CPUState *env) |
| 402 | { |
| 403 | return true; |
| 404 | } |
Jan Kiszka | a1b87fe | 2011-02-01 22:15:51 +0100 | [diff] [blame] | 405 | |
| 406 | int kvm_arch_on_sigbus_vcpu(CPUState *env, int code, void *addr) |
| 407 | { |
| 408 | return 1; |
| 409 | } |
| 410 | |
| 411 | int kvm_arch_on_sigbus(int code, void *addr) |
| 412 | { |
| 413 | return 1; |
| 414 | } |