mem.c 19 KB

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