4 * Replacement code for mm functions to support CPU's that don't
5 * have any form of memory management unit (thus no virtual memory).
7 * Copyright (c) 2000-2003 David McCullough <davidm@snapgear.com>
8 * Copyright (c) 2000-2001 D Jeff Dionne <jeff@uClinux.org>
9 * Copyright (c) 2002 Greg Ungerer <gerg@snapgear.com>
13 #include <linux/mman.h>
14 #include <linux/swap.h>
15 #include <linux/smp_lock.h>
16 #include <linux/highmem.h>
17 #include <linux/pagemap.h>
18 #include <linux/slab.h>
19 #include <linux/vmalloc.h>
20 #include <linux/blkdev.h>
21 #include <linux/backing-dev.h>
23 #include <asm/uaccess.h>
25 #include <asm/tlbflush.h>
29 unsigned long max_mapnr;
30 unsigned long num_physpages;
31 unsigned long askedalloc, realalloc;
32 atomic_t vm_committed_space = ATOMIC_INIT(0);
33 int sysctl_overcommit_memory = OVERCOMMIT_GUESS; /* heuristic overcommit */
34 int sysctl_overcommit_ratio = 50; /* default is 50% */
36 int sysctl_max_map_count = DEFAULT_MAX_MAP_COUNT;
37 EXPORT_SYMBOL(sysctl_max_map_count);
40 * Handle all mappings that got truncated by a "truncate()"
43 * NOTE! We have to be ready to update the memory sharing
44 * between the file and the memory map for a potential last
45 * incomplete page. Ugly, but necessary.
47 int vmtruncate(struct inode *inode, loff_t offset)
49 struct address_space *mapping = inode->i_mapping;
52 if (inode->i_size < offset)
54 i_size_write(inode, offset);
56 truncate_inode_pages(mapping, offset);
60 limit = current->rlim[RLIMIT_FSIZE].rlim_cur;
61 if (limit != RLIM_INFINITY && offset > limit)
63 if (offset > inode->i_sb->s_maxbytes)
65 i_size_write(inode, offset);
68 if (inode->i_op && inode->i_op->truncate)
69 inode->i_op->truncate(inode);
72 send_sig(SIGXFSZ, current, 0);
78 * Return the total memory allocated for this pointer, not
79 * just what the caller asked for.
81 * Doesn't have to be accurate, i.e. may have races.
83 unsigned int kobjsize(const void *objp)
87 if (!objp || !((page = virt_to_page(objp))))
93 BUG_ON(page->index < 0);
94 BUG_ON(page->index >= MAX_ORDER);
96 return (PAGE_SIZE << page->index);
100 * The nommu dodgy version :-)
102 int get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
103 unsigned long start, int len, int write, int force,
104 struct page **pages, struct vm_area_struct **vmas)
107 static struct vm_area_struct dummy_vma;
109 for (i = 0; i < len; i++) {
111 pages[i] = virt_to_page(start);
113 page_cache_get(pages[i]);
116 vmas[i] = &dummy_vma;
122 rwlock_t vmlist_lock = RW_LOCK_UNLOCKED;
123 struct vm_struct *vmlist;
125 void vfree(void *addr)
130 void *__vmalloc(unsigned long size, int gfp_mask, pgprot_t prot)
133 * kmalloc doesn't like __GFP_HIGHMEM for some reason
135 return kmalloc(size, gfp_mask & ~__GFP_HIGHMEM);
138 struct page * vmalloc_to_page(void *addr)
140 return virt_to_page(addr);
143 long vread(char *buf, char *addr, unsigned long count)
145 memcpy(buf, addr, count);
149 long vwrite(char *buf, char *addr, unsigned long count)
151 /* Don't allow overflow */
152 if ((unsigned long) addr + count < count)
153 count = -(unsigned long) addr;
155 memcpy(addr, buf, count);
160 * vmalloc - allocate virtually continguos memory
162 * @size: allocation size
164 * Allocate enough pages to cover @size from the page level
165 * allocator and map them into continguos kernel virtual space.
167 * For tight cotrol over page level allocator and protection flags
168 * use __vmalloc() instead.
170 void *vmalloc(unsigned long size)
172 return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL);
176 * vmalloc_32 - allocate virtually continguos memory (32bit addressable)
178 * @size: allocation size
180 * Allocate enough 32bit PA addressable pages to cover @size from the
181 * page level allocator and map them into continguos kernel virtual space.
183 void *vmalloc_32(unsigned long size)
185 return __vmalloc(size, GFP_KERNEL, PAGE_KERNEL);
188 void *vmap(struct page **pages, unsigned int count, unsigned long flags, pgprot_t prot)
194 void vunmap(void *addr)
200 * sys_brk() for the most part doesn't need the global kernel
201 * lock, except when an application is doing something nasty
202 * like trying to un-brk an area that has already been mapped
203 * to a regular file. in this case, the unmapping will need
204 * to invoke file system routines that need the global lock.
206 asmlinkage unsigned long sys_brk(unsigned long brk)
208 struct mm_struct *mm = current->mm;
210 if (brk < mm->end_code || brk < mm->start_brk || brk > mm->context.end_brk)
217 * Always allow shrinking brk
219 if (brk <= mm->brk) {
225 * Ok, looks good - let it rip.
227 return mm->brk = brk;
231 * Combine the mmap "prot" and "flags" argument into one "vm_flags" used
232 * internally. Essentially, translate the "PROT_xxx" and "MAP_xxx" bits
235 static inline unsigned long calc_vm_flags(unsigned long prot, unsigned long flags)
237 #define _trans(x,bit1,bit2) \
238 ((bit1==bit2)?(x&bit1):(x&bit1)?bit2:0)
240 unsigned long prot_bits, flag_bits;
242 _trans(prot, PROT_READ, VM_READ) |
243 _trans(prot, PROT_WRITE, VM_WRITE) |
244 _trans(prot, PROT_EXEC, VM_EXEC);
246 _trans(flags, MAP_GROWSDOWN, VM_GROWSDOWN) |
247 _trans(flags, MAP_DENYWRITE, VM_DENYWRITE) |
248 _trans(flags, MAP_EXECUTABLE, VM_EXECUTABLE);
249 return prot_bits | flag_bits;
254 static void show_process_blocks(void)
256 struct mm_tblock_struct *tblock;
258 printk("Process blocks %d:", current->pid);
260 for (tblock = ¤t->mm->context.tblock; tblock; tblock = tblock->next) {
261 printk(" %p: %p", tblock, tblock->rblock);
263 printk(" (%d @%p #%d)", kobjsize(tblock->rblock->kblock), tblock->rblock->kblock, tblock->rblock->refcount);
264 printk(tblock->next ? " ->" : ".\n");
269 unsigned long do_mmap_pgoff(
278 struct mm_tblock_struct * tblock;
279 unsigned int vm_flags;
282 * Get the !CONFIG_MMU specific checks done first
284 if ((flags & MAP_SHARED) && (prot & PROT_WRITE) && (file)) {
285 printk("MAP_SHARED not supported (cannot write mappings to disk)\n");
289 if ((prot & PROT_WRITE) && (flags & MAP_PRIVATE)) {
290 printk("Private writable mappings not supported\n");
295 * now all the standard checks
297 if (file && (!file->f_op || !file->f_op->mmap))
300 if (PAGE_ALIGN(len) == 0)
306 /* offset overflow? */
307 if ((pgoff + (len >> PAGE_SHIFT)) < pgoff)
310 /* Do simple checking here so the lower-level routines won't have
311 * to. we assume access permissions have been handled by the open
312 * of the memory object, so we don't do any here.
314 vm_flags = calc_vm_flags(prot,flags) /* | mm->def_flags */ | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
317 * determine the object being mapped and call the appropriate
321 struct vm_area_struct vma;
328 vma.vm_end = addr + len;
329 vma.vm_flags = vm_flags;
330 vma.vm_pgoff = pgoff;
333 /* First, try simpler routine designed to give us a ROM pointer. */
335 if (file->f_op->romptr && !(prot & PROT_WRITE)) {
336 error = file->f_op->romptr(file, &vma);
338 printk("romptr mmap returned %d, start 0x%.8x\n", error,
343 else if (error != -ENOSYS)
346 #endif /* MAGIC_ROM_PTR */
347 /* Then try full mmap routine, which might return a RAM pointer,
348 or do something truly complicated. */
350 if (file->f_op->mmap) {
351 error = file->f_op->mmap(file, &vma);
354 printk("f_op->mmap() returned %d/%lx\n", error, vma.vm_start);
358 else if (error != -ENOSYS)
361 return -ENODEV; /* No mapping operations defined */
363 /* An ENOSYS error indicates that mmap isn't possible (as opposed to
364 tried but failed) so we'll fall through to the copy. */
367 tblock = (struct mm_tblock_struct *)
368 kmalloc(sizeof(struct mm_tblock_struct), GFP_KERNEL);
370 printk("Allocation of tblock for %lu byte allocation from process %d failed\n", len, current->pid);
375 tblock->rblock = (struct mm_rblock_struct *)
376 kmalloc(sizeof(struct mm_rblock_struct), GFP_KERNEL);
378 if (!tblock->rblock) {
379 printk("Allocation of rblock for %lu byte allocation from process %d failed\n", len, current->pid);
385 result = kmalloc(len, GFP_KERNEL);
387 printk("Allocation of length %lu from process %d failed\n", len,
390 kfree(tblock->rblock);
395 tblock->rblock->refcount = 1;
396 tblock->rblock->kblock = result;
397 tblock->rblock->size = len;
399 realalloc += kobjsize(result);
403 if ((len+WARN_ON_SLACK) <= kobjsize(result))
404 printk("Allocation of %lu bytes from process %d has %lu bytes of slack\n", len, current->pid, kobjsize(result)-len);
409 mm_segment_t old_fs = get_fs();
411 error = file->f_op->read(file, (char *) result, len, &file->f_pos);
415 kfree(tblock->rblock);
420 memset(result+error, '\0', len-error);
422 memset(result, '\0', len);
425 realalloc += kobjsize(tblock);
426 askedalloc += sizeof(struct mm_tblock_struct);
428 realalloc += kobjsize(tblock->rblock);
429 askedalloc += sizeof(struct mm_rblock_struct);
431 tblock->next = current->mm->context.tblock.next;
432 current->mm->context.tblock.next = tblock;
435 printk("do_mmap:\n");
436 show_process_blocks();
439 return (unsigned long)result;
442 int do_munmap(struct mm_struct * mm, unsigned long addr, size_t len)
444 struct mm_tblock_struct * tblock, *tmp;
448 * For efficiency's sake, if the pointer is obviously in ROM,
449 * don't bother walking the lists to free it.
456 printk("do_munmap:\n");
459 tmp = &mm->context.tblock; /* dummy head */
460 while ((tblock=tmp->next) && tblock->rblock &&
461 tblock->rblock->kblock != (void*)addr)
465 printk("munmap of non-mmaped memory by process %d (%s): %p\n",
466 current->pid, current->comm, (void*)addr);
469 if (tblock->rblock) {
470 if (!--tblock->rblock->refcount) {
471 if (tblock->rblock->kblock) {
472 realalloc -= kobjsize(tblock->rblock->kblock);
473 askedalloc -= tblock->rblock->size;
474 kfree(tblock->rblock->kblock);
477 realalloc -= kobjsize(tblock->rblock);
478 askedalloc -= sizeof(struct mm_rblock_struct);
479 kfree(tblock->rblock);
482 tmp->next = tblock->next;
483 realalloc -= kobjsize(tblock);
484 askedalloc -= sizeof(struct mm_tblock_struct);
488 show_process_blocks();
494 /* Release all mmaps. */
495 void exit_mmap(struct mm_struct * mm)
497 struct mm_tblock_struct *tmp;
503 printk("Exit_mmap:\n");
506 while((tmp = mm->context.tblock.next)) {
508 if (!--tmp->rblock->refcount) {
509 if (tmp->rblock->kblock) {
510 realalloc -= kobjsize(tmp->rblock->kblock);
511 askedalloc -= tmp->rblock->size;
512 kfree(tmp->rblock->kblock);
514 realalloc -= kobjsize(tmp->rblock);
515 askedalloc -= sizeof(struct mm_rblock_struct);
520 mm->context.tblock.next = tmp->next;
521 realalloc -= kobjsize(tmp);
522 askedalloc -= sizeof(struct mm_tblock_struct);
527 show_process_blocks();
531 asmlinkage long sys_munmap(unsigned long addr, size_t len)
534 struct mm_struct *mm = current->mm;
536 down_write(&mm->mmap_sem);
537 ret = do_munmap(mm, addr, len);
538 up_write(&mm->mmap_sem);
542 unsigned long do_brk(unsigned long addr, unsigned long len)
547 struct vm_area_struct * find_vma(struct mm_struct * mm, unsigned long addr)
552 struct page * follow_page(struct mm_struct *mm, unsigned long addr, int write)
557 struct vm_area_struct *find_extend_vma(struct mm_struct *mm, unsigned long addr)
562 int remap_page_range(struct vm_area_struct *vma, unsigned long from,
563 unsigned long to, unsigned long size, pgprot_t prot)
568 unsigned long get_unmapped_area(struct file *file, unsigned long addr,
569 unsigned long len, unsigned long pgoff, unsigned long flags)
574 void swap_unplug_io_fn(struct backing_dev_info *bdi, struct page *page)