mem.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  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 <linux/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. #ifndef unxlate_dev_mem_ptr
  76. #define unxlate_dev_mem_ptr unxlate_dev_mem_ptr
  77. void __weak unxlate_dev_mem_ptr(phys_addr_t phys, void *addr)
  78. {
  79. }
  80. #endif
  81. /*
  82. * This funcion reads the *physical* memory. The f_pos points directly to the
  83. * memory location.
  84. */
  85. static ssize_t read_mem(struct file *file, char __user *buf,
  86. size_t count, loff_t *ppos)
  87. {
  88. phys_addr_t p = *ppos;
  89. ssize_t read, sz;
  90. void *ptr;
  91. if (p != *ppos)
  92. return 0;
  93. if (!valid_phys_addr_range(p, count))
  94. return -EFAULT;
  95. read = 0;
  96. #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
  97. /* we don't have page 0 mapped on sparc and m68k.. */
  98. if (p < PAGE_SIZE) {
  99. sz = size_inside_page(p, count);
  100. if (sz > 0) {
  101. if (clear_user(buf, sz))
  102. return -EFAULT;
  103. buf += sz;
  104. p += sz;
  105. count -= sz;
  106. read += sz;
  107. }
  108. }
  109. #endif
  110. while (count > 0) {
  111. unsigned long remaining;
  112. sz = size_inside_page(p, count);
  113. if (!range_is_allowed(p >> PAGE_SHIFT, count))
  114. return -EPERM;
  115. /*
  116. * On ia64 if a page has been mapped somewhere as uncached, then
  117. * it must also be accessed uncached by the kernel or data
  118. * corruption may occur.
  119. */
  120. ptr = xlate_dev_mem_ptr(p);
  121. if (!ptr)
  122. return -EFAULT;
  123. remaining = copy_to_user(buf, ptr, sz);
  124. unxlate_dev_mem_ptr(p, ptr);
  125. if (remaining)
  126. return -EFAULT;
  127. buf += sz;
  128. p += sz;
  129. count -= sz;
  130. read += sz;
  131. }
  132. *ppos += read;
  133. return read;
  134. }
  135. static ssize_t write_mem(struct file *file, const char __user *buf,
  136. size_t count, loff_t *ppos)
  137. {
  138. phys_addr_t p = *ppos;
  139. ssize_t written, sz;
  140. unsigned long copied;
  141. void *ptr;
  142. if (p != *ppos)
  143. return -EFBIG;
  144. if (!valid_phys_addr_range(p, count))
  145. return -EFAULT;
  146. written = 0;
  147. #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
  148. /* we don't have page 0 mapped on sparc and m68k.. */
  149. if (p < PAGE_SIZE) {
  150. sz = size_inside_page(p, count);
  151. /* Hmm. Do something? */
  152. buf += sz;
  153. p += sz;
  154. count -= sz;
  155. written += sz;
  156. }
  157. #endif
  158. while (count > 0) {
  159. sz = size_inside_page(p, count);
  160. if (!range_is_allowed(p >> PAGE_SHIFT, sz))
  161. return -EPERM;
  162. /*
  163. * On ia64 if a page has been mapped somewhere as uncached, then
  164. * it must also be accessed uncached by the kernel or data
  165. * corruption may occur.
  166. */
  167. ptr = xlate_dev_mem_ptr(p);
  168. if (!ptr) {
  169. if (written)
  170. break;
  171. return -EFAULT;
  172. }
  173. copied = copy_from_user(ptr, buf, sz);
  174. unxlate_dev_mem_ptr(p, ptr);
  175. if (copied) {
  176. written += sz - copied;
  177. if (written)
  178. break;
  179. return -EFAULT;
  180. }
  181. buf += sz;
  182. p += sz;
  183. count -= sz;
  184. written += sz;
  185. }
  186. *ppos += written;
  187. return written;
  188. }
  189. int __weak phys_mem_access_prot_allowed(struct file *file,
  190. unsigned long pfn, unsigned long size, pgprot_t *vma_prot)
  191. {
  192. return 1;
  193. }
  194. #ifndef __HAVE_PHYS_MEM_ACCESS_PROT
  195. /*
  196. * Architectures vary in how they handle caching for addresses
  197. * outside of main memory.
  198. *
  199. */
  200. #ifdef pgprot_noncached
  201. static int uncached_access(struct file *file, phys_addr_t addr)
  202. {
  203. #if defined(CONFIG_IA64)
  204. /*
  205. * On ia64, we ignore O_DSYNC because we cannot tolerate memory
  206. * attribute aliases.
  207. */
  208. return !(efi_mem_attributes(addr) & EFI_MEMORY_WB);
  209. #elif defined(CONFIG_MIPS)
  210. {
  211. extern int __uncached_access(struct file *file,
  212. unsigned long addr);
  213. return __uncached_access(file, addr);
  214. }
  215. #else
  216. /*
  217. * Accessing memory above the top the kernel knows about or through a
  218. * file pointer
  219. * that was marked O_DSYNC will be done non-cached.
  220. */
  221. if (file->f_flags & O_DSYNC)
  222. return 1;
  223. return addr >= __pa(high_memory);
  224. #endif
  225. }
  226. #endif
  227. static pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
  228. unsigned long size, pgprot_t vma_prot)
  229. {
  230. #ifdef pgprot_noncached
  231. phys_addr_t offset = pfn << PAGE_SHIFT;
  232. if (uncached_access(file, offset))
  233. return pgprot_noncached(vma_prot);
  234. #endif
  235. return vma_prot;
  236. }
  237. #endif
  238. #ifndef CONFIG_MMU
  239. static unsigned long get_unmapped_area_mem(struct file *file,
  240. unsigned long addr,
  241. unsigned long len,
  242. unsigned long pgoff,
  243. unsigned long flags)
  244. {
  245. if (!valid_mmap_phys_addr_range(pgoff, len))
  246. return (unsigned long) -EINVAL;
  247. return pgoff << PAGE_SHIFT;
  248. }
  249. /* can't do an in-place private mapping if there's no MMU */
  250. static inline int private_mapping_ok(struct vm_area_struct *vma)
  251. {
  252. return vma->vm_flags & VM_MAYSHARE;
  253. }
  254. #else
  255. #define get_unmapped_area_mem NULL
  256. static inline int private_mapping_ok(struct vm_area_struct *vma)
  257. {
  258. return 1;
  259. }
  260. #endif
  261. static const struct vm_operations_struct mmap_mem_ops = {
  262. #ifdef CONFIG_HAVE_IOREMAP_PROT
  263. .access = generic_access_phys
  264. #endif
  265. };
  266. static int mmap_mem(struct file *file, struct vm_area_struct *vma)
  267. {
  268. size_t size = vma->vm_end - vma->vm_start;
  269. if (!valid_mmap_phys_addr_range(vma->vm_pgoff, size))
  270. return -EINVAL;
  271. if (!private_mapping_ok(vma))
  272. return -ENOSYS;
  273. if (!range_is_allowed(vma->vm_pgoff, size))
  274. return -EPERM;
  275. if (!phys_mem_access_prot_allowed(file, vma->vm_pgoff, size,
  276. &vma->vm_page_prot))
  277. return -EINVAL;
  278. vma->vm_page_prot = phys_mem_access_prot(file, vma->vm_pgoff,
  279. size,
  280. vma->vm_page_prot);
  281. vma->vm_ops = &mmap_mem_ops;
  282. /* Remap-pfn-range will mark the range VM_IO */
  283. if (remap_pfn_range(vma,
  284. vma->vm_start,
  285. vma->vm_pgoff,
  286. size,
  287. vma->vm_page_prot)) {
  288. return -EAGAIN;
  289. }
  290. return 0;
  291. }
  292. static int mmap_kmem(struct file *file, struct vm_area_struct *vma)
  293. {
  294. unsigned long pfn;
  295. /* Turn a kernel-virtual address into a physical page frame */
  296. pfn = __pa((u64)vma->vm_pgoff << PAGE_SHIFT) >> PAGE_SHIFT;
  297. /*
  298. * RED-PEN: on some architectures there is more mapped memory than
  299. * available in mem_map which pfn_valid checks for. Perhaps should add a
  300. * new macro here.
  301. *
  302. * RED-PEN: vmalloc is not supported right now.
  303. */
  304. if (!pfn_valid(pfn))
  305. return -EIO;
  306. vma->vm_pgoff = pfn;
  307. return mmap_mem(file, vma);
  308. }
  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((void *)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. void *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((void *)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. static ssize_t read_port(struct file *file, char __user *buf,
  471. size_t count, loff_t *ppos)
  472. {
  473. unsigned long i = *ppos;
  474. char __user *tmp = buf;
  475. if (!access_ok(VERIFY_WRITE, buf, count))
  476. return -EFAULT;
  477. while (count-- > 0 && i < 65536) {
  478. if (__put_user(inb(i), tmp) < 0)
  479. return -EFAULT;
  480. i++;
  481. tmp++;
  482. }
  483. *ppos = i;
  484. return tmp-buf;
  485. }
  486. static ssize_t write_port(struct file *file, const char __user *buf,
  487. size_t count, loff_t *ppos)
  488. {
  489. unsigned long i = *ppos;
  490. const char __user *tmp = buf;
  491. if (!access_ok(VERIFY_READ, buf, count))
  492. return -EFAULT;
  493. while (count-- > 0 && i < 65536) {
  494. char c;
  495. if (__get_user(c, tmp)) {
  496. if (tmp > buf)
  497. break;
  498. return -EFAULT;
  499. }
  500. outb(c, i);
  501. i++;
  502. tmp++;
  503. }
  504. *ppos = i;
  505. return tmp-buf;
  506. }
  507. static ssize_t read_null(struct file *file, char __user *buf,
  508. size_t count, loff_t *ppos)
  509. {
  510. return 0;
  511. }
  512. static ssize_t write_null(struct file *file, const char __user *buf,
  513. size_t count, loff_t *ppos)
  514. {
  515. return count;
  516. }
  517. static ssize_t aio_read_null(struct kiocb *iocb, const struct iovec *iov,
  518. unsigned long nr_segs, loff_t pos)
  519. {
  520. return 0;
  521. }
  522. static ssize_t aio_write_null(struct kiocb *iocb, const struct iovec *iov,
  523. unsigned long nr_segs, loff_t pos)
  524. {
  525. return iov_length(iov, nr_segs);
  526. }
  527. static int pipe_to_null(struct pipe_inode_info *info, struct pipe_buffer *buf,
  528. struct splice_desc *sd)
  529. {
  530. return sd->len;
  531. }
  532. static ssize_t splice_write_null(struct pipe_inode_info *pipe, struct file *out,
  533. loff_t *ppos, size_t len, unsigned int flags)
  534. {
  535. return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_null);
  536. }
  537. static ssize_t read_iter_zero(struct kiocb *iocb, struct iov_iter *iter)
  538. {
  539. size_t written = 0;
  540. while (iov_iter_count(iter)) {
  541. size_t chunk = iov_iter_count(iter), n;
  542. if (chunk > PAGE_SIZE)
  543. chunk = PAGE_SIZE; /* Just for latency reasons */
  544. n = iov_iter_zero(chunk, iter);
  545. if (!n && iov_iter_count(iter))
  546. return written ? written : -EFAULT;
  547. written += n;
  548. if (signal_pending(current))
  549. return written ? written : -ERESTARTSYS;
  550. cond_resched();
  551. }
  552. return written;
  553. }
  554. static int mmap_zero(struct file *file, struct vm_area_struct *vma)
  555. {
  556. #ifndef CONFIG_MMU
  557. return -ENOSYS;
  558. #endif
  559. if (vma->vm_flags & VM_SHARED)
  560. return shmem_zero_setup(vma);
  561. return 0;
  562. }
  563. static ssize_t write_full(struct file *file, const char __user *buf,
  564. size_t count, loff_t *ppos)
  565. {
  566. return -ENOSPC;
  567. }
  568. /*
  569. * Special lseek() function for /dev/null and /dev/zero. Most notably, you
  570. * can fopen() both devices with "a" now. This was previously impossible.
  571. * -- SRB.
  572. */
  573. static loff_t null_lseek(struct file *file, loff_t offset, int orig)
  574. {
  575. return file->f_pos = 0;
  576. }
  577. /*
  578. * The memory devices use the full 32/64 bits of the offset, and so we cannot
  579. * check against negative addresses: they are ok. The return value is weird,
  580. * though, in that case (0).
  581. *
  582. * also note that seeking relative to the "end of file" isn't supported:
  583. * it has no meaning, so it returns -EINVAL.
  584. */
  585. static loff_t memory_lseek(struct file *file, loff_t offset, int orig)
  586. {
  587. loff_t ret;
  588. mutex_lock(&file_inode(file)->i_mutex);
  589. switch (orig) {
  590. case SEEK_CUR:
  591. offset += file->f_pos;
  592. case SEEK_SET:
  593. /* to avoid userland mistaking f_pos=-9 as -EBADF=-9 */
  594. if (IS_ERR_VALUE((unsigned long long)offset)) {
  595. ret = -EOVERFLOW;
  596. break;
  597. }
  598. file->f_pos = offset;
  599. ret = file->f_pos;
  600. force_successful_syscall_return();
  601. break;
  602. default:
  603. ret = -EINVAL;
  604. }
  605. mutex_unlock(&file_inode(file)->i_mutex);
  606. return ret;
  607. }
  608. static int open_port(struct inode *inode, struct file *filp)
  609. {
  610. return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
  611. }
  612. #define zero_lseek null_lseek
  613. #define full_lseek null_lseek
  614. #define write_zero write_null
  615. #define aio_write_zero aio_write_null
  616. #define open_mem open_port
  617. #define open_kmem open_mem
  618. static const struct file_operations __maybe_unused mem_fops = {
  619. .llseek = memory_lseek,
  620. .read = read_mem,
  621. .write = write_mem,
  622. .mmap = mmap_mem,
  623. .open = open_mem,
  624. .get_unmapped_area = get_unmapped_area_mem,
  625. };
  626. static const struct file_operations __maybe_unused kmem_fops = {
  627. .llseek = memory_lseek,
  628. .read = read_kmem,
  629. .write = write_kmem,
  630. .mmap = mmap_kmem,
  631. .open = open_kmem,
  632. .get_unmapped_area = get_unmapped_area_mem,
  633. };
  634. static const struct file_operations null_fops = {
  635. .llseek = null_lseek,
  636. .read = read_null,
  637. .write = write_null,
  638. .aio_read = aio_read_null,
  639. .aio_write = aio_write_null,
  640. .splice_write = splice_write_null,
  641. };
  642. static const struct file_operations __maybe_unused port_fops = {
  643. .llseek = memory_lseek,
  644. .read = read_port,
  645. .write = write_port,
  646. .open = open_port,
  647. };
  648. static const struct file_operations zero_fops = {
  649. .llseek = zero_lseek,
  650. .read = new_sync_read,
  651. .write = write_zero,
  652. .read_iter = read_iter_zero,
  653. .aio_write = aio_write_zero,
  654. .mmap = mmap_zero,
  655. };
  656. /*
  657. * capabilities for /dev/zero
  658. * - permits private mappings, "copies" are taken of the source of zeros
  659. * - no writeback happens
  660. */
  661. static struct backing_dev_info zero_bdi = {
  662. .name = "char/mem",
  663. .capabilities = BDI_CAP_MAP_COPY | BDI_CAP_NO_ACCT_AND_WRITEBACK,
  664. };
  665. static const struct file_operations full_fops = {
  666. .llseek = full_lseek,
  667. .read = new_sync_read,
  668. .read_iter = read_iter_zero,
  669. .write = write_full,
  670. };
  671. static const struct memdev {
  672. const char *name;
  673. umode_t mode;
  674. const struct file_operations *fops;
  675. struct backing_dev_info *dev_info;
  676. } devlist[] = {
  677. #ifdef CONFIG_DEVMEM
  678. [1] = { "mem", 0, &mem_fops, &directly_mappable_cdev_bdi },
  679. #endif
  680. #ifdef CONFIG_DEVKMEM
  681. [2] = { "kmem", 0, &kmem_fops, &directly_mappable_cdev_bdi },
  682. #endif
  683. [3] = { "null", 0666, &null_fops, NULL },
  684. #ifdef CONFIG_DEVPORT
  685. [4] = { "port", 0, &port_fops, NULL },
  686. #endif
  687. [5] = { "zero", 0666, &zero_fops, &zero_bdi },
  688. [7] = { "full", 0666, &full_fops, NULL },
  689. [8] = { "random", 0666, &random_fops, NULL },
  690. [9] = { "urandom", 0666, &urandom_fops, NULL },
  691. #ifdef CONFIG_PRINTK
  692. [11] = { "kmsg", 0644, &kmsg_fops, NULL },
  693. #endif
  694. };
  695. static int memory_open(struct inode *inode, struct file *filp)
  696. {
  697. int minor;
  698. const struct memdev *dev;
  699. minor = iminor(inode);
  700. if (minor >= ARRAY_SIZE(devlist))
  701. return -ENXIO;
  702. dev = &devlist[minor];
  703. if (!dev->fops)
  704. return -ENXIO;
  705. filp->f_op = dev->fops;
  706. if (dev->dev_info)
  707. filp->f_mapping->backing_dev_info = dev->dev_info;
  708. /* Is /dev/mem or /dev/kmem ? */
  709. if (dev->dev_info == &directly_mappable_cdev_bdi)
  710. filp->f_mode |= FMODE_UNSIGNED_OFFSET;
  711. if (dev->fops->open)
  712. return dev->fops->open(inode, filp);
  713. return 0;
  714. }
  715. static const struct file_operations memory_fops = {
  716. .open = memory_open,
  717. .llseek = noop_llseek,
  718. };
  719. static char *mem_devnode(struct device *dev, umode_t *mode)
  720. {
  721. if (mode && devlist[MINOR(dev->devt)].mode)
  722. *mode = devlist[MINOR(dev->devt)].mode;
  723. return NULL;
  724. }
  725. static struct class *mem_class;
  726. static int __init chr_dev_init(void)
  727. {
  728. int minor;
  729. int err;
  730. err = bdi_init(&zero_bdi);
  731. if (err)
  732. return err;
  733. if (register_chrdev(MEM_MAJOR, "mem", &memory_fops))
  734. printk("unable to get major %d for memory devs\n", MEM_MAJOR);
  735. mem_class = class_create(THIS_MODULE, "mem");
  736. if (IS_ERR(mem_class))
  737. return PTR_ERR(mem_class);
  738. mem_class->devnode = mem_devnode;
  739. for (minor = 1; minor < ARRAY_SIZE(devlist); minor++) {
  740. if (!devlist[minor].name)
  741. continue;
  742. /*
  743. * Create /dev/port?
  744. */
  745. if ((minor == DEVPORT_MINOR) && !arch_has_dev_port())
  746. continue;
  747. device_create(mem_class, NULL, MKDEV(MEM_MAJOR, minor),
  748. NULL, devlist[minor].name);
  749. }
  750. return tty_init();
  751. }
  752. fs_initcall(chr_dev_init);