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>
22 #include <asm/pgalloc.h>
23 #include <asm/uaccess.h>
25 #include <asm/tlbflush.h>
28 struct page
*mem_map
= NULL
;
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
= 0; /* default is heuristic overcommit */
34 int sysctl_overcommit_ratio
= 50; /* default is 50% */
37 * Handle all mappings that got truncated by a "truncate()"
40 * NOTE! We have to be ready to update the memory sharing
41 * between the file and the memory map for a potential last
42 * incomplete page. Ugly, but necessary.
44 int vmtruncate(struct inode
*inode
, loff_t offset
)
46 struct address_space
*mapping
= inode
->i_mapping
;
49 if (inode
->i_size
< offset
)
51 i_size_write(inode
, offset
);
53 truncate_inode_pages(mapping
, offset
);
57 limit
= current
->rlim
[RLIMIT_FSIZE
].rlim_cur
;
58 if (limit
!= RLIM_INFINITY
&& offset
> limit
)
60 if (offset
> inode
->i_sb
->s_maxbytes
)
62 i_size_write(inode
, offset
);
65 if (inode
->i_op
&& inode
->i_op
->truncate
)
66 inode
->i_op
->truncate(inode
);
69 send_sig(SIGXFSZ
, current
, 0);
75 * Return the total memory allocated for this pointer, not
76 * just what the caller asked for.
78 * Doesn't have to be accurate, i.e. may have races.
80 unsigned int kobjsize(const void *objp
)
84 if (!objp
|| !((page
= virt_to_page(objp
))))
90 BUG_ON(page
->index
< 0);
91 BUG_ON(page
->index
>= MAX_ORDER
);
93 return (PAGE_SIZE
<< page
->index
);
97 * The nommu dodgy version :-)
99 int get_user_pages(struct task_struct
*tsk
, struct mm_struct
*mm
,
100 unsigned long start
, int len
, int write
, int force
,
101 struct page
**pages
, struct vm_area_struct
**vmas
)
104 static struct vm_area_struct dummy_vma
;
106 for (i
= 0; i
< len
; i
++) {
108 pages
[i
] = virt_to_page(start
);
110 page_cache_get(pages
[i
]);
113 vmas
[i
] = &dummy_vma
;
119 rwlock_t vmlist_lock
= RW_LOCK_UNLOCKED
;
120 struct vm_struct
*vmlist
;
122 void vfree(void *addr
)
127 void *__vmalloc(unsigned long size
, int gfp_mask
, pgprot_t prot
)
130 * kmalloc doesn't like __GFP_HIGHMEM for some reason
132 return kmalloc(size
, gfp_mask
& ~__GFP_HIGHMEM
);
135 struct page
* vmalloc_to_page(void *addr
)
137 return virt_to_page(addr
);
140 long vread(char *buf
, char *addr
, unsigned long count
)
142 memcpy(buf
, addr
, count
);
146 long vwrite(char *buf
, char *addr
, unsigned long count
)
148 /* Don't allow overflow */
149 if ((unsigned long) addr
+ count
< count
)
150 count
= -(unsigned long) addr
;
152 memcpy(addr
, buf
, count
);
157 * vmalloc - allocate virtually continguos memory
159 * @size: allocation size
161 * Allocate enough pages to cover @size from the page level
162 * allocator and map them into continguos kernel virtual space.
164 * For tight cotrol over page level allocator and protection flags
165 * use __vmalloc() instead.
167 void *vmalloc(unsigned long size
)
169 return __vmalloc(size
, GFP_KERNEL
| __GFP_HIGHMEM
, PAGE_KERNEL
);
173 * vmalloc_32 - allocate virtually continguos memory (32bit addressable)
175 * @size: allocation size
177 * Allocate enough 32bit PA addressable pages to cover @size from the
178 * page level allocator and map them into continguos kernel virtual space.
180 void *vmalloc_32(unsigned long size
)
182 return __vmalloc(size
, GFP_KERNEL
, PAGE_KERNEL
);
185 void *vmap(struct page
**pages
, unsigned int count
, unsigned long flags
, pgprot_t prot
)
191 void vunmap(void *addr
)
197 * sys_brk() for the most part doesn't need the global kernel
198 * lock, except when an application is doing something nasty
199 * like trying to un-brk an area that has already been mapped
200 * to a regular file. in this case, the unmapping will need
201 * to invoke file system routines that need the global lock.
203 asmlinkage
unsigned long sys_brk(unsigned long brk
)
205 struct mm_struct
*mm
= current
->mm
;
207 if (brk
< mm
->end_code
|| brk
< mm
->start_brk
|| brk
> mm
->context
.end_brk
)
214 * Always allow shrinking brk
216 if (brk
<= mm
->brk
) {
222 * Ok, looks good - let it rip.
224 return mm
->brk
= brk
;
228 * Combine the mmap "prot" and "flags" argument into one "vm_flags" used
229 * internally. Essentially, translate the "PROT_xxx" and "MAP_xxx" bits
232 static inline unsigned long calc_vm_flags(unsigned long prot
, unsigned long flags
)
234 #define _trans(x,bit1,bit2) \
235 ((bit1==bit2)?(x&bit1):(x&bit1)?bit2:0)
237 unsigned long prot_bits
, flag_bits
;
239 _trans(prot
, PROT_READ
, VM_READ
) |
240 _trans(prot
, PROT_WRITE
, VM_WRITE
) |
241 _trans(prot
, PROT_EXEC
, VM_EXEC
);
243 _trans(flags
, MAP_GROWSDOWN
, VM_GROWSDOWN
) |
244 _trans(flags
, MAP_DENYWRITE
, VM_DENYWRITE
) |
245 _trans(flags
, MAP_EXECUTABLE
, VM_EXECUTABLE
);
246 return prot_bits
| flag_bits
;
251 static void show_process_blocks(void)
253 struct mm_tblock_struct
*tblock
;
255 printk("Process blocks %d:", current
->pid
);
257 for (tblock
= ¤t
->mm
->context
.tblock
; tblock
; tblock
= tblock
->next
) {
258 printk(" %p: %p", tblock
, tblock
->rblock
);
260 printk(" (%d @%p #%d)", kobjsize(tblock
->rblock
->kblock
), tblock
->rblock
->kblock
, tblock
->rblock
->refcount
);
261 printk(tblock
->next
? " ->" : ".\n");
266 unsigned long do_mmap_pgoff(
275 struct mm_tblock_struct
* tblock
;
276 unsigned int vm_flags
;
279 * Get the !CONFIG_MMU specific checks done first
281 if ((flags
& MAP_SHARED
) && (prot
& PROT_WRITE
) && (file
)) {
282 printk("MAP_SHARED not supported (cannot write mappings to disk)\n");
286 if ((prot
& PROT_WRITE
) && (flags
& MAP_PRIVATE
)) {
287 printk("Private writable mappings not supported\n");
292 * now all the standard checks
294 if (file
&& (!file
->f_op
|| !file
->f_op
->mmap
))
297 if (PAGE_ALIGN(len
) == 0)
303 /* offset overflow? */
304 if ((pgoff
+ (len
>> PAGE_SHIFT
)) < pgoff
)
307 /* Do simple checking here so the lower-level routines won't have
308 * to. we assume access permissions have been handled by the open
309 * of the memory object, so we don't do any here.
311 vm_flags
= calc_vm_flags(prot
,flags
) /* | mm->def_flags */ | VM_MAYREAD
| VM_MAYWRITE
| VM_MAYEXEC
;
314 * determine the object being mapped and call the appropriate
318 struct vm_area_struct vma
;
325 vma
.vm_end
= addr
+ len
;
326 vma
.vm_flags
= vm_flags
;
327 vma
.vm_pgoff
= pgoff
;
330 /* First, try simpler routine designed to give us a ROM pointer. */
332 if (file
->f_op
->romptr
&& !(prot
& PROT_WRITE
)) {
333 error
= file
->f_op
->romptr(file
, &vma
);
335 printk("romptr mmap returned %d, start 0x%.8x\n", error
,
340 else if (error
!= -ENOSYS
)
343 #endif /* MAGIC_ROM_PTR */
344 /* Then try full mmap routine, which might return a RAM pointer,
345 or do something truly complicated. */
347 if (file
->f_op
->mmap
) {
348 error
= file
->f_op
->mmap(file
, &vma
);
351 printk("f_op->mmap() returned %d/%lx\n", error
, vma
.vm_start
);
355 else if (error
!= -ENOSYS
)
358 return -ENODEV
; /* No mapping operations defined */
360 /* An ENOSYS error indicates that mmap isn't possible (as opposed to
361 tried but failed) so we'll fall through to the copy. */
364 tblock
= (struct mm_tblock_struct
*)
365 kmalloc(sizeof(struct mm_tblock_struct
), GFP_KERNEL
);
367 printk("Allocation of tblock for %lu byte allocation from process %d failed\n", len
, current
->pid
);
372 tblock
->rblock
= (struct mm_rblock_struct
*)
373 kmalloc(sizeof(struct mm_rblock_struct
), GFP_KERNEL
);
375 if (!tblock
->rblock
) {
376 printk("Allocation of rblock for %lu byte allocation from process %d failed\n", len
, current
->pid
);
382 result
= kmalloc(len
, GFP_KERNEL
);
384 printk("Allocation of length %lu from process %d failed\n", len
,
387 kfree(tblock
->rblock
);
392 tblock
->rblock
->refcount
= 1;
393 tblock
->rblock
->kblock
= result
;
394 tblock
->rblock
->size
= len
;
396 realalloc
+= kobjsize(result
);
400 if ((len
+WARN_ON_SLACK
) <= kobjsize(result
))
401 printk("Allocation of %lu bytes from process %d has %lu bytes of slack\n", len
, current
->pid
, kobjsize(result
)-len
);
406 mm_segment_t old_fs
= get_fs();
408 error
= file
->f_op
->read(file
, (char *) result
, len
, &file
->f_pos
);
412 kfree(tblock
->rblock
);
417 memset(result
+error
, '\0', len
-error
);
419 memset(result
, '\0', len
);
422 realalloc
+= kobjsize(tblock
);
423 askedalloc
+= sizeof(struct mm_tblock_struct
);
425 realalloc
+= kobjsize(tblock
->rblock
);
426 askedalloc
+= sizeof(struct mm_rblock_struct
);
428 tblock
->next
= current
->mm
->context
.tblock
.next
;
429 current
->mm
->context
.tblock
.next
= tblock
;
432 printk("do_mmap:\n");
433 show_process_blocks();
436 return (unsigned long)result
;
439 int do_munmap(struct mm_struct
* mm
, unsigned long addr
, size_t len
)
441 struct mm_tblock_struct
* tblock
, *tmp
;
445 * For efficiency's sake, if the pointer is obviously in ROM,
446 * don't bother walking the lists to free it.
453 printk("do_munmap:\n");
456 tmp
= &mm
->context
.tblock
; /* dummy head */
457 while ((tblock
=tmp
->next
) && tblock
->rblock
&&
458 tblock
->rblock
->kblock
!= (void*)addr
)
462 printk("munmap of non-mmaped memory by process %d (%s): %p\n",
463 current
->pid
, current
->comm
, (void*)addr
);
466 if (tblock
->rblock
) {
467 if (!--tblock
->rblock
->refcount
) {
468 if (tblock
->rblock
->kblock
) {
469 realalloc
-= kobjsize(tblock
->rblock
->kblock
);
470 askedalloc
-= tblock
->rblock
->size
;
471 kfree(tblock
->rblock
->kblock
);
474 realalloc
-= kobjsize(tblock
->rblock
);
475 askedalloc
-= sizeof(struct mm_rblock_struct
);
476 kfree(tblock
->rblock
);
479 tmp
->next
= tblock
->next
;
480 realalloc
-= kobjsize(tblock
);
481 askedalloc
-= sizeof(struct mm_tblock_struct
);
485 show_process_blocks();
491 /* Release all mmaps. */
492 void exit_mmap(struct mm_struct
* mm
)
494 struct mm_tblock_struct
*tmp
;
500 printk("Exit_mmap:\n");
503 while((tmp
= mm
->context
.tblock
.next
)) {
505 if (!--tmp
->rblock
->refcount
) {
506 if (tmp
->rblock
->kblock
) {
507 realalloc
-= kobjsize(tmp
->rblock
->kblock
);
508 askedalloc
-= tmp
->rblock
->size
;
509 kfree(tmp
->rblock
->kblock
);
511 realalloc
-= kobjsize(tmp
->rblock
);
512 askedalloc
-= sizeof(struct mm_rblock_struct
);
517 mm
->context
.tblock
.next
= tmp
->next
;
518 realalloc
-= kobjsize(tmp
);
519 askedalloc
-= sizeof(struct mm_tblock_struct
);
524 show_process_blocks();
528 asmlinkage
long sys_munmap(unsigned long addr
, size_t len
)
531 struct mm_struct
*mm
= current
->mm
;
533 down_write(&mm
->mmap_sem
);
534 ret
= do_munmap(mm
, addr
, len
);
535 up_write(&mm
->mmap_sem
);
539 unsigned long do_brk(unsigned long addr
, unsigned long len
)
544 struct vm_area_struct
* find_vma(struct mm_struct
* mm
, unsigned long addr
)
549 struct page
* follow_page(struct mm_struct
*mm
, unsigned long addr
, int write
)
554 int remap_page_range(struct vm_area_struct
*vma
, unsigned long from
,
555 unsigned long to
, unsigned long size
, pgprot_t prot
)
560 unsigned long get_unmapped_area(struct file
*file
, unsigned long addr
,
561 unsigned long len
, unsigned long pgoff
, unsigned long flags
)
566 void pte_chain_init(void)