nommu.c 28 KB

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