mem.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941
  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. 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. #ifdef CONFIG_MMU
  513. /*
  514. * For fun, we are using the MMU for this.
  515. */
  516. static inline size_t read_zero_pagealigned(char __user * buf, size_t size)
  517. {
  518. struct mm_struct *mm;
  519. struct vm_area_struct * vma;
  520. unsigned long addr=(unsigned long)buf;
  521. mm = current->mm;
  522. /* Oops, this was forgotten before. -ben */
  523. down_read(&mm->mmap_sem);
  524. /* For private mappings, just map in zero pages. */
  525. for (vma = find_vma(mm, addr); vma; vma = vma->vm_next) {
  526. unsigned long count;
  527. if (vma->vm_start > addr || (vma->vm_flags & VM_WRITE) == 0)
  528. goto out_up;
  529. if (vma->vm_flags & (VM_SHARED | VM_HUGETLB))
  530. break;
  531. count = vma->vm_end - addr;
  532. if (count > size)
  533. count = size;
  534. zap_page_range(vma, addr, count, NULL);
  535. zeromap_page_range(vma, addr, count, PAGE_COPY);
  536. size -= count;
  537. buf += count;
  538. addr += count;
  539. if (size == 0)
  540. goto out_up;
  541. }
  542. up_read(&mm->mmap_sem);
  543. /* The shared case is hard. Let's do the conventional zeroing. */
  544. do {
  545. unsigned long unwritten = clear_user(buf, PAGE_SIZE);
  546. if (unwritten)
  547. return size + unwritten - PAGE_SIZE;
  548. cond_resched();
  549. buf += PAGE_SIZE;
  550. size -= PAGE_SIZE;
  551. } while (size);
  552. return size;
  553. out_up:
  554. up_read(&mm->mmap_sem);
  555. return size;
  556. }
  557. static ssize_t read_zero(struct file * file, char __user * buf,
  558. size_t count, loff_t *ppos)
  559. {
  560. unsigned long left, unwritten, written = 0;
  561. if (!count)
  562. return 0;
  563. if (!access_ok(VERIFY_WRITE, buf, count))
  564. return -EFAULT;
  565. left = count;
  566. /* do we want to be clever? Arbitrary cut-off */
  567. if (count >= PAGE_SIZE*4) {
  568. unsigned long partial;
  569. /* How much left of the page? */
  570. partial = (PAGE_SIZE-1) & -(unsigned long) buf;
  571. unwritten = clear_user(buf, partial);
  572. written = partial - unwritten;
  573. if (unwritten)
  574. goto out;
  575. left -= partial;
  576. buf += partial;
  577. unwritten = read_zero_pagealigned(buf, left & PAGE_MASK);
  578. written += (left & PAGE_MASK) - unwritten;
  579. if (unwritten)
  580. goto out;
  581. buf += left & PAGE_MASK;
  582. left &= ~PAGE_MASK;
  583. }
  584. unwritten = clear_user(buf, left);
  585. written += left - unwritten;
  586. out:
  587. return written ? written : -EFAULT;
  588. }
  589. static int mmap_zero(struct file * file, struct vm_area_struct * vma)
  590. {
  591. if (vma->vm_flags & VM_SHARED)
  592. return shmem_zero_setup(vma);
  593. if (zeromap_page_range(vma, vma->vm_start, vma->vm_end - vma->vm_start, vma->vm_page_prot))
  594. return -EAGAIN;
  595. return 0;
  596. }
  597. #else /* CONFIG_MMU */
  598. static ssize_t read_zero(struct file * file, char * buf,
  599. size_t count, loff_t *ppos)
  600. {
  601. size_t todo = count;
  602. while (todo) {
  603. size_t chunk = todo;
  604. if (chunk > 4096)
  605. chunk = 4096; /* Just for latency reasons */
  606. if (clear_user(buf, chunk))
  607. return -EFAULT;
  608. buf += chunk;
  609. todo -= chunk;
  610. cond_resched();
  611. }
  612. return count;
  613. }
  614. static int mmap_zero(struct file * file, struct vm_area_struct * vma)
  615. {
  616. return -ENOSYS;
  617. }
  618. #endif /* CONFIG_MMU */
  619. static ssize_t write_full(struct file * file, const char __user * buf,
  620. size_t count, loff_t *ppos)
  621. {
  622. return -ENOSPC;
  623. }
  624. /*
  625. * Special lseek() function for /dev/null and /dev/zero. Most notably, you
  626. * can fopen() both devices with "a" now. This was previously impossible.
  627. * -- SRB.
  628. */
  629. static loff_t null_lseek(struct file * file, loff_t offset, int orig)
  630. {
  631. return file->f_pos = 0;
  632. }
  633. /*
  634. * The memory devices use the full 32/64 bits of the offset, and so we cannot
  635. * check against negative addresses: they are ok. The return value is weird,
  636. * though, in that case (0).
  637. *
  638. * also note that seeking relative to the "end of file" isn't supported:
  639. * it has no meaning, so it returns -EINVAL.
  640. */
  641. static loff_t memory_lseek(struct file * file, loff_t offset, int orig)
  642. {
  643. loff_t ret;
  644. mutex_lock(&file->f_dentry->d_inode->i_mutex);
  645. switch (orig) {
  646. case 0:
  647. file->f_pos = offset;
  648. ret = file->f_pos;
  649. force_successful_syscall_return();
  650. break;
  651. case 1:
  652. file->f_pos += offset;
  653. ret = file->f_pos;
  654. force_successful_syscall_return();
  655. break;
  656. default:
  657. ret = -EINVAL;
  658. }
  659. mutex_unlock(&file->f_dentry->d_inode->i_mutex);
  660. return ret;
  661. }
  662. static int open_port(struct inode * inode, struct file * filp)
  663. {
  664. return capable(CAP_SYS_RAWIO) ? 0 : -EPERM;
  665. }
  666. #define zero_lseek null_lseek
  667. #define full_lseek null_lseek
  668. #define write_zero write_null
  669. #define read_full read_zero
  670. #define open_mem open_port
  671. #define open_kmem open_mem
  672. #define open_oldmem open_mem
  673. static struct file_operations mem_fops = {
  674. .llseek = memory_lseek,
  675. .read = read_mem,
  676. .write = write_mem,
  677. .mmap = mmap_mem,
  678. .open = open_mem,
  679. };
  680. static struct file_operations kmem_fops = {
  681. .llseek = memory_lseek,
  682. .read = read_kmem,
  683. .write = write_kmem,
  684. .mmap = mmap_kmem,
  685. .open = open_kmem,
  686. };
  687. static struct file_operations null_fops = {
  688. .llseek = null_lseek,
  689. .read = read_null,
  690. .write = write_null,
  691. };
  692. #if defined(CONFIG_ISA) || !defined(__mc68000__)
  693. static struct file_operations port_fops = {
  694. .llseek = memory_lseek,
  695. .read = read_port,
  696. .write = write_port,
  697. .open = open_port,
  698. };
  699. #endif
  700. static struct file_operations zero_fops = {
  701. .llseek = zero_lseek,
  702. .read = read_zero,
  703. .write = write_zero,
  704. .mmap = mmap_zero,
  705. };
  706. static struct backing_dev_info zero_bdi = {
  707. .capabilities = BDI_CAP_MAP_COPY,
  708. };
  709. static struct file_operations full_fops = {
  710. .llseek = full_lseek,
  711. .read = read_full,
  712. .write = write_full,
  713. };
  714. #ifdef CONFIG_CRASH_DUMP
  715. static struct file_operations oldmem_fops = {
  716. .read = read_oldmem,
  717. .open = open_oldmem,
  718. };
  719. #endif
  720. static ssize_t kmsg_write(struct file * file, const char __user * buf,
  721. size_t count, loff_t *ppos)
  722. {
  723. char *tmp;
  724. ssize_t ret;
  725. tmp = kmalloc(count + 1, GFP_KERNEL);
  726. if (tmp == NULL)
  727. return -ENOMEM;
  728. ret = -EFAULT;
  729. if (!copy_from_user(tmp, buf, count)) {
  730. tmp[count] = 0;
  731. ret = printk("%s", tmp);
  732. if (ret > count)
  733. /* printk can add a prefix */
  734. ret = count;
  735. }
  736. kfree(tmp);
  737. return ret;
  738. }
  739. static struct file_operations kmsg_fops = {
  740. .write = kmsg_write,
  741. };
  742. static int memory_open(struct inode * inode, struct file * filp)
  743. {
  744. switch (iminor(inode)) {
  745. case 1:
  746. filp->f_op = &mem_fops;
  747. break;
  748. case 2:
  749. filp->f_op = &kmem_fops;
  750. break;
  751. case 3:
  752. filp->f_op = &null_fops;
  753. break;
  754. #if defined(CONFIG_ISA) || !defined(__mc68000__)
  755. case 4:
  756. filp->f_op = &port_fops;
  757. break;
  758. #endif
  759. case 5:
  760. filp->f_mapping->backing_dev_info = &zero_bdi;
  761. filp->f_op = &zero_fops;
  762. break;
  763. case 7:
  764. filp->f_op = &full_fops;
  765. break;
  766. case 8:
  767. filp->f_op = &random_fops;
  768. break;
  769. case 9:
  770. filp->f_op = &urandom_fops;
  771. break;
  772. case 11:
  773. filp->f_op = &kmsg_fops;
  774. break;
  775. #ifdef CONFIG_CRASH_DUMP
  776. case 12:
  777. filp->f_op = &oldmem_fops;
  778. break;
  779. #endif
  780. default:
  781. return -ENXIO;
  782. }
  783. if (filp->f_op && filp->f_op->open)
  784. return filp->f_op->open(inode,filp);
  785. return 0;
  786. }
  787. static struct file_operations memory_fops = {
  788. .open = memory_open, /* just a selector for the real open */
  789. };
  790. static const struct {
  791. unsigned int minor;
  792. char *name;
  793. umode_t mode;
  794. const struct file_operations *fops;
  795. } devlist[] = { /* list of minor devices */
  796. {1, "mem", S_IRUSR | S_IWUSR | S_IRGRP, &mem_fops},
  797. {2, "kmem", S_IRUSR | S_IWUSR | S_IRGRP, &kmem_fops},
  798. {3, "null", S_IRUGO | S_IWUGO, &null_fops},
  799. #if defined(CONFIG_ISA) || !defined(__mc68000__)
  800. {4, "port", S_IRUSR | S_IWUSR | S_IRGRP, &port_fops},
  801. #endif
  802. {5, "zero", S_IRUGO | S_IWUGO, &zero_fops},
  803. {7, "full", S_IRUGO | S_IWUGO, &full_fops},
  804. {8, "random", S_IRUGO | S_IWUSR, &random_fops},
  805. {9, "urandom", S_IRUGO | S_IWUSR, &urandom_fops},
  806. {11,"kmsg", S_IRUGO | S_IWUSR, &kmsg_fops},
  807. #ifdef CONFIG_CRASH_DUMP
  808. {12,"oldmem", S_IRUSR | S_IWUSR | S_IRGRP, &oldmem_fops},
  809. #endif
  810. };
  811. static struct class *mem_class;
  812. static int __init chr_dev_init(void)
  813. {
  814. int i;
  815. if (register_chrdev(MEM_MAJOR,"mem",&memory_fops))
  816. printk("unable to get major %d for memory devs\n", MEM_MAJOR);
  817. mem_class = class_create(THIS_MODULE, "mem");
  818. for (i = 0; i < ARRAY_SIZE(devlist); i++) {
  819. class_device_create(mem_class, NULL,
  820. MKDEV(MEM_MAJOR, devlist[i].minor),
  821. NULL, devlist[i].name);
  822. devfs_mk_cdev(MKDEV(MEM_MAJOR, devlist[i].minor),
  823. S_IFCHR | devlist[i].mode, devlist[i].name);
  824. }
  825. return 0;
  826. }
  827. fs_initcall(chr_dev_init);