Chameleon

Chameleon Svn Source Tree

Root/branches/azimutz/Chazileon/i386/boot2/kernel_patcher.c

1/*
2 * Copyright (c) 2009 Evan Lojewski. All rights reserved.
3 *
4 */
5
6#include "libsaio.h"
7#include "kernel_patcher.h"
8#include "platform.h"
9
10extern PlatformInfo_t Platform;
11
12
13#define SYMBOL_CPUID_SET_INFO0
14#define SYMBOL_PANIC1
15#define SYMBOL_PMCPUEXITHALTTOOFF2
16#define SYMBOL_LAPIC_INIT3
17#define SYMBOL_COMMPAGE_STUFF_ROUTINE4 //Azi: not needed to boot -legacy
18#define NUM_SYMBOLS5
19
20#define SYMBOL_CPUID_SET_INFO_STRING"_cpuid_set_info"
21#define SYMBOL_PANIC_STRING"_panic"
22#define SYMBOL_PMCPUEXITHALTTOOFF_STRING"_pmCPUExitHaltToOff"
23#define SYMBOL_LAPIC_INIT_STRING"_lapic_init"
24#define SYMBOL_COMMPAGE_STUFF_ROUTINE_STRING"_commpage_stuff_routine"
25
26char* kernelSymbols[NUM_SYMBOLS] = {
27SYMBOL_CPUID_SET_INFO_STRING,
28SYMBOL_PANIC_STRING,
29SYMBOL_PMCPUEXITHALTTOOFF_STRING,
30SYMBOL_LAPIC_INIT_STRING,
31SYMBOL_COMMPAGE_STUFF_ROUTINE_STRING
32};
33
34UInt32 kernelSymbolAddresses[NUM_SYMBOLS] = {
350,
360,
370,
380,
390
40};
41
42
43UInt32 textSection = 0;
44UInt32 textAddress = 0;
45
46
47void patch_kernel(void* kernelData)
48{
49switch (locate_symbols((void*)kernelData)) {
50case KERNEL_32:
51patch_kernel_32((void*)kernelData);
52break;
53
54case KERNEL_64:
55default:
56patch_kernel_64((void*)kernelData);
57break;
58}
59}
60
61// patches a 64bit kernel.
62void patch_kernel_64(void* kernelData)
63{
64// At the moment, the kernel patching code fails when used
65// in 64bit mode, so we don't patch it. This is due to 32bit vs 64bit
66// pointers as well as changes in structure sizes
67printf("Unable to patch 64bit kernel. Please use arch=i386 or -legacy flags.\n");
68}
69
70
71/**
72 ** patch_kernel_32
73 **patches kernel based on cpu info determined earlier in the boot process.
74 **It the machine is vmware, remove the artificial lapic panic
75 **If the CPU is not supported, remove the cpuid_set_info panic
76 **If the CPU is and Intel Atom, inject the penryn cpuid info.
77 **/
78void patch_kernel_32(void* kernelData)
79{
80// Remove panic in commpage
81patch_commpage_stuff_routine(kernelData);
82
83//patch_pmCPUExitHaltToOff(kernelData);// Not working as intended, disabled for now
84
85//if(vmware_detected)
86{
87patch_lapic_init(kernelData);
88}
89
90switch(Platform.CPU.Model)
91{
92// Known good CPU's, no reason to patch kernel
93case 13:
94case CPUID_MODEL_YONAH:
95case CPUID_MODEL_MEROM:
96case CPUID_MODEL_PENRYN:
97case CPUID_MODEL_NEHALEM:
98case CPUID_MODEL_FIELDS:
99case CPUID_MODEL_DALES:
100case CPUID_MODEL_NEHALEM_EX:
101break;
102
103// Known unsuported CPU's
104case CPUID_MODEL_ATOM:
105// TODO: Impersonate CPU based on user selection
106patch_cpuid_set_info(kernelData, CPUFAMILY_INTEL_PENRYN, CPUID_MODEL_PENRYN);// Impersonate Penryn CPU
107break;
108
109// Unknown CPU's
110default:
111// TODO: Impersonate CPU based on user selection
112patch_cpuid_set_info(kernelData, 0, 0);// Remove Panic Call
113
114break;
115}
116}
117
118
119/**
120 **This functions located the kernelSymbols[i] symbols in the mach-o header.
121 **as well as determines the start of the __TEXT segment and __TEXT,__text sections
122 **/
123int locate_symbols(void* kernelData)
124{
125UInt16 symbolIndexes[NUM_SYMBOLS];
126
127struct load_command *loadCommand;
128struct symtab_command *symtableData;
129struct nlist *symbolEntry;
130
131char* symbolString;
132
133UInt32 kernelIndex = 0;
134kernelIndex += sizeof(struct mach_header);
135
136if(((struct mach_header*)kernelData)->magic != MH_MAGIC) return KERNEL_64;
137
138
139//printf("%d load commands beginning at 0x%X\n", (unsigned int)header->ncmds, (unsigned int)kernelIndex);
140//printf("Commands take up %d bytes\n", header->sizeofcmds);
141
142
143int cmd = 0;
144while(cmd < ((struct mach_header*)kernelData)->ncmds)// TODO: for loop instead
145{
146cmd++;
147
148loadCommand = kernelData + kernelIndex;
149
150UInt cmdSize = loadCommand->cmdsize;
151
152// Locate start of _panic and _cpuid_set_info in the symbol tabe.
153// Load commands should be anded with 0x7FFFFFFF to ignore theLC_REQ_DYLD flag
154if((loadCommand->cmd & 0x7FFFFFFF) == LC_SYMTAB)// We only care about the symtab segment
155{
156//printf("Located symtable, length is 0x%X, 0x%X\n", (unsigned int)loadCommand->cmdsize, (unsigned int)sizeof(symtableData));
157
158symtableData = kernelData + kernelIndex;
159kernelIndex += sizeof(struct symtab_command);
160
161cmdSize -= sizeof(struct symtab_command);
162
163// Loop through symbol table untill all of the symbols have been found
164
165symbolString = kernelData + symtableData->stroff;
166
167
168UInt16 symbolIndex = 0;
169UInt8 numSymbolsFound = 0;
170
171while(symbolIndex < symtableData->nsyms && numSymbolsFound < NUM_SYMBOLS)// TODO: for loop
172{
173int i = 0;
174while(i < NUM_SYMBOLS)
175{
176if(strcmp(symbolString, kernelSymbols[i]) == 0)
177{
178symbolIndexes[i] = symbolIndex;
179numSymbolsFound++;
180}
181i++;
182
183}
184symbolString += strlen(symbolString) + 1;
185symbolIndex++;
186}
187
188// loop again
189symbolIndex = 0;
190numSymbolsFound = 0;
191while(symbolIndex < symtableData->nsyms && numSymbolsFound < NUM_SYMBOLS)// TODO: for loop
192{
193
194symbolEntry = kernelData + symtableData->symoff + (symbolIndex * sizeof(struct nlist));
195
196int i = 0;
197while(i < NUM_SYMBOLS)
198{
199if(symbolIndex == (symbolIndexes[i] - 4))
200{
201kernelSymbolAddresses[i] = (UInt32)symbolEntry->n_value;
202numSymbolsFound++;
203}
204i++;
205
206}
207
208symbolIndex ++;
209}
210// Load commands should be anded with 0x7FFFFFFF to ignore theLC_REQ_DYLD flag
211} else if((loadCommand->cmd & 0x7FFFFFFF) == LC_SEGMENT)// We only care about the __TEXT segment, any other load command can be ignored
212{
213
214struct segment_command *segCommand;
215
216segCommand = kernelData + kernelIndex;
217
218//printf("Segment name is %s\n", segCommand->segname);
219
220if(strcmp("__TEXT", segCommand->segname) == 0)
221{
222UInt32 sectionIndex;
223
224sectionIndex = sizeof(struct segment_command);
225
226struct section *sect;
227
228while(sectionIndex < segCommand->cmdsize)
229{
230sect = kernelData + kernelIndex + sectionIndex;
231
232sectionIndex += sizeof(struct section);
233
234
235if(strcmp("__text", sect->sectname) == 0)
236{
237// __TEXT,__text found, save the offset and address for when looking for the calls.
238textSection = sect->offset;
239textAddress = sect->addr;
240break;
241}
242}
243}
244
245
246kernelIndex += cmdSize;
247} else {
248kernelIndex += cmdSize;
249}
250}
251
252return KERNEL_32;
253}
254
255
256/**
257 ** Locate the fisrt instance of _panic inside of _cpuid_set_info, and either remove it
258 ** Or replace it so that the cpuid is set to a valid value.
259 **/
260void patch_cpuid_set_info(void* kernelData, UInt32 impersonateFamily, UInt8 impersonateModel)
261{
262UInt8* bytes = (UInt8*)kernelData;
263UInt32 patchLocation = (kernelSymbolAddresses[SYMBOL_CPUID_SET_INFO] - textAddress + textSection);
264UInt32 jumpLocation = 0;
265UInt32 panicAddr = kernelSymbolAddresses[SYMBOL_PANIC] - textAddress;
266if(kernelSymbolAddresses[SYMBOL_CPUID_SET_INFO] == 0)
267{
268printf("Unable to locate _cpuid_set_info\n");
269return;
270
271}
272if(kernelSymbolAddresses[SYMBOL_PANIC] == 0)
273{
274printf("Unable to locate _panic\n");
275return;
276}
277
278//TODO: don't assume it'll always work (Look for *next* function address in symtab and fail once it's been reached)
279while(
280 (bytes[patchLocation -1] != 0xE8) ||
281 ( ( (UInt32)(panicAddr - patchLocation - 4) + textSection ) != (UInt32)((bytes[patchLocation + 0] << 0 |
282bytes[patchLocation + 1] << 8 |
283bytes[patchLocation + 2] << 16 |
284bytes[patchLocation + 3] << 24)))
285 )
286{
287patchLocation++;
288}
289patchLocation--;
290
291
292// Remove panic call, just in case the following patch routines fail
293bytes[patchLocation + 0] = 0x90;
294bytes[patchLocation + 1] = 0x90;
295bytes[patchLocation + 2] = 0x90;
296bytes[patchLocation + 3] = 0x90;
297bytes[patchLocation + 4] = 0x90;
298
299
300// Locate the jump call, so that 10 bytes can be reclamed.
301// NOTE: This will *NOT* be located on pre 10.6.2 kernels
302jumpLocation = patchLocation - 15;
303while((bytes[jumpLocation - 1] != 0x77 ||
304 bytes[jumpLocation] != (patchLocation - jumpLocation - -8)) &&
305 (patchLocation - jumpLocation) < 0xF0)
306{
307jumpLocation--;
308}
309
310printf("Mode: %d Family %d P - JMP: 0x%X\n", impersonateModel, impersonateFamily, patchLocation - jumpLocation);
311// If found... AND we want to impersonate a specific cpumodel / family...
312if(impersonateFamily &&
313 impersonateModel &&
314 ((patchLocation - jumpLocation) < 0xF0))
315{
316printf("Patching CPUID to %d.%d\n", impersonateFamily, impersonateModel);
317
318bytes[jumpLocation] -= 10;// sizeof(movl$0x6b5a4cd2,0x00872eb4) = 10bytes
319
320/*
321 * Inpersonate the specified CPU FAMILY and CPU Model
322 */
323
324// bytes[patchLocation - 17] = 0xC7;// already here... not needed to be done
325// bytes[patchLocation - 16] = 0x05;// see above
326UInt32 cpuid_cpufamily_addr =bytes[patchLocation - 15] << 0 |
327bytes[patchLocation - 14] << 8 |
328bytes[patchLocation - 13] << 16 |
329bytes[patchLocation - 12] << 24;
330
331// NOTE: may change, determined based on cpuid_info struct
332UInt32 cpuid_model_addr = cpuid_cpufamily_addr - 299;
333
334
335// cpufamily = CPUFAMILY_INTEL_PENRYN
336bytes[patchLocation - 11] = (impersonateFamily & 0x000000FF) >> 0;
337bytes[patchLocation - 10] = (impersonateFamily & 0x0000FF00) >> 8;
338bytes[patchLocation - 9] = (impersonateFamily & 0x00FF0000) >> 16;
339bytes[patchLocation - 8] = (impersonateFamily & 0xFF000000) >> 24;
340
341// NOPS, just in case if the jmp call wasn't patched, we'll jump to a
342// nop and continue with the rest of the patch
343// Yay two free bytes :), 10 more can be reclamed if needed, as well as a few
344// from the above code (only cpuid_model needs to be set.
345bytes[patchLocation - 7] = 0x90;
346bytes[patchLocation - 6] = 0x90;
347
348bytes[patchLocation - 5] = 0xC7;
349bytes[patchLocation - 4] = 0x05;
350bytes[patchLocation - 3] = (cpuid_model_addr & 0x000000FF) >> 0;
351bytes[patchLocation - 2] = (cpuid_model_addr & 0x0000FF00) >> 8;
352bytes[patchLocation - 1] = (cpuid_model_addr & 0x00FF0000) >> 16;
353bytes[patchLocation - 0] = (cpuid_model_addr & 0xFF000000) >> 24;
354
355// Note: I could have just copied the 8bit cpuid_model in and saved about 4 bytes
356// so if this function need a different patch it's still possible. Also, about ten bytes previous can be freed.
357bytes[patchLocation + 1] = impersonateModel;// cpuid_model
358bytes[patchLocation + 2] = 0x01;// cpuid_extmodel
359bytes[patchLocation + 3] = 0x00;// cpuid_extfamily
360bytes[patchLocation + 4] = 0x02;// cpuid_stepping
361
362}
363else if(impersonateFamily && impersonateModel)
364{
365// pre 10.6.2 kernel
366// Locate the jump to directly *after* the panic call,
367jumpLocation = patchLocation - 4;
368while((bytes[jumpLocation - 1] != 0x77 ||
369 bytes[jumpLocation] != (patchLocation - jumpLocation + 4)) &&
370 (patchLocation - jumpLocation) < 0x20)
371{
372jumpLocation--;
373}
374// NOTE above isn't needed (I was going to use it, but I'm not, so instead,
375// I'll just leave it to verify the binary stucture.
376
377// NOTE: the cpumodel_familt data is not set in _cpuid_set_info
378// so we don't need to set it here, I'll get set later based on the model
379// we set now.
380
381if((patchLocation - jumpLocation) < 0x20)
382{
383UInt32 cpuid_model_addr =(bytes[patchLocation - 14] << 0 |
384bytes[patchLocation - 13] << 8 |
385bytes[patchLocation - 12] << 16 |
386bytes[patchLocation - 11] << 24);
387// Remove jump
388bytes[patchLocation - 9] = 0x90;/// Was a jump if supported cpu
389bytes[patchLocation - 8] = 0x90;// jumped past the panic call, we want to override the panic
390
391bytes[patchLocation - 7] = 0x90;
392bytes[patchLocation - 6] = 0x90;
393
394bytes[patchLocation - 5] = 0xC7;
395bytes[patchLocation - 4] = 0x05;
396bytes[patchLocation - 3] = (cpuid_model_addr & 0x000000FF) >> 0;
397bytes[patchLocation - 2] = (cpuid_model_addr & 0x0000FF00) >> 8;
398bytes[patchLocation - 1] = (cpuid_model_addr & 0x00FF0000) >> 16;
399bytes[patchLocation - 0] = (cpuid_model_addr & 0xFF000000) >> 24;
400
401// Note: I could have just copied the 8bit cpuid_model in and saved about 4 bytes
402// so if this function need a different patch it's still possible. Also, about ten bytes previous can be freed.
403bytes[patchLocation + 1] = impersonateModel;// cpuid_model
404bytes[patchLocation + 2] = 0x01;// cpuid_extmodel
405bytes[patchLocation + 3] = 0x00;// cpuid_extfamily
406bytes[patchLocation + 4] = 0x02;// cpuid_stepping
407
408
409
410patchLocation = jumpLocation;
411// We now have 14 bytes available for a patch
412
413}
414else
415{
416// Patching failed, using NOP replacement done initialy
417}
418}
419else
420{
421// Either We were unable to change the jump call due to the function's sctructure
422// changing, or the user did not request a patch. As such, resort to just
423// removing the panic call (using NOP replacement above). Note that the
424// IntelCPUPM kext may still panic due to the cpu's Model ID not being patched
425}
426}
427
428
429/**
430 ** SleepEnabler.kext replacement (for those that need it)
431 ** Located the KERN_INVALID_ARGUMENT return and replace it with KERN_SUCCESS
432 **/
433void patch_pmCPUExitHaltToOff(void* kernelData)
434{
435UInt8* bytes = (UInt8*)kernelData;
436UInt32 patchLocation = (kernelSymbolAddresses[SYMBOL_PMCPUEXITHALTTOOFF] - textAddress + textSection);
437
438if(kernelSymbolAddresses[SYMBOL_PMCPUEXITHALTTOOFF] == 0)
439{
440printf("Unable to locate _pmCPUExitHaltToOff\n");
441return;
442}
443
444while(bytes[patchLocation - 1]!= 0xB8 ||
445 bytes[patchLocation]!= 0x04 ||// KERN_INVALID_ARGUMENT (0x00000004)
446 bytes[patchLocation + 1]!= 0x00 ||// KERN_INVALID_ARGUMENT
447 bytes[patchLocation + 2]!= 0x00 ||// KERN_INVALID_ARGUMENT
448 bytes[patchLocation + 3]!= 0x00)// KERN_INVALID_ARGUMENT
449{
450patchLocation++;
451}
452bytes[patchLocation] = 0x00;// KERN_SUCCESS;
453}
454
455void patch_lapic_init(void* kernelData)
456{
457UInt8 panicIndex = 0;
458UInt8* bytes = (UInt8*)kernelData;
459UInt32 patchLocation = (kernelSymbolAddresses[SYMBOL_LAPIC_INIT] - textAddress + textSection);
460UInt32 panicAddr = kernelSymbolAddresses[SYMBOL_PANIC] - textAddress;
461
462if(kernelSymbolAddresses[SYMBOL_LAPIC_INIT] == 0)
463{
464printf("Unable to locate %s\n", SYMBOL_LAPIC_INIT_STRING);
465return;
466
467}
468if(kernelSymbolAddresses[SYMBOL_PANIC] == 0)
469{
470printf("Unable to locate %s\n", SYMBOL_PANIC_STRING);
471return;
472}
473
474
475
476// Locate the (panicIndex + 1) panic call
477while(panicIndex < 3)// Find the third panic call
478{
479while(
480 (bytes[patchLocation -1] != 0xE8) ||
481 ( ( (UInt32)(panicAddr - patchLocation - 4) + textSection ) != (UInt32)((bytes[patchLocation + 0] << 0 |
482bytes[patchLocation + 1] << 8 |
483bytes[patchLocation + 2] << 16 |
484bytes[patchLocation + 3] << 24)))
485 )
486{
487patchLocation++;
488}
489patchLocation++;
490panicIndex++;
491}
492patchLocation--;// Remove extra increment from the < 3 while loop
493
494bytes[--patchLocation] = 0x90;
495bytes[++patchLocation] = 0x90;
496bytes[++patchLocation] = 0x90;
497bytes[++patchLocation] = 0x90;
498bytes[++patchLocation] = 0x90;
499
500
501}
502
503void patch_commpage_stuff_routine(void* kernelData)
504{
505UInt8* bytes = (UInt8*)kernelData;
506UInt32 patchLocation = (kernelSymbolAddresses[SYMBOL_COMMPAGE_STUFF_ROUTINE] - textAddress + textSection);
507UInt32 panicAddr = kernelSymbolAddresses[SYMBOL_PANIC] - textAddress;
508
509if(kernelSymbolAddresses[SYMBOL_COMMPAGE_STUFF_ROUTINE] == 0)
510{
511printf("Unable to locate %s\n", SYMBOL_COMMPAGE_STUFF_ROUTINE_STRING);
512return;
513
514}
515if(kernelSymbolAddresses[SYMBOL_PANIC] == 0)
516{
517printf("Unable to locate %s\n", SYMBOL_PANIC_STRING);
518return;
519}
520
521
522while(
523 (bytes[patchLocation -1] != 0xE8) ||
524 ( ( (UInt32)(panicAddr - patchLocation - 4) + textSection ) != (UInt32)((bytes[patchLocation + 0] << 0 |
525bytes[patchLocation + 1] << 8 |
526bytes[patchLocation + 2] << 16 |
527bytes[patchLocation + 3] << 24)))
528 )
529{
530patchLocation++;
531}
532patchLocation--;
533
534
535// Remove panic call, just in case the following patch routines fail
536bytes[patchLocation + 0] = 0x90;
537bytes[patchLocation + 1] = 0x90;
538bytes[patchLocation + 2] = 0x90;
539bytes[patchLocation + 3] = 0x90;
540bytes[patchLocation + 4] = 0x90;
541
542
543}
544

Archive Download this file

Revision: 318