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