mem.c 20 KB

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