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