mem.c 20 KB

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