|
1 | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
2 | 2 | /* |
3 | 3 | * Copyright (C) 1994-2002 Russell King |
4 | | - * Copyright (c) 2003 ARM Limited |
| 4 | + * Copyright (c) 2003, 2020 ARM Limited |
5 | 5 | * All Rights Reserved |
6 | 6 | */ |
7 | 7 |
|
@@ -58,55 +58,140 @@ __fixup_a_pv_table: |
58 | 58 | mov r6, r6, lsr #24 |
59 | 59 | cmn r0, #1 |
60 | 60 | #ifdef CONFIG_THUMB2_KERNEL |
| 61 | + @ |
| 62 | + @ The Thumb-2 versions of the patchable sequences are |
| 63 | + @ |
| 64 | + @ phys-to-virt: movw <reg>, #offset<31:24> |
| 65 | + @ lsl <reg>, #24 |
| 66 | + @ sub <VA>, <PA>, <reg> |
| 67 | + @ |
| 68 | + @ virt-to-phys (non-LPAE): movw <reg>, #offset<31:24> |
| 69 | + @ lsl <reg>, #24 |
| 70 | + @ add <PA>, <VA>, <reg> |
| 71 | + @ |
| 72 | + @ virt-to-phys (LPAE): movw <reg>, #offset<31:24> |
| 73 | + @ lsl <reg>, #24 |
| 74 | + @ adds <PAlo>, <VA>, <reg> |
| 75 | + @ mov <PAhi>, #offset<39:32> |
| 76 | + @ adc <PAhi>, <PAhi>, #0 |
| 77 | + @ |
| 78 | + @ In the non-LPAE case, all patchable instructions are MOVW |
| 79 | + @ instructions, where we need to patch in the offset into the |
| 80 | + @ second halfword of the opcode (the 16-bit immediate is encoded |
| 81 | + @ as imm4:i:imm3:imm8) |
| 82 | + @ |
| 83 | + @ 15 11 10 9 4 3 0 15 14 12 11 8 7 0 |
| 84 | + @ +-----------+---+-------------+------++---+------+----+------+ |
| 85 | + @ MOVW | 1 1 1 1 0 | i | 1 0 0 1 0 0 | imm4 || 0 | imm3 | Rd | imm8 | |
| 86 | + @ +-----------+---+-------------+------++---+------+----+------+ |
| 87 | + @ |
| 88 | + @ In the LPAE case, we also need to patch in the high word of the |
| 89 | + @ offset into the immediate field of the MOV instruction, or patch it |
| 90 | + @ to a MVN instruction if the offset is negative. In this case, we |
| 91 | + @ need to inspect the first halfword of the opcode, to check whether |
| 92 | + @ it is MOVW or MOV/MVN, and to perform the MOV to MVN patching if |
| 93 | + @ needed. The encoding of the immediate is rather complex for values |
| 94 | + @ of i:imm3 != 0b0000, but fortunately, we never need more than 8 lower |
| 95 | + @ order bits, which can be patched into imm8 directly (and i:imm3 |
| 96 | + @ cleared) |
| 97 | + @ |
| 98 | + @ 15 11 10 9 5 0 15 14 12 11 8 7 0 |
| 99 | + @ +-----------+---+---------------------++---+------+----+------+ |
| 100 | + @ MOV | 1 1 1 1 0 | i | 0 0 0 1 0 0 1 1 1 1 || 0 | imm3 | Rd | imm8 | |
| 101 | + @ MVN | 1 1 1 1 0 | i | 0 0 0 1 1 0 1 1 1 1 || 0 | imm3 | Rd | imm8 | |
| 102 | + @ +-----------+---+---------------------++---+------+----+------+ |
| 103 | + @ |
61 | 104 | moveq r0, #0x200000 @ set bit 21, mov to mvn instruction |
62 | | - lsls r6, #24 |
63 | | - beq .Lnext |
64 | | - clz r7, r6 |
65 | | - lsr r6, #24 |
66 | | - lsl r6, r7 |
67 | | - bic r6, #0x0080 |
68 | | - lsrs r7, #1 |
69 | | - orrcs r6, #0x0080 |
70 | | - orr r6, r6, r7, lsl #12 |
71 | | - orr r6, #0x4000 |
72 | 105 | b .Lnext |
73 | 106 | .Lloop: add r7, r4 |
74 | | - adds r4, #4 |
75 | | - ldrh ip, [r7, #2] |
76 | | -ARM_BE8(rev16 ip, ip) |
77 | | - tst ip, #0x4000 |
78 | | - and ip, #0x8f00 |
79 | | - orrne ip, r6 @ mask in offset bits 31-24 |
80 | | - orreq ip, r0 @ mask in offset bits 7-0 |
81 | | -ARM_BE8(rev16 ip, ip) |
82 | | - strh ip, [r7, #2] |
83 | | - bne .Lnext |
| 107 | + adds r4, #4 @ clears Z flag |
| 108 | +#ifdef CONFIG_ARM_LPAE |
84 | 109 | ldrh ip, [r7] |
85 | 110 | ARM_BE8(rev16 ip, ip) |
86 | | - bic ip, #0x20 |
87 | | - orr ip, ip, r0, lsr #16 |
| 111 | + tst ip, #0x200 @ MOVW has bit 9 set, MVN has it clear |
| 112 | + bne 0f @ skip to MOVW handling (Z flag is clear) |
| 113 | + bic ip, #0x20 @ clear bit 5 (MVN -> MOV) |
| 114 | + orr ip, ip, r0, lsr #16 @ MOV -> MVN if offset < 0 |
88 | 115 | ARM_BE8(rev16 ip, ip) |
89 | 116 | strh ip, [r7] |
| 117 | + @ Z flag is set |
| 118 | +0: |
| 119 | +#endif |
| 120 | + ldrh ip, [r7, #2] |
| 121 | +ARM_BE8(rev16 ip, ip) |
| 122 | + and ip, #0xf00 @ clear everything except Rd field |
| 123 | + orreq ip, r0 @ Z flag set -> MOV/MVN -> patch in high bits |
| 124 | + orrne ip, r6 @ Z flag clear -> MOVW -> patch in low bits |
| 125 | +ARM_BE8(rev16 ip, ip) |
| 126 | + strh ip, [r7, #2] |
90 | 127 | #else |
91 | 128 | #ifdef CONFIG_CPU_ENDIAN_BE8 |
92 | 129 | @ in BE8, we load data in BE, but instructions still in LE |
93 | | -#define PV_BIT22 0x00004000 |
| 130 | +#define PV_BIT24 0x00000001 |
94 | 131 | #define PV_IMM8_MASK 0xff000000 |
95 | | -#define PV_ROT_MASK 0x000f0000 |
96 | 132 | #else |
97 | | -#define PV_BIT22 0x00400000 |
| 133 | +#define PV_BIT24 0x01000000 |
98 | 134 | #define PV_IMM8_MASK 0x000000ff |
99 | | -#define PV_ROT_MASK 0xf00 |
100 | 135 | #endif |
101 | 136 |
|
| 137 | + @ |
| 138 | + @ The ARM versions of the patchable sequences are |
| 139 | + @ |
| 140 | + @ phys-to-virt: sub <VA>, <PA>, #offset<31:24>, lsl #24 |
| 141 | + @ |
| 142 | + @ virt-to-phys (non-LPAE): add <PA>, <VA>, #offset<31:24>, lsl #24 |
| 143 | + @ |
| 144 | + @ virt-to-phys (LPAE): movw <reg>, #offset<31:24> |
| 145 | + @ adds <PAlo>, <VA>, <reg>, lsl #24 |
| 146 | + @ mov <PAhi>, #offset<39:32> |
| 147 | + @ adc <PAhi>, <PAhi>, #0 |
| 148 | + @ |
| 149 | + @ In the non-LPAE case, all patchable instructions are ADD or SUB |
| 150 | + @ instructions, where we need to patch in the offset into the |
| 151 | + @ immediate field of the opcode, which is emitted with the correct |
| 152 | + @ rotation value. (The effective value of the immediate is imm12<7:0> |
| 153 | + @ rotated right by [2 * imm12<11:8>] bits) |
| 154 | + @ |
| 155 | + @ 31 28 27 23 22 20 19 16 15 12 11 0 |
| 156 | + @ +------+-----------------+------+------+-------+ |
| 157 | + @ ADD | cond | 0 0 1 0 1 0 0 0 | Rn | Rd | imm12 | |
| 158 | + @ SUB | cond | 0 0 1 0 0 1 0 0 | Rn | Rd | imm12 | |
| 159 | + @ MOV | cond | 0 0 1 1 1 0 1 0 | Rn | Rd | imm12 | |
| 160 | + @ MVN | cond | 0 0 1 1 1 1 1 0 | Rn | Rd | imm12 | |
| 161 | + @ +------+-----------------+------+------+-------+ |
| 162 | + @ |
| 163 | + @ In the LPAE case, we use a MOVW instruction to carry the low offset |
| 164 | + @ word, and patch in the high word of the offset into the immediate |
| 165 | + @ field of the subsequent MOV instruction, or patch it to a MVN |
| 166 | + @ instruction if the offset is negative. We can distinguish MOVW |
| 167 | + @ instructions based on bits 23:22 of the opcode, and ADD/SUB can be |
| 168 | + @ distinguished from MOV/MVN (all using the encodings above) using |
| 169 | + @ bit 24. |
| 170 | + @ |
| 171 | + @ 31 28 27 23 22 20 19 16 15 12 11 0 |
| 172 | + @ +------+-----------------+------+------+-------+ |
| 173 | + @ MOVW | cond | 0 0 1 1 0 0 0 0 | imm4 | Rd | imm12 | |
| 174 | + @ +------+-----------------+------+------+-------+ |
| 175 | + @ |
102 | 176 | moveq r0, #0x400000 @ set bit 22, mov to mvn instruction |
103 | 177 | b .Lnext |
104 | 178 | .Lloop: ldr ip, [r7, r4] |
| 179 | +#ifdef CONFIG_ARM_LPAE |
| 180 | + tst ip, #PV_BIT24 @ ADD/SUB have bit 24 clear |
| 181 | + beq 1f |
| 182 | +ARM_BE8(rev ip, ip) |
| 183 | + tst ip, #0xc00000 @ MOVW has bits 23:22 clear |
| 184 | + bic ip, ip, #0x400000 @ clear bit 22 |
| 185 | + bfc ip, #0, #12 @ clear imm12 field of MOV[W] instruction |
| 186 | + orreq ip, ip, r6 @ MOVW -> mask in offset bits 31-24 |
| 187 | + orrne ip, ip, r0 @ MOV -> mask in offset bits 7-0 (or bit 22) |
| 188 | +ARM_BE8(rev ip, ip) |
| 189 | + b 2f |
| 190 | +1: |
| 191 | +#endif |
105 | 192 | bic ip, ip, #PV_IMM8_MASK |
106 | | - tst ip, #PV_ROT_MASK @ check the rotation field |
107 | | - orrne ip, ip, r6 ARM_BE8(, lsl #24) @ mask in offset bits 31-24 |
108 | | - biceq ip, ip, #PV_BIT22 @ clear bit 22 |
109 | | - orreq ip, ip, r0 ARM_BE8(, ror #8) @ mask in offset bits 7-0 (or bit 22) |
| 193 | + orr ip, ip, r6 ARM_BE8(, lsl #24) @ mask in offset bits 31-24 |
| 194 | +2: |
110 | 195 | str ip, [r7, r4] |
111 | 196 | add r4, r4, #4 |
112 | 197 | #endif |
|
0 commit comments