@@ -4045,85 +4045,6 @@ static int vcpu_mmio_gva_to_gpa(struct kvm_vcpu *vcpu, unsigned long gva,
40454045 return 0;
40464046}
40474047
4048- static int emulator_read_emulated_onepage(unsigned long addr,
4049- void *val,
4050- unsigned int bytes,
4051- struct x86_exception *exception,
4052- struct kvm_vcpu *vcpu)
4053- {
4054- gpa_t gpa;
4055- int handled, ret;
4056-
4057- if (vcpu->mmio_read_completed) {
4058- memcpy(val, vcpu->mmio_data, bytes);
4059- trace_kvm_mmio(KVM_TRACE_MMIO_READ, bytes,
4060- vcpu->mmio_phys_addr, *(u64 *)val);
4061- vcpu->mmio_read_completed = 0;
4062- return X86EMUL_CONTINUE;
4063- }
4064-
4065- ret = vcpu_mmio_gva_to_gpa(vcpu, addr, &gpa, exception, false);
4066-
4067- if (ret < 0)
4068- return X86EMUL_PROPAGATE_FAULT;
4069-
4070- if (ret)
4071- goto mmio;
4072-
4073- if (!kvm_read_guest(vcpu->kvm, gpa, val, bytes))
4074- return X86EMUL_CONTINUE;
4075-
4076- mmio:
4077- /*
4078- * Is this MMIO handled locally?
4079- */
4080- handled = vcpu_mmio_read(vcpu, gpa, bytes, val);
4081-
4082- if (handled == bytes)
4083- return X86EMUL_CONTINUE;
4084-
4085- gpa += handled;
4086- bytes -= handled;
4087- val += handled;
4088-
4089- trace_kvm_mmio(KVM_TRACE_MMIO_READ_UNSATISFIED, bytes, gpa, 0);
4090-
4091- vcpu->mmio_needed = 1;
4092- vcpu->run->exit_reason = KVM_EXIT_MMIO;
4093- vcpu->run->mmio.phys_addr = vcpu->mmio_phys_addr = gpa;
4094- vcpu->mmio_size = bytes;
4095- vcpu->run->mmio.len = min(vcpu->mmio_size, 8);
4096- vcpu->run->mmio.is_write = vcpu->mmio_is_write = 0;
4097- vcpu->mmio_index = 0;
4098-
4099- return X86EMUL_IO_NEEDED;
4100- }
4101-
4102- static int emulator_read_emulated(struct x86_emulate_ctxt *ctxt,
4103- unsigned long addr,
4104- void *val,
4105- unsigned int bytes,
4106- struct x86_exception *exception)
4107- {
4108- struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
4109-
4110- /* Crossing a page boundary? */
4111- if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
4112- int rc, now;
4113-
4114- now = -addr & ~PAGE_MASK;
4115- rc = emulator_read_emulated_onepage(addr, val, now, exception,
4116- vcpu);
4117- if (rc != X86EMUL_CONTINUE)
4118- return rc;
4119- addr += now;
4120- val += now;
4121- bytes -= now;
4122- }
4123- return emulator_read_emulated_onepage(addr, val, bytes, exception,
4124- vcpu);
4125- }
4126-
41274048int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
41284049 const void *val, int bytes)
41294050{
@@ -4208,16 +4129,21 @@ static struct read_write_emulator_ops write_emultor = {
42084129 .write = true,
42094130};
42104131
4211- static int emulator_write_emulated_onepage (unsigned long addr,
4212- const void *val ,
4213- unsigned int bytes ,
4214- struct x86_exception *exception ,
4215- struct kvm_vcpu *vcpu )
4132+ static int emulator_read_write_onepage (unsigned long addr, void *val ,
4133+ unsigned int bytes ,
4134+ struct x86_exception *exception ,
4135+ struct kvm_vcpu *vcpu ,
4136+ struct read_write_emulator_ops *ops )
42164137{
42174138 gpa_t gpa;
42184139 int handled, ret;
4140+ bool write = ops->write;
4141+
4142+ if (ops->read_write_prepare &&
4143+ ops->read_write_prepare(vcpu, val, bytes))
4144+ return X86EMUL_CONTINUE;
42194145
4220- ret = vcpu_mmio_gva_to_gpa(vcpu, addr, &gpa, exception, true );
4146+ ret = vcpu_mmio_gva_to_gpa(vcpu, addr, &gpa, exception, write );
42214147
42224148 if (ret < 0)
42234149 return X86EMUL_PROPAGATE_FAULT;
@@ -4226,15 +4152,14 @@ static int emulator_write_emulated_onepage(unsigned long addr,
42264152 if (ret)
42274153 goto mmio;
42284154
4229- if (emulator_write_phys (vcpu, gpa, val, bytes))
4155+ if (ops->read_write_emulate (vcpu, gpa, val, bytes))
42304156 return X86EMUL_CONTINUE;
42314157
42324158mmio:
4233- trace_kvm_mmio(KVM_TRACE_MMIO_WRITE, bytes, gpa, *(u64 *)val);
42344159 /*
42354160 * Is this MMIO handled locally?
42364161 */
4237- handled = vcpu_mmio_write (vcpu, gpa, bytes, val);
4162+ handled = ops->read_write_mmio (vcpu, gpa, bytes, val);
42384163 if (handled == bytes)
42394164 return X86EMUL_CONTINUE;
42404165
@@ -4243,23 +4168,20 @@ static int emulator_write_emulated_onepage(unsigned long addr,
42434168 val += handled;
42444169
42454170 vcpu->mmio_needed = 1;
4246- memcpy(vcpu->mmio_data, val, bytes);
42474171 vcpu->run->exit_reason = KVM_EXIT_MMIO;
42484172 vcpu->run->mmio.phys_addr = vcpu->mmio_phys_addr = gpa;
42494173 vcpu->mmio_size = bytes;
42504174 vcpu->run->mmio.len = min(vcpu->mmio_size, 8);
4251- vcpu->run->mmio.is_write = vcpu->mmio_is_write = 1;
4252- memcpy(vcpu->run->mmio.data, vcpu->mmio_data, 8);
4175+ vcpu->run->mmio.is_write = vcpu->mmio_is_write = write;
42534176 vcpu->mmio_index = 0;
42544177
4255- return X86EMUL_CONTINUE ;
4178+ return ops->read_write_exit_mmio(vcpu, gpa, val, bytes) ;
42564179}
42574180
4258- int emulator_write_emulated(struct x86_emulate_ctxt *ctxt,
4259- unsigned long addr,
4260- const void *val,
4261- unsigned int bytes,
4262- struct x86_exception *exception)
4181+ int emulator_read_write(struct x86_emulate_ctxt *ctxt, unsigned long addr,
4182+ void *val, unsigned int bytes,
4183+ struct x86_exception *exception,
4184+ struct read_write_emulator_ops *ops)
42634185{
42644186 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
42654187
@@ -4268,16 +4190,38 @@ int emulator_write_emulated(struct x86_emulate_ctxt *ctxt,
42684190 int rc, now;
42694191
42704192 now = -addr & ~PAGE_MASK;
4271- rc = emulator_write_emulated_onepage(addr, val, now, exception,
4272- vcpu);
4193+ rc = emulator_read_write_onepage(addr, val, now, exception,
4194+ vcpu, ops);
4195+
42734196 if (rc != X86EMUL_CONTINUE)
42744197 return rc;
42754198 addr += now;
42764199 val += now;
42774200 bytes -= now;
42784201 }
4279- return emulator_write_emulated_onepage(addr, val, bytes, exception,
4280- vcpu);
4202+
4203+ return emulator_read_write_onepage(addr, val, bytes, exception,
4204+ vcpu, ops);
4205+ }
4206+
4207+ static int emulator_read_emulated(struct x86_emulate_ctxt *ctxt,
4208+ unsigned long addr,
4209+ void *val,
4210+ unsigned int bytes,
4211+ struct x86_exception *exception)
4212+ {
4213+ return emulator_read_write(ctxt, addr, val, bytes,
4214+ exception, &read_emultor);
4215+ }
4216+
4217+ int emulator_write_emulated(struct x86_emulate_ctxt *ctxt,
4218+ unsigned long addr,
4219+ const void *val,
4220+ unsigned int bytes,
4221+ struct x86_exception *exception)
4222+ {
4223+ return emulator_read_write(ctxt, addr, (void *)val, bytes,
4224+ exception, &write_emultor);
42814225}
42824226
42834227#define CMPXCHG_TYPE(t, ptr, old, new) \
0 commit comments