nommu.c 31 KB

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