aarch64.inc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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: qword; nostackframe; assembler;
  29. asm
  30. mrs x0,fpcr
  31. end;
  32. procedure setfpcr(val: qword);
  33. begin
  34. asm
  35. ldr x0,val
  36. msr fpcr,x0
  37. {$if not defined(darwin) or defined(ios) or defined(watchos) or defined(tvos)}
  38. // read back the fpcr because on several (non-macOS) platforms it's raz
  39. mrs x0,fpcr
  40. str x0, val
  41. {$endif}
  42. end;
  43. DefaultFPUControlWord:=val;
  44. end;
  45. function getfpsr: qword; nostackframe; assembler;
  46. asm
  47. mrs x0,fpsr
  48. end;
  49. function GetNativeFPUControlWord: TNativeFPUControlWord;
  50. begin
  51. result:=getfpcr;
  52. end;
  53. procedure SetNativeFPUControlWord(const cw: TNativeFPUControlWord);
  54. begin
  55. setfpcr(cw);
  56. end;
  57. procedure setfpsr(val: qword); nostackframe; assembler;
  58. asm
  59. msr fpsr, x0
  60. end;
  61. const
  62. FPSR_IOC = 1;
  63. FPSR_DZC = 1 shl 1;
  64. FPSR_OFC = 1 shl 2;
  65. FPSR_UFC = 1 shl 3;
  66. FPSR_IXC = 1 shl 4;
  67. FPSR_IDC = 1 shl 7;
  68. FPSR_EXCEPTIONS = FPSR_IOC or FPSR_DZC or FPSR_OFC or FPSR_UFC or FPSR_IXC or FPSR_IDC;
  69. procedure RaisePendingExceptions;
  70. var
  71. fpsr : qword;
  72. f: TFPUException;
  73. begin
  74. fpsr:=getfpsr;
  75. if (fpsr and FPSR_DZC) <> 0 then
  76. float_raise(exZeroDivide);
  77. if (fpsr and FPSR_OFC) <> 0 then
  78. float_raise(exOverflow);
  79. if (fpsr and FPSR_UFC) <> 0 then
  80. float_raise(exUnderflow);
  81. if (fpsr and FPSR_IOC) <> 0 then
  82. float_raise(exInvalidOp);
  83. if (fpsr and FPSR_IXC) <> 0 then
  84. float_raise(exPrecision);
  85. if (fpsr and FPSR_IDC) <> 0 then
  86. float_raise(exDenormalized);
  87. { now the soft float exceptions }
  88. for f in softfloat_exception_flags do
  89. float_raise(f);
  90. end;
  91. { as so far no AArch64 flavour which supports hard floating point exceptions, we use solely
  92. the softfloat_exception_mask for masking as the masking flags are RAZ and WI if floating point
  93. exceptions are not supported }
  94. procedure fpc_throwfpuexception;[public,alias:'FPC_THROWFPUEXCEPTION'];
  95. var
  96. fpsr : qword;
  97. f: TFPUException;
  98. begin
  99. { at this point, we know already, that an exception will be risen }
  100. fpsr:=getfpsr;
  101. { check, if the exception is masked }
  102. if ((fpsr and FPSR_DZC) <> 0) and (exZeroDivide in softfloat_exception_mask) then
  103. fpsr:=fpsr and not(FPSR_DZC);
  104. if ((fpsr and FPSR_OFC) <> 0) and (exOverflow in softfloat_exception_mask) then
  105. fpsr:=fpsr and not(FPSR_OFC);
  106. if ((fpsr and FPSR_UFC) <> 0) and (exUnderflow in softfloat_exception_mask) then
  107. fpsr:=fpsr and not(FPSR_UFC);
  108. if ((fpsr and FPSR_IOC) <> 0) and (exInvalidOp in softfloat_exception_mask) then
  109. fpsr:=fpsr and not(FPSR_IOC);
  110. if ((fpsr and FPSR_IXC) <> 0) and (exPrecision in softfloat_exception_mask) then
  111. fpsr:=fpsr and not(FPSR_IXC);
  112. if ((fpsr and FPSR_IDC) <> 0) and (exDenormalized in softfloat_exception_mask) then
  113. fpsr:=fpsr and not(FPSR_IDC);
  114. setfpsr(fpsr);
  115. if (fpsr and FPSR_EXCEPTIONS)<>0 then
  116. RaisePendingExceptions;
  117. end;
  118. {$define FPC_SYSTEM_HAS_SYSINITFPU}
  119. procedure SysInitFPU;
  120. begin
  121. softfloat_rounding_mode:=rmNearest;
  122. { 0 is rmNearest }
  123. setfpcr(getfpcr and $ff3fffff);
  124. { clear all "exception happened" flags we care about}
  125. setfpsr(getfpsr and not(fpu_exception_mask shr fpu_exception_mask_to_status_mask_shift));
  126. { enable invalid operations, overflow and division by zero exceptions. }
  127. setfpcr(((getfpcr and not(fpu_exception_mask)) or fpu_dze or fpu_ofe or fpu_ioe));
  128. softfloat_exception_mask:=[float_flag_underflow,float_flag_inexact,float_flag_denormal];
  129. softfloat_exception_flags:=[];
  130. end;
  131. {****************************************************************************
  132. Move / Fill
  133. ****************************************************************************}
  134. {****************************************************************************
  135. String
  136. ****************************************************************************}
  137. {$define FPC_SYSTEM_HAS_GET_CALLER_ADDR}
  138. function get_caller_addr(framebp:pointer;addr:pointer=nil):pointer;assembler; nostackframe;
  139. asm
  140. cbz x0, .Lcaller_addr_invalid
  141. {$ifdef cpullvm}
  142. movn w1, #0
  143. cmp x0, x1
  144. csel x0, xzr, x0, ls
  145. b.ls .Lcaller_addr_invalid
  146. {$endif cpullvm}
  147. ldur x0, [x0, #8]
  148. .Lcaller_addr_invalid:
  149. end;
  150. {$define FPC_SYSTEM_HAS_GET_CALLER_FRAME}
  151. function get_caller_frame(framebp:pointer;addr:pointer=nil):pointer;assembler; nostackframe;
  152. asm
  153. cbz x0, .Lcaller_addr_invalid
  154. ldur x0, [x0]
  155. .Lcaller_addr_invalid:
  156. end;
  157. {$define FPC_SYSTEM_HAS_SPTR}
  158. Function Sptr : Pointer;assembler; nostackframe;
  159. asm
  160. mov x0, sp
  161. end;
  162. {****************************************************************************
  163. Str()
  164. ****************************************************************************}
  165. { int_str: generic implementation is used for now }
  166. {****************************************************************************
  167. Multithreading
  168. ****************************************************************************}
  169. { perform a thread-safe inc/dec }
  170. {$define FPC_SYSTEM_HAS_DECLOCKED_LONGINT}
  171. function declocked(var l : longint) : boolean;assembler;nostackframe;
  172. { input: address of l in x0 }
  173. { output: boolean indicating whether l is zero after decrementing }
  174. asm
  175. {$ifdef CPUAARCH64_HAS_LSE}
  176. mov w1,#-1
  177. ldadd w1,w2,[x0]
  178. adds w2,w2,w1
  179. cset w0,eq
  180. {$else CPUAARCH64_HAS_LSE}
  181. .LDecLockedLoop:
  182. ldxr w1,[x0]
  183. subs w1,w1,#1
  184. stxr w2,w1,[x0]
  185. cbnz w2,.LDecLockedLoop
  186. cset w0, eq
  187. {$endif CPUAARCH64_HAS_LSE}
  188. end;
  189. {$define FPC_SYSTEM_HAS_INCLOCKED_LONGINT}
  190. procedure inclocked(var l : longint);assembler;nostackframe;
  191. asm
  192. {$ifdef CPUAARCH64_HAS_LSE}
  193. mov w1,#1
  194. ldadd w1,w2,[x0]
  195. {$else CPUAARCH64_HAS_LSE}
  196. .LIncLockedLoop:
  197. ldxr w1,[x0]
  198. add w1,w1,#1
  199. stxr w2,w1,[x0]
  200. cbnz w2,.LIncLockedLoop
  201. {$endif CPUAARCH64_HAS_LSE}
  202. end;
  203. {$define FPC_SYSTEM_HAS_DECLOCKED_INT64}
  204. function declocked(var l : int64) : boolean;assembler;nostackframe;
  205. { input: address of l in x0 }
  206. { output: boolean indicating whether l is zero after decrementing }
  207. asm
  208. {$ifdef CPUAARCH64_HAS_LSE}
  209. mov x1,#-1
  210. ldadd x1,x2,[x0]
  211. adds x2,x2,x1
  212. cset w0,eq
  213. {$else CPUAARCH64_HAS_LSE}
  214. .LDecLockedLoop:
  215. ldxr x1,[x0]
  216. subs x1,x1,#1
  217. stxr w2,x1,[x0]
  218. cbnz w2,.LDecLockedLoop
  219. cset w0, eq
  220. {$endif CPUAARCH64_HAS_LSE}
  221. end;
  222. {$define FPC_SYSTEM_HAS_INCLOCKED_INT64}
  223. procedure inclocked(var l : int64);assembler;nostackframe;
  224. asm
  225. {$ifdef CPUAARCH64_HAS_LSE}
  226. mov x1,#1
  227. ldadd x1,x2,[x0]
  228. {$else CPUAARCH64_HAS_LSE}
  229. .LIncLockedLoop:
  230. ldxr x1,[x0]
  231. add x1,x1,#1
  232. stxr w2,x1,[x0]
  233. cbnz w2,.LIncLockedLoop
  234. {$endif CPUAARCH64_HAS_LSE}
  235. end;
  236. function InterLockedDecrement (var Target: longint) : longint; assembler; nostackframe;
  237. { input: address of target in x0 }
  238. { output: target-1 in x0 }
  239. { side-effect: target := target-1 }
  240. asm
  241. {$ifdef CPUAARCH64_HAS_LSE}
  242. mov w1,#-1
  243. ldadd w1,w2,[x0]
  244. add w0,w2,w1
  245. {$else CPUAARCH64_HAS_LSE}
  246. .LInterDecLockedLoop:
  247. ldxr w1,[x0]
  248. subs w1,w1,#1
  249. stxr w2,w1,[x0]
  250. cbnz w2,.LInterDecLockedLoop
  251. mov w0,w1
  252. {$endif CPUAARCH64_HAS_LSE}
  253. end;
  254. function InterLockedIncrement (var Target: longint) : longint; assembler; nostackframe;
  255. { input: address of target in x0 }
  256. { output: target+1 in x0 }
  257. { side-effect: target := target+1 }
  258. asm
  259. {$ifdef CPUAARCH64_HAS_LSE}
  260. mov w1,#1
  261. ldadd w1,w2,[x0]
  262. add w0,w2,w1
  263. {$else CPUAARCH64_HAS_LSE}
  264. .LInterIncLockedLoop:
  265. ldxr w1,[x0]
  266. add w1,w1,#1
  267. stxr w2,w1,[x0]
  268. cbnz w2,.LInterIncLockedLoop
  269. mov w0,w1
  270. {$endif CPUAARCH64_HAS_LSE}
  271. end;
  272. function InterLockedExchange (var Target: longint;Source : longint) : longint; assembler; nostackframe;
  273. { input: address of target in x0, source in w1 }
  274. { output: target in x0 }
  275. { side-effect: target := source }
  276. asm
  277. {$ifdef CPUAARCH64_HAS_LSE}
  278. swp w1,w0,[x0]
  279. {$else CPUAARCH64_HAS_LSE}
  280. .LInterLockedXchgLoop:
  281. ldxr w2,[x0]
  282. stxr w3,w1,[x0]
  283. cbnz w3,.LInterLockedXchgLoop
  284. mov w0,w2
  285. {$endif CPUAARCH64_HAS_LSE}
  286. end;
  287. function InterLockedExchangeAdd (var Target: longint;Source : longint) : longint; assembler; nostackframe;
  288. asm
  289. {$ifdef CPUAARCH64_HAS_LSE}
  290. ldadd w1,w0,[x0]
  291. {$else CPUAARCH64_HAS_LSE}
  292. .LInterLockedXchgAddLoop:
  293. ldxr w2,[x0]
  294. add w4,w2,w1
  295. stxr w3,w4,[x0]
  296. cbnz w3,.LInterLockedXchgAddLoop
  297. mov w0,w2
  298. {$endif CPUAARCH64_HAS_LSE}
  299. end;
  300. function InterlockedCompareExchange(var Target: longint; NewValue: longint; Comperand: longint): longint; assembler; nostackframe;
  301. { input: address of target in x0, newvalue in w1, comparand in w2 }
  302. { output: value stored in target before entry of the function }
  303. { side-effect: NewValue stored in target if (target = comparand) }
  304. asm
  305. {$ifdef CPUAARCH64_HAS_LSE}
  306. cas w2,w1,[x0]
  307. mov w0,w2
  308. {$else CPUAARCH64_HAS_LSE}
  309. .LInterlockedCompareExchangeLoop:
  310. ldxr w3,[x0]
  311. cmp w3,w2
  312. csel w4,w1,w3,eq
  313. stxr w5,w4,[x0]
  314. cbnz w5,.LInterlockedCompareExchangeLoop
  315. mov w0,w3
  316. {$endif CPUAARCH64_HAS_LSE}
  317. end;
  318. function InterLockedDecrement64 (var Target: int64) : int64; assembler; nostackframe;
  319. asm
  320. {$ifdef CPUAARCH64_HAS_LSE}
  321. mov x1,#-1
  322. ldadd x1,x2,[x0]
  323. add x0,x2,x1
  324. {$else CPUAARCH64_HAS_LSE}
  325. .LInterDecLockedLoop:
  326. ldxr x1,[x0]
  327. sub x1,x1,#1
  328. stxr w2,x1,[x0]
  329. cbnz w2,.LInterDecLockedLoop
  330. mov x0,x1
  331. {$endif CPUAARCH64_HAS_LSE}
  332. end;
  333. function InterLockedIncrement64 (var Target: int64) : int64; assembler; nostackframe;
  334. asm
  335. {$ifdef CPUAARCH64_HAS_LSE}
  336. mov x1,#1
  337. ldadd x1,x2,[x0]
  338. add x0,x2,x1
  339. {$else CPUAARCH64_HAS_LSE}
  340. .LInterIncLockedLoop:
  341. ldxr x1,[x0]
  342. add x1,x1,#1
  343. stxr w2,x1,[x0]
  344. cbnz w2,.LInterIncLockedLoop
  345. mov x0,x1
  346. {$endif CPUAARCH64_HAS_LSE}
  347. end;
  348. function InterLockedExchange64 (var Target: int64;Source : int64) : int64; assembler; nostackframe;
  349. asm
  350. {$ifdef CPUAARCH64_HAS_LSE}
  351. swp x1,x0,[x0]
  352. {$else CPUAARCH64_HAS_LSE}
  353. .LInterLockedXchgLoop:
  354. ldxr x2,[x0]
  355. stxr w3,x1,[x0]
  356. cbnz w3,.LInterLockedXchgLoop
  357. mov x0,x2
  358. {$endif CPUAARCH64_HAS_LSE}
  359. end;
  360. function InterLockedExchangeAdd64 (var Target: int64;Source : int64) : int64; assembler; nostackframe;
  361. asm
  362. {$ifdef CPUAARCH64_HAS_LSE}
  363. ldadd x1,x0,[x0]
  364. {$else CPUAARCH64_HAS_LSE}
  365. .LInterLockedXchgAddLoop:
  366. ldxr x2,[x0]
  367. add x4,x2,x1
  368. stxr w3,x4,[x0]
  369. cbnz w3,.LInterLockedXchgAddLoop
  370. mov x0,x2
  371. {$endif CPUAARCH64_HAS_LSE}
  372. end;
  373. function InterLockedCompareExchange64(var Target: int64; NewValue, Comperand : int64): int64; assembler; nostackframe;
  374. asm
  375. {$ifdef CPUAARCH64_HAS_LSE}
  376. cas x2,x1,[x0]
  377. mov x0,x2
  378. {$else CPUAARCH64_HAS_LSE}
  379. .LInterlockedCompareExchangeLoop:
  380. ldxr x3,[x0]
  381. cmp x3,x2
  382. csel x4,x1,x3,eq
  383. stxr w5,x4,[x0]
  384. cbnz w5,.LInterlockedCompareExchangeLoop
  385. mov x0,x3
  386. {$endif CPUAARCH64_HAS_LSE}
  387. end;
  388. {$ifndef FPC_SYSTEM_HAS_MEM_BARRIER}
  389. {$define FPC_SYSTEM_HAS_MEM_BARRIER}
  390. procedure ReadBarrier;assembler;nostackframe;{$ifdef SYSTEMINLINE}inline;{$endif}
  391. asm
  392. // { dmb ishld }
  393. dmb #9
  394. end;
  395. procedure ReadDependencyBarrier;{$ifdef SYSTEMINLINE}inline;{$endif}
  396. begin
  397. { reads imply barrier on earlier reads depended on }
  398. end;
  399. procedure ReadWriteBarrier;assembler;nostackframe;{$ifdef SYSTEMINLINE}inline;{$endif}
  400. asm
  401. // { dmb ish }
  402. dmb #11
  403. end;
  404. procedure WriteBarrier;assembler;nostackframe;{$ifdef SYSTEMINLINE}inline;{$endif}
  405. asm
  406. // { dmb ishst }
  407. dmb #10
  408. end;
  409. {$endif}