aarch64.inc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. procedure fpc_enable_fpu_exceptions;
  102. begin
  103. { clear all "exception happened" flags we care about}
  104. setfpsr(getfpsr and not(fpu_exception_mask shr fpu_exception_mask_to_status_mask_shift));
  105. { enable invalid operations and division by zero exceptions. }
  106. setfpcr((getfpcr and not(fpu_exception_mask)));
  107. softfloat_exception_mask:=[exPrecision,exUnderflow,exInvalidOp];
  108. end;
  109. procedure fpc_cpuinit;
  110. begin
  111. { don't let libraries influence the FPU cw set by the host program }
  112. if not IsLibrary then
  113. fpc_enable_fpu_exceptions;
  114. end;
  115. {****************************************************************************
  116. Move / Fill
  117. ****************************************************************************}
  118. {****************************************************************************
  119. String
  120. ****************************************************************************}
  121. {$define FPC_SYSTEM_HAS_GET_CALLER_ADDR}
  122. function get_caller_addr(framebp:pointer;addr:pointer=nil):pointer;assembler; nostackframe;
  123. asm
  124. cbz x0, .Lcaller_addr_invalid
  125. ldur x0, [x0]
  126. cbz x0, .Lcaller_addr_invalid
  127. ldur x0, [x0, #8]
  128. .Lcaller_addr_invalid:
  129. end;
  130. {$define FPC_SYSTEM_HAS_GET_CALLER_FRAME}
  131. function get_caller_frame(framebp:pointer;addr:pointer=nil):pointer;assembler; nostackframe;
  132. asm
  133. cbz x0, .Lcaller_addr_invalid
  134. ldur x0, [x0]
  135. .Lcaller_addr_invalid:
  136. end;
  137. {$define FPC_SYSTEM_HAS_SPTR}
  138. Function Sptr : Pointer;assembler; nostackframe;
  139. asm
  140. mov x0, sp
  141. end;
  142. {****************************************************************************
  143. Str()
  144. ****************************************************************************}
  145. { int_str: generic implementation is used for now }
  146. {****************************************************************************
  147. Multithreading
  148. ****************************************************************************}
  149. { perform a thread-safe inc/dec }
  150. {$define FPC_SYSTEM_HAS_DECLOCKED_LONGINT}
  151. function declocked(var l : longint) : boolean;assembler;nostackframe;
  152. { input: address of l in x0 }
  153. { output: boolean indicating whether l is zero after decrementing }
  154. asm
  155. .LDecLockedLoop:
  156. ldxr w1,[x0]
  157. sub w1,w1,#1
  158. stxr w2,w1,[x0]
  159. cbnz w2,.LDecLockedLoop
  160. cset w0, eq
  161. end;
  162. {$define FPC_SYSTEM_HAS_INCLOCKED_LONGINT}
  163. procedure inclocked(var l : longint);assembler;nostackframe;
  164. asm
  165. .LIncLockedLoop:
  166. ldxr w1,[x0]
  167. add w1,w1,#1
  168. stxr w2,w1,[x0]
  169. cbnz w2,.LIncLockedLoop
  170. end;
  171. {$define FPC_SYSTEM_HAS_DECLOCKED_INT64}
  172. function declocked(var l : int64) : boolean;assembler;nostackframe;
  173. { input: address of l in x0 }
  174. { output: boolean indicating whether l is zero after decrementing }
  175. asm
  176. .LDecLockedLoop:
  177. ldxr x1,[x0]
  178. subs x1,x1,#1
  179. stxr w2,x1,[x0]
  180. cbnz w2,.LDecLockedLoop
  181. cset w0, eq
  182. end;
  183. {$define FPC_SYSTEM_HAS_INCLOCKED_INT64}
  184. procedure inclocked(var l : int64);assembler;nostackframe;
  185. asm
  186. .LIncLockedLoop:
  187. ldxr x1,[x0]
  188. add x1,x1,#1
  189. stxr w2,x1,[x0]
  190. cbnz w2,.LIncLockedLoop
  191. end;
  192. function InterLockedDecrement (var Target: longint) : longint; assembler; nostackframe;
  193. { input: address of target in x0 }
  194. { output: target-1 in x0 }
  195. { side-effect: target := target-1 }
  196. asm
  197. .LInterDecLockedLoop:
  198. ldxr w1,[x0]
  199. sub w1,w1,#1
  200. stxr w2,w1,[x0]
  201. cbnz w2,.LInterDecLockedLoop
  202. mov w0,w1
  203. end;
  204. function InterLockedIncrement (var Target: longint) : longint; assembler; nostackframe;
  205. { input: address of target in x0 }
  206. { output: target+1 in x0 }
  207. { side-effect: target := target+1 }
  208. asm
  209. .LInterIncLockedLoop:
  210. ldxr w1,[x0]
  211. add w1,w1,#1
  212. stxr w2,w1,[x0]
  213. cbnz w2,.LInterIncLockedLoop
  214. mov w0,w1
  215. end;
  216. function InterLockedExchange (var Target: longint;Source : longint) : longint; assembler; nostackframe;
  217. { input: address of target in x0, source in w1 }
  218. { output: target in x0 }
  219. { side-effect: target := source }
  220. asm
  221. .LInterLockedXchgLoop:
  222. ldxr w2,[x0]
  223. stxr w3,w1,[x0]
  224. cbnz w3,.LInterLockedXchgLoop
  225. mov w0,w2
  226. end;
  227. function InterLockedExchangeAdd (var Target: longint;Source : longint) : longint; assembler; nostackframe;
  228. asm
  229. .LInterLockedXchgAddLoop:
  230. ldxr w2,[x0]
  231. add w4,w2,w1
  232. stxr w3,w4,[x0]
  233. cbnz w3,.LInterLockedXchgAddLoop
  234. mov w0,w2
  235. end;
  236. function InterlockedCompareExchange(var Target: longint; NewValue: longint; Comperand: longint): longint; assembler; nostackframe;
  237. { input: address of target in x0, newvalue in w1, comparand in w2 }
  238. { output: value stored in target before entry of the function }
  239. { side-effect: NewValue stored in target if (target = comparand) }
  240. asm
  241. .LInterlockedCompareExchangeLoop:
  242. ldxr w3,[x0]
  243. cmp w3,w2
  244. csel w4,w1,w3,eq
  245. stxr w5,w4,[x0]
  246. cbnz w5,.LInterlockedCompareExchangeLoop
  247. mov w0,w3
  248. end;
  249. function InterLockedDecrement64 (var Target: int64) : int64; assembler; nostackframe;
  250. asm
  251. .LInterDecLockedLoop:
  252. ldxr x1,[x0]
  253. sub x1,x1,#1
  254. stxr w2,x1,[x0]
  255. cbnz w2,.LInterDecLockedLoop
  256. mov x0,x1
  257. end;
  258. function InterLockedIncrement64 (var Target: int64) : int64; assembler; nostackframe;
  259. asm
  260. .LInterIncLockedLoop:
  261. ldxr x1,[x0]
  262. add x1,x1,#1
  263. stxr w2,x1,[x0]
  264. cbnz w2,.LInterIncLockedLoop
  265. mov x0,x1
  266. end;
  267. function InterLockedExchange64 (var Target: int64;Source : int64) : int64; assembler; nostackframe;
  268. asm
  269. .LInterLockedXchgLoop:
  270. ldxr x2,[x0]
  271. stxr w3,x1,[x0]
  272. cbnz w3,.LInterLockedXchgLoop
  273. mov x0,x2
  274. end;
  275. function InterLockedExchangeAdd64 (var Target: int64;Source : int64) : int64; assembler; nostackframe;
  276. asm
  277. .LInterLockedXchgAddLoop:
  278. ldxr x2,[x0]
  279. add x4,x2,x1
  280. stxr w3,x4,[x0]
  281. cbnz w3,.LInterLockedXchgAddLoop
  282. mov x0,x2
  283. end;
  284. function InterLockedCompareExchange64(var Target: int64; NewValue, Comperand : int64): int64; assembler; nostackframe;
  285. asm
  286. .LInterlockedCompareExchangeLoop:
  287. ldxr x3,[x0]
  288. cmp x3,x2
  289. csel x4,x1,x3,eq
  290. stxr w5,x4,[x0]
  291. cbnz w5,.LInterlockedCompareExchangeLoop
  292. mov x0,x3
  293. end;
  294. {$ifndef FPC_SYSTEM_HAS_MEM_BARRIER}
  295. {$define FPC_SYSTEM_HAS_MEM_BARRIER}
  296. procedure ReadBarrier;assembler;nostackframe;{$ifdef SYSTEMINLINE}inline;{$endif}
  297. asm
  298. { dmb ishld }
  299. dmb #9
  300. end;
  301. procedure ReadDependencyBarrier;{$ifdef SYSTEMINLINE}inline;{$endif}
  302. begin
  303. { reads imply barrier on earlier reads depended on }
  304. end;
  305. procedure ReadWriteBarrier;assembler;nostackframe;{$ifdef SYSTEMINLINE}inline;{$endif}
  306. asm
  307. { dmb ish }
  308. dmb #11
  309. end;
  310. procedure WriteBarrier;assembler;nostackframe;{$ifdef SYSTEMINLINE}inline;{$endif}
  311. asm
  312. { dmb ishst }
  313. dmb #10
  314. end;
  315. {$endif}