aarch64.inc 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. procedure fpc_enable_fpu_exceptions;
  45. begin
  46. { clear all "exception happened" flags we care about}
  47. setfpsr(getfpsr and not(fpu_exception_mask shr fpu_exception_mask_to_status_mask_shift));
  48. { enable invalid operations and division by zero exceptions. }
  49. setfpcr(getfpcr or fpu_exception_mask);
  50. end;
  51. procedure fpc_cpuinit;
  52. begin
  53. { don't let libraries influence the FPU cw set by the host program }
  54. if not IsLibrary then
  55. fpc_enable_fpu_exceptions;
  56. end;
  57. {****************************************************************************
  58. Move / Fill
  59. ****************************************************************************}
  60. {****************************************************************************
  61. String
  62. ****************************************************************************}
  63. {$define FPC_SYSTEM_HAS_GET_CALLER_ADDR}
  64. function get_caller_addr(framebp:pointer;addr:pointer=nil):pointer;assembler; nostackframe;
  65. asm
  66. cbz x0, .Lcaller_addr_invalid
  67. ldur x0, [x0]
  68. cbz x0, .Lcaller_addr_invalid
  69. ldur x0, [x0, #8]
  70. .Lcaller_addr_invalid:
  71. end;
  72. {$define FPC_SYSTEM_HAS_GET_CALLER_FRAME}
  73. function get_caller_frame(framebp:pointer;addr:pointer=nil):pointer;assembler; nostackframe;
  74. asm
  75. cbz x0, .Lcaller_addr_invalid
  76. ldur x0, [x0]
  77. .Lcaller_addr_invalid:
  78. end;
  79. {$define FPC_SYSTEM_HAS_SPTR}
  80. Function Sptr : Pointer;assembler; nostackframe;
  81. asm
  82. mov x0, sp
  83. end;
  84. {****************************************************************************
  85. Str()
  86. ****************************************************************************}
  87. { int_str: generic implementation is used for now }
  88. {****************************************************************************
  89. Multithreading
  90. ****************************************************************************}
  91. { perform a thread-safe inc/dec }
  92. {$define FPC_SYSTEM_HAS_DECLOCKED_LONGINT}
  93. function declocked(var l : longint) : boolean;assembler;nostackframe;
  94. { input: address of l in x0 }
  95. { output: boolean indicating whether l is zero after decrementing }
  96. asm
  97. .LDecLockedLoop:
  98. ldxr w1,[x0]
  99. sub w1,w1,#1
  100. stxr w2,w1,[x0]
  101. cbnz w2,.LDecLockedLoop
  102. cset w0, eq
  103. end;
  104. {$define FPC_SYSTEM_HAS_INCLOCKED_LONGINT}
  105. procedure inclocked(var l : longint);assembler;nostackframe;
  106. asm
  107. .LIncLockedLoop:
  108. ldxr w1,[x0]
  109. add w1,w1,#1
  110. stxr w2,w1,[x0]
  111. cbnz w2,.LIncLockedLoop
  112. end;
  113. {$define FPC_SYSTEM_HAS_DECLOCKED_INT64}
  114. function declocked(var l : int64) : boolean;assembler;nostackframe;
  115. { input: address of l in x0 }
  116. { output: boolean indicating whether l is zero after decrementing }
  117. asm
  118. .LDecLockedLoop:
  119. ldxr x1,[x0]
  120. subs x1,x1,#1
  121. stxr w2,x1,[x0]
  122. cbnz w2,.LDecLockedLoop
  123. cset w0, eq
  124. end;
  125. {$define FPC_SYSTEM_HAS_INCLOCKED_INT64}
  126. procedure inclocked(var l : int64);assembler;nostackframe;
  127. asm
  128. .LIncLockedLoop:
  129. ldxr x1,[x0]
  130. add x1,x1,#1
  131. stxr w2,x1,[x0]
  132. cbnz w2,.LIncLockedLoop
  133. end;
  134. function InterLockedDecrement (var Target: longint) : longint; assembler; nostackframe;
  135. { input: address of target in x0 }
  136. { output: target-1 in x0 }
  137. { side-effect: target := target-1 }
  138. asm
  139. .LInterDecLockedLoop:
  140. ldxr w1,[x0]
  141. sub w1,w1,#1
  142. stxr w2,w1,[x0]
  143. cbnz w2,.LInterDecLockedLoop
  144. mov w0,w1
  145. end;
  146. function InterLockedIncrement (var Target: longint) : longint; assembler; nostackframe;
  147. { input: address of target in x0 }
  148. { output: target+1 in x0 }
  149. { side-effect: target := target+1 }
  150. asm
  151. .LInterIncLockedLoop:
  152. ldxr w1,[x0]
  153. add w1,w1,#1
  154. stxr w2,w1,[x0]
  155. cbnz w2,.LInterIncLockedLoop
  156. mov w0,w1
  157. end;
  158. function InterLockedExchange (var Target: longint;Source : longint) : longint; assembler; nostackframe;
  159. { input: address of target in x0, source in w1 }
  160. { output: target in x0 }
  161. { side-effect: target := source }
  162. asm
  163. .LInterLockedXchgLoop:
  164. ldxr w2,[x0]
  165. stxr w3,w1,[x0]
  166. cbnz w3,.LInterLockedXchgLoop
  167. mov w0,w2
  168. end;
  169. function InterLockedExchangeAdd (var Target: longint;Source : longint) : longint; assembler; nostackframe;
  170. asm
  171. .LInterLockedXchgAddLoop:
  172. ldxr w2,[x0]
  173. add w4,w2,w1
  174. stxr w3,w4,[x0]
  175. cbnz w3,.LInterLockedXchgAddLoop
  176. mov w0,w2
  177. end;
  178. function InterlockedCompareExchange(var Target: longint; NewValue: longint; Comperand: longint): longint; assembler; nostackframe;
  179. { input: address of target in x0, newvalue in w1, comparand in w2 }
  180. { output: value stored in target before entry of the function }
  181. { side-effect: NewValue stored in target if (target = comparand) }
  182. asm
  183. .LInterlockedCompareExchangeLoop:
  184. ldxr w3,[x0]
  185. cmp w3,w2
  186. csel w4,w1,w3,eq
  187. stxr w5,w4,[x0]
  188. cbnz w5,.LInterlockedCompareExchangeLoop
  189. mov w0,w3
  190. end;
  191. function InterLockedDecrement64 (var Target: int64) : int64; assembler; nostackframe;
  192. asm
  193. .LInterDecLockedLoop:
  194. ldxr x1,[x0]
  195. sub x1,x1,#1
  196. stxr w2,x1,[x0]
  197. cbnz w2,.LInterDecLockedLoop
  198. mov x0,x1
  199. end;
  200. function InterLockedIncrement64 (var Target: int64) : int64; assembler; nostackframe;
  201. asm
  202. .LInterIncLockedLoop:
  203. ldxr x1,[x0]
  204. add x1,x1,#1
  205. stxr w2,x1,[x0]
  206. cbnz w2,.LInterIncLockedLoop
  207. mov x0,x1
  208. end;
  209. function InterLockedExchange64 (var Target: int64;Source : int64) : int64; assembler; nostackframe;
  210. asm
  211. .LInterLockedXchgLoop:
  212. ldxr x2,[x0]
  213. stxr w3,x1,[x0]
  214. cbnz w3,.LInterLockedXchgLoop
  215. mov x0,x2
  216. end;
  217. function InterLockedExchangeAdd64 (var Target: int64;Source : int64) : int64; assembler; nostackframe;
  218. asm
  219. .LInterLockedXchgAddLoop:
  220. ldxr x2,[x0]
  221. add x4,x2,x1
  222. stxr w3,x4,[x0]
  223. cbnz w3,.LInterLockedXchgAddLoop
  224. mov x0,x2
  225. end;
  226. function InterLockedCompareExchange64(var Target: int64; NewValue, Comperand : int64): int64; assembler; nostackframe;
  227. asm
  228. .LInterlockedCompareExchangeLoop:
  229. ldxr x3,[x0]
  230. cmp x3,x2
  231. csel x4,x1,x3,eq
  232. stxr w5,x4,[x0]
  233. cbnz w5,.LInterlockedCompareExchangeLoop
  234. mov x0,x3
  235. end;
  236. {$ifndef FPC_SYSTEM_HAS_MEM_BARRIER}
  237. {$define FPC_SYSTEM_HAS_MEM_BARRIER}
  238. procedure ReadBarrier;assembler;nostackframe;{$ifdef SYSTEMINLINE}inline;{$endif}
  239. asm
  240. { dmb ishld }
  241. dmb #9
  242. end;
  243. procedure ReadDependencyBarrier;{$ifdef SYSTEMINLINE}inline;{$endif}
  244. begin
  245. { reads imply barrier on earlier reads depended on }
  246. end;
  247. procedure ReadWriteBarrier;assembler;nostackframe;{$ifdef SYSTEMINLINE}inline;{$endif}
  248. asm
  249. { dmb ish }
  250. dmb #11
  251. end;
  252. procedure WriteBarrier;assembler;nostackframe;{$ifdef SYSTEMINLINE}inline;{$endif}
  253. asm
  254. { dmb ishst }
  255. dmb #10
  256. end;
  257. {$endif}