- patches.fixes/patch-2.6.11-rc1: 2.6.11-rc1.
[linux-flexiantxendom0-3.2.10.git] / arch / ppc64 / mm / hugetlbpage.c
1 /*
2  * PPC64 (POWER4) Huge TLB Page Support for Kernel.
3  *
4  * Copyright (C) 2003 David Gibson, IBM Corporation.
5  *
6  * Based on the IA-32 version:
7  * Copyright (C) 2002, Rohit Seth <rohit.seth@intel.com>
8  */
9
10 #include <linux/init.h>
11 #include <linux/fs.h>
12 #include <linux/mm.h>
13 #include <linux/hugetlb.h>
14 #include <linux/pagemap.h>
15 #include <linux/smp_lock.h>
16 #include <linux/slab.h>
17 #include <linux/err.h>
18 #include <linux/sysctl.h>
19 #include <asm/mman.h>
20 #include <asm/pgalloc.h>
21 #include <asm/tlb.h>
22 #include <asm/tlbflush.h>
23 #include <asm/mmu_context.h>
24 #include <asm/machdep.h>
25 #include <asm/cputable.h>
26 #include <asm/tlb.h>
27
28 #include <linux/sysctl.h>
29
30 #define HUGEPGDIR_SHIFT         (HPAGE_SHIFT + PAGE_SHIFT - 3)
31 #define HUGEPGDIR_SIZE          (1UL << HUGEPGDIR_SHIFT)
32 #define HUGEPGDIR_MASK          (~(HUGEPGDIR_SIZE-1))
33
34 #define HUGEPTE_INDEX_SIZE      9
35 #define HUGEPGD_INDEX_SIZE      10
36
37 #define PTRS_PER_HUGEPTE        (1 << HUGEPTE_INDEX_SIZE)
38 #define PTRS_PER_HUGEPGD        (1 << HUGEPGD_INDEX_SIZE)
39
40 static inline int hugepgd_index(unsigned long addr)
41 {
42         return (addr & ~REGION_MASK) >> HUGEPGDIR_SHIFT;
43 }
44
45 static pgd_t *hugepgd_offset(struct mm_struct *mm, unsigned long addr)
46 {
47         int index;
48
49         if (! mm->context.huge_pgdir)
50                 return NULL;
51
52
53         index = hugepgd_index(addr);
54         BUG_ON(index >= PTRS_PER_HUGEPGD);
55         return mm->context.huge_pgdir + index;
56 }
57
58 static inline pte_t *hugepte_offset(pgd_t *dir, unsigned long addr)
59 {
60         int index;
61
62         if (pgd_none(*dir))
63                 return NULL;
64
65         index = (addr >> HPAGE_SHIFT) % PTRS_PER_HUGEPTE;
66         return (pte_t *)pgd_page(*dir) + index;
67 }
68
69 static pgd_t *hugepgd_alloc(struct mm_struct *mm, unsigned long addr)
70 {
71         BUG_ON(! in_hugepage_area(mm->context, addr));
72
73         if (! mm->context.huge_pgdir) {
74                 pgd_t *new;
75                 spin_unlock(&mm->page_table_lock);
76                 /* Don't use pgd_alloc(), because we want __GFP_REPEAT */
77                 new = kmem_cache_alloc(zero_cache, GFP_KERNEL | __GFP_REPEAT);
78                 BUG_ON(memcmp(new, empty_zero_page, PAGE_SIZE));
79                 spin_lock(&mm->page_table_lock);
80
81                 /*
82                  * Because we dropped the lock, we should re-check the
83                  * entry, as somebody else could have populated it..
84                  */
85                 if (mm->context.huge_pgdir)
86                         pgd_free(new);
87                 else
88                         mm->context.huge_pgdir = new;
89         }
90         return hugepgd_offset(mm, addr);
91 }
92
93 static pte_t *hugepte_alloc(struct mm_struct *mm, pgd_t *dir,
94                             unsigned long addr)
95 {
96         if (! pgd_present(*dir)) {
97                 pte_t *new;
98
99                 spin_unlock(&mm->page_table_lock);
100                 new = kmem_cache_alloc(zero_cache, GFP_KERNEL | __GFP_REPEAT);
101                 BUG_ON(memcmp(new, empty_zero_page, PAGE_SIZE));
102                 spin_lock(&mm->page_table_lock);
103                 /*
104                  * Because we dropped the lock, we should re-check the
105                  * entry, as somebody else could have populated it..
106                  */
107                 if (pgd_present(*dir)) {
108                         if (new)
109                                 kmem_cache_free(zero_cache, new);
110                 } else {
111                         struct page *ptepage;
112
113                         if (! new)
114                                 return NULL;
115                         ptepage = virt_to_page(new);
116                         ptepage->mapping = (void *) mm;
117                         ptepage->index = addr & HUGEPGDIR_MASK;
118                         pgd_populate(mm, dir, new);
119                 }
120         }
121
122         return hugepte_offset(dir, addr);
123 }
124
125 static pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr)
126 {
127         pgd_t *pgd;
128
129         BUG_ON(! in_hugepage_area(mm->context, addr));
130
131         pgd = hugepgd_offset(mm, addr);
132         if (! pgd)
133                 return NULL;
134
135         return hugepte_offset(pgd, addr);
136 }
137
138 static pte_t *huge_pte_alloc(struct mm_struct *mm, unsigned long addr)
139 {
140         pgd_t *pgd;
141
142         BUG_ON(! in_hugepage_area(mm->context, addr));
143
144         pgd = hugepgd_alloc(mm, addr);
145         if (! pgd)
146                 return NULL;
147
148         return hugepte_alloc(mm, pgd, addr);
149 }
150
151 static void set_huge_pte(struct mm_struct *mm, struct vm_area_struct *vma,
152                          struct page *page, pte_t *ptep, int write_access)
153 {
154         pte_t entry;
155
156         mm->rss += (HPAGE_SIZE / PAGE_SIZE);
157         if (write_access) {
158                 entry =
159                     pte_mkwrite(pte_mkdirty(mk_pte(page, vma->vm_page_prot)));
160         } else {
161                 entry = pte_wrprotect(mk_pte(page, vma->vm_page_prot));
162         }
163         entry = pte_mkyoung(entry);
164         entry = pte_mkhuge(entry);
165
166         set_pte(ptep, entry);
167 }
168
169 /*
170  * This function checks for proper alignment of input addr and len parameters.
171  */
172 int is_aligned_hugepage_range(unsigned long addr, unsigned long len)
173 {
174         if (len & ~HPAGE_MASK)
175                 return -EINVAL;
176         if (addr & ~HPAGE_MASK)
177                 return -EINVAL;
178         if (! (within_hugepage_low_range(addr, len)
179                || within_hugepage_high_range(addr, len)) )
180                 return -EINVAL;
181         return 0;
182 }
183
184 static void flush_segments(void *parm)
185 {
186         u16 segs = (unsigned long) parm;
187         unsigned long i;
188
189         asm volatile("isync" : : : "memory");
190
191         for (i = 0; i < 16; i++) {
192                 if (! (segs & (1U << i)))
193                         continue;
194                 asm volatile("slbie %0" : : "r" (i << SID_SHIFT));
195         }
196
197         asm volatile("isync" : : : "memory");
198 }
199
200 static int prepare_low_seg_for_htlb(struct mm_struct *mm, unsigned long seg)
201 {
202         unsigned long start = seg << SID_SHIFT;
203         unsigned long end = (seg+1) << SID_SHIFT;
204         struct vm_area_struct *vma;
205         unsigned long addr;
206         struct mmu_gather *tlb;
207
208         BUG_ON(seg >= 16);
209
210         /* Check no VMAs are in the region */
211         vma = find_vma(mm, start);
212         if (vma && (vma->vm_start < end))
213                 return -EBUSY;
214
215         /* Clean up any leftover PTE pages in the region */
216         spin_lock(&mm->page_table_lock);
217         tlb = tlb_gather_mmu(mm, 0);
218         for (addr = start; addr < end; addr += PMD_SIZE) {
219                 pgd_t *pgd = pgd_offset(mm, addr);
220                 pmd_t *pmd;
221                 struct page *page;
222                 pte_t *pte;
223                 int i;
224
225                 if (pgd_none(*pgd))
226                         continue;
227                 pmd = pmd_offset(pgd, addr);
228                 if (!pmd || pmd_none(*pmd))
229                         continue;
230                 if (pmd_bad(*pmd)) {
231                         pmd_ERROR(*pmd);
232                         pmd_clear(pmd);
233                         continue;
234                 }
235                 pte = (pte_t *)pmd_page_kernel(*pmd);
236                 /* No VMAs, so there should be no PTEs, check just in case. */
237                 for (i = 0; i < PTRS_PER_PTE; i++) {
238                         BUG_ON(!pte_none(*pte));
239                         pte++;
240                 }
241                 page = pmd_page(*pmd);
242                 pmd_clear(pmd);
243                 mm->nr_ptes--;
244                 dec_page_state(nr_page_table_pages);
245                 pte_free_tlb(tlb, page);
246         }
247         tlb_finish_mmu(tlb, start, end);
248         spin_unlock(&mm->page_table_lock);
249
250         return 0;
251 }
252
253 static int open_low_hpage_segs(struct mm_struct *mm, u16 newsegs)
254 {
255         unsigned long i;
256
257         newsegs &= ~(mm->context.htlb_segs);
258         if (! newsegs)
259                 return 0; /* The segments we want are already open */
260
261         for (i = 0; i < 16; i++)
262                 if ((1 << i) & newsegs)
263                         if (prepare_low_seg_for_htlb(mm, i) != 0)
264                                 return -EBUSY;
265
266         mm->context.htlb_segs |= newsegs;
267         /* the context change must make it to memory before the flush,
268          * so that further SLB misses do the right thing. */
269         mb();
270         on_each_cpu(flush_segments, (void *)(unsigned long)newsegs, 0, 1);
271
272         return 0;
273 }
274
275 int prepare_hugepage_range(unsigned long addr, unsigned long len)
276 {
277         if (within_hugepage_high_range(addr, len))
278                 return 0;
279         else if ((addr < 0x100000000UL) && ((addr+len) < 0x100000000UL)) {
280                 int err;
281                 /* Yes, we need both tests, in case addr+len overflows
282                  * 64-bit arithmetic */
283                 err = open_low_hpage_segs(current->mm,
284                                           LOW_ESID_MASK(addr, len));
285                 if (err)
286                         printk(KERN_DEBUG "prepare_hugepage_range(%lx, %lx)"
287                                " failed (segs: 0x%04hx)\n", addr, len,
288                                LOW_ESID_MASK(addr, len));
289                 return err;
290         }
291
292         return -EINVAL;
293 }
294
295 int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src,
296                         struct vm_area_struct *vma)
297 {
298         pte_t *src_pte, *dst_pte, entry;
299         struct page *ptepage;
300         unsigned long addr = vma->vm_start;
301         unsigned long end = vma->vm_end;
302         int err = -ENOMEM;
303
304         while (addr < end) {
305                 dst_pte = huge_pte_alloc(dst, addr);
306                 if (!dst_pte)
307                         goto out;
308
309                 src_pte = huge_pte_offset(src, addr);
310                 entry = *src_pte;
311                 
312                 ptepage = pte_page(entry);
313                 get_page(ptepage);
314                 dst->rss += (HPAGE_SIZE / PAGE_SIZE);
315                 set_pte(dst_pte, entry);
316
317                 addr += HPAGE_SIZE;
318         }
319
320         err = 0;
321  out:
322         return err;
323 }
324
325 int
326 follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma,
327                     struct page **pages, struct vm_area_struct **vmas,
328                     unsigned long *position, int *length, int i)
329 {
330         unsigned long vpfn, vaddr = *position;
331         int remainder = *length;
332
333         WARN_ON(!is_vm_hugetlb_page(vma));
334
335         vpfn = vaddr/PAGE_SIZE;
336         while (vaddr < vma->vm_end && remainder) {
337                 if (pages) {
338                         pte_t *pte;
339                         struct page *page;
340
341                         pte = huge_pte_offset(mm, vaddr);
342
343                         /* hugetlb should be locked, and hence, prefaulted */
344                         WARN_ON(!pte || pte_none(*pte));
345
346                         page = &pte_page(*pte)[vpfn % (HPAGE_SIZE/PAGE_SIZE)];
347
348                         WARN_ON(!PageCompound(page));
349
350                         get_page(page);
351                         pages[i] = page;
352                 }
353
354                 if (vmas)
355                         vmas[i] = vma;
356
357                 vaddr += PAGE_SIZE;
358                 ++vpfn;
359                 --remainder;
360                 ++i;
361         }
362
363         *length = remainder;
364         *position = vaddr;
365
366         return i;
367 }
368
369 struct page *
370 follow_huge_addr(struct mm_struct *mm, unsigned long address, int write)
371 {
372         pte_t *ptep;
373         struct page *page;
374
375         if (! in_hugepage_area(mm->context, address))
376                 return ERR_PTR(-EINVAL);
377
378         ptep = huge_pte_offset(mm, address);
379         page = pte_page(*ptep);
380         if (page)
381                 page += (address % HPAGE_SIZE) / PAGE_SIZE;
382
383         return page;
384 }
385
386 int pmd_huge(pmd_t pmd)
387 {
388         return 0;
389 }
390
391 struct page *
392 follow_huge_pmd(struct mm_struct *mm, unsigned long address,
393                 pmd_t *pmd, int write)
394 {
395         BUG();
396         return NULL;
397 }
398
399 void unmap_hugepage_range(struct vm_area_struct *vma,
400                           unsigned long start, unsigned long end)
401 {
402         struct mm_struct *mm = vma->vm_mm;
403         unsigned long addr;
404         pte_t *ptep;
405         struct page *page;
406
407         WARN_ON(!is_vm_hugetlb_page(vma));
408         BUG_ON((start % HPAGE_SIZE) != 0);
409         BUG_ON((end % HPAGE_SIZE) != 0);
410
411         for (addr = start; addr < end; addr += HPAGE_SIZE) {
412                 pte_t pte;
413
414                 ptep = huge_pte_offset(mm, addr);
415                 if (!ptep || pte_none(*ptep))
416                         continue;
417
418                 pte = *ptep;
419                 page = pte_page(pte);
420                 pte_clear(ptep);
421
422                 put_page(page);
423         }
424         mm->rss -= (end - start) >> PAGE_SHIFT;
425         flush_tlb_pending();
426 }
427
428 void hugetlb_free_pgtables(struct mmu_gather *tlb, struct vm_area_struct *prev,
429                            unsigned long start, unsigned long end)
430 {
431         /* Because the huge pgtables are only 2 level, they can take
432          * at most around 4M, much less than one hugepage which the
433          * process is presumably entitled to use.  So we don't bother
434          * freeing up the pagetables on unmap, and wait until
435          * destroy_context() to clean up the lot. */
436 }
437
438 int hugetlb_prefault(struct address_space *mapping, struct vm_area_struct *vma)
439 {
440         struct mm_struct *mm = current->mm;
441         unsigned long addr;
442         int ret = 0;
443
444         WARN_ON(!is_vm_hugetlb_page(vma));
445         BUG_ON((vma->vm_start % HPAGE_SIZE) != 0);
446         BUG_ON((vma->vm_end % HPAGE_SIZE) != 0);
447
448         spin_lock(&mm->page_table_lock);
449         for (addr = vma->vm_start; addr < vma->vm_end; addr += HPAGE_SIZE) {
450                 unsigned long idx;
451                 pte_t *pte = huge_pte_alloc(mm, addr);
452                 struct page *page;
453
454                 if (!pte) {
455                         ret = -ENOMEM;
456                         goto out;
457                 }
458                 if (! pte_none(*pte))
459                         continue;
460
461                 idx = ((addr - vma->vm_start) >> HPAGE_SHIFT)
462                         + (vma->vm_pgoff >> (HPAGE_SHIFT - PAGE_SHIFT));
463                 page = find_get_page(mapping, idx);
464                 if (!page) {
465                         /* charge the fs quota first */
466                         if (hugetlb_get_quota(mapping)) {
467                                 ret = -ENOMEM;
468                                 goto out;
469                         }
470                         page = alloc_huge_page();
471                         if (!page) {
472                                 hugetlb_put_quota(mapping);
473                                 ret = -ENOMEM;
474                                 goto out;
475                         }
476                         ret = add_to_page_cache(page, mapping, idx, GFP_ATOMIC);
477                         if (! ret) {
478                                 unlock_page(page);
479                         } else {
480                                 hugetlb_put_quota(mapping);
481                                 free_huge_page(page);
482                                 goto out;
483                         }
484                 }
485                 set_huge_pte(mm, vma, page, pte, vma->vm_flags & VM_WRITE);
486         }
487 out:
488         spin_unlock(&mm->page_table_lock);
489         return ret;
490 }
491
492 /* Because we have an exclusive hugepage region which lies within the
493  * normal user address space, we have to take special measures to make
494  * non-huge mmap()s evade the hugepage reserved regions. */
495 unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr,
496                                      unsigned long len, unsigned long pgoff,
497                                      unsigned long flags)
498 {
499         struct mm_struct *mm = current->mm;
500         struct vm_area_struct *vma;
501         unsigned long start_addr;
502
503         if (len > TASK_SIZE)
504                 return -ENOMEM;
505
506         if (addr) {
507                 addr = PAGE_ALIGN(addr);
508                 vma = find_vma(mm, addr);
509                 if (((TASK_SIZE - len) >= addr)
510                     && (!vma || (addr+len) <= vma->vm_start)
511                     && !is_hugepage_only_range(addr,len))
512                         return addr;
513         }
514         start_addr = addr = mm->free_area_cache;
515
516 full_search:
517         vma = find_vma(mm, addr);
518         while (TASK_SIZE - len >= addr) {
519                 BUG_ON(vma && (addr >= vma->vm_end));
520
521                 if (touches_hugepage_low_range(addr, len)) {
522                         addr = ALIGN(addr+1, 1<<SID_SHIFT);
523                         vma = find_vma(mm, addr);
524                         continue;
525                 }
526                 if (touches_hugepage_high_range(addr, len)) {
527                         addr = TASK_HPAGE_END;
528                         vma = find_vma(mm, addr);
529                         continue;
530                 }
531                 if (!vma || addr + len <= vma->vm_start) {
532                         /*
533                          * Remember the place where we stopped the search:
534                          */
535                         mm->free_area_cache = addr + len;
536                         return addr;
537                 }
538                 addr = vma->vm_end;
539                 vma = vma->vm_next;
540         }
541
542         /* Make sure we didn't miss any holes */
543         if (start_addr != TASK_UNMAPPED_BASE) {
544                 start_addr = addr = TASK_UNMAPPED_BASE;
545                 goto full_search;
546         }
547         return -ENOMEM;
548 }
549
550 /*
551  * This mmap-allocator allocates new areas top-down from below the
552  * stack's low limit (the base):
553  *
554  * Because we have an exclusive hugepage region which lies within the
555  * normal user address space, we have to take special measures to make
556  * non-huge mmap()s evade the hugepage reserved regions.
557  */
558 unsigned long
559 arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
560                           const unsigned long len, const unsigned long pgoff,
561                           const unsigned long flags)
562 {
563         struct vm_area_struct *vma, *prev_vma;
564         struct mm_struct *mm = current->mm;
565         unsigned long base = mm->mmap_base, addr = addr0;
566         int first_time = 1;
567
568         /* requested length too big for entire address space */
569         if (len > TASK_SIZE)
570                 return -ENOMEM;
571
572         /* dont allow allocations above current base */
573         if (mm->free_area_cache > base)
574                 mm->free_area_cache = base;
575
576         /* requesting a specific address */
577         if (addr) {
578                 addr = PAGE_ALIGN(addr);
579                 vma = find_vma(mm, addr);
580                 if (TASK_SIZE - len >= addr &&
581                                 (!vma || addr + len <= vma->vm_start)
582                                 && !is_hugepage_only_range(addr,len))
583                         return addr;
584         }
585
586 try_again:
587         /* make sure it can fit in the remaining address space */
588         if (mm->free_area_cache < len)
589                 goto fail;
590
591         /* either no address requested or cant fit in requested address hole */
592         addr = (mm->free_area_cache - len) & PAGE_MASK;
593         do {
594 hugepage_recheck:
595                 if (touches_hugepage_low_range(addr, len)) {
596                         addr = (addr & ((~0) << SID_SHIFT)) - len;
597                         goto hugepage_recheck;
598                 } else if (touches_hugepage_high_range(addr, len)) {
599                         addr = TASK_HPAGE_BASE - len;
600                 }
601
602                 /*
603                  * Lookup failure means no vma is above this address,
604                  * i.e. return with success:
605                  */
606                 if (!(vma = find_vma_prev(mm, addr, &prev_vma)))
607                         return addr;
608
609                 /*
610                  * new region fits between prev_vma->vm_end and
611                  * vma->vm_start, use it:
612                  */
613                 if (addr+len <= vma->vm_start &&
614                                 (!prev_vma || (addr >= prev_vma->vm_end)))
615                         /* remember the address as a hint for next time */
616                         return (mm->free_area_cache = addr);
617                 else
618                         /* pull free_area_cache down to the first hole */
619                         if (mm->free_area_cache == vma->vm_end)
620                                 mm->free_area_cache = vma->vm_start;
621
622                 /* try just below the current vma->vm_start */
623                 addr = vma->vm_start-len;
624         } while (len <= vma->vm_start);
625
626 fail:
627         /*
628          * if hint left us with no space for the requested
629          * mapping then try again:
630          */
631         if (first_time) {
632                 mm->free_area_cache = base;
633                 first_time = 0;
634                 goto try_again;
635         }
636         /*
637          * A failed mmap() very likely causes application failure,
638          * so fall back to the bottom-up function here. This scenario
639          * can happen with large stack limits and large mmap()
640          * allocations.
641          */
642         mm->free_area_cache = TASK_UNMAPPED_BASE;
643         addr = arch_get_unmapped_area(filp, addr0, len, pgoff, flags);
644         /*
645          * Restore the topdown base:
646          */
647         mm->free_area_cache = base;
648
649         return addr;
650 }
651
652 static unsigned long htlb_get_low_area(unsigned long len, u16 segmask)
653 {
654         unsigned long addr = 0;
655         struct vm_area_struct *vma;
656
657         vma = find_vma(current->mm, addr);
658         while (addr + len <= 0x100000000UL) {
659                 BUG_ON(vma && (addr >= vma->vm_end)); /* invariant */
660
661                 if (! __within_hugepage_low_range(addr, len, segmask)) {
662                         addr = ALIGN(addr+1, 1<<SID_SHIFT);
663                         vma = find_vma(current->mm, addr);
664                         continue;
665                 }
666
667                 if (!vma || (addr + len) <= vma->vm_start)
668                         return addr;
669                 addr = ALIGN(vma->vm_end, HPAGE_SIZE);
670                 /* Depending on segmask this might not be a confirmed
671                  * hugepage region, so the ALIGN could have skipped
672                  * some VMAs */
673                 vma = find_vma(current->mm, addr);
674         }
675
676         return -ENOMEM;
677 }
678
679 static unsigned long htlb_get_high_area(unsigned long len)
680 {
681         unsigned long addr = TASK_HPAGE_BASE;
682         struct vm_area_struct *vma;
683
684         vma = find_vma(current->mm, addr);
685         for (vma = find_vma(current->mm, addr);
686              addr + len <= TASK_HPAGE_END;
687              vma = vma->vm_next) {
688                 BUG_ON(vma && (addr >= vma->vm_end)); /* invariant */
689                 BUG_ON(! within_hugepage_high_range(addr, len));
690
691                 if (!vma || (addr + len) <= vma->vm_start)
692                         return addr;
693                 addr = ALIGN(vma->vm_end, HPAGE_SIZE);
694                 /* Because we're in a hugepage region, this alignment
695                  * should not skip us over any VMAs */
696         }
697
698         return -ENOMEM;
699 }
700
701 unsigned long hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
702                                         unsigned long len, unsigned long pgoff,
703                                         unsigned long flags)
704 {
705         if (len & ~HPAGE_MASK)
706                 return -EINVAL;
707
708         if (!(cur_cpu_spec->cpu_features & CPU_FTR_16M_PAGE))
709                 return -EINVAL;
710
711         if (test_thread_flag(TIF_32BIT)) {
712                 int lastshift = 0;
713                 u16 segmask, cursegs = current->mm->context.htlb_segs;
714
715                 /* First see if we can do the mapping in the existing
716                  * low hpage segments */
717                 addr = htlb_get_low_area(len, cursegs);
718                 if (addr != -ENOMEM)
719                         return addr;
720
721                 for (segmask = LOW_ESID_MASK(0x100000000UL-len, len);
722                      ! lastshift; segmask >>=1) {
723                         if (segmask & 1)
724                                 lastshift = 1;
725
726                         addr = htlb_get_low_area(len, cursegs | segmask);
727                         if ((addr != -ENOMEM)
728                             && open_low_hpage_segs(current->mm, segmask) == 0)
729                                 return addr;
730                 }
731                 printk(KERN_DEBUG "hugetlb_get_unmapped_area() unable to open"
732                        " enough segments\n");
733                 return -ENOMEM;
734         } else {
735                 return htlb_get_high_area(len);
736         }
737 }
738
739 void hugetlb_mm_free_pgd(struct mm_struct *mm)
740 {
741         int i;
742         pgd_t *pgdir;
743
744         spin_lock(&mm->page_table_lock);
745
746         pgdir = mm->context.huge_pgdir;
747         if (! pgdir)
748                 goto out;
749
750         mm->context.huge_pgdir = NULL;
751
752         /* cleanup any hugepte pages leftover */
753         for (i = 0; i < PTRS_PER_HUGEPGD; i++) {
754                 pgd_t *pgd = pgdir + i;
755
756                 if (! pgd_none(*pgd)) {
757                         pte_t *pte = (pte_t *)pgd_page(*pgd);
758                         struct page *ptepage = virt_to_page(pte);
759
760                         ptepage->mapping = NULL;
761
762                         BUG_ON(memcmp(pte, empty_zero_page, PAGE_SIZE));
763                         kmem_cache_free(zero_cache, pte);
764                 }
765                 pgd_clear(pgd);
766         }
767
768         BUG_ON(memcmp(pgdir, empty_zero_page, PAGE_SIZE));
769         kmem_cache_free(zero_cache, pgdir);
770
771  out:
772         spin_unlock(&mm->page_table_lock);
773 }
774
775 int hash_huge_page(struct mm_struct *mm, unsigned long access,
776                    unsigned long ea, unsigned long vsid, int local)
777 {
778         pte_t *ptep;
779         unsigned long va, vpn;
780         int is_write;
781         pte_t old_pte, new_pte;
782         unsigned long hpteflags, prpn;
783         long slot;
784         int err = 1;
785
786         spin_lock(&mm->page_table_lock);
787
788         ptep = huge_pte_offset(mm, ea);
789
790         /* Search the Linux page table for a match with va */
791         va = (vsid << 28) | (ea & 0x0fffffff);
792         vpn = va >> HPAGE_SHIFT;
793
794         /*
795          * If no pte found or not present, send the problem up to
796          * do_page_fault
797          */
798         if (unlikely(!ptep || pte_none(*ptep)))
799                 goto out;
800
801 /*      BUG_ON(pte_bad(*ptep)); */
802
803         /* 
804          * Check the user's access rights to the page.  If access should be
805          * prevented then send the problem up to do_page_fault.
806          */
807         is_write = access & _PAGE_RW;
808         if (unlikely(is_write && !(pte_val(*ptep) & _PAGE_RW)))
809                 goto out;
810         /*
811          * At this point, we have a pte (old_pte) which can be used to build
812          * or update an HPTE. There are 2 cases:
813          *
814          * 1. There is a valid (present) pte with no associated HPTE (this is 
815          *      the most common case)
816          * 2. There is a valid (present) pte with an associated HPTE. The
817          *      current values of the pp bits in the HPTE prevent access
818          *      because we are doing software DIRTY bit management and the
819          *      page is currently not DIRTY. 
820          */
821
822
823         old_pte = *ptep;
824         new_pte = old_pte;
825
826         hpteflags = 0x2 | (! (pte_val(new_pte) & _PAGE_RW));
827
828         /* Check if pte already has an hpte (case 2) */
829         if (unlikely(pte_val(old_pte) & _PAGE_HASHPTE)) {
830                 /* There MIGHT be an HPTE for this pte */
831                 unsigned long hash, slot;
832
833                 hash = hpt_hash(vpn, 1);
834                 if (pte_val(old_pte) & _PAGE_SECONDARY)
835                         hash = ~hash;
836                 slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
837                 slot += (pte_val(old_pte) & _PAGE_GROUP_IX) >> 12;
838
839                 if (ppc_md.hpte_updatepp(slot, hpteflags, va, 1, local) == -1)
840                         pte_val(old_pte) &= ~_PAGE_HPTEFLAGS;
841         }
842
843         if (likely(!(pte_val(old_pte) & _PAGE_HASHPTE))) {
844                 unsigned long hash = hpt_hash(vpn, 1);
845                 unsigned long hpte_group;
846
847                 prpn = pte_pfn(old_pte);
848
849 repeat:
850                 hpte_group = ((hash & htab_hash_mask) *
851                               HPTES_PER_GROUP) & ~0x7UL;
852
853                 /* Update the linux pte with the HPTE slot */
854                 pte_val(new_pte) &= ~_PAGE_HPTEFLAGS;
855                 pte_val(new_pte) |= _PAGE_HASHPTE;
856
857                 /* Add in WIMG bits */
858                 /* XXX We should store these in the pte */
859                 hpteflags |= _PAGE_COHERENT;
860
861                 slot = ppc_md.hpte_insert(hpte_group, va, prpn, 0,
862                                           hpteflags, 0, 1);
863
864                 /* Primary is full, try the secondary */
865                 if (unlikely(slot == -1)) {
866                         pte_val(new_pte) |= _PAGE_SECONDARY;
867                         hpte_group = ((~hash & htab_hash_mask) *
868                                       HPTES_PER_GROUP) & ~0x7UL; 
869                         slot = ppc_md.hpte_insert(hpte_group, va, prpn,
870                                                   1, hpteflags, 0, 1);
871                         if (slot == -1) {
872                                 if (mftb() & 0x1)
873                                         hpte_group = ((hash & htab_hash_mask) * HPTES_PER_GROUP) & ~0x7UL;
874
875                                 ppc_md.hpte_remove(hpte_group);
876                                 goto repeat;
877                         }
878                 }
879
880                 if (unlikely(slot == -2))
881                         panic("hash_huge_page: pte_insert failed\n");
882
883                 pte_val(new_pte) |= (slot<<12) & _PAGE_GROUP_IX;
884
885                 /* 
886                  * No need to use ldarx/stdcx here because all who
887                  * might be updating the pte will hold the
888                  * page_table_lock
889                  */
890                 *ptep = new_pte;
891         }
892
893         err = 0;
894
895  out:
896         spin_unlock(&mm->page_table_lock);
897
898         return err;
899 }