This source file includes following definitions.
- oom
- free_one_table
- clear_page_tables
- free_page_tables
- clone_page_tables
- copy_page_tables
- unmap_page_range
- zeromap_page_range
- remap_page_range
- put_page
- put_dirty_page
- do_wp_page
- verify_area
- get_empty_page
- try_to_share
- share_page
- get_empty_pgtable
- do_swap_page
- do_no_page
- do_page_fault
- __bad_pagetable
- __bad_page
- __zero_page
- show_mem
- paging_init
- mem_init
- si_meminfo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36 #include <linux/config.h>
37 #include <linux/signal.h>
38 #include <linux/sched.h>
39 #include <linux/head.h>
40 #include <linux/kernel.h>
41 #include <linux/errno.h>
42 #include <linux/string.h>
43 #include <linux/types.h>
44 #include <linux/ptrace.h>
45 #include <linux/mman.h>
46
47 #include <asm/system.h>
48 #include <asm/segment.h>
49
50
51
52
53
54
55 #undef CONFIG_TEST_VERIFY_AREA
56
57 unsigned long high_memory = 0;
58
59 extern unsigned long pg0[1024];
60
61 extern void scsi_mem_init(unsigned long);
62 extern void sound_mem_init(void);
63 extern void die_if_kernel(char *,struct pt_regs *,long);
64 extern void show_net_buffers(void);
65
66
67
68
69
70 int nr_swap_pages = 0;
71 int nr_free_pages = 0;
72 struct mem_list free_area_list[NR_MEM_LISTS];
73 unsigned char * free_area_map[NR_MEM_LISTS];
74
75 #define copy_page(from,to) \
76 __asm__("cld ; rep ; movsl": :"S" (from),"D" (to),"c" (1024):"cx","di","si")
77
78 unsigned short * mem_map = NULL;
79
80 #define CODE_SPACE(addr,p) ((addr) < (p)->end_code)
81
82
83
84
85
86 void oom(struct task_struct * task)
87 {
88 printk("\nOut of memory for %s.\n", current->comm);
89 task->sigaction[SIGKILL-1].sa_handler = NULL;
90 task->blocked &= ~(1<<(SIGKILL-1));
91 send_sig(SIGKILL,task,1);
92 }
93
94 static void free_one_table(unsigned long * page_dir)
95 {
96 int j;
97 unsigned long pg_table = *page_dir;
98 unsigned long * page_table;
99
100 if (!pg_table)
101 return;
102 *page_dir = 0;
103 if (pg_table >= high_memory || !(pg_table & PAGE_PRESENT)) {
104 printk("Bad page table: [%p]=%08lx\n",page_dir,pg_table);
105 return;
106 }
107 if (mem_map[MAP_NR(pg_table)] & MAP_PAGE_RESERVED)
108 return;
109 page_table = (unsigned long *) (pg_table & PAGE_MASK);
110 for (j = 0 ; j < PTRS_PER_PAGE ; j++,page_table++) {
111 unsigned long pg = *page_table;
112
113 if (!pg)
114 continue;
115 *page_table = 0;
116 if (pg & PAGE_PRESENT)
117 free_page(PAGE_MASK & pg);
118 else
119 swap_free(pg);
120 }
121 free_page(PAGE_MASK & pg_table);
122 }
123
124
125
126
127
128
129
130
131 void clear_page_tables(struct task_struct * tsk)
132 {
133 int i;
134 unsigned long pg_dir;
135 unsigned long * page_dir;
136
137 if (!tsk)
138 return;
139 if (tsk == task[0])
140 panic("task[0] (swapper) doesn't support exec()\n");
141 pg_dir = tsk->tss.cr3;
142 page_dir = (unsigned long *) pg_dir;
143 if (!page_dir || page_dir == swapper_pg_dir) {
144 printk("Trying to clear kernel page-directory: not good\n");
145 return;
146 }
147 if (mem_map[MAP_NR(pg_dir)] > 1) {
148 unsigned long * new_pg;
149
150 if (!(new_pg = (unsigned long*) get_free_page(GFP_KERNEL))) {
151 oom(tsk);
152 return;
153 }
154 for (i = 768 ; i < 1024 ; i++)
155 new_pg[i] = page_dir[i];
156 free_page(pg_dir);
157 tsk->tss.cr3 = (unsigned long) new_pg;
158 return;
159 }
160 for (i = 0 ; i < 768 ; i++,page_dir++)
161 free_one_table(page_dir);
162 invalidate();
163 return;
164 }
165
166
167
168
169 void free_page_tables(struct task_struct * tsk)
170 {
171 int i;
172 unsigned long pg_dir;
173 unsigned long * page_dir;
174
175 if (!tsk)
176 return;
177 if (tsk == task[0]) {
178 printk("task[0] (swapper) killed: unable to recover\n");
179 panic("Trying to free up swapper memory space");
180 }
181 pg_dir = tsk->tss.cr3;
182 if (!pg_dir || pg_dir == (unsigned long) swapper_pg_dir) {
183 printk("Trying to free kernel page-directory: not good\n");
184 return;
185 }
186 tsk->tss.cr3 = (unsigned long) swapper_pg_dir;
187 if (tsk == current)
188 __asm__ __volatile__("movl %0,%%cr3": :"a" (tsk->tss.cr3));
189 if (mem_map[MAP_NR(pg_dir)] > 1) {
190 free_page(pg_dir);
191 return;
192 }
193 page_dir = (unsigned long *) pg_dir;
194 for (i = 0 ; i < PTRS_PER_PAGE ; i++,page_dir++)
195 free_one_table(page_dir);
196 free_page(pg_dir);
197 invalidate();
198 }
199
200
201
202
203
204
205
206 int clone_page_tables(struct task_struct * tsk)
207 {
208 unsigned long pg_dir;
209
210 pg_dir = current->tss.cr3;
211 mem_map[MAP_NR(pg_dir)]++;
212 tsk->tss.cr3 = pg_dir;
213 return 0;
214 }
215
216
217
218
219
220
221 int copy_page_tables(struct task_struct * tsk)
222 {
223 int i;
224 unsigned long old_pg_dir, *old_page_dir;
225 unsigned long new_pg_dir, *new_page_dir;
226
227 if (!(new_pg_dir = get_free_page(GFP_KERNEL)))
228 return -ENOMEM;
229 old_pg_dir = current->tss.cr3;
230 tsk->tss.cr3 = new_pg_dir;
231 old_page_dir = (unsigned long *) old_pg_dir;
232 new_page_dir = (unsigned long *) new_pg_dir;
233 for (i = 0 ; i < PTRS_PER_PAGE ; i++,old_page_dir++,new_page_dir++) {
234 int j;
235 unsigned long old_pg_table, *old_page_table;
236 unsigned long new_pg_table, *new_page_table;
237
238 old_pg_table = *old_page_dir;
239 if (!old_pg_table)
240 continue;
241 if (old_pg_table >= high_memory || !(old_pg_table & PAGE_PRESENT)) {
242 printk("copy_page_tables: bad page table: "
243 "probable memory corruption\n");
244 *old_page_dir = 0;
245 continue;
246 }
247 if (mem_map[MAP_NR(old_pg_table)] & MAP_PAGE_RESERVED) {
248 *new_page_dir = old_pg_table;
249 continue;
250 }
251 if (!(new_pg_table = get_free_page(GFP_KERNEL))) {
252 free_page_tables(tsk);
253 return -ENOMEM;
254 }
255 old_page_table = (unsigned long *) (PAGE_MASK & old_pg_table);
256 new_page_table = (unsigned long *) (PAGE_MASK & new_pg_table);
257 for (j = 0 ; j < PTRS_PER_PAGE ; j++,old_page_table++,new_page_table++) {
258 unsigned long pg;
259 pg = *old_page_table;
260 if (!pg)
261 continue;
262 if (!(pg & PAGE_PRESENT)) {
263 *new_page_table = swap_duplicate(pg);
264 continue;
265 }
266 if (pg > high_memory || (mem_map[MAP_NR(pg)] & MAP_PAGE_RESERVED)) {
267 *new_page_table = pg;
268 continue;
269 }
270 if (pg & PAGE_COW)
271 pg &= ~PAGE_RW;
272 if (delete_from_swap_cache(pg))
273 pg |= PAGE_DIRTY;
274 *new_page_table = pg;
275 *old_page_table = pg;
276 mem_map[MAP_NR(pg)]++;
277 }
278 *new_page_dir = new_pg_table | PAGE_TABLE;
279 }
280 invalidate();
281 return 0;
282 }
283
284
285
286
287
288 int unmap_page_range(unsigned long from, unsigned long size)
289 {
290 unsigned long page, page_dir;
291 unsigned long *page_table, *dir;
292 unsigned long poff, pcnt, pc;
293
294 if (from & ~PAGE_MASK) {
295 printk("unmap_page_range called with wrong alignment\n");
296 return -EINVAL;
297 }
298 size = (size + ~PAGE_MASK) >> PAGE_SHIFT;
299 dir = PAGE_DIR_OFFSET(current->tss.cr3,from);
300 poff = (from >> PAGE_SHIFT) & (PTRS_PER_PAGE-1);
301 if ((pcnt = PTRS_PER_PAGE - poff) > size)
302 pcnt = size;
303
304 for ( ; size > 0; ++dir, size -= pcnt,
305 pcnt = (size > PTRS_PER_PAGE ? PTRS_PER_PAGE : size)) {
306 if (!(page_dir = *dir)) {
307 poff = 0;
308 continue;
309 }
310 if (!(page_dir & PAGE_PRESENT)) {
311 printk("unmap_page_range: bad page directory.");
312 continue;
313 }
314 page_table = (unsigned long *)(PAGE_MASK & page_dir);
315 if (poff) {
316 page_table += poff;
317 poff = 0;
318 }
319 for (pc = pcnt; pc--; page_table++) {
320 if ((page = *page_table) != 0) {
321 *page_table = 0;
322 if (PAGE_PRESENT & page) {
323 if (!(mem_map[MAP_NR(page)] & MAP_PAGE_RESERVED))
324 if (current->mm->rss > 0)
325 --current->mm->rss;
326 free_page(PAGE_MASK & page);
327 } else
328 swap_free(page);
329 }
330 }
331 if (pcnt == PTRS_PER_PAGE) {
332 *dir = 0;
333 free_page(PAGE_MASK & page_dir);
334 }
335 }
336 invalidate();
337 return 0;
338 }
339
340 int zeromap_page_range(unsigned long from, unsigned long size, int mask)
341 {
342 unsigned long *page_table, *dir;
343 unsigned long poff, pcnt;
344 unsigned long page;
345
346 if (mask) {
347 if ((mask & (PAGE_MASK|PAGE_PRESENT)) != PAGE_PRESENT) {
348 printk("zeromap_page_range: mask = %08x\n",mask);
349 return -EINVAL;
350 }
351 mask |= ZERO_PAGE;
352 }
353 if (from & ~PAGE_MASK) {
354 printk("zeromap_page_range: from = %08lx\n",from);
355 return -EINVAL;
356 }
357 dir = PAGE_DIR_OFFSET(current->tss.cr3,from);
358 size = (size + ~PAGE_MASK) >> PAGE_SHIFT;
359 poff = (from >> PAGE_SHIFT) & (PTRS_PER_PAGE-1);
360 if ((pcnt = PTRS_PER_PAGE - poff) > size)
361 pcnt = size;
362
363 while (size > 0) {
364 if (!(PAGE_PRESENT & *dir)) {
365
366 if (!(page_table = (unsigned long*) get_free_page(GFP_KERNEL))) {
367 invalidate();
368 return -ENOMEM;
369 }
370 if (PAGE_PRESENT & *dir) {
371 free_page((unsigned long) page_table);
372 page_table = (unsigned long *)(PAGE_MASK & *dir++);
373 } else
374 *dir++ = ((unsigned long) page_table) | PAGE_TABLE;
375 } else
376 page_table = (unsigned long *)(PAGE_MASK & *dir++);
377 page_table += poff;
378 poff = 0;
379 for (size -= pcnt; pcnt-- ;) {
380 if ((page = *page_table) != 0) {
381 *page_table = 0;
382 if (page & PAGE_PRESENT) {
383 if (!(mem_map[MAP_NR(page)] & MAP_PAGE_RESERVED))
384 if (current->mm->rss > 0)
385 --current->mm->rss;
386 free_page(PAGE_MASK & page);
387 } else
388 swap_free(page);
389 }
390 *page_table++ = mask;
391 }
392 pcnt = (size > PTRS_PER_PAGE ? PTRS_PER_PAGE : size);
393 }
394 invalidate();
395 return 0;
396 }
397
398
399
400
401
402
403 int remap_page_range(unsigned long from, unsigned long to, unsigned long size, int mask)
404 {
405 unsigned long *page_table, *dir;
406 unsigned long poff, pcnt;
407 unsigned long page;
408
409 if (mask) {
410 if ((mask & (PAGE_MASK|PAGE_PRESENT)) != PAGE_PRESENT) {
411 printk("remap_page_range: mask = %08x\n",mask);
412 return -EINVAL;
413 }
414 }
415 if ((from & ~PAGE_MASK) || (to & ~PAGE_MASK)) {
416 printk("remap_page_range: from = %08lx, to=%08lx\n",from,to);
417 return -EINVAL;
418 }
419 dir = PAGE_DIR_OFFSET(current->tss.cr3,from);
420 size = (size + ~PAGE_MASK) >> PAGE_SHIFT;
421 poff = (from >> PAGE_SHIFT) & (PTRS_PER_PAGE-1);
422 if ((pcnt = PTRS_PER_PAGE - poff) > size)
423 pcnt = size;
424
425 while (size > 0) {
426 if (!(PAGE_PRESENT & *dir)) {
427
428 if (!(page_table = (unsigned long*) get_free_page(GFP_KERNEL))) {
429 invalidate();
430 return -1;
431 }
432 *dir++ = ((unsigned long) page_table) | PAGE_TABLE;
433 }
434 else
435 page_table = (unsigned long *)(PAGE_MASK & *dir++);
436 if (poff) {
437 page_table += poff;
438 poff = 0;
439 }
440
441 for (size -= pcnt; pcnt-- ;) {
442 if ((page = *page_table) != 0) {
443 *page_table = 0;
444 if (PAGE_PRESENT & page) {
445 if (!(mem_map[MAP_NR(page)] & MAP_PAGE_RESERVED))
446 if (current->mm->rss > 0)
447 --current->mm->rss;
448 free_page(PAGE_MASK & page);
449 } else
450 swap_free(page);
451 }
452
453
454
455
456
457
458
459 if (!mask)
460 *page_table++ = 0;
461 else if (to >= high_memory)
462 *page_table++ = (to | mask);
463 else if (!mem_map[MAP_NR(to)])
464 *page_table++ = 0;
465 else {
466 *page_table++ = (to | mask);
467 if (!(mem_map[MAP_NR(to)] & MAP_PAGE_RESERVED)) {
468 ++current->mm->rss;
469 mem_map[MAP_NR(to)]++;
470 }
471 }
472 to += PAGE_SIZE;
473 }
474 pcnt = (size > PTRS_PER_PAGE ? PTRS_PER_PAGE : size);
475 }
476 invalidate();
477 return 0;
478 }
479
480
481
482
483
484
485
486 unsigned long put_page(struct task_struct * tsk,unsigned long page,
487 unsigned long address,int prot)
488 {
489 unsigned long *page_table;
490
491 if ((prot & (PAGE_MASK|PAGE_PRESENT)) != PAGE_PRESENT)
492 printk("put_page: prot = %08x\n",prot);
493 if (page >= high_memory) {
494 printk("put_page: trying to put page %08lx at %08lx\n",page,address);
495 return 0;
496 }
497 page_table = PAGE_DIR_OFFSET(tsk->tss.cr3,address);
498 if ((*page_table) & PAGE_PRESENT)
499 page_table = (unsigned long *) (PAGE_MASK & *page_table);
500 else {
501 printk("put_page: bad page directory entry\n");
502 oom(tsk);
503 *page_table = BAD_PAGETABLE | PAGE_TABLE;
504 return 0;
505 }
506 page_table += (address >> PAGE_SHIFT) & (PTRS_PER_PAGE-1);
507 if (*page_table) {
508 printk("put_page: page already exists\n");
509 *page_table = 0;
510 invalidate();
511 }
512 *page_table = page | prot;
513
514 return page;
515 }
516
517
518
519
520
521
522
523 unsigned long put_dirty_page(struct task_struct * tsk, unsigned long page, unsigned long address)
524 {
525 unsigned long tmp, *page_table;
526
527 if (page >= high_memory)
528 printk("put_dirty_page: trying to put page %08lx at %08lx\n",page,address);
529 if (mem_map[MAP_NR(page)] != 1)
530 printk("mem_map disagrees with %08lx at %08lx\n",page,address);
531 page_table = PAGE_DIR_OFFSET(tsk->tss.cr3,address);
532 if (PAGE_PRESENT & *page_table)
533 page_table = (unsigned long *) (PAGE_MASK & *page_table);
534 else {
535 if (!(tmp = get_free_page(GFP_KERNEL)))
536 return 0;
537 if (PAGE_PRESENT & *page_table) {
538 free_page(tmp);
539 page_table = (unsigned long *) (PAGE_MASK & *page_table);
540 } else {
541 *page_table = tmp | PAGE_TABLE;
542 page_table = (unsigned long *) tmp;
543 }
544 }
545 page_table += (address >> PAGE_SHIFT) & (PTRS_PER_PAGE-1);
546 if (*page_table) {
547 printk("put_dirty_page: page already exists\n");
548 *page_table = 0;
549 invalidate();
550 }
551 *page_table = page | (PAGE_DIRTY | PAGE_PRIVATE);
552
553 return page;
554 }
555
556
557
558
559
560
561
562
563
564 void do_wp_page(struct vm_area_struct * vma, unsigned long address,
565 unsigned long error_code)
566 {
567 unsigned long *pde, pte, old_page, prot;
568 unsigned long new_page;
569
570 new_page = __get_free_page(GFP_KERNEL);
571 pde = PAGE_DIR_OFFSET(vma->vm_task->tss.cr3,address);
572 pte = *pde;
573 if (!(pte & PAGE_PRESENT))
574 goto end_wp_page;
575 if ((pte & PAGE_TABLE) != PAGE_TABLE || pte >= high_memory)
576 goto bad_wp_pagetable;
577 pte &= PAGE_MASK;
578 pte += PAGE_PTR(address);
579 old_page = *(unsigned long *) pte;
580 if (!(old_page & PAGE_PRESENT))
581 goto end_wp_page;
582 if (old_page >= high_memory)
583 goto bad_wp_page;
584 if (old_page & PAGE_RW)
585 goto end_wp_page;
586 vma->vm_task->mm->min_flt++;
587 prot = (old_page & ~PAGE_MASK) | PAGE_RW | PAGE_DIRTY;
588 old_page &= PAGE_MASK;
589 if (mem_map[MAP_NR(old_page)] != 1) {
590 if (new_page) {
591 if (mem_map[MAP_NR(old_page)] & MAP_PAGE_RESERVED)
592 ++vma->vm_task->mm->rss;
593 copy_page(old_page,new_page);
594 *(unsigned long *) pte = new_page | prot;
595 free_page(old_page);
596 invalidate();
597 return;
598 }
599 free_page(old_page);
600 oom(vma->vm_task);
601 *(unsigned long *) pte = BAD_PAGE | prot;
602 invalidate();
603 return;
604 }
605 *(unsigned long *) pte |= PAGE_RW | PAGE_DIRTY;
606 invalidate();
607 if (new_page)
608 free_page(new_page);
609 return;
610 bad_wp_page:
611 printk("do_wp_page: bogus page at address %08lx (%08lx)\n",address,old_page);
612 *(unsigned long *) pte = BAD_PAGE | PAGE_SHARED;
613 send_sig(SIGKILL, vma->vm_task, 1);
614 goto end_wp_page;
615 bad_wp_pagetable:
616 printk("do_wp_page: bogus page-table at address %08lx (%08lx)\n",address,pte);
617 *pde = BAD_PAGETABLE | PAGE_TABLE;
618 send_sig(SIGKILL, vma->vm_task, 1);
619 end_wp_page:
620 if (new_page)
621 free_page(new_page);
622 return;
623 }
624
625
626
627
628 int verify_area(int type, const void * addr, unsigned long size)
629 {
630 struct vm_area_struct * vma;
631 unsigned long start = (unsigned long) addr;
632
633
634
635
636
637 if (get_fs() == get_ds())
638 return 0;
639
640 for (vma = current->mm->mmap ; ; vma = vma->vm_next) {
641 if (!vma)
642 goto bad_area;
643 if (vma->vm_end > start)
644 break;
645 }
646 if (vma->vm_start <= start)
647 goto good_area;
648 if (!(vma->vm_flags & VM_GROWSDOWN))
649 goto bad_area;
650 if (vma->vm_end - start > current->rlim[RLIMIT_STACK].rlim_cur)
651 goto bad_area;
652
653 good_area:
654 if (!wp_works_ok && type == VERIFY_WRITE)
655 goto check_wp_fault_by_hand;
656 for (;;) {
657 struct vm_area_struct * next;
658 if (!(vma->vm_page_prot & PAGE_USER))
659 goto bad_area;
660 if (type != VERIFY_READ && !(vma->vm_page_prot & (PAGE_COW | PAGE_RW)))
661 goto bad_area;
662 if (vma->vm_end - start >= size)
663 return 0;
664 next = vma->vm_next;
665 if (!next || vma->vm_end != next->vm_start)
666 goto bad_area;
667 vma = next;
668 }
669
670 check_wp_fault_by_hand:
671 size--;
672 size += start & ~PAGE_MASK;
673 size >>= PAGE_SHIFT;
674 start &= PAGE_MASK;
675
676 for (;;) {
677 if (!(vma->vm_page_prot & (PAGE_COW | PAGE_RW)))
678 goto bad_area;
679 do_wp_page(vma, start, PAGE_PRESENT);
680 if (!size)
681 return 0;
682 size--;
683 start += PAGE_SIZE;
684 if (start < vma->vm_end)
685 continue;
686 vma = vma->vm_next;
687 if (!vma || vma->vm_start != start)
688 break;
689 }
690
691 bad_area:
692 return -EFAULT;
693 }
694
695 static inline void get_empty_page(struct task_struct * tsk, unsigned long address)
696 {
697 unsigned long tmp;
698
699 if (!(tmp = get_free_page(GFP_KERNEL))) {
700 oom(tsk);
701 tmp = BAD_PAGE;
702 }
703 if (!put_page(tsk,tmp,address,PAGE_PRIVATE))
704 free_page(tmp);
705 }
706
707
708
709
710
711
712
713
714
715 static int try_to_share(unsigned long to_address, struct vm_area_struct * to_area,
716 unsigned long from_address, struct vm_area_struct * from_area,
717 unsigned long newpage)
718 {
719 unsigned long from;
720 unsigned long to;
721 unsigned long from_page;
722 unsigned long to_page;
723
724 from_page = (unsigned long)PAGE_DIR_OFFSET(from_area->vm_task->tss.cr3,from_address);
725 to_page = (unsigned long)PAGE_DIR_OFFSET(to_area->vm_task->tss.cr3,to_address);
726
727 from = *(unsigned long *) from_page;
728 if (!(from & PAGE_PRESENT))
729 return 0;
730 from &= PAGE_MASK;
731 from_page = from + PAGE_PTR(from_address);
732 from = *(unsigned long *) from_page;
733
734 if (!(from & PAGE_PRESENT))
735 return 0;
736
737 if (from & PAGE_DIRTY) {
738 if (from_area->vm_page_prot & PAGE_COW)
739 return 0;
740 if (!(from_area->vm_page_prot & PAGE_RW))
741 return 0;
742 }
743
744 if (from >= high_memory)
745 return 0;
746 if (mem_map[MAP_NR(from)] & MAP_PAGE_RESERVED)
747 return 0;
748
749 to = *(unsigned long *) to_page;
750 if (!(to & PAGE_PRESENT))
751 return 0;
752 to &= PAGE_MASK;
753 to_page = to + PAGE_PTR(to_address);
754 if (*(unsigned long *) to_page)
755 return 0;
756
757 if (newpage) {
758 if (in_swap_cache(from)) {
759 if (from_area->vm_page_prot & PAGE_COW)
760 return 0;
761 if (!(from_area->vm_page_prot & PAGE_RW))
762 return 0;
763 }
764 copy_page((from & PAGE_MASK), newpage);
765 *(unsigned long *) to_page = newpage | to_area->vm_page_prot;
766 return 1;
767 }
768
769 if (in_swap_cache(from)) {
770 if (from_area->vm_page_prot & PAGE_COW)
771 return 0;
772 if (!(from_area->vm_page_prot & PAGE_RW))
773 return 0;
774 from |= PAGE_DIRTY;
775 *(unsigned long *) from_page = from;
776 delete_from_swap_cache(from);
777 invalidate();
778 }
779 mem_map[MAP_NR(from)]++;
780
781 to = (from & (PAGE_MASK | PAGE_DIRTY)) | to_area->vm_page_prot;
782 if (to & PAGE_COW)
783 to &= ~PAGE_RW;
784 *(unsigned long *) to_page = to;
785
786 if (!(from & PAGE_RW))
787 return 1;
788 if (!(from_area->vm_page_prot & PAGE_COW))
789 return 1;
790
791 from &= ~PAGE_RW;
792 *(unsigned long *) from_page = from;
793 invalidate();
794 return 1;
795 }
796
797
798
799
800
801
802
803
804 static int share_page(struct vm_area_struct * area, unsigned long address,
805 unsigned long error_code, unsigned long newpage)
806 {
807 struct inode * inode;
808 unsigned long offset;
809 unsigned long from_address;
810 unsigned long give_page;
811 struct vm_area_struct * mpnt;
812
813 if (!area || !(inode = area->vm_inode) || inode->i_count < 2)
814 return 0;
815
816 give_page = 0;
817 if ((area->vm_page_prot & PAGE_COW) && (error_code & PAGE_RW)) {
818 if (!newpage)
819 return 0;
820 give_page = newpage;
821 }
822 offset = address - area->vm_start + area->vm_offset;
823
824
825 for (mpnt = area->vm_next_share; mpnt != area; mpnt = mpnt->vm_next_share) {
826
827 if (mpnt->vm_inode != inode) {
828 printk("Aiee! Corrupt vm_area_struct i_mmap ring\n");
829 break;
830 }
831
832 if ((mpnt->vm_offset ^ area->vm_offset) & ~PAGE_MASK)
833 continue;
834
835 from_address = offset + mpnt->vm_start - mpnt->vm_offset;
836 if (from_address < mpnt->vm_start || from_address >= mpnt->vm_end)
837 continue;
838
839 if (!try_to_share(address, area, from_address, mpnt, give_page))
840 continue;
841
842 if (give_page || !newpage)
843 return 1;
844 free_page(newpage);
845 return 1;
846 }
847 return 0;
848 }
849
850
851
852
853 static inline unsigned long get_empty_pgtable(struct task_struct * tsk,unsigned long address)
854 {
855 unsigned long page;
856 unsigned long *p;
857
858 p = PAGE_DIR_OFFSET(tsk->tss.cr3,address);
859 if (PAGE_PRESENT & *p)
860 return *p;
861 if (*p) {
862 printk("get_empty_pgtable: bad page-directory entry \n");
863 *p = 0;
864 }
865 page = get_free_page(GFP_KERNEL);
866 p = PAGE_DIR_OFFSET(tsk->tss.cr3,address);
867 if (PAGE_PRESENT & *p) {
868 free_page(page);
869 return *p;
870 }
871 if (*p) {
872 printk("get_empty_pgtable: bad page-directory entry \n");
873 *p = 0;
874 }
875 if (page) {
876 *p = page | PAGE_TABLE;
877 return *p;
878 }
879 oom(current);
880 *p = BAD_PAGETABLE | PAGE_TABLE;
881 return 0;
882 }
883
884 static inline void do_swap_page(struct vm_area_struct * vma,
885 unsigned long address, unsigned long * pge, unsigned long entry)
886 {
887 unsigned long page;
888
889 if (vma->vm_ops && vma->vm_ops->swapin)
890 page = vma->vm_ops->swapin(vma, entry);
891 else
892 page = swap_in(entry);
893 if (*pge != entry) {
894 free_page(page);
895 return;
896 }
897 page = page | vma->vm_page_prot;
898 if (mem_map[MAP_NR(page)] > 1 && (page & PAGE_COW))
899 page &= ~PAGE_RW;
900 ++vma->vm_task->mm->rss;
901 ++vma->vm_task->mm->maj_flt;
902 *pge = page;
903 return;
904 }
905
906 void do_no_page(struct vm_area_struct * vma, unsigned long address,
907 unsigned long error_code)
908 {
909 unsigned long page, entry, prot;
910
911 page = get_empty_pgtable(vma->vm_task,address);
912 if (!page)
913 return;
914 page &= PAGE_MASK;
915 page += PAGE_PTR(address);
916 entry = *(unsigned long *) page;
917 if (entry & PAGE_PRESENT)
918 return;
919 if (entry) {
920 do_swap_page(vma, address, (unsigned long *) page, entry);
921 return;
922 }
923 address &= PAGE_MASK;
924
925 if (!vma->vm_ops || !vma->vm_ops->nopage) {
926 ++vma->vm_task->mm->rss;
927 ++vma->vm_task->mm->min_flt;
928 get_empty_page(vma->vm_task,address);
929 return;
930 }
931 page = get_free_page(GFP_KERNEL);
932 if (share_page(vma, address, error_code, page)) {
933 ++vma->vm_task->mm->min_flt;
934 ++vma->vm_task->mm->rss;
935 return;
936 }
937 if (!page) {
938 oom(current);
939 put_page(vma->vm_task, BAD_PAGE, address, PAGE_PRIVATE);
940 return;
941 }
942 ++vma->vm_task->mm->maj_flt;
943 ++vma->vm_task->mm->rss;
944 prot = vma->vm_page_prot;
945
946
947
948
949
950 page = vma->vm_ops->nopage(vma, address, page, (error_code & PAGE_RW) && (prot & PAGE_COW));
951 if (share_page(vma, address, error_code, 0)) {
952 free_page(page);
953 return;
954 }
955
956
957
958
959 if (error_code & PAGE_RW) {
960 prot |= PAGE_DIRTY;
961 } else if ((prot & PAGE_COW) && mem_map[MAP_NR(page)] > 1)
962 prot &= ~PAGE_RW;
963 if (put_page(vma->vm_task, page, address, prot))
964 return;
965 free_page(page);
966 oom(current);
967 }
968
969
970
971
972
973
974 asmlinkage void do_page_fault(struct pt_regs *regs, unsigned long error_code)
975 {
976 struct vm_area_struct * vma;
977 unsigned long address;
978 unsigned long page;
979
980
981 __asm__("movl %%cr2,%0":"=r" (address));
982 for (vma = current->mm->mmap ; ; vma = vma->vm_next) {
983 if (!vma)
984 goto bad_area;
985 if (vma->vm_end > address)
986 break;
987 }
988 if (vma->vm_start <= address)
989 goto good_area;
990 if (!(vma->vm_flags & VM_GROWSDOWN))
991 goto bad_area;
992 if (vma->vm_end - address > current->rlim[RLIMIT_STACK].rlim_cur)
993 goto bad_area;
994 vma->vm_offset -= vma->vm_start - (address & PAGE_MASK);
995 vma->vm_start = (address & PAGE_MASK);
996
997
998
999
1000 good_area:
1001 if (regs->eflags & VM_MASK) {
1002 unsigned long bit = (address - 0xA0000) >> PAGE_SHIFT;
1003 if (bit < 32)
1004 current->tss.screen_bitmap |= 1 << bit;
1005 }
1006 if (!(vma->vm_page_prot & PAGE_USER))
1007 goto bad_area;
1008 if (error_code & PAGE_PRESENT) {
1009 if (!(vma->vm_page_prot & (PAGE_RW | PAGE_COW)))
1010 goto bad_area;
1011 #ifdef CONFIG_TEST_VERIFY_AREA
1012 if (regs->cs == KERNEL_CS)
1013 printk("WP fault at %08x\n", regs->eip);
1014 #endif
1015 do_wp_page(vma, address, error_code);
1016 return;
1017 }
1018 do_no_page(vma, address, error_code);
1019 return;
1020
1021
1022
1023
1024
1025 bad_area:
1026 if (error_code & PAGE_USER) {
1027 current->tss.cr2 = address;
1028 current->tss.error_code = error_code;
1029 current->tss.trap_no = 14;
1030 send_sig(SIGSEGV, current, 1);
1031 return;
1032 }
1033
1034
1035
1036
1037 if (wp_works_ok < 0 && address == TASK_SIZE && (error_code & PAGE_PRESENT)) {
1038 wp_works_ok = 1;
1039 pg0[0] = PAGE_SHARED;
1040 invalidate();
1041 printk("This processor honours the WP bit even when in supervisor mode. Good.\n");
1042 return;
1043 }
1044 if ((unsigned long) (address-TASK_SIZE) < PAGE_SIZE) {
1045 printk(KERN_ALERT "Unable to handle kernel NULL pointer dereference");
1046 pg0[0] = PAGE_SHARED;
1047 } else
1048 printk(KERN_ALERT "Unable to handle kernel paging request");
1049 printk(" at virtual address %08lx\n",address);
1050 __asm__("movl %%cr3,%0" : "=r" (page));
1051 printk(KERN_ALERT "current->tss.cr3 = %08lx, %%cr3 = %08lx\n",
1052 current->tss.cr3, page);
1053 page = ((unsigned long *) page)[address >> 22];
1054 printk(KERN_ALERT "*pde = %08lx\n", page);
1055 if (page & PAGE_PRESENT) {
1056 page &= PAGE_MASK;
1057 address &= 0x003ff000;
1058 page = ((unsigned long *) page)[address >> PAGE_SHIFT];
1059 printk(KERN_ALERT "*pte = %08lx\n", page);
1060 }
1061 die_if_kernel("Oops", regs, error_code);
1062 do_exit(SIGKILL);
1063 }
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078 unsigned long __bad_pagetable(void)
1079 {
1080 extern char empty_bad_page_table[PAGE_SIZE];
1081
1082 __asm__ __volatile__("cld ; rep ; stosl":
1083 :"a" (BAD_PAGE + PAGE_TABLE),
1084 "D" ((long) empty_bad_page_table),
1085 "c" (PTRS_PER_PAGE)
1086 :"di","cx");
1087 return (unsigned long) empty_bad_page_table;
1088 }
1089
1090 unsigned long __bad_page(void)
1091 {
1092 extern char empty_bad_page[PAGE_SIZE];
1093
1094 __asm__ __volatile__("cld ; rep ; stosl":
1095 :"a" (0),
1096 "D" ((long) empty_bad_page),
1097 "c" (PTRS_PER_PAGE)
1098 :"di","cx");
1099 return (unsigned long) empty_bad_page;
1100 }
1101
1102 unsigned long __zero_page(void)
1103 {
1104 extern char empty_zero_page[PAGE_SIZE];
1105
1106 __asm__ __volatile__("cld ; rep ; stosl":
1107 :"a" (0),
1108 "D" ((long) empty_zero_page),
1109 "c" (PTRS_PER_PAGE)
1110 :"di","cx");
1111 return (unsigned long) empty_zero_page;
1112 }
1113
1114 void show_mem(void)
1115 {
1116 int i,free = 0,total = 0,reserved = 0;
1117 int shared = 0;
1118
1119 printk("Mem-info:\n");
1120 show_free_areas();
1121 printk("Free swap: %6dkB\n",nr_swap_pages<<(PAGE_SHIFT-10));
1122 i = high_memory >> PAGE_SHIFT;
1123 while (i-- > 0) {
1124 total++;
1125 if (mem_map[i] & MAP_PAGE_RESERVED)
1126 reserved++;
1127 else if (!mem_map[i])
1128 free++;
1129 else
1130 shared += mem_map[i]-1;
1131 }
1132 printk("%d pages of RAM\n",total);
1133 printk("%d free pages\n",free);
1134 printk("%d reserved pages\n",reserved);
1135 printk("%d pages shared\n",shared);
1136 show_buffers();
1137 #ifdef CONFIG_NET
1138 show_net_buffers();
1139 #endif
1140 }
1141
1142 extern unsigned long free_area_init(unsigned long, unsigned long);
1143
1144
1145
1146
1147
1148
1149
1150
1151 unsigned long paging_init(unsigned long start_mem, unsigned long end_mem)
1152 {
1153 unsigned long * pg_dir;
1154 unsigned long * pg_table;
1155 unsigned long tmp;
1156 unsigned long address;
1157
1158
1159
1160
1161
1162
1163
1164 #if 0
1165 memset((void *) 0, 0, PAGE_SIZE);
1166 #endif
1167 start_mem = PAGE_ALIGN(start_mem);
1168 address = 0;
1169 pg_dir = swapper_pg_dir;
1170 while (address < end_mem) {
1171 tmp = *(pg_dir + 768);
1172 if (!tmp) {
1173 tmp = start_mem | PAGE_TABLE;
1174 *(pg_dir + 768) = tmp;
1175 start_mem += PAGE_SIZE;
1176 }
1177 *pg_dir = tmp;
1178 pg_dir++;
1179 pg_table = (unsigned long *) (tmp & PAGE_MASK);
1180 for (tmp = 0 ; tmp < PTRS_PER_PAGE ; tmp++,pg_table++) {
1181 if (address < end_mem)
1182 *pg_table = address | PAGE_SHARED;
1183 else
1184 *pg_table = 0;
1185 address += PAGE_SIZE;
1186 }
1187 }
1188 invalidate();
1189 return free_area_init(start_mem, end_mem);
1190 }
1191
1192 void mem_init(unsigned long start_low_mem,
1193 unsigned long start_mem, unsigned long end_mem)
1194 {
1195 int codepages = 0;
1196 int reservedpages = 0;
1197 int datapages = 0;
1198 unsigned long tmp;
1199 extern int etext;
1200
1201 end_mem &= PAGE_MASK;
1202 high_memory = end_mem;
1203
1204
1205 start_low_mem = PAGE_ALIGN(start_low_mem);
1206 start_mem = PAGE_ALIGN(start_mem);
1207
1208
1209
1210
1211
1212
1213 while (start_low_mem < 0x9f000) {
1214 mem_map[MAP_NR(start_low_mem)] = 0;
1215 start_low_mem += PAGE_SIZE;
1216 }
1217
1218 while (start_mem < high_memory) {
1219 mem_map[MAP_NR(start_mem)] = 0;
1220 start_mem += PAGE_SIZE;
1221 }
1222 #ifdef CONFIG_SCSI
1223 scsi_mem_init(high_memory);
1224 #endif
1225 #ifdef CONFIG_SOUND
1226 sound_mem_init();
1227 #endif
1228 for (tmp = 0 ; tmp < high_memory ; tmp += PAGE_SIZE) {
1229 if (mem_map[MAP_NR(tmp)]) {
1230 if (tmp >= 0xA0000 && tmp < 0x100000)
1231 reservedpages++;
1232 else if (tmp < (unsigned long) &etext)
1233 codepages++;
1234 else
1235 datapages++;
1236 continue;
1237 }
1238 mem_map[MAP_NR(tmp)] = 1;
1239 free_page(tmp);
1240 }
1241 tmp = nr_free_pages << PAGE_SHIFT;
1242 printk("Memory: %luk/%luk available (%dk kernel code, %dk reserved, %dk data)\n",
1243 tmp >> 10,
1244 high_memory >> 10,
1245 codepages << (PAGE_SHIFT-10),
1246 reservedpages << (PAGE_SHIFT-10),
1247 datapages << (PAGE_SHIFT-10));
1248
1249 wp_works_ok = -1;
1250 pg0[0] = PAGE_READONLY;
1251 invalidate();
1252 __asm__ __volatile__("movb 0,%%al ; movb %%al,0": : :"ax", "memory");
1253 pg0[0] = 0;
1254 invalidate();
1255 if (wp_works_ok < 0)
1256 wp_works_ok = 0;
1257 #ifdef CONFIG_TEST_VERIFY_AREA
1258 wp_works_ok = 0;
1259 #endif
1260 return;
1261 }
1262
1263 void si_meminfo(struct sysinfo *val)
1264 {
1265 int i;
1266
1267 i = high_memory >> PAGE_SHIFT;
1268 val->totalram = 0;
1269 val->sharedram = 0;
1270 val->freeram = nr_free_pages << PAGE_SHIFT;
1271 val->bufferram = buffermem;
1272 while (i-- > 0) {
1273 if (mem_map[i] & MAP_PAGE_RESERVED)
1274 continue;
1275 val->totalram++;
1276 if (!mem_map[i])
1277 continue;
1278 val->sharedram += mem_map[i]-1;
1279 }
1280 val->totalram <<= PAGE_SHIFT;
1281 val->sharedram <<= PAGE_SHIFT;
1282 return;
1283 }