aarch64.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2014 by Jonas Maebe, member of
  4. the Free Pascal development team.
  5. Processor dependent implementation for the system unit for
  6. AArch64
  7. See the file COPYING.FPC, included in this distribution,
  8. for details about the copyright.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. **********************************************************************}
  13. {$IFNDEF LINUX}
  14. {$DEFINE USE_DCBZ}
  15. {$ENDIF LINUX}
  16. {****************************************************************************
  17. AArch64 specific stuff
  18. ****************************************************************************}
  19. const
  20. fpu_ioe = 1 shl 8;
  21. fpu_dze = 1 shl 9;
  22. fpu_ofe = 1 shl 10;
  23. fpu_ufe = 1 shl 11;
  24. fpu_ixe = 1 shl 12;
  25. fpu_ide = 1 shl 15;
  26. fpu_exception_mask = fpu_ioe or fpu_dze or fpu_ofe or fpu_ufe or fpu_ixe or fpu_ide;
  27. fpu_exception_mask_to_status_mask_shift = 8;
  28. function getfpcr: dword; nostackframe; assembler;
  29. asm
  30. mrs x0,fpcr
  31. end;
  32. procedure setfpcr(val: dword); nostackframe; assembler;
  33. asm
  34. msr fpcr,x0
  35. end;
  36. function getfpsr: dword; nostackframe; assembler;
  37. asm
  38. mrs x0,fpsr
  39. end;
  40. procedure setfpsr(val: dword); nostackframe; assembler;
  41. asm
  42. msr fpsr, x0
  43. end;
  44. const
  45. FPSR_IOC = 1;
  46. FPSR_DZC = 1 shl 1;
  47. FPSR_OFC = 1 shl 2;
  48. FPSR_UFC = 1 shl 3;
  49. FPSR_IXC = 1 shl 4;
  50. FPSR_IDC = 1 shl 7;
  51. FPSR_EXCEPTIONS = FPSR_IOC or FPSR_DZC or FPSR_OFC or FPSR_UFC or FPSR_IXC or FPSR_IDC;
  52. procedure RaisePendingExceptions;
  53. var
  54. fpsr : dword;
  55. f: TFPUException;
  56. begin
  57. fpsr:=getfpsr;
  58. if (fpsr and FPSR_DZC) <> 0 then
  59. float_raise(exZeroDivide);
  60. if (fpsr and FPSR_OFC) <> 0 then
  61. float_raise(exOverflow);
  62. if (fpsr and FPSR_UFC) <> 0 then
  63. float_raise(exUnderflow);
  64. if (fpsr and FPSR_IOC) <> 0 then
  65. float_raise(exInvalidOp);
  66. if (fpsr and FPSR_IXC) <> 0 then
  67. float_raise(exPrecision);
  68. if (fpsr and FPSR_IDC) <> 0 then
  69. float_raise(exDenormalized);
  70. { now the soft float exceptions }
  71. for f in softfloat_exception_flags do
  72. float_raise(f);
  73. end;
  74. { as so far no AArch64 flavour which supports hard floating point exceptions, we use solely
  75. the softfloat_exception_mask for masking as the masking flags are RAZ and WI if floating point
  76. exceptions are not supported }
  77. procedure fpc_throwfpuexception;[public,alias:'FPC_THROWFPUEXCEPTION'];
  78. var
  79. fpsr : dword;
  80. f: TFPUException;
  81. begin
  82. { at this point, we know already, that an exception will be risen }
  83. fpsr:=getfpsr;
  84. { check, if the exception is masked }
  85. if ((fpsr and FPSR_DZC) <> 0) and (exZeroDivide in softfloat_exception_mask) then
  86. fpsr:=fpsr and not(FPSR_DZC);
  87. if ((fpsr and FPSR_OFC) <> 0) and (exOverflow in softfloat_exception_mask) then
  88. fpsr:=fpsr and not(FPSR_OFC);
  89. if ((fpsr and FPSR_UFC) <> 0) and (exUnderflow in softfloat_exception_mask) then
  90. fpsr:=fpsr and not(FPSR_UFC);
  91. if ((fpsr and FPSR_IOC) <> 0) and (exInvalidOp in softfloat_exception_mask) then
  92. fpsr:=fpsr and not(FPSR_IOC);
  93. if ((fpsr and FPSR_IXC) <> 0) and (exPrecision in softfloat_exception_mask) then
  94. fpsr:=fpsr and not(FPSR_IXC);
  95. if ((fpsr and FPSR_IDC) <> 0) and (exDenormalized in softfloat_exception_mask) then
  96. fpsr:=fpsr and not(FPSR_IDC);
  97. setfpsr(fpsr);
  98. if (fpsr and FPSR_EXCEPTIONS)<>0 then
  99. RaisePendingExceptions;
  100. end;
  101. {$define FPC_SYSTEM_HAS_SYSINITFPU}
  102. procedure SysInitFPU;
  103. begin
  104. softfloat_rounding_mode:=rmNearest;
  105. { 0 is rmNearest }
  106. setfpcr(getfpcr and $ff3fffff);
  107. { clear all "exception happened" flags we care about}
  108. setfpsr(getfpsr and not(fpu_exception_mask shr fpu_exception_mask_to_status_mask_shift));
  109. { enable invalid operations and division by zero exceptions. }
  110. setfpcr(((getfpcr and not(fpu_exception_mask)) or fpu_dze or fpu_ofe or fpu_ioe));
  111. softfloat_exception_mask:=[float_flag_underflow,float_flag_inexact,float_flag_denormal];
  112. softfloat_exception_flags:=[];
  113. end;
  114. {$define FPC_SYSTEM_HAS_SYSRESETFPU}
  115. Procedure SysResetFPU;{$ifdef SYSTEMINLINE}inline;{$endif}
  116. begin
  117. softfloat_exception_flags:=[];
  118. setfpsr(getfpsr and not(FPSR_EXCEPTIONS));
  119. end;
  120. procedure fpc_cpuinit;
  121. begin
  122. { don't let libraries influence the FPU cw set by the host program }
  123. if not IsLibrary then
  124. SysInitFPU;
  125. end;
  126. {****************************************************************************
  127. Move / Fill
  128. ****************************************************************************}
  129. {****************************************************************************
  130. String
  131. ****************************************************************************}
  132. {$define FPC_SYSTEM_HAS_GET_CALLER_ADDR}
  133. function get_caller_addr(framebp:pointer;addr:pointer=nil):pointer;assembler; nostackframe;
  134. asm
  135. cbz x0, .Lcaller_addr_invalid
  136. ldur x0, [x0]
  137. cbz x0, .Lcaller_addr_invalid
  138. ldur x0, [x0, #8]
  139. .Lcaller_addr_invalid:
  140. end;
  141. {$define FPC_SYSTEM_HAS_GET_CALLER_FRAME}
  142. function get_caller_frame(framebp:pointer;addr:pointer=nil):pointer;assembler; nostackframe;
  143. asm
  144. cbz x0, .Lcaller_addr_invalid
  145. ldur x0, [x0]
  146. .Lcaller_addr_invalid:
  147. end;
  148. {$define FPC_SYSTEM_HAS_SPTR}
  149. Function Sptr : Pointer;assembler; nostackframe;
  150. asm
  151. mov x0, sp
  152. end;
  153. {****************************************************************************
  154. Str()
  155. ****************************************************************************}
  156. { int_str: generic implementation is used for now }
  157. {****************************************************************************
  158. Multithreading
  159. ****************************************************************************}
  160. { perform a thread-safe inc/dec }
  161. {$define FPC_SYSTEM_HAS_DECLOCKED_LONGINT}
  162. function declocked(var l : longint) : boolean;assembler;nostackframe;
  163. { input: address of l in x0 }
  164. { output: boolean indicating whether l is zero after decrementing }
  165. asm
  166. .LDecLockedLoop:
  167. ldxr w1,[x0]
  168. sub w1,w1,#1
  169. stxr w2,w1,[x0]
  170. cbnz w2,.LDecLockedLoop
  171. cset w0, eq
  172. end;
  173. {$define FPC_SYSTEM_HAS_INCLOCKED_LONGINT}
  174. procedure inclocked(var l : longint);assembler;nostackframe;
  175. asm
  176. .LIncLockedLoop:
  177. ldxr w1,[x0]
  178. add w1,w1,#1
  179. stxr w2,w1,[x0]
  180. cbnz w2,.LIncLockedLoop
  181. end;
  182. {$define FPC_SYSTEM_HAS_DECLOCKED_INT64}
  183. function declocked(var l : int64) : boolean;assembler;nostackframe;
  184. { input: address of l in x0 }
  185. { output: boolean indicating whether l is zero after decrementing }
  186. asm
  187. .LDecLockedLoop:
  188. ldxr x1,[x0]
  189. subs x1,x1,#1
  190. stxr w2,x1,[x0]
  191. cbnz w2,.LDecLockedLoop
  192. cset w0, eq
  193. end;
  194. {$define FPC_SYSTEM_HAS_INCLOCKED_INT64}
  195. procedure inclocked(var l : int64);assembler;nostackframe;
  196. asm
  197. .LIncLockedLoop:
  198. ldxr x1,[x0]
  199. add x1,x1,#1
  200. stxr w2,x1,[x0]
  201. cbnz w2,.LIncLockedLoop
  202. end;
  203. function InterLockedDecrement (var Target: longint) : longint; assembler; nostackframe;
  204. { input: address of target in x0 }
  205. { output: target-1 in x0 }
  206. { side-effect: target := target-1 }
  207. asm
  208. .LInterDecLockedLoop:
  209. ldxr w1,[x0]
  210. sub w1,w1,#1
  211. stxr w2,w1,[x0]
  212. cbnz w2,.LInterDecLockedLoop
  213. mov w0,w1
  214. end;
  215. function InterLockedIncrement (var Target: longint) : longint; assembler; nostackframe;
  216. { input: address of target in x0 }
  217. { output: target+1 in x0 }
  218. { side-effect: target := target+1 }
  219. asm
  220. .LInterIncLockedLoop:
  221. ldxr w1,[x0]
  222. add w1,w1,#1
  223. stxr w2,w1,[x0]
  224. cbnz w2,.LInterIncLockedLoop
  225. mov w0,w1
  226. end;
  227. function InterLockedExchange (var Target: longint;Source : longint) : longint; assembler; nostackframe;
  228. { input: address of target in x0, source in w1 }
  229. { output: target in x0 }
  230. { side-effect: target := source }
  231. asm
  232. .LInterLockedXchgLoop:
  233. ldxr w2,[x0]
  234. stxr w3,w1,[x0]
  235. cbnz w3,.LInterLockedXchgLoop
  236. mov w0,w2
  237. end;
  238. function InterLockedExchangeAdd (var Target: longint;Source : longint) : longint; assembler; nostackframe;
  239. asm
  240. .LInterLockedXchgAddLoop:
  241. ldxr w2,[x0]
  242. add w4,w2,w1
  243. stxr w3,w4,[x0]
  244. cbnz w3,.LInterLockedXchgAddLoop
  245. mov w0,w2
  246. end;
  247. function InterlockedCompareExchange(var Target: longint; NewValue: longint; Comperand: longint): longint; assembler; nostackframe;
  248. { input: address of target in x0, newvalue in w1, comparand in w2 }
  249. { output: value stored in target before entry of the function }
  250. { side-effect: NewValue stored in target if (target = comparand) }
  251. asm
  252. .LInterlockedCompareExchangeLoop:
  253. ldxr w3,[x0]
  254. cmp w3,w2
  255. csel w4,w1,w3,eq
  256. stxr w5,w4,[x0]
  257. cbnz w5,.LInterlockedCompareExchangeLoop
  258. mov w0,w3
  259. end;
  260. function InterLockedDecrement64 (var Target: int64) : int64; assembler; nostackframe;
  261. asm
  262. .LInterDecLockedLoop:
  263. ldxr x1,[x0]
  264. sub x1,x1,#1
  265. stxr w2,x1,[x0]
  266. cbnz w2,.LInterDecLockedLoop
  267. mov x0,x1
  268. end;
  269. function InterLockedIncrement64 (var Target: int64) : int64; assembler; nostackframe;
  270. asm
  271. .LInterIncLockedLoop:
  272. ldxr x1,[x0]
  273. add x1,x1,#1
  274. stxr w2,x1,[x0]
  275. cbnz w2,.LInterIncLockedLoop
  276. mov x0,x1
  277. end;
  278. function InterLockedExchange64 (var Target: int64;Source : int64) : int64; assembler; nostackframe;
  279. asm
  280. .LInterLockedXchgLoop:
  281. ldxr x2,[x0]
  282. stxr w3,x1,[x0]
  283. cbnz w3,.LInterLockedXchgLoop
  284. mov x0,x2
  285. end;
  286. function InterLockedExchangeAdd64 (var Target: int64;Source : int64) : int64; assembler; nostackframe;
  287. asm
  288. .LInterLockedXchgAddLoop:
  289. ldxr x2,[x0]
  290. add x4,x2,x1
  291. stxr w3,x4,[x0]
  292. cbnz w3,.LInterLockedXchgAddLoop
  293. mov x0,x2
  294. end;
  295. function InterLockedCompareExchange64(var Target: int64; NewValue, Comperand : int64): int64; assembler; nostackframe;
  296. asm
  297. .LInterlockedCompareExchangeLoop:
  298. ldxr x3,[x0]
  299. cmp x3,x2
  300. csel x4,x1,x3,eq
  301. stxr w5,x4,[x0]
  302. cbnz w5,.LInterlockedCompareExchangeLoop
  303. mov x0,x3
  304. end;
  305. {$ifndef FPC_SYSTEM_HAS_MEM_BARRIER}
  306. {$define FPC_SYSTEM_HAS_MEM_BARRIER}
  307. procedure ReadBarrier;assembler;nostackframe;{$ifdef SYSTEMINLINE}inline;{$endif}
  308. asm
  309. { dmb ishld }
  310. dmb #9
  311. end;
  312. procedure ReadDependencyBarrier;{$ifdef SYSTEMINLINE}inline;{$endif}
  313. begin
  314. { reads imply barrier on earlier reads depended on }
  315. end;
  316. procedure ReadWriteBarrier;assembler;nostackframe;{$ifdef SYSTEMINLINE}inline;{$endif}
  317. asm
  318. { dmb ish }
  319. dmb #11
  320. end;
  321. procedure WriteBarrier;assembler;nostackframe;{$ifdef SYSTEMINLINE}inline;{$endif}
  322. asm
  323. { dmb ishst }
  324. dmb #10
  325. end;
  326. {$endif}