x86.pp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1997-2004 by the Free Pascal development team
  4. Some x86 specific stuff. Has to be fixed still for *BSD
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY;without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. unit x86;
  12. interface
  13. Uses BaseUnix;
  14. function ReadPortB (Port : Longint): Byte;
  15. function ReadPortW (Port : Longint): Word;
  16. function ReadPortL (Port : Longint): Longint;
  17. Procedure ReadPort (Port : Longint; Var Value : Byte);
  18. Procedure ReadPort (Port : Longint; Var Value : Longint);
  19. Procedure ReadPort (Port : Longint; Var Value : Word);
  20. Procedure ReadPortB (Port : Longint; Var Buf; Count: longint);
  21. Procedure ReadPortL (Port : Longint; Var Buf; Count: longint);
  22. Procedure ReadPortW (Port : Longint; Var Buf; Count: longint);
  23. Procedure WritePort (Port : Longint; Value : Byte);
  24. Procedure WritePort (Port : Longint; Value : Longint);
  25. Procedure WritePort (Port : Longint; Value : Word);
  26. Procedure WritePortB (Port : Longint; Value : Byte);
  27. Procedure WritePortB (Port : Longint; Var Buf; Count: longint);
  28. Procedure WritePortL (Port : Longint; Value : Longint);
  29. Procedure WritePortW (Port : Longint; Value : Word);
  30. Procedure WritePortW (Port : Longint; Var Buf; Count: longint);
  31. Procedure WritePortl (Port : Longint; Var Buf; Count: longint);
  32. Function fpIOperm (From,Num : Cardinal; Value : cint) : cint;
  33. Function fpIoPL(Level : cint) : cint;
  34. implementation
  35. {$ASMMODE ATT}
  36. Uses Syscall;
  37. Procedure WritePort (Port : Longint; Value : Byte);
  38. {
  39. Writes 'Value' to port 'Port'
  40. }
  41. begin
  42. asm
  43. {$ifdef CPU386}
  44. movl port,%edx
  45. movb value,%al
  46. outb %al,%dx
  47. {$endif CPU386}
  48. {$ifdef CPUX86_64}
  49. movl port,%edx
  50. movb value,%al
  51. outb %al,%dx
  52. {$endif CPUX86_64}
  53. end;
  54. end;
  55. Procedure WritePort (Port : Longint; Value : Word);
  56. {
  57. Writes 'Value' to port 'Port'
  58. }
  59. begin
  60. asm
  61. {$ifdef CPU386}
  62. movl port,%edx
  63. movw value,%ax
  64. outw %ax,%dx
  65. {$endif CPU386}
  66. {$ifdef CPUX86_64}
  67. movl port,%edx
  68. movw value,%ax
  69. outw %ax,%dx
  70. {$endif CPUX86_64}
  71. end;
  72. end;
  73. Procedure WritePort (Port : Longint; Value : Longint);
  74. {
  75. Writes 'Value' to port 'Port'
  76. }
  77. begin
  78. asm
  79. {$ifdef CPU386}
  80. movl port,%edx
  81. movl value,%eax
  82. outl %eax,%dx
  83. {$endif CPU386}
  84. {$ifdef CPUX86_64}
  85. movl port,%edx
  86. movl value,%eax
  87. outl %eax,%dx
  88. {$endif CPUX86_64}
  89. end;
  90. end;
  91. Procedure WritePortB (Port : Longint; Value : Byte);
  92. {
  93. Writes 'Value' to port 'Port'
  94. }
  95. begin
  96. asm
  97. {$ifdef CPU386}
  98. movl port,%edx
  99. movb value,%al
  100. outb %al,%dx
  101. {$endif CPU386}
  102. {$ifdef CPUX86_64}
  103. movl port,%edx
  104. movb value,%al
  105. outb %al,%dx
  106. {$endif CPUX86_64}
  107. end;
  108. end;
  109. Procedure WritePortW (Port : Longint; Value : Word);
  110. {
  111. Writes 'Value' to port 'Port'
  112. }
  113. begin
  114. asm
  115. {$ifdef CPU386}
  116. movl port,%edx
  117. movw value,%ax
  118. outw %ax,%dx
  119. {$endif CPU386}
  120. {$ifdef CPUX86_64}
  121. movl port,%edx
  122. movw value,%ax
  123. outw %ax,%dx
  124. {$endif CPUX86_64}
  125. end;
  126. end;
  127. Procedure WritePortL (Port : Longint; Value : Longint);
  128. {
  129. Writes 'Value' to port 'Port'
  130. }
  131. begin
  132. asm
  133. {$ifdef CPU386}
  134. movl port,%edx
  135. movl value,%eax
  136. outl %eax,%dx
  137. {$endif CPU386}
  138. {$ifdef CPUX86_64}
  139. movl port,%edx
  140. movl value,%eax
  141. outl %eax,%dx
  142. {$endif CPUX86_64}
  143. end;
  144. end;
  145. Procedure WritePortl (Port : Longint; Var Buf; Count: longint);
  146. {
  147. Writes 'Count' longints from 'Buf' to Port
  148. }
  149. begin
  150. asm
  151. {$ifdef CPU386}
  152. movl count,%ecx
  153. movl buf,%esi
  154. movl port,%edx
  155. cld
  156. rep
  157. outsl
  158. {$endif CPU386}
  159. {$ifdef CPUX86_64}
  160. movl count,%ecx
  161. movq buf,%rsi
  162. movl port,%edx
  163. cld
  164. rep
  165. outsl
  166. {$endif CPUX86_64}
  167. end;
  168. end;
  169. Procedure WritePortW (Port : Longint; Var Buf; Count: longint);
  170. {
  171. Writes 'Count' words from 'Buf' to Port
  172. }
  173. begin
  174. asm
  175. {$ifdef CPU386}
  176. movl count,%ecx
  177. movl buf,%esi
  178. movl port,%edx
  179. cld
  180. rep
  181. outsw
  182. {$endif CPU386}
  183. {$ifdef CPUX86_64}
  184. movl count,%ecx
  185. movq buf,%rsi
  186. movl port,%edx
  187. cld
  188. rep
  189. outsw
  190. {$endif CPUX86_64}
  191. end;
  192. end;
  193. Procedure WritePortB (Port : Longint; Var Buf; Count: longint);
  194. {
  195. Writes 'Count' bytes from 'Buf' to Port
  196. }
  197. begin
  198. asm
  199. {$ifdef CPU386}
  200. movl count,%ecx
  201. movl buf,%esi
  202. movl port,%edx
  203. cld
  204. rep
  205. outsb
  206. {$endif CPU386}
  207. {$ifdef CPUX86_64}
  208. movl count,%ecx
  209. movq buf,%rsi
  210. movl port,%edx
  211. cld
  212. rep
  213. outsb
  214. {$endif CPUX86_64}
  215. end;
  216. end;
  217. Procedure ReadPort (Port : Longint; Var Value : Byte);
  218. {
  219. Reads 'Value' from port 'Port'
  220. }
  221. begin
  222. asm
  223. {$ifdef CPU386}
  224. movl port,%edx
  225. inb %dx,%al
  226. movl value,%edx
  227. movb %al,(%edx)
  228. {$endif CPU386}
  229. {$ifdef CPUX86_64}
  230. movl port,%edx
  231. inb %dx,%al
  232. movq value,%rdx
  233. movb %al,(%rdx)
  234. {$endif CPUX86_64}
  235. end;
  236. end;
  237. Procedure ReadPort (Port : Longint; Var Value : Word);
  238. {
  239. Reads 'Value' from port 'Port'
  240. }
  241. begin
  242. asm
  243. {$ifdef CPU386}
  244. movl port,%edx
  245. inw %dx,%ax
  246. movl value,%edx
  247. movw %ax,(%edx)
  248. {$endif CPU386}
  249. {$ifdef CPUX86_64}
  250. movl port,%edx
  251. inw %dx,%ax
  252. movq value,%rdx
  253. movw %ax,(%rdx)
  254. {$endif CPUX86_64}
  255. end;
  256. end;
  257. Procedure ReadPort (Port : Longint; Var Value : Longint);
  258. {
  259. Reads 'Value' from port 'Port'
  260. }
  261. begin
  262. asm
  263. {$ifdef CPU386}
  264. movl port,%edx
  265. inl %dx,%eax
  266. movl value,%edx
  267. movl %eax,(%edx)
  268. {$endif CPU386}
  269. {$ifdef CPUX86_64}
  270. movl port,%edx
  271. inl %dx,%eax
  272. movq value,%rdx
  273. movl %eax,(%rdx)
  274. {$endif CPUX86_64}
  275. end;
  276. end;
  277. function ReadPortB (Port : Longint): Byte; assembler;
  278. {
  279. Reads a byte from port 'Port'
  280. }
  281. asm
  282. {$ifdef CPU386}
  283. movl port,%edx
  284. xorl %eax,%eax
  285. inb %dx,%al
  286. {$endif CPU386}
  287. {$ifdef CPUX86_64}
  288. movl port,%edx
  289. xorl %eax,%eax
  290. inb %dx,%al
  291. {$endif CPUX86_64}
  292. end;
  293. function ReadPortW (Port : Longint): Word; assembler;
  294. {
  295. Reads a word from port 'Port'
  296. }
  297. asm
  298. {$ifdef CPU386}
  299. movl port,%edx
  300. xorl %eax,%eax
  301. inw %dx,%ax
  302. {$endif CPU386}
  303. {$ifdef CPUX86_64}
  304. movl port,%edx
  305. xorl %eax,%eax
  306. inw %dx,%ax
  307. {$endif CPUX86_64}
  308. end;
  309. function ReadPortL (Port : Longint): LongInt; assembler;
  310. {
  311. Reads a LongInt from port 'Port'
  312. }
  313. asm
  314. {$ifdef CPU386}
  315. movl port,%edx
  316. inl %dx,%eax
  317. {$endif CPU386}
  318. {$ifdef CPUX86_64}
  319. movl port,%edx
  320. inl %dx,%eax
  321. {$endif CPUX86_64}
  322. end;
  323. Procedure ReadPortL (Port : Longint; Var Buf; Count: longint);
  324. {
  325. Reads 'Count' longints from port 'Port' to 'Buf'.
  326. }
  327. begin
  328. asm
  329. {$ifdef CPU386}
  330. movl count,%ecx
  331. movl buf,%edi
  332. movl port,%edx
  333. cld
  334. rep
  335. insl
  336. {$endif CPU386}
  337. {$ifdef CPUX86_64}
  338. movl count,%ecx
  339. movq buf,%rdi
  340. movl port,%edx
  341. cld
  342. rep
  343. insl
  344. {$endif CPUX86_64}
  345. end;
  346. end;
  347. Procedure ReadPortW (Port : Longint; Var Buf; Count: longint);
  348. {
  349. Reads 'Count' words from port 'Port' to 'Buf'.
  350. }
  351. begin
  352. asm
  353. {$ifdef CPU386}
  354. movl count,%ecx
  355. movl buf,%edi
  356. movl port,%edx
  357. cld
  358. rep
  359. insw
  360. {$endif CPU386}
  361. {$ifdef CPUX86_64}
  362. movl count,%ecx
  363. movq buf,%rdi
  364. movl port,%edx
  365. cld
  366. rep
  367. insw
  368. {$endif CPUX86_64}
  369. end;
  370. end;
  371. Procedure ReadPortB (Port : Longint; Var Buf; Count: longint);
  372. {
  373. Reads 'Count' bytes from port 'Port' to 'Buf'.
  374. }
  375. begin
  376. asm
  377. {$ifdef CPU386}
  378. movl count,%ecx
  379. movl buf,%edi
  380. movl port,%edx
  381. cld
  382. rep
  383. insb
  384. {$endif CPU386}
  385. {$ifdef CPUX86_64}
  386. movl count,%ecx
  387. movq buf,%rdi
  388. movl port,%edx
  389. cld
  390. rep
  391. insb
  392. {$endif CPUX86_64}
  393. end;
  394. end;
  395. {$ifdef linux}
  396. Function fpIOperm (From,Num : Cardinal; Value : cint) : cint;
  397. {
  398. Set permissions on NUM ports starting with port FROM to VALUE
  399. this works ONLY as root.
  400. }
  401. begin
  402. fpIOPerm:=do_Syscall(Syscall_nr_ioperm,TSysParam(From),TSysParam(Num),TSysParam(Value));
  403. end;
  404. {$else}
  405. {$packrecords C}
  406. TYPE uint=CARDINAL;
  407. CONST
  408. I386_GET_LDT =0;
  409. I386_SET_LDT =1;
  410. { I386_IOPL }
  411. I386_GET_IOPERM =3;
  412. I386_SET_IOPERM =4;
  413. { xxxxx }
  414. I386_VM86 =6;
  415. type
  416. { i386_ldt_args = record
  417. int start : longint;
  418. union descriptor *descs;
  419. int num;
  420. end;
  421. }
  422. i386_ioperm_args = record
  423. start : cuint;
  424. length : cuint;
  425. enable : cint;
  426. end;
  427. i386_vm86_args = record
  428. sub_op : cint; { sub-operation to perform }
  429. sub_args : pchar; { args }
  430. end;
  431. sysarch_args = record
  432. op : longint;
  433. parms : pchar;
  434. end;
  435. Function fpIOPerm(From,Num:CARDINAL;Value:cint):cint;
  436. var sg : i386_ioperm_args;
  437. sa : sysarch_args;
  438. begin
  439. sg.start:=From;
  440. sg.length:=Num;
  441. sg.enable:=value;
  442. sa.op:=i386_SET_IOPERM;
  443. sa.parms:=@sg;
  444. fpIOPerm:=do_syscall(syscall_nr_sysarch,TSysParam(@sa));
  445. end;
  446. {$endif}
  447. Function fpIoPL(Level : cint) : cint;
  448. begin
  449. {$ifdef Linux}
  450. fpIOPL:=do_Syscall(Syscall_nr_iopl,TSysParam(Level));
  451. {$endif}
  452. end;
  453. end.