~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/arch/mips/kvm/kvm_mips.c

Version: ~ [ linux-5.4-rc3 ] ~ [ linux-5.3.6 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.79 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.149 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.196 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.196 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.75 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp