]> git.saurik.com Git - apple/xnu.git/blame - bsd/kern/mach_fat.c
xnu-1228.7.58.tar.gz
[apple/xnu.git] / bsd / kern / mach_fat.c
CommitLineData
1c79356b 1/*
2d21ac55 2 * Copyright (c) 1991-2005 Apple Computer, Inc. All rights reserved.
5d5c5d0d 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
2d21ac55
A
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.
8f6c56a5 14 *
2d21ac55
A
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b 27 */
1c79356b
A
28#include <sys/param.h>
29#include <sys/types.h>
30#include <sys/uio.h>
31#include <sys/vnode.h>
32#include <vm/vm_kern.h>
33#include <mach/kern_return.h>
34#include <mach/vm_param.h>
35#include <kern/cpu_number.h>
36#include <mach-o/fat.h>
37#include <kern/mach_loader.h>
0c530ab8
A
38#include <libkern/OSByteOrder.h>
39#include <machine/exec.h>
1c79356b
A
40
41/**********************************************************************
55e303ae 42 * Routine: fatfile_getarch2()
1c79356b
A
43 *
44 * Function: Locate the architecture-dependant contents of a fat
45 * file that match this CPU.
46 *
47 * Args: vp: The vnode for the fat file.
48 * header: A pointer to the fat file header.
91447636
A
49 * req_cpu_type: The required cpu type.
50 * mask_bits: Bits to mask from the sub-image type when
51 * grading it vs. the req_cpu_type
1c79356b
A
52 * archret (out): Pointer to fat_arch structure to hold
53 * the results.
54 *
55 * Returns: KERN_SUCCESS: Valid architecture found.
56 * KERN_FAILURE: No valid architecture found.
57 **********************************************************************/
55e303ae
A
58static load_return_t
59fatfile_getarch2(
91447636 60#if 0
55e303ae 61 struct vnode *vp,
91447636
A
62#else
63 __unused struct vnode *vp,
64#endif
55e303ae 65 vm_offset_t data_ptr,
91447636
A
66 cpu_type_t req_cpu_type,
67 cpu_type_t mask_bits,
55e303ae 68 struct fat_arch *archret)
1c79356b
A
69{
70 /* vm_pager_t pager; */
71 vm_offset_t addr;
72 vm_size_t size;
1c79356b 73 load_return_t lret;
1c79356b
A
74 struct fat_arch *arch;
75 struct fat_arch *best_arch;
76 int grade;
77 int best_grade;
78 int nfat_arch;
2d21ac55
A
79 off_t end_of_archs;
80 cpu_type_t testtype;
81 cpu_type_t testsubtype;
1c79356b 82 struct fat_header *header;
91447636 83#if 0
1c79356b 84 off_t filesize;
91447636 85#endif
1c79356b
A
86
87 /*
88 * Get the pager for the file.
89 */
90
91 header = (struct fat_header *)data_ptr;
92
93 /*
94 * Map portion that must be accessible directly into
95 * kernel's map.
96 */
0c530ab8 97 nfat_arch = OSSwapBigToHostInt32(header->nfat_arch);
1c79356b 98
2d21ac55
A
99 end_of_archs = (off_t)nfat_arch * sizeof(struct fat_arch) +
100 sizeof(struct fat_header);
1c79356b
A
101#if 0
102 filesize = ubc_getsize(vp);
0b4e3aa0 103 if (end_of_archs > (int)filesize) {
1c79356b
A
104 return(LOAD_BADMACHO);
105 }
106#endif
107
8f6c56a5
A
108 /*
109 * This check is limited on the top end because we are reading
110 * only PAGE_SIZE bytes
111 */
112 if (end_of_archs > PAGE_SIZE ||
2d21ac55 113 end_of_archs < (sizeof(struct fat_header)+sizeof(struct fat_arch)))
1c79356b 114 return(LOAD_BADMACHO);
0c530ab8 115
1c79356b
A
116 /*
117 * Round size of fat_arch structures up to page boundry.
118 */
55e303ae 119 size = round_page_32(end_of_archs);
91447636 120 if (size == 0)
1c79356b
A
121 return(LOAD_BADMACHO);
122
123 /*
2d21ac55
A
124 * Ignore LIB64 flag so that binary slices with the flag set
125 * don't choke in grade_binary.
1c79356b 126 */
2d21ac55
A
127 mask_bits |= CPU_SUBTYPE_LIB64;
128
129 /*
130 * Scan the fat_arch's looking for the best one. */
1c79356b 131 addr = data_ptr;
1c79356b
A
132 best_arch = NULL;
133 best_grade = 0;
134 arch = (struct fat_arch *) (addr + sizeof(struct fat_header));
135 for (; nfat_arch-- > 0; arch++) {
136
2d21ac55
A
137 /*
138 * Collect flags from both cputype and cpusubtype
139 */
140 testtype = OSSwapBigToHostInt32(arch->cputype) |
141 (OSSwapBigToHostInt32(arch->cpusubtype) &
142 CPU_SUBTYPE_MASK);
143 testsubtype = OSSwapBigToHostInt32(arch->cpusubtype)
144 & ~CPU_SUBTYPE_MASK;
145
1c79356b
A
146 /*
147 * Check to see if right cpu type.
148 */
2d21ac55 149 if((testtype & ~mask_bits) != req_cpu_type) {
1c79356b 150 continue;
2d21ac55 151 }
1c79356b
A
152
153 /*
2d21ac55 154 * Get the grade of the cpu subtype (without feature flags)
1c79356b 155 */
2d21ac55
A
156 grade = grade_binary(
157 (testtype & ~CPU_SUBTYPE_LIB64),
158 testsubtype);
1c79356b
A
159
160 /*
161 * Remember it if it's the best we've seen.
162 */
163 if (grade > best_grade) {
164 best_grade = grade;
165 best_arch = arch;
166 }
167 }
168
169 /*
170 * Return our results.
171 */
172 if (best_arch == NULL) {
173 lret = LOAD_BADARCH;
174 } else {
175 archret->cputype =
0c530ab8 176 OSSwapBigToHostInt32(best_arch->cputype);
1c79356b 177 archret->cpusubtype =
0c530ab8 178 OSSwapBigToHostInt32(best_arch->cpusubtype);
1c79356b 179 archret->offset =
0c530ab8 180 OSSwapBigToHostInt32(best_arch->offset);
1c79356b 181 archret->size =
0c530ab8 182 OSSwapBigToHostInt32(best_arch->size);
1c79356b 183 archret->align =
0c530ab8 184 OSSwapBigToHostInt32(best_arch->align);
1c79356b
A
185
186 lret = LOAD_SUCCESS;
187 }
188
189 /*
190 * Free the memory we allocated and return.
191 */
192 return(lret);
193}
194
55e303ae
A
195load_return_t
196fatfile_getarch_affinity(
197 struct vnode *vp,
198 vm_offset_t data_ptr,
199 struct fat_arch *archret,
200 int affinity)
201{
202 load_return_t lret;
0c530ab8 203 int handler = (exec_archhandler_ppc.path[0] != 0);
55e303ae
A
204 cpu_type_t primary_type, fallback_type;
205
206 if (handler && affinity) {
0c530ab8
A
207 primary_type = CPU_TYPE_POWERPC;
208 fallback_type = cpu_type();
55e303ae 209 } else {
0c530ab8
A
210 primary_type = cpu_type();
211 fallback_type = CPU_TYPE_POWERPC;
55e303ae 212 }
91447636 213 /*
2d21ac55 214 * Ignore all architectural bits when determining if an image
91447636
A
215 * in a fat file should be skipped or graded.
216 */
2d21ac55
A
217 lret = fatfile_getarch2(vp, data_ptr, primary_type,
218 CPU_ARCH_MASK, archret);
55e303ae
A
219 if ((lret != 0) && handler) {
220 lret = fatfile_getarch2(vp, data_ptr, fallback_type,
2d21ac55 221 CPU_SUBTYPE_LIB64, archret);
55e303ae
A
222 }
223 return lret;
224}
225
226/**********************************************************************
227 * Routine: fatfile_getarch()
228 *
229 * Function: Locate the architecture-dependant contents of a fat
230 * file that match this CPU.
231 *
232 * Args: vp: The vnode for the fat file.
233 * header: A pointer to the fat file header.
234 * archret (out): Pointer to fat_arch structure to hold
235 * the results.
236 *
237 * Returns: KERN_SUCCESS: Valid architecture found.
238 * KERN_FAILURE: No valid architecture found.
239 **********************************************************************/
240load_return_t
241fatfile_getarch(
242 struct vnode *vp,
243 vm_offset_t data_ptr,
244 struct fat_arch *archret)
245{
2d21ac55
A
246 return fatfile_getarch2(vp, data_ptr, cpu_type(),
247 CPU_SUBTYPE_LIB64, archret);
91447636
A
248}
249
250/**********************************************************************
251 * Routine: fatfile_getarch_with_bits()
252 *
253 * Function: Locate the architecture-dependant contents of a fat
254 * file that match this CPU.
255 *
256 * Args: vp: The vnode for the fat file.
257 * archbits: Architecture specific feature bits
258 * header: A pointer to the fat file header.
259 * archret (out): Pointer to fat_arch structure to hold
260 * the results.
261 *
262 * Returns: KERN_SUCCESS: Valid architecture found.
263 * KERN_FAILURE: No valid architecture found.
264 **********************************************************************/
265load_return_t
266fatfile_getarch_with_bits(
267 struct vnode *vp,
268 integer_t archbits,
269 vm_offset_t data_ptr,
270 struct fat_arch *archret)
271{
2d21ac55
A
272 return fatfile_getarch2(vp, data_ptr, archbits | cpu_type(),
273 CPU_SUBTYPE_LIB64, archret);
55e303ae 274}
1c79356b 275