x86.pp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. movl port,%edx
  44. movb value,%al
  45. outb %al,%dx
  46. end ['EAX','EDX'];
  47. end;
  48. Procedure WritePort (Port : Longint; Value : Word);
  49. {
  50. Writes 'Value' to port 'Port'
  51. }
  52. begin
  53. asm
  54. movl port,%edx
  55. movw value,%ax
  56. outw %ax,%dx
  57. end ['EAX','EDX'];
  58. end;
  59. Procedure WritePort (Port : Longint; Value : Longint);
  60. {
  61. Writes 'Value' to port 'Port'
  62. }
  63. begin
  64. asm
  65. movl port,%edx
  66. movl value,%eax
  67. outl %eax,%dx
  68. end ['EAX','EDX'];
  69. end;
  70. Procedure WritePortB (Port : Longint; Value : Byte);
  71. {
  72. Writes 'Value' to port 'Port'
  73. }
  74. begin
  75. asm
  76. movl port,%edx
  77. movb value,%al
  78. outb %al,%dx
  79. end ['EAX','EDX'];
  80. end;
  81. Procedure WritePortW (Port : Longint; Value : Word);
  82. {
  83. Writes 'Value' to port 'Port'
  84. }
  85. begin
  86. asm
  87. movl port,%edx
  88. movw value,%ax
  89. outw %ax,%dx
  90. end ['EAX','EDX'];
  91. end;
  92. Procedure WritePortL (Port : Longint; Value : Longint);
  93. {
  94. Writes 'Value' to port 'Port'
  95. }
  96. begin
  97. asm
  98. movl port,%edx
  99. movl value,%eax
  100. outl %eax,%dx
  101. end ['EAX','EDX'];
  102. end;
  103. Procedure WritePortl (Port : Longint; Var Buf; Count: longint);
  104. {
  105. Writes 'Count' longints from 'Buf' to Port
  106. }
  107. begin
  108. asm
  109. movl count,%ecx
  110. movl buf,%esi
  111. movl port,%edx
  112. cld
  113. rep
  114. outsl
  115. end ['ECX','ESI','EDX'];
  116. end;
  117. Procedure WritePortW (Port : Longint; Var Buf; Count: longint);
  118. {
  119. Writes 'Count' words from 'Buf' to Port
  120. }
  121. begin
  122. asm
  123. movl count,%ecx
  124. movl buf,%esi
  125. movl port,%edx
  126. cld
  127. rep
  128. outsw
  129. end ['ECX','ESI','EDX'];
  130. end;
  131. Procedure WritePortB (Port : Longint; Var Buf; Count: longint);
  132. {
  133. Writes 'Count' bytes from 'Buf' to Port
  134. }
  135. begin
  136. asm
  137. movl count,%ecx
  138. movl buf,%esi
  139. movl port,%edx
  140. cld
  141. rep
  142. outsb
  143. end ['ECX','ESI','EDX'];
  144. end;
  145. Procedure ReadPort (Port : Longint; Var Value : Byte);
  146. {
  147. Reads 'Value' from port 'Port'
  148. }
  149. begin
  150. asm
  151. movl port,%edx
  152. inb %dx,%al
  153. movl value,%edx
  154. movb %al,(%edx)
  155. end ['EAX','EDX'];
  156. end;
  157. Procedure ReadPort (Port : Longint; Var Value : Word);
  158. {
  159. Reads 'Value' from port 'Port'
  160. }
  161. begin
  162. asm
  163. movl port,%edx
  164. inw %dx,%ax
  165. movl value,%edx
  166. movw %ax,(%edx)
  167. end ['EAX','EDX'];
  168. end;
  169. Procedure ReadPort (Port : Longint; Var Value : Longint);
  170. {
  171. Reads 'Value' from port 'Port'
  172. }
  173. begin
  174. asm
  175. movl port,%edx
  176. inl %dx,%eax
  177. movl value,%edx
  178. movl %eax,(%edx)
  179. end ['EAX','EDX'];
  180. end;
  181. function ReadPortB (Port : Longint): Byte; assembler;
  182. {
  183. Reads a byte from port 'Port'
  184. }
  185. asm
  186. movl port,%edx
  187. xorl %eax,%eax
  188. inb %dx,%al
  189. end ['EAX','EDX'];
  190. function ReadPortW (Port : Longint): Word; assembler;
  191. {
  192. Reads a word from port 'Port'
  193. }
  194. asm
  195. movl port,%edx
  196. xorl %eax,%eax
  197. inw %dx,%ax
  198. end ['EAX','EDX'];
  199. function ReadPortL (Port : Longint): LongInt; assembler;
  200. {
  201. Reads a LongInt from port 'Port'
  202. }
  203. asm
  204. movl port,%edx
  205. inl %dx,%eax
  206. end ['EAX','EDX'];
  207. Procedure ReadPortL (Port : Longint; Var Buf; Count: longint);
  208. {
  209. Reads 'Count' longints from port 'Port' to 'Buf'.
  210. }
  211. begin
  212. asm
  213. movl count,%ecx
  214. movl buf,%edi
  215. movl port,%edx
  216. cld
  217. rep
  218. insl
  219. end ['ECX','EDI','EDX'];
  220. end;
  221. Procedure ReadPortW (Port : Longint; Var Buf; Count: longint);
  222. {
  223. Reads 'Count' words from port 'Port' to 'Buf'.
  224. }
  225. begin
  226. asm
  227. movl count,%ecx
  228. movl buf,%edi
  229. movl port,%edx
  230. cld
  231. rep
  232. insw
  233. end ['ECX','EDI','EDX'];
  234. end;
  235. Procedure ReadPortB (Port : Longint; Var Buf; Count: longint);
  236. {
  237. Reads 'Count' bytes from port 'Port' to 'Buf'.
  238. }
  239. begin
  240. asm
  241. movl count,%ecx
  242. movl buf,%edi
  243. movl port,%edx
  244. cld
  245. rep
  246. insb
  247. end ['ECX','EDI','EDX'];
  248. end;
  249. {$ifdef linux}
  250. Function fpIOperm (From,Num : Cardinal; Value : cint) : cint;
  251. {
  252. Set permissions on NUM ports starting with port FROM to VALUE
  253. this works ONLY as root.
  254. }
  255. begin
  256. fpIOPerm:=do_Syscall(Syscall_nr_ioperm,TSysParam(From),TSysParam(Num),TSysParam(Value));
  257. end;
  258. {$else}
  259. {$packrecords C}
  260. TYPE uint=CARDINAL;
  261. CONST
  262. I386_GET_LDT =0;
  263. I386_SET_LDT =1;
  264. { I386_IOPL }
  265. I386_GET_IOPERM =3;
  266. I386_SET_IOPERM =4;
  267. { xxxxx }
  268. I386_VM86 =6;
  269. type
  270. { i386_ldt_args = record
  271. int start : longint;
  272. union descriptor *descs;
  273. int num;
  274. end;
  275. }
  276. i386_ioperm_args = record
  277. start : cuint;
  278. length : cuint;
  279. enable : cint;
  280. end;
  281. i386_vm86_args = record
  282. sub_op : cint; { sub-operation to perform }
  283. sub_args : pchar; { args }
  284. end;
  285. sysarch_args = record
  286. op : longint;
  287. parms : pchar;
  288. end;
  289. Function fpIOPerm(From,Num:CARDINAL;Value:cint):cint;
  290. var sg : i386_ioperm_args;
  291. sa : sysarch_args;
  292. begin
  293. sg.start:=From;
  294. sg.length:=Num;
  295. sg.enable:=value;
  296. sa.op:=i386_SET_IOPERM;
  297. sa.parms:=@sg;
  298. fpIOPerm:=do_syscall(syscall_nr_sysarch,TSysParam(@sa));
  299. end;
  300. {$endif}
  301. Function fpIoPL(Level : cint) : cint;
  302. begin
  303. {$ifdef Linux}
  304. fpIOPL:=do_Syscall(Syscall_nr_iopl,TSysParam(Level));
  305. {$endif}
  306. end;
  307. end.