dsmethod.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  1. /******************************************************************************
  2. *
  3. * Module Name: dsmethod - Parser/Interpreter interface - control method parsing
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2005, R. Byron Moore
  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 <acpi/acparser.h>
  44. #include <acpi/amlcode.h>
  45. #include <acpi/acdispat.h>
  46. #include <acpi/acinterp.h>
  47. #include <acpi/acnamesp.h>
  48. #define _COMPONENT ACPI_DISPATCHER
  49. ACPI_MODULE_NAME("dsmethod")
  50. /*******************************************************************************
  51. *
  52. * FUNCTION: acpi_ds_begin_method_execution
  53. *
  54. * PARAMETERS: method_node - Node of the method
  55. * obj_desc - The method object
  56. * calling_method_node - Caller of this method (if non-null)
  57. *
  58. * RETURN: Status
  59. *
  60. * DESCRIPTION: Prepare a method for execution. Parses the method if necessary,
  61. * increments the thread count, and waits at the method semaphore
  62. * for clearance to execute.
  63. *
  64. ******************************************************************************/
  65. acpi_status
  66. acpi_ds_begin_method_execution(struct acpi_namespace_node *method_node,
  67. union acpi_operand_object *obj_desc,
  68. struct acpi_namespace_node *calling_method_node)
  69. {
  70. acpi_status status = AE_OK;
  71. ACPI_FUNCTION_TRACE_PTR("ds_begin_method_execution", method_node);
  72. if (!method_node) {
  73. return_ACPI_STATUS(AE_NULL_ENTRY);
  74. }
  75. /* Prevent wraparound of thread count */
  76. if (obj_desc->method.thread_count == ACPI_UINT8_MAX) {
  77. ACPI_REPORT_ERROR(("Method reached maximum reentrancy limit (255)\n"));
  78. return_ACPI_STATUS(AE_AML_METHOD_LIMIT);
  79. }
  80. /*
  81. * If there is a concurrency limit on this method, we need to
  82. * obtain a unit from the method semaphore.
  83. */
  84. if (obj_desc->method.semaphore) {
  85. /*
  86. * Allow recursive method calls, up to the reentrancy/concurrency
  87. * limit imposed by the SERIALIZED rule and the sync_level method
  88. * parameter.
  89. *
  90. * The point of this code is to avoid permanently blocking a
  91. * thread that is making recursive method calls.
  92. */
  93. if (method_node == calling_method_node) {
  94. if (obj_desc->method.thread_count >=
  95. obj_desc->method.concurrency) {
  96. return_ACPI_STATUS(AE_AML_METHOD_LIMIT);
  97. }
  98. }
  99. /*
  100. * Get a unit from the method semaphore. This releases the
  101. * interpreter if we block
  102. */
  103. status =
  104. acpi_ex_system_wait_semaphore(obj_desc->method.semaphore,
  105. ACPI_WAIT_FOREVER);
  106. }
  107. /*
  108. * Allocate an Owner ID for this method, only if this is the first thread
  109. * to begin concurrent execution. We only need one owner_id, even if the
  110. * method is invoked recursively.
  111. */
  112. if (!obj_desc->method.owner_id) {
  113. status = acpi_ut_allocate_owner_id(&obj_desc->method.owner_id);
  114. if (ACPI_FAILURE(status)) {
  115. return_ACPI_STATUS(status);
  116. }
  117. }
  118. /*
  119. * Increment the method parse tree thread count since it has been
  120. * reentered one more time (even if it is the same thread)
  121. */
  122. obj_desc->method.thread_count++;
  123. return_ACPI_STATUS(status);
  124. }
  125. /*******************************************************************************
  126. *
  127. * FUNCTION: acpi_ds_call_control_method
  128. *
  129. * PARAMETERS: Thread - Info for this thread
  130. * this_walk_state - Current walk state
  131. * Op - Current Op to be walked
  132. *
  133. * RETURN: Status
  134. *
  135. * DESCRIPTION: Transfer execution to a called control method
  136. *
  137. ******************************************************************************/
  138. acpi_status
  139. acpi_ds_call_control_method(struct acpi_thread_state *thread,
  140. struct acpi_walk_state *this_walk_state,
  141. union acpi_parse_object *op)
  142. {
  143. acpi_status status;
  144. struct acpi_namespace_node *method_node;
  145. struct acpi_walk_state *next_walk_state = NULL;
  146. union acpi_operand_object *obj_desc;
  147. struct acpi_parameter_info info;
  148. u32 i;
  149. ACPI_FUNCTION_TRACE_PTR("ds_call_control_method", this_walk_state);
  150. ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
  151. "Execute method %p, currentstate=%p\n",
  152. this_walk_state->prev_op, this_walk_state));
  153. /*
  154. * Get the namespace entry for the control method we are about to call
  155. */
  156. method_node = this_walk_state->method_call_node;
  157. if (!method_node) {
  158. return_ACPI_STATUS(AE_NULL_ENTRY);
  159. }
  160. obj_desc = acpi_ns_get_attached_object(method_node);
  161. if (!obj_desc) {
  162. return_ACPI_STATUS(AE_NULL_OBJECT);
  163. }
  164. /* Init for new method, wait on concurrency semaphore */
  165. status = acpi_ds_begin_method_execution(method_node, obj_desc,
  166. this_walk_state->method_node);
  167. if (ACPI_FAILURE(status)) {
  168. goto cleanup;
  169. }
  170. if (!(obj_desc->method.method_flags & AML_METHOD_INTERNAL_ONLY)) {
  171. /* 1) Parse: Create a new walk state for the preempting walk */
  172. next_walk_state =
  173. acpi_ds_create_walk_state(obj_desc->method.owner_id, op,
  174. obj_desc, NULL);
  175. if (!next_walk_state) {
  176. return_ACPI_STATUS(AE_NO_MEMORY);
  177. }
  178. /* Create and init a Root Node */
  179. op = acpi_ps_create_scope_op();
  180. if (!op) {
  181. status = AE_NO_MEMORY;
  182. goto cleanup;
  183. }
  184. status = acpi_ds_init_aml_walk(next_walk_state, op, method_node,
  185. obj_desc->method.aml_start,
  186. obj_desc->method.aml_length,
  187. NULL, 1);
  188. if (ACPI_FAILURE(status)) {
  189. acpi_ds_delete_walk_state(next_walk_state);
  190. goto cleanup;
  191. }
  192. /* Begin AML parse */
  193. status = acpi_ps_parse_aml(next_walk_state);
  194. acpi_ps_delete_parse_tree(op);
  195. }
  196. /* 2) Execute: Create a new state for the preempting walk */
  197. next_walk_state = acpi_ds_create_walk_state(obj_desc->method.owner_id,
  198. NULL, obj_desc, thread);
  199. if (!next_walk_state) {
  200. status = AE_NO_MEMORY;
  201. goto cleanup;
  202. }
  203. /*
  204. * The resolved arguments were put on the previous walk state's operand
  205. * stack. Operands on the previous walk state stack always
  206. * start at index 0. Also, null terminate the list of arguments
  207. */
  208. this_walk_state->operands[this_walk_state->num_operands] = NULL;
  209. info.parameters = &this_walk_state->operands[0];
  210. info.parameter_type = ACPI_PARAM_ARGS;
  211. status = acpi_ds_init_aml_walk(next_walk_state, NULL, method_node,
  212. obj_desc->method.aml_start,
  213. obj_desc->method.aml_length, &info, 3);
  214. if (ACPI_FAILURE(status)) {
  215. goto cleanup;
  216. }
  217. /*
  218. * Delete the operands on the previous walkstate operand stack
  219. * (they were copied to new objects)
  220. */
  221. for (i = 0; i < obj_desc->method.param_count; i++) {
  222. acpi_ut_remove_reference(this_walk_state->operands[i]);
  223. this_walk_state->operands[i] = NULL;
  224. }
  225. /* Clear the operand stack */
  226. this_walk_state->num_operands = 0;
  227. ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
  228. "Starting nested execution, newstate=%p\n",
  229. next_walk_state));
  230. if (obj_desc->method.method_flags & AML_METHOD_INTERNAL_ONLY) {
  231. status = obj_desc->method.implementation(next_walk_state);
  232. }
  233. return_ACPI_STATUS(status);
  234. cleanup:
  235. /* Decrement the thread count on the method parse tree */
  236. if (next_walk_state && (next_walk_state->method_desc)) {
  237. next_walk_state->method_desc->method.thread_count--;
  238. }
  239. /* On error, we must delete the new walk state */
  240. acpi_ds_terminate_control_method(next_walk_state);
  241. acpi_ds_delete_walk_state(next_walk_state);
  242. return_ACPI_STATUS(status);
  243. }
  244. /*******************************************************************************
  245. *
  246. * FUNCTION: acpi_ds_restart_control_method
  247. *
  248. * PARAMETERS: walk_state - State for preempted method (caller)
  249. * return_desc - Return value from the called method
  250. *
  251. * RETURN: Status
  252. *
  253. * DESCRIPTION: Restart a method that was preempted by another (nested) method
  254. * invocation. Handle the return value (if any) from the callee.
  255. *
  256. ******************************************************************************/
  257. acpi_status
  258. acpi_ds_restart_control_method(struct acpi_walk_state *walk_state,
  259. union acpi_operand_object *return_desc)
  260. {
  261. acpi_status status;
  262. ACPI_FUNCTION_TRACE_PTR("ds_restart_control_method", walk_state);
  263. ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
  264. "****Restart [%4.4s] Op %p return_value_from_callee %p\n",
  265. (char *)&walk_state->method_node->name,
  266. walk_state->method_call_op, return_desc));
  267. ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
  268. " return_from_this_method_used?=%X res_stack %p Walk %p\n",
  269. walk_state->return_used,
  270. walk_state->results, walk_state));
  271. /* Did the called method return a value? */
  272. if (return_desc) {
  273. /* Are we actually going to use the return value? */
  274. if (walk_state->return_used) {
  275. /* Save the return value from the previous method */
  276. status = acpi_ds_result_push(return_desc, walk_state);
  277. if (ACPI_FAILURE(status)) {
  278. acpi_ut_remove_reference(return_desc);
  279. return_ACPI_STATUS(status);
  280. }
  281. /*
  282. * Save as THIS method's return value in case it is returned
  283. * immediately to yet another method
  284. */
  285. walk_state->return_desc = return_desc;
  286. }
  287. /*
  288. * The following code is the
  289. * optional support for a so-called "implicit return". Some AML code
  290. * assumes that the last value of the method is "implicitly" returned
  291. * to the caller. Just save the last result as the return value.
  292. * NOTE: this is optional because the ASL language does not actually
  293. * support this behavior.
  294. */
  295. else if (!acpi_ds_do_implicit_return
  296. (return_desc, walk_state, FALSE)) {
  297. /*
  298. * Delete the return value if it will not be used by the
  299. * calling method
  300. */
  301. acpi_ut_remove_reference(return_desc);
  302. }
  303. }
  304. return_ACPI_STATUS(AE_OK);
  305. }
  306. /*******************************************************************************
  307. *
  308. * FUNCTION: acpi_ds_terminate_control_method
  309. *
  310. * PARAMETERS: walk_state - State of the method
  311. *
  312. * RETURN: None
  313. *
  314. * DESCRIPTION: Terminate a control method. Delete everything that the method
  315. * created, delete all locals and arguments, and delete the parse
  316. * tree if requested.
  317. *
  318. ******************************************************************************/
  319. void acpi_ds_terminate_control_method(struct acpi_walk_state *walk_state)
  320. {
  321. union acpi_operand_object *obj_desc;
  322. struct acpi_namespace_node *method_node;
  323. acpi_status status;
  324. ACPI_FUNCTION_TRACE_PTR("ds_terminate_control_method", walk_state);
  325. if (!walk_state) {
  326. return_VOID;
  327. }
  328. /* The current method object was saved in the walk state */
  329. obj_desc = walk_state->method_desc;
  330. if (!obj_desc) {
  331. return_VOID;
  332. }
  333. /* Delete all arguments and locals */
  334. acpi_ds_method_data_delete_all(walk_state);
  335. /*
  336. * Lock the parser while we terminate this method.
  337. * If this is the last thread executing the method,
  338. * we have additional cleanup to perform
  339. */
  340. status = acpi_ut_acquire_mutex(ACPI_MTX_PARSER);
  341. if (ACPI_FAILURE(status)) {
  342. return_VOID;
  343. }
  344. /* Signal completion of the execution of this method if necessary */
  345. if (walk_state->method_desc->method.semaphore) {
  346. status =
  347. acpi_os_signal_semaphore(walk_state->method_desc->method.
  348. semaphore, 1);
  349. if (ACPI_FAILURE(status)) {
  350. ACPI_REPORT_ERROR(("Could not signal method semaphore\n"));
  351. /* Ignore error and continue cleanup */
  352. }
  353. }
  354. /*
  355. * There are no more threads executing this method. Perform
  356. * additional cleanup.
  357. *
  358. * The method Node is stored in the walk state
  359. */
  360. method_node = walk_state->method_node;
  361. /* Lock namespace for possible update */
  362. status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
  363. if (ACPI_FAILURE(status)) {
  364. goto exit;
  365. }
  366. /*
  367. * Delete any namespace entries created immediately underneath
  368. * the method
  369. */
  370. if (method_node->child) {
  371. acpi_ns_delete_namespace_subtree(method_node);
  372. }
  373. /*
  374. * Delete any namespace entries created anywhere else within
  375. * the namespace by the execution of this method
  376. */
  377. acpi_ns_delete_namespace_by_owner(walk_state->method_desc->method.
  378. owner_id);
  379. status = acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
  380. /* Are there any other threads currently executing this method? */
  381. if (walk_state->method_desc->method.thread_count) {
  382. /*
  383. * Additional threads. Do not release the owner_id in this case,
  384. * we immediately reuse it for the next thread executing this method
  385. */
  386. ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
  387. "*** Completed execution of one thread, %d threads remaining\n",
  388. walk_state->method_desc->method.
  389. thread_count));
  390. } else {
  391. /* This is the only executing thread for this method */
  392. /*
  393. * Support to dynamically change a method from not_serialized to
  394. * Serialized if it appears that the method is incorrectly written and
  395. * does not support multiple thread execution. The best example of this
  396. * is if such a method creates namespace objects and blocks. A second
  397. * thread will fail with an AE_ALREADY_EXISTS exception
  398. *
  399. * This code is here because we must wait until the last thread exits
  400. * before creating the synchronization semaphore.
  401. */
  402. if ((walk_state->method_desc->method.concurrency == 1) &&
  403. (!walk_state->method_desc->method.semaphore)) {
  404. status = acpi_os_create_semaphore(1, 1,
  405. &walk_state->
  406. method_desc->method.
  407. semaphore);
  408. }
  409. /* No more threads, we can free the owner_id */
  410. acpi_ut_release_owner_id(&walk_state->method_desc->method.
  411. owner_id);
  412. }
  413. exit:
  414. (void)acpi_ut_release_mutex(ACPI_MTX_PARSER);
  415. return_VOID;
  416. }
  417. #ifdef ACPI_INIT_PARSE_METHODS
  418. /*
  419. * Note 11/2005: Removed this code to parse all methods during table
  420. * load because it causes problems if there are any errors during the
  421. * parse. Also, it seems like overkill and we probably don't want to
  422. * abort a table load because of an issue with a single method.
  423. */
  424. /*******************************************************************************
  425. *
  426. * FUNCTION: acpi_ds_parse_method
  427. *
  428. * PARAMETERS: Node - Method node
  429. *
  430. * RETURN: Status
  431. *
  432. * DESCRIPTION: Parse the AML that is associated with the method.
  433. *
  434. * MUTEX: Assumes parser is locked
  435. *
  436. ******************************************************************************/
  437. acpi_status acpi_ds_parse_method(struct acpi_namespace_node *node)
  438. {
  439. acpi_status status;
  440. union acpi_operand_object *obj_desc;
  441. union acpi_parse_object *op;
  442. struct acpi_walk_state *walk_state;
  443. ACPI_FUNCTION_TRACE_PTR("ds_parse_method", node);
  444. /* Parameter Validation */
  445. if (!node) {
  446. return_ACPI_STATUS(AE_NULL_ENTRY);
  447. }
  448. ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
  449. "**** Parsing [%4.4s] **** named_obj=%p\n",
  450. acpi_ut_get_node_name(node), node));
  451. /* Extract the method object from the method Node */
  452. obj_desc = acpi_ns_get_attached_object(node);
  453. if (!obj_desc) {
  454. return_ACPI_STATUS(AE_NULL_OBJECT);
  455. }
  456. /* Create a mutex for the method if there is a concurrency limit */
  457. if ((obj_desc->method.concurrency != ACPI_INFINITE_CONCURRENCY) &&
  458. (!obj_desc->method.semaphore)) {
  459. status = acpi_os_create_semaphore(obj_desc->method.concurrency,
  460. obj_desc->method.concurrency,
  461. &obj_desc->method.semaphore);
  462. if (ACPI_FAILURE(status)) {
  463. return_ACPI_STATUS(status);
  464. }
  465. }
  466. /*
  467. * Allocate a new parser op to be the root of the parsed
  468. * method tree
  469. */
  470. op = acpi_ps_alloc_op(AML_METHOD_OP);
  471. if (!op) {
  472. return_ACPI_STATUS(AE_NO_MEMORY);
  473. }
  474. /* Init new op with the method name and pointer back to the Node */
  475. acpi_ps_set_name(op, node->name.integer);
  476. op->common.node = node;
  477. /*
  478. * Get a new owner_id for objects created by this method. Namespace
  479. * objects (such as Operation Regions) can be created during the
  480. * first pass parse.
  481. */
  482. status = acpi_ut_allocate_owner_id(&obj_desc->method.owner_id);
  483. if (ACPI_FAILURE(status)) {
  484. goto cleanup;
  485. }
  486. /* Create and initialize a new walk state */
  487. walk_state =
  488. acpi_ds_create_walk_state(obj_desc->method.owner_id, NULL, NULL,
  489. NULL);
  490. if (!walk_state) {
  491. status = AE_NO_MEMORY;
  492. goto cleanup2;
  493. }
  494. status = acpi_ds_init_aml_walk(walk_state, op, node,
  495. obj_desc->method.aml_start,
  496. obj_desc->method.aml_length, NULL, 1);
  497. if (ACPI_FAILURE(status)) {
  498. acpi_ds_delete_walk_state(walk_state);
  499. goto cleanup2;
  500. }
  501. /*
  502. * Parse the method, first pass
  503. *
  504. * The first pass load is where newly declared named objects are added into
  505. * the namespace. Actual evaluation of the named objects (what would be
  506. * called a "second pass") happens during the actual execution of the
  507. * method so that operands to the named objects can take on dynamic
  508. * run-time values.
  509. */
  510. status = acpi_ps_parse_aml(walk_state);
  511. if (ACPI_FAILURE(status)) {
  512. goto cleanup2;
  513. }
  514. ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
  515. "**** [%4.4s] Parsed **** named_obj=%p Op=%p\n",
  516. acpi_ut_get_node_name(node), node, op));
  517. /*
  518. * Delete the parse tree. We simply re-parse the method for every
  519. * execution since there isn't much overhead (compared to keeping lots
  520. * of parse trees around)
  521. */
  522. acpi_ns_delete_namespace_subtree(node);
  523. acpi_ns_delete_namespace_by_owner(obj_desc->method.owner_id);
  524. cleanup2:
  525. acpi_ut_release_owner_id(&obj_desc->method.owner_id);
  526. cleanup:
  527. acpi_ps_delete_parse_tree(op);
  528. return_ACPI_STATUS(status);
  529. }
  530. #endif