mem.c 21 KB

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