mem.c 21 KB

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