Skip to contentSkip to conversation

KVM: s390: get rid of register asm usage

PATCH Applied Heiko Carstens 5 participants 12 messages Jun 21, 2021 – Jun 22, 2021

Subsystems (3)
lore
Share & tools
lore

Heiko Carstens

author maintainer
#0 lore raw

PATCH 1/1 KVM: s390: get rid of register asm usage

Using register asm statements has been proven to be very error prone, especially when using code instrumentation where gcc may add function calls, which clobbers register contents in an unexpected way.

Therefore get rid of register asm statements in kvm code, even though there is currently nothing wrong with them. This way we know for sure that this bug class won't be introduced here.

---
 arch/s390/kvm/kvm-s390.c | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)
arch/s390/kvm/kvm-s390.c +9 -9
index 1296fc10f80c..4b7b24f07790 100644
@@ -329,31 +329,31 @@ static void allow_cpu_feat(unsigned long nr)
 
 static inline int plo_test_bit(unsigned char nr)
 {
-	register unsigned long r0 asm("0") = (unsigned long) nr | 0x100;
+	unsigned long function = (unsigned long) nr | 0x100;
 	int cc;
 
 	asm volatile(
+		"	lgr	0,%[function]\n"
 		/* Parameter registers are ignored for "test bit" */
 		"	plo	0,0,0,0(0)\n"
 		"	ipm	%0\n"
 		"	srl	%0,28\n"
 		: "=d" (cc)
-		: "d" (r0)
-		: "cc");
+		: [function] "d" (function)
+		: "cc", "0");
 	return cc == 0;
 }
 
 static __always_inline void __insn32_query(unsigned int opcode, u8 *query)
 {
-	register unsigned long r0 asm("0") = 0;	/* query function */
-	register unsigned long r1 asm("1") = (unsigned long) query;
-
 	asm volatile(
-		/* Parameter regs are ignored */
+		"	lghi	0,0\n"
+		"	lgr	1,%[query]\n"
+		/* Parameter registers are ignored */
 		"	.insn	rrf,%[opc] << 16,2,4,6,0\n"
 		:
-		: "d" (r0), "a" (r1), [opc] "i" (opcode)
-		: "cc", "memory");
+		: [query] "d" ((unsigned long)query), [opc] "i" (opcode)
+		: "cc", "memory", "0", "1");
 }
 
 #define INSN_SORTL 0xb938
Signature · 1 line
2.25.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · Heiko Carstens

Using register asm statements has been proven to be very error prone, especially when using code instrumentation where gcc may add function calls, which clobbers register contents in an unexpected way.

Therefore get rid of register asm statements in kvm code, even though there is currently nothing wrong with them. This way we know for sure that this bug class won't be introduced here.

Quoted 5 lines
---
 arch/s390/kvm/kvm-s390.c | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)
maintainer Reviewed-by on v1 1/1
#2 lore raw
In reply to Heiko Carstens
Quoted 10 lines · On Mon, 21 Jun 2021 16:03:56 +0200 Heiko Carstens <[email protected]> wrote:

Using register asm statements has been proven to be very error prone, especially when using code instrumentation where gcc may add function calls, which clobbers register contents in an unexpected way.

Therefore get rid of register asm statements in kvm code, even though there is currently nothing wrong with them. This way we know for sure that this bug class won't be introduced here.

Quoted 52 lines
---
 arch/s390/kvm/kvm-s390.c | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)
arch/s390/kvm/kvm-s390.c +4 -4
index 1296fc10f80c..4b7b24f07790 100644
@@ -329,31 +329,31 @@ static void allow_cpu_feat(unsigned long nr)
 
 static inline int plo_test_bit(unsigned char nr)
 {
-	register unsigned long r0 asm("0") = (unsigned long) nr |
 0x100;
+	unsigned long function = (unsigned long) nr | 0x100;
 	int cc;
 
 	asm volatile(
+		"	lgr	0,%[function]\n"
 		/* Parameter registers are ignored for "test bit" */
 		"	plo	0,0,0,0(0)\n"
 		"	ipm	%0\n"
 		"	srl	%0,28\n"
 		: "=d" (cc)
-		: "d" (r0)
-		: "cc");
+		: [function] "d" (function)
+		: "cc", "0");
 	return cc == 0;
 }
 
 static __always_inline void __insn32_query(unsigned int opcode, u8
 *query) {
-	register unsigned long r0 asm("0") = 0;	/* query

function */

arch/s390/kvm/kvm-s390.c +4 -5
-	register unsigned long r1 asm("1") = (unsigned long) query;
-
 	asm volatile(
-		/* Parameter regs are ignored */
+		"	lghi	0,0\n"
+		"	lgr	1,%[query]\n"
+		/* Parameter registers are ignored */
 		"	.insn	rrf,%[opc] << 16,2,4,6,0\n"
 		:
-		: "d" (r0), "a" (r1), [opc] "i" (opcode)
-		: "cc", "memory");
+		: [query] "d" ((unsigned long)query), [opc] "i"

(opcode)

arch/s390/kvm/kvm-s390.c +1 -0
+		: "cc", "memory", "0", "1");
 }
 
 #define INSN_SORTL 0xb938
Reviewed-by on v1 1/1
#3 lore raw
In reply to Heiko Carstens
Quoted 45 lines · Heiko Carstens

Using register asm statements has been proven to be very error prone, especially when using code instrumentation where gcc may add function calls, which clobbers register contents in an unexpected way.

Therefore get rid of register asm statements in kvm code, even though there is currently nothing wrong with them. This way we know for sure that this bug class won't be introduced here.

---
  arch/s390/kvm/kvm-s390.c | 18 +++++++++---------
  1 file changed, 9 insertions(+), 9 deletions(-)
arch/s390/kvm/kvm-s390.c +4 -6
index 1296fc10f80c..4b7b24f07790 100644
@@ -329,31 +329,31 @@ static void allow_cpu_feat(unsigned long nr)
 
  static inline int plo_test_bit(unsigned char nr)
  {
-	register unsigned long r0 asm("0") = (unsigned long) nr | 0x100;
+	unsigned long function = (unsigned long) nr | 0x100;
  	int cc;
 
  	asm volatile(
+		"	lgr	0,%[function]\n"
  		/* Parameter registers are ignored for "test bit" */
  		"	plo	0,0,0,0(0)\n"
  		"	ipm	%0\n"
  		"	srl	%0,28\n"
  		: "=d" (cc)
-		: "d" (r0)
-		: "cc");
+		: [function] "d" (function)
+		: "cc", "0");
  	return cc == 0;
  }
 
  static __always_inline void __insn32_query(unsigned int opcode, u8 *query)
  {
-	register unsigned long r0 asm("0") = 0;	/* query function */
-	register unsigned long r1 asm("1") = (unsigned long) query;
-

arch/s390/kvm/kvm-s390.c static void allow_cpu_feat(unsigned long nr) · line 355

Quoted 10 lines
arch/s390/kvm/kvm-s390.c +4 -3
  	asm volatile(
-		/* Parameter regs are ignored */
+		"	lghi	0,0\n"
+		"	lgr	1,%[query]\n"
+		/* Parameter registers are ignored */
  		"	.insn	rrf,%[opc] << 16,2,4,6,0\n"
  		:
-		: "d" (r0), "a" (r1), [opc] "i" (opcode)
-		: "cc", "memory");
+		: [query] "d" ((unsigned long)query), [opc] "i" (opcode)

Wouldn't it be better to keep the "a" constraint instead of "d" to avoid that the compiler ever passes the "query" value in r0 ? Otherwise the query value might get trashed if it is passed in r0...

arch/s390/kvm/kvm-s390.c static void allow_cpu_feat(unsigned long nr) · line 357

Quoted 2 lines
arch/s390/kvm/kvm-s390.c +1 -0
+		: "cc", "memory", "0", "1");
  }

With "a" instead of "d":

#4 lore raw
Quoted 48 lines · Thomas Huth
Quoted 47 lines · Heiko Carstens

Using register asm statements has been proven to be very error prone, especially when using code instrumentation where gcc may add function calls, which clobbers register contents in an unexpected way.

Therefore get rid of register asm statements in kvm code, even though there is currently nothing wrong with them. This way we know for sure that this bug class won't be introduced here.

---
  arch/s390/kvm/kvm-s390.c | 18 +++++++++---------

  1 file changed, 9 insertions(+), 9 deletions(-)
arch/s390/kvm/kvm-s390.c +1 -1
index 1296fc10f80c..4b7b24f07790 100644
@@ -329,31 +329,31 @@ static void allow_cpu_feat(unsigned long nr)
   static inline int plo_test_bit(unsigned char nr)
   {
-    register unsigned long r0 asm("0") = (unsigned long) nr | 0x100;
+    unsigned long function = (unsigned long) nr | 0x100;
       int cc;

      asm volatile(
+        "    lgr    0,%[function]\n"
          /* Parameter registers are ignored for "test bit" */
          "    plo    0,0,0,0(0)\n"
          "    ipm    %0\n"
          "    srl    %0,28\n"
          : "=d" (cc)
-        : "d" (r0)
-        : "cc");
+        : [function] "d" (function)
+        : "cc", "0");
      return cc == 0;
  }
  static __always_inline void __insn32_query(unsigned int opcode, u8 *query) {
-    register unsigned long r0 asm("0") = 0;    /* query function */ -    register unsigned long r1 asm("1") = (unsigned long) query; -
      asm volatile(
-        /* Parameter regs are ignored */
+        "    lghi    0,0\n"
+        "    lgr    1,%[query]\n"
+        /* Parameter registers are ignored */

Quoted 8 lines
Quoted 5 lines

          "    .insn    rrf,%[opc] << 16,2,4,6,0\n"
          :
-        : "d" (r0), "a" (r1), [opc] "i" (opcode)
-        : "cc", "memory");
+        : [query] "d" ((unsigned long)query), [opc] "i" (opcode)

Wouldn't it be better to keep the "a" constraint instead of "d" to avoid that the compiler ever passes the "query" value in r0 ? Otherwise the query value might get trashed if it is passed in r0...

I first thought the same, but if you look closely the value is only used by the lgr, to load the value finally into r1. So d is correct as lgr can take all registers.

#5 lore raw
Quoted 60 lines · Christian Borntraeger
Quoted 48 lines · Thomas Huth
Quoted 47 lines · Heiko Carstens

Using register asm statements has been proven to be very error prone, especially when using code instrumentation where gcc may add function calls, which clobbers register contents in an unexpected way.

Therefore get rid of register asm statements in kvm code, even though there is currently nothing wrong with them. This way we know for sure that this bug class won't be introduced here.

---
  arch/s390/kvm/kvm-s390.c | 18 +++++++++---------

  1 file changed, 9 insertions(+), 9 deletions(-)
arch/s390/kvm/kvm-s390.c +1 -1
index 1296fc10f80c..4b7b24f07790 100644
@@ -329,31 +329,31 @@ static void allow_cpu_feat(unsigned long nr)
   static inline int plo_test_bit(unsigned char nr)
   {
-    register unsigned long r0 asm("0") = (unsigned long) nr | 0x100;
+    unsigned long function = (unsigned long) nr | 0x100;
       int cc;

      asm volatile(
+        "    lgr    0,%[function]\n"
          /* Parameter registers are ignored for "test bit" */
          "    plo    0,0,0,0(0)\n"
          "    ipm    %0\n"
          "    srl    %0,28\n"
          : "=d" (cc)
-        : "d" (r0)
-        : "cc");
+        : [function] "d" (function)
+        : "cc", "0");
      return cc == 0;
  }
  static __always_inline void __insn32_query(unsigned int opcode, u8 *query) {
-    register unsigned long r0 asm("0") = 0;    /* query function */ -    register unsigned long r1 asm("1") = (unsigned long) query; -
      asm volatile(
-        /* Parameter regs are ignored */
+        "    lghi    0,0\n"
+        "    lgr    1,%[query]\n"
+        /* Parameter registers are ignored */

Quoted 8 lines
Quoted 5 lines

          "    .insn    rrf,%[opc] << 16,2,4,6,0\n"
          :
-        : "d" (r0), "a" (r1), [opc] "i" (opcode)
-        : "cc", "memory");
+        : [query] "d" ((unsigned long)query), [opc] "i" (opcode)

Wouldn't it be better to keep the "a" constraint instead of "d" to avoid that the compiler ever passes the "query" value in r0 ? Otherwise the query value might get trashed if it is passed in r0...

Quoted 2 lines

I first thought the same, but if you look closely the value is only used by the lgr, to load the value finally into r1. So d is correct as lgr can take all registers.

But what about the "lghi 0,0" right in front of it? ... I've got the feeling that I'm missing something here...
Heiko, maybe you could at least swap the initialization of r0 and r1, then I'd feel a little bit better...

Thomas

#6 lore raw
Quoted 61 lines · Thomas Huth
Quoted 60 lines · Christian Borntraeger
Quoted 56 lines · Thomas Huth
Quoted 44 lines · Heiko Carstens

Using register asm statements has been proven to be very error prone, especially when using code instrumentation where gcc may add function calls, which clobbers register contents in an unexpected way.

Therefore get rid of register asm statements in kvm code, even though there is currently nothing wrong with them. This way we know for sure that this bug class won't be introduced here.

---
  arch/s390/kvm/kvm-s390.c | 18 +++++++++---------

  1 file changed, 9 insertions(+), 9 deletions(-)
arch/s390/kvm/kvm-s390.c +1 -1
index 1296fc10f80c..4b7b24f07790 100644
@@ -329,31 +329,31 @@ static void allow_cpu_feat(unsigned long nr)
   static inline int plo_test_bit(unsigned char nr)
   {
-    register unsigned long r0 asm("0") = (unsigned long) nr | 0x100;
+    unsigned long function = (unsigned long) nr | 0x100;
       int cc;

      asm volatile(
+        "    lgr    0,%[function]\n"
          /* Parameter registers are ignored for "test bit" */
          "    plo    0,0,0,0(0)\n"
          "    ipm    %0\n"
          "    srl    %0,28\n"
          : "=d" (cc)
-        : "d" (r0)
-        : "cc");
+        : [function] "d" (function)
+        : "cc", "0");
      return cc == 0;
  }
  static __always_inline void __insn32_query(unsigned int opcode, u8 *query) {
-    register unsigned long r0 asm("0") = 0;    /* query function */ -    register unsigned long r1 asm("1") = (unsigned long) query; -
      asm volatile(
-        /* Parameter regs are ignored */

Quoted 8 lines

+        "    lghi    0,0\n"
+        "    lgr    1,%[query]\n"
+        /* Parameter registers are ignored */
          "    .insn    rrf,%[opc] << 16,2,4,6,0\n"
          :
-        : "d" (r0), "a" (r1), [opc] "i" (opcode)
-        : "cc", "memory");
+        : [query] "d" ((unsigned long)query), [opc] "i" (opcode)

Wouldn't it be better to keep the "a" constraint instead of "d" to avoid that the compiler ever passes the "query" value in r0 ? Otherwise the query value might get trashed if it is passed in r0...

Quoted 4 lines
Quoted 2 lines

I first thought the same, but if you look closely the value is only used by the lgr, to load the value finally into r1. So d is correct as lgr can take all registers.

But what about the "lghi    0,0" right in front of it? ... I've got the feeling that I'm missing something here...

It does load an immediate value of 0 into register 0. Are you afraid of an early clobber if gcc decides to use r0 for query?

Quoted 4 lines

Heiko, maybe you could at least swap the initialization of r0 and r1, then I'd feel a little bit better...

 Thomas

#7 lore raw
Quoted 64 lines · Christian Borntraeger
Quoted 61 lines · Thomas Huth
Quoted 60 lines · Christian Borntraeger
Quoted 56 lines · Thomas Huth
Quoted 44 lines · Heiko Carstens

Using register asm statements has been proven to be very error prone, especially when using code instrumentation where gcc may add function calls, which clobbers register contents in an unexpected way.

Therefore get rid of register asm statements in kvm code, even though there is currently nothing wrong with them. This way we know for sure that this bug class won't be introduced here.

---
  arch/s390/kvm/kvm-s390.c | 18 +++++++++---------

  1 file changed, 9 insertions(+), 9 deletions(-)
arch/s390/kvm/kvm-s390.c +1 -1
index 1296fc10f80c..4b7b24f07790 100644
@@ -329,31 +329,31 @@ static void allow_cpu_feat(unsigned long nr)
   static inline int plo_test_bit(unsigned char nr)
   {
-    register unsigned long r0 asm("0") = (unsigned long) nr | 0x100;
+    unsigned long function = (unsigned long) nr | 0x100;
       int cc;

      asm volatile(
+        "    lgr    0,%[function]\n"
          /* Parameter registers are ignored for "test bit" */
          "    plo    0,0,0,0(0)\n"
          "    ipm    %0\n"
          "    srl    %0,28\n"
          : "=d" (cc)
-        : "d" (r0)
-        : "cc");
+        : [function] "d" (function)
+        : "cc", "0");
      return cc == 0;
  }
  static __always_inline void __insn32_query(unsigned int opcode, u8 *query) {
-    register unsigned long r0 asm("0") = 0;    /* query function */ -    register unsigned long r1 asm("1") = (unsigned long) query; -
      asm volatile(
-        /* Parameter regs are ignored */

Quoted 8 lines

+        "    lghi    0,0\n"
+        "    lgr    1,%[query]\n"
+        /* Parameter registers are ignored */
          "    .insn    rrf,%[opc] << 16,2,4,6,0\n"
          :
-        : "d" (r0), "a" (r1), [opc] "i" (opcode)
-        : "cc", "memory");
+        : [query] "d" ((unsigned long)query), [opc] "i" (opcode)

Wouldn't it be better to keep the "a" constraint instead of "d" to avoid that the compiler ever passes the "query" value in r0 ? Otherwise the query value might get trashed if it is passed in r0...

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

I first thought the same, but if you look closely the value is only used by the lgr, to load the value finally into r1. So d is correct as lgr can take all registers.

But what about the "lghi    0,0" right in front of it? ... I've got the feeling that I'm missing something here...

It does load an immediate value of 0 into register 0. Are you afraid of an early clobber if gcc decides to use r0 for query?

Right, that was my concern. It's a "static __always_inline" function, so can we be sure that query is still always located in a register that is reserved for parameters (i.e. >= r2) ?

Thomas

#8 lore raw
Quoted 53 lines · Thomas Huth
Quoted 52 lines · Christian Borntraeger
Quoted 49 lines · Thomas Huth
Quoted 48 lines · Christian Borntraeger
Quoted 45 lines · Thomas Huth
Quoted 44 lines · Heiko Carstens

Using register asm statements has been proven to be very error prone, especially when using code instrumentation where gcc may add function calls, which clobbers register contents in an unexpected way.

Therefore get rid of register asm statements in kvm code, even though there is currently nothing wrong with them. This way we know for sure that this bug class won't be introduced here.

---
  arch/s390/kvm/kvm-s390.c | 18 +++++++++---------

  1 file changed, 9 insertions(+), 9 deletions(-)
arch/s390/kvm/kvm-s390.c +1 -1
index 1296fc10f80c..4b7b24f07790 100644
@@ -329,31 +329,31 @@ static void allow_cpu_feat(unsigned long nr)
   static inline int plo_test_bit(unsigned char nr)
   {
-    register unsigned long r0 asm("0") = (unsigned long) nr | 0x100;
+    unsigned long function = (unsigned long) nr | 0x100;
       int cc;

      asm volatile(
+        "    lgr    0,%[function]\n"
          /* Parameter registers are ignored for "test bit" */
          "    plo    0,0,0,0(0)\n"
          "    ipm    %0\n"
          "    srl    %0,28\n"
          : "=d" (cc)
-        : "d" (r0)
-        : "cc");
+        : [function] "d" (function)
+        : "cc", "0");
      return cc == 0;
  }
  static __always_inline void __insn32_query(unsigned int opcode, u8 *query) {
-    register unsigned long r0 asm("0") = 0;    /* query function */ -    register unsigned long r1 asm("1") = (unsigned long) query; -
      asm volatile(
-        /* Parameter regs are ignored */

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

+        "    lghi    0,0\n"
+        "    lgr    1,%[query]\n"
+        /* Parameter registers are ignored */
          "    .insn    rrf,%[opc] << 16,2,4,6,0\n"
          :
-        : "d" (r0), "a" (r1), [opc] "i" (opcode)
-        : "cc", "memory");
+        : [query] "d" ((unsigned long)query), [opc] "i" (opcode)

		: "cc", "memory", "0", "1");
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
Quoted 3 lines

Wouldn't it be better to keep the "a" constraint instead of "d" to avoid that the compiler ever passes the "query" value in r0 ? Otherwise the query value might get trashed if it is passed in r0...

I first thought the same, but if you look closely the value is only used by the lgr, to load the value finally into r1. So d is correct as lgr can take all registers.

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

But what about the "lghi    0,0" right in front of it? ... I've got the feeling that I'm missing something here...

It does load an immediate value of 0 into register 0. Are you afraid of an early clobber if gcc decides to use r0 for query?

Right, that was my concern. It's a "static __always_inline" function, so can we be sure that query is still always located in a register that is reserved for parameters (i.e. >= r2) ?

Adding Andreas. I know that gcc provides the early clobber "&" if an output is written before all imputs are used. but in this case it is not an output but a clobbered register.
The clobber list contains 0 and 1, so I guess gcc will never use those as input register. Andreas, is that correct?

PS: Of course switching both instructions does not hurt

#9 lore raw
Quoted 79 lines · Christian Borntraeger
Quoted 53 lines · Thomas Huth
Quoted 52 lines · Christian Borntraeger
Quoted 49 lines · Thomas Huth
Quoted 48 lines · Christian Borntraeger
Quoted 45 lines · Thomas Huth
Quoted 44 lines · Heiko Carstens

Using register asm statements has been proven to be very error prone, especially when using code instrumentation where gcc may add function calls, which clobbers register contents in an unexpected way.

Therefore get rid of register asm statements in kvm code, even though there is currently nothing wrong with them. This way we know for sure that this bug class won't be introduced here.

---
  arch/s390/kvm/kvm-s390.c | 18 +++++++++---------

  1 file changed, 9 insertions(+), 9 deletions(-)
arch/s390/kvm/kvm-s390.c +1 -1
index 1296fc10f80c..4b7b24f07790 100644
@@ -329,31 +329,31 @@ static void allow_cpu_feat(unsigned long nr)
   static inline int plo_test_bit(unsigned char nr)
   {
-    register unsigned long r0 asm("0") = (unsigned long) nr | 0x100;
+    unsigned long function = (unsigned long) nr | 0x100;
       int cc;

      asm volatile(
+        "    lgr    0,%[function]\n"
          /* Parameter registers are ignored for "test bit" */
          "    plo    0,0,0,0(0)\n"
          "    ipm    %0\n"
          "    srl    %0,28\n"
          : "=d" (cc)
-        : "d" (r0)
-        : "cc");
+        : [function] "d" (function)
+        : "cc", "0");
      return cc == 0;
  }
  static __always_inline void __insn32_query(unsigned int opcode, u8 *query) {
-    register unsigned long r0 asm("0") = 0;    /* query function */ -    register unsigned long r1 asm("1") = (unsigned long) query; -
      asm volatile(
-        /* Parameter regs are ignored */

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

+        "    lghi    0,0\n"
+        "    lgr    1,%[query]\n"
+        /* Parameter registers are ignored */
          "    .insn    rrf,%[opc] << 16,2,4,6,0\n"
          :
-        : "d" (r0), "a" (r1), [opc] "i" (opcode)
-        : "cc", "memory");
+        : [query] "d" ((unsigned long)query), [opc] "i" (opcode)

        : "cc", "memory", "0", "1");

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
Quoted 3 lines

Wouldn't it be better to keep the "a" constraint instead of "d" to avoid that the compiler ever passes the "query" value in r0 ? Otherwise the query value might get trashed if it is passed in r0...

I first thought the same, but if you look closely the value is only used by the lgr, to load the value finally into r1. So d is correct as lgr can take all registers.

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

But what about the "lghi    0,0" right in front of it? ... I've got the feeling that I'm missing something here...

It does load an immediate value of 0 into register 0. Are you afraid of an early clobber if gcc decides to use r0 for query?

Right, that was my concern. It's a "static __always_inline" function, so can we be sure that query is still always located in a register that is reserved for parameters (i.e. >= r2) ?

Quoted 7 lines

Adding Andreas. I know that gcc provides the early clobber "&" if an output is written before all imputs are used. but in this case it is not an output but a clobbered register.
The clobber list contains 0 and 1, so I guess gcc will never use those as input register. Andreas, is that correct?

PS: Of course switching both instructions does not hurt

Answering myself:

https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#Clobbers-and-Scratch-Registers

When the compiler selects which registers to use to represent input and output operands, it does not use any of the clobbered registers. As a result, clobbered registers are available for any use in the assembler code.

#10 lore raw
Quoted 88 lines · Christian Borntraeger
Quoted 85 lines · Christian Borntraeger
Quoted 53 lines · Thomas Huth
Quoted 52 lines · Christian Borntraeger
Quoted 49 lines · Thomas Huth
Quoted 48 lines · Christian Borntraeger
Quoted 45 lines · Thomas Huth
Quoted 44 lines · Heiko Carstens

Using register asm statements has been proven to be very error prone, especially when using code instrumentation where gcc may add function calls, which clobbers register contents in an unexpected way.

Therefore get rid of register asm statements in kvm code, even though there is currently nothing wrong with them. This way we know for sure that this bug class won't be introduced here.

---
  arch/s390/kvm/kvm-s390.c | 18 +++++++++---------

  1 file changed, 9 insertions(+), 9 deletions(-)
arch/s390/kvm/kvm-s390.c +1 -1
index 1296fc10f80c..4b7b24f07790 100644
@@ -329,31 +329,31 @@ static void allow_cpu_feat(unsigned long nr)
   static inline int plo_test_bit(unsigned char nr)
   {
-    register unsigned long r0 asm("0") = (unsigned long) nr | 0x100;
+    unsigned long function = (unsigned long) nr | 0x100;
       int cc;

      asm volatile(
+        "    lgr    0,%[function]\n"
          /* Parameter registers are ignored for "test bit" */
          "    plo    0,0,0,0(0)\n"
          "    ipm    %0\n"
          "    srl    %0,28\n"
          : "=d" (cc)
-        : "d" (r0)
-        : "cc");
+        : [function] "d" (function)
+        : "cc", "0");
      return cc == 0;
  }
  static __always_inline void __insn32_query(unsigned int opcode, u8 *query) {
-    register unsigned long r0 asm("0") = 0;    /* query function */ -    register unsigned long r1 asm("1") = (unsigned long) query; -
      asm volatile(
-        /* Parameter regs are ignored */

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

+        "    lghi    0,0\n"
+        "    lgr    1,%[query]\n"
+        /* Parameter registers are ignored */
          "    .insn    rrf,%[opc] << 16,2,4,6,0\n"
          :
-        : "d" (r0), "a" (r1), [opc] "i" (opcode)
-        : "cc", "memory");
+        : [query] "d" ((unsigned long)query), [opc] "i" (opcode)

         : "cc", "memory", "0", "1");

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
Quoted 3 lines

Wouldn't it be better to keep the "a" constraint instead of "d" to avoid that the compiler ever passes the "query" value in r0 ? Otherwise the query value might get trashed if it is passed in r0...

I first thought the same, but if you look closely the value is only used by the lgr, to load the value finally into r1. So d is correct as lgr can take all registers.

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

But what about the "lghi    0,0" right in front of it? ... I've got the feeling that I'm missing something here...

It does load an immediate value of 0 into register 0. Are you afraid of an early clobber if gcc decides to use r0 for query?

Right, that was my concern. It's a "static __always_inline" function, so can we be sure that query is still always located in a register that is reserved for parameters (i.e. >= r2) ?

Adding Andreas. I know that gcc provides the early clobber "&" if an output is written before all imputs are used. but in this case it is not an output but a clobbered register.
The clobber list contains 0 and 1, so I guess gcc will never use those as input register. Andreas, is that correct?

Quoted 7 lines
Quoted 1 line

PS: Of course switching both instructions does not hurt

Answering myself:

https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html#Clobbers-and-Scratch-Registers

When the compiler selects which registers to use to represent input and output operands, it does not use any of the clobbered registers. As a result, clobbered registers are available for any use in the assembler code.

Ah, ok, TIL, thanks, then it is fine as it is. Feel free to add my Reviewed-by without any further modifications.

Thomas

#11 lore raw
In reply to Heiko Carstens
Quoted 10 lines · Heiko Carstens

Using register asm statements has been proven to be very error prone, especially when using code instrumentation where gcc may add function calls, which clobbers register contents in an unexpected way.

Therefore get rid of register asm statements in kvm code, even though there is currently nothing wrong with them. This way we know for sure that this bug class won't be introduced here.

thanks applied.

Quoted 50 lines
---
  arch/s390/kvm/kvm-s390.c | 18 +++++++++---------
  1 file changed, 9 insertions(+), 9 deletions(-)
arch/s390/kvm/kvm-s390.c +9 -9
index 1296fc10f80c..4b7b24f07790 100644
@@ -329,31 +329,31 @@ static void allow_cpu_feat(unsigned long nr)
 
  static inline int plo_test_bit(unsigned char nr)
  {
-	register unsigned long r0 asm("0") = (unsigned long) nr | 0x100;
+	unsigned long function = (unsigned long) nr | 0x100;
  	int cc;
 
  	asm volatile(
+		"	lgr	0,%[function]\n"
  		/* Parameter registers are ignored for "test bit" */
  		"	plo	0,0,0,0(0)\n"
  		"	ipm	%0\n"
  		"	srl	%0,28\n"
  		: "=d" (cc)
-		: "d" (r0)
-		: "cc");
+		: [function] "d" (function)
+		: "cc", "0");
  	return cc == 0;
  }
 
  static __always_inline void __insn32_query(unsigned int opcode, u8 *query)
  {
-	register unsigned long r0 asm("0") = 0;	/* query function */
-	register unsigned long r1 asm("1") = (unsigned long) query;
-
  	asm volatile(
-		/* Parameter regs are ignored */
+		"	lghi	0,0\n"
+		"	lgr	1,%[query]\n"
+		/* Parameter registers are ignored */
  		"	.insn	rrf,%[opc] << 16,2,4,6,0\n"
  		:
-		: "d" (r0), "a" (r1), [opc] "i" (opcode)
-		: "cc", "memory");
+		: [query] "d" ((unsigned long)query), [opc] "i" (opcode)
+		: "cc", "memory", "0", "1");
  }
 
  #define INSN_SORTL 0xb938