nommu.c 28 KB

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