-
Notifications
You must be signed in to change notification settings - Fork 274
/
unwind.c
3129 lines (2734 loc) · 82.1 KB
/
unwind.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 1999-2002 Hewlett-Packard Co
* David Mosberger-Tang <[email protected]>
*/
/*
* unwind.c
*
* Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2009, 2010, 2012 David Anderson
* Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2009, 2010, 2012 Red Hat, Inc. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* Adapted from:
*
* arch/ia64/kernel/unwind.c (kernel-2.4.18-6.23)
*/
#ifdef IA64
/*
* WARNING: unw_frame_info, pt_regs and switch_stack have been
* copied to unwind.h, under the UNWIND_V[123] sections; this is
* done to rectify the need for this user-land code to use the same
* data structures that the target kernel is using.
*
* Basically it's a juggling match to keep the unw_frame_info,
* switch_stack and pt_regs structures in a "known" state -- as defined by
* the UNWIND_V[123] definitions used in the unwind.h header file -- and
* then passed to the 3 compile lines of unwind.c to create the three
* unwind_v[123].o object files.
*/
/*
* 2004-09-14 J. Nomura Added OS_INIT handling
*/
/* #include <asm/ptrace.h> can't include this -- it's changing over time! */
#include "defs.h"
#include "xen_hyper_defs.h"
typedef unsigned char u8;
typedef unsigned long long u64;
#undef PAGE_SIZE
#define PAGE_SIZE PAGESIZE()
#define GATE_ADDR (0xa000000000000000 + PAGE_SIZE)
#define CLEAR_SCRIPT_CACHE (TRUE)
#define _ASM_IA64_FPU_H
#include "unwind.h"
#include "unwind_i.h"
#include "rse.h"
static struct unw_reg_state *alloc_reg_state(void);
static void free_reg_state(struct unw_reg_state *);
static void rse_function_params(struct bt_info *bt, struct unw_frame_info *, char *);
static int load_unw_table(int);
static void verify_unw_member(char *, long);
static void verify_common_struct(char *, long);
static void dump_unwind_table(struct unw_table *);
static int unw_init_from_blocked_task(struct unw_frame_info *,
struct bt_info *);
static void unw_init_from_interruption(struct unw_frame_info *,
struct bt_info *, ulong, ulong);
static int unw_switch_from_osinit_v1(struct unw_frame_info *,
struct bt_info *);
static int unw_switch_from_osinit_v2(struct unw_frame_info *,
struct bt_info *);
static int unw_switch_from_osinit_v3(struct unw_frame_info *,
struct bt_info *, char *);
static unsigned long get_init_stack_ulong(unsigned long addr);
static void unw_init_frame_info(struct unw_frame_info *,
struct bt_info *, ulong);
static int find_save_locs(struct unw_frame_info *);
static int unw_unwind(struct unw_frame_info *);
static void run_script(struct unw_script *, struct unw_frame_info *);
static struct unw_script *script_lookup(struct unw_frame_info *);
static struct unw_script *script_new(unsigned long);
static void script_finalize(struct unw_script *, struct unw_state_record *);
static void script_emit(struct unw_script *, struct unw_insn);
static void emit_nat_info(struct unw_state_record *, int, struct unw_script *);
static struct unw_script *build_script(struct unw_frame_info *);
static struct unw_table_entry *lookup(struct unw_table *, unsigned long);
static void compile_reg(struct unw_state_record *, int, struct unw_script *);
static void compile_reg_v2(struct unw_state_record *, int, struct unw_script *);
#define UNW_LOG_CACHE_SIZE 7 /* each unw_script is ~256 bytes in size */
#define UNW_CACHE_SIZE (1 << UNW_LOG_CACHE_SIZE)
#define UNW_LOG_HASH_SIZE (UNW_LOG_CACHE_SIZE + 1)
#define UNW_HASH_SIZE (1 << UNW_LOG_HASH_SIZE)
#define UNW_DEBUG 0
#define UNW_STATS 0
#define p5 5
#define pNonSys p5 /* complement of pSys */
# define STAT(x...)
#define struct_offset(str,fld) ((char *)&((str *)NULL)->fld - (char *) 0)
#undef offsetof
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
/*
* Local snapshot of kernel's "unw" table, minus the spinlock_t and anything
* after the kernel_table. This allows the unmodified porting of the kernel
* code pieces that reference "unw.xxx" directly.
*
* The 2.6 kernel introduced a new pt_regs_offsets[32] array positioned in
* between the preg_index array and the kernel_table members.
*/
#ifdef REDHAT
static struct unw {
#else
static struct {
spinlock_t lock; /* spinlock for unwind data */
#endif /* !REDHAT */
/* list of unwind tables (one per load-module) */
struct unw_table *tables;
/* table of registers that prologues can save
(and order in which they're saved): */
unsigned char save_order[8];
/* maps a preserved register index (preg_index) to corresponding
switch_stack offset: */
unsigned short sw_off[sizeof(struct unw_frame_info) / 8];
unsigned short lru_head; /* index of lead-recently used script */
unsigned short lru_tail; /* index of most-recently used script */
/* index into unw_frame_info for preserved register i */
unsigned short preg_index[UNW_NUM_REGS];
/* unwind table for the kernel: */
struct unw_table kernel_table;
#ifndef REDHAT
/* unwind table describing the gate page (kernel code that is mapped
into user space): */
size_t gate_table_size;
unsigned long *gate_table;
/* hash table that maps instruction pointer to script index: */
unsigned short hash[UNW_HASH_SIZE];
/* script cache: */
struct unw_script cache[UNW_CACHE_SIZE];
# if UNW_DEBUG
const char *preg_name[UNW_NUM_REGS];
# endif
# if UNW_STATS
struct {
struct {
int lookups;
int hinted_hits;
int normal_hits;
int collision_chain_traversals;
} cache;
struct {
unsigned long build_time;
unsigned long run_time;
unsigned long parse_time;
int builds;
int news;
int collisions;
int runs;
} script;
struct {
unsigned long init_time;
unsigned long unwind_time;
int inits;
int unwinds;
} api;
} stat;
# endif
#endif /* !REDHAT */
} unw = { 0 };
static short pt_regs_offsets[32] = { 0 };
static struct unw_reg_state *
alloc_reg_state(void)
{
return((struct unw_reg_state *) GETBUF(sizeof(struct unw_reg_state)));
}
static void
free_reg_state(struct unw_reg_state *rs)
{
FREEBUF(rs);
}
static struct unw_labeled_state *
alloc_labeled_state(void)
{
return((struct unw_labeled_state *)
GETBUF(sizeof(struct unw_labeled_state)));
}
static void
free_labeled_state(struct unw_labeled_state *ls)
{
FREEBUF(ls);
}
typedef unsigned long unw_word;
/* Unwind accessors. */
static inline unsigned long
pt_regs_off_v2 (unsigned long reg)
{
short off = -1;
if (reg < 32)
off = pt_regs_offsets[reg];
if (off < 0) {
if (reg > 0)
error(INFO, "unwind: bad scratch reg r%lu\n", reg);
off = 0;
}
return (unsigned long) off;
}
/*
* Returns offset of rREG in struct pt_regs.
*/
static inline unsigned long
pt_regs_off (unsigned long reg)
{
unsigned long off =0;
if (machdep->flags & UNW_PTREGS)
return pt_regs_off_v2(reg);
if (reg >= 1 && reg <= 3)
off = struct_offset(struct pt_regs, r1) + 8*(reg - 1);
else if (reg <= 11)
off = struct_offset(struct pt_regs, r8) + 8*(reg - 8);
else if (reg <= 15)
off = struct_offset(struct pt_regs, r12) + 8*(reg - 12);
else if (reg <= 31)
off = struct_offset(struct pt_regs, r16) + 8*(reg - 16);
else if (reg > 0)
error(INFO, "unwind: bad scratch reg r%lu\n", reg);
return off;
}
#ifdef UNWIND_V1
static inline struct pt_regs *
get_scratch_regs (struct unw_frame_info *info)
{
struct pt_regs *pt_unused = NULL;
error(INFO, "get_scratch_regs: should not be here!\n");
return pt_unused;
}
#endif
#ifdef UNWIND_V2
static inline struct pt_regs *
get_scratch_regs (struct unw_frame_info *info)
{
if (!info->pt) {
/* This should not happen with valid unwind info. */
error(INFO,
"get_scratch_regs: bad unwind info: resetting info->pt\n");
if (info->flags & UNW_FLAG_INTERRUPT_FRAME)
info->pt = (unsigned long)((struct pt_regs *)
info->psp - 1);
else
info->pt = info->sp - 16;
}
return (struct pt_regs *) info->pt;
}
#endif
#ifdef UNWIND_V3
static inline struct pt_regs *
get_scratch_regs (struct unw_frame_info *info)
{
if (!info->pt) {
/* This should not happen with valid unwind info. */
error(INFO,
"get_scratch_regs: bad unwind info: resetting info->pt\n");
if (info->flags & UNW_FLAG_INTERRUPT_FRAME)
info->pt = (unsigned long)((struct pt_regs *)
info->psp - 1);
else
info->pt = info->sp - 16;
}
return (struct pt_regs *) info->pt;
}
#endif
int
#ifdef UNWIND_V1
unw_access_gr_v1 (struct unw_frame_info *info, int regnum, unsigned long *val, char *nat, int write)
#endif
#ifdef UNWIND_V2
unw_access_gr_v2 (struct unw_frame_info *info, int regnum, unsigned long *val, char *nat, int write)
#endif
#ifdef UNWIND_V3
unw_access_gr_v3 (struct unw_frame_info *info, int regnum, unsigned long *val, char *nat, int write)
#endif
{
unsigned long *addr, *nat_addr, nat_mask = 0, dummy_nat;
struct unw_ireg *ireg;
struct pt_regs *pt;
struct bt_info *bt = (struct bt_info *)info->task;
if ((unsigned) regnum - 1 >= 127) {
error(INFO, "unwind: trying to access non-existent r%u\n",
regnum);
return -1;
}
if (regnum < 32) {
if (regnum >= 4 && regnum <= 7) {
/* access a preserved register */
ireg = &info->r4 + (regnum - 4);
addr = ireg->loc;
if (addr) {
nat_addr = addr + ireg->nat.off;
switch (ireg->nat.type) {
case UNW_NAT_VAL:
/* simulate getf.sig/setf.sig */
if (write) {
if (*nat) {
/* write NaTVal and be done with it */
addr[0] = 0;
addr[1] = 0x1fffe;
return 0;
}
addr[1] = 0x1003e;
} else {
if (addr[0] == 0 && addr[1] == 0x1ffe) {
/* return NaT and be done with it */
*val = 0;
*nat = 1;
return 0;
}
}
/* fall through */
case UNW_NAT_NONE:
dummy_nat = 0;
nat_addr = &dummy_nat;
break;
case UNW_NAT_MEMSTK:
nat_mask = (1UL << ((long) addr & 0x1f8)/8);
break;
case UNW_NAT_REGSTK:
nat_addr = ia64_rse_rnat_addr(addr);
if ((unsigned long) addr < info->regstk.limit
|| (unsigned long) addr >= info->regstk.top)
{
error(INFO,
"unwind: %p outside of regstk "
"[0x%lx-0x%lx)\n", (void *) addr,
info->regstk.limit,
info->regstk.top);
return -1;
}
if ((unsigned long) nat_addr >= info->regstk.top)
nat_addr = &info->sw->ar_rnat;
nat_mask = (1UL << ia64_rse_slot_num(addr));
break;
}
} else {
addr = &info->sw->r4 + (regnum - 4);
nat_addr = &info->sw->ar_unat;
nat_mask = (1UL << ((long) addr & 0x1f8)/8);
}
} else {
/* access a scratch register */
if (machdep->flags & UNW_PTREGS) {
pt = get_scratch_regs(info);
addr = (unsigned long *) ((unsigned long)pt + pt_regs_off(regnum));
} else {
if (info->flags & UNW_FLAG_INTERRUPT_FRAME)
pt = (struct pt_regs *) info->psp - 1;
else
pt = (struct pt_regs *) info->sp - 1;
addr = (unsigned long *) ((long) pt + pt_regs_off(regnum));
}
if (info->pri_unat_loc)
nat_addr = info->pri_unat_loc;
else
nat_addr = &info->sw->ar_unat;
nat_mask = (1UL << ((long) addr & 0x1f8)/8);
}
} else {
/* access a stacked register */
addr = ia64_rse_skip_regs((unsigned long *) info->bsp, regnum - 32);
nat_addr = ia64_rse_rnat_addr(addr);
if ((unsigned long) addr < info->regstk.limit
|| (unsigned long) addr >= info->regstk.top)
{
error(INFO, "unwind: ignoring attempt to access register outside of rbs\n");
return -1;
}
if ((unsigned long) nat_addr >= info->regstk.top)
nat_addr = &info->sw->ar_rnat;
nat_mask = (1UL << ia64_rse_slot_num(addr));
}
if (write) {
*addr = *val;
if (*nat)
*nat_addr |= nat_mask;
else
*nat_addr &= ~nat_mask;
} else {
if ((IA64_GET_STACK_ULONG(nat_addr) & nat_mask) == 0) {
*val = IA64_GET_STACK_ULONG(addr);
*nat = 0;
} else {
*val = 0; /* if register is a NaT, *addr may contain kernel data! */
*nat = 1;
}
}
return 0;
}
int
#ifdef UNWIND_V1
unw_access_br_v1 (struct unw_frame_info *info, int regnum, unsigned long *val, int write)
#endif
#ifdef UNWIND_V2
unw_access_br_v2 (struct unw_frame_info *info, int regnum, unsigned long *val, int write)
#endif
#ifdef UNWIND_V3
unw_access_br_v3 (struct unw_frame_info *info, int regnum, unsigned long *val, int write)
#endif
{
unsigned long *addr;
struct pt_regs *pt;
struct bt_info *bt = (struct bt_info *)info->task;
if (info->flags & UNW_FLAG_INTERRUPT_FRAME)
pt = (struct pt_regs *) info->psp - 1;
else
pt = (struct pt_regs *) info->sp - 1;
switch (regnum) {
/* scratch: */
case 0: addr = &pt->b0; break;
case 6: addr = &pt->b6; break;
case 7: addr = &pt->b7; break;
/* preserved: */
case 1: case 2: case 3: case 4: case 5:
addr = *(&info->b1_loc + (regnum - 1));
if (!addr)
addr = &info->sw->b1 + (regnum - 1);
break;
default:
error(INFO, "unwind: trying to access non-existent b%u\n",
regnum);
return -1;
}
if (write)
*addr = *val;
else
*val = IA64_GET_STACK_ULONG(addr);
return 0;
}
#ifdef UNWIND_V1
int
unw_access_fr_v1 (struct unw_frame_info *info, int regnum, struct ia64_fpreg *val, int write)
{
struct ia64_fpreg *addr = 0;
struct pt_regs *pt;
struct bt_info *bt = (struct bt_info *)info->task;
if ((unsigned) (regnum - 2) >= 126) {
error(INFO, "unwind: trying to access non-existent f%u\n",
regnum);
return -1;
}
if (info->flags & UNW_FLAG_INTERRUPT_FRAME)
pt = (struct pt_regs *) info->psp - 1;
else
pt = (struct pt_regs *) info->sp - 1;
if (regnum <= 5) {
addr = *(&info->f2_loc + (regnum - 2));
if (!addr)
addr = &info->sw->f2 + (regnum - 2);
} else if (regnum <= 15) {
if (regnum <= 9)
addr = &pt->f6 + (regnum - 6);
else
addr = &info->sw->f10 + (regnum - 10);
} else if (regnum <= 31) {
addr = info->fr_loc[regnum - 16];
if (!addr)
addr = &info->sw->f16 + (regnum - 16);
} else {
#ifdef REDHAT
struct bt_info *bt = (struct bt_info *)info->task;
addr = (struct ia64_fpreg *)
(bt->task + OFFSET(task_struct_thread) +
OFFSET(thread_struct_fph) +
((regnum - 32) * sizeof(struct ia64_fpreg)));
#else
struct task_struct *t = info->task;
if (write)
ia64_sync_fph(t);
else
ia64_flush_fph(t);
addr = t->thread.fph + (regnum - 32);
#endif
}
if (write)
*addr = *val;
else
GET_STACK_DATA(addr, val, sizeof(struct ia64_fpreg));
return 0;
}
#endif
#ifdef UNWIND_V2
int
unw_access_fr_v2 (struct unw_frame_info *info, int regnum, struct ia64_fpreg *val, int write)
{
struct ia64_fpreg *addr = 0;
struct pt_regs *pt;
struct bt_info *bt = (struct bt_info *)info->task;
if ((unsigned) (regnum - 2) >= 126) {
error(INFO, "unwind: trying to access non-existent f%u\n",
regnum);
return -1;
}
if (regnum <= 5) {
addr = *(&info->f2_loc + (regnum - 2));
if (!addr)
addr = &info->sw->f2 + (regnum - 2);
} else if (regnum <= 15) {
if (regnum <= 11) {
pt = get_scratch_regs(info);
addr = &pt->f6 + (regnum - 6);
}
else
addr = &info->sw->f12 + (regnum - 12);
} else if (regnum <= 31) {
addr = info->fr_loc[regnum - 16];
if (!addr)
addr = &info->sw->f16 + (regnum - 16);
} else {
#ifdef REDHAT
struct bt_info *bt = (struct bt_info *)info->task;
addr = (struct ia64_fpreg *)
(bt->task + OFFSET(task_struct_thread) +
OFFSET(thread_struct_fph) +
((regnum - 32) * sizeof(struct ia64_fpreg)));
#else
struct task_struct *t = info->task;
if (write)
ia64_sync_fph(t);
else
ia64_flush_fph(t);
addr = t->thread.fph + (regnum - 32);
#endif
}
if (write)
*addr = *val;
else
GET_STACK_DATA(addr, val, sizeof(struct ia64_fpreg));
return 0;
}
#endif
#ifdef UNWIND_V3
int
unw_access_fr_v3 (struct unw_frame_info *info, int regnum, struct ia64_fpreg *val, int write)
{
struct ia64_fpreg *addr = 0;
struct pt_regs *pt;
struct bt_info *bt = (struct bt_info *)info->task;
if ((unsigned) (regnum - 2) >= 126) {
error(INFO, "unwind: trying to access non-existent f%u\n",
regnum);
return -1;
}
if (regnum <= 5) {
addr = *(&info->f2_loc + (regnum - 2));
if (!addr)
addr = &info->sw->f2 + (regnum - 2);
} else if (regnum <= 15) {
if (regnum <= 11) {
pt = get_scratch_regs(info);
addr = &pt->f6 + (regnum - 6);
}
else
addr = &info->sw->f12 + (regnum - 12);
} else if (regnum <= 31) {
addr = info->fr_loc[regnum - 16];
if (!addr)
addr = &info->sw->f16 + (regnum - 16);
} else {
#ifdef REDHAT
struct bt_info *bt = (struct bt_info *)info->task;
addr = (struct ia64_fpreg *)
(bt->task + OFFSET(task_struct_thread) +
OFFSET(thread_struct_fph) +
((regnum - 32) * sizeof(struct ia64_fpreg)));
#else
struct task_struct *t = info->task;
if (write)
ia64_sync_fph(t);
else
ia64_flush_fph(t);
addr = t->thread.fph + (regnum - 32);
#endif
}
if (write)
*addr = *val;
else
GET_STACK_DATA(addr, val, sizeof(struct ia64_fpreg));
return 0;
}
#endif
int
#ifdef UNWIND_V1
unw_access_ar_v1 (struct unw_frame_info *info, int regnum, unsigned long *val, int write)
#endif
#ifdef UNWIND_V2
unw_access_ar_v2 (struct unw_frame_info *info, int regnum, unsigned long *val, int write)
#endif
#ifdef UNWIND_V3
unw_access_ar_v3 (struct unw_frame_info *info, int regnum, unsigned long *val, int write)
#endif
{
unsigned long *addr;
struct pt_regs *pt;
struct bt_info *bt = (struct bt_info *)info->task;
if (info->flags & UNW_FLAG_INTERRUPT_FRAME)
pt = (struct pt_regs *) info->psp - 1;
else
pt = (struct pt_regs *) info->sp - 1;
switch (regnum) {
case UNW_AR_BSP:
addr = info->bsp_loc;
if (!addr)
addr = &info->sw->ar_bspstore;
break;
case UNW_AR_BSPSTORE:
addr = info->bspstore_loc;
if (!addr)
addr = &info->sw->ar_bspstore;
break;
case UNW_AR_PFS:
addr = info->pfs_loc;
if (!addr)
addr = &info->sw->ar_pfs;
break;
case UNW_AR_RNAT:
addr = info->rnat_loc;
if (!addr)
addr = &info->sw->ar_rnat;
break;
case UNW_AR_UNAT:
addr = info->unat_loc;
if (!addr)
addr = &info->sw->ar_unat;
break;
case UNW_AR_LC:
addr = info->lc_loc;
if (!addr)
addr = &info->sw->ar_lc;
break;
case UNW_AR_EC:
if (!info->cfm_loc)
return -1;
if (write)
*info->cfm_loc =
(*info->cfm_loc & ~(0x3fUL << 52)) | ((*val & 0x3f) << 52);
else
*val = (IA64_GET_STACK_ULONG(info->cfm_loc) >> 52) & 0x3f;
return 0;
case UNW_AR_FPSR:
addr = info->fpsr_loc;
if (!addr)
addr = &info->sw->ar_fpsr;
break;
case UNW_AR_RSC:
if (machdep->flags & UNW_PTREGS)
pt = get_scratch_regs(info);
addr = &pt->ar_rsc;
break;
case UNW_AR_CCV:
if (machdep->flags & UNW_PTREGS)
pt = get_scratch_regs(info);
addr = &pt->ar_ccv;
break;
#if defined(UNWIND_V3)
case UNW_AR_CSD:
if (machdep->flags & UNW_PTREGS)
pt = get_scratch_regs(info);
addr = &pt->ar_csd;
break;
case UNW_AR_SSD:
if (machdep->flags & UNW_PTREGS)
pt = get_scratch_regs(info);
addr = &pt->ar_ssd;
break;
#endif
default:
error(INFO, "unwind: trying to access non-existent ar%u\n",
regnum);
return -1;
}
if (write)
*addr = *val;
else
*val = IA64_GET_STACK_ULONG(addr);
return 0;
}
int
#ifdef UNWIND_V1
unw_access_pr_v1 (struct unw_frame_info *info, unsigned long *val, int write)
#endif
#ifdef UNWIND_V2
unw_access_pr_v2 (struct unw_frame_info *info, unsigned long *val, int write)
#endif
#ifdef UNWIND_V3
unw_access_pr_v3 (struct unw_frame_info *info, unsigned long *val, int write)
#endif
{
unsigned long *addr;
struct bt_info *bt = (struct bt_info *)info->task;
addr = info->pr_loc;
if (!addr)
addr = &info->sw->pr;
if (write)
*addr = *val;
else
*val = IA64_GET_STACK_ULONG(addr);
return 0;
}
/* Routines to manipulate the state stack. */
static inline void
push (struct unw_state_record *sr)
{
struct unw_reg_state *rs;
rs = alloc_reg_state();
if (!rs) {
error(INFO, "unwind: cannot stack reg state!\n");
return;
}
memcpy(rs, &sr->curr, sizeof(*rs));
sr->curr.next = rs;
}
static void
pop (struct unw_state_record *sr)
{
struct unw_reg_state *rs = sr->curr.next;
if (!rs) {
error(INFO, "unwind: stack underflow!\n");
return;
}
memcpy(&sr->curr, rs, sizeof(*rs));
free_reg_state(rs);
}
/* Make a copy of the state stack. Non-recursive to avoid stack overflows. */
static struct unw_reg_state *
dup_state_stack (struct unw_reg_state *rs)
{
struct unw_reg_state *copy, *prev = NULL, *first = NULL;
while (rs) {
copy = alloc_reg_state();
if (!copy) {
error(INFO, "unwind.dup_state_stack: out of memory\n");
return NULL;
}
memcpy(copy, rs, sizeof(*copy));
if (first)
prev->next = copy;
else
first = copy;
rs = rs->next;
prev = copy;
}
return first;
}
/* Free all stacked register states (but not RS itself). */
static void
free_state_stack (struct unw_reg_state *rs)
{
struct unw_reg_state *p, *next;
for (p = rs->next; p != NULL; p = next) {
next = p->next;
free_reg_state(p);
}
rs->next = NULL;
}
/* Routines to manipulate the state stack. */
static enum unw_register_index __attribute__((const))
decode_abreg (unsigned char abreg, int memory)
{
switch (abreg) {
case 0x04 ... 0x07: return UNW_REG_R4 + (abreg - 0x04);
case 0x22 ... 0x25: return UNW_REG_F2 + (abreg - 0x22);
case 0x30 ... 0x3f: return UNW_REG_F16 + (abreg - 0x30);
case 0x41 ... 0x45: return UNW_REG_B1 + (abreg - 0x41);
case 0x60: return UNW_REG_PR;
case 0x61: return UNW_REG_PSP;
case 0x62: return memory ? UNW_REG_PRI_UNAT_MEM : UNW_REG_PRI_UNAT_GR;
case 0x63: return UNW_REG_RP;
case 0x64: return UNW_REG_BSP;
case 0x65: return UNW_REG_BSPSTORE;
case 0x66: return UNW_REG_RNAT;
case 0x67: return UNW_REG_UNAT;
case 0x68: return UNW_REG_FPSR;
case 0x69: return UNW_REG_PFS;
case 0x6a: return UNW_REG_LC;
default:
break;
}
error(INFO, "unwind: bad abreg=0x%x\n", abreg);
return UNW_REG_LC;
}
static void
set_reg (struct unw_reg_info *reg, enum unw_where where, int when, unsigned long val)
{
reg->val = val;
reg->where = where;
if (reg->when == UNW_WHEN_NEVER)
reg->when = when;
}
static void
alloc_spill_area (unsigned long *offp, unsigned long regsize,
struct unw_reg_info *lo, struct unw_reg_info *hi)
{
struct unw_reg_info *reg;
for (reg = hi; reg >= lo; --reg) {
if (reg->where == UNW_WHERE_SPILL_HOME) {
reg->where = UNW_WHERE_PSPREL;
*offp -= regsize;
reg->val = *offp;
#ifndef KERNEL_FIX
reg->val = 0x10 - *offp;
*offp += regsize;
#endif
}
}
}
static inline void
spill_next_when (struct unw_reg_info **regp, struct unw_reg_info *lim, unw_word t)
{
struct unw_reg_info *reg;
for (reg = *regp; reg <= lim; ++reg) {
if (reg->where == UNW_WHERE_SPILL_HOME) {
reg->when = t;
*regp = reg + 1;
return;
}
}
error(INFO, "unwind: excess spill!\n");
}
static inline void
finish_prologue (struct unw_state_record *sr)
{
struct unw_reg_info *reg;
unsigned long off;
int i;
/*
* First, resolve implicit register save locations (see Section "11.4.2.3 Rules
* for Using Unwind Descriptors", rule 3):
*/
for (i = 0; i < (int) sizeof(unw.save_order)/sizeof(unw.save_order[0]); ++i) {
reg = sr->curr.reg + unw.save_order[i];
if (reg->where == UNW_WHERE_GR_SAVE) {
reg->where = UNW_WHERE_GR;
reg->val = sr->gr_save_loc++;
}
}
/*
* Next, compute when the fp, general, and branch registers get
* saved. This must come before alloc_spill_area() because
* we need to know which registers are spilled to their home
* locations.
*/
if (sr->imask) {
unsigned char kind, mask = 0, *cp = sr->imask;
unsigned long t;
static const unsigned char limit[3] = {
UNW_REG_F31, UNW_REG_R7, UNW_REG_B5
};
struct unw_reg_info *(regs[3]);
regs[0] = sr->curr.reg + UNW_REG_F2;
regs[1] = sr->curr.reg + UNW_REG_R4;
regs[2] = sr->curr.reg + UNW_REG_B1;
for (t = 0; t < sr->region_len; ++t) {
if ((t & 3) == 0)
mask = *cp++;
kind = (mask >> 2*(3-(t & 3))) & 3;
if (kind > 0)
spill_next_when(®s[kind - 1], sr->curr.reg + limit[kind - 1],
sr->region_start + t);
}
}
/*
* Next, lay out the memory stack spill area:
*/
if (sr->any_spills) {
off = sr->spill_offset;
alloc_spill_area(&off, 16, sr->curr.reg + UNW_REG_F2, sr->curr.reg + UNW_REG_F31);
alloc_spill_area(&off, 8, sr->curr.reg + UNW_REG_B1, sr->curr.reg + UNW_REG_B5);
alloc_spill_area(&off, 8, sr->curr.reg + UNW_REG_R4, sr->curr.reg + UNW_REG_R7);
}
}
/*
* Region header descriptors.
*/
static void
desc_prologue (int body, unw_word rlen, unsigned char mask, unsigned char grsave,
struct unw_state_record *sr)
{
int i;
if (!(sr->in_body || sr->first_region))
finish_prologue(sr);
sr->first_region = 0;