| Ian Wienand | 5570a77 | 2006-02-17 02:00:00 +0100 | [diff] [blame] | 1 | /* IA64 breakpoint support. Much of this clagged from gdb |
| 2 | * -Ian Wienand <ianw@gelato.unsw.edu.au> 10/3/2005 |
| 3 | */ |
| 4 | |
| 5 | #include "config.h" |
| 6 | |
| 7 | #include <sys/ptrace.h> |
| 8 | #include <string.h> |
| 9 | #include "arch.h" |
| 10 | #include "options.h" |
| 11 | #include "output.h" |
| 12 | #include "debug.h" |
| 13 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame^] | 14 | static long long |
| 15 | extract_bit_field(char *bundle, int from, int len) { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 16 | long long result = 0LL; |
| 17 | int to = from + len; |
| 18 | int from_byte = from / 8; |
| 19 | int to_byte = to / 8; |
| 20 | unsigned char *b = (unsigned char *)bundle; |
| 21 | unsigned char c; |
| 22 | int lshift; |
| 23 | int i; |
| Ian Wienand | 5570a77 | 2006-02-17 02:00:00 +0100 | [diff] [blame] | 24 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 25 | c = b[from_byte]; |
| 26 | if (from_byte == to_byte) |
| 27 | c = ((unsigned char)(c << (8 - to % 8))) >> (8 - to % 8); |
| 28 | result = c >> (from % 8); |
| 29 | lshift = 8 - (from % 8); |
| Ian Wienand | 5570a77 | 2006-02-17 02:00:00 +0100 | [diff] [blame] | 30 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 31 | for (i = from_byte + 1; i < to_byte; i++) { |
| 32 | result |= ((long long)b[i]) << lshift; |
| 33 | lshift += 8; |
| 34 | } |
| Ian Wienand | 5570a77 | 2006-02-17 02:00:00 +0100 | [diff] [blame] | 35 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 36 | if (from_byte < to_byte && (to % 8 != 0)) { |
| 37 | c = b[to_byte]; |
| 38 | c = ((unsigned char)(c << (8 - to % 8))) >> (8 - to % 8); |
| 39 | result |= ((long long)c) << lshift; |
| 40 | } |
| Ian Wienand | 5570a77 | 2006-02-17 02:00:00 +0100 | [diff] [blame] | 41 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 42 | return result; |
| Ian Wienand | 5570a77 | 2006-02-17 02:00:00 +0100 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | /* Replace the specified bits in an instruction bundle */ |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame^] | 46 | static void |
| 47 | replace_bit_field(char *bundle, long long val, int from, int len) { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 48 | int to = from + len; |
| 49 | int from_byte = from / 8; |
| 50 | int to_byte = to / 8; |
| 51 | unsigned char *b = (unsigned char *)bundle; |
| 52 | unsigned char c; |
| Ian Wienand | 5570a77 | 2006-02-17 02:00:00 +0100 | [diff] [blame] | 53 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 54 | if (from_byte == to_byte) { |
| 55 | unsigned char left, right; |
| 56 | c = b[from_byte]; |
| 57 | left = (c >> (to % 8)) << (to % 8); |
| 58 | right = |
| 59 | ((unsigned char)(c << (8 - from % 8))) >> (8 - from % 8); |
| 60 | c = (unsigned char)(val & 0xff); |
| 61 | c = (unsigned char)(c << (from % 8 + 8 - to % 8)) >> (8 - |
| 62 | to % 8); |
| 63 | c |= right | left; |
| 64 | b[from_byte] = c; |
| 65 | } else { |
| 66 | int i; |
| 67 | c = b[from_byte]; |
| 68 | c = ((unsigned char)(c << (8 - from % 8))) >> (8 - from % 8); |
| 69 | c = c | (val << (from % 8)); |
| 70 | b[from_byte] = c; |
| 71 | val >>= 8 - from % 8; |
| Ian Wienand | 5570a77 | 2006-02-17 02:00:00 +0100 | [diff] [blame] | 72 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 73 | for (i = from_byte + 1; i < to_byte; i++) { |
| 74 | c = val & 0xff; |
| 75 | val >>= 8; |
| 76 | b[i] = c; |
| 77 | } |
| Ian Wienand | 5570a77 | 2006-02-17 02:00:00 +0100 | [diff] [blame] | 78 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 79 | if (to % 8 != 0) { |
| 80 | unsigned char cv = (unsigned char)val; |
| 81 | c = b[to_byte]; |
| 82 | c = c >> (to % 8) << (to % 8); |
| 83 | c |= ((unsigned char)(cv << (8 - to % 8))) >> (8 - |
| 84 | to % 8); |
| 85 | b[to_byte] = c; |
| 86 | } |
| 87 | } |
| Ian Wienand | 5570a77 | 2006-02-17 02:00:00 +0100 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | /* Return the contents of slot N (for N = 0, 1, or 2) in |
| 91 | and instruction bundle */ |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame^] | 92 | static long long |
| 93 | slotN_contents(char *bundle, int slotnum) { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 94 | return extract_bit_field(bundle, 5 + 41 * slotnum, 41); |
| Ian Wienand | 5570a77 | 2006-02-17 02:00:00 +0100 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | /* Store an instruction in an instruction bundle */ |
| 98 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame^] | 99 | static void |
| 100 | replace_slotN_contents(char *bundle, long long instr, int slotnum) { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 101 | replace_bit_field(bundle, instr, 5 + 41 * slotnum, 41); |
| Ian Wienand | 5570a77 | 2006-02-17 02:00:00 +0100 | [diff] [blame] | 102 | } |
| 103 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 104 | typedef enum instruction_type { |
| 105 | A, /* Integer ALU ; I-unit or M-unit */ |
| 106 | I, /* Non-ALU integer; I-unit */ |
| 107 | M, /* Memory ; M-unit */ |
| 108 | F, /* Floating-point ; F-unit */ |
| 109 | B, /* Branch ; B-unit */ |
| 110 | L, /* Extended (L+X) ; I-unit */ |
| 111 | X, /* Extended (L+X) ; I-unit */ |
| 112 | undefined /* undefined or reserved */ |
| Ian Wienand | 5570a77 | 2006-02-17 02:00:00 +0100 | [diff] [blame] | 113 | } instruction_type; |
| 114 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 115 | static enum instruction_type template_encoding_table[32][3] = { |
| 116 | {M, I, I}, /* 00 */ |
| 117 | {M, I, I}, /* 01 */ |
| 118 | {M, I, I}, /* 02 */ |
| 119 | {M, I, I}, /* 03 */ |
| 120 | {M, L, X}, /* 04 */ |
| 121 | {M, L, X}, /* 05 */ |
| 122 | {undefined, undefined, undefined}, /* 06 */ |
| 123 | {undefined, undefined, undefined}, /* 07 */ |
| 124 | {M, M, I}, /* 08 */ |
| 125 | {M, M, I}, /* 09 */ |
| 126 | {M, M, I}, /* 0A */ |
| 127 | {M, M, I}, /* 0B */ |
| 128 | {M, F, I}, /* 0C */ |
| 129 | {M, F, I}, /* 0D */ |
| 130 | {M, M, F}, /* 0E */ |
| 131 | {M, M, F}, /* 0F */ |
| 132 | {M, I, B}, /* 10 */ |
| 133 | {M, I, B}, /* 11 */ |
| 134 | {M, B, B}, /* 12 */ |
| 135 | {M, B, B}, /* 13 */ |
| 136 | {undefined, undefined, undefined}, /* 14 */ |
| 137 | {undefined, undefined, undefined}, /* 15 */ |
| 138 | {B, B, B}, /* 16 */ |
| 139 | {B, B, B}, /* 17 */ |
| 140 | {M, M, B}, /* 18 */ |
| 141 | {M, M, B}, /* 19 */ |
| 142 | {undefined, undefined, undefined}, /* 1A */ |
| 143 | {undefined, undefined, undefined}, /* 1B */ |
| 144 | {M, F, B}, /* 1C */ |
| 145 | {M, F, B}, /* 1D */ |
| 146 | {undefined, undefined, undefined}, /* 1E */ |
| 147 | {undefined, undefined, undefined}, /* 1F */ |
| Ian Wienand | 5570a77 | 2006-02-17 02:00:00 +0100 | [diff] [blame] | 148 | }; |
| 149 | |
| 150 | union bundle_t { |
| 151 | char cbundle[16]; |
| 152 | unsigned long ubundle[2]; |
| 153 | }; |
| 154 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame^] | 155 | void |
| 156 | arch_enable_breakpoint(pid_t pid, struct breakpoint *sbp) { |
| Ian Wienand | 5570a77 | 2006-02-17 02:00:00 +0100 | [diff] [blame] | 157 | |
| 158 | unsigned long addr = (unsigned long)sbp->addr; |
| 159 | union bundle_t bundle; |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 160 | int slotnum = (int)(addr & 0x0f) & 0x3; |
| Ian Wienand | 5570a77 | 2006-02-17 02:00:00 +0100 | [diff] [blame] | 161 | long long instr; |
| 162 | int template; |
| 163 | |
| 164 | debug(1, "Enable Breakpoint at %p)", sbp->addr); |
| 165 | |
| 166 | if (slotnum > 2) |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 167 | printf |
| 168 | ("Can't insert breakpoint for slot numbers greater than 2."); |
| 169 | |
| Ian Wienand | 5570a77 | 2006-02-17 02:00:00 +0100 | [diff] [blame] | 170 | addr &= ~0x0f; |
| 171 | bundle.ubundle[0] = ptrace(PTRACE_PEEKTEXT, pid, addr, 0); |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 172 | bundle.ubundle[1] = ptrace(PTRACE_PEEKTEXT, pid, addr + 8, 0); |
| Ian Wienand | 5570a77 | 2006-02-17 02:00:00 +0100 | [diff] [blame] | 173 | |
| 174 | /* Check for L type instruction in 2nd slot, if present then |
| 175 | bump up the slot number to the 3rd slot */ |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 176 | template = extract_bit_field(bundle.cbundle, 0, 5); |
| 177 | if (slotnum == 1 && template_encoding_table[template][1] == L) { |
| Ian Wienand | 5570a77 | 2006-02-17 02:00:00 +0100 | [diff] [blame] | 178 | slotnum = 2; |
| 179 | } |
| 180 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 181 | instr = slotN_contents(bundle.cbundle, slotnum); |
| Ian Wienand | 5570a77 | 2006-02-17 02:00:00 +0100 | [diff] [blame] | 182 | |
| 183 | memcpy(sbp->orig_value, &instr, sizeof(instr)); |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 184 | |
| 185 | replace_slotN_contents(bundle.cbundle, 0x00002000040LL, slotnum); |
| Ian Wienand | 5570a77 | 2006-02-17 02:00:00 +0100 | [diff] [blame] | 186 | |
| 187 | ptrace(PTRACE_POKETEXT, pid, addr, bundle.ubundle[0]); |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 188 | ptrace(PTRACE_POKETEXT, pid, addr + 8, bundle.ubundle[1]); |
| Ian Wienand | 5570a77 | 2006-02-17 02:00:00 +0100 | [diff] [blame] | 189 | |
| 190 | } |
| 191 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame^] | 192 | void |
| 193 | arch_disable_breakpoint(pid_t pid, const struct breakpoint *sbp) { |
| Ian Wienand | 5570a77 | 2006-02-17 02:00:00 +0100 | [diff] [blame] | 194 | |
| 195 | unsigned long addr = (unsigned long)sbp->addr; |
| 196 | int slotnum = (int)(addr & 0x0f) & 0x3; |
| 197 | union bundle_t bundle; |
| 198 | unsigned long instr; |
| 199 | |
| 200 | debug(1, "Disable Breakpoint at %p", sbp->addr); |
| 201 | |
| 202 | addr &= ~0x0f; |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 203 | |
| Ian Wienand | 5570a77 | 2006-02-17 02:00:00 +0100 | [diff] [blame] | 204 | bundle.ubundle[0] = ptrace(PTRACE_PEEKTEXT, pid, addr, 0); |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 205 | bundle.ubundle[1] = ptrace(PTRACE_PEEKTEXT, pid, addr + 8, 0); |
| Ian Wienand | 5570a77 | 2006-02-17 02:00:00 +0100 | [diff] [blame] | 206 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 207 | memcpy(&instr, sbp->orig_value, sizeof(instr)); |
| Ian Wienand | 5570a77 | 2006-02-17 02:00:00 +0100 | [diff] [blame] | 208 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 209 | replace_slotN_contents(bundle.cbundle, instr, slotnum); |
| Ian Wienand | 5570a77 | 2006-02-17 02:00:00 +0100 | [diff] [blame] | 210 | ptrace(PTRACE_POKETEXT, pid, addr, bundle.ubundle[0]); |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 211 | ptrace(PTRACE_POKETEXT, pid, addr + 8, bundle.ubundle[1]); |
| Ian Wienand | 5570a77 | 2006-02-17 02:00:00 +0100 | [diff] [blame] | 212 | } |