BIOS.I386.Traps.Mod 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. MODULE Traps; (** AUTHOR "pjm"; PURPOSE "Trap handling and symbolic debugging"; *)
  2. IMPORT SYSTEM, Machine, KernelLog, Streams, Modules, Objects, Kernel, Reflection, TrapWriters;
  3. CONST
  4. RecursiveLimit = 2; (* normally 1 or 2 - how many recursive traps to display before stopping *)
  5. TraceVerbose = FALSE;
  6. TestTrap = TRUE;
  7. (* Process termination halt codes *)
  8. halt* = Objects.halt;
  9. haltUnbreakable* = Objects.haltUnbreakable;
  10. TYPE
  11. Variable* = RECORD (** variable descriptor *)
  12. adr-: ADDRESS;
  13. type-, size-, n-, tdadr-: LONGINT
  14. END;
  15. VAR
  16. trapState: ARRAY Machine.MaxCPU OF LONGINT; (* indexed by Machine.ID() *)
  17. modes: ARRAY 25 OF CHAR;
  18. flags: ARRAY 13 OF CHAR;
  19. (* Write flag values. *)
  20. PROCEDURE Flags(w: Streams.Writer; s: SET);
  21. VAR i: SHORTINT; ch: CHAR;
  22. BEGIN
  23. FOR i := 0 TO 11 DO
  24. ch := flags[i];
  25. IF ch # "!" THEN
  26. IF i IN s THEN ch := CAP(ch) END;
  27. w.Char(ch)
  28. END
  29. END;
  30. w.String(" iopl"); w.Int(ASH(SYSTEM.VAL(LONGINT, s * {12,13}), -12), 1)
  31. END Flags;
  32. (** Display trap state. *)
  33. PROCEDURE Show*(p: Objects.Process; VAR int: Machine.State; VAR exc: Machine.ExceptionState; long: BOOLEAN);
  34. VAR id: LONGINT; overflow: BOOLEAN; w: Streams.Writer;
  35. PROCEDURE Val(CONST s: ARRAY OF CHAR; val: LONGINT);
  36. BEGIN
  37. w.Char(" "); w.String(s); w.Char("="); w.Hex(val, -8)
  38. END Val;
  39. BEGIN
  40. overflow := FALSE;
  41. w := TrapWriters.GetWriter();
  42. w.Update; (* flush previous output stuck in global writer w *)
  43. w.Char(1X); (* "start of trap" *)
  44. id := Machine.ID();
  45. INC(trapState[id]);
  46. IF trapState[id] > RecursiveLimit THEN
  47. w.String(" [Recursive TRAP]")
  48. ELSE
  49. (* output first line *)
  50. w.String("TRAP "); w.Int(exc.halt, 1);
  51. w.String(" ["); w.Int(trapState[id], 1); w.String("]");
  52. w.String(" PL"); w.Int(int.CS MOD 4, 2); w.Char(" ");
  53. CASE exc.halt OF
  54. -14: (* page fault *)
  55. IF (int.CS MOD 4 > Machine.KernelLevel) & (exc.pf+4 = int.SP) THEN
  56. w.String("stack overflow"); overflow := TRUE
  57. ELSE
  58. w.String("memory access violation (page fault)");
  59. END
  60. |0: w.String("division error")
  61. |1: w.String("WITH guard failed")
  62. |2: w.String("CASE invalid")
  63. |3: w.String("RETURN missing")
  64. |4: w.String("integer overflow")
  65. |5: w.String("implicit type guard failed")
  66. |6: w.String("type guard failed")
  67. |7: w.String("index out of range")
  68. |8: w.String("ASSERT failed")
  69. |9: w.String("array dimension error")
  70. |14: w.String("out of memory")
  71. |16: w.String("procedure returned")
  72. |17: w.String("nil pointer access")
  73. ELSE
  74. IF (exc.halt > MAX(INTEGER)+1) OR (exc.halt < MIN(INTEGER)) THEN
  75. w.String("module freed?")
  76. END
  77. END;
  78. IF exc.locks # {} THEN
  79. w.String(", Locks: "); w.Set(exc.locks)
  80. END;
  81. w.Ln; w.String("System: "); w.String(Machine.version); w.String(" Kernel_CRC=");(* w.Hex(SystemVersion.BootCRC,8);*)
  82. IF long THEN
  83. w.Char(0EX); (* "fixed font" *)
  84. w.Ln;
  85. w.String("Processor:");
  86. (* output values *)
  87. Val("CS", int.CS); Val("DS", exc.DS); Val("ES", exc.ES); Val("SS", exc.SS); Val("CR0", exc.CR[0]);
  88. Val("FPU", SYSTEM.VAL(LONGINT, exc.FPU[1] * {0..15} + LSH(exc.FPU[2], 16)));
  89. Val("PC", int.PC); Val("ESI", int.ESI); Val("EDI", int.EDI); Val("SP", exc.SP); Val("CR2", exc.CR[2]);
  90. Val("PID", id);
  91. Val("EAX", int.EAX); Val("EBX", int.EBX); Val("ECX", int.ECX); Val("EDX", int.EDX); Val("CR3", exc.CR[3]);
  92. Val("LCK", SYSTEM.VAL(LONGINT, exc.locks));
  93. Val("BP", int.BP); Val("FS", exc.FS); Val("GS", exc.GS); Val("ERR", int.ERR); Val("CR4", exc.CR[4]);
  94. Val("TMR", Kernel.GetTicks()); w.Ln;
  95. IF SYSTEM.VAL(CHAR, exc.DR[7]) # 0X THEN (* some breakpoints enabled *)
  96. Val("DR0", exc.DR[0]); Val("DR1", exc.DR[1]); Val("DR2", exc.DR[2]); Val("DR3", exc.DR[3]);
  97. Val("DR6", exc.DR[6]); Val("DR7", exc.DR[7]); w.Ln
  98. END;
  99. w.String(" FLAGS: "); Flags(w, int.FLAGS);
  100. w.Char(0FX); (* "proportional font" *)
  101. w.Char(" "); w.Set(int.FLAGS); w.Ln;
  102. w.String(" Features: "); (* w.Set(Machine.features); w.Set(Machine.features2); *) w.Ln;
  103. (*IF int.INT = Machine.UD THEN KernelLog.Memory(int.PC, 16) END*) (* show bad instruction *)
  104. ELSE
  105. w.Ln
  106. END;
  107. w.String("Process:"); Reflection.WriteProcess(w, p); w.Ln;
  108. (*IF exc.halt = 1301 THEN (* lock timeout - see Machine *)
  109. KernelLog.Memory(ADDRESSOF(Machine.trapState[0]), LEN(Machine.trapState) *
  110. (ADDRESSOF(Machine.trapState[1]) - ADDRESSOF(Machine.trapState[0])));
  111. w.Hex(SYSTEM.VAL(LONGINT, Machine.trapLocksBusy), 8); w.Ln
  112. END;
  113. IF (int.INT = Machine.PF) & (ABS(int.PC-exc.CR[2]) < 100H) THEN (* PF close to PC *)
  114. KernelLog.Memory(int.ESP-16, 64) (* show stack *)
  115. END;*)
  116. w.String( "StackTraceBack:" ); w.Ln;
  117. Reflection.StackTraceBack(w, int.PC, int.BP, int.SP, Objects.GetStackBottom(p), long, overflow);
  118. END;
  119. w.String("---------------------------------"); w.Ln;
  120. w.Char(02X); (* "end of trap" *)
  121. w.Update;
  122. TrapWriters.Trapped();
  123. trapState[id] := 0
  124. END Show;
  125. PROCEDURE SetLastExceptionState(ex: Machine.ExceptionState);
  126. VAR id: LONGINT;
  127. BEGIN
  128. id := Machine.AcquirePreemption();
  129. Objects.running[id].exp := ex;
  130. Machine.ReleasePreemption;
  131. END SetLastExceptionState;
  132. PROCEDURE GetLastExceptionState*(): Machine.ExceptionState;
  133. VAR
  134. id: LONGINT;
  135. ex: Machine.ExceptionState;
  136. BEGIN
  137. id := Machine.AcquirePreemption();
  138. ex := Objects.running[id].exp;
  139. Machine.ReleasePreemption;
  140. RETURN ex;
  141. END GetLastExceptionState;
  142. (** Handles an exception. Interrupts are on during this procedure. *)
  143. PROCEDURE HandleException(VAR int: Machine.State; VAR exc: Machine.ExceptionState; VAR handled: BOOLEAN);
  144. VAR
  145. bp, sp, pc, handler: ADDRESS;
  146. BEGIN
  147. bp := int.BP; sp := int.SP; pc := int.PC;
  148. handler := Modules.GetExceptionHandler(pc);
  149. IF handler # -1 THEN (* Handler in the current PAF *)
  150. int.PC := handler; handled := TRUE;
  151. SetTrapVariable(pc, bp); SetLastExceptionState(exc)
  152. ELSE
  153. WHILE (bp # 0) & (handler = -1) DO
  154. SYSTEM.GET(bp + 4, pc);
  155. pc := pc - 1; (* CALL instruction, machine dependant!!! *)
  156. handler := Modules.GetExceptionHandler(pc);
  157. sp := bp; (* Save the old basepointer into the stack pointer *)
  158. SYSTEM.GET(bp, bp) (* Unwind PAF *)
  159. END;
  160. IF handler = -1 THEN
  161. handled := FALSE;
  162. ELSE
  163. int.PC := handler; int.BP := bp; int.SP := sp;
  164. SetTrapVariable(pc, bp); SetLastExceptionState(exc);
  165. handled := TRUE
  166. END
  167. END
  168. END HandleException;
  169. PROCEDURE SetTrapVariable(pc, fp: ADDRESS);
  170. VAR
  171. varadr: ADDRESS;
  172. BEGIN
  173. varadr := Reflection.GetVariableAdr(pc, fp, "trap");
  174. IF varadr # -1 THEN
  175. SYSTEM.PUT8(varadr, 1)
  176. END
  177. END SetTrapVariable;
  178. (* Unbreakable stack trace back with regard to every FINALLY on the way *)
  179. PROCEDURE Unbreakable(p: Objects.Process; VAR int: Machine.State; VAR exc: Machine.ExceptionState; VAR handled: BOOLEAN);
  180. VAR
  181. bp, bpSave, pc, handler, bpBottom:ADDRESS;
  182. hasFinally : BOOLEAN;
  183. BEGIN
  184. bp := int.BP;
  185. pc := int.PC;
  186. hasFinally := FALSE;
  187. handler := Modules.GetExceptionHandler(pc);
  188. (* Handler in the current PAF *)
  189. IF handler # -1 THEN
  190. int.PC := handler;
  191. hasFinally := TRUE;
  192. SetTrapVariable(pc, bp);
  193. END;
  194. (* The first waypoint is the bp of the top PAF *)
  195. bpSave := bp;
  196. WHILE (bp # 0) DO
  197. (* Did we reach the last PAF? *)
  198. SYSTEM.GET(bp, pc);
  199. IF (pc = 0) THEN
  200. bpBottom := bp; (* Save the FP of the last PAF *)
  201. END;
  202. (* Get the return pc *)
  203. SYSTEM.GET(bp + SIZEOF(ADDRESS), pc);
  204. handler := Modules.GetExceptionHandler(pc);
  205. (* Save the last framepointer as stackpointer *)
  206. IF ~hasFinally THEN
  207. int.SP := bp;
  208. END;
  209. SYSTEM.GET(bp, bp);
  210. (* Here bp may be 0. *)
  211. IF (handler # -1) & (bp # 0) THEN (* If Objects.Terminate has a FINALLY this doesn't work !!! *)
  212. IF hasFinally THEN
  213. (* Connect Finally to Finally *)
  214. SYSTEM.PUT(bpSave + SIZEOF(ADDRESS), handler); (* Adapt the return pc *)
  215. SYSTEM.PUT(bpSave, bp); (* Adapt the dynamic link *)
  216. bpSave := bp;
  217. ELSE
  218. int.PC := handler;
  219. int.BP := bp;
  220. bpSave := bp;
  221. hasFinally := TRUE;
  222. END;
  223. SetTrapVariable(pc, bp)
  224. END
  225. END;
  226. (* Now bp = 0, bottom of the stack, so link the last known return PC to the Termination *)
  227. IF ~hasFinally THEN
  228. SYSTEM.GET(bpBottom + SIZEOF(ADDRESS), pc); (* PC of the Terminate *)
  229. int.PC := pc;
  230. int.BP := bpBottom;
  231. ELSIF bpSave # bpBottom THEN
  232. SYSTEM.GET(bpBottom + SIZEOF(ADDRESS), pc); (* PC of the Terminate *)
  233. SYSTEM.PUT(bpSave + SIZEOF(ADDRESS), pc);
  234. SetLastExceptionState(exc)
  235. END;
  236. handled := TRUE; (* If FALSE the process could be restarted, may be this is the meaning? *)
  237. END Unbreakable;
  238. (* General exception handler. *)
  239. PROCEDURE Exception(VAR int: Machine.State);
  240. VAR t: Objects.Process; exc: Machine.ExceptionState; user, traceTrap, handled: BOOLEAN;
  241. BEGIN (* interrupts off *)
  242. t := Objects.running[Machine.ID()]; (* t is running process *)
  243. handled := FALSE;
  244. Machine.GetExceptionState(int, exc);
  245. user := (int.CS MOD 4 > Machine.KernelLevel);
  246. traceTrap := (exc.locks = {}) & (exc.halt >= MAX(INTEGER)) & (exc.halt <= MAX(INTEGER)+1);
  247. Show(t, int, exc, exc.halt # MAX(INTEGER)+1); (* Always show the trap info!*)
  248. IF exc.halt = haltUnbreakable THEN
  249. Unbreakable(t, int, exc, handled)
  250. ELSIF ~ traceTrap THEN
  251. HandleException( int, exc, handled)
  252. END;
  253. IF ~handled THEN
  254. (* Taken from Machine to allow the FINALLY in the kernel *)
  255. exc.locks := Machine.BreakAll();
  256. Machine.Sti();
  257. IF ~traceTrap THEN (* trap *)
  258. IF user THEN (* return to outer level *)
  259. IF TraceVerbose THEN
  260. KernelLog.Enter;
  261. KernelLog.String("Jump"); KernelLog.Hex(t.restartPC, 9);
  262. KernelLog.Hex(t.restartSP, 9); KernelLog.Hex(t.stack.high, 9);
  263. KernelLog.Exit
  264. END;
  265. INCL(int.FLAGS, Machine.IFBit); (* enable interrupts *)
  266. int.BP := 0; int.SP := t.restartSP; (* reset stack *)
  267. int.PC := t.restartPC; (* restart object body or terminate *)
  268. ELSE (* trap was in kernel (interrupt handler) *) (* fixme: recover from trap in stack traceback *)
  269. KernelLog.Enter; KernelLog.String("Kernel halt"); KernelLog.Exit;
  270. Machine.Shutdown(FALSE)
  271. END
  272. END
  273. END;
  274. IF Objects.PleaseHalt IN t.flags THEN
  275. EXCL(t.flags, Objects.PleaseHalt);
  276. IF Objects.Unbreakable IN t.flags THEN EXCL(t.flags, Objects.Unbreakable) END;
  277. IF Objects.SelfTermination IN t.flags THEN EXCL(t.flags, Objects.SelfTermination) END
  278. END
  279. END Exception;
  280. (* Page fault handler. *)
  281. PROCEDURE PageFault(VAR state: Machine.State);
  282. VAR t: Objects.Process;
  283. BEGIN
  284. t := Objects.running[Machine.ID()];
  285. IF Machine.IFBit IN state.FLAGS THEN (* enable interrupts again if they were enabled *)
  286. Machine.Sti() (* avoid Processors.StopAll deadlock when waiting for locks below (fixme: remove) *)
  287. END;
  288. IF (t = NIL) OR ~Machine.ExtendStack(t.stack, Machine.CR2()) THEN
  289. IF TraceVerbose THEN
  290. IF t = NIL THEN
  291. KernelLog.Enter; KernelLog.String("GrowStack running=NIL");
  292. KernelLog.Hex(state.PC, 9); KernelLog.Exit
  293. ELSE
  294. KernelLog.Enter;
  295. KernelLog.String("GrowStack failed, pf="); KernelLog.Hex(Machine.CR2(), 8);
  296. KernelLog.String(" adr="); KernelLog.Hex(t.stack.adr, 8);
  297. KernelLog.String(" high="); KernelLog.Hex(t.stack.high, 8);
  298. (*KernelLog.Ln; KernelLog.Memory(t.stack.adr, 256);*)
  299. KernelLog.Exit
  300. END
  301. END;
  302. Exception(state)
  303. ELSE
  304. IF TraceVerbose THEN
  305. KernelLog.Enter; KernelLog.String("GrowStack");
  306. KernelLog.Hex(t.stack.adr, 9); KernelLog.Hex(t.stack.high, 9); KernelLog.Exit
  307. END
  308. END
  309. END PageFault;
  310. PROCEDURE Init;
  311. VAR i: LONGINT; s: ARRAY 8 OF CHAR;
  312. BEGIN
  313. IF TestTrap THEN
  314. Machine.GetConfig("TestTrap", s);
  315. IF s[0] = "1" THEN HALT(98) END
  316. END;
  317. FOR i := 0 TO Machine.MaxCPU-1 DO trapState[i] := 0 END;
  318. Machine.InstallHandler(PageFault, Machine.PF);
  319. FOR i := 0 TO 31 DO
  320. IF ~(i IN {Machine.PF}) THEN (* PF handler above *)
  321. Machine.InstallHandler(Exception, i)
  322. END
  323. END;
  324. IF TestTrap & (s[0] = "2") THEN HALT(99) END
  325. END Init;
  326. BEGIN
  327. modes := " rdy run awl awc awe rip"; (* 4 characters per mode from Objects.Ready to Objects.Terminated *)
  328. flags := "c!p!a!zstido"; (* bottom flags, !=reserved *)
  329. Init
  330. END Traps.
  331. (*
  332. 12.03.1998 pjm Started
  333. 06.08.1998 pjm Exported Show and removed AosException upcall installation & Modules lock
  334. 10.12.1998 pjm New refblk
  335. 23.06.1999 pjm State added
  336. *)
  337. (*
  338. to do:
  339. o stack overflow message is not correctly displayed in case of dynamic arrays (EDI = CR2, ESP # CR2)
  340. o fix KernelLog.Memory calls removed when switching to Streams
  341. o fix use of KernelLog lock in Show
  342. o if allowing modification of variables using their descriptors, it should also have reference to module to avoid gc after free.
  343. *)