MOXA linux-2.6.x / linux-2.6.19-uc1 from UC-7110-LX-BOOTLOADER-1.9_VERSION-4.2.tgz
[linux-2.6.19-moxart.git] / mm / nommu.c
blob03e205b0e3fab302cff35dc3637d6cfc8f51be45
1 /*
2 * linux/mm/nommu.c
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).
7 * See Documentation/nommu-mmap.txt
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>
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>
31 #include <asm/uaccess.h>
32 #include <asm/tlb.h>
33 #include <asm/tlbflush.h>
35 //#define VICTOR_DEBUG 1
37 #ifdef VICTOR_DEBUG
38 #define victor_debug(x...) printk(x)
39 #else
40 #define victor_debug(x...)
41 #endif // VICTOR_DEBUG
43 void *high_memory;
44 struct page *mem_map;
45 unsigned long max_mapnr;
46 unsigned long num_physpages;
47 unsigned long askedalloc, realalloc;
48 atomic_t vm_committed_space = ATOMIC_INIT(0);
49 int sysctl_overcommit_memory = OVERCOMMIT_GUESS; /* heuristic overcommit */
50 int sysctl_overcommit_ratio = 50; /* default is 50% */
51 int sysctl_max_map_count = DEFAULT_MAX_MAP_COUNT;
52 int heap_stack_gap = 0;
54 EXPORT_SYMBOL(mem_map);
55 EXPORT_SYMBOL(__vm_enough_memory);
56 #if 1 // add by Victor Yu. 04-10-2007, it needs by iptables
57 EXPORT_SYMBOL(num_physpages);
58 #endif
60 /* list of shareable VMAs */
61 struct rb_root nommu_vma_tree = RB_ROOT;
62 DECLARE_RWSEM(nommu_vma_sem);
64 struct vm_operations_struct generic_file_vm_ops = {
67 EXPORT_SYMBOL(vfree);
68 EXPORT_SYMBOL(vmalloc_to_page);
69 EXPORT_SYMBOL(vmalloc_32);
70 EXPORT_SYMBOL(vmap);
71 EXPORT_SYMBOL(vunmap);
74 * Handle all mappings that got truncated by a "truncate()"
75 * system call.
77 * NOTE! We have to be ready to update the memory sharing
78 * between the file and the memory map for a potential last
79 * incomplete page. Ugly, but necessary.
81 int vmtruncate(struct inode *inode, loff_t offset)
83 struct address_space *mapping = inode->i_mapping;
84 unsigned long limit;
86 if (inode->i_size < offset)
87 goto do_expand;
88 i_size_write(inode, offset);
90 truncate_inode_pages(mapping, offset);
91 goto out_truncate;
93 do_expand:
94 limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
95 if (limit != RLIM_INFINITY && offset > limit)
96 goto out_sig;
97 if (offset > inode->i_sb->s_maxbytes)
98 goto out;
99 i_size_write(inode, offset);
101 out_truncate:
102 if (inode->i_op && inode->i_op->truncate)
103 inode->i_op->truncate(inode);
104 return 0;
105 out_sig:
106 send_sig(SIGXFSZ, current, 0);
107 out:
108 return -EFBIG;
111 EXPORT_SYMBOL(vmtruncate);
114 * Return the total memory allocated for this pointer, not
115 * just what the caller asked for.
117 * Doesn't have to be accurate, i.e. may have races.
119 unsigned int kobjsize(const void *objp)
121 struct page *page;
123 if (!objp || !((page = virt_to_page(objp))))
124 return 0;
126 if (PageSlab(page))
127 return ksize(objp);
129 BUG_ON(page->index < 0);
130 BUG_ON(page->index >= MAX_ORDER);
132 return (PAGE_SIZE << page->index);
136 * get a list of pages in an address range belonging to the specified process
137 * and indicate the VMA that covers each page
138 * - this is potentially dodgy as we may end incrementing the page count of a
139 * slab page or a secondary page from a compound page
140 * - don't permit access to VMAs that don't support it, such as I/O mappings
142 int get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
143 unsigned long start, int len, int write, int force,
144 struct page **pages, struct vm_area_struct **vmas)
146 struct vm_area_struct *vma;
147 unsigned long vm_flags;
148 int i;
150 /* calculate required read or write permissions.
151 * - if 'force' is set, we only require the "MAY" flags.
153 vm_flags = write ? (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
154 vm_flags &= force ? (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE);
156 for (i = 0; i < len; i++) {
157 vma = find_vma(mm, start);
158 if (!vma)
159 goto finish_or_fault;
161 /* protect what we can, including chardevs */
162 if (vma->vm_flags & (VM_IO | VM_PFNMAP) ||
163 !(vm_flags & vma->vm_flags))
164 goto finish_or_fault;
166 if (pages) {
167 pages[i] = virt_to_page(start);
168 if (pages[i])
169 page_cache_get(pages[i]);
171 if (vmas)
172 vmas[i] = vma;
173 start += PAGE_SIZE;
176 return i;
178 finish_or_fault:
179 return i ? : -EFAULT;
182 EXPORT_SYMBOL(get_user_pages);
184 DEFINE_RWLOCK(vmlist_lock);
185 struct vm_struct *vmlist;
187 void vfree(void *addr)
189 kfree(addr);
192 void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot)
195 * kmalloc doesn't like __GFP_HIGHMEM for some reason
197 return kmalloc(size, (gfp_mask | __GFP_COMP) & ~__GFP_HIGHMEM);
200 struct page * vmalloc_to_page(void *addr)
202 return virt_to_page(addr);
205 unsigned long vmalloc_to_pfn(void *addr)
207 return page_to_pfn(virt_to_page(addr));
211 long vread(char *buf, char *addr, unsigned long count)
213 memcpy(buf, addr, count);
214 return count;
217 long vwrite(char *buf, char *addr, unsigned long count)
219 /* Don't allow overflow */
220 if ((unsigned long) addr + count < count)
221 count = -(unsigned long) addr;
223 memcpy(addr, buf, count);
224 return(count);
228 * vmalloc - allocate virtually continguos memory
230 * @size: allocation size
232 * Allocate enough pages to cover @size from the page level
233 * allocator and map them into continguos kernel virtual space.
235 * For tight control over page level allocator and protection flags
236 * use __vmalloc() instead.
238 void *vmalloc(unsigned long size)
240 return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL);
242 EXPORT_SYMBOL(vmalloc);
244 void *vmalloc_node(unsigned long size, int node)
246 return vmalloc(size);
248 EXPORT_SYMBOL(vmalloc_node);
251 * vmalloc_32 - allocate virtually continguos memory (32bit addressable)
253 * @size: allocation size
255 * Allocate enough 32bit PA addressable pages to cover @size from the
256 * page level allocator and map them into continguos kernel virtual space.
258 void *vmalloc_32(unsigned long size)
260 return __vmalloc(size, GFP_KERNEL, PAGE_KERNEL);
263 void *vmap(struct page **pages, unsigned int count, unsigned long flags, pgprot_t prot)
265 BUG();
266 return NULL;
269 void vunmap(void *addr)
271 BUG();
275 * sys_brk() for the most part doesn't need the global kernel
276 * lock, except when an application is doing something nasty
277 * like trying to un-brk an area that has already been mapped
278 * to a regular file. in this case, the unmapping will need
279 * to invoke file system routines that need the global lock.
281 asmlinkage unsigned long sys_brk(unsigned long brk)
283 struct mm_struct *mm = current->mm;
285 if (brk < mm->start_brk || brk > mm->context.end_brk)
286 return mm->brk;
288 if (mm->brk == brk)
289 return mm->brk;
292 * Always allow shrinking brk
294 if (brk <= mm->brk) {
295 mm->brk = brk;
296 return brk;
300 * Ok, looks good - let it rip.
302 return mm->brk = brk;
305 #ifdef DEBUG
306 static void show_process_blocks(void)
308 struct vm_list_struct *vml;
310 printk("Process blocks %d:", current->pid);
312 for (vml = &current->mm->context.vmlist; vml; vml = vml->next) {
313 printk(" %p: %p", vml, vml->vma);
314 if (vml->vma)
315 printk(" (%d @%lx #%d)",
316 kobjsize((void *) vml->vma->vm_start),
317 vml->vma->vm_start,
318 atomic_read(&vml->vma->vm_usage));
319 printk(vml->next ? " ->" : ".\n");
322 #endif /* DEBUG */
325 * add a VMA into a process's mm_struct in the appropriate place in the list
326 * - should be called with mm->mmap_sem held writelocked
328 static void add_vma_to_mm(struct mm_struct *mm, struct vm_list_struct *vml)
330 struct vm_list_struct **ppv;
332 for (ppv = &current->mm->context.vmlist; *ppv; ppv = &(*ppv)->next)
333 if ((*ppv)->vma->vm_start > vml->vma->vm_start)
334 break;
336 vml->next = *ppv;
337 *ppv = vml;
341 * look up the first VMA in which addr resides, NULL if none
342 * - should be called with mm->mmap_sem at least held readlocked
344 struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)
346 struct vm_list_struct *loop, *vml;
348 /* search the vm_start ordered list */
349 vml = NULL;
350 for (loop = mm->context.vmlist; loop; loop = loop->next) {
351 if (loop->vma->vm_start > addr)
352 break;
353 vml = loop;
356 if (vml && vml->vma->vm_end > addr)
357 return vml->vma;
359 return NULL;
361 EXPORT_SYMBOL(find_vma);
364 * find a VMA
365 * - we don't extend stack VMAs under NOMMU conditions
367 struct vm_area_struct *find_extend_vma(struct mm_struct *mm, unsigned long addr)
369 return find_vma(mm, addr);
373 * look up the first VMA exactly that exactly matches addr
374 * - should be called with mm->mmap_sem at least held readlocked
376 static inline struct vm_area_struct *find_vma_exact(struct mm_struct *mm,
377 unsigned long addr)
379 struct vm_list_struct *vml;
381 /* search the vm_start ordered list */
382 for (vml = mm->context.vmlist; vml; vml = vml->next) {
383 if (vml->vma->vm_start == addr)
384 return vml->vma;
385 if (vml->vma->vm_start > addr)
386 break;
389 return NULL;
393 * find a VMA in the global tree
395 static inline struct vm_area_struct *find_nommu_vma(unsigned long start)
397 struct vm_area_struct *vma;
398 struct rb_node *n = nommu_vma_tree.rb_node;
400 while (n) {
401 vma = rb_entry(n, struct vm_area_struct, vm_rb);
403 if (start < vma->vm_start)
404 n = n->rb_left;
405 else if (start > vma->vm_start)
406 n = n->rb_right;
407 else
408 return vma;
411 return NULL;
415 * add a VMA in the global tree
417 static void add_nommu_vma(struct vm_area_struct *vma)
419 struct vm_area_struct *pvma;
420 struct address_space *mapping;
421 struct rb_node **p = &nommu_vma_tree.rb_node;
422 struct rb_node *parent = NULL;
424 /* add the VMA to the mapping */
425 if (vma->vm_file) {
426 mapping = vma->vm_file->f_mapping;
428 flush_dcache_mmap_lock(mapping);
429 vma_prio_tree_insert(vma, &mapping->i_mmap);
430 flush_dcache_mmap_unlock(mapping);
433 /* add the VMA to the master list */
434 while (*p) {
435 parent = *p;
436 pvma = rb_entry(parent, struct vm_area_struct, vm_rb);
438 if (vma->vm_start < pvma->vm_start) {
439 p = &(*p)->rb_left;
441 else if (vma->vm_start > pvma->vm_start) {
442 p = &(*p)->rb_right;
444 else {
445 /* mappings are at the same address - this can only
446 * happen for shared-mem chardevs and shared file
447 * mappings backed by ramfs/tmpfs */
448 BUG_ON(!(pvma->vm_flags & VM_SHARED));
450 if (vma < pvma)
451 p = &(*p)->rb_left;
452 else if (vma > pvma)
453 p = &(*p)->rb_right;
454 else
455 BUG();
459 rb_link_node(&vma->vm_rb, parent, p);
460 rb_insert_color(&vma->vm_rb, &nommu_vma_tree);
464 * delete a VMA from the global list
466 static void delete_nommu_vma(struct vm_area_struct *vma)
468 struct address_space *mapping;
470 /* remove the VMA from the mapping */
471 if (vma->vm_file) {
472 mapping = vma->vm_file->f_mapping;
474 flush_dcache_mmap_lock(mapping);
475 vma_prio_tree_remove(vma, &mapping->i_mmap);
476 flush_dcache_mmap_unlock(mapping);
479 /* remove from the master list */
480 rb_erase(&vma->vm_rb, &nommu_vma_tree);
484 * determine whether a mapping should be permitted and, if so, what sort of
485 * mapping we're capable of supporting
487 static int validate_mmap_request(struct file *file,
488 unsigned long addr,
489 unsigned long len,
490 unsigned long prot,
491 unsigned long flags,
492 unsigned long pgoff,
493 unsigned long *_capabilities)
495 unsigned long capabilities;
496 unsigned long reqprot = prot;
497 int ret;
499 /* do the simple checks first */
500 if (flags & MAP_FIXED || addr) {
501 printk(KERN_DEBUG
502 "%d: Can't do fixed-address/overlay mmap of RAM\n",
503 current->pid);
504 return -EINVAL;
507 #if 0 // add by Victor Yu. 01-07-2008
508 if ( (flags & MAP_TYPE) == MAP_SHARED ) {
509 flags &= ~MAP_TYPE;
510 flags |= MAP_PRIVATE;
512 #endif
514 if ((flags & MAP_TYPE) != MAP_PRIVATE &&
515 (flags & MAP_TYPE) != MAP_SHARED) {
516 return -EINVAL;
519 if (!len) {
520 return -EINVAL;
523 /* Careful about overflows.. */
524 len = PAGE_ALIGN(len);
525 if (!len || len > TASK_SIZE) {
526 return -ENOMEM;
529 /* offset overflow? */
530 if ((pgoff + (len >> PAGE_SHIFT)) < pgoff) {
531 return -EOVERFLOW;
534 if (file) {
535 /* validate file mapping requests */
536 struct address_space *mapping;
538 /* files must support mmap */
539 if (!file->f_op || !file->f_op->mmap) {
540 return -ENODEV;
543 #if 0 // add by Victor Yu. 02-15-2007, I changed the insmod application.
544 flags |= MAP_PRIVATE;
545 flags &= !MAP_SHARED;
546 #endif
547 /* work out if what we've got could possibly be shared
548 * - we support chardevs that provide their own "memory"
549 * - we support files/blockdevs that are memory backed
551 mapping = file->f_mapping;
552 if (!mapping)
553 mapping = file->f_dentry->d_inode->i_mapping;
555 capabilities = 0;
556 if (mapping && mapping->backing_dev_info)
557 capabilities = mapping->backing_dev_info->capabilities;
559 if (!capabilities) {
560 /* no explicit capabilities set, so assume some
561 * defaults */
562 switch (file->f_dentry->d_inode->i_mode & S_IFMT) {
563 case S_IFREG:
564 case S_IFBLK:
565 capabilities = BDI_CAP_MAP_COPY;
566 break;
568 case S_IFCHR:
569 capabilities =
570 BDI_CAP_MAP_DIRECT |
571 BDI_CAP_READ_MAP |
572 BDI_CAP_WRITE_MAP;
573 break;
575 default:
576 return -EINVAL;
580 /* eliminate any capabilities that we can't support on this
581 * device */
582 if (!file->f_op->get_unmapped_area)
583 capabilities &= ~BDI_CAP_MAP_DIRECT;
584 if (!file->f_op->read)
585 capabilities &= ~BDI_CAP_MAP_COPY;
587 if (flags & MAP_SHARED) {
588 /* do checks for writing, appending and locking */
589 if ((prot & PROT_WRITE) &&
590 !(file->f_mode & FMODE_WRITE)) {
591 return -EACCES;
594 if (IS_APPEND(file->f_dentry->d_inode) &&
595 (file->f_mode & FMODE_WRITE)) {
596 return -EACCES;
599 if (locks_verify_locked(file->f_dentry->d_inode)) {
600 return -EAGAIN;
603 if (!(capabilities & BDI_CAP_MAP_DIRECT)) {
604 return -ENODEV;
607 if (((prot & PROT_READ) && !(capabilities & BDI_CAP_READ_MAP)) ||
608 ((prot & PROT_WRITE) && !(capabilities & BDI_CAP_WRITE_MAP)) ||
609 ((prot & PROT_EXEC) && !(capabilities & BDI_CAP_EXEC_MAP))
611 printk("MAP_SHARED not completely supported on !MMU\n");
612 return -EINVAL;
615 /* we mustn't privatise shared mappings */
616 capabilities &= ~BDI_CAP_MAP_COPY;
618 else {
619 /* we're going to read the file into private memory we
620 * allocate */
621 if (!(capabilities & BDI_CAP_MAP_COPY)) {
622 return -ENODEV;
625 /* we don't permit a private writable mapping to be
626 * shared with the backing device */
627 if (prot & PROT_WRITE)
628 capabilities &= ~BDI_CAP_MAP_DIRECT;
631 /* handle executable mappings and implied executable
632 * mappings */
633 if (file->f_vfsmnt->mnt_flags & MNT_NOEXEC) {
634 if (prot & PROT_EXEC) {
635 return -EPERM;
638 else if ((prot & PROT_READ) && !(prot & PROT_EXEC)) {
639 /* handle implication of PROT_EXEC by PROT_READ */
640 if (current->personality & READ_IMPLIES_EXEC) {
641 if (capabilities & BDI_CAP_EXEC_MAP)
642 prot |= PROT_EXEC;
645 else if ((prot & PROT_READ) &&
646 (prot & PROT_EXEC) &&
647 !(capabilities & BDI_CAP_EXEC_MAP)
649 /* backing file is not executable, try to copy */
650 capabilities &= ~BDI_CAP_MAP_DIRECT;
653 else {
654 /* anonymous mappings are always memory backed and can be
655 * privately mapped
657 capabilities = BDI_CAP_MAP_COPY;
659 /* handle PROT_EXEC implication by PROT_READ */
660 if ((prot & PROT_READ) &&
661 (current->personality & READ_IMPLIES_EXEC))
662 prot |= PROT_EXEC;
665 /* allow the security API to have its say */
666 ret = security_file_mmap(file, reqprot, prot, flags);
667 if (ret < 0) {
668 return ret;
671 /* looks okay */
672 *_capabilities = capabilities;
673 return 0;
677 * we've determined that we can make the mapping, now translate what we
678 * now know into VMA flags
680 static unsigned long determine_vm_flags(struct file *file,
681 unsigned long prot,
682 unsigned long flags,
683 unsigned long capabilities)
685 unsigned long vm_flags;
687 vm_flags = calc_vm_prot_bits(prot) | calc_vm_flag_bits(flags);
688 vm_flags |= VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
689 /* vm_flags |= mm->def_flags; */
691 #if 0 // add by Victor Yu. 01-07-2008
692 if ( (flags & MAP_TYPE) == MAP_SHARED ) {
693 flags &= ~MAP_TYPE;
694 flags |= MAP_PRIVATE;
696 #endif
698 if (!(capabilities & BDI_CAP_MAP_DIRECT)) {
699 /* attempt to share read-only copies of mapped file chunks */
700 if (file && !(prot & PROT_WRITE))
701 vm_flags |= VM_MAYSHARE;
703 else {
704 /* overlay a shareable mapping on the backing device or inode
705 * if possible - used for chardevs, ramfs/tmpfs/shmfs and
706 * romfs/cramfs */
707 if (flags & MAP_SHARED)
708 vm_flags |= VM_MAYSHARE | VM_SHARED;
709 else if ((((vm_flags & capabilities) ^ vm_flags) & BDI_CAP_VMFLAGS) == 0)
710 vm_flags |= VM_MAYSHARE;
713 /* refuse to let anyone share private mappings with this process if
714 * it's being traced - otherwise breakpoints set in it may interfere
715 * with another untraced process
717 if ((flags & MAP_PRIVATE) && (current->ptrace & PT_PTRACED))
718 vm_flags &= ~VM_MAYSHARE;
720 return vm_flags;
724 * set up a shared mapping on a file
726 static int do_mmap_shared_file(struct vm_area_struct *vma, unsigned long len)
728 int ret;
730 ret = vma->vm_file->f_op->mmap(vma->vm_file, vma);
731 if (ret != -ENOSYS)
732 return ret;
734 /* getting an ENOSYS error indicates that direct mmap isn't
735 * possible (as opposed to tried but failed) so we'll fall
736 * through to making a private copy of the data and mapping
737 * that if we can */
738 return -ENODEV;
742 * set up a private mapping or an anonymous shared mapping
744 static int do_mmap_private(struct vm_area_struct *vma, unsigned long len)
746 void *base;
747 int ret;
749 /* invoke the file's mapping function so that it can keep track of
750 * shared mappings on devices or memory
751 * - VM_MAYSHARE will be set if it may attempt to share
753 if (vma->vm_file) {
754 ret = vma->vm_file->f_op->mmap(vma->vm_file, vma);
755 if (ret != -ENOSYS) {
756 /* shouldn't return success if we're not sharing */
757 BUG_ON(ret == 0 && !(vma->vm_flags & VM_MAYSHARE));
758 return ret; /* success or a real error */
761 /* getting an ENOSYS error indicates that direct mmap isn't
762 * possible (as opposed to tried but failed) so we'll try to
763 * make a private copy of the data and map that instead */
766 /* allocate some memory to hold the mapping
767 * - note that this may not return a page-aligned address if the object
768 * we're allocating is smaller than a page
770 base = kmalloc(len, GFP_KERNEL|__GFP_COMP);
771 if (!base) {
772 goto enomem;
775 vma->vm_start = (unsigned long) base;
776 victor_debug("%s allocation memory = 0x%x!\n", __FUNCTION__, vma->vm_start);
777 vma->vm_end = vma->vm_start + len;
778 vma->vm_flags |= VM_MAPPED_COPY;
780 #ifdef WARN_ON_SLACK
781 if (len + WARN_ON_SLACK <= kobjsize(result))
782 printk("Allocation of %lu bytes from process %d has %lu bytes of slack\n",
783 len, current->pid, kobjsize(result) - len);
784 #endif
786 if (vma->vm_file) {
787 /* read the contents of a file into the copy */
788 mm_segment_t old_fs;
789 loff_t fpos;
791 fpos = vma->vm_pgoff;
792 fpos <<= PAGE_SHIFT;
794 old_fs = get_fs();
795 set_fs(KERNEL_DS);
796 ret = vma->vm_file->f_op->read(vma->vm_file, base, len, &fpos);
797 set_fs(old_fs);
799 if (ret < 0) {
800 goto error_free;
803 /* clear the last little bit */
804 if (ret < len)
805 memset(base + ret, 0, len - ret);
807 } else {
808 /* if it's an anonymous mapping, then just clear it */
809 memset(base, 0, len);
812 return 0;
814 error_free:
815 kfree(base);
816 vma->vm_start = 0;
817 return ret;
819 enomem:
820 #if 0 // mask by Victor Yu. 03-14-2007
821 printk("Allocation of length %lu from process %d failed\n",
822 len, current->pid);
823 show_free_areas();
824 #endif
825 return -ENOMEM;
829 * handle mapping creation for uClinux
831 unsigned long do_mmap_pgoff(struct file *file,
832 unsigned long addr,
833 unsigned long len,
834 unsigned long prot,
835 unsigned long flags,
836 unsigned long pgoff)
838 struct vm_list_struct *vml = NULL;
839 struct vm_area_struct *vma = NULL;
840 struct rb_node *rb;
841 unsigned long capabilities, vm_flags;
842 void *result;
843 int ret;
845 #if 0 // add by Victor Yu. 01-07-2008
846 if ( (flags & MAP_TYPE) == MAP_SHARED ) {
847 flags &= ~MAP_TYPE;
848 flags |= MAP_PRIVATE;
850 #endif
851 /* decide whether we should attempt the mapping, and if so what sort of
852 * mapping */
853 ret = validate_mmap_request(file, addr, len, prot, flags, pgoff,
854 &capabilities);
855 if (ret < 0) {
856 return ret;
859 /* we've determined that we can make the mapping, now translate what we
860 * now know into VMA flags */
861 vm_flags = determine_vm_flags(file, prot, flags, capabilities);
863 /* we're going to need to record the mapping if it works */
864 vml = kmalloc(sizeof(struct vm_list_struct), GFP_KERNEL);
865 if (!vml)
866 goto error_getting_vml;
867 memset(vml, 0, sizeof(*vml));
869 down_write(&nommu_vma_sem);
871 /* if we want to share, we need to check for VMAs created by other
872 * mmap() calls that overlap with our proposed mapping
873 * - we can only share with an exact match on most regular files
874 * - shared mappings on character devices and memory backed files are
875 * permitted to overlap inexactly as far as we are concerned for in
876 * these cases, sharing is handled in the driver or filesystem rather
877 * than here
879 if (vm_flags & VM_MAYSHARE) {
880 unsigned long pglen = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
881 unsigned long vmpglen;
883 for (rb = rb_first(&nommu_vma_tree); rb; rb = rb_next(rb)) {
884 vma = rb_entry(rb, struct vm_area_struct, vm_rb);
886 if (!(vma->vm_flags & VM_MAYSHARE))
887 continue;
889 /* search for overlapping mappings on the same file */
890 if (vma->vm_file->f_dentry->d_inode != file->f_dentry->d_inode)
891 continue;
893 if (vma->vm_pgoff >= pgoff + pglen)
894 continue;
896 vmpglen = vma->vm_end - vma->vm_start + PAGE_SIZE - 1;
897 vmpglen >>= PAGE_SHIFT;
898 if (pgoff >= vma->vm_pgoff + vmpglen)
899 continue;
901 /* handle inexactly overlapping matches between mappings */
902 if (vma->vm_pgoff != pgoff || vmpglen != pglen) {
903 if (!(capabilities & BDI_CAP_MAP_DIRECT))
904 goto sharing_violation;
905 continue;
908 /* we've found a VMA we can share */
909 atomic_inc(&vma->vm_usage);
911 vml->vma = vma;
912 result = (void *) vma->vm_start;
913 goto shared;
916 vma = NULL;
918 /* obtain the address at which to make a shared mapping
919 * - this is the hook for quasi-memory character devices to
920 * tell us the location of a shared mapping
922 if (file && file->f_op->get_unmapped_area) {
923 addr = file->f_op->get_unmapped_area(file, addr, len,
924 pgoff, flags);
925 if (IS_ERR((void *) addr)) {
926 ret = addr;
927 if (ret != (unsigned long) -ENOSYS)
928 goto error;
930 /* the driver refused to tell us where to site
931 * the mapping so we'll have to attempt to copy
932 * it */
933 ret = (unsigned long) -ENODEV;
934 if (!(capabilities & BDI_CAP_MAP_COPY))
935 goto error;
937 capabilities &= ~BDI_CAP_MAP_DIRECT;
942 /* we're going to need a VMA struct as well */
943 vma = kmalloc(sizeof(struct vm_area_struct), GFP_KERNEL);
944 if (!vma)
945 goto error_getting_vma;
947 memset(vma, 0, sizeof(*vma));
948 INIT_LIST_HEAD(&vma->anon_vma_node);
949 atomic_set(&vma->vm_usage, 1);
950 if (file)
951 get_file(file);
952 vma->vm_file = file;
953 vma->vm_flags = vm_flags;
954 vma->vm_start = addr;
955 vma->vm_end = addr + len;
956 vma->vm_pgoff = pgoff;
958 vml->vma = vma;
960 /* set up the mapping */
961 if (file && (vma->vm_flags & VM_SHARED) )
962 ret = do_mmap_shared_file(vma, len);
963 else
964 ret = do_mmap_private(vma, len);
965 if (ret < 0)
966 goto error;
968 /* okay... we have a mapping; now we have to register it */
969 result = (void *) vma->vm_start;
971 if (vma->vm_flags & VM_MAPPED_COPY) {
972 realalloc += kobjsize(result);
973 askedalloc += len;
976 realalloc += kobjsize(vma);
977 askedalloc += sizeof(*vma);
979 current->mm->total_vm += len >> PAGE_SHIFT;
981 add_nommu_vma(vma);
983 shared:
984 realalloc += kobjsize(vml);
985 askedalloc += sizeof(*vml);
987 add_vma_to_mm(current->mm, vml);
989 up_write(&nommu_vma_sem);
991 if (prot & PROT_EXEC)
992 flush_icache_range((unsigned long) result,
993 (unsigned long) result + len);
995 #ifdef DEBUG
996 printk("do_mmap:\n");
997 show_process_blocks();
998 #endif
1000 return (unsigned long) result;
1002 error:
1003 up_write(&nommu_vma_sem);
1004 kfree(vml);
1005 if (vma) {
1006 if (vma->vm_file)
1007 fput(vma->vm_file);
1008 kfree(vma);
1010 return ret;
1012 sharing_violation:
1013 up_write(&nommu_vma_sem);
1014 printk("Attempt to share mismatched mappings\n");
1015 kfree(vml);
1016 return -EINVAL;
1018 error_getting_vma:
1019 up_write(&nommu_vma_sem);
1020 kfree(vml);
1021 printk("Allocation of vma for %lu byte allocation from process %d failed\n",
1022 len, current->pid);
1023 show_free_areas();
1024 return -ENOMEM;
1026 error_getting_vml:
1027 printk("Allocation of vml for %lu byte allocation from process %d failed\n",
1028 len, current->pid);
1029 show_free_areas();
1030 return -ENOMEM;
1034 * handle mapping disposal for uClinux
1036 static void put_vma(struct vm_area_struct *vma)
1038 if (vma) {
1039 down_write(&nommu_vma_sem);
1041 if (atomic_dec_and_test(&vma->vm_usage)) {
1042 delete_nommu_vma(vma);
1044 if (vma->vm_ops && vma->vm_ops->close)
1045 vma->vm_ops->close(vma);
1047 /* IO memory and memory shared directly out of the pagecache from
1048 * ramfs/tmpfs mustn't be released here */
1049 if (vma->vm_flags & VM_MAPPED_COPY) {
1050 realalloc -= kobjsize((void *) vma->vm_start);
1051 askedalloc -= vma->vm_end - vma->vm_start;
1052 kfree((void *) vma->vm_start);
1053 victor_debug("%s free memory = 0x%x!\n", __FUNCTION__, vma->vm_start);
1056 realalloc -= kobjsize(vma);
1057 askedalloc -= sizeof(*vma);
1059 if (vma->vm_file)
1060 fput(vma->vm_file);
1061 kfree(vma);
1064 up_write(&nommu_vma_sem);
1069 * release a mapping
1070 * - under NOMMU conditions the parameters must match exactly to the mapping to
1071 * be removed
1073 int do_munmap(struct mm_struct *mm, unsigned long addr, size_t len)
1075 struct vm_list_struct *vml, **parent;
1076 unsigned long end = addr + len;
1078 #ifdef DEBUG
1079 printk("do_munmap:\n");
1080 #endif
1082 for (parent = &mm->context.vmlist; *parent; parent = &(*parent)->next) {
1083 #if 1 // mask by Victor Yu. 03-03-3007
1084 if ((*parent)->vma->vm_start > addr) {
1085 break;
1087 #endif
1088 if ((*parent)->vma->vm_start == addr &&
1089 ((len == 0) || ((*parent)->vma->vm_end == end)))
1090 goto found;
1093 #if 1 // mask by Victor Yu. 03-03-2007
1094 printk("munmap of non-mmaped memory by process %d (%s): %p\n",
1095 current->pid, current->comm, (void *) addr);
1096 return -EINVAL;
1097 #else
1098 return 0;
1099 #endif
1101 found:
1102 vml = *parent;
1104 put_vma(vml->vma);
1106 *parent = vml->next;
1107 realalloc -= kobjsize(vml);
1108 askedalloc -= sizeof(*vml);
1109 kfree(vml);
1111 update_hiwater_vm(mm);
1112 mm->total_vm -= len >> PAGE_SHIFT;
1114 #ifdef DEBUG
1115 show_process_blocks();
1116 #endif
1118 return 0;
1121 asmlinkage long sys_munmap(unsigned long addr, size_t len)
1123 int ret;
1124 struct mm_struct *mm = current->mm;
1126 down_write(&mm->mmap_sem);
1127 ret = do_munmap(mm, addr, len);
1128 up_write(&mm->mmap_sem);
1129 return ret;
1133 * Release all mappings
1135 void exit_mmap(struct mm_struct * mm)
1137 struct vm_list_struct *tmp;
1139 if (mm) {
1140 #ifdef DEBUG
1141 printk("Exit_mmap:\n");
1142 #endif
1144 mm->total_vm = 0;
1146 while ((tmp = mm->context.vmlist)) {
1147 mm->context.vmlist = tmp->next;
1148 put_vma(tmp->vma);
1150 realalloc -= kobjsize(tmp);
1151 askedalloc -= sizeof(*tmp);
1152 kfree(tmp);
1155 #ifdef DEBUG
1156 show_process_blocks();
1157 #endif
1161 unsigned long do_brk(unsigned long addr, unsigned long len)
1163 return -ENOMEM;
1167 * expand (or shrink) an existing mapping, potentially moving it at the same
1168 * time (controlled by the MREMAP_MAYMOVE flag and available VM space)
1170 * under NOMMU conditions, we only permit changing a mapping's size, and only
1171 * as long as it stays within the hole allocated by the kmalloc() call in
1172 * do_mmap_pgoff() and the block is not shareable
1174 * MREMAP_FIXED is not supported under NOMMU conditions
1176 unsigned long do_mremap(unsigned long addr,
1177 unsigned long old_len, unsigned long new_len,
1178 unsigned long flags, unsigned long new_addr)
1180 struct vm_area_struct *vma;
1182 /* insanity checks first */
1183 if (new_len == 0)
1184 return (unsigned long) -EINVAL;
1186 if (flags & MREMAP_FIXED && new_addr != addr)
1187 return (unsigned long) -EINVAL;
1189 vma = find_vma_exact(current->mm, addr);
1190 if (!vma)
1191 return (unsigned long) -EINVAL;
1193 if (vma->vm_end != vma->vm_start + old_len)
1194 return (unsigned long) -EFAULT;
1196 if (vma->vm_flags & VM_MAYSHARE)
1197 return (unsigned long) -EPERM;
1199 if (new_len > kobjsize((void *) addr))
1200 return (unsigned long) -ENOMEM;
1202 /* all checks complete - do it */
1203 vma->vm_end = vma->vm_start + new_len;
1205 askedalloc -= old_len;
1206 askedalloc += new_len;
1208 return vma->vm_start;
1211 asmlinkage unsigned long sys_mremap(unsigned long addr,
1212 unsigned long old_len, unsigned long new_len,
1213 unsigned long flags, unsigned long new_addr)
1215 unsigned long ret;
1217 down_write(&current->mm->mmap_sem);
1218 ret = do_mremap(addr, old_len, new_len, flags, new_addr);
1219 up_write(&current->mm->mmap_sem);
1220 return ret;
1223 struct page *follow_page(struct vm_area_struct *vma, unsigned long address,
1224 unsigned int foll_flags)
1226 return NULL;
1229 int remap_pfn_range(struct vm_area_struct *vma, unsigned long from,
1230 unsigned long to, unsigned long size, pgprot_t prot)
1232 vma->vm_start = vma->vm_pgoff << PAGE_SHIFT;
1233 return 0;
1235 EXPORT_SYMBOL(remap_pfn_range);
1237 void swap_unplug_io_fn(struct backing_dev_info *bdi, struct page *page)
1241 unsigned long arch_get_unmapped_area(struct file *file, unsigned long addr,
1242 unsigned long len, unsigned long pgoff, unsigned long flags)
1244 return -ENOMEM;
1247 void arch_unmap_area(struct mm_struct *mm, unsigned long addr)
1251 void unmap_mapping_range(struct address_space *mapping,
1252 loff_t const holebegin, loff_t const holelen,
1253 int even_cows)
1256 EXPORT_SYMBOL(unmap_mapping_range);
1259 * Check that a process has enough memory to allocate a new virtual
1260 * mapping. 0 means there is enough memory for the allocation to
1261 * succeed and -ENOMEM implies there is not.
1263 * We currently support three overcommit policies, which are set via the
1264 * vm.overcommit_memory sysctl. See Documentation/vm/overcommit-accounting
1266 * Strict overcommit modes added 2002 Feb 26 by Alan Cox.
1267 * Additional code 2002 Jul 20 by Robert Love.
1269 * cap_sys_admin is 1 if the process has admin privileges, 0 otherwise.
1271 * Note this is a helper function intended to be used by LSMs which
1272 * wish to use this logic.
1274 int __vm_enough_memory(long pages, int cap_sys_admin)
1276 unsigned long free, allowed;
1278 vm_acct_memory(pages);
1281 * Sometimes we want to use more memory than we have
1283 if (sysctl_overcommit_memory == OVERCOMMIT_ALWAYS)
1284 return 0;
1286 if (sysctl_overcommit_memory == OVERCOMMIT_GUESS) {
1287 unsigned long n;
1289 free = global_page_state(NR_FILE_PAGES);
1290 free += nr_swap_pages;
1293 * Any slabs which are created with the
1294 * SLAB_RECLAIM_ACCOUNT flag claim to have contents
1295 * which are reclaimable, under pressure. The dentry
1296 * cache and most inode caches should fall into this
1298 free += global_page_state(NR_SLAB_RECLAIMABLE);
1301 * Leave the last 3% for root
1303 if (!cap_sys_admin)
1304 free -= free / 32;
1306 if (free > pages)
1307 return 0;
1310 * nr_free_pages() is very expensive on large systems,
1311 * only call if we're about to fail.
1313 n = nr_free_pages();
1316 * Leave reserved pages. The pages are not for anonymous pages.
1318 if (n <= totalreserve_pages)
1319 goto error;
1320 else
1321 n -= totalreserve_pages;
1324 * Leave the last 3% for root
1326 if (!cap_sys_admin)
1327 n -= n / 32;
1328 free += n;
1330 if (free > pages)
1331 return 0;
1333 goto error;
1336 allowed = totalram_pages * sysctl_overcommit_ratio / 100;
1338 * Leave the last 3% for root
1340 if (!cap_sys_admin)
1341 allowed -= allowed / 32;
1342 allowed += total_swap_pages;
1344 /* Don't let a single process grow too big:
1345 leave 3% of the size of this process for other processes */
1346 allowed -= current->mm->total_vm / 32;
1349 * cast `allowed' as a signed long because vm_committed_space
1350 * sometimes has a negative value
1352 if (atomic_read(&vm_committed_space) < (long)allowed)
1353 return 0;
1354 error:
1355 vm_unacct_memory(pages);
1357 return -ENOMEM;
1360 int in_gate_area_no_task(unsigned long addr)
1362 return 0;
1365 struct page *filemap_nopage(struct vm_area_struct *area,
1366 unsigned long address, int *type)
1368 BUG();
1369 return NULL;
1373 * Access another process' address space.
1374 * - source/target buffer must be kernel space
1376 int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write)
1378 struct vm_area_struct *vma;
1379 struct mm_struct *mm;
1381 if (addr + len < addr)
1382 return 0;
1384 mm = get_task_mm(tsk);
1385 if (!mm)
1386 return 0;
1388 down_read(&mm->mmap_sem);
1390 /* the access must start within one of the target process's mappings */
1391 vma = find_vma(mm, addr);
1392 if (vma) {
1393 /* don't overrun this mapping */
1394 if (addr + len >= vma->vm_end)
1395 len = vma->vm_end - addr;
1397 /* only read or write mappings where it is permitted */
1398 if (write && vma->vm_flags & VM_MAYWRITE)
1399 len -= copy_to_user((void *) addr, buf, len);
1400 else if (!write && vma->vm_flags & VM_MAYREAD)
1401 len -= copy_from_user(buf, (void *) addr, len);
1402 else
1403 len = 0;
1404 } else {
1405 len = 0;
1408 up_read(&mm->mmap_sem);
1409 mmput(mm);
1410 return len;