xref: /illumos-gate/usr/src/uts/common/c2/adr.c (revision 581cede61ac9c14d8d4ea452562a567189eead78)
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  * Adr memory based encoding
31  */
32 
33 #include <sys/feature_tests.h>
34 
35 #pragma weak adr_ushort = adr_short
36 #pragma weak adr_uint32 = adr_int32
37 #pragma weak adr_uint64 = adr_int64
38 #pragma weak adr_getushort = adr_getshort
39 #pragma weak adr_getuint32 = adr_getint32
40 #pragma weak adr_getuint64 = adr_getint64
41 
42 #include <sys/types.h>
43 #include <sys/t_lock.h>
44 #include <sys/systm.h>
45 #include <sys/mutex.h>
46 #include <sys/thread.h>
47 #include <c2/audit.h>
48 #include <c2/audit_kernel.h>
49 #include <c2/audit_record.h>
50 
51 void
52 adr_start(adr_t *adr, char *p)
53 {
54 	adr->adr_stream = p;
55 	adr->adr_now = p;
56 }
57 
58 int
59 adr_count(adr_t *adr)
60 {
61 	return ((int)((uintptr_t)adr->adr_now - (uintptr_t)adr->adr_stream));
62 }
63 
64 
65 /*
66  * adr_char - pull out characters
67  */
68 void
69 adr_char(adr_t *adr, char *cp, int count)
70 {
71 	while (count-- > 0)
72 		*adr->adr_now++ = *cp++;
73 }
74 
75 /*
76  * adr_short - pull out shorts
77  */
78 void
79 adr_short(adr_t *adr, short *sp, int count)
80 {
81 
82 	for (; count-- > 0; sp++) {
83 		*adr->adr_now++ = (char)((*sp >> (int)8) & 0x00ff);
84 		*adr->adr_now++ = (char)(*sp & 0x00ff);
85 	}
86 }
87 
88 /*
89  * adr_int32 - pull out int32
90  */
91 void
92 adr_int32(adr_t *adr, int32_t *lp, int count)
93 {
94 	int i;		/* index for counting */
95 	int32_t l;		/* value for shifting */
96 
97 	for (; count-- > 0; lp++) {
98 		for (i = 0, l = *lp; i < 4; i++) {
99 			*adr->adr_now++ = (char)((l & (int32_t)0xff000000) >>
100 				(int)24);
101 			l <<= (int)8;
102 		}
103 	}
104 }
105 
106 /*
107  * adr_int64 - pull out int64
108  */
109 void
110 adr_int64(adr_t *adr, int64_t *lp, int count)
111 {
112 	int i;		/* index for counting */
113 	int64_t l;	/* value for shifting */
114 
115 	for (; count-- > 0; lp++) {
116 		for (i = 0, l = *lp; i < 8; i++) {
117 		    *adr->adr_now++ =
118 			(char)((l & (int64_t)0xff00000000000000) >> (int)56);
119 			l <<= (int)8;
120 		}
121 	}
122 }
123 
124 
125 char *
126 adr_getchar(adr_t *adr, char *cp)
127 {
128 	char	*old;
129 
130 	old = adr->adr_now;
131 	*cp = *adr->adr_now++;
132 	return (old);
133 }
134 
135 char *
136 adr_getshort(adr_t *adr, short	*sp)
137 {
138 	char	*old;
139 
140 	old = adr->adr_now;
141 	*sp = *adr->adr_now++;
142 	*sp >>= (int)8;
143 	*sp = *adr->adr_now++;
144 	*sp >>= (int)8;
145 	return (old);
146 }
147 
148 char *
149 adr_getint32(adr_t *adr, int32_t *lp)
150 {
151 	char	*old;
152 	int	i;
153 
154 	old = adr->adr_now;
155 	for (i = 0; i < 4; i++) {
156 		*lp <<= 8;
157 		*lp += ((int32_t)*adr->adr_now++) & 0x000000ff;
158 
159 	}
160 	return (old);
161 }
162 
163 char *
164 adr_getint64(adr_t *adr, int64_t *lp)
165 {
166 	char	*old;
167 	int	i;
168 
169 	old = adr->adr_now;
170 	for (i = 0; i < 8; i++) {
171 		*lp <<= 8;
172 		*lp += ((int64_t)*adr->adr_now++) & 0x00000000000000ff;
173 	}
174 	return (old);
175 }
176