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_single_step(CPUX86State *env)
100 #ifndef CONFIG_USER_ONLY
101 check_hw_breakpoints(env, true);
102 env->dr[6] |= DR6_BS;
104 raise_exception(env, EXCP01_DB);
107 void helper_cpuid(CPUX86State *env)
109 uint32_t eax, ebx, ecx, edx;
111 cpu_svm_check_intercept_param(env, SVM_EXIT_CPUID, 0);
113 cpu_x86_cpuid(env, (uint32_t)env->regs[R_EAX], (uint32_t)env->regs[R_ECX],
114 &eax, &ebx, &ecx, &edx);
115 env->regs[R_EAX] = eax;
116 env->regs[R_EBX] = ebx;
117 env->regs[R_ECX] = ecx;
118 env->regs[R_EDX] = edx;
121 #if defined(CONFIG_USER_ONLY)
122 target_ulong helper_read_crN(CPUX86State *env, int reg)
127 void helper_write_crN(CPUX86State *env, int reg, target_ulong t0)
131 void helper_movl_drN_T0(CPUX86State *env, int reg, target_ulong t0)
135 target_ulong helper_read_crN(CPUX86State *env, int reg)
139 cpu_svm_check_intercept_param(env, SVM_EXIT_READ_CR0 + reg, 0);
145 if (!(env->hflags2 & HF2_VINTR_MASK)) {
146 val = cpu_get_apic_tpr(x86_env_get_cpu(env)->apic_state);
155 void helper_write_crN(CPUX86State *env, int reg, target_ulong t0)
157 cpu_svm_check_intercept_param(env, SVM_EXIT_WRITE_CR0 + reg, 0);
160 cpu_x86_update_cr0(env, t0);
163 cpu_x86_update_cr3(env, t0);
166 cpu_x86_update_cr4(env, t0);
169 if (!(env->hflags2 & HF2_VINTR_MASK)) {
170 cpu_set_apic_tpr(x86_env_get_cpu(env)->apic_state, t0);
172 env->v_tpr = t0 & 0x0f;
180 void helper_movl_drN_T0(CPUX86State *env, int reg, target_ulong t0)
185 hw_breakpoint_remove(env, reg);
187 hw_breakpoint_insert(env, reg);
188 } else if (reg == 7) {
189 for (i = 0; i < DR7_MAX_BP; i++) {
190 hw_breakpoint_remove(env, i);
193 for (i = 0; i < DR7_MAX_BP; i++) {
194 hw_breakpoint_insert(env, i);
202 void helper_lmsw(CPUX86State *env, target_ulong t0)
204 /* only 4 lower bits of CR0 are modified. PE cannot be set to zero
205 if already set to one. */
206 t0 = (env->cr[0] & ~0xe) | (t0 & 0xf);
207 helper_write_crN(env, 0, t0);
210 void helper_invlpg(CPUX86State *env, target_ulong addr)
212 X86CPU *cpu = x86_env_get_cpu(env);
214 cpu_svm_check_intercept_param(env, SVM_EXIT_INVLPG, 0);
215 tlb_flush_page(CPU(cpu), addr);
218 void helper_rdtsc(CPUX86State *env)
222 if ((env->cr[4] & CR4_TSD_MASK) && ((env->hflags & HF_CPL_MASK) != 0)) {
223 raise_exception(env, EXCP0D_GPF);
225 cpu_svm_check_intercept_param(env, SVM_EXIT_RDTSC, 0);
227 val = cpu_get_tsc(env) + env->tsc_offset;
228 env->regs[R_EAX] = (uint32_t)(val);
229 env->regs[R_EDX] = (uint32_t)(val >> 32);
232 void helper_rdtscp(CPUX86State *env)
235 env->regs[R_ECX] = (uint32_t)(env->tsc_aux);
238 void helper_rdpmc(CPUX86State *env)
240 if ((env->cr[4] & CR4_PCE_MASK) && ((env->hflags & HF_CPL_MASK) != 0)) {
241 raise_exception(env, EXCP0D_GPF);
243 cpu_svm_check_intercept_param(env, SVM_EXIT_RDPMC, 0);
245 /* currently unimplemented */
246 qemu_log_mask(LOG_UNIMP, "x86: unimplemented rdpmc\n");
247 raise_exception_err(env, EXCP06_ILLOP, 0);
250 #if defined(CONFIG_USER_ONLY)
251 void helper_wrmsr(CPUX86State *env)
255 void helper_rdmsr(CPUX86State *env)
259 void helper_wrmsr(CPUX86State *env)
263 cpu_svm_check_intercept_param(env, SVM_EXIT_MSR, 1);
265 val = ((uint32_t)env->regs[R_EAX]) |
266 ((uint64_t)((uint32_t)env->regs[R_EDX]) << 32);
268 switch ((uint32_t)env->regs[R_ECX]) {
269 case MSR_IA32_SYSENTER_CS:
270 env->sysenter_cs = val & 0xffff;
272 case MSR_IA32_SYSENTER_ESP:
273 env->sysenter_esp = val;
275 case MSR_IA32_SYSENTER_EIP:
276 env->sysenter_eip = val;
278 case MSR_IA32_APICBASE:
279 cpu_set_apic_base(x86_env_get_cpu(env)->apic_state, val);
283 uint64_t update_mask;
286 if (env->features[FEAT_8000_0001_EDX] & CPUID_EXT2_SYSCALL) {
287 update_mask |= MSR_EFER_SCE;
289 if (env->features[FEAT_8000_0001_EDX] & CPUID_EXT2_LM) {
290 update_mask |= MSR_EFER_LME;
292 if (env->features[FEAT_8000_0001_EDX] & CPUID_EXT2_FFXSR) {
293 update_mask |= MSR_EFER_FFXSR;
295 if (env->features[FEAT_8000_0001_EDX] & CPUID_EXT2_NX) {
296 update_mask |= MSR_EFER_NXE;
298 if (env->features[FEAT_8000_0001_ECX] & CPUID_EXT3_SVM) {
299 update_mask |= MSR_EFER_SVME;
301 if (env->features[FEAT_8000_0001_EDX] & CPUID_EXT2_FFXSR) {
302 update_mask |= MSR_EFER_FFXSR;
304 cpu_load_efer(env, (env->efer & ~update_mask) |
305 (val & update_mask));
314 case MSR_VM_HSAVE_PA:
328 env->segs[R_FS].base = val;
331 env->segs[R_GS].base = val;
333 case MSR_KERNELGSBASE:
334 env->kernelgsbase = val;
337 case MSR_MTRRphysBase(0):
338 case MSR_MTRRphysBase(1):
339 case MSR_MTRRphysBase(2):
340 case MSR_MTRRphysBase(3):
341 case MSR_MTRRphysBase(4):
342 case MSR_MTRRphysBase(5):
343 case MSR_MTRRphysBase(6):
344 case MSR_MTRRphysBase(7):
345 env->mtrr_var[((uint32_t)env->regs[R_ECX] -
346 MSR_MTRRphysBase(0)) / 2].base = val;
348 case MSR_MTRRphysMask(0):
349 case MSR_MTRRphysMask(1):
350 case MSR_MTRRphysMask(2):
351 case MSR_MTRRphysMask(3):
352 case MSR_MTRRphysMask(4):
353 case MSR_MTRRphysMask(5):
354 case MSR_MTRRphysMask(6):
355 case MSR_MTRRphysMask(7):
356 env->mtrr_var[((uint32_t)env->regs[R_ECX] -
357 MSR_MTRRphysMask(0)) / 2].mask = val;
359 case MSR_MTRRfix64K_00000:
360 env->mtrr_fixed[(uint32_t)env->regs[R_ECX] -
361 MSR_MTRRfix64K_00000] = val;
363 case MSR_MTRRfix16K_80000:
364 case MSR_MTRRfix16K_A0000:
365 env->mtrr_fixed[(uint32_t)env->regs[R_ECX] -
366 MSR_MTRRfix16K_80000 + 1] = val;
368 case MSR_MTRRfix4K_C0000:
369 case MSR_MTRRfix4K_C8000:
370 case MSR_MTRRfix4K_D0000:
371 case MSR_MTRRfix4K_D8000:
372 case MSR_MTRRfix4K_E0000:
373 case MSR_MTRRfix4K_E8000:
374 case MSR_MTRRfix4K_F0000:
375 case MSR_MTRRfix4K_F8000:
376 env->mtrr_fixed[(uint32_t)env->regs[R_ECX] -
377 MSR_MTRRfix4K_C0000 + 3] = val;
379 case MSR_MTRRdefType:
380 env->mtrr_deftype = val;
383 env->mcg_status = val;
386 if ((env->mcg_cap & MCG_CTL_P)
387 && (val == 0 || val == ~(uint64_t)0)) {
394 case MSR_IA32_MISC_ENABLE:
395 env->msr_ia32_misc_enable = val;
398 if ((uint32_t)env->regs[R_ECX] >= MSR_MC0_CTL
399 && (uint32_t)env->regs[R_ECX] < MSR_MC0_CTL +
400 (4 * env->mcg_cap & 0xff)) {
401 uint32_t offset = (uint32_t)env->regs[R_ECX] - MSR_MC0_CTL;
402 if ((offset & 0x3) != 0
403 || (val == 0 || val == ~(uint64_t)0)) {
404 env->mce_banks[offset] = val;
408 /* XXX: exception? */
413 void helper_rdmsr(CPUX86State *env)
417 cpu_svm_check_intercept_param(env, SVM_EXIT_MSR, 0);
419 switch ((uint32_t)env->regs[R_ECX]) {
420 case MSR_IA32_SYSENTER_CS:
421 val = env->sysenter_cs;
423 case MSR_IA32_SYSENTER_ESP:
424 val = env->sysenter_esp;
426 case MSR_IA32_SYSENTER_EIP:
427 val = env->sysenter_eip;
429 case MSR_IA32_APICBASE:
430 val = cpu_get_apic_base(x86_env_get_cpu(env)->apic_state);
441 case MSR_VM_HSAVE_PA:
444 case MSR_IA32_PERF_STATUS:
445 /* tsc_increment_by_tick */
448 val |= (((uint64_t)4ULL) << 40);
461 val = env->segs[R_FS].base;
464 val = env->segs[R_GS].base;
466 case MSR_KERNELGSBASE:
467 val = env->kernelgsbase;
473 case MSR_MTRRphysBase(0):
474 case MSR_MTRRphysBase(1):
475 case MSR_MTRRphysBase(2):
476 case MSR_MTRRphysBase(3):
477 case MSR_MTRRphysBase(4):
478 case MSR_MTRRphysBase(5):
479 case MSR_MTRRphysBase(6):
480 case MSR_MTRRphysBase(7):
481 val = env->mtrr_var[((uint32_t)env->regs[R_ECX] -
482 MSR_MTRRphysBase(0)) / 2].base;
484 case MSR_MTRRphysMask(0):
485 case MSR_MTRRphysMask(1):
486 case MSR_MTRRphysMask(2):
487 case MSR_MTRRphysMask(3):
488 case MSR_MTRRphysMask(4):
489 case MSR_MTRRphysMask(5):
490 case MSR_MTRRphysMask(6):
491 case MSR_MTRRphysMask(7):
492 val = env->mtrr_var[((uint32_t)env->regs[R_ECX] -
493 MSR_MTRRphysMask(0)) / 2].mask;
495 case MSR_MTRRfix64K_00000:
496 val = env->mtrr_fixed[0];
498 case MSR_MTRRfix16K_80000:
499 case MSR_MTRRfix16K_A0000:
500 val = env->mtrr_fixed[(uint32_t)env->regs[R_ECX] -
501 MSR_MTRRfix16K_80000 + 1];
503 case MSR_MTRRfix4K_C0000:
504 case MSR_MTRRfix4K_C8000:
505 case MSR_MTRRfix4K_D0000:
506 case MSR_MTRRfix4K_D8000:
507 case MSR_MTRRfix4K_E0000:
508 case MSR_MTRRfix4K_E8000:
509 case MSR_MTRRfix4K_F0000:
510 case MSR_MTRRfix4K_F8000:
511 val = env->mtrr_fixed[(uint32_t)env->regs[R_ECX] -
512 MSR_MTRRfix4K_C0000 + 3];
514 case MSR_MTRRdefType:
515 val = env->mtrr_deftype;
518 if (env->features[FEAT_1_EDX] & CPUID_MTRR) {
519 val = MSR_MTRRcap_VCNT | MSR_MTRRcap_FIXRANGE_SUPPORT |
520 MSR_MTRRcap_WC_SUPPORTED;
522 /* XXX: exception? */
530 if (env->mcg_cap & MCG_CTL_P) {
537 val = env->mcg_status;
539 case MSR_IA32_MISC_ENABLE:
540 val = env->msr_ia32_misc_enable;
543 if ((uint32_t)env->regs[R_ECX] >= MSR_MC0_CTL
544 && (uint32_t)env->regs[R_ECX] < MSR_MC0_CTL +
545 (4 * env->mcg_cap & 0xff)) {
546 uint32_t offset = (uint32_t)env->regs[R_ECX] - MSR_MC0_CTL;
547 val = env->mce_banks[offset];
550 /* XXX: exception? */
554 env->regs[R_EAX] = (uint32_t)(val);
555 env->regs[R_EDX] = (uint32_t)(val >> 32);
559 static void do_pause(X86CPU *cpu)
561 CPUState *cs = CPU(cpu);
563 /* Just let another CPU run. */
564 cs->exception_index = EXCP_INTERRUPT;
568 static void do_hlt(X86CPU *cpu)
570 CPUState *cs = CPU(cpu);
571 CPUX86State *env = &cpu->env;
573 env->hflags &= ~HF_INHIBIT_IRQ_MASK; /* needed if sti is just before */
575 cs->exception_index = EXCP_HLT;
579 void helper_hlt(CPUX86State *env, int next_eip_addend)
581 X86CPU *cpu = x86_env_get_cpu(env);
583 cpu_svm_check_intercept_param(env, SVM_EXIT_HLT, 0);
584 env->eip += next_eip_addend;
589 void helper_monitor(CPUX86State *env, target_ulong ptr)
591 if ((uint32_t)env->regs[R_ECX] != 0) {
592 raise_exception(env, EXCP0D_GPF);
594 /* XXX: store address? */
595 cpu_svm_check_intercept_param(env, SVM_EXIT_MONITOR, 0);
598 void helper_mwait(CPUX86State *env, int next_eip_addend)
603 if ((uint32_t)env->regs[R_ECX] != 0) {
604 raise_exception(env, EXCP0D_GPF);
606 cpu_svm_check_intercept_param(env, SVM_EXIT_MWAIT, 0);
607 env->eip += next_eip_addend;
609 cpu = x86_env_get_cpu(env);
611 /* XXX: not complete but not completely erroneous */
612 if (cs->cpu_index != 0 || CPU_NEXT(cs) != NULL) {
619 void helper_pause(CPUX86State *env, int next_eip_addend)
621 X86CPU *cpu = x86_env_get_cpu(env);
623 cpu_svm_check_intercept_param(env, SVM_EXIT_PAUSE, 0);
624 env->eip += next_eip_addend;
629 void helper_debug(CPUX86State *env)
631 CPUState *cs = CPU(x86_env_get_cpu(env));
633 cs->exception_index = EXCP_DEBUG;