aarch64.inc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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); nostackframe; assembler;
  33. asm
  34. msr fpcr,x0
  35. end;
  36. function getfpsr: qword; nostackframe; assembler;
  37. asm
  38. mrs x0,fpsr
  39. end;
  40. procedure setfpsr(val: qword); 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 : qword;
  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 : qword;
  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. {$ifndef cpullvm}
  138. cbz x0, .Lcaller_addr_invalid
  139. {$else cpullvm}
  140. movn w1, #0
  141. cmp x0, x1
  142. csel x0, xzr, x0, ls
  143. b.ls .Lcaller_addr_invalid
  144. {$endif cpullvm}
  145. ldur x0, [x0, #8]
  146. .Lcaller_addr_invalid:
  147. end;
  148. {$define FPC_SYSTEM_HAS_GET_CALLER_FRAME}
  149. function get_caller_frame(framebp:pointer;addr:pointer=nil):pointer;assembler; nostackframe;
  150. asm
  151. cbz x0, .Lcaller_addr_invalid
  152. ldur x0, [x0]
  153. .Lcaller_addr_invalid:
  154. end;
  155. {$define FPC_SYSTEM_HAS_SPTR}
  156. Function Sptr : Pointer;assembler; nostackframe;
  157. asm
  158. mov x0, sp
  159. end;
  160. {****************************************************************************
  161. Str()
  162. ****************************************************************************}
  163. { int_str: generic implementation is used for now }
  164. {****************************************************************************
  165. Multithreading
  166. ****************************************************************************}
  167. { perform a thread-safe inc/dec }
  168. {$define FPC_SYSTEM_HAS_DECLOCKED_LONGINT}
  169. function declocked(var l : longint) : boolean;assembler;nostackframe;
  170. { input: address of l in x0 }
  171. { output: boolean indicating whether l is zero after decrementing }
  172. asm
  173. {$ifdef CPUAARCH64_HAS_LSE}
  174. mov w1,#-1
  175. ldadd w1,w2,[x0]
  176. adds w2,w2,w1
  177. cset w0,eq
  178. {$else CPUAARCH64_HAS_LSE}
  179. .LDecLockedLoop:
  180. ldxr w1,[x0]
  181. sub w1,w1,#1
  182. stxr w2,w1,[x0]
  183. cbnz w2,.LDecLockedLoop
  184. cset w0, eq
  185. {$endif CPUAARCH64_HAS_LSE}
  186. end;
  187. {$define FPC_SYSTEM_HAS_INCLOCKED_LONGINT}
  188. procedure inclocked(var l : longint);assembler;nostackframe;
  189. asm
  190. {$ifdef CPUAARCH64_HAS_LSE}
  191. mov w1,#1
  192. ldadd w1,w2,[x0]
  193. {$else CPUAARCH64_HAS_LSE}
  194. .LIncLockedLoop:
  195. ldxr w1,[x0]
  196. add w1,w1,#1
  197. stxr w2,w1,[x0]
  198. cbnz w2,.LIncLockedLoop
  199. {$endif CPUAARCH64_HAS_LSE}
  200. end;
  201. {$define FPC_SYSTEM_HAS_DECLOCKED_INT64}
  202. function declocked(var l : int64) : boolean;assembler;nostackframe;
  203. { input: address of l in x0 }
  204. { output: boolean indicating whether l is zero after decrementing }
  205. asm
  206. {$ifdef CPUAARCH64_HAS_LSE}
  207. mov x1,#-1
  208. ldadd x1,x2,[x0]
  209. adds x2,x2,x1
  210. cset w0,eq
  211. {$else CPUAARCH64_HAS_LSE}
  212. .LDecLockedLoop:
  213. ldxr x1,[x0]
  214. subs x1,x1,#1
  215. stxr w2,x1,[x0]
  216. cbnz w2,.LDecLockedLoop
  217. cset w0, eq
  218. {$endif CPUAARCH64_HAS_LSE}
  219. end;
  220. {$define FPC_SYSTEM_HAS_INCLOCKED_INT64}
  221. procedure inclocked(var l : int64);assembler;nostackframe;
  222. asm
  223. {$ifdef CPUAARCH64_HAS_LSE}
  224. mov x1,#1
  225. ldadd x1,x2,[x0]
  226. {$else CPUAARCH64_HAS_LSE}
  227. .LIncLockedLoop:
  228. ldxr x1,[x0]
  229. add x1,x1,#1
  230. stxr w2,x1,[x0]
  231. cbnz w2,.LIncLockedLoop
  232. {$endif CPUAARCH64_HAS_LSE}
  233. end;
  234. function InterLockedDecrement (var Target: longint) : longint; assembler; nostackframe;
  235. { input: address of target in x0 }
  236. { output: target-1 in x0 }
  237. { side-effect: target := target-1 }
  238. asm
  239. {$ifdef CPUAARCH64_HAS_LSE}
  240. mov w1,#-1
  241. ldadd w1,w2,[x0]
  242. add w0,w2,w1
  243. {$else CPUAARCH64_HAS_LSE}
  244. .LInterDecLockedLoop:
  245. ldxr w1,[x0]
  246. sub w1,w1,#1
  247. stxr w2,w1,[x0]
  248. cbnz w2,.LInterDecLockedLoop
  249. mov w0,w1
  250. {$endif CPUAARCH64_HAS_LSE}
  251. end;
  252. function InterLockedIncrement (var Target: longint) : longint; assembler; nostackframe;
  253. { input: address of target in x0 }
  254. { output: target+1 in x0 }
  255. { side-effect: target := target+1 }
  256. asm
  257. {$ifdef CPUAARCH64_HAS_LSE}
  258. mov w1,#1
  259. ldadd w1,w2,[x0]
  260. add w0,w2,w1
  261. {$else CPUAARCH64_HAS_LSE}
  262. .LInterIncLockedLoop:
  263. ldxr w1,[x0]
  264. add w1,w1,#1
  265. stxr w2,w1,[x0]
  266. cbnz w2,.LInterIncLockedLoop
  267. mov w0,w1
  268. {$endif CPUAARCH64_HAS_LSE}
  269. end;
  270. function InterLockedExchange (var Target: longint;Source : longint) : longint; assembler; nostackframe;
  271. { input: address of target in x0, source in w1 }
  272. { output: target in x0 }
  273. { side-effect: target := source }
  274. asm
  275. {$ifdef CPUAARCH64_HAS_LSE}
  276. swp w1,w0,[x0]
  277. {$else CPUAARCH64_HAS_LSE}
  278. .LInterLockedXchgLoop:
  279. ldxr w2,[x0]
  280. stxr w3,w1,[x0]
  281. cbnz w3,.LInterLockedXchgLoop
  282. mov w0,w2
  283. {$endif CPUAARCH64_HAS_LSE}
  284. end;
  285. function InterLockedExchangeAdd (var Target: longint;Source : longint) : longint; assembler; nostackframe;
  286. asm
  287. {$ifdef CPUAARCH64_HAS_LSE}
  288. ldadd w1,w0,[x0]
  289. {$else CPUAARCH64_HAS_LSE}
  290. .LInterLockedXchgAddLoop:
  291. ldxr w2,[x0]
  292. add w4,w2,w1
  293. stxr w3,w4,[x0]
  294. cbnz w3,.LInterLockedXchgAddLoop
  295. mov w0,w2
  296. {$endif CPUAARCH64_HAS_LSE}
  297. end;
  298. function InterlockedCompareExchange(var Target: longint; NewValue: longint; Comperand: longint): longint; assembler; nostackframe;
  299. { input: address of target in x0, newvalue in w1, comparand in w2 }
  300. { output: value stored in target before entry of the function }
  301. { side-effect: NewValue stored in target if (target = comparand) }
  302. asm
  303. {$ifdef CPUAARCH64_HAS_LSE}
  304. cas w2,w1,[x0]
  305. mov w0,w2
  306. {$else CPUAARCH64_HAS_LSE}
  307. .LInterlockedCompareExchangeLoop:
  308. ldxr w3,[x0]
  309. cmp w3,w2
  310. csel w4,w1,w3,eq
  311. stxr w5,w4,[x0]
  312. cbnz w5,.LInterlockedCompareExchangeLoop
  313. mov w0,w3
  314. {$endif CPUAARCH64_HAS_LSE}
  315. end;
  316. function InterLockedDecrement64 (var Target: int64) : int64; assembler; nostackframe;
  317. asm
  318. {$ifdef CPUAARCH64_HAS_LSE}
  319. mov x1,#-1
  320. ldadd x1,x2,[x0]
  321. add x0,x2,x1
  322. {$else CPUAARCH64_HAS_LSE}
  323. .LInterDecLockedLoop:
  324. ldxr x1,[x0]
  325. sub x1,x1,#1
  326. stxr w2,x1,[x0]
  327. cbnz w2,.LInterDecLockedLoop
  328. mov x0,x1
  329. {$endif CPUAARCH64_HAS_LSE}
  330. end;
  331. function InterLockedIncrement64 (var Target: int64) : int64; assembler; nostackframe;
  332. asm
  333. {$ifdef CPUAARCH64_HAS_LSE}
  334. mov x1,#1
  335. ldadd x1,x2,[x0]
  336. add x0,x2,x1
  337. {$else CPUAARCH64_HAS_LSE}
  338. .LInterIncLockedLoop:
  339. ldxr x1,[x0]
  340. add x1,x1,#1
  341. stxr w2,x1,[x0]
  342. cbnz w2,.LInterIncLockedLoop
  343. mov x0,x1
  344. {$endif CPUAARCH64_HAS_LSE}
  345. end;
  346. function InterLockedExchange64 (var Target: int64;Source : int64) : int64; assembler; nostackframe;
  347. asm
  348. {$ifdef CPUAARCH64_HAS_LSE}
  349. swp x1,x0,[x0]
  350. {$else CPUAARCH64_HAS_LSE}
  351. .LInterLockedXchgLoop:
  352. ldxr x2,[x0]
  353. stxr w3,x1,[x0]
  354. cbnz w3,.LInterLockedXchgLoop
  355. mov x0,x2
  356. {$endif CPUAARCH64_HAS_LSE}
  357. end;
  358. function InterLockedExchangeAdd64 (var Target: int64;Source : int64) : int64; assembler; nostackframe;
  359. asm
  360. {$ifdef CPUAARCH64_HAS_LSE}
  361. ldadd x1,x0,[x0]
  362. {$else CPUAARCH64_HAS_LSE}
  363. .LInterLockedXchgAddLoop:
  364. ldxr x2,[x0]
  365. add x4,x2,x1
  366. stxr w3,x4,[x0]
  367. cbnz w3,.LInterLockedXchgAddLoop
  368. mov x0,x2
  369. {$endif CPUAARCH64_HAS_LSE}
  370. end;
  371. function InterLockedCompareExchange64(var Target: int64; NewValue, Comperand : int64): int64; assembler; nostackframe;
  372. asm
  373. {$ifdef CPUAARCH64_HAS_LSE}
  374. cas x2,x1,[x0]
  375. mov x0,x2
  376. {$else CPUAARCH64_HAS_LSE}
  377. .LInterlockedCompareExchangeLoop:
  378. ldxr x3,[x0]
  379. cmp x3,x2
  380. csel x4,x1,x3,eq
  381. stxr w5,x4,[x0]
  382. cbnz w5,.LInterlockedCompareExchangeLoop
  383. mov x0,x3
  384. {$endif CPUAARCH64_HAS_LSE}
  385. end;
  386. {$ifndef FPC_SYSTEM_HAS_MEM_BARRIER}
  387. {$define FPC_SYSTEM_HAS_MEM_BARRIER}
  388. procedure ReadBarrier;assembler;nostackframe;{$ifdef SYSTEMINLINE}inline;{$endif}
  389. asm
  390. // { dmb ishld }
  391. dmb #9
  392. end;
  393. procedure ReadDependencyBarrier;{$ifdef SYSTEMINLINE}inline;{$endif}
  394. begin
  395. { reads imply barrier on earlier reads depended on }
  396. end;
  397. procedure ReadWriteBarrier;assembler;nostackframe;{$ifdef SYSTEMINLINE}inline;{$endif}
  398. asm
  399. // { dmb ish }
  400. dmb #11
  401. end;
  402. procedure WriteBarrier;assembler;nostackframe;{$ifdef SYSTEMINLINE}inline;{$endif}
  403. asm
  404. // { dmb ishst }
  405. dmb #10
  406. end;
  407. {$endif}