blob: 6a2826f1bf5e99532a08969be845022a2c8e2f76 [file] [log] [blame]
Thomas Gleixnerd94d71c2019-05-29 07:12:40 -07001// SPDX-License-Identifier: GPL-2.0-only
Christoffer Dall45e96ea2013-01-20 18:43:58 -05002/*
3 * Copyright (C) 2012 - Virtual Open Systems and Columbia University
4 * Author: Christoffer Dall <c.dall@virtualopensystems.com>
Christoffer Dall45e96ea2013-01-20 18:43:58 -05005 */
6
7#include <linux/kvm_host.h>
Christoffer Dall45e96ea2013-01-20 18:43:58 -05008#include <asm/kvm_emulate.h>
9#include <trace/events/kvm.h>
10
11#include "trace.h"
12
Christoffer Dalld5a5a0e2016-04-24 21:41:36 +020013void kvm_mmio_write_buf(void *buf, unsigned int len, unsigned long data)
Marc Zyngier6d89d2d2013-02-12 12:40:22 +000014{
15 void *datap = NULL;
16 union {
17 u8 byte;
18 u16 hword;
19 u32 word;
20 u64 dword;
21 } tmp;
22
23 switch (len) {
24 case 1:
25 tmp.byte = data;
26 datap = &tmp.byte;
27 break;
28 case 2:
29 tmp.hword = data;
30 datap = &tmp.hword;
31 break;
32 case 4:
33 tmp.word = data;
34 datap = &tmp.word;
35 break;
36 case 8:
37 tmp.dword = data;
38 datap = &tmp.dword;
39 break;
40 }
41
42 memcpy(buf, datap, len);
43}
44
Christoffer Dalld5a5a0e2016-04-24 21:41:36 +020045unsigned long kvm_mmio_read_buf(const void *buf, unsigned int len)
Marc Zyngier6d89d2d2013-02-12 12:40:22 +000046{
47 unsigned long data = 0;
48 union {
49 u16 hword;
50 u32 word;
51 u64 dword;
52 } tmp;
53
54 switch (len) {
55 case 1:
Christoffer Dalld5a5a0e2016-04-24 21:41:36 +020056 data = *(u8 *)buf;
Marc Zyngier6d89d2d2013-02-12 12:40:22 +000057 break;
58 case 2:
59 memcpy(&tmp.hword, buf, len);
60 data = tmp.hword;
61 break;
62 case 4:
63 memcpy(&tmp.word, buf, len);
64 data = tmp.word;
65 break;
66 case 8:
67 memcpy(&tmp.dword, buf, len);
68 data = tmp.dword;
69 break;
70 }
71
72 return data;
73}
74
Christoffer Dall45e96ea2013-01-20 18:43:58 -050075/**
76 * kvm_handle_mmio_return -- Handle MMIO loads after user space emulation
Christoffer Dall83091db2016-03-29 14:29:28 +020077 * or in-kernel IO emulation
78 *
Christoffer Dall45e96ea2013-01-20 18:43:58 -050079 * @vcpu: The VCPU pointer
Christoffer Dall45e96ea2013-01-20 18:43:58 -050080 */
Tianjia Zhang74cc7e02020-06-23 21:14:15 +080081int kvm_handle_mmio_return(struct kvm_vcpu *vcpu)
Christoffer Dall45e96ea2013-01-20 18:43:58 -050082{
Marc Zyngier6d89d2d2013-02-12 12:40:22 +000083 unsigned long data;
Christoffer Dall45e96ea2013-01-20 18:43:58 -050084 unsigned int len;
85 int mask;
86
Andrew Jones2113c5f2019-08-22 13:03:05 +020087 /* Detect an already handled MMIO return */
88 if (unlikely(!vcpu->mmio_needed))
89 return 0;
90
91 vcpu->mmio_needed = 0;
92
Marc Zyngier0e20f5e2019-12-13 13:25:25 +000093 if (!kvm_vcpu_dabt_iswrite(vcpu)) {
Tianjia Zhang74cc7e02020-06-23 21:14:15 +080094 struct kvm_run *run = vcpu->run;
95
Marc Zyngier0e20f5e2019-12-13 13:25:25 +000096 len = kvm_vcpu_dabt_get_as(vcpu);
Christoffer Dalld5a5a0e2016-04-24 21:41:36 +020097 data = kvm_mmio_read_buf(run->mmio.data, len);
Christoffer Dall45e96ea2013-01-20 18:43:58 -050098
Marc Zyngier0e20f5e2019-12-13 13:25:25 +000099 if (kvm_vcpu_dabt_issext(vcpu) &&
Marc Zyngierf42798c2013-03-05 02:43:23 +0000100 len < sizeof(unsigned long)) {
Christoffer Dall45e96ea2013-01-20 18:43:58 -0500101 mask = 1U << ((len * 8) - 1);
Marc Zyngier6d89d2d2013-02-12 12:40:22 +0000102 data = (data ^ mask) - mask;
Christoffer Dall45e96ea2013-01-20 18:43:58 -0500103 }
Marc Zyngier6d89d2d2013-02-12 12:40:22 +0000104
Marc Zyngier0e20f5e2019-12-13 13:25:25 +0000105 if (!kvm_vcpu_dabt_issf(vcpu))
Christoffer Dallb6ae2562019-12-12 20:50:55 +0100106 data = data & 0xffffffff;
107
Marc Zyngier6d89d2d2013-02-12 12:40:22 +0000108 trace_kvm_mmio(KVM_TRACE_MMIO_READ, len, run->mmio.phys_addr,
Wanpeng Lie39d200f2017-12-14 17:40:50 -0800109 &data);
Marc Zyngier6d89d2d2013-02-12 12:40:22 +0000110 data = vcpu_data_host_to_guest(vcpu, data, len);
Marc Zyngier0e20f5e2019-12-13 13:25:25 +0000111 vcpu_set_reg(vcpu, kvm_vcpu_dabt_get_rd(vcpu), data);
Christoffer Dall45e96ea2013-01-20 18:43:58 -0500112 }
113
Mark Rutland0d640732018-11-09 15:07:10 +0000114 /*
115 * The MMIO instruction is emulated and should not be re-executed
116 * in the guest.
117 */
118 kvm_skip_instr(vcpu, kvm_vcpu_trap_il_is32bit(vcpu));
119
Christoffer Dall45e96ea2013-01-20 18:43:58 -0500120 return 0;
121}
122
Tianjia Zhang74cc7e02020-06-23 21:14:15 +0800123int io_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa)
Christoffer Dall45e96ea2013-01-20 18:43:58 -0500124{
Tianjia Zhang74cc7e02020-06-23 21:14:15 +0800125 struct kvm_run *run = vcpu->run;
Marc Zyngier6d89d2d2013-02-12 12:40:22 +0000126 unsigned long data;
Christoffer Dall45e96ea2013-01-20 18:43:58 -0500127 unsigned long rt;
128 int ret;
Andre Przywara950324a2015-03-28 01:13:13 +0000129 bool is_write;
130 int len;
131 u8 data_buf[8];
Christoffer Dall45e96ea2013-01-20 18:43:58 -0500132
133 /*
Marc Zyngier0e20f5e2019-12-13 13:25:25 +0000134 * No valid syndrome? Ask userspace for help if it has
Fuad Tabba656012c2020-04-01 15:03:10 +0100135 * volunteered to do so, and bail out otherwise.
Christoffer Dall45e96ea2013-01-20 18:43:58 -0500136 */
Marc Zyngier0e20f5e2019-12-13 13:25:25 +0000137 if (!kvm_vcpu_dabt_isvalid(vcpu)) {
Christoffer Dallc7262002019-10-11 13:07:05 +0200138 if (vcpu->kvm->arch.return_nisv_io_abort_to_user) {
139 run->exit_reason = KVM_EXIT_ARM_NISV;
140 run->arm_nisv.esr_iss = kvm_vcpu_dabt_iss_nisv_sanitized(vcpu);
141 run->arm_nisv.fault_ipa = fault_ipa;
142 return 0;
143 }
144
145 kvm_pr_unimpl("Data abort outside memslots with no valid syndrome info\n");
Christoffer Dall45e96ea2013-01-20 18:43:58 -0500146 return -ENOSYS;
147 }
148
Marc Zyngier0e20f5e2019-12-13 13:25:25 +0000149 /*
150 * Prepare MMIO operation. First decode the syndrome data we get
151 * from the CPU. Then try if some in-kernel emulation feels
152 * responsible, otherwise let user space do its magic.
153 */
154 is_write = kvm_vcpu_dabt_iswrite(vcpu);
155 len = kvm_vcpu_dabt_get_as(vcpu);
156 rt = kvm_vcpu_dabt_get_rd(vcpu);
Marc Zyngier6d89d2d2013-02-12 12:40:22 +0000157
Andre Przywara950324a2015-03-28 01:13:13 +0000158 if (is_write) {
Pavel Fedinbc45a512015-12-04 15:03:11 +0300159 data = vcpu_data_guest_to_host(vcpu, vcpu_get_reg(vcpu, rt),
160 len);
Christoffer Dall45e96ea2013-01-20 18:43:58 -0500161
Wanpeng Lie39d200f2017-12-14 17:40:50 -0800162 trace_kvm_mmio(KVM_TRACE_MMIO_WRITE, len, fault_ipa, &data);
Christoffer Dalld5a5a0e2016-04-24 21:41:36 +0200163 kvm_mmio_write_buf(data_buf, len, data);
Andre Przywara950324a2015-03-28 01:13:13 +0000164
165 ret = kvm_io_bus_write(vcpu, KVM_MMIO_BUS, fault_ipa, len,
166 data_buf);
Andre Przywara5100f9832014-11-06 12:11:45 +0000167 } else {
Andre Przywara950324a2015-03-28 01:13:13 +0000168 trace_kvm_mmio(KVM_TRACE_MMIO_READ_UNSATISFIED, len,
Wanpeng Lie39d200f2017-12-14 17:40:50 -0800169 fault_ipa, NULL);
Andre Przywara950324a2015-03-28 01:13:13 +0000170
171 ret = kvm_io_bus_read(vcpu, KVM_MMIO_BUS, fault_ipa, len,
172 data_buf);
Andre Przywara5100f9832014-11-06 12:11:45 +0000173 }
Christoffer Dall45e96ea2013-01-20 18:43:58 -0500174
Andre Przywara950324a2015-03-28 01:13:13 +0000175 /* Now prepare kvm_run for the potential return to userland. */
176 run->mmio.is_write = is_write;
177 run->mmio.phys_addr = fault_ipa;
178 run->mmio.len = len;
Andrew Jones2113c5f2019-08-22 13:03:05 +0200179 vcpu->mmio_needed = 1;
Marc Zyngier1a89dd92013-01-21 19:36:12 -0500180
Andre Przywara950324a2015-03-28 01:13:13 +0000181 if (!ret) {
182 /* We handled the access successfully in the kernel. */
Christoffer Dall83091db2016-03-29 14:29:28 +0200183 if (!is_write)
184 memcpy(run->mmio.data, data_buf, len);
Amit Tomarb19e6892015-11-26 10:09:43 +0000185 vcpu->stat.mmio_exit_kernel++;
Tianjia Zhang74cc7e02020-06-23 21:14:15 +0800186 kvm_handle_mmio_return(vcpu);
Andre Przywara950324a2015-03-28 01:13:13 +0000187 return 1;
188 }
189
Christoffer Dall83091db2016-03-29 14:29:28 +0200190 if (is_write)
191 memcpy(run->mmio.data, data_buf, len);
192 vcpu->stat.mmio_exit_user++;
Andre Przywara950324a2015-03-28 01:13:13 +0000193 run->exit_reason = KVM_EXIT_MMIO;
Christoffer Dall45e96ea2013-01-20 18:43:58 -0500194 return 0;
195}