nommu.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414
  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. * See Documentation/nommu-mmap.txt
  8. *
  9. * Copyright (c) 2004-2005 David Howells <dhowells@redhat.com>
  10. * Copyright (c) 2000-2003 David McCullough <davidm@snapgear.com>
  11. * Copyright (c) 2000-2001 D Jeff Dionne <jeff@uClinux.org>
  12. * Copyright (c) 2002 Greg Ungerer <gerg@snapgear.com>
  13. */
  14. #include <linux/module.h>
  15. #include <linux/mm.h>
  16. #include <linux/mman.h>
  17. #include <linux/swap.h>
  18. #include <linux/file.h>
  19. #include <linux/highmem.h>
  20. #include <linux/pagemap.h>
  21. #include <linux/slab.h>
  22. #include <linux/vmalloc.h>
  23. #include <linux/ptrace.h>
  24. #include <linux/blkdev.h>
  25. #include <linux/backing-dev.h>
  26. #include <linux/mount.h>
  27. #include <linux/personality.h>
  28. #include <linux/security.h>
  29. #include <linux/syscalls.h>
  30. #include <asm/uaccess.h>
  31. #include <asm/tlb.h>
  32. #include <asm/tlbflush.h>
  33. void *high_memory;
  34. struct page *mem_map;
  35. unsigned long max_mapnr;
  36. unsigned long num_physpages;
  37. unsigned long askedalloc, realalloc;
  38. atomic_t vm_committed_space = ATOMIC_INIT(0);
  39. int sysctl_overcommit_memory = OVERCOMMIT_GUESS; /* heuristic overcommit */
  40. int sysctl_overcommit_ratio = 50; /* default is 50% */
  41. int sysctl_max_map_count = DEFAULT_MAX_MAP_COUNT;
  42. int heap_stack_gap = 0;
  43. EXPORT_SYMBOL(mem_map);
  44. EXPORT_SYMBOL(num_physpages);
  45. /* list of shareable VMAs */
  46. struct rb_root nommu_vma_tree = RB_ROOT;
  47. DECLARE_RWSEM(nommu_vma_sem);
  48. struct vm_operations_struct generic_file_vm_ops = {
  49. };
  50. /*
  51. * Handle all mappings that got truncated by a "truncate()"
  52. * system call.
  53. *
  54. * NOTE! We have to be ready to update the memory sharing
  55. * between the file and the memory map for a potential last
  56. * incomplete page. Ugly, but necessary.
  57. */
  58. int vmtruncate(struct inode *inode, loff_t offset)
  59. {
  60. struct address_space *mapping = inode->i_mapping;
  61. unsigned long limit;
  62. if (inode->i_size < offset)
  63. goto do_expand;
  64. i_size_write(inode, offset);
  65. truncate_inode_pages(mapping, offset);
  66. goto out_truncate;
  67. do_expand:
  68. limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
  69. if (limit != RLIM_INFINITY && offset > limit)
  70. goto out_sig;
  71. if (offset > inode->i_sb->s_maxbytes)
  72. goto out;
  73. i_size_write(inode, offset);
  74. out_truncate:
  75. if (inode->i_op && inode->i_op->truncate)
  76. inode->i_op->truncate(inode);
  77. return 0;
  78. out_sig:
  79. send_sig(SIGXFSZ, current, 0);
  80. out:
  81. return -EFBIG;
  82. }
  83. EXPORT_SYMBOL(vmtruncate);
  84. /*
  85. * Return the total memory allocated for this pointer, not
  86. * just what the caller asked for.
  87. *
  88. * Doesn't have to be accurate, i.e. may have races.
  89. */
  90. unsigned int kobjsize(const void *objp)
  91. {
  92. struct page *page;
  93. if (!objp || !((page = virt_to_page(objp))))
  94. return 0;
  95. if (PageSlab(page))
  96. return ksize(objp);
  97. BUG_ON(page->index < 0);
  98. BUG_ON(page->index >= MAX_ORDER);
  99. return (PAGE_SIZE << page->index);
  100. }
  101. /*
  102. * get a list of pages in an address range belonging to the specified process
  103. * and indicate the VMA that covers each page
  104. * - this is potentially dodgy as we may end incrementing the page count of a
  105. * slab page or a secondary page from a compound page
  106. * - don't permit access to VMAs that don't support it, such as I/O mappings
  107. */
  108. int get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
  109. unsigned long start, int len, int write, int force,
  110. struct page **pages, struct vm_area_struct **vmas)
  111. {
  112. struct vm_area_struct *vma;
  113. unsigned long vm_flags;
  114. int i;
  115. /* calculate required read or write permissions.
  116. * - if 'force' is set, we only require the "MAY" flags.
  117. */
  118. vm_flags = write ? (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
  119. vm_flags &= force ? (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE);
  120. for (i = 0; i < len; i++) {
  121. vma = find_vma(mm, start);
  122. if (!vma)
  123. goto finish_or_fault;
  124. /* protect what we can, including chardevs */
  125. if (vma->vm_flags & (VM_IO | VM_PFNMAP) ||
  126. !(vm_flags & vma->vm_flags))
  127. goto finish_or_fault;
  128. if (pages) {
  129. pages[i] = virt_to_page(start);
  130. if (pages[i])
  131. page_cache_get(pages[i]);
  132. }
  133. if (vmas)
  134. vmas[i] = vma;
  135. start += PAGE_SIZE;
  136. }
  137. return i;
  138. finish_or_fault:
  139. return i ? : -EFAULT;
  140. }
  141. EXPORT_SYMBOL(get_user_pages);
  142. DEFINE_RWLOCK(vmlist_lock);
  143. struct vm_struct *vmlist;
  144. void vfree(void *addr)
  145. {
  146. kfree(addr);
  147. }
  148. EXPORT_SYMBOL(vfree);
  149. void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot)
  150. {
  151. /*
  152. * You can't specify __GFP_HIGHMEM with kmalloc() since kmalloc()
  153. * returns only a logical address.
  154. */
  155. return kmalloc(size, (gfp_mask | __GFP_COMP) & ~__GFP_HIGHMEM);
  156. }
  157. EXPORT_SYMBOL(__vmalloc);
  158. struct page * vmalloc_to_page(void *addr)
  159. {
  160. return virt_to_page(addr);
  161. }
  162. EXPORT_SYMBOL(vmalloc_to_page);
  163. unsigned long vmalloc_to_pfn(void *addr)
  164. {
  165. return page_to_pfn(virt_to_page(addr));
  166. }
  167. EXPORT_SYMBOL(vmalloc_to_pfn);
  168. long vread(char *buf, char *addr, unsigned long count)
  169. {
  170. memcpy(buf, addr, count);
  171. return count;
  172. }
  173. long vwrite(char *buf, char *addr, unsigned long count)
  174. {
  175. /* Don't allow overflow */
  176. if ((unsigned long) addr + count < count)
  177. count = -(unsigned long) addr;
  178. memcpy(addr, buf, count);
  179. return(count);
  180. }
  181. /*
  182. * vmalloc - allocate virtually continguos memory
  183. *
  184. * @size: allocation size
  185. *
  186. * Allocate enough pages to cover @size from the page level
  187. * allocator and map them into continguos kernel virtual space.
  188. *
  189. * For tight control over page level allocator and protection flags
  190. * use __vmalloc() instead.
  191. */
  192. void *vmalloc(unsigned long size)
  193. {
  194. return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL);
  195. }
  196. EXPORT_SYMBOL(vmalloc);
  197. void *vmalloc_node(unsigned long size, int node)
  198. {
  199. return vmalloc(size);
  200. }
  201. EXPORT_SYMBOL(vmalloc_node);
  202. /**
  203. * vmalloc_32 - allocate virtually contiguous memory (32bit addressable)
  204. * @size: allocation size
  205. *
  206. * Allocate enough 32bit PA addressable pages to cover @size from the
  207. * page level allocator and map them into continguos kernel virtual space.
  208. */
  209. void *vmalloc_32(unsigned long size)
  210. {
  211. return __vmalloc(size, GFP_KERNEL, PAGE_KERNEL);
  212. }
  213. EXPORT_SYMBOL(vmalloc_32);
  214. /**
  215. * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
  216. * @size: allocation size
  217. *
  218. * The resulting memory area is 32bit addressable and zeroed so it can be
  219. * mapped to userspace without leaking data.
  220. */
  221. void *vmalloc_32_user(unsigned long size)
  222. {
  223. return __vmalloc(size, GFP_KERNEL | __GFP_ZERO, PAGE_KERNEL);
  224. }
  225. EXPORT_SYMBOL(vmalloc_32_user);
  226. void *vmap(struct page **pages, unsigned int count, unsigned long flags, pgprot_t prot)
  227. {
  228. BUG();
  229. return NULL;
  230. }
  231. EXPORT_SYMBOL(vmap);
  232. void vunmap(void *addr)
  233. {
  234. BUG();
  235. }
  236. EXPORT_SYMBOL(vunmap);
  237. /*
  238. * Implement a stub for vmalloc_sync_all() if the architecture chose not to
  239. * have one.
  240. */
  241. void __attribute__((weak)) vmalloc_sync_all(void)
  242. {
  243. }
  244. int vm_insert_page(struct vm_area_struct *vma, unsigned long addr,
  245. struct page *page)
  246. {
  247. return -EINVAL;
  248. }
  249. EXPORT_SYMBOL(vm_insert_page);
  250. /*
  251. * sys_brk() for the most part doesn't need the global kernel
  252. * lock, except when an application is doing something nasty
  253. * like trying to un-brk an area that has already been mapped
  254. * to a regular file. in this case, the unmapping will need
  255. * to invoke file system routines that need the global lock.
  256. */
  257. asmlinkage unsigned long sys_brk(unsigned long brk)
  258. {
  259. struct mm_struct *mm = current->mm;
  260. if (brk < mm->start_brk || brk > mm->context.end_brk)
  261. return mm->brk;
  262. if (mm->brk == brk)
  263. return mm->brk;
  264. /*
  265. * Always allow shrinking brk
  266. */
  267. if (brk <= mm->brk) {
  268. mm->brk = brk;
  269. return brk;
  270. }
  271. /*
  272. * Ok, looks good - let it rip.
  273. */
  274. return mm->brk = brk;
  275. }
  276. #ifdef DEBUG
  277. static void show_process_blocks(void)
  278. {
  279. struct vm_list_struct *vml;
  280. printk("Process blocks %d:", current->pid);
  281. for (vml = &current->mm->context.vmlist; vml; vml = vml->next) {
  282. printk(" %p: %p", vml, vml->vma);
  283. if (vml->vma)
  284. printk(" (%d @%lx #%d)",
  285. kobjsize((void *) vml->vma->vm_start),
  286. vml->vma->vm_start,
  287. atomic_read(&vml->vma->vm_usage));
  288. printk(vml->next ? " ->" : ".\n");
  289. }
  290. }
  291. #endif /* DEBUG */
  292. /*
  293. * add a VMA into a process's mm_struct in the appropriate place in the list
  294. * - should be called with mm->mmap_sem held writelocked
  295. */
  296. static void add_vma_to_mm(struct mm_struct *mm, struct vm_list_struct *vml)
  297. {
  298. struct vm_list_struct **ppv;
  299. for (ppv = &current->mm->context.vmlist; *ppv; ppv = &(*ppv)->next)
  300. if ((*ppv)->vma->vm_start > vml->vma->vm_start)
  301. break;
  302. vml->next = *ppv;
  303. *ppv = vml;
  304. }
  305. /*
  306. * look up the first VMA in which addr resides, NULL if none
  307. * - should be called with mm->mmap_sem at least held readlocked
  308. */
  309. struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)
  310. {
  311. struct vm_list_struct *loop, *vml;
  312. /* search the vm_start ordered list */
  313. vml = NULL;
  314. for (loop = mm->context.vmlist; loop; loop = loop->next) {
  315. if (loop->vma->vm_start > addr)
  316. break;
  317. vml = loop;
  318. }
  319. if (vml && vml->vma->vm_end > addr)
  320. return vml->vma;
  321. return NULL;
  322. }
  323. EXPORT_SYMBOL(find_vma);
  324. /*
  325. * find a VMA
  326. * - we don't extend stack VMAs under NOMMU conditions
  327. */
  328. struct vm_area_struct *find_extend_vma(struct mm_struct *mm, unsigned long addr)
  329. {
  330. return find_vma(mm, addr);
  331. }
  332. int expand_stack(struct vm_area_struct *vma, unsigned long address)
  333. {
  334. return -ENOMEM;
  335. }
  336. /*
  337. * look up the first VMA exactly that exactly matches addr
  338. * - should be called with mm->mmap_sem at least held readlocked
  339. */
  340. static inline struct vm_area_struct *find_vma_exact(struct mm_struct *mm,
  341. unsigned long addr)
  342. {
  343. struct vm_list_struct *vml;
  344. /* search the vm_start ordered list */
  345. for (vml = mm->context.vmlist; vml; vml = vml->next) {
  346. if (vml->vma->vm_start == addr)
  347. return vml->vma;
  348. if (vml->vma->vm_start > addr)
  349. break;
  350. }
  351. return NULL;
  352. }
  353. /*
  354. * find a VMA in the global tree
  355. */
  356. static inline struct vm_area_struct *find_nommu_vma(unsigned long start)
  357. {
  358. struct vm_area_struct *vma;
  359. struct rb_node *n = nommu_vma_tree.rb_node;
  360. while (n) {
  361. vma = rb_entry(n, struct vm_area_struct, vm_rb);
  362. if (start < vma->vm_start)
  363. n = n->rb_left;
  364. else if (start > vma->vm_start)
  365. n = n->rb_right;
  366. else
  367. return vma;
  368. }
  369. return NULL;
  370. }
  371. /*
  372. * add a VMA in the global tree
  373. */
  374. static void add_nommu_vma(struct vm_area_struct *vma)
  375. {
  376. struct vm_area_struct *pvma;
  377. struct address_space *mapping;
  378. struct rb_node **p = &nommu_vma_tree.rb_node;
  379. struct rb_node *parent = NULL;
  380. /* add the VMA to the mapping */
  381. if (vma->vm_file) {
  382. mapping = vma->vm_file->f_mapping;
  383. flush_dcache_mmap_lock(mapping);
  384. vma_prio_tree_insert(vma, &mapping->i_mmap);
  385. flush_dcache_mmap_unlock(mapping);
  386. }
  387. /* add the VMA to the master list */
  388. while (*p) {
  389. parent = *p;
  390. pvma = rb_entry(parent, struct vm_area_struct, vm_rb);
  391. if (vma->vm_start < pvma->vm_start) {
  392. p = &(*p)->rb_left;
  393. }
  394. else if (vma->vm_start > pvma->vm_start) {
  395. p = &(*p)->rb_right;
  396. }
  397. else {
  398. /* mappings are at the same address - this can only
  399. * happen for shared-mem chardevs and shared file
  400. * mappings backed by ramfs/tmpfs */
  401. BUG_ON(!(pvma->vm_flags & VM_SHARED));
  402. if (vma < pvma)
  403. p = &(*p)->rb_left;
  404. else if (vma > pvma)
  405. p = &(*p)->rb_right;
  406. else
  407. BUG();
  408. }
  409. }
  410. rb_link_node(&vma->vm_rb, parent, p);
  411. rb_insert_color(&vma->vm_rb, &nommu_vma_tree);
  412. }
  413. /*
  414. * delete a VMA from the global list
  415. */
  416. static void delete_nommu_vma(struct vm_area_struct *vma)
  417. {
  418. struct address_space *mapping;
  419. /* remove the VMA from the mapping */
  420. if (vma->vm_file) {
  421. mapping = vma->vm_file->f_mapping;
  422. flush_dcache_mmap_lock(mapping);
  423. vma_prio_tree_remove(vma, &mapping->i_mmap);
  424. flush_dcache_mmap_unlock(mapping);
  425. }
  426. /* remove from the master list */
  427. rb_erase(&vma->vm_rb, &nommu_vma_tree);
  428. }
  429. /*
  430. * determine whether a mapping should be permitted and, if so, what sort of
  431. * mapping we're capable of supporting
  432. */
  433. static int validate_mmap_request(struct file *file,
  434. unsigned long addr,
  435. unsigned long len,
  436. unsigned long prot,
  437. unsigned long flags,
  438. unsigned long pgoff,
  439. unsigned long *_capabilities)
  440. {
  441. unsigned long capabilities;
  442. unsigned long reqprot = prot;
  443. int ret;
  444. /* do the simple checks first */
  445. if (flags & MAP_FIXED || addr) {
  446. printk(KERN_DEBUG
  447. "%d: Can't do fixed-address/overlay mmap of RAM\n",
  448. current->pid);
  449. return -EINVAL;
  450. }
  451. if ((flags & MAP_TYPE) != MAP_PRIVATE &&
  452. (flags & MAP_TYPE) != MAP_SHARED)
  453. return -EINVAL;
  454. if (!len)
  455. return -EINVAL;
  456. /* Careful about overflows.. */
  457. len = PAGE_ALIGN(len);
  458. if (!len || len > TASK_SIZE)
  459. return -ENOMEM;
  460. /* offset overflow? */
  461. if ((pgoff + (len >> PAGE_SHIFT)) < pgoff)
  462. return -EOVERFLOW;
  463. if (file) {
  464. /* validate file mapping requests */
  465. struct address_space *mapping;
  466. /* files must support mmap */
  467. if (!file->f_op || !file->f_op->mmap)
  468. return -ENODEV;
  469. /* work out if what we've got could possibly be shared
  470. * - we support chardevs that provide their own "memory"
  471. * - we support files/blockdevs that are memory backed
  472. */
  473. mapping = file->f_mapping;
  474. if (!mapping)
  475. mapping = file->f_path.dentry->d_inode->i_mapping;
  476. capabilities = 0;
  477. if (mapping && mapping->backing_dev_info)
  478. capabilities = mapping->backing_dev_info->capabilities;
  479. if (!capabilities) {
  480. /* no explicit capabilities set, so assume some
  481. * defaults */
  482. switch (file->f_path.dentry->d_inode->i_mode & S_IFMT) {
  483. case S_IFREG:
  484. case S_IFBLK:
  485. capabilities = BDI_CAP_MAP_COPY;
  486. break;
  487. case S_IFCHR:
  488. capabilities =
  489. BDI_CAP_MAP_DIRECT |
  490. BDI_CAP_READ_MAP |
  491. BDI_CAP_WRITE_MAP;
  492. break;
  493. default:
  494. return -EINVAL;
  495. }
  496. }
  497. /* eliminate any capabilities that we can't support on this
  498. * device */
  499. if (!file->f_op->get_unmapped_area)
  500. capabilities &= ~BDI_CAP_MAP_DIRECT;
  501. if (!file->f_op->read)
  502. capabilities &= ~BDI_CAP_MAP_COPY;
  503. if (flags & MAP_SHARED) {
  504. /* do checks for writing, appending and locking */
  505. if ((prot & PROT_WRITE) &&
  506. !(file->f_mode & FMODE_WRITE))
  507. return -EACCES;
  508. if (IS_APPEND(file->f_path.dentry->d_inode) &&
  509. (file->f_mode & FMODE_WRITE))
  510. return -EACCES;
  511. if (locks_verify_locked(file->f_path.dentry->d_inode))
  512. return -EAGAIN;
  513. if (!(capabilities & BDI_CAP_MAP_DIRECT))
  514. return -ENODEV;
  515. if (((prot & PROT_READ) && !(capabilities & BDI_CAP_READ_MAP)) ||
  516. ((prot & PROT_WRITE) && !(capabilities & BDI_CAP_WRITE_MAP)) ||
  517. ((prot & PROT_EXEC) && !(capabilities & BDI_CAP_EXEC_MAP))
  518. ) {
  519. printk("MAP_SHARED not completely supported on !MMU\n");
  520. return -EINVAL;
  521. }
  522. /* we mustn't privatise shared mappings */
  523. capabilities &= ~BDI_CAP_MAP_COPY;
  524. }
  525. else {
  526. /* we're going to read the file into private memory we
  527. * allocate */
  528. if (!(capabilities & BDI_CAP_MAP_COPY))
  529. return -ENODEV;
  530. /* we don't permit a private writable mapping to be
  531. * shared with the backing device */
  532. if (prot & PROT_WRITE)
  533. capabilities &= ~BDI_CAP_MAP_DIRECT;
  534. }
  535. /* handle executable mappings and implied executable
  536. * mappings */
  537. if (file->f_path.mnt->mnt_flags & MNT_NOEXEC) {
  538. if (prot & PROT_EXEC)
  539. return -EPERM;
  540. }
  541. else if ((prot & PROT_READ) && !(prot & PROT_EXEC)) {
  542. /* handle implication of PROT_EXEC by PROT_READ */
  543. if (current->personality & READ_IMPLIES_EXEC) {
  544. if (capabilities & BDI_CAP_EXEC_MAP)
  545. prot |= PROT_EXEC;
  546. }
  547. }
  548. else if ((prot & PROT_READ) &&
  549. (prot & PROT_EXEC) &&
  550. !(capabilities & BDI_CAP_EXEC_MAP)
  551. ) {
  552. /* backing file is not executable, try to copy */
  553. capabilities &= ~BDI_CAP_MAP_DIRECT;
  554. }
  555. }
  556. else {
  557. /* anonymous mappings are always memory backed and can be
  558. * privately mapped
  559. */
  560. capabilities = BDI_CAP_MAP_COPY;
  561. /* handle PROT_EXEC implication by PROT_READ */
  562. if ((prot & PROT_READ) &&
  563. (current->personality & READ_IMPLIES_EXEC))
  564. prot |= PROT_EXEC;
  565. }
  566. /* allow the security API to have its say */
  567. ret = security_file_mmap(file, reqprot, prot, flags, addr, 0);
  568. if (ret < 0)
  569. return ret;
  570. /* looks okay */
  571. *_capabilities = capabilities;
  572. return 0;
  573. }
  574. /*
  575. * we've determined that we can make the mapping, now translate what we
  576. * now know into VMA flags
  577. */
  578. static unsigned long determine_vm_flags(struct file *file,
  579. unsigned long prot,
  580. unsigned long flags,
  581. unsigned long capabilities)
  582. {
  583. unsigned long vm_flags;
  584. vm_flags = calc_vm_prot_bits(prot) | calc_vm_flag_bits(flags);
  585. vm_flags |= VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
  586. /* vm_flags |= mm->def_flags; */
  587. if (!(capabilities & BDI_CAP_MAP_DIRECT)) {
  588. /* attempt to share read-only copies of mapped file chunks */
  589. if (file && !(prot & PROT_WRITE))
  590. vm_flags |= VM_MAYSHARE;
  591. }
  592. else {
  593. /* overlay a shareable mapping on the backing device or inode
  594. * if possible - used for chardevs, ramfs/tmpfs/shmfs and
  595. * romfs/cramfs */
  596. if (flags & MAP_SHARED)
  597. vm_flags |= VM_MAYSHARE | VM_SHARED;
  598. else if ((((vm_flags & capabilities) ^ vm_flags) & BDI_CAP_VMFLAGS) == 0)
  599. vm_flags |= VM_MAYSHARE;
  600. }
  601. /* refuse to let anyone share private mappings with this process if
  602. * it's being traced - otherwise breakpoints set in it may interfere
  603. * with another untraced process
  604. */
  605. if ((flags & MAP_PRIVATE) && (current->ptrace & PT_PTRACED))
  606. vm_flags &= ~VM_MAYSHARE;
  607. return vm_flags;
  608. }
  609. /*
  610. * set up a shared mapping on a file
  611. */
  612. static int do_mmap_shared_file(struct vm_area_struct *vma, unsigned long len)
  613. {
  614. int ret;
  615. ret = vma->vm_file->f_op->mmap(vma->vm_file, vma);
  616. if (ret != -ENOSYS)
  617. return ret;
  618. /* getting an ENOSYS error indicates that direct mmap isn't
  619. * possible (as opposed to tried but failed) so we'll fall
  620. * through to making a private copy of the data and mapping
  621. * that if we can */
  622. return -ENODEV;
  623. }
  624. /*
  625. * set up a private mapping or an anonymous shared mapping
  626. */
  627. static int do_mmap_private(struct vm_area_struct *vma, unsigned long len)
  628. {
  629. void *base;
  630. int ret;
  631. /* invoke the file's mapping function so that it can keep track of
  632. * shared mappings on devices or memory
  633. * - VM_MAYSHARE will be set if it may attempt to share
  634. */
  635. if (vma->vm_file) {
  636. ret = vma->vm_file->f_op->mmap(vma->vm_file, vma);
  637. if (ret != -ENOSYS) {
  638. /* shouldn't return success if we're not sharing */
  639. BUG_ON(ret == 0 && !(vma->vm_flags & VM_MAYSHARE));
  640. return ret; /* success or a real error */
  641. }
  642. /* getting an ENOSYS error indicates that direct mmap isn't
  643. * possible (as opposed to tried but failed) so we'll try to
  644. * make a private copy of the data and map that instead */
  645. }
  646. /* allocate some memory to hold the mapping
  647. * - note that this may not return a page-aligned address if the object
  648. * we're allocating is smaller than a page
  649. */
  650. base = kmalloc(len, GFP_KERNEL|__GFP_COMP);
  651. if (!base)
  652. goto enomem;
  653. vma->vm_start = (unsigned long) base;
  654. vma->vm_end = vma->vm_start + len;
  655. vma->vm_flags |= VM_MAPPED_COPY;
  656. #ifdef WARN_ON_SLACK
  657. if (len + WARN_ON_SLACK <= kobjsize(result))
  658. printk("Allocation of %lu bytes from process %d has %lu bytes of slack\n",
  659. len, current->pid, kobjsize(result) - len);
  660. #endif
  661. if (vma->vm_file) {
  662. /* read the contents of a file into the copy */
  663. mm_segment_t old_fs;
  664. loff_t fpos;
  665. fpos = vma->vm_pgoff;
  666. fpos <<= PAGE_SHIFT;
  667. old_fs = get_fs();
  668. set_fs(KERNEL_DS);
  669. ret = vma->vm_file->f_op->read(vma->vm_file, base, len, &fpos);
  670. set_fs(old_fs);
  671. if (ret < 0)
  672. goto error_free;
  673. /* clear the last little bit */
  674. if (ret < len)
  675. memset(base + ret, 0, len - ret);
  676. } else {
  677. /* if it's an anonymous mapping, then just clear it */
  678. memset(base, 0, len);
  679. }
  680. return 0;
  681. error_free:
  682. kfree(base);
  683. vma->vm_start = 0;
  684. return ret;
  685. enomem:
  686. printk("Allocation of length %lu from process %d failed\n",
  687. len, current->pid);
  688. show_free_areas();
  689. return -ENOMEM;
  690. }
  691. /*
  692. * handle mapping creation for uClinux
  693. */
  694. unsigned long do_mmap_pgoff(struct file *file,
  695. unsigned long addr,
  696. unsigned long len,
  697. unsigned long prot,
  698. unsigned long flags,
  699. unsigned long pgoff)
  700. {
  701. struct vm_list_struct *vml = NULL;
  702. struct vm_area_struct *vma = NULL;
  703. struct rb_node *rb;
  704. unsigned long capabilities, vm_flags;
  705. void *result;
  706. int ret;
  707. if (!(flags & MAP_FIXED))
  708. addr = round_hint_to_min(addr);
  709. /* decide whether we should attempt the mapping, and if so what sort of
  710. * mapping */
  711. ret = validate_mmap_request(file, addr, len, prot, flags, pgoff,
  712. &capabilities);
  713. if (ret < 0)
  714. return ret;
  715. /* we've determined that we can make the mapping, now translate what we
  716. * now know into VMA flags */
  717. vm_flags = determine_vm_flags(file, prot, flags, capabilities);
  718. /* we're going to need to record the mapping if it works */
  719. vml = kzalloc(sizeof(struct vm_list_struct), GFP_KERNEL);
  720. if (!vml)
  721. goto error_getting_vml;
  722. down_write(&nommu_vma_sem);
  723. /* if we want to share, we need to check for VMAs created by other
  724. * mmap() calls that overlap with our proposed mapping
  725. * - we can only share with an exact match on most regular files
  726. * - shared mappings on character devices and memory backed files are
  727. * permitted to overlap inexactly as far as we are concerned for in
  728. * these cases, sharing is handled in the driver or filesystem rather
  729. * than here
  730. */
  731. if (vm_flags & VM_MAYSHARE) {
  732. unsigned long pglen = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  733. unsigned long vmpglen;
  734. /* suppress VMA sharing for shared regions */
  735. if (vm_flags & VM_SHARED &&
  736. capabilities & BDI_CAP_MAP_DIRECT)
  737. goto dont_share_VMAs;
  738. for (rb = rb_first(&nommu_vma_tree); rb; rb = rb_next(rb)) {
  739. vma = rb_entry(rb, struct vm_area_struct, vm_rb);
  740. if (!(vma->vm_flags & VM_MAYSHARE))
  741. continue;
  742. /* search for overlapping mappings on the same file */
  743. if (vma->vm_file->f_path.dentry->d_inode != file->f_path.dentry->d_inode)
  744. continue;
  745. if (vma->vm_pgoff >= pgoff + pglen)
  746. continue;
  747. vmpglen = vma->vm_end - vma->vm_start + PAGE_SIZE - 1;
  748. vmpglen >>= PAGE_SHIFT;
  749. if (pgoff >= vma->vm_pgoff + vmpglen)
  750. continue;
  751. /* handle inexactly overlapping matches between mappings */
  752. if (vma->vm_pgoff != pgoff || vmpglen != pglen) {
  753. if (!(capabilities & BDI_CAP_MAP_DIRECT))
  754. goto sharing_violation;
  755. continue;
  756. }
  757. /* we've found a VMA we can share */
  758. atomic_inc(&vma->vm_usage);
  759. vml->vma = vma;
  760. result = (void *) vma->vm_start;
  761. goto shared;
  762. }
  763. dont_share_VMAs:
  764. vma = NULL;
  765. /* obtain the address at which to make a shared mapping
  766. * - this is the hook for quasi-memory character devices to
  767. * tell us the location of a shared mapping
  768. */
  769. if (file && file->f_op->get_unmapped_area) {
  770. addr = file->f_op->get_unmapped_area(file, addr, len,
  771. pgoff, flags);
  772. if (IS_ERR((void *) addr)) {
  773. ret = addr;
  774. if (ret != (unsigned long) -ENOSYS)
  775. goto error;
  776. /* the driver refused to tell us where to site
  777. * the mapping so we'll have to attempt to copy
  778. * it */
  779. ret = (unsigned long) -ENODEV;
  780. if (!(capabilities & BDI_CAP_MAP_COPY))
  781. goto error;
  782. capabilities &= ~BDI_CAP_MAP_DIRECT;
  783. }
  784. }
  785. }
  786. /* we're going to need a VMA struct as well */
  787. vma = kzalloc(sizeof(struct vm_area_struct), GFP_KERNEL);
  788. if (!vma)
  789. goto error_getting_vma;
  790. INIT_LIST_HEAD(&vma->anon_vma_node);
  791. atomic_set(&vma->vm_usage, 1);
  792. if (file)
  793. get_file(file);
  794. vma->vm_file = file;
  795. vma->vm_flags = vm_flags;
  796. vma->vm_start = addr;
  797. vma->vm_end = addr + len;
  798. vma->vm_pgoff = pgoff;
  799. vml->vma = vma;
  800. /* set up the mapping */
  801. if (file && vma->vm_flags & VM_SHARED)
  802. ret = do_mmap_shared_file(vma, len);
  803. else
  804. ret = do_mmap_private(vma, len);
  805. if (ret < 0)
  806. goto error;
  807. /* okay... we have a mapping; now we have to register it */
  808. result = (void *) vma->vm_start;
  809. if (vma->vm_flags & VM_MAPPED_COPY) {
  810. realalloc += kobjsize(result);
  811. askedalloc += len;
  812. }
  813. realalloc += kobjsize(vma);
  814. askedalloc += sizeof(*vma);
  815. current->mm->total_vm += len >> PAGE_SHIFT;
  816. add_nommu_vma(vma);
  817. shared:
  818. realalloc += kobjsize(vml);
  819. askedalloc += sizeof(*vml);
  820. add_vma_to_mm(current->mm, vml);
  821. up_write(&nommu_vma_sem);
  822. if (prot & PROT_EXEC)
  823. flush_icache_range((unsigned long) result,
  824. (unsigned long) result + len);
  825. #ifdef DEBUG
  826. printk("do_mmap:\n");
  827. show_process_blocks();
  828. #endif
  829. return (unsigned long) result;
  830. error:
  831. up_write(&nommu_vma_sem);
  832. kfree(vml);
  833. if (vma) {
  834. if (vma->vm_file)
  835. fput(vma->vm_file);
  836. kfree(vma);
  837. }
  838. return ret;
  839. sharing_violation:
  840. up_write(&nommu_vma_sem);
  841. printk("Attempt to share mismatched mappings\n");
  842. kfree(vml);
  843. return -EINVAL;
  844. error_getting_vma:
  845. up_write(&nommu_vma_sem);
  846. kfree(vml);
  847. printk("Allocation of vma for %lu byte allocation from process %d failed\n",
  848. len, current->pid);
  849. show_free_areas();
  850. return -ENOMEM;
  851. error_getting_vml:
  852. printk("Allocation of vml for %lu byte allocation from process %d failed\n",
  853. len, current->pid);
  854. show_free_areas();
  855. return -ENOMEM;
  856. }
  857. EXPORT_SYMBOL(do_mmap_pgoff);
  858. /*
  859. * handle mapping disposal for uClinux
  860. */
  861. static void put_vma(struct vm_area_struct *vma)
  862. {
  863. if (vma) {
  864. down_write(&nommu_vma_sem);
  865. if (atomic_dec_and_test(&vma->vm_usage)) {
  866. delete_nommu_vma(vma);
  867. if (vma->vm_ops && vma->vm_ops->close)
  868. vma->vm_ops->close(vma);
  869. /* IO memory and memory shared directly out of the pagecache from
  870. * ramfs/tmpfs mustn't be released here */
  871. if (vma->vm_flags & VM_MAPPED_COPY) {
  872. realalloc -= kobjsize((void *) vma->vm_start);
  873. askedalloc -= vma->vm_end - vma->vm_start;
  874. kfree((void *) vma->vm_start);
  875. }
  876. realalloc -= kobjsize(vma);
  877. askedalloc -= sizeof(*vma);
  878. if (vma->vm_file)
  879. fput(vma->vm_file);
  880. kfree(vma);
  881. }
  882. up_write(&nommu_vma_sem);
  883. }
  884. }
  885. /*
  886. * release a mapping
  887. * - under NOMMU conditions the parameters must match exactly to the mapping to
  888. * be removed
  889. */
  890. int do_munmap(struct mm_struct *mm, unsigned long addr, size_t len)
  891. {
  892. struct vm_list_struct *vml, **parent;
  893. unsigned long end = addr + len;
  894. #ifdef DEBUG
  895. printk("do_munmap:\n");
  896. #endif
  897. for (parent = &mm->context.vmlist; *parent; parent = &(*parent)->next) {
  898. if ((*parent)->vma->vm_start > addr)
  899. break;
  900. if ((*parent)->vma->vm_start == addr &&
  901. ((len == 0) || ((*parent)->vma->vm_end == end)))
  902. goto found;
  903. }
  904. printk("munmap of non-mmaped memory by process %d (%s): %p\n",
  905. current->pid, current->comm, (void *) addr);
  906. return -EINVAL;
  907. found:
  908. vml = *parent;
  909. put_vma(vml->vma);
  910. *parent = vml->next;
  911. realalloc -= kobjsize(vml);
  912. askedalloc -= sizeof(*vml);
  913. kfree(vml);
  914. update_hiwater_vm(mm);
  915. mm->total_vm -= len >> PAGE_SHIFT;
  916. #ifdef DEBUG
  917. show_process_blocks();
  918. #endif
  919. return 0;
  920. }
  921. EXPORT_SYMBOL(do_munmap);
  922. asmlinkage long sys_munmap(unsigned long addr, size_t len)
  923. {
  924. int ret;
  925. struct mm_struct *mm = current->mm;
  926. down_write(&mm->mmap_sem);
  927. ret = do_munmap(mm, addr, len);
  928. up_write(&mm->mmap_sem);
  929. return ret;
  930. }
  931. /*
  932. * Release all mappings
  933. */
  934. void exit_mmap(struct mm_struct * mm)
  935. {
  936. struct vm_list_struct *tmp;
  937. if (mm) {
  938. #ifdef DEBUG
  939. printk("Exit_mmap:\n");
  940. #endif
  941. mm->total_vm = 0;
  942. while ((tmp = mm->context.vmlist)) {
  943. mm->context.vmlist = tmp->next;
  944. put_vma(tmp->vma);
  945. realalloc -= kobjsize(tmp);
  946. askedalloc -= sizeof(*tmp);
  947. kfree(tmp);
  948. }
  949. #ifdef DEBUG
  950. show_process_blocks();
  951. #endif
  952. }
  953. }
  954. unsigned long do_brk(unsigned long addr, unsigned long len)
  955. {
  956. return -ENOMEM;
  957. }
  958. /*
  959. * expand (or shrink) an existing mapping, potentially moving it at the same
  960. * time (controlled by the MREMAP_MAYMOVE flag and available VM space)
  961. *
  962. * under NOMMU conditions, we only permit changing a mapping's size, and only
  963. * as long as it stays within the hole allocated by the kmalloc() call in
  964. * do_mmap_pgoff() and the block is not shareable
  965. *
  966. * MREMAP_FIXED is not supported under NOMMU conditions
  967. */
  968. unsigned long do_mremap(unsigned long addr,
  969. unsigned long old_len, unsigned long new_len,
  970. unsigned long flags, unsigned long new_addr)
  971. {
  972. struct vm_area_struct *vma;
  973. /* insanity checks first */
  974. if (new_len == 0)
  975. return (unsigned long) -EINVAL;
  976. if (flags & MREMAP_FIXED && new_addr != addr)
  977. return (unsigned long) -EINVAL;
  978. vma = find_vma_exact(current->mm, addr);
  979. if (!vma)
  980. return (unsigned long) -EINVAL;
  981. if (vma->vm_end != vma->vm_start + old_len)
  982. return (unsigned long) -EFAULT;
  983. if (vma->vm_flags & VM_MAYSHARE)
  984. return (unsigned long) -EPERM;
  985. if (new_len > kobjsize((void *) addr))
  986. return (unsigned long) -ENOMEM;
  987. /* all checks complete - do it */
  988. vma->vm_end = vma->vm_start + new_len;
  989. askedalloc -= old_len;
  990. askedalloc += new_len;
  991. return vma->vm_start;
  992. }
  993. EXPORT_SYMBOL(do_mremap);
  994. asmlinkage unsigned long sys_mremap(unsigned long addr,
  995. unsigned long old_len, unsigned long new_len,
  996. unsigned long flags, unsigned long new_addr)
  997. {
  998. unsigned long ret;
  999. down_write(&current->mm->mmap_sem);
  1000. ret = do_mremap(addr, old_len, new_len, flags, new_addr);
  1001. up_write(&current->mm->mmap_sem);
  1002. return ret;
  1003. }
  1004. struct page *follow_page(struct vm_area_struct *vma, unsigned long address,
  1005. unsigned int foll_flags)
  1006. {
  1007. return NULL;
  1008. }
  1009. int remap_pfn_range(struct vm_area_struct *vma, unsigned long from,
  1010. unsigned long to, unsigned long size, pgprot_t prot)
  1011. {
  1012. vma->vm_start = vma->vm_pgoff << PAGE_SHIFT;
  1013. return 0;
  1014. }
  1015. EXPORT_SYMBOL(remap_pfn_range);
  1016. void swap_unplug_io_fn(struct backing_dev_info *bdi, struct page *page)
  1017. {
  1018. }
  1019. unsigned long arch_get_unmapped_area(struct file *file, unsigned long addr,
  1020. unsigned long len, unsigned long pgoff, unsigned long flags)
  1021. {
  1022. return -ENOMEM;
  1023. }
  1024. void arch_unmap_area(struct mm_struct *mm, unsigned long addr)
  1025. {
  1026. }
  1027. void unmap_mapping_range(struct address_space *mapping,
  1028. loff_t const holebegin, loff_t const holelen,
  1029. int even_cows)
  1030. {
  1031. }
  1032. EXPORT_SYMBOL(unmap_mapping_range);
  1033. /*
  1034. * ask for an unmapped area at which to create a mapping on a file
  1035. */
  1036. unsigned long get_unmapped_area(struct file *file, unsigned long addr,
  1037. unsigned long len, unsigned long pgoff,
  1038. unsigned long flags)
  1039. {
  1040. unsigned long (*get_area)(struct file *, unsigned long, unsigned long,
  1041. unsigned long, unsigned long);
  1042. get_area = current->mm->get_unmapped_area;
  1043. if (file && file->f_op && file->f_op->get_unmapped_area)
  1044. get_area = file->f_op->get_unmapped_area;
  1045. if (!get_area)
  1046. return -ENOSYS;
  1047. return get_area(file, addr, len, pgoff, flags);
  1048. }
  1049. EXPORT_SYMBOL(get_unmapped_area);
  1050. /*
  1051. * Check that a process has enough memory to allocate a new virtual
  1052. * mapping. 0 means there is enough memory for the allocation to
  1053. * succeed and -ENOMEM implies there is not.
  1054. *
  1055. * We currently support three overcommit policies, which are set via the
  1056. * vm.overcommit_memory sysctl. See Documentation/vm/overcommit-accounting
  1057. *
  1058. * Strict overcommit modes added 2002 Feb 26 by Alan Cox.
  1059. * Additional code 2002 Jul 20 by Robert Love.
  1060. *
  1061. * cap_sys_admin is 1 if the process has admin privileges, 0 otherwise.
  1062. *
  1063. * Note this is a helper function intended to be used by LSMs which
  1064. * wish to use this logic.
  1065. */
  1066. int __vm_enough_memory(struct mm_struct *mm, long pages, int cap_sys_admin)
  1067. {
  1068. unsigned long free, allowed;
  1069. vm_acct_memory(pages);
  1070. /*
  1071. * Sometimes we want to use more memory than we have
  1072. */
  1073. if (sysctl_overcommit_memory == OVERCOMMIT_ALWAYS)
  1074. return 0;
  1075. if (sysctl_overcommit_memory == OVERCOMMIT_GUESS) {
  1076. unsigned long n;
  1077. free = global_page_state(NR_FILE_PAGES);
  1078. free += nr_swap_pages;
  1079. /*
  1080. * Any slabs which are created with the
  1081. * SLAB_RECLAIM_ACCOUNT flag claim to have contents
  1082. * which are reclaimable, under pressure. The dentry
  1083. * cache and most inode caches should fall into this
  1084. */
  1085. free += global_page_state(NR_SLAB_RECLAIMABLE);
  1086. /*
  1087. * Leave the last 3% for root
  1088. */
  1089. if (!cap_sys_admin)
  1090. free -= free / 32;
  1091. if (free > pages)
  1092. return 0;
  1093. /*
  1094. * nr_free_pages() is very expensive on large systems,
  1095. * only call if we're about to fail.
  1096. */
  1097. n = nr_free_pages();
  1098. /*
  1099. * Leave reserved pages. The pages are not for anonymous pages.
  1100. */
  1101. if (n <= totalreserve_pages)
  1102. goto error;
  1103. else
  1104. n -= totalreserve_pages;
  1105. /*
  1106. * Leave the last 3% for root
  1107. */
  1108. if (!cap_sys_admin)
  1109. n -= n / 32;
  1110. free += n;
  1111. if (free > pages)
  1112. return 0;
  1113. goto error;
  1114. }
  1115. allowed = totalram_pages * sysctl_overcommit_ratio / 100;
  1116. /*
  1117. * Leave the last 3% for root
  1118. */
  1119. if (!cap_sys_admin)
  1120. allowed -= allowed / 32;
  1121. allowed += total_swap_pages;
  1122. /* Don't let a single process grow too big:
  1123. leave 3% of the size of this process for other processes */
  1124. allowed -= current->mm->total_vm / 32;
  1125. /*
  1126. * cast `allowed' as a signed long because vm_committed_space
  1127. * sometimes has a negative value
  1128. */
  1129. if (atomic_read(&vm_committed_space) < (long)allowed)
  1130. return 0;
  1131. error:
  1132. vm_unacct_memory(pages);
  1133. return -ENOMEM;
  1134. }
  1135. int in_gate_area_no_task(unsigned long addr)
  1136. {
  1137. return 0;
  1138. }
  1139. int filemap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  1140. {
  1141. BUG();
  1142. return 0;
  1143. }
  1144. EXPORT_SYMBOL(filemap_fault);
  1145. /*
  1146. * Access another process' address space.
  1147. * - source/target buffer must be kernel space
  1148. */
  1149. int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write)
  1150. {
  1151. struct vm_area_struct *vma;
  1152. struct mm_struct *mm;
  1153. if (addr + len < addr)
  1154. return 0;
  1155. mm = get_task_mm(tsk);
  1156. if (!mm)
  1157. return 0;
  1158. down_read(&mm->mmap_sem);
  1159. /* the access must start within one of the target process's mappings */
  1160. vma = find_vma(mm, addr);
  1161. if (vma) {
  1162. /* don't overrun this mapping */
  1163. if (addr + len >= vma->vm_end)
  1164. len = vma->vm_end - addr;
  1165. /* only read or write mappings where it is permitted */
  1166. if (write && vma->vm_flags & VM_MAYWRITE)
  1167. len -= copy_to_user((void *) addr, buf, len);
  1168. else if (!write && vma->vm_flags & VM_MAYREAD)
  1169. len -= copy_from_user(buf, (void *) addr, len);
  1170. else
  1171. len = 0;
  1172. } else {
  1173. len = 0;
  1174. }
  1175. up_read(&mm->mmap_sem);
  1176. mmput(mm);
  1177. return len;
  1178. }