This commit was manufactured by cvs2svn to create tag
[linux-2.6.git] / mm / nommu.c
1 /*
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  *  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>
10  */
11
12 #include <linux/mm.h>
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>
23
24 #include <asm/uaccess.h>
25 #include <asm/tlb.h>
26 #include <asm/tlbflush.h>
27
28 void *high_memory;
29 struct page *mem_map;
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;
37
38 EXPORT_SYMBOL(sysctl_max_map_count);
39 EXPORT_SYMBOL(mem_map);
40
41 /*
42  * Handle all mappings that got truncated by a "truncate()"
43  * system call.
44  *
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.
48  */
49 int vmtruncate(struct inode *inode, loff_t offset)
50 {
51         struct address_space *mapping = inode->i_mapping;
52         unsigned long limit;
53
54         if (inode->i_size < offset)
55                 goto do_expand;
56         i_size_write(inode, offset);
57
58         truncate_inode_pages(mapping, offset);
59         goto out_truncate;
60
61 do_expand:
62         limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
63         if (limit != RLIM_INFINITY && offset > limit)
64                 goto out_sig;
65         if (offset > inode->i_sb->s_maxbytes)
66                 goto out;
67         i_size_write(inode, offset);
68
69 out_truncate:
70         if (inode->i_op && inode->i_op->truncate)
71                 inode->i_op->truncate(inode);
72         return 0;
73 out_sig:
74         send_sig(SIGXFSZ, current, 0);
75 out:
76         return -EFBIG;
77 }
78
79 EXPORT_SYMBOL(vmtruncate);
80
81 /*
82  * Return the total memory allocated for this pointer, not
83  * just what the caller asked for.
84  *
85  * Doesn't have to be accurate, i.e. may have races.
86  */
87 unsigned int kobjsize(const void *objp)
88 {
89         struct page *page;
90
91         if (!objp || !((page = virt_to_page(objp))))
92                 return 0;
93
94         if (PageSlab(page))
95                 return ksize(objp);
96
97         BUG_ON(page->index < 0);
98         BUG_ON(page->index >= MAX_ORDER);
99
100         return (PAGE_SIZE << page->index);
101 }
102
103 /*
104  * The nommu dodgy version :-)
105  */
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)
109 {
110         int i;
111         static struct vm_area_struct dummy_vma;
112
113         for (i = 0; i < len; i++) {
114                 if (pages) {
115                         pages[i] = virt_to_page(start);
116                         if (pages[i])
117                                 page_cache_get(pages[i]);
118                 }
119                 if (vmas)
120                         vmas[i] = &dummy_vma;
121                 start += PAGE_SIZE;
122         }
123         return(i);
124 }
125
126 rwlock_t vmlist_lock = RW_LOCK_UNLOCKED;
127 struct vm_struct *vmlist;
128
129 void vfree(void *addr)
130 {
131         kfree(addr);
132 }
133
134 void *__vmalloc(unsigned long size, int gfp_mask, pgprot_t prot)
135 {
136         /*
137          * kmalloc doesn't like __GFP_HIGHMEM for some reason
138          */
139         return kmalloc(size, gfp_mask & ~__GFP_HIGHMEM);
140 }
141
142 struct page * vmalloc_to_page(void *addr)
143 {
144         return virt_to_page(addr);
145 }
146
147 unsigned long vmalloc_to_pfn(void *addr)
148 {
149         return page_to_pfn(virt_to_page(addr));
150 }
151
152
153 long vread(char *buf, char *addr, unsigned long count)
154 {
155         memcpy(buf, addr, count);
156         return count;
157 }
158
159 long vwrite(char *buf, char *addr, unsigned long count)
160 {
161         /* Don't allow overflow */
162         if ((unsigned long) addr + count < count)
163                 count = -(unsigned long) addr;
164         
165         memcpy(addr, buf, count);
166         return(count);
167 }
168
169 /*
170  *      vmalloc  -  allocate virtually continguos memory
171  *
172  *      @size:          allocation size
173  *
174  *      Allocate enough pages to cover @size from the page level
175  *      allocator and map them into continguos kernel virtual space.
176  *
177  *      For tight cotrol over page level allocator and protection flags
178  *      use __vmalloc() instead.
179  */
180 void *vmalloc(unsigned long size)
181 {
182        return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL);
183 }
184
185 /*
186  *      vmalloc_32  -  allocate virtually continguos memory (32bit addressable)
187  *
188  *      @size:          allocation size
189  *
190  *      Allocate enough 32bit PA addressable pages to cover @size from the
191  *      page level allocator and map them into continguos kernel virtual space.
192  */
193 void *vmalloc_32(unsigned long size)
194 {
195         return __vmalloc(size, GFP_KERNEL, PAGE_KERNEL);
196 }
197
198 void *vmap(struct page **pages, unsigned int count, unsigned long flags, pgprot_t prot)
199 {
200         BUG();
201         return NULL;
202 }
203
204 void vunmap(void *addr)
205 {
206         BUG();
207 }
208
209 /*
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.
215  */
216 asmlinkage unsigned long sys_brk(unsigned long brk)
217 {
218         struct mm_struct *mm = current->mm;
219
220         if (brk < mm->end_code || brk < mm->start_brk || brk > mm->context.end_brk)
221                 return mm->brk;
222
223         if (mm->brk == brk)
224                 return mm->brk;
225
226         /*
227          * Always allow shrinking brk
228          */
229         if (brk <= mm->brk) {
230                 mm->brk = brk;
231                 return brk;
232         }
233
234         /*
235          * Ok, looks good - let it rip.
236          */
237         return mm->brk = brk;
238 }
239
240 /*
241  * Combine the mmap "prot" and "flags" argument into one "vm_flags" used
242  * internally. Essentially, translate the "PROT_xxx" and "MAP_xxx" bits
243  * into "VM_xxx".
244  */
245 static inline unsigned long calc_vm_flags(unsigned long prot, unsigned long flags)
246 {
247 #define _trans(x,bit1,bit2) \
248 ((bit1==bit2)?(x&bit1):(x&bit1)?bit2:0)
249
250         unsigned long prot_bits, flag_bits;
251         prot_bits =
252                 _trans(prot, PROT_READ, VM_READ) |
253                 _trans(prot, PROT_WRITE, VM_WRITE) |
254                 _trans(prot, PROT_EXEC, VM_EXEC);
255         flag_bits =
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;
260 #undef _trans
261 }
262
263 #ifdef DEBUG
264 static void show_process_blocks(void)
265 {
266         struct mm_tblock_struct *tblock;
267
268         printk("Process blocks %d:", current->pid);
269
270         for (tblock = &current->mm->context.tblock; tblock; tblock = tblock->next) {
271                 printk(" %p: %p", tblock, tblock->rblock);
272                 if (tblock->rblock)
273                         printk(" (%d @%p #%d)", kobjsize(tblock->rblock->kblock), tblock->rblock->kblock, tblock->rblock->refcount);
274                 printk(tblock->next ? " ->" : ".\n");
275         }
276 }
277 #endif /* DEBUG */
278
279 unsigned long do_mmap_pgoff(
280         struct file * file,
281         unsigned long addr,
282         unsigned long len,
283         unsigned long prot,
284         unsigned long flags,
285         unsigned long pgoff)
286 {
287         void * result;
288         struct mm_tblock_struct * tblock;
289         unsigned int vm_flags;
290
291         /*
292          * Get the !CONFIG_MMU specific checks done first
293          */
294         if ((flags & MAP_SHARED) && (prot & PROT_WRITE) && (file)) {
295                 printk("MAP_SHARED not supported (cannot write mappings to disk)\n");
296                 return -EINVAL;
297         }
298         
299         if ((prot & PROT_WRITE) && (flags & MAP_PRIVATE)) {
300                 printk("Private writable mappings not supported\n");
301                 return -EINVAL;
302         }
303         
304         /*
305          *      now all the standard checks
306          */
307         if (file && (!file->f_op || !file->f_op->mmap))
308                 return -ENODEV;
309
310         if (PAGE_ALIGN(len) == 0)
311                 return addr;
312
313         if (len > TASK_SIZE)
314                 return -EINVAL;
315
316         /* offset overflow? */
317         if ((pgoff + (len >> PAGE_SHIFT)) < pgoff)
318                 return -EINVAL;
319
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.
323          */
324         vm_flags = calc_vm_flags(prot,flags) /* | mm->def_flags */ | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
325
326         /*
327          * determine the object being mapped and call the appropriate
328          * specific mapper. 
329          */
330         if (file) {
331                 struct vm_area_struct vma;
332                 int error;
333
334                 if (!file->f_op)
335                         return -ENODEV;
336
337                 vma.vm_start = addr;
338                 vma.vm_end = addr + len;
339                 vma.vm_flags = vm_flags;
340                 vma.vm_pgoff = pgoff;
341
342 #ifdef MAGIC_ROM_PTR
343                 /* First, try simpler routine designed to give us a ROM pointer. */
344
345                 if (file->f_op->romptr && !(prot & PROT_WRITE)) {
346                         error = file->f_op->romptr(file, &vma);
347 #ifdef DEBUG
348                         printk("romptr mmap returned %d, start 0x%.8x\n", error,
349                                         vma.vm_start);
350 #endif
351                         if (!error)
352                                 return vma.vm_start;
353                         else if (error != -ENOSYS)
354                                 return error;
355                 } else
356 #endif /* MAGIC_ROM_PTR */
357                 /* Then try full mmap routine, which might return a RAM pointer,
358                    or do something truly complicated. */
359                    
360                 if (file->f_op->mmap) {
361                         error = file->f_op->mmap(file, &vma);
362                                    
363 #ifdef DEBUG
364                         printk("f_op->mmap() returned %d/%lx\n", error, vma.vm_start);
365 #endif
366                         if (!error)
367                                 return vma.vm_start;
368                         else if (error != -ENOSYS)
369                                 return error;
370                 } else
371                         return -ENODEV; /* No mapping operations defined */
372
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. */
375         }
376
377         tblock = (struct mm_tblock_struct *)
378                         kmalloc(sizeof(struct mm_tblock_struct), GFP_KERNEL);
379         if (!tblock) {
380                 printk("Allocation of tblock for %lu byte allocation from process %d failed\n", len, current->pid);
381                 show_free_areas();
382                 return -ENOMEM;
383         }
384
385         tblock->rblock = (struct mm_rblock_struct *)
386                         kmalloc(sizeof(struct mm_rblock_struct), GFP_KERNEL);
387
388         if (!tblock->rblock) {
389                 printk("Allocation of rblock for %lu byte allocation from process %d failed\n", len, current->pid);
390                 show_free_areas();
391                 kfree(tblock);
392                 return -ENOMEM;
393         }
394
395         result = kmalloc(len, GFP_KERNEL);
396         if (!result) {
397                 printk("Allocation of length %lu from process %d failed\n", len,
398                                 current->pid);
399                 show_free_areas();
400                 kfree(tblock->rblock);
401                 kfree(tblock);
402                 return -ENOMEM;
403         }
404
405         tblock->rblock->refcount = 1;
406         tblock->rblock->kblock = result;
407         tblock->rblock->size = len;
408         
409         realalloc += kobjsize(result);
410         askedalloc += len;
411
412 #ifdef WARN_ON_SLACK    
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);
415 #endif
416         
417         if (file) {
418                 int error;
419                 mm_segment_t old_fs = get_fs();
420                 set_fs(KERNEL_DS);
421                 error = file->f_op->read(file, (char *) result, len, &file->f_pos);
422                 set_fs(old_fs);
423                 if (error < 0) {
424                         kfree(result);
425                         kfree(tblock->rblock);
426                         kfree(tblock);
427                         return error;
428                 }
429                 if (error < len)
430                         memset(result+error, '\0', len-error);
431         } else {
432                 memset(result, '\0', len);
433         }
434
435         realalloc += kobjsize(tblock);
436         askedalloc += sizeof(struct mm_tblock_struct);
437
438         realalloc += kobjsize(tblock->rblock);
439         askedalloc += sizeof(struct mm_rblock_struct);
440
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);
445
446 #ifdef DEBUG
447         printk("do_mmap:\n");
448         show_process_blocks();
449 #endif    
450
451         return (unsigned long)result;
452 }
453
454 int do_munmap(struct mm_struct * mm, unsigned long addr, size_t len)
455 {
456         struct mm_tblock_struct * tblock, *tmp;
457
458 #ifdef MAGIC_ROM_PTR
459         /*
460          * For efficiency's sake, if the pointer is obviously in ROM,
461          * don't bother walking the lists to free it.
462          */
463         if (is_in_rom(addr))
464                 return 0;
465 #endif
466
467 #ifdef DEBUG
468         printk("do_munmap:\n");
469 #endif
470
471         tmp = &mm->context.tblock; /* dummy head */
472         while ((tblock=tmp->next) && tblock->rblock &&
473                         tblock->rblock->kblock != (void*)addr) 
474                 tmp = tblock;
475                 
476         if (!tblock) {
477                 printk("munmap of non-mmaped memory by process %d (%s): %p\n",
478                                 current->pid, current->comm, (void*)addr);
479                 return -EINVAL;
480         }
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);
487                         }
488                         
489                         realalloc -= kobjsize(tblock->rblock);
490                         askedalloc -= sizeof(struct mm_rblock_struct);
491                         kfree(tblock->rblock);
492                 }
493         }
494         tmp->next = tblock->next;
495         realalloc -= kobjsize(tblock);
496         askedalloc -= sizeof(struct mm_tblock_struct);
497         kfree(tblock);
498         // mm->total_vm -= len >> PAGE_SHIFT;
499         vx_vmpages_sub(mm, len >> PAGE_SHIFT);
500
501 #ifdef DEBUG
502         show_process_blocks();
503 #endif    
504
505         return 0;
506 }
507
508 /* Release all mmaps. */
509 void exit_mmap(struct mm_struct * mm)
510 {
511         struct mm_tblock_struct *tmp;
512         // mm->total_vm = 0;
513         vx_vmpages_sub(mm, mm->total_vm);
514
515         if (!mm)
516                 return;
517
518 #ifdef DEBUG
519         printk("Exit_mmap:\n");
520 #endif
521
522         while((tmp = mm->context.tblock.next)) {
523                 if (tmp->rblock) {
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);
529                                 }
530                                 realalloc -= kobjsize(tmp->rblock);
531                                 askedalloc -= sizeof(struct mm_rblock_struct);
532                                 kfree(tmp->rblock);
533                         }
534                         tmp->rblock = 0;
535                 }
536                 mm->context.tblock.next = tmp->next;
537                 realalloc -= kobjsize(tmp);
538                 askedalloc -= sizeof(struct mm_tblock_struct);
539                 kfree(tmp);
540         }
541
542 #ifdef DEBUG
543         show_process_blocks();
544 #endif    
545 }
546
547 asmlinkage long sys_munmap(unsigned long addr, size_t len)
548 {
549         int ret;
550         struct mm_struct *mm = current->mm;
551
552         down_write(&mm->mmap_sem);
553         ret = do_munmap(mm, addr, len);
554         up_write(&mm->mmap_sem);
555         return ret;
556 }
557
558 unsigned long do_brk(unsigned long addr, unsigned long len)
559 {
560         return -ENOMEM;
561 }
562
563 struct vm_area_struct * find_vma(struct mm_struct * mm, unsigned long addr)
564 {
565         return NULL;
566 }
567
568 struct page * follow_page(struct mm_struct *mm, unsigned long addr, int write)
569 {
570         return NULL;
571 }
572
573 struct vm_area_struct *find_extend_vma(struct mm_struct *mm, unsigned long addr)
574 {
575         return NULL;
576 }
577
578 int remap_pfn_range(struct vm_area_struct *vma, unsigned long from,
579                 unsigned long to, unsigned long size, pgprot_t prot)
580 {
581         return -EPERM;
582 }
583
584 unsigned long get_unmapped_area(struct file *file, unsigned long addr,
585         unsigned long len, unsigned long pgoff, unsigned long flags)
586 {
587         return -ENOMEM;
588 }
589
590 void swap_unplug_io_fn(struct backing_dev_info *bdi, struct page *page)
591 {
592 }
593
594 unsigned long arch_get_unmapped_area(struct file *file, unsigned long addr,
595         unsigned long len, unsigned long pgoff, unsigned long flags)
596 {
597         return -ENOMEM;
598 }
599
600 void arch_unmap_area(struct vm_area_struct *area)
601 {
602 }
603