nommu.c 31 KB

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