1 // SPDX-License-Identifier: GPL-2.0 !! 1 /* 2 // Copyright (C) 2018 Hangzhou C-SKY Microsyst !! 2 * highmem.c: virtual kernel memory mappings for high memory 3 !! 3 * 4 #include <linux/module.h> !! 4 * Provides kernel-static versions of atomic kmap functions originally >> 5 * found as inlines in include/asm-sparc/highmem.h. These became >> 6 * needed as kmap_atomic() and kunmap_atomic() started getting >> 7 * called from within modules. >> 8 * -- Tomas Szepe <szepe@pinerecords.com>, September 2002 >> 9 * >> 10 * But kmap_atomic() and kunmap_atomic() cannot be inlined in >> 11 * modules because they are loaded with btfixup-ped functions. >> 12 */ >> 13 >> 14 /* >> 15 * The use of kmap_atomic/kunmap_atomic is discouraged - kmap/kunmap >> 16 * gives a more generic (and caching) interface. But kmap_atomic can >> 17 * be used in IRQ contexts, so in some (very limited) cases we need it. >> 18 * >> 19 * XXX This is an old text. Actually, it's good to use atomic kmaps, >> 20 * provided you remember that they are atomic and not try to sleep >> 21 * with a kmap taken, much like a spinlock. Non-atomic kmaps are >> 22 * shared by CPUs, and so precious, and establishing them requires IPI. >> 23 * Atomic kmaps are lightweight and we may have NCPUS more of them. >> 24 */ 5 #include <linux/highmem.h> 25 #include <linux/highmem.h> 6 #include <linux/smp.h> !! 26 #include <linux/export.h> 7 #include <linux/memblock.h> !! 27 #include <linux/mm.h> 8 #include <asm/fixmap.h> !! 28 9 #include <asm/tlbflush.h> << 10 #include <asm/cacheflush.h> 29 #include <asm/cacheflush.h> >> 30 #include <asm/tlbflush.h> >> 31 #include <asm/pgalloc.h> >> 32 #include <asm/vaddrs.h> 11 33 12 unsigned long highstart_pfn, highend_pfn; !! 34 pgprot_t kmap_prot; 13 35 14 void kmap_flush_tlb(unsigned long addr) !! 36 static pte_t *kmap_pte; >> 37 >> 38 void __init kmap_init(void) 15 { 39 { 16 flush_tlb_one(addr); !! 40 unsigned long address; >> 41 pmd_t *dir; >> 42 >> 43 address = __fix_to_virt(FIX_KMAP_BEGIN); >> 44 dir = pmd_offset(pgd_offset_k(address), address); >> 45 >> 46 /* cache the first kmap pte */ >> 47 kmap_pte = pte_offset_kernel(dir, address); >> 48 kmap_prot = __pgprot(SRMMU_ET_PTE | SRMMU_PRIV | SRMMU_CACHE); 17 } 49 } 18 EXPORT_SYMBOL(kmap_flush_tlb); << 19 50 20 void __init kmap_init(void) !! 51 void *kmap_atomic(struct page *page) 21 { 52 { 22 unsigned long vaddr; 53 unsigned long vaddr; 23 pgd_t *pgd; !! 54 long idx, type; 24 pmd_t *pmd; !! 55 25 pud_t *pud; !! 56 preempt_disable(); 26 pte_t *pte; !! 57 pagefault_disable(); 27 !! 58 if (!PageHighMem(page)) 28 vaddr = PKMAP_BASE; !! 59 return page_address(page); 29 fixrange_init(vaddr, vaddr + PAGE_SIZE !! 60 30 !! 61 type = kmap_atomic_idx_push(); 31 pgd = swapper_pg_dir + pgd_index(vaddr !! 62 idx = type + KM_TYPE_NR*smp_processor_id(); 32 pud = (pud_t *)pgd; !! 63 vaddr = __fix_to_virt(FIX_KMAP_BEGIN + idx); 33 pmd = pmd_offset(pud, vaddr); !! 64 34 pte = pte_offset_kernel(pmd, vaddr); !! 65 /* XXX Fix - Anton */ 35 pkmap_page_table = pte; !! 66 #if 0 >> 67 __flush_cache_one(vaddr); >> 68 #else >> 69 flush_cache_all(); >> 70 #endif >> 71 >> 72 #ifdef CONFIG_DEBUG_HIGHMEM >> 73 BUG_ON(!pte_none(*(kmap_pte-idx))); >> 74 #endif >> 75 set_pte(kmap_pte-idx, mk_pte(page, kmap_prot)); >> 76 /* XXX Fix - Anton */ >> 77 #if 0 >> 78 __flush_tlb_one(vaddr); >> 79 #else >> 80 flush_tlb_all(); >> 81 #endif >> 82 >> 83 return (void*) vaddr; >> 84 } >> 85 EXPORT_SYMBOL(kmap_atomic); >> 86 >> 87 void __kunmap_atomic(void *kvaddr) >> 88 { >> 89 unsigned long vaddr = (unsigned long) kvaddr & PAGE_MASK; >> 90 int type; >> 91 >> 92 if (vaddr < FIXADDR_START) { // FIXME >> 93 pagefault_enable(); >> 94 preempt_enable(); >> 95 return; >> 96 } >> 97 >> 98 type = kmap_atomic_idx(); >> 99 >> 100 #ifdef CONFIG_DEBUG_HIGHMEM >> 101 { >> 102 unsigned long idx; >> 103 >> 104 idx = type + KM_TYPE_NR * smp_processor_id(); >> 105 BUG_ON(vaddr != __fix_to_virt(FIX_KMAP_BEGIN+idx)); >> 106 >> 107 /* XXX Fix - Anton */ >> 108 #if 0 >> 109 __flush_cache_one(vaddr); >> 110 #else >> 111 flush_cache_all(); >> 112 #endif >> 113 >> 114 /* >> 115 * force other mappings to Oops if they'll try to access >> 116 * this pte without first remap it >> 117 */ >> 118 pte_clear(&init_mm, vaddr, kmap_pte-idx); >> 119 /* XXX Fix - Anton */ >> 120 #if 0 >> 121 __flush_tlb_one(vaddr); >> 122 #else >> 123 flush_tlb_all(); >> 124 #endif >> 125 } >> 126 #endif >> 127 >> 128 kmap_atomic_idx_pop(); >> 129 pagefault_enable(); >> 130 preempt_enable(); 36 } 131 } >> 132 EXPORT_SYMBOL(__kunmap_atomic); 37 133
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.