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