mem.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993
  1. /*
  2. * linux/drivers/char/mem.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. *
  6. * Added devfs support.
  7. * Jan-11-1998, C. Scott Ananian <cananian@alumni.princeton.edu>
  8. * Shared /dev/zero mmaping support, Feb 2000, Kanoj Sarcar <kanoj@sgi.com>
  9. */
  10. #include <linux/mm.h>
  11. #include <linux/miscdevice.h>
  12. #include <linux/slab.h>
  13. #include <linux/vmalloc.h>
  14. #include <linux/mman.h>
  15. #include <linux/random.h>
  16. #include <linux/init.h>
  17. #include <linux/raw.h>
  18. #include <linux/tty.h>
  19. #include <linux/capability.h>
  20. #include <linux/ptrace.h>
  21. #include <linux/device.h>
  22. #include <linux/highmem.h>
  23. #include <linux/crash_dump.h>
  24. #include <linux/backing-dev.h>
  25. #include <linux/bootmem.h>
  26. #include <linux/pipe_fs_i.h>
  27. #include <linux/pfn.h>
  28. #include <asm/uaccess.h>
  29. #include <asm/io.h>
  30. #ifdef CONFIG_IA64
  31. # include <linux/efi.h>
  32. #endif
  33. /*
  34. * Architectures vary in how they handle caching for addresses
  35. * outside of main memory.
  36. *
  37. */
  38. static inline int uncached_access(struct file *file, unsigned long addr)
  39. {
  40. #if defined(__i386__)
  41. /*
  42. * On the PPro and successors, the MTRRs are used to set
  43. * memory types for physical addresses outside main memory,
  44. * so blindly setting PCD or PWT on those pages is wrong.
  45. * For Pentiums and earlier, the surround logic should disable
  46. * caching for the high addresses through the KEN pin, but
  47. * we maintain the tradition of paranoia in this code.
  48. */
  49. if (file->f_flags & O_SYNC)
  50. return 1;
  51. return !( test_bit(X86_FEATURE_MTRR, boot_cpu_data.x86_capability) ||
  52. test_bit(X86_FEATURE_K6_MTRR, boot_cpu_data.x86_capability) ||
  53. test_bit(X86_FEATURE_CYRIX_ARR, boot_cpu_data.x86_capability) ||
  54. test_bit(X86_FEATURE_CENTAUR_MCR, boot_cpu_data.x86_capability) )
  55. && addr >= __pa(high_memory);
  56. #elif defined(__x86_64__)
  57. /*
  58. * This is broken because it can generate memory type aliases,
  59. * which can cause cache corruptions
  60. * But it is only available for root and we have to be bug-to-bug
  61. * compatible with i386.
  62. */
  63. if (file->f_flags & O_SYNC)
  64. return 1;
  65. /* same behaviour as i386. PAT always set to cached and MTRRs control the
  66. caching behaviour.
  67. Hopefully a full PAT implementation will fix that soon. */
  68. return 0;
  69. #elif defined(CONFIG_IA64)
  70. /*
  71. * On ia64, we ignore O_SYNC because we cannot tolerate memory attribute aliases.
  72. */
  73. return !(efi_mem_attributes(addr) & EFI_MEMORY_WB);
  74. #else
  75. /*
  76. * Accessing memory above the top the kernel knows about or through a file pointer
  77. * that was marked O_SYNC will be done non-cached.
  78. */
  79. if (file->f_flags & O_SYNC)
  80. return 1;
  81. return addr >= __pa(high_memory);
  82. #endif
  83. }
  84. #ifndef ARCH_HAS_VALID_PHYS_ADDR_RANGE
  85. static inline int valid_phys_addr_range(unsigned long addr, size_t count)
  86. {
  87. if (addr + count > __pa(high_memory))
  88. return 0;
  89. return 1;
  90. }
  91. static inline int valid_mmap_phys_addr_range(unsigned long pfn, size_t size)
  92. {
  93. return 1;
  94. }
  95. #endif
  96. /*
  97. * This funcion reads the *physical* memory. The f_pos points directly to the
  98. * memory location.
  99. */
  100. static ssize_t read_mem(struct file * file, char __user * buf,
  101. size_t count, loff_t *ppos)
  102. {
  103. unsigned long p = *ppos;
  104. ssize_t read, sz;
  105. char *ptr;
  106. if (!valid_phys_addr_range(p, count))
  107. return -EFAULT;
  108. read = 0;
  109. #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
  110. /* we don't have page 0 mapped on sparc and m68k.. */
  111. if (p < PAGE_SIZE) {
  112. sz = PAGE_SIZE - p;
  113. if (sz > count)
  114. sz = count;
  115. if (sz > 0) {
  116. if (clear_user(buf, sz))
  117. return -EFAULT;
  118. buf += sz;
  119. p += sz;
  120. count -= sz;
  121. read += sz;
  122. }
  123. }
  124. #endif
  125. while (count > 0) {
  126. /*
  127. * Handle first page in case it's not aligned
  128. */
  129. if (-p & (PAGE_SIZE - 1))
  130. sz = -p & (PAGE_SIZE - 1);
  131. else
  132. sz = PAGE_SIZE;
  133. sz = min_t(unsigned long, sz, count);
  134. /*
  135. * On ia64 if a page has been mapped somewhere as
  136. * uncached, then it must also be accessed uncached
  137. * by the kernel or data corruption may occur
  138. */
  139. ptr = xlate_dev_mem_ptr(p);
  140. if (copy_to_user(buf, ptr, sz))
  141. return -EFAULT;
  142. buf += sz;
  143. p += sz;
  144. count -= sz;
  145. read += sz;
  146. }
  147. *ppos += read;
  148. return read;
  149. }
  150. static ssize_t write_mem(struct file * file, const char __user * buf,
  151. size_t count, loff_t *ppos)
  152. {
  153. unsigned long p = *ppos;
  154. ssize_t written, sz;
  155. unsigned long copied;
  156. void *ptr;
  157. if (!valid_phys_addr_range(p, count))
  158. return -EFAULT;
  159. written = 0;
  160. #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
  161. /* we don't have page 0 mapped on sparc and m68k.. */
  162. if (p < PAGE_SIZE) {
  163. unsigned long sz = PAGE_SIZE - p;
  164. if (sz > count)
  165. sz = count;
  166. /* Hmm. Do something? */
  167. buf += sz;
  168. p += sz;
  169. count -= sz;
  170. written += sz;
  171. }
  172. #endif
  173. while (count > 0) {
  174. /*
  175. * Handle first page in case it's not aligned
  176. */
  177. if (-p & (PAGE_SIZE - 1))
  178. sz = -p & (PAGE_SIZE - 1);
  179. else
  180. sz = PAGE_SIZE;
  181. sz = min_t(unsigned long, sz, count);
  182. /*
  183. * On ia64 if a page has been mapped somewhere as
  184. * uncached, then it must also be accessed uncached
  185. * by the kernel or data corruption may occur
  186. */
  187. ptr = xlate_dev_mem_ptr(p);
  188. copied = copy_from_user(ptr, buf, sz);
  189. if (copied) {
  190. written += sz - copied;
  191. if (written)
  192. break;
  193. return -EFAULT;
  194. }
  195. buf += sz;
  196. p += sz;
  197. count -= sz;
  198. written += sz;
  199. }
  200. *ppos += written;
  201. return written;
  202. }
  203. #ifndef __HAVE_PHYS_MEM_ACCESS_PROT
  204. static pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
  205. unsigned long size, pgprot_t vma_prot)
  206. {
  207. #ifdef pgprot_noncached
  208. unsigned long offset = pfn << PAGE_SHIFT;
  209. if (uncached_access(file, offset))
  210. return pgprot_noncached(vma_prot);
  211. #endif
  212. return vma_prot;
  213. }
  214. #endif
  215. #ifndef CONFIG_MMU
  216. static unsigned long get_unmapped_area_mem(struct file *file,
  217. unsigned long addr,
  218. unsigned long len,
  219. unsigned long pgoff,
  220. unsigned long flags)
  221. {
  222. if (!valid_mmap_phys_addr_range(pgoff, len))
  223. return (unsigned long) -EINVAL;
  224. return pgoff << PAGE_SHIFT;
  225. }
  226. /* can't do an in-place private mapping if there's no MMU */
  227. static inline int private_mapping_ok(struct vm_area_struct *vma)
  228. {
  229. return vma->vm_flags & VM_MAYSHARE;
  230. }
  231. #else
  232. #define get_unmapped_area_mem NULL
  233. static inline int private_mapping_ok(struct vm_area_struct *vma)
  234. {
  235. return 1;
  236. }
  237. #endif
  238. static int mmap_mem(struct file * file, struct vm_area_struct * vma)
  239. {
  240. size_t size = vma->vm_end - vma->vm_start;
  241. if (!valid_mmap_phys_addr_range(vma->vm_pgoff, size))
  242. return -EINVAL;
  243. if (!private_mapping_ok(vma))
  244. return -ENOSYS;
  245. vma->vm_page_prot = phys_mem_access_prot(file, vma->vm_pgoff,
  246. size,
  247. vma->vm_page_prot);
  248. /* Remap-pfn-range will mark the range VM_IO and VM_RESERVED */
  249. if (remap_pfn_range(vma,
  250. vma->vm_start,
  251. vma->vm_pgoff,
  252. size,
  253. vma->vm_page_prot))
  254. return -EAGAIN;
  255. return 0;
  256. }
  257. static int mmap_kmem(struct file * file, struct vm_area_struct * vma)
  258. {
  259. unsigned long pfn;
  260. /* Turn a kernel-virtual address into a physical page frame */
  261. pfn = __pa((u64)vma->vm_pgoff << PAGE_SHIFT) >> PAGE_SHIFT;
  262. /*
  263. * RED-PEN: on some architectures there is more mapped memory
  264. * than available in mem_map which pfn_valid checks
  265. * for. Perhaps should add a new macro here.
  266. *
  267. * RED-PEN: vmalloc is not supported right now.
  268. */
  269. if (!pfn_valid(pfn))
  270. return -EIO;
  271. vma->vm_pgoff = pfn;
  272. return mmap_mem(file, vma);
  273. }
  274. #ifdef CONFIG_CRASH_DUMP
  275. /*
  276. * Read memory corresponding to the old kernel.
  277. */
  278. static ssize_t read_oldmem(struct file *file, char __user *buf,
  279. size_t count, loff_t *ppos)
  280. {
  281. unsigned long pfn, offset;
  282. size_t read = 0, csize;
  283. int rc = 0;
  284. while (count) {
  285. pfn = *ppos / PAGE_SIZE;
  286. if (pfn > saved_max_pfn)
  287. return read;
  288. offset = (unsigned long)(*ppos % PAGE_SIZE);
  289. if (count > PAGE_SIZE - offset)
  290. csize = PAGE_SIZE - offset;
  291. else
  292. csize = count;
  293. rc = copy_oldmem_page(pfn, buf, csize, offset, 1);
  294. if (rc < 0)
  295. return rc;
  296. buf += csize;
  297. *ppos += csize;
  298. read += csize;
  299. count -= csize;
  300. }
  301. return read;
  302. }
  303. #endif
  304. extern long vread(char *buf, char *addr, unsigned long count);
  305. extern long vwrite(char *buf, char *addr, unsigned long count);
  306. /*
  307. * This function reads the *virtual* memory as seen by the kernel.
  308. */
  309. static ssize_t read_kmem(struct file *file, char __user *buf,
  310. size_t count, loff_t *ppos)
  311. {
  312. unsigned long p = *ppos;
  313. ssize_t low_count, read, sz;
  314. char * kbuf; /* k-addr because vread() takes vmlist_lock rwlock */
  315. read = 0;
  316. if (p < (unsigned long) high_memory) {
  317. low_count = count;
  318. if (count > (unsigned long) high_memory - p)
  319. low_count = (unsigned long) high_memory - p;
  320. #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
  321. /* we don't have page 0 mapped on sparc and m68k.. */
  322. if (p < PAGE_SIZE && low_count > 0) {
  323. size_t tmp = PAGE_SIZE - p;
  324. if (tmp > low_count) tmp = low_count;
  325. if (clear_user(buf, tmp))
  326. return -EFAULT;
  327. buf += tmp;
  328. p += tmp;
  329. read += tmp;
  330. low_count -= tmp;
  331. count -= tmp;
  332. }
  333. #endif
  334. while (low_count > 0) {
  335. /*
  336. * Handle first page in case it's not aligned
  337. */
  338. if (-p & (PAGE_SIZE - 1))
  339. sz = -p & (PAGE_SIZE - 1);
  340. else
  341. sz = PAGE_SIZE;
  342. sz = min_t(unsigned long, sz, low_count);
  343. /*
  344. * On ia64 if a page has been mapped somewhere as
  345. * uncached, then it must also be accessed uncached
  346. * by the kernel or data corruption may occur
  347. */
  348. kbuf = xlate_dev_kmem_ptr((char *)p);
  349. if (copy_to_user(buf, kbuf, sz))
  350. return -EFAULT;
  351. buf += sz;
  352. p += sz;
  353. read += sz;
  354. low_count -= sz;
  355. count -= sz;
  356. }
  357. }
  358. if (count > 0) {
  359. kbuf = (char *)__get_free_page(GFP_KERNEL);
  360. if (!kbuf)
  361. return -ENOMEM;
  362. while (count > 0) {
  363. int len = count;
  364. if (len > PAGE_SIZE)
  365. len = PAGE_SIZE;
  366. len = vread(kbuf, (char *)p, len);
  367. if (!len)
  368. break;
  369. if (copy_to_user(buf, kbuf, len)) {
  370. free_page((unsigned long)kbuf);
  371. return -EFAULT;
  372. }
  373. count -= len;
  374. buf += len;
  375. read += len;
  376. p += len;
  377. }
  378. free_page((unsigned long)kbuf);
  379. }
  380. *ppos = p;
  381. return read;
  382. }
  383. static inline ssize_t
  384. do_write_kmem(void *p, unsigned long realp, const char __user * buf,
  385. size_t count, loff_t *ppos)
  386. {
  387. ssize_t written, sz;
  388. unsigned long copied;
  389. written = 0;
  390. #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
  391. /* we don't have page 0 mapped on sparc and m68k.. */
  392. if (realp < PAGE_SIZE) {
  393. unsigned long sz = PAGE_SIZE - realp;
  394. if (sz > count)
  395. sz = count;
  396. /* Hmm. Do something? */
  397. buf += sz;
  398. p += sz;
  399. realp += sz;
  400. count -= sz;
  401. written += sz;
  402. }
  403. #endif
  404. while (count > 0) {
  405. char *ptr;
  406. /*
  407. * Handle first page in case it's not aligned
  408. */
  409. if (-realp & (PAGE_SIZE - 1))
  410. sz = -realp & (PAGE_SIZE - 1);
  411. else
  412. sz = PAGE_SIZE;
  413. sz = min_t(unsigned long, sz, count);
  414. /*
  415. * On ia64 if a page has been mapped somewhere as
  416. * uncached, then it must also be accessed uncached
  417. * by the kernel or data corruption may occur
  418. */
  419. ptr = xlate_dev_kmem_ptr(p);
  420. copied = copy_from_user(ptr, buf, sz);
  421. if (copied) {
  422. written += sz - copied;
  423. if (written)
  424. break;
  425. return -EFAULT;
  426. }
  427. buf += sz;
  428. p += sz;
  429. realp += sz;
  430. count -= sz;
  431. written += sz;
  432. }
  433. *ppos += written;
  434. return written;
  435. }
  436. /*
  437. * This function writes to the *virtual* memory as seen by the kernel.
  438. */
  439. static ssize_t write_kmem(struct file * file, const char __user * buf,
  440. size_t count, loff_t *ppos)
  441. {
  442. unsigned long p = *ppos;
  443. ssize_t wrote = 0;
  444. ssize_t virtr = 0;
  445. ssize_t written;
  446. char * kbuf; /* k-addr because vwrite() takes vmlist_lock rwlock */
  447. if (p < (unsigned long) high_memory) {
  448. wrote = count;
  449. if (count > (unsigned long) high_memory - p)
  450. wrote = (unsigned long) high_memory - p;
  451. written = do_write_kmem((void*)p, p, buf, wrote, ppos);
  452. if (written != wrote)
  453. return written;
  454. wrote = written;
  455. p += wrote;
  456. buf += wrote;
  457. count -= wrote;
  458. }
  459. if (count > 0) {
  460. kbuf = (char *)__get_free_page(GFP_KERNEL);
  461. if (!kbuf)
  462. return wrote ? wrote : -ENOMEM;
  463. while (count > 0) {
  464. int len = count;
  465. if (len > PAGE_SIZE)
  466. len = PAGE_SIZE;
  467. if (len) {
  468. written = copy_from_user(kbuf, buf, len);
  469. if (written) {
  470. if (wrote + virtr)
  471. break;
  472. free_page((unsigned long)kbuf);
  473. return -EFAULT;
  474. }
  475. }
  476. len = vwrite(kbuf, (char *)p, len);
  477. count -= len;
  478. buf += len;
  479. virtr += len;
  480. p += len;
  481. }
  482. free_page((unsigned long)kbuf);
  483. }
  484. *ppos = p;
  485. return virtr + wrote;
  486. }
  487. #ifdef CONFIG_DEVPORT
  488. static ssize_t read_port(struct file * file, char __user * buf,
  489. size_t count, loff_t *ppos)
  490. {
  491. unsigned long i = *ppos;
  492. char __user *tmp = buf;
  493. if (!access_ok(VERIFY_WRITE, buf, count))
  494. return -EFAULT;
  495. while (count-- > 0 && i < 65536) {
  496. if (__put_user(inb(i),tmp) < 0)
  497. return -EFAULT;
  498. i++;
  499. tmp++;
  500. }
  501. *ppos = i;
  502. return tmp-buf;
  503. }
  504. static ssize_t write_port(struct file * file, const char __user * buf,
  505. size_t count, loff_t *ppos)
  506. {
  507. unsigned long i = *ppos;
  508. const char __user * tmp = buf;
  509. if (!access_ok(VERIFY_READ,buf,count))
  510. return -EFAULT;
  511. while (count-- > 0 && i < 65536) {
  512. char c;
  513. if (__get_user(c, tmp)) {
  514. if (tmp > buf)
  515. break;
  516. return -EFAULT;
  517. }
  518. outb(c,i);
  519. i++;
  520. tmp++;
  521. }
  522. *ppos = i;
  523. return tmp-buf;
  524. }
  525. #endif
  526. static ssize_t read_null(struct file * file, char __user * buf,
  527. size_t count, loff_t *ppos)
  528. {
  529. return 0;
  530. }
  531. static ssize_t write_null(struct file * file, const char __user * buf,
  532. size_t count, loff_t *ppos)
  533. {
  534. return count;
  535. }
  536. static int pipe_to_null(struct pipe_inode_info *info, struct pipe_buffer *buf,
  537. struct splice_desc *sd)
  538. {
  539. return sd->len;
  540. }
  541. static ssize_t splice_write_null(struct pipe_inode_info *pipe,struct file *out,
  542. loff_t *ppos, size_t len, unsigned int flags)
  543. {
  544. return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_null);
  545. }
  546. #ifdef CONFIG_MMU
  547. /*
  548. * For fun, we are using the MMU for this.
  549. */
  550. static inline size_t read_zero_pagealigned(char __user * buf, size_t size)
  551. {
  552. struct mm_struct *mm;
  553. struct vm_area_struct * vma;
  554. unsigned long addr=(unsigned long)buf;
  555. mm = current->mm;
  556. /* Oops, this was forgotten before. -ben */
  557. down_read(&mm->mmap_sem);
  558. /* For private mappings, just map in zero pages. */
  559. for (vma = find_vma(mm, addr); vma; vma = vma->vm_next) {
  560. unsigned long count;
  561. if (vma->vm_start > addr || (vma->vm_flags & VM_WRITE) == 0)
  562. goto out_up;
  563. if (vma->vm_flags & (VM_SHARED | VM_HUGETLB))
  564. break;
  565. count = vma->vm_end - addr;
  566. if (count > size)
  567. count = size;
  568. zap_page_range(vma, addr, count, NULL);
  569. if (zeromap_page_range(vma, addr, count, PAGE_COPY))
  570. break;
  571. size -= count;
  572. buf += count;
  573. addr += count;
  574. if (size == 0)
  575. goto out_up;
  576. }
  577. up_read(&mm->mmap_sem);
  578. /* The shared case is hard. Let's do the conventional zeroing. */
  579. do {
  580. unsigned long unwritten = clear_user(buf, PAGE_SIZE);
  581. if (unwritten)
  582. return size + unwritten - PAGE_SIZE;
  583. cond_resched();
  584. buf += PAGE_SIZE;
  585. size -= PAGE_SIZE;
  586. } while (size);
  587. return size;
  588. out_up:
  589. up_read(&mm->mmap_sem);
  590. return size;
  591. }
  592. static ssize_t read_zero(struct file * file, char __user * buf,
  593. size_t count, loff_t *ppos)
  594. {
  595. unsigned long left, unwritten, written = 0;
  596. if (!count)
  597. return 0;
  598. if (!access_ok(VERIFY_WRITE, buf, count))
  599. return -EFAULT;
  600. left = count;
  601. /* do we want to be clever? Arbitrary cut-off */
  602. if (count >= PAGE_SIZE*4) {
  603. unsigned long partial;
  604. /* How much left of the page? */
  605. partial = (PAGE_SIZE-1) & -(unsigned long) buf;
  606. unwritten = clear_user(buf, partial);
  607. written = partial - unwritten;
  608. if (unwritten)
  609. goto out;
  610. left -= partial;
  611. buf += partial;
  612. unwritten = read_zero_pagealigned(buf, left & PAGE_MASK);
  613. written += (left & PAGE_MASK) - unwritten;
  614. if (unwritten)
  615. goto out;
  616. buf += left & PAGE_MASK;
  617. left &= ~PAGE_MASK;
  618. }
  619. unwritten = clear_user(buf, left);
  620. written += left - unwritten;
  621. out:
  622. return written ? written : -EFAULT;
  623. }
  624. static int mmap_zero(struct file * file, struct vm_area_struct * vma)
  625. {
  626. int err;
  627. if (vma->vm_flags & VM_SHARED)
  628. return shmem_zero_setup(vma);
  629. err = zeromap_page_range(vma, vma->vm_start,
  630. vma->vm_end - vma->vm_start, vma->vm_page_prot);
  631. BUG_ON(err == -EEXIST);
  632. return err;
  633. }
  634. #else /* CONFIG_MMU */
  635. static ssize_t read_zero(struct file * file, char * buf,
  636. size_t count, loff_t *ppos)
  637. {
  638. size_t todo = count;
  639. while (todo) {
  640. size_t chunk = todo;
  641. if (chunk > 4096)
  642. chunk = 4096; /* Just for latency reasons */
  643. if (clear_user(buf, chunk))
  644. return -EFAULT;
  645. buf += chunk;
  646. todo -= chunk;
  647. cond_resched();
  648. }
  649. return count;
  650. }
  651. static int mmap_zero(struct file * file, struct vm_area_struct * vma)
  652. {
  653. return -ENOSYS;
  654. }
  655. #endif /* CONFIG_MMU */
  656. static ssize_t write_full(struct file * file, const char __user * buf,
  657. size_t count, loff_t *ppos)
  658. {
  659. return -ENOSPC;
  660. }
  661. /*
  662. * Special lseek() function for /dev/null and /dev/zero. Most notably, you
  663. * can fopen() both devices with "a" now. This was previously impossible.
  664. * -- SRB.
  665. */
  666. static loff_t null_lseek(struct file * file, loff_t offset, int orig)
  667. {
  668. return file->f_pos = 0;
  669. }
  670. /*
  671. * The memory devices use the full 32/64 bits of the offset, and so we cannot
  672. * check against negative addresses: they are ok. The return value is weird,
  673. * though, in that case (0).
  674. *
  675. * also note that seeking relative to the "end of file" isn't supported:
  676. * it has no meaning, so it returns -EINVAL.
  677. */
  678. static loff_t memory_lseek(struct file * file, loff_t offset, int orig)
  679. {
  680. loff_t ret;
  681. mutex_lock(&file->f_path.dentry->d_inode->i_mutex);
  682. switch (orig) {
  683. case 0:
  684. file->f_pos = offset;
  685. ret = file->f_pos;
  686. force_successful_syscall_return();
  687. break;
  688. case 1:
  689. file->f_pos += offset;
  690. ret = file->f_pos;
  691. force_successful_syscall_return();
  692. break;
  693. default:
  694. ret = -EINVAL;
  695. }
  696. mutex_unlock(&file->f_path.dentry->d_inode->i_mutex);
  697. return ret;
  698. }
  699. static int open_port(struct inode * inode, struct file * filp)
  700. {
  701. return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
  702. }
  703. #define zero_lseek null_lseek
  704. #define full_lseek null_lseek
  705. #define write_zero write_null
  706. #define read_full read_zero
  707. #define open_mem open_port
  708. #define open_kmem open_mem
  709. #define open_oldmem open_mem
  710. static const struct file_operations mem_fops = {
  711. .llseek = memory_lseek,
  712. .read = read_mem,
  713. .write = write_mem,
  714. .mmap = mmap_mem,
  715. .open = open_mem,
  716. .get_unmapped_area = get_unmapped_area_mem,
  717. };
  718. static const struct file_operations kmem_fops = {
  719. .llseek = memory_lseek,
  720. .read = read_kmem,
  721. .write = write_kmem,
  722. .mmap = mmap_kmem,
  723. .open = open_kmem,
  724. .get_unmapped_area = get_unmapped_area_mem,
  725. };
  726. static const struct file_operations null_fops = {
  727. .llseek = null_lseek,
  728. .read = read_null,
  729. .write = write_null,
  730. .splice_write = splice_write_null,
  731. };
  732. #ifdef CONFIG_DEVPORT
  733. static const struct file_operations port_fops = {
  734. .llseek = memory_lseek,
  735. .read = read_port,
  736. .write = write_port,
  737. .open = open_port,
  738. };
  739. #endif
  740. static const struct file_operations zero_fops = {
  741. .llseek = zero_lseek,
  742. .read = read_zero,
  743. .write = write_zero,
  744. .mmap = mmap_zero,
  745. };
  746. /*
  747. * capabilities for /dev/zero
  748. * - permits private mappings, "copies" are taken of the source of zeros
  749. */
  750. static struct backing_dev_info zero_bdi = {
  751. .capabilities = BDI_CAP_MAP_COPY,
  752. };
  753. static const struct file_operations full_fops = {
  754. .llseek = full_lseek,
  755. .read = read_full,
  756. .write = write_full,
  757. };
  758. #ifdef CONFIG_CRASH_DUMP
  759. static const struct file_operations oldmem_fops = {
  760. .read = read_oldmem,
  761. .open = open_oldmem,
  762. };
  763. #endif
  764. static ssize_t kmsg_write(struct file * file, const char __user * buf,
  765. size_t count, loff_t *ppos)
  766. {
  767. char *tmp;
  768. ssize_t ret;
  769. tmp = kmalloc(count + 1, GFP_KERNEL);
  770. if (tmp == NULL)
  771. return -ENOMEM;
  772. ret = -EFAULT;
  773. if (!copy_from_user(tmp, buf, count)) {
  774. tmp[count] = 0;
  775. ret = printk("%s", tmp);
  776. if (ret > count)
  777. /* printk can add a prefix */
  778. ret = count;
  779. }
  780. kfree(tmp);
  781. return ret;
  782. }
  783. static const struct file_operations kmsg_fops = {
  784. .write = kmsg_write,
  785. };
  786. static int memory_open(struct inode * inode, struct file * filp)
  787. {
  788. switch (iminor(inode)) {
  789. case 1:
  790. filp->f_op = &mem_fops;
  791. filp->f_mapping->backing_dev_info =
  792. &directly_mappable_cdev_bdi;
  793. break;
  794. case 2:
  795. filp->f_op = &kmem_fops;
  796. filp->f_mapping->backing_dev_info =
  797. &directly_mappable_cdev_bdi;
  798. break;
  799. case 3:
  800. filp->f_op = &null_fops;
  801. break;
  802. #ifdef CONFIG_DEVPORT
  803. case 4:
  804. filp->f_op = &port_fops;
  805. break;
  806. #endif
  807. case 5:
  808. filp->f_mapping->backing_dev_info = &zero_bdi;
  809. filp->f_op = &zero_fops;
  810. break;
  811. case 7:
  812. filp->f_op = &full_fops;
  813. break;
  814. case 8:
  815. filp->f_op = &random_fops;
  816. break;
  817. case 9:
  818. filp->f_op = &urandom_fops;
  819. break;
  820. case 11:
  821. filp->f_op = &kmsg_fops;
  822. break;
  823. #ifdef CONFIG_CRASH_DUMP
  824. case 12:
  825. filp->f_op = &oldmem_fops;
  826. break;
  827. #endif
  828. default:
  829. return -ENXIO;
  830. }
  831. if (filp->f_op && filp->f_op->open)
  832. return filp->f_op->open(inode,filp);
  833. return 0;
  834. }
  835. static const struct file_operations memory_fops = {
  836. .open = memory_open, /* just a selector for the real open */
  837. };
  838. static const struct {
  839. unsigned int minor;
  840. char *name;
  841. umode_t mode;
  842. const struct file_operations *fops;
  843. } devlist[] = { /* list of minor devices */
  844. {1, "mem", S_IRUSR | S_IWUSR | S_IRGRP, &mem_fops},
  845. {2, "kmem", S_IRUSR | S_IWUSR | S_IRGRP, &kmem_fops},
  846. {3, "null", S_IRUGO | S_IWUGO, &null_fops},
  847. #ifdef CONFIG_DEVPORT
  848. {4, "port", S_IRUSR | S_IWUSR | S_IRGRP, &port_fops},
  849. #endif
  850. {5, "zero", S_IRUGO | S_IWUGO, &zero_fops},
  851. {7, "full", S_IRUGO | S_IWUGO, &full_fops},
  852. {8, "random", S_IRUGO | S_IWUSR, &random_fops},
  853. {9, "urandom", S_IRUGO | S_IWUSR, &urandom_fops},
  854. {11,"kmsg", S_IRUGO | S_IWUSR, &kmsg_fops},
  855. #ifdef CONFIG_CRASH_DUMP
  856. {12,"oldmem", S_IRUSR | S_IWUSR | S_IRGRP, &oldmem_fops},
  857. #endif
  858. };
  859. static struct class *mem_class;
  860. static int __init chr_dev_init(void)
  861. {
  862. int i;
  863. if (register_chrdev(MEM_MAJOR,"mem",&memory_fops))
  864. printk("unable to get major %d for memory devs\n", MEM_MAJOR);
  865. mem_class = class_create(THIS_MODULE, "mem");
  866. for (i = 0; i < ARRAY_SIZE(devlist); i++)
  867. device_create(mem_class, NULL,
  868. MKDEV(MEM_MAJOR, devlist[i].minor),
  869. devlist[i].name);
  870. return 0;
  871. }
  872. fs_initcall(chr_dev_init);