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