Skip to content
Snippets Groups Projects
x86.c 57.3 KiB
Newer Older
  • Learn to ignore specific revisions
  • 			mutex_lock(&kvm->lock);
    			if (irq_event.irq < 16)
    				kvm_pic_set_irq(pic_irqchip(kvm),
    					irq_event.irq,
    					irq_event.level);
    			kvm_ioapic_set_irq(kvm->vioapic,
    					irq_event.irq,
    					irq_event.level);
    			mutex_unlock(&kvm->lock);
    			r = 0;
    		}
    		break;
    	}
    	case KVM_GET_IRQCHIP: {
    		/* 0: PIC master, 1: PIC slave, 2: IOAPIC */
    		struct kvm_irqchip chip;
    
    		r = -EFAULT;
    		if (copy_from_user(&chip, argp, sizeof chip))
    			goto out;
    		r = -ENXIO;
    		if (!irqchip_in_kernel(kvm))
    			goto out;
    		r = kvm_vm_ioctl_get_irqchip(kvm, &chip);
    		if (r)
    			goto out;
    		r = -EFAULT;
    		if (copy_to_user(argp, &chip, sizeof chip))
    			goto out;
    		r = 0;
    		break;
    	}
    	case KVM_SET_IRQCHIP: {
    		/* 0: PIC master, 1: PIC slave, 2: IOAPIC */
    		struct kvm_irqchip chip;
    
    		r = -EFAULT;
    		if (copy_from_user(&chip, argp, sizeof chip))
    			goto out;
    		r = -ENXIO;
    		if (!irqchip_in_kernel(kvm))
    			goto out;
    		r = kvm_vm_ioctl_set_irqchip(kvm, &chip);
    		if (r)
    			goto out;
    		r = 0;
    		break;
    	}
    	default:
    		;
    	}
    out:
    	return r;
    }
    
    
    static void kvm_init_msr_list(void)
    
    {
    	u32 dummy[2];
    	unsigned i, j;
    
    	for (i = j = 0; i < ARRAY_SIZE(msrs_to_save); i++) {
    		if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
    			continue;
    		if (j < i)
    			msrs_to_save[j] = msrs_to_save[i];
    		j++;
    	}
    	num_msrs_to_save = j;
    }
    
    
    1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386
    /*
     * Only apic need an MMIO device hook, so shortcut now..
     */
    static struct kvm_io_device *vcpu_find_pervcpu_dev(struct kvm_vcpu *vcpu,
    						gpa_t addr)
    {
    	struct kvm_io_device *dev;
    
    	if (vcpu->apic) {
    		dev = &vcpu->apic->dev;
    		if (dev->in_range(dev, addr))
    			return dev;
    	}
    	return NULL;
    }
    
    
    static struct kvm_io_device *vcpu_find_mmio_dev(struct kvm_vcpu *vcpu,
    						gpa_t addr)
    {
    	struct kvm_io_device *dev;
    
    	dev = vcpu_find_pervcpu_dev(vcpu, addr);
    	if (dev == NULL)
    		dev = kvm_io_bus_find_dev(&vcpu->kvm->mmio_bus, addr);
    	return dev;
    }
    
    int emulator_read_std(unsigned long addr,
    			     void *val,
    			     unsigned int bytes,
    			     struct kvm_vcpu *vcpu)
    {
    	void *data = val;
    
    	while (bytes) {
    		gpa_t gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
    		unsigned offset = addr & (PAGE_SIZE-1);
    		unsigned tocopy = min(bytes, (unsigned)PAGE_SIZE - offset);
    		int ret;
    
    		if (gpa == UNMAPPED_GVA)
    			return X86EMUL_PROPAGATE_FAULT;
    		ret = kvm_read_guest(vcpu->kvm, gpa, data, tocopy);
    		if (ret < 0)
    			return X86EMUL_UNHANDLEABLE;
    
    		bytes -= tocopy;
    		data += tocopy;
    		addr += tocopy;
    	}
    
    	return X86EMUL_CONTINUE;
    }
    EXPORT_SYMBOL_GPL(emulator_read_std);
    
    static int emulator_write_std(unsigned long addr,
    			      const void *val,
    			      unsigned int bytes,
    			      struct kvm_vcpu *vcpu)
    {
    	pr_unimpl(vcpu, "emulator_write_std: addr %lx n %d\n", addr, bytes);
    	return X86EMUL_UNHANDLEABLE;
    }
    
    static int emulator_read_emulated(unsigned long addr,
    				  void *val,
    				  unsigned int bytes,
    				  struct kvm_vcpu *vcpu)
    {
    	struct kvm_io_device *mmio_dev;
    	gpa_t                 gpa;
    
    	if (vcpu->mmio_read_completed) {
    		memcpy(val, vcpu->mmio_data, bytes);
    		vcpu->mmio_read_completed = 0;
    		return X86EMUL_CONTINUE;
    	}
    
    	gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
    
    	/* For APIC access vmexit */
    	if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
    		goto mmio;
    
    	if (emulator_read_std(addr, val, bytes, vcpu)
    			== X86EMUL_CONTINUE)
    		return X86EMUL_CONTINUE;
    	if (gpa == UNMAPPED_GVA)
    		return X86EMUL_PROPAGATE_FAULT;
    
    mmio:
    	/*
    	 * Is this MMIO handled locally?
    	 */
    	mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
    	if (mmio_dev) {
    		kvm_iodevice_read(mmio_dev, gpa, bytes, val);
    		return X86EMUL_CONTINUE;
    	}
    
    	vcpu->mmio_needed = 1;
    	vcpu->mmio_phys_addr = gpa;
    	vcpu->mmio_size = bytes;
    	vcpu->mmio_is_write = 0;
    
    	return X86EMUL_UNHANDLEABLE;
    }
    
    static int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
    			       const void *val, int bytes)
    {
    	int ret;
    
    	ret = kvm_write_guest(vcpu->kvm, gpa, val, bytes);
    	if (ret < 0)
    		return 0;
    	kvm_mmu_pte_write(vcpu, gpa, val, bytes);
    	return 1;
    }
    
    static int emulator_write_emulated_onepage(unsigned long addr,
    					   const void *val,
    					   unsigned int bytes,
    					   struct kvm_vcpu *vcpu)
    {
    	struct kvm_io_device *mmio_dev;
    	gpa_t                 gpa = vcpu->mmu.gva_to_gpa(vcpu, addr);
    
    	if (gpa == UNMAPPED_GVA) {
    		kvm_x86_ops->inject_page_fault(vcpu, addr, 2);
    		return X86EMUL_PROPAGATE_FAULT;
    	}
    
    	/* For APIC access vmexit */
    	if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
    		goto mmio;
    
    	if (emulator_write_phys(vcpu, gpa, val, bytes))
    		return X86EMUL_CONTINUE;
    
    mmio:
    	/*
    	 * Is this MMIO handled locally?
    	 */
    	mmio_dev = vcpu_find_mmio_dev(vcpu, gpa);
    	if (mmio_dev) {
    		kvm_iodevice_write(mmio_dev, gpa, bytes, val);
    		return X86EMUL_CONTINUE;
    	}
    
    	vcpu->mmio_needed = 1;
    	vcpu->mmio_phys_addr = gpa;
    	vcpu->mmio_size = bytes;
    	vcpu->mmio_is_write = 1;
    	memcpy(vcpu->mmio_data, val, bytes);
    
    	return X86EMUL_CONTINUE;
    }
    
    int emulator_write_emulated(unsigned long addr,
    				   const void *val,
    				   unsigned int bytes,
    				   struct kvm_vcpu *vcpu)
    {
    	/* Crossing a page boundary? */
    	if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
    		int rc, now;
    
    		now = -addr & ~PAGE_MASK;
    		rc = emulator_write_emulated_onepage(addr, val, now, vcpu);
    		if (rc != X86EMUL_CONTINUE)
    			return rc;
    		addr += now;
    		val += now;
    		bytes -= now;
    	}
    	return emulator_write_emulated_onepage(addr, val, bytes, vcpu);
    }
    EXPORT_SYMBOL_GPL(emulator_write_emulated);
    
    static int emulator_cmpxchg_emulated(unsigned long addr,
    				     const void *old,
    				     const void *new,
    				     unsigned int bytes,
    				     struct kvm_vcpu *vcpu)
    {
    	static int reported;
    
    	if (!reported) {
    		reported = 1;
    		printk(KERN_WARNING "kvm: emulating exchange as write\n");
    	}
    	return emulator_write_emulated(addr, new, bytes, vcpu);
    }
    
    static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
    {
    	return kvm_x86_ops->get_segment_base(vcpu, seg);
    }
    
    int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
    {
    	return X86EMUL_CONTINUE;
    }
    
    int emulate_clts(struct kvm_vcpu *vcpu)
    {
    	kvm_x86_ops->set_cr0(vcpu, vcpu->cr0 & ~X86_CR0_TS);
    	return X86EMUL_CONTINUE;
    }
    
    int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long *dest)
    {
    	struct kvm_vcpu *vcpu = ctxt->vcpu;
    
    	switch (dr) {
    	case 0 ... 3:
    		*dest = kvm_x86_ops->get_dr(vcpu, dr);
    		return X86EMUL_CONTINUE;
    	default:
    		pr_unimpl(vcpu, "%s: unexpected dr %u\n", __FUNCTION__, dr);
    		return X86EMUL_UNHANDLEABLE;
    	}
    }
    
    int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, unsigned long value)
    {
    	unsigned long mask = (ctxt->mode == X86EMUL_MODE_PROT64) ? ~0ULL : ~0U;
    	int exception;
    
    	kvm_x86_ops->set_dr(ctxt->vcpu, dr, value & mask, &exception);
    	if (exception) {
    		/* FIXME: better handling */
    		return X86EMUL_UNHANDLEABLE;
    	}
    	return X86EMUL_CONTINUE;
    }
    
    void kvm_report_emulation_failure(struct kvm_vcpu *vcpu, const char *context)
    {
    	static int reported;
    	u8 opcodes[4];
    	unsigned long rip = vcpu->rip;
    	unsigned long rip_linear;
    
    	rip_linear = rip + get_segment_base(vcpu, VCPU_SREG_CS);
    
    	if (reported)
    		return;
    
    	emulator_read_std(rip_linear, (void *)opcodes, 4, vcpu);
    
    	printk(KERN_ERR "emulation failed (%s) rip %lx %02x %02x %02x %02x\n",
    	       context, rip, opcodes[0], opcodes[1], opcodes[2], opcodes[3]);
    	reported = 1;
    }
    EXPORT_SYMBOL_GPL(kvm_report_emulation_failure);
    
    struct x86_emulate_ops emulate_ops = {
    	.read_std            = emulator_read_std,
    	.write_std           = emulator_write_std,
    	.read_emulated       = emulator_read_emulated,
    	.write_emulated      = emulator_write_emulated,
    	.cmpxchg_emulated    = emulator_cmpxchg_emulated,
    };
    
    int emulate_instruction(struct kvm_vcpu *vcpu,
    			struct kvm_run *run,
    			unsigned long cr2,
    			u16 error_code,
    			int no_decode)
    {
    	int r;
    
    	vcpu->mmio_fault_cr2 = cr2;
    	kvm_x86_ops->cache_regs(vcpu);
    
    	vcpu->mmio_is_write = 0;
    	vcpu->pio.string = 0;
    
    	if (!no_decode) {
    		int cs_db, cs_l;
    		kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
    
    		vcpu->emulate_ctxt.vcpu = vcpu;
    		vcpu->emulate_ctxt.eflags = kvm_x86_ops->get_rflags(vcpu);
    		vcpu->emulate_ctxt.cr2 = cr2;
    		vcpu->emulate_ctxt.mode =
    			(vcpu->emulate_ctxt.eflags & X86_EFLAGS_VM)
    			? X86EMUL_MODE_REAL : cs_l
    			? X86EMUL_MODE_PROT64 :	cs_db
    			? X86EMUL_MODE_PROT32 : X86EMUL_MODE_PROT16;
    
    		if (vcpu->emulate_ctxt.mode == X86EMUL_MODE_PROT64) {
    			vcpu->emulate_ctxt.cs_base = 0;
    			vcpu->emulate_ctxt.ds_base = 0;
    			vcpu->emulate_ctxt.es_base = 0;
    			vcpu->emulate_ctxt.ss_base = 0;
    		} else {
    			vcpu->emulate_ctxt.cs_base =
    					get_segment_base(vcpu, VCPU_SREG_CS);
    			vcpu->emulate_ctxt.ds_base =
    					get_segment_base(vcpu, VCPU_SREG_DS);
    			vcpu->emulate_ctxt.es_base =
    					get_segment_base(vcpu, VCPU_SREG_ES);
    			vcpu->emulate_ctxt.ss_base =
    					get_segment_base(vcpu, VCPU_SREG_SS);
    		}
    
    		vcpu->emulate_ctxt.gs_base =
    					get_segment_base(vcpu, VCPU_SREG_GS);
    		vcpu->emulate_ctxt.fs_base =
    					get_segment_base(vcpu, VCPU_SREG_FS);
    
    		r = x86_decode_insn(&vcpu->emulate_ctxt, &emulate_ops);
    
    		++vcpu->stat.insn_emulation;
    
    			++vcpu->stat.insn_emulation_fail;
    
    			if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
    				return EMULATE_DONE;
    			return EMULATE_FAIL;
    		}
    	}
    
    	r = x86_emulate_insn(&vcpu->emulate_ctxt, &emulate_ops);
    
    	if (vcpu->pio.string)
    		return EMULATE_DO_MMIO;
    
    	if ((r || vcpu->mmio_is_write) && run) {
    		run->exit_reason = KVM_EXIT_MMIO;
    		run->mmio.phys_addr = vcpu->mmio_phys_addr;
    		memcpy(run->mmio.data, vcpu->mmio_data, 8);
    		run->mmio.len = vcpu->mmio_size;
    		run->mmio.is_write = vcpu->mmio_is_write;
    	}
    
    	if (r) {
    		if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
    			return EMULATE_DONE;
    		if (!vcpu->mmio_needed) {
    			kvm_report_emulation_failure(vcpu, "mmio");
    			return EMULATE_FAIL;
    		}
    		return EMULATE_DO_MMIO;
    	}
    
    	kvm_x86_ops->decache_regs(vcpu);
    	kvm_x86_ops->set_rflags(vcpu, vcpu->emulate_ctxt.eflags);
    
    	if (vcpu->mmio_is_write) {
    		vcpu->mmio_needed = 0;
    		return EMULATE_DO_MMIO;
    	}
    
    	return EMULATE_DONE;
    }
    EXPORT_SYMBOL_GPL(emulate_instruction);
    
    
    static void free_pio_guest_pages(struct kvm_vcpu *vcpu)
    {
    	int i;
    
    	for (i = 0; i < ARRAY_SIZE(vcpu->pio.guest_pages); ++i)
    		if (vcpu->pio.guest_pages[i]) {
    			kvm_release_page(vcpu->pio.guest_pages[i]);
    			vcpu->pio.guest_pages[i] = NULL;
    		}
    }
    
    static int pio_copy_data(struct kvm_vcpu *vcpu)
    {
    	void *p = vcpu->pio_data;
    	void *q;
    	unsigned bytes;
    	int nr_pages = vcpu->pio.guest_pages[1] ? 2 : 1;
    
    	q = vmap(vcpu->pio.guest_pages, nr_pages, VM_READ|VM_WRITE,
    		 PAGE_KERNEL);
    	if (!q) {
    		free_pio_guest_pages(vcpu);
    		return -ENOMEM;
    	}
    	q += vcpu->pio.guest_page_offset;
    	bytes = vcpu->pio.size * vcpu->pio.cur_count;
    	if (vcpu->pio.in)
    		memcpy(q, p, bytes);
    	else
    		memcpy(p, q, bytes);
    	q -= vcpu->pio.guest_page_offset;
    	vunmap(q);
    	free_pio_guest_pages(vcpu);
    	return 0;
    }
    
    int complete_pio(struct kvm_vcpu *vcpu)
    {
    	struct kvm_pio_request *io = &vcpu->pio;
    	long delta;
    	int r;
    
    	kvm_x86_ops->cache_regs(vcpu);
    
    	if (!io->string) {
    		if (io->in)
    			memcpy(&vcpu->regs[VCPU_REGS_RAX], vcpu->pio_data,
    			       io->size);
    	} else {
    		if (io->in) {
    			r = pio_copy_data(vcpu);
    			if (r) {
    				kvm_x86_ops->cache_regs(vcpu);
    				return r;
    			}
    		}
    
    		delta = 1;
    		if (io->rep) {
    			delta *= io->cur_count;
    			/*
    			 * The size of the register should really depend on
    			 * current address size.
    			 */
    			vcpu->regs[VCPU_REGS_RCX] -= delta;
    		}
    		if (io->down)
    			delta = -delta;
    		delta *= io->size;
    		if (io->in)
    			vcpu->regs[VCPU_REGS_RDI] += delta;
    		else
    			vcpu->regs[VCPU_REGS_RSI] += delta;
    	}
    
    	kvm_x86_ops->decache_regs(vcpu);
    
    	io->count -= io->cur_count;
    	io->cur_count = 0;
    
    	return 0;
    }
    
    static void kernel_pio(struct kvm_io_device *pio_dev,
    		       struct kvm_vcpu *vcpu,
    		       void *pd)
    {
    	/* TODO: String I/O for in kernel device */
    
    	mutex_lock(&vcpu->kvm->lock);
    	if (vcpu->pio.in)
    		kvm_iodevice_read(pio_dev, vcpu->pio.port,
    				  vcpu->pio.size,
    				  pd);
    	else
    		kvm_iodevice_write(pio_dev, vcpu->pio.port,
    				   vcpu->pio.size,
    				   pd);
    	mutex_unlock(&vcpu->kvm->lock);
    }
    
    static void pio_string_write(struct kvm_io_device *pio_dev,
    			     struct kvm_vcpu *vcpu)
    {
    	struct kvm_pio_request *io = &vcpu->pio;
    	void *pd = vcpu->pio_data;
    	int i;
    
    	mutex_lock(&vcpu->kvm->lock);
    	for (i = 0; i < io->cur_count; i++) {
    		kvm_iodevice_write(pio_dev, io->port,
    				   io->size,
    				   pd);
    		pd += io->size;
    	}
    	mutex_unlock(&vcpu->kvm->lock);
    }
    
    static struct kvm_io_device *vcpu_find_pio_dev(struct kvm_vcpu *vcpu,
    					       gpa_t addr)
    {
    	return kvm_io_bus_find_dev(&vcpu->kvm->pio_bus, addr);
    }
    
    int kvm_emulate_pio(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
    		  int size, unsigned port)
    {
    	struct kvm_io_device *pio_dev;
    
    	vcpu->run->exit_reason = KVM_EXIT_IO;
    	vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
    	vcpu->run->io.size = vcpu->pio.size = size;
    	vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
    	vcpu->run->io.count = vcpu->pio.count = vcpu->pio.cur_count = 1;
    	vcpu->run->io.port = vcpu->pio.port = port;
    	vcpu->pio.in = in;
    	vcpu->pio.string = 0;
    	vcpu->pio.down = 0;
    	vcpu->pio.guest_page_offset = 0;
    	vcpu->pio.rep = 0;
    
    	kvm_x86_ops->cache_regs(vcpu);
    	memcpy(vcpu->pio_data, &vcpu->regs[VCPU_REGS_RAX], 4);
    	kvm_x86_ops->decache_regs(vcpu);
    
    	kvm_x86_ops->skip_emulated_instruction(vcpu);
    
    	pio_dev = vcpu_find_pio_dev(vcpu, port);
    	if (pio_dev) {
    		kernel_pio(pio_dev, vcpu, vcpu->pio_data);
    		complete_pio(vcpu);
    		return 1;
    	}
    	return 0;
    }
    EXPORT_SYMBOL_GPL(kvm_emulate_pio);
    
    int kvm_emulate_pio_string(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
    		  int size, unsigned long count, int down,
    		  gva_t address, int rep, unsigned port)
    {
    	unsigned now, in_page;
    	int i, ret = 0;
    	int nr_pages = 1;
    	struct page *page;
    	struct kvm_io_device *pio_dev;
    
    	vcpu->run->exit_reason = KVM_EXIT_IO;
    	vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
    	vcpu->run->io.size = vcpu->pio.size = size;
    	vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
    	vcpu->run->io.count = vcpu->pio.count = vcpu->pio.cur_count = count;
    	vcpu->run->io.port = vcpu->pio.port = port;
    	vcpu->pio.in = in;
    	vcpu->pio.string = 1;
    	vcpu->pio.down = down;
    	vcpu->pio.guest_page_offset = offset_in_page(address);
    	vcpu->pio.rep = rep;
    
    	if (!count) {
    		kvm_x86_ops->skip_emulated_instruction(vcpu);
    		return 1;
    	}
    
    	if (!down)
    		in_page = PAGE_SIZE - offset_in_page(address);
    	else
    		in_page = offset_in_page(address) + size;
    	now = min(count, (unsigned long)in_page / size);
    	if (!now) {
    		/*
    		 * String I/O straddles page boundary.  Pin two guest pages
    		 * so that we satisfy atomicity constraints.  Do just one
    		 * transaction to avoid complexity.
    		 */
    		nr_pages = 2;
    		now = 1;
    	}
    	if (down) {
    		/*
    		 * String I/O in reverse.  Yuck.  Kill the guest, fix later.
    		 */
    		pr_unimpl(vcpu, "guest string pio down\n");
    		inject_gp(vcpu);
    		return 1;
    	}
    	vcpu->run->io.count = now;
    	vcpu->pio.cur_count = now;
    
    	if (vcpu->pio.cur_count == vcpu->pio.count)
    		kvm_x86_ops->skip_emulated_instruction(vcpu);
    
    	for (i = 0; i < nr_pages; ++i) {
    		mutex_lock(&vcpu->kvm->lock);
    		page = gva_to_page(vcpu, address + i * PAGE_SIZE);
    		vcpu->pio.guest_pages[i] = page;
    		mutex_unlock(&vcpu->kvm->lock);
    		if (!page) {
    			inject_gp(vcpu);
    			free_pio_guest_pages(vcpu);
    			return 1;
    		}
    	}
    
    	pio_dev = vcpu_find_pio_dev(vcpu, port);
    	if (!vcpu->pio.in) {
    		/* string PIO write */
    		ret = pio_copy_data(vcpu);
    		if (ret >= 0 && pio_dev) {
    			pio_string_write(pio_dev, vcpu);
    			complete_pio(vcpu);
    			if (vcpu->pio.count == 0)
    				ret = 1;
    		}
    	} else if (pio_dev)
    		pr_unimpl(vcpu, "no string pio read support yet, "
    		       "port %x size %d count %ld\n",
    			port, size, count);
    
    	return ret;
    }
    EXPORT_SYMBOL_GPL(kvm_emulate_pio_string);
    
    
    	struct kvm_x86_ops *ops = (struct kvm_x86_ops *)opaque;
    
    
    	kvm_init_msr_list();
    
    
    	if (kvm_x86_ops) {
    		printk(KERN_ERR "kvm: already loaded the other module\n");
    		return -EEXIST;
    	}
    
    	if (!ops->cpu_has_kvm_support()) {
    		printk(KERN_ERR "kvm: no hardware support\n");
    		return -EOPNOTSUPP;
    	}
    	if (ops->disabled_by_bios()) {
    		printk(KERN_ERR "kvm: disabled by bios\n");
    		return -EOPNOTSUPP;
    	}
    
    	kvm_x86_ops = ops;
    
    	return 0;
    
    int kvm_emulate_halt(struct kvm_vcpu *vcpu)
    {
    	++vcpu->stat.halt_exits;
    	if (irqchip_in_kernel(vcpu->kvm)) {
    		vcpu->mp_state = VCPU_MP_STATE_HALTED;
    		kvm_vcpu_block(vcpu);
    		if (vcpu->mp_state != VCPU_MP_STATE_RUNNABLE)
    			return -EINTR;
    		return 1;
    	} else {
    		vcpu->run->exit_reason = KVM_EXIT_HLT;
    		return 0;
    	}
    }
    EXPORT_SYMBOL_GPL(kvm_emulate_halt);
    
    int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
    {
    	unsigned long nr, a0, a1, a2, a3, ret;
    
    	kvm_x86_ops->cache_regs(vcpu);
    
    	nr = vcpu->regs[VCPU_REGS_RAX];
    	a0 = vcpu->regs[VCPU_REGS_RBX];
    	a1 = vcpu->regs[VCPU_REGS_RCX];
    	a2 = vcpu->regs[VCPU_REGS_RDX];
    	a3 = vcpu->regs[VCPU_REGS_RSI];
    
    	if (!is_long_mode(vcpu)) {
    		nr &= 0xFFFFFFFF;
    		a0 &= 0xFFFFFFFF;
    		a1 &= 0xFFFFFFFF;
    		a2 &= 0xFFFFFFFF;
    		a3 &= 0xFFFFFFFF;
    	}
    
    	switch (nr) {
    	default:
    		ret = -KVM_ENOSYS;
    		break;
    	}
    	vcpu->regs[VCPU_REGS_RAX] = ret;
    	kvm_x86_ops->decache_regs(vcpu);
    	return 0;
    }
    EXPORT_SYMBOL_GPL(kvm_emulate_hypercall);
    
    int kvm_fix_hypercall(struct kvm_vcpu *vcpu)
    {
    	char instruction[3];
    	int ret = 0;
    
    	mutex_lock(&vcpu->kvm->lock);
    
    	/*
    	 * Blow out the MMU to ensure that no other VCPU has an active mapping
    	 * to ensure that the updated hypercall appears atomically across all
    	 * VCPUs.
    	 */
    	kvm_mmu_zap_all(vcpu->kvm);
    
    	kvm_x86_ops->cache_regs(vcpu);
    	kvm_x86_ops->patch_hypercall(vcpu, instruction);
    	if (emulator_write_emulated(vcpu->rip, instruction, 3, vcpu)
    	    != X86EMUL_CONTINUE)
    		ret = -EFAULT;
    
    	mutex_unlock(&vcpu->kvm->lock);
    
    	return ret;
    }
    
    static u64 mk_cr_64(u64 curr_cr, u32 new_val)
    {
    	return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
    }
    
    void realmode_lgdt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
    {
    	struct descriptor_table dt = { limit, base };
    
    	kvm_x86_ops->set_gdt(vcpu, &dt);
    }
    
    void realmode_lidt(struct kvm_vcpu *vcpu, u16 limit, unsigned long base)
    {
    	struct descriptor_table dt = { limit, base };
    
    	kvm_x86_ops->set_idt(vcpu, &dt);
    }
    
    void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
    		   unsigned long *rflags)
    {
    	lmsw(vcpu, msw);
    	*rflags = kvm_x86_ops->get_rflags(vcpu);
    }
    
    unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr)
    {
    	kvm_x86_ops->decache_cr4_guest_bits(vcpu);
    	switch (cr) {
    	case 0:
    		return vcpu->cr0;
    	case 2:
    		return vcpu->cr2;
    	case 3:
    		return vcpu->cr3;
    	case 4:
    		return vcpu->cr4;
    	default:
    		vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
    		return 0;
    	}
    }
    
    void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long val,
    		     unsigned long *rflags)
    {
    	switch (cr) {
    	case 0:
    		set_cr0(vcpu, mk_cr_64(vcpu->cr0, val));
    		*rflags = kvm_x86_ops->get_rflags(vcpu);
    		break;
    	case 2:
    		vcpu->cr2 = val;
    		break;
    	case 3:
    		set_cr3(vcpu, val);
    		break;
    	case 4:
    		set_cr4(vcpu, mk_cr_64(vcpu->cr4, val));
    		break;
    	default:
    		vcpu_printf(vcpu, "%s: unexpected cr %u\n", __FUNCTION__, cr);
    	}
    }
    
    void kvm_emulate_cpuid(struct kvm_vcpu *vcpu)
    {
    	int i;
    	u32 function;
    	struct kvm_cpuid_entry *e, *best;
    
    	kvm_x86_ops->cache_regs(vcpu);
    	function = vcpu->regs[VCPU_REGS_RAX];
    	vcpu->regs[VCPU_REGS_RAX] = 0;
    	vcpu->regs[VCPU_REGS_RBX] = 0;
    	vcpu->regs[VCPU_REGS_RCX] = 0;
    	vcpu->regs[VCPU_REGS_RDX] = 0;
    	best = NULL;
    	for (i = 0; i < vcpu->cpuid_nent; ++i) {
    		e = &vcpu->cpuid_entries[i];
    		if (e->function == function) {
    			best = e;
    			break;
    		}
    		/*
    		 * Both basic or both extended?
    		 */
    		if (((e->function ^ function) & 0x80000000) == 0)
    			if (!best || e->function > best->function)
    				best = e;
    	}
    	if (best) {
    		vcpu->regs[VCPU_REGS_RAX] = best->eax;
    		vcpu->regs[VCPU_REGS_RBX] = best->ebx;
    		vcpu->regs[VCPU_REGS_RCX] = best->ecx;
    		vcpu->regs[VCPU_REGS_RDX] = best->edx;
    	}
    	kvm_x86_ops->decache_regs(vcpu);
    	kvm_x86_ops->skip_emulated_instruction(vcpu);
    }
    EXPORT_SYMBOL_GPL(kvm_emulate_cpuid);
    
    /*
     * Check if userspace requested an interrupt window, and that the
     * interrupt window is open.
     *
     * No need to exit to userspace if we already have an interrupt queued.
     */
    static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu,
    					  struct kvm_run *kvm_run)
    {
    	return (!vcpu->irq_summary &&
    		kvm_run->request_interrupt_window &&
    		vcpu->interrupt_window_open &&
    		(kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF));
    }
    
    static void post_kvm_run_save(struct kvm_vcpu *vcpu,
    			      struct kvm_run *kvm_run)
    {
    	kvm_run->if_flag = (kvm_x86_ops->get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
    	kvm_run->cr8 = get_cr8(vcpu);
    	kvm_run->apic_base = kvm_get_apic_base(vcpu);
    	if (irqchip_in_kernel(vcpu->kvm))
    		kvm_run->ready_for_interrupt_injection = 1;
    	else
    		kvm_run->ready_for_interrupt_injection =
    					(vcpu->interrupt_window_open &&
    					 vcpu->irq_summary == 0);
    }
    
    static int __vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
    {
    	int r;
    
    	if (unlikely(vcpu->mp_state == VCPU_MP_STATE_SIPI_RECEIVED)) {
    		pr_debug("vcpu %d received sipi with vector # %x\n",
    		       vcpu->vcpu_id, vcpu->sipi_vector);
    		kvm_lapic_reset(vcpu);
    		r = kvm_x86_ops->vcpu_reset(vcpu);
    		if (r)
    			return r;
    		vcpu->mp_state = VCPU_MP_STATE_RUNNABLE;
    	}
    
    preempted:
    	if (vcpu->guest_debug.enabled)
    		kvm_x86_ops->guest_debug_pre(vcpu);
    
    again:
    	r = kvm_mmu_reload(vcpu);
    	if (unlikely(r))
    		goto out;
    
    	kvm_inject_pending_timer_irqs(vcpu);
    
    	preempt_disable();
    
    	kvm_x86_ops->prepare_guest_switch(vcpu);
    	kvm_load_guest_fpu(vcpu);
    
    	local_irq_disable();
    
    	if (signal_pending(current)) {
    		local_irq_enable();
    		preempt_enable();
    		r = -EINTR;
    		kvm_run->exit_reason = KVM_EXIT_INTR;
    		++vcpu->stat.signal_exits;
    		goto out;
    	}
    
    	if (irqchip_in_kernel(vcpu->kvm))
    		kvm_x86_ops->inject_pending_irq(vcpu);
    	else if (!vcpu->mmio_read_completed)
    		kvm_x86_ops->inject_pending_vectors(vcpu, kvm_run);
    
    	vcpu->guest_mode = 1;
    	kvm_guest_enter();
    
    	if (vcpu->requests)
    		if (test_and_clear_bit(KVM_REQ_TLB_FLUSH, &vcpu->requests))
    			kvm_x86_ops->tlb_flush(vcpu);
    
    	kvm_x86_ops->run(vcpu, kvm_run);
    
    	vcpu->guest_mode = 0;
    	local_irq_enable();
    
    	++vcpu->stat.exits;
    
    	/*
    	 * We must have an instruction between local_irq_enable() and
    	 * kvm_guest_exit(), so the timer interrupt isn't delayed by
    	 * the interrupt shadow.  The stat.exits increment will do nicely.
    	 * But we need to prevent reordering, hence this barrier():
    	 */
    	barrier();
    
    	kvm_guest_exit();
    
    	preempt_enable();
    
    	/*
    	 * Profile KVM exit RIPs:
    	 */
    	if (unlikely(prof_on == KVM_PROFILING)) {
    		kvm_x86_ops->cache_regs(vcpu);
    		profile_hit(KVM_PROFILING, (void *)vcpu->rip);
    	}
    
    	r = kvm_x86_ops->handle_exit(kvm_run, vcpu);
    
    	if (r > 0) {
    		if (dm_request_for_irq_injection(vcpu, kvm_run)) {
    			r = -EINTR;
    			kvm_run->exit_reason = KVM_EXIT_INTR;
    			++vcpu->stat.request_irq_exits;
    			goto out;
    		}