mem.c 21 KB

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