root/mm/mmap.c

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

DEFINITIONS

This source file includes following definitions.
  1. do_mmap
  2. get_unmapped_area
  3. sys_mmap
  4. unmap_fixup
  5. sys_munmap
  6. do_munmap
  7. generic_mmap
  8. insert_vm_struct
  9. merge_segments
  10. anon_map

   1 /*
   2  *      linux/mm/mmap.c
   3  *
   4  * Written by obz.
   5  */
   6 #include <linux/stat.h>
   7 #include <linux/sched.h>
   8 #include <linux/kernel.h>
   9 #include <linux/mm.h>
  10 #include <linux/shm.h>
  11 #include <linux/errno.h>
  12 #include <linux/mman.h>
  13 #include <linux/string.h>
  14 #include <linux/malloc.h>
  15 
  16 #include <asm/segment.h>
  17 #include <asm/system.h>
  18 
  19 static int anon_map(struct inode *, struct file *, struct vm_area_struct *);
  20 
  21 /*
  22  * description of effects of mapping type and prot in current implementation.
  23  * this is due to the limited x86 page protection hardware.  The expected
  24  * behavior is in parens:
  25  *
  26  * map_type     prot
  27  *              PROT_NONE       PROT_READ       PROT_WRITE      PROT_EXEC
  28  * MAP_SHARED   r: (no) no      r: (yes) yes    r: (no) yes     r: (no) yes
  29  *              w: (no) no      w: (no) no      w: (yes) yes    w: (no) no
  30  *              x: (no) no      x: (no) yes     x: (no) yes     x: (yes) yes
  31  *              
  32  * MAP_PRIVATE  r: (no) no      r: (yes) yes    r: (no) yes     r: (no) yes
  33  *              w: (no) no      w: (no) no      w: (copy) copy  w: (no) no
  34  *              x: (no) no      x: (no) yes     x: (no) yes     x: (yes) yes
  35  *
  36  */
  37 
  38 int do_mmap(struct file * file, unsigned long addr, unsigned long len,
     /* [previous][next][first][last][top][bottom][index][help] */
  39         unsigned long prot, unsigned long flags, unsigned long off)
  40 {
  41         int mask, error;
  42         struct vm_area_struct * vma;
  43 
  44         if ((len = PAGE_ALIGN(len)) == 0)
  45                 return addr;
  46 
  47         if (addr > TASK_SIZE || len > TASK_SIZE || addr > TASK_SIZE-len)
  48                 return -EINVAL;
  49 
  50         /* offset overflow? */
  51         if (off + len < off)
  52                 return -EINVAL;
  53 
  54         /*
  55          * do simple checking here so the lower-level routines won't have
  56          * to. we assume access permissions have been handled by the open
  57          * of the memory object, so we don't do any here.
  58          */
  59 
  60         if (file != NULL) {
  61                 switch (flags & MAP_TYPE) {
  62                 case MAP_SHARED:
  63                         if ((prot & PROT_WRITE) && !(file->f_mode & 2))
  64                                 return -EACCES;
  65                         /* fall through */
  66                 case MAP_PRIVATE:
  67                         if (!(file->f_mode & 1))
  68                                 return -EACCES;
  69                         break;
  70 
  71                 default:
  72                         return -EINVAL;
  73                 }
  74                 if ((flags & MAP_DENYWRITE) && (file->f_inode->i_wcount > 0))
  75                         return -ETXTBSY;
  76         } else if ((flags & MAP_TYPE) == MAP_SHARED)
  77                 return -EINVAL;
  78 
  79         /*
  80          * obtain the address to map to. we verify (or select) it and ensure
  81          * that it represents a valid section of the address space.
  82          */
  83 
  84         if (flags & MAP_FIXED) {
  85                 if (addr & ~PAGE_MASK)
  86                         return -EINVAL;
  87                 if (len > TASK_SIZE || addr > TASK_SIZE - len)
  88                         return -EINVAL;
  89         } else {
  90                 addr = get_unmapped_area(len);
  91                 if (!addr)
  92                         return -ENOMEM;
  93         }
  94 
  95         /*
  96          * determine the object being mapped and call the appropriate
  97          * specific mapper. the address has already been validated, but
  98          * not unmapped, but the maps are removed from the list.
  99          */
 100         if (file && (!file->f_op || !file->f_op->mmap))
 101                 return -ENODEV;
 102         mask = PAGE_PRESENT;
 103         if (prot & (PROT_READ | PROT_EXEC))
 104                 mask |= PAGE_READONLY;
 105         if (prot & PROT_WRITE)
 106                 if ((flags & MAP_TYPE) == MAP_PRIVATE)
 107                         mask |= PAGE_COPY;
 108                 else
 109                         mask |= PAGE_SHARED;
 110 
 111         vma = (struct vm_area_struct *)kmalloc(sizeof(struct vm_area_struct),
 112                 GFP_KERNEL);
 113         if (!vma)
 114                 return -ENOMEM;
 115 
 116         vma->vm_task = current;
 117         vma->vm_start = addr;
 118         vma->vm_end = addr + len;
 119         vma->vm_page_prot = mask;
 120         vma->vm_flags = prot & (VM_READ | VM_WRITE | VM_EXEC);
 121         vma->vm_flags |= flags & (VM_GROWSDOWN | VM_DENYWRITE | VM_EXECUTABLE);
 122 
 123         if (file) {
 124                 if (file->f_mode & 1)
 125                         vma->vm_flags |= VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
 126                 if (flags & MAP_SHARED) {
 127                         vma->vm_flags |= VM_SHARED | VM_MAYSHARE;
 128                         if (!(file->f_mode & 2))
 129                                 vma->vm_flags &= ~VM_MAYWRITE;
 130                 }
 131         } else
 132                 vma->vm_flags |= VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
 133         vma->vm_ops = NULL;
 134         vma->vm_offset = off;
 135         vma->vm_inode = NULL;
 136         vma->vm_pte = 0;
 137 
 138         do_munmap(addr, len);   /* Clear old maps */
 139 
 140         if (file)
 141                 error = file->f_op->mmap(file->f_inode, file, vma);
 142         else
 143                 error = anon_map(NULL, NULL, vma);
 144         
 145         if (error) {
 146                 kfree(vma);
 147                 return error;
 148         }
 149         insert_vm_struct(current, vma);
 150         merge_segments(current->mm->mmap);
 151         return addr;
 152 }
 153 
 154 /*
 155  * Get an address range which is currently unmapped.
 156  * For mmap() without MAP_FIXED and shmat() with addr=0.
 157  * Return value 0 means ENOMEM.
 158  */
 159 unsigned long get_unmapped_area(unsigned long len)
     /* [previous][next][first][last][top][bottom][index][help] */
 160 {
 161         struct vm_area_struct * vmm;
 162         unsigned long gap_start = 0, gap_end;
 163 
 164         for (vmm = current->mm->mmap; ; vmm = vmm->vm_next) {
 165                 if (gap_start < SHM_RANGE_START)
 166                         gap_start = SHM_RANGE_START;
 167                 if (!vmm || ((gap_end = vmm->vm_start) > SHM_RANGE_END))
 168                         gap_end = SHM_RANGE_END;
 169                 gap_start = PAGE_ALIGN(gap_start);
 170                 gap_end &= PAGE_MASK;
 171                 if ((gap_start <= gap_end) && (gap_end - gap_start >= len))
 172                         return gap_start;
 173                 if (!vmm)
 174                         return 0;
 175                 gap_start = vmm->vm_end;
 176         }
 177 }
 178 
 179 asmlinkage int sys_mmap(unsigned long *buffer)
     /* [previous][next][first][last][top][bottom][index][help] */
 180 {
 181         int error;
 182         unsigned long flags;
 183         struct file * file = NULL;
 184 
 185         error = verify_area(VERIFY_READ, buffer, 6*sizeof(long));
 186         if (error)
 187                 return error;
 188         flags = get_fs_long(buffer+3);
 189         if (!(flags & MAP_ANONYMOUS)) {
 190                 unsigned long fd = get_fs_long(buffer+4);
 191                 if (fd >= NR_OPEN || !(file = current->files->fd[fd]))
 192                         return -EBADF;
 193         }
 194         return do_mmap(file, get_fs_long(buffer), get_fs_long(buffer+1),
 195                 get_fs_long(buffer+2), flags, get_fs_long(buffer+5));
 196 }
 197 
 198 /*
 199  * Normal function to fix up a mapping
 200  * This function is the default for when an area has no specific
 201  * function.  This may be used as part of a more specific routine.
 202  * This function works out what part of an area is affected and
 203  * adjusts the mapping information.  Since the actual page
 204  * manipulation is done in do_mmap(), none need be done here,
 205  * though it would probably be more appropriate.
 206  *
 207  * By the time this function is called, the area struct has been
 208  * removed from the process mapping list, so it needs to be
 209  * reinserted if necessary.
 210  *
 211  * The 4 main cases are:
 212  *    Unmapping the whole area
 213  *    Unmapping from the start of the segment to a point in it
 214  *    Unmapping from an intermediate point to the end
 215  *    Unmapping between to intermediate points, making a hole.
 216  *
 217  * Case 4 involves the creation of 2 new areas, for each side of
 218  * the hole.
 219  */
 220 void unmap_fixup(struct vm_area_struct *area,
     /* [previous][next][first][last][top][bottom][index][help] */
 221                  unsigned long addr, size_t len)
 222 {
 223         struct vm_area_struct *mpnt;
 224         unsigned long end = addr + len;
 225 
 226         if (addr < area->vm_start || addr >= area->vm_end ||
 227             end <= area->vm_start || end > area->vm_end ||
 228             end < addr)
 229         {
 230                 printk("unmap_fixup: area=%lx-%lx, unmap %lx-%lx!!\n",
 231                        area->vm_start, area->vm_end, addr, end);
 232                 return;
 233         }
 234 
 235         /* Unmapping the whole area */
 236         if (addr == area->vm_start && end == area->vm_end) {
 237                 if (area->vm_ops && area->vm_ops->close)
 238                         area->vm_ops->close(area);
 239                 if (area->vm_inode)
 240                         iput(area->vm_inode);
 241                 return;
 242         }
 243 
 244         /* Work out to one of the ends */
 245         if (addr >= area->vm_start && end == area->vm_end)
 246                 area->vm_end = addr;
 247         if (addr == area->vm_start && end <= area->vm_end) {
 248                 area->vm_offset += (end - area->vm_start);
 249                 area->vm_start = end;
 250         }
 251 
 252         /* Unmapping a hole */
 253         if (addr > area->vm_start && end < area->vm_end)
 254         {
 255                 /* Add end mapping -- leave beginning for below */
 256                 mpnt = (struct vm_area_struct *)kmalloc(sizeof(*mpnt), GFP_KERNEL);
 257 
 258                 if (!mpnt)
 259                         return;
 260                 *mpnt = *area;
 261                 mpnt->vm_offset += (end - area->vm_start);
 262                 mpnt->vm_start = end;
 263                 if (mpnt->vm_inode)
 264                         mpnt->vm_inode->i_count++;
 265                 if (mpnt->vm_ops && mpnt->vm_ops->open)
 266                         mpnt->vm_ops->open(mpnt);
 267                 area->vm_end = addr;    /* Truncate area */
 268                 insert_vm_struct(current, mpnt);
 269         }
 270 
 271         /* construct whatever mapping is needed */
 272         mpnt = (struct vm_area_struct *)kmalloc(sizeof(*mpnt), GFP_KERNEL);
 273         if (!mpnt)
 274                 return;
 275         *mpnt = *area;
 276         if (mpnt->vm_ops && mpnt->vm_ops->open)
 277                 mpnt->vm_ops->open(mpnt);
 278         if (area->vm_ops && area->vm_ops->close) {
 279                 area->vm_end = area->vm_start;
 280                 area->vm_ops->close(area);
 281         }
 282         insert_vm_struct(current, mpnt);
 283 }
 284 
 285 asmlinkage int sys_munmap(unsigned long addr, size_t len)
     /* [previous][next][first][last][top][bottom][index][help] */
 286 {
 287         return do_munmap(addr, len);
 288 }
 289 
 290 /*
 291  * Munmap is split into 2 main parts -- this part which finds
 292  * what needs doing, and the areas themselves, which do the
 293  * work.  This now handles partial unmappings.
 294  * Jeremy Fitzhardine <jeremy@sw.oz.au>
 295  */
 296 int do_munmap(unsigned long addr, size_t len)
     /* [previous][next][first][last][top][bottom][index][help] */
 297 {
 298         struct vm_area_struct *mpnt, **npp, *free;
 299 
 300         if ((addr & ~PAGE_MASK) || addr > TASK_SIZE || len > TASK_SIZE-addr)
 301                 return -EINVAL;
 302 
 303         if ((len = PAGE_ALIGN(len)) == 0)
 304                 return 0;
 305 
 306         /*
 307          * Check if this memory area is ok - put it on the temporary
 308          * list if so..  The checks here are pretty simple --
 309          * every area affected in some way (by any overlap) is put
 310          * on the list.  If nothing is put on, nothing is affected.
 311          */
 312         npp = &current->mm->mmap;
 313         free = NULL;
 314         for (mpnt = *npp; mpnt != NULL; mpnt = *npp) {
 315                 unsigned long end = addr+len;
 316 
 317                 if ((addr < mpnt->vm_start && end <= mpnt->vm_start) ||
 318                     (addr >= mpnt->vm_end && end > mpnt->vm_end))
 319                 {
 320                         npp = &mpnt->vm_next;
 321                         continue;
 322                 }
 323 
 324                 *npp = mpnt->vm_next;
 325                 mpnt->vm_next = free;
 326                 free = mpnt;
 327         }
 328 
 329         if (free == NULL)
 330                 return 0;
 331 
 332         /*
 333          * Ok - we have the memory areas we should free on the 'free' list,
 334          * so release them, and unmap the page range..
 335          * If the one of the segments is only being partially unmapped,
 336          * it will put new vm_area_struct(s) into the address space.
 337          */
 338         while (free) {
 339                 unsigned long st, end;
 340 
 341                 mpnt = free;
 342                 free = free->vm_next;
 343 
 344                 st = addr < mpnt->vm_start ? mpnt->vm_start : addr;
 345                 end = addr+len;
 346                 end = end > mpnt->vm_end ? mpnt->vm_end : end;
 347 
 348                 if (mpnt->vm_ops && mpnt->vm_ops->unmap)
 349                         mpnt->vm_ops->unmap(mpnt, st, end-st);
 350                 else
 351                         unmap_fixup(mpnt, st, end-st);
 352 
 353                 kfree(mpnt);
 354         }
 355 
 356         unmap_page_range(addr, len);
 357         return 0;
 358 }
 359 
 360 /* This is used for a general mmap of a disk file */
 361 int generic_mmap(struct inode * inode, struct file * file, struct vm_area_struct * vma)
     /* [previous][next][first][last][top][bottom][index][help] */
 362 {
 363         extern struct vm_operations_struct file_mmap;
 364 
 365         if (vma->vm_page_prot & PAGE_RW)        /* only PAGE_COW or read-only supported right now */
 366                 return -EINVAL;
 367         if (vma->vm_offset & (inode->i_sb->s_blocksize - 1))
 368                 return -EINVAL;
 369         if (!inode->i_sb || !S_ISREG(inode->i_mode))
 370                 return -EACCES;
 371         if (!inode->i_op || !inode->i_op->bmap)
 372                 return -ENOEXEC;
 373         if (!IS_RDONLY(inode)) {
 374                 inode->i_atime = CURRENT_TIME;
 375                 inode->i_dirt = 1;
 376         }
 377         vma->vm_inode = inode;
 378         inode->i_count++;
 379         vma->vm_ops = &file_mmap;
 380         return 0;
 381 }
 382 
 383 /*
 384  * Insert vm structure into process list sorted by address.
 385  */
 386 void insert_vm_struct(struct task_struct *t, struct vm_area_struct *vmp)
     /* [previous][next][first][last][top][bottom][index][help] */
 387 {
 388         struct vm_area_struct **p, *mpnt;
 389 
 390         p = &t->mm->mmap;
 391         while ((mpnt = *p) != NULL) {
 392                 if (mpnt->vm_start > vmp->vm_start)
 393                         break;
 394                 if (mpnt->vm_end > vmp->vm_start)
 395                         printk("insert_vm_struct: overlapping memory areas\n");
 396                 p = &mpnt->vm_next;
 397         }
 398         vmp->vm_next = mpnt;
 399         *p = vmp;
 400 }
 401 
 402 /*
 403  * Merge a list of memory segments if possible.
 404  * Redundant vm_area_structs are freed.
 405  * This assumes that the list is ordered by address.
 406  */
 407 void merge_segments(struct vm_area_struct *mpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
 408 {
 409         struct vm_area_struct *prev, *next;
 410 
 411         if (mpnt == NULL)
 412                 return;
 413         
 414         for(prev = mpnt, mpnt = mpnt->vm_next;
 415             mpnt != NULL;
 416             prev = mpnt, mpnt = next)
 417         {
 418                 next = mpnt->vm_next;
 419 
 420                 /*
 421                  * To share, we must have the same inode, operations.. 
 422                  */
 423                 if (mpnt->vm_inode != prev->vm_inode)
 424                         continue;
 425                 if (mpnt->vm_pte != prev->vm_pte)
 426                         continue;
 427                 if (mpnt->vm_ops != prev->vm_ops)
 428                         continue;
 429                 if (mpnt->vm_page_prot != prev->vm_page_prot ||
 430                     mpnt->vm_flags != prev->vm_flags)
 431                         continue;
 432                 if (prev->vm_end != mpnt->vm_start)
 433                         continue;
 434                 /*
 435                  * and if we have an inode, the offsets must be contiguous..
 436                  */
 437                 if ((mpnt->vm_inode != NULL) || (mpnt->vm_flags & VM_SHM)) {
 438                         if (prev->vm_offset + prev->vm_end - prev->vm_start != mpnt->vm_offset)
 439                                 continue;
 440                 }
 441 
 442                 /*
 443                  * merge prev with mpnt and set up pointers so the new
 444                  * big segment can possibly merge with the next one.
 445                  * The old unused mpnt is freed.
 446                  */
 447                 prev->vm_end = mpnt->vm_end;
 448                 prev->vm_next = mpnt->vm_next;
 449                 if (mpnt->vm_ops && mpnt->vm_ops->close) {
 450                         mpnt->vm_offset += mpnt->vm_end - mpnt->vm_start;
 451                         mpnt->vm_start = mpnt->vm_end;
 452                         mpnt->vm_ops->close(mpnt);
 453                 }
 454                 if (mpnt->vm_inode)
 455                         mpnt->vm_inode->i_count--;
 456                 kfree_s(mpnt, sizeof(*mpnt));
 457                 mpnt = prev;
 458         }
 459 }
 460 
 461 /*
 462  * Map memory not associated with any file into a process
 463  * address space.  Adjacent memory is merged.
 464  */
 465 static int anon_map(struct inode *ino, struct file * file, struct vm_area_struct * vma)
     /* [previous][next][first][last][top][bottom][index][help] */
 466 {
 467         if (zeromap_page_range(vma->vm_start, vma->vm_end - vma->vm_start, vma->vm_page_prot))
 468                 return -ENOMEM;
 469         return 0;
 470 }

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