* alignment handler instruction endian-ness
@ 2013-07-19 10:58 Ben Dooks
2013-07-19 11:09 ` Russell King - ARM Linux
0 siblings, 1 reply; 12+ messages in thread
From: Ben Dooks @ 2013-07-19 10:58 UTC (permalink / raw)
To: linux-arm-kernel
I ran in to an issue with the alignment handler when running BE8 where
it loads instructions and fails to swap.
Is there a better way of swapping instructions for ARM when loading
from arbitrary places? Have I missed any other places this could happen?
The following patch is my first attempt at solving the problem for the
alignment handler:
> Author: Ben Dooks <ben.dooks@codethink.co.uk>
> Date: Thu Jul 18 21:10:56 2013 +0100
>
> arm: alignment: deal with be8 mode when decoding instructions
>
> If we are in BE8 mode, we must deal with the instruction stream being
> in LE order when data is being loaded in BE order. Ensure the data is
> swapped before processing to avoid thre following:
>
> Alignment trap: not handling instruction 030091e8 at [<80333e8c>]
> Unhandled fault: alignment exception (0x001) at 0xbfa09567
>
> Signed-off-by: Ben Dooks <ben.dooks@codethink.co.uk>
>
> diff --git a/arch/arm/mm/alignment.c b/arch/arm/mm/alignment.c
> index db26e2e..e8793a7 100644
> --- a/arch/arm/mm/alignment.c
> +++ b/arch/arm/mm/alignment.c
> @@ -87,6 +87,12 @@ core_param(alignment, ai_usermode, int, 0600);
> #define UM_FIXUP (1 << 1)
> #define UM_SIGNAL (1 << 2)
>
> +#ifdef CONFIG_CPU_ENDIAN_BE8
> +#define need_swap() true
> +#else
> +#define need_swap() false
> +#endif
> +
> /* Return true if and only if the ARMv6 unaligned access model is in use. */
> static bool cpu_is_v6_unaligned(void)
> {
> @@ -762,12 +768,16 @@ do_alignment(unsigned long addr, unsigned int fsr, struct
> if (thumb_mode(regs)) {
> u16 *ptr = (u16 *)(instrptr & ~1);
> fault = probe_kernel_address(ptr, tinstr);
> + if (need_swap())
> + tinstr = cpu_to_le16(tinstr);
> if (!fault) {
> if (cpu_architecture() >= CPU_ARCH_ARMv7 &&
> IS_T32(tinstr)) {
> /* Thumb-2 32-bit */
> u16 tinst2 = 0;
> fault = probe_kernel_address(ptr + 1, tinst2);
> + if (need_swap())
> + tinst2 = cpu_to_le16(tinst2);
> instr = (tinstr << 16) | tinst2;
> thumb2_32b = 1;
> } else {
> @@ -775,8 +785,11 @@ do_alignment(unsigned long addr, unsigned int fsr, struct p
> instr = thumb2arm(tinstr);
> }
> }
> - } else
> + } else {
> fault = probe_kernel_address(instrptr, instr);
> + if (need_swap())
> + instr = cpu_to_le32(instr);
> + }
>
> if (fault) {
> type = TYPE_FAULT;
--
Ben Dooks http://www.codethink.co.uk/
Senior Engineer Codethink - Providing Genius
^ permalink raw reply [flat|nested] 12+ messages in thread
* alignment handler instruction endian-ness
2013-07-19 10:58 alignment handler instruction endian-ness Ben Dooks
@ 2013-07-19 11:09 ` Russell King - ARM Linux
2013-07-19 11:26 ` Ben Dooks
` (2 more replies)
0 siblings, 3 replies; 12+ messages in thread
From: Russell King - ARM Linux @ 2013-07-19 11:09 UTC (permalink / raw)
To: linux-arm-kernel
On Fri, Jul 19, 2013 at 11:58:45AM +0100, Ben Dooks wrote:
> I ran in to an issue with the alignment handler when running BE8 where
> it loads instructions and fails to swap.
>
> Is there a better way of swapping instructions for ARM when loading
> from arbitrary places? Have I missed any other places this could happen?
Maybe we need a macro which deals with this automatically?
arm_instr_to_cpu(x)
thumb_instr_to_cpu(x)
These should probably make use of the swab*() macros when an endian swap
is needed.
> The following patch is my first attempt at solving the problem for the
> alignment handler:
I'd suggest checking this with sparse too - you have some type errors here.
^ permalink raw reply [flat|nested] 12+ messages in thread
* alignment handler instruction endian-ness
2013-07-19 11:09 ` Russell King - ARM Linux
@ 2013-07-19 11:26 ` Ben Dooks
2013-07-19 12:15 ` Ben Dooks
2013-07-19 14:01 ` Jon Medhurst (Tixy)
2 siblings, 0 replies; 12+ messages in thread
From: Ben Dooks @ 2013-07-19 11:26 UTC (permalink / raw)
To: linux-arm-kernel
On 19/07/13 12:09, Russell King - ARM Linux wrote:
> On Fri, Jul 19, 2013 at 11:58:45AM +0100, Ben Dooks wrote:
>> I ran in to an issue with the alignment handler when running BE8 where
>> it loads instructions and fails to swap.
>>
>> Is there a better way of swapping instructions for ARM when loading
>> from arbitrary places? Have I missed any other places this could happen?
>
> Maybe we need a macro which deals with this automatically?
>
> arm_instr_to_cpu(x)
> thumb_instr_to_cpu(x)
>
> These should probably make use of the swab*() macros when an endian swap
> is needed.
>
>> The following patch is my first attempt at solving the problem for the
>> alignment handler:
>
> I'd suggest checking this with sparse too - you have some type errors here.
Very probably, I needed this in a hurry and was after some input on how
it could be improved.
The only issue I was thinking of for having those functions would be if
the userspace was running BE32 and the kernel was LE. I don't think this
is very likely, so it can probably be discounted.
I could not find where the kernel's relocation code does this to see if
it also needed changing.
--
Ben Dooks http://www.codethink.co.uk/
Senior Engineer Codethink - Providing Genius
^ permalink raw reply [flat|nested] 12+ messages in thread
* alignment handler instruction endian-ness
2013-07-19 11:09 ` Russell King - ARM Linux
2013-07-19 11:26 ` Ben Dooks
@ 2013-07-19 12:15 ` Ben Dooks
2013-07-19 12:32 ` Russell King - ARM Linux
2013-07-19 14:01 ` Jon Medhurst (Tixy)
2 siblings, 1 reply; 12+ messages in thread
From: Ben Dooks @ 2013-07-19 12:15 UTC (permalink / raw)
To: linux-arm-kernel
On 19/07/13 12:09, Russell King - ARM Linux wrote:
> On Fri, Jul 19, 2013 at 11:58:45AM +0100, Ben Dooks wrote:
>> I ran in to an issue with the alignment handler when running BE8 where
>> it loads instructions and fails to swap.
>>
>> Is there a better way of swapping instructions for ARM when loading
>> from arbitrary places? Have I missed any other places this could happen?
>
> Maybe we need a macro which deals with this automatically?
>
> arm_instr_to_cpu(x)
> thumb_instr_to_cpu(x)
>
> These should probably make use of the swab*() macros when an endian swap
> is needed.
>
>> The following patch is my first attempt at solving the problem for the
>> alignment handler:
>
> I'd suggest checking this with sparse too - you have some type errors here.
How about:
diff --git a/arch/arm/include/uapi/asm/byteorder.h
b/arch/arm/include/uapi/asm/b
index 7737974..9e63a00 100644
--- a/arch/arm/include/uapi/asm/byteorder.h
+++ b/arch/arm/include/uapi/asm/byteorder.h
@@ -21,5 +21,13 @@
#include <linux/byteorder/little_endian.h>
#endif
+#ifdef CONFIG_CPU_ENDIAN_BE8
+#define cpu_to_arm_instr(__instr) swab32(__instr)
+#define cpu_to_thum_instr(__instr) swab16(__instr)
+#else
+#define cpu_to_arm_instr(__instr) (__instr)
+#define cpu_to_thum_instr(__instr) (__instr)
+#endif
--
Ben Dooks http://www.codethink.co.uk/
Senior Engineer Codethink - Providing Genius
^ permalink raw reply related [flat|nested] 12+ messages in thread
* alignment handler instruction endian-ness
2013-07-19 12:15 ` Ben Dooks
@ 2013-07-19 12:32 ` Russell King - ARM Linux
2013-07-19 13:19 ` Ben Dooks
0 siblings, 1 reply; 12+ messages in thread
From: Russell King - ARM Linux @ 2013-07-19 12:32 UTC (permalink / raw)
To: linux-arm-kernel
On Fri, Jul 19, 2013 at 01:15:33PM +0100, Ben Dooks wrote:
> On 19/07/13 12:09, Russell King - ARM Linux wrote:
>> On Fri, Jul 19, 2013 at 11:58:45AM +0100, Ben Dooks wrote:
>>> I ran in to an issue with the alignment handler when running BE8 where
>>> it loads instructions and fails to swap.
>>>
>>> Is there a better way of swapping instructions for ARM when loading
>>> from arbitrary places? Have I missed any other places this could happen?
>>
>> Maybe we need a macro which deals with this automatically?
>>
>> arm_instr_to_cpu(x)
>> thumb_instr_to_cpu(x)
>>
>> These should probably make use of the swab*() macros when an endian swap
>> is needed.
>>
>>> The following patch is my first attempt at solving the problem for the
>>> alignment handler:
>>
>> I'd suggest checking this with sparse too - you have some type errors here.
>
> How about:
>
> diff --git a/arch/arm/include/uapi/asm/byteorder.h
> b/arch/arm/include/uapi/asm/b
> index 7737974..9e63a00 100644
> --- a/arch/arm/include/uapi/asm/byteorder.h
> +++ b/arch/arm/include/uapi/asm/byteorder.h
> @@ -21,5 +21,13 @@
> #include <linux/byteorder/little_endian.h>
> #endif
>
>
> +#ifdef CONFIG_CPU_ENDIAN_BE8
> +#define cpu_to_arm_instr(__instr) swab32(__instr)
> +#define cpu_to_thum_instr(__instr) swab16(__instr)
> +#else
> +#define cpu_to_arm_instr(__instr) (__instr)
> +#define cpu_to_thum_instr(__instr) (__instr)
> +#endif
ARGH.
First point: does userspace have any knowledge how the kernel is
configured? Do they include the kernel configuration header file? No.
Therefore the use of CONFIG_* in UAPI header files is wrong.
Second point: does userspace need these macros and should the kernel
provide them to userspace? No on both counts. Therefore, this must
not be placed in UAPI.
UAPI was created explicitly to separate the kernel private definitions
from the parts of the kernel interface which the user API depends on.
If you value your soul, NEVER EVER EVER place stuff which is not part
of the user API in a header file in a UAPI subdirectory. And never
modify a header file in a UAPI subdirectory without first having a
good long think about how that modification may impact _userspace_.
Third point: the name is totally and utterly wrong. I think you've
misunderstood the naming conventions of the cpu_to_xxx() and xxx_to_cpu()
macros. "cpu" is the CPUs native data endianness - the endianness that
you can do standard arithmetic on and you get the right answer. "xxx"
is the "foreign" endianness. In this case, it's the instructions which
are not the CPUs native data endianness, so they are the foreign state -
they are the "xxx".
So, I've no idea why you decided to reverse the name of the macro I gave
you... I am astounded at this.
^ permalink raw reply [flat|nested] 12+ messages in thread
* alignment handler instruction endian-ness
2013-07-19 12:32 ` Russell King - ARM Linux
@ 2013-07-19 13:19 ` Ben Dooks
2013-07-22 18:31 ` Nicolas Pitre
0 siblings, 1 reply; 12+ messages in thread
From: Ben Dooks @ 2013-07-19 13:19 UTC (permalink / raw)
To: linux-arm-kernel
On 19/07/13 13:32, Russell King - ARM Linux wrote:
> On Fri, Jul 19, 2013 at 01:15:33PM +0100, Ben Dooks wrote:
>> On 19/07/13 12:09, Russell King - ARM Linux wrote:
>>> On Fri, Jul 19, 2013 at 11:58:45AM +0100, Ben Dooks wrote:
>>>> I ran in to an issue with the alignment handler when running BE8 where
>>>> it loads instructions and fails to swap.
>>>>
>>>> Is there a better way of swapping instructions for ARM when loading
>>>> from arbitrary places? Have I missed any other places this could happen?
>>>
>>> Maybe we need a macro which deals with this automatically?
>>>
>>> arm_instr_to_cpu(x)
>>> thumb_instr_to_cpu(x)
>>>
>>> These should probably make use of the swab*() macros when an endian swap
>>> is needed.
>>>
>>>> The following patch is my first attempt at solving the problem for the
>>>> alignment handler:
>>>
>>> I'd suggest checking this with sparse too - you have some type errors here.
>>
>> How about:
>>
>> diff --git a/arch/arm/include/uapi/asm/byteorder.h
>> b/arch/arm/include/uapi/asm/b
>> index 7737974..9e63a00 100644
>> --- a/arch/arm/include/uapi/asm/byteorder.h
>> +++ b/arch/arm/include/uapi/asm/byteorder.h
>> @@ -21,5 +21,13 @@
>> #include<linux/byteorder/little_endian.h>
>> #endif
>>
>>
>> +#ifdef CONFIG_CPU_ENDIAN_BE8
>> +#define cpu_to_arm_instr(__instr) swab32(__instr)
>> +#define cpu_to_thum_instr(__instr) swab16(__instr)
>> +#else
>> +#define cpu_to_arm_instr(__instr) (__instr)
>> +#define cpu_to_thum_instr(__instr) (__instr)
>> +#endif
>
> ARGH.
>
> First point: does userspace have any knowledge how the kernel is
> configured? Do they include the kernel configuration header file? No.
> Therefore the use of CONFIG_* in UAPI header files is wrong.
>
> Second point: does userspace need these macros and should the kernel
> provide them to userspace? No on both counts. Therefore, this must
> not be placed in UAPI.
>
> UAPI was created explicitly to separate the kernel private definitions
> from the parts of the kernel interface which the user API depends on.
>
> If you value your soul, NEVER EVER EVER place stuff which is not part
> of the user API in a header file in a UAPI subdirectory. And never
> modify a header file in a UAPI subdirectory without first having a
> good long think about how that modification may impact _userspace_.
Sorry, searched for byteorder.h and just did not notice.
Where is the best place for these to go?
> Third point: the name is totally and utterly wrong. I think you've
> misunderstood the naming conventions of the cpu_to_xxx() and xxx_to_cpu()
> macros. "cpu" is the CPUs native data endianness - the endianness that
> you can do standard arithmetic on and you get the right answer. "xxx"
> is the "foreign" endianness. In this case, it's the instructions which
> are not the CPUs native data endianness, so they are the foreign state -
> they are the "xxx".
>
> So, I've no idea why you decided to reverse the name of the macro I gave
> you... I am astounded at this.
I meant to put both versions in as I thought they'd be needed/.
--
Ben Dooks http://www.codethink.co.uk/
Senior Engineer Codethink - Providing Genius
^ permalink raw reply [flat|nested] 12+ messages in thread
* alignment handler instruction endian-ness
2013-07-19 11:09 ` Russell King - ARM Linux
2013-07-19 11:26 ` Ben Dooks
2013-07-19 12:15 ` Ben Dooks
@ 2013-07-19 14:01 ` Jon Medhurst (Tixy)
2013-07-19 14:05 ` Russell King - ARM Linux
2 siblings, 1 reply; 12+ messages in thread
From: Jon Medhurst (Tixy) @ 2013-07-19 14:01 UTC (permalink / raw)
To: linux-arm-kernel
On Fri, 2013-07-19 at 12:09 +0100, Russell King - ARM Linux wrote:
> On Fri, Jul 19, 2013 at 11:58:45AM +0100, Ben Dooks wrote:
> > I ran in to an issue with the alignment handler when running BE8 where
> > it loads instructions and fails to swap.
> >
> > Is there a better way of swapping instructions for ARM when loading
> > from arbitrary places? Have I missed any other places this could happen?
>
> Maybe we need a macro which deals with this automatically?
Like the stuff in arch/arm/include/asm/opcodes.h ?
> arm_instr_to_cpu(x)
> thumb_instr_to_cpu(x)
>
> These should probably make use of the swab*() macros when an endian swap
> is needed.
>
> > The following patch is my first attempt at solving the problem for the
> > alignment handler:
>
> I'd suggest checking this with sparse too - you have some type errors here.
>
> _______________________________________________
> linux-arm-kernel mailing list
> linux-arm-kernel at lists.infradead.org
> http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
^ permalink raw reply [flat|nested] 12+ messages in thread
* alignment handler instruction endian-ness
2013-07-19 14:01 ` Jon Medhurst (Tixy)
@ 2013-07-19 14:05 ` Russell King - ARM Linux
2013-07-19 14:50 ` Ben Dooks
0 siblings, 1 reply; 12+ messages in thread
From: Russell King - ARM Linux @ 2013-07-19 14:05 UTC (permalink / raw)
To: linux-arm-kernel
On Fri, Jul 19, 2013 at 03:01:02PM +0100, Jon Medhurst (Tixy) wrote:
> On Fri, 2013-07-19 at 12:09 +0100, Russell King - ARM Linux wrote:
> > On Fri, Jul 19, 2013 at 11:58:45AM +0100, Ben Dooks wrote:
> > > I ran in to an issue with the alignment handler when running BE8 where
> > > it loads instructions and fails to swap.
> > >
> > > Is there a better way of swapping instructions for ARM when loading
> > > from arbitrary places? Have I missed any other places this could happen?
> >
> > Maybe we need a macro which deals with this automatically?
>
> Like the stuff in arch/arm/include/asm/opcodes.h ?
Not like asm/opcodes.h does at the moment, because there's really no
reason whatsoever that it isn't using the standard swab stuff, which
will automatically use the rev/rev16 instructions where available.
There's really no reason to open code that stuff.
^ permalink raw reply [flat|nested] 12+ messages in thread
* alignment handler instruction endian-ness
2013-07-19 14:05 ` Russell King - ARM Linux
@ 2013-07-19 14:50 ` Ben Dooks
2013-07-19 14:56 ` Russell King - ARM Linux
0 siblings, 1 reply; 12+ messages in thread
From: Ben Dooks @ 2013-07-19 14:50 UTC (permalink / raw)
To: linux-arm-kernel
On 19/07/13 15:05, Russell King - ARM Linux wrote:
> On Fri, Jul 19, 2013 at 03:01:02PM +0100, Jon Medhurst (Tixy) wrote:
>> On Fri, 2013-07-19 at 12:09 +0100, Russell King - ARM Linux wrote:
>>> On Fri, Jul 19, 2013 at 11:58:45AM +0100, Ben Dooks wrote:
>>>> I ran in to an issue with the alignment handler when running BE8 where
>>>> it loads instructions and fails to swap.
>>>>
>>>> Is there a better way of swapping instructions for ARM when loading
>>>> from arbitrary places? Have I missed any other places this could happen?
>>>
>>> Maybe we need a macro which deals with this automatically?
>>
>> Like the stuff in arch/arm/include/asm/opcodes.h ?
>
> Not like asm/opcodes.h does at the moment, because there's really no
> reason whatsoever that it isn't using the standard swab stuff, which
> will automatically use the rev/rev16 instructions where available.
>
> There's really no reason to open code that stuff.
It seems it is only open coded for the __ASSEMBLY__ version,
it does use swabXX() for the !__ASSEMBLY__ case.
Should we use __mem_to_opcode_arm() in the aligned and abort
handlers?
--
Ben Dooks http://www.codethink.co.uk/
Senior Engineer Codethink - Providing Genius
^ permalink raw reply [flat|nested] 12+ messages in thread
* alignment handler instruction endian-ness
2013-07-19 14:50 ` Ben Dooks
@ 2013-07-19 14:56 ` Russell King - ARM Linux
0 siblings, 0 replies; 12+ messages in thread
From: Russell King - ARM Linux @ 2013-07-19 14:56 UTC (permalink / raw)
To: linux-arm-kernel
On Fri, Jul 19, 2013 at 03:50:12PM +0100, Ben Dooks wrote:
> Should we use __mem_to_opcode_arm() in the aligned and abort
> handlers?
I think so.
^ permalink raw reply [flat|nested] 12+ messages in thread
* alignment handler instruction endian-ness
2013-07-19 13:19 ` Ben Dooks
@ 2013-07-22 18:31 ` Nicolas Pitre
2013-07-22 18:34 ` Ben Dooks
0 siblings, 1 reply; 12+ messages in thread
From: Nicolas Pitre @ 2013-07-22 18:31 UTC (permalink / raw)
To: linux-arm-kernel
On Fri, 19 Jul 2013, Ben Dooks wrote:
> On 19/07/13 13:32, Russell King - ARM Linux wrote:
> > If you value your soul, NEVER EVER EVER place stuff which is not part
> > of the user API in a header file in a UAPI subdirectory. And never
> > modify a header file in a UAPI subdirectory without first having a
> > good long think about how that modification may impact _userspace_.
>
> Sorry, searched for byteorder.h and just did not notice.
>
> Where is the best place for these to go?
You should really have a look at arch/arm/include/asm/opcodes.h.
Nicolas
^ permalink raw reply [flat|nested] 12+ messages in thread
* alignment handler instruction endian-ness
2013-07-22 18:31 ` Nicolas Pitre
@ 2013-07-22 18:34 ` Ben Dooks
0 siblings, 0 replies; 12+ messages in thread
From: Ben Dooks @ 2013-07-22 18:34 UTC (permalink / raw)
To: linux-arm-kernel
On 22/07/13 19:31, Nicolas Pitre wrote:
> On Fri, 19 Jul 2013, Ben Dooks wrote:
>> On 19/07/13 13:32, Russell King - ARM Linux wrote:
>>> If you value your soul, NEVER EVER EVER place stuff which is not part
>>> of the user API in a header file in a UAPI subdirectory. And never
>>> modify a header file in a UAPI subdirectory without first having a
>>> good long think about how that modification may impact _userspace_.
>>
>> Sorry, searched for byteorder.h and just did not notice.
>>
>> Where is the best place for these to go?
>
> You should really have a look at arch/arm/include/asm/opcodes.h.
Yes, re-worked using <asm/opcodes.h> thanks.
--
Ben Dooks http://www.codethink.co.uk/
Senior Engineer Codethink - Providing Genius
^ permalink raw reply [flat|nested] 12+ messages in thread
end of thread, other threads:[~2013-07-22 18:34 UTC | newest]
Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-07-19 10:58 alignment handler instruction endian-ness Ben Dooks
2013-07-19 11:09 ` Russell King - ARM Linux
2013-07-19 11:26 ` Ben Dooks
2013-07-19 12:15 ` Ben Dooks
2013-07-19 12:32 ` Russell King - ARM Linux
2013-07-19 13:19 ` Ben Dooks
2013-07-22 18:31 ` Nicolas Pitre
2013-07-22 18:34 ` Ben Dooks
2013-07-19 14:01 ` Jon Medhurst (Tixy)
2013-07-19 14:05 ` Russell King - ARM Linux
2013-07-19 14:50 ` Ben Dooks
2013-07-19 14:56 ` Russell King - ARM Linux
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).