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/>.
21 #include "exec/helper-proto.h"
22 #include "exec/cpu_ldst.h"
23 #include "exec/address-spaces.h"
25 void helper_outb(CPUX86State
*env
, uint32_t port
, uint32_t data
)
27 #ifdef CONFIG_USER_ONLY
28 fprintf(stderr
, "outb: port=0x%04x, data=%02x\n", port
, data
);
30 address_space_stb(&address_space_io
, port
, data
,
31 cpu_get_mem_attrs(env
), NULL
);
35 target_ulong
helper_inb(CPUX86State
*env
, uint32_t port
)
37 #ifdef CONFIG_USER_ONLY
38 fprintf(stderr
, "inb: port=0x%04x\n", port
);
41 return address_space_ldub(&address_space_io
, port
,
42 cpu_get_mem_attrs(env
), NULL
);
46 void helper_outw(CPUX86State
*env
, uint32_t port
, uint32_t data
)
48 #ifdef CONFIG_USER_ONLY
49 fprintf(stderr
, "outw: port=0x%04x, data=%04x\n", port
, data
);
51 address_space_stw(&address_space_io
, port
, data
,
52 cpu_get_mem_attrs(env
), NULL
);
56 target_ulong
helper_inw(CPUX86State
*env
, uint32_t port
)
58 #ifdef CONFIG_USER_ONLY
59 fprintf(stderr
, "inw: port=0x%04x\n", port
);
62 return address_space_lduw(&address_space_io
, port
,
63 cpu_get_mem_attrs(env
), NULL
);
67 void helper_outl(CPUX86State
*env
, uint32_t port
, uint32_t data
)
69 #ifdef CONFIG_USER_ONLY
70 fprintf(stderr
, "outw: port=0x%04x, data=%08x\n", port
, data
);
72 address_space_stl(&address_space_io
, port
, data
,
73 cpu_get_mem_attrs(env
), NULL
);
77 target_ulong
helper_inl(CPUX86State
*env
, uint32_t port
)
79 #ifdef CONFIG_USER_ONLY
80 fprintf(stderr
, "inl: port=0x%04x\n", port
);
83 return address_space_ldl(&address_space_io
, port
,
84 cpu_get_mem_attrs(env
), NULL
);
88 void helper_into(CPUX86State
*env
, int next_eip_addend
)
92 eflags
= cpu_cc_compute_all(env
, CC_OP
);
94 raise_interrupt(env
, EXCP04_INTO
, 1, 0, next_eip_addend
);
98 void helper_cpuid(CPUX86State
*env
)
100 uint32_t eax
, ebx
, ecx
, edx
;
102 cpu_svm_check_intercept_param(env
, SVM_EXIT_CPUID
, 0);
104 cpu_x86_cpuid(env
, (uint32_t)env
->regs
[R_EAX
], (uint32_t)env
->regs
[R_ECX
],
105 &eax
, &ebx
, &ecx
, &edx
);
106 env
->regs
[R_EAX
] = eax
;
107 env
->regs
[R_EBX
] = ebx
;
108 env
->regs
[R_ECX
] = ecx
;
109 env
->regs
[R_EDX
] = edx
;
112 #if defined(CONFIG_USER_ONLY)
113 target_ulong
helper_read_crN(CPUX86State
*env
, int reg
)
118 void helper_write_crN(CPUX86State
*env
, int reg
, target_ulong t0
)
122 target_ulong
helper_read_crN(CPUX86State
*env
, int reg
)
126 cpu_svm_check_intercept_param(env
, SVM_EXIT_READ_CR0
+ reg
, 0);
132 if (!(env
->hflags2
& HF2_VINTR_MASK
)) {
133 val
= cpu_get_apic_tpr(x86_env_get_cpu(env
)->apic_state
);
142 void helper_write_crN(CPUX86State
*env
, int reg
, target_ulong t0
)
144 cpu_svm_check_intercept_param(env
, SVM_EXIT_WRITE_CR0
+ reg
, 0);
147 cpu_x86_update_cr0(env
, t0
);
150 cpu_x86_update_cr3(env
, t0
);
153 cpu_x86_update_cr4(env
, t0
);
156 if (!(env
->hflags2
& HF2_VINTR_MASK
)) {
157 cpu_set_apic_tpr(x86_env_get_cpu(env
)->apic_state
, t0
);
159 env
->v_tpr
= t0
& 0x0f;
168 void helper_lmsw(CPUX86State
*env
, target_ulong t0
)
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);
173 helper_write_crN(env
, 0, t0
);
176 void helper_invlpg(CPUX86State
*env
, target_ulong addr
)
178 X86CPU
*cpu
= x86_env_get_cpu(env
);
180 cpu_svm_check_intercept_param(env
, SVM_EXIT_INVLPG
, 0);
181 tlb_flush_page(CPU(cpu
), addr
);
184 void helper_rdtsc(CPUX86State
*env
)
188 if ((env
->cr
[4] & CR4_TSD_MASK
) && ((env
->hflags
& HF_CPL_MASK
) != 0)) {
189 raise_exception_ra(env
, EXCP0D_GPF
, GETPC());
191 cpu_svm_check_intercept_param(env
, SVM_EXIT_RDTSC
, 0);
193 val
= cpu_get_tsc(env
) + env
->tsc_offset
;
194 env
->regs
[R_EAX
] = (uint32_t)(val
);
195 env
->regs
[R_EDX
] = (uint32_t)(val
>> 32);
198 void helper_rdtscp(CPUX86State
*env
)
201 env
->regs
[R_ECX
] = (uint32_t)(env
->tsc_aux
);
204 void helper_rdpmc(CPUX86State
*env
)
206 if ((env
->cr
[4] & CR4_PCE_MASK
) && ((env
->hflags
& HF_CPL_MASK
) != 0)) {
207 raise_exception_ra(env
, EXCP0D_GPF
, GETPC());
209 cpu_svm_check_intercept_param(env
, SVM_EXIT_RDPMC
, 0);
211 /* currently unimplemented */
212 qemu_log_mask(LOG_UNIMP
, "x86: unimplemented rdpmc\n");
213 raise_exception_err(env
, EXCP06_ILLOP
, 0);
216 #if defined(CONFIG_USER_ONLY)
217 void helper_wrmsr(CPUX86State
*env
)
221 void helper_rdmsr(CPUX86State
*env
)
225 void helper_wrmsr(CPUX86State
*env
)
229 cpu_svm_check_intercept_param(env
, SVM_EXIT_MSR
, 1);
231 val
= ((uint32_t)env
->regs
[R_EAX
]) |
232 ((uint64_t)((uint32_t)env
->regs
[R_EDX
]) << 32);
234 switch ((uint32_t)env
->regs
[R_ECX
]) {
235 case MSR_IA32_SYSENTER_CS
:
236 env
->sysenter_cs
= val
& 0xffff;
238 case MSR_IA32_SYSENTER_ESP
:
239 env
->sysenter_esp
= val
;
241 case MSR_IA32_SYSENTER_EIP
:
242 env
->sysenter_eip
= val
;
244 case MSR_IA32_APICBASE
:
245 cpu_set_apic_base(x86_env_get_cpu(env
)->apic_state
, val
);
249 uint64_t update_mask
;
252 if (env
->features
[FEAT_8000_0001_EDX
] & CPUID_EXT2_SYSCALL
) {
253 update_mask
|= MSR_EFER_SCE
;
255 if (env
->features
[FEAT_8000_0001_EDX
] & CPUID_EXT2_LM
) {
256 update_mask
|= MSR_EFER_LME
;
258 if (env
->features
[FEAT_8000_0001_EDX
] & CPUID_EXT2_FFXSR
) {
259 update_mask
|= MSR_EFER_FFXSR
;
261 if (env
->features
[FEAT_8000_0001_EDX
] & CPUID_EXT2_NX
) {
262 update_mask
|= MSR_EFER_NXE
;
264 if (env
->features
[FEAT_8000_0001_ECX
] & CPUID_EXT3_SVM
) {
265 update_mask
|= MSR_EFER_SVME
;
267 if (env
->features
[FEAT_8000_0001_EDX
] & CPUID_EXT2_FFXSR
) {
268 update_mask
|= MSR_EFER_FFXSR
;
270 cpu_load_efer(env
, (env
->efer
& ~update_mask
) |
271 (val
& update_mask
));
280 case MSR_VM_HSAVE_PA
:
294 env
->segs
[R_FS
].base
= val
;
297 env
->segs
[R_GS
].base
= val
;
299 case MSR_KERNELGSBASE
:
300 env
->kernelgsbase
= val
;
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):
311 env
->mtrr_var
[((uint32_t)env
->regs
[R_ECX
] -
312 MSR_MTRRphysBase(0)) / 2].base
= val
;
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):
322 env
->mtrr_var
[((uint32_t)env
->regs
[R_ECX
] -
323 MSR_MTRRphysMask(0)) / 2].mask
= val
;
325 case MSR_MTRRfix64K_00000
:
326 env
->mtrr_fixed
[(uint32_t)env
->regs
[R_ECX
] -
327 MSR_MTRRfix64K_00000
] = val
;
329 case MSR_MTRRfix16K_80000
:
330 case MSR_MTRRfix16K_A0000
:
331 env
->mtrr_fixed
[(uint32_t)env
->regs
[R_ECX
] -
332 MSR_MTRRfix16K_80000
+ 1] = val
;
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
:
342 env
->mtrr_fixed
[(uint32_t)env
->regs
[R_ECX
] -
343 MSR_MTRRfix4K_C0000
+ 3] = val
;
345 case MSR_MTRRdefType
:
346 env
->mtrr_deftype
= val
;
349 env
->mcg_status
= val
;
352 if ((env
->mcg_cap
& MCG_CTL_P
)
353 && (val
== 0 || val
== ~(uint64_t)0)) {
360 case MSR_IA32_MISC_ENABLE
:
361 env
->msr_ia32_misc_enable
= val
;
364 if ((uint32_t)env
->regs
[R_ECX
] >= MSR_MC0_CTL
365 && (uint32_t)env
->regs
[R_ECX
] < MSR_MC0_CTL
+
366 (4 * env
->mcg_cap
& 0xff)) {
367 uint32_t offset
= (uint32_t)env
->regs
[R_ECX
] - MSR_MC0_CTL
;
368 if ((offset
& 0x3) != 0
369 || (val
== 0 || val
== ~(uint64_t)0)) {
370 env
->mce_banks
[offset
] = val
;
374 /* XXX: exception? */
379 void helper_rdmsr(CPUX86State
*env
)
383 cpu_svm_check_intercept_param(env
, SVM_EXIT_MSR
, 0);
385 switch ((uint32_t)env
->regs
[R_ECX
]) {
386 case MSR_IA32_SYSENTER_CS
:
387 val
= env
->sysenter_cs
;
389 case MSR_IA32_SYSENTER_ESP
:
390 val
= env
->sysenter_esp
;
392 case MSR_IA32_SYSENTER_EIP
:
393 val
= env
->sysenter_eip
;
395 case MSR_IA32_APICBASE
:
396 val
= cpu_get_apic_base(x86_env_get_cpu(env
)->apic_state
);
407 case MSR_VM_HSAVE_PA
:
410 case MSR_IA32_PERF_STATUS
:
411 /* tsc_increment_by_tick */
414 val
|= (((uint64_t)4ULL) << 40);
427 val
= env
->segs
[R_FS
].base
;
430 val
= env
->segs
[R_GS
].base
;
432 case MSR_KERNELGSBASE
:
433 val
= env
->kernelgsbase
;
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):
447 val
= env
->mtrr_var
[((uint32_t)env
->regs
[R_ECX
] -
448 MSR_MTRRphysBase(0)) / 2].base
;
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):
458 val
= env
->mtrr_var
[((uint32_t)env
->regs
[R_ECX
] -
459 MSR_MTRRphysMask(0)) / 2].mask
;
461 case MSR_MTRRfix64K_00000
:
462 val
= env
->mtrr_fixed
[0];
464 case MSR_MTRRfix16K_80000
:
465 case MSR_MTRRfix16K_A0000
:
466 val
= env
->mtrr_fixed
[(uint32_t)env
->regs
[R_ECX
] -
467 MSR_MTRRfix16K_80000
+ 1];
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
:
477 val
= env
->mtrr_fixed
[(uint32_t)env
->regs
[R_ECX
] -
478 MSR_MTRRfix4K_C0000
+ 3];
480 case MSR_MTRRdefType
:
481 val
= env
->mtrr_deftype
;
484 if (env
->features
[FEAT_1_EDX
] & CPUID_MTRR
) {
485 val
= MSR_MTRRcap_VCNT
| MSR_MTRRcap_FIXRANGE_SUPPORT
|
486 MSR_MTRRcap_WC_SUPPORTED
;
488 /* XXX: exception? */
496 if (env
->mcg_cap
& MCG_CTL_P
) {
503 val
= env
->mcg_status
;
505 case MSR_IA32_MISC_ENABLE
:
506 val
= env
->msr_ia32_misc_enable
;
509 if ((uint32_t)env
->regs
[R_ECX
] >= MSR_MC0_CTL
510 && (uint32_t)env
->regs
[R_ECX
] < MSR_MC0_CTL
+
511 (4 * env
->mcg_cap
& 0xff)) {
512 uint32_t offset
= (uint32_t)env
->regs
[R_ECX
] - MSR_MC0_CTL
;
513 val
= env
->mce_banks
[offset
];
516 /* XXX: exception? */
520 env
->regs
[R_EAX
] = (uint32_t)(val
);
521 env
->regs
[R_EDX
] = (uint32_t)(val
>> 32);
525 static void do_pause(X86CPU
*cpu
)
527 CPUState
*cs
= CPU(cpu
);
529 /* Just let another CPU run. */
530 cs
->exception_index
= EXCP_INTERRUPT
;
534 static void do_hlt(X86CPU
*cpu
)
536 CPUState
*cs
= CPU(cpu
);
537 CPUX86State
*env
= &cpu
->env
;
539 env
->hflags
&= ~HF_INHIBIT_IRQ_MASK
; /* needed if sti is just before */
541 cs
->exception_index
= EXCP_HLT
;
545 void helper_hlt(CPUX86State
*env
, int next_eip_addend
)
547 X86CPU
*cpu
= x86_env_get_cpu(env
);
549 cpu_svm_check_intercept_param(env
, SVM_EXIT_HLT
, 0);
550 env
->eip
+= next_eip_addend
;
555 void helper_monitor(CPUX86State
*env
, target_ulong ptr
)
557 if ((uint32_t)env
->regs
[R_ECX
] != 0) {
558 raise_exception_ra(env
, EXCP0D_GPF
, GETPC());
560 /* XXX: store address? */
561 cpu_svm_check_intercept_param(env
, SVM_EXIT_MONITOR
, 0);
564 void helper_mwait(CPUX86State
*env
, int next_eip_addend
)
569 if ((uint32_t)env
->regs
[R_ECX
] != 0) {
570 raise_exception_ra(env
, EXCP0D_GPF
, GETPC());
572 cpu_svm_check_intercept_param(env
, SVM_EXIT_MWAIT
, 0);
573 env
->eip
+= next_eip_addend
;
575 cpu
= x86_env_get_cpu(env
);
577 /* XXX: not complete but not completely erroneous */
578 if (cs
->cpu_index
!= 0 || CPU_NEXT(cs
) != NULL
) {
585 void helper_pause(CPUX86State
*env
, int next_eip_addend
)
587 X86CPU
*cpu
= x86_env_get_cpu(env
);
589 cpu_svm_check_intercept_param(env
, SVM_EXIT_PAUSE
, 0);
590 env
->eip
+= next_eip_addend
;
595 void helper_debug(CPUX86State
*env
)
597 CPUState
*cs
= CPU(x86_env_get_cpu(env
));
599 cs
->exception_index
= EXCP_DEBUG
;