Skip to contentSkip to conversation

clang: fixes for the indirect thunk

PATCH In review Roger Pau Monne 2 participants 11 messages Jan 24, 2018 – Jan 25, 2018

lore
Share & tools
lore
#0 lore raw

cover clang: fixes for the indirect thunk

Hello,

The following two patches fix building Xen with clang and the indirect thunk.

Patch 2 is not strictly needed, because when building with clang we still compile assembly only files with -no-integrated-as, but it's a nice to have so that we don't regress more in assembly only code.

Thanks, Roger.

Roger Pau Monne (2):
x86/clang: fix build with indirect thunks
x86: fix indirect thunk usage of CONFIG_INDIRECT_THUNK

 xen/Rules.mk                           |  6 ++++--
 xen/arch/x86/extable.c                 |  3 ++-
 xen/arch/x86/x86_emulate/x86_emulate.c |  3 ++-
 xen/common/wait.c                      |  1 +
 xen/include/asm-x86/asm_defns.h        | 10 +++++++---
 5 files changed, 16 insertions(+), 7 deletions(-)
Signature · 7 lines
2.15.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#1 lore raw

PATCH 1/2 x86/clang: fix build with indirect thunks

The build with clang is currently broken because clang requires asm macros to be declared inside the same inline asm declaration where they are used.

In order to fix this always include indirect_thunk_asm.h in the same asm declaration where it's being used.

---
Cc: Andrew Cooper <[email protected]>
Cc: George Dunlap <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Konrad Rzeszutek Wilk <[email protected]>
Cc: Stefano Stabellini <[email protected]>
Cc: Tim Deegan <[email protected]>
Cc: Wei Liu <[email protected]>

---
 xen/Rules.mk                           | 6 ++++--
 xen/arch/x86/extable.c                 | 3 ++-
 xen/arch/x86/x86_emulate/x86_emulate.c | 3 ++-
 xen/common/wait.c                      | 1 +
 xen/include/asm-x86/asm_defns.h        | 7 ++++---
 5 files changed, 13 insertions(+), 7 deletions(-)
xen/Rules.mk +4 -2
index 3cf40754a6..1bd3fa1773 100644
@@ -66,8 +66,10 @@ endif
 
 AFLAGS-y                += -D__ASSEMBLY__
 
-# Clang's built-in assembler can't handle embedded .include's
-CFLAGS-$(clang)         += -no-integrated-as
+# Clang's built-in assembler doesn't understand assembler directives without
+# an absolute value:
+# https://bugs.llvm.org/show_bug.cgi?id=27369
+AFLAGS-$(clang)         += -no-integrated-as
 
 ALL_OBJS := $(ALL_OBJS-y)
 
xen/arch/x86/extable.c +2 -1
index 72f30d9060..30893c3770 100644
@@ -158,7 +158,8 @@ static int __init stub_selftest(void)
         memcpy(ptr, tests[i].opc, ARRAY_SIZE(tests[i].opc));
         unmap_domain_page(ptr);
 
-        asm volatile ( "INDIRECT_CALL %[stb]\n"
+        asm volatile ( INCLUDE_INDIRECT_THUNK
+                       "INDIRECT_CALL %[stb]\n"
                        ".Lret%=:\n\t"
                        ".pushsection .fixup,\"ax\"\n"
                        ".Lfix%=:\n\t"
xen/arch/x86/x86_emulate/x86_emulate.c +2 -1
index ff0a003902..e525e6bb0d 100644
@@ -867,7 +867,8 @@ static inline int mkec(uint8_t e, int32_t ec, ...)
 #ifdef __XEN__
 # define invoke_stub(pre, post, constraints...) do {                    \
     union stub_exception_token res_ = { .raw = ~0 };                    \
-    asm volatile ( pre "\n\tINDIRECT_CALL %[stub]\n\t" post "\n"        \
+    asm volatile ( INCLUDE_INDIRECT_THUNK                               \
+                   pre "\n\tINDIRECT_CALL %[stub]\n\t" post "\n"        \
                    ".Lret%=:\n\t"                                       \
                    ".pushsection .fixup,\"ax\"\n"                       \
                    ".Lfix%=:\n\t"                                       \
xen/common/wait.c +1 -0
index a57bc10d61..91dcd46342 100644
@@ -207,6 +207,7 @@ void check_wakeup_from_wait(void)
      * restored from the stack, so are available for use here.
      */
     asm volatile (
+        INCLUDE_INDIRECT_THUNK
         "mov %1,%%"__OP"sp; INDIRECT_JMP %[ip]"
         : : "S" (wqv->stack), "D" (wqv->esp),
           "c" ((char *)get_cpu_info() - (char *)wqv->esp),
xen/include/asm-x86/asm_defns.h +4 -3
index d2d91ca1fa..627a544326 100644
@@ -16,9 +16,10 @@
 #ifdef __ASSEMBLY__
 # include <asm/indirect_thunk_asm.h>
 #else
-asm ( "\t.equ CONFIG_INDIRECT_THUNK, "
-      __stringify(IS_ENABLED(CONFIG_INDIRECT_THUNK)) );
-asm ( "\t.include \"asm/indirect_thunk_asm.h\"" );
+#define INCLUDE_INDIRECT_THUNK                                  \
+    "\t.equ CONFIG_INDIRECT_THUNK, "                            \
+    __stringify(IS_ENABLED(CONFIG_INDIRECT_THUNK)) "\n"         \
+    "\t.include \"asm/indirect_thunk_asm.h\"\n"
 #endif
 
 #ifndef __ASSEMBLY__
Signature · 7 lines
2.15.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#2 lore raw

Re: [PATCH 1/2] x86/clang: fix build with indirect thunks

Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 24.01.18 at 16:48, <[email protected]> wrote:

The build with clang is currently broken because clang requires asm macros to be declared inside the same inline asm declaration where they are used.

I don't understand this: What if I have two asm()-s needing it? Does this need to be done in each one? I'd expect this to result in duplicate definitions on gas then (which may or may not be benign).

xen/Rules.mk endif · line 72

Quoted 12 lines
xen/Rules.mk +4 -2
@@ -66,8 +66,10 @@ endif
 
 AFLAGS-y                += -D__ASSEMBLY__
 
-# Clang's built-in assembler can't handle embedded .include's
-CFLAGS-$(clang)         += -no-integrated-as
+# Clang's built-in assembler doesn't understand assembler directives without
+# an absolute value:
+# https://bugs.llvm.org/show_bug.cgi?id=27369 
+AFLAGS-$(clang)         += -no-integrated-as

I also don't understand this - when you switch (back) to AFLAGS, you don't affect C files. Furthermore without using its integrated assembler for C files at present - how is the build broken? Is the description of the change perhaps in need of some re-work (and maybe the title as well)?

Nor am I of the opinion that the comment above is really correct - I'm sure there are directives where their assembler supports non- constant values (.include being the obvious first case in the context here).

And finally, if you switch back to use AFLAGS here, you should either restore the original comment as well, or explain in the description why it isn't applicable anymore.

xen/arch/x86/extable.c static int __init stub_selftest(void) · line 162

Quoted 9 lines
xen/arch/x86/extable.c +2 -1
@@ -158,7 +158,8 @@ static int __init stub_selftest(void)
         memcpy(ptr, tests[i].opc, ARRAY_SIZE(tests[i].opc));
         unmap_domain_page(ptr);
 
-        asm volatile ( "INDIRECT_CALL %[stb]\n"
+        asm volatile ( INCLUDE_INDIRECT_THUNK
+                       "INDIRECT_CALL %[stb]\n"

Besides this being somewhat ugly, having to remember to add this going forward, should any new indirect calls be added, is surely prone to be forgotten.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#3 lore raw

Re: [PATCH 1/2] x86/clang: fix build with indirect thunks

Quoted 8 lines · Jan Beulich
Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 24.01.18 at 16:48, <[email protected]> wrote:

The build with clang is currently broken because clang requires asm macros to be declared inside the same inline asm declaration where they are used.

I don't understand this: What if I have two asm()-s needing it? Does this need to be done in each one? I'd expect this to result in duplicate definitions on gas then (which may or may not be benign).

It's quite fun, this approach works fine with clang regardless of the number of asm()-s needing it. It doesn't complain about duplicate macros or anything. OTOH gcc complains with "Error: Macro `foo' was already defined".

One option might be to guard indirect_thunk_asm.h with:

.ifndef INDIRECT_THUNK_ASM
.equ INDIRECT_THUNK_ASM, 1

...

.endif

Not the best, but should do the trick I guess...

Quoted 6 lines
Quoted 6 lines
xen/Rules.mk +0 -0
@@ -66,8 +66,10 @@ endif
 
 AFLAGS-y                += -D__ASSEMBLY__
 
Quoted 9 lines

xen/Rules.mk endif · line 72

Quoted 6 lines
xen/Rules.mk +4 -2
-# Clang's built-in assembler can't handle embedded .include's
-CFLAGS-$(clang)         += -no-integrated-as
+# Clang's built-in assembler doesn't understand assembler directives without
+# an absolute value:
+# https://bugs.llvm.org/show_bug.cgi?id=27369 
+AFLAGS-$(clang)         += -no-integrated-as

I also don't understand this - when you switch (back) to AFLAGS, you don't affect C files.

Yes, it affects C files because now they are assembled using the integrated as, not the external one.

Quoted 2 lines

Furthermore without using its integrated assembler for C files at present - how is the build broken?

Not using the integrated as is a workaround for using the indirect thunk. If we can manage to get the indirect thunk to work with the integrated as there's no need to use the external one for C files.

Quoted 8 lines

Is the description of
the change perhaps in need of some re-work (and maybe the title as well)?

Nor am I of the opinion that the comment above is really correct - I'm sure there are directives where their assembler supports non- constant values (.include being the obvious first case in the context here).

Right, clang only accepts constant values for assembler directives like .rept and .skip.

Quoted 3 lines

And finally, if you switch back to use AFLAGS here, you should either restore the original comment as well, or explain in the description why it isn't applicable anymore.

I will check, but I'm fairly sure that all the clang versions that Xen supports (>= 3.5) support .code16/.code32/.code64.

Quoted 6 lines
Quoted 6 lines
xen/arch/x86/extable.c +0 -0
@@ -158,7 +158,8 @@ static int __init stub_selftest(void)
         memcpy(ptr, tests[i].opc, ARRAY_SIZE(tests[i].opc));
         unmap_domain_page(ptr);
 
Quoted 7 lines

xen/arch/x86/extable.c static int __init stub_selftest(void) · line 162

Quoted 3 lines
xen/arch/x86/extable.c +2 -1
-        asm volatile ( "INDIRECT_CALL %[stb]\n"
+        asm volatile ( INCLUDE_INDIRECT_THUNK
+                       "INDIRECT_CALL %[stb]\n"

Besides this being somewhat ugly, having to remember to add this going forward, should any new indirect calls be added, is surely prone to be forgotten.

I don't have a better solution ATM I'm afraid.

Roger.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#4 lore raw

Re: [PATCH 1/2] x86/clang: fix build with indirect thunks

Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 24.01.18 at 18:06, <[email protected]> wrote:
Quoted 5 lines · Jan Beulich
Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 24.01.18 at 16:48, <[email protected]> wrote:

The build with clang is currently broken because clang requires asm macros to be declared inside the same inline asm declaration where they are used.

Quoted 8 lines
Quoted 3 lines

I don't understand this: What if I have two asm()-s needing it? Does this need to be done in each one? I'd expect this to result in duplicate definitions on gas then (which may or may not be benign).

It's quite fun, this approach works fine with clang regardless of the number of asm()-s needing it. It doesn't complain about duplicate macros or anything. OTOH gcc complains with "Error: Macro `foo' was already defined".

And does clang need it in each asm()? Albeit - since the order functions are being output is undefined, it probably should go into each one. Hence the answer would only affect whether ...

Quoted 8 lines

One option might be to guard indirect_thunk_asm.h with:

.ifndef INDIRECT_THUNK_ASM
.equ INDIRECT_THUNK_ASM, 1

...

.endif

... this would be needed conditionally for gas, or unconditionally.

Quoted 19 lines
Quoted 6 lines
Quoted 6 lines
xen/Rules.mk +0 -0
@@ -66,8 +66,10 @@ endif
 
 AFLAGS-y                += -D__ASSEMBLY__
 
Quoted 9 lines

xen/Rules.mk endif · line 72

Quoted 6 lines
xen/Rules.mk +4 -2
-# Clang's built-in assembler can't handle embedded .include's
-CFLAGS-$(clang)         += -no-integrated-as
+# Clang's built-in assembler doesn't understand assembler directives without
+# an absolute value:
+# https://bugs.llvm.org/show_bug.cgi?id=27369 
+AFLAGS-$(clang)         += -no-integrated-as

I also don't understand this - when you switch (back) to AFLAGS, you don't affect C files.

Yes, it affects C files because now they are assembled using the integrated as, not the external one.

Quoted 6 lines
Quoted 2 lines

Furthermore without using its integrated assembler for C files at present - how is the build broken?

Not using the integrated as is a workaround for using the indirect thunk. If we can manage to get the indirect thunk to work with the integrated as there's no need to use the external one for C files.

Well, that'll hopefully become more clear with a more precise description.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#5 lore raw

Re: [PATCH 1/2] x86/clang: fix build with indirect thunks

Quoted 11 lines · Jan Beulich
Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 24.01.18 at 18:06, <[email protected]> wrote:
Quoted 8 lines · Jan Beulich
Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 24.01.18 at 16:48, <[email protected]> wrote:

The build with clang is currently broken because clang requires asm macros to be declared inside the same inline asm declaration where they are used.

I don't understand this: What if I have two asm()-s needing it? Does this need to be done in each one? I'd expect this to result in duplicate definitions on gas then (which may or may not be benign).

Quoted 6 lines
Quoted 4 lines

It's quite fun, this approach works fine with clang regardless of the number of asm()-s needing it. It doesn't complain about duplicate macros or anything. OTOH gcc complains with "Error: Macro `foo' was already defined".

And does clang need it in each asm()?

Yes. AFAICT .includes inside of asm()-s in clang are only valid inside the same asm().

Quoted 6 lines

Albeit - since the order
functions are being output is undefined, it probably should go into each one. Hence the answer would only affect whether ...

Quoted 2 lines

One option might be to guard indirect_thunk_asm.h with:

Quoted 8 lines
Quoted 6 lines

.ifndef INDIRECT_THUNK_ASM
.equ INDIRECT_THUNK_ASM, 1

...

.endif

... this would be needed conditionally for gas, or unconditionally.

The guard is needed unconditionally for gcc, or else having two instances of the asm() in the same file is not going to work.

For gcc we could also avoid defining INCLUDE_INDIRECT_THUNK and keep the same approach that's used currently, but I would rather have gcc and clang using the same approach if possible.

Thanks, Roger.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#6 lore raw

PATCH 2/2 x86: fix indirect thunk usage of CONFIG_INDIRECT_THUNK

In reply to Roger Pau Monne

When indirect_thunk_asm.h is instantiated directly into assembly files CONFIG_INDIRECT_THUNK might not be defined, and thus using .if against it is wrong.

Add a check to define CONFIG_INDIRECT_THUNK to 0 if not defined, so that using .if CONFIG_INDIRECT_THUNK is always correct.

This suppresses the following clang error:

<instantiation>:8:9: error: expected absolute expression

    .if CONFIG_INDIRECT_THUNK == 1
        ^

<instantiation>:1:1: note: while in macro instantiation INDIRECT_BRANCH call %rdx
^
entry.S:589:9: note: while in macro instantiation

        INDIRECT_CALL %rdx
        ^

---
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>

---
 xen/include/asm-x86/asm_defns.h | 3 +++
 1 file changed, 3 insertions(+)
xen/include/asm-x86/asm_defns.h +3 -0
index 627a544326..f7e53627ff 100644
@@ -14,6 +14,9 @@
 #include <asm/alternative.h>
 
 #ifdef __ASSEMBLY__
+# ifndef CONFIG_INDIRECT_THUNK
+#  define CONFIG_INDIRECT_THUNK 0
+# endif
 # include <asm/indirect_thunk_asm.h>
 #else
 #define INCLUDE_INDIRECT_THUNK                                  \
Signature · 7 lines
2.15.1


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#7 lore raw

Re: [PATCH 2/2] x86: fix indirect thunk usage of CONFIG_INDIRECT_THUNK

Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 24.01.18 at 16:48, <[email protected]> wrote:

When indirect_thunk_asm.h is instantiated directly into assembly files CONFIG_INDIRECT_THUNK might not be defined, and thus using .if against it is wrong.

Add a check to define CONFIG_INDIRECT_THUNK to 0 if not defined, so that using .if CONFIG_INDIRECT_THUNK is always correct.

This suppresses the following clang error:

<instantiation>:8:9: error: expected absolute expression

Quoted 8 lines
    .if CONFIG_INDIRECT_THUNK == 1
        ^

<instantiation>:1:1: note: while in macro instantiation INDIRECT_BRANCH call %rdx
^
entry.S:589:9: note: while in macro instantiation

        INDIRECT_CALL %rdx
        ^

Why is the same no problem with gas? It wants constant expressions with .if too, after all.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#8 lore raw

Re: [PATCH 2/2] x86: fix indirect thunk usage of CONFIG_INDIRECT_THUNK

Quoted 20 lines · Jan Beulich
Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 24.01.18 at 16:48, <[email protected]> wrote:

When indirect_thunk_asm.h is instantiated directly into assembly files CONFIG_INDIRECT_THUNK might not be defined, and thus using .if against it is wrong.

Add a check to define CONFIG_INDIRECT_THUNK to 0 if not defined, so that using .if CONFIG_INDIRECT_THUNK is always correct.

This suppresses the following clang error:

<instantiation>:8:9: error: expected absolute expression

Quoted 8 lines
    .if CONFIG_INDIRECT_THUNK == 1
        ^

<instantiation>:1:1: note: while in macro instantiation INDIRECT_BRANCH call %rdx
^
entry.S:589:9: note: while in macro instantiation

        INDIRECT_CALL %rdx
        ^
Quoted 2 lines

Why is the same no problem with gas? It wants constant expressions with .if too, after all.

Right, and I cannot figure out why it works when using -no-integrated-as and not when not using it. The more that I cannot see CONFIG_INDIRECT_THUNK being unconditionally defined to either 0 or
1. I guess I'm missing something.

Roger.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#9 lore raw

Re: [PATCH 2/2] x86: fix indirect thunk usage of CONFIG_INDIRECT_THUNK

Quoted 22 lines
Quoted 1 line
Quoted 1 line · On 24.01.18 at 17:52, <[email protected]> wrote:
Quoted 20 lines · Jan Beulich
Quoted 19 lines
Quoted 1 line
Quoted 1 line · On 24.01.18 at 16:48, <[email protected]> wrote:

When indirect_thunk_asm.h is instantiated directly into assembly files CONFIG_INDIRECT_THUNK might not be defined, and thus using .if against it is wrong.

Add a check to define CONFIG_INDIRECT_THUNK to 0 if not defined, so that using .if CONFIG_INDIRECT_THUNK is always correct.

This suppresses the following clang error:

<instantiation>:8:9: error: expected absolute expression

    .if CONFIG_INDIRECT_THUNK == 1
        ^

<instantiation>:1:1: note: while in macro instantiation INDIRECT_BRANCH call %rdx
^
entry.S:589:9: note: while in macro instantiation

        INDIRECT_CALL %rdx
        ^
Quoted 7 lines
Quoted 2 lines

Why is the same no problem with gas? It wants constant expressions with .if too, after all.

Right, and I cannot figure out why it works when using -no-integrated-as and not when not using it. The more that I cannot see CONFIG_INDIRECT_THUNK being unconditionally defined to either 0 or
1. I guess I'm missing something.

Hmm, it looks to be (half way?) intended behavior for gas to consider "<symbol> == <constant>" always false if <symbol> is undefined (and similarly "<symbol> != <constant>" always true). I'll inquire on the binutils list if this is really meant to remain that way forever, as it's not spelled out that way in the docs.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#10 lore raw

Re: [PATCH 2/2] x86: fix indirect thunk usage of CONFIG_INDIRECT_THUNK

In reply to Roger Pau Monné
Quoted 21 lines · Roger Pau Monné
Quoted 20 lines · Jan Beulich
Quoted 19 lines
Quoted 1 line
Quoted 1 line · On 24.01.18 at 16:48, <[email protected]> wrote:

When indirect_thunk_asm.h is instantiated directly into assembly files CONFIG_INDIRECT_THUNK might not be defined, and thus using .if against it is wrong.

Add a check to define CONFIG_INDIRECT_THUNK to 0 if not defined, so that using .if CONFIG_INDIRECT_THUNK is always correct.

This suppresses the following clang error:

<instantiation>:8:9: error: expected absolute expression

    .if CONFIG_INDIRECT_THUNK == 1
        ^

<instantiation>:1:1: note: while in macro instantiation INDIRECT_BRANCH call %rdx
^
entry.S:589:9: note: while in macro instantiation

        INDIRECT_CALL %rdx
        ^
Quoted 7 lines
Quoted 2 lines

Why is the same no problem with gas? It wants constant expressions with .if too, after all.

Right, and I cannot figure out why it works when using -no-integrated-as and not when not using it. The more that I cannot see CONFIG_INDIRECT_THUNK being unconditionally defined to either 0 or
1. I guess I'm missing something.

So given the following asm snippet:

# cat snip.S
.if FOO == 1
.error "FOO"
.endif

Attempting to compile it with different compilers/assemblers:

# clang -o snip.o -c snip.S
snip.S:1:5: error: expected absolute expression
.if FOO == 1
    ^
snip.S:2:1: error: FOO
.error "FOO"
^
# gcc -o snip.o -c snip.S
# as -o snip.o snip.S

So there's clearly something different in how as/gcc handle .if directives when they reference undefined symbols versus clang.

I can expand the commit message to reflect/include the above, but I think the change still stands.

Thanks, Roger.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel