x86: --- arch/x86_64/mm/pat.c | 62 ++++++++++++++++++++++++++++++++++++++----- drivers/char/mem.c | 48 ++++++++++++--------------------- include/asm-i386/pgtable.h | 4 ++ include/asm-x86_64/pgtable.h | 4 ++ 4 files changed, 82 insertions(+), 36 deletions(-) Index: linux/drivers/char/mem.c =================================================================== --- linux.orig/drivers/char/mem.c +++ linux/drivers/char/mem.c @@ -41,36 +41,7 @@ */ static inline int uncached_access(struct file *file, unsigned long addr) { -#if defined(__i386__) - /* - * On the PPro and successors, the MTRRs are used to set - * memory types for physical addresses outside main memory, - * so blindly setting PCD or PWT on those pages is wrong. - * For Pentiums and earlier, the surround logic should disable - * caching for the high addresses through the KEN pin, but - * we maintain the tradition of paranoia in this code. - */ - if (file->f_flags & O_SYNC) - return 1; - return !( test_bit(X86_FEATURE_MTRR, boot_cpu_data.x86_capability) || - test_bit(X86_FEATURE_K6_MTRR, boot_cpu_data.x86_capability) || - test_bit(X86_FEATURE_CYRIX_ARR, boot_cpu_data.x86_capability) || - test_bit(X86_FEATURE_CENTAUR_MCR, boot_cpu_data.x86_capability) ) - && addr >= __pa(high_memory); -#elif defined(__x86_64__) - /* - * This is broken because it can generate memory type aliases, - * which can cause cache corruptions - * But it is only available for root and we have to be bug-to-bug - * compatible with i386. - */ - if (file->f_flags & O_SYNC) - return 1; - /* same behaviour as i386. PAT always set to cached and MTRRs control the - caching behaviour. - Hopefully a full PAT implementation will fix that soon. */ - return 0; -#elif defined(CONFIG_IA64) +#if defined(CONFIG_IA64) /* * On ia64, we ignore O_SYNC because we cannot tolerate memory attribute aliases. */ @@ -238,6 +209,21 @@ static pgprot_t phys_mem_access_prot(str } #endif +void __attribute__((weak)) +unmap_devmem(unsigned long pfn, unsigned long len) +{ + /* nothing. architectures can override. */ +} + +static void mmap_mem_close(struct vm_area_struct *vma) +{ + unmap_devmem(vma->vm_pgoff, vma->vm_end - vma->vm_start); +} + +static struct vm_operations_struct mmap_mem_ops = { + .close = mmap_mem_close +}; + static int mmap_mem(struct file * file, struct vm_area_struct * vma) { size_t size = vma->vm_end - vma->vm_start; @@ -249,6 +235,8 @@ static int mmap_mem(struct file * file, size, vma->vm_page_prot); + vma->vm_ops = &mmap_mem_ops; + /* Remap-pfn-range will mark the range VM_IO and VM_RESERVED */ if (remap_pfn_range(vma, vma->vm_start, Index: linux/arch/x86_64/mm/pat.c =================================================================== --- linux.orig/arch/x86_64/mm/pat.c +++ linux/arch/x86_64/mm/pat.c @@ -8,6 +8,7 @@ #include #include #include +#include static u64 boot_pat_state; @@ -54,6 +55,16 @@ void pat_shutdown(void) wrmsrl(MSR_IA32_CR_PAT, boot_pat_state); } +static char *cattr_name(unsigned long flags) +{ + switch (flags & _PAGE_CACHE_MASK) { + case _PAGE_WC: return "write combining"; + case _PAGE_PCD: return "uncached"; + case 0: return "default"; + default: return "broken"; + } +} + /* The global memattr list keeps track of caching attributes for specific physical memory areas. Conflicting caching attributes in different mappings can cause CPU cache corruption. To avoid this we keep track. @@ -102,9 +113,10 @@ int reserve_mattr(u64 start, u64 end, un } if (attr != ml->attr) { printk( - KERN_ERR "%s:%d conflicting cache attribute %Lx-%Lx %x<->%x\n", + KERN_ERR "%s:%d conflicting cache attribute %Lx-%Lx %s<->%s\n", current->comm, current->pid, - start, end, attr, ml->attr); + start, end, + cattr_name(attr), cattr_name(ml->attr)); err = -EBUSY; break; } @@ -131,8 +143,9 @@ int free_mattr(u64 start, u64 end, unsig if (ml->start == start && ml->end == end) { if (ml->attr != attr) printk(KERN_ERR - "%s:%d conflicting cache attributes on free %Lx-%Lx %x<->%x\n", - current->comm, current->pid, start, end, attr,ml->attr); + "%s:%d conflicting cache attributes on free %Lx-%Lx %s<->%s\n", + current->comm, current->pid, start, end, + cattr_name(attr), cattr_name(ml->attr)); list_del(&ml->nd); err = 0; break; @@ -140,9 +153,48 @@ int free_mattr(u64 start, u64 end, unsig } spin_unlock(&mattr_lock); if (err) - printk(KERN_ERR "%s:%d freeing invalid mattr %Lx-%Lx %x\n", + printk(KERN_ERR "%s:%d freeing invalid mattr %Lx-%Lx %s\n", current->comm, current->pid, - start, end, attr); + start, end, cattr_name(attr)); return err; } +/* /dev/mem interface. Use the previous mapping */ +pgprot_t +phys_mem_access_prot(struct file *file, unsigned long pfn, unsigned long size, + pgprot_t vma_prot) +{ + u64 offset = pfn << PAGE_SHIFT; + unsigned long flags; + unsigned long want_flags = 0; + if ((file->f_flags & O_SYNC) || (offset >= __pa(high_memory))) + want_flags = _PAGE_PCD; + + /* ignore error because we can't handle it here */ + reserve_mattr(offset, offset+size, want_flags, &flags); + if (flags != want_flags) { + printk(KERN_INFO + "%s:%d /dev/mem expected mapping type %s for %Lx-%Lx, got %s\n", + current->comm, current->pid, + cattr_name(want_flags), + offset, offset+size, + cattr_name(flags)); + } + + if (offset < __pa(high_memory)) { + /* RED-PEN when the kernel memory was write protected + or similar before we'll destroy that here. need a pgprot + mask in cpa? */ + change_page_attr_addr(offset, size >> PAGE_SHIFT, + __pgprot(__PAGE_KERNEL | flags)); + } + return __pgprot((pgprot_val(vma_prot) & ~_PAGE_CACHE_MASK)|flags); +} + +void unmap_devmem(unsigned long pfn, unsigned long size) +{ + u64 addr = (u64)pfn << PAGE_SHIFT; + free_mattr(addr, size, 0); + if (addr < __pa(high_memory)) + change_page_attr_addr(addr, size >> PAGE_SHIFT, PAGE_KERNEL); +} Index: linux/include/asm-x86_64/pgtable.h =================================================================== --- linux.orig/include/asm-x86_64/pgtable.h +++ linux/include/asm-x86_64/pgtable.h @@ -474,4 +474,9 @@ extern int kern_addr_valid(unsigned long #define __HAVE_ARCH_PTE_SAME #include +#define __HAVE_PHYS_MEM_ACCESS_PROT +struct file; +pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn, + unsigned long size, pgprot_t vma_prot); + #endif /* _X86_64_PGTABLE_H */ Index: linux/include/asm-i386/pgtable.h =================================================================== --- linux.orig/include/asm-i386/pgtable.h +++ linux/include/asm-i386/pgtable.h @@ -477,4 +477,11 @@ extern void noexec_setup(const char *str #define __HAVE_ARCH_PTE_SAME #include +#ifndef __ASSEMBLY__ +#define __HAVE_PHYS_MEM_ACCESS_PROT +struct file; +pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn, + unsigned long size, pgprot_t vma_prot); +#endif + #endif /* _I386_PGTABLE_H */