--- np2/i386c/ia32/cpu_mem.c 2004/03/12 13:34:08 1.14 +++ np2/i386c/ia32/cpu_mem.c 2012/06/18 14:30:27 1.35 @@ -1,7 +1,5 @@ -/* $Id: cpu_mem.c,v 1.14 2004/03/12 13:34:08 monaka Exp $ */ - /* - * Copyright (c) 2002-2003 NONAKA Kimihiro + * Copyright (c) 2002-2004 NONAKA Kimihiro * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -12,8 +10,6 @@ * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES @@ -35,309 +31,338 @@ /* * memory access check */ -void -cpu_memoryread_check(descriptor_t *sd, UINT32 offset, UINT length, int e) +static int MEMCALL check_limit_upstairs(descriptor_t *sdp, UINT32 offset, UINT len, BOOL is32bit); +static void MEMCALL cpu_memoryread_check(descriptor_t *sdp, UINT32 offset, UINT len, int e); +static void MEMCALL cpu_memorywrite_check(descriptor_t *sdp, UINT32 offset, UINT len, int e); + +static int MEMCALL +check_limit_upstairs(descriptor_t *sdp, UINT32 offset, UINT len, BOOL is32bit) { - UINT32 uplimit; + UINT32 limit; + UINT32 end; - if (CPU_STAT_PM) { - /* invalid */ - if (!sd->valid) { - VERBOSE(("cpu_memoryread_check: invalid")); - EXCEPTION(GP_EXCEPTION, 0); - } + __ASSERT(sdp != NULL); + __ASSERT(len > 0); - /* not present */ - if (!sd->p) { - VERBOSE(("cpu_memoryread_check: not present")); - EXCEPTION(e, 0); + len--; + end = offset + len; + + if (SEG_IS_DATA(sdp) && SEG_IS_EXPANDDOWN_DATA(sdp)) { + /* expand-down data segment */ + limit = SEG_IS_32BIT(sdp) ? 0xffffffff : 0x0000ffff; + if (sdp->u.seg.limit == 0) { + /* + * 32bit 16bit + * +-------+ +-------+ FFFFFFFFh + * | | | | + * | | + [1] + 0000FFFFh + * | valid | | | + * | | +-------+ 0000FFFFh - len -1 + * | | | valid | + * +-------+ +-------+ 00000000h + */ + if (!SEG_IS_32BIT(sdp)) { + if ((len > limit) /* len check */ + || (end > limit)) { /* [1] */ + goto exc; + } + } else { + sdp->flag |= CPU_DESC_FLAG_WHOLEADR; + } + } else { + /* + * 32bit 16bit + * +-------+ +-------+ FFFFFFFFh + * | [2] | | | + * +-------+ +.......+ FFFFFFFFh - len - 1 + * | | | [2] | + * | | +.......+ 0000FFFFh + * | valid | | | + * | | +-------+ 0000FFFFh - len - 1 + * | | | valid | + * +-------+ +-------+ seg.limit + * | [1] | | [1] | + * +-------+ +-------+ 00000000h + */ + if ((len > limit - sdp->u.seg.limit) /* len check */ + || (end < offset) /* wrap check */ + || (offset < sdp->u.seg.limit) /* [1] */ + || (end > limit)) { /* [2] */ + goto exc; + } + } + } else { + /* expand-up data or code segment */ + if (sdp->u.seg.limit == 0xffffffff) { + /* + * 16/32bit + * +-------+ FFFFFFFFh + * | | + * | | + * | valid | + * | | + * | | + * +-------+ 00000000h + */ + sdp->flag |= CPU_DESC_FLAG_WHOLEADR; + } else { + /* + * 16/32bit + * +-------+ FFFFFFFFh + * | | + * | | + * | [1] | + * +.......+ seg.limit + * | | + * +-------+ seg.limit - len - 1 + * | valid | + * +-------+ 00000000h + */ + if ((len > sdp->u.seg.limit) /* len check */ + || (end < offset) /* wrap check */ + || (end > sdp->u.seg.limit + 1)) { /* [1] */ + goto exc; + } } } + return 1; /* Ok! */ + +exc: + VERBOSE(("check_limit_upstairs: check failure: offset = 0x%08x, len = %d", offset, len + 1)); +#if defined(DEBUG) + segdesc_dump(sdp); +#endif + return 0; +} + +static void MEMCALL +cpu_memoryread_check(descriptor_t *sdp, UINT32 offset, UINT len, int e) +{ + + __ASSERT(sdp != NULL); + __ASSERT(len > 0); + + if (!SEG_IS_VALID(sdp)) { + e = GP_EXCEPTION; + goto exc; + } + if (!SEG_IS_PRESENT(sdp) + || SEG_IS_SYSTEM(sdp) + || (SEG_IS_CODE(sdp) && !SEG_IS_READABLE_CODE(sdp))) { + goto exc; + } - switch (sd->type) { + switch (sdp->type) { case 0: case 1: /* ro */ case 2: case 3: /* rw */ - case 10: case 11: /* rx */ - case 14: case 15: /* rxc */ - if (offset > sd->u.seg.limit - length + 1) { - VERBOSE(("cpu_memoryread_check: offset(%08x) > sd->u.seg.limit(%08x) - length(%08x) + 1", offset, sd->u.seg.limit, length)); - EXCEPTION(e, 0); - } - if (length - 1 > sd->u.seg.limit) { - VERBOSE(("cpu_memoryread_check: length(%08x) - 1 > sd->u.seg.limit(%08x)", length, sd->u.seg.limit)); - EXCEPTION(e, 0); - } - break; - case 4: case 5: /* ro (expand down) */ case 6: case 7: /* rw (expand down) */ - uplimit = sd->d ? 0xffffffff : 0x0000ffff; - if (offset <= sd->u.seg.limit) { - VERBOSE(("cpu_memoryread_check: offset(%08x) <= sd->u.seg.limit(%08x)", offset, sd->u.seg.limit)); - EXCEPTION(e, 0); - } - if (offset > uplimit) { - VERBOSE(("cpu_memoryread_check: offset(%08x) > uplimit(%08x)", offset, uplimit)); - EXCEPTION(e, 0); - } - if (uplimit - offset < length - 1) { - VERBOSE(("cpu_memoryread_check: uplimit(%08x) - offset(%08x) < length(%08x) - 1", uplimit, offset, length)); - EXCEPTION(e, 0); - } + case 10: case 11: /* rx */ + case 14: case 15: /* rxc */ + if (!check_limit_upstairs(sdp, offset, len, SEG_IS_32BIT(sdp))) + goto exc; break; default: - VERBOSE(("cpu_memoryread_check: invalid type (type = %d)", sd->type)); - EXCEPTION(e, 0); - break; + goto exc; } - sd->flag |= CPU_DESC_FLAG_READABLE; + sdp->flag |= CPU_DESC_FLAG_READABLE; + return; + +exc: + VERBOSE(("cpu_memoryread_check: check failure: offset = 0x%08x, len = %d", offset, len)); +#if defined(DEBUG) + segdesc_dump(sdp); +#endif + EXCEPTION(e, 0); } -void -cpu_memorywrite_check(descriptor_t *sd, UINT32 offset, UINT length, int e) +static void MEMCALL +cpu_memorywrite_check(descriptor_t *sdp, UINT32 offset, UINT len, int e) { - UINT32 uplimit; - - if (CPU_STAT_PM) { - /* invalid */ - if (!sd->valid) { - VERBOSE(("cpu_memorywrite_check: invalid")); - EXCEPTION(GP_EXCEPTION, 0); - } - /* not present */ - if (!sd->p) { - VERBOSE(("cpu_memorywrite_check: not present")); - EXCEPTION(e, 0); - } + __ASSERT(sdp != NULL); + __ASSERT(len > 0); - if (!sd->s) { - VERBOSE(("cpu_memorywrite_check: system segment")); - EXCEPTION(e, 0); - } + if (!SEG_IS_VALID(sdp)) { + e = GP_EXCEPTION; + goto exc; + } + if (!SEG_IS_PRESENT(sdp) + || SEG_IS_SYSTEM(sdp) + || SEG_IS_CODE(sdp) + || (SEG_IS_DATA(sdp) && !SEG_IS_WRITABLE_DATA(sdp))) { + goto exc; } - switch (sd->type) { + switch (sdp->type) { case 2: case 3: /* rw */ - if (offset > sd->u.seg.limit - length + 1) { - VERBOSE(("cpu_memorywrite_check: offset(%08x) > sd->u.seg.limit(%08x) - length(%08x) + 1", offset, sd->u.seg.limit, length)); - EXCEPTION(e, 0); - } - if (length - 1 > sd->u.seg.limit) { - VERBOSE(("cpu_memorywrite_check: length(%08x) - 1 > sd->u.seg.limit(%08x)", length, sd->u.seg.limit)); - EXCEPTION(e, 0); - } - break; - case 6: case 7: /* rw (expand down) */ - uplimit = sd->d ? 0xffffffff : 0x0000ffff; - if (offset <= sd->u.seg.limit) { - VERBOSE(("cpu_memorywrite_check: offset(%08x) <= sd->u.seg.limit(%08x)", offset, sd->u.seg.limit)); - EXCEPTION(e, 0); - } - if (offset > uplimit) { - VERBOSE(("cpu_memorywrite_check: offset(%08x) > uplimit(%08x)", offset, uplimit)); - EXCEPTION(e, 0); - } - if (uplimit - offset < length - 1) { - VERBOSE(("cpu_memorywrite_check: uplimit(%08x) - offset(%08x) < length(%08x) - 1", uplimit, offset, length)); - EXCEPTION(e, 0); - } + if (!check_limit_upstairs(sdp, offset, len, SEG_IS_32BIT(sdp))) + goto exc; break; default: - VERBOSE(("cpu_memorywrite_check: invalid type (type = %d)", sd->type)); - EXCEPTION(e, 0); - break; + goto exc; } - sd->flag |= CPU_DESC_FLAG_WRITABLE; + sdp->flag |= CPU_DESC_FLAG_WRITABLE | CPU_DESC_FLAG_READABLE; + return; + +exc: + VERBOSE(("cpu_memorywrite_check: check failure: offset = 0x%08x, len = %d", offset, len)); +#if defined(DEBUG) + segdesc_dump(sdp); +#endif + EXCEPTION(e, 0); } -void -cpu_stack_push_check(UINT16 s, descriptor_t *sd, UINT32 esp, UINT length) +void MEMCALL +cpu_stack_push_check(UINT16 s, descriptor_t *sdp, UINT32 sp, UINT len, + BOOL is32bit) { UINT32 limit; + UINT32 start; - if (CPU_STAT_PM) { - 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); - } + __ASSERT(sdp != NULL); + __ASSERT(len > 0); - if (!sd->d) { - limit = 0xffff; - } else { - limit = 0xffffffff; - } - if (sd->u.seg.ec) { - /* expand-down stack */ - if ((esp == 0) - || (esp < length) - || (esp - length <= sd->u.seg.limit) - || (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))) { - 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)) { - 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); - } - } - } + len--; + + if (!SEG_IS_VALID(sdp) + || !SEG_IS_PRESENT(sdp) + || SEG_IS_SYSTEM(sdp) + || SEG_IS_CODE(sdp) + || !SEG_IS_WRITABLE_DATA(sdp)) { + goto exc; } -} -void -cpu_stack_pop_check(UINT16 s, descriptor_t *sd, UINT32 esp, UINT length) -{ - UINT32 limit; + start = sp - len; + limit = is32bit ? 0xffffffff : 0x0000ffff; - if (CPU_STAT_PM) { - 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 (SEG_IS_EXPANDDOWN_DATA(sdp)) { + /* expand-down stack */ + if (!SEG_IS_32BIT(sdp)) { + if (sp > limit) { /* [*] */ + goto exc; + } } - - if (!sd->d) { - limit = 0xffff; + if (sdp->u.seg.limit == 0) { + /* + * 32bit 16bit + * +-------+ +-------+ FFFFFFFFh + * | | | [*] | + * | | +-------+ 0000FFFFh + * | valid | | | + * | | | valid | + * | | | | + * +-------+ +-------+ 00000000h + */ + if (!SEG_IS_32BIT(sdp)) { + if (sp > limit) { /* [1] */ + goto exc; + } + } else { + sdp->flag |= CPU_DESC_FLAG_WHOLEADR; + } } else { - limit = 0xffffffff; + /* + * 32bit 16bit + * +-------+ +-------+ FFFFFFFFh + * | | | [*] | + * | valid | +-------+ 0000FFFFh + * | | | valid | + * +-------+ +-------+ seg.limit + len - 1 + * | | | | + * +..[1]..+ +..[1]..+ seg.limit + * | | | | + * +-------+ +-------+ 00000000h + */ + if ((len > limit - sdp->u.seg.limit) /* len check */ + || (start > sp) /* wrap check */ + || (start < sdp->u.seg.limit)) { /* [1] */ + goto exc; + } } - if (sd->u.seg.ec) { - /* expand-down stack */ - if ((esp == limit) - || ((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 (sdp->u.seg.limit == limit) { + /* + * 32bit 16bit + * +-------+ +-------+ FFFFFFFFh + * | | | [1] | + * | | +-------+ 0000FFFFh + * | valid | | | + * | | | valid | + * | | | | + * +-------+ +-------+ 00000000h + */ + if (!SEG_IS_32BIT(sdp)) { + if (sp > limit) { /* [1] */ + goto exc; + } + } else { + sdp->flag |= CPU_DESC_FLAG_WHOLEADR; } } else { - /* expand-up stack */ - if ((esp == limit) - || (sd->u.seg.segend == 0) - || (esp > sd->u.seg.limit) - || ((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); + /* + * 32bit 16bit + * +-------+ +-------+ FFFFFFFFh + * | | | | + * | [1] | + [1] + 0000FFFFh + * | | | | + * +-------+ +-------+ seg.limit + * | valid | | valid | + * +.......+ +.......+ len - 1 + * | [+] | | [+] | + * +-------+ +-------+ 00000000h + * + * [+]: wrap check + */ + if ((len > sdp->u.seg.limit) /* len check */ + || (start > sp) /* wrap check */ + || (sp > sdp->u.seg.limit + 1)) { /* [1] */ + goto exc; } } } -} - - -#if defined(IA32_SUPPORT_PREFETCH_QUEUE) -/* - * code prefetch - */ -#define CPU_PREFETCHQ_MASK (CPU_PREFETCH_QUEUE_LENGTH - 1) - -INLINE static MEMCALL void -cpu_prefetch(UINT32 address) -{ - UINT offset = address & CPU_PREFETCHQ_MASK; - 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 = (SINT8)length; -} - -INLINE static MEMCALL UINT8 -cpu_prefetchq(UINT32 address) -{ - UINT8 v; + return; - CPU_PREFETCHQ_REMAIN--; - v = CPU_PREFETCHQ[address & CPU_PREFETCHQ_MASK]; - return v; +exc: + VERBOSE(("cpu_stack_push_check: check failure: selector = 0x%04x, sp = 0x%08x, len = %d", s, sp, len)); +#if defined(DEBUG) + segdesc_dump(sdp); +#endif + EXCEPTION(SS_EXCEPTION, s & 0xfffc); } -INLINE static MEMCALL UINT16 -cpu_prefetchq_w(UINT32 address) +void MEMCALL +cpu_stack_pop_check(UINT16 s, descriptor_t *sdp, UINT32 sp, UINT len, + BOOL is32bit) { - BYTE *p; - UINT16 v; - CPU_PREFETCHQ_REMAIN -= 2; - p = CPU_PREFETCHQ + (address & CPU_PREFETCHQ_MASK); - v = LOADINTELWORD(p); - return v; -} + __ASSERT(sdp != NULL); + __ASSERT(len > 0); -INLINE static MEMCALL UINT32 -cpu_prefetchq_3(UINT32 address) -{ - BYTE *p; - UINT32 v; - - CPU_PREFETCHQ_REMAIN -= 3; - p = CPU_PREFETCHQ + (address & CPU_PREFETCHQ_MASK); - v = LOADINTELWORD(p); - v += ((UINT32)p[2]) << 16; - return v; -} + if (!SEG_IS_VALID(sdp) + || !SEG_IS_PRESENT(sdp) + || SEG_IS_SYSTEM(sdp) + || SEG_IS_CODE(sdp) + || !SEG_IS_WRITABLE_DATA(sdp)) { + goto exc; + } -INLINE static MEMCALL UINT32 -cpu_prefetchq_d(UINT32 address) -{ - BYTE *p; - UINT32 v; + if (!check_limit_upstairs(sdp, sp, len, is32bit)) + goto exc; + return; - CPU_PREFETCHQ_REMAIN -= 4; - p = CPU_PREFETCHQ + (address & CPU_PREFETCHQ_MASK); - v = LOADINTELDWORD(p); - return v; +exc: + VERBOSE(("cpu_stack_pop_check: check failure: selector = 0x%04x, sp = 0x%08x, len = %d", s, sp, len)); +#if defined(DEBUG) + segdesc_dump(sdp); +#endif + EXCEPTION(SS_EXCEPTION, s & 0xfffc); } -#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 @@ -345,23 +370,18 @@ check_memory_break_point(UINT32 address, UINT8 MEMCALL cpu_codefetch(UINT32 offset) { - descriptor_t *sd; + const int ucrw = CPU_PAGE_READ_CODE | CPU_STAT_USER_MODE; + descriptor_t *sdp; UINT32 addr; - sd = &CPU_STAT_SREG(CPU_CS_INDEX); - if (offset <= sd->u.seg.limit) { - addr = sd->u.seg.segbase + offset; -#if defined(IA32_SUPPORT_PREFETCH_QUEUE) - if (CPU_PREFETCHQ_REMAIN <= 0) { - cpu_prefetch(addr); - } - return cpu_prefetchq(addr); -#else /* !IA32_SUPPORT_PREFETCH_QUEUE */ - if (!CPU_STAT_PM) - return cpu_memoryread(addr); - return cpu_lcmemoryread(addr); -#endif /* IA32_SUPPORT_PREFETCH_QUEUE */ - } + sdp = &CPU_CS_DESC; + addr = sdp->u.seg.segbase + offset; + + if (!CPU_STAT_PM) + return cpu_memoryread(addr); + if (offset <= sdp->u.seg.limit) + return cpu_lmemoryread(addr, ucrw); + EXCEPTION(GP_EXCEPTION, 0); return 0; /* compiler happy */ } @@ -369,34 +389,18 @@ cpu_codefetch(UINT32 offset) UINT16 MEMCALL cpu_codefetch_w(UINT32 offset) { - descriptor_t *sd; + const int ucrw = CPU_PAGE_READ_CODE | CPU_STAT_USER_MODE; + descriptor_t *sdp; UINT32 addr; -#if defined(IA32_SUPPORT_PREFETCH_QUEUE) - UINT16 v; -#endif - sd = &CPU_STAT_SREG(CPU_CS_INDEX); - if (offset <= sd->u.seg.limit - 1) { - addr = sd->u.seg.segbase + offset; -#if defined(IA32_SUPPORT_PREFETCH_QUEUE) - if (CPU_PREFETCHQ_REMAIN <= 0) { - cpu_prefetch(addr); - } - if (CPU_PREFETCHQ_REMAIN >= 2) { - return cpu_prefetchq_w(addr); - } + sdp = &CPU_CS_DESC; + addr = sdp->u.seg.segbase + offset; + + if (!CPU_STAT_PM) + return cpu_memoryread_w(addr); + if (offset <= sdp->u.seg.limit - 1) + return cpu_lmemoryread_w(addr, ucrw); - v = cpu_prefetchq(addr); - addr++; - cpu_prefetch(addr); - v += (UINT16)cpu_prefetchq(addr) << 8; - return v; -#else /* !IA32_SUPPORT_PREFETCH_QUEUE */ - if (!CPU_STAT_PM) - return cpu_memoryread_w(addr); - return cpu_lcmemoryread_w(addr); -#endif /* IA32_SUPPORT_PREFETCH_QUEUE */ - } EXCEPTION(GP_EXCEPTION, 0); return 0; /* compiler happy */ } @@ -404,363 +408,155 @@ cpu_codefetch_w(UINT32 offset) UINT32 MEMCALL cpu_codefetch_d(UINT32 offset) { - descriptor_t *sd; + const int ucrw = CPU_PAGE_READ_CODE | CPU_STAT_USER_MODE; + descriptor_t *sdp; UINT32 addr; -#if defined(IA32_SUPPORT_PREFETCH_QUEUE) - UINT32 v; -#endif - sd = &CPU_STAT_SREG(CPU_CS_INDEX); - if (offset <= sd->u.seg.limit - 3) { - addr = sd->u.seg.segbase + offset; -#if defined(IA32_SUPPORT_PREFETCH_QUEUE) - if (CPU_PREFETCHQ_REMAIN <= 0) { - cpu_prefetch(addr); - } - if (CPU_PREFETCHQ_REMAIN >= 4) { - return cpu_prefetchq_d(addr); - } else { - switch (CPU_PREFETCHQ_REMAIN) { - case 1: - v = cpu_prefetchq(addr); - addr++; - cpu_prefetch(addr); - v += (UINT32)cpu_prefetchq_3(addr) << 8; - break; - - case 2: - v = cpu_prefetchq_w(addr); - addr += 2; - cpu_prefetch(addr); - v += (UINT32)cpu_prefetchq_w(addr) << 16; - break; - - case 3: - v = cpu_prefetchq_3(addr); - addr += 3; - cpu_prefetch(addr); - v += (UINT32)cpu_prefetchq(addr) << 24; - break; - } - return v; - } -#else /* !IA32_SUPPORT_PREFETCH_QUEUE */ - if (!CPU_STAT_PM) - return cpu_memoryread_d(addr); - return cpu_lcmemoryread_d(addr); -#endif /* IA32_SUPPORT_PREFETCH_QUEUE */ - } + sdp = &CPU_CS_DESC; + addr = sdp->u.seg.segbase + offset; + + if (!CPU_STAT_PM) + return cpu_memoryread_d(addr); + + if (offset <= sdp->u.seg.limit - 3) + return cpu_lmemoryread_d(addr, ucrw); + EXCEPTION(GP_EXCEPTION, 0); return 0; /* compiler happy */ } - /* - * virtual address -> linear address + * additional physical address memory access functions */ -UINT8 MEMCALL -cpu_vmemoryread(int idx, UINT32 offset) +UINT64 MEMCALL +cpu_memoryread_q(UINT32 paddr) { - descriptor_t *sd; - UINT32 addr; - int exc; - - __ASSERT((unsigned int)idx < CPU_SEGREG_NUM); - - sd = &CPU_STAT_SREG(idx); - if (!sd->valid) { - exc = GP_EXCEPTION; - goto err; - } + UINT64 value; - if (!(sd->flag & CPU_DESC_FLAG_READABLE)) { - cpu_memoryread_check(sd, offset, 1, - (idx == CPU_SS_INDEX) ? SS_EXCEPTION : GP_EXCEPTION); - } else { - switch (sd->type) { - case 4: case 5: case 6: case 7: - if (offset <= sd->u.seg.limit) - goto range_failure; - break; - - default: - 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); + value = cpu_memoryread_d(paddr); + value += (UINT64)cpu_memoryread_d(paddr + 4) << 32; -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 */ + return value; } -UINT16 MEMCALL -cpu_vmemoryread_w(int idx, UINT32 offset) +void MEMCALL +cpu_memorywrite_q(UINT32 paddr, UINT64 value) { - descriptor_t *sd; - UINT32 addr; - int exc; - - __ASSERT((unsigned int)idx < CPU_SEGREG_NUM); - sd = &CPU_STAT_SREG(idx); - if (!sd->valid) { - exc = GP_EXCEPTION; - goto err; - } + cpu_memorywrite_d(paddr, (UINT32)value); + cpu_memorywrite_d(paddr + 4, (UINT32)(value >> 32)); +} - if (!(sd->flag & CPU_DESC_FLAG_READABLE)) { - cpu_memoryread_check(sd, offset, 2, - (idx == CPU_SS_INDEX) ? SS_EXCEPTION : GP_EXCEPTION); - } else { - switch (sd->type) { - case 4: case 5: case 6: case 7: - if (offset - 1 <= sd->u.seg.limit) - goto range_failure; - break; - - default: - 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); +REG80 MEMCALL +cpu_memoryread_f(UINT32 paddr) +{ + REG80 value; + int i; -range_failure: - if (idx == CPU_SS_INDEX) { - exc = SS_EXCEPTION; - } else { - exc = GP_EXCEPTION; + for (i = 0; i < (int)sizeof(REG80); ++i) { + value.b[i] = cpu_memoryread(paddr + i); } - 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 */ + return value; } -UINT32 MEMCALL -cpu_vmemoryread_d(int idx, UINT32 offset) +void MEMCALL +cpu_memorywrite_f(UINT32 paddr, const REG80 *value) { - descriptor_t *sd; - UINT32 addr; - int exc; - - __ASSERT((unsigned int)idx < CPU_SEGREG_NUM); + int i; - sd = &CPU_STAT_SREG(idx); - if (!sd->valid) { - exc = GP_EXCEPTION; - goto err; + for (i = 0; i < (int)sizeof(REG80); ++i) { + cpu_memorywrite(paddr + i, value->b[i]); } +} - if (!(sd->flag & CPU_DESC_FLAG_READABLE)) { - cpu_memoryread_check(sd, offset, 4, - (idx == CPU_SS_INDEX) ? SS_EXCEPTION : GP_EXCEPTION); - } else { - switch (sd->type) { - case 4: case 5: case 6: case 7: - if (offset - 3 <= sd->u.seg.limit) - goto range_failure; - break; - - default: - 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); +/* + * virtual address memory access functions + */ +#define CHOOSE_EXCEPTION(sreg) \ + (((sreg) == CPU_SS_INDEX) ? SS_EXCEPTION : GP_EXCEPTION) -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 */ -} +#include "cpu_mem.mcr" -/* vaddr memory write */ -void MEMCALL -cpu_vmemorywrite(int idx, UINT32 offset, UINT8 val) +DECLARE_VIRTUAL_ADDRESS_MEMORY_RW_FUNCTIONS(b, UINT8, 1) +DECLARE_VIRTUAL_ADDRESS_MEMORY_RMW_FUNCTIONS(b, UINT8, 1) +DECLARE_VIRTUAL_ADDRESS_MEMORY_RW_FUNCTIONS(w, UINT16, 2) +DECLARE_VIRTUAL_ADDRESS_MEMORY_RMW_FUNCTIONS(w, UINT16, 2) +DECLARE_VIRTUAL_ADDRESS_MEMORY_RW_FUNCTIONS(d, UINT32, 4) +DECLARE_VIRTUAL_ADDRESS_MEMORY_RMW_FUNCTIONS(d, UINT32, 4) +DECLARE_VIRTUAL_ADDRESS_MEMORY_RW_FUNCTIONS(q, UINT64, 8) + +REG80 MEMCALL +cpu_vmemoryread_f(int idx, UINT32 offset) { - descriptor_t *sd; + descriptor_t *sdp; UINT32 addr; int exc; __ASSERT((unsigned int)idx < CPU_SEGREG_NUM); - sd = &CPU_STAT_SREG(idx); - if (!sd->valid) { + sdp = &CPU_STAT_SREG(idx); + addr = sdp->u.seg.segbase + offset; + + if (!CPU_STAT_PM) + return cpu_memoryread_f(addr); + + if (!SEG_IS_VALID(sdp)) { exc = GP_EXCEPTION; goto err; } - - if (!(sd->flag & CPU_DESC_FLAG_WRITABLE)) { - cpu_memorywrite_check(sd, offset, 1, - (idx == CPU_SS_INDEX) ? SS_EXCEPTION : GP_EXCEPTION); - } else { - switch (sd->type) { - case 6: case 7: - if (offset <= sd->u.seg.limit) - goto range_failure; - break; - - default: - 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); - } else { - /* protected mode */ - cpu_lmemorywrite(addr, val, CPU_STAT_USER_MODE); - } - return; + if (!(sdp->flag & CPU_DESC_FLAG_READABLE)) { + cpu_memoryread_check(sdp, offset, 10, CHOOSE_EXCEPTION(idx)); + } else if (!(sdp->flag & CPU_DESC_FLAG_WHOLEADR)) { + if (!check_limit_upstairs(sdp, offset, 10, SEG_IS_32BIT(sdp))) + goto range_failure; + } + return cpu_lmemoryread_f(addr, CPU_PAGE_READ_DATA | CPU_STAT_USER_MODE); 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)); + VERBOSE(("cpu_vmemoryread_f: type = %d, offset = %08x, limit = %08x", sdp->type, offset, sdp->u.seg.limit)); + exc = CHOOSE_EXCEPTION(idx); err: EXCEPTION(exc, 0); + { + REG80 dummy; + memset(&dummy, 0, sizeof(dummy)); + return dummy; /* compiler happy */ + } } void MEMCALL -cpu_vmemorywrite_w(int idx, UINT32 offset, UINT16 val) +cpu_vmemorywrite_f(int idx, UINT32 offset, const REG80 *value) { - descriptor_t *sd; + descriptor_t *sdp; UINT32 addr; int exc; __ASSERT((unsigned int)idx < CPU_SEGREG_NUM); - sd = &CPU_STAT_SREG(idx); - if (!sd->valid) { - exc = GP_EXCEPTION; - goto err; - } + sdp = &CPU_STAT_SREG(idx); + addr = sdp->u.seg.segbase + offset; - if (!(sd->flag & CPU_DESC_FLAG_WRITABLE)) { - cpu_memorywrite_check(sd, offset, 2, - (idx == CPU_SS_INDEX) ? SS_EXCEPTION : GP_EXCEPTION); - } else { - switch (sd->type) { - case 6: case 7: - if (offset - 1 <= sd->u.seg.limit) - goto range_failure; - break; - - default: - 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); - } else { - /* protected mode */ - cpu_lmemorywrite_w(addr, val, CPU_STAT_USER_MODE); + cpu_memorywrite_f(addr, value); + return; } - 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); -} - -void MEMCALL -cpu_vmemorywrite_d(int idx, UINT32 offset, UINT32 val) -{ - descriptor_t *sd; - UINT32 addr; - int exc; - - __ASSERT((unsigned int)idx < CPU_SEGREG_NUM); - - sd = &CPU_STAT_SREG(idx); - if (!sd->valid) { + if (!SEG_IS_VALID(sdp)) { exc = GP_EXCEPTION; goto err; } - - if (!(sd->flag & CPU_DESC_FLAG_WRITABLE)) { - cpu_memorywrite_check(sd, offset, 4, - (idx == CPU_SS_INDEX) ? SS_EXCEPTION : GP_EXCEPTION); - } else { - switch (sd->type) { - case 6: case 7: - if (offset - 3 <= sd->u.seg.limit) - goto range_failure; - break; - - default: - 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); - } else { - /* protected mode */ - cpu_lmemorywrite_d(addr, val, CPU_STAT_USER_MODE); + if (!(sdp->flag & CPU_DESC_FLAG_WRITABLE)) { + cpu_memorywrite_check(sdp, offset, 10, CHOOSE_EXCEPTION(idx)); + } else if (!(sdp->flag & CPU_DESC_FLAG_WHOLEADR)) { + if (!check_limit_upstairs(sdp, offset, 10, SEG_IS_32BIT(sdp))) + goto range_failure; } + cpu_lmemorywrite_f(addr, value, CPU_PAGE_WRITE_DATA | CPU_STAT_USER_MODE); 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)); + VERBOSE(("cpu_vmemorywrite_f: type = %d, offset = %08x, limit = %08x", sdp->type, offset, sdp->u.seg.limit)); + exc = CHOOSE_EXCEPTION(idx); err: EXCEPTION(exc, 0); }