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"
22 #include "exec/helper-proto.h"
23 #include "exec/exec-all.h"
24 #include "exec/cpu_ldst.h"
25 #include "exec/address-spaces.h"
27 void helper_outb(CPUX86State *env, uint32_t port, uint32_t data)
29 #ifdef CONFIG_USER_ONLY
30 fprintf(stderr, "outb: port=0x%04x, data=%02x\n", port, data);
32 address_space_stb(&address_space_io, port, data,
33 cpu_get_mem_attrs(env), NULL);
37 target_ulong helper_inb(CPUX86State *env, uint32_t port)
39 #ifdef CONFIG_USER_ONLY
40 fprintf(stderr, "inb: port=0x%04x\n", port);
43 return address_space_ldub(&address_space_io, port,
44 cpu_get_mem_attrs(env), NULL);
48 void helper_outw(CPUX86State *env, uint32_t port, uint32_t data)
50 #ifdef CONFIG_USER_ONLY
51 fprintf(stderr, "outw: port=0x%04x, data=%04x\n", port, data);
53 address_space_stw(&address_space_io, port, data,
54 cpu_get_mem_attrs(env), NULL);
58 target_ulong helper_inw(CPUX86State *env, uint32_t port)
60 #ifdef CONFIG_USER_ONLY
61 fprintf(stderr, "inw: port=0x%04x\n", port);
64 return address_space_lduw(&address_space_io, port,
65 cpu_get_mem_attrs(env), NULL);
69 void helper_outl(CPUX86State *env, uint32_t port, uint32_t data)
71 #ifdef CONFIG_USER_ONLY
72 fprintf(stderr, "outw: port=0x%04x, data=%08x\n", port, data);
74 address_space_stl(&address_space_io, port, data,
75 cpu_get_mem_attrs(env), NULL);
79 target_ulong helper_inl(CPUX86State *env, uint32_t port)
81 #ifdef CONFIG_USER_ONLY
82 fprintf(stderr, "inl: port=0x%04x\n", port);
85 return address_space_ldl(&address_space_io, port,
86 cpu_get_mem_attrs(env), NULL);
90 void helper_into(CPUX86State *env, int next_eip_addend)
94 eflags = cpu_cc_compute_all(env, CC_OP);
96 raise_interrupt(env, EXCP04_INTO, 1, 0, next_eip_addend);
100 void helper_cpuid(CPUX86State *env)
102 uint32_t eax, ebx, ecx, edx;
104 cpu_svm_check_intercept_param(env, SVM_EXIT_CPUID, 0);
106 cpu_x86_cpuid(env, (uint32_t)env->regs[R_EAX], (uint32_t)env->regs[R_ECX],
107 &eax, &ebx, &ecx, &edx);
108 env->regs[R_EAX] = eax;
109 env->regs[R_EBX] = ebx;
110 env->regs[R_ECX] = ecx;
111 env->regs[R_EDX] = edx;
114 #if defined(CONFIG_USER_ONLY)
115 target_ulong helper_read_crN(CPUX86State *env, int reg)
120 void helper_write_crN(CPUX86State *env, int reg, target_ulong t0)
124 target_ulong helper_read_crN(CPUX86State *env, int reg)
128 cpu_svm_check_intercept_param(env, SVM_EXIT_READ_CR0 + reg, 0);
134 if (!(env->hflags2 & HF2_VINTR_MASK)) {
135 val = cpu_get_apic_tpr(x86_env_get_cpu(env)->apic_state);
144 void helper_write_crN(CPUX86State *env, int reg, target_ulong t0)
146 cpu_svm_check_intercept_param(env, SVM_EXIT_WRITE_CR0 + reg, 0);
149 cpu_x86_update_cr0(env, t0);
152 cpu_x86_update_cr3(env, t0);
155 cpu_x86_update_cr4(env, t0);
158 if (!(env->hflags2 & HF2_VINTR_MASK)) {
159 cpu_set_apic_tpr(x86_env_get_cpu(env)->apic_state, t0);
161 env->v_tpr = t0 & 0x0f;
170 void helper_lmsw(CPUX86State *env, target_ulong t0)
172 /* only 4 lower bits of CR0 are modified. PE cannot be set to zero
173 if already set to one. */
174 t0 = (env->cr[0] & ~0xe) | (t0 & 0xf);
175 helper_write_crN(env, 0, t0);
178 void helper_invlpg(CPUX86State *env, target_ulong addr)
180 X86CPU *cpu = x86_env_get_cpu(env);
182 cpu_svm_check_intercept_param(env, SVM_EXIT_INVLPG, 0);
183 tlb_flush_page(CPU(cpu), addr);
186 void helper_rdtsc(CPUX86State *env)
190 if ((env->cr[4] & CR4_TSD_MASK) && ((env->hflags & HF_CPL_MASK) != 0)) {
191 raise_exception_ra(env, EXCP0D_GPF, GETPC());
193 cpu_svm_check_intercept_param(env, SVM_EXIT_RDTSC, 0);
195 val = cpu_get_tsc(env) + env->tsc_offset;
196 env->regs[R_EAX] = (uint32_t)(val);
197 env->regs[R_EDX] = (uint32_t)(val >> 32);
200 void helper_rdtscp(CPUX86State *env)
203 env->regs[R_ECX] = (uint32_t)(env->tsc_aux);
206 void helper_rdpmc(CPUX86State *env)
208 if ((env->cr[4] & CR4_PCE_MASK) && ((env->hflags & HF_CPL_MASK) != 0)) {
209 raise_exception_ra(env, EXCP0D_GPF, GETPC());
211 cpu_svm_check_intercept_param(env, SVM_EXIT_RDPMC, 0);
213 /* currently unimplemented */
214 qemu_log_mask(LOG_UNIMP, "x86: unimplemented rdpmc\n");
215 raise_exception_err(env, EXCP06_ILLOP, 0);
218 #if defined(CONFIG_USER_ONLY)
219 void helper_wrmsr(CPUX86State *env)
223 void helper_rdmsr(CPUX86State *env)
227 void helper_wrmsr(CPUX86State *env)
231 cpu_svm_check_intercept_param(env, SVM_EXIT_MSR, 1);
233 val = ((uint32_t)env->regs[R_EAX]) |
234 ((uint64_t)((uint32_t)env->regs[R_EDX]) << 32);
236 switch ((uint32_t)env->regs[R_ECX]) {
237 case MSR_IA32_SYSENTER_CS:
238 env->sysenter_cs = val & 0xffff;
240 case MSR_IA32_SYSENTER_ESP:
241 env->sysenter_esp = val;
243 case MSR_IA32_SYSENTER_EIP:
244 env->sysenter_eip = val;
246 case MSR_IA32_APICBASE:
247 cpu_set_apic_base(x86_env_get_cpu(env)->apic_state, val);
251 uint64_t update_mask;
254 if (env->features[FEAT_8000_0001_EDX] & CPUID_EXT2_SYSCALL) {
255 update_mask |= MSR_EFER_SCE;
257 if (env->features[FEAT_8000_0001_EDX] & CPUID_EXT2_LM) {
258 update_mask |= MSR_EFER_LME;
260 if (env->features[FEAT_8000_0001_EDX] & CPUID_EXT2_FFXSR) {
261 update_mask |= MSR_EFER_FFXSR;
263 if (env->features[FEAT_8000_0001_EDX] & CPUID_EXT2_NX) {
264 update_mask |= MSR_EFER_NXE;
266 if (env->features[FEAT_8000_0001_ECX] & CPUID_EXT3_SVM) {
267 update_mask |= MSR_EFER_SVME;
269 if (env->features[FEAT_8000_0001_EDX] & CPUID_EXT2_FFXSR) {
270 update_mask |= MSR_EFER_FFXSR;
272 cpu_load_efer(env, (env->efer & ~update_mask) |
273 (val & update_mask));
282 case MSR_VM_HSAVE_PA:
296 env->segs[R_FS].base = val;
299 env->segs[R_GS].base = val;
301 case MSR_KERNELGSBASE:
302 env->kernelgsbase = val;
305 case MSR_MTRRphysBase(0):
306 case MSR_MTRRphysBase(1):
307 case MSR_MTRRphysBase(2):
308 case MSR_MTRRphysBase(3):
309 case MSR_MTRRphysBase(4):
310 case MSR_MTRRphysBase(5):
311 case MSR_MTRRphysBase(6):
312 case MSR_MTRRphysBase(7):
313 env->mtrr_var[((uint32_t)env->regs[R_ECX] -
314 MSR_MTRRphysBase(0)) / 2].base = val;
316 case MSR_MTRRphysMask(0):
317 case MSR_MTRRphysMask(1):
318 case MSR_MTRRphysMask(2):
319 case MSR_MTRRphysMask(3):
320 case MSR_MTRRphysMask(4):
321 case MSR_MTRRphysMask(5):
322 case MSR_MTRRphysMask(6):
323 case MSR_MTRRphysMask(7):
324 env->mtrr_var[((uint32_t)env->regs[R_ECX] -
325 MSR_MTRRphysMask(0)) / 2].mask = val;
327 case MSR_MTRRfix64K_00000:
328 env->mtrr_fixed[(uint32_t)env->regs[R_ECX] -
329 MSR_MTRRfix64K_00000] = val;
331 case MSR_MTRRfix16K_80000:
332 case MSR_MTRRfix16K_A0000:
333 env->mtrr_fixed[(uint32_t)env->regs[R_ECX] -
334 MSR_MTRRfix16K_80000 + 1] = val;
336 case MSR_MTRRfix4K_C0000:
337 case MSR_MTRRfix4K_C8000:
338 case MSR_MTRRfix4K_D0000:
339 case MSR_MTRRfix4K_D8000:
340 case MSR_MTRRfix4K_E0000:
341 case MSR_MTRRfix4K_E8000:
342 case MSR_MTRRfix4K_F0000:
343 case MSR_MTRRfix4K_F8000:
344 env->mtrr_fixed[(uint32_t)env->regs[R_ECX] -
345 MSR_MTRRfix4K_C0000 + 3] = val;
347 case MSR_MTRRdefType:
348 env->mtrr_deftype = val;
351 env->mcg_status = val;
354 if ((env->mcg_cap & MCG_CTL_P)
355 && (val == 0 || val == ~(uint64_t)0)) {
362 case MSR_IA32_MISC_ENABLE:
363 env->msr_ia32_misc_enable = val;
365 case MSR_IA32_BNDCFGS:
366 /* FIXME: #GP if reserved bits are set. */
367 /* FIXME: Extend highest implemented bit of linear address. */
368 env->msr_bndcfgs = val;
369 cpu_sync_bndcs_hflags(env);
372 if ((uint32_t)env->regs[R_ECX] >= MSR_MC0_CTL
373 && (uint32_t)env->regs[R_ECX] < MSR_MC0_CTL +
374 (4 * env->mcg_cap & 0xff)) {
375 uint32_t offset = (uint32_t)env->regs[R_ECX] - MSR_MC0_CTL;
376 if ((offset & 0x3) != 0
377 || (val == 0 || val == ~(uint64_t)0)) {
378 env->mce_banks[offset] = val;
382 /* XXX: exception? */
387 void helper_rdmsr(CPUX86State *env)
391 cpu_svm_check_intercept_param(env, SVM_EXIT_MSR, 0);
393 switch ((uint32_t)env->regs[R_ECX]) {
394 case MSR_IA32_SYSENTER_CS:
395 val = env->sysenter_cs;
397 case MSR_IA32_SYSENTER_ESP:
398 val = env->sysenter_esp;
400 case MSR_IA32_SYSENTER_EIP:
401 val = env->sysenter_eip;
403 case MSR_IA32_APICBASE:
404 val = cpu_get_apic_base(x86_env_get_cpu(env)->apic_state);
415 case MSR_VM_HSAVE_PA:
418 case MSR_IA32_PERF_STATUS:
419 /* tsc_increment_by_tick */
422 val |= (((uint64_t)4ULL) << 40);
435 val = env->segs[R_FS].base;
438 val = env->segs[R_GS].base;
440 case MSR_KERNELGSBASE:
441 val = env->kernelgsbase;
447 case MSR_MTRRphysBase(0):
448 case MSR_MTRRphysBase(1):
449 case MSR_MTRRphysBase(2):
450 case MSR_MTRRphysBase(3):
451 case MSR_MTRRphysBase(4):
452 case MSR_MTRRphysBase(5):
453 case MSR_MTRRphysBase(6):
454 case MSR_MTRRphysBase(7):
455 val = env->mtrr_var[((uint32_t)env->regs[R_ECX] -
456 MSR_MTRRphysBase(0)) / 2].base;
458 case MSR_MTRRphysMask(0):
459 case MSR_MTRRphysMask(1):
460 case MSR_MTRRphysMask(2):
461 case MSR_MTRRphysMask(3):
462 case MSR_MTRRphysMask(4):
463 case MSR_MTRRphysMask(5):
464 case MSR_MTRRphysMask(6):
465 case MSR_MTRRphysMask(7):
466 val = env->mtrr_var[((uint32_t)env->regs[R_ECX] -
467 MSR_MTRRphysMask(0)) / 2].mask;
469 case MSR_MTRRfix64K_00000:
470 val = env->mtrr_fixed[0];
472 case MSR_MTRRfix16K_80000:
473 case MSR_MTRRfix16K_A0000:
474 val = env->mtrr_fixed[(uint32_t)env->regs[R_ECX] -
475 MSR_MTRRfix16K_80000 + 1];
477 case MSR_MTRRfix4K_C0000:
478 case MSR_MTRRfix4K_C8000:
479 case MSR_MTRRfix4K_D0000:
480 case MSR_MTRRfix4K_D8000:
481 case MSR_MTRRfix4K_E0000:
482 case MSR_MTRRfix4K_E8000:
483 case MSR_MTRRfix4K_F0000:
484 case MSR_MTRRfix4K_F8000:
485 val = env->mtrr_fixed[(uint32_t)env->regs[R_ECX] -
486 MSR_MTRRfix4K_C0000 + 3];
488 case MSR_MTRRdefType:
489 val = env->mtrr_deftype;
492 if (env->features[FEAT_1_EDX] & CPUID_MTRR) {
493 val = MSR_MTRRcap_VCNT | MSR_MTRRcap_FIXRANGE_SUPPORT |
494 MSR_MTRRcap_WC_SUPPORTED;
496 /* XXX: exception? */
504 if (env->mcg_cap & MCG_CTL_P) {
511 val = env->mcg_status;
513 case MSR_IA32_MISC_ENABLE:
514 val = env->msr_ia32_misc_enable;
516 case MSR_IA32_BNDCFGS:
517 val = env->msr_bndcfgs;
520 if ((uint32_t)env->regs[R_ECX] >= MSR_MC0_CTL
521 && (uint32_t)env->regs[R_ECX] < MSR_MC0_CTL +
522 (4 * env->mcg_cap & 0xff)) {
523 uint32_t offset = (uint32_t)env->regs[R_ECX] - MSR_MC0_CTL;
524 val = env->mce_banks[offset];
527 /* XXX: exception? */
531 env->regs[R_EAX] = (uint32_t)(val);
532 env->regs[R_EDX] = (uint32_t)(val >> 32);
536 static void do_pause(X86CPU *cpu)
538 CPUState *cs = CPU(cpu);
540 /* Just let another CPU run. */
541 cs->exception_index = EXCP_INTERRUPT;
545 static void do_hlt(X86CPU *cpu)
547 CPUState *cs = CPU(cpu);
548 CPUX86State *env = &cpu->env;
550 env->hflags &= ~HF_INHIBIT_IRQ_MASK; /* needed if sti is just before */
552 cs->exception_index = EXCP_HLT;
556 void helper_hlt(CPUX86State *env, int next_eip_addend)
558 X86CPU *cpu = x86_env_get_cpu(env);
560 cpu_svm_check_intercept_param(env, SVM_EXIT_HLT, 0);
561 env->eip += next_eip_addend;
566 void helper_monitor(CPUX86State *env, target_ulong ptr)
568 if ((uint32_t)env->regs[R_ECX] != 0) {
569 raise_exception_ra(env, EXCP0D_GPF, GETPC());
571 /* XXX: store address? */
572 cpu_svm_check_intercept_param(env, SVM_EXIT_MONITOR, 0);
575 void helper_mwait(CPUX86State *env, int next_eip_addend)
580 if ((uint32_t)env->regs[R_ECX] != 0) {
581 raise_exception_ra(env, EXCP0D_GPF, GETPC());
583 cpu_svm_check_intercept_param(env, SVM_EXIT_MWAIT, 0);
584 env->eip += next_eip_addend;
586 cpu = x86_env_get_cpu(env);
588 /* XXX: not complete but not completely erroneous */
589 if (cs->cpu_index != 0 || CPU_NEXT(cs) != NULL) {
596 void helper_pause(CPUX86State *env, int next_eip_addend)
598 X86CPU *cpu = x86_env_get_cpu(env);
600 cpu_svm_check_intercept_param(env, SVM_EXIT_PAUSE, 0);
601 env->eip += next_eip_addend;
606 void helper_debug(CPUX86State *env)
608 CPUState *cs = CPU(x86_env_get_cpu(env));
610 cs->exception_index = EXCP_DEBUG;
614 uint64_t helper_rdpkru(CPUX86State *env, uint32_t ecx)
616 if ((env->cr[4] & CR4_PKE_MASK) == 0) {
617 raise_exception_err_ra(env, EXCP06_ILLOP, 0, GETPC());
620 raise_exception_err_ra(env, EXCP0D_GPF, 0, GETPC());
626 void helper_wrpkru(CPUX86State *env, uint32_t ecx, uint64_t val)
628 CPUState *cs = CPU(x86_env_get_cpu(env));
630 if ((env->cr[4] & CR4_PKE_MASK) == 0) {
631 raise_exception_err_ra(env, EXCP06_ILLOP, 0, GETPC());
633 if (ecx != 0 || (val & 0xFFFFFFFF00000000ull)) {
634 raise_exception_err_ra(env, EXCP0D_GPF, 0, GETPC());