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. verify_area
  14. get_empty_page
  15. try_to_share
  16. share_page
  17. get_empty_pgtable
  18. do_no_page
  19. do_page_fault
  20. __bad_pagetable
  21. __bad_page
  22. __zero_page
  23. show_mem
  24. paging_init
  25. mem_init
  26. si_meminfo
  27. file_mmap_nopage
  28. file_mmap_free
  29. file_mmap_share

   1 /*
   2  *  linux/mm/memory.c
   3  *
   4  *  Copyright (C) 1991, 1992, 1993, 1994  Linus Torvalds
   5  */
   6 
   7 /*
   8  * demand-loading started 01.12.91 - seems it is high on the list of
   9  * things wanted, and it should be easy to implement. - Linus
  10  */
  11 
  12 /*
  13  * Ok, demand-loading was easy, shared pages a little bit tricker. Shared
  14  * pages started 02.12.91, seems to work. - Linus.
  15  *
  16  * Tested sharing by executing about 30 /bin/sh: under the old kernel it
  17  * would have taken more than the 6M I have free, but it worked well as
  18  * far as I could see.
  19  *
  20  * Also corrected some "invalidate()"s - I wasn't doing enough of them.
  21  */
  22 
  23 /*
  24  * Real VM (paging to/from disk) started 18.12.91. Much more work and
  25  * thought has to go into this. Oh, well..
  26  * 19.12.91  -  works, somewhat. Sometimes I get faults, don't know why.
  27  *              Found it. Everything seems to work now.
  28  * 20.12.91  -  Ok, making the swap-device changeable like the root.
  29  */
  30 
  31 /*
  32  * 05.04.94  -  Multi-page memory management added for v1.1.
  33  *              Idea by Alex Bligh (alex@cconcepts.co.uk)
  34  */
  35 
  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 #include <linux/segment.h>
  49 #include <asm/segment.h>
  50 
  51 /*
  52  * Define this if things work differently on a i386 and a i486:
  53  * it will (on a i486) warn about kernel memory accesses that are
  54  * done without a 'verify_area(VERIFY_WRITE,..)'
  55  */
  56 #undef CONFIG_TEST_VERIFY_AREA
  57 
  58 unsigned long high_memory = 0;
  59 
  60 extern unsigned long pg0[1024];         /* page table for 0-4MB for everybody */
  61 
  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  * The free_area_list arrays point to the queue heads of the free areas
  68  * of different sizes
  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  * oom() prints a message (so that the user knows why the process died),
  84  * and gives the process an untrappable SIGKILL.
  85  */
  86 void oom(struct task_struct * task)
     /* [previous][next][first][last][top][bottom][index][help] */
  87 {
  88         printk("\nOut of memory.\n");
  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)
     /* [previous][next][first][last][top][bottom][index][help] */
  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  * This function clears all user-level page tables of a process - this
 126  * is needed by execve(), so that old pages aren't in the way. Note that
 127  * unlike 'free_page_tables()', this function still leaves a valid
 128  * page-table-tree in memory: it just removes the user pages. The two
 129  * functions are similar, but there is a fundamental difference.
 130  */
 131 void clear_page_tables(struct task_struct * tsk)
     /* [previous][next][first][last][top][bottom][index][help] */
 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  * This function frees up all page tables of a process when it exits.
 168  */
 169 void free_page_tables(struct task_struct * tsk)
     /* [previous][next][first][last][top][bottom][index][help] */
 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  * clone_page_tables() clones the page table for a process - both
 202  * processes will have the exact same pages in memory. There are
 203  * probably races in the memory management with cloning, but we'll
 204  * see..
 205  */
 206 int clone_page_tables(struct task_struct * tsk)
     /* [previous][next][first][last][top][bottom][index][help] */
 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  * copy_page_tables() just copies the whole process memory range:
 218  * note the special handling of RESERVED (ie kernel) pages, which
 219  * means that they are always shared by all processes.
 220  */
 221 int copy_page_tables(struct task_struct * tsk)
     /* [previous][next][first][last][top][bottom][index][help] */
 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");
 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 & (PAGE_RW | PAGE_COW)) == (PAGE_RW | PAGE_COW))
 267                                 pg &= ~PAGE_RW;
 268                         *new_page_table = pg;
 269                         if (mem_map[MAP_NR(pg)] & MAP_PAGE_RESERVED)
 270                                 continue;
 271                         *old_page_table = pg;
 272                         mem_map[MAP_NR(pg)]++;
 273                 }
 274                 *new_page_dir = new_pg_table | PAGE_TABLE;
 275         }
 276         invalidate();
 277         return 0;
 278 }
 279 
 280 /*
 281  * a more complete version of free_page_tables which performs with page
 282  * granularity.
 283  */
 284 int unmap_page_range(unsigned long from, unsigned long size)
     /* [previous][next][first][last][top][bottom][index][help] */
 285 {
 286         unsigned long page, page_dir;
 287         unsigned long *page_table, *dir;
 288         unsigned long poff, pcnt, pc;
 289 
 290         if (from & ~PAGE_MASK) {
 291                 printk("unmap_page_range called with wrong alignment\n");
 292                 return -EINVAL;
 293         }
 294         size = (size + ~PAGE_MASK) >> PAGE_SHIFT;
 295         dir = PAGE_DIR_OFFSET(current->tss.cr3,from);
 296         poff = (from >> PAGE_SHIFT) & (PTRS_PER_PAGE-1);
 297         if ((pcnt = PTRS_PER_PAGE - poff) > size)
 298                 pcnt = size;
 299 
 300         for ( ; size > 0; ++dir, size -= pcnt,
 301              pcnt = (size > PTRS_PER_PAGE ? PTRS_PER_PAGE : size)) {
 302                 if (!(page_dir = *dir)) {
 303                         poff = 0;
 304                         continue;
 305                 }
 306                 if (!(page_dir & PAGE_PRESENT)) {
 307                         printk("unmap_page_range: bad page directory.");
 308                         continue;
 309                 }
 310                 page_table = (unsigned long *)(PAGE_MASK & page_dir);
 311                 if (poff) {
 312                         page_table += poff;
 313                         poff = 0;
 314                 }
 315                 for (pc = pcnt; pc--; page_table++) {
 316                         if ((page = *page_table) != 0) {
 317                                 *page_table = 0;
 318                                 if (1 & page) {
 319                                         if (!(mem_map[MAP_NR(page)] & MAP_PAGE_RESERVED))
 320                                                 if (current->mm->rss > 0)
 321                                                         --current->mm->rss;
 322                                         free_page(PAGE_MASK & page);
 323                                 } else
 324                                         swap_free(page);
 325                         }
 326                 }
 327                 if (pcnt == PTRS_PER_PAGE) {
 328                         *dir = 0;
 329                         free_page(PAGE_MASK & page_dir);
 330                 }
 331         }
 332         invalidate();
 333         return 0;
 334 }
 335 
 336 int zeromap_page_range(unsigned long from, unsigned long size, int mask)
     /* [previous][next][first][last][top][bottom][index][help] */
 337 {
 338         unsigned long *page_table, *dir;
 339         unsigned long poff, pcnt;
 340         unsigned long page;
 341 
 342         if (mask) {
 343                 if ((mask & (PAGE_MASK|PAGE_PRESENT)) != PAGE_PRESENT) {
 344                         printk("zeromap_page_range: mask = %08x\n",mask);
 345                         return -EINVAL;
 346                 }
 347                 mask |= ZERO_PAGE;
 348         }
 349         if (from & ~PAGE_MASK) {
 350                 printk("zeromap_page_range: from = %08lx\n",from);
 351                 return -EINVAL;
 352         }
 353         dir = PAGE_DIR_OFFSET(current->tss.cr3,from);
 354         size = (size + ~PAGE_MASK) >> PAGE_SHIFT;
 355         poff = (from >> PAGE_SHIFT) & (PTRS_PER_PAGE-1);
 356         if ((pcnt = PTRS_PER_PAGE - poff) > size)
 357                 pcnt = size;
 358 
 359         while (size > 0) {
 360                 if (!(PAGE_PRESENT & *dir)) {
 361                                 /* clear page needed here?  SRB. */
 362                         if (!(page_table = (unsigned long*) get_free_page(GFP_KERNEL))) {
 363                                 invalidate();
 364                                 return -ENOMEM;
 365                         }
 366                         if (PAGE_PRESENT & *dir) {
 367                                 free_page((unsigned long) page_table);
 368                                 page_table = (unsigned long *)(PAGE_MASK & *dir++);
 369                         } else
 370                                 *dir++ = ((unsigned long) page_table) | PAGE_TABLE;
 371                 } else
 372                         page_table = (unsigned long *)(PAGE_MASK & *dir++);
 373                 page_table += poff;
 374                 poff = 0;
 375                 for (size -= pcnt; pcnt-- ;) {
 376                         if ((page = *page_table) != 0) {
 377                                 *page_table = 0;
 378                                 if (page & PAGE_PRESENT) {
 379                                         if (!(mem_map[MAP_NR(page)] & MAP_PAGE_RESERVED))
 380                                                 if (current->mm->rss > 0)
 381                                                         --current->mm->rss;
 382                                         free_page(PAGE_MASK & page);
 383                                 } else
 384                                         swap_free(page);
 385                         }
 386                         *page_table++ = mask;
 387                 }
 388                 pcnt = (size > PTRS_PER_PAGE ? PTRS_PER_PAGE : size);
 389         }
 390         invalidate();
 391         return 0;
 392 }
 393 
 394 /*
 395  * maps a range of physical memory into the requested pages. the old
 396  * mappings are removed. any references to nonexistent pages results
 397  * in null mappings (currently treated as "copy-on-access")
 398  */
 399 int remap_page_range(unsigned long from, unsigned long to, unsigned long size, int mask)
     /* [previous][next][first][last][top][bottom][index][help] */
 400 {
 401         unsigned long *page_table, *dir;
 402         unsigned long poff, pcnt;
 403         unsigned long page;
 404 
 405         if (mask) {
 406                 if ((mask & (PAGE_MASK|PAGE_PRESENT)) != PAGE_PRESENT) {
 407                         printk("remap_page_range: mask = %08x\n",mask);
 408                         return -EINVAL;
 409                 }
 410         }
 411         if ((from & ~PAGE_MASK) || (to & ~PAGE_MASK)) {
 412                 printk("remap_page_range: from = %08lx, to=%08lx\n",from,to);
 413                 return -EINVAL;
 414         }
 415         dir = PAGE_DIR_OFFSET(current->tss.cr3,from);
 416         size = (size + ~PAGE_MASK) >> PAGE_SHIFT;
 417         poff = (from >> PAGE_SHIFT) & (PTRS_PER_PAGE-1);
 418         if ((pcnt = PTRS_PER_PAGE - poff) > size)
 419                 pcnt = size;
 420 
 421         while (size > 0) {
 422                 if (!(PAGE_PRESENT & *dir)) {
 423                         /* clearing page here, needed?  SRB. */
 424                         if (!(page_table = (unsigned long*) get_free_page(GFP_KERNEL))) {
 425                                 invalidate();
 426                                 return -1;
 427                         }
 428                         *dir++ = ((unsigned long) page_table) | PAGE_TABLE;
 429                 }
 430                 else
 431                         page_table = (unsigned long *)(PAGE_MASK & *dir++);
 432                 if (poff) {
 433                         page_table += poff;
 434                         poff = 0;
 435                 }
 436 
 437                 for (size -= pcnt; pcnt-- ;) {
 438                         if ((page = *page_table) != 0) {
 439                                 *page_table = 0;
 440                                 if (PAGE_PRESENT & page) {
 441                                         if (!(mem_map[MAP_NR(page)] & MAP_PAGE_RESERVED))
 442                                                 if (current->mm->rss > 0)
 443                                                         --current->mm->rss;
 444                                         free_page(PAGE_MASK & page);
 445                                 } else
 446                                         swap_free(page);
 447                         }
 448 
 449                         /*
 450                          * the first condition should return an invalid access
 451                          * when the page is referenced. current assumptions
 452                          * cause it to be treated as demand allocation in some
 453                          * cases.
 454                          */
 455                         if (!mask)
 456                                 *page_table++ = 0;      /* not present */
 457                         else if (to >= high_memory)
 458                                 *page_table++ = (to | mask);
 459                         else if (!mem_map[MAP_NR(to)])
 460                                 *page_table++ = 0;      /* not present */
 461                         else {
 462                                 *page_table++ = (to | mask);
 463                                 if (!(mem_map[MAP_NR(to)] & MAP_PAGE_RESERVED)) {
 464                                         ++current->mm->rss;
 465                                         mem_map[MAP_NR(to)]++;
 466                                 }
 467                         }
 468                         to += PAGE_SIZE;
 469                 }
 470                 pcnt = (size > PTRS_PER_PAGE ? PTRS_PER_PAGE : size);
 471         }
 472         invalidate();
 473         return 0;
 474 }
 475 
 476 /*
 477  * This function puts a page in memory at the wanted address.
 478  * It returns the physical address of the page gotten, 0 if
 479  * out of memory (either when trying to access page-table or
 480  * page.)
 481  */
 482 unsigned long put_page(struct task_struct * tsk,unsigned long page,
     /* [previous][next][first][last][top][bottom][index][help] */
 483         unsigned long address,int prot)
 484 {
 485         unsigned long *page_table;
 486 
 487         if ((prot & (PAGE_MASK|PAGE_PRESENT)) != PAGE_PRESENT)
 488                 printk("put_page: prot = %08x\n",prot);
 489         if (page >= high_memory) {
 490                 printk("put_page: trying to put page %08lx at %08lx\n",page,address);
 491                 return 0;
 492         }
 493         page_table = PAGE_DIR_OFFSET(tsk->tss.cr3,address);
 494         if ((*page_table) & PAGE_PRESENT)
 495                 page_table = (unsigned long *) (PAGE_MASK & *page_table);
 496         else {
 497                 printk("put_page: bad page directory entry\n");
 498                 oom(tsk);
 499                 *page_table = BAD_PAGETABLE | PAGE_TABLE;
 500                 return 0;
 501         }
 502         page_table += (address >> PAGE_SHIFT) & (PTRS_PER_PAGE-1);
 503         if (*page_table) {
 504                 printk("put_page: page already exists\n");
 505                 *page_table = 0;
 506                 invalidate();
 507         }
 508         *page_table = page | prot;
 509 /* no need for invalidate */
 510         return page;
 511 }
 512 
 513 /*
 514  * The previous function doesn't work very well if you also want to mark
 515  * the page dirty: exec.c wants this, as it has earlier changed the page,
 516  * and we want the dirty-status to be correct (for VM). Thus the same
 517  * routine, but this time we mark it dirty too.
 518  */
 519 unsigned long put_dirty_page(struct task_struct * tsk, unsigned long page, unsigned long address)
     /* [previous][next][first][last][top][bottom][index][help] */
 520 {
 521         unsigned long tmp, *page_table;
 522 
 523         if (page >= high_memory)
 524                 printk("put_dirty_page: trying to put page %08lx at %08lx\n",page,address);
 525         if (mem_map[MAP_NR(page)] != 1)
 526                 printk("mem_map disagrees with %08lx at %08lx\n",page,address);
 527         page_table = PAGE_DIR_OFFSET(tsk->tss.cr3,address);
 528         if (PAGE_PRESENT & *page_table)
 529                 page_table = (unsigned long *) (PAGE_MASK & *page_table);
 530         else {
 531                 if (!(tmp = get_free_page(GFP_KERNEL)))
 532                         return 0;
 533                 if (PAGE_PRESENT & *page_table) {
 534                         free_page(tmp);
 535                         page_table = (unsigned long *) (PAGE_MASK & *page_table);
 536                 } else {
 537                         *page_table = tmp | PAGE_TABLE;
 538                         page_table = (unsigned long *) tmp;
 539                 }
 540         }
 541         page_table += (address >> PAGE_SHIFT) & (PTRS_PER_PAGE-1);
 542         if (*page_table) {
 543                 printk("put_dirty_page: page already exists\n");
 544                 *page_table = 0;
 545                 invalidate();
 546         }
 547         *page_table = page | (PAGE_DIRTY | PAGE_PRIVATE);
 548 /* no need for invalidate */
 549         return page;
 550 }
 551 
 552 /*
 553  * This routine handles present pages, when users try to write
 554  * to a shared page. It is done by copying the page to a new address
 555  * and decrementing the shared-page counter for the old page.
 556  *
 557  * Goto-purists beware: the only reason for goto's here is that it results
 558  * in better assembly code.. The "default" path will see no jumps at all.
 559  */
 560 void do_wp_page(struct vm_area_struct * vma, unsigned long address,
     /* [previous][next][first][last][top][bottom][index][help] */
 561         unsigned long error_code)
 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(vma->vm_task->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         vma->vm_task->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                                 ++vma->vm_task->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(vma->vm_task);
 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, vma->vm_task, 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, vma->vm_task, 1);
 615 end_wp_page:
 616         if (new_page)
 617                 free_page(new_page);
 618         return;
 619 }
 620 
 621 /*
 622  * Ugly, ugly, but the goto's result in better assembly..
 623  */
 624 int verify_area(int type, const void * addr, unsigned long size)
     /* [previous][next][first][last][top][bottom][index][help] */
 625 {
 626         struct vm_area_struct * vma;
 627         unsigned long start = (unsigned long) addr;
 628 
 629         /* If the current user space is mapped to kernel space (for the
 630          * case where we use a fake user buffer with get_fs/set_fs()) we
 631          * don't expect to find the address in the user vm map.
 632          */
 633         if (get_fs() == get_ds())
 634                 return 0;
 635 
 636         for (vma = current->mm->mmap ; ; vma = vma->vm_next) {
 637                 if (!vma)
 638                         goto bad_area;
 639                 if (vma->vm_end > start)
 640                         break;
 641         }
 642         if (vma->vm_start <= start)
 643                 goto good_area;
 644         if (!(vma->vm_flags & VM_GROWSDOWN))
 645                 goto bad_area;
 646         if (vma->vm_end - start > current->rlim[RLIMIT_STACK].rlim_cur)
 647                 goto bad_area;
 648 
 649 good_area:
 650         if (!wp_works_ok && type == VERIFY_WRITE)
 651                 goto check_wp_fault_by_hand;
 652         for (;;) {
 653                 struct vm_area_struct * next;
 654                 if (type != VERIFY_READ && !(vma->vm_page_prot & (PAGE_COW | PAGE_RW)))
 655                         goto bad_area;
 656                 if (vma->vm_end - start >= size)
 657                         return 0;
 658                 next = vma->vm_next;
 659                 if (!next || vma->vm_end != next->vm_start)
 660                         goto bad_area;
 661                 vma = next;
 662         }
 663 
 664 check_wp_fault_by_hand:
 665         size--;
 666         size += start & ~PAGE_MASK;
 667         size >>= PAGE_SHIFT;
 668         start &= PAGE_MASK;
 669 
 670         for (;;) {
 671                 if (!(vma->vm_page_prot & (PAGE_COW | PAGE_RW)))
 672                         goto bad_area;
 673                 do_wp_page(vma, start, PAGE_PRESENT);
 674                 if (!size)
 675                         return 0;
 676                 size--;
 677                 start += PAGE_SIZE;
 678                 if (start < vma->vm_end)
 679                         continue;
 680                 vma = vma->vm_next;
 681                 if (!vma || vma->vm_start != start)
 682                         break;
 683         }
 684 
 685 bad_area:
 686         return -EFAULT;
 687 }
 688 
 689 static inline void get_empty_page(struct task_struct * tsk, unsigned long address)
     /* [previous][next][first][last][top][bottom][index][help] */
 690 {
 691         unsigned long tmp;
 692 
 693         if (!(tmp = get_free_page(GFP_KERNEL))) {
 694                 oom(tsk);
 695                 tmp = BAD_PAGE;
 696         }
 697         if (!put_page(tsk,tmp,address,PAGE_PRIVATE))
 698                 free_page(tmp);
 699 }
 700 
 701 /*
 702  * try_to_share() checks the page at address "address" in the task "p",
 703  * to see if it exists, and if it is clean. If so, share it with the current
 704  * task.
 705  *
 706  * NOTE! This assumes we have checked that p != current, and that they
 707  * share the same inode and can generally otherwise be shared.
 708  */
 709 static int try_to_share(unsigned long to_address, struct vm_area_struct * to_area,
     /* [previous][next][first][last][top][bottom][index][help] */
 710         unsigned long from_address, struct vm_area_struct * from_area,
 711         unsigned long newpage)
 712 {
 713         unsigned long from;
 714         unsigned long to;
 715         unsigned long from_page;
 716         unsigned long to_page;
 717 
 718         from_page = (unsigned long)PAGE_DIR_OFFSET(from_area->vm_task->tss.cr3,from_address);
 719         to_page = (unsigned long)PAGE_DIR_OFFSET(to_area->vm_task->tss.cr3,to_address);
 720 /* is there a page-directory at from? */
 721         from = *(unsigned long *) from_page;
 722         if (!(from & PAGE_PRESENT))
 723                 return 0;
 724         from &= PAGE_MASK;
 725         from_page = from + PAGE_PTR(from_address);
 726         from = *(unsigned long *) from_page;
 727 /* is the page clean and present? */
 728         if ((from & (PAGE_PRESENT | PAGE_DIRTY)) != PAGE_PRESENT)
 729                 return 0;
 730         if (from >= high_memory)
 731                 return 0;
 732         if (mem_map[MAP_NR(from)] & MAP_PAGE_RESERVED)
 733                 return 0;
 734 /* is the destination ok? */
 735         to = *(unsigned long *) to_page;
 736         if (!(to & PAGE_PRESENT))
 737                 return 0;
 738         to &= PAGE_MASK;
 739         to_page = to + PAGE_PTR(to_address);
 740         if (*(unsigned long *) to_page)
 741                 return 0;
 742 /* do we copy? */
 743         if (newpage) {
 744                 copy_page((from & PAGE_MASK), newpage);
 745                 *(unsigned long *) to_page = newpage | to_area->vm_page_prot;
 746                 return 1;
 747         }
 748 /* just share them.. */
 749         mem_map[MAP_NR(from)]++;
 750 /* fill in the 'to' field, checking for COW-stuff */
 751         to = (from & PAGE_MASK) | to_area->vm_page_prot;
 752         if (to & PAGE_COW)
 753                 to &= ~PAGE_RW;
 754         *(unsigned long *) to_page = to;
 755 /* Check if we need to do anything at all to the 'from' field */
 756         if (!(from & PAGE_RW))
 757                 return 1;
 758         if (!(from_area->vm_page_prot & PAGE_COW))
 759                 return 1;
 760 /* ok, need to mark it read-only, so invalidate aany possible old TB entry */
 761         from &= ~PAGE_RW;
 762         *(unsigned long *) from_page = from;
 763         invalidate();
 764         return 1;
 765 }
 766 
 767 /*
 768  * share_page() tries to find a process that could share a page with
 769  * the current one.
 770  *
 771  * We first check if it is at all feasible by checking inode->i_count.
 772  * It should be >1 if there are other tasks sharing this inode.
 773  */
 774 static int share_page(struct vm_area_struct * area, unsigned long address,
     /* [previous][next][first][last][top][bottom][index][help] */
 775         unsigned long error_code, unsigned long newpage)
 776 {
 777         struct inode * inode;
 778         struct task_struct ** p;
 779         unsigned long offset;
 780         unsigned long from_address;
 781         unsigned long give_page;
 782 
 783         if (!area || !(inode = area->vm_inode) || inode->i_count < 2)
 784                 return 0;
 785         /* do we need to copy or can we just share? */
 786         give_page = 0;
 787         if ((area->vm_page_prot & PAGE_COW) && (error_code & PAGE_RW)) {
 788                 if (!newpage)
 789                         return 0;
 790                 give_page = newpage;
 791         }
 792         offset = address - area->vm_start + area->vm_offset;
 793         for (p = &LAST_TASK ; p > &FIRST_TASK ; --p) {
 794                 struct vm_area_struct * mpnt;
 795                 if (!*p)
 796                         continue;
 797                 if (area->vm_task == *p)
 798                         continue;
 799                 /* Now see if there is something in the VMM that
 800                    we can share pages with */
 801                 for (mpnt = (*p)->mm->mmap; mpnt; mpnt = mpnt->vm_next) {
 802                         /* must be same inode */
 803                         if (mpnt->vm_inode != inode)
 804                                 continue;
 805                         /* offsets must be mutually page-aligned */
 806                         if ((mpnt->vm_offset ^ area->vm_offset) & ~PAGE_MASK)
 807                                 continue;
 808                         /* the other area must actually cover the wanted page.. */
 809                         from_address = offset + mpnt->vm_start - mpnt->vm_offset;
 810                         if (from_address < mpnt->vm_start || from_address >= mpnt->vm_end)
 811                                 continue;
 812                         /* .. NOW we can actually try to use the same physical page */
 813                         if (!try_to_share(address, area, from_address, mpnt, give_page))
 814                                 continue;
 815                         /* free newpage if we never used it.. */
 816                         if (give_page || !newpage)
 817                                 return 1;
 818                         free_page(newpage);
 819                         return 1;
 820                 }
 821         }
 822         return 0;
 823 }
 824 
 825 /*
 826  * fill in an empty page-table if none exists.
 827  */
 828 static inline unsigned long get_empty_pgtable(struct task_struct * tsk,unsigned long address)
     /* [previous][next][first][last][top][bottom][index][help] */
 829 {
 830         unsigned long page;
 831         unsigned long *p;
 832 
 833         p = PAGE_DIR_OFFSET(tsk->tss.cr3,address);
 834         if (PAGE_PRESENT & *p)
 835                 return *p;
 836         if (*p) {
 837                 printk("get_empty_pgtable: bad page-directory entry \n");
 838                 *p = 0;
 839         }
 840         page = get_free_page(GFP_KERNEL);
 841         p = PAGE_DIR_OFFSET(tsk->tss.cr3,address);
 842         if (PAGE_PRESENT & *p) {
 843                 free_page(page);
 844                 return *p;
 845         }
 846         if (*p) {
 847                 printk("get_empty_pgtable: bad page-directory entry \n");
 848                 *p = 0;
 849         }
 850         if (page) {
 851                 *p = page | PAGE_TABLE;
 852                 return *p;
 853         }
 854         oom(current);
 855         *p = BAD_PAGETABLE | PAGE_TABLE;
 856         return 0;
 857 }
 858 
 859 void do_no_page(struct vm_area_struct * vma, unsigned long address,
     /* [previous][next][first][last][top][bottom][index][help] */
 860         unsigned long error_code)
 861 {
 862         unsigned long page, entry, prot;
 863 
 864         page = get_empty_pgtable(vma->vm_task,address);
 865         if (!page)
 866                 return;
 867         page &= PAGE_MASK;
 868         page += PAGE_PTR(address);
 869         entry = *(unsigned long *) page;
 870         if (entry & PAGE_PRESENT)
 871                 return;
 872         if (entry) {
 873                 ++vma->vm_task->mm->rss;
 874                 ++vma->vm_task->mm->maj_flt;
 875                 swap_in((unsigned long *) page);
 876                 return;
 877         }
 878         address &= PAGE_MASK;
 879 
 880         if (!vma->vm_ops || !vma->vm_ops->nopage) {
 881                 ++vma->vm_task->mm->rss;
 882                 ++vma->vm_task->mm->min_flt;
 883                 get_empty_page(vma->vm_task,address);
 884                 return;
 885         }
 886         page = get_free_page(GFP_KERNEL);
 887         if (share_page(vma, address, error_code, page)) {
 888                 ++vma->vm_task->mm->min_flt;
 889                 return;
 890         }
 891         if (!page) {
 892                 oom(current);
 893                 put_page(vma->vm_task, BAD_PAGE, address, PAGE_PRIVATE);
 894                 return;
 895         }
 896         ++vma->vm_task->mm->maj_flt;
 897         ++vma->vm_task->mm->rss;
 898         page = vma->vm_ops->nopage(vma, address, page, error_code);
 899         if (share_page(vma, address, error_code, 0)) {
 900                 free_page(page);
 901                 return;
 902         }
 903         prot = vma->vm_page_prot;
 904         if ((prot & PAGE_COW) && mem_map[MAP_NR(page)] > 1)
 905                 prot &= ~PAGE_RW;
 906         if (put_page(vma->vm_task, page, address, prot))
 907                 return;
 908         free_page(page);
 909         oom(current);
 910 }
 911 
 912 /*
 913  * This routine handles page faults.  It determines the address,
 914  * and the problem, and then passes it off to one of the appropriate
 915  * routines.
 916  */
 917 asmlinkage void do_page_fault(struct pt_regs *regs, unsigned long error_code)
     /* [previous][next][first][last][top][bottom][index][help] */
 918 {
 919         struct vm_area_struct * vma;
 920         unsigned long address;
 921         unsigned long page;
 922 
 923         /* get the address */
 924         __asm__("movl %%cr2,%0":"=r" (address));
 925         for (vma = current->mm->mmap ; ; vma = vma->vm_next) {
 926                 if (!vma)
 927                         goto bad_area;
 928                 if (vma->vm_end > address)
 929                         break;
 930         }
 931         if (vma->vm_start <= address)
 932                 goto good_area;
 933         if (!(vma->vm_flags & VM_GROWSDOWN))
 934                 goto bad_area;
 935         if (vma->vm_end - address > current->rlim[RLIMIT_STACK].rlim_cur)
 936                 goto bad_area;
 937         vma->vm_offset -= vma->vm_start - (address & PAGE_MASK);
 938         vma->vm_start = (address & PAGE_MASK);
 939 /*
 940  * Ok, we have a good vm_area for this memory access, so
 941  * we can handle it..
 942  */
 943 good_area:
 944         if (regs->eflags & VM_MASK) {
 945                 unsigned long bit = (address - 0xA0000) >> PAGE_SHIFT;
 946                 if (bit < 32)
 947                         current->screen_bitmap |= 1 << bit;
 948         }
 949         if (error_code & PAGE_PRESENT) {
 950                 if ((vma->vm_page_prot & (PAGE_RW | PAGE_COW | PAGE_PRESENT)) == PAGE_PRESENT)
 951                         goto bad_area;
 952 #ifdef CONFIG_TEST_VERIFY_AREA
 953                 if (regs->cs == KERNEL_CS)
 954                         printk("WP fault at %08x\n", regs->eip);
 955 #endif
 956                 do_wp_page(vma, address, error_code);
 957                 return;
 958         }
 959         if (!(vma->vm_page_prot & PAGE_PRESENT))
 960                 goto bad_area;
 961         do_no_page(vma, address, error_code);
 962         return;
 963 
 964 /*
 965  * Something tried to access memory that isn't in our memory map..
 966  * Fix it, but check if it's kernel or user first..
 967  */
 968 bad_area:
 969         if (error_code & PAGE_USER) {
 970                 current->tss.cr2 = address;
 971                 current->tss.error_code = error_code;
 972                 current->tss.trap_no = 14;
 973                 send_sig(SIGSEGV, current, 1);
 974                 return;
 975         }
 976 /*
 977  * Oops. The kernel tried to access some bad page. We'll have to
 978  * terminate things with extreme prejudice.
 979  */
 980         if (wp_works_ok < 0 && address == TASK_SIZE && (error_code & PAGE_PRESENT)) {
 981                 wp_works_ok = 1;
 982                 pg0[0] = PAGE_SHARED;
 983                 printk("This processor honours the WP bit even when in supervisor mode. Good.\n");
 984                 return;
 985         }
 986         if ((unsigned long) (address-TASK_SIZE) < PAGE_SIZE) {
 987                 printk(KERN_ALERT "Unable to handle kernel NULL pointer dereference");
 988                 pg0[0] = PAGE_SHARED;
 989         } else
 990                 printk(KERN_ALERT "Unable to handle kernel paging request");
 991         printk(" at virtual address %08lx\n",address);
 992         __asm__("movl %%cr3,%0" : "=r" (page));
 993         printk(KERN_ALERT "current->tss.cr3 = %08lx, %%cr3 = %08lx\n",
 994                 current->tss.cr3, page);
 995         page = ((unsigned long *) page)[address >> 22];
 996         printk(KERN_ALERT "*pde = %08lx\n", page);
 997         if (page & PAGE_PRESENT) {
 998                 page &= PAGE_MASK;
 999                 address &= 0x003ff000;
1000                 page = ((unsigned long *) page)[address >> PAGE_SHIFT];
1001                 printk(KERN_ALERT "*pte = %08lx\n", page);
1002         }
1003         die_if_kernel("Oops", regs, error_code);
1004         do_exit(SIGKILL);
1005 }
1006 
1007 /*
1008  * BAD_PAGE is the page that is used for page faults when linux
1009  * is out-of-memory. Older versions of linux just did a
1010  * do_exit(), but using this instead means there is less risk
1011  * for a process dying in kernel mode, possibly leaving a inode
1012  * unused etc..
1013  *
1014  * BAD_PAGETABLE is the accompanying page-table: it is initialized
1015  * to point to BAD_PAGE entries.
1016  *
1017  * ZERO_PAGE is a special page that is used for zero-initialized
1018  * data and COW.
1019  */
1020 unsigned long __bad_pagetable(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1021 {
1022         extern char empty_bad_page_table[PAGE_SIZE];
1023 
1024         __asm__ __volatile__("cld ; rep ; stosl":
1025                 :"a" (BAD_PAGE + PAGE_TABLE),
1026                  "D" ((long) empty_bad_page_table),
1027                  "c" (PTRS_PER_PAGE)
1028                 :"di","cx");
1029         return (unsigned long) empty_bad_page_table;
1030 }
1031 
1032 unsigned long __bad_page(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1033 {
1034         extern char empty_bad_page[PAGE_SIZE];
1035 
1036         __asm__ __volatile__("cld ; rep ; stosl":
1037                 :"a" (0),
1038                  "D" ((long) empty_bad_page),
1039                  "c" (PTRS_PER_PAGE)
1040                 :"di","cx");
1041         return (unsigned long) empty_bad_page;
1042 }
1043 
1044 unsigned long __zero_page(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1045 {
1046         extern char empty_zero_page[PAGE_SIZE];
1047 
1048         __asm__ __volatile__("cld ; rep ; stosl":
1049                 :"a" (0),
1050                  "D" ((long) empty_zero_page),
1051                  "c" (PTRS_PER_PAGE)
1052                 :"di","cx");
1053         return (unsigned long) empty_zero_page;
1054 }
1055 
1056 void show_mem(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1057 {
1058         int i,free = 0,total = 0,reserved = 0;
1059         int shared = 0;
1060 
1061         printk("Mem-info:\n");
1062         show_free_areas();
1063         printk("Free swap:       %6dkB\n",nr_swap_pages<<(PAGE_SHIFT-10));
1064         i = high_memory >> PAGE_SHIFT;
1065         while (i-- > 0) {
1066                 total++;
1067                 if (mem_map[i] & MAP_PAGE_RESERVED)
1068                         reserved++;
1069                 else if (!mem_map[i])
1070                         free++;
1071                 else
1072                         shared += mem_map[i]-1;
1073         }
1074         printk("%d pages of RAM\n",total);
1075         printk("%d free pages\n",free);
1076         printk("%d reserved pages\n",reserved);
1077         printk("%d pages shared\n",shared);
1078         show_buffers();
1079 #ifdef CONFIG_NET
1080         show_net_buffers();
1081 #endif
1082 }
1083 
1084 extern unsigned long free_area_init(unsigned long, unsigned long);
1085 
1086 /*
1087  * paging_init() sets up the page tables - note that the first 4MB are
1088  * already mapped by head.S.
1089  *
1090  * This routines also unmaps the page at virtual kernel address 0, so
1091  * that we can trap those pesky NULL-reference errors in the kernel.
1092  */
1093 unsigned long paging_init(unsigned long start_mem, unsigned long end_mem)
     /* [previous][next][first][last][top][bottom][index][help] */
1094 {
1095         unsigned long * pg_dir;
1096         unsigned long * pg_table;
1097         unsigned long tmp;
1098         unsigned long address;
1099 
1100 /*
1101  * Physical page 0 is special; it's not touched by Linux since BIOS
1102  * and SMM (for laptops with [34]86/SL chips) may need it.  It is read
1103  * and write protected to detect null pointer references in the
1104  * kernel.
1105  */
1106 #if 0
1107         memset((void *) 0, 0, PAGE_SIZE);
1108 #endif
1109         start_mem = PAGE_ALIGN(start_mem);
1110         address = 0;
1111         pg_dir = swapper_pg_dir;
1112         while (address < end_mem) {
1113                 tmp = *(pg_dir + 768);          /* at virtual addr 0xC0000000 */
1114                 if (!tmp) {
1115                         tmp = start_mem | PAGE_TABLE;
1116                         *(pg_dir + 768) = tmp;
1117                         start_mem += PAGE_SIZE;
1118                 }
1119                 *pg_dir = tmp;                  /* also map it in at 0x0000000 for init */
1120                 pg_dir++;
1121                 pg_table = (unsigned long *) (tmp & PAGE_MASK);
1122                 for (tmp = 0 ; tmp < PTRS_PER_PAGE ; tmp++,pg_table++) {
1123                         if (address < end_mem)
1124                                 *pg_table = address | PAGE_SHARED;
1125                         else
1126                                 *pg_table = 0;
1127                         address += PAGE_SIZE;
1128                 }
1129         }
1130         invalidate();
1131         return free_area_init(start_mem, end_mem);
1132 }
1133 
1134 void mem_init(unsigned long start_low_mem,
     /* [previous][next][first][last][top][bottom][index][help] */
1135               unsigned long start_mem, unsigned long end_mem)
1136 {
1137         int codepages = 0;
1138         int reservedpages = 0;
1139         int datapages = 0;
1140         unsigned long tmp;
1141         extern int etext;
1142 
1143         cli();
1144         end_mem &= PAGE_MASK;
1145         high_memory = end_mem;
1146 
1147         /* mark usable pages in the mem_map[] */
1148         start_low_mem = PAGE_ALIGN(start_low_mem);
1149         start_mem = PAGE_ALIGN(start_mem);
1150 
1151         /*
1152          * IBM messed up *AGAIN* in their thinkpad: 0xA0000 -> 0x9F000.
1153          * They seem to have done something stupid with the floppy
1154          * controller as well..
1155          */
1156         while (start_low_mem < 0x9f000) {
1157                 mem_map[MAP_NR(start_low_mem)] = 0;
1158                 start_low_mem += PAGE_SIZE;
1159         }
1160 
1161         while (start_mem < high_memory) {
1162                 mem_map[MAP_NR(start_mem)] = 0;
1163                 start_mem += PAGE_SIZE;
1164         }
1165 #ifdef CONFIG_SOUND
1166         sound_mem_init();
1167 #endif
1168         for (tmp = 0 ; tmp < high_memory ; tmp += PAGE_SIZE) {
1169                 if (mem_map[MAP_NR(tmp)]) {
1170                         if (tmp >= 0xA0000 && tmp < 0x100000)
1171                                 reservedpages++;
1172                         else if (tmp < (unsigned long) &etext)
1173                                 codepages++;
1174                         else
1175                                 datapages++;
1176                         continue;
1177                 }
1178                 mem_map[MAP_NR(tmp)] = 1;
1179                 free_page(tmp);
1180         }
1181         tmp = nr_free_pages << PAGE_SHIFT;
1182         printk("Memory: %luk/%luk available (%dk kernel code, %dk reserved, %dk data)\n",
1183                 tmp >> 10,
1184                 high_memory >> 10,
1185                 codepages << (PAGE_SHIFT-10),
1186                 reservedpages << (PAGE_SHIFT-10),
1187                 datapages << (PAGE_SHIFT-10));
1188 /* test if the WP bit is honoured in supervisor mode */
1189         wp_works_ok = -1;
1190         pg0[0] = PAGE_READONLY;
1191         invalidate();
1192         __asm__ __volatile__("movb 0,%%al ; movb %%al,0": : :"ax", "memory");
1193         pg0[0] = 0;
1194         invalidate();
1195         if (wp_works_ok < 0)
1196                 wp_works_ok = 0;
1197 #ifdef CONFIG_TEST_VERIFY_AREA
1198         wp_works_ok = 0;
1199 #endif
1200         return;
1201 }
1202 
1203 void si_meminfo(struct sysinfo *val)
     /* [previous][next][first][last][top][bottom][index][help] */
1204 {
1205         int i;
1206 
1207         i = high_memory >> PAGE_SHIFT;
1208         val->totalram = 0;
1209         val->sharedram = 0;
1210         val->freeram = nr_free_pages << PAGE_SHIFT;
1211         val->bufferram = buffermem;
1212         while (i-- > 0)  {
1213                 if (mem_map[i] & MAP_PAGE_RESERVED)
1214                         continue;
1215                 val->totalram++;
1216                 if (!mem_map[i])
1217                         continue;
1218                 val->sharedram += mem_map[i]-1;
1219         }
1220         val->totalram <<= PAGE_SHIFT;
1221         val->sharedram <<= PAGE_SHIFT;
1222         return;
1223 }
1224 
1225 
1226 /* This handles a generic mmap of a disk file */
1227 unsigned long file_mmap_nopage(struct vm_area_struct * area, unsigned long address,
     /* [previous][next][first][last][top][bottom][index][help] */
1228         unsigned long page, int error_code)
1229 {
1230         struct inode * inode = area->vm_inode;
1231         unsigned int block;
1232         int nr[8];
1233         int i, j;
1234         int prot = area->vm_page_prot;
1235 
1236         address &= PAGE_MASK;
1237         block = address - area->vm_start + area->vm_offset;
1238         block >>= inode->i_sb->s_blocksize_bits;
1239 
1240         for (i=0, j=0; i< PAGE_SIZE ; j++, block++, i += inode->i_sb->s_blocksize)
1241                 nr[j] = bmap(inode,block);
1242         if (error_code & PAGE_RW)
1243                 prot |= PAGE_RW | PAGE_DIRTY;
1244         return bread_page(page, inode->i_dev, nr, inode->i_sb->s_blocksize, !(error_code & PAGE_RW));
1245 }
1246 
1247 void file_mmap_free(struct vm_area_struct * area)
     /* [previous][next][first][last][top][bottom][index][help] */
1248 {
1249         if (area->vm_inode)
1250                 iput(area->vm_inode);
1251 #if 0
1252         if (area->vm_inode)
1253                 printk("Free inode %x:%d (%d)\n",area->vm_inode->i_dev, 
1254                                  area->vm_inode->i_ino, area->vm_inode->i_count);
1255 #endif
1256 }
1257 
1258 /*
1259  * Compare the contents of the mmap entries, and decide if we are allowed to
1260  * share the pages
1261  */
1262 int file_mmap_share(struct vm_area_struct * area1, 
     /* [previous][next][first][last][top][bottom][index][help] */
1263                     struct vm_area_struct * area2, 
1264                     unsigned long address)
1265 {
1266         return 1;
1267 }
1268 
1269 struct vm_operations_struct file_mmap = {
1270         NULL,                   /* open */
1271         file_mmap_free,         /* close */
1272         file_mmap_nopage,       /* nopage */
1273         NULL,                   /* wppage */
1274         file_mmap_share,        /* share */
1275         NULL,                   /* unmap */
1276 };

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