mem.c 20 KB

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