Skip to contentSkip to conversation

x86-64 machine_to_phys vs NX bit

DISCUSSION Rik van Riel 3 participants 4 messages Aug 24, 2006 – Aug 25, 2006

lore
Share & tools
lore
#0 lore raw

On x86-64 machine_to_phys looks like it should never succeed, yet I'm guessing it must somehow be lucky...

The problem would be that the NX bit is bit 63 of the pte, meaning that when pte_val is called we are working with a value 2^63 higher than we should be.

#define pte_val(x)      (((x).pte & 1) ? machine_to_phys((x).pte) : \
                         (x).pte)

static inline paddr_t machine_to_phys(maddr_t machine)

{
        paddr_t phys = mfn_to_pfn(machine >> PAGE_SHIFT);
        phys = (phys << PAGE_SHIFT) | (machine & ~PAGE_MASK);
        return phys;
}

Should we mask the 'machine' variable with PHYSICAL_MASK at some point so we cut off the NX bit and other reserved bits?

Say, something like the following?

- paddr_t phys = mfn_to_pfn(machine >> PAGE_SHIFT);
+ paddr_t phys = mfn_to_pfn((machine >> PAGE_SHIFT) & PHYSICAL_MASK);

I'm still thinking I may have missed something in the code somewhere, but I've been looking at this for over an hour now and can't seem to find it...

Any ideas?

Signature · 1 line
What is important?  What you want to be true, or what is true?
#1 lore raw
Quoted 10 lines · Rik van Riel

Say, something like the following?

(quoted diff) +1 -1
-    paddr_t phys = mfn_to_pfn(machine >> PAGE_SHIFT);
+    paddr_t phys = mfn_to_pfn((machine >> PAGE_SHIFT) & PHYSICAL_MASK);

I'm still thinking I may have missed something in the code somewhere, but I've been looking at this for over an hour now and can't seem to find it...

Any ideas?

Your suggested patch looks reasonable but it'd be good to find out why this hasn't caused us problems. For example, perhaps supported_pte_mask doesn't include PAGE_NX, so we're never setting the NX bit on 64-bit PTEs? That must be worth checking out, possibly also tracing machine_to_phys to find out where that bit 63 goes -- I agree that it looks like mfn_to_pfn() shouldn#t work if bit63 is set in the 'maddr' argument.

-- Keir

#2 lore raw
Quoted 8 lines
Quoted 1 line
Quoted 1 line

Keir Fraser <[email protected]> 25.08.06 09:32 >>>

Quoted 5 lines · Rik van Riel

Say, something like the following?

(quoted diff) +1 -1
-    paddr_t phys = mfn_to_pfn(machine >> PAGE_SHIFT);
+    paddr_t phys = mfn_to_pfn((machine >> PAGE_SHIFT) & PHYSICAL_MASK);
Quoted 7 lines
Quoted 5 lines

I'm still thinking I may have missed something in the code somewhere, but I've been looking at this for over an hour now and can't seem to find it...

Any ideas?

Your suggested patch looks reasonable but it'd be good to find out why this

I'd suggest not changing machine_to_phys(), but its callers (where needed), in order to prevent hiding errors.

Quoted 2 lines

hasn't caused us problems. For example, perhaps supported_pte_mask doesn't include PAGE_NX, so we're never setting the NX bit on 64-bit PTEs? That must

__supported_pte_mask is all ones on a system I just checked this on; pte_val() for a page with _PAGE_NX set indeed returns (end_pfn << PAGE_SHIFT) | prot_flags. With that I guess there are just too few pages making use of _PAGE_NX yet, so the problem went unnoticed so far.

Quoted 3 lines

be worth checking out, possibly also tracing machine_to_phys to find out where that bit 63 goes -- I agree that it looks like mfn_to_pfn() shouldn#t work if bit63 is set in the 'maddr' argument.

Jan

#3 lore raw
In reply to Keir Fraser
Quoted 11 lines · Keir Fraser
Quoted 9 lines · Rik van Riel

Say, something like the following?

(quoted diff) +1 -1
-    paddr_t phys = mfn_to_pfn(machine >> PAGE_SHIFT);
+    paddr_t phys = mfn_to_pfn((machine >> PAGE_SHIFT) & PHYSICAL_MASK);

I'm still thinking I may have missed something in the code somewhere, but I've been looking at this for over an hour now and can't seem to find it...

Quoted 5 lines
Quoted 1 line

Any ideas?

Your suggested patch looks reasonable but it'd be good to find out why this hasn't caused us problems. For example, perhaps supported_pte_mask doesn't include PAGE_NX, so we're never setting the NX bit on 64-bit PTEs?

We do set the NX bit. Including on vmalloced pages...

Quoted 3 lines

That must be worth checking out, possibly also tracing machine_to_phys to find out where that bit 63 goes -- I agree that it looks like mfn_to_pfn() shouldn't work if bit63 is set in the
'maddr' argument.

Absolutely :)

Signature · 1 line
What is important?  What you want to be true, or what is true?