mem.c 17 KB

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