mem.c 20 KB

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