mem.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951
  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/ptrace.h>
  23. #include <linux/device.h>
  24. #include <linux/highmem.h>
  25. #include <linux/crash_dump.h>
  26. #include <linux/backing-dev.h>
  27. #include <linux/bootmem.h>
  28. #include <linux/pipe_fs_i.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 addr, 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. static int mmap_mem(struct file * file, struct vm_area_struct * vma)
  217. {
  218. size_t size = vma->vm_end - vma->vm_start;
  219. if (!valid_mmap_phys_addr_range(vma->vm_pgoff << PAGE_SHIFT, size))
  220. return -EINVAL;
  221. vma->vm_page_prot = phys_mem_access_prot(file, vma->vm_pgoff,
  222. size,
  223. vma->vm_page_prot);
  224. /* Remap-pfn-range will mark the range VM_IO and VM_RESERVED */
  225. if (remap_pfn_range(vma,
  226. vma->vm_start,
  227. vma->vm_pgoff,
  228. size,
  229. vma->vm_page_prot))
  230. return -EAGAIN;
  231. return 0;
  232. }
  233. static int mmap_kmem(struct file * file, struct vm_area_struct * vma)
  234. {
  235. unsigned long pfn;
  236. /* Turn a kernel-virtual address into a physical page frame */
  237. pfn = __pa((u64)vma->vm_pgoff << PAGE_SHIFT) >> PAGE_SHIFT;
  238. /*
  239. * RED-PEN: on some architectures there is more mapped memory
  240. * than available in mem_map which pfn_valid checks
  241. * for. Perhaps should add a new macro here.
  242. *
  243. * RED-PEN: vmalloc is not supported right now.
  244. */
  245. if (!pfn_valid(pfn))
  246. return -EIO;
  247. vma->vm_pgoff = pfn;
  248. return mmap_mem(file, vma);
  249. }
  250. #ifdef CONFIG_CRASH_DUMP
  251. /*
  252. * Read memory corresponding to the old kernel.
  253. */
  254. static ssize_t read_oldmem(struct file *file, char __user *buf,
  255. size_t count, loff_t *ppos)
  256. {
  257. unsigned long pfn, offset;
  258. size_t read = 0, csize;
  259. int rc = 0;
  260. while (count) {
  261. pfn = *ppos / PAGE_SIZE;
  262. if (pfn > saved_max_pfn)
  263. return read;
  264. offset = (unsigned long)(*ppos % PAGE_SIZE);
  265. if (count > PAGE_SIZE - offset)
  266. csize = PAGE_SIZE - offset;
  267. else
  268. csize = count;
  269. rc = copy_oldmem_page(pfn, buf, csize, offset, 1);
  270. if (rc < 0)
  271. return rc;
  272. buf += csize;
  273. *ppos += csize;
  274. read += csize;
  275. count -= csize;
  276. }
  277. return read;
  278. }
  279. #endif
  280. extern long vread(char *buf, char *addr, unsigned long count);
  281. extern long vwrite(char *buf, char *addr, unsigned long count);
  282. /*
  283. * This function reads the *virtual* memory as seen by the kernel.
  284. */
  285. static ssize_t read_kmem(struct file *file, char __user *buf,
  286. size_t count, loff_t *ppos)
  287. {
  288. unsigned long p = *ppos;
  289. ssize_t low_count, read, sz;
  290. char * kbuf; /* k-addr because vread() takes vmlist_lock rwlock */
  291. read = 0;
  292. if (p < (unsigned long) high_memory) {
  293. low_count = count;
  294. if (count > (unsigned long) high_memory - p)
  295. low_count = (unsigned long) high_memory - p;
  296. #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
  297. /* we don't have page 0 mapped on sparc and m68k.. */
  298. if (p < PAGE_SIZE && low_count > 0) {
  299. size_t tmp = PAGE_SIZE - p;
  300. if (tmp > low_count) tmp = low_count;
  301. if (clear_user(buf, tmp))
  302. return -EFAULT;
  303. buf += tmp;
  304. p += tmp;
  305. read += tmp;
  306. low_count -= tmp;
  307. count -= tmp;
  308. }
  309. #endif
  310. while (low_count > 0) {
  311. /*
  312. * Handle first page in case it's not aligned
  313. */
  314. if (-p & (PAGE_SIZE - 1))
  315. sz = -p & (PAGE_SIZE - 1);
  316. else
  317. sz = PAGE_SIZE;
  318. sz = min_t(unsigned long, sz, low_count);
  319. /*
  320. * On ia64 if a page has been mapped somewhere as
  321. * uncached, then it must also be accessed uncached
  322. * by the kernel or data corruption may occur
  323. */
  324. kbuf = xlate_dev_kmem_ptr((char *)p);
  325. if (copy_to_user(buf, kbuf, sz))
  326. return -EFAULT;
  327. buf += sz;
  328. p += sz;
  329. read += sz;
  330. low_count -= sz;
  331. count -= sz;
  332. }
  333. }
  334. if (count > 0) {
  335. kbuf = (char *)__get_free_page(GFP_KERNEL);
  336. if (!kbuf)
  337. return -ENOMEM;
  338. while (count > 0) {
  339. int len = count;
  340. if (len > PAGE_SIZE)
  341. len = PAGE_SIZE;
  342. len = vread(kbuf, (char *)p, len);
  343. if (!len)
  344. break;
  345. if (copy_to_user(buf, kbuf, len)) {
  346. free_page((unsigned long)kbuf);
  347. return -EFAULT;
  348. }
  349. count -= len;
  350. buf += len;
  351. read += len;
  352. p += len;
  353. }
  354. free_page((unsigned long)kbuf);
  355. }
  356. *ppos = p;
  357. return read;
  358. }
  359. static inline ssize_t
  360. do_write_kmem(void *p, unsigned long realp, const char __user * buf,
  361. size_t count, loff_t *ppos)
  362. {
  363. ssize_t written, sz;
  364. unsigned long copied;
  365. written = 0;
  366. #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
  367. /* we don't have page 0 mapped on sparc and m68k.. */
  368. if (realp < PAGE_SIZE) {
  369. unsigned long sz = PAGE_SIZE - realp;
  370. if (sz > count)
  371. sz = count;
  372. /* Hmm. Do something? */
  373. buf += sz;
  374. p += sz;
  375. realp += sz;
  376. count -= sz;
  377. written += sz;
  378. }
  379. #endif
  380. while (count > 0) {
  381. char *ptr;
  382. /*
  383. * Handle first page in case it's not aligned
  384. */
  385. if (-realp & (PAGE_SIZE - 1))
  386. sz = -realp & (PAGE_SIZE - 1);
  387. else
  388. sz = PAGE_SIZE;
  389. sz = min_t(unsigned long, sz, count);
  390. /*
  391. * On ia64 if a page has been mapped somewhere as
  392. * uncached, then it must also be accessed uncached
  393. * by the kernel or data corruption may occur
  394. */
  395. ptr = xlate_dev_kmem_ptr(p);
  396. copied = copy_from_user(ptr, buf, sz);
  397. if (copied) {
  398. written += sz - copied;
  399. if (written)
  400. break;
  401. return -EFAULT;
  402. }
  403. buf += sz;
  404. p += sz;
  405. realp += sz;
  406. count -= sz;
  407. written += sz;
  408. }
  409. *ppos += written;
  410. return written;
  411. }
  412. /*
  413. * This function writes to the *virtual* memory as seen by the kernel.
  414. */
  415. static ssize_t write_kmem(struct file * file, const char __user * buf,
  416. size_t count, loff_t *ppos)
  417. {
  418. unsigned long p = *ppos;
  419. ssize_t wrote = 0;
  420. ssize_t virtr = 0;
  421. ssize_t written;
  422. char * kbuf; /* k-addr because vwrite() takes vmlist_lock rwlock */
  423. if (p < (unsigned long) high_memory) {
  424. wrote = count;
  425. if (count > (unsigned long) high_memory - p)
  426. wrote = (unsigned long) high_memory - p;
  427. written = do_write_kmem((void*)p, p, buf, wrote, ppos);
  428. if (written != wrote)
  429. return written;
  430. wrote = written;
  431. p += wrote;
  432. buf += wrote;
  433. count -= wrote;
  434. }
  435. if (count > 0) {
  436. kbuf = (char *)__get_free_page(GFP_KERNEL);
  437. if (!kbuf)
  438. return wrote ? wrote : -ENOMEM;
  439. while (count > 0) {
  440. int len = count;
  441. if (len > PAGE_SIZE)
  442. len = PAGE_SIZE;
  443. if (len) {
  444. written = copy_from_user(kbuf, buf, len);
  445. if (written) {
  446. if (wrote + virtr)
  447. break;
  448. free_page((unsigned long)kbuf);
  449. return -EFAULT;
  450. }
  451. }
  452. len = vwrite(kbuf, (char *)p, len);
  453. count -= len;
  454. buf += len;
  455. virtr += len;
  456. p += len;
  457. }
  458. free_page((unsigned long)kbuf);
  459. }
  460. *ppos = p;
  461. return virtr + wrote;
  462. }
  463. #if defined(CONFIG_ISA) || !defined(__mc68000__)
  464. static ssize_t read_port(struct file * file, char __user * buf,
  465. size_t count, loff_t *ppos)
  466. {
  467. unsigned long i = *ppos;
  468. char __user *tmp = buf;
  469. if (!access_ok(VERIFY_WRITE, buf, count))
  470. return -EFAULT;
  471. while (count-- > 0 && i < 65536) {
  472. if (__put_user(inb(i),tmp) < 0)
  473. return -EFAULT;
  474. i++;
  475. tmp++;
  476. }
  477. *ppos = i;
  478. return tmp-buf;
  479. }
  480. static ssize_t write_port(struct file * file, const char __user * buf,
  481. size_t count, loff_t *ppos)
  482. {
  483. unsigned long i = *ppos;
  484. const char __user * tmp = buf;
  485. if (!access_ok(VERIFY_READ,buf,count))
  486. return -EFAULT;
  487. while (count-- > 0 && i < 65536) {
  488. char c;
  489. if (__get_user(c, tmp)) {
  490. if (tmp > buf)
  491. break;
  492. return -EFAULT;
  493. }
  494. outb(c,i);
  495. i++;
  496. tmp++;
  497. }
  498. *ppos = i;
  499. return tmp-buf;
  500. }
  501. #endif
  502. static ssize_t read_null(struct file * file, char __user * buf,
  503. size_t count, loff_t *ppos)
  504. {
  505. return 0;
  506. }
  507. static ssize_t write_null(struct file * file, const char __user * buf,
  508. size_t count, loff_t *ppos)
  509. {
  510. return count;
  511. }
  512. static int pipe_to_null(struct pipe_inode_info *info, struct pipe_buffer *buf,
  513. struct splice_desc *sd)
  514. {
  515. return sd->len;
  516. }
  517. static ssize_t splice_write_null(struct pipe_inode_info *pipe,struct file *out,
  518. loff_t *ppos, size_t len, unsigned int flags)
  519. {
  520. return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_null);
  521. }
  522. #ifdef CONFIG_MMU
  523. /*
  524. * For fun, we are using the MMU for this.
  525. */
  526. static inline size_t read_zero_pagealigned(char __user * buf, size_t size)
  527. {
  528. struct mm_struct *mm;
  529. struct vm_area_struct * vma;
  530. unsigned long addr=(unsigned long)buf;
  531. mm = current->mm;
  532. /* Oops, this was forgotten before. -ben */
  533. down_read(&mm->mmap_sem);
  534. /* For private mappings, just map in zero pages. */
  535. for (vma = find_vma(mm, addr); vma; vma = vma->vm_next) {
  536. unsigned long count;
  537. if (vma->vm_start > addr || (vma->vm_flags & VM_WRITE) == 0)
  538. goto out_up;
  539. if (vma->vm_flags & (VM_SHARED | VM_HUGETLB))
  540. break;
  541. count = vma->vm_end - addr;
  542. if (count > size)
  543. count = size;
  544. zap_page_range(vma, addr, count, NULL);
  545. zeromap_page_range(vma, addr, count, PAGE_COPY);
  546. size -= count;
  547. buf += count;
  548. addr += count;
  549. if (size == 0)
  550. goto out_up;
  551. }
  552. up_read(&mm->mmap_sem);
  553. /* The shared case is hard. Let's do the conventional zeroing. */
  554. do {
  555. unsigned long unwritten = clear_user(buf, PAGE_SIZE);
  556. if (unwritten)
  557. return size + unwritten - PAGE_SIZE;
  558. cond_resched();
  559. buf += PAGE_SIZE;
  560. size -= PAGE_SIZE;
  561. } while (size);
  562. return size;
  563. out_up:
  564. up_read(&mm->mmap_sem);
  565. return size;
  566. }
  567. static ssize_t read_zero(struct file * file, char __user * buf,
  568. size_t count, loff_t *ppos)
  569. {
  570. unsigned long left, unwritten, written = 0;
  571. if (!count)
  572. return 0;
  573. if (!access_ok(VERIFY_WRITE, buf, count))
  574. return -EFAULT;
  575. left = count;
  576. /* do we want to be clever? Arbitrary cut-off */
  577. if (count >= PAGE_SIZE*4) {
  578. unsigned long partial;
  579. /* How much left of the page? */
  580. partial = (PAGE_SIZE-1) & -(unsigned long) buf;
  581. unwritten = clear_user(buf, partial);
  582. written = partial - unwritten;
  583. if (unwritten)
  584. goto out;
  585. left -= partial;
  586. buf += partial;
  587. unwritten = read_zero_pagealigned(buf, left & PAGE_MASK);
  588. written += (left & PAGE_MASK) - unwritten;
  589. if (unwritten)
  590. goto out;
  591. buf += left & PAGE_MASK;
  592. left &= ~PAGE_MASK;
  593. }
  594. unwritten = clear_user(buf, left);
  595. written += left - unwritten;
  596. out:
  597. return written ? written : -EFAULT;
  598. }
  599. static int mmap_zero(struct file * file, struct vm_area_struct * vma)
  600. {
  601. if (vma->vm_flags & VM_SHARED)
  602. return shmem_zero_setup(vma);
  603. if (zeromap_page_range(vma, vma->vm_start, vma->vm_end - vma->vm_start, vma->vm_page_prot))
  604. return -EAGAIN;
  605. return 0;
  606. }
  607. #else /* CONFIG_MMU */
  608. static ssize_t read_zero(struct file * file, char * buf,
  609. size_t count, loff_t *ppos)
  610. {
  611. size_t todo = count;
  612. while (todo) {
  613. size_t chunk = todo;
  614. if (chunk > 4096)
  615. chunk = 4096; /* Just for latency reasons */
  616. if (clear_user(buf, chunk))
  617. return -EFAULT;
  618. buf += chunk;
  619. todo -= chunk;
  620. cond_resched();
  621. }
  622. return count;
  623. }
  624. static int mmap_zero(struct file * file, struct vm_area_struct * vma)
  625. {
  626. return -ENOSYS;
  627. }
  628. #endif /* CONFIG_MMU */
  629. static ssize_t write_full(struct file * file, const char __user * buf,
  630. size_t count, loff_t *ppos)
  631. {
  632. return -ENOSPC;
  633. }
  634. /*
  635. * Special lseek() function for /dev/null and /dev/zero. Most notably, you
  636. * can fopen() both devices with "a" now. This was previously impossible.
  637. * -- SRB.
  638. */
  639. static loff_t null_lseek(struct file * file, loff_t offset, int orig)
  640. {
  641. return file->f_pos = 0;
  642. }
  643. /*
  644. * The memory devices use the full 32/64 bits of the offset, and so we cannot
  645. * check against negative addresses: they are ok. The return value is weird,
  646. * though, in that case (0).
  647. *
  648. * also note that seeking relative to the "end of file" isn't supported:
  649. * it has no meaning, so it returns -EINVAL.
  650. */
  651. static loff_t memory_lseek(struct file * file, loff_t offset, int orig)
  652. {
  653. loff_t ret;
  654. mutex_lock(&file->f_dentry->d_inode->i_mutex);
  655. switch (orig) {
  656. case 0:
  657. file->f_pos = offset;
  658. ret = file->f_pos;
  659. force_successful_syscall_return();
  660. break;
  661. case 1:
  662. file->f_pos += offset;
  663. ret = file->f_pos;
  664. force_successful_syscall_return();
  665. break;
  666. default:
  667. ret = -EINVAL;
  668. }
  669. mutex_unlock(&file->f_dentry->d_inode->i_mutex);
  670. return ret;
  671. }
  672. static int open_port(struct inode * inode, struct file * filp)
  673. {
  674. return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
  675. }
  676. #define zero_lseek null_lseek
  677. #define full_lseek null_lseek
  678. #define write_zero write_null
  679. #define read_full read_zero
  680. #define open_mem open_port
  681. #define open_kmem open_mem
  682. #define open_oldmem open_mem
  683. static struct file_operations mem_fops = {
  684. .llseek = memory_lseek,
  685. .read = read_mem,
  686. .write = write_mem,
  687. .mmap = mmap_mem,
  688. .open = open_mem,
  689. };
  690. static struct file_operations kmem_fops = {
  691. .llseek = memory_lseek,
  692. .read = read_kmem,
  693. .write = write_kmem,
  694. .mmap = mmap_kmem,
  695. .open = open_kmem,
  696. };
  697. static struct file_operations null_fops = {
  698. .llseek = null_lseek,
  699. .read = read_null,
  700. .write = write_null,
  701. .splice_write = splice_write_null,
  702. };
  703. #if defined(CONFIG_ISA) || !defined(__mc68000__)
  704. static struct file_operations port_fops = {
  705. .llseek = memory_lseek,
  706. .read = read_port,
  707. .write = write_port,
  708. .open = open_port,
  709. };
  710. #endif
  711. static struct file_operations zero_fops = {
  712. .llseek = zero_lseek,
  713. .read = read_zero,
  714. .write = write_zero,
  715. .mmap = mmap_zero,
  716. };
  717. static struct backing_dev_info zero_bdi = {
  718. .capabilities = BDI_CAP_MAP_COPY,
  719. };
  720. static struct file_operations full_fops = {
  721. .llseek = full_lseek,
  722. .read = read_full,
  723. .write = write_full,
  724. };
  725. #ifdef CONFIG_CRASH_DUMP
  726. static struct file_operations oldmem_fops = {
  727. .read = read_oldmem,
  728. .open = open_oldmem,
  729. };
  730. #endif
  731. static ssize_t kmsg_write(struct file * file, const char __user * buf,
  732. size_t count, loff_t *ppos)
  733. {
  734. char *tmp;
  735. ssize_t ret;
  736. tmp = kmalloc(count + 1, GFP_KERNEL);
  737. if (tmp == NULL)
  738. return -ENOMEM;
  739. ret = -EFAULT;
  740. if (!copy_from_user(tmp, buf, count)) {
  741. tmp[count] = 0;
  742. ret = printk("%s", tmp);
  743. if (ret > count)
  744. /* printk can add a prefix */
  745. ret = count;
  746. }
  747. kfree(tmp);
  748. return ret;
  749. }
  750. static struct file_operations kmsg_fops = {
  751. .write = kmsg_write,
  752. };
  753. static int memory_open(struct inode * inode, struct file * filp)
  754. {
  755. switch (iminor(inode)) {
  756. case 1:
  757. filp->f_op = &mem_fops;
  758. break;
  759. case 2:
  760. filp->f_op = &kmem_fops;
  761. break;
  762. case 3:
  763. filp->f_op = &null_fops;
  764. break;
  765. #if defined(CONFIG_ISA) || !defined(__mc68000__)
  766. case 4:
  767. filp->f_op = &port_fops;
  768. break;
  769. #endif
  770. case 5:
  771. filp->f_mapping->backing_dev_info = &zero_bdi;
  772. filp->f_op = &zero_fops;
  773. break;
  774. case 7:
  775. filp->f_op = &full_fops;
  776. break;
  777. case 8:
  778. filp->f_op = &random_fops;
  779. break;
  780. case 9:
  781. filp->f_op = &urandom_fops;
  782. break;
  783. case 11:
  784. filp->f_op = &kmsg_fops;
  785. break;
  786. #ifdef CONFIG_CRASH_DUMP
  787. case 12:
  788. filp->f_op = &oldmem_fops;
  789. break;
  790. #endif
  791. default:
  792. return -ENXIO;
  793. }
  794. if (filp->f_op && filp->f_op->open)
  795. return filp->f_op->open(inode,filp);
  796. return 0;
  797. }
  798. static struct file_operations memory_fops = {
  799. .open = memory_open, /* just a selector for the real open */
  800. };
  801. static const struct {
  802. unsigned int minor;
  803. char *name;
  804. umode_t mode;
  805. const struct file_operations *fops;
  806. } devlist[] = { /* list of minor devices */
  807. {1, "mem", S_IRUSR | S_IWUSR | S_IRGRP, &mem_fops},
  808. {2, "kmem", S_IRUSR | S_IWUSR | S_IRGRP, &kmem_fops},
  809. {3, "null", S_IRUGO | S_IWUGO, &null_fops},
  810. #if defined(CONFIG_ISA) || !defined(__mc68000__)
  811. {4, "port", S_IRUSR | S_IWUSR | S_IRGRP, &port_fops},
  812. #endif
  813. {5, "zero", S_IRUGO | S_IWUGO, &zero_fops},
  814. {7, "full", S_IRUGO | S_IWUGO, &full_fops},
  815. {8, "random", S_IRUGO | S_IWUSR, &random_fops},
  816. {9, "urandom", S_IRUGO | S_IWUSR, &urandom_fops},
  817. {11,"kmsg", S_IRUGO | S_IWUSR, &kmsg_fops},
  818. #ifdef CONFIG_CRASH_DUMP
  819. {12,"oldmem", S_IRUSR | S_IWUSR | S_IRGRP, &oldmem_fops},
  820. #endif
  821. };
  822. static struct class *mem_class;
  823. static int __init chr_dev_init(void)
  824. {
  825. int i;
  826. if (register_chrdev(MEM_MAJOR,"mem",&memory_fops))
  827. printk("unable to get major %d for memory devs\n", MEM_MAJOR);
  828. mem_class = class_create(THIS_MODULE, "mem");
  829. for (i = 0; i < ARRAY_SIZE(devlist); i++)
  830. class_device_create(mem_class, NULL,
  831. MKDEV(MEM_MAJOR, devlist[i].minor),
  832. NULL, devlist[i].name);
  833. return 0;
  834. }
  835. fs_initcall(chr_dev_init);