Add xfail and notarget.
[deliverable/binutils-gdb.git] / gdb / ia64-tdep.c
CommitLineData
16461d7d 1/* Target-dependent code for the IA-64 for GDB, the GNU debugger.
ca557f44 2
0fb0cc75 3 Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
7b6bb8da 4 2009, 2010, 2011 Free Software Foundation, Inc.
16461d7d
KB
5
6 This file is part of GDB.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
a9762ec7 10 the Free Software Foundation; either version 3 of the License, or
16461d7d
KB
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
a9762ec7 19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
16461d7d
KB
20
21#include "defs.h"
22#include "inferior.h"
16461d7d 23#include "gdbcore.h"
8064c6ae 24#include "arch-utils.h"
16461d7d 25#include "floatformat.h"
e6bb342a 26#include "gdbtypes.h"
4e052eda 27#include "regcache.h"
004d836a
JJ
28#include "reggroups.h"
29#include "frame.h"
30#include "frame-base.h"
31#include "frame-unwind.h"
d16aafd8 32#include "doublest.h"
fd0407d6 33#include "value.h"
bd1ce8ba 34#include "gdb_assert.h"
16461d7d
KB
35#include "objfiles.h"
36#include "elf/common.h" /* for DT_PLTGOT value */
244bc108 37#include "elf-bfd.h"
a89aa300 38#include "dis-asm.h"
7d9b040b 39#include "infcall.h"
b33e8514 40#include "osabi.h"
9fc9f5e2 41#include "ia64-tdep.h"
0d5de010 42#include "cp-abi.h"
16461d7d 43
968d1cb4 44#ifdef HAVE_LIBUNWIND_IA64_H
8973ff21 45#include "elf/ia64.h" /* for PT_IA_64_UNWIND value */
968d1cb4
JJ
46#include "libunwind-frame.h"
47#include "libunwind-ia64.h"
c5a27d9c
JJ
48
49/* Note: KERNEL_START is supposed to be an address which is not going
50 to ever contain any valid unwind info. For ia64 linux, the choice
51 of 0xc000000000000000 is fairly safe since that's uncached space.
52
53 We use KERNEL_START as follows: after obtaining the kernel's
54 unwind table via getunwind(), we project its unwind data into
55 address-range KERNEL_START-(KERNEL_START+ktab_size) and then
56 when ia64_access_mem() sees a memory access to this
57 address-range, we redirect it to ktab instead.
58
59 None of this hackery is needed with a modern kernel/libcs
60 which uses the kernel virtual DSO to provide access to the
61 kernel's unwind info. In that case, ktab_size remains 0 and
62 hence the value of KERNEL_START doesn't matter. */
63
64#define KERNEL_START 0xc000000000000000ULL
65
66static size_t ktab_size = 0;
67struct ia64_table_entry
68 {
69 uint64_t start_offset;
70 uint64_t end_offset;
71 uint64_t info_offset;
72 };
73
74static struct ia64_table_entry *ktab = NULL;
75
968d1cb4
JJ
76#endif
77
698cb3f0
KB
78/* An enumeration of the different IA-64 instruction types. */
79
16461d7d
KB
80typedef enum instruction_type
81{
82 A, /* Integer ALU ; I-unit or M-unit */
83 I, /* Non-ALU integer; I-unit */
84 M, /* Memory ; M-unit */
85 F, /* Floating-point ; F-unit */
86 B, /* Branch ; B-unit */
87 L, /* Extended (L+X) ; I-unit */
88 X, /* Extended (L+X) ; I-unit */
89 undefined /* undefined or reserved */
90} instruction_type;
91
92/* We represent IA-64 PC addresses as the value of the instruction
93 pointer or'd with some bit combination in the low nibble which
94 represents the slot number in the bundle addressed by the
95 instruction pointer. The problem is that the Linux kernel
96 multiplies its slot numbers (for exceptions) by one while the
97 disassembler multiplies its slot numbers by 6. In addition, I've
98 heard it said that the simulator uses 1 as the multiplier.
99
100 I've fixed the disassembler so that the bytes_per_line field will
101 be the slot multiplier. If bytes_per_line comes in as zero, it
102 is set to six (which is how it was set up initially). -- objdump
103 displays pretty disassembly dumps with this value. For our purposes,
104 we'll set bytes_per_line to SLOT_MULTIPLIER. This is okay since we
1777feb0 105 never want to also display the raw bytes the way objdump does. */
16461d7d
KB
106
107#define SLOT_MULTIPLIER 1
108
1777feb0 109/* Length in bytes of an instruction bundle. */
16461d7d
KB
110
111#define BUNDLE_LEN 16
112
939c61fa
JK
113/* See the saved memory layout comment for ia64_memory_insert_breakpoint. */
114
115#if BREAKPOINT_MAX < BUNDLE_LEN - 2
116# error "BREAKPOINT_MAX < BUNDLE_LEN - 2"
117#endif
118
16461d7d
KB
119static gdbarch_init_ftype ia64_gdbarch_init;
120
121static gdbarch_register_name_ftype ia64_register_name;
004d836a 122static gdbarch_register_type_ftype ia64_register_type;
16461d7d 123static gdbarch_breakpoint_from_pc_ftype ia64_breakpoint_from_pc;
16461d7d 124static gdbarch_skip_prologue_ftype ia64_skip_prologue;
64a5b29c 125static struct type *is_float_or_hfa_type (struct type *t);
e17a4113
UW
126static CORE_ADDR ia64_find_global_pointer (struct gdbarch *gdbarch,
127 CORE_ADDR faddr);
16461d7d 128
004d836a 129#define NUM_IA64_RAW_REGS 462
16461d7d 130
16461d7d
KB
131static int sp_regnum = IA64_GR12_REGNUM;
132static int fp_regnum = IA64_VFP_REGNUM;
133static int lr_regnum = IA64_VRAP_REGNUM;
134
1777feb0
MS
135/* NOTE: we treat the register stack registers r32-r127 as
136 pseudo-registers because they may not be accessible via the ptrace
137 register get/set interfaces. */
138
139enum pseudo_regs { FIRST_PSEUDO_REGNUM = NUM_IA64_RAW_REGS,
140 VBOF_REGNUM = IA64_NAT127_REGNUM + 1, V32_REGNUM,
004d836a 141 V127_REGNUM = V32_REGNUM + 95,
1777feb0
MS
142 VP0_REGNUM, VP16_REGNUM = VP0_REGNUM + 16,
143 VP63_REGNUM = VP0_REGNUM + 63, LAST_PSEUDO_REGNUM };
16461d7d
KB
144
145/* Array of register names; There should be ia64_num_regs strings in
146 the initializer. */
147
148static char *ia64_register_names[] =
149{ "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
150 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
151 "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
152 "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
004d836a
JJ
153 "", "", "", "", "", "", "", "",
154 "", "", "", "", "", "", "", "",
155 "", "", "", "", "", "", "", "",
156 "", "", "", "", "", "", "", "",
157 "", "", "", "", "", "", "", "",
158 "", "", "", "", "", "", "", "",
159 "", "", "", "", "", "", "", "",
160 "", "", "", "", "", "", "", "",
161 "", "", "", "", "", "", "", "",
162 "", "", "", "", "", "", "", "",
163 "", "", "", "", "", "", "", "",
164 "", "", "", "", "", "", "", "",
16461d7d
KB
165
166 "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
167 "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15",
168 "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",
169 "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31",
170 "f32", "f33", "f34", "f35", "f36", "f37", "f38", "f39",
171 "f40", "f41", "f42", "f43", "f44", "f45", "f46", "f47",
172 "f48", "f49", "f50", "f51", "f52", "f53", "f54", "f55",
173 "f56", "f57", "f58", "f59", "f60", "f61", "f62", "f63",
174 "f64", "f65", "f66", "f67", "f68", "f69", "f70", "f71",
175 "f72", "f73", "f74", "f75", "f76", "f77", "f78", "f79",
176 "f80", "f81", "f82", "f83", "f84", "f85", "f86", "f87",
177 "f88", "f89", "f90", "f91", "f92", "f93", "f94", "f95",
178 "f96", "f97", "f98", "f99", "f100", "f101", "f102", "f103",
179 "f104", "f105", "f106", "f107", "f108", "f109", "f110", "f111",
180 "f112", "f113", "f114", "f115", "f116", "f117", "f118", "f119",
181 "f120", "f121", "f122", "f123", "f124", "f125", "f126", "f127",
182
004d836a
JJ
183 "", "", "", "", "", "", "", "",
184 "", "", "", "", "", "", "", "",
185 "", "", "", "", "", "", "", "",
186 "", "", "", "", "", "", "", "",
187 "", "", "", "", "", "", "", "",
188 "", "", "", "", "", "", "", "",
189 "", "", "", "", "", "", "", "",
190 "", "", "", "", "", "", "", "",
16461d7d
KB
191
192 "b0", "b1", "b2", "b3", "b4", "b5", "b6", "b7",
193
194 "vfp", "vrap",
195
196 "pr", "ip", "psr", "cfm",
197
198 "kr0", "kr1", "kr2", "kr3", "kr4", "kr5", "kr6", "kr7",
199 "", "", "", "", "", "", "", "",
200 "rsc", "bsp", "bspstore", "rnat",
201 "", "fcr", "", "",
202 "eflag", "csd", "ssd", "cflg", "fsr", "fir", "fdr", "",
203 "ccv", "", "", "", "unat", "", "", "",
204 "fpsr", "", "", "", "itc",
205 "", "", "", "", "", "", "", "", "", "",
206 "", "", "", "", "", "", "", "", "",
207 "pfs", "lc", "ec",
208 "", "", "", "", "", "", "", "", "", "",
209 "", "", "", "", "", "", "", "", "", "",
210 "", "", "", "", "", "", "", "", "", "",
211 "", "", "", "", "", "", "", "", "", "",
212 "", "", "", "", "", "", "", "", "", "",
213 "", "", "", "", "", "", "", "", "", "",
214 "",
215 "nat0", "nat1", "nat2", "nat3", "nat4", "nat5", "nat6", "nat7",
216 "nat8", "nat9", "nat10", "nat11", "nat12", "nat13", "nat14", "nat15",
217 "nat16", "nat17", "nat18", "nat19", "nat20", "nat21", "nat22", "nat23",
218 "nat24", "nat25", "nat26", "nat27", "nat28", "nat29", "nat30", "nat31",
219 "nat32", "nat33", "nat34", "nat35", "nat36", "nat37", "nat38", "nat39",
220 "nat40", "nat41", "nat42", "nat43", "nat44", "nat45", "nat46", "nat47",
221 "nat48", "nat49", "nat50", "nat51", "nat52", "nat53", "nat54", "nat55",
222 "nat56", "nat57", "nat58", "nat59", "nat60", "nat61", "nat62", "nat63",
223 "nat64", "nat65", "nat66", "nat67", "nat68", "nat69", "nat70", "nat71",
224 "nat72", "nat73", "nat74", "nat75", "nat76", "nat77", "nat78", "nat79",
225 "nat80", "nat81", "nat82", "nat83", "nat84", "nat85", "nat86", "nat87",
226 "nat88", "nat89", "nat90", "nat91", "nat92", "nat93", "nat94", "nat95",
227 "nat96", "nat97", "nat98", "nat99", "nat100","nat101","nat102","nat103",
228 "nat104","nat105","nat106","nat107","nat108","nat109","nat110","nat111",
229 "nat112","nat113","nat114","nat115","nat116","nat117","nat118","nat119",
230 "nat120","nat121","nat122","nat123","nat124","nat125","nat126","nat127",
004d836a
JJ
231
232 "bof",
233
234 "r32", "r33", "r34", "r35", "r36", "r37", "r38", "r39",
235 "r40", "r41", "r42", "r43", "r44", "r45", "r46", "r47",
236 "r48", "r49", "r50", "r51", "r52", "r53", "r54", "r55",
237 "r56", "r57", "r58", "r59", "r60", "r61", "r62", "r63",
238 "r64", "r65", "r66", "r67", "r68", "r69", "r70", "r71",
239 "r72", "r73", "r74", "r75", "r76", "r77", "r78", "r79",
240 "r80", "r81", "r82", "r83", "r84", "r85", "r86", "r87",
241 "r88", "r89", "r90", "r91", "r92", "r93", "r94", "r95",
242 "r96", "r97", "r98", "r99", "r100", "r101", "r102", "r103",
243 "r104", "r105", "r106", "r107", "r108", "r109", "r110", "r111",
244 "r112", "r113", "r114", "r115", "r116", "r117", "r118", "r119",
245 "r120", "r121", "r122", "r123", "r124", "r125", "r126", "r127",
246
247 "p0", "p1", "p2", "p3", "p4", "p5", "p6", "p7",
248 "p8", "p9", "p10", "p11", "p12", "p13", "p14", "p15",
249 "p16", "p17", "p18", "p19", "p20", "p21", "p22", "p23",
250 "p24", "p25", "p26", "p27", "p28", "p29", "p30", "p31",
251 "p32", "p33", "p34", "p35", "p36", "p37", "p38", "p39",
252 "p40", "p41", "p42", "p43", "p44", "p45", "p46", "p47",
253 "p48", "p49", "p50", "p51", "p52", "p53", "p54", "p55",
254 "p56", "p57", "p58", "p59", "p60", "p61", "p62", "p63",
16461d7d
KB
255};
256
004d836a
JJ
257struct ia64_frame_cache
258{
259 CORE_ADDR base; /* frame pointer base for frame */
260 CORE_ADDR pc; /* function start pc for frame */
261 CORE_ADDR saved_sp; /* stack pointer for frame */
262 CORE_ADDR bsp; /* points at r32 for the current frame */
263 CORE_ADDR cfm; /* cfm value for current frame */
4afcc598 264 CORE_ADDR prev_cfm; /* cfm value for previous frame */
004d836a 265 int frameless;
1777feb0
MS
266 int sof; /* Size of frame (decoded from cfm value). */
267 int sol; /* Size of locals (decoded from cfm value). */
268 int sor; /* Number of rotating registers (decoded from
269 cfm value). */
004d836a
JJ
270 CORE_ADDR after_prologue;
271 /* Address of first instruction after the last
272 prologue instruction; Note that there may
273 be instructions from the function's body
1777feb0 274 intermingled with the prologue. */
004d836a
JJ
275 int mem_stack_frame_size;
276 /* Size of the memory stack frame (may be zero),
1777feb0 277 or -1 if it has not been determined yet. */
004d836a 278 int fp_reg; /* Register number (if any) used a frame pointer
244bc108 279 for this frame. 0 if no register is being used
1777feb0 280 as the frame pointer. */
004d836a
JJ
281
282 /* Saved registers. */
283 CORE_ADDR saved_regs[NUM_IA64_RAW_REGS];
284
285};
244bc108 286
27067745
UW
287static int
288floatformat_valid (const struct floatformat *fmt, const void *from)
289{
290 return 1;
291}
292
293static const struct floatformat floatformat_ia64_ext =
294{
295 floatformat_little, 82, 0, 1, 17, 65535, 0x1ffff, 18, 64,
296 floatformat_intbit_yes, "floatformat_ia64_ext", floatformat_valid, NULL
297};
298
299static const struct floatformat *floatformats_ia64_ext[2] =
300{
301 &floatformat_ia64_ext,
302 &floatformat_ia64_ext
303};
304
305static struct type *
306ia64_ext_type (struct gdbarch *gdbarch)
307{
308 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
309
310 if (!tdep->ia64_ext_type)
311 tdep->ia64_ext_type
e9bb382b 312 = arch_float_type (gdbarch, 128, "builtin_type_ia64_ext",
27067745
UW
313 floatformats_ia64_ext);
314
315 return tdep->ia64_ext_type;
316}
317
63807e1d 318static int
004d836a
JJ
319ia64_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
320 struct reggroup *group)
16461d7d 321{
004d836a
JJ
322 int vector_p;
323 int float_p;
324 int raw_p;
325 if (group == all_reggroup)
326 return 1;
327 vector_p = TYPE_VECTOR (register_type (gdbarch, regnum));
328 float_p = TYPE_CODE (register_type (gdbarch, regnum)) == TYPE_CODE_FLT;
329 raw_p = regnum < NUM_IA64_RAW_REGS;
330 if (group == float_reggroup)
331 return float_p;
332 if (group == vector_reggroup)
333 return vector_p;
334 if (group == general_reggroup)
335 return (!vector_p && !float_p);
336 if (group == save_reggroup || group == restore_reggroup)
337 return raw_p;
338 return 0;
16461d7d
KB
339}
340
004d836a 341static const char *
d93859e2 342ia64_register_name (struct gdbarch *gdbarch, int reg)
16461d7d 343{
004d836a 344 return ia64_register_names[reg];
16461d7d
KB
345}
346
004d836a
JJ
347struct type *
348ia64_register_type (struct gdbarch *arch, int reg)
16461d7d 349{
004d836a 350 if (reg >= IA64_FR0_REGNUM && reg <= IA64_FR127_REGNUM)
27067745 351 return ia64_ext_type (arch);
004d836a 352 else
0dfff4cb 353 return builtin_type (arch)->builtin_long;
16461d7d
KB
354}
355
a78f21af 356static int
d3f73121 357ia64_dwarf_reg_to_regnum (struct gdbarch *gdbarch, int reg)
16461d7d 358{
004d836a
JJ
359 if (reg >= IA64_GR32_REGNUM && reg <= IA64_GR127_REGNUM)
360 return V32_REGNUM + (reg - IA64_GR32_REGNUM);
361 return reg;
16461d7d
KB
362}
363
16461d7d
KB
364
365/* Extract ``len'' bits from an instruction bundle starting at
366 bit ``from''. */
367
244bc108 368static long long
15c1e57f 369extract_bit_field (const char *bundle, int from, int len)
16461d7d
KB
370{
371 long long result = 0LL;
372 int to = from + len;
373 int from_byte = from / 8;
374 int to_byte = to / 8;
375 unsigned char *b = (unsigned char *) bundle;
376 unsigned char c;
377 int lshift;
378 int i;
379
380 c = b[from_byte];
381 if (from_byte == to_byte)
382 c = ((unsigned char) (c << (8 - to % 8))) >> (8 - to % 8);
383 result = c >> (from % 8);
384 lshift = 8 - (from % 8);
385
386 for (i = from_byte+1; i < to_byte; i++)
387 {
388 result |= ((long long) b[i]) << lshift;
389 lshift += 8;
390 }
391
392 if (from_byte < to_byte && (to % 8 != 0))
393 {
394 c = b[to_byte];
395 c = ((unsigned char) (c << (8 - to % 8))) >> (8 - to % 8);
396 result |= ((long long) c) << lshift;
397 }
398
399 return result;
400}
401
1777feb0 402/* Replace the specified bits in an instruction bundle. */
16461d7d 403
244bc108 404static void
16461d7d
KB
405replace_bit_field (char *bundle, long long val, int from, int len)
406{
407 int to = from + len;
408 int from_byte = from / 8;
409 int to_byte = to / 8;
410 unsigned char *b = (unsigned char *) bundle;
411 unsigned char c;
412
413 if (from_byte == to_byte)
414 {
415 unsigned char left, right;
416 c = b[from_byte];
417 left = (c >> (to % 8)) << (to % 8);
418 right = ((unsigned char) (c << (8 - from % 8))) >> (8 - from % 8);
419 c = (unsigned char) (val & 0xff);
420 c = (unsigned char) (c << (from % 8 + 8 - to % 8)) >> (8 - to % 8);
421 c |= right | left;
422 b[from_byte] = c;
423 }
424 else
425 {
426 int i;
427 c = b[from_byte];
428 c = ((unsigned char) (c << (8 - from % 8))) >> (8 - from % 8);
429 c = c | (val << (from % 8));
430 b[from_byte] = c;
431 val >>= 8 - from % 8;
432
433 for (i = from_byte+1; i < to_byte; i++)
434 {
435 c = val & 0xff;
436 val >>= 8;
437 b[i] = c;
438 }
439
440 if (to % 8 != 0)
441 {
442 unsigned char cv = (unsigned char) val;
443 c = b[to_byte];
444 c = c >> (to % 8) << (to % 8);
445 c |= ((unsigned char) (cv << (8 - to % 8))) >> (8 - to % 8);
446 b[to_byte] = c;
447 }
448 }
449}
450
451/* Return the contents of slot N (for N = 0, 1, or 2) in
1777feb0 452 and instruction bundle. */
16461d7d 453
244bc108 454static long long
2fc3ac7e 455slotN_contents (char *bundle, int slotnum)
16461d7d
KB
456{
457 return extract_bit_field (bundle, 5+41*slotnum, 41);
458}
459
1777feb0 460/* Store an instruction in an instruction bundle. */
16461d7d 461
244bc108 462static void
2fc3ac7e 463replace_slotN_contents (char *bundle, long long instr, int slotnum)
16461d7d
KB
464{
465 replace_bit_field (bundle, instr, 5+41*slotnum, 41);
466}
467
939c61fa 468static const enum instruction_type template_encoding_table[32][3] =
16461d7d
KB
469{
470 { M, I, I }, /* 00 */
471 { M, I, I }, /* 01 */
472 { M, I, I }, /* 02 */
473 { M, I, I }, /* 03 */
474 { M, L, X }, /* 04 */
475 { M, L, X }, /* 05 */
476 { undefined, undefined, undefined }, /* 06 */
477 { undefined, undefined, undefined }, /* 07 */
478 { M, M, I }, /* 08 */
479 { M, M, I }, /* 09 */
480 { M, M, I }, /* 0A */
481 { M, M, I }, /* 0B */
482 { M, F, I }, /* 0C */
483 { M, F, I }, /* 0D */
484 { M, M, F }, /* 0E */
485 { M, M, F }, /* 0F */
486 { M, I, B }, /* 10 */
487 { M, I, B }, /* 11 */
488 { M, B, B }, /* 12 */
489 { M, B, B }, /* 13 */
490 { undefined, undefined, undefined }, /* 14 */
491 { undefined, undefined, undefined }, /* 15 */
492 { B, B, B }, /* 16 */
493 { B, B, B }, /* 17 */
494 { M, M, B }, /* 18 */
495 { M, M, B }, /* 19 */
496 { undefined, undefined, undefined }, /* 1A */
497 { undefined, undefined, undefined }, /* 1B */
498 { M, F, B }, /* 1C */
499 { M, F, B }, /* 1D */
500 { undefined, undefined, undefined }, /* 1E */
501 { undefined, undefined, undefined }, /* 1F */
502};
503
504/* Fetch and (partially) decode an instruction at ADDR and return the
505 address of the next instruction to fetch. */
506
507static CORE_ADDR
508fetch_instruction (CORE_ADDR addr, instruction_type *it, long long *instr)
509{
510 char bundle[BUNDLE_LEN];
511 int slotnum = (int) (addr & 0x0f) / SLOT_MULTIPLIER;
512 long long template;
513 int val;
514
c26e1c2b
KB
515 /* Warn about slot numbers greater than 2. We used to generate
516 an error here on the assumption that the user entered an invalid
517 address. But, sometimes GDB itself requests an invalid address.
518 This can (easily) happen when execution stops in a function for
519 which there are no symbols. The prologue scanner will attempt to
520 find the beginning of the function - if the nearest symbol
521 happens to not be aligned on a bundle boundary (16 bytes), the
522 resulting starting address will cause GDB to think that the slot
523 number is too large.
524
525 So we warn about it and set the slot number to zero. It is
526 not necessarily a fatal condition, particularly if debugging
527 at the assembly language level. */
16461d7d 528 if (slotnum > 2)
c26e1c2b 529 {
8a3fe4f8
AC
530 warning (_("Can't fetch instructions for slot numbers greater than 2.\n"
531 "Using slot 0 instead"));
c26e1c2b
KB
532 slotnum = 0;
533 }
16461d7d
KB
534
535 addr &= ~0x0f;
536
537 val = target_read_memory (addr, bundle, BUNDLE_LEN);
538
539 if (val != 0)
540 return 0;
541
542 *instr = slotN_contents (bundle, slotnum);
543 template = extract_bit_field (bundle, 0, 5);
544 *it = template_encoding_table[(int)template][slotnum];
545
64a5b29c 546 if (slotnum == 2 || (slotnum == 1 && *it == L))
16461d7d
KB
547 addr += 16;
548 else
549 addr += (slotnum + 1) * SLOT_MULTIPLIER;
550
551 return addr;
552}
553
554/* There are 5 different break instructions (break.i, break.b,
555 break.m, break.f, and break.x), but they all have the same
556 encoding. (The five bit template in the low five bits of the
557 instruction bundle distinguishes one from another.)
558
559 The runtime architecture manual specifies that break instructions
560 used for debugging purposes must have the upper two bits of the 21
561 bit immediate set to a 0 and a 1 respectively. A breakpoint
562 instruction encodes the most significant bit of its 21 bit
563 immediate at bit 36 of the 41 bit instruction. The penultimate msb
564 is at bit 25 which leads to the pattern below.
565
566 Originally, I had this set up to do, e.g, a "break.i 0x80000" But
567 it turns out that 0x80000 was used as the syscall break in the early
568 simulators. So I changed the pattern slightly to do "break.i 0x080001"
569 instead. But that didn't work either (I later found out that this
570 pattern was used by the simulator that I was using.) So I ended up
939c61fa
JK
571 using the pattern seen below.
572
573 SHADOW_CONTENTS has byte-based addressing (PLACED_ADDRESS and SHADOW_LEN)
574 while we need bit-based addressing as the instructions length is 41 bits and
575 we must not modify/corrupt the adjacent slots in the same bundle.
576 Fortunately we may store larger memory incl. the adjacent bits with the
577 original memory content (not the possibly already stored breakpoints there).
578 We need to be careful in ia64_memory_remove_breakpoint to always restore
579 only the specific bits of this instruction ignoring any adjacent stored
580 bits.
581
582 We use the original addressing with the low nibble in the range <0..2> which
583 gets incorrectly interpreted by generic non-ia64 breakpoint_restore_shadows
584 as the direct byte offset of SHADOW_CONTENTS. We store whole BUNDLE_LEN
585 bytes just without these two possibly skipped bytes to not to exceed to the
586 next bundle.
587
588 If we would like to store the whole bundle to SHADOW_CONTENTS we would have
589 to store already the base address (`address & ~0x0f') into PLACED_ADDRESS.
590 In such case there is no other place where to store
591 SLOTNUM (`adress & 0x0f', value in the range <0..2>). We need to know
592 SLOTNUM in ia64_memory_remove_breakpoint.
593
ca8b5032
JB
594 There is one special case where we need to be extra careful:
595 L-X instructions, which are instructions that occupy 2 slots
596 (The L part is always in slot 1, and the X part is always in
597 slot 2). We must refuse to insert breakpoints for an address
598 that points at slot 2 of a bundle where an L-X instruction is
599 present, since there is logically no instruction at that address.
600 However, to make things more interesting, the opcode of L-X
601 instructions is located in slot 2. This means that, to insert
602 a breakpoint at an address that points to slot 1, we actually
603 need to write the breakpoint in slot 2! Slot 1 is actually
604 the extended operand, so writing the breakpoint there would not
605 have the desired effect. Another side-effect of this issue
606 is that we need to make sure that the shadow contents buffer
607 does save byte 15 of our instruction bundle (this is the tail
608 end of slot 2, which wouldn't be saved if we were to insert
609 the breakpoint in slot 1).
610
939c61fa
JK
611 ia64 16-byte bundle layout:
612 | 5 bits | slot 0 with 41 bits | slot 1 with 41 bits | slot 2 with 41 bits |
613
614 The current addressing used by the code below:
615 original PC placed_address placed_size required covered
616 == bp_tgt->shadow_len reqd \subset covered
73a9714c
JB
617 0xABCDE0 0xABCDE0 0x10 <0x0...0x5> <0x0..0xF>
618 0xABCDE1 0xABCDE1 0xF <0x5...0xA> <0x1..0xF>
939c61fa 619 0xABCDE2 0xABCDE2 0xE <0xA...0xF> <0x2..0xF>
ca8b5032
JB
620
621 L-X instructions are treated a little specially, as explained above:
622 0xABCDE1 0xABCDE1 0xF <0xA...0xF> <0x1..0xF>
623
939c61fa
JK
624 `objdump -d' and some other tools show a bit unjustified offsets:
625 original PC byte where starts the instruction objdump offset
626 0xABCDE0 0xABCDE0 0xABCDE0
627 0xABCDE1 0xABCDE5 0xABCDE6
628 0xABCDE2 0xABCDEA 0xABCDEC
629 */
16461d7d 630
aaab4dba 631#define IA64_BREAKPOINT 0x00003333300LL
16461d7d
KB
632
633static int
ae4b2284
MD
634ia64_memory_insert_breakpoint (struct gdbarch *gdbarch,
635 struct bp_target_info *bp_tgt)
16461d7d 636{
8181d85f 637 CORE_ADDR addr = bp_tgt->placed_address;
939c61fa 638 gdb_byte bundle[BUNDLE_LEN];
73a9714c 639 int slotnum = (int) (addr & 0x0f) / SLOT_MULTIPLIER, shadow_slotnum;
939c61fa 640 long long instr_breakpoint;
16461d7d 641 int val;
126fa72d 642 int template;
939c61fa 643 struct cleanup *cleanup;
16461d7d
KB
644
645 if (slotnum > 2)
8a3fe4f8 646 error (_("Can't insert breakpoint for slot numbers greater than 2."));
16461d7d
KB
647
648 addr &= ~0x0f;
649
b554e4bd
JK
650 /* Enable the automatic memory restoration from breakpoints while
651 we read our instruction bundle for the purpose of SHADOW_CONTENTS.
652 Otherwise, we could possibly store into the shadow parts of the adjacent
939c61fa
JK
653 placed breakpoints. It is due to our SHADOW_CONTENTS overlapping the real
654 breakpoint instruction bits region. */
b554e4bd 655 cleanup = make_show_memory_breakpoints_cleanup (0);
16461d7d 656 val = target_read_memory (addr, bundle, BUNDLE_LEN);
fbfaaae5
JK
657 if (val != 0)
658 {
659 do_cleanups (cleanup);
660 return val;
661 }
126fa72d 662
73a9714c
JB
663 /* SHADOW_SLOTNUM saves the original slot number as expected by the caller
664 for addressing the SHADOW_CONTENTS placement. */
665 shadow_slotnum = slotnum;
666
ca8b5032
JB
667 /* Always cover the last byte of the bundle in case we are inserting
668 a breakpoint on an L-X instruction. */
73a9714c
JB
669 bp_tgt->shadow_len = BUNDLE_LEN - shadow_slotnum;
670
35ec2a3e 671 template = extract_bit_field (bundle, 0, 5);
73a9714c
JB
672 if (template_encoding_table[template][slotnum] == X)
673 {
ca8b5032
JB
674 /* X unit types can only be used in slot 2, and are actually
675 part of a 2-slot L-X instruction. We cannot break at this
676 address, as this is the second half of an instruction that
677 lives in slot 1 of that bundle. */
73a9714c
JB
678 gdb_assert (slotnum == 2);
679 error (_("Can't insert breakpoint for non-existing slot X"));
680 }
681 if (template_encoding_table[template][slotnum] == L)
682 {
ca8b5032
JB
683 /* L unit types can only be used in slot 1. But the associated
684 opcode for that instruction is in slot 2, so bump the slot number
685 accordingly. */
73a9714c
JB
686 gdb_assert (slotnum == 1);
687 slotnum = 2;
688 }
939c61fa
JK
689
690 /* Store the whole bundle, except for the initial skipped bytes by the slot
691 number interpreted as bytes offset in PLACED_ADDRESS. */
1777feb0
MS
692 memcpy (bp_tgt->shadow_contents, bundle + shadow_slotnum,
693 bp_tgt->shadow_len);
939c61fa 694
b554e4bd
JK
695 /* Re-read the same bundle as above except that, this time, read it in order
696 to compute the new bundle inside which we will be inserting the
697 breakpoint. Therefore, disable the automatic memory restoration from
698 breakpoints while we read our instruction bundle. Otherwise, the general
699 restoration mechanism kicks in and we would possibly remove parts of the
700 adjacent placed breakpoints. It is due to our SHADOW_CONTENTS overlapping
701 the real breakpoint instruction bits region. */
702 make_show_memory_breakpoints_cleanup (1);
fbfaaae5
JK
703 val = target_read_memory (addr, bundle, BUNDLE_LEN);
704 if (val != 0)
705 {
706 do_cleanups (cleanup);
707 return val;
708 }
b554e4bd 709
939c61fa
JK
710 /* Breakpoints already present in the code will get deteacted and not get
711 reinserted by bp_loc_is_permanent. Multiple breakpoints at the same
712 location cannot induce the internal error as they are optimized into
713 a single instance by update_global_location_list. */
714 instr_breakpoint = slotN_contents (bundle, slotnum);
715 if (instr_breakpoint == IA64_BREAKPOINT)
716 internal_error (__FILE__, __LINE__,
717 _("Address %s already contains a breakpoint."),
5af949e3 718 paddress (gdbarch, bp_tgt->placed_address));
aaab4dba 719 replace_slotN_contents (bundle, IA64_BREAKPOINT, slotnum);
939c61fa 720
b554e4bd
JK
721 bp_tgt->placed_size = bp_tgt->shadow_len;
722
73a9714c 723 val = target_write_memory (addr + shadow_slotnum, bundle + shadow_slotnum,
fbfaaae5 724 bp_tgt->shadow_len);
16461d7d 725
939c61fa 726 do_cleanups (cleanup);
16461d7d
KB
727 return val;
728}
729
730static int
ae4b2284
MD
731ia64_memory_remove_breakpoint (struct gdbarch *gdbarch,
732 struct bp_target_info *bp_tgt)
16461d7d 733{
8181d85f 734 CORE_ADDR addr = bp_tgt->placed_address;
939c61fa 735 gdb_byte bundle_mem[BUNDLE_LEN], bundle_saved[BUNDLE_LEN];
73a9714c 736 int slotnum = (addr & 0x0f) / SLOT_MULTIPLIER, shadow_slotnum;
939c61fa 737 long long instr_breakpoint, instr_saved;
16461d7d 738 int val;
126fa72d 739 int template;
1de34ab7 740 struct cleanup *cleanup;
16461d7d
KB
741
742 addr &= ~0x0f;
743
1de34ab7
JB
744 /* Disable the automatic memory restoration from breakpoints while
745 we read our instruction bundle. Otherwise, the general restoration
939c61fa
JK
746 mechanism kicks in and we would possibly remove parts of the adjacent
747 placed breakpoints. It is due to our SHADOW_CONTENTS overlapping the real
748 breakpoint instruction bits region. */
1de34ab7 749 cleanup = make_show_memory_breakpoints_cleanup (1);
939c61fa 750 val = target_read_memory (addr, bundle_mem, BUNDLE_LEN);
fbfaaae5
JK
751 if (val != 0)
752 {
753 do_cleanups (cleanup);
754 return val;
755 }
126fa72d 756
73a9714c
JB
757 /* SHADOW_SLOTNUM saves the original slot number as expected by the caller
758 for addressing the SHADOW_CONTENTS placement. */
759 shadow_slotnum = slotnum;
760
939c61fa 761 template = extract_bit_field (bundle_mem, 0, 5);
73a9714c
JB
762 if (template_encoding_table[template][slotnum] == X)
763 {
ca8b5032
JB
764 /* X unit types can only be used in slot 2, and are actually
765 part of a 2-slot L-X instruction. We refuse to insert
766 breakpoints at this address, so there should be no reason
767 for us attempting to remove one there, except if the program's
768 code somehow got modified in memory. */
73a9714c 769 gdb_assert (slotnum == 2);
ca8b5032
JB
770 warning (_("Cannot remove breakpoint at address %s from non-existing "
771 "X-type slot, memory has changed underneath"),
73a9714c
JB
772 paddress (gdbarch, bp_tgt->placed_address));
773 do_cleanups (cleanup);
774 return -1;
775 }
776 if (template_encoding_table[template][slotnum] == L)
777 {
ca8b5032
JB
778 /* L unit types can only be used in slot 1. But the breakpoint
779 was actually saved using slot 2, so update the slot number
780 accordingly. */
73a9714c
JB
781 gdb_assert (slotnum == 1);
782 slotnum = 2;
783 }
939c61fa 784
73a9714c 785 gdb_assert (bp_tgt->placed_size == BUNDLE_LEN - shadow_slotnum);
939c61fa
JK
786 gdb_assert (bp_tgt->placed_size == bp_tgt->shadow_len);
787
788 instr_breakpoint = slotN_contents (bundle_mem, slotnum);
789 if (instr_breakpoint != IA64_BREAKPOINT)
126fa72d 790 {
939c61fa
JK
791 warning (_("Cannot remove breakpoint at address %s, "
792 "no break instruction at such address."),
5af949e3 793 paddress (gdbarch, bp_tgt->placed_address));
a58162c2 794 do_cleanups (cleanup);
939c61fa 795 return -1;
126fa72d
PS
796 }
797
939c61fa
JK
798 /* Extract the original saved instruction from SLOTNUM normalizing its
799 bit-shift for INSTR_SAVED. */
800 memcpy (bundle_saved, bundle_mem, BUNDLE_LEN);
73a9714c
JB
801 memcpy (bundle_saved + shadow_slotnum, bp_tgt->shadow_contents,
802 bp_tgt->shadow_len);
939c61fa
JK
803 instr_saved = slotN_contents (bundle_saved, slotnum);
804
ca8b5032
JB
805 /* In BUNDLE_MEM, be careful to modify only the bits belonging to SLOTNUM
806 and not any of the other ones that are stored in SHADOW_CONTENTS. */
939c61fa 807 replace_slotN_contents (bundle_mem, instr_saved, slotnum);
fbfaaae5 808 val = target_write_memory (addr, bundle_mem, BUNDLE_LEN);
16461d7d 809
1de34ab7 810 do_cleanups (cleanup);
16461d7d
KB
811 return val;
812}
813
939c61fa
JK
814/* As gdbarch_breakpoint_from_pc ranges have byte granularity and ia64
815 instruction slots ranges are bit-granular (41 bits) we have to provide an
816 extended range as described for ia64_memory_insert_breakpoint. We also take
817 care of preserving the `break' instruction 21-bit (or 62-bit) parameter to
818 make a match for permanent breakpoints. */
819
820static const gdb_byte *
1777feb0
MS
821ia64_breakpoint_from_pc (struct gdbarch *gdbarch,
822 CORE_ADDR *pcptr, int *lenptr)
16461d7d 823{
939c61fa
JK
824 CORE_ADDR addr = *pcptr;
825 static gdb_byte bundle[BUNDLE_LEN];
73a9714c 826 int slotnum = (int) (*pcptr & 0x0f) / SLOT_MULTIPLIER, shadow_slotnum;
939c61fa
JK
827 long long instr_fetched;
828 int val;
829 int template;
830 struct cleanup *cleanup;
831
832 if (slotnum > 2)
833 error (_("Can't insert breakpoint for slot numbers greater than 2."));
834
835 addr &= ~0x0f;
836
837 /* Enable the automatic memory restoration from breakpoints while
838 we read our instruction bundle to match bp_loc_is_permanent. */
839 cleanup = make_show_memory_breakpoints_cleanup (0);
840 val = target_read_memory (addr, bundle, BUNDLE_LEN);
841 do_cleanups (cleanup);
842
843 /* The memory might be unreachable. This can happen, for instance,
844 when the user inserts a breakpoint at an invalid address. */
845 if (val != 0)
846 return NULL;
847
73a9714c
JB
848 /* SHADOW_SLOTNUM saves the original slot number as expected by the caller
849 for addressing the SHADOW_CONTENTS placement. */
850 shadow_slotnum = slotnum;
851
852 /* Cover always the last byte of the bundle for the L-X slot case. */
853 *lenptr = BUNDLE_LEN - shadow_slotnum;
854
939c61fa
JK
855 /* Check for L type instruction in slot 1, if present then bump up the slot
856 number to the slot 2. */
857 template = extract_bit_field (bundle, 0, 5);
73a9714c
JB
858 if (template_encoding_table[template][slotnum] == X)
859 {
860 gdb_assert (slotnum == 2);
861 error (_("Can't insert breakpoint for non-existing slot X"));
862 }
863 if (template_encoding_table[template][slotnum] == L)
864 {
865 gdb_assert (slotnum == 1);
866 slotnum = 2;
867 }
939c61fa
JK
868
869 /* A break instruction has its all its opcode bits cleared except for
870 the parameter value. For L+X slot pair we are at the X slot (slot 2) so
871 we should not touch the L slot - the upper 41 bits of the parameter. */
872 instr_fetched = slotN_contents (bundle, slotnum);
116e0965 873 instr_fetched &= 0x1003ffffc0LL;
939c61fa
JK
874 replace_slotN_contents (bundle, instr_fetched, slotnum);
875
73a9714c 876 return bundle + shadow_slotnum;
16461d7d
KB
877}
878
a78f21af 879static CORE_ADDR
61a1198a 880ia64_read_pc (struct regcache *regcache)
16461d7d 881{
61a1198a
UW
882 ULONGEST psr_value, pc_value;
883 int slot_num;
884
885 regcache_cooked_read_unsigned (regcache, IA64_PSR_REGNUM, &psr_value);
886 regcache_cooked_read_unsigned (regcache, IA64_IP_REGNUM, &pc_value);
887 slot_num = (psr_value >> 41) & 3;
16461d7d
KB
888
889 return pc_value | (slot_num * SLOT_MULTIPLIER);
890}
891
54a5c8d8 892void
61a1198a 893ia64_write_pc (struct regcache *regcache, CORE_ADDR new_pc)
16461d7d
KB
894{
895 int slot_num = (int) (new_pc & 0xf) / SLOT_MULTIPLIER;
61a1198a
UW
896 ULONGEST psr_value;
897
898 regcache_cooked_read_unsigned (regcache, IA64_PSR_REGNUM, &psr_value);
16461d7d 899 psr_value &= ~(3LL << 41);
61a1198a 900 psr_value |= (ULONGEST)(slot_num & 0x3) << 41;
16461d7d
KB
901
902 new_pc &= ~0xfLL;
903
61a1198a
UW
904 regcache_cooked_write_unsigned (regcache, IA64_PSR_REGNUM, psr_value);
905 regcache_cooked_write_unsigned (regcache, IA64_IP_REGNUM, new_pc);
16461d7d
KB
906}
907
908#define IS_NaT_COLLECTION_ADDR(addr) ((((addr) >> 3) & 0x3f) == 0x3f)
909
910/* Returns the address of the slot that's NSLOTS slots away from
1777feb0 911 the address ADDR. NSLOTS may be positive or negative. */
16461d7d
KB
912static CORE_ADDR
913rse_address_add(CORE_ADDR addr, int nslots)
914{
915 CORE_ADDR new_addr;
916 int mandatory_nat_slots = nslots / 63;
917 int direction = nslots < 0 ? -1 : 1;
918
919 new_addr = addr + 8 * (nslots + mandatory_nat_slots);
920
921 if ((new_addr >> 9) != ((addr + 8 * 64 * mandatory_nat_slots) >> 9))
922 new_addr += 8 * direction;
923
924 if (IS_NaT_COLLECTION_ADDR(new_addr))
925 new_addr += 8 * direction;
926
927 return new_addr;
928}
929
004d836a
JJ
930static void
931ia64_pseudo_register_read (struct gdbarch *gdbarch, struct regcache *regcache,
88d82102 932 int regnum, gdb_byte *buf)
16461d7d 933{
e17a4113
UW
934 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
935
004d836a 936 if (regnum >= V32_REGNUM && regnum <= V127_REGNUM)
244bc108 937 {
88d82102 938#ifdef HAVE_LIBUNWIND_IA64_H
1777feb0
MS
939 /* First try and use the libunwind special reg accessor,
940 otherwise fallback to standard logic. */
c5a27d9c 941 if (!libunwind_is_initialized ()
45ecac4b 942 || libunwind_get_reg_special (gdbarch, regcache, regnum, buf) != 0)
88d82102 943#endif
004d836a 944 {
1777feb0
MS
945 /* The fallback position is to assume that r32-r127 are
946 found sequentially in memory starting at $bof. This
947 isn't always true, but without libunwind, this is the
948 best we can do. */
c5a27d9c
JJ
949 ULONGEST cfm;
950 ULONGEST bsp;
951 CORE_ADDR reg;
952 regcache_cooked_read_unsigned (regcache, IA64_BSP_REGNUM, &bsp);
953 regcache_cooked_read_unsigned (regcache, IA64_CFM_REGNUM, &cfm);
954
955 /* The bsp points at the end of the register frame so we
1777feb0
MS
956 subtract the size of frame from it to get start of
957 register frame. */
c5a27d9c
JJ
958 bsp = rse_address_add (bsp, -(cfm & 0x7f));
959
960 if ((cfm & 0x7f) > regnum - V32_REGNUM)
961 {
962 ULONGEST reg_addr = rse_address_add (bsp, (regnum - V32_REGNUM));
e17a4113
UW
963 reg = read_memory_integer ((CORE_ADDR)reg_addr, 8, byte_order);
964 store_unsigned_integer (buf, register_size (gdbarch, regnum),
965 byte_order, reg);
c5a27d9c
JJ
966 }
967 else
e17a4113
UW
968 store_unsigned_integer (buf, register_size (gdbarch, regnum),
969 byte_order, 0);
004d836a 970 }
004d836a
JJ
971 }
972 else if (IA64_NAT0_REGNUM <= regnum && regnum <= IA64_NAT31_REGNUM)
973 {
974 ULONGEST unatN_val;
975 ULONGEST unat;
976 regcache_cooked_read_unsigned (regcache, IA64_UNAT_REGNUM, &unat);
977 unatN_val = (unat & (1LL << (regnum - IA64_NAT0_REGNUM))) != 0;
e17a4113
UW
978 store_unsigned_integer (buf, register_size (gdbarch, regnum),
979 byte_order, unatN_val);
004d836a
JJ
980 }
981 else if (IA64_NAT32_REGNUM <= regnum && regnum <= IA64_NAT127_REGNUM)
982 {
983 ULONGEST natN_val = 0;
984 ULONGEST bsp;
985 ULONGEST cfm;
986 CORE_ADDR gr_addr = 0;
987 regcache_cooked_read_unsigned (regcache, IA64_BSP_REGNUM, &bsp);
988 regcache_cooked_read_unsigned (regcache, IA64_CFM_REGNUM, &cfm);
989
990 /* The bsp points at the end of the register frame so we
991 subtract the size of frame from it to get start of register frame. */
992 bsp = rse_address_add (bsp, -(cfm & 0x7f));
993
994 if ((cfm & 0x7f) > regnum - V32_REGNUM)
995 gr_addr = rse_address_add (bsp, (regnum - V32_REGNUM));
996
997 if (gr_addr != 0)
998 {
999 /* Compute address of nat collection bits. */
1000 CORE_ADDR nat_addr = gr_addr | 0x1f8;
1001 CORE_ADDR nat_collection;
1002 int nat_bit;
1003 /* If our nat collection address is bigger than bsp, we have to get
1004 the nat collection from rnat. Otherwise, we fetch the nat
1005 collection from the computed address. */
1006 if (nat_addr >= bsp)
1777feb0
MS
1007 regcache_cooked_read_unsigned (regcache, IA64_RNAT_REGNUM,
1008 &nat_collection);
004d836a 1009 else
e17a4113 1010 nat_collection = read_memory_integer (nat_addr, 8, byte_order);
004d836a
JJ
1011 nat_bit = (gr_addr >> 3) & 0x3f;
1012 natN_val = (nat_collection >> nat_bit) & 1;
1013 }
1014
e17a4113
UW
1015 store_unsigned_integer (buf, register_size (gdbarch, regnum),
1016 byte_order, natN_val);
244bc108 1017 }
004d836a
JJ
1018 else if (regnum == VBOF_REGNUM)
1019 {
1020 /* A virtual register frame start is provided for user convenience.
1777feb0 1021 It can be calculated as the bsp - sof (sizeof frame). */
004d836a
JJ
1022 ULONGEST bsp, vbsp;
1023 ULONGEST cfm;
1024 CORE_ADDR reg;
1025 regcache_cooked_read_unsigned (regcache, IA64_BSP_REGNUM, &bsp);
1026 regcache_cooked_read_unsigned (regcache, IA64_CFM_REGNUM, &cfm);
1027
1028 /* The bsp points at the end of the register frame so we
1029 subtract the size of frame from it to get beginning of frame. */
1030 vbsp = rse_address_add (bsp, -(cfm & 0x7f));
e17a4113
UW
1031 store_unsigned_integer (buf, register_size (gdbarch, regnum),
1032 byte_order, vbsp);
004d836a
JJ
1033 }
1034 else if (VP0_REGNUM <= regnum && regnum <= VP63_REGNUM)
1035 {
1036 ULONGEST pr;
1037 ULONGEST cfm;
1038 ULONGEST prN_val;
1039 CORE_ADDR reg;
1040 regcache_cooked_read_unsigned (regcache, IA64_PR_REGNUM, &pr);
1041 regcache_cooked_read_unsigned (regcache, IA64_CFM_REGNUM, &cfm);
1042
1043 if (VP16_REGNUM <= regnum && regnum <= VP63_REGNUM)
1044 {
1045 /* Fetch predicate register rename base from current frame
1777feb0 1046 marker for this frame. */
004d836a
JJ
1047 int rrb_pr = (cfm >> 32) & 0x3f;
1048
1777feb0 1049 /* Adjust the register number to account for register rotation. */
004d836a
JJ
1050 regnum = VP16_REGNUM
1051 + ((regnum - VP16_REGNUM) + rrb_pr) % 48;
1052 }
1053 prN_val = (pr & (1LL << (regnum - VP0_REGNUM))) != 0;
e17a4113
UW
1054 store_unsigned_integer (buf, register_size (gdbarch, regnum),
1055 byte_order, prN_val);
004d836a
JJ
1056 }
1057 else
088568da 1058 memset (buf, 0, register_size (gdbarch, regnum));
16461d7d
KB
1059}
1060
004d836a
JJ
1061static void
1062ia64_pseudo_register_write (struct gdbarch *gdbarch, struct regcache *regcache,
88d82102 1063 int regnum, const gdb_byte *buf)
16461d7d 1064{
e17a4113
UW
1065 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1066
004d836a 1067 if (regnum >= V32_REGNUM && regnum <= V127_REGNUM)
244bc108 1068 {
004d836a
JJ
1069 ULONGEST bsp;
1070 ULONGEST cfm;
1071 CORE_ADDR reg;
1072 regcache_cooked_read_unsigned (regcache, IA64_BSP_REGNUM, &bsp);
1073 regcache_cooked_read_unsigned (regcache, IA64_CFM_REGNUM, &cfm);
1074
1075 bsp = rse_address_add (bsp, -(cfm & 0x7f));
1076
1077 if ((cfm & 0x7f) > regnum - V32_REGNUM)
1078 {
1079 ULONGEST reg_addr = rse_address_add (bsp, (regnum - V32_REGNUM));
1777feb0 1080 write_memory (reg_addr, (void *) buf, 8);
004d836a
JJ
1081 }
1082 }
1083 else if (IA64_NAT0_REGNUM <= regnum && regnum <= IA64_NAT31_REGNUM)
1084 {
1085 ULONGEST unatN_val, unat, unatN_mask;
1086 regcache_cooked_read_unsigned (regcache, IA64_UNAT_REGNUM, &unat);
1777feb0
MS
1087 unatN_val = extract_unsigned_integer (buf, register_size (gdbarch,
1088 regnum),
e17a4113 1089 byte_order);
004d836a
JJ
1090 unatN_mask = (1LL << (regnum - IA64_NAT0_REGNUM));
1091 if (unatN_val == 0)
1092 unat &= ~unatN_mask;
1093 else if (unatN_val == 1)
1094 unat |= unatN_mask;
1095 regcache_cooked_write_unsigned (regcache, IA64_UNAT_REGNUM, unat);
1096 }
1097 else if (IA64_NAT32_REGNUM <= regnum && regnum <= IA64_NAT127_REGNUM)
1098 {
1099 ULONGEST natN_val;
1100 ULONGEST bsp;
1101 ULONGEST cfm;
1102 CORE_ADDR gr_addr = 0;
1103 regcache_cooked_read_unsigned (regcache, IA64_BSP_REGNUM, &bsp);
1104 regcache_cooked_read_unsigned (regcache, IA64_CFM_REGNUM, &cfm);
1105
1106 /* The bsp points at the end of the register frame so we
1107 subtract the size of frame from it to get start of register frame. */
1108 bsp = rse_address_add (bsp, -(cfm & 0x7f));
1109
1110 if ((cfm & 0x7f) > regnum - V32_REGNUM)
1111 gr_addr = rse_address_add (bsp, (regnum - V32_REGNUM));
1112
1777feb0
MS
1113 natN_val = extract_unsigned_integer (buf, register_size (gdbarch,
1114 regnum),
e17a4113 1115 byte_order);
004d836a
JJ
1116
1117 if (gr_addr != 0 && (natN_val == 0 || natN_val == 1))
1118 {
1119 /* Compute address of nat collection bits. */
1120 CORE_ADDR nat_addr = gr_addr | 0x1f8;
1121 CORE_ADDR nat_collection;
1122 int natN_bit = (gr_addr >> 3) & 0x3f;
1123 ULONGEST natN_mask = (1LL << natN_bit);
1124 /* If our nat collection address is bigger than bsp, we have to get
1125 the nat collection from rnat. Otherwise, we fetch the nat
1126 collection from the computed address. */
1127 if (nat_addr >= bsp)
1128 {
1777feb0
MS
1129 regcache_cooked_read_unsigned (regcache, IA64_RNAT_REGNUM,
1130 &nat_collection);
004d836a
JJ
1131 if (natN_val)
1132 nat_collection |= natN_mask;
1133 else
1134 nat_collection &= ~natN_mask;
1777feb0
MS
1135 regcache_cooked_write_unsigned (regcache, IA64_RNAT_REGNUM,
1136 nat_collection);
004d836a
JJ
1137 }
1138 else
1139 {
1140 char nat_buf[8];
e17a4113 1141 nat_collection = read_memory_integer (nat_addr, 8, byte_order);
004d836a
JJ
1142 if (natN_val)
1143 nat_collection |= natN_mask;
1144 else
1145 nat_collection &= ~natN_mask;
e17a4113
UW
1146 store_unsigned_integer (nat_buf, register_size (gdbarch, regnum),
1147 byte_order, nat_collection);
004d836a
JJ
1148 write_memory (nat_addr, nat_buf, 8);
1149 }
1150 }
1151 }
1152 else if (VP0_REGNUM <= regnum && regnum <= VP63_REGNUM)
1153 {
1154 ULONGEST pr;
1155 ULONGEST cfm;
1156 ULONGEST prN_val;
1157 ULONGEST prN_mask;
1158
1159 regcache_cooked_read_unsigned (regcache, IA64_PR_REGNUM, &pr);
1160 regcache_cooked_read_unsigned (regcache, IA64_CFM_REGNUM, &cfm);
1161
1162 if (VP16_REGNUM <= regnum && regnum <= VP63_REGNUM)
1163 {
1164 /* Fetch predicate register rename base from current frame
1777feb0 1165 marker for this frame. */
004d836a
JJ
1166 int rrb_pr = (cfm >> 32) & 0x3f;
1167
1777feb0 1168 /* Adjust the register number to account for register rotation. */
004d836a
JJ
1169 regnum = VP16_REGNUM
1170 + ((regnum - VP16_REGNUM) + rrb_pr) % 48;
1171 }
e17a4113
UW
1172 prN_val = extract_unsigned_integer (buf, register_size (gdbarch, regnum),
1173 byte_order);
004d836a
JJ
1174 prN_mask = (1LL << (regnum - VP0_REGNUM));
1175 if (prN_val == 0)
1176 pr &= ~prN_mask;
1177 else if (prN_val == 1)
1178 pr |= prN_mask;
1179 regcache_cooked_write_unsigned (regcache, IA64_PR_REGNUM, pr);
244bc108 1180 }
16461d7d
KB
1181}
1182
004d836a
JJ
1183/* The ia64 needs to convert between various ieee floating-point formats
1184 and the special ia64 floating point register format. */
1185
1186static int
0abe36f5 1187ia64_convert_register_p (struct gdbarch *gdbarch, int regno, struct type *type)
004d836a 1188{
83acabca 1189 return (regno >= IA64_FR0_REGNUM && regno <= IA64_FR127_REGNUM
27067745 1190 && type != ia64_ext_type (gdbarch));
004d836a
JJ
1191}
1192
1193static void
1194ia64_register_to_value (struct frame_info *frame, int regnum,
88d82102 1195 struct type *valtype, gdb_byte *out)
004d836a 1196{
27067745 1197 struct gdbarch *gdbarch = get_frame_arch (frame);
004d836a
JJ
1198 char in[MAX_REGISTER_SIZE];
1199 frame_register_read (frame, regnum, in);
27067745 1200 convert_typed_floating (in, ia64_ext_type (gdbarch), out, valtype);
004d836a
JJ
1201}
1202
1203static void
1204ia64_value_to_register (struct frame_info *frame, int regnum,
88d82102 1205 struct type *valtype, const gdb_byte *in)
004d836a 1206{
27067745 1207 struct gdbarch *gdbarch = get_frame_arch (frame);
004d836a 1208 char out[MAX_REGISTER_SIZE];
27067745 1209 convert_typed_floating (in, valtype, out, ia64_ext_type (gdbarch));
004d836a
JJ
1210 put_frame_register (frame, regnum, out);
1211}
1212
1213
58ab00f9
KB
1214/* Limit the number of skipped non-prologue instructions since examining
1215 of the prologue is expensive. */
5ea2bd7f 1216static int max_skip_non_prologue_insns = 40;
58ab00f9
KB
1217
1218/* Given PC representing the starting address of a function, and
1219 LIM_PC which is the (sloppy) limit to which to scan when looking
1220 for a prologue, attempt to further refine this limit by using
1221 the line data in the symbol table. If successful, a better guess
1222 on where the prologue ends is returned, otherwise the previous
1223 value of lim_pc is returned. TRUST_LIMIT is a pointer to a flag
1224 which will be set to indicate whether the returned limit may be
1225 used with no further scanning in the event that the function is
1226 frameless. */
1227
634aa483
AC
1228/* FIXME: cagney/2004-02-14: This function and logic have largely been
1229 superseded by skip_prologue_using_sal. */
1230
58ab00f9
KB
1231static CORE_ADDR
1232refine_prologue_limit (CORE_ADDR pc, CORE_ADDR lim_pc, int *trust_limit)
1233{
1234 struct symtab_and_line prologue_sal;
1235 CORE_ADDR start_pc = pc;
39312971
JB
1236 CORE_ADDR end_pc;
1237
1238 /* The prologue can not possibly go past the function end itself,
1239 so we can already adjust LIM_PC accordingly. */
1240 if (find_pc_partial_function (pc, NULL, NULL, &end_pc) && end_pc < lim_pc)
1241 lim_pc = end_pc;
58ab00f9
KB
1242
1243 /* Start off not trusting the limit. */
1244 *trust_limit = 0;
1245
1246 prologue_sal = find_pc_line (pc, 0);
1247 if (prologue_sal.line != 0)
1248 {
1249 int i;
1250 CORE_ADDR addr = prologue_sal.end;
1251
1252 /* Handle the case in which compiler's optimizer/scheduler
1253 has moved instructions into the prologue. We scan ahead
1254 in the function looking for address ranges whose corresponding
1255 line number is less than or equal to the first one that we
1256 found for the function. (It can be less than when the
1257 scheduler puts a body instruction before the first prologue
1258 instruction.) */
1259 for (i = 2 * max_skip_non_prologue_insns;
1260 i > 0 && (lim_pc == 0 || addr < lim_pc);
1261 i--)
1262 {
1263 struct symtab_and_line sal;
1264
1265 sal = find_pc_line (addr, 0);
1266 if (sal.line == 0)
1267 break;
1268 if (sal.line <= prologue_sal.line
1269 && sal.symtab == prologue_sal.symtab)
1270 {
1271 prologue_sal = sal;
1272 }
1273 addr = sal.end;
1274 }
1275
1276 if (lim_pc == 0 || prologue_sal.end < lim_pc)
1277 {
1278 lim_pc = prologue_sal.end;
1279 if (start_pc == get_pc_function_start (lim_pc))
1280 *trust_limit = 1;
1281 }
1282 }
1283 return lim_pc;
1284}
1285
16461d7d
KB
1286#define isScratch(_regnum_) ((_regnum_) == 2 || (_regnum_) == 3 \
1287 || (8 <= (_regnum_) && (_regnum_) <= 11) \
1288 || (14 <= (_regnum_) && (_regnum_) <= 31))
1289#define imm9(_instr_) \
1290 ( ((((_instr_) & 0x01000000000LL) ? -1 : 0) << 8) \
1291 | (((_instr_) & 0x00008000000LL) >> 20) \
1292 | (((_instr_) & 0x00000001fc0LL) >> 6))
1293
004d836a
JJ
1294/* Allocate and initialize a frame cache. */
1295
1296static struct ia64_frame_cache *
1297ia64_alloc_frame_cache (void)
1298{
1299 struct ia64_frame_cache *cache;
1300 int i;
1301
1302 cache = FRAME_OBSTACK_ZALLOC (struct ia64_frame_cache);
1303
1304 /* Base address. */
1305 cache->base = 0;
1306 cache->pc = 0;
1307 cache->cfm = 0;
4afcc598 1308 cache->prev_cfm = 0;
004d836a
JJ
1309 cache->sof = 0;
1310 cache->sol = 0;
1311 cache->sor = 0;
1312 cache->bsp = 0;
1313 cache->fp_reg = 0;
1314 cache->frameless = 1;
1315
1316 for (i = 0; i < NUM_IA64_RAW_REGS; i++)
1317 cache->saved_regs[i] = 0;
1318
1319 return cache;
1320}
1321
16461d7d 1322static CORE_ADDR
15c1e57f
JB
1323examine_prologue (CORE_ADDR pc, CORE_ADDR lim_pc,
1324 struct frame_info *this_frame,
1325 struct ia64_frame_cache *cache)
16461d7d
KB
1326{
1327 CORE_ADDR next_pc;
1328 CORE_ADDR last_prologue_pc = pc;
16461d7d
KB
1329 instruction_type it;
1330 long long instr;
16461d7d
KB
1331 int cfm_reg = 0;
1332 int ret_reg = 0;
1333 int fp_reg = 0;
1334 int unat_save_reg = 0;
1335 int pr_save_reg = 0;
1336 int mem_stack_frame_size = 0;
1337 int spill_reg = 0;
1338 CORE_ADDR spill_addr = 0;
0927a22b
KB
1339 char instores[8];
1340 char infpstores[8];
5ea2bd7f 1341 char reg_contents[256];
58ab00f9 1342 int trust_limit;
004d836a
JJ
1343 int frameless = 1;
1344 int i;
1345 CORE_ADDR addr;
1346 char buf[8];
1347 CORE_ADDR bof, sor, sol, sof, cfm, rrb_gr;
0927a22b
KB
1348
1349 memset (instores, 0, sizeof instores);
1350 memset (infpstores, 0, sizeof infpstores);
5ea2bd7f 1351 memset (reg_contents, 0, sizeof reg_contents);
16461d7d 1352
004d836a
JJ
1353 if (cache->after_prologue != 0
1354 && cache->after_prologue <= lim_pc)
1355 return cache->after_prologue;
16461d7d 1356
58ab00f9 1357 lim_pc = refine_prologue_limit (pc, lim_pc, &trust_limit);
16461d7d 1358 next_pc = fetch_instruction (pc, &it, &instr);
5ea2bd7f
JJ
1359
1360 /* We want to check if we have a recognizable function start before we
1361 look ahead for a prologue. */
16461d7d
KB
1362 if (pc < lim_pc && next_pc
1363 && it == M && ((instr & 0x1ee0000003fLL) == 0x02c00000000LL))
1364 {
5ea2bd7f 1365 /* alloc - start of a regular function. */
16461d7d
KB
1366 int sor = (int) ((instr & 0x00078000000LL) >> 27);
1367 int sol = (int) ((instr & 0x00007f00000LL) >> 20);
1368 int sof = (int) ((instr & 0x000000fe000LL) >> 13);
16461d7d 1369 int rN = (int) ((instr & 0x00000001fc0LL) >> 6);
004d836a
JJ
1370
1371 /* Verify that the current cfm matches what we think is the
1372 function start. If we have somehow jumped within a function,
1373 we do not want to interpret the prologue and calculate the
1777feb0
MS
1374 addresses of various registers such as the return address.
1375 We will instead treat the frame as frameless. */
15c1e57f 1376 if (!this_frame ||
004d836a
JJ
1377 (sof == (cache->cfm & 0x7f) &&
1378 sol == ((cache->cfm >> 7) & 0x7f)))
1379 frameless = 0;
1380
16461d7d
KB
1381 cfm_reg = rN;
1382 last_prologue_pc = next_pc;
1383 pc = next_pc;
1384 }
1385 else
58ab00f9 1386 {
5ea2bd7f
JJ
1387 /* Look for a leaf routine. */
1388 if (pc < lim_pc && next_pc
1389 && (it == I || it == M)
1390 && ((instr & 0x1ee00000000LL) == 0x10800000000LL))
1391 {
1392 /* adds rN = imm14, rM (or mov rN, rM when imm14 is 0) */
1393 int imm = (int) ((((instr & 0x01000000000LL) ? -1 : 0) << 13)
1394 | ((instr & 0x001f8000000LL) >> 20)
1395 | ((instr & 0x000000fe000LL) >> 13));
1396 int rM = (int) ((instr & 0x00007f00000LL) >> 20);
1397 int rN = (int) ((instr & 0x00000001fc0LL) >> 6);
1398 int qp = (int) (instr & 0x0000000003fLL);
1399 if (qp == 0 && rN == 2 && imm == 0 && rM == 12 && fp_reg == 0)
1400 {
1777feb0 1401 /* mov r2, r12 - beginning of leaf routine. */
5ea2bd7f 1402 fp_reg = rN;
5ea2bd7f
JJ
1403 last_prologue_pc = next_pc;
1404 }
1405 }
1406
1407 /* If we don't recognize a regular function or leaf routine, we are
1408 done. */
1409 if (!fp_reg)
1410 {
1411 pc = lim_pc;
1412 if (trust_limit)
1413 last_prologue_pc = lim_pc;
1414 }
58ab00f9 1415 }
16461d7d
KB
1416
1417 /* Loop, looking for prologue instructions, keeping track of
1777feb0 1418 where preserved registers were spilled. */
16461d7d
KB
1419 while (pc < lim_pc)
1420 {
1421 next_pc = fetch_instruction (pc, &it, &instr);
1422 if (next_pc == 0)
1423 break;
1424
594706e6 1425 if (it == B && ((instr & 0x1e1f800003fLL) != 0x04000000000LL))
0927a22b 1426 {
1777feb0 1427 /* Exit loop upon hitting a non-nop branch instruction. */
102d615a
JJ
1428 if (trust_limit)
1429 lim_pc = pc;
1430 break;
1431 }
1432 else if (((instr & 0x3fLL) != 0LL) &&
1433 (frameless || ret_reg != 0))
1434 {
1435 /* Exit loop upon hitting a predicated instruction if
1436 we already have the return register or if we are frameless. */
5ea2bd7f
JJ
1437 if (trust_limit)
1438 lim_pc = pc;
0927a22b
KB
1439 break;
1440 }
1441 else if (it == I && ((instr & 0x1eff8000000LL) == 0x00188000000LL))
16461d7d
KB
1442 {
1443 /* Move from BR */
1444 int b2 = (int) ((instr & 0x0000000e000LL) >> 13);
1445 int rN = (int) ((instr & 0x00000001fc0LL) >> 6);
1446 int qp = (int) (instr & 0x0000000003f);
1447
1448 if (qp == 0 && b2 == 0 && rN >= 32 && ret_reg == 0)
1449 {
1450 ret_reg = rN;
1451 last_prologue_pc = next_pc;
1452 }
1453 }
1454 else if ((it == I || it == M)
1455 && ((instr & 0x1ee00000000LL) == 0x10800000000LL))
1456 {
1457 /* adds rN = imm14, rM (or mov rN, rM when imm14 is 0) */
1458 int imm = (int) ((((instr & 0x01000000000LL) ? -1 : 0) << 13)
1459 | ((instr & 0x001f8000000LL) >> 20)
1460 | ((instr & 0x000000fe000LL) >> 13));
1461 int rM = (int) ((instr & 0x00007f00000LL) >> 20);
1462 int rN = (int) ((instr & 0x00000001fc0LL) >> 6);
1463 int qp = (int) (instr & 0x0000000003fLL);
1464
1465 if (qp == 0 && rN >= 32 && imm == 0 && rM == 12 && fp_reg == 0)
1466 {
1467 /* mov rN, r12 */
1468 fp_reg = rN;
1469 last_prologue_pc = next_pc;
1470 }
1471 else if (qp == 0 && rN == 12 && rM == 12)
1472 {
1473 /* adds r12, -mem_stack_frame_size, r12 */
1474 mem_stack_frame_size -= imm;
1475 last_prologue_pc = next_pc;
1476 }
1477 else if (qp == 0 && rN == 2
1478 && ((rM == fp_reg && fp_reg != 0) || rM == 12))
1479 {
004d836a
JJ
1480 char buf[MAX_REGISTER_SIZE];
1481 CORE_ADDR saved_sp = 0;
16461d7d
KB
1482 /* adds r2, spilloffset, rFramePointer
1483 or
1484 adds r2, spilloffset, r12
1485
1486 Get ready for stf.spill or st8.spill instructions.
1777feb0 1487 The address to start spilling at is loaded into r2.
16461d7d
KB
1488 FIXME: Why r2? That's what gcc currently uses; it
1489 could well be different for other compilers. */
1490
1777feb0 1491 /* Hmm... whether or not this will work will depend on
16461d7d
KB
1492 where the pc is. If it's still early in the prologue
1493 this'll be wrong. FIXME */
15c1e57f 1494 if (this_frame)
004d836a 1495 {
e17a4113
UW
1496 struct gdbarch *gdbarch = get_frame_arch (this_frame);
1497 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
15c1e57f 1498 get_frame_register (this_frame, sp_regnum, buf);
e17a4113 1499 saved_sp = extract_unsigned_integer (buf, 8, byte_order);
004d836a
JJ
1500 }
1501 spill_addr = saved_sp
16461d7d
KB
1502 + (rM == 12 ? 0 : mem_stack_frame_size)
1503 + imm;
1504 spill_reg = rN;
1505 last_prologue_pc = next_pc;
1506 }
b7d038ae 1507 else if (qp == 0 && rM >= 32 && rM < 40 && !instores[rM-32] &&
5ea2bd7f
JJ
1508 rN < 256 && imm == 0)
1509 {
1777feb0 1510 /* mov rN, rM where rM is an input register. */
5ea2bd7f
JJ
1511 reg_contents[rN] = rM;
1512 last_prologue_pc = next_pc;
1513 }
1514 else if (frameless && qp == 0 && rN == fp_reg && imm == 0 &&
1515 rM == 2)
1516 {
1517 /* mov r12, r2 */
1518 last_prologue_pc = next_pc;
1519 break;
1520 }
16461d7d
KB
1521 }
1522 else if (it == M
1523 && ( ((instr & 0x1efc0000000LL) == 0x0eec0000000LL)
1524 || ((instr & 0x1ffc8000000LL) == 0x0cec0000000LL) ))
1525 {
1526 /* stf.spill [rN] = fM, imm9
1527 or
1528 stf.spill [rN] = fM */
1529
1530 int imm = imm9(instr);
1531 int rN = (int) ((instr & 0x00007f00000LL) >> 20);
1532 int fM = (int) ((instr & 0x000000fe000LL) >> 13);
1533 int qp = (int) (instr & 0x0000000003fLL);
1534 if (qp == 0 && rN == spill_reg && spill_addr != 0
1535 && ((2 <= fM && fM <= 5) || (16 <= fM && fM <= 31)))
1536 {
004d836a 1537 cache->saved_regs[IA64_FR0_REGNUM + fM] = spill_addr;
16461d7d 1538
594706e6 1539 if ((instr & 0x1efc0000000LL) == 0x0eec0000000LL)
16461d7d
KB
1540 spill_addr += imm;
1541 else
1777feb0 1542 spill_addr = 0; /* last one; must be done. */
16461d7d
KB
1543 last_prologue_pc = next_pc;
1544 }
1545 }
1546 else if ((it == M && ((instr & 0x1eff8000000LL) == 0x02110000000LL))
1547 || (it == I && ((instr & 0x1eff8000000LL) == 0x00050000000LL)) )
1548 {
1549 /* mov.m rN = arM
1550 or
1551 mov.i rN = arM */
1552
1553 int arM = (int) ((instr & 0x00007f00000LL) >> 20);
1554 int rN = (int) ((instr & 0x00000001fc0LL) >> 6);
1555 int qp = (int) (instr & 0x0000000003fLL);
1556 if (qp == 0 && isScratch (rN) && arM == 36 /* ar.unat */)
1557 {
1558 /* We have something like "mov.m r3 = ar.unat". Remember the
1777feb0 1559 r3 (or whatever) and watch for a store of this register... */
16461d7d
KB
1560 unat_save_reg = rN;
1561 last_prologue_pc = next_pc;
1562 }
1563 }
1564 else if (it == I && ((instr & 0x1eff8000000LL) == 0x00198000000LL))
1565 {
1566 /* mov rN = pr */
1567 int rN = (int) ((instr & 0x00000001fc0LL) >> 6);
1568 int qp = (int) (instr & 0x0000000003fLL);
1569 if (qp == 0 && isScratch (rN))
1570 {
1571 pr_save_reg = rN;
1572 last_prologue_pc = next_pc;
1573 }
1574 }
1575 else if (it == M
1576 && ( ((instr & 0x1ffc8000000LL) == 0x08cc0000000LL)
1577 || ((instr & 0x1efc0000000LL) == 0x0acc0000000LL)))
1578 {
1579 /* st8 [rN] = rM
1580 or
1581 st8 [rN] = rM, imm9 */
1582 int rN = (int) ((instr & 0x00007f00000LL) >> 20);
1583 int rM = (int) ((instr & 0x000000fe000LL) >> 13);
1584 int qp = (int) (instr & 0x0000000003fLL);
5ea2bd7f 1585 int indirect = rM < 256 ? reg_contents[rM] : 0;
16461d7d
KB
1586 if (qp == 0 && rN == spill_reg && spill_addr != 0
1587 && (rM == unat_save_reg || rM == pr_save_reg))
1588 {
1589 /* We've found a spill of either the UNAT register or the PR
1590 register. (Well, not exactly; what we've actually found is
1591 a spill of the register that UNAT or PR was moved to).
1777feb0 1592 Record that fact and move on... */
16461d7d
KB
1593 if (rM == unat_save_reg)
1594 {
1777feb0 1595 /* Track UNAT register. */
004d836a 1596 cache->saved_regs[IA64_UNAT_REGNUM] = spill_addr;
16461d7d
KB
1597 unat_save_reg = 0;
1598 }
1599 else
1600 {
1777feb0 1601 /* Track PR register. */
004d836a 1602 cache->saved_regs[IA64_PR_REGNUM] = spill_addr;
16461d7d
KB
1603 pr_save_reg = 0;
1604 }
1605 if ((instr & 0x1efc0000000LL) == 0x0acc0000000LL)
1606 /* st8 [rN] = rM, imm9 */
1607 spill_addr += imm9(instr);
1608 else
1777feb0 1609 spill_addr = 0; /* Must be done spilling. */
16461d7d
KB
1610 last_prologue_pc = next_pc;
1611 }
0927a22b
KB
1612 else if (qp == 0 && 32 <= rM && rM < 40 && !instores[rM-32])
1613 {
1777feb0 1614 /* Allow up to one store of each input register. */
0927a22b
KB
1615 instores[rM-32] = 1;
1616 last_prologue_pc = next_pc;
1617 }
5ea2bd7f
JJ
1618 else if (qp == 0 && 32 <= indirect && indirect < 40 &&
1619 !instores[indirect-32])
1620 {
1621 /* Allow an indirect store of an input register. */
1622 instores[indirect-32] = 1;
1623 last_prologue_pc = next_pc;
1624 }
0927a22b
KB
1625 }
1626 else if (it == M && ((instr & 0x1ff08000000LL) == 0x08c00000000LL))
1627 {
1628 /* One of
1629 st1 [rN] = rM
1630 st2 [rN] = rM
1631 st4 [rN] = rM
1632 st8 [rN] = rM
1633 Note that the st8 case is handled in the clause above.
1634
1777feb0
MS
1635 Advance over stores of input registers. One store per input
1636 register is permitted. */
0927a22b
KB
1637 int rM = (int) ((instr & 0x000000fe000LL) >> 13);
1638 int qp = (int) (instr & 0x0000000003fLL);
5ea2bd7f 1639 int indirect = rM < 256 ? reg_contents[rM] : 0;
0927a22b
KB
1640 if (qp == 0 && 32 <= rM && rM < 40 && !instores[rM-32])
1641 {
1642 instores[rM-32] = 1;
1643 last_prologue_pc = next_pc;
1644 }
5ea2bd7f
JJ
1645 else if (qp == 0 && 32 <= indirect && indirect < 40 &&
1646 !instores[indirect-32])
1647 {
1648 /* Allow an indirect store of an input register. */
1649 instores[indirect-32] = 1;
1650 last_prologue_pc = next_pc;
1651 }
0927a22b
KB
1652 }
1653 else if (it == M && ((instr & 0x1ff88000000LL) == 0x0cc80000000LL))
1654 {
1655 /* Either
1656 stfs [rN] = fM
1657 or
1658 stfd [rN] = fM
1659
1660 Advance over stores of floating point input registers. Again
1777feb0 1661 one store per register is permitted. */
0927a22b
KB
1662 int fM = (int) ((instr & 0x000000fe000LL) >> 13);
1663 int qp = (int) (instr & 0x0000000003fLL);
1664 if (qp == 0 && 8 <= fM && fM < 16 && !infpstores[fM - 8])
1665 {
1666 infpstores[fM-8] = 1;
1667 last_prologue_pc = next_pc;
1668 }
16461d7d
KB
1669 }
1670 else if (it == M
1671 && ( ((instr & 0x1ffc8000000LL) == 0x08ec0000000LL)
1672 || ((instr & 0x1efc0000000LL) == 0x0aec0000000LL)))
1673 {
1674 /* st8.spill [rN] = rM
1675 or
1676 st8.spill [rN] = rM, imm9 */
1677 int rN = (int) ((instr & 0x00007f00000LL) >> 20);
1678 int rM = (int) ((instr & 0x000000fe000LL) >> 13);
1679 int qp = (int) (instr & 0x0000000003fLL);
1680 if (qp == 0 && rN == spill_reg && 4 <= rM && rM <= 7)
1681 {
1682 /* We've found a spill of one of the preserved general purpose
1683 regs. Record the spill address and advance the spill
1777feb0 1684 register if appropriate. */
004d836a 1685 cache->saved_regs[IA64_GR0_REGNUM + rM] = spill_addr;
16461d7d
KB
1686 if ((instr & 0x1efc0000000LL) == 0x0aec0000000LL)
1687 /* st8.spill [rN] = rM, imm9 */
1688 spill_addr += imm9(instr);
1689 else
1777feb0 1690 spill_addr = 0; /* Done spilling. */
16461d7d
KB
1691 last_prologue_pc = next_pc;
1692 }
1693 }
16461d7d
KB
1694
1695 pc = next_pc;
1696 }
1697
15c1e57f
JB
1698 /* If not frameless and we aren't called by skip_prologue, then we need
1699 to calculate registers for the previous frame which will be needed
1700 later. */
16461d7d 1701
15c1e57f 1702 if (!frameless && this_frame)
da50a4b7 1703 {
e17a4113
UW
1704 struct gdbarch *gdbarch = get_frame_arch (this_frame);
1705 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1706
004d836a
JJ
1707 /* Extract the size of the rotating portion of the stack
1708 frame and the register rename base from the current
1777feb0 1709 frame marker. */
004d836a
JJ
1710 cfm = cache->cfm;
1711 sor = cache->sor;
1712 sof = cache->sof;
1713 sol = cache->sol;
1714 rrb_gr = (cfm >> 18) & 0x7f;
1715
1716 /* Find the bof (beginning of frame). */
1717 bof = rse_address_add (cache->bsp, -sof);
1718
1719 for (i = 0, addr = bof;
1720 i < sof;
1721 i++, addr += 8)
1722 {
1723 if (IS_NaT_COLLECTION_ADDR (addr))
1724 {
1725 addr += 8;
1726 }
1727 if (i+32 == cfm_reg)
1728 cache->saved_regs[IA64_CFM_REGNUM] = addr;
1729 if (i+32 == ret_reg)
1730 cache->saved_regs[IA64_VRAP_REGNUM] = addr;
1731 if (i+32 == fp_reg)
1732 cache->saved_regs[IA64_VFP_REGNUM] = addr;
1733 }
16461d7d 1734
1777feb0 1735 /* For the previous argument registers we require the previous bof.
004d836a 1736 If we can't find the previous cfm, then we can do nothing. */
4afcc598 1737 cfm = 0;
004d836a
JJ
1738 if (cache->saved_regs[IA64_CFM_REGNUM] != 0)
1739 {
e17a4113
UW
1740 cfm = read_memory_integer (cache->saved_regs[IA64_CFM_REGNUM],
1741 8, byte_order);
4afcc598
JJ
1742 }
1743 else if (cfm_reg != 0)
1744 {
15c1e57f 1745 get_frame_register (this_frame, cfm_reg, buf);
e17a4113 1746 cfm = extract_unsigned_integer (buf, 8, byte_order);
4afcc598
JJ
1747 }
1748 cache->prev_cfm = cfm;
1749
1750 if (cfm != 0)
1751 {
004d836a
JJ
1752 sor = ((cfm >> 14) & 0xf) * 8;
1753 sof = (cfm & 0x7f);
1754 sol = (cfm >> 7) & 0x7f;
1755 rrb_gr = (cfm >> 18) & 0x7f;
1756
15c1e57f
JB
1757 /* The previous bof only requires subtraction of the sol (size of
1758 locals) due to the overlap between output and input of
1759 subsequent frames. */
004d836a
JJ
1760 bof = rse_address_add (bof, -sol);
1761
1762 for (i = 0, addr = bof;
1763 i < sof;
1764 i++, addr += 8)
1765 {
1766 if (IS_NaT_COLLECTION_ADDR (addr))
1767 {
1768 addr += 8;
1769 }
1770 if (i < sor)
1777feb0
MS
1771 cache->saved_regs[IA64_GR32_REGNUM
1772 + ((i + (sor - rrb_gr)) % sor)]
004d836a
JJ
1773 = addr;
1774 else
1775 cache->saved_regs[IA64_GR32_REGNUM + i] = addr;
1776 }
1777
1778 }
1779 }
1780
5ea2bd7f
JJ
1781 /* Try and trust the lim_pc value whenever possible. */
1782 if (trust_limit && lim_pc >= last_prologue_pc)
004d836a
JJ
1783 last_prologue_pc = lim_pc;
1784
1785 cache->frameless = frameless;
1786 cache->after_prologue = last_prologue_pc;
1787 cache->mem_stack_frame_size = mem_stack_frame_size;
1788 cache->fp_reg = fp_reg;
5ea2bd7f 1789
16461d7d
KB
1790 return last_prologue_pc;
1791}
1792
1793CORE_ADDR
6093d2eb 1794ia64_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
16461d7d 1795{
004d836a
JJ
1796 struct ia64_frame_cache cache;
1797 cache.base = 0;
1798 cache.after_prologue = 0;
1799 cache.cfm = 0;
1800 cache.bsp = 0;
1801
1777feb0
MS
1802 /* Call examine_prologue with - as third argument since we don't
1803 have a next frame pointer to send. */
004d836a 1804 return examine_prologue (pc, pc+1024, 0, &cache);
16461d7d
KB
1805}
1806
004d836a
JJ
1807
1808/* Normal frames. */
1809
1810static struct ia64_frame_cache *
15c1e57f 1811ia64_frame_cache (struct frame_info *this_frame, void **this_cache)
16461d7d 1812{
e17a4113
UW
1813 struct gdbarch *gdbarch = get_frame_arch (this_frame);
1814 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
004d836a
JJ
1815 struct ia64_frame_cache *cache;
1816 char buf[8];
1817 CORE_ADDR cfm, sof, sol, bsp, psr;
1818 int i;
16461d7d 1819
004d836a
JJ
1820 if (*this_cache)
1821 return *this_cache;
16461d7d 1822
004d836a
JJ
1823 cache = ia64_alloc_frame_cache ();
1824 *this_cache = cache;
16461d7d 1825
15c1e57f 1826 get_frame_register (this_frame, sp_regnum, buf);
e17a4113 1827 cache->saved_sp = extract_unsigned_integer (buf, 8, byte_order);
16461d7d 1828
004d836a
JJ
1829 /* We always want the bsp to point to the end of frame.
1830 This way, we can always get the beginning of frame (bof)
1831 by subtracting frame size. */
15c1e57f 1832 get_frame_register (this_frame, IA64_BSP_REGNUM, buf);
e17a4113 1833 cache->bsp = extract_unsigned_integer (buf, 8, byte_order);
004d836a 1834
15c1e57f 1835 get_frame_register (this_frame, IA64_PSR_REGNUM, buf);
e17a4113 1836 psr = extract_unsigned_integer (buf, 8, byte_order);
004d836a 1837
15c1e57f 1838 get_frame_register (this_frame, IA64_CFM_REGNUM, buf);
e17a4113 1839 cfm = extract_unsigned_integer (buf, 8, byte_order);
004d836a
JJ
1840
1841 cache->sof = (cfm & 0x7f);
1842 cache->sol = (cfm >> 7) & 0x7f;
1843 cache->sor = ((cfm >> 14) & 0xf) * 8;
1844
1845 cache->cfm = cfm;
1846
15c1e57f 1847 cache->pc = get_frame_func (this_frame);
004d836a
JJ
1848
1849 if (cache->pc != 0)
15c1e57f 1850 examine_prologue (cache->pc, get_frame_pc (this_frame), this_frame, cache);
004d836a
JJ
1851
1852 cache->base = cache->saved_sp + cache->mem_stack_frame_size;
1853
1854 return cache;
16461d7d
KB
1855}
1856
a78f21af 1857static void
15c1e57f 1858ia64_frame_this_id (struct frame_info *this_frame, void **this_cache,
004d836a 1859 struct frame_id *this_id)
16461d7d 1860{
5af949e3 1861 struct gdbarch *gdbarch = get_frame_arch (this_frame);
004d836a 1862 struct ia64_frame_cache *cache =
15c1e57f 1863 ia64_frame_cache (this_frame, this_cache);
16461d7d 1864
c5a27d9c 1865 /* If outermost frame, mark with null frame id. */
005ca36a 1866 if (cache->base != 0)
c5a27d9c 1867 (*this_id) = frame_id_build_special (cache->base, cache->pc, cache->bsp);
4afcc598
JJ
1868 if (gdbarch_debug >= 1)
1869 fprintf_unfiltered (gdb_stdlog,
1777feb0
MS
1870 "regular frame id: code %s, stack %s, "
1871 "special %s, this_frame %s\n",
5af949e3
UW
1872 paddress (gdbarch, this_id->code_addr),
1873 paddress (gdbarch, this_id->stack_addr),
1874 paddress (gdbarch, cache->bsp),
dfc3cd0e 1875 host_address_to_string (this_frame));
004d836a 1876}
244bc108 1877
15c1e57f
JB
1878static struct value *
1879ia64_frame_prev_register (struct frame_info *this_frame, void **this_cache,
1880 int regnum)
004d836a 1881{
15c1e57f 1882 struct gdbarch *gdbarch = get_frame_arch (this_frame);
e17a4113 1883 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
15c1e57f 1884 struct ia64_frame_cache *cache = ia64_frame_cache (this_frame, this_cache);
004d836a
JJ
1885 char buf[8];
1886
1887 gdb_assert (regnum >= 0);
244bc108 1888
004d836a 1889 if (!target_has_registers)
8a3fe4f8 1890 error (_("No registers."));
244bc108 1891
088568da 1892 if (regnum == gdbarch_sp_regnum (gdbarch))
15c1e57f
JB
1893 return frame_unwind_got_constant (this_frame, regnum, cache->base);
1894
16461d7d
KB
1895 else if (regnum == IA64_BSP_REGNUM)
1896 {
15c1e57f
JB
1897 struct value *val;
1898 CORE_ADDR prev_cfm, bsp, prev_bsp;
1899
1900 /* We want to calculate the previous bsp as the end of the previous
1901 register stack frame. This corresponds to what the hardware bsp
1902 register will be if we pop the frame back which is why we might
1903 have been called. We know the beginning of the current frame is
1904 cache->bsp - cache->sof. This value in the previous frame points
1905 to the start of the output registers. We can calculate the end of
1906 that frame by adding the size of output:
1907 (sof (size of frame) - sol (size of locals)). */
1908 val = ia64_frame_prev_register (this_frame, this_cache, IA64_CFM_REGNUM);
e17a4113
UW
1909 prev_cfm = extract_unsigned_integer (value_contents_all (val),
1910 8, byte_order);
004d836a 1911 bsp = rse_address_add (cache->bsp, -(cache->sof));
15c1e57f
JB
1912 prev_bsp =
1913 rse_address_add (bsp, (prev_cfm & 0x7f) - ((prev_cfm >> 7) & 0x7f));
004d836a 1914
15c1e57f 1915 return frame_unwind_got_constant (this_frame, regnum, prev_bsp);
004d836a 1916 }
15c1e57f 1917
004d836a
JJ
1918 else if (regnum == IA64_CFM_REGNUM)
1919 {
4afcc598
JJ
1920 CORE_ADDR addr = cache->saved_regs[IA64_CFM_REGNUM];
1921
1922 if (addr != 0)
15c1e57f
JB
1923 return frame_unwind_got_memory (this_frame, regnum, addr);
1924
1925 if (cache->prev_cfm)
1926 return frame_unwind_got_constant (this_frame, regnum, cache->prev_cfm);
1927
1928 if (cache->frameless)
1929 return frame_unwind_got_register (this_frame, IA64_PFS_REGNUM,
1930 IA64_PFS_REGNUM);
1931 return frame_unwind_got_register (this_frame, regnum, 0);
16461d7d 1932 }
15c1e57f 1933
16461d7d
KB
1934 else if (regnum == IA64_VFP_REGNUM)
1935 {
1936 /* If the function in question uses an automatic register (r32-r127)
1937 for the frame pointer, it'll be found by ia64_find_saved_register()
1938 above. If the function lacks one of these frame pointers, we can
004d836a 1939 still provide a value since we know the size of the frame. */
15c1e57f 1940 return frame_unwind_got_constant (this_frame, regnum, cache->base);
16461d7d 1941 }
15c1e57f 1942
004d836a 1943 else if (VP0_REGNUM <= regnum && regnum <= VP63_REGNUM)
16461d7d 1944 {
15c1e57f
JB
1945 struct value *pr_val;
1946 ULONGEST prN;
1947
1948 pr_val = ia64_frame_prev_register (this_frame, this_cache,
1949 IA64_PR_REGNUM);
004d836a 1950 if (VP16_REGNUM <= regnum && regnum <= VP63_REGNUM)
3a854e23
KB
1951 {
1952 /* Fetch predicate register rename base from current frame
004d836a
JJ
1953 marker for this frame. */
1954 int rrb_pr = (cache->cfm >> 32) & 0x3f;
3a854e23 1955
004d836a 1956 /* Adjust the register number to account for register rotation. */
15c1e57f 1957 regnum = VP16_REGNUM + ((regnum - VP16_REGNUM) + rrb_pr) % 48;
3a854e23 1958 }
15c1e57f
JB
1959 prN = extract_bit_field (value_contents_all (pr_val),
1960 regnum - VP0_REGNUM, 1);
1961 return frame_unwind_got_constant (this_frame, regnum, prN);
16461d7d 1962 }
15c1e57f 1963
16461d7d
KB
1964 else if (IA64_NAT0_REGNUM <= regnum && regnum <= IA64_NAT31_REGNUM)
1965 {
15c1e57f
JB
1966 struct value *unat_val;
1967 ULONGEST unatN;
1968 unat_val = ia64_frame_prev_register (this_frame, this_cache,
1969 IA64_UNAT_REGNUM);
1970 unatN = extract_bit_field (value_contents_all (unat_val),
1971 regnum - IA64_NAT0_REGNUM, 1);
1972 return frame_unwind_got_constant (this_frame, regnum, unatN);
16461d7d 1973 }
15c1e57f 1974
16461d7d
KB
1975 else if (IA64_NAT32_REGNUM <= regnum && regnum <= IA64_NAT127_REGNUM)
1976 {
1977 int natval = 0;
1978 /* Find address of general register corresponding to nat bit we're
004d836a
JJ
1979 interested in. */
1980 CORE_ADDR gr_addr;
244bc108 1981
15c1e57f
JB
1982 gr_addr = cache->saved_regs[regnum - IA64_NAT0_REGNUM + IA64_GR0_REGNUM];
1983
004d836a 1984 if (gr_addr != 0)
244bc108 1985 {
004d836a 1986 /* Compute address of nat collection bits. */
16461d7d 1987 CORE_ADDR nat_addr = gr_addr | 0x1f8;
004d836a 1988 CORE_ADDR bsp;
16461d7d
KB
1989 CORE_ADDR nat_collection;
1990 int nat_bit;
15c1e57f 1991
16461d7d
KB
1992 /* If our nat collection address is bigger than bsp, we have to get
1993 the nat collection from rnat. Otherwise, we fetch the nat
004d836a 1994 collection from the computed address. */
15c1e57f 1995 get_frame_register (this_frame, IA64_BSP_REGNUM, buf);
e17a4113 1996 bsp = extract_unsigned_integer (buf, 8, byte_order);
16461d7d 1997 if (nat_addr >= bsp)
004d836a 1998 {
15c1e57f 1999 get_frame_register (this_frame, IA64_RNAT_REGNUM, buf);
e17a4113 2000 nat_collection = extract_unsigned_integer (buf, 8, byte_order);
004d836a 2001 }
16461d7d 2002 else
e17a4113 2003 nat_collection = read_memory_integer (nat_addr, 8, byte_order);
16461d7d
KB
2004 nat_bit = (gr_addr >> 3) & 0x3f;
2005 natval = (nat_collection >> nat_bit) & 1;
2006 }
004d836a 2007
15c1e57f 2008 return frame_unwind_got_constant (this_frame, regnum, natval);
244bc108 2009 }
15c1e57f 2010
244bc108
KB
2011 else if (regnum == IA64_IP_REGNUM)
2012 {
004d836a 2013 CORE_ADDR pc = 0;
4afcc598 2014 CORE_ADDR addr = cache->saved_regs[IA64_VRAP_REGNUM];
004d836a 2015
4afcc598 2016 if (addr != 0)
15c1e57f
JB
2017 {
2018 read_memory (addr, buf, register_size (gdbarch, IA64_IP_REGNUM));
e17a4113 2019 pc = extract_unsigned_integer (buf, 8, byte_order);
15c1e57f 2020 }
4afcc598 2021 else if (cache->frameless)
004d836a 2022 {
15c1e57f 2023 get_frame_register (this_frame, IA64_BR0_REGNUM, buf);
e17a4113 2024 pc = extract_unsigned_integer (buf, 8, byte_order);
244bc108 2025 }
004d836a 2026 pc &= ~0xf;
15c1e57f 2027 return frame_unwind_got_constant (this_frame, regnum, pc);
244bc108 2028 }
15c1e57f 2029
004d836a 2030 else if (regnum == IA64_PSR_REGNUM)
244bc108 2031 {
15c1e57f
JB
2032 /* We don't know how to get the complete previous PSR, but we need it
2033 for the slot information when we unwind the pc (pc is formed of IP
2034 register plus slot information from PSR). To get the previous
2035 slot information, we mask it off the return address. */
004d836a 2036 ULONGEST slot_num = 0;
15c1e57f 2037 CORE_ADDR pc = 0;
004d836a 2038 CORE_ADDR psr = 0;
4afcc598 2039 CORE_ADDR addr = cache->saved_regs[IA64_VRAP_REGNUM];
004d836a 2040
15c1e57f 2041 get_frame_register (this_frame, IA64_PSR_REGNUM, buf);
e17a4113 2042 psr = extract_unsigned_integer (buf, 8, byte_order);
004d836a 2043
4afcc598 2044 if (addr != 0)
244bc108 2045 {
088568da 2046 read_memory (addr, buf, register_size (gdbarch, IA64_IP_REGNUM));
e17a4113 2047 pc = extract_unsigned_integer (buf, 8, byte_order);
244bc108 2048 }
4afcc598 2049 else if (cache->frameless)
004d836a 2050 {
15c1e57f 2051 get_frame_register (this_frame, IA64_BR0_REGNUM, buf);
e17a4113 2052 pc = extract_unsigned_integer (buf, 8, byte_order);
004d836a
JJ
2053 }
2054 psr &= ~(3LL << 41);
2055 slot_num = pc & 0x3LL;
2056 psr |= (CORE_ADDR)slot_num << 41;
15c1e57f 2057 return frame_unwind_got_constant (this_frame, regnum, psr);
004d836a 2058 }
15c1e57f 2059
4afcc598
JJ
2060 else if (regnum == IA64_BR0_REGNUM)
2061 {
4afcc598 2062 CORE_ADDR addr = cache->saved_regs[IA64_BR0_REGNUM];
15c1e57f 2063
4afcc598 2064 if (addr != 0)
15c1e57f
JB
2065 return frame_unwind_got_memory (this_frame, regnum, addr);
2066
2067 return frame_unwind_got_constant (this_frame, regnum, 0);
4afcc598 2068 }
15c1e57f
JB
2069
2070 else if ((regnum >= IA64_GR32_REGNUM && regnum <= IA64_GR127_REGNUM)
2071 || (regnum >= V32_REGNUM && regnum <= V127_REGNUM))
004d836a
JJ
2072 {
2073 CORE_ADDR addr = 0;
15c1e57f 2074
004d836a
JJ
2075 if (regnum >= V32_REGNUM)
2076 regnum = IA64_GR32_REGNUM + (regnum - V32_REGNUM);
2077 addr = cache->saved_regs[regnum];
244bc108 2078 if (addr != 0)
15c1e57f
JB
2079 return frame_unwind_got_memory (this_frame, regnum, addr);
2080
2081 if (cache->frameless)
244bc108 2082 {
15c1e57f
JB
2083 struct value *reg_val;
2084 CORE_ADDR prev_cfm, prev_bsp, prev_bof;
2085
2086 /* FIXME: brobecker/2008-05-01: Doesn't this seem redundant
2087 with the same code above? */
004d836a
JJ
2088 if (regnum >= V32_REGNUM)
2089 regnum = IA64_GR32_REGNUM + (regnum - V32_REGNUM);
15c1e57f
JB
2090 reg_val = ia64_frame_prev_register (this_frame, this_cache,
2091 IA64_CFM_REGNUM);
2092 prev_cfm = extract_unsigned_integer (value_contents_all (reg_val),
e17a4113 2093 8, byte_order);
15c1e57f
JB
2094 reg_val = ia64_frame_prev_register (this_frame, this_cache,
2095 IA64_BSP_REGNUM);
2096 prev_bsp = extract_unsigned_integer (value_contents_all (reg_val),
e17a4113 2097 8, byte_order);
004d836a
JJ
2098 prev_bof = rse_address_add (prev_bsp, -(prev_cfm & 0x7f));
2099
2100 addr = rse_address_add (prev_bof, (regnum - IA64_GR32_REGNUM));
15c1e57f 2101 return frame_unwind_got_memory (this_frame, regnum, addr);
244bc108 2102 }
15c1e57f
JB
2103
2104 return frame_unwind_got_constant (this_frame, regnum, 0);
16461d7d 2105 }
15c1e57f
JB
2106
2107 else /* All other registers. */
16461d7d 2108 {
004d836a 2109 CORE_ADDR addr = 0;
15c1e57f 2110
3a854e23
KB
2111 if (IA64_FR32_REGNUM <= regnum && regnum <= IA64_FR127_REGNUM)
2112 {
2113 /* Fetch floating point register rename base from current
004d836a
JJ
2114 frame marker for this frame. */
2115 int rrb_fr = (cache->cfm >> 25) & 0x7f;
3a854e23
KB
2116
2117 /* Adjust the floating point register number to account for
004d836a 2118 register rotation. */
3a854e23
KB
2119 regnum = IA64_FR32_REGNUM
2120 + ((regnum - IA64_FR32_REGNUM) + rrb_fr) % 96;
2121 }
2122
004d836a
JJ
2123 /* If we have stored a memory address, access the register. */
2124 addr = cache->saved_regs[regnum];
2125 if (addr != 0)
15c1e57f 2126 return frame_unwind_got_memory (this_frame, regnum, addr);
004d836a
JJ
2127 /* Otherwise, punt and get the current value of the register. */
2128 else
15c1e57f 2129 return frame_unwind_got_register (this_frame, regnum, regnum);
16461d7d 2130 }
16461d7d 2131}
004d836a
JJ
2132
2133static const struct frame_unwind ia64_frame_unwind =
2134{
2135 NORMAL_FRAME,
2136 &ia64_frame_this_id,
15c1e57f
JB
2137 &ia64_frame_prev_register,
2138 NULL,
2139 default_frame_sniffer
004d836a
JJ
2140};
2141
004d836a
JJ
2142/* Signal trampolines. */
2143
2144static void
15c1e57f 2145ia64_sigtramp_frame_init_saved_regs (struct frame_info *this_frame,
2685572f 2146 struct ia64_frame_cache *cache)
004d836a 2147{
e17a4113
UW
2148 struct gdbarch *gdbarch = get_frame_arch (this_frame);
2149 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2685572f
UW
2150
2151 if (tdep->sigcontext_register_address)
004d836a
JJ
2152 {
2153 int regno;
2154
1777feb0
MS
2155 cache->saved_regs[IA64_VRAP_REGNUM]
2156 = tdep->sigcontext_register_address (gdbarch, cache->base,
2157 IA64_IP_REGNUM);
2158 cache->saved_regs[IA64_CFM_REGNUM]
2159 = tdep->sigcontext_register_address (gdbarch, cache->base,
2160 IA64_CFM_REGNUM);
2161 cache->saved_regs[IA64_PSR_REGNUM]
2162 = tdep->sigcontext_register_address (gdbarch, cache->base,
2163 IA64_PSR_REGNUM);
2164 cache->saved_regs[IA64_BSP_REGNUM]
2165 = tdep->sigcontext_register_address (gdbarch, cache->base,
2166 IA64_BSP_REGNUM);
2167 cache->saved_regs[IA64_RNAT_REGNUM]
2168 = tdep->sigcontext_register_address (gdbarch, cache->base,
2169 IA64_RNAT_REGNUM);
2170 cache->saved_regs[IA64_CCV_REGNUM]
2171 = tdep->sigcontext_register_address (gdbarch, cache->base,
2172 IA64_CCV_REGNUM);
2173 cache->saved_regs[IA64_UNAT_REGNUM]
2174 = tdep->sigcontext_register_address (gdbarch, cache->base,
2175 IA64_UNAT_REGNUM);
2176 cache->saved_regs[IA64_FPSR_REGNUM]
2177 = tdep->sigcontext_register_address (gdbarch, cache->base,
2178 IA64_FPSR_REGNUM);
2179 cache->saved_regs[IA64_PFS_REGNUM]
2180 = tdep->sigcontext_register_address (gdbarch, cache->base,
2181 IA64_PFS_REGNUM);
2182 cache->saved_regs[IA64_LC_REGNUM]
2183 = tdep->sigcontext_register_address (gdbarch, cache->base,
2184 IA64_LC_REGNUM);
2185
004d836a 2186 for (regno = IA64_GR1_REGNUM; regno <= IA64_GR31_REGNUM; regno++)
4afcc598 2187 cache->saved_regs[regno] =
e17a4113 2188 tdep->sigcontext_register_address (gdbarch, cache->base, regno);
004d836a
JJ
2189 for (regno = IA64_BR0_REGNUM; regno <= IA64_BR7_REGNUM; regno++)
2190 cache->saved_regs[regno] =
e17a4113 2191 tdep->sigcontext_register_address (gdbarch, cache->base, regno);
932644f0 2192 for (regno = IA64_FR2_REGNUM; regno <= IA64_FR31_REGNUM; regno++)
004d836a 2193 cache->saved_regs[regno] =
e17a4113 2194 tdep->sigcontext_register_address (gdbarch, cache->base, regno);
004d836a
JJ
2195 }
2196}
2197
2198static struct ia64_frame_cache *
15c1e57f 2199ia64_sigtramp_frame_cache (struct frame_info *this_frame, void **this_cache)
004d836a 2200{
e17a4113
UW
2201 struct gdbarch *gdbarch = get_frame_arch (this_frame);
2202 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
004d836a
JJ
2203 struct ia64_frame_cache *cache;
2204 CORE_ADDR addr;
2205 char buf[8];
2206 int i;
2207
2208 if (*this_cache)
2209 return *this_cache;
2210
2211 cache = ia64_alloc_frame_cache ();
2212
15c1e57f 2213 get_frame_register (this_frame, sp_regnum, buf);
4afcc598
JJ
2214 /* Note that frame size is hard-coded below. We cannot calculate it
2215 via prologue examination. */
e17a4113 2216 cache->base = extract_unsigned_integer (buf, 8, byte_order) + 16;
4afcc598 2217
15c1e57f 2218 get_frame_register (this_frame, IA64_BSP_REGNUM, buf);
e17a4113 2219 cache->bsp = extract_unsigned_integer (buf, 8, byte_order);
4afcc598 2220
15c1e57f 2221 get_frame_register (this_frame, IA64_CFM_REGNUM, buf);
e17a4113 2222 cache->cfm = extract_unsigned_integer (buf, 8, byte_order);
4afcc598 2223 cache->sof = cache->cfm & 0x7f;
004d836a 2224
15c1e57f 2225 ia64_sigtramp_frame_init_saved_regs (this_frame, cache);
004d836a
JJ
2226
2227 *this_cache = cache;
2228 return cache;
2229}
2230
2231static void
15c1e57f
JB
2232ia64_sigtramp_frame_this_id (struct frame_info *this_frame,
2233 void **this_cache, struct frame_id *this_id)
004d836a 2234{
5af949e3 2235 struct gdbarch *gdbarch = get_frame_arch (this_frame);
004d836a 2236 struct ia64_frame_cache *cache =
15c1e57f 2237 ia64_sigtramp_frame_cache (this_frame, this_cache);
004d836a 2238
15c1e57f
JB
2239 (*this_id) = frame_id_build_special (cache->base,
2240 get_frame_pc (this_frame),
2241 cache->bsp);
4afcc598
JJ
2242 if (gdbarch_debug >= 1)
2243 fprintf_unfiltered (gdb_stdlog,
1777feb0
MS
2244 "sigtramp frame id: code %s, stack %s, "
2245 "special %s, this_frame %s\n",
5af949e3
UW
2246 paddress (gdbarch, this_id->code_addr),
2247 paddress (gdbarch, this_id->stack_addr),
2248 paddress (gdbarch, cache->bsp),
dfc3cd0e 2249 host_address_to_string (this_frame));
004d836a
JJ
2250}
2251
15c1e57f
JB
2252static struct value *
2253ia64_sigtramp_frame_prev_register (struct frame_info *this_frame,
2254 void **this_cache, int regnum)
004d836a 2255{
4afcc598
JJ
2256 char buf[MAX_REGISTER_SIZE];
2257
15c1e57f 2258 struct gdbarch *gdbarch = get_frame_arch (this_frame);
e17a4113 2259 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
4afcc598 2260 struct ia64_frame_cache *cache =
15c1e57f 2261 ia64_sigtramp_frame_cache (this_frame, this_cache);
4afcc598
JJ
2262
2263 gdb_assert (regnum >= 0);
2264
2265 if (!target_has_registers)
8a3fe4f8 2266 error (_("No registers."));
4afcc598 2267
4afcc598
JJ
2268 if (regnum == IA64_IP_REGNUM)
2269 {
2270 CORE_ADDR pc = 0;
2271 CORE_ADDR addr = cache->saved_regs[IA64_VRAP_REGNUM];
2272
2273 if (addr != 0)
2274 {
088568da 2275 read_memory (addr, buf, register_size (gdbarch, IA64_IP_REGNUM));
e17a4113 2276 pc = extract_unsigned_integer (buf, 8, byte_order);
4afcc598
JJ
2277 }
2278 pc &= ~0xf;
15c1e57f 2279 return frame_unwind_got_constant (this_frame, regnum, pc);
4afcc598 2280 }
15c1e57f
JB
2281
2282 else if ((regnum >= IA64_GR32_REGNUM && regnum <= IA64_GR127_REGNUM)
2283 || (regnum >= V32_REGNUM && regnum <= V127_REGNUM))
4afcc598
JJ
2284 {
2285 CORE_ADDR addr = 0;
15c1e57f 2286
4afcc598
JJ
2287 if (regnum >= V32_REGNUM)
2288 regnum = IA64_GR32_REGNUM + (regnum - V32_REGNUM);
2289 addr = cache->saved_regs[regnum];
2290 if (addr != 0)
15c1e57f
JB
2291 return frame_unwind_got_memory (this_frame, regnum, addr);
2292
2293 return frame_unwind_got_constant (this_frame, regnum, 0);
4afcc598 2294 }
15c1e57f
JB
2295
2296 else /* All other registers not listed above. */
4afcc598 2297 {
4afcc598 2298 CORE_ADDR addr = cache->saved_regs[regnum];
15c1e57f 2299
4afcc598 2300 if (addr != 0)
15c1e57f 2301 return frame_unwind_got_memory (this_frame, regnum, addr);
004d836a 2302
15c1e57f
JB
2303 return frame_unwind_got_constant (this_frame, regnum, 0);
2304 }
004d836a
JJ
2305}
2306
15c1e57f
JB
2307static int
2308ia64_sigtramp_frame_sniffer (const struct frame_unwind *self,
2309 struct frame_info *this_frame,
2310 void **this_cache)
004d836a 2311{
15c1e57f 2312 struct gdbarch_tdep *tdep = gdbarch_tdep (get_frame_arch (this_frame));
74174d2e
UW
2313 if (tdep->pc_in_sigtramp)
2314 {
15c1e57f 2315 CORE_ADDR pc = get_frame_pc (this_frame);
004d836a 2316
74174d2e 2317 if (tdep->pc_in_sigtramp (pc))
15c1e57f 2318 return 1;
74174d2e 2319 }
004d836a 2320
15c1e57f 2321 return 0;
004d836a 2322}
15c1e57f
JB
2323
2324static const struct frame_unwind ia64_sigtramp_frame_unwind =
2325{
2326 SIGTRAMP_FRAME,
2327 ia64_sigtramp_frame_this_id,
2328 ia64_sigtramp_frame_prev_register,
2329 NULL,
2330 ia64_sigtramp_frame_sniffer
2331};
2332
004d836a
JJ
2333\f
2334
2335static CORE_ADDR
15c1e57f 2336ia64_frame_base_address (struct frame_info *this_frame, void **this_cache)
004d836a 2337{
15c1e57f 2338 struct ia64_frame_cache *cache = ia64_frame_cache (this_frame, this_cache);
004d836a
JJ
2339
2340 return cache->base;
2341}
2342
2343static const struct frame_base ia64_frame_base =
2344{
2345 &ia64_frame_unwind,
2346 ia64_frame_base_address,
2347 ia64_frame_base_address,
2348 ia64_frame_base_address
2349};
16461d7d 2350
968d1cb4
JJ
2351#ifdef HAVE_LIBUNWIND_IA64_H
2352
2353struct ia64_unwind_table_entry
2354 {
2355 unw_word_t start_offset;
2356 unw_word_t end_offset;
2357 unw_word_t info_offset;
2358 };
2359
2360static __inline__ uint64_t
2361ia64_rse_slot_num (uint64_t addr)
2362{
2363 return (addr >> 3) & 0x3f;
2364}
2365
2366/* Skip over a designated number of registers in the backing
2367 store, remembering every 64th position is for NAT. */
2368static __inline__ uint64_t
2369ia64_rse_skip_regs (uint64_t addr, long num_regs)
2370{
2371 long delta = ia64_rse_slot_num(addr) + num_regs;
2372
2373 if (num_regs < 0)
2374 delta -= 0x3e;
2375 return addr + ((num_regs + delta/0x3f) << 3);
2376}
2377
2378/* Gdb libunwind-frame callback function to convert from an ia64 gdb register
2379 number to a libunwind register number. */
2380static int
2381ia64_gdb2uw_regnum (int regnum)
2382{
2383 if (regnum == sp_regnum)
2384 return UNW_IA64_SP;
2385 else if (regnum == IA64_BSP_REGNUM)
2386 return UNW_IA64_BSP;
2387 else if ((unsigned) (regnum - IA64_GR0_REGNUM) < 128)
2388 return UNW_IA64_GR + (regnum - IA64_GR0_REGNUM);
2389 else if ((unsigned) (regnum - V32_REGNUM) < 95)
2390 return UNW_IA64_GR + 32 + (regnum - V32_REGNUM);
2391 else if ((unsigned) (regnum - IA64_FR0_REGNUM) < 128)
2392 return UNW_IA64_FR + (regnum - IA64_FR0_REGNUM);
2393 else if ((unsigned) (regnum - IA64_PR0_REGNUM) < 64)
2394 return -1;
2395 else if ((unsigned) (regnum - IA64_BR0_REGNUM) < 8)
2396 return UNW_IA64_BR + (regnum - IA64_BR0_REGNUM);
2397 else if (regnum == IA64_PR_REGNUM)
2398 return UNW_IA64_PR;
2399 else if (regnum == IA64_IP_REGNUM)
2400 return UNW_REG_IP;
2401 else if (regnum == IA64_CFM_REGNUM)
2402 return UNW_IA64_CFM;
2403 else if ((unsigned) (regnum - IA64_AR0_REGNUM) < 128)
2404 return UNW_IA64_AR + (regnum - IA64_AR0_REGNUM);
2405 else if ((unsigned) (regnum - IA64_NAT0_REGNUM) < 128)
2406 return UNW_IA64_NAT + (regnum - IA64_NAT0_REGNUM);
2407 else
2408 return -1;
2409}
2410
2411/* Gdb libunwind-frame callback function to convert from a libunwind register
2412 number to a ia64 gdb register number. */
2413static int
2414ia64_uw2gdb_regnum (int uw_regnum)
2415{
2416 if (uw_regnum == UNW_IA64_SP)
2417 return sp_regnum;
2418 else if (uw_regnum == UNW_IA64_BSP)
2419 return IA64_BSP_REGNUM;
2420 else if ((unsigned) (uw_regnum - UNW_IA64_GR) < 32)
2421 return IA64_GR0_REGNUM + (uw_regnum - UNW_IA64_GR);
2422 else if ((unsigned) (uw_regnum - UNW_IA64_GR) < 128)
2423 return V32_REGNUM + (uw_regnum - (IA64_GR0_REGNUM + 32));
2424 else if ((unsigned) (uw_regnum - UNW_IA64_FR) < 128)
2425 return IA64_FR0_REGNUM + (uw_regnum - UNW_IA64_FR);
2426 else if ((unsigned) (uw_regnum - UNW_IA64_BR) < 8)
2427 return IA64_BR0_REGNUM + (uw_regnum - UNW_IA64_BR);
2428 else if (uw_regnum == UNW_IA64_PR)
2429 return IA64_PR_REGNUM;
2430 else if (uw_regnum == UNW_REG_IP)
2431 return IA64_IP_REGNUM;
2432 else if (uw_regnum == UNW_IA64_CFM)
2433 return IA64_CFM_REGNUM;
2434 else if ((unsigned) (uw_regnum - UNW_IA64_AR) < 128)
2435 return IA64_AR0_REGNUM + (uw_regnum - UNW_IA64_AR);
2436 else if ((unsigned) (uw_regnum - UNW_IA64_NAT) < 128)
2437 return IA64_NAT0_REGNUM + (uw_regnum - UNW_IA64_NAT);
2438 else
2439 return -1;
2440}
2441
2442/* Gdb libunwind-frame callback function to reveal if register is a float
2443 register or not. */
2444static int
2445ia64_is_fpreg (int uw_regnum)
2446{
2447 return unw_is_fpreg (uw_regnum);
2448}
2449
2450/* Libunwind callback accessor function for general registers. */
2451static int
2452ia64_access_reg (unw_addr_space_t as, unw_regnum_t uw_regnum, unw_word_t *val,
2453 int write, void *arg)
2454{
2455 int regnum = ia64_uw2gdb_regnum (uw_regnum);
2456 unw_word_t bsp, sof, sol, cfm, psr, ip;
15c1e57f 2457 struct frame_info *this_frame = arg;
5af949e3 2458 struct gdbarch *gdbarch = get_frame_arch (this_frame);
e17a4113 2459 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
968d1cb4
JJ
2460 long new_sof, old_sof;
2461 char buf[MAX_REGISTER_SIZE];
2462
45ecac4b
UW
2463 /* We never call any libunwind routines that need to write registers. */
2464 gdb_assert (!write);
968d1cb4 2465
45ecac4b 2466 switch (uw_regnum)
968d1cb4 2467 {
45ecac4b
UW
2468 case UNW_REG_IP:
2469 /* Libunwind expects to see the pc value which means the slot number
2470 from the psr must be merged with the ip word address. */
15c1e57f 2471 get_frame_register (this_frame, IA64_IP_REGNUM, buf);
e17a4113 2472 ip = extract_unsigned_integer (buf, 8, byte_order);
15c1e57f 2473 get_frame_register (this_frame, IA64_PSR_REGNUM, buf);
e17a4113 2474 psr = extract_unsigned_integer (buf, 8, byte_order);
45ecac4b
UW
2475 *val = ip | ((psr >> 41) & 0x3);
2476 break;
2477
2478 case UNW_IA64_AR_BSP:
1777feb0
MS
2479 /* Libunwind expects to see the beginning of the current
2480 register frame so we must account for the fact that
2481 ptrace() will return a value for bsp that points *after*
2482 the current register frame. */
15c1e57f 2483 get_frame_register (this_frame, IA64_BSP_REGNUM, buf);
e17a4113 2484 bsp = extract_unsigned_integer (buf, 8, byte_order);
15c1e57f 2485 get_frame_register (this_frame, IA64_CFM_REGNUM, buf);
e17a4113 2486 cfm = extract_unsigned_integer (buf, 8, byte_order);
45ecac4b
UW
2487 sof = (cfm & 0x7f);
2488 *val = ia64_rse_skip_regs (bsp, -sof);
2489 break;
968d1cb4 2490
45ecac4b
UW
2491 case UNW_IA64_AR_BSPSTORE:
2492 /* Libunwind wants bspstore to be after the current register frame.
2493 This is what ptrace() and gdb treats as the regular bsp value. */
15c1e57f 2494 get_frame_register (this_frame, IA64_BSP_REGNUM, buf);
e17a4113 2495 *val = extract_unsigned_integer (buf, 8, byte_order);
45ecac4b
UW
2496 break;
2497
2498 default:
2499 /* For all other registers, just unwind the value directly. */
15c1e57f 2500 get_frame_register (this_frame, regnum, buf);
e17a4113 2501 *val = extract_unsigned_integer (buf, 8, byte_order);
45ecac4b 2502 break;
968d1cb4 2503 }
45ecac4b
UW
2504
2505 if (gdbarch_debug >= 1)
2506 fprintf_unfiltered (gdb_stdlog,
5af949e3 2507 " access_reg: from cache: %4s=%s\n",
45ecac4b
UW
2508 (((unsigned) regnum <= IA64_NAT127_REGNUM)
2509 ? ia64_register_names[regnum] : "r??"),
2edfe795 2510 paddress (gdbarch, *val));
968d1cb4
JJ
2511 return 0;
2512}
2513
2514/* Libunwind callback accessor function for floating-point registers. */
2515static int
1777feb0
MS
2516ia64_access_fpreg (unw_addr_space_t as, unw_regnum_t uw_regnum,
2517 unw_fpreg_t *val, int write, void *arg)
968d1cb4
JJ
2518{
2519 int regnum = ia64_uw2gdb_regnum (uw_regnum);
15c1e57f 2520 struct frame_info *this_frame = arg;
968d1cb4 2521
45ecac4b
UW
2522 /* We never call any libunwind routines that need to write registers. */
2523 gdb_assert (!write);
2524
15c1e57f 2525 get_frame_register (this_frame, regnum, (char *) val);
45ecac4b 2526
968d1cb4
JJ
2527 return 0;
2528}
2529
c5a27d9c
JJ
2530/* Libunwind callback accessor function for top-level rse registers. */
2531static int
1777feb0
MS
2532ia64_access_rse_reg (unw_addr_space_t as, unw_regnum_t uw_regnum,
2533 unw_word_t *val, int write, void *arg)
c5a27d9c
JJ
2534{
2535 int regnum = ia64_uw2gdb_regnum (uw_regnum);
2536 unw_word_t bsp, sof, sol, cfm, psr, ip;
45ecac4b 2537 struct regcache *regcache = arg;
5af949e3 2538 struct gdbarch *gdbarch = get_regcache_arch (regcache);
e17a4113 2539 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
c5a27d9c 2540 long new_sof, old_sof;
45ecac4b 2541 char buf[MAX_REGISTER_SIZE];
c5a27d9c 2542
45ecac4b
UW
2543 /* We never call any libunwind routines that need to write registers. */
2544 gdb_assert (!write);
c5a27d9c 2545
45ecac4b 2546 switch (uw_regnum)
c5a27d9c 2547 {
45ecac4b
UW
2548 case UNW_REG_IP:
2549 /* Libunwind expects to see the pc value which means the slot number
2550 from the psr must be merged with the ip word address. */
2551 regcache_cooked_read (regcache, IA64_IP_REGNUM, buf);
e17a4113 2552 ip = extract_unsigned_integer (buf, 8, byte_order);
45ecac4b 2553 regcache_cooked_read (regcache, IA64_PSR_REGNUM, buf);
e17a4113 2554 psr = extract_unsigned_integer (buf, 8, byte_order);
45ecac4b
UW
2555 *val = ip | ((psr >> 41) & 0x3);
2556 break;
c5a27d9c 2557
45ecac4b 2558 case UNW_IA64_AR_BSP:
1777feb0
MS
2559 /* Libunwind expects to see the beginning of the current
2560 register frame so we must account for the fact that
2561 ptrace() will return a value for bsp that points *after*
2562 the current register frame. */
45ecac4b 2563 regcache_cooked_read (regcache, IA64_BSP_REGNUM, buf);
e17a4113 2564 bsp = extract_unsigned_integer (buf, 8, byte_order);
45ecac4b 2565 regcache_cooked_read (regcache, IA64_CFM_REGNUM, buf);
e17a4113 2566 cfm = extract_unsigned_integer (buf, 8, byte_order);
45ecac4b
UW
2567 sof = (cfm & 0x7f);
2568 *val = ia64_rse_skip_regs (bsp, -sof);
2569 break;
c5a27d9c 2570
45ecac4b
UW
2571 case UNW_IA64_AR_BSPSTORE:
2572 /* Libunwind wants bspstore to be after the current register frame.
2573 This is what ptrace() and gdb treats as the regular bsp value. */
2574 regcache_cooked_read (regcache, IA64_BSP_REGNUM, buf);
e17a4113 2575 *val = extract_unsigned_integer (buf, 8, byte_order);
45ecac4b 2576 break;
c5a27d9c 2577
45ecac4b
UW
2578 default:
2579 /* For all other registers, just unwind the value directly. */
2580 regcache_cooked_read (regcache, regnum, buf);
e17a4113 2581 *val = extract_unsigned_integer (buf, 8, byte_order);
45ecac4b 2582 break;
c5a27d9c
JJ
2583 }
2584
2585 if (gdbarch_debug >= 1)
2586 fprintf_unfiltered (gdb_stdlog,
5af949e3 2587 " access_rse_reg: from cache: %4s=%s\n",
c5a27d9c
JJ
2588 (((unsigned) regnum <= IA64_NAT127_REGNUM)
2589 ? ia64_register_names[regnum] : "r??"),
5af949e3 2590 paddress (gdbarch, *val));
c5a27d9c
JJ
2591
2592 return 0;
2593}
2594
45ecac4b
UW
2595/* Libunwind callback accessor function for top-level fp registers. */
2596static int
2597ia64_access_rse_fpreg (unw_addr_space_t as, unw_regnum_t uw_regnum,
2598 unw_fpreg_t *val, int write, void *arg)
2599{
2600 int regnum = ia64_uw2gdb_regnum (uw_regnum);
2601 struct regcache *regcache = arg;
2602
2603 /* We never call any libunwind routines that need to write registers. */
2604 gdb_assert (!write);
2605
2606 regcache_cooked_read (regcache, regnum, (char *) val);
2607
2608 return 0;
2609}
2610
968d1cb4
JJ
2611/* Libunwind callback accessor function for accessing memory. */
2612static int
2613ia64_access_mem (unw_addr_space_t as,
2614 unw_word_t addr, unw_word_t *val,
2615 int write, void *arg)
2616{
c5a27d9c
JJ
2617 if (addr - KERNEL_START < ktab_size)
2618 {
2619 unw_word_t *laddr = (unw_word_t*) ((char *) ktab
2620 + (addr - KERNEL_START));
2621
2622 if (write)
2623 *laddr = *val;
2624 else
2625 *val = *laddr;
2626 return 0;
2627 }
2628
968d1cb4
JJ
2629 /* XXX do we need to normalize byte-order here? */
2630 if (write)
2631 return target_write_memory (addr, (char *) val, sizeof (unw_word_t));
2632 else
2633 return target_read_memory (addr, (char *) val, sizeof (unw_word_t));
2634}
2635
2636/* Call low-level function to access the kernel unwind table. */
13547ab6
DJ
2637static LONGEST
2638getunwind_table (gdb_byte **buf_p)
968d1cb4
JJ
2639{
2640 LONGEST x;
c5a27d9c 2641
10d6c8cd
DJ
2642 /* FIXME drow/2005-09-10: This code used to call
2643 ia64_linux_xfer_unwind_table directly to fetch the unwind table
2644 for the currently running ia64-linux kernel. That data should
2645 come from the core file and be accessed via the auxv vector; if
2646 we want to preserve fall back to the running kernel's table, then
2647 we should find a way to override the corefile layer's
2648 xfer_partial method. */
968d1cb4 2649
13547ab6
DJ
2650 x = target_read_alloc (&current_target, TARGET_OBJECT_UNWIND_TABLE,
2651 NULL, buf_p);
2652
2653 return x;
968d1cb4 2654}
10d6c8cd 2655
968d1cb4
JJ
2656/* Get the kernel unwind table. */
2657static int
2658get_kernel_table (unw_word_t ip, unw_dyn_info_t *di)
2659{
c5a27d9c 2660 static struct ia64_table_entry *etab;
968d1cb4 2661
c5a27d9c 2662 if (!ktab)
968d1cb4 2663 {
13547ab6 2664 gdb_byte *ktab_buf;
eeec829c 2665 LONGEST size;
13547ab6 2666
eeec829c
DJ
2667 size = getunwind_table (&ktab_buf);
2668 if (size <= 0)
13547ab6 2669 return -UNW_ENOINFO;
eeec829c
DJ
2670
2671 ktab = (struct ia64_table_entry *) ktab_buf;
2672 ktab_size = size;
13547ab6 2673
968d1cb4 2674 for (etab = ktab; etab->start_offset; ++etab)
c5a27d9c 2675 etab->info_offset += KERNEL_START;
968d1cb4
JJ
2676 }
2677
2678 if (ip < ktab[0].start_offset || ip >= etab[-1].end_offset)
2679 return -UNW_ENOINFO;
2680
2681 di->format = UNW_INFO_FORMAT_TABLE;
2682 di->gp = 0;
2683 di->start_ip = ktab[0].start_offset;
2684 di->end_ip = etab[-1].end_offset;
2685 di->u.ti.name_ptr = (unw_word_t) "<kernel>";
2686 di->u.ti.segbase = 0;
2687 di->u.ti.table_len = ((char *) etab - (char *) ktab) / sizeof (unw_word_t);
2688 di->u.ti.table_data = (unw_word_t *) ktab;
2689
2690 if (gdbarch_debug >= 1)
2691 fprintf_unfiltered (gdb_stdlog, "get_kernel_table: found table `%s': "
5af949e3 2692 "segbase=%s, length=%s, gp=%s\n",
78ced177 2693 (char *) di->u.ti.name_ptr,
5af949e3 2694 hex_string (di->u.ti.segbase),
623d3eb1 2695 pulongest (di->u.ti.table_len),
5af949e3 2696 hex_string (di->gp));
968d1cb4
JJ
2697 return 0;
2698}
2699
2700/* Find the unwind table entry for a specified address. */
2701static int
2702ia64_find_unwind_table (struct objfile *objfile, unw_word_t ip,
2703 unw_dyn_info_t *dip, void **buf)
2704{
2705 Elf_Internal_Phdr *phdr, *p_text = NULL, *p_unwind = NULL;
2706 Elf_Internal_Ehdr *ehdr;
2707 unw_word_t segbase = 0;
2708 CORE_ADDR load_base;
2709 bfd *bfd;
2710 int i;
2711
2712 bfd = objfile->obfd;
2713
2714 ehdr = elf_tdata (bfd)->elf_header;
2715 phdr = elf_tdata (bfd)->phdr;
2716
2717 load_base = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2718
2719 for (i = 0; i < ehdr->e_phnum; ++i)
2720 {
2721 switch (phdr[i].p_type)
2722 {
2723 case PT_LOAD:
2724 if ((unw_word_t) (ip - load_base - phdr[i].p_vaddr)
2725 < phdr[i].p_memsz)
2726 p_text = phdr + i;
2727 break;
2728
2729 case PT_IA_64_UNWIND:
2730 p_unwind = phdr + i;
2731 break;
2732
2733 default:
2734 break;
2735 }
2736 }
2737
c5a27d9c 2738 if (!p_text || !p_unwind)
968d1cb4
JJ
2739 return -UNW_ENOINFO;
2740
c5a27d9c
JJ
2741 /* Verify that the segment that contains the IP also contains
2742 the static unwind table. If not, we may be in the Linux kernel's
1777feb0 2743 DSO gate page in which case the unwind table is another segment.
c5a27d9c
JJ
2744 Otherwise, we are dealing with runtime-generated code, for which we
2745 have no info here. */
968d1cb4
JJ
2746 segbase = p_text->p_vaddr + load_base;
2747
c5a27d9c
JJ
2748 if ((p_unwind->p_vaddr - p_text->p_vaddr) >= p_text->p_memsz)
2749 {
2750 int ok = 0;
2751 for (i = 0; i < ehdr->e_phnum; ++i)
2752 {
2753 if (phdr[i].p_type == PT_LOAD
2754 && (p_unwind->p_vaddr - phdr[i].p_vaddr) < phdr[i].p_memsz)
2755 {
2756 ok = 1;
2757 /* Get the segbase from the section containing the
2758 libunwind table. */
2759 segbase = phdr[i].p_vaddr + load_base;
2760 }
2761 }
2762 if (!ok)
2763 return -UNW_ENOINFO;
2764 }
2765
2766 dip->start_ip = p_text->p_vaddr + load_base;
968d1cb4 2767 dip->end_ip = dip->start_ip + p_text->p_memsz;
e17a4113 2768 dip->gp = ia64_find_global_pointer (get_objfile_arch (objfile), ip);
503ff15d
KB
2769 dip->format = UNW_INFO_FORMAT_REMOTE_TABLE;
2770 dip->u.rti.name_ptr = (unw_word_t) bfd_get_filename (bfd);
2771 dip->u.rti.segbase = segbase;
2772 dip->u.rti.table_len = p_unwind->p_memsz / sizeof (unw_word_t);
2773 dip->u.rti.table_data = p_unwind->p_vaddr + load_base;
968d1cb4
JJ
2774
2775 return 0;
2776}
2777
2778/* Libunwind callback accessor function to acquire procedure unwind-info. */
2779static int
2780ia64_find_proc_info_x (unw_addr_space_t as, unw_word_t ip, unw_proc_info_t *pi,
2781 int need_unwind_info, void *arg)
2782{
2783 struct obj_section *sec = find_pc_section (ip);
2784 unw_dyn_info_t di;
2785 int ret;
2786 void *buf = NULL;
2787
2788 if (!sec)
2789 {
2790 /* XXX This only works if the host and the target architecture are
2791 both ia64 and if the have (more or less) the same kernel
2792 version. */
2793 if (get_kernel_table (ip, &di) < 0)
2794 return -UNW_ENOINFO;
503ff15d
KB
2795
2796 if (gdbarch_debug >= 1)
5af949e3
UW
2797 fprintf_unfiltered (gdb_stdlog, "ia64_find_proc_info_x: %s -> "
2798 "(name=`%s',segbase=%s,start=%s,end=%s,gp=%s,"
2799 "length=%s,data=%s)\n",
2800 hex_string (ip), (char *)di.u.ti.name_ptr,
2801 hex_string (di.u.ti.segbase),
2802 hex_string (di.start_ip), hex_string (di.end_ip),
2803 hex_string (di.gp),
623d3eb1 2804 pulongest (di.u.ti.table_len),
5af949e3 2805 hex_string ((CORE_ADDR)di.u.ti.table_data));
968d1cb4
JJ
2806 }
2807 else
2808 {
2809 ret = ia64_find_unwind_table (sec->objfile, ip, &di, &buf);
2810 if (ret < 0)
2811 return ret;
968d1cb4 2812
503ff15d 2813 if (gdbarch_debug >= 1)
5af949e3
UW
2814 fprintf_unfiltered (gdb_stdlog, "ia64_find_proc_info_x: %s -> "
2815 "(name=`%s',segbase=%s,start=%s,end=%s,gp=%s,"
2816 "length=%s,data=%s)\n",
2817 hex_string (ip), (char *)di.u.rti.name_ptr,
2818 hex_string (di.u.rti.segbase),
2819 hex_string (di.start_ip), hex_string (di.end_ip),
2820 hex_string (di.gp),
623d3eb1 2821 pulongest (di.u.rti.table_len),
5af949e3 2822 hex_string (di.u.rti.table_data));
503ff15d 2823 }
968d1cb4 2824
503ff15d
KB
2825 ret = libunwind_search_unwind_table (&as, ip, &di, pi, need_unwind_info,
2826 arg);
968d1cb4
JJ
2827
2828 /* We no longer need the dyn info storage so free it. */
2829 xfree (buf);
2830
2831 return ret;
2832}
2833
2834/* Libunwind callback accessor function for cleanup. */
2835static void
2836ia64_put_unwind_info (unw_addr_space_t as,
2837 unw_proc_info_t *pip, void *arg)
2838{
2839 /* Nothing required for now. */
2840}
2841
2842/* Libunwind callback accessor function to get head of the dynamic
2843 unwind-info registration list. */
2844static int
2845ia64_get_dyn_info_list (unw_addr_space_t as,
2846 unw_word_t *dilap, void *arg)
2847{
2848 struct obj_section *text_sec;
2849 struct objfile *objfile;
2850 unw_word_t ip, addr;
2851 unw_dyn_info_t di;
2852 int ret;
2853
2854 if (!libunwind_is_initialized ())
2855 return -UNW_ENOINFO;
2856
2857 for (objfile = object_files; objfile; objfile = objfile->next)
2858 {
2859 void *buf = NULL;
2860
2861 text_sec = objfile->sections + SECT_OFF_TEXT (objfile);
8b7a6d61 2862 ip = obj_section_addr (text_sec);
968d1cb4
JJ
2863 ret = ia64_find_unwind_table (objfile, ip, &di, &buf);
2864 if (ret >= 0)
2865 {
503ff15d 2866 addr = libunwind_find_dyn_list (as, &di, arg);
968d1cb4
JJ
2867 /* We no longer need the dyn info storage so free it. */
2868 xfree (buf);
2869
2870 if (addr)
2871 {
2872 if (gdbarch_debug >= 1)
2873 fprintf_unfiltered (gdb_stdlog,
2874 "dynamic unwind table in objfile %s "
5af949e3 2875 "at %s (gp=%s)\n",
968d1cb4 2876 bfd_get_filename (objfile->obfd),
5af949e3 2877 hex_string (addr), hex_string (di.gp));
968d1cb4
JJ
2878 *dilap = addr;
2879 return 0;
2880 }
2881 }
2882 }
2883 return -UNW_ENOINFO;
2884}
2885
2886
2887/* Frame interface functions for libunwind. */
2888
2889static void
15c1e57f 2890ia64_libunwind_frame_this_id (struct frame_info *this_frame, void **this_cache,
7166c4a9 2891 struct frame_id *this_id)
968d1cb4 2892{
5af949e3 2893 struct gdbarch *gdbarch = get_frame_arch (this_frame);
e17a4113 2894 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
005ca36a 2895 struct frame_id id = outer_frame_id;
968d1cb4
JJ
2896 char buf[8];
2897 CORE_ADDR bsp;
c5a27d9c 2898
15c1e57f 2899 libunwind_frame_this_id (this_frame, this_cache, &id);
005ca36a 2900 if (frame_id_eq (id, outer_frame_id))
c5a27d9c 2901 {
005ca36a 2902 (*this_id) = outer_frame_id;
c5a27d9c
JJ
2903 return;
2904 }
968d1cb4 2905
c5a27d9c
JJ
2906 /* We must add the bsp as the special address for frame comparison
2907 purposes. */
15c1e57f 2908 get_frame_register (this_frame, IA64_BSP_REGNUM, buf);
e17a4113 2909 bsp = extract_unsigned_integer (buf, 8, byte_order);
968d1cb4 2910
15c1e57f 2911 (*this_id) = frame_id_build_special (id.stack_addr, id.code_addr, bsp);
968d1cb4
JJ
2912
2913 if (gdbarch_debug >= 1)
2914 fprintf_unfiltered (gdb_stdlog,
1777feb0
MS
2915 "libunwind frame id: code %s, stack %s, "
2916 "special %s, this_frame %s\n",
5af949e3
UW
2917 paddress (gdbarch, id.code_addr),
2918 paddress (gdbarch, id.stack_addr),
2919 paddress (gdbarch, bsp),
dfc3cd0e 2920 host_address_to_string (this_frame));
968d1cb4
JJ
2921}
2922
15c1e57f
JB
2923static struct value *
2924ia64_libunwind_frame_prev_register (struct frame_info *this_frame,
2925 void **this_cache, int regnum)
968d1cb4
JJ
2926{
2927 int reg = regnum;
15c1e57f 2928 struct gdbarch *gdbarch = get_frame_arch (this_frame);
e17a4113 2929 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
15c1e57f 2930 struct value *val;
968d1cb4
JJ
2931
2932 if (VP0_REGNUM <= regnum && regnum <= VP63_REGNUM)
2933 reg = IA64_PR_REGNUM;
2934 else if (IA64_NAT0_REGNUM <= regnum && regnum <= IA64_NAT127_REGNUM)
2935 reg = IA64_UNAT_REGNUM;
2936
2937 /* Let libunwind do most of the work. */
15c1e57f 2938 val = libunwind_frame_prev_register (this_frame, this_cache, reg);
6672f2ae 2939
968d1cb4
JJ
2940 if (VP0_REGNUM <= regnum && regnum <= VP63_REGNUM)
2941 {
2942 ULONGEST prN_val;
2943
2944 if (VP16_REGNUM <= regnum && regnum <= VP63_REGNUM)
2945 {
2946 int rrb_pr = 0;
2947 ULONGEST cfm;
2948 unsigned char buf[MAX_REGISTER_SIZE];
2949
2950 /* Fetch predicate register rename base from current frame
2951 marker for this frame. */
15c1e57f 2952 get_frame_register (this_frame, IA64_CFM_REGNUM, buf);
e17a4113 2953 cfm = extract_unsigned_integer (buf, 8, byte_order);
968d1cb4
JJ
2954 rrb_pr = (cfm >> 32) & 0x3f;
2955
2956 /* Adjust the register number to account for register rotation. */
15c1e57f 2957 regnum = VP16_REGNUM + ((regnum - VP16_REGNUM) + rrb_pr) % 48;
968d1cb4 2958 }
15c1e57f 2959 prN_val = extract_bit_field (value_contents_all (val),
968d1cb4 2960 regnum - VP0_REGNUM, 1);
15c1e57f 2961 return frame_unwind_got_constant (this_frame, regnum, prN_val);
968d1cb4 2962 }
15c1e57f 2963
968d1cb4
JJ
2964 else if (IA64_NAT0_REGNUM <= regnum && regnum <= IA64_NAT127_REGNUM)
2965 {
2966 ULONGEST unatN_val;
2967
15c1e57f
JB
2968 unatN_val = extract_bit_field (value_contents_all (val),
2969 regnum - IA64_NAT0_REGNUM, 1);
2970 return frame_unwind_got_constant (this_frame, regnum, unatN_val);
968d1cb4 2971 }
15c1e57f 2972
968d1cb4
JJ
2973 else if (regnum == IA64_BSP_REGNUM)
2974 {
15c1e57f
JB
2975 struct value *cfm_val;
2976 CORE_ADDR prev_bsp, prev_cfm;
2977
2978 /* We want to calculate the previous bsp as the end of the previous
2979 register stack frame. This corresponds to what the hardware bsp
2980 register will be if we pop the frame back which is why we might
2981 have been called. We know that libunwind will pass us back the
1777feb0 2982 beginning of the current frame so we should just add sof to it. */
e17a4113
UW
2983 prev_bsp = extract_unsigned_integer (value_contents_all (val),
2984 8, byte_order);
15c1e57f
JB
2985 cfm_val = libunwind_frame_prev_register (this_frame, this_cache,
2986 IA64_CFM_REGNUM);
e17a4113
UW
2987 prev_cfm = extract_unsigned_integer (value_contents_all (cfm_val),
2988 8, byte_order);
968d1cb4
JJ
2989 prev_bsp = rse_address_add (prev_bsp, (prev_cfm & 0x7f));
2990
15c1e57f 2991 return frame_unwind_got_constant (this_frame, regnum, prev_bsp);
968d1cb4 2992 }
15c1e57f
JB
2993 else
2994 return val;
2995}
968d1cb4 2996
15c1e57f
JB
2997static int
2998ia64_libunwind_frame_sniffer (const struct frame_unwind *self,
2999 struct frame_info *this_frame,
3000 void **this_cache)
3001{
3002 if (libunwind_is_initialized ()
3003 && libunwind_frame_sniffer (self, this_frame, this_cache))
3004 return 1;
3005
3006 return 0;
968d1cb4
JJ
3007}
3008
3009static const struct frame_unwind ia64_libunwind_frame_unwind =
3010{
3011 NORMAL_FRAME,
3012 ia64_libunwind_frame_this_id,
272dfcfd
AS
3013 ia64_libunwind_frame_prev_register,
3014 NULL,
15c1e57f 3015 ia64_libunwind_frame_sniffer,
272dfcfd 3016 libunwind_frame_dealloc_cache
968d1cb4
JJ
3017};
3018
c5a27d9c 3019static void
15c1e57f
JB
3020ia64_libunwind_sigtramp_frame_this_id (struct frame_info *this_frame,
3021 void **this_cache,
c5a27d9c
JJ
3022 struct frame_id *this_id)
3023{
5af949e3 3024 struct gdbarch *gdbarch = get_frame_arch (this_frame);
e17a4113 3025 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
c5a27d9c
JJ
3026 char buf[8];
3027 CORE_ADDR bsp;
005ca36a 3028 struct frame_id id = outer_frame_id;
c5a27d9c
JJ
3029 CORE_ADDR prev_ip;
3030
15c1e57f 3031 libunwind_frame_this_id (this_frame, this_cache, &id);
005ca36a 3032 if (frame_id_eq (id, outer_frame_id))
c5a27d9c 3033 {
005ca36a 3034 (*this_id) = outer_frame_id;
c5a27d9c
JJ
3035 return;
3036 }
3037
3038 /* We must add the bsp as the special address for frame comparison
3039 purposes. */
15c1e57f 3040 get_frame_register (this_frame, IA64_BSP_REGNUM, buf);
e17a4113 3041 bsp = extract_unsigned_integer (buf, 8, byte_order);
c5a27d9c
JJ
3042
3043 /* For a sigtramp frame, we don't make the check for previous ip being 0. */
3044 (*this_id) = frame_id_build_special (id.stack_addr, id.code_addr, bsp);
3045
3046 if (gdbarch_debug >= 1)
3047 fprintf_unfiltered (gdb_stdlog,
1777feb0
MS
3048 "libunwind sigtramp frame id: code %s, "
3049 "stack %s, special %s, this_frame %s\n",
5af949e3
UW
3050 paddress (gdbarch, id.code_addr),
3051 paddress (gdbarch, id.stack_addr),
3052 paddress (gdbarch, bsp),
dfc3cd0e 3053 host_address_to_string (this_frame));
c5a27d9c
JJ
3054}
3055
15c1e57f
JB
3056static struct value *
3057ia64_libunwind_sigtramp_frame_prev_register (struct frame_info *this_frame,
3058 void **this_cache, int regnum)
c5a27d9c 3059{
e17a4113
UW
3060 struct gdbarch *gdbarch = get_frame_arch (this_frame);
3061 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
15c1e57f
JB
3062 struct value *prev_ip_val;
3063 CORE_ADDR prev_ip;
c5a27d9c
JJ
3064
3065 /* If the previous frame pc value is 0, then we want to use the SIGCONTEXT
3066 method of getting previous registers. */
15c1e57f
JB
3067 prev_ip_val = libunwind_frame_prev_register (this_frame, this_cache,
3068 IA64_IP_REGNUM);
e17a4113
UW
3069 prev_ip = extract_unsigned_integer (value_contents_all (prev_ip_val),
3070 8, byte_order);
c5a27d9c
JJ
3071
3072 if (prev_ip == 0)
3073 {
3074 void *tmp_cache = NULL;
15c1e57f
JB
3075 return ia64_sigtramp_frame_prev_register (this_frame, &tmp_cache,
3076 regnum);
c5a27d9c
JJ
3077 }
3078 else
15c1e57f 3079 return ia64_libunwind_frame_prev_register (this_frame, this_cache, regnum);
c5a27d9c
JJ
3080}
3081
15c1e57f
JB
3082static int
3083ia64_libunwind_sigtramp_frame_sniffer (const struct frame_unwind *self,
3084 struct frame_info *this_frame,
3085 void **this_cache)
c5a27d9c
JJ
3086{
3087 if (libunwind_is_initialized ())
3088 {
15c1e57f
JB
3089 if (libunwind_sigtramp_frame_sniffer (self, this_frame, this_cache))
3090 return 1;
3091 return 0;
c5a27d9c
JJ
3092 }
3093 else
15c1e57f 3094 return ia64_sigtramp_frame_sniffer (self, this_frame, this_cache);
c5a27d9c
JJ
3095}
3096
15c1e57f
JB
3097static const struct frame_unwind ia64_libunwind_sigtramp_frame_unwind =
3098{
3099 SIGTRAMP_FRAME,
3100 ia64_libunwind_sigtramp_frame_this_id,
3101 ia64_libunwind_sigtramp_frame_prev_register,
3102 NULL,
3103 ia64_libunwind_sigtramp_frame_sniffer
3104};
3105
968d1cb4
JJ
3106/* Set of libunwind callback acccessor functions. */
3107static unw_accessors_t ia64_unw_accessors =
3108{
3109 ia64_find_proc_info_x,
3110 ia64_put_unwind_info,
3111 ia64_get_dyn_info_list,
3112 ia64_access_mem,
3113 ia64_access_reg,
3114 ia64_access_fpreg,
3115 /* resume */
3116 /* get_proc_name */
3117};
3118
c5a27d9c
JJ
3119/* Set of special libunwind callback acccessor functions specific for accessing
3120 the rse registers. At the top of the stack, we want libunwind to figure out
1777feb0
MS
3121 how to read r32 - r127. Though usually they are found sequentially in
3122 memory starting from $bof, this is not always true. */
c5a27d9c
JJ
3123static unw_accessors_t ia64_unw_rse_accessors =
3124{
3125 ia64_find_proc_info_x,
3126 ia64_put_unwind_info,
3127 ia64_get_dyn_info_list,
3128 ia64_access_mem,
3129 ia64_access_rse_reg,
45ecac4b 3130 ia64_access_rse_fpreg,
c5a27d9c
JJ
3131 /* resume */
3132 /* get_proc_name */
3133};
3134
1777feb0
MS
3135/* Set of ia64 gdb libunwind-frame callbacks and data for generic
3136 libunwind-frame code to use. */
968d1cb4
JJ
3137static struct libunwind_descr ia64_libunwind_descr =
3138{
3139 ia64_gdb2uw_regnum,
3140 ia64_uw2gdb_regnum,
3141 ia64_is_fpreg,
3142 &ia64_unw_accessors,
c5a27d9c 3143 &ia64_unw_rse_accessors,
968d1cb4
JJ
3144};
3145
3146#endif /* HAVE_LIBUNWIND_IA64_H */
3147
4c8b6ae0
UW
3148static int
3149ia64_use_struct_convention (struct type *type)
16461d7d 3150{
64a5b29c
KB
3151 struct type *float_elt_type;
3152
4c8b6ae0
UW
3153 /* Don't use the struct convention for anything but structure,
3154 union, or array types. */
3155 if (!(TYPE_CODE (type) == TYPE_CODE_STRUCT
3156 || TYPE_CODE (type) == TYPE_CODE_UNION
3157 || TYPE_CODE (type) == TYPE_CODE_ARRAY))
3158 return 0;
3159
64a5b29c
KB
3160 /* HFAs are structures (or arrays) consisting entirely of floating
3161 point values of the same length. Up to 8 of these are returned
3162 in registers. Don't use the struct convention when this is the
004d836a 3163 case. */
64a5b29c
KB
3164 float_elt_type = is_float_or_hfa_type (type);
3165 if (float_elt_type != NULL
3166 && TYPE_LENGTH (type) / TYPE_LENGTH (float_elt_type) <= 8)
3167 return 0;
3168
3169 /* Other structs of length 32 or less are returned in r8-r11.
004d836a 3170 Don't use the struct convention for those either. */
16461d7d
KB
3171 return TYPE_LENGTH (type) > 32;
3172}
3173
4c8b6ae0 3174static void
2d522557
AC
3175ia64_extract_return_value (struct type *type, struct regcache *regcache,
3176 gdb_byte *valbuf)
16461d7d 3177{
27067745 3178 struct gdbarch *gdbarch = get_regcache_arch (regcache);
64a5b29c
KB
3179 struct type *float_elt_type;
3180
3181 float_elt_type = is_float_or_hfa_type (type);
3182 if (float_elt_type != NULL)
3183 {
004d836a 3184 char from[MAX_REGISTER_SIZE];
64a5b29c
KB
3185 int offset = 0;
3186 int regnum = IA64_FR8_REGNUM;
3187 int n = TYPE_LENGTH (type) / TYPE_LENGTH (float_elt_type);
3188
3189 while (n-- > 0)
3190 {
004d836a 3191 regcache_cooked_read (regcache, regnum, from);
27067745 3192 convert_typed_floating (from, ia64_ext_type (gdbarch),
1777feb0 3193 (char *)valbuf + offset, float_elt_type);
64a5b29c
KB
3194 offset += TYPE_LENGTH (float_elt_type);
3195 regnum++;
3196 }
3197 }
16461d7d 3198 else
004d836a
JJ
3199 {
3200 ULONGEST val;
3201 int offset = 0;
3202 int regnum = IA64_GR8_REGNUM;
27067745 3203 int reglen = TYPE_LENGTH (register_type (gdbarch, IA64_GR8_REGNUM));
004d836a
JJ
3204 int n = TYPE_LENGTH (type) / reglen;
3205 int m = TYPE_LENGTH (type) % reglen;
16461d7d 3206
004d836a
JJ
3207 while (n-- > 0)
3208 {
3209 ULONGEST val;
3210 regcache_cooked_read_unsigned (regcache, regnum, &val);
3211 memcpy ((char *)valbuf + offset, &val, reglen);
3212 offset += reglen;
3213 regnum++;
3214 }
16461d7d 3215
004d836a
JJ
3216 if (m)
3217 {
3218 regcache_cooked_read_unsigned (regcache, regnum, &val);
3219 memcpy ((char *)valbuf + offset, &val, m);
3220 }
3221 }
16461d7d
KB
3222}
3223
4c8b6ae0
UW
3224static void
3225ia64_store_return_value (struct type *type, struct regcache *regcache,
3226 const gdb_byte *valbuf)
3227{
27067745 3228 struct gdbarch *gdbarch = get_regcache_arch (regcache);
4c8b6ae0
UW
3229 struct type *float_elt_type;
3230
3231 float_elt_type = is_float_or_hfa_type (type);
3232 if (float_elt_type != NULL)
3233 {
3234 char to[MAX_REGISTER_SIZE];
3235 int offset = 0;
3236 int regnum = IA64_FR8_REGNUM;
3237 int n = TYPE_LENGTH (type) / TYPE_LENGTH (float_elt_type);
3238
3239 while (n-- > 0)
3240 {
3241 convert_typed_floating ((char *)valbuf + offset, float_elt_type,
27067745 3242 to, ia64_ext_type (gdbarch));
4c8b6ae0
UW
3243 regcache_cooked_write (regcache, regnum, to);
3244 offset += TYPE_LENGTH (float_elt_type);
3245 regnum++;
3246 }
3247 }
3248 else
3249 {
3250 ULONGEST val;
3251 int offset = 0;
3252 int regnum = IA64_GR8_REGNUM;
27067745 3253 int reglen = TYPE_LENGTH (register_type (gdbarch, IA64_GR8_REGNUM));
4c8b6ae0
UW
3254 int n = TYPE_LENGTH (type) / reglen;
3255 int m = TYPE_LENGTH (type) % reglen;
3256
3257 while (n-- > 0)
3258 {
3259 ULONGEST val;
3260 memcpy (&val, (char *)valbuf + offset, reglen);
3261 regcache_cooked_write_unsigned (regcache, regnum, val);
3262 offset += reglen;
3263 regnum++;
3264 }
3265
3266 if (m)
3267 {
3268 memcpy (&val, (char *)valbuf + offset, m);
3269 regcache_cooked_write_unsigned (regcache, regnum, val);
3270 }
3271 }
3272}
3273
3274static enum return_value_convention
c055b101
CV
3275ia64_return_value (struct gdbarch *gdbarch, struct type *func_type,
3276 struct type *valtype, struct regcache *regcache,
3277 gdb_byte *readbuf, const gdb_byte *writebuf)
4c8b6ae0
UW
3278{
3279 int struct_return = ia64_use_struct_convention (valtype);
3280
3281 if (writebuf != NULL)
3282 {
3283 gdb_assert (!struct_return);
3284 ia64_store_return_value (valtype, regcache, writebuf);
3285 }
3286
3287 if (readbuf != NULL)
3288 {
3289 gdb_assert (!struct_return);
3290 ia64_extract_return_value (valtype, regcache, readbuf);
3291 }
3292
3293 if (struct_return)
3294 return RETURN_VALUE_STRUCT_CONVENTION;
3295 else
3296 return RETURN_VALUE_REGISTER_CONVENTION;
3297}
16461d7d 3298
64a5b29c
KB
3299static int
3300is_float_or_hfa_type_recurse (struct type *t, struct type **etp)
3301{
3302 switch (TYPE_CODE (t))
3303 {
3304 case TYPE_CODE_FLT:
3305 if (*etp)
3306 return TYPE_LENGTH (*etp) == TYPE_LENGTH (t);
3307 else
3308 {
3309 *etp = t;
3310 return 1;
3311 }
3312 break;
3313 case TYPE_CODE_ARRAY:
98f96ba1
KB
3314 return
3315 is_float_or_hfa_type_recurse (check_typedef (TYPE_TARGET_TYPE (t)),
3316 etp);
64a5b29c
KB
3317 break;
3318 case TYPE_CODE_STRUCT:
3319 {
3320 int i;
3321
3322 for (i = 0; i < TYPE_NFIELDS (t); i++)
98f96ba1
KB
3323 if (!is_float_or_hfa_type_recurse
3324 (check_typedef (TYPE_FIELD_TYPE (t, i)), etp))
64a5b29c
KB
3325 return 0;
3326 return 1;
3327 }
3328 break;
3329 default:
3330 return 0;
3331 break;
3332 }
3333}
3334
3335/* Determine if the given type is one of the floating point types or
3336 and HFA (which is a struct, array, or combination thereof whose
004d836a 3337 bottom-most elements are all of the same floating point type). */
64a5b29c
KB
3338
3339static struct type *
3340is_float_or_hfa_type (struct type *t)
3341{
3342 struct type *et = 0;
3343
3344 return is_float_or_hfa_type_recurse (t, &et) ? et : 0;
3345}
3346
3347
98f96ba1
KB
3348/* Return 1 if the alignment of T is such that the next even slot
3349 should be used. Return 0, if the next available slot should
3350 be used. (See section 8.5.1 of the IA-64 Software Conventions
004d836a 3351 and Runtime manual). */
98f96ba1
KB
3352
3353static int
3354slot_alignment_is_next_even (struct type *t)
3355{
3356 switch (TYPE_CODE (t))
3357 {
3358 case TYPE_CODE_INT:
3359 case TYPE_CODE_FLT:
3360 if (TYPE_LENGTH (t) > 8)
3361 return 1;
3362 else
3363 return 0;
3364 case TYPE_CODE_ARRAY:
3365 return
3366 slot_alignment_is_next_even (check_typedef (TYPE_TARGET_TYPE (t)));
3367 case TYPE_CODE_STRUCT:
3368 {
3369 int i;
3370
3371 for (i = 0; i < TYPE_NFIELDS (t); i++)
3372 if (slot_alignment_is_next_even
3373 (check_typedef (TYPE_FIELD_TYPE (t, i))))
3374 return 1;
3375 return 0;
3376 }
3377 default:
3378 return 0;
3379 }
3380}
3381
64a5b29c
KB
3382/* Attempt to find (and return) the global pointer for the given
3383 function.
3384
3385 This is a rather nasty bit of code searchs for the .dynamic section
3386 in the objfile corresponding to the pc of the function we're trying
3387 to call. Once it finds the addresses at which the .dynamic section
3388 lives in the child process, it scans the Elf64_Dyn entries for a
3389 DT_PLTGOT tag. If it finds one of these, the corresponding
3390 d_un.d_ptr value is the global pointer. */
3391
3392static CORE_ADDR
e17a4113 3393ia64_find_global_pointer (struct gdbarch *gdbarch, CORE_ADDR faddr)
64a5b29c 3394{
e17a4113 3395 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
76d689a6 3396 struct obj_section *faddr_sect;
64a5b29c 3397
76d689a6
KB
3398 faddr_sect = find_pc_section (faddr);
3399 if (faddr_sect != NULL)
64a5b29c
KB
3400 {
3401 struct obj_section *osect;
3402
76d689a6 3403 ALL_OBJFILE_OSECTIONS (faddr_sect->objfile, osect)
64a5b29c
KB
3404 {
3405 if (strcmp (osect->the_bfd_section->name, ".dynamic") == 0)
3406 break;
3407 }
3408
76d689a6 3409 if (osect < faddr_sect->objfile->sections_end)
64a5b29c 3410 {
aded6f54 3411 CORE_ADDR addr, endaddr;
64a5b29c 3412
aded6f54
PA
3413 addr = obj_section_addr (osect);
3414 endaddr = obj_section_endaddr (osect);
3415
3416 while (addr < endaddr)
64a5b29c
KB
3417 {
3418 int status;
3419 LONGEST tag;
3420 char buf[8];
3421
3422 status = target_read_memory (addr, buf, sizeof (buf));
3423 if (status != 0)
3424 break;
e17a4113 3425 tag = extract_signed_integer (buf, sizeof (buf), byte_order);
64a5b29c
KB
3426
3427 if (tag == DT_PLTGOT)
3428 {
3429 CORE_ADDR global_pointer;
3430
3431 status = target_read_memory (addr + 8, buf, sizeof (buf));
3432 if (status != 0)
3433 break;
e17a4113
UW
3434 global_pointer = extract_unsigned_integer (buf, sizeof (buf),
3435 byte_order);
64a5b29c 3436
1777feb0 3437 /* The payoff... */
64a5b29c
KB
3438 return global_pointer;
3439 }
3440
3441 if (tag == DT_NULL)
3442 break;
3443
3444 addr += 16;
3445 }
3446 }
3447 }
3448 return 0;
3449}
3450
3451/* Given a function's address, attempt to find (and return) the
3452 corresponding (canonical) function descriptor. Return 0 if
004d836a 3453 not found. */
64a5b29c 3454static CORE_ADDR
e17a4113 3455find_extant_func_descr (struct gdbarch *gdbarch, CORE_ADDR faddr)
64a5b29c 3456{
e17a4113 3457 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
76d689a6 3458 struct obj_section *faddr_sect;
64a5b29c 3459
004d836a 3460 /* Return early if faddr is already a function descriptor. */
76d689a6
KB
3461 faddr_sect = find_pc_section (faddr);
3462 if (faddr_sect && strcmp (faddr_sect->the_bfd_section->name, ".opd") == 0)
64a5b29c
KB
3463 return faddr;
3464
76d689a6 3465 if (faddr_sect != NULL)
64a5b29c 3466 {
76d689a6
KB
3467 struct obj_section *osect;
3468 ALL_OBJFILE_OSECTIONS (faddr_sect->objfile, osect)
64a5b29c
KB
3469 {
3470 if (strcmp (osect->the_bfd_section->name, ".opd") == 0)
3471 break;
3472 }
3473
76d689a6 3474 if (osect < faddr_sect->objfile->sections_end)
64a5b29c 3475 {
aded6f54
PA
3476 CORE_ADDR addr, endaddr;
3477
3478 addr = obj_section_addr (osect);
3479 endaddr = obj_section_endaddr (osect);
64a5b29c 3480
aded6f54 3481 while (addr < endaddr)
64a5b29c
KB
3482 {
3483 int status;
3484 LONGEST faddr2;
3485 char buf[8];
3486
3487 status = target_read_memory (addr, buf, sizeof (buf));
3488 if (status != 0)
3489 break;
e17a4113 3490 faddr2 = extract_signed_integer (buf, sizeof (buf), byte_order);
64a5b29c
KB
3491
3492 if (faddr == faddr2)
3493 return addr;
3494
3495 addr += 16;
3496 }
3497 }
3498 }
3499 return 0;
3500}
3501
3502/* Attempt to find a function descriptor corresponding to the
3503 given address. If none is found, construct one on the
004d836a 3504 stack using the address at fdaptr. */
64a5b29c
KB
3505
3506static CORE_ADDR
9c9acae0 3507find_func_descr (struct regcache *regcache, CORE_ADDR faddr, CORE_ADDR *fdaptr)
64a5b29c 3508{
e17a4113
UW
3509 struct gdbarch *gdbarch = get_regcache_arch (regcache);
3510 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
64a5b29c
KB
3511 CORE_ADDR fdesc;
3512
e17a4113 3513 fdesc = find_extant_func_descr (gdbarch, faddr);
64a5b29c
KB
3514
3515 if (fdesc == 0)
3516 {
9c9acae0 3517 ULONGEST global_pointer;
64a5b29c
KB
3518 char buf[16];
3519
3520 fdesc = *fdaptr;
3521 *fdaptr += 16;
3522
e17a4113 3523 global_pointer = ia64_find_global_pointer (gdbarch, faddr);
64a5b29c
KB
3524
3525 if (global_pointer == 0)
9c9acae0
UW
3526 regcache_cooked_read_unsigned (regcache,
3527 IA64_GR1_REGNUM, &global_pointer);
64a5b29c 3528
e17a4113
UW
3529 store_unsigned_integer (buf, 8, byte_order, faddr);
3530 store_unsigned_integer (buf + 8, 8, byte_order, global_pointer);
64a5b29c
KB
3531
3532 write_memory (fdesc, buf, 16);
3533 }
3534
3535 return fdesc;
3536}
16461d7d 3537
af8b88dd
JJ
3538/* Use the following routine when printing out function pointers
3539 so the user can see the function address rather than just the
3540 function descriptor. */
3541static CORE_ADDR
e2d0e7eb
AC
3542ia64_convert_from_func_ptr_addr (struct gdbarch *gdbarch, CORE_ADDR addr,
3543 struct target_ops *targ)
af8b88dd 3544{
e17a4113 3545 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
af8b88dd 3546 struct obj_section *s;
e453266f 3547 gdb_byte buf[8];
af8b88dd
JJ
3548
3549 s = find_pc_section (addr);
3550
3551 /* check if ADDR points to a function descriptor. */
3552 if (s && strcmp (s->the_bfd_section->name, ".opd") == 0)
e17a4113 3553 return read_memory_unsigned_integer (addr, 8, byte_order);
af8b88dd 3554
fcac911a
JB
3555 /* Normally, functions live inside a section that is executable.
3556 So, if ADDR points to a non-executable section, then treat it
3557 as a function descriptor and return the target address iff
e453266f
JK
3558 the target address itself points to a section that is executable.
3559 Check first the memory of the whole length of 8 bytes is readable. */
3560 if (s && (s->the_bfd_section->flags & SEC_CODE) == 0
3561 && target_read_memory (addr, buf, 8) == 0)
fcac911a 3562 {
e453266f 3563 CORE_ADDR pc = extract_unsigned_integer (buf, 8, byte_order);
fcac911a
JB
3564 struct obj_section *pc_section = find_pc_section (pc);
3565
3566 if (pc_section && (pc_section->the_bfd_section->flags & SEC_CODE))
3567 return pc;
3568 }
b1e6fd19 3569
0d5de010
DJ
3570 /* There are also descriptors embedded in vtables. */
3571 if (s)
3572 {
3573 struct minimal_symbol *minsym;
3574
3575 minsym = lookup_minimal_symbol_by_pc (addr);
3576
3577 if (minsym && is_vtable_name (SYMBOL_LINKAGE_NAME (minsym)))
e17a4113 3578 return read_memory_unsigned_integer (addr, 8, byte_order);
0d5de010
DJ
3579 }
3580
af8b88dd
JJ
3581 return addr;
3582}
3583
a78f21af 3584static CORE_ADDR
004d836a
JJ
3585ia64_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
3586{
3587 return sp & ~0xfLL;
3588}
3589
3590static CORE_ADDR
7d9b040b 3591ia64_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
8dd5115e
AS
3592 struct regcache *regcache, CORE_ADDR bp_addr,
3593 int nargs, struct value **args, CORE_ADDR sp,
3594 int struct_return, CORE_ADDR struct_addr)
16461d7d 3595{
e17a4113 3596 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
16461d7d 3597 int argno;
ea7c478f 3598 struct value *arg;
16461d7d
KB
3599 struct type *type;
3600 int len, argoffset;
64a5b29c 3601 int nslots, rseslots, memslots, slotnum, nfuncargs;
16461d7d 3602 int floatreg;
9c9acae0
UW
3603 ULONGEST bsp, cfm, pfs, new_bsp;
3604 CORE_ADDR funcdescaddr, pc, global_pointer;
7d9b040b 3605 CORE_ADDR func_addr = find_function_addr (function, NULL);
16461d7d
KB
3606
3607 nslots = 0;
64a5b29c 3608 nfuncargs = 0;
004d836a 3609 /* Count the number of slots needed for the arguments. */
16461d7d
KB
3610 for (argno = 0; argno < nargs; argno++)
3611 {
3612 arg = args[argno];
4991999e 3613 type = check_typedef (value_type (arg));
16461d7d
KB
3614 len = TYPE_LENGTH (type);
3615
98f96ba1 3616 if ((nslots & 1) && slot_alignment_is_next_even (type))
16461d7d
KB
3617 nslots++;
3618
64a5b29c
KB
3619 if (TYPE_CODE (type) == TYPE_CODE_FUNC)
3620 nfuncargs++;
3621
16461d7d
KB
3622 nslots += (len + 7) / 8;
3623 }
3624
004d836a 3625 /* Divvy up the slots between the RSE and the memory stack. */
16461d7d
KB
3626 rseslots = (nslots > 8) ? 8 : nslots;
3627 memslots = nslots - rseslots;
3628
004d836a 3629 /* Allocate a new RSE frame. */
9c9acae0 3630 regcache_cooked_read_unsigned (regcache, IA64_CFM_REGNUM, &cfm);
16461d7d 3631
9c9acae0 3632 regcache_cooked_read_unsigned (regcache, IA64_BSP_REGNUM, &bsp);
16461d7d 3633 new_bsp = rse_address_add (bsp, rseslots);
9c9acae0 3634 regcache_cooked_write_unsigned (regcache, IA64_BSP_REGNUM, new_bsp);
16461d7d 3635
9c9acae0 3636 regcache_cooked_read_unsigned (regcache, IA64_PFS_REGNUM, &pfs);
16461d7d
KB
3637 pfs &= 0xc000000000000000LL;
3638 pfs |= (cfm & 0xffffffffffffLL);
9c9acae0 3639 regcache_cooked_write_unsigned (regcache, IA64_PFS_REGNUM, pfs);
16461d7d
KB
3640
3641 cfm &= 0xc000000000000000LL;
3642 cfm |= rseslots;
9c9acae0 3643 regcache_cooked_write_unsigned (regcache, IA64_CFM_REGNUM, cfm);
16461d7d 3644
64a5b29c
KB
3645 /* We will attempt to find function descriptors in the .opd segment,
3646 but if we can't we'll construct them ourselves. That being the
004d836a 3647 case, we'll need to reserve space on the stack for them. */
64a5b29c
KB
3648 funcdescaddr = sp - nfuncargs * 16;
3649 funcdescaddr &= ~0xfLL;
3650
3651 /* Adjust the stack pointer to it's new value. The calling conventions
3652 require us to have 16 bytes of scratch, plus whatever space is
004d836a 3653 necessary for the memory slots and our function descriptors. */
64a5b29c 3654 sp = sp - 16 - (memslots + nfuncargs) * 8;
004d836a 3655 sp &= ~0xfLL; /* Maintain 16 byte alignment. */
16461d7d 3656
64a5b29c
KB
3657 /* Place the arguments where they belong. The arguments will be
3658 either placed in the RSE backing store or on the memory stack.
3659 In addition, floating point arguments or HFAs are placed in
004d836a 3660 floating point registers. */
16461d7d
KB
3661 slotnum = 0;
3662 floatreg = IA64_FR8_REGNUM;
3663 for (argno = 0; argno < nargs; argno++)
3664 {
64a5b29c
KB
3665 struct type *float_elt_type;
3666
16461d7d 3667 arg = args[argno];
4991999e 3668 type = check_typedef (value_type (arg));
16461d7d 3669 len = TYPE_LENGTH (type);
64a5b29c 3670
004d836a 3671 /* Special handling for function parameters. */
64a5b29c
KB
3672 if (len == 8
3673 && TYPE_CODE (type) == TYPE_CODE_PTR
3674 && TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_FUNC)
3675 {
3676 char val_buf[8];
e17a4113
UW
3677 ULONGEST faddr = extract_unsigned_integer (value_contents (arg),
3678 8, byte_order);
3679 store_unsigned_integer (val_buf, 8, byte_order,
9c9acae0 3680 find_func_descr (regcache, faddr,
fbd9dcd3 3681 &funcdescaddr));
64a5b29c
KB
3682 if (slotnum < rseslots)
3683 write_memory (rse_address_add (bsp, slotnum), val_buf, 8);
3684 else
3685 write_memory (sp + 16 + 8 * (slotnum - rseslots), val_buf, 8);
3686 slotnum++;
3687 continue;
3688 }
3689
004d836a 3690 /* Normal slots. */
98f96ba1
KB
3691
3692 /* Skip odd slot if necessary... */
3693 if ((slotnum & 1) && slot_alignment_is_next_even (type))
16461d7d 3694 slotnum++;
98f96ba1 3695
16461d7d
KB
3696 argoffset = 0;
3697 while (len > 0)
3698 {
3699 char val_buf[8];
3700
3701 memset (val_buf, 0, 8);
1777feb0
MS
3702 memcpy (val_buf, value_contents (arg) + argoffset,
3703 (len > 8) ? 8 : len);
16461d7d
KB
3704
3705 if (slotnum < rseslots)
3706 write_memory (rse_address_add (bsp, slotnum), val_buf, 8);
3707 else
3708 write_memory (sp + 16 + 8 * (slotnum - rseslots), val_buf, 8);
3709
3710 argoffset += 8;
3711 len -= 8;
3712 slotnum++;
3713 }
64a5b29c 3714
004d836a 3715 /* Handle floating point types (including HFAs). */
64a5b29c
KB
3716 float_elt_type = is_float_or_hfa_type (type);
3717 if (float_elt_type != NULL)
3718 {
3719 argoffset = 0;
3720 len = TYPE_LENGTH (type);
3721 while (len > 0 && floatreg < IA64_FR16_REGNUM)
3722 {
004d836a 3723 char to[MAX_REGISTER_SIZE];
1777feb0
MS
3724 convert_typed_floating (value_contents (arg) + argoffset,
3725 float_elt_type, to,
3726 ia64_ext_type (gdbarch));
004d836a 3727 regcache_cooked_write (regcache, floatreg, (void *)to);
64a5b29c
KB
3728 floatreg++;
3729 argoffset += TYPE_LENGTH (float_elt_type);
3730 len -= TYPE_LENGTH (float_elt_type);
3731 }
16461d7d
KB
3732 }
3733 }
3734
004d836a 3735 /* Store the struct return value in r8 if necessary. */
16461d7d
KB
3736 if (struct_return)
3737 {
1777feb0
MS
3738 regcache_cooked_write_unsigned (regcache, IA64_GR8_REGNUM,
3739 (ULONGEST) struct_addr);
16461d7d
KB
3740 }
3741
e17a4113 3742 global_pointer = ia64_find_global_pointer (gdbarch, func_addr);
8dd5115e 3743
004d836a 3744 if (global_pointer != 0)
9c9acae0 3745 regcache_cooked_write_unsigned (regcache, IA64_GR1_REGNUM, global_pointer);
a59fe496 3746
9c9acae0 3747 regcache_cooked_write_unsigned (regcache, IA64_BR0_REGNUM, bp_addr);
16461d7d 3748
9c9acae0 3749 regcache_cooked_write_unsigned (regcache, sp_regnum, sp);
16461d7d
KB
3750
3751 return sp;
3752}
3753
004d836a 3754static struct frame_id
15c1e57f 3755ia64_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
16461d7d 3756{
e17a4113 3757 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
004d836a 3758 char buf[8];
4afcc598 3759 CORE_ADDR sp, bsp;
004d836a 3760
15c1e57f 3761 get_frame_register (this_frame, sp_regnum, buf);
e17a4113 3762 sp = extract_unsigned_integer (buf, 8, byte_order);
004d836a 3763
15c1e57f 3764 get_frame_register (this_frame, IA64_BSP_REGNUM, buf);
e17a4113 3765 bsp = extract_unsigned_integer (buf, 8, byte_order);
4afcc598
JJ
3766
3767 if (gdbarch_debug >= 1)
3768 fprintf_unfiltered (gdb_stdlog,
5af949e3
UW
3769 "dummy frame id: code %s, stack %s, special %s\n",
3770 paddress (gdbarch, get_frame_pc (this_frame)),
3771 paddress (gdbarch, sp), paddress (gdbarch, bsp));
4afcc598 3772
15c1e57f 3773 return frame_id_build_special (sp, get_frame_pc (this_frame), bsp);
16461d7d
KB
3774}
3775
004d836a
JJ
3776static CORE_ADDR
3777ia64_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
16461d7d 3778{
e17a4113 3779 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
004d836a
JJ
3780 char buf[8];
3781 CORE_ADDR ip, psr, pc;
3782
3783 frame_unwind_register (next_frame, IA64_IP_REGNUM, buf);
e17a4113 3784 ip = extract_unsigned_integer (buf, 8, byte_order);
004d836a 3785 frame_unwind_register (next_frame, IA64_PSR_REGNUM, buf);
e17a4113 3786 psr = extract_unsigned_integer (buf, 8, byte_order);
004d836a
JJ
3787
3788 pc = (ip & ~0xf) | ((psr >> 41) & 3);
3789 return pc;
16461d7d
KB
3790}
3791
6926787d
AS
3792static int
3793ia64_print_insn (bfd_vma memaddr, struct disassemble_info *info)
3794{
3795 info->bytes_per_line = SLOT_MULTIPLIER;
3796 return print_insn_ia64 (memaddr, info);
3797}
3798
16461d7d
KB
3799static struct gdbarch *
3800ia64_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
3801{
3802 struct gdbarch *gdbarch;
244bc108 3803 struct gdbarch_tdep *tdep;
244bc108 3804
85bf2b91
JJ
3805 /* If there is already a candidate, use it. */
3806 arches = gdbarch_list_lookup_by_info (arches, &info);
3807 if (arches != NULL)
3808 return arches->gdbarch;
16461d7d 3809
924d6a4f 3810 tdep = xzalloc (sizeof (struct gdbarch_tdep));
244bc108 3811 gdbarch = gdbarch_alloc (&info, tdep);
244bc108 3812
5439edaa
AC
3813 /* According to the ia64 specs, instructions that store long double
3814 floats in memory use a long-double format different than that
3815 used in the floating registers. The memory format matches the
3816 x86 extended float format which is 80 bits. An OS may choose to
3817 use this format (e.g. GNU/Linux) or choose to use a different
3818 format for storing long doubles (e.g. HPUX). In the latter case,
3819 the setting of the format may be moved/overridden in an
3820 OS-specific tdep file. */
8da61cc4 3821 set_gdbarch_long_double_format (gdbarch, floatformats_i387_ext);
32edc941 3822
16461d7d
KB
3823 set_gdbarch_short_bit (gdbarch, 16);
3824 set_gdbarch_int_bit (gdbarch, 32);
3825 set_gdbarch_long_bit (gdbarch, 64);
3826 set_gdbarch_long_long_bit (gdbarch, 64);
3827 set_gdbarch_float_bit (gdbarch, 32);
3828 set_gdbarch_double_bit (gdbarch, 64);
33c08150 3829 set_gdbarch_long_double_bit (gdbarch, 128);
16461d7d
KB
3830 set_gdbarch_ptr_bit (gdbarch, 64);
3831
004d836a 3832 set_gdbarch_num_regs (gdbarch, NUM_IA64_RAW_REGS);
1777feb0
MS
3833 set_gdbarch_num_pseudo_regs (gdbarch,
3834 LAST_PSEUDO_REGNUM - FIRST_PSEUDO_REGNUM);
16461d7d 3835 set_gdbarch_sp_regnum (gdbarch, sp_regnum);
698cb3f0 3836 set_gdbarch_fp0_regnum (gdbarch, IA64_FR0_REGNUM);
16461d7d
KB
3837
3838 set_gdbarch_register_name (gdbarch, ia64_register_name);
004d836a 3839 set_gdbarch_register_type (gdbarch, ia64_register_type);
16461d7d 3840
004d836a
JJ
3841 set_gdbarch_pseudo_register_read (gdbarch, ia64_pseudo_register_read);
3842 set_gdbarch_pseudo_register_write (gdbarch, ia64_pseudo_register_write);
3843 set_gdbarch_dwarf2_reg_to_regnum (gdbarch, ia64_dwarf_reg_to_regnum);
3844 set_gdbarch_register_reggroup_p (gdbarch, ia64_register_reggroup_p);
3845 set_gdbarch_convert_register_p (gdbarch, ia64_convert_register_p);
3846 set_gdbarch_register_to_value (gdbarch, ia64_register_to_value);
3847 set_gdbarch_value_to_register (gdbarch, ia64_value_to_register);
16461d7d 3848
004d836a 3849 set_gdbarch_skip_prologue (gdbarch, ia64_skip_prologue);
16461d7d 3850
4c8b6ae0 3851 set_gdbarch_return_value (gdbarch, ia64_return_value);
16461d7d 3852
1777feb0
MS
3853 set_gdbarch_memory_insert_breakpoint (gdbarch,
3854 ia64_memory_insert_breakpoint);
3855 set_gdbarch_memory_remove_breakpoint (gdbarch,
3856 ia64_memory_remove_breakpoint);
16461d7d
KB
3857 set_gdbarch_breakpoint_from_pc (gdbarch, ia64_breakpoint_from_pc);
3858 set_gdbarch_read_pc (gdbarch, ia64_read_pc);
b33e8514 3859 set_gdbarch_write_pc (gdbarch, ia64_write_pc);
16461d7d
KB
3860
3861 /* Settings for calling functions in the inferior. */
8dd5115e 3862 set_gdbarch_push_dummy_call (gdbarch, ia64_push_dummy_call);
004d836a 3863 set_gdbarch_frame_align (gdbarch, ia64_frame_align);
15c1e57f 3864 set_gdbarch_dummy_id (gdbarch, ia64_dummy_id);
16461d7d 3865
004d836a 3866 set_gdbarch_unwind_pc (gdbarch, ia64_unwind_pc);
968d1cb4 3867#ifdef HAVE_LIBUNWIND_IA64_H
15c1e57f
JB
3868 frame_unwind_append_unwinder (gdbarch,
3869 &ia64_libunwind_sigtramp_frame_unwind);
3870 frame_unwind_append_unwinder (gdbarch, &ia64_libunwind_frame_unwind);
3871 frame_unwind_append_unwinder (gdbarch, &ia64_sigtramp_frame_unwind);
968d1cb4 3872 libunwind_frame_set_descr (gdbarch, &ia64_libunwind_descr);
c5a27d9c 3873#else
15c1e57f 3874 frame_unwind_append_unwinder (gdbarch, &ia64_sigtramp_frame_unwind);
968d1cb4 3875#endif
15c1e57f 3876 frame_unwind_append_unwinder (gdbarch, &ia64_frame_unwind);
004d836a 3877 frame_base_set_default (gdbarch, &ia64_frame_base);
16461d7d
KB
3878
3879 /* Settings that should be unnecessary. */
3880 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
3881
6926787d 3882 set_gdbarch_print_insn (gdbarch, ia64_print_insn);
1777feb0
MS
3883 set_gdbarch_convert_from_func_ptr_addr (gdbarch,
3884 ia64_convert_from_func_ptr_addr);
6926787d 3885
0d5de010
DJ
3886 /* The virtual table contains 16-byte descriptors, not pointers to
3887 descriptors. */
3888 set_gdbarch_vtable_function_descriptors (gdbarch, 1);
3889
b33e8514
AS
3890 /* Hook in ABI-specific overrides, if they have been registered. */
3891 gdbarch_init_osabi (info, gdbarch);
3892
16461d7d
KB
3893 return gdbarch;
3894}
3895
a78f21af
AC
3896extern initialize_file_ftype _initialize_ia64_tdep; /* -Wmissing-prototypes */
3897
16461d7d
KB
3898void
3899_initialize_ia64_tdep (void)
3900{
b33e8514 3901 gdbarch_register (bfd_arch_ia64, ia64_gdbarch_init, NULL);
16461d7d 3902}
This page took 1.147466 seconds and 4 git commands to generate.