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