nommu.c 29 KB

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