You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

533 lines
15 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2017 by Square, Inc. *
  3. * Steven Stallion <stallion@squareup.com> *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation; either version 2 of the License, or *
  8. * (at your option) any later version. *
  9. * *
  10. * This program is distributed in the hope that it will be useful, *
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  13. * GNU General Public License for more details. *
  14. * *
  15. * You should have received a copy of the GNU General Public License *
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>. *
  17. ***************************************************************************/
  18. #ifdef HAVE_CONFIG_H
  19. #include "config.h"
  20. #endif
  21. #include <helper/log.h>
  22. #include <helper/time_support.h>
  23. #include <helper/types.h>
  24. #include <rtos/rtos.h>
  25. #include <target/target.h>
  26. #include <target/target_type.h>
  27. #include "rtos_ucos_iii_stackings.h"
  28. #ifndef UCOS_III_MAX_STRLEN
  29. #define UCOS_III_MAX_STRLEN 64
  30. #endif
  31. #ifndef UCOS_III_MAX_THREADS
  32. #define UCOS_III_MAX_THREADS 256
  33. #endif
  34. struct ucos_iii_params {
  35. const char *target_name;
  36. const unsigned char pointer_width;
  37. symbol_address_t thread_stack_offset;
  38. symbol_address_t thread_name_offset;
  39. symbol_address_t thread_state_offset;
  40. symbol_address_t thread_priority_offset;
  41. symbol_address_t thread_prev_offset;
  42. symbol_address_t thread_next_offset;
  43. bool thread_offsets_updated;
  44. size_t threadid_start;
  45. const struct rtos_register_stacking *stacking_info;
  46. size_t num_threads;
  47. symbol_address_t threads[];
  48. };
  49. static const struct ucos_iii_params ucos_iii_params_list[] = {
  50. {
  51. "cortex_m", /* target_name */
  52. sizeof(uint32_t), /* pointer_width */
  53. 0, /* thread_stack_offset */
  54. 0, /* thread_name_offset */
  55. 0, /* thread_state_offset */
  56. 0, /* thread_priority_offset */
  57. 0, /* thread_prev_offset */
  58. 0, /* thread_next_offset */
  59. false, /* thread_offsets_updated */
  60. 1, /* threadid_start */
  61. &rtos_ucos_iii_cortex_m_stacking, /* stacking_info */
  62. 0, /* num_threads */
  63. },
  64. {
  65. "esirisc", /* target_name */
  66. sizeof(uint32_t), /* pointer_width */
  67. 0, /* thread_stack_offset */
  68. 0, /* thread_name_offset */
  69. 0, /* thread_state_offset */
  70. 0, /* thread_priority_offset */
  71. 0, /* thread_prev_offset */
  72. 0, /* thread_next_offset */
  73. false, /* thread_offsets_updated */
  74. 1, /* threadid_start */
  75. &rtos_ucos_iii_esi_risc_stacking, /* stacking_info */
  76. 0, /* num_threads */
  77. },
  78. };
  79. static const char * const ucos_iii_symbol_list[] = {
  80. "OSRunning",
  81. "OSTCBCurPtr",
  82. "OSTaskDbgListPtr",
  83. "OSTaskQty",
  84. /* also see: contrib/rtos-helpers/uCOS-III-openocd.c */
  85. "openocd_OS_TCB_StkPtr_offset",
  86. "openocd_OS_TCB_NamePtr_offset",
  87. "openocd_OS_TCB_TaskState_offset",
  88. "openocd_OS_TCB_Prio_offset",
  89. "openocd_OS_TCB_DbgPrevPtr_offset",
  90. "openocd_OS_TCB_DbgNextPtr_offset",
  91. NULL
  92. };
  93. enum ucos_iii_symbol_values {
  94. UCOS_III_VAL_OS_RUNNING,
  95. UCOS_III_VAL_OS_TCB_CUR_PTR,
  96. UCOS_III_VAL_OS_TASK_DBG_LIST_PTR,
  97. UCOS_III_VAL_OS_TASK_QTY,
  98. /* also see: contrib/rtos-helpers/uCOS-III-openocd.c */
  99. UCOS_III_VAL_OS_TCB_STK_PTR_OFFSET,
  100. UCOS_III_VAL_OS_TCB_NAME_PTR_OFFSET,
  101. UCOS_III_VAL_OS_TCB_TASK_STATE_OFFSET,
  102. UCOS_III_VAL_OS_TCB_PRIO_OFFSET,
  103. UCOS_III_VAL_OS_TCB_DBG_PREV_PTR_OFFSET,
  104. UCOS_III_VAL_OS_TCB_DBG_NEXT_PTR_OFFSET,
  105. };
  106. static const char * const ucos_iii_thread_state_list[] = {
  107. "Ready",
  108. "Delay",
  109. "Pend",
  110. "Pend Timeout",
  111. "Suspended",
  112. "Delay Suspended",
  113. "Pend Suspended",
  114. "Pend Timeout Suspended",
  115. };
  116. static int ucos_iii_find_or_create_thread(struct rtos *rtos, symbol_address_t thread_address,
  117. threadid_t *threadid)
  118. {
  119. struct ucos_iii_params *params = rtos->rtos_specific_params;
  120. size_t thread_index;
  121. for (thread_index = 0; thread_index < params->num_threads; thread_index++)
  122. if (params->threads[thread_index] == thread_address)
  123. goto found;
  124. if (params->num_threads == UCOS_III_MAX_THREADS) {
  125. LOG_WARNING("uCOS-III: too many threads; increase UCOS_III_MAX_THREADS");
  126. return ERROR_FAIL;
  127. }
  128. params->threads[thread_index] = thread_address;
  129. params->num_threads++;
  130. found:
  131. *threadid = thread_index + params->threadid_start;
  132. return ERROR_OK;
  133. }
  134. static int ucos_iii_find_thread_address(struct rtos *rtos, threadid_t threadid,
  135. symbol_address_t *thread_address)
  136. {
  137. struct ucos_iii_params *params = rtos->rtos_specific_params;
  138. size_t thread_index;
  139. thread_index = threadid - params->threadid_start;
  140. if (thread_index >= params->num_threads) {
  141. LOG_ERROR("uCOS-III: failed to find thread address");
  142. return ERROR_FAIL;
  143. }
  144. *thread_address = params->threads[thread_index];
  145. return ERROR_OK;
  146. }
  147. static int ucos_iii_find_last_thread_address(struct rtos *rtos, symbol_address_t *thread_address)
  148. {
  149. struct ucos_iii_params *params = rtos->rtos_specific_params;
  150. int retval;
  151. /* read the thread list head */
  152. symbol_address_t thread_list_address = 0;
  153. retval = target_read_memory(rtos->target,
  154. rtos->symbols[UCOS_III_VAL_OS_TASK_DBG_LIST_PTR].address,
  155. params->pointer_width,
  156. 1,
  157. (void *)&thread_list_address);
  158. if (retval != ERROR_OK) {
  159. LOG_ERROR("uCOS-III: failed to read thread list address");
  160. return retval;
  161. }
  162. /* advance to end of thread list */
  163. do {
  164. *thread_address = thread_list_address;
  165. retval = target_read_memory(rtos->target,
  166. thread_list_address + params->thread_next_offset,
  167. params->pointer_width,
  168. 1,
  169. (void *)&thread_list_address);
  170. if (retval != ERROR_OK) {
  171. LOG_ERROR("uCOS-III: failed to read next thread address");
  172. return retval;
  173. }
  174. } while (thread_list_address != 0);
  175. return ERROR_OK;
  176. }
  177. static int ucos_iii_update_thread_offsets(struct rtos *rtos)
  178. {
  179. struct ucos_iii_params *params = rtos->rtos_specific_params;
  180. if (params->thread_offsets_updated)
  181. return ERROR_OK;
  182. const struct thread_offset_map {
  183. enum ucos_iii_symbol_values symbol_value;
  184. symbol_address_t *thread_offset;
  185. } thread_offset_maps[] = {
  186. {
  187. UCOS_III_VAL_OS_TCB_STK_PTR_OFFSET,
  188. &params->thread_stack_offset,
  189. },
  190. {
  191. UCOS_III_VAL_OS_TCB_NAME_PTR_OFFSET,
  192. &params->thread_name_offset,
  193. },
  194. {
  195. UCOS_III_VAL_OS_TCB_TASK_STATE_OFFSET,
  196. &params->thread_state_offset,
  197. },
  198. {
  199. UCOS_III_VAL_OS_TCB_PRIO_OFFSET,
  200. &params->thread_priority_offset,
  201. },
  202. {
  203. UCOS_III_VAL_OS_TCB_DBG_PREV_PTR_OFFSET,
  204. &params->thread_prev_offset,
  205. },
  206. {
  207. UCOS_III_VAL_OS_TCB_DBG_NEXT_PTR_OFFSET,
  208. &params->thread_next_offset,
  209. },
  210. };
  211. for (size_t i = 0; i < ARRAY_SIZE(thread_offset_maps); i++) {
  212. const struct thread_offset_map *thread_offset_map = &thread_offset_maps[i];
  213. int retval = target_read_memory(rtos->target,
  214. rtos->symbols[thread_offset_map->symbol_value].address,
  215. params->pointer_width,
  216. 1,
  217. (void *)thread_offset_map->thread_offset);
  218. if (retval != ERROR_OK) {
  219. LOG_ERROR("uCOS-III: failed to read thread offset");
  220. return retval;
  221. }
  222. }
  223. params->thread_offsets_updated = true;
  224. return ERROR_OK;
  225. }
  226. static bool ucos_iii_detect_rtos(struct target *target)
  227. {
  228. return target->rtos->symbols &&
  229. target->rtos->symbols[UCOS_III_VAL_OS_RUNNING].address != 0;
  230. }
  231. static int ucos_iii_reset_handler(struct target *target, enum target_reset_mode reset_mode, void *priv)
  232. {
  233. struct ucos_iii_params *params = target->rtos->rtos_specific_params;
  234. params->thread_offsets_updated = false;
  235. params->num_threads = 0;
  236. return ERROR_OK;
  237. }
  238. static int ucos_iii_create(struct target *target)
  239. {
  240. struct ucos_iii_params *params;
  241. for (size_t i = 0; i < ARRAY_SIZE(ucos_iii_params_list); i++)
  242. if (strcmp(ucos_iii_params_list[i].target_name, target->type->name) == 0) {
  243. params = malloc(sizeof(*params) + (UCOS_III_MAX_THREADS * sizeof(*params->threads)));
  244. if (!params) {
  245. LOG_ERROR("uCOS-III: out of memory");
  246. return ERROR_FAIL;
  247. }
  248. memcpy(params, &ucos_iii_params_list[i], sizeof(ucos_iii_params_list[i]));
  249. target->rtos->rtos_specific_params = (void *)params;
  250. target_register_reset_callback(ucos_iii_reset_handler, NULL);
  251. return ERROR_OK;
  252. }
  253. LOG_ERROR("uCOS-III: target not supported: %s", target->type->name);
  254. return ERROR_FAIL;
  255. }
  256. static int ucos_iii_update_threads(struct rtos *rtos)
  257. {
  258. struct ucos_iii_params *params = rtos->rtos_specific_params;
  259. int retval;
  260. if (!rtos->symbols) {
  261. LOG_ERROR("uCOS-III: symbol list not loaded");
  262. return ERROR_FAIL;
  263. }
  264. /* free previous thread details */
  265. rtos_free_threadlist(rtos);
  266. /* verify RTOS is running */
  267. uint8_t rtos_running;
  268. retval = target_read_u8(rtos->target,
  269. rtos->symbols[UCOS_III_VAL_OS_RUNNING].address,
  270. &rtos_running);
  271. if (retval != ERROR_OK) {
  272. LOG_ERROR("uCOS-III: failed to read RTOS running");
  273. return retval;
  274. }
  275. if (rtos_running != 1 && rtos_running != 0) {
  276. LOG_ERROR("uCOS-III: invalid RTOS running value");
  277. return ERROR_FAIL;
  278. }
  279. if (!rtos_running) {
  280. rtos->thread_details = calloc(1, sizeof(struct thread_detail));
  281. if (!rtos->thread_details) {
  282. LOG_ERROR("uCOS-III: out of memory");
  283. return ERROR_FAIL;
  284. }
  285. rtos->thread_count = 1;
  286. rtos->thread_details->threadid = 0;
  287. rtos->thread_details->exists = true;
  288. rtos->current_thread = 0;
  289. return ERROR_OK;
  290. }
  291. /* update thread offsets */
  292. retval = ucos_iii_update_thread_offsets(rtos);
  293. if (retval != ERROR_OK) {
  294. LOG_ERROR("uCOS-III: failed to update thread offsets");
  295. return retval;
  296. }
  297. /* read current thread address */
  298. symbol_address_t current_thread_address = 0;
  299. retval = target_read_memory(rtos->target,
  300. rtos->symbols[UCOS_III_VAL_OS_TCB_CUR_PTR].address,
  301. params->pointer_width,
  302. 1,
  303. (void *)&current_thread_address);
  304. if (retval != ERROR_OK) {
  305. LOG_ERROR("uCOS-III: failed to read current thread address");
  306. return retval;
  307. }
  308. /* read number of tasks */
  309. retval = target_read_u16(rtos->target,
  310. rtos->symbols[UCOS_III_VAL_OS_TASK_QTY].address,
  311. (void *)&rtos->thread_count);
  312. if (retval != ERROR_OK) {
  313. LOG_ERROR("uCOS-III: failed to read thread count");
  314. return retval;
  315. }
  316. rtos->thread_details = calloc(rtos->thread_count, sizeof(struct thread_detail));
  317. if (!rtos->thread_details) {
  318. LOG_ERROR("uCOS-III: out of memory");
  319. return ERROR_FAIL;
  320. }
  321. /*
  322. * uC/OS-III adds tasks in LIFO order; advance to the end of the
  323. * list and work backwards to preserve the intended order.
  324. */
  325. symbol_address_t thread_address = 0;
  326. retval = ucos_iii_find_last_thread_address(rtos, &thread_address);
  327. if (retval != ERROR_OK) {
  328. LOG_ERROR("uCOS-III: failed to find last thread address");
  329. return retval;
  330. }
  331. for (int i = 0; i < rtos->thread_count; i++) {
  332. struct thread_detail *thread_detail = &rtos->thread_details[i];
  333. char thread_str_buffer[UCOS_III_MAX_STRLEN + 1];
  334. /* find or create new threadid */
  335. retval = ucos_iii_find_or_create_thread(rtos, thread_address, &thread_detail->threadid);
  336. if (retval != ERROR_OK) {
  337. LOG_ERROR("uCOS-III: failed to find or create thread");
  338. return retval;
  339. }
  340. if (thread_address == current_thread_address)
  341. rtos->current_thread = thread_detail->threadid;
  342. thread_detail->exists = true;
  343. /* read thread name */
  344. symbol_address_t thread_name_address = 0;
  345. retval = target_read_memory(rtos->target,
  346. thread_address + params->thread_name_offset,
  347. params->pointer_width,
  348. 1,
  349. (void *)&thread_name_address);
  350. if (retval != ERROR_OK) {
  351. LOG_ERROR("uCOS-III: failed to name address");
  352. return retval;
  353. }
  354. retval = target_read_buffer(rtos->target,
  355. thread_name_address,
  356. sizeof(thread_str_buffer),
  357. (void *)thread_str_buffer);
  358. if (retval != ERROR_OK) {
  359. LOG_ERROR("uCOS-III: failed to read thread name");
  360. return retval;
  361. }
  362. thread_str_buffer[sizeof(thread_str_buffer) - 1] = '\0';
  363. thread_detail->thread_name_str = strdup(thread_str_buffer);
  364. /* read thread extra info */
  365. uint8_t thread_state;
  366. uint8_t thread_priority;
  367. retval = target_read_u8(rtos->target,
  368. thread_address + params->thread_state_offset,
  369. &thread_state);
  370. if (retval != ERROR_OK) {
  371. LOG_ERROR("uCOS-III: failed to read thread state");
  372. return retval;
  373. }
  374. retval = target_read_u8(rtos->target,
  375. thread_address + params->thread_priority_offset,
  376. &thread_priority);
  377. if (retval != ERROR_OK) {
  378. LOG_ERROR("uCOS-III: failed to read thread priority");
  379. return retval;
  380. }
  381. const char *thread_state_str;
  382. if (thread_state < ARRAY_SIZE(ucos_iii_thread_state_list))
  383. thread_state_str = ucos_iii_thread_state_list[thread_state];
  384. else
  385. thread_state_str = "Unknown";
  386. snprintf(thread_str_buffer, sizeof(thread_str_buffer), "State: %s, Priority: %d",
  387. thread_state_str, thread_priority);
  388. thread_detail->extra_info_str = strdup(thread_str_buffer);
  389. /* read previous thread address */
  390. retval = target_read_memory(rtos->target,
  391. thread_address + params->thread_prev_offset,
  392. params->pointer_width,
  393. 1,
  394. (void *)&thread_address);
  395. if (retval != ERROR_OK) {
  396. LOG_ERROR("uCOS-III: failed to read previous thread address");
  397. return retval;
  398. }
  399. }
  400. return ERROR_OK;
  401. }
  402. static int ucos_iii_get_thread_reg_list(struct rtos *rtos, threadid_t threadid,
  403. struct rtos_reg **reg_list, int *num_regs)
  404. {
  405. struct ucos_iii_params *params = rtos->rtos_specific_params;
  406. int retval;
  407. /* find thread address for threadid */
  408. symbol_address_t thread_address = 0;
  409. retval = ucos_iii_find_thread_address(rtos, threadid, &thread_address);
  410. if (retval != ERROR_OK) {
  411. LOG_ERROR("uCOS-III: failed to find thread address");
  412. return retval;
  413. }
  414. /* read thread stack address */
  415. symbol_address_t stack_address = 0;
  416. retval = target_read_memory(rtos->target,
  417. thread_address + params->thread_stack_offset,
  418. params->pointer_width,
  419. 1,
  420. (void *)&stack_address);
  421. if (retval != ERROR_OK) {
  422. LOG_ERROR("uCOS-III: failed to read stack address");
  423. return retval;
  424. }
  425. return rtos_generic_stack_read(rtos->target,
  426. params->stacking_info,
  427. stack_address,
  428. reg_list,
  429. num_regs);
  430. }
  431. static int ucos_iii_get_symbol_list_to_lookup(struct symbol_table_elem *symbol_list[])
  432. {
  433. *symbol_list = calloc(ARRAY_SIZE(ucos_iii_symbol_list), sizeof(struct symbol_table_elem));
  434. if (!*symbol_list) {
  435. LOG_ERROR("uCOS-III: out of memory");
  436. return ERROR_FAIL;
  437. }
  438. for (size_t i = 0; i < ARRAY_SIZE(ucos_iii_symbol_list); i++)
  439. (*symbol_list)[i].symbol_name = ucos_iii_symbol_list[i];
  440. return ERROR_OK;
  441. }
  442. const struct rtos_type ucos_iii_rtos = {
  443. .name = "uCOS-III",
  444. .detect_rtos = ucos_iii_detect_rtos,
  445. .create = ucos_iii_create,
  446. .update_threads = ucos_iii_update_threads,
  447. .get_thread_reg_list = ucos_iii_get_thread_reg_list,
  448. .get_symbol_list_to_lookup = ucos_iii_get_symbol_list_to_lookup,
  449. };