mem.c 24 KB

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