mem.c 21 KB

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