nommu.c 28 KB

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