nommu.c 28 KB

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