unwind.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305
  1. /*
  2. * Copyright (C) 2002-2006 Novell, Inc.
  3. * Jan Beulich <jbeulich@novell.com>
  4. * This code is released under version 2 of the GNU GPL.
  5. *
  6. * A simple API for unwinding kernel stacks. This is used for
  7. * debugging and error reporting purposes. The kernel doesn't need
  8. * full-blown stack unwinding with all the bells and whistles, so there
  9. * is not much point in implementing the full Dwarf2 unwind API.
  10. */
  11. #include <linux/unwind.h>
  12. #include <linux/module.h>
  13. #include <linux/bootmem.h>
  14. #include <linux/sort.h>
  15. #include <linux/stop_machine.h>
  16. #include <linux/uaccess.h>
  17. #include <asm/sections.h>
  18. #include <asm/uaccess.h>
  19. #include <asm/unaligned.h>
  20. extern const char __start_unwind[], __end_unwind[];
  21. extern const u8 __start_unwind_hdr[], __end_unwind_hdr[];
  22. #define MAX_STACK_DEPTH 8
  23. #define EXTRA_INFO(f) { \
  24. BUILD_BUG_ON_ZERO(offsetof(struct unwind_frame_info, f) \
  25. % FIELD_SIZEOF(struct unwind_frame_info, f)) \
  26. + offsetof(struct unwind_frame_info, f) \
  27. / FIELD_SIZEOF(struct unwind_frame_info, f), \
  28. FIELD_SIZEOF(struct unwind_frame_info, f) \
  29. }
  30. #define PTREGS_INFO(f) EXTRA_INFO(regs.f)
  31. static const struct {
  32. unsigned offs:BITS_PER_LONG / 2;
  33. unsigned width:BITS_PER_LONG / 2;
  34. } reg_info[] = {
  35. UNW_REGISTER_INFO
  36. };
  37. #undef PTREGS_INFO
  38. #undef EXTRA_INFO
  39. #ifndef REG_INVALID
  40. #define REG_INVALID(r) (reg_info[r].width == 0)
  41. #endif
  42. #define DW_CFA_nop 0x00
  43. #define DW_CFA_set_loc 0x01
  44. #define DW_CFA_advance_loc1 0x02
  45. #define DW_CFA_advance_loc2 0x03
  46. #define DW_CFA_advance_loc4 0x04
  47. #define DW_CFA_offset_extended 0x05
  48. #define DW_CFA_restore_extended 0x06
  49. #define DW_CFA_undefined 0x07
  50. #define DW_CFA_same_value 0x08
  51. #define DW_CFA_register 0x09
  52. #define DW_CFA_remember_state 0x0a
  53. #define DW_CFA_restore_state 0x0b
  54. #define DW_CFA_def_cfa 0x0c
  55. #define DW_CFA_def_cfa_register 0x0d
  56. #define DW_CFA_def_cfa_offset 0x0e
  57. #define DW_CFA_def_cfa_expression 0x0f
  58. #define DW_CFA_expression 0x10
  59. #define DW_CFA_offset_extended_sf 0x11
  60. #define DW_CFA_def_cfa_sf 0x12
  61. #define DW_CFA_def_cfa_offset_sf 0x13
  62. #define DW_CFA_val_offset 0x14
  63. #define DW_CFA_val_offset_sf 0x15
  64. #define DW_CFA_val_expression 0x16
  65. #define DW_CFA_lo_user 0x1c
  66. #define DW_CFA_GNU_window_save 0x2d
  67. #define DW_CFA_GNU_args_size 0x2e
  68. #define DW_CFA_GNU_negative_offset_extended 0x2f
  69. #define DW_CFA_hi_user 0x3f
  70. #define DW_EH_PE_FORM 0x07
  71. #define DW_EH_PE_native 0x00
  72. #define DW_EH_PE_leb128 0x01
  73. #define DW_EH_PE_data2 0x02
  74. #define DW_EH_PE_data4 0x03
  75. #define DW_EH_PE_data8 0x04
  76. #define DW_EH_PE_signed 0x08
  77. #define DW_EH_PE_ADJUST 0x70
  78. #define DW_EH_PE_abs 0x00
  79. #define DW_EH_PE_pcrel 0x10
  80. #define DW_EH_PE_textrel 0x20
  81. #define DW_EH_PE_datarel 0x30
  82. #define DW_EH_PE_funcrel 0x40
  83. #define DW_EH_PE_aligned 0x50
  84. #define DW_EH_PE_indirect 0x80
  85. #define DW_EH_PE_omit 0xff
  86. typedef unsigned long uleb128_t;
  87. typedef signed long sleb128_t;
  88. #define sleb128abs __builtin_labs
  89. static struct unwind_table {
  90. struct {
  91. unsigned long pc;
  92. unsigned long range;
  93. } core, init;
  94. const void *address;
  95. unsigned long size;
  96. const unsigned char *header;
  97. unsigned long hdrsz;
  98. struct unwind_table *link;
  99. const char *name;
  100. } root_table;
  101. struct unwind_item {
  102. enum item_location {
  103. Nowhere,
  104. Memory,
  105. Register,
  106. Value
  107. } where;
  108. uleb128_t value;
  109. };
  110. struct unwind_state {
  111. uleb128_t loc, org;
  112. const u8 *cieStart, *cieEnd;
  113. uleb128_t codeAlign;
  114. sleb128_t dataAlign;
  115. struct cfa {
  116. uleb128_t reg, offs;
  117. } cfa;
  118. struct unwind_item regs[ARRAY_SIZE(reg_info)];
  119. unsigned stackDepth:8;
  120. unsigned version:8;
  121. const u8 *label;
  122. const u8 *stack[MAX_STACK_DEPTH];
  123. };
  124. static const struct cfa badCFA = { ARRAY_SIZE(reg_info), 1 };
  125. static unsigned unwind_debug;
  126. static int __init unwind_debug_setup(char *s)
  127. {
  128. unwind_debug = simple_strtoul(s, NULL, 0);
  129. return 1;
  130. }
  131. __setup("unwind_debug=", unwind_debug_setup);
  132. #define dprintk(lvl, fmt, args...) \
  133. ((void)(lvl > unwind_debug \
  134. || printk(KERN_DEBUG "unwind: " fmt "\n", ##args)))
  135. static struct unwind_table *find_table(unsigned long pc)
  136. {
  137. struct unwind_table *table;
  138. for (table = &root_table; table; table = table->link)
  139. if ((pc >= table->core.pc
  140. && pc < table->core.pc + table->core.range)
  141. || (pc >= table->init.pc
  142. && pc < table->init.pc + table->init.range))
  143. break;
  144. return table;
  145. }
  146. static unsigned long read_pointer(const u8 **pLoc,
  147. const void *end,
  148. signed ptrType,
  149. unsigned long text_base,
  150. unsigned long data_base);
  151. static void init_unwind_table(struct unwind_table *table,
  152. const char *name,
  153. const void *core_start,
  154. unsigned long core_size,
  155. const void *init_start,
  156. unsigned long init_size,
  157. const void *table_start,
  158. unsigned long table_size,
  159. const u8 *header_start,
  160. unsigned long header_size)
  161. {
  162. const u8 *ptr = header_start + 4;
  163. const u8 *end = header_start + header_size;
  164. table->core.pc = (unsigned long)core_start;
  165. table->core.range = core_size;
  166. table->init.pc = (unsigned long)init_start;
  167. table->init.range = init_size;
  168. table->address = table_start;
  169. table->size = table_size;
  170. /* See if the linker provided table looks valid. */
  171. if (header_size <= 4
  172. || header_start[0] != 1
  173. || (void *)read_pointer(&ptr, end, header_start[1], 0, 0)
  174. != table_start
  175. || !read_pointer(&ptr, end, header_start[2], 0, 0)
  176. || !read_pointer(&ptr, end, header_start[3], 0,
  177. (unsigned long)header_start)
  178. || !read_pointer(&ptr, end, header_start[3], 0,
  179. (unsigned long)header_start))
  180. header_start = NULL;
  181. table->hdrsz = header_size;
  182. smp_wmb();
  183. table->header = header_start;
  184. table->link = NULL;
  185. table->name = name;
  186. }
  187. void __init unwind_init(void)
  188. {
  189. init_unwind_table(&root_table, "kernel",
  190. _text, _end - _text,
  191. NULL, 0,
  192. __start_unwind, __end_unwind - __start_unwind,
  193. __start_unwind_hdr, __end_unwind_hdr - __start_unwind_hdr);
  194. }
  195. static const u32 bad_cie, not_fde;
  196. static const u32 *cie_for_fde(const u32 *fde, const struct unwind_table *);
  197. static signed fde_pointer_type(const u32 *cie);
  198. struct eh_frame_hdr_table_entry {
  199. unsigned long start, fde;
  200. };
  201. static int cmp_eh_frame_hdr_table_entries(const void *p1, const void *p2)
  202. {
  203. const struct eh_frame_hdr_table_entry *e1 = p1;
  204. const struct eh_frame_hdr_table_entry *e2 = p2;
  205. return (e1->start > e2->start) - (e1->start < e2->start);
  206. }
  207. static void swap_eh_frame_hdr_table_entries(void *p1, void *p2, int size)
  208. {
  209. struct eh_frame_hdr_table_entry *e1 = p1;
  210. struct eh_frame_hdr_table_entry *e2 = p2;
  211. unsigned long v;
  212. v = e1->start;
  213. e1->start = e2->start;
  214. e2->start = v;
  215. v = e1->fde;
  216. e1->fde = e2->fde;
  217. e2->fde = v;
  218. }
  219. static void __init setup_unwind_table(struct unwind_table *table,
  220. void *(*alloc)(unsigned long))
  221. {
  222. const u8 *ptr;
  223. unsigned long tableSize = table->size, hdrSize;
  224. unsigned n;
  225. const u32 *fde;
  226. struct {
  227. u8 version;
  228. u8 eh_frame_ptr_enc;
  229. u8 fde_count_enc;
  230. u8 table_enc;
  231. unsigned long eh_frame_ptr;
  232. unsigned int fde_count;
  233. struct eh_frame_hdr_table_entry table[];
  234. } __attribute__((__packed__)) *header;
  235. if (table->header)
  236. return;
  237. if (table->hdrsz)
  238. printk(KERN_WARNING ".eh_frame_hdr for '%s' present but unusable\n",
  239. table->name);
  240. if (tableSize & (sizeof(*fde) - 1))
  241. return;
  242. for (fde = table->address, n = 0;
  243. tableSize > sizeof(*fde) && tableSize - sizeof(*fde) >= *fde;
  244. tableSize -= sizeof(*fde) + *fde, fde += 1 + *fde / sizeof(*fde)) {
  245. const u32 *cie = cie_for_fde(fde, table);
  246. signed ptrType;
  247. if (cie == &not_fde)
  248. continue;
  249. if (cie == NULL
  250. || cie == &bad_cie
  251. || (ptrType = fde_pointer_type(cie)) < 0)
  252. return;
  253. ptr = (const u8 *)(fde + 2);
  254. if (!read_pointer(&ptr,
  255. (const u8 *)(fde + 1) + *fde,
  256. ptrType, 0, 0))
  257. return;
  258. ++n;
  259. }
  260. if (tableSize || !n)
  261. return;
  262. hdrSize = 4 + sizeof(unsigned long) + sizeof(unsigned int)
  263. + 2 * n * sizeof(unsigned long);
  264. dprintk(2, "Binary lookup table size for %s: %lu bytes", table->name, hdrSize);
  265. header = alloc(hdrSize);
  266. if (!header)
  267. return;
  268. header->version = 1;
  269. header->eh_frame_ptr_enc = DW_EH_PE_abs|DW_EH_PE_native;
  270. header->fde_count_enc = DW_EH_PE_abs|DW_EH_PE_data4;
  271. header->table_enc = DW_EH_PE_abs|DW_EH_PE_native;
  272. put_unaligned((unsigned long)table->address, &header->eh_frame_ptr);
  273. BUILD_BUG_ON(offsetof(typeof(*header), fde_count)
  274. % __alignof(typeof(header->fde_count)));
  275. header->fde_count = n;
  276. BUILD_BUG_ON(offsetof(typeof(*header), table)
  277. % __alignof(typeof(*header->table)));
  278. for (fde = table->address, tableSize = table->size, n = 0;
  279. tableSize;
  280. tableSize -= sizeof(*fde) + *fde, fde += 1 + *fde / sizeof(*fde)) {
  281. const u32 *cie = fde + 1 - fde[1] / sizeof(*fde);
  282. if (!fde[1])
  283. continue; /* this is a CIE */
  284. ptr = (const u8 *)(fde + 2);
  285. header->table[n].start = read_pointer(&ptr,
  286. (const u8 *)(fde + 1) + *fde,
  287. fde_pointer_type(cie), 0, 0);
  288. header->table[n].fde = (unsigned long)fde;
  289. ++n;
  290. }
  291. WARN_ON(n != header->fde_count);
  292. sort(header->table,
  293. n,
  294. sizeof(*header->table),
  295. cmp_eh_frame_hdr_table_entries,
  296. swap_eh_frame_hdr_table_entries);
  297. table->hdrsz = hdrSize;
  298. smp_wmb();
  299. table->header = (const void *)header;
  300. }
  301. static void *__init balloc(unsigned long sz)
  302. {
  303. return __alloc_bootmem_nopanic(sz,
  304. sizeof(unsigned int),
  305. __pa(MAX_DMA_ADDRESS));
  306. }
  307. void __init unwind_setup(void)
  308. {
  309. setup_unwind_table(&root_table, balloc);
  310. }
  311. #ifdef CONFIG_MODULES
  312. static struct unwind_table *last_table;
  313. /* Must be called with module_mutex held. */
  314. void *unwind_add_table(struct module *module,
  315. const void *table_start,
  316. unsigned long table_size)
  317. {
  318. struct unwind_table *table;
  319. if (table_size <= 0)
  320. return NULL;
  321. table = kmalloc(sizeof(*table), GFP_KERNEL);
  322. if (!table)
  323. return NULL;
  324. init_unwind_table(table, module->name,
  325. module->module_core, module->core_size,
  326. module->module_init, module->init_size,
  327. table_start, table_size,
  328. NULL, 0);
  329. if (last_table)
  330. last_table->link = table;
  331. else
  332. root_table.link = table;
  333. last_table = table;
  334. return table;
  335. }
  336. struct unlink_table_info
  337. {
  338. struct unwind_table *table;
  339. int init_only;
  340. };
  341. static int unlink_table(void *arg)
  342. {
  343. struct unlink_table_info *info = arg;
  344. struct unwind_table *table = info->table, *prev;
  345. for (prev = &root_table; prev->link && prev->link != table; prev = prev->link)
  346. ;
  347. if (prev->link) {
  348. if (info->init_only) {
  349. table->init.pc = 0;
  350. table->init.range = 0;
  351. info->table = NULL;
  352. } else {
  353. prev->link = table->link;
  354. if (!prev->link)
  355. last_table = prev;
  356. }
  357. } else
  358. info->table = NULL;
  359. return 0;
  360. }
  361. /* Must be called with module_mutex held. */
  362. void unwind_remove_table(void *handle, int init_only)
  363. {
  364. struct unwind_table *table = handle;
  365. struct unlink_table_info info;
  366. if (!table || table == &root_table)
  367. return;
  368. if (init_only && table == last_table) {
  369. table->init.pc = 0;
  370. table->init.range = 0;
  371. return;
  372. }
  373. info.table = table;
  374. info.init_only = init_only;
  375. stop_machine_run(unlink_table, &info, NR_CPUS);
  376. if (info.table)
  377. kfree(table);
  378. }
  379. #endif /* CONFIG_MODULES */
  380. static uleb128_t get_uleb128(const u8 **pcur, const u8 *end)
  381. {
  382. const u8 *cur = *pcur;
  383. uleb128_t value;
  384. unsigned shift;
  385. for (shift = 0, value = 0; cur < end; shift += 7) {
  386. if (shift + 7 > 8 * sizeof(value)
  387. && (*cur & 0x7fU) >= (1U << (8 * sizeof(value) - shift))) {
  388. cur = end + 1;
  389. break;
  390. }
  391. value |= (uleb128_t)(*cur & 0x7f) << shift;
  392. if (!(*cur++ & 0x80))
  393. break;
  394. }
  395. *pcur = cur;
  396. return value;
  397. }
  398. static sleb128_t get_sleb128(const u8 **pcur, const u8 *end)
  399. {
  400. const u8 *cur = *pcur;
  401. sleb128_t value;
  402. unsigned shift;
  403. for (shift = 0, value = 0; cur < end; shift += 7) {
  404. if (shift + 7 > 8 * sizeof(value)
  405. && (*cur & 0x7fU) >= (1U << (8 * sizeof(value) - shift))) {
  406. cur = end + 1;
  407. break;
  408. }
  409. value |= (sleb128_t)(*cur & 0x7f) << shift;
  410. if (!(*cur & 0x80)) {
  411. value |= -(*cur++ & 0x40) << shift;
  412. break;
  413. }
  414. }
  415. *pcur = cur;
  416. return value;
  417. }
  418. static const u32 *cie_for_fde(const u32 *fde, const struct unwind_table *table)
  419. {
  420. const u32 *cie;
  421. if (!*fde || (*fde & (sizeof(*fde) - 1)))
  422. return &bad_cie;
  423. if (!fde[1])
  424. return &not_fde; /* this is a CIE */
  425. if ((fde[1] & (sizeof(*fde) - 1))
  426. || fde[1] > (unsigned long)(fde + 1) - (unsigned long)table->address)
  427. return NULL; /* this is not a valid FDE */
  428. cie = fde + 1 - fde[1] / sizeof(*fde);
  429. if (*cie <= sizeof(*cie) + 4
  430. || *cie >= fde[1] - sizeof(*fde)
  431. || (*cie & (sizeof(*cie) - 1))
  432. || cie[1])
  433. return NULL; /* this is not a (valid) CIE */
  434. return cie;
  435. }
  436. static unsigned long read_pointer(const u8 **pLoc,
  437. const void *end,
  438. signed ptrType,
  439. unsigned long text_base,
  440. unsigned long data_base)
  441. {
  442. unsigned long value = 0;
  443. union {
  444. const u8 *p8;
  445. const u16 *p16u;
  446. const s16 *p16s;
  447. const u32 *p32u;
  448. const s32 *p32s;
  449. const unsigned long *pul;
  450. } ptr;
  451. if (ptrType < 0 || ptrType == DW_EH_PE_omit) {
  452. dprintk(1, "Invalid pointer encoding %02X (%p,%p).", ptrType, *pLoc, end);
  453. return 0;
  454. }
  455. ptr.p8 = *pLoc;
  456. switch(ptrType & DW_EH_PE_FORM) {
  457. case DW_EH_PE_data2:
  458. if (end < (const void *)(ptr.p16u + 1)) {
  459. dprintk(1, "Data16 overrun (%p,%p).", ptr.p8, end);
  460. return 0;
  461. }
  462. if(ptrType & DW_EH_PE_signed)
  463. value = get_unaligned(ptr.p16s++);
  464. else
  465. value = get_unaligned(ptr.p16u++);
  466. break;
  467. case DW_EH_PE_data4:
  468. #ifdef CONFIG_64BIT
  469. if (end < (const void *)(ptr.p32u + 1)) {
  470. dprintk(1, "Data32 overrun (%p,%p).", ptr.p8, end);
  471. return 0;
  472. }
  473. if(ptrType & DW_EH_PE_signed)
  474. value = get_unaligned(ptr.p32s++);
  475. else
  476. value = get_unaligned(ptr.p32u++);
  477. break;
  478. case DW_EH_PE_data8:
  479. BUILD_BUG_ON(sizeof(u64) != sizeof(value));
  480. #else
  481. BUILD_BUG_ON(sizeof(u32) != sizeof(value));
  482. #endif
  483. case DW_EH_PE_native:
  484. if (end < (const void *)(ptr.pul + 1)) {
  485. dprintk(1, "DataUL overrun (%p,%p).", ptr.p8, end);
  486. return 0;
  487. }
  488. value = get_unaligned(ptr.pul++);
  489. break;
  490. case DW_EH_PE_leb128:
  491. BUILD_BUG_ON(sizeof(uleb128_t) > sizeof(value));
  492. value = ptrType & DW_EH_PE_signed
  493. ? get_sleb128(&ptr.p8, end)
  494. : get_uleb128(&ptr.p8, end);
  495. if ((const void *)ptr.p8 > end) {
  496. dprintk(1, "DataLEB overrun (%p,%p).", ptr.p8, end);
  497. return 0;
  498. }
  499. break;
  500. default:
  501. dprintk(2, "Cannot decode pointer type %02X (%p,%p).",
  502. ptrType, ptr.p8, end);
  503. return 0;
  504. }
  505. switch(ptrType & DW_EH_PE_ADJUST) {
  506. case DW_EH_PE_abs:
  507. break;
  508. case DW_EH_PE_pcrel:
  509. value += (unsigned long)*pLoc;
  510. break;
  511. case DW_EH_PE_textrel:
  512. if (likely(text_base)) {
  513. value += text_base;
  514. break;
  515. }
  516. dprintk(2, "Text-relative encoding %02X (%p,%p), but zero text base.",
  517. ptrType, *pLoc, end);
  518. return 0;
  519. case DW_EH_PE_datarel:
  520. if (likely(data_base)) {
  521. value += data_base;
  522. break;
  523. }
  524. dprintk(2, "Data-relative encoding %02X (%p,%p), but zero data base.",
  525. ptrType, *pLoc, end);
  526. return 0;
  527. default:
  528. dprintk(2, "Cannot adjust pointer type %02X (%p,%p).",
  529. ptrType, *pLoc, end);
  530. return 0;
  531. }
  532. if ((ptrType & DW_EH_PE_indirect)
  533. && probe_kernel_address((unsigned long *)value, value)) {
  534. dprintk(1, "Cannot read indirect value %lx (%p,%p).",
  535. value, *pLoc, end);
  536. return 0;
  537. }
  538. *pLoc = ptr.p8;
  539. return value;
  540. }
  541. static signed fde_pointer_type(const u32 *cie)
  542. {
  543. const u8 *ptr = (const u8 *)(cie + 2);
  544. unsigned version = *ptr;
  545. if (version != 1)
  546. return -1; /* unsupported */
  547. if (*++ptr) {
  548. const char *aug;
  549. const u8 *end = (const u8 *)(cie + 1) + *cie;
  550. uleb128_t len;
  551. /* check if augmentation size is first (and thus present) */
  552. if (*ptr != 'z')
  553. return -1;
  554. /* check if augmentation string is nul-terminated */
  555. if ((ptr = memchr(aug = (const void *)ptr, 0, end - ptr)) == NULL)
  556. return -1;
  557. ++ptr; /* skip terminator */
  558. get_uleb128(&ptr, end); /* skip code alignment */
  559. get_sleb128(&ptr, end); /* skip data alignment */
  560. /* skip return address column */
  561. version <= 1 ? (void)++ptr : (void)get_uleb128(&ptr, end);
  562. len = get_uleb128(&ptr, end); /* augmentation length */
  563. if (ptr + len < ptr || ptr + len > end)
  564. return -1;
  565. end = ptr + len;
  566. while (*++aug) {
  567. if (ptr >= end)
  568. return -1;
  569. switch(*aug) {
  570. case 'L':
  571. ++ptr;
  572. break;
  573. case 'P': {
  574. signed ptrType = *ptr++;
  575. if (!read_pointer(&ptr, end, ptrType, 0, 0)
  576. || ptr > end)
  577. return -1;
  578. }
  579. break;
  580. case 'R':
  581. return *ptr;
  582. default:
  583. return -1;
  584. }
  585. }
  586. }
  587. return DW_EH_PE_native|DW_EH_PE_abs;
  588. }
  589. static int advance_loc(unsigned long delta, struct unwind_state *state)
  590. {
  591. state->loc += delta * state->codeAlign;
  592. return delta > 0;
  593. }
  594. static void set_rule(uleb128_t reg,
  595. enum item_location where,
  596. uleb128_t value,
  597. struct unwind_state *state)
  598. {
  599. if (reg < ARRAY_SIZE(state->regs)) {
  600. state->regs[reg].where = where;
  601. state->regs[reg].value = value;
  602. }
  603. }
  604. static int processCFI(const u8 *start,
  605. const u8 *end,
  606. unsigned long targetLoc,
  607. signed ptrType,
  608. struct unwind_state *state)
  609. {
  610. union {
  611. const u8 *p8;
  612. const u16 *p16;
  613. const u32 *p32;
  614. } ptr;
  615. int result = 1;
  616. if (start != state->cieStart) {
  617. state->loc = state->org;
  618. result = processCFI(state->cieStart, state->cieEnd, 0, ptrType, state);
  619. if (targetLoc == 0 && state->label == NULL)
  620. return result;
  621. }
  622. for (ptr.p8 = start; result && ptr.p8 < end; ) {
  623. switch(*ptr.p8 >> 6) {
  624. uleb128_t value;
  625. case 0:
  626. switch(*ptr.p8++) {
  627. case DW_CFA_nop:
  628. break;
  629. case DW_CFA_set_loc:
  630. state->loc = read_pointer(&ptr.p8, end, ptrType, 0, 0);
  631. if (state->loc == 0)
  632. result = 0;
  633. break;
  634. case DW_CFA_advance_loc1:
  635. result = ptr.p8 < end && advance_loc(*ptr.p8++, state);
  636. break;
  637. case DW_CFA_advance_loc2:
  638. result = ptr.p8 <= end + 2
  639. && advance_loc(*ptr.p16++, state);
  640. break;
  641. case DW_CFA_advance_loc4:
  642. result = ptr.p8 <= end + 4
  643. && advance_loc(*ptr.p32++, state);
  644. break;
  645. case DW_CFA_offset_extended:
  646. value = get_uleb128(&ptr.p8, end);
  647. set_rule(value, Memory, get_uleb128(&ptr.p8, end), state);
  648. break;
  649. case DW_CFA_val_offset:
  650. value = get_uleb128(&ptr.p8, end);
  651. set_rule(value, Value, get_uleb128(&ptr.p8, end), state);
  652. break;
  653. case DW_CFA_offset_extended_sf:
  654. value = get_uleb128(&ptr.p8, end);
  655. set_rule(value, Memory, get_sleb128(&ptr.p8, end), state);
  656. break;
  657. case DW_CFA_val_offset_sf:
  658. value = get_uleb128(&ptr.p8, end);
  659. set_rule(value, Value, get_sleb128(&ptr.p8, end), state);
  660. break;
  661. case DW_CFA_restore_extended:
  662. case DW_CFA_undefined:
  663. case DW_CFA_same_value:
  664. set_rule(get_uleb128(&ptr.p8, end), Nowhere, 0, state);
  665. break;
  666. case DW_CFA_register:
  667. value = get_uleb128(&ptr.p8, end);
  668. set_rule(value,
  669. Register,
  670. get_uleb128(&ptr.p8, end), state);
  671. break;
  672. case DW_CFA_remember_state:
  673. if (ptr.p8 == state->label) {
  674. state->label = NULL;
  675. return 1;
  676. }
  677. if (state->stackDepth >= MAX_STACK_DEPTH) {
  678. dprintk(1, "State stack overflow (%p,%p).", ptr.p8, end);
  679. return 0;
  680. }
  681. state->stack[state->stackDepth++] = ptr.p8;
  682. break;
  683. case DW_CFA_restore_state:
  684. if (state->stackDepth) {
  685. const uleb128_t loc = state->loc;
  686. const u8 *label = state->label;
  687. state->label = state->stack[state->stackDepth - 1];
  688. memcpy(&state->cfa, &badCFA, sizeof(state->cfa));
  689. memset(state->regs, 0, sizeof(state->regs));
  690. state->stackDepth = 0;
  691. result = processCFI(start, end, 0, ptrType, state);
  692. state->loc = loc;
  693. state->label = label;
  694. } else {
  695. dprintk(1, "State stack underflow (%p,%p).", ptr.p8, end);
  696. return 0;
  697. }
  698. break;
  699. case DW_CFA_def_cfa:
  700. state->cfa.reg = get_uleb128(&ptr.p8, end);
  701. /*nobreak*/
  702. case DW_CFA_def_cfa_offset:
  703. state->cfa.offs = get_uleb128(&ptr.p8, end);
  704. break;
  705. case DW_CFA_def_cfa_sf:
  706. state->cfa.reg = get_uleb128(&ptr.p8, end);
  707. /*nobreak*/
  708. case DW_CFA_def_cfa_offset_sf:
  709. state->cfa.offs = get_sleb128(&ptr.p8, end)
  710. * state->dataAlign;
  711. break;
  712. case DW_CFA_def_cfa_register:
  713. state->cfa.reg = get_uleb128(&ptr.p8, end);
  714. break;
  715. /*todo case DW_CFA_def_cfa_expression: */
  716. /*todo case DW_CFA_expression: */
  717. /*todo case DW_CFA_val_expression: */
  718. case DW_CFA_GNU_args_size:
  719. get_uleb128(&ptr.p8, end);
  720. break;
  721. case DW_CFA_GNU_negative_offset_extended:
  722. value = get_uleb128(&ptr.p8, end);
  723. set_rule(value,
  724. Memory,
  725. (uleb128_t)0 - get_uleb128(&ptr.p8, end), state);
  726. break;
  727. case DW_CFA_GNU_window_save:
  728. default:
  729. dprintk(1, "Unrecognized CFI op %02X (%p,%p).", ptr.p8[-1], ptr.p8 - 1, end);
  730. result = 0;
  731. break;
  732. }
  733. break;
  734. case 1:
  735. result = advance_loc(*ptr.p8++ & 0x3f, state);
  736. break;
  737. case 2:
  738. value = *ptr.p8++ & 0x3f;
  739. set_rule(value, Memory, get_uleb128(&ptr.p8, end), state);
  740. break;
  741. case 3:
  742. set_rule(*ptr.p8++ & 0x3f, Nowhere, 0, state);
  743. break;
  744. }
  745. if (ptr.p8 > end) {
  746. dprintk(1, "Data overrun (%p,%p).", ptr.p8, end);
  747. result = 0;
  748. }
  749. if (result && targetLoc != 0 && targetLoc < state->loc)
  750. return 1;
  751. }
  752. if (result && ptr.p8 < end)
  753. dprintk(1, "Data underrun (%p,%p).", ptr.p8, end);
  754. return result
  755. && ptr.p8 == end
  756. && (targetLoc == 0
  757. || (/*todo While in theory this should apply, gcc in practice omits
  758. everything past the function prolog, and hence the location
  759. never reaches the end of the function.
  760. targetLoc < state->loc &&*/ state->label == NULL));
  761. }
  762. /* Unwind to previous to frame. Returns 0 if successful, negative
  763. * number in case of an error. */
  764. int unwind(struct unwind_frame_info *frame)
  765. {
  766. #define FRAME_REG(r, t) (((t *)frame)[reg_info[r].offs])
  767. const u32 *fde = NULL, *cie = NULL;
  768. const u8 *ptr = NULL, *end = NULL;
  769. unsigned long pc = UNW_PC(frame) - frame->call_frame, sp;
  770. unsigned long startLoc = 0, endLoc = 0, cfa;
  771. unsigned i;
  772. signed ptrType = -1;
  773. uleb128_t retAddrReg = 0;
  774. const struct unwind_table *table;
  775. struct unwind_state state;
  776. if (UNW_PC(frame) == 0)
  777. return -EINVAL;
  778. if ((table = find_table(pc)) != NULL
  779. && !(table->size & (sizeof(*fde) - 1))) {
  780. const u8 *hdr = table->header;
  781. unsigned long tableSize;
  782. smp_rmb();
  783. if (hdr && hdr[0] == 1) {
  784. switch(hdr[3] & DW_EH_PE_FORM) {
  785. case DW_EH_PE_native: tableSize = sizeof(unsigned long); break;
  786. case DW_EH_PE_data2: tableSize = 2; break;
  787. case DW_EH_PE_data4: tableSize = 4; break;
  788. case DW_EH_PE_data8: tableSize = 8; break;
  789. default: tableSize = 0; break;
  790. }
  791. ptr = hdr + 4;
  792. end = hdr + table->hdrsz;
  793. if (tableSize
  794. && read_pointer(&ptr, end, hdr[1], 0, 0)
  795. == (unsigned long)table->address
  796. && (i = read_pointer(&ptr, end, hdr[2], 0, 0)) > 0
  797. && i == (end - ptr) / (2 * tableSize)
  798. && !((end - ptr) % (2 * tableSize))) {
  799. do {
  800. const u8 *cur = ptr + (i / 2) * (2 * tableSize);
  801. startLoc = read_pointer(&cur,
  802. cur + tableSize,
  803. hdr[3], 0,
  804. (unsigned long)hdr);
  805. if (pc < startLoc)
  806. i /= 2;
  807. else {
  808. ptr = cur - tableSize;
  809. i = (i + 1) / 2;
  810. }
  811. } while (startLoc && i > 1);
  812. if (i == 1
  813. && (startLoc = read_pointer(&ptr,
  814. ptr + tableSize,
  815. hdr[3], 0,
  816. (unsigned long)hdr)) != 0
  817. && pc >= startLoc)
  818. fde = (void *)read_pointer(&ptr,
  819. ptr + tableSize,
  820. hdr[3], 0,
  821. (unsigned long)hdr);
  822. }
  823. }
  824. if(hdr && !fde)
  825. dprintk(3, "Binary lookup for %lx failed.", pc);
  826. if (fde != NULL) {
  827. cie = cie_for_fde(fde, table);
  828. ptr = (const u8 *)(fde + 2);
  829. if(cie != NULL
  830. && cie != &bad_cie
  831. && cie != &not_fde
  832. && (ptrType = fde_pointer_type(cie)) >= 0
  833. && read_pointer(&ptr,
  834. (const u8 *)(fde + 1) + *fde,
  835. ptrType, 0, 0) == startLoc) {
  836. if (!(ptrType & DW_EH_PE_indirect))
  837. ptrType &= DW_EH_PE_FORM|DW_EH_PE_signed;
  838. endLoc = startLoc
  839. + read_pointer(&ptr,
  840. (const u8 *)(fde + 1) + *fde,
  841. ptrType, 0, 0);
  842. if(pc >= endLoc)
  843. fde = NULL;
  844. } else
  845. fde = NULL;
  846. if(!fde)
  847. dprintk(1, "Binary lookup result for %lx discarded.", pc);
  848. }
  849. if (fde == NULL) {
  850. for (fde = table->address, tableSize = table->size;
  851. cie = NULL, tableSize > sizeof(*fde)
  852. && tableSize - sizeof(*fde) >= *fde;
  853. tableSize -= sizeof(*fde) + *fde,
  854. fde += 1 + *fde / sizeof(*fde)) {
  855. cie = cie_for_fde(fde, table);
  856. if (cie == &bad_cie) {
  857. cie = NULL;
  858. break;
  859. }
  860. if (cie == NULL
  861. || cie == &not_fde
  862. || (ptrType = fde_pointer_type(cie)) < 0)
  863. continue;
  864. ptr = (const u8 *)(fde + 2);
  865. startLoc = read_pointer(&ptr,
  866. (const u8 *)(fde + 1) + *fde,
  867. ptrType, 0, 0);
  868. if (!startLoc)
  869. continue;
  870. if (!(ptrType & DW_EH_PE_indirect))
  871. ptrType &= DW_EH_PE_FORM|DW_EH_PE_signed;
  872. endLoc = startLoc
  873. + read_pointer(&ptr,
  874. (const u8 *)(fde + 1) + *fde,
  875. ptrType, 0, 0);
  876. if (pc >= startLoc && pc < endLoc)
  877. break;
  878. }
  879. if(!fde)
  880. dprintk(3, "Linear lookup for %lx failed.", pc);
  881. }
  882. }
  883. if (cie != NULL) {
  884. memset(&state, 0, sizeof(state));
  885. state.cieEnd = ptr; /* keep here temporarily */
  886. ptr = (const u8 *)(cie + 2);
  887. end = (const u8 *)(cie + 1) + *cie;
  888. frame->call_frame = 1;
  889. if ((state.version = *ptr) != 1)
  890. cie = NULL; /* unsupported version */
  891. else if (*++ptr) {
  892. /* check if augmentation size is first (and thus present) */
  893. if (*ptr == 'z') {
  894. while (++ptr < end && *ptr) {
  895. switch(*ptr) {
  896. /* check for ignorable (or already handled)
  897. * nul-terminated augmentation string */
  898. case 'L':
  899. case 'P':
  900. case 'R':
  901. continue;
  902. case 'S':
  903. frame->call_frame = 0;
  904. continue;
  905. default:
  906. break;
  907. }
  908. break;
  909. }
  910. }
  911. if (ptr >= end || *ptr)
  912. cie = NULL;
  913. }
  914. if(!cie)
  915. dprintk(1, "CIE unusable (%p,%p).", ptr, end);
  916. ++ptr;
  917. }
  918. if (cie != NULL) {
  919. /* get code aligment factor */
  920. state.codeAlign = get_uleb128(&ptr, end);
  921. /* get data aligment factor */
  922. state.dataAlign = get_sleb128(&ptr, end);
  923. if (state.codeAlign == 0 || state.dataAlign == 0 || ptr >= end)
  924. cie = NULL;
  925. else if (UNW_PC(frame) % state.codeAlign
  926. || UNW_SP(frame) % sleb128abs(state.dataAlign)) {
  927. dprintk(1, "Input pointer(s) misaligned (%lx,%lx).",
  928. UNW_PC(frame), UNW_SP(frame));
  929. return -EPERM;
  930. } else {
  931. retAddrReg = state.version <= 1 ? *ptr++ : get_uleb128(&ptr, end);
  932. /* skip augmentation */
  933. if (((const char *)(cie + 2))[1] == 'z') {
  934. uleb128_t augSize = get_uleb128(&ptr, end);
  935. ptr += augSize;
  936. }
  937. if (ptr > end
  938. || retAddrReg >= ARRAY_SIZE(reg_info)
  939. || REG_INVALID(retAddrReg)
  940. || reg_info[retAddrReg].width != sizeof(unsigned long))
  941. cie = NULL;
  942. }
  943. if(!cie)
  944. dprintk(1, "CIE validation failed (%p,%p).", ptr, end);
  945. }
  946. if (cie != NULL) {
  947. state.cieStart = ptr;
  948. ptr = state.cieEnd;
  949. state.cieEnd = end;
  950. end = (const u8 *)(fde + 1) + *fde;
  951. /* skip augmentation */
  952. if (((const char *)(cie + 2))[1] == 'z') {
  953. uleb128_t augSize = get_uleb128(&ptr, end);
  954. if ((ptr += augSize) > end)
  955. fde = NULL;
  956. }
  957. if(!fde)
  958. dprintk(1, "FDE validation failed (%p,%p).", ptr, end);
  959. }
  960. if (cie == NULL || fde == NULL) {
  961. #ifdef CONFIG_FRAME_POINTER
  962. unsigned long top, bottom;
  963. if ((UNW_SP(frame) | UNW_FP(frame)) % sizeof(unsigned long))
  964. return -EPERM;
  965. top = STACK_TOP(frame->task);
  966. bottom = STACK_BOTTOM(frame->task);
  967. # if FRAME_RETADDR_OFFSET < 0
  968. if (UNW_SP(frame) < top
  969. && UNW_FP(frame) <= UNW_SP(frame)
  970. && bottom < UNW_FP(frame)
  971. # else
  972. if (UNW_SP(frame) > top
  973. && UNW_FP(frame) >= UNW_SP(frame)
  974. && bottom > UNW_FP(frame)
  975. # endif
  976. && !((UNW_SP(frame) | UNW_FP(frame))
  977. & (sizeof(unsigned long) - 1))) {
  978. unsigned long link;
  979. if (!probe_kernel_address(
  980. (unsigned long *)(UNW_FP(frame)
  981. + FRAME_LINK_OFFSET),
  982. link)
  983. # if FRAME_RETADDR_OFFSET < 0
  984. && link > bottom && link < UNW_FP(frame)
  985. # else
  986. && link > UNW_FP(frame) && link < bottom
  987. # endif
  988. && !(link & (sizeof(link) - 1))
  989. && !probe_kernel_address(
  990. (unsigned long *)(UNW_FP(frame)
  991. + FRAME_RETADDR_OFFSET), UNW_PC(frame))) {
  992. UNW_SP(frame) = UNW_FP(frame) + FRAME_RETADDR_OFFSET
  993. # if FRAME_RETADDR_OFFSET < 0
  994. -
  995. # else
  996. +
  997. # endif
  998. sizeof(UNW_PC(frame));
  999. UNW_FP(frame) = link;
  1000. return 0;
  1001. }
  1002. }
  1003. #endif
  1004. return -ENXIO;
  1005. }
  1006. state.org = startLoc;
  1007. memcpy(&state.cfa, &badCFA, sizeof(state.cfa));
  1008. /* process instructions */
  1009. if (!processCFI(ptr, end, pc, ptrType, &state)
  1010. || state.loc > endLoc
  1011. || state.regs[retAddrReg].where == Nowhere
  1012. || state.cfa.reg >= ARRAY_SIZE(reg_info)
  1013. || reg_info[state.cfa.reg].width != sizeof(unsigned long)
  1014. || FRAME_REG(state.cfa.reg, unsigned long) % sizeof(unsigned long)
  1015. || state.cfa.offs % sizeof(unsigned long)) {
  1016. dprintk(1, "Unusable unwind info (%p,%p).", ptr, end);
  1017. return -EIO;
  1018. }
  1019. /* update frame */
  1020. #ifndef CONFIG_AS_CFI_SIGNAL_FRAME
  1021. if(frame->call_frame
  1022. && !UNW_DEFAULT_RA(state.regs[retAddrReg], state.dataAlign))
  1023. frame->call_frame = 0;
  1024. #endif
  1025. cfa = FRAME_REG(state.cfa.reg, unsigned long) + state.cfa.offs;
  1026. startLoc = min((unsigned long)UNW_SP(frame), cfa);
  1027. endLoc = max((unsigned long)UNW_SP(frame), cfa);
  1028. if (STACK_LIMIT(startLoc) != STACK_LIMIT(endLoc)) {
  1029. startLoc = min(STACK_LIMIT(cfa), cfa);
  1030. endLoc = max(STACK_LIMIT(cfa), cfa);
  1031. }
  1032. #ifndef CONFIG_64BIT
  1033. # define CASES CASE(8); CASE(16); CASE(32)
  1034. #else
  1035. # define CASES CASE(8); CASE(16); CASE(32); CASE(64)
  1036. #endif
  1037. pc = UNW_PC(frame);
  1038. sp = UNW_SP(frame);
  1039. for (i = 0; i < ARRAY_SIZE(state.regs); ++i) {
  1040. if (REG_INVALID(i)) {
  1041. if (state.regs[i].where == Nowhere)
  1042. continue;
  1043. dprintk(1, "Cannot restore register %u (%d).",
  1044. i, state.regs[i].where);
  1045. return -EIO;
  1046. }
  1047. switch(state.regs[i].where) {
  1048. default:
  1049. break;
  1050. case Register:
  1051. if (state.regs[i].value >= ARRAY_SIZE(reg_info)
  1052. || REG_INVALID(state.regs[i].value)
  1053. || reg_info[i].width > reg_info[state.regs[i].value].width) {
  1054. dprintk(1, "Cannot restore register %u from register %lu.",
  1055. i, state.regs[i].value);
  1056. return -EIO;
  1057. }
  1058. switch(reg_info[state.regs[i].value].width) {
  1059. #define CASE(n) \
  1060. case sizeof(u##n): \
  1061. state.regs[i].value = FRAME_REG(state.regs[i].value, \
  1062. const u##n); \
  1063. break
  1064. CASES;
  1065. #undef CASE
  1066. default:
  1067. dprintk(1, "Unsupported register size %u (%lu).",
  1068. reg_info[state.regs[i].value].width,
  1069. state.regs[i].value);
  1070. return -EIO;
  1071. }
  1072. break;
  1073. }
  1074. }
  1075. for (i = 0; i < ARRAY_SIZE(state.regs); ++i) {
  1076. if (REG_INVALID(i))
  1077. continue;
  1078. switch(state.regs[i].where) {
  1079. case Nowhere:
  1080. if (reg_info[i].width != sizeof(UNW_SP(frame))
  1081. || &FRAME_REG(i, __typeof__(UNW_SP(frame)))
  1082. != &UNW_SP(frame))
  1083. continue;
  1084. UNW_SP(frame) = cfa;
  1085. break;
  1086. case Register:
  1087. switch(reg_info[i].width) {
  1088. #define CASE(n) case sizeof(u##n): \
  1089. FRAME_REG(i, u##n) = state.regs[i].value; \
  1090. break
  1091. CASES;
  1092. #undef CASE
  1093. default:
  1094. dprintk(1, "Unsupported register size %u (%u).",
  1095. reg_info[i].width, i);
  1096. return -EIO;
  1097. }
  1098. break;
  1099. case Value:
  1100. if (reg_info[i].width != sizeof(unsigned long)) {
  1101. dprintk(1, "Unsupported value size %u (%u).",
  1102. reg_info[i].width, i);
  1103. return -EIO;
  1104. }
  1105. FRAME_REG(i, unsigned long) = cfa + state.regs[i].value
  1106. * state.dataAlign;
  1107. break;
  1108. case Memory: {
  1109. unsigned long addr = cfa + state.regs[i].value
  1110. * state.dataAlign;
  1111. if ((state.regs[i].value * state.dataAlign)
  1112. % sizeof(unsigned long)
  1113. || addr < startLoc
  1114. || addr + sizeof(unsigned long) < addr
  1115. || addr + sizeof(unsigned long) > endLoc) {
  1116. dprintk(1, "Bad memory location %lx (%lx).",
  1117. addr, state.regs[i].value);
  1118. return -EIO;
  1119. }
  1120. switch(reg_info[i].width) {
  1121. #define CASE(n) case sizeof(u##n): \
  1122. probe_kernel_address((u##n *)addr, FRAME_REG(i, u##n)); \
  1123. break
  1124. CASES;
  1125. #undef CASE
  1126. default:
  1127. dprintk(1, "Unsupported memory size %u (%u).",
  1128. reg_info[i].width, i);
  1129. return -EIO;
  1130. }
  1131. }
  1132. break;
  1133. }
  1134. }
  1135. if (UNW_PC(frame) % state.codeAlign
  1136. || UNW_SP(frame) % sleb128abs(state.dataAlign)) {
  1137. dprintk(1, "Output pointer(s) misaligned (%lx,%lx).",
  1138. UNW_PC(frame), UNW_SP(frame));
  1139. return -EIO;
  1140. }
  1141. if (pc == UNW_PC(frame) && sp == UNW_SP(frame)) {
  1142. dprintk(1, "No progress (%lx,%lx).", pc, sp);
  1143. return -EIO;
  1144. }
  1145. return 0;
  1146. #undef CASES
  1147. #undef FRAME_REG
  1148. }
  1149. EXPORT_SYMBOL(unwind);
  1150. int unwind_init_frame_info(struct unwind_frame_info *info,
  1151. struct task_struct *tsk,
  1152. /*const*/ struct pt_regs *regs)
  1153. {
  1154. info->task = tsk;
  1155. info->call_frame = 0;
  1156. arch_unw_init_frame_info(info, regs);
  1157. return 0;
  1158. }
  1159. EXPORT_SYMBOL(unwind_init_frame_info);
  1160. /*
  1161. * Prepare to unwind a blocked task.
  1162. */
  1163. int unwind_init_blocked(struct unwind_frame_info *info,
  1164. struct task_struct *tsk)
  1165. {
  1166. info->task = tsk;
  1167. info->call_frame = 0;
  1168. arch_unw_init_blocked(info);
  1169. return 0;
  1170. }
  1171. EXPORT_SYMBOL(unwind_init_blocked);
  1172. /*
  1173. * Prepare to unwind the currently running thread.
  1174. */
  1175. int unwind_init_running(struct unwind_frame_info *info,
  1176. asmlinkage int (*callback)(struct unwind_frame_info *,
  1177. void *arg),
  1178. void *arg)
  1179. {
  1180. info->task = current;
  1181. info->call_frame = 0;
  1182. return arch_unwind_init_running(info, callback, arg);
  1183. }
  1184. EXPORT_SYMBOL(unwind_init_running);
  1185. /*
  1186. * Unwind until the return pointer is in user-land (or until an error
  1187. * occurs). Returns 0 if successful, negative number in case of
  1188. * error.
  1189. */
  1190. int unwind_to_user(struct unwind_frame_info *info)
  1191. {
  1192. while (!arch_unw_user_mode(info)) {
  1193. int err = unwind(info);
  1194. if (err < 0)
  1195. return err;
  1196. }
  1197. return 0;
  1198. }
  1199. EXPORT_SYMBOL(unwind_to_user);