Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 #include <linux/bug.h>
0003 #include <linux/kernel.h>
0004 #include <asm/opcodes.h>
0005 
0006 static unsigned long __arm_gen_branch_thumb2(unsigned long pc,
0007                          unsigned long addr, bool link,
0008                          bool warn)
0009 {
0010     unsigned long s, j1, j2, i1, i2, imm10, imm11;
0011     unsigned long first, second;
0012     long offset;
0013 
0014     offset = (long)addr - (long)(pc + 4);
0015     if (offset < -16777216 || offset > 16777214) {
0016         WARN_ON_ONCE(warn);
0017         return 0;
0018     }
0019 
0020     s   = (offset >> 24) & 0x1;
0021     i1  = (offset >> 23) & 0x1;
0022     i2  = (offset >> 22) & 0x1;
0023     imm10   = (offset >> 12) & 0x3ff;
0024     imm11   = (offset >>  1) & 0x7ff;
0025 
0026     j1 = (!i1) ^ s;
0027     j2 = (!i2) ^ s;
0028 
0029     first = 0xf000 | (s << 10) | imm10;
0030     second = 0x9000 | (j1 << 13) | (j2 << 11) | imm11;
0031     if (link)
0032         second |= 1 << 14;
0033 
0034     return __opcode_thumb32_compose(first, second);
0035 }
0036 
0037 static unsigned long __arm_gen_branch_arm(unsigned long pc, unsigned long addr,
0038                       bool link, bool warn)
0039 {
0040     unsigned long opcode = 0xea000000;
0041     long offset;
0042 
0043     if (link)
0044         opcode |= 1 << 24;
0045 
0046     offset = (long)addr - (long)(pc + 8);
0047     if (unlikely(offset < -33554432 || offset > 33554428)) {
0048         WARN_ON_ONCE(warn);
0049         return 0;
0050     }
0051 
0052     offset = (offset >> 2) & 0x00ffffff;
0053 
0054     return opcode | offset;
0055 }
0056 
0057 unsigned long
0058 __arm_gen_branch(unsigned long pc, unsigned long addr, bool link, bool warn)
0059 {
0060     if (IS_ENABLED(CONFIG_THUMB2_KERNEL))
0061         return __arm_gen_branch_thumb2(pc, addr, link, warn);
0062     else
0063         return __arm_gen_branch_arm(pc, addr, link, warn);
0064 }