1 | ␉/*␊ |
2 | * Copyright (c) 2009-2010 Evan Lojewski. All rights reserved.␊ |
3 | *␊ |
4 | */␊ |
5 | ␊ |
6 | #include "libsaio.h"␊ |
7 | #include "kernel_patcher.h"␊ |
8 | #include "platform.h"␊ |
9 | #include "modules.h"␊ |
10 | ␊ |
11 | #ifndef DEBUG_KERNEL_PATCHER␊ |
12 | #define DEBUG_KERNEL_PATCHER 0␊ |
13 | #endif␊ |
14 | ␊ |
15 | #if DEBUG_KERNEL_PATCHER␊ |
16 | #define DBG(x...)␉printf(x)␊ |
17 | #else␊ |
18 | #define DBG(x...)␊ |
19 | #endif␊ |
20 | ␊ |
21 | long long symbol_handler(char* module, char* symbolName, long long addr, char is64);␊ |
22 | ␊ |
23 | patchRoutine_t* patches = NULL;␊ |
24 | kernSymbols_t* kernelSymbols = NULL;␊ |
25 | static void register_kernel_patch(void* patch, int arch, int cpus);␊ |
26 | static void register_kernel_symbol(int kernelType, const char* name);␊ |
27 | static kernSymbols_t* lookup_kernel_symbol(const char* name);␊ |
28 | static int determineKernelArchitecture(void* kernelData);␊ |
29 | static int locate_symbols(void* kernelData);␊ |
30 | static unsigned int parse_mach_64(char *module, void* binary, long long(*symbol_handler)(char*, char*, long long, char));␊ |
31 | static unsigned int handle_symtable_64(char *module, UInt32 base, struct symtab_command* symtabCommand, long long(*symbol_handler)(char*, char*, long long, char), char is64);␊ |
32 | ␊ |
33 | /*␊ |
34 | * Internal patches provided by this module.␊ |
35 | */␊ |
36 | static void patch_cpuid_set_info_all(void* kernelData);␊ |
37 | static void patch_cpuid_set_info_32(void* kernelData, UInt32 impersonateFamily, UInt8 impersonateModel);␊ |
38 | static void patch_cpuid_set_info_64(void* kernelData, UInt32 impersonateFamily, UInt8 impersonateModel);␊ |
39 | ␊ |
40 | //static void patch_pmCPUExitHaltToOff(void* kernelData);␊ |
41 | static void patch_lapic_init(void* kernelData);␊ |
42 | static void patch_commpage_stuff_routine(void* kernelData);␊ |
43 | static void patch_lapic_configure(void* kernelData);␊ |
44 | //static void patch_lapic_interrupt(void* kernelData);␊ |
45 | ␊ |
46 | void patch_kernel(void* kernelData, void* arg2, void* arg3, void *arg4, void* arg5, void* arg6);␊ |
47 | void kernel_patcher_ignore_cache(void* arg1, void* arg2, void* arg3, void *arg4, void* arg5, void* arg6);␊ |
48 | void kernel_patcher_ignore_cache(void* arg1, void* arg2, void* arg3, void *arg4, void* arg5, void* arg6){}␊ |
49 | ␊ |
50 | void KernelPatcher_start(void);␊ |
51 | void KernelPatcher_start(void)␊ |
52 | {␊ |
53 | ␉register_kernel_patch(patch_cpuid_set_info_all, KERNEL_ANY, CPUID_MODEL_UNKNOWN); ␊ |
54 | ␊ |
55 | ␉register_kernel_patch(patch_commpage_stuff_routine, KERNEL_ANY, CPUID_MODEL_ANY);␊ |
56 | ␉␊ |
57 | ␉register_kernel_patch(patch_lapic_init, KERNEL_ANY, CPUID_MODEL_ANY);␊ |
58 | ␊ |
59 | ␉// NOTE: following is currently 32bit only␊ |
60 | ␉register_kernel_patch(patch_lapic_configure, KERNEL_32, CPUID_MODEL_ANY);␊ |
61 | ␊ |
62 | ␉register_kernel_symbol(KERNEL_ANY, "_panic");␊ |
63 | ␉register_kernel_symbol(KERNEL_ANY, "_cpuid_set_info");␊ |
64 | ␉register_kernel_symbol(KERNEL_ANY, "_pmCPUExitHaltToOff");␊ |
65 | ␉register_kernel_symbol(KERNEL_ANY, "_lapic_init");␊ |
66 | ␉register_kernel_symbol(KERNEL_ANY, "_commpage_stuff_routine");␊ |
67 | ␊ |
68 | ␉// lapic_configure symbols␊ |
69 | ␉register_kernel_symbol(KERNEL_ANY, "_lapic_configure");␊ |
70 | ␉register_kernel_symbol(KERNEL_ANY, "_lapic_start");␊ |
71 | ␉register_kernel_symbol(KERNEL_ANY, "_lapic_interrupt_base");␊ |
72 | ␊ |
73 | ␉// lapic_interrup symbols␊ |
74 | ␉//register_kernel_patch(patch_lapic_interrupt, KERNEL_ANY, CPUID_MODEL_ANY);␊ |
75 | ␉//register_kernel_symbol(KERNEL_ANY, "_lapic_interrupt");␊ |
76 | ␊ |
77 | ␉// TODO: register needed symbols␊ |
78 | ␊ |
79 | ␉register_hook_callback("ExecKernel", &patch_kernel); ␊ |
80 | ␉␊ |
81 | ␉replace_system_function("_getKernelCachePath", &kernel_patcher_ignore_cache);␊ |
82 | }␊ |
83 | ␊ |
84 | /*␊ |
85 | * Register a kernel patch␊ |
86 | */␊ |
87 | static void register_kernel_patch(void* patch, int arch, int cpus)␊ |
88 | {␊ |
89 | ␉// TODO: only insert valid patches based on current cpuid and architecture␊ |
90 | ␉// AKA, don't at 64bit patches if it's a 32bit only machine␊ |
91 | ␉patchRoutine_t* entry;␊ |
92 | ␉␊ |
93 | ␉// Check to ensure that the patch is valid on this machine␊ |
94 | ␉// If it is not, exit early form this function␊ |
95 | ␉if(cpus != get_env(envModel))␊ |
96 | ␉{␊ |
97 | ␉␉if(cpus != CPUID_MODEL_ANY)␊ |
98 | ␉␉{␊ |
99 | ␉␉␉if(cpus == CPUID_MODEL_UNKNOWN)␊ |
100 | ␉␉␉{␊ |
101 | ␉␉␉␉switch(get_env(envModel))␊ |
102 | ␉␉␉␉{␊ |
103 | ␉␉␉␉␉case 13:␊ |
104 | ␉␉␉␉␉case CPUID_MODEL_YONAH:␊ |
105 | ␉␉␉␉␉case CPUID_MODEL_MEROM:␊ |
106 | ␉␉␉␉␉case CPUID_MODEL_PENRYN:␊ |
107 | ␉␉␉␉␉case CPUID_MODEL_NEHALEM:␊ |
108 | ␉␉␉␉␉case CPUID_MODEL_FIELDS:␊ |
109 | ␉␉␉␉␉case CPUID_MODEL_DALES:␊ |
110 | ␉␉␉␉␉case CPUID_MODEL_NEHALEM_EX:␊ |
111 | ␉␉␉␉␉␉// Known cpu's we don't want to add the patch␊ |
112 | ␉␉␉␉␉␉return;␊ |
113 | ␉␉␉␉␉␉break;␊ |
114 | ␊ |
115 | ␉␉␉␉␉default:␊ |
116 | ␉␉␉␉␉␉// CPU not in supported list, so we are going to add␊ |
117 | ␉␉␉␉␉␉// The patch will be applied␊ |
118 | ␉␉␉␉␉␉break;␊ |
119 | ␉␉␉␉}␊ |
120 | ␉␉␉}␊ |
121 | ␉␉␉else␊ |
122 | ␉␉␉{␊ |
123 | ␉␉␉␉// Invalid cpuid for current cpu. Ignoring patch␊ |
124 | ␉␉␉␉return;␊ |
125 | ␉␉␉}␊ |
126 | ␉␉}␊ |
127 | ␉}␊ |
128 | ␉␉␊ |
129 | ␉if(patches == NULL)␊ |
130 | ␉{␊ |
131 | ␉␉patches = entry = malloc(sizeof(patchRoutine_t));␊ |
132 | ␉␉␊ |
133 | ␉␉if (!entry || !patches) return;␊ |
134 | ␉}␊ |
135 | ␉else␊ |
136 | ␉{␊ |
137 | ␉␉entry = patches;␊ |
138 | ␉␉while(entry->next)␊ |
139 | ␉␉{␊ |
140 | ␉␉␉entry = entry->next;␊ |
141 | ␉␉}␊ |
142 | ␉␉␊ |
143 | ␉␉entry->next = malloc(sizeof(patchRoutine_t));␊ |
144 | ␉␉␊ |
145 | ␉␉if (!entry) return;␊ |
146 | ␉␉␊ |
147 | ␉␉entry = entry->next;␊ |
148 | ␉}␊ |
149 | ␉␊ |
150 | ␉entry->next = NULL;␊ |
151 | ␉entry->patchRoutine = patch;␊ |
152 | ␉entry->validArchs = arch;␊ |
153 | ␉entry->validCpu = cpus;␊ |
154 | }␊ |
155 | ␊ |
156 | static void register_kernel_symbol(int kernelType, const char* name)␊ |
157 | {␊ |
158 | ␉if(kernelSymbols == NULL)␊ |
159 | ␉{␊ |
160 | ␉␉kernelSymbols = malloc(sizeof(kernSymbols_t));␊ |
161 | ␉␉if (!kernelSymbols) return;␊ |
162 | ␉␉kernelSymbols->next = NULL;␊ |
163 | ␉␉kernelSymbols->symbol = (char*)name;␊ |
164 | ␉␉kernelSymbols->addr = 0;␊ |
165 | ␉}␊ |
166 | ␉else␊ |
167 | ␉{␊ |
168 | ␉␉kernSymbols_t *symbol = kernelSymbols;␊ |
169 | ␉␉while(symbol->next != NULL)␊ |
170 | ␉␉{␊ |
171 | ␉␉␉symbol = symbol->next;␊ |
172 | ␉␉}␊ |
173 | ␉␉␊ |
174 | ␉␉symbol->next = malloc(sizeof(kernSymbols_t));␊ |
175 | ␉␉if (!symbol->next) return;␊ |
176 | ␊ |
177 | ␉␉symbol = symbol->next;␊ |
178 | ␊ |
179 | ␉␉symbol->next = NULL;␊ |
180 | ␉␉symbol->symbol = (char*)name;␊ |
181 | ␉␉symbol->addr = 0;␊ |
182 | ␉}␊ |
183 | }␊ |
184 | ␊ |
185 | static kernSymbols_t* lookup_kernel_symbol(const char* name)␊ |
186 | {␊ |
187 | ␉kernSymbols_t *symbol = kernelSymbols;␊ |
188 | ␊ |
189 | ␉while(symbol && strcmp(symbol->symbol, name) !=0)␊ |
190 | ␉{␊ |
191 | ␉␉symbol = symbol->next;␊ |
192 | ␉}␊ |
193 | ␉␊ |
194 | ␉if(!symbol)␊ |
195 | ␉{␊ |
196 | ␉␉return NULL;␊ |
197 | ␉}␊ |
198 | ␉else␊ |
199 | ␉{␊ |
200 | ␉␉return symbol;␊ |
201 | ␉}␊ |
202 | }␊ |
203 | ␊ |
204 | void patch_kernel(void* kernelData, void* arg2, void* arg3, void *arg4, void* arg5, void* arg6)␊ |
205 | {␊ |
206 | ␉patchRoutine_t* entry = patches;␊ |
207 | ␊ |
208 | ␉int arch = determineKernelArchitecture(kernelData);␊ |
209 | ␊ |
210 | ␉locate_symbols(kernelData);␊ |
211 | ␉␊ |
212 | ␉if(patches != NULL)␊ |
213 | ␉{␊ |
214 | ␉␉while(entry)␊ |
215 | ␉␉{␊ |
216 | ␉␉␉if(entry->validArchs == KERNEL_ANY || arch == entry->validArchs)␊ |
217 | ␉␉␉{␊ |
218 | ␉␉␉␉if(entry->patchRoutine) entry->patchRoutine(kernelData);␊ |
219 | ␉␉␉}␊ |
220 | ␉␉␉entry = entry->next;␊ |
221 | ␉␉}␊ |
222 | ␉}␊ |
223 | }␊ |
224 | ␊ |
225 | static int determineKernelArchitecture(void* kernelData)␊ |
226 | {␉␊ |
227 | ␉if(((struct mach_header*)kernelData)->magic == MH_MAGIC)␊ |
228 | ␉{␊ |
229 | ␉␉return KERNEL_32;␊ |
230 | ␉}␊ |
231 | ␊ |
232 | ␉if(((struct mach_header*)kernelData)->magic == MH_MAGIC_64)␊ |
233 | ␉{␊ |
234 | ␉␉return KERNEL_64;␊ |
235 | ␉}␊ |
236 | ␉else␊ |
237 | ␉{␊ |
238 | ␉␉return KERNEL_ERR;␊ |
239 | ␉}␊ |
240 | }␊ |
241 | ␊ |
242 | static unsigned int parse_mach_64(char *module, void* binary, long long(*symbol_handler)(char*, char*, long long, char))␉// TODO: add param to specify valid archs␊ |
243 | {␉␊ |
244 | ␉char is64 = false;␊ |
245 | ␉unsigned int module_start = 0xFFFFFFFF;␊ |
246 | ␉EFI_STATUS bind_status = EFI_SUCCESS;␊ |
247 | ␊ |
248 | ␉// TODO convert all of the structs to a union␉␊ |
249 | ␉struct dyld_info_command* dyldInfoCommand = NULL;␉␊ |
250 | ␉struct symtab_command* symtabCommand = NULL;␉␊ |
251 | ␉␊ |
252 | ␉{␊ |
253 | ␉␉struct segment_command *segCommand = NULL;␊ |
254 | ␉␉struct segment_command_64 *segCommand64 = NULL;␊ |
255 | ␉␉struct load_command *loadCommand = NULL;␊ |
256 | ␉␉UInt32 binaryIndex = 0;␊ |
257 | ␉␉UInt16 cmd = 0;␊ |
258 | ␉␉␊ |
259 | ␉␉// Parse through the load commands␊ |
260 | ␉␉if(((struct mach_header*)binary)->magic == MH_MAGIC)␊ |
261 | ␉␉{␊ |
262 | ␉␉␉is64 = false;␊ |
263 | ␉␉␉binaryIndex += sizeof(struct mach_header);␊ |
264 | ␉␉}␊ |
265 | ␉␉else if(((struct mach_header_64*)binary)->magic == MH_MAGIC_64)␊ |
266 | ␉␉{␊ |
267 | ␉␉␉// NOTE: modules cannot be 64bit...␊ |
268 | ␉␉␉is64 = true;␊ |
269 | ␉␉␉binaryIndex += sizeof(struct mach_header_64);␊ |
270 | ␉␉}␊ |
271 | ␉␉else␊ |
272 | ␉␉{␊ |
273 | ␉␉␉printf("Modules: Invalid mach magic\n");␊ |
274 | ␉␉␉getc();␊ |
275 | ␉␉␉return 0xFFFFFFFF;␊ |
276 | ␉␉}␊ |
277 | ␉␉␊ |
278 | ␉␉␊ |
279 | ␉␉␊ |
280 | ␉␉/*if(((struct mach_header*)binary)->filetype != MH_DYLIB)␊ |
281 | ␉␉ {␊ |
282 | ␉␉ printf("Module is not a dylib. Unable to load.\n");␊ |
283 | ␉␉ getc();␊ |
284 | ␉␉ return NULL; // Module is in the incorrect format␊ |
285 | ␉␉ }*/␊ |
286 | ␉␉␊ |
287 | ␉␉while(cmd < ((struct mach_header*)binary)->ncmds)␊ |
288 | ␉␉{␊ |
289 | ␉␉␉cmd++;␊ |
290 | ␉␉␉␊ |
291 | ␉␉␉loadCommand = binary + binaryIndex;␊ |
292 | ␉␉␉UInt32 cmdSize = loadCommand->cmdsize;␊ |
293 | ␉␉␉␊ |
294 | ␉␉␉␊ |
295 | ␉␉␉switch ((loadCommand->cmd & 0x7FFFFFFF))␊ |
296 | ␉␉␉{␊ |
297 | ␉␉␉␉case LC_SYMTAB:␊ |
298 | ␉␉␉␉␉symtabCommand = binary + binaryIndex;␊ |
299 | ␉␉␉␉␉break;␊ |
300 | ␉␉␉␉␉␊ |
301 | ␉␉␉␉case LC_SEGMENT: // 32bit macho␊ |
302 | ␉␉␉␉{␊ |
303 | ␉␉␉␉␉segCommand = binary + binaryIndex;␊ |
304 | ␉␉␉␉␉␊ |
305 | ␉␉␉␉␉//printf("Segment name is %s\n", segCommand->segname);␊ |
306 | ␉␉␉␉␉␊ |
307 | ␉␉␉␉␉if(strncmp("__TEXT", segCommand->segname, sizeof("__TEXT")) == 0)␊ |
308 | ␉␉␉␉␉{␊ |
309 | ␉␉␉␉␉␉UInt32 sectionIndex;␊ |
310 | ␉␉␉␉␉␉␊ |
311 | #if DEBUG_KERNEL_PATCHER␊ |
312 | ␉␉␉␉␉␉unsigned long fileaddr;␊ |
313 | ␉␉␉␉␉␉long filesize;␉␉␉␉␉␊ |
314 | ␉␉␉␉␉␉vmaddr = (segCommand->vmaddr & 0x3fffffff);␊ |
315 | ␉␉␉␉␉␉vmsize = segCommand->vmsize;␉ ␊ |
316 | ␉␉␉␉␉␉fileaddr = ((unsigned long)(binary + binaryIndex) + segCommand->fileoff);␊ |
317 | ␉␉␉␉␉␉filesize = segCommand->filesize;␊ |
318 | ␉␉␉␉␉␉␊ |
319 | ␉␉␉␉␉␉printf("segname: %s, vmaddr: %x, vmsize: %x, fileoff: %x, filesize: %x, nsects: %d, flags: %x.\n",␊ |
320 | ␉␉␉␉␉␉␉ segCommand->segname, (unsigned)vmaddr, (unsigned)vmsize, (unsigned)fileaddr, (unsigned)filesize,␊ |
321 | ␉␉␉␉␉␉␉ (unsigned) segCommand->nsects, (unsigned)segCommand->flags);␊ |
322 | #if DEBUG_KERNEL_PATCHER==2␊ |
323 | ␉␉␉␉␉␉␊ |
324 | ␉␉␉␉␉␉getc();␊ |
325 | #endif␊ |
326 | #endif␊ |
327 | ␉␉␉␉␉␉␊ |
328 | ␉␉␉␉␉␉sectionIndex = sizeof(struct segment_command);␊ |
329 | ␉␉␉␉␉␉␊ |
330 | ␉␉␉␉␉␉struct section *sect;␊ |
331 | ␉␉␉␉␉␉␊ |
332 | ␉␉␉␉␉␉while(sectionIndex < segCommand->cmdsize)␊ |
333 | ␉␉␉␉␉␉{␊ |
334 | ␉␉␉␉␉␉␉sect = binary + binaryIndex + sectionIndex;␊ |
335 | ␉␉␉␉␉␉␉␊ |
336 | ␉␉␉␉␉␉␉sectionIndex += sizeof(struct section);␊ |
337 | ␉␉␉␉␉␉␉␊ |
338 | ␉␉␉␉␉␉␉␊ |
339 | ␉␉␉␉␉␉␉if(strncmp("__text", sect->sectname,sizeof("__text")) == 0)␊ |
340 | ␉␉␉␉␉␉␉{␊ |
341 | ␉␉␉␉␉␉␉␉// __TEXT,__text found, save the offset and address for when looking for the calls.␊ |
342 | ␉␉␉␉␉␉␉␉textSection = sect->offset;␊ |
343 | ␉␉␉␉␉␉␉␉textAddress = sect->addr;␊ |
344 | ␉␉␉␉␉␉␉␉break;␊ |
345 | ␉␉␉␉␉␉␉}␉␉␉␉␉␊ |
346 | ␉␉␉␉␉␉}␊ |
347 | ␉␉␉␉␉}␊ |
348 | ␉␉␉␉␉break;␊ |
349 | ␉␉␉␉}␉␊ |
350 | ␉␉␉␉case LC_SEGMENT_64:␉// 64bit macho's␊ |
351 | ␉␉␉␉{␊ |
352 | ␉␉␉␉␉segCommand64 = binary + binaryIndex;␊ |
353 | ␉␉␉␉␉␊ |
354 | ␉␉␉␉␉//printf("Segment name is %s\n", segCommand->segname);␊ |
355 | ␉␉␉␉␉␊ |
356 | ␉␉␉␉␉if(strncmp("__TEXT", segCommand64->segname, sizeof("__TEXT")) == 0)␊ |
357 | ␉␉␉␉␉{␊ |
358 | ␉␉␉␉␉␉UInt32 sectionIndex;␊ |
359 | ␉␉␉␉␉␉␊ |
360 | #if DEBUG_KERNEL_PATCHER␉␉␉␉␉␉␊ |
361 | ␉␉␉␉␉␉unsigned long fileaddr;␊ |
362 | ␉␉␉␉␉␉long filesize;␉␉␉␉␉␊ |
363 | ␉␉␉␉␉␉vmaddr = (segCommand64->vmaddr & 0x3fffffff);␊ |
364 | ␉␉␉␉␉␉vmsize = segCommand64->vmsize;␉ ␊ |
365 | ␉␉␉␉␉␉fileaddr = ((unsigned long)(binary + binaryIndex) + segCommand64->fileoff);␊ |
366 | ␉␉␉␉␉␉filesize = segCommand64->filesize;␊ |
367 | ␉␉␉␉␉␉␊ |
368 | ␉␉␉␉␉␉printf("segname: %s, vmaddr: %x, vmsize: %x, fileoff: %x, filesize: %x, nsects: %d, flags: %x.\n",␊ |
369 | ␉␉␉␉␉␉␉ segCommand64->segname, (unsigned)vmaddr, (unsigned)vmsize, (unsigned)fileaddr, (unsigned)filesize,␊ |
370 | ␉␉␉␉␉␉␉ (unsigned) segCommand64->nsects, (unsigned)segCommand64->flags);␊ |
371 | #if DEBUG_KERNEL_PATCHER==2␊ |
372 | ␉␉␉␉␉␉␊ |
373 | ␉␉␉␉␉␉getc();␊ |
374 | #endif␊ |
375 | #endif␊ |
376 | ␉␉␉␉␉␉␊ |
377 | ␉␉␉␉␉␉sectionIndex = sizeof(struct segment_command_64);␊ |
378 | ␉␉␉␉␉␉␊ |
379 | ␉␉␉␉␉␉struct section_64 *sect;␊ |
380 | ␉␉␉␉␉␉␊ |
381 | ␉␉␉␉␉␉while(sectionIndex < segCommand64->cmdsize)␊ |
382 | ␉␉␉␉␉␉{␊ |
383 | ␉␉␉␉␉␉␉sect = binary + binaryIndex + sectionIndex;␊ |
384 | ␉␉␉␉␉␉␉␊ |
385 | ␉␉␉␉␉␉␉sectionIndex += sizeof(struct section_64);␊ |
386 | ␉␉␉␉␉␉␉␊ |
387 | ␉␉␉␉␉␉␉␊ |
388 | ␉␉␉␉␉␉␉if(strncmp("__text", sect->sectname, sizeof("__text")) == 0)␊ |
389 | ␉␉␉␉␉␉␉{␊ |
390 | ␉␉␉␉␉␉␉␉// __TEXT,__text found, save the offset and address for when looking for the calls.␊ |
391 | ␉␉␉␉␉␉␉␉textSection = sect->offset;␊ |
392 | ␉␉␉␉␉␉␉␉textAddress = sect->addr;␊ |
393 | ␉␉␉␉␉␉␉␉␊ |
394 | ␉␉␉␉␉␉␉␉break;␊ |
395 | ␉␉␉␉␉␉␉}␉␉␉␉␉␊ |
396 | ␉␉␉␉␉␉}␊ |
397 | ␉␉␉␉␉}␊ |
398 | ␉␉␉␉␉␊ |
399 | ␉␉␉␉␉break;␊ |
400 | ␉␉␉␉}␊ |
401 | ␉␉␉␉case LC_DYSYMTAB:␊ |
402 | ␉␉␉␉␉break;␊ |
403 | ␉␉␉␉␉␊ |
404 | ␉␉␉␉case LC_LOAD_DYLIB:␊ |
405 | ␉␉␉␉case LC_LOAD_WEAK_DYLIB ^ LC_REQ_DYLD:␊ |
406 | break;␊ |
407 | ␊ |
408 | ␉␉␉␉case LC_ID_DYLIB:␊ |
409 | break;␊ |
410 | ␊ |
411 | ␉␉␉␉␉␊ |
412 | ␉␉␉␉case LC_DYLD_INFO:␊ |
413 | ␉␉␉␉␉// Bind and rebase info is stored here␊ |
414 | ␉␉␉␉␉dyldInfoCommand = binary + binaryIndex;␊ |
415 | ␉␉␉␉␉break;␊ |
416 | ␉␉␉␉␉␊ |
417 | ␉␉␉␉case LC_UUID:␊ |
418 | ␉␉␉␉␉break;␊ |
419 | ␉␉␉␉␉␊ |
420 | ␉␉␉␉case LC_UNIXTHREAD:␊ |
421 | ␉␉␉␉␉break;␊ |
422 | ␉␉␉␉␉␊ |
423 | ␉␉␉␉default:␊ |
424 | ␉␉␉␉␉DBG("Unhandled loadcommand 0x%X\n", loadCommand->cmd & 0x7FFFFFFF);␊ |
425 | ␉␉␉␉␉break;␊ |
426 | ␉␉␉␉␉␊ |
427 | ␉␉␉}␊ |
428 | ␉␉␉␊ |
429 | ␉␉␉binaryIndex += cmdSize;␊ |
430 | ␉␉}␊ |
431 | ␉␉//if(!moduleName) return NULL;␊ |
432 | ␉}␉␉␊ |
433 | ␉␊ |
434 | ␉// bind_macho uses the symbols.␊ |
435 | ␉module_start = handle_symtable_64(module, (UInt32)binary, symtabCommand, symbol_handler, is64);␊ |
436 | ␊ |
437 | ␉// Rebase the module before binding it.␊ |
438 | ␉if(dyldInfoCommand && dyldInfoCommand->rebase_off)␊ |
439 | ␉{␊ |
440 | ␉␉rebase_macho(binary, (char*)dyldInfoCommand->rebase_off, dyldInfoCommand->rebase_size);␊ |
441 | ␉}␊ |
442 | ␉␊ |
443 | ␉if(dyldInfoCommand && dyldInfoCommand->bind_off)␊ |
444 | ␉{␊ |
445 | ␉␉bind_status = bind_macho(module, binary, (char*)dyldInfoCommand->bind_off, dyldInfoCommand->bind_size);␊ |
446 | ␉}␊ |
447 | ␉␊ |
448 | ␉if(dyldInfoCommand && dyldInfoCommand->weak_bind_off && (bind_status == EFI_SUCCESS))␊ |
449 | ␉{␊ |
450 | ␉␉// NOTE: this currently should never happen.␊ |
451 | ␉␉bind_status = bind_macho(module, binary, (char*)dyldInfoCommand->weak_bind_off, dyldInfoCommand->weak_bind_size);␊ |
452 | ␉}␊ |
453 | ␉␊ |
454 | ␉if(dyldInfoCommand && dyldInfoCommand->lazy_bind_off && (bind_status == EFI_SUCCESS))␊ |
455 | ␉{␊ |
456 | ␉␉// NOTE: we are binding the lazy pointers as a module is laoded,␊ |
457 | ␉␉// This should be changed to bind when a symbol is referened at runtime instead.␊ |
458 | ␉␉bind_status = bind_macho(module, binary, (char*)dyldInfoCommand->lazy_bind_off, dyldInfoCommand->lazy_bind_size);␊ |
459 | ␉}␊ |
460 | ␉␊ |
461 | if (bind_status != EFI_SUCCESS) {␊ |
462 | module_start = 0xFFFFFFFF;␊ |
463 | }␊ |
464 | ␊ |
465 | ␉return module_start;␊ |
466 | ␉␊ |
467 | }␊ |
468 | ␊ |
469 | static unsigned int handle_symtable_64(char *module, UInt32 base, struct symtab_command* symtabCommand, long long(*symbol_handler)(char*, char*, long long, char), char is64)␊ |
470 | {␉␉␊ |
471 | ␉unsigned int module_start = 0xFFFFFFFF;␊ |
472 | ␉␊ |
473 | ␉UInt32 symbolIndex = 0;␊ |
474 | if (!symtabCommand) {␊ |
475 | return 0xFFFFFFFF;␊ |
476 | }␊ |
477 | ␉char* symbolString = base + (char*)symtabCommand->stroff;␊ |
478 | ␉if(!is64)␊ |
479 | ␉{␊ |
480 | ␉␉struct nlist* symbolEntry = (void*)base + symtabCommand->symoff;␊ |
481 | ␉␉while(symbolIndex < symtabCommand->nsyms)␊ |
482 | ␉␉{␉␉␉␉␉␉␊ |
483 | ␉␉␉if(symbolEntry->n_value)␊ |
484 | ␉␉␉{␉␉␉␉␊ |
485 | ␉␉␉␉if(strstr(symbolString + symbolEntry->n_un.n_strx, "module_start") || (strncmp(symbolString + symbolEntry->n_un.n_strx, "start", sizeof("start")) == 0))␊ |
486 | ␉␉␉␉{␊ |
487 | ␉␉␉␉␉module_start = base + symbolEntry->n_value;␊ |
488 | ␉␉␉␉␉DBG("n_value %x module_start %x\n", (unsigned)symbolEntry->n_value, (unsigned)module_start);␊ |
489 | ␉␉␉␉}␊ |
490 | ␉␉␉␉else␊ |
491 | ␉␉␉␉{␊ |
492 | ␉␉␉␉␉symbol_handler(module, symbolString + symbolEntry->n_un.n_strx, (long long)base + symbolEntry->n_value, is64);␊ |
493 | ␊ |
494 | ␉␉␉␉␉␊ |
495 | ␉␉␉␉}␊ |
496 | #if DEBUG_KERNEL_PATCHER␊ |
497 | ␉␉␉␉bool isTexT = (((unsigned)symbolEntry->n_value > (unsigned)vmaddr) && ((unsigned)(vmaddr + vmsize) > (unsigned)symbolEntry->n_value ));␊ |
498 | ␉␉␉␉printf("%s %s\n", isTexT ? "__TEXT :" : "__DATA(OR ANY) :", symbolString + symbolEntry->n_un.n_strx);␊ |
499 | #if DEBUG_KERNEL_PATCHER==2␉␊ |
500 | ␉␉␉␉␊ |
501 | ␉␉␉␉if(strcmp(symbolString + symbolEntry->n_un.n_strx, "_BootHelp_txt") == 0)␊ |
502 | ␉␉␉␉{␊ |
503 | ␉␉␉␉␉long long addr = (long long)base + symbolEntry->n_value;␊ |
504 | ␉␉␉␉␉unsigned char *BootHelp = NULL;␊ |
505 | ␉␉␉␉␉BootHelp = (unsigned char*)(UInt32)addr;␉␉␉␉␉␊ |
506 | ␉␉␉␉␉printf("method 1: __DATA : BootHelp_txt[0] %x\n", BootHelp[0]);␊ |
507 | ␉␉␉␉␉␊ |
508 | ␉␉␉␉␉long long addr2 = symbolEntry->n_value;␊ |
509 | ␉␉␉␉␉unsigned char *BootHelp2 = NULL;␊ |
510 | ␉␉␉␉␉BootHelp2 = (unsigned char*)(UInt32)addr2;␉␉␉␉␉␊ |
511 | ␉␉␉␉␉printf("method 2: __DATA : BootHelp_txt[0] %x\n", BootHelp2[0]);␊ |
512 | ␉␉␉␉}␊ |
513 | #endif␊ |
514 | #endif␊ |
515 | ␉␉␉␉␊ |
516 | ␉␉␉}␊ |
517 | ␉␉␉␊ |
518 | ␉␉␉symbolEntry++;␊ |
519 | ␉␉␉symbolIndex++;␉// TODO remove␊ |
520 | ␉␉}␊ |
521 | ␉}␊ |
522 | ␉else␊ |
523 | ␉{␊ |
524 | ␉␉struct nlist_64* symbolEntry = (void*)base + symtabCommand->symoff;␊ |
525 | ␉␉// NOTE First entry is *not* correct, but we can ignore it (i'm getting radar:// right now)␉␊ |
526 | ␉␉while(symbolIndex < symtabCommand->nsyms)␊ |
527 | ␉␉{␉␊ |
528 | ␉␉␉␊ |
529 | ␉␉␉if(strstr(symbolString + symbolEntry->n_un.n_strx, "module_start") || (strncmp(symbolString + symbolEntry->n_un.n_strx, "start",sizeof("start")) == 0))␊ |
530 | ␉␉␉{␊ |
531 | ␉␉␉␉module_start = (unsigned int)(base + symbolEntry->n_value);␊ |
532 | ␉␉␉}␊ |
533 | ␉␉␉else␊ |
534 | ␉␉␉{␊ |
535 | ␉␉␉␉symbol_handler(module, symbolString + symbolEntry->n_un.n_strx, (long long)base + symbolEntry->n_value, is64);␊ |
536 | ␉␉␉}␊ |
537 | ␉␉␉␊ |
538 | ␉␉␉symbolEntry++;␊ |
539 | ␉␉␉symbolIndex++;␉// TODO remove␊ |
540 | ␉␉}␊ |
541 | ␉}␉␊ |
542 | ␉return module_start;␊ |
543 | ␉␊ |
544 | }␊ |
545 | ␊ |
546 | /**␊ |
547 | **␉␉This functions located the requested symbols in the mach-o file.␊ |
548 | **␉␉␉as well as determines the start of the __TEXT segment and __TEXT,__text sections␊ |
549 | **/␊ |
550 | static int locate_symbols(void* kernelData)␊ |
551 | {␊ |
552 | ␉char is64 = 1;␊ |
553 | ␉parse_mach_64("VirtualXnuSyms",kernelData, symbol_handler);␊ |
554 | ␉//handle_symtable((UInt32)kernelData, symtableData, &symbol_handler, determineKernelArchitecture(kernelData) == KERNEL_64);␊ |
555 | ␉return 1 << is64;␊ |
556 | }␊ |
557 | ␊ |
558 | long long symbol_handler(char* module, char* symbolName, long long addr, char is64)␊ |
559 | {␊ |
560 | ␉// Locate the symbol in the list, if it exists, update it's address␊ |
561 | ␉kernSymbols_t *symbol = lookup_kernel_symbol(symbolName);␊ |
562 | ␉␊ |
563 | ␉if(symbol)␊ |
564 | ␉{␊ |
565 | ␉␉symbol->addr = addr;␊ |
566 | ␉}␊ |
567 | ␉␊ |
568 | ␉return 0xFFFFFFFF; // fixme␊ |
569 | }␊ |
570 | ␊ |
571 | /**␊ |
572 | ** Locate the fisrt instance of _panic inside of _cpuid_set_info, and either remove it␊ |
573 | ** Or replace it so that the cpuid is set to a valid value.␊ |
574 | **/␊ |
575 | static void patch_cpuid_set_info_all(void* kernelData)␊ |
576 | {␊ |
577 | ␉switch(get_env(envModel))␊ |
578 | ␉{␊ |
579 | ␉␉case CPUID_MODEL_ATOM:␊ |
580 | ␉␉␉if(determineKernelArchitecture(kernelData) == KERNEL_32)␊ |
581 | ␉␉␉{␊ |
582 | ␉␉␉␉patch_cpuid_set_info_32(kernelData, CPUFAMILY_INTEL_PENRYN, CPUID_MODEL_PENRYN);␊ |
583 | ␉␉␉}␊ |
584 | ␉␉␉else ␊ |
585 | ␉␉␉{␊ |
586 | ␉␉␉␉patch_cpuid_set_info_64(kernelData, CPUFAMILY_INTEL_PENRYN, CPUID_MODEL_PENRYN);␊ |
587 | ␉␉␉}␊ |
588 | ␊ |
589 | ␉␉␉break;␊ |
590 | ␊ |
591 | ␉␉default:␊ |
592 | ␉␉{␊ |
593 | ␉␉␉// AnV: Extra cpuid fix for spoofing Nehalem CPU for i5/i9␊ |
594 | ␉␉␉switch(get_env(envFamily))␊ |
595 | ␉␉␉{␊ |
596 | ␉␉␉␉case 0x1E: /* Intel i5 */␊ |
597 | ␉␉␉␉case 0x2C: /* Intel i9 */␊ |
598 | ␉␉␉␉␉if(determineKernelArchitecture(kernelData) == KERNEL_32)␊ |
599 | ␉␉␉␉␉{␊ |
600 | ␉␉␉␉␉␉patch_cpuid_set_info_32(kernelData, CPUFAMILY_INTEL_NEHALEM, CPUID_MODEL_NEHALEM);␊ |
601 | ␉␉␉␉␉}␊ |
602 | ␉␉␉␉␉else ␊ |
603 | ␉␉␉␉␉{␊ |
604 | ␉␉␉␉␉␉patch_cpuid_set_info_64(kernelData, CPUFAMILY_INTEL_NEHALEM, CPUID_MODEL_NEHALEM);␊ |
605 | ␉␉␉␉␉}␊ |
606 | ␉␉␉␉␉␊ |
607 | ␉␉␉␉␉break;␊ |
608 | ␉␉␉␉␊ |
609 | ␉␉␉␉default:␊ |
610 | ␉␉␉␉␉if(determineKernelArchitecture(kernelData) == KERNEL_32)␊ |
611 | ␉␉␉␉␉{␊ |
612 | ␉␉␉␉␉␉patch_cpuid_set_info_32(kernelData, 0, 0);␊ |
613 | ␉␉␉␉␉}␊ |
614 | ␉␉␉␉␉else␊ |
615 | ␉␉␉␉␉{␊ |
616 | ␉␉␉␉␉␉patch_cpuid_set_info_64(kernelData, 0, 0);␊ |
617 | ␉␉␉␉␉}␉␉␉␉␉␊ |
618 | ␉␉␉␉␉break;␊ |
619 | ␉␉␉}␊ |
620 | ␉␉␉break;␉␉␉␊ |
621 | ␉␉}␊ |
622 | ␉}␊ |
623 | }␊ |
624 | ␊ |
625 | static void patch_cpuid_set_info_64(void* kernelData, UInt32 impersonateFamily, UInt8 impersonateModel)␊ |
626 | {␊ |
627 | ␉UInt8* bytes = (UInt8*)kernelData;␊ |
628 | ␉␊ |
629 | ␉kernSymbols_t *symbol = lookup_kernel_symbol("_cpuid_set_info");␊ |
630 | ␉␊ |
631 | ␉UInt32 patchLocation = symbol ? symbol->addr - textAddress + textSection: 0; //␉(kernelSymbolAddresses[SYMBOL_CPUID_SET_INFO] - textAddress + textSection);␊ |
632 | ␉patchLocation -= (UInt32)kernelData;␉// Remove offset␊ |
633 | ␊ |
634 | ␉//UInt32 jumpLocation = 0;␊ |
635 | ␊ |
636 | ␉if(symbol == 0 || symbol->addr == 0)␊ |
637 | ␉{␊ |
638 | ␉␉verbose("Unable to locate _cpuid_set_info\n");␊ |
639 | ␉␉return;␊ |
640 | ␉}␊ |
641 | ␉␊ |
642 | ␉symbol = lookup_kernel_symbol("_panic");␊ |
643 | ␉UInt32 panicAddr = symbol ? symbol->addr - textAddress: 0; //kernelSymbolAddresses[SYMBOL_PANIC] - textAddress;␊ |
644 | ␉if(symbol == 0 || symbol->addr == 0)␊ |
645 | ␉{␊ |
646 | ␉␉printf("Unable to locate _panic\n");␊ |
647 | ␉␉return;␊ |
648 | ␉}␊ |
649 | ␉panicAddr -= (UInt32)kernelData;␊ |
650 | ␊ |
651 | ␉//TODO: don't assume it'll always work (Look for *next* function address in symtab and fail once it's been reached)␊ |
652 | ␉while( ␊ |
653 | ␉␉ (bytes[patchLocation -1] != 0xE8) ||␊ |
654 | ␉␉ ( ( (UInt32)(panicAddr - patchLocation - 4) + textSection ) != (UInt32)((bytes[patchLocation + 0] << 0 | ␊ |
655 | ␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉bytes[patchLocation + 1] << 8 | ␊ |
656 | ␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉bytes[patchLocation + 2] << 16 |␊ |
657 | ␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉bytes[patchLocation + 3] << 24)))␊ |
658 | ␉␉ )␊ |
659 | ␉{␊ |
660 | ␉␉patchLocation++;␊ |
661 | ␉}␊ |
662 | ␉patchLocation--;␊ |
663 | ␊ |
664 | ␉// Remove panic just in ca se␊ |
665 | ␉// The panic instruction is exactly 5 bytes long.␊ |
666 | ␉bytes[patchLocation + 0] = 0x90;␊ |
667 | ␉bytes[patchLocation + 1] = 0x90;␊ |
668 | ␉bytes[patchLocation + 2] = 0x90;␊ |
669 | ␉bytes[patchLocation + 3] = 0x90;␊ |
670 | ␉bytes[patchLocation + 4] = 0x90;␊ |
671 | ␊ |
672 | ␉// Check for a 10.2.0+ kernel␊ |
673 | ␉if(bytes[patchLocation - 19] == 0xC7 && bytes[patchLocation - 18] == 0x05)␊ |
674 | ␉{␊ |
675 | ␉␉UInt32 cpuid_cpufamily_addr =␉bytes[patchLocation - 17] << 0 |␊ |
676 | ␉␉␉␉␉␉␉␉␉␉bytes[patchLocation - 16] << 8 |␊ |
677 | ␉␉␉␉␉␉␉␉␉␉bytes[patchLocation - 15] << 16 |␊ |
678 | ␉␉␉␉␉␉␉␉␉␉bytes[patchLocation - 14] << 24;␊ |
679 | ␉␉␊ |
680 | ␉␉// NOTE: may change, determined based on cpuid_info struct␊ |
681 | ␉␉UInt32 cpuid_model_addr = cpuid_cpufamily_addr - 310; ␊ |
682 | ␊ |
683 | ␉␉//ffffff8000228b3b -> 0x00490e8b␊ |
684 | ␉␉//ffffff8000228c28 -> -237 -> 0x490D9E -> -310␊ |
685 | ␊ |
686 | ␉␉// The mov is 10 bytes␊ |
687 | ␉␉/*␊ |
688 | ␉␉ bytes[patchLocation - 19] = 0x90;␉// c7␊ |
689 | ␉␉ bytes[patchLocation - 18] = 0x90;␉// 05␊ |
690 | ␉␉ bytes[patchLocation - 17] = 0x90;␉// family location␊ |
691 | ␉␉ bytes[patchLocation - 16] = 0x90;␉// family location␊ |
692 | ␉␉ bytes[patchLocation - 15] = 0x90;␉// family location␊ |
693 | ␉␉ bytes[patchLocation - 14] = 0x90;␉// family location␊ |
694 | ␉␉ */␊ |
695 | ␉␉bytes[patchLocation - 13] = (impersonateFamily & 0x000000FF) >> 0;␊ |
696 | ␉␉bytes[patchLocation - 12] = (impersonateFamily & 0x0000FF00) >> 8;␊ |
697 | ␉␉bytes[patchLocation - 11] = (impersonateFamily & 0x00FF0000) >> 16;␉␊ |
698 | ␉␉bytes[patchLocation - 10] = (impersonateFamily & 0xFF000000) >> 24;␊ |
699 | ␊ |
700 | ␉␉// The lea (%rip),%rip is 7 bytes␊ |
701 | ␉␉bytes[patchLocation - 9] = 0xC7;␊ |
702 | ␉␉bytes[patchLocation - 8] = 0x05;␊ |
703 | ␉␉bytes[patchLocation - 7] = ((cpuid_model_addr -10) & 0x000000FF) >> 0;␉// NOTE: this opcode is relative in 64bit mode, subtract offset␊ |
704 | ␉␉bytes[patchLocation - 6] = ((cpuid_model_addr -10) & 0x0000FF00) >> 8;␉␊ |
705 | ␉␉bytes[patchLocation - 5] = ((cpuid_model_addr -10) & 0x00FF0000) >> 16;␊ |
706 | ␉␉bytes[patchLocation - 4] = ((cpuid_model_addr -10) & 0xFF000000) >> 24;␊ |
707 | ␉␉bytes[patchLocation - 3] = impersonateModel;␉// cpuid_model␊ |
708 | ␊ |
709 | ␉␉// The xor eax eax is 2 bytes␊ |
710 | ␉␉bytes[patchLocation - 2] = 0x01;␉// cpuid_extmodel␊ |
711 | ␉␉bytes[patchLocation - 1] = 0x00;␉// cpuid_extfamily␊ |
712 | ␉␉␊ |
713 | ␉␉// The panic instruction is exactly 5 bytes long.␊ |
714 | ␉␉bytes[patchLocation - 0] = 0x02;␉// cpuid_stepping␊ |
715 | ␉␉/*bytes[patchLocation + 1] = 0x90;␊ |
716 | ␉␉bytes[patchLocation + 2] = 0x90;␊ |
717 | ␉␉bytes[patchLocation + 3] = 0x90;␊ |
718 | ␉␉bytes[patchLocation + 4] = 0x90;␊ |
719 | ␉␉*/␊ |
720 | ␉␉␊ |
721 | ␉␉// Panic call has been removed.␊ |
722 | ␉␉// Override the CPUID now. This requires ~ 10 bytes on 10.0.0 kernels␊ |
723 | ␉␉// On 10.2.0+ kernels, this requires ~16 bytes␊ |
724 | ␉␉␊ |
725 | ␉␉// Total: 24 bytes␊ |
726 | ␉␉printf("Running on a 10.2.0+ kernel\n");␊ |
727 | ␉␉getc();␊ |
728 | ␉}␊ |
729 | ␉else {␊ |
730 | ␉␉printf("Running on a 10.0.0 kernel, patch unsupported\n");␊ |
731 | ␉␉getc();␊ |
732 | ␉}␊ |
733 | }␊ |
734 | ␊ |
735 | static void patch_cpuid_set_info_32(void* kernelData, UInt32 impersonateFamily, UInt8 impersonateModel)␊ |
736 | {␉␊ |
737 | ␉UInt8* bytes = (UInt8*)kernelData;␊ |
738 | ␉␊ |
739 | ␉kernSymbols_t *symbol = lookup_kernel_symbol("_cpuid_set_info");␊ |
740 | ␊ |
741 | ␉UInt32 patchLocation = symbol ? symbol->addr - textAddress + textSection: 0; //␉(kernelSymbolAddresses[SYMBOL_CPUID_SET_INFO] - textAddress + textSection);␊ |
742 | ␉patchLocation -= (UInt32)kernelData;␉// Remove offset␊ |
743 | ␊ |
744 | ␉UInt32 jumpLocation = 0;␉␊ |
745 | ␊ |
746 | ␉if(symbol == 0 || symbol->addr == 0)␊ |
747 | ␉{␊ |
748 | ␉␉verbose("Unable to locate _cpuid_set_info\n");␊ |
749 | ␉␉return;␊ |
750 | ␉}␊ |
751 | ␊ |
752 | ␉symbol = lookup_kernel_symbol("_panic");␊ |
753 | ␉UInt32 panicAddr = symbol ? symbol->addr - textAddress: 0; //kernelSymbolAddresses[SYMBOL_PANIC] - textAddress;␊ |
754 | ␉if(symbol == 0 || symbol->addr == 0)␊ |
755 | ␉{␊ |
756 | ␉␉printf("Unable to locate _panic\n");␊ |
757 | ␉␉return;␊ |
758 | ␉}␊ |
759 | ␉panicAddr -= (UInt32)kernelData;␊ |
760 | ␊ |
761 | ␉//TODO: don't assume it'll always work (Look for *next* function address in symtab and fail once it's been reached)␊ |
762 | ␉while( ␊ |
763 | ␉␉ (bytes[patchLocation -1] != 0xE8) ||␊ |
764 | ␉␉ ( ( (UInt32)(panicAddr - patchLocation - 4) + textSection ) != (UInt32)((bytes[patchLocation + 0] << 0 | ␊ |
765 | ␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉bytes[patchLocation + 1] << 8 | ␊ |
766 | ␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉bytes[patchLocation + 2] << 16 |␊ |
767 | ␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉bytes[patchLocation + 3] << 24)))␊ |
768 | ␉␉ )␊ |
769 | ␉{␊ |
770 | ␉␉patchLocation++;␊ |
771 | ␉}␊ |
772 | ␉patchLocation--;␊ |
773 | ␉␊ |
774 | ␉// Remove panic call, just in case the following patch routines fail␊ |
775 | ␉bytes[patchLocation + 0] = 0x90;␊ |
776 | ␉bytes[patchLocation + 1] = 0x90;␊ |
777 | ␉bytes[patchLocation + 2] = 0x90;␊ |
778 | ␉bytes[patchLocation + 3] = 0x90;␊ |
779 | ␉bytes[patchLocation + 4] = 0x90;␊ |
780 | ␊ |
781 | ␉// Locate the jump call, so that 10 bytes can be reclamed.␊ |
782 | ␉// NOTE: This will *NOT* be located on pre 10.6.2 kernels␊ |
783 | ␉jumpLocation = patchLocation - 15;␊ |
784 | ␉while((bytes[jumpLocation - 1] != 0x77 ||␊ |
785 | ␉␉ bytes[jumpLocation] != (patchLocation - jumpLocation - -8)) &&␊ |
786 | ␉␉ (patchLocation - jumpLocation) < 0xF0)␊ |
787 | ␉{␊ |
788 | ␉␉jumpLocation--;␊ |
789 | ␉}␊ |
790 | ␉␊ |
791 | ␉// If found... AND we want to impersonate a specific cpumodel / family...␊ |
792 | ␉if(impersonateFamily &&␊ |
793 | ␉ impersonateModel &&␊ |
794 | ␉ ((patchLocation - jumpLocation) < 0xF0))␊ |
795 | ␉{␊ |
796 | ␉␉␊ |
797 | ␉␉bytes[jumpLocation] -= 10;␉␉// sizeof(movl␉$0x6b5a4cd2,0x00872eb4) = 10bytes␊ |
798 | ␉␉␊ |
799 | ␉␉/* ␊ |
800 | ␉␉ * Inpersonate the specified CPU FAMILY and CPU Model␊ |
801 | ␉␉ */␊ |
802 | ␉␉␊ |
803 | ␉␉// bytes[patchLocation - 17] = 0xC7;␉// already here... not needed to be done␊ |
804 | ␉␉// bytes[patchLocation - 16] = 0x05;␉// see above␊ |
805 | ␉␉UInt32 cpuid_cpufamily_addr =␉bytes[patchLocation - 15] << 0 |␊ |
806 | ␉␉bytes[patchLocation - 14] << 8 |␊ |
807 | ␉␉bytes[patchLocation - 13] << 16 |␊ |
808 | ␉␉bytes[patchLocation - 12] << 24;␊ |
809 | ␉␉␊ |
810 | ␉␉// NOTE: may change, determined based on cpuid_info struct␊ |
811 | ␉␉UInt32 cpuid_model_addr = cpuid_cpufamily_addr - 299; ␊ |
812 | ␊ |
813 | ␉␉// cpufamily␊ |
814 | ␉␉bytes[patchLocation - 11] = (impersonateFamily & 0x000000FF) >> 0;␊ |
815 | ␉␉bytes[patchLocation - 10] = (impersonateFamily & 0x0000FF00) >> 8;␊ |
816 | ␉␉bytes[patchLocation - 9] = (impersonateFamily & 0x00FF0000) >> 16;␉␊ |
817 | ␉␉bytes[patchLocation - 8] = (impersonateFamily & 0xFF000000) >> 24;␊ |
818 | ␉␉␊ |
819 | ␉␉// NOPS, just in case if the jmp call wasn't patched, we'll jump to a␊ |
820 | ␉␉// nop and continue with the rest of the patch␊ |
821 | ␉␉// Yay two free bytes :), 10 more can be reclamed if needed, as well as a few␊ |
822 | ␉␉// from the above code (only cpuid_model needs to be set.␊ |
823 | ␉␉bytes[patchLocation - 7] = 0x90;␊ |
824 | ␉␉bytes[patchLocation - 6] = 0x90;␊ |
825 | ␉␉␊ |
826 | ␉␉bytes[patchLocation - 5] = 0xC7;␊ |
827 | ␉␉bytes[patchLocation - 4] = 0x05;␊ |
828 | ␉␉bytes[patchLocation - 3] = (cpuid_model_addr & 0x000000FF) >> 0;␊ |
829 | ␉␉bytes[patchLocation - 2] = (cpuid_model_addr & 0x0000FF00) >> 8;␉␊ |
830 | ␉␉bytes[patchLocation - 1] = (cpuid_model_addr & 0x00FF0000) >> 16;␊ |
831 | ␉␉bytes[patchLocation - 0] = (cpuid_model_addr & 0xFF000000) >> 24;␊ |
832 | ␉␉␊ |
833 | ␉␉// Note: I could have just copied the 8bit cpuid_model in and saved about 4 bytes␊ |
834 | ␉␉// so if this function need a different patch it's still possible. Also, about ten bytes previous can be freed.␊ |
835 | ␉␉bytes[patchLocation + 1] = impersonateModel;␉// cpuid_model␊ |
836 | ␉␉bytes[patchLocation + 2] = 0x01;␉// cpuid_extmodel␊ |
837 | ␉␉bytes[patchLocation + 3] = 0x00;␉// cpuid_extfamily␊ |
838 | ␉␉bytes[patchLocation + 4] = 0x02;␉// cpuid_stepping␊ |
839 | ␉}␊ |
840 | ␉else if(impersonateFamily && impersonateModel)␊ |
841 | ␉{␊ |
842 | ␉␉// pre 10.6.2 kernel␊ |
843 | ␉␉// Locate the jump to directly *after* the panic call,␊ |
844 | ␉␉jumpLocation = patchLocation - 4;␊ |
845 | ␉␉while((bytes[jumpLocation - 1] != 0x77 ||␊ |
846 | ␉␉␉ bytes[jumpLocation] != (patchLocation - jumpLocation + 4)) &&␊ |
847 | ␉␉␉ (patchLocation - jumpLocation) < 0x20)␊ |
848 | ␉␉{␊ |
849 | ␉␉␉jumpLocation--;␊ |
850 | ␉␉}␊ |
851 | ␉␉// NOTE above isn't needed (I was going to use it, but I'm not, so instead,␊ |
852 | ␉␉// I'll just leave it to verify the binary stucture.␊ |
853 | ␉␉␊ |
854 | ␉␉// NOTE: the cpumodel_familt data is not set in _cpuid_set_info␊ |
855 | ␉␉// so we don't need to set it here, I'll get set later based on the model␊ |
856 | ␉␉// we set now.␊ |
857 | ␉␉␊ |
858 | ␉␉if((patchLocation - jumpLocation) < 0x20)␊ |
859 | ␉␉{␊ |
860 | ␉␉␉UInt32 cpuid_model_addr =␉(bytes[patchLocation - 14] << 0 |␊ |
861 | ␉␉␉␉␉␉␉␉␉␉ bytes[patchLocation - 13] << 8 |␊ |
862 | ␉␉␉␉␉␉␉␉␉␉ bytes[patchLocation - 12] << 16 |␊ |
863 | ␉␉␉␉␉␉␉␉␉␉ bytes[patchLocation - 11] << 24);␊ |
864 | ␉␉␉// Remove jump␊ |
865 | ␉␉␉bytes[patchLocation - 9] = 0x90;␉␉/// Was a jump if supported cpu␊ |
866 | ␉␉␉bytes[patchLocation - 8] = 0x90;␉␉// jumped past the panic call, we want to override the panic␊ |
867 | ␉␉␉␊ |
868 | ␉␉␉bytes[patchLocation - 7] = 0x90;␊ |
869 | ␉␉␉bytes[patchLocation - 6] = 0x90;␊ |
870 | ␉␉␉␊ |
871 | ␉␉␉bytes[patchLocation - 5] = 0xC7;␊ |
872 | ␉␉␉bytes[patchLocation - 4] = 0x05;␊ |
873 | ␉␉␉bytes[patchLocation - 3] = (cpuid_model_addr & 0x000000FF) >> 0;␊ |
874 | ␉␉␉bytes[patchLocation - 2] = (cpuid_model_addr & 0x0000FF00) >> 8;␉␊ |
875 | ␉␉␉bytes[patchLocation - 1] = (cpuid_model_addr & 0x00FF0000) >> 16;␊ |
876 | ␉␉␉bytes[patchLocation - 0] = (cpuid_model_addr & 0xFF000000) >> 24;␊ |
877 | ␉␉␉␊ |
878 | ␉␉␉// Note: I could have just copied the 8bit cpuid_model in and saved about 4 bytes␊ |
879 | ␉␉␉// so if this function need a different patch it's still possible. Also, about ten bytes previous can be freed.␊ |
880 | ␉␉␉bytes[patchLocation + 1] = impersonateModel;␉// cpuid_model␊ |
881 | ␉␉␉bytes[patchLocation + 2] = 0x01;␉// cpuid_extmodel␊ |
882 | ␉␉␉bytes[patchLocation + 3] = 0x00;␉// cpuid_extfamily␊ |
883 | ␉␉␉bytes[patchLocation + 4] = 0x02;␉// cpuid_stepping␊ |
884 | ␊ |
885 | #if 0␊ |
886 | ␉␉␉patchLocation = jumpLocation;␊ |
887 | #endif␊ |
888 | ␉␉␉// We now have 14 bytes available for a patch␊ |
889 | ␉␉}␊ |
890 | ␉␉else ␊ |
891 | ␉␉{␊ |
892 | ␉␉␉// Patching failed, using NOP replacement done initialy␊ |
893 | ␉␉}␊ |
894 | ␉}␊ |
895 | ␉else ␊ |
896 | ␉{␊ |
897 | ␉␉// Either We were unable to change the jump call due to the function's sctructure␊ |
898 | ␉␉// changing, or the user did not request a patch. As such, resort to just ␊ |
899 | ␉␉// removing the panic call (using NOP replacement above). Note that the␊ |
900 | ␉␉// IntelCPUPM kext may still panic due to the cpu's Model ID not being patched␊ |
901 | ␉}␊ |
902 | }␊ |
903 | ␊ |
904 | /**␊ |
905 | ** SleepEnabler.kext replacement (for those that need it)␊ |
906 | ** Located the KERN_INVALID_ARGUMENT return and replace it with KERN_SUCCESS␊ |
907 | **/␊ |
908 | #if 0␊ |
909 | static void patch_pmCPUExitHaltToOff(void* kernelData)␊ |
910 | {␊ |
911 | ␉UInt8* bytes = (UInt8*)kernelData;␊ |
912 | ␊ |
913 | ␉kernSymbols_t *symbol = lookup_kernel_symbol("_PmCpuExitHaltToOff");␊ |
914 | ␉UInt32 patchLocation = symbol ? symbol->addr - textAddress + textSection: 0;␊ |
915 | ␉␊ |
916 | ␉if(symbol == 0 || symbol->addr == 0)␊ |
917 | ␉{␊ |
918 | ␉␉printf("Unable to locate _pmCPUExitHaltToOff\n");␊ |
919 | ␉␉return;␊ |
920 | ␉}␊ |
921 | ␉␊ |
922 | ␉patchLocation -= (UInt32)kernelData;␉// Remove offset␊ |
923 | ␊ |
924 | ␉while(bytes[patchLocation - 1]␉!= 0xB8 ||␊ |
925 | ␉␉ bytes[patchLocation]␉␉!= 0x04 ||␉// KERN_INVALID_ARGUMENT (0x00000004)␊ |
926 | ␉␉ bytes[patchLocation + 1]␉!= 0x00 ||␉// KERN_INVALID_ARGUMENT␊ |
927 | ␉␉ bytes[patchLocation + 2]␉!= 0x00 ||␉// KERN_INVALID_ARGUMENT␊ |
928 | ␉␉ bytes[patchLocation + 3]␉!= 0x00)␉// KERN_INVALID_ARGUMENT␊ |
929 | ␊ |
930 | ␉{␊ |
931 | ␉␉patchLocation++;␊ |
932 | ␉}␊ |
933 | ␉bytes[patchLocation] = 0x00;␉// KERN_SUCCESS;␊ |
934 | }␊ |
935 | #endif␊ |
936 | ␊ |
937 | static void patch_lapic_init(void* kernelData)␊ |
938 | {␊ |
939 | ␉UInt8 panicIndex = 0;␊ |
940 | ␉UInt8* bytes = (UInt8*)kernelData;␊ |
941 | ␉␊ |
942 | ␉kernSymbols_t *symbol = lookup_kernel_symbol("_lapic_init");␊ |
943 | ␉UInt32 patchLocation = symbol ? symbol->addr - textAddress + textSection: 0; ␊ |
944 | ␉if(symbol == 0 || symbol->addr == 0)␊ |
945 | ␉{␊ |
946 | ␉␉printf("Unable to locate %s\n", "_lapic_init");␊ |
947 | ␉␉return;␊ |
948 | ␉}␊ |
949 | ␉␊ |
950 | ␉symbol = lookup_kernel_symbol("_panic");␊ |
951 | ␉UInt32 panicAddr = symbol ? symbol->addr - textAddress: 0; ␊ |
952 | ␉if(symbol == 0 || symbol->addr == 0)␊ |
953 | ␉{␊ |
954 | ␉␉printf("Unable to locate %s\n", "_panic");␊ |
955 | ␉␉return;␊ |
956 | ␉}␊ |
957 | ␉␊ |
958 | ␉patchLocation -= (UInt32)kernelData;␉// Remove offset␊ |
959 | ␉panicAddr -= (UInt32)kernelData;␉// Remove offset␊ |
960 | ␊ |
961 | ␉// Locate the (panicIndex + 1) panic call␊ |
962 | ␉while(panicIndex < 3)␉// Find the third panic call␊ |
963 | ␉{␊ |
964 | ␉␉while( ␊ |
965 | ␉␉␉ (bytes[patchLocation -1] != 0xE8) ||␊ |
966 | ␉␉␉ ( ( (UInt32)(panicAddr - patchLocation - 4) + textSection ) != (UInt32)((bytes[patchLocation + 0] << 0 | ␊ |
967 | ␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉bytes[patchLocation + 1] << 8 | ␊ |
968 | ␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉bytes[patchLocation + 2] << 16 |␊ |
969 | ␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉bytes[patchLocation + 3] << 24)))␊ |
970 | ␉␉␉ )␊ |
971 | ␉␉{␊ |
972 | ␉␉␉patchLocation++;␊ |
973 | ␉␉}␊ |
974 | ␉␉patchLocation++;␊ |
975 | ␉␉panicIndex++;␊ |
976 | ␉}␊ |
977 | ␉patchLocation--;␉// Remove extra increment from the < 3 while loop␊ |
978 | ␉␊ |
979 | ␉bytes[--patchLocation] = 0x90;␉␊ |
980 | ␉bytes[++patchLocation] = 0x90;␊ |
981 | ␉bytes[++patchLocation] = 0x90;␊ |
982 | ␉bytes[++patchLocation] = 0x90;␊ |
983 | ␉bytes[++patchLocation] = 0x90;␊ |
984 | }␊ |
985 | ␊ |
986 | static void patch_commpage_stuff_routine(void* kernelData)␊ |
987 | {␊ |
988 | ␉UInt8* bytes = (UInt8*)kernelData;␊ |
989 | ␊ |
990 | ␉kernSymbols_t *symbol = lookup_kernel_symbol("_commpage_stuff_routine");␊ |
991 | ␉if(symbol == 0 || symbol->addr == 0)␊ |
992 | ␉{␊ |
993 | ␉␉//printf("Unable to locate %s\n", "_commpage_stuff_routine");␊ |
994 | ␉␉return;␊ |
995 | ␉␉␊ |
996 | ␉}␊ |
997 | ␉␊ |
998 | ␉UInt32 patchLocation = symbol->addr - textAddress + textSection; ␊ |
999 | ␊ |
1000 | ␉symbol = lookup_kernel_symbol("_panic");␊ |
1001 | ␉if(symbol == 0 || symbol->addr == 0)␊ |
1002 | ␉{␊ |
1003 | ␉␉printf("Unable to locate %s\n", "_panic");␊ |
1004 | ␉␉return;␊ |
1005 | ␉}␊ |
1006 | ␉UInt32 panicAddr = symbol->addr - textAddress; ␊ |
1007 | ␊ |
1008 | ␉patchLocation -= (UInt32)kernelData;␊ |
1009 | ␉panicAddr -= (UInt32)kernelData;␊ |
1010 | ␉␊ |
1011 | ␉while( ␊ |
1012 | ␉␉ (bytes[patchLocation -1] != 0xE8) ||␊ |
1013 | ␉␉ ( ( (UInt32)(panicAddr - patchLocation - 4) + textSection ) != (UInt32)((bytes[patchLocation + 0] << 0 | ␊ |
1014 | ␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉bytes[patchLocation + 1] << 8 | ␊ |
1015 | ␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉bytes[patchLocation + 2] << 16 |␊ |
1016 | ␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉bytes[patchLocation + 3] << 24)))␊ |
1017 | ␉␉ )␊ |
1018 | ␉{␊ |
1019 | ␉␉patchLocation++;␊ |
1020 | ␉}␊ |
1021 | ␉patchLocation--;␊ |
1022 | ␉␊ |
1023 | ␉// Replace panic with nops␊ |
1024 | ␉bytes[patchLocation + 0] = 0x90;␊ |
1025 | ␉bytes[patchLocation + 1] = 0x90;␊ |
1026 | ␉bytes[patchLocation + 2] = 0x90;␊ |
1027 | ␉bytes[patchLocation + 3] = 0x90;␊ |
1028 | ␉bytes[patchLocation + 4] = 0x90;␊ |
1029 | }␊ |
1030 | #if 0␊ |
1031 | static void patch_lapic_interrupt(void* kernelData)␊ |
1032 | {␊ |
1033 | ␉// NOTE: this is a hack untill I finish patch_lapic_configure␊ |
1034 | ␉UInt8* bytes = (UInt8*)kernelData;␊ |
1035 | ␊ |
1036 | ␉kernSymbols_t *symbol = lookup_kernel_symbol("_lapic_interrupt");␊ |
1037 | ␉if(symbol == 0 || symbol->addr == 0)␊ |
1038 | ␉{␊ |
1039 | ␉␉printf("Unable to locate %s\n", "_lapic_interrupt");␊ |
1040 | ␉␉return;␊ |
1041 | ␉}␊ |
1042 | ␊ |
1043 | ␉UInt32 patchLocation = symbol->addr - textAddress + textSection; ␊ |
1044 | ␊ |
1045 | ␉symbol = lookup_kernel_symbol("_panic");␊ |
1046 | ␉if(symbol == 0 || symbol->addr == 0)␊ |
1047 | ␉{␊ |
1048 | ␉␉printf("Unable to locate %s\n", "_panic");␊ |
1049 | ␉␉return;␊ |
1050 | ␉}␊ |
1051 | ␉UInt32 panicAddr = symbol->addr - textAddress; ␊ |
1052 | ␉␊ |
1053 | ␉patchLocation -= (UInt32)kernelData;␊ |
1054 | ␉panicAddr -= (UInt32)kernelData;␊ |
1055 | ␉␊ |
1056 | ␉while( ␊ |
1057 | ␉␉ (bytes[patchLocation -1] != 0xE8) ||␊ |
1058 | ␉␉ ( ( (UInt32)(panicAddr - patchLocation - 4) + textSection ) != (UInt32)((bytes[patchLocation + 0] << 0 | ␊ |
1059 | ␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉bytes[patchLocation + 1] << 8 | ␊ |
1060 | ␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉bytes[patchLocation + 2] << 16 |␊ |
1061 | ␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉bytes[patchLocation + 3] << 24)))␊ |
1062 | ␉␉ )␊ |
1063 | ␉{␊ |
1064 | ␉␉patchLocation++;␊ |
1065 | ␉}␊ |
1066 | ␉patchLocation--;␊ |
1067 | ␉␊ |
1068 | ␉// Replace panic with nops␊ |
1069 | ␉bytes[patchLocation + 0] = 0x90;␊ |
1070 | ␉bytes[patchLocation + 1] = 0x90;␊ |
1071 | ␉bytes[patchLocation + 2] = 0x90;␊ |
1072 | ␉bytes[patchLocation + 3] = 0x90;␊ |
1073 | ␉bytes[patchLocation + 4] = 0x90;␊ |
1074 | }␊ |
1075 | #endif␊ |
1076 | static void patch_lapic_configure(void* kernelData)␊ |
1077 | {␊ |
1078 | ␉UInt8* bytes = (UInt8*)kernelData;␊ |
1079 | ␉␊ |
1080 | ␉UInt32 patchLocation;␊ |
1081 | ␉UInt32 lapicStart;␊ |
1082 | ␉UInt32 lapicInterruptBase;␊ |
1083 | ␉␊ |
1084 | ␉kernSymbols_t *symbol = lookup_kernel_symbol("_lapic_configure");␊ |
1085 | ␉if(symbol == 0 || symbol->addr == 0)␊ |
1086 | ␉{␊ |
1087 | ␉␉printf("Unable to locate %s\n", "_lapic_configure");␊ |
1088 | ␉␉return;␊ |
1089 | ␉}␊ |
1090 | ␉patchLocation = symbol->addr - textAddress + textSection; ␊ |
1091 | ␉␊ |
1092 | ␉symbol = lookup_kernel_symbol("_lapic_start");␊ |
1093 | ␉if(symbol == 0 || symbol->addr == 0)␊ |
1094 | ␉{␊ |
1095 | ␉␉printf("Unable to locate %s\n", "_lapic_start");␊ |
1096 | ␉␉return;␊ |
1097 | ␉}␊ |
1098 | ␉lapicStart = symbol->addr; ␊ |
1099 | ␊ |
1100 | ␉symbol = lookup_kernel_symbol("_lapic_interrupt_base");␊ |
1101 | ␉if(symbol == 0 || symbol->addr == 0)␊ |
1102 | ␉{␊ |
1103 | ␉␉printf("Unable to locate %s\n", "_lapic_interrupt_base");␊ |
1104 | ␉␉return;␊ |
1105 | ␉}␊ |
1106 | ␉lapicInterruptBase = symbol->addr;␊ |
1107 | ␉patchLocation -= (UInt32)kernelData;␊ |
1108 | ␉lapicStart -= (UInt32)kernelData;␊ |
1109 | ␉lapicInterruptBase -= (UInt32)kernelData;␊ |
1110 | ␉␊ |
1111 | ␉// Looking for the following:␊ |
1112 | ␉//movl _lapic_start,%e_x␊ |
1113 | ␉//addl $0x00000320,%e_x␊ |
1114 | ␉// 8b 15 __ __ __ __ 81 c2 20 03 00 00␊ |
1115 | ␉while( ␊ |
1116 | ␉␉ (bytes[patchLocation - 2] != 0x8b) ||␊ |
1117 | ␉␉ //bytes[patchLocation -1] != 0x15) ||␉// Register, we don't care what it is␊ |
1118 | ␉␉ ( lapicStart != (UInt32)(␊ |
1119 | ␉␉␉␉␉␉␉␉␉(bytes[patchLocation + 0] << 0 | ␊ |
1120 | ␉␉␉␉␉␉␉␉␉ bytes[patchLocation + 1] << 8 | ␊ |
1121 | ␉␉␉␉␉␉␉␉␉ bytes[patchLocation + 2] << 16 |␊ |
1122 | ␉␉␉␉␉␉␉␉␉ bytes[patchLocation + 3] << 24␊ |
1123 | ␉␉␉␉␉␉␉␉␉)␊ |
1124 | ␉␉␉␉␉␉␉␉ )␊ |
1125 | ␉␉ ) || ␊ |
1126 | ␉␉ (bytes[patchLocation + 4 ] != 0x81) ||␊ |
1127 | ␉␉ //(bytes[patchLocation + 5 ] != 0Cx2) ||␉// register␊ |
1128 | ␉␉ (bytes[patchLocation + 6 ] != 0x20) ||␊ |
1129 | ␉␉ (bytes[patchLocation + 7 ] != 0x03) ||␊ |
1130 | ␉␉ (bytes[patchLocation + 8 ] != 0x00) ||␊ |
1131 | ␉␉ (bytes[patchLocation + 9] != 0x00)␊ |
1132 | ␊ |
1133 | ␉␉ )␊ |
1134 | ␉{␊ |
1135 | ␉␉patchLocation++;␊ |
1136 | ␉}␊ |
1137 | ␉patchLocation-=2;␊ |
1138 | ␊ |
1139 | ␉// NOTE: this is currently hardcoded, change it to be more resilient to changes␊ |
1140 | ␉// At a minimum, I should have this do a cheksup first and if not matching, remove the panic instead.␊ |
1141 | ␉␊ |
1142 | ␉// 8b 15 __ __ __ __ -> movl␉␉ _lapic_start,%edx (NOTE: this should already be here)␊ |
1143 | ␉/*␊ |
1144 | ␉bytes[patchLocation++] = 0x8B;␊ |
1145 | ␉bytes[patchLocation++] = 0x15;␊ |
1146 | ␉bytes[patchLocation++] = (lapicStart & 0x000000FF) >> 0;␊ |
1147 | ␉bytes[patchLocation++] = (lapicStart & 0x0000FF00) >> 8;␊ |
1148 | ␉bytes[patchLocation++] = (lapicStart & 0x00FF0000) >> 16;␊ |
1149 | ␉bytes[patchLocation++] = (lapicStart & 0xFF000000) >> 24;␊ |
1150 | ␉*/␊ |
1151 | ␉patchLocation += 6;␊ |
1152 | ␉␊ |
1153 | ␉// 81 c2 60 03 00 00 -> addl␉␉ $0x00000320,%edx␊ |
1154 | ␉/*␊ |
1155 | ␉bytes[patchLocation++] = 0x81;␊ |
1156 | ␉bytes[patchLocation++] = 0xC2;␊ |
1157 | ␉*/␊ |
1158 | ␉patchLocation += 2;␊ |
1159 | ␉bytes[patchLocation++] = 0x60;␊ |
1160 | ␉/*␊ |
1161 | ␉bytes[patchLocation++];// = 0x03;␊ |
1162 | ␉bytes[patchLocation++];// = 0x00;␊ |
1163 | ␉bytes[patchLocation++];// = 0x00;␊ |
1164 | ␉*/␊ |
1165 | ␉patchLocation += 3;␊ |
1166 | ␊ |
1167 | ␉// c7 02 00 04 00 00 -> movl␉␉ $0x00000400,(%edx)␊ |
1168 | ␉bytes[patchLocation++] = 0xC7;␊ |
1169 | ␉bytes[patchLocation++] = 0x02;␊ |
1170 | ␉bytes[patchLocation++] = 0x00;␊ |
1171 | ␉bytes[patchLocation++] = 0x04;␊ |
1172 | ␉bytes[patchLocation++] = 0x00;␊ |
1173 | ␉bytes[patchLocation++] = 0x00;␊ |
1174 | ␉␊ |
1175 | ␉// 83 ea 40 -> subl␉␉ $0x40,edx␊ |
1176 | ␉bytes[patchLocation++] = 0x83;␊ |
1177 | ␉bytes[patchLocation++] = 0xEA;␊ |
1178 | ␉bytes[patchLocation++] = 0x40;␊ |
1179 | ␊ |
1180 | ␉// a1 __ __ __ __ -> movl␉␉ _lapic_interrupt_base,%eax␊ |
1181 | ␉bytes[patchLocation++] = 0xA1;␊ |
1182 | ␉bytes[patchLocation++] = (lapicInterruptBase & 0x000000FF) >> 0;␊ |
1183 | ␉bytes[patchLocation++] = (lapicInterruptBase & 0x0000FF00) >> 8;␊ |
1184 | ␉bytes[patchLocation++] = (lapicInterruptBase & 0x00FF0000) >> 16;␊ |
1185 | ␉bytes[patchLocation++] = (lapicInterruptBase & 0xFF000000) >> 24;␊ |
1186 | ␊ |
1187 | ␉// 83 c0 0e -> addl␉␉ $0x0e,%eax␊ |
1188 | ␉bytes[patchLocation++] = 0x83;␊ |
1189 | ␉bytes[patchLocation++] = 0xC0;␊ |
1190 | ␉bytes[patchLocation++] = 0x0E;␊ |
1191 | ␊ |
1192 | ␉// 89 02 -> movl␉␉ %eax,(%edx)␊ |
1193 | ␉bytes[patchLocation++] = 0x89;␊ |
1194 | ␉bytes[patchLocation++] = 0x02;␊ |
1195 | ␉␊ |
1196 | ␉// 81c230030000␉␉ addl␉␉ $0x00000330,%edx␊ |
1197 | ␉bytes[patchLocation++] = 0x81;␊ |
1198 | ␉bytes[patchLocation++] = 0xC2;␊ |
1199 | ␉bytes[patchLocation++] = 0x30;␊ |
1200 | ␉bytes[patchLocation++] = 0x03;␊ |
1201 | ␉bytes[patchLocation++] = 0x00;␊ |
1202 | ␉bytes[patchLocation++] = 0x00;␊ |
1203 | ␉␊ |
1204 | ␉// a1 __ __ __ __ -> movl␉␉ _lapic_interrupt_base,%eax␊ |
1205 | ␉bytes[patchLocation++] = 0xA1;␊ |
1206 | ␉bytes[patchLocation++] = (lapicInterruptBase & 0x000000FF) >> 0;␊ |
1207 | ␉bytes[patchLocation++] = (lapicInterruptBase & 0x0000FF00) >> 8;␊ |
1208 | ␉bytes[patchLocation++] = (lapicInterruptBase & 0x00FF0000) >> 16;␊ |
1209 | ␉bytes[patchLocation++] = (lapicInterruptBase & 0xFF000000) >> 24;␊ |
1210 | ␉␊ |
1211 | ␉// 83 c0 0f -> addl␉␉ $0x0f,%eax␊ |
1212 | ␉bytes[patchLocation++] = 0x83;␊ |
1213 | ␉bytes[patchLocation++] = 0xC0;␊ |
1214 | ␉bytes[patchLocation++] = 0x0F;␊ |
1215 | ␉␊ |
1216 | ␉// 89 02 -> movl␉␉ %eax,(%edx)␊ |
1217 | ␉bytes[patchLocation++] = 0x89;␊ |
1218 | ␉bytes[patchLocation++] = 0x02;␊ |
1219 | ␉␊ |
1220 | ␉// 83 ea 10 -> subl␉␉ $0x10,edx␊ |
1221 | ␉bytes[patchLocation++] = 0x83;␊ |
1222 | ␉bytes[patchLocation++] = 0xEA;␊ |
1223 | ␉bytes[patchLocation++] = 0x10;␊ |
1224 | ␊ |
1225 | ␉// a1 __ __ __ __ -> movl␉␉ _lapic_interrupt_base,%eax␊ |
1226 | ␉bytes[patchLocation++] = 0xA1;␊ |
1227 | ␉bytes[patchLocation++] = (lapicInterruptBase & 0x000000FF) >> 0;␊ |
1228 | ␉bytes[patchLocation++] = (lapicInterruptBase & 0x0000FF00) >> 8;␊ |
1229 | ␉bytes[patchLocation++] = (lapicInterruptBase & 0x00FF0000) >> 16;␊ |
1230 | ␉bytes[patchLocation++] = (lapicInterruptBase & 0xFF000000) >> 24;␊ |
1231 | ␉␊ |
1232 | ␉// 83 c0 0c -> addl␉␉ $0x0c,%eax␊ |
1233 | ␉bytes[patchLocation++] = 0x83;␊ |
1234 | ␉bytes[patchLocation++] = 0xC0;␊ |
1235 | ␉bytes[patchLocation++] = 0x0C;␊ |
1236 | ␊ |
1237 | ␉// 89 02 -> movl␉␉ %eax,(%edx)␊ |
1238 | ␉bytes[patchLocation++] = 0x89;␊ |
1239 | ␉bytes[patchLocation++] = 0x02;␊ |
1240 | ␊ |
1241 | ␉// Replace remaining with nops␊ |
1242 | ␊ |
1243 | ␉bytes[patchLocation++] = 0x90;␊ |
1244 | ␉bytes[patchLocation++] = 0x90;␊ |
1245 | ␉bytes[patchLocation++] = 0x90;␊ |
1246 | ␉bytes[patchLocation++] = 0x90;␊ |
1247 | ␉//␉bytes[patchLocation++] = 0x90; // double check the lenght of the patch...␊ |
1248 | ␉//␉bytes[patchLocation++] = 0x90;␊ |
1249 | }␊ |
1250 | |