dpmiexcp.pp 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552
  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. [public, 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. var
  846. _os_trueversion : word;external name '__os_trueversion';
  847. procedure djgpp_exception_processor;[public,alias : '___djgpp_exception_processor'];
  848. var
  849. sig : longint;
  850. begin
  851. if not assigned(djgpp_exception_state_ptr^.__exception_ptr) then
  852. exception_level:=1
  853. else
  854. inc(exception_level);
  855. sig:=djgpp_exception_state_ptr^.__signum;
  856. if (exception_level=1) or (sig=$78) then
  857. begin
  858. sig := except_to_sig(sig);
  859. if signal_list[djgpp_exception_state_ptr^.__signum]
  860. <>SignalHandler(@SIG_DFL) then
  861. _raise(djgpp_exception_state_ptr^.__signum)
  862. else
  863. _raise(sig);
  864. if (djgpp_exception_state_ptr^.__signum >= EXCEPTIONCOUNT) then
  865. { Not exception so continue OK }
  866. dpmi_longjmp(pdpmi_jmp_buf(djgpp_exception_state_ptr)^, djgpp_exception_state_ptr^.__eax);
  867. { User handler did not exit or longjmp, we must exit }
  868. err('FPC cannot continue from exception, exiting due to signal ');
  869. itox(sig, 4);
  870. errln('');
  871. end
  872. else
  873. begin
  874. if exception_level>2 then
  875. begin
  876. if exception_level=3 then
  877. errln('FPC triple exception, exiting !!! ');
  878. if (exceptions_on) then
  879. djgpp_exception_toggle;
  880. ___exit(1);
  881. end;
  882. err('FPC double exception, exiting due to signal ');
  883. itox(sig, 4);
  884. errln('');
  885. end;
  886. do_faulting_finish_message(djgpp_exception_state<>nil);
  887. end;
  888. type
  889. trealseginfo = tseginfo;
  890. pseginfo = ^tseginfo;
  891. var
  892. except_ori : array [0..EXCEPTIONCOUNT-1] of tseginfo;
  893. {$ifdef DPMIEXCP_DEBUG}
  894. export name '_ori_exceptions';
  895. {$endif def DPMIEXCP_DEBUG}
  896. kbd_ori : tseginfo;
  897. int0_ori,
  898. npx_ori : tseginfo;
  899. cbrk_ori,
  900. cbrk_rmcb : trealseginfo;
  901. cbrk_regs : trealregs;
  902. v2prt0_exceptions_on : longbool;external name '_v2prt0_exceptions_on';
  903. procedure djgpp_exception_toggle;
  904. [public,alias : '___djgpp_exception_toggle'];
  905. var
  906. _except : tseginfo;
  907. i : longint;
  908. begin
  909. {$ifdef DPMIEXCP_DEBUG}
  910. if exceptions_on then
  911. errln('Disabling FPC exceptions')
  912. else
  913. errln('Enabling FPC exceptions');
  914. {$endif DPMIEXCP_DEBUG}
  915. { toggle here to avoid infinite recursion }
  916. { if a subfunction calls runerror !! }
  917. exceptions_on:=not exceptions_on;
  918. v2prt0_exceptions_on:=exceptions_on;
  919. { Exceptions 18 and 19 settings generates a bug in
  920. the DJGPP debug code PM }
  921. for i:=0 to 17{EXCEPTIONCOUNT-1} do
  922. begin
  923. {$ifdef DPMIEXCP_DEBUG}
  924. errln('new exception '+hexstr(i,2)+' '+hexstr(except_ori[i].segment,4)+':'+hexstr(longint(except_ori[i].offset),8));
  925. {$endif DPMIEXCP_DEBUG}
  926. { Windows 2000 seems to not set carryflag on func 0x210 :( PM }
  927. if (_os_trueversion <> $532) and get_pm_exception_handler(i,_except) then
  928. begin
  929. if (i <> 2) {or (_crt0_startup_flags & _CRT0_FLAG_NMI_SIGNAL))} then
  930. begin
  931. {$ifdef DPMIEXCP_DEBUG}
  932. errln('Using DPMI 1.0 functions');
  933. {$endif DPMIEXCP_DEBUG}
  934. if not set_pm_exception_handler(i,except_ori[i]) then
  935. errln('error setting exception nø'+hexstr(i,2));
  936. end;
  937. except_ori[i]:=_except;
  938. end
  939. else
  940. begin
  941. if get_exception_handler(i,_except) then
  942. begin
  943. {$ifdef DPMIEXCP_DEBUG}
  944. errln('Using DPMI 0.9 functions');
  945. {$endif DPMIEXCP_DEBUG}
  946. if (i <> 2) {or (_crt0_startup_flags & _CRT0_FLAG_NMI_SIGNAL))} then
  947. begin
  948. if not set_exception_handler(i,except_ori[i]) then
  949. errln('error setting exception nø'+hexstr(i,2));
  950. end;
  951. except_ori[i]:=_except;
  952. end;
  953. end;
  954. {$ifdef DPMIEXCP_DEBUG}
  955. errln('prev exception '+hexstr(i,2)+' '+hexstr(_except.segment,4)+':'+hexstr(longint(_except.offset),8));
  956. {$endif DPMIEXCP_DEBUG}
  957. end;
  958. get_pm_interrupt($75,_except);
  959. set_pm_interrupt($75,npx_ori);
  960. npx_ori:=_except;
  961. get_pm_interrupt($0,_except);
  962. set_pm_interrupt($0,int0_ori);
  963. int0_ori:=_except;
  964. get_pm_interrupt(9,_except);
  965. set_pm_interrupt(9,kbd_ori);
  966. kbd_ori:=_except;
  967. if (cbrk_hooked) then
  968. begin
  969. set_rm_interrupt(cbrk_vect,cbrk_ori);
  970. free_rm_callback(cbrk_rmcb);
  971. cbrk_hooked := false;
  972. {$ifdef DPMIEXCP_DEBUG}
  973. errln('back to ori rm cbrk '+hexstr(cbrk_ori.segment,4)+':'+hexstr(longint(cbrk_ori.offset),4));
  974. {$endif DPMIEXCP_DEBUG}
  975. end
  976. else
  977. begin
  978. get_rm_interrupt(cbrk_vect, cbrk_ori);
  979. {$ifdef DPMIEXCP_DEBUG}
  980. errln('ori rm cbrk '+hexstr(cbrk_ori.segment,4)+':'+hexstr(longint(cbrk_ori.offset),4));
  981. {$endif DPMIEXCP_DEBUG}
  982. get_rm_callback(@djgpp_cbrk_hdlr, cbrk_regs, cbrk_rmcb);
  983. set_rm_interrupt(cbrk_vect, cbrk_rmcb);
  984. {$ifdef DPMIEXCP_DEBUG}
  985. errln('now rm cbrk '+hexstr(cbrk_rmcb.segment,4)+':'+hexstr(longint(cbrk_rmcb.offset),4));
  986. {$endif DPMIEXCP_DEBUG}
  987. cbrk_hooked := true;
  988. end;
  989. end;
  990. {$endif CREATE_C_FUNCTIONS}
  991. function dpmi_set_coprocessor_emulation(flag : longint) : longint;
  992. var
  993. res : longint;
  994. begin
  995. asm
  996. movl flag,%ebx
  997. movl $0xe01,%eax
  998. int $0x31
  999. jc .L_coproc_error
  1000. xorl %eax,%eax
  1001. .L_coproc_error:
  1002. movl %eax,res
  1003. end;
  1004. dpmi_set_coprocessor_emulation:=res;
  1005. end;
  1006. {$ifdef CREATE_C_FUNCTIONS}
  1007. var
  1008. _swap_in : pointer;external name '_swap_in';
  1009. _swap_out : pointer;external name '_swap_out';
  1010. _exception_exit : pointer;external name '_exception_exit';
  1011. const
  1012. STUBINFO_END = $54;
  1013. procedure __maybe_fix_w2k_ntvdm_bug;[public,alias : '___maybe_fix_w2k_ntvdm_bug'];
  1014. var
  1015. psp_sel : word;
  1016. begin
  1017. if _os_trueversion = $532 then
  1018. begin
  1019. { avoid NTVDM bug on NT,2000 or XP }
  1020. { see dpmiexcp.c source of DJGPP PM }
  1021. if stub_info^.size < STUBINFO_END then
  1022. begin
  1023. asm
  1024. movb $0x51,%ah
  1025. int $0x21
  1026. movb $0x50,%ah
  1027. int $0x21
  1028. end;
  1029. end
  1030. else
  1031. begin
  1032. psp_sel:=stub_info^.psp_selector;
  1033. asm
  1034. movw psp_sel,%bx
  1035. movb $0x50,%ah
  1036. int $0x21
  1037. end;
  1038. end;
  1039. end;
  1040. end;
  1041. procedure dpmiexcp_exit{(status : longint)};[public,alias : 'excep_exit'];
  1042. { We need to restore hardware interrupt handlers even if somebody calls
  1043. `_exit' directly, or else we crash the machine in nested programs.
  1044. We only toggle the handlers if the original keyboard handler is intact
  1045. (otherwise, they might have already toggled them). }
  1046. begin
  1047. if (exceptions_on) then
  1048. djgpp_exception_toggle;
  1049. _exception_exit:=nil;
  1050. _swap_in:=nil;
  1051. _swap_out:=nil;
  1052. __maybe_fix_w2k_ntvdm_bug;
  1053. { restore the FPU state }
  1054. dpmi_set_coprocessor_emulation(1);
  1055. end;
  1056. { _exit in dpmiexcp.c
  1057. is already present in v2prt0.as PM}
  1058. { used by dos.pp for swap vectors }
  1059. procedure dpmi_swap_in;[public,alias : 'swap_in'];
  1060. begin
  1061. if not (exceptions_on) then
  1062. djgpp_exception_toggle;
  1063. end;
  1064. procedure dpmi_swap_out;[public,alias : 'swap_out'];
  1065. begin
  1066. if (exceptions_on) then
  1067. djgpp_exception_toggle;
  1068. end;
  1069. var
  1070. ___djgpp_app_DS : word;external name '___djgpp_app_DS';
  1071. ___djgpp_our_DS : word;external name '___djgpp_our_DS';
  1072. __djgpp_sigint_mask : word;external name '___djgpp_sigint_mask';
  1073. __djgpp_sigint_key : word;external name '___djgpp_sigint_key';
  1074. __djgpp_sigquit_mask : word;external name '___djgpp_sigquit_mask';
  1075. __djgpp_sigquit_key : word;external name '___djgpp_sigquit_key';
  1076. { to avoid loading of C lib version of dpmiexcp
  1077. I need to have all exported assembler labels
  1078. of dpmiexcp.c in this unit.
  1079. DJGPP v2.03 add to new functions:
  1080. __djgpp_set_sigint_key
  1081. __djgpp_set_sigquit_key
  1082. that I implement here simply translating C code PM }
  1083. Const
  1084. LSHIFT = 1;
  1085. RSHIFT = 2;
  1086. CTRL = 4;
  1087. ALT = 8;
  1088. DEFAULT_SIGINT = $042e; { Ctrl-C: scan code 2Eh, kb status 04h }
  1089. DEFAULT_SIGQUIT = $042b; { Ctrl-\: scan code 2Bh, kb status 04h }
  1090. DEFAULT_SIGINT_98 = $042b; { Ctrl-C: scan code 2Bh, kb status 04h }
  1091. DEFAULT_SIGQUIT_98 = $040d; { Ctrl-\: scan code 0Dh, kb status 04h }
  1092. { Make it so the key NEW_KEY will generate the signal SIG.
  1093. NEW_KEY must include the keyboard status byte in bits 8-15 and the
  1094. scan code in bits 0-7. }
  1095. function set_signal_key(sig,new_key : longint) : longint;
  1096. type
  1097. pword = ^word;
  1098. var
  1099. old_key : longint;
  1100. mask,key : pword;
  1101. kb_status : word;
  1102. begin
  1103. if (sig = SIGINT) then
  1104. begin
  1105. mask := @__djgpp_sigint_mask;
  1106. key := @__djgpp_sigint_key;
  1107. end
  1108. else if (sig = SIGQUIT) then
  1109. begin
  1110. mask := @__djgpp_sigquit_mask;
  1111. key := @__djgpp_sigquit_key;
  1112. end
  1113. else
  1114. exit(-1);
  1115. old_key := key^;
  1116. key^ := new_key and $ffff;
  1117. kb_status := key^ shr 8;
  1118. mask^ := $f; { Alt, Ctrl and Shift bits only }
  1119. { Mask off the RShift bit unless they explicitly asked for it.
  1120. Our keyboard handler pretends that LShift is pressed when they
  1121. press RShift. }
  1122. if ((kb_status and RSHIFT) = 0) then
  1123. mask^ :=mask^ and not RSHIFT;
  1124. { Mask off the LShift bit if any of the Ctrl or Alt are set
  1125. since Shift doesn't matter when Ctrl and/or Alt are pressed. }
  1126. if (kb_status and (CTRL or ALT))<>0 then
  1127. mask^:= mask^ and not LSHIFT;
  1128. exit(old_key);
  1129. end;
  1130. function __djgpp_set_sigint_key(new_key : longint) : longint;cdecl;
  1131. begin
  1132. __djgpp_set_sigint_key:=set_signal_key(SIGINT, new_key);
  1133. end;
  1134. function __djgpp_set_sigquit_key(new_key : longint) : longint;cdecl;
  1135. begin
  1136. __djgpp_set_sigquit_key:=set_signal_key(SIGQUIT, new_key);
  1137. end;
  1138. function __djgpp__traceback_exit(sig : longint) : longint;cdecl;
  1139. var
  1140. fake_exception : texception_state;
  1141. begin
  1142. if (sig >= SIGABRT) and (sig <= SIGTRAP) then
  1143. begin
  1144. if djgpp_exception_state_ptr=nil then
  1145. begin
  1146. { This is a software signal, like SIGABRT or SIGKILL.
  1147. Fill the exception structure, so we get the traceback. }
  1148. djgpp_exception_state_ptr:=@fake_exception;
  1149. if (dpmi_setjmp(pdpmi_jmp_buf(djgpp_exception_state_ptr)^)<>0) then
  1150. begin
  1151. errln('Bad longjmp to __djgpp_exception_state--aborting');
  1152. do_faulting_finish_message(true); { does not return }
  1153. end
  1154. else
  1155. { Fake the exception number. 7Ah is the last one hardwired
  1156. inside exceptn.S, for SIGQUIT. }
  1157. djgpp_exception_state_ptr^.__signum:=$7a + 1 + sig - SIGABRT;
  1158. end;
  1159. end;
  1160. print_signal_name(sig);
  1161. if assigned(djgpp_exception_state_ptr) then
  1162. { This exits, does not return. }
  1163. do_faulting_finish_message(djgpp_exception_state_ptr=@fake_exception);
  1164. ___exit(-1);
  1165. __djgpp__traceback_exit:=0;
  1166. end;
  1167. procedure djgpp_int0;
  1168. begin
  1169. HandleError(200);
  1170. end;
  1171. procedure djgpp_exception_setup;
  1172. [public,alias : '___djgpp_exception_setup'];
  1173. var
  1174. temp_kbd,
  1175. temp_npx : pointer;
  1176. _except,
  1177. old_kbd : tseginfo;
  1178. locksize : longint;
  1179. i : longint;
  1180. begin
  1181. if assigned(_exception_exit) then
  1182. exit;
  1183. if (go32_info_block.linear_address_of_primary_screen <> $a0000) then
  1184. begin
  1185. __djgpp_set_sigint_key(DEFAULT_SIGINT);
  1186. __djgpp_set_sigquit_key(DEFAULT_SIGQUIT);
  1187. end
  1188. else
  1189. begin { for PC98 }
  1190. __djgpp_set_sigint_key(DEFAULT_SIGINT_98);
  1191. __djgpp_set_sigquit_key(DEFAULT_SIGQUIT_98);
  1192. end;
  1193. _exception_exit:=@dpmiexcp_exit;
  1194. _swap_in:=@dpmi_swap_in;
  1195. _swap_out:=@dpmi_swap_out;
  1196. { reset signals }
  1197. for i := 0 to SIGMAX do
  1198. signal_list[i] := SignalHandler(@SIG_DFL);
  1199. { app_DS only used when converting HW interrupts to exceptions }
  1200. asm
  1201. movw %ds,___djgpp_app_DS
  1202. movw %ds,___djgpp_our_DS
  1203. end;
  1204. djgpp_dos_sel:=dosmemselector;
  1205. { lock addresses which may see HW interrupts }
  1206. lock_code(@djgpp_hw_lock_start,@djgpp_hw_lock_end-@djgpp_hw_lock_start);
  1207. _except.segment:=get_cs;
  1208. { the first 18 exceptions start at offset +8 since exception
  1209. #18 and #19 had to be put in front of the table. }
  1210. _except.offset:=@djgpp_exception_table + 8;
  1211. for i:=0 to 17 do
  1212. begin
  1213. except_ori[i] := _except; { New value to set }
  1214. inc(_except.offset,4); { This is the size of push n, jmp }
  1215. end;
  1216. except_ori[18].segment := _except.segment;
  1217. except_ori[19].segment := _except.segment;
  1218. except_ori[18].offset := @djgpp_exception_table;
  1219. except_ori[19].offset := @djgpp_exception_table + 4;
  1220. kbd_ori.segment:=_except.segment;
  1221. npx_ori.segment:=_except.segment;
  1222. npx_ori.offset:=@djgpp_npx_hdlr;
  1223. int0_ori.segment:=_except.segment;
  1224. int0_ori.offset:=@djgpp_int0;
  1225. if (go32_info_block.linear_address_of_primary_screen <> $a0000) then
  1226. kbd_ori.offset:=@djgpp_kbd_hdlr
  1227. else
  1228. begin
  1229. kbd_ori.offset:=@djgpp_kbd_hdlr_pc98;
  1230. cbrk_vect := $06;
  1231. _except.offset:=@djgpp_iret;
  1232. set_pm_interrupt($23,_except);
  1233. end;
  1234. _except.offset:=@djgpp_i24;
  1235. set_pm_interrupt($24, _except);
  1236. get_pm_interrupt(9,djgpp_old_kbd);
  1237. djgpp_exception_toggle; { Set new values & save old values }
  1238. { get original video mode and save }
  1239. old_video_mode := farpeekb(dosmemselector, $449);
  1240. end;
  1241. {$endif CREATE_C_FUNCTIONS}
  1242. function djgpp_set_ctrl_c(enable : boolean) : boolean;
  1243. begin
  1244. djgpp_set_ctrl_c:=(djgpp_hwint_flags and 1)=0;
  1245. if enable then
  1246. djgpp_hwint_flags:=djgpp_hwint_flags and (not 1)
  1247. else
  1248. djgpp_hwint_flags:=djgpp_hwint_flags or 1;
  1249. end;
  1250. {$ifdef CREATE_C_FUNCTIONS}
  1251. function c_djgpp_set_ctrl_c(enable : longint) : boolean;cdecl;[public,alias : '___djgpp_set_ctrl_c'];
  1252. begin
  1253. c_djgpp_set_ctrl_c:=djgpp_set_ctrl_c(boolean(enable));
  1254. end;
  1255. {$endif def CREATE_C_FUNCTIONS}
  1256. {$ifdef IN_DPMIEXCP_UNIT}
  1257. procedure ResetDefaultHandlers;
  1258. begin
  1259. Signal(SIGSEGV,@SIG_DFL);
  1260. Signal(SIGFPE,@SIG_DFL);
  1261. Signal(SIGNOFP,@SIG_DFL);
  1262. Signal(SIGTRAP,@SIG_DFL);
  1263. Signal(SIGTIMR,@SIG_DFL);
  1264. Signal(SIGINT,@SIG_DFL);
  1265. Signal(SIGQUIT,@SIG_DFL);
  1266. Signal(SIGILL,@SIG_DFL);
  1267. end;
  1268. {$endif IN_DPMIEXCP_UNIT}
  1269. procedure InitDPMIExcp;
  1270. begin
  1271. {$ifdef CREATE_C_FUNCTIONS}
  1272. djgpp_ds_alias:=v2prt0_ds_alias;
  1273. djgpp_exception_setup;
  1274. {$endif CREATE_C_FUNCTIONS}
  1275. end;
  1276. {$ifndef IN_SYSTEM}
  1277. begin
  1278. {$ifdef CREATE_C_FUNCTIONS}
  1279. InitDPMIExcp;
  1280. {$else not CREATE_C_FUNCTIONS}
  1281. ResetDefaultHandlers;
  1282. {$endif CREATE_C_FUNCTIONS}
  1283. end.
  1284. {$else IN_SYSTEM}
  1285. const
  1286. FPU_ControlWord : word = $1332;
  1287. function HandleException(sig : longint) : longint;
  1288. var
  1289. truesig : longint;
  1290. ErrorOfSig : longint;
  1291. FpuStatus,FPUControl : word;
  1292. eip,ebp : longint;
  1293. begin
  1294. if assigned(djgpp_exception_state_ptr) then
  1295. truesig:=djgpp_exception_state_ptr^.__signum
  1296. else
  1297. truesig:=sig;
  1298. ErrorOfSig:=0;
  1299. case truesig of
  1300. {exception_names : array[0..EXCEPTIONCOUNT-1] of pchar = (}
  1301. 0 : ErrorOfSig:=200; {'Division by Zero'}
  1302. 5 : ErrorOfSig:=201; {'Bounds Check'}
  1303. 12 : ErrorOfSig:=202; {'Stack Fault'}
  1304. 7, {'Coprocessor not available'}
  1305. 9, {'Coprocessor overrun'}
  1306. SIGNOFP : ErrorOfSig:=207;
  1307. 16,SIGFPE,$75 : begin
  1308. { This needs special handling }
  1309. { to discriminate between 205,206 and 207 }
  1310. if truesig=$75 then
  1311. fpustatus:=djgpp_exception_state_ptr^.__sigmask and $ffff
  1312. else
  1313. asm
  1314. fnstsw %ax
  1315. fnclex
  1316. movw %ax,fpustatus
  1317. end;
  1318. if (FpuStatus and FPU_Invalid)<>0 then
  1319. ErrorOfSig:=216
  1320. else if (FpuStatus and FPU_Denormal)<>0 then
  1321. ErrorOfSig:=216
  1322. else if (FpuStatus and FPU_DivisionByZero)<>0 then
  1323. ErrorOfSig:=200
  1324. else if (FpuStatus and FPU_Overflow)<>0 then
  1325. ErrorOfSig:=205
  1326. else if (FpuStatus and FPU_Underflow)<>0 then
  1327. ErrorOfSig:=206
  1328. else
  1329. ErrorOfSig:=207; {'Coprocessor Error'}
  1330. { if exceptions then Reset FPU and reload control word }
  1331. if (FPUStatus and FPU_ExceptionMask)<>0 then
  1332. asm
  1333. fninit
  1334. fldcw FPU_ControlWord
  1335. end;
  1336. end;
  1337. 4 : ErrorOfSig:=215; {'Overflow'}
  1338. 1, {'Debug'}
  1339. 2, {'NMI'}
  1340. 3, {'Breakpoint'}
  1341. 6, {'Invalid Opcode'}
  1342. 8, {'Double Fault'}
  1343. 10, {'Invalid TSS'}
  1344. 11, {'Segment Not Present'}
  1345. 13, {'General Protection Fault'}
  1346. 14, {'Page fault'}
  1347. 15, {' ',}
  1348. 17, {'Alignment Check',}
  1349. 18, {'Machine Check',}
  1350. 19, {'SSE FP error'}
  1351. SIGSEGV,SIGTRAP,SIGTIMR,SIGINT,SIGQUIT
  1352. : ErrorOfSig:=216;
  1353. end;
  1354. if assigned(djgpp_exception_state_ptr) then
  1355. Begin
  1356. if exception_level>0 then
  1357. dec(exception_level);
  1358. eip:=djgpp_exception_state_ptr^.__eip;
  1359. ebp:=djgpp_exception_state_ptr^.__ebp;
  1360. djgpp_exception_state_ptr:=djgpp_exception_state_ptr^.__exception_ptr;
  1361. HandleErrorAddrFrame(ErrorOfSig,eip,ebp);
  1362. End
  1363. else
  1364. { probably higher level is required }
  1365. HandleErrorFrame(ErrorOfSig,get_caller_frame(get_frame));
  1366. HandleException:=0;
  1367. end;
  1368. procedure InstallDefaultHandlers;
  1369. begin
  1370. Signal(SIGSEGV,@HandleException);
  1371. Signal(SIGFPE,@HandleException);
  1372. Signal(SIGNOFP,@HandleException);
  1373. Signal(SIGTRAP,@HandleException);
  1374. Signal(SIGTIMR,@HandleException);
  1375. Signal(SIGINT,@HandleException);
  1376. Signal(SIGQUIT,@HandleException);
  1377. Signal(SIGILL,@HandleException);
  1378. end;
  1379. {$endif IN_SYSTEM}
  1380. {
  1381. $Log$
  1382. Revision 1.11 2002-10-14 19:39:16 peter
  1383. * threads unit added for thread support
  1384. Revision 1.10 2002/09/07 16:01:18 peter
  1385. * old logs removed and tabs fixed
  1386. Revision 1.9 2002/02/03 09:51:41 peter
  1387. * merged winxp fixes
  1388. Revision 1.8 2002/01/25 16:23:03 peter
  1389. * merged filesearch() fix
  1390. }