1
2
3
4
5
6
7
8
9
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
23#include <asm/uaccess.h>
24#include <asm/tlb.h>
25#include <asm/tlbflush.h>
26
27void *high_memory;
28struct page *mem_map;
29unsigned long max_mapnr;
30unsigned long num_physpages;
31unsigned long askedalloc, realalloc;
32atomic_t vm_committed_space = ATOMIC_INIT(0);
33int sysctl_overcommit_memory = OVERCOMMIT_GUESS;
34int sysctl_overcommit_ratio = 50;
35
36int sysctl_max_map_count = DEFAULT_MAX_MAP_COUNT;
37EXPORT_SYMBOL(sysctl_max_map_count);
38
39
40
41
42
43
44
45
46
47int vmtruncate(struct inode *inode, loff_t offset)
48{
49 struct address_space *mapping = inode->i_mapping;
50 unsigned long limit;
51
52 if (inode->i_size < offset)
53 goto do_expand;
54 i_size_write(inode, offset);
55
56 truncate_inode_pages(mapping, offset);
57 goto out_truncate;
58
59do_expand:
60 limit = current->rlim[RLIMIT_FSIZE].rlim_cur;
61 if (limit != RLIM_INFINITY && offset > limit)
62 goto out_sig;
63 if (offset > inode->i_sb->s_maxbytes)
64 goto out;
65 i_size_write(inode, offset);
66
67out_truncate:
68 if (inode->i_op && inode->i_op->truncate)
69 inode->i_op->truncate(inode);
70 return 0;
71out_sig:
72 send_sig(SIGXFSZ, current, 0);
73out:
74 return -EFBIG;
75}
76
77
78
79
80
81
82
83unsigned int kobjsize(const void *objp)
84{
85 struct page *page;
86
87 if (!objp || !((page = virt_to_page(objp))))
88 return 0;
89
90 if (PageSlab(page))
91 return ksize(objp);
92
93 BUG_ON(page->index < 0);
94 BUG_ON(page->index >= MAX_ORDER);
95
96 return (PAGE_SIZE << page->index);
97}
98
99
100
101
102int 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)
105{
106 int i;
107 static struct vm_area_struct dummy_vma;
108
109 for (i = 0; i < len; i++) {
110 if (pages) {
111 pages[i] = virt_to_page(start);
112 if (pages[i])
113 page_cache_get(pages[i]);
114 }
115 if (vmas)
116 vmas[i] = &dummy_vma;
117 start += PAGE_SIZE;
118 }
119 return(i);
120}
121
122rwlock_t vmlist_lock = RW_LOCK_UNLOCKED;
123struct vm_struct *vmlist;
124
125void vfree(void *addr)
126{
127 kfree(addr);
128}
129
130void *__vmalloc(unsigned long size, int gfp_mask, pgprot_t prot)
131{
132
133
134
135 return kmalloc(size, gfp_mask & ~__GFP_HIGHMEM);
136}
137
138struct page * vmalloc_to_page(void *addr)
139{
140 return virt_to_page(addr);
141}
142
143long vread(char *buf, char *addr, unsigned long count)
144{
145 memcpy(buf, addr, count);
146 return count;
147}
148
149long vwrite(char *buf, char *addr, unsigned long count)
150{
151
152 if ((unsigned long) addr + count < count)
153 count = -(unsigned long) addr;
154
155 memcpy(addr, buf, count);
156 return(count);
157}
158
159
160
161
162
163
164
165
166
167
168
169
170void *vmalloc(unsigned long size)
171{
172 return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL);
173}
174
175
176
177
178
179
180
181
182
183void *vmalloc_32(unsigned long size)
184{
185 return __vmalloc(size, GFP_KERNEL, PAGE_KERNEL);
186}
187
188void *vmap(struct page **pages, unsigned int count, unsigned long flags, pgprot_t prot)
189{
190 BUG();
191 return NULL;
192}
193
194void vunmap(void *addr)
195{
196 BUG();
197}
198
199
200
201
202
203
204
205
206asmlinkage unsigned long sys_brk(unsigned long brk)
207{
208 struct mm_struct *mm = current->mm;
209
210 if (brk < mm->end_code || brk < mm->start_brk || brk > mm->context.end_brk)
211 return mm->brk;
212
213 if (mm->brk == brk)
214 return mm->brk;
215
216
217
218
219 if (brk <= mm->brk) {
220 mm->brk = brk;
221 return brk;
222 }
223
224
225
226
227 return mm->brk = brk;
228}
229
230unsigned long do_brk_locked(unsigned long addr, unsigned long len)
231{
232 return -ENOMEM;
233}
234
235
236
237
238
239
240static inline unsigned long calc_vm_flags(unsigned long prot, unsigned long flags)
241{
242#define _trans(x,bit1,bit2) \
243((bit1==bit2)?(x&bit1):(x&bit1)?bit2:0)
244
245 unsigned long prot_bits, flag_bits;
246 prot_bits =
247 _trans(prot, PROT_READ, VM_READ) |
248 _trans(prot, PROT_WRITE, VM_WRITE) |
249 _trans(prot, PROT_EXEC, VM_EXEC);
250 flag_bits =
251 _trans(flags, MAP_GROWSDOWN, VM_GROWSDOWN) |
252 _trans(flags, MAP_DENYWRITE, VM_DENYWRITE) |
253 _trans(flags, MAP_EXECUTABLE, VM_EXECUTABLE);
254 return prot_bits | flag_bits;
255#undef _trans
256}
257
258#ifdef DEBUG
259static void show_process_blocks(void)
260{
261 struct mm_tblock_struct *tblock;
262
263 printk("Process blocks %d:", current->pid);
264
265 for (tblock = ¤t->mm->context.tblock; tblock; tblock = tblock->next) {
266 printk(" %p: %p", tblock, tblock->rblock);
267 if (tblock->rblock)
268 printk(" (%d @%p #%d)", kobjsize(tblock->rblock->kblock), tblock->rblock->kblock, tblock->rblock->refcount);
269 printk(tblock->next ? " ->" : ".\n");
270 }
271}
272#endif
273
274unsigned long do_mmap_pgoff(
275 struct file * file,
276 unsigned long addr,
277 unsigned long len,
278 unsigned long prot,
279 unsigned long flags,
280 unsigned long pgoff)
281{
282 void * result;
283 struct mm_tblock_struct * tblock;
284 unsigned int vm_flags;
285
286
287
288
289 if ((flags & MAP_SHARED) && (prot & PROT_WRITE) && (file)) {
290 printk("MAP_SHARED not supported (cannot write mappings to disk)\n");
291 return -EINVAL;
292 }
293
294 if ((prot & PROT_WRITE) && (flags & MAP_PRIVATE)) {
295 printk("Private writable mappings not supported\n");
296 return -EINVAL;
297 }
298
299
300
301
302 if (file && (!file->f_op || !file->f_op->mmap))
303 return -ENODEV;
304
305 if (PAGE_ALIGN(len) == 0)
306 return addr;
307
308 if (len > TASK_SIZE)
309 return -EINVAL;
310
311
312 if ((pgoff + (len >> PAGE_SHIFT)) < pgoff)
313 return -EINVAL;
314
315
316
317
318
319 vm_flags = calc_vm_flags(prot,flags) | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
320
321
322
323
324
325 if (file) {
326 struct vm_area_struct vma;
327 int error;
328
329 if (!file->f_op)
330 return -ENODEV;
331
332 vma.vm_start = addr;
333 vma.vm_end = addr + len;
334 vma.vm_flags = vm_flags;
335 vma.vm_pgoff = pgoff;
336
337#ifdef MAGIC_ROM_PTR
338
339
340 if (file->f_op->romptr && !(prot & PROT_WRITE)) {
341 error = file->f_op->romptr(file, &vma);
342#ifdef DEBUG
343 printk("romptr mmap returned %d, start 0x%.8x\n", error,
344 vma.vm_start);
345#endif
346 if (!error)
347 return vma.vm_start;
348 else if (error != -ENOSYS)
349 return error;
350 } else
351#endif
352
353
354
355 if (file->f_op->mmap) {
356 error = file->f_op->mmap(file, &vma);
357
358#ifdef DEBUG
359 printk("f_op->mmap() returned %d/%lx\n", error, vma.vm_start);
360#endif
361 if (file->f_op->mmap == generic_file_noatime_mmap &&
362 !(file->f_flags & O_NOATIME))
363 update_atime(file->f_dentry->d_inode);
364
365 if (!error)
366 return vma.vm_start;
367 else if (error != -ENOSYS)
368 return error;
369 } else
370 return -ENODEV;
371
372
373
374 }
375
376 tblock = (struct mm_tblock_struct *)
377 kmalloc(sizeof(struct mm_tblock_struct), GFP_KERNEL);
378 if (!tblock) {
379 printk("Allocation of tblock for %lu byte allocation from process %d failed\n", len, current->pid);
380 show_free_areas();
381 return -ENOMEM;
382 }
383
384 tblock->rblock = (struct mm_rblock_struct *)
385 kmalloc(sizeof(struct mm_rblock_struct), GFP_KERNEL);
386
387 if (!tblock->rblock) {
388 printk("Allocation of rblock for %lu byte allocation from process %d failed\n", len, current->pid);
389 show_free_areas();
390 kfree(tblock);
391 return -ENOMEM;
392 }
393
394 result = kmalloc(len, GFP_KERNEL);
395 if (!result) {
396 printk("Allocation of length %lu from process %d failed\n", len,
397 current->pid);
398 show_free_areas();
399 kfree(tblock->rblock);
400 kfree(tblock);
401 return -ENOMEM;
402 }
403
404 tblock->rblock->refcount = 1;
405 tblock->rblock->kblock = result;
406 tblock->rblock->size = len;
407
408 realalloc += kobjsize(result);
409 askedalloc += len;
410
411#ifdef WARN_ON_SLACK
412 if ((len+WARN_ON_SLACK) <= kobjsize(result))
413 printk("Allocation of %lu bytes from process %d has %lu bytes of slack\n", len, current->pid, kobjsize(result)-len);
414#endif
415
416 if (file) {
417 int error;
418 mm_segment_t old_fs = get_fs();
419 set_fs(KERNEL_DS);
420 error = file->f_op->read(file, (char *) result, len, &file->f_pos);
421 set_fs(old_fs);
422 if (error < 0) {
423 kfree(result);
424 kfree(tblock->rblock);
425 kfree(tblock);
426 return error;
427 }
428 if (error < len)
429 memset(result+error, '\0', len-error);
430 } else {
431 memset(result, '\0', len);
432 }
433
434 realalloc += kobjsize(tblock);
435 askedalloc += sizeof(struct mm_tblock_struct);
436
437 realalloc += kobjsize(tblock->rblock);
438 askedalloc += sizeof(struct mm_rblock_struct);
439
440 tblock->next = current->mm->context.tblock.next;
441 current->mm->context.tblock.next = tblock;
442
443#ifdef DEBUG
444 printk("do_mmap:\n");
445 show_process_blocks();
446#endif
447
448 return (unsigned long)result;
449}
450
451int do_munmap(struct mm_struct * mm, unsigned long addr, size_t len)
452{
453 struct mm_tblock_struct * tblock, *tmp;
454
455#ifdef MAGIC_ROM_PTR
456
457
458
459
460 if (is_in_rom(addr))
461 return 0;
462#endif
463
464#ifdef DEBUG
465 printk("do_munmap:\n");
466#endif
467
468 tmp = &mm->context.tblock;
469 while ((tblock=tmp->next) && tblock->rblock &&
470 tblock->rblock->kblock != (void*)addr)
471 tmp = tblock;
472
473 if (!tblock) {
474 printk("munmap of non-mmaped memory by process %d (%s): %p\n",
475 current->pid, current->comm, (void*)addr);
476 return -EINVAL;
477 }
478 if (tblock->rblock) {
479 if (!--tblock->rblock->refcount) {
480 if (tblock->rblock->kblock) {
481 realalloc -= kobjsize(tblock->rblock->kblock);
482 askedalloc -= tblock->rblock->size;
483 kfree(tblock->rblock->kblock);
484 }
485
486 realalloc -= kobjsize(tblock->rblock);
487 askedalloc -= sizeof(struct mm_rblock_struct);
488 kfree(tblock->rblock);
489 }
490 }
491 tmp->next = tblock->next;
492 realalloc -= kobjsize(tblock);
493 askedalloc -= sizeof(struct mm_tblock_struct);
494 kfree(tblock);
495
496#ifdef DEBUG
497 show_process_blocks();
498#endif
499
500 return 0;
501}
502
503
504void exit_mmap(struct mm_struct * mm)
505{
506 struct mm_tblock_struct *tmp;
507
508 if (!mm)
509 return;
510
511#ifdef DEBUG
512 printk("Exit_mmap:\n");
513#endif
514
515 while((tmp = mm->context.tblock.next)) {
516 if (tmp->rblock) {
517 if (!--tmp->rblock->refcount) {
518 if (tmp->rblock->kblock) {
519 realalloc -= kobjsize(tmp->rblock->kblock);
520 askedalloc -= tmp->rblock->size;
521 kfree(tmp->rblock->kblock);
522 }
523 realalloc -= kobjsize(tmp->rblock);
524 askedalloc -= sizeof(struct mm_rblock_struct);
525 kfree(tmp->rblock);
526 }
527 tmp->rblock = 0;
528 }
529 mm->context.tblock.next = tmp->next;
530 realalloc -= kobjsize(tmp);
531 askedalloc -= sizeof(struct mm_tblock_struct);
532 kfree(tmp);
533 }
534
535#ifdef DEBUG
536 show_process_blocks();
537#endif
538}
539
540asmlinkage long sys_munmap(unsigned long addr, size_t len)
541{
542 int ret;
543 struct mm_struct *mm = current->mm;
544
545 down_write(&mm->mmap_sem);
546 ret = do_munmap(mm, addr, len);
547 up_write(&mm->mmap_sem);
548 return ret;
549}
550
551unsigned long do_brk(unsigned long addr, unsigned long len)
552{
553 return -ENOMEM;
554}
555
556struct vm_area_struct * find_vma(struct mm_struct * mm, unsigned long addr)
557{
558 return NULL;
559}
560
561struct page * follow_page(struct mm_struct *mm, unsigned long addr, int write)
562{
563 return NULL;
564}
565
566struct vm_area_struct *find_extend_vma(struct mm_struct *mm, unsigned long addr)
567{
568 return NULL;
569}
570
571int remap_page_range(struct vm_area_struct *vma, unsigned long from,
572 unsigned long to, unsigned long size, pgprot_t prot)
573{
574 return -EPERM;
575}
576
577unsigned long get_unmapped_area(struct file *file, unsigned long addr,
578 unsigned long len, unsigned long pgoff, unsigned long flags)
579{
580 return -ENOMEM;
581}
582
583void swap_unplug_io_fn(struct backing_dev_info *bdi, struct page *page)
584{
585}
586