mem.c 20 KB

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