unwind.c 32 KB

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