debuggers.hg

view xenolinux-2.4.21-sparse/mm/memory.c @ 666:6d07235a19e8

bitkeeper revision 1.339.1.12 (3f132a3btAOPZiDtrKz16GwA9Wr_ow)

memory.c, fault.c, i386_ksyms.c:
Simplified Xenolinux mm code by removing some 386-only functionality.
author kaf24@scramble.cl.cam.ac.uk
date Mon Jul 14 22:10:03 2003 +0000 (2003-07-14)
parents 9339f3942f4e
children 4fd1861ec41a
line source
1 /*
2 * linux/mm/memory.c
3 *
4 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
5 */
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 */
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 */
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 */
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 */
39 #include <linux/mm.h>
40 #include <linux/mman.h>
41 #include <linux/swap.h>
42 #include <linux/smp_lock.h>
43 #include <linux/swapctl.h>
44 #include <linux/iobuf.h>
45 #include <linux/highmem.h>
46 #include <linux/pagemap.h>
47 #include <linux/module.h>
49 #include <asm/pgalloc.h>
50 #include <asm/uaccess.h>
51 #include <asm/tlb.h>
53 unsigned long max_mapnr;
54 unsigned long num_physpages;
55 unsigned long num_mappedpages;
56 void * high_memory;
57 struct page *highmem_start_page;
59 /*
60 * We special-case the C-O-W ZERO_PAGE, because it's such
61 * a common occurrence (no need to read the page to know
62 * that it's zero - better for the cache and memory subsystem).
63 */
64 static inline void copy_cow_page(struct page * from, struct page * to, unsigned long address)
65 {
66 if (from == ZERO_PAGE(address)) {
67 clear_user_highpage(to, address);
68 return;
69 }
70 copy_user_highpage(to, from, address);
71 }
73 mem_map_t * mem_map;
75 /*
76 * Called by TLB shootdown
77 */
78 void __free_pte(pte_t pte)
79 {
80 struct page *page = pte_page(pte);
81 if ((!VALID_PAGE(page)) || PageReserved(page))
82 return;
83 if (pte_dirty(pte))
84 set_page_dirty(page);
85 free_page_and_swap_cache(page);
86 }
89 /*
90 * Note: this doesn't free the actual pages themselves. That
91 * has been handled earlier when unmapping all the memory regions.
92 */
93 static inline void free_one_pmd(pmd_t * dir)
94 {
95 pte_t * pte;
97 if (pmd_none(*dir))
98 return;
99 if (pmd_bad(*dir)) {
100 pmd_ERROR(*dir);
101 pmd_clear(dir);
102 return;
103 }
104 pte = pte_offset(dir, 0);
105 pmd_clear(dir);
106 pte_free(pte);
107 }
109 static inline void free_one_pgd(pgd_t * dir)
110 {
111 int j;
112 pmd_t * pmd;
114 if (pgd_none(*dir))
115 return;
116 if (pgd_bad(*dir)) {
117 pgd_ERROR(*dir);
118 pgd_clear(dir);
119 return;
120 }
121 pmd = pmd_offset(dir, 0);
122 pgd_clear(dir);
123 for (j = 0; j < PTRS_PER_PMD ; j++) {
124 prefetchw(pmd+j+(PREFETCH_STRIDE/16));
125 free_one_pmd(pmd+j);
126 }
127 pmd_free(pmd);
128 }
130 /* Low and high watermarks for page table cache.
131 The system should try to have pgt_water[0] <= cache elements <= pgt_water[1]
132 */
133 int pgt_cache_water[2] = { 25, 50 };
135 /* Returns the number of pages freed */
136 int check_pgt_cache(void)
137 {
138 return do_check_pgt_cache(pgt_cache_water[0], pgt_cache_water[1]);
139 }
142 /*
143 * This function clears all user-level page tables of a process - this
144 * is needed by execve(), so that old pages aren't in the way.
145 */
146 void clear_page_tables(struct mm_struct *mm, unsigned long first, int nr)
147 {
148 pgd_t * page_dir = mm->pgd;
150 spin_lock(&mm->page_table_lock);
151 page_dir += first;
152 do {
153 free_one_pgd(page_dir);
154 page_dir++;
155 } while (--nr);
156 XENO_flush_page_update_queue();
157 spin_unlock(&mm->page_table_lock);
159 /* keep the page table cache within bounds */
160 check_pgt_cache();
161 }
163 #define PTE_TABLE_MASK ((PTRS_PER_PTE-1) * sizeof(pte_t))
164 #define PMD_TABLE_MASK ((PTRS_PER_PMD-1) * sizeof(pmd_t))
166 /*
167 * copy one vm_area from one task to the other. Assumes the page tables
168 * already present in the new task to be cleared in the whole range
169 * covered by this vma.
170 *
171 * 08Jan98 Merged into one routine from several inline routines to reduce
172 * variable count and make things faster. -jj
173 *
174 * dst->page_table_lock is held on entry and exit,
175 * but may be dropped within pmd_alloc() and pte_alloc().
176 */
177 int copy_page_range(struct mm_struct *dst, struct mm_struct *src,
178 struct vm_area_struct *vma)
179 {
180 pgd_t * src_pgd, * dst_pgd;
181 unsigned long address = vma->vm_start;
182 unsigned long end = vma->vm_end;
183 unsigned long cow = (vma->vm_flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE;
185 src_pgd = pgd_offset(src, address)-1;
186 dst_pgd = pgd_offset(dst, address)-1;
188 for (;;) {
189 pmd_t * src_pmd, * dst_pmd;
191 src_pgd++; dst_pgd++;
193 /* copy_pmd_range */
195 if (pgd_none(*src_pgd))
196 goto skip_copy_pmd_range;
197 if (pgd_bad(*src_pgd)) {
198 pgd_ERROR(*src_pgd);
199 pgd_clear(src_pgd);
200 skip_copy_pmd_range: address = (address + PGDIR_SIZE) & PGDIR_MASK;
201 if (!address || (address >= end))
202 goto out;
203 continue;
204 }
206 src_pmd = pmd_offset(src_pgd, address);
207 dst_pmd = pmd_alloc(dst, dst_pgd, address);
208 if (!dst_pmd)
209 goto nomem;
211 do {
212 pte_t * src_pte, * dst_pte;
214 /* copy_pte_range */
216 if (pmd_none(*src_pmd))
217 goto skip_copy_pte_range;
218 if (pmd_bad(*src_pmd)) {
219 pmd_ERROR(*src_pmd);
220 pmd_clear(src_pmd);
221 skip_copy_pte_range: address = (address + PMD_SIZE) & PMD_MASK;
222 if (address >= end)
223 goto out;
224 goto cont_copy_pmd_range;
225 }
227 src_pte = pte_offset(src_pmd, address);
228 dst_pte = pte_alloc(dst, dst_pmd, address);
229 if (!dst_pte)
230 goto nomem;
232 spin_lock(&src->page_table_lock);
233 do {
234 pte_t pte = *src_pte;
235 struct page *ptepage;
237 /* copy_one_pte */
239 if (pte_none(pte))
240 goto cont_copy_pte_range_noset;
241 if (!pte_present(pte)) {
242 swap_duplicate(pte_to_swp_entry(pte));
243 goto cont_copy_pte_range;
244 }
245 ptepage = pte_page(pte);
246 if ((!VALID_PAGE(ptepage)) ||
247 PageReserved(ptepage))
248 goto cont_copy_pte_range;
250 /* If it's a COW mapping, write protect it both in the parent and the child */
251 if (cow && pte_write(pte)) {
252 /* XENO modification: modified ordering here to avoid RaW hazard. */
253 pte = *src_pte;
254 pte = pte_wrprotect(pte);
255 ptep_set_wrprotect(src_pte);
256 }
258 /* If it's a shared mapping, mark it clean in the child */
259 if (vma->vm_flags & VM_SHARED)
260 pte = pte_mkclean(pte);
261 pte = pte_mkold(pte);
262 get_page(ptepage);
263 dst->rss++;
265 cont_copy_pte_range: set_pte(dst_pte, pte);
266 cont_copy_pte_range_noset: address += PAGE_SIZE;
267 if (address >= end)
268 goto out_unlock;
269 src_pte++;
270 dst_pte++;
271 } while ((unsigned long)src_pte & PTE_TABLE_MASK);
272 spin_unlock(&src->page_table_lock);
274 cont_copy_pmd_range: src_pmd++;
275 dst_pmd++;
276 } while ((unsigned long)src_pmd & PMD_TABLE_MASK);
277 }
278 out_unlock:
279 spin_unlock(&src->page_table_lock);
280 out:
281 return 0;
282 nomem:
283 return -ENOMEM;
284 }
286 /*
287 * Return indicates whether a page was freed so caller can adjust rss
288 */
289 static inline void forget_pte(pte_t page)
290 {
291 if (!pte_none(page)) {
292 printk("forget_pte: old mapping existed!\n");
293 BUG();
294 }
295 }
297 static inline int zap_pte_range(mmu_gather_t *tlb, pmd_t * pmd, unsigned long address, unsigned long size)
298 {
299 unsigned long offset;
300 pte_t * ptep;
301 int freed = 0;
303 if (pmd_none(*pmd))
304 return 0;
305 if (pmd_bad(*pmd)) {
306 pmd_ERROR(*pmd);
307 pmd_clear(pmd);
308 return 0;
309 }
310 ptep = pte_offset(pmd, address);
311 offset = address & ~PMD_MASK;
312 if (offset + size > PMD_SIZE)
313 size = PMD_SIZE - offset;
314 size &= PAGE_MASK;
315 for (offset=0; offset < size; ptep++, offset += PAGE_SIZE) {
316 pte_t pte = *ptep;
317 if (pte_none(pte))
318 continue;
319 if (pte_present(pte)) {
320 struct page *page = pte_page(pte);
321 #if defined(CONFIG_XENO_PRIV)
322 if (pte_io(pte)) {
323 queue_l1_entry_update(
324 __pa(ptep)|PGREQ_UNCHECKED_UPDATE, 0);
325 continue;
326 }
327 #endif
328 if (VALID_PAGE(page) && !PageReserved(page))
329 freed ++;
330 /* This will eventually call __free_pte on the pte. */
331 tlb_remove_page(tlb, ptep, address + offset);
332 } else {
333 free_swap_and_cache(pte_to_swp_entry(pte));
334 pte_clear(ptep);
335 }
336 }
338 return freed;
339 }
341 static inline int zap_pmd_range(mmu_gather_t *tlb, pgd_t * dir, unsigned long address, unsigned long size)
342 {
343 pmd_t * pmd;
344 unsigned long end;
345 int freed;
347 if (pgd_none(*dir))
348 return 0;
349 if (pgd_bad(*dir)) {
350 pgd_ERROR(*dir);
351 pgd_clear(dir);
352 return 0;
353 }
354 pmd = pmd_offset(dir, address);
355 end = address + size;
356 if (end > ((address + PGDIR_SIZE) & PGDIR_MASK))
357 end = ((address + PGDIR_SIZE) & PGDIR_MASK);
358 freed = 0;
359 do {
360 freed += zap_pte_range(tlb, pmd, address, end - address);
361 address = (address + PMD_SIZE) & PMD_MASK;
362 pmd++;
363 } while (address < end);
364 return freed;
365 }
367 /*
368 * remove user pages in a given range.
369 */
370 void zap_page_range(struct mm_struct *mm, unsigned long address, unsigned long size)
371 {
372 mmu_gather_t *tlb;
373 pgd_t * dir;
374 unsigned long start = address, end = address + size;
375 int freed = 0;
377 dir = pgd_offset(mm, address);
379 /*
380 * This is a long-lived spinlock. That's fine.
381 * There's no contention, because the page table
382 * lock only protects against kswapd anyway, and
383 * even if kswapd happened to be looking at this
384 * process we _want_ it to get stuck.
385 */
386 if (address >= end)
387 BUG();
388 spin_lock(&mm->page_table_lock);
389 flush_cache_range(mm, address, end);
390 tlb = tlb_gather_mmu(mm);
392 do {
393 freed += zap_pmd_range(tlb, dir, address, end - address);
394 address = (address + PGDIR_SIZE) & PGDIR_MASK;
395 dir++;
396 } while (address && (address < end));
398 /* this will flush any remaining tlb entries */
399 tlb_finish_mmu(tlb, start, end);
401 /*
402 * Update rss for the mm_struct (not necessarily current->mm)
403 * Notice that rss is an unsigned long.
404 */
405 if (mm->rss > freed)
406 mm->rss -= freed;
407 else
408 mm->rss = 0;
409 spin_unlock(&mm->page_table_lock);
410 }
412 /*
413 * Do a quick page-table lookup for a single page.
414 */
415 static struct page * follow_page(struct mm_struct *mm, unsigned long address, int write)
416 {
417 pgd_t *pgd;
418 pmd_t *pmd;
419 pte_t *ptep, pte;
421 pgd = pgd_offset(mm, address);
422 if (pgd_none(*pgd) || pgd_bad(*pgd))
423 goto out;
425 pmd = pmd_offset(pgd, address);
426 if (pmd_none(*pmd) || pmd_bad(*pmd))
427 goto out;
429 ptep = pte_offset(pmd, address);
430 if (!ptep)
431 goto out;
433 pte = *ptep;
434 if (pte_present(pte)) {
435 if (!write ||
436 (pte_write(pte) && pte_dirty(pte)))
437 return pte_page(pte);
438 }
440 out:
441 return 0;
442 }
444 /*
445 * Given a physical address, is there a useful struct page pointing to
446 * it? This may become more complex in the future if we start dealing
447 * with IO-aperture pages in kiobufs.
448 */
450 static inline struct page * get_page_map(struct page *page)
451 {
452 if (!VALID_PAGE(page))
453 return 0;
454 return page;
455 }
457 /*
458 * Please read Documentation/cachetlb.txt before using this function,
459 * accessing foreign memory spaces can cause cache coherency problems.
460 *
461 * Accessing a VM_IO area is even more dangerous, therefore the function
462 * fails if pages is != NULL and a VM_IO area is found.
463 */
464 int get_user_pages(struct task_struct *tsk, struct mm_struct *mm, unsigned long start,
465 int len, int write, int force, struct page **pages, struct vm_area_struct **vmas)
466 {
467 int i;
468 unsigned int flags;
470 /*
471 * Require read or write permissions.
472 * If 'force' is set, we only require the "MAY" flags.
473 */
474 flags = write ? (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
475 flags &= force ? (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE);
476 i = 0;
478 do {
479 struct vm_area_struct * vma;
481 vma = find_extend_vma(mm, start);
483 if ( !vma || (pages && vma->vm_flags & VM_IO) || !(flags & vma->vm_flags) )
484 return i ? : -EFAULT;
486 spin_lock(&mm->page_table_lock);
487 do {
488 struct page *map;
489 while (!(map = follow_page(mm, start, write))) {
490 spin_unlock(&mm->page_table_lock);
491 switch (handle_mm_fault(mm, vma, start, write)) {
492 case 1:
493 tsk->min_flt++;
494 break;
495 case 2:
496 tsk->maj_flt++;
497 break;
498 case 0:
499 if (i) return i;
500 return -EFAULT;
501 default:
502 if (i) return i;
503 return -ENOMEM;
504 }
505 spin_lock(&mm->page_table_lock);
506 }
507 if (pages) {
508 pages[i] = get_page_map(map);
509 /* FIXME: call the correct function,
510 * depending on the type of the found page
511 */
512 if (!pages[i])
513 goto bad_page;
514 page_cache_get(pages[i]);
515 }
516 if (vmas)
517 vmas[i] = vma;
518 i++;
519 start += PAGE_SIZE;
520 len--;
521 } while(len && start < vma->vm_end);
522 spin_unlock(&mm->page_table_lock);
523 } while(len);
524 out:
525 return i;
527 /*
528 * We found an invalid page in the VMA. Release all we have
529 * so far and fail.
530 */
531 bad_page:
532 spin_unlock(&mm->page_table_lock);
533 while (i--)
534 page_cache_release(pages[i]);
535 i = -EFAULT;
536 goto out;
537 }
539 EXPORT_SYMBOL(get_user_pages);
541 /*
542 * Force in an entire range of pages from the current process's user VA,
543 * and pin them in physical memory.
544 */
545 #define dprintk(x...)
547 int map_user_kiobuf(int rw, struct kiobuf *iobuf, unsigned long va, size_t len)
548 {
549 int pgcount, err;
550 struct mm_struct * mm;
552 /* Make sure the iobuf is not already mapped somewhere. */
553 if (iobuf->nr_pages)
554 return -EINVAL;
556 mm = current->mm;
557 dprintk ("map_user_kiobuf: begin\n");
559 pgcount = (va + len + PAGE_SIZE - 1)/PAGE_SIZE - va/PAGE_SIZE;
560 /* mapping 0 bytes is not permitted */
561 if (!pgcount) BUG();
562 err = expand_kiobuf(iobuf, pgcount);
563 if (err)
564 return err;
566 iobuf->locked = 0;
567 iobuf->offset = va & (PAGE_SIZE-1);
568 iobuf->length = len;
570 /* Try to fault in all of the necessary pages */
571 down_read(&mm->mmap_sem);
572 /* rw==READ means read from disk, write into memory area */
573 err = get_user_pages(current, mm, va, pgcount,
574 (rw==READ), 0, iobuf->maplist, NULL);
575 up_read(&mm->mmap_sem);
576 if (err < 0) {
577 unmap_kiobuf(iobuf);
578 dprintk ("map_user_kiobuf: end %d\n", err);
579 return err;
580 }
581 iobuf->nr_pages = err;
582 while (pgcount--) {
583 /* FIXME: flush superflous for rw==READ,
584 * probably wrong function for rw==WRITE
585 */
586 flush_dcache_page(iobuf->maplist[pgcount]);
587 }
588 dprintk ("map_user_kiobuf: end OK\n");
589 return 0;
590 }
592 /*
593 * Mark all of the pages in a kiobuf as dirty
594 *
595 * We need to be able to deal with short reads from disk: if an IO error
596 * occurs, the number of bytes read into memory may be less than the
597 * size of the kiobuf, so we have to stop marking pages dirty once the
598 * requested byte count has been reached.
599 *
600 * Must be called from process context - set_page_dirty() takes VFS locks.
601 */
603 void mark_dirty_kiobuf(struct kiobuf *iobuf, int bytes)
604 {
605 int index, offset, remaining;
606 struct page *page;
608 index = iobuf->offset >> PAGE_SHIFT;
609 offset = iobuf->offset & ~PAGE_MASK;
610 remaining = bytes;
611 if (remaining > iobuf->length)
612 remaining = iobuf->length;
614 while (remaining > 0 && index < iobuf->nr_pages) {
615 page = iobuf->maplist[index];
617 if (!PageReserved(page))
618 set_page_dirty(page);
620 remaining -= (PAGE_SIZE - offset);
621 offset = 0;
622 index++;
623 }
624 }
626 /*
627 * Unmap all of the pages referenced by a kiobuf. We release the pages,
628 * and unlock them if they were locked.
629 */
631 void unmap_kiobuf (struct kiobuf *iobuf)
632 {
633 int i;
634 struct page *map;
636 for (i = 0; i < iobuf->nr_pages; i++) {
637 map = iobuf->maplist[i];
638 if (map) {
639 if (iobuf->locked)
640 UnlockPage(map);
641 /* FIXME: cache flush missing for rw==READ
642 * FIXME: call the correct reference counting function
643 */
644 page_cache_release(map);
645 }
646 }
648 iobuf->nr_pages = 0;
649 iobuf->locked = 0;
650 }
653 /*
654 * Lock down all of the pages of a kiovec for IO.
655 *
656 * If any page is mapped twice in the kiovec, we return the error -EINVAL.
657 *
658 * The optional wait parameter causes the lock call to block until all
659 * pages can be locked if set. If wait==0, the lock operation is
660 * aborted if any locked pages are found and -EAGAIN is returned.
661 */
663 int lock_kiovec(int nr, struct kiobuf *iovec[], int wait)
664 {
665 struct kiobuf *iobuf;
666 int i, j;
667 struct page *page, **ppage;
668 int doublepage = 0;
669 int repeat = 0;
671 repeat:
673 for (i = 0; i < nr; i++) {
674 iobuf = iovec[i];
676 if (iobuf->locked)
677 continue;
679 ppage = iobuf->maplist;
680 for (j = 0; j < iobuf->nr_pages; ppage++, j++) {
681 page = *ppage;
682 if (!page)
683 continue;
685 if (TryLockPage(page)) {
686 while (j--) {
687 struct page *tmp = *--ppage;
688 if (tmp)
689 UnlockPage(tmp);
690 }
691 goto retry;
692 }
693 }
694 iobuf->locked = 1;
695 }
697 return 0;
699 retry:
701 /*
702 * We couldn't lock one of the pages. Undo the locking so far,
703 * wait on the page we got to, and try again.
704 */
706 unlock_kiovec(nr, iovec);
707 if (!wait)
708 return -EAGAIN;
710 /*
711 * Did the release also unlock the page we got stuck on?
712 */
713 if (!PageLocked(page)) {
714 /*
715 * If so, we may well have the page mapped twice
716 * in the IO address range. Bad news. Of
717 * course, it _might_ just be a coincidence,
718 * but if it happens more than once, chances
719 * are we have a double-mapped page.
720 */
721 if (++doublepage >= 3)
722 return -EINVAL;
724 /* Try again... */
725 wait_on_page(page);
726 }
728 if (++repeat < 16)
729 goto repeat;
730 return -EAGAIN;
731 }
733 /*
734 * Unlock all of the pages of a kiovec after IO.
735 */
737 int unlock_kiovec(int nr, struct kiobuf *iovec[])
738 {
739 struct kiobuf *iobuf;
740 int i, j;
741 struct page *page, **ppage;
743 for (i = 0; i < nr; i++) {
744 iobuf = iovec[i];
746 if (!iobuf->locked)
747 continue;
748 iobuf->locked = 0;
750 ppage = iobuf->maplist;
751 for (j = 0; j < iobuf->nr_pages; ppage++, j++) {
752 page = *ppage;
753 if (!page)
754 continue;
755 UnlockPage(page);
756 }
757 }
758 return 0;
759 }
761 static inline void zeromap_pte_range(pte_t * pte, unsigned long address,
762 unsigned long size, pgprot_t prot)
763 {
764 unsigned long end;
766 address &= ~PMD_MASK;
767 end = address + size;
768 if (end > PMD_SIZE)
769 end = PMD_SIZE;
770 do {
771 pte_t zero_pte = pte_wrprotect(mk_pte(ZERO_PAGE(address), prot));
772 pte_t oldpage = ptep_get_and_clear(pte);
773 set_pte(pte, zero_pte);
774 forget_pte(oldpage);
775 address += PAGE_SIZE;
776 pte++;
777 } while (address && (address < end));
778 }
780 static inline int zeromap_pmd_range(struct mm_struct *mm, pmd_t * pmd, unsigned long address,
781 unsigned long size, pgprot_t prot)
782 {
783 unsigned long end;
785 address &= ~PGDIR_MASK;
786 end = address + size;
787 if (end > PGDIR_SIZE)
788 end = PGDIR_SIZE;
789 do {
790 pte_t * pte = pte_alloc(mm, pmd, address);
791 if (!pte)
792 return -ENOMEM;
793 zeromap_pte_range(pte, address, end - address, prot);
794 address = (address + PMD_SIZE) & PMD_MASK;
795 pmd++;
796 } while (address && (address < end));
797 return 0;
798 }
800 int zeromap_page_range(unsigned long address, unsigned long size, pgprot_t prot)
801 {
802 int error = 0;
803 pgd_t * dir;
804 unsigned long beg = address;
805 unsigned long end = address + size;
806 struct mm_struct *mm = current->mm;
808 dir = pgd_offset(mm, address);
809 flush_cache_range(mm, beg, end);
810 if (address >= end)
811 BUG();
813 spin_lock(&mm->page_table_lock);
814 do {
815 pmd_t *pmd = pmd_alloc(mm, dir, address);
816 error = -ENOMEM;
817 if (!pmd)
818 break;
819 error = zeromap_pmd_range(mm, pmd, address, end - address, prot);
820 if (error)
821 break;
822 address = (address + PGDIR_SIZE) & PGDIR_MASK;
823 dir++;
824 } while (address && (address < end));
825 spin_unlock(&mm->page_table_lock);
826 flush_tlb_range(mm, beg, end);
827 return error;
828 }
830 /*
831 * maps a range of physical memory into the requested pages. the old
832 * mappings are removed. any references to nonexistent pages results
833 * in null mappings (currently treated as "copy-on-access")
834 */
835 static inline void remap_pte_range(pte_t * pte, unsigned long address, unsigned long size,
836 unsigned long phys_addr, pgprot_t prot)
837 {
838 unsigned long end;
840 address &= ~PMD_MASK;
841 end = address + size;
842 if (end > PMD_SIZE)
843 end = PMD_SIZE;
844 do {
845 struct page *page;
846 pte_t oldpage;
847 oldpage = ptep_get_and_clear(pte);
849 page = virt_to_page(__va(phys_addr));
850 if ((!VALID_PAGE(page)) || PageReserved(page))
851 set_pte(pte, mk_pte_phys(phys_addr, prot));
852 forget_pte(oldpage);
853 address += PAGE_SIZE;
854 phys_addr += PAGE_SIZE;
855 pte++;
856 } while (address && (address < end));
857 }
859 static inline int remap_pmd_range(struct mm_struct *mm, pmd_t * pmd, unsigned long address, unsigned long size,
860 unsigned long phys_addr, pgprot_t prot)
861 {
862 unsigned long end;
864 address &= ~PGDIR_MASK;
865 end = address + size;
866 if (end > PGDIR_SIZE)
867 end = PGDIR_SIZE;
868 phys_addr -= address;
869 do {
870 pte_t * pte = pte_alloc(mm, pmd, address);
871 if (!pte)
872 return -ENOMEM;
873 remap_pte_range(pte, address, end - address, address + phys_addr, prot);
874 address = (address + PMD_SIZE) & PMD_MASK;
875 pmd++;
876 } while (address && (address < end));
877 return 0;
878 }
880 /* Note: this is only safe if the mm semaphore is held when called. */
881 int remap_page_range(unsigned long from, unsigned long phys_addr, unsigned long size, pgprot_t prot)
882 {
883 int error = 0;
884 pgd_t * dir;
885 unsigned long beg = from;
886 unsigned long end = from + size;
887 struct mm_struct *mm = current->mm;
889 phys_addr -= from;
890 dir = pgd_offset(mm, from);
891 flush_cache_range(mm, beg, end);
892 if (from >= end)
893 BUG();
895 spin_lock(&mm->page_table_lock);
896 do {
897 pmd_t *pmd = pmd_alloc(mm, dir, from);
898 error = -ENOMEM;
899 if (!pmd)
900 break;
901 error = remap_pmd_range(mm, pmd, from, end - from, phys_addr + from, prot);
902 if (error)
903 break;
904 from = (from + PGDIR_SIZE) & PGDIR_MASK;
905 dir++;
906 } while (from && (from < end));
907 spin_unlock(&mm->page_table_lock);
908 flush_tlb_range(mm, beg, end);
909 return error;
910 }
912 /*
913 * Establish a new mapping:
914 * - flush the old one
915 * - update the page tables
916 * - inform the TLB about the new one
917 *
918 * We hold the mm semaphore for reading and vma->vm_mm->page_table_lock
919 */
920 static inline void establish_pte(struct vm_area_struct * vma, unsigned long address, pte_t *page_table, pte_t entry)
921 {
922 set_pte(page_table, entry);
923 flush_tlb_page(vma, address);
924 update_mmu_cache(vma, address, entry);
925 }
927 /*
928 * We hold the mm semaphore for reading and vma->vm_mm->page_table_lock
929 */
930 static inline void break_cow(struct vm_area_struct * vma, struct page * new_page, unsigned long address,
931 pte_t *page_table)
932 {
933 flush_page_to_ram(new_page);
934 flush_cache_page(vma, address);
935 establish_pte(vma, address, page_table, pte_mkwrite(pte_mkdirty(mk_pte(new_page, vma->vm_page_prot))));
936 }
938 /*
939 * This routine handles present pages, when users try to write
940 * to a shared page. It is done by copying the page to a new address
941 * and decrementing the shared-page counter for the old page.
942 *
943 * Goto-purists beware: the only reason for goto's here is that it results
944 * in better assembly code.. The "default" path will see no jumps at all.
945 *
946 * Note that this routine assumes that the protection checks have been
947 * done by the caller (the low-level page fault routine in most cases).
948 * Thus we can safely just mark it writable once we've done any necessary
949 * COW.
950 *
951 * We also mark the page dirty at this point even though the page will
952 * change only once the write actually happens. This avoids a few races,
953 * and potentially makes it more efficient.
954 *
955 * We hold the mm semaphore and the page_table_lock on entry and exit
956 * with the page_table_lock released.
957 */
958 static int do_wp_page(struct mm_struct *mm, struct vm_area_struct * vma,
959 unsigned long address, pte_t *page_table, pte_t pte)
960 {
961 struct page *old_page, *new_page;
963 old_page = pte_page(pte);
964 if (!VALID_PAGE(old_page))
965 goto bad_wp_page;
967 if (!TryLockPage(old_page)) {
968 int reuse = can_share_swap_page(old_page);
969 unlock_page(old_page);
970 if (reuse) {
971 flush_cache_page(vma, address);
972 establish_pte(vma, address, page_table, pte_mkyoung(pte_mkdirty(pte_mkwrite(pte))));
973 spin_unlock(&mm->page_table_lock);
974 return 1; /* Minor fault */
975 }
976 }
978 /*
979 * Ok, we need to copy. Oh, well..
980 */
981 page_cache_get(old_page);
982 spin_unlock(&mm->page_table_lock);
984 new_page = alloc_page(GFP_HIGHUSER);
985 if (!new_page)
986 goto no_mem;
987 copy_cow_page(old_page,new_page,address);
989 /*
990 * Re-check the pte - we dropped the lock
991 */
992 spin_lock(&mm->page_table_lock);
993 if (pte_same(*page_table, pte)) {
994 if (PageReserved(old_page))
995 ++mm->rss;
996 break_cow(vma, new_page, address, page_table);
997 lru_cache_add(new_page);
999 /* Free the old page.. */
1000 new_page = old_page;
1002 spin_unlock(&mm->page_table_lock);
1003 page_cache_release(new_page);
1004 page_cache_release(old_page);
1005 return 1; /* Minor fault */
1007 bad_wp_page:
1008 spin_unlock(&mm->page_table_lock);
1009 printk("do_wp_page: bogus page at address %08lx (page 0x%lx)\n",address,(unsigned long)old_page);
1010 return -1;
1011 no_mem:
1012 page_cache_release(old_page);
1013 return -1;
1016 static void vmtruncate_list(struct vm_area_struct *mpnt, unsigned long pgoff)
1018 do {
1019 struct mm_struct *mm = mpnt->vm_mm;
1020 unsigned long start = mpnt->vm_start;
1021 unsigned long end = mpnt->vm_end;
1022 unsigned long len = end - start;
1023 unsigned long diff;
1025 /* mapping wholly truncated? */
1026 if (mpnt->vm_pgoff >= pgoff) {
1027 zap_page_range(mm, start, len);
1028 continue;
1031 /* mapping wholly unaffected? */
1032 len = len >> PAGE_SHIFT;
1033 diff = pgoff - mpnt->vm_pgoff;
1034 if (diff >= len)
1035 continue;
1037 /* Ok, partially affected.. */
1038 start += diff << PAGE_SHIFT;
1039 len = (len - diff) << PAGE_SHIFT;
1040 zap_page_range(mm, start, len);
1041 } while ((mpnt = mpnt->vm_next_share) != NULL);
1044 /*
1045 * Handle all mappings that got truncated by a "truncate()"
1046 * system call.
1048 * NOTE! We have to be ready to update the memory sharing
1049 * between the file and the memory map for a potential last
1050 * incomplete page. Ugly, but necessary.
1051 */
1052 int vmtruncate(struct inode * inode, loff_t offset)
1054 unsigned long pgoff;
1055 struct address_space *mapping = inode->i_mapping;
1056 unsigned long limit;
1058 if (inode->i_size < offset)
1059 goto do_expand;
1060 inode->i_size = offset;
1061 spin_lock(&mapping->i_shared_lock);
1062 if (!mapping->i_mmap && !mapping->i_mmap_shared)
1063 goto out_unlock;
1065 pgoff = (offset + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1066 if (mapping->i_mmap != NULL)
1067 vmtruncate_list(mapping->i_mmap, pgoff);
1068 if (mapping->i_mmap_shared != NULL)
1069 vmtruncate_list(mapping->i_mmap_shared, pgoff);
1071 out_unlock:
1072 spin_unlock(&mapping->i_shared_lock);
1073 truncate_inode_pages(mapping, offset);
1074 goto out_truncate;
1076 do_expand:
1077 limit = current->rlim[RLIMIT_FSIZE].rlim_cur;
1078 if (limit != RLIM_INFINITY && offset > limit)
1079 goto out_sig;
1080 if (offset > inode->i_sb->s_maxbytes)
1081 goto out;
1082 inode->i_size = offset;
1084 out_truncate:
1085 if (inode->i_op && inode->i_op->truncate) {
1086 lock_kernel();
1087 inode->i_op->truncate(inode);
1088 unlock_kernel();
1090 return 0;
1091 out_sig:
1092 send_sig(SIGXFSZ, current, 0);
1093 out:
1094 return -EFBIG;
1097 /*
1098 * Primitive swap readahead code. We simply read an aligned block of
1099 * (1 << page_cluster) entries in the swap area. This method is chosen
1100 * because it doesn't cost us any seek time. We also make sure to queue
1101 * the 'original' request together with the readahead ones...
1102 */
1103 void swapin_readahead(swp_entry_t entry)
1105 int i, num;
1106 struct page *new_page;
1107 unsigned long offset;
1109 /*
1110 * Get the number of handles we should do readahead io to.
1111 */
1112 num = valid_swaphandles(entry, &offset);
1113 for (i = 0; i < num; offset++, i++) {
1114 /* Ok, do the async read-ahead now */
1115 new_page = read_swap_cache_async(SWP_ENTRY(SWP_TYPE(entry), offset));
1116 if (!new_page)
1117 break;
1118 page_cache_release(new_page);
1120 return;
1123 /*
1124 * We hold the mm semaphore and the page_table_lock on entry and
1125 * should release the pagetable lock on exit..
1126 */
1127 static int do_swap_page(struct mm_struct * mm,
1128 struct vm_area_struct * vma, unsigned long address,
1129 pte_t * page_table, pte_t orig_pte, int write_access)
1131 struct page *page;
1132 swp_entry_t entry = pte_to_swp_entry(orig_pte);
1133 pte_t pte;
1134 int ret = 1;
1136 spin_unlock(&mm->page_table_lock);
1137 page = lookup_swap_cache(entry);
1138 if (!page) {
1139 swapin_readahead(entry);
1140 page = read_swap_cache_async(entry);
1141 if (!page) {
1142 /*
1143 * Back out if somebody else faulted in this pte while
1144 * we released the page table lock.
1145 */
1146 int retval;
1147 spin_lock(&mm->page_table_lock);
1148 retval = pte_same(*page_table, orig_pte) ? -1 : 1;
1149 spin_unlock(&mm->page_table_lock);
1150 return retval;
1153 /* Had to read the page from swap area: Major fault */
1154 ret = 2;
1157 mark_page_accessed(page);
1159 lock_page(page);
1161 /*
1162 * Back out if somebody else faulted in this pte while we
1163 * released the page table lock.
1164 */
1165 spin_lock(&mm->page_table_lock);
1166 if (!pte_same(*page_table, orig_pte)) {
1167 spin_unlock(&mm->page_table_lock);
1168 unlock_page(page);
1169 page_cache_release(page);
1170 return 1;
1173 /* The page isn't present yet, go ahead with the fault. */
1175 swap_free(entry);
1176 if (vm_swap_full())
1177 remove_exclusive_swap_page(page);
1179 mm->rss++;
1180 pte = mk_pte(page, vma->vm_page_prot);
1181 if (write_access && can_share_swap_page(page))
1182 pte = pte_mkdirty(pte_mkwrite(pte));
1183 unlock_page(page);
1185 flush_page_to_ram(page);
1186 flush_icache_page(vma, page);
1187 set_pte(page_table, pte);
1189 /* No need to invalidate - it was non-present before */
1190 update_mmu_cache(vma, address, pte);
1191 XENO_flush_page_update_queue();
1192 spin_unlock(&mm->page_table_lock);
1193 return ret;
1196 /*
1197 * We are called with the MM semaphore and page_table_lock
1198 * spinlock held to protect against concurrent faults in
1199 * multithreaded programs.
1200 */
1201 static int do_anonymous_page(struct mm_struct * mm, struct vm_area_struct * vma, pte_t *page_table, int write_access, unsigned long addr)
1203 pte_t entry;
1205 /* Read-only mapping of ZERO_PAGE. */
1206 entry = pte_wrprotect(mk_pte(ZERO_PAGE(addr), vma->vm_page_prot));
1208 /* ..except if it's a write access */
1209 if (write_access) {
1210 struct page *page;
1212 /* Allocate our own private page. */
1213 spin_unlock(&mm->page_table_lock);
1215 page = alloc_page(GFP_HIGHUSER);
1216 if (!page)
1217 goto no_mem;
1218 clear_user_highpage(page, addr);
1220 spin_lock(&mm->page_table_lock);
1221 if (!pte_none(*page_table)) {
1222 page_cache_release(page);
1223 spin_unlock(&mm->page_table_lock);
1224 return 1;
1226 mm->rss++;
1227 flush_page_to_ram(page);
1228 entry = pte_mkwrite(pte_mkdirty(mk_pte(page, vma->vm_page_prot)));
1229 lru_cache_add(page);
1230 mark_page_accessed(page);
1233 set_pte(page_table, entry);
1235 /* No need to invalidate - it was non-present before */
1236 update_mmu_cache(vma, addr, entry);
1237 XENO_flush_page_update_queue();
1238 spin_unlock(&mm->page_table_lock);
1239 return 1; /* Minor fault */
1241 no_mem:
1242 return -1;
1245 /*
1246 * do_no_page() tries to create a new page mapping. It aggressively
1247 * tries to share with existing pages, but makes a separate copy if
1248 * the "write_access" parameter is true in order to avoid the next
1249 * page fault.
1251 * As this is called only for pages that do not currently exist, we
1252 * do not need to flush old virtual caches or the TLB.
1254 * This is called with the MM semaphore held and the page table
1255 * spinlock held. Exit with the spinlock released.
1256 */
1257 static int do_no_page(struct mm_struct * mm, struct vm_area_struct * vma,
1258 unsigned long address, int write_access, pte_t *page_table)
1260 struct page * new_page;
1261 pte_t entry;
1263 if (!vma->vm_ops || !vma->vm_ops->nopage)
1264 return do_anonymous_page(mm, vma, page_table, write_access, address);
1265 spin_unlock(&mm->page_table_lock);
1267 new_page = vma->vm_ops->nopage(vma, address & PAGE_MASK, 0);
1269 if (new_page == NULL) /* no page was available -- SIGBUS */
1270 return 0;
1271 if (new_page == NOPAGE_OOM)
1272 return -1;
1274 /*
1275 * Should we do an early C-O-W break?
1276 */
1277 if (write_access && !(vma->vm_flags & VM_SHARED)) {
1278 struct page * page = alloc_page(GFP_HIGHUSER);
1279 if (!page) {
1280 page_cache_release(new_page);
1281 return -1;
1283 copy_user_highpage(page, new_page, address);
1284 page_cache_release(new_page);
1285 lru_cache_add(page);
1286 new_page = page;
1289 spin_lock(&mm->page_table_lock);
1290 /*
1291 * This silly early PAGE_DIRTY setting removes a race
1292 * due to the bad i386 page protection. But it's valid
1293 * for other architectures too.
1295 * Note that if write_access is true, we either now have
1296 * an exclusive copy of the page, or this is a shared mapping,
1297 * so we can make it writable and dirty to avoid having to
1298 * handle that later.
1299 */
1300 /* Only go through if we didn't race with anybody else... */
1301 if (pte_none(*page_table)) {
1302 ++mm->rss;
1303 flush_page_to_ram(new_page);
1304 flush_icache_page(vma, new_page);
1305 entry = mk_pte(new_page, vma->vm_page_prot);
1306 if (write_access)
1307 entry = pte_mkwrite(pte_mkdirty(entry));
1308 set_pte(page_table, entry);
1309 } else {
1310 /* One of our sibling threads was faster, back out. */
1311 page_cache_release(new_page);
1312 spin_unlock(&mm->page_table_lock);
1313 return 1;
1316 /* no need to invalidate: a not-present page shouldn't be cached */
1317 update_mmu_cache(vma, address, entry);
1318 XENO_flush_page_update_queue();
1319 spin_unlock(&mm->page_table_lock);
1320 return 2; /* Major fault */
1323 /*
1324 * These routines also need to handle stuff like marking pages dirty
1325 * and/or accessed for architectures that don't do it in hardware (most
1326 * RISC architectures). The early dirtying is also good on the i386.
1328 * There is also a hook called "update_mmu_cache()" that architectures
1329 * with external mmu caches can use to update those (ie the Sparc or
1330 * PowerPC hashed page tables that act as extended TLBs).
1332 * Note the "page_table_lock". It is to protect against kswapd removing
1333 * pages from under us. Note that kswapd only ever _removes_ pages, never
1334 * adds them. As such, once we have noticed that the page is not present,
1335 * we can drop the lock early.
1337 * The adding of pages is protected by the MM semaphore (which we hold),
1338 * so we don't need to worry about a page being suddenly been added into
1339 * our VM.
1341 * We enter with the pagetable spinlock held, we are supposed to
1342 * release it when done.
1343 */
1344 static inline int handle_pte_fault(struct mm_struct *mm,
1345 struct vm_area_struct * vma, unsigned long address,
1346 int write_access, pte_t * pte)
1348 pte_t entry;
1350 entry = *pte;
1351 if (!pte_present(entry)) {
1352 /*
1353 * If it truly wasn't present, we know that kswapd
1354 * and the PTE updates will not touch it later. So
1355 * drop the lock.
1356 */
1357 if (pte_none(entry))
1358 return do_no_page(mm, vma, address, write_access, pte);
1359 return do_swap_page(mm, vma, address, pte, entry, write_access);
1362 if (write_access) {
1363 if (!pte_write(entry))
1364 return do_wp_page(mm, vma, address, pte, entry);
1366 entry = pte_mkdirty(entry);
1368 entry = pte_mkyoung(entry);
1369 establish_pte(vma, address, pte, entry);
1370 XENO_flush_page_update_queue();
1371 spin_unlock(&mm->page_table_lock);
1372 return 1;
1375 /*
1376 * By the time we get here, we already hold the mm semaphore
1377 */
1378 int handle_mm_fault(struct mm_struct *mm, struct vm_area_struct * vma,
1379 unsigned long address, int write_access)
1381 pgd_t *pgd;
1382 pmd_t *pmd;
1384 current->state = TASK_RUNNING;
1385 pgd = pgd_offset(mm, address);
1387 /*
1388 * We need the page table lock to synchronize with kswapd
1389 * and the SMP-safe atomic PTE updates.
1390 */
1391 spin_lock(&mm->page_table_lock);
1392 pmd = pmd_alloc(mm, pgd, address);
1394 if (pmd) {
1395 pte_t * pte = pte_alloc(mm, pmd, address);
1396 if (pte)
1397 return handle_pte_fault(mm, vma, address, write_access, pte);
1399 spin_unlock(&mm->page_table_lock);
1400 return -1;
1403 /*
1404 * Allocate page middle directory.
1406 * We've already handled the fast-path in-line, and we own the
1407 * page table lock.
1409 * On a two-level page table, this ends up actually being entirely
1410 * optimized away.
1411 */
1412 pmd_t *__pmd_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address)
1414 pmd_t *new;
1416 /* "fast" allocation can happen without dropping the lock.. */
1417 new = pmd_alloc_one_fast(mm, address);
1418 if (!new) {
1419 spin_unlock(&mm->page_table_lock);
1420 new = pmd_alloc_one(mm, address);
1421 spin_lock(&mm->page_table_lock);
1422 if (!new)
1423 return NULL;
1425 /*
1426 * Because we dropped the lock, we should re-check the
1427 * entry, as somebody else could have populated it..
1428 */
1429 if (!pgd_none(*pgd)) {
1430 pmd_free(new);
1431 goto out;
1434 pgd_populate(mm, pgd, new);
1435 out:
1436 return pmd_offset(pgd, address);
1439 /*
1440 * Allocate the page table directory.
1442 * We've already handled the fast-path in-line, and we own the
1443 * page table lock.
1444 */
1445 pte_t *pte_alloc(struct mm_struct *mm, pmd_t *pmd, unsigned long address)
1447 if (pmd_none(*pmd)) {
1448 pte_t *new;
1450 /* "fast" allocation can happen without dropping the lock.. */
1451 new = pte_alloc_one_fast(mm, address);
1452 if (!new) {
1453 XENO_flush_page_update_queue();
1454 spin_unlock(&mm->page_table_lock);
1455 new = pte_alloc_one(mm, address);
1456 spin_lock(&mm->page_table_lock);
1457 if (!new)
1458 return NULL;
1460 /*
1461 * Because we dropped the lock, we should re-check the
1462 * entry, as somebody else could have populated it..
1463 */
1464 if (!pmd_none(*pmd)) {
1465 pte_free(new);
1466 goto out;
1469 pmd_populate(mm, pmd, new);
1471 out:
1472 return pte_offset(pmd, address);
1475 int make_pages_present(unsigned long addr, unsigned long end)
1477 int ret, len, write;
1478 struct vm_area_struct * vma;
1480 vma = find_vma(current->mm, addr);
1481 write = (vma->vm_flags & VM_WRITE) != 0;
1482 if (addr >= end)
1483 BUG();
1484 if (end > vma->vm_end)
1485 BUG();
1486 len = (end+PAGE_SIZE-1)/PAGE_SIZE-addr/PAGE_SIZE;
1487 ret = get_user_pages(current, current->mm, addr,
1488 len, write, 0, NULL, NULL);
1489 return ret == len ? 0 : -1;
1492 struct page * vmalloc_to_page(void * vmalloc_addr)
1494 unsigned long addr = (unsigned long) vmalloc_addr;
1495 struct page *page = NULL;
1496 pmd_t *pmd;
1497 pte_t *pte;
1498 pgd_t *pgd;
1500 pgd = pgd_offset_k(addr);
1501 if (!pgd_none(*pgd)) {
1502 pmd = pmd_offset(pgd, addr);
1503 if (!pmd_none(*pmd)) {
1504 pte = pte_offset(pmd, addr);
1505 if (pte_present(*pte)) {
1506 page = pte_page(*pte);
1510 return page;