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