mem.c 19 KB

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