|
|
| version 1.1, 2003/12/08 00:55:31 | version 1.11, 2004/07/29 13:06:08 |
|---|---|
| Line 27 | Line 27 |
| * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| */ | */ |
| #include "compiler.h" | |
| #include "cpu.h" | #include "cpu.h" |
| #include "memory.h" | |
| #ifdef USE_FPU | #ifdef USE_FPU |
| #include "instructions/fpu/fpu.h" | #include "instructions/fpu/fpu.h" |
| #endif | #endif |
| extern BOOL is_a20(void); /* in ../cpuio.c */ | |
| static char *cpu_reg2str(void) | char * |
| cpu_reg2str(void) | |
| { | { |
| static char buf[512]; | static char buf[512]; |
| sprintf(buf, | snprintf(buf, sizeof(buf), |
| "eax=%08x ebx=%08x ecx=%08x edx=%08x esi=%08x edi=%08x\n" | "eax=%08x ecx=%08x edx=%08x ebx=%08x\n" |
| "eip=%08x esp=%08x ebp=%08x prev_eip=%08x\n" | "esp=%08x ebp=%08x esi=%08x edi=%08x\n" |
| "cs=%04x ss=%04x ds=%04x es=%04x fs=%04x gs=%04x\n" | "eip=%08x prev_eip=%08x\n" |
| "eflag=%08x " | "es=%04x cs=%04x ss=%04x ds=%04x fs=%04x gs=%04x\n" |
| /* ID VIP VIF AC VM RF NT IOPL OF DF IF TF SF ZF AF PF CF */ | "eflag=%08x " |
| "[ ID=%d VIP=%d VIF=%d AC=%d VM=%d RF=%d NT=%d IOPL=%d%d %s %s %s TF=%d %s %s %s %s %s ]\n" | /* ID VIP VIF AC VM RF NT IOPL OF DF IF TF SF ZF AF PF CF */ |
| "gdtr=%08x:%04x idtr=%08x:%04x ldtr=%04x tr=%04x\n" | "[ ID=%d VIP=%d VIF=%d AC=%d VM=%d RF=%d NT=%d IOPL=%d %s %s %s TF=%d %s %s %s %s %s ]\n" |
| "cr0=%08x cr1=%08x cr2=%08x cr3=%08x cr4=%08x mxcsr=%08x\n", | "gdtr=%08x:%04x idtr=%08x:%04x\n" |
| I286_EAX, I286_EBX, I286_ECX, I286_EDX, I286_ESI, I286_EDI, | "ldtr=%04x(%08x:%04x) tr=%04x(%08x:%04x)\n" |
| I286_EIP, I286_ESP, I286_EBP, CPU_PREV_EIP, | "cr0=%08x cr1=%08x cr2=%08x cr3=%08x cr4=%08x mxcsr=%08x", |
| I286_CS, I286_SS, I286_DS, I286_ES, I286_FS, I286_GS, | CPU_EAX, CPU_ECX, CPU_EDX, CPU_EBX, |
| I286_EFLAG, | CPU_ESP, CPU_EBP,CPU_ESI, CPU_EDI, |
| (I286_EFLAG & ID_FLAG) != 0, | CPU_EIP, CPU_PREV_EIP, |
| (I286_EFLAG & VIP_FLAG) != 0, | CPU_CS, CPU_SS, CPU_DS, CPU_ES, CPU_FS, CPU_GS, |
| (I286_EFLAG & VIF_FLAG) != 0, | CPU_EFLAG, |
| (I286_EFLAG & AC_FLAG) != 0, | (CPU_EFLAG & ID_FLAG) != 0, |
| (I286_EFLAG & VM_FLAG) != 0, | (CPU_EFLAG & VIP_FLAG) != 0, |
| (I286_EFLAG & RF_FLAG) != 0, | (CPU_EFLAG & VIF_FLAG) != 0, |
| (I286_EFLAG & NT_FLAG) != 0, | (CPU_EFLAG & AC_FLAG) != 0, |
| I286_EFLAG >> 13 & 1, | (CPU_EFLAG & VM_FLAG) != 0, |
| I286_EFLAG >> 12 & 1, | (CPU_EFLAG & RF_FLAG) != 0, |
| I286_EFLAG & O_FLAG ? "OV" : "NV", | (CPU_EFLAG & NT_FLAG) != 0, |
| I286_EFLAG & D_FLAG ? "UP" : "DN", | (int)((CPU_EFLAG >> 12) & 3), |
| I286_EFLAG & I_FLAG ? "DI" : "EI", | CPU_OV ? "OV" : "NV", |
| (I286_EFLAG & T_FLAG) != 0, | CPU_EFLAG & D_FLAG ? "UP" : "DN", |
| I286_EFLAG & S_FLAG ? "NG" : "PL", | CPU_EFLAG & I_FLAG ? "DI" : "EI", |
| I286_EFLAG & Z_FLAG ? "ZR" : "NZ", | (CPU_EFLAG & T_FLAG) != 0, |
| I286_EFLAG & A_FLAG ? "AC" : "NA", | CPU_EFLAG & S_FLAG ? "NG" : "PL", |
| I286_EFLAG & P_FLAG ? "PE" : "PO", | CPU_EFLAG & Z_FLAG ? "ZR" : "NZ", |
| I286_EFLAG & C_FLAG ? "CY" : "NC", | CPU_EFLAG & A_FLAG ? "AC" : "NA", |
| CPU_GDTR_BASE, CPU_GDTR_LIMIT, CPU_IDTR_BASE, CPU_IDTR_LIMIT, CPU_LDTR, CPU_TR, | CPU_EFLAG & P_FLAG ? "PE" : "PO", |
| CPU_CR0, CPU_CR1, CPU_CR2, CPU_CR3, CPU_CR4, CPU_MXCSR); | CPU_EFLAG & C_FLAG ? "CY" : "NC", |
| CPU_GDTR_BASE, CPU_GDTR_LIMIT, CPU_IDTR_BASE, CPU_IDTR_LIMIT, | |
| CPU_LDTR, CPU_LDTR_BASE, CPU_LDTR_LIMIT, | |
| CPU_TR, CPU_TR_BASE, CPU_TR_LIMIT, | |
| CPU_CR0, CPU_CR1, CPU_CR2, CPU_CR3, CPU_CR4, CPU_MXCSR); | |
| return buf; | return buf; |
| } | } |
| #ifdef USE_FPU | #ifdef USE_FPU |
| static char *fpu_reg2str(void) | char * |
| fpu_reg2str(void) | |
| { | { |
| static char buf[512]; | static char buf[512]; |
| char tmp[128]; | char tmp[128]; |
| int i; | int i; |
| int no; | int no; |
| strcpy(buf, "st=\n"); | strcpy(buf, "st=\n"); |
| for(no = 0; no < 8; no++) | for (no = 0; no < 8; no++) { |
| { | for (i = 9; i >= 0; i--) { |
| for(i = 9; i >= 0; i--) | snprintf(tmp, sizeof(tmp), "%02x", FPU_ST[no][i]); |
| { | strcat(buf, tmp); |
| sprintf(tmp, "%02x", FPU_ST[no][i]); | } |
| strcat(buf, tmp); | strcat(buf, "\n"); |
| } | } |
| strcat(buf, "\n"); | |
| } | snprintf(tmp, sizeof(tmp), |
| "ctrl=%04x status=%04x tag=%04x\n" | |
| sprintf(tmp, | "inst=%08x%04x data=%08x%04x op=%03x\n", |
| "ctrl=%04x status=%04x tag=%04x\n" | FPU_CTRLWORD, |
| "inst=%08x%04x data=%08x%04x op=%03x\n", | FPU_STATUSWORD, |
| FPU_CTRLWORD, | FPU_TAGWORD, |
| FPU_STATUSWORD, | FPU_INSTPTR_OFFSET, FPU_INSTPTR_SEG, |
| FPU_TAGWORD, | FPU_DATAPTR_OFFSET, FPU_DATAPTR_SEG, |
| FPU_INSTPTR_OFFSET, FPU_INSTPTR_SEG, | FPU_LASTINSTOP); |
| FPU_DATAPTR_OFFSET, FPU_DATAPTR_SEG, | strcat(buf, tmp); |
| FPU_LASTINSTOP); | |
| strcat(buf, tmp); | |
| return buf; | return buf; |
| } | } |
| #endif | #endif |
| static char *a20str(void) | static char * |
| a20str(void) | |
| { | { |
| static char buf[32]; | static char buf[32]; |
| sprintf(buf, "a20line=%s\n", is_a20() ? "enable" : "disable"); | snprintf(buf, sizeof(buf), "a20line=%s\n", |
| return buf; | (CPU_STAT_ADRSMASK == 0xffffffff) ? "enable" : "disable"); |
| return buf; | |
| } | } |
| static char *mem2str(DWORD cs, DWORD ip) | void |
| put_cpuinfo(void) | |
| { | { |
| static char buf[128]; | char buf[2048]; |
| char tmp[16]; | |
| int i; | |
| strcpy(buf, "mem=... "); | strcpy(buf, cpu_reg2str()); |
| for(i = -10; i < 0; i++) | strcat(buf, "\n"); |
| { | #ifdef USE_FPU |
| sprintf(tmp, "%02x ", __i286_memoryread((cs << 4) + ip + i)); | strcat(buf, fpu_reg2str()); |
| strcat(buf, tmp); | strcat(buf, "\n"); |
| } | #endif |
| sprintf(tmp, "<%02x> ", __i286_memoryread((cs << 4) + ip)); | strcat(buf, a20str()); |
| strcat(buf, tmp); | |
| for(i = 1; i <= 10; i++) | |
| { | |
| sprintf(tmp, "%02x ", __i286_memoryread((cs << 4) + ip + i)); | |
| strcat(buf, tmp); | |
| } | |
| strcat(buf, "...\n"); | |
| return buf; | printf(buf); |
| } | } |
| void FASTCALL msgbox_str(char *msg) | void |
| dbg_printf(const char *str, ...) | |
| { | { |
| char buf[1024]; | |
| va_list ap; | |
| printf(msg); | va_start(ap, str); |
| fflush(stdout); | vsnprintf(buf, sizeof(buf), str, ap); |
| exit(1); | va_end(ap); |
| strcat(buf, "\n"); | |
| printf(buf); | |
| } | } |
| void FASTCALL msgbox_mem(DWORD no) | void |
| memory_dump(int idx, UINT32 madr) | |
| { | { |
| char buf[2048]; | UINT32 addr; |
| char tmp[16]; | size_t size; |
| size_t s, i; | |
| UINT8 buf[16]; | |
| UINT8 c; | |
| if (madr < 0x80) { | |
| size = madr + 0x80; | |
| addr = 0; | |
| } else { | |
| size = 0x100; | |
| addr = madr - 0x80; | |
| } | |
| VERBOSE(("memory dump\n-- \n")); | |
| for (s = 0; s < size; s++) { | |
| if ((s % 16) == 0) { | |
| VERBOSE(("%08x: ", addr + s)); | |
| memset(buf, '.', sizeof(buf)); | |
| } | |
| c = cpu_vmemoryread(idx, addr + s); | |
| VERBOSE(("%02x ", c)); | |
| if (c >= 0x20 && c <= 0x7e) | |
| buf[s % 16] = c; | |
| if ((s % 16) == 15) { | |
| VERBOSE(("| ")); | |
| for (i = 0; i < sizeof(buf); i++) | |
| VERBOSE(("%c", buf[i])); | |
| VERBOSE(("\n")); | |
| } | |
| } | |
| } | |
| strcpy(buf, cpu_reg2str()); | void |
| strcat(buf, "\n"); | gdtr_dump(UINT32 base, UINT limit) |
| #ifdef USE_FPU | { |
| strcat(buf, fpu_reg2str()); | UINT32 v[2]; |
| strcat(buf, "\n"); | UINT i; |
| #endif | |
| strcat(buf, mem2str(I286_CS, I286_IP)); | VERBOSE(("GDTR_DUMP: GDTR_BASE = 0x%08x, GDTR_LIMIT = 0x%04x", base, limit)); |
| strcat(buf, "\n"); | |
| sprintf(tmp, "no=%08x\n", no); | |
| strcat(buf, tmp); | |
| msgbox_str(buf); | for (i = 0; i < limit; i += 8) { |
| v[0] = cpu_kmemoryread_d(base + i); | |
| v[1] = cpu_kmemoryread_d(base + i + 4); | |
| VERBOSE(("GDTR_DUMP: %08x: %08x%08x", base + i, v[0], v[1])); | |
| } | |
| } | } |
| void put_cpuinfo(void) | void |
| ldtr_dump(UINT32 base, UINT limit) | |
| { | { |
| char buf[2048]; | UINT32 v[2]; |
| UINT i; | |
| strcpy(buf, cpu_reg2str()); | VERBOSE(("LDTR_DUMP: LDTR_BASE = 0x%08x, LDTR_LIMIT = 0x%04x", base, limit)); |
| strcat(buf, "\n"); | |
| #ifdef USE_FPU | for (i = 0; i < limit; i += 8) { |
| strcat(buf, fpu_reg2str()); | v[0] = cpu_kmemoryread_d(base + i); |
| strcat(buf, "\n"); | v[1] = cpu_kmemoryread_d(base + i + 4); |
| #endif | VERBOSE(("LDTR_DUMP: %08x: %08x%08x", base + i, v[0], v[1])); |
| strcat(buf, a20str()); | } |
| } | |
| void | |
| idtr_dump(UINT32 base, UINT limit) | |
| { | |
| UINT32 v[2]; | |
| UINT i; | |
| VERBOSE(("IDTR_DUMP: IDTR_BASE = 0x%08x, IDTR_LIMIT = 0x%04x", base, limit)); | |
| for (i = 0; i < limit; i += 8) { | |
| v[0] = cpu_kmemoryread_d(base + i); | |
| v[1] = cpu_kmemoryread_d(base + i + 4); | |
| VERBOSE(("IDTR_DUMP: %08x: %08x%08x", base + i, v[0], v[1])); | |
| } | |
| } | |
| void | |
| tr_dump(UINT16 selector, UINT32 base, UINT limit) | |
| { | |
| UINT32 v; | |
| UINT i; | |
| (void)selector; | |
| VERBOSE(("TR_DUMP: selector = %04x", selector)); | |
| for (i = 0; i < limit; i += 4) { | |
| v = cpu_kmemoryread_d(base + i); | |
| VERBOSE(("TR_DUMP: %08x: %08x", base + i, v)); | |
| } | |
| } | |
| UINT32 | |
| pde_dump(UINT32 base, int idx) | |
| { | |
| UINT32 paddr; | |
| UINT32 v; | |
| int i; | |
| if (idx < 0 && idx > -8192) { | |
| idx = -idx; | |
| VERBOSE(("PDE_DUMP: address = 0x%08x, num = %d", base, idx)); | |
| for (i = 0; i < idx; i++) { | |
| paddr = (base & CPU_CR3_PD_MASK) | (idx << 2); | |
| v = cpu_memoryread_d(paddr); | |
| VERBOSE(("PDE_DUMP: 0x%08x: %08x", paddr, v)); | |
| } | |
| paddr = 0; | |
| } else if (idx < 8192) { | |
| VERBOSE(("PDE_DUMP: address = 0x%08x", base)); | |
| paddr = (base & CPU_CR3_PD_MASK) | (idx << 2); | |
| v = cpu_memoryread_d(paddr); | |
| VERBOSE(("PDE_DUMP: 0x%08x: %08x", paddr, v)); | |
| } else { | |
| paddr = 0; | |
| } | |
| return paddr; | |
| } | |
| UINT32 | |
| convert_laddr_to_paddr(UINT32 laddr) | |
| { | |
| UINT32 paddr; /* physical address */ | |
| UINT32 pde_addr; /* page directory entry address */ | |
| UINT32 pde; /* page directory entry */ | |
| UINT32 pte_addr; /* page table entry address */ | |
| UINT32 pte; /* page table entry */ | |
| pde_addr = (CPU_CR3 & CPU_CR3_PD_MASK) | ((laddr >> 20) & 0xffc); | |
| pde = cpu_memoryread_d(pde_addr); | |
| if ((CPU_CR4 & CPU_CR4_PSE) && (pde & CPU_PDE_PAGE_SIZE)) { | |
| /* 4MB page size */ | |
| paddr = (pde & CPU_PDE_4M_BASEADDR_MASK) | (laddr & 0x003fffff); | |
| } else { | |
| /* 4KB page size */ | |
| pte_addr = (pde & CPU_PDE_BASEADDR_MASK) | ((laddr >> 10) & 0xffc); | |
| pte = cpu_memoryread_d(pte_addr); | |
| paddr = (pte & CPU_PTE_BASEADDR_MASK) | (laddr & 0x00000fff); | |
| } | |
| return paddr; | |
| } | |
| UINT32 | |
| convert_vaddr_to_paddr(unsigned int idx, UINT32 offset) | |
| { | |
| descriptor_t *sdp; | |
| UINT32 laddr; | |
| printf(buf); | if (idx < CPU_SEGREG_NUM) { |
| sdp = &CPU_STAT_SREG(idx); | |
| if (sdp->valid) { | |
| laddr = CPU_STAT_SREGBASE(idx) + offset; | |
| return convert_laddr_to_paddr(laddr); | |
| } | |
| } | |
| return 0; | |
| } | } |