diff options
Diffstat (limited to 'i386.h')
| -rw-r--r-- | i386.h | 184 |
1 files changed, 184 insertions, 0 deletions
| @@ -0,0 +1,184 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 2004 Security Architects Corporation. All rights reserved. | ||
| 3 | * | ||
| 4 | * Module Name: | ||
| 5 | * | ||
| 6 | * i386.h | ||
| 7 | * | ||
| 8 | * Abstract: | ||
| 9 | * | ||
| 10 | * This module definies various types and macros used by x86 specific routines. | ||
| 11 | * | ||
| 12 | * Author: | ||
| 13 | * | ||
| 14 | * Eugene Tsyrklevich 24-Mar-2004 | ||
| 15 | * | ||
| 16 | * Revision History: | ||
| 17 | * | ||
| 18 | * None. | ||
| 19 | */ | ||
| 20 | |||
| 21 | |||
| 22 | #ifndef __I386_H__ | ||
| 23 | #define __I386_H__ | ||
| 24 | |||
| 25 | |||
| 26 | |||
| 27 | typedef struct _KTSS { | ||
| 28 | |||
| 29 | USHORT Backlink; | ||
| 30 | USHORT Reserved0; | ||
| 31 | |||
| 32 | ULONG Esp0; | ||
| 33 | USHORT Ss0; | ||
| 34 | USHORT Reserved1; | ||
| 35 | |||
| 36 | ULONG NotUsed1[4]; | ||
| 37 | |||
| 38 | ULONG CR3; | ||
| 39 | |||
| 40 | ULONG Eip; | ||
| 41 | |||
| 42 | ULONG NotUsed2[9]; | ||
| 43 | |||
| 44 | USHORT Es; | ||
| 45 | USHORT Reserved2; | ||
| 46 | |||
| 47 | USHORT Cs; | ||
| 48 | USHORT Reserved3; | ||
| 49 | |||
| 50 | USHORT Ss; | ||
| 51 | USHORT Reserved4; | ||
| 52 | |||
| 53 | USHORT Ds; | ||
| 54 | USHORT Reserved5; | ||
| 55 | |||
| 56 | USHORT Fs; | ||
| 57 | USHORT Reserved6; | ||
| 58 | |||
| 59 | USHORT Gs; | ||
| 60 | USHORT Reserved7; | ||
| 61 | |||
| 62 | USHORT LDT; | ||
| 63 | USHORT Reserved8; | ||
| 64 | |||
| 65 | USHORT Flags; | ||
| 66 | |||
| 67 | USHORT IoMapBase; | ||
| 68 | |||
| 69 | /* IO/INT MAPS go here */ | ||
| 70 | |||
| 71 | } KTSS, *PKTSS; | ||
| 72 | |||
| 73 | |||
| 74 | typedef struct _KGDTENTRY { | ||
| 75 | USHORT LimitLow; | ||
| 76 | USHORT BaseLow; | ||
| 77 | union { | ||
| 78 | struct { | ||
| 79 | UCHAR BaseMid; | ||
| 80 | UCHAR Flags1; // Declare as bytes to avoid alignment | ||
| 81 | UCHAR Flags2; // Problems. | ||
| 82 | UCHAR BaseHi; | ||
| 83 | } Bytes; | ||
| 84 | struct { | ||
| 85 | ULONG BaseMid : 8; | ||
| 86 | ULONG Type : 5; | ||
| 87 | ULONG Dpl : 2; | ||
| 88 | ULONG Pres : 1; | ||
| 89 | ULONG LimitHi : 4; | ||
| 90 | ULONG Sys : 1; | ||
| 91 | ULONG Reserved_0 : 1; | ||
| 92 | ULONG Default_Big : 1; | ||
| 93 | ULONG Granularity : 1; | ||
| 94 | ULONG BaseHi : 8; | ||
| 95 | } Bits; | ||
| 96 | } HighWord; | ||
| 97 | } KGDTENTRY, *PKGDTENTRY; | ||
| 98 | |||
| 99 | |||
| 100 | #define INTERRUPTS_OFF() _asm { cli } | ||
| 101 | #define INTERRUPTS_ON() _asm { sti } | ||
| 102 | |||
| 103 | |||
| 104 | /* | ||
| 105 | * WP Write Protect (bit 16 of CR0). | ||
| 106 | * Inhibits supervisor-level procedures from writing into user-level read-only pages when set; | ||
| 107 | * allows supervisor-level procedures to write into user-level read-only pages when clear. | ||
| 108 | * This flag facilitates implementation of the copyon-write method of creating a new process (forking) | ||
| 109 | * used by operating systems such as UNIX. | ||
| 110 | */ | ||
| 111 | |||
| 112 | #define CR0_WP_BIT (0x10000) | ||
| 113 | |||
| 114 | #define MEMORY_PROTECTION_OFF() \ | ||
| 115 | __asm mov eax, cr0 \ | ||
| 116 | __asm and eax, NOT CR0_WP_BIT \ | ||
| 117 | __asm mov cr0, eax | ||
| 118 | |||
| 119 | #define MEMORY_PROTECTION_ON() \ | ||
| 120 | __asm mov eax, cr0 \ | ||
| 121 | __asm or eax, CR0_WP_BIT \ | ||
| 122 | __asm mov cr0, eax | ||
| 123 | |||
| 124 | |||
| 125 | |||
| 126 | /* x86 opcodes */ | ||
| 127 | |||
| 128 | #define X86_OPCODE_PUSH 0x68 | ||
| 129 | #define X86_OPCODE_MOV_EAX_VALUE 0xB8 | ||
| 130 | |||
| 131 | #define X86_OPCODE_CALL_EAX 0xD0FF | ||
| 132 | #define X86_OPCODE_JMP_DWORD_PTR 0x25FF | ||
| 133 | |||
| 134 | |||
| 135 | |||
| 136 | /* | ||
| 137 | * Save a value on stack: | ||
| 138 | * | ||
| 139 | * push PushValue | ||
| 140 | */ | ||
| 141 | |||
| 142 | #define ASM_PUSH(CodeAddress, PushValue) \ | ||
| 143 | * (PCHAR) (CodeAddress)++ = X86_OPCODE_PUSH; \ | ||
| 144 | * (PULONG) (CodeAddress) = (ULONG) (PushValue); \ | ||
| 145 | (PCHAR) (CodeAddress) += 4; | ||
| 146 | |||
| 147 | /* | ||
| 148 | * Call a function: | ||
| 149 | * | ||
| 150 | * mov eax, FunctionAddress | ||
| 151 | * call eax | ||
| 152 | */ | ||
| 153 | |||
| 154 | #define ASM_CALL(CodeAddress, FunctionAddress) \ | ||
| 155 | * (PCHAR) (CodeAddress)++ = X86_OPCODE_MOV_EAX_VALUE; \ | ||
| 156 | * (PULONG) (CodeAddress) = (ULONG) (FunctionAddress); \ | ||
| 157 | (PCHAR) (CodeAddress) += 4; \ | ||
| 158 | * ((PUSHORT) (CodeAddress))++ = X86_OPCODE_CALL_EAX; | ||
| 159 | |||
| 160 | |||
| 161 | /* | ||
| 162 | * Jump to a specified address: | ||
| 163 | * | ||
| 164 | * jmp dword ptr [next_4_bytes] | ||
| 165 | * *(next_4_bytes) = address | ||
| 166 | * | ||
| 167 | * NOTE XXX: this should be converted to a direct jmp address but i | ||
| 168 | * can't figure out how that instruction is encoded (opcode 0xE9) | ||
| 169 | */ | ||
| 170 | |||
| 171 | #define ASM_JMP(CodeAddress, JmpAddress) \ | ||
| 172 | * ((PUSHORT) (CodeAddress))++ = X86_OPCODE_JMP_DWORD_PTR; \ | ||
| 173 | * (PULONG) (CodeAddress) = (ULONG) (JmpAddress); \ | ||
| 174 | (PCHAR) (CodeAddress) += 4; | ||
| 175 | |||
| 176 | |||
| 177 | extern ULONG SystemAddressStart; | ||
| 178 | |||
| 179 | |||
| 180 | BOOLEAN InitI386(); | ||
| 181 | VOID VerifyUserReturnAddress(); | ||
| 182 | |||
| 183 | |||
| 184 | #endif /* __I386_H__ */ \ No newline at end of file | ||
