root/include/linux/mm.h

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

INCLUDED FROM


DEFINITIONS

This source file includes following definitions.
  1. get_free_page
  2. expand_stack
  3. find_vma
  4. find_vma_intersection

   1 #ifndef _LINUX_MM_H
   2 #define _LINUX_MM_H
   3 
   4 #include <linux/sched.h>
   5 #include <linux/errno.h>
   6 #include <linux/kernel.h>
   7 
   8 #ifdef __KERNEL__
   9 
  10 #include <linux/string.h>
  11 
  12 extern unsigned long high_memory;
  13 
  14 #include <asm/page.h>
  15 #include <asm/atomic.h>
  16 
  17 #define VERIFY_READ 0
  18 #define VERIFY_WRITE 1
  19 
  20 extern int verify_area(int, const void *, unsigned long);
  21 
  22 /*
  23  * Linux kernel virtual memory manager primitives.
  24  * The idea being to have a "virtual" mm in the same way
  25  * we have a virtual fs - giving a cleaner interface to the
  26  * mm details, and allowing different kinds of memory mappings
  27  * (from shared memory to executable loading to arbitrary
  28  * mmap() functions).
  29  */
  30 
  31 /*
  32  * This struct defines a memory VMM memory area. There is one of these
  33  * per VM-area/task.  A VM area is any part of the process virtual memory
  34  * space that has a special rule for the page-fault handlers (ie a shared
  35  * library, the executable area etc).
  36  */
  37 struct vm_area_struct {
  38         struct mm_struct * vm_mm;       /* VM area parameters */
  39         unsigned long vm_start;
  40         unsigned long vm_end;
  41         pgprot_t vm_page_prot;
  42         unsigned short vm_flags;
  43 /* AVL tree of VM areas per task, sorted by address */
  44         short vm_avl_height;
  45         struct vm_area_struct * vm_avl_left;
  46         struct vm_area_struct * vm_avl_right;
  47 /* linked list of VM areas per task, sorted by address */
  48         struct vm_area_struct * vm_next;
  49 /* for areas with inode, the circular list inode->i_mmap */
  50 /* for shm areas, the circular list of attaches */
  51 /* otherwise unused */
  52         struct vm_area_struct * vm_next_share;
  53         struct vm_area_struct * vm_prev_share;
  54 /* more */
  55         struct vm_operations_struct * vm_ops;
  56         unsigned long vm_offset;
  57         struct inode * vm_inode;
  58         unsigned long vm_pte;                   /* shared mem */
  59 };
  60 
  61 /*
  62  * vm_flags..
  63  */
  64 #define VM_READ         0x0001  /* currently active flags */
  65 #define VM_WRITE        0x0002
  66 #define VM_EXEC         0x0004
  67 #define VM_SHARED       0x0008
  68 
  69 #define VM_MAYREAD      0x0010  /* limits for mprotect() etc */
  70 #define VM_MAYWRITE     0x0020
  71 #define VM_MAYEXEC      0x0040
  72 #define VM_MAYSHARE     0x0080
  73 
  74 #define VM_GROWSDOWN    0x0100  /* general info on the segment */
  75 #define VM_GROWSUP      0x0200
  76 #define VM_SHM          0x0400  /* shared memory area, don't swap out */
  77 #define VM_DENYWRITE    0x0800  /* ETXTBSY on write attempts.. */
  78 
  79 #define VM_EXECUTABLE   0x1000
  80 #define VM_LOCKED       0x2000
  81 
  82 #define VM_STACK_FLAGS  0x0177
  83 
  84 /*
  85  * mapping from the currently active vm_flags protection bits (the
  86  * low four bits) to a page protection mask..
  87  */
  88 extern pgprot_t protection_map[16];
  89 
  90 
  91 /*
  92  * These are the virtual MM functions - opening of an area, closing and
  93  * unmapping it (needed to keep files on disk up-to-date etc), pointer
  94  * to the functions called when a no-page or a wp-page exception occurs. 
  95  */
  96 struct vm_operations_struct {
  97         void (*open)(struct vm_area_struct * area);
  98         void (*close)(struct vm_area_struct * area);
  99         void (*unmap)(struct vm_area_struct *area, unsigned long, size_t);
 100         void (*protect)(struct vm_area_struct *area, unsigned long, size_t, unsigned int newprot);
 101         int (*sync)(struct vm_area_struct *area, unsigned long, size_t, unsigned int flags);
 102         void (*advise)(struct vm_area_struct *area, unsigned long, size_t, unsigned int advise);
 103         unsigned long (*nopage)(struct vm_area_struct * area, unsigned long address, int write_access);
 104         unsigned long (*wppage)(struct vm_area_struct * area, unsigned long address,
 105                 unsigned long page);
 106         int (*swapout)(struct vm_area_struct *,  unsigned long, pte_t *);
 107         pte_t (*swapin)(struct vm_area_struct *, unsigned long, unsigned long);
 108 };
 109 
 110 /*
 111  * Try to keep the most commonly accessed fields in single cache lines
 112  * here (16 bytes or greater).  This ordering should be particularly
 113  * beneficial on 32-bit processors.
 114  *
 115  * The first line is data used in linear searches (eg. clock algorithm
 116  * scans).  The second line is data used in page searches through the
 117  * page-cache.  -- sct 
 118  */
 119 typedef struct page {
 120         atomic_t count;
 121         unsigned dirty:16,
 122                  age:8;
 123         unsigned flags; /* atomic flags, some possibly updated asynchronously */
 124         struct wait_queue *wait;
 125         struct page *next;
 126         struct page *next_hash;
 127         unsigned long offset;
 128         struct inode *inode;
 129         struct page *prev;
 130         struct page *prev_hash;
 131         struct buffer_head * buffers;
 132         unsigned long swap_unlock_entry;
 133         unsigned long map_nr;   /* page->map_nr == page - mem_map */
 134 } mem_map_t;
 135 
 136 /* Page flag bit values */
 137 #define PG_locked                0
 138 #define PG_error                 1
 139 #define PG_referenced            2
 140 #define PG_uptodate              3
 141 #define PG_free_after            4
 142 #define PG_decr_after            5
 143 #define PG_swap_unlock_after     6
 144 #define PG_DMA                   7
 145 #define PG_reserved             31
 146 
 147 /* Make it prettier to test the above... */
 148 #define PageLocked(page)        (test_bit(PG_locked, &(page)->flags))
 149 #define PageError(page)         (test_bit(PG_error, &(page)->flags))
 150 #define PageReferenced(page)    (test_bit(PG_referenced, &(page)->flags))
 151 #define PageDirty(page)         (test_bit(PG_dirty, &(page)->flags))
 152 #define PageUptodate(page)      (test_bit(PG_uptodate, &(page)->flags))
 153 #define PageFreeAfter(page)     (test_bit(PG_free_after, &(page)->flags))
 154 #define PageDecrAfter(page)     (test_bit(PG_decr_after, &(page)->flags))
 155 #define PageSwapUnlockAfter(page) (test_bit(PG_swap_unlock_after, &(page)->flags))
 156 #define PageDMA(page)           (test_bit(PG_DMA, &(page)->flags))
 157 #define PageReserved(page)      (test_bit(PG_reserved, &(page)->flags))
 158 
 159 /*
 160  * page->reserved denotes a page which must never be accessed (which
 161  * may not even be present).
 162  *
 163  * page->dma is set for those pages which lie in the range of
 164  * physical addresses capable of carrying DMA transfers.
 165  *
 166  * Multiple processes may "see" the same page. E.g. for untouched
 167  * mappings of /dev/null, all processes see the same page full of
 168  * zeroes, and text pages of executables and shared libraries have
 169  * only one copy in memory, at most, normally.
 170  *
 171  * For the non-reserved pages, page->count denotes a reference count.
 172  *   page->count == 0 means the page is free.
 173  *   page->count == 1 means the page is used for exactly one purpose
 174  *   (e.g. a private data page of one process).
 175  *
 176  * A page may be used for kmalloc() or anyone else who does a
 177  * get_free_page(). In this case the page->count is at least 1, and
 178  * all other fields are unused but should be 0 or NULL. The
 179  * management of this page is the responsibility of the one who uses
 180  * it.
 181  *
 182  * The other pages (we may call them "process pages") are completely
 183  * managed by the Linux memory manager: I/O, buffers, swapping etc.
 184  * The following discussion applies only to them.
 185  *
 186  * A page may belong to an inode's memory mapping. In this case,
 187  * page->inode is the inode, and page->offset is the file offset
 188  * of the page (not necessarily a multiple of PAGE_SIZE).
 189  *
 190  * A page may have buffers allocated to it. In this case,
 191  * page->buffers is a circular list of these buffer heads. Else,
 192  * page->buffers == NULL.
 193  *
 194  * For pages belonging to inodes, the page->count is the number of
 195  * attaches, plus 1 if buffers are allocated to the page.
 196  *
 197  * All pages belonging to an inode make up a doubly linked list
 198  * inode->i_pages, using the fields page->next and page->prev. (These
 199  * fields are also used for freelist management when page->count==0.)
 200  * There is also a hash table mapping (inode,offset) to the page
 201  * in memory if present. The lists for this hash table use the fields
 202  * page->next_hash and page->prev_hash.
 203  *
 204  * All process pages can do I/O:
 205  * - inode pages may need to be read from disk,
 206  * - inode pages which have been modified and are MAP_SHARED may need
 207  *   to be written to disk,
 208  * - private pages which have been modified may need to be swapped out
 209  *   to swap space and (later) to be read back into memory.
 210  * During disk I/O, page->locked is true. This bit is set before I/O
 211  * and reset when I/O completes. page->wait is a wait queue of all
 212  * tasks waiting for the I/O on this page to complete.
 213  * page->uptodate tells whether the page's contents is valid.
 214  * When a read completes, the page becomes uptodate, unless a disk I/O
 215  * error happened.
 216  * When a write completes, and page->free_after is true, the page is
 217  * freed without any further delay.
 218  *
 219  * For choosing which pages to swap out, inode pages carry a
 220  * page->referenced bit, which is set any time the system accesses
 221  * that page through the (inode,offset) hash table.
 222  * There is also the page->age counter, which implements a linear
 223  * decay (why not an exponential decay?), see swapctl.h.
 224  */
 225 
 226 extern mem_map_t * mem_map;
 227 
 228 /*
 229  * This is timing-critical - most of the time in getting a new page
 230  * goes to clearing the page. If you want a page without the clearing
 231  * overhead, just use __get_free_page() directly..
 232  */
 233 #define __get_free_page(priority) __get_free_pages((priority),0,0)
 234 #define __get_dma_pages(priority, order) __get_free_pages((priority),(order),1)
 235 extern unsigned long __get_free_pages(int priority, unsigned long gfporder, int dma);
 236 
 237 extern inline unsigned long get_free_page(int priority)
     /* [previous][next][first][last][top][bottom][index][help] */
 238 {
 239         unsigned long page;
 240 
 241         page = __get_free_page(priority);
 242         if (page)
 243                 memset((void *) page, 0, PAGE_SIZE);
 244         return page;
 245 }
 246 
 247 /* memory.c & swap.c*/
 248 
 249 #define free_page(addr) free_pages((addr),0)
 250 extern void free_pages(unsigned long addr, unsigned long order);
 251 
 252 extern void show_free_areas(void);
 253 extern unsigned long put_dirty_page(struct task_struct * tsk,unsigned long page,
 254         unsigned long address);
 255 
 256 extern void free_page_tables(struct mm_struct * mm);
 257 extern void clear_page_tables(struct task_struct * tsk);
 258 extern int new_page_tables(struct task_struct * tsk);
 259 extern int copy_page_tables(struct task_struct * to);
 260 
 261 extern int zap_page_range(struct mm_struct *mm, unsigned long address, unsigned long size);
 262 extern int copy_page_range(struct mm_struct *dst, struct mm_struct *src, struct vm_area_struct *vma);
 263 extern int remap_page_range(unsigned long from, unsigned long to, unsigned long size, pgprot_t prot);
 264 extern int zeromap_page_range(unsigned long from, unsigned long size, pgprot_t prot);
 265 
 266 extern void vmtruncate(struct inode * inode, unsigned long offset);
 267 extern void handle_mm_fault(struct vm_area_struct *vma, unsigned long address, int write_access);
 268 extern void do_wp_page(struct task_struct * tsk, struct vm_area_struct * vma, unsigned long address, int write_access);
 269 extern void do_no_page(struct task_struct * tsk, struct vm_area_struct * vma, unsigned long address, int write_access);
 270 
 271 extern unsigned long paging_init(unsigned long start_mem, unsigned long end_mem);
 272 extern void mem_init(unsigned long start_mem, unsigned long end_mem);
 273 extern void show_mem(void);
 274 extern void oom(struct task_struct * tsk);
 275 extern void si_meminfo(struct sysinfo * val);
 276 
 277 /* vmalloc.c */
 278 
 279 extern void * vmalloc(unsigned long size);
 280 extern void * vremap(unsigned long offset, unsigned long size);
 281 extern void vfree(void * addr);
 282 extern int vread(char *buf, char *addr, int count);
 283 
 284 /* mmap.c */
 285 extern unsigned long do_mmap(struct file * file, unsigned long addr, unsigned long len,
 286         unsigned long prot, unsigned long flags, unsigned long off);
 287 extern void merge_segments(struct task_struct *, unsigned long, unsigned long);
 288 extern void insert_vm_struct(struct task_struct *, struct vm_area_struct *);
 289 extern void remove_shared_vm_struct(struct vm_area_struct *);
 290 extern void build_mmap_avl(struct mm_struct *);
 291 extern void exit_mmap(struct mm_struct *);
 292 extern int do_munmap(unsigned long, size_t);
 293 extern unsigned long get_unmapped_area(unsigned long, unsigned long);
 294 
 295 /* filemap.c */
 296 extern unsigned long page_unuse(unsigned long);
 297 extern int shrink_mmap(int, int);
 298 extern void truncate_inode_pages(struct inode *, unsigned long);
 299 
 300 #define GFP_BUFFER      0x00
 301 #define GFP_ATOMIC      0x01
 302 #define GFP_USER        0x02
 303 #define GFP_KERNEL      0x03
 304 #define GFP_NOBUFFER    0x04
 305 #define GFP_NFS         0x05
 306 
 307 /* Flag - indicates that the buffer will be suitable for DMA.  Ignored on some
 308    platforms, used as appropriate on others */
 309 
 310 #define GFP_DMA         0x80
 311 
 312 #define GFP_LEVEL_MASK 0xf
 313 
 314 /* vma is the first one with  address < vma->vm_end,
 315  * and even  address < vma->vm_start. Have to extend vma. */
 316 static inline int expand_stack(struct vm_area_struct * vma, unsigned long address)
     /* [previous][next][first][last][top][bottom][index][help] */
 317 {
 318         unsigned long grow;
 319 
 320         address &= PAGE_MASK;
 321         if (vma->vm_end - address > current->rlim[RLIMIT_STACK].rlim_cur)
 322                 return -ENOMEM;
 323         grow = vma->vm_start - address;
 324         vma->vm_start = address;
 325         vma->vm_offset -= grow;
 326         vma->vm_mm->total_vm += grow >> PAGE_SHIFT;
 327         if (vma->vm_flags & VM_LOCKED)
 328                 vma->vm_mm->locked_vm += grow >> PAGE_SHIFT;
 329         return 0;
 330 }
 331 
 332 #define avl_empty       (struct vm_area_struct *) NULL
 333 
 334 /* Look up the first VMA which satisfies  addr < vm_end,  NULL if none. */
 335 static inline struct vm_area_struct * find_vma (struct task_struct * task, unsigned long addr)
     /* [previous][next][first][last][top][bottom][index][help] */
 336 {
 337         struct vm_area_struct * result = NULL;
 338 
 339         if (task->mm) {
 340                 struct vm_area_struct * tree = task->mm->mmap_avl;
 341                 for (;;) {
 342                         if (tree == avl_empty)
 343                                 break;
 344                         if (tree->vm_end > addr) {
 345                                 result = tree;
 346                                 if (tree->vm_start <= addr)
 347                                         break;
 348                                 tree = tree->vm_avl_left;
 349                         } else
 350                                 tree = tree->vm_avl_right;
 351                 }
 352         }
 353         return result;
 354 }
 355 
 356 /* Look up the first VMA which intersects the interval start_addr..end_addr-1,
 357    NULL if none.  Assume start_addr < end_addr. */
 358 static inline struct vm_area_struct * find_vma_intersection (struct task_struct * task, unsigned long start_addr, unsigned long end_addr)
     /* [previous][next][first][last][top][bottom][index][help] */
 359 {
 360         struct vm_area_struct * vma;
 361 
 362         vma = find_vma(task,start_addr);
 363         if (!vma || end_addr <= vma->vm_start)
 364                 return NULL;
 365         return vma;
 366 }
 367 
 368 #endif /* __KERNEL__ */
 369 
 370 #endif

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