xref: /titanic_50/usr/src/uts/common/c2/adr.c (revision 23a1ccea6aac035f084a7a4cdc968687d1b02daf)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright (c) 1992, 2010, Oracle and/or its affiliates. All rights reserved.
23  */
24 
25 /*
26  * Adr memory based encoding
27  */
28 
29 #include <sys/feature_tests.h>
30 
31 #pragma weak adr_ushort = adr_short
32 #pragma weak adr_uint32 = adr_int32
33 #pragma weak adr_uint64 = adr_int64
34 #pragma weak adr_getushort = adr_getshort
35 #pragma weak adr_getuint32 = adr_getint32
36 #pragma weak adr_getuint64 = adr_getint64
37 
38 #include <sys/types.h>
39 #include <sys/t_lock.h>
40 #include <sys/systm.h>
41 #include <sys/mutex.h>
42 #include <sys/thread.h>
43 #include <c2/audit.h>
44 #include <c2/audit_kernel.h>
45 #include <c2/audit_record.h>
46 
47 void
48 adr_start(adr_t *adr, char *p)
49 {
50 	adr->adr_stream = p;
51 	adr->adr_now = p;
52 }
53 
54 int
55 adr_count(adr_t *adr)
56 {
57 	return ((int)((uintptr_t)adr->adr_now - (uintptr_t)adr->adr_stream));
58 }
59 
60 
61 /*
62  * adr_char - pull out characters
63  */
64 void
65 adr_char(adr_t *adr, char *cp, int count)
66 {
67 	while (count-- > 0)
68 		*adr->adr_now++ = *cp++;
69 }
70 
71 /*
72  * adr_short - pull out shorts
73  */
74 void
75 adr_short(adr_t *adr, short *sp, int count)
76 {
77 
78 	for (; count-- > 0; sp++) {
79 		*adr->adr_now++ = (char)((*sp >> (int)8) & 0x00ff);
80 		*adr->adr_now++ = (char)(*sp & 0x00ff);
81 	}
82 }
83 
84 /*
85  * adr_int32 - pull out int32
86  */
87 void
88 adr_int32(adr_t *adr, int32_t *lp, int count)
89 {
90 	int i;		/* index for counting */
91 	int32_t l;		/* value for shifting */
92 
93 	for (; count-- > 0; lp++) {
94 		for (i = 0, l = *lp; i < 4; i++) {
95 			*adr->adr_now++ = (char)((l & (int32_t)0xff000000) >>
96 			    (int)24);
97 			l <<= (int)8;
98 		}
99 	}
100 }
101 
102 /*
103  * adr_int64 - pull out int64
104  */
105 void
106 adr_int64(adr_t *adr, int64_t *lp, int count)
107 {
108 	int i;		/* index for counting */
109 	int64_t l;	/* value for shifting */
110 
111 	for (; count-- > 0; lp++) {
112 		for (i = 0, l = *lp; i < 8; i++) {
113 			*adr->adr_now++ =
114 			    (char)((l & (int64_t)0xff00000000000000) >>
115 			    (int)56);
116 			l <<= (int)8;
117 		}
118 	}
119 }
120 
121 
122 char *
123 adr_getchar(adr_t *adr, char *cp)
124 {
125 	char	*old;
126 
127 	old = adr->adr_now;
128 	*cp = *adr->adr_now++;
129 	return (old);
130 }
131 
132 char *
133 adr_getshort(adr_t *adr, short	*sp)
134 {
135 	char	*old;
136 
137 	old = adr->adr_now;
138 	*sp = *adr->adr_now++;
139 	*sp >>= (int)8;
140 	*sp = *adr->adr_now++;
141 	*sp >>= (int)8;
142 	return (old);
143 }
144 
145 char *
146 adr_getint32(adr_t *adr, int32_t *lp)
147 {
148 	char	*old;
149 	int	i;
150 
151 	old = adr->adr_now;
152 	for (i = 0; i < 4; i++) {
153 		*lp <<= 8;
154 		*lp += ((int32_t)*adr->adr_now++) & 0x000000ff;
155 
156 	}
157 	return (old);
158 }
159 
160 char *
161 adr_getint64(adr_t *adr, int64_t *lp)
162 {
163 	char	*old;
164 	int	i;
165 
166 	old = adr->adr_now;
167 	for (i = 0; i < 8; i++) {
168 		*lp <<= 8;
169 		*lp += ((int64_t)*adr->adr_now++) & 0x00000000000000ff;
170 	}
171 	return (old);
172 }
173