2 * i386 helpers (without register variable usage)
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 "sysemu/kvm.h"
23 #ifndef CONFIG_USER_ONLY
24 #include "sysemu/sysemu.h"
25 #include "monitor/monitor.h"
28 static void cpu_x86_version(CPUX86State *env, int *family, int *model)
30 int cpuver = env->cpuid_version;
32 if (family == NULL || model == NULL) {
36 *family = (cpuver >> 8) & 0x0f;
37 *model = ((cpuver >> 12) & 0xf0) + ((cpuver >> 4) & 0x0f);
40 /* Broadcast MCA signal for processor version 06H_EH and above */
41 int cpu_x86_support_mca_broadcast(CPUX86State *env)
46 cpu_x86_version(env, &family, &model);
47 if ((family == 6 && model >= 14) || family > 6) {
54 /***********************************************************/
57 static const char *cc_op_str[CC_OP_NB] = {
124 cpu_x86_dump_seg_cache(CPUX86State *env, FILE *f, fprintf_function cpu_fprintf,
125 const char *name, struct SegmentCache *sc)
128 if (env->hflags & HF_CS64_MASK) {
129 cpu_fprintf(f, "%-3s=%04x %016" PRIx64 " %08x %08x", name,
130 sc->selector, sc->base, sc->limit, sc->flags & 0x00ffff00);
134 cpu_fprintf(f, "%-3s=%04x %08x %08x %08x", name, sc->selector,
135 (uint32_t)sc->base, sc->limit, sc->flags & 0x00ffff00);
138 if (!(env->hflags & HF_PE_MASK) || !(sc->flags & DESC_P_MASK))
141 cpu_fprintf(f, " DPL=%d ", (sc->flags & DESC_DPL_MASK) >> DESC_DPL_SHIFT);
142 if (sc->flags & DESC_S_MASK) {
143 if (sc->flags & DESC_CS_MASK) {
144 cpu_fprintf(f, (sc->flags & DESC_L_MASK) ? "CS64" :
145 ((sc->flags & DESC_B_MASK) ? "CS32" : "CS16"));
146 cpu_fprintf(f, " [%c%c", (sc->flags & DESC_C_MASK) ? 'C' : '-',
147 (sc->flags & DESC_R_MASK) ? 'R' : '-');
150 (sc->flags & DESC_B_MASK || env->hflags & HF_LMA_MASK)
152 cpu_fprintf(f, " [%c%c", (sc->flags & DESC_E_MASK) ? 'E' : '-',
153 (sc->flags & DESC_W_MASK) ? 'W' : '-');
155 cpu_fprintf(f, "%c]", (sc->flags & DESC_A_MASK) ? 'A' : '-');
157 static const char *sys_type_name[2][16] = {
159 "Reserved", "TSS16-avl", "LDT", "TSS16-busy",
160 "CallGate16", "TaskGate", "IntGate16", "TrapGate16",
161 "Reserved", "TSS32-avl", "Reserved", "TSS32-busy",
162 "CallGate32", "Reserved", "IntGate32", "TrapGate32"
165 "<hiword>", "Reserved", "LDT", "Reserved", "Reserved",
166 "Reserved", "Reserved", "Reserved", "Reserved",
167 "TSS64-avl", "Reserved", "TSS64-busy", "CallGate64",
168 "Reserved", "IntGate64", "TrapGate64"
172 sys_type_name[(env->hflags & HF_LMA_MASK) ? 1 : 0]
173 [(sc->flags & DESC_TYPE_MASK)
174 >> DESC_TYPE_SHIFT]);
177 cpu_fprintf(f, "\n");
180 #define DUMP_CODE_BYTES_TOTAL 50
181 #define DUMP_CODE_BYTES_BACKWARD 20
183 void x86_cpu_dump_state(CPUState *cs, FILE *f, fprintf_function cpu_fprintf,
186 X86CPU *cpu = X86_CPU(cs);
187 CPUX86State *env = &cpu->env;
190 static const char *seg_name[6] = { "ES", "CS", "SS", "DS", "FS", "GS" };
192 eflags = cpu_compute_eflags(env);
194 if (env->hflags & HF_CS64_MASK) {
196 "RAX=%016" PRIx64 " RBX=%016" PRIx64 " RCX=%016" PRIx64 " RDX=%016" PRIx64 "\n"
197 "RSI=%016" PRIx64 " RDI=%016" PRIx64 " RBP=%016" PRIx64 " RSP=%016" PRIx64 "\n"
198 "R8 =%016" PRIx64 " R9 =%016" PRIx64 " R10=%016" PRIx64 " R11=%016" PRIx64 "\n"
199 "R12=%016" PRIx64 " R13=%016" PRIx64 " R14=%016" PRIx64 " R15=%016" PRIx64 "\n"
200 "RIP=%016" PRIx64 " RFL=%08x [%c%c%c%c%c%c%c] CPL=%d II=%d A20=%d SMM=%d HLT=%d\n",
218 eflags & DF_MASK ? 'D' : '-',
219 eflags & CC_O ? 'O' : '-',
220 eflags & CC_S ? 'S' : '-',
221 eflags & CC_Z ? 'Z' : '-',
222 eflags & CC_A ? 'A' : '-',
223 eflags & CC_P ? 'P' : '-',
224 eflags & CC_C ? 'C' : '-',
225 env->hflags & HF_CPL_MASK,
226 (env->hflags >> HF_INHIBIT_IRQ_SHIFT) & 1,
227 (env->a20_mask >> 20) & 1,
228 (env->hflags >> HF_SMM_SHIFT) & 1,
233 cpu_fprintf(f, "EAX=%08x EBX=%08x ECX=%08x EDX=%08x\n"
234 "ESI=%08x EDI=%08x EBP=%08x ESP=%08x\n"
235 "EIP=%08x EFL=%08x [%c%c%c%c%c%c%c] CPL=%d II=%d A20=%d SMM=%d HLT=%d\n",
236 (uint32_t)env->regs[R_EAX],
237 (uint32_t)env->regs[R_EBX],
238 (uint32_t)env->regs[R_ECX],
239 (uint32_t)env->regs[R_EDX],
240 (uint32_t)env->regs[R_ESI],
241 (uint32_t)env->regs[R_EDI],
242 (uint32_t)env->regs[R_EBP],
243 (uint32_t)env->regs[R_ESP],
244 (uint32_t)env->eip, eflags,
245 eflags & DF_MASK ? 'D' : '-',
246 eflags & CC_O ? 'O' : '-',
247 eflags & CC_S ? 'S' : '-',
248 eflags & CC_Z ? 'Z' : '-',
249 eflags & CC_A ? 'A' : '-',
250 eflags & CC_P ? 'P' : '-',
251 eflags & CC_C ? 'C' : '-',
252 env->hflags & HF_CPL_MASK,
253 (env->hflags >> HF_INHIBIT_IRQ_SHIFT) & 1,
254 (env->a20_mask >> 20) & 1,
255 (env->hflags >> HF_SMM_SHIFT) & 1,
259 for(i = 0; i < 6; i++) {
260 cpu_x86_dump_seg_cache(env, f, cpu_fprintf, seg_name[i],
263 cpu_x86_dump_seg_cache(env, f, cpu_fprintf, "LDT", &env->ldt);
264 cpu_x86_dump_seg_cache(env, f, cpu_fprintf, "TR", &env->tr);
267 if (env->hflags & HF_LMA_MASK) {
268 cpu_fprintf(f, "GDT= %016" PRIx64 " %08x\n",
269 env->gdt.base, env->gdt.limit);
270 cpu_fprintf(f, "IDT= %016" PRIx64 " %08x\n",
271 env->idt.base, env->idt.limit);
272 cpu_fprintf(f, "CR0=%08x CR2=%016" PRIx64 " CR3=%016" PRIx64 " CR4=%08x\n",
273 (uint32_t)env->cr[0],
276 (uint32_t)env->cr[4]);
277 for(i = 0; i < 4; i++)
278 cpu_fprintf(f, "DR%d=%016" PRIx64 " ", i, env->dr[i]);
279 cpu_fprintf(f, "\nDR6=%016" PRIx64 " DR7=%016" PRIx64 "\n",
280 env->dr[6], env->dr[7]);
284 cpu_fprintf(f, "GDT= %08x %08x\n",
285 (uint32_t)env->gdt.base, env->gdt.limit);
286 cpu_fprintf(f, "IDT= %08x %08x\n",
287 (uint32_t)env->idt.base, env->idt.limit);
288 cpu_fprintf(f, "CR0=%08x CR2=%08x CR3=%08x CR4=%08x\n",
289 (uint32_t)env->cr[0],
290 (uint32_t)env->cr[2],
291 (uint32_t)env->cr[3],
292 (uint32_t)env->cr[4]);
293 for(i = 0; i < 4; i++) {
294 cpu_fprintf(f, "DR%d=" TARGET_FMT_lx " ", i, env->dr[i]);
296 cpu_fprintf(f, "\nDR6=" TARGET_FMT_lx " DR7=" TARGET_FMT_lx "\n",
297 env->dr[6], env->dr[7]);
299 if (flags & CPU_DUMP_CCOP) {
300 if ((unsigned)env->cc_op < CC_OP_NB)
301 snprintf(cc_op_name, sizeof(cc_op_name), "%s", cc_op_str[env->cc_op]);
303 snprintf(cc_op_name, sizeof(cc_op_name), "[%d]", env->cc_op);
305 if (env->hflags & HF_CS64_MASK) {
306 cpu_fprintf(f, "CCS=%016" PRIx64 " CCD=%016" PRIx64 " CCO=%-8s\n",
307 env->cc_src, env->cc_dst,
312 cpu_fprintf(f, "CCS=%08x CCD=%08x CCO=%-8s\n",
313 (uint32_t)env->cc_src, (uint32_t)env->cc_dst,
317 cpu_fprintf(f, "EFER=%016" PRIx64 "\n", env->efer);
318 if (flags & CPU_DUMP_FPU) {
321 for(i = 0; i < 8; i++) {
322 fptag |= ((!env->fptags[i]) << i);
324 cpu_fprintf(f, "FCW=%04x FSW=%04x [ST=%d] FTW=%02x MXCSR=%08x\n",
326 (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11,
332 u.d = env->fpregs[i].d;
333 cpu_fprintf(f, "FPR%d=%016" PRIx64 " %04x",
334 i, u.l.lower, u.l.upper);
336 cpu_fprintf(f, "\n");
340 if (env->hflags & HF_CS64_MASK)
345 cpu_fprintf(f, "XMM%02d=%08x%08x%08x%08x",
347 env->xmm_regs[i].XMM_L(3),
348 env->xmm_regs[i].XMM_L(2),
349 env->xmm_regs[i].XMM_L(1),
350 env->xmm_regs[i].XMM_L(0));
352 cpu_fprintf(f, "\n");
357 if (flags & CPU_DUMP_CODE) {
358 target_ulong base = env->segs[R_CS].base + env->eip;
359 target_ulong offs = MIN(env->eip, DUMP_CODE_BYTES_BACKWARD);
363 cpu_fprintf(f, "Code=");
364 for (i = 0; i < DUMP_CODE_BYTES_TOTAL; i++) {
365 if (cpu_memory_rw_debug(cs, base - offs + i, &code, 1, 0) == 0) {
366 snprintf(codestr, sizeof(codestr), "%02x", code);
368 snprintf(codestr, sizeof(codestr), "??");
370 cpu_fprintf(f, "%s%s%s%s", i > 0 ? " " : "",
371 i == offs ? "<" : "", codestr, i == offs ? ">" : "");
373 cpu_fprintf(f, "\n");
377 /***********************************************************/
379 /* XXX: add PGE support */
381 void x86_cpu_set_a20(X86CPU *cpu, int a20_state)
383 CPUX86State *env = &cpu->env;
385 a20_state = (a20_state != 0);
386 if (a20_state != ((env->a20_mask >> 20) & 1)) {
387 CPUState *cs = CPU(cpu);
389 qemu_log_mask(CPU_LOG_MMU, "A20 update: a20=%d\n", a20_state);
390 /* if the cpu is currently executing code, we must unlink it and
391 all the potentially executing TB */
392 cpu_interrupt(cs, CPU_INTERRUPT_EXITTB);
394 /* when a20 is changed, all the MMU mappings are invalid, so
395 we must flush everything */
397 env->a20_mask = ~(1 << 20) | (a20_state << 20);
401 void cpu_x86_update_cr0(CPUX86State *env, uint32_t new_cr0)
403 X86CPU *cpu = x86_env_get_cpu(env);
406 qemu_log_mask(CPU_LOG_MMU, "CR0 update: CR0=0x%08x\n", new_cr0);
407 if ((new_cr0 & (CR0_PG_MASK | CR0_WP_MASK | CR0_PE_MASK)) !=
408 (env->cr[0] & (CR0_PG_MASK | CR0_WP_MASK | CR0_PE_MASK))) {
409 tlb_flush(CPU(cpu), 1);
413 if (!(env->cr[0] & CR0_PG_MASK) && (new_cr0 & CR0_PG_MASK) &&
414 (env->efer & MSR_EFER_LME)) {
415 /* enter in long mode */
416 /* XXX: generate an exception */
417 if (!(env->cr[4] & CR4_PAE_MASK))
419 env->efer |= MSR_EFER_LMA;
420 env->hflags |= HF_LMA_MASK;
421 } else if ((env->cr[0] & CR0_PG_MASK) && !(new_cr0 & CR0_PG_MASK) &&
422 (env->efer & MSR_EFER_LMA)) {
424 env->efer &= ~MSR_EFER_LMA;
425 env->hflags &= ~(HF_LMA_MASK | HF_CS64_MASK);
426 env->eip &= 0xffffffff;
429 env->cr[0] = new_cr0 | CR0_ET_MASK;
431 /* update PE flag in hidden flags */
432 pe_state = (env->cr[0] & CR0_PE_MASK);
433 env->hflags = (env->hflags & ~HF_PE_MASK) | (pe_state << HF_PE_SHIFT);
434 /* ensure that ADDSEG is always set in real mode */
435 env->hflags |= ((pe_state ^ 1) << HF_ADDSEG_SHIFT);
436 /* update FPU flags */
437 env->hflags = (env->hflags & ~(HF_MP_MASK | HF_EM_MASK | HF_TS_MASK)) |
438 ((new_cr0 << (HF_MP_SHIFT - 1)) & (HF_MP_MASK | HF_EM_MASK | HF_TS_MASK));
441 /* XXX: in legacy PAE mode, generate a GPF if reserved bits are set in
443 void cpu_x86_update_cr3(CPUX86State *env, target_ulong new_cr3)
445 X86CPU *cpu = x86_env_get_cpu(env);
447 env->cr[3] = new_cr3;
448 if (env->cr[0] & CR0_PG_MASK) {
449 qemu_log_mask(CPU_LOG_MMU,
450 "CR3 update: CR3=" TARGET_FMT_lx "\n", new_cr3);
451 tlb_flush(CPU(cpu), 0);
455 void cpu_x86_update_cr4(CPUX86State *env, uint32_t new_cr4)
457 X86CPU *cpu = x86_env_get_cpu(env);
459 #if defined(DEBUG_MMU)
460 printf("CR4 update: CR4=%08x\n", (uint32_t)env->cr[4]);
462 if ((new_cr4 ^ env->cr[4]) &
463 (CR4_PGE_MASK | CR4_PAE_MASK | CR4_PSE_MASK |
464 CR4_SMEP_MASK | CR4_SMAP_MASK)) {
465 tlb_flush(CPU(cpu), 1);
468 if (!(env->features[FEAT_1_EDX] & CPUID_SSE)) {
469 new_cr4 &= ~CR4_OSFXSR_MASK;
471 env->hflags &= ~HF_OSFXSR_MASK;
472 if (new_cr4 & CR4_OSFXSR_MASK) {
473 env->hflags |= HF_OSFXSR_MASK;
476 if (!(env->features[FEAT_7_0_EBX] & CPUID_7_0_EBX_SMAP)) {
477 new_cr4 &= ~CR4_SMAP_MASK;
479 env->hflags &= ~HF_SMAP_MASK;
480 if (new_cr4 & CR4_SMAP_MASK) {
481 env->hflags |= HF_SMAP_MASK;
484 env->cr[4] = new_cr4;
487 #if defined(CONFIG_USER_ONLY)
489 int x86_cpu_handle_mmu_fault(CPUState *cs, vaddr addr,
490 int is_write, int mmu_idx)
492 X86CPU *cpu = X86_CPU(cs);
493 CPUX86State *env = &cpu->env;
495 /* user mode only emulation */
498 env->error_code = (is_write << PG_ERROR_W_BIT);
499 env->error_code |= PG_ERROR_U_MASK;
500 cs->exception_index = EXCP0E_PAGE;
507 * -1 = cannot handle fault
508 * 0 = nothing more to do
509 * 1 = generate PF fault
511 int x86_cpu_handle_mmu_fault(CPUState *cs, vaddr addr,
512 int is_write1, int mmu_idx)
514 X86CPU *cpu = X86_CPU(cs);
515 CPUX86State *env = &cpu->env;
517 target_ulong pde_addr, pte_addr;
519 int is_dirty, prot, page_size, is_write, is_user;
521 uint64_t rsvd_mask = PG_HI_RSVD_MASK;
522 uint32_t page_offset;
525 is_user = mmu_idx == MMU_USER_IDX;
526 #if defined(DEBUG_MMU)
527 printf("MMU fault: addr=%" VADDR_PRIx " w=%d u=%d eip=" TARGET_FMT_lx "\n",
528 addr, is_write1, is_user, env->eip);
530 is_write = is_write1 & 1;
532 if (!(env->cr[0] & CR0_PG_MASK)) {
535 if (!(env->hflags & HF_LMA_MASK)) {
536 /* Without long mode we can only address 32bits in real mode */
540 prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
545 if (!(env->efer & MSR_EFER_NXE)) {
546 rsvd_mask |= PG_NX_MASK;
549 if (env->cr[4] & CR4_PAE_MASK) {
551 target_ulong pdpe_addr;
554 if (env->hflags & HF_LMA_MASK) {
555 uint64_t pml4e_addr, pml4e;
558 /* test virtual address sign extension */
559 sext = (int64_t)addr >> 47;
560 if (sext != 0 && sext != -1) {
562 cs->exception_index = EXCP0D_GPF;
566 pml4e_addr = ((env->cr[3] & ~0xfff) + (((addr >> 39) & 0x1ff) << 3)) &
568 pml4e = x86_ldq_phys(cs, pml4e_addr);
569 if (!(pml4e & PG_PRESENT_MASK)) {
572 if (pml4e & (rsvd_mask | PG_PSE_MASK)) {
575 if (!(pml4e & PG_ACCESSED_MASK)) {
576 pml4e |= PG_ACCESSED_MASK;
577 x86_stl_phys_notdirty(cs, pml4e_addr, pml4e);
579 ptep = pml4e ^ PG_NX_MASK;
580 pdpe_addr = ((pml4e & PG_ADDRESS_MASK) + (((addr >> 30) & 0x1ff) << 3)) &
582 pdpe = x86_ldq_phys(cs, pdpe_addr);
583 if (!(pdpe & PG_PRESENT_MASK)) {
586 if (pdpe & rsvd_mask) {
589 ptep &= pdpe ^ PG_NX_MASK;
590 if (!(pdpe & PG_ACCESSED_MASK)) {
591 pdpe |= PG_ACCESSED_MASK;
592 x86_stl_phys_notdirty(cs, pdpe_addr, pdpe);
594 if (pdpe & PG_PSE_MASK) {
596 page_size = 1024 * 1024 * 1024;
597 pte_addr = pdpe_addr;
599 goto do_check_protect;
604 /* XXX: load them when cr3 is loaded ? */
605 pdpe_addr = ((env->cr[3] & ~0x1f) + ((addr >> 27) & 0x18)) &
607 pdpe = x86_ldq_phys(cs, pdpe_addr);
608 if (!(pdpe & PG_PRESENT_MASK)) {
611 rsvd_mask |= PG_HI_USER_MASK;
612 if (pdpe & (rsvd_mask | PG_NX_MASK)) {
615 ptep = PG_NX_MASK | PG_USER_MASK | PG_RW_MASK;
618 pde_addr = ((pdpe & PG_ADDRESS_MASK) + (((addr >> 21) & 0x1ff) << 3)) &
620 pde = x86_ldq_phys(cs, pde_addr);
621 if (!(pde & PG_PRESENT_MASK)) {
624 if (pde & rsvd_mask) {
627 ptep &= pde ^ PG_NX_MASK;
628 if (pde & PG_PSE_MASK) {
630 page_size = 2048 * 1024;
633 goto do_check_protect;
636 if (!(pde & PG_ACCESSED_MASK)) {
637 pde |= PG_ACCESSED_MASK;
638 x86_stl_phys_notdirty(cs, pde_addr, pde);
640 pte_addr = ((pde & PG_ADDRESS_MASK) + (((addr >> 12) & 0x1ff) << 3)) &
642 pte = x86_ldq_phys(cs, pte_addr);
643 if (!(pte & PG_PRESENT_MASK)) {
646 if (pte & rsvd_mask) {
649 /* combine pde and pte nx, user and rw protections */
650 ptep &= pte ^ PG_NX_MASK;
655 /* page directory entry */
656 pde_addr = ((env->cr[3] & ~0xfff) + ((addr >> 20) & 0xffc)) &
658 pde = x86_ldl_phys(cs, pde_addr);
659 if (!(pde & PG_PRESENT_MASK)) {
662 ptep = pde | PG_NX_MASK;
664 /* if PSE bit is set, then we use a 4MB page */
665 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
666 page_size = 4096 * 1024;
669 /* Bits 20-13 provide bits 39-32 of the address, bit 21 is reserved.
670 * Leave bits 20-13 in place for setting accessed/dirty bits below.
672 pte = pde | ((pde & 0x1fe000) << (32 - 13));
673 rsvd_mask = 0x200000;
674 goto do_check_protect_pse36;
677 if (!(pde & PG_ACCESSED_MASK)) {
678 pde |= PG_ACCESSED_MASK;
679 x86_stl_phys_notdirty(cs, pde_addr, pde);
682 /* page directory entry */
683 pte_addr = ((pde & ~0xfff) + ((addr >> 10) & 0xffc)) &
685 pte = x86_ldl_phys(cs, pte_addr);
686 if (!(pte & PG_PRESENT_MASK)) {
689 /* combine pde and pte user and rw protections */
690 ptep &= pte | PG_NX_MASK;
696 rsvd_mask |= (page_size - 1) & PG_ADDRESS_MASK & ~PG_PSE_PAT_MASK;
697 do_check_protect_pse36:
698 if (pte & rsvd_mask) {
702 if ((ptep & PG_NX_MASK) && is_write1 == 2) {
703 goto do_fault_protect;
707 if (!(ptep & PG_USER_MASK)) {
708 goto do_fault_protect;
710 if (is_write && !(ptep & PG_RW_MASK)) {
711 goto do_fault_protect;
716 if (is_write1 != 2 && (ptep & PG_USER_MASK)) {
717 goto do_fault_protect;
720 case MMU_KNOSMAP_IDX:
721 if (is_write1 == 2 && (env->cr[4] & CR4_SMEP_MASK) &&
722 (ptep & PG_USER_MASK)) {
723 goto do_fault_protect;
725 if ((env->cr[0] & CR0_WP_MASK) &&
726 is_write && !(ptep & PG_RW_MASK)) {
727 goto do_fault_protect;
731 default: /* cannot happen */
734 is_dirty = is_write && !(pte & PG_DIRTY_MASK);
735 if (!(pte & PG_ACCESSED_MASK) || is_dirty) {
736 pte |= PG_ACCESSED_MASK;
738 pte |= PG_DIRTY_MASK;
740 x86_stl_phys_notdirty(cs, pte_addr, pte);
743 /* the page can be put in the TLB */
745 if (!(ptep & PG_NX_MASK) &&
746 (mmu_idx == MMU_USER_IDX ||
747 !((env->cr[4] & CR4_SMEP_MASK) && (ptep & PG_USER_MASK)))) {
750 if (pte & PG_DIRTY_MASK) {
751 /* only set write access if already dirty... otherwise wait
754 if (ptep & PG_RW_MASK)
757 if (!(env->cr[0] & CR0_WP_MASK) ||
763 pte = pte & env->a20_mask;
765 /* align to page_size */
766 pte &= PG_ADDRESS_MASK & ~(page_size - 1);
768 /* Even if 4MB pages, we map only one 4KB page in the cache to
769 avoid filling it too fast */
770 vaddr = addr & TARGET_PAGE_MASK;
771 page_offset = vaddr & (page_size - 1);
772 paddr = pte + page_offset;
774 tlb_set_page_with_attrs(cs, vaddr, paddr, cpu_get_mem_attrs(env),
775 prot, mmu_idx, page_size);
778 error_code |= PG_ERROR_RSVD_MASK;
780 error_code |= PG_ERROR_P_MASK;
782 error_code |= (is_write << PG_ERROR_W_BIT);
784 error_code |= PG_ERROR_U_MASK;
785 if (is_write1 == 2 &&
786 (((env->efer & MSR_EFER_NXE) &&
787 (env->cr[4] & CR4_PAE_MASK)) ||
788 (env->cr[4] & CR4_SMEP_MASK)))
789 error_code |= PG_ERROR_I_D_MASK;
790 if (env->intercept_exceptions & (1 << EXCP0E_PAGE)) {
791 /* cr2 is not modified in case of exceptions */
793 env->vm_vmcb + offsetof(struct vmcb, control.exit_info_2),
798 env->error_code = error_code;
799 cs->exception_index = EXCP0E_PAGE;
803 hwaddr x86_cpu_get_phys_page_debug(CPUState *cs, vaddr addr)
805 X86CPU *cpu = X86_CPU(cs);
806 CPUX86State *env = &cpu->env;
807 target_ulong pde_addr, pte_addr;
809 uint32_t page_offset;
812 if (!(env->cr[0] & CR0_PG_MASK)) {
813 pte = addr & env->a20_mask;
815 } else if (env->cr[4] & CR4_PAE_MASK) {
816 target_ulong pdpe_addr;
820 if (env->hflags & HF_LMA_MASK) {
821 uint64_t pml4e_addr, pml4e;
824 /* test virtual address sign extension */
825 sext = (int64_t)addr >> 47;
826 if (sext != 0 && sext != -1) {
829 pml4e_addr = ((env->cr[3] & ~0xfff) + (((addr >> 39) & 0x1ff) << 3)) &
831 pml4e = x86_ldq_phys(cs, pml4e_addr);
832 if (!(pml4e & PG_PRESENT_MASK)) {
835 pdpe_addr = ((pml4e & PG_ADDRESS_MASK) +
836 (((addr >> 30) & 0x1ff) << 3)) & env->a20_mask;
837 pdpe = x86_ldq_phys(cs, pdpe_addr);
838 if (!(pdpe & PG_PRESENT_MASK)) {
841 if (pdpe & PG_PSE_MASK) {
842 page_size = 1024 * 1024 * 1024;
850 pdpe_addr = ((env->cr[3] & ~0x1f) + ((addr >> 27) & 0x18)) &
852 pdpe = x86_ldq_phys(cs, pdpe_addr);
853 if (!(pdpe & PG_PRESENT_MASK))
857 pde_addr = ((pdpe & PG_ADDRESS_MASK) +
858 (((addr >> 21) & 0x1ff) << 3)) & env->a20_mask;
859 pde = x86_ldq_phys(cs, pde_addr);
860 if (!(pde & PG_PRESENT_MASK)) {
863 if (pde & PG_PSE_MASK) {
865 page_size = 2048 * 1024;
869 pte_addr = ((pde & PG_ADDRESS_MASK) +
870 (((addr >> 12) & 0x1ff) << 3)) & env->a20_mask;
872 pte = x86_ldq_phys(cs, pte_addr);
874 if (!(pte & PG_PRESENT_MASK)) {
880 /* page directory entry */
881 pde_addr = ((env->cr[3] & ~0xfff) + ((addr >> 20) & 0xffc)) & env->a20_mask;
882 pde = x86_ldl_phys(cs, pde_addr);
883 if (!(pde & PG_PRESENT_MASK))
885 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
886 pte = pde | ((pde & 0x1fe000) << (32 - 13));
887 page_size = 4096 * 1024;
889 /* page directory entry */
890 pte_addr = ((pde & ~0xfff) + ((addr >> 10) & 0xffc)) & env->a20_mask;
891 pte = x86_ldl_phys(cs, pte_addr);
892 if (!(pte & PG_PRESENT_MASK)) {
897 pte = pte & env->a20_mask;
903 pte &= PG_ADDRESS_MASK & ~(page_size - 1);
904 page_offset = (addr & TARGET_PAGE_MASK) & (page_size - 1);
905 return pte | page_offset;
908 void hw_breakpoint_insert(CPUX86State *env, int index)
910 CPUState *cs = CPU(x86_env_get_cpu(env));
911 int type = 0, err = 0;
913 switch (hw_breakpoint_type(env->dr[7], index)) {
914 case DR7_TYPE_BP_INST:
915 if (hw_breakpoint_enabled(env->dr[7], index)) {
916 err = cpu_breakpoint_insert(cs, env->dr[index], BP_CPU,
917 &env->cpu_breakpoint[index]);
920 case DR7_TYPE_DATA_WR:
921 type = BP_CPU | BP_MEM_WRITE;
924 /* No support for I/O watchpoints yet */
926 case DR7_TYPE_DATA_RW:
927 type = BP_CPU | BP_MEM_ACCESS;
932 err = cpu_watchpoint_insert(cs, env->dr[index],
933 hw_breakpoint_len(env->dr[7], index),
934 type, &env->cpu_watchpoint[index]);
938 env->cpu_breakpoint[index] = NULL;
942 void hw_breakpoint_remove(CPUX86State *env, int index)
946 if (!env->cpu_breakpoint[index]) {
949 cs = CPU(x86_env_get_cpu(env));
950 switch (hw_breakpoint_type(env->dr[7], index)) {
951 case DR7_TYPE_BP_INST:
952 if (hw_breakpoint_enabled(env->dr[7], index)) {
953 cpu_breakpoint_remove_by_ref(cs, env->cpu_breakpoint[index]);
956 case DR7_TYPE_DATA_WR:
957 case DR7_TYPE_DATA_RW:
958 cpu_watchpoint_remove_by_ref(cs, env->cpu_watchpoint[index]);
961 /* No support for I/O watchpoints yet */
966 bool check_hw_breakpoints(CPUX86State *env, bool force_dr6_update)
970 bool hit_enabled = false;
972 dr6 = env->dr[6] & ~0xf;
973 for (reg = 0; reg < DR7_MAX_BP; reg++) {
974 bool bp_match = false;
975 bool wp_match = false;
977 switch (hw_breakpoint_type(env->dr[7], reg)) {
978 case DR7_TYPE_BP_INST:
979 if (env->dr[reg] == env->eip) {
983 case DR7_TYPE_DATA_WR:
984 case DR7_TYPE_DATA_RW:
985 if (env->cpu_watchpoint[reg] &&
986 env->cpu_watchpoint[reg]->flags & BP_WATCHPOINT_HIT) {
993 if (bp_match || wp_match) {
995 if (hw_breakpoint_enabled(env->dr[7], reg)) {
1001 if (hit_enabled || force_dr6_update) {
1008 void breakpoint_handler(CPUState *cs)
1010 X86CPU *cpu = X86_CPU(cs);
1011 CPUX86State *env = &cpu->env;
1014 if (cs->watchpoint_hit) {
1015 if (cs->watchpoint_hit->flags & BP_CPU) {
1016 cs->watchpoint_hit = NULL;
1017 if (check_hw_breakpoints(env, false)) {
1018 raise_exception(env, EXCP01_DB);
1020 cpu_resume_from_signal(cs, NULL);
1024 QTAILQ_FOREACH(bp, &cs->breakpoints, entry) {
1025 if (bp->pc == env->eip) {
1026 if (bp->flags & BP_CPU) {
1027 check_hw_breakpoints(env, true);
1028 raise_exception(env, EXCP01_DB);
1036 typedef struct MCEInjectionParams {
1041 uint64_t mcg_status;
1045 } MCEInjectionParams;
1047 static void do_inject_x86_mce(void *data)
1049 MCEInjectionParams *params = data;
1050 CPUX86State *cenv = ¶ms->cpu->env;
1051 CPUState *cpu = CPU(params->cpu);
1052 uint64_t *banks = cenv->mce_banks + 4 * params->bank;
1054 cpu_synchronize_state(cpu);
1057 * If there is an MCE exception being processed, ignore this SRAO MCE
1058 * unless unconditional injection was requested.
1060 if (!(params->flags & MCE_INJECT_UNCOND_AO)
1061 && !(params->status & MCI_STATUS_AR)
1062 && (cenv->mcg_status & MCG_STATUS_MCIP)) {
1066 if (params->status & MCI_STATUS_UC) {
1068 * if MSR_MCG_CTL is not all 1s, the uncorrected error
1069 * reporting is disabled
1071 if ((cenv->mcg_cap & MCG_CTL_P) && cenv->mcg_ctl != ~(uint64_t)0) {
1072 monitor_printf(params->mon,
1073 "CPU %d: Uncorrected error reporting disabled\n",
1079 * if MSR_MCi_CTL is not all 1s, the uncorrected error
1080 * reporting is disabled for the bank
1082 if (banks[0] != ~(uint64_t)0) {
1083 monitor_printf(params->mon,
1084 "CPU %d: Uncorrected error reporting disabled for"
1086 cpu->cpu_index, params->bank);
1090 if ((cenv->mcg_status & MCG_STATUS_MCIP) ||
1091 !(cenv->cr[4] & CR4_MCE_MASK)) {
1092 monitor_printf(params->mon,
1093 "CPU %d: Previous MCE still in progress, raising"
1096 qemu_log_mask(CPU_LOG_RESET, "Triple fault\n");
1097 qemu_system_reset_request();
1100 if (banks[1] & MCI_STATUS_VAL) {
1101 params->status |= MCI_STATUS_OVER;
1103 banks[2] = params->addr;
1104 banks[3] = params->misc;
1105 cenv->mcg_status = params->mcg_status;
1106 banks[1] = params->status;
1107 cpu_interrupt(cpu, CPU_INTERRUPT_MCE);
1108 } else if (!(banks[1] & MCI_STATUS_VAL)
1109 || !(banks[1] & MCI_STATUS_UC)) {
1110 if (banks[1] & MCI_STATUS_VAL) {
1111 params->status |= MCI_STATUS_OVER;
1113 banks[2] = params->addr;
1114 banks[3] = params->misc;
1115 banks[1] = params->status;
1117 banks[1] |= MCI_STATUS_OVER;
1121 void cpu_x86_inject_mce(Monitor *mon, X86CPU *cpu, int bank,
1122 uint64_t status, uint64_t mcg_status, uint64_t addr,
1123 uint64_t misc, int flags)
1125 CPUState *cs = CPU(cpu);
1126 CPUX86State *cenv = &cpu->env;
1127 MCEInjectionParams params = {
1132 .mcg_status = mcg_status,
1137 unsigned bank_num = cenv->mcg_cap & 0xff;
1139 if (!cenv->mcg_cap) {
1140 monitor_printf(mon, "MCE injection not supported\n");
1143 if (bank >= bank_num) {
1144 monitor_printf(mon, "Invalid MCE bank number\n");
1147 if (!(status & MCI_STATUS_VAL)) {
1148 monitor_printf(mon, "Invalid MCE status code\n");
1151 if ((flags & MCE_INJECT_BROADCAST)
1152 && !cpu_x86_support_mca_broadcast(cenv)) {
1153 monitor_printf(mon, "Guest CPU does not support MCA broadcast\n");
1157 run_on_cpu(cs, do_inject_x86_mce, ¶ms);
1158 if (flags & MCE_INJECT_BROADCAST) {
1162 params.status = MCI_STATUS_VAL | MCI_STATUS_UC;
1163 params.mcg_status = MCG_STATUS_MCIP | MCG_STATUS_RIPV;
1166 CPU_FOREACH(other_cs) {
1167 if (other_cs == cs) {
1170 params.cpu = X86_CPU(other_cs);
1171 run_on_cpu(other_cs, do_inject_x86_mce, ¶ms);
1176 void cpu_report_tpr_access(CPUX86State *env, TPRAccess access)
1178 X86CPU *cpu = x86_env_get_cpu(env);
1179 CPUState *cs = CPU(cpu);
1181 if (kvm_enabled()) {
1182 env->tpr_access_type = access;
1184 cpu_interrupt(cs, CPU_INTERRUPT_TPR);
1186 cpu_restore_state(cs, cs->mem_io_pc);
1188 apic_handle_tpr_access_report(cpu->apic_state, env->eip, access);
1191 #endif /* !CONFIG_USER_ONLY */
1193 int cpu_x86_get_descr_debug(CPUX86State *env, unsigned int selector,
1194 target_ulong *base, unsigned int *limit,
1195 unsigned int *flags)
1197 X86CPU *cpu = x86_env_get_cpu(env);
1198 CPUState *cs = CPU(cpu);
1208 index = selector & ~7;
1209 ptr = dt->base + index;
1210 if ((index + 7) > dt->limit
1211 || cpu_memory_rw_debug(cs, ptr, (uint8_t *)&e1, sizeof(e1), 0) != 0
1212 || cpu_memory_rw_debug(cs, ptr+4, (uint8_t *)&e2, sizeof(e2), 0) != 0)
1215 *base = ((e1 >> 16) | ((e2 & 0xff) << 16) | (e2 & 0xff000000));
1216 *limit = (e1 & 0xffff) | (e2 & 0x000f0000);
1217 if (e2 & DESC_G_MASK)
1218 *limit = (*limit << 12) | 0xfff;
1224 #if !defined(CONFIG_USER_ONLY)
1225 void do_cpu_init(X86CPU *cpu)
1227 CPUState *cs = CPU(cpu);
1228 CPUX86State *env = &cpu->env;
1229 CPUX86State *save = g_new(CPUX86State, 1);
1230 int sipi = cs->interrupt_request & CPU_INTERRUPT_SIPI;
1235 cs->interrupt_request = sipi;
1236 memcpy(&env->start_init_save, &save->start_init_save,
1237 offsetof(CPUX86State, end_init_save) -
1238 offsetof(CPUX86State, start_init_save));
1241 if (kvm_enabled()) {
1242 kvm_arch_do_init_vcpu(cpu);
1244 apic_init_reset(cpu->apic_state);
1247 void do_cpu_sipi(X86CPU *cpu)
1249 apic_sipi(cpu->apic_state);
1252 void do_cpu_init(X86CPU *cpu)
1255 void do_cpu_sipi(X86CPU *cpu)
1260 /* Frob eflags into and out of the CPU temporary format. */
1262 void x86_cpu_exec_enter(CPUState *cs)
1264 X86CPU *cpu = X86_CPU(cs);
1265 CPUX86State *env = &cpu->env;
1267 CC_SRC = env->eflags & (CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
1268 env->df = 1 - (2 * ((env->eflags >> 10) & 1));
1269 CC_OP = CC_OP_EFLAGS;
1270 env->eflags &= ~(DF_MASK | CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
1273 void x86_cpu_exec_exit(CPUState *cs)
1275 X86CPU *cpu = X86_CPU(cs);
1276 CPUX86State *env = &cpu->env;
1278 env->eflags = cpu_compute_eflags(env);
1281 #ifndef CONFIG_USER_ONLY
1282 uint8_t x86_ldub_phys(CPUState *cs, hwaddr addr)
1284 X86CPU *cpu = X86_CPU(cs);
1285 CPUX86State *env = &cpu->env;
1287 return address_space_ldub(cs->as, addr,
1288 cpu_get_mem_attrs(env),
1292 uint32_t x86_lduw_phys(CPUState *cs, hwaddr addr)
1294 X86CPU *cpu = X86_CPU(cs);
1295 CPUX86State *env = &cpu->env;
1297 return address_space_lduw(cs->as, addr,
1298 cpu_get_mem_attrs(env),
1302 uint32_t x86_ldl_phys(CPUState *cs, hwaddr addr)
1304 X86CPU *cpu = X86_CPU(cs);
1305 CPUX86State *env = &cpu->env;
1307 return address_space_ldl(cs->as, addr,
1308 cpu_get_mem_attrs(env),
1312 uint64_t x86_ldq_phys(CPUState *cs, hwaddr addr)
1314 X86CPU *cpu = X86_CPU(cs);
1315 CPUX86State *env = &cpu->env;
1317 return address_space_ldq(cs->as, addr,
1318 cpu_get_mem_attrs(env),
1322 void x86_stb_phys(CPUState *cs, hwaddr addr, uint8_t val)
1324 X86CPU *cpu = X86_CPU(cs);
1325 CPUX86State *env = &cpu->env;
1327 address_space_stb(cs->as, addr, val,
1328 cpu_get_mem_attrs(env),
1332 void x86_stl_phys_notdirty(CPUState *cs, hwaddr addr, uint32_t val)
1334 X86CPU *cpu = X86_CPU(cs);
1335 CPUX86State *env = &cpu->env;
1337 address_space_stl_notdirty(cs->as, addr, val,
1338 cpu_get_mem_attrs(env),
1342 void x86_stw_phys(CPUState *cs, hwaddr addr, uint32_t val)
1344 X86CPU *cpu = X86_CPU(cs);
1345 CPUX86State *env = &cpu->env;
1347 address_space_stw(cs->as, addr, val,
1348 cpu_get_mem_attrs(env),
1352 void x86_stl_phys(CPUState *cs, hwaddr addr, uint32_t val)
1354 X86CPU *cpu = X86_CPU(cs);
1355 CPUX86State *env = &cpu->env;
1357 address_space_stl(cs->as, addr, val,
1358 cpu_get_mem_attrs(env),
1362 void x86_stq_phys(CPUState *cs, hwaddr addr, uint64_t val)
1364 X86CPU *cpu = X86_CPU(cs);
1365 CPUX86State *env = &cpu->env;
1367 address_space_stq(cs->as, addr, val,
1368 cpu_get_mem_attrs(env),