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>
22 #include <linux/syscalls.h>
24 #include <asm/uaccess.h>
26 #include <asm/tlbflush.h>
30 unsigned long max_mapnr;
31 unsigned long num_physpages;
32 unsigned long askedalloc, realalloc;
33 atomic_t vm_committed_space = ATOMIC_INIT(0);
34 int sysctl_overcommit_memory = OVERCOMMIT_GUESS; /* heuristic overcommit */
35 int sysctl_overcommit_ratio = 50; /* default is 50% */
36 int sysctl_max_map_count = DEFAULT_MAX_MAP_COUNT;
38 EXPORT_SYMBOL(sysctl_max_map_count);
39 EXPORT_SYMBOL(mem_map);
42 * Handle all mappings that got truncated by a "truncate()"
45 * NOTE! We have to be ready to update the memory sharing
46 * between the file and the memory map for a potential last
47 * incomplete page. Ugly, but necessary.
49 int vmtruncate(struct inode *inode, loff_t offset)
51 struct address_space *mapping = inode->i_mapping;
54 if (inode->i_size < offset)
56 i_size_write(inode, offset);
58 truncate_inode_pages(mapping, offset);
62 limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
63 if (limit != RLIM_INFINITY && offset > limit)
65 if (offset > inode->i_sb->s_maxbytes)
67 i_size_write(inode, offset);
70 if (inode->i_op && inode->i_op->truncate)
71 inode->i_op->truncate(inode);
74 send_sig(SIGXFSZ, current, 0);
79 EXPORT_SYMBOL(vmtruncate);
82 * Return the total memory allocated for this pointer, not
83 * just what the caller asked for.
85 * Doesn't have to be accurate, i.e. may have races.
87 unsigned int kobjsize(const void *objp)
91 if (!objp || !((page = virt_to_page(objp))))
97 BUG_ON(page->index < 0);
98 BUG_ON(page->index >= MAX_ORDER);
100 return (PAGE_SIZE << page->index);
104 * The nommu dodgy version :-)
106 int get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
107 unsigned long start, int len, int write, int force,
108 struct page **pages, struct vm_area_struct **vmas)
111 static struct vm_area_struct dummy_vma;
113 for (i = 0; i < len; i++) {
115 pages[i] = virt_to_page(start);
117 page_cache_get(pages[i]);
120 vmas[i] = &dummy_vma;
126 rwlock_t vmlist_lock = RW_LOCK_UNLOCKED;
127 struct vm_struct *vmlist;
129 void vfree(void *addr)
134 void *__vmalloc(unsigned long size, int gfp_mask, pgprot_t prot)
137 * kmalloc doesn't like __GFP_HIGHMEM for some reason
139 return kmalloc(size, gfp_mask & ~__GFP_HIGHMEM);
142 struct page * vmalloc_to_page(void *addr)
144 return virt_to_page(addr);
147 unsigned long vmalloc_to_pfn(void *addr)
149 return page_to_pfn(virt_to_page(addr));
153 long vread(char *buf, char *addr, unsigned long count)
155 memcpy(buf, addr, count);
159 long vwrite(char *buf, char *addr, unsigned long count)
161 /* Don't allow overflow */
162 if ((unsigned long) addr + count < count)
163 count = -(unsigned long) addr;
165 memcpy(addr, buf, count);
170 * vmalloc - allocate virtually continguos memory
172 * @size: allocation size
174 * Allocate enough pages to cover @size from the page level
175 * allocator and map them into continguos kernel virtual space.
177 * For tight cotrol over page level allocator and protection flags
178 * use __vmalloc() instead.
180 void *vmalloc(unsigned long size)
182 return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL);
186 * vmalloc_32 - allocate virtually continguos memory (32bit addressable)
188 * @size: allocation size
190 * Allocate enough 32bit PA addressable pages to cover @size from the
191 * page level allocator and map them into continguos kernel virtual space.
193 void *vmalloc_32(unsigned long size)
195 return __vmalloc(size, GFP_KERNEL, PAGE_KERNEL);
198 void *vmap(struct page **pages, unsigned int count, unsigned long flags, pgprot_t prot)
204 void vunmap(void *addr)
210 * sys_brk() for the most part doesn't need the global kernel
211 * lock, except when an application is doing something nasty
212 * like trying to un-brk an area that has already been mapped
213 * to a regular file. in this case, the unmapping will need
214 * to invoke file system routines that need the global lock.
216 asmlinkage unsigned long sys_brk(unsigned long brk)
218 struct mm_struct *mm = current->mm;
220 if (brk < mm->end_code || brk < mm->start_brk || brk > mm->context.end_brk)
227 * Always allow shrinking brk
229 if (brk <= mm->brk) {
235 * Ok, looks good - let it rip.
237 return mm->brk = brk;
241 * Combine the mmap "prot" and "flags" argument into one "vm_flags" used
242 * internally. Essentially, translate the "PROT_xxx" and "MAP_xxx" bits
245 static inline unsigned long calc_vm_flags(unsigned long prot, unsigned long flags)
247 #define _trans(x,bit1,bit2) \
248 ((bit1==bit2)?(x&bit1):(x&bit1)?bit2:0)
250 unsigned long prot_bits, flag_bits;
252 _trans(prot, PROT_READ, VM_READ) |
253 _trans(prot, PROT_WRITE, VM_WRITE) |
254 _trans(prot, PROT_EXEC, VM_EXEC);
256 _trans(flags, MAP_GROWSDOWN, VM_GROWSDOWN) |
257 _trans(flags, MAP_DENYWRITE, VM_DENYWRITE) |
258 _trans(flags, MAP_EXECUTABLE, VM_EXECUTABLE);
259 return prot_bits | flag_bits;
264 static void show_process_blocks(void)
266 struct mm_tblock_struct *tblock;
268 printk("Process blocks %d:", current->pid);
270 for (tblock = ¤t->mm->context.tblock; tblock; tblock = tblock->next) {
271 printk(" %p: %p", tblock, tblock->rblock);
273 printk(" (%d @%p #%d)", kobjsize(tblock->rblock->kblock), tblock->rblock->kblock, tblock->rblock->refcount);
274 printk(tblock->next ? " ->" : ".\n");
279 unsigned long do_mmap_pgoff(
288 struct mm_tblock_struct * tblock;
289 unsigned int vm_flags;
292 * Get the !CONFIG_MMU specific checks done first
294 if ((flags & MAP_SHARED) && (prot & PROT_WRITE) && (file)) {
295 printk("MAP_SHARED not supported (cannot write mappings to disk)\n");
299 if ((prot & PROT_WRITE) && (flags & MAP_PRIVATE)) {
300 printk("Private writable mappings not supported\n");
305 * now all the standard checks
307 if (file && (!file->f_op || !file->f_op->mmap))
310 if (PAGE_ALIGN(len) == 0)
316 /* offset overflow? */
317 if ((pgoff + (len >> PAGE_SHIFT)) < pgoff)
320 /* Do simple checking here so the lower-level routines won't have
321 * to. we assume access permissions have been handled by the open
322 * of the memory object, so we don't do any here.
324 vm_flags = calc_vm_flags(prot,flags) /* | mm->def_flags */ | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
327 * determine the object being mapped and call the appropriate
331 struct vm_area_struct vma;
338 vma.vm_end = addr + len;
339 vma.vm_flags = vm_flags;
340 vma.vm_pgoff = pgoff;
343 /* First, try simpler routine designed to give us a ROM pointer. */
345 if (file->f_op->romptr && !(prot & PROT_WRITE)) {
346 error = file->f_op->romptr(file, &vma);
348 printk("romptr mmap returned %d, start 0x%.8x\n", error,
353 else if (error != -ENOSYS)
356 #endif /* MAGIC_ROM_PTR */
357 /* Then try full mmap routine, which might return a RAM pointer,
358 or do something truly complicated. */
360 if (file->f_op->mmap) {
361 error = file->f_op->mmap(file, &vma);
364 printk("f_op->mmap() returned %d/%lx\n", error, vma.vm_start);
368 else if (error != -ENOSYS)
371 return -ENODEV; /* No mapping operations defined */
373 /* An ENOSYS error indicates that mmap isn't possible (as opposed to
374 tried but failed) so we'll fall through to the copy. */
377 tblock = (struct mm_tblock_struct *)
378 kmalloc(sizeof(struct mm_tblock_struct), GFP_KERNEL);
380 printk("Allocation of tblock for %lu byte allocation from process %d failed\n", len, current->pid);
385 tblock->rblock = (struct mm_rblock_struct *)
386 kmalloc(sizeof(struct mm_rblock_struct), GFP_KERNEL);
388 if (!tblock->rblock) {
389 printk("Allocation of rblock for %lu byte allocation from process %d failed\n", len, current->pid);
395 result = kmalloc(len, GFP_KERNEL);
397 printk("Allocation of length %lu from process %d failed\n", len,
400 kfree(tblock->rblock);
405 tblock->rblock->refcount = 1;
406 tblock->rblock->kblock = result;
407 tblock->rblock->size = len;
409 realalloc += kobjsize(result);
413 if ((len+WARN_ON_SLACK) <= kobjsize(result))
414 printk("Allocation of %lu bytes from process %d has %lu bytes of slack\n", len, current->pid, kobjsize(result)-len);
419 mm_segment_t old_fs = get_fs();
421 error = file->f_op->read(file, (char *) result, len, &file->f_pos);
425 kfree(tblock->rblock);
430 memset(result+error, '\0', len-error);
432 memset(result, '\0', len);
435 realalloc += kobjsize(tblock);
436 askedalloc += sizeof(struct mm_tblock_struct);
438 realalloc += kobjsize(tblock->rblock);
439 askedalloc += sizeof(struct mm_rblock_struct);
441 tblock->next = current->mm->context.tblock.next;
442 current->mm->context.tblock.next = tblock;
443 // current->mm->total_vm += len >> PAGE_SHIFT;
444 vx_vmpages_add(current->mm, len >> PAGE_SHIFT);
447 printk("do_mmap:\n");
448 show_process_blocks();
451 return (unsigned long)result;
454 int do_munmap(struct mm_struct * mm, unsigned long addr, size_t len)
456 struct mm_tblock_struct * tblock, *tmp;
460 * For efficiency's sake, if the pointer is obviously in ROM,
461 * don't bother walking the lists to free it.
468 printk("do_munmap:\n");
471 tmp = &mm->context.tblock; /* dummy head */
472 while ((tblock=tmp->next) && tblock->rblock &&
473 tblock->rblock->kblock != (void*)addr)
477 printk("munmap of non-mmaped memory by process %d (%s): %p\n",
478 current->pid, current->comm, (void*)addr);
481 if (tblock->rblock) {
482 if (!--tblock->rblock->refcount) {
483 if (tblock->rblock->kblock) {
484 realalloc -= kobjsize(tblock->rblock->kblock);
485 askedalloc -= tblock->rblock->size;
486 kfree(tblock->rblock->kblock);
489 realalloc -= kobjsize(tblock->rblock);
490 askedalloc -= sizeof(struct mm_rblock_struct);
491 kfree(tblock->rblock);
494 tmp->next = tblock->next;
495 realalloc -= kobjsize(tblock);
496 askedalloc -= sizeof(struct mm_tblock_struct);
498 // mm->total_vm -= len >> PAGE_SHIFT;
499 vx_vmpages_sub(mm, len >> PAGE_SHIFT);
502 show_process_blocks();
508 /* Release all mmaps. */
509 void exit_mmap(struct mm_struct * mm)
511 struct mm_tblock_struct *tmp;
513 vx_vmpages_sub(mm, mm->total_vm);
519 printk("Exit_mmap:\n");
522 while((tmp = mm->context.tblock.next)) {
524 if (!--tmp->rblock->refcount) {
525 if (tmp->rblock->kblock) {
526 realalloc -= kobjsize(tmp->rblock->kblock);
527 askedalloc -= tmp->rblock->size;
528 kfree(tmp->rblock->kblock);
530 realalloc -= kobjsize(tmp->rblock);
531 askedalloc -= sizeof(struct mm_rblock_struct);
536 mm->context.tblock.next = tmp->next;
537 realalloc -= kobjsize(tmp);
538 askedalloc -= sizeof(struct mm_tblock_struct);
543 show_process_blocks();
547 asmlinkage long sys_munmap(unsigned long addr, size_t len)
550 struct mm_struct *mm = current->mm;
552 down_write(&mm->mmap_sem);
553 ret = do_munmap(mm, addr, len);
554 up_write(&mm->mmap_sem);
558 unsigned long do_brk(unsigned long addr, unsigned long len)
563 struct vm_area_struct * find_vma(struct mm_struct * mm, unsigned long addr)
568 struct page * follow_page(struct mm_struct *mm, unsigned long addr, int write)
573 struct vm_area_struct *find_extend_vma(struct mm_struct *mm, unsigned long addr)
578 int remap_pfn_range(struct vm_area_struct *vma, unsigned long from,
579 unsigned long to, unsigned long size, pgprot_t prot)
584 unsigned long get_unmapped_area(struct file *file, unsigned long addr,
585 unsigned long len, unsigned long pgoff, unsigned long flags)
590 void swap_unplug_io_fn(struct backing_dev_info *bdi, struct page *page)
594 unsigned long arch_get_unmapped_area(struct file *file, unsigned long addr,
595 unsigned long len, unsigned long pgoff, unsigned long flags)
600 void arch_unmap_area(struct vm_area_struct *area)