]> git.saurik.com Git - apple/xnu.git/blob - osfmk/i386/loose_ends.c
xnu-344.21.74.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 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 /*
26 * @OSF_COPYRIGHT@
27 */
28 /*
29 * Mach Operating System
30 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
31 * All Rights Reserved.
32 *
33 * Permission to use, copy, modify and distribute this software and its
34 * documentation is hereby granted, provided that both the copyright
35 * notice and this permission notice appear in all copies of the
36 * software, derivative works or modified versions, and any portions
37 * thereof, and that both notices appear in supporting documentation.
38 *
39 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
40 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
41 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
42 *
43 * Carnegie Mellon requests users of this software to return to
44 *
45 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
46 * School of Computer Science
47 * Carnegie Mellon University
48 * Pittsburgh PA 15213-3890
49 *
50 * any improvements or extensions that they make and grant Carnegie Mellon
51 * the rights to redistribute these changes.
52 */
53 /*
54 */
55 #include <mach_assert.h>
56
57 #include <string.h>
58 #include <mach/boolean.h>
59 #include <mach/i386/vm_types.h>
60 #include <mach/i386/vm_param.h>
61 #include <kern/kern_types.h>
62 #include <kern/misc_protos.h>
63 #include <i386/misc_protos.h>
64
65 /*
66 * Should be rewritten in asm anyway.
67 */
68
69
70 /*
71 * copy 'size' bytes from physical to physical address
72 * the caller must validate the physical ranges
73 *
74 * if flush_action == 0, no cache flush necessary
75 * if flush_action == 1, flush the source
76 * if flush_action == 2, flush the dest
77 * if flush_action == 3, flush both source and dest
78 */
79
80 kern_return_t copyp2p(vm_offset_t source, vm_offset_t dest, unsigned int size, unsigned int flush_action) {
81
82 switch(flush_action) {
83 case 1:
84 flush_dcache(source, size, 1);
85 break;
86 case 2:
87 flush_dcache(dest, size, 1);
88 break;
89 case 3:
90 flush_dcache(source, size, 1);
91 flush_dcache(dest, size, 1);
92 break;
93
94 }
95 bcopy_phys((char *)source, (char *)dest, size); /* Do a physical copy */
96
97 switch(flush_action) {
98 case 1:
99 flush_dcache(source, size, 1);
100 break;
101 case 2:
102 flush_dcache(dest, size, 1);
103 break;
104 case 3:
105 flush_dcache(source, size, 1);
106 flush_dcache(dest, size, 1);
107 break;
108
109 }
110 }
111
112
113
114 /*
115 * Copies data from a physical page to a virtual page. This is used to
116 * move data from the kernel to user state.
117 *
118 */
119
120 kern_return_t
121 copyp2v(char *from, char *to, unsigned int size) {
122
123 return(copyout(phystokv(from), to, size));
124 }
125
126 /*
127 * bcopy_phys - like bcopy but copies from/to physical addresses.
128 * this is trivial since all phys mem is mapped into
129 * kernel virtual space
130 */
131
132 void
133 bcopy_phys(const char *from, char *to, vm_size_t bytes)
134 {
135 bcopy((char *)phystokv(from), (char *)phystokv(to), bytes);
136 }
137
138
139 /*
140 * ovbcopy - like bcopy, but recognizes overlapping ranges and handles
141 * them correctly.
142 */
143
144 void
145 ovbcopy(
146 const char *from,
147 char *to,
148 vm_size_t bytes) /* num bytes to copy */
149 {
150 /* Assume that bcopy copies left-to-right (low addr first). */
151 if (from + bytes <= to || to + bytes <= from || to == from)
152 bcopy_no_overwrite(from, to, bytes); /* non-overlapping or no-op*/
153 else if (from > to)
154 bcopy_no_overwrite(from, to, bytes); /* overlapping but OK */
155 else {
156 /* to > from: overlapping, and must copy right-to-left. */
157 from += bytes - 1;
158 to += bytes - 1;
159 while (bytes-- > 0)
160 *to-- = *from--;
161 }
162 }
163
164 void
165 bcopy(
166 const char *from,
167 char *to,
168 vm_size_t bytes) /* num bytes to copy */
169 {
170 ovbcopy(from, to, bytes);
171 }
172
173 int bcmp(
174 const char *a,
175 const char *b,
176 vm_size_t len)
177 {
178 if (len == 0)
179 return 0;
180
181 do
182 if (*a++ != *b++)
183 break;
184 while (--len);
185
186 return len;
187 }
188
189 int
190 memcmp(s1, s2, n)
191 register char *s1, *s2;
192 register n;
193 {
194 while (--n >= 0)
195 if (*s1++ != *s2++)
196 return (*--s1 - *--s2);
197 return (0);
198 }
199
200 /*
201 * Abstract:
202 * strlen returns the number of characters in "string" preceeding
203 * the terminating null character.
204 */
205
206 size_t
207 strlen(
208 register const char *string)
209 {
210 register const char *ret = string;
211
212 while (*string++ != '\0')
213 continue;
214 return string - 1 - ret;
215 }
216
217 #include <libkern/OSAtomic.h>
218
219 uint32_t
220 hw_atomic_add(
221 uint32_t *dest,
222 uint32_t delt)
223 {
224 uint32_t oldValue;
225 uint32_t newValue;
226
227 do {
228 oldValue = *dest;
229 newValue = (oldValue + delt);
230 } while (!OSCompareAndSwap((UInt32)oldValue,
231 (UInt32)newValue, (UInt32 *)dest));
232
233 return newValue;
234 }
235
236 uint32_t
237 hw_atomic_sub(
238 uint32_t *dest,
239 uint32_t delt)
240 {
241 uint32_t oldValue;
242 uint32_t newValue;
243
244 do {
245 oldValue = *dest;
246 newValue = (oldValue - delt);
247 } while (!OSCompareAndSwap((UInt32)oldValue,
248 (UInt32)newValue, (UInt32 *)dest));
249
250 return newValue;
251 }
252
253 uint32_t
254 hw_atomic_or(
255 uint32_t *dest,
256 uint32_t mask)
257 {
258 uint32_t oldValue;
259 uint32_t newValue;
260
261 do {
262 oldValue = *dest;
263 newValue = (oldValue | mask);
264 } while (!OSCompareAndSwap((UInt32)oldValue,
265 (UInt32)newValue, (UInt32 *)dest));
266
267 return newValue;
268 }
269
270 uint32_t
271 hw_atomic_and(
272 uint32_t *dest,
273 uint32_t mask)
274 {
275 uint32_t oldValue;
276 uint32_t newValue;
277
278 do {
279 oldValue = *dest;
280 newValue = (oldValue & mask);
281 } while (!OSCompareAndSwap((UInt32)oldValue,
282 (UInt32)newValue, (UInt32 *)dest));
283
284 return newValue;
285 }
286
287 uint32_t
288 hw_compare_and_store(
289 uint32_t oldval,
290 uint32_t newval,
291 uint32_t *dest)
292 {
293 return OSCompareAndSwap((UInt32)oldval, (UInt32)newval, (UInt32 *)dest);
294 }
295
296 #if MACH_ASSERT
297
298 /*
299 * Machine-dependent routine to fill in an array with up to callstack_max
300 * levels of return pc information.
301 */
302 void machine_callstack(
303 natural_t *buf,
304 vm_size_t callstack_max)
305 {
306 }
307
308 #endif /* MACH_ASSERT */
309
310
311
312
313 /* FIXMEx86 */
314 /* These all need to be implemented somewhere */
315
316 void fillPage(ppnum_t pa, unsigned int fill)
317 {
318 }
319
320 uint64_t max_mem;
321 uint64_t sane_size;
322
323 #include <vm/pmap.h>
324
325 ppnum_t pmap_find_phys(pmap_t pmap, addr64_t va)
326 {
327 return 0;
328 }
329
330 kern_return_t copypv(addr64_t source, addr64_t sink, unsigned int size, int which)
331 {
332 return KERN_SUCCESS;
333 }
334
335 void mapping_set_mod(ppnum_t pa)
336 {
337 }
338
339 void flush_dcache64(addr64_t addr, unsigned count, int phys)
340 {
341 }
342
343 void invalidate_icache64(addr64_t addr, unsigned cnt, int phys)
344 {
345 }