dpmiexcp.pp 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1999-2000 by Pierre Muller
  5. DPMI Exception routines for Go32V2
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. {$ifndef IN_SYSTEM}
  13. {$GOTO ON}
  14. {$define IN_DPMIEXCP_UNIT}
  15. {$ifndef NO_EXCEPTIONS_IN_SYSTEM}
  16. {$define EXCEPTIONS_IN_SYSTEM}
  17. {$endif NO_EXCEPTIONS_IN_SYSTEM}
  18. Unit DpmiExcp;
  19. { If linking to C code we must avoid loading of the dpmiexcp.o
  20. in libc.a from the equivalent C code
  21. => all global functions from dpmiexcp.c must be aliased PM
  22. Problem this is only valid for DJGPP v2.01 }
  23. interface
  24. {$ifdef NO_EXCEPTIONS_IN_SYSTEM}
  25. uses
  26. go32;
  27. {$endif NO_EXCEPTIONS_IN_SYSTEM}
  28. {$endif ndef IN_SYSTEM}
  29. { No stack checking ! }
  30. {$S-}
  31. { Decide if we want to create the C functions or not }
  32. {$ifdef EXCEPTIONS_IN_SYSTEM}
  33. { If exceptions are in system the C functions must be
  34. inserted in the system unit }
  35. {$ifdef IN_DPMIEXCP_UNIT}
  36. {$undef CREATE_C_FUNCTIONS}
  37. {$else not IN_DPMIEXCP_UNIT}
  38. {$define CREATE_C_FUNCTIONS}
  39. {$endif ndef IN_DPMIEXCP_UNIT}
  40. {$else not EXCEPTIONS_IN_SYSTEM}
  41. {$define CREATE_C_FUNCTIONS}
  42. {$endif not EXCEPTIONS_IN_SYSTEM}
  43. { Error Messages }
  44. function do_faulting_finish_message(fake : boolean) : integer;cdecl;
  45. {$ifdef IN_SYSTEM}forward;{$endif IN_SYSTEM}
  46. { SetJmp/LongJmp }
  47. type
  48. { must also contain exception_state !! }
  49. pdpmi_jmp_buf = ^dpmi_jmp_buf;
  50. dpmi_jmp_buf = packed record
  51. eax,ebx,ecx,edx,esi,edi,ebp,esp,eip,flags : longint;
  52. cs,ds,es,fs,gs,ss : word;
  53. sigmask : longint; { for POSIX signals only }
  54. signum : longint; { for expansion ie 386 exception number }
  55. exception_ptr : pdpmi_jmp_buf; { pointer to previous exception if exists }
  56. end;
  57. function dpmi_setjmp(var rec : dpmi_jmp_buf) : longint;
  58. {$ifdef IN_SYSTEM}forward;{$endif IN_SYSTEM}
  59. procedure dpmi_longjmp(var rec : dpmi_jmp_buf;return_value : longint);
  60. {$ifdef IN_SYSTEM}forward;{$endif IN_SYSTEM}
  61. { Signals }
  62. const
  63. SIGABRT = 288;
  64. SIGFPE = 289;
  65. SIGILL = 290;
  66. SIGSEGV = 291;
  67. SIGTERM = 292;
  68. SIGALRM = 293;
  69. SIGHUP = 294;
  70. SIGINT = 295;
  71. SIGKILL = 296;
  72. SIGPIPE = 297;
  73. SIGQUIT = 298;
  74. SIGUSR1 = 299;
  75. SIGUSR2 = 300;
  76. SIGNOFP = 301;
  77. SIGTRAP = 302;
  78. SIGTIMR = 303; { Internal for setitimer (SIGALRM, SIGPROF) }
  79. SIGPROF = 304;
  80. SIGMAX = 320;
  81. SIG_BLOCK = 1;
  82. SIG_SETMASK = 2;
  83. SIG_UNBLOCK = 3;
  84. function SIG_DFL( x: longint) : longint;
  85. {$ifdef IN_SYSTEM}forward;{$endif IN_SYSTEM}
  86. function SIG_ERR( x: longint) : longint;
  87. {$ifdef IN_SYSTEM}forward;{$endif IN_SYSTEM}
  88. function SIG_IGN( x: longint) : longint;
  89. {$ifdef IN_SYSTEM}forward;{$endif IN_SYSTEM}
  90. type
  91. SignalHandler = function (v : longint) : longint;
  92. PSignalHandler = ^SignalHandler; { to be compatible with linux.pp }
  93. function signal(sig : longint;func : SignalHandler) : SignalHandler;
  94. {$ifdef IN_SYSTEM}forward;{$endif IN_SYSTEM}
  95. function _raise(sig : longint) : longint;
  96. {$ifdef IN_SYSTEM}forward;{$endif IN_SYSTEM}
  97. { Exceptions }
  98. type
  99. pexception_state = ^texception_state;
  100. texception_state = record
  101. __eax, __ebx, __ecx, __edx, __esi : longint;
  102. __edi, __ebp, __esp, __eip, __eflags : longint;
  103. __cs, __ds, __es, __fs, __gs, __ss : word;
  104. __sigmask : longint; { for POSIX signals only }
  105. __signum : longint; { for expansion }
  106. __exception_ptr : pexception_state; { pointer to previous exception }
  107. __fpu_state : array [0..108-1] of byte; { for future use }
  108. end;
  109. procedure djgpp_exception_toggle;
  110. {$ifdef IN_SYSTEM}forward;{$endif IN_SYSTEM}
  111. procedure djgpp_exception_setup;
  112. {$ifdef IN_SYSTEM}forward;{$endif IN_SYSTEM}
  113. function djgpp_exception_state : pexception_state;
  114. {$ifdef IN_SYSTEM}forward;{$endif IN_SYSTEM}
  115. function djgpp_set_ctrl_c(enable : boolean) : boolean;
  116. {$ifdef IN_SYSTEM}forward;{$endif IN_SYSTEM}
  117. { Other }
  118. function dpmi_set_coprocessor_emulation(flag : longint) : longint;
  119. {$ifdef IN_SYSTEM}forward;{$endif IN_SYSTEM}
  120. function __djgpp_set_sigint_key(new_key : longint) : longint;cdecl;
  121. {$ifdef IN_SYSTEM}forward;{$endif IN_SYSTEM}
  122. function __djgpp_set_sigquit_key(new_key : longint) : longint;cdecl;
  123. {$ifdef IN_SYSTEM}forward;{$endif IN_SYSTEM}
  124. function __djgpp__traceback_exit(sig : longint) : longint;cdecl;
  125. {$ifdef IN_SYSTEM}forward;{$endif IN_SYSTEM}
  126. {$ifndef IN_SYSTEM}
  127. implementation
  128. {$endif IN_SYSTEM}
  129. {$asmmode ATT}
  130. {$ifdef CREATE_C_FUNCTIONS}
  131. {$L exceptn.o}
  132. {$endif CREATE_C_FUNCTIONS}
  133. {$ifndef CREATE_C_FUNCTIONS}
  134. procedure djgpp_exception_toggle;
  135. external name '___djgpp_exception_toggle';
  136. procedure djgpp_exception_setup;
  137. external name '___djgpp_exception_setup';
  138. function __djgpp_set_sigint_key(new_key : longint) : longint;cdecl;
  139. external name '___djgpp_set_sigint_key';
  140. function __djgpp_set_sigquit_key(new_key : longint) : longint;cdecl;
  141. external name '___djgpp_set_sigquit_key';
  142. function __djgpp__traceback_exit(sig : longint) : longint;cdecl;
  143. external name '__djgpp__traceback_exit';
  144. {$else CREATE_C_FUNCTIONS}
  145. var
  146. v2prt0_ds_alias : word;external name '___v2prt0_ds_alias';
  147. djgpp_ds_alias : word;external name '___djgpp_ds_alias';
  148. djgpp_old_kbd : tseginfo;external name '___djgpp_old_kbd';
  149. djgpp_hw_lock_start : longint;external name '___djgpp_hw_lock_start';
  150. djgpp_hw_lock_end : longint;external name '___djgpp_hw_lock_end';
  151. djgpp_dos_sel : word;external name '___djgpp_dos_sel';
  152. djgpp_exception_table : array[0..0] of pointer;external name '___djgpp_exception_table';
  153. dosmemselector : word;external name '_core_selector';
  154. procedure djgpp_i24;external name '___djgpp_i24';
  155. procedure djgpp_iret;external name '___djgpp_iret';
  156. procedure djgpp_npx_hdlr;external name '___djgpp_npx_hdlr';
  157. procedure djgpp_kbd_hdlr;external name '___djgpp_kbd_hdlr';
  158. procedure djgpp_kbd_hdlr_pc98;external name '___djgpp_kbd_hdlr_pc98';
  159. procedure djgpp_cbrk_hdlr;external name '___djgpp_cbrk_hdlr';
  160. {$endif CREATE_C_FUNCTIONS}
  161. var
  162. endtext : longint;external name '_etext';
  163. starttext : longint;external name 'start';
  164. djgpp_exception_state_ptr : pexception_state;external name '___djgpp_exception_state_ptr';
  165. djgpp_hwint_flags : longint;external name '___djgpp_hwint_flags';
  166. {$ifdef CREATE_C_FUNCTIONS}
  167. var
  168. exceptions_on : boolean;
  169. { old_int00 : tseginfo;cvar;external;
  170. old_int75 : tseginfo;cvar;external; }
  171. const
  172. cbrk_vect : byte = $1b;
  173. exception_level : longint = 0;
  174. {$endif CREATE_C_FUNCTIONS}
  175. {$ifndef IN_DPMIEXCP_UNIT}
  176. {****************************************************************************
  177. DPMI functions copied from go32 unit
  178. ****************************************************************************}
  179. const
  180. int31error : word = 0;
  181. procedure test_int31(flag : longint);
  182. begin
  183. asm
  184. pushl %ebx
  185. movw $0,INT31ERROR
  186. movl flag,%ebx
  187. testb $1,%bl
  188. jz .Lti31_1
  189. movw %ax,INT31ERROR
  190. xorl %eax,%eax
  191. jmp .Lti31_2
  192. .Lti31_1:
  193. movl $1,%eax
  194. .Lti31_2:
  195. popl %ebx
  196. end;
  197. end;
  198. function set_pm_exception_handler(e : byte;const intaddr : tseginfo) : boolean;
  199. begin
  200. asm
  201. movl intaddr,%eax
  202. movl (%eax),%edx
  203. movw 4(%eax),%cx
  204. movl $0x212,%eax
  205. movb e,%bl
  206. int $0x31
  207. pushf
  208. call test_int31
  209. movb %al,__RESULT
  210. end;
  211. end;
  212. function set_exception_handler(e : byte;const intaddr : tseginfo) : boolean;
  213. begin
  214. asm
  215. movl intaddr,%eax
  216. movl (%eax),%edx
  217. movw 4(%eax),%cx
  218. movl $0x203,%eax
  219. movb e,%bl
  220. int $0x31
  221. pushf
  222. call test_int31
  223. movb %al,__RESULT
  224. end;
  225. end;
  226. function get_pm_exception_handler(e : byte;var intaddr : tseginfo) : boolean;
  227. begin
  228. asm
  229. movl $0x210,%eax
  230. movb e,%bl
  231. int $0x31
  232. pushf
  233. call test_int31
  234. movb %al,__RESULT
  235. movl intaddr,%eax
  236. movl %edx,(%eax)
  237. movw %cx,4(%eax)
  238. end;
  239. end;
  240. function get_exception_handler(e : byte;var intaddr : tseginfo) : boolean;
  241. begin
  242. asm
  243. movl $0x202,%eax
  244. movb e,%bl
  245. int $0x31
  246. pushf
  247. call test_int31
  248. movb %al,__RESULT
  249. movl intaddr,%eax
  250. movl %edx,(%eax)
  251. movw %cx,4(%eax)
  252. end;
  253. end;
  254. function get_segment_base_address(d : word) : longint;
  255. begin
  256. asm
  257. movw d,%bx
  258. movl $6,%eax
  259. int $0x31
  260. xorl %eax,%eax
  261. movw %dx,%ax
  262. shll $16,%ecx
  263. orl %ecx,%eax
  264. movl %eax,__RESULT
  265. end;
  266. end;
  267. function get_segment_limit(d : word) : longint;
  268. begin
  269. asm
  270. movzwl d,%eax
  271. lsl %eax,%eax
  272. jz .L_ok2
  273. xorl %eax,%eax
  274. .L_ok2:
  275. movl %eax,__RESULT
  276. end;
  277. end;
  278. function set_rm_interrupt(vector : byte;const intaddr : tseginfo) : boolean;
  279. begin
  280. asm
  281. movl intaddr,%eax
  282. movw (%eax),%dx
  283. movw 4(%eax),%cx
  284. movl $0x201,%eax
  285. movb vector,%bl
  286. int $0x31
  287. pushf
  288. call test_int31
  289. movb %al,__RESULT
  290. end;
  291. end;
  292. function get_rm_interrupt(vector : byte;var intaddr : tseginfo) : boolean;
  293. begin
  294. asm
  295. movb vector,%bl
  296. movl $0x200,%eax
  297. int $0x31
  298. pushf
  299. call test_int31
  300. movb %al,__RESULT
  301. movl intaddr,%eax
  302. movzwl %dx,%edx
  303. movl %edx,(%eax)
  304. movw %cx,4(%eax)
  305. end;
  306. end;
  307. function free_rm_callback(var intaddr : tseginfo) : boolean;
  308. begin
  309. asm
  310. movl intaddr,%eax
  311. movw (%eax),%dx
  312. movw 4(%eax),%cx
  313. movl $0x304,%eax
  314. int $0x31
  315. pushf
  316. call test_int31
  317. movb %al,__RESULT
  318. end;
  319. end;
  320. function get_rm_callback(pm_func : pointer;const reg : trealregs;var rmcb : tseginfo) : boolean;
  321. begin
  322. asm
  323. movl pm_func,%esi
  324. movl reg,%edi
  325. pushw %es
  326. movw v2prt0_ds_alias,%ax
  327. movw %ax,%es
  328. pushw %ds
  329. movw %cs,%ax
  330. movw %ax,%ds
  331. movl $0x303,%eax
  332. int $0x31
  333. popw %ds
  334. popw %es
  335. pushf
  336. call test_int31
  337. movb %al,__RESULT
  338. movl rmcb,%eax
  339. movzwl %dx,%edx
  340. movl %edx,(%eax)
  341. movw %cx,4(%eax)
  342. end;
  343. end;
  344. function lock_linear_region(linearaddr, size : longint) : boolean;
  345. begin
  346. asm
  347. movl $0x600,%eax
  348. movl linearaddr,%ecx
  349. movl %ecx,%ebx
  350. shrl $16,%ebx
  351. movl size,%esi
  352. movl %esi,%edi
  353. shrl $16,%esi
  354. int $0x31
  355. pushf
  356. call test_int31
  357. movb %al,__RESULT
  358. end;
  359. end;
  360. function lock_code(functionaddr : pointer;size : longint) : boolean;
  361. var
  362. linearaddr : longint;
  363. begin
  364. linearaddr:=longint(functionaddr)+get_segment_base_address(get_cs);
  365. lock_code:=lock_linear_region(linearaddr,size);
  366. end;
  367. {$endif ndef IN_DPMIEXCP_UNIT}
  368. {****************************************************************************
  369. Helpers
  370. ****************************************************************************}
  371. procedure err(const x : string);
  372. begin
  373. write(stderr, x);
  374. end;
  375. procedure errln(const x : string);
  376. begin
  377. writeln(stderr, x);
  378. end;
  379. procedure itox(v,len : longint);
  380. var
  381. st : string;
  382. begin
  383. st:=hexstr(v,len);
  384. err(st);
  385. end;
  386. {****************************************************************************
  387. SetJmp/LongJmp
  388. ****************************************************************************}
  389. {$ifdef CREATE_C_FUNCTIONS}
  390. function c_setjmp(var rec : dpmi_jmp_buf) : longint;[public, alias : '_setjmp'];
  391. begin
  392. { here we need to be subtle :
  393. - we need to return with the arg still on the stack
  394. - but we also need to jmp to FPC_setjmp and not to call it
  395. because otherwise the return address is wrong !!
  396. For this we shift the return address down and
  397. duplicate the rec on stack }
  398. asm
  399. movl %ebp,%esp
  400. popl %ebp
  401. subl $8,%esp
  402. movl %eax,(%esp)
  403. movl 8(%esp),%eax
  404. movl %eax,4(%esp)
  405. movl 12(%esp),%eax
  406. movl %eax,8(%esp)
  407. popl %eax
  408. jmp dpmi_setjmp
  409. end;
  410. end;
  411. {$endif CREATE_C_FUNCTIONS}
  412. function dpmi_setjmp(var rec : dpmi_jmp_buf) : longint;
  413. {$ifndef CREATE_C_FUNCTIONS}
  414. external name 'FPC_setjmp';
  415. {$else CREATE_C_FUNCTIONS}
  416. [alias : 'FPC_setjmp'];
  417. begin
  418. asm
  419. pushl %edi
  420. movl rec,%edi
  421. movl %eax,(%edi)
  422. movl %ebx,4(%edi)
  423. movl %ecx,8(%edi)
  424. movl %edx,12(%edi)
  425. movl %esi,16(%edi)
  426. { load edi }
  427. movl -4(%ebp),%eax
  428. { ... and store it }
  429. movl %eax,20(%edi)
  430. { ebp ... }
  431. movl (%ebp),%eax
  432. movl %eax,24(%edi)
  433. { esp ... }
  434. movl %esp,%eax
  435. addl $12,%eax
  436. movl %eax,28(%edi)
  437. { the return address }
  438. movl 4(%ebp),%eax
  439. movl %eax,32(%edi)
  440. { flags ... }
  441. pushfl
  442. popl 36(%edi)
  443. { !!!!! the segment registers, not yet needed }
  444. { you need them if the exception comes from
  445. an interrupt or a seg_move }
  446. movw %cs,40(%edi)
  447. movw %ds,42(%edi)
  448. movw %es,44(%edi)
  449. movw %fs,46(%edi)
  450. movw %gs,48(%edi)
  451. movw %ss,50(%edi)
  452. movl djgpp_exception_state_ptr, %eax
  453. movl %eax, 60(%edi)
  454. { restore EDI }
  455. pop %edi
  456. { we come from the initial call }
  457. xorl %eax,%eax
  458. movl %eax,__RESULT
  459. { leave USING RET inside CDECL functions is risky as
  460. some registers are pushed at entry
  461. ret $4 not anymore since cdecl !! }
  462. end;
  463. end;
  464. {$endif CREATE_C_FUNCTIONS}
  465. {$ifdef CREATE_C_FUNCTIONS}
  466. procedure c_longjmp(var rec : dpmi_jmp_buf;return_value : longint);[public, alias : '_longjmp'];
  467. begin
  468. dpmi_longjmp(rec,return_value);
  469. { never gets here !! so pascal stack convention is no problem }
  470. end;
  471. {$endif CREATE_C_FUNCTIONS}
  472. procedure dpmi_longjmp(var rec : dpmi_jmp_buf;return_value : longint);
  473. {$ifndef CREATE_C_FUNCTIONS}
  474. external name 'FPC_longjmp';
  475. {$else CREATE_C_FUNCTIONS}
  476. [public, alias : 'FPC_longjmp'];
  477. begin
  478. if (exception_level>0) then
  479. dec(exception_level);
  480. asm
  481. { restore compiler shit }
  482. popl %ebp
  483. { copy from longjmp.S }
  484. movl 4(%esp),%edi { get dpmi_jmp_buf }
  485. movl 8(%esp),%eax { store retval in j->eax }
  486. movl %eax,0(%edi)
  487. movw 46(%edi),%fs
  488. movw 48(%edi),%gs
  489. movl 4(%edi),%ebx
  490. movl 8(%edi),%ecx
  491. movl 12(%edi),%edx
  492. movl 24(%edi),%ebp
  493. { Now for some uglyness. The dpmi_jmp_buf structure may be ABOVE the
  494. point on the new SS:ESP we are moving to. We don't allow overlap,
  495. but do force that it always be valid. We will use ES:ESI for
  496. our new stack before swapping to it. }
  497. movw 50(%edi),%es
  498. movl 28(%edi),%esi
  499. subl $28,%esi { We need 7 working longwords on stack }
  500. movl 60(%edi),%eax
  501. movl %eax,%es:(%esi) { Exception pointer }
  502. movzwl 42(%edi),%eax
  503. movl %eax,%es:4(%esi) { DS }
  504. movl 20(%edi),%eax
  505. movl %eax,%es:8(%esi) { EDI }
  506. movl 16(%edi),%eax
  507. movl %eax,%es:12(%esi) { ESI }
  508. movl 32(%edi),%eax
  509. movl %eax,%es:16(%esi) { EIP - start of IRET frame }
  510. movl 40(%edi),%eax
  511. movl %eax,%es:20(%esi) { CS }
  512. movl 36(%edi),%eax
  513. movl %eax,%es:24(%esi) { EFLAGS }
  514. movl 0(%edi),%eax
  515. movw 44(%edi),%es
  516. movw 50(%edi),%ss
  517. movl %esi,%esp
  518. popl djgpp_exception_state_ptr
  519. popl %ds
  520. popl %edi
  521. popl %esi
  522. iret { actually jump to new cs:eip loading flags }
  523. end;
  524. end;
  525. {$endif CREATE_C_FUNCTIONS}
  526. {****************************************************************************
  527. Signals
  528. ****************************************************************************}
  529. var
  530. signal_list : Array[0..SIGMAX] of SignalHandler;cvar;
  531. {$ifndef CREATE_C_FUNCTIONS}external;{$endif}
  532. {$ifdef CREATE_C_FUNCTIONS}
  533. function SIG_ERR(x:longint):longint;[public,alias : '___djgpp_SIG_ERR'];
  534. begin
  535. SIG_ERR:=-1;
  536. end;
  537. function SIG_IGN(x:longint):longint;[public,alias : '___djgpp_SIG_IGN'];
  538. begin
  539. SIG_IGN:=-1;
  540. end;
  541. function SIG_DFL(x:longint):longint;[public,alias : '___djgpp_SIG_DFL'];
  542. begin
  543. SIG_DFL:=0;
  544. end;
  545. {$else CREATE_C_FUNCTIONS}
  546. function SIG_ERR(x:longint):longint;external name '___djgpp_SIG_ERR';
  547. function SIG_IGN(x:longint):longint;external name '___djgpp_SIG_IGN';
  548. function SIG_DFL(x:longint):longint;external name '___djgpp_SIG_DFL';
  549. {$endif CREATE_C_FUNCTIONS}
  550. function signal(sig : longint;func : SignalHandler) : SignalHandler;
  551. var
  552. temp : SignalHandler;
  553. begin
  554. if ((sig < 0) or (sig > SIGMAX) or (sig = SIGKILL)) then
  555. begin
  556. signal:=@SIG_ERR;
  557. runerror(201);
  558. end;
  559. temp := signal_list[sig];
  560. signal_list[sig] := func;
  561. signal:=temp;
  562. end;
  563. {$ifdef CREATE_C_FUNCTIONS}
  564. { C counter part }
  565. function c_signal(sig : longint;func : SignalHandler) : SignalHandler;cdecl;[public,alias : '_signal'];
  566. var
  567. temp : SignalHandler;
  568. begin
  569. temp:=signal(sig,func);
  570. c_signal:=temp;
  571. end;
  572. {$endif CREATE_C_FUNCTIONS}
  573. const
  574. signames : array [0..14] of string[4] = (
  575. 'ABRT','FPE ','ILL ','SEGV','TERM','ALRM','HUP ',
  576. 'INT ','KILL','PIPE','QUIT','USR1','USR2','NOFP','TRAP');
  577. procedure print_signal_name(sig : longint);
  578. begin
  579. if ((sig >= SIGABRT) and (sig <= SIGTRAP)) then
  580. begin
  581. err('Exiting due to signal SIG');
  582. err(signames[sig-sigabrt]);
  583. end
  584. else
  585. begin
  586. err('Exiting due to signal $');
  587. itox(sig, 4);
  588. end;
  589. errln('');
  590. end;
  591. function _raise(sig : longint) : longint;
  592. var
  593. temp : SignalHandler;
  594. begin
  595. if(sig < 0) or (sig > SIGMAX) then
  596. exit(-1);
  597. temp:=signal_list[sig];
  598. if (temp = SignalHandler(@SIG_IGN)) then
  599. exit(0);
  600. if (temp = SignalHandler(@SIG_DFL)) then
  601. begin
  602. print_signal_name(sig);
  603. do_faulting_finish_message(djgpp_exception_state<>nil); { Exits, does not return }
  604. exit(-1);
  605. end;
  606. { this is incompatible with dxegen-dxeload stuff PM }
  607. if ((cardinal(temp) < cardinal(@starttext)) or
  608. (cardinal(temp) > cardinal(@endtext))) then
  609. begin
  610. errln('Bad signal handler, ');
  611. print_signal_name(sig);
  612. do_faulting_finish_message(djgpp_exception_state<>nil); { Exits, does not return }
  613. exit(-1);
  614. end;
  615. { WARNING !!! temp can be a pascal or a C
  616. function... thus %esp can be modified here !!!
  617. This might be dangerous for some optimizations ?? PM }
  618. temp(sig);
  619. exit(0);
  620. end;
  621. {$ifdef CREATE_C_FUNCTIONS}
  622. function c_raise(sig : longint) : longint;cdecl;[public,alias : '_raise'];
  623. begin
  624. c_raise:=_raise(sig);
  625. end;
  626. {$endif CREATE_C_FUNCTIONS}
  627. {****************************************************************************
  628. Exceptions
  629. ****************************************************************************}
  630. {$ifdef CREATE_C_FUNCTIONS}
  631. function except_to_sig(excep : longint) : longint;
  632. begin
  633. case excep of
  634. 5,8,9,11,12,13,14,
  635. 18, 19 : exit(SIGSEGV);
  636. 0,4,16 : exit(SIGFPE);
  637. 1,3 : exit(SIGTRAP);
  638. 7 : exit(SIGNOFP);
  639. else
  640. begin
  641. case excep of
  642. $75 : exit(SIGFPE);
  643. $78 : exit(SIGTIMR);
  644. $1b,
  645. $79 : exit(SIGINT);
  646. $7a : exit(SIGQUIT);
  647. else
  648. exit(SIGILL);
  649. end;
  650. end;
  651. end;
  652. end;
  653. procedure show_call_frame(djgpp_exception_state : pexception_state);
  654. begin
  655. errln('Call frame traceback EIPs:');
  656. errln(BackTraceStrFunc(djgpp_exception_state^.__eip));
  657. dump_stack(stderr,djgpp_exception_state^.__ebp);
  658. end;
  659. const
  660. EXCEPTIONCOUNT = 20;
  661. exception_names : array[0..EXCEPTIONCOUNT-1] of pchar = (
  662. 'Division by Zero',
  663. 'Debug',
  664. 'NMI',
  665. 'Breakpoint',
  666. 'Overflow',
  667. 'Bounds Check',
  668. 'Invalid Opcode',
  669. 'Coprocessor not available',
  670. 'Double Fault',
  671. 'Coprocessor overrun',
  672. 'Invalid TSS',
  673. 'Segment Not Present',
  674. 'Stack Fault',
  675. 'General Protection Fault',
  676. 'Page fault',
  677. ' ',
  678. 'Coprocessor Error',
  679. 'Alignment Check',
  680. 'Machine check',
  681. 'SIMD FP Error');
  682. has_error : array [0..EXCEPTIONCOUNT-1] of byte =
  683. (0,0,0,0,0,0,0,0,1,0,1,1,1,1,1,0,0,1,0,0);
  684. cbrk_hooked : boolean = false;
  685. old_video_mode : byte = 3;
  686. procedure dump_selector(const name : string; sel : word);
  687. var
  688. base,limit : longint;
  689. begin
  690. err(name);
  691. err(': sel=');
  692. itox(sel, 4);
  693. if (sel<>0) then
  694. begin
  695. base:=get_segment_base_address(sel);
  696. err(' base='); itox(base, 8);
  697. limit:=get_segment_limit(sel);
  698. err(' limit='); itox(limit, 8);
  699. end;
  700. errln('');
  701. end;
  702. function farpeekb(sel : word;offset : longint) : byte;
  703. var
  704. b : byte;
  705. begin
  706. {$ifdef IN_DPMIEXCP_UNIT}
  707. seg_move(sel,offset,get_ds,longint(@b),1);
  708. {$else not IN_DPMIEXCP_UNIT}
  709. sysseg_move(sel,offset,get_ds,longint(@b),1);
  710. {$endif IN_DPMIEXCP_UNIT}
  711. farpeekb:=b;
  712. end;
  713. const message_level : byte = 0;
  714. {$ifdef IN_DPMIEXCP_UNIT}
  715. procedure ___exit(c:longint);cdecl;external name '___exit';
  716. {$endif}
  717. {$endif CREATE_C_FUNCTIONS}
  718. function do_faulting_finish_message(fake : boolean) : integer;cdecl;
  719. {$ifndef CREATE_C_FUNCTIONS}
  720. external;
  721. {$else CREATE_C_FUNCTIONS}
  722. public;
  723. var
  724. en : pchar;
  725. signum,i : longint;
  726. old_vid : byte;
  727. label
  728. simple_exit;
  729. begin
  730. inc(message_level);
  731. if message_level>2 then
  732. goto simple_exit;
  733. do_faulting_finish_message:=0;
  734. signum:=djgpp_exception_state_ptr^.__signum;
  735. { check video mode for original here and reset (not if PC98) */ }
  736. if ((go32_info_block.linear_address_of_primary_screen <> $a0000) and
  737. (farpeekb(dosmemselector, $449) <> old_video_mode)) then
  738. begin
  739. old_vid:=old_video_mode;
  740. asm
  741. pusha
  742. movzbl old_vid,%eax
  743. int $0x10
  744. popa
  745. nop
  746. end;
  747. end;
  748. if (signum >= EXCEPTIONCOUNT) then
  749. begin
  750. case signum of
  751. $75 : en:='Floating Point exception';
  752. $1b : en:='Control-Break Pressed';
  753. $79 : en:='Control-C Pressed';
  754. else
  755. en:=nil;
  756. end;
  757. end
  758. else
  759. en:=exception_names[signum];
  760. if (en = nil) then
  761. begin
  762. if fake then
  763. err('Raised ')
  764. else
  765. err('Exception ');
  766. itox(signum, 2);
  767. err(' at eip=');
  768. itox(djgpp_exception_state_ptr^.__eip, 8);
  769. end
  770. else
  771. begin
  772. write(stderr, 'FPC ',en);
  773. err(' at eip=');
  774. itox(djgpp_exception_state_ptr^.__eip, 8);
  775. end;
  776. { Control-C should stop the program also !}
  777. {if (signum = $79) then
  778. begin
  779. errln('');
  780. exit(-1);
  781. end;}
  782. if ((signum < EXCEPTIONCOUNT) and (has_error[signum]=1)) then
  783. begin
  784. errorcode := djgpp_exception_state_ptr^.__sigmask and $ffff;
  785. if(errorcode<>0) then
  786. begin
  787. err(', error=');
  788. itox(errorcode, 4);
  789. end;
  790. end;
  791. errln('');
  792. err('eax=');
  793. itox(djgpp_exception_state_ptr^.__eax, 8);
  794. err(' ebx='); itox(djgpp_exception_state_ptr^.__ebx, 8);
  795. err(' ecx='); itox(djgpp_exception_state_ptr^.__ecx, 8);
  796. err(' edx='); itox(djgpp_exception_state_ptr^.__edx, 8);
  797. err(' esi='); itox(djgpp_exception_state_ptr^.__esi, 8);
  798. err(' edi='); itox(djgpp_exception_state_ptr^.__edi, 8);
  799. errln('');
  800. err('ebp='); itox(djgpp_exception_state_ptr^.__ebp, 8);
  801. err(' esp='); itox(djgpp_exception_state_ptr^.__esp, 8);
  802. err(' program=');
  803. errln(paramstr(0));
  804. dump_selector('cs', djgpp_exception_state_ptr^.__cs);
  805. dump_selector('ds', djgpp_exception_state_ptr^.__ds);
  806. dump_selector('es', djgpp_exception_state_ptr^.__es);
  807. dump_selector('fs', djgpp_exception_state_ptr^.__fs);
  808. dump_selector('gs', djgpp_exception_state_ptr^.__gs);
  809. dump_selector('ss', djgpp_exception_state_ptr^.__ss);
  810. errln('');
  811. if (djgpp_exception_state_ptr^.__cs = get_cs) then
  812. show_call_frame(djgpp_exception_state_ptr)
  813. {$ifdef DPMIEXCP_DEBUG}
  814. else
  815. errln('Exception occured in another context');
  816. {$endif def DPMIEXCP_DEBUG}
  817. ;
  818. if assigned(djgpp_exception_state_ptr^.__exception_ptr) then
  819. if (djgpp_exception_state_ptr^.__exception_ptr^.__cs = get_cs) then
  820. begin
  821. Errln('First exception level stack');
  822. show_call_frame(djgpp_exception_state_ptr^.__exception_ptr);
  823. end
  824. {$ifdef DPMIEXCP_DEBUG}
  825. else
  826. begin
  827. errln('First exception occured in another context');
  828. djgpp_exception_state_ptr:=djgpp_exception_state_ptr^.__exception_ptr;
  829. do_faulting_finish_message(false);
  830. end;
  831. {$endif def DPMIEXCP_DEBUG}
  832. ;
  833. { must not return !! }
  834. simple_exit:
  835. if exceptions_on then
  836. djgpp_exception_toggle;
  837. ___exit(-1);
  838. end;
  839. {$endif CREATE_C_FUNCTIONS}
  840. function djgpp_exception_state:pexception_state;assembler;
  841. asm
  842. movl djgpp_exception_state_ptr,%eax
  843. end;
  844. {$ifdef CREATE_C_FUNCTIONS}
  845. procedure djgpp_exception_processor;[public,alias : '___djgpp_exception_processor'];
  846. var
  847. sig : longint;
  848. begin
  849. if not assigned(djgpp_exception_state_ptr^.__exception_ptr) then
  850. exception_level:=1
  851. else
  852. inc(exception_level);
  853. sig:=djgpp_exception_state_ptr^.__signum;
  854. if (exception_level=1) or (sig=$78) then
  855. begin
  856. sig := except_to_sig(sig);
  857. if signal_list[djgpp_exception_state_ptr^.__signum]
  858. <>SignalHandler(@SIG_DFL) then
  859. _raise(djgpp_exception_state_ptr^.__signum)
  860. else
  861. _raise(sig);
  862. if (djgpp_exception_state_ptr^.__signum >= EXCEPTIONCOUNT) then
  863. { Not exception so continue OK }
  864. dpmi_longjmp(pdpmi_jmp_buf(djgpp_exception_state_ptr)^, djgpp_exception_state_ptr^.__eax);
  865. { User handler did not exit or longjmp, we must exit }
  866. err('FPC cannot continue from exception, exiting due to signal ');
  867. itox(sig, 4);
  868. errln('');
  869. end
  870. else
  871. begin
  872. if exception_level>2 then
  873. begin
  874. if exception_level=3 then
  875. errln('FPC triple exception, exiting !!! ');
  876. if (exceptions_on) then
  877. djgpp_exception_toggle;
  878. ___exit(1);
  879. end;
  880. err('FPC double exception, exiting due to signal ');
  881. itox(sig, 4);
  882. errln('');
  883. end;
  884. do_faulting_finish_message(djgpp_exception_state<>nil);
  885. end;
  886. type
  887. trealseginfo = tseginfo;
  888. pseginfo = ^tseginfo;
  889. var
  890. except_ori : array [0..EXCEPTIONCOUNT-1] of tseginfo;
  891. {$ifdef DPMIEXCP_DEBUG}
  892. export name '_ori_exceptions';
  893. {$endif def DPMIEXCP_DEBUG}
  894. kbd_ori : tseginfo;
  895. npx_ori : tseginfo;
  896. cbrk_ori,
  897. cbrk_rmcb : trealseginfo;
  898. cbrk_regs : trealregs;
  899. v2prt0_exceptions_on : longbool;external name '_v2prt0_exceptions_on';
  900. procedure djgpp_exception_toggle;
  901. [public,alias : '___djgpp_exception_toggle'];
  902. var
  903. _except : tseginfo;
  904. i : longint;
  905. begin
  906. {$ifdef DPMIEXCP_DEBUG}
  907. if exceptions_on then
  908. errln('Disabling FPC exceptions')
  909. else
  910. errln('Enabling FPC exceptions');
  911. {$endif DPMIEXCP_DEBUG}
  912. { toggle here to avoid infinite recursion }
  913. { if a subfunction calls runerror !! }
  914. exceptions_on:=not exceptions_on;
  915. v2prt0_exceptions_on:=exceptions_on;
  916. for i:=0 to EXCEPTIONCOUNT-1 do
  917. begin
  918. if get_pm_exception_handler(i,_except) then
  919. begin
  920. if (i <> 2) {or (_crt0_startup_flags & _CRT0_FLAG_NMI_SIGNAL))} then
  921. begin
  922. if not set_pm_exception_handler(i,except_ori[i]) then
  923. errln('error setting exception nø'+hexstr(i,2));
  924. end;
  925. except_ori[i]:=_except;
  926. end
  927. else
  928. begin
  929. if get_exception_handler(i,_except) then
  930. begin
  931. if (i <> 2) {or (_crt0_startup_flags & _CRT0_FLAG_NMI_SIGNAL))} then
  932. begin
  933. if not set_exception_handler(i,except_ori[i]) then
  934. errln('error setting exception nø'+hexstr(i,2));
  935. end;
  936. except_ori[i]:=_except;
  937. end;
  938. end;
  939. end;
  940. get_pm_interrupt($75,_except);
  941. set_pm_interrupt($75,npx_ori);
  942. npx_ori:=_except;
  943. get_pm_interrupt(9,_except);
  944. set_pm_interrupt(9,kbd_ori);
  945. kbd_ori:=_except;
  946. if (cbrk_hooked) then
  947. begin
  948. set_rm_interrupt(cbrk_vect,cbrk_ori);
  949. free_rm_callback(cbrk_rmcb);
  950. cbrk_hooked := false;
  951. {$ifdef DPMIEXCP_DEBUG}
  952. errln('back to ori rm cbrk '+hexstr(cbrk_ori.segment,4)+':'+hexstr(longint(cbrk_ori.offset),4));
  953. {$endif DPMIEXCP_DEBUG}
  954. end
  955. else
  956. begin
  957. get_rm_interrupt(cbrk_vect, cbrk_ori);
  958. {$ifdef DPMIEXCP_DEBUG}
  959. errln('ori rm cbrk '+hexstr(cbrk_ori.segment,4)+':'+hexstr(longint(cbrk_ori.offset),4));
  960. {$endif DPMIEXCP_DEBUG}
  961. get_rm_callback(@djgpp_cbrk_hdlr, cbrk_regs, cbrk_rmcb);
  962. set_rm_interrupt(cbrk_vect, cbrk_rmcb);
  963. {$ifdef DPMIEXCP_DEBUG}
  964. errln('now rm cbrk '+hexstr(cbrk_rmcb.segment,4)+':'+hexstr(longint(cbrk_rmcb.offset),4));
  965. {$endif DPMIEXCP_DEBUG}
  966. cbrk_hooked := true;
  967. end;
  968. end;
  969. {$endif CREATE_C_FUNCTIONS}
  970. function dpmi_set_coprocessor_emulation(flag : longint) : longint;
  971. var
  972. res : longint;
  973. begin
  974. asm
  975. movl flag,%ebx
  976. movl $0xe01,%eax
  977. int $0x31
  978. jc .L_coproc_error
  979. xorl %eax,%eax
  980. .L_coproc_error:
  981. movl %eax,res
  982. end;
  983. dpmi_set_coprocessor_emulation:=res;
  984. end;
  985. {$ifdef CREATE_C_FUNCTIONS}
  986. var
  987. _swap_in : pointer;external name '_swap_in';
  988. _swap_out : pointer;external name '_swap_out';
  989. _exception_exit : pointer;external name '_exception_exit';
  990. procedure dpmiexcp_exit{(status : longint)};[public,alias : 'excep_exit'];
  991. { We need to restore hardware interrupt handlers even if somebody calls
  992. `_exit' directly, or else we crash the machine in nested programs.
  993. We only toggle the handlers if the original keyboard handler is intact
  994. (otherwise, they might have already toggled them). }
  995. begin
  996. if (exceptions_on) then
  997. djgpp_exception_toggle;
  998. _exception_exit:=nil;
  999. _swap_in:=nil;
  1000. _swap_out:=nil;
  1001. { restore the FPU state }
  1002. dpmi_set_coprocessor_emulation(1);
  1003. end;
  1004. { _exit in dpmiexcp.c
  1005. is already present in v2prt0.as PM}
  1006. { used by dos.pp for swap vectors }
  1007. procedure dpmi_swap_in;[public,alias : 'swap_in'];
  1008. begin
  1009. if not (exceptions_on) then
  1010. djgpp_exception_toggle;
  1011. end;
  1012. procedure dpmi_swap_out;[public,alias : 'swap_out'];
  1013. begin
  1014. if (exceptions_on) then
  1015. djgpp_exception_toggle;
  1016. end;
  1017. var
  1018. ___djgpp_app_DS : word;external name '___djgpp_app_DS';
  1019. ___djgpp_our_DS : word;external name '___djgpp_our_DS';
  1020. __djgpp_sigint_mask : word;external name '___djgpp_sigint_mask';
  1021. __djgpp_sigint_key : word;external name '___djgpp_sigint_key';
  1022. __djgpp_sigquit_mask : word;external name '___djgpp_sigquit_mask';
  1023. __djgpp_sigquit_key : word;external name '___djgpp_sigquit_key';
  1024. { to avoid loading of C lib version of dpmiexcp
  1025. I need to have all exported assembler labels
  1026. of dpmiexcp.c in this unit.
  1027. DJGPP v2.03 add to new functions:
  1028. __djgpp_set_sigint_key
  1029. __djgpp_set_sigquit_key
  1030. that I implement here simply translating C code PM }
  1031. Const
  1032. LSHIFT = 1;
  1033. RSHIFT = 2;
  1034. CTRL = 4;
  1035. ALT = 8;
  1036. DEFAULT_SIGINT = $042e; { Ctrl-C: scan code 2Eh, kb status 04h }
  1037. DEFAULT_SIGQUIT = $042b; { Ctrl-\: scan code 2Bh, kb status 04h }
  1038. DEFAULT_SIGINT_98 = $042b; { Ctrl-C: scan code 2Bh, kb status 04h }
  1039. DEFAULT_SIGQUIT_98 = $040d; { Ctrl-\: scan code 0Dh, kb status 04h }
  1040. { Make it so the key NEW_KEY will generate the signal SIG.
  1041. NEW_KEY must include the keyboard status byte in bits 8-15 and the
  1042. scan code in bits 0-7. }
  1043. function set_signal_key(sig,new_key : longint) : longint;
  1044. type
  1045. pword = ^word;
  1046. var
  1047. old_key : longint;
  1048. mask,key : pword;
  1049. kb_status : word;
  1050. begin
  1051. if (sig = SIGINT) then
  1052. begin
  1053. mask := @__djgpp_sigint_mask;
  1054. key := @__djgpp_sigint_key;
  1055. end
  1056. else if (sig = SIGQUIT) then
  1057. begin
  1058. mask := @__djgpp_sigquit_mask;
  1059. key := @__djgpp_sigquit_key;
  1060. end
  1061. else
  1062. exit(-1);
  1063. old_key := key^;
  1064. key^ := new_key and $ffff;
  1065. kb_status := key^ shr 8;
  1066. mask^ := $f; { Alt, Ctrl and Shift bits only }
  1067. { Mask off the RShift bit unless they explicitly asked for it.
  1068. Our keyboard handler pretends that LShift is pressed when they
  1069. press RShift. }
  1070. if ((kb_status and RSHIFT) = 0) then
  1071. mask^ :=mask^ and not RSHIFT;
  1072. { Mask off the LShift bit if any of the Ctrl or Alt are set
  1073. since Shift doesn't matter when Ctrl and/or Alt are pressed. }
  1074. if (kb_status and (CTRL or ALT))<>0 then
  1075. mask^:= mask^ and not LSHIFT;
  1076. exit(old_key);
  1077. end;
  1078. function __djgpp_set_sigint_key(new_key : longint) : longint;cdecl;
  1079. begin
  1080. __djgpp_set_sigint_key:=set_signal_key(SIGINT, new_key);
  1081. end;
  1082. function __djgpp_set_sigquit_key(new_key : longint) : longint;cdecl;
  1083. begin
  1084. __djgpp_set_sigquit_key:=set_signal_key(SIGQUIT, new_key);
  1085. end;
  1086. function __djgpp__traceback_exit(sig : longint) : longint;cdecl;
  1087. var
  1088. fake_exception : texception_state;
  1089. begin
  1090. if (sig >= SIGABRT) and (sig <= SIGTRAP) then
  1091. begin
  1092. if djgpp_exception_state_ptr=nil then
  1093. begin
  1094. { This is a software signal, like SIGABRT or SIGKILL.
  1095. Fill the exception structure, so we get the traceback. }
  1096. djgpp_exception_state_ptr:=@fake_exception;
  1097. if (dpmi_setjmp(pdpmi_jmp_buf(djgpp_exception_state_ptr)^)<>0) then
  1098. begin
  1099. errln('Bad longjmp to __djgpp_exception_state--aborting');
  1100. do_faulting_finish_message(true); { does not return }
  1101. end
  1102. else
  1103. { Fake the exception number. 7Ah is the last one hardwired
  1104. inside exceptn.S, for SIGQUIT. }
  1105. djgpp_exception_state_ptr^.__signum:=$7a + 1 + sig - SIGABRT;
  1106. end;
  1107. end;
  1108. print_signal_name(sig);
  1109. if assigned(djgpp_exception_state_ptr) then
  1110. { This exits, does not return. }
  1111. do_faulting_finish_message(djgpp_exception_state_ptr=@fake_exception);
  1112. ___exit(-1);
  1113. __djgpp__traceback_exit:=0;
  1114. end;
  1115. procedure djgpp_exception_setup;
  1116. [alias : '___djgpp_exception_setup'];
  1117. var
  1118. temp_kbd,
  1119. temp_npx : pointer;
  1120. _except,
  1121. old_kbd : tseginfo;
  1122. locksize : longint;
  1123. i : longint;
  1124. begin
  1125. if assigned(_exception_exit) then
  1126. exit;
  1127. if (go32_info_block.linear_address_of_primary_screen <> $a0000) then
  1128. begin
  1129. __djgpp_set_sigint_key(DEFAULT_SIGINT);
  1130. __djgpp_set_sigquit_key(DEFAULT_SIGQUIT);
  1131. end
  1132. else
  1133. begin { for PC98 }
  1134. __djgpp_set_sigint_key(DEFAULT_SIGINT_98);
  1135. __djgpp_set_sigquit_key(DEFAULT_SIGQUIT_98);
  1136. end;
  1137. _exception_exit:=@dpmiexcp_exit;
  1138. _swap_in:=@dpmi_swap_in;
  1139. _swap_out:=@dpmi_swap_out;
  1140. { reset signals }
  1141. for i := 0 to SIGMAX do
  1142. signal_list[i] := SignalHandler(@SIG_DFL);
  1143. { app_DS only used when converting HW interrupts to exceptions }
  1144. asm
  1145. movw %ds,___djgpp_app_DS
  1146. movw %ds,___djgpp_our_DS
  1147. end;
  1148. djgpp_dos_sel:=dosmemselector;
  1149. { lock addresses which may see HW interrupts }
  1150. lock_code(@djgpp_hw_lock_start,@djgpp_hw_lock_end-@djgpp_hw_lock_start);
  1151. _except.segment:=get_cs;
  1152. { the first 18 exceptions start at offset +8 since exception
  1153. #18 and #19 had to be put in front of the table. }
  1154. _except.offset:=@djgpp_exception_table + 8;
  1155. for i:=0 to ExceptionCount-3 do
  1156. begin
  1157. except_ori[i] := _except; { New value to set }
  1158. inc(_except.offset,4); { This is the size of push n, jmp }
  1159. end;
  1160. except_ori[18].offset := @djgpp_exception_table;
  1161. except_ori[19].offset := @djgpp_exception_table + 4;
  1162. kbd_ori.segment:=_except.segment;
  1163. npx_ori.segment:=_except.segment;
  1164. npx_ori.offset:=@djgpp_npx_hdlr;
  1165. if (go32_info_block.linear_address_of_primary_screen <> $a0000) then
  1166. kbd_ori.offset:=@djgpp_kbd_hdlr
  1167. else
  1168. begin
  1169. kbd_ori.offset:=@djgpp_kbd_hdlr_pc98;
  1170. cbrk_vect := $06;
  1171. _except.offset:=@djgpp_iret;
  1172. set_pm_interrupt($23,_except);
  1173. end;
  1174. _except.offset:=@djgpp_i24;
  1175. set_pm_interrupt($24, _except);
  1176. get_pm_interrupt(9,djgpp_old_kbd);
  1177. djgpp_exception_toggle; { Set new values & save old values }
  1178. { get original video mode and save }
  1179. old_video_mode := farpeekb(dosmemselector, $449);
  1180. end;
  1181. {$endif CREATE_C_FUNCTIONS}
  1182. function djgpp_set_ctrl_c(enable : boolean) : boolean;
  1183. begin
  1184. djgpp_set_ctrl_c:=(djgpp_hwint_flags and 1)=0;
  1185. if enable then
  1186. djgpp_hwint_flags:=djgpp_hwint_flags and (not 1)
  1187. else
  1188. djgpp_hwint_flags:=djgpp_hwint_flags or 1;
  1189. end;
  1190. {$ifdef CREATE_C_FUNCTIONS}
  1191. function c_djgpp_set_ctrl_c(enable : longint) : boolean;cdecl;[public,alias : '___djgpp_set_ctrl_c'];
  1192. begin
  1193. c_djgpp_set_ctrl_c:=djgpp_set_ctrl_c(boolean(enable));
  1194. end;
  1195. {$endif def CREATE_C_FUNCTIONS}
  1196. {$ifdef IN_DPMIEXCP_UNIT}
  1197. procedure ResetDefaultHandlers;
  1198. begin
  1199. Signal(SIGSEGV,@SIG_DFL);
  1200. Signal(SIGFPE,@SIG_DFL);
  1201. Signal(SIGNOFP,@SIG_DFL);
  1202. Signal(SIGTRAP,@SIG_DFL);
  1203. Signal(SIGTIMR,@SIG_DFL);
  1204. Signal(SIGINT,@SIG_DFL);
  1205. Signal(SIGQUIT,@SIG_DFL);
  1206. Signal(SIGILL,@SIG_DFL);
  1207. end;
  1208. {$endif IN_DPMIEXCP_UNIT}
  1209. procedure InitDPMIExcp;
  1210. begin
  1211. {$ifdef CREATE_C_FUNCTIONS}
  1212. djgpp_ds_alias:=v2prt0_ds_alias;
  1213. djgpp_exception_setup;
  1214. {$endif CREATE_C_FUNCTIONS}
  1215. end;
  1216. {$ifndef IN_SYSTEM}
  1217. begin
  1218. {$ifdef CREATE_C_FUNCTIONS}
  1219. InitDPMIExcp;
  1220. {$else not CREATE_C_FUNCTIONS}
  1221. ResetDefaultHandlers;
  1222. {$endif CREATE_C_FUNCTIONS}
  1223. end.
  1224. {$else IN_SYSTEM}
  1225. const
  1226. FPU_Invalid = 1;
  1227. FPU_Denormal = 2;
  1228. FPU_DivisionByZero = 4;
  1229. FPU_Overflow = 8;
  1230. FPU_Underflow = $10;
  1231. FPU_StackUnderflow = $20;
  1232. FPU_StackOverflow = $40;
  1233. FPU_ExceptionMask = $ff;
  1234. FPU_ControlWord : word = $1332;
  1235. function HandleException(sig : longint) : longint;
  1236. var
  1237. truesig : longint;
  1238. ErrorOfSig : longint;
  1239. FpuStatus,FPUControl : word;
  1240. eip,ebp : longint;
  1241. begin
  1242. if assigned(djgpp_exception_state_ptr) then
  1243. truesig:=djgpp_exception_state_ptr^.__signum
  1244. else
  1245. truesig:=sig;
  1246. ErrorOfSig:=0;
  1247. case truesig of
  1248. {exception_names : array[0..EXCEPTIONCOUNT-1] of pchar = (}
  1249. 0 : ErrorOfSig:=200; {'Division by Zero'}
  1250. 5 : ErrorOfSig:=201; {'Bounds Check'}
  1251. 12 : ErrorOfSig:=202; {'Stack Fault'}
  1252. 7, {'Coprocessor not available'}
  1253. 9, {'Coprocessor overrun'}
  1254. SIGNOFP : ErrorOfSig:=207;
  1255. 16,SIGFPE,$75 : begin
  1256. { This needs special handling }
  1257. { to discriminate between 205,206 and 207 }
  1258. if truesig=$75 then
  1259. fpustatus:=djgpp_exception_state_ptr^.__sigmask and $ffff
  1260. else
  1261. asm
  1262. fnstsw %ax
  1263. fnclex
  1264. movw %ax,fpustatus
  1265. end;
  1266. if (FpuStatus and FPU_Invalid)<>0 then
  1267. ErrorOfSig:=216
  1268. else if (FpuStatus and FPU_Denormal)<>0 then
  1269. ErrorOfSig:=216
  1270. else if (FpuStatus and FPU_DivisionByZero)<>0 then
  1271. ErrorOfSig:=200
  1272. else if (FpuStatus and FPU_Overflow)<>0 then
  1273. ErrorOfSig:=205
  1274. else if (FpuStatus and FPU_Underflow)<>0 then
  1275. ErrorOfSig:=206
  1276. else
  1277. ErrorOfSig:=207; {'Coprocessor Error'}
  1278. { if exceptions then Reset FPU and reload control word }
  1279. if (FPUStatus and FPU_ExceptionMask)<>0 then
  1280. asm
  1281. fninit
  1282. fldcw FPU_ControlWord
  1283. end;
  1284. end;
  1285. 4 : ErrorOfSig:=215; {'Overflow'}
  1286. 1, {'Debug'}
  1287. 2, {'NMI'}
  1288. 3, {'Breakpoint'}
  1289. 6, {'Invalid Opcode'}
  1290. 8, {'Double Fault'}
  1291. 10, {'Invalid TSS'}
  1292. 11, {'Segment Not Present'}
  1293. 13, {'General Protection Fault'}
  1294. 14, {'Page fault'}
  1295. 15, {' ',}
  1296. 17, {'Alignment Check',}
  1297. 18, {'Machine Check',}
  1298. 19, {'SSE FP error'}
  1299. SIGSEGV,SIGTRAP,SIGTIMR,SIGINT,SIGQUIT
  1300. : ErrorOfSig:=216;
  1301. end;
  1302. if assigned(djgpp_exception_state_ptr) then
  1303. Begin
  1304. if exception_level>0 then
  1305. dec(exception_level);
  1306. eip:=djgpp_exception_state_ptr^.__eip;
  1307. ebp:=djgpp_exception_state_ptr^.__ebp;
  1308. djgpp_exception_state_ptr:=djgpp_exception_state_ptr^.__exception_ptr;
  1309. HandleErrorAddrFrame(ErrorOfSig,eip,ebp);
  1310. End
  1311. else
  1312. { probably higher level is required }
  1313. HandleErrorFrame(ErrorOfSig,get_caller_frame(get_frame));
  1314. HandleException:=0;
  1315. end;
  1316. procedure InstallDefaultHandlers;
  1317. begin
  1318. Signal(SIGSEGV,@HandleException);
  1319. Signal(SIGFPE,@HandleException);
  1320. Signal(SIGNOFP,@HandleException);
  1321. Signal(SIGTRAP,@HandleException);
  1322. Signal(SIGTIMR,@HandleException);
  1323. Signal(SIGINT,@HandleException);
  1324. Signal(SIGQUIT,@HandleException);
  1325. Signal(SIGILL,@HandleException);
  1326. end;
  1327. {$endif IN_SYSTEM}
  1328. {
  1329. $Log$
  1330. Revision 1.4 2000-10-05 21:56:45 pierre
  1331. + exceptions 18 and 19 contributed by Thomas Schatzl (merged)
  1332. Revision 1.3 2000/08/13 19:23:26 peter
  1333. * fixed double declared ___exit() (merged)
  1334. Revision 1.2 2000/07/13 11:33:39 michael
  1335. + removed logs
  1336. }