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