aarch64.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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. {$ifdef cpullvm}
  137. movn w1, #0
  138. cmp x0, x1
  139. csel x0, xzr, x0, ls
  140. b.ls .Lcaller_addr_invalid
  141. {$endif cpullvm}
  142. ldur x0, [x0, #8]
  143. .Lcaller_addr_invalid:
  144. end;
  145. {$define FPC_SYSTEM_HAS_GET_CALLER_FRAME}
  146. function get_caller_frame(framebp:pointer;addr:pointer=nil):pointer;assembler; nostackframe;
  147. asm
  148. cbz x0, .Lcaller_addr_invalid
  149. ldur x0, [x0]
  150. .Lcaller_addr_invalid:
  151. end;
  152. {$define FPC_SYSTEM_HAS_SPTR}
  153. Function Sptr : Pointer;assembler; nostackframe;
  154. asm
  155. mov x0, sp
  156. end;
  157. {****************************************************************************
  158. Str()
  159. ****************************************************************************}
  160. { int_str: generic implementation is used for now }
  161. {****************************************************************************
  162. Multithreading
  163. ****************************************************************************}
  164. { perform a thread-safe inc/dec }
  165. {$define FPC_SYSTEM_HAS_DECLOCKED_LONGINT}
  166. function declocked(var l : longint) : boolean;assembler;nostackframe;
  167. { input: address of l in x0 }
  168. { output: boolean indicating whether l is zero after decrementing }
  169. asm
  170. .LDecLockedLoop:
  171. ldxr w1,[x0]
  172. sub w1,w1,#1
  173. stxr w2,w1,[x0]
  174. cbnz w2,.LDecLockedLoop
  175. cset w0, eq
  176. end;
  177. {$define FPC_SYSTEM_HAS_INCLOCKED_LONGINT}
  178. procedure inclocked(var l : longint);assembler;nostackframe;
  179. asm
  180. .LIncLockedLoop:
  181. ldxr w1,[x0]
  182. add w1,w1,#1
  183. stxr w2,w1,[x0]
  184. cbnz w2,.LIncLockedLoop
  185. end;
  186. {$define FPC_SYSTEM_HAS_DECLOCKED_INT64}
  187. function declocked(var l : int64) : boolean;assembler;nostackframe;
  188. { input: address of l in x0 }
  189. { output: boolean indicating whether l is zero after decrementing }
  190. asm
  191. .LDecLockedLoop:
  192. ldxr x1,[x0]
  193. subs x1,x1,#1
  194. stxr w2,x1,[x0]
  195. cbnz w2,.LDecLockedLoop
  196. cset w0, eq
  197. end;
  198. {$define FPC_SYSTEM_HAS_INCLOCKED_INT64}
  199. procedure inclocked(var l : int64);assembler;nostackframe;
  200. asm
  201. .LIncLockedLoop:
  202. ldxr x1,[x0]
  203. add x1,x1,#1
  204. stxr w2,x1,[x0]
  205. cbnz w2,.LIncLockedLoop
  206. end;
  207. function InterLockedDecrement (var Target: longint) : longint; assembler; nostackframe;
  208. { input: address of target in x0 }
  209. { output: target-1 in x0 }
  210. { side-effect: target := target-1 }
  211. asm
  212. .LInterDecLockedLoop:
  213. ldxr w1,[x0]
  214. sub w1,w1,#1
  215. stxr w2,w1,[x0]
  216. cbnz w2,.LInterDecLockedLoop
  217. mov w0,w1
  218. end;
  219. function InterLockedIncrement (var Target: longint) : longint; assembler; nostackframe;
  220. { input: address of target in x0 }
  221. { output: target+1 in x0 }
  222. { side-effect: target := target+1 }
  223. asm
  224. .LInterIncLockedLoop:
  225. ldxr w1,[x0]
  226. add w1,w1,#1
  227. stxr w2,w1,[x0]
  228. cbnz w2,.LInterIncLockedLoop
  229. mov w0,w1
  230. end;
  231. function InterLockedExchange (var Target: longint;Source : longint) : longint; assembler; nostackframe;
  232. { input: address of target in x0, source in w1 }
  233. { output: target in x0 }
  234. { side-effect: target := source }
  235. asm
  236. .LInterLockedXchgLoop:
  237. ldxr w2,[x0]
  238. stxr w3,w1,[x0]
  239. cbnz w3,.LInterLockedXchgLoop
  240. mov w0,w2
  241. end;
  242. function InterLockedExchangeAdd (var Target: longint;Source : longint) : longint; assembler; nostackframe;
  243. asm
  244. .LInterLockedXchgAddLoop:
  245. ldxr w2,[x0]
  246. add w4,w2,w1
  247. stxr w3,w4,[x0]
  248. cbnz w3,.LInterLockedXchgAddLoop
  249. mov w0,w2
  250. end;
  251. function InterlockedCompareExchange(var Target: longint; NewValue: longint; Comperand: longint): longint; assembler; nostackframe;
  252. { input: address of target in x0, newvalue in w1, comparand in w2 }
  253. { output: value stored in target before entry of the function }
  254. { side-effect: NewValue stored in target if (target = comparand) }
  255. asm
  256. .LInterlockedCompareExchangeLoop:
  257. ldxr w3,[x0]
  258. cmp w3,w2
  259. csel w4,w1,w3,eq
  260. stxr w5,w4,[x0]
  261. cbnz w5,.LInterlockedCompareExchangeLoop
  262. mov w0,w3
  263. end;
  264. function InterLockedDecrement64 (var Target: int64) : int64; assembler; nostackframe;
  265. asm
  266. .LInterDecLockedLoop:
  267. ldxr x1,[x0]
  268. sub x1,x1,#1
  269. stxr w2,x1,[x0]
  270. cbnz w2,.LInterDecLockedLoop
  271. mov x0,x1
  272. end;
  273. function InterLockedIncrement64 (var Target: int64) : int64; assembler; nostackframe;
  274. asm
  275. .LInterIncLockedLoop:
  276. ldxr x1,[x0]
  277. add x1,x1,#1
  278. stxr w2,x1,[x0]
  279. cbnz w2,.LInterIncLockedLoop
  280. mov x0,x1
  281. end;
  282. function InterLockedExchange64 (var Target: int64;Source : int64) : int64; assembler; nostackframe;
  283. asm
  284. .LInterLockedXchgLoop:
  285. ldxr x2,[x0]
  286. stxr w3,x1,[x0]
  287. cbnz w3,.LInterLockedXchgLoop
  288. mov x0,x2
  289. end;
  290. function InterLockedExchangeAdd64 (var Target: int64;Source : int64) : int64; assembler; nostackframe;
  291. asm
  292. .LInterLockedXchgAddLoop:
  293. ldxr x2,[x0]
  294. add x4,x2,x1
  295. stxr w3,x4,[x0]
  296. cbnz w3,.LInterLockedXchgAddLoop
  297. mov x0,x2
  298. end;
  299. function InterLockedCompareExchange64(var Target: int64; NewValue, Comperand : int64): int64; assembler; nostackframe;
  300. asm
  301. .LInterlockedCompareExchangeLoop:
  302. ldxr x3,[x0]
  303. cmp x3,x2
  304. csel x4,x1,x3,eq
  305. stxr w5,x4,[x0]
  306. cbnz w5,.LInterlockedCompareExchangeLoop
  307. mov x0,x3
  308. end;
  309. {$ifndef FPC_SYSTEM_HAS_MEM_BARRIER}
  310. {$define FPC_SYSTEM_HAS_MEM_BARRIER}
  311. procedure ReadBarrier;assembler;nostackframe;{$ifdef SYSTEMINLINE}inline;{$endif}
  312. asm
  313. { dmb ishld }
  314. dmb #9
  315. end;
  316. procedure ReadDependencyBarrier;{$ifdef SYSTEMINLINE}inline;{$endif}
  317. begin
  318. { reads imply barrier on earlier reads depended on }
  319. end;
  320. procedure ReadWriteBarrier;assembler;nostackframe;{$ifdef SYSTEMINLINE}inline;{$endif}
  321. asm
  322. { dmb ish }
  323. dmb #11
  324. end;
  325. procedure WriteBarrier;assembler;nostackframe;{$ifdef SYSTEMINLINE}inline;{$endif}
  326. asm
  327. { dmb ishst }
  328. dmb #10
  329. end;
  330. {$endif}