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 | extern PlatformInfo_t Platform;␊ |
11 | ␊ |
12 | patchRoutine_t* patches = NULL;␊ |
13 | kernSymbols_t* kernelSymbols = NULL;␊ |
14 | ␊ |
15 | ␊ |
16 | UInt32 textSection = 0;␊ |
17 | UInt32 textAddress = 0;␊ |
18 | ␊ |
19 | ␊ |
20 | void KernelPatcher_start()␊ |
21 | {␊ |
22 | ␉//register_kernel_patch(patch_cpuid_set_info, KERNEL_32, CPUID_MODEL_ATOM);␉␉// TODO: CPUFAMILY_INTEL_PENRYN, CPUID_MODEL_PENRYN␊ |
23 | ␉//register_kernel_patch(patch_cpuid_set_info, KERNEL_32, CPUID_MODEL_UNKNOWN);␉// 0, 0 ␊ |
24 | ␉register_kernel_patch(patch_cpuid_set_info_all, KERNEL_32, CPUID_MODEL_UNKNOWN); ␊ |
25 | ␊ |
26 | ␉register_kernel_patch(patch_commpage_stuff_routine, KERNEL_32, CPUID_MODEL_ANY);␊ |
27 | ␉␊ |
28 | ␉register_kernel_patch(patch_lapic_init, KERNEL_32, CPUID_MODEL_ANY);␊ |
29 | ␊ |
30 | ␉register_kernel_patch(patch_lapic_configure, KERNEL_32, CPUID_MODEL_ANY);␊ |
31 | ␉//register_kernel_patch(patch_lapic_interrupt, KERNEL_32, CPUID_MODEL_ANY);␊ |
32 | ␊ |
33 | ␉␊ |
34 | ␉register_kernel_symbol(KERNEL_32, "_panic");␊ |
35 | ␉register_kernel_symbol(KERNEL_32, "_cpuid_set_info");␊ |
36 | ␉register_kernel_symbol(KERNEL_32, "_pmCPUExitHaltToOff");␊ |
37 | ␉register_kernel_symbol(KERNEL_32, "_lapic_init");␊ |
38 | ␉register_kernel_symbol(KERNEL_32, "_commpage_stuff_routine");␊ |
39 | ␊ |
40 | ␉// LAPIC configure symbols␊ |
41 | ␉register_kernel_symbol(KERNEL_32, "_lapic_configure");␊ |
42 | ␉register_kernel_symbol(KERNEL_32, "_lapic_interrupt");␊ |
43 | ␊ |
44 | ␉register_kernel_symbol(KERNEL_32, "_lapic_start");␊ |
45 | ␉register_kernel_symbol(KERNEL_32, "_lapic_interrupt_base");␊ |
46 | ␊ |
47 | ␉␊ |
48 | ␉// TODO: register needed symbols␊ |
49 | ␉␊ |
50 | ␉␊ |
51 | ␉register_hook_callback("ExecKernel", &patch_kernel); ␊ |
52 | }␊ |
53 | ␊ |
54 | /*␊ |
55 | * Register a kerenl patch␊ |
56 | */␊ |
57 | void register_kernel_patch(void* patch, int arch, int cpus)␊ |
58 | {␊ |
59 | ␉// TODO: only insert valid patches based on current cpuid and architecture␊ |
60 | ␉// AKA, don't at 64bit patches if it's a 32bit only machine␊ |
61 | ␉patchRoutine_t* entry;␊ |
62 | ␉␊ |
63 | ␉// TODO: verify Platform.CPU.Model is populated this early in bootup␊ |
64 | ␉// Check to ensure that the patch is valid on this machine␊ |
65 | ␉// If it is not, exit early form this function␊ |
66 | ␉if(cpus != Platform.CPU.Model)␊ |
67 | ␉{␊ |
68 | ␉␉if(cpus != CPUID_MODEL_ANY)␊ |
69 | ␉␉{␊ |
70 | ␉␉␉if(cpus == CPUID_MODEL_UNKNOWN)␊ |
71 | ␉␉␉{␊ |
72 | ␉␉␉␉switch(Platform.CPU.Model)␊ |
73 | ␉␉␉␉{␊ |
74 | ␉␉␉␉␉case 13:␊ |
75 | ␉␉␉␉␉case CPUID_MODEL_YONAH:␊ |
76 | ␉␉␉␉␉case CPUID_MODEL_MEROM:␊ |
77 | ␉␉␉␉␉case CPUID_MODEL_PENRYN:␊ |
78 | ␉␉␉␉␉case CPUID_MODEL_NEHALEM:␊ |
79 | ␉␉␉␉␉case CPUID_MODEL_FIELDS:␊ |
80 | ␉␉␉␉␉case CPUID_MODEL_DALES:␊ |
81 | ␉␉␉␉␉case CPUID_MODEL_NEHALEM_EX:␊ |
82 | ␉␉␉␉␉␉// Known cpu's we don't want to add the patch␊ |
83 | ␉␉␉␉␉␉return;␊ |
84 | ␉␉␉␉␉␉break;␊ |
85 | ␊ |
86 | ␉␉␉␉␉default:␊ |
87 | ␉␉␉␉␉␉// CPU not in supported list, so we are going to add␊ |
88 | ␉␉␉␉␉␉// The patch will be applied␊ |
89 | ␉␉␉␉␉␉break;␊ |
90 | ␉␉␉␉␉␉␊ |
91 | ␉␉␉␉}␊ |
92 | ␉␉␉}␊ |
93 | ␉␉␉else␊ |
94 | ␉␉␉{␊ |
95 | ␉␉␉␉// Invalid cpuid for current cpu. Ignoring patch␊ |
96 | ␉␉␉␉return;␊ |
97 | ␉␉␉}␊ |
98 | ␊ |
99 | ␉␉}␊ |
100 | ␉}␊ |
101 | ␉␉␊ |
102 | ␉if(patches == NULL)␊ |
103 | ␉{␊ |
104 | ␉␉patches = entry = malloc(sizeof(patchRoutine_t));␊ |
105 | ␉}␊ |
106 | ␉else␊ |
107 | ␉{␊ |
108 | ␉␉entry = patches;␊ |
109 | ␉␉while(entry->next)␊ |
110 | ␉␉{␊ |
111 | ␉␉␉entry = entry->next;␊ |
112 | ␉␉}␊ |
113 | ␉␉␊ |
114 | ␉␉entry->next = malloc(sizeof(patchRoutine_t));␊ |
115 | ␉␉entry = entry->next;␊ |
116 | ␉}␊ |
117 | ␉␊ |
118 | ␉entry->next = NULL;␊ |
119 | ␉entry->patchRoutine = patch;␊ |
120 | ␉entry->validArchs = arch;␊ |
121 | ␉entry->validCpu = cpus;␊ |
122 | }␊ |
123 | ␊ |
124 | void register_kernel_symbol(int kernelType, const char* name)␊ |
125 | {␊ |
126 | ␉if(kernelSymbols == NULL)␊ |
127 | ␉{␊ |
128 | ␉␉kernelSymbols = malloc(sizeof(kernSymbols_t));␊ |
129 | ␉␉kernelSymbols->next = NULL;␊ |
130 | ␉␉kernelSymbols->symbol = (char*)name;␊ |
131 | ␉␉kernelSymbols->addr = 0;␊ |
132 | ␉}␊ |
133 | ␉else {␊ |
134 | ␉␉kernSymbols_t *symbol = kernelSymbols;␊ |
135 | ␉␉while(symbol->next != NULL)␊ |
136 | ␉␉{␊ |
137 | ␉␉␉symbol = symbol->next;␊ |
138 | ␉␉}␊ |
139 | ␉␉␊ |
140 | ␉␉symbol->next = malloc(sizeof(kernSymbols_t));␊ |
141 | ␉␉symbol = symbol->next;␊ |
142 | ␊ |
143 | ␉␉symbol->next = NULL;␊ |
144 | ␉␉symbol->symbol = (char*)name;␊ |
145 | ␉␉symbol->addr = 0;␊ |
146 | ␉}␊ |
147 | }␊ |
148 | ␊ |
149 | kernSymbols_t* lookup_kernel_symbol(const char* name)␊ |
150 | {␊ |
151 | ␉kernSymbols_t *symbol = kernelSymbols;␊ |
152 | ␊ |
153 | ␉while(symbol && strcmp(symbol->symbol, name) !=0)␊ |
154 | ␉{␊ |
155 | ␉␉symbol = symbol->next;␊ |
156 | ␉}␊ |
157 | ␉␊ |
158 | ␉if(!symbol)␊ |
159 | ␉{␊ |
160 | ␉␉return NULL;␊ |
161 | ␉}␊ |
162 | ␉else␊ |
163 | ␉{␊ |
164 | ␉␉return symbol;␊ |
165 | ␉}␊ |
166 | ␊ |
167 | }␊ |
168 | ␊ |
169 | void patch_kernel(void* kernelData, void* arg2, void* arg3, void *arg4)␊ |
170 | {␊ |
171 | ␉patchRoutine_t* entry = patches;␊ |
172 | ␊ |
173 | ␉␊ |
174 | ␉int arch = determineKernelArchitecture(kernelData);␊ |
175 | ␊ |
176 | ␉locate_symbols(kernelData);␊ |
177 | ␉␊ |
178 | ␉␊ |
179 | ␉if(patches != NULL)␊ |
180 | ␉{␊ |
181 | ␉␉while(entry)␊ |
182 | ␉␉{␊ |
183 | ␉␉␉if(entry->validArchs == KERNEL_ANY || arch == entry->validArchs)␊ |
184 | ␉␉␉{␊ |
185 | ␉␉␉␉if(entry->patchRoutine) entry->patchRoutine(kernelData);␊ |
186 | ␉␉␉}␊ |
187 | ␉␉␉entry = entry->next;␊ |
188 | ␉␉}␊ |
189 | ␉␉␊ |
190 | ␉}␊ |
191 | }␊ |
192 | ␊ |
193 | int determineKernelArchitecture(void* kernelData)␊ |
194 | {␉␊ |
195 | ␉if(((struct mach_header*)kernelData)->magic == MH_MAGIC)␊ |
196 | ␉{␊ |
197 | ␉␉return KERNEL_32;␊ |
198 | ␉}␊ |
199 | ␉if(((struct mach_header*)kernelData)->magic == MH_MAGIC_64)␊ |
200 | ␉{␊ |
201 | ␉␉return KERNEL_64;␊ |
202 | ␉}␊ |
203 | ␉else␊ |
204 | ␉{␊ |
205 | ␉␉return KERNEL_ERR;␊ |
206 | ␉}␊ |
207 | }␊ |
208 | ␊ |
209 | ␊ |
210 | /**␊ |
211 | **␉␉This functions located the requested symbols in the mach-o file.␊ |
212 | **␉␉␉as well as determines the start of the __TEXT segment and __TEXT,__text sections␊ |
213 | **/␊ |
214 | int locate_symbols(void* kernelData)␊ |
215 | {␊ |
216 | ␊ |
217 | ␉struct load_command *loadCommand;␊ |
218 | ␉struct symtab_command *symtableData = 0; //Azi:warning␊ |
219 | ␉//␉struct nlist *symbolEntry;␊ |
220 | ␉␊ |
221 | ␉char* symbolString;␊ |
222 | ␊ |
223 | ␉UInt32 kernelIndex = 0;␊ |
224 | ␉kernelIndex += sizeof(struct mach_header);␊ |
225 | ␉␊ |
226 | ␉if(((struct mach_header*)kernelData)->magic != MH_MAGIC) return KERNEL_64;␊ |
227 | ␉␊ |
228 | ␉␊ |
229 | ␉int cmd = 0;␊ |
230 | ␉while(cmd < ((struct mach_header*)kernelData)->ncmds)␉// TODO: for loop instead␊ |
231 | ␉{␊ |
232 | ␉␉cmd++;␊ |
233 | ␉␉␊ |
234 | ␉␉loadCommand = kernelData + kernelIndex;␊ |
235 | ␉␉␊ |
236 | ␉␉UInt cmdSize = loadCommand->cmdsize;␊ |
237 | ␉␉␊ |
238 | ␉␉␊ |
239 | ␉␉if((loadCommand->cmd & 0x7FFFFFFF) == LC_SYMTAB)␉␉// We only care about the symtab segment␊ |
240 | ␉␉{␊ |
241 | ␉␉␉//printf("Located symtable, length is 0x%X, 0x%X\n", (unsigned int)loadCommand->cmdsize, (unsigned int)sizeof(symtableData));␊ |
242 | ␉␉␉␊ |
243 | ␉␉␉symtableData = kernelData + kernelIndex;␊ |
244 | ␉␉␉kernelIndex += sizeof(struct symtab_command);␊ |
245 | ␉␉␊ |
246 | ␉␉␉symbolString = kernelData + symtableData->stroff;␊ |
247 | ␉␉}␊ |
248 | ␉␉else if((loadCommand->cmd & 0x7FFFFFFF) == LC_SEGMENT)␉␉// We only care about the __TEXT segment, any other load command can be ignored␊ |
249 | ␉␉{␊ |
250 | ␉␉␉␊ |
251 | ␉␉␉struct segment_command *segCommand;␊ |
252 | ␉␉␉␊ |
253 | ␉␉␉segCommand = kernelData + kernelIndex;␊ |
254 | ␉␉␉␊ |
255 | ␉␉␉//printf("Segment name is %s\n", segCommand->segname);␊ |
256 | ␉␉␉␊ |
257 | ␉␉␉if(strcmp("__TEXT", segCommand->segname) == 0)␊ |
258 | ␉␉␉{␊ |
259 | ␉␉␉␉UInt32 sectionIndex;␊ |
260 | ␉␉␉␉␊ |
261 | ␉␉␉␉sectionIndex = sizeof(struct segment_command);␊ |
262 | ␉␉␉␉␊ |
263 | ␉␉␉␉struct section *sect;␊ |
264 | ␉␉␉␉␊ |
265 | ␉␉␉␉while(sectionIndex < segCommand->cmdsize)␊ |
266 | ␉␉␉␉{␊ |
267 | ␉␉␉␉␉sect = kernelData + kernelIndex + sectionIndex;␊ |
268 | ␉␉␉␉␉␊ |
269 | ␉␉␉␉␉sectionIndex += sizeof(struct section);␊ |
270 | ␉␉␉␉␉␊ |
271 | ␉␉␉␉␉␊ |
272 | ␉␉␉␉␉if(strcmp("__text", sect->sectname) == 0)␊ |
273 | ␉␉␉␉␉{␊ |
274 | ␉␉␉␉␉␉// __TEXT,__text found, save the offset and address for when looking for the calls.␊ |
275 | ␉␉␉␉␉␉textSection = sect->offset;␊ |
276 | ␉␉␉␉␉␉textAddress = sect->addr;␊ |
277 | ␉␉␉␉␉␉break;␊ |
278 | ␉␉␉␉␉}␉␉␉␉␉␊ |
279 | ␉␉␉␉}␊ |
280 | ␉␉␉}␊ |
281 | ␉␉␉␊ |
282 | ␉␉␉␊ |
283 | ␉␉␉kernelIndex += cmdSize;␊ |
284 | ␉␉} else {␊ |
285 | ␉␉␉kernelIndex += cmdSize;␊ |
286 | ␉␉}␊ |
287 | ␉}␊ |
288 | ␉␊ |
289 | ␉return handle_symtable((UInt32)kernelData, symtableData, &symbol_handler,␊ |
290 | ␉␉␉␉␉␉␉determineKernelArchitecture(kernelData) == KERNEL_64);␊ |
291 | }␊ |
292 | ␊ |
293 | long long symbol_handler(char* symbolName, long long addr, char is64)␊ |
294 | {␊ |
295 | ␉// Locate the symbol in the list, if it exists, update it's address␊ |
296 | ␉kernSymbols_t *symbol = lookup_kernel_symbol(symbolName);␊ |
297 | ␉␊ |
298 | ␉␊ |
299 | ␉if(symbol)␊ |
300 | ␉{␊ |
301 | ␉␉symbol->addr = addr;␊ |
302 | ␉}␊ |
303 | ␉return 0xFFFFFFFF; // fixme␊ |
304 | }␊ |
305 | ␊ |
306 | ␊ |
307 | /**␊ |
308 | ** Locate the fisrt instance of _panic inside of _cpuid_set_info, and either remove it␊ |
309 | ** Or replace it so that the cpuid is set to a valid value.␊ |
310 | **/␊ |
311 | void patch_cpuid_set_info_all(void* kernelData)␊ |
312 | {␊ |
313 | ␉switch(Platform.CPU.Model)␊ |
314 | ␉{␊ |
315 | ␉␉case CPUID_MODEL_ATOM:␊ |
316 | ␉␉␉patch_cpuid_set_info(kernelData, CPUFAMILY_INTEL_PENRYN, CPUID_MODEL_PENRYN); ␊ |
317 | ␉␉␉break;␊ |
318 | ␉␉␉␊ |
319 | ␉␉default:␊ |
320 | ␉␉␉patch_cpuid_set_info(kernelData, 0, 0);␊ |
321 | ␉␉␉break;␊ |
322 | ␉}␊ |
323 | }␊ |
324 | ␊ |
325 | void patch_cpuid_set_info(void* kernelData, UInt32 impersonateFamily, UInt8 impersonateModel)␊ |
326 | {␉␊ |
327 | ␉UInt8* bytes = (UInt8*)kernelData;␊ |
328 | ␉␊ |
329 | ␉kernSymbols_t *symbol = lookup_kernel_symbol("_cpuid_set_info");␊ |
330 | ␉UInt32 patchLocation = symbol ? symbol->addr - textAddress + textSection: 0; //␉(kernelSymbolAddresses[SYMBOL_CPUID_SET_INFO] - textAddress + textSection);␊ |
331 | ␉␊ |
332 | ␉UInt32 jumpLocation = 0;␊ |
333 | ␉␊ |
334 | ␉␊ |
335 | ␉if(symbol == 0 || symbol->addr == 0)␊ |
336 | ␉{␊ |
337 | ␉␉printf("Unable to locate _cpuid_set_info\n");␊ |
338 | ␉␉return;␊ |
339 | ␉␉␊ |
340 | ␉}␊ |
341 | ␊ |
342 | ␉symbol = lookup_kernel_symbol("_panic");␊ |
343 | ␉UInt32 panicAddr = symbol ? symbol->addr - textAddress: 0; //kernelSymbolAddresses[SYMBOL_PANIC] - textAddress;␊ |
344 | ␉if(symbol == 0 || symbol->addr == 0)␊ |
345 | ␉{␊ |
346 | ␉␉printf("Unable to locate _panic\n");␊ |
347 | ␉␉return;␊ |
348 | ␉}␊ |
349 | ␉␊ |
350 | ␉patchLocation -= (UInt32)kernelData;␉// Remove offset␊ |
351 | ␉panicAddr -= (UInt32)kernelData;␊ |
352 | ␊ |
353 | ␊ |
354 | ␉␊ |
355 | ␉␊ |
356 | ␉␊ |
357 | ␉//TODO: don't assume it'll always work (Look for *next* function address in symtab and fail once it's been reached)␊ |
358 | ␉while( ␊ |
359 | ␉␉ (bytes[patchLocation -1] != 0xE8) ||␊ |
360 | ␉␉ ( ( (UInt32)(panicAddr - patchLocation - 4) + textSection ) != (UInt32)((bytes[patchLocation + 0] << 0 | ␊ |
361 | ␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉bytes[patchLocation + 1] << 8 | ␊ |
362 | ␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉bytes[patchLocation + 2] << 16 |␊ |
363 | ␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉bytes[patchLocation + 3] << 24)))␊ |
364 | ␉␉ )␊ |
365 | ␉{␊ |
366 | ␉␉patchLocation++;␊ |
367 | ␉}␊ |
368 | ␉patchLocation--;␊ |
369 | ␉␊ |
370 | ␉// Remove panic call, just in case the following patch routines fail␊ |
371 | ␉bytes[patchLocation + 0] = 0x90;␊ |
372 | ␉bytes[patchLocation + 1] = 0x90;␊ |
373 | ␉bytes[patchLocation + 2] = 0x90;␊ |
374 | ␉bytes[patchLocation + 3] = 0x90;␊ |
375 | ␉bytes[patchLocation + 4] = 0x90;␊ |
376 | ␉␊ |
377 | ␉␊ |
378 | ␉// Locate the jump call, so that 10 bytes can be reclamed.␊ |
379 | ␉// NOTE: This will *NOT* be located on pre 10.6.2 kernels␊ |
380 | ␉jumpLocation = patchLocation - 15;␊ |
381 | ␉while((bytes[jumpLocation - 1] != 0x77 ||␊ |
382 | ␉␉ bytes[jumpLocation] != (patchLocation - jumpLocation - -8)) &&␊ |
383 | ␉␉ (patchLocation - jumpLocation) < 0xF0)␊ |
384 | ␉{␊ |
385 | ␉␉jumpLocation--;␊ |
386 | ␉}␊ |
387 | ␉␊ |
388 | ␉// If found... AND we want to impersonate a specific cpumodel / family...␊ |
389 | ␉if(impersonateFamily &&␊ |
390 | ␉ impersonateModel &&␊ |
391 | ␉ ((patchLocation - jumpLocation) < 0xF0))␊ |
392 | ␉{␊ |
393 | ␉␉␊ |
394 | ␉␉bytes[jumpLocation] -= 10;␉␉// sizeof(movl␉$0x6b5a4cd2,0x00872eb4) = 10bytes␊ |
395 | ␉␉␊ |
396 | ␉␉/* ␊ |
397 | ␉␉ * Inpersonate the specified CPU FAMILY and CPU Model␊ |
398 | ␉␉ */␊ |
399 | ␊ |
400 | ␉␉// bytes[patchLocation - 17] = 0xC7;␉// already here... not needed to be done␊ |
401 | ␉␉// bytes[patchLocation - 16] = 0x05;␉// see above␊ |
402 | ␉␉UInt32 cpuid_cpufamily_addr =␉bytes[patchLocation - 15] << 0 |␊ |
403 | ␉␉␉␉␉␉␉␉␉␉bytes[patchLocation - 14] << 8 |␊ |
404 | ␉␉␉␉␉␉␉␉␉␉bytes[patchLocation - 13] << 16 |␊ |
405 | ␉␉␉␉␉␉␉␉␉␉bytes[patchLocation - 12] << 24;␊ |
406 | ␉␉␊ |
407 | ␉␉// NOTE: may change, determined based on cpuid_info struct␊ |
408 | ␉␉UInt32 cpuid_model_addr = cpuid_cpufamily_addr - 299; ␊ |
409 | ␉␉␊ |
410 | ␉␉␊ |
411 | ␉␉// cpufamily = CPUFAMILY_INTEL_PENRYN␊ |
412 | ␉␉bytes[patchLocation - 11] = (impersonateFamily & 0x000000FF) >> 0;␊ |
413 | ␉␉bytes[patchLocation - 10] = (impersonateFamily & 0x0000FF00) >> 8;␊ |
414 | ␉␉bytes[patchLocation - 9] = (impersonateFamily & 0x00FF0000) >> 16;␉␊ |
415 | ␉␉bytes[patchLocation - 8] = (impersonateFamily & 0xFF000000) >> 24;␊ |
416 | ␉␉␊ |
417 | ␉␉// NOPS, just in case if the jmp call wasn't patched, we'll jump to a␊ |
418 | ␉␉// nop and continue with the rest of the patch␊ |
419 | ␉␉// Yay two free bytes :), 10 more can be reclamed if needed, as well as a few␊ |
420 | ␉␉// from the above code (only cpuid_model needs to be set.␊ |
421 | ␉␉bytes[patchLocation - 7] = 0x90;␊ |
422 | ␉␉bytes[patchLocation - 6] = 0x90;␊ |
423 | ␉␉␊ |
424 | ␉␉bytes[patchLocation - 5] = 0xC7;␊ |
425 | ␉␉bytes[patchLocation - 4] = 0x05;␊ |
426 | ␉␉bytes[patchLocation - 3] = (cpuid_model_addr & 0x000000FF) >> 0;␊ |
427 | ␉␉bytes[patchLocation - 2] = (cpuid_model_addr & 0x0000FF00) >> 8;␉␊ |
428 | ␉␉bytes[patchLocation - 1] = (cpuid_model_addr & 0x00FF0000) >> 16;␊ |
429 | ␉␉bytes[patchLocation - 0] = (cpuid_model_addr & 0xFF000000) >> 24;␊ |
430 | ␉␉␊ |
431 | ␉␉// Note: I could have just copied the 8bit cpuid_model in and saved about 4 bytes␊ |
432 | ␉␉// so if this function need a different patch it's still possible. Also, about ten bytes previous can be freed.␊ |
433 | ␉␉bytes[patchLocation + 1] = impersonateModel;␉// cpuid_model␊ |
434 | ␉␉bytes[patchLocation + 2] = 0x01;␉// cpuid_extmodel␊ |
435 | ␉␉bytes[patchLocation + 3] = 0x00;␉// cpuid_extfamily␊ |
436 | ␉␉bytes[patchLocation + 4] = 0x02;␉// cpuid_stepping␊ |
437 | ␉␉␊ |
438 | ␉}␊ |
439 | ␉else if(impersonateFamily && impersonateModel)␊ |
440 | ␉{␊ |
441 | ␉␉// pre 10.6.2 kernel␊ |
442 | ␉␉// Locate the jump to directly *after* the panic call,␊ |
443 | ␉␉jumpLocation = patchLocation - 4;␊ |
444 | ␉␉while((bytes[jumpLocation - 1] != 0x77 ||␊ |
445 | ␉␉␉ bytes[jumpLocation] != (patchLocation - jumpLocation + 4)) &&␊ |
446 | ␉␉␉ (patchLocation - jumpLocation) < 0x20)␊ |
447 | ␉␉{␊ |
448 | ␉␉␉jumpLocation--;␊ |
449 | ␉␉}␊ |
450 | ␉␉// NOTE above isn't needed (I was going to use it, but I'm not, so instead,␊ |
451 | ␉␉// I'll just leave it to verify the binary stucture.␊ |
452 | ␉␉␊ |
453 | ␉␉// NOTE: the cpumodel_familt data is not set in _cpuid_set_info␊ |
454 | ␉␉// so we don't need to set it here, I'll get set later based on the model␊ |
455 | ␉␉// we set now.␊ |
456 | ␉␉␊ |
457 | ␉␉if((patchLocation - jumpLocation) < 0x20)␊ |
458 | ␉␉{␊ |
459 | ␉␉␉UInt32 cpuid_model_addr =␉(bytes[patchLocation - 14] << 0 |␊ |
460 | ␉␉␉␉␉␉␉␉␉␉␉bytes[patchLocation - 13] << 8 |␊ |
461 | ␉␉␉␉␉␉␉␉␉␉␉bytes[patchLocation - 12] << 16 |␊ |
462 | ␉␉␉␉␉␉␉␉␉␉␉bytes[patchLocation - 11] << 24);␊ |
463 | ␉␉␉// Remove jump␊ |
464 | ␉␉␉bytes[patchLocation - 9] = 0x90;␉␉/// Was a jump if supported cpu␊ |
465 | ␉␉␉bytes[patchLocation - 8] = 0x90;␉␉// jumped past the panic call, we want to override the panic␊ |
466 | ␊ |
467 | ␉␉␉bytes[patchLocation - 7] = 0x90;␊ |
468 | ␉␉␉bytes[patchLocation - 6] = 0x90;␊ |
469 | ␉␉␉␊ |
470 | ␉␉␉bytes[patchLocation - 5] = 0xC7;␊ |
471 | ␉␉␉bytes[patchLocation - 4] = 0x05;␊ |
472 | ␉␉␉bytes[patchLocation - 3] = (cpuid_model_addr & 0x000000FF) >> 0;␊ |
473 | ␉␉␉bytes[patchLocation - 2] = (cpuid_model_addr & 0x0000FF00) >> 8;␉␊ |
474 | ␉␉␉bytes[patchLocation - 1] = (cpuid_model_addr & 0x00FF0000) >> 16;␊ |
475 | ␉␉␉bytes[patchLocation - 0] = (cpuid_model_addr & 0xFF000000) >> 24;␊ |
476 | ␉␉␉␊ |
477 | ␉␉␉// Note: I could have just copied the 8bit cpuid_model in and saved about 4 bytes␊ |
478 | ␉␉␉// so if this function need a different patch it's still possible. Also, about ten bytes previous can be freed.␊ |
479 | ␉␉␉bytes[patchLocation + 1] = impersonateModel;␉// cpuid_model␊ |
480 | ␉␉␉bytes[patchLocation + 2] = 0x01;␉// cpuid_extmodel␊ |
481 | ␉␉␉bytes[patchLocation + 3] = 0x00;␉// cpuid_extfamily␊ |
482 | ␉␉␉bytes[patchLocation + 4] = 0x02;␉// cpuid_stepping␊ |
483 | ␉␉␉␊ |
484 | ␉␉␉␊ |
485 | ␉␉␉␊ |
486 | ␉␉␉patchLocation = jumpLocation;␊ |
487 | ␉␉␉// We now have 14 bytes available for a patch␊ |
488 | ␉␉␉␊ |
489 | ␉␉}␊ |
490 | ␉␉else ␊ |
491 | ␉␉{␊ |
492 | ␉␉␉// Patching failed, using NOP replacement done initialy␊ |
493 | ␉␉}␊ |
494 | ␉}␊ |
495 | ␉else ␊ |
496 | ␉{␊ |
497 | ␉␉// Either We were unable to change the jump call due to the function's sctructure␊ |
498 | ␉␉// changing, or the user did not request a patch. As such, resort to just ␊ |
499 | ␉␉// removing the panic call (using NOP replacement above). Note that the␊ |
500 | ␉␉// IntelCPUPM kext may still panic due to the cpu's Model ID not being patched␊ |
501 | ␉}␊ |
502 | }␊ |
503 | ␊ |
504 | ␊ |
505 | /**␊ |
506 | ** SleepEnabler.kext replacement (for those that need it)␊ |
507 | ** Located the KERN_INVALID_ARGUMENT return and replace it with KERN_SUCCESS␊ |
508 | **/␊ |
509 | void patch_pmCPUExitHaltToOff(void* kernelData)␊ |
510 | {␊ |
511 | ␉UInt8* bytes = (UInt8*)kernelData;␊ |
512 | ␊ |
513 | ␉kernSymbols_t *symbol = lookup_kernel_symbol("_PmCpuExitHaltToOff");␊ |
514 | ␉UInt32 patchLocation = symbol ? symbol->addr - textAddress + textSection: 0;␊ |
515 | ␉␊ |
516 | ␉if(symbol == 0 || symbol->addr == 0)␊ |
517 | ␉{␊ |
518 | ␉␉printf("Unable to locate _pmCPUExitHaltToOff\n");␊ |
519 | ␉␉return;␊ |
520 | ␉}␊ |
521 | ␉␊ |
522 | ␉patchLocation -= (UInt32)kernelData;␉// Remove offset␊ |
523 | ␉␊ |
524 | ␉␊ |
525 | ␉␊ |
526 | ␉while(bytes[patchLocation - 1]␉!= 0xB8 ||␊ |
527 | ␉␉ bytes[patchLocation]␉␉!= 0x04 ||␉// KERN_INVALID_ARGUMENT (0x00000004)␊ |
528 | ␉␉ bytes[patchLocation + 1]␉!= 0x00 ||␉// KERN_INVALID_ARGUMENT␊ |
529 | ␉␉ bytes[patchLocation + 2]␉!= 0x00 ||␉// KERN_INVALID_ARGUMENT␊ |
530 | ␉␉ bytes[patchLocation + 3]␉!= 0x00)␉// KERN_INVALID_ARGUMENT␊ |
531 | ␊ |
532 | ␉{␊ |
533 | ␉␉patchLocation++;␊ |
534 | ␉}␊ |
535 | ␉bytes[patchLocation] = 0x00;␉// KERN_SUCCESS;␊ |
536 | }␊ |
537 | ␊ |
538 | void patch_lapic_init(void* kernelData)␊ |
539 | {␊ |
540 | ␊ |
541 | ␉UInt8 panicIndex = 0;␊ |
542 | ␉UInt8* bytes = (UInt8*)kernelData;␊ |
543 | ␉␊ |
544 | ␉kernSymbols_t *symbol = lookup_kernel_symbol("_lapic_init");␊ |
545 | ␉UInt32 patchLocation = symbol ? symbol->addr - textAddress + textSection: 0; ␊ |
546 | ␉if(symbol == 0 || symbol->addr == 0)␊ |
547 | ␉{␊ |
548 | ␉␉printf("Unable to locate %s\n", "_lapic_init");␊ |
549 | ␉␉return;␊ |
550 | ␉␉␊ |
551 | ␉}␊ |
552 | ␉␊ |
553 | ␉symbol = lookup_kernel_symbol("_panic");␊ |
554 | ␉UInt32 panicAddr = symbol ? symbol->addr - textAddress: 0; ␊ |
555 | ␉if(symbol == 0 || symbol->addr == 0)␊ |
556 | ␉{␊ |
557 | ␉␉printf("Unable to locate %s\n", "_panic");␊ |
558 | ␉␉return;␊ |
559 | ␉}␊ |
560 | ␉␊ |
561 | ␉patchLocation -= (UInt32)kernelData;␉// Remove offset␊ |
562 | ␉panicAddr -= (UInt32)kernelData;␉// Remove offset␊ |
563 | ␊ |
564 | ␉␊ |
565 | ␉␊ |
566 | ␉␊ |
567 | ␉// Locate the (panicIndex + 1) panic call␊ |
568 | ␉while(panicIndex < 3)␉// Find the third panic call␊ |
569 | ␉{␊ |
570 | ␉␉while( ␊ |
571 | ␉␉␉ (bytes[patchLocation -1] != 0xE8) ||␊ |
572 | ␉␉␉ ( ( (UInt32)(panicAddr - patchLocation - 4) + textSection ) != (UInt32)((bytes[patchLocation + 0] << 0 | ␊ |
573 | ␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉bytes[patchLocation + 1] << 8 | ␊ |
574 | ␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉bytes[patchLocation + 2] << 16 |␊ |
575 | ␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉bytes[patchLocation + 3] << 24)))␊ |
576 | ␉␉␉ )␊ |
577 | ␉␉{␊ |
578 | ␉␉␉patchLocation++;␊ |
579 | ␉␉}␊ |
580 | ␉␉patchLocation++;␊ |
581 | ␉␉panicIndex++;␊ |
582 | ␉}␊ |
583 | ␉patchLocation--;␉// Remove extra increment from the < 3 while loop␊ |
584 | ␉␊ |
585 | ␉bytes[--patchLocation] = 0x90;␉␊ |
586 | ␉bytes[++patchLocation] = 0x90;␊ |
587 | ␉bytes[++patchLocation] = 0x90;␊ |
588 | ␉bytes[++patchLocation] = 0x90;␊ |
589 | ␉bytes[++patchLocation] = 0x90;␊ |
590 | ␉␊ |
591 | ␉␊ |
592 | }␊ |
593 | ␊ |
594 | ␊ |
595 | void patch_commpage_stuff_routine(void* kernelData)␊ |
596 | {␊ |
597 | ␉UInt8* bytes = (UInt8*)kernelData;␊ |
598 | ␊ |
599 | ␉kernSymbols_t *symbol = lookup_kernel_symbol("_commpage_stuff_routine");␊ |
600 | ␉if(symbol == 0 || symbol->addr == 0)␊ |
601 | ␉{␊ |
602 | ␉␉printf("Unable to locate %s\n", "_commpage_stuff_routine");␊ |
603 | ␉␉return;␊ |
604 | ␉␉␊ |
605 | ␉}␊ |
606 | ␉␊ |
607 | ␉UInt32 patchLocation = symbol->addr - textAddress + textSection; ␊ |
608 | ␊ |
609 | ␉␊ |
610 | ␉symbol = lookup_kernel_symbol("_panic");␊ |
611 | ␉if(symbol == 0 || symbol->addr == 0)␊ |
612 | ␉{␊ |
613 | ␉␉printf("Unable to locate %s\n", "_panic");␊ |
614 | ␉␉return;␊ |
615 | ␉}␊ |
616 | ␉UInt32 panicAddr = symbol->addr - textAddress; ␊ |
617 | ␊ |
618 | ␉patchLocation -= (UInt32)kernelData;␊ |
619 | ␉panicAddr -= (UInt32)kernelData;␊ |
620 | ␉␊ |
621 | ␉while( ␊ |
622 | ␉␉ (bytes[patchLocation -1] != 0xE8) ||␊ |
623 | ␉␉ ( ( (UInt32)(panicAddr - patchLocation - 4) + textSection ) != (UInt32)((bytes[patchLocation + 0] << 0 | ␊ |
624 | ␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉bytes[patchLocation + 1] << 8 | ␊ |
625 | ␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉bytes[patchLocation + 2] << 16 |␊ |
626 | ␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉bytes[patchLocation + 3] << 24)))␊ |
627 | ␉␉ )␊ |
628 | ␉{␊ |
629 | ␉␉patchLocation++;␊ |
630 | ␉}␊ |
631 | ␉patchLocation--;␊ |
632 | ␉␊ |
633 | ␉// Replace panic with nops␊ |
634 | ␉bytes[patchLocation + 0] = 0x90;␊ |
635 | ␉bytes[patchLocation + 1] = 0x90;␊ |
636 | ␉bytes[patchLocation + 2] = 0x90;␊ |
637 | ␉bytes[patchLocation + 3] = 0x90;␊ |
638 | ␉bytes[patchLocation + 4] = 0x90;␊ |
639 | ␉␊ |
640 | ␉␊ |
641 | }␊ |
642 | ␊ |
643 | void patch_lapic_interrupt(void* kernelData)␊ |
644 | {␊ |
645 | ␉// NOTE: this is a hack untill I finish patch_lapic_configure␊ |
646 | ␉UInt8* bytes = (UInt8*)kernelData;␊ |
647 | ␉␊ |
648 | ␉kernSymbols_t *symbol = lookup_kernel_symbol("_lapic_interrupt");␊ |
649 | ␉if(symbol == 0 || symbol->addr == 0)␊ |
650 | ␉{␊ |
651 | ␉␉printf("Unable to locate %s\n", "_lapic_interrupt");␊ |
652 | ␉␉return;␊ |
653 | ␉␉␊ |
654 | ␉}␊ |
655 | ␉␊ |
656 | ␉UInt32 patchLocation = symbol->addr - textAddress + textSection; ␊ |
657 | ␉␊ |
658 | ␉␊ |
659 | ␉symbol = lookup_kernel_symbol("_panic");␊ |
660 | ␉if(symbol == 0 || symbol->addr == 0)␊ |
661 | ␉{␊ |
662 | ␉␉printf("Unable to locate %s\n", "_panic");␊ |
663 | ␉␉return;␊ |
664 | ␉}␊ |
665 | ␉UInt32 panicAddr = symbol->addr - textAddress; ␊ |
666 | ␉␊ |
667 | ␉patchLocation -= (UInt32)kernelData;␊ |
668 | ␉panicAddr -= (UInt32)kernelData;␊ |
669 | ␉␊ |
670 | ␉while( ␊ |
671 | ␉␉ (bytes[patchLocation -1] != 0xE8) ||␊ |
672 | ␉␉ ( ( (UInt32)(panicAddr - patchLocation - 4) + textSection ) != (UInt32)((bytes[patchLocation + 0] << 0 | ␊ |
673 | ␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉bytes[patchLocation + 1] << 8 | ␊ |
674 | ␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉bytes[patchLocation + 2] << 16 |␊ |
675 | ␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉bytes[patchLocation + 3] << 24)))␊ |
676 | ␉␉ )␊ |
677 | ␉{␊ |
678 | ␉␉patchLocation++;␊ |
679 | ␉}␊ |
680 | ␉patchLocation--;␊ |
681 | ␉␊ |
682 | ␉// Replace panic with nops␊ |
683 | ␉bytes[patchLocation + 0] = 0x90;␊ |
684 | ␉bytes[patchLocation + 1] = 0x90;␊ |
685 | ␉bytes[patchLocation + 2] = 0x90;␊ |
686 | ␉bytes[patchLocation + 3] = 0x90;␊ |
687 | ␉bytes[patchLocation + 4] = 0x90;␊ |
688 | ␉␊ |
689 | ␉␊ |
690 | }␊ |
691 | ␊ |
692 | ␊ |
693 | void patch_lapic_configure(void* kernelData)␊ |
694 | {␊ |
695 | ␉UInt8* bytes = (UInt8*)kernelData;␊ |
696 | ␉␊ |
697 | ␉UInt32 patchLocation;␊ |
698 | ␉UInt32 lapicStart;␊ |
699 | ␉UInt32 lapicInterruptBase;␊ |
700 | ␉␊ |
701 | ␉kernSymbols_t *symbol = lookup_kernel_symbol("_lapic_configure");␊ |
702 | ␉if(symbol == 0 || symbol->addr == 0)␊ |
703 | ␉{␊ |
704 | ␉␉printf("Unable to locate %s\n", "_lapic_configure");␊ |
705 | ␉␉return;␊ |
706 | ␉}␊ |
707 | ␉patchLocation = symbol->addr - textAddress + textSection; ␊ |
708 | ␉␊ |
709 | ␉symbol = lookup_kernel_symbol("_lapic_start");␊ |
710 | ␉if(symbol == 0 || symbol->addr == 0)␊ |
711 | ␉{␊ |
712 | ␉␉printf("Unable to locate %s\n", "_lapic_start");␊ |
713 | ␉␉return;␊ |
714 | ␉}␊ |
715 | ␉lapicStart = symbol->addr; ␊ |
716 | ␊ |
717 | ␊ |
718 | ␉symbol = lookup_kernel_symbol("_lapic_interrupt_base");␊ |
719 | ␉if(symbol == 0 || symbol->addr == 0)␊ |
720 | ␉{␊ |
721 | ␉␉printf("Unable to locate %s\n", "_lapic_interrupt_base");␊ |
722 | ␉␉return;␊ |
723 | ␉}␊ |
724 | ␉lapicInterruptBase = symbol->addr;␊ |
725 | ␉patchLocation -= (UInt32)kernelData;␊ |
726 | ␉lapicStart -= (UInt32)kernelData;␊ |
727 | ␉lapicInterruptBase -= (UInt32)kernelData;␊ |
728 | ␉␊ |
729 | ␉␊ |
730 | ␉// Looking for the following:␊ |
731 | ␉//movl _lapic_start,%e_x␊ |
732 | ␉//addl $0x00000320,%e_x␊ |
733 | ␉// 8b 15 __ __ __ __ 81 c2 20 03 00 00␊ |
734 | ␉while( ␊ |
735 | ␉␉ (bytes[patchLocation - 2] != 0x8b) ||␊ |
736 | ␉␉ //bytes[patchLocation -1] != 0x15) ||␉// Register, we don't care what it is␊ |
737 | ␉␉ ( lapicStart != (UInt32)(␊ |
738 | ␉␉␉␉␉␉␉␉␉(bytes[patchLocation + 0] << 0 | ␊ |
739 | ␉␉␉␉␉␉␉␉␉ bytes[patchLocation + 1] << 8 | ␊ |
740 | ␉␉␉␉␉␉␉␉␉ bytes[patchLocation + 2] << 16 |␊ |
741 | ␉␉␉␉␉␉␉␉␉ bytes[patchLocation + 3] << 24␊ |
742 | ␉␉␉␉␉␉␉␉␉)␊ |
743 | ␉␉␉␉␉␉␉␉ )␊ |
744 | ␉␉ ) || ␊ |
745 | ␉␉ (bytes[patchLocation + 4 ] != 0x81) ||␊ |
746 | ␉␉ //(bytes[patchLocation + 5 ] != 0Cx2) ||␉// register␊ |
747 | ␉␉ (bytes[patchLocation + 6 ] != 0x20) ||␊ |
748 | ␉␉ (bytes[patchLocation + 7 ] != 0x03) ||␊ |
749 | ␉␉ (bytes[patchLocation + 8 ] != 0x00) ||␊ |
750 | ␉␉ (bytes[patchLocation + 9] != 0x00)␊ |
751 | ␊ |
752 | ␉␉ )␊ |
753 | ␉{␊ |
754 | ␉␉patchLocation++;␊ |
755 | ␉}␊ |
756 | ␉patchLocation-=2;␊ |
757 | ␊ |
758 | ␉// NOTE: this is currently hardcoded, change it to be more resilient to changes␊ |
759 | ␉// At a minimum, I should have this do a cheksup first and if not matching, remove the panic instead.␊ |
760 | ␉␊ |
761 | ␉// 8b 15 __ __ __ __ -> movl␉␉ _lapic_start,%edx (NOTE: this should already be here)␊ |
762 | ␉/*␊ |
763 | ␉bytes[patchLocation++] = 0x8B;␊ |
764 | ␉bytes[patchLocation++] = 0x15;␊ |
765 | ␉bytes[patchLocation++] = (lapicStart & 0x000000FF) >> 0;␊ |
766 | ␉bytes[patchLocation++] = (lapicStart & 0x0000FF00) >> 8;␊ |
767 | ␉bytes[patchLocation++] = (lapicStart & 0x00FF0000) >> 16;␊ |
768 | ␉bytes[patchLocation++] = (lapicStart & 0xFF000000) >> 24;␊ |
769 | ␉*/␊ |
770 | ␉patchLocation += 6;␊ |
771 | ␉␊ |
772 | ␉// 81 c2 60 03 00 00 -> addl␉␉ $0x00000320,%edx␊ |
773 | ␉/*␊ |
774 | ␉bytes[patchLocation++] = 0x81;␊ |
775 | ␉bytes[patchLocation++] = 0xC2;␊ |
776 | ␉*/␊ |
777 | ␉patchLocation += 2;␊ |
778 | ␉bytes[patchLocation++] = 0x60;␊ |
779 | ␉/*␊ |
780 | ␉bytes[patchLocation++];// = 0x03;␊ |
781 | ␉bytes[patchLocation++];// = 0x00;␊ |
782 | ␉bytes[patchLocation++];// = 0x00;␊ |
783 | ␉*/␊ |
784 | ␉ patchLocation += 3;␊ |
785 | ␊ |
786 | ␉// c7 02 00 04 00 00 -> movl␉␉ $0x00000400,(%edx)␊ |
787 | ␉bytes[patchLocation++] = 0xC7;␊ |
788 | ␉bytes[patchLocation++] = 0x02;␊ |
789 | ␉bytes[patchLocation++] = 0x00;␊ |
790 | ␉bytes[patchLocation++] = 0x04;␊ |
791 | ␉bytes[patchLocation++] = 0x00;␊ |
792 | ␉bytes[patchLocation++] = 0x00;␊ |
793 | ␉␊ |
794 | ␉// 83 ea 40 -> subl␉␉ $0x40,edx␊ |
795 | ␉bytes[patchLocation++] = 0x83;␊ |
796 | ␉bytes[patchLocation++] = 0xEA;␊ |
797 | ␉bytes[patchLocation++] = 0x40;␊ |
798 | ␊ |
799 | ␉// a1 __ __ __ __ -> movl␉␉ _lapic_interrupt_base,%eax␊ |
800 | ␉bytes[patchLocation++] = 0xA1;␊ |
801 | ␉bytes[patchLocation++] = (lapicInterruptBase & 0x000000FF) >> 0;␊ |
802 | ␉bytes[patchLocation++] = (lapicInterruptBase & 0x0000FF00) >> 8;␊ |
803 | ␉bytes[patchLocation++] = (lapicInterruptBase & 0x00FF0000) >> 16;␊ |
804 | ␉bytes[patchLocation++] = (lapicInterruptBase & 0xFF000000) >> 24;␊ |
805 | ␊ |
806 | ␉// 83 c0 0e -> addl␉␉ $0x0e,%eax␊ |
807 | ␉bytes[patchLocation++] = 0x83;␊ |
808 | ␉bytes[patchLocation++] = 0xC0;␊ |
809 | ␉bytes[patchLocation++] = 0x0E;␊ |
810 | ␊ |
811 | ␉// 89 02 -> movl␉␉ %eax,(%edx)␊ |
812 | ␉bytes[patchLocation++] = 0x89;␊ |
813 | ␉bytes[patchLocation++] = 0x02;␊ |
814 | ␉␊ |
815 | ␉// 81c230030000␉␉ addl␉␉ $0x00000330,%edx␊ |
816 | ␉bytes[patchLocation++] = 0x81;␊ |
817 | ␉bytes[patchLocation++] = 0xC2;␊ |
818 | ␉bytes[patchLocation++] = 0x30;␊ |
819 | ␉bytes[patchLocation++] = 0x03;␊ |
820 | ␉bytes[patchLocation++] = 0x00;␊ |
821 | ␉bytes[patchLocation++] = 0x00;␊ |
822 | ␉␊ |
823 | ␉// a1 __ __ __ __ -> movl␉␉ _lapic_interrupt_base,%eax␊ |
824 | ␉bytes[patchLocation++] = 0xA1;␊ |
825 | ␉bytes[patchLocation++] = (lapicInterruptBase & 0x000000FF) >> 0;␊ |
826 | ␉bytes[patchLocation++] = (lapicInterruptBase & 0x0000FF00) >> 8;␊ |
827 | ␉bytes[patchLocation++] = (lapicInterruptBase & 0x00FF0000) >> 16;␊ |
828 | ␉bytes[patchLocation++] = (lapicInterruptBase & 0xFF000000) >> 24;␊ |
829 | ␉␊ |
830 | ␉// 83 c0 0f -> addl␉␉ $0x0f,%eax␊ |
831 | ␉bytes[patchLocation++] = 0x83;␊ |
832 | ␉bytes[patchLocation++] = 0xC0;␊ |
833 | ␉bytes[patchLocation++] = 0x0F;␊ |
834 | ␉␊ |
835 | ␉// 89 02 -> movl␉␉ %eax,(%edx)␊ |
836 | ␉bytes[patchLocation++] = 0x89;␊ |
837 | ␉bytes[patchLocation++] = 0x02;␊ |
838 | ␉␊ |
839 | ␉// 83 ea 10 -> subl␉␉ $0x10,edx␊ |
840 | ␉bytes[patchLocation++] = 0x83;␊ |
841 | ␉bytes[patchLocation++] = 0xEA;␊ |
842 | ␉bytes[patchLocation++] = 0x10;␊ |
843 | ␊ |
844 | ␉// a1 __ __ __ __ -> movl␉␉ _lapic_interrupt_base,%eax␊ |
845 | ␉bytes[patchLocation++] = 0xA1;␊ |
846 | ␉bytes[patchLocation++] = (lapicInterruptBase & 0x000000FF) >> 0;␊ |
847 | ␉bytes[patchLocation++] = (lapicInterruptBase & 0x0000FF00) >> 8;␊ |
848 | ␉bytes[patchLocation++] = (lapicInterruptBase & 0x00FF0000) >> 16;␊ |
849 | ␉bytes[patchLocation++] = (lapicInterruptBase & 0xFF000000) >> 24;␊ |
850 | ␉␊ |
851 | ␉// 83 c0 0c -> addl␉␉ $0x0c,%eax␊ |
852 | ␉bytes[patchLocation++] = 0x83;␊ |
853 | ␉bytes[patchLocation++] = 0xC0;␊ |
854 | ␉bytes[patchLocation++] = 0x0C;␊ |
855 | ␊ |
856 | ␉// 89 02 -> movl␉␉ %eax,(%edx)␊ |
857 | ␉bytes[patchLocation++] = 0x89;␊ |
858 | ␉bytes[patchLocation++] = 0x02;␊ |
859 | ␊ |
860 | ␉// Replace remaining with nops␊ |
861 | ␊ |
862 | ␊ |
863 | ␉bytes[patchLocation++] = 0x90;␊ |
864 | ␉bytes[patchLocation++] = 0x90;␊ |
865 | ␉bytes[patchLocation++] = 0x90;␊ |
866 | ␉bytes[patchLocation++] = 0x90;␊ |
867 | ␉//␉bytes[patchLocation++] = 0x90; // double check the lenght of the patch...␊ |
868 | ␉//␉bytes[patchLocation++] = 0x90;␊ |
869 | }␊ |
870 | |