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"
21 extern kvm_context_t kvm_context
;
23 static uint64_t map_addr(struct kvm_sregs
*sregs
, target_ulong virt
, unsigned *perms
)
25 uint64_t mask
= ((1ull << 48) - 1) & ~4095ull;
29 if (sregs
->cr4
& 0x20) {
31 p
= ldq_phys(p
+ 8 * (virt
>> 30));
35 p
= ldq_phys(p
+ 8 * ((virt
>> 21) & 511));
40 p
+= ((virt
>> 12) & 511) << 12;
43 p
= ldq_phys(p
+ 8 * ((virt
>> 12) & 511));
50 p
= ldl_phys(p
+ 4 * ((virt
>> 22) & 1023));
55 p
+= ((virt
>> 12) & 1023) << 12;
58 p
= ldl_phys(p
+ 4 * ((virt
>> 12) & 1023));
67 return p
+ (virt
& 4095);
70 static uint8_t read_byte_virt(CPUState
*env
, target_ulong virt
)
72 struct kvm_sregs sregs
;
74 kvm_get_sregs(kvm_context
, env
->cpu_index
, &sregs
);
75 return ldub_phys(map_addr(&sregs
, virt
, NULL
));
78 static void write_byte_virt(CPUState
*env
, target_ulong virt
, uint8_t b
)
80 struct kvm_sregs sregs
;
82 kvm_get_sregs(kvm_context
, env
->cpu_index
, &sregs
);
83 stb_phys(map_addr(&sregs
, virt
, NULL
), b
);
95 struct vapic_patches
{
99 } __attribute__((packed
)) up
, mp
;
100 } __attribute__((packed
));
102 static struct vapic_bios vapic_bios
;
104 static uint32_t real_tpr
;
105 static uint32_t bios_addr
;
106 static uint32_t vapic_phys
;
107 static int bios_enabled
;
108 static uint32_t vbios_desc_phys
;
110 void update_vbios_real_tpr()
112 cpu_physical_memory_rw(vbios_desc_phys
, (void *)&vapic_bios
, sizeof vapic_bios
, 0);
113 vapic_bios
.real_tpr
= real_tpr
;
114 vapic_bios
.vcpu_shift
= 7;
115 cpu_physical_memory_rw(vbios_desc_phys
, (void *)&vapic_bios
, sizeof vapic_bios
, 1);
118 static unsigned modrm_reg(uint8_t modrm
)
120 return (modrm
>> 3) & 7;
123 static int is_abs_modrm(uint8_t modrm
)
125 return (modrm
& 0xc7) == 0x05;
128 static int instruction_is_ok(CPUState
*env
, uint64_t rip
, int is_write
)
131 unsigned addr_offset
;
135 if ((rip
& 0xf0000000) != 0x80000000 && (rip
& 0xf0000000) != 0xe0000000)
137 b1
= read_byte_virt(env
, rip
);
138 b2
= read_byte_virt(env
, rip
+ 1);
140 case 0xc7: /* mov imm32, r/m32 (c7/0) */
141 if (modrm_reg(b2
) != 0)
144 case 0x89: /* mov r32 to r/m32 */
145 case 0x8b: /* mov r/m32 to r32 */
146 if (!is_abs_modrm(b2
))
150 case 0xa1: /* mov abs to eax */
151 case 0xa3: /* mov eax to abs */
157 p
= rip
+ addr_offset
;
158 addr
= read_byte_virt(env
, p
++);
159 addr
|= read_byte_virt(env
, p
++) << 8;
160 addr
|= read_byte_virt(env
, p
++) << 16;
161 addr
|= read_byte_virt(env
, p
++) << 24;
162 if ((addr
& 0xfff) != 0x80)
165 update_vbios_real_tpr();
169 static int bios_is_mapped(CPUState
*env
, uint64_t rip
)
173 struct kvm_sregs sregs
;
176 uint32_t offset
, fixup
;
181 kvm_get_sregs(kvm_context
, env
->cpu_index
, &sregs
);
183 probe
= (rip
& 0xf0000000) + 0xe0000;
184 phys
= map_addr(&sregs
, probe
, &perms
);
188 for (i
= 0; i
< 64; ++i
) {
189 cpu_physical_memory_read(phys
, (void *)&vapic_bios
, sizeof(vapic_bios
));
190 if (memcmp(vapic_bios
.signature
, "kvm aPiC", 8) == 0)
197 if (bios_addr
== vapic_bios
.virt_base
)
199 vbios_desc_phys
= phys
;
200 for (i
= vapic_bios
.fixup_start
; i
< vapic_bios
.fixup_end
; i
+= 4) {
201 offset
= ldl_phys(phys
+ i
- vapic_bios
.virt_base
);
202 fixup
= phys
+ offset
;
203 stl_phys(fixup
, ldl_phys(fixup
) + bios_addr
- vapic_bios
.virt_base
);
205 vapic_phys
= vapic_bios
.vapic
- vapic_bios
.virt_base
+ phys
;
209 static int enable_vapic(CPUState
*env
)
211 struct kvm_sregs sregs
;
213 if (smp_cpus
> 1) {/* uniprocessor doesn't need cpu id */
214 kvm_get_sregs(kvm_context
, env
->cpu_index
, &sregs
);
215 sregs
.tr
.selector
= 0xdb + (env
->cpu_index
<< 8);
216 kvm_set_sregs(kvm_context
, env
->cpu_index
, &sregs
);
219 kvm_enable_vapic(kvm_context
, env
->cpu_index
,
220 vapic_phys
+ (env
->cpu_index
<< 7));
226 static void patch_call(CPUState
*env
, uint64_t rip
, uint32_t target
)
230 offset
= target
- vapic_bios
.virt_base
+ bios_addr
- rip
- 5;
231 write_byte_virt(env
, rip
, 0xe8); /* call near */
232 write_byte_virt(env
, rip
+ 1, offset
);
233 write_byte_virt(env
, rip
+ 2, offset
>> 8);
234 write_byte_virt(env
, rip
+ 3, offset
>> 16);
235 write_byte_virt(env
, rip
+ 4, offset
>> 24);
238 static void patch_instruction(CPUState
*env
, uint64_t rip
)
241 struct vapic_patches
*vp
;
243 vp
= smp_cpus
== 1 ? &vapic_bios
.up
: &vapic_bios
.mp
;
244 b1
= read_byte_virt(env
, rip
);
245 b2
= read_byte_virt(env
, rip
+ 1);
247 case 0x89: /* mov r32 to r/m32 */
248 write_byte_virt(env
, rip
, 0x50 + modrm_reg(b2
)); /* push reg */
249 patch_call(env
, rip
+ 1, vp
->set_tpr
);
251 case 0x8b: /* mov r/m32 to r32 */
252 write_byte_virt(env
, rip
, 0x90);
253 patch_call(env
, rip
+ 1, vp
->get_tpr
[modrm_reg(b2
)]);
255 case 0xa1: /* mov abs to eax */
256 patch_call(env
, rip
, vp
->get_tpr
[0]);
258 case 0xa3: /* mov eax to abs */
259 patch_call(env
, rip
, vp
->set_tpr_eax
);
261 case 0xc7: /* mov imm32, r/m32 (c7/0) */
262 write_byte_virt(env
, rip
, 0x68); /* push imm32 */
263 write_byte_virt(env
, rip
+ 1, read_byte_virt(env
, rip
+6));
264 write_byte_virt(env
, rip
+ 2, read_byte_virt(env
, rip
+7));
265 write_byte_virt(env
, rip
+ 3, read_byte_virt(env
, rip
+8));
266 write_byte_virt(env
, rip
+ 4, read_byte_virt(env
, rip
+9));
267 patch_call(env
, rip
+ 5, vp
->set_tpr
);
270 printf("funny insn %02x %02x\n", b1
, b2
);
274 void kvm_tpr_access_report(CPUState
*env
, uint64_t rip
, int is_write
)
276 if (!instruction_is_ok(env
, rip
, is_write
))
278 if (!bios_is_mapped(env
, rip
))
280 if (!enable_vapic(env
))
282 patch_instruction(env
, rip
);
285 void kvm_tpr_vcpu_start(CPUState
*env
)
287 kvm_enable_tpr_access_reporting(kvm_context
, env
->cpu_index
);
292 static void tpr_save(QEMUFile
*f
, void *s
)
296 for (i
= 0; i
< (sizeof vapic_bios
) / 4; ++i
)
297 qemu_put_be32s(f
, &((uint32_t *)&vapic_bios
)[i
]);
298 qemu_put_be32s(f
, &bios_enabled
);
299 qemu_put_be32s(f
, &real_tpr
);
300 qemu_put_be32s(f
, &bios_addr
);
301 qemu_put_be32s(f
, &vapic_phys
);
302 qemu_put_be32s(f
, &vbios_desc_phys
);
305 static int tpr_load(QEMUFile
*f
, void *s
, int version_id
)
312 for (i
= 0; i
< (sizeof vapic_bios
) / 4; ++i
)
313 qemu_get_be32s(f
, &((uint32_t *)&vapic_bios
)[i
]);
314 qemu_get_be32s(f
, &bios_enabled
);
315 qemu_get_be32s(f
, &real_tpr
);
316 qemu_get_be32s(f
, &bios_addr
);
317 qemu_get_be32s(f
, &vapic_phys
);
318 qemu_get_be32s(f
, &vbios_desc_phys
);
321 CPUState
*env
= first_cpu
->next_cpu
;
323 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
330 void kvm_tpr_opt_setup(CPUState
*env
)
332 register_savevm("kvm-tpr-opt", 0, 1, tpr_save
, tpr_load
, NULL
);