nommu.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181
  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, unsigned int __nocast gfp_mask,
  132. 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 vml 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. (*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. mm->total_vm -= len >> PAGE_SHIFT;
  775. #ifdef DEBUG
  776. show_process_blocks();
  777. #endif
  778. return 0;
  779. }
  780. /* Release all mmaps. */
  781. void exit_mmap(struct mm_struct * mm)
  782. {
  783. struct vm_list_struct *tmp;
  784. if (mm) {
  785. #ifdef DEBUG
  786. printk("Exit_mmap:\n");
  787. #endif
  788. mm->total_vm = 0;
  789. while ((tmp = mm->context.vmlist)) {
  790. mm->context.vmlist = tmp->next;
  791. put_vma(tmp->vma);
  792. realalloc -= kobjsize(tmp);
  793. askedalloc -= sizeof(*tmp);
  794. kfree(tmp);
  795. }
  796. #ifdef DEBUG
  797. show_process_blocks();
  798. #endif
  799. }
  800. }
  801. asmlinkage long sys_munmap(unsigned long addr, size_t len)
  802. {
  803. int ret;
  804. struct mm_struct *mm = current->mm;
  805. down_write(&mm->mmap_sem);
  806. ret = do_munmap(mm, addr, len);
  807. up_write(&mm->mmap_sem);
  808. return ret;
  809. }
  810. unsigned long do_brk(unsigned long addr, unsigned long len)
  811. {
  812. return -ENOMEM;
  813. }
  814. /*
  815. * Expand (or shrink) an existing mapping, potentially moving it at the
  816. * same time (controlled by the MREMAP_MAYMOVE flag and available VM space)
  817. *
  818. * MREMAP_FIXED option added 5-Dec-1999 by Benjamin LaHaise
  819. * This option implies MREMAP_MAYMOVE.
  820. *
  821. * on uClinux, we only permit changing a mapping's size, and only as long as it stays within the
  822. * hole allocated by the kmalloc() call in do_mmap_pgoff() and the block is not shareable
  823. */
  824. unsigned long do_mremap(unsigned long addr,
  825. unsigned long old_len, unsigned long new_len,
  826. unsigned long flags, unsigned long new_addr)
  827. {
  828. struct vm_list_struct *vml = NULL;
  829. /* insanity checks first */
  830. if (new_len == 0)
  831. return (unsigned long) -EINVAL;
  832. if (flags & MREMAP_FIXED && new_addr != addr)
  833. return (unsigned long) -EINVAL;
  834. for (vml = current->mm->context.vmlist; vml; vml = vml->next)
  835. if (vml->vma->vm_start == addr)
  836. goto found;
  837. return (unsigned long) -EINVAL;
  838. found:
  839. if (vml->vma->vm_end != vml->vma->vm_start + old_len)
  840. return (unsigned long) -EFAULT;
  841. if (vml->vma->vm_flags & VM_MAYSHARE)
  842. return (unsigned long) -EPERM;
  843. if (new_len > kobjsize((void *) addr))
  844. return (unsigned long) -ENOMEM;
  845. /* all checks complete - do it */
  846. vml->vma->vm_end = vml->vma->vm_start + new_len;
  847. askedalloc -= old_len;
  848. askedalloc += new_len;
  849. return vml->vma->vm_start;
  850. }
  851. /*
  852. * Look up the first VMA which satisfies addr < vm_end, NULL if none
  853. */
  854. struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)
  855. {
  856. struct vm_list_struct *vml;
  857. for (vml = mm->context.vmlist; vml; vml = vml->next)
  858. if (addr >= vml->vma->vm_start && addr < vml->vma->vm_end)
  859. return vml->vma;
  860. return NULL;
  861. }
  862. EXPORT_SYMBOL(find_vma);
  863. struct page * follow_page(struct mm_struct *mm, unsigned long addr, int write)
  864. {
  865. return NULL;
  866. }
  867. struct vm_area_struct *find_extend_vma(struct mm_struct *mm, unsigned long addr)
  868. {
  869. return NULL;
  870. }
  871. int remap_pfn_range(struct vm_area_struct *vma, unsigned long from,
  872. unsigned long to, unsigned long size, pgprot_t prot)
  873. {
  874. return -EPERM;
  875. }
  876. void swap_unplug_io_fn(struct backing_dev_info *bdi, struct page *page)
  877. {
  878. }
  879. unsigned long arch_get_unmapped_area(struct file *file, unsigned long addr,
  880. unsigned long len, unsigned long pgoff, unsigned long flags)
  881. {
  882. return -ENOMEM;
  883. }
  884. void arch_unmap_area(struct mm_struct *mm, unsigned long addr)
  885. {
  886. }
  887. void update_mem_hiwater(struct task_struct *tsk)
  888. {
  889. unsigned long rss = get_mm_counter(tsk->mm, rss);
  890. if (likely(tsk->mm)) {
  891. if (tsk->mm->hiwater_rss < rss)
  892. tsk->mm->hiwater_rss = rss;
  893. if (tsk->mm->hiwater_vm < tsk->mm->total_vm)
  894. tsk->mm->hiwater_vm = tsk->mm->total_vm;
  895. }
  896. }
  897. void unmap_mapping_range(struct address_space *mapping,
  898. loff_t const holebegin, loff_t const holelen,
  899. int even_cows)
  900. {
  901. }
  902. /*
  903. * Check that a process has enough memory to allocate a new virtual
  904. * mapping. 0 means there is enough memory for the allocation to
  905. * succeed and -ENOMEM implies there is not.
  906. *
  907. * We currently support three overcommit policies, which are set via the
  908. * vm.overcommit_memory sysctl. See Documentation/vm/overcommit-accounting
  909. *
  910. * Strict overcommit modes added 2002 Feb 26 by Alan Cox.
  911. * Additional code 2002 Jul 20 by Robert Love.
  912. *
  913. * cap_sys_admin is 1 if the process has admin privileges, 0 otherwise.
  914. *
  915. * Note this is a helper function intended to be used by LSMs which
  916. * wish to use this logic.
  917. */
  918. int __vm_enough_memory(long pages, int cap_sys_admin)
  919. {
  920. unsigned long free, allowed;
  921. vm_acct_memory(pages);
  922. /*
  923. * Sometimes we want to use more memory than we have
  924. */
  925. if (sysctl_overcommit_memory == OVERCOMMIT_ALWAYS)
  926. return 0;
  927. if (sysctl_overcommit_memory == OVERCOMMIT_GUESS) {
  928. unsigned long n;
  929. free = get_page_cache_size();
  930. free += nr_swap_pages;
  931. /*
  932. * Any slabs which are created with the
  933. * SLAB_RECLAIM_ACCOUNT flag claim to have contents
  934. * which are reclaimable, under pressure. The dentry
  935. * cache and most inode caches should fall into this
  936. */
  937. free += atomic_read(&slab_reclaim_pages);
  938. /*
  939. * Leave the last 3% for root
  940. */
  941. if (!cap_sys_admin)
  942. free -= free / 32;
  943. if (free > pages)
  944. return 0;
  945. /*
  946. * nr_free_pages() is very expensive on large systems,
  947. * only call if we're about to fail.
  948. */
  949. n = nr_free_pages();
  950. if (!cap_sys_admin)
  951. n -= n / 32;
  952. free += n;
  953. if (free > pages)
  954. return 0;
  955. vm_unacct_memory(pages);
  956. return -ENOMEM;
  957. }
  958. allowed = totalram_pages * sysctl_overcommit_ratio / 100;
  959. /*
  960. * Leave the last 3% for root
  961. */
  962. if (!cap_sys_admin)
  963. allowed -= allowed / 32;
  964. allowed += total_swap_pages;
  965. /* Don't let a single process grow too big:
  966. leave 3% of the size of this process for other processes */
  967. allowed -= current->mm->total_vm / 32;
  968. if (atomic_read(&vm_committed_space) < allowed)
  969. return 0;
  970. vm_unacct_memory(pages);
  971. return -ENOMEM;
  972. }
  973. int in_gate_area_no_task(unsigned long addr)
  974. {
  975. return 0;
  976. }