nommu.c 28 KB

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