1/*
2 * Coyright (c) 2005-2006 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29#include <sys/cdefs.h>
30#include <sys/types.h>
31#include <sys/munge.h>
32#include <stdint.h>
33
34/*
35 * Refer to comments in bsd/sys/munge.h
36 */
37
38static inline __attribute__((always_inline)) void
39munge_32_to_64_unsigned(volatile uint64_t *dest, volatile uint32_t *src, int count);
40
41void
42munge_w(void *args)
43{
44 munge_32_to_64_unsigned(dest: args, src: args, count: 1);
45}
46
47void
48munge_ww(void *args)
49{
50 munge_32_to_64_unsigned(dest: args, src: args, count: 2);
51}
52
53void
54munge_www(void *args)
55{
56 munge_32_to_64_unsigned(dest: args, src: args, count: 3);
57}
58
59void
60munge_wwww(void *args)
61{
62 munge_32_to_64_unsigned(dest: args, src: args, count: 4);
63}
64
65void
66munge_wwwww(void *args)
67{
68 munge_32_to_64_unsigned(dest: args, src: args, count: 5);
69}
70
71void
72munge_wwwwww(void *args)
73{
74 munge_32_to_64_unsigned(dest: args, src: args, count: 6);
75}
76
77void
78munge_wwwwwww(void *args)
79{
80 munge_32_to_64_unsigned(dest: args, src: args, count: 7);
81}
82
83void
84munge_wwwwwwww(void *args)
85{
86 munge_32_to_64_unsigned(dest: args, src: args, count: 8);
87}
88
89void
90munge_wl(void *args)
91{
92 volatile uint64_t *out_args = (volatile uint64_t*)args;
93 volatile uint32_t *in_args = (volatile uint32_t*)args;
94
95 out_args[1] = *(volatile uint64_t*)&in_args[1];
96 out_args[0] = in_args[0];
97}
98
99void
100munge_wwl(void *args)
101{
102 volatile uint64_t *out_args = (volatile uint64_t*)args;
103 volatile uint32_t *in_args = (volatile uint32_t*)args;
104
105 out_args[2] = *(volatile uint64_t*)&in_args[2];
106 out_args[1] = in_args[1];
107 out_args[0] = in_args[0];
108}
109
110void
111munge_wwlw(void *args)
112{
113 volatile uint64_t *out_args = (volatile uint64_t*)args;
114 volatile uint32_t *in_args = (volatile uint32_t*)args;
115
116 out_args[3] = in_args[4];
117 out_args[2] = *(volatile uint64_t*)&in_args[2];
118 out_args[1] = in_args[1];
119 out_args[0] = in_args[0];
120}
121void
122munge_wwlll(void *args)
123{
124 volatile uint64_t *out_args = (volatile uint64_t*)args;
125 volatile uint32_t *in_args = (volatile uint32_t*)args;
126
127 out_args[4] = *(volatile uint64_t*)&in_args[6];
128 out_args[3] = *(volatile uint64_t*)&in_args[4];
129 out_args[2] = *(volatile uint64_t*)&in_args[2];
130 out_args[1] = in_args[1];
131 out_args[0] = in_args[0];
132}
133
134void
135munge_wwlllll(void *args)
136{
137 volatile uint64_t *out_args = (volatile uint64_t*)args;
138 volatile uint32_t *in_args = (volatile uint32_t*)args;
139
140 out_args[6] = *(volatile uint64_t*)&in_args[10];
141 out_args[5] = *(volatile uint64_t*)&in_args[8];
142 out_args[4] = *(volatile uint64_t*)&in_args[6];
143 out_args[3] = *(volatile uint64_t*)&in_args[4];
144 out_args[2] = *(volatile uint64_t*)&in_args[2];
145 out_args[1] = in_args[1];
146 out_args[0] = in_args[0];
147}
148
149void
150munge_wwllww(void *args)
151{
152 volatile uint64_t *out_args = (volatile uint64_t*)args;
153 volatile uint32_t *in_args = (volatile uint32_t*)args;
154
155 out_args[5] = in_args[7];
156 out_args[4] = in_args[6];
157 out_args[3] = *(volatile uint64_t*)&in_args[4];
158 out_args[2] = *(volatile uint64_t*)&in_args[2];
159 out_args[1] = in_args[1];
160 out_args[0] = in_args[0];
161}
162
163void
164munge_wlw(void *args)
165{
166 volatile uint64_t *out_args = (volatile uint64_t*)args;
167 volatile uint32_t *in_args = (volatile uint32_t*)args;
168
169 out_args[2] = in_args[3];
170 out_args[1] = *(volatile uint64_t*)&in_args[1];
171 out_args[0] = in_args[0];
172}
173
174void
175munge_wlww(void *args)
176{
177 volatile uint64_t *out_args = (volatile uint64_t*)args;
178 volatile uint32_t *in_args = (volatile uint32_t*)args;
179
180 out_args[3] = in_args[4];
181 out_args[2] = in_args[3];
182 out_args[1] = *(volatile uint64_t*)&in_args[1];
183 out_args[0] = in_args[0];
184}
185
186void
187munge_wlwwwll(void *args)
188{
189 volatile uint64_t *out_args = (volatile uint64_t*)args;
190 volatile uint32_t *in_args = (volatile uint32_t*)args;
191
192 out_args[6] = *(volatile uint64_t*)&in_args[8];
193 out_args[5] = *(volatile uint64_t*)&in_args[6];
194 out_args[4] = in_args[5];
195 out_args[3] = in_args[4];
196 out_args[2] = in_args[3];
197 out_args[1] = *(volatile uint64_t*)&in_args[1];
198 out_args[0] = in_args[0];
199}
200
201void
202munge_wlwwwllw(void *args)
203{
204 volatile uint64_t *out_args = (volatile uint64_t*)args;
205 volatile uint32_t *in_args = (volatile uint32_t*)args;
206
207 out_args[7] = in_args[10];
208 munge_wlwwwll(args);
209}
210
211void
212munge_wlwwlwlw(void *args)
213{
214 volatile uint64_t *out_args = (volatile uint64_t*)args;
215 volatile uint32_t *in_args = (volatile uint32_t*)args;
216
217 out_args[7] = in_args[10];
218 out_args[6] = *(volatile uint64_t*)&in_args[8];
219 out_args[5] = in_args[7];
220 out_args[4] = *(volatile uint64_t*)&in_args[5];
221 out_args[3] = in_args[4];
222 out_args[2] = in_args[3];
223 out_args[1] = *(volatile uint64_t*)&in_args[1];
224 out_args[0] = in_args[0];
225}
226
227void
228munge_wll(void *args)
229{
230 volatile uint64_t *out_args = (volatile uint64_t*)args;
231 volatile uint32_t *in_args = (volatile uint32_t*)args;
232
233 out_args[2] = *(volatile uint64_t*)&in_args[3];
234 out_args[1] = *(volatile uint64_t*)&in_args[1];
235 out_args[0] = in_args[0];
236}
237
238void
239munge_wlll(void *args)
240{
241 volatile uint64_t *out_args = (volatile uint64_t*)args;
242 volatile uint32_t *in_args = (volatile uint32_t*)args;
243
244 out_args[3] = *(volatile uint64_t*)&in_args[5];
245 out_args[2] = *(volatile uint64_t*)&in_args[3];
246 out_args[1] = *(volatile uint64_t*)&in_args[1];
247 out_args[0] = in_args[0];
248}
249
250void
251munge_wlllww(void *args)
252{
253 volatile uint64_t *out_args = (volatile uint64_t*)args;
254 volatile uint32_t *in_args = (volatile uint32_t*)args;
255
256 out_args[5] = in_args[8];
257 out_args[4] = in_args[7];
258 out_args[3] = *(volatile uint64_t*)&in_args[5];
259 out_args[2] = *(volatile uint64_t*)&in_args[3];
260 out_args[1] = *(volatile uint64_t*)&in_args[1];
261 out_args[0] = in_args[0];
262}
263
264void
265munge_wllll(void *args)
266{
267 volatile uint64_t *out_args = (volatile uint64_t*)args;
268 volatile uint32_t *in_args = (volatile uint32_t*)args;
269
270 out_args[4] = *(volatile uint64_t*)&in_args[7];
271 out_args[3] = *(volatile uint64_t*)&in_args[5];
272 out_args[2] = *(volatile uint64_t*)&in_args[3];
273 out_args[1] = *(volatile uint64_t*)&in_args[1];
274 out_args[0] = in_args[0];
275}
276
277void
278munge_wllww(void *args)
279{
280 volatile uint64_t *out_args = (volatile uint64_t*)args;
281 volatile uint32_t *in_args = (volatile uint32_t*)args;
282
283 out_args[4] = in_args[6];
284 out_args[3] = in_args[5];
285 out_args[2] = *(volatile uint64_t*)&in_args[3];
286 out_args[1] = *(volatile uint64_t*)&in_args[1];
287 out_args[0] = in_args[0];
288}
289
290void
291munge_wllwwll(void *args)
292{
293 volatile uint64_t *out_args = (volatile uint64_t*)args;
294 volatile uint32_t *in_args = (volatile uint32_t*)args;
295
296 out_args[6] = *(volatile uint64_t*)&in_args[9];
297 out_args[5] = *(volatile uint64_t*)&in_args[7];
298 out_args[4] = in_args[6];
299 out_args[3] = in_args[5];
300 out_args[2] = *(volatile uint64_t*)&in_args[3];
301 out_args[1] = *(volatile uint64_t*)&in_args[1];
302 out_args[0] = in_args[0];
303}
304
305void
306munge_wwwlw(void *args)
307{
308 volatile uint64_t *out_args = (volatile uint64_t*)args;
309 volatile uint32_t *in_args = (volatile uint32_t*)args;
310
311 out_args[4] = in_args[5];
312 out_args[3] = *(volatile uint64_t*)&in_args[3];
313 out_args[2] = in_args[2];
314 out_args[1] = in_args[1];
315 out_args[0] = in_args[0];
316}
317
318void
319munge_wwwlww(void *args)
320{
321 volatile uint64_t *out_args = (volatile uint64_t*)args;
322 volatile uint32_t *in_args = (volatile uint32_t*)args;
323
324 out_args[5] = in_args[6];
325 out_args[4] = in_args[5];
326 out_args[3] = *(volatile uint64_t*)&in_args[3];
327 out_args[2] = in_args[2];
328 out_args[1] = in_args[1];
329 out_args[0] = in_args[0];
330}
331
332void
333munge_wwwlwww(void *args)
334{
335 volatile uint64_t *out_args = (volatile uint64_t*)args;
336 volatile uint32_t *in_args = (volatile uint32_t*)args;
337
338 out_args[6] = in_args[7];
339 out_args[5] = in_args[6];
340 out_args[4] = in_args[5];
341 out_args[3] = *(volatile uint64_t*)&in_args[3];
342 out_args[2] = in_args[2];
343 out_args[1] = in_args[1];
344 out_args[0] = in_args[0];
345}
346
347void
348munge_wwwl(void *args)
349{
350 volatile uint64_t *out_args = (volatile uint64_t*)args;
351 volatile uint32_t *in_args = (volatile uint32_t*)args;
352
353 out_args[3] = *(volatile uint64_t*)&in_args[3];
354 out_args[2] = in_args[2];
355 out_args[1] = in_args[1];
356 out_args[0] = in_args[0];
357}
358
359void
360munge_wwwwlw(void *args)
361{
362 volatile uint64_t *out_args = (volatile uint64_t*)args;
363 volatile uint32_t *in_args = (volatile uint32_t*)args;
364
365 out_args[5] = in_args[6];
366 out_args[4] = *(volatile uint64_t*)&in_args[4];
367 out_args[3] = in_args[3];
368 out_args[2] = in_args[2];
369 out_args[1] = in_args[1];
370 out_args[0] = in_args[0];
371}
372
373void
374munge_wwwwllww(void *args)
375{
376 volatile uint64_t *out_args = (volatile uint64_t*)args;
377 volatile uint32_t *in_args = (volatile uint32_t*)args;
378
379 out_args[7] = in_args[9];
380 out_args[6] = in_args[8];
381 out_args[5] = *(volatile uint64_t*)&in_args[6];
382 out_args[4] = *(volatile uint64_t*)&in_args[4];
383 out_args[3] = in_args[3];
384 out_args[2] = in_args[2];
385 out_args[1] = in_args[1];
386 out_args[0] = in_args[0];
387}
388
389void
390munge_wwwwl(void *args)
391{
392 volatile uint64_t *out_args = (volatile uint64_t*)args;
393 volatile uint32_t *in_args = (volatile uint32_t*)args;
394
395 out_args[4] = *(volatile uint64_t*)&in_args[4];
396 out_args[3] = in_args[3];
397 out_args[2] = in_args[2];
398 out_args[1] = in_args[1];
399 out_args[0] = in_args[0];
400}
401
402void
403munge_wwwwwl(void *args)
404{
405 volatile uint64_t *out_args = (volatile uint64_t*)args;
406 volatile uint32_t *in_args = (volatile uint32_t*)args;
407
408 out_args[5] = *(volatile uint64_t*)&in_args[5];
409 out_args[4] = in_args[4];
410 out_args[3] = in_args[3];
411 out_args[2] = in_args[2];
412 out_args[1] = in_args[1];
413 out_args[0] = in_args[0];
414}
415
416void
417munge_wwwwwlww(void *args)
418{
419 volatile uint64_t *out_args = (volatile uint64_t*)args;
420 volatile uint32_t *in_args = (volatile uint32_t*)args;
421
422 out_args[7] = in_args[8];
423 out_args[6] = in_args[7];
424 out_args[5] = *(volatile uint64_t*)&in_args[5];
425 out_args[4] = in_args[4];
426 out_args[3] = in_args[3];
427 out_args[2] = in_args[2];
428 out_args[1] = in_args[1];
429 out_args[0] = in_args[0];
430}
431
432void
433munge_wwwwwllw(void *args)
434{
435 volatile uint64_t *out_args = (volatile uint64_t*)args;
436 volatile uint32_t *in_args = (volatile uint32_t*)args;
437
438 out_args[7] = in_args[9];
439 out_args[6] = *(volatile uint64_t*)&in_args[7];
440 out_args[5] = *(volatile uint64_t*)&in_args[5];
441 out_args[4] = in_args[4];
442 out_args[3] = in_args[3];
443 out_args[2] = in_args[2];
444 out_args[1] = in_args[1];
445 out_args[0] = in_args[0];
446}
447
448void
449munge_wwwwwlll(void *args)
450{
451 volatile uint64_t *out_args = (volatile uint64_t*)args;
452 volatile uint32_t *in_args = (volatile uint32_t*)args;
453
454 out_args[7] = *(volatile uint64_t*)&in_args[9];
455 out_args[6] = *(volatile uint64_t*)&in_args[7];
456 out_args[5] = *(volatile uint64_t*)&in_args[5];
457 out_args[4] = in_args[4];
458 out_args[3] = in_args[3];
459 out_args[2] = in_args[2];
460 out_args[1] = in_args[1];
461 out_args[0] = in_args[0];
462}
463
464void
465munge_wwwwwwl(void *args)
466{
467 volatile uint64_t *out_args = (volatile uint64_t*)args;
468 volatile uint32_t *in_args = (volatile uint32_t*)args;
469
470 out_args[6] = *(volatile uint64_t*)&in_args[6];
471 out_args[5] = in_args[5];
472 out_args[4] = in_args[4];
473 out_args[3] = in_args[3];
474 out_args[2] = in_args[2];
475 out_args[1] = in_args[1];
476 out_args[0] = in_args[0];
477}
478
479void
480munge_wwwwwwlw(void *args)
481{
482 volatile uint64_t *out_args = (volatile uint64_t*)args;
483 volatile uint32_t *in_args = (volatile uint32_t*)args;
484
485 out_args[7] = in_args[8];
486 out_args[6] = *(volatile uint64_t*)&in_args[6];
487 out_args[5] = in_args[5];
488 out_args[4] = in_args[4];
489 out_args[3] = in_args[3];
490 out_args[2] = in_args[2];
491 out_args[1] = in_args[1];
492 out_args[0] = in_args[0];
493}
494
495void
496munge_wwwwwwll(void *args)
497{
498 volatile uint64_t *out_args = (volatile uint64_t*)args;
499 volatile uint32_t *in_args = (volatile uint32_t*)args;
500
501 out_args[7] = *(volatile uint64_t*)&in_args[8];
502 out_args[6] = *(volatile uint64_t*)&in_args[6];
503 out_args[5] = in_args[5];
504 out_args[4] = in_args[4];
505 out_args[3] = in_args[3];
506 out_args[2] = in_args[2];
507 out_args[1] = in_args[1];
508 out_args[0] = in_args[0];
509}
510
511void
512munge_wsw(void *args)
513{
514 volatile uint64_t *out_args = (volatile uint64_t*)args;
515 volatile uint32_t *in_args = (volatile uint32_t*)args;
516
517 out_args[2] = in_args[2];
518 out_args[1] = (int64_t)(int)in_args[1]; /* Sign-extend */
519 out_args[0] = in_args[0];
520}
521
522void
523munge_wws(void *args)
524{
525 volatile uint64_t *out_args = (volatile uint64_t*)args;
526 volatile uint32_t *in_args = (volatile uint32_t*)args;
527
528 out_args[2] = (int64_t)(int)in_args[2]; /* Sign-extend */
529 out_args[1] = in_args[1];
530 out_args[0] = in_args[0];
531}
532
533void
534munge_wwws(void *args)
535{
536 volatile uint64_t *out_args = (volatile uint64_t*)args;
537 volatile uint32_t *in_args = (volatile uint32_t*)args;
538
539 out_args[3] = (int64_t)(int)in_args[3]; /* Sign-extend */
540 out_args[2] = in_args[2];
541 out_args[1] = in_args[1];
542 out_args[0] = in_args[0];
543}
544
545
546void
547munge_wwwsw(void *args)
548{
549 volatile uint64_t *out_args = (volatile uint64_t*)args;
550 volatile uint32_t *in_args = (volatile uint32_t*)args;
551
552 out_args[4] = in_args[4];
553 out_args[3] = (int64_t)(int)in_args[3]; /* Sign-extend */
554 out_args[2] = in_args[2];
555 out_args[1] = in_args[1];
556 out_args[0] = in_args[0];
557}
558
559void
560munge_llllllll(void *args __unused)
561{
562 /* Nothing to do, already all 64-bit */
563}
564
565void
566munge_llllll(void *args __unused)
567{
568 /* Nothing to do, already all 64-bit */
569}
570
571void
572munge_llll(void *args __unused)
573{
574 /* Nothing to do, already all 64-bit */
575}
576
577void
578munge_lll(void *args __unused)
579{
580 /* Nothing to do, already all 64-bit */
581}
582
583void
584munge_ll(void *args __unused)
585{
586 /* Nothing to do, already all 64-bit */
587}
588
589void
590munge_l(void *args __unused)
591{
592 /* Nothing to do, already all 64-bit */
593}
594
595void
596munge_lw(void *args)
597{
598 volatile uint64_t *out_args = (volatile uint64_t*)args;
599 volatile uint32_t *in_args = (volatile uint32_t*)args;
600
601 out_args[1] = in_args[2];
602 out_args[0] = *(volatile uint64_t*)&in_args[0];
603}
604
605void
606munge_lwww(void *args)
607{
608 volatile uint64_t *out_args = (volatile uint64_t*)args;
609 volatile uint32_t *in_args = (volatile uint32_t*)args;
610
611 out_args[3] = in_args[4];
612 out_args[2] = in_args[3];
613 out_args[1] = in_args[2];
614 out_args[0] = *(volatile uint64_t*)&in_args[0];
615}
616
617void
618munge_lwwwwwww(void *args)
619{
620 volatile uint64_t *out_args = (volatile uint64_t*)args;
621 volatile uint32_t *in_args = (volatile uint32_t*)args;
622
623 out_args[7] = in_args[8];
624 out_args[6] = in_args[7];
625 out_args[5] = in_args[6];
626 out_args[4] = in_args[5];
627 out_args[3] = in_args[4];
628 out_args[2] = in_args[3];
629 out_args[1] = in_args[2];
630 out_args[0] = *(volatile uint64_t*)&in_args[0];
631}
632
633void
634munge_wwlww(void *args)
635{
636 volatile uint64_t *out_args = (volatile uint64_t*)args;
637 volatile uint32_t *in_args = (volatile uint32_t*)args;
638
639 out_args[4] = in_args[5];
640 out_args[3] = in_args[4];
641 out_args[2] = *(volatile uint64_t*)&in_args[2];
642 out_args[1] = in_args[1];
643 out_args[0] = in_args[0];
644}
645
646void
647munge_wwlwww(void *args)
648{
649 volatile uint64_t *out_args = (volatile uint64_t*)args;
650 volatile uint32_t *in_args = (volatile uint32_t*)args;
651
652 out_args[5] = in_args[6];
653 out_args[4] = in_args[5];
654 out_args[3] = in_args[4];
655 out_args[2] = *(volatile uint64_t*)&in_args[2];
656 out_args[1] = in_args[1];
657 out_args[0] = in_args[0];
658}
659
660void
661munge_wlwwwl(void *args)
662{
663 volatile uint64_t *out_args = (volatile uint64_t*)args;
664 volatile uint32_t *in_args = (volatile uint32_t*)args;
665
666 out_args[5] = *(volatile uint64_t*)&in_args[6];
667 out_args[4] = in_args[5];
668 out_args[3] = in_args[4];
669 out_args[2] = in_args[3];
670 out_args[1] = *(volatile uint64_t*)&in_args[1];
671 out_args[0] = in_args[0];
672}
673
674void
675munge_wwlwwwl(void *args)
676{
677 volatile uint64_t *out_args = (volatile uint64_t*)args;
678 volatile uint32_t *in_args = (volatile uint32_t*)args;
679
680 out_args[6] = *(volatile uint64_t*)&in_args[7];
681 out_args[5] = in_args[6];
682 out_args[4] = in_args[5];
683 out_args[3] = in_args[4];
684 out_args[2] = *(volatile uint64_t*)&in_args[2];
685 out_args[1] = in_args[1];
686 out_args[0] = in_args[0];
687}
688
689/*
690 * Munge array of 32-bit values into an array of 64-bit values,
691 * without sign extension. Note, src and dest can be the same
692 * (copies from end of array)
693 */
694static inline __attribute__((always_inline)) void
695munge_32_to_64_unsigned(volatile uint64_t *dest, volatile uint32_t *src, int count)
696{
697 int i;
698
699 for (i = count - 1; i >= 0; i--) {
700 dest[i] = src[i];
701 }
702}
703