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