Skip to content

Commit 357367a

Browse files
ardbiesheuvelpelwell
authored andcommitted
ARM: p2v: switch to MOVW for Thumb2 and ARM/LPAE
commit e8e00f5 upstream. In preparation for reducing the phys-to-virt minimum relative alignment from 16 MiB to 2 MiB, switch to patchable sequences involving MOVW instructions that can more easily be manipulated to carry a 12-bit immediate. Note that the non-LPAE ARM sequence is not updated: MOVW may not be supported on non-LPAE platforms, and the sequence itself can be updated more easily to apply the 12 bits of displacement. For Thumb2, which has many more versions of opcodes, switch to a sequence that can be patched by the same patching code for both versions. Note that the Thumb2 opcodes for MOVW and MVN are unambiguous, and have no rotation bits in their immediate fields, so there is no need to use placeholder constants in the asm blocks. While at it, drop the 'volatile' qualifiers from the asm blocks: the code does not have any side effects that are invisible to the compiler, so it is free to omit these sequences if the outputs are not used. Suggested-by: Russell King <linux@armlinux.org.uk> Acked-by: Nicolas Pitre <nico@fluxnic.net> Reviewed-by: Linus Walleij <linus.walleij@linaro.org> Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
1 parent 03e8e5d commit 357367a

2 files changed

Lines changed: 148 additions & 43 deletions

File tree

‎arch/arm/include/asm/memory.h‎

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ extern const void *__pv_table_begin, *__pv_table_end;
188188
#define PHYS_OFFSET ((phys_addr_t)__pv_phys_pfn_offset << PAGE_SHIFT)
189189
#define PHYS_PFN_OFFSET (__pv_phys_pfn_offset)
190190

191+
#ifndef CONFIG_THUMB2_KERNEL
191192
#define __pv_stub(from,to,instr) \
192193
__asm__("@ __pv_stub\n" \
193194
"1: " instr " %0, %1, %2\n" \
@@ -197,25 +198,45 @@ extern const void *__pv_table_begin, *__pv_table_end;
197198
: "=r" (to) \
198199
: "r" (from), "I" (__PV_BITS_31_24))
199200

200-
#define __pv_stub_mov_hi(t) \
201-
__asm__ volatile("@ __pv_stub_mov\n" \
202-
"1: mov %R0, %1\n" \
201+
#define __pv_add_carry_stub(x, y) \
202+
__asm__("@ __pv_add_carry_stub\n" \
203+
"0: movw %R0, #0\n" \
204+
" adds %Q0, %1, %R0, lsl #24\n" \
205+
"1: mov %R0, %2\n" \
206+
" adc %R0, %R0, #0\n" \
203207
" .pushsection .pv_table,\"a\"\n" \
204-
" .long 1b - .\n" \
208+
" .long 0b - ., 1b - .\n" \
205209
" .popsection\n" \
206-
: "=r" (t) \
207-
: "I" (__PV_BITS_7_0))
210+
: "=&r" (y) \
211+
: "r" (x), "I" (__PV_BITS_7_0) \
212+
: "cc")
213+
214+
#else
215+
#define __pv_stub(from,to,instr) \
216+
__asm__("@ __pv_stub\n" \
217+
"0: movw %0, #0\n" \
218+
" lsl %0, #24\n" \
219+
" " instr " %0, %1, %0\n" \
220+
" .pushsection .pv_table,\"a\"\n" \
221+
" .long 0b - .\n" \
222+
" .popsection\n" \
223+
: "=&r" (to) \
224+
: "r" (from))
208225

209226
#define __pv_add_carry_stub(x, y) \
210-
__asm__ volatile("@ __pv_add_carry_stub\n" \
211-
"1: adds %Q0, %1, %2\n" \
227+
__asm__("@ __pv_add_carry_stub\n" \
228+
"0: movw %R0, #0\n" \
229+
" lsls %R0, #24\n" \
230+
" adds %Q0, %1, %R0\n" \
231+
"1: mvn %R0, #0\n" \
212232
" adc %R0, %R0, #0\n" \
213233
" .pushsection .pv_table,\"a\"\n" \
214-
" .long 1b - .\n" \
234+
" .long 0b - ., 1b - .\n" \
215235
" .popsection\n" \
216-
: "+r" (y) \
217-
: "r" (x), "I" (__PV_BITS_31_24) \
236+
: "=&r" (y) \
237+
: "r" (x) \
218238
: "cc")
239+
#endif
219240

220241
static inline phys_addr_t __virt_to_phys_nodebug(unsigned long x)
221242
{
@@ -224,7 +245,6 @@ static inline phys_addr_t __virt_to_phys_nodebug(unsigned long x)
224245
if (sizeof(phys_addr_t) == 4) {
225246
__pv_stub(x, t, "add");
226247
} else {
227-
__pv_stub_mov_hi(t);
228248
__pv_add_carry_stub(x, t);
229249
}
230250
return t;

‎arch/arm/kernel/phys2virt.S‎

Lines changed: 116 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* SPDX-License-Identifier: GPL-2.0-only */
22
/*
33
* Copyright (C) 1994-2002 Russell King
4-
* Copyright (c) 2003 ARM Limited
4+
* Copyright (c) 2003, 2020 ARM Limited
55
* All Rights Reserved
66
*/
77

@@ -58,55 +58,140 @@ __fixup_a_pv_table:
5858
mov r6, r6, lsr #24
5959
cmn r0, #1
6060
#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+
@
61104
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
72105
b .Lnext
73106
.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
84109
ldrh ip, [r7]
85110
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
88115
ARM_BE8(rev16 ip, ip)
89116
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]
90127
#else
91128
#ifdef CONFIG_CPU_ENDIAN_BE8
92129
@ in BE8, we load data in BE, but instructions still in LE
93-
#define PV_BIT22 0x00004000
130+
#define PV_BIT24 0x00000001
94131
#define PV_IMM8_MASK 0xff000000
95-
#define PV_ROT_MASK 0x000f0000
96132
#else
97-
#define PV_BIT22 0x00400000
133+
#define PV_BIT24 0x01000000
98134
#define PV_IMM8_MASK 0x000000ff
99-
#define PV_ROT_MASK 0xf00
100135
#endif
101136

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+
@
102176
moveq r0, #0x400000 @ set bit 22, mov to mvn instruction
103177
b .Lnext
104178
.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
105192
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:
110195
str ip, [r7, r4]
111196
add r4, r4, #4
112197
#endif

0 commit comments

Comments
 (0)