nommu.c 28 KB

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