|
|
| version 1.9, 2004/02/04 13:24:35 | version 1.17, 2004/03/25 15:08:32 |
|---|---|
| Line 1 | Line 1 |
| /* $Id$ */ | /* $Id$ */ |
| /* | /* |
| * Copyright (c) 2002-2003 NONAKA Kimihiro | * Copyright (c) 2002-2004 NONAKA Kimihiro |
| * All rights reserved. | * All rights reserved. |
| * | * |
| * Redistribution and use in source and binary forms, with or without | * Redistribution and use in source and binary forms, with or without |
| Line 31 | Line 31 |
| #include "cpu.h" | #include "cpu.h" |
| #include "memory.h" | #include "memory.h" |
| #define cpumem i386core.e.ext | |
| #define extmem_size i386core.e.extsize | |
| /* | |
| * initialize 1MB-16MB memory | |
| */ | |
| int | |
| init_cpumem(UINT8 usemem) | |
| { | |
| UINT32 size; | |
| size = usemem << 20; | |
| if (size >= (LOWMEM - 0x100000)) { | |
| size -= (LOWMEM - 0x100000); | |
| } | |
| else { | |
| size = 0; | |
| } | |
| if (extmem_size != size) { | |
| if (cpumem) { | |
| free(cpumem); | |
| cpumem = 0; | |
| } | |
| if (size) { | |
| cpumem = (BYTE *)malloc(size); | |
| if (cpumem == NULL) { | |
| size = 0; | |
| } | |
| } | |
| extmem_size = size; | |
| } | |
| return SUCCESS; | |
| } | |
| /* | /* |
| * memory access check | * memory access check |
| */ | */ |
| void | void |
| cpu_memoryread_check(descriptor_t* sd, DWORD offset, DWORD length, int e) | cpu_memoryread_check(descriptor_t *sd, UINT32 offset, UINT length, int e) |
| { | { |
| DWORD uplimit; | UINT32 uplimit; |
| if (CPU_STAT_PM) { | if (CPU_STAT_PM) { |
| /* invalid */ | /* invalid */ |
| Line 87 cpu_memoryread_check(descriptor_t* sd, D | Line 52 cpu_memoryread_check(descriptor_t* sd, D |
| VERBOSE(("cpu_memoryread_check: not present")); | VERBOSE(("cpu_memoryread_check: not present")); |
| EXCEPTION(e, 0); | EXCEPTION(e, 0); |
| } | } |
| } | |
| switch (sd->type) { | switch (sd->type) { |
| case 0: case 1: /* ro */ | case 0: case 1: /* ro */ |
| case 2: case 3: /* rw */ | case 2: case 3: /* rw */ |
| case 10: case 11: /* rx */ | case 10: case 11: /* rx */ |
| case 14: case 15: /* rxc */ | case 14: case 15: /* rxc */ |
| if (offset > sd->u.seg.limit - length + 1) { | 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)); | VERBOSE(("cpu_memoryread_check: offset(%08x) > sd->u.seg.limit(%08x) - length(%08x) + 1", offset, sd->u.seg.limit, length)); |
| EXCEPTION(e, 0); | EXCEPTION(e, 0); |
| } | } |
| if (length - 1 > sd->u.seg.limit) { | if (length - 1 > sd->u.seg.limit) { |
| VERBOSE(("cpu_memoryread_check: length(%08x) - 1 > sd->u.seg.limit(%08x)", length, sd->u.seg.limit)); | VERBOSE(("cpu_memoryread_check: length(%08x) - 1 > sd->u.seg.limit(%08x)", length, sd->u.seg.limit)); |
| EXCEPTION(e, 0); | EXCEPTION(e, 0); |
| } | } |
| break; | 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); | |
| } | |
| break; | |
| default: | case 4: case 5: /* ro (expand down) */ |
| VERBOSE(("cpu_memoryread_check: invalid type (type = %d)", sd->type)); | 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); | EXCEPTION(e, 0); |
| break; | |
| } | } |
| 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); | |
| } | |
| break; | |
| default: | |
| VERBOSE(("cpu_memoryread_check: invalid type (type = %d)", sd->type)); | |
| EXCEPTION(e, 0); | |
| break; | |
| } | } |
| sd->flag |= CPU_DESC_FLAG_READABLE; | sd->flag |= CPU_DESC_FLAG_READABLE; |
| } | } |
| void | void |
| cpu_memorywrite_check(descriptor_t* sd, DWORD offset, DWORD length, int e) | cpu_memorywrite_check(descriptor_t *sd, UINT32 offset, UINT length, int e) |
| { | { |
| DWORD uplimit; | UINT32 uplimit; |
| if (CPU_STAT_PM) { | if (CPU_STAT_PM) { |
| /* invalid */ | /* invalid */ |
| Line 151 cpu_memorywrite_check(descriptor_t* sd, | Line 116 cpu_memorywrite_check(descriptor_t* sd, |
| VERBOSE(("cpu_memorywrite_check: system segment")); | VERBOSE(("cpu_memorywrite_check: system segment")); |
| EXCEPTION(e, 0); | EXCEPTION(e, 0); |
| } | } |
| } | |
| switch (sd->type) { | switch (sd->type) { |
| case 2: case 3: /* rw */ | case 2: case 3: /* rw */ |
| if (offset > sd->u.seg.limit - length + 1) { | 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)); | VERBOSE(("cpu_memorywrite_check: offset(%08x) > sd->u.seg.limit(%08x) - length(%08x) + 1", offset, sd->u.seg.limit, length)); |
| EXCEPTION(e, 0); | EXCEPTION(e, 0); |
| } | } |
| if (length - 1 > sd->u.seg.limit) { | if (length - 1 > sd->u.seg.limit) { |
| VERBOSE(("cpu_memorywrite_check: length(%08x) - 1 > sd->u.seg.limit(%08x)", length, sd->u.seg.limit)); | VERBOSE(("cpu_memorywrite_check: length(%08x) - 1 > sd->u.seg.limit(%08x)", length, sd->u.seg.limit)); |
| EXCEPTION(e, 0); | EXCEPTION(e, 0); |
| } | } |
| break; | 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); | |
| } | |
| break; | |
| default: | case 6: case 7: /* rw (expand down) */ |
| VERBOSE(("cpu_memorywrite_check: invalid type (type = %d)", sd->type)); | 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); | EXCEPTION(e, 0); |
| break; | |
| } | } |
| 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); | |
| } | |
| break; | |
| default: | |
| VERBOSE(("cpu_memorywrite_check: invalid type (type = %d)", sd->type)); | |
| EXCEPTION(e, 0); | |
| break; | |
| } | } |
| sd->flag |= CPU_DESC_FLAG_WRITABLE; | sd->flag |= CPU_DESC_FLAG_WRITABLE; |
| } | } |
| BOOL | void |
| cpu_stack_push_check(descriptor_t* sdp, DWORD esp, DWORD length) | cpu_stack_push_check(UINT16 s, descriptor_t *sd, UINT32 esp, UINT length) |
| { | { |
| DWORD limit; | UINT32 limit; |
| if (!CPU_STAT_PM) | if (CPU_STAT_PM) { |
| return TRUE; | 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 (!sdp->valid || !sdp->p) | if (!sd->d) { |
| return FALSE; | limit = 0xffff; |
| if (!sdp->s || sdp->u.seg.c || !sdp->u.seg.wr) | |
| return FALSE; | |
| if (!sdp->d) { | |
| esp &= 0xffff; | |
| limit = 0xffff; | |
| } else { | |
| limit = 0xffffffff; | |
| } | |
| if (sdp->u.seg.ec) { | |
| /* expand-down stack */ | |
| if ((esp == 0) | |
| || (esp < length) | |
| || (esp - length <= sdp->u.seg.limit) | |
| || (esp > limit)) | |
| return FALSE; | |
| } else { | |
| /* expand-up stack */ | |
| if (esp == 0) { | |
| if ((sdp->d && (sdp->u.seg.segend != 0xffffffff)) | |
| || (!sdp->d && (sdp->u.seg.segend != 0xffff))) | |
| return FALSE; | |
| } else { | } else { |
| if ((esp < length) | limit = 0xffffffff; |
| || (esp - 1 > sdp->u.seg.limit)) | } |
| return FALSE; | 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); | |
| } | |
| } | |
| } | } |
| } | } |
| return TRUE; | |
| } | } |
| BOOL | void |
| cpu_stack_pop_check(descriptor_t* sdp, DWORD esp, DWORD length) | cpu_stack_pop_check(UINT16 s, descriptor_t *sd, UINT32 esp, UINT length) |
| { | { |
| DWORD limit; | UINT32 limit; |
| if (!CPU_STAT_PM) | if (CPU_STAT_PM) { |
| return TRUE; | if (!sd->valid || !sd->p) { |
| VERBOSE(("cpu_stack_pop_check: valid = %d, present = %d", sd->valid, sd->p)); | |
| if (!sdp->valid || !sdp->p) | EXCEPTION(SS_EXCEPTION, s & 0xfffc); |
| return FALSE; | } |
| if (!sdp->s || sdp->u.seg.c || !sdp->u.seg.wr) | if (!sd->s || sd->u.seg.c || !sd->u.seg.wr) { |
| return FALSE; | 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 (!sdp->d) { | if (!sd->d) { |
| esp &= 0xffff; | limit = 0xffff; |
| limit = 0xffff; | } else { |
| } else { | limit = 0xffffffff; |
| limit = 0xffffffff; | } |
| } | if (sd->u.seg.ec) { |
| if (sdp->u.seg.ec) { | /* expand-down stack */ |
| /* expand-down stack */ | if ((esp == limit) |
| if ((esp == limit) | || ((limit - esp) + 1 < length)) { |
| || ((limit - esp) + 1 < length)) | VERBOSE(("cpu_stack_pop_check: expand-up, esp = %08x, length = %08x", esp, length)); |
| return FALSE; | VERBOSE(("cpu_stack_pop_check: limit = %08x, seglimit = %08x", limit, sd->u.seg.limit)); |
| } else { | VERBOSE(("cpu_stack_pop_check: segbase = %08x, segend = %08x", sd->u.seg.segbase, sd->u.seg.segend)); |
| /* expand-up stack */ | EXCEPTION(SS_EXCEPTION, s & 0xfffc); |
| if ((esp == limit) | } |
| || (sdp->u.seg.segend == 0) | } else { |
| || (esp > sdp->u.seg.limit) | /* expand-up stack */ |
| || ((sdp->u.seg.limit - esp) + 1 < length)) | if ((esp == limit) |
| return FALSE; | || (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); | |
| } | |
| } | |
| } | } |
| return TRUE; | |
| } | } |
| #undef OVERRUN_CHECK | #if defined(IA32_SUPPORT_PREFETCH_QUEUE) |
| #if defined(OVERRUN_CHECK) | |
| #define OVERRUN_EXCEPTION() EXCEPTION(GP_EXCEPTION, 0) | |
| #else | |
| #define OVERRUN_EXCEPTION() | |
| #endif | |
| /* | /* |
| * code fetch | * code prefetch |
| */ | */ |
| BYTE MEMCALL | #define CPU_PREFETCHQ_MASK (CPU_PREFETCH_QUEUE_LENGTH - 1) |
| cpu_codefetch(DWORD offset) | |
| INLINE static MEMCALL void | |
| cpu_prefetch(UINT32 address) | |
| { | { |
| descriptor_t *sd; | UINT offset = address & CPU_PREFETCHQ_MASK; |
| DWORD addr; | UINT length = CPU_PREFETCH_QUEUE_LENGTH - offset; |
| sd = &CPU_STAT_SREG(CPU_CS_INDEX); | cpu_memory_access_la_region(address, length, CPU_PAGE_READ_CODE|CPU_STAT_USER_MODE, CPU_PREFETCHQ + offset); |
| if (offset <= sd->u.seg.limit) { | CPU_PREFETCHQ_REMAIN = (SINT8)length; |
| addr = CPU_STAT_SREGBASE(CPU_CS_INDEX) + offset; | |
| if (!CPU_STAT_PM) | |
| return cpu_memoryread(addr); | |
| return cpu_lcmemoryread(addr); | |
| } | |
| EXCEPTION(GP_EXCEPTION, 0); | |
| return 0; /* compiler happy */ | |
| } | } |
| WORD MEMCALL | INLINE static MEMCALL UINT8 |
| cpu_codefetch_w(DWORD offset) | cpu_prefetchq(UINT32 address) |
| { | { |
| descriptor_t *sd; | UINT8 v; |
| DWORD addr; | |
| sd = &CPU_STAT_SREG(CPU_CS_INDEX); | CPU_PREFETCHQ_REMAIN--; |
| if (offset <= sd->u.seg.limit - 1) { | v = CPU_PREFETCHQ[address & CPU_PREFETCHQ_MASK]; |
| addr = CPU_STAT_SREGBASE(CPU_CS_INDEX) + offset; | return v; |
| if (!CPU_STAT_PM) | |
| return cpu_memoryread_w(addr); | |
| return cpu_lcmemoryread_w(addr); | |
| } | |
| EXCEPTION(GP_EXCEPTION, 0); | |
| return 0; /* compiler happy */ | |
| } | } |
| DWORD MEMCALL | INLINE static MEMCALL UINT16 |
| cpu_codefetch_d(DWORD offset) | cpu_prefetchq_w(UINT32 address) |
| { | { |
| descriptor_t *sd; | BYTE *p; |
| DWORD addr; | UINT16 v; |
| sd = &CPU_STAT_SREG(CPU_CS_INDEX); | CPU_PREFETCHQ_REMAIN -= 2; |
| if (offset <= sd->u.seg.limit - 3) { | p = CPU_PREFETCHQ + (address & CPU_PREFETCHQ_MASK); |
| addr = CPU_STAT_SREGBASE(CPU_CS_INDEX) + offset; | v = LOADINTELWORD(p); |
| if (!CPU_STAT_PM) | return v; |
| return cpu_memoryread_d(addr); | |
| return cpu_lcmemoryread_d(addr); | |
| } | |
| EXCEPTION(GP_EXCEPTION, 0); | |
| return 0; /* compiler happy */ | |
| } | } |
| INLINE static MEMCALL UINT32 | |
| /* | cpu_prefetchq_3(UINT32 address) |
| * virtual address -> linear address | |
| */ | |
| BYTE MEMCALL | |
| cpu_vmemoryread(int idx, DWORD offset) | |
| { | { |
| descriptor_t *sd; | BYTE *p; |
| DWORD addr; | UINT32 v; |
| int exc; | |
| __ASSERT((unsigned int)idx < CPU_SEGREG_NUM); | |
| sd = &CPU_STAT_SREG(idx); | |
| if (!sd->valid) { | |
| exc = GP_EXCEPTION; | |
| goto err; | |
| } | |
| 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) { | |
| if (idx == CPU_SS_INDEX) | |
| exc = SS_EXCEPTION; | |
| else | |
| exc = GP_EXCEPTION; | |
| goto err; | |
| } | |
| break; | |
| default: | |
| if (offset > sd->u.seg.limit) { | |
| if (idx == CPU_SS_INDEX) | |
| exc = SS_EXCEPTION; | |
| else | |
| exc = GP_EXCEPTION; | |
| goto err; | |
| } | |
| break; | |
| } | |
| } | |
| addr = CPU_STAT_SREGBASE(idx) + offset; | |
| if (!CPU_STAT_PM) | |
| return cpu_memoryread(addr); | |
| return cpu_lmemoryread(addr, CPU_IS_USER_MODE()); | |
| err: | CPU_PREFETCHQ_REMAIN -= 3; |
| EXCEPTION(exc, 0); | p = CPU_PREFETCHQ + (address & CPU_PREFETCHQ_MASK); |
| return 0; /* compiler happy */ | v = LOADINTELWORD(p); |
| v += ((UINT32)p[2]) << 16; | |
| return v; | |
| } | } |
| WORD MEMCALL | INLINE static MEMCALL UINT32 |
| cpu_vmemoryread_w(int idx, DWORD offset) | cpu_prefetchq_d(UINT32 address) |
| { | { |
| descriptor_t *sd; | BYTE *p; |
| DWORD addr; | UINT32 v; |
| int exc; | |
| __ASSERT((unsigned int)idx < CPU_SEGREG_NUM); | |
| sd = &CPU_STAT_SREG(idx); | |
| if (!sd->valid) { | |
| exc = GP_EXCEPTION; | |
| goto err; | |
| } | |
| if (!(sd->flag & CPU_DESC_FLAG_READABLE)) { | CPU_PREFETCHQ_REMAIN -= 4; |
| cpu_memoryread_check(sd, offset, 2, | p = CPU_PREFETCHQ + (address & CPU_PREFETCHQ_MASK); |
| (idx == CPU_SS_INDEX) ? SS_EXCEPTION : GP_EXCEPTION); | v = LOADINTELDWORD(p); |
| } else { | return v; |
| 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; | |
| } | |
| break; | |
| default: | |
| if (offset > sd->u.seg.limit - 1) { | |
| if (idx == CPU_SS_INDEX) | |
| exc = SS_EXCEPTION; | |
| else | |
| exc = GP_EXCEPTION; | |
| goto err; | |
| } | |
| break; | |
| } | |
| } | |
| addr = CPU_STAT_SREGBASE(idx) + offset; | |
| if (!CPU_STAT_PM) | |
| return cpu_memoryread_w(addr); | |
| return cpu_lmemoryread_w(addr, CPU_IS_USER_MODE()); | |
| err: | |
| EXCEPTION(exc, 0); | |
| return 0; /* compiler happy */ | |
| } | } |
| #endif /* IA32_SUPPORT_PREFETCH_QUEUE */ | |
| DWORD MEMCALL | #if defined(IA32_SUPPORT_DEBUG_REGISTER) |
| cpu_vmemoryread_d(int idx, DWORD offset) | INLINE static void |
| check_memory_break_point(UINT32 address, UINT length, UINT rw) | |
| { | { |
| descriptor_t *sd; | int i; |
| DWORD addr; | |
| int exc; | |
| __ASSERT((unsigned int)idx < CPU_SEGREG_NUM); | |
| sd = &CPU_STAT_SREG(idx); | if (CPU_STAT_BP && !(CPU_EFLAG & RF_FLAG)) { |
| if (!sd->valid) { | for (i = 0; i < CPU_DEBUG_REG_INDEX_NUM; i++) { |
| exc = GP_EXCEPTION; | if ((CPU_STAT_BP & (1 << i)) |
| goto err; | && (CPU_DR7_GET_RW(i) & rw) |
| } | |
| 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) { | |
| if (idx == CPU_SS_INDEX) | |
| exc = SS_EXCEPTION; | |
| else | |
| exc = GP_EXCEPTION; | |
| goto err; | |
| } | |
| break; | |
| default: | && ((address <= CPU_DR(i) && address + length > CPU_DR(i)) |
| if (offset > sd->u.seg.limit - 3) { | || (address > CPU_DR(i) && address < CPU_DR(i) + CPU_DR7_GET_LEN(i)))) { |
| if (idx == CPU_SS_INDEX) | CPU_STAT_BP_EVENT |= CPU_STAT_BP_EVENT_B(i); |
| exc = SS_EXCEPTION; | |
| else | |
| exc = GP_EXCEPTION; | |
| goto err; | |
| } | } |
| break; | |
| } | } |
| } | } |
| addr = CPU_STAT_SREGBASE(idx) + offset; | |
| if (!CPU_STAT_PM) | |
| return cpu_memoryread_d(addr); | |
| return cpu_lmemoryread_d(addr, CPU_IS_USER_MODE()); | |
| err: | |
| EXCEPTION(exc, 0); | |
| return 0; /* compiler happy */ | |
| } | } |
| #else | |
| #define check_memory_break_point(address, length, rw) | |
| #endif | |
| /* vaddr memory write */ | /* |
| void MEMCALL | * code fetch |
| cpu_vmemorywrite(int idx, DWORD offset, BYTE val) | */ |
| UINT8 MEMCALL | |
| cpu_codefetch(UINT32 offset) | |
| { | { |
| descriptor_t *sd; | descriptor_t *sd; |
| DWORD addr; | UINT32 addr; |
| int exc; | |
| __ASSERT((unsigned int)idx < CPU_SEGREG_NUM); | |
| sd = &CPU_STAT_SREG(idx); | sd = &CPU_STAT_SREG(CPU_CS_INDEX); |
| if (!sd->valid) { | if (offset <= sd->u.seg.limit) { |
| exc = GP_EXCEPTION; | addr = sd->u.seg.segbase + offset; |
| goto err; | #if defined(IA32_SUPPORT_PREFETCH_QUEUE) |
| } | if (CPU_PREFETCHQ_REMAIN <= 0) { |
| cpu_prefetch(addr); | |
| if (!(sd->flag & CPU_DESC_FLAG_WRITABLE)) { | } |
| cpu_memorywrite_check(sd, offset, 1, | return cpu_prefetchq(addr); |
| (idx == CPU_SS_INDEX) ? SS_EXCEPTION : GP_EXCEPTION); | #else /* !IA32_SUPPORT_PREFETCH_QUEUE */ |
| } else { | if (!CPU_STAT_PAGING) |
| switch (sd->type) { | return cpu_memoryread(addr); |
| case 6: case 7: | return cpu_linear_memory_read_b(addr, CPU_PAGE_READ_CODE | CPU_STAT_USER_MODE); |
| if (offset <= sd->u.seg.limit) { | #endif /* IA32_SUPPORT_PREFETCH_QUEUE */ |
| if (idx == CPU_SS_INDEX) | |
| exc = SS_EXCEPTION; | |
| else | |
| exc = GP_EXCEPTION; | |
| goto err; | |
| } | |
| break; | |
| default: | |
| if (offset > sd->u.seg.limit) { | |
| if (idx == CPU_SS_INDEX) | |
| exc = SS_EXCEPTION; | |
| else | |
| exc = GP_EXCEPTION; | |
| goto err; | |
| } | |
| break; | |
| } | |
| } | |
| addr = CPU_STAT_SREGBASE(idx) + offset; | |
| if (!CPU_STAT_PM) { | |
| /* real mode */ | |
| cpu_memorywrite(addr, val); | |
| } else { | |
| /* protected mode */ | |
| cpu_lmemorywrite(addr, val, CPU_IS_USER_MODE()); | |
| } | } |
| return; | EXCEPTION(GP_EXCEPTION, 0); |
| return 0; /* compiler happy */ | |
| err: | |
| EXCEPTION(exc, 0); | |
| } | } |
| void MEMCALL | UINT16 MEMCALL |
| cpu_vmemorywrite_w(int idx, DWORD offset, WORD val) | cpu_codefetch_w(UINT32 offset) |
| { | { |
| descriptor_t *sd; | descriptor_t *sd; |
| DWORD addr; | UINT32 addr; |
| int exc; | #if defined(IA32_SUPPORT_PREFETCH_QUEUE) |
| UINT16 v; | |
| __ASSERT((unsigned int)idx < CPU_SEGREG_NUM); | #endif |
| sd = &CPU_STAT_SREG(idx); | |
| if (!sd->valid) { | |
| exc = GP_EXCEPTION; | |
| goto err; | |
| } | |
| 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) { | |
| if (idx == CPU_SS_INDEX) | |
| exc = SS_EXCEPTION; | |
| else | |
| exc = GP_EXCEPTION; | |
| goto err; | |
| } | |
| break; | |
| default: | sd = &CPU_STAT_SREG(CPU_CS_INDEX); |
| if (offset > sd->u.seg.limit - 1) { | if (offset <= sd->u.seg.limit - 1) { |
| if (idx == CPU_SS_INDEX) | addr = sd->u.seg.segbase + offset; |
| exc = SS_EXCEPTION; | #if defined(IA32_SUPPORT_PREFETCH_QUEUE) |
| else | if (CPU_PREFETCHQ_REMAIN <= 0) { |
| exc = GP_EXCEPTION; | cpu_prefetch(addr); |
| goto err; | } |
| } | if (CPU_PREFETCHQ_REMAIN >= 2) { |
| break; | return cpu_prefetchq_w(addr); |
| } | } |
| } | |
| addr = CPU_STAT_SREGBASE(idx) + offset; | v = cpu_prefetchq(addr); |
| if (!CPU_STAT_PM) { | addr++; |
| /* real mode */ | cpu_prefetch(addr); |
| cpu_memorywrite_w(addr, val); | v += (UINT16)cpu_prefetchq(addr) << 8; |
| } else { | return v; |
| /* protected mode */ | #else /* !IA32_SUPPORT_PREFETCH_QUEUE */ |
| cpu_lmemorywrite_w(addr, val, CPU_IS_USER_MODE()); | if (!CPU_STAT_PAGING) |
| return cpu_memoryread_w(addr); | |
| return cpu_linear_memory_read_w(addr, CPU_PAGE_READ_CODE | CPU_STAT_USER_MODE); | |
| #endif /* IA32_SUPPORT_PREFETCH_QUEUE */ | |
| } | } |
| return; | EXCEPTION(GP_EXCEPTION, 0); |
| return 0; /* compiler happy */ | |
| err: | |
| EXCEPTION(exc, 0); | |
| } | } |
| void MEMCALL | UINT32 MEMCALL |
| cpu_vmemorywrite_d(int idx, DWORD offset, DWORD val) | cpu_codefetch_d(UINT32 offset) |
| { | { |
| descriptor_t *sd; | descriptor_t *sd; |
| DWORD addr; | UINT32 addr; |
| int exc; | #if defined(IA32_SUPPORT_PREFETCH_QUEUE) |
| UINT32 v; | |
| __ASSERT((unsigned int)idx < CPU_SEGREG_NUM); | #endif |
| sd = &CPU_STAT_SREG(idx); | |
| if (!sd->valid) { | |
| 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) { | |
| if (idx == CPU_SS_INDEX) | |
| exc = SS_EXCEPTION; | |
| else | |
| exc = GP_EXCEPTION; | |
| goto err; | |
| } | |
| break; | |
| default: | |
| if (offset > sd->u.seg.limit - 3) { | |
| if (idx == CPU_SS_INDEX) | |
| exc = SS_EXCEPTION; | |
| else | |
| exc = GP_EXCEPTION; | |
| goto err; | |
| } | |
| break; | |
| } | |
| } | |
| addr = CPU_STAT_SREGBASE(idx) + offset; | |
| if (!CPU_STAT_PM) { | |
| /* real mode */ | |
| cpu_memorywrite_d(addr, val); | |
| } else { | |
| /* protected mode */ | |
| cpu_lmemorywrite_d(addr, val, CPU_IS_USER_MODE()); | |
| } | |
| return; | |
| err: | |
| EXCEPTION(exc, 0); | |
| } | |
| /* | sd = &CPU_STAT_SREG(CPU_CS_INDEX); |
| * physical address memory function | if (offset <= sd->u.seg.limit - 3) { |
| */ | addr = sd->u.seg.segbase + offset; |
| void MEMCALL | #if defined(IA32_SUPPORT_PREFETCH_QUEUE) |
| cpu_memorywrite_d(DWORD address, DWORD value) | if (CPU_PREFETCHQ_REMAIN <= 0) { |
| { | cpu_prefetch(addr); |
| DWORD adr = address & CPU_STAT_ADRSMASK; | |
| DWORD diff; | |
| DWORD off; | |
| if (adr < LOWMEM - 3) { | |
| __i286_memorywrite_d(adr, value); | |
| } else if (adr < LOWMEM) { | |
| diff = LOWMEM - adr; | |
| switch (diff) { | |
| default: | |
| ia32_panic("cpu_memorywrite_d: diff(%d)", diff); | |
| break; | |
| case 3: | |
| __i286_memorywrite_w(adr, value & 0xffff); | |
| value >>= 16; | |
| __i286_memorywrite(adr + 2, value & 0xff); | |
| value >>= 8; | |
| break; | |
| case 2: | |
| __i286_memorywrite_w(adr, value & 0xffff); | |
| value >>= 16; | |
| break; | |
| case 1: | |
| __i286_memorywrite(adr, value & 0xff); | |
| value >>= 8; | |
| break; | |
| } | } |
| if (CPU_PREFETCHQ_REMAIN >= 4) { | |
| if (extmem_size > 0) { | return cpu_prefetchq_d(addr); |
| off = 0; | } else { |
| switch (CPU_PREFETCHQ_REMAIN) { | |
| switch (4 - diff) { | |
| case 3: | |
| cpumem[off++] = value & 0xff; | |
| if (off >= extmem_size) { | |
| OVERRUN_EXCEPTION(); | |
| break; | |
| } | |
| value >>= 8; | |
| /*FALLTHROUGH*/ | |
| case 2: | |
| cpumem[off++] = value & 0xff; | |
| if (off >= extmem_size) { | |
| OVERRUN_EXCEPTION(); | |
| break; | |
| } | |
| value >>= 8; | |
| /*FALLTHROUGH*/ | |
| case 1: | case 1: |
| cpumem[off] = value & 0xff; | v = cpu_prefetchq(addr); |
| addr++; | |
| cpu_prefetch(addr); | |
| v += (UINT32)cpu_prefetchq_3(addr) << 8; | |
| break; | break; |
| } | |
| } else { | |
| OVERRUN_EXCEPTION(); | |
| } | |
| } else if (extmem_size > 0) { | |
| adr -= LOWMEM; | |
| if (adr < extmem_size - 3) { | |
| STOREINTELDWORD(cpumem + adr, value); | |
| } else if (adr < extmem_size) { | |
| diff = extmem_size - adr; | |
| switch (diff) { | case 2: |
| default: | v = cpu_prefetchq_w(addr); |
| ia32_panic("cpu_memorywrite_d: diff(%d)", diff); | addr += 2; |
| cpu_prefetch(addr); | |
| v += (UINT32)cpu_prefetchq_w(addr) << 16; | |
| break; | break; |
| case 3: | case 3: |
| cpumem[adr] = value & 0xff; | v = cpu_prefetchq_3(addr); |
| value >>= 8; | addr += 3; |
| adr++; | cpu_prefetch(addr); |
| /*FALLTHROUGH*/ | v += (UINT32)cpu_prefetchq(addr) << 24; |
| case 2: | |
| cpumem[adr] = value & 0xff; | |
| value >>= 8; | |
| adr++; | |
| /*FALLTHROUGH*/ | |
| case 1: | |
| cpumem[adr] = value & 0xff; | |
| break; | break; |
| } | |
| OVERRUN_EXCEPTION(); | |
| } else { | |
| OVERRUN_EXCEPTION(); | |
| } | |
| } else { | |
| OVERRUN_EXCEPTION(); | |
| } | |
| } | |
| void MEMCALL | |
| cpu_memorywrite_w(DWORD address, WORD value) | |
| { | |
| DWORD adr = address & CPU_STAT_ADRSMASK; | |
| if (adr < LOWMEM - 1) { | |
| __i286_memorywrite_w(adr, value); | |
| } else if (adr < LOWMEM) { | |
| __i286_memorywrite(adr, value & 0xff); | |
| if (extmem_size > 0) { | |
| cpumem[0] = (value >> 8) & 0xff; | |
| } else { | |
| OVERRUN_EXCEPTION(); | |
| } | |
| } else if (extmem_size > 0) { | |
| adr -= LOWMEM; | |
| if (adr < extmem_size - 1) { | |
| STOREINTELWORD(cpumem + adr, value); | |
| } else if (adr == extmem_size - 1) { | |
| cpumem[adr] = value & 0xff; | |
| OVERRUN_EXCEPTION(); | |
| } else { | |
| OVERRUN_EXCEPTION(); | |
| } | |
| } else { | |
| OVERRUN_EXCEPTION(); | |
| } | |
| } | |
| void MEMCALL | |
| cpu_memorywrite(DWORD address, BYTE value) | |
| { | |
| DWORD adr = address & CPU_STAT_ADRSMASK; | |
| if (adr < LOWMEM) { | |
| __i286_memorywrite(adr, value); | |
| } else if (extmem_size > 0) { | |
| adr -= LOWMEM; | |
| if (adr < extmem_size) { | |
| cpumem[adr] = value; | |
| } else { | |
| OVERRUN_EXCEPTION(); | |
| } | |
| } else { | |
| OVERRUN_EXCEPTION(); | |
| } | |
| } | |
| DWORD MEMCALL | |
| cpu_memoryread_d(DWORD address) | |
| { | |
| DWORD adr = address & CPU_STAT_ADRSMASK; | |
| DWORD val; | |
| DWORD diff; | |
| int shift; | |
| if (adr < LOWMEM - 3) { | |
| val = __i286_memoryread_d(adr); | |
| } else if (adr < LOWMEM) { | |
| diff = LOWMEM - adr; | |
| switch (diff) { | |
| default: | |
| ia32_panic("cpu_memoryread_d: diff(%d)", diff); | |
| val = 0; /* compiler happy */ | |
| break; | |
| case 3: | |
| val = __i286_memoryread_w(adr); | |
| val |= (DWORD)__i286_memoryread(adr + 2) << 16; | |
| if (extmem_size > 0) { | |
| val |= (DWORD)cpumem[0] << 24; | |
| } else { | |
| val |= 0xff000000; | |
| OVERRUN_EXCEPTION(); | |
| } | |
| break; | |
| case 2: | |
| val = __i286_memoryread_w(adr); | |
| if (extmem_size > 1) { | |
| val |= ((DWORD)LOADINTELWORD(cpumem)) << 16; | |
| } else if (extmem_size > 0) { | |
| val |= 0xff000000 | ((DWORD)cpumem[0] << 16); | |
| OVERRUN_EXCEPTION(); | |
| } else { | |
| val |= 0xffff0000; | |
| OVERRUN_EXCEPTION(); | |
| } | |
| break; | |
| case 1: | |
| val = __i286_memoryread(adr); | |
| if (extmem_size > 2) { | |
| val |= (DWORD)LOADINTELWORD(cpumem) << 8; | |
| val |= (DWORD)cpumem[2] << 24; | |
| } else if (extmem_size > 1) { | |
| val |= ((DWORD)LOADINTELWORD(cpumem)) << 8; | |
| val |= 0xff000000; | |
| OVERRUN_EXCEPTION(); | |
| } else if (extmem_size > 0) { | |
| val |= 0xffff0000 | ((DWORD)cpumem[0] << 8); | |
| OVERRUN_EXCEPTION(); | |
| } else { | |
| val |= 0xffffff00; | |
| OVERRUN_EXCEPTION(); | |
| } | |
| break; | |
| } | |
| } else if (extmem_size > 0) { | |
| adr -= LOWMEM; | |
| if (adr < extmem_size - 3) { | |
| val = LOADINTELDWORD(cpumem + adr); | |
| } else if (adr < extmem_size) { | |
| diff = extmem_size - adr; | |
| val = 0; | |
| shift = 0; | |
| switch (diff) { | |
| default: | default: |
| ia32_panic("cpu_memoryread_d: diff(%d)", diff); | ia32_panic("cpu_codefetch_d: remain bytes is invalid"); |
| break; | v = 0; /* compiler happy */ |
| case 3: | |
| val |= (DWORD)cpumem[adr]; | |
| shift += 8; | |
| adr++; | |
| /*FALLTHROUGH*/ | |
| case 2: | |
| val |= (DWORD)cpumem[adr] << shift; | |
| shift += 8; | |
| adr++; | |
| /*FALLTHROUGH*/ | |
| case 1: | |
| val |= (DWORD)cpumem[adr] << shift; | |
| shift += 8; | |
| break; | break; |
| } | } |
| val |= ((DWORD)-1) << shift; | return v; |
| OVERRUN_EXCEPTION(); | |
| } else { | |
| val = (DWORD)-1; | |
| OVERRUN_EXCEPTION(); | |
| } | } |
| } else { | #else /* !IA32_SUPPORT_PREFETCH_QUEUE */ |
| val = (DWORD)-1; | if (!CPU_STAT_PAGING) |
| OVERRUN_EXCEPTION(); | return cpu_memoryread_d(addr); |
| return cpu_linear_memory_read_d(addr, CPU_PAGE_READ_CODE | CPU_STAT_USER_MODE); | |
| #endif /* IA32_SUPPORT_PREFETCH_QUEUE */ | |
| } | } |
| return val; | EXCEPTION(GP_EXCEPTION, 0); |
| return 0; /* compiler happy */ | |
| } | } |
| WORD MEMCALL | |
| cpu_memoryread_w(DWORD address) | |
| { | |
| DWORD adr = address & CPU_STAT_ADRSMASK; | |
| WORD val; | |
| if (adr < LOWMEM - 1) { | |
| val = __i286_memoryread_w(adr); | |
| } else if (adr < LOWMEM) { | |
| val = __i286_memoryread(adr); | |
| if (extmem_size > 0) { | |
| val |= (WORD)cpumem[0] << 8; | |
| } else { | |
| val |= 0xff00; | |
| OVERRUN_EXCEPTION(); | |
| } | |
| } else if (extmem_size > 0) { | |
| adr -= LOWMEM; | |
| if (adr < extmem_size - 1) { | |
| val = LOADINTELWORD(cpumem + adr); | |
| } else if (adr == extmem_size - 1) { | |
| val = 0xff00 | cpumem[adr]; | |
| OVERRUN_EXCEPTION(); | |
| } else { | |
| val = (WORD)-1; | |
| OVERRUN_EXCEPTION(); | |
| } | |
| } else { | |
| val = (WORD)-1; | |
| OVERRUN_EXCEPTION(); | |
| } | |
| return val; | |
| } | |
| BYTE MEMCALL | /* |
| cpu_memoryread(DWORD address) | * virtual address memory access functions |
| { | */ |
| DWORD adr = address & CPU_STAT_ADRSMASK; | #include "cpu_mem.mcr" |
| BYTE val; | |
| if (adr < LOWMEM) { | VIRTUAL_ADDRESS_MEMORY_ACCESS_FUNCTION(b, UINT8, 1) |
| val = __i286_memoryread(adr); | VIRTUAL_ADDRESS_MEMORY_ACCESS_FUNCTION(w, UINT16, 2) |
| } else if (extmem_size > 0) { | VIRTUAL_ADDRESS_MEMORY_ACCESS_FUNCTION(d, UINT32, 4) |
| adr -= LOWMEM; | |
| if (adr < extmem_size) { | |
| val = cpumem[adr]; | |
| } else { | |
| val = (BYTE)-1; | |
| OVERRUN_EXCEPTION(); | |
| } | |
| } else { | |
| val = (BYTE)-1; | |
| OVERRUN_EXCEPTION(); | |
| } | |
| return val; | |
| } |