mem.c 20 KB

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