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 extern kvm_context_t kvm_context
;
24 static uint64_t map_addr(struct kvm_sregs
*sregs
, target_ulong virt
, unsigned *perms
)
26 uint64_t mask
= ((1ull << 48) - 1) & ~4095ull;
30 if (sregs
->cr4
& 0x20) {
32 p
= ldq_phys(p
+ 8 * (virt
>> 30));
36 p
= ldq_phys(p
+ 8 * ((virt
>> 21) & 511));
41 p
+= ((virt
>> 12) & 511) << 12;
44 p
= ldq_phys(p
+ 8 * ((virt
>> 12) & 511));
51 p
= ldl_phys(p
+ 4 * ((virt
>> 22) & 1023));
56 p
+= ((virt
>> 12) & 1023) << 12;
59 p
= ldl_phys(p
+ 4 * ((virt
>> 12) & 1023));
68 return p
+ (virt
& 4095);
71 static uint8_t read_byte_virt(CPUState
*env
, target_ulong virt
)
73 struct kvm_sregs sregs
;
75 kvm_get_sregs(kvm_context
, env
->cpu_index
, &sregs
);
76 return ldub_phys(map_addr(&sregs
, virt
, NULL
));
79 static void write_byte_virt(CPUState
*env
, target_ulong virt
, uint8_t b
)
81 struct kvm_sregs sregs
;
83 kvm_get_sregs(kvm_context
, env
->cpu_index
, &sregs
);
84 stb_phys(map_addr(&sregs
, virt
, NULL
), b
);
87 static __u64
kvm_rsp_read(CPUState
*env
)
91 kvm_get_regs(kvm_context
, env
->cpu_index
, ®s
);
104 struct vapic_patches
{
106 uint32_t set_tpr_eax
;
108 uint32_t get_tpr_stack
;
109 } __attribute__((packed
)) up
, mp
;
110 } __attribute__((packed
));
112 static struct vapic_bios vapic_bios
;
114 static uint32_t real_tpr
;
115 static uint32_t bios_addr
;
116 static uint32_t vapic_phys
;
117 static int bios_enabled
;
118 static uint32_t vbios_desc_phys
;
120 void update_vbios_real_tpr()
122 cpu_physical_memory_rw(vbios_desc_phys
, (void *)&vapic_bios
, sizeof vapic_bios
, 0);
123 vapic_bios
.real_tpr
= real_tpr
;
124 vapic_bios
.vcpu_shift
= 7;
125 cpu_physical_memory_rw(vbios_desc_phys
, (void *)&vapic_bios
, sizeof vapic_bios
, 1);
128 static unsigned modrm_reg(uint8_t modrm
)
130 return (modrm
>> 3) & 7;
133 static int is_abs_modrm(uint8_t modrm
)
135 return (modrm
& 0xc7) == 0x05;
138 static int instruction_is_ok(CPUState
*env
, uint64_t rip
, int is_write
)
141 unsigned addr_offset
;
145 if ((rip
& 0xf0000000) != 0x80000000 && (rip
& 0xf0000000) != 0xe0000000)
147 if (kvm_rsp_read(env
) == 0)
149 b1
= read_byte_virt(env
, rip
);
150 b2
= read_byte_virt(env
, rip
+ 1);
152 case 0xc7: /* mov imm32, r/m32 (c7/0) */
153 if (modrm_reg(b2
) != 0)
156 case 0x89: /* mov r32 to r/m32 */
157 case 0x8b: /* mov r/m32 to r32 */
158 if (!is_abs_modrm(b2
))
162 case 0xa1: /* mov abs to eax */
163 case 0xa3: /* mov eax to abs */
166 case 0xff: /* push r/m32 */
167 if (modrm_reg(b2
) != 6 || !is_abs_modrm(b2
))
173 p
= rip
+ addr_offset
;
174 addr
= read_byte_virt(env
, p
++);
175 addr
|= read_byte_virt(env
, p
++) << 8;
176 addr
|= read_byte_virt(env
, p
++) << 16;
177 addr
|= read_byte_virt(env
, p
++) << 24;
178 if ((addr
& 0xfff) != 0x80)
181 update_vbios_real_tpr();
185 static int bios_is_mapped(CPUState
*env
, uint64_t rip
)
189 struct kvm_sregs sregs
;
192 uint32_t offset
, fixup
;
197 kvm_get_sregs(kvm_context
, env
->cpu_index
, &sregs
);
199 probe
= (rip
& 0xf0000000) + 0xe0000;
200 phys
= map_addr(&sregs
, probe
, &perms
);
204 for (i
= 0; i
< 64; ++i
) {
205 cpu_physical_memory_read(phys
, (void *)&vapic_bios
, sizeof(vapic_bios
));
206 if (memcmp(vapic_bios
.signature
, "kvm aPiC", 8) == 0)
213 if (bios_addr
== vapic_bios
.virt_base
)
215 vbios_desc_phys
= phys
;
216 for (i
= vapic_bios
.fixup_start
; i
< vapic_bios
.fixup_end
; i
+= 4) {
217 offset
= ldl_phys(phys
+ i
- vapic_bios
.virt_base
);
218 fixup
= phys
+ offset
;
219 stl_phys(fixup
, ldl_phys(fixup
) + bios_addr
- vapic_bios
.virt_base
);
221 vapic_phys
= vapic_bios
.vapic
- vapic_bios
.virt_base
+ phys
;
225 static int enable_vapic(CPUState
*env
)
227 struct kvm_sregs sregs
;
228 static uint8_t one
= 1;
230 kvm_enable_vapic(kvm_context
, env
->cpu_index
,
231 vapic_phys
+ (env
->cpu_index
<< 7));
232 cpu_physical_memory_rw(vapic_phys
+ (env
->cpu_index
<< 7) + 4, &one
, 1, 1);
238 static void patch_call(CPUState
*env
, uint64_t rip
, uint32_t target
)
242 offset
= target
- vapic_bios
.virt_base
+ bios_addr
- rip
- 5;
243 write_byte_virt(env
, rip
, 0xe8); /* call near */
244 write_byte_virt(env
, rip
+ 1, offset
);
245 write_byte_virt(env
, rip
+ 2, offset
>> 8);
246 write_byte_virt(env
, rip
+ 3, offset
>> 16);
247 write_byte_virt(env
, rip
+ 4, offset
>> 24);
250 static void patch_instruction(CPUState
*env
, uint64_t rip
)
253 struct vapic_patches
*vp
;
255 vp
= smp_cpus
== 1 ? &vapic_bios
.up
: &vapic_bios
.mp
;
256 b1
= read_byte_virt(env
, rip
);
257 b2
= read_byte_virt(env
, rip
+ 1);
259 case 0x89: /* mov r32 to r/m32 */
260 write_byte_virt(env
, rip
, 0x50 + modrm_reg(b2
)); /* push reg */
261 patch_call(env
, rip
+ 1, vp
->set_tpr
);
263 case 0x8b: /* mov r/m32 to r32 */
264 write_byte_virt(env
, rip
, 0x90);
265 patch_call(env
, rip
+ 1, vp
->get_tpr
[modrm_reg(b2
)]);
267 case 0xa1: /* mov abs to eax */
268 patch_call(env
, rip
, vp
->get_tpr
[0]);
270 case 0xa3: /* mov eax to abs */
271 patch_call(env
, rip
, vp
->set_tpr_eax
);
273 case 0xc7: /* mov imm32, r/m32 (c7/0) */
274 write_byte_virt(env
, rip
, 0x68); /* push imm32 */
275 write_byte_virt(env
, rip
+ 1, read_byte_virt(env
, rip
+6));
276 write_byte_virt(env
, rip
+ 2, read_byte_virt(env
, rip
+7));
277 write_byte_virt(env
, rip
+ 3, read_byte_virt(env
, rip
+8));
278 write_byte_virt(env
, rip
+ 4, read_byte_virt(env
, rip
+9));
279 patch_call(env
, rip
+ 5, vp
->set_tpr
);
281 case 0xff: /* push r/m32 */
282 printf("patching push\n");
283 write_byte_virt(env
, rip
, 0x50); /* push eax */
284 patch_call(env
, rip
+ 1, vp
->get_tpr_stack
);
287 printf("funny insn %02x %02x\n", b1
, b2
);
291 void kvm_tpr_access_report(CPUState
*env
, uint64_t rip
, int is_write
)
293 if (!instruction_is_ok(env
, rip
, is_write
))
295 if (!bios_is_mapped(env
, rip
))
297 if (!enable_vapic(env
))
299 patch_instruction(env
, rip
);
302 void kvm_tpr_vcpu_start(CPUState
*env
)
304 kvm_enable_tpr_access_reporting(kvm_context
, env
->cpu_index
);
309 static void tpr_save(QEMUFile
*f
, void *s
)
313 for (i
= 0; i
< (sizeof vapic_bios
) / 4; ++i
)
314 qemu_put_be32s(f
, &((uint32_t *)&vapic_bios
)[i
]);
315 qemu_put_be32s(f
, &bios_enabled
);
316 qemu_put_be32s(f
, &real_tpr
);
317 qemu_put_be32s(f
, &bios_addr
);
318 qemu_put_be32s(f
, &vapic_phys
);
319 qemu_put_be32s(f
, &vbios_desc_phys
);
322 static int tpr_load(QEMUFile
*f
, void *s
, int version_id
)
329 for (i
= 0; i
< (sizeof vapic_bios
) / 4; ++i
)
330 qemu_get_be32s(f
, &((uint32_t *)&vapic_bios
)[i
]);
331 qemu_get_be32s(f
, &bios_enabled
);
332 qemu_get_be32s(f
, &real_tpr
);
333 qemu_get_be32s(f
, &bios_addr
);
334 qemu_get_be32s(f
, &vapic_phys
);
335 qemu_get_be32s(f
, &vbios_desc_phys
);
338 CPUState
*env
= first_cpu
->next_cpu
;
340 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
347 static void vtpr_ioport_write(void *opaque
, uint32_t addr
, uint32_t val
)
349 CPUState
*env
= cpu_single_env
;
350 struct kvm_regs regs
;
351 struct kvm_sregs sregs
;
354 kvm_get_regs(kvm_context
, env
->cpu_index
, ®s
);
356 write_byte_virt(env
, rip
, 0x66);
357 write_byte_virt(env
, rip
+ 1, 0x90);
360 if (!bios_is_mapped(env
, rip
))
361 printf("bios not mapped?\n");
362 kvm_get_sregs(kvm_context
, env
->cpu_index
, &sregs
);
363 for (addr
= 0xfffff000u
; addr
>= 0x80000000u
; addr
-= 4096)
364 if (map_addr(&sregs
, addr
, NULL
) == 0xfee00000u
) {
365 real_tpr
= addr
+ 0x80;
369 update_vbios_real_tpr();
373 void kvm_tpr_opt_setup(CPUState
*env
)
375 register_savevm("kvm-tpr-opt", 0, 1, tpr_save
, tpr_load
, NULL
);
376 register_ioport_write(0x7e, 1, 1, vtpr_ioport_write
, NULL
);