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