[PATCH] NOMMU: Permit ptrace to ignore non-PROT_WRITE VMAs in NOMMU mode
[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(__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 cotrol 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 static inline struct vm_area_struct *find_nommu_vma(unsigned long start)
314 {
315         struct vm_area_struct *vma;
316         struct rb_node *n = nommu_vma_tree.rb_node;
317
318         while (n) {
319                 vma = rb_entry(n, struct vm_area_struct, vm_rb);
320
321                 if (start < vma->vm_start)
322                         n = n->rb_left;
323                 else if (start > vma->vm_start)
324                         n = n->rb_right;
325                 else
326                         return vma;
327         }
328
329         return NULL;
330 }
331
332 static void add_nommu_vma(struct vm_area_struct *vma)
333 {
334         struct vm_area_struct *pvma;
335         struct address_space *mapping;
336         struct rb_node **p = &nommu_vma_tree.rb_node;
337         struct rb_node *parent = NULL;
338
339         /* add the VMA to the mapping */
340         if (vma->vm_file) {
341                 mapping = vma->vm_file->f_mapping;
342
343                 flush_dcache_mmap_lock(mapping);
344                 vma_prio_tree_insert(vma, &mapping->i_mmap);
345                 flush_dcache_mmap_unlock(mapping);
346         }
347
348         /* add the VMA to the master list */
349         while (*p) {
350                 parent = *p;
351                 pvma = rb_entry(parent, struct vm_area_struct, vm_rb);
352
353                 if (vma->vm_start < pvma->vm_start) {
354                         p = &(*p)->rb_left;
355                 }
356                 else if (vma->vm_start > pvma->vm_start) {
357                         p = &(*p)->rb_right;
358                 }
359                 else {
360                         /* mappings are at the same address - this can only
361                          * happen for shared-mem chardevs and shared file
362                          * mappings backed by ramfs/tmpfs */
363                         BUG_ON(!(pvma->vm_flags & VM_SHARED));
364
365                         if (vma < pvma)
366                                 p = &(*p)->rb_left;
367                         else if (vma > pvma)
368                                 p = &(*p)->rb_right;
369                         else
370                                 BUG();
371                 }
372         }
373
374         rb_link_node(&vma->vm_rb, parent, p);
375         rb_insert_color(&vma->vm_rb, &nommu_vma_tree);
376 }
377
378 static void delete_nommu_vma(struct vm_area_struct *vma)
379 {
380         struct address_space *mapping;
381
382         /* remove the VMA from the mapping */
383         if (vma->vm_file) {
384                 mapping = vma->vm_file->f_mapping;
385
386                 flush_dcache_mmap_lock(mapping);
387                 vma_prio_tree_remove(vma, &mapping->i_mmap);
388                 flush_dcache_mmap_unlock(mapping);
389         }
390
391         /* remove from the master list */
392         rb_erase(&vma->vm_rb, &nommu_vma_tree);
393 }
394
395 /*
396  * determine whether a mapping should be permitted and, if so, what sort of
397  * mapping we're capable of supporting
398  */
399 static int validate_mmap_request(struct file *file,
400                                  unsigned long addr,
401                                  unsigned long len,
402                                  unsigned long prot,
403                                  unsigned long flags,
404                                  unsigned long pgoff,
405                                  unsigned long *_capabilities)
406 {
407         unsigned long capabilities;
408         unsigned long reqprot = prot;
409         int ret;
410
411         /* do the simple checks first */
412         if (flags & MAP_FIXED || addr) {
413                 printk(KERN_DEBUG
414                        "%d: Can't do fixed-address/overlay mmap of RAM\n",
415                        current->pid);
416                 return -EINVAL;
417         }
418
419         if ((flags & MAP_TYPE) != MAP_PRIVATE &&
420             (flags & MAP_TYPE) != MAP_SHARED)
421                 return -EINVAL;
422
423         if (PAGE_ALIGN(len) == 0)
424                 return addr;
425
426         if (len > TASK_SIZE)
427                 return -EINVAL;
428
429         /* offset overflow? */
430         if ((pgoff + (len >> PAGE_SHIFT)) < pgoff)
431                 return -EINVAL;
432
433         if (file) {
434                 /* validate file mapping requests */
435                 struct address_space *mapping;
436
437                 /* files must support mmap */
438                 if (!file->f_op || !file->f_op->mmap)
439                         return -ENODEV;
440
441                 /* work out if what we've got could possibly be shared
442                  * - we support chardevs that provide their own "memory"
443                  * - we support files/blockdevs that are memory backed
444                  */
445                 mapping = file->f_mapping;
446                 if (!mapping)
447                         mapping = file->f_dentry->d_inode->i_mapping;
448
449                 capabilities = 0;
450                 if (mapping && mapping->backing_dev_info)
451                         capabilities = mapping->backing_dev_info->capabilities;
452
453                 if (!capabilities) {
454                         /* no explicit capabilities set, so assume some
455                          * defaults */
456                         switch (file->f_dentry->d_inode->i_mode & S_IFMT) {
457                         case S_IFREG:
458                         case S_IFBLK:
459                                 capabilities = BDI_CAP_MAP_COPY;
460                                 break;
461
462                         case S_IFCHR:
463                                 capabilities =
464                                         BDI_CAP_MAP_DIRECT |
465                                         BDI_CAP_READ_MAP |
466                                         BDI_CAP_WRITE_MAP;
467                                 break;
468
469                         default:
470                                 return -EINVAL;
471                         }
472                 }
473
474                 /* eliminate any capabilities that we can't support on this
475                  * device */
476                 if (!file->f_op->get_unmapped_area)
477                         capabilities &= ~BDI_CAP_MAP_DIRECT;
478                 if (!file->f_op->read)
479                         capabilities &= ~BDI_CAP_MAP_COPY;
480
481                 if (flags & MAP_SHARED) {
482                         /* do checks for writing, appending and locking */
483                         if ((prot & PROT_WRITE) &&
484                             !(file->f_mode & FMODE_WRITE))
485                                 return -EACCES;
486
487                         if (IS_APPEND(file->f_dentry->d_inode) &&
488                             (file->f_mode & FMODE_WRITE))
489                                 return -EACCES;
490
491                         if (locks_verify_locked(file->f_dentry->d_inode))
492                                 return -EAGAIN;
493
494                         if (!(capabilities & BDI_CAP_MAP_DIRECT))
495                                 return -ENODEV;
496
497                         if (((prot & PROT_READ)  && !(capabilities & BDI_CAP_READ_MAP))  ||
498                             ((prot & PROT_WRITE) && !(capabilities & BDI_CAP_WRITE_MAP)) ||
499                             ((prot & PROT_EXEC)  && !(capabilities & BDI_CAP_EXEC_MAP))
500                             ) {
501                                 printk("MAP_SHARED not completely supported on !MMU\n");
502                                 return -EINVAL;
503                         }
504
505                         /* we mustn't privatise shared mappings */
506                         capabilities &= ~BDI_CAP_MAP_COPY;
507                 }
508                 else {
509                         /* we're going to read the file into private memory we
510                          * allocate */
511                         if (!(capabilities & BDI_CAP_MAP_COPY))
512                                 return -ENODEV;
513
514                         /* we don't permit a private writable mapping to be
515                          * shared with the backing device */
516                         if (prot & PROT_WRITE)
517                                 capabilities &= ~BDI_CAP_MAP_DIRECT;
518                 }
519
520                 /* handle executable mappings and implied executable
521                  * mappings */
522                 if (file->f_vfsmnt->mnt_flags & MNT_NOEXEC) {
523                         if (prot & PROT_EXEC)
524                                 return -EPERM;
525                 }
526                 else if ((prot & PROT_READ) && !(prot & PROT_EXEC)) {
527                         /* handle implication of PROT_EXEC by PROT_READ */
528                         if (current->personality & READ_IMPLIES_EXEC) {
529                                 if (capabilities & BDI_CAP_EXEC_MAP)
530                                         prot |= PROT_EXEC;
531                         }
532                 }
533                 else if ((prot & PROT_READ) &&
534                          (prot & PROT_EXEC) &&
535                          !(capabilities & BDI_CAP_EXEC_MAP)
536                          ) {
537                         /* backing file is not executable, try to copy */
538                         capabilities &= ~BDI_CAP_MAP_DIRECT;
539                 }
540         }
541         else {
542                 /* anonymous mappings are always memory backed and can be
543                  * privately mapped
544                  */
545                 capabilities = BDI_CAP_MAP_COPY;
546
547                 /* handle PROT_EXEC implication by PROT_READ */
548                 if ((prot & PROT_READ) &&
549                     (current->personality & READ_IMPLIES_EXEC))
550                         prot |= PROT_EXEC;
551         }
552
553         /* allow the security API to have its say */
554         ret = security_file_mmap(file, reqprot, prot, flags);
555         if (ret < 0)
556                 return ret;
557
558         /* looks okay */
559         *_capabilities = capabilities;
560         return 0;
561 }
562
563 /*
564  * we've determined that we can make the mapping, now translate what we
565  * now know into VMA flags
566  */
567 static unsigned long determine_vm_flags(struct file *file,
568                                         unsigned long prot,
569                                         unsigned long flags,
570                                         unsigned long capabilities)
571 {
572         unsigned long vm_flags;
573
574         vm_flags = calc_vm_prot_bits(prot) | calc_vm_flag_bits(flags);
575         vm_flags |= VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
576         /* vm_flags |= mm->def_flags; */
577
578         if (!(capabilities & BDI_CAP_MAP_DIRECT)) {
579                 /* attempt to share read-only copies of mapped file chunks */
580                 if (file && !(prot & PROT_WRITE))
581                         vm_flags |= VM_MAYSHARE;
582         }
583         else {
584                 /* overlay a shareable mapping on the backing device or inode
585                  * if possible - used for chardevs, ramfs/tmpfs/shmfs and
586                  * romfs/cramfs */
587                 if (flags & MAP_SHARED)
588                         vm_flags |= VM_MAYSHARE | VM_SHARED;
589                 else if ((((vm_flags & capabilities) ^ vm_flags) & BDI_CAP_VMFLAGS) == 0)
590                         vm_flags |= VM_MAYSHARE;
591         }
592
593         /* refuse to let anyone share private mappings with this process if
594          * it's being traced - otherwise breakpoints set in it may interfere
595          * with another untraced process
596          */
597         if ((flags & MAP_PRIVATE) && (current->ptrace & PT_PTRACED))
598                 vm_flags &= ~VM_MAYSHARE;
599
600         return vm_flags;
601 }
602
603 /*
604  * set up a shared mapping on a file
605  */
606 static int do_mmap_shared_file(struct vm_area_struct *vma, unsigned long len)
607 {
608         int ret;
609
610         ret = vma->vm_file->f_op->mmap(vma->vm_file, vma);
611         if (ret != -ENOSYS)
612                 return ret;
613
614         /* getting an ENOSYS error indicates that direct mmap isn't
615          * possible (as opposed to tried but failed) so we'll fall
616          * through to making a private copy of the data and mapping
617          * that if we can */
618         return -ENODEV;
619 }
620
621 /*
622  * set up a private mapping or an anonymous shared mapping
623  */
624 static int do_mmap_private(struct vm_area_struct *vma, unsigned long len)
625 {
626         void *base;
627         int ret;
628
629         /* invoke the file's mapping function so that it can keep track of
630          * shared mappings on devices or memory
631          * - VM_MAYSHARE will be set if it may attempt to share
632          */
633         if (vma->vm_file) {
634                 ret = vma->vm_file->f_op->mmap(vma->vm_file, vma);
635                 if (ret != -ENOSYS) {
636                         /* shouldn't return success if we're not sharing */
637                         BUG_ON(ret == 0 && !(vma->vm_flags & VM_MAYSHARE));
638                         return ret; /* success or a real error */
639                 }
640
641                 /* getting an ENOSYS error indicates that direct mmap isn't
642                  * possible (as opposed to tried but failed) so we'll try to
643                  * make a private copy of the data and map that instead */
644         }
645
646         /* allocate some memory to hold the mapping
647          * - note that this may not return a page-aligned address if the object
648          *   we're allocating is smaller than a page
649          */
650         base = kmalloc(len, GFP_KERNEL|__GFP_COMP);
651         if (!base)
652                 goto enomem;
653
654         vma->vm_start = (unsigned long) base;
655         vma->vm_end = vma->vm_start + len;
656         vma->vm_flags |= VM_MAPPED_COPY;
657
658 #ifdef WARN_ON_SLACK
659         if (len + WARN_ON_SLACK <= kobjsize(result))
660                 printk("Allocation of %lu bytes from process %d has %lu bytes of slack\n",
661                        len, current->pid, kobjsize(result) - len);
662 #endif
663
664         if (vma->vm_file) {
665                 /* read the contents of a file into the copy */
666                 mm_segment_t old_fs;
667                 loff_t fpos;
668
669                 fpos = vma->vm_pgoff;
670                 fpos <<= PAGE_SHIFT;
671
672                 old_fs = get_fs();
673                 set_fs(KERNEL_DS);
674                 ret = vma->vm_file->f_op->read(vma->vm_file, base, len, &fpos);
675                 set_fs(old_fs);
676
677                 if (ret < 0)
678                         goto error_free;
679
680                 /* clear the last little bit */
681                 if (ret < len)
682                         memset(base + ret, 0, len - ret);
683
684         } else {
685                 /* if it's an anonymous mapping, then just clear it */
686                 memset(base, 0, len);
687         }
688
689         return 0;
690
691 error_free:
692         kfree(base);
693         vma->vm_start = 0;
694         return ret;
695
696 enomem:
697         printk("Allocation of length %lu from process %d failed\n",
698                len, current->pid);
699         show_free_areas();
700         return -ENOMEM;
701 }
702
703 /*
704  * handle mapping creation for uClinux
705  */
706 unsigned long do_mmap_pgoff(struct file *file,
707                             unsigned long addr,
708                             unsigned long len,
709                             unsigned long prot,
710                             unsigned long flags,
711                             unsigned long pgoff)
712 {
713         struct vm_list_struct *vml = NULL;
714         struct vm_area_struct *vma = NULL;
715         struct rb_node *rb;
716         unsigned long capabilities, vm_flags;
717         void *result;
718         int ret;
719
720         /* decide whether we should attempt the mapping, and if so what sort of
721          * mapping */
722         ret = validate_mmap_request(file, addr, len, prot, flags, pgoff,
723                                     &capabilities);
724         if (ret < 0)
725                 return ret;
726
727         /* we've determined that we can make the mapping, now translate what we
728          * now know into VMA flags */
729         vm_flags = determine_vm_flags(file, prot, flags, capabilities);
730
731         /* we're going to need to record the mapping if it works */
732         vml = kmalloc(sizeof(struct vm_list_struct), GFP_KERNEL);
733         if (!vml)
734                 goto error_getting_vml;
735         memset(vml, 0, sizeof(*vml));
736
737         down_write(&nommu_vma_sem);
738
739         /* if we want to share, we need to check for VMAs created by other
740          * mmap() calls that overlap with our proposed mapping
741          * - we can only share with an exact match on most regular files
742          * - shared mappings on character devices and memory backed files are
743          *   permitted to overlap inexactly as far as we are concerned for in
744          *   these cases, sharing is handled in the driver or filesystem rather
745          *   than here
746          */
747         if (vm_flags & VM_MAYSHARE) {
748                 unsigned long pglen = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
749                 unsigned long vmpglen;
750
751                 for (rb = rb_first(&nommu_vma_tree); rb; rb = rb_next(rb)) {
752                         vma = rb_entry(rb, struct vm_area_struct, vm_rb);
753
754                         if (!(vma->vm_flags & VM_MAYSHARE))
755                                 continue;
756
757                         /* search for overlapping mappings on the same file */
758                         if (vma->vm_file->f_dentry->d_inode != file->f_dentry->d_inode)
759                                 continue;
760
761                         if (vma->vm_pgoff >= pgoff + pglen)
762                                 continue;
763
764                         vmpglen = vma->vm_end - vma->vm_start + PAGE_SIZE - 1;
765                         vmpglen >>= PAGE_SHIFT;
766                         if (pgoff >= vma->vm_pgoff + vmpglen)
767                                 continue;
768
769                         /* handle inexactly overlapping matches between mappings */
770                         if (vma->vm_pgoff != pgoff || vmpglen != pglen) {
771                                 if (!(capabilities & BDI_CAP_MAP_DIRECT))
772                                         goto sharing_violation;
773                                 continue;
774                         }
775
776                         /* we've found a VMA we can share */
777                         atomic_inc(&vma->vm_usage);
778
779                         vml->vma = vma;
780                         result = (void *) vma->vm_start;
781                         goto shared;
782                 }
783
784                 vma = NULL;
785
786                 /* obtain the address at which to make a shared mapping
787                  * - this is the hook for quasi-memory character devices to
788                  *   tell us the location of a shared mapping
789                  */
790                 if (file && file->f_op->get_unmapped_area) {
791                         addr = file->f_op->get_unmapped_area(file, addr, len,
792                                                              pgoff, flags);
793                         if (IS_ERR((void *) addr)) {
794                                 ret = addr;
795                                 if (ret != (unsigned long) -ENOSYS)
796                                         goto error;
797
798                                 /* the driver refused to tell us where to site
799                                  * the mapping so we'll have to attempt to copy
800                                  * it */
801                                 ret = (unsigned long) -ENODEV;
802                                 if (!(capabilities & BDI_CAP_MAP_COPY))
803                                         goto error;
804
805                                 capabilities &= ~BDI_CAP_MAP_DIRECT;
806                         }
807                 }
808         }
809
810         /* we're going to need a VMA struct as well */
811         vma = kmalloc(sizeof(struct vm_area_struct), GFP_KERNEL);
812         if (!vma)
813                 goto error_getting_vma;
814
815         memset(vma, 0, sizeof(*vma));
816         INIT_LIST_HEAD(&vma->anon_vma_node);
817         atomic_set(&vma->vm_usage, 1);
818         if (file)
819                 get_file(file);
820         vma->vm_file    = file;
821         vma->vm_flags   = vm_flags;
822         vma->vm_start   = addr;
823         vma->vm_end     = addr + len;
824         vma->vm_pgoff   = pgoff;
825
826         vml->vma = vma;
827
828         /* set up the mapping */
829         if (file && vma->vm_flags & VM_SHARED)
830                 ret = do_mmap_shared_file(vma, len);
831         else
832                 ret = do_mmap_private(vma, len);
833         if (ret < 0)
834                 goto error;
835
836         /* okay... we have a mapping; now we have to register it */
837         result = (void *) vma->vm_start;
838
839         if (vma->vm_flags & VM_MAPPED_COPY) {
840                 realalloc += kobjsize(result);
841                 askedalloc += len;
842         }
843
844         realalloc += kobjsize(vma);
845         askedalloc += sizeof(*vma);
846
847         current->mm->total_vm += len >> PAGE_SHIFT;
848
849         add_nommu_vma(vma);
850
851  shared:
852         realalloc += kobjsize(vml);
853         askedalloc += sizeof(*vml);
854
855         vml->next = current->mm->context.vmlist;
856         current->mm->context.vmlist = vml;
857
858         up_write(&nommu_vma_sem);
859
860         if (prot & PROT_EXEC)
861                 flush_icache_range((unsigned long) result,
862                                    (unsigned long) result + len);
863
864 #ifdef DEBUG
865         printk("do_mmap:\n");
866         show_process_blocks();
867 #endif
868
869         return (unsigned long) result;
870
871  error:
872         up_write(&nommu_vma_sem);
873         kfree(vml);
874         if (vma) {
875                 fput(vma->vm_file);
876                 kfree(vma);
877         }
878         return ret;
879
880  sharing_violation:
881         up_write(&nommu_vma_sem);
882         printk("Attempt to share mismatched mappings\n");
883         kfree(vml);
884         return -EINVAL;
885
886  error_getting_vma:
887         up_write(&nommu_vma_sem);
888         kfree(vml);
889         printk("Allocation of vma for %lu byte allocation from process %d failed\n",
890                len, current->pid);
891         show_free_areas();
892         return -ENOMEM;
893
894  error_getting_vml:
895         printk("Allocation of vml for %lu byte allocation from process %d failed\n",
896                len, current->pid);
897         show_free_areas();
898         return -ENOMEM;
899 }
900
901 /*
902  * handle mapping disposal for uClinux
903  */
904 static void put_vma(struct vm_area_struct *vma)
905 {
906         if (vma) {
907                 down_write(&nommu_vma_sem);
908
909                 if (atomic_dec_and_test(&vma->vm_usage)) {
910                         delete_nommu_vma(vma);
911
912                         if (vma->vm_ops && vma->vm_ops->close)
913                                 vma->vm_ops->close(vma);
914
915                         /* IO memory and memory shared directly out of the pagecache from
916                          * ramfs/tmpfs mustn't be released here */
917                         if (vma->vm_flags & VM_MAPPED_COPY) {
918                                 realalloc -= kobjsize((void *) vma->vm_start);
919                                 askedalloc -= vma->vm_end - vma->vm_start;
920                                 kfree((void *) vma->vm_start);
921                         }
922
923                         realalloc -= kobjsize(vma);
924                         askedalloc -= sizeof(*vma);
925
926                         if (vma->vm_file)
927                                 fput(vma->vm_file);
928                         kfree(vma);
929                 }
930
931                 up_write(&nommu_vma_sem);
932         }
933 }
934
935 int do_munmap(struct mm_struct *mm, unsigned long addr, size_t len)
936 {
937         struct vm_list_struct *vml, **parent;
938         unsigned long end = addr + len;
939
940 #ifdef DEBUG
941         printk("do_munmap:\n");
942 #endif
943
944         for (parent = &mm->context.vmlist; *parent; parent = &(*parent)->next)
945                 if ((*parent)->vma->vm_start == addr &&
946                     ((len == 0) || ((*parent)->vma->vm_end == end)))
947                         goto found;
948
949         printk("munmap of non-mmaped memory by process %d (%s): %p\n",
950                current->pid, current->comm, (void *) addr);
951         return -EINVAL;
952
953  found:
954         vml = *parent;
955
956         put_vma(vml->vma);
957
958         *parent = vml->next;
959         realalloc -= kobjsize(vml);
960         askedalloc -= sizeof(*vml);
961         kfree(vml);
962
963         update_hiwater_vm(mm);
964         mm->total_vm -= len >> PAGE_SHIFT;
965
966 #ifdef DEBUG
967         show_process_blocks();
968 #endif
969
970         return 0;
971 }
972
973 /* Release all mmaps. */
974 void exit_mmap(struct mm_struct * mm)
975 {
976         struct vm_list_struct *tmp;
977
978         if (mm) {
979 #ifdef DEBUG
980                 printk("Exit_mmap:\n");
981 #endif
982
983                 mm->total_vm = 0;
984
985                 while ((tmp = mm->context.vmlist)) {
986                         mm->context.vmlist = tmp->next;
987                         put_vma(tmp->vma);
988
989                         realalloc -= kobjsize(tmp);
990                         askedalloc -= sizeof(*tmp);
991                         kfree(tmp);
992                 }
993
994 #ifdef DEBUG
995                 show_process_blocks();
996 #endif
997         }
998 }
999
1000 asmlinkage long sys_munmap(unsigned long addr, size_t len)
1001 {
1002         int ret;
1003         struct mm_struct *mm = current->mm;
1004
1005         down_write(&mm->mmap_sem);
1006         ret = do_munmap(mm, addr, len);
1007         up_write(&mm->mmap_sem);
1008         return ret;
1009 }
1010
1011 unsigned long do_brk(unsigned long addr, unsigned long len)
1012 {
1013         return -ENOMEM;
1014 }
1015
1016 /*
1017  * Expand (or shrink) an existing mapping, potentially moving it at the
1018  * same time (controlled by the MREMAP_MAYMOVE flag and available VM space)
1019  *
1020  * MREMAP_FIXED option added 5-Dec-1999 by Benjamin LaHaise
1021  * This option implies MREMAP_MAYMOVE.
1022  *
1023  * on uClinux, we only permit changing a mapping's size, and only as long as it stays within the
1024  * hole allocated by the kmalloc() call in do_mmap_pgoff() and the block is not shareable
1025  */
1026 unsigned long do_mremap(unsigned long addr,
1027                         unsigned long old_len, unsigned long new_len,
1028                         unsigned long flags, unsigned long new_addr)
1029 {
1030         struct vm_list_struct *vml = NULL;
1031
1032         /* insanity checks first */
1033         if (new_len == 0)
1034                 return (unsigned long) -EINVAL;
1035
1036         if (flags & MREMAP_FIXED && new_addr != addr)
1037                 return (unsigned long) -EINVAL;
1038
1039         for (vml = current->mm->context.vmlist; vml; vml = vml->next)
1040                 if (vml->vma->vm_start == addr)
1041                         goto found;
1042
1043         return (unsigned long) -EINVAL;
1044
1045  found:
1046         if (vml->vma->vm_end != vml->vma->vm_start + old_len)
1047                 return (unsigned long) -EFAULT;
1048
1049         if (vml->vma->vm_flags & VM_MAYSHARE)
1050                 return (unsigned long) -EPERM;
1051
1052         if (new_len > kobjsize((void *) addr))
1053                 return (unsigned long) -ENOMEM;
1054
1055         /* all checks complete - do it */
1056         vml->vma->vm_end = vml->vma->vm_start + new_len;
1057
1058         askedalloc -= old_len;
1059         askedalloc += new_len;
1060
1061         return vml->vma->vm_start;
1062 }
1063
1064 /*
1065  * Look up the first VMA which satisfies  addr < vm_end,  NULL if none
1066  * - should be called with mm->mmap_sem at least readlocked
1067  */
1068 struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)
1069 {
1070         struct vm_list_struct *vml;
1071
1072         for (vml = mm->context.vmlist; vml; vml = vml->next)
1073                 if (addr >= vml->vma->vm_start && addr < vml->vma->vm_end)
1074                         return vml->vma;
1075
1076         return NULL;
1077 }
1078
1079 EXPORT_SYMBOL(find_vma);
1080
1081 struct page *follow_page(struct vm_area_struct *vma, unsigned long address,
1082                         unsigned int foll_flags)
1083 {
1084         return NULL;
1085 }
1086
1087 struct vm_area_struct *find_extend_vma(struct mm_struct *mm, unsigned long addr)
1088 {
1089         return NULL;
1090 }
1091
1092 int remap_pfn_range(struct vm_area_struct *vma, unsigned long from,
1093                 unsigned long to, unsigned long size, pgprot_t prot)
1094 {
1095         vma->vm_start = vma->vm_pgoff << PAGE_SHIFT;
1096         return 0;
1097 }
1098 EXPORT_SYMBOL(remap_pfn_range);
1099
1100 void swap_unplug_io_fn(struct backing_dev_info *bdi, struct page *page)
1101 {
1102 }
1103
1104 unsigned long arch_get_unmapped_area(struct file *file, unsigned long addr,
1105         unsigned long len, unsigned long pgoff, unsigned long flags)
1106 {
1107         return -ENOMEM;
1108 }
1109
1110 void arch_unmap_area(struct mm_struct *mm, unsigned long addr)
1111 {
1112 }
1113
1114 void unmap_mapping_range(struct address_space *mapping,
1115                          loff_t const holebegin, loff_t const holelen,
1116                          int even_cows)
1117 {
1118 }
1119 EXPORT_SYMBOL(unmap_mapping_range);
1120
1121 /*
1122  * Check that a process has enough memory to allocate a new virtual
1123  * mapping. 0 means there is enough memory for the allocation to
1124  * succeed and -ENOMEM implies there is not.
1125  *
1126  * We currently support three overcommit policies, which are set via the
1127  * vm.overcommit_memory sysctl.  See Documentation/vm/overcommit-accounting
1128  *
1129  * Strict overcommit modes added 2002 Feb 26 by Alan Cox.
1130  * Additional code 2002 Jul 20 by Robert Love.
1131  *
1132  * cap_sys_admin is 1 if the process has admin privileges, 0 otherwise.
1133  *
1134  * Note this is a helper function intended to be used by LSMs which
1135  * wish to use this logic.
1136  */
1137 int __vm_enough_memory(long pages, int cap_sys_admin)
1138 {
1139         unsigned long free, allowed;
1140
1141         vm_acct_memory(pages);
1142
1143         /*
1144          * Sometimes we want to use more memory than we have
1145          */
1146         if (sysctl_overcommit_memory == OVERCOMMIT_ALWAYS)
1147                 return 0;
1148
1149         if (sysctl_overcommit_memory == OVERCOMMIT_GUESS) {
1150                 unsigned long n;
1151
1152                 free = global_page_state(NR_FILE_PAGES);
1153                 free += nr_swap_pages;
1154
1155                 /*
1156                  * Any slabs which are created with the
1157                  * SLAB_RECLAIM_ACCOUNT flag claim to have contents
1158                  * which are reclaimable, under pressure.  The dentry
1159                  * cache and most inode caches should fall into this
1160                  */
1161                 free += global_page_state(NR_SLAB_RECLAIMABLE);
1162
1163                 /*
1164                  * Leave the last 3% for root
1165                  */
1166                 if (!cap_sys_admin)
1167                         free -= free / 32;
1168
1169                 if (free > pages)
1170                         return 0;
1171
1172                 /*
1173                  * nr_free_pages() is very expensive on large systems,
1174                  * only call if we're about to fail.
1175                  */
1176                 n = nr_free_pages();
1177
1178                 /*
1179                  * Leave reserved pages. The pages are not for anonymous pages.
1180                  */
1181                 if (n <= totalreserve_pages)
1182                         goto error;
1183                 else
1184                         n -= totalreserve_pages;
1185
1186                 /*
1187                  * Leave the last 3% for root
1188                  */
1189                 if (!cap_sys_admin)
1190                         n -= n / 32;
1191                 free += n;
1192
1193                 if (free > pages)
1194                         return 0;
1195
1196                 goto error;
1197         }
1198
1199         allowed = totalram_pages * sysctl_overcommit_ratio / 100;
1200         /*
1201          * Leave the last 3% for root
1202          */
1203         if (!cap_sys_admin)
1204                 allowed -= allowed / 32;
1205         allowed += total_swap_pages;
1206
1207         /* Don't let a single process grow too big:
1208            leave 3% of the size of this process for other processes */
1209         allowed -= current->mm->total_vm / 32;
1210
1211         /*
1212          * cast `allowed' as a signed long because vm_committed_space
1213          * sometimes has a negative value
1214          */
1215         if (atomic_read(&vm_committed_space) < (long)allowed)
1216                 return 0;
1217 error:
1218         vm_unacct_memory(pages);
1219
1220         return -ENOMEM;
1221 }
1222
1223 int in_gate_area_no_task(unsigned long addr)
1224 {
1225         return 0;
1226 }
1227
1228 struct page *filemap_nopage(struct vm_area_struct *area,
1229                         unsigned long address, int *type)
1230 {
1231         BUG();
1232         return NULL;
1233 }
1234
1235 /*
1236  * Access another process' address space.
1237  * - source/target buffer must be kernel space
1238  */
1239 int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write)
1240 {
1241         struct vm_area_struct *vma;
1242         struct mm_struct *mm;
1243
1244         if (addr + len < addr)
1245                 return 0;
1246
1247         mm = get_task_mm(tsk);
1248         if (!mm)
1249                 return 0;
1250
1251         down_read(&mm->mmap_sem);
1252
1253         /* the access must start within one of the target process's mappings */
1254         vma = find_vma(mm, addr);
1255         if (vma) {
1256                 /* don't overrun this mapping */
1257                 if (addr + len >= vma->vm_end)
1258                         len = vma->vm_end - addr;
1259
1260                 /* only read or write mappings where it is permitted */
1261                 if (write && vma->vm_flags & VM_MAYWRITE)
1262                         len -= copy_to_user((void *) addr, buf, len);
1263                 else if (!write && vma->vm_flags & VM_MAYREAD)
1264                         len -= copy_from_user(buf, (void *) addr, len);
1265                 else
1266                         len = 0;
1267         } else {
1268                 len = 0;
1269         }
1270
1271         up_read(&mm->mmap_sem);
1272         mmput(mm);
1273         return len;
1274 }