mem.c 21 KB

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