unwind.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:56k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  * Copyright (C) 1999-2002 Hewlett-Packard Co
  3.  * David Mosberger-Tang <davidm@hpl.hp.com>
  4.  */
  5. /*
  6.  * This file implements call frame unwind support for the Linux
  7.  * kernel.  Parsing and processing the unwind information is
  8.  * time-consuming, so this implementation translates the unwind
  9.  * descriptors into unwind scripts.  These scripts are very simple
  10.  * (basically a sequence of assignments) and efficient to execute.
  11.  * They are cached for later re-use.  Each script is specific for a
  12.  * given instruction pointer address and the set of predicate values
  13.  * that the script depends on (most unwind descriptors are
  14.  * unconditional and scripts often do not depend on predicates at
  15.  * all).  This code is based on the unwind conventions described in
  16.  * the "IA-64 Software Conventions and Runtime Architecture" manual.
  17.  *
  18.  * SMP conventions:
  19.  * o updates to the global unwind data (in structure "unw") are serialized
  20.  *   by the unw.lock spinlock
  21.  * o each unwind script has its own read-write lock; a thread must acquire
  22.  *   a read lock before executing a script and must acquire a write lock
  23.  *   before modifying a script
  24.  * o if both the unw.lock spinlock and a script's read-write lock must be
  25.  *   acquired, then the read-write lock must be acquired first.
  26.  */
  27. #include <linux/bootmem.h>
  28. #include <linux/kernel.h>
  29. #include <linux/sched.h>
  30. #include <linux/slab.h>
  31. #include <asm/unwind.h>
  32. #include <asm/delay.h>
  33. #include <asm/page.h>
  34. #include <asm/ptrace.h>
  35. #include <asm/ptrace_offsets.h>
  36. #include <asm/rse.h>
  37. #include <asm/system.h>
  38. #include <asm/uaccess.h>
  39. #include "entry.h"
  40. #include "unwind_i.h"
  41. #define MIN(a,b) ((a) < (b) ? (a) : (b))
  42. #define p5 5
  43. #define UNW_LOG_CACHE_SIZE 7 /* each unw_script is ~256 bytes in size */
  44. #define UNW_CACHE_SIZE (1 << UNW_LOG_CACHE_SIZE)
  45. #define UNW_LOG_HASH_SIZE (UNW_LOG_CACHE_SIZE + 1)
  46. #define UNW_HASH_SIZE (1 << UNW_LOG_HASH_SIZE)
  47. #define UNW_DEBUG 0
  48. #define UNW_STATS 0 /* WARNING: this disabled interrupts for long time-spans!! */
  49. #if UNW_DEBUG
  50.   static long unw_debug_level = 255;
  51. # define debug(level,format...) if (unw_debug_level > level) printk(format)
  52. # define dprintk(format...) printk(format)
  53. # define inline
  54. #else
  55. # define debug(level,format...)
  56. # define dprintk(format...)
  57. #endif
  58. #if UNW_STATS
  59. # define STAT(x...) x
  60. #else
  61. # define STAT(x...)
  62. #endif
  63. #define alloc_reg_state() kmalloc(sizeof(struct unw_state_record), GFP_ATOMIC)
  64. #define free_reg_state(usr) kfree(usr)
  65. #define alloc_labeled_state() kmalloc(sizeof(struct unw_labeled_state), GFP_ATOMIC)
  66. #define free_labeled_state(usr) kfree(usr)
  67. typedef unsigned long unw_word;
  68. typedef unsigned char unw_hash_index_t;
  69. #define struct_offset(str,fld) ((char *)&((str *)NULL)->fld - (char *) 0)
  70. static struct {
  71. spinlock_t lock; /* spinlock for unwind data */
  72. /* list of unwind tables (one per load-module) */
  73. struct unw_table *tables;
  74. /* table of registers that prologues can save (and order in which they're saved): */
  75. const unsigned char save_order[8];
  76. /* maps a preserved register index (preg_index) to corresponding switch_stack offset: */
  77. unsigned short sw_off[sizeof(struct unw_frame_info) / 8];
  78. unsigned short lru_head; /* index of lead-recently used script */
  79. unsigned short lru_tail; /* index of most-recently used script */
  80. /* index into unw_frame_info for preserved register i */
  81. unsigned short preg_index[UNW_NUM_REGS];
  82. /* unwind table for the kernel: */
  83. struct unw_table kernel_table;
  84. /* unwind table describing the gate page (kernel code that is mapped into user space): */
  85. size_t gate_table_size;
  86. unsigned long *gate_table;
  87. /* hash table that maps instruction pointer to script index: */
  88. unsigned short hash[UNW_HASH_SIZE];
  89. /* script cache: */
  90. struct unw_script cache[UNW_CACHE_SIZE];
  91. # if UNW_DEBUG
  92. const char *preg_name[UNW_NUM_REGS];
  93. # endif
  94. # if UNW_STATS
  95. struct {
  96. struct {
  97. int lookups;
  98. int hinted_hits;
  99. int normal_hits;
  100. int collision_chain_traversals;
  101. } cache;
  102. struct {
  103. unsigned long build_time;
  104. unsigned long run_time;
  105. unsigned long parse_time;
  106. int builds;
  107. int news;
  108. int collisions;
  109. int runs;
  110. } script;
  111. struct {
  112. unsigned long init_time;
  113. unsigned long unwind_time;
  114. int inits;
  115. int unwinds;
  116. } api;
  117. } stat;
  118. # endif
  119. } unw = {
  120. tables: &unw.kernel_table,
  121. lock: SPIN_LOCK_UNLOCKED,
  122. save_order: {
  123. UNW_REG_RP, UNW_REG_PFS, UNW_REG_PSP, UNW_REG_PR,
  124. UNW_REG_UNAT, UNW_REG_LC, UNW_REG_FPSR, UNW_REG_PRI_UNAT_GR
  125. },
  126. preg_index: {
  127. struct_offset(struct unw_frame_info, pri_unat_loc)/8, /* PRI_UNAT_GR */
  128. struct_offset(struct unw_frame_info, pri_unat_loc)/8, /* PRI_UNAT_MEM */
  129. struct_offset(struct unw_frame_info, bsp_loc)/8,
  130. struct_offset(struct unw_frame_info, bspstore_loc)/8,
  131. struct_offset(struct unw_frame_info, pfs_loc)/8,
  132. struct_offset(struct unw_frame_info, rnat_loc)/8,
  133. struct_offset(struct unw_frame_info, psp)/8,
  134. struct_offset(struct unw_frame_info, rp_loc)/8,
  135. struct_offset(struct unw_frame_info, r4)/8,
  136. struct_offset(struct unw_frame_info, r5)/8,
  137. struct_offset(struct unw_frame_info, r6)/8,
  138. struct_offset(struct unw_frame_info, r7)/8,
  139. struct_offset(struct unw_frame_info, unat_loc)/8,
  140. struct_offset(struct unw_frame_info, pr_loc)/8,
  141. struct_offset(struct unw_frame_info, lc_loc)/8,
  142. struct_offset(struct unw_frame_info, fpsr_loc)/8,
  143. struct_offset(struct unw_frame_info, b1_loc)/8,
  144. struct_offset(struct unw_frame_info, b2_loc)/8,
  145. struct_offset(struct unw_frame_info, b3_loc)/8,
  146. struct_offset(struct unw_frame_info, b4_loc)/8,
  147. struct_offset(struct unw_frame_info, b5_loc)/8,
  148. struct_offset(struct unw_frame_info, f2_loc)/8,
  149. struct_offset(struct unw_frame_info, f3_loc)/8,
  150. struct_offset(struct unw_frame_info, f4_loc)/8,
  151. struct_offset(struct unw_frame_info, f5_loc)/8,
  152. struct_offset(struct unw_frame_info, fr_loc[16 - 16])/8,
  153. struct_offset(struct unw_frame_info, fr_loc[17 - 16])/8,
  154. struct_offset(struct unw_frame_info, fr_loc[18 - 16])/8,
  155. struct_offset(struct unw_frame_info, fr_loc[19 - 16])/8,
  156. struct_offset(struct unw_frame_info, fr_loc[20 - 16])/8,
  157. struct_offset(struct unw_frame_info, fr_loc[21 - 16])/8,
  158. struct_offset(struct unw_frame_info, fr_loc[22 - 16])/8,
  159. struct_offset(struct unw_frame_info, fr_loc[23 - 16])/8,
  160. struct_offset(struct unw_frame_info, fr_loc[24 - 16])/8,
  161. struct_offset(struct unw_frame_info, fr_loc[25 - 16])/8,
  162. struct_offset(struct unw_frame_info, fr_loc[26 - 16])/8,
  163. struct_offset(struct unw_frame_info, fr_loc[27 - 16])/8,
  164. struct_offset(struct unw_frame_info, fr_loc[28 - 16])/8,
  165. struct_offset(struct unw_frame_info, fr_loc[29 - 16])/8,
  166. struct_offset(struct unw_frame_info, fr_loc[30 - 16])/8,
  167. struct_offset(struct unw_frame_info, fr_loc[31 - 16])/8,
  168. },
  169. hash : { [0 ... UNW_HASH_SIZE - 1] = -1 },
  170. #if UNW_DEBUG
  171. preg_name: {
  172. "pri_unat_gr", "pri_unat_mem", "bsp", "bspstore", "ar.pfs", "ar.rnat", "psp", "rp",
  173. "r4", "r5", "r6", "r7",
  174. "ar.unat", "pr", "ar.lc", "ar.fpsr",
  175. "b1", "b2", "b3", "b4", "b5",
  176. "f2", "f3", "f4", "f5",
  177. "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",
  178. "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31"
  179. }
  180. #endif
  181. };
  182. /* Unwind accessors.  */
  183. /*
  184.  * Returns offset of rREG in struct pt_regs.
  185.  */
  186. static inline unsigned long
  187. pt_regs_off (unsigned long reg)
  188. {
  189. unsigned long off =0;
  190. if (reg >= 1 && reg <= 3)
  191. off = struct_offset(struct pt_regs, r1) + 8*(reg - 1);
  192. else if (reg <= 11)
  193. off = struct_offset(struct pt_regs, r8) + 8*(reg - 8);
  194. else if (reg <= 15)
  195. off = struct_offset(struct pt_regs, r12) + 8*(reg - 12);
  196. else if (reg <= 31)
  197. off = struct_offset(struct pt_regs, r16) + 8*(reg - 16);
  198. else
  199. dprintk("unwind: bad scratch reg r%lun", reg);
  200. return off;
  201. }
  202. int
  203. unw_access_gr (struct unw_frame_info *info, int regnum, unsigned long *val, char *nat, int write)
  204. {
  205. unsigned long *addr, *nat_addr, nat_mask = 0, dummy_nat;
  206. struct unw_ireg *ireg;
  207. struct pt_regs *pt;
  208. if ((unsigned) regnum - 1 >= 127) {
  209. dprintk("unwind: trying to access non-existent r%un", regnum);
  210. return -1;
  211. }
  212. if (regnum < 32) {
  213. if (regnum >= 4 && regnum <= 7) {
  214. /* access a preserved register */
  215. ireg = &info->r4 + (regnum - 4);
  216. addr = ireg->loc;
  217. if (addr) {
  218. nat_addr = addr + ireg->nat.off;
  219. switch (ireg->nat.type) {
  220.       case UNW_NAT_VAL:
  221. /* simulate getf.sig/setf.sig */
  222. if (write) {
  223. if (*nat) {
  224. /* write NaTVal and be done with it */
  225. addr[0] = 0;
  226. addr[1] = 0x1fffe;
  227. return 0;
  228. }
  229. addr[1] = 0x1003e;
  230. } else {
  231. if (addr[0] == 0 && addr[1] == 0x1ffe) {
  232. /* return NaT and be done with it */
  233. *val = 0;
  234. *nat = 1;
  235. return 0;
  236. }
  237. }
  238. /* fall through */
  239.       case UNW_NAT_NONE:
  240. dummy_nat = 0;
  241. nat_addr = &dummy_nat;
  242. break;
  243.       case UNW_NAT_MEMSTK:
  244. nat_mask = (1UL << ((long) addr & 0x1f8)/8);
  245. break;
  246.       case UNW_NAT_REGSTK:
  247. nat_addr = ia64_rse_rnat_addr(addr);
  248. if ((unsigned long) addr < info->regstk.limit
  249.     || (unsigned long) addr >= info->regstk.top)
  250. {
  251. dprintk("unwind: %p outside of regstk "
  252. "[0x%lx-0x%lx)n", (void *) addr,
  253. info->regstk.limit,
  254. info->regstk.top);
  255. return -1;
  256. }
  257. if ((unsigned long) nat_addr >= info->regstk.top)
  258. nat_addr = &info->sw->ar_rnat;
  259. nat_mask = (1UL << ia64_rse_slot_num(addr));
  260. break;
  261. }
  262. } else {
  263. addr = &info->sw->r4 + (regnum - 4);
  264. nat_addr = &info->sw->ar_unat;
  265. nat_mask = (1UL << ((long) addr & 0x1f8)/8);
  266. }
  267. } else {
  268. /* access a scratch register */
  269. if (info->flags & UNW_FLAG_INTERRUPT_FRAME)
  270. pt = (struct pt_regs *) info->psp - 1;
  271. else
  272. pt = (struct pt_regs *) info->sp - 1;
  273. addr = (unsigned long *) ((long) pt + pt_regs_off(regnum));
  274. if (info->pri_unat_loc)
  275. nat_addr = info->pri_unat_loc;
  276. else
  277. nat_addr = &info->sw->ar_unat;
  278. nat_mask = (1UL << ((long) addr & 0x1f8)/8);
  279. }
  280. } else {
  281. /* access a stacked register */
  282. addr = ia64_rse_skip_regs((unsigned long *) info->bsp, regnum - 32);
  283. nat_addr = ia64_rse_rnat_addr(addr);
  284. if ((unsigned long) addr < info->regstk.limit
  285.     || (unsigned long) addr >= info->regstk.top)
  286. {
  287. dprintk("unwind: ignoring attempt to access register outside of rbsn");
  288. return -1;
  289. }
  290. if ((unsigned long) nat_addr >= info->regstk.top)
  291. nat_addr = &info->sw->ar_rnat;
  292. nat_mask = (1UL << ia64_rse_slot_num(addr));
  293. }
  294. if (write) {
  295. *addr = *val;
  296. if (*nat)
  297. *nat_addr |= nat_mask;
  298. else
  299. *nat_addr &= ~nat_mask;
  300. } else {
  301. if ((*nat_addr & nat_mask) == 0) {
  302. *val = *addr;
  303. *nat = 0;
  304. } else {
  305. *val = 0; /* if register is a NaT, *addr may contain kernel data! */
  306. *nat = 1;
  307. }
  308. }
  309. return 0;
  310. }
  311. int
  312. unw_access_br (struct unw_frame_info *info, int regnum, unsigned long *val, int write)
  313. {
  314. unsigned long *addr;
  315. struct pt_regs *pt;
  316. if (info->flags & UNW_FLAG_INTERRUPT_FRAME)
  317. pt = (struct pt_regs *) info->psp - 1;
  318. else
  319. pt = (struct pt_regs *) info->sp - 1;
  320. switch (regnum) {
  321. /* scratch: */
  322.       case 0: addr = &pt->b0; break;
  323.       case 6: addr = &pt->b6; break;
  324.       case 7: addr = &pt->b7; break;
  325. /* preserved: */
  326.       case 1: case 2: case 3: case 4: case 5:
  327. addr = *(&info->b1_loc + (regnum - 1));
  328. if (!addr)
  329. addr = &info->sw->b1 + (regnum - 1);
  330. break;
  331.       default:
  332. dprintk("unwind: trying to access non-existent b%un", regnum);
  333. return -1;
  334. }
  335. if (write)
  336. *addr = *val;
  337. else
  338. *val = *addr;
  339. return 0;
  340. }
  341. int
  342. unw_access_fr (struct unw_frame_info *info, int regnum, struct ia64_fpreg *val, int write)
  343. {
  344. struct ia64_fpreg *addr = 0;
  345. struct pt_regs *pt;
  346. if ((unsigned) (regnum - 2) >= 126) {
  347. dprintk("unwind: trying to access non-existent f%un", regnum);
  348. return -1;
  349. }
  350. if (info->flags & UNW_FLAG_INTERRUPT_FRAME)
  351. pt = (struct pt_regs *) info->psp - 1;
  352. else
  353. pt = (struct pt_regs *) info->sp - 1;
  354. if (regnum <= 5) {
  355. addr = *(&info->f2_loc + (regnum - 2));
  356. if (!addr)
  357. addr = &info->sw->f2 + (regnum - 2);
  358. } else if (regnum <= 15) {
  359. if (regnum <= 9)
  360. addr = &pt->f6  + (regnum - 6);
  361. else
  362. addr = &info->sw->f10 + (regnum - 10);
  363. } else if (regnum <= 31) {
  364. addr = info->fr_loc[regnum - 16];
  365. if (!addr)
  366. addr = &info->sw->f16 + (regnum - 16);
  367. } else {
  368. struct task_struct *t = info->task;
  369. if (write)
  370. ia64_sync_fph(t);
  371. else
  372. ia64_flush_fph(t);
  373. addr = t->thread.fph + (regnum - 32);
  374. }
  375. if (write)
  376. *addr = *val;
  377. else
  378. *val = *addr;
  379. return 0;
  380. }
  381. int
  382. unw_access_ar (struct unw_frame_info *info, int regnum, unsigned long *val, int write)
  383. {
  384. unsigned long *addr;
  385. struct pt_regs *pt;
  386. if (info->flags & UNW_FLAG_INTERRUPT_FRAME)
  387. pt = (struct pt_regs *) info->psp - 1;
  388. else
  389. pt = (struct pt_regs *) info->sp - 1;
  390. switch (regnum) {
  391.       case UNW_AR_BSP:
  392. addr = info->bsp_loc;
  393. if (!addr)
  394. addr = &info->sw->ar_bspstore;
  395. break;
  396.       case UNW_AR_BSPSTORE:
  397. addr = info->bspstore_loc;
  398. if (!addr)
  399. addr = &info->sw->ar_bspstore;
  400. break;
  401.       case UNW_AR_PFS:
  402. addr = info->pfs_loc;
  403. if (!addr)
  404. addr = &info->sw->ar_pfs;
  405. break;
  406.       case UNW_AR_RNAT:
  407. addr = info->rnat_loc;
  408. if (!addr)
  409. addr = &info->sw->ar_rnat;
  410. break;
  411.       case UNW_AR_UNAT:
  412. addr = info->unat_loc;
  413. if (!addr)
  414. addr = &info->sw->ar_unat;
  415. break;
  416.       case UNW_AR_LC:
  417. addr = info->lc_loc;
  418. if (!addr)
  419. addr = &info->sw->ar_lc;
  420. break;
  421.       case UNW_AR_EC:
  422. if (!info->cfm_loc)
  423. return -1;
  424. if (write)
  425. *info->cfm_loc =
  426. (*info->cfm_loc & ~(0x3fUL << 52)) | ((*val & 0x3f) << 52);
  427. else
  428. *val = (*info->cfm_loc >> 52) & 0x3f;
  429. return 0;
  430.       case UNW_AR_FPSR:
  431. addr = info->fpsr_loc;
  432. if (!addr)
  433. addr = &info->sw->ar_fpsr;
  434. break;
  435.       case UNW_AR_RSC:
  436. addr = &pt->ar_rsc;
  437. break;
  438.       case UNW_AR_CCV:
  439. addr = &pt->ar_ccv;
  440. break;
  441.       default:
  442. dprintk("unwind: trying to access non-existent ar%un", regnum);
  443. return -1;
  444. }
  445. if (write)
  446. *addr = *val;
  447. else
  448. *val = *addr;
  449. return 0;
  450. }
  451. int
  452. unw_access_pr (struct unw_frame_info *info, unsigned long *val, int write)
  453. {
  454. unsigned long *addr;
  455. addr = info->pr_loc;
  456. if (!addr)
  457. addr = &info->sw->pr;
  458. if (write)
  459. *addr = *val;
  460. else
  461. *val = *addr;
  462. return 0;
  463. }
  464. /* Routines to manipulate the state stack.  */
  465. static inline void
  466. push (struct unw_state_record *sr)
  467. {
  468. struct unw_reg_state *rs;
  469. rs = alloc_reg_state();
  470. if (!rs) {
  471. printk("unwind: cannot stack reg state!n");
  472. return;
  473. }
  474. memcpy(rs, &sr->curr, sizeof(*rs));
  475. sr->curr.next = rs;
  476. }
  477. static void
  478. pop (struct unw_state_record *sr)
  479. {
  480. struct unw_reg_state *rs = sr->curr.next;
  481. if (!rs) {
  482. printk("unwind: stack underflow!n");
  483. return;
  484. }
  485. memcpy(&sr->curr, rs, sizeof(*rs));
  486. free_reg_state(rs);
  487. }
  488. /* Make a copy of the state stack.  Non-recursive to avoid stack overflows.  */
  489. static struct unw_reg_state *
  490. dup_state_stack (struct unw_reg_state *rs)
  491. {
  492. struct unw_reg_state *copy, *prev = NULL, *first = NULL;
  493. while (rs) {
  494. copy = alloc_reg_state();
  495. if (!copy) {
  496. printk ("unwind.dup_state_stack: out of memoryn");
  497. return NULL;
  498. }
  499. memcpy(copy, rs, sizeof(*copy));
  500. if (first)
  501. prev->next = copy;
  502. else
  503. first = copy;
  504. rs = rs->next;
  505. prev = copy;
  506. }
  507. return first;
  508. }
  509. /* Free all stacked register states (but not RS itself).  */
  510. static void
  511. free_state_stack (struct unw_reg_state *rs)
  512. {
  513. struct unw_reg_state *p, *next;
  514. for (p = rs->next; p != NULL; p = next) {
  515. next = p->next;
  516. free_reg_state(p);
  517. }
  518. rs->next = NULL;
  519. }
  520. /* Unwind decoder routines */
  521. static enum unw_register_index __attribute__((const))
  522. decode_abreg (unsigned char abreg, int memory)
  523. {
  524. switch (abreg) {
  525.       case 0x04 ... 0x07: return UNW_REG_R4 + (abreg - 0x04);
  526.       case 0x22 ... 0x25: return UNW_REG_F2 + (abreg - 0x22);
  527.       case 0x30 ... 0x3f: return UNW_REG_F16 + (abreg - 0x30);
  528.       case 0x41 ... 0x45: return UNW_REG_B1 + (abreg - 0x41);
  529.       case 0x60: return UNW_REG_PR;
  530.       case 0x61: return UNW_REG_PSP;
  531.       case 0x62: return memory ? UNW_REG_PRI_UNAT_MEM : UNW_REG_PRI_UNAT_GR;
  532.       case 0x63: return UNW_REG_RP;
  533.       case 0x64: return UNW_REG_BSP;
  534.       case 0x65: return UNW_REG_BSPSTORE;
  535.       case 0x66: return UNW_REG_RNAT;
  536.       case 0x67: return UNW_REG_UNAT;
  537.       case 0x68: return UNW_REG_FPSR;
  538.       case 0x69: return UNW_REG_PFS;
  539.       case 0x6a: return UNW_REG_LC;
  540.       default:
  541. break;
  542. }
  543. dprintk("unwind: bad abreg=0x%xn", abreg);
  544. return UNW_REG_LC;
  545. }
  546. static void
  547. set_reg (struct unw_reg_info *reg, enum unw_where where, int when, unsigned long val)
  548. {
  549. reg->val = val;
  550. reg->where = where;
  551. if (reg->when == UNW_WHEN_NEVER)
  552. reg->when = when;
  553. }
  554. static void
  555. alloc_spill_area (unsigned long *offp, unsigned long regsize,
  556.   struct unw_reg_info *lo, struct unw_reg_info *hi)
  557. {
  558. struct unw_reg_info *reg;
  559. for (reg = hi; reg >= lo; --reg) {
  560. if (reg->where == UNW_WHERE_SPILL_HOME) {
  561. reg->where = UNW_WHERE_PSPREL;
  562. *offp -= regsize;
  563. reg->val = *offp;
  564. }
  565. }
  566. }
  567. static inline void
  568. spill_next_when (struct unw_reg_info **regp, struct unw_reg_info *lim, unw_word t)
  569. {
  570. struct unw_reg_info *reg;
  571. for (reg = *regp; reg <= lim; ++reg) {
  572. if (reg->where == UNW_WHERE_SPILL_HOME) {
  573. reg->when = t;
  574. *regp = reg + 1;
  575. return;
  576. }
  577. }
  578. dprintk("unwind: excess spill!n");
  579. }
  580. static inline void
  581. finish_prologue (struct unw_state_record *sr)
  582. {
  583. struct unw_reg_info *reg;
  584. unsigned long off;
  585. int i;
  586. /*
  587.  * First, resolve implicit register save locations (see Section "11.4.2.3 Rules
  588.  * for Using Unwind Descriptors", rule 3):
  589.  */
  590. for (i = 0; i < (int) sizeof(unw.save_order)/sizeof(unw.save_order[0]); ++i) {
  591. reg = sr->curr.reg + unw.save_order[i];
  592. if (reg->where == UNW_WHERE_GR_SAVE) {
  593. reg->where = UNW_WHERE_GR;
  594. reg->val = sr->gr_save_loc++;
  595. }
  596. }
  597. /*
  598.  * Next, compute when the fp, general, and branch registers get
  599.  * saved.  This must come before alloc_spill_area() because
  600.  * we need to know which registers are spilled to their home
  601.  * locations.
  602.  */
  603. if (sr->imask) {
  604. unsigned char kind, mask = 0, *cp = sr->imask;
  605. unsigned long t;
  606. static const unsigned char limit[3] = {
  607. UNW_REG_F31, UNW_REG_R7, UNW_REG_B5
  608. };
  609. struct unw_reg_info *(regs[3]);
  610. regs[0] = sr->curr.reg + UNW_REG_F2;
  611. regs[1] = sr->curr.reg + UNW_REG_R4;
  612. regs[2] = sr->curr.reg + UNW_REG_B1;
  613. for (t = 0; t < sr->region_len; ++t) {
  614. if ((t & 3) == 0)
  615. mask = *cp++;
  616. kind = (mask >> 2*(3-(t & 3))) & 3;
  617. if (kind > 0)
  618. spill_next_when(&regs[kind - 1], sr->curr.reg + limit[kind - 1],
  619. sr->region_start + t);
  620. }
  621. }
  622. /*
  623.  * Next, lay out the memory stack spill area:
  624.  */
  625. if (sr->any_spills) {
  626. off = sr->spill_offset;
  627. alloc_spill_area(&off, 16, sr->curr.reg + UNW_REG_F2, sr->curr.reg + UNW_REG_F31);
  628. alloc_spill_area(&off,  8, sr->curr.reg + UNW_REG_B1, sr->curr.reg + UNW_REG_B5);
  629. alloc_spill_area(&off,  8, sr->curr.reg + UNW_REG_R4, sr->curr.reg + UNW_REG_R7);
  630. }
  631. }
  632. /*
  633.  * Region header descriptors.
  634.  */
  635. static void
  636. desc_prologue (int body, unw_word rlen, unsigned char mask, unsigned char grsave,
  637.        struct unw_state_record *sr)
  638. {
  639. int i;
  640. if (!(sr->in_body || sr->first_region))
  641. finish_prologue(sr);
  642. sr->first_region = 0;
  643. /* check if we're done: */
  644. if (sr->when_target < sr->region_start + sr->region_len) {
  645. sr->done = 1;
  646. return;
  647. }
  648. for (i = 0; i < sr->epilogue_count; ++i)
  649. pop(sr);
  650. sr->epilogue_count = 0;
  651. sr->epilogue_start = UNW_WHEN_NEVER;
  652. if (!body)
  653. push(sr);
  654. sr->region_start += sr->region_len;
  655. sr->region_len = rlen;
  656. sr->in_body = body;
  657. if (!body) {
  658. for (i = 0; i < 4; ++i) {
  659. if (mask & 0x8)
  660. set_reg(sr->curr.reg + unw.save_order[i], UNW_WHERE_GR,
  661. sr->region_start + sr->region_len - 1, grsave++);
  662. mask <<= 1;
  663. }
  664. sr->gr_save_loc = grsave;
  665. sr->any_spills = 0;
  666. sr->imask = 0;
  667. sr->spill_offset = 0x10; /* default to psp+16 */
  668. }
  669. }
  670. /*
  671.  * Prologue descriptors.
  672.  */
  673. static inline void
  674. desc_abi (unsigned char abi, unsigned char context, struct unw_state_record *sr)
  675. {
  676. if (abi == 0 && context == 'i')
  677. sr->flags |= UNW_FLAG_INTERRUPT_FRAME;
  678. else
  679. dprintk("unwind: ignoring unwabi(abi=0x%x,context=0x%x)n", abi, context);
  680. }
  681. static inline void
  682. desc_br_gr (unsigned char brmask, unsigned char gr, struct unw_state_record *sr)
  683. {
  684. int i;
  685. for (i = 0; i < 5; ++i) {
  686. if (brmask & 1)
  687. set_reg(sr->curr.reg + UNW_REG_B1 + i, UNW_WHERE_GR,
  688. sr->region_start + sr->region_len - 1, gr++);
  689. brmask >>= 1;
  690. }
  691. }
  692. static inline void
  693. desc_br_mem (unsigned char brmask, struct unw_state_record *sr)
  694. {
  695. int i;
  696. for (i = 0; i < 5; ++i) {
  697. if (brmask & 1) {
  698. set_reg(sr->curr.reg + UNW_REG_B1 + i, UNW_WHERE_SPILL_HOME,
  699. sr->region_start + sr->region_len - 1, 0);
  700. sr->any_spills = 1;
  701. }
  702. brmask >>= 1;
  703. }
  704. }
  705. static inline void
  706. desc_frgr_mem (unsigned char grmask, unw_word frmask, struct unw_state_record *sr)
  707. {
  708. int i;
  709. for (i = 0; i < 4; ++i) {
  710. if ((grmask & 1) != 0) {
  711. set_reg(sr->curr.reg + UNW_REG_R4 + i, UNW_WHERE_SPILL_HOME,
  712. sr->region_start + sr->region_len - 1, 0);
  713. sr->any_spills = 1;
  714. }
  715. grmask >>= 1;
  716. }
  717. for (i = 0; i < 20; ++i) {
  718. if ((frmask & 1) != 0) {
  719. int base = (i < 4) ? UNW_REG_F2 : UNW_REG_F16 - 4;
  720. set_reg(sr->curr.reg + base + i, UNW_WHERE_SPILL_HOME,
  721. sr->region_start + sr->region_len - 1, 0);
  722. sr->any_spills = 1;
  723. }
  724. frmask >>= 1;
  725. }
  726. }
  727. static inline void
  728. desc_fr_mem (unsigned char frmask, struct unw_state_record *sr)
  729. {
  730. int i;
  731. for (i = 0; i < 4; ++i) {
  732. if ((frmask & 1) != 0) {
  733. set_reg(sr->curr.reg + UNW_REG_F2 + i, UNW_WHERE_SPILL_HOME,
  734. sr->region_start + sr->region_len - 1, 0);
  735. sr->any_spills = 1;
  736. }
  737. frmask >>= 1;
  738. }
  739. }
  740. static inline void
  741. desc_gr_gr (unsigned char grmask, unsigned char gr, struct unw_state_record *sr)
  742. {
  743. int i;
  744. for (i = 0; i < 4; ++i) {
  745. if ((grmask & 1) != 0)
  746. set_reg(sr->curr.reg + UNW_REG_R4 + i, UNW_WHERE_GR,
  747. sr->region_start + sr->region_len - 1, gr++);
  748. grmask >>= 1;
  749. }
  750. }
  751. static inline void
  752. desc_gr_mem (unsigned char grmask, struct unw_state_record *sr)
  753. {
  754. int i;
  755. for (i = 0; i < 4; ++i) {
  756. if ((grmask & 1) != 0) {
  757. set_reg(sr->curr.reg + UNW_REG_R4 + i, UNW_WHERE_SPILL_HOME,
  758. sr->region_start + sr->region_len - 1, 0);
  759. sr->any_spills = 1;
  760. }
  761. grmask >>= 1;
  762. }
  763. }
  764. static inline void
  765. desc_mem_stack_f (unw_word t, unw_word size, struct unw_state_record *sr)
  766. {
  767. set_reg(sr->curr.reg + UNW_REG_PSP, UNW_WHERE_NONE,
  768. sr->region_start + MIN((int)t, sr->region_len - 1), 16*size);
  769. }
  770. static inline void
  771. desc_mem_stack_v (unw_word t, struct unw_state_record *sr)
  772. {
  773. sr->curr.reg[UNW_REG_PSP].when = sr->region_start + MIN((int)t, sr->region_len - 1);
  774. }
  775. static inline void
  776. desc_reg_gr (unsigned char reg, unsigned char dst, struct unw_state_record *sr)
  777. {
  778. set_reg(sr->curr.reg + reg, UNW_WHERE_GR, sr->region_start + sr->region_len - 1, dst);
  779. }
  780. static inline void
  781. desc_reg_psprel (unsigned char reg, unw_word pspoff, struct unw_state_record *sr)
  782. {
  783. set_reg(sr->curr.reg + reg, UNW_WHERE_PSPREL, sr->region_start + sr->region_len - 1,
  784. 0x10 - 4*pspoff);
  785. }
  786. static inline void
  787. desc_reg_sprel (unsigned char reg, unw_word spoff, struct unw_state_record *sr)
  788. {
  789. set_reg(sr->curr.reg + reg, UNW_WHERE_SPREL, sr->region_start + sr->region_len - 1,
  790. 4*spoff);
  791. }
  792. static inline void
  793. desc_rp_br (unsigned char dst, struct unw_state_record *sr)
  794. {
  795. sr->return_link_reg = dst;
  796. }
  797. static inline void
  798. desc_reg_when (unsigned char regnum, unw_word t, struct unw_state_record *sr)
  799. {
  800. struct unw_reg_info *reg = sr->curr.reg + regnum;
  801. if (reg->where == UNW_WHERE_NONE)
  802. reg->where = UNW_WHERE_GR_SAVE;
  803. reg->when = sr->region_start + MIN((int)t, sr->region_len - 1);
  804. }
  805. static inline void
  806. desc_spill_base (unw_word pspoff, struct unw_state_record *sr)
  807. {
  808. sr->spill_offset = 0x10 - 4*pspoff;
  809. }
  810. static inline unsigned char *
  811. desc_spill_mask (unsigned char *imaskp, struct unw_state_record *sr)
  812. {
  813. sr->imask = imaskp;
  814. return imaskp + (2*sr->region_len + 7)/8;
  815. }
  816. /*
  817.  * Body descriptors.
  818.  */
  819. static inline void
  820. desc_epilogue (unw_word t, unw_word ecount, struct unw_state_record *sr)
  821. {
  822. sr->epilogue_start = sr->region_start + sr->region_len - 1 - t;
  823. sr->epilogue_count = ecount + 1;
  824. }
  825. static inline void
  826. desc_copy_state (unw_word label, struct unw_state_record *sr)
  827. {
  828. struct unw_labeled_state *ls;
  829. for (ls = sr->labeled_states; ls; ls = ls->next) {
  830. if (ls->label == label) {
  831. free_state_stack(&sr->curr);
  832. memcpy(&sr->curr, &ls->saved_state, sizeof(sr->curr));
  833. sr->curr.next = dup_state_stack(ls->saved_state.next);
  834. return;
  835. }
  836. }
  837. printk("unwind: failed to find state labeled 0x%lxn", label);
  838. }
  839. static inline void
  840. desc_label_state (unw_word label, struct unw_state_record *sr)
  841. {
  842. struct unw_labeled_state *ls;
  843. ls = alloc_labeled_state();
  844. if (!ls) {
  845. printk("unwind.desc_label_state(): out of memoryn");
  846. return;
  847. }
  848. ls->label = label;
  849. memcpy(&ls->saved_state, &sr->curr, sizeof(ls->saved_state));
  850. ls->saved_state.next = dup_state_stack(sr->curr.next);
  851. /* insert into list of labeled states: */
  852. ls->next = sr->labeled_states;
  853. sr->labeled_states = ls;
  854. }
  855. /*
  856.  * General descriptors.
  857.  */
  858. static inline int
  859. desc_is_active (unsigned char qp, unw_word t, struct unw_state_record *sr)
  860. {
  861. if (sr->when_target <= sr->region_start + MIN((int)t, sr->region_len - 1))
  862. return 0;
  863. if (qp > 0) {
  864. if ((sr->pr_val & (1UL << qp)) == 0)
  865. return 0;
  866. sr->pr_mask |= (1UL << qp);
  867. }
  868. return 1;
  869. }
  870. static inline void
  871. desc_restore_p (unsigned char qp, unw_word t, unsigned char abreg, struct unw_state_record *sr)
  872. {
  873. struct unw_reg_info *r;
  874. if (!desc_is_active(qp, t, sr))
  875. return;
  876. r = sr->curr.reg + decode_abreg(abreg, 0);
  877. r->where = UNW_WHERE_NONE;
  878. r->when = UNW_WHEN_NEVER;
  879. r->val = 0;
  880. }
  881. static inline void
  882. desc_spill_reg_p (unsigned char qp, unw_word t, unsigned char abreg, unsigned char x,
  883.      unsigned char ytreg, struct unw_state_record *sr)
  884. {
  885. enum unw_where where = UNW_WHERE_GR;
  886. struct unw_reg_info *r;
  887. if (!desc_is_active(qp, t, sr))
  888. return;
  889. if (x)
  890. where = UNW_WHERE_BR;
  891. else if (ytreg & 0x80)
  892. where = UNW_WHERE_FR;
  893. r = sr->curr.reg + decode_abreg(abreg, 0);
  894. r->where = where;
  895. r->when = sr->region_start + MIN((int)t, sr->region_len - 1);
  896. r->val = (ytreg & 0x7f);
  897. }
  898. static inline void
  899. desc_spill_psprel_p (unsigned char qp, unw_word t, unsigned char abreg, unw_word pspoff,
  900.      struct unw_state_record *sr)
  901. {
  902. struct unw_reg_info *r;
  903. if (!desc_is_active(qp, t, sr))
  904. return;
  905. r = sr->curr.reg + decode_abreg(abreg, 1);
  906. r->where = UNW_WHERE_PSPREL;
  907. r->when = sr->region_start + MIN((int)t, sr->region_len - 1);
  908. r->val = 0x10 - 4*pspoff;
  909. }
  910. static inline void
  911. desc_spill_sprel_p (unsigned char qp, unw_word t, unsigned char abreg, unw_word spoff,
  912.        struct unw_state_record *sr)
  913. {
  914. struct unw_reg_info *r;
  915. if (!desc_is_active(qp, t, sr))
  916. return;
  917. r = sr->curr.reg + decode_abreg(abreg, 1);
  918. r->where = UNW_WHERE_SPREL;
  919. r->when = sr->region_start + MIN((int)t, sr->region_len - 1);
  920. r->val = 4*spoff;
  921. }
  922. #define UNW_DEC_BAD_CODE(code) printk("unwind: unknown code 0x%02xn", code);
  923. /*
  924.  * region headers:
  925.  */
  926. #define UNW_DEC_PROLOGUE_GR(fmt,r,m,gr,arg) desc_prologue(0,r,m,gr,arg)
  927. #define UNW_DEC_PROLOGUE(fmt,b,r,arg) desc_prologue(b,r,0,32,arg)
  928. /*
  929.  * prologue descriptors:
  930.  */
  931. #define UNW_DEC_ABI(fmt,a,c,arg) desc_abi(a,c,arg)
  932. #define UNW_DEC_BR_GR(fmt,b,g,arg) desc_br_gr(b,g,arg)
  933. #define UNW_DEC_BR_MEM(fmt,b,arg) desc_br_mem(b,arg)
  934. #define UNW_DEC_FRGR_MEM(fmt,g,f,arg) desc_frgr_mem(g,f,arg)
  935. #define UNW_DEC_FR_MEM(fmt,f,arg) desc_fr_mem(f,arg)
  936. #define UNW_DEC_GR_GR(fmt,m,g,arg) desc_gr_gr(m,g,arg)
  937. #define UNW_DEC_GR_MEM(fmt,m,arg) desc_gr_mem(m,arg)
  938. #define UNW_DEC_MEM_STACK_F(fmt,t,s,arg) desc_mem_stack_f(t,s,arg)
  939. #define UNW_DEC_MEM_STACK_V(fmt,t,arg) desc_mem_stack_v(t,arg)
  940. #define UNW_DEC_REG_GR(fmt,r,d,arg) desc_reg_gr(r,d,arg)
  941. #define UNW_DEC_REG_PSPREL(fmt,r,o,arg) desc_reg_psprel(r,o,arg)
  942. #define UNW_DEC_REG_SPREL(fmt,r,o,arg) desc_reg_sprel(r,o,arg)
  943. #define UNW_DEC_REG_WHEN(fmt,r,t,arg) desc_reg_when(r,t,arg)
  944. #define UNW_DEC_PRIUNAT_WHEN_GR(fmt,t,arg) desc_reg_when(UNW_REG_PRI_UNAT_GR,t,arg)
  945. #define UNW_DEC_PRIUNAT_WHEN_MEM(fmt,t,arg) desc_reg_when(UNW_REG_PRI_UNAT_MEM,t,arg)
  946. #define UNW_DEC_PRIUNAT_GR(fmt,r,arg) desc_reg_gr(UNW_REG_PRI_UNAT_GR,r,arg)
  947. #define UNW_DEC_PRIUNAT_PSPREL(fmt,o,arg) desc_reg_psprel(UNW_REG_PRI_UNAT_MEM,o,arg)
  948. #define UNW_DEC_PRIUNAT_SPREL(fmt,o,arg) desc_reg_sprel(UNW_REG_PRI_UNAT_MEM,o,arg)
  949. #define UNW_DEC_RP_BR(fmt,d,arg) desc_rp_br(d,arg)
  950. #define UNW_DEC_SPILL_BASE(fmt,o,arg) desc_spill_base(o,arg)
  951. #define UNW_DEC_SPILL_MASK(fmt,m,arg) (m = desc_spill_mask(m,arg))
  952. /*
  953.  * body descriptors:
  954.  */
  955. #define UNW_DEC_EPILOGUE(fmt,t,c,arg) desc_epilogue(t,c,arg)
  956. #define UNW_DEC_COPY_STATE(fmt,l,arg) desc_copy_state(l,arg)
  957. #define UNW_DEC_LABEL_STATE(fmt,l,arg) desc_label_state(l,arg)
  958. /*
  959.  * general unwind descriptors:
  960.  */
  961. #define UNW_DEC_SPILL_REG_P(f,p,t,a,x,y,arg) desc_spill_reg_p(p,t,a,x,y,arg)
  962. #define UNW_DEC_SPILL_REG(f,t,a,x,y,arg) desc_spill_reg_p(0,t,a,x,y,arg)
  963. #define UNW_DEC_SPILL_PSPREL_P(f,p,t,a,o,arg) desc_spill_psprel_p(p,t,a,o,arg)
  964. #define UNW_DEC_SPILL_PSPREL(f,t,a,o,arg) desc_spill_psprel_p(0,t,a,o,arg)
  965. #define UNW_DEC_SPILL_SPREL_P(f,p,t,a,o,arg) desc_spill_sprel_p(p,t,a,o,arg)
  966. #define UNW_DEC_SPILL_SPREL(f,t,a,o,arg) desc_spill_sprel_p(0,t,a,o,arg)
  967. #define UNW_DEC_RESTORE_P(f,p,t,a,arg) desc_restore_p(p,t,a,arg)
  968. #define UNW_DEC_RESTORE(f,t,a,arg) desc_restore_p(0,t,a,arg)
  969. #include "unwind_decoder.c"
  970. /* Unwind scripts. */
  971. static inline unw_hash_index_t
  972. hash (unsigned long ip)
  973. {
  974. # define magic 0x9e3779b97f4a7c16 /* based on (sqrt(5)/2-1)*2^64 */
  975. return (ip >> 4)*magic >> (64 - UNW_LOG_HASH_SIZE);
  976. }
  977. static inline long
  978. cache_match (struct unw_script *script, unsigned long ip, unsigned long pr)
  979. {
  980. read_lock(&script->lock);
  981. if (ip == script->ip && ((pr ^ script->pr_val) & script->pr_mask) == 0)
  982. /* keep the read lock... */
  983. return 1;
  984. read_unlock(&script->lock);
  985. return 0;
  986. }
  987. static inline struct unw_script *
  988. script_lookup (struct unw_frame_info *info)
  989. {
  990. struct unw_script *script = unw.cache + info->hint;
  991. unsigned short index;
  992. unsigned long ip, pr;
  993. STAT(++unw.stat.cache.lookups);
  994. ip = info->ip;
  995. pr = info->pr;
  996. if (cache_match(script, ip, pr)) {
  997. STAT(++unw.stat.cache.hinted_hits);
  998. return script;
  999. }
  1000. index = unw.hash[hash(ip)];
  1001. if (index >= UNW_CACHE_SIZE)
  1002. return 0;
  1003. script = unw.cache + index;
  1004. while (1) {
  1005. if (cache_match(script, ip, pr)) {
  1006. /* update hint; no locking required as single-word writes are atomic */
  1007. STAT(++unw.stat.cache.normal_hits);
  1008. unw.cache[info->prev_script].hint = script - unw.cache;
  1009. return script;
  1010. }
  1011. if (script->coll_chain >= UNW_HASH_SIZE)
  1012. return 0;
  1013. script = unw.cache + script->coll_chain;
  1014. STAT(++unw.stat.cache.collision_chain_traversals);
  1015. }
  1016. }
  1017. /*
  1018.  * On returning, a write lock for the SCRIPT is still being held.
  1019.  */
  1020. static inline struct unw_script *
  1021. script_new (unsigned long ip)
  1022. {
  1023. struct unw_script *script, *prev, *tmp;
  1024. unw_hash_index_t index;
  1025. unsigned long flags;
  1026. unsigned short head;
  1027. STAT(++unw.stat.script.news);
  1028. /*
  1029.  * Can't (easily) use cmpxchg() here because of ABA problem
  1030.  * that is intrinsic in cmpxchg()...
  1031.  */
  1032. spin_lock_irqsave(&unw.lock, flags);
  1033. {
  1034. head = unw.lru_head;
  1035. script = unw.cache + head;
  1036. unw.lru_head = script->lru_chain;
  1037. }
  1038. spin_unlock(&unw.lock);
  1039. /*
  1040.  * XXX We'll deadlock here if we interrupt a thread that is
  1041.  * holding a read lock on script->lock.  A try_write_lock()
  1042.  * might be mighty handy here...  Alternatively, we could
  1043.  * disable interrupts whenever we hold a read-lock, but that
  1044.  * seems silly.
  1045.  */
  1046. write_lock(&script->lock);
  1047. spin_lock(&unw.lock);
  1048. {
  1049. /* re-insert script at the tail of the LRU chain: */
  1050. unw.cache[unw.lru_tail].lru_chain = head;
  1051. unw.lru_tail = head;
  1052. /* remove the old script from the hash table (if it's there): */
  1053. if (script->ip) {
  1054. index = hash(script->ip);
  1055. tmp = unw.cache + unw.hash[index];
  1056. prev = 0;
  1057. while (1) {
  1058. if (tmp == script) {
  1059. if (prev)
  1060. prev->coll_chain = tmp->coll_chain;
  1061. else
  1062. unw.hash[index] = tmp->coll_chain;
  1063. break;
  1064. } else
  1065. prev = tmp;
  1066. if (tmp->coll_chain >= UNW_CACHE_SIZE)
  1067. /* old script wasn't in the hash-table */
  1068. break;
  1069. tmp = unw.cache + tmp->coll_chain;
  1070. }
  1071. }
  1072. /* enter new script in the hash table */
  1073. index = hash(ip);
  1074. script->coll_chain = unw.hash[index];
  1075. unw.hash[index] = script - unw.cache;
  1076. script->ip = ip; /* set new IP while we're holding the locks */
  1077. STAT(if (script->coll_chain < UNW_CACHE_SIZE) ++unw.stat.script.collisions);
  1078. }
  1079. spin_unlock_irqrestore(&unw.lock, flags);
  1080. script->flags = 0;
  1081. script->hint = 0;
  1082. script->count = 0;
  1083. return script;
  1084. }
  1085. static void
  1086. script_finalize (struct unw_script *script, struct unw_state_record *sr)
  1087. {
  1088. script->pr_mask = sr->pr_mask;
  1089. script->pr_val = sr->pr_val;
  1090. /*
  1091.  * We could down-grade our write-lock on script->lock here but
  1092.  * the rwlock API doesn't offer atomic lock downgrading, so
  1093.  * we'll just keep the write-lock and release it later when
  1094.  * we're done using the script.
  1095.  */
  1096. }
  1097. static inline void
  1098. script_emit (struct unw_script *script, struct unw_insn insn)
  1099. {
  1100. if (script->count >= UNW_MAX_SCRIPT_LEN) {
  1101. dprintk("unwind: script exceeds maximum size of %u instructions!n",
  1102. UNW_MAX_SCRIPT_LEN);
  1103. return;
  1104. }
  1105. script->insn[script->count++] = insn;
  1106. }
  1107. static inline void
  1108. emit_nat_info (struct unw_state_record *sr, int i, struct unw_script *script)
  1109. {
  1110. struct unw_reg_info *r = sr->curr.reg + i;
  1111. enum unw_insn_opcode opc;
  1112. struct unw_insn insn;
  1113. unsigned long val = 0;
  1114. switch (r->where) {
  1115.       case UNW_WHERE_GR:
  1116. if (r->val >= 32) {
  1117. /* register got spilled to a stacked register */
  1118. opc = UNW_INSN_SETNAT_TYPE;
  1119. val = UNW_NAT_REGSTK;
  1120. } else
  1121. /* register got spilled to a scratch register */
  1122. opc = UNW_INSN_SETNAT_MEMSTK;
  1123. break;
  1124.       case UNW_WHERE_FR:
  1125. opc = UNW_INSN_SETNAT_TYPE;
  1126. val = UNW_NAT_VAL;
  1127. break;
  1128.       case UNW_WHERE_BR:
  1129. opc = UNW_INSN_SETNAT_TYPE;
  1130. val = UNW_NAT_NONE;
  1131. break;
  1132.       case UNW_WHERE_PSPREL:
  1133.       case UNW_WHERE_SPREL:
  1134. opc = UNW_INSN_SETNAT_MEMSTK;
  1135. break;
  1136.       default:
  1137. dprintk("unwind: don't know how to emit nat info for where = %un", r->where);
  1138. return;
  1139. }
  1140. insn.opc = opc;
  1141. insn.dst = unw.preg_index[i];
  1142. insn.val = val;
  1143. script_emit(script, insn);
  1144. }
  1145. static void
  1146. compile_reg (struct unw_state_record *sr, int i, struct unw_script *script)
  1147. {
  1148. struct unw_reg_info *r = sr->curr.reg + i;
  1149. enum unw_insn_opcode opc;
  1150. unsigned long val, rval;
  1151. struct unw_insn insn;
  1152. long need_nat_info;
  1153. if (r->where == UNW_WHERE_NONE || r->when >= sr->when_target)
  1154. return;
  1155. opc = UNW_INSN_MOVE;
  1156. val = rval = r->val;
  1157. need_nat_info = (i >= UNW_REG_R4 && i <= UNW_REG_R7);
  1158. switch (r->where) {
  1159.       case UNW_WHERE_GR:
  1160. if (rval >= 32) {
  1161. opc = UNW_INSN_MOVE_STACKED;
  1162. val = rval - 32;
  1163. } else if (rval >= 4 && rval <= 7) {
  1164. if (need_nat_info) {
  1165. opc = UNW_INSN_MOVE2;
  1166. need_nat_info = 0;
  1167. }
  1168. val = unw.preg_index[UNW_REG_R4 + (rval - 4)];
  1169. } else {
  1170. opc = UNW_INSN_ADD_SP;
  1171. val = -sizeof(struct pt_regs) + pt_regs_off(rval);
  1172. }
  1173. break;
  1174.       case UNW_WHERE_FR:
  1175. if (rval <= 5)
  1176. val = unw.preg_index[UNW_REG_F2  + (rval -  1)];
  1177. else if (rval >= 16 && rval <= 31)
  1178. val = unw.preg_index[UNW_REG_F16 + (rval - 16)];
  1179. else {
  1180. opc = UNW_INSN_ADD_SP;
  1181. val = -sizeof(struct pt_regs);
  1182. if (rval <= 9)
  1183. val += struct_offset(struct pt_regs, f6) + 16*(rval - 6);
  1184. else
  1185. dprintk("unwind: kernel may not touch f%lun", rval);
  1186. }
  1187. break;
  1188.       case UNW_WHERE_BR:
  1189. if (rval >= 1 && rval <= 5)
  1190. val = unw.preg_index[UNW_REG_B1 + (rval - 1)];
  1191. else {
  1192. opc = UNW_INSN_ADD_SP;
  1193. val = -sizeof(struct pt_regs);
  1194. if (rval == 0)
  1195. val += struct_offset(struct pt_regs, b0);
  1196. else if (rval == 6)
  1197. val += struct_offset(struct pt_regs, b6);
  1198. else
  1199. val += struct_offset(struct pt_regs, b7);
  1200. }
  1201. break;
  1202.       case UNW_WHERE_SPREL:
  1203. opc = UNW_INSN_ADD_SP;
  1204. break;
  1205.       case UNW_WHERE_PSPREL:
  1206. opc = UNW_INSN_ADD_PSP;
  1207. break;
  1208.       default:
  1209. dprintk("unwind: register %u has unexpected `where' value of %un", i, r->where);
  1210. break;
  1211. }
  1212. insn.opc = opc;
  1213. insn.dst = unw.preg_index[i];
  1214. insn.val = val;
  1215. script_emit(script, insn);
  1216. if (need_nat_info)
  1217. emit_nat_info(sr, i, script);
  1218. if (i == UNW_REG_PSP) {
  1219. /*
  1220.  * info->psp must contain the _value_ of the previous
  1221.  * sp, not it's save location.  We get this by
  1222.  * dereferencing the value we just stored in
  1223.  * info->psp:
  1224.  */
  1225. insn.opc = UNW_INSN_LOAD;
  1226. insn.dst = insn.val = unw.preg_index[UNW_REG_PSP];
  1227. script_emit(script, insn);
  1228. }
  1229. }
  1230. static inline const struct unw_table_entry *
  1231. lookup (struct unw_table *table, unsigned long rel_ip)
  1232. {
  1233. const struct unw_table_entry *e = 0;
  1234. unsigned long lo, hi, mid;
  1235. /* do a binary search for right entry: */
  1236. for (lo = 0, hi = table->length; lo < hi; ) {
  1237. mid = (lo + hi) / 2;
  1238. e = &table->array[mid];
  1239. if (rel_ip < e->start_offset)
  1240. hi = mid;
  1241. else if (rel_ip >= e->end_offset)
  1242. lo = mid + 1;
  1243. else
  1244. break;
  1245. }
  1246. if (rel_ip < e->start_offset || rel_ip >= e->end_offset)
  1247. return NULL;
  1248. return e;
  1249. }
  1250. /*
  1251.  * Build an unwind script that unwinds from state OLD_STATE to the
  1252.  * entrypoint of the function that called OLD_STATE.
  1253.  */
  1254. static inline struct unw_script *
  1255. build_script (struct unw_frame_info *info)
  1256. {
  1257. const struct unw_table_entry *e = 0;
  1258. struct unw_script *script = 0;
  1259. struct unw_labeled_state *ls, *next;
  1260. unsigned long ip = info->ip;
  1261. struct unw_state_record sr;
  1262. struct unw_table *table;
  1263. struct unw_reg_info *r;
  1264. struct unw_insn insn;
  1265. u8 *dp, *desc_end;
  1266. u64 hdr;
  1267. int i;
  1268. STAT(unsigned long start, parse_start;)
  1269. STAT(++unw.stat.script.builds; start = ia64_get_itc());
  1270. /* build state record */
  1271. memset(&sr, 0, sizeof(sr));
  1272. for (r = sr.curr.reg; r < sr.curr.reg + UNW_NUM_REGS; ++r)
  1273. r->when = UNW_WHEN_NEVER;
  1274. sr.pr_val = info->pr;
  1275. script = script_new(ip);
  1276. if (!script) {
  1277. dprintk("unwind: failed to create unwind scriptn");
  1278. STAT(unw.stat.script.build_time += ia64_get_itc() - start);
  1279. return 0;
  1280. }
  1281. unw.cache[info->prev_script].hint = script - unw.cache;
  1282. /* search the kernels and the modules' unwind tables for IP: */
  1283. STAT(parse_start = ia64_get_itc());
  1284. for (table = unw.tables; table; table = table->next) {
  1285. if (ip >= table->start && ip < table->end) {
  1286. e = lookup(table, ip - table->segment_base);
  1287. break;
  1288. }
  1289. }
  1290. if (!e) {
  1291. /* no info, return default unwinder (leaf proc, no mem stack, no saved regs)  */
  1292. dprintk("unwind: no unwind info for ip=0x%lx (prev ip=0x%lx)n", ip,
  1293. unw.cache[info->prev_script].ip);
  1294. sr.curr.reg[UNW_REG_RP].where = UNW_WHERE_BR;
  1295. sr.curr.reg[UNW_REG_RP].when = -1;
  1296. sr.curr.reg[UNW_REG_RP].val = 0;
  1297. compile_reg(&sr, UNW_REG_RP, script);
  1298. script_finalize(script, &sr);
  1299. STAT(unw.stat.script.parse_time += ia64_get_itc() - parse_start);
  1300. STAT(unw.stat.script.build_time += ia64_get_itc() - start);
  1301. return script;
  1302. }
  1303. sr.when_target = (3*((ip & ~0xfUL) - (table->segment_base + e->start_offset))/16
  1304.   + (ip & 0xfUL));
  1305. hdr = *(u64 *) (table->segment_base + e->info_offset);
  1306. dp =   (u8 *)  (table->segment_base + e->info_offset + 8);
  1307. desc_end = dp + 8*UNW_LENGTH(hdr);
  1308. while (!sr.done && dp < desc_end)
  1309. dp = unw_decode(dp, sr.in_body, &sr);
  1310. if (sr.when_target > sr.epilogue_start) {
  1311. /*
  1312.  * sp has been restored and all values on the memory stack below
  1313.  * psp also have been restored.
  1314.  */
  1315. sr.curr.reg[UNW_REG_PSP].val = 0;
  1316. sr.curr.reg[UNW_REG_PSP].where = UNW_WHERE_NONE;
  1317. sr.curr.reg[UNW_REG_PSP].when = UNW_WHEN_NEVER;
  1318. for (r = sr.curr.reg; r < sr.curr.reg + UNW_NUM_REGS; ++r)
  1319. if ((r->where == UNW_WHERE_PSPREL && r->val <= 0x10)
  1320.     || r->where == UNW_WHERE_SPREL)
  1321. {
  1322. r->val = 0;
  1323. r->where = UNW_WHERE_NONE;
  1324. r->when = UNW_WHEN_NEVER;
  1325. }
  1326. }
  1327. script->flags = sr.flags;
  1328. /*
  1329.  * If RP did't get saved, generate entry for the return link
  1330.  * register.
  1331.  */
  1332. if (sr.curr.reg[UNW_REG_RP].when >= sr.when_target) {
  1333. sr.curr.reg[UNW_REG_RP].where = UNW_WHERE_BR;
  1334. sr.curr.reg[UNW_REG_RP].when = -1;
  1335. sr.curr.reg[UNW_REG_RP].val = sr.return_link_reg;
  1336. }
  1337. #if UNW_DEBUG
  1338. printk("unwind: state record for func 0x%lx, t=%u:n",
  1339.        table->segment_base + e->start_offset, sr.when_target);
  1340. for (r = sr.curr.reg; r < sr.curr.reg + UNW_NUM_REGS; ++r) {
  1341. if (r->where != UNW_WHERE_NONE || r->when != UNW_WHEN_NEVER) {
  1342. printk("  %s <- ", unw.preg_name[r - sr.curr.reg]);
  1343. switch (r->where) {
  1344.       case UNW_WHERE_GR:     printk("r%lu", r->val); break;
  1345.       case UNW_WHERE_FR:     printk("f%lu", r->val); break;
  1346.       case UNW_WHERE_BR:     printk("b%lu", r->val); break;
  1347.       case UNW_WHERE_SPREL:  printk("[sp+0x%lx]", r->val); break;
  1348.       case UNW_WHERE_PSPREL: printk("[psp+0x%lx]", r->val); break;
  1349.       case UNW_WHERE_NONE:
  1350. printk("%s+0x%lx", unw.preg_name[r - sr.curr.reg], r->val);
  1351. break;
  1352.       default:      printk("BADWHERE(%d)", r->where); break;
  1353. }
  1354. printk("tt%dn", r->when);
  1355. }
  1356. }
  1357. #endif
  1358. STAT(unw.stat.script.parse_time += ia64_get_itc() - parse_start);
  1359. /* translate state record into unwinder instructions: */
  1360. /*
  1361.  * First, set psp if we're dealing with a fixed-size frame;
  1362.  * subsequent instructions may depend on this value.
  1363.  */
  1364. if (sr.when_target > sr.curr.reg[UNW_REG_PSP].when
  1365.     && (sr.curr.reg[UNW_REG_PSP].where == UNW_WHERE_NONE)
  1366.     && sr.curr.reg[UNW_REG_PSP].val != 0) {
  1367. /* new psp is sp plus frame size */
  1368. insn.opc = UNW_INSN_ADD;
  1369. insn.dst = struct_offset(struct unw_frame_info, psp)/8;
  1370. insn.val = sr.curr.reg[UNW_REG_PSP].val; /* frame size */
  1371. script_emit(script, insn);
  1372. }
  1373. /* determine where the primary UNaT is: */
  1374. if (sr.when_target < sr.curr.reg[UNW_REG_PRI_UNAT_GR].when)
  1375. i = UNW_REG_PRI_UNAT_MEM;
  1376. else if (sr.when_target < sr.curr.reg[UNW_REG_PRI_UNAT_MEM].when)
  1377. i = UNW_REG_PRI_UNAT_GR;
  1378. else if (sr.curr.reg[UNW_REG_PRI_UNAT_MEM].when > sr.curr.reg[UNW_REG_PRI_UNAT_GR].when)
  1379. i = UNW_REG_PRI_UNAT_MEM;
  1380. else
  1381. i = UNW_REG_PRI_UNAT_GR;
  1382. compile_reg(&sr, i, script);
  1383. for (i = UNW_REG_BSP; i < UNW_NUM_REGS; ++i)
  1384. compile_reg(&sr, i, script);
  1385. /* free labeled register states & stack: */
  1386. STAT(parse_start = ia64_get_itc());
  1387. for (ls = sr.labeled_states; ls; ls = next) {
  1388. next = ls->next;
  1389. free_state_stack(&ls->saved_state);
  1390. free_labeled_state(ls);
  1391. }
  1392. free_state_stack(&sr.curr);
  1393. STAT(unw.stat.script.parse_time += ia64_get_itc() - parse_start);
  1394. script_finalize(script, &sr);
  1395. STAT(unw.stat.script.build_time += ia64_get_itc() - start);
  1396. return script;
  1397. }
  1398. /*
  1399.  * Apply the unwinding actions represented by OPS and update SR to
  1400.  * reflect the state that existed upon entry to the function that this
  1401.  * unwinder represents.
  1402.  */
  1403. static inline void
  1404. run_script (struct unw_script *script, struct unw_frame_info *state)
  1405. {
  1406. struct unw_insn *ip, *limit, next_insn;
  1407. unsigned long opc, dst, val, off;
  1408. unsigned long *s = (unsigned long *) state;
  1409. STAT(unsigned long start;)
  1410. STAT(++unw.stat.script.runs; start = ia64_get_itc());
  1411. state->flags = script->flags;
  1412. ip = script->insn;
  1413. limit = script->insn + script->count;
  1414. next_insn = *ip;
  1415. while (ip++ < limit) {
  1416. opc = next_insn.opc;
  1417. dst = next_insn.dst;
  1418. val = next_insn.val;
  1419. next_insn = *ip;
  1420.   redo:
  1421. switch (opc) {
  1422.       case UNW_INSN_ADD:
  1423. s[dst] += val;
  1424. break;
  1425.       case UNW_INSN_MOVE2:
  1426. if (!s[val])
  1427. goto lazy_init;
  1428. s[dst+1] = s[val+1];
  1429. s[dst] = s[val];
  1430. break;
  1431.       case UNW_INSN_MOVE:
  1432. if (!s[val])
  1433. goto lazy_init;
  1434. s[dst] = s[val];
  1435. break;
  1436.       case UNW_INSN_MOVE_STACKED:
  1437. s[dst] = (unsigned long) ia64_rse_skip_regs((unsigned long *)state->bsp,
  1438.     val);
  1439. break;
  1440.       case UNW_INSN_ADD_PSP:
  1441. s[dst] = state->psp + val;
  1442. break;
  1443.       case UNW_INSN_ADD_SP:
  1444. s[dst] = state->sp + val;
  1445. break;
  1446.       case UNW_INSN_SETNAT_MEMSTK:
  1447. if (!state->pri_unat_loc)
  1448. state->pri_unat_loc = &state->sw->ar_unat;
  1449. /* register off. is a multiple of 8, so the least 3 bits (type) are 0 */
  1450. s[dst+1] = (*state->pri_unat_loc - s[dst]) | UNW_NAT_MEMSTK;
  1451. break;
  1452.       case UNW_INSN_SETNAT_TYPE:
  1453. s[dst+1] = val;
  1454. break;
  1455.       case UNW_INSN_LOAD:
  1456. #if UNW_DEBUG
  1457. if ((s[val] & (local_cpu_data->unimpl_va_mask | 0x7)) != 0
  1458.     || s[val] < TASK_SIZE)
  1459. {
  1460. debug(1, "unwind: rejecting bad psp=0x%lxn", s[val]);
  1461. break;
  1462. }
  1463. #endif
  1464. s[dst] = *(unsigned long *) s[val];
  1465. break;
  1466. }
  1467. }
  1468. STAT(unw.stat.script.run_time += ia64_get_itc() - start);
  1469. return;
  1470.   lazy_init:
  1471. off = unw.sw_off[val];
  1472. s[val] = (unsigned long) state->sw + off;
  1473. if (off >= struct_offset(struct switch_stack, r4)
  1474.     && off <= struct_offset(struct switch_stack, r7))
  1475. /*
  1476.  * We're initializing a general register: init NaT info, too.  Note that
  1477.  * the offset is a multiple of 8 which gives us the 3 bits needed for
  1478.  * the type field.
  1479.  */
  1480. s[val+1] = (struct_offset(struct switch_stack, ar_unat) - off) | UNW_NAT_MEMSTK;
  1481. goto redo;
  1482. }
  1483. static int
  1484. find_save_locs (struct unw_frame_info *info)
  1485. {
  1486. int have_write_lock = 0;
  1487. struct unw_script *scr;
  1488. if ((info->ip & (local_cpu_data->unimpl_va_mask | 0xf)) || info->ip < TASK_SIZE) {
  1489. /* don't let obviously bad addresses pollute the cache */
  1490. debug(1, "unwind: rejecting bad ip=0x%lxn", info->ip);
  1491. info->rp_loc = 0;
  1492. return -1;
  1493. }
  1494. scr = script_lookup(info);
  1495. if (!scr) {
  1496. scr = build_script(info);
  1497. if (!scr) {
  1498. dprintk("unwind: failed to locate/build unwind script for ip %lxn",
  1499. info->ip);
  1500. return -1;
  1501. }
  1502. have_write_lock = 1;
  1503. }
  1504. info->hint = scr->hint;
  1505. info->prev_script = scr - unw.cache;
  1506. run_script(scr, info);
  1507. if (have_write_lock)
  1508. write_unlock(&scr->lock);
  1509. else
  1510. read_unlock(&scr->lock);
  1511. return 0;
  1512. }
  1513. int
  1514. unw_unwind (struct unw_frame_info *info)
  1515. {
  1516. unsigned long prev_ip, prev_sp, prev_bsp;
  1517. unsigned long ip, pr, num_regs;
  1518. STAT(unsigned long start, flags;)
  1519. int retval;
  1520. STAT(local_irq_save(flags); ++unw.stat.api.unwinds; start = ia64_get_itc());
  1521. prev_ip = info->ip;
  1522. prev_sp = info->sp;
  1523. prev_bsp = info->bsp;
  1524. /* restore the ip */
  1525. if (!info->rp_loc) {
  1526. debug(1, "unwind: failed to locate return link (ip=0x%lx)!n", info->ip);
  1527. STAT(unw.stat.api.unwind_time += ia64_get_itc() - start; local_irq_restore(flags));
  1528. return -1;
  1529. }
  1530. ip = info->ip = *info->rp_loc;
  1531. if (ip < GATE_ADDR + PAGE_SIZE) {
  1532. /*
  1533.  * We don't have unwind info for the gate page, so we consider that part
  1534.  * of user-space for the purpose of unwinding.
  1535.  */
  1536. debug(1, "unwind: reached user-space (ip=0x%lx)n", ip);
  1537. STAT(unw.stat.api.unwind_time += ia64_get_itc() - start; local_irq_restore(flags));
  1538. return -1;
  1539. }
  1540. /* restore the cfm: */
  1541. if (!info->pfs_loc) {
  1542. dprintk("unwind: failed to locate ar.pfs!n");
  1543. STAT(unw.stat.api.unwind_time += ia64_get_itc() - start; local_irq_restore(flags));
  1544. return -1;
  1545. }
  1546. info->cfm_loc = info->pfs_loc;
  1547. /* restore the bsp: */
  1548. pr = info->pr;
  1549. num_regs = 0;
  1550. if ((info->flags & UNW_FLAG_INTERRUPT_FRAME)) {
  1551. if ((pr & (1UL << pNonSys)) != 0)
  1552. num_regs = *info->cfm_loc & 0x7f; /* size of frame */
  1553. info->pfs_loc =
  1554. (unsigned long *) (info->sp + 16 + struct_offset(struct pt_regs, ar_pfs));
  1555. } else
  1556. num_regs = (*info->cfm_loc >> 7) & 0x7f; /* size of locals */
  1557. info->bsp = (unsigned long) ia64_rse_skip_regs((unsigned long *) info->bsp, -num_regs);
  1558. if (info->bsp < info->regstk.limit || info->bsp > info->regstk.top) {
  1559. dprintk("unwind: bsp (0x%lx) out of range [0x%lx-0x%lx]n",
  1560. info->bsp, info->regstk.limit, info->regstk.top);
  1561. STAT(unw.stat.api.unwind_time += ia64_get_itc() - start; local_irq_restore(flags));
  1562. return -1;
  1563. }
  1564. /* restore the sp: */
  1565. info->sp = info->psp;
  1566. if (info->sp < info->memstk.top || info->sp > info->memstk.limit) {
  1567. dprintk("unwind: sp (0x%lx) out of range [0x%lx-0x%lx]n",
  1568. info->sp, info->memstk.top, info->memstk.limit);
  1569. STAT(unw.stat.api.unwind_time += ia64_get_itc() - start; local_irq_restore(flags));
  1570. return -1;
  1571. }
  1572. if (info->ip == prev_ip && info->sp == prev_sp && info->bsp == prev_bsp) {
  1573. dprintk("unwind: ip, sp, bsp remain unchanged; stopping here (ip=0x%lx)n", ip);
  1574. STAT(unw.stat.api.unwind_time += ia64_get_itc() - start; local_irq_restore(flags));
  1575. return -1;
  1576. }
  1577. /* as we unwind, the saved ar.unat becomes the primary unat: */
  1578. info->pri_unat_loc = info->unat_loc;
  1579. /* finally, restore the predicates: */
  1580. unw_get_pr(info, &info->pr);
  1581. retval = find_save_locs(info);
  1582. STAT(unw.stat.api.unwind_time += ia64_get_itc() - start; local_irq_restore(flags));
  1583. return retval;
  1584. }
  1585. int
  1586. unw_unwind_to_user (struct unw_frame_info *info)
  1587. {
  1588. unsigned long ip;
  1589. while (unw_unwind(info) >= 0) {
  1590. if (unw_get_rp(info, &ip) < 0) {
  1591. unw_get_ip(info, &ip);
  1592. dprintk("unwind: failed to read return pointer (ip=0x%lx)n", ip);
  1593. return -1;
  1594. }
  1595. /*
  1596.  * We don't have unwind info for the gate page, so we consider that part
  1597.  * of user-space for the purpose of unwinding.
  1598.  */
  1599. if (ip < GATE_ADDR + PAGE_SIZE)
  1600. return 0;
  1601. }
  1602. unw_get_ip(info, &ip);
  1603. dprintk("unwind: failed to unwind to user-level (ip=0x%lx)n", ip);
  1604. return -1;
  1605. }
  1606. void
  1607. unw_init_frame_info (struct unw_frame_info *info, struct task_struct *t, struct switch_stack *sw)
  1608. {
  1609. unsigned long rbslimit, rbstop, stklimit, stktop, sol;
  1610. STAT(unsigned long start, flags;)
  1611. STAT(local_irq_save(flags); ++unw.stat.api.inits; start = ia64_get_itc());
  1612. /*
  1613.  * Subtle stuff here: we _could_ unwind through the
  1614.  * switch_stack frame but we don't want to do that because it
  1615.  * would be slow as each preserved register would have to be
  1616.  * processed.  Instead, what we do here is zero out the frame
  1617.  * info and start the unwind process at the function that
  1618.  * created the switch_stack frame.  When a preserved value in
  1619.  * switch_stack needs to be accessed, run_script() will
  1620.  * initialize the appropriate pointer on demand.
  1621.  */
  1622. memset(info, 0, sizeof(*info));
  1623. rbslimit = (unsigned long) t + IA64_RBS_OFFSET;
  1624. rbstop   = sw->ar_bspstore;
  1625. if (rbstop - (unsigned long) t >= IA64_STK_OFFSET)
  1626. rbstop = rbslimit;
  1627. stklimit = (unsigned long) t + IA64_STK_OFFSET;
  1628. stktop   = (unsigned long) sw - 16;
  1629. if (stktop <= rbstop)
  1630. stktop = rbstop;
  1631. info->regstk.limit = rbslimit;
  1632. info->regstk.top   = rbstop;
  1633. info->memstk.limit = stklimit;
  1634. info->memstk.top   = stktop;
  1635. info->task = t;
  1636. info->sw  = sw;
  1637. info->sp = info->psp = (unsigned long) (sw + 1) - 16;
  1638. info->cfm_loc = &sw->ar_pfs;
  1639. sol = (*info->cfm_loc >> 7) & 0x7f;
  1640. info->bsp = (unsigned long) ia64_rse_skip_regs((unsigned long *) info->regstk.top, -sol);
  1641. info->ip = sw->b0;
  1642. info->pr = sw->pr;
  1643. find_save_locs(info);
  1644. STAT(unw.stat.api.init_time += ia64_get_itc() - start; local_irq_restore(flags));
  1645. }
  1646. void
  1647. unw_init_from_blocked_task (struct unw_frame_info *info, struct task_struct *t)
  1648. {
  1649. struct switch_stack *sw = (struct switch_stack *) (t->thread.ksp + 16);
  1650. unw_init_frame_info(info, t, sw);
  1651. }
  1652. static void
  1653. init_unwind_table (struct unw_table *table, const char *name, unsigned long segment_base,
  1654.    unsigned long gp, const void *table_start, const void *table_end)
  1655. {
  1656. const struct unw_table_entry *start = table_start, *end = table_end;
  1657. table->name = name;
  1658. table->segment_base = segment_base;
  1659. table->gp = gp;
  1660. table->start = segment_base + start[0].start_offset;
  1661. table->end = segment_base + end[-1].end_offset;
  1662. table->array = start;
  1663. table->length = end - start;
  1664. }
  1665. void *
  1666. unw_add_unwind_table (const char *name, unsigned long segment_base, unsigned long gp,
  1667.       const void *table_start, const void *table_end)
  1668. {
  1669. const struct unw_table_entry *start = table_start, *end = table_end;
  1670. struct unw_table *table;
  1671. unsigned long flags;
  1672. if (end - start <= 0) {
  1673. dprintk("unwind: ignoring attempt to insert empty unwind tablen");
  1674. return 0;
  1675. }
  1676. table = kmalloc(sizeof(*table), GFP_USER);
  1677. if (!table)
  1678. return 0;
  1679. init_unwind_table(table, name, segment_base, gp, table_start, table_end);
  1680. spin_lock_irqsave(&unw.lock, flags);
  1681. {
  1682. /* keep kernel unwind table at the front (it's searched most commonly): */
  1683. table->next = unw.tables->next;
  1684. unw.tables->next = table;
  1685. }
  1686. spin_unlock_irqrestore(&unw.lock, flags);
  1687. return table;
  1688. }
  1689. void
  1690. unw_remove_unwind_table (void *handle)
  1691. {
  1692. struct unw_table *table, *prev;
  1693. struct unw_script *tmp;
  1694. unsigned long flags;
  1695. long index;
  1696. if (!handle) {
  1697. dprintk("unwind: ignoring attempt to remove non-existent unwind tablen");
  1698. return;
  1699. }
  1700. table = handle;
  1701. if (table == &unw.kernel_table) {
  1702. dprintk("unwind: sorry, freeing the kernel's unwind table is a no-can-do!n");
  1703. return;
  1704. }
  1705. spin_lock_irqsave(&unw.lock, flags);
  1706. {
  1707. /* first, delete the table: */
  1708. for (prev = (struct unw_table *) &unw.tables; prev; prev = prev->next)
  1709. if (prev->next == table)
  1710. break;
  1711. if (!prev) {
  1712. dprintk("unwind: failed to find unwind table %pn", (void *) table);
  1713. spin_unlock_irqrestore(&unw.lock, flags);
  1714. return;
  1715. }
  1716. prev->next = table->next;
  1717. }
  1718. spin_unlock_irqrestore(&unw.lock, flags);
  1719. /* next, remove hash table entries for this table */
  1720. for (index = 0; index <= UNW_HASH_SIZE; ++index) {
  1721. tmp = unw.cache + unw.hash[index];
  1722. if (unw.hash[index] >= UNW_CACHE_SIZE
  1723.     || tmp->ip < table->start || tmp->ip >= table->end)
  1724. continue;
  1725. write_lock(&tmp->lock);
  1726. {
  1727. if (tmp->ip >= table->start && tmp->ip < table->end) {
  1728. unw.hash[index] = tmp->coll_chain;
  1729. tmp->ip = 0;
  1730. }
  1731. }
  1732. write_unlock(&tmp->lock);
  1733. }
  1734. kfree(table);
  1735. }
  1736. void
  1737. unw_create_gate_table (void)
  1738. {
  1739. extern char __start_gate_section[], __stop_gate_section[];
  1740. unsigned long *lp, start, end, segbase = unw.kernel_table.segment_base;
  1741. const struct unw_table_entry *entry, *first;
  1742. size_t info_size, size;
  1743. char *info;
  1744. start = (unsigned long) __start_gate_section - segbase;
  1745. end   = (unsigned long) __stop_gate_section - segbase;
  1746. size  = 0;
  1747. first = lookup(&unw.kernel_table, start);
  1748. for (entry = first; entry->start_offset < end; ++entry)
  1749. size += 3*8 + 8 + 8*UNW_LENGTH(*(u64 *) (segbase + entry->info_offset));
  1750. size += 8; /* reserve space for "end of table" marker */
  1751. unw.gate_table = alloc_bootmem(size);
  1752. if (!unw.gate_table) {
  1753. unw.gate_table_size = 0;
  1754. printk("unwind: unable to create unwind data for gate page!n");
  1755. return;
  1756. }
  1757. unw.gate_table_size = size;
  1758. lp = unw.gate_table;
  1759. info = (char *) unw.gate_table + size;
  1760. for (entry = first; entry->start_offset < end; ++entry, lp += 3) {
  1761. info_size = 8 + 8*UNW_LENGTH(*(u64 *) (segbase + entry->info_offset));
  1762. info -= info_size;
  1763. memcpy(info, (char *) segbase + entry->info_offset, info_size);
  1764. lp[0] = entry->start_offset - start + GATE_ADDR; /* start */
  1765. lp[1] = entry->end_offset - start + GATE_ADDR; /* end */
  1766. lp[2] = info - (char *) unw.gate_table; /* info */
  1767. }
  1768. *lp = 0; /* end-of-table marker */
  1769. }
  1770. void
  1771. unw_init (void)
  1772. {
  1773. extern int ia64_unw_start, ia64_unw_end, __gp;
  1774. extern void unw_hash_index_t_is_too_narrow (void);
  1775. long i, off;
  1776. if (8*sizeof(unw_hash_index_t) < UNW_LOG_HASH_SIZE)
  1777. unw_hash_index_t_is_too_narrow();
  1778. unw.sw_off[unw.preg_index[UNW_REG_PRI_UNAT_GR]] = SW(AR_UNAT);
  1779. unw.sw_off[unw.preg_index[UNW_REG_BSPSTORE]] = SW(AR_BSPSTORE);
  1780. unw.sw_off[unw.preg_index[UNW_REG_PFS]] = SW(AR_UNAT);
  1781. unw.sw_off[unw.preg_index[UNW_REG_RP]] = SW(B0);
  1782. unw.sw_off[unw.preg_index[UNW_REG_UNAT]] = SW(AR_UNAT);
  1783. unw.sw_off[unw.preg_index[UNW_REG_PR]] = SW(PR);
  1784. unw.sw_off[unw.preg_index[UNW_REG_LC]] = SW(AR_LC);
  1785. unw.sw_off[unw.preg_index[UNW_REG_FPSR]] = SW(AR_FPSR);
  1786. for (i = UNW_REG_R4, off = SW(R4); i <= UNW_REG_R7; ++i, off += 8)
  1787. unw.sw_off[unw.preg_index[i]] = off;
  1788. for (i = UNW_REG_B1, off = SW(B1); i <= UNW_REG_B5; ++i, off += 8)
  1789. unw.sw_off[unw.preg_index[i]] = off;
  1790. for (i = UNW_REG_F2, off = SW(F2); i <= UNW_REG_F5; ++i, off += 16)
  1791. unw.sw_off[unw.preg_index[i]] = off;
  1792. for (i = UNW_REG_F16, off = SW(F16); i <= UNW_REG_F31; ++i, off += 16)
  1793. unw.sw_off[unw.preg_index[i]] = off;
  1794. for (i = 0; i < UNW_CACHE_SIZE; ++i) {
  1795. if (i > 0)
  1796. unw.cache[i].lru_chain = (i - 1);
  1797. unw.cache[i].coll_chain = -1;
  1798. unw.cache[i].lock = RW_LOCK_UNLOCKED;
  1799. }
  1800. unw.lru_head = UNW_CACHE_SIZE - 1;
  1801. unw.lru_tail = 0;
  1802. init_unwind_table(&unw.kernel_table, "kernel", KERNEL_START, (unsigned long) &__gp,
  1803.   &ia64_unw_start, &ia64_unw_end);
  1804. }
  1805. /*
  1806.  * This system call copies the unwind data into the buffer pointed to by BUF and returns
  1807.  * the size of the unwind data.  If BUF_SIZE is smaller than the size of the unwind data
  1808.  * or if BUF is NULL, nothing is copied, but the system call still returns the size of the
  1809.  * unwind data.
  1810.  *
  1811.  * The first portion of the unwind data contains an unwind table and rest contains the
  1812.  * associated unwind info (in no particular order).  The unwind table consists of a table
  1813.  * of entries of the form:
  1814.  *
  1815.  * u64 start; (64-bit address of start of function)
  1816.  * u64 end; (64-bit address of start of function)
  1817.  * u64 info; (BUF-relative offset to unwind info)
  1818.  *
  1819.  * The end of the unwind table is indicated by an entry with a START address of zero.
  1820.  *
  1821.  * Please see the IA-64 Software Conventions and Runtime Architecture manual for details
  1822.  * on the format of the unwind info.
  1823.  *
  1824.  * ERRORS
  1825.  * EFAULT BUF points outside your accessible address space.
  1826.  */
  1827. asmlinkage long
  1828. sys_getunwind (void *buf, size_t buf_size)
  1829. {
  1830. if (buf && buf_size >= unw.gate_table_size)
  1831. if (copy_to_user(buf, unw.gate_table, unw.gate_table_size) != 0)
  1832. return -EFAULT;
  1833. return unw.gate_table_size;
  1834. }