nommu.c 33 KB

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