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/init.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | #include <linux/fs.h> |
| 9 | #include <linux/mm.h> |
| 10 | #include <linux/hugetlb.h> |
| 11 | #include <linux/pagemap.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/sysctl.h> |
| 13 | |
| 14 | #include <asm/mman.h> |
| 15 | #include <asm/pgalloc.h> |
| 16 | #include <asm/tlb.h> |
| 17 | #include <asm/tlbflush.h> |
| 18 | #include <asm/cacheflush.h> |
| 19 | #include <asm/mmu_context.h> |
| 20 | |
David S. Miller | f6b83f0 | 2006-03-20 01:17:17 -0800 | [diff] [blame] | 21 | /* Slightly simplified from the non-hugepage variant because by |
| 22 | * definition we don't have to worry about any page coloring stuff |
| 23 | */ |
| 24 | #define VA_EXCLUDE_START (0x0000080000000000UL - (1UL << 32UL)) |
| 25 | #define VA_EXCLUDE_END (0xfffff80000000000UL + (1UL << 32UL)) |
| 26 | |
| 27 | static unsigned long hugetlb_get_unmapped_area_bottomup(struct file *filp, |
| 28 | unsigned long addr, |
| 29 | unsigned long len, |
| 30 | unsigned long pgoff, |
| 31 | unsigned long flags) |
| 32 | { |
| 33 | struct mm_struct *mm = current->mm; |
| 34 | struct vm_area_struct * vma; |
| 35 | unsigned long task_size = TASK_SIZE; |
| 36 | unsigned long start_addr; |
| 37 | |
| 38 | if (test_thread_flag(TIF_32BIT)) |
| 39 | task_size = STACK_TOP32; |
| 40 | if (unlikely(len >= VA_EXCLUDE_START)) |
| 41 | return -ENOMEM; |
| 42 | |
| 43 | if (len > mm->cached_hole_size) { |
| 44 | start_addr = addr = mm->free_area_cache; |
| 45 | } else { |
| 46 | start_addr = addr = TASK_UNMAPPED_BASE; |
| 47 | mm->cached_hole_size = 0; |
| 48 | } |
| 49 | |
| 50 | task_size -= len; |
| 51 | |
| 52 | full_search: |
| 53 | addr = ALIGN(addr, HPAGE_SIZE); |
| 54 | |
| 55 | for (vma = find_vma(mm, addr); ; vma = vma->vm_next) { |
| 56 | /* At this point: (!vma || addr < vma->vm_end). */ |
| 57 | if (addr < VA_EXCLUDE_START && |
| 58 | (addr + len) >= VA_EXCLUDE_START) { |
| 59 | addr = VA_EXCLUDE_END; |
| 60 | vma = find_vma(mm, VA_EXCLUDE_END); |
| 61 | } |
| 62 | if (unlikely(task_size < addr)) { |
| 63 | if (start_addr != TASK_UNMAPPED_BASE) { |
| 64 | start_addr = addr = TASK_UNMAPPED_BASE; |
| 65 | mm->cached_hole_size = 0; |
| 66 | goto full_search; |
| 67 | } |
| 68 | return -ENOMEM; |
| 69 | } |
| 70 | if (likely(!vma || addr + len <= vma->vm_start)) { |
| 71 | /* |
| 72 | * Remember the place where we stopped the search: |
| 73 | */ |
| 74 | mm->free_area_cache = addr + len; |
| 75 | return addr; |
| 76 | } |
| 77 | if (addr + mm->cached_hole_size < vma->vm_start) |
| 78 | mm->cached_hole_size = vma->vm_start - addr; |
| 79 | |
| 80 | addr = ALIGN(vma->vm_end, HPAGE_SIZE); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | static unsigned long |
| 85 | hugetlb_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0, |
| 86 | const unsigned long len, |
| 87 | const unsigned long pgoff, |
| 88 | const unsigned long flags) |
| 89 | { |
| 90 | struct vm_area_struct *vma; |
| 91 | struct mm_struct *mm = current->mm; |
| 92 | unsigned long addr = addr0; |
| 93 | |
| 94 | /* This should only ever run for 32-bit processes. */ |
| 95 | BUG_ON(!test_thread_flag(TIF_32BIT)); |
| 96 | |
| 97 | /* check if free_area_cache is useful for us */ |
| 98 | if (len <= mm->cached_hole_size) { |
| 99 | mm->cached_hole_size = 0; |
| 100 | mm->free_area_cache = mm->mmap_base; |
| 101 | } |
| 102 | |
| 103 | /* either no address requested or can't fit in requested address hole */ |
| 104 | addr = mm->free_area_cache & HPAGE_MASK; |
| 105 | |
| 106 | /* make sure it can fit in the remaining address space */ |
| 107 | if (likely(addr > len)) { |
| 108 | vma = find_vma(mm, addr-len); |
| 109 | if (!vma || addr <= vma->vm_start) { |
| 110 | /* remember the address as a hint for next time */ |
| 111 | return (mm->free_area_cache = addr-len); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | if (unlikely(mm->mmap_base < len)) |
| 116 | goto bottomup; |
| 117 | |
| 118 | addr = (mm->mmap_base-len) & HPAGE_MASK; |
| 119 | |
| 120 | do { |
| 121 | /* |
| 122 | * Lookup failure means no vma is above this address, |
| 123 | * else if new region fits below vma->vm_start, |
| 124 | * return with success: |
| 125 | */ |
| 126 | vma = find_vma(mm, addr); |
| 127 | if (likely(!vma || addr+len <= vma->vm_start)) { |
| 128 | /* remember the address as a hint for next time */ |
| 129 | return (mm->free_area_cache = addr); |
| 130 | } |
| 131 | |
| 132 | /* remember the largest hole we saw so far */ |
| 133 | if (addr + mm->cached_hole_size < vma->vm_start) |
| 134 | mm->cached_hole_size = vma->vm_start - addr; |
| 135 | |
| 136 | /* try just below the current vma->vm_start */ |
| 137 | addr = (vma->vm_start-len) & HPAGE_MASK; |
| 138 | } while (likely(len < vma->vm_start)); |
| 139 | |
| 140 | bottomup: |
| 141 | /* |
| 142 | * A failed mmap() very likely causes application failure, |
| 143 | * so fall back to the bottom-up function here. This scenario |
| 144 | * can happen with large stack limits and large mmap() |
| 145 | * allocations. |
| 146 | */ |
| 147 | mm->cached_hole_size = ~0UL; |
| 148 | mm->free_area_cache = TASK_UNMAPPED_BASE; |
| 149 | addr = arch_get_unmapped_area(filp, addr0, len, pgoff, flags); |
| 150 | /* |
| 151 | * Restore the topdown base: |
| 152 | */ |
| 153 | mm->free_area_cache = mm->mmap_base; |
| 154 | mm->cached_hole_size = ~0UL; |
| 155 | |
| 156 | return addr; |
| 157 | } |
| 158 | |
| 159 | unsigned long |
| 160 | hugetlb_get_unmapped_area(struct file *file, unsigned long addr, |
| 161 | unsigned long len, unsigned long pgoff, unsigned long flags) |
| 162 | { |
| 163 | struct mm_struct *mm = current->mm; |
| 164 | struct vm_area_struct *vma; |
| 165 | unsigned long task_size = TASK_SIZE; |
| 166 | |
| 167 | if (test_thread_flag(TIF_32BIT)) |
| 168 | task_size = STACK_TOP32; |
| 169 | |
| 170 | if (len & ~HPAGE_MASK) |
| 171 | return -EINVAL; |
| 172 | if (len > task_size) |
| 173 | return -ENOMEM; |
| 174 | |
Benjamin Herrenschmidt | ac35ee4 | 2007-05-06 14:50:10 -0700 | [diff] [blame] | 175 | if (flags & MAP_FIXED) { |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 176 | if (prepare_hugepage_range(file, addr, len)) |
Benjamin Herrenschmidt | ac35ee4 | 2007-05-06 14:50:10 -0700 | [diff] [blame] | 177 | return -EINVAL; |
| 178 | return addr; |
| 179 | } |
| 180 | |
David S. Miller | f6b83f0 | 2006-03-20 01:17:17 -0800 | [diff] [blame] | 181 | if (addr) { |
| 182 | addr = ALIGN(addr, HPAGE_SIZE); |
| 183 | vma = find_vma(mm, addr); |
| 184 | if (task_size - len >= addr && |
| 185 | (!vma || addr + len <= vma->vm_start)) |
| 186 | return addr; |
| 187 | } |
| 188 | if (mm->get_unmapped_area == arch_get_unmapped_area) |
| 189 | return hugetlb_get_unmapped_area_bottomup(file, addr, len, |
| 190 | pgoff, flags); |
| 191 | else |
| 192 | return hugetlb_get_unmapped_area_topdown(file, addr, len, |
| 193 | pgoff, flags); |
| 194 | } |
| 195 | |
Andi Kleen | a551643 | 2008-07-23 21:27:41 -0700 | [diff] [blame] | 196 | pte_t *huge_pte_alloc(struct mm_struct *mm, |
| 197 | unsigned long addr, unsigned long sz) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | { |
| 199 | pgd_t *pgd; |
| 200 | pud_t *pud; |
| 201 | pmd_t *pmd; |
| 202 | pte_t *pte = NULL; |
| 203 | |
David S. Miller | 9df1dab | 2006-03-31 00:36:25 -0800 | [diff] [blame] | 204 | /* We must align the address, because our caller will run |
| 205 | * set_huge_pte_at() on whatever we return, which writes out |
| 206 | * all of the sub-ptes for the hugepage range. So we have |
| 207 | * to give it the first such sub-pte. |
| 208 | */ |
| 209 | addr &= HPAGE_MASK; |
| 210 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | pgd = pgd_offset(mm, addr); |
David S. Miller | dcc1e8d | 2006-03-22 00:49:59 -0800 | [diff] [blame] | 212 | pud = pud_alloc(mm, pgd, addr); |
| 213 | if (pud) { |
| 214 | pmd = pmd_alloc(mm, pud, addr); |
| 215 | if (pmd) |
Andrea Arcangeli | 8ac1f83 | 2011-01-13 15:46:43 -0800 | [diff] [blame] | 216 | pte = pte_alloc_map(mm, NULL, pmd, addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | } |
| 218 | return pte; |
| 219 | } |
| 220 | |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 221 | pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | { |
| 223 | pgd_t *pgd; |
| 224 | pud_t *pud; |
| 225 | pmd_t *pmd; |
| 226 | pte_t *pte = NULL; |
| 227 | |
David S. Miller | f6b83f0 | 2006-03-20 01:17:17 -0800 | [diff] [blame] | 228 | addr &= HPAGE_MASK; |
| 229 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | pgd = pgd_offset(mm, addr); |
David S. Miller | f6b83f0 | 2006-03-20 01:17:17 -0800 | [diff] [blame] | 231 | if (!pgd_none(*pgd)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | pud = pud_offset(pgd, addr); |
David S. Miller | f6b83f0 | 2006-03-20 01:17:17 -0800 | [diff] [blame] | 233 | if (!pud_none(*pud)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | pmd = pmd_offset(pud, addr); |
David S. Miller | f6b83f0 | 2006-03-20 01:17:17 -0800 | [diff] [blame] | 235 | if (!pmd_none(*pmd)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | pte = pte_offset_map(pmd, addr); |
| 237 | } |
| 238 | } |
| 239 | return pte; |
| 240 | } |
| 241 | |
Chen, Kenneth W | 39dde65 | 2006-12-06 20:32:03 -0800 | [diff] [blame] | 242 | int huge_pmd_unshare(struct mm_struct *mm, unsigned long *addr, pte_t *ptep) |
| 243 | { |
| 244 | return 0; |
| 245 | } |
| 246 | |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 247 | void set_huge_pte_at(struct mm_struct *mm, unsigned long addr, |
| 248 | pte_t *ptep, pte_t entry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | { |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 250 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | |
David S. Miller | dcc1e8d | 2006-03-22 00:49:59 -0800 | [diff] [blame] | 252 | if (!pte_present(*ptep) && pte_present(entry)) |
| 253 | mm->context.huge_pte_count++; |
| 254 | |
David S. Miller | bb8236f | 2007-03-12 22:55:39 -0700 | [diff] [blame] | 255 | addr &= HPAGE_MASK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | for (i = 0; i < (1 << HUGETLB_PAGE_ORDER); i++) { |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 257 | set_pte_at(mm, addr, ptep, entry); |
| 258 | ptep++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | addr += PAGE_SIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | pte_val(entry) += PAGE_SIZE; |
| 261 | } |
| 262 | } |
| 263 | |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 264 | pte_t huge_ptep_get_and_clear(struct mm_struct *mm, unsigned long addr, |
| 265 | pte_t *ptep) |
| 266 | { |
| 267 | pte_t entry; |
| 268 | int i; |
| 269 | |
| 270 | entry = *ptep; |
David S. Miller | dcc1e8d | 2006-03-22 00:49:59 -0800 | [diff] [blame] | 271 | if (pte_present(entry)) |
| 272 | mm->context.huge_pte_count--; |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 273 | |
David S. Miller | bb8236f | 2007-03-12 22:55:39 -0700 | [diff] [blame] | 274 | addr &= HPAGE_MASK; |
| 275 | |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 276 | for (i = 0; i < (1 << HUGETLB_PAGE_ORDER); i++) { |
| 277 | pte_clear(mm, addr, ptep); |
| 278 | addr += PAGE_SIZE; |
| 279 | ptep++; |
| 280 | } |
| 281 | |
| 282 | return entry; |
| 283 | } |
| 284 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | struct page *follow_huge_addr(struct mm_struct *mm, |
| 286 | unsigned long address, int write) |
| 287 | { |
| 288 | return ERR_PTR(-EINVAL); |
| 289 | } |
| 290 | |
| 291 | int pmd_huge(pmd_t pmd) |
| 292 | { |
| 293 | return 0; |
| 294 | } |
| 295 | |
Andi Kleen | ceb8687 | 2008-07-23 21:27:50 -0700 | [diff] [blame] | 296 | int pud_huge(pud_t pud) |
| 297 | { |
| 298 | return 0; |
| 299 | } |
| 300 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | struct page *follow_huge_pmd(struct mm_struct *mm, unsigned long address, |
| 302 | pmd_t *pmd, int write) |
| 303 | { |
| 304 | return NULL; |
| 305 | } |
| 306 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | static void context_reload(void *__data) |
| 308 | { |
| 309 | struct mm_struct *mm = __data; |
| 310 | |
| 311 | if (mm == current->mm) |
| 312 | load_secondary_context(mm); |
| 313 | } |
| 314 | |
David Gibson | 63551ae | 2005-06-21 17:14:44 -0700 | [diff] [blame] | 315 | void hugetlb_prefault_arch_hook(struct mm_struct *mm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | { |
David S. Miller | dcc1e8d | 2006-03-22 00:49:59 -0800 | [diff] [blame] | 317 | struct tsb_config *tp = &mm->context.tsb_block[MM_TSB_HUGE]; |
| 318 | |
| 319 | if (likely(tp->tsb != NULL)) |
| 320 | return; |
| 321 | |
| 322 | tsb_grow(mm, MM_TSB_HUGE, 0); |
| 323 | tsb_context_switch(mm); |
| 324 | smp_tsb_sync(mm); |
| 325 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | /* On UltraSPARC-III+ and later, configure the second half of |
| 327 | * the Data-TLB for huge pages. |
| 328 | */ |
| 329 | if (tlb_type == cheetah_plus) { |
| 330 | unsigned long ctx; |
| 331 | |
| 332 | spin_lock(&ctx_alloc_lock); |
| 333 | ctx = mm->context.sparc64_ctx_val; |
| 334 | ctx &= ~CTX_PGSZ_MASK; |
| 335 | ctx |= CTX_PGSZ_BASE << CTX_PGSZ0_SHIFT; |
| 336 | ctx |= CTX_PGSZ_HUGE << CTX_PGSZ1_SHIFT; |
| 337 | |
| 338 | if (ctx != mm->context.sparc64_ctx_val) { |
| 339 | /* When changing the page size fields, we |
| 340 | * must perform a context flush so that no |
| 341 | * stale entries match. This flush must |
| 342 | * occur with the original context register |
| 343 | * settings. |
| 344 | */ |
| 345 | do_flush_tlb_mm(mm); |
| 346 | |
| 347 | /* Reload the context register of all processors |
| 348 | * also executing in this address space. |
| 349 | */ |
| 350 | mm->context.sparc64_ctx_val = ctx; |
Jens Axboe | 15c8b6c | 2008-05-09 09:39:44 +0200 | [diff] [blame] | 351 | on_each_cpu(context_reload, mm, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | } |
| 353 | spin_unlock(&ctx_alloc_lock); |
| 354 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | } |