mem.c 19 KB

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