mem.c 19 KB

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