10 |
|
* tjw@omnigroup.com Sun, 4 Jun 2000 |
11 |
|
* www.omnigroup.com/mailman/archive/macosx-dev/2000-June/002030.html |
12 |
|
* |
13 |
< |
* Basilisk II (C) 1997-2004 Christian Bauer |
13 |
> |
* Basilisk II (C) 1997-2005 Christian Bauer |
14 |
|
* |
15 |
|
* This program is free software; you can redistribute it and/or modify |
16 |
|
* it under the terms of the GNU General Public License as published by |
97 |
|
char ra, rd; |
98 |
|
}; |
99 |
|
|
100 |
< |
static void powerpc_decode_instruction(instruction_t *instruction, unsigned int nip, unsigned int * gpr) |
100 |
> |
static void powerpc_decode_instruction(instruction_t *instruction, unsigned int nip, unsigned long * gpr) |
101 |
|
{ |
102 |
|
// Get opcode and divide into fields |
103 |
< |
unsigned int opcode = *((unsigned int *)nip); |
103 |
> |
unsigned int opcode = *((unsigned int *)(unsigned long)nip); |
104 |
|
unsigned int primop = opcode >> 26; |
105 |
|
unsigned int exop = (opcode >> 1) & 0x3ff; |
106 |
|
unsigned int ra = (opcode >> 16) & 0x1f; |
174 |
|
transfer_type = SIGSEGV_TRANSFER_STORE; transfer_size = SIZE_WORD; addr_mode = MODE_NORM; break; |
175 |
|
case 45: // sthu |
176 |
|
transfer_type = SIGSEGV_TRANSFER_STORE; transfer_size = SIZE_WORD; addr_mode = MODE_U; break; |
177 |
+ |
case 58: // ld, ldu, lwa |
178 |
+ |
transfer_type = SIGSEGV_TRANSFER_LOAD; |
179 |
+ |
transfer_size = SIZE_QUAD; |
180 |
+ |
addr_mode = ((opcode & 3) == 1) ? MODE_U : MODE_NORM; |
181 |
+ |
imm &= ~3; |
182 |
+ |
break; |
183 |
+ |
case 62: // std, stdu, stq |
184 |
+ |
transfer_type = SIGSEGV_TRANSFER_STORE; |
185 |
+ |
transfer_size = SIZE_QUAD; |
186 |
+ |
addr_mode = ((opcode & 3) == 1) ? MODE_U : MODE_NORM; |
187 |
+ |
imm &= ~3; |
188 |
+ |
break; |
189 |
|
} |
190 |
|
|
191 |
|
// Calculate effective address |
226 |
|
|
227 |
|
#if HAVE_SIGINFO_T |
228 |
|
// Generic extended signal handler |
229 |
< |
#if defined(__NetBSD__) || defined(__FreeBSD__) |
229 |
> |
#if defined(__FreeBSD__) |
230 |
|
#define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGBUS) |
231 |
|
#else |
232 |
|
#define SIGSEGV_ALL_SIGNALS FAULT_HANDLER(SIGSEGV) |
256 |
|
#define SIGSEGV_REGISTER_FILE ((unsigned long *)SIGSEGV_CONTEXT_REGS), SIGSEGV_SPARC_GWINDOWS, SIGSEGV_SPARC_RWINDOW |
257 |
|
#define SIGSEGV_SKIP_INSTRUCTION sparc_skip_instruction |
258 |
|
#endif |
259 |
+ |
#if defined(__i386__) |
260 |
+ |
#include <sys/regset.h> |
261 |
+ |
#define SIGSEGV_CONTEXT_REGS (((ucontext_t *)scp)->uc_mcontext.gregs) |
262 |
+ |
#define SIGSEGV_FAULT_INSTRUCTION SIGSEGV_CONTEXT_REGS[EIP] |
263 |
+ |
#define SIGSEGV_REGISTER_FILE (unsigned long *)SIGSEGV_CONTEXT_REGS |
264 |
+ |
#define SIGSEGV_SKIP_INSTRUCTION ix86_skip_instruction |
265 |
+ |
#endif |
266 |
|
#endif |
267 |
< |
#if defined(__FreeBSD__) |
267 |
> |
#if defined(__FreeBSD__) || defined(__OpenBSD__) |
268 |
|
#if (defined(i386) || defined(__i386__)) |
269 |
|
#define SIGSEGV_FAULT_INSTRUCTION (((struct sigcontext *)scp)->sc_eip) |
270 |
|
#define SIGSEGV_REGISTER_FILE ((unsigned long *)&(((struct sigcontext *)scp)->sc_edi)) /* EDI is the first GPR (even below EIP) in sigcontext */ |
271 |
|
#define SIGSEGV_SKIP_INSTRUCTION ix86_skip_instruction |
272 |
|
#endif |
273 |
|
#endif |
274 |
+ |
#if defined(__NetBSD__) |
275 |
+ |
#if (defined(i386) || defined(__i386__)) |
276 |
+ |
#include <sys/ucontext.h> |
277 |
+ |
#define SIGSEGV_CONTEXT_REGS (((ucontext_t *)scp)->uc_mcontext.__gregs) |
278 |
+ |
#define SIGSEGV_FAULT_INSTRUCTION SIGSEGV_CONTEXT_REGS[_REG_EIP] |
279 |
+ |
#define SIGSEGV_REGISTER_FILE (unsigned long *)SIGSEGV_CONTEXT_REGS |
280 |
+ |
#define SIGSEGV_SKIP_INSTRUCTION ix86_skip_instruction |
281 |
+ |
#endif |
282 |
+ |
#if (defined(powerpc) || defined(__powerpc__)) |
283 |
+ |
#include <sys/ucontext.h> |
284 |
+ |
#define SIGSEGV_CONTEXT_REGS (((ucontext_t *)scp)->uc_mcontext.__gregs) |
285 |
+ |
#define SIGSEGV_FAULT_INSTRUCTION SIGSEGV_CONTEXT_REGS[_REG_PC] |
286 |
+ |
#define SIGSEGV_REGISTER_FILE (unsigned long *)&SIGSEGV_CONTEXT_REGS[_REG_PC], (unsigned long *)&SIGSEGV_CONTEXT_REGS[_REG_R0] |
287 |
+ |
#define SIGSEGV_SKIP_INSTRUCTION powerpc_skip_instruction |
288 |
+ |
#endif |
289 |
+ |
#endif |
290 |
|
#if defined(__linux__) |
291 |
|
#if (defined(i386) || defined(__i386__)) |
292 |
|
#include <sys/ucontext.h> |
309 |
|
#include <sys/ucontext.h> |
310 |
|
#define SIGSEGV_CONTEXT_REGS (((ucontext_t *)scp)->uc_mcontext.regs) |
311 |
|
#define SIGSEGV_FAULT_INSTRUCTION (SIGSEGV_CONTEXT_REGS->nip) |
312 |
< |
#define SIGSEGV_REGISTER_FILE (unsigned int *)&SIGSEGV_CONTEXT_REGS->nip, (unsigned int *)(SIGSEGV_CONTEXT_REGS->gpr) |
312 |
> |
#define SIGSEGV_REGISTER_FILE (unsigned long *)&SIGSEGV_CONTEXT_REGS->nip, (unsigned long *)(SIGSEGV_CONTEXT_REGS->gpr) |
313 |
|
#define SIGSEGV_SKIP_INSTRUCTION powerpc_skip_instruction |
314 |
|
#endif |
315 |
|
#if (defined(hppa) || defined(__hppa__)) |
320 |
|
#include <asm/ucontext.h> /* use kernel structure, glibc may not be in sync */ |
321 |
|
#define SIGSEGV_CONTEXT_REGS (((struct ucontext *)scp)->uc_mcontext) |
322 |
|
#define SIGSEGV_FAULT_INSTRUCTION (SIGSEGV_CONTEXT_REGS.arm_pc) |
323 |
+ |
#define SIGSEGV_REGISTER_FILE (&SIGSEGV_CONTEXT_REGS.arm_r0) |
324 |
+ |
#define SIGSEGV_SKIP_INSTRUCTION arm_skip_instruction |
325 |
|
#endif |
326 |
|
#endif |
327 |
|
#endif |
352 |
|
#define SIGSEGV_FAULT_HANDLER_ARGS sig, scp |
353 |
|
#define SIGSEGV_FAULT_ADDRESS scp->regs->dar |
354 |
|
#define SIGSEGV_FAULT_INSTRUCTION scp->regs->nip |
355 |
< |
#define SIGSEGV_REGISTER_FILE (unsigned int *)&scp->regs->nip, (unsigned int *)(scp->regs->gpr) |
355 |
> |
#define SIGSEGV_REGISTER_FILE (unsigned long *)&scp->regs->nip, (unsigned long *)(scp->regs->gpr) |
356 |
|
#define SIGSEGV_SKIP_INSTRUCTION powerpc_skip_instruction |
357 |
|
#endif |
358 |
|
#if (defined(alpha) || defined(__alpha__)) |
368 |
|
#define SIGSEGV_FAULT_HANDLER_ARGS &sc |
369 |
|
#define SIGSEGV_FAULT_ADDRESS scp->fault_address |
370 |
|
#define SIGSEGV_FAULT_INSTRUCTION scp->arm_pc |
371 |
+ |
#define SIGSEGV_REGISTER_FILE &scp->arm_r0 |
372 |
+ |
#define SIGSEGV_SKIP_INSTRUCTION arm_skip_instruction |
373 |
|
#endif |
374 |
|
#endif |
375 |
|
|
516 |
|
#endif |
517 |
|
#endif |
518 |
|
|
519 |
+ |
#if HAVE_WIN32_EXCEPTIONS |
520 |
+ |
#define WIN32_LEAN_AND_MEAN /* avoid including junk */ |
521 |
+ |
#include <windows.h> |
522 |
+ |
#include <winerror.h> |
523 |
+ |
|
524 |
+ |
#define SIGSEGV_FAULT_HANDLER_ARGLIST EXCEPTION_POINTERS *ExceptionInfo |
525 |
+ |
#define SIGSEGV_FAULT_HANDLER_ARGS ExceptionInfo |
526 |
+ |
#define SIGSEGV_FAULT_ADDRESS ExceptionInfo->ExceptionRecord->ExceptionInformation[1] |
527 |
+ |
#define SIGSEGV_CONTEXT_REGS ExceptionInfo->ContextRecord |
528 |
+ |
#define SIGSEGV_FAULT_INSTRUCTION SIGSEGV_CONTEXT_REGS->Eip |
529 |
+ |
#define SIGSEGV_REGISTER_FILE ((unsigned long *)&SIGSEGV_CONTEXT_REGS->Edi) |
530 |
+ |
#define SIGSEGV_SKIP_INSTRUCTION ix86_skip_instruction |
531 |
+ |
#endif |
532 |
+ |
|
533 |
|
#if HAVE_MACH_EXCEPTIONS |
534 |
|
|
535 |
|
// This can easily be extended to other Mach systems, but really who |
707 |
|
#endif |
708 |
|
}; |
709 |
|
#endif |
710 |
< |
#if defined(__NetBSD__) || defined(__FreeBSD__) |
710 |
> |
#if defined(__NetBSD__) |
711 |
> |
enum { |
712 |
> |
#if (defined(i386) || defined(__i386__)) |
713 |
> |
X86_REG_EIP = _REG_EIP, |
714 |
> |
X86_REG_EAX = _REG_EAX, |
715 |
> |
X86_REG_ECX = _REG_ECX, |
716 |
> |
X86_REG_EDX = _REG_EDX, |
717 |
> |
X86_REG_EBX = _REG_EBX, |
718 |
> |
X86_REG_ESP = _REG_ESP, |
719 |
> |
X86_REG_EBP = _REG_EBP, |
720 |
> |
X86_REG_ESI = _REG_ESI, |
721 |
> |
X86_REG_EDI = _REG_EDI |
722 |
> |
#endif |
723 |
> |
}; |
724 |
> |
#endif |
725 |
> |
#if defined(__FreeBSD__) |
726 |
|
enum { |
727 |
|
#if (defined(i386) || defined(__i386__)) |
728 |
|
X86_REG_EIP = 10, |
737 |
|
#endif |
738 |
|
}; |
739 |
|
#endif |
740 |
+ |
#if defined(__OpenBSD__) |
741 |
+ |
enum { |
742 |
+ |
#if defined(__i386__) |
743 |
+ |
// EDI is the first register we consider |
744 |
+ |
#define OREG(REG) offsetof(struct sigcontext, sc_##REG) |
745 |
+ |
#define DREG(REG) ((OREG(REG) - OREG(edi)) / 4) |
746 |
+ |
X86_REG_EIP = DREG(eip), // 7 |
747 |
+ |
X86_REG_EAX = DREG(eax), // 6 |
748 |
+ |
X86_REG_ECX = DREG(ecx), // 5 |
749 |
+ |
X86_REG_EDX = DREG(edx), // 4 |
750 |
+ |
X86_REG_EBX = DREG(ebx), // 3 |
751 |
+ |
X86_REG_ESP = DREG(esp), // 10 |
752 |
+ |
X86_REG_EBP = DREG(ebp), // 2 |
753 |
+ |
X86_REG_ESI = DREG(esi), // 1 |
754 |
+ |
X86_REG_EDI = DREG(edi) // 0 |
755 |
+ |
#undef DREG |
756 |
+ |
#undef OREG |
757 |
+ |
#endif |
758 |
+ |
}; |
759 |
+ |
#endif |
760 |
+ |
#if defined(__sun__) |
761 |
+ |
// Same as for Linux, need to check for x86-64 |
762 |
+ |
enum { |
763 |
+ |
#if defined(__i386__) |
764 |
+ |
X86_REG_EIP = EIP, |
765 |
+ |
X86_REG_EAX = EAX, |
766 |
+ |
X86_REG_ECX = ECX, |
767 |
+ |
X86_REG_EDX = EDX, |
768 |
+ |
X86_REG_EBX = EBX, |
769 |
+ |
X86_REG_ESP = ESP, |
770 |
+ |
X86_REG_EBP = EBP, |
771 |
+ |
X86_REG_ESI = ESI, |
772 |
+ |
X86_REG_EDI = EDI |
773 |
+ |
#endif |
774 |
+ |
}; |
775 |
+ |
#endif |
776 |
+ |
#if defined(_WIN32) |
777 |
+ |
enum { |
778 |
+ |
#if (defined(i386) || defined(__i386__)) |
779 |
+ |
X86_REG_EIP = 7, |
780 |
+ |
X86_REG_EAX = 5, |
781 |
+ |
X86_REG_ECX = 4, |
782 |
+ |
X86_REG_EDX = 3, |
783 |
+ |
X86_REG_EBX = 2, |
784 |
+ |
X86_REG_ESP = 10, |
785 |
+ |
X86_REG_EBP = 6, |
786 |
+ |
X86_REG_ESI = 1, |
787 |
+ |
X86_REG_EDI = 0 |
788 |
+ |
#endif |
789 |
+ |
}; |
790 |
+ |
#endif |
791 |
|
// FIXME: this is partly redundant with the instruction decoding phase |
792 |
|
// to discover transfer type and register number |
793 |
|
static inline int ix86_step_over_modrm(unsigned char * p) |
828 |
|
|
829 |
|
if (eip == 0) |
830 |
|
return false; |
831 |
+ |
#ifdef _WIN32 |
832 |
+ |
if (IsBadCodePtr((FARPROC)eip)) |
833 |
+ |
return false; |
834 |
+ |
#endif |
835 |
|
|
836 |
|
transfer_type_t transfer_type = SIGSEGV_TRANSFER_UNKNOWN; |
837 |
|
transfer_size_t transfer_size = SIZE_LONG; |
885 |
|
#endif |
886 |
|
|
887 |
|
// Decode instruction |
888 |
+ |
int target_size = SIZE_UNKNOWN; |
889 |
|
switch (eip[0]) { |
890 |
|
case 0x0f: |
891 |
+ |
target_size = transfer_size; |
892 |
|
switch (eip[1]) { |
893 |
+ |
case 0xbe: // MOVSX r32, r/m8 |
894 |
|
case 0xb6: // MOVZX r32, r/m8 |
895 |
+ |
transfer_size = SIZE_BYTE; |
896 |
+ |
goto do_mov_extend; |
897 |
+ |
case 0xbf: // MOVSX r32, r/m16 |
898 |
|
case 0xb7: // MOVZX r32, r/m16 |
899 |
< |
switch (eip[2] & 0xc0) { |
900 |
< |
case 0x80: |
901 |
< |
reg = (eip[2] >> 3) & 7; |
902 |
< |
transfer_type = SIGSEGV_TRANSFER_LOAD; |
903 |
< |
break; |
904 |
< |
case 0x40: |
905 |
< |
reg = (eip[2] >> 3) & 7; |
906 |
< |
transfer_type = SIGSEGV_TRANSFER_LOAD; |
907 |
< |
break; |
908 |
< |
case 0x00: |
909 |
< |
reg = (eip[2] >> 3) & 7; |
910 |
< |
transfer_type = SIGSEGV_TRANSFER_LOAD; |
911 |
< |
break; |
912 |
< |
} |
913 |
< |
len += 3 + ix86_step_over_modrm(eip + 2); |
914 |
< |
break; |
899 |
> |
transfer_size = SIZE_WORD; |
900 |
> |
goto do_mov_extend; |
901 |
> |
do_mov_extend: |
902 |
> |
switch (eip[2] & 0xc0) { |
903 |
> |
case 0x80: |
904 |
> |
reg = (eip[2] >> 3) & 7; |
905 |
> |
transfer_type = SIGSEGV_TRANSFER_LOAD; |
906 |
> |
break; |
907 |
> |
case 0x40: |
908 |
> |
reg = (eip[2] >> 3) & 7; |
909 |
> |
transfer_type = SIGSEGV_TRANSFER_LOAD; |
910 |
> |
break; |
911 |
> |
case 0x00: |
912 |
> |
reg = (eip[2] >> 3) & 7; |
913 |
> |
transfer_type = SIGSEGV_TRANSFER_LOAD; |
914 |
> |
break; |
915 |
> |
} |
916 |
> |
len += 3 + ix86_step_over_modrm(eip + 2); |
917 |
> |
break; |
918 |
|
} |
919 |
|
break; |
920 |
|
case 0x8a: // MOV r8, r/m8 |
956 |
|
len += 2 + ix86_step_over_modrm(eip + 1); |
957 |
|
break; |
958 |
|
} |
959 |
+ |
if (target_size == SIZE_UNKNOWN) |
960 |
+ |
target_size = transfer_size; |
961 |
|
|
962 |
|
if (transfer_type == SIGSEGV_TRANSFER_UNKNOWN) { |
963 |
|
// Unknown machine code, let it crash. Then patch the decoder |
985 |
|
// Set 0 to the relevant register part |
986 |
|
// NOTE: this is only valid for MOV alike instructions |
987 |
|
int rloc = x86_reg_map[reg]; |
988 |
< |
switch (transfer_size) { |
988 |
> |
switch (target_size) { |
989 |
|
case SIZE_BYTE: |
990 |
|
if (has_rex || reg < 4) |
991 |
|
regs[rloc] = (regs[rloc] & ~0x00ffL); |
1039 |
|
"r12", "r13", "r14", "r15", |
1040 |
|
}; |
1041 |
|
const char * reg_str = NULL; |
1042 |
< |
switch (transfer_size) { |
1042 |
> |
switch (target_size) { |
1043 |
|
case SIZE_BYTE: |
1044 |
|
reg_str = x86_byte_reg_str_map[(!has_rex && reg >= 4 ? 12 : 0) + reg]; |
1045 |
|
break; |
1062 |
|
|
1063 |
|
// Decode and skip PPC instruction |
1064 |
|
#if (defined(powerpc) || defined(__powerpc__) || defined(__ppc__)) |
1065 |
< |
static bool powerpc_skip_instruction(unsigned int * nip_p, unsigned int * regs) |
1065 |
> |
static bool powerpc_skip_instruction(unsigned long * nip_p, unsigned long * regs) |
1066 |
|
{ |
1067 |
|
instruction_t instr; |
1068 |
|
powerpc_decode_instruction(&instr, *nip_p, regs); |
1074 |
|
|
1075 |
|
#if DEBUG |
1076 |
|
printf("%08x: %s %s access", *nip_p, |
1077 |
< |
instr.transfer_size == SIZE_BYTE ? "byte" : instr.transfer_size == SIZE_WORD ? "word" : "long", |
1077 |
> |
instr.transfer_size == SIZE_BYTE ? "byte" : |
1078 |
> |
instr.transfer_size == SIZE_WORD ? "word" : |
1079 |
> |
instr.transfer_size == SIZE_LONG ? "long" : "quad", |
1080 |
|
instr.transfer_type == SIGSEGV_TRANSFER_LOAD ? "read" : "write"); |
1081 |
|
|
1082 |
|
if (instr.addr_mode == MODE_U || instr.addr_mode == MODE_UX) |
1388 |
|
#endif |
1389 |
|
#endif |
1390 |
|
|
1391 |
+ |
// Decode and skip ARM instruction |
1392 |
+ |
#if (defined(arm) || defined(__arm__)) |
1393 |
+ |
enum { |
1394 |
+ |
#if (defined(__linux__)) |
1395 |
+ |
ARM_REG_PC = 15, |
1396 |
+ |
ARM_REG_CPSR = 16 |
1397 |
+ |
#endif |
1398 |
+ |
}; |
1399 |
+ |
static bool arm_skip_instruction(unsigned long * regs) |
1400 |
+ |
{ |
1401 |
+ |
unsigned int * pc = (unsigned int *)regs[ARM_REG_PC]; |
1402 |
+ |
|
1403 |
+ |
if (pc == 0) |
1404 |
+ |
return false; |
1405 |
+ |
|
1406 |
+ |
#if DEBUG |
1407 |
+ |
printf("IP: %p [%08x]\n", pc, pc[0]); |
1408 |
+ |
#endif |
1409 |
+ |
|
1410 |
+ |
transfer_type_t transfer_type = SIGSEGV_TRANSFER_UNKNOWN; |
1411 |
+ |
transfer_size_t transfer_size = SIZE_UNKNOWN; |
1412 |
+ |
enum { op_sdt = 1, op_sdth = 2 }; |
1413 |
+ |
int op = 0; |
1414 |
+ |
|
1415 |
+ |
// Handle load/store instructions only |
1416 |
+ |
const unsigned int opcode = pc[0]; |
1417 |
+ |
switch ((opcode >> 25) & 7) { |
1418 |
+ |
case 0: // Halfword and Signed Data Transfer (LDRH, STRH, LDRSB, LDRSH) |
1419 |
+ |
op = op_sdth; |
1420 |
+ |
// Determine transfer size (S/H bits) |
1421 |
+ |
switch ((opcode >> 5) & 3) { |
1422 |
+ |
case 0: // SWP instruction |
1423 |
+ |
break; |
1424 |
+ |
case 1: // Unsigned halfwords |
1425 |
+ |
case 3: // Signed halfwords |
1426 |
+ |
transfer_size = SIZE_WORD; |
1427 |
+ |
break; |
1428 |
+ |
case 2: // Signed byte |
1429 |
+ |
transfer_size = SIZE_BYTE; |
1430 |
+ |
break; |
1431 |
+ |
} |
1432 |
+ |
break; |
1433 |
+ |
case 2: |
1434 |
+ |
case 3: // Single Data Transfer (LDR, STR) |
1435 |
+ |
op = op_sdt; |
1436 |
+ |
// Determine transfer size (B bit) |
1437 |
+ |
if (((opcode >> 22) & 1) == 1) |
1438 |
+ |
transfer_size = SIZE_BYTE; |
1439 |
+ |
else |
1440 |
+ |
transfer_size = SIZE_LONG; |
1441 |
+ |
break; |
1442 |
+ |
default: |
1443 |
+ |
// FIXME: support load/store mutliple? |
1444 |
+ |
return false; |
1445 |
+ |
} |
1446 |
+ |
|
1447 |
+ |
// Check for invalid transfer size (SWP instruction?) |
1448 |
+ |
if (transfer_size == SIZE_UNKNOWN) |
1449 |
+ |
return false; |
1450 |
+ |
|
1451 |
+ |
// Determine transfer type (L bit) |
1452 |
+ |
if (((opcode >> 20) & 1) == 1) |
1453 |
+ |
transfer_type = SIGSEGV_TRANSFER_LOAD; |
1454 |
+ |
else |
1455 |
+ |
transfer_type = SIGSEGV_TRANSFER_STORE; |
1456 |
+ |
|
1457 |
+ |
// Compute offset |
1458 |
+ |
int offset; |
1459 |
+ |
if (((opcode >> 25) & 1) == 0) { |
1460 |
+ |
if (op == op_sdt) |
1461 |
+ |
offset = opcode & 0xfff; |
1462 |
+ |
else if (op == op_sdth) { |
1463 |
+ |
int rm = opcode & 0xf; |
1464 |
+ |
if (((opcode >> 22) & 1) == 0) { |
1465 |
+ |
// register offset |
1466 |
+ |
offset = regs[rm]; |
1467 |
+ |
} |
1468 |
+ |
else { |
1469 |
+ |
// immediate offset |
1470 |
+ |
offset = ((opcode >> 4) & 0xf0) | (opcode & 0x0f); |
1471 |
+ |
} |
1472 |
+ |
} |
1473 |
+ |
} |
1474 |
+ |
else { |
1475 |
+ |
const int rm = opcode & 0xf; |
1476 |
+ |
const int sh = (opcode >> 7) & 0x1f; |
1477 |
+ |
if (((opcode >> 4) & 1) == 1) { |
1478 |
+ |
// we expect only legal load/store instructions |
1479 |
+ |
printf("FATAL: invalid shift operand\n"); |
1480 |
+ |
return false; |
1481 |
+ |
} |
1482 |
+ |
const unsigned int v = regs[rm]; |
1483 |
+ |
switch ((opcode >> 5) & 3) { |
1484 |
+ |
case 0: // logical shift left |
1485 |
+ |
offset = sh ? v << sh : v; |
1486 |
+ |
break; |
1487 |
+ |
case 1: // logical shift right |
1488 |
+ |
offset = sh ? v >> sh : 0; |
1489 |
+ |
break; |
1490 |
+ |
case 2: // arithmetic shift right |
1491 |
+ |
if (sh) |
1492 |
+ |
offset = ((signed int)v) >> sh; |
1493 |
+ |
else |
1494 |
+ |
offset = (v & 0x80000000) ? 0xffffffff : 0; |
1495 |
+ |
break; |
1496 |
+ |
case 3: // rotate right |
1497 |
+ |
if (sh) |
1498 |
+ |
offset = (v >> sh) | (v << (32 - sh)); |
1499 |
+ |
else |
1500 |
+ |
offset = (v >> 1) | ((regs[ARM_REG_CPSR] << 2) & 0x80000000); |
1501 |
+ |
break; |
1502 |
+ |
} |
1503 |
+ |
} |
1504 |
+ |
if (((opcode >> 23) & 1) == 0) |
1505 |
+ |
offset = -offset; |
1506 |
+ |
|
1507 |
+ |
int rd = (opcode >> 12) & 0xf; |
1508 |
+ |
int rn = (opcode >> 16) & 0xf; |
1509 |
+ |
#if DEBUG |
1510 |
+ |
static const char * reg_names[] = { |
1511 |
+ |
"r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", |
1512 |
+ |
"r9", "r9", "sl", "fp", "ip", "sp", "lr", "pc" |
1513 |
+ |
}; |
1514 |
+ |
printf("%s %s register %s\n", |
1515 |
+ |
transfer_size == SIZE_BYTE ? "byte" : |
1516 |
+ |
transfer_size == SIZE_WORD ? "word" : |
1517 |
+ |
transfer_size == SIZE_LONG ? "long" : "unknown", |
1518 |
+ |
transfer_type == SIGSEGV_TRANSFER_LOAD ? "load to" : "store from", |
1519 |
+ |
reg_names[rd]); |
1520 |
+ |
#endif |
1521 |
+ |
|
1522 |
+ |
unsigned int base = regs[rn]; |
1523 |
+ |
if (((opcode >> 24) & 1) == 1) |
1524 |
+ |
base += offset; |
1525 |
+ |
|
1526 |
+ |
if (transfer_type == SIGSEGV_TRANSFER_LOAD) |
1527 |
+ |
regs[rd] = 0; |
1528 |
+ |
|
1529 |
+ |
if (((opcode >> 24) & 1) == 0) // post-index addressing |
1530 |
+ |
regs[rn] += offset; |
1531 |
+ |
else if (((opcode >> 21) & 1) == 1) // write-back address into base |
1532 |
+ |
regs[rn] = base; |
1533 |
+ |
|
1534 |
+ |
regs[ARM_REG_PC] += 4; |
1535 |
+ |
return true; |
1536 |
+ |
} |
1537 |
+ |
#endif |
1538 |
+ |
|
1539 |
+ |
|
1540 |
|
// Fallbacks |
1541 |
|
#ifndef SIGSEGV_FAULT_INSTRUCTION |
1542 |
|
#define SIGSEGV_FAULT_INSTRUCTION SIGSEGV_INVALID_PC |
1558 |
|
* SIGSEGV global handler |
1559 |
|
*/ |
1560 |
|
|
1276 |
– |
#if defined(HAVE_SIGSEGV_RECOVERY) || defined(HAVE_MACH_EXCEPTIONS) |
1561 |
|
// This function handles the badaccess to memory. |
1562 |
|
// It is called from the signal handler or the exception handler. |
1563 |
|
static bool handle_badaccess(SIGSEGV_FAULT_HANDLER_ARGLIST_1) |
1591 |
|
} |
1592 |
|
break; |
1593 |
|
#endif |
1594 |
+ |
case SIGSEGV_RETURN_FAILURE: |
1595 |
+ |
// We can't do anything with the fault_address, dump state? |
1596 |
+ |
if (sigsegv_state_dumper != 0) |
1597 |
+ |
sigsegv_state_dumper(fault_address, fault_instruction); |
1598 |
+ |
break; |
1599 |
|
} |
1311 |
– |
|
1312 |
– |
// We can't do anything with the fault_address, dump state? |
1313 |
– |
if (sigsegv_state_dumper != 0) |
1314 |
– |
sigsegv_state_dumper(fault_address, fault_instruction); |
1600 |
|
|
1601 |
|
return false; |
1602 |
|
} |
1318 |
– |
#endif |
1603 |
|
|
1604 |
|
|
1605 |
|
/* |
1893 |
|
} |
1894 |
|
#endif |
1895 |
|
|
1896 |
+ |
#ifdef HAVE_WIN32_EXCEPTIONS |
1897 |
+ |
static LONG WINAPI main_exception_filter(EXCEPTION_POINTERS *ExceptionInfo) |
1898 |
+ |
{ |
1899 |
+ |
if (sigsegv_fault_handler != NULL |
1900 |
+ |
&& ExceptionInfo->ExceptionRecord->ExceptionCode == EXCEPTION_ACCESS_VIOLATION |
1901 |
+ |
&& ExceptionInfo->ExceptionRecord->NumberParameters == 2 |
1902 |
+ |
&& handle_badaccess(ExceptionInfo)) |
1903 |
+ |
return EXCEPTION_CONTINUE_EXECUTION; |
1904 |
+ |
|
1905 |
+ |
return EXCEPTION_CONTINUE_SEARCH; |
1906 |
+ |
} |
1907 |
+ |
|
1908 |
+ |
#if defined __CYGWIN__ && defined __i386__ |
1909 |
+ |
/* In Cygwin programs, SetUnhandledExceptionFilter has no effect because Cygwin |
1910 |
+ |
installs a global exception handler. We have to dig deep in order to install |
1911 |
+ |
our main_exception_filter. */ |
1912 |
+ |
|
1913 |
+ |
/* Data structures for the current thread's exception handler chain. |
1914 |
+ |
On the x86 Windows uses register fs, offset 0 to point to the current |
1915 |
+ |
exception handler; Cygwin mucks with it, so we must do the same... :-/ */ |
1916 |
+ |
|
1917 |
+ |
/* Magic taken from winsup/cygwin/include/exceptions.h. */ |
1918 |
+ |
|
1919 |
+ |
struct exception_list { |
1920 |
+ |
struct exception_list *prev; |
1921 |
+ |
int (*handler) (EXCEPTION_RECORD *, void *, CONTEXT *, void *); |
1922 |
+ |
}; |
1923 |
+ |
typedef struct exception_list exception_list; |
1924 |
+ |
|
1925 |
+ |
/* Magic taken from winsup/cygwin/exceptions.cc. */ |
1926 |
+ |
|
1927 |
+ |
__asm__ (".equ __except_list,0"); |
1928 |
+ |
|
1929 |
+ |
extern exception_list *_except_list __asm__ ("%fs:__except_list"); |
1930 |
+ |
|
1931 |
+ |
/* For debugging. _except_list is not otherwise accessible from gdb. */ |
1932 |
+ |
static exception_list * |
1933 |
+ |
debug_get_except_list () |
1934 |
+ |
{ |
1935 |
+ |
return _except_list; |
1936 |
+ |
} |
1937 |
+ |
|
1938 |
+ |
/* Cygwin's original exception handler. */ |
1939 |
+ |
static int (*cygwin_exception_handler) (EXCEPTION_RECORD *, void *, CONTEXT *, void *); |
1940 |
+ |
|
1941 |
+ |
/* Our exception handler. */ |
1942 |
+ |
static int |
1943 |
+ |
libsigsegv_exception_handler (EXCEPTION_RECORD *exception, void *frame, CONTEXT *context, void *dispatch) |
1944 |
+ |
{ |
1945 |
+ |
EXCEPTION_POINTERS ExceptionInfo; |
1946 |
+ |
ExceptionInfo.ExceptionRecord = exception; |
1947 |
+ |
ExceptionInfo.ContextRecord = context; |
1948 |
+ |
if (main_exception_filter (&ExceptionInfo) == EXCEPTION_CONTINUE_SEARCH) |
1949 |
+ |
return cygwin_exception_handler (exception, frame, context, dispatch); |
1950 |
+ |
else |
1951 |
+ |
return 0; |
1952 |
+ |
} |
1953 |
+ |
|
1954 |
+ |
static void |
1955 |
+ |
do_install_main_exception_filter () |
1956 |
+ |
{ |
1957 |
+ |
/* We cannot insert any handler into the chain, because such handlers |
1958 |
+ |
must lie on the stack (?). Instead, we have to replace(!) Cygwin's |
1959 |
+ |
global exception handler. */ |
1960 |
+ |
cygwin_exception_handler = _except_list->handler; |
1961 |
+ |
_except_list->handler = libsigsegv_exception_handler; |
1962 |
+ |
} |
1963 |
+ |
|
1964 |
+ |
#else |
1965 |
+ |
|
1966 |
+ |
static void |
1967 |
+ |
do_install_main_exception_filter () |
1968 |
+ |
{ |
1969 |
+ |
SetUnhandledExceptionFilter ((LPTOP_LEVEL_EXCEPTION_FILTER) &main_exception_filter); |
1970 |
+ |
} |
1971 |
+ |
#endif |
1972 |
+ |
|
1973 |
+ |
static bool sigsegv_do_install_handler(sigsegv_fault_handler_t handler) |
1974 |
+ |
{ |
1975 |
+ |
static bool main_exception_filter_installed = false; |
1976 |
+ |
if (!main_exception_filter_installed) { |
1977 |
+ |
do_install_main_exception_filter(); |
1978 |
+ |
main_exception_filter_installed = true; |
1979 |
+ |
} |
1980 |
+ |
sigsegv_fault_handler = handler; |
1981 |
+ |
return true; |
1982 |
+ |
} |
1983 |
+ |
#endif |
1984 |
+ |
|
1985 |
|
bool sigsegv_install_handler(sigsegv_fault_handler_t handler) |
1986 |
|
{ |
1987 |
|
#if defined(HAVE_SIGSEGV_RECOVERY) |
1992 |
|
if (success) |
1993 |
|
sigsegv_fault_handler = handler; |
1994 |
|
return success; |
1995 |
< |
#elif defined(HAVE_MACH_EXCEPTIONS) |
1995 |
> |
#elif defined(HAVE_MACH_EXCEPTIONS) || defined(HAVE_WIN32_EXCEPTIONS) |
1996 |
|
return sigsegv_do_install_handler(handler); |
1997 |
|
#else |
1998 |
|
// FAIL: no siginfo_t nor sigcontext subterfuge is available |
2018 |
|
SIGSEGV_ALL_SIGNALS |
2019 |
|
#undef FAULT_HANDLER |
2020 |
|
#endif |
2021 |
+ |
#ifdef HAVE_WIN32_EXCEPTIONS |
2022 |
+ |
sigsegv_fault_handler = NULL; |
2023 |
+ |
#endif |
2024 |
|
} |
2025 |
|
|
2026 |
|
|
2042 |
|
#include <stdio.h> |
2043 |
|
#include <stdlib.h> |
2044 |
|
#include <fcntl.h> |
2045 |
+ |
#ifdef HAVE_SYS_MMAN_H |
2046 |
|
#include <sys/mman.h> |
2047 |
+ |
#endif |
2048 |
|
#include "vm_alloc.h" |
2049 |
|
|
2050 |
|
const int REF_INDEX = 123; |
2087 |
|
#ifdef HAVE_SIGSEGV_SKIP_INSTRUCTION |
2088 |
|
static sigsegv_return_t sigsegv_insn_handler(sigsegv_address_t fault_address, sigsegv_address_t instruction_address) |
2089 |
|
{ |
2090 |
+ |
#if DEBUG |
2091 |
+ |
printf("sigsegv_insn_handler(%p, %p)\n", fault_address, instruction_address); |
2092 |
+ |
#endif |
2093 |
|
if (((unsigned long)fault_address - (unsigned long)page) < page_size) { |
2094 |
|
#ifdef __GNUC__ |
2095 |
|
// Make sure reported fault instruction address falls into |
2168 |
|
if (vm_init() < 0) |
2169 |
|
return 1; |
2170 |
|
|
2171 |
< |
page_size = getpagesize(); |
2171 |
> |
page_size = vm_get_page_size(); |
2172 |
|
if ((page = (char *)vm_acquire(page_size)) == VM_MAP_FAILED) |
2173 |
|
return 2; |
2174 |
|
|
2224 |
|
TEST_SKIP_INSTRUCTION(unsigned short); |
2225 |
|
TEST_SKIP_INSTRUCTION(unsigned int); |
2226 |
|
TEST_SKIP_INSTRUCTION(unsigned long); |
2227 |
+ |
TEST_SKIP_INSTRUCTION(signed char); |
2228 |
+ |
TEST_SKIP_INSTRUCTION(signed short); |
2229 |
+ |
TEST_SKIP_INSTRUCTION(signed int); |
2230 |
+ |
TEST_SKIP_INSTRUCTION(signed long); |
2231 |
|
L_e_region2: |
2232 |
|
|
2233 |
|
if (!arch_insn_skipper_tests()) |