nommu.c 32 KB

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