Integrate cctools-822 changes
[striptease.git] / libstuff / arch.c
blob10488c15eae851d8ce1d401bda26aa7b6195c6f1
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
23 #ifndef RLD
24 #include "stdio.h"
25 #endif /* !defined(RLD) */
26 #include "stdlib.h"
27 #include "string.h"
28 #include <mach/mach.h>
29 #include "stuff/openstep_mach.h"
30 #include "stuff/arch.h"
31 #include "stuff/allocate.h"
34 * The array of all currently know architecture flags (terminated with an entry
35 * with all zeros). Pointer to this returned with get_arch_flags().
37 #ifdef __DYNAMIC__
38 static struct arch_flag arch_flags[] = {
39 #else
40 static const struct arch_flag arch_flags[] = {
41 #endif
42 { "any", CPU_TYPE_ANY, CPU_SUBTYPE_MULTIPLE },
43 { "little", CPU_TYPE_ANY, CPU_SUBTYPE_LITTLE_ENDIAN },
44 { "big", CPU_TYPE_ANY, CPU_SUBTYPE_BIG_ENDIAN },
46 /* 64-bit Mach-O architectures */
48 /* architecture families */
49 { "ppc64", CPU_TYPE_POWERPC64, CPU_SUBTYPE_POWERPC_ALL },
50 { "x86_64", CPU_TYPE_X86_64, CPU_SUBTYPE_X86_64_ALL },
51 /* specific architecture implementations */
52 { "ppc970-64", CPU_TYPE_POWERPC64, CPU_SUBTYPE_POWERPC_970 },
54 /* 32-bit Mach-O architectures */
56 /* architecture families */
57 { "ppc", CPU_TYPE_POWERPC, CPU_SUBTYPE_POWERPC_ALL },
58 { "i386", CPU_TYPE_I386, CPU_SUBTYPE_I386_ALL },
59 { "m68k", CPU_TYPE_MC680x0, CPU_SUBTYPE_MC680x0_ALL },
60 { "hppa", CPU_TYPE_HPPA, CPU_SUBTYPE_HPPA_ALL },
61 { "sparc", CPU_TYPE_SPARC, CPU_SUBTYPE_SPARC_ALL },
62 { "m88k", CPU_TYPE_MC88000, CPU_SUBTYPE_MC88000_ALL },
63 { "i860", CPU_TYPE_I860, CPU_SUBTYPE_I860_ALL },
64 { "veo", CPU_TYPE_VEO, CPU_SUBTYPE_VEO_ALL },
65 { "arm", CPU_TYPE_ARM, CPU_SUBTYPE_ARM_ALL },
66 /* specific architecture implementations */
67 { "ppc601", CPU_TYPE_POWERPC, CPU_SUBTYPE_POWERPC_601 },
68 { "ppc603", CPU_TYPE_POWERPC, CPU_SUBTYPE_POWERPC_603 },
69 { "ppc603e",CPU_TYPE_POWERPC, CPU_SUBTYPE_POWERPC_603e },
70 { "ppc603ev",CPU_TYPE_POWERPC,CPU_SUBTYPE_POWERPC_603ev },
71 { "ppc604", CPU_TYPE_POWERPC, CPU_SUBTYPE_POWERPC_604 },
72 { "ppc604e",CPU_TYPE_POWERPC, CPU_SUBTYPE_POWERPC_604e },
73 { "ppc750", CPU_TYPE_POWERPC, CPU_SUBTYPE_POWERPC_750 },
74 { "ppc7400",CPU_TYPE_POWERPC, CPU_SUBTYPE_POWERPC_7400 },
75 { "ppc7450",CPU_TYPE_POWERPC, CPU_SUBTYPE_POWERPC_7450 },
76 { "ppc970", CPU_TYPE_POWERPC, CPU_SUBTYPE_POWERPC_970 },
77 { "i486", CPU_TYPE_I386, CPU_SUBTYPE_486 },
78 { "i486SX", CPU_TYPE_I386, CPU_SUBTYPE_486SX },
79 { "pentium",CPU_TYPE_I386, CPU_SUBTYPE_PENT }, /* same as i586 */
80 { "i586", CPU_TYPE_I386, CPU_SUBTYPE_586 },
81 { "pentpro", CPU_TYPE_I386, CPU_SUBTYPE_PENTPRO }, /* same as i686 */
82 { "i686", CPU_TYPE_I386, CPU_SUBTYPE_PENTPRO },
83 { "pentIIm3",CPU_TYPE_I386, CPU_SUBTYPE_PENTII_M3 },
84 { "pentIIm5",CPU_TYPE_I386, CPU_SUBTYPE_PENTII_M5 },
85 { "pentium4",CPU_TYPE_I386, CPU_SUBTYPE_PENTIUM_4 },
86 { "m68030", CPU_TYPE_MC680x0, CPU_SUBTYPE_MC68030_ONLY },
87 { "m68040", CPU_TYPE_MC680x0, CPU_SUBTYPE_MC68040 },
88 { "hppa7100LC", CPU_TYPE_HPPA, CPU_SUBTYPE_HPPA_7100LC },
89 { "veo1", CPU_TYPE_VEO, CPU_SUBTYPE_VEO_1 },
90 { "veo2", CPU_TYPE_VEO, CPU_SUBTYPE_VEO_2 },
91 { "veo3", CPU_TYPE_VEO, CPU_SUBTYPE_VEO_3 },
92 { "veo4", CPU_TYPE_VEO, CPU_SUBTYPE_VEO_4 },
93 { "armv4t", CPU_TYPE_ARM, CPU_SUBTYPE_ARM_V4T},
94 { "armv5", CPU_TYPE_ARM, CPU_SUBTYPE_ARM_V5TEJ},
95 { "xscale", CPU_TYPE_ARM, CPU_SUBTYPE_ARM_XSCALE},
96 { "armv6", CPU_TYPE_ARM, CPU_SUBTYPE_ARM_V6 },
97 { "armv7", CPU_TYPE_ARM, CPU_SUBTYPE_ARM_V7 },
98 { "armv7f", CPU_TYPE_ARM, CPU_SUBTYPE_ARM_V7F },
99 { "armv7k", CPU_TYPE_ARM, CPU_SUBTYPE_ARM_V7K },
100 { NULL, 0, 0 }
103 #ifndef RLD
105 * get_arch_from_flag() is passed a name of an architecture flag and returns
106 * zero if that flag is not known and non-zero if the flag is known.
107 * If the pointer to the arch_flag is not NULL it is filled in with the
108 * arch_flag struct that matches the name.
110 __private_extern__
112 get_arch_from_flag(
113 char *name,
114 struct arch_flag *arch_flag)
116 uint32_t i;
118 for(i = 0; arch_flags[i].name != NULL; i++){
119 if(strcmp(arch_flags[i].name, name) == 0){
120 if(arch_flag != NULL)
121 *arch_flag = arch_flags[i];
122 return(1);
125 if(arch_flag != NULL)
126 memset(arch_flag, '\0', sizeof(struct arch_flag));
127 return(0);
131 * get_arch_flags() returns a pointer to an array of all currently know
132 * architecture flags (terminated with an entry with all zeros).
134 __private_extern__
135 const struct arch_flag *
136 get_arch_flags(
137 void)
139 return(arch_flags);
141 #endif /* !defined(RLD) */
144 * get_arch_name_from_types() returns the name of the architecture for the
145 * specified cputype and cpusubtype if known. If unknown it returns a pointer
146 * to the an allocated string "cputype X cpusubtype Y" where X and Y are decimal
147 * values.
149 __private_extern__
150 const char *
151 get_arch_name_from_types(
152 cpu_type_t cputype,
153 cpu_subtype_t cpusubtype)
155 uint32_t i;
156 char *p;
158 for(i = 0; arch_flags[i].name != NULL; i++){
159 if(arch_flags[i].cputype == cputype &&
160 (arch_flags[i].cpusubtype & ~CPU_SUBTYPE_MASK) ==
161 (cpusubtype & ~CPU_SUBTYPE_MASK))
162 return(arch_flags[i].name);
164 #ifndef RLD
165 p = savestr("cputype 1234567890 cpusubtype 1234567890");
166 if(p != NULL)
167 sprintf(p, "cputype %u cpusubtype %u", cputype,
168 cpusubtype & ~CPU_SUBTYPE_MASK);
169 #else
170 /* there is no sprintf() in the rld kernel API's */
171 p = savestr("cputype ?? cpusubtype ??");
172 #endif
173 return(p);
177 * get_arch_family_from_cputype() returns the family architecture for the
178 * specified cputype if known. If unknown it returns NULL.
180 __private_extern__
181 const struct arch_flag *
182 get_arch_family_from_cputype(
183 cpu_type_t cputype)
185 uint32_t i;
187 for(i = 0; arch_flags[i].name != NULL; i++){
188 if(arch_flags[i].cputype == cputype)
189 return(arch_flags + i);
191 return(NULL);
195 * get_byte_sex_from_flag() returns the byte sex of the architecture for the
196 * specified cputype and cpusubtype if known. If unknown it returns
197 * UNKNOWN_BYTE_SEX. If the bytesex can be determined directly as in the case
198 * of reading a magic number from a file that should be done and this routine
199 * should not be used as it could be out of date.
201 __private_extern__
202 enum byte_sex
203 get_byte_sex_from_flag(
204 const struct arch_flag *flag)
206 if(flag->cputype == CPU_TYPE_MC680x0 ||
207 flag->cputype == CPU_TYPE_MC88000 ||
208 flag->cputype == CPU_TYPE_POWERPC ||
209 flag->cputype == CPU_TYPE_POWERPC64 ||
210 flag->cputype == CPU_TYPE_HPPA ||
211 flag->cputype == CPU_TYPE_SPARC ||
212 flag->cputype == CPU_TYPE_I860 ||
213 flag->cputype == CPU_TYPE_VEO)
214 return BIG_ENDIAN_BYTE_SEX;
215 else if(flag->cputype == CPU_TYPE_I386 ||
216 flag->cputype == CPU_TYPE_X86_64 ||
217 flag->cputype == CPU_TYPE_ARM)
218 return LITTLE_ENDIAN_BYTE_SEX;
219 else
220 return UNKNOWN_BYTE_SEX;
223 #ifndef RLD
225 * get_stack_direction_from_flag() returns the direction the stack grows as
226 * either positive (+1) or negative (-1) of the architecture for the
227 * specified cputype and cpusubtype if known. If unknown it returns 0.
229 __private_extern__
231 get_stack_direction_from_flag(
232 const struct arch_flag *flag)
234 if(flag->cputype == CPU_TYPE_MC680x0 ||
235 flag->cputype == CPU_TYPE_MC88000 ||
236 flag->cputype == CPU_TYPE_POWERPC ||
237 flag->cputype == CPU_TYPE_I386 ||
238 flag->cputype == CPU_TYPE_SPARC ||
239 flag->cputype == CPU_TYPE_I860 ||
240 flag->cputype == CPU_TYPE_VEO ||
241 flag->cputype == CPU_TYPE_ARM)
242 return(-1);
243 else if(flag->cputype == CPU_TYPE_HPPA)
244 return(+1);
245 else
246 return(0);
250 * get_stack_addr_from_flag() returns the default starting address of the user
251 * stack. This should be in the header file <bsd/XXX/vmparam.h> as USRSTACK.
252 * Since some architectures have come and gone and come back and because you
253 * can't include all of these headers in one source the constants have been
254 * copied here.
256 __private_extern__
257 uint64_t
258 get_stack_addr_from_flag(
259 const struct arch_flag *flag)
261 switch(flag->cputype){
262 case CPU_TYPE_MC680x0:
263 return(0x04000000);
264 case CPU_TYPE_MC88000:
265 return(0xffffe000);
266 case CPU_TYPE_POWERPC:
267 case CPU_TYPE_VEO:
268 case CPU_TYPE_I386:
269 return(0xc0000000);
270 case CPU_TYPE_ARM:
271 return(0x30000000);
272 case CPU_TYPE_SPARC:
273 return(0xf0000000);
274 case CPU_TYPE_I860:
275 return(0);
276 case CPU_TYPE_HPPA:
277 return(0xc0000000-0x04000000);
278 case CPU_TYPE_POWERPC64:
279 return(0x7ffff00000000LL);
280 case CPU_TYPE_X86_64:
281 return(0x7fff5fc00000LL);
282 default:
283 return(0);
288 * get_stack_size_from_flag() returns the default size of the userstack. This
289 * should be in the header file <bsd/XXX/vmparam.h> as MAXSSIZ. Since some
290 * architectures have come and gone and come back, you can't include all of
291 * these headers in one source and some of the constants covered the whole
292 * address space the common value of 64meg was chosen.
294 __private_extern__
295 uint32_t
296 get_stack_size_from_flag(
297 const struct arch_flag *flag)
299 #ifdef __MWERKS__
300 const struct arch_flag *dummy;
301 dummy = flag;
302 #endif
304 return(64*1024*1024);
306 #endif /* !defined(RLD) */
309 * get_segalign_from_flag() returns the default segment alignment (page size).
311 __private_extern__
312 uint32_t
313 get_segalign_from_flag(
314 const struct arch_flag *flag)
316 if(flag->cputype == CPU_TYPE_POWERPC ||
317 flag->cputype == CPU_TYPE_POWERPC64 ||
318 flag->cputype == CPU_TYPE_VEO ||
319 flag->cputype == CPU_TYPE_I386 ||
320 flag->cputype == CPU_TYPE_X86_64 ||
321 flag->cputype == CPU_TYPE_ARM)
322 return(0x1000); /* 4K */
323 else
324 return(0x2000); /* 8K */
328 * get_segprot_from_flag() returns the default segment protection.
330 __private_extern__
331 vm_prot_t
332 get_segprot_from_flag(
333 const struct arch_flag *flag)
335 if(flag->cputype == CPU_TYPE_I386)
336 return(VM_PROT_READ | VM_PROT_WRITE);
337 else
338 return(VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE);
342 * get_shared_region_size_from_flag() returns the default shared
343 * region size.
345 __private_extern__
346 uint32_t
347 get_shared_region_size_from_flag(
348 const struct arch_flag *flag)
350 if(flag->cputype == CPU_TYPE_ARM)
351 return (0x08000000);
352 else
353 return (0x10000000);
357 * force_cpusubtype_ALL_for_cputype() takes a cputype and returns TRUE if for
358 * that cputype the cpusubtype should always be forced to the ALL cpusubtype,
359 * otherwise it returns FALSE.
361 __private_extern__
362 enum bool
363 force_cpusubtype_ALL_for_cputype(
364 cpu_type_t cputype)
366 if(cputype == CPU_TYPE_I386)
367 return(TRUE);
368 else
369 return(FALSE);