mem.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873
  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/uio.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. /* permit direct mmap, for read, write or exec */
  250. static unsigned memory_mmap_capabilities(struct file *file)
  251. {
  252. return NOMMU_MAP_DIRECT |
  253. NOMMU_MAP_READ | NOMMU_MAP_WRITE | NOMMU_MAP_EXEC;
  254. }
  255. static unsigned zero_mmap_capabilities(struct file *file)
  256. {
  257. return NOMMU_MAP_COPY;
  258. }
  259. /* can't do an in-place private mapping if there's no MMU */
  260. static inline int private_mapping_ok(struct vm_area_struct *vma)
  261. {
  262. return vma->vm_flags & VM_MAYSHARE;
  263. }
  264. #else
  265. static inline int private_mapping_ok(struct vm_area_struct *vma)
  266. {
  267. return 1;
  268. }
  269. #endif
  270. static const struct vm_operations_struct mmap_mem_ops = {
  271. #ifdef CONFIG_HAVE_IOREMAP_PROT
  272. .access = generic_access_phys
  273. #endif
  274. };
  275. static int mmap_mem(struct file *file, struct vm_area_struct *vma)
  276. {
  277. size_t size = vma->vm_end - vma->vm_start;
  278. if (!valid_mmap_phys_addr_range(vma->vm_pgoff, size))
  279. return -EINVAL;
  280. if (!private_mapping_ok(vma))
  281. return -ENOSYS;
  282. if (!range_is_allowed(vma->vm_pgoff, size))
  283. return -EPERM;
  284. if (!phys_mem_access_prot_allowed(file, vma->vm_pgoff, size,
  285. &vma->vm_page_prot))
  286. return -EINVAL;
  287. vma->vm_page_prot = phys_mem_access_prot(file, vma->vm_pgoff,
  288. size,
  289. vma->vm_page_prot);
  290. vma->vm_ops = &mmap_mem_ops;
  291. /* Remap-pfn-range will mark the range VM_IO */
  292. if (remap_pfn_range(vma,
  293. vma->vm_start,
  294. vma->vm_pgoff,
  295. size,
  296. vma->vm_page_prot)) {
  297. return -EAGAIN;
  298. }
  299. return 0;
  300. }
  301. static int mmap_kmem(struct file *file, struct vm_area_struct *vma)
  302. {
  303. unsigned long pfn;
  304. /* Turn a kernel-virtual address into a physical page frame */
  305. pfn = __pa((u64)vma->vm_pgoff << PAGE_SHIFT) >> PAGE_SHIFT;
  306. /*
  307. * RED-PEN: on some architectures there is more mapped memory than
  308. * available in mem_map which pfn_valid checks for. Perhaps should add a
  309. * new macro here.
  310. *
  311. * RED-PEN: vmalloc is not supported right now.
  312. */
  313. if (!pfn_valid(pfn))
  314. return -EIO;
  315. vma->vm_pgoff = pfn;
  316. return mmap_mem(file, vma);
  317. }
  318. /*
  319. * This function reads the *virtual* memory as seen by the kernel.
  320. */
  321. static ssize_t read_kmem(struct file *file, char __user *buf,
  322. size_t count, loff_t *ppos)
  323. {
  324. unsigned long p = *ppos;
  325. ssize_t low_count, read, sz;
  326. char *kbuf; /* k-addr because vread() takes vmlist_lock rwlock */
  327. int err = 0;
  328. read = 0;
  329. if (p < (unsigned long) high_memory) {
  330. low_count = count;
  331. if (count > (unsigned long)high_memory - p)
  332. low_count = (unsigned long)high_memory - p;
  333. #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
  334. /* we don't have page 0 mapped on sparc and m68k.. */
  335. if (p < PAGE_SIZE && low_count > 0) {
  336. sz = size_inside_page(p, low_count);
  337. if (clear_user(buf, sz))
  338. return -EFAULT;
  339. buf += sz;
  340. p += sz;
  341. read += sz;
  342. low_count -= sz;
  343. count -= sz;
  344. }
  345. #endif
  346. while (low_count > 0) {
  347. sz = size_inside_page(p, low_count);
  348. /*
  349. * On ia64 if a page has been mapped somewhere as
  350. * uncached, then it must also be accessed uncached
  351. * by the kernel or data corruption may occur
  352. */
  353. kbuf = xlate_dev_kmem_ptr((void *)p);
  354. if (copy_to_user(buf, kbuf, sz))
  355. return -EFAULT;
  356. buf += sz;
  357. p += sz;
  358. read += sz;
  359. low_count -= sz;
  360. count -= sz;
  361. }
  362. }
  363. if (count > 0) {
  364. kbuf = (char *)__get_free_page(GFP_KERNEL);
  365. if (!kbuf)
  366. return -ENOMEM;
  367. while (count > 0) {
  368. sz = size_inside_page(p, count);
  369. if (!is_vmalloc_or_module_addr((void *)p)) {
  370. err = -ENXIO;
  371. break;
  372. }
  373. sz = vread(kbuf, (char *)p, sz);
  374. if (!sz)
  375. break;
  376. if (copy_to_user(buf, kbuf, sz)) {
  377. err = -EFAULT;
  378. break;
  379. }
  380. count -= sz;
  381. buf += sz;
  382. read += sz;
  383. p += sz;
  384. }
  385. free_page((unsigned long)kbuf);
  386. }
  387. *ppos = p;
  388. return read ? read : err;
  389. }
  390. static ssize_t do_write_kmem(unsigned long p, const char __user *buf,
  391. size_t count, loff_t *ppos)
  392. {
  393. ssize_t written, sz;
  394. unsigned long copied;
  395. written = 0;
  396. #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
  397. /* we don't have page 0 mapped on sparc and m68k.. */
  398. if (p < PAGE_SIZE) {
  399. sz = size_inside_page(p, count);
  400. /* Hmm. Do something? */
  401. buf += sz;
  402. p += sz;
  403. count -= sz;
  404. written += sz;
  405. }
  406. #endif
  407. while (count > 0) {
  408. void *ptr;
  409. sz = size_inside_page(p, count);
  410. /*
  411. * On ia64 if a page has been mapped somewhere as uncached, then
  412. * it must also be accessed uncached by the kernel or data
  413. * corruption may occur.
  414. */
  415. ptr = xlate_dev_kmem_ptr((void *)p);
  416. copied = copy_from_user(ptr, buf, sz);
  417. if (copied) {
  418. written += sz - copied;
  419. if (written)
  420. break;
  421. return -EFAULT;
  422. }
  423. buf += sz;
  424. p += sz;
  425. count -= sz;
  426. written += sz;
  427. }
  428. *ppos += written;
  429. return written;
  430. }
  431. /*
  432. * This function writes to the *virtual* memory as seen by the kernel.
  433. */
  434. static ssize_t write_kmem(struct file *file, const char __user *buf,
  435. size_t count, loff_t *ppos)
  436. {
  437. unsigned long p = *ppos;
  438. ssize_t wrote = 0;
  439. ssize_t virtr = 0;
  440. char *kbuf; /* k-addr because vwrite() takes vmlist_lock rwlock */
  441. int err = 0;
  442. if (p < (unsigned long) high_memory) {
  443. unsigned long to_write = min_t(unsigned long, count,
  444. (unsigned long)high_memory - p);
  445. wrote = do_write_kmem(p, buf, to_write, ppos);
  446. if (wrote != to_write)
  447. return wrote;
  448. p += wrote;
  449. buf += wrote;
  450. count -= wrote;
  451. }
  452. if (count > 0) {
  453. kbuf = (char *)__get_free_page(GFP_KERNEL);
  454. if (!kbuf)
  455. return wrote ? wrote : -ENOMEM;
  456. while (count > 0) {
  457. unsigned long sz = size_inside_page(p, count);
  458. unsigned long n;
  459. if (!is_vmalloc_or_module_addr((void *)p)) {
  460. err = -ENXIO;
  461. break;
  462. }
  463. n = copy_from_user(kbuf, buf, sz);
  464. if (n) {
  465. err = -EFAULT;
  466. break;
  467. }
  468. vwrite(kbuf, (char *)p, sz);
  469. count -= sz;
  470. buf += sz;
  471. virtr += sz;
  472. p += sz;
  473. }
  474. free_page((unsigned long)kbuf);
  475. }
  476. *ppos = p;
  477. return virtr + wrote ? : err;
  478. }
  479. static ssize_t read_port(struct file *file, char __user *buf,
  480. size_t count, loff_t *ppos)
  481. {
  482. unsigned long i = *ppos;
  483. char __user *tmp = buf;
  484. if (!access_ok(VERIFY_WRITE, buf, count))
  485. return -EFAULT;
  486. while (count-- > 0 && i < 65536) {
  487. if (__put_user(inb(i), tmp) < 0)
  488. return -EFAULT;
  489. i++;
  490. tmp++;
  491. }
  492. *ppos = i;
  493. return tmp-buf;
  494. }
  495. static ssize_t write_port(struct file *file, const char __user *buf,
  496. size_t count, loff_t *ppos)
  497. {
  498. unsigned long i = *ppos;
  499. const char __user *tmp = buf;
  500. if (!access_ok(VERIFY_READ, buf, count))
  501. return -EFAULT;
  502. while (count-- > 0 && i < 65536) {
  503. char c;
  504. if (__get_user(c, tmp)) {
  505. if (tmp > buf)
  506. break;
  507. return -EFAULT;
  508. }
  509. outb(c, i);
  510. i++;
  511. tmp++;
  512. }
  513. *ppos = i;
  514. return tmp-buf;
  515. }
  516. static ssize_t read_null(struct file *file, char __user *buf,
  517. size_t count, loff_t *ppos)
  518. {
  519. return 0;
  520. }
  521. static ssize_t write_null(struct file *file, const char __user *buf,
  522. size_t count, loff_t *ppos)
  523. {
  524. return count;
  525. }
  526. static ssize_t read_iter_null(struct kiocb *iocb, struct iov_iter *to)
  527. {
  528. return 0;
  529. }
  530. static ssize_t write_iter_null(struct kiocb *iocb, struct iov_iter *from)
  531. {
  532. size_t count = iov_iter_count(from);
  533. iov_iter_advance(from, count);
  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. static ssize_t read_iter_zero(struct kiocb *iocb, struct iov_iter *iter)
  547. {
  548. size_t written = 0;
  549. while (iov_iter_count(iter)) {
  550. size_t chunk = iov_iter_count(iter), n;
  551. if (chunk > PAGE_SIZE)
  552. chunk = PAGE_SIZE; /* Just for latency reasons */
  553. n = iov_iter_zero(chunk, iter);
  554. if (!n && iov_iter_count(iter))
  555. return written ? written : -EFAULT;
  556. written += n;
  557. if (signal_pending(current))
  558. return written ? written : -ERESTARTSYS;
  559. cond_resched();
  560. }
  561. return written;
  562. }
  563. static int mmap_zero(struct file *file, struct vm_area_struct *vma)
  564. {
  565. #ifndef CONFIG_MMU
  566. return -ENOSYS;
  567. #endif
  568. if (vma->vm_flags & VM_SHARED)
  569. return shmem_zero_setup(vma);
  570. return 0;
  571. }
  572. static ssize_t write_full(struct file *file, const char __user *buf,
  573. size_t count, loff_t *ppos)
  574. {
  575. return -ENOSPC;
  576. }
  577. /*
  578. * Special lseek() function for /dev/null and /dev/zero. Most notably, you
  579. * can fopen() both devices with "a" now. This was previously impossible.
  580. * -- SRB.
  581. */
  582. static loff_t null_lseek(struct file *file, loff_t offset, int orig)
  583. {
  584. return file->f_pos = 0;
  585. }
  586. /*
  587. * The memory devices use the full 32/64 bits of the offset, and so we cannot
  588. * check against negative addresses: they are ok. The return value is weird,
  589. * though, in that case (0).
  590. *
  591. * also note that seeking relative to the "end of file" isn't supported:
  592. * it has no meaning, so it returns -EINVAL.
  593. */
  594. static loff_t memory_lseek(struct file *file, loff_t offset, int orig)
  595. {
  596. loff_t ret;
  597. mutex_lock(&file_inode(file)->i_mutex);
  598. switch (orig) {
  599. case SEEK_CUR:
  600. offset += file->f_pos;
  601. case SEEK_SET:
  602. /* to avoid userland mistaking f_pos=-9 as -EBADF=-9 */
  603. if (IS_ERR_VALUE((unsigned long long)offset)) {
  604. ret = -EOVERFLOW;
  605. break;
  606. }
  607. file->f_pos = offset;
  608. ret = file->f_pos;
  609. force_successful_syscall_return();
  610. break;
  611. default:
  612. ret = -EINVAL;
  613. }
  614. mutex_unlock(&file_inode(file)->i_mutex);
  615. return ret;
  616. }
  617. static int open_port(struct inode *inode, struct file *filp)
  618. {
  619. return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
  620. }
  621. #define zero_lseek null_lseek
  622. #define full_lseek null_lseek
  623. #define write_zero write_null
  624. #define write_iter_zero write_iter_null
  625. #define open_mem open_port
  626. #define open_kmem open_mem
  627. static const struct file_operations __maybe_unused mem_fops = {
  628. .llseek = memory_lseek,
  629. .read = read_mem,
  630. .write = write_mem,
  631. .mmap = mmap_mem,
  632. .open = open_mem,
  633. #ifndef CONFIG_MMU
  634. .get_unmapped_area = get_unmapped_area_mem,
  635. .mmap_capabilities = memory_mmap_capabilities,
  636. #endif
  637. };
  638. static const struct file_operations __maybe_unused kmem_fops = {
  639. .llseek = memory_lseek,
  640. .read = read_kmem,
  641. .write = write_kmem,
  642. .mmap = mmap_kmem,
  643. .open = open_kmem,
  644. #ifndef CONFIG_MMU
  645. .get_unmapped_area = get_unmapped_area_mem,
  646. .mmap_capabilities = memory_mmap_capabilities,
  647. #endif
  648. };
  649. static const struct file_operations null_fops = {
  650. .llseek = null_lseek,
  651. .read = read_null,
  652. .write = write_null,
  653. .read_iter = read_iter_null,
  654. .write_iter = write_iter_null,
  655. .splice_write = splice_write_null,
  656. };
  657. static const struct file_operations __maybe_unused port_fops = {
  658. .llseek = memory_lseek,
  659. .read = read_port,
  660. .write = write_port,
  661. .open = open_port,
  662. };
  663. static const struct file_operations zero_fops = {
  664. .llseek = zero_lseek,
  665. .write = write_zero,
  666. .read_iter = read_iter_zero,
  667. .write_iter = write_iter_zero,
  668. .mmap = mmap_zero,
  669. #ifndef CONFIG_MMU
  670. .mmap_capabilities = zero_mmap_capabilities,
  671. #endif
  672. };
  673. static const struct file_operations full_fops = {
  674. .llseek = full_lseek,
  675. .read_iter = read_iter_zero,
  676. .write = write_full,
  677. };
  678. static const struct memdev {
  679. const char *name;
  680. umode_t mode;
  681. const struct file_operations *fops;
  682. fmode_t fmode;
  683. } devlist[] = {
  684. #ifdef CONFIG_DEVMEM
  685. [1] = { "mem", 0, &mem_fops, FMODE_UNSIGNED_OFFSET },
  686. #endif
  687. #ifdef CONFIG_DEVKMEM
  688. [2] = { "kmem", 0, &kmem_fops, FMODE_UNSIGNED_OFFSET },
  689. #endif
  690. [3] = { "null", 0666, &null_fops, 0 },
  691. #ifdef CONFIG_DEVPORT
  692. [4] = { "port", 0, &port_fops, 0 },
  693. #endif
  694. [5] = { "zero", 0666, &zero_fops, 0 },
  695. [7] = { "full", 0666, &full_fops, 0 },
  696. [8] = { "random", 0666, &random_fops, 0 },
  697. [9] = { "urandom", 0666, &urandom_fops, 0 },
  698. #ifdef CONFIG_PRINTK
  699. [11] = { "kmsg", 0644, &kmsg_fops, 0 },
  700. #endif
  701. };
  702. static int memory_open(struct inode *inode, struct file *filp)
  703. {
  704. int minor;
  705. const struct memdev *dev;
  706. minor = iminor(inode);
  707. if (minor >= ARRAY_SIZE(devlist))
  708. return -ENXIO;
  709. dev = &devlist[minor];
  710. if (!dev->fops)
  711. return -ENXIO;
  712. filp->f_op = dev->fops;
  713. filp->f_mode |= dev->fmode;
  714. if (dev->fops->open)
  715. return dev->fops->open(inode, filp);
  716. return 0;
  717. }
  718. static const struct file_operations memory_fops = {
  719. .open = memory_open,
  720. .llseek = noop_llseek,
  721. };
  722. static char *mem_devnode(struct device *dev, umode_t *mode)
  723. {
  724. if (mode && devlist[MINOR(dev->devt)].mode)
  725. *mode = devlist[MINOR(dev->devt)].mode;
  726. return NULL;
  727. }
  728. static struct class *mem_class;
  729. static int __init chr_dev_init(void)
  730. {
  731. int minor;
  732. if (register_chrdev(MEM_MAJOR, "mem", &memory_fops))
  733. printk("unable to get major %d for memory devs\n", MEM_MAJOR);
  734. mem_class = class_create(THIS_MODULE, "mem");
  735. if (IS_ERR(mem_class))
  736. return PTR_ERR(mem_class);
  737. mem_class->devnode = mem_devnode;
  738. for (minor = 1; minor < ARRAY_SIZE(devlist); minor++) {
  739. if (!devlist[minor].name)
  740. continue;
  741. /*
  742. * Create /dev/port?
  743. */
  744. if ((minor == DEVPORT_MINOR) && !arch_has_dev_port())
  745. continue;
  746. device_create(mem_class, NULL, MKDEV(MEM_MAJOR, minor),
  747. NULL, devlist[minor].name);
  748. }
  749. return tty_init();
  750. }
  751. fs_initcall(chr_dev_init);