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