1/*
2 * Copyright (c) 2000-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 * @OSF_FREE_COPYRIGHT@
30 */
31
32#include <pexpert/pexpert.h>
33#include <pexpert/protos.h>
34#include <pexpert/device_tree.h>
35#include <kern/debug.h>
36
37#if CONFIG_EMBEDDED
38#include <libkern/section_keywords.h>
39#endif
40
41static int DEBUGFlag;
42
43#if CONFIG_EMBEDDED
44SECURITY_READ_ONLY_LATE(static uint32_t) gPEKernelConfigurationBitmask;
45#else
46static uint32_t gPEKernelConfigurationBitmask;
47#endif
48
49int32_t gPESerialBaud = -1;
50
51int debug_cpu_performance_degradation_factor = 1;
52
53void pe_init_debug(void)
54{
55 boolean_t boot_arg_value;
56
57 if (!PE_parse_boot_argn("debug", &DEBUGFlag, sizeof (DEBUGFlag)))
58 DEBUGFlag = 0;
59
60 gPEKernelConfigurationBitmask = 0;
61
62 if (!PE_parse_boot_argn("assertions", &boot_arg_value, sizeof(boot_arg_value))) {
63#if MACH_ASSERT
64 boot_arg_value = TRUE;
65#else
66 boot_arg_value = FALSE;
67#endif
68 }
69 gPEKernelConfigurationBitmask |= (boot_arg_value ? kPEICanHasAssertions : 0);
70
71 if (!PE_parse_boot_argn("statistics", &boot_arg_value, sizeof(boot_arg_value))) {
72#if DEVELOPMENT || DEBUG
73 boot_arg_value = TRUE;
74#else
75 boot_arg_value = FALSE;
76#endif
77 }
78 gPEKernelConfigurationBitmask |= (boot_arg_value ? kPEICanHasStatistics : 0);
79
80#if SECURE_KERNEL
81 boot_arg_value = FALSE;
82#else
83 if (!PE_i_can_has_debugger(NULL)) {
84 boot_arg_value = FALSE;
85 } else if (!PE_parse_boot_argn("diagnostic_api", &boot_arg_value, sizeof(boot_arg_value))) {
86 boot_arg_value = TRUE;
87 }
88#endif
89 gPEKernelConfigurationBitmask |= (boot_arg_value ? kPEICanHasDiagnosticAPI : 0);
90
91
92 int factor = 1;
93 boolean_t have_bootarg = PE_parse_boot_argn("cpu-factor", &factor, sizeof (factor));
94 if (have_bootarg) {
95 debug_cpu_performance_degradation_factor = factor;
96 } else {
97 DTEntry root;
98 if (DTLookupEntry(NULL, "/", &root) == kSuccess) {
99 void *prop = NULL;
100 uint32_t size = 0;
101 if (DTGetProperty(root, "target-is-fpga", &prop, &size) == kSuccess) {
102 debug_cpu_performance_degradation_factor = 10;
103 }
104 }
105 }
106}
107
108void PE_enter_debugger(const char *cause)
109{
110 if (DEBUGFlag & DB_NMI)
111 Debugger(cause);
112}
113
114uint32_t
115PE_i_can_has_kernel_configuration(void)
116{
117 return gPEKernelConfigurationBitmask;
118}
119
120/* extern references */
121extern void vcattach(void);
122
123/* Globals */
124void (*PE_putc)(char c);
125
126void PE_init_printf(boolean_t vm_initialized)
127{
128 if (!vm_initialized) {
129 PE_putc = cnputc;
130 } else {
131 vcattach();
132 }
133}
134
135uint32_t
136PE_get_random_seed(unsigned char *dst_random_seed, uint32_t request_size)
137{
138 DTEntry entryP;
139 uint32_t size = 0;
140 void *dt_random_seed;
141
142 if ((DTLookupEntry(NULL, "/chosen", &entryP) == kSuccess)
143 && (DTGetProperty(entryP, "random-seed",
144 (void **)&dt_random_seed, &size) == kSuccess)) {
145 unsigned char *src_random_seed;
146 unsigned int i;
147 unsigned int null_count = 0;
148
149 src_random_seed = (unsigned char *)dt_random_seed;
150
151 if (size > request_size) size = request_size;
152
153 /*
154 * Copy from the device tree into the destination buffer,
155 * count the number of null bytes and null out the device tree.
156 */
157 for (i=0 ; i< size; i++, src_random_seed++, dst_random_seed++) {
158 *dst_random_seed = *src_random_seed;
159 null_count += *src_random_seed == (unsigned char)0;
160 *src_random_seed = (unsigned char)0;
161 }
162 if (null_count == size)
163 /* All nulls is no seed - return 0 */
164 size = 0;
165 }
166
167 return(size);
168}
169
170unsigned char appleClut8[ 256 * 3 ] = {
171// 00
172 0xFF,0xFF,0xFF, 0xFF,0xFF,0xCC, 0xFF,0xFF,0x99, 0xFF,0xFF,0x66,
173 0xFF,0xFF,0x33, 0xFF,0xFF,0x00, 0xFF,0xCC,0xFF, 0xFF,0xCC,0xCC,
174 0xFF,0xCC,0x99, 0xFF,0xCC,0x66, 0xFF,0xCC,0x33, 0xFF,0xCC,0x00,
175 0xFF,0x99,0xFF, 0xFF,0x99,0xCC, 0xFF,0x99,0x99, 0xFF,0x99,0x66,
176// 10
177 0xFF,0x99,0x33, 0xFF,0x99,0x00, 0xFF,0x66,0xFF, 0xFF,0x66,0xCC,
178 0xFF,0x66,0x99, 0xFF,0x66,0x66, 0xFF,0x66,0x33, 0xFF,0x66,0x00,
179 0xFF,0x33,0xFF, 0xFF,0x33,0xCC, 0xFF,0x33,0x99, 0xFF,0x33,0x66,
180 0xFF,0x33,0x33, 0xFF,0x33,0x00, 0xFF,0x00,0xFF, 0xFF,0x00,0xCC,
181// 20
182 0xFF,0x00,0x99, 0xFF,0x00,0x66, 0xFF,0x00,0x33, 0xFF,0x00,0x00,
183 0xCC,0xFF,0xFF, 0xCC,0xFF,0xCC, 0xCC,0xFF,0x99, 0xCC,0xFF,0x66,
184 0xCC,0xFF,0x33, 0xCC,0xFF,0x00, 0xCC,0xCC,0xFF, 0xCC,0xCC,0xCC,
185 0xCC,0xCC,0x99, 0xCC,0xCC,0x66, 0xCC,0xCC,0x33, 0xCC,0xCC,0x00,
186// 30
187 0xCC,0x99,0xFF, 0xCC,0x99,0xCC, 0xCC,0x99,0x99, 0xCC,0x99,0x66,
188 0xCC,0x99,0x33, 0xCC,0x99,0x00, 0xCC,0x66,0xFF, 0xCC,0x66,0xCC,
189 0xCC,0x66,0x99, 0xCC,0x66,0x66, 0xCC,0x66,0x33, 0xCC,0x66,0x00,
190 0xCC,0x33,0xFF, 0xCC,0x33,0xCC, 0xCC,0x33,0x99, 0xCC,0x33,0x66,
191// 40
192 0xCC,0x33,0x33, 0xCC,0x33,0x00, 0xCC,0x00,0xFF, 0xCC,0x00,0xCC,
193 0xCC,0x00,0x99, 0xCC,0x00,0x66, 0xCC,0x00,0x33, 0xCC,0x00,0x00,
194 0x99,0xFF,0xFF, 0x99,0xFF,0xCC, 0x99,0xFF,0x99, 0x99,0xFF,0x66,
195 0x99,0xFF,0x33, 0x99,0xFF,0x00, 0x99,0xCC,0xFF, 0x99,0xCC,0xCC,
196// 50
197 0x99,0xCC,0x99, 0x99,0xCC,0x66, 0x99,0xCC,0x33, 0x99,0xCC,0x00,
198 0x99,0x99,0xFF, 0x99,0x99,0xCC, 0x99,0x99,0x99, 0x99,0x99,0x66,
199 0x99,0x99,0x33, 0x99,0x99,0x00, 0x99,0x66,0xFF, 0x99,0x66,0xCC,
200 0x99,0x66,0x99, 0x99,0x66,0x66, 0x99,0x66,0x33, 0x99,0x66,0x00,
201// 60
202 0x99,0x33,0xFF, 0x99,0x33,0xCC, 0x99,0x33,0x99, 0x99,0x33,0x66,
203 0x99,0x33,0x33, 0x99,0x33,0x00, 0x99,0x00,0xFF, 0x99,0x00,0xCC,
204 0x99,0x00,0x99, 0x99,0x00,0x66, 0x99,0x00,0x33, 0x99,0x00,0x00,
205 0x66,0xFF,0xFF, 0x66,0xFF,0xCC, 0x66,0xFF,0x99, 0x66,0xFF,0x66,
206// 70
207 0x66,0xFF,0x33, 0x66,0xFF,0x00, 0x66,0xCC,0xFF, 0x66,0xCC,0xCC,
208 0x66,0xCC,0x99, 0x66,0xCC,0x66, 0x66,0xCC,0x33, 0x66,0xCC,0x00,
209 0x66,0x99,0xFF, 0x66,0x99,0xCC, 0x66,0x99,0x99, 0x66,0x99,0x66,
210 0x66,0x99,0x33, 0x66,0x99,0x00, 0x66,0x66,0xFF, 0x66,0x66,0xCC,
211// 80
212 0x66,0x66,0x99, 0x66,0x66,0x66, 0x66,0x66,0x33, 0x66,0x66,0x00,
213 0x66,0x33,0xFF, 0x66,0x33,0xCC, 0x66,0x33,0x99, 0x66,0x33,0x66,
214 0x66,0x33,0x33, 0x66,0x33,0x00, 0x66,0x00,0xFF, 0x66,0x00,0xCC,
215 0x66,0x00,0x99, 0x66,0x00,0x66, 0x66,0x00,0x33, 0x66,0x00,0x00,
216// 90
217 0x33,0xFF,0xFF, 0x33,0xFF,0xCC, 0x33,0xFF,0x99, 0x33,0xFF,0x66,
218 0x33,0xFF,0x33, 0x33,0xFF,0x00, 0x33,0xCC,0xFF, 0x33,0xCC,0xCC,
219 0x33,0xCC,0x99, 0x33,0xCC,0x66, 0x33,0xCC,0x33, 0x33,0xCC,0x00,
220 0x33,0x99,0xFF, 0x33,0x99,0xCC, 0x33,0x99,0x99, 0x33,0x99,0x66,
221// a0
222 0x33,0x99,0x33, 0x33,0x99,0x00, 0x33,0x66,0xFF, 0x33,0x66,0xCC,
223 0x33,0x66,0x99, 0x33,0x66,0x66, 0x33,0x66,0x33, 0x33,0x66,0x00,
224 0x33,0x33,0xFF, 0x33,0x33,0xCC, 0x33,0x33,0x99, 0x33,0x33,0x66,
225 0x33,0x33,0x33, 0x33,0x33,0x00, 0x33,0x00,0xFF, 0x33,0x00,0xCC,
226// b0
227 0x33,0x00,0x99, 0x33,0x00,0x66, 0x33,0x00,0x33, 0x33,0x00,0x00,
228 0x00,0xFF,0xFF, 0x00,0xFF,0xCC, 0x00,0xFF,0x99, 0x00,0xFF,0x66,
229 0x00,0xFF,0x33, 0x00,0xFF,0x00, 0x00,0xCC,0xFF, 0x00,0xCC,0xCC,
230 0x00,0xCC,0x99, 0x00,0xCC,0x66, 0x00,0xCC,0x33, 0x00,0xCC,0x00,
231// c0
232 0x00,0x99,0xFF, 0x00,0x99,0xCC, 0x00,0x99,0x99, 0x00,0x99,0x66,
233 0x00,0x99,0x33, 0x00,0x99,0x00, 0x00,0x66,0xFF, 0x00,0x66,0xCC,
234 0x00,0x66,0x99, 0x00,0x66,0x66, 0x00,0x66,0x33, 0x00,0x66,0x00,
235 0x00,0x33,0xFF, 0x00,0x33,0xCC, 0x00,0x33,0x99, 0x00,0x33,0x66,
236// d0
237 0x00,0x33,0x33, 0x00,0x33,0x00, 0x00,0x00,0xFF, 0x00,0x00,0xCC,
238 0x00,0x00,0x99, 0x00,0x00,0x66, 0x00,0x00,0x33, 0xEE,0x00,0x00,
239 0xDD,0x00,0x00, 0xBB,0x00,0x00, 0xAA,0x00,0x00, 0x88,0x00,0x00,
240 0x77,0x00,0x00, 0x55,0x00,0x00, 0x44,0x00,0x00, 0x22,0x00,0x00,
241// e0
242 0x11,0x00,0x00, 0x00,0xEE,0x00, 0x00,0xDD,0x00, 0x00,0xBB,0x00,
243 0x00,0xAA,0x00, 0x00,0x88,0x00, 0x00,0x77,0x00, 0x00,0x55,0x00,
244 0x00,0x44,0x00, 0x00,0x22,0x00, 0x00,0x11,0x00, 0x00,0x00,0xEE,
245 0x00,0x00,0xDD, 0x00,0x00,0xBB, 0x00,0x00,0xAA, 0x00,0x00,0x88,
246// f0
247 0x00,0x00,0x77, 0x00,0x00,0x55, 0x00,0x00,0x44, 0x00,0x00,0x22,
248 0x00,0x00,0x11, 0xEE,0xEE,0xEE, 0xDD,0xDD,0xDD, 0xBB,0xBB,0xBB,
249 0xAA,0xAA,0xAA, 0x88,0x88,0x88, 0x77,0x77,0x77, 0x55,0x55,0x55,
250 0x44,0x44,0x44, 0x22,0x22,0x22, 0x11,0x11,0x11, 0x00,0x00,0x00
251};
252
253