* [PATCH 1/2] x86, boot: #undef memcpy etc in string.c
@ 2017-03-15 21:48 Michael Davidson
2017-03-15 21:48 ` [PATCH 2/2] x86, boot: Use regparm=0 for memcpy and memset when using clang Michael Davidson
0 siblings, 1 reply; 4+ messages in thread
From: Michael Davidson @ 2017-03-15 21:48 UTC (permalink / raw)
To: H . Peter Anvin, Thomas Gleixner, Ingo Molnar
Cc: Alexander Potapenko, Dmitry Vyukov, x86, linux-kernel, Michael Davidson
undef memcpy and friends in boot/string.c so that the functions
defined here will have the correct names, otherwise we end up
up trying to redefine __builtin_memcpy etc.
Surprisingly, gcc allows this (and, helpfully, discards the
--builtin_ prefix from the function name when compiling it),
but clang does not.
Adding these #undef's appears to preserve what I assume was
the original intent of the code.
Signed-off-by: Michael Davidson <md@google.com>
---
arch/x86/boot/string.c | 9 +++++++++
1 file changed, 9 insertions(+)
diff --git a/arch/x86/boot/string.c b/arch/x86/boot/string.c
index 5457b02fc050..b40266850869 100644
--- a/arch/x86/boot/string.c
+++ b/arch/x86/boot/string.c
@@ -16,6 +16,15 @@
#include "ctype.h"
#include "string.h"
+/*
+ * Undef these macros so that the functions that we provide
+ * here will have the correct names regardless of how string.h
+ * may have chosen to #define them.
+ */
+#undef memcpy
+#undef memset
+#undef memcmp
+
int memcmp(const void *s1, const void *s2, size_t len)
{
bool diff;
--
2.12.0.367.g23dc2f6d3c-goog
^ permalink raw reply related [flat|nested] 4+ messages in thread
* [PATCH 2/2] x86, boot: Use regparm=0 for memcpy and memset when using clang
2017-03-15 21:48 [PATCH 1/2] x86, boot: #undef memcpy etc in string.c Michael Davidson
@ 2017-03-15 21:48 ` Michael Davidson
2017-03-16 7:46 ` Ingo Molnar
0 siblings, 1 reply; 4+ messages in thread
From: Michael Davidson @ 2017-03-15 21:48 UTC (permalink / raw)
To: H . Peter Anvin, Thomas Gleixner, Ingo Molnar
Cc: Alexander Potapenko, Dmitry Vyukov, x86, linux-kernel, Michael Davidson
Use the standard regparm=0 calling convention for memcpy and
memset when building with clang.
This is a work around for a long standing clang bug
(see https://llvm.org/bugs/show_bug.cgi?id=3997) where
clang always uses the standard regparm=0 calling convention
for any implcit calls to memcpy and memset that it generates
(eg for structure assignments and initialization) even if an
alternate calling convention such as regparm=3 has been specified.
Signed-off-by: Michael Davidson <md@google.com>
---
arch/x86/boot/copy.S | 15 +++++++++++++--
arch/x86/boot/string.h | 13 +++++++++++++
2 files changed, 26 insertions(+), 2 deletions(-)
diff --git a/arch/x86/boot/copy.S b/arch/x86/boot/copy.S
index 1eb7d298b47d..57142d1ad0d2 100644
--- a/arch/x86/boot/copy.S
+++ b/arch/x86/boot/copy.S
@@ -18,6 +18,12 @@
.text
GLOBAL(memcpy)
+#ifdef __clang__ /* Use normal ABI calling conventions */
+ movw 4(%esp), %ax
+ movw 8(%esp), %dx
+ movw 12(%esp), %cx
+#endif
+_memcpy:
pushw %si
pushw %di
movw %ax, %di
@@ -34,6 +40,11 @@ GLOBAL(memcpy)
ENDPROC(memcpy)
GLOBAL(memset)
+#ifdef __clang__ /* Use normal ABI calling conventions */
+ movw 4(%esp), %ax
+ movw 8(%esp), %dx
+ movw 12(%esp), %cx
+#endif
pushw %di
movw %ax, %di
movzbl %dl, %eax
@@ -52,7 +63,7 @@ GLOBAL(copy_from_fs)
pushw %ds
pushw %fs
popw %ds
- calll memcpy
+ calll _memcpy
popw %ds
retl
ENDPROC(copy_from_fs)
@@ -61,7 +72,7 @@ GLOBAL(copy_to_fs)
pushw %es
pushw %fs
popw %es
- calll memcpy
+ calll _memcpy
popw %es
retl
ENDPROC(copy_to_fs)
diff --git a/arch/x86/boot/string.h b/arch/x86/boot/string.h
index 113588ddb43f..e735cccb3fc8 100644
--- a/arch/x86/boot/string.h
+++ b/arch/x86/boot/string.h
@@ -6,8 +6,21 @@
#undef memset
#undef memcmp
+/*
+ * Use normal ABI calling conventions - i.e. regparm(0) -
+ * for memcpy() and memset() if we are building the real
+ * mode setup code with clang since clang may make implicit
+ * calls to these functions that assume regparm(0).
+ */
+#if defined(_SETUP) && defined(__clang__)
+void __attribute__((regparm(0))) *memcpy(void *dst, const void *src,
+ size_t len);
+void __attribute__((regparm(0))) *memset(void *dst, int c, size_t len);
+#else
void *memcpy(void *dst, const void *src, size_t len);
void *memset(void *dst, int c, size_t len);
+#endif
+
int memcmp(const void *s1, const void *s2, size_t len);
/*
--
2.12.0.367.g23dc2f6d3c-goog
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH 2/2] x86, boot: Use regparm=0 for memcpy and memset when using clang
2017-03-15 21:48 ` [PATCH 2/2] x86, boot: Use regparm=0 for memcpy and memset when using clang Michael Davidson
@ 2017-03-16 7:46 ` Ingo Molnar
2017-03-16 15:21 ` Michael Davidson
0 siblings, 1 reply; 4+ messages in thread
From: Ingo Molnar @ 2017-03-16 7:46 UTC (permalink / raw)
To: Michael Davidson
Cc: H . Peter Anvin, Thomas Gleixner, Ingo Molnar,
Alexander Potapenko, Dmitry Vyukov, x86, linux-kernel
* Michael Davidson <md@google.com> wrote:
> Use the standard regparm=0 calling convention for memcpy and
> memset when building with clang.
>
> This is a work around for a long standing clang bug
> (see https://llvm.org/bugs/show_bug.cgi?id=3997) where
> clang always uses the standard regparm=0 calling convention
> for any implcit calls to memcpy and memset that it generates
> (eg for structure assignments and initialization) even if an
> alternate calling convention such as regparm=3 has been specified.
>
> Signed-off-by: Michael Davidson <md@google.com>
> ---
> arch/x86/boot/copy.S | 15 +++++++++++++--
> arch/x86/boot/string.h | 13 +++++++++++++
> 2 files changed, 26 insertions(+), 2 deletions(-)
>
> diff --git a/arch/x86/boot/copy.S b/arch/x86/boot/copy.S
> index 1eb7d298b47d..57142d1ad0d2 100644
> --- a/arch/x86/boot/copy.S
> +++ b/arch/x86/boot/copy.S
> @@ -18,6 +18,12 @@
> .text
>
> GLOBAL(memcpy)
> +#ifdef __clang__ /* Use normal ABI calling conventions */
> + movw 4(%esp), %ax
> + movw 8(%esp), %dx
> + movw 12(%esp), %cx
> +#endif
> +_memcpy:
> pushw %si
> pushw %di
> movw %ax, %di
> @@ -34,6 +40,11 @@ GLOBAL(memcpy)
> ENDPROC(memcpy)
>
> GLOBAL(memset)
> +#ifdef __clang__ /* Use normal ABI calling conventions */
> + movw 4(%esp), %ax
> + movw 8(%esp), %dx
> + movw 12(%esp), %cx
> +#endif
I don't think we want to add such ugly workarounds for clang bugs in a piecemail
wise manner. If this was the _only_ workaround required to have a working kernel
built with clang then maybe, but there's no way to tell from this submission.
Thanks,
Ingo
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH 2/2] x86, boot: Use regparm=0 for memcpy and memset when using clang
2017-03-16 7:46 ` Ingo Molnar
@ 2017-03-16 15:21 ` Michael Davidson
0 siblings, 0 replies; 4+ messages in thread
From: Michael Davidson @ 2017-03-16 15:21 UTC (permalink / raw)
To: Ingo Molnar
Cc: H . Peter Anvin, Thomas Gleixner, Ingo Molnar,
Alexander Potapenko, Dmitry Vyukov, x86, LKML
On Thu, Mar 16, 2017 at 12:46 AM, Ingo Molnar <mingo@kernel.org> wrote:
>
> I don't think we want to add such ugly workarounds for clang bugs in a piecemail
> wise manner. If this was the _only_ workaround required to have a working kernel
> built with clang then maybe, but there's no way to tell from this submission.
>
Apologies for the lack of context. This change, and the #undef of mem*
in x86/boot/string.c
are actually the only patches that are absolutely needed to get an
x86_64 kernel to
build and boot successfully with clang provided that you also arrange
for clang
be invoked with the -no-integrated-as option. In practice it is also
necessary to disable
a few more clang warnings to make the build tolerable.
I will send out a more complete set of patches later today which deal
with those other
issues.
As far as the specific issue of this particular clang bug is concerned
I did also consider
adding a configuration option to simply not build the real mode boot
code at all but
that looked like it would require much more extensive surgery.
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2017-03-16 15:23 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-03-15 21:48 [PATCH 1/2] x86, boot: #undef memcpy etc in string.c Michael Davidson
2017-03-15 21:48 ` [PATCH 2/2] x86, boot: Use regparm=0 for memcpy and memset when using clang Michael Davidson
2017-03-16 7:46 ` Ingo Molnar
2017-03-16 15:21 ` Michael Davidson
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.