mem.c 22 KB

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