mem.c 19 KB

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