mem.c 16 KB

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