mem.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926
  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. sz = size_inside_page(p, count);
  141. if (!range_is_allowed(p >> PAGE_SHIFT, count))
  142. return -EPERM;
  143. /*
  144. * On ia64 if a page has been mapped somewhere as
  145. * uncached, then it must also be accessed uncached
  146. * by the kernel or data corruption may occur
  147. */
  148. ptr = xlate_dev_mem_ptr(p);
  149. if (!ptr)
  150. return -EFAULT;
  151. if (copy_to_user(buf, ptr, sz)) {
  152. unxlate_dev_mem_ptr(p, ptr);
  153. return -EFAULT;
  154. }
  155. unxlate_dev_mem_ptr(p, ptr);
  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. if (copied) {
  204. written += sz - copied;
  205. unxlate_dev_mem_ptr(p, ptr);
  206. if (written)
  207. break;
  208. return -EFAULT;
  209. }
  210. unxlate_dev_mem_ptr(p, ptr);
  211. buf += sz;
  212. p += sz;
  213. count -= sz;
  214. written += sz;
  215. }
  216. *ppos += written;
  217. return written;
  218. }
  219. int __attribute__((weak)) phys_mem_access_prot_allowed(struct file *file,
  220. unsigned long pfn, unsigned long size, pgprot_t *vma_prot)
  221. {
  222. return 1;
  223. }
  224. #ifndef __HAVE_PHYS_MEM_ACCESS_PROT
  225. static pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
  226. unsigned long size, pgprot_t vma_prot)
  227. {
  228. #ifdef pgprot_noncached
  229. unsigned long offset = pfn << PAGE_SHIFT;
  230. if (uncached_access(file, offset))
  231. return pgprot_noncached(vma_prot);
  232. #endif
  233. return vma_prot;
  234. }
  235. #endif
  236. #ifndef CONFIG_MMU
  237. static unsigned long get_unmapped_area_mem(struct file *file,
  238. unsigned long addr,
  239. unsigned long len,
  240. unsigned long pgoff,
  241. unsigned long flags)
  242. {
  243. if (!valid_mmap_phys_addr_range(pgoff, len))
  244. return (unsigned long) -EINVAL;
  245. return pgoff << PAGE_SHIFT;
  246. }
  247. /* can't do an in-place private mapping if there's no MMU */
  248. static inline int private_mapping_ok(struct vm_area_struct *vma)
  249. {
  250. return vma->vm_flags & VM_MAYSHARE;
  251. }
  252. #else
  253. #define get_unmapped_area_mem NULL
  254. static inline int private_mapping_ok(struct vm_area_struct *vma)
  255. {
  256. return 1;
  257. }
  258. #endif
  259. static const struct vm_operations_struct mmap_mem_ops = {
  260. #ifdef CONFIG_HAVE_IOREMAP_PROT
  261. .access = generic_access_phys
  262. #endif
  263. };
  264. static int mmap_mem(struct file * file, struct vm_area_struct * vma)
  265. {
  266. size_t size = vma->vm_end - vma->vm_start;
  267. if (!valid_mmap_phys_addr_range(vma->vm_pgoff, size))
  268. return -EINVAL;
  269. if (!private_mapping_ok(vma))
  270. return -ENOSYS;
  271. if (!range_is_allowed(vma->vm_pgoff, size))
  272. return -EPERM;
  273. if (!phys_mem_access_prot_allowed(file, vma->vm_pgoff, size,
  274. &vma->vm_page_prot))
  275. return -EINVAL;
  276. vma->vm_page_prot = phys_mem_access_prot(file, vma->vm_pgoff,
  277. size,
  278. vma->vm_page_prot);
  279. vma->vm_ops = &mmap_mem_ops;
  280. /* Remap-pfn-range will mark the range VM_IO and VM_RESERVED */
  281. if (remap_pfn_range(vma,
  282. vma->vm_start,
  283. vma->vm_pgoff,
  284. size,
  285. vma->vm_page_prot)) {
  286. return -EAGAIN;
  287. }
  288. return 0;
  289. }
  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
  298. * than available in mem_map which pfn_valid checks
  299. * for. Perhaps should add a 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. read = 0;
  350. if (p < (unsigned long) high_memory) {
  351. low_count = count;
  352. if (count > (unsigned long) high_memory - p)
  353. low_count = (unsigned long) high_memory - p;
  354. #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
  355. /* we don't have page 0 mapped on sparc and m68k.. */
  356. if (p < PAGE_SIZE && low_count > 0) {
  357. size_t tmp = PAGE_SIZE - p;
  358. if (tmp > low_count) tmp = low_count;
  359. if (clear_user(buf, tmp))
  360. return -EFAULT;
  361. buf += tmp;
  362. p += tmp;
  363. read += tmp;
  364. low_count -= tmp;
  365. count -= tmp;
  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. int len = size_inside_page(p, count);
  391. len = vread(kbuf, (char *)p, len);
  392. if (!len)
  393. break;
  394. if (copy_to_user(buf, kbuf, len)) {
  395. free_page((unsigned long)kbuf);
  396. return -EFAULT;
  397. }
  398. count -= len;
  399. buf += len;
  400. read += len;
  401. p += len;
  402. }
  403. free_page((unsigned long)kbuf);
  404. }
  405. *ppos = p;
  406. return read;
  407. }
  408. static inline ssize_t
  409. do_write_kmem(void *p, unsigned long realp, const char __user * buf,
  410. size_t count, loff_t *ppos)
  411. {
  412. ssize_t written, sz;
  413. unsigned long copied;
  414. written = 0;
  415. #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
  416. /* we don't have page 0 mapped on sparc and m68k.. */
  417. if (realp < PAGE_SIZE) {
  418. unsigned long sz = PAGE_SIZE - realp;
  419. if (sz > count)
  420. sz = count;
  421. /* Hmm. Do something? */
  422. buf += sz;
  423. p += sz;
  424. realp += sz;
  425. count -= sz;
  426. written += sz;
  427. }
  428. #endif
  429. while (count > 0) {
  430. char *ptr;
  431. sz = size_inside_page(realp, count);
  432. /*
  433. * On ia64 if a page has been mapped somewhere as
  434. * uncached, then it must also be accessed uncached
  435. * by the kernel or data corruption may occur
  436. */
  437. ptr = xlate_dev_kmem_ptr(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. realp += sz;
  448. count -= sz;
  449. written += sz;
  450. }
  451. *ppos += written;
  452. return written;
  453. }
  454. /*
  455. * This function writes to the *virtual* memory as seen by the kernel.
  456. */
  457. static ssize_t write_kmem(struct file * file, const char __user * buf,
  458. size_t count, loff_t *ppos)
  459. {
  460. unsigned long p = *ppos;
  461. ssize_t wrote = 0;
  462. ssize_t virtr = 0;
  463. ssize_t written;
  464. char * kbuf; /* k-addr because vwrite() takes vmlist_lock rwlock */
  465. if (p < (unsigned long) high_memory) {
  466. wrote = count;
  467. if (count > (unsigned long) high_memory - p)
  468. wrote = (unsigned long) high_memory - p;
  469. written = do_write_kmem((void*)p, p, buf, wrote, ppos);
  470. if (written != wrote)
  471. return written;
  472. wrote = written;
  473. p += wrote;
  474. buf += wrote;
  475. count -= wrote;
  476. }
  477. if (count > 0) {
  478. kbuf = (char *)__get_free_page(GFP_KERNEL);
  479. if (!kbuf)
  480. return wrote ? wrote : -ENOMEM;
  481. while (count > 0) {
  482. int len = size_inside_page(p, count);
  483. written = copy_from_user(kbuf, buf, len);
  484. if (written) {
  485. if (wrote + virtr)
  486. break;
  487. free_page((unsigned long)kbuf);
  488. return -EFAULT;
  489. }
  490. len = vwrite(kbuf, (char *)p, len);
  491. count -= len;
  492. buf += len;
  493. virtr += len;
  494. p += len;
  495. }
  496. free_page((unsigned long)kbuf);
  497. }
  498. *ppos = p;
  499. return virtr + wrote;
  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. /*
  611. * The memory devices use the full 32/64 bits of the offset, and so we cannot
  612. * check against negative addresses: they are ok. The return value is weird,
  613. * though, in that case (0).
  614. *
  615. * also note that seeking relative to the "end of file" isn't supported:
  616. * it has no meaning, so it returns -EINVAL.
  617. */
  618. static loff_t memory_lseek(struct file * file, loff_t offset, int orig)
  619. {
  620. loff_t ret;
  621. mutex_lock(&file->f_path.dentry->d_inode->i_mutex);
  622. switch (orig) {
  623. case 0:
  624. file->f_pos = offset;
  625. ret = file->f_pos;
  626. force_successful_syscall_return();
  627. break;
  628. case 1:
  629. file->f_pos += offset;
  630. ret = file->f_pos;
  631. force_successful_syscall_return();
  632. break;
  633. default:
  634. ret = -EINVAL;
  635. }
  636. mutex_unlock(&file->f_path.dentry->d_inode->i_mutex);
  637. return ret;
  638. }
  639. static int open_port(struct inode * inode, struct file * filp)
  640. {
  641. return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
  642. }
  643. #define zero_lseek null_lseek
  644. #define full_lseek null_lseek
  645. #define write_zero write_null
  646. #define read_full read_zero
  647. #define open_mem open_port
  648. #define open_kmem open_mem
  649. #define open_oldmem open_mem
  650. static const struct file_operations mem_fops = {
  651. .llseek = memory_lseek,
  652. .read = read_mem,
  653. .write = write_mem,
  654. .mmap = mmap_mem,
  655. .open = open_mem,
  656. .get_unmapped_area = get_unmapped_area_mem,
  657. };
  658. #ifdef CONFIG_DEVKMEM
  659. static const struct file_operations kmem_fops = {
  660. .llseek = memory_lseek,
  661. .read = read_kmem,
  662. .write = write_kmem,
  663. .mmap = mmap_kmem,
  664. .open = open_kmem,
  665. .get_unmapped_area = get_unmapped_area_mem,
  666. };
  667. #endif
  668. static const struct file_operations null_fops = {
  669. .llseek = null_lseek,
  670. .read = read_null,
  671. .write = write_null,
  672. .splice_write = splice_write_null,
  673. };
  674. #ifdef CONFIG_DEVPORT
  675. static const struct file_operations port_fops = {
  676. .llseek = memory_lseek,
  677. .read = read_port,
  678. .write = write_port,
  679. .open = open_port,
  680. };
  681. #endif
  682. static const struct file_operations zero_fops = {
  683. .llseek = zero_lseek,
  684. .read = read_zero,
  685. .write = write_zero,
  686. .mmap = mmap_zero,
  687. };
  688. /*
  689. * capabilities for /dev/zero
  690. * - permits private mappings, "copies" are taken of the source of zeros
  691. */
  692. static struct backing_dev_info zero_bdi = {
  693. .name = "char/mem",
  694. .capabilities = BDI_CAP_MAP_COPY,
  695. };
  696. static const struct file_operations full_fops = {
  697. .llseek = full_lseek,
  698. .read = read_full,
  699. .write = write_full,
  700. };
  701. #ifdef CONFIG_CRASH_DUMP
  702. static const struct file_operations oldmem_fops = {
  703. .read = read_oldmem,
  704. .open = open_oldmem,
  705. };
  706. #endif
  707. static ssize_t kmsg_write(struct file * file, const char __user * buf,
  708. size_t count, loff_t *ppos)
  709. {
  710. char *tmp;
  711. ssize_t ret;
  712. tmp = kmalloc(count + 1, GFP_KERNEL);
  713. if (tmp == NULL)
  714. return -ENOMEM;
  715. ret = -EFAULT;
  716. if (!copy_from_user(tmp, buf, count)) {
  717. tmp[count] = 0;
  718. ret = printk("%s", tmp);
  719. if (ret > count)
  720. /* printk can add a prefix */
  721. ret = count;
  722. }
  723. kfree(tmp);
  724. return ret;
  725. }
  726. static const struct file_operations kmsg_fops = {
  727. .write = kmsg_write,
  728. };
  729. static const struct memdev {
  730. const char *name;
  731. mode_t mode;
  732. const struct file_operations *fops;
  733. struct backing_dev_info *dev_info;
  734. } devlist[] = {
  735. [1] = { "mem", 0, &mem_fops, &directly_mappable_cdev_bdi },
  736. #ifdef CONFIG_DEVKMEM
  737. [2] = { "kmem", 0, &kmem_fops, &directly_mappable_cdev_bdi },
  738. #endif
  739. [3] = { "null", 0666, &null_fops, NULL },
  740. #ifdef CONFIG_DEVPORT
  741. [4] = { "port", 0, &port_fops, NULL },
  742. #endif
  743. [5] = { "zero", 0666, &zero_fops, &zero_bdi },
  744. [7] = { "full", 0666, &full_fops, NULL },
  745. [8] = { "random", 0666, &random_fops, NULL },
  746. [9] = { "urandom", 0666, &urandom_fops, NULL },
  747. [11] = { "kmsg", 0, &kmsg_fops, NULL },
  748. #ifdef CONFIG_CRASH_DUMP
  749. [12] = { "oldmem", 0, &oldmem_fops, NULL },
  750. #endif
  751. };
  752. static int memory_open(struct inode *inode, struct file *filp)
  753. {
  754. int minor;
  755. const struct memdev *dev;
  756. minor = iminor(inode);
  757. if (minor >= ARRAY_SIZE(devlist))
  758. return -ENXIO;
  759. dev = &devlist[minor];
  760. if (!dev->fops)
  761. return -ENXIO;
  762. filp->f_op = dev->fops;
  763. if (dev->dev_info)
  764. filp->f_mapping->backing_dev_info = dev->dev_info;
  765. if (dev->fops->open)
  766. return dev->fops->open(inode, filp);
  767. return 0;
  768. }
  769. static const struct file_operations memory_fops = {
  770. .open = memory_open,
  771. };
  772. static char *mem_devnode(struct device *dev, mode_t *mode)
  773. {
  774. if (mode && devlist[MINOR(dev->devt)].mode)
  775. *mode = devlist[MINOR(dev->devt)].mode;
  776. return NULL;
  777. }
  778. static struct class *mem_class;
  779. static int __init chr_dev_init(void)
  780. {
  781. int minor;
  782. int err;
  783. err = bdi_init(&zero_bdi);
  784. if (err)
  785. return err;
  786. if (register_chrdev(MEM_MAJOR,"mem",&memory_fops))
  787. printk("unable to get major %d for memory devs\n", MEM_MAJOR);
  788. mem_class = class_create(THIS_MODULE, "mem");
  789. mem_class->devnode = mem_devnode;
  790. for (minor = 1; minor < ARRAY_SIZE(devlist); minor++) {
  791. if (!devlist[minor].name)
  792. continue;
  793. device_create(mem_class, NULL, MKDEV(MEM_MAJOR, minor),
  794. NULL, devlist[minor].name);
  795. }
  796. return 0;
  797. }
  798. fs_initcall(chr_dev_init);