unwind.c 33 KB

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