home *** CD-ROM | disk | FTP | other *** search
/ Media Share 13 / mediashare_13.zip / mediashare_13 / ZIPPED / PROGRAM / DDJ9403A.ZIP / UC394.ZIP / 386.H next >
C/C++ Source or Header  |  1993-12-05  |  12KB  |  297 lines

  1. //**************************************
  2. // GATES.H
  3. // by Alex Shmidt, Nov 1993
  4. //
  5. // useful defines for system descriptors
  6. //**************************************
  7.  
  8. #ifndef _386_H
  9. #define _386_H
  10.  
  11. #pragma pack (1)
  12.  
  13. // 80386/80486 exceptions
  14. #define DIVIDE_ERROR     0      // fault
  15. #define DEBUGGER_INT     1      // fault or trap
  16. #define NONMASKABLE_INT  2      // interrupt
  17. #define BREAKPOINT       3      // trap
  18. #define OVERFLOW_INT     4      // trap - interrupt on overflow ( INTO )
  19. #define BOUND_VIOLATION  5      // fault - array boundary violation ( BOUND )
  20. #define INVALID_OPCODE   6      // fault
  21. #define MATH_NOT_AVL     7      // fault - coprocessor not available
  22. #define DOUBLE_FAULT     8      // abort
  23. #define MATHSEG_OVERRUN  9      // coprocessor segment overrun (reserved on 486)
  24. #define INVALID_TSS      10     // fault
  25. #define SEG_NOT_PRESENT  11     // fault
  26. #define STACK_EXCEPTION  12     // fault
  27. #define GENERAL_PROTECT  13     // fault - General Protection Violation
  28. #define PAGE_FAULT       14     // fault
  29. #define RESERVED_EXCEPT  15
  30. #define MATH_ERROR       16     // fault - coprocessor error
  31. #define ALIGNMENT_CHECK  17     // fault - ( 80486 only )
  32.  
  33. // Task Segment State
  34. typedef struct {
  35.  WORD  TSS_Back_Link;      // Selector of the TSS previously executing
  36.  WORD  padding1;
  37.  DWORD TSS_esp0;           // ESP for RING 0 execution
  38.  WORD  TSS_ss0;            // SS for RING 0 execution
  39.  WORD  padding2;
  40.  DWORD TSS_esp1;           // ESP for RING 1 execution
  41.  WORD  TSS_ss1;            // SS for RING 1 execution
  42.  WORD  padding3;
  43.  DWORD TSS_esp2;           // ESP for RING 2 execution
  44.  WORD  TSS_ss2;            // SS for RING 2 execution
  45.  WORD  padding4;
  46.  DWORD TSS_cr3;            // Task CR3 ( page directory address )
  47.  DWORD TSS_eip;            // Task EIP
  48.  DWORD TSS_EFlags;         // Task EFLAGS register
  49.  DWORD TSS_eax;
  50.  DWORD TSS_ecx;
  51.  DWORD TSS_edx;
  52.  DWORD TSS_ebx;
  53.  DWORD TSS_esp;
  54.  DWORD TSS_ebp;
  55.  DWORD TSS_esi;
  56.  DWORD TSS_edi;
  57.  WORD  TSS_es;
  58.  WORD  padding5;
  59.  WORD  TSS_cs;
  60.  WORD  padding6;
  61.  WORD  TSS_ss;
  62.  WORD  padding7;
  63.  WORD  TSS_ds;
  64.  WORD  padding8;
  65.  WORD  TSS_fs;
  66.  WORD  padding9;
  67.  WORD  TSS_gs;
  68.  WORD  padding10;
  69.  WORD  TSS_LDTR;           // Selector for the task LDT
  70.  WORD  padding11;
  71.  WORD  TSS_Trap_Word;      // contains T bit ( trap on task switch )
  72.  WORD  TSS_IOBP;           // a base of IO permission bitmap
  73. }TSS_386,* PTSS_386,far *LPTSS_386;
  74.  
  75. // Segment Descriptors
  76. typedef struct {
  77.  WORD Seg_Desc_Limit_0_15;   // Limit bits 0 - 15
  78.  WORD Seg_Desc_Base_0_15;    // Base BITS 0 - 15
  79.  BYTE Seg_Desc_Base_16_23;   // Base BITS 16 - 23
  80.  BYTE Seg_Desc_Access_Right; // Access right byte
  81.  BYTE Seg_Desc_Gran_Byte;    // Granularity, etc
  82.  BYTE Seg_Desc_Base_24_31;   // Base bits 24 - 31
  83. }SEGDESCRIPTOR,*PSEGDESCTIPTOR,far *LPSEGDESCRIPTOR;
  84.  
  85. typedef struct {
  86.  WORD Offset_O_15;            // entry point's offset, lower 16
  87.  WORD Selector;               // entry point's selector
  88.  BYTE DWord_Count;            // (D)Word parameter count
  89.  BYTE Access_Rights;          // present, dpl, system, type
  90.  WORD Offset_16_31;           // entry point's offset, upper 16
  91. }CALLGATEDESCRPT,* PCALLGATEDESCRPT, far *LPCALLGATEDESCRPT;
  92.  
  93. // Descriptor types
  94. #define  DESC_MASK    0x0f     /* TYPE field for system and gate descriptors */
  95.  
  96. #define DESC_TYPE_UNUSED            0       // invalid descriptor
  97. #define DESC_TYPE_286_TSS           1       // 80286 TSS descriptor
  98. #define DESC_TYPE_LDT               2
  99. #define DESC_TYPE_BUSY_286_TSS      3
  100. #define DESC_TYPE_286_CALL_GATE     4       // 286 call gate
  101. #define DESC_TYPE_TASK_GATE         5
  102. #define DESC_TYPE_286_INT_GATE      6
  103. #define DESC_TYPE_286_TRAP_GATE     7
  104. #define DESC_TYPE_386_TSS           9       // 80386/80486 TSS
  105. #define DESC_TYPE_BUSY_386_TSS      11      // 80386/80486 busy TSS
  106. #define DESC_TYPE_386_CALL_GATE     12      // 80386/80486 call gate
  107. #define DESC_TYPE_386_INT_GATE      14      // 80386/80486 interrupt gate
  108. #define DESC_TYPE_386_TRAP_GATE     15      // 80386/80486 trap gate
  109.  
  110. #define CALLGATE_DDCOUNT_MASK       0x1f    // dword count < 32
  111.  
  112. /* CR0 bit assignments */
  113. #define  PE_Mask           0x1   /* 1 = Protected Mode */
  114. #define  PE_Bit            0
  115. #define  MP_Mask           0x2   /* 1 = Monitor Coprocessor */
  116. #define  MP_Bit            1
  117. #define  EM_Mask           0x4   /* 1 = Emulate Math Coprocessor */
  118. #define  EM_Bit            2
  119. #define  TS_Mask           0x8   /* 1 = Task Switch occured */
  120. #define  TS_Bit            3
  121. #define  ET_Mask           0x10  /* 1 = 387 present, 0 = 287 present */
  122. #define  ET_Bit            4
  123. #define  PG_Mask      0x80000000 /* 1 = paging enabled, 0 = paging disabled */
  124. #define  PG_Bit            31
  125.  
  126.  
  127. /* EFLAGs bit assignments */
  128. #define  CF_Mask           0x1      /* Carry flag */
  129. #define  CF_Bit            0
  130. #define  PF_Mask           0x2      /* Parity flag */
  131. #define  PF_Bit            2
  132. #define  AF_Mask           0x10     /* Aux flag */
  133. #define  AF_Bit            4
  134. #define  ZF_Mask           0x40     /* Zero flag */
  135. #define  ZF_Bit            6
  136. #define  SF_Mask           0x80     /* Sign flag */
  137. #define  SF_Bit            7
  138. #define  TF_Mask           0x100    /* Trace flag */
  139. #define  TF_Bit            8
  140. #define  IF_Mask           0x200    /* Int flag */
  141. #define  IF_Bit            9
  142. #define  DF_Mask           0x400    /* Direction flag */
  143. #define  DB_Bit            10
  144. #define  OF_Mask           0x800    /* Overflow flag */
  145. #define  OF_Bit            11
  146. #define  IOPL_Mask         0x3000   /* IOPL flags */
  147. #define  IOPL_Bit0         12
  148. #define  IOPL_Bit1         13
  149. #define  NT_Mask           0x4000   /* Nested task flag */
  150. #define  NT_Bit            14
  151. #define  RF_Mask           0x10000  /* Resume flag */
  152. #define  RF_Bit            16
  153. #define  VM_Mask           0x20000  /* Virtual Mode flag */
  154. #define  VM_Bit            17
  155.  
  156.  
  157. /*******************************************************************/
  158. /* PAGE TABLE EQUATES */
  159. /*******************************************************************/
  160.  
  161. /* Linear address breakdown */
  162.  
  163. #define LIN_DIR      0xffc00000        /* page directory */
  164. #define LIN_PAGE     0x003ff000        /* page number */
  165. #define LIN_OFFSET   0x00000fff        /* offset */
  166.  
  167. #define PAGE_FRAME_ADS_MASK (LIN_DIR | LIN_PAGE) // page frame adress 12 - 31
  168. #define PAGE_PWT              8           // page write-through ( 486 only )
  169. #define PAGE_PCD              0x10        // page cache disable ( 486 only )
  170.  
  171. #define PTABLE_SIZE           1024
  172. #define P_SIZE                0x1000         /* page size */
  173.  
  174. /*******************************************************************
  175.  
  176.         Page table entry bits
  177.  
  178. *******************************************************************/
  179.  
  180. #define  P_PRES            0x1            /* page present bit (1 - present)*/
  181. #define  P_WRITE           0x2            /* write access bit */
  182. #define  P_USER            0x4            /* 1 - User mode , 0 - Supervisor */
  183. #define  P_ACC             0x20           /* page accessed (write, read, fetch)*/
  184. #define  P_DIRTY           0x40           /* page 'dirty'(written) */
  185.  
  186. #define  P_AVAIL           (P_PRES+P_WRITE+P_USER) /* avail to everyone & present */
  187.  
  188. /*******************************************************************
  189.  
  190.    Page types - definition of the OS reserved bits in the page table
  191.                entry.
  192. *******************************************************************/
  193.  
  194. #define  PG_TYPE            0xE00         /* TYPE bits in PTE */
  195.  
  196. /*******************************************************************
  197.  
  198.          Page types for page allocator calls (Windows specific)
  199.  
  200. *******************************************************************/
  201. #define  PG_VM              0
  202. #define  PG_SYS             1
  203. #define  PG_RESERVED1       2
  204. #define  PG_PRIVATE         3
  205. #define  PG_RESERVED2       4
  206. #define  PG_RELOCK          5             /* PRIVATE to MMGR */
  207. #define  PG_INSTANCE        6
  208. #define  PG_HOOKED          7
  209. #define  PG_IGNORE          0xFFFFFFFF
  210.  
  211.  
  212. /*******************************************************************
  213.  
  214.          Types for page table entries
  215.  
  216. *******************************************************************/
  217. #define  PgT_VM              (PG_VM << 9)
  218. #define  PgT_SYS             (PG_SYS << 9)
  219. #define  PgT_RESERVED1       (PG_RESERVED1 << 9)
  220. #define  PgT_PRIVATE         (PG_PRIVATE << 9)
  221. #define  PgT_RESERVED2       (PG_RESERVED2 << 9)
  222. #define  PgT_RELOCK          (PG_RELOCK << 9)
  223. #define  PgT_INSTANCE        (PG_INSTANCE << 9)
  224. #define  PgT_HOOKED          (PG_HOOKED << 9)
  225.  
  226. /*******************************************************************
  227.  
  228.   Definitions for the access byte in a descriptor
  229.  
  230. *******************************************************************/
  231.  
  232.  
  233. /* Following fields are common to segment and control descriptors */
  234.  
  235. #define  D_PRES         0x80           /* present in memory */
  236. #define  D_NOTPRES      0              /* not present in memory */
  237.  
  238. #define  D_DPL0         0              /* Ring 0 */
  239. #define  D_DPL1         0x20           /* Ring 1 */
  240. #define  D_DPL2         0x40           /* Ring 2 */
  241. #define  D_DPL3         0x60           /* Ring 3 */
  242.  
  243. #define  D_SEG          0x10           /* Segment descriptor */
  244. #define  D_CTRL         0              /* Control descriptor */
  245.  
  246. #define  D_GRAN_BYTE    000            /* Segment length is byte granular */
  247. #define  D_GRAN_PAGE    0x80           /* Segment length is page granular */
  248. #define  D_DEF16        000            /* Default operation size is 16 bits */
  249. #define  D_DEF32        0x40           /* Default operation size is 32 bits */
  250.  
  251.  
  252. /* Following fields are specific to segment descriptors */
  253.  
  254. #define  D_CODE         0x08           /* code */
  255. #define  D_DATA         0              /* data */
  256.  
  257. #define  D_RX           0x02           /* if code, readable */
  258. #define  D_X            0              /* if code, exec only */
  259. #define  D_W            0x02           /* if data, writable */
  260. #define  D_R            0              /* if data, read only */
  261.  
  262. #define  D_C            0x04           /* if code, conforming */
  263. #define  D_E            0x04           /* if data, expand-down */
  264.  
  265. #define  D_ACCESSED     1              /* segment accessed bit */
  266.  
  267.  
  268. /* Useful combination access rights bytes */
  269.  
  270. #define  GATE32_RING3   (D_PRES+D_DPL3+D_CTRL+DESC_TYPE_386_CALL_GATE)
  271. #define  RW_Data_Type   (D_PRES+D_SEG+D_DATA+D_W)
  272. #define  R_Data_Type    (D_PRES+D_SEG+D_DATA+D_R)
  273. #define  Code_Type      (D_PRES+D_SEG+D_CODE+D_RX)
  274.  
  275. #define  D_PAGE32       (D_GRAN_PAGE+D_DEF32)      /* 32 bit Page granular */
  276.  
  277. /* Masks for selector fields */
  278.  
  279. #define  SELECTOR_MASK  0xfff8         /* selector index */
  280. #define  SEL_LOW_MASK   0xf8           /* mask for low byte of sel indx */
  281. #define  TABLE_MASK     0x04           /* table bit */
  282. #define  RPL_MASK       0x03           /* privilige bits */
  283. #define  RPL_CLR        ~0x03          /* clear ring bits */
  284.  
  285. #define MAKEBASE(base015, base1623, base2431) \
  286.         ((DWORD)(((WORD)(base2431) << 8) | (WORD)(base1623)) << 16) | \
  287.         ((WORD)(base015))
  288.  
  289. #define MAKELIMIT(limit015, limit1619) \
  290.         ((DWORD)((WORD)(limit1619) & 0x000f) << 16) | (WORD)(limit015)
  291.  
  292. #define MAKELPG(dir,tab) (((DWORD)((dir) & 0x3ff) << 10 ) | ((tab) & 0x3ff))
  293.  
  294. #define MAKELIN(dir,tab,off) ((MAKELPG(dir,tab) << 12) | ((off) & 0xfff))
  295.  
  296. #endif // _386_H
  297.