Skip to contentSkip to conversation

x86: Fix exceptions for fxsave/fxrstor

PATCH In review Kevin Wolf 2 participants 4 messages Oct 2, 2009 – Oct 4, 2009

lore
Share & tools
lore

Kevin Wolf

author
#0 lore raw

PATCH 1/1 x86: Fix exceptions for fxsave/fxrstor

This patch corrects the following aspects of exception generation in fxsave/fxrstor:

* Generate #GP if the operand is not aligned to a 16 byte boundary
* Generate #UD if the LOCK prefix is used
* For CR0.EM = 1 #NM is generated, not #UD

---
 target-i386/op_helper.c |   10 ++++++++++
 target-i386/translate.c |    8 ++++----
 2 files changed, 14 insertions(+), 4 deletions(-)
target-i386/op_helper.c +10 -0
index 33d44b0..3f05532 100644
@@ -4338,6 +4338,11 @@ void helper_fxsave(target_ulong ptr, int data64)
     CPU86_LDouble tmp;
     target_ulong addr;
 
+    /* The operand must be 16 byte aligned */
+    if (ptr & 0xf) {
+        raise_exception(EXCP0D_GPF);
+    }
+
     fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11;
     fptag = 0;
     for(i = 0; i < 8; i++) {
@@ -4394,6 +4399,11 @@ void helper_fxrstor(target_ulong ptr, int data64)
     CPU86_LDouble tmp;
     target_ulong addr;
 
+    /* The operand must be 16 byte aligned */
+    if (ptr & 0xf) {
+        raise_exception(EXCP0D_GPF);
+    }
+
     env->fpuc = lduw(ptr);
     fpus = lduw(ptr + 2);
     fptag = lduw(ptr + 4);
target-i386/translate.c +4 -4
index 5b11d7f..9af2eed 100644
@@ -7436,9 +7436,9 @@ static target_ulong disas_insn(DisasContext *s, target_ulong pc_start)
         switch(op) {
         case 0: /* fxsave */
             if (mod == 3 || !(s->cpuid_features & CPUID_FXSR) ||
-                (s->flags & HF_EM_MASK))
+                (s->prefix & PREFIX_LOCK))
                 goto illegal_op;
-            if (s->flags & HF_TS_MASK) {
+            if ((s->flags & HF_EM_MASK) || (s->flags & HF_TS_MASK)) {
                 gen_exception(s, EXCP07_PREX, pc_start - s->cs_base);
                 break;
             }
@@ -7450,9 +7450,9 @@ static target_ulong disas_insn(DisasContext *s, target_ulong pc_start)
             break;
         case 1: /* fxrstor */
             if (mod == 3 || !(s->cpuid_features & CPUID_FXSR) ||
-                (s->flags & HF_EM_MASK))
+                (s->prefix & PREFIX_LOCK))
                 goto illegal_op;
-            if (s->flags & HF_TS_MASK) {
+            if ((s->flags & HF_EM_MASK) || (s->flags & HF_TS_MASK)) {
                 gen_exception(s, EXCP07_PREX, pc_start - s->cs_base);
                 break;
             }
Signature · 1 line
1.6.0.2
#1 lore raw
Quoted 4 lines · Kevin Wolf

This patch corrects the following aspects of exception generation in fxsave/fxrstor:

* Generate #GP if the operand is not aligned to a 16 byte boundary

Agreed.

Quoted 1 line

* Generate #UD if the LOCK prefix is used

Agreed.

Quoted 1 line

* For CR0.EM = 1 #NM is generated, not #UD

This does not match the Intel manual:

| #NM If CR0.TS[bit 3] = 1.

| #UD If CR0.EM[bit 2] = 1.
|     If CPUID.01H:EDX.FXSR[bit 24] = 0.
|     If the LOCK prefix is used.
Quoted 68 lines
---
 target-i386/op_helper.c |   10 ++++++++++
 target-i386/translate.c |    8 ++++----
 2 files changed, 14 insertions(+), 4 deletions(-)
target-i386/op_helper.c +10 -0
index 33d44b0..3f05532 100644
@@ -4338,6 +4338,11 @@ void helper_fxsave(target_ulong ptr, int data64)
     CPU86_LDouble tmp;
     target_ulong addr;
 
+    /* The operand must be 16 byte aligned */
+    if (ptr & 0xf) {
+        raise_exception(EXCP0D_GPF);
+    }
+
     fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11;
     fptag = 0;
     for(i = 0; i < 8; i++) {
@@ -4394,6 +4399,11 @@ void helper_fxrstor(target_ulong ptr, int data64)
     CPU86_LDouble tmp;
     target_ulong addr;
 
+    /* The operand must be 16 byte aligned */
+    if (ptr & 0xf) {
+        raise_exception(EXCP0D_GPF);
+    }
+
     env->fpuc = lduw(ptr);
     fpus = lduw(ptr + 2);
     fptag = lduw(ptr + 4);
target-i386/translate.c +4 -4
index 5b11d7f..9af2eed 100644
@@ -7436,9 +7436,9 @@ static target_ulong disas_insn(DisasContext *s, target_ulong pc_start)
         switch(op) {
         case 0: /* fxsave */
             if (mod == 3 || !(s->cpuid_features & CPUID_FXSR) ||
-                (s->flags & HF_EM_MASK))
+                (s->prefix & PREFIX_LOCK))
                 goto illegal_op;
-            if (s->flags & HF_TS_MASK) {
+            if ((s->flags & HF_EM_MASK) || (s->flags & HF_TS_MASK)) {
                 gen_exception(s, EXCP07_PREX, pc_start - s->cs_base);
                 break;
             }
@@ -7450,9 +7450,9 @@ static target_ulong disas_insn(DisasContext *s, target_ulong pc_start)
             break;
         case 1: /* fxrstor */
             if (mod == 3 || !(s->cpuid_features & CPUID_FXSR) ||
-                (s->flags & HF_EM_MASK))
+                (s->prefix & PREFIX_LOCK))
                 goto illegal_op;
-            if (s->flags & HF_TS_MASK) {
+            if ((s->flags & HF_EM_MASK) || (s->flags & HF_TS_MASK)) {
                 gen_exception(s, EXCP07_PREX, pc_start - s->cs_base);
                 break;
             }
Signature · 1 line
1.6.0.2
Signature · 2 lines
Aurelien Jarno	                        GPG: 1024D/F1BCDB73
[email protected]                 http://www.aurel32.net
author
#2 lore raw

Am Sonntag, 4. Oktober 2009 12:05 schrieb Aurelien Jarno:

Quoted 14 lines · Kevin Wolf
Quoted 4 lines

This patch corrects the following aspects of exception generation in fxsave/fxrstor:

* Generate #GP if the operand is not aligned to a 16 byte boundary

Agreed.

Quoted 1 line

* Generate #UD if the LOCK prefix is used

Agreed.

Quoted 1 line

* For CR0.EM = 1 #NM is generated, not #UD

Quoted 7 lines

This does not match the Intel manual:

| #NM If CR0.TS[bit 3] = 1.
|
| #UD If CR0.EM[bit 2] = 1.
|     If CPUID.01H:EDX.FXSR[bit 24] = 0.
|     If the LOCK prefix is used.
|

Hm, you seem to have a different Intel manual. In my copy the CR0.EM part still belongs to #NM. Also, I ran my test code in KVM for comparision and it did generate an #NM (on two different machines, one Intel, one AMD), so I'm quite sure this is right (well, at least not completely wrong).

On the other hand, I just had a look at the AMD documentation and it seems to support your version... So while my hardware suggests that #NM is right, I'm not going to insist on it. Maybe there is some hardware that actually does generate #UD.

If you don't like to commit this part of the fix despite my explanation, just let me know and I'll resend the patch without it.

Kevin

#3 lore raw
Quoted 23 lines · Kevin Wolf

Am Sonntag, 4. Oktober 2009 12:05 schrieb Aurelien Jarno:

Quoted 14 lines · Kevin Wolf
Quoted 4 lines

This patch corrects the following aspects of exception generation in fxsave/fxrstor:

* Generate #GP if the operand is not aligned to a 16 byte boundary

Agreed.

Quoted 1 line

* Generate #UD if the LOCK prefix is used

Agreed.

Quoted 1 line

* For CR0.EM = 1 #NM is generated, not #UD

Quoted 7 lines

This does not match the Intel manual:

| #NM If CR0.TS[bit 3] = 1.
|
| #UD If CR0.EM[bit 2] = 1.
|     If CPUID.01H:EDX.FXSR[bit 24] = 0.
|     If the LOCK prefix is used.
|
Quoted 4 lines

Hm, you seem to have a different Intel manual. In my copy the CR0.EM part still belongs to #NM. Also, I ran my test code in KVM for comparision and it did generate an #NM (on two different machines, one Intel, one AMD), so I'm quite sure this is right (well, at least not completely wrong).

My copy of the Intel Manual was quite outdated (May 2007). The new version from September 2009 matches your patch, so I have applied it. Sorry.

Signature · 2 lines
Aurelien Jarno                          GPG: 1024D/F1BCDB73
[email protected]                 http://www.aurel32.net