forked from ~ljy/RK356X_SDK_RELEASE

hc
2024-05-10 cde9070d9970eef1f7ec2360586c802a16230ad8
kernel/arch/powerpc/kvm/booke.c
....@@ -1,16 +1,5 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
2
- * This program is free software; you can redistribute it and/or modify
3
- * it under the terms of the GNU General Public License, version 2, as
4
- * published by the Free Software Foundation.
5
- *
6
- * This program is distributed in the hope that it will be useful,
7
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
8
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9
- * GNU General Public License for more details.
10
- *
11
- * You should have received a copy of the GNU General Public License
12
- * along with this program; if not, write to the Free Software
13
- * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
143 *
154 * Copyright IBM Corp. 2007
165 * Copyright 2010-2011 Freescale Semiconductor, Inc.
....@@ -46,29 +35,28 @@
4635
4736 unsigned long kvmppc_booke_handlers;
4837
49
-#define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM
50
-#define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
51
-
5238 struct kvm_stats_debugfs_item debugfs_entries[] = {
53
- { "mmio", VCPU_STAT(mmio_exits) },
54
- { "sig", VCPU_STAT(signal_exits) },
55
- { "itlb_r", VCPU_STAT(itlb_real_miss_exits) },
56
- { "itlb_v", VCPU_STAT(itlb_virt_miss_exits) },
57
- { "dtlb_r", VCPU_STAT(dtlb_real_miss_exits) },
58
- { "dtlb_v", VCPU_STAT(dtlb_virt_miss_exits) },
59
- { "sysc", VCPU_STAT(syscall_exits) },
60
- { "isi", VCPU_STAT(isi_exits) },
61
- { "dsi", VCPU_STAT(dsi_exits) },
62
- { "inst_emu", VCPU_STAT(emulated_inst_exits) },
63
- { "dec", VCPU_STAT(dec_exits) },
64
- { "ext_intr", VCPU_STAT(ext_intr_exits) },
65
- { "halt_successful_poll", VCPU_STAT(halt_successful_poll) },
66
- { "halt_attempted_poll", VCPU_STAT(halt_attempted_poll) },
67
- { "halt_poll_invalid", VCPU_STAT(halt_poll_invalid) },
68
- { "halt_wakeup", VCPU_STAT(halt_wakeup) },
69
- { "doorbell", VCPU_STAT(dbell_exits) },
70
- { "guest doorbell", VCPU_STAT(gdbell_exits) },
71
- { "remote_tlb_flush", VM_STAT(remote_tlb_flush) },
39
+ VCPU_STAT("mmio", mmio_exits),
40
+ VCPU_STAT("sig", signal_exits),
41
+ VCPU_STAT("itlb_r", itlb_real_miss_exits),
42
+ VCPU_STAT("itlb_v", itlb_virt_miss_exits),
43
+ VCPU_STAT("dtlb_r", dtlb_real_miss_exits),
44
+ VCPU_STAT("dtlb_v", dtlb_virt_miss_exits),
45
+ VCPU_STAT("sysc", syscall_exits),
46
+ VCPU_STAT("isi", isi_exits),
47
+ VCPU_STAT("dsi", dsi_exits),
48
+ VCPU_STAT("inst_emu", emulated_inst_exits),
49
+ VCPU_STAT("dec", dec_exits),
50
+ VCPU_STAT("ext_intr", ext_intr_exits),
51
+ VCPU_STAT("halt_successful_poll", halt_successful_poll),
52
+ VCPU_STAT("halt_attempted_poll", halt_attempted_poll),
53
+ VCPU_STAT("halt_poll_invalid", halt_poll_invalid),
54
+ VCPU_STAT("halt_wakeup", halt_wakeup),
55
+ VCPU_STAT("doorbell", dbell_exits),
56
+ VCPU_STAT("guest doorbell", gdbell_exits),
57
+ VCPU_STAT("halt_poll_success_ns", halt_poll_success_ns),
58
+ VCPU_STAT("halt_poll_fail_ns", halt_poll_fail_ns),
59
+ VM_STAT("remote_tlb_flush", remote_tlb_flush),
7260 { NULL }
7361 };
7462
....@@ -432,11 +420,11 @@
432420 case BOOKE_IRQPRIO_DATA_STORAGE:
433421 case BOOKE_IRQPRIO_ALIGNMENT:
434422 update_dear = true;
435
- /* fall through */
423
+ fallthrough;
436424 case BOOKE_IRQPRIO_INST_STORAGE:
437425 case BOOKE_IRQPRIO_PROGRAM:
438426 update_esr = true;
439
- /* fall through */
427
+ fallthrough;
440428 case BOOKE_IRQPRIO_ITLB_MISS:
441429 case BOOKE_IRQPRIO_SYSCALL:
442430 case BOOKE_IRQPRIO_FP_UNAVAIL:
....@@ -470,7 +458,7 @@
470458 case BOOKE_IRQPRIO_DECREMENTER:
471459 case BOOKE_IRQPRIO_FIT:
472460 keep_irq = true;
473
- /* fall through */
461
+ fallthrough;
474462 case BOOKE_IRQPRIO_EXTERNAL:
475463 case BOOKE_IRQPRIO_DBELL:
476464 allowed = vcpu->arch.shared->msr & MSR_EE;
....@@ -741,13 +729,13 @@
741729 return r;
742730 }
743731
744
-int kvmppc_vcpu_run(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
732
+int kvmppc_vcpu_run(struct kvm_vcpu *vcpu)
745733 {
746734 int ret, s;
747735 struct debug_reg debug;
748736
749737 if (!vcpu->arch.sane) {
750
- kvm_run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
738
+ vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
751739 return -EINVAL;
752740 }
753741
....@@ -786,10 +774,10 @@
786774 debug = current->thread.debug;
787775 current->thread.debug = vcpu->arch.dbg_reg;
788776
789
- vcpu->arch.pgdir = current->mm->pgd;
777
+ vcpu->arch.pgdir = vcpu->kvm->mm->pgd;
790778 kvmppc_fix_ee_before_entry();
791779
792
- ret = __kvmppc_vcpu_run(kvm_run, vcpu);
780
+ ret = __kvmppc_vcpu_run(vcpu);
793781
794782 /* No need for guest_exit. It's done in handle_exit.
795783 We also get here with interrupts enabled. */
....@@ -811,11 +799,11 @@
811799 return ret;
812800 }
813801
814
-static int emulation_exit(struct kvm_run *run, struct kvm_vcpu *vcpu)
802
+static int emulation_exit(struct kvm_vcpu *vcpu)
815803 {
816804 enum emulation_result er;
817805
818
- er = kvmppc_emulate_instruction(run, vcpu);
806
+ er = kvmppc_emulate_instruction(vcpu);
819807 switch (er) {
820808 case EMULATE_DONE:
821809 /* don't overwrite subtypes, just account kvm_stats */
....@@ -832,8 +820,8 @@
832820 __func__, vcpu->arch.regs.nip, vcpu->arch.last_inst);
833821 /* For debugging, encode the failing instruction and
834822 * report it to userspace. */
835
- run->hw.hardware_exit_reason = ~0ULL << 32;
836
- run->hw.hardware_exit_reason |= vcpu->arch.last_inst;
823
+ vcpu->run->hw.hardware_exit_reason = ~0ULL << 32;
824
+ vcpu->run->hw.hardware_exit_reason |= vcpu->arch.last_inst;
837825 kvmppc_core_queue_program(vcpu, ESR_PIL);
838826 return RESUME_HOST;
839827
....@@ -845,8 +833,9 @@
845833 }
846834 }
847835
848
-static int kvmppc_handle_debug(struct kvm_run *run, struct kvm_vcpu *vcpu)
836
+static int kvmppc_handle_debug(struct kvm_vcpu *vcpu)
849837 {
838
+ struct kvm_run *run = vcpu->run;
850839 struct debug_reg *dbg_reg = &(vcpu->arch.dbg_reg);
851840 u32 dbsr = vcpu->arch.dbsr;
852841
....@@ -965,7 +954,7 @@
965954 }
966955 }
967956
968
-static int kvmppc_resume_inst_load(struct kvm_run *run, struct kvm_vcpu *vcpu,
957
+static int kvmppc_resume_inst_load(struct kvm_vcpu *vcpu,
969958 enum emulation_result emulated, u32 last_inst)
970959 {
971960 switch (emulated) {
....@@ -977,8 +966,8 @@
977966 __func__, vcpu->arch.regs.nip);
978967 /* For debugging, encode the failing instruction and
979968 * report it to userspace. */
980
- run->hw.hardware_exit_reason = ~0ULL << 32;
981
- run->hw.hardware_exit_reason |= last_inst;
969
+ vcpu->run->hw.hardware_exit_reason = ~0ULL << 32;
970
+ vcpu->run->hw.hardware_exit_reason |= last_inst;
982971 kvmppc_core_queue_program(vcpu, ESR_PIL);
983972 return RESUME_HOST;
984973
....@@ -992,9 +981,9 @@
992981 *
993982 * Return value is in the form (errcode<<2 | RESUME_FLAG_HOST | RESUME_FLAG_NV)
994983 */
995
-int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
996
- unsigned int exit_nr)
984
+int kvmppc_handle_exit(struct kvm_vcpu *vcpu, unsigned int exit_nr)
997985 {
986
+ struct kvm_run *run = vcpu->run;
998987 int r = RESUME_HOST;
999988 int s;
1000989 int idx;
....@@ -1027,7 +1016,21 @@
10271016 }
10281017
10291018 trace_kvm_exit(exit_nr, vcpu);
1030
- guest_exit_irqoff();
1019
+
1020
+ context_tracking_guest_exit();
1021
+ if (!vtime_accounting_enabled_this_cpu()) {
1022
+ local_irq_enable();
1023
+ /*
1024
+ * Service IRQs here before vtime_account_guest_exit() so any
1025
+ * ticks that occurred while running the guest are accounted to
1026
+ * the guest. If vtime accounting is enabled, accounting uses
1027
+ * TB rather than ticks, so it can be done without enabling
1028
+ * interrupts here, which has the problem that it accounts
1029
+ * interrupt processing overhead to the host.
1030
+ */
1031
+ local_irq_disable();
1032
+ }
1033
+ vtime_account_guest_exit();
10311034
10321035 local_irq_enable();
10331036
....@@ -1035,7 +1038,7 @@
10351038 run->ready_for_interrupt_injection = 1;
10361039
10371040 if (emulated != EMULATE_DONE) {
1038
- r = kvmppc_resume_inst_load(run, vcpu, emulated, last_inst);
1041
+ r = kvmppc_resume_inst_load(vcpu, emulated, last_inst);
10391042 goto out;
10401043 }
10411044
....@@ -1095,7 +1098,7 @@
10951098 break;
10961099
10971100 case BOOKE_INTERRUPT_HV_PRIV:
1098
- r = emulation_exit(run, vcpu);
1101
+ r = emulation_exit(vcpu);
10991102 break;
11001103
11011104 case BOOKE_INTERRUPT_PROGRAM:
....@@ -1105,7 +1108,7 @@
11051108 * We are here because of an SW breakpoint instr,
11061109 * so lets return to host to handle.
11071110 */
1108
- r = kvmppc_handle_debug(run, vcpu);
1111
+ r = kvmppc_handle_debug(vcpu);
11091112 run->exit_reason = KVM_EXIT_DEBUG;
11101113 kvmppc_account_exit(vcpu, DEBUG_EXITS);
11111114 break;
....@@ -1126,7 +1129,7 @@
11261129 break;
11271130 }
11281131
1129
- r = emulation_exit(run, vcpu);
1132
+ r = emulation_exit(vcpu);
11301133 break;
11311134
11321135 case BOOKE_INTERRUPT_FP_UNAVAIL:
....@@ -1293,7 +1296,7 @@
12931296 * actually RAM. */
12941297 vcpu->arch.paddr_accessed = gpaddr;
12951298 vcpu->arch.vaddr_accessed = eaddr;
1296
- r = kvmppc_emulate_mmio(run, vcpu);
1299
+ r = kvmppc_emulate_mmio(vcpu);
12971300 kvmppc_account_exit(vcpu, MMIO_EXITS);
12981301 }
12991302
....@@ -1344,7 +1347,7 @@
13441347 }
13451348
13461349 case BOOKE_INTERRUPT_DEBUG: {
1347
- r = kvmppc_handle_debug(run, vcpu);
1350
+ r = kvmppc_handle_debug(vcpu);
13481351 if (r == RESUME_HOST)
13491352 run->exit_reason = KVM_EXIT_DEBUG;
13501353 kvmppc_account_exit(vcpu, DEBUG_EXITS);
....@@ -1386,36 +1389,6 @@
13861389 arm_next_watchdog(vcpu);
13871390
13881391 update_timer_ints(vcpu);
1389
-}
1390
-
1391
-/* Initial guest state: 16MB mapping 0 -> 0, PC = 0, MSR = 0, R1 = 16MB */
1392
-int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
1393
-{
1394
- int i;
1395
- int r;
1396
-
1397
- vcpu->arch.regs.nip = 0;
1398
- vcpu->arch.shared->pir = vcpu->vcpu_id;
1399
- kvmppc_set_gpr(vcpu, 1, (16<<20) - 8); /* -8 for the callee-save LR slot */
1400
- kvmppc_set_msr(vcpu, 0);
1401
-
1402
-#ifndef CONFIG_KVM_BOOKE_HV
1403
- vcpu->arch.shadow_msr = MSR_USER | MSR_IS | MSR_DS;
1404
- vcpu->arch.shadow_pid = 1;
1405
- vcpu->arch.shared->msr = 0;
1406
-#endif
1407
-
1408
- /* Eye-catching numbers so we know if the guest takes an interrupt
1409
- * before it's programmed its own IVPR/IVORs. */
1410
- vcpu->arch.ivpr = 0x55550000;
1411
- for (i = 0; i < BOOKE_IRQPRIO_MAX; i++)
1412
- vcpu->arch.ivor[i] = 0x7700 | i * 4;
1413
-
1414
- kvmppc_init_timing_stats(vcpu);
1415
-
1416
- r = kvmppc_core_vcpu_setup(vcpu);
1417
- kvmppc_sanity_check(vcpu);
1418
- return r;
14191392 }
14201393
14211394 int kvmppc_subarch_vcpu_init(struct kvm_vcpu *vcpu)
....@@ -1788,12 +1761,12 @@
17881761
17891762 int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
17901763 {
1791
- return -ENOTSUPP;
1764
+ return -EOPNOTSUPP;
17921765 }
17931766
17941767 int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
17951768 {
1796
- return -ENOTSUPP;
1769
+ return -EOPNOTSUPP;
17971770 }
17981771
17991772 int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
....@@ -1807,25 +1780,24 @@
18071780 return r;
18081781 }
18091782
1783
+void kvm_arch_sync_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot)
1784
+{
1785
+
1786
+}
1787
+
18101788 int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
18111789 {
1812
- return -ENOTSUPP;
1790
+ return -EOPNOTSUPP;
18131791 }
18141792
1815
-void kvmppc_core_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free,
1816
- struct kvm_memory_slot *dont)
1793
+void kvmppc_core_free_memslot(struct kvm *kvm, struct kvm_memory_slot *slot)
18171794 {
1818
-}
1819
-
1820
-int kvmppc_core_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
1821
- unsigned long npages)
1822
-{
1823
- return 0;
18241795 }
18251796
18261797 int kvmppc_core_prepare_memory_region(struct kvm *kvm,
18271798 struct kvm_memory_slot *memslot,
1828
- const struct kvm_userspace_memory_region *mem)
1799
+ const struct kvm_userspace_memory_region *mem,
1800
+ enum kvm_mr_change change)
18291801 {
18301802 return 0;
18311803 }
....@@ -1833,7 +1805,8 @@
18331805 void kvmppc_core_commit_memory_region(struct kvm *kvm,
18341806 const struct kvm_userspace_memory_region *mem,
18351807 const struct kvm_memory_slot *old,
1836
- const struct kvm_memory_slot *new)
1808
+ const struct kvm_memory_slot *new,
1809
+ enum kvm_mr_change change)
18371810 {
18381811 }
18391812
....@@ -2114,19 +2087,45 @@
21142087 kvmppc_clear_dbsr();
21152088 }
21162089
2117
-void kvmppc_mmu_destroy(struct kvm_vcpu *vcpu)
2118
-{
2119
- vcpu->kvm->arch.kvm_ops->mmu_destroy(vcpu);
2120
-}
2121
-
21222090 int kvmppc_core_init_vm(struct kvm *kvm)
21232091 {
21242092 return kvm->arch.kvm_ops->init_vm(kvm);
21252093 }
21262094
2127
-struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
2095
+int kvmppc_core_vcpu_create(struct kvm_vcpu *vcpu)
21282096 {
2129
- return kvm->arch.kvm_ops->vcpu_create(kvm, id);
2097
+ int i;
2098
+ int r;
2099
+
2100
+ r = vcpu->kvm->arch.kvm_ops->vcpu_create(vcpu);
2101
+ if (r)
2102
+ return r;
2103
+
2104
+ /* Initial guest state: 16MB mapping 0 -> 0, PC = 0, MSR = 0, R1 = 16MB */
2105
+ vcpu->arch.regs.nip = 0;
2106
+ vcpu->arch.shared->pir = vcpu->vcpu_id;
2107
+ kvmppc_set_gpr(vcpu, 1, (16<<20) - 8); /* -8 for the callee-save LR slot */
2108
+ kvmppc_set_msr(vcpu, 0);
2109
+
2110
+#ifndef CONFIG_KVM_BOOKE_HV
2111
+ vcpu->arch.shadow_msr = MSR_USER | MSR_IS | MSR_DS;
2112
+ vcpu->arch.shadow_pid = 1;
2113
+ vcpu->arch.shared->msr = 0;
2114
+#endif
2115
+
2116
+ /* Eye-catching numbers so we know if the guest takes an interrupt
2117
+ * before it's programmed its own IVPR/IVORs. */
2118
+ vcpu->arch.ivpr = 0x55550000;
2119
+ for (i = 0; i < BOOKE_IRQPRIO_MAX; i++)
2120
+ vcpu->arch.ivor[i] = 0x7700 | i * 4;
2121
+
2122
+ kvmppc_init_timing_stats(vcpu);
2123
+
2124
+ r = kvmppc_core_vcpu_setup(vcpu);
2125
+ if (r)
2126
+ vcpu->kvm->arch.kvm_ops->vcpu_free(vcpu);
2127
+ kvmppc_sanity_check(vcpu);
2128
+ return r;
21302129 }
21312130
21322131 void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)