2008-10-23 05:26:29 +00:00
|
|
|
#ifndef _ASM_X86_PGTABLE_2LEVEL_H
|
|
|
|
#define _ASM_X86_PGTABLE_2LEVEL_H
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
#define pte_ERROR(e) \
|
2012-05-22 02:50:07 +00:00
|
|
|
pr_err("%s:%d: bad pte %08lx\n", __FILE__, __LINE__, (e).pte_low)
|
2005-04-16 22:20:36 +00:00
|
|
|
#define pgd_ERROR(e) \
|
2012-05-22 02:50:07 +00:00
|
|
|
pr_err("%s:%d: bad pgd %08lx\n", __FILE__, __LINE__, pgd_val(e))
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Certain architectures need to do special things when PTEs
|
|
|
|
* within a page table are directly modified. Thus, the following
|
|
|
|
* hook is made available.
|
|
|
|
*/
|
2007-05-02 17:27:13 +00:00
|
|
|
static inline void native_set_pte(pte_t *ptep , pte_t pte)
|
|
|
|
{
|
|
|
|
*ptep = pte;
|
|
|
|
}
|
2008-01-30 12:32:58 +00:00
|
|
|
|
2007-05-02 17:27:13 +00:00
|
|
|
static inline void native_set_pmd(pmd_t *pmdp, pmd_t pmd)
|
|
|
|
{
|
|
|
|
*pmdp = pmd;
|
|
|
|
}
|
2006-12-07 01:14:08 +00:00
|
|
|
|
2008-01-30 12:32:58 +00:00
|
|
|
static inline void native_set_pte_atomic(pte_t *ptep, pte_t pte)
|
|
|
|
{
|
|
|
|
native_set_pte(ptep, pte);
|
|
|
|
}
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2008-01-30 12:32:58 +00:00
|
|
|
static inline void native_pmd_clear(pmd_t *pmdp)
|
|
|
|
{
|
|
|
|
native_set_pmd(pmdp, __pmd(0));
|
|
|
|
}
|
[PATCH] x86/PAE: Fix pte_clear for the >4GB RAM case
Proposed fix for ptep_get_and_clear_full PAE bug. Pte_clear had the same bug,
so use the same fix for both. Turns out pmd_clear had it as well, but pgds
are not affected.
The problem is rather intricate. Page table entries in PAE mode are 64-bits
wide, but the only atomic 8-byte write operation available in 32-bit mode is
cmpxchg8b, which is expensive (at least on P4), and thus avoided. But it can
happen that the processor may prefetch entries into the TLB in the middle of an
operation which clears a page table entry. So one must always clear the P-bit
in the low word of the page table entry first when clearing it.
Since the sequence *ptep = __pte(0) leaves the order of the write dependent on
the compiler, it must be coded explicitly as a clear of the low word followed
by a clear of the high word. Further, there must be a write memory barrier
here to enforce proper ordering by the compiler (and, in the future, by the
processor as well).
On > 4GB memory machines, the implementation of pte_clear for PAE was clearly
deficient, as it could leave virtual mappings of physical memory above 4GB
aliased to memory below 4GB in the TLB. The implementation of
ptep_get_and_clear_full has a similar bug, although not nearly as likely to
occur, since the mappings being cleared are in the process of being destroyed,
and should never be dereferenced again.
But, as luck would have it, it is possible to trigger bugs even without ever
dereferencing these bogus TLB mappings, even if the clear is followed fairly
soon after with a TLB flush or invalidation. The problem is that memory above
4GB may now be aliased into the first 4GB of memory, and in fact, may hit a
region of memory with non-memory semantics. These regions include AGP and PCI
space. As such, these memory regions are not cached by the processor. This
introduces the bug.
The processor can speculate memory operations, including memory writes, as long
as they are committed with the proper ordering. Speculating a memory write to
a linear address that has a bogus TLB mapping is possible. Normally, the
speculation is harmless. But for cached memory, it does leave the falsely
speculated cacheline unmodified, but in a dirty state. This cache line will be
eventually written back. If this cacheline happens to intersect a region of
memory that is not protected by the cache coherency protocol, it can corrupt
data in I/O memory, which is generally a very bad thing to do, and can cause
total system failure or just plain undefined behavior.
These bugs are extremely unlikely, but the severity is of such magnitude, and
the fix so simple that I think fixing them immediately is justified. Also,
they are nearly impossible to debug.
Signed-off-by: Zachary Amsden <zach@vmware.com>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
2006-04-27 18:32:29 +00:00
|
|
|
|
2008-03-23 08:03:08 +00:00
|
|
|
static inline void native_pte_clear(struct mm_struct *mm,
|
|
|
|
unsigned long addr, pte_t *xp)
|
2007-05-02 17:27:19 +00:00
|
|
|
{
|
2008-01-30 12:32:58 +00:00
|
|
|
*xp = native_make_pte(0);
|
2007-05-02 17:27:19 +00:00
|
|
|
}
|
|
|
|
|
2007-05-02 17:27:19 +00:00
|
|
|
#ifdef CONFIG_SMP
|
2007-05-02 17:27:13 +00:00
|
|
|
static inline pte_t native_ptep_get_and_clear(pte_t *xp)
|
|
|
|
{
|
|
|
|
return __pte(xchg(&xp->pte_low, 0));
|
|
|
|
}
|
2007-05-02 17:27:19 +00:00
|
|
|
#else
|
|
|
|
#define native_ptep_get_and_clear(xp) native_local_ptep_get_and_clear(xp)
|
|
|
|
#endif
|
2006-09-26 06:32:30 +00:00
|
|
|
|
2011-01-13 23:47:01 +00:00
|
|
|
#ifdef CONFIG_SMP
|
|
|
|
static inline pmd_t native_pmdp_get_and_clear(pmd_t *xp)
|
|
|
|
{
|
|
|
|
return __pmd(xchg((pmdval_t *)xp, 0));
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define native_pmdp_get_and_clear(xp) native_local_pmdp_get_and_clear(xp)
|
|
|
|
#endif
|
|
|
|
|
2013-11-15 22:14:00 +00:00
|
|
|
/* Bit manipulation helper on pte/pgoff entry */
|
|
|
|
static inline unsigned long pte_bitop(unsigned long value, unsigned int rightshift,
|
|
|
|
unsigned long mask, unsigned int leftshift)
|
|
|
|
{
|
|
|
|
return ((value >> rightshift) & mask) << leftshift;
|
|
|
|
}
|
|
|
|
|
2013-08-13 23:00:51 +00:00
|
|
|
#ifdef CONFIG_MEM_SOFT_DIRTY
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Bits _PAGE_BIT_PRESENT, _PAGE_BIT_FILE, _PAGE_BIT_SOFT_DIRTY and
|
|
|
|
* _PAGE_BIT_PROTNONE are taken, split up the 28 bits of offset
|
|
|
|
* into this range.
|
|
|
|
*/
|
|
|
|
#define PTE_FILE_MAX_BITS 28
|
|
|
|
#define PTE_FILE_SHIFT1 (_PAGE_BIT_PRESENT + 1)
|
|
|
|
#define PTE_FILE_SHIFT2 (_PAGE_BIT_FILE + 1)
|
|
|
|
#define PTE_FILE_SHIFT3 (_PAGE_BIT_PROTNONE + 1)
|
|
|
|
#define PTE_FILE_SHIFT4 (_PAGE_BIT_SOFT_DIRTY + 1)
|
|
|
|
#define PTE_FILE_BITS1 (PTE_FILE_SHIFT2 - PTE_FILE_SHIFT1 - 1)
|
|
|
|
#define PTE_FILE_BITS2 (PTE_FILE_SHIFT3 - PTE_FILE_SHIFT2 - 1)
|
|
|
|
#define PTE_FILE_BITS3 (PTE_FILE_SHIFT4 - PTE_FILE_SHIFT3 - 1)
|
|
|
|
|
2013-11-15 22:14:00 +00:00
|
|
|
#define PTE_FILE_MASK1 ((1U << PTE_FILE_BITS1) - 1)
|
|
|
|
#define PTE_FILE_MASK2 ((1U << PTE_FILE_BITS2) - 1)
|
|
|
|
#define PTE_FILE_MASK3 ((1U << PTE_FILE_BITS3) - 1)
|
|
|
|
|
|
|
|
#define PTE_FILE_LSHIFT2 (PTE_FILE_BITS1)
|
|
|
|
#define PTE_FILE_LSHIFT3 (PTE_FILE_BITS1 + PTE_FILE_BITS2)
|
|
|
|
#define PTE_FILE_LSHIFT4 (PTE_FILE_BITS1 + PTE_FILE_BITS2 + PTE_FILE_BITS3)
|
|
|
|
|
|
|
|
static __always_inline pgoff_t pte_to_pgoff(pte_t pte)
|
|
|
|
{
|
|
|
|
return (pgoff_t)
|
|
|
|
(pte_bitop(pte.pte_low, PTE_FILE_SHIFT1, PTE_FILE_MASK1, 0) +
|
|
|
|
pte_bitop(pte.pte_low, PTE_FILE_SHIFT2, PTE_FILE_MASK2, PTE_FILE_LSHIFT2) +
|
|
|
|
pte_bitop(pte.pte_low, PTE_FILE_SHIFT3, PTE_FILE_MASK3, PTE_FILE_LSHIFT3) +
|
|
|
|
pte_bitop(pte.pte_low, PTE_FILE_SHIFT4, -1UL, PTE_FILE_LSHIFT4));
|
|
|
|
}
|
|
|
|
|
|
|
|
static __always_inline pte_t pgoff_to_pte(pgoff_t off)
|
|
|
|
{
|
|
|
|
return (pte_t){
|
|
|
|
.pte_low =
|
|
|
|
pte_bitop(off, 0, PTE_FILE_MASK1, PTE_FILE_SHIFT1) +
|
|
|
|
pte_bitop(off, PTE_FILE_LSHIFT2, PTE_FILE_MASK2, PTE_FILE_SHIFT2) +
|
|
|
|
pte_bitop(off, PTE_FILE_LSHIFT3, PTE_FILE_MASK3, PTE_FILE_SHIFT3) +
|
|
|
|
pte_bitop(off, PTE_FILE_LSHIFT4, -1UL, PTE_FILE_SHIFT4) +
|
|
|
|
_PAGE_FILE,
|
|
|
|
};
|
|
|
|
}
|
2013-08-13 23:00:51 +00:00
|
|
|
|
|
|
|
#else /* CONFIG_MEM_SOFT_DIRTY */
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
2008-12-16 11:35:24 +00:00
|
|
|
* Bits _PAGE_BIT_PRESENT, _PAGE_BIT_FILE and _PAGE_BIT_PROTNONE are taken,
|
2013-08-13 23:00:51 +00:00
|
|
|
* split up the 29 bits of offset into this range.
|
2005-04-16 22:20:36 +00:00
|
|
|
*/
|
|
|
|
#define PTE_FILE_MAX_BITS 29
|
2008-12-16 11:35:24 +00:00
|
|
|
#define PTE_FILE_SHIFT1 (_PAGE_BIT_PRESENT + 1)
|
|
|
|
#if _PAGE_BIT_FILE < _PAGE_BIT_PROTNONE
|
|
|
|
#define PTE_FILE_SHIFT2 (_PAGE_BIT_FILE + 1)
|
|
|
|
#define PTE_FILE_SHIFT3 (_PAGE_BIT_PROTNONE + 1)
|
|
|
|
#else
|
|
|
|
#define PTE_FILE_SHIFT2 (_PAGE_BIT_PROTNONE + 1)
|
|
|
|
#define PTE_FILE_SHIFT3 (_PAGE_BIT_FILE + 1)
|
|
|
|
#endif
|
|
|
|
#define PTE_FILE_BITS1 (PTE_FILE_SHIFT2 - PTE_FILE_SHIFT1 - 1)
|
|
|
|
#define PTE_FILE_BITS2 (PTE_FILE_SHIFT3 - PTE_FILE_SHIFT2 - 1)
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2013-11-15 22:14:00 +00:00
|
|
|
#define PTE_FILE_MASK1 ((1U << PTE_FILE_BITS1) - 1)
|
|
|
|
#define PTE_FILE_MASK2 ((1U << PTE_FILE_BITS2) - 1)
|
|
|
|
|
|
|
|
#define PTE_FILE_LSHIFT2 (PTE_FILE_BITS1)
|
|
|
|
#define PTE_FILE_LSHIFT3 (PTE_FILE_BITS1 + PTE_FILE_BITS2)
|
|
|
|
|
|
|
|
static __always_inline pgoff_t pte_to_pgoff(pte_t pte)
|
|
|
|
{
|
|
|
|
return (pgoff_t)
|
|
|
|
(pte_bitop(pte.pte_low, PTE_FILE_SHIFT1, PTE_FILE_MASK1, 0) +
|
|
|
|
pte_bitop(pte.pte_low, PTE_FILE_SHIFT2, PTE_FILE_MASK2, PTE_FILE_LSHIFT2) +
|
|
|
|
pte_bitop(pte.pte_low, PTE_FILE_SHIFT3, -1UL, PTE_FILE_LSHIFT3));
|
|
|
|
}
|
|
|
|
|
|
|
|
static __always_inline pte_t pgoff_to_pte(pgoff_t off)
|
|
|
|
{
|
|
|
|
return (pte_t){
|
|
|
|
.pte_low =
|
|
|
|
pte_bitop(off, 0, PTE_FILE_MASK1, PTE_FILE_SHIFT1) +
|
|
|
|
pte_bitop(off, PTE_FILE_LSHIFT2, PTE_FILE_MASK2, PTE_FILE_SHIFT2) +
|
|
|
|
pte_bitop(off, PTE_FILE_LSHIFT3, -1UL, PTE_FILE_SHIFT3) +
|
|
|
|
_PAGE_FILE,
|
|
|
|
};
|
|
|
|
}
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2013-08-13 23:00:51 +00:00
|
|
|
#endif /* CONFIG_MEM_SOFT_DIRTY */
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/* Encode and de-code a swap entry */
|
2008-12-16 11:35:24 +00:00
|
|
|
#if _PAGE_BIT_FILE < _PAGE_BIT_PROTNONE
|
|
|
|
#define SWP_TYPE_BITS (_PAGE_BIT_FILE - _PAGE_BIT_PRESENT - 1)
|
|
|
|
#define SWP_OFFSET_SHIFT (_PAGE_BIT_PROTNONE + 1)
|
|
|
|
#else
|
|
|
|
#define SWP_TYPE_BITS (_PAGE_BIT_PROTNONE - _PAGE_BIT_PRESENT - 1)
|
|
|
|
#define SWP_OFFSET_SHIFT (_PAGE_BIT_FILE + 1)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define MAX_SWAPFILES_CHECK() BUILD_BUG_ON(MAX_SWAPFILES_SHIFT > SWP_TYPE_BITS)
|
|
|
|
|
|
|
|
#define __swp_type(x) (((x).val >> (_PAGE_BIT_PRESENT + 1)) \
|
|
|
|
& ((1U << SWP_TYPE_BITS) - 1))
|
|
|
|
#define __swp_offset(x) ((x).val >> SWP_OFFSET_SHIFT)
|
|
|
|
#define __swp_entry(type, offset) ((swp_entry_t) { \
|
|
|
|
((type) << (_PAGE_BIT_PRESENT + 1)) \
|
|
|
|
| ((offset) << SWP_OFFSET_SHIFT) })
|
2005-04-16 22:20:36 +00:00
|
|
|
#define __pte_to_swp_entry(pte) ((swp_entry_t) { (pte).pte_low })
|
2008-01-30 12:32:57 +00:00
|
|
|
#define __swp_entry_to_pte(x) ((pte_t) { .pte = (x).val })
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2008-10-23 05:26:29 +00:00
|
|
|
#endif /* _ASM_X86_PGTABLE_2LEVEL_H */
|