mem.c 20 KB

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