Diff for /np2/i386c/ia32/cpu_mem.c between versions 1.20 and 1.24

version 1.20, 2005/03/09 17:12:34 version 1.24, 2011/12/16 09:05:42
Line 1 Line 1
 /*      $Id$    */  
   
 /*  /*
  * Copyright (c) 2002-2004 NONAKA Kimihiro   * Copyright (c) 2002-2004 NONAKA Kimihiro
  * All rights reserved.   * All rights reserved.
Line 12 Line 10
  * 2. Redistributions in binary form must reproduce the above copyright   * 2. Redistributions in binary form must reproduce the above copyright
  *    notice, this list of conditions and the following disclaimer in the   *    notice, this list of conditions and the following disclaimer in the
  *    documentation and/or other materials provided with the distribution.   *    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   * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES   * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
Line 35 Line 31
 /*  /*
  * memory access check   * memory access check
  */   */
 void  static int MEMCALL check_limit_upstairs(descriptor_t *sdp, UINT32 offset, UINT len);
 cpu_memoryread_check(descriptor_t *sd, UINT32 offset, UINT length, int e)  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)
 {  {
         UINT32 uplimit;          UINT32 limit;
           UINT32 end;
   
         if (CPU_STAT_PM) {          __ASSERT(sdp != NULL);
                 /* invalid */          __ASSERT(len > 0);
                 if (!sd->valid) {  
                         VERBOSE(("cpu_memoryread_check: invalid"));  
                         EXCEPTION(GP_EXCEPTION, 0);  
                 }  
   
                 /* not present */          len--;
                 if (!sd->p) {          end = offset + len;
                         VERBOSE(("cpu_memoryread_check: not present"));          limit = SEG_IS_32BIT(sdp) ? 0xffffffff : 0x0000ffff;
                         EXCEPTION(e, 0);  
           if (SEG_IS_DATA(sdp) && SEG_IS_EXPANDDOWN_DATA(sdp)) {
                   /* expand-down data segment */
                   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] */
                                           return 0;
                                   }
                           } 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] */
                                   return 0;
                           }
                 }                  }
           } else {
                   /* expand-up data or code segment */
                   if (sdp->u.seg.limit == limit) {
                           /*
                            *   32bit       16bit
                            * +-------+   +-------+ FFFFFFFFh
                            * |       |   |       |
                            * |       |   +  [1]  + 0000FFFFh
                            * | valid |   |       |
                            * |       |   +-------+ 0000FFFFh - len - 1
                            * |       |   | valid |
                            * +-------+   +-------+ 00000000h
                            */
                           if (!SEG_IS_32BIT(sdp)) {
                                   if ((len > limit)               /* len check */
                                    || (offset + len > limit)) {   /* [1] */
                                           return 0;
                                   }
                           } else {
                                   sdp->flag |= CPU_DESC_FLAG_WHOLEADR;
                           }
                   } else {
                           /*
                            *   32bit       16bit
                            * +-------+   +-------+ FFFFFFFFh
                            * |       |   |       |
                            * |       |   +.......+ 0000FFFFh
                            * |  [1]  |   |  [1]  |
                            * +.......+   +.......+ seg.limit
                            * |       |   |       |
                            * +-------+   +-------+ seg.limit - len - 1
                            * | valid |   | valid |
                            * +-------+   +-------+ 00000000h
                            */
                           if ((len > sdp->u.seg.limit)            /* len check */
                            || (end < offset)                      /* wrap check */
                            || (end > sdp->u.seg.limit)) {         /* [1] */
                                   return 0;
                           }
                   }
           }
           return 1;       /* Ok! */
   }
   
   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 0:  case 1:        /* ro */
         case 2:  case 3:        /* rw */          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 4:  case 5:        /* ro (expand down) */
         case 6:  case 7:        /* rw (expand down) */          case 6:  case 7:        /* rw (expand down) */
                 uplimit = sd->d ? 0xffffffff : 0x0000ffff;          case 10: case 11:       /* rx */
                 if (offset <= sd->u.seg.limit) {          case 14: case 15:       /* rxc */
                         VERBOSE(("cpu_memoryread_check: offset(%08x) <= sd->u.seg.limit(%08x)", offset, sd->u.seg.limit));                  if (!check_limit_upstairs(sdp, offset, len))
                         EXCEPTION(e, 0);                          goto exc;
                 }  
                 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;                  break;
   
         default:          default:
                 VERBOSE(("cpu_memoryread_check: invalid type (type = %d)", sd->type));                  goto exc;
                 EXCEPTION(e, 0);  
                 break;  
         }          }
         sd->flag |= CPU_DESC_FLAG_READABLE;          sdp->flag |= CPU_DESC_FLAG_READABLE;
           return;
   
   exc:
           VERBOSE(("cpu_memoryread_check: check failure."));
           VERBOSE(("offset = 0x%08x, len = %d", offset, len));
   #if defined(DEBUG)
           segdesc_dump(sdp);
   #endif
           EXCEPTION(e, 0);
 }  }
   
 void  static void MEMCALL
 cpu_memorywrite_check(descriptor_t *sd, UINT32 offset, UINT length, int e)  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 */          __ASSERT(sdp != NULL);
                 if (!sd->p) {          __ASSERT(len > 0);
                         VERBOSE(("cpu_memorywrite_check: not present"));  
                         EXCEPTION(e, 0);  
                 }  
   
                 if (!sd->s) {          if (!SEG_IS_VALID(sdp)) {
                         VERBOSE(("cpu_memorywrite_check: system segment"));                  e = GP_EXCEPTION;
                         EXCEPTION(e, 0);                  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 */          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) */          case 6: case 7: /* rw (expand down) */
                 uplimit = sd->d ? 0xffffffff : 0x0000ffff;                  if (!check_limit_upstairs(sdp, offset, len))
                 if (offset <= sd->u.seg.limit) {                          goto exc;
                         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;                  break;
   
         default:          default:
                 VERBOSE(("cpu_memorywrite_check: invalid type (type = %d)", sd->type));                  goto exc;
                 EXCEPTION(e, 0);  
                 break;  
         }          }
         sd->flag |= CPU_DESC_FLAG_WRITABLE;          sdp->flag |= CPU_DESC_FLAG_WRITABLE | CPU_DESC_FLAG_READABLE;
           return;
   
   exc:
           VERBOSE(("cpu_memorywrite_check: check failure."));
           VERBOSE(("offset = 0x%08x, len = %d", offset, len));
   #if defined(DEBUG)
           segdesc_dump(sdp);
   #endif
           EXCEPTION(e, 0);
 }  }
   
 void  void
 cpu_stack_push_check(UINT16 s, descriptor_t *sd, UINT32 esp, UINT length)  cpu_stack_push_check(UINT16 s, descriptor_t *sdp, UINT32 sp, UINT len)
 {  {
         UINT32 limit;          UINT32 limit;
           UINT32 start;
   
         if (CPU_STAT_PM) {          __ASSERT(sdp != NULL);
                 if (!sd->valid || !sd->p) {          __ASSERT(len > 0);
                         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) {          if (!SEG_IS_VALID(sdp)
                         limit = 0xffff;           || !SEG_IS_PRESENT(sdp)
                 } else {           || SEG_IS_SYSTEM(sdp)
                         limit = 0xffffffff;           || SEG_IS_CODE(sdp)
            || !SEG_IS_WRITABLE_DATA(sdp)) {
                   goto exc;
           }
   
           len--;
           start = sp - len;
           limit = SEG_IS_32BIT(sdp) ? 0xffffffff : 0x0000ffff;
   
           if (SEG_IS_EXPANDDOWN_DATA(sdp)) {
                   /* expand-down stack */
                   if (!SEG_IS_32BIT(sdp)) {
                           if (sp > limit) {                       /* [*] */
                                   goto exc;
                           }
                 }                  }
                 if (sd->u.seg.ec) {                  if (sdp->u.seg.limit == 0) {
                         /* expand-down stack */                          /*
                         if ((esp == 0)                           *   32bit       16bit
                          || (esp < length)                           * +-------+   +-------+ FFFFFFFFh
                          || (esp - length <= sd->u.seg.limit)                           * |       |   |  [*]  |
                          || (esp > limit)) {                           * |       |   +-------+ 0000FFFFh
                                 VERBOSE(("cpu_stack_push_check: expand-down, esp = %08x, length = %08x", esp, length));                           * | valid |   |       |
                                 VERBOSE(("cpu_stack_push_check: limit = %08x, seglimit = %08x", limit, sd->u.seg.limit));                           * |       |   | valid |
                                 VERBOSE(("cpu_stack_push_check: segbase = %08x, segend = %08x", sd->u.seg.segbase, sd->u.seg.segend));                           * |       |   |       |
                                 EXCEPTION(SS_EXCEPTION, s & 0xfffc);                           * +-------+   +-------+ 00000000h
                            */
                           if (!SEG_IS_32BIT(sdp)) {
                                   if (sp > limit) {               /* [1] */
                                           goto exc;
                                   }
                           } else {
                                   sdp->flag |= CPU_DESC_FLAG_WHOLEADR;
                         }                          }
                 } else {                  } else {
                         /* expand-up stack */                          /*
                         if (esp == 0) {                           *   32bit       16bit
                                 if ((sd->d && (sd->u.seg.segend != 0xffffffff))                           * +-------+   +-------+ FFFFFFFFh
                                  || (!sd->d && (sd->u.seg.segend != 0xffff))) {                           * |       |   |  [*]  |
                                         VERBOSE(("cpu_stack_push_check: expand-up, esp = %08x, length = %08x", esp, length));                           * | valid |   +-------+ 0000FFFFh
                                         VERBOSE(("cpu_stack_push_check: limit = %08x, seglimit = %08x", limit, sd->u.seg.limit));                           * |       |   | valid |
                                         VERBOSE(("cpu_stack_push_check: segbase = %08x, segend = %08x", sd->u.seg.segbase, sd->u.seg.segend));                           * +-------+   +-------+ seg.limit + len - 1
                                         EXCEPTION(SS_EXCEPTION, s & 0xfffc);                           * |       |   |       |
                            * +..[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;
                           }
                   }
           } 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 {                          } else {
                                 if ((esp < length)                                  sdp->flag |= CPU_DESC_FLAG_WHOLEADR;
                                  || (esp - 1 > sd->u.seg.limit)) {                          }
                                         VERBOSE(("cpu_stack_push_check: expand-up, esp = %08x, length = %08x", esp, length));                  } else {
                                         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));                           *   32bit       16bit
                                         EXCEPTION(SS_EXCEPTION, s & 0xfffc);                           * +-------+   +-------+ 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] */
                                   goto exc;
                         }                          }
                 }                  }
         }          }
           return;
   
   exc:
           VERBOSE(("cpu_stack_push_check: check failure."));
           VERBOSE(("s = 0x%04x, sp = 0x%08x, len = %d", s, sp, len));
   #if defined(DEBUG)
           segdesc_dump(sdp);
   #endif
           EXCEPTION(SS_EXCEPTION, s & 0xfffc);
 }  }
   
 void  void
 cpu_stack_pop_check(UINT16 s, descriptor_t *sd, UINT32 esp, UINT length)  cpu_stack_pop_check(UINT16 s, descriptor_t *sdp, UINT32 sp, UINT len)
 {  {
         UINT32 limit;  
   
         if (CPU_STAT_PM) {          __ASSERT(sdp != NULL);
                 if (!sd->valid || !sd->p) {          __ASSERT(len > 0);
                         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) {          if (!SEG_IS_VALID(sdp)
                         limit = 0xffff;           || !SEG_IS_PRESENT(sdp)
                 } else {           || SEG_IS_SYSTEM(sdp)
                         limit = 0xffffffff;           || SEG_IS_CODE(sdp)
                 }           || !SEG_IS_WRITABLE_DATA(sdp)) {
                 if (sd->u.seg.ec) {                  goto exc;
                         /* 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 ((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);  
                         }  
                 }  
         }          }
   
           if (!check_limit_upstairs(sdp, sp, len))
                   goto exc;
           return;
   
   exc:
           VERBOSE(("cpu_stack_pop_check: check failure."));
           VERBOSE(("s = 0x%04x, sp = 0x%08x, len = %d", s, sp, len));
   #if defined(DEBUG)
           segdesc_dump(sdp);
   #endif
           EXCEPTION(SS_EXCEPTION, s & 0xfffc);
 }  }
   
 #if defined(IA32_SUPPORT_DEBUG_REGISTER)  #if defined(IA32_SUPPORT_DEBUG_REGISTER)
 INLINE static void  static INLINE void
 check_memory_break_point(UINT32 address, UINT length, UINT rw)  check_memory_break_point(UINT32 address, UINT length, UINT rw)
 {  {
         int i;          int i;
Line 283  check_memory_break_point(UINT32 address, Line 395  check_memory_break_point(UINT32 address,
 UINT8 MEMCALL  UINT8 MEMCALL
 cpu_codefetch(UINT32 offset)  cpu_codefetch(UINT32 offset)
 {  {
         descriptor_t *sd;          descriptor_t *sdp;
         UINT32 addr;          UINT32 addr;
 #if defined(IA32_SUPPORT_TLB)  
         TLB_ENTRY_T *ep;          TLB_ENTRY_T *ep;
 #endif  
   
         sd = &CPU_STAT_SREG(CPU_CS_INDEX);          sdp = &CPU_CS_DESC;
         if (offset <= sd->u.seg.limit) {          if (offset <= sdp->u.seg.limit) {
                 addr = sd->u.seg.segbase + offset;                  addr = sdp->u.seg.segbase + offset;
                 if (!CPU_STAT_PAGING)                  if (!CPU_STAT_PAGING)
                         return cpu_memoryread(addr);                          return cpu_memoryread(addr);
 #if defined(IA32_SUPPORT_TLB)  
                 ep = tlb_lookup(addr, ucrw);                  ep = tlb_lookup(addr, ucrw);
                 if (ep != NULL && ep->memp != NULL) {                  if (ep != NULL && ep->memp != NULL) {
                         return ep->memp[addr & 0xfff];                          return ep->memp[addr & 0xfff];
                 }                  }
 #endif  
                 return cpu_linear_memory_read_b(addr, ucrw);                  return cpu_linear_memory_read_b(addr, ucrw);
         }          }
         EXCEPTION(GP_EXCEPTION, 0);          EXCEPTION(GP_EXCEPTION, 0);
Line 309  cpu_codefetch(UINT32 offset) Line 417  cpu_codefetch(UINT32 offset)
 UINT16 MEMCALL  UINT16 MEMCALL
 cpu_codefetch_w(UINT32 offset)  cpu_codefetch_w(UINT32 offset)
 {  {
         descriptor_t *sd;          descriptor_t *sdp;
         UINT32 addr;          UINT32 addr;
 #if defined(IA32_SUPPORT_TLB)  
         TLB_ENTRY_T *ep;          TLB_ENTRY_T *ep;
         UINT16 value;          UINT16 value;
 #endif  
   
         sd = &CPU_STAT_SREG(CPU_CS_INDEX);          sdp = &CPU_CS_DESC;
         if (offset <= sd->u.seg.limit - 1) {          if (offset <= sdp->u.seg.limit - 1) {
                 addr = sd->u.seg.segbase + offset;                  addr = sdp->u.seg.segbase + offset;
                 if (!CPU_STAT_PAGING)                  if (!CPU_STAT_PAGING)
                         return cpu_memoryread_w(addr);                          return cpu_memoryread_w(addr);
 #if defined(IA32_SUPPORT_TLB)  
                 ep = tlb_lookup(addr, ucrw);                  ep = tlb_lookup(addr, ucrw);
                 if (ep != NULL && ep->memp != NULL) {                  if (ep != NULL && ep->memp != NULL) {
                         if ((addr + 1) & 0x00000fff) {                          if ((addr + 1) & 0x00000fff) {
Line 334  cpu_codefetch_w(UINT32 offset) Line 439  cpu_codefetch_w(UINT32 offset)
                                 return value;                                  return value;
                         }                          }
                 }                  }
 #endif  
                 return cpu_linear_memory_read_w(addr, ucrw);                  return cpu_linear_memory_read_w(addr, ucrw);
         }          }
         EXCEPTION(GP_EXCEPTION, 0);          EXCEPTION(GP_EXCEPTION, 0);
Line 344  cpu_codefetch_w(UINT32 offset) Line 448  cpu_codefetch_w(UINT32 offset)
 UINT32 MEMCALL  UINT32 MEMCALL
 cpu_codefetch_d(UINT32 offset)  cpu_codefetch_d(UINT32 offset)
 {  {
         descriptor_t *sd;          descriptor_t *sdp;
         UINT32 addr;          UINT32 addr;
 #if defined(IA32_SUPPORT_TLB)  
         TLB_ENTRY_T *ep[2];          TLB_ENTRY_T *ep[2];
         UINT32 value;          UINT32 value;
         UINT remain;          UINT remain;
 #endif  
   
         sd = &CPU_STAT_SREG(CPU_CS_INDEX);          sdp = &CPU_CS_DESC;
         if (offset <= sd->u.seg.limit - 3) {          if (offset <= sdp->u.seg.limit - 3) {
                 addr = sd->u.seg.segbase + offset;                  addr = sdp->u.seg.segbase + offset;
                 if (!CPU_STAT_PAGING)                  if (!CPU_STAT_PAGING)
                         return cpu_memoryread_d(addr);                          return cpu_memoryread_d(addr);
 #if defined(IA32_SUPPORT_TLB)  
                 ep[0] = tlb_lookup(addr, ucrw);                  ep[0] = tlb_lookup(addr, ucrw);
                 if (ep[0] != NULL && ep[0]->memp != NULL) {                  if (ep[0] != NULL && ep[0]->memp != NULL) {
                         remain = 0x1000 - (addr & 0xfff);                          remain = 0x1000 - (addr & 0xfff);
Line 391  cpu_codefetch_d(UINT32 offset) Line 492  cpu_codefetch_d(UINT32 offset)
                                 return value;                                  return value;
                         }                          }
                 }                  }
 #endif  
                 return cpu_linear_memory_read_d(addr, ucrw);                  return cpu_linear_memory_read_d(addr, ucrw);
         }          }
         EXCEPTION(GP_EXCEPTION, 0);          EXCEPTION(GP_EXCEPTION, 0);
         return 0;       /* compiler happy */          return 0;       /* compiler happy */
 }  }
   
   #undef  ucrw
   
 /*  /*
  * additional physical address memory access functions   * additional physical address memory access functions
  */   */
 UINT64 MEMCALL  UINT64 MEMCALL
 cpu_memoryread_q(UINT32 address)  cpu_memoryread_q(UINT32 paddr)
 {  {
         UINT64 value;          UINT64 value;
   
         value = cpu_memoryread_d(address);          value = cpu_memoryread_d(paddr);
         value += (UINT64)cpu_memoryread_d(address + 4) << 32;          value += (UINT64)cpu_memoryread_d(paddr + 4) << 32;
   
         return value;          return value;
 }  }
   
 REG80 MEMCALL  void MEMCALL
 cpu_memoryread_f(UINT32 address)  cpu_memorywrite_q(UINT32 paddr, UINT64 value)
 {  {
         REG80 value;  
         UINT i;  
   
         for (i = 0; i < sizeof(REG80); ++i) {          cpu_memorywrite_d(paddr, (UINT32)value);
                 value.b[i] = cpu_memoryread(address + i);          cpu_memorywrite_d(paddr + 4, (UINT32)(value >> 32));
         }  
         return value;  
 }  }
   
 void MEMCALL  REG80 MEMCALL
 cpu_memorywrite_q(UINT32 address, UINT64 value)  cpu_memoryread_f(UINT32 paddr)
 {  {
           REG80 value;
           int i;
   
         cpu_memorywrite_d(address, (UINT32)value);          for (i = 0; i < (int)sizeof(REG80); ++i) {
         cpu_memorywrite_d(address + 4, (UINT32)(value >> 32));                  value.b[i] = cpu_memoryread(paddr + i);
           }
           return value;
 }  }
   
 void MEMCALL  void MEMCALL
 cpu_memorywrite_f(UINT32 address, const REG80 *value)  cpu_memorywrite_f(UINT32 paddr, const REG80 *value)
 {  {
         UINT i;          int i;
   
         for (i = 0; i < sizeof(REG80); ++i) {          for (i = 0; i < (int)sizeof(REG80); ++i) {
                 cpu_memorywrite(address + i, value->b[i]);                  cpu_memorywrite(paddr + i, value->b[i]);
         }          }
 }  }
   
Line 454  VIRTUAL_ADDRESS_MEMORY_ACCESS_FUNCTION(d Line 556  VIRTUAL_ADDRESS_MEMORY_ACCESS_FUNCTION(d
 UINT64 MEMCALL  UINT64 MEMCALL
 cpu_vmemoryread_q(int idx, UINT32 offset)  cpu_vmemoryread_q(int idx, UINT32 offset)
 {  {
         descriptor_t *sd;          descriptor_t *sdp;
         UINT32 addr;          UINT32 addr;
         int exc;          int exc;
   
         __ASSERT((unsigned int)idx < CPU_SEGREG_NUM);          __ASSERT((unsigned int)idx < CPU_SEGREG_NUM);
   
         sd = &CPU_STAT_SREG(idx);          sdp = &CPU_STAT_SREG(idx);
         if (!sd->valid) {          if (!SEG_IS_VALID(sdp)) {
                 exc = GP_EXCEPTION;                  exc = GP_EXCEPTION;
                 goto err;                  goto err;
         }          }
   
         if (!(sd->flag & CPU_DESC_FLAG_READABLE)) {          if (!(sdp->flag & CPU_DESC_FLAG_READABLE)) {
                 cpu_memoryread_check(sd, offset, 8,                  cpu_memoryread_check(sdp, offset, 8,
                     (idx == CPU_SS_INDEX) ? SS_EXCEPTION : GP_EXCEPTION);                      (idx == CPU_SS_INDEX) ? SS_EXCEPTION : GP_EXCEPTION);
         } else {          } else if (!(sdp->flag & CPU_DESC_FLAG_WHOLEADR)) {
                 switch (sd->type) {                  if (!check_limit_upstairs(sdp, offset, 8))
                 case 4: case 5: case 6: case 7:                          goto range_failure;
                         if (offset - (8 - 1) <= sd->u.seg.limit)  
                                 goto range_failure;  
                         break;  
   
                 default:  
                         if (offset > sd->u.seg.limit - (8 - 1))  
                                 goto range_failure;  
                         break;  
                 }  
         }           } 
         addr = sd->u.seg.segbase + offset;          addr = sdp->u.seg.segbase + offset;
         check_memory_break_point(addr, 8, CPU_DR7_RW_RO);          check_memory_break_point(addr, 8, CPU_DR7_RW_RO);
         if (!CPU_STAT_PAGING)          if (!CPU_STAT_PAGING)
                 return cpu_memoryread_q(addr);                  return cpu_memoryread_q(addr);
         return cpu_linear_memory_read_q(addr, CPU_PAGE_READ_DATA | CPU_STAT_USER_MODE);          return cpu_linear_memory_read_q(addr, CPU_PAGE_READ_DATA | CPU_STAT_USER_MODE);
   
 range_failure:  range_failure:
         if (idx == CPU_SS_INDEX) {          VERBOSE(("cpu_vmemoryread_q: type = %d, offset = %08x, limit = %08x", sdp->type, offset, sdp->u.seg.limit));
                 exc = SS_EXCEPTION;          exc = (idx == CPU_SS_INDEX) ? SS_EXCEPTION : GP_EXCEPTION;
         } else {  
                 exc = GP_EXCEPTION;  
         }  
         VERBOSE(("cpu_vmemoryread_q: type = %d, offset = %08x, limit = %08x", sd->type, offset, sd->u.seg.limit));  
 err:  err:
         EXCEPTION(exc, 0);          EXCEPTION(exc, 0);
         return 0;       /* compiler happy */          return 0;       /* compiler happy */
Line 503  err: Line 592  err:
 void MEMCALL  void MEMCALL
 cpu_vmemorywrite_q(int idx, UINT32 offset, UINT64 value)  cpu_vmemorywrite_q(int idx, UINT32 offset, UINT64 value)
 {  {
         descriptor_t *sd;          descriptor_t *sdp;
         UINT32 addr;          UINT32 addr;
         int exc;          int exc;
   
         __ASSERT((unsigned int)idx < CPU_SEGREG_NUM);          __ASSERT((unsigned int)idx < CPU_SEGREG_NUM);
   
         sd = &CPU_STAT_SREG(idx);          sdp = &CPU_STAT_SREG(idx);
         if (!sd->valid) {          if (!SEG_IS_VALID(sdp)) {
                 exc = GP_EXCEPTION;                  exc = GP_EXCEPTION;
                 goto err;                  goto err;
         }          }
   
         if (!(sd->flag & CPU_DESC_FLAG_WRITABLE)) {          if (!(sdp->flag & CPU_DESC_FLAG_WRITABLE)) {
                 cpu_memorywrite_check(sd, offset, 8,                  cpu_memorywrite_check(sdp, offset, 8,
                     (idx == CPU_SS_INDEX) ? SS_EXCEPTION : GP_EXCEPTION);                      (idx == CPU_SS_INDEX) ? SS_EXCEPTION : GP_EXCEPTION);
         } else {          } else if (!(sdp->flag & CPU_DESC_FLAG_WHOLEADR)) {
                 switch (sd->type) {                  if (!check_limit_upstairs(sdp, offset, 8))
                 case 6: case 7:                          goto range_failure;
                         if (offset - (8 - 1) <= sd->u.seg.limit)  
                                 goto range_failure;  
                         break;  
   
                 default:  
                         if (offset > sd->u.seg.limit - (8 - 1))  
                                 goto range_failure;  
                         break;  
                 }  
         }          }
         addr = sd->u.seg.segbase + offset;          addr = sdp->u.seg.segbase + offset;
         check_memory_break_point(addr, 8, CPU_DR7_RW_RW);          check_memory_break_point(addr, 8, CPU_DR7_RW_RW);
         if (!CPU_STAT_PAGING) {          if (!CPU_STAT_PAGING) {
                 cpu_memorywrite_q(addr, value);                  cpu_memorywrite_q(addr, value);
         } else {          } else {
                 cpu_linear_memory_write_q(addr, value, CPU_PAGE_WRITE_DATA | CPU_STAT_USER_MODE);                  cpu_linear_memory_write_q(addr, value, CPU_PAGE_READ_DATA | CPU_STAT_USER_MODE);
         }          }
         return;          return;
   
 range_failure:  range_failure:
         if (idx == CPU_SS_INDEX) {          VERBOSE(("cpu_vmemorywrite_q: type = %d, offset = %08x, limit = %08x", sdp->type, offset, sdp->u.seg.limit));
                 exc = SS_EXCEPTION;          exc = (idx == CPU_SS_INDEX) ? SS_EXCEPTION : GP_EXCEPTION;
         } else {  
                 exc = GP_EXCEPTION;  
         }  
         VERBOSE(("cpu_vmemorywrite_q: type = %d, offset = %08x, limit = %08x", sd->type, offset, sd->u.seg.limit));  
 err:  err:
         EXCEPTION(exc, 0);          EXCEPTION(exc, 0);
 }  }
Line 554  err: Line 630  err:
 REG80 MEMCALL  REG80 MEMCALL
 cpu_vmemoryread_f(int idx, UINT32 offset)  cpu_vmemoryread_f(int idx, UINT32 offset)
 {  {
         descriptor_t *sd;          descriptor_t *sdp;
         UINT32 addr;          UINT32 addr;
         int exc;          int exc;
   
         __ASSERT((unsigned int)idx < CPU_SEGREG_NUM);          __ASSERT((unsigned int)idx < CPU_SEGREG_NUM);
   
         sd = &CPU_STAT_SREG(idx);          sdp = &CPU_STAT_SREG(idx);
         if (!sd->valid) {          if (!SEG_IS_VALID(sdp)) {
                 exc = GP_EXCEPTION;                  exc = GP_EXCEPTION;
                 goto err;                  goto err;
         }          }
   
         if (!(sd->flag & CPU_DESC_FLAG_READABLE)) {          if (!(sdp->flag & CPU_DESC_FLAG_READABLE)) {
                 cpu_memoryread_check(sd, offset, 10,                  cpu_memoryread_check(sdp, offset, 10,
                     (idx == CPU_SS_INDEX) ? SS_EXCEPTION : GP_EXCEPTION);                      (idx == CPU_SS_INDEX) ? SS_EXCEPTION : GP_EXCEPTION);
         } else {          } else if (!(sdp->flag & CPU_DESC_FLAG_WHOLEADR)) {
                 switch (sd->type) {                  if (!check_limit_upstairs(sdp, offset, 10))
                 case 4: case 5: case 6: case 7:                          goto range_failure;
                         if (offset - (10 - 1) <= sd->u.seg.limit)  
                                 goto range_failure;  
                         break;  
   
                 default:  
                         if (offset > sd->u.seg.limit - (10 - 1))  
                                 goto range_failure;  
                         break;  
                 }  
         }           } 
         addr = sd->u.seg.segbase + offset;          addr = sdp->u.seg.segbase + offset;
         check_memory_break_point(addr, 10, CPU_DR7_RW_RO);          check_memory_break_point(addr, 10, CPU_DR7_RW_RO);
         if (!CPU_STAT_PAGING)          if (!CPU_STAT_PAGING)
                 return cpu_memoryread_f(addr);                  return cpu_memoryread_f(addr);
         return cpu_linear_memory_read_f(addr, CPU_PAGE_READ_DATA | CPU_STAT_USER_MODE);          return cpu_linear_memory_read_f(addr, CPU_PAGE_READ_DATA | CPU_PAGE_READ_DATA | CPU_STAT_USER_MODE);
   
 range_failure:  range_failure:
         if (idx == CPU_SS_INDEX) {          VERBOSE(("cpu_vmemoryread_f: type = %d, offset = %08x, limit = %08x", sdp->type, offset, sdp->u.seg.limit));
                 exc = SS_EXCEPTION;          exc = (idx == CPU_SS_INDEX) ? SS_EXCEPTION : GP_EXCEPTION;
         } else {  
                 exc = GP_EXCEPTION;  
         }  
         VERBOSE(("cpu_vmemoryread_f: type = %d, offset = %08x, limit = %08x", sd->type, offset, sd->u.seg.limit));  
 err:  err:
         EXCEPTION(exc, 0);          EXCEPTION(exc, 0);
         {          {
Line 607  err: Line 670  err:
 void MEMCALL  void MEMCALL
 cpu_vmemorywrite_f(int idx, UINT32 offset, const REG80 *value)  cpu_vmemorywrite_f(int idx, UINT32 offset, const REG80 *value)
 {  {
         descriptor_t *sd;          descriptor_t *sdp;
         UINT32 addr;          UINT32 addr;
         int exc;          int exc;
   
         __ASSERT((unsigned int)idx < CPU_SEGREG_NUM);          __ASSERT((unsigned int)idx < CPU_SEGREG_NUM);
   
         sd = &CPU_STAT_SREG(idx);          sdp = &CPU_STAT_SREG(idx);
         if (!sd->valid) {          if (!SEG_IS_VALID(sdp)) {
                 exc = GP_EXCEPTION;                  exc = GP_EXCEPTION;
                 goto err;                  goto err;
         }          }
   
         if (!(sd->flag & CPU_DESC_FLAG_WRITABLE)) {          if (!(sdp->flag & CPU_DESC_FLAG_WRITABLE)) {
                 cpu_memorywrite_check(sd, offset, 10,                  cpu_memorywrite_check(sdp, offset, 10,
                     (idx == CPU_SS_INDEX) ? SS_EXCEPTION : GP_EXCEPTION);                      (idx == CPU_SS_INDEX) ? SS_EXCEPTION : GP_EXCEPTION);
         } else {          } else if (!(sdp->flag & CPU_DESC_FLAG_WHOLEADR)) {
                 switch (sd->type) {                  if (!check_limit_upstairs(sdp, offset, 10))
                 case 6: case 7:                          goto range_failure;
                         if (offset - (10 - 1) <= sd->u.seg.limit)  
                                 goto range_failure;  
                         break;  
   
                 default:  
                         if (offset > sd->u.seg.limit - (10 - 1))  
                                 goto range_failure;  
                         break;  
                 }  
         }          }
         addr = sd->u.seg.segbase + offset;          addr = sdp->u.seg.segbase + offset;
         check_memory_break_point(addr, 10, CPU_DR7_RW_RW);          check_memory_break_point(addr, 10, CPU_DR7_RW_RW);
         if (!CPU_STAT_PAGING) {          if (!CPU_STAT_PAGING) {
                 cpu_memorywrite_f(addr, value);                  cpu_memorywrite_f(addr, value);
Line 645  cpu_vmemorywrite_f(int idx, UINT32 offse Line 699  cpu_vmemorywrite_f(int idx, UINT32 offse
         return;          return;
   
 range_failure:  range_failure:
         if (idx == CPU_SS_INDEX) {          VERBOSE(("cpu_vmemorywrite_f: type = %d, offset = %08x, limit = %08x", sdp->type, offset, sdp->u.seg.limit));
                 exc = SS_EXCEPTION;          exc = (idx == CPU_SS_INDEX) ? SS_EXCEPTION : GP_EXCEPTION;
         } else {  
                 exc = GP_EXCEPTION;  
         }  
         VERBOSE(("cpu_vmemorywrite_f: type = %d, offset = %08x, limit = %08x", sd->type, offset, sd->u.seg.limit));  
 err:  err:
         EXCEPTION(exc, 0);          EXCEPTION(exc, 0);
 }  }

Removed from v.1.20  
changed lines
  Added in v.1.24


RetroPC.NET-CVS <cvs@retropc.net>