mem.c 21 KB

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