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