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

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