mem.c 21 KB

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