--- np2/i386c/ia32/cpu_mem.c 2004/03/05 14:17:35 1.12 +++ np2/i386c/ia32/cpu_mem.c 2004/03/12 13:34:08 1.14 @@ -1,4 +1,4 @@ -/* $Id: cpu_mem.c,v 1.12 2004/03/05 14:17:35 monaka Exp $ */ +/* $Id: cpu_mem.c,v 1.14 2004/03/12 13:34:08 monaka Exp $ */ /* * Copyright (c) 2002-2003 NONAKA Kimihiro @@ -154,19 +154,22 @@ cpu_memorywrite_check(descriptor_t *sd, sd->flag |= CPU_DESC_FLAG_WRITABLE; } -BOOL -cpu_stack_push_check(descriptor_t *sd, UINT32 esp, UINT length) +void +cpu_stack_push_check(UINT16 s, descriptor_t *sd, UINT32 esp, UINT length) { UINT32 limit; if (CPU_STAT_PM) { - if (!sd->valid || !sd->p) - return FALSE; - if (!sd->s || sd->u.seg.c || !sd->u.seg.wr) - return FALSE; + if (!sd->valid || !sd->p) { + VERBOSE(("cpu_stack_push_check: valid = %d, present = %d", sd->valid, sd->p)); + EXCEPTION(SS_EXCEPTION, s & 0xfffc); + } + if (!sd->s || sd->u.seg.c || !sd->u.seg.wr) { + VERBOSE(("cpu_stack_push_check: s = %d, c = %d, wr", sd->s, sd->u.seg.c, sd->u.seg.wr)); + EXCEPTION(SS_EXCEPTION, s & 0xfffc); + } if (!sd->d) { - esp &= 0xffff; limit = 0xffff; } else { limit = 0xffffffff; @@ -176,37 +179,51 @@ cpu_stack_push_check(descriptor_t *sd, U if ((esp == 0) || (esp < length) || (esp - length <= sd->u.seg.limit) - || (esp > limit)) - return FALSE; + || (esp > limit)) { + VERBOSE(("cpu_stack_push_check: expand-down, esp = %08x, length = %08x", esp, length)); + VERBOSE(("cpu_stack_push_check: limit = %08x, seglimit = %08x", limit, sd->u.seg.limit)); + VERBOSE(("cpu_stack_push_check: segbase = %08x, segend = %08x", sd->u.seg.segbase, sd->u.seg.segend)); + EXCEPTION(SS_EXCEPTION, s & 0xfffc); + } } else { /* expand-up stack */ if (esp == 0) { if ((sd->d && (sd->u.seg.segend != 0xffffffff)) - || (!sd->d && (sd->u.seg.segend != 0xffff))) - return FALSE; + || (!sd->d && (sd->u.seg.segend != 0xffff))) { + VERBOSE(("cpu_stack_push_check: expand-up, esp = %08x, length = %08x", esp, length)); + VERBOSE(("cpu_stack_push_check: limit = %08x, seglimit = %08x", limit, sd->u.seg.limit)); + VERBOSE(("cpu_stack_push_check: segbase = %08x, segend = %08x", sd->u.seg.segbase, sd->u.seg.segend)); + EXCEPTION(SS_EXCEPTION, s & 0xfffc); + } } else { if ((esp < length) - || (esp - 1 > sd->u.seg.limit)) - return FALSE; + || (esp - 1 > sd->u.seg.limit)) { + VERBOSE(("cpu_stack_push_check: expand-up, esp = %08x, length = %08x", esp, length)); + VERBOSE(("cpu_stack_push_check: limit = %08x, seglimit = %08x", limit, sd->u.seg.limit)); + VERBOSE(("cpu_stack_push_check: segbase = %08x, segend = %08x", sd->u.seg.segbase, sd->u.seg.segend)); + EXCEPTION(SS_EXCEPTION, s & 0xfffc); + } } } } - return TRUE; } -BOOL -cpu_stack_pop_check(descriptor_t *sd, UINT32 esp, UINT length) +void +cpu_stack_pop_check(UINT16 s, descriptor_t *sd, UINT32 esp, UINT length) { UINT32 limit; if (CPU_STAT_PM) { - if (!sd->valid || !sd->p) - return FALSE; - if (!sd->s || sd->u.seg.c || !sd->u.seg.wr) - return FALSE; + if (!sd->valid || !sd->p) { + VERBOSE(("cpu_stack_pop_check: valid = %d, present = %d", sd->valid, sd->p)); + EXCEPTION(SS_EXCEPTION, s & 0xfffc); + } + if (!sd->s || sd->u.seg.c || !sd->u.seg.wr) { + VERBOSE(("cpu_stack_pop_check: s = %d, c = %d, wr", sd->s, sd->u.seg.c, sd->u.seg.wr)); + EXCEPTION(SS_EXCEPTION, s & 0xfffc); + } if (!sd->d) { - esp &= 0xffff; limit = 0xffff; } else { limit = 0xffffffff; @@ -214,18 +231,25 @@ cpu_stack_pop_check(descriptor_t *sd, UI if (sd->u.seg.ec) { /* expand-down stack */ if ((esp == limit) - || ((limit - esp) + 1 < length)) - return FALSE; + || ((limit - esp) + 1 < length)) { + VERBOSE(("cpu_stack_pop_check: expand-up, esp = %08x, length = %08x", esp, length)); + VERBOSE(("cpu_stack_pop_check: limit = %08x, seglimit = %08x", limit, sd->u.seg.limit)); + VERBOSE(("cpu_stack_pop_check: segbase = %08x, segend = %08x", sd->u.seg.segbase, sd->u.seg.segend)); + EXCEPTION(SS_EXCEPTION, s & 0xfffc); + } } else { /* expand-up stack */ if ((esp == limit) || (sd->u.seg.segend == 0) || (esp > sd->u.seg.limit) - || ((sd->u.seg.limit - esp) + 1 < length)) - return FALSE; + || ((sd->u.seg.limit - esp) + 1 < length)) { + VERBOSE(("cpu_stack_pop_check: expand-up, esp = %08x, length = %08x", esp, length)); + VERBOSE(("cpu_stack_pop_check: limit = %08x, seglimit = %08x", limit, sd->u.seg.limit)); + VERBOSE(("cpu_stack_pop_check: segbase = %08x, segend = %08x", sd->u.seg.segbase, sd->u.seg.segend)); + EXCEPTION(SS_EXCEPTION, s & 0xfffc); + } } } - return TRUE; } @@ -242,7 +266,7 @@ cpu_prefetch(UINT32 address) UINT length = CPU_PREFETCH_QUEUE_LENGTH - offset; cpu_memory_access_la_region(address, length, CPU_PAGE_READ_CODE, CPU_STAT_USER_MODE, CPU_PREFETCHQ + offset); - CPU_PREFETCHQ_REMAIN = length; + CPU_PREFETCHQ_REMAIN = (SINT8)length; } INLINE static MEMCALL UINT8 @@ -276,7 +300,7 @@ cpu_prefetchq_3(UINT32 address) CPU_PREFETCHQ_REMAIN -= 3; p = CPU_PREFETCHQ + (address & CPU_PREFETCHQ_MASK); v = LOADINTELWORD(p); - v += (UINT32)*p << 16; + v += ((UINT32)p[2]) << 16; return v; } @@ -293,6 +317,28 @@ cpu_prefetchq_d(UINT32 address) } #endif /* IA32_SUPPORT_PREFETCH_QUEUE */ +#if defined(IA32_SUPPORT_DEBUG_REGISTER) +INLINE static void +check_memory_break_point(UINT32 address, UINT length, UINT rw) +{ + int i; + + if (CPU_STAT_BP && !(CPU_EFLAG & RF_FLAG)) { + for (i = 0; i < CPU_DEBUG_REG_INDEX_NUM; i++) { + if ((CPU_STAT_BP & (1 << i)) + && (CPU_DR7_GET_RW(i) & rw) + + && ((address <= CPU_DR(i) && address + length > CPU_DR(i)) + || (address > CPU_DR(i) && address < CPU_DR(i) + CPU_DR7_GET_LEN(i)))) { + CPU_STAT_BP_EVENT |= CPU_STAT_BP_EVENT_B(i); + } + } + } +} +#else +#define check_memory_break_point(address, length, rw) +#endif + /* * code fetch */ @@ -306,11 +352,11 @@ cpu_codefetch(UINT32 offset) if (offset <= sd->u.seg.limit) { addr = sd->u.seg.segbase + offset; #if defined(IA32_SUPPORT_PREFETCH_QUEUE) - if (CPU_PREFETCHQ_REMAIN == 0) { + if (CPU_PREFETCHQ_REMAIN <= 0) { cpu_prefetch(addr); } return cpu_prefetchq(addr); -#else /* IA32_SUPPORT_PREFETCH_QUEUE */ +#else /* !IA32_SUPPORT_PREFETCH_QUEUE */ if (!CPU_STAT_PM) return cpu_memoryread(addr); return cpu_lcmemoryread(addr); @@ -333,7 +379,7 @@ cpu_codefetch_w(UINT32 offset) if (offset <= sd->u.seg.limit - 1) { addr = sd->u.seg.segbase + offset; #if defined(IA32_SUPPORT_PREFETCH_QUEUE) - if (CPU_PREFETCHQ_REMAIN == 0) { + if (CPU_PREFETCHQ_REMAIN <= 0) { cpu_prefetch(addr); } if (CPU_PREFETCHQ_REMAIN >= 2) { @@ -343,9 +389,9 @@ cpu_codefetch_w(UINT32 offset) v = cpu_prefetchq(addr); addr++; cpu_prefetch(addr); - v |= cpu_prefetchq(addr) << 8; + v += (UINT16)cpu_prefetchq(addr) << 8; return v; -#else /* IA32_SUPPORT_PREFETCH_QUEUE */ +#else /* !IA32_SUPPORT_PREFETCH_QUEUE */ if (!CPU_STAT_PM) return cpu_memoryread_w(addr); return cpu_lcmemoryread_w(addr); @@ -368,7 +414,7 @@ cpu_codefetch_d(UINT32 offset) if (offset <= sd->u.seg.limit - 3) { addr = sd->u.seg.segbase + offset; #if defined(IA32_SUPPORT_PREFETCH_QUEUE) - if (CPU_PREFETCHQ_REMAIN == 0) { + if (CPU_PREFETCHQ_REMAIN <= 0) { cpu_prefetch(addr); } if (CPU_PREFETCHQ_REMAIN >= 4) { @@ -377,25 +423,28 @@ cpu_codefetch_d(UINT32 offset) switch (CPU_PREFETCHQ_REMAIN) { case 1: v = cpu_prefetchq(addr); - cpu_prefetch(addr + 1); - v += (UINT32)cpu_prefetchq_3(addr + 1) << 8; + addr++; + cpu_prefetch(addr); + v += (UINT32)cpu_prefetchq_3(addr) << 8; break; case 2: v = cpu_prefetchq_w(addr); - cpu_prefetch(addr + 2); - v += (UINT32)cpu_prefetchq_w(addr + 2) << 16; + addr += 2; + cpu_prefetch(addr); + v += (UINT32)cpu_prefetchq_w(addr) << 16; break; case 3: v = cpu_prefetchq_3(addr); - cpu_prefetch(addr + 3); - v += (UINT32)cpu_prefetchq(addr + 3) << 24; + addr += 3; + cpu_prefetch(addr); + v += (UINT32)cpu_prefetchq(addr) << 24; break; } return v; } -#else /* IA32_SUPPORT_PREFETCH_QUEUE */ +#else /* !IA32_SUPPORT_PREFETCH_QUEUE */ if (!CPU_STAT_PM) return cpu_memoryread_d(addr); return cpu_lcmemoryread_d(addr); @@ -430,31 +479,29 @@ cpu_vmemoryread(int idx, UINT32 offset) } else { switch (sd->type) { case 4: case 5: case 6: case 7: - if (offset <= sd->u.seg.limit) { - if (idx == CPU_SS_INDEX) - exc = SS_EXCEPTION; - else - exc = GP_EXCEPTION; - goto err; - } + if (offset <= sd->u.seg.limit) + goto range_failure; break; default: - if (offset > sd->u.seg.limit) { - if (idx == CPU_SS_INDEX) - exc = SS_EXCEPTION; - else - exc = GP_EXCEPTION; - goto err; - } + if (offset > sd->u.seg.limit) + goto range_failure; break; } } addr = sd->u.seg.segbase + offset; + check_memory_break_point(addr, 1, CPU_DR7_RW_RO); if (!CPU_STAT_PM) return cpu_memoryread(addr); return cpu_lmemoryread(addr, CPU_STAT_USER_MODE); +range_failure: + if (idx == CPU_SS_INDEX) { + exc = SS_EXCEPTION; + } else { + exc = GP_EXCEPTION; + } + VERBOSE(("cpu_vmemoryread: type = %d, offset = %08x, limit = %08x", sd->type, offset, sd->u.seg.limit)); err: EXCEPTION(exc, 0); return 0; /* compiler happy */ @@ -481,31 +528,29 @@ cpu_vmemoryread_w(int idx, UINT32 offset } else { switch (sd->type) { case 4: case 5: case 6: case 7: - if (offset - 1 <= sd->u.seg.limit) { - if (idx == CPU_SS_INDEX) - exc = SS_EXCEPTION; - else - exc = GP_EXCEPTION; - goto err; - } + if (offset - 1 <= sd->u.seg.limit) + goto range_failure; break; default: - if (offset > sd->u.seg.limit - 1) { - if (idx == CPU_SS_INDEX) - exc = SS_EXCEPTION; - else - exc = GP_EXCEPTION; - goto err; - } + if (offset > sd->u.seg.limit - 1) + goto range_failure; break; } } addr = sd->u.seg.segbase + offset; + check_memory_break_point(addr, 2, CPU_DR7_RW_RO); if (!CPU_STAT_PM) return cpu_memoryread_w(addr); return cpu_lmemoryread_w(addr, CPU_STAT_USER_MODE); +range_failure: + if (idx == CPU_SS_INDEX) { + exc = SS_EXCEPTION; + } else { + exc = GP_EXCEPTION; + } + VERBOSE(("cpu_vmemoryread_w: type = %d, offset = %08x, limit = %08x", sd->type, offset, sd->u.seg.limit)); err: EXCEPTION(exc, 0); return 0; /* compiler happy */ @@ -532,31 +577,29 @@ cpu_vmemoryread_d(int idx, UINT32 offset } else { switch (sd->type) { case 4: case 5: case 6: case 7: - if (offset - 3 <= sd->u.seg.limit) { - if (idx == CPU_SS_INDEX) - exc = SS_EXCEPTION; - else - exc = GP_EXCEPTION; - goto err; - } + if (offset - 3 <= sd->u.seg.limit) + goto range_failure; break; default: - if (offset > sd->u.seg.limit - 3) { - if (idx == CPU_SS_INDEX) - exc = SS_EXCEPTION; - else - exc = GP_EXCEPTION; - goto err; - } + if (offset > sd->u.seg.limit - 3) + goto range_failure; break; } } addr = sd->u.seg.segbase + offset; + check_memory_break_point(addr, 4, CPU_DR7_RW_RO); if (!CPU_STAT_PM) return cpu_memoryread_d(addr); return cpu_lmemoryread_d(addr, CPU_STAT_USER_MODE); +range_failure: + if (idx == CPU_SS_INDEX) { + exc = SS_EXCEPTION; + } else { + exc = GP_EXCEPTION; + } + VERBOSE(("cpu_vmemoryread_d: type = %d, offset = %08x, limit = %08x", sd->type, offset, sd->u.seg.limit)); err: EXCEPTION(exc, 0); return 0; /* compiler happy */ @@ -584,27 +627,18 @@ cpu_vmemorywrite(int idx, UINT32 offset, } else { switch (sd->type) { case 6: case 7: - if (offset <= sd->u.seg.limit) { - if (idx == CPU_SS_INDEX) - exc = SS_EXCEPTION; - else - exc = GP_EXCEPTION; - goto err; - } + if (offset <= sd->u.seg.limit) + goto range_failure; break; default: - if (offset > sd->u.seg.limit) { - if (idx == CPU_SS_INDEX) - exc = SS_EXCEPTION; - else - exc = GP_EXCEPTION; - goto err; - } + if (offset > sd->u.seg.limit) + goto range_failure; break; } } addr = sd->u.seg.segbase + offset; + check_memory_break_point(addr, 1, CPU_DR7_RW_RW); if (!CPU_STAT_PM) { /* real mode */ cpu_memorywrite(addr, val); @@ -614,6 +648,13 @@ cpu_vmemorywrite(int idx, UINT32 offset, } return; +range_failure: + if (idx == CPU_SS_INDEX) { + exc = SS_EXCEPTION; + } else { + exc = GP_EXCEPTION; + } + VERBOSE(("cpu_vmemorywrite: type = %d, offset = %08x, limit = %08x", sd->type, offset, sd->u.seg.limit)); err: EXCEPTION(exc, 0); } @@ -639,27 +680,18 @@ cpu_vmemorywrite_w(int idx, UINT32 offse } else { switch (sd->type) { case 6: case 7: - if (offset - 1 <= sd->u.seg.limit) { - if (idx == CPU_SS_INDEX) - exc = SS_EXCEPTION; - else - exc = GP_EXCEPTION; - goto err; - } + if (offset - 1 <= sd->u.seg.limit) + goto range_failure; break; default: - if (offset > sd->u.seg.limit - 1) { - if (idx == CPU_SS_INDEX) - exc = SS_EXCEPTION; - else - exc = GP_EXCEPTION; - goto err; - } + if (offset > sd->u.seg.limit - 1) + goto range_failure; break; } } addr = sd->u.seg.segbase + offset; + check_memory_break_point(addr, 2, CPU_DR7_RW_RW); if (!CPU_STAT_PM) { /* real mode */ cpu_memorywrite_w(addr, val); @@ -669,6 +701,13 @@ cpu_vmemorywrite_w(int idx, UINT32 offse } return; +range_failure: + if (idx == CPU_SS_INDEX) { + exc = SS_EXCEPTION; + } else { + exc = GP_EXCEPTION; + } + VERBOSE(("cpu_vmemorywrite_w: type = %d, offset = %08x, limit = %08x", sd->type, offset, sd->u.seg.limit)); err: EXCEPTION(exc, 0); } @@ -694,27 +733,18 @@ cpu_vmemorywrite_d(int idx, UINT32 offse } else { switch (sd->type) { case 6: case 7: - if (offset - 3 <= sd->u.seg.limit) { - if (idx == CPU_SS_INDEX) - exc = SS_EXCEPTION; - else - exc = GP_EXCEPTION; - goto err; - } + if (offset - 3 <= sd->u.seg.limit) + goto range_failure; break; default: - if (offset > sd->u.seg.limit - 3) { - if (idx == CPU_SS_INDEX) - exc = SS_EXCEPTION; - else - exc = GP_EXCEPTION; - goto err; - } + if (offset > sd->u.seg.limit - 3) + goto range_failure; break; } } addr = sd->u.seg.segbase + offset; + check_memory_break_point(addr, 4, CPU_DR7_RW_RW); if (!CPU_STAT_PM) { /* real mode */ cpu_memorywrite_d(addr, val); @@ -724,6 +754,13 @@ cpu_vmemorywrite_d(int idx, UINT32 offse } return; +range_failure: + if (idx == CPU_SS_INDEX) { + exc = SS_EXCEPTION; + } else { + exc = GP_EXCEPTION; + } + VERBOSE(("cpu_vmemorywrite_d: type = %d, offset = %08x, limit = %08x", sd->type, offset, sd->u.seg.limit)); err: EXCEPTION(exc, 0); }