root/mm/memory.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. oom
  2. free_one_table
  3. clear_page_tables
  4. free_page_tables
  5. clone_page_tables
  6. copy_page_tables
  7. unmap_page_range
  8. zeromap_page_range
  9. remap_page_range
  10. put_page
  11. put_dirty_page
  12. __do_wp_page
  13. do_wp_page
  14. verify_area
  15. get_empty_page
  16. try_to_share
  17. share_page
  18. get_empty_pgtable
  19. do_no_page
  20. do_page_fault
  21. __bad_pagetable
  22. __bad_page
  23. __zero_page
  24. show_mem
  25. paging_init
  26. mem_init
  27. si_meminfo
  28. file_mmap_nopage
  29. file_mmap_free
  30. file_mmap_share

   1 /*
   2  *  linux/mm/memory.c
   3  *
   4  *  Copyright (C) 1991, 1992  Linus Torvalds
   5  */
   6 
   7 /*
   8  * demand-loading started 01.12.91 - seems it is high on the list of
   9  * things wanted, and it should be easy to implement. - Linus
  10  */
  11 
  12 /*
  13  * Ok, demand-loading was easy, shared pages a little bit tricker. Shared
  14  * pages started 02.12.91, seems to work. - Linus.
  15  *
  16  * Tested sharing by executing about 30 /bin/sh: under the old kernel it
  17  * would have taken more than the 6M I have free, but it worked well as
  18  * far as I could see.
  19  *
  20  * Also corrected some "invalidate()"s - I wasn't doing enough of them.
  21  */
  22 
  23 /*
  24  * Real VM (paging to/from disk) started 18.12.91. Much more work and
  25  * thought has to go into this. Oh, well..
  26  * 19.12.91  -  works, somewhat. Sometimes I get faults, don't know why.
  27  *              Found it. Everything seems to work now.
  28  * 20.12.91  -  Ok, making the swap-device changeable like the root.
  29  */
  30 
  31 #include <asm/system.h>
  32 #include <linux/config.h>
  33 
  34 #include <linux/signal.h>
  35 #include <linux/sched.h>
  36 #include <linux/head.h>
  37 #include <linux/kernel.h>
  38 #include <linux/errno.h>
  39 #include <linux/string.h>
  40 #include <linux/types.h>
  41 #include <linux/ptrace.h>
  42 #include <linux/mman.h>
  43 
  44 unsigned long high_memory = 0;
  45 
  46 extern unsigned long pg0[1024];         /* page table for 0-4MB for everybody */
  47 
  48 extern void sound_mem_init(void);
  49 extern void die_if_kernel(char *,struct pt_regs *,long);
  50 
  51 int nr_swap_pages = 0;
  52 int nr_free_pages = 0;
  53 unsigned long free_page_list = 0;
  54 /*
  55  * The secondary free_page_list is used for malloc() etc things that
  56  * may need pages during interrupts etc. Normal get_free_page() operations
  57  * don't touch it, so it stays as a kind of "panic-list", that can be
  58  * accessed when all other mm tricks have failed.
  59  */
  60 int nr_secondary_pages = 0;
  61 unsigned long secondary_page_list = 0;
  62 
  63 #define copy_page(from,to) \
  64 __asm__("cld ; rep ; movsl": :"S" (from),"D" (to),"c" (1024):"cx","di","si")
  65 
  66 unsigned short * mem_map = NULL;
  67 
  68 #define CODE_SPACE(addr,p) ((addr) < (p)->end_code)
  69 
  70 /*
  71  * oom() prints a message (so that the user knows why the process died),
  72  * and gives the process an untrappable SIGSEGV.
  73  */
  74 void oom(struct task_struct * task)
     /* [previous][next][first][last][top][bottom][index][help] */
  75 {
  76         printk("\nout of memory\n");
  77         task->sigaction[SIGKILL-1].sa_handler = NULL;
  78         task->blocked &= ~(1<<(SIGKILL-1));
  79         send_sig(SIGKILL,task,1);
  80 }
  81 
  82 static void free_one_table(unsigned long * page_dir)
     /* [previous][next][first][last][top][bottom][index][help] */
  83 {
  84         int j;
  85         unsigned long pg_table = *page_dir;
  86         unsigned long * page_table;
  87 
  88         if (!pg_table)
  89                 return;
  90         *page_dir = 0;
  91         if (pg_table >= high_memory || !(pg_table & PAGE_PRESENT)) {
  92                 printk("Bad page table: [%p]=%08lx\n",page_dir,pg_table);
  93                 return;
  94         }
  95         if (mem_map[MAP_NR(pg_table)] & MAP_PAGE_RESERVED)
  96                 return;
  97         page_table = (unsigned long *) (pg_table & PAGE_MASK);
  98         for (j = 0 ; j < PTRS_PER_PAGE ; j++,page_table++) {
  99                 unsigned long pg = *page_table;
 100                 
 101                 if (!pg)
 102                         continue;
 103                 *page_table = 0;
 104                 if (pg & PAGE_PRESENT)
 105                         free_page(PAGE_MASK & pg);
 106                 else
 107                         swap_free(pg);
 108         }
 109         free_page(PAGE_MASK & pg_table);
 110 }
 111 
 112 /*
 113  * This function clears all user-level page tables of a process - this
 114  * is needed by execve(), so that old pages aren't in the way. Note that
 115  * unlike 'free_page_tables()', this function still leaves a valid
 116  * page-table-tree in memory: it just removes the user pages. The two
 117  * functions are similar, but there is a fundamental difference.
 118  */
 119 void clear_page_tables(struct task_struct * tsk)
     /* [previous][next][first][last][top][bottom][index][help] */
 120 {
 121         int i;
 122         unsigned long pg_dir;
 123         unsigned long * page_dir;
 124 
 125         if (!tsk)
 126                 return;
 127         if (tsk == task[0])
 128                 panic("task[0] (swapper) doesn't support exec()\n");
 129         pg_dir = tsk->tss.cr3;
 130         page_dir = (unsigned long *) pg_dir;
 131         if (!page_dir || page_dir == swapper_pg_dir) {
 132                 printk("Trying to clear kernel page-directory: not good\n");
 133                 return;
 134         }
 135         if (mem_map[MAP_NR(pg_dir)] > 1) {
 136                 unsigned long * new_pg;
 137 
 138                 if (!(new_pg = (unsigned long*) get_free_page(GFP_KERNEL))) {
 139                         oom(tsk);
 140                         return;
 141                 }
 142                 for (i = 768 ; i < 1024 ; i++)
 143                         new_pg[i] = page_dir[i];
 144                 free_page(pg_dir);
 145                 tsk->tss.cr3 = (unsigned long) new_pg;
 146                 return;
 147         }
 148         for (i = 0 ; i < 768 ; i++,page_dir++)
 149                 free_one_table(page_dir);
 150         invalidate();
 151         return;
 152 }
 153 
 154 /*
 155  * This function frees up all page tables of a process when it exits.
 156  */
 157 void free_page_tables(struct task_struct * tsk)
     /* [previous][next][first][last][top][bottom][index][help] */
 158 {
 159         int i;
 160         unsigned long pg_dir;
 161         unsigned long * page_dir;
 162 
 163         if (!tsk)
 164                 return;
 165         if (tsk == task[0]) {
 166                 printk("task[0] (swapper) killed: unable to recover\n");
 167                 panic("Trying to free up swapper memory space");
 168         }
 169         pg_dir = tsk->tss.cr3;
 170         if (!pg_dir || pg_dir == (unsigned long) swapper_pg_dir) {
 171                 printk("Trying to free kernel page-directory: not good\n");
 172                 return;
 173         }
 174         tsk->tss.cr3 = (unsigned long) swapper_pg_dir;
 175         if (tsk == current)
 176                 __asm__ __volatile__("movl %0,%%cr3": :"a" (tsk->tss.cr3));
 177         if (mem_map[MAP_NR(pg_dir)] > 1) {
 178                 free_page(pg_dir);
 179                 return;
 180         }
 181         page_dir = (unsigned long *) pg_dir;
 182         for (i = 0 ; i < PTRS_PER_PAGE ; i++,page_dir++)
 183                 free_one_table(page_dir);
 184         free_page(pg_dir);
 185         invalidate();
 186 }
 187 
 188 /*
 189  * clone_page_tables() clones the page table for a process - both
 190  * processes will have the exact same pages in memory. There are
 191  * probably races in the memory management with cloning, but we'll
 192  * see..
 193  */
 194 int clone_page_tables(struct task_struct * tsk)
     /* [previous][next][first][last][top][bottom][index][help] */
 195 {
 196         unsigned long pg_dir;
 197 
 198         pg_dir = current->tss.cr3;
 199         mem_map[MAP_NR(pg_dir)]++;
 200         tsk->tss.cr3 = pg_dir;
 201         return 0;
 202 }
 203 
 204 /*
 205  * copy_page_tables() just copies the whole process memory range:
 206  * note the special handling of RESERVED (ie kernel) pages, which
 207  * means that they are always shared by all processes.
 208  */
 209 int copy_page_tables(struct task_struct * tsk)
     /* [previous][next][first][last][top][bottom][index][help] */
 210 {
 211         int i;
 212         unsigned long old_pg_dir, *old_page_dir;
 213         unsigned long new_pg_dir, *new_page_dir;
 214 
 215         if (!(new_pg_dir = get_free_page(GFP_KERNEL)))
 216                 return -ENOMEM;
 217         old_pg_dir = current->tss.cr3;
 218         tsk->tss.cr3 = new_pg_dir;
 219         old_page_dir = (unsigned long *) old_pg_dir;
 220         new_page_dir = (unsigned long *) new_pg_dir;
 221         for (i = 0 ; i < PTRS_PER_PAGE ; i++,old_page_dir++,new_page_dir++) {
 222                 int j;
 223                 unsigned long old_pg_table, *old_page_table;
 224                 unsigned long new_pg_table, *new_page_table;
 225 
 226                 old_pg_table = *old_page_dir;
 227                 if (!old_pg_table)
 228                         continue;
 229                 if (old_pg_table >= high_memory || !(old_pg_table & PAGE_PRESENT)) {
 230                         printk("copy_page_tables: bad page table: "
 231                                 "probable memory corruption");
 232                         *old_page_dir = 0;
 233                         continue;
 234                 }
 235                 if (mem_map[MAP_NR(old_pg_table)] & MAP_PAGE_RESERVED) {
 236                         *new_page_dir = old_pg_table;
 237                         continue;
 238                 }
 239                 if (!(new_pg_table = get_free_page(GFP_KERNEL))) {
 240                         free_page_tables(tsk);
 241                         return -ENOMEM;
 242                 }
 243                 old_page_table = (unsigned long *) (PAGE_MASK & old_pg_table);
 244                 new_page_table = (unsigned long *) (PAGE_MASK & new_pg_table);
 245                 for (j = 0 ; j < PTRS_PER_PAGE ; j++,old_page_table++,new_page_table++) {
 246                         unsigned long pg;
 247                         pg = *old_page_table;
 248                         if (!pg)
 249                                 continue;
 250                         if (!(pg & PAGE_PRESENT)) {
 251                                 *new_page_table = swap_duplicate(pg);
 252                                 continue;
 253                         }
 254                         if ((pg & (PAGE_RW | PAGE_COW)) == (PAGE_RW | PAGE_COW))
 255                                 pg &= ~PAGE_RW;
 256                         *new_page_table = pg;
 257                         if (mem_map[MAP_NR(pg)] & MAP_PAGE_RESERVED)
 258                                 continue;
 259                         *old_page_table = pg;
 260                         mem_map[MAP_NR(pg)]++;
 261                 }
 262                 *new_page_dir = new_pg_table | PAGE_TABLE;
 263         }
 264         invalidate();
 265         return 0;
 266 }
 267 
 268 /*
 269  * a more complete version of free_page_tables which performs with page
 270  * granularity.
 271  */
 272 int unmap_page_range(unsigned long from, unsigned long size)
     /* [previous][next][first][last][top][bottom][index][help] */
 273 {
 274         unsigned long page, page_dir;
 275         unsigned long *page_table, *dir;
 276         unsigned long poff, pcnt, pc;
 277 
 278         if (from & ~PAGE_MASK) {
 279                 printk("unmap_page_range called with wrong alignment\n");
 280                 return -EINVAL;
 281         }
 282         size = (size + ~PAGE_MASK) >> PAGE_SHIFT;
 283         dir = PAGE_DIR_OFFSET(current->tss.cr3,from);
 284         poff = (from >> PAGE_SHIFT) & (PTRS_PER_PAGE-1);
 285         if ((pcnt = PTRS_PER_PAGE - poff) > size)
 286                 pcnt = size;
 287 
 288         for ( ; size > 0; ++dir, size -= pcnt,
 289              pcnt = (size > PTRS_PER_PAGE ? PTRS_PER_PAGE : size)) {
 290                 if (!(page_dir = *dir)) {
 291                         poff = 0;
 292                         continue;
 293                 }
 294                 if (!(page_dir & PAGE_PRESENT)) {
 295                         printk("unmap_page_range: bad page directory.");
 296                         continue;
 297                 }
 298                 page_table = (unsigned long *)(PAGE_MASK & page_dir);
 299                 if (poff) {
 300                         page_table += poff;
 301                         poff = 0;
 302                 }
 303                 for (pc = pcnt; pc--; page_table++) {
 304                         if ((page = *page_table) != 0) {
 305                                 *page_table = 0;
 306                                 if (1 & page) {
 307                                         if (!(mem_map[MAP_NR(page)]
 308                                               & MAP_PAGE_RESERVED))
 309                                                 --current->rss;
 310                                         free_page(PAGE_MASK & page);
 311                                 } else
 312                                         swap_free(page);
 313                         }
 314                 }
 315                 if (pcnt == PTRS_PER_PAGE) {
 316                         *dir = 0;
 317                         free_page(PAGE_MASK & page_dir);
 318                 }
 319         }
 320         invalidate();
 321         return 0;
 322 }
 323 
 324 int zeromap_page_range(unsigned long from, unsigned long size, int mask)
     /* [previous][next][first][last][top][bottom][index][help] */
 325 {
 326         unsigned long *page_table, *dir;
 327         unsigned long poff, pcnt;
 328         unsigned long page;
 329 
 330         if (mask) {
 331                 if ((mask & (PAGE_MASK|PAGE_PRESENT)) != PAGE_PRESENT) {
 332                         printk("zeromap_page_range: mask = %08x\n",mask);
 333                         return -EINVAL;
 334                 }
 335                 mask |= ZERO_PAGE;
 336         }
 337         if (from & ~PAGE_MASK) {
 338                 printk("zeromap_page_range: from = %08lx\n",from);
 339                 return -EINVAL;
 340         }
 341         dir = PAGE_DIR_OFFSET(current->tss.cr3,from);
 342         size = (size + ~PAGE_MASK) >> PAGE_SHIFT;
 343         poff = (from >> PAGE_SHIFT) & (PTRS_PER_PAGE-1);
 344         if ((pcnt = PTRS_PER_PAGE - poff) > size)
 345                 pcnt = size;
 346 
 347         while (size > 0) {
 348                 if (!(PAGE_PRESENT & *dir)) {
 349                                 /* clear page needed here?  SRB. */
 350                         if (!(page_table = (unsigned long*) get_free_page(GFP_KERNEL))) {
 351                                 invalidate();
 352                                 return -ENOMEM;
 353                         }
 354                         if (PAGE_PRESENT & *dir) {
 355                                 free_page((unsigned long) page_table);
 356                                 page_table = (unsigned long *)(PAGE_MASK & *dir++);
 357                         } else
 358                                 *dir++ = ((unsigned long) page_table) | PAGE_TABLE;
 359                 } else
 360                         page_table = (unsigned long *)(PAGE_MASK & *dir++);
 361                 page_table += poff;
 362                 poff = 0;
 363                 for (size -= pcnt; pcnt-- ;) {
 364                         if ((page = *page_table) != 0) {
 365                                 *page_table = 0;
 366                                 if (page & PAGE_PRESENT) {
 367                                         if (!(mem_map[MAP_NR(page)]
 368                                               & MAP_PAGE_RESERVED))
 369                                                 --current->rss;
 370                                         free_page(PAGE_MASK & page);
 371                                 } else
 372                                         swap_free(page);
 373                         }
 374                         *page_table++ = mask;
 375                 }
 376                 pcnt = (size > PTRS_PER_PAGE ? PTRS_PER_PAGE : size);
 377         }
 378         invalidate();
 379         return 0;
 380 }
 381 
 382 /*
 383  * maps a range of physical memory into the requested pages. the old
 384  * mappings are removed. any references to nonexistent pages results
 385  * in null mappings (currently treated as "copy-on-access")
 386  */
 387 int remap_page_range(unsigned long from, unsigned long to, unsigned long size, int mask)
     /* [previous][next][first][last][top][bottom][index][help] */
 388 {
 389         unsigned long *page_table, *dir;
 390         unsigned long poff, pcnt;
 391         unsigned long page;
 392 
 393         if (mask) {
 394                 if ((mask & (PAGE_MASK|PAGE_PRESENT)) != PAGE_PRESENT) {
 395                         printk("remap_page_range: mask = %08x\n",mask);
 396                         return -EINVAL;
 397                 }
 398         }
 399         if ((from & ~PAGE_MASK) || (to & ~PAGE_MASK)) {
 400                 printk("remap_page_range: from = %08lx, to=%08lx\n",from,to);
 401                 return -EINVAL;
 402         }
 403         dir = PAGE_DIR_OFFSET(current->tss.cr3,from);
 404         size = (size + ~PAGE_MASK) >> PAGE_SHIFT;
 405         poff = (from >> PAGE_SHIFT) & (PTRS_PER_PAGE-1);
 406         if ((pcnt = PTRS_PER_PAGE - poff) > size)
 407                 pcnt = size;
 408 
 409         while (size > 0) {
 410                 if (!(PAGE_PRESENT & *dir)) {
 411                         /* clearing page here, needed?  SRB. */
 412                         if (!(page_table = (unsigned long*) get_free_page(GFP_KERNEL))) {
 413                                 invalidate();
 414                                 return -1;
 415                         }
 416                         *dir++ = ((unsigned long) page_table) | PAGE_TABLE;
 417                 }
 418                 else
 419                         page_table = (unsigned long *)(PAGE_MASK & *dir++);
 420                 if (poff) {
 421                         page_table += poff;
 422                         poff = 0;
 423                 }
 424 
 425                 for (size -= pcnt; pcnt-- ;) {
 426                         if ((page = *page_table) != 0) {
 427                                 *page_table = 0;
 428                                 if (PAGE_PRESENT & page) {
 429                                         if (!(mem_map[MAP_NR(page)]
 430                                               & MAP_PAGE_RESERVED))
 431                                                 --current->rss;
 432                                         free_page(PAGE_MASK & page);
 433                                 } else
 434                                         swap_free(page);
 435                         }
 436 
 437                         /*
 438                          * the first condition should return an invalid access
 439                          * when the page is referenced. current assumptions
 440                          * cause it to be treated as demand allocation in some
 441                          * cases.
 442                          */
 443                         if (!mask)
 444                                 *page_table++ = 0;      /* not present */
 445                         else if (to >= high_memory)
 446                                 *page_table++ = (to | mask);
 447                         else if (!mem_map[MAP_NR(to)])
 448                                 *page_table++ = 0;      /* not present */
 449                         else {
 450                                 *page_table++ = (to | mask);
 451                                 if (!(mem_map[MAP_NR(to)] & MAP_PAGE_RESERVED)) {
 452                                         ++current->rss;
 453                                         mem_map[MAP_NR(to)]++;
 454                                 }
 455                         }
 456                         to += PAGE_SIZE;
 457                 }
 458                 pcnt = (size > PTRS_PER_PAGE ? PTRS_PER_PAGE : size);
 459         }
 460         invalidate();
 461         return 0;
 462 }
 463 
 464 /*
 465  * This function puts a page in memory at the wanted address.
 466  * It returns the physical address of the page gotten, 0 if
 467  * out of memory (either when trying to access page-table or
 468  * page.)
 469  */
 470 unsigned long put_page(struct task_struct * tsk,unsigned long page,
     /* [previous][next][first][last][top][bottom][index][help] */
 471         unsigned long address,int prot)
 472 {
 473         unsigned long *page_table;
 474 
 475         if ((prot & (PAGE_MASK|PAGE_PRESENT)) != PAGE_PRESENT)
 476                 printk("put_page: prot = %08x\n",prot);
 477         if (page >= high_memory) {
 478                 printk("put_page: trying to put page %08lx at %08lx\n",page,address);
 479                 return 0;
 480         }
 481         page_table = PAGE_DIR_OFFSET(tsk->tss.cr3,address);
 482         if ((*page_table) & PAGE_PRESENT)
 483                 page_table = (unsigned long *) (PAGE_MASK & *page_table);
 484         else {
 485                 printk("put_page: bad page directory entry\n");
 486                 oom(tsk);
 487                 *page_table = BAD_PAGETABLE | PAGE_TABLE;
 488                 return 0;
 489         }
 490         page_table += (address >> PAGE_SHIFT) & (PTRS_PER_PAGE-1);
 491         if (*page_table) {
 492                 printk("put_page: page already exists\n");
 493                 *page_table = 0;
 494                 invalidate();
 495         }
 496         *page_table = page | prot;
 497 /* no need for invalidate */
 498         return page;
 499 }
 500 
 501 /*
 502  * The previous function doesn't work very well if you also want to mark
 503  * the page dirty: exec.c wants this, as it has earlier changed the page,
 504  * and we want the dirty-status to be correct (for VM). Thus the same
 505  * routine, but this time we mark it dirty too.
 506  */
 507 unsigned long put_dirty_page(struct task_struct * tsk, unsigned long page, unsigned long address)
     /* [previous][next][first][last][top][bottom][index][help] */
 508 {
 509         unsigned long tmp, *page_table;
 510 
 511         if (page >= high_memory)
 512                 printk("put_dirty_page: trying to put page %08lx at %08lx\n",page,address);
 513         if (mem_map[MAP_NR(page)] != 1)
 514                 printk("mem_map disagrees with %08lx at %08lx\n",page,address);
 515         page_table = PAGE_DIR_OFFSET(tsk->tss.cr3,address);
 516         if (PAGE_PRESENT & *page_table)
 517                 page_table = (unsigned long *) (PAGE_MASK & *page_table);
 518         else {
 519                 if (!(tmp = get_free_page(GFP_KERNEL)))
 520                         return 0;
 521                 if (PAGE_PRESENT & *page_table) {
 522                         free_page(tmp);
 523                         page_table = (unsigned long *) (PAGE_MASK & *page_table);
 524                 } else {
 525                         *page_table = tmp | PAGE_TABLE;
 526                         page_table = (unsigned long *) tmp;
 527                 }
 528         }
 529         page_table += (address >> PAGE_SHIFT) & (PTRS_PER_PAGE-1);
 530         if (*page_table) {
 531                 printk("put_dirty_page: page already exists\n");
 532                 *page_table = 0;
 533                 invalidate();
 534         }
 535         *page_table = page | (PAGE_DIRTY | PAGE_PRIVATE);
 536 /* no need for invalidate */
 537         return page;
 538 }
 539 
 540 /*
 541  * This routine handles present pages, when users try to write
 542  * to a shared page. It is done by copying the page to a new address
 543  * and decrementing the shared-page counter for the old page.
 544  *
 545  * Note that we do many checks twice (look at do_wp_page()), as
 546  * we have to be careful about race-conditions.
 547  *
 548  * Goto-purists beware: the only reason for goto's here is that it results
 549  * in better assembly code.. The "default" path will see no jumps at all.
 550  */
 551 static void __do_wp_page(unsigned long error_code, unsigned long address,
     /* [previous][next][first][last][top][bottom][index][help] */
 552         struct task_struct * tsk, unsigned long user_esp)
 553 {
 554         unsigned long *pde, pte, old_page, prot;
 555         unsigned long new_page;
 556 
 557         new_page = __get_free_page(GFP_KERNEL);
 558         pde = PAGE_DIR_OFFSET(tsk->tss.cr3,address);
 559         pte = *pde;
 560         if (!(pte & PAGE_PRESENT))
 561                 goto end_wp_page;
 562         if ((pte & PAGE_TABLE) != PAGE_TABLE || pte >= high_memory)
 563                 goto bad_wp_pagetable;
 564         pte &= PAGE_MASK;
 565         pte += PAGE_PTR(address);
 566         old_page = *(unsigned long *) pte;
 567         if (!(old_page & PAGE_PRESENT))
 568                 goto end_wp_page;
 569         if (old_page >= high_memory)
 570                 goto bad_wp_page;
 571         if (old_page & PAGE_RW)
 572                 goto end_wp_page;
 573         tsk->min_flt++;
 574         prot = (old_page & ~PAGE_MASK) | PAGE_RW;
 575         old_page &= PAGE_MASK;
 576         if (mem_map[MAP_NR(old_page)] != 1) {
 577                 if (new_page) {
 578                         if (mem_map[MAP_NR(old_page)] & MAP_PAGE_RESERVED)
 579                                 ++tsk->rss;
 580                         copy_page(old_page,new_page);
 581                         *(unsigned long *) pte = new_page | prot;
 582                         free_page(old_page);
 583                         invalidate();
 584                         return;
 585                 }
 586                 free_page(old_page);
 587                 oom(tsk);
 588                 *(unsigned long *) pte = BAD_PAGE | prot;
 589                 invalidate();
 590                 return;
 591         }
 592         *(unsigned long *) pte |= PAGE_RW;
 593         invalidate();
 594         if (new_page)
 595                 free_page(new_page);
 596         return;
 597 bad_wp_page:
 598         printk("do_wp_page: bogus page at address %08lx (%08lx)\n",address,old_page);
 599         *(unsigned long *) pte = BAD_PAGE | PAGE_SHARED;
 600         send_sig(SIGKILL, tsk, 1);
 601         goto end_wp_page;
 602 bad_wp_pagetable:
 603         printk("do_wp_page: bogus page-table at address %08lx (%08lx)\n",address,pte);
 604         *pde = BAD_PAGETABLE | PAGE_TABLE;
 605         send_sig(SIGKILL, tsk, 1);
 606 end_wp_page:
 607         if (new_page)
 608                 free_page(new_page);
 609         return;
 610 }
 611 
 612 /*
 613  * check that a page table change is actually needed, and call
 614  * the low-level function only in that case..
 615  */
 616 void do_wp_page(unsigned long error_code, unsigned long address,
     /* [previous][next][first][last][top][bottom][index][help] */
 617         struct task_struct * tsk, unsigned long user_esp)
 618 {
 619         unsigned long page;
 620         unsigned long * pg_table;
 621 
 622         pg_table = PAGE_DIR_OFFSET(tsk->tss.cr3,address);
 623         page = *pg_table;
 624         if (!page)
 625                 return;
 626         if ((page & PAGE_PRESENT) && page < high_memory) {
 627                 pg_table = (unsigned long *) ((page & PAGE_MASK) + PAGE_PTR(address));
 628                 page = *pg_table;
 629                 if (!(page & PAGE_PRESENT))
 630                         return;
 631                 if (page & PAGE_RW)
 632                         return;
 633                 if (!(page & PAGE_COW)) {
 634                         if (user_esp && tsk == current) {
 635                                 current->tss.cr2 = address;
 636                                 current->tss.error_code = error_code;
 637                                 current->tss.trap_no = 14;
 638                                 send_sig(SIGSEGV, tsk, 1);
 639                                 return;
 640                         }
 641                 }
 642                 if (mem_map[MAP_NR(page)] == 1) {
 643                         *pg_table |= PAGE_RW | PAGE_DIRTY;
 644                         invalidate();
 645                         return;
 646                 }
 647                 __do_wp_page(error_code, address, tsk, user_esp);
 648                 return;
 649         }
 650         printk("bad page directory entry %08lx\n",page);
 651         *pg_table = 0;
 652 }
 653 
 654 int verify_area(int type, void * addr, unsigned long size)
     /* [previous][next][first][last][top][bottom][index][help] */
 655 {
 656         unsigned long start;
 657 
 658         start = (unsigned long) addr;
 659         if (start >= TASK_SIZE)
 660                 return -EFAULT;
 661         if (size > TASK_SIZE - start)
 662                 return -EFAULT;
 663         if (wp_works_ok || type == VERIFY_READ || !size)
 664                 return 0;
 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)
     /* [previous][next][first][last][top][bottom][index][help] */
 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  * try_to_share() checks the page at address "address" in the task "p",
 690  * to see if it exists, and if it is clean. If so, share it with the current
 691  * task.
 692  *
 693  * NOTE! This assumes we have checked that p != current, and that they
 694  * share the same executable or library.
 695  *
 696  * We may want to fix this to allow page sharing for PIC pages at different
 697  * addresses so that ELF will really perform properly. As long as the vast
 698  * majority of sharable libraries load at fixed addresses this is not a
 699  * big concern. Any sharing of pages between the buffer cache and the
 700  * code space reduces the need for this as well.  - ERY
 701  */
 702 static int try_to_share(unsigned long address, struct task_struct * tsk,
     /* [previous][next][first][last][top][bottom][index][help] */
 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 /* is there a page-directory at from? */
 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 /* is the page clean and present? */
 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 /* is the destination ok? */
 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 /* share them if read - do COW immediately otherwise */
 735         if (error_code & PAGE_RW) {
 736                 if(!newpage)    /* did the page exist?  SRB. */
 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)     /* only if it existed. SRB. */
 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  * share_page() tries to find a process that could share a page with
 755  * the current one. Address is the address of the wanted page relative
 756  * to the current data space.
 757  *
 758  * We first check if it is at all feasible by checking executable->i_count.
 759  * It should be >1 if there are other tasks sharing this inode.
 760  */
 761 int share_page(struct vm_area_struct * area, struct task_struct * tsk,
     /* [previous][next][first][last][top][bottom][index][help] */
 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                         /* Now see if there is something in the VMM that
 777                            we can share pages with */
 778                         if(area){
 779                           struct vm_area_struct * mpnt;
 780                           for (mpnt = (*p)->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;  /* Nope.  Nuthin here */
 789                         };
 790                 }
 791                 if (try_to_share(address,tsk,*p,error_code,newpage))
 792                         return 1;
 793         }
 794         return 0;
 795 }
 796 
 797 /*
 798  * fill in an empty page-table if none exists.
 799  */
 800 static inline unsigned long get_empty_pgtable(struct task_struct * tsk,unsigned long address)
     /* [previous][next][first][last][top][bottom][index][help] */
 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,
     /* [previous][next][first][last][top][bottom][index][help] */
 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->rss;
 847         if (tmp) {
 848                 ++tsk->maj_flt;
 849                 swap_in((unsigned long *) page);
 850                 return;
 851         }
 852         address &= 0xfffff000;
 853         tmp = 0;
 854         for (mpnt = tsk->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->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->end_data && address < tsk->brk)
 872                 goto ok_no_page;
 873         if (mpnt && mpnt == tsk->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)     /* user level access? */
 884                 return;
 885 ok_no_page:
 886         ++tsk->min_flt;
 887         get_empty_page(tsk,address);
 888 }
 889 
 890 /*
 891  * This routine handles page faults.  It determines the address,
 892  * and the problem, and then passes it off to one of the appropriate
 893  * routines.
 894  */
 895 asmlinkage void do_page_fault(struct pt_regs *regs, unsigned long error_code)
     /* [previous][next][first][last][top][bottom][index][help] */
 896 {
 897         unsigned long address;
 898         unsigned long user_esp = 0;
 899         unsigned int bit;
 900 
 901         /* get the address */
 902         __asm__("movl %%cr2,%0":"=r" (address));
 903         if (address < TASK_SIZE) {
 904                 if (error_code & 4) {   /* user mode access? */
 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 & 1)
 913                         do_wp_page(error_code, address, current, user_esp);
 914                 else
 915                         do_no_page(error_code, address, current, user_esp);
 916                 return;
 917         }
 918         address -= TASK_SIZE;
 919         if (wp_works_ok < 0 && address == 0 && (error_code & PAGE_PRESENT)) {
 920                 wp_works_ok = 1;
 921                 pg0[0] = PAGE_SHARED;
 922                 printk("This processor honours the WP bit even when in supervisor mode. Good.\n");
 923                 return;
 924         }
 925         if (address < PAGE_SIZE) {
 926                 printk("Unable to handle kernel NULL pointer dereference");
 927                 pg0[0] = PAGE_SHARED;
 928         } else
 929                 printk("Unable to handle kernel paging request");
 930         printk(" at address %08lx\n",address);
 931         die_if_kernel("Oops", regs, error_code);
 932         do_exit(SIGKILL);
 933 }
 934 
 935 /*
 936  * BAD_PAGE is the page that is used for page faults when linux
 937  * is out-of-memory. Older versions of linux just did a
 938  * do_exit(), but using this instead means there is less risk
 939  * for a process dying in kernel mode, possibly leaving a inode
 940  * unused etc..
 941  *
 942  * BAD_PAGETABLE is the accompanying page-table: it is initialized
 943  * to point to BAD_PAGE entries.
 944  *
 945  * ZERO_PAGE is a special page that is used for zero-initialized
 946  * data and COW.
 947  */
 948 unsigned long __bad_pagetable(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 949 {
 950         extern char empty_bad_page_table[PAGE_SIZE];
 951 
 952         __asm__ __volatile__("cld ; rep ; stosl":
 953                 :"a" (BAD_PAGE + PAGE_TABLE),
 954                  "D" ((long) empty_bad_page_table),
 955                  "c" (PTRS_PER_PAGE)
 956                 :"di","cx");
 957         return (unsigned long) empty_bad_page_table;
 958 }
 959 
 960 unsigned long __bad_page(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 961 {
 962         extern char empty_bad_page[PAGE_SIZE];
 963 
 964         __asm__ __volatile__("cld ; rep ; stosl":
 965                 :"a" (0),
 966                  "D" ((long) empty_bad_page),
 967                  "c" (PTRS_PER_PAGE)
 968                 :"di","cx");
 969         return (unsigned long) empty_bad_page;
 970 }
 971 
 972 unsigned long __zero_page(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 973 {
 974         extern char empty_zero_page[PAGE_SIZE];
 975 
 976         __asm__ __volatile__("cld ; rep ; stosl":
 977                 :"a" (0),
 978                  "D" ((long) empty_zero_page),
 979                  "c" (PTRS_PER_PAGE)
 980                 :"di","cx");
 981         return (unsigned long) empty_zero_page;
 982 }
 983 
 984 void show_mem(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 985 {
 986         int i,free = 0,total = 0,reserved = 0;
 987         int shared = 0;
 988 
 989         printk("Mem-info:\n");
 990         printk("Free pages:      %6dkB\n",nr_free_pages<<(PAGE_SHIFT-10));
 991         printk("Secondary pages: %6dkB\n",nr_secondary_pages<<(PAGE_SHIFT-10));
 992         printk("Free swap:       %6dkB\n",nr_swap_pages<<(PAGE_SHIFT-10));
 993         printk("Buffer memory:   %6dkB\n",buffermem>>10);
 994         printk("Buffer heads:    %6d\n",nr_buffer_heads);
 995         printk("Buffer blocks:   %6d\n",nr_buffers);
 996         i = high_memory >> PAGE_SHIFT;
 997         while (i-- > 0) {
 998                 total++;
 999                 if (mem_map[i] & MAP_PAGE_RESERVED)
1000                         reserved++;
1001                 else if (!mem_map[i])
1002                         free++;
1003                 else
1004                         shared += mem_map[i]-1;
1005         }
1006         printk("%d pages of RAM\n",total);
1007         printk("%d free pages\n",free);
1008         printk("%d reserved pages\n",reserved);
1009         printk("%d pages shared\n",shared);
1010 }
1011 
1012 /*
1013  * paging_init() sets up the page tables - note that the first 4MB are
1014  * already mapped by head.S.
1015  *
1016  * This routines also unmaps the page at virtual kernel address 0, so
1017  * that we can trap those pesky NULL-reference errors in the kernel.
1018  */
1019 unsigned long paging_init(unsigned long start_mem, unsigned long end_mem)
     /* [previous][next][first][last][top][bottom][index][help] */
1020 {
1021         unsigned long * pg_dir;
1022         unsigned long * pg_table;
1023         unsigned long tmp;
1024         unsigned long address;
1025 
1026 /*
1027  * Physical page 0 is special; it's not touched by Linux since BIOS
1028  * and SMM (for laptops with [34]86/SL chips) may need it.  It is read
1029  * and write protected to detect null pointer references in the
1030  * kernel.
1031  */
1032 #if 0
1033         memset((void *) 0, 0, PAGE_SIZE);
1034 #endif
1035         start_mem = PAGE_ALIGN(start_mem);
1036         address = 0;
1037         pg_dir = swapper_pg_dir;
1038         while (address < end_mem) {
1039                 tmp = *(pg_dir + 768);          /* at virtual addr 0xC0000000 */
1040                 if (!tmp) {
1041                         tmp = start_mem | PAGE_TABLE;
1042                         *(pg_dir + 768) = tmp;
1043                         start_mem += PAGE_SIZE;
1044                 }
1045                 *pg_dir = tmp;                  /* also map it in at 0x0000000 for init */
1046                 pg_dir++;
1047                 pg_table = (unsigned long *) (tmp & PAGE_MASK);
1048                 for (tmp = 0 ; tmp < PTRS_PER_PAGE ; tmp++,pg_table++) {
1049                         if (address < end_mem)
1050                                 *pg_table = address | PAGE_SHARED;
1051                         else
1052                                 *pg_table = 0;
1053                         address += PAGE_SIZE;
1054                 }
1055         }
1056         invalidate();
1057         return start_mem;
1058 }
1059 
1060 void mem_init(unsigned long start_low_mem,
     /* [previous][next][first][last][top][bottom][index][help] */
1061               unsigned long start_mem, unsigned long end_mem)
1062 {
1063         int codepages = 0;
1064         int reservedpages = 0;
1065         int datapages = 0;
1066         unsigned long tmp;
1067         unsigned short * p;
1068         extern int etext;
1069 
1070         cli();
1071         end_mem &= PAGE_MASK;
1072         high_memory = end_mem;
1073         start_mem +=  0x0000000f;
1074         start_mem &= ~0x0000000f;
1075         tmp = MAP_NR(end_mem);
1076         mem_map = (unsigned short *) start_mem;
1077         p = mem_map + tmp;
1078         start_mem = (unsigned long) p;
1079         while (p > mem_map)
1080                 *--p = MAP_PAGE_RESERVED;
1081         start_low_mem = PAGE_ALIGN(start_low_mem);
1082         start_mem = PAGE_ALIGN(start_mem);
1083         while (start_low_mem < 0xA0000) {
1084                 mem_map[MAP_NR(start_low_mem)] = 0;
1085                 start_low_mem += PAGE_SIZE;
1086         }
1087         while (start_mem < end_mem) {
1088                 mem_map[MAP_NR(start_mem)] = 0;
1089                 start_mem += PAGE_SIZE;
1090         }
1091 #ifdef CONFIG_SOUND
1092         sound_mem_init();
1093 #endif
1094         free_page_list = 0;
1095         nr_free_pages = 0;
1096         for (tmp = 0 ; tmp < end_mem ; tmp += PAGE_SIZE) {
1097                 if (mem_map[MAP_NR(tmp)]) {
1098                         if (tmp >= 0xA0000 && tmp < 0x100000)
1099                                 reservedpages++;
1100                         else if (tmp < (unsigned long) &etext)
1101                                 codepages++;
1102                         else
1103                                 datapages++;
1104                         continue;
1105                 }
1106                 *(unsigned long *) tmp = free_page_list;
1107                 free_page_list = tmp;
1108                 nr_free_pages++;
1109         }
1110         tmp = nr_free_pages << PAGE_SHIFT;
1111         printk("Memory: %luk/%luk available (%dk kernel code, %dk reserved, %dk data)\n",
1112                 tmp >> 10,
1113                 end_mem >> 10,
1114                 codepages << (PAGE_SHIFT-10),
1115                 reservedpages << (PAGE_SHIFT-10),
1116                 datapages << (PAGE_SHIFT-10));
1117 /* test if the WP bit is honoured in supervisor mode */
1118         wp_works_ok = -1;
1119         pg0[0] = PAGE_READONLY;
1120         invalidate();
1121         __asm__ __volatile__("movb 0,%%al ; movb %%al,0": : :"ax", "memory");
1122         pg0[0] = 0;
1123         invalidate();
1124         if (wp_works_ok < 0)
1125                 wp_works_ok = 0;
1126         return;
1127 }
1128 
1129 void si_meminfo(struct sysinfo *val)
     /* [previous][next][first][last][top][bottom][index][help] */
1130 {
1131         int i;
1132 
1133         i = high_memory >> PAGE_SHIFT;
1134         val->totalram = 0;
1135         val->freeram = 0;
1136         val->sharedram = 0;
1137         val->bufferram = buffermem;
1138         while (i-- > 0)  {
1139                 if (mem_map[i] & MAP_PAGE_RESERVED)
1140                         continue;
1141                 val->totalram++;
1142                 if (!mem_map[i]) {
1143                         val->freeram++;
1144                         continue;
1145                 }
1146                 val->sharedram += mem_map[i]-1;
1147         }
1148         val->totalram <<= PAGE_SHIFT;
1149         val->freeram <<= PAGE_SHIFT;
1150         val->sharedram <<= PAGE_SHIFT;
1151         return;
1152 }
1153 
1154 
1155 /* This handles a generic mmap of a disk file */
1156 void file_mmap_nopage(int error_code, struct vm_area_struct * area, unsigned long address)
     /* [previous][next][first][last][top][bottom][index][help] */
1157 {
1158         struct inode * inode = area->vm_inode;
1159         unsigned int block;
1160         unsigned long page;
1161         int nr[8];
1162         int i, j;
1163         int prot = area->vm_page_prot;
1164 
1165         address &= PAGE_MASK;
1166         block = address - area->vm_start + area->vm_offset;
1167         block >>= inode->i_sb->s_blocksize_bits;
1168 
1169         page = get_free_page(GFP_KERNEL);
1170         if (share_page(area, area->vm_task, inode, address, error_code, page)) {
1171                 ++area->vm_task->min_flt;
1172                 return;
1173         }
1174 
1175         ++area->vm_task->maj_flt;
1176         if (!page) {
1177                 oom(current);
1178                 put_page(area->vm_task, BAD_PAGE, address, PAGE_PRIVATE);
1179                 return;
1180         }
1181         for (i=0, j=0; i< PAGE_SIZE ; j++, block++, i += inode->i_sb->s_blocksize)
1182                 nr[j] = bmap(inode,block);
1183         if (error_code & PAGE_RW)
1184                 prot |= PAGE_RW | PAGE_DIRTY;
1185         page = bread_page(page, inode->i_dev, nr, inode->i_sb->s_blocksize, prot);
1186 
1187         if (!(prot & PAGE_RW)) {
1188                 if (share_page(area, area->vm_task, inode, address, error_code, page))
1189                         return;
1190         }
1191         if (put_page(area->vm_task,page,address,prot))
1192                 return;
1193         free_page(page);
1194         oom(current);
1195 }
1196 
1197 void file_mmap_free(struct vm_area_struct * area)
     /* [previous][next][first][last][top][bottom][index][help] */
1198 {
1199         if (area->vm_inode)
1200                 iput(area->vm_inode);
1201 #if 0
1202         if (area->vm_inode)
1203                 printk("Free inode %x:%d (%d)\n",area->vm_inode->i_dev, 
1204                                  area->vm_inode->i_ino, area->vm_inode->i_count);
1205 #endif
1206 }
1207 
1208 /*
1209  * Compare the contents of the mmap entries, and decide if we are allowed to
1210  * share the pages
1211  */
1212 int file_mmap_share(struct vm_area_struct * area1, 
     /* [previous][next][first][last][top][bottom][index][help] */
1213                     struct vm_area_struct * area2, 
1214                     unsigned long address)
1215 {
1216         if (area1->vm_inode != area2->vm_inode)
1217                 return 0;
1218         if (area1->vm_start != area2->vm_start)
1219                 return 0;
1220         if (area1->vm_end != area2->vm_end)
1221                 return 0;
1222         if (area1->vm_offset != area2->vm_offset)
1223                 return 0;
1224         if (area1->vm_page_prot != area2->vm_page_prot)
1225                 return 0;
1226         return 1;
1227 }
1228 
1229 struct vm_operations_struct file_mmap = {
1230         NULL,                   /* open */
1231         file_mmap_free,         /* close */
1232         file_mmap_nopage,       /* nopage */
1233         NULL,                   /* wppage */
1234         file_mmap_share,        /* share */
1235         NULL,                   /* unmap */
1236 };

/* [previous][next][first][last][top][bottom][index][help] */