Source code of Windows XP (NT5)
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.

423 lines
6.7 KiB

  1. /*++
  2. Copyright (c) 1990 Microsoft Corporation
  3. Module Name:
  4. trap.c
  5. Author:
  6. Thomas Parslow [TomP] Mar-01-90
  7. Abstract:
  8. General purpose trap handler for 80386 boot loader. When built in
  9. debugger is present, output is redirected to the com port. When no
  10. debugger is present, output goes to the display.
  11. --*/
  12. #include "su.h"
  13. extern
  14. USHORT
  15. InDebugger;
  16. extern
  17. USHORT
  18. DebuggerPresent;
  19. extern
  20. UCHAR
  21. GDTregister;
  22. extern
  23. UCHAR
  24. IDTregister;
  25. extern
  26. VOID
  27. OutPort(
  28. USHORT
  29. );
  30. extern
  31. USHORT
  32. InPort(
  33. VOID
  34. );
  35. extern
  36. VOID
  37. ReEnterDebugger(
  38. VOID
  39. );
  40. extern
  41. USHORT
  42. TssKernel;
  43. extern
  44. USHORT
  45. Redirect;
  46. extern
  47. VOID RealMode(
  48. VOID
  49. );
  50. VOID
  51. TrapHandler(
  52. IN ULONG,
  53. IN USHORT
  54. );
  55. VOID
  56. DumpProcessorContext(
  57. VOID
  58. );
  59. VOID
  60. DumpSystemRegisters(
  61. VOID
  62. );
  63. VOID
  64. DumpCommonRegisters(
  65. VOID
  66. );
  67. VOID
  68. DisplayFlags(
  69. ULONG f
  70. );
  71. VOID
  72. DumpTSS(
  73. VOID
  74. );
  75. ULONG
  76. GetAddress(
  77. VOID
  78. );
  79. VOID
  80. GetNumber(
  81. PCHAR cp
  82. );
  83. USHORT
  84. GetChar(
  85. VOID
  86. );
  87. VOID
  88. DumpAddress(
  89. ULONG
  90. );
  91. #define PG_FAULT_MSG " =================== PAGE FAULT ================================= \n\n"
  92. #define DBL_FAULT_MSG " ================== DOUBLE FAULT ================================ \n\n"
  93. #define GP_FAULT_MSG " ============== GENERAL PROTECTION FAULT ======================== \n\n"
  94. #define STK_OVERRUN_MSG " ===== STACK SEGMENT OVERRUN or NOT PRESENT FAULT =============== \n\n"
  95. #define EX_FAULT_MSG " ===================== EXCEPTION ================================ \n\n"
  96. #define DEBUG_EXCEPTION "\nDEBUG TRAP "
  97. #define ishex(x) ( ( x >= '0' && x <= '9') || (x >= 'A' && x <= 'F') || (x >= 'a' && x <= 'f') )
  98. //
  99. // Global Trap Frame Pointer
  100. //
  101. PTF TrapFrame;
  102. VOID
  103. TrapHandler(
  104. IN ULONG Padding,
  105. IN USHORT TF_base
  106. )
  107. /*++
  108. Routine Description:
  109. Prints minimal trap information
  110. Arguments:
  111. 386 Trap Frame on Stack
  112. Environment:
  113. 16-bit protect mode only.
  114. --*/
  115. {
  116. //
  117. // Initialize global trap frame pointer and print trap number
  118. //
  119. TrapFrame = (PTF)&TF_base;
  120. //
  121. // Fix esp to point to where it pointed before trap
  122. //
  123. TrapFrame->Fesp += 24;
  124. BlPrint("\n TRAP %lx ",TrapFrame->TrapNum);
  125. //
  126. // Print the trap specific header and display processor context
  127. //
  128. switch(TrapFrame->TrapNum) {
  129. case 1:
  130. case 3:
  131. puts( DEBUG_EXCEPTION );
  132. DumpCommonRegisters();
  133. break;
  134. case 8:
  135. puts( DBL_FAULT_MSG );
  136. DumpTSS();
  137. break;
  138. case 12:
  139. puts( STK_OVERRUN_MSG );
  140. DumpProcessorContext();
  141. break;
  142. case 13:
  143. puts( GP_FAULT_MSG );
  144. DumpProcessorContext();
  145. break;
  146. case 14:
  147. puts( PG_FAULT_MSG );
  148. BlPrint("** At linear address %lx\n",TrapFrame->Fcr2);
  149. DumpProcessorContext();
  150. break;
  151. default :
  152. puts( EX_FAULT_MSG );
  153. DumpProcessorContext();
  154. break;
  155. }
  156. RealMode();
  157. while (1); //**** WAITFOREVER *** //
  158. }
  159. VOID
  160. DumpProcessorContext(
  161. VOID
  162. )
  163. /*++
  164. Routine Description:
  165. Dumps all the processors registers. Called whenever a trap or fault
  166. occurs.
  167. Arguments:
  168. None
  169. Returns:
  170. Nothing
  171. --*/
  172. {
  173. DumpSystemRegisters();
  174. DumpCommonRegisters();
  175. }
  176. VOID
  177. DumpSystemRegisters(
  178. VOID
  179. )
  180. /*++
  181. Routine Description:
  182. Dumps (writes to the display or com poirt) the x86 processor control
  183. registers only. Does not dump the common registers (see
  184. DumpCommonRegisters)
  185. Arguments:
  186. None
  187. Returns:
  188. Nothing
  189. --*/
  190. {
  191. BlPrint("\n tr=%x cr0=%lx cr2=%lx cr3=%lx\n",
  192. TrapFrame->Ftr,TrapFrame->Fcr0,TrapFrame->Fcr2,TrapFrame->Fcr3);
  193. BlPrint(" gdt limit=%x base=%lx idt limit=%x base=%lx\n",
  194. *(PUSHORT)&GDTregister,*(PULONG)(&GDTregister + 2),
  195. *(PUSHORT)&IDTregister,*(PULONG)(&IDTregister + 2));
  196. }
  197. VOID
  198. DumpCommonRegisters(
  199. VOID
  200. )
  201. /*++
  202. Routine Description:
  203. Dumps (writes to the display or com poirt) the x86 processor
  204. commond registers only.
  205. Arguments:
  206. None
  207. Returns:
  208. Nothing
  209. --*/
  210. {
  211. USHORT err;
  212. //
  213. // Is the error code valid or just a padding dword
  214. //
  215. if ((TrapFrame->TrapNum == 8) || (TrapFrame->TrapNum >= 10 && TrapFrame->TrapNum <= 14) )
  216. err = (USHORT)TrapFrame->Error;
  217. else
  218. err = 0;
  219. //
  220. // Display the processor's common registers
  221. //
  222. BlPrint("\n cs:eip=%x:%lx ss:esp=%x:%lx errcode=%x\n",
  223. (USHORT)(TrapFrame->Fcs & 0xffff),TrapFrame->Feip,(USHORT)TrapFrame->Fss,TrapFrame->Fesp,err);
  224. DisplayFlags(TrapFrame->Feflags);
  225. BlPrint(" eax=%lx ebx=%lx ecx=%lx edx=%lx",TrapFrame->Feax,TrapFrame->Febx,TrapFrame->Fecx,TrapFrame->Fedx);
  226. BlPrint(" ds=%x es=%x\n",TrapFrame->Fds,TrapFrame->Fes);
  227. BlPrint(" edi=%lx esi=%lx ebp=%lx cr0=%lx",TrapFrame->Fedi,TrapFrame->Fesi,TrapFrame->Febp,TrapFrame->Fcr0);
  228. BlPrint(" fs=%x gs=%x\n",TrapFrame->Ffs,TrapFrame->Fgs);
  229. }
  230. VOID
  231. DisplayFlags(
  232. ULONG f
  233. )
  234. /*++
  235. Routine Description:
  236. Writes the value of the key flags in the flags register to
  237. the display or com port.
  238. Arguments:
  239. f - the 32bit flags word
  240. Returns:
  241. Nothing
  242. --*/
  243. {
  244. BlPrint(" flags=%lx ",f);
  245. if (f & FLAG_CF) puts("Cy "); else puts("NoCy ");
  246. if (f & FLAG_ZF) puts("Zr "); else puts("NoZr ");
  247. if (f & FLAG_IE) puts("IntEn"); else puts("IntDis ");
  248. if (f & FLAG_DF) puts("Up "); else puts("Down ");
  249. if (f & FLAG_TF) puts("TrapEn \n"); else puts("TrapDis \n");
  250. }
  251. VOID
  252. DumpTSS(
  253. VOID
  254. )
  255. /*++
  256. Routine Description:
  257. Writes the contents of the TSS to the display or com port when
  258. called after a double fault.
  259. Arguments:
  260. None
  261. Returns:
  262. Nothing
  263. --*/
  264. {
  265. PTSS_FRAME pTss;
  266. // FP_SEG(Fp) = Fcs;
  267. // FP_OFF(Fp) = Fip;
  268. pTss = (PTSS_FRAME) &TssKernel;
  269. //
  270. // Dump the outgoing TSS
  271. //
  272. BlPrint("Link %x\n",pTss->Link);
  273. BlPrint("Esp0 %x\n",pTss->Esp0);
  274. BlPrint("SS0 %x\n",pTss->SS0);
  275. BlPrint("Esp1 %lx\n",pTss->Esp1);
  276. BlPrint("Cr3 %lx\n",pTss->Cr3);
  277. BlPrint("Eip %lx\n",pTss->Eip);
  278. BlPrint("Eflg %lx\n",pTss->Eflags);
  279. BlPrint("Eax %lx\n",pTss->Eax);
  280. BlPrint("Ebx %lx\n",pTss->Ebx);
  281. BlPrint("Ecx %lx\n",pTss->Ecx);
  282. BlPrint("Edx %lx\n",pTss->Edx);
  283. BlPrint("Esp %lx\n",pTss->Esp);
  284. BlPrint("Ebp %lx\n",pTss->Ebp);
  285. BlPrint("Esi %lx\n",pTss->Esi);
  286. BlPrint("Edi %lx\n",pTss->Edi);
  287. BlPrint("ES %x\n",pTss->ES);
  288. BlPrint("CS %x\n",pTss->CS);
  289. BlPrint("SS %x\n",pTss->SS);
  290. BlPrint("DS %x\n",pTss->DS);
  291. BlPrint("FS %x\n",pTss->FS);
  292. BlPrint("GS %x\n",pTss->GS);
  293. BlPrint("Ldt %x\n",pTss->Ldt);
  294. RealMode();
  295. while(1);
  296. }
  297. // END OF FILE
  298.