vserver 2.0-rc4
[linux-2.6.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         vx_rsspages_add(mm, 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
268         /* update the paca copy of the context struct */
269         get_paca()->context = mm->context;
270
271         /* the context change must make it to memory before the flush,
272          * so that further SLB misses do the right thing. */
273         mb();
274         on_each_cpu(flush_segments, (void *)(unsigned long)newsegs, 0, 1);
275
276         return 0;
277 }
278
279 int prepare_hugepage_range(unsigned long addr, unsigned long len)
280 {
281         if (within_hugepage_high_range(addr, len))
282                 return 0;
283         else if ((addr < 0x100000000UL) && ((addr+len) < 0x100000000UL)) {
284                 int err;
285                 /* Yes, we need both tests, in case addr+len overflows
286                  * 64-bit arithmetic */
287                 err = open_low_hpage_segs(current->mm,
288                                           LOW_ESID_MASK(addr, len));
289                 if (err)
290                         printk(KERN_DEBUG "prepare_hugepage_range(%lx, %lx)"
291                                " failed (segs: 0x%04hx)\n", addr, len,
292                                LOW_ESID_MASK(addr, len));
293                 return err;
294         }
295
296         return -EINVAL;
297 }
298
299 int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src,
300                         struct vm_area_struct *vma)
301 {
302         pte_t *src_pte, *dst_pte, entry;
303         struct page *ptepage;
304         unsigned long addr = vma->vm_start;
305         unsigned long end = vma->vm_end;
306         int err = -ENOMEM;
307
308         while (addr < end) {
309                 dst_pte = huge_pte_alloc(dst, addr);
310                 if (!dst_pte)
311                         goto out;
312
313                 src_pte = huge_pte_offset(src, addr);
314                 entry = *src_pte;
315                 
316                 ptepage = pte_page(entry);
317                 get_page(ptepage);
318                 vx_rsspages_add(dst, HPAGE_SIZE / PAGE_SIZE);
319                 set_pte(dst_pte, entry);
320
321                 addr += HPAGE_SIZE;
322         }
323
324         err = 0;
325  out:
326         return err;
327 }
328
329 int
330 follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma,
331                     struct page **pages, struct vm_area_struct **vmas,
332                     unsigned long *position, int *length, int i)
333 {
334         unsigned long vpfn, vaddr = *position;
335         int remainder = *length;
336
337         WARN_ON(!is_vm_hugetlb_page(vma));
338
339         vpfn = vaddr/PAGE_SIZE;
340         while (vaddr < vma->vm_end && remainder) {
341                 if (pages) {
342                         pte_t *pte;
343                         struct page *page;
344
345                         pte = huge_pte_offset(mm, vaddr);
346
347                         /* hugetlb should be locked, and hence, prefaulted */
348                         WARN_ON(!pte || pte_none(*pte));
349
350                         page = &pte_page(*pte)[vpfn % (HPAGE_SIZE/PAGE_SIZE)];
351
352                         WARN_ON(!PageCompound(page));
353
354                         get_page(page);
355                         pages[i] = page;
356                 }
357
358                 if (vmas)
359                         vmas[i] = vma;
360
361                 vaddr += PAGE_SIZE;
362                 ++vpfn;
363                 --remainder;
364                 ++i;
365         }
366
367         *length = remainder;
368         *position = vaddr;
369
370         return i;
371 }
372
373 struct page *
374 follow_huge_addr(struct mm_struct *mm, unsigned long address, int write)
375 {
376         pte_t *ptep;
377         struct page *page;
378
379         if (! in_hugepage_area(mm->context, address))
380                 return ERR_PTR(-EINVAL);
381
382         ptep = huge_pte_offset(mm, address);
383         page = pte_page(*ptep);
384         if (page)
385                 page += (address % HPAGE_SIZE) / PAGE_SIZE;
386
387         return page;
388 }
389
390 int pmd_huge(pmd_t pmd)
391 {
392         return 0;
393 }
394
395 struct page *
396 follow_huge_pmd(struct mm_struct *mm, unsigned long address,
397                 pmd_t *pmd, int write)
398 {
399         BUG();
400         return NULL;
401 }
402
403 void unmap_hugepage_range(struct vm_area_struct *vma,
404                           unsigned long start, unsigned long end)
405 {
406         struct mm_struct *mm = vma->vm_mm;
407         unsigned long addr;
408         pte_t *ptep;
409         struct page *page;
410
411         WARN_ON(!is_vm_hugetlb_page(vma));
412         BUG_ON((start % HPAGE_SIZE) != 0);
413         BUG_ON((end % HPAGE_SIZE) != 0);
414
415         for (addr = start; addr < end; addr += HPAGE_SIZE) {
416                 pte_t pte;
417
418                 ptep = huge_pte_offset(mm, addr);
419                 if (!ptep || pte_none(*ptep))
420                         continue;
421
422                 pte = *ptep;
423                 page = pte_page(pte);
424                 pte_clear(ptep);
425
426                 put_page(page);
427         }
428         vx_rsspages_sub(mm, (end - start) >> PAGE_SHIFT);
429         flush_tlb_pending();
430 }
431
432 void hugetlb_free_pgtables(struct mmu_gather *tlb, struct vm_area_struct *prev,
433                            unsigned long start, unsigned long end)
434 {
435         /* Because the huge pgtables are only 2 level, they can take
436          * at most around 4M, much less than one hugepage which the
437          * process is presumably entitled to use.  So we don't bother
438          * freeing up the pagetables on unmap, and wait until
439          * destroy_context() to clean up the lot. */
440 }
441
442 int hugetlb_prefault(struct address_space *mapping, struct vm_area_struct *vma)
443 {
444         struct mm_struct *mm = current->mm;
445         unsigned long addr;
446         int ret = 0;
447
448         WARN_ON(!is_vm_hugetlb_page(vma));
449         BUG_ON((vma->vm_start % HPAGE_SIZE) != 0);
450         BUG_ON((vma->vm_end % HPAGE_SIZE) != 0);
451
452         spin_lock(&mm->page_table_lock);
453         for (addr = vma->vm_start; addr < vma->vm_end; addr += HPAGE_SIZE) {
454                 unsigned long idx;
455                 pte_t *pte = huge_pte_alloc(mm, addr);
456                 struct page *page;
457
458                 if (!pte) {
459                         ret = -ENOMEM;
460                         goto out;
461                 }
462                 if (! pte_none(*pte))
463                         continue;
464
465                 idx = ((addr - vma->vm_start) >> HPAGE_SHIFT)
466                         + (vma->vm_pgoff >> (HPAGE_SHIFT - PAGE_SHIFT));
467                 page = find_get_page(mapping, idx);
468                 if (!page) {
469                         /* charge the fs quota first */
470                         if (hugetlb_get_quota(mapping)) {
471                                 ret = -ENOMEM;
472                                 goto out;
473                         }
474                         page = alloc_huge_page();
475                         if (!page) {
476                                 hugetlb_put_quota(mapping);
477                                 ret = -ENOMEM;
478                                 goto out;
479                         }
480                         ret = add_to_page_cache(page, mapping, idx, GFP_ATOMIC);
481                         if (! ret) {
482                                 unlock_page(page);
483                         } else {
484                                 hugetlb_put_quota(mapping);
485                                 free_huge_page(page);
486                                 goto out;
487                         }
488                 }
489                 set_huge_pte(mm, vma, page, pte, vma->vm_flags & VM_WRITE);
490         }
491 out:
492         spin_unlock(&mm->page_table_lock);
493         return ret;
494 }
495
496 /* Because we have an exclusive hugepage region which lies within the
497  * normal user address space, we have to take special measures to make
498  * non-huge mmap()s evade the hugepage reserved regions. */
499 unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr,
500                                      unsigned long len, unsigned long pgoff,
501                                      unsigned long flags)
502 {
503         struct mm_struct *mm = current->mm;
504         struct vm_area_struct *vma;
505         unsigned long start_addr;
506
507         if (len > TASK_SIZE)
508                 return -ENOMEM;
509
510         if (addr) {
511                 addr = PAGE_ALIGN(addr);
512                 vma = find_vma(mm, addr);
513                 if (((TASK_SIZE - len) >= addr)
514                     && (!vma || (addr+len) <= vma->vm_start)
515                     && !is_hugepage_only_range(addr,len))
516                         return addr;
517         }
518         start_addr = addr = mm->free_area_cache;
519
520 full_search:
521         vma = find_vma(mm, addr);
522         while (TASK_SIZE - len >= addr) {
523                 BUG_ON(vma && (addr >= vma->vm_end));
524
525                 if (touches_hugepage_low_range(addr, len)) {
526                         addr = ALIGN(addr+1, 1<<SID_SHIFT);
527                         vma = find_vma(mm, addr);
528                         continue;
529                 }
530                 if (touches_hugepage_high_range(addr, len)) {
531                         addr = TASK_HPAGE_END;
532                         vma = find_vma(mm, addr);
533                         continue;
534                 }
535                 if (!vma || addr + len <= vma->vm_start) {
536                         /*
537                          * Remember the place where we stopped the search:
538                          */
539                         mm->free_area_cache = addr + len;
540                         return addr;
541                 }
542                 addr = vma->vm_end;
543                 vma = vma->vm_next;
544         }
545
546         /* Make sure we didn't miss any holes */
547         if (start_addr != TASK_UNMAPPED_BASE) {
548                 start_addr = addr = TASK_UNMAPPED_BASE;
549                 goto full_search;
550         }
551         return -ENOMEM;
552 }
553
554 /*
555  * This mmap-allocator allocates new areas top-down from below the
556  * stack's low limit (the base):
557  *
558  * Because we have an exclusive hugepage region which lies within the
559  * normal user address space, we have to take special measures to make
560  * non-huge mmap()s evade the hugepage reserved regions.
561  */
562 unsigned long
563 arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
564                           const unsigned long len, const unsigned long pgoff,
565                           const unsigned long flags)
566 {
567         struct vm_area_struct *vma, *prev_vma;
568         struct mm_struct *mm = current->mm;
569         unsigned long base = mm->mmap_base, addr = addr0;
570         int first_time = 1;
571
572         /* requested length too big for entire address space */
573         if (len > TASK_SIZE)
574                 return -ENOMEM;
575
576         /* dont allow allocations above current base */
577         if (mm->free_area_cache > base)
578                 mm->free_area_cache = base;
579
580         /* requesting a specific address */
581         if (addr) {
582                 addr = PAGE_ALIGN(addr);
583                 vma = find_vma(mm, addr);
584                 if (TASK_SIZE - len >= addr &&
585                                 (!vma || addr + len <= vma->vm_start)
586                                 && !is_hugepage_only_range(addr,len))
587                         return addr;
588         }
589
590 try_again:
591         /* make sure it can fit in the remaining address space */
592         if (mm->free_area_cache < len)
593                 goto fail;
594
595         /* either no address requested or cant fit in requested address hole */
596         addr = (mm->free_area_cache - len) & PAGE_MASK;
597         do {
598 hugepage_recheck:
599                 if (touches_hugepage_low_range(addr, len)) {
600                         addr = (addr & ((~0) << SID_SHIFT)) - len;
601                         goto hugepage_recheck;
602                 } else if (touches_hugepage_high_range(addr, len)) {
603                         addr = TASK_HPAGE_BASE - len;
604                 }
605
606                 /*
607                  * Lookup failure means no vma is above this address,
608                  * i.e. return with success:
609                  */
610                 if (!(vma = find_vma_prev(mm, addr, &prev_vma)))
611                         return addr;
612
613                 /*
614                  * new region fits between prev_vma->vm_end and
615                  * vma->vm_start, use it:
616                  */
617                 if (addr+len <= vma->vm_start &&
618                                 (!prev_vma || (addr >= prev_vma->vm_end)))
619                         /* remember the address as a hint for next time */
620                         return (mm->free_area_cache = addr);
621                 else
622                         /* pull free_area_cache down to the first hole */
623                         if (mm->free_area_cache == vma->vm_end)
624                                 mm->free_area_cache = vma->vm_start;
625
626                 /* try just below the current vma->vm_start */
627                 addr = vma->vm_start-len;
628         } while (len <= vma->vm_start);
629
630 fail:
631         /*
632          * if hint left us with no space for the requested
633          * mapping then try again:
634          */
635         if (first_time) {
636                 mm->free_area_cache = base;
637                 first_time = 0;
638                 goto try_again;
639         }
640         /*
641          * A failed mmap() very likely causes application failure,
642          * so fall back to the bottom-up function here. This scenario
643          * can happen with large stack limits and large mmap()
644          * allocations.
645          */
646         mm->free_area_cache = TASK_UNMAPPED_BASE;
647         addr = arch_get_unmapped_area(filp, addr0, len, pgoff, flags);
648         /*
649          * Restore the topdown base:
650          */
651         mm->free_area_cache = base;
652
653         return addr;
654 }
655
656 static unsigned long htlb_get_low_area(unsigned long len, u16 segmask)
657 {
658         unsigned long addr = 0;
659         struct vm_area_struct *vma;
660
661         vma = find_vma(current->mm, addr);
662         while (addr + len <= 0x100000000UL) {
663                 BUG_ON(vma && (addr >= vma->vm_end)); /* invariant */
664
665                 if (! __within_hugepage_low_range(addr, len, segmask)) {
666                         addr = ALIGN(addr+1, 1<<SID_SHIFT);
667                         vma = find_vma(current->mm, addr);
668                         continue;
669                 }
670
671                 if (!vma || (addr + len) <= vma->vm_start)
672                         return addr;
673                 addr = ALIGN(vma->vm_end, HPAGE_SIZE);
674                 /* Depending on segmask this might not be a confirmed
675                  * hugepage region, so the ALIGN could have skipped
676                  * some VMAs */
677                 vma = find_vma(current->mm, addr);
678         }
679
680         return -ENOMEM;
681 }
682
683 static unsigned long htlb_get_high_area(unsigned long len)
684 {
685         unsigned long addr = TASK_HPAGE_BASE;
686         struct vm_area_struct *vma;
687
688         vma = find_vma(current->mm, addr);
689         for (vma = find_vma(current->mm, addr);
690              addr + len <= TASK_HPAGE_END;
691              vma = vma->vm_next) {
692                 BUG_ON(vma && (addr >= vma->vm_end)); /* invariant */
693                 BUG_ON(! within_hugepage_high_range(addr, len));
694
695                 if (!vma || (addr + len) <= vma->vm_start)
696                         return addr;
697                 addr = ALIGN(vma->vm_end, HPAGE_SIZE);
698                 /* Because we're in a hugepage region, this alignment
699                  * should not skip us over any VMAs */
700         }
701
702         return -ENOMEM;
703 }
704
705 unsigned long hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
706                                         unsigned long len, unsigned long pgoff,
707                                         unsigned long flags)
708 {
709         if (len & ~HPAGE_MASK)
710                 return -EINVAL;
711
712         if (!(cur_cpu_spec->cpu_features & CPU_FTR_16M_PAGE))
713                 return -EINVAL;
714
715         if (test_thread_flag(TIF_32BIT)) {
716                 int lastshift = 0;
717                 u16 segmask, cursegs = current->mm->context.htlb_segs;
718
719                 /* First see if we can do the mapping in the existing
720                  * low hpage segments */
721                 addr = htlb_get_low_area(len, cursegs);
722                 if (addr != -ENOMEM)
723                         return addr;
724
725                 for (segmask = LOW_ESID_MASK(0x100000000UL-len, len);
726                      ! lastshift; segmask >>=1) {
727                         if (segmask & 1)
728                                 lastshift = 1;
729
730                         addr = htlb_get_low_area(len, cursegs | segmask);
731                         if ((addr != -ENOMEM)
732                             && open_low_hpage_segs(current->mm, segmask) == 0)
733                                 return addr;
734                 }
735                 printk(KERN_DEBUG "hugetlb_get_unmapped_area() unable to open"
736                        " enough segments\n");
737                 return -ENOMEM;
738         } else {
739                 return htlb_get_high_area(len);
740         }
741 }
742
743 void hugetlb_mm_free_pgd(struct mm_struct *mm)
744 {
745         int i;
746         pgd_t *pgdir;
747
748         spin_lock(&mm->page_table_lock);
749
750         pgdir = mm->context.huge_pgdir;
751         if (! pgdir)
752                 goto out;
753
754         mm->context.huge_pgdir = NULL;
755
756         /* cleanup any hugepte pages leftover */
757         for (i = 0; i < PTRS_PER_HUGEPGD; i++) {
758                 pgd_t *pgd = pgdir + i;
759
760                 if (! pgd_none(*pgd)) {
761                         pte_t *pte = (pte_t *)pgd_page(*pgd);
762                         struct page *ptepage = virt_to_page(pte);
763
764                         ptepage->mapping = NULL;
765
766                         BUG_ON(memcmp(pte, empty_zero_page, PAGE_SIZE));
767                         kmem_cache_free(zero_cache, pte);
768                 }
769                 pgd_clear(pgd);
770         }
771
772         BUG_ON(memcmp(pgdir, empty_zero_page, PAGE_SIZE));
773         kmem_cache_free(zero_cache, pgdir);
774
775  out:
776         spin_unlock(&mm->page_table_lock);
777 }
778
779 int hash_huge_page(struct mm_struct *mm, unsigned long access,
780                    unsigned long ea, unsigned long vsid, int local)
781 {
782         pte_t *ptep;
783         unsigned long va, vpn;
784         int is_write;
785         pte_t old_pte, new_pte;
786         unsigned long hpteflags, prpn;
787         long slot;
788         int err = 1;
789
790         spin_lock(&mm->page_table_lock);
791
792         ptep = huge_pte_offset(mm, ea);
793
794         /* Search the Linux page table for a match with va */
795         va = (vsid << 28) | (ea & 0x0fffffff);
796         vpn = va >> HPAGE_SHIFT;
797
798         /*
799          * If no pte found or not present, send the problem up to
800          * do_page_fault
801          */
802         if (unlikely(!ptep || pte_none(*ptep)))
803                 goto out;
804
805 /*      BUG_ON(pte_bad(*ptep)); */
806
807         /* 
808          * Check the user's access rights to the page.  If access should be
809          * prevented then send the problem up to do_page_fault.
810          */
811         is_write = access & _PAGE_RW;
812         if (unlikely(is_write && !(pte_val(*ptep) & _PAGE_RW)))
813                 goto out;
814         /*
815          * At this point, we have a pte (old_pte) which can be used to build
816          * or update an HPTE. There are 2 cases:
817          *
818          * 1. There is a valid (present) pte with no associated HPTE (this is 
819          *      the most common case)
820          * 2. There is a valid (present) pte with an associated HPTE. The
821          *      current values of the pp bits in the HPTE prevent access
822          *      because we are doing software DIRTY bit management and the
823          *      page is currently not DIRTY. 
824          */
825
826
827         old_pte = *ptep;
828         new_pte = old_pte;
829
830         hpteflags = 0x2 | (! (pte_val(new_pte) & _PAGE_RW));
831
832         /* Check if pte already has an hpte (case 2) */
833         if (unlikely(pte_val(old_pte) & _PAGE_HASHPTE)) {
834                 /* There MIGHT be an HPTE for this pte */
835                 unsigned long hash, slot;
836
837                 hash = hpt_hash(vpn, 1);
838                 if (pte_val(old_pte) & _PAGE_SECONDARY)
839                         hash = ~hash;
840                 slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
841                 slot += (pte_val(old_pte) & _PAGE_GROUP_IX) >> 12;
842
843                 if (ppc_md.hpte_updatepp(slot, hpteflags, va, 1, local) == -1)
844                         pte_val(old_pte) &= ~_PAGE_HPTEFLAGS;
845         }
846
847         if (likely(!(pte_val(old_pte) & _PAGE_HASHPTE))) {
848                 unsigned long hash = hpt_hash(vpn, 1);
849                 unsigned long hpte_group;
850
851                 prpn = pte_pfn(old_pte);
852
853 repeat:
854                 hpte_group = ((hash & htab_hash_mask) *
855                               HPTES_PER_GROUP) & ~0x7UL;
856
857                 /* Update the linux pte with the HPTE slot */
858                 pte_val(new_pte) &= ~_PAGE_HPTEFLAGS;
859                 pte_val(new_pte) |= _PAGE_HASHPTE;
860
861                 /* Add in WIMG bits */
862                 /* XXX We should store these in the pte */
863                 hpteflags |= _PAGE_COHERENT;
864
865                 slot = ppc_md.hpte_insert(hpte_group, va, prpn, 0,
866                                           hpteflags, 0, 1);
867
868                 /* Primary is full, try the secondary */
869                 if (unlikely(slot == -1)) {
870                         pte_val(new_pte) |= _PAGE_SECONDARY;
871                         hpte_group = ((~hash & htab_hash_mask) *
872                                       HPTES_PER_GROUP) & ~0x7UL; 
873                         slot = ppc_md.hpte_insert(hpte_group, va, prpn,
874                                                   1, hpteflags, 0, 1);
875                         if (slot == -1) {
876                                 if (mftb() & 0x1)
877                                         hpte_group = ((hash & htab_hash_mask) * HPTES_PER_GROUP) & ~0x7UL;
878
879                                 ppc_md.hpte_remove(hpte_group);
880                                 goto repeat;
881                         }
882                 }
883
884                 if (unlikely(slot == -2))
885                         panic("hash_huge_page: pte_insert failed\n");
886
887                 pte_val(new_pte) |= (slot<<12) & _PAGE_GROUP_IX;
888
889                 /* 
890                  * No need to use ldarx/stdcx here because all who
891                  * might be updating the pte will hold the
892                  * page_table_lock
893                  */
894                 *ptep = new_pte;
895         }
896
897         err = 0;
898
899  out:
900         spin_unlock(&mm->page_table_lock);
901
902         return err;
903 }