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.
 
 
 
 
 
 

553 lines
18 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2011 by Broadcom Corporation *
  3. * Evan Hunter - ehunter@broadcom.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/time_support.h>
  22. #include <jtag/jtag.h>
  23. #include "target/target.h"
  24. #include "target/target_type.h"
  25. #include "rtos.h"
  26. #include "helper/log.h"
  27. #include "helper/types.h"
  28. #include "rtos_standard_stackings.h"
  29. #include "target/armv7m.h"
  30. #include "target/cortex_m.h"
  31. #define FREERTOS_MAX_PRIORITIES 63
  32. /* FIXME: none of the _width parameters are actually observed properly!
  33. * you WILL need to edit more if you actually attempt to target a 8/16/64
  34. * bit target!
  35. */
  36. struct freertos_params {
  37. const char *target_name;
  38. const unsigned char thread_count_width;
  39. const unsigned char pointer_width;
  40. const unsigned char list_next_offset;
  41. const unsigned char list_width;
  42. const unsigned char list_elem_next_offset;
  43. const unsigned char list_elem_content_offset;
  44. const unsigned char thread_stack_offset;
  45. const unsigned char thread_name_offset;
  46. const struct rtos_register_stacking *stacking_info_cm3;
  47. const struct rtos_register_stacking *stacking_info_cm4f;
  48. const struct rtos_register_stacking *stacking_info_cm4f_fpu;
  49. };
  50. static const struct freertos_params freertos_params_list[] = {
  51. {
  52. "cortex_m", /* target_name */
  53. 4, /* thread_count_width; */
  54. 4, /* pointer_width; */
  55. 16, /* list_next_offset; */
  56. 20, /* list_width; */
  57. 8, /* list_elem_next_offset; */
  58. 12, /* list_elem_content_offset */
  59. 0, /* thread_stack_offset; */
  60. 52, /* thread_name_offset; */
  61. &rtos_standard_cortex_m3_stacking, /* stacking_info */
  62. &rtos_standard_cortex_m4f_stacking,
  63. &rtos_standard_cortex_m4f_fpu_stacking,
  64. },
  65. {
  66. "hla_target", /* target_name */
  67. 4, /* thread_count_width; */
  68. 4, /* pointer_width; */
  69. 16, /* list_next_offset; */
  70. 20, /* list_width; */
  71. 8, /* list_elem_next_offset; */
  72. 12, /* list_elem_content_offset */
  73. 0, /* thread_stack_offset; */
  74. 52, /* thread_name_offset; */
  75. &rtos_standard_cortex_m3_stacking, /* stacking_info */
  76. &rtos_standard_cortex_m4f_stacking,
  77. &rtos_standard_cortex_m4f_fpu_stacking,
  78. },
  79. {
  80. "nds32_v3", /* target_name */
  81. 4, /* thread_count_width; */
  82. 4, /* pointer_width; */
  83. 16, /* list_next_offset; */
  84. 20, /* list_width; */
  85. 8, /* list_elem_next_offset; */
  86. 12, /* list_elem_content_offset */
  87. 0, /* thread_stack_offset; */
  88. 52, /* thread_name_offset; */
  89. &rtos_standard_nds32_n1068_stacking, /* stacking_info */
  90. &rtos_standard_cortex_m4f_stacking,
  91. &rtos_standard_cortex_m4f_fpu_stacking,
  92. },
  93. };
  94. static bool freertos_detect_rtos(struct target *target);
  95. static int freertos_create(struct target *target);
  96. static int freertos_update_threads(struct rtos *rtos);
  97. static int freertos_get_thread_reg_list(struct rtos *rtos, int64_t thread_id,
  98. struct rtos_reg **reg_list, int *num_regs);
  99. static int freertos_get_symbol_list_to_lookup(struct symbol_table_elem *symbol_list[]);
  100. struct rtos_type freertos_rtos = {
  101. .name = "FreeRTOS",
  102. .detect_rtos = freertos_detect_rtos,
  103. .create = freertos_create,
  104. .update_threads = freertos_update_threads,
  105. .get_thread_reg_list = freertos_get_thread_reg_list,
  106. .get_symbol_list_to_lookup = freertos_get_symbol_list_to_lookup,
  107. };
  108. enum freertos_symbol_values {
  109. FREERTOS_VAL_PX_CURRENT_TCB = 0,
  110. FREERTOS_VAL_PX_READY_TASKS_LISTS = 1,
  111. FREERTOS_VAL_X_DELAYED_TASK_LIST1 = 2,
  112. FREERTOS_VAL_X_DELAYED_TASK_LIST2 = 3,
  113. FREERTOS_VAL_PX_DELAYED_TASK_LIST = 4,
  114. FREERTOS_VAL_PX_OVERFLOW_DELAYED_TASK_LIST = 5,
  115. FREERTOS_VAL_X_PENDING_READY_LIST = 6,
  116. FREERTOS_VAL_X_TASKS_WAITING_TERMINATION = 7,
  117. FREERTOS_VAL_X_SUSPENDED_TASK_LIST = 8,
  118. FREERTOS_VAL_UX_CURRENT_NUMBER_OF_TASKS = 9,
  119. FREERTOS_VAL_UX_TOP_USED_PRIORITY = 10,
  120. };
  121. struct symbols {
  122. const char *name;
  123. bool optional;
  124. };
  125. static const struct symbols freertos_symbol_list[] = {
  126. { "pxCurrentTCB", false },
  127. { "pxReadyTasksLists", false },
  128. { "xDelayedTaskList1", false },
  129. { "xDelayedTaskList2", false },
  130. { "pxDelayedTaskList", false },
  131. { "pxOverflowDelayedTaskList", false },
  132. { "xPendingReadyList", false },
  133. { "xTasksWaitingTermination", true }, /* Only if INCLUDE_vTaskDelete */
  134. { "xSuspendedTaskList", true }, /* Only if INCLUDE_vTaskSuspend */
  135. { "uxCurrentNumberOfTasks", false },
  136. { "uxTopUsedPriority", true }, /* Unavailable since v7.5.3 */
  137. { NULL, false }
  138. };
  139. /* TODO: */
  140. /* this is not safe for little endian yet */
  141. /* may be problems reading if sizes are not 32 bit long integers. */
  142. /* test mallocs for failure */
  143. static int freertos_update_threads(struct rtos *rtos)
  144. {
  145. int retval;
  146. unsigned int tasks_found = 0;
  147. const struct freertos_params *param;
  148. if (!rtos->rtos_specific_params)
  149. return -1;
  150. param = (const struct freertos_params *) rtos->rtos_specific_params;
  151. if (!rtos->symbols) {
  152. LOG_ERROR("No symbols for FreeRTOS");
  153. return -3;
  154. }
  155. if (rtos->symbols[FREERTOS_VAL_UX_CURRENT_NUMBER_OF_TASKS].address == 0) {
  156. LOG_ERROR("Don't have the number of threads in FreeRTOS");
  157. return -2;
  158. }
  159. uint32_t thread_list_size = 0;
  160. retval = target_read_u32(rtos->target,
  161. rtos->symbols[FREERTOS_VAL_UX_CURRENT_NUMBER_OF_TASKS].address,
  162. &thread_list_size);
  163. LOG_DEBUG("FreeRTOS: Read uxCurrentNumberOfTasks at 0x%" PRIx64 ", value %" PRIu32,
  164. rtos->symbols[FREERTOS_VAL_UX_CURRENT_NUMBER_OF_TASKS].address,
  165. thread_list_size);
  166. if (retval != ERROR_OK) {
  167. LOG_ERROR("Could not read FreeRTOS thread count from target");
  168. return retval;
  169. }
  170. /* wipe out previous thread details if any */
  171. rtos_free_threadlist(rtos);
  172. /* read the current thread */
  173. uint32_t pointer_casts_are_bad;
  174. retval = target_read_u32(rtos->target,
  175. rtos->symbols[FREERTOS_VAL_PX_CURRENT_TCB].address,
  176. &pointer_casts_are_bad);
  177. if (retval != ERROR_OK) {
  178. LOG_ERROR("Error reading current thread in FreeRTOS thread list");
  179. return retval;
  180. }
  181. rtos->current_thread = pointer_casts_are_bad;
  182. LOG_DEBUG("FreeRTOS: Read pxCurrentTCB at 0x%" PRIx64 ", value 0x%" PRIx64,
  183. rtos->symbols[FREERTOS_VAL_PX_CURRENT_TCB].address,
  184. rtos->current_thread);
  185. if ((thread_list_size == 0) || (rtos->current_thread == 0)) {
  186. /* Either : No RTOS threads - there is always at least the current execution though */
  187. /* OR : No current thread - all threads suspended - show the current execution
  188. * of idling */
  189. char tmp_str[] = "Current Execution";
  190. thread_list_size++;
  191. tasks_found++;
  192. rtos->thread_details = malloc(
  193. sizeof(struct thread_detail) * thread_list_size);
  194. if (!rtos->thread_details) {
  195. LOG_ERROR("Error allocating memory for %d threads", thread_list_size);
  196. return ERROR_FAIL;
  197. }
  198. rtos->thread_details->threadid = 1;
  199. rtos->thread_details->exists = true;
  200. rtos->thread_details->extra_info_str = NULL;
  201. rtos->thread_details->thread_name_str = malloc(sizeof(tmp_str));
  202. strcpy(rtos->thread_details->thread_name_str, tmp_str);
  203. if (thread_list_size == 1) {
  204. rtos->thread_count = 1;
  205. return ERROR_OK;
  206. }
  207. } else {
  208. /* create space for new thread details */
  209. rtos->thread_details = malloc(
  210. sizeof(struct thread_detail) * thread_list_size);
  211. if (!rtos->thread_details) {
  212. LOG_ERROR("Error allocating memory for %d threads", thread_list_size);
  213. return ERROR_FAIL;
  214. }
  215. }
  216. /* Find out how many lists are needed to be read from pxReadyTasksLists, */
  217. if (rtos->symbols[FREERTOS_VAL_UX_TOP_USED_PRIORITY].address == 0) {
  218. LOG_ERROR("FreeRTOS: uxTopUsedPriority is not defined, consult the OpenOCD manual for a work-around");
  219. return ERROR_FAIL;
  220. }
  221. uint32_t top_used_priority = 0;
  222. retval = target_read_u32(rtos->target,
  223. rtos->symbols[FREERTOS_VAL_UX_TOP_USED_PRIORITY].address,
  224. &top_used_priority);
  225. if (retval != ERROR_OK)
  226. return retval;
  227. LOG_DEBUG("FreeRTOS: Read uxTopUsedPriority at 0x%" PRIx64 ", value %" PRIu32,
  228. rtos->symbols[FREERTOS_VAL_UX_TOP_USED_PRIORITY].address,
  229. top_used_priority);
  230. if (top_used_priority > FREERTOS_MAX_PRIORITIES) {
  231. LOG_ERROR("FreeRTOS top used priority is unreasonably big, not proceeding: %" PRIu32,
  232. top_used_priority);
  233. return ERROR_FAIL;
  234. }
  235. /* uxTopUsedPriority was defined as configMAX_PRIORITIES - 1
  236. * in old FreeRTOS versions (before V7.5.3)
  237. * Use contrib/rtos-helpers/FreeRTOS-openocd.c to get compatible symbol
  238. * in newer FreeRTOS versions.
  239. * Here we restore the original configMAX_PRIORITIES value */
  240. unsigned int config_max_priorities = top_used_priority + 1;
  241. symbol_address_t *list_of_lists =
  242. malloc(sizeof(symbol_address_t) * (config_max_priorities + 5));
  243. if (!list_of_lists) {
  244. LOG_ERROR("Error allocating memory for %u priorities", config_max_priorities);
  245. return ERROR_FAIL;
  246. }
  247. unsigned int num_lists;
  248. for (num_lists = 0; num_lists < config_max_priorities; num_lists++)
  249. list_of_lists[num_lists] = rtos->symbols[FREERTOS_VAL_PX_READY_TASKS_LISTS].address +
  250. num_lists * param->list_width;
  251. list_of_lists[num_lists++] = rtos->symbols[FREERTOS_VAL_X_DELAYED_TASK_LIST1].address;
  252. list_of_lists[num_lists++] = rtos->symbols[FREERTOS_VAL_X_DELAYED_TASK_LIST2].address;
  253. list_of_lists[num_lists++] = rtos->symbols[FREERTOS_VAL_X_PENDING_READY_LIST].address;
  254. list_of_lists[num_lists++] = rtos->symbols[FREERTOS_VAL_X_SUSPENDED_TASK_LIST].address;
  255. list_of_lists[num_lists++] = rtos->symbols[FREERTOS_VAL_X_TASKS_WAITING_TERMINATION].address;
  256. for (unsigned int i = 0; i < num_lists; i++) {
  257. if (list_of_lists[i] == 0)
  258. continue;
  259. /* Read the number of threads in this list */
  260. uint32_t list_thread_count = 0;
  261. retval = target_read_u32(rtos->target,
  262. list_of_lists[i],
  263. &list_thread_count);
  264. if (retval != ERROR_OK) {
  265. LOG_ERROR("Error reading number of threads in FreeRTOS thread list");
  266. free(list_of_lists);
  267. return retval;
  268. }
  269. LOG_DEBUG("FreeRTOS: Read thread count for list %u at 0x%" PRIx64 ", value %" PRIu32,
  270. i, list_of_lists[i], list_thread_count);
  271. if (list_thread_count == 0)
  272. continue;
  273. /* Read the location of first list item */
  274. uint32_t prev_list_elem_ptr = -1;
  275. uint32_t list_elem_ptr = 0;
  276. retval = target_read_u32(rtos->target,
  277. list_of_lists[i] + param->list_next_offset,
  278. &list_elem_ptr);
  279. if (retval != ERROR_OK) {
  280. LOG_ERROR("Error reading first thread item location in FreeRTOS thread list");
  281. free(list_of_lists);
  282. return retval;
  283. }
  284. LOG_DEBUG("FreeRTOS: Read first item for list %u at 0x%" PRIx64 ", value 0x%" PRIx32,
  285. i, list_of_lists[i] + param->list_next_offset, list_elem_ptr);
  286. while ((list_thread_count > 0) && (list_elem_ptr != 0) &&
  287. (list_elem_ptr != prev_list_elem_ptr) &&
  288. (tasks_found < thread_list_size)) {
  289. /* Get the location of the thread structure. */
  290. rtos->thread_details[tasks_found].threadid = 0;
  291. retval = target_read_u32(rtos->target,
  292. list_elem_ptr + param->list_elem_content_offset,
  293. &pointer_casts_are_bad);
  294. if (retval != ERROR_OK) {
  295. LOG_ERROR("Error reading thread list item object in FreeRTOS thread list");
  296. free(list_of_lists);
  297. return retval;
  298. }
  299. rtos->thread_details[tasks_found].threadid = pointer_casts_are_bad;
  300. LOG_DEBUG("FreeRTOS: Read Thread ID at 0x%" PRIx32 ", value 0x%" PRIx64,
  301. list_elem_ptr + param->list_elem_content_offset,
  302. rtos->thread_details[tasks_found].threadid);
  303. /* get thread name */
  304. #define FREERTOS_THREAD_NAME_STR_SIZE (200)
  305. char tmp_str[FREERTOS_THREAD_NAME_STR_SIZE];
  306. /* Read the thread name */
  307. retval = target_read_buffer(rtos->target,
  308. rtos->thread_details[tasks_found].threadid + param->thread_name_offset,
  309. FREERTOS_THREAD_NAME_STR_SIZE,
  310. (uint8_t *)&tmp_str);
  311. if (retval != ERROR_OK) {
  312. LOG_ERROR("Error reading first thread item location in FreeRTOS thread list");
  313. free(list_of_lists);
  314. return retval;
  315. }
  316. tmp_str[FREERTOS_THREAD_NAME_STR_SIZE-1] = '\x00';
  317. LOG_DEBUG("FreeRTOS: Read Thread Name at 0x%" PRIx64 ", value '%s'",
  318. rtos->thread_details[tasks_found].threadid + param->thread_name_offset,
  319. tmp_str);
  320. if (tmp_str[0] == '\x00')
  321. strcpy(tmp_str, "No Name");
  322. rtos->thread_details[tasks_found].thread_name_str =
  323. malloc(strlen(tmp_str)+1);
  324. strcpy(rtos->thread_details[tasks_found].thread_name_str, tmp_str);
  325. rtos->thread_details[tasks_found].exists = true;
  326. if (rtos->thread_details[tasks_found].threadid == rtos->current_thread) {
  327. char running_str[] = "State: Running";
  328. rtos->thread_details[tasks_found].extra_info_str = malloc(
  329. sizeof(running_str));
  330. strcpy(rtos->thread_details[tasks_found].extra_info_str,
  331. running_str);
  332. } else
  333. rtos->thread_details[tasks_found].extra_info_str = NULL;
  334. tasks_found++;
  335. list_thread_count--;
  336. prev_list_elem_ptr = list_elem_ptr;
  337. list_elem_ptr = 0;
  338. retval = target_read_u32(rtos->target,
  339. prev_list_elem_ptr + param->list_elem_next_offset,
  340. &list_elem_ptr);
  341. if (retval != ERROR_OK) {
  342. LOG_ERROR("Error reading next thread item location in FreeRTOS thread list");
  343. free(list_of_lists);
  344. return retval;
  345. }
  346. LOG_DEBUG("FreeRTOS: Read next thread location at 0x%" PRIx32 ", value 0x%" PRIx32,
  347. prev_list_elem_ptr + param->list_elem_next_offset,
  348. list_elem_ptr);
  349. }
  350. }
  351. free(list_of_lists);
  352. rtos->thread_count = tasks_found;
  353. return 0;
  354. }
  355. static int freertos_get_thread_reg_list(struct rtos *rtos, int64_t thread_id,
  356. struct rtos_reg **reg_list, int *num_regs)
  357. {
  358. int retval;
  359. const struct freertos_params *param;
  360. int64_t stack_ptr = 0;
  361. if (!rtos)
  362. return -1;
  363. if (thread_id == 0)
  364. return -2;
  365. if (!rtos->rtos_specific_params)
  366. return -1;
  367. param = (const struct freertos_params *) rtos->rtos_specific_params;
  368. /* Read the stack pointer */
  369. uint32_t pointer_casts_are_bad;
  370. retval = target_read_u32(rtos->target,
  371. thread_id + param->thread_stack_offset,
  372. &pointer_casts_are_bad);
  373. if (retval != ERROR_OK) {
  374. LOG_ERROR("Error reading stack frame from FreeRTOS thread");
  375. return retval;
  376. }
  377. stack_ptr = pointer_casts_are_bad;
  378. LOG_DEBUG("FreeRTOS: Read stack pointer at 0x%" PRIx64 ", value 0x%" PRIx64,
  379. thread_id + param->thread_stack_offset,
  380. stack_ptr);
  381. /* Check for armv7m with *enabled* FPU, i.e. a Cortex-M4F */
  382. int cm4_fpu_enabled = 0;
  383. struct armv7m_common *armv7m_target = target_to_armv7m(rtos->target);
  384. if (is_armv7m(armv7m_target)) {
  385. if (armv7m_target->fp_feature == FPV4_SP) {
  386. /* Found ARM v7m target which includes a FPU */
  387. uint32_t cpacr;
  388. retval = target_read_u32(rtos->target, FPU_CPACR, &cpacr);
  389. if (retval != ERROR_OK) {
  390. LOG_ERROR("Could not read CPACR register to check FPU state");
  391. return -1;
  392. }
  393. /* Check if CP10 and CP11 are set to full access. */
  394. if (cpacr & 0x00F00000) {
  395. /* Found target with enabled FPU */
  396. cm4_fpu_enabled = 1;
  397. }
  398. }
  399. }
  400. if (cm4_fpu_enabled == 1) {
  401. /* Read the LR to decide between stacking with or without FPU */
  402. uint32_t lr_svc = 0;
  403. retval = target_read_u32(rtos->target,
  404. stack_ptr + 0x20,
  405. &lr_svc);
  406. if (retval != ERROR_OK) {
  407. LOG_OUTPUT("Error reading stack frame from FreeRTOS thread");
  408. return retval;
  409. }
  410. if ((lr_svc & 0x10) == 0)
  411. return rtos_generic_stack_read(rtos->target, param->stacking_info_cm4f_fpu, stack_ptr, reg_list, num_regs);
  412. else
  413. return rtos_generic_stack_read(rtos->target, param->stacking_info_cm4f, stack_ptr, reg_list, num_regs);
  414. } else
  415. return rtos_generic_stack_read(rtos->target, param->stacking_info_cm3, stack_ptr, reg_list, num_regs);
  416. }
  417. static int freertos_get_symbol_list_to_lookup(struct symbol_table_elem *symbol_list[])
  418. {
  419. unsigned int i;
  420. *symbol_list = calloc(
  421. ARRAY_SIZE(freertos_symbol_list), sizeof(struct symbol_table_elem));
  422. for (i = 0; i < ARRAY_SIZE(freertos_symbol_list); i++) {
  423. (*symbol_list)[i].symbol_name = freertos_symbol_list[i].name;
  424. (*symbol_list)[i].optional = freertos_symbol_list[i].optional;
  425. }
  426. return 0;
  427. }
  428. #if 0
  429. static int freertos_set_current_thread(struct rtos *rtos, threadid_t thread_id)
  430. {
  431. return 0;
  432. }
  433. static int freertos_get_thread_ascii_info(struct rtos *rtos, threadid_t thread_id, char **info)
  434. {
  435. int retval;
  436. const struct freertos_params *param;
  437. if (!rtos)
  438. return -1;
  439. if (thread_id == 0)
  440. return -2;
  441. if (!rtos->rtos_specific_params)
  442. return -3;
  443. param = (const struct freertos_params *) rtos->rtos_specific_params;
  444. #define FREERTOS_THREAD_NAME_STR_SIZE (200)
  445. char tmp_str[FREERTOS_THREAD_NAME_STR_SIZE];
  446. /* Read the thread name */
  447. retval = target_read_buffer(rtos->target,
  448. thread_id + param->thread_name_offset,
  449. FREERTOS_THREAD_NAME_STR_SIZE,
  450. (uint8_t *)&tmp_str);
  451. if (retval != ERROR_OK) {
  452. LOG_ERROR("Error reading first thread item location in FreeRTOS thread list");
  453. return retval;
  454. }
  455. tmp_str[FREERTOS_THREAD_NAME_STR_SIZE-1] = '\x00';
  456. if (tmp_str[0] == '\x00')
  457. strcpy(tmp_str, "No Name");
  458. *info = malloc(strlen(tmp_str)+1);
  459. strcpy(*info, tmp_str);
  460. return 0;
  461. }
  462. #endif
  463. static bool freertos_detect_rtos(struct target *target)
  464. {
  465. if ((target->rtos->symbols) &&
  466. (target->rtos->symbols[FREERTOS_VAL_PX_READY_TASKS_LISTS].address != 0)) {
  467. /* looks like FreeRTOS */
  468. return true;
  469. }
  470. return false;
  471. }
  472. static int freertos_create(struct target *target)
  473. {
  474. for (unsigned int i = 0; i < ARRAY_SIZE(freertos_params_list); i++)
  475. if (strcmp(freertos_params_list[i].target_name, target->type->name) == 0) {
  476. target->rtos->rtos_specific_params = (void *)&freertos_params_list[i];
  477. return 0;
  478. }
  479. LOG_ERROR("Could not find target in FreeRTOS compatibility list");
  480. return -1;
  481. }