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
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.
10 * Copyright (C) 2004, 2005, 2006, 2008 Thiemo Seufer
11 * Copyright (C) 2005, 2007 Maciej W. Rozycki
12 * Copyright (C) 2006 Ralf Baechle (ralf@linux-mips.org)
13 * Copyright (C) 2012, 2013 MIPS Technologies, Inc. All rights reserved.
37 #define IMM_MASK 0xffff
39 #define JIMM_MASK 0x3ffffff
41 #define FUNC_MASK 0x3f
46 #define SIMM9_MASK 0x1ff
49 insn_addiu
, insn_addu
, insn_and
, insn_andi
, insn_bbit0
, insn_bbit1
,
50 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
,
53 insn_di
, insn_dins
, insn_dinsm
, insn_dinsu
, insn_divu
, insn_dmfc0
,
54 insn_dmtc0
, insn_dmultu
, insn_drotr
, insn_drotr32
, insn_dsbh
, insn_dshd
,
55 insn_dsll
, insn_dsll32
, insn_dsllv
, insn_dsra
, insn_dsra32
, insn_dsrav
,
56 insn_dsrl
, insn_dsrl32
, insn_dsrlv
, insn_dsubu
, insn_eret
, insn_ext
,
57 insn_ins
, insn_j
, insn_jal
, insn_jalr
, insn_jr
, insn_lb
, insn_lbu
,
58 insn_ld
, insn_lddir
, insn_ldpte
, insn_ldx
, insn_lh
, insn_lhu
,
59 insn_ll
, insn_lld
, insn_lui
, insn_lw
, insn_lwu
, insn_lwx
, insn_mfc0
,
60 insn_mfhc0
, insn_mfhi
, insn_mflo
, insn_movn
, insn_movz
, insn_mtc0
,
61 insn_mthc0
, insn_mthi
, insn_mtlo
, insn_mul
, insn_multu
, insn_nor
,
62 insn_or
, insn_ori
, insn_pref
, insn_rfe
, insn_rotr
, insn_sb
,
63 insn_sc
, insn_scd
, insn_sd
, insn_sh
, insn_sll
, insn_sllv
,
64 insn_slt
, insn_slti
, insn_sltiu
, insn_sltu
, insn_sra
, insn_srl
,
65 insn_srlv
, insn_subu
, insn_sw
, insn_sync
, insn_syscall
, insn_tlbp
,
66 insn_tlbr
, insn_tlbwi
, insn_tlbwr
, insn_wait
, insn_wsbh
, insn_xor
,
67 insn_xori
, insn_yield
,
68 insn_invalid
/* insn_invalid must be last */
76 static inline u32
build_rs(u32 arg
)
78 WARN(arg
& ~RS_MASK
, KERN_WARNING
"Micro-assembler field overflow\n");
80 return (arg
& RS_MASK
) << RS_SH
;
83 static inline u32
build_rt(u32 arg
)
85 WARN(arg
& ~RT_MASK
, KERN_WARNING
"Micro-assembler field overflow\n");
87 return (arg
& RT_MASK
) << RT_SH
;
90 static inline u32
build_rd(u32 arg
)
92 WARN(arg
& ~RD_MASK
, KERN_WARNING
"Micro-assembler field overflow\n");
94 return (arg
& RD_MASK
) << RD_SH
;
97 static inline u32
build_re(u32 arg
)
99 WARN(arg
& ~RE_MASK
, KERN_WARNING
"Micro-assembler field overflow\n");
101 return (arg
& RE_MASK
) << RE_SH
;
104 static inline u32
build_simm(s32 arg
)
106 WARN(arg
> 0x7fff || arg
< -0x8000,
107 KERN_WARNING
"Micro-assembler field overflow\n");
112 static inline u32
build_uimm(u32 arg
)
114 WARN(arg
& ~IMM_MASK
, KERN_WARNING
"Micro-assembler field overflow\n");
116 return arg
& IMM_MASK
;
119 static inline u32
build_scimm(u32 arg
)
121 WARN(arg
& ~SCIMM_MASK
,
122 KERN_WARNING
"Micro-assembler field overflow\n");
124 return (arg
& SCIMM_MASK
) << SCIMM_SH
;
127 static inline u32
build_scimm9(s32 arg
)
129 WARN((arg
> 0xff || arg
< -0x100),
130 KERN_WARNING
"Micro-assembler field overflow\n");
132 return (arg
& SIMM9_MASK
) << SIMM9_SH
;
135 static inline u32
build_func(u32 arg
)
137 WARN(arg
& ~FUNC_MASK
, KERN_WARNING
"Micro-assembler field overflow\n");
139 return arg
& FUNC_MASK
;
142 static inline u32
build_set(u32 arg
)
144 WARN(arg
& ~SET_MASK
, KERN_WARNING
"Micro-assembler field overflow\n");
146 return arg
& SET_MASK
;
149 static void build_insn(u32
**buf
, enum opcode opc
, ...);
151 #define I_u1u2u3(op) \
154 build_insn(buf, insn##op, a, b, c); \
156 UASM_EXPORT_SYMBOL(uasm_i##op);
158 #define I_s3s1s2(op) \
161 build_insn(buf, insn##op, b, c, a); \
163 UASM_EXPORT_SYMBOL(uasm_i##op);
165 #define I_u2u1u3(op) \
168 build_insn(buf, insn##op, b, a, c); \
170 UASM_EXPORT_SYMBOL(uasm_i##op);
172 #define I_u3u2u1(op) \
175 build_insn(buf, insn##op, c, b, a); \
177 UASM_EXPORT_SYMBOL(uasm_i##op);
179 #define I_u3u1u2(op) \
182 build_insn(buf, insn##op, b, c, a); \
184 UASM_EXPORT_SYMBOL(uasm_i##op);
186 #define I_u1u2s3(op) \
189 build_insn(buf, insn##op, a, b, c); \
191 UASM_EXPORT_SYMBOL(uasm_i##op);
193 #define I_u2s3u1(op) \
196 build_insn(buf, insn##op, c, a, b); \
198 UASM_EXPORT_SYMBOL(uasm_i##op);
200 #define I_u2u1s3(op) \
203 build_insn(buf, insn##op, b, a, c); \
205 UASM_EXPORT_SYMBOL(uasm_i##op);
207 #define I_u2u1msbu3(op) \
210 build_insn(buf, insn##op, b, a, c+d-1, c); \
212 UASM_EXPORT_SYMBOL(uasm_i##op);
214 #define I_u2u1msb32u3(op) \
217 build_insn(buf, insn##op, b, a, c+d-33, c); \
219 UASM_EXPORT_SYMBOL(uasm_i##op);
221 #define I_u2u1msb32msb3(op) \
224 build_insn(buf, insn##op, b, a, c+d-33, c-32); \
226 UASM_EXPORT_SYMBOL(uasm_i##op);
228 #define I_u2u1msbdu3(op) \
231 build_insn(buf, insn##op, b, a, d-1, c); \
233 UASM_EXPORT_SYMBOL(uasm_i##op);
238 build_insn(buf, insn##op, a, b); \
240 UASM_EXPORT_SYMBOL(uasm_i##op);
245 build_insn(buf, insn##op, b, a); \
247 UASM_EXPORT_SYMBOL(uasm_i##op);
252 build_insn(buf, insn##op, a, b); \
254 UASM_EXPORT_SYMBOL(uasm_i##op);
259 build_insn(buf, insn##op, a); \
261 UASM_EXPORT_SYMBOL(uasm_i##op);
266 build_insn(buf, insn##op); \
268 UASM_EXPORT_SYMBOL(uasm_i##op);
372 I_u2u1msb32u3(_dinsm
);
373 I_u2u1msb32msb3(_dinsu
);
382 #ifdef CONFIG_CPU_CAVIUM_OCTEON
383 #include <asm/octeon/octeon.h>
384 void uasm_i_pref(u32
**buf
, unsigned int a
, signed int b
,
387 if (CAVIUM_OCTEON_DCACHE_PREFETCH_WAR
&& a
<= 24 && a
!= 5)
389 * As per erratum Core-14449, replace prefetches 0-4,
390 * 6-24 with 'pref 28'.
392 build_insn(buf
, insn_pref
, c
, 28, b
);
394 build_insn(buf
, insn_pref
, c
, a
, b
);
396 UASM_EXPORT_SYMBOL(uasm_i_pref
);
402 void uasm_build_label(struct uasm_label
**lab
, u32
*addr
, int lid
)
408 UASM_EXPORT_SYMBOL(uasm_build_label
);
410 int uasm_in_compat_space_p(long addr
)
412 /* Is this address in 32bit compat space? */
413 return addr
== (int)addr
;
415 UASM_EXPORT_SYMBOL(uasm_in_compat_space_p
);
417 static int uasm_rel_highest(long val
)
420 return ((((val
+ 0x800080008000L
) >> 48) & 0xffff) ^ 0x8000) - 0x8000;
426 static int uasm_rel_higher(long val
)
429 return ((((val
+ 0x80008000L
) >> 32) & 0xffff) ^ 0x8000) - 0x8000;
435 int uasm_rel_hi(long val
)
437 return ((((val
+ 0x8000L
) >> 16) & 0xffff) ^ 0x8000) - 0x8000;
439 UASM_EXPORT_SYMBOL(uasm_rel_hi
);
441 int uasm_rel_lo(long val
)
443 return ((val
& 0xffff) ^ 0x8000) - 0x8000;
445 UASM_EXPORT_SYMBOL(uasm_rel_lo
);
447 void UASM_i_LA_mostly(u32
**buf
, unsigned int rs
, long addr
)
449 if (!uasm_in_compat_space_p(addr
)) {
450 uasm_i_lui(buf
, rs
, uasm_rel_highest(addr
));
451 if (uasm_rel_higher(addr
))
452 uasm_i_daddiu(buf
, rs
, rs
, uasm_rel_higher(addr
));
453 if (uasm_rel_hi(addr
)) {
454 uasm_i_dsll(buf
, rs
, rs
, 16);
455 uasm_i_daddiu(buf
, rs
, rs
,
457 uasm_i_dsll(buf
, rs
, rs
, 16);
459 uasm_i_dsll32(buf
, rs
, rs
, 0);
461 uasm_i_lui(buf
, rs
, uasm_rel_hi(addr
));
463 UASM_EXPORT_SYMBOL(UASM_i_LA_mostly
);
465 void UASM_i_LA(u32
**buf
, unsigned int rs
, long addr
)
467 UASM_i_LA_mostly(buf
, rs
, addr
);
468 if (uasm_rel_lo(addr
)) {
469 if (!uasm_in_compat_space_p(addr
))
470 uasm_i_daddiu(buf
, rs
, rs
,
473 uasm_i_addiu(buf
, rs
, rs
,
477 UASM_EXPORT_SYMBOL(UASM_i_LA
);
479 /* Handle relocations. */
480 void uasm_r_mips_pc16(struct uasm_reloc
**rel
, u32
*addr
, int lid
)
483 (*rel
)->type
= R_MIPS_PC16
;
487 UASM_EXPORT_SYMBOL(uasm_r_mips_pc16
);
489 static inline void __resolve_relocs(struct uasm_reloc
*rel
,
490 struct uasm_label
*lab
);
492 void uasm_resolve_relocs(struct uasm_reloc
*rel
,
493 struct uasm_label
*lab
)
495 struct uasm_label
*l
;
497 for (; rel
->lab
!= UASM_LABEL_INVALID
; rel
++)
498 for (l
= lab
; l
->lab
!= UASM_LABEL_INVALID
; l
++)
499 if (rel
->lab
== l
->lab
)
500 __resolve_relocs(rel
, l
);
502 UASM_EXPORT_SYMBOL(uasm_resolve_relocs
);
504 void uasm_move_relocs(struct uasm_reloc
*rel
, u32
*first
, u32
*end
,
507 for (; rel
->lab
!= UASM_LABEL_INVALID
; rel
++)
508 if (rel
->addr
>= first
&& rel
->addr
< end
)
511 UASM_EXPORT_SYMBOL(uasm_move_relocs
);
513 void uasm_move_labels(struct uasm_label
*lab
, u32
*first
, u32
*end
,
516 for (; lab
->lab
!= UASM_LABEL_INVALID
; lab
++)
517 if (lab
->addr
>= first
&& lab
->addr
< end
)
520 UASM_EXPORT_SYMBOL(uasm_move_labels
);
522 void uasm_copy_handler(struct uasm_reloc
*rel
, struct uasm_label
*lab
,
523 u32
*first
, u32
*end
, u32
*target
)
525 long off
= (long)(target
- first
);
527 memcpy(target
, first
, (end
- first
) * sizeof(u32
));
529 uasm_move_relocs(rel
, first
, end
, off
);
530 uasm_move_labels(lab
, first
, end
, off
);
532 UASM_EXPORT_SYMBOL(uasm_copy_handler
);
534 int uasm_insn_has_bdelay(struct uasm_reloc
*rel
, u32
*addr
)
536 for (; rel
->lab
!= UASM_LABEL_INVALID
; rel
++) {
537 if (rel
->addr
== addr
538 && (rel
->type
== R_MIPS_PC16
539 || rel
->type
== R_MIPS_26
))
545 UASM_EXPORT_SYMBOL(uasm_insn_has_bdelay
);
547 /* Convenience functions for labeled branches. */
548 void uasm_il_bltz(u32
**p
, struct uasm_reloc
**r
, unsigned int reg
,
551 uasm_r_mips_pc16(r
, *p
, lid
);
552 uasm_i_bltz(p
, reg
, 0);
554 UASM_EXPORT_SYMBOL(uasm_il_bltz
);
556 void uasm_il_b(u32
**p
, struct uasm_reloc
**r
, int lid
)
558 uasm_r_mips_pc16(r
, *p
, lid
);
561 UASM_EXPORT_SYMBOL(uasm_il_b
);
563 void uasm_il_beq(u32
**p
, struct uasm_reloc
**r
, unsigned int r1
,
564 unsigned int r2
, int lid
)
566 uasm_r_mips_pc16(r
, *p
, lid
);
567 uasm_i_beq(p
, r1
, r2
, 0);
569 UASM_EXPORT_SYMBOL(uasm_il_beq
);
571 void uasm_il_beqz(u32
**p
, struct uasm_reloc
**r
, unsigned int reg
,
574 uasm_r_mips_pc16(r
, *p
, lid
);
575 uasm_i_beqz(p
, reg
, 0);
577 UASM_EXPORT_SYMBOL(uasm_il_beqz
);
579 void uasm_il_beqzl(u32
**p
, struct uasm_reloc
**r
, unsigned int reg
,
582 uasm_r_mips_pc16(r
, *p
, lid
);
583 uasm_i_beqzl(p
, reg
, 0);
585 UASM_EXPORT_SYMBOL(uasm_il_beqzl
);
587 void uasm_il_bne(u32
**p
, struct uasm_reloc
**r
, unsigned int reg1
,
588 unsigned int reg2
, int lid
)
590 uasm_r_mips_pc16(r
, *p
, lid
);
591 uasm_i_bne(p
, reg1
, reg2
, 0);
593 UASM_EXPORT_SYMBOL(uasm_il_bne
);
595 void uasm_il_bnez(u32
**p
, struct uasm_reloc
**r
, unsigned int reg
,
598 uasm_r_mips_pc16(r
, *p
, lid
);
599 uasm_i_bnez(p
, reg
, 0);
601 UASM_EXPORT_SYMBOL(uasm_il_bnez
);
603 void uasm_il_bgezl(u32
**p
, struct uasm_reloc
**r
, unsigned int reg
,
606 uasm_r_mips_pc16(r
, *p
, lid
);
607 uasm_i_bgezl(p
, reg
, 0);
609 UASM_EXPORT_SYMBOL(uasm_il_bgezl
);
611 void uasm_il_bgez(u32
**p
, struct uasm_reloc
**r
, unsigned int reg
,
614 uasm_r_mips_pc16(r
, *p
, lid
);
615 uasm_i_bgez(p
, reg
, 0);
617 UASM_EXPORT_SYMBOL(uasm_il_bgez
);
619 void uasm_il_bbit0(u32
**p
, struct uasm_reloc
**r
, unsigned int reg
,
620 unsigned int bit
, int lid
)
622 uasm_r_mips_pc16(r
, *p
, lid
);
623 uasm_i_bbit0(p
, reg
, bit
, 0);
625 UASM_EXPORT_SYMBOL(uasm_il_bbit0
);
627 void uasm_il_bbit1(u32
**p
, struct uasm_reloc
**r
, unsigned int reg
,
628 unsigned int bit
, int lid
)
630 uasm_r_mips_pc16(r
, *p
, lid
);
631 uasm_i_bbit1(p
, reg
, bit
, 0);
633 UASM_EXPORT_SYMBOL(uasm_il_bbit1
);