mem.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/drivers/char/mem.c
  4. *
  5. * Copyright (C) 1991, 1992 Linus Torvalds
  6. *
  7. * Added devfs support.
  8. * Jan-11-1998, C. Scott Ananian <[email protected]>
  9. * Shared /dev/zero mmapping support, Feb 2000, Kanoj Sarcar <[email protected]>
  10. */
  11. #include <linux/mm.h>
  12. #include <linux/miscdevice.h>
  13. #include <linux/slab.h>
  14. #include <linux/vmalloc.h>
  15. #include <linux/mman.h>
  16. #include <linux/random.h>
  17. #include <linux/init.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/backing-dev.h>
  24. #include <linux/shmem_fs.h>
  25. #include <linux/splice.h>
  26. #include <linux/pfn.h>
  27. #include <linux/export.h>
  28. #include <linux/io.h>
  29. #include <linux/uio.h>
  30. #include <linux/uaccess.h>
  31. #include <linux/security.h>
  32. #ifdef CONFIG_IA64
  33. # include <linux/efi.h>
  34. #endif
  35. #define DEVMEM_MINOR 1
  36. #define DEVPORT_MINOR 4
  37. static inline unsigned long size_inside_page(unsigned long start,
  38. unsigned long size)
  39. {
  40. unsigned long sz;
  41. sz = PAGE_SIZE - (start & (PAGE_SIZE - 1));
  42. return min(sz, size);
  43. }
  44. #ifndef ARCH_HAS_VALID_PHYS_ADDR_RANGE
  45. static inline int valid_phys_addr_range(phys_addr_t addr, size_t count)
  46. {
  47. return addr + count <= __pa(high_memory);
  48. }
  49. static inline int valid_mmap_phys_addr_range(unsigned long pfn, size_t size)
  50. {
  51. return 1;
  52. }
  53. #endif
  54. #ifdef CONFIG_STRICT_DEVMEM
  55. static inline int page_is_allowed(unsigned long pfn)
  56. {
  57. return devmem_is_allowed(pfn);
  58. }
  59. static inline int range_is_allowed(unsigned long pfn, unsigned long size)
  60. {
  61. u64 from = ((u64)pfn) << PAGE_SHIFT;
  62. u64 to = from + size;
  63. u64 cursor = from;
  64. while (cursor < to) {
  65. if (!devmem_is_allowed(pfn))
  66. return 0;
  67. cursor += PAGE_SIZE;
  68. pfn++;
  69. }
  70. return 1;
  71. }
  72. #else
  73. static inline int page_is_allowed(unsigned long pfn)
  74. {
  75. return 1;
  76. }
  77. static inline int range_is_allowed(unsigned long pfn, unsigned long size)
  78. {
  79. return 1;
  80. }
  81. #endif
  82. #ifndef unxlate_dev_mem_ptr
  83. #define unxlate_dev_mem_ptr unxlate_dev_mem_ptr
  84. void __weak unxlate_dev_mem_ptr(phys_addr_t phys, void *addr)
  85. {
  86. }
  87. #endif
  88. static inline bool should_stop_iteration(void)
  89. {
  90. if (need_resched())
  91. cond_resched();
  92. return signal_pending(current);
  93. }
  94. /*
  95. * This funcion reads the *physical* memory. The f_pos points directly to the
  96. * memory location.
  97. */
  98. static ssize_t read_mem(struct file *file, char __user *buf,
  99. size_t count, loff_t *ppos)
  100. {
  101. phys_addr_t p = *ppos;
  102. ssize_t read, sz;
  103. void *ptr;
  104. char *bounce;
  105. int err;
  106. if (p != *ppos)
  107. return 0;
  108. if (!valid_phys_addr_range(p, count))
  109. return -EFAULT;
  110. read = 0;
  111. #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
  112. /* we don't have page 0 mapped on sparc and m68k.. */
  113. if (p < PAGE_SIZE) {
  114. sz = size_inside_page(p, count);
  115. if (sz > 0) {
  116. if (clear_user(buf, sz))
  117. return -EFAULT;
  118. buf += sz;
  119. p += sz;
  120. count -= sz;
  121. read += sz;
  122. }
  123. }
  124. #endif
  125. bounce = kmalloc(PAGE_SIZE, GFP_KERNEL);
  126. if (!bounce)
  127. return -ENOMEM;
  128. while (count > 0) {
  129. unsigned long remaining;
  130. int allowed, probe;
  131. sz = size_inside_page(p, count);
  132. err = -EPERM;
  133. allowed = page_is_allowed(p >> PAGE_SHIFT);
  134. if (!allowed)
  135. goto failed;
  136. err = -EFAULT;
  137. if (allowed == 2) {
  138. /* Show zeros for restricted memory. */
  139. remaining = clear_user(buf, sz);
  140. } else {
  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. goto failed;
  149. probe = copy_from_kernel_nofault(bounce, ptr, sz);
  150. unxlate_dev_mem_ptr(p, ptr);
  151. if (probe)
  152. goto failed;
  153. remaining = copy_to_user(buf, bounce, sz);
  154. }
  155. if (remaining)
  156. goto failed;
  157. buf += sz;
  158. p += sz;
  159. count -= sz;
  160. read += sz;
  161. if (should_stop_iteration())
  162. break;
  163. }
  164. kfree(bounce);
  165. *ppos += read;
  166. return read;
  167. failed:
  168. kfree(bounce);
  169. return err;
  170. }
  171. static ssize_t write_mem(struct file *file, const char __user *buf,
  172. size_t count, loff_t *ppos)
  173. {
  174. phys_addr_t p = *ppos;
  175. ssize_t written, sz;
  176. unsigned long copied;
  177. void *ptr;
  178. if (p != *ppos)
  179. return -EFBIG;
  180. if (!valid_phys_addr_range(p, count))
  181. return -EFAULT;
  182. written = 0;
  183. #ifdef __ARCH_HAS_NO_PAGE_ZERO_MAPPED
  184. /* we don't have page 0 mapped on sparc and m68k.. */
  185. if (p < PAGE_SIZE) {
  186. sz = size_inside_page(p, count);
  187. /* Hmm. Do something? */
  188. buf += sz;
  189. p += sz;
  190. count -= sz;
  191. written += sz;
  192. }
  193. #endif
  194. while (count > 0) {
  195. int allowed;
  196. sz = size_inside_page(p, count);
  197. allowed = page_is_allowed(p >> PAGE_SHIFT);
  198. if (!allowed)
  199. return -EPERM;
  200. /* Skip actual writing when a page is marked as restricted. */
  201. if (allowed == 1) {
  202. /*
  203. * On ia64 if a page has been mapped somewhere as
  204. * uncached, then it must also be accessed uncached
  205. * by the kernel or data corruption may occur.
  206. */
  207. ptr = xlate_dev_mem_ptr(p);
  208. if (!ptr) {
  209. if (written)
  210. break;
  211. return -EFAULT;
  212. }
  213. copied = copy_from_user(ptr, buf, sz);
  214. unxlate_dev_mem_ptr(p, ptr);
  215. if (copied) {
  216. written += sz - copied;
  217. if (written)
  218. break;
  219. return -EFAULT;
  220. }
  221. }
  222. buf += sz;
  223. p += sz;
  224. count -= sz;
  225. written += sz;
  226. if (should_stop_iteration())
  227. break;
  228. }
  229. *ppos += written;
  230. return written;
  231. }
  232. int __weak phys_mem_access_prot_allowed(struct file *file,
  233. unsigned long pfn, unsigned long size, pgprot_t *vma_prot)
  234. {
  235. return 1;
  236. }
  237. #ifndef __HAVE_PHYS_MEM_ACCESS_PROT
  238. /*
  239. * Architectures vary in how they handle caching for addresses
  240. * outside of main memory.
  241. *
  242. */
  243. #ifdef pgprot_noncached
  244. static int uncached_access(struct file *file, phys_addr_t addr)
  245. {
  246. #if defined(CONFIG_IA64)
  247. /*
  248. * On ia64, we ignore O_DSYNC because we cannot tolerate memory
  249. * attribute aliases.
  250. */
  251. return !(efi_mem_attributes(addr) & EFI_MEMORY_WB);
  252. #else
  253. /*
  254. * Accessing memory above the top the kernel knows about or through a
  255. * file pointer
  256. * that was marked O_DSYNC will be done non-cached.
  257. */
  258. if (file->f_flags & O_DSYNC)
  259. return 1;
  260. return addr >= __pa(high_memory);
  261. #endif
  262. }
  263. #endif
  264. static pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
  265. unsigned long size, pgprot_t vma_prot)
  266. {
  267. #ifdef pgprot_noncached
  268. phys_addr_t offset = pfn << PAGE_SHIFT;
  269. if (uncached_access(file, offset))
  270. return pgprot_noncached(vma_prot);
  271. #endif
  272. return vma_prot;
  273. }
  274. #endif
  275. #ifndef CONFIG_MMU
  276. static unsigned long get_unmapped_area_mem(struct file *file,
  277. unsigned long addr,
  278. unsigned long len,
  279. unsigned long pgoff,
  280. unsigned long flags)
  281. {
  282. if (!valid_mmap_phys_addr_range(pgoff, len))
  283. return (unsigned long) -EINVAL;
  284. return pgoff << PAGE_SHIFT;
  285. }
  286. /* permit direct mmap, for read, write or exec */
  287. static unsigned memory_mmap_capabilities(struct file *file)
  288. {
  289. return NOMMU_MAP_DIRECT |
  290. NOMMU_MAP_READ | NOMMU_MAP_WRITE | NOMMU_MAP_EXEC;
  291. }
  292. static unsigned zero_mmap_capabilities(struct file *file)
  293. {
  294. return NOMMU_MAP_COPY;
  295. }
  296. /* can't do an in-place private mapping if there's no MMU */
  297. static inline int private_mapping_ok(struct vm_area_struct *vma)
  298. {
  299. return vma->vm_flags & VM_MAYSHARE;
  300. }
  301. #else
  302. static inline int private_mapping_ok(struct vm_area_struct *vma)
  303. {
  304. return 1;
  305. }
  306. #endif
  307. static const struct vm_operations_struct mmap_mem_ops = {
  308. #ifdef CONFIG_HAVE_IOREMAP_PROT
  309. .access = generic_access_phys
  310. #endif
  311. };
  312. static int mmap_mem(struct file *file, struct vm_area_struct *vma)
  313. {
  314. size_t size = vma->vm_end - vma->vm_start;
  315. phys_addr_t offset = (phys_addr_t)vma->vm_pgoff << PAGE_SHIFT;
  316. /* Does it even fit in phys_addr_t? */
  317. if (offset >> PAGE_SHIFT != vma->vm_pgoff)
  318. return -EINVAL;
  319. /* It's illegal to wrap around the end of the physical address space. */
  320. if (offset + (phys_addr_t)size - 1 < offset)
  321. return -EINVAL;
  322. if (!valid_mmap_phys_addr_range(vma->vm_pgoff, size))
  323. return -EINVAL;
  324. if (!private_mapping_ok(vma))
  325. return -ENOSYS;
  326. if (!range_is_allowed(vma->vm_pgoff, size))
  327. return -EPERM;
  328. if (!phys_mem_access_prot_allowed(file, vma->vm_pgoff, size,
  329. &vma->vm_page_prot))
  330. return -EINVAL;
  331. vma->vm_page_prot = phys_mem_access_prot(file, vma->vm_pgoff,
  332. size,
  333. vma->vm_page_prot);
  334. vma->vm_ops = &mmap_mem_ops;
  335. /* Remap-pfn-range will mark the range VM_IO */
  336. if (remap_pfn_range(vma,
  337. vma->vm_start,
  338. vma->vm_pgoff,
  339. size,
  340. vma->vm_page_prot)) {
  341. return -EAGAIN;
  342. }
  343. return 0;
  344. }
  345. static ssize_t read_port(struct file *file, char __user *buf,
  346. size_t count, loff_t *ppos)
  347. {
  348. unsigned long i = *ppos;
  349. char __user *tmp = buf;
  350. if (!access_ok(buf, count))
  351. return -EFAULT;
  352. while (count-- > 0 && i < 65536) {
  353. if (__put_user(inb(i), tmp) < 0)
  354. return -EFAULT;
  355. i++;
  356. tmp++;
  357. }
  358. *ppos = i;
  359. return tmp-buf;
  360. }
  361. static ssize_t write_port(struct file *file, const char __user *buf,
  362. size_t count, loff_t *ppos)
  363. {
  364. unsigned long i = *ppos;
  365. const char __user *tmp = buf;
  366. if (!access_ok(buf, count))
  367. return -EFAULT;
  368. while (count-- > 0 && i < 65536) {
  369. char c;
  370. if (__get_user(c, tmp)) {
  371. if (tmp > buf)
  372. break;
  373. return -EFAULT;
  374. }
  375. outb(c, i);
  376. i++;
  377. tmp++;
  378. }
  379. *ppos = i;
  380. return tmp-buf;
  381. }
  382. static ssize_t read_null(struct file *file, char __user *buf,
  383. size_t count, loff_t *ppos)
  384. {
  385. return 0;
  386. }
  387. static ssize_t write_null(struct file *file, const char __user *buf,
  388. size_t count, loff_t *ppos)
  389. {
  390. return count;
  391. }
  392. static ssize_t read_iter_null(struct kiocb *iocb, struct iov_iter *to)
  393. {
  394. return 0;
  395. }
  396. static ssize_t write_iter_null(struct kiocb *iocb, struct iov_iter *from)
  397. {
  398. size_t count = iov_iter_count(from);
  399. iov_iter_advance(from, count);
  400. return count;
  401. }
  402. static int pipe_to_null(struct pipe_inode_info *info, struct pipe_buffer *buf,
  403. struct splice_desc *sd)
  404. {
  405. return sd->len;
  406. }
  407. static ssize_t splice_write_null(struct pipe_inode_info *pipe, struct file *out,
  408. loff_t *ppos, size_t len, unsigned int flags)
  409. {
  410. return splice_from_pipe(pipe, out, ppos, len, flags, pipe_to_null);
  411. }
  412. static int uring_cmd_null(struct io_uring_cmd *ioucmd, unsigned int issue_flags)
  413. {
  414. return 0;
  415. }
  416. static ssize_t read_iter_zero(struct kiocb *iocb, struct iov_iter *iter)
  417. {
  418. size_t written = 0;
  419. while (iov_iter_count(iter)) {
  420. size_t chunk = iov_iter_count(iter), n;
  421. if (chunk > PAGE_SIZE)
  422. chunk = PAGE_SIZE; /* Just for latency reasons */
  423. n = iov_iter_zero(chunk, iter);
  424. if (!n && iov_iter_count(iter))
  425. return written ? written : -EFAULT;
  426. written += n;
  427. if (signal_pending(current))
  428. return written ? written : -ERESTARTSYS;
  429. if (!need_resched())
  430. continue;
  431. if (iocb->ki_flags & IOCB_NOWAIT)
  432. return written ? written : -EAGAIN;
  433. cond_resched();
  434. }
  435. return written;
  436. }
  437. static ssize_t read_zero(struct file *file, char __user *buf,
  438. size_t count, loff_t *ppos)
  439. {
  440. size_t cleared = 0;
  441. while (count) {
  442. size_t chunk = min_t(size_t, count, PAGE_SIZE);
  443. size_t left;
  444. left = clear_user(buf + cleared, chunk);
  445. if (unlikely(left)) {
  446. cleared += (chunk - left);
  447. if (!cleared)
  448. return -EFAULT;
  449. break;
  450. }
  451. cleared += chunk;
  452. count -= chunk;
  453. if (signal_pending(current))
  454. break;
  455. cond_resched();
  456. }
  457. return cleared;
  458. }
  459. static int mmap_zero(struct file *file, struct vm_area_struct *vma)
  460. {
  461. #ifndef CONFIG_MMU
  462. return -ENOSYS;
  463. #endif
  464. if (vma->vm_flags & VM_SHARED)
  465. return shmem_zero_setup(vma);
  466. vma_set_anonymous(vma);
  467. return 0;
  468. }
  469. static unsigned long get_unmapped_area_zero(struct file *file,
  470. unsigned long addr, unsigned long len,
  471. unsigned long pgoff, unsigned long flags)
  472. {
  473. #ifdef CONFIG_MMU
  474. if (flags & MAP_SHARED) {
  475. /*
  476. * mmap_zero() will call shmem_zero_setup() to create a file,
  477. * so use shmem's get_unmapped_area in case it can be huge;
  478. * and pass NULL for file as in mmap.c's get_unmapped_area(),
  479. * so as not to confuse shmem with our handle on "/dev/zero".
  480. */
  481. return shmem_get_unmapped_area(NULL, addr, len, pgoff, flags);
  482. }
  483. /* Otherwise flags & MAP_PRIVATE: with no shmem object beneath it */
  484. return current->mm->get_unmapped_area(file, addr, len, pgoff, flags);
  485. #else
  486. return -ENOSYS;
  487. #endif
  488. }
  489. static ssize_t write_full(struct file *file, const char __user *buf,
  490. size_t count, loff_t *ppos)
  491. {
  492. return -ENOSPC;
  493. }
  494. /*
  495. * Special lseek() function for /dev/null and /dev/zero. Most notably, you
  496. * can fopen() both devices with "a" now. This was previously impossible.
  497. * -- SRB.
  498. */
  499. static loff_t null_lseek(struct file *file, loff_t offset, int orig)
  500. {
  501. return file->f_pos = 0;
  502. }
  503. /*
  504. * The memory devices use the full 32/64 bits of the offset, and so we cannot
  505. * check against negative addresses: they are ok. The return value is weird,
  506. * though, in that case (0).
  507. *
  508. * also note that seeking relative to the "end of file" isn't supported:
  509. * it has no meaning, so it returns -EINVAL.
  510. */
  511. static loff_t memory_lseek(struct file *file, loff_t offset, int orig)
  512. {
  513. loff_t ret;
  514. inode_lock(file_inode(file));
  515. switch (orig) {
  516. case SEEK_CUR:
  517. offset += file->f_pos;
  518. fallthrough;
  519. case SEEK_SET:
  520. /* to avoid userland mistaking f_pos=-9 as -EBADF=-9 */
  521. if ((unsigned long long)offset >= -MAX_ERRNO) {
  522. ret = -EOVERFLOW;
  523. break;
  524. }
  525. file->f_pos = offset;
  526. ret = file->f_pos;
  527. force_successful_syscall_return();
  528. break;
  529. default:
  530. ret = -EINVAL;
  531. }
  532. inode_unlock(file_inode(file));
  533. return ret;
  534. }
  535. static int open_port(struct inode *inode, struct file *filp)
  536. {
  537. int rc;
  538. if (!capable(CAP_SYS_RAWIO))
  539. return -EPERM;
  540. rc = security_locked_down(LOCKDOWN_DEV_MEM);
  541. if (rc)
  542. return rc;
  543. if (iminor(inode) != DEVMEM_MINOR)
  544. return 0;
  545. /*
  546. * Use a unified address space to have a single point to manage
  547. * revocations when drivers want to take over a /dev/mem mapped
  548. * range.
  549. */
  550. filp->f_mapping = iomem_get_mapping();
  551. return 0;
  552. }
  553. #define zero_lseek null_lseek
  554. #define full_lseek null_lseek
  555. #define write_zero write_null
  556. #define write_iter_zero write_iter_null
  557. #define open_mem open_port
  558. static const struct file_operations __maybe_unused mem_fops = {
  559. .llseek = memory_lseek,
  560. .read = read_mem,
  561. .write = write_mem,
  562. .mmap = mmap_mem,
  563. .open = open_mem,
  564. #ifndef CONFIG_MMU
  565. .get_unmapped_area = get_unmapped_area_mem,
  566. .mmap_capabilities = memory_mmap_capabilities,
  567. #endif
  568. };
  569. static const struct file_operations null_fops = {
  570. .llseek = null_lseek,
  571. .read = read_null,
  572. .write = write_null,
  573. .read_iter = read_iter_null,
  574. .write_iter = write_iter_null,
  575. .splice_write = splice_write_null,
  576. .uring_cmd = uring_cmd_null,
  577. };
  578. static const struct file_operations __maybe_unused port_fops = {
  579. .llseek = memory_lseek,
  580. .read = read_port,
  581. .write = write_port,
  582. .open = open_port,
  583. };
  584. static const struct file_operations zero_fops = {
  585. .llseek = zero_lseek,
  586. .write = write_zero,
  587. .read_iter = read_iter_zero,
  588. .read = read_zero,
  589. .write_iter = write_iter_zero,
  590. .mmap = mmap_zero,
  591. .get_unmapped_area = get_unmapped_area_zero,
  592. #ifndef CONFIG_MMU
  593. .mmap_capabilities = zero_mmap_capabilities,
  594. #endif
  595. };
  596. static const struct file_operations full_fops = {
  597. .llseek = full_lseek,
  598. .read_iter = read_iter_zero,
  599. .write = write_full,
  600. };
  601. static const struct memdev {
  602. const char *name;
  603. umode_t mode;
  604. const struct file_operations *fops;
  605. fmode_t fmode;
  606. } devlist[] = {
  607. #ifdef CONFIG_DEVMEM
  608. [DEVMEM_MINOR] = { "mem", 0, &mem_fops, FMODE_UNSIGNED_OFFSET },
  609. #endif
  610. [3] = { "null", 0666, &null_fops, FMODE_NOWAIT },
  611. #ifdef CONFIG_DEVPORT
  612. [4] = { "port", 0, &port_fops, 0 },
  613. #endif
  614. [5] = { "zero", 0666, &zero_fops, FMODE_NOWAIT },
  615. [7] = { "full", 0666, &full_fops, 0 },
  616. [8] = { "random", 0666, &random_fops, FMODE_NOWAIT },
  617. [9] = { "urandom", 0666, &urandom_fops, FMODE_NOWAIT },
  618. #ifdef CONFIG_PRINTK
  619. [11] = { "kmsg", 0644, &kmsg_fops, 0 },
  620. #endif
  621. };
  622. static int memory_open(struct inode *inode, struct file *filp)
  623. {
  624. int minor;
  625. const struct memdev *dev;
  626. minor = iminor(inode);
  627. if (minor >= ARRAY_SIZE(devlist))
  628. return -ENXIO;
  629. dev = &devlist[minor];
  630. if (!dev->fops)
  631. return -ENXIO;
  632. filp->f_op = dev->fops;
  633. filp->f_mode |= dev->fmode;
  634. if (dev->fops->open)
  635. return dev->fops->open(inode, filp);
  636. return 0;
  637. }
  638. static const struct file_operations memory_fops = {
  639. .open = memory_open,
  640. .llseek = noop_llseek,
  641. };
  642. static char *mem_devnode(struct device *dev, umode_t *mode)
  643. {
  644. if (mode && devlist[MINOR(dev->devt)].mode)
  645. *mode = devlist[MINOR(dev->devt)].mode;
  646. return NULL;
  647. }
  648. static struct class *mem_class;
  649. static int __init chr_dev_init(void)
  650. {
  651. int minor;
  652. if (register_chrdev(MEM_MAJOR, "mem", &memory_fops))
  653. printk("unable to get major %d for memory devs\n", MEM_MAJOR);
  654. mem_class = class_create(THIS_MODULE, "mem");
  655. if (IS_ERR(mem_class))
  656. return PTR_ERR(mem_class);
  657. mem_class->devnode = mem_devnode;
  658. for (minor = 1; minor < ARRAY_SIZE(devlist); minor++) {
  659. if (!devlist[minor].name)
  660. continue;
  661. /*
  662. * Create /dev/port?
  663. */
  664. if ((minor == DEVPORT_MINOR) && !arch_has_dev_port())
  665. continue;
  666. device_create(mem_class, NULL, MKDEV(MEM_MAJOR, minor),
  667. NULL, devlist[minor].name);
  668. }
  669. return tty_init();
  670. }
  671. fs_initcall(chr_dev_init);