bpf_jit_arcv2.c 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * The ARCv2 backend of Just-In-Time compiler for eBPF bytecode.
  4. *
  5. * Copyright (c) 2024 Synopsys Inc.
  6. * Author: Shahab Vahedi <shahab@synopsys.com>
  7. */
  8. #include <linux/bug.h>
  9. #include "bpf_jit.h"
  10. /* ARC core registers. */
  11. enum {
  12. ARC_R_0, ARC_R_1, ARC_R_2, ARC_R_3, ARC_R_4, ARC_R_5,
  13. ARC_R_6, ARC_R_7, ARC_R_8, ARC_R_9, ARC_R_10, ARC_R_11,
  14. ARC_R_12, ARC_R_13, ARC_R_14, ARC_R_15, ARC_R_16, ARC_R_17,
  15. ARC_R_18, ARC_R_19, ARC_R_20, ARC_R_21, ARC_R_22, ARC_R_23,
  16. ARC_R_24, ARC_R_25, ARC_R_26, ARC_R_FP, ARC_R_SP, ARC_R_ILINK,
  17. ARC_R_30, ARC_R_BLINK,
  18. /*
  19. * Having ARC_R_IMM encoded as source register means there is an
  20. * immediate that must be interpreted from the next 4 bytes. If
  21. * encoded as the destination register though, it implies that the
  22. * output of the operation is not assigned to any register. The
  23. * latter is helpful if we only care about updating the CPU status
  24. * flags.
  25. */
  26. ARC_R_IMM = 62
  27. };
  28. /*
  29. * Remarks about the rationale behind the chosen mapping:
  30. *
  31. * - BPF_REG_{1,2,3,4} are the argument registers and must be mapped to
  32. * argument registers in ARCv2 ABI: r0-r7. The r7 registers is the last
  33. * argument register in the ABI. Therefore BPF_REG_5, as the fifth
  34. * argument, must be pushed onto the stack. This is a must for calling
  35. * in-kernel functions.
  36. *
  37. * - In ARCv2 ABI, the return value is in r0 for 32-bit results and (r1,r0)
  38. * for 64-bit results. However, because they're already used for BPF_REG_1,
  39. * the next available scratch registers, r8 and r9, are the best candidates
  40. * for BPF_REG_0. After a "call" to a(n) (in-kernel) function, the result
  41. * is "mov"ed to these registers. At a BPF_EXIT, their value is "mov"ed to
  42. * (r1,r0).
  43. * It is worth mentioning that scratch registers are the best choice for
  44. * BPF_REG_0, because it is very popular in BPF instruction encoding.
  45. *
  46. * - JIT_REG_TMP is an artifact needed to translate some BPF instructions.
  47. * Its life span is one single BPF instruction. Since during the
  48. * analyze_reg_usage(), it is not known if temporary registers are used,
  49. * it is mapped to ARC's scratch registers: r10 and r11. Therefore, they
  50. * don't matter in analysing phase and don't need saving. This temporary
  51. * register is added as yet another index in the bpf2arc array, so it will
  52. * unfold like the rest of registers during the code generation process.
  53. *
  54. * - Mapping of callee-saved BPF registers, BPF_REG_{6,7,8,9}, starts from
  55. * (r15,r14) register pair. The (r13,r12) is not a good choice, because
  56. * in ARCv2 ABI, r12 is not a callee-saved register and this can cause
  57. * problem when calling an in-kernel function. Theoretically, the mapping
  58. * could start from (r14,r13), but it is not a conventional ARCv2 register
  59. * pair. To have a future proof design, I opted for this arrangement.
  60. * If/when we decide to add ARCv2 instructions that do use register pairs,
  61. * the mapping, hopefully, doesn't need to be revisited.
  62. */
  63. static const u8 bpf2arc[][2] = {
  64. /* Return value from in-kernel function, and exit value from eBPF */
  65. [BPF_REG_0] = {ARC_R_8, ARC_R_9},
  66. /* Arguments from eBPF program to in-kernel function */
  67. [BPF_REG_1] = {ARC_R_0, ARC_R_1},
  68. [BPF_REG_2] = {ARC_R_2, ARC_R_3},
  69. [BPF_REG_3] = {ARC_R_4, ARC_R_5},
  70. [BPF_REG_4] = {ARC_R_6, ARC_R_7},
  71. /* Remaining arguments, to be passed on the stack per 32-bit ABI */
  72. [BPF_REG_5] = {ARC_R_22, ARC_R_23},
  73. /* Callee-saved registers that in-kernel function will preserve */
  74. [BPF_REG_6] = {ARC_R_14, ARC_R_15},
  75. [BPF_REG_7] = {ARC_R_16, ARC_R_17},
  76. [BPF_REG_8] = {ARC_R_18, ARC_R_19},
  77. [BPF_REG_9] = {ARC_R_20, ARC_R_21},
  78. /* Read-only frame pointer to access the eBPF stack. 32-bit only. */
  79. [BPF_REG_FP] = {ARC_R_FP, },
  80. /* Register for blinding constants */
  81. [BPF_REG_AX] = {ARC_R_24, ARC_R_25},
  82. /* Temporary registers for internal use */
  83. [JIT_REG_TMP] = {ARC_R_10, ARC_R_11}
  84. };
  85. #define ARC_CALLEE_SAVED_REG_FIRST ARC_R_13
  86. #define ARC_CALLEE_SAVED_REG_LAST ARC_R_25
  87. #define REG_LO(r) (bpf2arc[(r)][0])
  88. #define REG_HI(r) (bpf2arc[(r)][1])
  89. /*
  90. * To comply with ARCv2 ABI, BPF's arg5 must be put on stack. After which,
  91. * the stack needs to be restored by ARG5_SIZE.
  92. */
  93. #define ARG5_SIZE 8
  94. /* Instruction lengths in bytes. */
  95. enum {
  96. INSN_len_normal = 4, /* Normal instructions length. */
  97. INSN_len_imm = 4 /* Length of an extra 32-bit immediate. */
  98. };
  99. /* ZZ defines the size of operation in encodings that it is used. */
  100. enum {
  101. ZZ_1_byte = 1,
  102. ZZ_2_byte = 2,
  103. ZZ_4_byte = 0,
  104. ZZ_8_byte = 3
  105. };
  106. /*
  107. * AA is mostly about address write back mode. It determines if the
  108. * address in question should be updated before usage or after:
  109. * addr += offset; data = *addr;
  110. * data = *addr; addr += offset;
  111. *
  112. * In "scaling" mode, the effective address will become the sum
  113. * of "address" + "index"*"size". The "size" is specified by the
  114. * "ZZ" field. There is no write back when AA is set for scaling:
  115. * data = *(addr + offset<<zz)
  116. */
  117. enum {
  118. AA_none = 0,
  119. AA_pre = 1, /* in assembly known as "a/aw". */
  120. AA_post = 2, /* in assembly known as "ab". */
  121. AA_scale = 3 /* in assembly known as "as". */
  122. };
  123. /* X flag determines the mode of extension. */
  124. enum {
  125. X_zero = 0,
  126. X_sign = 1
  127. };
  128. /* Condition codes. */
  129. enum {
  130. CC_always = 0, /* condition is true all the time */
  131. CC_equal = 1, /* if status32.z flag is set */
  132. CC_unequal = 2, /* if status32.z flag is clear */
  133. CC_positive = 3, /* if status32.n flag is clear */
  134. CC_negative = 4, /* if status32.n flag is set */
  135. CC_less_u = 5, /* less than (unsigned) */
  136. CC_less_eq_u = 14, /* less than or equal (unsigned) */
  137. CC_great_eq_u = 6, /* greater than or equal (unsigned) */
  138. CC_great_u = 13, /* greater than (unsigned) */
  139. CC_less_s = 11, /* less than (signed) */
  140. CC_less_eq_s = 12, /* less than or equal (signed) */
  141. CC_great_eq_s = 10, /* greater than or equal (signed) */
  142. CC_great_s = 9 /* greater than (signed) */
  143. };
  144. #define IN_U6_RANGE(x) ((x) <= (0x40 - 1) && (x) >= 0)
  145. #define IN_S9_RANGE(x) ((x) <= (0x100 - 1) && (x) >= -0x100)
  146. #define IN_S12_RANGE(x) ((x) <= (0x800 - 1) && (x) >= -0x800)
  147. #define IN_S21_RANGE(x) ((x) <= (0x100000 - 1) && (x) >= -0x100000)
  148. #define IN_S25_RANGE(x) ((x) <= (0x1000000 - 1) && (x) >= -0x1000000)
  149. /* Operands in most of the encodings. */
  150. #define OP_A(x) ((x) & 0x03f)
  151. #define OP_B(x) ((((x) & 0x07) << 24) | (((x) & 0x38) << 9))
  152. #define OP_C(x) (((x) & 0x03f) << 6)
  153. #define OP_IMM (OP_C(ARC_R_IMM))
  154. #define COND(x) (OP_A((x) & 31))
  155. #define FLAG(x) (((x) & 1) << 15)
  156. /*
  157. * The 4-byte encoding of "mov b,c":
  158. *
  159. * 0010_0bbb 0000_1010 0BBB_cccc cc00_0000
  160. *
  161. * b: BBBbbb destination register
  162. * c: cccccc source register
  163. */
  164. #define OPC_MOV 0x200a0000
  165. /*
  166. * The 4-byte encoding of "mov b,s12" (used for moving small immediates):
  167. *
  168. * 0010_0bbb 1000_1010 0BBB_ssss ssSS_SSSS
  169. *
  170. * b: BBBbbb destination register
  171. * s: SSSSSSssssss source immediate (signed)
  172. */
  173. #define OPC_MOVI 0x208a0000
  174. #define MOVI_S12(x) ((((x) & 0xfc0) >> 6) | (((x) & 0x3f) << 6))
  175. /*
  176. * The 4-byte encoding of "mov[.qq] b,u6", used for conditional
  177. * moving of even smaller immediates:
  178. *
  179. * 0010_0bbb 1100_1010 0BBB_cccc cciq_qqqq
  180. *
  181. * qq: qqqqq condition code
  182. * i: If set, c is considered a 6-bit immediate, else a reg.
  183. *
  184. * b: BBBbbb destination register
  185. * c: cccccc source
  186. */
  187. #define OPC_MOV_CC 0x20ca0000
  188. #define MOV_CC_I BIT(5)
  189. #define OPC_MOVU_CC (OPC_MOV_CC | MOV_CC_I)
  190. /*
  191. * The 4-byte encoding of "sexb b,c" (8-bit sign extension):
  192. *
  193. * 0010_0bbb 0010_1111 0BBB_cccc cc00_0101
  194. *
  195. * b: BBBbbb destination register
  196. * c: cccccc source register
  197. */
  198. #define OPC_SEXB 0x202f0005
  199. /*
  200. * The 4-byte encoding of "sexh b,c" (16-bit sign extension):
  201. *
  202. * 0010_0bbb 0010_1111 0BBB_cccc cc00_0110
  203. *
  204. * b: BBBbbb destination register
  205. * c: cccccc source register
  206. */
  207. #define OPC_SEXH 0x202f0006
  208. /*
  209. * The 4-byte encoding of "ld[zz][.x][.aa] c,[b,s9]":
  210. *
  211. * 0001_0bbb ssss_ssss SBBB_0aaz zxcc_cccc
  212. *
  213. * zz: size mode
  214. * aa: address write back mode
  215. * x: extension mode
  216. *
  217. * s9: S_ssss_ssss 9-bit signed number
  218. * b: BBBbbb source reg for address
  219. * c: cccccc destination register
  220. */
  221. #define OPC_LOAD 0x10000000
  222. #define LOAD_X(x) ((x) << 6)
  223. #define LOAD_ZZ(x) ((x) << 7)
  224. #define LOAD_AA(x) ((x) << 9)
  225. #define LOAD_S9(x) ((((x) & 0x0ff) << 16) | (((x) & 0x100) << 7))
  226. #define LOAD_C(x) ((x) & 0x03f)
  227. /* Unsigned and signed loads. */
  228. #define OPC_LDU (OPC_LOAD | LOAD_X(X_zero))
  229. #define OPC_LDS (OPC_LOAD | LOAD_X(X_sign))
  230. /* 32-bit load. */
  231. #define OPC_LD32 (OPC_LDU | LOAD_ZZ(ZZ_4_byte))
  232. /* "pop reg" is merely a "ld.ab reg,[sp,4]". */
  233. #define OPC_POP \
  234. (OPC_LD32 | LOAD_AA(AA_post) | LOAD_S9(4) | OP_B(ARC_R_SP))
  235. /*
  236. * The 4-byte encoding of "st[zz][.aa] c,[b,s9]":
  237. *
  238. * 0001_1bbb ssss_ssss SBBB_cccc cc0a_azz0
  239. *
  240. * zz: zz size mode
  241. * aa: aa address write back mode
  242. *
  243. * s9: S_ssss_ssss 9-bit signed number
  244. * b: BBBbbb source reg for address
  245. * c: cccccc source reg to be stored
  246. */
  247. #define OPC_STORE 0x18000000
  248. #define STORE_ZZ(x) ((x) << 1)
  249. #define STORE_AA(x) ((x) << 3)
  250. #define STORE_S9(x) ((((x) & 0x0ff) << 16) | (((x) & 0x100) << 7))
  251. /* 32-bit store. */
  252. #define OPC_ST32 (OPC_STORE | STORE_ZZ(ZZ_4_byte))
  253. /* "push reg" is merely a "st.aw reg,[sp,-4]". */
  254. #define OPC_PUSH \
  255. (OPC_ST32 | STORE_AA(AA_pre) | STORE_S9(-4) | OP_B(ARC_R_SP))
  256. /*
  257. * The 4-byte encoding of "add a,b,c":
  258. *
  259. * 0010_0bbb 0i00_0000 fBBB_cccc ccaa_aaaa
  260. *
  261. * f: indicates if flags (carry, etc.) should be updated
  262. * i: If set, c is considered a 6-bit immediate, else a reg.
  263. *
  264. * a: aaaaaa result
  265. * b: BBBbbb the 1st input operand
  266. * c: cccccc the 2nd input operand
  267. */
  268. #define OPC_ADD 0x20000000
  269. /* Addition with updating the pertinent flags in "status32" register. */
  270. #define OPC_ADDF (OPC_ADD | FLAG(1))
  271. #define ADDI BIT(22)
  272. #define ADDI_U6(x) OP_C(x)
  273. #define OPC_ADDI (OPC_ADD | ADDI)
  274. #define OPC_ADDIF (OPC_ADDI | FLAG(1))
  275. #define OPC_ADD_I (OPC_ADD | OP_IMM)
  276. /*
  277. * The 4-byte encoding of "adc a,b,c" (addition with carry):
  278. *
  279. * 0010_0bbb 0i00_0001 0BBB_cccc ccaa_aaaa
  280. *
  281. * i: if set, c is considered a 6-bit immediate, else a reg.
  282. *
  283. * a: aaaaaa result
  284. * b: BBBbbb the 1st input operand
  285. * c: cccccc the 2nd input operand
  286. */
  287. #define OPC_ADC 0x20010000
  288. #define ADCI BIT(22)
  289. #define ADCI_U6(x) OP_C(x)
  290. #define OPC_ADCI (OPC_ADC | ADCI)
  291. /*
  292. * The 4-byte encoding of "sub a,b,c":
  293. *
  294. * 0010_0bbb 0i00_0010 fBBB_cccc ccaa_aaaa
  295. *
  296. * f: indicates if flags (carry, etc.) should be updated
  297. * i: if set, c is considered a 6-bit immediate, else a reg.
  298. *
  299. * a: aaaaaa result
  300. * b: BBBbbb the 1st input operand
  301. * c: cccccc the 2nd input operand
  302. */
  303. #define OPC_SUB 0x20020000
  304. /* Subtraction with updating the pertinent flags in "status32" register. */
  305. #define OPC_SUBF (OPC_SUB | FLAG(1))
  306. #define SUBI BIT(22)
  307. #define SUBI_U6(x) OP_C(x)
  308. #define OPC_SUBI (OPC_SUB | SUBI)
  309. #define OPC_SUB_I (OPC_SUB | OP_IMM)
  310. /*
  311. * The 4-byte encoding of "sbc a,b,c" (subtraction with carry):
  312. *
  313. * 0010_0bbb 0000_0011 fBBB_cccc ccaa_aaaa
  314. *
  315. * f: indicates if flags (carry, etc.) should be updated
  316. *
  317. * a: aaaaaa result
  318. * b: BBBbbb the 1st input operand
  319. * c: cccccc the 2nd input operand
  320. */
  321. #define OPC_SBC 0x20030000
  322. /*
  323. * The 4-byte encoding of "cmp[.qq] b,c":
  324. *
  325. * 0010_0bbb 1100_1100 1BBB_cccc cc0q_qqqq
  326. *
  327. * qq: qqqqq condition code
  328. *
  329. * b: BBBbbb the 1st operand
  330. * c: cccccc the 2nd operand
  331. */
  332. #define OPC_CMP 0x20cc8000
  333. /*
  334. * The 4-byte encoding of "neg a,b":
  335. *
  336. * 0010_0bbb 0100_1110 0BBB_0000 00aa_aaaa
  337. *
  338. * a: aaaaaa result
  339. * b: BBBbbb input
  340. */
  341. #define OPC_NEG 0x204e0000
  342. /*
  343. * The 4-byte encoding of "mpy a,b,c".
  344. * mpy is the signed 32-bit multiplication with the lower 32-bit
  345. * of the product as the result.
  346. *
  347. * 0010_0bbb 0001_1010 0BBB_cccc ccaa_aaaa
  348. *
  349. * a: aaaaaa result
  350. * b: BBBbbb the 1st input operand
  351. * c: cccccc the 2nd input operand
  352. */
  353. #define OPC_MPY 0x201a0000
  354. #define OPC_MPYI (OPC_MPY | OP_IMM)
  355. /*
  356. * The 4-byte encoding of "mpydu a,b,c".
  357. * mpydu is the unsigned 32-bit multiplication with the lower 32-bit of
  358. * the product in register "a" and the higher 32-bit in register "a+1".
  359. *
  360. * 0010_1bbb 0001_1001 0BBB_cccc ccaa_aaaa
  361. *
  362. * a: aaaaaa 64-bit result in registers (R_a+1,R_a)
  363. * b: BBBbbb the 1st input operand
  364. * c: cccccc the 2nd input operand
  365. */
  366. #define OPC_MPYDU 0x28190000
  367. #define OPC_MPYDUI (OPC_MPYDU | OP_IMM)
  368. /*
  369. * The 4-byte encoding of "divu a,b,c" (unsigned division):
  370. *
  371. * 0010_1bbb 0000_0101 0BBB_cccc ccaa_aaaa
  372. *
  373. * a: aaaaaa result (quotient)
  374. * b: BBBbbb the 1st input operand
  375. * c: cccccc the 2nd input operand (divisor)
  376. */
  377. #define OPC_DIVU 0x28050000
  378. #define OPC_DIVUI (OPC_DIVU | OP_IMM)
  379. /*
  380. * The 4-byte encoding of "div a,b,c" (signed division):
  381. *
  382. * 0010_1bbb 0000_0100 0BBB_cccc ccaa_aaaa
  383. *
  384. * a: aaaaaa result (quotient)
  385. * b: BBBbbb the 1st input operand
  386. * c: cccccc the 2nd input operand (divisor)
  387. */
  388. #define OPC_DIVS 0x28040000
  389. #define OPC_DIVSI (OPC_DIVS | OP_IMM)
  390. /*
  391. * The 4-byte encoding of "remu a,b,c" (unsigned remainder):
  392. *
  393. * 0010_1bbb 0000_1001 0BBB_cccc ccaa_aaaa
  394. *
  395. * a: aaaaaa result (remainder)
  396. * b: BBBbbb the 1st input operand
  397. * c: cccccc the 2nd input operand (divisor)
  398. */
  399. #define OPC_REMU 0x28090000
  400. #define OPC_REMUI (OPC_REMU | OP_IMM)
  401. /*
  402. * The 4-byte encoding of "rem a,b,c" (signed remainder):
  403. *
  404. * 0010_1bbb 0000_1000 0BBB_cccc ccaa_aaaa
  405. *
  406. * a: aaaaaa result (remainder)
  407. * b: BBBbbb the 1st input operand
  408. * c: cccccc the 2nd input operand (divisor)
  409. */
  410. #define OPC_REMS 0x28080000
  411. #define OPC_REMSI (OPC_REMS | OP_IMM)
  412. /*
  413. * The 4-byte encoding of "and a,b,c":
  414. *
  415. * 0010_0bbb 0000_0100 fBBB_cccc ccaa_aaaa
  416. *
  417. * f: indicates if zero and negative flags should be updated
  418. *
  419. * a: aaaaaa result
  420. * b: BBBbbb the 1st input operand
  421. * c: cccccc the 2nd input operand
  422. */
  423. #define OPC_AND 0x20040000
  424. #define OPC_ANDI (OPC_AND | OP_IMM)
  425. /*
  426. * The 4-byte encoding of "tst[.qq] b,c".
  427. * Checks if the two input operands have any bit set at the same
  428. * position.
  429. *
  430. * 0010_0bbb 1100_1011 1BBB_cccc cc0q_qqqq
  431. *
  432. * qq: qqqqq condition code
  433. *
  434. * b: BBBbbb the 1st input operand
  435. * c: cccccc the 2nd input operand
  436. */
  437. #define OPC_TST 0x20cb8000
  438. /*
  439. * The 4-byte encoding of "or a,b,c":
  440. *
  441. * 0010_0bbb 0000_0101 0BBB_cccc ccaa_aaaa
  442. *
  443. * a: aaaaaa result
  444. * b: BBBbbb the 1st input operand
  445. * c: cccccc the 2nd input operand
  446. */
  447. #define OPC_OR 0x20050000
  448. #define OPC_ORI (OPC_OR | OP_IMM)
  449. /*
  450. * The 4-byte encoding of "xor a,b,c":
  451. *
  452. * 0010_0bbb 0000_0111 0BBB_cccc ccaa_aaaa
  453. *
  454. * a: aaaaaa result
  455. * b: BBBbbb the 1st input operand
  456. * c: cccccc the 2nd input operand
  457. */
  458. #define OPC_XOR 0x20070000
  459. #define OPC_XORI (OPC_XOR | OP_IMM)
  460. /*
  461. * The 4-byte encoding of "not b,c":
  462. *
  463. * 0010_0bbb 0010_1111 0BBB_cccc cc00_1010
  464. *
  465. * b: BBBbbb result
  466. * c: cccccc input
  467. */
  468. #define OPC_NOT 0x202f000a
  469. /*
  470. * The 4-byte encoding of "btst b,u6":
  471. *
  472. * 0010_0bbb 0101_0001 1BBB_uuuu uu00_0000
  473. *
  474. * b: BBBbbb input number to check
  475. * u6: uuuuuu 6-bit unsigned number specifying bit position to check
  476. */
  477. #define OPC_BTSTU6 0x20518000
  478. #define BTST_U6(x) (OP_C((x) & 63))
  479. /*
  480. * The 4-byte encoding of "asl[.qq] b,b,c" (arithmetic shift left):
  481. *
  482. * 0010_1bbb 0i00_0000 0BBB_cccc ccaa_aaaa
  483. *
  484. * i: if set, c is considered a 5-bit immediate, else a reg.
  485. *
  486. * b: BBBbbb result and the first operand (number to be shifted)
  487. * c: cccccc amount to be shifted
  488. */
  489. #define OPC_ASL 0x28000000
  490. #define ASL_I BIT(22)
  491. #define ASLI_U6(x) OP_C((x) & 31)
  492. #define OPC_ASLI (OPC_ASL | ASL_I)
  493. /*
  494. * The 4-byte encoding of "asr a,b,c" (arithmetic shift right):
  495. *
  496. * 0010_1bbb 0i00_0010 0BBB_cccc ccaa_aaaa
  497. *
  498. * i: if set, c is considered a 6-bit immediate, else a reg.
  499. *
  500. * a: aaaaaa result
  501. * b: BBBbbb first input: number to be shifted
  502. * c: cccccc second input: amount to be shifted
  503. */
  504. #define OPC_ASR 0x28020000
  505. #define ASR_I ASL_I
  506. #define ASRI_U6(x) ASLI_U6(x)
  507. #define OPC_ASRI (OPC_ASR | ASR_I)
  508. /*
  509. * The 4-byte encoding of "lsr a,b,c" (logical shift right):
  510. *
  511. * 0010_1bbb 0i00_0001 0BBB_cccc ccaa_aaaa
  512. *
  513. * i: if set, c is considered a 6-bit immediate, else a reg.
  514. *
  515. * a: aaaaaa result
  516. * b: BBBbbb first input: number to be shifted
  517. * c: cccccc second input: amount to be shifted
  518. */
  519. #define OPC_LSR 0x28010000
  520. #define LSR_I ASL_I
  521. #define LSRI_U6(x) ASLI_U6(x)
  522. #define OPC_LSRI (OPC_LSR | LSR_I)
  523. /*
  524. * The 4-byte encoding of "swape b,c":
  525. *
  526. * 0010_1bbb 0010_1111 0bbb_cccc cc00_1001
  527. *
  528. * b: BBBbbb destination register
  529. * c: cccccc source register
  530. */
  531. #define OPC_SWAPE 0x282f0009
  532. /*
  533. * Encoding for jump to an address in register:
  534. * j reg_c
  535. *
  536. * 0010_0000 1110_0000 0000_cccc cc00_0000
  537. *
  538. * c: cccccc register holding the destination address
  539. */
  540. #define OPC_JMP 0x20e00000
  541. /* Jump to "branch-and-link" register, which effectively is a "return". */
  542. #define OPC_J_BLINK (OPC_JMP | OP_C(ARC_R_BLINK))
  543. /*
  544. * Encoding for jump-and-link to an address in register:
  545. * jl reg_c
  546. *
  547. * 0010_0000 0010_0010 0000_cccc cc00_0000
  548. *
  549. * c: cccccc register holding the destination address
  550. */
  551. #define OPC_JL 0x20220000
  552. /*
  553. * Encoding for (conditional) branch to an offset from the current location
  554. * that is word aligned: (PC & 0xffff_fffc) + s21
  555. * B[qq] s21
  556. *
  557. * 0000_0sss ssss_sss0 SSSS_SSSS SS0q_qqqq
  558. *
  559. * qq: qqqqq condition code
  560. * s21: SSSS SSSS_SSss ssss_ssss The displacement (21-bit signed)
  561. *
  562. * The displacement is supposed to be 16-bit (2-byte) aligned. Therefore,
  563. * it should be a multiple of 2. Hence, there is an implied '0' bit at its
  564. * LSB: S_SSSS SSSS_Ssss ssss_sss0
  565. */
  566. #define OPC_BCC 0x00000000
  567. #define BCC_S21(d) ((((d) & 0x7fe) << 16) | (((d) & 0x1ff800) >> 5))
  568. /*
  569. * Encoding for unconditional branch to an offset from the current location
  570. * that is word aligned: (PC & 0xffff_fffc) + s25
  571. * B s25
  572. *
  573. * 0000_0sss ssss_sss1 SSSS_SSSS SS00_TTTT
  574. *
  575. * s25: TTTT SSSS SSSS_SSss ssss_ssss The displacement (25-bit signed)
  576. *
  577. * The displacement is supposed to be 16-bit (2-byte) aligned. Therefore,
  578. * it should be a multiple of 2. Hence, there is an implied '0' bit at its
  579. * LSB: T TTTS_SSSS SSSS_Ssss ssss_sss0
  580. */
  581. #define OPC_B 0x00010000
  582. #define B_S25(d) ((((d) & 0x1e00000) >> 21) | BCC_S21(d))
  583. static inline void emit_2_bytes(u8 *buf, u16 bytes)
  584. {
  585. *((u16 *)buf) = bytes;
  586. }
  587. static inline void emit_4_bytes(u8 *buf, u32 bytes)
  588. {
  589. emit_2_bytes(buf, bytes >> 16);
  590. emit_2_bytes(buf + 2, bytes & 0xffff);
  591. }
  592. static inline u8 bpf_to_arc_size(u8 size)
  593. {
  594. switch (size) {
  595. case BPF_B:
  596. return ZZ_1_byte;
  597. case BPF_H:
  598. return ZZ_2_byte;
  599. case BPF_W:
  600. return ZZ_4_byte;
  601. case BPF_DW:
  602. return ZZ_8_byte;
  603. default:
  604. return ZZ_4_byte;
  605. }
  606. }
  607. /************** Encoders (Deal with ARC regs) ************/
  608. /* Move an immediate to register with a 4-byte instruction. */
  609. static u8 arc_movi_r(u8 *buf, u8 reg, s16 imm)
  610. {
  611. const u32 insn = OPC_MOVI | OP_B(reg) | MOVI_S12(imm);
  612. if (buf)
  613. emit_4_bytes(buf, insn);
  614. return INSN_len_normal;
  615. }
  616. /* rd <- rs */
  617. static u8 arc_mov_r(u8 *buf, u8 rd, u8 rs)
  618. {
  619. const u32 insn = OPC_MOV | OP_B(rd) | OP_C(rs);
  620. if (buf)
  621. emit_4_bytes(buf, insn);
  622. return INSN_len_normal;
  623. }
  624. /* The emitted code may have different sizes based on "imm". */
  625. static u8 arc_mov_i(u8 *buf, u8 rd, s32 imm)
  626. {
  627. const u32 insn = OPC_MOV | OP_B(rd) | OP_IMM;
  628. if (IN_S12_RANGE(imm))
  629. return arc_movi_r(buf, rd, imm);
  630. if (buf) {
  631. emit_4_bytes(buf, insn);
  632. emit_4_bytes(buf + INSN_len_normal, imm);
  633. }
  634. return INSN_len_normal + INSN_len_imm;
  635. }
  636. /* The emitted code will always have the same size (8). */
  637. static u8 arc_mov_i_fixed(u8 *buf, u8 rd, s32 imm)
  638. {
  639. const u32 insn = OPC_MOV | OP_B(rd) | OP_IMM;
  640. if (buf) {
  641. emit_4_bytes(buf, insn);
  642. emit_4_bytes(buf + INSN_len_normal, imm);
  643. }
  644. return INSN_len_normal + INSN_len_imm;
  645. }
  646. /* Conditional move. */
  647. static u8 arc_mov_cc_r(u8 *buf, u8 cc, u8 rd, u8 rs)
  648. {
  649. const u32 insn = OPC_MOV_CC | OP_B(rd) | OP_C(rs) | COND(cc);
  650. if (buf)
  651. emit_4_bytes(buf, insn);
  652. return INSN_len_normal;
  653. }
  654. /* Conditional move of a small immediate to rd. */
  655. static u8 arc_movu_cc_r(u8 *buf, u8 cc, u8 rd, u8 imm)
  656. {
  657. const u32 insn = OPC_MOVU_CC | OP_B(rd) | OP_C(imm) | COND(cc);
  658. if (buf)
  659. emit_4_bytes(buf, insn);
  660. return INSN_len_normal;
  661. }
  662. /* Sign extension from a byte. */
  663. static u8 arc_sexb_r(u8 *buf, u8 rd, u8 rs)
  664. {
  665. const u32 insn = OPC_SEXB | OP_B(rd) | OP_C(rs);
  666. if (buf)
  667. emit_4_bytes(buf, insn);
  668. return INSN_len_normal;
  669. }
  670. /* Sign extension from two bytes. */
  671. static u8 arc_sexh_r(u8 *buf, u8 rd, u8 rs)
  672. {
  673. const u32 insn = OPC_SEXH | OP_B(rd) | OP_C(rs);
  674. if (buf)
  675. emit_4_bytes(buf, insn);
  676. return INSN_len_normal;
  677. }
  678. /* st reg, [reg_mem, off] */
  679. static u8 arc_st_r(u8 *buf, u8 reg, u8 reg_mem, s16 off, u8 zz)
  680. {
  681. const u32 insn = OPC_STORE | STORE_ZZ(zz) | OP_C(reg) |
  682. OP_B(reg_mem) | STORE_S9(off);
  683. if (buf)
  684. emit_4_bytes(buf, insn);
  685. return INSN_len_normal;
  686. }
  687. /* st.aw reg, [sp, -4] */
  688. static u8 arc_push_r(u8 *buf, u8 reg)
  689. {
  690. const u32 insn = OPC_PUSH | OP_C(reg);
  691. if (buf)
  692. emit_4_bytes(buf, insn);
  693. return INSN_len_normal;
  694. }
  695. /* ld reg, [reg_mem, off] (unsigned) */
  696. static u8 arc_ld_r(u8 *buf, u8 reg, u8 reg_mem, s16 off, u8 zz)
  697. {
  698. const u32 insn = OPC_LDU | LOAD_ZZ(zz) | LOAD_C(reg) |
  699. OP_B(reg_mem) | LOAD_S9(off);
  700. if (buf)
  701. emit_4_bytes(buf, insn);
  702. return INSN_len_normal;
  703. }
  704. /* ld.x reg, [reg_mem, off] (sign extend) */
  705. static u8 arc_ldx_r(u8 *buf, u8 reg, u8 reg_mem, s16 off, u8 zz)
  706. {
  707. const u32 insn = OPC_LDS | LOAD_ZZ(zz) | LOAD_C(reg) |
  708. OP_B(reg_mem) | LOAD_S9(off);
  709. if (buf)
  710. emit_4_bytes(buf, insn);
  711. return INSN_len_normal;
  712. }
  713. /* ld.ab reg,[sp,4] */
  714. static u8 arc_pop_r(u8 *buf, u8 reg)
  715. {
  716. const u32 insn = OPC_POP | LOAD_C(reg);
  717. if (buf)
  718. emit_4_bytes(buf, insn);
  719. return INSN_len_normal;
  720. }
  721. /* add Ra,Ra,Rc */
  722. static u8 arc_add_r(u8 *buf, u8 ra, u8 rc)
  723. {
  724. const u32 insn = OPC_ADD | OP_A(ra) | OP_B(ra) | OP_C(rc);
  725. if (buf)
  726. emit_4_bytes(buf, insn);
  727. return INSN_len_normal;
  728. }
  729. /* add.f Ra,Ra,Rc */
  730. static u8 arc_addf_r(u8 *buf, u8 ra, u8 rc)
  731. {
  732. const u32 insn = OPC_ADDF | OP_A(ra) | OP_B(ra) | OP_C(rc);
  733. if (buf)
  734. emit_4_bytes(buf, insn);
  735. return INSN_len_normal;
  736. }
  737. /* add.f Ra,Ra,u6 */
  738. static u8 arc_addif_r(u8 *buf, u8 ra, u8 u6)
  739. {
  740. const u32 insn = OPC_ADDIF | OP_A(ra) | OP_B(ra) | ADDI_U6(u6);
  741. if (buf)
  742. emit_4_bytes(buf, insn);
  743. return INSN_len_normal;
  744. }
  745. /* add Ra,Ra,u6 */
  746. static u8 arc_addi_r(u8 *buf, u8 ra, u8 u6)
  747. {
  748. const u32 insn = OPC_ADDI | OP_A(ra) | OP_B(ra) | ADDI_U6(u6);
  749. if (buf)
  750. emit_4_bytes(buf, insn);
  751. return INSN_len_normal;
  752. }
  753. /* add Ra,Rb,imm */
  754. static u8 arc_add_i(u8 *buf, u8 ra, u8 rb, s32 imm)
  755. {
  756. const u32 insn = OPC_ADD_I | OP_A(ra) | OP_B(rb);
  757. if (buf) {
  758. emit_4_bytes(buf, insn);
  759. emit_4_bytes(buf + INSN_len_normal, imm);
  760. }
  761. return INSN_len_normal + INSN_len_imm;
  762. }
  763. /* adc Ra,Ra,Rc */
  764. static u8 arc_adc_r(u8 *buf, u8 ra, u8 rc)
  765. {
  766. const u32 insn = OPC_ADC | OP_A(ra) | OP_B(ra) | OP_C(rc);
  767. if (buf)
  768. emit_4_bytes(buf, insn);
  769. return INSN_len_normal;
  770. }
  771. /* adc Ra,Ra,u6 */
  772. static u8 arc_adci_r(u8 *buf, u8 ra, u8 u6)
  773. {
  774. const u32 insn = OPC_ADCI | OP_A(ra) | OP_B(ra) | ADCI_U6(u6);
  775. if (buf)
  776. emit_4_bytes(buf, insn);
  777. return INSN_len_normal;
  778. }
  779. /* sub Ra,Ra,Rc */
  780. static u8 arc_sub_r(u8 *buf, u8 ra, u8 rc)
  781. {
  782. const u32 insn = OPC_SUB | OP_A(ra) | OP_B(ra) | OP_C(rc);
  783. if (buf)
  784. emit_4_bytes(buf, insn);
  785. return INSN_len_normal;
  786. }
  787. /* sub.f Ra,Ra,Rc */
  788. static u8 arc_subf_r(u8 *buf, u8 ra, u8 rc)
  789. {
  790. const u32 insn = OPC_SUBF | OP_A(ra) | OP_B(ra) | OP_C(rc);
  791. if (buf)
  792. emit_4_bytes(buf, insn);
  793. return INSN_len_normal;
  794. }
  795. /* sub Ra,Ra,u6 */
  796. static u8 arc_subi_r(u8 *buf, u8 ra, u8 u6)
  797. {
  798. const u32 insn = OPC_SUBI | OP_A(ra) | OP_B(ra) | SUBI_U6(u6);
  799. if (buf)
  800. emit_4_bytes(buf, insn);
  801. return INSN_len_normal;
  802. }
  803. /* sub Ra,Ra,imm */
  804. static u8 arc_sub_i(u8 *buf, u8 ra, s32 imm)
  805. {
  806. const u32 insn = OPC_SUB_I | OP_A(ra) | OP_B(ra);
  807. if (buf) {
  808. emit_4_bytes(buf, insn);
  809. emit_4_bytes(buf + INSN_len_normal, imm);
  810. }
  811. return INSN_len_normal + INSN_len_imm;
  812. }
  813. /* sbc Ra,Ra,Rc */
  814. static u8 arc_sbc_r(u8 *buf, u8 ra, u8 rc)
  815. {
  816. const u32 insn = OPC_SBC | OP_A(ra) | OP_B(ra) | OP_C(rc);
  817. if (buf)
  818. emit_4_bytes(buf, insn);
  819. return INSN_len_normal;
  820. }
  821. /* cmp Rb,Rc */
  822. static u8 arc_cmp_r(u8 *buf, u8 rb, u8 rc)
  823. {
  824. const u32 insn = OPC_CMP | OP_B(rb) | OP_C(rc);
  825. if (buf)
  826. emit_4_bytes(buf, insn);
  827. return INSN_len_normal;
  828. }
  829. /*
  830. * cmp.z Rb,Rc
  831. *
  832. * This "cmp.z" variant of compare instruction is used on lower
  833. * 32-bits of register pairs after "cmp"ing their upper parts. If the
  834. * upper parts are equal (z), then this one will proceed to check the
  835. * rest.
  836. */
  837. static u8 arc_cmpz_r(u8 *buf, u8 rb, u8 rc)
  838. {
  839. const u32 insn = OPC_CMP | OP_B(rb) | OP_C(rc) | CC_equal;
  840. if (buf)
  841. emit_4_bytes(buf, insn);
  842. return INSN_len_normal;
  843. }
  844. /* neg Ra,Rb */
  845. static u8 arc_neg_r(u8 *buf, u8 ra, u8 rb)
  846. {
  847. const u32 insn = OPC_NEG | OP_A(ra) | OP_B(rb);
  848. if (buf)
  849. emit_4_bytes(buf, insn);
  850. return INSN_len_normal;
  851. }
  852. /* mpy Ra,Rb,Rc */
  853. static u8 arc_mpy_r(u8 *buf, u8 ra, u8 rb, u8 rc)
  854. {
  855. const u32 insn = OPC_MPY | OP_A(ra) | OP_B(rb) | OP_C(rc);
  856. if (buf)
  857. emit_4_bytes(buf, insn);
  858. return INSN_len_normal;
  859. }
  860. /* mpy Ra,Rb,imm */
  861. static u8 arc_mpy_i(u8 *buf, u8 ra, u8 rb, s32 imm)
  862. {
  863. const u32 insn = OPC_MPYI | OP_A(ra) | OP_B(rb);
  864. if (buf) {
  865. emit_4_bytes(buf, insn);
  866. emit_4_bytes(buf + INSN_len_normal, imm);
  867. }
  868. return INSN_len_normal + INSN_len_imm;
  869. }
  870. /* mpydu Ra,Ra,Rc */
  871. static u8 arc_mpydu_r(u8 *buf, u8 ra, u8 rc)
  872. {
  873. const u32 insn = OPC_MPYDU | OP_A(ra) | OP_B(ra) | OP_C(rc);
  874. if (buf)
  875. emit_4_bytes(buf, insn);
  876. return INSN_len_normal;
  877. }
  878. /* mpydu Ra,Ra,imm */
  879. static u8 arc_mpydu_i(u8 *buf, u8 ra, s32 imm)
  880. {
  881. const u32 insn = OPC_MPYDUI | OP_A(ra) | OP_B(ra);
  882. if (buf) {
  883. emit_4_bytes(buf, insn);
  884. emit_4_bytes(buf + INSN_len_normal, imm);
  885. }
  886. return INSN_len_normal + INSN_len_imm;
  887. }
  888. /* divu Rd,Rd,Rs */
  889. static u8 arc_divu_r(u8 *buf, u8 rd, u8 rs)
  890. {
  891. const u32 insn = OPC_DIVU | OP_A(rd) | OP_B(rd) | OP_C(rs);
  892. if (buf)
  893. emit_4_bytes(buf, insn);
  894. return INSN_len_normal;
  895. }
  896. /* divu Rd,Rd,imm */
  897. static u8 arc_divu_i(u8 *buf, u8 rd, s32 imm)
  898. {
  899. const u32 insn = OPC_DIVUI | OP_A(rd) | OP_B(rd);
  900. if (buf) {
  901. emit_4_bytes(buf, insn);
  902. emit_4_bytes(buf + INSN_len_normal, imm);
  903. }
  904. return INSN_len_normal + INSN_len_imm;
  905. }
  906. /* div Rd,Rd,Rs */
  907. static u8 arc_divs_r(u8 *buf, u8 rd, u8 rs)
  908. {
  909. const u32 insn = OPC_DIVS | OP_A(rd) | OP_B(rd) | OP_C(rs);
  910. if (buf)
  911. emit_4_bytes(buf, insn);
  912. return INSN_len_normal;
  913. }
  914. /* div Rd,Rd,imm */
  915. static u8 arc_divs_i(u8 *buf, u8 rd, s32 imm)
  916. {
  917. const u32 insn = OPC_DIVSI | OP_A(rd) | OP_B(rd);
  918. if (buf) {
  919. emit_4_bytes(buf, insn);
  920. emit_4_bytes(buf + INSN_len_normal, imm);
  921. }
  922. return INSN_len_normal + INSN_len_imm;
  923. }
  924. /* remu Rd,Rd,Rs */
  925. static u8 arc_remu_r(u8 *buf, u8 rd, u8 rs)
  926. {
  927. const u32 insn = OPC_REMU | OP_A(rd) | OP_B(rd) | OP_C(rs);
  928. if (buf)
  929. emit_4_bytes(buf, insn);
  930. return INSN_len_normal;
  931. }
  932. /* remu Rd,Rd,imm */
  933. static u8 arc_remu_i(u8 *buf, u8 rd, s32 imm)
  934. {
  935. const u32 insn = OPC_REMUI | OP_A(rd) | OP_B(rd);
  936. if (buf) {
  937. emit_4_bytes(buf, insn);
  938. emit_4_bytes(buf + INSN_len_normal, imm);
  939. }
  940. return INSN_len_normal + INSN_len_imm;
  941. }
  942. /* rem Rd,Rd,Rs */
  943. static u8 arc_rems_r(u8 *buf, u8 rd, u8 rs)
  944. {
  945. const u32 insn = OPC_REMS | OP_A(rd) | OP_B(rd) | OP_C(rs);
  946. if (buf)
  947. emit_4_bytes(buf, insn);
  948. return INSN_len_normal;
  949. }
  950. /* rem Rd,Rd,imm */
  951. static u8 arc_rems_i(u8 *buf, u8 rd, s32 imm)
  952. {
  953. const u32 insn = OPC_REMSI | OP_A(rd) | OP_B(rd);
  954. if (buf) {
  955. emit_4_bytes(buf, insn);
  956. emit_4_bytes(buf + INSN_len_normal, imm);
  957. }
  958. return INSN_len_normal + INSN_len_imm;
  959. }
  960. /* and Rd,Rd,Rs */
  961. static u8 arc_and_r(u8 *buf, u8 rd, u8 rs)
  962. {
  963. const u32 insn = OPC_AND | OP_A(rd) | OP_B(rd) | OP_C(rs);
  964. if (buf)
  965. emit_4_bytes(buf, insn);
  966. return INSN_len_normal;
  967. }
  968. /* and Rd,Rd,limm */
  969. static u8 arc_and_i(u8 *buf, u8 rd, s32 imm)
  970. {
  971. const u32 insn = OPC_ANDI | OP_A(rd) | OP_B(rd);
  972. if (buf) {
  973. emit_4_bytes(buf, insn);
  974. emit_4_bytes(buf + INSN_len_normal, imm);
  975. }
  976. return INSN_len_normal + INSN_len_imm;
  977. }
  978. /* tst Rd,Rs */
  979. static u8 arc_tst_r(u8 *buf, u8 rd, u8 rs)
  980. {
  981. const u32 insn = OPC_TST | OP_B(rd) | OP_C(rs);
  982. if (buf)
  983. emit_4_bytes(buf, insn);
  984. return INSN_len_normal;
  985. }
  986. /*
  987. * This particular version, "tst.z ...", is meant to be used after a
  988. * "tst" on the low 32-bit of register pairs. If that "tst" is not
  989. * zero, then we don't need to test the upper 32-bits lest it sets
  990. * the zero flag.
  991. */
  992. static u8 arc_tstz_r(u8 *buf, u8 rd, u8 rs)
  993. {
  994. const u32 insn = OPC_TST | OP_B(rd) | OP_C(rs) | CC_equal;
  995. if (buf)
  996. emit_4_bytes(buf, insn);
  997. return INSN_len_normal;
  998. }
  999. static u8 arc_or_r(u8 *buf, u8 rd, u8 rs1, u8 rs2)
  1000. {
  1001. const u32 insn = OPC_OR | OP_A(rd) | OP_B(rs1) | OP_C(rs2);
  1002. if (buf)
  1003. emit_4_bytes(buf, insn);
  1004. return INSN_len_normal;
  1005. }
  1006. static u8 arc_or_i(u8 *buf, u8 rd, s32 imm)
  1007. {
  1008. const u32 insn = OPC_ORI | OP_A(rd) | OP_B(rd);
  1009. if (buf) {
  1010. emit_4_bytes(buf, insn);
  1011. emit_4_bytes(buf + INSN_len_normal, imm);
  1012. }
  1013. return INSN_len_normal + INSN_len_imm;
  1014. }
  1015. static u8 arc_xor_r(u8 *buf, u8 rd, u8 rs)
  1016. {
  1017. const u32 insn = OPC_XOR | OP_A(rd) | OP_B(rd) | OP_C(rs);
  1018. if (buf)
  1019. emit_4_bytes(buf, insn);
  1020. return INSN_len_normal;
  1021. }
  1022. static u8 arc_xor_i(u8 *buf, u8 rd, s32 imm)
  1023. {
  1024. const u32 insn = OPC_XORI | OP_A(rd) | OP_B(rd);
  1025. if (buf) {
  1026. emit_4_bytes(buf, insn);
  1027. emit_4_bytes(buf + INSN_len_normal, imm);
  1028. }
  1029. return INSN_len_normal + INSN_len_imm;
  1030. }
  1031. static u8 arc_not_r(u8 *buf, u8 rd, u8 rs)
  1032. {
  1033. const u32 insn = OPC_NOT | OP_B(rd) | OP_C(rs);
  1034. if (buf)
  1035. emit_4_bytes(buf, insn);
  1036. return INSN_len_normal;
  1037. }
  1038. static u8 arc_btst_i(u8 *buf, u8 rs, u8 imm)
  1039. {
  1040. const u32 insn = OPC_BTSTU6 | OP_B(rs) | BTST_U6(imm);
  1041. if (buf)
  1042. emit_4_bytes(buf, insn);
  1043. return INSN_len_normal;
  1044. }
  1045. static u8 arc_asl_r(u8 *buf, u8 rd, u8 rs1, u8 rs2)
  1046. {
  1047. const u32 insn = OPC_ASL | OP_A(rd) | OP_B(rs1) | OP_C(rs2);
  1048. if (buf)
  1049. emit_4_bytes(buf, insn);
  1050. return INSN_len_normal;
  1051. }
  1052. static u8 arc_asli_r(u8 *buf, u8 rd, u8 rs, u8 imm)
  1053. {
  1054. const u32 insn = OPC_ASLI | OP_A(rd) | OP_B(rs) | ASLI_U6(imm);
  1055. if (buf)
  1056. emit_4_bytes(buf, insn);
  1057. return INSN_len_normal;
  1058. }
  1059. static u8 arc_asr_r(u8 *buf, u8 rd, u8 rs1, u8 rs2)
  1060. {
  1061. const u32 insn = OPC_ASR | OP_A(rd) | OP_B(rs1) | OP_C(rs2);
  1062. if (buf)
  1063. emit_4_bytes(buf, insn);
  1064. return INSN_len_normal;
  1065. }
  1066. static u8 arc_asri_r(u8 *buf, u8 rd, u8 rs, u8 imm)
  1067. {
  1068. const u32 insn = OPC_ASRI | OP_A(rd) | OP_B(rs) | ASRI_U6(imm);
  1069. if (buf)
  1070. emit_4_bytes(buf, insn);
  1071. return INSN_len_normal;
  1072. }
  1073. static u8 arc_lsr_r(u8 *buf, u8 rd, u8 rs1, u8 rs2)
  1074. {
  1075. const u32 insn = OPC_LSR | OP_A(rd) | OP_B(rs1) | OP_C(rs2);
  1076. if (buf)
  1077. emit_4_bytes(buf, insn);
  1078. return INSN_len_normal;
  1079. }
  1080. static u8 arc_lsri_r(u8 *buf, u8 rd, u8 rs, u8 imm)
  1081. {
  1082. const u32 insn = OPC_LSRI | OP_A(rd) | OP_B(rs) | LSRI_U6(imm);
  1083. if (buf)
  1084. emit_4_bytes(buf, insn);
  1085. return INSN_len_normal;
  1086. }
  1087. static u8 arc_swape_r(u8 *buf, u8 r)
  1088. {
  1089. const u32 insn = OPC_SWAPE | OP_B(r) | OP_C(r);
  1090. if (buf)
  1091. emit_4_bytes(buf, insn);
  1092. return INSN_len_normal;
  1093. }
  1094. static u8 arc_jmp_return(u8 *buf)
  1095. {
  1096. if (buf)
  1097. emit_4_bytes(buf, OPC_J_BLINK);
  1098. return INSN_len_normal;
  1099. }
  1100. static u8 arc_jl(u8 *buf, u8 reg)
  1101. {
  1102. const u32 insn = OPC_JL | OP_C(reg);
  1103. if (buf)
  1104. emit_4_bytes(buf, insn);
  1105. return INSN_len_normal;
  1106. }
  1107. /*
  1108. * Conditional jump to an address that is max 21 bits away (signed).
  1109. *
  1110. * b<cc> s21
  1111. */
  1112. static u8 arc_bcc(u8 *buf, u8 cc, int offset)
  1113. {
  1114. const u32 insn = OPC_BCC | BCC_S21(offset) | COND(cc);
  1115. if (buf)
  1116. emit_4_bytes(buf, insn);
  1117. return INSN_len_normal;
  1118. }
  1119. /*
  1120. * Unconditional jump to an address that is max 25 bits away (signed).
  1121. *
  1122. * b s25
  1123. */
  1124. static u8 arc_b(u8 *buf, s32 offset)
  1125. {
  1126. const u32 insn = OPC_B | B_S25(offset);
  1127. if (buf)
  1128. emit_4_bytes(buf, insn);
  1129. return INSN_len_normal;
  1130. }
  1131. /************* Packers (Deal with BPF_REGs) **************/
  1132. u8 zext(u8 *buf, u8 rd)
  1133. {
  1134. if (rd != BPF_REG_FP)
  1135. return arc_movi_r(buf, REG_HI(rd), 0);
  1136. else
  1137. return 0;
  1138. }
  1139. u8 mov_r32(u8 *buf, u8 rd, u8 rs, u8 sign_ext)
  1140. {
  1141. u8 len = 0;
  1142. if (sign_ext) {
  1143. if (sign_ext == 8)
  1144. len = arc_sexb_r(buf, REG_LO(rd), REG_LO(rs));
  1145. else if (sign_ext == 16)
  1146. len = arc_sexh_r(buf, REG_LO(rd), REG_LO(rs));
  1147. else if (sign_ext == 32 && rd != rs)
  1148. len = arc_mov_r(buf, REG_LO(rd), REG_LO(rs));
  1149. return len;
  1150. }
  1151. /* Unsigned move. */
  1152. if (rd != rs)
  1153. len = arc_mov_r(buf, REG_LO(rd), REG_LO(rs));
  1154. return len;
  1155. }
  1156. u8 mov_r32_i32(u8 *buf, u8 reg, s32 imm)
  1157. {
  1158. return arc_mov_i(buf, REG_LO(reg), imm);
  1159. }
  1160. u8 mov_r64(u8 *buf, u8 rd, u8 rs, u8 sign_ext)
  1161. {
  1162. u8 len = 0;
  1163. if (sign_ext) {
  1164. /* First handle the low 32-bit part. */
  1165. len = mov_r32(buf, rd, rs, sign_ext);
  1166. /* Now propagate the sign bit of LO to HI. */
  1167. if (sign_ext == 8 || sign_ext == 16 || sign_ext == 32) {
  1168. len += arc_asri_r(BUF(buf, len),
  1169. REG_HI(rd), REG_LO(rd), 31);
  1170. }
  1171. return len;
  1172. }
  1173. /* Unsigned move. */
  1174. if (rd == rs)
  1175. return 0;
  1176. len = arc_mov_r(buf, REG_LO(rd), REG_LO(rs));
  1177. if (rs != BPF_REG_FP)
  1178. len += arc_mov_r(BUF(buf, len), REG_HI(rd), REG_HI(rs));
  1179. /* BPF_REG_FP is mapped to 32-bit "fp" register. */
  1180. else
  1181. len += arc_movi_r(BUF(buf, len), REG_HI(rd), 0);
  1182. return len;
  1183. }
  1184. /* Sign extend the 32-bit immediate into 64-bit register pair. */
  1185. u8 mov_r64_i32(u8 *buf, u8 reg, s32 imm)
  1186. {
  1187. u8 len = 0;
  1188. len = arc_mov_i(buf, REG_LO(reg), imm);
  1189. /* BPF_REG_FP is mapped to 32-bit "fp" register. */
  1190. if (reg != BPF_REG_FP) {
  1191. if (imm >= 0)
  1192. len += arc_movi_r(BUF(buf, len), REG_HI(reg), 0);
  1193. else
  1194. len += arc_movi_r(BUF(buf, len), REG_HI(reg), -1);
  1195. }
  1196. return len;
  1197. }
  1198. /*
  1199. * This is merely used for translation of "LD R, IMM64" instructions
  1200. * of the BPF. These sort of instructions are sometimes used for
  1201. * relocations. If during the normal pass, the relocation value is
  1202. * not known, the BPF instruction may look something like:
  1203. *
  1204. * LD R <- 0x0000_0001_0000_0001
  1205. *
  1206. * Which will nicely translate to two 4-byte ARC instructions:
  1207. *
  1208. * mov R_lo, 1 # imm is small enough to be s12
  1209. * mov R_hi, 1 # same
  1210. *
  1211. * However, during the extra pass, the IMM64 will have changed
  1212. * to the resolved address and looks something like:
  1213. *
  1214. * LD R <- 0x0000_0000_1234_5678
  1215. *
  1216. * Now, the translated code will require 12 bytes:
  1217. *
  1218. * mov R_lo, 0x12345678 # this is an 8-byte instruction
  1219. * mov R_hi, 0 # still 4 bytes
  1220. *
  1221. * Which in practice will result in overwriting the following
  1222. * instruction. To avoid such cases, we will always emit codes
  1223. * with fixed sizes.
  1224. */
  1225. u8 mov_r64_i64(u8 *buf, u8 reg, u32 lo, u32 hi)
  1226. {
  1227. u8 len;
  1228. len = arc_mov_i_fixed(buf, REG_LO(reg), lo);
  1229. len += arc_mov_i_fixed(BUF(buf, len), REG_HI(reg), hi);
  1230. return len;
  1231. }
  1232. /*
  1233. * If the "off"set is too big (doesn't encode as S9) for:
  1234. *
  1235. * {ld,st} r, [rm, off]
  1236. *
  1237. * Then emit:
  1238. *
  1239. * add r10, REG_LO(rm), off
  1240. *
  1241. * and make sure that r10 becomes the effective address:
  1242. *
  1243. * {ld,st} r, [r10, 0]
  1244. */
  1245. static u8 adjust_mem_access(u8 *buf, s16 *off, u8 size,
  1246. u8 rm, u8 *arc_reg_mem)
  1247. {
  1248. u8 len = 0;
  1249. *arc_reg_mem = REG_LO(rm);
  1250. if (!IN_S9_RANGE(*off) ||
  1251. (size == BPF_DW && !IN_S9_RANGE(*off + 4))) {
  1252. len += arc_add_i(BUF(buf, len),
  1253. REG_LO(JIT_REG_TMP), REG_LO(rm), (u32)(*off));
  1254. *arc_reg_mem = REG_LO(JIT_REG_TMP);
  1255. *off = 0;
  1256. }
  1257. return len;
  1258. }
  1259. /* store rs, [rd, off] */
  1260. u8 store_r(u8 *buf, u8 rs, u8 rd, s16 off, u8 size)
  1261. {
  1262. u8 len, arc_reg_mem;
  1263. len = adjust_mem_access(buf, &off, size, rd, &arc_reg_mem);
  1264. if (size == BPF_DW) {
  1265. len += arc_st_r(BUF(buf, len), REG_LO(rs), arc_reg_mem,
  1266. off, ZZ_4_byte);
  1267. len += arc_st_r(BUF(buf, len), REG_HI(rs), arc_reg_mem,
  1268. off + 4, ZZ_4_byte);
  1269. } else {
  1270. u8 zz = bpf_to_arc_size(size);
  1271. len += arc_st_r(BUF(buf, len), REG_LO(rs), arc_reg_mem,
  1272. off, zz);
  1273. }
  1274. return len;
  1275. }
  1276. /*
  1277. * For {8,16,32}-bit stores:
  1278. * mov r21, imm
  1279. * st r21, [...]
  1280. * For 64-bit stores:
  1281. * mov r21, imm
  1282. * st r21, [...]
  1283. * mov r21, {0,-1}
  1284. * st r21, [...+4]
  1285. */
  1286. u8 store_i(u8 *buf, s32 imm, u8 rd, s16 off, u8 size)
  1287. {
  1288. u8 len, arc_reg_mem;
  1289. /* REG_LO(JIT_REG_TMP) might be used by "adjust_mem_access()". */
  1290. const u8 arc_rs = REG_HI(JIT_REG_TMP);
  1291. len = adjust_mem_access(buf, &off, size, rd, &arc_reg_mem);
  1292. if (size == BPF_DW) {
  1293. len += arc_mov_i(BUF(buf, len), arc_rs, imm);
  1294. len += arc_st_r(BUF(buf, len), arc_rs, arc_reg_mem,
  1295. off, ZZ_4_byte);
  1296. imm = (imm >= 0 ? 0 : -1);
  1297. len += arc_mov_i(BUF(buf, len), arc_rs, imm);
  1298. len += arc_st_r(BUF(buf, len), arc_rs, arc_reg_mem,
  1299. off + 4, ZZ_4_byte);
  1300. } else {
  1301. u8 zz = bpf_to_arc_size(size);
  1302. len += arc_mov_i(BUF(buf, len), arc_rs, imm);
  1303. len += arc_st_r(BUF(buf, len), arc_rs, arc_reg_mem, off, zz);
  1304. }
  1305. return len;
  1306. }
  1307. /*
  1308. * For the calling convention of a little endian machine, the LO part
  1309. * must be on top of the stack.
  1310. */
  1311. static u8 push_r64(u8 *buf, u8 reg)
  1312. {
  1313. u8 len = 0;
  1314. #ifdef __LITTLE_ENDIAN
  1315. /* BPF_REG_FP is mapped to 32-bit "fp" register. */
  1316. if (reg != BPF_REG_FP)
  1317. len += arc_push_r(BUF(buf, len), REG_HI(reg));
  1318. len += arc_push_r(BUF(buf, len), REG_LO(reg));
  1319. #else
  1320. len += arc_push_r(BUF(buf, len), REG_LO(reg));
  1321. if (reg != BPF_REG_FP)
  1322. len += arc_push_r(BUF(buf, len), REG_HI(reg));
  1323. #endif
  1324. return len;
  1325. }
  1326. /* load rd, [rs, off] */
  1327. u8 load_r(u8 *buf, u8 rd, u8 rs, s16 off, u8 size, bool sign_ext)
  1328. {
  1329. u8 len, arc_reg_mem;
  1330. len = adjust_mem_access(buf, &off, size, rs, &arc_reg_mem);
  1331. if (size == BPF_B || size == BPF_H || size == BPF_W) {
  1332. const u8 zz = bpf_to_arc_size(size);
  1333. /* Use LD.X only if the data size is less than 32-bit. */
  1334. if (sign_ext && (zz == ZZ_1_byte || zz == ZZ_2_byte)) {
  1335. len += arc_ldx_r(BUF(buf, len), REG_LO(rd),
  1336. arc_reg_mem, off, zz);
  1337. } else {
  1338. len += arc_ld_r(BUF(buf, len), REG_LO(rd),
  1339. arc_reg_mem, off, zz);
  1340. }
  1341. if (sign_ext) {
  1342. /* Propagate the sign bit to the higher reg. */
  1343. len += arc_asri_r(BUF(buf, len),
  1344. REG_HI(rd), REG_LO(rd), 31);
  1345. } else {
  1346. len += arc_movi_r(BUF(buf, len), REG_HI(rd), 0);
  1347. }
  1348. } else if (size == BPF_DW) {
  1349. /*
  1350. * We are about to issue 2 consecutive loads:
  1351. *
  1352. * ld rx, [rb, off+0]
  1353. * ld ry, [rb, off+4]
  1354. *
  1355. * If "rx" and "rb" are the same registers, then the order
  1356. * should change to guarantee that "rb" remains intact
  1357. * during these 2 operations:
  1358. *
  1359. * ld ry, [rb, off+4]
  1360. * ld rx, [rb, off+0]
  1361. */
  1362. if (REG_LO(rd) != arc_reg_mem) {
  1363. len += arc_ld_r(BUF(buf, len), REG_LO(rd), arc_reg_mem,
  1364. off, ZZ_4_byte);
  1365. len += arc_ld_r(BUF(buf, len), REG_HI(rd), arc_reg_mem,
  1366. off + 4, ZZ_4_byte);
  1367. } else {
  1368. len += arc_ld_r(BUF(buf, len), REG_HI(rd), arc_reg_mem,
  1369. off + 4, ZZ_4_byte);
  1370. len += arc_ld_r(BUF(buf, len), REG_LO(rd), arc_reg_mem,
  1371. off, ZZ_4_byte);
  1372. }
  1373. }
  1374. return len;
  1375. }
  1376. u8 add_r32(u8 *buf, u8 rd, u8 rs)
  1377. {
  1378. return arc_add_r(buf, REG_LO(rd), REG_LO(rs));
  1379. }
  1380. u8 add_r32_i32(u8 *buf, u8 rd, s32 imm)
  1381. {
  1382. if (IN_U6_RANGE(imm))
  1383. return arc_addi_r(buf, REG_LO(rd), imm);
  1384. else
  1385. return arc_add_i(buf, REG_LO(rd), REG_LO(rd), imm);
  1386. }
  1387. u8 add_r64(u8 *buf, u8 rd, u8 rs)
  1388. {
  1389. u8 len;
  1390. len = arc_addf_r(buf, REG_LO(rd), REG_LO(rs));
  1391. len += arc_adc_r(BUF(buf, len), REG_HI(rd), REG_HI(rs));
  1392. return len;
  1393. }
  1394. u8 add_r64_i32(u8 *buf, u8 rd, s32 imm)
  1395. {
  1396. u8 len;
  1397. if (IN_U6_RANGE(imm)) {
  1398. len = arc_addif_r(buf, REG_LO(rd), imm);
  1399. len += arc_adci_r(BUF(buf, len), REG_HI(rd), 0);
  1400. } else {
  1401. len = mov_r64_i32(buf, JIT_REG_TMP, imm);
  1402. len += add_r64(BUF(buf, len), rd, JIT_REG_TMP);
  1403. }
  1404. return len;
  1405. }
  1406. u8 sub_r32(u8 *buf, u8 rd, u8 rs)
  1407. {
  1408. return arc_sub_r(buf, REG_LO(rd), REG_LO(rs));
  1409. }
  1410. u8 sub_r32_i32(u8 *buf, u8 rd, s32 imm)
  1411. {
  1412. if (IN_U6_RANGE(imm))
  1413. return arc_subi_r(buf, REG_LO(rd), imm);
  1414. else
  1415. return arc_sub_i(buf, REG_LO(rd), imm);
  1416. }
  1417. u8 sub_r64(u8 *buf, u8 rd, u8 rs)
  1418. {
  1419. u8 len;
  1420. len = arc_subf_r(buf, REG_LO(rd), REG_LO(rs));
  1421. len += arc_sbc_r(BUF(buf, len), REG_HI(rd), REG_HI(rs));
  1422. return len;
  1423. }
  1424. u8 sub_r64_i32(u8 *buf, u8 rd, s32 imm)
  1425. {
  1426. u8 len;
  1427. len = mov_r64_i32(buf, JIT_REG_TMP, imm);
  1428. len += sub_r64(BUF(buf, len), rd, JIT_REG_TMP);
  1429. return len;
  1430. }
  1431. static u8 cmp_r32(u8 *buf, u8 rd, u8 rs)
  1432. {
  1433. return arc_cmp_r(buf, REG_LO(rd), REG_LO(rs));
  1434. }
  1435. u8 neg_r32(u8 *buf, u8 r)
  1436. {
  1437. return arc_neg_r(buf, REG_LO(r), REG_LO(r));
  1438. }
  1439. /* In a two's complement system, -r is (~r + 1). */
  1440. u8 neg_r64(u8 *buf, u8 r)
  1441. {
  1442. u8 len;
  1443. len = arc_not_r(buf, REG_LO(r), REG_LO(r));
  1444. len += arc_not_r(BUF(buf, len), REG_HI(r), REG_HI(r));
  1445. len += add_r64_i32(BUF(buf, len), r, 1);
  1446. return len;
  1447. }
  1448. u8 mul_r32(u8 *buf, u8 rd, u8 rs)
  1449. {
  1450. return arc_mpy_r(buf, REG_LO(rd), REG_LO(rd), REG_LO(rs));
  1451. }
  1452. u8 mul_r32_i32(u8 *buf, u8 rd, s32 imm)
  1453. {
  1454. return arc_mpy_i(buf, REG_LO(rd), REG_LO(rd), imm);
  1455. }
  1456. /*
  1457. * MUL B, C
  1458. * --------
  1459. * mpy t0, B_hi, C_lo
  1460. * mpy t1, B_lo, C_hi
  1461. * mpydu B_lo, B_lo, C_lo
  1462. * add B_hi, B_hi, t0
  1463. * add B_hi, B_hi, t1
  1464. */
  1465. u8 mul_r64(u8 *buf, u8 rd, u8 rs)
  1466. {
  1467. const u8 t0 = REG_LO(JIT_REG_TMP);
  1468. const u8 t1 = REG_HI(JIT_REG_TMP);
  1469. const u8 C_lo = REG_LO(rs);
  1470. const u8 C_hi = REG_HI(rs);
  1471. const u8 B_lo = REG_LO(rd);
  1472. const u8 B_hi = REG_HI(rd);
  1473. u8 len;
  1474. len = arc_mpy_r(buf, t0, B_hi, C_lo);
  1475. len += arc_mpy_r(BUF(buf, len), t1, B_lo, C_hi);
  1476. len += arc_mpydu_r(BUF(buf, len), B_lo, C_lo);
  1477. len += arc_add_r(BUF(buf, len), B_hi, t0);
  1478. len += arc_add_r(BUF(buf, len), B_hi, t1);
  1479. return len;
  1480. }
  1481. /*
  1482. * MUL B, imm
  1483. * ----------
  1484. *
  1485. * To get a 64-bit result from a signed 64x32 multiplication:
  1486. *
  1487. * B_hi B_lo *
  1488. * sign imm
  1489. * -----------------------------
  1490. * HI(B_lo*imm) LO(B_lo*imm) +
  1491. * B_hi*imm +
  1492. * B_lo*sign
  1493. * -----------------------------
  1494. * res_hi res_lo
  1495. *
  1496. * mpy t1, B_lo, sign(imm)
  1497. * mpy t0, B_hi, imm
  1498. * mpydu B_lo, B_lo, imm
  1499. * add B_hi, B_hi, t0
  1500. * add B_hi, B_hi, t1
  1501. *
  1502. * Note: We can't use signed double multiplication, "mpyd", instead of an
  1503. * unsigned version, "mpydu", and then get rid of the sign adjustments
  1504. * calculated in "t1". The signed multiplication, "mpyd", will consider
  1505. * both operands, "B_lo" and "imm", as signed inputs. However, for this
  1506. * 64x32 multiplication, "B_lo" must be treated as an unsigned number.
  1507. */
  1508. u8 mul_r64_i32(u8 *buf, u8 rd, s32 imm)
  1509. {
  1510. const u8 t0 = REG_LO(JIT_REG_TMP);
  1511. const u8 t1 = REG_HI(JIT_REG_TMP);
  1512. const u8 B_lo = REG_LO(rd);
  1513. const u8 B_hi = REG_HI(rd);
  1514. u8 len = 0;
  1515. if (imm == 1)
  1516. return 0;
  1517. /* Is the sign-extension of the immediate "-1"? */
  1518. if (imm < 0)
  1519. len += arc_neg_r(BUF(buf, len), t1, B_lo);
  1520. len += arc_mpy_i(BUF(buf, len), t0, B_hi, imm);
  1521. len += arc_mpydu_i(BUF(buf, len), B_lo, imm);
  1522. len += arc_add_r(BUF(buf, len), B_hi, t0);
  1523. /* Add the "sign*B_lo" part, if necessary. */
  1524. if (imm < 0)
  1525. len += arc_add_r(BUF(buf, len), B_hi, t1);
  1526. return len;
  1527. }
  1528. u8 div_r32(u8 *buf, u8 rd, u8 rs, bool sign_ext)
  1529. {
  1530. if (sign_ext)
  1531. return arc_divs_r(buf, REG_LO(rd), REG_LO(rs));
  1532. else
  1533. return arc_divu_r(buf, REG_LO(rd), REG_LO(rs));
  1534. }
  1535. u8 div_r32_i32(u8 *buf, u8 rd, s32 imm, bool sign_ext)
  1536. {
  1537. if (imm == 0)
  1538. return 0;
  1539. if (sign_ext)
  1540. return arc_divs_i(buf, REG_LO(rd), imm);
  1541. else
  1542. return arc_divu_i(buf, REG_LO(rd), imm);
  1543. }
  1544. u8 mod_r32(u8 *buf, u8 rd, u8 rs, bool sign_ext)
  1545. {
  1546. if (sign_ext)
  1547. return arc_rems_r(buf, REG_LO(rd), REG_LO(rs));
  1548. else
  1549. return arc_remu_r(buf, REG_LO(rd), REG_LO(rs));
  1550. }
  1551. u8 mod_r32_i32(u8 *buf, u8 rd, s32 imm, bool sign_ext)
  1552. {
  1553. if (imm == 0)
  1554. return 0;
  1555. if (sign_ext)
  1556. return arc_rems_i(buf, REG_LO(rd), imm);
  1557. else
  1558. return arc_remu_i(buf, REG_LO(rd), imm);
  1559. }
  1560. u8 and_r32(u8 *buf, u8 rd, u8 rs)
  1561. {
  1562. return arc_and_r(buf, REG_LO(rd), REG_LO(rs));
  1563. }
  1564. u8 and_r32_i32(u8 *buf, u8 rd, s32 imm)
  1565. {
  1566. return arc_and_i(buf, REG_LO(rd), imm);
  1567. }
  1568. u8 and_r64(u8 *buf, u8 rd, u8 rs)
  1569. {
  1570. u8 len;
  1571. len = arc_and_r(buf, REG_LO(rd), REG_LO(rs));
  1572. len += arc_and_r(BUF(buf, len), REG_HI(rd), REG_HI(rs));
  1573. return len;
  1574. }
  1575. u8 and_r64_i32(u8 *buf, u8 rd, s32 imm)
  1576. {
  1577. u8 len;
  1578. len = mov_r64_i32(buf, JIT_REG_TMP, imm);
  1579. len += and_r64(BUF(buf, len), rd, JIT_REG_TMP);
  1580. return len;
  1581. }
  1582. static u8 tst_r32(u8 *buf, u8 rd, u8 rs)
  1583. {
  1584. return arc_tst_r(buf, REG_LO(rd), REG_LO(rs));
  1585. }
  1586. u8 or_r32(u8 *buf, u8 rd, u8 rs)
  1587. {
  1588. return arc_or_r(buf, REG_LO(rd), REG_LO(rd), REG_LO(rs));
  1589. }
  1590. u8 or_r32_i32(u8 *buf, u8 rd, s32 imm)
  1591. {
  1592. return arc_or_i(buf, REG_LO(rd), imm);
  1593. }
  1594. u8 or_r64(u8 *buf, u8 rd, u8 rs)
  1595. {
  1596. u8 len;
  1597. len = arc_or_r(buf, REG_LO(rd), REG_LO(rd), REG_LO(rs));
  1598. len += arc_or_r(BUF(buf, len), REG_HI(rd), REG_HI(rd), REG_HI(rs));
  1599. return len;
  1600. }
  1601. u8 or_r64_i32(u8 *buf, u8 rd, s32 imm)
  1602. {
  1603. u8 len;
  1604. len = mov_r64_i32(buf, JIT_REG_TMP, imm);
  1605. len += or_r64(BUF(buf, len), rd, JIT_REG_TMP);
  1606. return len;
  1607. }
  1608. u8 xor_r32(u8 *buf, u8 rd, u8 rs)
  1609. {
  1610. return arc_xor_r(buf, REG_LO(rd), REG_LO(rs));
  1611. }
  1612. u8 xor_r32_i32(u8 *buf, u8 rd, s32 imm)
  1613. {
  1614. return arc_xor_i(buf, REG_LO(rd), imm);
  1615. }
  1616. u8 xor_r64(u8 *buf, u8 rd, u8 rs)
  1617. {
  1618. u8 len;
  1619. len = arc_xor_r(buf, REG_LO(rd), REG_LO(rs));
  1620. len += arc_xor_r(BUF(buf, len), REG_HI(rd), REG_HI(rs));
  1621. return len;
  1622. }
  1623. u8 xor_r64_i32(u8 *buf, u8 rd, s32 imm)
  1624. {
  1625. u8 len;
  1626. len = mov_r64_i32(buf, JIT_REG_TMP, imm);
  1627. len += xor_r64(BUF(buf, len), rd, JIT_REG_TMP);
  1628. return len;
  1629. }
  1630. /* "asl a,b,c" --> "a = (b << (c & 31))". */
  1631. u8 lsh_r32(u8 *buf, u8 rd, u8 rs)
  1632. {
  1633. return arc_asl_r(buf, REG_LO(rd), REG_LO(rd), REG_LO(rs));
  1634. }
  1635. u8 lsh_r32_i32(u8 *buf, u8 rd, u8 imm)
  1636. {
  1637. return arc_asli_r(buf, REG_LO(rd), REG_LO(rd), imm);
  1638. }
  1639. /*
  1640. * algorithm
  1641. * ---------
  1642. * if (n <= 32)
  1643. * to_hi = lo >> (32-n) # (32-n) is the negate of "n" in a 5-bit width.
  1644. * lo <<= n
  1645. * hi <<= n
  1646. * hi |= to_hi
  1647. * else
  1648. * hi = lo << (n-32)
  1649. * lo = 0
  1650. *
  1651. * assembly translation for "LSH B, C"
  1652. * (heavily influenced by ARC gcc)
  1653. * -----------------------------------
  1654. * not t0, C_lo # The first 3 lines are almost the same as:
  1655. * lsr t1, B_lo, 1 # neg t0, C_lo
  1656. * lsr t1, t1, t0 # lsr t1, B_lo, t0 --> t1 is "to_hi"
  1657. * mov t0, C_lo* # with one important difference. In "neg"
  1658. * asl B_lo, B_lo, t0 # version, when C_lo=0, t1 becomes B_lo while
  1659. * asl B_hi, B_hi, t0 # it should be 0. The "not" approach instead,
  1660. * or B_hi, B_hi, t1 # "shift"s t1 once and 31 times, practically
  1661. * btst t0, 5 # setting it to 0 when C_lo=0.
  1662. * mov.ne B_hi, B_lo**
  1663. * mov.ne B_lo, 0
  1664. *
  1665. * *The "mov t0, C_lo" is necessary to cover the cases that C is the same
  1666. * register as B.
  1667. *
  1668. * **ARC performs a shift in this manner: B <<= (C & 31)
  1669. * For 32<=n<64, "n-32" and "n&31" are the same. Therefore, "B << n" and
  1670. * "B << (n-32)" yield the same results. e.g. the results of "B << 35" and
  1671. * "B << 3" are the same.
  1672. *
  1673. * The behaviour is undefined for n >= 64.
  1674. */
  1675. u8 lsh_r64(u8 *buf, u8 rd, u8 rs)
  1676. {
  1677. const u8 t0 = REG_LO(JIT_REG_TMP);
  1678. const u8 t1 = REG_HI(JIT_REG_TMP);
  1679. const u8 C_lo = REG_LO(rs);
  1680. const u8 B_lo = REG_LO(rd);
  1681. const u8 B_hi = REG_HI(rd);
  1682. u8 len;
  1683. len = arc_not_r(buf, t0, C_lo);
  1684. len += arc_lsri_r(BUF(buf, len), t1, B_lo, 1);
  1685. len += arc_lsr_r(BUF(buf, len), t1, t1, t0);
  1686. len += arc_mov_r(BUF(buf, len), t0, C_lo);
  1687. len += arc_asl_r(BUF(buf, len), B_lo, B_lo, t0);
  1688. len += arc_asl_r(BUF(buf, len), B_hi, B_hi, t0);
  1689. len += arc_or_r(BUF(buf, len), B_hi, B_hi, t1);
  1690. len += arc_btst_i(BUF(buf, len), t0, 5);
  1691. len += arc_mov_cc_r(BUF(buf, len), CC_unequal, B_hi, B_lo);
  1692. len += arc_movu_cc_r(BUF(buf, len), CC_unequal, B_lo, 0);
  1693. return len;
  1694. }
  1695. /*
  1696. * if (n < 32)
  1697. * to_hi = B_lo >> 32-n # extract upper n bits
  1698. * lo <<= n
  1699. * hi <<=n
  1700. * hi |= to_hi
  1701. * else if (n < 64)
  1702. * hi = lo << n-32
  1703. * lo = 0
  1704. */
  1705. u8 lsh_r64_i32(u8 *buf, u8 rd, s32 imm)
  1706. {
  1707. const u8 t0 = REG_LO(JIT_REG_TMP);
  1708. const u8 B_lo = REG_LO(rd);
  1709. const u8 B_hi = REG_HI(rd);
  1710. const u8 n = (u8)imm;
  1711. u8 len = 0;
  1712. if (n == 0) {
  1713. return 0;
  1714. } else if (n <= 31) {
  1715. len = arc_lsri_r(buf, t0, B_lo, 32 - n);
  1716. len += arc_asli_r(BUF(buf, len), B_lo, B_lo, n);
  1717. len += arc_asli_r(BUF(buf, len), B_hi, B_hi, n);
  1718. len += arc_or_r(BUF(buf, len), B_hi, B_hi, t0);
  1719. } else if (n <= 63) {
  1720. len = arc_asli_r(buf, B_hi, B_lo, n - 32);
  1721. len += arc_movi_r(BUF(buf, len), B_lo, 0);
  1722. }
  1723. /* n >= 64 is undefined behaviour. */
  1724. return len;
  1725. }
  1726. /* "lsr a,b,c" --> "a = (b >> (c & 31))". */
  1727. u8 rsh_r32(u8 *buf, u8 rd, u8 rs)
  1728. {
  1729. return arc_lsr_r(buf, REG_LO(rd), REG_LO(rd), REG_LO(rs));
  1730. }
  1731. u8 rsh_r32_i32(u8 *buf, u8 rd, u8 imm)
  1732. {
  1733. return arc_lsri_r(buf, REG_LO(rd), REG_LO(rd), imm);
  1734. }
  1735. /*
  1736. * For better commentary, see lsh_r64().
  1737. *
  1738. * algorithm
  1739. * ---------
  1740. * if (n <= 32)
  1741. * to_lo = hi << (32-n)
  1742. * hi >>= n
  1743. * lo >>= n
  1744. * lo |= to_lo
  1745. * else
  1746. * lo = hi >> (n-32)
  1747. * hi = 0
  1748. *
  1749. * RSH B,C
  1750. * ----------
  1751. * not t0, C_lo
  1752. * asl t1, B_hi, 1
  1753. * asl t1, t1, t0
  1754. * mov t0, C_lo
  1755. * lsr B_hi, B_hi, t0
  1756. * lsr B_lo, B_lo, t0
  1757. * or B_lo, B_lo, t1
  1758. * btst t0, 5
  1759. * mov.ne B_lo, B_hi
  1760. * mov.ne B_hi, 0
  1761. */
  1762. u8 rsh_r64(u8 *buf, u8 rd, u8 rs)
  1763. {
  1764. const u8 t0 = REG_LO(JIT_REG_TMP);
  1765. const u8 t1 = REG_HI(JIT_REG_TMP);
  1766. const u8 C_lo = REG_LO(rs);
  1767. const u8 B_lo = REG_LO(rd);
  1768. const u8 B_hi = REG_HI(rd);
  1769. u8 len;
  1770. len = arc_not_r(buf, t0, C_lo);
  1771. len += arc_asli_r(BUF(buf, len), t1, B_hi, 1);
  1772. len += arc_asl_r(BUF(buf, len), t1, t1, t0);
  1773. len += arc_mov_r(BUF(buf, len), t0, C_lo);
  1774. len += arc_lsr_r(BUF(buf, len), B_hi, B_hi, t0);
  1775. len += arc_lsr_r(BUF(buf, len), B_lo, B_lo, t0);
  1776. len += arc_or_r(BUF(buf, len), B_lo, B_lo, t1);
  1777. len += arc_btst_i(BUF(buf, len), t0, 5);
  1778. len += arc_mov_cc_r(BUF(buf, len), CC_unequal, B_lo, B_hi);
  1779. len += arc_movu_cc_r(BUF(buf, len), CC_unequal, B_hi, 0);
  1780. return len;
  1781. }
  1782. /*
  1783. * if (n < 32)
  1784. * to_lo = B_lo << 32-n # extract lower n bits, right-padded with 32-n 0s
  1785. * lo >>=n
  1786. * hi >>=n
  1787. * hi |= to_lo
  1788. * else if (n < 64)
  1789. * lo = hi >> n-32
  1790. * hi = 0
  1791. */
  1792. u8 rsh_r64_i32(u8 *buf, u8 rd, s32 imm)
  1793. {
  1794. const u8 t0 = REG_LO(JIT_REG_TMP);
  1795. const u8 B_lo = REG_LO(rd);
  1796. const u8 B_hi = REG_HI(rd);
  1797. const u8 n = (u8)imm;
  1798. u8 len = 0;
  1799. if (n == 0) {
  1800. return 0;
  1801. } else if (n <= 31) {
  1802. len = arc_asli_r(buf, t0, B_hi, 32 - n);
  1803. len += arc_lsri_r(BUF(buf, len), B_lo, B_lo, n);
  1804. len += arc_lsri_r(BUF(buf, len), B_hi, B_hi, n);
  1805. len += arc_or_r(BUF(buf, len), B_lo, B_lo, t0);
  1806. } else if (n <= 63) {
  1807. len = arc_lsri_r(buf, B_lo, B_hi, n - 32);
  1808. len += arc_movi_r(BUF(buf, len), B_hi, 0);
  1809. }
  1810. /* n >= 64 is undefined behaviour. */
  1811. return len;
  1812. }
  1813. /* "asr a,b,c" --> "a = (b s>> (c & 31))". */
  1814. u8 arsh_r32(u8 *buf, u8 rd, u8 rs)
  1815. {
  1816. return arc_asr_r(buf, REG_LO(rd), REG_LO(rd), REG_LO(rs));
  1817. }
  1818. u8 arsh_r32_i32(u8 *buf, u8 rd, u8 imm)
  1819. {
  1820. return arc_asri_r(buf, REG_LO(rd), REG_LO(rd), imm);
  1821. }
  1822. /*
  1823. * For comparison, see rsh_r64().
  1824. *
  1825. * algorithm
  1826. * ---------
  1827. * if (n <= 32)
  1828. * to_lo = hi << (32-n)
  1829. * hi s>>= n
  1830. * lo >>= n
  1831. * lo |= to_lo
  1832. * else
  1833. * hi_sign = hi s>>31
  1834. * lo = hi s>> (n-32)
  1835. * hi = hi_sign
  1836. *
  1837. * ARSH B,C
  1838. * ----------
  1839. * not t0, C_lo
  1840. * asl t1, B_hi, 1
  1841. * asl t1, t1, t0
  1842. * mov t0, C_lo
  1843. * asr B_hi, B_hi, t0
  1844. * lsr B_lo, B_lo, t0
  1845. * or B_lo, B_lo, t1
  1846. * btst t0, 5
  1847. * asr t0, B_hi, 31 # now, t0 = 0 or -1 based on B_hi's sign
  1848. * mov.ne B_lo, B_hi
  1849. * mov.ne B_hi, t0
  1850. */
  1851. u8 arsh_r64(u8 *buf, u8 rd, u8 rs)
  1852. {
  1853. const u8 t0 = REG_LO(JIT_REG_TMP);
  1854. const u8 t1 = REG_HI(JIT_REG_TMP);
  1855. const u8 C_lo = REG_LO(rs);
  1856. const u8 B_lo = REG_LO(rd);
  1857. const u8 B_hi = REG_HI(rd);
  1858. u8 len;
  1859. len = arc_not_r(buf, t0, C_lo);
  1860. len += arc_asli_r(BUF(buf, len), t1, B_hi, 1);
  1861. len += arc_asl_r(BUF(buf, len), t1, t1, t0);
  1862. len += arc_mov_r(BUF(buf, len), t0, C_lo);
  1863. len += arc_asr_r(BUF(buf, len), B_hi, B_hi, t0);
  1864. len += arc_lsr_r(BUF(buf, len), B_lo, B_lo, t0);
  1865. len += arc_or_r(BUF(buf, len), B_lo, B_lo, t1);
  1866. len += arc_btst_i(BUF(buf, len), t0, 5);
  1867. len += arc_asri_r(BUF(buf, len), t0, B_hi, 31);
  1868. len += arc_mov_cc_r(BUF(buf, len), CC_unequal, B_lo, B_hi);
  1869. len += arc_mov_cc_r(BUF(buf, len), CC_unequal, B_hi, t0);
  1870. return len;
  1871. }
  1872. /*
  1873. * if (n < 32)
  1874. * to_lo = lo << 32-n # extract lower n bits, right-padded with 32-n 0s
  1875. * lo >>=n
  1876. * hi s>>=n
  1877. * hi |= to_lo
  1878. * else if (n < 64)
  1879. * lo = hi s>> n-32
  1880. * hi = (lo[msb] ? -1 : 0)
  1881. */
  1882. u8 arsh_r64_i32(u8 *buf, u8 rd, s32 imm)
  1883. {
  1884. const u8 t0 = REG_LO(JIT_REG_TMP);
  1885. const u8 B_lo = REG_LO(rd);
  1886. const u8 B_hi = REG_HI(rd);
  1887. const u8 n = (u8)imm;
  1888. u8 len = 0;
  1889. if (n == 0) {
  1890. return 0;
  1891. } else if (n <= 31) {
  1892. len = arc_asli_r(buf, t0, B_hi, 32 - n);
  1893. len += arc_lsri_r(BUF(buf, len), B_lo, B_lo, n);
  1894. len += arc_asri_r(BUF(buf, len), B_hi, B_hi, n);
  1895. len += arc_or_r(BUF(buf, len), B_lo, B_lo, t0);
  1896. } else if (n <= 63) {
  1897. len = arc_asri_r(buf, B_lo, B_hi, n - 32);
  1898. len += arc_movi_r(BUF(buf, len), B_hi, -1);
  1899. len += arc_btst_i(BUF(buf, len), B_lo, 31);
  1900. len += arc_movu_cc_r(BUF(buf, len), CC_equal, B_hi, 0);
  1901. }
  1902. /* n >= 64 is undefined behaviour. */
  1903. return len;
  1904. }
  1905. u8 gen_swap(u8 *buf, u8 rd, u8 size, u8 endian, bool force, bool do_zext)
  1906. {
  1907. u8 len = 0;
  1908. #ifdef __BIG_ENDIAN
  1909. const u8 host_endian = BPF_FROM_BE;
  1910. #else
  1911. const u8 host_endian = BPF_FROM_LE;
  1912. #endif
  1913. if (host_endian != endian || force) {
  1914. switch (size) {
  1915. case 16:
  1916. /*
  1917. * r = B4B3_B2B1 << 16 --> r = B2B1_0000
  1918. * then, swape(r) would become the desired 0000_B1B2
  1919. */
  1920. len = arc_asli_r(buf, REG_LO(rd), REG_LO(rd), 16);
  1921. fallthrough;
  1922. case 32:
  1923. len += arc_swape_r(BUF(buf, len), REG_LO(rd));
  1924. if (do_zext)
  1925. len += zext(BUF(buf, len), rd);
  1926. break;
  1927. case 64:
  1928. /*
  1929. * swap "hi" and "lo":
  1930. * hi ^= lo;
  1931. * lo ^= hi;
  1932. * hi ^= lo;
  1933. * and then swap the bytes in "hi" and "lo".
  1934. */
  1935. len = arc_xor_r(buf, REG_HI(rd), REG_LO(rd));
  1936. len += arc_xor_r(BUF(buf, len), REG_LO(rd), REG_HI(rd));
  1937. len += arc_xor_r(BUF(buf, len), REG_HI(rd), REG_LO(rd));
  1938. len += arc_swape_r(BUF(buf, len), REG_LO(rd));
  1939. len += arc_swape_r(BUF(buf, len), REG_HI(rd));
  1940. break;
  1941. default:
  1942. /* The caller must have handled this. */
  1943. break;
  1944. }
  1945. } else {
  1946. /*
  1947. * If the same endianness, there's not much to do other
  1948. * than zeroing out the upper bytes based on the "size".
  1949. */
  1950. switch (size) {
  1951. case 16:
  1952. len = arc_and_i(buf, REG_LO(rd), 0xffff);
  1953. fallthrough;
  1954. case 32:
  1955. if (do_zext)
  1956. len += zext(BUF(buf, len), rd);
  1957. break;
  1958. case 64:
  1959. break;
  1960. default:
  1961. /* The caller must have handled this. */
  1962. break;
  1963. }
  1964. }
  1965. return len;
  1966. }
  1967. /*
  1968. * To create a frame, all that is needed is:
  1969. *
  1970. * push fp
  1971. * mov fp, sp
  1972. * sub sp, <frame_size>
  1973. *
  1974. * "push fp" is taken care of separately while saving the clobbered registers.
  1975. * All that remains is copying SP value to FP and shrinking SP's address space
  1976. * for any possible function call to come.
  1977. */
  1978. static inline u8 frame_create(u8 *buf, u16 size)
  1979. {
  1980. u8 len;
  1981. len = arc_mov_r(buf, ARC_R_FP, ARC_R_SP);
  1982. if (IN_U6_RANGE(size))
  1983. len += arc_subi_r(BUF(buf, len), ARC_R_SP, size);
  1984. else
  1985. len += arc_sub_i(BUF(buf, len), ARC_R_SP, size);
  1986. return len;
  1987. }
  1988. /*
  1989. * mov sp, fp
  1990. *
  1991. * The value of SP upon entering was copied to FP.
  1992. */
  1993. static inline u8 frame_restore(u8 *buf)
  1994. {
  1995. return arc_mov_r(buf, ARC_R_SP, ARC_R_FP);
  1996. }
  1997. /*
  1998. * Going from a JITed code to the native caller:
  1999. *
  2000. * mov ARC_ABI_RET_lo, BPF_REG_0_lo # r0 <- r8
  2001. * mov ARC_ABI_RET_hi, BPF_REG_0_hi # r1 <- r9
  2002. */
  2003. static u8 bpf_to_arc_return(u8 *buf)
  2004. {
  2005. u8 len;
  2006. len = arc_mov_r(buf, ARC_R_0, REG_LO(BPF_REG_0));
  2007. len += arc_mov_r(BUF(buf, len), ARC_R_1, REG_HI(BPF_REG_0));
  2008. return len;
  2009. }
  2010. /*
  2011. * Coming back from an external (in-kernel) function to the JITed code:
  2012. *
  2013. * mov ARC_ABI_RET_lo, BPF_REG_0_lo # r8 <- r0
  2014. * mov ARC_ABI_RET_hi, BPF_REG_0_hi # r9 <- r1
  2015. */
  2016. u8 arc_to_bpf_return(u8 *buf)
  2017. {
  2018. u8 len;
  2019. len = arc_mov_r(buf, REG_LO(BPF_REG_0), ARC_R_0);
  2020. len += arc_mov_r(BUF(buf, len), REG_HI(BPF_REG_0), ARC_R_1);
  2021. return len;
  2022. }
  2023. /*
  2024. * This translation leads to:
  2025. *
  2026. * mov r10, addr # always an 8-byte instruction
  2027. * jl [r10]
  2028. *
  2029. * The length of the "mov" must be fixed (8), otherwise it may diverge
  2030. * during the normal and extra passes:
  2031. *
  2032. * normal pass extra pass
  2033. *
  2034. * 180: mov r10,0 | 180: mov r10,0x700578d8
  2035. * 184: jl [r10] | 188: jl [r10]
  2036. * 188: add.f r16,r16,0x1 | 18c: adc r17,r17,0
  2037. * 18c: adc r17,r17,0 |
  2038. *
  2039. * In the above example, the change from "r10 <- 0" to "r10 <- 0x700578d8"
  2040. * has led to an increase in the length of the "mov" instruction.
  2041. * Inadvertently, that caused the loss of the "add.f" instruction.
  2042. */
  2043. static u8 jump_and_link(u8 *buf, u32 addr)
  2044. {
  2045. u8 len;
  2046. len = arc_mov_i_fixed(buf, REG_LO(JIT_REG_TMP), addr);
  2047. len += arc_jl(BUF(buf, len), REG_LO(JIT_REG_TMP));
  2048. return len;
  2049. }
  2050. /*
  2051. * This function determines which ARC registers must be saved and restored.
  2052. * It does so by looking into:
  2053. *
  2054. * "bpf_reg": The clobbered (destination) BPF register
  2055. * "is_call": Indicator if the current instruction is a call
  2056. *
  2057. * When a register of interest is clobbered, its corresponding bit position
  2058. * in return value, "usage", is set to true.
  2059. */
  2060. u32 mask_for_used_regs(u8 bpf_reg, bool is_call)
  2061. {
  2062. u32 usage = 0;
  2063. /* BPF registers that must be saved. */
  2064. if (bpf_reg >= BPF_REG_6 && bpf_reg <= BPF_REG_9) {
  2065. usage |= BIT(REG_LO(bpf_reg));
  2066. usage |= BIT(REG_HI(bpf_reg));
  2067. /*
  2068. * Using the frame pointer register implies that it should
  2069. * be saved and reinitialised with the current frame data.
  2070. */
  2071. } else if (bpf_reg == BPF_REG_FP) {
  2072. usage |= BIT(REG_LO(BPF_REG_FP));
  2073. /* Could there be some ARC registers that must to be saved? */
  2074. } else {
  2075. if (REG_LO(bpf_reg) >= ARC_CALLEE_SAVED_REG_FIRST &&
  2076. REG_LO(bpf_reg) <= ARC_CALLEE_SAVED_REG_LAST)
  2077. usage |= BIT(REG_LO(bpf_reg));
  2078. if (REG_HI(bpf_reg) >= ARC_CALLEE_SAVED_REG_FIRST &&
  2079. REG_HI(bpf_reg) <= ARC_CALLEE_SAVED_REG_LAST)
  2080. usage |= BIT(REG_HI(bpf_reg));
  2081. }
  2082. /* A "call" indicates that ARC's "blink" reg must be saved. */
  2083. usage |= is_call ? BIT(ARC_R_BLINK) : 0;
  2084. return usage;
  2085. }
  2086. /*
  2087. * push blink # if blink is marked as clobbered
  2088. * push r[0-n] # if r[i] is marked as clobbered
  2089. * push fp # if fp is marked as clobbered
  2090. * mov fp, sp # if frame_size > 0 (clobbers fp)
  2091. * sub sp, <frame_size> # same as above
  2092. */
  2093. u8 arc_prologue(u8 *buf, u32 usage, u16 frame_size)
  2094. {
  2095. u8 len = 0;
  2096. u32 gp_regs = 0;
  2097. /* Deal with blink first. */
  2098. if (usage & BIT(ARC_R_BLINK))
  2099. len += arc_push_r(BUF(buf, len), ARC_R_BLINK);
  2100. gp_regs = usage & ~(BIT(ARC_R_BLINK) | BIT(ARC_R_FP));
  2101. while (gp_regs) {
  2102. u8 reg = __builtin_ffs(gp_regs) - 1;
  2103. len += arc_push_r(BUF(buf, len), reg);
  2104. gp_regs &= ~BIT(reg);
  2105. }
  2106. /* Deal with fp last. */
  2107. if ((usage & BIT(ARC_R_FP)) || frame_size > 0)
  2108. len += arc_push_r(BUF(buf, len), ARC_R_FP);
  2109. if (frame_size > 0)
  2110. len += frame_create(BUF(buf, len), frame_size);
  2111. #ifdef ARC_BPF_JIT_DEBUG
  2112. if ((usage & BIT(ARC_R_FP)) && frame_size == 0) {
  2113. pr_err("FP is being saved while there is no frame.");
  2114. BUG();
  2115. }
  2116. #endif
  2117. return len;
  2118. }
  2119. /*
  2120. * mov sp, fp # if frame_size > 0
  2121. * pop fp # if fp is marked as clobbered
  2122. * pop r[n-0] # if r[i] is marked as clobbered
  2123. * pop blink # if blink is marked as clobbered
  2124. * mov r0, r8 # always: ABI_return <- BPF_return
  2125. * mov r1, r9 # continuation of above
  2126. * j [blink] # always
  2127. *
  2128. * "fp being marked as clobbered" and "frame_size > 0" are the two sides of
  2129. * the same coin.
  2130. */
  2131. u8 arc_epilogue(u8 *buf, u32 usage, u16 frame_size)
  2132. {
  2133. u32 len = 0;
  2134. u32 gp_regs = 0;
  2135. #ifdef ARC_BPF_JIT_DEBUG
  2136. if ((usage & BIT(ARC_R_FP)) && frame_size == 0) {
  2137. pr_err("FP is being saved while there is no frame.");
  2138. BUG();
  2139. }
  2140. #endif
  2141. if (frame_size > 0)
  2142. len += frame_restore(BUF(buf, len));
  2143. /* Deal with fp first. */
  2144. if ((usage & BIT(ARC_R_FP)) || frame_size > 0)
  2145. len += arc_pop_r(BUF(buf, len), ARC_R_FP);
  2146. gp_regs = usage & ~(BIT(ARC_R_BLINK) | BIT(ARC_R_FP));
  2147. while (gp_regs) {
  2148. /* "usage" is 32-bit, each bit indicating an ARC register. */
  2149. u8 reg = 31 - __builtin_clz(gp_regs);
  2150. len += arc_pop_r(BUF(buf, len), reg);
  2151. gp_regs &= ~BIT(reg);
  2152. }
  2153. /* Deal with blink last. */
  2154. if (usage & BIT(ARC_R_BLINK))
  2155. len += arc_pop_r(BUF(buf, len), ARC_R_BLINK);
  2156. /* Wrap up the return value and jump back to the caller. */
  2157. len += bpf_to_arc_return(BUF(buf, len));
  2158. len += arc_jmp_return(BUF(buf, len));
  2159. return len;
  2160. }
  2161. /*
  2162. * For details on the algorithm, see the comments of "gen_jcc_64()".
  2163. *
  2164. * This data structure is holding information for jump translations.
  2165. *
  2166. * jit_off: How many bytes into the current JIT address, "b"ranch insn. occurs
  2167. * cond: The condition that the ARC branch instruction must use
  2168. *
  2169. * e.g.:
  2170. *
  2171. * BPF_JGE R1, R0, @target
  2172. * ------------------------
  2173. * |
  2174. * v
  2175. * 0x1000: cmp r3, r1 # 0x1000 is the JIT address for "BPF_JGE ..." insn
  2176. * 0x1004: bhi @target # first jump (branch higher)
  2177. * 0x1008: blo @end # second jump acting as a skip (end is 0x1014)
  2178. * 0x100C: cmp r2, r0 # the lower 32 bits are evaluated
  2179. * 0x1010: bhs @target # third jump (branch higher or same)
  2180. * 0x1014: ...
  2181. *
  2182. * The jit_off(set) of the "bhi" is 4 bytes.
  2183. * The cond(ition) for the "bhi" is "CC_great_u".
  2184. *
  2185. * The jit_off(set) is necessary for calculating the exact displacement
  2186. * to the "target" address:
  2187. *
  2188. * jit_address + jit_off(set) - @target
  2189. * 0x1000 + 4 - @target
  2190. */
  2191. #define JCC64_NR_OF_JMPS 3 /* Number of jumps in jcc64 template. */
  2192. #define JCC64_INSNS_TO_END 3 /* Number of insn. inclusive the 2nd jmp to end. */
  2193. #define JCC64_SKIP_JMP 1 /* Index of the "skip" jump to "end". */
  2194. static const struct {
  2195. /*
  2196. * "jit_off" is common between all "jmp[]" and is coupled with
  2197. * "cond" of each "jmp[]" instance. e.g.:
  2198. *
  2199. * arcv2_64_jccs.jit_off[1]
  2200. * arcv2_64_jccs.jmp[ARC_CC_UGT].cond[1]
  2201. *
  2202. * Are indicating that the second jump in JITed code of "UGT"
  2203. * is at offset "jit_off[1]" while its condition is "cond[1]".
  2204. */
  2205. u8 jit_off[JCC64_NR_OF_JMPS];
  2206. struct {
  2207. u8 cond[JCC64_NR_OF_JMPS];
  2208. } jmp[ARC_CC_SLE + 1];
  2209. } arcv2_64_jccs = {
  2210. .jit_off = {
  2211. INSN_len_normal * 1,
  2212. INSN_len_normal * 2,
  2213. INSN_len_normal * 4
  2214. },
  2215. /*
  2216. * cmp rd_hi, rs_hi
  2217. * bhi @target # 1: u>
  2218. * blo @end # 2: u<
  2219. * cmp rd_lo, rs_lo
  2220. * bhi @target # 3: u>
  2221. * end:
  2222. */
  2223. .jmp[ARC_CC_UGT] = {
  2224. .cond = {CC_great_u, CC_less_u, CC_great_u}
  2225. },
  2226. /*
  2227. * cmp rd_hi, rs_hi
  2228. * bhi @target # 1: u>
  2229. * blo @end # 2: u<
  2230. * cmp rd_lo, rs_lo
  2231. * bhs @target # 3: u>=
  2232. * end:
  2233. */
  2234. .jmp[ARC_CC_UGE] = {
  2235. .cond = {CC_great_u, CC_less_u, CC_great_eq_u}
  2236. },
  2237. /*
  2238. * cmp rd_hi, rs_hi
  2239. * blo @target # 1: u<
  2240. * bhi @end # 2: u>
  2241. * cmp rd_lo, rs_lo
  2242. * blo @target # 3: u<
  2243. * end:
  2244. */
  2245. .jmp[ARC_CC_ULT] = {
  2246. .cond = {CC_less_u, CC_great_u, CC_less_u}
  2247. },
  2248. /*
  2249. * cmp rd_hi, rs_hi
  2250. * blo @target # 1: u<
  2251. * bhi @end # 2: u>
  2252. * cmp rd_lo, rs_lo
  2253. * bls @target # 3: u<=
  2254. * end:
  2255. */
  2256. .jmp[ARC_CC_ULE] = {
  2257. .cond = {CC_less_u, CC_great_u, CC_less_eq_u}
  2258. },
  2259. /*
  2260. * cmp rd_hi, rs_hi
  2261. * bgt @target # 1: s>
  2262. * blt @end # 2: s<
  2263. * cmp rd_lo, rs_lo
  2264. * bhi @target # 3: u>
  2265. * end:
  2266. */
  2267. .jmp[ARC_CC_SGT] = {
  2268. .cond = {CC_great_s, CC_less_s, CC_great_u}
  2269. },
  2270. /*
  2271. * cmp rd_hi, rs_hi
  2272. * bgt @target # 1: s>
  2273. * blt @end # 2: s<
  2274. * cmp rd_lo, rs_lo
  2275. * bhs @target # 3: u>=
  2276. * end:
  2277. */
  2278. .jmp[ARC_CC_SGE] = {
  2279. .cond = {CC_great_s, CC_less_s, CC_great_eq_u}
  2280. },
  2281. /*
  2282. * cmp rd_hi, rs_hi
  2283. * blt @target # 1: s<
  2284. * bgt @end # 2: s>
  2285. * cmp rd_lo, rs_lo
  2286. * blo @target # 3: u<
  2287. * end:
  2288. */
  2289. .jmp[ARC_CC_SLT] = {
  2290. .cond = {CC_less_s, CC_great_s, CC_less_u}
  2291. },
  2292. /*
  2293. * cmp rd_hi, rs_hi
  2294. * blt @target # 1: s<
  2295. * bgt @end # 2: s>
  2296. * cmp rd_lo, rs_lo
  2297. * bls @target # 3: u<=
  2298. * end:
  2299. */
  2300. .jmp[ARC_CC_SLE] = {
  2301. .cond = {CC_less_s, CC_great_s, CC_less_eq_u}
  2302. }
  2303. };
  2304. /*
  2305. * The displacement (offset) for ARC's "b"ranch instruction is the distance
  2306. * from the aligned version of _current_ instruction (PCL) to the target
  2307. * instruction:
  2308. *
  2309. * DISP = TARGET - PCL # PCL is the word aligned PC
  2310. */
  2311. static inline s32 get_displacement(u32 curr_off, u32 targ_off)
  2312. {
  2313. return (s32)(targ_off - (curr_off & ~3L));
  2314. }
  2315. /*
  2316. * "disp"lacement should be:
  2317. *
  2318. * 1. 16-bit aligned.
  2319. * 2. fit in S25, because no "condition code" is supposed to be encoded.
  2320. */
  2321. static inline bool is_valid_far_disp(s32 disp)
  2322. {
  2323. return (!(disp & 1) && IN_S25_RANGE(disp));
  2324. }
  2325. /*
  2326. * "disp"lacement should be:
  2327. *
  2328. * 1. 16-bit aligned.
  2329. * 2. fit in S21, because "condition code" is supposed to be encoded too.
  2330. */
  2331. static inline bool is_valid_near_disp(s32 disp)
  2332. {
  2333. return (!(disp & 1) && IN_S21_RANGE(disp));
  2334. }
  2335. /*
  2336. * cmp rd_hi, rs_hi
  2337. * cmp.z rd_lo, rs_lo
  2338. * b{eq,ne} @target
  2339. * | |
  2340. * | `--> "eq" param is false (JNE)
  2341. * `-----> "eq" param is true (JEQ)
  2342. */
  2343. static int gen_j_eq_64(u8 *buf, u8 rd, u8 rs, bool eq,
  2344. u32 curr_off, u32 targ_off)
  2345. {
  2346. s32 disp;
  2347. u8 len = 0;
  2348. len += arc_cmp_r(BUF(buf, len), REG_HI(rd), REG_HI(rs));
  2349. len += arc_cmpz_r(BUF(buf, len), REG_LO(rd), REG_LO(rs));
  2350. disp = get_displacement(curr_off + len, targ_off);
  2351. len += arc_bcc(BUF(buf, len), eq ? CC_equal : CC_unequal, disp);
  2352. return len;
  2353. }
  2354. /*
  2355. * tst rd_hi, rs_hi
  2356. * tst.z rd_lo, rs_lo
  2357. * bne @target
  2358. */
  2359. static u8 gen_jset_64(u8 *buf, u8 rd, u8 rs, u32 curr_off, u32 targ_off)
  2360. {
  2361. u8 len = 0;
  2362. s32 disp;
  2363. len += arc_tst_r(BUF(buf, len), REG_HI(rd), REG_HI(rs));
  2364. len += arc_tstz_r(BUF(buf, len), REG_LO(rd), REG_LO(rs));
  2365. disp = get_displacement(curr_off + len, targ_off);
  2366. len += arc_bcc(BUF(buf, len), CC_unequal, disp);
  2367. return len;
  2368. }
  2369. /*
  2370. * Verify if all the jumps for a JITed jcc64 operation are valid,
  2371. * by consulting the data stored at "arcv2_64_jccs".
  2372. */
  2373. static bool check_jcc_64(u32 curr_off, u32 targ_off, u8 cond)
  2374. {
  2375. size_t i;
  2376. if (cond >= ARC_CC_LAST)
  2377. return false;
  2378. for (i = 0; i < JCC64_NR_OF_JMPS; i++) {
  2379. u32 from, to;
  2380. from = curr_off + arcv2_64_jccs.jit_off[i];
  2381. /* for the 2nd jump, we jump to the end of block. */
  2382. if (i != JCC64_SKIP_JMP)
  2383. to = targ_off;
  2384. else
  2385. to = from + (JCC64_INSNS_TO_END * INSN_len_normal);
  2386. /* There is a "cc" in the instruction, so a "near" jump. */
  2387. if (!is_valid_near_disp(get_displacement(from, to)))
  2388. return false;
  2389. }
  2390. return true;
  2391. }
  2392. /* Can the jump from "curr_off" to "targ_off" actually happen? */
  2393. bool check_jmp_64(u32 curr_off, u32 targ_off, u8 cond)
  2394. {
  2395. s32 disp;
  2396. switch (cond) {
  2397. case ARC_CC_UGT:
  2398. case ARC_CC_UGE:
  2399. case ARC_CC_ULT:
  2400. case ARC_CC_ULE:
  2401. case ARC_CC_SGT:
  2402. case ARC_CC_SGE:
  2403. case ARC_CC_SLT:
  2404. case ARC_CC_SLE:
  2405. return check_jcc_64(curr_off, targ_off, cond);
  2406. case ARC_CC_EQ:
  2407. case ARC_CC_NE:
  2408. case ARC_CC_SET:
  2409. /*
  2410. * The "jump" for the JITed BPF_J{SET,EQ,NE} is actually the
  2411. * 3rd instruction. See comments of "gen_j{set,_eq}_64()".
  2412. */
  2413. curr_off += 2 * INSN_len_normal;
  2414. disp = get_displacement(curr_off, targ_off);
  2415. /* There is a "cc" field in the issued instruction. */
  2416. return is_valid_near_disp(disp);
  2417. case ARC_CC_AL:
  2418. disp = get_displacement(curr_off, targ_off);
  2419. return is_valid_far_disp(disp);
  2420. default:
  2421. return false;
  2422. }
  2423. }
  2424. /*
  2425. * The template for the 64-bit jumps with the following BPF conditions
  2426. *
  2427. * u< u<= u> u>= s< s<= s> s>=
  2428. *
  2429. * Looks like below:
  2430. *
  2431. * cmp rd_hi, rs_hi
  2432. * b<c1> @target
  2433. * b<c2> @end
  2434. * cmp rd_lo, rs_lo # if execution reaches here, r{d,s}_hi are equal
  2435. * b<c3> @target
  2436. * end:
  2437. *
  2438. * "c1" is the condition that JIT is handling minus the equality part.
  2439. * For instance if we have to translate an "unsigned greater or equal",
  2440. * then "c1" will be "unsigned greater". We won't know about equality
  2441. * until all 64-bits of data (higeher and lower registers) are processed.
  2442. *
  2443. * "c2" is the counter logic of "c1". For instance, if "c1" is originated
  2444. * from "s>", then "c2" would be "s<". Notice that equality doesn't play
  2445. * a role here either, because the lower 32 bits are not processed yet.
  2446. *
  2447. * "c3" is the unsigned version of "c1", no matter if the BPF condition
  2448. * was signed or unsigned. An unsigned version is necessary, because the
  2449. * MSB of the lower 32 bits does not reflect a sign in the whole 64-bit
  2450. * scheme. Otherwise, 64-bit comparisons like
  2451. * (0x0000_0000,0x8000_0000) s>= (0x0000_0000,0x0000_0000)
  2452. * would yield an incorrect result. Finally, if there is an equality
  2453. * check in the BPF condition, it will be reflected in "c3".
  2454. *
  2455. * You can find all the instances of this template where the
  2456. * "arcv2_64_jccs" is getting initialised.
  2457. */
  2458. static u8 gen_jcc_64(u8 *buf, u8 rd, u8 rs, u8 cond,
  2459. u32 curr_off, u32 targ_off)
  2460. {
  2461. s32 disp;
  2462. u32 end_off;
  2463. const u8 *cc = arcv2_64_jccs.jmp[cond].cond;
  2464. u8 len = 0;
  2465. /* cmp rd_hi, rs_hi */
  2466. len += arc_cmp_r(buf, REG_HI(rd), REG_HI(rs));
  2467. /* b<c1> @target */
  2468. disp = get_displacement(curr_off + len, targ_off);
  2469. len += arc_bcc(BUF(buf, len), cc[0], disp);
  2470. /* b<c2> @end */
  2471. end_off = curr_off + len + (JCC64_INSNS_TO_END * INSN_len_normal);
  2472. disp = get_displacement(curr_off + len, end_off);
  2473. len += arc_bcc(BUF(buf, len), cc[1], disp);
  2474. /* cmp rd_lo, rs_lo */
  2475. len += arc_cmp_r(BUF(buf, len), REG_LO(rd), REG_LO(rs));
  2476. /* b<c3> @target */
  2477. disp = get_displacement(curr_off + len, targ_off);
  2478. len += arc_bcc(BUF(buf, len), cc[2], disp);
  2479. return len;
  2480. }
  2481. /*
  2482. * This function only applies the necessary logic to make the proper
  2483. * translations. All the sanity checks must have already been done
  2484. * by calling the check_jmp_64().
  2485. */
  2486. u8 gen_jmp_64(u8 *buf, u8 rd, u8 rs, u8 cond, u32 curr_off, u32 targ_off)
  2487. {
  2488. u8 len = 0;
  2489. bool eq = false;
  2490. s32 disp;
  2491. switch (cond) {
  2492. case ARC_CC_AL:
  2493. disp = get_displacement(curr_off, targ_off);
  2494. len = arc_b(buf, disp);
  2495. break;
  2496. case ARC_CC_UGT:
  2497. case ARC_CC_UGE:
  2498. case ARC_CC_ULT:
  2499. case ARC_CC_ULE:
  2500. case ARC_CC_SGT:
  2501. case ARC_CC_SGE:
  2502. case ARC_CC_SLT:
  2503. case ARC_CC_SLE:
  2504. len = gen_jcc_64(buf, rd, rs, cond, curr_off, targ_off);
  2505. break;
  2506. case ARC_CC_EQ:
  2507. eq = true;
  2508. fallthrough;
  2509. case ARC_CC_NE:
  2510. len = gen_j_eq_64(buf, rd, rs, eq, curr_off, targ_off);
  2511. break;
  2512. case ARC_CC_SET:
  2513. len = gen_jset_64(buf, rd, rs, curr_off, targ_off);
  2514. break;
  2515. default:
  2516. #ifdef ARC_BPF_JIT_DEBUG
  2517. pr_err("64-bit jump condition is not known.");
  2518. BUG();
  2519. #endif
  2520. }
  2521. return len;
  2522. }
  2523. /*
  2524. * The condition codes to use when generating JIT instructions
  2525. * for 32-bit jumps.
  2526. *
  2527. * The "ARC_CC_AL" index is not really used by the code, but it
  2528. * is here for the sake of completeness.
  2529. *
  2530. * The "ARC_CC_SET" becomes "CC_unequal" because of the "tst"
  2531. * instruction that precedes the conditional branch.
  2532. */
  2533. static const u8 arcv2_32_jmps[ARC_CC_LAST] = {
  2534. [ARC_CC_UGT] = CC_great_u,
  2535. [ARC_CC_UGE] = CC_great_eq_u,
  2536. [ARC_CC_ULT] = CC_less_u,
  2537. [ARC_CC_ULE] = CC_less_eq_u,
  2538. [ARC_CC_SGT] = CC_great_s,
  2539. [ARC_CC_SGE] = CC_great_eq_s,
  2540. [ARC_CC_SLT] = CC_less_s,
  2541. [ARC_CC_SLE] = CC_less_eq_s,
  2542. [ARC_CC_AL] = CC_always,
  2543. [ARC_CC_EQ] = CC_equal,
  2544. [ARC_CC_NE] = CC_unequal,
  2545. [ARC_CC_SET] = CC_unequal
  2546. };
  2547. /* Can the jump from "curr_off" to "targ_off" actually happen? */
  2548. bool check_jmp_32(u32 curr_off, u32 targ_off, u8 cond)
  2549. {
  2550. u8 addendum;
  2551. s32 disp;
  2552. if (cond >= ARC_CC_LAST)
  2553. return false;
  2554. /*
  2555. * The unconditional jump happens immediately, while the rest
  2556. * are either preceded by a "cmp" or "tst" instruction.
  2557. */
  2558. addendum = (cond == ARC_CC_AL) ? 0 : INSN_len_normal;
  2559. disp = get_displacement(curr_off + addendum, targ_off);
  2560. if (cond == ARC_CC_AL)
  2561. return is_valid_far_disp(disp);
  2562. else
  2563. return is_valid_near_disp(disp);
  2564. }
  2565. /*
  2566. * The JITed code for 32-bit (conditional) branches:
  2567. *
  2568. * ARC_CC_AL @target
  2569. * b @jit_targ_addr
  2570. *
  2571. * ARC_CC_SET rd, rs, @target
  2572. * tst rd, rs
  2573. * bnz @jit_targ_addr
  2574. *
  2575. * ARC_CC_xx rd, rs, @target
  2576. * cmp rd, rs
  2577. * b<cc> @jit_targ_addr # cc = arcv2_32_jmps[xx]
  2578. */
  2579. u8 gen_jmp_32(u8 *buf, u8 rd, u8 rs, u8 cond, u32 curr_off, u32 targ_off)
  2580. {
  2581. s32 disp;
  2582. u8 len = 0;
  2583. /*
  2584. * Although this must have already been checked by "check_jmp_32()",
  2585. * we're not going to risk accessing "arcv2_32_jmps" array without
  2586. * the boundary check.
  2587. */
  2588. if (cond >= ARC_CC_LAST) {
  2589. #ifdef ARC_BPF_JIT_DEBUG
  2590. pr_err("32-bit jump condition is not known.");
  2591. BUG();
  2592. #endif
  2593. return 0;
  2594. }
  2595. /* If there is a "condition", issue the "cmp" or "tst" first. */
  2596. if (cond != ARC_CC_AL) {
  2597. if (cond == ARC_CC_SET)
  2598. len = tst_r32(buf, rd, rs);
  2599. else
  2600. len = cmp_r32(buf, rd, rs);
  2601. /*
  2602. * The issued instruction affects the "disp"lacement as
  2603. * it alters the "curr_off" by its "len"gth. The "curr_off"
  2604. * should always point to the jump instruction.
  2605. */
  2606. disp = get_displacement(curr_off + len, targ_off);
  2607. len += arc_bcc(BUF(buf, len), arcv2_32_jmps[cond], disp);
  2608. } else {
  2609. /* The straight forward unconditional jump. */
  2610. disp = get_displacement(curr_off, targ_off);
  2611. len = arc_b(buf, disp);
  2612. }
  2613. return len;
  2614. }
  2615. /*
  2616. * Generate code for functions calls. There can be two types of calls:
  2617. *
  2618. * - Calling another BPF function
  2619. * - Calling an in-kernel function which is compiled by ARC gcc
  2620. *
  2621. * In the later case, we must comply to ARCv2 ABI and handle arguments
  2622. * and return values accordingly.
  2623. */
  2624. u8 gen_func_call(u8 *buf, ARC_ADDR func_addr, bool external_func)
  2625. {
  2626. u8 len = 0;
  2627. /*
  2628. * In case of an in-kernel function call, always push the 5th
  2629. * argument onto the stack, because that's where the ABI dictates
  2630. * it should be found. If the callee doesn't really use it, no harm
  2631. * is done. The stack is readjusted either way after the call.
  2632. */
  2633. if (external_func)
  2634. len += push_r64(BUF(buf, len), BPF_REG_5);
  2635. len += jump_and_link(BUF(buf, len), func_addr);
  2636. if (external_func)
  2637. len += arc_add_i(BUF(buf, len), ARC_R_SP, ARC_R_SP, ARG5_SIZE);
  2638. return len;
  2639. }