blob: 2d741bbf94842ae365ca9a3c2e3d27fa438568da [file] [log] [blame]
Ian Wienand5570a772006-02-17 02:00:00 +01001/* 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 Cespedesf1350522008-12-16 18:19:58 +010014static long long
15extract_bit_field(char *bundle, int from, int len) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +010016 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 Wienand5570a772006-02-17 02:00:00 +010024
Ian Wienand2d45b1a2006-02-20 22:48:07 +010025 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 Wienand5570a772006-02-17 02:00:00 +010030
Ian Wienand2d45b1a2006-02-20 22:48:07 +010031 for (i = from_byte + 1; i < to_byte; i++) {
32 result |= ((long long)b[i]) << lshift;
33 lshift += 8;
34 }
Ian Wienand5570a772006-02-17 02:00:00 +010035
Ian Wienand2d45b1a2006-02-20 22:48:07 +010036 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 Wienand5570a772006-02-17 02:00:00 +010041
Ian Wienand2d45b1a2006-02-20 22:48:07 +010042 return result;
Ian Wienand5570a772006-02-17 02:00:00 +010043}
44
45/* Replace the specified bits in an instruction bundle */
Juan Cespedesf1350522008-12-16 18:19:58 +010046static void
47replace_bit_field(char *bundle, long long val, int from, int len) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +010048 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 Wienand5570a772006-02-17 02:00:00 +010053
Ian Wienand2d45b1a2006-02-20 22:48:07 +010054 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 Wienand5570a772006-02-17 02:00:00 +010072
Ian Wienand2d45b1a2006-02-20 22:48:07 +010073 for (i = from_byte + 1; i < to_byte; i++) {
74 c = val & 0xff;
75 val >>= 8;
76 b[i] = c;
77 }
Ian Wienand5570a772006-02-17 02:00:00 +010078
Ian Wienand2d45b1a2006-02-20 22:48:07 +010079 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 Wienand5570a772006-02-17 02:00:00 +010088}
89
90/* Return the contents of slot N (for N = 0, 1, or 2) in
91 and instruction bundle */
Juan Cespedesf1350522008-12-16 18:19:58 +010092static long long
93slotN_contents(char *bundle, int slotnum) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +010094 return extract_bit_field(bundle, 5 + 41 * slotnum, 41);
Ian Wienand5570a772006-02-17 02:00:00 +010095}
96
97/* Store an instruction in an instruction bundle */
98
Juan Cespedesf1350522008-12-16 18:19:58 +010099static void
100replace_slotN_contents(char *bundle, long long instr, int slotnum) {
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100101 replace_bit_field(bundle, instr, 5 + 41 * slotnum, 41);
Ian Wienand5570a772006-02-17 02:00:00 +0100102}
103
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100104typedef 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 Wienand5570a772006-02-17 02:00:00 +0100113} instruction_type;
114
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100115static 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 Wienand5570a772006-02-17 02:00:00 +0100148};
149
150union bundle_t {
151 char cbundle[16];
152 unsigned long ubundle[2];
153};
154
Juan Cespedesf1350522008-12-16 18:19:58 +0100155void
156arch_enable_breakpoint(pid_t pid, struct breakpoint *sbp) {
Ian Wienand5570a772006-02-17 02:00:00 +0100157
158 unsigned long addr = (unsigned long)sbp->addr;
159 union bundle_t bundle;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100160 int slotnum = (int)(addr & 0x0f) & 0x3;
Ian Wienand5570a772006-02-17 02:00:00 +0100161 long long instr;
162 int template;
163
164 debug(1, "Enable Breakpoint at %p)", sbp->addr);
165
166 if (slotnum > 2)
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100167 printf
168 ("Can't insert breakpoint for slot numbers greater than 2.");
169
Ian Wienand5570a772006-02-17 02:00:00 +0100170 addr &= ~0x0f;
171 bundle.ubundle[0] = ptrace(PTRACE_PEEKTEXT, pid, addr, 0);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100172 bundle.ubundle[1] = ptrace(PTRACE_PEEKTEXT, pid, addr + 8, 0);
Ian Wienand5570a772006-02-17 02:00:00 +0100173
174 /* Check for L type instruction in 2nd slot, if present then
175 bump up the slot number to the 3rd slot */
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100176 template = extract_bit_field(bundle.cbundle, 0, 5);
177 if (slotnum == 1 && template_encoding_table[template][1] == L) {
Ian Wienand5570a772006-02-17 02:00:00 +0100178 slotnum = 2;
179 }
180
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100181 instr = slotN_contents(bundle.cbundle, slotnum);
Ian Wienand5570a772006-02-17 02:00:00 +0100182
183 memcpy(sbp->orig_value, &instr, sizeof(instr));
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100184
185 replace_slotN_contents(bundle.cbundle, 0x00002000040LL, slotnum);
Ian Wienand5570a772006-02-17 02:00:00 +0100186
187 ptrace(PTRACE_POKETEXT, pid, addr, bundle.ubundle[0]);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100188 ptrace(PTRACE_POKETEXT, pid, addr + 8, bundle.ubundle[1]);
Ian Wienand5570a772006-02-17 02:00:00 +0100189
190}
191
Juan Cespedesf1350522008-12-16 18:19:58 +0100192void
193arch_disable_breakpoint(pid_t pid, const struct breakpoint *sbp) {
Ian Wienand5570a772006-02-17 02:00:00 +0100194
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 Wienand2d45b1a2006-02-20 22:48:07 +0100203
Ian Wienand5570a772006-02-17 02:00:00 +0100204 bundle.ubundle[0] = ptrace(PTRACE_PEEKTEXT, pid, addr, 0);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100205 bundle.ubundle[1] = ptrace(PTRACE_PEEKTEXT, pid, addr + 8, 0);
Ian Wienand5570a772006-02-17 02:00:00 +0100206
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100207 memcpy(&instr, sbp->orig_value, sizeof(instr));
Ian Wienand5570a772006-02-17 02:00:00 +0100208
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100209 replace_slotN_contents(bundle.cbundle, instr, slotnum);
Ian Wienand5570a772006-02-17 02:00:00 +0100210 ptrace(PTRACE_POKETEXT, pid, addr, bundle.ubundle[0]);
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100211 ptrace(PTRACE_POKETEXT, pid, addr + 8, bundle.ubundle[1]);
Ian Wienand5570a772006-02-17 02:00:00 +0100212}