mem.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907
  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 mmapping 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/backing-dev.h>
  24. #include <linux/splice.h>
  25. #include <linux/pfn.h>
  26. #include <linux/export.h>
  27. #include <linux/io.h>
  28. #include <linux/aio.h>
  29. #include <asm/uaccess.h>
  30. #ifdef CONFIG_IA64
  31. # include <linux/efi.h>
  32. #endif
  33. #define DEVPORT_MINOR 4
  34. static inline unsigned long size_inside_page(unsigned long start,
  35. unsigned long size)
  36. {
  37. unsigned long sz;
  38. sz = PAGE_SIZE - (start & (PAGE_SIZE - 1));
  39. return min(sz, size);
  40. }
  41. #ifndef ARCH_HAS_VALID_PHYS_ADDR_RANGE
  42. static inline int valid_phys_addr_range(phys_addr_t addr, size_t count)
  43. {
  44. return addr + count <= __pa(high_memory);
  45. }
  46. static inline int valid_mmap_phys_addr_range(unsigned long pfn, size_t size)
  47. {
  48. return 1;
  49. }
  50. #endif
  51. #ifdef CONFIG_STRICT_DEVMEM
  52. static inline int range_is_allowed(unsigned long pfn, unsigned long size)
  53. {
  54. u64 from = ((u64)pfn) << PAGE_SHIFT;
  55. u64 to = from + size;
  56. u64 cursor = from;
  57. while (cursor < to) {
  58. if (!devmem_is_allowed(pfn)) {
  59. printk(KERN_INFO
  60. "Program %s tried to access /dev/mem between %Lx->%Lx.\n",
  61. current->comm, from, to);
  62. return 0;
  63. }
  64. cursor += PAGE_SIZE;
  65. pfn++;
  66. }
  67. return 1;
  68. }
  69. #else
  70. static inline int range_is_allowed(unsigned long pfn, unsigned long size)
  71. {
  72. return 1;
  73. }
  74. #endif
  75. void __weak unxlate_dev_mem_ptr(unsigned long phys, void *addr)
  76. {
  77. }
  78. /*
  79. * This funcion reads the *physical* memory. The f_pos points directly to the
  80. * memory location.
  81. */
  82. static ssize_t read_mem(struct file *file, char __user *buf,
  83. size_t count, loff_t *ppos)
  84. {
  85. phys_addr_t p = *ppos;
  86. ssize_t read, sz;
  87. char *ptr;
  88. if (p != *ppos)
  89. return 0;
  90. if (!valid_phys_addr_range(p, count))
  91. return -EFAULT;
  92. read = 0;
  93. #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
  94. /* we don't have page 0 mapped on sparc and m68k.. */
  95. if (p < PAGE_SIZE) {
  96. sz = size_inside_page(p, count);
  97. if (sz > 0) {
  98. if (clear_user(buf, sz))
  99. return -EFAULT;
  100. buf += sz;
  101. p += sz;
  102. count -= sz;
  103. read += sz;
  104. }
  105. }
  106. #endif
  107. while (count > 0) {
  108. unsigned long remaining;
  109. sz = size_inside_page(p, count);
  110. if (!range_is_allowed(p >> PAGE_SHIFT, count))
  111. return -EPERM;
  112. /*
  113. * On ia64 if a page has been mapped somewhere as uncached, then
  114. * it must also be accessed uncached by the kernel or data
  115. * corruption may occur.
  116. */
  117. ptr = xlate_dev_mem_ptr(p);
  118. if (!ptr)
  119. return -EFAULT;
  120. remaining = copy_to_user(buf, ptr, sz);
  121. unxlate_dev_mem_ptr(p, ptr);
  122. if (remaining)
  123. return -EFAULT;
  124. buf += sz;
  125. p += sz;
  126. count -= sz;
  127. read += sz;
  128. }
  129. *ppos += read;
  130. return read;
  131. }
  132. static ssize_t write_mem(struct file *file, const char __user *buf,
  133. size_t count, loff_t *ppos)
  134. {
  135. phys_addr_t p = *ppos;
  136. ssize_t written, sz;
  137. unsigned long copied;
  138. void *ptr;
  139. if (p != *ppos)
  140. return -EFBIG;
  141. if (!valid_phys_addr_range(p, count))
  142. return -EFAULT;
  143. written = 0;
  144. #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
  145. /* we don't have page 0 mapped on sparc and m68k.. */
  146. if (p < PAGE_SIZE) {
  147. sz = size_inside_page(p, count);
  148. /* Hmm. Do something? */
  149. buf += sz;
  150. p += sz;
  151. count -= sz;
  152. written += sz;
  153. }
  154. #endif
  155. while (count > 0) {
  156. sz = size_inside_page(p, count);
  157. if (!range_is_allowed(p >> PAGE_SHIFT, sz))
  158. return -EPERM;
  159. /*
  160. * On ia64 if a page has been mapped somewhere as uncached, then
  161. * it must also be accessed uncached by the kernel or data
  162. * corruption may occur.
  163. */
  164. ptr = xlate_dev_mem_ptr(p);
  165. if (!ptr) {
  166. if (written)
  167. break;
  168. return -EFAULT;
  169. }
  170. copied = copy_from_user(ptr, buf, sz);
  171. unxlate_dev_mem_ptr(p, ptr);
  172. if (copied) {
  173. written += sz - copied;
  174. if (written)
  175. break;
  176. return -EFAULT;
  177. }
  178. buf += sz;
  179. p += sz;
  180. count -= sz;
  181. written += sz;
  182. }
  183. *ppos += written;
  184. return written;
  185. }
  186. int __weak phys_mem_access_prot_allowed(struct file *file,
  187. unsigned long pfn, unsigned long size, pgprot_t *vma_prot)
  188. {
  189. return 1;
  190. }
  191. #ifndef __HAVE_PHYS_MEM_ACCESS_PROT
  192. /*
  193. * Architectures vary in how they handle caching for addresses
  194. * outside of main memory.
  195. *
  196. */
  197. #ifdef pgprot_noncached
  198. static int uncached_access(struct file *file, phys_addr_t addr)
  199. {
  200. #if defined(CONFIG_IA64)
  201. /*
  202. * On ia64, we ignore O_DSYNC because we cannot tolerate memory
  203. * attribute aliases.
  204. */
  205. return !(efi_mem_attributes(addr) & EFI_MEMORY_WB);
  206. #elif defined(CONFIG_MIPS)
  207. {
  208. extern int __uncached_access(struct file *file,
  209. unsigned long addr);
  210. return __uncached_access(file, addr);
  211. }
  212. #else
  213. /*
  214. * Accessing memory above the top the kernel knows about or through a
  215. * file pointer
  216. * that was marked O_DSYNC will be done non-cached.
  217. */
  218. if (file->f_flags & O_DSYNC)
  219. return 1;
  220. return addr >= __pa(high_memory);
  221. #endif
  222. }
  223. #endif
  224. static pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
  225. unsigned long size, pgprot_t vma_prot)
  226. {
  227. #ifdef pgprot_noncached
  228. phys_addr_t offset = pfn << PAGE_SHIFT;
  229. if (uncached_access(file, offset))
  230. return pgprot_noncached(vma_prot);
  231. #endif
  232. return vma_prot;
  233. }
  234. #endif
  235. #ifndef CONFIG_MMU
  236. static unsigned long get_unmapped_area_mem(struct file *file,
  237. unsigned long addr,
  238. unsigned long len,
  239. unsigned long pgoff,
  240. unsigned long flags)
  241. {
  242. if (!valid_mmap_phys_addr_range(pgoff, len))
  243. return (unsigned long) -EINVAL;
  244. return pgoff << PAGE_SHIFT;
  245. }
  246. /* can't do an in-place private mapping if there's no MMU */
  247. static inline int private_mapping_ok(struct vm_area_struct *vma)
  248. {
  249. return vma->vm_flags & VM_MAYSHARE;
  250. }
  251. #else
  252. #define get_unmapped_area_mem NULL
  253. static inline int private_mapping_ok(struct vm_area_struct *vma)
  254. {
  255. return 1;
  256. }
  257. #endif
  258. static const struct vm_operations_struct mmap_mem_ops = {
  259. #ifdef CONFIG_HAVE_IOREMAP_PROT
  260. .access = generic_access_phys
  261. #endif
  262. };
  263. static int mmap_mem(struct file *file, struct vm_area_struct *vma)
  264. {
  265. size_t size = vma->vm_end - vma->vm_start;
  266. if (!valid_mmap_phys_addr_range(vma->vm_pgoff, size))
  267. return -EINVAL;
  268. if (!private_mapping_ok(vma))
  269. return -ENOSYS;
  270. if (!range_is_allowed(vma->vm_pgoff, size))
  271. return -EPERM;
  272. if (!phys_mem_access_prot_allowed(file, vma->vm_pgoff, size,
  273. &vma->vm_page_prot))
  274. return -EINVAL;
  275. vma->vm_page_prot = phys_mem_access_prot(file, vma->vm_pgoff,
  276. size,
  277. vma->vm_page_prot);
  278. vma->vm_ops = &mmap_mem_ops;
  279. /* Remap-pfn-range will mark the range VM_IO */
  280. if (remap_pfn_range(vma,
  281. vma->vm_start,
  282. vma->vm_pgoff,
  283. size,
  284. vma->vm_page_prot)) {
  285. return -EAGAIN;
  286. }
  287. return 0;
  288. }
  289. #ifdef CONFIG_DEVKMEM
  290. static int mmap_kmem(struct file *file, struct vm_area_struct *vma)
  291. {
  292. unsigned long pfn;
  293. /* Turn a kernel-virtual address into a physical page frame */
  294. pfn = __pa((u64)vma->vm_pgoff << PAGE_SHIFT) >> PAGE_SHIFT;
  295. /*
  296. * RED-PEN: on some architectures there is more mapped memory than
  297. * available in mem_map which pfn_valid checks for. Perhaps should add a
  298. * new macro here.
  299. *
  300. * RED-PEN: vmalloc is not supported right now.
  301. */
  302. if (!pfn_valid(pfn))
  303. return -EIO;
  304. vma->vm_pgoff = pfn;
  305. return mmap_mem(file, vma);
  306. }
  307. #endif
  308. #ifdef CONFIG_DEVKMEM
  309. /*
  310. * This function reads the *virtual* memory as seen by the kernel.
  311. */
  312. static ssize_t read_kmem(struct file *file, char __user *buf,
  313. size_t count, loff_t *ppos)
  314. {
  315. unsigned long p = *ppos;
  316. ssize_t low_count, read, sz;
  317. char *kbuf; /* k-addr because vread() takes vmlist_lock rwlock */
  318. int err = 0;
  319. read = 0;
  320. if (p < (unsigned long) high_memory) {
  321. low_count = count;
  322. if (count > (unsigned long)high_memory - p)
  323. low_count = (unsigned long)high_memory - p;
  324. #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
  325. /* we don't have page 0 mapped on sparc and m68k.. */
  326. if (p < PAGE_SIZE && low_count > 0) {
  327. sz = size_inside_page(p, low_count);
  328. if (clear_user(buf, sz))
  329. return -EFAULT;
  330. buf += sz;
  331. p += sz;
  332. read += sz;
  333. low_count -= sz;
  334. count -= sz;
  335. }
  336. #endif
  337. while (low_count > 0) {
  338. sz = size_inside_page(p, low_count);
  339. /*
  340. * On ia64 if a page has been mapped somewhere as
  341. * uncached, then it must also be accessed uncached
  342. * by the kernel or data corruption may occur
  343. */
  344. kbuf = xlate_dev_kmem_ptr((char *)p);
  345. if (copy_to_user(buf, kbuf, sz))
  346. return -EFAULT;
  347. buf += sz;
  348. p += sz;
  349. read += sz;
  350. low_count -= sz;
  351. count -= sz;
  352. }
  353. }
  354. if (count > 0) {
  355. kbuf = (char *)__get_free_page(GFP_KERNEL);
  356. if (!kbuf)
  357. return -ENOMEM;
  358. while (count > 0) {
  359. sz = size_inside_page(p, count);
  360. if (!is_vmalloc_or_module_addr((void *)p)) {
  361. err = -ENXIO;
  362. break;
  363. }
  364. sz = vread(kbuf, (char *)p, sz);
  365. if (!sz)
  366. break;
  367. if (copy_to_user(buf, kbuf, sz)) {
  368. err = -EFAULT;
  369. break;
  370. }
  371. count -= sz;
  372. buf += sz;
  373. read += sz;
  374. p += sz;
  375. }
  376. free_page((unsigned long)kbuf);
  377. }
  378. *ppos = p;
  379. return read ? read : err;
  380. }
  381. static ssize_t do_write_kmem(unsigned long p, const char __user *buf,
  382. size_t count, loff_t *ppos)
  383. {
  384. ssize_t written, sz;
  385. unsigned long copied;
  386. written = 0;
  387. #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
  388. /* we don't have page 0 mapped on sparc and m68k.. */
  389. if (p < PAGE_SIZE) {
  390. sz = size_inside_page(p, count);
  391. /* Hmm. Do something? */
  392. buf += sz;
  393. p += sz;
  394. count -= sz;
  395. written += sz;
  396. }
  397. #endif
  398. while (count > 0) {
  399. char *ptr;
  400. sz = size_inside_page(p, count);
  401. /*
  402. * On ia64 if a page has been mapped somewhere as uncached, then
  403. * it must also be accessed uncached by the kernel or data
  404. * corruption may occur.
  405. */
  406. ptr = xlate_dev_kmem_ptr((char *)p);
  407. copied = copy_from_user(ptr, buf, sz);
  408. if (copied) {
  409. written += sz - copied;
  410. if (written)
  411. break;
  412. return -EFAULT;
  413. }
  414. buf += sz;
  415. p += sz;
  416. count -= sz;
  417. written += sz;
  418. }
  419. *ppos += written;
  420. return written;
  421. }
  422. /*
  423. * This function writes to the *virtual* memory as seen by the kernel.
  424. */
  425. static ssize_t write_kmem(struct file *file, const char __user *buf,
  426. size_t count, loff_t *ppos)
  427. {
  428. unsigned long p = *ppos;
  429. ssize_t wrote = 0;
  430. ssize_t virtr = 0;
  431. char *kbuf; /* k-addr because vwrite() takes vmlist_lock rwlock */
  432. int err = 0;
  433. if (p < (unsigned long) high_memory) {
  434. unsigned long to_write = min_t(unsigned long, count,
  435. (unsigned long)high_memory - p);
  436. wrote = do_write_kmem(p, buf, to_write, ppos);
  437. if (wrote != to_write)
  438. return wrote;
  439. p += wrote;
  440. buf += wrote;
  441. count -= wrote;
  442. }
  443. if (count > 0) {
  444. kbuf = (char *)__get_free_page(GFP_KERNEL);
  445. if (!kbuf)
  446. return wrote ? wrote : -ENOMEM;
  447. while (count > 0) {
  448. unsigned long sz = size_inside_page(p, count);
  449. unsigned long n;
  450. if (!is_vmalloc_or_module_addr((void *)p)) {
  451. err = -ENXIO;
  452. break;
  453. }
  454. n = copy_from_user(kbuf, buf, sz);
  455. if (n) {
  456. err = -EFAULT;
  457. break;
  458. }
  459. vwrite(kbuf, (char *)p, sz);
  460. count -= sz;
  461. buf += sz;
  462. virtr += sz;
  463. p += sz;
  464. }
  465. free_page((unsigned long)kbuf);
  466. }
  467. *ppos = p;
  468. return virtr + wrote ? : err;
  469. }
  470. #endif
  471. #ifdef CONFIG_DEVPORT
  472. static ssize_t read_port(struct file *file, char __user *buf,
  473. size_t count, loff_t *ppos)
  474. {
  475. unsigned long i = *ppos;
  476. char __user *tmp = buf;
  477. if (!access_ok(VERIFY_WRITE, buf, count))
  478. return -EFAULT;
  479. while (count-- > 0 && i < 65536) {
  480. if (__put_user(inb(i), tmp) < 0)
  481. return -EFAULT;
  482. i++;
  483. tmp++;
  484. }
  485. *ppos = i;
  486. return tmp-buf;
  487. }
  488. static ssize_t write_port(struct file *file, const char __user *buf,
  489. size_t count, loff_t *ppos)
  490. {
  491. unsigned long i = *ppos;
  492. const char __user *tmp = buf;
  493. if (!access_ok(VERIFY_READ, buf, count))
  494. return -EFAULT;
  495. while (count-- > 0 && i < 65536) {
  496. char c;
  497. if (__get_user(c, tmp)) {
  498. if (tmp > buf)
  499. break;
  500. return -EFAULT;
  501. }
  502. outb(c, i);
  503. i++;
  504. tmp++;
  505. }
  506. *ppos = i;
  507. return tmp-buf;
  508. }
  509. #endif
  510. static ssize_t read_null(struct file *file, char __user *buf,
  511. size_t count, loff_t *ppos)
  512. {
  513. return 0;
  514. }
  515. static ssize_t write_null(struct file *file, const char __user *buf,
  516. size_t count, loff_t *ppos)
  517. {
  518. return count;
  519. }
  520. static ssize_t aio_read_null(struct kiocb *iocb, const struct iovec *iov,
  521. unsigned long nr_segs, loff_t pos)
  522. {
  523. return 0;
  524. }
  525. static ssize_t aio_write_null(struct kiocb *iocb, const struct iovec *iov,
  526. unsigned long nr_segs, loff_t pos)
  527. {
  528. return iov_length(iov, nr_segs);
  529. }
  530. static int pipe_to_null(struct pipe_inode_info *info, struct pipe_buffer *buf,
  531. struct splice_desc *sd)
  532. {
  533. return sd->len;
  534. }
  535. static ssize_t splice_write_null(struct pipe_inode_info *pipe, struct file *out,
  536. loff_t *ppos, size_t len, unsigned int flags)
  537. {
  538. return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_null);
  539. }
  540. static ssize_t read_zero(struct file *file, char __user *buf,
  541. size_t count, loff_t *ppos)
  542. {
  543. size_t written;
  544. if (!count)
  545. return 0;
  546. if (!access_ok(VERIFY_WRITE, buf, count))
  547. return -EFAULT;
  548. written = 0;
  549. while (count) {
  550. unsigned long unwritten;
  551. size_t chunk = count;
  552. if (chunk > PAGE_SIZE)
  553. chunk = PAGE_SIZE; /* Just for latency reasons */
  554. unwritten = __clear_user(buf, chunk);
  555. written += chunk - unwritten;
  556. if (unwritten)
  557. break;
  558. if (signal_pending(current))
  559. return written ? written : -ERESTARTSYS;
  560. buf += chunk;
  561. count -= chunk;
  562. cond_resched();
  563. }
  564. return written ? written : -EFAULT;
  565. }
  566. static ssize_t aio_read_zero(struct kiocb *iocb, const struct iovec *iov,
  567. unsigned long nr_segs, loff_t pos)
  568. {
  569. size_t written = 0;
  570. unsigned long i;
  571. ssize_t ret;
  572. for (i = 0; i < nr_segs; i++) {
  573. ret = read_zero(iocb->ki_filp, iov[i].iov_base, iov[i].iov_len,
  574. &pos);
  575. if (ret < 0)
  576. break;
  577. written += ret;
  578. }
  579. return written ? written : -EFAULT;
  580. }
  581. static int mmap_zero(struct file *file, struct vm_area_struct *vma)
  582. {
  583. #ifndef CONFIG_MMU
  584. return -ENOSYS;
  585. #endif
  586. if (vma->vm_flags & VM_SHARED)
  587. return shmem_zero_setup(vma);
  588. return 0;
  589. }
  590. static ssize_t write_full(struct file *file, const char __user *buf,
  591. size_t count, loff_t *ppos)
  592. {
  593. return -ENOSPC;
  594. }
  595. /*
  596. * Special lseek() function for /dev/null and /dev/zero. Most notably, you
  597. * can fopen() both devices with "a" now. This was previously impossible.
  598. * -- SRB.
  599. */
  600. static loff_t null_lseek(struct file *file, loff_t offset, int orig)
  601. {
  602. return file->f_pos = 0;
  603. }
  604. /*
  605. * The memory devices use the full 32/64 bits of the offset, and so we cannot
  606. * check against negative addresses: they are ok. The return value is weird,
  607. * though, in that case (0).
  608. *
  609. * also note that seeking relative to the "end of file" isn't supported:
  610. * it has no meaning, so it returns -EINVAL.
  611. */
  612. static loff_t memory_lseek(struct file *file, loff_t offset, int orig)
  613. {
  614. loff_t ret;
  615. mutex_lock(&file_inode(file)->i_mutex);
  616. switch (orig) {
  617. case SEEK_CUR:
  618. offset += file->f_pos;
  619. case SEEK_SET:
  620. /* to avoid userland mistaking f_pos=-9 as -EBADF=-9 */
  621. if (IS_ERR_VALUE((unsigned long long)offset)) {
  622. ret = -EOVERFLOW;
  623. break;
  624. }
  625. file->f_pos = offset;
  626. ret = file->f_pos;
  627. force_successful_syscall_return();
  628. break;
  629. default:
  630. ret = -EINVAL;
  631. }
  632. mutex_unlock(&file_inode(file)->i_mutex);
  633. return ret;
  634. }
  635. static int open_port(struct inode *inode, struct file *filp)
  636. {
  637. return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
  638. }
  639. #define zero_lseek null_lseek
  640. #define full_lseek null_lseek
  641. #define write_zero write_null
  642. #define read_full read_zero
  643. #define aio_write_zero aio_write_null
  644. #define open_mem open_port
  645. #define open_kmem open_mem
  646. static const struct file_operations mem_fops = {
  647. .llseek = memory_lseek,
  648. .read = read_mem,
  649. .write = write_mem,
  650. .mmap = mmap_mem,
  651. .open = open_mem,
  652. .get_unmapped_area = get_unmapped_area_mem,
  653. };
  654. #ifdef CONFIG_DEVKMEM
  655. static const struct file_operations kmem_fops = {
  656. .llseek = memory_lseek,
  657. .read = read_kmem,
  658. .write = write_kmem,
  659. .mmap = mmap_kmem,
  660. .open = open_kmem,
  661. .get_unmapped_area = get_unmapped_area_mem,
  662. };
  663. #endif
  664. static const struct file_operations null_fops = {
  665. .llseek = null_lseek,
  666. .read = read_null,
  667. .write = write_null,
  668. .aio_read = aio_read_null,
  669. .aio_write = aio_write_null,
  670. .splice_write = splice_write_null,
  671. };
  672. #ifdef CONFIG_DEVPORT
  673. static const struct file_operations port_fops = {
  674. .llseek = memory_lseek,
  675. .read = read_port,
  676. .write = write_port,
  677. .open = open_port,
  678. };
  679. #endif
  680. static const struct file_operations zero_fops = {
  681. .llseek = zero_lseek,
  682. .read = read_zero,
  683. .write = write_zero,
  684. .aio_read = aio_read_zero,
  685. .aio_write = aio_write_zero,
  686. .mmap = mmap_zero,
  687. };
  688. /*
  689. * capabilities for /dev/zero
  690. * - permits private mappings, "copies" are taken of the source of zeros
  691. * - no writeback happens
  692. */
  693. static struct backing_dev_info zero_bdi = {
  694. .name = "char/mem",
  695. .capabilities = BDI_CAP_MAP_COPY | BDI_CAP_NO_ACCT_AND_WRITEBACK,
  696. };
  697. static const struct file_operations full_fops = {
  698. .llseek = full_lseek,
  699. .read = read_full,
  700. .write = write_full,
  701. };
  702. static const struct memdev {
  703. const char *name;
  704. umode_t mode;
  705. const struct file_operations *fops;
  706. struct backing_dev_info *dev_info;
  707. } devlist[] = {
  708. [1] = { "mem", 0, &mem_fops, &directly_mappable_cdev_bdi },
  709. #ifdef CONFIG_DEVKMEM
  710. [2] = { "kmem", 0, &kmem_fops, &directly_mappable_cdev_bdi },
  711. #endif
  712. [3] = { "null", 0666, &null_fops, NULL },
  713. #ifdef CONFIG_DEVPORT
  714. [4] = { "port", 0, &port_fops, NULL },
  715. #endif
  716. [5] = { "zero", 0666, &zero_fops, &zero_bdi },
  717. [7] = { "full", 0666, &full_fops, NULL },
  718. [8] = { "random", 0666, &random_fops, NULL },
  719. [9] = { "urandom", 0666, &urandom_fops, NULL },
  720. #ifdef CONFIG_PRINTK
  721. [11] = { "kmsg", 0644, &kmsg_fops, NULL },
  722. #endif
  723. };
  724. static int memory_open(struct inode *inode, struct file *filp)
  725. {
  726. int minor;
  727. const struct memdev *dev;
  728. minor = iminor(inode);
  729. if (minor >= ARRAY_SIZE(devlist))
  730. return -ENXIO;
  731. dev = &devlist[minor];
  732. if (!dev->fops)
  733. return -ENXIO;
  734. filp->f_op = dev->fops;
  735. if (dev->dev_info)
  736. filp->f_mapping->backing_dev_info = dev->dev_info;
  737. /* Is /dev/mem or /dev/kmem ? */
  738. if (dev->dev_info == &directly_mappable_cdev_bdi)
  739. filp->f_mode |= FMODE_UNSIGNED_OFFSET;
  740. if (dev->fops->open)
  741. return dev->fops->open(inode, filp);
  742. return 0;
  743. }
  744. static const struct file_operations memory_fops = {
  745. .open = memory_open,
  746. .llseek = noop_llseek,
  747. };
  748. static char *mem_devnode(struct device *dev, umode_t *mode)
  749. {
  750. if (mode && devlist[MINOR(dev->devt)].mode)
  751. *mode = devlist[MINOR(dev->devt)].mode;
  752. return NULL;
  753. }
  754. static struct class *mem_class;
  755. static int __init chr_dev_init(void)
  756. {
  757. int minor;
  758. int err;
  759. err = bdi_init(&zero_bdi);
  760. if (err)
  761. return err;
  762. if (register_chrdev(MEM_MAJOR, "mem", &memory_fops))
  763. printk("unable to get major %d for memory devs\n", MEM_MAJOR);
  764. mem_class = class_create(THIS_MODULE, "mem");
  765. if (IS_ERR(mem_class))
  766. return PTR_ERR(mem_class);
  767. mem_class->devnode = mem_devnode;
  768. for (minor = 1; minor < ARRAY_SIZE(devlist); minor++) {
  769. if (!devlist[minor].name)
  770. continue;
  771. /*
  772. * Create /dev/port?
  773. */
  774. if ((minor == DEVPORT_MINOR) && !arch_has_dev_port())
  775. continue;
  776. device_create(mem_class, NULL, MKDEV(MEM_MAJOR, minor),
  777. NULL, devlist[minor].name);
  778. }
  779. return tty_init();
  780. }
  781. fs_initcall(chr_dev_init);