1 /******************************************************************************
3 * Module Name: dsmethod - Parser/Interpreter interface - control method parsing
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2005, R. Byron Moore
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 * of any contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
45 #include <acpi/acpi.h>
46 #include <acpi/acparser.h>
47 #include <acpi/amlcode.h>
48 #include <acpi/acdispat.h>
49 #include <acpi/acinterp.h>
50 #include <acpi/acnamesp.h>
53 #define _COMPONENT ACPI_DISPATCHER
54 ACPI_MODULE_NAME ("dsmethod")
57 /*******************************************************************************
59 * FUNCTION: acpi_ds_parse_method
61 * PARAMETERS: obj_handle - Method node
65 * DESCRIPTION: Call the parser and parse the AML that is associated with the
68 * MUTEX: Assumes parser is locked
70 ******************************************************************************/
73 acpi_ds_parse_method (
74 acpi_handle obj_handle)
77 union acpi_operand_object *obj_desc;
78 union acpi_parse_object *op;
79 struct acpi_namespace_node *node;
80 acpi_owner_id owner_id;
81 struct acpi_walk_state *walk_state;
84 ACPI_FUNCTION_TRACE_PTR ("ds_parse_method", obj_handle);
87 /* Parameter Validation */
90 return_ACPI_STATUS (AE_NULL_ENTRY);
93 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "**** Parsing [%4.4s] **** named_obj=%p\n",
94 acpi_ut_get_node_name (obj_handle), obj_handle));
96 /* Extract the method object from the method Node */
98 node = (struct acpi_namespace_node *) obj_handle;
99 obj_desc = acpi_ns_get_attached_object (node);
101 return_ACPI_STATUS (AE_NULL_OBJECT);
104 /* Create a mutex for the method if there is a concurrency limit */
106 if ((obj_desc->method.concurrency != ACPI_INFINITE_CONCURRENCY) &&
107 (!obj_desc->method.semaphore)) {
108 status = acpi_os_create_semaphore (obj_desc->method.concurrency,
109 obj_desc->method.concurrency,
110 &obj_desc->method.semaphore);
111 if (ACPI_FAILURE (status)) {
112 return_ACPI_STATUS (status);
117 * Allocate a new parser op to be the root of the parsed
120 op = acpi_ps_alloc_op (AML_METHOD_OP);
122 return_ACPI_STATUS (AE_NO_MEMORY);
125 /* Init new op with the method name and pointer back to the Node */
127 acpi_ps_set_name (op, node->name.integer);
128 op->common.node = node;
131 * Get a new owner_id for objects created by this method. Namespace
132 * objects (such as Operation Regions) can be created during the
135 owner_id = acpi_ut_allocate_owner_id (ACPI_OWNER_TYPE_METHOD);
136 obj_desc->method.owning_id = owner_id;
138 /* Create and initialize a new walk state */
140 walk_state = acpi_ds_create_walk_state (owner_id, NULL, NULL, NULL);
142 return_ACPI_STATUS (AE_NO_MEMORY);
145 status = acpi_ds_init_aml_walk (walk_state, op, node,
146 obj_desc->method.aml_start,
147 obj_desc->method.aml_length, NULL, 1);
148 if (ACPI_FAILURE (status)) {
149 acpi_ds_delete_walk_state (walk_state);
150 return_ACPI_STATUS (status);
154 * Parse the method, first pass
156 * The first pass load is where newly declared named objects are added into
157 * the namespace. Actual evaluation of the named objects (what would be
158 * called a "second pass") happens during the actual execution of the
159 * method so that operands to the named objects can take on dynamic
162 status = acpi_ps_parse_aml (walk_state);
163 if (ACPI_FAILURE (status)) {
164 return_ACPI_STATUS (status);
167 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE,
168 "**** [%4.4s] Parsed **** named_obj=%p Op=%p\n",
169 acpi_ut_get_node_name (obj_handle), obj_handle, op));
171 acpi_ps_delete_parse_tree (op);
172 return_ACPI_STATUS (status);
176 /*******************************************************************************
178 * FUNCTION: acpi_ds_begin_method_execution
180 * PARAMETERS: method_node - Node of the method
181 * obj_desc - The method object
182 * calling_method_node - Caller of this method (if non-null)
186 * DESCRIPTION: Prepare a method for execution. Parses the method if necessary,
187 * increments the thread count, and waits at the method semaphore
188 * for clearance to execute.
190 ******************************************************************************/
193 acpi_ds_begin_method_execution (
194 struct acpi_namespace_node *method_node,
195 union acpi_operand_object *obj_desc,
196 struct acpi_namespace_node *calling_method_node)
198 acpi_status status = AE_OK;
201 ACPI_FUNCTION_TRACE_PTR ("ds_begin_method_execution", method_node);
205 return_ACPI_STATUS (AE_NULL_ENTRY);
209 * If there is a concurrency limit on this method, we need to
210 * obtain a unit from the method semaphore.
212 if (obj_desc->method.semaphore) {
214 * Allow recursive method calls, up to the reentrancy/concurrency
215 * limit imposed by the SERIALIZED rule and the sync_level method
218 * The point of this code is to avoid permanently blocking a
219 * thread that is making recursive method calls.
221 if (method_node == calling_method_node) {
222 if (obj_desc->method.thread_count >= obj_desc->method.concurrency) {
223 return_ACPI_STATUS (AE_AML_METHOD_LIMIT);
228 * Get a unit from the method semaphore. This releases the
229 * interpreter if we block
231 status = acpi_ex_system_wait_semaphore (obj_desc->method.semaphore,
236 * Increment the method parse tree thread count since it has been
237 * reentered one more time (even if it is the same thread)
239 obj_desc->method.thread_count++;
240 return_ACPI_STATUS (status);
244 /*******************************************************************************
246 * FUNCTION: acpi_ds_call_control_method
248 * PARAMETERS: Thread - Info for this thread
249 * this_walk_state - Current walk state
250 * Op - Current Op to be walked
254 * DESCRIPTION: Transfer execution to a called control method
256 ******************************************************************************/
259 acpi_ds_call_control_method (
260 struct acpi_thread_state *thread,
261 struct acpi_walk_state *this_walk_state,
262 union acpi_parse_object *op)
265 struct acpi_namespace_node *method_node;
266 struct acpi_walk_state *next_walk_state;
267 union acpi_operand_object *obj_desc;
268 struct acpi_parameter_info info;
272 ACPI_FUNCTION_TRACE_PTR ("ds_call_control_method", this_walk_state);
274 ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH, "Execute method %p, currentstate=%p\n",
275 this_walk_state->prev_op, this_walk_state));
278 * Get the namespace entry for the control method we are about to call
280 method_node = this_walk_state->method_call_node;
282 return_ACPI_STATUS (AE_NULL_ENTRY);
285 obj_desc = acpi_ns_get_attached_object (method_node);
287 return_ACPI_STATUS (AE_NULL_OBJECT);
290 obj_desc->method.owning_id = acpi_ut_allocate_owner_id (ACPI_OWNER_TYPE_METHOD);
292 /* Init for new method, wait on concurrency semaphore */
294 status = acpi_ds_begin_method_execution (method_node, obj_desc,
295 this_walk_state->method_node);
296 if (ACPI_FAILURE (status)) {
297 return_ACPI_STATUS (status);
300 if (!(obj_desc->method.method_flags & AML_METHOD_INTERNAL_ONLY)) {
301 /* 1) Parse: Create a new walk state for the preempting walk */
303 next_walk_state = acpi_ds_create_walk_state (obj_desc->method.owning_id,
305 if (!next_walk_state) {
306 return_ACPI_STATUS (AE_NO_MEMORY);
309 /* Create and init a Root Node */
311 op = acpi_ps_create_scope_op ();
313 status = AE_NO_MEMORY;
317 status = acpi_ds_init_aml_walk (next_walk_state, op, method_node,
318 obj_desc->method.aml_start, obj_desc->method.aml_length,
320 if (ACPI_FAILURE (status)) {
321 acpi_ds_delete_walk_state (next_walk_state);
325 /* Begin AML parse */
327 status = acpi_ps_parse_aml (next_walk_state);
328 acpi_ps_delete_parse_tree (op);
331 /* 2) Execute: Create a new state for the preempting walk */
333 next_walk_state = acpi_ds_create_walk_state (obj_desc->method.owning_id,
334 NULL, obj_desc, thread);
335 if (!next_walk_state) {
336 status = AE_NO_MEMORY;
340 * The resolved arguments were put on the previous walk state's operand
341 * stack. Operands on the previous walk state stack always
343 * Null terminate the list of arguments
345 this_walk_state->operands [this_walk_state->num_operands] = NULL;
347 info.parameters = &this_walk_state->operands[0];
348 info.parameter_type = ACPI_PARAM_ARGS;
350 status = acpi_ds_init_aml_walk (next_walk_state, NULL, method_node,
351 obj_desc->method.aml_start, obj_desc->method.aml_length,
353 if (ACPI_FAILURE (status)) {
358 * Delete the operands on the previous walkstate operand stack
359 * (they were copied to new objects)
361 for (i = 0; i < obj_desc->method.param_count; i++) {
362 acpi_ut_remove_reference (this_walk_state->operands [i]);
363 this_walk_state->operands [i] = NULL;
366 /* Clear the operand stack */
368 this_walk_state->num_operands = 0;
370 ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH,
371 "Starting nested execution, newstate=%p\n", next_walk_state));
373 if (obj_desc->method.method_flags & AML_METHOD_INTERNAL_ONLY) {
374 status = obj_desc->method.implementation (next_walk_state);
375 return_ACPI_STATUS (status);
378 return_ACPI_STATUS (AE_OK);
381 /* On error, we must delete the new walk state */
384 if (next_walk_state && (next_walk_state->method_desc)) {
385 /* Decrement the thread count on the method parse tree */
387 next_walk_state->method_desc->method.thread_count--;
389 (void) acpi_ds_terminate_control_method (next_walk_state);
390 acpi_ds_delete_walk_state (next_walk_state);
391 return_ACPI_STATUS (status);
395 /*******************************************************************************
397 * FUNCTION: acpi_ds_restart_control_method
399 * PARAMETERS: walk_state - State for preempted method (caller)
400 * return_desc - Return value from the called method
404 * DESCRIPTION: Restart a method that was preempted by another (nested) method
405 * invocation. Handle the return value (if any) from the callee.
407 ******************************************************************************/
410 acpi_ds_restart_control_method (
411 struct acpi_walk_state *walk_state,
412 union acpi_operand_object *return_desc)
417 ACPI_FUNCTION_TRACE_PTR ("ds_restart_control_method", walk_state);
420 ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH,
421 "****Restart [%4.4s] Op %p return_value_from_callee %p\n",
422 (char *) &walk_state->method_node->name, walk_state->method_call_op,
425 ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH,
426 " return_from_this_method_used?=%X res_stack %p Walk %p\n",
427 walk_state->return_used,
428 walk_state->results, walk_state));
430 /* Did the called method return a value? */
433 /* Are we actually going to use the return value? */
435 if (walk_state->return_used) {
436 /* Save the return value from the previous method */
438 status = acpi_ds_result_push (return_desc, walk_state);
439 if (ACPI_FAILURE (status)) {
440 acpi_ut_remove_reference (return_desc);
441 return_ACPI_STATUS (status);
445 * Save as THIS method's return value in case it is returned
446 * immediately to yet another method
448 walk_state->return_desc = return_desc;
452 * The following code is the
453 * optional support for a so-called "implicit return". Some AML code
454 * assumes that the last value of the method is "implicitly" returned
455 * to the caller. Just save the last result as the return value.
456 * NOTE: this is optional because the ASL language does not actually
457 * support this behavior.
459 else if (!acpi_ds_do_implicit_return (return_desc, walk_state, FALSE)) {
461 * Delete the return value if it will not be used by the
464 acpi_ut_remove_reference (return_desc);
468 return_ACPI_STATUS (AE_OK);
472 /*******************************************************************************
474 * FUNCTION: acpi_ds_terminate_control_method
476 * PARAMETERS: walk_state - State of the method
480 * DESCRIPTION: Terminate a control method. Delete everything that the method
481 * created, delete all locals and arguments, and delete the parse
484 ******************************************************************************/
487 acpi_ds_terminate_control_method (
488 struct acpi_walk_state *walk_state)
490 union acpi_operand_object *obj_desc;
491 struct acpi_namespace_node *method_node;
495 ACPI_FUNCTION_TRACE_PTR ("ds_terminate_control_method", walk_state);
499 return (AE_BAD_PARAMETER);
502 /* The current method object was saved in the walk state */
504 obj_desc = walk_state->method_desc;
506 return_ACPI_STATUS (AE_OK);
509 /* Delete all arguments and locals */
511 acpi_ds_method_data_delete_all (walk_state);
514 * Lock the parser while we terminate this method.
515 * If this is the last thread executing the method,
516 * we have additional cleanup to perform
518 status = acpi_ut_acquire_mutex (ACPI_MTX_PARSER);
519 if (ACPI_FAILURE (status)) {
520 return_ACPI_STATUS (status);
523 /* Signal completion of the execution of this method if necessary */
525 if (walk_state->method_desc->method.semaphore) {
526 status = acpi_os_signal_semaphore (
527 walk_state->method_desc->method.semaphore, 1);
528 if (ACPI_FAILURE (status)) {
529 ACPI_REPORT_ERROR (("Could not signal method semaphore\n"));
532 /* Ignore error and continue cleanup */
536 if (walk_state->method_desc->method.thread_count) {
537 ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH,
538 "*** Not deleting method namespace, there are still %d threads\n",
539 walk_state->method_desc->method.thread_count));
542 if (!walk_state->method_desc->method.thread_count) {
544 * Support to dynamically change a method from not_serialized to
545 * Serialized if it appears that the method is written foolishly and
546 * does not support multiple thread execution. The best example of this
547 * is if such a method creates namespace objects and blocks. A second
548 * thread will fail with an AE_ALREADY_EXISTS exception
550 * This code is here because we must wait until the last thread exits
551 * before creating the synchronization semaphore.
553 if ((walk_state->method_desc->method.concurrency == 1) &&
554 (!walk_state->method_desc->method.semaphore)) {
555 status = acpi_os_create_semaphore (1,
557 &walk_state->method_desc->method.semaphore);
561 * There are no more threads executing this method. Perform
562 * additional cleanup.
564 * The method Node is stored in the walk state
566 method_node = walk_state->method_node;
569 * Delete any namespace entries created immediately underneath
572 status = acpi_ut_acquire_mutex (ACPI_MTX_NAMESPACE);
573 if (ACPI_FAILURE (status)) {
574 return_ACPI_STATUS (status);
577 if (method_node->child) {
578 acpi_ns_delete_namespace_subtree (method_node);
582 * Delete any namespace entries created anywhere else within
585 acpi_ns_delete_namespace_by_owner (walk_state->method_desc->method.owning_id);
586 status = acpi_ut_release_mutex (ACPI_MTX_NAMESPACE);
587 if (ACPI_FAILURE (status)) {
588 return_ACPI_STATUS (status);
592 status = acpi_ut_release_mutex (ACPI_MTX_PARSER);
593 return_ACPI_STATUS (status);