Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 1 | /* |
| 2 | * This file is subject to the terms and conditions of the GNU General Public |
| 3 | * License. See the file "COPYING" in the main directory of this archive |
| 4 | * for more details. |
| 5 | * |
| 6 | * KVM/MIPS: MIPS specific KVM APIs |
| 7 | * |
| 8 | * Copyright (C) 2012 MIPS Technologies, Inc. All rights reserved. |
| 9 | * Authors: Sanjay Lal <sanjayl@kymasys.com> |
| 10 | */ |
| 11 | |
| 12 | #include <linux/errno.h> |
| 13 | #include <linux/err.h> |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/vmalloc.h> |
| 16 | #include <linux/fs.h> |
| 17 | #include <linux/bootmem.h> |
| 18 | #include <asm/page.h> |
| 19 | #include <asm/cacheflush.h> |
| 20 | #include <asm/mmu_context.h> |
| 21 | |
| 22 | #include <linux/kvm_host.h> |
| 23 | |
| 24 | #include "kvm_mips_int.h" |
| 25 | #include "kvm_mips_comm.h" |
| 26 | |
| 27 | #define CREATE_TRACE_POINTS |
| 28 | #include "trace.h" |
| 29 | |
| 30 | #ifndef VECTORSPACING |
| 31 | #define VECTORSPACING 0x100 /* for EI/VI mode */ |
| 32 | #endif |
| 33 | |
| 34 | #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU |
| 35 | struct kvm_stats_debugfs_item debugfs_entries[] = { |
| 36 | { "wait", VCPU_STAT(wait_exits) }, |
| 37 | { "cache", VCPU_STAT(cache_exits) }, |
| 38 | { "signal", VCPU_STAT(signal_exits) }, |
| 39 | { "interrupt", VCPU_STAT(int_exits) }, |
| 40 | { "cop_unsuable", VCPU_STAT(cop_unusable_exits) }, |
| 41 | { "tlbmod", VCPU_STAT(tlbmod_exits) }, |
| 42 | { "tlbmiss_ld", VCPU_STAT(tlbmiss_ld_exits) }, |
| 43 | { "tlbmiss_st", VCPU_STAT(tlbmiss_st_exits) }, |
| 44 | { "addrerr_st", VCPU_STAT(addrerr_st_exits) }, |
| 45 | { "addrerr_ld", VCPU_STAT(addrerr_ld_exits) }, |
| 46 | { "syscall", VCPU_STAT(syscall_exits) }, |
| 47 | { "resvd_inst", VCPU_STAT(resvd_inst_exits) }, |
| 48 | { "break_inst", VCPU_STAT(break_inst_exits) }, |
| 49 | { "flush_dcache", VCPU_STAT(flush_dcache_exits) }, |
| 50 | { "halt_wakeup", VCPU_STAT(halt_wakeup) }, |
| 51 | {NULL} |
| 52 | }; |
| 53 | |
| 54 | static int kvm_mips_reset_vcpu(struct kvm_vcpu *vcpu) |
| 55 | { |
| 56 | int i; |
| 57 | for_each_possible_cpu(i) { |
| 58 | vcpu->arch.guest_kernel_asid[i] = 0; |
| 59 | vcpu->arch.guest_user_asid[i] = 0; |
| 60 | } |
| 61 | return 0; |
| 62 | } |
| 63 | |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 64 | /* XXXKYMA: We are simulatoring a processor that has the WII bit set in Config7, so we |
| 65 | * are "runnable" if interrupts are pending |
| 66 | */ |
| 67 | int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu) |
| 68 | { |
| 69 | return !!(vcpu->arch.pending_exceptions); |
| 70 | } |
| 71 | |
| 72 | int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu) |
| 73 | { |
| 74 | return 1; |
| 75 | } |
| 76 | |
| 77 | int kvm_arch_hardware_enable(void *garbage) |
| 78 | { |
| 79 | return 0; |
| 80 | } |
| 81 | |
| 82 | void kvm_arch_hardware_disable(void *garbage) |
| 83 | { |
| 84 | } |
| 85 | |
| 86 | int kvm_arch_hardware_setup(void) |
| 87 | { |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | void kvm_arch_hardware_unsetup(void) |
| 92 | { |
| 93 | } |
| 94 | |
| 95 | void kvm_arch_check_processor_compat(void *rtn) |
| 96 | { |
| 97 | int *r = (int *)rtn; |
| 98 | *r = 0; |
| 99 | return; |
| 100 | } |
| 101 | |
| 102 | static void kvm_mips_init_tlbs(struct kvm *kvm) |
| 103 | { |
| 104 | unsigned long wired; |
| 105 | |
| 106 | /* Add a wired entry to the TLB, it is used to map the commpage to the Guest kernel */ |
| 107 | wired = read_c0_wired(); |
| 108 | write_c0_wired(wired + 1); |
| 109 | mtc0_tlbw_hazard(); |
| 110 | kvm->arch.commpage_tlb = wired; |
| 111 | |
| 112 | kvm_debug("[%d] commpage TLB: %d\n", smp_processor_id(), |
| 113 | kvm->arch.commpage_tlb); |
| 114 | } |
| 115 | |
| 116 | static void kvm_mips_init_vm_percpu(void *arg) |
| 117 | { |
| 118 | struct kvm *kvm = (struct kvm *)arg; |
| 119 | |
| 120 | kvm_mips_init_tlbs(kvm); |
| 121 | kvm_mips_callbacks->vm_init(kvm); |
| 122 | |
| 123 | } |
| 124 | |
| 125 | int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) |
| 126 | { |
| 127 | if (atomic_inc_return(&kvm_mips_instance) == 1) { |
| 128 | kvm_info("%s: 1st KVM instance, setup host TLB parameters\n", |
| 129 | __func__); |
| 130 | on_each_cpu(kvm_mips_init_vm_percpu, kvm, 1); |
| 131 | } |
| 132 | |
| 133 | |
| 134 | return 0; |
| 135 | } |
| 136 | |
| 137 | void kvm_mips_free_vcpus(struct kvm *kvm) |
| 138 | { |
| 139 | unsigned int i; |
| 140 | struct kvm_vcpu *vcpu; |
| 141 | |
| 142 | /* Put the pages we reserved for the guest pmap */ |
| 143 | for (i = 0; i < kvm->arch.guest_pmap_npages; i++) { |
| 144 | if (kvm->arch.guest_pmap[i] != KVM_INVALID_PAGE) |
| 145 | kvm_mips_release_pfn_clean(kvm->arch.guest_pmap[i]); |
| 146 | } |
| 147 | |
| 148 | if (kvm->arch.guest_pmap) |
| 149 | kfree(kvm->arch.guest_pmap); |
| 150 | |
| 151 | kvm_for_each_vcpu(i, vcpu, kvm) { |
| 152 | kvm_arch_vcpu_free(vcpu); |
| 153 | } |
| 154 | |
| 155 | mutex_lock(&kvm->lock); |
| 156 | |
| 157 | for (i = 0; i < atomic_read(&kvm->online_vcpus); i++) |
| 158 | kvm->vcpus[i] = NULL; |
| 159 | |
| 160 | atomic_set(&kvm->online_vcpus, 0); |
| 161 | |
| 162 | mutex_unlock(&kvm->lock); |
| 163 | } |
| 164 | |
| 165 | void kvm_arch_sync_events(struct kvm *kvm) |
| 166 | { |
| 167 | } |
| 168 | |
| 169 | static void kvm_mips_uninit_tlbs(void *arg) |
| 170 | { |
| 171 | /* Restore wired count */ |
| 172 | write_c0_wired(0); |
| 173 | mtc0_tlbw_hazard(); |
| 174 | /* Clear out all the TLBs */ |
| 175 | kvm_local_flush_tlb_all(); |
| 176 | } |
| 177 | |
| 178 | void kvm_arch_destroy_vm(struct kvm *kvm) |
| 179 | { |
| 180 | kvm_mips_free_vcpus(kvm); |
| 181 | |
| 182 | /* If this is the last instance, restore wired count */ |
| 183 | if (atomic_dec_return(&kvm_mips_instance) == 0) { |
| 184 | kvm_info("%s: last KVM instance, restoring TLB parameters\n", |
| 185 | __func__); |
| 186 | on_each_cpu(kvm_mips_uninit_tlbs, NULL, 1); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | long |
| 191 | kvm_arch_dev_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg) |
| 192 | { |
David Daney | ed82985 | 2013-05-23 09:49:10 -0700 | [diff] [blame] | 193 | return -ENOIOCTLCMD; |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 194 | } |
| 195 | |
Aneesh Kumar K.V | 5587027 | 2013-10-07 22:18:00 +0530 | [diff] [blame] | 196 | void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free, |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 197 | struct kvm_memory_slot *dont) |
| 198 | { |
| 199 | } |
| 200 | |
Aneesh Kumar K.V | 5587027 | 2013-10-07 22:18:00 +0530 | [diff] [blame] | 201 | int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot, |
| 202 | unsigned long npages) |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 203 | { |
| 204 | return 0; |
| 205 | } |
| 206 | |
Takuya Yoshikawa | e59dbe0 | 2013-07-04 13:40:29 +0900 | [diff] [blame] | 207 | void kvm_arch_memslots_updated(struct kvm *kvm) |
| 208 | { |
| 209 | } |
| 210 | |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 211 | int kvm_arch_prepare_memory_region(struct kvm *kvm, |
Linus Torvalds | daf799c | 2013-05-10 07:48:05 -0700 | [diff] [blame] | 212 | struct kvm_memory_slot *memslot, |
| 213 | struct kvm_userspace_memory_region *mem, |
| 214 | enum kvm_mr_change change) |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 215 | { |
| 216 | return 0; |
| 217 | } |
| 218 | |
| 219 | void kvm_arch_commit_memory_region(struct kvm *kvm, |
Linus Torvalds | daf799c | 2013-05-10 07:48:05 -0700 | [diff] [blame] | 220 | struct kvm_userspace_memory_region *mem, |
| 221 | const struct kvm_memory_slot *old, |
| 222 | enum kvm_mr_change change) |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 223 | { |
| 224 | unsigned long npages = 0; |
| 225 | int i, err = 0; |
| 226 | |
| 227 | kvm_debug("%s: kvm: %p slot: %d, GPA: %llx, size: %llx, QVA: %llx\n", |
| 228 | __func__, kvm, mem->slot, mem->guest_phys_addr, |
| 229 | mem->memory_size, mem->userspace_addr); |
| 230 | |
| 231 | /* Setup Guest PMAP table */ |
| 232 | if (!kvm->arch.guest_pmap) { |
| 233 | if (mem->slot == 0) |
| 234 | npages = mem->memory_size >> PAGE_SHIFT; |
| 235 | |
| 236 | if (npages) { |
| 237 | kvm->arch.guest_pmap_npages = npages; |
| 238 | kvm->arch.guest_pmap = |
| 239 | kzalloc(npages * sizeof(unsigned long), GFP_KERNEL); |
| 240 | |
| 241 | if (!kvm->arch.guest_pmap) { |
| 242 | kvm_err("Failed to allocate guest PMAP"); |
| 243 | err = -ENOMEM; |
| 244 | goto out; |
| 245 | } |
| 246 | |
| 247 | kvm_info |
| 248 | ("Allocated space for Guest PMAP Table (%ld pages) @ %p\n", |
| 249 | npages, kvm->arch.guest_pmap); |
| 250 | |
| 251 | /* Now setup the page table */ |
| 252 | for (i = 0; i < npages; i++) { |
| 253 | kvm->arch.guest_pmap[i] = KVM_INVALID_PAGE; |
| 254 | } |
| 255 | } |
| 256 | } |
| 257 | out: |
| 258 | return; |
| 259 | } |
| 260 | |
| 261 | void kvm_arch_flush_shadow_all(struct kvm *kvm) |
| 262 | { |
| 263 | } |
| 264 | |
| 265 | void kvm_arch_flush_shadow_memslot(struct kvm *kvm, |
| 266 | struct kvm_memory_slot *slot) |
| 267 | { |
| 268 | } |
| 269 | |
| 270 | void kvm_arch_flush_shadow(struct kvm *kvm) |
| 271 | { |
| 272 | } |
| 273 | |
| 274 | struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm, unsigned int id) |
| 275 | { |
| 276 | extern char mips32_exception[], mips32_exceptionEnd[]; |
| 277 | extern char mips32_GuestException[], mips32_GuestExceptionEnd[]; |
| 278 | int err, size, offset; |
| 279 | void *gebase; |
| 280 | int i; |
| 281 | |
| 282 | struct kvm_vcpu *vcpu = kzalloc(sizeof(struct kvm_vcpu), GFP_KERNEL); |
| 283 | |
| 284 | if (!vcpu) { |
| 285 | err = -ENOMEM; |
| 286 | goto out; |
| 287 | } |
| 288 | |
| 289 | err = kvm_vcpu_init(vcpu, kvm, id); |
| 290 | |
| 291 | if (err) |
| 292 | goto out_free_cpu; |
| 293 | |
| 294 | kvm_info("kvm @ %p: create cpu %d at %p\n", kvm, id, vcpu); |
| 295 | |
| 296 | /* Allocate space for host mode exception handlers that handle |
| 297 | * guest mode exits |
| 298 | */ |
| 299 | if (cpu_has_veic || cpu_has_vint) { |
| 300 | size = 0x200 + VECTORSPACING * 64; |
| 301 | } else { |
James Hogan | 7006e2d | 2014-05-29 10:16:23 +0100 | [diff] [blame] | 302 | size = 0x4000; |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | /* Save Linux EBASE */ |
| 306 | vcpu->arch.host_ebase = (void *)read_c0_ebase(); |
| 307 | |
| 308 | gebase = kzalloc(ALIGN(size, PAGE_SIZE), GFP_KERNEL); |
| 309 | |
| 310 | if (!gebase) { |
| 311 | err = -ENOMEM; |
| 312 | goto out_free_cpu; |
| 313 | } |
| 314 | kvm_info("Allocated %d bytes for KVM Exception Handlers @ %p\n", |
| 315 | ALIGN(size, PAGE_SIZE), gebase); |
| 316 | |
| 317 | /* Save new ebase */ |
| 318 | vcpu->arch.guest_ebase = gebase; |
| 319 | |
| 320 | /* Copy L1 Guest Exception handler to correct offset */ |
| 321 | |
| 322 | /* TLB Refill, EXL = 0 */ |
| 323 | memcpy(gebase, mips32_exception, |
| 324 | mips32_exceptionEnd - mips32_exception); |
| 325 | |
| 326 | /* General Exception Entry point */ |
| 327 | memcpy(gebase + 0x180, mips32_exception, |
| 328 | mips32_exceptionEnd - mips32_exception); |
| 329 | |
| 330 | /* For vectored interrupts poke the exception code @ all offsets 0-7 */ |
| 331 | for (i = 0; i < 8; i++) { |
| 332 | kvm_debug("L1 Vectored handler @ %p\n", |
| 333 | gebase + 0x200 + (i * VECTORSPACING)); |
| 334 | memcpy(gebase + 0x200 + (i * VECTORSPACING), mips32_exception, |
| 335 | mips32_exceptionEnd - mips32_exception); |
| 336 | } |
| 337 | |
| 338 | /* General handler, relocate to unmapped space for sanity's sake */ |
| 339 | offset = 0x2000; |
| 340 | kvm_info("Installing KVM Exception handlers @ %p, %#x bytes\n", |
| 341 | gebase + offset, |
| 342 | mips32_GuestExceptionEnd - mips32_GuestException); |
| 343 | |
| 344 | memcpy(gebase + offset, mips32_GuestException, |
| 345 | mips32_GuestExceptionEnd - mips32_GuestException); |
| 346 | |
| 347 | /* Invalidate the icache for these ranges */ |
James Hogan | facaaec | 2014-05-29 10:16:25 +0100 | [diff] [blame] | 348 | local_flush_icache_range((unsigned long)gebase, |
| 349 | (unsigned long)gebase + ALIGN(size, PAGE_SIZE)); |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 350 | |
| 351 | /* Allocate comm page for guest kernel, a TLB will be reserved for mapping GVA @ 0xFFFF8000 to this page */ |
| 352 | vcpu->arch.kseg0_commpage = kzalloc(PAGE_SIZE << 1, GFP_KERNEL); |
| 353 | |
| 354 | if (!vcpu->arch.kseg0_commpage) { |
| 355 | err = -ENOMEM; |
| 356 | goto out_free_gebase; |
| 357 | } |
| 358 | |
| 359 | kvm_info("Allocated COMM page @ %p\n", vcpu->arch.kseg0_commpage); |
| 360 | kvm_mips_commpage_init(vcpu); |
| 361 | |
| 362 | /* Init */ |
| 363 | vcpu->arch.last_sched_cpu = -1; |
| 364 | |
| 365 | /* Start off the timer */ |
| 366 | kvm_mips_emulate_count(vcpu); |
| 367 | |
| 368 | return vcpu; |
| 369 | |
| 370 | out_free_gebase: |
| 371 | kfree(gebase); |
| 372 | |
| 373 | out_free_cpu: |
| 374 | kfree(vcpu); |
| 375 | |
| 376 | out: |
| 377 | return ERR_PTR(err); |
| 378 | } |
| 379 | |
| 380 | void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu) |
| 381 | { |
| 382 | hrtimer_cancel(&vcpu->arch.comparecount_timer); |
| 383 | |
| 384 | kvm_vcpu_uninit(vcpu); |
| 385 | |
| 386 | kvm_mips_dump_stats(vcpu); |
| 387 | |
| 388 | if (vcpu->arch.guest_ebase) |
| 389 | kfree(vcpu->arch.guest_ebase); |
| 390 | |
| 391 | if (vcpu->arch.kseg0_commpage) |
| 392 | kfree(vcpu->arch.kseg0_commpage); |
| 393 | |
| 394 | } |
| 395 | |
| 396 | void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu) |
| 397 | { |
| 398 | kvm_arch_vcpu_free(vcpu); |
| 399 | } |
| 400 | |
| 401 | int |
| 402 | kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu, |
| 403 | struct kvm_guest_debug *dbg) |
| 404 | { |
David Daney | ed82985 | 2013-05-23 09:49:10 -0700 | [diff] [blame] | 405 | return -ENOIOCTLCMD; |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *run) |
| 409 | { |
| 410 | int r = 0; |
| 411 | sigset_t sigsaved; |
| 412 | |
| 413 | if (vcpu->sigset_active) |
| 414 | sigprocmask(SIG_SETMASK, &vcpu->sigset, &sigsaved); |
| 415 | |
| 416 | if (vcpu->mmio_needed) { |
| 417 | if (!vcpu->mmio_is_write) |
| 418 | kvm_mips_complete_mmio_load(vcpu, run); |
| 419 | vcpu->mmio_needed = 0; |
| 420 | } |
| 421 | |
| 422 | /* Check if we have any exceptions/interrupts pending */ |
| 423 | kvm_mips_deliver_interrupts(vcpu, |
| 424 | kvm_read_c0_guest_cause(vcpu->arch.cop0)); |
| 425 | |
| 426 | local_irq_disable(); |
| 427 | kvm_guest_enter(); |
| 428 | |
| 429 | r = __kvm_mips_vcpu_run(run, vcpu); |
| 430 | |
| 431 | kvm_guest_exit(); |
| 432 | local_irq_enable(); |
| 433 | |
| 434 | if (vcpu->sigset_active) |
| 435 | sigprocmask(SIG_SETMASK, &sigsaved, NULL); |
| 436 | |
| 437 | return r; |
| 438 | } |
| 439 | |
| 440 | int |
| 441 | kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm_mips_interrupt *irq) |
| 442 | { |
| 443 | int intr = (int)irq->irq; |
| 444 | struct kvm_vcpu *dvcpu = NULL; |
| 445 | |
| 446 | if (intr == 3 || intr == -3 || intr == 4 || intr == -4) |
| 447 | kvm_debug("%s: CPU: %d, INTR: %d\n", __func__, irq->cpu, |
| 448 | (int)intr); |
| 449 | |
| 450 | if (irq->cpu == -1) |
| 451 | dvcpu = vcpu; |
| 452 | else |
| 453 | dvcpu = vcpu->kvm->vcpus[irq->cpu]; |
| 454 | |
| 455 | if (intr == 2 || intr == 3 || intr == 4) { |
| 456 | kvm_mips_callbacks->queue_io_int(dvcpu, irq); |
| 457 | |
| 458 | } else if (intr == -2 || intr == -3 || intr == -4) { |
| 459 | kvm_mips_callbacks->dequeue_io_int(dvcpu, irq); |
| 460 | } else { |
| 461 | kvm_err("%s: invalid interrupt ioctl (%d:%d)\n", __func__, |
| 462 | irq->cpu, irq->irq); |
| 463 | return -EINVAL; |
| 464 | } |
| 465 | |
| 466 | dvcpu->arch.wait = 0; |
| 467 | |
| 468 | if (waitqueue_active(&dvcpu->wq)) { |
| 469 | wake_up_interruptible(&dvcpu->wq); |
| 470 | } |
| 471 | |
| 472 | return 0; |
| 473 | } |
| 474 | |
| 475 | int |
| 476 | kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu, |
| 477 | struct kvm_mp_state *mp_state) |
| 478 | { |
David Daney | ed82985 | 2013-05-23 09:49:10 -0700 | [diff] [blame] | 479 | return -ENOIOCTLCMD; |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | int |
| 483 | kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, |
| 484 | struct kvm_mp_state *mp_state) |
| 485 | { |
David Daney | ed82985 | 2013-05-23 09:49:10 -0700 | [diff] [blame] | 486 | return -ENOIOCTLCMD; |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 487 | } |
| 488 | |
David Daney | 4c73fb2 | 2013-05-23 09:49:09 -0700 | [diff] [blame] | 489 | static u64 kvm_mips_get_one_regs[] = { |
| 490 | KVM_REG_MIPS_R0, |
| 491 | KVM_REG_MIPS_R1, |
| 492 | KVM_REG_MIPS_R2, |
| 493 | KVM_REG_MIPS_R3, |
| 494 | KVM_REG_MIPS_R4, |
| 495 | KVM_REG_MIPS_R5, |
| 496 | KVM_REG_MIPS_R6, |
| 497 | KVM_REG_MIPS_R7, |
| 498 | KVM_REG_MIPS_R8, |
| 499 | KVM_REG_MIPS_R9, |
| 500 | KVM_REG_MIPS_R10, |
| 501 | KVM_REG_MIPS_R11, |
| 502 | KVM_REG_MIPS_R12, |
| 503 | KVM_REG_MIPS_R13, |
| 504 | KVM_REG_MIPS_R14, |
| 505 | KVM_REG_MIPS_R15, |
| 506 | KVM_REG_MIPS_R16, |
| 507 | KVM_REG_MIPS_R17, |
| 508 | KVM_REG_MIPS_R18, |
| 509 | KVM_REG_MIPS_R19, |
| 510 | KVM_REG_MIPS_R20, |
| 511 | KVM_REG_MIPS_R21, |
| 512 | KVM_REG_MIPS_R22, |
| 513 | KVM_REG_MIPS_R23, |
| 514 | KVM_REG_MIPS_R24, |
| 515 | KVM_REG_MIPS_R25, |
| 516 | KVM_REG_MIPS_R26, |
| 517 | KVM_REG_MIPS_R27, |
| 518 | KVM_REG_MIPS_R28, |
| 519 | KVM_REG_MIPS_R29, |
| 520 | KVM_REG_MIPS_R30, |
| 521 | KVM_REG_MIPS_R31, |
| 522 | |
| 523 | KVM_REG_MIPS_HI, |
| 524 | KVM_REG_MIPS_LO, |
| 525 | KVM_REG_MIPS_PC, |
| 526 | |
| 527 | KVM_REG_MIPS_CP0_INDEX, |
| 528 | KVM_REG_MIPS_CP0_CONTEXT, |
James Hogan | 7767b7d | 2014-05-29 10:16:30 +0100 | [diff] [blame] | 529 | KVM_REG_MIPS_CP0_USERLOCAL, |
David Daney | 4c73fb2 | 2013-05-23 09:49:09 -0700 | [diff] [blame] | 530 | KVM_REG_MIPS_CP0_PAGEMASK, |
| 531 | KVM_REG_MIPS_CP0_WIRED, |
James Hogan | 16fd5c1 | 2014-05-29 10:16:31 +0100 | [diff] [blame^] | 532 | KVM_REG_MIPS_CP0_HWRENA, |
David Daney | 4c73fb2 | 2013-05-23 09:49:09 -0700 | [diff] [blame] | 533 | KVM_REG_MIPS_CP0_BADVADDR, |
James Hogan | f8be02d | 2014-05-29 10:16:29 +0100 | [diff] [blame] | 534 | KVM_REG_MIPS_CP0_COUNT, |
David Daney | 4c73fb2 | 2013-05-23 09:49:09 -0700 | [diff] [blame] | 535 | KVM_REG_MIPS_CP0_ENTRYHI, |
James Hogan | f8be02d | 2014-05-29 10:16:29 +0100 | [diff] [blame] | 536 | KVM_REG_MIPS_CP0_COMPARE, |
David Daney | 4c73fb2 | 2013-05-23 09:49:09 -0700 | [diff] [blame] | 537 | KVM_REG_MIPS_CP0_STATUS, |
| 538 | KVM_REG_MIPS_CP0_CAUSE, |
James Hogan | fb6df0c | 2014-05-29 10:16:27 +0100 | [diff] [blame] | 539 | KVM_REG_MIPS_CP0_EPC, |
David Daney | 4c73fb2 | 2013-05-23 09:49:09 -0700 | [diff] [blame] | 540 | KVM_REG_MIPS_CP0_CONFIG, |
| 541 | KVM_REG_MIPS_CP0_CONFIG1, |
| 542 | KVM_REG_MIPS_CP0_CONFIG2, |
| 543 | KVM_REG_MIPS_CP0_CONFIG3, |
| 544 | KVM_REG_MIPS_CP0_CONFIG7, |
| 545 | KVM_REG_MIPS_CP0_ERROREPC |
| 546 | }; |
| 547 | |
| 548 | static int kvm_mips_get_reg(struct kvm_vcpu *vcpu, |
| 549 | const struct kvm_one_reg *reg) |
| 550 | { |
David Daney | 4c73fb2 | 2013-05-23 09:49:09 -0700 | [diff] [blame] | 551 | struct mips_coproc *cop0 = vcpu->arch.cop0; |
James Hogan | f8be02d | 2014-05-29 10:16:29 +0100 | [diff] [blame] | 552 | int ret; |
David Daney | 4c73fb2 | 2013-05-23 09:49:09 -0700 | [diff] [blame] | 553 | s64 v; |
| 554 | |
| 555 | switch (reg->id) { |
| 556 | case KVM_REG_MIPS_R0 ... KVM_REG_MIPS_R31: |
| 557 | v = (long)vcpu->arch.gprs[reg->id - KVM_REG_MIPS_R0]; |
| 558 | break; |
| 559 | case KVM_REG_MIPS_HI: |
| 560 | v = (long)vcpu->arch.hi; |
| 561 | break; |
| 562 | case KVM_REG_MIPS_LO: |
| 563 | v = (long)vcpu->arch.lo; |
| 564 | break; |
| 565 | case KVM_REG_MIPS_PC: |
| 566 | v = (long)vcpu->arch.pc; |
| 567 | break; |
| 568 | |
| 569 | case KVM_REG_MIPS_CP0_INDEX: |
| 570 | v = (long)kvm_read_c0_guest_index(cop0); |
| 571 | break; |
| 572 | case KVM_REG_MIPS_CP0_CONTEXT: |
| 573 | v = (long)kvm_read_c0_guest_context(cop0); |
| 574 | break; |
James Hogan | 7767b7d | 2014-05-29 10:16:30 +0100 | [diff] [blame] | 575 | case KVM_REG_MIPS_CP0_USERLOCAL: |
| 576 | v = (long)kvm_read_c0_guest_userlocal(cop0); |
| 577 | break; |
David Daney | 4c73fb2 | 2013-05-23 09:49:09 -0700 | [diff] [blame] | 578 | case KVM_REG_MIPS_CP0_PAGEMASK: |
| 579 | v = (long)kvm_read_c0_guest_pagemask(cop0); |
| 580 | break; |
| 581 | case KVM_REG_MIPS_CP0_WIRED: |
| 582 | v = (long)kvm_read_c0_guest_wired(cop0); |
| 583 | break; |
James Hogan | 16fd5c1 | 2014-05-29 10:16:31 +0100 | [diff] [blame^] | 584 | case KVM_REG_MIPS_CP0_HWRENA: |
| 585 | v = (long)kvm_read_c0_guest_hwrena(cop0); |
| 586 | break; |
David Daney | 4c73fb2 | 2013-05-23 09:49:09 -0700 | [diff] [blame] | 587 | case KVM_REG_MIPS_CP0_BADVADDR: |
| 588 | v = (long)kvm_read_c0_guest_badvaddr(cop0); |
| 589 | break; |
| 590 | case KVM_REG_MIPS_CP0_ENTRYHI: |
| 591 | v = (long)kvm_read_c0_guest_entryhi(cop0); |
| 592 | break; |
James Hogan | f8be02d | 2014-05-29 10:16:29 +0100 | [diff] [blame] | 593 | case KVM_REG_MIPS_CP0_COMPARE: |
| 594 | v = (long)kvm_read_c0_guest_compare(cop0); |
| 595 | break; |
David Daney | 4c73fb2 | 2013-05-23 09:49:09 -0700 | [diff] [blame] | 596 | case KVM_REG_MIPS_CP0_STATUS: |
| 597 | v = (long)kvm_read_c0_guest_status(cop0); |
| 598 | break; |
| 599 | case KVM_REG_MIPS_CP0_CAUSE: |
| 600 | v = (long)kvm_read_c0_guest_cause(cop0); |
| 601 | break; |
James Hogan | fb6df0c | 2014-05-29 10:16:27 +0100 | [diff] [blame] | 602 | case KVM_REG_MIPS_CP0_EPC: |
| 603 | v = (long)kvm_read_c0_guest_epc(cop0); |
| 604 | break; |
David Daney | 4c73fb2 | 2013-05-23 09:49:09 -0700 | [diff] [blame] | 605 | case KVM_REG_MIPS_CP0_ERROREPC: |
| 606 | v = (long)kvm_read_c0_guest_errorepc(cop0); |
| 607 | break; |
| 608 | case KVM_REG_MIPS_CP0_CONFIG: |
| 609 | v = (long)kvm_read_c0_guest_config(cop0); |
| 610 | break; |
| 611 | case KVM_REG_MIPS_CP0_CONFIG1: |
| 612 | v = (long)kvm_read_c0_guest_config1(cop0); |
| 613 | break; |
| 614 | case KVM_REG_MIPS_CP0_CONFIG2: |
| 615 | v = (long)kvm_read_c0_guest_config2(cop0); |
| 616 | break; |
| 617 | case KVM_REG_MIPS_CP0_CONFIG3: |
| 618 | v = (long)kvm_read_c0_guest_config3(cop0); |
| 619 | break; |
| 620 | case KVM_REG_MIPS_CP0_CONFIG7: |
| 621 | v = (long)kvm_read_c0_guest_config7(cop0); |
| 622 | break; |
James Hogan | f8be02d | 2014-05-29 10:16:29 +0100 | [diff] [blame] | 623 | /* registers to be handled specially */ |
| 624 | case KVM_REG_MIPS_CP0_COUNT: |
| 625 | ret = kvm_mips_callbacks->get_one_reg(vcpu, reg, &v); |
| 626 | if (ret) |
| 627 | return ret; |
| 628 | break; |
David Daney | 4c73fb2 | 2013-05-23 09:49:09 -0700 | [diff] [blame] | 629 | default: |
| 630 | return -EINVAL; |
| 631 | } |
David Daney | 681865d | 2013-06-10 12:33:48 -0700 | [diff] [blame] | 632 | if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U64) { |
| 633 | u64 __user *uaddr64 = (u64 __user *)(long)reg->addr; |
| 634 | return put_user(v, uaddr64); |
| 635 | } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U32) { |
| 636 | u32 __user *uaddr32 = (u32 __user *)(long)reg->addr; |
| 637 | u32 v32 = (u32)v; |
| 638 | return put_user(v32, uaddr32); |
| 639 | } else { |
| 640 | return -EINVAL; |
| 641 | } |
David Daney | 4c73fb2 | 2013-05-23 09:49:09 -0700 | [diff] [blame] | 642 | } |
| 643 | |
| 644 | static int kvm_mips_set_reg(struct kvm_vcpu *vcpu, |
| 645 | const struct kvm_one_reg *reg) |
| 646 | { |
David Daney | 4c73fb2 | 2013-05-23 09:49:09 -0700 | [diff] [blame] | 647 | struct mips_coproc *cop0 = vcpu->arch.cop0; |
| 648 | u64 v; |
| 649 | |
David Daney | 681865d | 2013-06-10 12:33:48 -0700 | [diff] [blame] | 650 | if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U64) { |
| 651 | u64 __user *uaddr64 = (u64 __user *)(long)reg->addr; |
| 652 | |
| 653 | if (get_user(v, uaddr64) != 0) |
| 654 | return -EFAULT; |
| 655 | } else if ((reg->id & KVM_REG_SIZE_MASK) == KVM_REG_SIZE_U32) { |
| 656 | u32 __user *uaddr32 = (u32 __user *)(long)reg->addr; |
| 657 | s32 v32; |
| 658 | |
| 659 | if (get_user(v32, uaddr32) != 0) |
| 660 | return -EFAULT; |
| 661 | v = (s64)v32; |
| 662 | } else { |
| 663 | return -EINVAL; |
| 664 | } |
David Daney | 4c73fb2 | 2013-05-23 09:49:09 -0700 | [diff] [blame] | 665 | |
| 666 | switch (reg->id) { |
| 667 | case KVM_REG_MIPS_R0: |
| 668 | /* Silently ignore requests to set $0 */ |
| 669 | break; |
| 670 | case KVM_REG_MIPS_R1 ... KVM_REG_MIPS_R31: |
| 671 | vcpu->arch.gprs[reg->id - KVM_REG_MIPS_R0] = v; |
| 672 | break; |
| 673 | case KVM_REG_MIPS_HI: |
| 674 | vcpu->arch.hi = v; |
| 675 | break; |
| 676 | case KVM_REG_MIPS_LO: |
| 677 | vcpu->arch.lo = v; |
| 678 | break; |
| 679 | case KVM_REG_MIPS_PC: |
| 680 | vcpu->arch.pc = v; |
| 681 | break; |
| 682 | |
| 683 | case KVM_REG_MIPS_CP0_INDEX: |
| 684 | kvm_write_c0_guest_index(cop0, v); |
| 685 | break; |
| 686 | case KVM_REG_MIPS_CP0_CONTEXT: |
| 687 | kvm_write_c0_guest_context(cop0, v); |
| 688 | break; |
James Hogan | 7767b7d | 2014-05-29 10:16:30 +0100 | [diff] [blame] | 689 | case KVM_REG_MIPS_CP0_USERLOCAL: |
| 690 | kvm_write_c0_guest_userlocal(cop0, v); |
| 691 | break; |
David Daney | 4c73fb2 | 2013-05-23 09:49:09 -0700 | [diff] [blame] | 692 | case KVM_REG_MIPS_CP0_PAGEMASK: |
| 693 | kvm_write_c0_guest_pagemask(cop0, v); |
| 694 | break; |
| 695 | case KVM_REG_MIPS_CP0_WIRED: |
| 696 | kvm_write_c0_guest_wired(cop0, v); |
| 697 | break; |
James Hogan | 16fd5c1 | 2014-05-29 10:16:31 +0100 | [diff] [blame^] | 698 | case KVM_REG_MIPS_CP0_HWRENA: |
| 699 | kvm_write_c0_guest_hwrena(cop0, v); |
| 700 | break; |
David Daney | 4c73fb2 | 2013-05-23 09:49:09 -0700 | [diff] [blame] | 701 | case KVM_REG_MIPS_CP0_BADVADDR: |
| 702 | kvm_write_c0_guest_badvaddr(cop0, v); |
| 703 | break; |
| 704 | case KVM_REG_MIPS_CP0_ENTRYHI: |
| 705 | kvm_write_c0_guest_entryhi(cop0, v); |
| 706 | break; |
| 707 | case KVM_REG_MIPS_CP0_STATUS: |
| 708 | kvm_write_c0_guest_status(cop0, v); |
| 709 | break; |
| 710 | case KVM_REG_MIPS_CP0_CAUSE: |
| 711 | kvm_write_c0_guest_cause(cop0, v); |
| 712 | break; |
James Hogan | fb6df0c | 2014-05-29 10:16:27 +0100 | [diff] [blame] | 713 | case KVM_REG_MIPS_CP0_EPC: |
| 714 | kvm_write_c0_guest_epc(cop0, v); |
| 715 | break; |
David Daney | 4c73fb2 | 2013-05-23 09:49:09 -0700 | [diff] [blame] | 716 | case KVM_REG_MIPS_CP0_ERROREPC: |
| 717 | kvm_write_c0_guest_errorepc(cop0, v); |
| 718 | break; |
James Hogan | f8be02d | 2014-05-29 10:16:29 +0100 | [diff] [blame] | 719 | /* registers to be handled specially */ |
| 720 | case KVM_REG_MIPS_CP0_COUNT: |
| 721 | case KVM_REG_MIPS_CP0_COMPARE: |
| 722 | return kvm_mips_callbacks->set_one_reg(vcpu, reg, v); |
David Daney | 4c73fb2 | 2013-05-23 09:49:09 -0700 | [diff] [blame] | 723 | default: |
| 724 | return -EINVAL; |
| 725 | } |
| 726 | return 0; |
| 727 | } |
| 728 | |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 729 | long |
| 730 | kvm_arch_vcpu_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg) |
| 731 | { |
| 732 | struct kvm_vcpu *vcpu = filp->private_data; |
| 733 | void __user *argp = (void __user *)arg; |
| 734 | long r; |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 735 | |
| 736 | switch (ioctl) { |
David Daney | 4c73fb2 | 2013-05-23 09:49:09 -0700 | [diff] [blame] | 737 | case KVM_SET_ONE_REG: |
| 738 | case KVM_GET_ONE_REG: { |
| 739 | struct kvm_one_reg reg; |
| 740 | if (copy_from_user(®, argp, sizeof(reg))) |
| 741 | return -EFAULT; |
| 742 | if (ioctl == KVM_SET_ONE_REG) |
| 743 | return kvm_mips_set_reg(vcpu, ®); |
| 744 | else |
| 745 | return kvm_mips_get_reg(vcpu, ®); |
| 746 | } |
| 747 | case KVM_GET_REG_LIST: { |
| 748 | struct kvm_reg_list __user *user_list = argp; |
| 749 | u64 __user *reg_dest; |
| 750 | struct kvm_reg_list reg_list; |
| 751 | unsigned n; |
| 752 | |
| 753 | if (copy_from_user(®_list, user_list, sizeof(reg_list))) |
| 754 | return -EFAULT; |
| 755 | n = reg_list.n; |
| 756 | reg_list.n = ARRAY_SIZE(kvm_mips_get_one_regs); |
| 757 | if (copy_to_user(user_list, ®_list, sizeof(reg_list))) |
| 758 | return -EFAULT; |
| 759 | if (n < reg_list.n) |
| 760 | return -E2BIG; |
| 761 | reg_dest = user_list->reg; |
| 762 | if (copy_to_user(reg_dest, kvm_mips_get_one_regs, |
| 763 | sizeof(kvm_mips_get_one_regs))) |
| 764 | return -EFAULT; |
| 765 | return 0; |
| 766 | } |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 767 | case KVM_NMI: |
| 768 | /* Treat the NMI as a CPU reset */ |
| 769 | r = kvm_mips_reset_vcpu(vcpu); |
| 770 | break; |
| 771 | case KVM_INTERRUPT: |
| 772 | { |
| 773 | struct kvm_mips_interrupt irq; |
| 774 | r = -EFAULT; |
| 775 | if (copy_from_user(&irq, argp, sizeof(irq))) |
| 776 | goto out; |
| 777 | |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 778 | kvm_debug("[%d] %s: irq: %d\n", vcpu->vcpu_id, __func__, |
| 779 | irq.irq); |
| 780 | |
| 781 | r = kvm_vcpu_ioctl_interrupt(vcpu, &irq); |
| 782 | break; |
| 783 | } |
| 784 | default: |
David Daney | 4c73fb2 | 2013-05-23 09:49:09 -0700 | [diff] [blame] | 785 | r = -ENOIOCTLCMD; |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 786 | } |
| 787 | |
| 788 | out: |
| 789 | return r; |
| 790 | } |
| 791 | |
| 792 | /* |
| 793 | * Get (and clear) the dirty memory log for a memory slot. |
| 794 | */ |
| 795 | int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log) |
| 796 | { |
| 797 | struct kvm_memory_slot *memslot; |
| 798 | unsigned long ga, ga_end; |
| 799 | int is_dirty = 0; |
| 800 | int r; |
| 801 | unsigned long n; |
| 802 | |
| 803 | mutex_lock(&kvm->slots_lock); |
| 804 | |
| 805 | r = kvm_get_dirty_log(kvm, log, &is_dirty); |
| 806 | if (r) |
| 807 | goto out; |
| 808 | |
| 809 | /* If nothing is dirty, don't bother messing with page tables. */ |
| 810 | if (is_dirty) { |
| 811 | memslot = &kvm->memslots->memslots[log->slot]; |
| 812 | |
| 813 | ga = memslot->base_gfn << PAGE_SHIFT; |
| 814 | ga_end = ga + (memslot->npages << PAGE_SHIFT); |
| 815 | |
| 816 | printk("%s: dirty, ga: %#lx, ga_end %#lx\n", __func__, ga, |
| 817 | ga_end); |
| 818 | |
| 819 | n = kvm_dirty_bitmap_bytes(memslot); |
| 820 | memset(memslot->dirty_bitmap, 0, n); |
| 821 | } |
| 822 | |
| 823 | r = 0; |
| 824 | out: |
| 825 | mutex_unlock(&kvm->slots_lock); |
| 826 | return r; |
| 827 | |
| 828 | } |
| 829 | |
| 830 | long kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg) |
| 831 | { |
| 832 | long r; |
| 833 | |
| 834 | switch (ioctl) { |
| 835 | default: |
David Daney | ed82985 | 2013-05-23 09:49:10 -0700 | [diff] [blame] | 836 | r = -ENOIOCTLCMD; |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 837 | } |
| 838 | |
| 839 | return r; |
| 840 | } |
| 841 | |
| 842 | int kvm_arch_init(void *opaque) |
| 843 | { |
| 844 | int ret; |
| 845 | |
| 846 | if (kvm_mips_callbacks) { |
| 847 | kvm_err("kvm: module already exists\n"); |
| 848 | return -EEXIST; |
| 849 | } |
| 850 | |
| 851 | ret = kvm_mips_emulation_init(&kvm_mips_callbacks); |
| 852 | |
| 853 | return ret; |
| 854 | } |
| 855 | |
| 856 | void kvm_arch_exit(void) |
| 857 | { |
| 858 | kvm_mips_callbacks = NULL; |
| 859 | } |
| 860 | |
| 861 | int |
| 862 | kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs) |
| 863 | { |
David Daney | ed82985 | 2013-05-23 09:49:10 -0700 | [diff] [blame] | 864 | return -ENOIOCTLCMD; |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 865 | } |
| 866 | |
| 867 | int |
| 868 | kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs) |
| 869 | { |
David Daney | ed82985 | 2013-05-23 09:49:10 -0700 | [diff] [blame] | 870 | return -ENOIOCTLCMD; |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 871 | } |
| 872 | |
| 873 | int kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu) |
| 874 | { |
| 875 | return 0; |
| 876 | } |
| 877 | |
| 878 | int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) |
| 879 | { |
David Daney | ed82985 | 2013-05-23 09:49:10 -0700 | [diff] [blame] | 880 | return -ENOIOCTLCMD; |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 881 | } |
| 882 | |
| 883 | int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) |
| 884 | { |
David Daney | ed82985 | 2013-05-23 09:49:10 -0700 | [diff] [blame] | 885 | return -ENOIOCTLCMD; |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 886 | } |
| 887 | |
| 888 | int kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf) |
| 889 | { |
| 890 | return VM_FAULT_SIGBUS; |
| 891 | } |
| 892 | |
| 893 | int kvm_dev_ioctl_check_extension(long ext) |
| 894 | { |
| 895 | int r; |
| 896 | |
| 897 | switch (ext) { |
David Daney | 4c73fb2 | 2013-05-23 09:49:09 -0700 | [diff] [blame] | 898 | case KVM_CAP_ONE_REG: |
| 899 | r = 1; |
| 900 | break; |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 901 | case KVM_CAP_COALESCED_MMIO: |
| 902 | r = KVM_COALESCED_MMIO_PAGE_OFFSET; |
| 903 | break; |
| 904 | default: |
| 905 | r = 0; |
| 906 | break; |
| 907 | } |
| 908 | return r; |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 909 | } |
| 910 | |
| 911 | int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu) |
| 912 | { |
| 913 | return kvm_mips_pending_timer(vcpu); |
| 914 | } |
| 915 | |
| 916 | int kvm_arch_vcpu_dump_regs(struct kvm_vcpu *vcpu) |
| 917 | { |
| 918 | int i; |
| 919 | struct mips_coproc *cop0; |
| 920 | |
| 921 | if (!vcpu) |
| 922 | return -1; |
| 923 | |
| 924 | printk("VCPU Register Dump:\n"); |
| 925 | printk("\tpc = 0x%08lx\n", vcpu->arch.pc);; |
| 926 | printk("\texceptions: %08lx\n", vcpu->arch.pending_exceptions); |
| 927 | |
| 928 | for (i = 0; i < 32; i += 4) { |
| 929 | printk("\tgpr%02d: %08lx %08lx %08lx %08lx\n", i, |
| 930 | vcpu->arch.gprs[i], |
| 931 | vcpu->arch.gprs[i + 1], |
| 932 | vcpu->arch.gprs[i + 2], vcpu->arch.gprs[i + 3]); |
| 933 | } |
| 934 | printk("\thi: 0x%08lx\n", vcpu->arch.hi); |
| 935 | printk("\tlo: 0x%08lx\n", vcpu->arch.lo); |
| 936 | |
| 937 | cop0 = vcpu->arch.cop0; |
| 938 | printk("\tStatus: 0x%08lx, Cause: 0x%08lx\n", |
| 939 | kvm_read_c0_guest_status(cop0), kvm_read_c0_guest_cause(cop0)); |
| 940 | |
| 941 | printk("\tEPC: 0x%08lx\n", kvm_read_c0_guest_epc(cop0)); |
| 942 | |
| 943 | return 0; |
| 944 | } |
| 945 | |
| 946 | int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) |
| 947 | { |
| 948 | int i; |
| 949 | |
David Daney | 8d17dd0 | 2013-05-23 09:49:08 -0700 | [diff] [blame] | 950 | for (i = 1; i < ARRAY_SIZE(vcpu->arch.gprs); i++) |
David Daney | bf32ebf | 2013-05-23 09:49:07 -0700 | [diff] [blame] | 951 | vcpu->arch.gprs[i] = regs->gpr[i]; |
David Daney | 8d17dd0 | 2013-05-23 09:49:08 -0700 | [diff] [blame] | 952 | vcpu->arch.gprs[0] = 0; /* zero is special, and cannot be set. */ |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 953 | vcpu->arch.hi = regs->hi; |
| 954 | vcpu->arch.lo = regs->lo; |
| 955 | vcpu->arch.pc = regs->pc; |
| 956 | |
David Daney | 4c73fb2 | 2013-05-23 09:49:09 -0700 | [diff] [blame] | 957 | return 0; |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 958 | } |
| 959 | |
| 960 | int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) |
| 961 | { |
| 962 | int i; |
| 963 | |
David Daney | 8d17dd0 | 2013-05-23 09:49:08 -0700 | [diff] [blame] | 964 | for (i = 0; i < ARRAY_SIZE(vcpu->arch.gprs); i++) |
David Daney | bf32ebf | 2013-05-23 09:49:07 -0700 | [diff] [blame] | 965 | regs->gpr[i] = vcpu->arch.gprs[i]; |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 966 | |
| 967 | regs->hi = vcpu->arch.hi; |
| 968 | regs->lo = vcpu->arch.lo; |
| 969 | regs->pc = vcpu->arch.pc; |
| 970 | |
David Daney | 4c73fb2 | 2013-05-23 09:49:09 -0700 | [diff] [blame] | 971 | return 0; |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 972 | } |
| 973 | |
| 974 | void kvm_mips_comparecount_func(unsigned long data) |
| 975 | { |
| 976 | struct kvm_vcpu *vcpu = (struct kvm_vcpu *)data; |
| 977 | |
| 978 | kvm_mips_callbacks->queue_timer_int(vcpu); |
| 979 | |
| 980 | vcpu->arch.wait = 0; |
| 981 | if (waitqueue_active(&vcpu->wq)) { |
| 982 | wake_up_interruptible(&vcpu->wq); |
| 983 | } |
| 984 | } |
| 985 | |
| 986 | /* |
| 987 | * low level hrtimer wake routine. |
| 988 | */ |
| 989 | enum hrtimer_restart kvm_mips_comparecount_wakeup(struct hrtimer *timer) |
| 990 | { |
| 991 | struct kvm_vcpu *vcpu; |
| 992 | |
| 993 | vcpu = container_of(timer, struct kvm_vcpu, arch.comparecount_timer); |
| 994 | kvm_mips_comparecount_func((unsigned long) vcpu); |
| 995 | hrtimer_forward_now(&vcpu->arch.comparecount_timer, |
| 996 | ktime_set(0, MS_TO_NS(10))); |
| 997 | return HRTIMER_RESTART; |
| 998 | } |
| 999 | |
| 1000 | int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu) |
| 1001 | { |
| 1002 | kvm_mips_callbacks->vcpu_init(vcpu); |
| 1003 | hrtimer_init(&vcpu->arch.comparecount_timer, CLOCK_MONOTONIC, |
| 1004 | HRTIMER_MODE_REL); |
| 1005 | vcpu->arch.comparecount_timer.function = kvm_mips_comparecount_wakeup; |
Sanjay Lal | 669e846 | 2012-11-21 18:34:02 -0800 | [diff] [blame] | 1006 | return 0; |
| 1007 | } |
| 1008 | |
| 1009 | void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu) |
| 1010 | { |
| 1011 | return; |
| 1012 | } |
| 1013 | |
| 1014 | int |
| 1015 | kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu, struct kvm_translation *tr) |
| 1016 | { |
| 1017 | return 0; |
| 1018 | } |
| 1019 | |
| 1020 | /* Initial guest state */ |
| 1021 | int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu) |
| 1022 | { |
| 1023 | return kvm_mips_callbacks->vcpu_setup(vcpu); |
| 1024 | } |
| 1025 | |
| 1026 | static |
| 1027 | void kvm_mips_set_c0_status(void) |
| 1028 | { |
| 1029 | uint32_t status = read_c0_status(); |
| 1030 | |
| 1031 | if (cpu_has_fpu) |
| 1032 | status |= (ST0_CU1); |
| 1033 | |
| 1034 | if (cpu_has_dsp) |
| 1035 | status |= (ST0_MX); |
| 1036 | |
| 1037 | write_c0_status(status); |
| 1038 | ehb(); |
| 1039 | } |
| 1040 | |
| 1041 | /* |
| 1042 | * Return value is in the form (errcode<<2 | RESUME_FLAG_HOST | RESUME_FLAG_NV) |
| 1043 | */ |
| 1044 | int kvm_mips_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu) |
| 1045 | { |
| 1046 | uint32_t cause = vcpu->arch.host_cp0_cause; |
| 1047 | uint32_t exccode = (cause >> CAUSEB_EXCCODE) & 0x1f; |
| 1048 | uint32_t __user *opc = (uint32_t __user *) vcpu->arch.pc; |
| 1049 | unsigned long badvaddr = vcpu->arch.host_cp0_badvaddr; |
| 1050 | enum emulation_result er = EMULATE_DONE; |
| 1051 | int ret = RESUME_GUEST; |
| 1052 | |
| 1053 | /* Set a default exit reason */ |
| 1054 | run->exit_reason = KVM_EXIT_UNKNOWN; |
| 1055 | run->ready_for_interrupt_injection = 1; |
| 1056 | |
| 1057 | /* Set the appropriate status bits based on host CPU features, before we hit the scheduler */ |
| 1058 | kvm_mips_set_c0_status(); |
| 1059 | |
| 1060 | local_irq_enable(); |
| 1061 | |
| 1062 | kvm_debug("kvm_mips_handle_exit: cause: %#x, PC: %p, kvm_run: %p, kvm_vcpu: %p\n", |
| 1063 | cause, opc, run, vcpu); |
| 1064 | |
| 1065 | /* Do a privilege check, if in UM most of these exit conditions end up |
| 1066 | * causing an exception to be delivered to the Guest Kernel |
| 1067 | */ |
| 1068 | er = kvm_mips_check_privilege(cause, opc, run, vcpu); |
| 1069 | if (er == EMULATE_PRIV_FAIL) { |
| 1070 | goto skip_emul; |
| 1071 | } else if (er == EMULATE_FAIL) { |
| 1072 | run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 1073 | ret = RESUME_HOST; |
| 1074 | goto skip_emul; |
| 1075 | } |
| 1076 | |
| 1077 | switch (exccode) { |
| 1078 | case T_INT: |
| 1079 | kvm_debug("[%d]T_INT @ %p\n", vcpu->vcpu_id, opc); |
| 1080 | |
| 1081 | ++vcpu->stat.int_exits; |
| 1082 | trace_kvm_exit(vcpu, INT_EXITS); |
| 1083 | |
| 1084 | if (need_resched()) { |
| 1085 | cond_resched(); |
| 1086 | } |
| 1087 | |
| 1088 | ret = RESUME_GUEST; |
| 1089 | break; |
| 1090 | |
| 1091 | case T_COP_UNUSABLE: |
| 1092 | kvm_debug("T_COP_UNUSABLE: @ PC: %p\n", opc); |
| 1093 | |
| 1094 | ++vcpu->stat.cop_unusable_exits; |
| 1095 | trace_kvm_exit(vcpu, COP_UNUSABLE_EXITS); |
| 1096 | ret = kvm_mips_callbacks->handle_cop_unusable(vcpu); |
| 1097 | /* XXXKYMA: Might need to return to user space */ |
| 1098 | if (run->exit_reason == KVM_EXIT_IRQ_WINDOW_OPEN) { |
| 1099 | ret = RESUME_HOST; |
| 1100 | } |
| 1101 | break; |
| 1102 | |
| 1103 | case T_TLB_MOD: |
| 1104 | ++vcpu->stat.tlbmod_exits; |
| 1105 | trace_kvm_exit(vcpu, TLBMOD_EXITS); |
| 1106 | ret = kvm_mips_callbacks->handle_tlb_mod(vcpu); |
| 1107 | break; |
| 1108 | |
| 1109 | case T_TLB_ST_MISS: |
| 1110 | kvm_debug |
| 1111 | ("TLB ST fault: cause %#x, status %#lx, PC: %p, BadVaddr: %#lx\n", |
| 1112 | cause, kvm_read_c0_guest_status(vcpu->arch.cop0), opc, |
| 1113 | badvaddr); |
| 1114 | |
| 1115 | ++vcpu->stat.tlbmiss_st_exits; |
| 1116 | trace_kvm_exit(vcpu, TLBMISS_ST_EXITS); |
| 1117 | ret = kvm_mips_callbacks->handle_tlb_st_miss(vcpu); |
| 1118 | break; |
| 1119 | |
| 1120 | case T_TLB_LD_MISS: |
| 1121 | kvm_debug("TLB LD fault: cause %#x, PC: %p, BadVaddr: %#lx\n", |
| 1122 | cause, opc, badvaddr); |
| 1123 | |
| 1124 | ++vcpu->stat.tlbmiss_ld_exits; |
| 1125 | trace_kvm_exit(vcpu, TLBMISS_LD_EXITS); |
| 1126 | ret = kvm_mips_callbacks->handle_tlb_ld_miss(vcpu); |
| 1127 | break; |
| 1128 | |
| 1129 | case T_ADDR_ERR_ST: |
| 1130 | ++vcpu->stat.addrerr_st_exits; |
| 1131 | trace_kvm_exit(vcpu, ADDRERR_ST_EXITS); |
| 1132 | ret = kvm_mips_callbacks->handle_addr_err_st(vcpu); |
| 1133 | break; |
| 1134 | |
| 1135 | case T_ADDR_ERR_LD: |
| 1136 | ++vcpu->stat.addrerr_ld_exits; |
| 1137 | trace_kvm_exit(vcpu, ADDRERR_LD_EXITS); |
| 1138 | ret = kvm_mips_callbacks->handle_addr_err_ld(vcpu); |
| 1139 | break; |
| 1140 | |
| 1141 | case T_SYSCALL: |
| 1142 | ++vcpu->stat.syscall_exits; |
| 1143 | trace_kvm_exit(vcpu, SYSCALL_EXITS); |
| 1144 | ret = kvm_mips_callbacks->handle_syscall(vcpu); |
| 1145 | break; |
| 1146 | |
| 1147 | case T_RES_INST: |
| 1148 | ++vcpu->stat.resvd_inst_exits; |
| 1149 | trace_kvm_exit(vcpu, RESVD_INST_EXITS); |
| 1150 | ret = kvm_mips_callbacks->handle_res_inst(vcpu); |
| 1151 | break; |
| 1152 | |
| 1153 | case T_BREAK: |
| 1154 | ++vcpu->stat.break_inst_exits; |
| 1155 | trace_kvm_exit(vcpu, BREAK_INST_EXITS); |
| 1156 | ret = kvm_mips_callbacks->handle_break(vcpu); |
| 1157 | break; |
| 1158 | |
| 1159 | default: |
| 1160 | kvm_err |
| 1161 | ("Exception Code: %d, not yet handled, @ PC: %p, inst: 0x%08x BadVaddr: %#lx Status: %#lx\n", |
| 1162 | exccode, opc, kvm_get_inst(opc, vcpu), badvaddr, |
| 1163 | kvm_read_c0_guest_status(vcpu->arch.cop0)); |
| 1164 | kvm_arch_vcpu_dump_regs(vcpu); |
| 1165 | run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 1166 | ret = RESUME_HOST; |
| 1167 | break; |
| 1168 | |
| 1169 | } |
| 1170 | |
| 1171 | skip_emul: |
| 1172 | local_irq_disable(); |
| 1173 | |
| 1174 | if (er == EMULATE_DONE && !(ret & RESUME_HOST)) |
| 1175 | kvm_mips_deliver_interrupts(vcpu, cause); |
| 1176 | |
| 1177 | if (!(ret & RESUME_HOST)) { |
| 1178 | /* Only check for signals if not already exiting to userspace */ |
| 1179 | if (signal_pending(current)) { |
| 1180 | run->exit_reason = KVM_EXIT_INTR; |
| 1181 | ret = (-EINTR << 2) | RESUME_HOST; |
| 1182 | ++vcpu->stat.signal_exits; |
| 1183 | trace_kvm_exit(vcpu, SIGNAL_EXITS); |
| 1184 | } |
| 1185 | } |
| 1186 | |
| 1187 | return ret; |
| 1188 | } |
| 1189 | |
| 1190 | int __init kvm_mips_init(void) |
| 1191 | { |
| 1192 | int ret; |
| 1193 | |
| 1194 | ret = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE); |
| 1195 | |
| 1196 | if (ret) |
| 1197 | return ret; |
| 1198 | |
| 1199 | /* On MIPS, kernel modules are executed from "mapped space", which requires TLBs. |
| 1200 | * The TLB handling code is statically linked with the rest of the kernel (kvm_tlb.c) |
| 1201 | * to avoid the possibility of double faulting. The issue is that the TLB code |
| 1202 | * references routines that are part of the the KVM module, |
| 1203 | * which are only available once the module is loaded. |
| 1204 | */ |
| 1205 | kvm_mips_gfn_to_pfn = gfn_to_pfn; |
| 1206 | kvm_mips_release_pfn_clean = kvm_release_pfn_clean; |
| 1207 | kvm_mips_is_error_pfn = is_error_pfn; |
| 1208 | |
| 1209 | pr_info("KVM/MIPS Initialized\n"); |
| 1210 | return 0; |
| 1211 | } |
| 1212 | |
| 1213 | void __exit kvm_mips_exit(void) |
| 1214 | { |
| 1215 | kvm_exit(); |
| 1216 | |
| 1217 | kvm_mips_gfn_to_pfn = NULL; |
| 1218 | kvm_mips_release_pfn_clean = NULL; |
| 1219 | kvm_mips_is_error_pfn = NULL; |
| 1220 | |
| 1221 | pr_info("KVM/MIPS unloaded\n"); |
| 1222 | } |
| 1223 | |
| 1224 | module_init(kvm_mips_init); |
| 1225 | module_exit(kvm_mips_exit); |
| 1226 | |
| 1227 | EXPORT_TRACEPOINT_SYMBOL(kvm_exit); |