d3bf86a5c1ad40a745ded13bdf9d9b6aa80da0c8
[linux-3.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 pud_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 (pud_t *)(mm->context.huge_pgdir + index);
56 }
57
58 static inline pte_t *hugepte_offset(pud_t *dir, unsigned long addr)
59 {
60         int index;
61
62         if (pud_none(*dir))
63                 return NULL;
64
65         index = (addr >> HPAGE_SHIFT) % PTRS_PER_HUGEPTE;
66         return (pte_t *)pud_page(*dir) + index;
67 }
68
69 static pud_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, pud_t *dir, unsigned long addr)
94 {
95         if (! pud_present(*dir)) {
96                 pte_t *new;
97
98                 spin_unlock(&mm->page_table_lock);
99                 new = kmem_cache_alloc(zero_cache, GFP_KERNEL | __GFP_REPEAT);
100                 BUG_ON(memcmp(new, empty_zero_page, PAGE_SIZE));
101                 spin_lock(&mm->page_table_lock);
102                 /*
103                  * Because we dropped the lock, we should re-check the
104                  * entry, as somebody else could have populated it..
105                  */
106                 if (pud_present(*dir)) {
107                         if (new)
108                                 kmem_cache_free(zero_cache, new);
109                 } else {
110                         struct page *ptepage;
111
112                         if (! new)
113                                 return NULL;
114                         ptepage = virt_to_page(new);
115                         ptepage->mapping = (void *) mm;
116                         ptepage->index = addr & HUGEPGDIR_MASK;
117                         pud_populate(mm, dir, new);
118                 }
119         }
120
121         return hugepte_offset(dir, addr);
122 }
123
124 static pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr)
125 {
126         pud_t *pud;
127
128         BUG_ON(! in_hugepage_area(mm->context, addr));
129
130         pud = hugepgd_offset(mm, addr);
131         if (! pud)
132                 return NULL;
133
134         return hugepte_offset(pud, addr);
135 }
136
137 static pte_t *huge_pte_alloc(struct mm_struct *mm, unsigned long addr)
138 {
139         pud_t *pud;
140
141         BUG_ON(! in_hugepage_area(mm->context, addr));
142
143         pud = hugepgd_alloc(mm, addr);
144         if (! pud)
145                 return NULL;
146
147         return hugepte_alloc(mm, pud, addr);
148 }
149
150 static void set_huge_pte(struct mm_struct *mm, struct vm_area_struct *vma,
151                          unsigned long addr, struct page *page,
152                          pte_t *ptep, int write_access)
153 {
154         pte_t entry;
155
156         add_mm_counter(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_at(mm, addr, 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
206         BUG_ON(seg >= 16);
207
208         /* Check no VMAs are in the region */
209         vma = find_vma(mm, start);
210         if (vma && (vma->vm_start < end))
211                 return -EBUSY;
212
213         return 0;
214 }
215
216 static int open_low_hpage_segs(struct mm_struct *mm, u16 newsegs)
217 {
218         unsigned long i;
219
220         newsegs &= ~(mm->context.htlb_segs);
221         if (! newsegs)
222                 return 0; /* The segments we want are already open */
223
224         for (i = 0; i < 16; i++)
225                 if ((1 << i) & newsegs)
226                         if (prepare_low_seg_for_htlb(mm, i) != 0)
227                                 return -EBUSY;
228
229         mm->context.htlb_segs |= newsegs;
230
231         /* update the paca copy of the context struct */
232         get_paca()->context = mm->context;
233
234         /* the context change must make it to memory before the flush,
235          * so that further SLB misses do the right thing. */
236         mb();
237         on_each_cpu(flush_segments, (void *)(unsigned long)newsegs, 0, 1);
238
239         return 0;
240 }
241
242 int prepare_hugepage_range(unsigned long addr, unsigned long len)
243 {
244         if (within_hugepage_high_range(addr, len))
245                 return 0;
246         else if ((addr < 0x100000000UL) && ((addr+len) < 0x100000000UL)) {
247                 int err;
248                 /* Yes, we need both tests, in case addr+len overflows
249                  * 64-bit arithmetic */
250                 err = open_low_hpage_segs(current->mm,
251                                           LOW_ESID_MASK(addr, len));
252                 if (err)
253                         printk(KERN_DEBUG "prepare_hugepage_range(%lx, %lx)"
254                                " failed (segs: 0x%04hx)\n", addr, len,
255                                LOW_ESID_MASK(addr, len));
256                 return err;
257         }
258
259         return -EINVAL;
260 }
261
262 int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src,
263                         struct vm_area_struct *vma)
264 {
265         pte_t *src_pte, *dst_pte, entry;
266         struct page *ptepage;
267         unsigned long addr = vma->vm_start;
268         unsigned long end = vma->vm_end;
269         int err = -ENOMEM;
270
271         while (addr < end) {
272                 dst_pte = huge_pte_alloc(dst, addr);
273                 if (!dst_pte)
274                         goto out;
275
276                 src_pte = huge_pte_offset(src, addr);
277                 entry = *src_pte;
278                 
279                 ptepage = pte_page(entry);
280                 get_page(ptepage);
281                 add_mm_counter(dst, rss, HPAGE_SIZE / PAGE_SIZE);
282                 set_pte_at(dst, addr, dst_pte, entry);
283
284                 addr += HPAGE_SIZE;
285         }
286
287         err = 0;
288  out:
289         return err;
290 }
291
292 int
293 follow_hugetlb_page(struct mm_struct *mm, struct vm_area_struct *vma,
294                     struct page **pages, struct vm_area_struct **vmas,
295                     unsigned long *position, int *length, int i)
296 {
297         unsigned long vpfn, vaddr = *position;
298         int remainder = *length;
299
300         WARN_ON(!is_vm_hugetlb_page(vma));
301
302         vpfn = vaddr/PAGE_SIZE;
303         while (vaddr < vma->vm_end && remainder) {
304                 if (pages) {
305                         pte_t *pte;
306                         struct page *page;
307
308                         pte = huge_pte_offset(mm, vaddr);
309
310                         /* hugetlb should be locked, and hence, prefaulted */
311                         WARN_ON(!pte || pte_none(*pte));
312
313                         page = &pte_page(*pte)[vpfn % (HPAGE_SIZE/PAGE_SIZE)];
314
315                         WARN_ON(!PageCompound(page));
316
317                         get_page(page);
318                         pages[i] = page;
319                 }
320
321                 if (vmas)
322                         vmas[i] = vma;
323
324                 vaddr += PAGE_SIZE;
325                 ++vpfn;
326                 --remainder;
327                 ++i;
328         }
329
330         *length = remainder;
331         *position = vaddr;
332
333         return i;
334 }
335
336 struct page *
337 follow_huge_addr(struct mm_struct *mm, unsigned long address, int write)
338 {
339         pte_t *ptep;
340         struct page *page;
341
342         if (! in_hugepage_area(mm->context, address))
343                 return ERR_PTR(-EINVAL);
344
345         ptep = huge_pte_offset(mm, address);
346         page = pte_page(*ptep);
347         if (page)
348                 page += (address % HPAGE_SIZE) / PAGE_SIZE;
349
350         return page;
351 }
352
353 int pmd_huge(pmd_t pmd)
354 {
355         return 0;
356 }
357
358 struct page *
359 follow_huge_pmd(struct mm_struct *mm, unsigned long address,
360                 pmd_t *pmd, int write)
361 {
362         BUG();
363         return NULL;
364 }
365
366 void unmap_hugepage_range(struct vm_area_struct *vma,
367                           unsigned long start, unsigned long end)
368 {
369         struct mm_struct *mm = vma->vm_mm;
370         unsigned long addr;
371         pte_t *ptep;
372         struct page *page;
373
374         WARN_ON(!is_vm_hugetlb_page(vma));
375         BUG_ON((start % HPAGE_SIZE) != 0);
376         BUG_ON((end % HPAGE_SIZE) != 0);
377
378         for (addr = start; addr < end; addr += HPAGE_SIZE) {
379                 pte_t pte;
380
381                 ptep = huge_pte_offset(mm, addr);
382                 if (!ptep || pte_none(*ptep))
383                         continue;
384
385                 pte = *ptep;
386                 page = pte_page(pte);
387                 pte_clear(mm, addr, ptep);
388
389                 put_page(page);
390         }
391         add_mm_counter(mm, rss, -((end - start) >> PAGE_SHIFT));
392         flush_tlb_pending();
393 }
394
395 int hugetlb_prefault(struct address_space *mapping, struct vm_area_struct *vma)
396 {
397         struct mm_struct *mm = current->mm;
398         unsigned long addr;
399         int ret = 0;
400
401         WARN_ON(!is_vm_hugetlb_page(vma));
402         BUG_ON((vma->vm_start % HPAGE_SIZE) != 0);
403         BUG_ON((vma->vm_end % HPAGE_SIZE) != 0);
404
405         spin_lock(&mm->page_table_lock);
406         for (addr = vma->vm_start; addr < vma->vm_end; addr += HPAGE_SIZE) {
407                 unsigned long idx;
408                 pte_t *pte = huge_pte_alloc(mm, addr);
409                 struct page *page;
410
411                 if (!pte) {
412                         ret = -ENOMEM;
413                         goto out;
414                 }
415                 if (! pte_none(*pte))
416                         continue;
417
418                 idx = ((addr - vma->vm_start) >> HPAGE_SHIFT)
419                         + (vma->vm_pgoff >> (HPAGE_SHIFT - PAGE_SHIFT));
420                 page = find_get_page(mapping, idx);
421                 if (!page) {
422                         /* charge the fs quota first */
423                         if (hugetlb_get_quota(mapping)) {
424                                 ret = -ENOMEM;
425                                 goto out;
426                         }
427                         page = alloc_huge_page();
428                         if (!page) {
429                                 hugetlb_put_quota(mapping);
430                                 ret = -ENOMEM;
431                                 goto out;
432                         }
433                         ret = add_to_page_cache(page, mapping, idx, GFP_ATOMIC);
434                         if (! ret) {
435                                 unlock_page(page);
436                         } else {
437                                 hugetlb_put_quota(mapping);
438                                 free_huge_page(page);
439                                 goto out;
440                         }
441                 }
442                 set_huge_pte(mm, vma, addr, page, pte, vma->vm_flags & VM_WRITE);
443         }
444 out:
445         spin_unlock(&mm->page_table_lock);
446         return ret;
447 }
448
449 /* Because we have an exclusive hugepage region which lies within the
450  * normal user address space, we have to take special measures to make
451  * non-huge mmap()s evade the hugepage reserved regions. */
452 unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr,
453                                      unsigned long len, unsigned long pgoff,
454                                      unsigned long flags)
455 {
456         struct mm_struct *mm = current->mm;
457         struct vm_area_struct *vma;
458         unsigned long start_addr;
459
460         if (len > TASK_SIZE)
461                 return -ENOMEM;
462
463         if (addr) {
464                 addr = PAGE_ALIGN(addr);
465                 vma = find_vma(mm, addr);
466                 if (((TASK_SIZE - len) >= addr)
467                     && (!vma || (addr+len) <= vma->vm_start)
468                     && !is_hugepage_only_range(mm, addr,len))
469                         return addr;
470         }
471         start_addr = addr = mm->free_area_cache;
472
473 full_search:
474         vma = find_vma(mm, addr);
475         while (TASK_SIZE - len >= addr) {
476                 BUG_ON(vma && (addr >= vma->vm_end));
477
478                 if (touches_hugepage_low_range(mm, addr, len)) {
479                         addr = ALIGN(addr+1, 1<<SID_SHIFT);
480                         vma = find_vma(mm, addr);
481                         continue;
482                 }
483                 if (touches_hugepage_high_range(addr, len)) {
484                         addr = TASK_HPAGE_END;
485                         vma = find_vma(mm, addr);
486                         continue;
487                 }
488                 if (!vma || addr + len <= vma->vm_start) {
489                         /*
490                          * Remember the place where we stopped the search:
491                          */
492                         mm->free_area_cache = addr + len;
493                         return addr;
494                 }
495                 addr = vma->vm_end;
496                 vma = vma->vm_next;
497         }
498
499         /* Make sure we didn't miss any holes */
500         if (start_addr != TASK_UNMAPPED_BASE) {
501                 start_addr = addr = TASK_UNMAPPED_BASE;
502                 goto full_search;
503         }
504         return -ENOMEM;
505 }
506
507 /*
508  * This mmap-allocator allocates new areas top-down from below the
509  * stack's low limit (the base):
510  *
511  * Because we have an exclusive hugepage region which lies within the
512  * normal user address space, we have to take special measures to make
513  * non-huge mmap()s evade the hugepage reserved regions.
514  */
515 unsigned long
516 arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
517                           const unsigned long len, const unsigned long pgoff,
518                           const unsigned long flags)
519 {
520         struct vm_area_struct *vma, *prev_vma;
521         struct mm_struct *mm = current->mm;
522         unsigned long base = mm->mmap_base, addr = addr0;
523         int first_time = 1;
524
525         /* requested length too big for entire address space */
526         if (len > TASK_SIZE)
527                 return -ENOMEM;
528
529         /* dont allow allocations above current base */
530         if (mm->free_area_cache > base)
531                 mm->free_area_cache = base;
532
533         /* requesting a specific address */
534         if (addr) {
535                 addr = PAGE_ALIGN(addr);
536                 vma = find_vma(mm, addr);
537                 if (TASK_SIZE - len >= addr &&
538                                 (!vma || addr + len <= vma->vm_start)
539                                 && !is_hugepage_only_range(mm, addr,len))
540                         return addr;
541         }
542
543 try_again:
544         /* make sure it can fit in the remaining address space */
545         if (mm->free_area_cache < len)
546                 goto fail;
547
548         /* either no address requested or cant fit in requested address hole */
549         addr = (mm->free_area_cache - len) & PAGE_MASK;
550         do {
551 hugepage_recheck:
552                 if (touches_hugepage_low_range(mm, addr, len)) {
553                         addr = (addr & ((~0) << SID_SHIFT)) - len;
554                         goto hugepage_recheck;
555                 } else if (touches_hugepage_high_range(addr, len)) {
556                         addr = TASK_HPAGE_BASE - len;
557                 }
558
559                 /*
560                  * Lookup failure means no vma is above this address,
561                  * i.e. return with success:
562                  */
563                 if (!(vma = find_vma_prev(mm, addr, &prev_vma)))
564                         return addr;
565
566                 /*
567                  * new region fits between prev_vma->vm_end and
568                  * vma->vm_start, use it:
569                  */
570                 if (addr+len <= vma->vm_start &&
571                                 (!prev_vma || (addr >= prev_vma->vm_end)))
572                         /* remember the address as a hint for next time */
573                         return (mm->free_area_cache = addr);
574                 else
575                         /* pull free_area_cache down to the first hole */
576                         if (mm->free_area_cache == vma->vm_end)
577                                 mm->free_area_cache = vma->vm_start;
578
579                 /* try just below the current vma->vm_start */
580                 addr = vma->vm_start-len;
581         } while (len <= vma->vm_start);
582
583 fail:
584         /*
585          * if hint left us with no space for the requested
586          * mapping then try again:
587          */
588         if (first_time) {
589                 mm->free_area_cache = base;
590                 first_time = 0;
591                 goto try_again;
592         }
593         /*
594          * A failed mmap() very likely causes application failure,
595          * so fall back to the bottom-up function here. This scenario
596          * can happen with large stack limits and large mmap()
597          * allocations.
598          */
599         mm->free_area_cache = TASK_UNMAPPED_BASE;
600         addr = arch_get_unmapped_area(filp, addr0, len, pgoff, flags);
601         /*
602          * Restore the topdown base:
603          */
604         mm->free_area_cache = base;
605
606         return addr;
607 }
608
609 static unsigned long htlb_get_low_area(unsigned long len, u16 segmask)
610 {
611         unsigned long addr = 0;
612         struct vm_area_struct *vma;
613
614         vma = find_vma(current->mm, addr);
615         while (addr + len <= 0x100000000UL) {
616                 BUG_ON(vma && (addr >= vma->vm_end)); /* invariant */
617
618                 if (! __within_hugepage_low_range(addr, len, segmask)) {
619                         addr = ALIGN(addr+1, 1<<SID_SHIFT);
620                         vma = find_vma(current->mm, addr);
621                         continue;
622                 }
623
624                 if (!vma || (addr + len) <= vma->vm_start)
625                         return addr;
626                 addr = ALIGN(vma->vm_end, HPAGE_SIZE);
627                 /* Depending on segmask this might not be a confirmed
628                  * hugepage region, so the ALIGN could have skipped
629                  * some VMAs */
630                 vma = find_vma(current->mm, addr);
631         }
632
633         return -ENOMEM;
634 }
635
636 static unsigned long htlb_get_high_area(unsigned long len)
637 {
638         unsigned long addr = TASK_HPAGE_BASE;
639         struct vm_area_struct *vma;
640
641         vma = find_vma(current->mm, addr);
642         for (vma = find_vma(current->mm, addr);
643              addr + len <= TASK_HPAGE_END;
644              vma = vma->vm_next) {
645                 BUG_ON(vma && (addr >= vma->vm_end)); /* invariant */
646                 BUG_ON(! within_hugepage_high_range(addr, len));
647
648                 if (!vma || (addr + len) <= vma->vm_start)
649                         return addr;
650                 addr = ALIGN(vma->vm_end, HPAGE_SIZE);
651                 /* Because we're in a hugepage region, this alignment
652                  * should not skip us over any VMAs */
653         }
654
655         return -ENOMEM;
656 }
657
658 unsigned long hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
659                                         unsigned long len, unsigned long pgoff,
660                                         unsigned long flags)
661 {
662         if (len & ~HPAGE_MASK)
663                 return -EINVAL;
664
665         if (!cpu_has_feature(CPU_FTR_16M_PAGE))
666                 return -EINVAL;
667
668         if (test_thread_flag(TIF_32BIT)) {
669                 int lastshift = 0;
670                 u16 segmask, cursegs = current->mm->context.htlb_segs;
671
672                 /* First see if we can do the mapping in the existing
673                  * low hpage segments */
674                 addr = htlb_get_low_area(len, cursegs);
675                 if (addr != -ENOMEM)
676                         return addr;
677
678                 for (segmask = LOW_ESID_MASK(0x100000000UL-len, len);
679                      ! lastshift; segmask >>=1) {
680                         if (segmask & 1)
681                                 lastshift = 1;
682
683                         addr = htlb_get_low_area(len, cursegs | segmask);
684                         if ((addr != -ENOMEM)
685                             && open_low_hpage_segs(current->mm, segmask) == 0)
686                                 return addr;
687                 }
688                 printk(KERN_DEBUG "hugetlb_get_unmapped_area() unable to open"
689                        " enough segments\n");
690                 return -ENOMEM;
691         } else {
692                 return htlb_get_high_area(len);
693         }
694 }
695
696 void hugetlb_mm_free_pgd(struct mm_struct *mm)
697 {
698         int i;
699         pgd_t *pgdir;
700
701         spin_lock(&mm->page_table_lock);
702
703         pgdir = mm->context.huge_pgdir;
704         if (! pgdir)
705                 goto out;
706
707         mm->context.huge_pgdir = NULL;
708
709         /* cleanup any hugepte pages leftover */
710         for (i = 0; i < PTRS_PER_HUGEPGD; i++) {
711                 pud_t *pud = (pud_t *)(pgdir + i);
712
713                 if (! pud_none(*pud)) {
714                         pte_t *pte = (pte_t *)pud_page(*pud);
715                         struct page *ptepage = virt_to_page(pte);
716
717                         ptepage->mapping = NULL;
718
719                         BUG_ON(memcmp(pte, empty_zero_page, PAGE_SIZE));
720                         kmem_cache_free(zero_cache, pte);
721                 }
722                 pud_clear(pud);
723         }
724
725         BUG_ON(memcmp(pgdir, empty_zero_page, PAGE_SIZE));
726         kmem_cache_free(zero_cache, pgdir);
727
728  out:
729         spin_unlock(&mm->page_table_lock);
730 }
731
732 int hash_huge_page(struct mm_struct *mm, unsigned long access,
733                    unsigned long ea, unsigned long vsid, int local)
734 {
735         pte_t *ptep;
736         unsigned long va, vpn;
737         pte_t old_pte, new_pte;
738         unsigned long hpteflags, prpn;
739         long slot;
740         int err = 1;
741
742         spin_lock(&mm->page_table_lock);
743
744         ptep = huge_pte_offset(mm, ea);
745
746         /* Search the Linux page table for a match with va */
747         va = (vsid << 28) | (ea & 0x0fffffff);
748         vpn = va >> HPAGE_SHIFT;
749
750         /*
751          * If no pte found or not present, send the problem up to
752          * do_page_fault
753          */
754         if (unlikely(!ptep || pte_none(*ptep)))
755                 goto out;
756
757 /*      BUG_ON(pte_bad(*ptep)); */
758
759         /* 
760          * Check the user's access rights to the page.  If access should be
761          * prevented then send the problem up to do_page_fault.
762          */
763         if (unlikely(access & ~pte_val(*ptep)))
764                 goto out;
765         /*
766          * At this point, we have a pte (old_pte) which can be used to build
767          * or update an HPTE. There are 2 cases:
768          *
769          * 1. There is a valid (present) pte with no associated HPTE (this is 
770          *      the most common case)
771          * 2. There is a valid (present) pte with an associated HPTE. The
772          *      current values of the pp bits in the HPTE prevent access
773          *      because we are doing software DIRTY bit management and the
774          *      page is currently not DIRTY. 
775          */
776
777
778         old_pte = *ptep;
779         new_pte = old_pte;
780
781         hpteflags = 0x2 | (! (pte_val(new_pte) & _PAGE_RW));
782         /* _PAGE_EXEC -> HW_NO_EXEC since it's inverted */
783         hpteflags |= ((pte_val(new_pte) & _PAGE_EXEC) ? 0 : HW_NO_EXEC);
784
785         /* Check if pte already has an hpte (case 2) */
786         if (unlikely(pte_val(old_pte) & _PAGE_HASHPTE)) {
787                 /* There MIGHT be an HPTE for this pte */
788                 unsigned long hash, slot;
789
790                 hash = hpt_hash(vpn, 1);
791                 if (pte_val(old_pte) & _PAGE_SECONDARY)
792                         hash = ~hash;
793                 slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
794                 slot += (pte_val(old_pte) & _PAGE_GROUP_IX) >> 12;
795
796                 if (ppc_md.hpte_updatepp(slot, hpteflags, va, 1, local) == -1)
797                         pte_val(old_pte) &= ~_PAGE_HPTEFLAGS;
798         }
799
800         if (likely(!(pte_val(old_pte) & _PAGE_HASHPTE))) {
801                 unsigned long hash = hpt_hash(vpn, 1);
802                 unsigned long hpte_group;
803
804                 prpn = pte_pfn(old_pte);
805
806 repeat:
807                 hpte_group = ((hash & htab_hash_mask) *
808                               HPTES_PER_GROUP) & ~0x7UL;
809
810                 /* Update the linux pte with the HPTE slot */
811                 pte_val(new_pte) &= ~_PAGE_HPTEFLAGS;
812                 pte_val(new_pte) |= _PAGE_HASHPTE;
813
814                 /* Add in WIMG bits */
815                 /* XXX We should store these in the pte */
816                 hpteflags |= _PAGE_COHERENT;
817
818                 slot = ppc_md.hpte_insert(hpte_group, va, prpn, 0,
819                                           hpteflags, 0, 1);
820
821                 /* Primary is full, try the secondary */
822                 if (unlikely(slot == -1)) {
823                         pte_val(new_pte) |= _PAGE_SECONDARY;
824                         hpte_group = ((~hash & htab_hash_mask) *
825                                       HPTES_PER_GROUP) & ~0x7UL; 
826                         slot = ppc_md.hpte_insert(hpte_group, va, prpn,
827                                                   1, hpteflags, 0, 1);
828                         if (slot == -1) {
829                                 if (mftb() & 0x1)
830                                         hpte_group = ((hash & htab_hash_mask) * HPTES_PER_GROUP) & ~0x7UL;
831
832                                 ppc_md.hpte_remove(hpte_group);
833                                 goto repeat;
834                         }
835                 }
836
837                 if (unlikely(slot == -2))
838                         panic("hash_huge_page: pte_insert failed\n");
839
840                 pte_val(new_pte) |= (slot<<12) & _PAGE_GROUP_IX;
841
842                 /* 
843                  * No need to use ldarx/stdcx here because all who
844                  * might be updating the pte will hold the
845                  * page_table_lock
846                  */
847                 *ptep = new_pte;
848         }
849
850         err = 0;
851
852  out:
853         spin_unlock(&mm->page_table_lock);
854
855         return err;
856 }