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