blob: c56f129c9a4ba400e70a3be459b689f4fd103b25 [file] [log] [blame]
Thiemo Seufere30ec452008-01-28 20:05:38 +00001/*
2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
4 * for more details.
5 *
6 * A small micro-assembler. It is intentionally kept simple, does only
7 * support a subset of instructions, and does not try to hide pipeline
8 * effects like branch delay slots.
9 *
Ralf Baechle70342282013-01-22 12:59:30 +010010 * Copyright (C) 2004, 2005, 2006, 2008 Thiemo Seufer
Thiemo Seufere30ec452008-01-28 20:05:38 +000011 * Copyright (C) 2005, 2007 Maciej W. Rozycki
12 * Copyright (C) 2006 Ralf Baechle (ralf@linux-mips.org)
Steven J. Hillabc597f2013-02-05 16:52:01 -060013 * Copyright (C) 2012, 2013 MIPS Technologies, Inc. All rights reserved.
Thiemo Seufere30ec452008-01-28 20:05:38 +000014 */
15
Thiemo Seufere30ec452008-01-28 20:05:38 +000016enum fields {
17 RS = 0x001,
18 RT = 0x002,
19 RD = 0x004,
20 RE = 0x008,
21 SIMM = 0x010,
22 UIMM = 0x020,
23 BIMM = 0x040,
24 JIMM = 0x080,
25 FUNC = 0x100,
David Daney58b9e222010-02-18 16:13:03 -080026 SET = 0x200,
Leonid Yegoshin51eec48e2014-11-18 16:24:15 +000027 SCIMM = 0x400,
28 SIMM9 = 0x800,
Thiemo Seufere30ec452008-01-28 20:05:38 +000029};
30
31#define OP_MASK 0x3f
32#define OP_SH 26
Thiemo Seufere30ec452008-01-28 20:05:38 +000033#define RD_MASK 0x1f
34#define RD_SH 11
35#define RE_MASK 0x1f
36#define RE_SH 6
37#define IMM_MASK 0xffff
38#define IMM_SH 0
39#define JIMM_MASK 0x3ffffff
40#define JIMM_SH 0
41#define FUNC_MASK 0x3f
42#define FUNC_SH 0
43#define SET_MASK 0x7
44#define SET_SH 0
Leonid Yegoshin51eec48e2014-11-18 16:24:15 +000045#define SIMM9_SH 7
46#define SIMM9_MASK 0x1ff
Thiemo Seufere30ec452008-01-28 20:05:38 +000047
48enum opcode {
Steven J. Hill71a1c772012-06-19 19:59:29 +010049 insn_addiu, insn_addu, insn_and, insn_andi, insn_bbit0, insn_bbit1,
David Daneydc190122017-06-13 15:28:45 -070050 insn_beq, insn_beql, insn_bgez, insn_bgezl, insn_bgtz, insn_blez,
51 insn_bltz, insn_bltzl, insn_bne, insn_break, insn_cache, insn_cfc1,
52 insn_cfcmsa, insn_ctc1, insn_ctcmsa, insn_daddiu, insn_daddu, insn_ddivu,
Hassan Naveed0d1d17b2019-03-12 22:47:56 +000053 insn_ddivu_r6, insn_di, insn_dins, insn_dinsm, insn_dinsu, insn_divu,
54 insn_divu_r6, insn_dmfc0, insn_dmodu, insn_dmtc0, insn_dmultu,
55 insn_dmulu, insn_drotr, insn_drotr32, insn_dsbh, insn_dshd, insn_dsll,
56 insn_dsll32, insn_dsllv, insn_dsra, insn_dsra32, insn_dsrav, insn_dsrl,
57 insn_dsrl32, insn_dsrlv, insn_dsubu, insn_eret, insn_ext, insn_ins,
58 insn_j, insn_jal, insn_jalr, insn_jr, insn_lb, insn_lbu, insn_ld,
59 insn_lddir, insn_ldpte, insn_ldx, insn_lh, insn_lhu, insn_ll, insn_lld,
60 insn_lui, insn_lw, insn_lwu, insn_lwx, insn_mfc0, insn_mfhc0, insn_mfhi,
61 insn_mflo, insn_modu, insn_movn, insn_movz, insn_mtc0, insn_mthc0,
62 insn_mthi, insn_mtlo, insn_mul, insn_multu, insn_mulu, insn_nor,
63 insn_or, insn_ori, insn_pref, insn_rfe, insn_rotr, insn_sb, insn_sc,
64 insn_scd, insn_seleqz, insn_selnez, insn_sd, insn_sh, insn_sll,
65 insn_sllv, insn_slt, insn_slti, insn_sltiu, insn_sltu, insn_sra,
66 insn_srav, insn_srl, insn_srlv, insn_subu, insn_sw, insn_sync,
67 insn_syscall, insn_tlbp, insn_tlbr, insn_tlbwi, insn_tlbwr, insn_wait,
68 insn_wsbh, insn_xor, insn_xori, insn_yield,
David Daneyce807d52017-06-13 15:28:43 -070069 insn_invalid /* insn_invalid must be last */
Thiemo Seufere30ec452008-01-28 20:05:38 +000070};
71
72struct insn {
Thiemo Seufere30ec452008-01-28 20:05:38 +000073 u32 match;
74 enum fields fields;
75};
76
Paul Gortmaker078a55f2013-06-18 13:38:59 +000077static inline u32 build_rs(u32 arg)
Thiemo Seufere30ec452008-01-28 20:05:38 +000078{
David Daney8d662c8d2010-12-27 18:18:29 -080079 WARN(arg & ~RS_MASK, KERN_WARNING "Micro-assembler field overflow\n");
Thiemo Seufere30ec452008-01-28 20:05:38 +000080
81 return (arg & RS_MASK) << RS_SH;
82}
83
Paul Gortmaker078a55f2013-06-18 13:38:59 +000084static inline u32 build_rt(u32 arg)
Thiemo Seufere30ec452008-01-28 20:05:38 +000085{
David Daney8d662c8d2010-12-27 18:18:29 -080086 WARN(arg & ~RT_MASK, KERN_WARNING "Micro-assembler field overflow\n");
Thiemo Seufere30ec452008-01-28 20:05:38 +000087
88 return (arg & RT_MASK) << RT_SH;
89}
90
Paul Gortmaker078a55f2013-06-18 13:38:59 +000091static inline u32 build_rd(u32 arg)
Thiemo Seufere30ec452008-01-28 20:05:38 +000092{
David Daney8d662c8d2010-12-27 18:18:29 -080093 WARN(arg & ~RD_MASK, KERN_WARNING "Micro-assembler field overflow\n");
Thiemo Seufere30ec452008-01-28 20:05:38 +000094
95 return (arg & RD_MASK) << RD_SH;
96}
97
Paul Gortmaker078a55f2013-06-18 13:38:59 +000098static inline u32 build_re(u32 arg)
Thiemo Seufere30ec452008-01-28 20:05:38 +000099{
David Daney8d662c8d2010-12-27 18:18:29 -0800100 WARN(arg & ~RE_MASK, KERN_WARNING "Micro-assembler field overflow\n");
Thiemo Seufere30ec452008-01-28 20:05:38 +0000101
102 return (arg & RE_MASK) << RE_SH;
103}
104
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000105static inline u32 build_simm(s32 arg)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000106{
David Daney8d662c8d2010-12-27 18:18:29 -0800107 WARN(arg > 0x7fff || arg < -0x8000,
108 KERN_WARNING "Micro-assembler field overflow\n");
Thiemo Seufere30ec452008-01-28 20:05:38 +0000109
110 return arg & 0xffff;
111}
112
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000113static inline u32 build_uimm(u32 arg)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000114{
David Daney8d662c8d2010-12-27 18:18:29 -0800115 WARN(arg & ~IMM_MASK, KERN_WARNING "Micro-assembler field overflow\n");
Thiemo Seufere30ec452008-01-28 20:05:38 +0000116
117 return arg & IMM_MASK;
118}
119
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000120static inline u32 build_scimm(u32 arg)
David Daney58b9e222010-02-18 16:13:03 -0800121{
David Daney8d662c8d2010-12-27 18:18:29 -0800122 WARN(arg & ~SCIMM_MASK,
123 KERN_WARNING "Micro-assembler field overflow\n");
David Daney58b9e222010-02-18 16:13:03 -0800124
125 return (arg & SCIMM_MASK) << SCIMM_SH;
126}
127
Leonid Yegoshin51eec48e2014-11-18 16:24:15 +0000128static inline u32 build_scimm9(s32 arg)
129{
130 WARN((arg > 0xff || arg < -0x100),
131 KERN_WARNING "Micro-assembler field overflow\n");
132
133 return (arg & SIMM9_MASK) << SIMM9_SH;
134}
135
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000136static inline u32 build_func(u32 arg)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000137{
David Daney8d662c8d2010-12-27 18:18:29 -0800138 WARN(arg & ~FUNC_MASK, KERN_WARNING "Micro-assembler field overflow\n");
Thiemo Seufere30ec452008-01-28 20:05:38 +0000139
140 return arg & FUNC_MASK;
141}
142
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000143static inline u32 build_set(u32 arg)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000144{
David Daney8d662c8d2010-12-27 18:18:29 -0800145 WARN(arg & ~SET_MASK, KERN_WARNING "Micro-assembler field overflow\n");
Thiemo Seufere30ec452008-01-28 20:05:38 +0000146
147 return arg & SET_MASK;
148}
149
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000150static void build_insn(u32 **buf, enum opcode opc, ...);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000151
152#define I_u1u2u3(op) \
153Ip_u1u2u3(op) \
154{ \
155 build_insn(buf, insn##op, a, b, c); \
David Daney22b07632010-07-23 18:41:43 -0700156} \
157UASM_EXPORT_SYMBOL(uasm_i##op);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000158
Markos Chandras9d987362014-06-23 10:38:44 +0100159#define I_s3s1s2(op) \
160Ip_s3s1s2(op) \
161{ \
162 build_insn(buf, insn##op, b, c, a); \
163} \
164UASM_EXPORT_SYMBOL(uasm_i##op);
165
Thiemo Seufere30ec452008-01-28 20:05:38 +0000166#define I_u2u1u3(op) \
167Ip_u2u1u3(op) \
168{ \
169 build_insn(buf, insn##op, b, a, c); \
David Daney22b07632010-07-23 18:41:43 -0700170} \
171UASM_EXPORT_SYMBOL(uasm_i##op);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000172
Markos Chandrasbeef8e02014-04-08 12:47:02 +0100173#define I_u3u2u1(op) \
174Ip_u3u2u1(op) \
175{ \
176 build_insn(buf, insn##op, c, b, a); \
177} \
178UASM_EXPORT_SYMBOL(uasm_i##op);
179
Thiemo Seufere30ec452008-01-28 20:05:38 +0000180#define I_u3u1u2(op) \
181Ip_u3u1u2(op) \
182{ \
183 build_insn(buf, insn##op, b, c, a); \
David Daney22b07632010-07-23 18:41:43 -0700184} \
185UASM_EXPORT_SYMBOL(uasm_i##op);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000186
187#define I_u1u2s3(op) \
188Ip_u1u2s3(op) \
189{ \
190 build_insn(buf, insn##op, a, b, c); \
David Daney22b07632010-07-23 18:41:43 -0700191} \
192UASM_EXPORT_SYMBOL(uasm_i##op);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000193
194#define I_u2s3u1(op) \
195Ip_u2s3u1(op) \
196{ \
197 build_insn(buf, insn##op, c, a, b); \
David Daney22b07632010-07-23 18:41:43 -0700198} \
199UASM_EXPORT_SYMBOL(uasm_i##op);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000200
201#define I_u2u1s3(op) \
202Ip_u2u1s3(op) \
203{ \
204 build_insn(buf, insn##op, b, a, c); \
David Daney22b07632010-07-23 18:41:43 -0700205} \
206UASM_EXPORT_SYMBOL(uasm_i##op);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000207
David Daney92078e02009-10-14 12:16:55 -0700208#define I_u2u1msbu3(op) \
209Ip_u2u1msbu3(op) \
210{ \
211 build_insn(buf, insn##op, b, a, c+d-1, c); \
David Daney22b07632010-07-23 18:41:43 -0700212} \
213UASM_EXPORT_SYMBOL(uasm_i##op);
David Daney92078e02009-10-14 12:16:55 -0700214
David Daneyc42aef02010-12-21 14:19:10 -0800215#define I_u2u1msb32u3(op) \
216Ip_u2u1msbu3(op) \
217{ \
218 build_insn(buf, insn##op, b, a, c+d-33, c); \
219} \
220UASM_EXPORT_SYMBOL(uasm_i##op);
221
David Daneydc190122017-06-13 15:28:45 -0700222#define I_u2u1msb32msb3(op) \
223Ip_u2u1msbu3(op) \
224{ \
225 build_insn(buf, insn##op, b, a, c+d-33, c-32); \
226} \
227UASM_EXPORT_SYMBOL(uasm_i##op);
228
Ralf Baechle70342282013-01-22 12:59:30 +0100229#define I_u2u1msbdu3(op) \
Steven J. Hille6de1a02012-07-12 17:21:31 +0000230Ip_u2u1msbu3(op) \
231{ \
232 build_insn(buf, insn##op, b, a, d-1, c); \
233} \
234UASM_EXPORT_SYMBOL(uasm_i##op);
235
Thiemo Seufere30ec452008-01-28 20:05:38 +0000236#define I_u1u2(op) \
237Ip_u1u2(op) \
238{ \
239 build_insn(buf, insn##op, a, b); \
David Daney22b07632010-07-23 18:41:43 -0700240} \
241UASM_EXPORT_SYMBOL(uasm_i##op);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000242
Paul Burtond674dd12014-03-04 15:12:36 +0000243#define I_u2u1(op) \
244Ip_u1u2(op) \
245{ \
246 build_insn(buf, insn##op, b, a); \
247} \
248UASM_EXPORT_SYMBOL(uasm_i##op);
249
Thiemo Seufere30ec452008-01-28 20:05:38 +0000250#define I_u1s2(op) \
251Ip_u1s2(op) \
252{ \
253 build_insn(buf, insn##op, a, b); \
David Daney22b07632010-07-23 18:41:43 -0700254} \
255UASM_EXPORT_SYMBOL(uasm_i##op);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000256
257#define I_u1(op) \
258Ip_u1(op) \
259{ \
260 build_insn(buf, insn##op, a); \
David Daney22b07632010-07-23 18:41:43 -0700261} \
262UASM_EXPORT_SYMBOL(uasm_i##op);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000263
264#define I_0(op) \
265Ip_0(op) \
266{ \
267 build_insn(buf, insn##op); \
David Daney22b07632010-07-23 18:41:43 -0700268} \
269UASM_EXPORT_SYMBOL(uasm_i##op);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000270
271I_u2u1s3(_addiu)
272I_u3u1u2(_addu)
273I_u2u1u3(_andi)
274I_u3u1u2(_and)
275I_u1u2s3(_beq)
276I_u1u2s3(_beql)
277I_u1s2(_bgez)
278I_u1s2(_bgezl)
David Daneydc190122017-06-13 15:28:45 -0700279I_u1s2(_bgtz)
280I_u1s2(_blez)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000281I_u1s2(_bltz)
282I_u1s2(_bltzl)
283I_u1u2s3(_bne)
David Daneydc190122017-06-13 15:28:45 -0700284I_u1(_break)
Thiemo Seuferfb2a27e72008-02-18 19:32:49 +0000285I_u2s3u1(_cache)
James Hoganc29732a2016-06-23 17:34:34 +0100286I_u1u2(_cfc1)
James Hogan59e35592016-06-23 17:34:35 +0100287I_u2u1(_cfcmsa)
James Hoganc29732a2016-06-23 17:34:34 +0100288I_u1u2(_ctc1)
James Hogan59e35592016-06-23 17:34:35 +0100289I_u2u1(_ctcmsa)
David Daneydc190122017-06-13 15:28:45 -0700290I_u1u2(_ddivu)
Hassan Naveed0d1d17b2019-03-12 22:47:56 +0000291I_u3u1u2(_ddivu_r6)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000292I_u1u2u3(_dmfc0)
Hassan Naveed0d1d17b2019-03-12 22:47:56 +0000293I_u3u1u2(_dmodu)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000294I_u1u2u3(_dmtc0)
David Daneydc190122017-06-13 15:28:45 -0700295I_u1u2(_dmultu)
Hassan Naveed0d1d17b2019-03-12 22:47:56 +0000296I_u3u1u2(_dmulu)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000297I_u2u1s3(_daddiu)
298I_u3u1u2(_daddu)
James Hogan61c64cf2016-06-23 17:34:36 +0100299I_u1(_di);
Markos Chandras4c12a852014-04-08 12:47:06 +0100300I_u1u2(_divu)
Hassan Naveed0d1d17b2019-03-12 22:47:56 +0000301I_u3u1u2(_divu_r6)
David Daneydc190122017-06-13 15:28:45 -0700302I_u2u1(_dsbh);
303I_u2u1(_dshd);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000304I_u2u1u3(_dsll)
305I_u2u1u3(_dsll32)
David Daneydc190122017-06-13 15:28:45 -0700306I_u3u2u1(_dsllv)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000307I_u2u1u3(_dsra)
David Daneydc190122017-06-13 15:28:45 -0700308I_u2u1u3(_dsra32)
309I_u3u2u1(_dsrav)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000310I_u2u1u3(_dsrl)
311I_u2u1u3(_dsrl32)
David Daneydc190122017-06-13 15:28:45 -0700312I_u3u2u1(_dsrlv)
David Daney92078e02009-10-14 12:16:55 -0700313I_u2u1u3(_drotr)
David Daneyde6d5b552010-07-23 18:41:41 -0700314I_u2u1u3(_drotr32)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000315I_u3u1u2(_dsubu)
316I_0(_eret)
Steven J. Hille6de1a02012-07-12 17:21:31 +0000317I_u2u1msbdu3(_ext)
318I_u2u1msbu3(_ins)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000319I_u1(_j)
320I_u1(_jal)
Paul Burton49e9529b2014-03-16 12:58:05 +0000321I_u2u1(_jalr)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000322I_u1(_jr)
Markos Chandras82488812014-04-16 13:49:57 +0100323I_u2s3u1(_lb)
David Daneydc190122017-06-13 15:28:45 -0700324I_u2s3u1(_lbu)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000325I_u2s3u1(_ld)
Markos Chandrasd6b33142014-04-08 12:47:12 +0100326I_u2s3u1(_lh)
David Daneybfbfa9d2017-03-14 14:21:40 -0700327I_u2s3u1(_lhu)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000328I_u2s3u1(_ll)
329I_u2s3u1(_lld)
330I_u1s2(_lui)
331I_u2s3u1(_lw)
David Daneydc190122017-06-13 15:28:45 -0700332I_u2s3u1(_lwu)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000333I_u1u2u3(_mfc0)
Steven J. Hille2965cd2014-11-13 09:52:02 -0600334I_u1u2u3(_mfhc0)
Hassan Naveed0d1d17b2019-03-12 22:47:56 +0000335I_u3u1u2(_modu)
David Daneydc190122017-06-13 15:28:45 -0700336I_u3u1u2(_movn)
337I_u3u1u2(_movz)
Markos Chandrasf3ec7a22014-04-08 12:47:07 +0100338I_u1(_mfhi)
Markos Chandras16d21a82014-04-14 15:42:31 +0100339I_u1(_mflo)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000340I_u1u2u3(_mtc0)
Steven J. Hille2965cd2014-11-13 09:52:02 -0600341I_u1u2u3(_mthc0)
James Hogan9f730a62016-06-23 17:34:37 +0100342I_u1(_mthi)
343I_u1(_mtlo)
Markos Chandrasa8e897a2014-04-08 12:47:13 +0100344I_u3u1u2(_mul)
David Daneydc190122017-06-13 15:28:45 -0700345I_u1u2(_multu)
Hassan Naveed0d1d17b2019-03-12 22:47:56 +0000346I_u3u1u2(_mulu)
David Daneydc190122017-06-13 15:28:45 -0700347I_u3u1u2(_nor)
Ralf Baechle58081842010-03-23 15:54:50 +0100348I_u3u1u2(_or)
David Daneydc190122017-06-13 15:28:45 -0700349I_u2u1u3(_ori)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000350I_0(_rfe)
David Daneydc190122017-06-13 15:28:45 -0700351I_u2s3u1(_sb)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000352I_u2s3u1(_sc)
353I_u2s3u1(_scd)
354I_u2s3u1(_sd)
Hassan Naveed0d1d17b2019-03-12 22:47:56 +0000355I_u3u1u2(_seleqz)
356I_u3u1u2(_selnez)
David Daneydc190122017-06-13 15:28:45 -0700357I_u2s3u1(_sh)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000358I_u2u1u3(_sll)
Markos Chandrasbef581b2014-04-08 12:47:04 +0100359I_u3u2u1(_sllv)
Markos Chandras7682f9e2014-06-23 10:38:45 +0100360I_s3s1s2(_slt)
David Daneydc190122017-06-13 15:28:45 -0700361I_u2u1s3(_slti)
Markos Chandras390363e2014-04-08 12:47:09 +0100362I_u2u1s3(_sltiu)
Markos Chandrase8ef8682014-04-08 12:47:10 +0100363I_u3u1u2(_sltu)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000364I_u2u1u3(_sra)
Jiong Wangee94b902018-12-05 13:52:30 -0500365I_u3u2u1(_srav)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000366I_u2u1u3(_srl)
Markos Chandrasf31318f2014-04-08 12:47:05 +0100367I_u3u2u1(_srlv)
David Daney32546f32010-02-10 15:12:46 -0800368I_u2u1u3(_rotr)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000369I_u3u1u2(_subu)
370I_u2s3u1(_sw)
Paul Burton729ff562013-12-24 03:49:45 +0000371I_u1(_sync)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000372I_0(_tlbp)
David Daney32546f32010-02-10 15:12:46 -0800373I_0(_tlbr)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000374I_0(_tlbwi)
375I_0(_tlbwr)
Paul Burton53ed1382013-12-24 03:50:35 +0000376I_u1(_wait);
Markos Chandrasab9e4fa2014-04-08 12:47:11 +0100377I_u2u1(_wsbh)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000378I_u3u1u2(_xor)
379I_u2u1u3(_xori)
Paul Burtond674dd12014-03-04 15:12:36 +0000380I_u2u1(_yield)
David Daney92078e02009-10-14 12:16:55 -0700381I_u2u1msbu3(_dins);
David Daneyc42aef02010-12-21 14:19:10 -0800382I_u2u1msb32u3(_dinsm);
David Daneydc190122017-06-13 15:28:45 -0700383I_u2u1msb32msb3(_dinsu);
David Daney58b9e222010-02-18 16:13:03 -0800384I_u1(_syscall);
David Daney5b97c3f2010-07-23 18:41:42 -0700385I_u1u2s3(_bbit0);
386I_u1u2s3(_bbit1);
David Daneybb3d68c2010-12-27 18:07:56 -0800387I_u3u1u2(_lwx)
388I_u3u1u2(_ldx)
Huacai Chen380cd582016-03-03 09:45:12 +0800389I_u1u2(_ldpte)
390I_u2u1u3(_lddir)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000391
David Daneyc9941152010-10-07 16:03:53 -0700392#ifdef CONFIG_CPU_CAVIUM_OCTEON
393#include <asm/octeon/octeon.h>
Paul Burton33679a52017-03-30 14:52:15 -0700394void uasm_i_pref(u32 **buf, unsigned int a, signed int b,
David Daneyc9941152010-10-07 16:03:53 -0700395 unsigned int c)
396{
David Daneye3d0ead2015-01-15 16:11:13 +0300397 if (CAVIUM_OCTEON_DCACHE_PREFETCH_WAR && a <= 24 && a != 5)
David Daneyc9941152010-10-07 16:03:53 -0700398 /*
399 * As per erratum Core-14449, replace prefetches 0-4,
400 * 6-24 with 'pref 28'.
401 */
402 build_insn(buf, insn_pref, c, 28, b);
403 else
404 build_insn(buf, insn_pref, c, a, b);
405}
Paul Burton33679a52017-03-30 14:52:15 -0700406UASM_EXPORT_SYMBOL(uasm_i_pref);
David Daneyc9941152010-10-07 16:03:53 -0700407#else
408I_u2s3u1(_pref)
409#endif
410
Thiemo Seufere30ec452008-01-28 20:05:38 +0000411/* Handle labels. */
Paul Burton33679a52017-03-30 14:52:15 -0700412void uasm_build_label(struct uasm_label **lab, u32 *addr, int lid)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000413{
414 (*lab)->addr = addr;
415 (*lab)->lab = lid;
416 (*lab)++;
417}
Paul Burton33679a52017-03-30 14:52:15 -0700418UASM_EXPORT_SYMBOL(uasm_build_label);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000419
Paul Burton33679a52017-03-30 14:52:15 -0700420int uasm_in_compat_space_p(long addr)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000421{
422 /* Is this address in 32bit compat space? */
James Hoganf7d9afe2016-07-08 14:05:26 +0100423 return addr == (int)addr;
Thiemo Seufere30ec452008-01-28 20:05:38 +0000424}
Paul Burton33679a52017-03-30 14:52:15 -0700425UASM_EXPORT_SYMBOL(uasm_in_compat_space_p);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000426
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000427static int uasm_rel_highest(long val)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000428{
429#ifdef CONFIG_64BIT
430 return ((((val + 0x800080008000L) >> 48) & 0xffff) ^ 0x8000) - 0x8000;
431#else
432 return 0;
433#endif
434}
435
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000436static int uasm_rel_higher(long val)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000437{
438#ifdef CONFIG_64BIT
439 return ((((val + 0x80008000L) >> 32) & 0xffff) ^ 0x8000) - 0x8000;
440#else
441 return 0;
442#endif
443}
444
Paul Burton33679a52017-03-30 14:52:15 -0700445int uasm_rel_hi(long val)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000446{
447 return ((((val + 0x8000L) >> 16) & 0xffff) ^ 0x8000) - 0x8000;
448}
Paul Burton33679a52017-03-30 14:52:15 -0700449UASM_EXPORT_SYMBOL(uasm_rel_hi);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000450
Paul Burton33679a52017-03-30 14:52:15 -0700451int uasm_rel_lo(long val)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000452{
453 return ((val & 0xffff) ^ 0x8000) - 0x8000;
454}
Paul Burton33679a52017-03-30 14:52:15 -0700455UASM_EXPORT_SYMBOL(uasm_rel_lo);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000456
Paul Burton33679a52017-03-30 14:52:15 -0700457void UASM_i_LA_mostly(u32 **buf, unsigned int rs, long addr)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000458{
Paul Burton33679a52017-03-30 14:52:15 -0700459 if (!uasm_in_compat_space_p(addr)) {
460 uasm_i_lui(buf, rs, uasm_rel_highest(addr));
Thiemo Seufere30ec452008-01-28 20:05:38 +0000461 if (uasm_rel_higher(addr))
Paul Burton33679a52017-03-30 14:52:15 -0700462 uasm_i_daddiu(buf, rs, rs, uasm_rel_higher(addr));
463 if (uasm_rel_hi(addr)) {
464 uasm_i_dsll(buf, rs, rs, 16);
465 uasm_i_daddiu(buf, rs, rs,
466 uasm_rel_hi(addr));
467 uasm_i_dsll(buf, rs, rs, 16);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000468 } else
Paul Burton33679a52017-03-30 14:52:15 -0700469 uasm_i_dsll32(buf, rs, rs, 0);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000470 } else
Paul Burton33679a52017-03-30 14:52:15 -0700471 uasm_i_lui(buf, rs, uasm_rel_hi(addr));
Thiemo Seufere30ec452008-01-28 20:05:38 +0000472}
Paul Burton33679a52017-03-30 14:52:15 -0700473UASM_EXPORT_SYMBOL(UASM_i_LA_mostly);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000474
Paul Burton33679a52017-03-30 14:52:15 -0700475void UASM_i_LA(u32 **buf, unsigned int rs, long addr)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000476{
Paul Burton33679a52017-03-30 14:52:15 -0700477 UASM_i_LA_mostly(buf, rs, addr);
478 if (uasm_rel_lo(addr)) {
479 if (!uasm_in_compat_space_p(addr))
480 uasm_i_daddiu(buf, rs, rs,
481 uasm_rel_lo(addr));
Thiemo Seufere30ec452008-01-28 20:05:38 +0000482 else
Paul Burton33679a52017-03-30 14:52:15 -0700483 uasm_i_addiu(buf, rs, rs,
484 uasm_rel_lo(addr));
Thiemo Seufere30ec452008-01-28 20:05:38 +0000485 }
486}
Paul Burton33679a52017-03-30 14:52:15 -0700487UASM_EXPORT_SYMBOL(UASM_i_LA);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000488
489/* Handle relocations. */
Paul Burton33679a52017-03-30 14:52:15 -0700490void uasm_r_mips_pc16(struct uasm_reloc **rel, u32 *addr, int lid)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000491{
492 (*rel)->addr = addr;
493 (*rel)->type = R_MIPS_PC16;
494 (*rel)->lab = lid;
495 (*rel)++;
496}
Paul Burton33679a52017-03-30 14:52:15 -0700497UASM_EXPORT_SYMBOL(uasm_r_mips_pc16);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000498
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000499static inline void __resolve_relocs(struct uasm_reloc *rel,
500 struct uasm_label *lab);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000501
Paul Burton33679a52017-03-30 14:52:15 -0700502void uasm_resolve_relocs(struct uasm_reloc *rel,
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000503 struct uasm_label *lab)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000504{
505 struct uasm_label *l;
506
507 for (; rel->lab != UASM_LABEL_INVALID; rel++)
508 for (l = lab; l->lab != UASM_LABEL_INVALID; l++)
509 if (rel->lab == l->lab)
510 __resolve_relocs(rel, l);
511}
Paul Burton33679a52017-03-30 14:52:15 -0700512UASM_EXPORT_SYMBOL(uasm_resolve_relocs);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000513
Paul Burton33679a52017-03-30 14:52:15 -0700514void uasm_move_relocs(struct uasm_reloc *rel, u32 *first, u32 *end,
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000515 long off)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000516{
517 for (; rel->lab != UASM_LABEL_INVALID; rel++)
518 if (rel->addr >= first && rel->addr < end)
519 rel->addr += off;
520}
Paul Burton33679a52017-03-30 14:52:15 -0700521UASM_EXPORT_SYMBOL(uasm_move_relocs);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000522
Paul Burton33679a52017-03-30 14:52:15 -0700523void uasm_move_labels(struct uasm_label *lab, u32 *first, u32 *end,
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000524 long off)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000525{
526 for (; lab->lab != UASM_LABEL_INVALID; lab++)
527 if (lab->addr >= first && lab->addr < end)
528 lab->addr += off;
529}
Paul Burton33679a52017-03-30 14:52:15 -0700530UASM_EXPORT_SYMBOL(uasm_move_labels);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000531
Paul Burton33679a52017-03-30 14:52:15 -0700532void uasm_copy_handler(struct uasm_reloc *rel, struct uasm_label *lab,
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000533 u32 *first, u32 *end, u32 *target)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000534{
535 long off = (long)(target - first);
536
537 memcpy(target, first, (end - first) * sizeof(u32));
538
Paul Burton33679a52017-03-30 14:52:15 -0700539 uasm_move_relocs(rel, first, end, off);
540 uasm_move_labels(lab, first, end, off);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000541}
Paul Burton33679a52017-03-30 14:52:15 -0700542UASM_EXPORT_SYMBOL(uasm_copy_handler);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000543
Paul Burton33679a52017-03-30 14:52:15 -0700544int uasm_insn_has_bdelay(struct uasm_reloc *rel, u32 *addr)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000545{
546 for (; rel->lab != UASM_LABEL_INVALID; rel++) {
547 if (rel->addr == addr
548 && (rel->type == R_MIPS_PC16
549 || rel->type == R_MIPS_26))
550 return 1;
551 }
552
553 return 0;
554}
Paul Burton33679a52017-03-30 14:52:15 -0700555UASM_EXPORT_SYMBOL(uasm_insn_has_bdelay);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000556
557/* Convenience functions for labeled branches. */
Paul Burton33679a52017-03-30 14:52:15 -0700558void uasm_il_bltz(u32 **p, struct uasm_reloc **r, unsigned int reg,
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000559 int lid)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000560{
561 uasm_r_mips_pc16(r, *p, lid);
Paul Burton33679a52017-03-30 14:52:15 -0700562 uasm_i_bltz(p, reg, 0);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000563}
Paul Burton33679a52017-03-30 14:52:15 -0700564UASM_EXPORT_SYMBOL(uasm_il_bltz);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000565
Paul Burton33679a52017-03-30 14:52:15 -0700566void uasm_il_b(u32 **p, struct uasm_reloc **r, int lid)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000567{
568 uasm_r_mips_pc16(r, *p, lid);
Paul Burton33679a52017-03-30 14:52:15 -0700569 uasm_i_b(p, 0);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000570}
Paul Burton33679a52017-03-30 14:52:15 -0700571UASM_EXPORT_SYMBOL(uasm_il_b);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000572
Paul Burton33679a52017-03-30 14:52:15 -0700573void uasm_il_beq(u32 **p, struct uasm_reloc **r, unsigned int r1,
Paul Burton8dee5902013-12-24 03:51:39 +0000574 unsigned int r2, int lid)
575{
576 uasm_r_mips_pc16(r, *p, lid);
Paul Burton33679a52017-03-30 14:52:15 -0700577 uasm_i_beq(p, r1, r2, 0);
Paul Burton8dee5902013-12-24 03:51:39 +0000578}
Paul Burton33679a52017-03-30 14:52:15 -0700579UASM_EXPORT_SYMBOL(uasm_il_beq);
Paul Burton8dee5902013-12-24 03:51:39 +0000580
Paul Burton33679a52017-03-30 14:52:15 -0700581void uasm_il_beqz(u32 **p, struct uasm_reloc **r, unsigned int reg,
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000582 int lid)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000583{
584 uasm_r_mips_pc16(r, *p, lid);
Paul Burton33679a52017-03-30 14:52:15 -0700585 uasm_i_beqz(p, reg, 0);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000586}
Paul Burton33679a52017-03-30 14:52:15 -0700587UASM_EXPORT_SYMBOL(uasm_il_beqz);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000588
Paul Burton33679a52017-03-30 14:52:15 -0700589void uasm_il_beqzl(u32 **p, struct uasm_reloc **r, unsigned int reg,
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000590 int lid)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000591{
592 uasm_r_mips_pc16(r, *p, lid);
Paul Burton33679a52017-03-30 14:52:15 -0700593 uasm_i_beqzl(p, reg, 0);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000594}
Paul Burton33679a52017-03-30 14:52:15 -0700595UASM_EXPORT_SYMBOL(uasm_il_beqzl);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000596
Paul Burton33679a52017-03-30 14:52:15 -0700597void uasm_il_bne(u32 **p, struct uasm_reloc **r, unsigned int reg1,
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000598 unsigned int reg2, int lid)
Thiemo Seuferfb2a27e72008-02-18 19:32:49 +0000599{
600 uasm_r_mips_pc16(r, *p, lid);
Paul Burton33679a52017-03-30 14:52:15 -0700601 uasm_i_bne(p, reg1, reg2, 0);
Thiemo Seuferfb2a27e72008-02-18 19:32:49 +0000602}
Paul Burton33679a52017-03-30 14:52:15 -0700603UASM_EXPORT_SYMBOL(uasm_il_bne);
Thiemo Seuferfb2a27e72008-02-18 19:32:49 +0000604
Paul Burton33679a52017-03-30 14:52:15 -0700605void uasm_il_bnez(u32 **p, struct uasm_reloc **r, unsigned int reg,
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000606 int lid)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000607{
608 uasm_r_mips_pc16(r, *p, lid);
Paul Burton33679a52017-03-30 14:52:15 -0700609 uasm_i_bnez(p, reg, 0);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000610}
Paul Burton33679a52017-03-30 14:52:15 -0700611UASM_EXPORT_SYMBOL(uasm_il_bnez);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000612
Paul Burton33679a52017-03-30 14:52:15 -0700613void uasm_il_bgezl(u32 **p, struct uasm_reloc **r, unsigned int reg,
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000614 int lid)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000615{
616 uasm_r_mips_pc16(r, *p, lid);
Paul Burton33679a52017-03-30 14:52:15 -0700617 uasm_i_bgezl(p, reg, 0);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000618}
Paul Burton33679a52017-03-30 14:52:15 -0700619UASM_EXPORT_SYMBOL(uasm_il_bgezl);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000620
Paul Burton33679a52017-03-30 14:52:15 -0700621void uasm_il_bgez(u32 **p, struct uasm_reloc **r, unsigned int reg,
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000622 int lid)
Thiemo Seufere30ec452008-01-28 20:05:38 +0000623{
624 uasm_r_mips_pc16(r, *p, lid);
Paul Burton33679a52017-03-30 14:52:15 -0700625 uasm_i_bgez(p, reg, 0);
Thiemo Seufere30ec452008-01-28 20:05:38 +0000626}
Paul Burton33679a52017-03-30 14:52:15 -0700627UASM_EXPORT_SYMBOL(uasm_il_bgez);
David Daney5b97c3f2010-07-23 18:41:42 -0700628
Paul Burton33679a52017-03-30 14:52:15 -0700629void uasm_il_bbit0(u32 **p, struct uasm_reloc **r, unsigned int reg,
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000630 unsigned int bit, int lid)
David Daney5b97c3f2010-07-23 18:41:42 -0700631{
632 uasm_r_mips_pc16(r, *p, lid);
Paul Burton33679a52017-03-30 14:52:15 -0700633 uasm_i_bbit0(p, reg, bit, 0);
David Daney5b97c3f2010-07-23 18:41:42 -0700634}
Paul Burton33679a52017-03-30 14:52:15 -0700635UASM_EXPORT_SYMBOL(uasm_il_bbit0);
David Daney5b97c3f2010-07-23 18:41:42 -0700636
Paul Burton33679a52017-03-30 14:52:15 -0700637void uasm_il_bbit1(u32 **p, struct uasm_reloc **r, unsigned int reg,
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000638 unsigned int bit, int lid)
David Daney5b97c3f2010-07-23 18:41:42 -0700639{
640 uasm_r_mips_pc16(r, *p, lid);
Paul Burton33679a52017-03-30 14:52:15 -0700641 uasm_i_bbit1(p, reg, bit, 0);
David Daney5b97c3f2010-07-23 18:41:42 -0700642}
Paul Burton33679a52017-03-30 14:52:15 -0700643UASM_EXPORT_SYMBOL(uasm_il_bbit1);