dsmethod.c 18 KB

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