dbexec.c 25 KB

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