xref: /titanic_51/usr/src/uts/intel/ia32/os/copy_subr.c (revision b3697b90e692e3e5d859fb77d285d4c056d99eda)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * Miscellaneous C routines for copying data around without
31  * descending into assembler.  Compilers are pretty good at
32  * scheduling instructions, and humans are pretty hopeless at
33  * writing correct assembler.
34  */
35 
36 #include <sys/types.h>
37 #include <sys/systm.h>
38 #include <sys/errno.h>
39 #include <sys/param.h>
40 
41 /*
42  * copyinstr_noerr and copyoutstr_noerr can be implemented completely
43  * in C on machines with shared user and kernel context.
44  */
45 static int
46 copystr_nofault(const char *src, char *dst, size_t maxlength,
47     size_t *lencopied)
48 {
49 	int error = 0;
50 	size_t leftover;
51 
52 	if ((leftover = maxlength) == 0)
53 		error = ENAMETOOLONG;
54 	else
55 		do {
56 			leftover--;
57 			if ((*dst++ = *src++) == '\0')
58 				break;
59 			if (leftover == 0) {
60 				error = ENAMETOOLONG;
61 				break;
62 			}
63 		/*CONSTCOND*/
64 		} while (1);
65 
66 	if (lencopied)
67 		*lencopied = maxlength - leftover;
68 	return (error);
69 }
70 
71 
72 int
73 copyinstr_noerr(const char *uaddr, char *kaddr, size_t maxlength,
74     size_t *lencopied)
75 {
76 	char *ua = (char *)uaddr;
77 
78 	ASSERT((uintptr_t)kaddr > kernelbase);
79 
80 	if ((uintptr_t)ua > kernelbase) {
81 		/*
82 		 * force fault at kernelbase
83 		 */
84 		ua = (char *)kernelbase;
85 	}
86 	return (copystr_nofault(ua, kaddr, maxlength, lencopied));
87 }
88 
89 int
90 copyoutstr_noerr(const char *kaddr, char *uaddr, size_t maxlength,
91     size_t *lencopied)
92 {
93 	char *ua = (char *)uaddr;
94 
95 	ASSERT((uintptr_t)kaddr > kernelbase);
96 
97 	if ((uintptr_t)ua > kernelbase) {
98 		/*
99 		 * force fault at kernelbase
100 		 */
101 		ua = (char *)kernelbase;
102 	}
103 	return (copystr_nofault(kaddr, ua, maxlength, lencopied));
104 }
105