1 #ifndef _LINUX_MM_H
2 #define _LINUX_MM_H
3
4 #include <linux/sched.h>
5 #include <linux/errno.h>
6
7 #ifdef __KERNEL__
8
9 #include <linux/config.h>
10 #include <linux/string.h>
11 #include <linux/list.h>
12 #include <linux/mmzone.h>
13
14 extern unsigned long max_mapnr;
15 extern unsigned long num_physpages;
16 extern void * high_memory;
17 extern int page_cluster;
18 /* The inactive_clean lists are per zone. */
19 extern struct list_head active_list;
20 extern struct list_head inactive_dirty_list;
21
22 #include <asm/page.h>
23 #include <asm/pgtable.h>
24 #include <asm/atomic.h>
25
26 /*
27 * Linux kernel virtual memory manager primitives.
28 * The idea being to have a "virtual" mm in the same way
29 * we have a virtual fs - giving a cleaner interface to the
30 * mm details, and allowing different kinds of memory mappings
31 * (from shared memory to executable loading to arbitrary
32 * mmap() functions).
33 */
34
35 /*
36 * This struct defines a memory VMM memory area. There is one of these
37 * per VM-area/task. A VM area is any part of the process virtual memory
38 * space that has a special rule for the page-fault handlers (ie a shared
39 * library, the executable area etc).
40 */
41 struct vm_area_struct {
42 struct mm_struct * vm_mm; /* VM area parameters */
43 unsigned long vm_start;
44 unsigned long vm_end;
45
46 /* linked list of VM areas per task, sorted by address */
47 struct vm_area_struct *vm_next;
48
49 pgprot_t vm_page_prot;
50 unsigned long vm_flags;
51
52 /* AVL tree of VM areas per task, sorted by address */
53 short vm_avl_height;
54 struct vm_area_struct * vm_avl_left;
55 struct vm_area_struct * vm_avl_right;
56
57 /* For areas with an address space and backing store,
58 * one of the address_space->i_mmap{,shared} lists,
59 * for shm areas, the list of attaches, otherwise unused.
60 */
61 struct vm_area_struct *vm_next_share;
62 struct vm_area_struct **vm_pprev_share;
63
64 struct vm_operations_struct * vm_ops;
65 unsigned long vm_pgoff; /* offset in PAGE_SIZE units, *not* PAGE_CACHE_SIZE */
66 struct file * vm_file;
67 unsigned long vm_raend;
68 void * vm_private_data; /* was vm_pte (shared mem) */
69 };
70
71 /*
72 * vm_flags..
73 */
74 #define VM_READ 0x00000001 /* currently active flags */
75 #define VM_WRITE 0x00000002
76 #define VM_EXEC 0x00000004
77 #define VM_SHARED 0x00000008
78
79 #define VM_MAYREAD 0x00000010 /* limits for mprotect() etc */
80 #define VM_MAYWRITE 0x00000020
81 #define VM_MAYEXEC 0x00000040
82 #define VM_MAYSHARE 0x00000080
83
84 #define VM_GROWSDOWN 0x00000100 /* general info on the segment */
85 #define VM_GROWSUP 0x00000200
86 #define VM_SHM 0x00000400 /* shared memory area, don't swap out */
87 #define VM_DENYWRITE 0x00000800 /* ETXTBSY on write attempts.. */
88
89 #define VM_EXECUTABLE 0x00001000
90 #define VM_LOCKED 0x00002000
91 #define VM_IO 0x00004000 /* Memory mapped I/O or similar */
92
93 #define VM_SEQ_READ 0x00008000 /* App will access data sequentially */
94 #define VM_RAND_READ 0x00010000 /* App will not benefit from clustered reads */
95
96 #define VM_DONTCOPY 0x00020000 /* Do not copy this vma on fork */
97 #define VM_DONTEXPAND 0x00040000 /* Cannot expand with mremap() */
98 #define VM_RESERVED 0x00080000 /* Don't unmap it from swap_out */
99
100 #define VM_STACK_FLAGS 0x00000177
101
102 #define VM_READHINTMASK (VM_SEQ_READ | VM_RAND_READ)
103 #define VM_ClearReadHint(v) (v)->vm_flags &= ~VM_READHINTMASK
104 #define VM_NormalReadHint(v) (!((v)->vm_flags & VM_READHINTMASK))
105 #define VM_SequentialReadHint(v) ((v)->vm_flags & VM_SEQ_READ)
106 #define VM_RandomReadHint(v) ((v)->vm_flags & VM_RAND_READ)
107
108 /*
109 * mapping from the currently active vm_flags protection bits (the
110 * low four bits) to a page protection mask..
111 */
112 extern pgprot_t protection_map[16];
113
114
115 /*
116 * These are the virtual MM functions - opening of an area, closing and
117 * unmapping it (needed to keep files on disk up-to-date etc), pointer
118 * to the functions called when a no-page or a wp-page exception occurs.
119 */
120 struct vm_operations_struct {
121 void (*open)(struct vm_area_struct * area);
122 void (*close)(struct vm_area_struct * area);
123 struct page * (*nopage)(struct vm_area_struct * area, unsigned long address, int write_access);
124 };
125
126 /*
127 * Try to keep the most commonly accessed fields in single cache lines
128 * here (16 bytes or greater). This ordering should be particularly
129 * beneficial on 32-bit processors.
130 *
131 * The first line is data used in page cache lookup, the second line
132 * is used for linear searches (eg. clock algorithm scans).
133 */
134 typedef struct page {
135 struct list_head list;
136 struct address_space *mapping;
137 unsigned long index;
138 struct page *next_hash;
139 atomic_t count;
140 unsigned long flags; /* atomic flags, some possibly updated asynchronously */
141 struct list_head lru;
142 unsigned long age;
143 wait_queue_head_t wait;
144 struct page **pprev_hash;
145 struct buffer_head * buffers;
146 void *virtual; /* non-NULL if kmapped */
147 struct zone_struct *zone;
148 } mem_map_t;
149
150 #define get_page(p) atomic_inc(&(p)->count)
151 #define put_page(p) __free_page(p)
152 #define put_page_testzero(p) atomic_dec_and_test(&(p)->count)
153 #define page_count(p) atomic_read(&(p)->count)
154 #define set_page_count(p,v) atomic_set(&(p)->count, v)
155
156 /* Page flag bit values */
157 #define PG_locked 0
158 #define PG_error 1
159 #define PG_referenced 2
160 #define PG_uptodate 3
161 #define PG_dirty 4
162 #define PG_decr_after 5
163 #define PG_active 6
164 #define PG_inactive_dirty 7
165 #define PG_slab 8
166 #define PG_swap_cache 9
167 #define PG_skip 10
168 #define PG_inactive_clean 11
169 #define PG_highmem 12
170 /* bits 21-29 unused */
171 #define PG_arch_1 30
172 #define PG_reserved 31
173
174 /* Make it prettier to test the above... */
175 #define Page_Uptodate(page) test_bit(PG_uptodate, &(page)->flags)
176 #define SetPageUptodate(page) set_bit(PG_uptodate, &(page)->flags)
177 #define ClearPageUptodate(page) clear_bit(PG_uptodate, &(page)->flags)
178 #define PageDirty(page) test_bit(PG_dirty, &(page)->flags)
179 #define SetPageDirty(page) set_bit(PG_dirty, &(page)->flags)
180 #define ClearPageDirty(page) clear_bit(PG_dirty, &(page)->flags)
181 #define PageLocked(page) test_bit(PG_locked, &(page)->flags)
182 #define LockPage(page) set_bit(PG_locked, &(page)->flags)
183 #define TryLockPage(page) test_and_set_bit(PG_locked, &(page)->flags)
184
185 extern void __set_page_dirty(struct page *);
186
187 static inline void set_page_dirty(struct page * page)
188 {
189 if (!test_and_set_bit(PG_dirty, &page->flags))
190 __set_page_dirty(page);
191 }
192
193 /*
194 * The first mb is necessary to safely close the critical section opened by the
195 * TryLockPage(), the second mb is necessary to enforce ordering between
196 * the clear_bit and the read of the waitqueue (to avoid SMP races with a
197 * parallel wait_on_page).
198 */
199 #define UnlockPage(page) do { \
200 smp_mb__before_clear_bit(); \
201 if (!test_and_clear_bit(PG_locked, &(page)->flags)) BUG(); \
202 smp_mb__after_clear_bit(); \
203 if (waitqueue_active(&page->wait)) \
204 wake_up(&page->wait); \
205 } while (0)
206 #define PageError(page) test_bit(PG_error, &(page)->flags)
207 #define SetPageError(page) set_bit(PG_error, &(page)->flags)
208 #define ClearPageError(page) clear_bit(PG_error, &(page)->flags)
209 #define PageReferenced(page) test_bit(PG_referenced, &(page)->flags)
210 #define SetPageReferenced(page) set_bit(PG_referenced, &(page)->flags)
211 #define ClearPageReferenced(page) clear_bit(PG_referenced, &(page)->flags)
212 #define PageTestandClearReferenced(page) test_and_clear_bit(PG_referenced, &(page)->flags)
213 #define PageDecrAfter(page) test_bit(PG_decr_after, &(page)->flags)
214 #define SetPageDecrAfter(page) set_bit(PG_decr_after, &(page)->flags)
215 #define PageTestandClearDecrAfter(page) test_and_clear_bit(PG_decr_after, &(page)->flags)
216 #define PageSlab(page) test_bit(PG_slab, &(page)->flags)
217 #define PageSwapCache(page) test_bit(PG_swap_cache, &(page)->flags)
218 #define PageReserved(page) test_bit(PG_reserved, &(page)->flags)
219
220 #define PageSetSlab(page) set_bit(PG_slab, &(page)->flags)
221 #define PageSetSwapCache(page) set_bit(PG_swap_cache, &(page)->flags)
222
223 #define PageTestandSetSwapCache(page) test_and_set_bit(PG_swap_cache, &(page)->flags)
224
225 #define PageClearSlab(page) clear_bit(PG_slab, &(page)->flags)
226 #define PageClearSwapCache(page) clear_bit(PG_swap_cache, &(page)->flags)
227
228 #define PageTestandClearSwapCache(page) test_and_clear_bit(PG_swap_cache, &(page)->flags)
229
230 #define PageActive(page) test_bit(PG_active, &(page)->flags)
231 #define SetPageActive(page) set_bit(PG_active, &(page)->flags)
232 #define ClearPageActive(page) clear_bit(PG_active, &(page)->flags)
233
234 #define PageInactiveDirty(page) test_bit(PG_inactive_dirty, &(page)->flags)
235 #define SetPageInactiveDirty(page) set_bit(PG_inactive_dirty, &(page)->flags)
236 #define ClearPageInactiveDirty(page) clear_bit(PG_inactive_dirty, &(page)->flags)
237
238 #define PageInactiveClean(page) test_bit(PG_inactive_clean, &(page)->flags)
239 #define SetPageInactiveClean(page) set_bit(PG_inactive_clean, &(page)->flags)
240 #define ClearPageInactiveClean(page) clear_bit(PG_inactive_clean, &(page)->flags)
241
242 #ifdef CONFIG_HIGHMEM
243 #define PageHighMem(page) test_bit(PG_highmem, &(page)->flags)
244 #else
245 #define PageHighMem(page) 0 /* needed to optimize away at compile time */
246 #endif
247
248 #define SetPageReserved(page) set_bit(PG_reserved, &(page)->flags)
249 #define ClearPageReserved(page) clear_bit(PG_reserved, &(page)->flags)
250
251 /*
252 * Error return values for the *_nopage functions
253 */
254 #define NOPAGE_SIGBUS (NULL)
255 #define NOPAGE_OOM ((struct page *) (-1))
256
257
258 /*
259 * Various page->flags bits:
260 *
261 * PG_reserved is set for a page which must never be accessed (which
262 * may not even be present).
263 *
264 * PG_DMA has been removed, page->zone now tells exactly wether the
265 * page is suited to do DMAing into.
266 *
267 * Multiple processes may "see" the same page. E.g. for untouched
268 * mappings of /dev/null, all processes see the same page full of
269 * zeroes, and text pages of executables and shared libraries have
270 * only one copy in memory, at most, normally.
271 *
272 * For the non-reserved pages, page->count denotes a reference count.
273 * page->count == 0 means the page is free.
274 * page->count == 1 means the page is used for exactly one purpose
275 * (e.g. a private data page of one process).
276 *
277 * A page may be used for kmalloc() or anyone else who does a
278 * __get_free_page(). In this case the page->count is at least 1, and
279 * all other fields are unused but should be 0 or NULL. The
280 * management of this page is the responsibility of the one who uses
281 * it.
282 *
283 * The other pages (we may call them "process pages") are completely
284 * managed by the Linux memory manager: I/O, buffers, swapping etc.
285 * The following discussion applies only to them.
286 *
287 * A page may belong to an inode's memory mapping. In this case,
288 * page->inode is the pointer to the inode, and page->offset is the
289 * file offset of the page (not necessarily a multiple of PAGE_SIZE).
290 *
291 * A page may have buffers allocated to it. In this case,
292 * page->buffers is a circular list of these buffer heads. Else,
293 * page->buffers == NULL.
294 *
295 * For pages belonging to inodes, the page->count is the number of
296 * attaches, plus 1 if buffers are allocated to the page.
297 *
298 * All pages belonging to an inode make up a doubly linked list
299 * inode->i_pages, using the fields page->next and page->prev. (These
300 * fields are also used for freelist management when page->count==0.)
301 * There is also a hash table mapping (inode,offset) to the page
302 * in memory if present. The lists for this hash table use the fields
303 * page->next_hash and page->pprev_hash.
304 *
305 * All process pages can do I/O:
306 * - inode pages may need to be read from disk,
307 * - inode pages which have been modified and are MAP_SHARED may need
308 * to be written to disk,
309 * - private pages which have been modified may need to be swapped out
310 * to swap space and (later) to be read back into memory.
311 * During disk I/O, PG_locked is used. This bit is set before I/O
312 * and reset when I/O completes. page->wait is a wait queue of all
313 * tasks waiting for the I/O on this page to complete.
314 * PG_uptodate tells whether the page's contents is valid.
315 * When a read completes, the page becomes uptodate, unless a disk I/O
316 * error happened.
317 *
318 * For choosing which pages to swap out, inode pages carry a
319 * PG_referenced bit, which is set any time the system accesses
320 * that page through the (inode,offset) hash table.
321 *
322 * PG_skip is used on sparc/sparc64 architectures to "skip" certain
323 * parts of the address space.
324 *
325 * PG_error is set to indicate that an I/O error occurred on this page.
326 *
327 * PG_arch_1 is an architecture specific page state bit. The generic
328 * code guarentees that this bit is cleared for a page when it first
329 * is entered into the page cache.
330 */
331
332 extern mem_map_t * mem_map;
333
334 /*
335 * There is only one page-allocator function, and two main namespaces to
336 * it. The alloc_page*() variants return 'struct page *' and as such
337 * can allocate highmem pages, the *get*page*() variants return
338 * virtual kernel addresses to the allocated page(s).
339 */
340 extern struct page * FASTCALL(__alloc_pages(zonelist_t *zonelist, unsigned long order));
341 extern struct page * alloc_pages_node(int nid, int gfp_mask, unsigned long order);
342
343 #ifndef CONFIG_DISCONTIGMEM
344 static inline struct page * alloc_pages(int gfp_mask, unsigned long order)
345 {
346 /*
347 * Gets optimized away by the compiler.
348 */
349 if (order >= MAX_ORDER)
350 return NULL;
351 return __alloc_pages(contig_page_data.node_zonelists+(gfp_mask), order);
352 }
353 #else /* !CONFIG_DISCONTIGMEM */
354 extern struct page * alloc_pages(int gfp_mask, unsigned long order);
355 #endif /* !CONFIG_DISCONTIGMEM */
356
357 #define alloc_page(gfp_mask) alloc_pages(gfp_mask, 0)
358
359 extern unsigned long FASTCALL(__get_free_pages(int gfp_mask, unsigned long order));
360 extern unsigned long FASTCALL(get_zeroed_page(int gfp_mask));
361
362 #define __get_free_page(gfp_mask) \
363 __get_free_pages((gfp_mask),0)
364
365 #define __get_dma_pages(gfp_mask, order) \
366 __get_free_pages((gfp_mask) | GFP_DMA,(order))
367
368 /*
369 * The old interface name will be removed in 2.5:
370 */
371 #define get_free_page get_zeroed_page
372
373 /*
374 * There is only one 'core' page-freeing function.
375 */
376 extern void FASTCALL(__free_pages(struct page *page, unsigned long order));
377 extern void FASTCALL(free_pages(unsigned long addr, unsigned long order));
378
379 #define __free_page(page) __free_pages((page), 0)
380 #define free_page(addr) free_pages((addr),0)
381
382 extern void show_free_areas(void);
383 extern void show_free_areas_node(pg_data_t *pgdat);
384
385 extern void clear_page_tables(struct mm_struct *, unsigned long, int);
386
387 struct page * shmem_nopage(struct vm_area_struct * vma, unsigned long address, int no_share);
388 struct file *shmem_file_setup(char * name, loff_t size);
389 extern int shmem_zero_setup(struct vm_area_struct *);
390
391 extern void zap_page_range(struct mm_struct *mm, unsigned long address, unsigned long size);
392 extern int copy_page_range(struct mm_struct *dst, struct mm_struct *src, struct vm_area_struct *vma);
393 extern int remap_page_range(unsigned long from, unsigned long to, unsigned long size, pgprot_t prot);
394 extern int zeromap_page_range(unsigned long from, unsigned long size, pgprot_t prot);
395
396 extern void vmtruncate(struct inode * inode, loff_t offset);
397 extern int handle_mm_fault(struct mm_struct *mm,struct vm_area_struct *vma, unsigned long address, int write_access);
398 extern int make_pages_present(unsigned long addr, unsigned long end);
399 extern int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write);
400 extern int ptrace_readdata(struct task_struct *tsk, unsigned long src, char *dst, int len);
401 extern int ptrace_writedata(struct task_struct *tsk, char * src, unsigned long dst, int len);
402
403 extern int pgt_cache_water[2];
404 extern int check_pgt_cache(void);
405
406 extern void free_area_init(unsigned long * zones_size);
407 extern void free_area_init_node(int nid, pg_data_t *pgdat, struct page *pmap,
408 unsigned long * zones_size, unsigned long zone_start_paddr,
409 unsigned long *zholes_size);
410 extern void mem_init(void);
411 extern void show_mem(void);
412 extern void si_meminfo(struct sysinfo * val);
413 extern void swapin_readahead(swp_entry_t);
414
415 /* mmap.c */
416 extern void lock_vma_mappings(struct vm_area_struct *);
417 extern void unlock_vma_mappings(struct vm_area_struct *);
418 extern void insert_vm_struct(struct mm_struct *, struct vm_area_struct *);
419 extern void __insert_vm_struct(struct mm_struct *, struct vm_area_struct *);
420 extern void build_mmap_avl(struct mm_struct *);
421 extern void exit_mmap(struct mm_struct *);
422 extern unsigned long get_unmapped_area(unsigned long, unsigned long);
423
424 extern unsigned long do_mmap_pgoff(struct file *file, unsigned long addr,
425 unsigned long len, unsigned long prot,
426 unsigned long flag, unsigned long pgoff);
427
428 static inline unsigned long do_mmap(struct file *file, unsigned long addr,
429 unsigned long len, unsigned long prot,
430 unsigned long flag, unsigned long offset)
431 {
432 unsigned long ret = -EINVAL;
433 if ((offset + PAGE_ALIGN(len)) < offset)
434 goto out;
435 if (!(offset & ~PAGE_MASK))
436 ret = do_mmap_pgoff(file, addr, len, prot, flag, offset >> PAGE_SHIFT);
437 out:
438 return ret;
439 }
440
441 extern int do_munmap(struct mm_struct *, unsigned long, size_t);
442
443 extern unsigned long do_brk(unsigned long, unsigned long);
444
445 struct zone_t;
446 /* filemap.c */
447 extern void remove_inode_page(struct page *);
448 extern unsigned long page_unuse(struct page *);
449 extern void truncate_inode_pages(struct address_space *, loff_t);
450
451 /* generic vm_area_ops exported for stackable file systems */
452 extern int filemap_sync(struct vm_area_struct *, unsigned long, size_t, unsigned int);
453 extern struct page *filemap_nopage(struct vm_area_struct *, unsigned long, int);
454
455 /*
456 * GFP bitmasks..
457 */
458 #define __GFP_WAIT 0x01
459 #define __GFP_HIGH 0x02
460 #define __GFP_IO 0x04
461 #define __GFP_DMA 0x08
462 #ifdef CONFIG_HIGHMEM
463 #define __GFP_HIGHMEM 0x10
464 #else
465 #define __GFP_HIGHMEM 0x0 /* noop */
466 #endif
467
468
469 #define GFP_BUFFER (__GFP_HIGH | __GFP_WAIT)
470 #define GFP_ATOMIC (__GFP_HIGH)
471 #define GFP_USER ( __GFP_WAIT | __GFP_IO)
472 #define GFP_HIGHUSER ( __GFP_WAIT | __GFP_IO | __GFP_HIGHMEM)
473 #define GFP_KERNEL (__GFP_HIGH | __GFP_WAIT | __GFP_IO)
474 #define GFP_NFS (__GFP_HIGH | __GFP_WAIT | __GFP_IO)
475 #define GFP_KSWAPD ( __GFP_IO)
476
477 /* Flag - indicates that the buffer will be suitable for DMA. Ignored on some
478 platforms, used as appropriate on others */
479
480 #define GFP_DMA __GFP_DMA
481
482 /* Flag - indicates that the buffer can be taken from high memory which is not
483 permanently mapped by the kernel */
484
485 #define GFP_HIGHMEM __GFP_HIGHMEM
486
487 /* vma is the first one with address < vma->vm_end,
488 * and even address < vma->vm_start. Have to extend vma. */
489 static inline int expand_stack(struct vm_area_struct * vma, unsigned long address)
490 {
491 unsigned long grow;
492
493 address &= PAGE_MASK;
494 grow = (vma->vm_start - address) >> PAGE_SHIFT;
495 if (vma->vm_end - address > current->rlim[RLIMIT_STACK].rlim_cur ||
496 ((vma->vm_mm->total_vm + grow) << PAGE_SHIFT) > current->rlim[RLIMIT_AS].rlim_cur)
497 return -ENOMEM;
498 vma->vm_start = address;
499 vma->vm_pgoff -= grow;
500 vma->vm_mm->total_vm += grow;
501 if (vma->vm_flags & VM_LOCKED)
502 vma->vm_mm->locked_vm += grow;
503 return 0;
504 }
505
506 /* Look up the first VMA which satisfies addr < vm_end, NULL if none. */
507 extern struct vm_area_struct * find_vma(struct mm_struct * mm, unsigned long addr);
508 extern struct vm_area_struct * find_vma_prev(struct mm_struct * mm, unsigned long addr,
509 struct vm_area_struct **pprev);
510
511 /* Look up the first VMA which intersects the interval start_addr..end_addr-1,
512 NULL if none. Assume start_addr < end_addr. */
513 static inline struct vm_area_struct * find_vma_intersection(struct mm_struct * mm, unsigned long start_addr, unsigned long end_addr)
514 {
515 struct vm_area_struct * vma = find_vma(mm,start_addr);
516
517 if (vma && end_addr <= vma->vm_start)
518 vma = NULL;
519 return vma;
520 }
521
522 extern struct vm_area_struct *find_extend_vma(struct mm_struct *mm, unsigned long addr);
523
524 #define buffer_under_min() (atomic_read(&buffermem_pages) * 100 < \
525 buffer_mem.min_percent * num_physpages)
526 #define pgcache_under_min() (atomic_read(&page_cache_size) * 100 < \
527 page_cache.min_percent * num_physpages)
528
529 #endif /* __KERNEL__ */
530
531 #endif
532
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.