Skip to contentSkip to conversation

arm64: add workaround for ambiguous C99 stdint.h types

PATCH New Ard Biesheuvel 2 participants 8 messages Jan 26, 2014 – Feb 20, 2014 arm64

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 arm64: add workaround for ambiguous C99 stdint.h types

In a way similar to ARM commit 09096f6a0ee2 ("ARM: 7822/1: add workaround for ambiguous C99 stdint.h types"), this patch redefines the macros that are used in stdint.h so its definitions of uint64_t and int64_t are compatible with those of the kernel.

In order to do so, drop types.h from generic-y and create a specific arm64 version identical to the generic one with just the #define overrides added.

---
 arch/arm64/include/asm/Kbuild  |  1 -
 arch/arm64/include/asm/types.h | 26 ++++++++++++++++++++++++++
 2 files changed, 26 insertions(+), 1 deletion(-)
 create mode 100644 arch/arm64/include/asm/types.h
arch/arm64/include/asm/Kbuild +0 -1
index d0ff25de67ca..93e0653e8c65 100644
@@ -44,7 +44,6 @@ generic-y += termbits.h
 generic-y += termios.h
 generic-y += topology.h
 generic-y += trace_clock.h
-generic-y += types.h
 generic-y += unaligned.h
 generic-y += user.h
 generic-y += vga.h
arch/arm64/include/asm/types.h new file +26 -0
new file mode 100644
index 000000000000..6519296da003
@@ -0,0 +1,26 @@
+#ifndef __ASM_TYPES_H
+#define __ASM_TYPES_H
+
+#include <asm-generic/int-ll64.h>
+
+/*
+ * For Aarch64, there is some ambiguity in the definition of the types below
+ * between the kernel and GCC itself. This is usually not a big deal, but it
+ * causes trouble when including GCC's version of 'stdint.h' (this is the file
+ * that gets included when you #include <stdint.h> on a -ffreestanding build).
+ * As this file also gets included implicitly when including 'arm_neon.h' (the
+ * NEON intrinsics support header), we need the following to work around the
+ * issue if we want to use NEON intrinsics in the kernel.
+ */
+
+#ifdef __INT64_TYPE__
+#undef __INT64_TYPE__
+#define __INT64_TYPE__		__signed__ long long
+#endif
+
+#ifdef __UINT64_TYPE__
+#undef __UINT64_TYPE__
+#define __UINT64_TYPE__		unsigned long long
+#endif
+
+#endif /* __ASM_TYPES_H */
Signature · 1 line
1.8.3.2
maintainer
#1 lore raw

PATCH 1/1 arm64: add workaround for ambiguous C99 stdint.h types

Hi Ard,

Quoted 7 lines · Ard Biesheuvel

In a way similar to ARM commit 09096f6a0ee2 ("ARM: 7822/1: add workaround for ambiguous C99 stdint.h types"), this patch redefines the macros that are used in stdint.h so its definitions of uint64_t and int64_t are compatible with those of the kernel.

In order to do so, drop types.h from generic-y and create a specific arm64 version identical to the generic one with just the #define overrides added.

I tried but still can't get what this patch is about. Do the linux/types.h types ever get to user space? We have uapi/linux/types.h for this.

Can you give an example of where this is needed? Which source file includes both stdint.h and linux/types.h (non-uapi version)?

Signature · 1 line
Catalin
#2 lore raw

PATCH 1/1 arm64: add workaround for ambiguous C99 stdint.h types

Quoted 9 lines · Catalin Marinas
Quoted 7 lines · Ard Biesheuvel

In a way similar to ARM commit 09096f6a0ee2 ("ARM: 7822/1: add workaround for ambiguous C99 stdint.h types"), this patch redefines the macros that are used in stdint.h so its definitions of uint64_t and int64_t are compatible with those of the kernel.

In order to do so, drop types.h from generic-y and create a specific arm64 version identical to the generic one with just the #define overrides added.

Quoted 7 lines

I tried but still can't get what this patch is about. Do the linux/types.h types ever get to user space? We have uapi/linux/types.h for this.

Can you give an example of where this is needed? Which source file includes both stdint.h and linux/types.h (non-uapi version)?

It's not about user space, it is mainly about the use of NEON instrinsics in the kernel.

If you do the following:

#Include <linux/types.h>
#include <arm_neon.h>

(and build with -ffreestanding or you will get /another/ error) you will get the following error:

/usr/lib/gcc-cross/aarch64-linux-gnu/4.8/include/stdint-gcc.h:43:24:
error: conflicting types for 'int64_t'

 typedef __INT64_TYPE__ int64_t;
                        ^

In file included from /home/ard/linux-2.6/drivers/crypto/neon/aes-async-modes.c:11:0:
/home/ard/linux-2.6/include/linux/types.h:113:17: note: previous declaration of 'int64_t' was here

 typedef  __s64  int64_t;
                 ^

In file included from /usr/lib/gcc-cross/aarch64-linux-gnu/4.8/include/stdint.h:11:0,
from
/usr/lib/gcc-cross/aarch64-linux-gnu/4.8/include/arm_neon.h:30,

This is caused by the fact that GCC and the kernel don't agree on the definition of int64_t and uint64_t. (long vs long long)

Fortunately, GCC's definition is not built in but based on the builtin #define __INT64_TYPE, which we can override if we want to. That is what the patch does.

Regards,
Ard.

maintainer
#3 lore raw

PATCH 1/1 arm64: add workaround for ambiguous C99 stdint.h types

Quoted 17 lines · Ard Biesheuvel
Quoted 9 lines · Catalin Marinas
Quoted 7 lines · Ard Biesheuvel

In a way similar to ARM commit 09096f6a0ee2 ("ARM: 7822/1: add workaround for ambiguous C99 stdint.h types"), this patch redefines the macros that are used in stdint.h so its definitions of uint64_t and int64_t are compatible with those of the kernel.

In order to do so, drop types.h from generic-y and create a specific arm64 version identical to the generic one with just the #define overrides added.

Quoted 6 lines

I tried but still can't get what this patch is about. Do the linux/types.h types ever get to user space? We have uapi/linux/types.h for this.

Can you give an example of where this is needed? Which source file includes both stdint.h and linux/types.h (non-uapi version)?

Quoted 7 lines

It's not about user space, it is mainly about the use of NEON instrinsics in the kernel.

If you do the following:

#Include <linux/types.h>
#include <arm_neon.h>

For other intrinsics that we use like __builtin_ctzl(), do we need to explicitly include gcc headers? I don't think we do and I really don't like such arm_neon.h include which brings in other user headers. Don't we have any work around this?

My inbox only has some discussion in May last year on the linaro-kernel list without any clear conclusion (it could be that I deleted other emails).

Signature · 1 line
Catalin
#4 lore raw

PATCH 1/1 arm64: add workaround for ambiguous C99 stdint.h types

Quoted 23 lines · Catalin Marinas
Quoted 22 lines · Ard Biesheuvel
Quoted 9 lines · Catalin Marinas
Quoted 7 lines · Ard Biesheuvel

In a way similar to ARM commit 09096f6a0ee2 ("ARM: 7822/1: add workaround for ambiguous C99 stdint.h types"), this patch redefines the macros that are used in stdint.h so its definitions of uint64_t and int64_t are compatible with those of the kernel.

In order to do so, drop types.h from generic-y and create a specific arm64 version identical to the generic one with just the #define overrides added.

Quoted 6 lines

I tried but still can't get what this patch is about. Do the linux/types.h types ever get to user space? We have uapi/linux/types.h for this.

Can you give an example of where this is needed? Which source file includes both stdint.h and linux/types.h (non-uapi version)?

It's not about user space, it is mainly about the use of NEON instrinsics in the kernel.

If you do the following:

Quoted 8 lines
Quoted 2 lines

#Include <linux/types.h>
#include <arm_neon.h>

For other intrinsics that we use like __builtin_ctzl(), do we need to explicitly include gcc headers? I don't think we do and I really don't like such arm_neon.h include which brings in other user headers. Don't we have any work around this?

Well, I talked to the toolchain guys at the time and they really disliked the idea of coding directly against the __builtins because they are not considered a stable interface, especially because the interface that /is/ considered stable (arm_neon.h) is supported both on ARM and on arm64.

Quoted 4 lines

My inbox only has some discussion in May last year on the linaro-kernel list without any clear conclusion (it could be that I deleted other emails).

There was some discussion, indeed, but for ARM, with the conclusion being the fix I mentioned in the patch: 09096f6a0ee2 ("ARM: 7822/1:
add workaround
for ambiguous C99 stdint.h types"), only in that case, the ambiguity is (unsurprisingly) about the 32 bit types, not the 64 bit ones.

It all comes down to whether you are interested in supporting NEON intrinsics (not __builtins, but intrinsics that require a support header). If you do, this is probably the easiest way to do so, i.e., merge this patch and document the fact that the NEON intrinsics object files need to be built with -ffreestanding (as is the case for ARM). Only then can you freely #include <arm_neon.h> in code that also (indirectly) includes <linux/types.h>.

Regards,
Ard.

maintainer
#5 lore raw

PATCH 1/1 arm64: add workaround for ambiguous C99 stdint.h types

Quoted 32 lines · Ard Biesheuvel
Quoted 23 lines · Catalin Marinas
Quoted 22 lines · Ard Biesheuvel
Quoted 9 lines · Catalin Marinas
Quoted 7 lines · Ard Biesheuvel

In a way similar to ARM commit 09096f6a0ee2 ("ARM: 7822/1: add workaround for ambiguous C99 stdint.h types"), this patch redefines the macros that are used in stdint.h so its definitions of uint64_t and int64_t are compatible with those of the kernel.

In order to do so, drop types.h from generic-y and create a specific arm64 version identical to the generic one with just the #define overrides added.

Quoted 6 lines

I tried but still can't get what this patch is about. Do the linux/types.h types ever get to user space? We have uapi/linux/types.h for this.

Can you give an example of where this is needed? Which source file includes both stdint.h and linux/types.h (non-uapi version)?

It's not about user space, it is mainly about the use of NEON instrinsics in the kernel.

If you do the following:

Quoted 7 lines
Quoted 2 lines

#Include <linux/types.h>
#include <arm_neon.h>

For other intrinsics that we use like __builtin_ctzl(), do we need to explicitly include gcc headers? I don't think we do and I really don't like such arm_neon.h include which brings in other user headers. Don't we have any work around this?

Quoted 5 lines

Well, I talked to the toolchain guys at the time and they really disliked the idea of coding directly against the __builtins because they are not considered a stable interface, especially because the interface that /is/ considered stable (arm_neon.h) is supported both on ARM and on arm64.

Than we don't use the Neon __builtins in the kernel.

Quoted 9 lines
Quoted 3 lines

My inbox only has some discussion in May last year on the linaro-kernel list without any clear conclusion (it could be that I deleted other emails).

There was some discussion, indeed, but for ARM, with the conclusion being the fix I mentioned in the patch: 09096f6a0ee2 ("ARM: 7822/1:
add workaround
for ambiguous C99 stdint.h types"), only in that case, the ambiguity is (unsurprisingly) about the 32 bit types, not the 64 bit ones.

My worry is that some future toolchain may include something else in this file and get other type conflicts. It really looks fragile.

Signature · 1 line
Catalin
#6 lore raw

PATCH 1/1 arm64: add workaround for ambiguous C99 stdint.h types

Quoted 45 lines · Catalin Marinas
Quoted 32 lines · Ard Biesheuvel
Quoted 23 lines · Catalin Marinas
Quoted 22 lines · Ard Biesheuvel
Quoted 9 lines · Catalin Marinas
Quoted 7 lines · Ard Biesheuvel

In a way similar to ARM commit 09096f6a0ee2 ("ARM: 7822/1: add workaround for ambiguous C99 stdint.h types"), this patch redefines the macros that are used in stdint.h so its definitions of uint64_t and int64_t are compatible with those of the kernel.

In order to do so, drop types.h from generic-y and create a specific arm64 version identical to the generic one with just the #define overrides added.

Quoted 6 lines

I tried but still can't get what this patch is about. Do the linux/types.h types ever get to user space? We have uapi/linux/types.h for this.

Can you give an example of where this is needed? Which source file includes both stdint.h and linux/types.h (non-uapi version)?

It's not about user space, it is mainly about the use of NEON instrinsics in the kernel.

If you do the following:

Quoted 7 lines
Quoted 2 lines

#Include <linux/types.h>
#include <arm_neon.h>

For other intrinsics that we use like __builtin_ctzl(), do we need to explicitly include gcc headers? I don't think we do and I really don't like such arm_neon.h include which brings in other user headers. Don't we have any work around this?

Quoted 5 lines

Well, I talked to the toolchain guys at the time and they really disliked the idea of coding directly against the __builtins because they are not considered a stable interface, especially because the interface that /is/ considered stable (arm_neon.h) is supported both on ARM and on arm64.

Than we don't use the Neon __builtins in the kernel.

Quoted 4 lines
Quoted 3 lines

My inbox only has some discussion in May last year on the linaro-kernel list without any clear conclusion (it could be that I deleted other emails).

Quoted 9 lines
Quoted 5 lines

There was some discussion, indeed, but for ARM, with the conclusion being the fix I mentioned in the patch: 09096f6a0ee2 ("ARM: 7822/1:
add workaround
for ambiguous C99 stdint.h types"), only in that case, the ambiguity is (unsurprisingly) about the 32 bit types, not the 64 bit ones.

My worry is that some future toolchain may include something else in this file and get other type conflicts. It really looks fragile.

Well, the GCC folks are quite careful not to depend on arbitrary user headers when the -ffreestanding option is set. Also, the real problem is the fact that Linux defines C99 types, but does so in an incompatible way. (I.e., one could also argue that the Linux typedefs should be based on GCC's builtin #defines of __INT64_TYPE, __UINT64_TYPE, etc if defined). So the chances of something similar reappearing all of a sudden are quite slim imo.

Regards,
Ard.

#7 lore raw

PATCH 1/1 arm64: add workaround for ambiguous C99 stdint.h types

Quoted 3 lines · Ard Biesheuvel
Quoted 2 lines · Catalin Marinas
Quoted 1 line · Ard Biesheuvel

On 17 February 2014 18:42, Catalin Marinas <[email protected]> wrote:

[...]

Quoted 27 lines
Quoted 17 lines
Quoted 10 lines
Quoted 4 lines

For other intrinsics that we use like __builtin_ctzl(), do we need to explicitly include gcc headers? I don't think we do and I really don't like such arm_neon.h include which brings in other user headers. Don't we have any work around this?

Well, I talked to the toolchain guys at the time and they really disliked the idea of coding directly against the __builtins because they are not considered a stable interface, especially because the interface that /is/ considered stable (arm_neon.h) is supported both on ARM and on arm64.

Than we don't use the Neon __builtins in the kernel.

Quoted 4 lines
Quoted 3 lines

My inbox only has some discussion in May last year on the linaro-kernel list without any clear conclusion (it could be that I deleted other emails).

Quoted 9 lines
Quoted 5 lines

There was some discussion, indeed, but for ARM, with the conclusion being the fix I mentioned in the patch: 09096f6a0ee2 ("ARM: 7822/1:
add workaround
for ambiguous C99 stdint.h types"), only in that case, the ambiguity is (unsurprisingly) about the 32 bit types, not the 64 bit ones.

My worry is that some future toolchain may include something else in this file and get other type conflicts. It really looks fragile.

Quoted 8 lines

Well, the GCC folks are quite careful not to depend on arbitrary user headers when the -ffreestanding option is set. Also, the real problem is the fact that Linux defines C99 types, but does so in an incompatible way. (I.e., one could also argue that the Linux typedefs should be based on GCC's builtin #defines of __INT64_TYPE, __UINT64_TYPE, etc if defined). So the chances of something similar reappearing all of a sudden are quite slim imo.

Perhaps this alternative approach would be better?

include/linux/types.h +3 -3
index 4d118ba11349..78344874fff0 100644
@@ -108,9 +108,9 @@ typedef             __u16           uint16_t;
 typedef                __u32           uint32_t;
 
 #if defined(__GNUC__)
-typedef                __u64           uint64_t;
-typedef                __u64           u_int64_t;
-typedef                __s64           int64_t;
+typedef                __UINT64_TYPE__ uint64_t;
+typedef                __UINT64_TYPE__ u_int64_t;
+typedef                __INT64_TYPE__  int64_t;
 #endif
 

I mean, we are already depending explicitly on __GNUC__, and GNUC tells us exactly which types it thinks should be used for typedef'ing [u]int64_t.

Anyway, I am perfectly happy to park this until a real use case shows up. I have some crypto coded up in intrinsics, but we won't know if it's fast enough until I manage to run it on actual hardware.

Regards,
Ard.