x64.h 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645
  1. /*++
  2. Copyright (c) 2017 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. x64.h
  9. Abstract:
  10. This header contains definitions for aspects of the system that are specific
  11. to the AMD64 architecture.
  12. Author:
  13. Evan Green 26-May-2017
  14. --*/
  15. //
  16. // ------------------------------------------------------------------- Includes
  17. //
  18. #include <minoca/kernel/x86defs.h>
  19. //
  20. // --------------------------------------------------------------------- Macros
  21. //
  22. //
  23. // These macros get the various page table components of an address. The
  24. // levels, from top (highest address bits) to bottom are Page Map Level 4
  25. // (PML4), Page Directory Pointer (PDP), Page Directory (PD), and Page Table
  26. // (PT).
  27. //
  28. #define X64_PML4_INDEX(_VirtualAddress) \
  29. (((UINTN)(_VirtualAddress) >> X64_PML4E_SHIFT) & X64_PT_MASK)
  30. #define X64_PDP_INDEX(_VirtualAddress) \
  31. (((UINTN)(_VirtualAddress) >> X64_PDPE_SHIFT) & X64_PT_MASK)
  32. #define X64_PD_INDEX(_VirtualAddress) \
  33. (((UINTN)(_VirtualAddress) >> X64_PDE_SHIFT) & X64_PT_MASK)
  34. #define X64_PT_INDEX(_VirtualAddress) \
  35. (((UINTN)(_VirtualAddress) >> X64_PTE_SHIFT) & X64_PT_MASK)
  36. //
  37. // ---------------------------------------------------------------- Definitions
  38. //
  39. //
  40. // Define the nesting level of page tables.
  41. //
  42. #define X64_PAGE_LEVEL 4
  43. //
  44. // Define the number of entries in a page table, directory, directory pointer,
  45. // and level 4 table.
  46. //
  47. #define X64_PTE_COUNT 512ULL
  48. //
  49. // Define page address masks.
  50. //
  51. #define X64_PTE_BITS 9
  52. #define X64_PT_MASK 0x1FFULL
  53. #define X64_PTE_SHIFT 12
  54. #define X64_PTE_MASK (X64_PT_MASK << X64_PTE_SHIFT)
  55. #define X64_PDE_SHIFT 21
  56. #define X64_PDE_MASK (X64_PT_MASK << X64_PDE_SHIFT)
  57. #define X64_PDPE_SHIFT 30
  58. #define X64_PDPE_MASK (X64_PT_MASK << X64_PDPE_SHIFT)
  59. #define X64_PML4E_SHIFT 39
  60. #define X64_PML4E_MASK (X64_PT_MASK << X64_PML4E_SHIFT)
  61. //
  62. // Define the fixed self map address. This is set up by the boot loader and
  63. // used directly by the kernel. The advantage is it's a compile-time constant
  64. // to get to page tables. The disadvantage is that VA can't be used by anything
  65. // else. But given that there's no physical memory up there and there's oodles
  66. // of VA space, that seems fine.
  67. //
  68. // Don't use the last index as that would put the PML4T at 0xFFFFFFFFFFFFF000.
  69. // Any kernel underflows from null would hit that page and be awful to debug.
  70. // Use the second to last index.
  71. //
  72. #define X64_SELF_MAP_INDEX (X64_PTE_COUNT - 2)
  73. //
  74. // ------------------------------------------------------ Data Type Definitions
  75. //
  76. typedef ULONGLONG PTE, *PPTE;
  77. typedef
  78. VOID
  79. (*PAR_SAVE_RESTORE_FPU_CONTEXT) (
  80. PFPU_CONTEXT Buffer
  81. );
  82. /*++
  83. Routine Description:
  84. This routine saves or restores floating point context from the processor.
  85. Arguments:
  86. Buffer - Supplies a pointer to the buffer where the information will be
  87. saved to or loaded from. This buffer must be 16-byte aligned.
  88. Return Value:
  89. None.
  90. --*/
  91. /*++
  92. Structure Description:
  93. This structure defines the format of a task, interrupt, or call gate
  94. descriptor. This structure must not be padded, since the hardware relies on
  95. this exact format.
  96. Members:
  97. LowOffset - Stores the lower 16 bits of the gate's destination address.
  98. Selector - Stores the code segment selector the gate code should run in.
  99. Ist - Stores the interrupt stack table index specifying the stack to use
  100. when taking this interrupt or trap. Set to 0 to not switch stacks.
  101. Access - Stores various properties of the gate.
  102. Bit 7: Present. 1 if the gate is present, 0 if not present.
  103. Bits 6-5: DPL. Sets the ring number this handler executes in. Zero is
  104. the most privileged ring, 3 is least privileged.
  105. Bit 4: Reserved (set to 0).
  106. Bits 3-0: The gate type. Set to CALL_GATE_TYPE, INTERRUPT_GATE_TYPE,
  107. TASK_GATE_TYPE, or TRAP_GATE_TYPE.
  108. MidOffset - Stores bits 16-31 of the handler's address.
  109. HighWord - Stores bits 32-63 of the handler's address.
  110. Reserved - Stores a reserved word that should be zero.
  111. --*/
  112. typedef struct _PROCESSOR_GATE {
  113. USHORT LowOffset;
  114. USHORT Selector;
  115. BYTE Ist;
  116. BYTE Access;
  117. USHORT MidOffset;
  118. ULONG HighWord;
  119. ULONG Reserved;
  120. } PACKED PROCESSOR_GATE, *PPROCESSOR_GATE;
  121. /*++
  122. Structure Description:
  123. This structure define a Global Descriptor Table entry. The GDT table sets
  124. up the segmentation features of the processor and privilege levels.
  125. Members:
  126. LimitLow - Stores the lower 16 bits of the descriptor limit.
  127. BaseLow - Stores the lower 16 bits of the descriptor base.
  128. BaseMiddle - Stores the next 8 bits of the base.
  129. Access - Stores the access flags. The access byte has the following format:
  130. | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
  131. | | | | |
  132. | P | DPL | S | Type |
  133. P - Is segment present (1 = Yes)
  134. DPL - Descriptor privilege level: Ring 0-3. Zero is the highest
  135. privilege, 3 is the lowest (least privileged).
  136. S - System flag. Set to 0 if it's a system segment, or 1 if it's a
  137. code/data segment.
  138. Type - Segment type: code segment / data segment. The Type field
  139. has the following definition:
  140. Bit 3 - Set to 1 for Code, or 0 for Data.
  141. Bit 2 - Expansion direction. Set to 0 for expand-up, or 1 for
  142. expand-down.
  143. Bit 1 - Write-Enable. Set to 0 for Read Only, or 1 for
  144. Read/Write.
  145. Bit 0 - Accessed. This bit is set by the processor when memory
  146. in this segment is accessed. It is never cleared by
  147. hardware.
  148. Granularity - Stores the granularity for the descriptor. The granularity
  149. byte has the following format:
  150. | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
  151. | | | | | |
  152. | G | D | L | A | Segment length 19:16 |
  153. G - Granularity. 0 = 1 byte, 1 = 1 KByte.
  154. D - Operand Size. 0 = 16/64 bit, 1 = 32 bit.
  155. L - Long mode (64 bit).
  156. A - Available for system use (always zero).
  157. BaseHigh - Stores the high 8 bits of the base address.
  158. --*/
  159. typedef struct _GDT_ENTRY {
  160. USHORT LimitLow;
  161. USHORT BaseLow;
  162. UCHAR BaseMiddle;
  163. UCHAR Access;
  164. UCHAR Granularity;
  165. UCHAR BaseHigh;
  166. } PACKED GDT_ENTRY, *PGDT_ENTRY;
  167. /*++
  168. Structure Description:
  169. This structure define a 64-bit global descriptor table entry. These are
  170. used by the TSS and LDT types.
  171. Members:
  172. LimitLow - Stores the lower 16 bits of the descriptor limit.
  173. BaseLow - Stores the lower 16 bits of the descriptor base.
  174. BaseMiddle - Stores the next 8 bits of the base.
  175. Access - Stores the access flags. See GATE_ACCESS_* definitions.
  176. Granularity - Stores the granularity bits for the descriptor. See
  177. GDT_GRANULARITY_* definitions.
  178. BaseHigh - Stores bits 24-31 of the base address.
  179. BaseHigh32 - Stores bits 32-63 of the base address.
  180. Reserved - Stores a reserved value that must be zero.
  181. --*/
  182. typedef struct _GDT64_ENTRY {
  183. USHORT LimitLow;
  184. USHORT BaseLow;
  185. UCHAR BaseMiddle;
  186. UCHAR Access;
  187. UCHAR Granularity;
  188. UCHAR BaseHigh;
  189. ULONG BaseHigh32;
  190. ULONG Reserved;
  191. } PACKED GDT64_ENTRY, *PGDT64_ENTRY;
  192. /*++
  193. Structure Description:
  194. This structure defines the format of the GDTR, IDTR, or TR. This structure
  195. must be packed since it represents a hardware construct.
  196. Members:
  197. Limit - Stores the last valid byte of the table, essentially size - 1.
  198. Base - Stores a pointer to the Global Descriptor Table, Interrupt
  199. Descriptor Table, or Task Table.
  200. --*/
  201. typedef struct _TABLE_REGISTER {
  202. USHORT Limit;
  203. ULONGLONG Base;
  204. } PACKED TABLE_REGISTER, *PTABLE_REGISTER;
  205. /*++
  206. Structure Description:
  207. This structure defines the x86 Task State Segment. It represents a complete
  208. task state as understood by the hardware.
  209. Members:
  210. Rsp0-2 - Stores the stack pointer to load for each of the privilege levels.
  211. Ist - Stores the interrupt stack values for RSP, indices 0-7. Technically
  212. in the spec Ist[0] is marked as reserved, since IST index zero means
  213. "no stack switching".
  214. IoMapBase - Stores the 16 bit offset from the TSS base to the 8192 byte I/O
  215. Bitmap.
  216. --*/
  217. typedef struct _TSS64 {
  218. ULONG Reserved0;
  219. ULONGLONG Rsp[3];
  220. ULONGLONG Ist[8];
  221. ULONGLONG Reserved2;
  222. USHORT Reserved3;
  223. USHORT IoMapBase;
  224. } PACKED TSS64, *PTSS64;
  225. /*++
  226. Structure Description:
  227. This structure defines the extended state of the x86 architecture. This
  228. structure is architecturally defined by the FXSAVE and FXRSTOR instructions.
  229. Members:
  230. Registers - Stores the extended processor state.
  231. --*/
  232. struct _FPU_CONTEXT {
  233. USHORT Fcw;
  234. USHORT Fsw;
  235. USHORT Ftw;
  236. USHORT Fop;
  237. ULONG FpuIp;
  238. USHORT Cs;
  239. USHORT Reserved1;
  240. ULONG FpuDp;
  241. USHORT Ds;
  242. USHORT Reserved2;
  243. ULONG Mxcsr;
  244. ULONG MxcsrMask;
  245. UCHAR St0Mm0[16];
  246. UCHAR St1Mm1[16];
  247. UCHAR St2Mm2[16];
  248. UCHAR St3Mm3[16];
  249. UCHAR St4Mm4[16];
  250. UCHAR St5Mm5[16];
  251. UCHAR St6Mm6[16];
  252. UCHAR St7Mm7[16];
  253. UCHAR Xmm0[16];
  254. UCHAR Xmm1[16];
  255. UCHAR Xmm2[16];
  256. UCHAR Xmm3[16];
  257. UCHAR Xmm4[16];
  258. UCHAR Xmm5[16];
  259. UCHAR Xmm6[16];
  260. UCHAR Xmm7[16];
  261. UCHAR Xmm8[16];
  262. UCHAR Xmm9[16];
  263. UCHAR Xmm10[16];
  264. UCHAR Xmm11[16];
  265. UCHAR Xmm12[16];
  266. UCHAR Xmm13[16];
  267. UCHAR Xmm14[16];
  268. UCHAR Xmm15[16];
  269. UCHAR Padding[96];
  270. } PACKED ALIGNED64;
  271. /*++
  272. Structure Description:
  273. This structure outlines a trap frame that will be generated during most
  274. interrupts and exceptions.
  275. Members:
  276. Registers - Stores the current state of the machine's registers. These
  277. values will be restored upon completion of the interrupt or exception.
  278. --*/
  279. struct _TRAP_FRAME {
  280. ULONG Ds;
  281. ULONG Es;
  282. ULONG Fs;
  283. ULONG Gs;
  284. ULONGLONG Padding;
  285. ULONGLONG Rax;
  286. ULONGLONG Rbx;
  287. ULONGLONG Rcx;
  288. ULONGLONG Rdx;
  289. ULONGLONG Rsi;
  290. ULONGLONG Rdi;
  291. ULONGLONG Rbp;
  292. ULONGLONG R8;
  293. ULONGLONG R9;
  294. ULONGLONG R10;
  295. ULONGLONG R11;
  296. ULONGLONG R12;
  297. ULONGLONG R13;
  298. ULONGLONG R14;
  299. ULONGLONG R15;
  300. ULONGLONG ErrorCode;
  301. ULONGLONG Rip;
  302. ULONGLONG Cs;
  303. ULONGLONG Rflags;
  304. ULONGLONG Rsp;
  305. ULONGLONG Ss;
  306. };
  307. /*++
  308. Structure Description:
  309. This structure outlines the register state saved by the kernel when a
  310. user mode signal is dispatched. This generally contains 1) control
  311. registers which are clobbered by switching to the signal handler, and
  312. 2) volatile registers.
  313. Members:
  314. Common - Stores the common signal context information.
  315. TrapFrame - Stores the general register state.
  316. FpuContext - Stores the FPU state.
  317. --*/
  318. typedef struct _SIGNAL_CONTEXT_X64 {
  319. SIGNAL_CONTEXT Common;
  320. TRAP_FRAME TrapFrame;
  321. FPU_CONTEXT FpuContext;
  322. } SIGNAL_CONTEXT_X64, *PSIGNAL_CONTEXT_X64;
  323. /*++
  324. Structure Description:
  325. This structure contains the state of the processor, including both the
  326. non-volatile general registers and the system registers configured by the
  327. kernel. This structure is used in a manner similar to the C library
  328. setjmp/longjmp routines, the save context function appears to return
  329. twice. It returns once after the saving is complete, and then again with
  330. a different return value after restoring. Be careful when modifying this
  331. structure, as its offsets are used directly in assembly by the save/restore
  332. routines.
  333. Members:
  334. Rax - Stores the value to return when restoring.
  335. Rip - Stores the instruction pointer to jump back to on restore. By default
  336. this is initialized to the return from whoever called save.
  337. Cs - Stores the code segment.
  338. Rflags - Stores the eflags register.
  339. Rdi - Stores an argument register.
  340. Rsi - Stores an argument register.
  341. Rdx - Stores an argument register.
  342. Rcx - Stores an argument register.
  343. R8 - Stores an argument register.
  344. R9 - Stores an argument register.
  345. Rbx - Stores a non-volatile general register.
  346. Rbp - Stores a non-volatile general register.
  347. Rsp - Stores the stack pointer. This should be restored after the final
  348. page tables are in place to avoid NMIs having an invalid stack.
  349. R12 - Stores a non-volatile general register.
  350. R13 - Stores a non-volatile general register.
  351. R14 - Stores a non-volatile general register.
  352. R15 - Stores a non-volatile general register.
  353. Dr7 - Stores a debug register. This should be restored last of the debug
  354. registers.
  355. Dr6 - Stores a debug register.
  356. Dr0 - Stores a debug register.
  357. Dr1 - Stores a debug register.
  358. Dr2 - Stores a debug register.
  359. Dr3 - Stores a debug register.
  360. VirtualAddress - Stores the virtual address of this structure member, which
  361. is used in case the restore of CR0 that just happened enabled paging
  362. suddenly.
  363. Cr0 - Stores the CR0 control register value.
  364. Cr2 - Stores the CR2 control register value (faulting address).
  365. Cr3 - Stores the CR3 control register value (top level page directory).
  366. Cr4 - Stores the CR4 control register value.
  367. Fsbase - Stores the FS: base address.
  368. Gsbase - Stores the GS: base address.
  369. KernelGsbase - Stores the kernel GS: base MSR.
  370. Efer - Stores the EFER register.
  371. Tr - Stores the task register (must be restored after the GDT).
  372. Idt - Stores the interrupt descriptor table. The task register and GDT
  373. should be restored before this so that the ISTs are set up if an NMI
  374. comes in.
  375. Gdt - Stores the global descriptor table.
  376. --*/
  377. struct _PROCESSOR_CONTEXT {
  378. UINTN Rax;
  379. UINTN Rip;
  380. UINTN Cs;
  381. UINTN Rflags;
  382. UINTN Rdi;
  383. UINTN Rsi;
  384. UINTN Rdx;
  385. UINTN Rcx;
  386. UINTN R8;
  387. UINTN R9;
  388. UINTN Rbx;
  389. UINTN Rbp;
  390. UINTN Rsp;
  391. UINTN R12;
  392. UINTN R13;
  393. UINTN R14;
  394. UINTN R15;
  395. UINTN Dr7;
  396. UINTN Dr6;
  397. UINTN Dr0;
  398. UINTN Dr1;
  399. UINTN Dr2;
  400. UINTN Dr3;
  401. UINTN VirtualAddress;
  402. UINTN Cr0;
  403. UINTN Cr2;
  404. UINTN Cr3;
  405. UINTN Cr4;
  406. UINTN Fsbase;
  407. UINTN Gsbase;
  408. UINTN KernelGsbase;
  409. UINTN Efer;
  410. UINTN Tr;
  411. TABLE_REGISTER Idt;
  412. TABLE_REGISTER Gdt;
  413. };
  414. /*++
  415. Structure Description:
  416. This structure defines the architecture specific form of an address space
  417. structure.
  418. Members:
  419. Common - Stores the common address space information.
  420. Pml4Physical - Stores the physical address of the top level page
  421. directory.
  422. AllocatedPageTables - Stores the total number of active and inactive page
  423. table pages allocated on behalf of user mode for this process.
  424. ActivePageTables - Stores the number of page table pages that are in
  425. service for user mode of this process.
  426. --*/
  427. typedef struct _ADDRESS_SPACE_X64 {
  428. ADDRESS_SPACE Common;
  429. PHYSICAL_ADDRESS Pml4Physical;
  430. UINTN AllocatedPageTables;
  431. UINTN ActivePageTables;
  432. } ADDRESS_SPACE_X64, *PADDRESS_SPACE_X64;
  433. //
  434. // -------------------------------------------------------------------- Globals
  435. //
  436. //
  437. // Store pointers to functions used to save and restore floating point state.
  438. //
  439. extern PAR_SAVE_RESTORE_FPU_CONTEXT ArSaveFpuState;
  440. extern PAR_SAVE_RESTORE_FPU_CONTEXT ArRestoreFpuState;
  441. //
  442. // -------------------------------------------------------- Function Prototypes
  443. //
  444. INTN
  445. ArSyscallHandlerAsm (
  446. VOID
  447. );
  448. /*++
  449. Routine Description:
  450. This routine is executed when user mode invokes the SYSCALL instruction.
  451. Upon entry, CS, SS, RIP, and RFLAGS are set to predefined values. Execution
  452. is still running on the user mode stack.
  453. Arguments:
  454. None.
  455. Return Value:
  456. STATUS_SUCCESS or positive integer on success.
  457. Error status code on failure.
  458. --*/
  459. VOID
  460. ArLoadKernelDataSegments (
  461. VOID
  462. );
  463. /*++
  464. Routine Description:
  465. This routine switches the data segments DS and ES to the kernel data
  466. segment selectors.
  467. Arguments:
  468. None.
  469. Return Value:
  470. None.
  471. --*/
  472. VOID
  473. ArLoadTr (
  474. USHORT TssSegment
  475. );
  476. /*++
  477. Routine Description:
  478. This routine loads a TSS (Task Selector State).
  479. Arguments:
  480. TssSegment - Supplies the segment selector in the GDT that describes the
  481. TSS.
  482. Return Value:
  483. None.
  484. --*/
  485. VOID
  486. ArStoreTr (
  487. PULONG TssSegment
  488. );
  489. /*++
  490. Routine Description:
  491. This routine retrieves the current TSS (Task Selector State) register.
  492. Arguments:
  493. TssSegment - Supplies a pointer where the current TSS segment register will
  494. be returned.
  495. Return Value:
  496. None.
  497. --*/
  498. VOID
  499. ArLoadIdtr (
  500. PVOID IdtBase
  501. );
  502. /*++
  503. Routine Description:
  504. This routine loads the given Interrupt Descriptor Table.
  505. Arguments:
  506. IdtBase - Supplies a pointer to the base of the IDT.
  507. Return Value:
  508. None.
  509. --*/
  510. VOID
  511. ArStoreIdtr (
  512. PTABLE_REGISTER IdtRegister
  513. );
  514. /*++
  515. Routine Description:
  516. This routine stores the interrupt descriptor table register into the given
  517. value.
  518. Arguments:
  519. IdtRegister - Supplies a pointer that will receive the value.
  520. Return Value:
  521. None.
  522. --*/
  523. VOID
  524. ArLoadGdtr (
  525. PTABLE_REGISTER Gdt
  526. );
  527. /*++
  528. Routine Description:
  529. This routine loads a global descriptor table.
  530. Arguments:
  531. Gdt - Supplies a pointer to the Gdt pointer, which contains the base and
  532. limit for the GDT.
  533. Return Value:
  534. None.
  535. --*/
  536. VOID
  537. ArStoreGdtr (
  538. PTABLE_REGISTER GdtRegister
  539. );
  540. /*++
  541. Routine Description:
  542. This routine stores the GDT register into the given value.
  543. Arguments:
  544. GdtRegister - Supplies a pointer that will receive the value.
  545. Return Value:
  546. None.
  547. --*/
  548. PVOID
  549. ArGetFaultingAddress (
  550. VOID
  551. );
  552. /*++
  553. Routine Description:
  554. This routine determines which address caused a page fault.
  555. Arguments:
  556. None.
  557. Return Value:
  558. Returns the faulting address.
  559. --*/
  560. VOID
  561. ArSetFaultingAddress (
  562. PVOID Value
  563. );
  564. /*++
  565. Routine Description:
  566. This routine sets the CR2 register.
  567. Arguments:
  568. Value - Supplies the value to set.
  569. Return Value:
  570. None.
  571. --*/
  572. UINTN
  573. ArGetCurrentPageDirectory (
  574. VOID
  575. );
  576. /*++
  577. Routine Description:
  578. This routine returns the active page directory.
  579. Arguments:
  580. None.
  581. Return Value:
  582. Returns the page directory currently in use by the system.
  583. --*/
  584. VOID
  585. ArSetCurrentPageDirectory (
  586. UINTN Value
  587. );
  588. /*++
  589. Routine Description:
  590. This routine sets the CR3 register.
  591. Arguments:
  592. Value - Supplies the value to set.
  593. Return Value:
  594. None.
  595. --*/
  596. VOID
  597. ArCpuid (
  598. PULONG Eax,
  599. PULONG Ebx,
  600. PULONG Ecx,
  601. PULONG Edx
  602. );
  603. /*++
  604. Routine Description:
  605. This routine executes the CPUID instruction to get processor architecture
  606. information.
  607. Arguments:
  608. Eax - Supplies a pointer to the value that EAX should be set to when the
  609. CPUID instruction is executed. On output, contains the contents of
  610. EAX immediately after the CPUID instruction.
  611. Ebx - Supplies a pointer to the value that EBX should be set to when the
  612. CPUID instruction is executed. On output, contains the contents of
  613. EAX immediately after the CPUID instruction.
  614. Ecx - Supplies a pointer to the value that ECX should be set to when the
  615. CPUID instruction is executed. On output, contains the contents of
  616. EAX immediately after the CPUID instruction.
  617. Edx - Supplies a pointer to the value that EDX should be set to when the
  618. CPUID instruction is executed. On output, contains the contents of
  619. EAX immediately after the CPUID instruction.
  620. Return Value:
  621. None.
  622. --*/
  623. UINTN
  624. ArGetControlRegister0 (
  625. VOID
  626. );
  627. /*++
  628. Routine Description:
  629. This routine returns the current value of CR0.
  630. Arguments:
  631. None.
  632. Return Value:
  633. Returns CR0.
  634. --*/
  635. VOID
  636. ArSetControlRegister0 (
  637. UINTN Value
  638. );
  639. /*++
  640. Routine Description:
  641. This routine sets the CR0 register.
  642. Arguments:
  643. Value - Supplies the value to set.
  644. Return Value:
  645. None.
  646. --*/
  647. UINTN
  648. ArGetControlRegister4 (
  649. VOID
  650. );
  651. /*++
  652. Routine Description:
  653. This routine returns the current value of CR4.
  654. Arguments:
  655. None.
  656. Return Value:
  657. Returns CR4.
  658. --*/
  659. VOID
  660. ArSetControlRegister4 (
  661. UINTN Value
  662. );
  663. /*++
  664. Routine Description:
  665. This routine sets the CR4 register.
  666. Arguments:
  667. Value - Supplies the value to set.
  668. Return Value:
  669. None.
  670. --*/
  671. UINTN
  672. ArGetDebugRegister0 (
  673. VOID
  674. );
  675. /*++
  676. Routine Description:
  677. This routine returns the current value of DR0.
  678. Arguments:
  679. None.
  680. Return Value:
  681. Returns DR0.
  682. --*/
  683. VOID
  684. ArSetDebugRegister0 (
  685. UINTN Value
  686. );
  687. /*++
  688. Routine Description:
  689. This routine sets the DR0 register.
  690. Arguments:
  691. Value - Supplies the value to set.
  692. Return Value:
  693. None.
  694. --*/
  695. UINTN
  696. ArGetDebugRegister1 (
  697. VOID
  698. );
  699. /*++
  700. Routine Description:
  701. This routine returns the current value of DR1.
  702. Arguments:
  703. None.
  704. Return Value:
  705. Returns DR1.
  706. --*/
  707. VOID
  708. ArSetDebugRegister1 (
  709. UINTN Value
  710. );
  711. /*++
  712. Routine Description:
  713. This routine sets the DR1 register.
  714. Arguments:
  715. Value - Supplies the value to set.
  716. Return Value:
  717. None.
  718. --*/
  719. UINTN
  720. ArGetDebugRegister2 (
  721. VOID
  722. );
  723. /*++
  724. Routine Description:
  725. This routine returns the current value of DR2.
  726. Arguments:
  727. None.
  728. Return Value:
  729. Returns DR2.
  730. --*/
  731. VOID
  732. ArSetDebugRegister2 (
  733. UINTN Value
  734. );
  735. /*++
  736. Routine Description:
  737. This routine sets the DR2 register.
  738. Arguments:
  739. Value - Supplies the value to set.
  740. Return Value:
  741. None.
  742. --*/
  743. UINTN
  744. ArGetDebugRegister3 (
  745. VOID
  746. );
  747. /*++
  748. Routine Description:
  749. This routine returns the current value of DR3.
  750. Arguments:
  751. None.
  752. Return Value:
  753. Returns DR3.
  754. --*/
  755. VOID
  756. ArSetDebugRegister3 (
  757. UINTN Value
  758. );
  759. /*++
  760. Routine Description:
  761. This routine sets the DR3 register.
  762. Arguments:
  763. Value - Supplies the value to set.
  764. Return Value:
  765. None.
  766. --*/
  767. UINTN
  768. ArGetDebugRegister6 (
  769. VOID
  770. );
  771. /*++
  772. Routine Description:
  773. This routine returns the current value of DR6.
  774. Arguments:
  775. None.
  776. Return Value:
  777. Returns DR6.
  778. --*/
  779. VOID
  780. ArSetDebugRegister6 (
  781. UINTN Value
  782. );
  783. /*++
  784. Routine Description:
  785. This routine sets the DR6 register.
  786. Arguments:
  787. Value - Supplies the value to set.
  788. Return Value:
  789. None.
  790. --*/
  791. UINTN
  792. ArGetDebugRegister7 (
  793. VOID
  794. );
  795. /*++
  796. Routine Description:
  797. This routine returns the current value of DR7.
  798. Arguments:
  799. None.
  800. Return Value:
  801. Returns DR7.
  802. --*/
  803. VOID
  804. ArSetDebugRegister7 (
  805. UINTN Value
  806. );
  807. /*++
  808. Routine Description:
  809. This routine sets the DR7 register.
  810. Arguments:
  811. Value - Supplies the value to set.
  812. Return Value:
  813. None.
  814. --*/
  815. VOID
  816. ArFxSave (
  817. PFPU_CONTEXT Buffer
  818. );
  819. /*++
  820. Routine Description:
  821. This routine saves the current x87 FPU, MMX, XMM, and MXCSR registers to a
  822. 512 byte memory location.
  823. Arguments:
  824. Buffer - Supplies a pointer to the buffer where the information will be
  825. saved. This buffer must be 16-byte aligned.
  826. Return Value:
  827. None.
  828. --*/
  829. VOID
  830. ArFxRestore (
  831. PFPU_CONTEXT Buffer
  832. );
  833. /*++
  834. Routine Description:
  835. This routine restores the current x87 FPU, MMX, XMM, and MXCSR registers
  836. from a 512 byte memory location.
  837. Arguments:
  838. Buffer - Supplies a pointer to the buffer where the information will be
  839. loaded from. This buffer must be 16-byte aligned.
  840. Return Value:
  841. None.
  842. --*/
  843. VOID
  844. ArEnableFpu (
  845. VOID
  846. );
  847. /*++
  848. Routine Description:
  849. This routine clears the TS bit of CR0, allowing access to the FPU.
  850. Arguments:
  851. None.
  852. Return Value:
  853. None.
  854. --*/
  855. VOID
  856. ArDisableFpu (
  857. VOID
  858. );
  859. /*++
  860. Routine Description:
  861. This routine sets the TS bit of CR0, disallowing access to the FPU.
  862. Arguments:
  863. None.
  864. Return Value:
  865. None.
  866. --*/
  867. VOID
  868. ArInitializeFpu (
  869. VOID
  870. );
  871. /*++
  872. Routine Description:
  873. This routine resets the FPU state.
  874. Arguments:
  875. None.
  876. Return Value:
  877. None.
  878. --*/
  879. ULONGLONG
  880. ArReadTimeStampCounter (
  881. VOID
  882. );
  883. /*++
  884. Routine Description:
  885. This routine reads the time stamp counter from the current processor. It
  886. is essential that callers of this function understand that this returns
  887. instruction cycles, which does not always translate directly into units
  888. of time. For example, some processors halt the timestamp counter during
  889. performance and CPU idle state transitions. In other cases, the timestamp
  890. counters of all processors are not in sync, so as execution of a thread
  891. bounces unpredictably from one core to another, different timelines may be
  892. observed. Additionally, one must understand that this intrinsic is not a
  893. serializing instruction to the hardware, so the processor may decide to
  894. execute any number of instructions after this one before actually snapping
  895. the timestamp counter. To all those who choose to continue to use this
  896. primitive to measure time, you have been warned.
  897. Arguments:
  898. None.
  899. Return Value:
  900. Returns the current instruction cycle count since the processor was started.
  901. --*/
  902. ULONGLONG
  903. ArReadMsr (
  904. ULONG Msr
  905. );
  906. /*++
  907. Routine Description:
  908. This routine reads the requested Model Specific Register.
  909. Arguments:
  910. Msr - Supplies the MSR to read.
  911. Return Value:
  912. Returns the 64-bit MSR value.
  913. --*/
  914. VOID
  915. ArWriteMsr (
  916. ULONGLONG Msr,
  917. ULONGLONG Value
  918. );
  919. /*++
  920. Routine Description:
  921. This routine writes the requested Model Specific Register.
  922. Arguments:
  923. Msr - Supplies the MSR to write.
  924. Value - Supplies the 64-bit value to write.
  925. Return Value:
  926. None.
  927. --*/
  928. PVOID
  929. ArReadFsbase (
  930. VOID
  931. );
  932. /*++
  933. Routine Description:
  934. This routine reads the fs: base register.
  935. Arguments:
  936. None.
  937. Return Value:
  938. Returns the fsbase pointer.
  939. --*/
  940. VOID
  941. ArWriteFsbase (
  942. PVOID Fsbase
  943. );
  944. /*++
  945. Routine Description:
  946. This routine writes the fs: base register.
  947. Arguments:
  948. Fsbase - Supplies the new fsbase value to write.
  949. Return Value:
  950. None.
  951. --*/
  952. PVOID
  953. ArReadGsbase (
  954. VOID
  955. );
  956. /*++
  957. Routine Description:
  958. This routine reads the gs: base register.
  959. Arguments:
  960. None.
  961. Return Value:
  962. Returns the gsbase pointer.
  963. --*/
  964. VOID
  965. ArWriteGsbase (
  966. PVOID Gsbase
  967. );
  968. /*++
  969. Routine Description:
  970. This routine writes the gs: base register.
  971. Arguments:
  972. Gsbase - Supplies the new gsbase value to write.
  973. Return Value:
  974. None.
  975. --*/
  976. VOID
  977. ArSwapGs (
  978. VOID
  979. );
  980. /*++
  981. Routine Description:
  982. This routine exchanges the GS base hidden register with the kernel GS base
  983. MSR.
  984. Arguments:
  985. None.
  986. Return Value:
  987. None.
  988. --*/
  989. KERNEL_API
  990. VOID
  991. ArMonitor (
  992. PVOID Address,
  993. UINTN Rcx,
  994. UINTN Rdx
  995. );
  996. /*++
  997. Routine Description:
  998. This routine arms the monitoring hardware in preparation for an mwait
  999. instruction.
  1000. Arguments:
  1001. Address - Supplies the address pointer to monitor.
  1002. Rcx - Supplies the contents to load into the RCX register when executing
  1003. the monitor instruction. These are defined as hints.
  1004. Rdx - Supplies the contents to load into the RDX register. These are also
  1005. hints.
  1006. Return Value:
  1007. None.
  1008. --*/
  1009. KERNEL_API
  1010. VOID
  1011. ArMwait (
  1012. UINTN Rax,
  1013. UINTN Rcx
  1014. );
  1015. /*++
  1016. Routine Description:
  1017. This routine executes the mwait instruction, which is used to halt the
  1018. processor until a specified memory location is written to. It is also used
  1019. on Intel processors to enter C-states. A monitor instruction must have
  1020. been executed prior to this to set up the monitoring region.
  1021. Arguments:
  1022. Rax - Supplies the contents to load into RAX when executing the mwait
  1023. instruction. This is a set of hints, including which C-state to enter
  1024. on Intel processors.
  1025. Rcx - Supplies the contents to load into the RCX register when executing
  1026. the mwait instruction. This is 1 when entering a C-state with
  1027. interrupts disabled to indicate that an interrupt should still break
  1028. out.
  1029. Return Value:
  1030. None.
  1031. --*/
  1032. KERNEL_API
  1033. VOID
  1034. ArIoReadAndHalt (
  1035. USHORT IoPort
  1036. );
  1037. /*++
  1038. Routine Description:
  1039. This routine performs a single 8-bit I/O port read and then halts the
  1040. processor until the next interrupt comes in. This routine should be called
  1041. with interrupts disabled, and will return with interrupts enabled.
  1042. Arguments:
  1043. IoPort - Supplies the I/O port to read from.
  1044. Return Value:
  1045. None.
  1046. --*/