dbexec.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  1. // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
  2. /*******************************************************************************
  3. *
  4. * Module Name: dbexec - debugger control method execution
  5. *
  6. ******************************************************************************/
  7. #include <acpi/acpi.h>
  8. #include "accommon.h"
  9. #include "acdebug.h"
  10. #include "acnamesp.h"
  11. #define _COMPONENT ACPI_CA_DEBUGGER
  12. ACPI_MODULE_NAME("dbexec")
  13. static struct acpi_db_method_info acpi_gbl_db_method_info;
  14. /* Local prototypes */
  15. static acpi_status
  16. acpi_db_execute_method(struct acpi_db_method_info *info,
  17. struct acpi_buffer *return_obj);
  18. static acpi_status acpi_db_execute_setup(struct acpi_db_method_info *info);
  19. static u32 acpi_db_get_outstanding_allocations(void);
  20. static void ACPI_SYSTEM_XFACE acpi_db_method_thread(void *context);
  21. static acpi_status
  22. acpi_db_execution_walk(acpi_handle obj_handle,
  23. u32 nesting_level, void *context, void **return_value);
  24. static void ACPI_SYSTEM_XFACE acpi_db_single_execution_thread(void *context);
  25. /*******************************************************************************
  26. *
  27. * FUNCTION: acpi_db_delete_objects
  28. *
  29. * PARAMETERS: count - Count of objects in the list
  30. * objects - Array of ACPI_OBJECTs to be deleted
  31. *
  32. * RETURN: None
  33. *
  34. * DESCRIPTION: Delete a list of ACPI_OBJECTS. Handles packages and nested
  35. * packages via recursion.
  36. *
  37. ******************************************************************************/
  38. void acpi_db_delete_objects(u32 count, union acpi_object *objects)
  39. {
  40. u32 i;
  41. for (i = 0; i < count; i++) {
  42. switch (objects[i].type) {
  43. case ACPI_TYPE_BUFFER:
  44. ACPI_FREE(objects[i].buffer.pointer);
  45. break;
  46. case ACPI_TYPE_PACKAGE:
  47. /* Recursive call to delete package elements */
  48. acpi_db_delete_objects(objects[i].package.count,
  49. objects[i].package.elements);
  50. /* Free the elements array */
  51. ACPI_FREE(objects[i].package.elements);
  52. break;
  53. default:
  54. break;
  55. }
  56. }
  57. }
  58. /*******************************************************************************
  59. *
  60. * FUNCTION: acpi_db_execute_method
  61. *
  62. * PARAMETERS: info - Valid info segment
  63. * return_obj - Where to put return object
  64. *
  65. * RETURN: Status
  66. *
  67. * DESCRIPTION: Execute a control method.
  68. *
  69. ******************************************************************************/
  70. static acpi_status
  71. acpi_db_execute_method(struct acpi_db_method_info *info,
  72. struct acpi_buffer *return_obj)
  73. {
  74. acpi_status status;
  75. struct acpi_object_list param_objects;
  76. union acpi_object params[ACPI_DEBUGGER_MAX_ARGS + 1];
  77. u32 i;
  78. ACPI_FUNCTION_TRACE(db_execute_method);
  79. if (acpi_gbl_db_output_to_file && !acpi_dbg_level) {
  80. acpi_os_printf("Warning: debug output is not enabled!\n");
  81. }
  82. param_objects.count = 0;
  83. param_objects.pointer = NULL;
  84. /* Pass through any command-line arguments */
  85. if (info->args && info->args[0]) {
  86. /* Get arguments passed on the command line */
  87. for (i = 0; (info->args[i] && *(info->args[i])); i++) {
  88. /* Convert input string (token) to an actual union acpi_object */
  89. status = acpi_db_convert_to_object(info->types[i],
  90. info->args[i],
  91. &params[i]);
  92. if (ACPI_FAILURE(status)) {
  93. ACPI_EXCEPTION((AE_INFO, status,
  94. "While parsing method arguments"));
  95. goto cleanup;
  96. }
  97. }
  98. param_objects.count = i;
  99. param_objects.pointer = params;
  100. }
  101. /* Prepare for a return object of arbitrary size */
  102. return_obj->pointer = acpi_gbl_db_buffer;
  103. return_obj->length = ACPI_DEBUG_BUFFER_SIZE;
  104. /* Do the actual method execution */
  105. acpi_gbl_method_executing = TRUE;
  106. status = acpi_evaluate_object(NULL, info->pathname,
  107. &param_objects, return_obj);
  108. acpi_gbl_cm_single_step = FALSE;
  109. acpi_gbl_method_executing = FALSE;
  110. if (ACPI_FAILURE(status)) {
  111. if ((status == AE_ABORT_METHOD) || acpi_gbl_abort_method) {
  112. /* Clear the abort and fall back to the debugger prompt */
  113. ACPI_EXCEPTION((AE_INFO, status,
  114. "Aborting top-level method"));
  115. acpi_gbl_abort_method = FALSE;
  116. status = AE_OK;
  117. goto cleanup;
  118. }
  119. ACPI_EXCEPTION((AE_INFO, status,
  120. "while executing %s from debugger",
  121. info->pathname));
  122. if (status == AE_BUFFER_OVERFLOW) {
  123. ACPI_ERROR((AE_INFO,
  124. "Possible overflow of internal debugger "
  125. "buffer (size 0x%X needed 0x%X)",
  126. ACPI_DEBUG_BUFFER_SIZE,
  127. (u32)return_obj->length));
  128. }
  129. }
  130. cleanup:
  131. acpi_db_delete_objects(param_objects.count, params);
  132. return_ACPI_STATUS(status);
  133. }
  134. /*******************************************************************************
  135. *
  136. * FUNCTION: acpi_db_execute_setup
  137. *
  138. * PARAMETERS: info - Valid method info
  139. *
  140. * RETURN: None
  141. *
  142. * DESCRIPTION: Setup info segment prior to method execution
  143. *
  144. ******************************************************************************/
  145. static acpi_status acpi_db_execute_setup(struct acpi_db_method_info *info)
  146. {
  147. acpi_status status;
  148. ACPI_FUNCTION_NAME(db_execute_setup);
  149. /* Concatenate the current scope to the supplied name */
  150. info->pathname[0] = 0;
  151. if ((info->name[0] != '\\') && (info->name[0] != '/')) {
  152. if (acpi_ut_safe_strcat(info->pathname, sizeof(info->pathname),
  153. acpi_gbl_db_scope_buf)) {
  154. status = AE_BUFFER_OVERFLOW;
  155. goto error_exit;
  156. }
  157. }
  158. if (acpi_ut_safe_strcat(info->pathname, sizeof(info->pathname),
  159. info->name)) {
  160. status = AE_BUFFER_OVERFLOW;
  161. goto error_exit;
  162. }
  163. acpi_db_prep_namestring(info->pathname);
  164. acpi_db_set_output_destination(ACPI_DB_DUPLICATE_OUTPUT);
  165. acpi_os_printf("Evaluating %s\n", info->pathname);
  166. if (info->flags & EX_SINGLE_STEP) {
  167. acpi_gbl_cm_single_step = TRUE;
  168. acpi_db_set_output_destination(ACPI_DB_CONSOLE_OUTPUT);
  169. }
  170. else {
  171. /* No single step, allow redirection to a file */
  172. acpi_db_set_output_destination(ACPI_DB_REDIRECTABLE_OUTPUT);
  173. }
  174. return (AE_OK);
  175. error_exit:
  176. ACPI_EXCEPTION((AE_INFO, status, "During setup for method execution"));
  177. return (status);
  178. }
  179. #ifdef ACPI_DBG_TRACK_ALLOCATIONS
  180. u32 acpi_db_get_cache_info(struct acpi_memory_list *cache)
  181. {
  182. return (cache->total_allocated - cache->total_freed -
  183. cache->current_depth);
  184. }
  185. #endif
  186. /*******************************************************************************
  187. *
  188. * FUNCTION: acpi_db_get_outstanding_allocations
  189. *
  190. * PARAMETERS: None
  191. *
  192. * RETURN: Current global allocation count minus cache entries
  193. *
  194. * DESCRIPTION: Determine the current number of "outstanding" allocations --
  195. * those allocations that have not been freed and also are not
  196. * in one of the various object caches.
  197. *
  198. ******************************************************************************/
  199. static u32 acpi_db_get_outstanding_allocations(void)
  200. {
  201. u32 outstanding = 0;
  202. #ifdef ACPI_DBG_TRACK_ALLOCATIONS
  203. outstanding += acpi_db_get_cache_info(acpi_gbl_state_cache);
  204. outstanding += acpi_db_get_cache_info(acpi_gbl_ps_node_cache);
  205. outstanding += acpi_db_get_cache_info(acpi_gbl_ps_node_ext_cache);
  206. outstanding += acpi_db_get_cache_info(acpi_gbl_operand_cache);
  207. #endif
  208. return (outstanding);
  209. }
  210. /*******************************************************************************
  211. *
  212. * FUNCTION: acpi_db_execution_walk
  213. *
  214. * PARAMETERS: WALK_CALLBACK
  215. *
  216. * RETURN: Status
  217. *
  218. * DESCRIPTION: Execute a control method. Name is relative to the current
  219. * scope.
  220. *
  221. ******************************************************************************/
  222. static acpi_status
  223. acpi_db_execution_walk(acpi_handle obj_handle,
  224. u32 nesting_level, void *context, void **return_value)
  225. {
  226. union acpi_operand_object *obj_desc;
  227. struct acpi_namespace_node *node =
  228. (struct acpi_namespace_node *)obj_handle;
  229. struct acpi_buffer return_obj;
  230. acpi_status status;
  231. obj_desc = acpi_ns_get_attached_object(node);
  232. if (obj_desc->method.param_count) {
  233. return (AE_OK);
  234. }
  235. return_obj.pointer = NULL;
  236. return_obj.length = ACPI_ALLOCATE_BUFFER;
  237. acpi_ns_print_node_pathname(node, "Evaluating");
  238. /* Do the actual method execution */
  239. acpi_os_printf("\n");
  240. acpi_gbl_method_executing = TRUE;
  241. status = acpi_evaluate_object(node, NULL, NULL, &return_obj);
  242. acpi_os_printf("Evaluation of [%4.4s] returned %s\n",
  243. acpi_ut_get_node_name(node),
  244. acpi_format_exception(status));
  245. acpi_gbl_method_executing = FALSE;
  246. return (AE_OK);
  247. }
  248. /*******************************************************************************
  249. *
  250. * FUNCTION: acpi_db_execute
  251. *
  252. * PARAMETERS: name - Name of method to execute
  253. * args - Parameters to the method
  254. * Types -
  255. * flags - single step/no single step
  256. *
  257. * RETURN: None
  258. *
  259. * DESCRIPTION: Execute a control method. Name is relative to the current
  260. * scope.
  261. *
  262. ******************************************************************************/
  263. void
  264. acpi_db_execute(char *name, char **args, acpi_object_type *types, u32 flags)
  265. {
  266. acpi_status status;
  267. struct acpi_buffer return_obj;
  268. char *name_string;
  269. #ifdef ACPI_DEBUG_OUTPUT
  270. u32 previous_allocations;
  271. u32 allocations;
  272. #endif
  273. /*
  274. * Allow one execution to be performed by debugger or single step
  275. * execution will be dead locked by the interpreter mutexes.
  276. */
  277. if (acpi_gbl_method_executing) {
  278. acpi_os_printf("Only one debugger execution is allowed.\n");
  279. return;
  280. }
  281. #ifdef ACPI_DEBUG_OUTPUT
  282. /* Memory allocation tracking */
  283. previous_allocations = acpi_db_get_outstanding_allocations();
  284. #endif
  285. if (*name == '*') {
  286. (void)acpi_walk_namespace(ACPI_TYPE_METHOD, ACPI_ROOT_OBJECT,
  287. ACPI_UINT32_MAX,
  288. acpi_db_execution_walk, NULL, NULL,
  289. NULL);
  290. return;
  291. }
  292. name_string = ACPI_ALLOCATE(strlen(name) + 1);
  293. if (!name_string) {
  294. return;
  295. }
  296. memset(&acpi_gbl_db_method_info, 0, sizeof(struct acpi_db_method_info));
  297. strcpy(name_string, name);
  298. acpi_ut_strupr(name_string);
  299. /* Subcommand to Execute all predefined names in the namespace */
  300. if (!strncmp(name_string, "PREDEF", 6)) {
  301. acpi_db_evaluate_predefined_names();
  302. ACPI_FREE(name_string);
  303. return;
  304. }
  305. acpi_gbl_db_method_info.name = name_string;
  306. acpi_gbl_db_method_info.args = args;
  307. acpi_gbl_db_method_info.types = types;
  308. acpi_gbl_db_method_info.flags = flags;
  309. return_obj.pointer = NULL;
  310. return_obj.length = ACPI_ALLOCATE_BUFFER;
  311. status = acpi_db_execute_setup(&acpi_gbl_db_method_info);
  312. if (ACPI_FAILURE(status)) {
  313. ACPI_FREE(name_string);
  314. return;
  315. }
  316. /* Get the NS node, determines existence also */
  317. status = acpi_get_handle(NULL, acpi_gbl_db_method_info.pathname,
  318. &acpi_gbl_db_method_info.method);
  319. if (ACPI_SUCCESS(status)) {
  320. status = acpi_db_execute_method(&acpi_gbl_db_method_info,
  321. &return_obj);
  322. }
  323. ACPI_FREE(name_string);
  324. /*
  325. * Allow any handlers in separate threads to complete.
  326. * (Such as Notify handlers invoked from AML executed above).
  327. */
  328. acpi_os_sleep((u64)10);
  329. #ifdef ACPI_DEBUG_OUTPUT
  330. /* Memory allocation tracking */
  331. allocations =
  332. acpi_db_get_outstanding_allocations() - previous_allocations;
  333. acpi_db_set_output_destination(ACPI_DB_DUPLICATE_OUTPUT);
  334. if (allocations > 0) {
  335. acpi_os_printf
  336. ("0x%X Outstanding allocations after evaluation of %s\n",
  337. allocations, acpi_gbl_db_method_info.pathname);
  338. }
  339. #endif
  340. if (ACPI_FAILURE(status)) {
  341. acpi_os_printf("Evaluation of %s failed with status %s\n",
  342. acpi_gbl_db_method_info.pathname,
  343. acpi_format_exception(status));
  344. } else {
  345. /* Display a return object, if any */
  346. if (return_obj.length) {
  347. acpi_os_printf("Evaluation of %s returned object %p, "
  348. "external buffer length %X\n",
  349. acpi_gbl_db_method_info.pathname,
  350. return_obj.pointer,
  351. (u32)return_obj.length);
  352. acpi_db_dump_external_object(return_obj.pointer, 1);
  353. /* Dump a _PLD buffer if present */
  354. if (ACPI_COMPARE_NAME
  355. ((ACPI_CAST_PTR
  356. (struct acpi_namespace_node,
  357. acpi_gbl_db_method_info.method)->name.ascii),
  358. METHOD_NAME__PLD)) {
  359. acpi_db_dump_pld_buffer(return_obj.pointer);
  360. }
  361. } else {
  362. acpi_os_printf
  363. ("No object was returned from evaluation of %s\n",
  364. acpi_gbl_db_method_info.pathname);
  365. }
  366. }
  367. acpi_db_set_output_destination(ACPI_DB_CONSOLE_OUTPUT);
  368. }
  369. /*******************************************************************************
  370. *
  371. * FUNCTION: acpi_db_method_thread
  372. *
  373. * PARAMETERS: context - Execution info segment
  374. *
  375. * RETURN: None
  376. *
  377. * DESCRIPTION: Debugger execute thread. Waits for a command line, then
  378. * simply dispatches it.
  379. *
  380. ******************************************************************************/
  381. static void ACPI_SYSTEM_XFACE acpi_db_method_thread(void *context)
  382. {
  383. acpi_status status;
  384. struct acpi_db_method_info *info = context;
  385. struct acpi_db_method_info local_info;
  386. u32 i;
  387. u8 allow;
  388. struct acpi_buffer return_obj;
  389. /*
  390. * acpi_gbl_db_method_info.Arguments will be passed as method arguments.
  391. * Prevent acpi_gbl_db_method_info from being modified by multiple threads
  392. * concurrently.
  393. *
  394. * Note: The arguments we are passing are used by the ASL test suite
  395. * (aslts). Do not change them without updating the tests.
  396. */
  397. (void)acpi_os_wait_semaphore(info->info_gate, 1, ACPI_WAIT_FOREVER);
  398. if (info->init_args) {
  399. acpi_db_uint32_to_hex_string(info->num_created,
  400. info->index_of_thread_str);
  401. acpi_db_uint32_to_hex_string((u32)acpi_os_get_thread_id(),
  402. info->id_of_thread_str);
  403. }
  404. if (info->threads && (info->num_created < info->num_threads)) {
  405. info->threads[info->num_created++] = acpi_os_get_thread_id();
  406. }
  407. local_info = *info;
  408. local_info.args = local_info.arguments;
  409. local_info.arguments[0] = local_info.num_threads_str;
  410. local_info.arguments[1] = local_info.id_of_thread_str;
  411. local_info.arguments[2] = local_info.index_of_thread_str;
  412. local_info.arguments[3] = NULL;
  413. local_info.types = local_info.arg_types;
  414. (void)acpi_os_signal_semaphore(info->info_gate, 1);
  415. for (i = 0; i < info->num_loops; i++) {
  416. status = acpi_db_execute_method(&local_info, &return_obj);
  417. if (ACPI_FAILURE(status)) {
  418. acpi_os_printf
  419. ("%s During evaluation of %s at iteration %X\n",
  420. acpi_format_exception(status), info->pathname, i);
  421. if (status == AE_ABORT_METHOD) {
  422. break;
  423. }
  424. }
  425. #if 0
  426. if ((i % 100) == 0) {
  427. acpi_os_printf("%u loops, Thread 0x%x\n",
  428. i, acpi_os_get_thread_id());
  429. }
  430. if (return_obj.length) {
  431. acpi_os_printf
  432. ("Evaluation of %s returned object %p Buflen %X\n",
  433. info->pathname, return_obj.pointer,
  434. (u32)return_obj.length);
  435. acpi_db_dump_external_object(return_obj.pointer, 1);
  436. }
  437. #endif
  438. }
  439. /* Signal our completion */
  440. allow = 0;
  441. (void)acpi_os_wait_semaphore(info->thread_complete_gate,
  442. 1, ACPI_WAIT_FOREVER);
  443. info->num_completed++;
  444. if (info->num_completed == info->num_threads) {
  445. /* Do signal for main thread once only */
  446. allow = 1;
  447. }
  448. (void)acpi_os_signal_semaphore(info->thread_complete_gate, 1);
  449. if (allow) {
  450. status = acpi_os_signal_semaphore(info->main_thread_gate, 1);
  451. if (ACPI_FAILURE(status)) {
  452. acpi_os_printf
  453. ("Could not signal debugger thread sync semaphore, %s\n",
  454. acpi_format_exception(status));
  455. }
  456. }
  457. }
  458. /*******************************************************************************
  459. *
  460. * FUNCTION: acpi_db_single_execution_thread
  461. *
  462. * PARAMETERS: context - Method info struct
  463. *
  464. * RETURN: None
  465. *
  466. * DESCRIPTION: Create one thread and execute a method
  467. *
  468. ******************************************************************************/
  469. static void ACPI_SYSTEM_XFACE acpi_db_single_execution_thread(void *context)
  470. {
  471. struct acpi_db_method_info *info = context;
  472. acpi_status status;
  473. struct acpi_buffer return_obj;
  474. acpi_os_printf("\n");
  475. status = acpi_db_execute_method(info, &return_obj);
  476. if (ACPI_FAILURE(status)) {
  477. acpi_os_printf("%s During evaluation of %s\n",
  478. acpi_format_exception(status), info->pathname);
  479. return;
  480. }
  481. /* Display a return object, if any */
  482. if (return_obj.length) {
  483. acpi_os_printf("Evaluation of %s returned object %p, "
  484. "external buffer length %X\n",
  485. acpi_gbl_db_method_info.pathname,
  486. return_obj.pointer, (u32)return_obj.length);
  487. acpi_db_dump_external_object(return_obj.pointer, 1);
  488. }
  489. acpi_os_printf("\nBackground thread completed\n%c ",
  490. ACPI_DEBUGGER_COMMAND_PROMPT);
  491. }
  492. /*******************************************************************************
  493. *
  494. * FUNCTION: acpi_db_create_execution_thread
  495. *
  496. * PARAMETERS: method_name_arg - Control method to execute
  497. * arguments - Array of arguments to the method
  498. * types - Corresponding array of object types
  499. *
  500. * RETURN: None
  501. *
  502. * DESCRIPTION: Create a single thread to evaluate a namespace object. Handles
  503. * arguments passed on command line for control methods.
  504. *
  505. ******************************************************************************/
  506. void
  507. acpi_db_create_execution_thread(char *method_name_arg,
  508. char **arguments, acpi_object_type *types)
  509. {
  510. acpi_status status;
  511. u32 i;
  512. memset(&acpi_gbl_db_method_info, 0, sizeof(struct acpi_db_method_info));
  513. acpi_gbl_db_method_info.name = method_name_arg;
  514. acpi_gbl_db_method_info.init_args = 1;
  515. acpi_gbl_db_method_info.args = acpi_gbl_db_method_info.arguments;
  516. acpi_gbl_db_method_info.types = acpi_gbl_db_method_info.arg_types;
  517. /* Setup method arguments, up to 7 (0-6) */
  518. for (i = 0; (i < ACPI_METHOD_NUM_ARGS) && *arguments; i++) {
  519. acpi_gbl_db_method_info.arguments[i] = *arguments;
  520. arguments++;
  521. acpi_gbl_db_method_info.arg_types[i] = *types;
  522. types++;
  523. }
  524. status = acpi_db_execute_setup(&acpi_gbl_db_method_info);
  525. if (ACPI_FAILURE(status)) {
  526. return;
  527. }
  528. /* Get the NS node, determines existence also */
  529. status = acpi_get_handle(NULL, acpi_gbl_db_method_info.pathname,
  530. &acpi_gbl_db_method_info.method);
  531. if (ACPI_FAILURE(status)) {
  532. acpi_os_printf("%s Could not get handle for %s\n",
  533. acpi_format_exception(status),
  534. acpi_gbl_db_method_info.pathname);
  535. return;
  536. }
  537. status = acpi_os_execute(OSL_DEBUGGER_EXEC_THREAD,
  538. acpi_db_single_execution_thread,
  539. &acpi_gbl_db_method_info);
  540. if (ACPI_FAILURE(status)) {
  541. return;
  542. }
  543. acpi_os_printf("\nBackground thread started\n");
  544. }
  545. /*******************************************************************************
  546. *
  547. * FUNCTION: acpi_db_create_execution_threads
  548. *
  549. * PARAMETERS: num_threads_arg - Number of threads to create
  550. * num_loops_arg - Loop count for the thread(s)
  551. * method_name_arg - Control method to execute
  552. *
  553. * RETURN: None
  554. *
  555. * DESCRIPTION: Create threads to execute method(s)
  556. *
  557. ******************************************************************************/
  558. void
  559. acpi_db_create_execution_threads(char *num_threads_arg,
  560. char *num_loops_arg, char *method_name_arg)
  561. {
  562. acpi_status status;
  563. u32 num_threads;
  564. u32 num_loops;
  565. u32 i;
  566. u32 size;
  567. acpi_mutex main_thread_gate;
  568. acpi_mutex thread_complete_gate;
  569. acpi_mutex info_gate;
  570. /* Get the arguments */
  571. num_threads = strtoul(num_threads_arg, NULL, 0);
  572. num_loops = strtoul(num_loops_arg, NULL, 0);
  573. if (!num_threads || !num_loops) {
  574. acpi_os_printf("Bad argument: Threads %X, Loops %X\n",
  575. num_threads, num_loops);
  576. return;
  577. }
  578. /*
  579. * Create the semaphore for synchronization of
  580. * the created threads with the main thread.
  581. */
  582. status = acpi_os_create_semaphore(1, 0, &main_thread_gate);
  583. if (ACPI_FAILURE(status)) {
  584. acpi_os_printf("Could not create semaphore for "
  585. "synchronization with the main thread, %s\n",
  586. acpi_format_exception(status));
  587. return;
  588. }
  589. /*
  590. * Create the semaphore for synchronization
  591. * between the created threads.
  592. */
  593. status = acpi_os_create_semaphore(1, 1, &thread_complete_gate);
  594. if (ACPI_FAILURE(status)) {
  595. acpi_os_printf("Could not create semaphore for "
  596. "synchronization between the created threads, %s\n",
  597. acpi_format_exception(status));
  598. (void)acpi_os_delete_semaphore(main_thread_gate);
  599. return;
  600. }
  601. status = acpi_os_create_semaphore(1, 1, &info_gate);
  602. if (ACPI_FAILURE(status)) {
  603. acpi_os_printf("Could not create semaphore for "
  604. "synchronization of AcpiGbl_DbMethodInfo, %s\n",
  605. acpi_format_exception(status));
  606. (void)acpi_os_delete_semaphore(thread_complete_gate);
  607. (void)acpi_os_delete_semaphore(main_thread_gate);
  608. return;
  609. }
  610. memset(&acpi_gbl_db_method_info, 0, sizeof(struct acpi_db_method_info));
  611. /* Array to store IDs of threads */
  612. acpi_gbl_db_method_info.num_threads = num_threads;
  613. size = sizeof(acpi_thread_id) * acpi_gbl_db_method_info.num_threads;
  614. acpi_gbl_db_method_info.threads = acpi_os_allocate(size);
  615. if (acpi_gbl_db_method_info.threads == NULL) {
  616. acpi_os_printf("No memory for thread IDs array\n");
  617. (void)acpi_os_delete_semaphore(main_thread_gate);
  618. (void)acpi_os_delete_semaphore(thread_complete_gate);
  619. (void)acpi_os_delete_semaphore(info_gate);
  620. return;
  621. }
  622. memset(acpi_gbl_db_method_info.threads, 0, size);
  623. /* Setup the context to be passed to each thread */
  624. acpi_gbl_db_method_info.name = method_name_arg;
  625. acpi_gbl_db_method_info.flags = 0;
  626. acpi_gbl_db_method_info.num_loops = num_loops;
  627. acpi_gbl_db_method_info.main_thread_gate = main_thread_gate;
  628. acpi_gbl_db_method_info.thread_complete_gate = thread_complete_gate;
  629. acpi_gbl_db_method_info.info_gate = info_gate;
  630. /* Init arguments to be passed to method */
  631. acpi_gbl_db_method_info.init_args = 1;
  632. acpi_gbl_db_method_info.args = acpi_gbl_db_method_info.arguments;
  633. acpi_gbl_db_method_info.arguments[0] =
  634. acpi_gbl_db_method_info.num_threads_str;
  635. acpi_gbl_db_method_info.arguments[1] =
  636. acpi_gbl_db_method_info.id_of_thread_str;
  637. acpi_gbl_db_method_info.arguments[2] =
  638. acpi_gbl_db_method_info.index_of_thread_str;
  639. acpi_gbl_db_method_info.arguments[3] = NULL;
  640. acpi_gbl_db_method_info.types = acpi_gbl_db_method_info.arg_types;
  641. acpi_gbl_db_method_info.arg_types[0] = ACPI_TYPE_INTEGER;
  642. acpi_gbl_db_method_info.arg_types[1] = ACPI_TYPE_INTEGER;
  643. acpi_gbl_db_method_info.arg_types[2] = ACPI_TYPE_INTEGER;
  644. acpi_db_uint32_to_hex_string(num_threads,
  645. acpi_gbl_db_method_info.num_threads_str);
  646. status = acpi_db_execute_setup(&acpi_gbl_db_method_info);
  647. if (ACPI_FAILURE(status)) {
  648. goto cleanup_and_exit;
  649. }
  650. /* Get the NS node, determines existence also */
  651. status = acpi_get_handle(NULL, acpi_gbl_db_method_info.pathname,
  652. &acpi_gbl_db_method_info.method);
  653. if (ACPI_FAILURE(status)) {
  654. acpi_os_printf("%s Could not get handle for %s\n",
  655. acpi_format_exception(status),
  656. acpi_gbl_db_method_info.pathname);
  657. goto cleanup_and_exit;
  658. }
  659. /* Create the threads */
  660. acpi_os_printf("Creating %X threads to execute %X times each\n",
  661. num_threads, num_loops);
  662. for (i = 0; i < (num_threads); i++) {
  663. status =
  664. acpi_os_execute(OSL_DEBUGGER_EXEC_THREAD,
  665. acpi_db_method_thread,
  666. &acpi_gbl_db_method_info);
  667. if (ACPI_FAILURE(status)) {
  668. break;
  669. }
  670. }
  671. /* Wait for all threads to complete */
  672. (void)acpi_os_wait_semaphore(main_thread_gate, 1, ACPI_WAIT_FOREVER);
  673. acpi_db_set_output_destination(ACPI_DB_DUPLICATE_OUTPUT);
  674. acpi_os_printf("All threads (%X) have completed\n", num_threads);
  675. acpi_db_set_output_destination(ACPI_DB_CONSOLE_OUTPUT);
  676. cleanup_and_exit:
  677. /* Cleanup and exit */
  678. (void)acpi_os_delete_semaphore(main_thread_gate);
  679. (void)acpi_os_delete_semaphore(thread_complete_gate);
  680. (void)acpi_os_delete_semaphore(info_gate);
  681. acpi_os_free(acpi_gbl_db_method_info.threads);
  682. acpi_gbl_db_method_info.threads = NULL;
  683. }