Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

493 lines
11 KiB

  1. /*++
  2. Copyright (c) 1991 Microsoft Corporation
  3. Module Name:
  4. xxbiosc.c
  5. Abstract:
  6. This module implements the protect-mode routines necessary to make the
  7. transition to real mode and return to protected mode.
  8. Author:
  9. John Vert (jvert) 29-Oct-1991
  10. Environment:
  11. Kernel mode only.
  12. Probably a panic-stop, so we cannot use any system services.
  13. Revision History:
  14. --*/
  15. #include "halp.h"
  16. #ifdef ALLOC_PRAGMA
  17. #pragma alloc_text(PAGE, HalpGetDisplayBiosInformation)
  18. #endif // ALLOC_PRAGMA
  19. //
  20. // The IOPM should be mostly 0xff. However it is possible a few
  21. // bits may be cleared. Build a table of what's not 0xff.
  22. //
  23. #define MAX_DIFFERENCES 0x20
  24. typedef struct _IOPM_DIFF_ENTRY
  25. {
  26. USHORT Entry;
  27. USHORT Value;
  28. } IOPM_DIFF_ENTRY, *PIOPM_DIFF_ENTRY;
  29. //
  30. // Function definitions
  31. //
  32. ULONG
  33. HalpBorrowTss(
  34. VOID
  35. );
  36. VOID
  37. HalpReturnTss(
  38. ULONG TssSelector
  39. );
  40. VOID
  41. HalpBiosCall(
  42. VOID
  43. );
  44. VOID
  45. HalpTrap06(
  46. VOID
  47. );
  48. VOID
  49. HalpTrap0D(
  50. VOID
  51. );
  52. ULONG
  53. HalpStoreAndClearIopm(
  54. PVOID Iopm,
  55. PIOPM_DIFF_ENTRY IopmDiffTable,
  56. ULONG MaxIopmTableEntries
  57. )
  58. /*++
  59. Routine Description:
  60. The primary function of this routine is to clear all the bits in the
  61. IOPM. However, we will need to recover any of our changes later.
  62. It is very likely that the IOPM will be all 0xff's. If there are
  63. deviations from this, they should be minimal. So lets only store what's
  64. different.
  65. Arguments:
  66. Iopm - Pointer to the IOPM to clear.
  67. IopmDiffTable - Pointer to the table of IOPM deviations from 0xff.
  68. MaxIopmTableEntries - The maximum number of entries in our table.
  69. Returns:
  70. Number of entries added to the table.
  71. --*/
  72. {
  73. PUSHORT IoMap = Iopm;
  74. ULONG IopmDiffTableEntries = 0;
  75. ULONG i;
  76. for (i=0; i<(IOPM_SIZE / 2); i++) {
  77. if (*IoMap != 0xffff) {
  78. if (IopmDiffTableEntries < MaxIopmTableEntries) {
  79. IopmDiffTable[IopmDiffTableEntries].Entry = (USHORT) i;
  80. IopmDiffTable[IopmDiffTableEntries].Value = *IoMap;
  81. IopmDiffTableEntries++;
  82. } else {
  83. ASSERT(IopmDiffTableEntries < MaxIopmTableEntries);
  84. }
  85. }
  86. *IoMap++ = 0;
  87. }
  88. //
  89. // The end of the IOPM table must be followed by a string of FF's.
  90. //
  91. while (i < (PIOPM_SIZE / 2)) {
  92. *IoMap++ = 0xffff;
  93. i++;
  94. }
  95. return IopmDiffTableEntries;
  96. }
  97. VOID
  98. HalpRestoreIopm(
  99. PVOID Iopm,
  100. PIOPM_DIFF_ENTRY IopmDiffTable,
  101. ULONG IopmTableEntries
  102. )
  103. /*++
  104. Routine Description:
  105. We expect that most IOPM's will be all FF's. So we'll reset to that
  106. state, and then we'll apply any changes from our differences table.
  107. Arguments:
  108. Iopm - Pointer to the IOPM to restore.
  109. IopmDiffTable - Pointer to the table of IOPM deviations from 0xff.
  110. IopmTableEntries - The number of entries in our table.
  111. Returns:
  112. none
  113. --*/
  114. {
  115. PUSHORT IoMap = Iopm;
  116. memset(Iopm, 0xff, PIOPM_SIZE);
  117. while (IopmTableEntries--) {
  118. IoMap[IopmDiffTable[IopmTableEntries].Entry] =
  119. IopmDiffTable[IopmTableEntries].Value;
  120. }
  121. }
  122. BOOLEAN
  123. HalpBiosDisplayReset(
  124. VOID
  125. )
  126. /*++
  127. Routine Description:
  128. Calls BIOS by putting the machine into V86 mode. This involves setting up
  129. a physical==virtual identity mapping for the first 1Mb of memory, setting
  130. up V86-specific trap handlers, and granting I/O privilege to the V86
  131. process by editing the IOPM bitmap in the TSS.
  132. Environment:
  133. Interrupts disabled.
  134. Arguments:
  135. None
  136. Return Value:
  137. Always returns TRUE
  138. --*/
  139. {
  140. HARDWARE_PTE OldPageTable;
  141. HARDWARE_PTE_X86PAE OldPageTablePae;
  142. ULONGLONG OldPageTablePfn;
  143. USHORT OldIoMapBase;
  144. ULONG OldEsp0;
  145. PHARDWARE_PTE Pte;
  146. PHARDWARE_PTE V86CodePte;
  147. ULONG OldTrap0DHandler;
  148. ULONG OldTrap06Handler;
  149. PUCHAR IoMap;
  150. ULONG Virtual;
  151. KIRQL OldIrql;
  152. ULONG OriginalTssSelector;
  153. extern PVOID HalpRealModeStart;
  154. extern PVOID HalpRealModeEnd;
  155. extern volatile ULONG HalpNMIInProgress;
  156. PHARDWARE_PTE PointerPde;
  157. PHARDWARE_PTE IdtPte;
  158. ULONG OldIdtWrite;
  159. ULONG PageFrame;
  160. ULONG PageFrameEnd;
  161. PKPCR Pcr;
  162. IOPM_DIFF_ENTRY IopmDiffTable[MAX_DIFFERENCES];
  163. ULONG IopmDiffTableEntries;
  164. //
  165. // Interrupts are off, but V86 mode might turn them back on again.
  166. //
  167. OldIrql = HalpDisableAllInterrupts ();
  168. Pcr = KeGetPcr();
  169. //
  170. // We need to set up an identity mapping in the first page table. First,
  171. // we save away the old page table.
  172. //
  173. PointerPde = MiGetPdeAddress((PVOID)0);
  174. OldPageTablePfn = HalpGetPageFrameNumber( PointerPde );
  175. if (HalPaeEnabled() != FALSE) {
  176. OldPageTablePae = *(PHARDWARE_PTE_X86PAE)PointerPde;
  177. ((PHARDWARE_PTE_X86PAE)PointerPde)->reserved1 = 0;
  178. } else {
  179. OldPageTable = *PointerPde;
  180. }
  181. //
  182. // Now we put the HAL page table into the first slot of the page
  183. // directory. Note that this page table is now the first and last
  184. // entries in the page directory.
  185. //
  186. Pte = MiGetPdeAddress((PVOID)0);
  187. HalpCopyPageFrameNumber( Pte,
  188. MiGetPdeAddress( MM_HAL_RESERVED ));
  189. Pte->Valid = 1;
  190. Pte->Write = 1;
  191. Pte->Owner = 1; // User-accessible
  192. Pte->LargePage = 0;
  193. //
  194. // Flush TLB
  195. //
  196. HalpFlushTLB();
  197. //
  198. // Map the first 1Mb of virtual memory to the first 1Mb of physical
  199. // memory
  200. //
  201. for (Virtual=0; Virtual < 0x100000; Virtual += PAGE_SIZE) {
  202. Pte = MiGetPteAddress((PVOID)Virtual);
  203. HalpSetPageFrameNumber( Pte, Virtual >> PAGE_SHIFT );
  204. Pte->Valid = 1;
  205. Pte->Write = 1;
  206. Pte->Owner = 1; // User-accessible
  207. }
  208. //
  209. // Map our code into the virtual machine
  210. //
  211. Pte = MiGetPteAddress((PVOID)0x20000);
  212. PointerPde = MiGetPdeAddress(&HalpRealModeStart);
  213. if ( PointerPde->LargePage ) {
  214. //
  215. // Map real mode PTEs into virtual mapping. The source PDE is
  216. // from the indenity large pte map, so map the virtual machine PTEs
  217. // based on the base of the large PDE frame.
  218. //
  219. PageFrame = MiGetPteIndex( &HalpRealModeStart );
  220. PageFrameEnd = MiGetPteIndex( &HalpRealModeEnd );
  221. do {
  222. HalpSetPageFrameNumber( Pte,
  223. HalpGetPageFrameNumber( PointerPde ) +
  224. PageFrame );
  225. HalpIncrementPte( &Pte );
  226. ++PageFrame;
  227. } while (PageFrame <= PageFrameEnd);
  228. } else {
  229. //
  230. // Map real mode PTEs into virtual machine PTEs, by copying the
  231. // page frames from the source to the virtual machine PTEs.
  232. //
  233. V86CodePte = MiGetPteAddress(&HalpRealModeStart);
  234. do {
  235. HalpCopyPageFrameNumber( Pte, V86CodePte );
  236. HalpIncrementPte( &Pte );
  237. HalpIncrementPte( &V86CodePte );
  238. } while ( V86CodePte <= MiGetPteAddress(&HalpRealModeEnd) );
  239. }
  240. //
  241. // Verify the IDT is writable
  242. //
  243. Pte = MiGetPteAddress(Pcr->IDT);
  244. PointerPde = MiGetPdeAddress(Pcr->IDT);
  245. IdtPte = PointerPde->LargePage ? PointerPde : Pte;
  246. OldIdtWrite = (ULONG)IdtPte->Write;
  247. IdtPte->Write = 1;
  248. //
  249. // Flush TLB
  250. //
  251. HalpFlushTLB();
  252. //
  253. // We need to replace the current TRAP D handler with our own, so
  254. // we can do instruction emulation for V86 mode
  255. //
  256. OldTrap0DHandler = KiReturnHandlerAddressFromIDT(0xd);
  257. KiSetHandlerAddressToIDT(0xd, HalpTrap0D);
  258. OldTrap06Handler = KiReturnHandlerAddressFromIDT(6);
  259. KiSetHandlerAddressToIDT(6, HalpTrap06);
  260. //
  261. // Make sure current TSS has IoMap space available. If no, borrow
  262. // Normal TSS.
  263. //
  264. OriginalTssSelector = HalpBorrowTss();
  265. //
  266. // Overwrite the first access map with zeroes, so the V86 code can
  267. // party on all the registers.
  268. //
  269. IoMap = (PUCHAR)&(Pcr->TSS->IoMaps[0].IoMap);
  270. IopmDiffTableEntries =
  271. HalpStoreAndClearIopm(IoMap, IopmDiffTable, MAX_DIFFERENCES);
  272. OldIoMapBase = Pcr->TSS->IoMapBase;
  273. Pcr->TSS->IoMapBase = KiComputeIopmOffset(1);
  274. //
  275. // Save the current ESP0, as HalpBiosCall() trashes it.
  276. //
  277. OldEsp0 = Pcr->TSS->Esp0;
  278. //
  279. // Call the V86-mode code
  280. //
  281. HalpBiosCall();
  282. //
  283. // Restore the TRAP handlers
  284. //
  285. if ((HalpNMIInProgress == FALSE) ||
  286. ((*((PBOOLEAN)(*(PLONG)&KdDebuggerNotPresent)) == FALSE) &&
  287. (**((PUCHAR *)&KdDebuggerEnabled) != FALSE))) {
  288. // If we are here due to an NMI, the IRET performed in HalpBiosCall()
  289. // allows a second NMI to occur. The second NMI causes a trap 0d because
  290. // the NMI TSS is busy and proceeds to bugcheck which trashes the screen.
  291. // Thus in this case we leave this trap 0d handler in place which will then
  292. // just spin on a jump to self if a second NMI occurs.
  293. KiSetHandlerAddressToIDT(0xd, OldTrap0DHandler);
  294. }
  295. KiSetHandlerAddressToIDT(6, OldTrap06Handler);
  296. IdtPte->Write = OldIdtWrite;
  297. //
  298. // Restore Esp0 value
  299. //
  300. Pcr->TSS->Esp0 = OldEsp0;
  301. //
  302. // Restore the IoMap to its previous state.
  303. //
  304. HalpRestoreIopm(IoMap, IopmDiffTable, IopmDiffTableEntries);
  305. Pcr->TSS->IoMapBase = OldIoMapBase;
  306. //
  307. // Return borrowed TSS if any.
  308. //
  309. if (OriginalTssSelector != 0) {
  310. HalpReturnTss(OriginalTssSelector);
  311. }
  312. //
  313. // Unmap the first 1Mb of virtual memory
  314. //
  315. for (Virtual = 0; Virtual < 0x100000; Virtual += PAGE_SIZE) {
  316. Pte = MiGetPteAddress((PVOID)Virtual);
  317. Pte->Valid = 0;
  318. Pte->Write = 0;
  319. HalpSetPageFrameNumber( Pte, 0 );
  320. }
  321. //
  322. // Restore the original page table that we replaced.
  323. //
  324. PointerPde = MiGetPdeAddress((PVOID)0);
  325. if (HalPaeEnabled() != FALSE) {
  326. *(PHARDWARE_PTE_X86PAE)PointerPde = OldPageTablePae;
  327. } else {
  328. *PointerPde = OldPageTable;
  329. }
  330. HalpSetPageFrameNumber( PointerPde, OldPageTablePfn );
  331. //
  332. // Flush TLB
  333. //
  334. HalpFlushTLB();
  335. //
  336. // Re-enable Interrupts
  337. //
  338. HalpReenableInterrupts(OldIrql);
  339. return TRUE;
  340. }
  341. HAL_DISPLAY_BIOS_INFORMATION
  342. HalpGetDisplayBiosInformation (
  343. VOID
  344. )
  345. {
  346. // this hal uses native int-10
  347. return HalDisplayInt10Bios;
  348. }