Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * SPARC64 Huge TLB page support. |
| 3 | * |
David S. Miller | f6b83f0 | 2006-03-20 01:17:17 -0800 | [diff] [blame] | 4 | * Copyright (C) 2002, 2003, 2006 David S. Miller (davem@davemloft.net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | */ |
| 6 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | #include <linux/fs.h> |
| 8 | #include <linux/mm.h> |
| 9 | #include <linux/hugetlb.h> |
| 10 | #include <linux/pagemap.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/sysctl.h> |
| 12 | |
| 13 | #include <asm/mman.h> |
| 14 | #include <asm/pgalloc.h> |
| 15 | #include <asm/tlb.h> |
| 16 | #include <asm/tlbflush.h> |
| 17 | #include <asm/cacheflush.h> |
| 18 | #include <asm/mmu_context.h> |
| 19 | |
David S. Miller | f6b83f0 | 2006-03-20 01:17:17 -0800 | [diff] [blame] | 20 | /* Slightly simplified from the non-hugepage variant because by |
| 21 | * definition we don't have to worry about any page coloring stuff |
| 22 | */ |
David S. Miller | f6b83f0 | 2006-03-20 01:17:17 -0800 | [diff] [blame] | 23 | |
| 24 | static unsigned long hugetlb_get_unmapped_area_bottomup(struct file *filp, |
| 25 | unsigned long addr, |
| 26 | unsigned long len, |
| 27 | unsigned long pgoff, |
| 28 | unsigned long flags) |
| 29 | { |
David S. Miller | f6b83f0 | 2006-03-20 01:17:17 -0800 | [diff] [blame] | 30 | unsigned long task_size = TASK_SIZE; |
Michel Lespinasse | 2aea28b | 2012-12-11 16:02:25 -0800 | [diff] [blame] | 31 | struct vm_unmapped_area_info info; |
David S. Miller | f6b83f0 | 2006-03-20 01:17:17 -0800 | [diff] [blame] | 32 | |
| 33 | if (test_thread_flag(TIF_32BIT)) |
| 34 | task_size = STACK_TOP32; |
David S. Miller | f6b83f0 | 2006-03-20 01:17:17 -0800 | [diff] [blame] | 35 | |
Michel Lespinasse | 2aea28b | 2012-12-11 16:02:25 -0800 | [diff] [blame] | 36 | info.flags = 0; |
| 37 | info.length = len; |
| 38 | info.low_limit = TASK_UNMAPPED_BASE; |
| 39 | info.high_limit = min(task_size, VA_EXCLUDE_START); |
| 40 | info.align_mask = PAGE_MASK & ~HPAGE_MASK; |
| 41 | info.align_offset = 0; |
| 42 | addr = vm_unmapped_area(&info); |
| 43 | |
| 44 | if ((addr & ~PAGE_MASK) && task_size > VA_EXCLUDE_END) { |
| 45 | VM_BUG_ON(addr != -ENOMEM); |
| 46 | info.low_limit = VA_EXCLUDE_END; |
| 47 | info.high_limit = task_size; |
| 48 | addr = vm_unmapped_area(&info); |
David S. Miller | f6b83f0 | 2006-03-20 01:17:17 -0800 | [diff] [blame] | 49 | } |
| 50 | |
Michel Lespinasse | 2aea28b | 2012-12-11 16:02:25 -0800 | [diff] [blame] | 51 | return addr; |
David S. Miller | f6b83f0 | 2006-03-20 01:17:17 -0800 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | static unsigned long |
| 55 | hugetlb_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0, |
| 56 | const unsigned long len, |
| 57 | const unsigned long pgoff, |
| 58 | const unsigned long flags) |
| 59 | { |
David S. Miller | f6b83f0 | 2006-03-20 01:17:17 -0800 | [diff] [blame] | 60 | struct mm_struct *mm = current->mm; |
| 61 | unsigned long addr = addr0; |
Michel Lespinasse | 2aea28b | 2012-12-11 16:02:25 -0800 | [diff] [blame] | 62 | struct vm_unmapped_area_info info; |
David S. Miller | f6b83f0 | 2006-03-20 01:17:17 -0800 | [diff] [blame] | 63 | |
| 64 | /* This should only ever run for 32-bit processes. */ |
| 65 | BUG_ON(!test_thread_flag(TIF_32BIT)); |
| 66 | |
Michel Lespinasse | 2aea28b | 2012-12-11 16:02:25 -0800 | [diff] [blame] | 67 | info.flags = VM_UNMAPPED_AREA_TOPDOWN; |
| 68 | info.length = len; |
| 69 | info.low_limit = PAGE_SIZE; |
| 70 | info.high_limit = mm->mmap_base; |
| 71 | info.align_mask = PAGE_MASK & ~HPAGE_MASK; |
| 72 | info.align_offset = 0; |
| 73 | addr = vm_unmapped_area(&info); |
David S. Miller | f6b83f0 | 2006-03-20 01:17:17 -0800 | [diff] [blame] | 74 | |
David S. Miller | f6b83f0 | 2006-03-20 01:17:17 -0800 | [diff] [blame] | 75 | /* |
| 76 | * A failed mmap() very likely causes application failure, |
| 77 | * so fall back to the bottom-up function here. This scenario |
| 78 | * can happen with large stack limits and large mmap() |
| 79 | * allocations. |
| 80 | */ |
Michel Lespinasse | 2aea28b | 2012-12-11 16:02:25 -0800 | [diff] [blame] | 81 | if (addr & ~PAGE_MASK) { |
| 82 | VM_BUG_ON(addr != -ENOMEM); |
| 83 | info.flags = 0; |
| 84 | info.low_limit = TASK_UNMAPPED_BASE; |
| 85 | info.high_limit = STACK_TOP32; |
| 86 | addr = vm_unmapped_area(&info); |
| 87 | } |
David S. Miller | f6b83f0 | 2006-03-20 01:17:17 -0800 | [diff] [blame] | 88 | |
| 89 | return addr; |
| 90 | } |
| 91 | |
| 92 | unsigned long |
| 93 | hugetlb_get_unmapped_area(struct file *file, unsigned long addr, |
| 94 | unsigned long len, unsigned long pgoff, unsigned long flags) |
| 95 | { |
| 96 | struct mm_struct *mm = current->mm; |
| 97 | struct vm_area_struct *vma; |
| 98 | unsigned long task_size = TASK_SIZE; |
| 99 | |
| 100 | if (test_thread_flag(TIF_32BIT)) |
| 101 | task_size = STACK_TOP32; |
| 102 | |
| 103 | if (len & ~HPAGE_MASK) |
| 104 | return -EINVAL; |
| 105 | if (len > task_size) |
| 106 | return -ENOMEM; |
| 107 | |
Benjamin Herrenschmidt | ac35ee4 | 2007-05-06 14:50:10 -0700 | [diff] [blame] | 108 | if (flags & MAP_FIXED) { |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 109 | if (prepare_hugepage_range(file, addr, len)) |
Benjamin Herrenschmidt | ac35ee4 | 2007-05-06 14:50:10 -0700 | [diff] [blame] | 110 | return -EINVAL; |
| 111 | return addr; |
| 112 | } |
| 113 | |
David S. Miller | f6b83f0 | 2006-03-20 01:17:17 -0800 | [diff] [blame] | 114 | if (addr) { |
| 115 | addr = ALIGN(addr, HPAGE_SIZE); |
| 116 | vma = find_vma(mm, addr); |
| 117 | if (task_size - len >= addr && |
| 118 | (!vma || addr + len <= vma->vm_start)) |
| 119 | return addr; |
| 120 | } |
| 121 | if (mm->get_unmapped_area == arch_get_unmapped_area) |
| 122 | return hugetlb_get_unmapped_area_bottomup(file, addr, len, |
| 123 | pgoff, flags); |
| 124 | else |
| 125 | return hugetlb_get_unmapped_area_topdown(file, addr, len, |
| 126 | pgoff, flags); |
| 127 | } |
| 128 | |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 129 | pte_t *huge_pte_alloc(struct mm_struct *mm, |
| 130 | unsigned long addr, unsigned long sz) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | { |
| 132 | pgd_t *pgd; |
| 133 | pud_t *pud; |
| 134 | pmd_t *pmd; |
| 135 | pte_t *pte = NULL; |
| 136 | |
David S. Miller | 9df1dab | 2006-03-31 00:36:25 -0800 | [diff] [blame] | 137 | /* We must align the address, because our caller will run |
| 138 | * set_huge_pte_at() on whatever we return, which writes out |
| 139 | * all of the sub-ptes for the hugepage range. So we have |
| 140 | * to give it the first such sub-pte. |
| 141 | */ |
| 142 | addr &= HPAGE_MASK; |
| 143 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | pgd = pgd_offset(mm, addr); |
David S. Miller | dcc1e8d | 2006-03-22 00:49:59 -0800 | [diff] [blame] | 145 | pud = pud_alloc(mm, pgd, addr); |
| 146 | if (pud) { |
| 147 | pmd = pmd_alloc(mm, pud, addr); |
| 148 | if (pmd) |
Kirill A. Shutemov | 3ed3a4f | 2016-03-17 14:19:11 -0700 | [diff] [blame] | 149 | pte = pte_alloc_map(mm, pmd, addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | } |
| 151 | return pte; |
| 152 | } |
| 153 | |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 154 | pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | { |
| 156 | pgd_t *pgd; |
| 157 | pud_t *pud; |
| 158 | pmd_t *pmd; |
| 159 | pte_t *pte = NULL; |
| 160 | |
David S. Miller | f6b83f0 | 2006-03-20 01:17:17 -0800 | [diff] [blame] | 161 | addr &= HPAGE_MASK; |
| 162 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | pgd = pgd_offset(mm, addr); |
David S. Miller | f6b83f0 | 2006-03-20 01:17:17 -0800 | [diff] [blame] | 164 | if (!pgd_none(*pgd)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | pud = pud_offset(pgd, addr); |
David S. Miller | f6b83f0 | 2006-03-20 01:17:17 -0800 | [diff] [blame] | 166 | if (!pud_none(*pud)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | pmd = pmd_offset(pud, addr); |
David S. Miller | f6b83f0 | 2006-03-20 01:17:17 -0800 | [diff] [blame] | 168 | if (!pmd_none(*pmd)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | pte = pte_offset_map(pmd, addr); |
| 170 | } |
| 171 | } |
| 172 | return pte; |
| 173 | } |
| 174 | |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 175 | void set_huge_pte_at(struct mm_struct *mm, unsigned long addr, |
| 176 | pte_t *ptep, pte_t entry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | { |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 178 | int i; |
Nitin Gupta | 24e49ee | 2016-03-30 11:17:13 -0700 | [diff] [blame] | 179 | pte_t orig[2]; |
| 180 | unsigned long nptes; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | |
David S. Miller | dcc1e8d | 2006-03-22 00:49:59 -0800 | [diff] [blame] | 182 | if (!pte_present(*ptep) && pte_present(entry)) |
| 183 | mm->context.huge_pte_count++; |
| 184 | |
David S. Miller | bb8236f | 2007-03-12 22:55:39 -0700 | [diff] [blame] | 185 | addr &= HPAGE_MASK; |
Nitin Gupta | 24e49ee | 2016-03-30 11:17:13 -0700 | [diff] [blame] | 186 | |
| 187 | nptes = 1 << HUGETLB_PAGE_ORDER; |
| 188 | orig[0] = *ptep; |
| 189 | orig[1] = *(ptep + nptes / 2); |
| 190 | for (i = 0; i < nptes; i++) { |
| 191 | *ptep = entry; |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 192 | ptep++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | addr += PAGE_SIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | pte_val(entry) += PAGE_SIZE; |
| 195 | } |
Nitin Gupta | 24e49ee | 2016-03-30 11:17:13 -0700 | [diff] [blame] | 196 | |
| 197 | /* Issue TLB flush at REAL_HPAGE_SIZE boundaries */ |
| 198 | addr -= REAL_HPAGE_SIZE; |
| 199 | ptep -= nptes / 2; |
| 200 | maybe_tlb_batch_add(mm, addr, ptep, orig[1], 0); |
| 201 | addr -= REAL_HPAGE_SIZE; |
| 202 | ptep -= nptes / 2; |
| 203 | maybe_tlb_batch_add(mm, addr, ptep, orig[0], 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | } |
| 205 | |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 206 | pte_t huge_ptep_get_and_clear(struct mm_struct *mm, unsigned long addr, |
| 207 | pte_t *ptep) |
| 208 | { |
| 209 | pte_t entry; |
| 210 | int i; |
Nitin Gupta | 24e49ee | 2016-03-30 11:17:13 -0700 | [diff] [blame] | 211 | unsigned long nptes; |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 212 | |
| 213 | entry = *ptep; |
David S. Miller | dcc1e8d | 2006-03-22 00:49:59 -0800 | [diff] [blame] | 214 | if (pte_present(entry)) |
| 215 | mm->context.huge_pte_count--; |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 216 | |
David S. Miller | bb8236f | 2007-03-12 22:55:39 -0700 | [diff] [blame] | 217 | addr &= HPAGE_MASK; |
Nitin Gupta | 24e49ee | 2016-03-30 11:17:13 -0700 | [diff] [blame] | 218 | nptes = 1 << HUGETLB_PAGE_ORDER; |
| 219 | for (i = 0; i < nptes; i++) { |
| 220 | *ptep = __pte(0UL); |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 221 | addr += PAGE_SIZE; |
| 222 | ptep++; |
| 223 | } |
| 224 | |
Nitin Gupta | 24e49ee | 2016-03-30 11:17:13 -0700 | [diff] [blame] | 225 | /* Issue TLB flush at REAL_HPAGE_SIZE boundaries */ |
| 226 | addr -= REAL_HPAGE_SIZE; |
| 227 | ptep -= nptes / 2; |
| 228 | maybe_tlb_batch_add(mm, addr, ptep, entry, 0); |
| 229 | addr -= REAL_HPAGE_SIZE; |
| 230 | ptep -= nptes / 2; |
| 231 | maybe_tlb_batch_add(mm, addr, ptep, entry, 0); |
| 232 | |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 233 | return entry; |
| 234 | } |
| 235 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | int pmd_huge(pmd_t pmd) |
| 237 | { |
| 238 | return 0; |
| 239 | } |
| 240 | |
Andi Kleen | ceb8687 | 2008-07-23 21:27:50 -0700 | [diff] [blame] | 241 | int pud_huge(pud_t pud) |
| 242 | { |
| 243 | return 0; |
| 244 | } |