Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | #include <linux/highmem.h> |
Alexey Dobriyan | 129f694 | 2005-06-23 00:08:33 -0700 | [diff] [blame] | 2 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | |
| 4 | void *kmap(struct page *page) |
| 5 | { |
| 6 | might_sleep(); |
| 7 | if (!PageHighMem(page)) |
| 8 | return page_address(page); |
| 9 | return kmap_high(page); |
| 10 | } |
| 11 | |
| 12 | void kunmap(struct page *page) |
| 13 | { |
| 14 | if (in_interrupt()) |
| 15 | BUG(); |
| 16 | if (!PageHighMem(page)) |
| 17 | return; |
| 18 | kunmap_high(page); |
| 19 | } |
| 20 | |
| 21 | /* |
| 22 | * kmap_atomic/kunmap_atomic is significantly faster than kmap/kunmap because |
| 23 | * no global lock is needed and because the kmap code must perform a global TLB |
| 24 | * invalidation when the kmap pool wraps. |
| 25 | * |
| 26 | * However when holding an atomic kmap is is not legal to sleep, so atomic |
| 27 | * kmaps are appropriate for short, tight code paths only. |
| 28 | */ |
| 29 | void *kmap_atomic(struct page *page, enum km_type type) |
| 30 | { |
| 31 | enum fixed_addresses idx; |
| 32 | unsigned long vaddr; |
| 33 | |
| 34 | /* even !CONFIG_PREEMPT needs this, for in_atomic in do_page_fault */ |
Peter Zijlstra | a866374 | 2006-12-06 20:32:20 -0800 | [diff] [blame^] | 35 | pagefault_disable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | if (!PageHighMem(page)) |
| 37 | return page_address(page); |
| 38 | |
| 39 | idx = type + KM_TYPE_NR*smp_processor_id(); |
| 40 | vaddr = __fix_to_virt(FIX_KMAP_BEGIN + idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | if (!pte_none(*(kmap_pte-idx))) |
| 42 | BUG(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | set_pte(kmap_pte-idx, mk_pte(page, kmap_prot)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | |
| 45 | return (void*) vaddr; |
| 46 | } |
| 47 | |
| 48 | void kunmap_atomic(void *kvaddr, enum km_type type) |
| 49 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | unsigned long vaddr = (unsigned long) kvaddr & PAGE_MASK; |
| 51 | enum fixed_addresses idx = type + KM_TYPE_NR*smp_processor_id(); |
| 52 | |
Zachary Amsden | 23002d8 | 2006-09-30 23:29:35 -0700 | [diff] [blame] | 53 | #ifdef CONFIG_DEBUG_HIGHMEM |
Jan Beulich | ba9c231 | 2006-09-26 10:52:31 +0200 | [diff] [blame] | 54 | if (vaddr >= PAGE_OFFSET && vaddr < (unsigned long)high_memory) { |
Peter Zijlstra | a866374 | 2006-12-06 20:32:20 -0800 | [diff] [blame^] | 55 | pagefault_enable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | return; |
| 57 | } |
| 58 | |
| 59 | if (vaddr != __fix_to_virt(FIX_KMAP_BEGIN+idx)) |
| 60 | BUG(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | #endif |
Zachary Amsden | 23002d8 | 2006-09-30 23:29:35 -0700 | [diff] [blame] | 62 | /* |
| 63 | * Force other mappings to Oops if they'll try to access this pte |
| 64 | * without first remap it. Keeping stale mappings around is a bad idea |
| 65 | * also, in case the page changes cacheability attributes or becomes |
| 66 | * a protected page in a hypervisor. |
| 67 | */ |
| 68 | kpte_clear_flush(kmap_pte-idx, vaddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | |
Peter Zijlstra | a866374 | 2006-12-06 20:32:20 -0800 | [diff] [blame^] | 70 | pagefault_enable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Vivek Goyal | 60e64d4 | 2005-06-25 14:58:19 -0700 | [diff] [blame] | 73 | /* This is the same as kmap_atomic() but can map memory that doesn't |
| 74 | * have a struct page associated with it. |
| 75 | */ |
| 76 | void *kmap_atomic_pfn(unsigned long pfn, enum km_type type) |
| 77 | { |
| 78 | enum fixed_addresses idx; |
| 79 | unsigned long vaddr; |
| 80 | |
Peter Zijlstra | a866374 | 2006-12-06 20:32:20 -0800 | [diff] [blame^] | 81 | pagefault_disable(); |
Vivek Goyal | 60e64d4 | 2005-06-25 14:58:19 -0700 | [diff] [blame] | 82 | |
| 83 | idx = type + KM_TYPE_NR*smp_processor_id(); |
| 84 | vaddr = __fix_to_virt(FIX_KMAP_BEGIN + idx); |
| 85 | set_pte(kmap_pte-idx, pfn_pte(pfn, kmap_prot)); |
Vivek Goyal | 60e64d4 | 2005-06-25 14:58:19 -0700 | [diff] [blame] | 86 | |
| 87 | return (void*) vaddr; |
| 88 | } |
| 89 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | struct page *kmap_atomic_to_page(void *ptr) |
| 91 | { |
| 92 | unsigned long idx, vaddr = (unsigned long)ptr; |
| 93 | pte_t *pte; |
| 94 | |
| 95 | if (vaddr < FIXADDR_START) |
| 96 | return virt_to_page(ptr); |
| 97 | |
| 98 | idx = virt_to_fix(vaddr); |
| 99 | pte = kmap_pte - (idx - FIX_KMAP_BEGIN); |
| 100 | return pte_page(*pte); |
| 101 | } |
| 102 | |
Alexey Dobriyan | 129f694 | 2005-06-23 00:08:33 -0700 | [diff] [blame] | 103 | EXPORT_SYMBOL(kmap); |
| 104 | EXPORT_SYMBOL(kunmap); |
| 105 | EXPORT_SYMBOL(kmap_atomic); |
| 106 | EXPORT_SYMBOL(kunmap_atomic); |
| 107 | EXPORT_SYMBOL(kmap_atomic_to_page); |