mem.c 21 KB

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