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