Skip to contentSkip to conversation

KVM: arm64: Inject UNDEF when host is executing an smc with imm16 != 0

PATCH In review Sebastian Ene 4 participants 10 messages Mar 24 – Mar 25

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 KVM: arm64: Inject UNDEF when host is executing an smc with imm16 != 0

The ARM Service Calling Convention (SMCCC) specifies that the function identifier and parameters should be passed in registers, leaving the 16-bit immediate field of the SMC instruction un-handled. Currently, our pKVM handler ignores the immediate value, which could lead to non-compliant software relying on implementation-defined behavior. Enforce the host kernel running under pKVM to use an immediate value of 0 by decoding the ISS from the ESR_EL2 and inject an undefined instruction exception back to the caller.

---
 arch/arm64/kvm/hyp/nvhe/hyp-main.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
arch/arm64/kvm/hyp/nvhe/hyp-main.c +6 -1
index e7790097db93..ff6a90a4a4c7 100644
@@ -756,13 +756,18 @@ static bool handle_host_mte(u64 esr)
 void handle_trap(struct kvm_cpu_context *host_ctxt)
 {
 	u64 esr = read_sysreg_el2(SYS_ESR);
+	u16 imm16;
 
 	switch (ESR_ELx_EC(esr)) {
 	case ESR_ELx_EC_HVC64:
 		handle_host_hcall(host_ctxt);
 		break;
 	case ESR_ELx_EC_SMC64:
-		handle_host_smc(host_ctxt);
+		imm16 = esr & U16_MAX;
+		if (!imm16)
+			handle_host_smc(host_ctxt);
+		else
+			inject_undef64();
 		break;
 	case ESR_ELx_EC_IABT_LOW:
 	case ESR_ELx_EC_DABT_LOW:
Signature · 1 line
2.53.0.983.g0bb29b3bc5-goog
#1 lore raw
Quoted 22 lines · Sebastian Ene

The ARM Service Calling Convention (SMCCC) specifies that the function identifier and parameters should be passed in registers, leaving the 16-bit immediate field of the SMC instruction un-handled. Currently, our pKVM handler ignores the immediate value, which could lead to non-compliant software relying on implementation-defined behavior. Enforce the host kernel running under pKVM to use an immediate value of 0 by decoding the ISS from the ESR_EL2 and inject an undefined instruction exception back to the caller.

---
 arch/arm64/kvm/hyp/nvhe/hyp-main.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
arch/arm64/kvm/hyp/nvhe/hyp-main.c +0 -0
index e7790097db93..ff6a90a4a4c7 100644
@@ -756,13 +756,18 @@ static bool handle_host_mte(u64 esr)
 void handle_trap(struct kvm_cpu_context *host_ctxt)
 {
 	u64 esr = read_sysreg_el2(SYS_ESR);

arch/arm64/kvm/hyp/nvhe/hyp-main.c static bool handle_host_mte(u64 esr) · line 767

Quoted 10 lines
arch/arm64/kvm/hyp/nvhe/hyp-main.c +3 -1
+	u16 imm16;
 
 	switch (ESR_ELx_EC(esr)) {
 	case ESR_ELx_EC_HVC64:
 		handle_host_hcall(host_ctxt);
 		break;
 	case ESR_ELx_EC_SMC64:
-		handle_host_smc(host_ctxt);
+		imm16 = esr & U16_MAX;
+		if (!imm16)

if (ESR_ELx_xVC_IMM_MASK(esr)) ?

Also, I can't find void inject_undef64(void); I think you need a vcpu for that?

Quoted 9 lines
arch/arm64/kvm/hyp/nvhe/hyp-main.c +3 -0
+			handle_host_smc(host_ctxt);
+		else
+			inject_undef64();
 		break;
 	case ESR_ELx_EC_IABT_LOW:
 	case ESR_ELx_EC_DABT_LOW:
Signature · 1 line
2.53.0.983.g0bb29b3bc5-goog
#2 lore raw
Quoted 36 lines · Vincent Donnefort
Quoted 22 lines · Sebastian Ene

The ARM Service Calling Convention (SMCCC) specifies that the function identifier and parameters should be passed in registers, leaving the 16-bit immediate field of the SMC instruction un-handled. Currently, our pKVM handler ignores the immediate value, which could lead to non-compliant software relying on implementation-defined behavior. Enforce the host kernel running under pKVM to use an immediate value of 0 by decoding the ISS from the ESR_EL2 and inject an undefined instruction exception back to the caller.

---
 arch/arm64/kvm/hyp/nvhe/hyp-main.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
arch/arm64/kvm/hyp/nvhe/hyp-main.c +0 -0
index e7790097db93..ff6a90a4a4c7 100644
@@ -756,13 +756,18 @@ static bool handle_host_mte(u64 esr)
 void handle_trap(struct kvm_cpu_context *host_ctxt)
 {
 	u64 esr = read_sysreg_el2(SYS_ESR);

arch/arm64/kvm/hyp/nvhe/hyp-main.c static bool handle_host_mte(u64 esr) · line 767

Quoted 10 lines
arch/arm64/kvm/hyp/nvhe/hyp-main.c +3 -1
+	u16 imm16;
 
 	switch (ESR_ELx_EC(esr)) {
 	case ESR_ELx_EC_HVC64:
 		handle_host_hcall(host_ctxt);
 		break;
 	case ESR_ELx_EC_SMC64:
-		handle_host_smc(host_ctxt);
+		imm16 = esr & U16_MAX;
+		if (!imm16)

if (ESR_ELx_xVC_IMM_MASK(esr)) ?

Quoted 1 line

Also, I can't find void inject_undef64(void); I think you need a vcpu for that?

Ah my bad, handle_host_mte() introduced it!

Quoted 10 lines
Quoted 10 lines
arch/arm64/kvm/hyp/nvhe/hyp-main.c +3 -0
+			handle_host_smc(host_ctxt);
+		else
+			inject_undef64();
 		break;
 	case ESR_ELx_EC_IABT_LOW:
 	case ESR_ELx_EC_DABT_LOW:
Signature · 1 line
2.53.0.983.g0bb29b3bc5-goog
#3 lore raw
Quoted 37 lines · Vincent Donnefort
Quoted 36 lines · Vincent Donnefort
Quoted 22 lines · Sebastian Ene

The ARM Service Calling Convention (SMCCC) specifies that the function identifier and parameters should be passed in registers, leaving the 16-bit immediate field of the SMC instruction un-handled. Currently, our pKVM handler ignores the immediate value, which could lead to non-compliant software relying on implementation-defined behavior. Enforce the host kernel running under pKVM to use an immediate value of 0 by decoding the ISS from the ESR_EL2 and inject an undefined instruction exception back to the caller.

---
 arch/arm64/kvm/hyp/nvhe/hyp-main.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
arch/arm64/kvm/hyp/nvhe/hyp-main.c +0 -0
index e7790097db93..ff6a90a4a4c7 100644
@@ -756,13 +756,18 @@ static bool handle_host_mte(u64 esr)
 void handle_trap(struct kvm_cpu_context *host_ctxt)
 {
 	u64 esr = read_sysreg_el2(SYS_ESR);

arch/arm64/kvm/hyp/nvhe/hyp-main.c static bool handle_host_mte(u64 esr) · line 767

Quoted 10 lines
arch/arm64/kvm/hyp/nvhe/hyp-main.c +3 -1
+	u16 imm16;
 
 	switch (ESR_ELx_EC(esr)) {
 	case ESR_ELx_EC_HVC64:
 		handle_host_hcall(host_ctxt);
 		break;
 	case ESR_ELx_EC_SMC64:
-		handle_host_smc(host_ctxt);
+		imm16 = esr & U16_MAX;
+		if (!imm16)

if (ESR_ELx_xVC_IMM_MASK(esr)) ?

Quoted 8 lines
Quoted 1 line

Also, I can't find void inject_undef64(void); I think you need a vcpu for that?

Ah my bad, handle_host_mte() introduced it!

Quoted 4 lines

arch/arm64/kvm/hyp/nvhe/hyp-main.c static bool handle_host_mte(u64 esr) · line 770

Quoted 4 lines
arch/arm64/kvm/hyp/nvhe/hyp-main.c +3 -0
+			handle_host_smc(host_ctxt);
+		else
+			inject_undef64();

Could it be better to return SMCCC_RET_NOT_SUPPORTED?

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
arch/arm64/kvm/hyp/nvhe/hyp-main.c +0 -0
 		break;
 	case ESR_ELx_EC_IABT_LOW:
 	case ESR_ELx_EC_DABT_LOW:
Signature · 1 line
2.53.0.983.g0bb29b3bc5-goog
#4 lore raw
Quoted 40 lines · Vincent Donnefort
Quoted 39 lines · Vincent Donnefort
Quoted 37 lines · Vincent Donnefort
Quoted 22 lines · Sebastian Ene

The ARM Service Calling Convention (SMCCC) specifies that the function identifier and parameters should be passed in registers, leaving the 16-bit immediate field of the SMC instruction un-handled. Currently, our pKVM handler ignores the immediate value, which could lead to non-compliant software relying on implementation-defined behavior. Enforce the host kernel running under pKVM to use an immediate value of 0 by decoding the ISS from the ESR_EL2 and inject an undefined instruction exception back to the caller.

---
 arch/arm64/kvm/hyp/nvhe/hyp-main.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
arch/arm64/kvm/hyp/nvhe/hyp-main.c +0 -0
index e7790097db93..ff6a90a4a4c7 100644
@@ -756,13 +756,18 @@ static bool handle_host_mte(u64 esr)
 void handle_trap(struct kvm_cpu_context *host_ctxt)
 {
 	u64 esr = read_sysreg_el2(SYS_ESR);

arch/arm64/kvm/hyp/nvhe/hyp-main.c static bool handle_host_mte(u64 esr) · line 767

Quoted 10 lines
arch/arm64/kvm/hyp/nvhe/hyp-main.c +3 -1
+	u16 imm16;
 
 	switch (ESR_ELx_EC(esr)) {
 	case ESR_ELx_EC_HVC64:
 		handle_host_hcall(host_ctxt);
 		break;
 	case ESR_ELx_EC_SMC64:
-		handle_host_smc(host_ctxt);
+		imm16 = esr & U16_MAX;
+		if (!imm16)

if (ESR_ELx_xVC_IMM_MASK(esr)) ?

Also, I can't find void inject_undef64(void); I think you need a vcpu for that?

Quoted 8 lines
Quoted 6 lines

Ah my bad, handle_host_mte() introduced it!

Quoted 4 lines

arch/arm64/kvm/hyp/nvhe/hyp-main.c static bool handle_host_mte(u64 esr) · line 770

Quoted 4 lines
arch/arm64/kvm/hyp/nvhe/hyp-main.c +3 -0
+			handle_host_smc(host_ctxt);
+		else
+			inject_undef64();

Could it be better to return SMCCC_RET_NOT_SUPPORTED?

Right, I will do that since it seems this is the correct approach.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
arch/arm64/kvm/hyp/nvhe/hyp-main.c +0 -0
 		break;
 	case ESR_ELx_EC_IABT_LOW:
 	case ESR_ELx_EC_DABT_LOW:
Signature · 1 line
2.53.0.983.g0bb29b3bc5-goog

Thanks,
Sebastian

#5 lore raw
In reply to Vincent Donnefort
Quoted 25 lines · Vincent Donnefort
Quoted 24 lines · Sebastian Ene

The ARM Service Calling Convention (SMCCC) specifies that the function identifier and parameters should be passed in registers, leaving the 16-bit immediate field of the SMC instruction un-handled. Currently, our pKVM handler ignores the immediate value, which could lead to non-compliant software relying on implementation-defined behavior. Enforce the host kernel running under pKVM to use an immediate value of 0 by decoding the ISS from the ESR_EL2 and inject an undefined instruction exception back to the caller.

---
 arch/arm64/kvm/hyp/nvhe/hyp-main.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
arch/arm64/kvm/hyp/nvhe/hyp-main.c +1 -0
index e7790097db93..ff6a90a4a4c7 100644
@@ -756,13 +756,18 @@ static bool handle_host_mte(u64 esr)
 void handle_trap(struct kvm_cpu_context *host_ctxt)
 {
 	u64 esr = read_sysreg_el2(SYS_ESR);
+	u16 imm16;
 
Quoted 9 lines

arch/arm64/kvm/hyp/nvhe/hyp-main.c static bool handle_host_mte(u64 esr) · line 767

Quoted 8 lines
arch/arm64/kvm/hyp/nvhe/hyp-main.c +2 -1
 	switch (ESR_ELx_EC(esr)) {
 	case ESR_ELx_EC_HVC64:
 		handle_host_hcall(host_ctxt);
 		break;
 	case ESR_ELx_EC_SMC64:
-		handle_host_smc(host_ctxt);
+		imm16 = esr & U16_MAX;
+		if (!imm16)

Hi Vincent,

Quoted 1 line

if (ESR_ELx_xVC_IMM_MASK(esr)) ?

Yes we can use this instead.

Quoted 3 lines

Also, I can't find void inject_undef64(void); I think you need a vcpu for that?

Right, there is one version that does that.

Quoted 9 lines
Quoted 9 lines
arch/arm64/kvm/hyp/nvhe/hyp-main.c +3 -0
+			handle_host_smc(host_ctxt);
+		else
+			inject_undef64();
 		break;
 	case ESR_ELx_EC_IABT_LOW:
 	case ESR_ELx_EC_DABT_LOW:
Signature · 1 line
2.53.0.983.g0bb29b3bc5-goog

Thanks,
Sebastian

maintainer
#6 lore raw
In reply to Sebastian Ene
Quoted 9 lines · Sebastian Ene

The ARM Service Calling Convention (SMCCC) specifies that the function identifier and parameters should be passed in registers, leaving the 16-bit immediate field of the SMC instruction un-handled. Currently, our pKVM handler ignores the immediate value, which could lead to non-compliant software relying on implementation-defined behavior. Enforce the host kernel running under pKVM to use an immediate value of 0 by decoding the ISS from the ESR_EL2 and inject an undefined instruction exception back to the caller.

No, that's completely wrong. SMC never UNDEFs, unless EL3 isn't implemented. You can't just decide to generate an UNDEF because you don't like the immediate.

All you are allowed to do is to return an error code.

Thanks,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
#7 lore raw
Quoted 11 lines · Marc Zyngier
Quoted 9 lines · Sebastian Ene

The ARM Service Calling Convention (SMCCC) specifies that the function identifier and parameters should be passed in registers, leaving the 16-bit immediate field of the SMC instruction un-handled. Currently, our pKVM handler ignores the immediate value, which could lead to non-compliant software relying on implementation-defined behavior. Enforce the host kernel running under pKVM to use an immediate value of 0 by decoding the ISS from the ESR_EL2 and inject an undefined instruction exception back to the caller.

Hi Marc,

Quoted 6 lines

No, that's completely wrong. SMC never UNDEFs, unless EL3 isn't implemented. You can't just decide to generate an UNDEF because you don't like the immediate.

All you are allowed to do is to return an error code.

Thanks for letting me know, I will update the patch to do this.

Quoted 5 lines

Thanks,

	M.

Cheers,
Sebastian

Quoted 2 lines
Signature · 1 line
Without deviation from the norm, progress is not possible.
#8 lore raw
In reply to Sebastian Ene

Hi Seb,

Quoted 23 lines · Sebastian Ene

The ARM Service Calling Convention (SMCCC) specifies that the function identifier and parameters should be passed in registers, leaving the 16-bit immediate field of the SMC instruction un-handled. Currently, our pKVM handler ignores the immediate value, which could lead to non-compliant software relying on implementation-defined behavior. Enforce the host kernel running under pKVM to use an immediate value of 0 by decoding the ISS from the ESR_EL2 and inject an undefined instruction exception back to the caller.

---
 arch/arm64/kvm/hyp/nvhe/hyp-main.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
arch/arm64/kvm/hyp/nvhe/hyp-main.c +0 -0
index e7790097db93..ff6a90a4a4c7 100644
@@ -756,13 +756,18 @@ static bool handle_host_mte(u64 esr)
 void handle_trap(struct kvm_cpu_context *host_ctxt)
 {
        u64 esr = read_sysreg_el2(SYS_ESR);

arch/arm64/kvm/hyp/nvhe/hyp-main.c static bool handle_host_mte(u64 esr) · line 767

Quoted 10 lines
arch/arm64/kvm/hyp/nvhe/hyp-main.c +3 -1
+       u16 imm16;
 
        switch (ESR_ELx_EC(esr)) {
        case ESR_ELx_EC_HVC64:
                handle_host_hcall(host_ctxt);
                break;
        case ESR_ELx_EC_SMC64:
-               handle_host_smc(host_ctxt);
+               imm16 = esr & U16_MAX;
+               if (!imm16)

Do we need to introduce a new variable for this? This isn't easier to read than:
+ if (!(esr & U16_MAX))

Also, we have a mask for this: ESR_ELx_xVC_IMM_MASK , please use that instead

Cheers,
/fuad

Quoted 9 lines
arch/arm64/kvm/hyp/nvhe/hyp-main.c +3 -1
+                       handle_host_smc(host_ctxt);
+               else
+                       inject_undef64();
                break;
        case ESR_ELx_EC_IABT_LOW:
        case ESR_ELx_EC_DABT_LOW:
--

2.53.0.983.g0bb29b3bc5-goog

#9 lore raw

On Tue, Mar 24, 2026 at 03:13:51PM +0000, Fuad Tabba wrote:

Hi Fuad,

Quoted 40 lines

Hi Seb,

Quoted 23 lines · Sebastian Ene

The ARM Service Calling Convention (SMCCC) specifies that the function identifier and parameters should be passed in registers, leaving the 16-bit immediate field of the SMC instruction un-handled. Currently, our pKVM handler ignores the immediate value, which could lead to non-compliant software relying on implementation-defined behavior. Enforce the host kernel running under pKVM to use an immediate value of 0 by decoding the ISS from the ESR_EL2 and inject an undefined instruction exception back to the caller.

---
 arch/arm64/kvm/hyp/nvhe/hyp-main.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
arch/arm64/kvm/hyp/nvhe/hyp-main.c +0 -0
index e7790097db93..ff6a90a4a4c7 100644
@@ -756,13 +756,18 @@ static bool handle_host_mte(u64 esr)
 void handle_trap(struct kvm_cpu_context *host_ctxt)
 {
        u64 esr = read_sysreg_el2(SYS_ESR);

arch/arm64/kvm/hyp/nvhe/hyp-main.c static bool handle_host_mte(u64 esr) · line 767

Quoted 10 lines
arch/arm64/kvm/hyp/nvhe/hyp-main.c +3 -1
+       u16 imm16;
 
        switch (ESR_ELx_EC(esr)) {
        case ESR_ELx_EC_HVC64:
                handle_host_hcall(host_ctxt);
                break;
        case ESR_ELx_EC_SMC64:
-               handle_host_smc(host_ctxt);
+               imm16 = esr & U16_MAX;
+               if (!imm16)

Do we need to introduce a new variable for this? This isn't easier to read than:
+ if (!(esr & U16_MAX))

Quoted 1 line

Also, we have a mask for this: ESR_ELx_xVC_IMM_MASK , please use that instead

I will drop that variable and use the ESR_ELx_xVC_IMM_MASK as you pointed out.

Quoted 15 lines

Cheers,
/fuad

Quoted 9 lines
arch/arm64/kvm/hyp/nvhe/hyp-main.c +3 -1
+                       handle_host_smc(host_ctxt);
+               else
+                       inject_undef64();
                break;
        case ESR_ELx_EC_IABT_LOW:
        case ESR_ELx_EC_DABT_LOW:
--

2.53.0.983.g0bb29b3bc5-goog

Thanks,
Sebastian