]> git.saurik.com Git - apple/xnu.git/blob - osfmk/i386/loose_ends.c
xnu-344.tar.gz
[apple/xnu.git] / osfmk / i386 / loose_ends.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 /*
23 * @OSF_COPYRIGHT@
24 */
25 /*
26 * Mach Operating System
27 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
28 * All Rights Reserved.
29 *
30 * Permission to use, copy, modify and distribute this software and its
31 * documentation is hereby granted, provided that both the copyright
32 * notice and this permission notice appear in all copies of the
33 * software, derivative works or modified versions, and any portions
34 * thereof, and that both notices appear in supporting documentation.
35 *
36 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
37 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
38 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
39 *
40 * Carnegie Mellon requests users of this software to return to
41 *
42 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
43 * School of Computer Science
44 * Carnegie Mellon University
45 * Pittsburgh PA 15213-3890
46 *
47 * any improvements or extensions that they make and grant Carnegie Mellon
48 * the rights to redistribute these changes.
49 */
50 /*
51 */
52 #include <mach_assert.h>
53
54 #include <string.h>
55 #include <mach/boolean.h>
56 #include <mach/i386/vm_types.h>
57 #include <mach/i386/vm_param.h>
58 #include <kern/kern_types.h>
59 #include <kern/misc_protos.h>
60 #include <i386/misc_protos.h>
61
62 /*
63 * Should be rewritten in asm anyway.
64 */
65
66
67
68 /*
69 * Copies data from a physical page to a virtual page. This is used to
70 * move data from the kernel to user state.
71 *
72 */
73
74 kern_return_t
75 copyp2v(char *from, char *to, unsigned int size) {
76
77 return(copyout(phystokv(from), to, size));
78 }
79
80 /*
81 * bcopy_phys - like bcopy but copies from/to physical addresses.
82 * this is trivial since all phys mem is mapped into
83 * kernel virtual space
84 */
85
86 void
87 bcopy_phys(const char *from, char *to, vm_size_t bytes)
88 {
89 bcopy((char *)phystokv(from), (char *)phystokv(to), bytes);
90 }
91
92
93 /*
94 * ovbcopy - like bcopy, but recognizes overlapping ranges and handles
95 * them correctly.
96 */
97
98 void
99 ovbcopy(
100 const char *from,
101 char *to,
102 vm_size_t bytes) /* num bytes to copy */
103 {
104 /* Assume that bcopy copies left-to-right (low addr first). */
105 if (from + bytes <= to || to + bytes <= from || to == from)
106 bcopy_no_overwrite(from, to, bytes); /* non-overlapping or no-op*/
107 else if (from > to)
108 bcopy_no_overwrite(from, to, bytes); /* overlapping but OK */
109 else {
110 /* to > from: overlapping, and must copy right-to-left. */
111 from += bytes - 1;
112 to += bytes - 1;
113 while (bytes-- > 0)
114 *to-- = *from--;
115 }
116 }
117
118 void
119 bcopy(
120 const char *from,
121 char *to,
122 vm_size_t bytes) /* num bytes to copy */
123 {
124 ovbcopy(from, to, bytes);
125 }
126
127 int bcmp(
128 const char *a,
129 const char *b,
130 vm_size_t len)
131 {
132 if (len == 0)
133 return 0;
134
135 do
136 if (*a++ != *b++)
137 break;
138 while (--len);
139
140 return len;
141 }
142
143 int
144 memcmp(s1, s2, n)
145 register char *s1, *s2;
146 register n;
147 {
148 while (--n >= 0)
149 if (*s1++ != *s2++)
150 return (*--s1 - *--s2);
151 return (0);
152 }
153
154 /*
155 * Abstract:
156 * strlen returns the number of characters in "string" preceeding
157 * the terminating null character.
158 */
159
160 size_t
161 strlen(
162 register const char *string)
163 {
164 register const char *ret = string;
165
166 while (*string++ != '\0')
167 continue;
168 return string - 1 - ret;
169 }
170
171 #include <libkern/OSAtomic.h>
172
173 uint32_t
174 hw_atomic_add(
175 uint32_t *dest,
176 uint32_t delt)
177 {
178 uint32_t oldValue;
179 uint32_t newValue;
180
181 do {
182 oldValue = *dest;
183 newValue = (oldValue + delt);
184 } while (!OSCompareAndSwap((UInt32)oldValue,
185 (UInt32)newValue, (UInt32 *)dest));
186
187 return newValue;
188 }
189
190 uint32_t
191 hw_atomic_sub(
192 uint32_t *dest,
193 uint32_t delt)
194 {
195 uint32_t oldValue;
196 uint32_t newValue;
197
198 do {
199 oldValue = *dest;
200 newValue = (oldValue - delt);
201 } while (!OSCompareAndSwap((UInt32)oldValue,
202 (UInt32)newValue, (UInt32 *)dest));
203
204 return newValue;
205 }
206
207 uint32_t
208 hw_atomic_or(
209 uint32_t *dest,
210 uint32_t mask)
211 {
212 uint32_t oldValue;
213 uint32_t newValue;
214
215 do {
216 oldValue = *dest;
217 newValue = (oldValue | mask);
218 } while (!OSCompareAndSwap((UInt32)oldValue,
219 (UInt32)newValue, (UInt32 *)dest));
220
221 return newValue;
222 }
223
224 uint32_t
225 hw_atomic_and(
226 uint32_t *dest,
227 uint32_t mask)
228 {
229 uint32_t oldValue;
230 uint32_t newValue;
231
232 do {
233 oldValue = *dest;
234 newValue = (oldValue & mask);
235 } while (!OSCompareAndSwap((UInt32)oldValue,
236 (UInt32)newValue, (UInt32 *)dest));
237
238 return newValue;
239 }
240
241 uint32_t
242 hw_compare_and_store(
243 uint32_t oldval,
244 uint32_t newval,
245 uint32_t *dest)
246 {
247 return OSCompareAndSwap((UInt32)oldval, (UInt32)newval, (UInt32 *)dest);
248 }
249
250 #if MACH_ASSERT
251
252 /*
253 * Machine-dependent routine to fill in an array with up to callstack_max
254 * levels of return pc information.
255 */
256 void machine_callstack(
257 natural_t *buf,
258 vm_size_t callstack_max)
259 {
260 }
261
262 #endif /* MACH_ASSERT */