4 #include <linux/sched.h>
5 #include <linux/errno.h>
9 #include <linux/config.h>
10 #include <linux/string.h>
11 #include <linux/list.h>
12 #include <linux/mmzone.h>
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
;
23 #include <asm/pgtable.h>
24 #include <asm/atomic.h>
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
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).
41 struct vm_area_struct
{
42 struct mm_struct
* vm_mm
; /* VM area parameters */
43 unsigned long vm_start
;
46 /* linked list of VM areas per task, sorted by address */
47 struct vm_area_struct
*vm_next
;
49 pgprot_t vm_page_prot
;
50 unsigned long vm_flags
;
52 /* AVL tree of VM areas per task, sorted by address */
54 struct vm_area_struct
* vm_avl_left
;
55 struct vm_area_struct
* vm_avl_right
;
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.
61 struct vm_area_struct
*vm_next_share
;
62 struct vm_area_struct
**vm_pprev_share
;
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) */
74 #define VM_READ 0x00000001 /* currently active flags */
75 #define VM_WRITE 0x00000002
76 #define VM_EXEC 0x00000004
77 #define VM_SHARED 0x00000008
79 #define VM_MAYREAD 0x00000010 /* limits for mprotect() etc */
80 #define VM_MAYWRITE 0x00000020
81 #define VM_MAYEXEC 0x00000040
82 #define VM_MAYSHARE 0x00000080
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.. */
89 #define VM_EXECUTABLE 0x00001000
90 #define VM_LOCKED 0x00002000
91 #define VM_IO 0x00004000 /* Memory mapped I/O or similar */
93 #define VM_SEQ_READ 0x00008000 /* App will access data sequentially */
94 #define VM_RAND_READ 0x00010000 /* App will not benefit from clustered reads */
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 */
100 #define VM_STACK_FLAGS 0x00000177
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)
109 * mapping from the currently active vm_flags protection bits (the
110 * low four bits) to a page protection mask..
112 extern pgprot_t protection_map
[16];
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.
120 struct vm_operations_struct
{
121 void (*open
)(struct vm_area_struct
* area
);
122 void (*close
)(struct vm_area_struct
* area
);
123 void (*unmap
)(struct vm_area_struct
*area
, unsigned long, size_t);
124 void (*protect
)(struct vm_area_struct
*area
, unsigned long, size_t, unsigned int newprot
);
125 int (*sync
)(struct vm_area_struct
*area
, unsigned long, size_t, unsigned int flags
);
126 struct page
* (*nopage
)(struct vm_area_struct
* area
, unsigned long address
, int write_access
);
127 struct page
* (*wppage
)(struct vm_area_struct
* area
, unsigned long address
, struct page
* page
);
131 * Try to keep the most commonly accessed fields in single cache lines
132 * here (16 bytes or greater). This ordering should be particularly
133 * beneficial on 32-bit processors.
135 * The first line is data used in page cache lookup, the second line
136 * is used for linear searches (eg. clock algorithm scans).
138 typedef struct page
{
139 struct list_head list
;
140 struct address_space
*mapping
;
142 struct page
*next_hash
;
144 unsigned long flags
; /* atomic flags, some possibly updated asynchronously */
145 struct list_head lru
;
147 wait_queue_head_t wait
;
148 struct page
**pprev_hash
;
149 struct buffer_head
* buffers
;
150 void *virtual; /* non-NULL if kmapped */
151 struct zone_struct
*zone
;
154 #define get_page(p) atomic_inc(&(p)->count)
155 #define put_page(p) __free_page(p)
156 #define put_page_testzero(p) atomic_dec_and_test(&(p)->count)
157 #define page_count(p) atomic_read(&(p)->count)
158 #define set_page_count(p,v) atomic_set(&(p)->count, v)
160 /* Page flag bit values */
163 #define PG_referenced 2
164 #define PG_uptodate 3
166 #define PG_decr_after 5
168 #define PG_inactive_dirty 7
170 #define PG_swap_cache 9
172 #define PG_inactive_clean 11
173 #define PG_highmem 12
174 /* bits 21-29 unused */
176 #define PG_reserved 31
179 /* Make it prettier to test the above... */
180 #define Page_Uptodate(page) test_bit(PG_uptodate, &(page)->flags)
181 #define SetPageUptodate(page) set_bit(PG_uptodate, &(page)->flags)
182 #define ClearPageUptodate(page) clear_bit(PG_uptodate, &(page)->flags)
183 #define PageDirty(page) test_bit(PG_dirty, &(page)->flags)
184 #define SetPageDirty(page) set_bit(PG_dirty, &(page)->flags)
185 #define ClearPageDirty(page) clear_bit(PG_dirty, &(page)->flags)
186 #define PageLocked(page) test_bit(PG_locked, &(page)->flags)
187 #define LockPage(page) set_bit(PG_locked, &(page)->flags)
188 #define TryLockPage(page) test_and_set_bit(PG_locked, &(page)->flags)
190 * The first mb is necessary to safely close the critical section opened by the
191 * TryLockPage(), the second mb is necessary to enforce ordering between
192 * the clear_bit and the read of the waitqueue (to avoid SMP races with a
193 * parallel wait_on_page).
195 #define UnlockPage(page) do { \
196 smp_mb__before_clear_bit(); \
197 if (!test_and_clear_bit(PG_locked, &(page)->flags)) BUG(); \
198 smp_mb__after_clear_bit(); \
199 if (waitqueue_active(&page->wait)) \
200 wake_up(&page->wait); \
202 #define PageError(page) test_bit(PG_error, &(page)->flags)
203 #define SetPageError(page) set_bit(PG_error, &(page)->flags)
204 #define ClearPageError(page) clear_bit(PG_error, &(page)->flags)
205 #define PageReferenced(page) test_bit(PG_referenced, &(page)->flags)
206 #define SetPageReferenced(page) set_bit(PG_referenced, &(page)->flags)
207 #define ClearPageReferenced(page) clear_bit(PG_referenced, &(page)->flags)
208 #define PageTestandClearReferenced(page) test_and_clear_bit(PG_referenced, &(page)->flags)
209 #define PageDecrAfter(page) test_bit(PG_decr_after, &(page)->flags)
210 #define SetPageDecrAfter(page) set_bit(PG_decr_after, &(page)->flags)
211 #define PageTestandClearDecrAfter(page) test_and_clear_bit(PG_decr_after, &(page)->flags)
212 #define PageSlab(page) test_bit(PG_slab, &(page)->flags)
213 #define PageSwapCache(page) test_bit(PG_swap_cache, &(page)->flags)
214 #define PageReserved(page) test_bit(PG_reserved, &(page)->flags)
216 #define PageSetSlab(page) set_bit(PG_slab, &(page)->flags)
217 #define PageSetSwapCache(page) set_bit(PG_swap_cache, &(page)->flags)
219 #define PageTestandSetSwapCache(page) test_and_set_bit(PG_swap_cache, &(page)->flags)
221 #define PageClearSlab(page) clear_bit(PG_slab, &(page)->flags)
222 #define PageClearSwapCache(page) clear_bit(PG_swap_cache, &(page)->flags)
224 #define PageTestandClearSwapCache(page) test_and_clear_bit(PG_swap_cache, &(page)->flags)
226 #define PageActive(page) test_bit(PG_active, &(page)->flags)
227 #define SetPageActive(page) set_bit(PG_active, &(page)->flags)
228 #define ClearPageActive(page) clear_bit(PG_active, &(page)->flags)
230 #define PageInactiveDirty(page) test_bit(PG_inactive_dirty, &(page)->flags)
231 #define SetPageInactiveDirty(page) set_bit(PG_inactive_dirty, &(page)->flags)
232 #define ClearPageInactiveDirty(page) clear_bit(PG_inactive_dirty, &(page)->flags)
234 #define PageInactiveClean(page) test_bit(PG_inactive_clean, &(page)->flags)
235 #define SetPageInactiveClean(page) set_bit(PG_inactive_clean, &(page)->flags)
236 #define ClearPageInactiveClean(page) clear_bit(PG_inactive_clean, &(page)->flags)
238 #ifdef CONFIG_HIGHMEM
239 #define PageHighMem(page) test_bit(PG_highmem, &(page)->flags)
241 #define PageHighMem(page) 0 /* needed to optimize away at compile time */
244 #define SetPageReserved(page) set_bit(PG_reserved, &(page)->flags)
245 #define ClearPageReserved(page) clear_bit(PG_reserved, &(page)->flags)
248 * Error return values for the *_nopage functions
250 #define NOPAGE_SIGBUS (NULL)
251 #define NOPAGE_OOM ((struct page *) (-1))
255 * Various page->flags bits:
257 * PG_reserved is set for a page which must never be accessed (which
258 * may not even be present).
260 * PG_DMA has been removed, page->zone now tells exactly wether the
261 * page is suited to do DMAing into.
263 * Multiple processes may "see" the same page. E.g. for untouched
264 * mappings of /dev/null, all processes see the same page full of
265 * zeroes, and text pages of executables and shared libraries have
266 * only one copy in memory, at most, normally.
268 * For the non-reserved pages, page->count denotes a reference count.
269 * page->count == 0 means the page is free.
270 * page->count == 1 means the page is used for exactly one purpose
271 * (e.g. a private data page of one process).
273 * A page may be used for kmalloc() or anyone else who does a
274 * __get_free_page(). In this case the page->count is at least 1, and
275 * all other fields are unused but should be 0 or NULL. The
276 * management of this page is the responsibility of the one who uses
279 * The other pages (we may call them "process pages") are completely
280 * managed by the Linux memory manager: I/O, buffers, swapping etc.
281 * The following discussion applies only to them.
283 * A page may belong to an inode's memory mapping. In this case,
284 * page->inode is the pointer to the inode, and page->offset is the
285 * file offset of the page (not necessarily a multiple of PAGE_SIZE).
287 * A page may have buffers allocated to it. In this case,
288 * page->buffers is a circular list of these buffer heads. Else,
289 * page->buffers == NULL.
291 * For pages belonging to inodes, the page->count is the number of
292 * attaches, plus 1 if buffers are allocated to the page.
294 * All pages belonging to an inode make up a doubly linked list
295 * inode->i_pages, using the fields page->next and page->prev. (These
296 * fields are also used for freelist management when page->count==0.)
297 * There is also a hash table mapping (inode,offset) to the page
298 * in memory if present. The lists for this hash table use the fields
299 * page->next_hash and page->pprev_hash.
301 * All process pages can do I/O:
302 * - inode pages may need to be read from disk,
303 * - inode pages which have been modified and are MAP_SHARED may need
304 * to be written to disk,
305 * - private pages which have been modified may need to be swapped out
306 * to swap space and (later) to be read back into memory.
307 * During disk I/O, PG_locked is used. This bit is set before I/O
308 * and reset when I/O completes. page->wait is a wait queue of all
309 * tasks waiting for the I/O on this page to complete.
310 * PG_uptodate tells whether the page's contents is valid.
311 * When a read completes, the page becomes uptodate, unless a disk I/O
314 * For choosing which pages to swap out, inode pages carry a
315 * PG_referenced bit, which is set any time the system accesses
316 * that page through the (inode,offset) hash table.
318 * PG_skip is used on sparc/sparc64 architectures to "skip" certain
319 * parts of the address space.
321 * PG_error is set to indicate that an I/O error occurred on this page.
323 * PG_arch_1 is an architecture specific page state bit. The generic
324 * code guarentees that this bit is cleared for a page when it first
325 * is entered into the page cache.
328 extern mem_map_t
* mem_map
;
331 * There is only one page-allocator function, and two main namespaces to
332 * it. The alloc_page*() variants return 'struct page *' and as such
333 * can allocate highmem pages, the *get*page*() variants return
334 * virtual kernel addresses to the allocated page(s).
336 extern struct page
* FASTCALL(__alloc_pages(zonelist_t
*zonelist
, unsigned long order
));
337 extern struct page
* alloc_pages_node(int nid
, int gfp_mask
, unsigned long order
);
339 #ifndef CONFIG_DISCONTIGMEM
340 static inline struct page
* alloc_pages(int gfp_mask
, unsigned long order
)
343 * Gets optimized away by the compiler.
345 if (order
>= MAX_ORDER
)
347 return __alloc_pages(contig_page_data
.node_zonelists
+(gfp_mask
), order
);
349 #else /* !CONFIG_DISCONTIGMEM */
350 extern struct page
* alloc_pages(int gfp_mask
, unsigned long order
);
351 #endif /* !CONFIG_DISCONTIGMEM */
353 #define alloc_page(gfp_mask) alloc_pages(gfp_mask, 0)
355 extern unsigned long FASTCALL(__get_free_pages(int gfp_mask
, unsigned long order
));
356 extern unsigned long FASTCALL(get_zeroed_page(int gfp_mask
));
358 #define __get_free_page(gfp_mask) \
359 __get_free_pages((gfp_mask),0)
361 #define __get_dma_pages(gfp_mask, order) \
362 __get_free_pages((gfp_mask) | GFP_DMA,(order))
365 * The old interface name will be removed in 2.5:
367 #define get_free_page get_zeroed_page
370 * There is only one 'core' page-freeing function.
372 extern void FASTCALL(__free_pages(struct page
*page
, unsigned long order
));
373 extern void FASTCALL(free_pages(unsigned long addr
, unsigned long order
));
375 #define __free_page(page) __free_pages((page), 0)
376 #define free_page(addr) free_pages((addr),0)
378 extern void show_free_areas(void);
379 extern void show_free_areas_node(pg_data_t
*pgdat
);
381 extern void clear_page_tables(struct mm_struct
*, unsigned long, int);
383 struct page
* shmem_nopage(struct vm_area_struct
* vma
, unsigned long address
, int no_share
);
384 struct file
*shmem_file_setup(char * name
, loff_t size
);
385 extern int shmem_zero_setup(struct vm_area_struct
*);
387 extern void zap_page_range(struct mm_struct
*mm
, unsigned long address
, unsigned long size
);
388 extern int copy_page_range(struct mm_struct
*dst
, struct mm_struct
*src
, struct vm_area_struct
*vma
);
389 extern int remap_page_range(unsigned long from
, unsigned long to
, unsigned long size
, pgprot_t prot
);
390 extern int zeromap_page_range(unsigned long from
, unsigned long size
, pgprot_t prot
);
392 extern void vmtruncate(struct inode
* inode
, loff_t offset
);
393 extern int handle_mm_fault(struct mm_struct
*mm
,struct vm_area_struct
*vma
, unsigned long address
, int write_access
);
394 extern int make_pages_present(unsigned long addr
, unsigned long end
);
395 extern int access_process_vm(struct task_struct
*tsk
, unsigned long addr
, void *buf
, int len
, int write
);
396 extern int ptrace_readdata(struct task_struct
*tsk
, unsigned long src
, char *dst
, int len
);
397 extern int ptrace_writedata(struct task_struct
*tsk
, char * src
, unsigned long dst
, int len
);
399 extern int pgt_cache_water
[2];
400 extern int check_pgt_cache(void);
402 extern void free_area_init(unsigned long * zones_size
);
403 extern void free_area_init_node(int nid
, pg_data_t
*pgdat
, struct page
*pmap
,
404 unsigned long * zones_size
, unsigned long zone_start_paddr
,
405 unsigned long *zholes_size
);
406 extern void mem_init(void);
407 extern void show_mem(void);
408 extern void si_meminfo(struct sysinfo
* val
);
409 extern void swapin_readahead(swp_entry_t
);
412 extern void lock_vma_mappings(struct vm_area_struct
*);
413 extern void unlock_vma_mappings(struct vm_area_struct
*);
414 extern void insert_vm_struct(struct mm_struct
*, struct vm_area_struct
*);
415 extern void __insert_vm_struct(struct mm_struct
*, struct vm_area_struct
*);
416 extern void build_mmap_avl(struct mm_struct
*);
417 extern void exit_mmap(struct mm_struct
*);
418 extern unsigned long get_unmapped_area(unsigned long, unsigned long);
420 extern unsigned long do_mmap_pgoff(struct file
*file
, unsigned long addr
,
421 unsigned long len
, unsigned long prot
,
422 unsigned long flag
, unsigned long pgoff
);
424 static inline unsigned long do_mmap(struct file
*file
, unsigned long addr
,
425 unsigned long len
, unsigned long prot
,
426 unsigned long flag
, unsigned long offset
)
428 unsigned long ret
= -EINVAL
;
429 if ((offset
+ PAGE_ALIGN(len
)) < offset
)
431 if (!(offset
& ~PAGE_MASK
))
432 ret
= do_mmap_pgoff(file
, addr
, len
, prot
, flag
, offset
>> PAGE_SHIFT
);
437 extern int do_munmap(struct mm_struct
*, unsigned long, size_t);
439 extern unsigned long do_brk(unsigned long, unsigned long);
443 extern void remove_inode_page(struct page
*);
444 extern unsigned long page_unuse(struct page
*);
445 extern void truncate_inode_pages(struct address_space
*, loff_t
);
447 /* generic vm_area_ops exported for stackable file systems */
448 extern int filemap_sync(struct vm_area_struct
*, unsigned long, size_t, unsigned int);
449 extern struct page
*filemap_nopage(struct vm_area_struct
*, unsigned long, int);
454 #define __GFP_WAIT 0x01
455 #define __GFP_HIGH 0x02
456 #define __GFP_IO 0x04
457 #define __GFP_DMA 0x08
458 #ifdef CONFIG_HIGHMEM
459 #define __GFP_HIGHMEM 0x10
461 #define __GFP_HIGHMEM 0x0 /* noop */
465 #define GFP_BUFFER (__GFP_HIGH | __GFP_WAIT)
466 #define GFP_ATOMIC (__GFP_HIGH)
467 #define GFP_USER ( __GFP_WAIT | __GFP_IO)
468 #define GFP_HIGHUSER ( __GFP_WAIT | __GFP_IO | __GFP_HIGHMEM)
469 #define GFP_KERNEL (__GFP_HIGH | __GFP_WAIT | __GFP_IO)
470 #define GFP_NFS (__GFP_HIGH | __GFP_WAIT | __GFP_IO)
471 #define GFP_KSWAPD ( __GFP_IO)
473 /* Flag - indicates that the buffer will be suitable for DMA. Ignored on some
474 platforms, used as appropriate on others */
476 #define GFP_DMA __GFP_DMA
478 /* Flag - indicates that the buffer can be taken from high memory which is not
479 permanently mapped by the kernel */
481 #define GFP_HIGHMEM __GFP_HIGHMEM
483 /* vma is the first one with address < vma->vm_end,
484 * and even address < vma->vm_start. Have to extend vma. */
485 static inline int expand_stack(struct vm_area_struct
* vma
, unsigned long address
)
489 address
&= PAGE_MASK
;
490 grow
= (vma
->vm_start
- address
) >> PAGE_SHIFT
;
491 if (vma
->vm_end
- address
> current
->rlim
[RLIMIT_STACK
].rlim_cur
||
492 ((vma
->vm_mm
->total_vm
+ grow
) << PAGE_SHIFT
) > current
->rlim
[RLIMIT_AS
].rlim_cur
)
494 vma
->vm_start
= address
;
495 vma
->vm_pgoff
-= grow
;
496 vma
->vm_mm
->total_vm
+= grow
;
497 if (vma
->vm_flags
& VM_LOCKED
)
498 vma
->vm_mm
->locked_vm
+= grow
;
502 /* Look up the first VMA which satisfies addr < vm_end, NULL if none. */
503 extern struct vm_area_struct
* find_vma(struct mm_struct
* mm
, unsigned long addr
);
504 extern struct vm_area_struct
* find_vma_prev(struct mm_struct
* mm
, unsigned long addr
,
505 struct vm_area_struct
**pprev
);
507 /* Look up the first VMA which intersects the interval start_addr..end_addr-1,
508 NULL if none. Assume start_addr < end_addr. */
509 static inline struct vm_area_struct
* find_vma_intersection(struct mm_struct
* mm
, unsigned long start_addr
, unsigned long end_addr
)
511 struct vm_area_struct
* vma
= find_vma(mm
,start_addr
);
513 if (vma
&& end_addr
<= vma
->vm_start
)
518 extern struct vm_area_struct
*find_extend_vma(struct mm_struct
*mm
, unsigned long addr
);
520 #define buffer_under_min() (atomic_read(&buffermem_pages) * 100 < \
521 buffer_mem.min_percent * num_physpages)
522 #define pgcache_under_min() (atomic_read(&page_cache_size) * 100 < \
523 page_cache.min_percent * num_physpages)
525 #endif /* __KERNEL__ */