x86.h 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821
  1. /*++
  2. Copyright (c) 2012 Minoca Corp.
  3. This file is licensed under the terms of the GNU General Public License
  4. version 3. Alternative licensing terms are available. Contact
  5. info@minocacorp.com for details. See the LICENSE file at the root of this
  6. project for complete licensing information.
  7. Module Name:
  8. x86.h
  9. Abstract:
  10. This header contains definitions for aspects of the system that are specific
  11. to the x86 architecture.
  12. Author:
  13. Evan Green 3-Jul-2012
  14. --*/
  15. //
  16. // ------------------------------------------------------------------- Includes
  17. //
  18. #include <minoca/kernel/x86defs.h>
  19. //
  20. // ---------------------------------------------------------------- Definitions
  21. //
  22. //
  23. // --------------------------------------------------------------------- Macros
  24. //
  25. //
  26. // ------------------------------------------------------ Data Type Definitions
  27. //
  28. /*++
  29. Structure Description:
  30. This structure defines the format of a task, interrupt, or call gate
  31. descriptor. This structure must not be padded, since the hardware relies on
  32. this exact format.
  33. Members:
  34. LowOffset - Stores the lower 16 bits of the gate's destination address.
  35. Selector - Stores the code segment selector the gate code should run in.
  36. Count - Must be 0 for entries in the IDT.
  37. Access - Stores various properties of the gate.
  38. Bit 7: Present. 1 if the gate is present, 0 if not present.
  39. Bits 6-5: DPL. Sets the ring number this handler executes in. Zero is
  40. the most privileged ring, 3 is least privileged.
  41. Bit 4: Reserved (set to 0).
  42. Bits 3-0: The gate type. Set to CALL_GATE_TYPE, INTERRUPT_GATE_TYPE,
  43. TASK_GATE_TYPE, or TRAP_GATE_TYPE.
  44. HighOffset - Stores the upper 16 bits of the interrupt handler's address.
  45. --*/
  46. typedef struct _PROCESSOR_GATE {
  47. USHORT LowOffset;
  48. USHORT Selector;
  49. BYTE Count;
  50. BYTE Access;
  51. USHORT HighOffset;
  52. } PACKED PROCESSOR_GATE, *PPROCESSOR_GATE;
  53. /*++
  54. Structure Description:
  55. This structure defines the format of the GDTR, IDTR, or TR. This structure
  56. must be packed since it represents a hardware construct.
  57. Members:
  58. Limit - Stores the last valid byte of the table, essentially size - 1.
  59. Base - Stores a pointer to the Global Descriptor Table, Interrupt
  60. Descriptor Table, or Task Table.
  61. --*/
  62. typedef struct _TABLE_REGISTER {
  63. USHORT Limit;
  64. ULONG Base;
  65. } PACKED TABLE_REGISTER, *PTABLE_REGISTER;
  66. /*++
  67. Structure Description:
  68. This structure defines the x86 Task State Segment. It represents a complete
  69. task state as understood by the hardware.
  70. Members:
  71. BackLink - Stores a pointer to the previous executing task. This value is
  72. written by the processor.
  73. Esp0-2 - Stores the stack pointer to load for each of the privilege levels.
  74. Ss0-2 - Stores the stack segment to load for each of the privilege levels.
  75. Pad0-9 - Stores padding in the structure. The processor does not use these
  76. fields, but they should not be modified.
  77. Cr3 - Stores the value of CR3 used by the task.
  78. Eip - Stores the currently executing instruction pointer.
  79. Eflags through Edi - Stores the state of the general registers when this
  80. task was last run.
  81. Es through Gs - Stores the state of the segment registers when this task
  82. was last run.
  83. LdtSelector - Stores the selector of the Local Descriptor Table when this
  84. task was last run.
  85. DebugTrap - Stores information only relevant when doing on-chip debugging.
  86. IoMapBase - Stores the 16 bit offset from the TSS base to the 8192 byte I/O
  87. Bitmap.
  88. --*/
  89. typedef struct _TSS {
  90. ULONG BackLink;
  91. ULONG Esp0;
  92. USHORT Ss0;
  93. USHORT Pad0;
  94. ULONG Esp1;
  95. USHORT Ss1;
  96. USHORT Pad1;
  97. ULONG Esp2;
  98. USHORT Ss2;
  99. USHORT Pad2;
  100. ULONG Cr3;
  101. ULONG Eip;
  102. ULONG Eflags;
  103. ULONG Eax;
  104. ULONG Ecx;
  105. ULONG Edx;
  106. ULONG Ebx;
  107. ULONG Esp;
  108. ULONG Ebp;
  109. ULONG Esi;
  110. ULONG Edi;
  111. USHORT Es;
  112. USHORT Pad3;
  113. USHORT Cs;
  114. USHORT Pad4;
  115. USHORT Ss;
  116. USHORT Pad5;
  117. USHORT Ds;
  118. USHORT Pad6;
  119. USHORT Fs;
  120. USHORT Pad7;
  121. USHORT Gs;
  122. USHORT Pad8;
  123. USHORT LdtSelector;
  124. USHORT Pad9;
  125. USHORT DebugTrap;
  126. USHORT IoMapBase;
  127. } PACKED TSS, *PTSS;
  128. /*++
  129. Structure Description:
  130. This structure define a Global Descriptor Table entry. The GDT table sets
  131. up the segmentation features of the processor and privilege levels.
  132. Members:
  133. LimitLow - Stores the lower 16 bits of the descriptor limit.
  134. BaseLow - Stores the lower 16 bits of the descriptor base.
  135. BaseMiddle - Stores the next 8 bits of the base.
  136. Access - Stores the access flags. The access byte has the following format:
  137. | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
  138. | | | | |
  139. | P | DPL | S | Type |
  140. P - Is segment present (1 = Yes)
  141. DPL - Descriptor privilege level: Ring 0-3. Zero is the highest
  142. privilege, 3 is the lowest (least privileged).
  143. S - System flag. Set to 0 if it's a system segment, or 1 if it's a
  144. code/data segment.
  145. Type - Segment type: code segment / data segment. The Type field
  146. has the following definition:
  147. Bit 3 - Set to 1 for Code, or 0 for Data.
  148. Bit 2 - Expansion direction. Set to 0 for expand-up, or 1 for
  149. expand-down.
  150. Bit 1 - Write-Enable. Set to 0 for Read Only, or 1 for
  151. Read/Write.
  152. Bit 0 - Accessed. This bit is set by the processor when memory
  153. in this segment is accessed. It is never cleared by
  154. hardware.
  155. Granularity - Stores the granularity for the descriptor. The granularity
  156. byte has the following format:
  157. | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
  158. | | | | | |
  159. | G | D | L | A | Segment length 19:16 |
  160. G - Granularity. 0 = 1 byte, 1 = 1 KByte.
  161. D - Operand Size. 0 = 16/64 bit, 1 = 32 bit.
  162. L - Long mode (64 bit).
  163. A - Available for system use (always zero).
  164. BaseHigh - Stores the high 8 bits of the base address.
  165. --*/
  166. typedef struct _GDT_ENTRY {
  167. USHORT LimitLow;
  168. USHORT BaseLow;
  169. UCHAR BaseMiddle;
  170. UCHAR Access;
  171. UCHAR Granularity;
  172. UCHAR BaseHigh;
  173. } PACKED GDT_ENTRY, *PGDT_ENTRY;
  174. /*++
  175. Structure Description:
  176. This structure defines the format of an entry in a page table or directory.
  177. Members:
  178. Present - Stores whether or not the page is present in memory.
  179. Writable - Stores whether or not this page is read-only (0) or writable (1).
  180. User - Stores whether or not this page is accessible by user mode (1) or
  181. only by kernel mode (0).
  182. WriteThrough - Stores whether or not write-through caching is enabled (1)
  183. or write-back caching (0).
  184. CacheDisabled - Stores whether or not to use caching. If this bit is set,
  185. the page will not be cached.
  186. Accessed - Stores whether or not the page has been accessed. This bit is
  187. set automatically by the processor, but will never be cleared by the
  188. processor.
  189. Dirty - Stores whether or not this page has been written to. This bit is
  190. set automatically by the processor, but must be cleared by software.
  191. LargePage - Stores whether or not large 4MB pages are in use (1) or 4kB
  192. pages (0).
  193. Global - Stores whether or not the TLB should avoid flushing this address
  194. if CR3 is changed. If this bit is set, then the TLB entry for this page
  195. will not be invalidated when CR3 is reset.
  196. Unused - These bits are unused by both the processor and the OS.
  197. Entry - Stores a pointer to the 4kB aligned page.
  198. --*/
  199. typedef struct _PTE {
  200. ULONG Present:1;
  201. ULONG Writable:1;
  202. ULONG User:1;
  203. ULONG WriteThrough:1;
  204. ULONG CacheDisabled:1;
  205. ULONG Accessed:1;
  206. ULONG Dirty:1;
  207. ULONG LargePage:1;
  208. ULONG Global:1;
  209. ULONG Unused:3;
  210. ULONG Entry:20;
  211. } PACKED PTE, *PPTE;
  212. /*++
  213. Structure Description:
  214. This structure defines the extended state of the x86 architecture. This
  215. structure is architecturally defined by the FXSAVE and FXRSTOR instructions.
  216. Members:
  217. Registers - Stores the extended processor state.
  218. --*/
  219. struct _FPU_CONTEXT {
  220. USHORT Fcw;
  221. USHORT Fsw;
  222. USHORT Ftw;
  223. USHORT Fop;
  224. ULONG FpuIp;
  225. USHORT Cs;
  226. USHORT Reserved1;
  227. ULONG FpuDp;
  228. USHORT Ds;
  229. USHORT Reserved2;
  230. ULONG Mxcsr;
  231. ULONG MxcsrMask;
  232. UCHAR St0Mm0[16];
  233. UCHAR St1Mm1[16];
  234. UCHAR St2Mm2[16];
  235. UCHAR St3Mm3[16];
  236. UCHAR St4Mm4[16];
  237. UCHAR St5Mm5[16];
  238. UCHAR St6Mm6[16];
  239. UCHAR St7Mm7[16];
  240. UCHAR Xmm0[16];
  241. UCHAR Xmm1[16];
  242. UCHAR Xmm2[16];
  243. UCHAR Xmm3[16];
  244. UCHAR Xmm4[16];
  245. UCHAR Xmm5[16];
  246. UCHAR Xmm6[16];
  247. UCHAR Xmm7[16];
  248. UCHAR Xmm8[16];
  249. UCHAR Xmm9[16];
  250. UCHAR Xmm10[16];
  251. UCHAR Xmm11[16];
  252. UCHAR Xmm12[16];
  253. UCHAR Xmm13[16];
  254. UCHAR Xmm14[16];
  255. UCHAR Xmm15[16];
  256. UCHAR Padding[96];
  257. } PACKED ALIGNED64;
  258. /*++
  259. Structure Description:
  260. This structure outlines a trap frame that will be generated during most
  261. interrupts and exceptions.
  262. Members:
  263. Registers - Stores the current state of the machine's registers. These
  264. values will be restored upon completion of the interrupt or exception.
  265. --*/
  266. struct _TRAP_FRAME {
  267. ULONG Ds;
  268. ULONG Es;
  269. ULONG Fs;
  270. ULONG Gs;
  271. ULONG Ss;
  272. ULONG Eax;
  273. ULONG Ebx;
  274. ULONG Ecx;
  275. ULONG Edx;
  276. ULONG Esi;
  277. ULONG Edi;
  278. ULONG Ebp;
  279. ULONG ErrorCode;
  280. ULONG Eip;
  281. ULONG Cs;
  282. ULONG Eflags;
  283. ULONG Esp;
  284. } PACKED;
  285. /*++
  286. Structure Description:
  287. This structure outlines the register state saved by the kernel when a
  288. user mode signal is dispatched. This generally contains 1) control
  289. registers which are clobbered by switching to the signal handler, and
  290. 2) volatile registers.
  291. Members:
  292. Common - Stores the common signal context information.
  293. TrapFrame - Stores the general register state.
  294. FpuContext - Stores the FPU state.
  295. --*/
  296. typedef struct _SIGNAL_CONTEXT_X86 {
  297. SIGNAL_CONTEXT Common;
  298. TRAP_FRAME TrapFrame;
  299. FPU_CONTEXT FpuContext;
  300. } PACKED SIGNAL_CONTEXT_X86, *PSIGNAL_CONTEXT_X86;
  301. /*++
  302. Structure Description:
  303. This structure contains the state of the processor, including both the
  304. non-volatile general registers and the system registers configured by the
  305. kernel. This structure is used in a manner similar to the C library
  306. setjmp/longjmp routines, the save context function appears to return
  307. twice. It returns once after the saving is complete, and then again with
  308. a different return value after restoring. Be careful when modifying this
  309. structure, as its offsets are used directly in assembly by the save/restore
  310. routines.
  311. Members:
  312. Eax - Stores the value to return when restoring.
  313. Eip - Stores the instruction pointer to jump back to on restore. By default
  314. this is initialized to the return from whoever called save.
  315. Cs - Stores the code segment.
  316. Eflags - Stores the eflags register.
  317. Ebx - Stores a non-volatile general register.
  318. Esi - Stores a non-volatile general register.
  319. Edi - Stores a non-volatile general register.
  320. Ebp - Stores a non-volatile general register.
  321. Esp - Stores the stack pointer. This should be restored after the final
  322. page tables are in place to avoid NMIs having an invalid stack.
  323. Dr7 - Stores a debug register. This should be restored last of the debug
  324. registers.
  325. Dr6 - Stores a debug register.
  326. Dr0 - Stores a debug register.
  327. Dr1 - Stores a debug register.
  328. Dr2 - Stores a debug register.
  329. Dr3 - Stores a debug register.
  330. VirtualAddress - Stores the virtual address of this structure member, which
  331. is used in case the restore of CR0 that just happened enabled paging
  332. suddenly.
  333. Cr0 - Stores the CR0 control register value.
  334. Cr2 - Stores the CR2 control register value (faulting address).
  335. Cr3 - Stores the CR3 control register value (top level page directory).
  336. Cr4 - Stores the CR4 control register value.
  337. Tr - Stores the task register (must be restored after the GDT).
  338. Idt - Stores the interrupt descriptor table. The stack should be restored
  339. before this because once this is restored NMIs could come in and use
  340. stack (rather than the stub function they may currently be on).
  341. Gdt - Stores the global descriptor table.
  342. --*/
  343. struct _PROCESSOR_CONTEXT {
  344. ULONG Eax;
  345. ULONG Eip;
  346. ULONG Cs;
  347. ULONG Eflags;
  348. ULONG Ebx;
  349. ULONG Esi;
  350. ULONG Edi;
  351. ULONG Ebp;
  352. ULONG Esp;
  353. ULONG Dr7;
  354. ULONG Dr6;
  355. ULONG Dr0;
  356. ULONG Dr1;
  357. ULONG Dr2;
  358. ULONG Dr3;
  359. ULONG VirtualAddress;
  360. ULONG Cr0;
  361. ULONG Cr2;
  362. ULONG Cr3;
  363. ULONG Cr4;
  364. ULONG Tr;
  365. TABLE_REGISTER Idt;
  366. TABLE_REGISTER Gdt;
  367. } PACKED;
  368. typedef
  369. VOID
  370. (*PAR_SAVE_RESTORE_FPU_CONTEXT) (
  371. PFPU_CONTEXT Buffer
  372. );
  373. /*++
  374. Routine Description:
  375. This routine saves or restores floating point context from the processor.
  376. Arguments:
  377. Buffer - Supplies a pointer to the buffer where the information will be
  378. saved to or loaded from. This buffer must be 16-byte aligned.
  379. Return Value:
  380. None.
  381. --*/
  382. /*++
  383. Structure Description:
  384. This structure defines the architecture specific form of an address space
  385. structure.
  386. Members:
  387. Common - Stores the common address space information.
  388. PageDirectory - Stores the virtual address of the top level page directory.
  389. PageDirectoryPhysical - Stores the physical address of the top level page
  390. directory.
  391. PageTableCount - Stores the number of page tables that were allocated on
  392. behalf of this process (user mode only).
  393. --*/
  394. typedef struct _ADDRESS_SPACE_X86 {
  395. ADDRESS_SPACE Common;
  396. PPTE PageDirectory;
  397. ULONG PageDirectoryPhysical;
  398. ULONG PageTableCount;
  399. } ADDRESS_SPACE_X86, *PADDRESS_SPACE_X86;
  400. //
  401. // -------------------------------------------------------------------- Globals
  402. //
  403. //
  404. // Store pointers to functions used to save and restore floating point state.
  405. //
  406. extern PAR_SAVE_RESTORE_FPU_CONTEXT ArSaveFpuState;
  407. extern PAR_SAVE_RESTORE_FPU_CONTEXT ArRestoreFpuState;
  408. //
  409. // -------------------------------------------------------- Function Prototypes
  410. //
  411. VOID
  412. ArLoadKernelDataSegments (
  413. VOID
  414. );
  415. /*++
  416. Routine Description:
  417. This routine switches the data segments DS and ES to the kernel data
  418. segment selectors.
  419. Arguments:
  420. None.
  421. Return Value:
  422. None.
  423. --*/
  424. VOID
  425. ArLoadTr (
  426. USHORT TssSegment
  427. );
  428. /*++
  429. Routine Description:
  430. This routine loads a TSS (Task Selector State).
  431. Arguments:
  432. TssSegment - Supplies the segment selector in the GDT that describes the
  433. TSS.
  434. Return Value:
  435. None.
  436. --*/
  437. VOID
  438. ArStoreTr (
  439. PULONG TssSegment
  440. );
  441. /*++
  442. Routine Description:
  443. This routine retrieves the current TSS (Task Selector State) register.
  444. Arguments:
  445. TssSegment - Supplies a pointer where the current TSS segment register will
  446. be returned.
  447. Return Value:
  448. None.
  449. --*/
  450. VOID
  451. ArLoadIdtr (
  452. PVOID IdtBase
  453. );
  454. /*++
  455. Routine Description:
  456. This routine loads the given Interrupt Descriptor Table.
  457. Arguments:
  458. IdtBase - Supplies a pointer to the base of the IDT.
  459. Return Value:
  460. None.
  461. --*/
  462. VOID
  463. ArStoreIdtr (
  464. PTABLE_REGISTER IdtRegister
  465. );
  466. /*++
  467. Routine Description:
  468. This routine stores the interrupt descriptor table register into the given
  469. value.
  470. Arguments:
  471. IdtRegister - Supplies a pointer that will receive the value.
  472. Return Value:
  473. None.
  474. --*/
  475. VOID
  476. ArLoadGdtr (
  477. TABLE_REGISTER Gdt
  478. );
  479. /*++
  480. Routine Description:
  481. This routine loads a global descriptor table.
  482. Arguments:
  483. Gdt - Supplies a pointer to the Gdt pointer, which contains the base and
  484. limit for the GDT.
  485. Return Value:
  486. None.
  487. --*/
  488. VOID
  489. ArStoreGdtr (
  490. PTABLE_REGISTER GdtRegister
  491. );
  492. /*++
  493. Routine Description:
  494. This routine stores the GDT register into the given value.
  495. Arguments:
  496. GdtRegister - Supplies a pointer that will receive the value.
  497. Return Value:
  498. None.
  499. --*/
  500. PVOID
  501. ArGetFaultingAddress (
  502. );
  503. /*++
  504. Routine Description:
  505. This routine determines which address caused a page fault.
  506. Arguments:
  507. None.
  508. Return Value:
  509. Returns the faulting address.
  510. --*/
  511. VOID
  512. ArSetFaultingAddress (
  513. PVOID Value
  514. );
  515. /*++
  516. Routine Description:
  517. This routine sets the CR2 register.
  518. Arguments:
  519. Value - Supplies the value to set.
  520. Return Value:
  521. None.
  522. --*/
  523. UINTN
  524. ArGetCurrentPageDirectory (
  525. VOID
  526. );
  527. /*++
  528. Routine Description:
  529. This routine returns the active page directory.
  530. Arguments:
  531. None.
  532. Return Value:
  533. Returns the page directory currently in use by the system.
  534. --*/
  535. VOID
  536. ArSetCurrentPageDirectory (
  537. ULONG Value
  538. );
  539. /*++
  540. Routine Description:
  541. This routine sets the CR3 register.
  542. Arguments:
  543. Value - Supplies the value to set.
  544. Return Value:
  545. None.
  546. --*/
  547. VOID
  548. ArDoubleFaultHandlerAsm (
  549. );
  550. /*++
  551. Routine Description:
  552. This routine is entered via an IDT entry when a double fault exception
  553. occurs. Double faults are non-recoverable. This machine loops attempting
  554. to enter the debugger indefinitely.
  555. Arguments:
  556. None.
  557. Return Value:
  558. None, this routine does not return.
  559. --*/
  560. VOID
  561. ArProtectionFaultHandlerAsm (
  562. ULONG ReturnEip,
  563. ULONG ReturnCodeSelector,
  564. ULONG ReturnEflags
  565. );
  566. /*++
  567. Routine Description:
  568. This routine is called directly when a general protection fault occurs.
  569. It's job is to prepare the trap frame, call the appropriate handler, and
  570. then restore the trap frame.
  571. Arguments:
  572. ReturnEip - Supplies the address after the instruction that caused the trap.
  573. ReturnCodeSelector - Supplies the code selector the code that trapped was
  574. running under.
  575. ReturnEflags - Supplies the EFLAGS register immediately before the trap.
  576. Return Value:
  577. None.
  578. --*/
  579. VOID
  580. ArMathFaultHandlerAsm (
  581. ULONG ReturnEip,
  582. ULONG ReturnCodeSelector,
  583. ULONG ReturnEflags
  584. );
  585. /*++
  586. Routine Description:
  587. This routine is called directly when a x87 FPU fault occurs.
  588. Arguments:
  589. ReturnEip - Supplies the address after the instruction that caused the trap.
  590. ReturnCodeSelector - Supplies the code selector the code that trapped was
  591. running under.
  592. ReturnEflags - Supplies the EFLAGS register immediately before the trap.
  593. Return Value:
  594. None.
  595. --*/
  596. VOID
  597. ArTrapSystemCallHandlerAsm (
  598. ULONG ReturnEip,
  599. ULONG ReturnCodeSelector,
  600. ULONG ReturnEflags
  601. );
  602. /*++
  603. Routine Description:
  604. This routine is entered when the sysenter routine is entered with the TF
  605. flag set. It performs a normal save and sets the TF.
  606. Arguments:
  607. ReturnEip - Supplies the address after the instruction that caused the trap.
  608. ReturnCodeSelector - Supplies the code selector the code that trapped was
  609. running under.
  610. ReturnEflags - Supplies the EFLAGS register immediately before the trap.
  611. Return Value:
  612. None.
  613. --*/
  614. INTN
  615. ArSystemCallHandlerAsm (
  616. ULONG ReturnEip,
  617. ULONG ReturnCodeSelector,
  618. ULONG ReturnEflags
  619. );
  620. /*++
  621. Routine Description:
  622. This routine is entered via an IDT entry to service a user mode request.
  623. Ecx contains the system call number, and Edx contains the argument.
  624. Arguments:
  625. ReturnEip - Supplies the address after the instruction that caused the trap.
  626. ReturnCodeSelector - Supplies the code selector the code that trapped was
  627. running under.
  628. ReturnEflags - Supplies the EFLAGS register immediately before the trap.
  629. Return Value:
  630. STATUS_SUCCESS or positive integer on success.
  631. Error status code on failure.
  632. --*/
  633. INTN
  634. ArSysenterHandlerAsm (
  635. VOID
  636. );
  637. /*++
  638. Routine Description:
  639. This routine is executed when user mode invokes the SYSENTER instruction.
  640. Upon entry, CS, EIP, and ESP are set to predefined values set in MSRs.
  641. Arguments:
  642. None.
  643. Return Value:
  644. STATUS_SUCCESS or positive integer on success.
  645. Error status code on failure.
  646. --*/
  647. VOID
  648. ArCpuid (
  649. PULONG Eax,
  650. PULONG Ebx,
  651. PULONG Ecx,
  652. PULONG Edx
  653. );
  654. /*++
  655. Routine Description:
  656. This routine executes the CPUID instruction to get processor architecture
  657. information.
  658. Arguments:
  659. Eax - Supplies a pointer to the value that EAX should be set to when the
  660. CPUID instruction is executed. On output, contains the contents of
  661. EAX immediately after the CPUID instruction.
  662. Ebx - Supplies a pointer to the value that EBX should be set to when the
  663. CPUID instruction is executed. On output, contains the contents of
  664. EAX immediately after the CPUID instruction.
  665. Ecx - Supplies a pointer to the value that ECX should be set to when the
  666. CPUID instruction is executed. On output, contains the contents of
  667. EAX immediately after the CPUID instruction.
  668. Edx - Supplies a pointer to the value that EDX should be set to when the
  669. CPUID instruction is executed. On output, contains the contents of
  670. EAX immediately after the CPUID instruction.
  671. Return Value:
  672. None.
  673. --*/
  674. ULONG
  675. ArGetControlRegister0 (
  676. VOID
  677. );
  678. /*++
  679. Routine Description:
  680. This routine returns the current value of CR0.
  681. Arguments:
  682. None.
  683. Return Value:
  684. Returns CR0.
  685. --*/
  686. VOID
  687. ArSetControlRegister0 (
  688. ULONG Value
  689. );
  690. /*++
  691. Routine Description:
  692. This routine sets the CR0 register.
  693. Arguments:
  694. Value - Supplies the value to set.
  695. Return Value:
  696. None.
  697. --*/
  698. ULONG
  699. ArGetControlRegister4 (
  700. VOID
  701. );
  702. /*++
  703. Routine Description:
  704. This routine returns the current value of CR4.
  705. Arguments:
  706. None.
  707. Return Value:
  708. Returns CR4.
  709. --*/
  710. VOID
  711. ArSetControlRegister4 (
  712. ULONG Value
  713. );
  714. /*++
  715. Routine Description:
  716. This routine sets the CR4 register.
  717. Arguments:
  718. Value - Supplies the value to set.
  719. Return Value:
  720. None.
  721. --*/
  722. ULONG
  723. ArGetDebugRegister0 (
  724. VOID
  725. );
  726. /*++
  727. Routine Description:
  728. This routine returns the current value of DR0.
  729. Arguments:
  730. None.
  731. Return Value:
  732. Returns DR0.
  733. --*/
  734. VOID
  735. ArSetDebugRegister0 (
  736. ULONG Value
  737. );
  738. /*++
  739. Routine Description:
  740. This routine sets the DR0 register.
  741. Arguments:
  742. Value - Supplies the value to set.
  743. Return Value:
  744. None.
  745. --*/
  746. ULONG
  747. ArGetDebugRegister1 (
  748. VOID
  749. );
  750. /*++
  751. Routine Description:
  752. This routine returns the current value of DR1.
  753. Arguments:
  754. None.
  755. Return Value:
  756. Returns DR1.
  757. --*/
  758. VOID
  759. ArSetDebugRegister1 (
  760. ULONG Value
  761. );
  762. /*++
  763. Routine Description:
  764. This routine sets the DR1 register.
  765. Arguments:
  766. Value - Supplies the value to set.
  767. Return Value:
  768. None.
  769. --*/
  770. ULONG
  771. ArGetDebugRegister2 (
  772. VOID
  773. );
  774. /*++
  775. Routine Description:
  776. This routine returns the current value of DR2.
  777. Arguments:
  778. None.
  779. Return Value:
  780. Returns DR2.
  781. --*/
  782. VOID
  783. ArSetDebugRegister2 (
  784. ULONG Value
  785. );
  786. /*++
  787. Routine Description:
  788. This routine sets the DR2 register.
  789. Arguments:
  790. Value - Supplies the value to set.
  791. Return Value:
  792. None.
  793. --*/
  794. ULONG
  795. ArGetDebugRegister3 (
  796. VOID
  797. );
  798. /*++
  799. Routine Description:
  800. This routine returns the current value of DR3.
  801. Arguments:
  802. None.
  803. Return Value:
  804. Returns DR3.
  805. --*/
  806. VOID
  807. ArSetDebugRegister3 (
  808. ULONG Value
  809. );
  810. /*++
  811. Routine Description:
  812. This routine sets the DR3 register.
  813. Arguments:
  814. Value - Supplies the value to set.
  815. Return Value:
  816. None.
  817. --*/
  818. ULONG
  819. ArGetDebugRegister6 (
  820. VOID
  821. );
  822. /*++
  823. Routine Description:
  824. This routine returns the current value of DR6.
  825. Arguments:
  826. None.
  827. Return Value:
  828. Returns DR6.
  829. --*/
  830. VOID
  831. ArSetDebugRegister6 (
  832. ULONG Value
  833. );
  834. /*++
  835. Routine Description:
  836. This routine sets the DR6 register.
  837. Arguments:
  838. Value - Supplies the value to set.
  839. Return Value:
  840. None.
  841. --*/
  842. ULONG
  843. ArGetDebugRegister7 (
  844. VOID
  845. );
  846. /*++
  847. Routine Description:
  848. This routine returns the current value of DR7.
  849. Arguments:
  850. None.
  851. Return Value:
  852. Returns DR7.
  853. --*/
  854. VOID
  855. ArSetDebugRegister7 (
  856. ULONG Value
  857. );
  858. /*++
  859. Routine Description:
  860. This routine sets the DR7 register.
  861. Arguments:
  862. Value - Supplies the value to set.
  863. Return Value:
  864. None.
  865. --*/
  866. VOID
  867. ArFxSave (
  868. PFPU_CONTEXT Buffer
  869. );
  870. /*++
  871. Routine Description:
  872. This routine saves the current x87 FPU, MMX, XMM, and MXCSR registers to a
  873. 512 byte memory location.
  874. Arguments:
  875. Buffer - Supplies a pointer to the buffer where the information will be
  876. saved. This buffer must be 16-byte aligned.
  877. Return Value:
  878. None.
  879. --*/
  880. VOID
  881. ArFxRestore (
  882. PFPU_CONTEXT Buffer
  883. );
  884. /*++
  885. Routine Description:
  886. This routine restores the current x87 FPU, MMX, XMM, and MXCSR registers
  887. from a 512 byte memory location.
  888. Arguments:
  889. Buffer - Supplies a pointer to the buffer where the information will be
  890. loaded from. This buffer must be 16-byte aligned.
  891. Return Value:
  892. None.
  893. --*/
  894. VOID
  895. ArSaveX87State (
  896. PFPU_CONTEXT Buffer
  897. );
  898. /*++
  899. Routine Description:
  900. This routine saves the current x87 FPU (floating point unit) state.
  901. Arguments:
  902. Buffer - Supplies a pointer to the buffer where the information will be
  903. saved. This buffer must be 16-byte aligned.
  904. Return Value:
  905. None.
  906. --*/
  907. VOID
  908. ArRestoreX87State (
  909. PFPU_CONTEXT Buffer
  910. );
  911. /*++
  912. Routine Description:
  913. This routine restores the x87 FPU (floating point unit) state.
  914. Arguments:
  915. Buffer - Supplies a pointer to the buffer where the information will be
  916. loaded from. This buffer must be 16-byte aligned.
  917. Return Value:
  918. None.
  919. --*/
  920. VOID
  921. ArEnableFpu (
  922. VOID
  923. );
  924. /*++
  925. Routine Description:
  926. This routine clears the TS bit of CR0, allowing access to the FPU.
  927. Arguments:
  928. None.
  929. Return Value:
  930. None.
  931. --*/
  932. VOID
  933. ArDisableFpu (
  934. VOID
  935. );
  936. /*++
  937. Routine Description:
  938. This routine sets the TS bit of CR0, disallowing access to the FPU.
  939. Arguments:
  940. None.
  941. Return Value:
  942. None.
  943. --*/
  944. VOID
  945. ArInitializeFpu (
  946. VOID
  947. );
  948. /*++
  949. Routine Description:
  950. This routine resets the FPU state.
  951. Arguments:
  952. None.
  953. Return Value:
  954. None.
  955. --*/
  956. ULONGLONG
  957. ArReadTimeStampCounter (
  958. VOID
  959. );
  960. /*++
  961. Routine Description:
  962. This routine reads the time stamp counter from the current processor. It
  963. is essential that callers of this function understand that this returns
  964. instruction cycles, which does not always translate directly into units
  965. of time. For example, some processors halt the timestamp counter during
  966. performance and CPU idle state transitions. In other cases, the timestamp
  967. counters of all processors are not in sync, so as execution of a thread
  968. bounces unpredictably from one core to another, different timelines may be
  969. observed. Additionally, one must understand that this intrinsic is not a
  970. serializing instruction to the hardware, so the processor may decide to
  971. execute any number of instructions after this one before actually snapping
  972. the timestamp counter. To all those who choose to continue to use this
  973. primitive to measure time, you have been warned.
  974. Arguments:
  975. None.
  976. Return Value:
  977. Returns the current instruction cycle count since the processor was started.
  978. --*/
  979. ULONGLONG
  980. ArReadMsr (
  981. ULONG Msr
  982. );
  983. /*++
  984. Routine Description:
  985. This routine reads the requested Model Specific Register.
  986. Arguments:
  987. Msr - Supplies the MSR to read.
  988. Return Value:
  989. Returns the 64-bit MSR value.
  990. --*/
  991. VOID
  992. ArWriteMsr (
  993. ULONG Msr,
  994. ULONGLONG Value
  995. );
  996. /*++
  997. Routine Description:
  998. This routine writes the requested Model Specific Register.
  999. Arguments:
  1000. Msr - Supplies the MSR to write.
  1001. Value - Supplies the 64-bit value to write.
  1002. Return Value:
  1003. None.
  1004. --*/
  1005. VOID
  1006. ArReloadThreadSegment (
  1007. VOID
  1008. );
  1009. /*++
  1010. Routine Description:
  1011. This routine reloads the thread segment register.
  1012. Arguments:
  1013. None.
  1014. Return Value:
  1015. None.
  1016. --*/
  1017. KERNEL_API
  1018. VOID
  1019. ArMonitor (
  1020. PVOID Address,
  1021. UINTN Ecx,
  1022. UINTN Edx
  1023. );
  1024. /*++
  1025. Routine Description:
  1026. This routine arms the monitoring hardware in preparation for an mwait
  1027. instruction.
  1028. Arguments:
  1029. Address - Supplies the address pointer to monitor.
  1030. Ecx - Supplies the contents to load into the ECX (RCX in 64-bit) register
  1031. when executing the monitor instruction. These are defined as hints.
  1032. Edx - Supplies the contents to load into the EDX/RDX register. These are
  1033. also hints.
  1034. Return Value:
  1035. None.
  1036. --*/
  1037. KERNEL_API
  1038. VOID
  1039. ArMwait (
  1040. UINTN Eax,
  1041. UINTN Ecx
  1042. );
  1043. /*++
  1044. Routine Description:
  1045. This routine executes the mwait instruction, which is used to halt the
  1046. processor until a specified memory location is written to. It is also used
  1047. on Intel processors to enter C-states. A monitor instruction must have
  1048. been executed prior to this to set up the monitoring region.
  1049. Arguments:
  1050. Eax - Supplies the contents to load into EAX/RAX when executing the mwait
  1051. instruction. This is a set of hints, including which C-state to enter
  1052. on Intel processors.
  1053. Ecx - Supplies the contents to load into the ECX (RCX in 64-bit) register
  1054. when executing the mwait instruction. This is 1 when entering a C-state
  1055. with interrupts disabled to indicate that an interrupt should still
  1056. break out.
  1057. Return Value:
  1058. None.
  1059. --*/
  1060. KERNEL_API
  1061. VOID
  1062. ArIoReadAndHalt (
  1063. USHORT IoPort
  1064. );
  1065. /*++
  1066. Routine Description:
  1067. This routine performs a single 8-bit I/O port read and then halts the
  1068. processor until the next interrupt comes in. This routine should be called
  1069. with interrupts disabled, and will return with interrupts enabled.
  1070. Arguments:
  1071. IoPort - Supplies the I/O port to read from.
  1072. Return Value:
  1073. None.
  1074. --*/
  1075. VOID
  1076. ArGetKernelTssTrapFrame (
  1077. PTRAP_FRAME TrapFrame
  1078. );
  1079. /*++
  1080. Routine Description:
  1081. This routine converts the kernel TSS to a trap frame.
  1082. Arguments:
  1083. TrapFrame - Supplies a pointer where the filled out trap frame information
  1084. will be returned.
  1085. Return Value:
  1086. None.
  1087. --*/
  1088. VOID
  1089. ArSetKernelTssTrapFrame (
  1090. PTRAP_FRAME TrapFrame
  1091. );
  1092. /*++
  1093. Routine Description:
  1094. This routine converts writes the given trap frame into the kernel TSS.
  1095. Arguments:
  1096. TrapFrame - Supplies a pointer to the trap frame data to write.
  1097. Return Value:
  1098. None.
  1099. --*/
  1100. VOID
  1101. ArClearTssBusyBit (
  1102. USHORT TssSegment
  1103. );
  1104. /*++
  1105. Routine Description:
  1106. This routine clears the busy bit in the GDT for the given segment. It is
  1107. assumed this segment is used on the current processor.
  1108. Arguments:
  1109. TssSegment - Supplies the TSS segment for the busy bit to clear.
  1110. Return Value:
  1111. None.
  1112. --*/
  1113. VOID
  1114. ArpPageFaultHandlerAsm (
  1115. ULONG ReturnEip,
  1116. ULONG ReturnCodeSelector,
  1117. ULONG ReturnEflags
  1118. );
  1119. /*++
  1120. Routine Description:
  1121. This routine is called directly when a page fault occurs.
  1122. Arguments:
  1123. ReturnEip - Supplies the address after the instruction that caused the
  1124. fault.
  1125. ReturnCodeSelector - Supplies the code selector the code that faulted was
  1126. running under.
  1127. ReturnEflags - Supplies the EFLAGS register immediately before the fault.
  1128. Return Value:
  1129. None.
  1130. --*/
  1131. VOID
  1132. ArpCreateSegmentDescriptor (
  1133. PGDT_ENTRY GdtEntry,
  1134. PVOID Base,
  1135. ULONG Limit,
  1136. UCHAR Granularity,
  1137. UCHAR Access
  1138. );
  1139. /*++
  1140. Routine Description:
  1141. This routine initializes a GDT entry given the parameters.
  1142. Arguments:
  1143. GdtEntry - Supplies a pointer to the GDT entry that will be initialized.
  1144. Base - Supplies the base address where this segment begins.
  1145. Limit - Supplies the size of the segment, either in bytes or kilobytes,
  1146. depending on the Granularity parameter.
  1147. Granularity - Supplies the granularity of the segment. Valid values are byte
  1148. granularity or kilobyte granularity.
  1149. Access - Supplies the access permissions on the segment.
  1150. Return Value:
  1151. None.
  1152. --*/