4 * Copyright (c) 2003 Fabrice Bellard
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.
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.
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/>.
20 #include "qemu/osdep.h"
21 #include "qemu/main-loop.h"
23 #include "exec/helper-proto.h"
24 #include "exec/exec-all.h"
25 #include "exec/cpu_ldst.h"
26 #include "exec/address-spaces.h"
28 void helper_outb(CPUX86State
*env
, uint32_t port
, uint32_t data
)
30 #ifdef CONFIG_USER_ONLY
31 fprintf(stderr
, "outb: port=0x%04x, data=%02x\n", port
, data
);
33 address_space_stb(&address_space_io
, port
, data
,
34 cpu_get_mem_attrs(env
), NULL
);
38 target_ulong
helper_inb(CPUX86State
*env
, uint32_t port
)
40 #ifdef CONFIG_USER_ONLY
41 fprintf(stderr
, "inb: port=0x%04x\n", port
);
44 return address_space_ldub(&address_space_io
, port
,
45 cpu_get_mem_attrs(env
), NULL
);
49 void helper_outw(CPUX86State
*env
, uint32_t port
, uint32_t data
)
51 #ifdef CONFIG_USER_ONLY
52 fprintf(stderr
, "outw: port=0x%04x, data=%04x\n", port
, data
);
54 address_space_stw(&address_space_io
, port
, data
,
55 cpu_get_mem_attrs(env
), NULL
);
59 target_ulong
helper_inw(CPUX86State
*env
, uint32_t port
)
61 #ifdef CONFIG_USER_ONLY
62 fprintf(stderr
, "inw: port=0x%04x\n", port
);
65 return address_space_lduw(&address_space_io
, port
,
66 cpu_get_mem_attrs(env
), NULL
);
70 void helper_outl(CPUX86State
*env
, uint32_t port
, uint32_t data
)
72 #ifdef CONFIG_USER_ONLY
73 fprintf(stderr
, "outl: port=0x%04x, data=%08x\n", port
, data
);
75 address_space_stl(&address_space_io
, port
, data
,
76 cpu_get_mem_attrs(env
), NULL
);
80 target_ulong
helper_inl(CPUX86State
*env
, uint32_t port
)
82 #ifdef CONFIG_USER_ONLY
83 fprintf(stderr
, "inl: port=0x%04x\n", port
);
86 return address_space_ldl(&address_space_io
, port
,
87 cpu_get_mem_attrs(env
), NULL
);
91 void helper_into(CPUX86State
*env
, int next_eip_addend
)
95 eflags
= cpu_cc_compute_all(env
, CC_OP
);
97 raise_interrupt(env
, EXCP04_INTO
, 1, 0, next_eip_addend
);
101 void helper_cpuid(CPUX86State
*env
)
103 uint32_t eax
, ebx
, ecx
, edx
;
105 cpu_svm_check_intercept_param(env
, SVM_EXIT_CPUID
, 0, GETPC());
107 cpu_x86_cpuid(env
, (uint32_t)env
->regs
[R_EAX
], (uint32_t)env
->regs
[R_ECX
],
108 &eax
, &ebx
, &ecx
, &edx
);
109 env
->regs
[R_EAX
] = eax
;
110 env
->regs
[R_EBX
] = ebx
;
111 env
->regs
[R_ECX
] = ecx
;
112 env
->regs
[R_EDX
] = edx
;
115 #if defined(CONFIG_USER_ONLY)
116 target_ulong
helper_read_crN(CPUX86State
*env
, int reg
)
121 void helper_write_crN(CPUX86State
*env
, int reg
, target_ulong t0
)
125 target_ulong
helper_read_crN(CPUX86State
*env
, int reg
)
129 cpu_svm_check_intercept_param(env
, SVM_EXIT_READ_CR0
+ reg
, 0, GETPC());
135 if (!(env
->hflags2
& HF2_VINTR_MASK
)) {
136 val
= cpu_get_apic_tpr(env_archcpu(env
)->apic_state
);
145 void helper_write_crN(CPUX86State
*env
, int reg
, target_ulong t0
)
147 cpu_svm_check_intercept_param(env
, SVM_EXIT_WRITE_CR0
+ reg
, 0, GETPC());
150 cpu_x86_update_cr0(env
, t0
);
153 cpu_x86_update_cr3(env
, t0
);
156 cpu_x86_update_cr4(env
, t0
);
159 if (!(env
->hflags2
& HF2_VINTR_MASK
)) {
160 qemu_mutex_lock_iothread();
161 cpu_set_apic_tpr(env_archcpu(env
)->apic_state
, t0
);
162 qemu_mutex_unlock_iothread();
164 env
->v_tpr
= t0
& 0x0f;
173 void helper_lmsw(CPUX86State
*env
, target_ulong t0
)
175 /* only 4 lower bits of CR0 are modified. PE cannot be set to zero
176 if already set to one. */
177 t0
= (env
->cr
[0] & ~0xe) | (t0
& 0xf);
178 helper_write_crN(env
, 0, t0
);
181 void helper_invlpg(CPUX86State
*env
, target_ulong addr
)
183 X86CPU
*cpu
= env_archcpu(env
);
185 cpu_svm_check_intercept_param(env
, SVM_EXIT_INVLPG
, 0, GETPC());
186 tlb_flush_page(CPU(cpu
), addr
);
189 void helper_rdtsc(CPUX86State
*env
)
193 if ((env
->cr
[4] & CR4_TSD_MASK
) && ((env
->hflags
& HF_CPL_MASK
) != 0)) {
194 raise_exception_ra(env
, EXCP0D_GPF
, GETPC());
196 cpu_svm_check_intercept_param(env
, SVM_EXIT_RDTSC
, 0, GETPC());
198 val
= cpu_get_tsc(env
) + env
->tsc_offset
;
199 env
->regs
[R_EAX
] = (uint32_t)(val
);
200 env
->regs
[R_EDX
] = (uint32_t)(val
>> 32);
203 void helper_rdtscp(CPUX86State
*env
)
206 env
->regs
[R_ECX
] = (uint32_t)(env
->tsc_aux
);
209 void helper_rdpmc(CPUX86State
*env
)
211 if ((env
->cr
[4] & CR4_PCE_MASK
) && ((env
->hflags
& HF_CPL_MASK
) != 0)) {
212 raise_exception_ra(env
, EXCP0D_GPF
, GETPC());
214 cpu_svm_check_intercept_param(env
, SVM_EXIT_RDPMC
, 0, GETPC());
216 /* currently unimplemented */
217 qemu_log_mask(LOG_UNIMP
, "x86: unimplemented rdpmc\n");
218 raise_exception_err(env
, EXCP06_ILLOP
, 0);
221 #if defined(CONFIG_USER_ONLY)
222 void helper_wrmsr(CPUX86State
*env
)
226 void helper_rdmsr(CPUX86State
*env
)
230 void helper_wrmsr(CPUX86State
*env
)
234 cpu_svm_check_intercept_param(env
, SVM_EXIT_MSR
, 1, GETPC());
236 val
= ((uint32_t)env
->regs
[R_EAX
]) |
237 ((uint64_t)((uint32_t)env
->regs
[R_EDX
]) << 32);
239 switch ((uint32_t)env
->regs
[R_ECX
]) {
240 case MSR_IA32_SYSENTER_CS
:
241 env
->sysenter_cs
= val
& 0xffff;
243 case MSR_IA32_SYSENTER_ESP
:
244 env
->sysenter_esp
= val
;
246 case MSR_IA32_SYSENTER_EIP
:
247 env
->sysenter_eip
= val
;
249 case MSR_IA32_APICBASE
:
250 cpu_set_apic_base(env_archcpu(env
)->apic_state
, val
);
254 uint64_t update_mask
;
257 if (env
->features
[FEAT_8000_0001_EDX
] & CPUID_EXT2_SYSCALL
) {
258 update_mask
|= MSR_EFER_SCE
;
260 if (env
->features
[FEAT_8000_0001_EDX
] & CPUID_EXT2_LM
) {
261 update_mask
|= MSR_EFER_LME
;
263 if (env
->features
[FEAT_8000_0001_EDX
] & CPUID_EXT2_FFXSR
) {
264 update_mask
|= MSR_EFER_FFXSR
;
266 if (env
->features
[FEAT_8000_0001_EDX
] & CPUID_EXT2_NX
) {
267 update_mask
|= MSR_EFER_NXE
;
269 if (env
->features
[FEAT_8000_0001_ECX
] & CPUID_EXT3_SVM
) {
270 update_mask
|= MSR_EFER_SVME
;
272 if (env
->features
[FEAT_8000_0001_EDX
] & CPUID_EXT2_FFXSR
) {
273 update_mask
|= MSR_EFER_FFXSR
;
275 cpu_load_efer(env
, (env
->efer
& ~update_mask
) |
276 (val
& update_mask
));
285 case MSR_VM_HSAVE_PA
:
299 env
->segs
[R_FS
].base
= val
;
302 env
->segs
[R_GS
].base
= val
;
304 case MSR_KERNELGSBASE
:
305 env
->kernelgsbase
= val
;
308 case MSR_MTRRphysBase(0):
309 case MSR_MTRRphysBase(1):
310 case MSR_MTRRphysBase(2):
311 case MSR_MTRRphysBase(3):
312 case MSR_MTRRphysBase(4):
313 case MSR_MTRRphysBase(5):
314 case MSR_MTRRphysBase(6):
315 case MSR_MTRRphysBase(7):
316 env
->mtrr_var
[((uint32_t)env
->regs
[R_ECX
] -
317 MSR_MTRRphysBase(0)) / 2].base
= val
;
319 case MSR_MTRRphysMask(0):
320 case MSR_MTRRphysMask(1):
321 case MSR_MTRRphysMask(2):
322 case MSR_MTRRphysMask(3):
323 case MSR_MTRRphysMask(4):
324 case MSR_MTRRphysMask(5):
325 case MSR_MTRRphysMask(6):
326 case MSR_MTRRphysMask(7):
327 env
->mtrr_var
[((uint32_t)env
->regs
[R_ECX
] -
328 MSR_MTRRphysMask(0)) / 2].mask
= val
;
330 case MSR_MTRRfix64K_00000
:
331 env
->mtrr_fixed
[(uint32_t)env
->regs
[R_ECX
] -
332 MSR_MTRRfix64K_00000
] = val
;
334 case MSR_MTRRfix16K_80000
:
335 case MSR_MTRRfix16K_A0000
:
336 env
->mtrr_fixed
[(uint32_t)env
->regs
[R_ECX
] -
337 MSR_MTRRfix16K_80000
+ 1] = val
;
339 case MSR_MTRRfix4K_C0000
:
340 case MSR_MTRRfix4K_C8000
:
341 case MSR_MTRRfix4K_D0000
:
342 case MSR_MTRRfix4K_D8000
:
343 case MSR_MTRRfix4K_E0000
:
344 case MSR_MTRRfix4K_E8000
:
345 case MSR_MTRRfix4K_F0000
:
346 case MSR_MTRRfix4K_F8000
:
347 env
->mtrr_fixed
[(uint32_t)env
->regs
[R_ECX
] -
348 MSR_MTRRfix4K_C0000
+ 3] = val
;
350 case MSR_MTRRdefType
:
351 env
->mtrr_deftype
= val
;
354 env
->mcg_status
= val
;
357 if ((env
->mcg_cap
& MCG_CTL_P
)
358 && (val
== 0 || val
== ~(uint64_t)0)) {
365 case MSR_IA32_MISC_ENABLE
:
366 env
->msr_ia32_misc_enable
= val
;
368 case MSR_IA32_BNDCFGS
:
369 /* FIXME: #GP if reserved bits are set. */
370 /* FIXME: Extend highest implemented bit of linear address. */
371 env
->msr_bndcfgs
= val
;
372 cpu_sync_bndcs_hflags(env
);
375 if ((uint32_t)env
->regs
[R_ECX
] >= MSR_MC0_CTL
376 && (uint32_t)env
->regs
[R_ECX
] < MSR_MC0_CTL
+
377 (4 * env
->mcg_cap
& 0xff)) {
378 uint32_t offset
= (uint32_t)env
->regs
[R_ECX
] - MSR_MC0_CTL
;
379 if ((offset
& 0x3) != 0
380 || (val
== 0 || val
== ~(uint64_t)0)) {
381 env
->mce_banks
[offset
] = val
;
385 /* XXX: exception? */
390 void helper_rdmsr(CPUX86State
*env
)
392 X86CPU
*x86_cpu
= env_archcpu(env
);
395 cpu_svm_check_intercept_param(env
, SVM_EXIT_MSR
, 0, GETPC());
397 switch ((uint32_t)env
->regs
[R_ECX
]) {
398 case MSR_IA32_SYSENTER_CS
:
399 val
= env
->sysenter_cs
;
401 case MSR_IA32_SYSENTER_ESP
:
402 val
= env
->sysenter_esp
;
404 case MSR_IA32_SYSENTER_EIP
:
405 val
= env
->sysenter_eip
;
407 case MSR_IA32_APICBASE
:
408 val
= cpu_get_apic_base(env_archcpu(env
)->apic_state
);
419 case MSR_VM_HSAVE_PA
:
422 case MSR_IA32_PERF_STATUS
:
423 /* tsc_increment_by_tick */
426 val
|= (((uint64_t)4ULL) << 40);
439 val
= env
->segs
[R_FS
].base
;
442 val
= env
->segs
[R_GS
].base
;
444 case MSR_KERNELGSBASE
:
445 val
= env
->kernelgsbase
;
452 val
= env
->msr_smi_count
;
454 case MSR_MTRRphysBase(0):
455 case MSR_MTRRphysBase(1):
456 case MSR_MTRRphysBase(2):
457 case MSR_MTRRphysBase(3):
458 case MSR_MTRRphysBase(4):
459 case MSR_MTRRphysBase(5):
460 case MSR_MTRRphysBase(6):
461 case MSR_MTRRphysBase(7):
462 val
= env
->mtrr_var
[((uint32_t)env
->regs
[R_ECX
] -
463 MSR_MTRRphysBase(0)) / 2].base
;
465 case MSR_MTRRphysMask(0):
466 case MSR_MTRRphysMask(1):
467 case MSR_MTRRphysMask(2):
468 case MSR_MTRRphysMask(3):
469 case MSR_MTRRphysMask(4):
470 case MSR_MTRRphysMask(5):
471 case MSR_MTRRphysMask(6):
472 case MSR_MTRRphysMask(7):
473 val
= env
->mtrr_var
[((uint32_t)env
->regs
[R_ECX
] -
474 MSR_MTRRphysMask(0)) / 2].mask
;
476 case MSR_MTRRfix64K_00000
:
477 val
= env
->mtrr_fixed
[0];
479 case MSR_MTRRfix16K_80000
:
480 case MSR_MTRRfix16K_A0000
:
481 val
= env
->mtrr_fixed
[(uint32_t)env
->regs
[R_ECX
] -
482 MSR_MTRRfix16K_80000
+ 1];
484 case MSR_MTRRfix4K_C0000
:
485 case MSR_MTRRfix4K_C8000
:
486 case MSR_MTRRfix4K_D0000
:
487 case MSR_MTRRfix4K_D8000
:
488 case MSR_MTRRfix4K_E0000
:
489 case MSR_MTRRfix4K_E8000
:
490 case MSR_MTRRfix4K_F0000
:
491 case MSR_MTRRfix4K_F8000
:
492 val
= env
->mtrr_fixed
[(uint32_t)env
->regs
[R_ECX
] -
493 MSR_MTRRfix4K_C0000
+ 3];
495 case MSR_MTRRdefType
:
496 val
= env
->mtrr_deftype
;
499 if (env
->features
[FEAT_1_EDX
] & CPUID_MTRR
) {
500 val
= MSR_MTRRcap_VCNT
| MSR_MTRRcap_FIXRANGE_SUPPORT
|
501 MSR_MTRRcap_WC_SUPPORTED
;
503 /* XXX: exception? */
511 if (env
->mcg_cap
& MCG_CTL_P
) {
518 val
= env
->mcg_status
;
520 case MSR_IA32_MISC_ENABLE
:
521 val
= env
->msr_ia32_misc_enable
;
523 case MSR_IA32_BNDCFGS
:
524 val
= env
->msr_bndcfgs
;
526 case MSR_IA32_UCODE_REV
:
527 val
= x86_cpu
->ucode_rev
;
530 if ((uint32_t)env
->regs
[R_ECX
] >= MSR_MC0_CTL
531 && (uint32_t)env
->regs
[R_ECX
] < MSR_MC0_CTL
+
532 (4 * env
->mcg_cap
& 0xff)) {
533 uint32_t offset
= (uint32_t)env
->regs
[R_ECX
] - MSR_MC0_CTL
;
534 val
= env
->mce_banks
[offset
];
537 /* XXX: exception? */
541 env
->regs
[R_EAX
] = (uint32_t)(val
);
542 env
->regs
[R_EDX
] = (uint32_t)(val
>> 32);
546 static void do_pause(X86CPU
*cpu
)
548 CPUState
*cs
= CPU(cpu
);
550 /* Just let another CPU run. */
551 cs
->exception_index
= EXCP_INTERRUPT
;
555 static void do_hlt(X86CPU
*cpu
)
557 CPUState
*cs
= CPU(cpu
);
558 CPUX86State
*env
= &cpu
->env
;
560 env
->hflags
&= ~HF_INHIBIT_IRQ_MASK
; /* needed if sti is just before */
562 cs
->exception_index
= EXCP_HLT
;
566 void helper_hlt(CPUX86State
*env
, int next_eip_addend
)
568 X86CPU
*cpu
= env_archcpu(env
);
570 cpu_svm_check_intercept_param(env
, SVM_EXIT_HLT
, 0, GETPC());
571 env
->eip
+= next_eip_addend
;
576 void helper_monitor(CPUX86State
*env
, target_ulong ptr
)
578 if ((uint32_t)env
->regs
[R_ECX
] != 0) {
579 raise_exception_ra(env
, EXCP0D_GPF
, GETPC());
581 /* XXX: store address? */
582 cpu_svm_check_intercept_param(env
, SVM_EXIT_MONITOR
, 0, GETPC());
585 void helper_mwait(CPUX86State
*env
, int next_eip_addend
)
587 CPUState
*cs
= env_cpu(env
);
588 X86CPU
*cpu
= env_archcpu(env
);
590 if ((uint32_t)env
->regs
[R_ECX
] != 0) {
591 raise_exception_ra(env
, EXCP0D_GPF
, GETPC());
593 cpu_svm_check_intercept_param(env
, SVM_EXIT_MWAIT
, 0, GETPC());
594 env
->eip
+= next_eip_addend
;
596 /* XXX: not complete but not completely erroneous */
597 if (cs
->cpu_index
!= 0 || CPU_NEXT(cs
) != NULL
) {
604 void helper_pause(CPUX86State
*env
, int next_eip_addend
)
606 X86CPU
*cpu
= env_archcpu(env
);
608 cpu_svm_check_intercept_param(env
, SVM_EXIT_PAUSE
, 0, GETPC());
609 env
->eip
+= next_eip_addend
;
614 void helper_debug(CPUX86State
*env
)
616 CPUState
*cs
= env_cpu(env
);
618 cs
->exception_index
= EXCP_DEBUG
;
622 uint64_t helper_rdpkru(CPUX86State
*env
, uint32_t ecx
)
624 if ((env
->cr
[4] & CR4_PKE_MASK
) == 0) {
625 raise_exception_err_ra(env
, EXCP06_ILLOP
, 0, GETPC());
628 raise_exception_err_ra(env
, EXCP0D_GPF
, 0, GETPC());
634 void helper_wrpkru(CPUX86State
*env
, uint32_t ecx
, uint64_t val
)
636 CPUState
*cs
= env_cpu(env
);
638 if ((env
->cr
[4] & CR4_PKE_MASK
) == 0) {
639 raise_exception_err_ra(env
, EXCP06_ILLOP
, 0, GETPC());
641 if (ecx
!= 0 || (val
& 0xFFFFFFFF00000000ull
)) {
642 raise_exception_err_ra(env
, EXCP0D_GPF
, 0, GETPC());