blob: 13bd4f5eec555015980302db082cd9d53edd12c5 [file] [log] [blame]
Blue Swirlf7b24292012-04-29 18:20:34 +00001/*
2 * x86 misc helpers
3 *
4 * Copyright (c) 2003 Fabrice Bellard
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "cpu.h"
Richard Henderson2ef61752014-04-07 22:31:41 -070021#include "exec/helper-proto.h"
Paolo Bonzinif08b6172014-03-28 19:42:10 +010022#include "exec/cpu_ldst.h"
Paolo Bonzini3f7d8462015-04-08 14:45:53 +020023#include "exec/address-spaces.h"
Blue Swirl92fc4b52012-04-29 20:35:48 +000024
Paolo Bonzini3f7d8462015-04-08 14:45:53 +020025void helper_outb(CPUX86State *env, uint32_t port, uint32_t data)
Blue Swirlf7b24292012-04-29 18:20:34 +000026{
Paolo Bonzini3f7d8462015-04-08 14:45:53 +020027#ifdef CONFIG_USER_ONLY
28 fprintf(stderr, "outb: port=0x%04x, data=%02x\n", port, data);
29#else
30 address_space_stb(&address_space_io, port, data,
31 cpu_get_mem_attrs(env), NULL);
32#endif
Blue Swirlf7b24292012-04-29 18:20:34 +000033}
34
Paolo Bonzini3f7d8462015-04-08 14:45:53 +020035target_ulong helper_inb(CPUX86State *env, uint32_t port)
Blue Swirlf7b24292012-04-29 18:20:34 +000036{
Paolo Bonzini3f7d8462015-04-08 14:45:53 +020037#ifdef CONFIG_USER_ONLY
38 fprintf(stderr, "inb: port=0x%04x\n", port);
39 return 0;
40#else
41 return address_space_ldub(&address_space_io, port,
42 cpu_get_mem_attrs(env), NULL);
43#endif
Blue Swirlf7b24292012-04-29 18:20:34 +000044}
45
Paolo Bonzini3f7d8462015-04-08 14:45:53 +020046void helper_outw(CPUX86State *env, uint32_t port, uint32_t data)
Blue Swirlf7b24292012-04-29 18:20:34 +000047{
Paolo Bonzini3f7d8462015-04-08 14:45:53 +020048#ifdef CONFIG_USER_ONLY
49 fprintf(stderr, "outw: port=0x%04x, data=%04x\n", port, data);
50#else
51 address_space_stw(&address_space_io, port, data,
52 cpu_get_mem_attrs(env), NULL);
53#endif
Blue Swirlf7b24292012-04-29 18:20:34 +000054}
55
Paolo Bonzini3f7d8462015-04-08 14:45:53 +020056target_ulong helper_inw(CPUX86State *env, uint32_t port)
Blue Swirlf7b24292012-04-29 18:20:34 +000057{
Paolo Bonzini3f7d8462015-04-08 14:45:53 +020058#ifdef CONFIG_USER_ONLY
59 fprintf(stderr, "inw: port=0x%04x\n", port);
60 return 0;
61#else
62 return address_space_lduw(&address_space_io, port,
63 cpu_get_mem_attrs(env), NULL);
64#endif
Blue Swirlf7b24292012-04-29 18:20:34 +000065}
66
Paolo Bonzini3f7d8462015-04-08 14:45:53 +020067void helper_outl(CPUX86State *env, uint32_t port, uint32_t data)
Blue Swirlf7b24292012-04-29 18:20:34 +000068{
Paolo Bonzini3f7d8462015-04-08 14:45:53 +020069#ifdef CONFIG_USER_ONLY
70 fprintf(stderr, "outw: port=0x%04x, data=%08x\n", port, data);
71#else
72 address_space_stl(&address_space_io, port, data,
73 cpu_get_mem_attrs(env), NULL);
74#endif
Blue Swirlf7b24292012-04-29 18:20:34 +000075}
76
Paolo Bonzini3f7d8462015-04-08 14:45:53 +020077target_ulong helper_inl(CPUX86State *env, uint32_t port)
Blue Swirlf7b24292012-04-29 18:20:34 +000078{
Paolo Bonzini3f7d8462015-04-08 14:45:53 +020079#ifdef CONFIG_USER_ONLY
80 fprintf(stderr, "inl: port=0x%04x\n", port);
81 return 0;
82#else
83 return address_space_ldl(&address_space_io, port,
84 cpu_get_mem_attrs(env), NULL);
85#endif
Blue Swirlf7b24292012-04-29 18:20:34 +000086}
87
Blue Swirl4a7443b2012-04-29 18:42:47 +000088void helper_into(CPUX86State *env, int next_eip_addend)
Blue Swirlf7b24292012-04-29 18:20:34 +000089{
90 int eflags;
91
Blue Swirlf0967a12012-04-29 12:45:34 +000092 eflags = cpu_cc_compute_all(env, CC_OP);
Blue Swirlf7b24292012-04-29 18:20:34 +000093 if (eflags & CC_O) {
94 raise_interrupt(env, EXCP04_INTO, 1, 0, next_eip_addend);
95 }
96}
97
Blue Swirl4a7443b2012-04-29 18:42:47 +000098void helper_cpuid(CPUX86State *env)
Blue Swirlf7b24292012-04-29 18:20:34 +000099{
100 uint32_t eax, ebx, ecx, edx;
101
102 cpu_svm_check_intercept_param(env, SVM_EXIT_CPUID, 0);
103
liguang90a25412013-05-28 16:21:10 +0800104 cpu_x86_cpuid(env, (uint32_t)env->regs[R_EAX], (uint32_t)env->regs[R_ECX],
105 &eax, &ebx, &ecx, &edx);
liguang4b34e3a2013-05-28 16:20:59 +0800106 env->regs[R_EAX] = eax;
liguang70b51362013-05-28 16:21:00 +0800107 env->regs[R_EBX] = ebx;
liguanga4165612013-05-28 16:21:01 +0800108 env->regs[R_ECX] = ecx;
liguang00f5e6f2013-05-28 16:21:02 +0800109 env->regs[R_EDX] = edx;
Blue Swirlf7b24292012-04-29 18:20:34 +0000110}
111
112#if defined(CONFIG_USER_ONLY)
Blue Swirl4a7443b2012-04-29 18:42:47 +0000113target_ulong helper_read_crN(CPUX86State *env, int reg)
Blue Swirlf7b24292012-04-29 18:20:34 +0000114{
115 return 0;
116}
117
Blue Swirl4a7443b2012-04-29 18:42:47 +0000118void helper_write_crN(CPUX86State *env, int reg, target_ulong t0)
Blue Swirlf7b24292012-04-29 18:20:34 +0000119{
120}
Blue Swirlf7b24292012-04-29 18:20:34 +0000121#else
Blue Swirl4a7443b2012-04-29 18:42:47 +0000122target_ulong helper_read_crN(CPUX86State *env, int reg)
Blue Swirlf7b24292012-04-29 18:20:34 +0000123{
124 target_ulong val;
125
126 cpu_svm_check_intercept_param(env, SVM_EXIT_READ_CR0 + reg, 0);
127 switch (reg) {
128 default:
129 val = env->cr[reg];
130 break;
131 case 8:
132 if (!(env->hflags2 & HF2_VINTR_MASK)) {
Chen Fan02e51482013-12-23 17:04:02 +0800133 val = cpu_get_apic_tpr(x86_env_get_cpu(env)->apic_state);
Blue Swirlf7b24292012-04-29 18:20:34 +0000134 } else {
135 val = env->v_tpr;
136 }
137 break;
138 }
139 return val;
140}
141
Blue Swirl4a7443b2012-04-29 18:42:47 +0000142void helper_write_crN(CPUX86State *env, int reg, target_ulong t0)
Blue Swirlf7b24292012-04-29 18:20:34 +0000143{
144 cpu_svm_check_intercept_param(env, SVM_EXIT_WRITE_CR0 + reg, 0);
145 switch (reg) {
146 case 0:
147 cpu_x86_update_cr0(env, t0);
148 break;
149 case 3:
150 cpu_x86_update_cr3(env, t0);
151 break;
152 case 4:
153 cpu_x86_update_cr4(env, t0);
154 break;
155 case 8:
156 if (!(env->hflags2 & HF2_VINTR_MASK)) {
Chen Fan02e51482013-12-23 17:04:02 +0800157 cpu_set_apic_tpr(x86_env_get_cpu(env)->apic_state, t0);
Blue Swirlf7b24292012-04-29 18:20:34 +0000158 }
159 env->v_tpr = t0 & 0x0f;
160 break;
161 default:
162 env->cr[reg] = t0;
163 break;
164 }
165}
Blue Swirlf7b24292012-04-29 18:20:34 +0000166#endif
167
Blue Swirl4a7443b2012-04-29 18:42:47 +0000168void helper_lmsw(CPUX86State *env, target_ulong t0)
Blue Swirlf7b24292012-04-29 18:20:34 +0000169{
170 /* only 4 lower bits of CR0 are modified. PE cannot be set to zero
171 if already set to one. */
172 t0 = (env->cr[0] & ~0xe) | (t0 & 0xf);
Blue Swirl4a7443b2012-04-29 18:42:47 +0000173 helper_write_crN(env, 0, t0);
Blue Swirlf7b24292012-04-29 18:20:34 +0000174}
175
Blue Swirl4a7443b2012-04-29 18:42:47 +0000176void helper_invlpg(CPUX86State *env, target_ulong addr)
Blue Swirlf7b24292012-04-29 18:20:34 +0000177{
Andreas Färber31b030d2013-09-04 01:29:02 +0200178 X86CPU *cpu = x86_env_get_cpu(env);
179
Blue Swirlf7b24292012-04-29 18:20:34 +0000180 cpu_svm_check_intercept_param(env, SVM_EXIT_INVLPG, 0);
Andreas Färber31b030d2013-09-04 01:29:02 +0200181 tlb_flush_page(CPU(cpu), addr);
Blue Swirlf7b24292012-04-29 18:20:34 +0000182}
183
Blue Swirl4a7443b2012-04-29 18:42:47 +0000184void helper_rdtsc(CPUX86State *env)
Blue Swirlf7b24292012-04-29 18:20:34 +0000185{
186 uint64_t val;
187
188 if ((env->cr[4] & CR4_TSD_MASK) && ((env->hflags & HF_CPL_MASK) != 0)) {
Pavel Dovgalyuk4054cde2015-07-10 12:57:41 +0300189 raise_exception_ra(env, EXCP0D_GPF, GETPC());
Blue Swirlf7b24292012-04-29 18:20:34 +0000190 }
191 cpu_svm_check_intercept_param(env, SVM_EXIT_RDTSC, 0);
192
193 val = cpu_get_tsc(env) + env->tsc_offset;
liguang4b34e3a2013-05-28 16:20:59 +0800194 env->regs[R_EAX] = (uint32_t)(val);
liguang00f5e6f2013-05-28 16:21:02 +0800195 env->regs[R_EDX] = (uint32_t)(val >> 32);
Blue Swirlf7b24292012-04-29 18:20:34 +0000196}
197
Blue Swirl4a7443b2012-04-29 18:42:47 +0000198void helper_rdtscp(CPUX86State *env)
Blue Swirlf7b24292012-04-29 18:20:34 +0000199{
Blue Swirl4a7443b2012-04-29 18:42:47 +0000200 helper_rdtsc(env);
liguanga4165612013-05-28 16:21:01 +0800201 env->regs[R_ECX] = (uint32_t)(env->tsc_aux);
Blue Swirlf7b24292012-04-29 18:20:34 +0000202}
203
Blue Swirl4a7443b2012-04-29 18:42:47 +0000204void helper_rdpmc(CPUX86State *env)
Blue Swirlf7b24292012-04-29 18:20:34 +0000205{
206 if ((env->cr[4] & CR4_PCE_MASK) && ((env->hflags & HF_CPL_MASK) != 0)) {
Pavel Dovgalyuk4054cde2015-07-10 12:57:41 +0300207 raise_exception_ra(env, EXCP0D_GPF, GETPC());
Blue Swirlf7b24292012-04-29 18:20:34 +0000208 }
209 cpu_svm_check_intercept_param(env, SVM_EXIT_RDPMC, 0);
210
211 /* currently unimplemented */
212 qemu_log_mask(LOG_UNIMP, "x86: unimplemented rdpmc\n");
213 raise_exception_err(env, EXCP06_ILLOP, 0);
214}
215
216#if defined(CONFIG_USER_ONLY)
Blue Swirl4a7443b2012-04-29 18:42:47 +0000217void helper_wrmsr(CPUX86State *env)
Blue Swirlf7b24292012-04-29 18:20:34 +0000218{
219}
220
Blue Swirl4a7443b2012-04-29 18:42:47 +0000221void helper_rdmsr(CPUX86State *env)
Blue Swirlf7b24292012-04-29 18:20:34 +0000222{
223}
224#else
Blue Swirl4a7443b2012-04-29 18:42:47 +0000225void helper_wrmsr(CPUX86State *env)
Blue Swirlf7b24292012-04-29 18:20:34 +0000226{
227 uint64_t val;
228
229 cpu_svm_check_intercept_param(env, SVM_EXIT_MSR, 1);
230
liguang90a25412013-05-28 16:21:10 +0800231 val = ((uint32_t)env->regs[R_EAX]) |
232 ((uint64_t)((uint32_t)env->regs[R_EDX]) << 32);
Blue Swirlf7b24292012-04-29 18:20:34 +0000233
liguanga4165612013-05-28 16:21:01 +0800234 switch ((uint32_t)env->regs[R_ECX]) {
Blue Swirlf7b24292012-04-29 18:20:34 +0000235 case MSR_IA32_SYSENTER_CS:
236 env->sysenter_cs = val & 0xffff;
237 break;
238 case MSR_IA32_SYSENTER_ESP:
239 env->sysenter_esp = val;
240 break;
241 case MSR_IA32_SYSENTER_EIP:
242 env->sysenter_eip = val;
243 break;
244 case MSR_IA32_APICBASE:
Chen Fan02e51482013-12-23 17:04:02 +0800245 cpu_set_apic_base(x86_env_get_cpu(env)->apic_state, val);
Blue Swirlf7b24292012-04-29 18:20:34 +0000246 break;
247 case MSR_EFER:
248 {
249 uint64_t update_mask;
250
251 update_mask = 0;
Eduardo Habkost0514ef22013-04-22 16:00:15 -0300252 if (env->features[FEAT_8000_0001_EDX] & CPUID_EXT2_SYSCALL) {
Blue Swirlf7b24292012-04-29 18:20:34 +0000253 update_mask |= MSR_EFER_SCE;
254 }
Eduardo Habkost0514ef22013-04-22 16:00:15 -0300255 if (env->features[FEAT_8000_0001_EDX] & CPUID_EXT2_LM) {
Blue Swirlf7b24292012-04-29 18:20:34 +0000256 update_mask |= MSR_EFER_LME;
257 }
Eduardo Habkost0514ef22013-04-22 16:00:15 -0300258 if (env->features[FEAT_8000_0001_EDX] & CPUID_EXT2_FFXSR) {
Blue Swirlf7b24292012-04-29 18:20:34 +0000259 update_mask |= MSR_EFER_FFXSR;
260 }
Eduardo Habkost0514ef22013-04-22 16:00:15 -0300261 if (env->features[FEAT_8000_0001_EDX] & CPUID_EXT2_NX) {
Blue Swirlf7b24292012-04-29 18:20:34 +0000262 update_mask |= MSR_EFER_NXE;
263 }
Eduardo Habkost0514ef22013-04-22 16:00:15 -0300264 if (env->features[FEAT_8000_0001_ECX] & CPUID_EXT3_SVM) {
Blue Swirlf7b24292012-04-29 18:20:34 +0000265 update_mask |= MSR_EFER_SVME;
266 }
Eduardo Habkost0514ef22013-04-22 16:00:15 -0300267 if (env->features[FEAT_8000_0001_EDX] & CPUID_EXT2_FFXSR) {
Blue Swirlf7b24292012-04-29 18:20:34 +0000268 update_mask |= MSR_EFER_FFXSR;
269 }
270 cpu_load_efer(env, (env->efer & ~update_mask) |
271 (val & update_mask));
272 }
273 break;
274 case MSR_STAR:
275 env->star = val;
276 break;
277 case MSR_PAT:
278 env->pat = val;
279 break;
280 case MSR_VM_HSAVE_PA:
281 env->vm_hsave = val;
282 break;
283#ifdef TARGET_X86_64
284 case MSR_LSTAR:
285 env->lstar = val;
286 break;
287 case MSR_CSTAR:
288 env->cstar = val;
289 break;
290 case MSR_FMASK:
291 env->fmask = val;
292 break;
293 case MSR_FSBASE:
294 env->segs[R_FS].base = val;
295 break;
296 case MSR_GSBASE:
297 env->segs[R_GS].base = val;
298 break;
299 case MSR_KERNELGSBASE:
300 env->kernelgsbase = val;
301 break;
302#endif
303 case MSR_MTRRphysBase(0):
304 case MSR_MTRRphysBase(1):
305 case MSR_MTRRphysBase(2):
306 case MSR_MTRRphysBase(3):
307 case MSR_MTRRphysBase(4):
308 case MSR_MTRRphysBase(5):
309 case MSR_MTRRphysBase(6):
310 case MSR_MTRRphysBase(7):
liguang90a25412013-05-28 16:21:10 +0800311 env->mtrr_var[((uint32_t)env->regs[R_ECX] -
312 MSR_MTRRphysBase(0)) / 2].base = val;
Blue Swirlf7b24292012-04-29 18:20:34 +0000313 break;
314 case MSR_MTRRphysMask(0):
315 case MSR_MTRRphysMask(1):
316 case MSR_MTRRphysMask(2):
317 case MSR_MTRRphysMask(3):
318 case MSR_MTRRphysMask(4):
319 case MSR_MTRRphysMask(5):
320 case MSR_MTRRphysMask(6):
321 case MSR_MTRRphysMask(7):
liguang90a25412013-05-28 16:21:10 +0800322 env->mtrr_var[((uint32_t)env->regs[R_ECX] -
323 MSR_MTRRphysMask(0)) / 2].mask = val;
Blue Swirlf7b24292012-04-29 18:20:34 +0000324 break;
325 case MSR_MTRRfix64K_00000:
liguang90a25412013-05-28 16:21:10 +0800326 env->mtrr_fixed[(uint32_t)env->regs[R_ECX] -
327 MSR_MTRRfix64K_00000] = val;
Blue Swirlf7b24292012-04-29 18:20:34 +0000328 break;
329 case MSR_MTRRfix16K_80000:
330 case MSR_MTRRfix16K_A0000:
liguang90a25412013-05-28 16:21:10 +0800331 env->mtrr_fixed[(uint32_t)env->regs[R_ECX] -
332 MSR_MTRRfix16K_80000 + 1] = val;
Blue Swirlf7b24292012-04-29 18:20:34 +0000333 break;
334 case MSR_MTRRfix4K_C0000:
335 case MSR_MTRRfix4K_C8000:
336 case MSR_MTRRfix4K_D0000:
337 case MSR_MTRRfix4K_D8000:
338 case MSR_MTRRfix4K_E0000:
339 case MSR_MTRRfix4K_E8000:
340 case MSR_MTRRfix4K_F0000:
341 case MSR_MTRRfix4K_F8000:
liguang90a25412013-05-28 16:21:10 +0800342 env->mtrr_fixed[(uint32_t)env->regs[R_ECX] -
343 MSR_MTRRfix4K_C0000 + 3] = val;
Blue Swirlf7b24292012-04-29 18:20:34 +0000344 break;
345 case MSR_MTRRdefType:
346 env->mtrr_deftype = val;
347 break;
348 case MSR_MCG_STATUS:
349 env->mcg_status = val;
350 break;
351 case MSR_MCG_CTL:
352 if ((env->mcg_cap & MCG_CTL_P)
353 && (val == 0 || val == ~(uint64_t)0)) {
354 env->mcg_ctl = val;
355 }
356 break;
357 case MSR_TSC_AUX:
358 env->tsc_aux = val;
359 break;
360 case MSR_IA32_MISC_ENABLE:
361 env->msr_ia32_misc_enable = val;
362 break;
363 default:
liguanga4165612013-05-28 16:21:01 +0800364 if ((uint32_t)env->regs[R_ECX] >= MSR_MC0_CTL
liguang90a25412013-05-28 16:21:10 +0800365 && (uint32_t)env->regs[R_ECX] < MSR_MC0_CTL +
366 (4 * env->mcg_cap & 0xff)) {
liguanga4165612013-05-28 16:21:01 +0800367 uint32_t offset = (uint32_t)env->regs[R_ECX] - MSR_MC0_CTL;
Blue Swirlf7b24292012-04-29 18:20:34 +0000368 if ((offset & 0x3) != 0
369 || (val == 0 || val == ~(uint64_t)0)) {
370 env->mce_banks[offset] = val;
371 }
372 break;
373 }
374 /* XXX: exception? */
375 break;
376 }
377}
378
Blue Swirl4a7443b2012-04-29 18:42:47 +0000379void helper_rdmsr(CPUX86State *env)
Blue Swirlf7b24292012-04-29 18:20:34 +0000380{
381 uint64_t val;
382
383 cpu_svm_check_intercept_param(env, SVM_EXIT_MSR, 0);
384
liguanga4165612013-05-28 16:21:01 +0800385 switch ((uint32_t)env->regs[R_ECX]) {
Blue Swirlf7b24292012-04-29 18:20:34 +0000386 case MSR_IA32_SYSENTER_CS:
387 val = env->sysenter_cs;
388 break;
389 case MSR_IA32_SYSENTER_ESP:
390 val = env->sysenter_esp;
391 break;
392 case MSR_IA32_SYSENTER_EIP:
393 val = env->sysenter_eip;
394 break;
395 case MSR_IA32_APICBASE:
Chen Fan02e51482013-12-23 17:04:02 +0800396 val = cpu_get_apic_base(x86_env_get_cpu(env)->apic_state);
Blue Swirlf7b24292012-04-29 18:20:34 +0000397 break;
398 case MSR_EFER:
399 val = env->efer;
400 break;
401 case MSR_STAR:
402 val = env->star;
403 break;
404 case MSR_PAT:
405 val = env->pat;
406 break;
407 case MSR_VM_HSAVE_PA:
408 val = env->vm_hsave;
409 break;
410 case MSR_IA32_PERF_STATUS:
411 /* tsc_increment_by_tick */
412 val = 1000ULL;
413 /* CPU multiplier */
414 val |= (((uint64_t)4ULL) << 40);
415 break;
416#ifdef TARGET_X86_64
417 case MSR_LSTAR:
418 val = env->lstar;
419 break;
420 case MSR_CSTAR:
421 val = env->cstar;
422 break;
423 case MSR_FMASK:
424 val = env->fmask;
425 break;
426 case MSR_FSBASE:
427 val = env->segs[R_FS].base;
428 break;
429 case MSR_GSBASE:
430 val = env->segs[R_GS].base;
431 break;
432 case MSR_KERNELGSBASE:
433 val = env->kernelgsbase;
434 break;
435 case MSR_TSC_AUX:
436 val = env->tsc_aux;
437 break;
438#endif
439 case MSR_MTRRphysBase(0):
440 case MSR_MTRRphysBase(1):
441 case MSR_MTRRphysBase(2):
442 case MSR_MTRRphysBase(3):
443 case MSR_MTRRphysBase(4):
444 case MSR_MTRRphysBase(5):
445 case MSR_MTRRphysBase(6):
446 case MSR_MTRRphysBase(7):
liguang90a25412013-05-28 16:21:10 +0800447 val = env->mtrr_var[((uint32_t)env->regs[R_ECX] -
448 MSR_MTRRphysBase(0)) / 2].base;
Blue Swirlf7b24292012-04-29 18:20:34 +0000449 break;
450 case MSR_MTRRphysMask(0):
451 case MSR_MTRRphysMask(1):
452 case MSR_MTRRphysMask(2):
453 case MSR_MTRRphysMask(3):
454 case MSR_MTRRphysMask(4):
455 case MSR_MTRRphysMask(5):
456 case MSR_MTRRphysMask(6):
457 case MSR_MTRRphysMask(7):
liguang90a25412013-05-28 16:21:10 +0800458 val = env->mtrr_var[((uint32_t)env->regs[R_ECX] -
459 MSR_MTRRphysMask(0)) / 2].mask;
Blue Swirlf7b24292012-04-29 18:20:34 +0000460 break;
461 case MSR_MTRRfix64K_00000:
462 val = env->mtrr_fixed[0];
463 break;
464 case MSR_MTRRfix16K_80000:
465 case MSR_MTRRfix16K_A0000:
liguang90a25412013-05-28 16:21:10 +0800466 val = env->mtrr_fixed[(uint32_t)env->regs[R_ECX] -
467 MSR_MTRRfix16K_80000 + 1];
Blue Swirlf7b24292012-04-29 18:20:34 +0000468 break;
469 case MSR_MTRRfix4K_C0000:
470 case MSR_MTRRfix4K_C8000:
471 case MSR_MTRRfix4K_D0000:
472 case MSR_MTRRfix4K_D8000:
473 case MSR_MTRRfix4K_E0000:
474 case MSR_MTRRfix4K_E8000:
475 case MSR_MTRRfix4K_F0000:
476 case MSR_MTRRfix4K_F8000:
liguang90a25412013-05-28 16:21:10 +0800477 val = env->mtrr_fixed[(uint32_t)env->regs[R_ECX] -
478 MSR_MTRRfix4K_C0000 + 3];
Blue Swirlf7b24292012-04-29 18:20:34 +0000479 break;
480 case MSR_MTRRdefType:
481 val = env->mtrr_deftype;
482 break;
483 case MSR_MTRRcap:
Eduardo Habkost0514ef22013-04-22 16:00:15 -0300484 if (env->features[FEAT_1_EDX] & CPUID_MTRR) {
Blue Swirlf7b24292012-04-29 18:20:34 +0000485 val = MSR_MTRRcap_VCNT | MSR_MTRRcap_FIXRANGE_SUPPORT |
486 MSR_MTRRcap_WC_SUPPORTED;
487 } else {
488 /* XXX: exception? */
489 val = 0;
490 }
491 break;
492 case MSR_MCG_CAP:
493 val = env->mcg_cap;
494 break;
495 case MSR_MCG_CTL:
496 if (env->mcg_cap & MCG_CTL_P) {
497 val = env->mcg_ctl;
498 } else {
499 val = 0;
500 }
501 break;
502 case MSR_MCG_STATUS:
503 val = env->mcg_status;
504 break;
505 case MSR_IA32_MISC_ENABLE:
506 val = env->msr_ia32_misc_enable;
507 break;
508 default:
liguanga4165612013-05-28 16:21:01 +0800509 if ((uint32_t)env->regs[R_ECX] >= MSR_MC0_CTL
liguang90a25412013-05-28 16:21:10 +0800510 && (uint32_t)env->regs[R_ECX] < MSR_MC0_CTL +
511 (4 * env->mcg_cap & 0xff)) {
liguanga4165612013-05-28 16:21:01 +0800512 uint32_t offset = (uint32_t)env->regs[R_ECX] - MSR_MC0_CTL;
Blue Swirlf7b24292012-04-29 18:20:34 +0000513 val = env->mce_banks[offset];
514 break;
515 }
516 /* XXX: exception? */
517 val = 0;
518 break;
519 }
liguang4b34e3a2013-05-28 16:20:59 +0800520 env->regs[R_EAX] = (uint32_t)(val);
liguang00f5e6f2013-05-28 16:21:02 +0800521 env->regs[R_EDX] = (uint32_t)(val >> 32);
Blue Swirlf7b24292012-04-29 18:20:34 +0000522}
523#endif
524
Paolo Bonzini81f30532013-11-20 12:54:02 +0100525static void do_pause(X86CPU *cpu)
526{
Andreas Färber27103422013-08-26 08:31:06 +0200527 CPUState *cs = CPU(cpu);
Paolo Bonzini81f30532013-11-20 12:54:02 +0100528
529 /* Just let another CPU run. */
Andreas Färber27103422013-08-26 08:31:06 +0200530 cs->exception_index = EXCP_INTERRUPT;
Andreas Färber5638d182013-08-27 17:52:12 +0200531 cpu_loop_exit(cs);
Paolo Bonzini81f30532013-11-20 12:54:02 +0100532}
533
Andreas Färber259186a2013-01-17 18:51:17 +0100534static void do_hlt(X86CPU *cpu)
Blue Swirlf7b24292012-04-29 18:20:34 +0000535{
Andreas Färber259186a2013-01-17 18:51:17 +0100536 CPUState *cs = CPU(cpu);
537 CPUX86State *env = &cpu->env;
538
Blue Swirlf7b24292012-04-29 18:20:34 +0000539 env->hflags &= ~HF_INHIBIT_IRQ_MASK; /* needed if sti is just before */
Andreas Färber259186a2013-01-17 18:51:17 +0100540 cs->halted = 1;
Andreas Färber27103422013-08-26 08:31:06 +0200541 cs->exception_index = EXCP_HLT;
Andreas Färber5638d182013-08-27 17:52:12 +0200542 cpu_loop_exit(cs);
Blue Swirlf7b24292012-04-29 18:20:34 +0000543}
544
Blue Swirl4a7443b2012-04-29 18:42:47 +0000545void helper_hlt(CPUX86State *env, int next_eip_addend)
Blue Swirlf7b24292012-04-29 18:20:34 +0000546{
Andreas Färber259186a2013-01-17 18:51:17 +0100547 X86CPU *cpu = x86_env_get_cpu(env);
548
Blue Swirlf7b24292012-04-29 18:20:34 +0000549 cpu_svm_check_intercept_param(env, SVM_EXIT_HLT, 0);
liguanga78d0ea2013-05-28 16:21:07 +0800550 env->eip += next_eip_addend;
Blue Swirlf7b24292012-04-29 18:20:34 +0000551
Andreas Färber259186a2013-01-17 18:51:17 +0100552 do_hlt(cpu);
Blue Swirlf7b24292012-04-29 18:20:34 +0000553}
554
Blue Swirl4a7443b2012-04-29 18:42:47 +0000555void helper_monitor(CPUX86State *env, target_ulong ptr)
Blue Swirlf7b24292012-04-29 18:20:34 +0000556{
liguanga4165612013-05-28 16:21:01 +0800557 if ((uint32_t)env->regs[R_ECX] != 0) {
Pavel Dovgalyuk4054cde2015-07-10 12:57:41 +0300558 raise_exception_ra(env, EXCP0D_GPF, GETPC());
Blue Swirlf7b24292012-04-29 18:20:34 +0000559 }
560 /* XXX: store address? */
561 cpu_svm_check_intercept_param(env, SVM_EXIT_MONITOR, 0);
562}
563
Blue Swirl4a7443b2012-04-29 18:42:47 +0000564void helper_mwait(CPUX86State *env, int next_eip_addend)
Blue Swirlf7b24292012-04-29 18:20:34 +0000565{
Andreas Färber259186a2013-01-17 18:51:17 +0100566 CPUState *cs;
567 X86CPU *cpu;
Andreas Färber55e5c282012-12-17 06:18:02 +0100568
liguanga4165612013-05-28 16:21:01 +0800569 if ((uint32_t)env->regs[R_ECX] != 0) {
Pavel Dovgalyuk4054cde2015-07-10 12:57:41 +0300570 raise_exception_ra(env, EXCP0D_GPF, GETPC());
Blue Swirlf7b24292012-04-29 18:20:34 +0000571 }
572 cpu_svm_check_intercept_param(env, SVM_EXIT_MWAIT, 0);
liguanga78d0ea2013-05-28 16:21:07 +0800573 env->eip += next_eip_addend;
Blue Swirlf7b24292012-04-29 18:20:34 +0000574
Andreas Färber259186a2013-01-17 18:51:17 +0100575 cpu = x86_env_get_cpu(env);
576 cs = CPU(cpu);
Blue Swirlf7b24292012-04-29 18:20:34 +0000577 /* XXX: not complete but not completely erroneous */
Andreas Färberbdc44642013-06-24 23:50:24 +0200578 if (cs->cpu_index != 0 || CPU_NEXT(cs) != NULL) {
Paolo Bonzini81f30532013-11-20 12:54:02 +0100579 do_pause(cpu);
Blue Swirlf7b24292012-04-29 18:20:34 +0000580 } else {
Andreas Färber259186a2013-01-17 18:51:17 +0100581 do_hlt(cpu);
Blue Swirlf7b24292012-04-29 18:20:34 +0000582 }
583}
584
Paolo Bonzini81f30532013-11-20 12:54:02 +0100585void helper_pause(CPUX86State *env, int next_eip_addend)
586{
587 X86CPU *cpu = x86_env_get_cpu(env);
588
589 cpu_svm_check_intercept_param(env, SVM_EXIT_PAUSE, 0);
590 env->eip += next_eip_addend;
591
592 do_pause(cpu);
593}
594
Blue Swirl4a7443b2012-04-29 18:42:47 +0000595void helper_debug(CPUX86State *env)
Blue Swirlf7b24292012-04-29 18:20:34 +0000596{
Andreas Färber27103422013-08-26 08:31:06 +0200597 CPUState *cs = CPU(x86_env_get_cpu(env));
598
599 cs->exception_index = EXCP_DEBUG;
Andreas Färber5638d182013-08-27 17:52:12 +0200600 cpu_loop_exit(cs);
Blue Swirlf7b24292012-04-29 18:20:34 +0000601}