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