2 * tpr optimization for qemu/kvm
4 * Copyright (C) 2007-2008 Qumranet Technologies
6 * Licensed under the terms of the GNU GPL version 2 or higher.
10 #include "config-host.h"
22 static uint64_t map_addr(struct kvm_sregs
*sregs
, target_ulong virt
, unsigned *perms
)
24 uint64_t mask
= ((1ull << 48) - 1) & ~4095ull;
28 if (sregs
->cr4
& 0x20) {
30 p
= ldq_phys(p
+ 8 * (virt
>> 30));
34 p
= ldq_phys(p
+ 8 * ((virt
>> 21) & 511));
39 p
+= ((virt
>> 12) & 511) << 12;
42 p
= ldq_phys(p
+ 8 * ((virt
>> 12) & 511));
49 p
= ldl_phys(p
+ 4 * ((virt
>> 22) & 1023));
54 p
+= ((virt
>> 12) & 1023) << 12;
57 p
= ldl_phys(p
+ 4 * ((virt
>> 12) & 1023));
66 return p
+ (virt
& 4095);
69 static uint8_t read_byte_virt(CPUState
*env
, target_ulong virt
)
71 struct kvm_sregs sregs
;
73 kvm_get_sregs(kvm_context
, env
->cpu_index
, &sregs
);
74 return ldub_phys(map_addr(&sregs
, virt
, NULL
));
77 static void write_byte_virt(CPUState
*env
, target_ulong virt
, uint8_t b
)
79 struct kvm_sregs sregs
;
81 kvm_get_sregs(kvm_context
, env
->cpu_index
, &sregs
);
82 stb_phys(map_addr(&sregs
, virt
, NULL
), b
);
85 static __u64
kvm_rsp_read(CPUState
*env
)
89 kvm_get_regs(kvm_context
, env
->cpu_index
, ®s
);
102 struct vapic_patches
{
104 uint32_t set_tpr_eax
;
106 uint32_t get_tpr_stack
;
107 } __attribute__((packed
)) up
, mp
;
108 } __attribute__((packed
));
110 static struct vapic_bios vapic_bios
;
112 static uint32_t real_tpr
;
113 static uint32_t bios_addr
;
114 static uint32_t vapic_phys
;
115 static uint32_t bios_enabled
;
116 static uint32_t vbios_desc_phys
;
118 static void update_vbios_real_tpr(void)
120 cpu_physical_memory_rw(vbios_desc_phys
, (void *)&vapic_bios
, sizeof vapic_bios
, 0);
121 vapic_bios
.real_tpr
= real_tpr
;
122 vapic_bios
.vcpu_shift
= 7;
123 cpu_physical_memory_rw(vbios_desc_phys
, (void *)&vapic_bios
, sizeof vapic_bios
, 1);
126 static unsigned modrm_reg(uint8_t modrm
)
128 return (modrm
>> 3) & 7;
131 static int is_abs_modrm(uint8_t modrm
)
133 return (modrm
& 0xc7) == 0x05;
136 static int instruction_is_ok(CPUState
*env
, uint64_t rip
, int is_write
)
139 unsigned addr_offset
;
143 if ((rip
& 0xf0000000) != 0x80000000 && (rip
& 0xf0000000) != 0xe0000000)
145 if (kvm_rsp_read(env
) == 0)
147 b1
= read_byte_virt(env
, rip
);
148 b2
= read_byte_virt(env
, rip
+ 1);
150 case 0xc7: /* mov imm32, r/m32 (c7/0) */
151 if (modrm_reg(b2
) != 0)
154 case 0x89: /* mov r32 to r/m32 */
155 case 0x8b: /* mov r/m32 to r32 */
156 if (!is_abs_modrm(b2
))
160 case 0xa1: /* mov abs to eax */
161 case 0xa3: /* mov eax to abs */
164 case 0xff: /* push r/m32 */
165 if (modrm_reg(b2
) != 6 || !is_abs_modrm(b2
))
171 p
= rip
+ addr_offset
;
172 addr
= read_byte_virt(env
, p
++);
173 addr
|= read_byte_virt(env
, p
++) << 8;
174 addr
|= read_byte_virt(env
, p
++) << 16;
175 addr
|= read_byte_virt(env
, p
++) << 24;
176 if ((addr
& 0xfff) != 0x80)
179 update_vbios_real_tpr();
183 static int bios_is_mapped(CPUState
*env
, uint64_t rip
)
187 struct kvm_sregs sregs
;
190 uint32_t offset
, fixup
;
195 kvm_get_sregs(kvm_context
, env
->cpu_index
, &sregs
);
197 probe
= (rip
& 0xf0000000) + 0xe0000;
198 phys
= map_addr(&sregs
, probe
, &perms
);
202 for (i
= 0; i
< 64; ++i
) {
203 cpu_physical_memory_read(phys
, (void *)&vapic_bios
, sizeof(vapic_bios
));
204 if (memcmp(vapic_bios
.signature
, "kvm aPiC", 8) == 0)
211 if (bios_addr
== vapic_bios
.virt_base
)
213 vbios_desc_phys
= phys
;
214 for (i
= vapic_bios
.fixup_start
; i
< vapic_bios
.fixup_end
; i
+= 4) {
215 offset
= ldl_phys(phys
+ i
- vapic_bios
.virt_base
);
216 fixup
= phys
+ offset
;
217 stl_phys(fixup
, ldl_phys(fixup
) + bios_addr
- vapic_bios
.virt_base
);
219 vapic_phys
= vapic_bios
.vapic
- vapic_bios
.virt_base
+ phys
;
223 static int enable_vapic(CPUState
*env
)
225 static uint8_t one
= 1;
227 kvm_enable_vapic(kvm_context
, env
->cpu_index
,
228 vapic_phys
+ (env
->cpu_index
<< 7));
229 cpu_physical_memory_rw(vapic_phys
+ (env
->cpu_index
<< 7) + 4, &one
, 1, 1);
235 static void patch_call(CPUState
*env
, uint64_t rip
, uint32_t target
)
239 offset
= target
- vapic_bios
.virt_base
+ bios_addr
- rip
- 5;
240 write_byte_virt(env
, rip
, 0xe8); /* call near */
241 write_byte_virt(env
, rip
+ 1, offset
);
242 write_byte_virt(env
, rip
+ 2, offset
>> 8);
243 write_byte_virt(env
, rip
+ 3, offset
>> 16);
244 write_byte_virt(env
, rip
+ 4, offset
>> 24);
247 static void patch_instruction(CPUState
*env
, uint64_t rip
)
250 struct vapic_patches
*vp
;
252 vp
= smp_cpus
== 1 ? &vapic_bios
.up
: &vapic_bios
.mp
;
253 b1
= read_byte_virt(env
, rip
);
254 b2
= read_byte_virt(env
, rip
+ 1);
256 case 0x89: /* mov r32 to r/m32 */
257 write_byte_virt(env
, rip
, 0x50 + modrm_reg(b2
)); /* push reg */
258 patch_call(env
, rip
+ 1, vp
->set_tpr
);
260 case 0x8b: /* mov r/m32 to r32 */
261 write_byte_virt(env
, rip
, 0x90);
262 patch_call(env
, rip
+ 1, vp
->get_tpr
[modrm_reg(b2
)]);
264 case 0xa1: /* mov abs to eax */
265 patch_call(env
, rip
, vp
->get_tpr
[0]);
267 case 0xa3: /* mov eax to abs */
268 patch_call(env
, rip
, vp
->set_tpr_eax
);
270 case 0xc7: /* mov imm32, r/m32 (c7/0) */
271 write_byte_virt(env
, rip
, 0x68); /* push imm32 */
272 write_byte_virt(env
, rip
+ 1, read_byte_virt(env
, rip
+6));
273 write_byte_virt(env
, rip
+ 2, read_byte_virt(env
, rip
+7));
274 write_byte_virt(env
, rip
+ 3, read_byte_virt(env
, rip
+8));
275 write_byte_virt(env
, rip
+ 4, read_byte_virt(env
, rip
+9));
276 patch_call(env
, rip
+ 5, vp
->set_tpr
);
278 case 0xff: /* push r/m32 */
279 printf("patching push\n");
280 write_byte_virt(env
, rip
, 0x50); /* push eax */
281 patch_call(env
, rip
+ 1, vp
->get_tpr_stack
);
284 printf("funny insn %02x %02x\n", b1
, b2
);
288 void kvm_tpr_access_report(CPUState
*env
, uint64_t rip
, int is_write
)
290 if (!instruction_is_ok(env
, rip
, is_write
))
292 if (!bios_is_mapped(env
, rip
))
294 if (!enable_vapic(env
))
296 patch_instruction(env
, rip
);
299 void kvm_tpr_vcpu_start(CPUState
*env
)
301 kvm_enable_tpr_access_reporting(kvm_context
, env
->cpu_index
);
306 static void tpr_save(QEMUFile
*f
, void *s
)
310 for (i
= 0; i
< (sizeof vapic_bios
) / 4; ++i
)
311 qemu_put_be32s(f
, &((uint32_t *)&vapic_bios
)[i
]);
312 qemu_put_be32s(f
, &bios_enabled
);
313 qemu_put_be32s(f
, &real_tpr
);
314 qemu_put_be32s(f
, &bios_addr
);
315 qemu_put_be32s(f
, &vapic_phys
);
316 qemu_put_be32s(f
, &vbios_desc_phys
);
319 static int tpr_load(QEMUFile
*f
, void *s
, int version_id
)
326 for (i
= 0; i
< (sizeof vapic_bios
) / 4; ++i
)
327 qemu_get_be32s(f
, &((uint32_t *)&vapic_bios
)[i
]);
328 qemu_get_be32s(f
, &bios_enabled
);
329 qemu_get_be32s(f
, &real_tpr
);
330 qemu_get_be32s(f
, &bios_addr
);
331 qemu_get_be32s(f
, &vapic_phys
);
332 qemu_get_be32s(f
, &vbios_desc_phys
);
335 CPUState
*env
= first_cpu
->next_cpu
;
337 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
344 static void vtpr_ioport_write(void *opaque
, uint32_t addr
, uint32_t val
)
346 CPUState
*env
= cpu_single_env
;
347 struct kvm_regs regs
;
348 struct kvm_sregs sregs
;
351 kvm_get_regs(kvm_context
, env
->cpu_index
, ®s
);
353 write_byte_virt(env
, rip
, 0x66);
354 write_byte_virt(env
, rip
+ 1, 0x90);
357 if (!bios_is_mapped(env
, rip
))
358 printf("bios not mapped?\n");
359 kvm_get_sregs(kvm_context
, env
->cpu_index
, &sregs
);
360 for (addr
= 0xfffff000u
; addr
>= 0x80000000u
; addr
-= 4096)
361 if (map_addr(&sregs
, addr
, NULL
) == 0xfee00000u
) {
362 real_tpr
= addr
+ 0x80;
366 update_vbios_real_tpr();
370 void kvm_tpr_opt_setup(void)
372 register_savevm("kvm-tpr-opt", 0, 1, tpr_save
, tpr_load
, NULL
);
373 register_ioport_write(0x7e, 1, 1, vtpr_ioport_write
, NULL
);