ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/Unix/sigsegv.cpp
(Generate patch)

Comparing BasiliskII/src/Unix/sigsegv.cpp (file contents):
Revision 1.33 by gbeauche, 2003-10-21T23:10:19Z vs.
Revision 1.40 by gbeauche, 2003-12-20T23:22:25Z

# Line 36 | Line 36
36   #endif
37  
38   #include <list>
39 + #include <stdio.h>
40   #include <signal.h>
41   #include "sigsegv.h"
42  
# Line 69 | Line 70 | static bool sigsegv_do_install_handler(i
70   enum transfer_size_t {
71          SIZE_UNKNOWN,
72          SIZE_BYTE,
73 <        SIZE_WORD,
74 <        SIZE_LONG
73 >        SIZE_WORD, // 2 bytes
74 >        SIZE_LONG, // 4 bytes
75 >        SIZE_QUAD, // 8 bytes
76   };
77  
78   // Transfer type
# Line 221 | Line 223 | static void powerpc_decode_instruction(i
223   #define SIGSEGV_FAULT_HANDLER_ARGLIST_1 siginfo_t *sip, void *scp
224   #define SIGSEGV_FAULT_HANDLER_ARGS              sip, scp
225   #define SIGSEGV_FAULT_ADDRESS                   sip->si_addr
226 + #if (defined(sgi) || defined(__sgi))
227 + #include <ucontext.h>
228 + #define SIGSEGV_CONTEXT_REGS                    (((ucontext_t *)scp)->uc_mcontext.gregs)
229 + #define SIGSEGV_FAULT_INSTRUCTION               (unsigned long)SIGSEGV_CONTEXT_REGS[CTX_EPC]
230 + #if (defined(mips) || defined(__mips))
231 + #define SIGSEGV_REGISTER_FILE                   SIGSEGV_CONTEXT_REGS
232 + #define SIGSEGV_SKIP_INSTRUCTION                mips_skip_instruction
233 + #endif
234 + #endif
235   #if defined(__sun__)
236   #if (defined(sparc) || defined(__sparc__))
237 + #include <sys/stack.h>
238 + #include <sys/regset.h>
239   #include <sys/ucontext.h>
240   #define SIGSEGV_CONTEXT_REGS                    (((ucontext_t *)scp)->uc_mcontext.gregs)
241   #define SIGSEGV_FAULT_INSTRUCTION               SIGSEGV_CONTEXT_REGS[REG_PC]
242 + #define SIGSEGV_SPARC_GWINDOWS                  (((ucontext_t *)scp)->uc_mcontext.gwins)
243 + #define SIGSEGV_SPARC_RWINDOW                   (struct rwindow *)((char *)SIGSEGV_CONTEXT_REGS[REG_SP] + STACK_BIAS)
244 + #define SIGSEGV_REGISTER_FILE                   ((unsigned long *)SIGSEGV_CONTEXT_REGS), SIGSEGV_SPARC_GWINDOWS, SIGSEGV_SPARC_RWINDOW
245 + #define SIGSEGV_SKIP_INSTRUCTION                sparc_skip_instruction
246   #endif
247   #endif
248   #if defined(__FreeBSD__)
249   #if (defined(i386) || defined(__i386__))
250   #define SIGSEGV_FAULT_INSTRUCTION               (((struct sigcontext *)scp)->sc_eip)
251 < #define SIGSEGV_REGISTER_FILE                   ((unsigned int *)&(((struct sigcontext *)scp)->sc_edi)) /* EDI is the first GPR (even below EIP) in sigcontext */
251 > #define SIGSEGV_REGISTER_FILE                   ((unsigned long *)&(((struct sigcontext *)scp)->sc_edi)) /* EDI is the first GPR (even below EIP) in sigcontext */
252   #define SIGSEGV_SKIP_INSTRUCTION                ix86_skip_instruction
253   #endif
254   #endif
# Line 240 | Line 257 | static void powerpc_decode_instruction(i
257   #include <sys/ucontext.h>
258   #define SIGSEGV_CONTEXT_REGS                    (((ucontext_t *)scp)->uc_mcontext.gregs)
259   #define SIGSEGV_FAULT_INSTRUCTION               SIGSEGV_CONTEXT_REGS[14] /* should use REG_EIP instead */
260 < #define SIGSEGV_REGISTER_FILE                   (unsigned int *)SIGSEGV_CONTEXT_REGS
260 > #define SIGSEGV_REGISTER_FILE                   (unsigned long *)SIGSEGV_CONTEXT_REGS
261   #define SIGSEGV_SKIP_INSTRUCTION                ix86_skip_instruction
262   #endif
263   #if (defined(x86_64) || defined(__x86_64__))
# Line 248 | Line 265 | static void powerpc_decode_instruction(i
265   #define SIGSEGV_CONTEXT_REGS                    (((ucontext_t *)scp)->uc_mcontext.gregs)
266   #define SIGSEGV_FAULT_INSTRUCTION               SIGSEGV_CONTEXT_REGS[16] /* should use REG_RIP instead */
267   #define SIGSEGV_REGISTER_FILE                   (unsigned long *)SIGSEGV_CONTEXT_REGS
268 + #define SIGSEGV_SKIP_INSTRUCTION                ix86_skip_instruction
269   #endif
270   #if (defined(ia64) || defined(__ia64__))
271   #define SIGSEGV_FAULT_INSTRUCTION               (((struct sigcontext *)scp)->sc_ip & ~0x3ULL) /* slot number is in bits 0 and 1 */
# Line 259 | Line 277 | static void powerpc_decode_instruction(i
277   #define SIGSEGV_REGISTER_FILE                   (unsigned int *)&SIGSEGV_CONTEXT_REGS->nip, (unsigned int *)(SIGSEGV_CONTEXT_REGS->gpr)
278   #define SIGSEGV_SKIP_INSTRUCTION                powerpc_skip_instruction
279   #endif
280 + #if (defined(hppa) || defined(__hppa__))
281 + #undef  SIGSEGV_FAULT_ADDRESS
282 + #define SIGSEGV_FAULT_ADDRESS                   sip->si_ptr
283 + #endif
284   #endif
285   #endif
286  
# Line 273 | Line 295 | static void powerpc_decode_instruction(i
295   #define SIGSEGV_FAULT_HANDLER_ARGS              &scs
296   #define SIGSEGV_FAULT_ADDRESS                   scp->cr2
297   #define SIGSEGV_FAULT_INSTRUCTION               scp->eip
298 < #define SIGSEGV_REGISTER_FILE                   (unsigned int *)scp
298 > #define SIGSEGV_REGISTER_FILE                   (unsigned long *)scp
299   #define SIGSEGV_SKIP_INSTRUCTION                ix86_skip_instruction
300   #endif
301   #if (defined(sparc) || defined(__sparc__))
# Line 301 | Line 323 | static void powerpc_decode_instruction(i
323   #endif
324  
325   // Irix 5 or 6 on MIPS
326 < #if (defined(sgi) || defined(__sgi)) && (defined(SYSTYPE_SVR4) || defined(__SYSTYPE_SVR4))
326 > #if (defined(sgi) || defined(__sgi)) && (defined(SYSTYPE_SVR4) || defined(_SYSTYPE_SVR4))
327   #include <ucontext.h>
328   #define SIGSEGV_FAULT_HANDLER_ARGLIST   int sig, int code, struct sigcontext *scp
329   #define SIGSEGV_FAULT_HANDLER_ARGS              sig, code, scp
330 < #define SIGSEGV_FAULT_ADDRESS                   scp->sc_badvaddr
330 > #define SIGSEGV_FAULT_ADDRESS                   (unsigned long)scp->sc_badvaddr
331 > #define SIGSEGV_FAULT_INSTRUCTION               (unsigned long)scp->sc_pc
332   #define SIGSEGV_ALL_SIGNALS                             FAULT_HANDLER(SIGSEGV)
333   #endif
334  
# Line 378 | Line 401 | static sigsegv_address_t get_fault_addre
401   #endif
402   #endif
403   #if defined(__FreeBSD__)
381 #define SIGSEGV_ALL_SIGNALS                             FAULT_HANDLER(SIGBUS)
404   #if (defined(i386) || defined(__i386__))
405 + #define SIGSEGV_ALL_SIGNALS                             FAULT_HANDLER(SIGBUS)
406   #define SIGSEGV_FAULT_HANDLER_ARGLIST   int sig, int code, struct sigcontext *scp, char *addr
407   #define SIGSEGV_FAULT_HANDLER_ARGS              sig, code, scp, addr
408   #define SIGSEGV_FAULT_ADDRESS                   addr
409   #define SIGSEGV_FAULT_INSTRUCTION               scp->sc_eip
410 < #define SIGSEGV_REGISTER_FILE                   ((unsigned int *)&scp->sc_edi)
410 > #define SIGSEGV_REGISTER_FILE                   ((unsigned long *)&scp->sc_edi)
411   #define SIGSEGV_SKIP_INSTRUCTION                ix86_skip_instruction
412   #endif
413 + #if (defined(alpha) || defined(__alpha__))
414 + #define SIGSEGV_ALL_SIGNALS                             FAULT_HANDLER(SIGSEGV)
415 + #define SIGSEGV_FAULT_HANDLER_ARGLIST   int sig, char *addr, struct sigcontext *scp
416 + #define SIGSEGV_FAULT_HANDLER_ARGS              sig, addr, scp
417 + #define SIGSEGV_FAULT_ADDRESS                   addr
418 + #define SIGSEGV_FAULT_INSTRUCTION               scp->sc_pc
419 + #endif
420   #endif
421  
422   // Extract fault address out of a sigcontext
# Line 577 | Line 607 | handleExceptions(void *priv)
607  
608   #ifdef HAVE_SIGSEGV_SKIP_INSTRUCTION
609   // Decode and skip X86 instruction
610 < #if (defined(i386) || defined(__i386__))
610 > #if (defined(i386) || defined(__i386__)) || defined(__x86_64__)
611   #if defined(__linux__)
612   enum {
613 + #if (defined(i386) || defined(__i386__))
614          X86_REG_EIP = 14,
615          X86_REG_EAX = 11,
616          X86_REG_ECX = 10,
# Line 589 | Line 620 | enum {
620          X86_REG_EBP = 6,
621          X86_REG_ESI = 5,
622          X86_REG_EDI = 4
623 + #endif
624 + #if defined(__x86_64__)
625 +        X86_REG_R8  = 0,
626 +        X86_REG_R9  = 1,
627 +        X86_REG_R10 = 2,
628 +        X86_REG_R11 = 3,
629 +        X86_REG_R12 = 4,
630 +        X86_REG_R13 = 5,
631 +        X86_REG_R14 = 6,
632 +        X86_REG_R15 = 7,
633 +        X86_REG_EDI = 8,
634 +        X86_REG_ESI = 9,
635 +        X86_REG_EBP = 10,
636 +        X86_REG_EBX = 11,
637 +        X86_REG_EDX = 12,
638 +        X86_REG_EAX = 13,
639 +        X86_REG_ECX = 14,
640 +        X86_REG_ESP = 15,
641 +        X86_REG_EIP = 16
642 + #endif
643   };
644   #endif
645   #if defined(__NetBSD__) || defined(__FreeBSD__)
646   enum {
647 + #if (defined(i386) || defined(__i386__))
648          X86_REG_EIP = 10,
649          X86_REG_EAX = 7,
650          X86_REG_ECX = 6,
# Line 602 | Line 654 | enum {
654          X86_REG_EBP = 2,
655          X86_REG_ESI = 1,
656          X86_REG_EDI = 0
657 + #endif
658   };
659   #endif
660   // FIXME: this is partly redundant with the instruction decoding phase
# Line 638 | Line 691 | static inline int ix86_step_over_modrm(u
691          return offset;
692   }
693  
694 < static bool ix86_skip_instruction(unsigned int * regs)
694 > static bool ix86_skip_instruction(unsigned long * regs)
695   {
696          unsigned char * eip = (unsigned char *)regs[X86_REG_EIP];
697  
# Line 650 | Line 703 | static bool ix86_skip_instruction(unsign
703          
704          int reg = -1;
705          int len = 0;
706 <        
706 >
707 > #if DEBUG
708 >        printf("IP: %p [%02x %02x %02x %02x...]\n",
709 >                   eip, eip[0], eip[1], eip[2], eip[3]);
710 > #endif
711 >
712          // Operand size prefix
713          if (*eip == 0x66) {
714                  eip++;
# Line 658 | Line 716 | static bool ix86_skip_instruction(unsign
716                  transfer_size = SIZE_WORD;
717          }
718  
719 +        // REX prefix
720 + #if defined(__x86_64__)
721 +        struct rex_t {
722 +                unsigned char W;
723 +                unsigned char R;
724 +                unsigned char X;
725 +                unsigned char B;
726 +        };
727 +        rex_t rex = { 0, 0, 0, 0 };
728 +        bool has_rex = false;
729 +        if ((*eip & 0xf0) == 0x40) {
730 +                has_rex = true;
731 +                const unsigned char b = *eip;
732 +                rex.W = b & (1 << 3);
733 +                rex.R = b & (1 << 2);
734 +                rex.X = b & (1 << 1);
735 +                rex.B = b & (1 << 0);
736 + #if DEBUG
737 +                printf("REX: %c,%c,%c,%c\n",
738 +                           rex.W ? 'W' : '_',
739 +                           rex.R ? 'R' : '_',
740 +                           rex.X ? 'X' : '_',
741 +                           rex.B ? 'B' : '_');
742 + #endif
743 +                eip++;
744 +                len++;
745 +                if (rex.W)
746 +                        transfer_size = SIZE_QUAD;
747 +        }
748 + #else
749 +        const bool has_rex = false;
750 + #endif
751 +
752          // Decode instruction
753          switch (eip[0]) {
754          case 0x0f:
# Line 727 | Line 818 | static bool ix86_skip_instruction(unsign
818                  return false;
819          }
820  
821 + #if defined(__x86_64__)
822 +        if (rex.R)
823 +                reg += 8;
824 + #endif
825 +
826          if (transfer_type == SIGSEGV_TRANSFER_LOAD && reg != -1) {
827 <                static const int x86_reg_map[8] = {
827 >                static const int x86_reg_map[] = {
828                          X86_REG_EAX, X86_REG_ECX, X86_REG_EDX, X86_REG_EBX,
829 <                        X86_REG_ESP, X86_REG_EBP, X86_REG_ESI, X86_REG_EDI
829 >                        X86_REG_ESP, X86_REG_EBP, X86_REG_ESI, X86_REG_EDI,
830 > #if defined(__x86_64__)
831 >                        X86_REG_R8,  X86_REG_R9,  X86_REG_R10, X86_REG_R11,
832 >                        X86_REG_R12, X86_REG_R13, X86_REG_R14, X86_REG_R15,
833 > #endif
834                  };
835                  
836 <                if (reg < 0 || reg >= 8)
836 >                if (reg < 0 || reg >= (sizeof(x86_reg_map)/sizeof(x86_reg_map[0]) - 1))
837                          return false;
838  
839 +                // Set 0 to the relevant register part
840 +                // NOTE: this is only valid for MOV alike instructions
841                  int rloc = x86_reg_map[reg];
842                  switch (transfer_size) {
843                  case SIZE_BYTE:
844 <                        regs[rloc] = (regs[rloc] & ~0xff);
844 >                        if (has_rex || reg < 4)
845 >                                regs[rloc] = (regs[rloc] & ~0x00ffL);
846 >                        else {
847 >                                rloc = x86_reg_map[reg - 4];
848 >                                regs[rloc] = (regs[rloc] & ~0xff00L);
849 >                        }
850                          break;
851                  case SIZE_WORD:
852 <                        regs[rloc] = (regs[rloc] & ~0xffff);
852 >                        regs[rloc] = (regs[rloc] & ~0xffffL);
853                          break;
854                  case SIZE_LONG:
855 +                case SIZE_QUAD: // zero-extension
856                          regs[rloc] = 0;
857                          break;
858                  }
# Line 752 | Line 860 | static bool ix86_skip_instruction(unsign
860  
861   #if DEBUG
862          printf("%08x: %s %s access", regs[X86_REG_EIP],
863 <                   transfer_size == SIZE_BYTE ? "byte" : transfer_size == SIZE_WORD ? "word" : "long",
863 >                   transfer_size == SIZE_BYTE ? "byte" :
864 >                   transfer_size == SIZE_WORD ? "word" :
865 >                   transfer_size == SIZE_LONG ? "long" :
866 >                   transfer_size == SIZE_QUAD ? "quad" : "unknown",
867                     transfer_type == SIGSEGV_TRANSFER_LOAD ? "read" : "write");
868          
869          if (reg != -1) {
870 <                static const char * x86_reg_str_map[8] = {
871 <                        "eax", "ecx", "edx", "ebx",
872 <                        "esp", "ebp", "esi", "edi"
870 >                static const char * x86_byte_reg_str_map[] = {
871 >                        "al",   "cl",   "dl",   "bl",
872 >                        "spl",  "bpl",  "sil",  "dil",
873 >                        "r8b",  "r9b",  "r10b", "r11b",
874 >                        "r12b", "r13b", "r14b", "r15b",
875 >                        "ah",   "ch",   "dh",   "bh",
876                  };
877 <                printf(" %s register %%%s", transfer_type == SIGSEGV_TRANSFER_LOAD ? "to" : "from", x86_reg_str_map[reg]);
877 >                static const char * x86_word_reg_str_map[] = {
878 >                        "ax",   "cx",   "dx",   "bx",
879 >                        "sp",   "bp",   "si",   "di",
880 >                        "r8w",  "r9w",  "r10w", "r11w",
881 >                        "r12w", "r13w", "r14w", "r15w",
882 >                };
883 >                static const char *x86_long_reg_str_map[] = {
884 >                        "eax",  "ecx",  "edx",  "ebx",
885 >                        "esp",  "ebp",  "esi",  "edi",
886 >                        "r8d",  "r9d",  "r10d", "r11d",
887 >                        "r12d", "r13d", "r14d", "r15d",
888 >                };
889 >                static const char *x86_quad_reg_str_map[] = {
890 >                        "rax", "rcx", "rdx", "rbx",
891 >                        "rsp", "rbp", "rsi", "rdi",
892 >                        "r8",  "r9",  "r10", "r11",
893 >                        "r12", "r13", "r14", "r15",
894 >                };
895 >                const char * reg_str = NULL;
896 >                switch (transfer_size) {
897 >                case SIZE_BYTE:
898 >                        reg_str = x86_byte_reg_str_map[(!has_rex && reg >= 4 ? 12 : 0) + reg];
899 >                        break;
900 >                case SIZE_WORD: reg_str = x86_word_reg_str_map[reg]; break;
901 >                case SIZE_LONG: reg_str = x86_long_reg_str_map[reg]; break;
902 >                case SIZE_QUAD: reg_str = x86_quad_reg_str_map[reg]; break;
903 >                }
904 >                if (reg_str)
905 >                        printf(" %s register %%%s",
906 >                                   transfer_type == SIGSEGV_TRANSFER_LOAD ? "to" : "from",
907 >                                   reg_str);
908          }
909          printf(", %d bytes instruction\n", len);
910   #endif
# Line 802 | Line 946 | static bool powerpc_skip_instruction(uns
946          return true;
947   }
948   #endif
949 +
950 + // Decode and skip MIPS instruction
951 + #if (defined(mips) || defined(__mips))
952 + enum {
953 + #if (defined(sgi) || defined(__sgi))
954 +  MIPS_REG_EPC = 35,
955 + #endif
956 + };
957 + static bool mips_skip_instruction(greg_t * regs)
958 + {
959 +  unsigned int * epc = (unsigned int *)(unsigned long)regs[MIPS_REG_EPC];
960 +
961 +  if (epc == 0)
962 +        return false;
963 +
964 + #if DEBUG
965 +  printf("IP: %p [%08x]\n", epc, epc[0]);
966 + #endif
967 +
968 +  transfer_type_t transfer_type = SIGSEGV_TRANSFER_UNKNOWN;
969 +  transfer_size_t transfer_size = SIZE_LONG;
970 +  int direction = 0;
971 +
972 +  const unsigned int opcode = epc[0];
973 +  switch (opcode >> 26) {
974 +  case 32: // Load Byte
975 +  case 36: // Load Byte Unsigned
976 +        transfer_type = SIGSEGV_TRANSFER_LOAD;
977 +        transfer_size = SIZE_BYTE;
978 +        break;
979 +  case 33: // Load Halfword
980 +  case 37: // Load Halfword Unsigned
981 +        transfer_type = SIGSEGV_TRANSFER_LOAD;
982 +        transfer_size = SIZE_WORD;
983 +        break;
984 +  case 35: // Load Word
985 +  case 39: // Load Word Unsigned
986 +        transfer_type = SIGSEGV_TRANSFER_LOAD;
987 +        transfer_size = SIZE_LONG;
988 +        break;
989 +  case 34: // Load Word Left
990 +        transfer_type = SIGSEGV_TRANSFER_LOAD;
991 +        transfer_size = SIZE_LONG;
992 +        direction = -1;
993 +        break;
994 +  case 38: // Load Word Right
995 +        transfer_type = SIGSEGV_TRANSFER_LOAD;
996 +        transfer_size = SIZE_LONG;
997 +        direction = 1;
998 +        break;
999 +  case 55: // Load Doubleword
1000 +        transfer_type = SIGSEGV_TRANSFER_LOAD;
1001 +        transfer_size = SIZE_QUAD;
1002 +        break;
1003 +  case 26: // Load Doubleword Left
1004 +        transfer_type = SIGSEGV_TRANSFER_LOAD;
1005 +        transfer_size = SIZE_QUAD;
1006 +        direction = -1;
1007 +        break;
1008 +  case 27: // Load Doubleword Right
1009 +        transfer_type = SIGSEGV_TRANSFER_LOAD;
1010 +        transfer_size = SIZE_QUAD;
1011 +        direction = 1;
1012 +        break;
1013 +  case 40: // Store Byte
1014 +        transfer_type = SIGSEGV_TRANSFER_STORE;
1015 +        transfer_size = SIZE_BYTE;
1016 +        break;
1017 +  case 41: // Store Halfword
1018 +        transfer_type = SIGSEGV_TRANSFER_STORE;
1019 +        transfer_size = SIZE_WORD;
1020 +        break;
1021 +  case 43: // Store Word
1022 +  case 42: // Store Word Left
1023 +  case 46: // Store Word Right
1024 +        transfer_type = SIGSEGV_TRANSFER_STORE;
1025 +        transfer_size = SIZE_LONG;
1026 +        break;
1027 +  case 63: // Store Doubleword
1028 +  case 44: // Store Doubleword Left
1029 +  case 45: // Store Doubleword Right
1030 +        transfer_type = SIGSEGV_TRANSFER_STORE;
1031 +        transfer_size = SIZE_QUAD;
1032 +        break;
1033 +  /* Misc instructions unlikely to be used within CPU emulators */
1034 +  case 48: // Load Linked Word
1035 +        transfer_type = SIGSEGV_TRANSFER_LOAD;
1036 +        transfer_size = SIZE_LONG;
1037 +        break;
1038 +  case 52: // Load Linked Doubleword
1039 +        transfer_type = SIGSEGV_TRANSFER_LOAD;
1040 +        transfer_size = SIZE_QUAD;
1041 +        break;
1042 +  case 56: // Store Conditional Word
1043 +        transfer_type = SIGSEGV_TRANSFER_STORE;
1044 +        transfer_size = SIZE_LONG;
1045 +        break;
1046 +  case 60: // Store Conditional Doubleword
1047 +        transfer_type = SIGSEGV_TRANSFER_STORE;
1048 +        transfer_size = SIZE_QUAD;
1049 +        break;
1050 +  }
1051 +
1052 +  if (transfer_type == SIGSEGV_TRANSFER_UNKNOWN) {
1053 +        // Unknown machine code, let it crash. Then patch the decoder
1054 +        return false;
1055 +  }
1056 +
1057 +  // Zero target register in case of a load operation
1058 +  const int reg = (opcode >> 16) & 0x1f;
1059 +  if (transfer_type == SIGSEGV_TRANSFER_LOAD) {
1060 +        if (direction == 0)
1061 +          regs[reg] = 0;
1062 +        else {
1063 +          // FIXME: untested code
1064 +          unsigned long ea = regs[(opcode >> 21) & 0x1f];
1065 +          ea += (signed long)(signed int)(signed short)(opcode & 0xffff);
1066 +          const int offset = ea & (transfer_size == SIZE_LONG ? 3 : 7);
1067 +          unsigned long value;
1068 +          if (direction > 0) {
1069 +                const unsigned long rmask = ~((1L << ((offset + 1) * 8)) - 1);
1070 +                value = regs[reg] & rmask;
1071 +          }
1072 +          else {
1073 +                const unsigned long lmask = (1L << (offset * 8)) - 1;
1074 +                value = regs[reg] & lmask;
1075 +          }
1076 +          // restore most significant bits
1077 +          if (transfer_size == SIZE_LONG)
1078 +                value = (signed long)(signed int)value;
1079 +          regs[reg] = value;
1080 +        }
1081 +  }
1082 +
1083 + #if DEBUG
1084 + #if (defined(_ABIN32) || defined(_ABI64))
1085 +  static const char * mips_gpr_names[32] = {
1086 +        "zero", "at",   "v0",   "v1",   "a0",   "a1",   "a2",   "a3",
1087 +        "t0",   "t1",   "t2",   "t3",   "t4",   "t5",   "t6",   "t7",
1088 +        "s0",   "s1",   "s2",   "s3",   "s4",   "s5",   "s6",   "s7",
1089 +        "t8",   "t9",   "k0",   "k1",   "gp",   "sp",   "s8",   "ra"
1090 +  };
1091 + #else
1092 +  static const char * mips_gpr_names[32] = {
1093 +        "zero", "at",   "v0",   "v1",   "a0",   "a1",   "a2",   "a3",
1094 +        "a4",   "a5",   "a6",   "a7",   "t0",   "t1",   "t2",   "t3",
1095 +        "s0",   "s1",   "s2",   "s3",   "s4",   "s5",   "s6",   "s7",
1096 +        "t8",   "t9",   "k0",   "k1",   "gp",   "sp",   "s8",   "ra"
1097 +  };
1098 + #endif
1099 +  printf("%s %s register %s\n",
1100 +                 transfer_size == SIZE_BYTE ? "byte" :
1101 +                 transfer_size == SIZE_WORD ? "word" :
1102 +                 transfer_size == SIZE_LONG ? "long" :
1103 +                 transfer_size == SIZE_QUAD ? "quad" : "unknown",
1104 +                 transfer_type == SIGSEGV_TRANSFER_LOAD ? "load to" : "store from",
1105 +                 mips_gpr_names[reg]);
1106 + #endif
1107 +
1108 +  regs[MIPS_REG_EPC] += 4;
1109 +  return true;
1110 + }
1111 + #endif
1112 +
1113 + // Decode and skip SPARC instruction
1114 + #if (defined(sparc) || defined(__sparc__))
1115 + enum {
1116 + #if (defined(__sun__))
1117 +  SPARC_REG_G1 = REG_G1,
1118 +  SPARC_REG_O0 = REG_O0,
1119 +  SPARC_REG_PC = REG_PC,
1120 + #endif
1121 + };
1122 + static bool sparc_skip_instruction(unsigned long * regs, gwindows_t * gwins, struct rwindow * rwin)
1123 + {
1124 +  unsigned int * pc = (unsigned int *)regs[SPARC_REG_PC];
1125 +
1126 +  if (pc == 0)
1127 +        return false;
1128 +
1129 + #if DEBUG
1130 +  printf("IP: %p [%08x]\n", pc, pc[0]);
1131 + #endif
1132 +
1133 +  transfer_type_t transfer_type = SIGSEGV_TRANSFER_UNKNOWN;
1134 +  transfer_size_t transfer_size = SIZE_LONG;
1135 +  bool register_pair = false;
1136 +
1137 +  const unsigned int opcode = pc[0];
1138 +  if ((opcode >> 30) != 3)
1139 +        return false;
1140 +  switch ((opcode >> 19) & 0x3f) {
1141 +  case 9: // Load Signed Byte
1142 +  case 1: // Load Unsigned Byte
1143 +        transfer_type = SIGSEGV_TRANSFER_LOAD;
1144 +        transfer_size = SIZE_BYTE;
1145 +        break;
1146 +  case 10:// Load Signed Halfword
1147 +  case 2: // Load Unsigned Word
1148 +        transfer_type = SIGSEGV_TRANSFER_LOAD;
1149 +        transfer_size = SIZE_WORD;
1150 +        break;
1151 +  case 8: // Load Word
1152 +  case 0: // Load Unsigned Word
1153 +        transfer_type = SIGSEGV_TRANSFER_LOAD;
1154 +        transfer_size = SIZE_LONG;
1155 +        break;
1156 +  case 11:// Load Extended Word
1157 +        transfer_type = SIGSEGV_TRANSFER_LOAD;
1158 +        transfer_size = SIZE_QUAD;
1159 +        break;
1160 +  case 3: // Load Doubleword
1161 +        transfer_type = SIGSEGV_TRANSFER_LOAD;
1162 +        transfer_size = SIZE_LONG;
1163 +        register_pair = true;
1164 +        break;
1165 +  case 5: // Store Byte
1166 +        transfer_type = SIGSEGV_TRANSFER_STORE;
1167 +        transfer_size = SIZE_BYTE;
1168 +        break;
1169 +  case 6: // Store Halfword
1170 +        transfer_type = SIGSEGV_TRANSFER_STORE;
1171 +        transfer_size = SIZE_WORD;
1172 +        break;
1173 +  case 4: // Store Word
1174 +        transfer_type = SIGSEGV_TRANSFER_STORE;
1175 +        transfer_size = SIZE_LONG;
1176 +        break;
1177 +  case 14:// Store Extended Word
1178 +        transfer_type = SIGSEGV_TRANSFER_STORE;
1179 +        transfer_size = SIZE_QUAD;
1180 +        break;
1181 +  case 7: // Store Doubleword
1182 +        transfer_type = SIGSEGV_TRANSFER_STORE;
1183 +        transfer_size = SIZE_WORD;
1184 +        register_pair = true;
1185 +        break;
1186 +  }
1187 +
1188 +  if (transfer_type == SIGSEGV_TRANSFER_UNKNOWN) {
1189 +        // Unknown machine code, let it crash. Then patch the decoder
1190 +        return false;
1191 +  }
1192 +
1193 +  // Zero target register in case of a load operation
1194 +  const int reg = (opcode >> 25) & 0x1f;
1195 +  if (transfer_type == SIGSEGV_TRANSFER_LOAD && reg != 0) {
1196 +        // FIXME: code to handle local & input registers is not tested
1197 +        if (reg >= 1 && reg <= 7) {
1198 +          // global registers
1199 +          regs[reg - 1 + SPARC_REG_G1] = 0;
1200 +        }
1201 +        else if (reg >= 8 && reg <= 15) {
1202 +          // output registers
1203 +          regs[reg - 8 + SPARC_REG_O0] = 0;
1204 +        }
1205 +        else if (reg >= 16 && reg <= 23) {
1206 +          // local registers (in register windows)
1207 +          if (gwins)
1208 +                gwins->wbuf->rw_local[reg - 16] = 0;
1209 +          else
1210 +                rwin->rw_local[reg - 16] = 0;
1211 +        }
1212 +        else {
1213 +          // input registers (in register windows)
1214 +          if (gwins)
1215 +                gwins->wbuf->rw_in[reg - 24] = 0;
1216 +          else
1217 +                rwin->rw_in[reg - 24] = 0;
1218 +        }
1219 +  }
1220 +
1221 + #if DEBUG
1222 +  static const char * reg_names[] = {
1223 +        "g0", "g1", "g2", "g3", "g4", "g5", "g6", "g7",
1224 +        "o0", "o1", "o2", "o3", "o4", "o5", "sp", "o7",
1225 +        "l0", "l1", "l2", "l3", "l4", "l5", "l6", "l7",
1226 +        "i0", "i1", "i2", "i3", "i4", "i5", "fp", "i7"
1227 +  };
1228 +  printf("%s %s register %s\n",
1229 +                 transfer_size == SIZE_BYTE ? "byte" :
1230 +                 transfer_size == SIZE_WORD ? "word" :
1231 +                 transfer_size == SIZE_LONG ? "long" :
1232 +                 transfer_size == SIZE_QUAD ? "quad" : "unknown",
1233 +                 transfer_type == SIGSEGV_TRANSFER_LOAD ? "load to" : "store from",
1234 +                 reg_names[reg]);
1235 + #endif
1236 +
1237 +  regs[SPARC_REG_PC] += 4;
1238 +  return true;
1239 + }
1240 + #endif
1241   #endif
1242  
1243   // Fallbacks
# Line 1235 | Line 1671 | static void *b_region, *e_region;
1671  
1672   static sigsegv_return_t sigsegv_test_handler(sigsegv_address_t fault_address, sigsegv_address_t instruction_address)
1673   {
1674 + #if DEBUG
1675 +        printf("sigsegv_test_handler(%p, %p)\n", fault_address, instruction_address);
1676 +        printf("expected fault at %p\n", page + REF_INDEX);
1677 + #ifdef __GNUC__
1678 +        printf("expected instruction address range: %p-%p\n", b_region, e_region);
1679 + #endif
1680 + #endif
1681          handler_called++;
1682          if ((fault_address - REF_INDEX) != page)
1683                  exit(10);
# Line 1268 | Line 1711 | static sigsegv_return_t sigsegv_insn_han
1711  
1712          return SIGSEGV_RETURN_FAILURE;
1713   }
1714 +
1715 + // More sophisticated tests for instruction skipper
1716 + static bool arch_insn_skipper_tests()
1717 + {
1718 + #if (defined(i386) || defined(__i386__)) || defined(__x86_64__)
1719 +        static const unsigned char code[] = {
1720 +                0x8a, 0x00,                    // mov    (%eax),%al
1721 +                0x8a, 0x2c, 0x18,              // mov    (%eax,%ebx,1),%ch
1722 +                0x88, 0x20,                    // mov    %ah,(%eax)
1723 +                0x88, 0x08,                    // mov    %cl,(%eax)
1724 +                0x66, 0x8b, 0x00,              // mov    (%eax),%ax
1725 +                0x66, 0x8b, 0x0c, 0x18,        // mov    (%eax,%ebx,1),%cx
1726 +                0x66, 0x89, 0x00,              // mov    %ax,(%eax)
1727 +                0x66, 0x89, 0x0c, 0x18,        // mov    %cx,(%eax,%ebx,1)
1728 +                0x8b, 0x00,                    // mov    (%eax),%eax
1729 +                0x8b, 0x0c, 0x18,              // mov    (%eax,%ebx,1),%ecx
1730 +                0x89, 0x00,                    // mov    %eax,(%eax)
1731 +                0x89, 0x0c, 0x18,              // mov    %ecx,(%eax,%ebx,1)
1732 + #if defined(__x86_64__)
1733 +                0x44, 0x8a, 0x00,              // mov    (%rax),%r8b
1734 +                0x44, 0x8a, 0x20,              // mov    (%rax),%r12b
1735 +                0x42, 0x8a, 0x3c, 0x10,        // mov    (%rax,%r10,1),%dil
1736 +                0x44, 0x88, 0x00,              // mov    %r8b,(%rax)
1737 +                0x44, 0x88, 0x20,              // mov    %r12b,(%rax)
1738 +                0x42, 0x88, 0x3c, 0x10,        // mov    %dil,(%rax,%r10,1)
1739 +                0x66, 0x44, 0x8b, 0x00,        // mov    (%rax),%r8w
1740 +                0x66, 0x42, 0x8b, 0x0c, 0x10,  // mov    (%rax,%r10,1),%cx
1741 +                0x66, 0x44, 0x89, 0x00,        // mov    %r8w,(%rax)
1742 +                0x66, 0x42, 0x89, 0x0c, 0x10,  // mov    %cx,(%rax,%r10,1)
1743 +                0x44, 0x8b, 0x00,              // mov    (%rax),%r8d
1744 +                0x42, 0x8b, 0x0c, 0x10,        // mov    (%rax,%r10,1),%ecx
1745 +                0x44, 0x89, 0x00,              // mov    %r8d,(%rax)
1746 +                0x42, 0x89, 0x0c, 0x10,        // mov    %ecx,(%rax,%r10,1)
1747 +                0x48, 0x8b, 0x08,              // mov    (%rax),%rcx
1748 +                0x4c, 0x8b, 0x18,              // mov    (%rax),%r11
1749 +                0x4a, 0x8b, 0x0c, 0x10,        // mov    (%rax,%r10,1),%rcx
1750 +                0x4e, 0x8b, 0x1c, 0x10,        // mov    (%rax,%r10,1),%r11
1751 +                0x48, 0x89, 0x08,              // mov    %rcx,(%rax)
1752 +                0x4c, 0x89, 0x18,              // mov    %r11,(%rax)
1753 +                0x4a, 0x89, 0x0c, 0x10,        // mov    %rcx,(%rax,%r10,1)
1754 +                0x4e, 0x89, 0x1c, 0x10,        // mov    %r11,(%rax,%r10,1)
1755 + #endif
1756 +                0                              // end
1757 +        };
1758 +        const int N_REGS = 20;
1759 +        unsigned long regs[N_REGS];
1760 +        for (int i = 0; i < N_REGS; i++)
1761 +                regs[i] = i;
1762 +        const unsigned long start_code = (unsigned long)&code;
1763 +        regs[X86_REG_EIP] = start_code;
1764 +        while ((regs[X86_REG_EIP] - start_code) < (sizeof(code) - 1)
1765 +                   && ix86_skip_instruction(regs))
1766 +                ; /* simply iterate */
1767 +        return (regs[X86_REG_EIP] - start_code) == (sizeof(code) - 1);
1768 + #endif
1769 +        return true;
1770 + }
1771   #endif
1772  
1773   int main(void)
# Line 1314 | Line 1814 | int main(void)
1814                  return 8;
1815          
1816   #define TEST_SKIP_INSTRUCTION(TYPE) do {                                \
1817 <                const unsigned int TAG = 0x12345678;                    \
1817 >                const unsigned long TAG = 0x12345678 |                  \
1818 >                (sizeof(long) == 8 ? 0x9abcdef0UL << 31 : 0);   \
1819                  TYPE data = *((TYPE *)(page + sizeof(TYPE)));   \
1820 <                volatile unsigned int effect = data + TAG;              \
1820 >                volatile unsigned long effect = data + TAG;             \
1821                  if (effect != TAG)                                                              \
1822                          return 9;                                                                       \
1823          } while (0)
# Line 1329 | Line 1830 | int main(void)
1830          TEST_SKIP_INSTRUCTION(unsigned char);
1831          TEST_SKIP_INSTRUCTION(unsigned short);
1832          TEST_SKIP_INSTRUCTION(unsigned int);
1833 +        TEST_SKIP_INSTRUCTION(unsigned long);
1834   L_e_region2:
1835 +
1836 +        if (!arch_insn_skipper_tests())
1837 +                return 20;
1838   #endif
1839  
1840          vm_exit();
1841          return 0;
1842   }
1843   #endif
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines