mem.c 19 KB

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