Fix arch headers.
[linux-flexiantxendom0-3.2.10.git] / mm / memory.c
1 /*
2  *  linux/mm/memory.c
3  *
4  *  Copyright (C) 1991, 1992, 1993, 1994  Linus Torvalds
5  */
6
7 /*
8  * demand-loading started 01.12.91 - seems it is high on the list of
9  * things wanted, and it should be easy to implement. - Linus
10  */
11
12 /*
13  * Ok, demand-loading was easy, shared pages a little bit tricker. Shared
14  * pages started 02.12.91, seems to work. - Linus.
15  *
16  * Tested sharing by executing about 30 /bin/sh: under the old kernel it
17  * would have taken more than the 6M I have free, but it worked well as
18  * far as I could see.
19  *
20  * Also corrected some "invalidate()"s - I wasn't doing enough of them.
21  */
22
23 /*
24  * Real VM (paging to/from disk) started 18.12.91. Much more work and
25  * thought has to go into this. Oh, well..
26  * 19.12.91  -  works, somewhat. Sometimes I get faults, don't know why.
27  *              Found it. Everything seems to work now.
28  * 20.12.91  -  Ok, making the swap-device changeable like the root.
29  */
30
31 /*
32  * 05.04.94  -  Multi-page memory management added for v1.1.
33  *              Idea by Alex Bligh (alex@cconcepts.co.uk)
34  *
35  * 16.07.99  -  Support of BIGMEM added by Gerhard Wichert, Siemens AG
36  *              (Gerhard.Wichert@pdb.siemens.de)
37  */
38
39 #include <linux/kernel_stat.h>
40 #include <linux/mm.h>
41 #include <linux/hugetlb.h>
42 #include <linux/mman.h>
43 #include <linux/swap.h>
44 #include <linux/highmem.h>
45 #include <linux/pagemap.h>
46 #include <linux/vcache.h>
47 #include <linux/rmap-locking.h>
48
49 #include <asm/pgalloc.h>
50 #include <asm/rmap.h>
51 #include <asm/uaccess.h>
52 #include <asm/tlb.h>
53 #include <asm/tlbflush.h>
54 #include <asm/pgtable.h>
55
56 #include <linux/swapops.h>
57
58 #ifndef CONFIG_DISCONTIGMEM
59 /* use the per-pgdat data instead for discontigmem - mbligh */
60 unsigned long max_mapnr;
61 struct page *mem_map;
62 #endif
63
64 unsigned long num_physpages;
65 void * high_memory;
66 struct page *highmem_start_page;
67
68 /*
69  * We special-case the C-O-W ZERO_PAGE, because it's such
70  * a common occurrence (no need to read the page to know
71  * that it's zero - better for the cache and memory subsystem).
72  */
73 static inline void copy_cow_page(struct page * from, struct page * to, unsigned long address)
74 {
75         if (from == ZERO_PAGE(address)) {
76                 clear_user_highpage(to, address);
77                 return;
78         }
79         copy_user_highpage(to, from, address);
80 }
81
82 /*
83  * Note: this doesn't free the actual pages themselves. That
84  * has been handled earlier when unmapping all the memory regions.
85  */
86 static inline void free_one_pmd(struct mmu_gather *tlb, pmd_t * dir)
87 {
88         struct page *page;
89
90         if (pmd_none(*dir))
91                 return;
92         if (pmd_bad(*dir)) {
93                 pmd_ERROR(*dir);
94                 pmd_clear(dir);
95                 return;
96         }
97         page = pmd_page(*dir);
98         pmd_clear(dir);
99         pgtable_remove_rmap(page);
100         pte_free_tlb(tlb, page);
101 }
102
103 static inline void free_one_pgd(struct mmu_gather *tlb, pgd_t * dir)
104 {
105         int j;
106         pmd_t * pmd;
107
108         if (pgd_none(*dir))
109                 return;
110         if (pgd_bad(*dir)) {
111                 pgd_ERROR(*dir);
112                 pgd_clear(dir);
113                 return;
114         }
115         pmd = pmd_offset(dir, 0);
116         pgd_clear(dir);
117         for (j = 0; j < PTRS_PER_PMD ; j++) {
118                 prefetchw(pmd + j + PREFETCH_STRIDE/sizeof(*pmd));
119                 free_one_pmd(tlb, pmd+j);
120         }
121         pmd_free_tlb(tlb, pmd);
122 }
123
124 /*
125  * This function clears all user-level page tables of a process - this
126  * is needed by execve(), so that old pages aren't in the way.
127  *
128  * Must be called with pagetable lock held.
129  */
130 void clear_page_tables(struct mmu_gather *tlb, unsigned long first, int nr)
131 {
132         pgd_t * page_dir = tlb->mm->pgd;
133
134         page_dir += first;
135         do {
136                 free_one_pgd(tlb, page_dir);
137                 page_dir++;
138         } while (--nr);
139 }
140
141 pte_t * pte_alloc_map(struct mm_struct *mm, pmd_t *pmd, unsigned long address)
142 {
143         if (!pmd_present(*pmd)) {
144                 struct page *new;
145
146                 spin_unlock(&mm->page_table_lock);
147                 new = pte_alloc_one(mm, address);
148                 spin_lock(&mm->page_table_lock);
149                 if (!new)
150                         return NULL;
151
152                 /*
153                  * Because we dropped the lock, we should re-check the
154                  * entry, as somebody else could have populated it..
155                  */
156                 if (pmd_present(*pmd)) {
157                         pte_free(new);
158                         goto out;
159                 }
160                 pgtable_add_rmap(new, mm, address);
161                 pmd_populate(mm, pmd, new);
162         }
163 out:
164         return pte_offset_map(pmd, address);
165 }
166
167 pte_t * pte_alloc_kernel(struct mm_struct *mm, pmd_t *pmd, unsigned long address)
168 {
169         if (!pmd_present(*pmd)) {
170                 pte_t *new;
171
172                 spin_unlock(&mm->page_table_lock);
173                 new = pte_alloc_one_kernel(mm, address);
174                 spin_lock(&mm->page_table_lock);
175                 if (!new)
176                         return NULL;
177
178                 /*
179                  * Because we dropped the lock, we should re-check the
180                  * entry, as somebody else could have populated it..
181                  */
182                 if (pmd_present(*pmd)) {
183                         pte_free_kernel(new);
184                         goto out;
185                 }
186                 pgtable_add_rmap(virt_to_page(new), mm, address);
187                 pmd_populate_kernel(mm, pmd, new);
188         }
189 out:
190         return pte_offset_kernel(pmd, address);
191 }
192 #define PTE_TABLE_MASK  ((PTRS_PER_PTE-1) * sizeof(pte_t))
193 #define PMD_TABLE_MASK  ((PTRS_PER_PMD-1) * sizeof(pmd_t))
194
195 /*
196  * copy one vm_area from one task to the other. Assumes the page tables
197  * already present in the new task to be cleared in the whole range
198  * covered by this vma.
199  *
200  * 08Jan98 Merged into one routine from several inline routines to reduce
201  *         variable count and make things faster. -jj
202  *
203  * dst->page_table_lock is held on entry and exit,
204  * but may be dropped within pmd_alloc() and pte_alloc_map().
205  */
206 int copy_page_range(struct mm_struct *dst, struct mm_struct *src,
207                         struct vm_area_struct *vma)
208 {
209         pgd_t * src_pgd, * dst_pgd;
210         unsigned long address = vma->vm_start;
211         unsigned long end = vma->vm_end;
212         unsigned long cow;
213         struct pte_chain *pte_chain = NULL;
214
215         if (is_vm_hugetlb_page(vma))
216                 return copy_hugetlb_page_range(dst, src, vma);
217
218         pte_chain = pte_chain_alloc(GFP_ATOMIC);
219         if (!pte_chain) {
220                 spin_unlock(&dst->page_table_lock);
221                 pte_chain = pte_chain_alloc(GFP_KERNEL);
222                 spin_lock(&dst->page_table_lock);
223                 if (!pte_chain)
224                         goto nomem;
225         }
226         
227         cow = (vma->vm_flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE;
228         src_pgd = pgd_offset(src, address)-1;
229         dst_pgd = pgd_offset(dst, address)-1;
230
231         for (;;) {
232                 pmd_t * src_pmd, * dst_pmd;
233
234                 src_pgd++; dst_pgd++;
235                 
236                 /* copy_pmd_range */
237                 
238                 if (pgd_none(*src_pgd))
239                         goto skip_copy_pmd_range;
240                 if (pgd_bad(*src_pgd)) {
241                         pgd_ERROR(*src_pgd);
242                         pgd_clear(src_pgd);
243 skip_copy_pmd_range:    address = (address + PGDIR_SIZE) & PGDIR_MASK;
244                         if (!address || (address >= end))
245                                 goto out;
246                         continue;
247                 }
248
249                 src_pmd = pmd_offset(src_pgd, address);
250                 dst_pmd = pmd_alloc(dst, dst_pgd, address);
251                 if (!dst_pmd)
252                         goto nomem;
253
254                 do {
255                         pte_t * src_pte, * dst_pte;
256                 
257                         /* copy_pte_range */
258                 
259                         if (pmd_none(*src_pmd))
260                                 goto skip_copy_pte_range;
261                         if (pmd_bad(*src_pmd)) {
262                                 pmd_ERROR(*src_pmd);
263                                 pmd_clear(src_pmd);
264 skip_copy_pte_range:
265                                 address = (address + PMD_SIZE) & PMD_MASK;
266                                 if (address >= end)
267                                         goto out;
268                                 goto cont_copy_pmd_range;
269                         }
270
271                         dst_pte = pte_alloc_map(dst, dst_pmd, address);
272                         if (!dst_pte)
273                                 goto nomem;
274                         spin_lock(&src->page_table_lock);       
275                         src_pte = pte_offset_map_nested(src_pmd, address);
276                         do {
277                                 pte_t pte = *src_pte;
278                                 struct page *page;
279                                 unsigned long pfn;
280
281                                 /* copy_one_pte */
282
283                                 if (pte_none(pte))
284                                         goto cont_copy_pte_range_noset;
285                                 /* pte contains position in swap, so copy. */
286                                 if (!pte_present(pte)) {
287                                         if (!pte_file(pte))
288                                                 swap_duplicate(pte_to_swp_entry(pte));
289                                         set_pte(dst_pte, pte);
290                                         goto cont_copy_pte_range_noset;
291                                 }
292                                 pfn = pte_pfn(pte);
293                                 /* the pte points outside of valid memory, the
294                                  * mapping is assumed to be good, meaningful
295                                  * and not mapped via rmap - duplicate the
296                                  * mapping as is.
297                                  */
298                                 page = NULL;
299                                 if (pfn_valid(pfn)) 
300                                         page = pfn_to_page(pfn); 
301
302                                 if (!page || PageReserved(page)) {
303                                         set_pte(dst_pte, pte);
304                                         goto cont_copy_pte_range_noset;
305                                 }
306
307                                 /*
308                                  * If it's a COW mapping, write protect it both
309                                  * in the parent and the child
310                                  */
311                                 if (cow) {
312                                         ptep_set_wrprotect(src_pte);
313                                         pte = *src_pte;
314                                 }
315
316                                 /*
317                                  * If it's a shared mapping, mark it clean in
318                                  * the child
319                                  */
320                                 if (vma->vm_flags & VM_SHARED)
321                                         pte = pte_mkclean(pte);
322                                 pte = pte_mkold(pte);
323                                 get_page(page);
324                                 dst->rss++;
325
326                                 set_pte(dst_pte, pte);
327                                 pte_chain = page_add_rmap(page, dst_pte,
328                                                         pte_chain);
329                                 if (pte_chain)
330                                         goto cont_copy_pte_range_noset;
331                                 pte_chain = pte_chain_alloc(GFP_ATOMIC);
332                                 if (pte_chain)
333                                         goto cont_copy_pte_range_noset;
334
335                                 /*
336                                  * pte_chain allocation failed, and we need to
337                                  * run page reclaim.
338                                  */
339                                 pte_unmap_nested(src_pte);
340                                 pte_unmap(dst_pte);
341                                 spin_unlock(&src->page_table_lock);     
342                                 spin_unlock(&dst->page_table_lock);     
343                                 pte_chain = pte_chain_alloc(GFP_KERNEL);
344                                 spin_lock(&dst->page_table_lock);       
345                                 if (!pte_chain)
346                                         goto nomem;
347                                 spin_lock(&src->page_table_lock);
348                                 dst_pte = pte_offset_map(dst_pmd, address);
349                                 src_pte = pte_offset_map_nested(src_pmd,
350                                                                 address);
351 cont_copy_pte_range_noset:
352                                 address += PAGE_SIZE;
353                                 if (address >= end) {
354                                         pte_unmap_nested(src_pte);
355                                         pte_unmap(dst_pte);
356                                         goto out_unlock;
357                                 }
358                                 src_pte++;
359                                 dst_pte++;
360                         } while ((unsigned long)src_pte & PTE_TABLE_MASK);
361                         pte_unmap_nested(src_pte-1);
362                         pte_unmap(dst_pte-1);
363                         spin_unlock(&src->page_table_lock);
364                 
365 cont_copy_pmd_range:
366                         src_pmd++;
367                         dst_pmd++;
368                 } while ((unsigned long)src_pmd & PMD_TABLE_MASK);
369         }
370 out_unlock:
371         spin_unlock(&src->page_table_lock);
372 out:
373         pte_chain_free(pte_chain);
374         return 0;
375 nomem:
376         pte_chain_free(pte_chain);
377         return -ENOMEM;
378 }
379
380 static void
381 zap_pte_range(struct mmu_gather *tlb, pmd_t * pmd,
382                 unsigned long address, unsigned long size)
383 {
384         unsigned long offset;
385         pte_t *ptep;
386
387         if (pmd_none(*pmd))
388                 return;
389         if (pmd_bad(*pmd)) {
390                 pmd_ERROR(*pmd);
391                 pmd_clear(pmd);
392                 return;
393         }
394         ptep = pte_offset_map(pmd, address);
395         offset = address & ~PMD_MASK;
396         if (offset + size > PMD_SIZE)
397                 size = PMD_SIZE - offset;
398         size &= PAGE_MASK;
399         for (offset=0; offset < size; ptep++, offset += PAGE_SIZE) {
400                 pte_t pte = *ptep;
401                 if (pte_none(pte))
402                         continue;
403                 if (pte_present(pte)) {
404                         unsigned long pfn = pte_pfn(pte);
405
406                         pte = ptep_get_and_clear(ptep);
407                         tlb_remove_tlb_entry(tlb, ptep, address+offset);
408                         if (pfn_valid(pfn)) {
409                                 struct page *page = pfn_to_page(pfn);
410                                 if (!PageReserved(page)) {
411                                         if (pte_dirty(pte))
412                                                 set_page_dirty(page);
413                                         if (page->mapping && pte_young(pte) &&
414                                                         !PageSwapCache(page))
415                                                 mark_page_accessed(page);
416                                         tlb->freed++;
417                                         page_remove_rmap(page, ptep);
418                                         tlb_remove_page(tlb, page);
419                                 }
420                         }
421                 } else {
422                         if (!pte_file(pte))
423                                 free_swap_and_cache(pte_to_swp_entry(pte));
424                         pte_clear(ptep);
425                 }
426         }
427         pte_unmap(ptep-1);
428 }
429
430 static void
431 zap_pmd_range(struct mmu_gather *tlb, pgd_t * dir,
432                 unsigned long address, unsigned long size)
433 {
434         pmd_t * pmd;
435         unsigned long end;
436
437         if (pgd_none(*dir))
438                 return;
439         if (pgd_bad(*dir)) {
440                 pgd_ERROR(*dir);
441                 pgd_clear(dir);
442                 return;
443         }
444         pmd = pmd_offset(dir, address);
445         end = address + size;
446         if (end > ((address + PGDIR_SIZE) & PGDIR_MASK))
447                 end = ((address + PGDIR_SIZE) & PGDIR_MASK);
448         do {
449                 zap_pte_range(tlb, pmd, address, end - address);
450                 address = (address + PMD_SIZE) & PMD_MASK; 
451                 pmd++;
452         } while (address < end);
453 }
454
455 void unmap_page_range(struct mmu_gather *tlb, struct vm_area_struct *vma,
456                         unsigned long address, unsigned long end)
457 {
458         pgd_t * dir;
459
460         if (is_vm_hugetlb_page(vma)) {
461                 unmap_hugepage_range(vma, address, end);
462                 return;
463         }
464
465         BUG_ON(address >= end);
466
467         dir = pgd_offset(vma->vm_mm, address);
468         tlb_start_vma(tlb, vma);
469         do {
470                 zap_pmd_range(tlb, dir, address, end - address);
471                 address = (address + PGDIR_SIZE) & PGDIR_MASK;
472                 dir++;
473         } while (address && (address < end));
474         tlb_end_vma(tlb, vma);
475 }
476
477 /* Dispose of an entire struct mmu_gather per rescheduling point */
478 #if defined(CONFIG_SMP) && defined(CONFIG_PREEMPT)
479 #define ZAP_BLOCK_SIZE  (FREE_PTE_NR * PAGE_SIZE)
480 #endif
481
482 /* For UP, 256 pages at a time gives nice low latency */
483 #if !defined(CONFIG_SMP) && defined(CONFIG_PREEMPT)
484 #define ZAP_BLOCK_SIZE  (256 * PAGE_SIZE)
485 #endif
486
487 /* No preempt: go for the best straight-line efficiency */
488 #if !defined(CONFIG_PREEMPT)
489 #define ZAP_BLOCK_SIZE  (~(0UL))
490 #endif
491
492 /**
493  * unmap_vmas - unmap a range of memory covered by a list of vma's
494  * @tlbp: address of the caller's struct mmu_gather
495  * @mm: the controlling mm_struct
496  * @vma: the starting vma
497  * @start_addr: virtual address at which to start unmapping
498  * @end_addr: virtual address at which to end unmapping
499  * @nr_accounted: Place number of unmapped pages in vm-accountable vma's here
500  *
501  * Returns the number of vma's which were covered by the unmapping.
502  *
503  * Unmap all pages in the vma list.  Called under page_table_lock.
504  *
505  * We aim to not hold page_table_lock for too long (for scheduling latency
506  * reasons).  So zap pages in ZAP_BLOCK_SIZE bytecounts.  This means we need to
507  * return the ending mmu_gather to the caller.
508  *
509  * Only addresses between `start' and `end' will be unmapped.
510  *
511  * The VMA list must be sorted in ascending virtual address order.
512  *
513  * unmap_vmas() assumes that the caller will flush the whole unmapped address
514  * range after unmap_vmas() returns.  So the only responsibility here is to
515  * ensure that any thus-far unmapped pages are flushed before unmap_vmas()
516  * drops the lock and schedules.
517  */
518 int unmap_vmas(struct mmu_gather **tlbp, struct mm_struct *mm,
519                 struct vm_area_struct *vma, unsigned long start_addr,
520                 unsigned long end_addr, unsigned long *nr_accounted)
521 {
522         unsigned long zap_bytes = ZAP_BLOCK_SIZE;
523         unsigned long tlb_start;        /* For tlb_finish_mmu */
524         int tlb_start_valid = 0;
525         int ret = 0;
526
527         if (vma) {      /* debug.  killme. */
528                 if (end_addr <= vma->vm_start)
529                         printk("%s: end_addr(0x%08lx) <= vm_start(0x%08lx)\n",
530                                 __FUNCTION__, end_addr, vma->vm_start);
531                 if (start_addr >= vma->vm_end)
532                         printk("%s: start_addr(0x%08lx) <= vm_end(0x%08lx)\n",
533                                 __FUNCTION__, start_addr, vma->vm_end);
534         }
535
536         for ( ; vma && vma->vm_start < end_addr; vma = vma->vm_next) {
537                 unsigned long start;
538                 unsigned long end;
539
540                 start = max(vma->vm_start, start_addr);
541                 if (start >= vma->vm_end)
542                         continue;
543                 end = min(vma->vm_end, end_addr);
544                 if (end <= vma->vm_start)
545                         continue;
546
547                 if (vma->vm_flags & VM_ACCOUNT)
548                         *nr_accounted += (end - start) >> PAGE_SHIFT;
549
550                 ret++;
551                 while (start != end) {
552                         unsigned long block;
553
554                         if (is_vm_hugetlb_page(vma))
555                                 block = end - start;
556                         else
557                                 block = min(zap_bytes, end - start);
558
559                         if (!tlb_start_valid) {
560                                 tlb_start = start;
561                                 tlb_start_valid = 1;
562                         }
563
564                         unmap_page_range(*tlbp, vma, start, start + block);
565                         start += block;
566                         zap_bytes -= block;
567                         if ((long)zap_bytes > 0)
568                                 continue;
569                         if (need_resched()) {
570                                 tlb_finish_mmu(*tlbp, tlb_start, start);
571                                 cond_resched_lock(&mm->page_table_lock);
572                                 *tlbp = tlb_gather_mmu(mm, 0);
573                                 tlb_start_valid = 0;
574                         }
575                         zap_bytes = ZAP_BLOCK_SIZE;
576                 }
577                 if (vma->vm_next && vma->vm_next->vm_start < vma->vm_end)
578                         printk("%s: VMA list is not sorted correctly!\n",
579                                 __FUNCTION__);          
580         }
581         return ret;
582 }
583
584 /**
585  * zap_page_range - remove user pages in a given range
586  * @vma: vm_area_struct holding the applicable pages
587  * @address: starting address of pages to zap
588  * @size: number of bytes to zap
589  */
590 void zap_page_range(struct vm_area_struct *vma,
591                         unsigned long address, unsigned long size)
592 {
593         struct mm_struct *mm = vma->vm_mm;
594         struct mmu_gather *tlb;
595         unsigned long end = address + size;
596         unsigned long nr_accounted = 0;
597
598         might_sleep();
599
600         if (is_vm_hugetlb_page(vma)) {
601                 zap_hugepage_range(vma, address, size);
602                 return;
603         }
604
605         lru_add_drain();
606         spin_lock(&mm->page_table_lock);
607         tlb = tlb_gather_mmu(mm, 0);
608         unmap_vmas(&tlb, mm, vma, address, end, &nr_accounted);
609         tlb_finish_mmu(tlb, address, end);
610         spin_unlock(&mm->page_table_lock);
611 }
612
613 /*
614  * Do a quick page-table lookup for a single page.
615  * mm->page_table_lock must be held.
616  */
617 struct page *
618 follow_page(struct mm_struct *mm, unsigned long address, int write) 
619 {
620         pgd_t *pgd;
621         pmd_t *pmd;
622         pte_t *ptep, pte;
623         unsigned long pfn;
624         struct vm_area_struct *vma;
625
626         vma = hugepage_vma(mm, address);
627         if (vma)
628                 return follow_huge_addr(mm, vma, address, write);
629
630         pgd = pgd_offset(mm, address);
631         if (pgd_none(*pgd) || pgd_bad(*pgd))
632                 goto out;
633
634         pmd = pmd_offset(pgd, address);
635         if (pmd_none(*pmd))
636                 goto out;
637         if (pmd_huge(*pmd))
638                 return follow_huge_pmd(mm, address, pmd, write);
639         if (pmd_bad(*pmd))
640                 goto out;
641
642         ptep = pte_offset_map(pmd, address);
643         if (!ptep)
644                 goto out;
645
646         pte = *ptep;
647         pte_unmap(ptep);
648         if (pte_present(pte)) {
649                 if (!write || (pte_write(pte) && pte_dirty(pte))) {
650                         pfn = pte_pfn(pte);
651                         if (pfn_valid(pfn))
652                                 return pfn_to_page(pfn);
653                 }
654         }
655
656 out:
657         return NULL;
658 }
659
660 /* 
661  * Given a physical address, is there a useful struct page pointing to
662  * it?  This may become more complex in the future if we start dealing
663  * with IO-aperture pages for direct-IO.
664  */
665
666 static inline struct page *get_page_map(struct page *page)
667 {
668         if (!pfn_valid(page_to_pfn(page)))
669                 return 0;
670         return page;
671 }
672
673
674 int get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
675                 unsigned long start, int len, int write, int force,
676                 struct page **pages, struct vm_area_struct **vmas)
677 {
678         int i;
679         unsigned int flags;
680
681         /* 
682          * Require read or write permissions.
683          * If 'force' is set, we only require the "MAY" flags.
684          */
685         flags = write ? (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
686         flags &= force ? (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE);
687         i = 0;
688
689         do {
690                 struct vm_area_struct * vma;
691
692                 vma = find_extend_vma(mm, start);
693
694 #ifdef VSYSCALL_START
695                 if (!vma && start >= VSYSCALL_START && start < VSYSCALL_END) {
696                         static struct vm_area_struct fixmap_vma = {
697                                 /* Catch users - if there are any valid
698                                    ones, we can make this be "&init_mm" or
699                                    something.  */
700                                 .vm_mm = NULL,
701                                 .vm_start = FIXADDR_START,
702                                 .vm_end = FIXADDR_TOP,
703                                 .vm_page_prot = PAGE_READONLY,
704                                 .vm_flags = VM_READ | VM_EXEC,
705                         };
706                         unsigned long pg = start & PAGE_MASK;
707                         pgd_t *pgd;
708                         pmd_t *pmd;
709                         pte_t *pte;
710                         pgd = pgd_offset_k(pg);
711                         if (!pgd)
712                                 return i ? : -EFAULT;
713                         pmd = pmd_offset(pgd, pg);
714                         if (!pmd)
715                                 return i ? : -EFAULT;
716                         pte = pte_offset_kernel(pmd, pg);
717                         if (!pte || !pte_present(*pte) || !pte_user(*pte) ||
718                             !(write ? pte_write(*pte) : pte_read(*pte)))
719                                 return i ? : -EFAULT;
720                         if (pages) {
721                                 pages[i] = pte_page(*pte);
722                                 get_page(pages[i]);
723                         }
724                         if (vmas)
725                                 vmas[i] = &fixmap_vma;
726                         i++;
727                         start += PAGE_SIZE;
728                         len--;
729                         continue;
730                 }
731 #endif
732
733                 if (!vma || (pages && (vma->vm_flags & VM_IO))
734                                 || !(flags & vma->vm_flags))
735                         return i ? : -EFAULT;
736
737                 if (is_vm_hugetlb_page(vma)) {
738                         i = follow_hugetlb_page(mm, vma, pages, vmas,
739                                                 &start, &len, i);
740                         continue;
741                 }
742                 spin_lock(&mm->page_table_lock);
743                 do {
744                         struct page *map;
745                         while (!(map = follow_page(mm, start, write))) {
746                                 spin_unlock(&mm->page_table_lock);
747                                 switch (handle_mm_fault(mm,vma,start,write)) {
748                                 case VM_FAULT_MINOR:
749                                         tsk->min_flt++;
750                                         break;
751                                 case VM_FAULT_MAJOR:
752                                         tsk->maj_flt++;
753                                         break;
754                                 case VM_FAULT_SIGBUS:
755                                         return i ? i : -EFAULT;
756                                 case VM_FAULT_OOM:
757                                         return i ? i : -ENOMEM;
758                                 default:
759                                         BUG();
760                                 }
761                                 spin_lock(&mm->page_table_lock);
762                         }
763                         if (pages) {
764                                 pages[i] = get_page_map(map);
765                                 if (!pages[i]) {
766                                         spin_unlock(&mm->page_table_lock);
767                                         while (i--)
768                                                 page_cache_release(pages[i]);
769                                         i = -EFAULT;
770                                         goto out;
771                                 }
772                                 flush_dcache_page(pages[i]);
773                                 if (!PageReserved(pages[i]))
774                                         page_cache_get(pages[i]);
775                         }
776                         if (vmas)
777                                 vmas[i] = vma;
778                         i++;
779                         start += PAGE_SIZE;
780                         len--;
781                 } while(len && start < vma->vm_end);
782                 spin_unlock(&mm->page_table_lock);
783         } while(len);
784 out:
785         return i;
786 }
787
788 static void zeromap_pte_range(pte_t * pte, unsigned long address,
789                                      unsigned long size, pgprot_t prot)
790 {
791         unsigned long end;
792
793         address &= ~PMD_MASK;
794         end = address + size;
795         if (end > PMD_SIZE)
796                 end = PMD_SIZE;
797         do {
798                 pte_t zero_pte = pte_wrprotect(mk_pte(ZERO_PAGE(address), prot));
799                 BUG_ON(!pte_none(*pte));
800                 set_pte(pte, zero_pte);
801                 address += PAGE_SIZE;
802                 pte++;
803         } while (address && (address < end));
804 }
805
806 static inline int zeromap_pmd_range(struct mm_struct *mm, pmd_t * pmd, unsigned long address,
807                                     unsigned long size, pgprot_t prot)
808 {
809         unsigned long end;
810
811         address &= ~PGDIR_MASK;
812         end = address + size;
813         if (end > PGDIR_SIZE)
814                 end = PGDIR_SIZE;
815         do {
816                 pte_t * pte = pte_alloc_map(mm, pmd, address);
817                 if (!pte)
818                         return -ENOMEM;
819                 zeromap_pte_range(pte, address, end - address, prot);
820                 pte_unmap(pte);
821                 address = (address + PMD_SIZE) & PMD_MASK;
822                 pmd++;
823         } while (address && (address < end));
824         return 0;
825 }
826
827 int zeromap_page_range(struct vm_area_struct *vma, unsigned long address, unsigned long size, pgprot_t prot)
828 {
829         int error = 0;
830         pgd_t * dir;
831         unsigned long beg = address;
832         unsigned long end = address + size;
833         struct mm_struct *mm = vma->vm_mm;
834
835         dir = pgd_offset(mm, address);
836         flush_cache_range(vma, beg, end);
837         if (address >= end)
838                 BUG();
839
840         spin_lock(&mm->page_table_lock);
841         do {
842                 pmd_t *pmd = pmd_alloc(mm, dir, address);
843                 error = -ENOMEM;
844                 if (!pmd)
845                         break;
846                 error = zeromap_pmd_range(mm, pmd, address, end - address, prot);
847                 if (error)
848                         break;
849                 address = (address + PGDIR_SIZE) & PGDIR_MASK;
850                 dir++;
851         } while (address && (address < end));
852         flush_tlb_range(vma, beg, end);
853         spin_unlock(&mm->page_table_lock);
854         return error;
855 }
856
857 /*
858  * maps a range of physical memory into the requested pages. the old
859  * mappings are removed. any references to nonexistent pages results
860  * in null mappings (currently treated as "copy-on-access")
861  */
862 static inline void remap_pte_range(pte_t * pte, unsigned long address, unsigned long size,
863         unsigned long phys_addr, pgprot_t prot)
864 {
865         unsigned long end;
866         unsigned long pfn;
867
868         address &= ~PMD_MASK;
869         end = address + size;
870         if (end > PMD_SIZE)
871                 end = PMD_SIZE;
872         pfn = phys_addr >> PAGE_SHIFT;
873         do {
874                 BUG_ON(!pte_none(*pte));
875                 if (!pfn_valid(pfn) || PageReserved(pfn_to_page(pfn)))
876                         set_pte(pte, pfn_pte(pfn, prot));
877                 address += PAGE_SIZE;
878                 pfn++;
879                 pte++;
880         } while (address && (address < end));
881 }
882
883 static inline int remap_pmd_range(struct mm_struct *mm, pmd_t * pmd, unsigned long address, unsigned long size,
884         unsigned long phys_addr, pgprot_t prot)
885 {
886         unsigned long base, end;
887
888         base = address & PGDIR_MASK;
889         address &= ~PGDIR_MASK;
890         end = address + size;
891         if (end > PGDIR_SIZE)
892                 end = PGDIR_SIZE;
893         phys_addr -= address;
894         do {
895                 pte_t * pte = pte_alloc_map(mm, pmd, base + address);
896                 if (!pte)
897                         return -ENOMEM;
898                 remap_pte_range(pte, base + address, end - address, address + phys_addr, prot);
899                 pte_unmap(pte);
900                 address = (address + PMD_SIZE) & PMD_MASK;
901                 pmd++;
902         } while (address && (address < end));
903         return 0;
904 }
905
906 /*  Note: this is only safe if the mm semaphore is held when called. */
907 int remap_page_range(struct vm_area_struct *vma, unsigned long from, unsigned long phys_addr, unsigned long size, pgprot_t prot)
908 {
909         int error = 0;
910         pgd_t * dir;
911         unsigned long beg = from;
912         unsigned long end = from + size;
913         struct mm_struct *mm = vma->vm_mm;
914
915         phys_addr -= from;
916         dir = pgd_offset(mm, from);
917         flush_cache_range(vma, beg, end);
918         if (from >= end)
919                 BUG();
920
921         spin_lock(&mm->page_table_lock);
922         do {
923                 pmd_t *pmd = pmd_alloc(mm, dir, from);
924                 error = -ENOMEM;
925                 if (!pmd)
926                         break;
927                 error = remap_pmd_range(mm, pmd, from, end - from, phys_addr + from, prot);
928                 if (error)
929                         break;
930                 from = (from + PGDIR_SIZE) & PGDIR_MASK;
931                 dir++;
932         } while (from && (from < end));
933         flush_tlb_range(vma, beg, end);
934         spin_unlock(&mm->page_table_lock);
935         return error;
936 }
937
938 /*
939  * Establish a new mapping:
940  *  - flush the old one
941  *  - update the page tables
942  *  - inform the TLB about the new one
943  *
944  * We hold the mm semaphore for reading and vma->vm_mm->page_table_lock
945  */
946 static inline void establish_pte(struct vm_area_struct * vma, unsigned long address, pte_t *page_table, pte_t entry)
947 {
948         set_pte(page_table, entry);
949         flush_tlb_page(vma, address);
950         update_mmu_cache(vma, address, entry);
951 }
952
953 /*
954  * We hold the mm semaphore for reading and vma->vm_mm->page_table_lock
955  */
956 static inline void break_cow(struct vm_area_struct * vma, struct page * new_page, unsigned long address, 
957                 pte_t *page_table)
958 {
959         invalidate_vcache(address, vma->vm_mm, new_page);
960         flush_cache_page(vma, address);
961         establish_pte(vma, address, page_table, pte_mkwrite(pte_mkdirty(mk_pte(new_page, vma->vm_page_prot))));
962 }
963
964 /*
965  * This routine handles present pages, when users try to write
966  * to a shared page. It is done by copying the page to a new address
967  * and decrementing the shared-page counter for the old page.
968  *
969  * Goto-purists beware: the only reason for goto's here is that it results
970  * in better assembly code.. The "default" path will see no jumps at all.
971  *
972  * Note that this routine assumes that the protection checks have been
973  * done by the caller (the low-level page fault routine in most cases).
974  * Thus we can safely just mark it writable once we've done any necessary
975  * COW.
976  *
977  * We also mark the page dirty at this point even though the page will
978  * change only once the write actually happens. This avoids a few races,
979  * and potentially makes it more efficient.
980  *
981  * We hold the mm semaphore and the page_table_lock on entry and exit
982  * with the page_table_lock released.
983  */
984 static int do_wp_page(struct mm_struct *mm, struct vm_area_struct * vma,
985         unsigned long address, pte_t *page_table, pmd_t *pmd, pte_t pte)
986 {
987         struct page *old_page, *new_page;
988         unsigned long pfn = pte_pfn(pte);
989         struct pte_chain *pte_chain = NULL;
990         int ret;
991
992         if (unlikely(!pfn_valid(pfn))) {
993                 /*
994                  * This should really halt the system so it can be debugged or
995                  * at least the kernel stops what it's doing before it corrupts
996                  * data, but for the moment just pretend this is OOM.
997                  */
998                 pte_unmap(page_table);
999                 printk(KERN_ERR "do_wp_page: bogus page at address %08lx\n",
1000                                 address);
1001                 goto oom;
1002         }
1003         old_page = pfn_to_page(pfn);
1004
1005         if (!TestSetPageLocked(old_page)) {
1006                 int reuse = can_share_swap_page(old_page);
1007                 unlock_page(old_page);
1008                 if (reuse) {
1009                         flush_cache_page(vma, address);
1010                         establish_pte(vma, address, page_table,
1011                                 pte_mkyoung(pte_mkdirty(pte_mkwrite(pte))));
1012                         pte_unmap(page_table);
1013                         ret = VM_FAULT_MINOR;
1014                         goto out;
1015                 }
1016         }
1017         pte_unmap(page_table);
1018
1019         /*
1020          * Ok, we need to copy. Oh, well..
1021          */
1022         page_cache_get(old_page);
1023         spin_unlock(&mm->page_table_lock);
1024
1025         pte_chain = pte_chain_alloc(GFP_KERNEL);
1026         if (!pte_chain)
1027                 goto no_mem;
1028         new_page = alloc_page(GFP_HIGHUSER);
1029         if (!new_page)
1030                 goto no_mem;
1031         copy_cow_page(old_page,new_page,address);
1032
1033         /*
1034          * Re-check the pte - we dropped the lock
1035          */
1036         spin_lock(&mm->page_table_lock);
1037         page_table = pte_offset_map(pmd, address);
1038         if (pte_same(*page_table, pte)) {
1039                 if (PageReserved(old_page))
1040                         ++mm->rss;
1041                 page_remove_rmap(old_page, page_table);
1042                 break_cow(vma, new_page, address, page_table);
1043                 pte_chain = page_add_rmap(new_page, page_table, pte_chain);
1044                 lru_cache_add_active(new_page);
1045
1046                 /* Free the old page.. */
1047                 new_page = old_page;
1048         }
1049         pte_unmap(page_table);
1050         page_cache_release(new_page);
1051         page_cache_release(old_page);
1052         ret = VM_FAULT_MINOR;
1053         goto out;
1054
1055 no_mem:
1056         page_cache_release(old_page);
1057 oom:
1058         ret = VM_FAULT_OOM;
1059 out:
1060         spin_unlock(&mm->page_table_lock);
1061         pte_chain_free(pte_chain);
1062         return ret;
1063 }
1064
1065 static void vmtruncate_list(struct list_head *head, unsigned long pgoff)
1066 {
1067         unsigned long start, end, len, diff;
1068         struct vm_area_struct *vma;
1069         struct list_head *curr;
1070
1071         list_for_each(curr, head) {
1072                 vma = list_entry(curr, struct vm_area_struct, shared);
1073                 start = vma->vm_start;
1074                 end = vma->vm_end;
1075                 len = end - start;
1076
1077                 /* mapping wholly truncated? */
1078                 if (vma->vm_pgoff >= pgoff) {
1079                         zap_page_range(vma, start, len);
1080                         continue;
1081                 }
1082
1083                 /* mapping wholly unaffected? */
1084                 len = len >> PAGE_SHIFT;
1085                 diff = pgoff - vma->vm_pgoff;
1086                 if (diff >= len)
1087                         continue;
1088
1089                 /* Ok, partially affected.. */
1090                 start += diff << PAGE_SHIFT;
1091                 len = (len - diff) << PAGE_SHIFT;
1092                 zap_page_range(vma, start, len);
1093         }
1094 }
1095
1096 /*
1097  * Handle all mappings that got truncated by a "truncate()"
1098  * system call.
1099  *
1100  * NOTE! We have to be ready to update the memory sharing
1101  * between the file and the memory map for a potential last
1102  * incomplete page.  Ugly, but necessary.
1103  */
1104 int vmtruncate(struct inode * inode, loff_t offset)
1105 {
1106         unsigned long pgoff;
1107         struct address_space *mapping = inode->i_mapping;
1108         unsigned long limit;
1109
1110         if (inode->i_size < offset)
1111                 goto do_expand;
1112         inode->i_size = offset;
1113         down(&mapping->i_shared_sem);
1114         if (list_empty(&mapping->i_mmap) && list_empty(&mapping->i_mmap_shared))
1115                 goto out_unlock;
1116
1117         pgoff = (offset + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1118         if (!list_empty(&mapping->i_mmap))
1119                 vmtruncate_list(&mapping->i_mmap, pgoff);
1120         if (!list_empty(&mapping->i_mmap_shared))
1121                 vmtruncate_list(&mapping->i_mmap_shared, pgoff);
1122
1123 out_unlock:
1124         up(&mapping->i_shared_sem);
1125         truncate_inode_pages(mapping, offset);
1126         goto out_truncate;
1127
1128 do_expand:
1129         limit = current->rlim[RLIMIT_FSIZE].rlim_cur;
1130         if (limit != RLIM_INFINITY && offset > limit)
1131                 goto out_sig;
1132         if (offset > inode->i_sb->s_maxbytes)
1133                 goto out;
1134         inode->i_size = offset;
1135
1136 out_truncate:
1137         if (inode->i_op && inode->i_op->truncate)
1138                 inode->i_op->truncate(inode);
1139         return 0;
1140 out_sig:
1141         send_sig(SIGXFSZ, current, 0);
1142 out:
1143         return -EFBIG;
1144 }
1145
1146 /* 
1147  * Primitive swap readahead code. We simply read an aligned block of
1148  * (1 << page_cluster) entries in the swap area. This method is chosen
1149  * because it doesn't cost us any seek time.  We also make sure to queue
1150  * the 'original' request together with the readahead ones...  
1151  */
1152 void swapin_readahead(swp_entry_t entry)
1153 {
1154         int i, num;
1155         struct page *new_page;
1156         unsigned long offset;
1157
1158         /*
1159          * Get the number of handles we should do readahead io to.
1160          */
1161         num = valid_swaphandles(entry, &offset);
1162         for (i = 0; i < num; offset++, i++) {
1163                 /* Ok, do the async read-ahead now */
1164                 new_page = read_swap_cache_async(swp_entry(swp_type(entry),
1165                                                 offset));
1166                 if (!new_page)
1167                         break;
1168                 page_cache_release(new_page);
1169         }
1170         lru_add_drain();        /* Push any new pages onto the LRU now */
1171 }
1172
1173 /*
1174  * We hold the mm semaphore and the page_table_lock on entry and
1175  * should release the pagetable lock on exit..
1176  */
1177 static int do_swap_page(struct mm_struct * mm,
1178         struct vm_area_struct * vma, unsigned long address,
1179         pte_t *page_table, pmd_t *pmd, pte_t orig_pte, int write_access)
1180 {
1181         struct page *page;
1182         swp_entry_t entry = pte_to_swp_entry(orig_pte);
1183         pte_t pte;
1184         int ret = VM_FAULT_MINOR;
1185         struct pte_chain *pte_chain = NULL;
1186
1187         pte_unmap(page_table);
1188         spin_unlock(&mm->page_table_lock);
1189         page = lookup_swap_cache(entry);
1190         if (!page) {
1191                 swapin_readahead(entry);
1192                 page = read_swap_cache_async(entry);
1193                 if (!page) {
1194                         /*
1195                          * Back out if somebody else faulted in this pte while
1196                          * we released the page table lock.
1197                          */
1198                         spin_lock(&mm->page_table_lock);
1199                         page_table = pte_offset_map(pmd, address);
1200                         if (pte_same(*page_table, orig_pte))
1201                                 ret = VM_FAULT_OOM;
1202                         else
1203                                 ret = VM_FAULT_MINOR;
1204                         pte_unmap(page_table);
1205                         spin_unlock(&mm->page_table_lock);
1206                         goto out;
1207                 }
1208
1209                 /* Had to read the page from swap area: Major fault */
1210                 ret = VM_FAULT_MAJOR;
1211                 inc_page_state(pgmajfault);
1212         }
1213
1214         mark_page_accessed(page);
1215         pte_chain = pte_chain_alloc(GFP_KERNEL);
1216         if (!pte_chain) {
1217                 ret = -ENOMEM;
1218                 goto out;
1219         }
1220         lock_page(page);
1221
1222         /*
1223          * Back out if somebody else faulted in this pte while we
1224          * released the page table lock.
1225          */
1226         spin_lock(&mm->page_table_lock);
1227         page_table = pte_offset_map(pmd, address);
1228         if (!pte_same(*page_table, orig_pte)) {
1229                 pte_unmap(page_table);
1230                 spin_unlock(&mm->page_table_lock);
1231                 unlock_page(page);
1232                 page_cache_release(page);
1233                 ret = VM_FAULT_MINOR;
1234                 goto out;
1235         }
1236
1237         /* The page isn't present yet, go ahead with the fault. */
1238                 
1239         swap_free(entry);
1240         if (vm_swap_full())
1241                 remove_exclusive_swap_page(page);
1242
1243         mm->rss++;
1244         pte = mk_pte(page, vma->vm_page_prot);
1245         if (write_access && can_share_swap_page(page))
1246                 pte = pte_mkdirty(pte_mkwrite(pte));
1247         unlock_page(page);
1248
1249         flush_icache_page(vma, page);
1250         set_pte(page_table, pte);
1251         pte_chain = page_add_rmap(page, page_table, pte_chain);
1252
1253         /* No need to invalidate - it was non-present before */
1254         update_mmu_cache(vma, address, pte);
1255         pte_unmap(page_table);
1256         spin_unlock(&mm->page_table_lock);
1257 out:
1258         pte_chain_free(pte_chain);
1259         return ret;
1260 }
1261
1262 /*
1263  * We are called with the MM semaphore and page_table_lock
1264  * spinlock held to protect against concurrent faults in
1265  * multithreaded programs. 
1266  */
1267 static int
1268 do_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma,
1269                 pte_t *page_table, pmd_t *pmd, int write_access,
1270                 unsigned long addr)
1271 {
1272         pte_t entry;
1273         struct page * page = ZERO_PAGE(addr);
1274         struct pte_chain *pte_chain;
1275         int ret;
1276
1277         pte_chain = pte_chain_alloc(GFP_ATOMIC);
1278         if (!pte_chain) {
1279                 pte_unmap(page_table);
1280                 spin_unlock(&mm->page_table_lock);
1281                 pte_chain = pte_chain_alloc(GFP_KERNEL);
1282                 if (!pte_chain)
1283                         goto no_mem;
1284                 spin_lock(&mm->page_table_lock);
1285                 page_table = pte_offset_map(pmd, addr);
1286         }
1287                 
1288         /* Read-only mapping of ZERO_PAGE. */
1289         entry = pte_wrprotect(mk_pte(ZERO_PAGE(addr), vma->vm_page_prot));
1290
1291         /* ..except if it's a write access */
1292         if (write_access) {
1293                 /* Allocate our own private page. */
1294                 pte_unmap(page_table);
1295                 spin_unlock(&mm->page_table_lock);
1296
1297                 page = alloc_page(GFP_HIGHUSER);
1298                 if (!page)
1299                         goto no_mem;
1300                 clear_user_highpage(page, addr);
1301
1302                 spin_lock(&mm->page_table_lock);
1303                 page_table = pte_offset_map(pmd, addr);
1304
1305                 if (!pte_none(*page_table)) {
1306                         pte_unmap(page_table);
1307                         page_cache_release(page);
1308                         spin_unlock(&mm->page_table_lock);
1309                         ret = VM_FAULT_MINOR;
1310                         goto out;
1311                 }
1312                 mm->rss++;
1313                 entry = pte_mkwrite(pte_mkdirty(mk_pte(page, vma->vm_page_prot)));
1314                 lru_cache_add_active(page);
1315                 mark_page_accessed(page);
1316         }
1317
1318         set_pte(page_table, entry);
1319         /* ignores ZERO_PAGE */
1320         pte_chain = page_add_rmap(page, page_table, pte_chain);
1321         pte_unmap(page_table);
1322
1323         /* No need to invalidate - it was non-present before */
1324         update_mmu_cache(vma, addr, entry);
1325         spin_unlock(&mm->page_table_lock);
1326         ret = VM_FAULT_MINOR;
1327         goto out;
1328
1329 no_mem:
1330         ret = VM_FAULT_OOM;
1331 out:
1332         pte_chain_free(pte_chain);
1333         return ret;
1334 }
1335
1336 /*
1337  * do_no_page() tries to create a new page mapping. It aggressively
1338  * tries to share with existing pages, but makes a separate copy if
1339  * the "write_access" parameter is true in order to avoid the next
1340  * page fault.
1341  *
1342  * As this is called only for pages that do not currently exist, we
1343  * do not need to flush old virtual caches or the TLB.
1344  *
1345  * This is called with the MM semaphore held and the page table
1346  * spinlock held. Exit with the spinlock released.
1347  */
1348 static int
1349 do_no_page(struct mm_struct *mm, struct vm_area_struct *vma,
1350         unsigned long address, int write_access, pte_t *page_table, pmd_t *pmd)
1351 {
1352         struct page * new_page;
1353         pte_t entry;
1354         struct pte_chain *pte_chain;
1355         int ret;
1356
1357         if (!vma->vm_ops || !vma->vm_ops->nopage)
1358                 return do_anonymous_page(mm, vma, page_table,
1359                                         pmd, write_access, address);
1360         pte_unmap(page_table);
1361         spin_unlock(&mm->page_table_lock);
1362
1363         new_page = vma->vm_ops->nopage(vma, address & PAGE_MASK, 0);
1364
1365         /* no page was available -- either SIGBUS or OOM */
1366         if (new_page == NOPAGE_SIGBUS)
1367                 return VM_FAULT_SIGBUS;
1368         if (new_page == NOPAGE_OOM)
1369                 return VM_FAULT_OOM;
1370
1371         pte_chain = pte_chain_alloc(GFP_KERNEL);
1372         if (!pte_chain)
1373                 goto oom;
1374
1375         /*
1376          * Should we do an early C-O-W break?
1377          */
1378         if (write_access && !(vma->vm_flags & VM_SHARED)) {
1379                 struct page * page = alloc_page(GFP_HIGHUSER);
1380                 if (!page) {
1381                         page_cache_release(new_page);
1382                         goto oom;
1383                 }
1384                 copy_user_highpage(page, new_page, address);
1385                 page_cache_release(new_page);
1386                 lru_cache_add_active(page);
1387                 new_page = page;
1388         }
1389
1390         spin_lock(&mm->page_table_lock);
1391         page_table = pte_offset_map(pmd, address);
1392
1393         /*
1394          * This silly early PAGE_DIRTY setting removes a race
1395          * due to the bad i386 page protection. But it's valid
1396          * for other architectures too.
1397          *
1398          * Note that if write_access is true, we either now have
1399          * an exclusive copy of the page, or this is a shared mapping,
1400          * so we can make it writable and dirty to avoid having to
1401          * handle that later.
1402          */
1403         /* Only go through if we didn't race with anybody else... */
1404         if (pte_none(*page_table)) {
1405                 ++mm->rss;
1406                 flush_icache_page(vma, new_page);
1407                 entry = mk_pte(new_page, vma->vm_page_prot);
1408                 if (write_access)
1409                         entry = pte_mkwrite(pte_mkdirty(entry));
1410                 set_pte(page_table, entry);
1411                 pte_chain = page_add_rmap(new_page, page_table, pte_chain);
1412                 pte_unmap(page_table);
1413         } else {
1414                 /* One of our sibling threads was faster, back out. */
1415                 pte_unmap(page_table);
1416                 page_cache_release(new_page);
1417                 spin_unlock(&mm->page_table_lock);
1418                 ret = VM_FAULT_MINOR;
1419                 goto out;
1420         }
1421
1422         /* no need to invalidate: a not-present page shouldn't be cached */
1423         update_mmu_cache(vma, address, entry);
1424         spin_unlock(&mm->page_table_lock);
1425         ret = VM_FAULT_MAJOR;
1426         goto out;
1427 oom:
1428         ret = VM_FAULT_OOM;
1429 out:
1430         pte_chain_free(pte_chain);
1431         return ret;
1432 }
1433
1434 /*
1435  * Fault of a previously existing named mapping. Repopulate the pte
1436  * from the encoded file_pte if possible. This enables swappable
1437  * nonlinear vmas.
1438  */
1439 static int do_file_page(struct mm_struct * mm, struct vm_area_struct * vma,
1440         unsigned long address, int write_access, pte_t *pte, pmd_t *pmd)
1441 {
1442         unsigned long pgoff;
1443         int err;
1444
1445         BUG_ON(!vma->vm_ops || !vma->vm_ops->nopage);
1446         /*
1447          * Fall back to the linear mapping if the fs does not support
1448          * ->populate:
1449          */
1450         if (!vma->vm_ops || !vma->vm_ops->populate || 
1451                         (write_access && !(vma->vm_flags & VM_SHARED))) {
1452                 pte_clear(pte);
1453                 return do_no_page(mm, vma, address, write_access, pte, pmd);
1454         }
1455
1456         pgoff = pte_to_pgoff(*pte);
1457
1458         pte_unmap(pte);
1459         spin_unlock(&mm->page_table_lock);
1460
1461         err = vma->vm_ops->populate(vma, address & PAGE_MASK, PAGE_SIZE, vma->vm_page_prot, pgoff, 0);
1462         if (err == -ENOMEM)
1463                 return VM_FAULT_OOM;
1464         if (err)
1465                 return VM_FAULT_SIGBUS;
1466         return VM_FAULT_MAJOR;
1467 }
1468
1469 /*
1470  * These routines also need to handle stuff like marking pages dirty
1471  * and/or accessed for architectures that don't do it in hardware (most
1472  * RISC architectures).  The early dirtying is also good on the i386.
1473  *
1474  * There is also a hook called "update_mmu_cache()" that architectures
1475  * with external mmu caches can use to update those (ie the Sparc or
1476  * PowerPC hashed page tables that act as extended TLBs).
1477  *
1478  * Note the "page_table_lock". It is to protect against kswapd removing
1479  * pages from under us. Note that kswapd only ever _removes_ pages, never
1480  * adds them. As such, once we have noticed that the page is not present,
1481  * we can drop the lock early.
1482  *
1483  * The adding of pages is protected by the MM semaphore (which we hold),
1484  * so we don't need to worry about a page being suddenly been added into
1485  * our VM.
1486  *
1487  * We enter with the pagetable spinlock held, we are supposed to
1488  * release it when done.
1489  */
1490 static inline int handle_pte_fault(struct mm_struct *mm,
1491         struct vm_area_struct * vma, unsigned long address,
1492         int write_access, pte_t *pte, pmd_t *pmd)
1493 {
1494         pte_t entry;
1495
1496         entry = *pte;
1497         if (!pte_present(entry)) {
1498                 /*
1499                  * If it truly wasn't present, we know that kswapd
1500                  * and the PTE updates will not touch it later. So
1501                  * drop the lock.
1502                  */
1503                 if (pte_none(entry))
1504                         return do_no_page(mm, vma, address, write_access, pte, pmd);
1505                 if (pte_file(entry))
1506                         return do_file_page(mm, vma, address, write_access, pte, pmd);
1507                 return do_swap_page(mm, vma, address, pte, pmd, entry, write_access);
1508         }
1509
1510         if (write_access) {
1511                 if (!pte_write(entry))
1512                         return do_wp_page(mm, vma, address, pte, pmd, entry);
1513
1514                 entry = pte_mkdirty(entry);
1515         }
1516         entry = pte_mkyoung(entry);
1517         establish_pte(vma, address, pte, entry);
1518         pte_unmap(pte);
1519         spin_unlock(&mm->page_table_lock);
1520         return VM_FAULT_MINOR;
1521 }
1522
1523 /*
1524  * By the time we get here, we already hold the mm semaphore
1525  */
1526 int handle_mm_fault(struct mm_struct *mm, struct vm_area_struct * vma,
1527         unsigned long address, int write_access)
1528 {
1529         pgd_t *pgd;
1530         pmd_t *pmd;
1531
1532         __set_current_state(TASK_RUNNING);
1533         pgd = pgd_offset(mm, address);
1534
1535         inc_page_state(pgfault);
1536
1537         if (is_vm_hugetlb_page(vma))
1538                 return VM_FAULT_SIGBUS; /* mapping truncation does this. */
1539
1540         /*
1541          * We need the page table lock to synchronize with kswapd
1542          * and the SMP-safe atomic PTE updates.
1543          */
1544         spin_lock(&mm->page_table_lock);
1545         pmd = pmd_alloc(mm, pgd, address);
1546
1547         if (pmd) {
1548                 pte_t * pte = pte_alloc_map(mm, pmd, address);
1549                 if (pte)
1550                         return handle_pte_fault(mm, vma, address, write_access, pte, pmd);
1551         }
1552         spin_unlock(&mm->page_table_lock);
1553         return VM_FAULT_OOM;
1554 }
1555
1556 /*
1557  * Allocate page middle directory.
1558  *
1559  * We've already handled the fast-path in-line, and we own the
1560  * page table lock.
1561  *
1562  * On a two-level page table, this ends up actually being entirely
1563  * optimized away.
1564  */
1565 pmd_t *__pmd_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address)
1566 {
1567         pmd_t *new;
1568
1569         spin_unlock(&mm->page_table_lock);
1570         new = pmd_alloc_one(mm, address);
1571         spin_lock(&mm->page_table_lock);
1572         if (!new)
1573                 return NULL;
1574
1575         /*
1576          * Because we dropped the lock, we should re-check the
1577          * entry, as somebody else could have populated it..
1578          */
1579         if (pgd_present(*pgd)) {
1580                 pmd_free(new);
1581                 goto out;
1582         }
1583         pgd_populate(mm, pgd, new);
1584 out:
1585         return pmd_offset(pgd, address);
1586 }
1587
1588 int make_pages_present(unsigned long addr, unsigned long end)
1589 {
1590         int ret, len, write;
1591         struct vm_area_struct * vma;
1592
1593         vma = find_vma(current->mm, addr);
1594         write = (vma->vm_flags & VM_WRITE) != 0;
1595         if (addr >= end)
1596                 BUG();
1597         if (end > vma->vm_end)
1598                 BUG();
1599         len = (end+PAGE_SIZE-1)/PAGE_SIZE-addr/PAGE_SIZE;
1600         ret = get_user_pages(current, current->mm, addr,
1601                         len, write, 0, NULL, NULL);
1602         return ret == len ? 0 : -1;
1603 }
1604
1605 /* 
1606  * Map a vmalloc()-space virtual address to the physical page.
1607  */
1608 struct page * vmalloc_to_page(void * vmalloc_addr)
1609 {
1610         unsigned long addr = (unsigned long) vmalloc_addr;
1611         struct page *page = NULL;
1612         pgd_t *pgd = pgd_offset_k(addr);
1613         pmd_t *pmd;
1614         pte_t *ptep, pte;
1615   
1616         if (!pgd_none(*pgd)) {
1617                 pmd = pmd_offset(pgd, addr);
1618                 if (!pmd_none(*pmd)) {
1619                         preempt_disable();
1620                         ptep = pte_offset_map(pmd, addr);
1621                         pte = *ptep;
1622                         if (pte_present(pte))
1623                                 page = pte_page(pte);
1624                         pte_unmap(ptep);
1625                         preempt_enable();
1626                 }
1627         }
1628         return page;
1629 }