nommu.c 32 KB

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