mem.c 21 KB

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