nommu.c 32 KB

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