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