mem.c 21 KB

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