nommu.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347
  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 cotrol 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 (PAGE_ALIGN(len) == 0)
  423. return addr;
  424. if (len > TASK_SIZE)
  425. return -EINVAL;
  426. /* offset overflow? */
  427. if ((pgoff + (len >> PAGE_SHIFT)) < pgoff)
  428. return -EINVAL;
  429. if (file) {
  430. /* validate file mapping requests */
  431. struct address_space *mapping;
  432. /* files must support mmap */
  433. if (!file->f_op || !file->f_op->mmap)
  434. return -ENODEV;
  435. /* work out if what we've got could possibly be shared
  436. * - we support chardevs that provide their own "memory"
  437. * - we support files/blockdevs that are memory backed
  438. */
  439. mapping = file->f_mapping;
  440. if (!mapping)
  441. mapping = file->f_dentry->d_inode->i_mapping;
  442. capabilities = 0;
  443. if (mapping && mapping->backing_dev_info)
  444. capabilities = mapping->backing_dev_info->capabilities;
  445. if (!capabilities) {
  446. /* no explicit capabilities set, so assume some
  447. * defaults */
  448. switch (file->f_dentry->d_inode->i_mode & S_IFMT) {
  449. case S_IFREG:
  450. case S_IFBLK:
  451. capabilities = BDI_CAP_MAP_COPY;
  452. break;
  453. case S_IFCHR:
  454. capabilities =
  455. BDI_CAP_MAP_DIRECT |
  456. BDI_CAP_READ_MAP |
  457. BDI_CAP_WRITE_MAP;
  458. break;
  459. default:
  460. return -EINVAL;
  461. }
  462. }
  463. /* eliminate any capabilities that we can't support on this
  464. * device */
  465. if (!file->f_op->get_unmapped_area)
  466. capabilities &= ~BDI_CAP_MAP_DIRECT;
  467. if (!file->f_op->read)
  468. capabilities &= ~BDI_CAP_MAP_COPY;
  469. if (flags & MAP_SHARED) {
  470. /* do checks for writing, appending and locking */
  471. if ((prot & PROT_WRITE) &&
  472. !(file->f_mode & FMODE_WRITE))
  473. return -EACCES;
  474. if (IS_APPEND(file->f_dentry->d_inode) &&
  475. (file->f_mode & FMODE_WRITE))
  476. return -EACCES;
  477. if (locks_verify_locked(file->f_dentry->d_inode))
  478. return -EAGAIN;
  479. if (!(capabilities & BDI_CAP_MAP_DIRECT))
  480. return -ENODEV;
  481. if (((prot & PROT_READ) && !(capabilities & BDI_CAP_READ_MAP)) ||
  482. ((prot & PROT_WRITE) && !(capabilities & BDI_CAP_WRITE_MAP)) ||
  483. ((prot & PROT_EXEC) && !(capabilities & BDI_CAP_EXEC_MAP))
  484. ) {
  485. printk("MAP_SHARED not completely supported on !MMU\n");
  486. return -EINVAL;
  487. }
  488. /* we mustn't privatise shared mappings */
  489. capabilities &= ~BDI_CAP_MAP_COPY;
  490. }
  491. else {
  492. /* we're going to read the file into private memory we
  493. * allocate */
  494. if (!(capabilities & BDI_CAP_MAP_COPY))
  495. return -ENODEV;
  496. /* we don't permit a private writable mapping to be
  497. * shared with the backing device */
  498. if (prot & PROT_WRITE)
  499. capabilities &= ~BDI_CAP_MAP_DIRECT;
  500. }
  501. /* handle executable mappings and implied executable
  502. * mappings */
  503. if (file->f_vfsmnt->mnt_flags & MNT_NOEXEC) {
  504. if (prot & PROT_EXEC)
  505. return -EPERM;
  506. }
  507. else if ((prot & PROT_READ) && !(prot & PROT_EXEC)) {
  508. /* handle implication of PROT_EXEC by PROT_READ */
  509. if (current->personality & READ_IMPLIES_EXEC) {
  510. if (capabilities & BDI_CAP_EXEC_MAP)
  511. prot |= PROT_EXEC;
  512. }
  513. }
  514. else if ((prot & PROT_READ) &&
  515. (prot & PROT_EXEC) &&
  516. !(capabilities & BDI_CAP_EXEC_MAP)
  517. ) {
  518. /* backing file is not executable, try to copy */
  519. capabilities &= ~BDI_CAP_MAP_DIRECT;
  520. }
  521. }
  522. else {
  523. /* anonymous mappings are always memory backed and can be
  524. * privately mapped
  525. */
  526. capabilities = BDI_CAP_MAP_COPY;
  527. /* handle PROT_EXEC implication by PROT_READ */
  528. if ((prot & PROT_READ) &&
  529. (current->personality & READ_IMPLIES_EXEC))
  530. prot |= PROT_EXEC;
  531. }
  532. /* allow the security API to have its say */
  533. ret = security_file_mmap(file, reqprot, prot, flags);
  534. if (ret < 0)
  535. return ret;
  536. /* looks okay */
  537. *_capabilities = capabilities;
  538. return 0;
  539. }
  540. /*
  541. * we've determined that we can make the mapping, now translate what we
  542. * now know into VMA flags
  543. */
  544. static unsigned long determine_vm_flags(struct file *file,
  545. unsigned long prot,
  546. unsigned long flags,
  547. unsigned long capabilities)
  548. {
  549. unsigned long vm_flags;
  550. vm_flags = calc_vm_prot_bits(prot) | calc_vm_flag_bits(flags);
  551. vm_flags |= VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
  552. /* vm_flags |= mm->def_flags; */
  553. if (!(capabilities & BDI_CAP_MAP_DIRECT)) {
  554. /* attempt to share read-only copies of mapped file chunks */
  555. if (file && !(prot & PROT_WRITE))
  556. vm_flags |= VM_MAYSHARE;
  557. }
  558. else {
  559. /* overlay a shareable mapping on the backing device or inode
  560. * if possible - used for chardevs, ramfs/tmpfs/shmfs and
  561. * romfs/cramfs */
  562. if (flags & MAP_SHARED)
  563. vm_flags |= VM_MAYSHARE | VM_SHARED;
  564. else if ((((vm_flags & capabilities) ^ vm_flags) & BDI_CAP_VMFLAGS) == 0)
  565. vm_flags |= VM_MAYSHARE;
  566. }
  567. /* refuse to let anyone share private mappings with this process if
  568. * it's being traced - otherwise breakpoints set in it may interfere
  569. * with another untraced process
  570. */
  571. if ((flags & MAP_PRIVATE) && (current->ptrace & PT_PTRACED))
  572. vm_flags &= ~VM_MAYSHARE;
  573. return vm_flags;
  574. }
  575. /*
  576. * set up a shared mapping on a file
  577. */
  578. static int do_mmap_shared_file(struct vm_area_struct *vma, unsigned long len)
  579. {
  580. int ret;
  581. ret = vma->vm_file->f_op->mmap(vma->vm_file, vma);
  582. if (ret != -ENOSYS)
  583. return ret;
  584. /* getting an ENOSYS error indicates that direct mmap isn't
  585. * possible (as opposed to tried but failed) so we'll fall
  586. * through to making a private copy of the data and mapping
  587. * that if we can */
  588. return -ENODEV;
  589. }
  590. /*
  591. * set up a private mapping or an anonymous shared mapping
  592. */
  593. static int do_mmap_private(struct vm_area_struct *vma, unsigned long len)
  594. {
  595. void *base;
  596. int ret;
  597. /* invoke the file's mapping function so that it can keep track of
  598. * shared mappings on devices or memory
  599. * - VM_MAYSHARE will be set if it may attempt to share
  600. */
  601. if (vma->vm_file) {
  602. ret = vma->vm_file->f_op->mmap(vma->vm_file, vma);
  603. if (ret != -ENOSYS) {
  604. /* shouldn't return success if we're not sharing */
  605. BUG_ON(ret == 0 && !(vma->vm_flags & VM_MAYSHARE));
  606. return ret; /* success or a real error */
  607. }
  608. /* getting an ENOSYS error indicates that direct mmap isn't
  609. * possible (as opposed to tried but failed) so we'll try to
  610. * make a private copy of the data and map that instead */
  611. }
  612. /* allocate some memory to hold the mapping
  613. * - note that this may not return a page-aligned address if the object
  614. * we're allocating is smaller than a page
  615. */
  616. base = kmalloc(len, GFP_KERNEL|__GFP_COMP);
  617. if (!base)
  618. goto enomem;
  619. vma->vm_start = (unsigned long) base;
  620. vma->vm_end = vma->vm_start + len;
  621. vma->vm_flags |= VM_MAPPED_COPY;
  622. #ifdef WARN_ON_SLACK
  623. if (len + WARN_ON_SLACK <= kobjsize(result))
  624. printk("Allocation of %lu bytes from process %d has %lu bytes of slack\n",
  625. len, current->pid, kobjsize(result) - len);
  626. #endif
  627. if (vma->vm_file) {
  628. /* read the contents of a file into the copy */
  629. mm_segment_t old_fs;
  630. loff_t fpos;
  631. fpos = vma->vm_pgoff;
  632. fpos <<= PAGE_SHIFT;
  633. old_fs = get_fs();
  634. set_fs(KERNEL_DS);
  635. ret = vma->vm_file->f_op->read(vma->vm_file, base, len, &fpos);
  636. set_fs(old_fs);
  637. if (ret < 0)
  638. goto error_free;
  639. /* clear the last little bit */
  640. if (ret < len)
  641. memset(base + ret, 0, len - ret);
  642. } else {
  643. /* if it's an anonymous mapping, then just clear it */
  644. memset(base, 0, len);
  645. }
  646. return 0;
  647. error_free:
  648. kfree(base);
  649. vma->vm_start = 0;
  650. return ret;
  651. enomem:
  652. printk("Allocation of length %lu from process %d failed\n",
  653. len, current->pid);
  654. show_free_areas();
  655. return -ENOMEM;
  656. }
  657. /*
  658. * handle mapping creation for uClinux
  659. */
  660. unsigned long do_mmap_pgoff(struct file *file,
  661. unsigned long addr,
  662. unsigned long len,
  663. unsigned long prot,
  664. unsigned long flags,
  665. unsigned long pgoff)
  666. {
  667. struct vm_list_struct *vml = NULL;
  668. struct vm_area_struct *vma = NULL;
  669. struct rb_node *rb;
  670. unsigned long capabilities, vm_flags;
  671. void *result;
  672. int ret;
  673. /* decide whether we should attempt the mapping, and if so what sort of
  674. * mapping */
  675. ret = validate_mmap_request(file, addr, len, prot, flags, pgoff,
  676. &capabilities);
  677. if (ret < 0)
  678. return ret;
  679. /* we've determined that we can make the mapping, now translate what we
  680. * now know into VMA flags */
  681. vm_flags = determine_vm_flags(file, prot, flags, capabilities);
  682. /* we're going to need to record the mapping if it works */
  683. vml = kmalloc(sizeof(struct vm_list_struct), GFP_KERNEL);
  684. if (!vml)
  685. goto error_getting_vml;
  686. memset(vml, 0, sizeof(*vml));
  687. down_write(&nommu_vma_sem);
  688. /* if we want to share, we need to check for VMAs created by other
  689. * mmap() calls that overlap with our proposed mapping
  690. * - we can only share with an exact match on most regular files
  691. * - shared mappings on character devices and memory backed files are
  692. * permitted to overlap inexactly as far as we are concerned for in
  693. * these cases, sharing is handled in the driver or filesystem rather
  694. * than here
  695. */
  696. if (vm_flags & VM_MAYSHARE) {
  697. unsigned long pglen = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  698. unsigned long vmpglen;
  699. for (rb = rb_first(&nommu_vma_tree); rb; rb = rb_next(rb)) {
  700. vma = rb_entry(rb, struct vm_area_struct, vm_rb);
  701. if (!(vma->vm_flags & VM_MAYSHARE))
  702. continue;
  703. /* search for overlapping mappings on the same file */
  704. if (vma->vm_file->f_dentry->d_inode != file->f_dentry->d_inode)
  705. continue;
  706. if (vma->vm_pgoff >= pgoff + pglen)
  707. continue;
  708. vmpglen = vma->vm_end - vma->vm_start + PAGE_SIZE - 1;
  709. vmpglen >>= PAGE_SHIFT;
  710. if (pgoff >= vma->vm_pgoff + vmpglen)
  711. continue;
  712. /* handle inexactly overlapping matches between mappings */
  713. if (vma->vm_pgoff != pgoff || vmpglen != pglen) {
  714. if (!(capabilities & BDI_CAP_MAP_DIRECT))
  715. goto sharing_violation;
  716. continue;
  717. }
  718. /* we've found a VMA we can share */
  719. atomic_inc(&vma->vm_usage);
  720. vml->vma = vma;
  721. result = (void *) vma->vm_start;
  722. goto shared;
  723. }
  724. vma = NULL;
  725. /* obtain the address at which to make a shared mapping
  726. * - this is the hook for quasi-memory character devices to
  727. * tell us the location of a shared mapping
  728. */
  729. if (file && file->f_op->get_unmapped_area) {
  730. addr = file->f_op->get_unmapped_area(file, addr, len,
  731. pgoff, flags);
  732. if (IS_ERR((void *) addr)) {
  733. ret = addr;
  734. if (ret != (unsigned long) -ENOSYS)
  735. goto error;
  736. /* the driver refused to tell us where to site
  737. * the mapping so we'll have to attempt to copy
  738. * it */
  739. ret = (unsigned long) -ENODEV;
  740. if (!(capabilities & BDI_CAP_MAP_COPY))
  741. goto error;
  742. capabilities &= ~BDI_CAP_MAP_DIRECT;
  743. }
  744. }
  745. }
  746. /* we're going to need a VMA struct as well */
  747. vma = kmalloc(sizeof(struct vm_area_struct), GFP_KERNEL);
  748. if (!vma)
  749. goto error_getting_vma;
  750. memset(vma, 0, sizeof(*vma));
  751. INIT_LIST_HEAD(&vma->anon_vma_node);
  752. atomic_set(&vma->vm_usage, 1);
  753. if (file)
  754. get_file(file);
  755. vma->vm_file = file;
  756. vma->vm_flags = vm_flags;
  757. vma->vm_start = addr;
  758. vma->vm_end = addr + len;
  759. vma->vm_pgoff = pgoff;
  760. vml->vma = vma;
  761. /* set up the mapping */
  762. if (file && vma->vm_flags & VM_SHARED)
  763. ret = do_mmap_shared_file(vma, len);
  764. else
  765. ret = do_mmap_private(vma, len);
  766. if (ret < 0)
  767. goto error;
  768. /* okay... we have a mapping; now we have to register it */
  769. result = (void *) vma->vm_start;
  770. if (vma->vm_flags & VM_MAPPED_COPY) {
  771. realalloc += kobjsize(result);
  772. askedalloc += len;
  773. }
  774. realalloc += kobjsize(vma);
  775. askedalloc += sizeof(*vma);
  776. current->mm->total_vm += len >> PAGE_SHIFT;
  777. add_nommu_vma(vma);
  778. shared:
  779. realalloc += kobjsize(vml);
  780. askedalloc += sizeof(*vml);
  781. add_vma_to_mm(current->mm, vml);
  782. up_write(&nommu_vma_sem);
  783. if (prot & PROT_EXEC)
  784. flush_icache_range((unsigned long) result,
  785. (unsigned long) result + len);
  786. #ifdef DEBUG
  787. printk("do_mmap:\n");
  788. show_process_blocks();
  789. #endif
  790. return (unsigned long) result;
  791. error:
  792. up_write(&nommu_vma_sem);
  793. kfree(vml);
  794. if (vma) {
  795. fput(vma->vm_file);
  796. kfree(vma);
  797. }
  798. return ret;
  799. sharing_violation:
  800. up_write(&nommu_vma_sem);
  801. printk("Attempt to share mismatched mappings\n");
  802. kfree(vml);
  803. return -EINVAL;
  804. error_getting_vma:
  805. up_write(&nommu_vma_sem);
  806. kfree(vml);
  807. printk("Allocation of vma for %lu byte allocation from process %d failed\n",
  808. len, current->pid);
  809. show_free_areas();
  810. return -ENOMEM;
  811. error_getting_vml:
  812. printk("Allocation of vml for %lu byte allocation from process %d failed\n",
  813. len, current->pid);
  814. show_free_areas();
  815. return -ENOMEM;
  816. }
  817. /*
  818. * handle mapping disposal for uClinux
  819. */
  820. static void put_vma(struct vm_area_struct *vma)
  821. {
  822. if (vma) {
  823. down_write(&nommu_vma_sem);
  824. if (atomic_dec_and_test(&vma->vm_usage)) {
  825. delete_nommu_vma(vma);
  826. if (vma->vm_ops && vma->vm_ops->close)
  827. vma->vm_ops->close(vma);
  828. /* IO memory and memory shared directly out of the pagecache from
  829. * ramfs/tmpfs mustn't be released here */
  830. if (vma->vm_flags & VM_MAPPED_COPY) {
  831. realalloc -= kobjsize((void *) vma->vm_start);
  832. askedalloc -= vma->vm_end - vma->vm_start;
  833. kfree((void *) vma->vm_start);
  834. }
  835. realalloc -= kobjsize(vma);
  836. askedalloc -= sizeof(*vma);
  837. if (vma->vm_file)
  838. fput(vma->vm_file);
  839. kfree(vma);
  840. }
  841. up_write(&nommu_vma_sem);
  842. }
  843. }
  844. /*
  845. * release a mapping
  846. * - under NOMMU conditions the parameters must match exactly to the mapping to
  847. * be removed
  848. */
  849. int do_munmap(struct mm_struct *mm, unsigned long addr, size_t len)
  850. {
  851. struct vm_list_struct *vml, **parent;
  852. unsigned long end = addr + len;
  853. #ifdef DEBUG
  854. printk("do_munmap:\n");
  855. #endif
  856. for (parent = &mm->context.vmlist; *parent; parent = &(*parent)->next) {
  857. if ((*parent)->vma->vm_start > addr)
  858. break;
  859. if ((*parent)->vma->vm_start == addr &&
  860. ((len == 0) || ((*parent)->vma->vm_end == end)))
  861. goto found;
  862. }
  863. printk("munmap of non-mmaped memory by process %d (%s): %p\n",
  864. current->pid, current->comm, (void *) addr);
  865. return -EINVAL;
  866. found:
  867. vml = *parent;
  868. put_vma(vml->vma);
  869. *parent = vml->next;
  870. realalloc -= kobjsize(vml);
  871. askedalloc -= sizeof(*vml);
  872. kfree(vml);
  873. update_hiwater_vm(mm);
  874. mm->total_vm -= len >> PAGE_SHIFT;
  875. #ifdef DEBUG
  876. show_process_blocks();
  877. #endif
  878. return 0;
  879. }
  880. asmlinkage long sys_munmap(unsigned long addr, size_t len)
  881. {
  882. int ret;
  883. struct mm_struct *mm = current->mm;
  884. down_write(&mm->mmap_sem);
  885. ret = do_munmap(mm, addr, len);
  886. up_write(&mm->mmap_sem);
  887. return ret;
  888. }
  889. /*
  890. * Release all mappings
  891. */
  892. void exit_mmap(struct mm_struct * mm)
  893. {
  894. struct vm_list_struct *tmp;
  895. if (mm) {
  896. #ifdef DEBUG
  897. printk("Exit_mmap:\n");
  898. #endif
  899. mm->total_vm = 0;
  900. while ((tmp = mm->context.vmlist)) {
  901. mm->context.vmlist = tmp->next;
  902. put_vma(tmp->vma);
  903. realalloc -= kobjsize(tmp);
  904. askedalloc -= sizeof(*tmp);
  905. kfree(tmp);
  906. }
  907. #ifdef DEBUG
  908. show_process_blocks();
  909. #endif
  910. }
  911. }
  912. unsigned long do_brk(unsigned long addr, unsigned long len)
  913. {
  914. return -ENOMEM;
  915. }
  916. /*
  917. * expand (or shrink) an existing mapping, potentially moving it at the same
  918. * time (controlled by the MREMAP_MAYMOVE flag and available VM space)
  919. *
  920. * under NOMMU conditions, we only permit changing a mapping's size, and only
  921. * as long as it stays within the hole allocated by the kmalloc() call in
  922. * do_mmap_pgoff() and the block is not shareable
  923. *
  924. * MREMAP_FIXED is not supported under NOMMU conditions
  925. */
  926. unsigned long do_mremap(unsigned long addr,
  927. unsigned long old_len, unsigned long new_len,
  928. unsigned long flags, unsigned long new_addr)
  929. {
  930. struct vm_area_struct *vma;
  931. /* insanity checks first */
  932. if (new_len == 0)
  933. return (unsigned long) -EINVAL;
  934. if (flags & MREMAP_FIXED && new_addr != addr)
  935. return (unsigned long) -EINVAL;
  936. vma = find_vma_exact(current->mm, addr);
  937. if (!vma)
  938. return (unsigned long) -EINVAL;
  939. if (vma->vm_end != vma->vm_start + old_len)
  940. return (unsigned long) -EFAULT;
  941. if (vma->vm_flags & VM_MAYSHARE)
  942. return (unsigned long) -EPERM;
  943. if (new_len > kobjsize((void *) addr))
  944. return (unsigned long) -ENOMEM;
  945. /* all checks complete - do it */
  946. vma->vm_end = vma->vm_start + new_len;
  947. askedalloc -= old_len;
  948. askedalloc += new_len;
  949. return vma->vm_start;
  950. }
  951. asmlinkage unsigned long sys_mremap(unsigned long addr,
  952. unsigned long old_len, unsigned long new_len,
  953. unsigned long flags, unsigned long new_addr)
  954. {
  955. unsigned long ret;
  956. down_write(&current->mm->mmap_sem);
  957. ret = do_mremap(addr, old_len, new_len, flags, new_addr);
  958. up_write(&current->mm->mmap_sem);
  959. return ret;
  960. }
  961. struct page *follow_page(struct vm_area_struct *vma, unsigned long address,
  962. unsigned int foll_flags)
  963. {
  964. return NULL;
  965. }
  966. int remap_pfn_range(struct vm_area_struct *vma, unsigned long from,
  967. unsigned long to, unsigned long size, pgprot_t prot)
  968. {
  969. vma->vm_start = vma->vm_pgoff << PAGE_SHIFT;
  970. return 0;
  971. }
  972. EXPORT_SYMBOL(remap_pfn_range);
  973. void swap_unplug_io_fn(struct backing_dev_info *bdi, struct page *page)
  974. {
  975. }
  976. unsigned long arch_get_unmapped_area(struct file *file, unsigned long addr,
  977. unsigned long len, unsigned long pgoff, unsigned long flags)
  978. {
  979. return -ENOMEM;
  980. }
  981. void arch_unmap_area(struct mm_struct *mm, unsigned long addr)
  982. {
  983. }
  984. void unmap_mapping_range(struct address_space *mapping,
  985. loff_t const holebegin, loff_t const holelen,
  986. int even_cows)
  987. {
  988. }
  989. EXPORT_SYMBOL(unmap_mapping_range);
  990. /*
  991. * Check that a process has enough memory to allocate a new virtual
  992. * mapping. 0 means there is enough memory for the allocation to
  993. * succeed and -ENOMEM implies there is not.
  994. *
  995. * We currently support three overcommit policies, which are set via the
  996. * vm.overcommit_memory sysctl. See Documentation/vm/overcommit-accounting
  997. *
  998. * Strict overcommit modes added 2002 Feb 26 by Alan Cox.
  999. * Additional code 2002 Jul 20 by Robert Love.
  1000. *
  1001. * cap_sys_admin is 1 if the process has admin privileges, 0 otherwise.
  1002. *
  1003. * Note this is a helper function intended to be used by LSMs which
  1004. * wish to use this logic.
  1005. */
  1006. int __vm_enough_memory(long pages, int cap_sys_admin)
  1007. {
  1008. unsigned long free, allowed;
  1009. vm_acct_memory(pages);
  1010. /*
  1011. * Sometimes we want to use more memory than we have
  1012. */
  1013. if (sysctl_overcommit_memory == OVERCOMMIT_ALWAYS)
  1014. return 0;
  1015. if (sysctl_overcommit_memory == OVERCOMMIT_GUESS) {
  1016. unsigned long n;
  1017. free = global_page_state(NR_FILE_PAGES);
  1018. free += nr_swap_pages;
  1019. /*
  1020. * Any slabs which are created with the
  1021. * SLAB_RECLAIM_ACCOUNT flag claim to have contents
  1022. * which are reclaimable, under pressure. The dentry
  1023. * cache and most inode caches should fall into this
  1024. */
  1025. free += global_page_state(NR_SLAB_RECLAIMABLE);
  1026. /*
  1027. * Leave the last 3% for root
  1028. */
  1029. if (!cap_sys_admin)
  1030. free -= free / 32;
  1031. if (free > pages)
  1032. return 0;
  1033. /*
  1034. * nr_free_pages() is very expensive on large systems,
  1035. * only call if we're about to fail.
  1036. */
  1037. n = nr_free_pages();
  1038. /*
  1039. * Leave reserved pages. The pages are not for anonymous pages.
  1040. */
  1041. if (n <= totalreserve_pages)
  1042. goto error;
  1043. else
  1044. n -= totalreserve_pages;
  1045. /*
  1046. * Leave the last 3% for root
  1047. */
  1048. if (!cap_sys_admin)
  1049. n -= n / 32;
  1050. free += n;
  1051. if (free > pages)
  1052. return 0;
  1053. goto error;
  1054. }
  1055. allowed = totalram_pages * sysctl_overcommit_ratio / 100;
  1056. /*
  1057. * Leave the last 3% for root
  1058. */
  1059. if (!cap_sys_admin)
  1060. allowed -= allowed / 32;
  1061. allowed += total_swap_pages;
  1062. /* Don't let a single process grow too big:
  1063. leave 3% of the size of this process for other processes */
  1064. allowed -= current->mm->total_vm / 32;
  1065. /*
  1066. * cast `allowed' as a signed long because vm_committed_space
  1067. * sometimes has a negative value
  1068. */
  1069. if (atomic_read(&vm_committed_space) < (long)allowed)
  1070. return 0;
  1071. error:
  1072. vm_unacct_memory(pages);
  1073. return -ENOMEM;
  1074. }
  1075. int in_gate_area_no_task(unsigned long addr)
  1076. {
  1077. return 0;
  1078. }
  1079. struct page *filemap_nopage(struct vm_area_struct *area,
  1080. unsigned long address, int *type)
  1081. {
  1082. BUG();
  1083. return NULL;
  1084. }
  1085. /*
  1086. * Access another process' address space.
  1087. * - source/target buffer must be kernel space
  1088. */
  1089. int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write)
  1090. {
  1091. struct vm_area_struct *vma;
  1092. struct mm_struct *mm;
  1093. if (addr + len < addr)
  1094. return 0;
  1095. mm = get_task_mm(tsk);
  1096. if (!mm)
  1097. return 0;
  1098. down_read(&mm->mmap_sem);
  1099. /* the access must start within one of the target process's mappings */
  1100. vma = find_vma(mm, addr);
  1101. if (vma) {
  1102. /* don't overrun this mapping */
  1103. if (addr + len >= vma->vm_end)
  1104. len = vma->vm_end - addr;
  1105. /* only read or write mappings where it is permitted */
  1106. if (write && vma->vm_flags & VM_MAYWRITE)
  1107. len -= copy_to_user((void *) addr, buf, len);
  1108. else if (!write && vma->vm_flags & VM_MAYREAD)
  1109. len -= copy_from_user(buf, (void *) addr, len);
  1110. else
  1111. len = 0;
  1112. } else {
  1113. len = 0;
  1114. }
  1115. up_read(&mm->mmap_sem);
  1116. mmput(mm);
  1117. return len;
  1118. }