mem.c 20 KB

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