Home | History | Annotate | Line # | Download | only in unwind
stackframe.c revision 1.4
      1  1.4   cegger /*	$NetBSD: stackframe.c,v 1.4 2009/03/18 16:00:13 cegger Exp $	*/
      2  1.1   cherry 
      3  1.1   cherry /* Contributed to the NetBSD foundation by Cherry G. Mathew <cherry (at) mahiti.org>
      4  1.1   cherry  * This file contains routines to use decoded unwind descriptor entries
      5  1.1   cherry  * to build a stack configuration. The unwinder consults the stack
      6  1.1   cherry  * configuration to fetch registers used to unwind the frame.
      7  1.1   cherry  * References:
      8  1.1   cherry  *	[1] section. 11.4.2.6., Itanium Software Conventions and
      9  1.1   cherry  *			Runtime Architecture Guide.
     10  1.1   cherry  */
     11  1.1   cherry 
     12  1.1   cherry #include <sys/cdefs.h>
     13  1.1   cherry #include <sys/param.h>
     14  1.1   cherry #include <sys/systm.h>
     15  1.1   cherry 
     16  1.1   cherry 
     17  1.1   cherry #include <ia64/unwind/decode.h>
     18  1.1   cherry #include <ia64/unwind/stackframe.h>
     19  1.1   cherry 
     20  1.1   cherry //#define UNWIND_DIAGNOSTIC
     21  1.1   cherry 
     22  1.1   cherry /* Global variables:
     23  1.1   cherry    array of struct recordchain
     24  1.1   cherry    size of record chain array.
     25  1.1   cherry */
     26  1.1   cherry struct recordchain strc[MAXSTATERECS];
     27  1.1   cherry int rec_cnt = 0;
     28  1.1   cherry 
     29  1.1   cherry /* Build a recordchain of a region, given the pointer to unwind table
     30  1.1   cherry  * entry, and the number of entries to decode.
     31  1.1   cherry  */
     32  1.1   cherry 
     33  1.1   cherry void buildrecordchain(uint64_t unwind_infop, struct recordchain *xxx)
     34  1.1   cherry {
     35  1.1   cherry 
     36  1.1   cherry 
     37  1.1   cherry 	uint64_t unwindstart, unwindend;
     38  1.1   cherry 	uint64_t unwindlen;
     39  1.1   cherry 	uint64_t region_len = 0;
     40  1.3  thorpej 	bool region_type = false; /* Prologue */
     41  1.1   cherry 
     42  1.1   cherry 	struct unwind_hdr_t {
     43  1.1   cherry 		uint64_t uwh;
     44  1.1   cherry 	} *uwhp = (void *) unwind_infop;
     45  1.1   cherry 
     46  1.1   cherry 	char *nextrecp, *recptr = (char *) unwind_infop + sizeof(uint64_t);
     47  1.1   cherry 
     48  1.1   cherry 	unwindstart = (uint64_t) recptr;
     49  1.1   cherry 
     50  1.1   cherry 	if (UNW_VER(uwhp->uwh) != 1) {
     51  1.1   cherry 		printf("Wrong unwind version! \n");
     52  1.1   cherry 		return;
     53  1.1   cherry 	}
     54  1.1   cherry 
     55  1.1   cherry 	unwindlen = UNW_LENGTH(uwhp->uwh) * sizeof(uint64_t);
     56  1.1   cherry 	unwindend = unwindstart + unwindlen;
     57  1.1   cherry 
     58  1.1   cherry #ifdef UNWIND_DIAGNOSTIC
     59  1.1   cherry 	printf("recptr = %p \n", recptr);
     60  1.1   cherry 	printf("unwindlen = %lx \n", unwindlen);
     61  1.1   cherry 	printf("unwindend = %lx \n", unwindend);
     62  1.1   cherry #endif
     63  1.1   cherry 
     64  1.1   cherry 	/* XXX: Ignore zero length records. */
     65  1.1   cherry 
     66  1.1   cherry 
     67  1.1   cherry 	for(rec_cnt = 0; rec_cnt < MAXSTATERECS && (uint64_t)recptr < unwindend;
     68  1.1   cherry 	    rec_cnt++) {
     69  1.1   cherry 		if ((nextrecp = unwind_decode_R1(recptr, &strc[rec_cnt].udesc))){
     70  1.1   cherry 			region_len = strc[rec_cnt].udesc.R1.rlen;
     71  1.1   cherry 			region_type = strc[rec_cnt].udesc.R1.r;
     72  1.1   cherry 			strc[rec_cnt].type = R1;
     73  1.1   cherry 			recptr = nextrecp;
     74  1.1   cherry 			continue;
     75  1.1   cherry 		}
     76  1.1   cherry 
     77  1.1   cherry 		if ((nextrecp = unwind_decode_R2(recptr, &strc[rec_cnt].udesc))){
     78  1.1   cherry 			region_len = strc[rec_cnt].udesc.R2.rlen;
     79  1.3  thorpej 			region_type = false; /* R2 regions are prologue regions */
     80  1.1   cherry 			strc[rec_cnt].type = R2;
     81  1.1   cherry 			recptr = nextrecp;
     82  1.1   cherry 			continue;
     83  1.1   cherry 		}
     84  1.1   cherry 
     85  1.1   cherry 		if ((nextrecp = unwind_decode_R3(recptr, &strc[rec_cnt].udesc))){
     86  1.1   cherry 			region_len = strc[rec_cnt].udesc.R3.rlen;
     87  1.1   cherry 			region_type = strc[rec_cnt].udesc.R3.r;
     88  1.1   cherry 			strc[rec_cnt].type = R3;
     89  1.1   cherry 			recptr = nextrecp;
     90  1.1   cherry 			continue;
     91  1.1   cherry 		}
     92  1.1   cherry 
     93  1.3  thorpej 		if(region_type == false) { /* Prologue Region */
     94  1.1   cherry 			if ((nextrecp = unwind_decode_P1(recptr, &strc[rec_cnt].udesc))){
     95  1.1   cherry 				strc[rec_cnt].type = P1;
     96  1.1   cherry 				recptr = nextrecp;
     97  1.1   cherry 				continue;
     98  1.1   cherry 			}
     99  1.1   cherry 
    100  1.1   cherry 			if ((nextrecp = unwind_decode_P2(recptr, &strc[rec_cnt].udesc))){
    101  1.1   cherry 				strc[rec_cnt].type = P2;
    102  1.1   cherry 				recptr = nextrecp;
    103  1.1   cherry 				continue;
    104  1.1   cherry 			}
    105  1.1   cherry 
    106  1.1   cherry 			if ((nextrecp = unwind_decode_P3(recptr, &strc[rec_cnt].udesc))){
    107  1.1   cherry 				strc[rec_cnt].type = P3;
    108  1.1   cherry 				recptr = nextrecp;
    109  1.1   cherry 				continue;
    110  1.1   cherry 			}
    111  1.1   cherry 
    112  1.1   cherry 
    113  1.1   cherry 			if ((nextrecp = unwind_decode_P4(recptr, &strc[rec_cnt].udesc, region_len))){
    114  1.1   cherry 				strc[rec_cnt].type = P4;
    115  1.1   cherry 				recptr = nextrecp;
    116  1.1   cherry 				break;
    117  1.1   cherry 			}
    118  1.1   cherry 
    119  1.1   cherry 
    120  1.1   cherry 			if ((nextrecp = unwind_decode_P5(recptr, &strc[rec_cnt].udesc))){
    121  1.1   cherry 				strc[rec_cnt].type = P5;
    122  1.1   cherry 				recptr = nextrecp;
    123  1.1   cherry 				continue;
    124  1.1   cherry 			}
    125  1.1   cherry 
    126  1.1   cherry 			if ((nextrecp = unwind_decode_P6(recptr, &strc[rec_cnt].udesc))){
    127  1.1   cherry 				strc[rec_cnt].type = P6;
    128  1.1   cherry 				recptr = nextrecp;
    129  1.1   cherry 				continue;
    130  1.1   cherry 			}
    131  1.1   cherry 
    132  1.1   cherry 			if ((nextrecp = unwind_decode_P7(recptr, &strc[rec_cnt].udesc))){
    133  1.1   cherry 				strc[rec_cnt].type = P7;
    134  1.1   cherry 				recptr = nextrecp;
    135  1.1   cherry 				continue;
    136  1.1   cherry 			}
    137  1.1   cherry 
    138  1.1   cherry 			if ((nextrecp = unwind_decode_P8(recptr, &strc[rec_cnt].udesc))){
    139  1.1   cherry 				strc[rec_cnt].type = P8;
    140  1.1   cherry 				recptr = nextrecp;
    141  1.1   cherry 				continue;
    142  1.1   cherry 			}
    143  1.1   cherry 
    144  1.1   cherry 			if ((nextrecp = unwind_decode_P9(recptr, &strc[rec_cnt].udesc))){
    145  1.1   cherry 				strc[rec_cnt].type = P9;
    146  1.1   cherry 				recptr = nextrecp;
    147  1.1   cherry 				continue;
    148  1.1   cherry 			}
    149  1.1   cherry 
    150  1.1   cherry 			if ((nextrecp = unwind_decode_P10(recptr, &strc[rec_cnt].udesc))){
    151  1.1   cherry 				strc[rec_cnt].type = P10;
    152  1.1   cherry 				recptr = nextrecp;
    153  1.1   cherry 				continue;
    154  1.1   cherry 			}
    155  1.1   cherry 
    156  1.1   cherry 			printf("Skipping prologue desc slot :: %d \n", rec_cnt);
    157  1.1   cherry 		}
    158  1.1   cherry 
    159  1.1   cherry 		else {
    160  1.1   cherry 
    161  1.1   cherry 			if ((nextrecp = unwind_decode_B1(recptr, &strc[rec_cnt].udesc))){
    162  1.1   cherry 				strc[rec_cnt].type = B1;
    163  1.1   cherry 				recptr = nextrecp;
    164  1.1   cherry 				continue;
    165  1.1   cherry 			}
    166  1.1   cherry 
    167  1.1   cherry 			if ((nextrecp = unwind_decode_B2(recptr, &strc[rec_cnt].udesc))){
    168  1.1   cherry 				strc[rec_cnt].type = B2;
    169  1.1   cherry 				recptr = nextrecp;
    170  1.1   cherry 				continue;
    171  1.1   cherry 			}
    172  1.1   cherry 
    173  1.1   cherry 			if ((nextrecp = unwind_decode_B3(recptr, &strc[rec_cnt].udesc))){
    174  1.1   cherry 				strc[rec_cnt].type = B3;
    175  1.1   cherry 				recptr = nextrecp;
    176  1.1   cherry 				continue;
    177  1.1   cherry 			}
    178  1.1   cherry 
    179  1.1   cherry 			if ((nextrecp = unwind_decode_B4(recptr, &strc[rec_cnt].udesc))){
    180  1.1   cherry 				strc[rec_cnt].type = B4;
    181  1.1   cherry 				recptr = nextrecp;
    182  1.1   cherry 				continue;
    183  1.1   cherry 			}
    184  1.1   cherry 
    185  1.1   cherry 			if ((nextrecp = unwind_decode_X1(recptr, &strc[rec_cnt].udesc))){
    186  1.1   cherry 				strc[rec_cnt].type = X1;
    187  1.1   cherry 				recptr = nextrecp;
    188  1.1   cherry 				continue;
    189  1.1   cherry 			}
    190  1.1   cherry 
    191  1.1   cherry 			if ((nextrecp = unwind_decode_X2(recptr, &strc[rec_cnt].udesc))){
    192  1.1   cherry 				strc[rec_cnt].type = X2;
    193  1.1   cherry 				recptr = nextrecp;
    194  1.1   cherry 				continue;
    195  1.1   cherry 			}
    196  1.1   cherry 
    197  1.1   cherry 
    198  1.1   cherry 			if ((nextrecp = unwind_decode_X3(recptr, &strc[rec_cnt].udesc))){
    199  1.1   cherry 				strc[rec_cnt].type = X3;
    200  1.1   cherry 				recptr = nextrecp;
    201  1.1   cherry 				continue;
    202  1.1   cherry 			}
    203  1.1   cherry 
    204  1.1   cherry 			if ((nextrecp = unwind_decode_X4(recptr, &strc[rec_cnt].udesc))){
    205  1.1   cherry 				strc[rec_cnt].type = X4;
    206  1.1   cherry 				recptr = nextrecp;
    207  1.1   cherry 				continue;
    208  1.1   cherry 			}
    209  1.1   cherry 
    210  1.1   cherry 			printf("Skipping body desc slot :: %d \n", rec_cnt);
    211  1.1   cherry 
    212  1.1   cherry 
    213  1.1   cherry 		}
    214  1.1   cherry 	}
    215  1.1   cherry 
    216  1.1   cherry #ifdef UNWIND_DIAGNOSTIC
    217  1.1   cherry 	int i;
    218  1.1   cherry 	for(i = 0;i < rec_cnt;i++) {
    219  1.1   cherry 		dump_recordchain(&strc[i]);
    220  1.1   cherry 	}
    221  1.1   cherry 
    222  1.1   cherry #endif /* UNWIND_DIAGNOSTIC */
    223  1.1   cherry 
    224  1.1   cherry }
    225  1.1   cherry 
    226  1.1   cherry 
    227  1.1   cherry 
    228  1.1   cherry 
    229  1.1   cherry /* Debug support: dump a record chain entry */
    230  1.1   cherry void dump_recordchain(struct recordchain *rchain)
    231  1.1   cherry {
    232  1.1   cherry 
    233  1.1   cherry 	switch(rchain->type) {
    234  1.1   cherry 	case R1:
    235  1.1   cherry 
    236  1.1   cherry 
    237  1.1   cherry 		printf("\t R1:");
    238  1.1   cherry 		if(rchain->udesc.R1.r)
    239  1.1   cherry 			printf("body (");
    240  1.1   cherry 		else
    241  1.1   cherry 			printf("prologue (");
    242  1.1   cherry 		printf("rlen = %ld) \n", rchain->udesc.R1.rlen);
    243  1.1   cherry 		break;
    244  1.1   cherry 
    245  1.1   cherry 	case R2:
    246  1.1   cherry 		printf("\t R2:");
    247  1.1   cherry 		printf("prologue_gr (");
    248  1.1   cherry 		printf("mask = %x, ", rchain->udesc.R2.mask);
    249  1.1   cherry 		printf("grsave = %d, ", rchain->udesc.R2.grsave);
    250  1.1   cherry 		printf("rlen = %ld )\n", rchain->udesc.R2.rlen);
    251  1.1   cherry 		break;
    252  1.1   cherry 
    253  1.1   cherry 	case R3:
    254  1.1   cherry 		printf("\t R3:");
    255  1.1   cherry 		if(rchain->udesc.R3.r)
    256  1.1   cherry 			printf("body (");
    257  1.1   cherry 		else
    258  1.1   cherry 			printf("prologue (");
    259  1.1   cherry 		printf("rlen = %ld )\n", rchain->udesc.R3.rlen);
    260  1.1   cherry 		break;
    261  1.1   cherry 
    262  1.1   cherry 	case P1:
    263  1.1   cherry 		printf("\t\tP1:");
    264  1.1   cherry 		printf("br_mem (brmask = %x) \n", rchain->udesc.P1.brmask);
    265  1.1   cherry 		break;
    266  1.1   cherry 
    267  1.1   cherry 	case P2:
    268  1.1   cherry 		printf("\t\tP2:");
    269  1.1   cherry 		printf("br_gr(brmask = %x, ", rchain->udesc.P2.brmask);
    270  1.1   cherry 		printf("gr = %d ) \n", rchain->udesc.P2.gr);
    271  1.1   cherry 		break;
    272  1.1   cherry 
    273  1.1   cherry 	case P3:
    274  1.1   cherry 		printf("\t\tP3:");
    275  1.1   cherry 		switch(rchain->udesc.P3.r) {
    276  1.1   cherry 		case 0:
    277  1.1   cherry 			printf("psp_gr");
    278  1.1   cherry 			break;
    279  1.1   cherry 		case 1:
    280  1.1   cherry 			printf("rp_gr");
    281  1.1   cherry 			break;
    282  1.1   cherry 		case 2:
    283  1.1   cherry 			printf("pfs_gr");
    284  1.1   cherry 			break;
    285  1.1   cherry 		case 3:
    286  1.1   cherry 			printf("preds_gr");
    287  1.1   cherry 			break;
    288  1.1   cherry 		case 4:
    289  1.1   cherry 			printf("unat_gr");
    290  1.1   cherry 			break;
    291  1.1   cherry 		case 5:
    292  1.1   cherry 			printf("lc_gr");
    293  1.1   cherry 			break;
    294  1.1   cherry 		case 6:
    295  1.1   cherry 			printf("rp_br");
    296  1.1   cherry 			break;
    297  1.1   cherry 		case 7:
    298  1.1   cherry 			printf("rnat_gr");
    299  1.1   cherry 			break;
    300  1.1   cherry 		case 8:
    301  1.1   cherry 			printf("bsp_gr");
    302  1.1   cherry 			break;
    303  1.1   cherry 		case 9:
    304  1.1   cherry 			printf("bspstore_gr");
    305  1.1   cherry 			break;
    306  1.1   cherry 		case 10:
    307  1.1   cherry 			printf("fpsr_gr");
    308  1.1   cherry 			break;
    309  1.1   cherry 		case 11:
    310  1.1   cherry 			printf("priunat_gr");
    311  1.1   cherry 			break;
    312  1.1   cherry 		default:
    313  1.1   cherry 			printf("unknown desc: %d", rchain->udesc.P3.r);
    314  1.1   cherry 
    315  1.1   cherry 		}
    316  1.1   cherry 		printf("(gr/br = %d) \n", rchain->udesc.P3.grbr);
    317  1.1   cherry 
    318  1.1   cherry 		break;
    319  1.1   cherry 
    320  1.1   cherry 	case P4:
    321  1.1   cherry 		printf("P4: (unimplemented): \n");
    322  1.1   cherry 		break;
    323  1.1   cherry 
    324  1.1   cherry 	case P5:
    325  1.1   cherry 		printf("\t\tP5:");
    326  1.1   cherry 		printf("frgr_mem(grmask = %x, frmask = %x )\n",
    327  1.1   cherry 		       rchain->udesc.P5.grmask, rchain->udesc.P5.frmask);
    328  1.1   cherry 		break;
    329  1.1   cherry 
    330  1.1   cherry 	case P6:
    331  1.1   cherry 		printf("\t\tP6: ");
    332  1.1   cherry 		if(rchain->udesc.P6.r)
    333  1.1   cherry 			printf("gr_mem( ");
    334  1.1   cherry 		else
    335  1.1   cherry 			printf("fr_mem( ");
    336  1.1   cherry 		printf("rmask = %x) \n", rchain->udesc.P6.rmask);
    337  1.1   cherry 		break;
    338  1.1   cherry 
    339  1.1   cherry 	case P7:
    340  1.1   cherry 		printf("\t\tP7:");
    341  1.1   cherry 		switch(rchain->udesc.P7.r) {
    342  1.1   cherry 		case 0:
    343  1.1   cherry 			printf("memstack_f( ");
    344  1.1   cherry 			printf("t = %ld, ", rchain->udesc.P7.t);
    345  1.1   cherry 			printf("size = %ld) \n", rchain->udesc.P7.size);
    346  1.1   cherry 			break;
    347  1.1   cherry 		case 1:
    348  1.1   cherry 			printf("memstack_v( ");
    349  1.1   cherry 			printf("t = %ld) \n", rchain->udesc.P7.t);
    350  1.1   cherry 			break;
    351  1.1   cherry 		case 2:
    352  1.1   cherry 			printf("spillbase( ");
    353  1.1   cherry 			printf("pspoff = %ld) \n", rchain->udesc.P7.t);
    354  1.1   cherry 			break;
    355  1.1   cherry 		case 3:
    356  1.1   cherry 			printf("psp_sprel( ");
    357  1.1   cherry 			printf("spoff = %ld) \n", rchain->udesc.P7.t);
    358  1.1   cherry 			break;
    359  1.1   cherry 		case 4:
    360  1.1   cherry 			printf("rp_when( ");
    361  1.1   cherry 			printf("t = %ld) \n", rchain->udesc.P7.t);
    362  1.1   cherry 			break;
    363  1.1   cherry 		case 5:
    364  1.1   cherry 			printf("rp_psprel( ");
    365  1.1   cherry 			printf("pspoff = %ld) \n", rchain->udesc.P7.t);
    366  1.1   cherry 			break;
    367  1.1   cherry 		case 6:
    368  1.1   cherry 			printf("pfs_when( ");
    369  1.1   cherry 			printf("t = %ld) \n", rchain->udesc.P7.t);
    370  1.1   cherry 			break;
    371  1.1   cherry 		case 7:
    372  1.1   cherry 			printf("pfs_psprel( ");
    373  1.1   cherry 			printf("pspoff = %ld) \n", rchain->udesc.P7.t);
    374  1.1   cherry 			break;
    375  1.1   cherry 		case 8:
    376  1.1   cherry 			printf("preds_when( ");
    377  1.1   cherry 			printf("t = %ld) \n", rchain->udesc.P7.t);
    378  1.1   cherry 			break;
    379  1.1   cherry 		case 9:
    380  1.1   cherry 			printf("preds_psprel( ");
    381  1.1   cherry 			printf("pspoff = %ld) \n", rchain->udesc.P7.t);
    382  1.1   cherry 			break;
    383  1.1   cherry 		case 10:
    384  1.1   cherry 			printf("lc_when( ");
    385  1.1   cherry 			printf("t = %ld) \n", rchain->udesc.P7.t);
    386  1.1   cherry 			break;
    387  1.1   cherry 		case 11:
    388  1.1   cherry 			printf("lc_psprel( ");
    389  1.1   cherry 			printf("pspoff = %ld) \n", rchain->udesc.P7.t);
    390  1.1   cherry 			break;
    391  1.1   cherry 		case 12:
    392  1.1   cherry 			printf("unat_when( ");
    393  1.1   cherry 			printf("t = %ld) \n", rchain->udesc.P7.t);
    394  1.1   cherry 			break;
    395  1.1   cherry 		case 13:
    396  1.1   cherry 			printf("unat_psprel( ");
    397  1.1   cherry 			printf("pspoff = %ld) \n", rchain->udesc.P7.t);
    398  1.1   cherry 			break;
    399  1.1   cherry 		case 14:
    400  1.1   cherry 			printf("fpsr_when( ");
    401  1.1   cherry 			printf("t = %ld) \n", rchain->udesc.P7.t);
    402  1.1   cherry 			break;
    403  1.1   cherry 		case 15:
    404  1.1   cherry 			printf("fpsr_psprel( ");
    405  1.1   cherry 			printf("pspoff = %ld) \n", rchain->udesc.P7.t);
    406  1.1   cherry 			break;
    407  1.1   cherry 		default:
    408  1.1   cherry 			printf("unknown \n");
    409  1.1   cherry 		}
    410  1.1   cherry 
    411  1.1   cherry 		break;
    412  1.1   cherry 
    413  1.1   cherry 	case P8:
    414  1.1   cherry 		printf("\t\tP8:");
    415  1.1   cherry 		switch(rchain->udesc.P8.r) {
    416  1.1   cherry 		case 1:
    417  1.1   cherry 			printf("rp_sprel( ");
    418  1.1   cherry 			printf("spoff = %ld) \n", rchain->udesc.P8.t);
    419  1.1   cherry 			break;
    420  1.1   cherry 		case 2:
    421  1.1   cherry 			printf("pfs_sprel( ");
    422  1.1   cherry 			printf("spoff = %ld) \n", rchain->udesc.P8.t);
    423  1.1   cherry 			break;
    424  1.1   cherry 		case 3:
    425  1.1   cherry 			printf("preds_sprel( ");
    426  1.1   cherry 			printf("spoff = %ld) \n", rchain->udesc.P8.t);
    427  1.1   cherry 			break;
    428  1.1   cherry 		case 4:
    429  1.1   cherry 			printf("lc_sprel( ");
    430  1.1   cherry 			printf("spoff = %ld) \n", rchain->udesc.P8.t);
    431  1.1   cherry 			break;
    432  1.1   cherry 		case 5:
    433  1.1   cherry 			printf("unat_sprel( ");
    434  1.1   cherry 			printf("spoff = %ld) \n", rchain->udesc.P8.t);
    435  1.1   cherry 			break;
    436  1.1   cherry 		case 6:
    437  1.1   cherry 			printf("fpsr_sprel( ");
    438  1.1   cherry 			printf("spoff = %ld) \n", rchain->udesc.P8.t);
    439  1.1   cherry 			break;
    440  1.1   cherry 		case 7:
    441  1.1   cherry 			printf("bsp_when( ");
    442  1.1   cherry 			printf("t = %ld) \n", rchain->udesc.P8.t);
    443  1.1   cherry 			break;
    444  1.1   cherry 		case 8:
    445  1.1   cherry 			printf("bsp_psprel( ");
    446  1.1   cherry 			printf("pspoff = %ld) \n", rchain->udesc.P8.t);
    447  1.1   cherry 			break;
    448  1.1   cherry 		case 9:
    449  1.1   cherry 			printf("bsp_sprel( ");
    450  1.1   cherry 			printf("spoff = %ld) \n", rchain->udesc.P8.t);
    451  1.1   cherry 			break;
    452  1.1   cherry 		case 10:
    453  1.1   cherry 			printf("bspstore_when( ");
    454  1.1   cherry 			printf("t = %ld) \n", rchain->udesc.P8.t);
    455  1.1   cherry 			break;
    456  1.1   cherry 		case 11:
    457  1.1   cherry 			printf("bspstore_psprel( ");
    458  1.1   cherry 			printf("pspoff = %ld) \n", rchain->udesc.P8.t);
    459  1.1   cherry 			break;
    460  1.1   cherry 		case 12:
    461  1.1   cherry 			printf("bspstore_sprel( ");
    462  1.1   cherry 			printf("spoff = %ld) \n", rchain->udesc.P8.t);
    463  1.1   cherry 			break;
    464  1.1   cherry 		case 13:
    465  1.1   cherry 			printf("rnat_when( ");
    466  1.1   cherry 			printf("t = %ld) \n", rchain->udesc.P8.t);
    467  1.1   cherry 			break;
    468  1.1   cherry 		case 14:
    469  1.1   cherry 			printf("rnat_psprel( ");
    470  1.1   cherry 			printf("pspoff = %ld) \n", rchain->udesc.P8.t);
    471  1.1   cherry 			break;
    472  1.1   cherry 		case 15:
    473  1.1   cherry 			printf("rnat_sprel( ");
    474  1.1   cherry 			printf("spoff = %ld) \n", rchain->udesc.P8.t);
    475  1.1   cherry 			break;
    476  1.1   cherry 		case 16:
    477  1.1   cherry 			printf("priunat_when_gr( ");
    478  1.1   cherry 			printf("t = %ld) \n", rchain->udesc.P8.t);
    479  1.1   cherry 			break;
    480  1.1   cherry 		case 17:
    481  1.1   cherry 			printf("priunat_psprel( ");
    482  1.1   cherry 			printf("pspoff = %ld) \n", rchain->udesc.P8.t);
    483  1.1   cherry 			break;
    484  1.1   cherry 		case 18:
    485  1.1   cherry 			printf("priunat_sprel( ");
    486  1.1   cherry 			printf("spoff = %ld) \n", rchain->udesc.P8.t);
    487  1.1   cherry 			break;
    488  1.1   cherry 		case 19:
    489  1.1   cherry 			printf("priunat_when_mem( ");
    490  1.1   cherry 			printf("t = %ld) \n", rchain->udesc.P8.t);
    491  1.1   cherry 			break;
    492  1.1   cherry 
    493  1.1   cherry 		default:
    494  1.1   cherry 			printf("unknown \n");
    495  1.1   cherry 		}
    496  1.1   cherry 
    497  1.1   cherry 		break;
    498  1.1   cherry 
    499  1.1   cherry 	case P9:
    500  1.1   cherry 		printf("\t\tP9:");
    501  1.1   cherry 		printf("(grmask = %x, gr = %d) \n",
    502  1.1   cherry 		       rchain->udesc.P9.grmask, rchain->udesc.P9.gr);
    503  1.1   cherry 		break;
    504  1.1   cherry 
    505  1.1   cherry 	case P10:
    506  1.1   cherry 		printf("\t\tP10:");
    507  1.1   cherry 		printf("(abi: ");
    508  1.1   cherry 		switch(rchain->udesc.P10.abi) {
    509  1.1   cherry 		case 0:
    510  1.1   cherry 			printf("Unix SVR4) \n");
    511  1.1   cherry 			break;
    512  1.1   cherry 		case 1:
    513  1.1   cherry 			printf("HP-UX) \n");
    514  1.1   cherry 			break;
    515  1.1   cherry 		default:
    516  1.1   cherry 			printf("Other) \n");
    517  1.1   cherry 		}
    518  1.1   cherry 		break;
    519  1.1   cherry 
    520  1.1   cherry 	case B1:
    521  1.1   cherry 		printf("\t\tB1:");
    522  1.1   cherry 		if(rchain->udesc.B1.r)
    523  1.1   cherry 			printf("copy_state( ");
    524  1.1   cherry 		else
    525  1.1   cherry 			printf("label_state( ");
    526  1.1   cherry 		printf("label = %d) \n", rchain->udesc.B1.label);
    527  1.1   cherry 
    528  1.1   cherry 		break;
    529  1.1   cherry 
    530  1.1   cherry 	case B2:
    531  1.1   cherry 		printf("\t\tB2:");
    532  1.1   cherry 		printf("(ecount = %d, t = %ld)\n",
    533  1.1   cherry 		       rchain->udesc.B2.ecount, rchain->udesc.B2.t);
    534  1.1   cherry 
    535  1.1   cherry 		break;
    536  1.1   cherry 
    537  1.1   cherry 	case B3:
    538  1.1   cherry 		printf("\t\tB3:");
    539  1.1   cherry 		printf("(t = %ld, ecount = %ld) \n",
    540  1.1   cherry 		       rchain->udesc.B3.t, rchain->udesc.B3.ecount);
    541  1.1   cherry 
    542  1.1   cherry 		break;
    543  1.1   cherry 
    544  1.1   cherry 	case B4:
    545  1.1   cherry 		printf("\t\tB4:");
    546  1.1   cherry 		if(rchain->udesc.B4.r)
    547  1.1   cherry 			printf("copy_state( ");
    548  1.1   cherry 		else
    549  1.1   cherry 			printf("label_state( ");
    550  1.1   cherry 
    551  1.1   cherry 		printf("label = %ld) \n", rchain->udesc.B4.label);
    552  1.1   cherry 
    553  1.1   cherry 		break;
    554  1.1   cherry 
    555  1.1   cherry 
    556  1.1   cherry 	case X1:
    557  1.1   cherry 		printf("\tX1:\n ");
    558  1.1   cherry 		break;
    559  1.1   cherry 
    560  1.1   cherry 	case X2:
    561  1.1   cherry 		printf("\tX2:\n");
    562  1.1   cherry 		break;
    563  1.1   cherry 
    564  1.1   cherry 	case X3:
    565  1.1   cherry 		printf("\tX3:\n");
    566  1.1   cherry 		break;
    567  1.1   cherry 
    568  1.1   cherry 	case X4:
    569  1.1   cherry 		printf("\tX4:\n");
    570  1.1   cherry 		break;
    571  1.1   cherry 	default:
    572  1.1   cherry 		printf("\tunknow: \n");
    573  1.1   cherry 	}
    574  1.1   cherry 
    575  1.1   cherry }
    576  1.1   cherry 
    577  1.1   cherry /* State record stuff..... based on section 11. and Appendix A. of the
    578  1.1   cherry  *"Itanium Software Conventions and Runtime Architecture Guide"
    579  1.1   cherry  */
    580  1.1   cherry 
    581  1.1   cherry 
    582  1.1   cherry /* Global variables:
    583  1.1   cherry  * 1. Two arrays of staterecords: recordstack[], recordstackcopy[]
    584  1.1   cherry  *	XXX:	Since we don't use malloc, we have two arbitrary sized arrays
    585  1.1   cherry  *		providing guaranteed memory from the BSS. See the TODO file
    586  1.1   cherry  *		for more details.
    587  1.1   cherry  * 2. Two head variables to hold the member index: unwind_rsp,unwind_rscp
    588  1.1   cherry  */
    589  1.1   cherry 
    590  1.1   cherry struct staterecord recordstack[MAXSTATERECS];
    591  1.1   cherry struct staterecord recordstackcopy[MAXSTATERECS];
    592  1.1   cherry struct staterecord current_state;
    593  1.1   cherry struct staterecord *unwind_rsp, *unwind_rscp;
    594  1.1   cherry 
    595  1.1   cherry 
    596  1.1   cherry uint64_t spill_base = 0;	/* Base of spill area in memory stack frame as a psp relative offset */
    597  1.1   cherry 
    598  1.1   cherry /* Initialises a staterecord from a given template,
    599  1.1   cherry  * with default values as described by the Runtime Spec.
    600  1.1   cherry  */
    601  1.1   cherry 
    602  1.1   cherry void
    603  1.1   cherry initrecord(struct staterecord *target)
    604  1.1   cherry {
    605  1.1   cherry 	target->bsp.where = UNSAVED;
    606  1.1   cherry 	target->bsp.when = 0;
    607  1.1   cherry 	target->bsp.offset = INVALID;
    608  1.1   cherry 	target->psp.where = UNSAVED;
    609  1.1   cherry 	target->psp.when = 0;
    610  1.1   cherry 	target->psp.offset = INVALID;
    611  1.1   cherry 	target->rp.where = UNSAVED;
    612  1.1   cherry 	target->rp.when = 0;
    613  1.1   cherry 	target->rp.offset = INVALID;
    614  1.1   cherry 	target->pfs.where = UNSAVED;
    615  1.1   cherry 	target->pfs.when = 0;
    616  1.1   cherry 	target->pfs.offset = INVALID;
    617  1.1   cherry }
    618  1.1   cherry 
    619  1.1   cherry 
    620  1.1   cherry /* Modifies a staterecord structure by parsing
    621  1.1   cherry  * a single record chain structure.
    622  1.1   cherry  * regionoffset is the offset within a (prologue) region
    623  1.1   cherry  * where the stack unwinding began.
    624  1.1   cherry  */
    625  1.1   cherry 
    626  1.1   cherry void modifyrecord(struct staterecord *srec, struct recordchain *rchain,
    627  1.1   cherry 			 uint64_t regionoffset)
    628  1.1   cherry {
    629  1.1   cherry 
    630  1.1   cherry 
    631  1.1   cherry 	uint64_t grno = 32; /* Default start save GR for prologue_save
    632  1.1   cherry 			     * GRs.
    633  1.1   cherry 			     */
    634  1.1   cherry 
    635  1.1   cherry 
    636  1.1   cherry 
    637  1.1   cherry 	switch (rchain->type) {
    638  1.1   cherry 
    639  1.1   cherry 	case R2:
    640  1.1   cherry 		/* R2, prologue_gr is the only region encoding
    641  1.1   cherry 		 * with register save info.
    642  1.1   cherry 		 */
    643  1.1   cherry 
    644  1.1   cherry 		grno = rchain->udesc.R2.grsave;
    645  1.1   cherry 
    646  1.1   cherry 		if (rchain->udesc.R2.mask & R2MASKRP) {
    647  1.1   cherry 			srec->rp.when = 0;
    648  1.1   cherry 			srec->rp.where = GRREL;
    649  1.1   cherry 			srec->rp.offset = grno++;
    650  1.1   cherry 		}
    651  1.1   cherry 
    652  1.1   cherry 		if (rchain->udesc.R2.mask & R2MASKPFS) {
    653  1.1   cherry 			srec->pfs.when = 0;
    654  1.1   cherry 			srec->pfs.where = GRREL;
    655  1.1   cherry 			srec->pfs.offset = grno++;
    656  1.1   cherry 		}
    657  1.1   cherry 
    658  1.1   cherry 		if (rchain->udesc.R2.mask & R2MASKPSP) {
    659  1.1   cherry 			srec->psp.when = 0;
    660  1.1   cherry 			srec->psp.where = GRREL;
    661  1.1   cherry 			srec->psp.offset = grno++;
    662  1.1   cherry 		}
    663  1.1   cherry 		break;
    664  1.1   cherry 
    665  1.1   cherry 	case P3:
    666  1.1   cherry 		switch (rchain->udesc.P3.r) {
    667  1.1   cherry 		case 0: /* psp_gr */
    668  1.1   cherry 			if (srec->psp.when < regionoffset) {
    669  1.1   cherry 				srec->psp.where = GRREL;
    670  1.1   cherry 				srec->psp.offset = rchain->udesc.P3.grbr;
    671  1.1   cherry 			}
    672  1.1   cherry 			break;
    673  1.1   cherry 
    674  1.1   cherry 		case 1: /* rp_gr */
    675  1.1   cherry 			if (srec->rp.when < regionoffset) {
    676  1.1   cherry 				srec->rp.where = GRREL;
    677  1.1   cherry 				srec->rp.offset = rchain->udesc.P3.grbr;
    678  1.1   cherry 			}
    679  1.1   cherry 			break;
    680  1.1   cherry 
    681  1.1   cherry 		case 2: /* pfs_gr */
    682  1.1   cherry 			if (srec->pfs.when < regionoffset) {
    683  1.1   cherry 				srec->pfs.where = GRREL;
    684  1.1   cherry 				srec->pfs.offset = rchain->udesc.P3.grbr;
    685  1.1   cherry 			}
    686  1.1   cherry 			break;
    687  1.1   cherry 
    688  1.1   cherry 		}
    689  1.1   cherry 		break;
    690  1.1   cherry 
    691  1.1   cherry 
    692  1.1   cherry 	/* XXX: P4 spill_mask and P7: spill_base are for GRs, FRs, and BRs.
    693  1.1   cherry 	 * We're not particularly worried about those right now.
    694  1.1   cherry 	 */
    695  1.1   cherry 
    696  1.1   cherry 	case P7:
    697  1.1   cherry 		switch (rchain->udesc.P7.r) {
    698  1.1   cherry 
    699  1.1   cherry 		case 0: /* mem_stack_f */
    700  1.1   cherry 			if (srec->psp.offset != INVALID) printf("!!!saw mem_stack_f more than once. \n");
    701  1.1   cherry 			srec->psp.when = rchain->udesc.P7.t;
    702  1.1   cherry 			if (srec->psp.when < regionoffset) {
    703  1.1   cherry 				srec->psp.where = IMMED;
    704  1.1   cherry 				srec->psp.offset = rchain->udesc.P7.size; /* spsz.offset is "overloaded" */
    705  1.1   cherry 			}
    706  1.1   cherry 			break;
    707  1.1   cherry 
    708  1.1   cherry 		case 1: /* mem_stack_v */
    709  1.1   cherry 			srec->psp.when = rchain->udesc.P7.t;
    710  1.1   cherry 			break;
    711  1.1   cherry 
    712  1.1   cherry 		case 2: /* spill_base */
    713  1.1   cherry 			spill_base = rchain->udesc.P7.t;
    714  1.1   cherry 			break;
    715  1.1   cherry 
    716  1.1   cherry 		case 3: /* psp_sprel */
    717  1.1   cherry 			if (srec->psp.when < regionoffset) {
    718  1.1   cherry 				srec->psp.where = SPREL;
    719  1.1   cherry 				srec->psp.offset = rchain->udesc.P7.t;
    720  1.1   cherry 			}
    721  1.1   cherry 			break;
    722  1.1   cherry 
    723  1.1   cherry 		case 4: /* rp_when */
    724  1.1   cherry 			srec->rp.when = rchain->udesc.P7.t;
    725  1.1   cherry 			/* XXX: Need to set to prologue_gr(grno) for the orphan case
    726  1.1   cherry 			 *	ie; _gr/_psprel/_sprel not set and therefore default
    727  1.1   cherry 			 *	to begin from the gr specified in prologue_gr.
    728  1.1   cherry 			 */
    729  1.1   cherry 			break;
    730  1.1   cherry 
    731  1.1   cherry 		case 5: /* rp_psprel */
    732  1.1   cherry 			if (srec->rp.when < regionoffset) {
    733  1.1   cherry 				srec->rp.where = PSPREL;
    734  1.1   cherry 				srec->rp.offset = rchain->udesc.P7.t;
    735  1.1   cherry 			}
    736  1.1   cherry 			break;
    737  1.1   cherry 
    738  1.1   cherry 		case 6: /* pfs_when */
    739  1.1   cherry 			srec->pfs.when = rchain->udesc.P7.t;
    740  1.1   cherry 			/* XXX: Need to set to prologue_gr(grno) for the orphan case
    741  1.1   cherry 			 *	ie; _gr/_psprel/_sprel not set and therefore default
    742  1.1   cherry 			 *	to begin from the gr specified in prologue_gr.
    743  1.1   cherry 			 */
    744  1.1   cherry 			break;
    745  1.1   cherry 
    746  1.1   cherry 		case 7: /* pfs_psprel */
    747  1.1   cherry 			if (srec->pfs.when < regionoffset) {
    748  1.1   cherry 				srec->pfs.where = PSPREL;
    749  1.1   cherry 				srec->pfs.offset = rchain->udesc.P7.t;
    750  1.1   cherry 			}
    751  1.1   cherry 			break;
    752  1.1   cherry 
    753  1.1   cherry 		}
    754  1.1   cherry 		break;
    755  1.1   cherry 
    756  1.1   cherry 	case P8:
    757  1.1   cherry 		switch (rchain->udesc.P8.r) {
    758  1.1   cherry 		case 1: /* rp_sprel */
    759  1.1   cherry 			if (srec->rp.when < regionoffset) {
    760  1.1   cherry 				srec->rp.where = SPREL;
    761  1.1   cherry 				srec->rp.offset = rchain->udesc.P8.t;
    762  1.1   cherry 			}
    763  1.1   cherry 			break;
    764  1.1   cherry 		case 2: /* pfs_sprel */
    765  1.1   cherry 			if (srec->pfs.when < regionoffset) {
    766  1.1   cherry 				srec->pfs.where = SPREL;
    767  1.1   cherry 				srec->pfs.offset = rchain->udesc.P8.t;
    768  1.1   cherry 
    769  1.1   cherry 			}
    770  1.1   cherry 			break;
    771  1.1   cherry 		}
    772  1.1   cherry 		break;
    773  1.1   cherry 
    774  1.1   cherry 	case B1:
    775  1.1   cherry 
    776  1.1   cherry 		rchain->udesc.B1.r ? switchrecordstack(0) :
    777  1.1   cherry 			clonerecordstack(0);
    778  1.1   cherry 		break;
    779  1.1   cherry 
    780  1.1   cherry 	case B2:
    781  1.1   cherry 		if (regionoffset < rchain->udesc.B2.t) {
    782  1.1   cherry 		poprecord(&current_state, rchain->udesc.B2.ecount);
    783  1.1   cherry 		}
    784  1.1   cherry 		break;
    785  1.1   cherry 	case B3:
    786  1.1   cherry 		if (regionoffset < rchain->udesc.B3.t) {
    787  1.1   cherry 		poprecord(&current_state, rchain->udesc.B3.ecount);
    788  1.1   cherry 		}
    789  1.1   cherry 		break;
    790  1.1   cherry 	case B4:
    791  1.1   cherry 		rchain->udesc.B4.r ? switchrecordstack(0) :
    792  1.1   cherry 			clonerecordstack(0);
    793  1.1   cherry 		break;
    794  1.1   cherry 
    795  1.1   cherry 	case X1:
    796  1.1   cherry 	case X2:
    797  1.1   cherry 	case X3:
    798  1.1   cherry 		/* XXX: Todo */
    799  1.1   cherry 		break;
    800  1.1   cherry 
    801  1.1   cherry 
    802  1.1   cherry 	case R1:
    803  1.1   cherry 	case R3:
    804  1.1   cherry 	case P1:
    805  1.1   cherry 	case P2:
    806  1.1   cherry 	case P4:
    807  1.1   cherry 	case P5:
    808  1.1   cherry 	case P6:
    809  1.1   cherry 	case P9:
    810  1.1   cherry 	case P10:
    811  1.1   cherry 	default:
    812  1.1   cherry 		/* Ignore. */
    813  1.1   cherry 		printf("XXX: Ignored. \n");
    814  1.1   cherry 	}
    815  1.1   cherry 
    816  1.1   cherry 
    817  1.1   cherry }
    818  1.1   cherry 
    819  1.1   cherry void dump_staterecord(struct staterecord *srec)
    820  1.1   cherry {
    821  1.1   cherry 	printf("rp.where: ");
    822  1.1   cherry 	switch(srec->rp.where) {
    823  1.1   cherry 	case UNSAVED:
    824  1.1   cherry 		printf("UNSAVED ");
    825  1.1   cherry 		break;
    826  1.1   cherry 	case BRREL:
    827  1.1   cherry 		printf("BRREL ");
    828  1.1   cherry 		break;
    829  1.1   cherry 	case GRREL:
    830  1.1   cherry 		printf("GRREL ");
    831  1.1   cherry 		break;
    832  1.1   cherry 	case SPREL:
    833  1.1   cherry 		printf("SPREL ");
    834  1.1   cherry 		break;
    835  1.1   cherry 	case PSPREL:
    836  1.1   cherry 		printf("PSPSREL ");
    837  1.1   cherry 		break;
    838  1.1   cherry 	default:
    839  1.1   cherry 		printf("unknown ");
    840  1.1   cherry 	}
    841  1.1   cherry 
    842  1.1   cherry 	printf(", rp.when = %lu, ", srec->rp.when);
    843  1.1   cherry 	printf("rp.offset = %lu \n", srec->rp.offset);
    844  1.1   cherry 
    845  1.1   cherry 
    846  1.1   cherry 	printf("pfs.where: ");
    847  1.1   cherry 	switch(srec->pfs.where) {
    848  1.1   cherry 	case UNSAVED:
    849  1.1   cherry 		printf("UNSAVED ");
    850  1.1   cherry 		break;
    851  1.1   cherry 	case BRREL:
    852  1.1   cherry 		printf("BRREL ");
    853  1.1   cherry 		break;
    854  1.1   cherry 	case GRREL:
    855  1.1   cherry 		printf("GRREL ");
    856  1.1   cherry 		break;
    857  1.1   cherry 	case SPREL:
    858  1.1   cherry 		printf("SPREL ");
    859  1.1   cherry 		break;
    860  1.1   cherry 	case PSPREL:
    861  1.1   cherry 		printf("PSPSREL ");
    862  1.1   cherry 		break;
    863  1.1   cherry 	default:
    864  1.1   cherry 		printf("unknown ");
    865  1.1   cherry 	}
    866  1.1   cherry 
    867  1.1   cherry 	printf(", pfs.when = %lu, ", srec->pfs.when);
    868  1.1   cherry 	printf("pfs.offset = %lu \n", srec->pfs.offset);
    869  1.1   cherry 
    870  1.1   cherry 
    871  1.1   cherry }
    872  1.1   cherry 
    873  1.1   cherry 
    874  1.1   cherry /* Push a state record on the record stack. */
    875  1.1   cherry 
    876  1.1   cherry void pushrecord(struct staterecord *srec)
    877  1.1   cherry {
    878  1.1   cherry 	if(unwind_rsp >= recordstack + MAXSTATERECS) {
    879  1.1   cherry 		printf("Push exceeded array size!!! \n");
    880  1.1   cherry 		return;
    881  1.1   cherry 	}
    882  1.1   cherry 
    883  1.1   cherry 	memcpy(unwind_rsp, srec, sizeof(struct staterecord));
    884  1.1   cherry 	unwind_rsp++;
    885  1.1   cherry 
    886  1.1   cherry }
    887  1.1   cherry 
    888  1.1   cherry /* Pop n state records off the record stack. */
    889  1.1   cherry 
    890  1.1   cherry void poprecord(struct staterecord *srec, int n)
    891  1.1   cherry {
    892  1.1   cherry 	if(unwind_rsp == recordstack) {
    893  1.1   cherry 		printf("Popped beyond end of Stack!!! \n");
    894  1.1   cherry 		return;
    895  1.1   cherry 	}
    896  1.1   cherry 	unwind_rsp -= n;
    897  1.1   cherry 	memcpy(srec, unwind_rsp, sizeof(struct staterecord));
    898  1.1   cherry #ifdef DEBUG
    899  1.4   cegger 	memset(unwind_rsp, 0, sizeof(struct staterecord) * n);
    900  1.1   cherry #endif
    901  1.1   cherry 
    902  1.1   cherry }
    903  1.1   cherry 
    904  1.1   cherry /* Clone the whole record stack upto this one. */
    905  1.1   cherry void clonerecordstack(u_int label)
    906  1.1   cherry {
    907  1.1   cherry 	memcpy(recordstackcopy, recordstack,
    908  1.1   cherry 	       (unwind_rsp - recordstack) * sizeof(struct staterecord));
    909  1.1   cherry 	unwind_rscp = unwind_rsp;
    910  1.1   cherry }
    911  1.1   cherry 
    912  1.1   cherry /* Discard the current stack, and adopt a clone. */
    913  1.1   cherry void switchrecordstack(u_int label)
    914  1.1   cherry {
    915  1.1   cherry 	memcpy((void *) recordstack, (void *) recordstackcopy,
    916  1.1   cherry 	       (unwind_rscp - recordstackcopy) * sizeof(struct staterecord));
    917  1.1   cherry 	unwind_rsp = unwind_rscp;
    918  1.1   cherry 
    919  1.1   cherry }
    920  1.1   cherry 
    921  1.1   cherry /* In the context of a procedure:
    922  1.1   cherry  * Parses through a record chain, building, pushing and/or popping staterecords,
    923  1.1   cherry  * or cloning/destroying stacks of staterecords as required.
    924  1.1   cherry  * Parameters are:
    925  1.1   cherry  *	rchain: pointer to recordchain array.
    926  1.1   cherry  *	procoffset: offset of point of interest, in slots, within procedure starting from slot 0
    927  1.1   cherry  * This routine obeys [1]
    928  1.1   cherry  */
    929  1.1   cherry struct staterecord *buildrecordstack(struct recordchain *rchain, uint64_t procoffset)
    930  1.1   cherry {
    931  1.1   cherry 
    932  1.1   cherry 	uint64_t rlen = 0;		/* Current region length, defaults to zero, if not specified */
    933  1.1   cherry 	uint64_t roffset = 0;		/* Accumulated region length */
    934  1.1   cherry 	uint64_t rdepth = 0;		/* Offset within current region */
    935  1.1   cherry 
    936  1.1   cherry 
    937  1.1   cherry 	char *spill_mask = NULL;	/* Specifies when preserved registers are spilled, as a bit mask */
    938  1.1   cherry 
    939  1.1   cherry 	spill_mask = NULL;
    940  1.2  thorpej 	bool rtype;
    941  1.1   cherry 
    942  1.1   cherry 	unwind_rsp = recordstack; /* Start with bottom of staterecord stack. */
    943  1.1   cherry 
    944  1.1   cherry 	initrecord(&current_state);
    945  1.1   cherry 
    946  1.1   cherry 	int i;
    947  1.1   cherry 
    948  1.1   cherry 
    949  1.1   cherry 	for (i = 0;i < rec_cnt;i++) {
    950  1.1   cherry 
    951  1.1   cherry 		switch (rchain[i].type) {
    952  1.1   cherry 		case R1:
    953  1.1   cherry 			rlen = rchain[i].udesc.R1.rlen;
    954  1.1   cherry 			rdepth = procoffset - roffset;
    955  1.1   cherry 			if (rdepth < 0) goto out; /* Overshot Region containing procoffset. Bailout. */
    956  1.1   cherry 			roffset += rlen;
    957  1.1   cherry 			rtype = rchain[i].udesc.R1.r;
    958  1.1   cherry 			if (!rtype) {
    959  1.1   cherry 				pushrecord(&current_state);
    960  1.1   cherry 			}
    961  1.1   cherry 			break;
    962  1.1   cherry 
    963  1.1   cherry 		case R3:
    964  1.1   cherry 			rlen = rchain[i].udesc.R3.rlen;
    965  1.1   cherry 			rdepth = procoffset - roffset;
    966  1.1   cherry 			if (rdepth < 0) goto out; /* Overshot Region containing procoffset. Bailout. */
    967  1.1   cherry 			roffset += rlen;
    968  1.1   cherry 			rtype = rchain[i].udesc.R3.r;
    969  1.1   cherry 			if (!rtype) {
    970  1.1   cherry 				pushrecord(&current_state);
    971  1.1   cherry 			}
    972  1.1   cherry 			break;
    973  1.1   cherry 
    974  1.1   cherry 		case R2:
    975  1.1   cherry 			rlen = rchain[i].udesc.R2.rlen;
    976  1.1   cherry 			rdepth = procoffset - roffset;
    977  1.1   cherry 			if (rdepth < 0) goto out; /* Overshot Region containing procoffset. Bailout. */
    978  1.1   cherry 			roffset += rlen;
    979  1.3  thorpej 			rtype = false; /* prologue region */
    980  1.1   cherry 			pushrecord(&current_state);
    981  1.1   cherry 
    982  1.1   cherry 			/* R2 has save info. Continue down. */
    983  1.1   cherry 
    984  1.1   cherry 		case P1:
    985  1.1   cherry 		case P2:
    986  1.1   cherry 		case P3:
    987  1.1   cherry 		case P4:
    988  1.1   cherry 		case P5:
    989  1.1   cherry 		case P6:
    990  1.1   cherry 		case P7:
    991  1.1   cherry 		case P8:
    992  1.1   cherry 		case P9:
    993  1.1   cherry 		case P10:
    994  1.1   cherry 			modifyrecord(&current_state, &rchain[i], rdepth);
    995  1.1   cherry 			break;
    996  1.1   cherry 
    997  1.1   cherry 		case B1:
    998  1.1   cherry 		case B2:
    999  1.1   cherry 		case B3:
   1000  1.1   cherry 		case B4:
   1001  1.1   cherry 			modifyrecord(&current_state, &rchain[i], rlen - 1 - rdepth);
   1002  1.1   cherry 			break;
   1003  1.1   cherry 
   1004  1.1   cherry 		case X1:
   1005  1.1   cherry 		case X2:
   1006  1.1   cherry 		case X3:
   1007  1.1   cherry 		case X4:
   1008  1.1   cherry 		default:
   1009  1.1   cherry 			printf("Error: Unknown descriptor type!!! \n");
   1010  1.1   cherry 
   1011  1.1   cherry 		}
   1012  1.1   cherry 
   1013  1.1   cherry #if UNWIND_DIAGNOSTIC
   1014  1.1   cherry 		dump_staterecord(&current_state);
   1015  1.1   cherry #endif
   1016  1.1   cherry 
   1017  1.1   cherry 
   1018  1.1   cherry 	}
   1019  1.1   cherry 
   1020  1.1   cherry out:
   1021  1.1   cherry 
   1022  1.1   cherry 	return &current_state;
   1023  1.1   cherry }
   1024  1.1   cherry 
   1025  1.1   cherry void updateregs(struct unwind_frame *uwf, struct staterecord *srec, uint64_t procoffset)
   1026  1.1   cherry {
   1027  1.1   cherry 
   1028  1.1   cherry #ifdef UNWIND_DIAGNOSTIC
   1029  1.1   cherry 	printf("updateregs(): \n");
   1030  1.1   cherry 	printf("procoffset (slots) = %lu \n", procoffset);
   1031  1.1   cherry #endif
   1032  1.1   cherry 	/* XXX: Update uwf for regs other than rp and pfs*/
   1033  1.1   cherry 	uint64_t roffset = 0;
   1034  1.1   cherry 
   1035  1.1   cherry 
   1036  1.1   cherry 	/* Uses shadow arrays to update uwf from srec in a loop. */
   1037  1.1   cherry 	/* Count of number of regstate elements in struct staterecord */
   1038  1.1   cherry 	int statecount = sizeof(struct staterecord)/sizeof(struct regstate);
   1039  1.1   cherry 	/* Pointer to current regstate. */
   1040  1.1   cherry 	struct regstate *stptr = (void *) srec;
   1041  1.1   cherry 	/* Pointer to current unwind_frame element */
   1042  1.1   cherry 	uint64_t *gr = (void *) uwf;
   1043  1.1   cherry 
   1044  1.1   cherry 
   1045  1.1   cherry 	int i;
   1046  1.1   cherry 
   1047  1.1   cherry 	for(i = 0; i < statecount; i++) {
   1048  1.1   cherry 		switch (stptr[i].where) {
   1049  1.1   cherry 		case IMMED: /* currently only mem_stack_f */
   1050  1.1   cherry 			if (stptr[i].when >= procoffset) break;
   1051  1.1   cherry 			uwf->psp -= (stptr[i].offset << 4);
   1052  1.1   cherry 			break;
   1053  1.1   cherry 
   1054  1.1   cherry 		case GRREL:
   1055  1.1   cherry 			if (stptr[i].when >= procoffset) break;
   1056  1.1   cherry 
   1057  1.1   cherry 			roffset = stptr[i].offset;
   1058  1.1   cherry 			if (roffset == 0) {
   1059  1.1   cherry 				gr[i] = 0;
   1060  1.1   cherry 				break;
   1061  1.1   cherry 			}
   1062  1.1   cherry 
   1063  1.1   cherry 
   1064  1.1   cherry 			if (roffset < 32) {
   1065  1.1   cherry 				printf("GR%ld: static register save ??? \n", roffset);
   1066  1.1   cherry 				break;
   1067  1.1   cherry 			}
   1068  1.1   cherry 
   1069  1.1   cherry 			/* Fetch from bsp + offset - 32 + Adjust for RNAT. */
   1070  1.1   cherry 			roffset -= 32;
   1071  1.1   cherry 			gr[i] = ia64_getrse_gr(uwf->bsp, roffset);
   1072  1.1   cherry 			break;
   1073  1.1   cherry 
   1074  1.1   cherry 		case SPREL:
   1075  1.1   cherry 			if (stptr[i].when >= procoffset) break;
   1076  1.1   cherry 
   1077  1.1   cherry 			/* Check if frame has been setup. */
   1078  1.1   cherry 			if (srec->psp.offset == INVALID) {
   1079  1.1   cherry 				printf("sprel used without setting up stackframe!!! \n");
   1080  1.1   cherry 				break;
   1081  1.1   cherry 			}
   1082  1.1   cherry 
   1083  1.1   cherry 			roffset = stptr[i].offset;
   1084  1.1   cherry 
   1085  1.1   cherry 			/* Fetch from sp + offset */
   1086  1.1   cherry 			memcpy(&gr[i], (char *) uwf->sp + roffset * 4, sizeof(uint64_t));
   1087  1.1   cherry 			break;
   1088  1.1   cherry 
   1089  1.1   cherry 
   1090  1.1   cherry 		case PSPREL:
   1091  1.1   cherry 			if (stptr[i].when >= procoffset) break;
   1092  1.1   cherry 
   1093  1.1   cherry 			/* Check if frame has been setup. */
   1094  1.1   cherry 			if (srec->psp.offset == INVALID) {
   1095  1.1   cherry 				printf("psprel used without setting up stackframe!!! \n");
   1096  1.1   cherry 				break;
   1097  1.1   cherry 			}
   1098  1.1   cherry 
   1099  1.1   cherry 			roffset = stptr[i].offset;
   1100  1.1   cherry 
   1101  1.1   cherry 			/* Fetch from sp + offset */
   1102  1.1   cherry 			memcpy(&gr[i], (char *) uwf->psp + 16 - (roffset * 4), sizeof(uint64_t));
   1103  1.1   cherry 			break;
   1104  1.1   cherry 
   1105  1.1   cherry 		case UNSAVED:
   1106  1.1   cherry 		case BRREL:
   1107  1.1   cherry 		default:
   1108  1.1   cherry #ifdef UNWIND_DIAGNOSTIC
   1109  1.1   cherry 			printf ("updateregs: reg[%d] is UNSAVED \n", i);
   1110  1.1   cherry #endif
   1111  1.1   cherry 			break;
   1112  1.1   cherry 			/* XXX: Not implemented yet. */
   1113  1.1   cherry 		}
   1114  1.1   cherry 
   1115  1.1   cherry 	}
   1116  1.1   cherry 
   1117  1.1   cherry }
   1118  1.1   cherry 
   1119  1.1   cherry 
   1120  1.1   cherry /* Locates unwind table entry, given unwind table entry info.
   1121  1.1   cherry  * Expects the variables ia64_unwindtab, and ia64_unwindtablen
   1122  1.1   cherry  * to be set appropriately.
   1123  1.1   cherry  */
   1124  1.1   cherry 
   1125  1.1   cherry struct uwtable_ent *
   1126  1.1   cherry get_unwind_table_entry(uint64_t iprel)
   1127  1.1   cherry {
   1128  1.1   cherry 
   1129  1.1   cherry 	extern uint64_t ia64_unwindtab, ia64_unwindtablen;
   1130  1.1   cherry 
   1131  1.1   cherry 	struct uwtable_ent *uwt;
   1132  1.1   cherry 
   1133  1.1   cherry 
   1134  1.1   cherry 	int tabent;
   1135  1.1   cherry 
   1136  1.1   cherry 	for(uwt = (struct uwtable_ent *) ia64_unwindtab, tabent = 0;
   1137  1.1   cherry 	    /* The Runtime spec tells me the table entries are sorted. */
   1138  1.1   cherry 	    uwt->end <= iprel && tabent < ia64_unwindtablen;
   1139  1.1   cherry 	    uwt++, tabent += sizeof(struct uwtable_ent));
   1140  1.1   cherry 
   1141  1.1   cherry 
   1142  1.1   cherry 	if (!(uwt->start <= iprel && iprel < uwt->end)) {
   1143  1.1   cherry #ifdef UNWIND_DIAGNOSTIC
   1144  1.1   cherry 		printf("Entry not found \n");
   1145  1.1   cherry 		printf("iprel = %lx \n", iprel);
   1146  1.1   cherry 		printf("uwt->start = %lx \nuwt->end = %lx \n",
   1147  1.1   cherry 		       uwt->start, uwt->end);
   1148  1.1   cherry 		printf("tabent = %d \n", tabent);
   1149  1.1   cherry 		printf("ia64_unwindtablen = %ld \n",
   1150  1.1   cherry 		       ia64_unwindtablen);
   1151  1.1   cherry #endif
   1152  1.1   cherry 		return NULL;
   1153  1.1   cherry 	}
   1154  1.1   cherry 
   1155  1.1   cherry #ifdef UNWIND_DIAGNOSTIC
   1156  1.1   cherry 	printf("uwt->start = %lx \nuwt->end = %lx \n"
   1157  1.1   cherry 	       "uwt->infoptr = %p\n", uwt->start, uwt->end, uwt->infoptr);
   1158  1.1   cherry #endif
   1159  1.1   cherry 
   1160  1.1   cherry 	return uwt;
   1161  1.1   cherry }
   1162  1.1   cherry 
   1163  1.1   cherry 
   1164  1.1   cherry /*
   1165  1.1   cherry  * Reads unwind table info and updates register values.
   1166  1.1   cherry  */
   1167  1.1   cherry 
   1168  1.1   cherry void
   1169  1.1   cherry patchunwindframe(struct unwind_frame *uwf, uint64_t iprel, uint64_t relocoffset)
   1170  1.1   cherry {
   1171  1.1   cherry 
   1172  1.1   cherry 	extern struct recordchain strc[];
   1173  1.1   cherry 	struct staterecord *srec;
   1174  1.1   cherry 	struct uwtable_ent *uwt;
   1175  1.1   cherry 	uint64_t infoptr, procoffset, slotoffset;
   1176  1.1   cherry 
   1177  1.1   cherry 	if (iprel < 0) {
   1178  1.1   cherry 		panic("unwind ip out of range!!! \n");
   1179  1.1   cherry 		return;
   1180  1.1   cherry 	}
   1181  1.1   cherry 
   1182  1.1   cherry 
   1183  1.1   cherry 	uwt = get_unwind_table_entry(iprel);
   1184  1.1   cherry 
   1185  1.1   cherry 	if (uwt == NULL) return;
   1186  1.1   cherry 
   1187  1.1   cherry 	infoptr = (uint64_t) uwt->infoptr + relocoffset;
   1188  1.1   cherry 
   1189  1.1   cherry 	if (infoptr > relocoffset) {
   1190  1.1   cherry 		buildrecordchain(infoptr, NULL);
   1191  1.1   cherry 	}
   1192  1.1   cherry 	else return;
   1193  1.1   cherry 
   1194  1.1   cherry 	slotoffset = iprel & 3;
   1195  1.1   cherry 
   1196  1.1   cherry 	/* procoffset in Number of _slots_ , _not_ a byte offset. */
   1197  1.1   cherry 
   1198  1.1   cherry 	procoffset = (((iprel - slotoffset) - (uwt->start)) / 0x10 * 3) + slotoffset;
   1199  1.1   cherry 	srec = buildrecordstack(strc, procoffset);
   1200  1.1   cherry 
   1201  1.1   cherry 	updateregs(uwf, srec, procoffset);
   1202  1.1   cherry }
   1203  1.1   cherry 
   1204