blob: 99d21020ec9d1158677ee7a32ad15aff5fcd4ad8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/mm/nommu.c
3 *
4 * Replacement code for mm functions to support CPU's that don't
5 * have any form of memory management unit (thus no virtual memory).
6 *
7 * See Documentation/nommu-mmap.txt
8 *
9 * Copyright (c) 2004-2005 David Howells <dhowells@redhat.com>
10 * Copyright (c) 2000-2003 David McCullough <davidm@snapgear.com>
11 * Copyright (c) 2000-2001 D Jeff Dionne <jeff@uClinux.org>
12 * Copyright (c) 2002 Greg Ungerer <gerg@snapgear.com>
13 */
14
15#include <linux/mm.h>
16#include <linux/mman.h>
17#include <linux/swap.h>
18#include <linux/file.h>
19#include <linux/highmem.h>
20#include <linux/pagemap.h>
21#include <linux/slab.h>
22#include <linux/vmalloc.h>
23#include <linux/ptrace.h>
24#include <linux/blkdev.h>
25#include <linux/backing-dev.h>
26#include <linux/mount.h>
27#include <linux/personality.h>
28#include <linux/security.h>
29#include <linux/syscalls.h>
30
31#include <asm/uaccess.h>
32#include <asm/tlb.h>
33#include <asm/tlbflush.h>
34
35void *high_memory;
36struct page *mem_map;
37unsigned long max_mapnr;
38unsigned long num_physpages;
39unsigned long askedalloc, realalloc;
40atomic_t vm_committed_space = ATOMIC_INIT(0);
41int sysctl_overcommit_memory = OVERCOMMIT_GUESS; /* heuristic overcommit */
42int sysctl_overcommit_ratio = 50; /* default is 50% */
43int sysctl_max_map_count = DEFAULT_MAX_MAP_COUNT;
44int heap_stack_gap = 0;
45
46EXPORT_SYMBOL(mem_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047EXPORT_SYMBOL(__vm_enough_memory);
48
49/* list of shareable VMAs */
50struct rb_root nommu_vma_tree = RB_ROOT;
51DECLARE_RWSEM(nommu_vma_sem);
52
53struct vm_operations_struct generic_file_vm_ops = {
54};
55
Greg Ungerer66aa2b42005-09-12 11:18:10 +100056EXPORT_SYMBOL(vmalloc);
57EXPORT_SYMBOL(vfree);
58EXPORT_SYMBOL(vmalloc_to_page);
59EXPORT_SYMBOL(vmalloc_32);
Luke Yang7a9166e2006-02-20 18:28:07 -080060EXPORT_SYMBOL(vmap);
61EXPORT_SYMBOL(vunmap);
Greg Ungerer66aa2b42005-09-12 11:18:10 +100062
Linus Torvalds1da177e2005-04-16 15:20:36 -070063/*
64 * Handle all mappings that got truncated by a "truncate()"
65 * system call.
66 *
67 * NOTE! We have to be ready to update the memory sharing
68 * between the file and the memory map for a potential last
69 * incomplete page. Ugly, but necessary.
70 */
71int vmtruncate(struct inode *inode, loff_t offset)
72{
73 struct address_space *mapping = inode->i_mapping;
74 unsigned long limit;
75
76 if (inode->i_size < offset)
77 goto do_expand;
78 i_size_write(inode, offset);
79
80 truncate_inode_pages(mapping, offset);
81 goto out_truncate;
82
83do_expand:
84 limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
85 if (limit != RLIM_INFINITY && offset > limit)
86 goto out_sig;
87 if (offset > inode->i_sb->s_maxbytes)
88 goto out;
89 i_size_write(inode, offset);
90
91out_truncate:
92 if (inode->i_op && inode->i_op->truncate)
93 inode->i_op->truncate(inode);
94 return 0;
95out_sig:
96 send_sig(SIGXFSZ, current, 0);
97out:
98 return -EFBIG;
99}
100
101EXPORT_SYMBOL(vmtruncate);
102
103/*
104 * Return the total memory allocated for this pointer, not
105 * just what the caller asked for.
106 *
107 * Doesn't have to be accurate, i.e. may have races.
108 */
109unsigned int kobjsize(const void *objp)
110{
111 struct page *page;
112
113 if (!objp || !((page = virt_to_page(objp))))
114 return 0;
115
116 if (PageSlab(page))
117 return ksize(objp);
118
119 BUG_ON(page->index < 0);
120 BUG_ON(page->index >= MAX_ORDER);
121
122 return (PAGE_SIZE << page->index);
123}
124
125/*
126 * The nommu dodgy version :-)
127 */
128int get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
129 unsigned long start, int len, int write, int force,
130 struct page **pages, struct vm_area_struct **vmas)
131{
132 int i;
133 static struct vm_area_struct dummy_vma;
134
135 for (i = 0; i < len; i++) {
136 if (pages) {
137 pages[i] = virt_to_page(start);
138 if (pages[i])
139 page_cache_get(pages[i]);
140 }
141 if (vmas)
142 vmas[i] = &dummy_vma;
143 start += PAGE_SIZE;
144 }
145 return(i);
146}
147
Greg Ungerer66aa2b42005-09-12 11:18:10 +1000148EXPORT_SYMBOL(get_user_pages);
149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150DEFINE_RWLOCK(vmlist_lock);
151struct vm_struct *vmlist;
152
153void vfree(void *addr)
154{
155 kfree(addr);
156}
157
Al Virodd0fc662005-10-07 07:46:04 +0100158void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
160 /*
161 * kmalloc doesn't like __GFP_HIGHMEM for some reason
162 */
163 return kmalloc(size, gfp_mask & ~__GFP_HIGHMEM);
164}
165
166struct page * vmalloc_to_page(void *addr)
167{
168 return virt_to_page(addr);
169}
170
171unsigned long vmalloc_to_pfn(void *addr)
172{
173 return page_to_pfn(virt_to_page(addr));
174}
175
176
177long vread(char *buf, char *addr, unsigned long count)
178{
179 memcpy(buf, addr, count);
180 return count;
181}
182
183long vwrite(char *buf, char *addr, unsigned long count)
184{
185 /* Don't allow overflow */
186 if ((unsigned long) addr + count < count)
187 count = -(unsigned long) addr;
188
189 memcpy(addr, buf, count);
190 return(count);
191}
192
193/*
194 * vmalloc - allocate virtually continguos memory
195 *
196 * @size: allocation size
197 *
198 * Allocate enough pages to cover @size from the page level
199 * allocator and map them into continguos kernel virtual space.
200 *
201 * For tight cotrol over page level allocator and protection flags
202 * use __vmalloc() instead.
203 */
204void *vmalloc(unsigned long size)
205{
206 return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL);
207}
208
209/*
210 * vmalloc_32 - allocate virtually continguos memory (32bit addressable)
211 *
212 * @size: allocation size
213 *
214 * Allocate enough 32bit PA addressable pages to cover @size from the
215 * page level allocator and map them into continguos kernel virtual space.
216 */
217void *vmalloc_32(unsigned long size)
218{
219 return __vmalloc(size, GFP_KERNEL, PAGE_KERNEL);
220}
221
222void *vmap(struct page **pages, unsigned int count, unsigned long flags, pgprot_t prot)
223{
224 BUG();
225 return NULL;
226}
227
228void vunmap(void *addr)
229{
230 BUG();
231}
232
233/*
234 * sys_brk() for the most part doesn't need the global kernel
235 * lock, except when an application is doing something nasty
236 * like trying to un-brk an area that has already been mapped
237 * to a regular file. in this case, the unmapping will need
238 * to invoke file system routines that need the global lock.
239 */
240asmlinkage unsigned long sys_brk(unsigned long brk)
241{
242 struct mm_struct *mm = current->mm;
243
244 if (brk < mm->start_brk || brk > mm->context.end_brk)
245 return mm->brk;
246
247 if (mm->brk == brk)
248 return mm->brk;
249
250 /*
251 * Always allow shrinking brk
252 */
253 if (brk <= mm->brk) {
254 mm->brk = brk;
255 return brk;
256 }
257
258 /*
259 * Ok, looks good - let it rip.
260 */
261 return mm->brk = brk;
262}
263
264#ifdef DEBUG
265static void show_process_blocks(void)
266{
267 struct vm_list_struct *vml;
268
269 printk("Process blocks %d:", current->pid);
270
271 for (vml = &current->mm->context.vmlist; vml; vml = vml->next) {
272 printk(" %p: %p", vml, vml->vma);
273 if (vml->vma)
274 printk(" (%d @%lx #%d)",
275 kobjsize((void *) vml->vma->vm_start),
276 vml->vma->vm_start,
277 atomic_read(&vml->vma->vm_usage));
278 printk(vml->next ? " ->" : ".\n");
279 }
280}
281#endif /* DEBUG */
282
283static inline struct vm_area_struct *find_nommu_vma(unsigned long start)
284{
285 struct vm_area_struct *vma;
286 struct rb_node *n = nommu_vma_tree.rb_node;
287
288 while (n) {
289 vma = rb_entry(n, struct vm_area_struct, vm_rb);
290
291 if (start < vma->vm_start)
292 n = n->rb_left;
293 else if (start > vma->vm_start)
294 n = n->rb_right;
295 else
296 return vma;
297 }
298
299 return NULL;
300}
301
302static void add_nommu_vma(struct vm_area_struct *vma)
303{
304 struct vm_area_struct *pvma;
305 struct address_space *mapping;
306 struct rb_node **p = &nommu_vma_tree.rb_node;
307 struct rb_node *parent = NULL;
308
309 /* add the VMA to the mapping */
310 if (vma->vm_file) {
311 mapping = vma->vm_file->f_mapping;
312
313 flush_dcache_mmap_lock(mapping);
314 vma_prio_tree_insert(vma, &mapping->i_mmap);
315 flush_dcache_mmap_unlock(mapping);
316 }
317
318 /* add the VMA to the master list */
319 while (*p) {
320 parent = *p;
321 pvma = rb_entry(parent, struct vm_area_struct, vm_rb);
322
323 if (vma->vm_start < pvma->vm_start) {
324 p = &(*p)->rb_left;
325 }
326 else if (vma->vm_start > pvma->vm_start) {
327 p = &(*p)->rb_right;
328 }
329 else {
330 /* mappings are at the same address - this can only
331 * happen for shared-mem chardevs and shared file
332 * mappings backed by ramfs/tmpfs */
333 BUG_ON(!(pvma->vm_flags & VM_SHARED));
334
335 if (vma < pvma)
336 p = &(*p)->rb_left;
337 else if (vma > pvma)
338 p = &(*p)->rb_right;
339 else
340 BUG();
341 }
342 }
343
344 rb_link_node(&vma->vm_rb, parent, p);
345 rb_insert_color(&vma->vm_rb, &nommu_vma_tree);
346}
347
348static void delete_nommu_vma(struct vm_area_struct *vma)
349{
350 struct address_space *mapping;
351
352 /* remove the VMA from the mapping */
353 if (vma->vm_file) {
354 mapping = vma->vm_file->f_mapping;
355
356 flush_dcache_mmap_lock(mapping);
357 vma_prio_tree_remove(vma, &mapping->i_mmap);
358 flush_dcache_mmap_unlock(mapping);
359 }
360
361 /* remove from the master list */
362 rb_erase(&vma->vm_rb, &nommu_vma_tree);
363}
364
365/*
366 * determine whether a mapping should be permitted and, if so, what sort of
367 * mapping we're capable of supporting
368 */
369static int validate_mmap_request(struct file *file,
370 unsigned long addr,
371 unsigned long len,
372 unsigned long prot,
373 unsigned long flags,
374 unsigned long pgoff,
375 unsigned long *_capabilities)
376{
377 unsigned long capabilities;
378 unsigned long reqprot = prot;
379 int ret;
380
381 /* do the simple checks first */
382 if (flags & MAP_FIXED || addr) {
383 printk(KERN_DEBUG
384 "%d: Can't do fixed-address/overlay mmap of RAM\n",
385 current->pid);
386 return -EINVAL;
387 }
388
389 if ((flags & MAP_TYPE) != MAP_PRIVATE &&
390 (flags & MAP_TYPE) != MAP_SHARED)
391 return -EINVAL;
392
393 if (PAGE_ALIGN(len) == 0)
394 return addr;
395
396 if (len > TASK_SIZE)
397 return -EINVAL;
398
399 /* offset overflow? */
400 if ((pgoff + (len >> PAGE_SHIFT)) < pgoff)
401 return -EINVAL;
402
403 if (file) {
404 /* validate file mapping requests */
405 struct address_space *mapping;
406
407 /* files must support mmap */
408 if (!file->f_op || !file->f_op->mmap)
409 return -ENODEV;
410
411 /* work out if what we've got could possibly be shared
412 * - we support chardevs that provide their own "memory"
413 * - we support files/blockdevs that are memory backed
414 */
415 mapping = file->f_mapping;
416 if (!mapping)
417 mapping = file->f_dentry->d_inode->i_mapping;
418
419 capabilities = 0;
420 if (mapping && mapping->backing_dev_info)
421 capabilities = mapping->backing_dev_info->capabilities;
422
423 if (!capabilities) {
424 /* no explicit capabilities set, so assume some
425 * defaults */
426 switch (file->f_dentry->d_inode->i_mode & S_IFMT) {
427 case S_IFREG:
428 case S_IFBLK:
429 capabilities = BDI_CAP_MAP_COPY;
430 break;
431
432 case S_IFCHR:
433 capabilities =
434 BDI_CAP_MAP_DIRECT |
435 BDI_CAP_READ_MAP |
436 BDI_CAP_WRITE_MAP;
437 break;
438
439 default:
440 return -EINVAL;
441 }
442 }
443
444 /* eliminate any capabilities that we can't support on this
445 * device */
446 if (!file->f_op->get_unmapped_area)
447 capabilities &= ~BDI_CAP_MAP_DIRECT;
448 if (!file->f_op->read)
449 capabilities &= ~BDI_CAP_MAP_COPY;
450
451 if (flags & MAP_SHARED) {
452 /* do checks for writing, appending and locking */
453 if ((prot & PROT_WRITE) &&
454 !(file->f_mode & FMODE_WRITE))
455 return -EACCES;
456
457 if (IS_APPEND(file->f_dentry->d_inode) &&
458 (file->f_mode & FMODE_WRITE))
459 return -EACCES;
460
461 if (locks_verify_locked(file->f_dentry->d_inode))
462 return -EAGAIN;
463
464 if (!(capabilities & BDI_CAP_MAP_DIRECT))
465 return -ENODEV;
466
467 if (((prot & PROT_READ) && !(capabilities & BDI_CAP_READ_MAP)) ||
468 ((prot & PROT_WRITE) && !(capabilities & BDI_CAP_WRITE_MAP)) ||
469 ((prot & PROT_EXEC) && !(capabilities & BDI_CAP_EXEC_MAP))
470 ) {
471 printk("MAP_SHARED not completely supported on !MMU\n");
472 return -EINVAL;
473 }
474
475 /* we mustn't privatise shared mappings */
476 capabilities &= ~BDI_CAP_MAP_COPY;
477 }
478 else {
479 /* we're going to read the file into private memory we
480 * allocate */
481 if (!(capabilities & BDI_CAP_MAP_COPY))
482 return -ENODEV;
483
484 /* we don't permit a private writable mapping to be
485 * shared with the backing device */
486 if (prot & PROT_WRITE)
487 capabilities &= ~BDI_CAP_MAP_DIRECT;
488 }
489
490 /* handle executable mappings and implied executable
491 * mappings */
492 if (file->f_vfsmnt->mnt_flags & MNT_NOEXEC) {
493 if (prot & PROT_EXEC)
494 return -EPERM;
495 }
496 else if ((prot & PROT_READ) && !(prot & PROT_EXEC)) {
497 /* handle implication of PROT_EXEC by PROT_READ */
498 if (current->personality & READ_IMPLIES_EXEC) {
499 if (capabilities & BDI_CAP_EXEC_MAP)
500 prot |= PROT_EXEC;
501 }
502 }
503 else if ((prot & PROT_READ) &&
504 (prot & PROT_EXEC) &&
505 !(capabilities & BDI_CAP_EXEC_MAP)
506 ) {
507 /* backing file is not executable, try to copy */
508 capabilities &= ~BDI_CAP_MAP_DIRECT;
509 }
510 }
511 else {
512 /* anonymous mappings are always memory backed and can be
513 * privately mapped
514 */
515 capabilities = BDI_CAP_MAP_COPY;
516
517 /* handle PROT_EXEC implication by PROT_READ */
518 if ((prot & PROT_READ) &&
519 (current->personality & READ_IMPLIES_EXEC))
520 prot |= PROT_EXEC;
521 }
522
523 /* allow the security API to have its say */
524 ret = security_file_mmap(file, reqprot, prot, flags);
525 if (ret < 0)
526 return ret;
527
528 /* looks okay */
529 *_capabilities = capabilities;
530 return 0;
531}
532
533/*
534 * we've determined that we can make the mapping, now translate what we
535 * now know into VMA flags
536 */
537static unsigned long determine_vm_flags(struct file *file,
538 unsigned long prot,
539 unsigned long flags,
540 unsigned long capabilities)
541{
542 unsigned long vm_flags;
543
544 vm_flags = calc_vm_prot_bits(prot) | calc_vm_flag_bits(flags);
545 vm_flags |= VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
546 /* vm_flags |= mm->def_flags; */
547
548 if (!(capabilities & BDI_CAP_MAP_DIRECT)) {
549 /* attempt to share read-only copies of mapped file chunks */
550 if (file && !(prot & PROT_WRITE))
551 vm_flags |= VM_MAYSHARE;
552 }
553 else {
554 /* overlay a shareable mapping on the backing device or inode
555 * if possible - used for chardevs, ramfs/tmpfs/shmfs and
556 * romfs/cramfs */
557 if (flags & MAP_SHARED)
558 vm_flags |= VM_MAYSHARE | VM_SHARED;
559 else if ((((vm_flags & capabilities) ^ vm_flags) & BDI_CAP_VMFLAGS) == 0)
560 vm_flags |= VM_MAYSHARE;
561 }
562
563 /* refuse to let anyone share private mappings with this process if
564 * it's being traced - otherwise breakpoints set in it may interfere
565 * with another untraced process
566 */
567 if ((flags & MAP_PRIVATE) && (current->ptrace & PT_PTRACED))
568 vm_flags &= ~VM_MAYSHARE;
569
570 return vm_flags;
571}
572
573/*
574 * set up a shared mapping on a file
575 */
576static int do_mmap_shared_file(struct vm_area_struct *vma, unsigned long len)
577{
578 int ret;
579
580 ret = vma->vm_file->f_op->mmap(vma->vm_file, vma);
581 if (ret != -ENOSYS)
582 return ret;
583
584 /* getting an ENOSYS error indicates that direct mmap isn't
585 * possible (as opposed to tried but failed) so we'll fall
586 * through to making a private copy of the data and mapping
587 * that if we can */
588 return -ENODEV;
589}
590
591/*
592 * set up a private mapping or an anonymous shared mapping
593 */
594static int do_mmap_private(struct vm_area_struct *vma, unsigned long len)
595{
596 void *base;
597 int ret;
598
599 /* invoke the file's mapping function so that it can keep track of
600 * shared mappings on devices or memory
601 * - VM_MAYSHARE will be set if it may attempt to share
602 */
603 if (vma->vm_file) {
604 ret = vma->vm_file->f_op->mmap(vma->vm_file, vma);
605 if (ret != -ENOSYS) {
606 /* shouldn't return success if we're not sharing */
607 BUG_ON(ret == 0 && !(vma->vm_flags & VM_MAYSHARE));
608 return ret; /* success or a real error */
609 }
610
611 /* getting an ENOSYS error indicates that direct mmap isn't
612 * possible (as opposed to tried but failed) so we'll try to
613 * make a private copy of the data and map that instead */
614 }
615
616 /* allocate some memory to hold the mapping
617 * - note that this may not return a page-aligned address if the object
618 * we're allocating is smaller than a page
619 */
620 base = kmalloc(len, GFP_KERNEL);
621 if (!base)
622 goto enomem;
623
624 vma->vm_start = (unsigned long) base;
625 vma->vm_end = vma->vm_start + len;
626 vma->vm_flags |= VM_MAPPED_COPY;
627
628#ifdef WARN_ON_SLACK
629 if (len + WARN_ON_SLACK <= kobjsize(result))
630 printk("Allocation of %lu bytes from process %d has %lu bytes of slack\n",
631 len, current->pid, kobjsize(result) - len);
632#endif
633
634 if (vma->vm_file) {
635 /* read the contents of a file into the copy */
636 mm_segment_t old_fs;
637 loff_t fpos;
638
639 fpos = vma->vm_pgoff;
640 fpos <<= PAGE_SHIFT;
641
642 old_fs = get_fs();
643 set_fs(KERNEL_DS);
644 ret = vma->vm_file->f_op->read(vma->vm_file, base, len, &fpos);
645 set_fs(old_fs);
646
647 if (ret < 0)
648 goto error_free;
649
650 /* clear the last little bit */
651 if (ret < len)
652 memset(base + ret, 0, len - ret);
653
654 } else {
655 /* if it's an anonymous mapping, then just clear it */
656 memset(base, 0, len);
657 }
658
659 return 0;
660
661error_free:
662 kfree(base);
663 vma->vm_start = 0;
664 return ret;
665
666enomem:
667 printk("Allocation of length %lu from process %d failed\n",
668 len, current->pid);
669 show_free_areas();
670 return -ENOMEM;
671}
672
673/*
674 * handle mapping creation for uClinux
675 */
676unsigned long do_mmap_pgoff(struct file *file,
677 unsigned long addr,
678 unsigned long len,
679 unsigned long prot,
680 unsigned long flags,
681 unsigned long pgoff)
682{
683 struct vm_list_struct *vml = NULL;
684 struct vm_area_struct *vma = NULL;
685 struct rb_node *rb;
686 unsigned long capabilities, vm_flags;
687 void *result;
688 int ret;
689
690 /* decide whether we should attempt the mapping, and if so what sort of
691 * mapping */
692 ret = validate_mmap_request(file, addr, len, prot, flags, pgoff,
693 &capabilities);
694 if (ret < 0)
695 return ret;
696
697 /* we've determined that we can make the mapping, now translate what we
698 * now know into VMA flags */
699 vm_flags = determine_vm_flags(file, prot, flags, capabilities);
700
701 /* we're going to need to record the mapping if it works */
702 vml = kmalloc(sizeof(struct vm_list_struct), GFP_KERNEL);
703 if (!vml)
704 goto error_getting_vml;
705 memset(vml, 0, sizeof(*vml));
706
707 down_write(&nommu_vma_sem);
708
709 /* if we want to share, we need to check for VMAs created by other
710 * mmap() calls that overlap with our proposed mapping
711 * - we can only share with an exact match on most regular files
712 * - shared mappings on character devices and memory backed files are
713 * permitted to overlap inexactly as far as we are concerned for in
714 * these cases, sharing is handled in the driver or filesystem rather
715 * than here
716 */
717 if (vm_flags & VM_MAYSHARE) {
718 unsigned long pglen = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
719 unsigned long vmpglen;
720
721 for (rb = rb_first(&nommu_vma_tree); rb; rb = rb_next(rb)) {
722 vma = rb_entry(rb, struct vm_area_struct, vm_rb);
723
724 if (!(vma->vm_flags & VM_MAYSHARE))
725 continue;
726
727 /* search for overlapping mappings on the same file */
728 if (vma->vm_file->f_dentry->d_inode != file->f_dentry->d_inode)
729 continue;
730
731 if (vma->vm_pgoff >= pgoff + pglen)
732 continue;
733
734 vmpglen = vma->vm_end - vma->vm_start + PAGE_SIZE - 1;
735 vmpglen >>= PAGE_SHIFT;
736 if (pgoff >= vma->vm_pgoff + vmpglen)
737 continue;
738
739 /* handle inexactly overlapping matches between mappings */
740 if (vma->vm_pgoff != pgoff || vmpglen != pglen) {
741 if (!(capabilities & BDI_CAP_MAP_DIRECT))
742 goto sharing_violation;
743 continue;
744 }
745
746 /* we've found a VMA we can share */
747 atomic_inc(&vma->vm_usage);
748
749 vml->vma = vma;
750 result = (void *) vma->vm_start;
751 goto shared;
752 }
753
754 vma = NULL;
755
756 /* obtain the address at which to make a shared mapping
757 * - this is the hook for quasi-memory character devices to
758 * tell us the location of a shared mapping
759 */
760 if (file && file->f_op->get_unmapped_area) {
761 addr = file->f_op->get_unmapped_area(file, addr, len,
762 pgoff, flags);
763 if (IS_ERR((void *) addr)) {
764 ret = addr;
765 if (ret != (unsigned long) -ENOSYS)
766 goto error;
767
768 /* the driver refused to tell us where to site
769 * the mapping so we'll have to attempt to copy
770 * it */
771 ret = (unsigned long) -ENODEV;
772 if (!(capabilities & BDI_CAP_MAP_COPY))
773 goto error;
774
775 capabilities &= ~BDI_CAP_MAP_DIRECT;
776 }
777 }
778 }
779
780 /* we're going to need a VMA struct as well */
781 vma = kmalloc(sizeof(struct vm_area_struct), GFP_KERNEL);
782 if (!vma)
783 goto error_getting_vma;
784
785 memset(vma, 0, sizeof(*vma));
786 INIT_LIST_HEAD(&vma->anon_vma_node);
787 atomic_set(&vma->vm_usage, 1);
788 if (file)
789 get_file(file);
790 vma->vm_file = file;
791 vma->vm_flags = vm_flags;
792 vma->vm_start = addr;
793 vma->vm_end = addr + len;
794 vma->vm_pgoff = pgoff;
795
796 vml->vma = vma;
797
798 /* set up the mapping */
799 if (file && vma->vm_flags & VM_SHARED)
800 ret = do_mmap_shared_file(vma, len);
801 else
802 ret = do_mmap_private(vma, len);
803 if (ret < 0)
804 goto error;
805
806 /* okay... we have a mapping; now we have to register it */
807 result = (void *) vma->vm_start;
808
809 if (vma->vm_flags & VM_MAPPED_COPY) {
810 realalloc += kobjsize(result);
811 askedalloc += len;
812 }
813
814 realalloc += kobjsize(vma);
815 askedalloc += sizeof(*vma);
816
817 current->mm->total_vm += len >> PAGE_SHIFT;
818
819 add_nommu_vma(vma);
820
821 shared:
822 realalloc += kobjsize(vml);
823 askedalloc += sizeof(*vml);
824
825 vml->next = current->mm->context.vmlist;
826 current->mm->context.vmlist = vml;
827
828 up_write(&nommu_vma_sem);
829
830 if (prot & PROT_EXEC)
831 flush_icache_range((unsigned long) result,
832 (unsigned long) result + len);
833
834#ifdef DEBUG
835 printk("do_mmap:\n");
836 show_process_blocks();
837#endif
838
839 return (unsigned long) result;
840
841 error:
842 up_write(&nommu_vma_sem);
843 kfree(vml);
844 if (vma) {
845 fput(vma->vm_file);
846 kfree(vma);
847 }
848 return ret;
849
850 sharing_violation:
851 up_write(&nommu_vma_sem);
852 printk("Attempt to share mismatched mappings\n");
853 kfree(vml);
854 return -EINVAL;
855
856 error_getting_vma:
857 up_write(&nommu_vma_sem);
858 kfree(vml);
Greg Ungerer66aa2b42005-09-12 11:18:10 +1000859 printk("Allocation of vma for %lu byte allocation from process %d failed\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 len, current->pid);
861 show_free_areas();
862 return -ENOMEM;
863
864 error_getting_vml:
865 printk("Allocation of vml for %lu byte allocation from process %d failed\n",
866 len, current->pid);
867 show_free_areas();
868 return -ENOMEM;
869}
870
871/*
872 * handle mapping disposal for uClinux
873 */
874static void put_vma(struct vm_area_struct *vma)
875{
876 if (vma) {
877 down_write(&nommu_vma_sem);
878
879 if (atomic_dec_and_test(&vma->vm_usage)) {
880 delete_nommu_vma(vma);
881
882 if (vma->vm_ops && vma->vm_ops->close)
883 vma->vm_ops->close(vma);
884
885 /* IO memory and memory shared directly out of the pagecache from
886 * ramfs/tmpfs mustn't be released here */
887 if (vma->vm_flags & VM_MAPPED_COPY) {
888 realalloc -= kobjsize((void *) vma->vm_start);
889 askedalloc -= vma->vm_end - vma->vm_start;
890 kfree((void *) vma->vm_start);
891 }
892
893 realalloc -= kobjsize(vma);
894 askedalloc -= sizeof(*vma);
895
896 if (vma->vm_file)
897 fput(vma->vm_file);
898 kfree(vma);
899 }
900
901 up_write(&nommu_vma_sem);
902 }
903}
904
905int do_munmap(struct mm_struct *mm, unsigned long addr, size_t len)
906{
907 struct vm_list_struct *vml, **parent;
908 unsigned long end = addr + len;
909
910#ifdef DEBUG
911 printk("do_munmap:\n");
912#endif
913
914 for (parent = &mm->context.vmlist; *parent; parent = &(*parent)->next)
915 if ((*parent)->vma->vm_start == addr &&
Greg Ungerer66aa2b42005-09-12 11:18:10 +1000916 ((len == 0) || ((*parent)->vma->vm_end == end)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 goto found;
918
919 printk("munmap of non-mmaped memory by process %d (%s): %p\n",
920 current->pid, current->comm, (void *) addr);
921 return -EINVAL;
922
923 found:
924 vml = *parent;
925
926 put_vma(vml->vma);
927
928 *parent = vml->next;
929 realalloc -= kobjsize(vml);
930 askedalloc -= sizeof(*vml);
931 kfree(vml);
Hugh Dickins365e9c872005-10-29 18:16:18 -0700932
933 update_hiwater_vm(mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 mm->total_vm -= len >> PAGE_SHIFT;
935
936#ifdef DEBUG
937 show_process_blocks();
938#endif
939
940 return 0;
941}
942
943/* Release all mmaps. */
944void exit_mmap(struct mm_struct * mm)
945{
946 struct vm_list_struct *tmp;
947
948 if (mm) {
949#ifdef DEBUG
950 printk("Exit_mmap:\n");
951#endif
952
953 mm->total_vm = 0;
954
955 while ((tmp = mm->context.vmlist)) {
956 mm->context.vmlist = tmp->next;
957 put_vma(tmp->vma);
958
959 realalloc -= kobjsize(tmp);
960 askedalloc -= sizeof(*tmp);
961 kfree(tmp);
962 }
963
964#ifdef DEBUG
965 show_process_blocks();
966#endif
967 }
968}
969
970asmlinkage long sys_munmap(unsigned long addr, size_t len)
971{
972 int ret;
973 struct mm_struct *mm = current->mm;
974
975 down_write(&mm->mmap_sem);
976 ret = do_munmap(mm, addr, len);
977 up_write(&mm->mmap_sem);
978 return ret;
979}
980
981unsigned long do_brk(unsigned long addr, unsigned long len)
982{
983 return -ENOMEM;
984}
985
986/*
987 * Expand (or shrink) an existing mapping, potentially moving it at the
988 * same time (controlled by the MREMAP_MAYMOVE flag and available VM space)
989 *
990 * MREMAP_FIXED option added 5-Dec-1999 by Benjamin LaHaise
991 * This option implies MREMAP_MAYMOVE.
992 *
993 * on uClinux, we only permit changing a mapping's size, and only as long as it stays within the
994 * hole allocated by the kmalloc() call in do_mmap_pgoff() and the block is not shareable
995 */
996unsigned long do_mremap(unsigned long addr,
997 unsigned long old_len, unsigned long new_len,
998 unsigned long flags, unsigned long new_addr)
999{
1000 struct vm_list_struct *vml = NULL;
1001
1002 /* insanity checks first */
1003 if (new_len == 0)
1004 return (unsigned long) -EINVAL;
1005
1006 if (flags & MREMAP_FIXED && new_addr != addr)
1007 return (unsigned long) -EINVAL;
1008
1009 for (vml = current->mm->context.vmlist; vml; vml = vml->next)
1010 if (vml->vma->vm_start == addr)
1011 goto found;
1012
1013 return (unsigned long) -EINVAL;
1014
1015 found:
1016 if (vml->vma->vm_end != vml->vma->vm_start + old_len)
1017 return (unsigned long) -EFAULT;
1018
1019 if (vml->vma->vm_flags & VM_MAYSHARE)
1020 return (unsigned long) -EPERM;
1021
1022 if (new_len > kobjsize((void *) addr))
1023 return (unsigned long) -ENOMEM;
1024
1025 /* all checks complete - do it */
1026 vml->vma->vm_end = vml->vma->vm_start + new_len;
1027
1028 askedalloc -= old_len;
1029 askedalloc += new_len;
1030
1031 return vml->vma->vm_start;
1032}
1033
1034/*
1035 * Look up the first VMA which satisfies addr < vm_end, NULL if none
1036 */
1037struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)
1038{
1039 struct vm_list_struct *vml;
1040
1041 for (vml = mm->context.vmlist; vml; vml = vml->next)
1042 if (addr >= vml->vma->vm_start && addr < vml->vma->vm_end)
1043 return vml->vma;
1044
1045 return NULL;
1046}
1047
1048EXPORT_SYMBOL(find_vma);
1049
Linus Torvalds6aab3412005-11-28 14:34:23 -08001050struct page *follow_page(struct vm_area_struct *vma, unsigned long address,
Hugh Dickinsdeceb6c2005-10-29 18:16:33 -07001051 unsigned int foll_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052{
1053 return NULL;
1054}
1055
1056struct vm_area_struct *find_extend_vma(struct mm_struct *mm, unsigned long addr)
1057{
1058 return NULL;
1059}
1060
1061int remap_pfn_range(struct vm_area_struct *vma, unsigned long from,
1062 unsigned long to, unsigned long size, pgprot_t prot)
1063{
Greg Ungerer66aa2b42005-09-12 11:18:10 +10001064 vma->vm_start = vma->vm_pgoff << PAGE_SHIFT;
1065 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066}
1067
1068void swap_unplug_io_fn(struct backing_dev_info *bdi, struct page *page)
1069{
1070}
1071
1072unsigned long arch_get_unmapped_area(struct file *file, unsigned long addr,
1073 unsigned long len, unsigned long pgoff, unsigned long flags)
1074{
1075 return -ENOMEM;
1076}
1077
Wolfgang Wander1363c3c2005-06-21 17:14:49 -07001078void arch_unmap_area(struct mm_struct *mm, unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079{
1080}
1081
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082void unmap_mapping_range(struct address_space *mapping,
1083 loff_t const holebegin, loff_t const holelen,
1084 int even_cows)
1085{
1086}
1087
1088/*
1089 * Check that a process has enough memory to allocate a new virtual
1090 * mapping. 0 means there is enough memory for the allocation to
1091 * succeed and -ENOMEM implies there is not.
1092 *
1093 * We currently support three overcommit policies, which are set via the
1094 * vm.overcommit_memory sysctl. See Documentation/vm/overcommit-accounting
1095 *
1096 * Strict overcommit modes added 2002 Feb 26 by Alan Cox.
1097 * Additional code 2002 Jul 20 by Robert Love.
1098 *
1099 * cap_sys_admin is 1 if the process has admin privileges, 0 otherwise.
1100 *
1101 * Note this is a helper function intended to be used by LSMs which
1102 * wish to use this logic.
1103 */
1104int __vm_enough_memory(long pages, int cap_sys_admin)
1105{
1106 unsigned long free, allowed;
1107
1108 vm_acct_memory(pages);
1109
1110 /*
1111 * Sometimes we want to use more memory than we have
1112 */
1113 if (sysctl_overcommit_memory == OVERCOMMIT_ALWAYS)
1114 return 0;
1115
1116 if (sysctl_overcommit_memory == OVERCOMMIT_GUESS) {
1117 unsigned long n;
1118
1119 free = get_page_cache_size();
1120 free += nr_swap_pages;
1121
1122 /*
1123 * Any slabs which are created with the
1124 * SLAB_RECLAIM_ACCOUNT flag claim to have contents
1125 * which are reclaimable, under pressure. The dentry
1126 * cache and most inode caches should fall into this
1127 */
1128 free += atomic_read(&slab_reclaim_pages);
1129
1130 /*
1131 * Leave the last 3% for root
1132 */
1133 if (!cap_sys_admin)
1134 free -= free / 32;
1135
1136 if (free > pages)
1137 return 0;
1138
1139 /*
1140 * nr_free_pages() is very expensive on large systems,
1141 * only call if we're about to fail.
1142 */
1143 n = nr_free_pages();
1144 if (!cap_sys_admin)
1145 n -= n / 32;
1146 free += n;
1147
1148 if (free > pages)
1149 return 0;
1150 vm_unacct_memory(pages);
1151 return -ENOMEM;
1152 }
1153
1154 allowed = totalram_pages * sysctl_overcommit_ratio / 100;
1155 /*
1156 * Leave the last 3% for root
1157 */
1158 if (!cap_sys_admin)
1159 allowed -= allowed / 32;
1160 allowed += total_swap_pages;
1161
1162 /* Don't let a single process grow too big:
1163 leave 3% of the size of this process for other processes */
1164 allowed -= current->mm->total_vm / 32;
1165
Simon Derr2f60f8d2005-08-04 19:52:03 -07001166 /*
1167 * cast `allowed' as a signed long because vm_committed_space
1168 * sometimes has a negative value
1169 */
1170 if (atomic_read(&vm_committed_space) < (long)allowed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 return 0;
1172
1173 vm_unacct_memory(pages);
1174
1175 return -ENOMEM;
1176}
1177
1178int in_gate_area_no_task(unsigned long addr)
1179{
1180 return 0;
1181}
David Howellsb0e15192006-01-06 00:11:42 -08001182
1183struct page *filemap_nopage(struct vm_area_struct *area,
1184 unsigned long address, int *type)
1185{
1186 BUG();
1187 return NULL;
1188}