1/******************************************************************************
2 *
3 * Module Name: psparse - Parser top level AML parse routines
4 *
5 *****************************************************************************/
6
7/*
8 * Copyright (C) 2000 - 2007, R. Byron Moore
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
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.
25 *
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.
29 *
30 * NO WARRANTY
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.
42 */
43
44/*
45 * Parse the AML and build an operation tree as most interpreters,
46 * like Perl, do.  Parsing is done by hand rather than with a YACC
47 * generated parser to tightly constrain stack and dynamic memory
48 * usage.  At the same time, parsing is kept flexible and the code
49 * fairly compact by parsing based on a list of AML opcode
50 * templates in aml_op_info[]
51 */
52
53#include <acpi/acpi.h>
54#include <acpi/acparser.h>
55#include <acpi/acdispat.h>
56#include <acpi/amlcode.h>
57#include <acpi/acnamesp.h>
58#include <acpi/acinterp.h>
59
60#define _COMPONENT          ACPI_PARSER
61ACPI_MODULE_NAME("psparse")
62
63/*******************************************************************************
64 *
65 * FUNCTION:    acpi_ps_get_opcode_size
66 *
67 * PARAMETERS:  Opcode          - An AML opcode
68 *
69 * RETURN:      Size of the opcode, in bytes (1 or 2)
70 *
71 * DESCRIPTION: Get the size of the current opcode.
72 *
73 ******************************************************************************/
74u32 acpi_ps_get_opcode_size(u32 opcode)
75{
76
77	/* Extended (2-byte) opcode if > 255 */
78
79	if (opcode > 0x00FF) {
80		return (2);
81	}
82
83	/* Otherwise, just a single byte opcode */
84
85	return (1);
86}
87
88/*******************************************************************************
89 *
90 * FUNCTION:    acpi_ps_peek_opcode
91 *
92 * PARAMETERS:  parser_state        - A parser state object
93 *
94 * RETURN:      Next AML opcode
95 *
96 * DESCRIPTION: Get next AML opcode (without incrementing AML pointer)
97 *
98 ******************************************************************************/
99
100u16 acpi_ps_peek_opcode(struct acpi_parse_state * parser_state)
101{
102	u8 *aml;
103	u16 opcode;
104
105	aml = parser_state->aml;
106	opcode = (u16) ACPI_GET8(aml);
107
108	if (opcode == AML_EXTENDED_OP_PREFIX) {
109
110		/* Extended opcode, get the second opcode byte */
111
112		aml++;
113		opcode = (u16) ((opcode << 8) | ACPI_GET8(aml));
114	}
115
116	return (opcode);
117}
118
119/*******************************************************************************
120 *
121 * FUNCTION:    acpi_ps_complete_this_op
122 *
123 * PARAMETERS:  walk_state      - Current State
124 *              Op              - Op to complete
125 *
126 * RETURN:      Status
127 *
128 * DESCRIPTION: Perform any cleanup at the completion of an Op.
129 *
130 ******************************************************************************/
131
132acpi_status
133acpi_ps_complete_this_op(struct acpi_walk_state * walk_state,
134			 union acpi_parse_object * op)
135{
136	union acpi_parse_object *prev;
137	union acpi_parse_object *next;
138	const struct acpi_opcode_info *parent_info;
139	union acpi_parse_object *replacement_op = NULL;
140
141	ACPI_FUNCTION_TRACE_PTR(ps_complete_this_op, op);
142
143	/* Check for null Op, can happen if AML code is corrupt */
144
145	if (!op) {
146		return_ACPI_STATUS(AE_OK);	/* OK for now */
147	}
148
149	/* Delete this op and the subtree below it if asked to */
150
151	if (((walk_state->parse_flags & ACPI_PARSE_TREE_MASK) !=
152	     ACPI_PARSE_DELETE_TREE)
153	    || (walk_state->op_info->class == AML_CLASS_ARGUMENT)) {
154		return_ACPI_STATUS(AE_OK);
155	}
156
157	/* Make sure that we only delete this subtree */
158
159	if (op->common.parent) {
160		prev = op->common.parent->common.value.arg;
161		if (!prev) {
162
163			/* Nothing more to do */
164
165			goto cleanup;
166		}
167
168		/*
169		 * Check if we need to replace the operator and its subtree
170		 * with a return value op (placeholder op)
171		 */
172		parent_info =
173		    acpi_ps_get_opcode_info(op->common.parent->common.
174					    aml_opcode);
175
176		switch (parent_info->class) {
177		case AML_CLASS_CONTROL:
178			break;
179
180		case AML_CLASS_CREATE:
181
182			/*
183			 * These opcodes contain term_arg operands. The current
184			 * op must be replaced by a placeholder return op
185			 */
186			replacement_op =
187			    acpi_ps_alloc_op(AML_INT_RETURN_VALUE_OP);
188			if (!replacement_op) {
189				goto allocate_error;
190			}
191			break;
192
193		case AML_CLASS_NAMED_OBJECT:
194
195			/*
196			 * These opcodes contain term_arg operands. The current
197			 * op must be replaced by a placeholder return op
198			 */
199			if ((op->common.parent->common.aml_opcode ==
200			     AML_REGION_OP)
201			    || (op->common.parent->common.aml_opcode ==
202				AML_DATA_REGION_OP)
203			    || (op->common.parent->common.aml_opcode ==
204				AML_BUFFER_OP)
205			    || (op->common.parent->common.aml_opcode ==
206				AML_PACKAGE_OP)
207			    || (op->common.parent->common.aml_opcode ==
208				AML_VAR_PACKAGE_OP)) {
209				replacement_op =
210				    acpi_ps_alloc_op(AML_INT_RETURN_VALUE_OP);
211				if (!replacement_op) {
212					goto allocate_error;
213				}
214			} else
215			    if ((op->common.parent->common.aml_opcode ==
216				 AML_NAME_OP)
217				&& (walk_state->pass_number <=
218				    ACPI_IMODE_LOAD_PASS2)) {
219				if ((op->common.aml_opcode == AML_BUFFER_OP)
220				    || (op->common.aml_opcode == AML_PACKAGE_OP)
221				    || (op->common.aml_opcode ==
222					AML_VAR_PACKAGE_OP)) {
223					replacement_op =
224					    acpi_ps_alloc_op(op->common.
225							     aml_opcode);
226					if (!replacement_op) {
227						goto allocate_error;
228					}
229
230					replacement_op->named.data =
231					    op->named.data;
232					replacement_op->named.length =
233					    op->named.length;
234				}
235			}
236			break;
237
238		default:
239
240			replacement_op =
241			    acpi_ps_alloc_op(AML_INT_RETURN_VALUE_OP);
242			if (!replacement_op) {
243				goto allocate_error;
244			}
245		}
246
247		/* We must unlink this op from the parent tree */
248
249		if (prev == op) {
250
251			/* This op is the first in the list */
252
253			if (replacement_op) {
254				replacement_op->common.parent =
255				    op->common.parent;
256				replacement_op->common.value.arg = NULL;
257				replacement_op->common.node = op->common.node;
258				op->common.parent->common.value.arg =
259				    replacement_op;
260				replacement_op->common.next = op->common.next;
261			} else {
262				op->common.parent->common.value.arg =
263				    op->common.next;
264			}
265		}
266
267		/* Search the parent list */
268
269		else
270			while (prev) {
271
272				/* Traverse all siblings in the parent's argument list */
273
274				next = prev->common.next;
275				if (next == op) {
276					if (replacement_op) {
277						replacement_op->common.parent =
278						    op->common.parent;
279						replacement_op->common.value.
280						    arg = NULL;
281						replacement_op->common.node =
282						    op->common.node;
283						prev->common.next =
284						    replacement_op;
285						replacement_op->common.next =
286						    op->common.next;
287						next = NULL;
288					} else {
289						prev->common.next =
290						    op->common.next;
291						next = NULL;
292					}
293				}
294				prev = next;
295			}
296	}
297
298      cleanup:
299
300	/* Now we can actually delete the subtree rooted at Op */
301
302	acpi_ps_delete_parse_tree(op);
303	return_ACPI_STATUS(AE_OK);
304
305      allocate_error:
306
307	/* Always delete the subtree, even on error */
308
309	acpi_ps_delete_parse_tree(op);
310	return_ACPI_STATUS(AE_NO_MEMORY);
311}
312
313/*******************************************************************************
314 *
315 * FUNCTION:    acpi_ps_next_parse_state
316 *
317 * PARAMETERS:  walk_state          - Current state
318 *              Op                  - Current parse op
319 *              callback_status     - Status from previous operation
320 *
321 * RETURN:      Status
322 *
323 * DESCRIPTION: Update the parser state based upon the return exception from
324 *              the parser callback.
325 *
326 ******************************************************************************/
327
328acpi_status
329acpi_ps_next_parse_state(struct acpi_walk_state *walk_state,
330			 union acpi_parse_object *op,
331			 acpi_status callback_status)
332{
333	struct acpi_parse_state *parser_state = &walk_state->parser_state;
334	acpi_status status = AE_CTRL_PENDING;
335
336	ACPI_FUNCTION_TRACE_PTR(ps_next_parse_state, op);
337
338	switch (callback_status) {
339	case AE_CTRL_TERMINATE:
340		/*
341		 * A control method was terminated via a RETURN statement.
342		 * The walk of this method is complete.
343		 */
344		parser_state->aml = parser_state->aml_end;
345		status = AE_CTRL_TERMINATE;
346		break;
347
348	case AE_CTRL_BREAK:
349
350		parser_state->aml = walk_state->aml_last_while;
351		walk_state->control_state->common.value = FALSE;
352		status = acpi_ds_result_stack_pop(walk_state);
353		if (ACPI_SUCCESS(status)) {
354			status = AE_CTRL_BREAK;
355		}
356		break;
357
358	case AE_CTRL_CONTINUE:
359
360		parser_state->aml = walk_state->aml_last_while;
361		status = acpi_ds_result_stack_pop(walk_state);
362		if (ACPI_SUCCESS(status)) {
363			status = AE_CTRL_CONTINUE;
364		}
365		break;
366
367	case AE_CTRL_PENDING:
368
369		parser_state->aml = walk_state->aml_last_while;
370		break;
371
372
373	case AE_CTRL_TRUE:
374		/*
375		 * Predicate of an IF was true, and we are at the matching ELSE.
376		 * Just close out this package
377		 */
378		parser_state->aml = acpi_ps_get_next_package_end(parser_state);
379		status = acpi_ds_result_stack_pop(walk_state);
380		if (ACPI_SUCCESS(status)) {
381			status = AE_CTRL_PENDING;
382		}
383		break;
384
385	case AE_CTRL_FALSE:
386		/*
387		 * Either an IF/WHILE Predicate was false or we encountered a BREAK
388		 * opcode.  In both cases, we do not execute the rest of the
389		 * package;  We simply close out the parent (finishing the walk of
390		 * this branch of the tree) and continue execution at the parent
391		 * level.
392		 */
393		parser_state->aml = parser_state->scope->parse_scope.pkg_end;
394
395		/* In the case of a BREAK, just force a predicate (if any) to FALSE */
396
397		walk_state->control_state->common.value = FALSE;
398		status = AE_CTRL_END;
399		break;
400
401	case AE_CTRL_TRANSFER:
402
403		/* A method call (invocation) -- transfer control */
404
405		status = AE_CTRL_TRANSFER;
406		walk_state->prev_op = op;
407		walk_state->method_call_op = op;
408		walk_state->method_call_node =
409		    (op->common.value.arg)->common.node;
410
411		/* Will return value (if any) be used by the caller? */
412
413		walk_state->return_used =
414		    acpi_ds_is_result_used(op, walk_state);
415		break;
416
417	default:
418
419		status = callback_status;
420		if ((callback_status & AE_CODE_MASK) == AE_CODE_CONTROL) {
421			status = AE_OK;
422		}
423		break;
424	}
425
426	return_ACPI_STATUS(status);
427}
428
429/*******************************************************************************
430 *
431 * FUNCTION:    acpi_ps_parse_aml
432 *
433 * PARAMETERS:  walk_state      - Current state
434 *
435 *
436 * RETURN:      Status
437 *
438 * DESCRIPTION: Parse raw AML and return a tree of ops
439 *
440 ******************************************************************************/
441
442acpi_status acpi_ps_parse_aml(struct acpi_walk_state *walk_state)
443{
444	acpi_status status;
445	struct acpi_thread_state *thread;
446	struct acpi_thread_state *prev_walk_list = acpi_gbl_current_walk_list;
447	struct acpi_walk_state *previous_walk_state;
448
449	ACPI_FUNCTION_TRACE(ps_parse_aml);
450
451	ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
452			  "Entered with WalkState=%p Aml=%p size=%X\n",
453			  walk_state, walk_state->parser_state.aml,
454			  walk_state->parser_state.aml_size));
455
456	/* Create and initialize a new thread state */
457
458	thread = acpi_ut_create_thread_state();
459	if (!thread) {
460		acpi_ds_delete_walk_state(walk_state);
461		return_ACPI_STATUS(AE_NO_MEMORY);
462	}
463
464	walk_state->thread = thread;
465
466	/*
467	 * If executing a method, the starting sync_level is this method's
468	 * sync_level
469	 */
470	if (walk_state->method_desc) {
471		walk_state->thread->current_sync_level =
472		    walk_state->method_desc->method.sync_level;
473	}
474
475	acpi_ds_push_walk_state(walk_state, thread);
476
477	/*
478	 * This global allows the AML debugger to get a handle to the currently
479	 * executing control method.
480	 */
481	acpi_gbl_current_walk_list = thread;
482
483	/*
484	 * Execute the walk loop as long as there is a valid Walk State.  This
485	 * handles nested control method invocations without recursion.
486	 */
487	ACPI_DEBUG_PRINT((ACPI_DB_PARSE, "State=%p\n", walk_state));
488
489	status = AE_OK;
490	while (walk_state) {
491		if (ACPI_SUCCESS(status)) {
492			/*
493			 * The parse_loop executes AML until the method terminates
494			 * or calls another method.
495			 */
496			status = acpi_ps_parse_loop(walk_state);
497		}
498
499		ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
500				  "Completed one call to walk loop, %s State=%p\n",
501				  acpi_format_exception(status), walk_state));
502
503		if (status == AE_CTRL_TRANSFER) {
504			/*
505			 * A method call was detected.
506			 * Transfer control to the called control method
507			 */
508			status =
509			    acpi_ds_call_control_method(thread, walk_state,
510							NULL);
511			if (ACPI_FAILURE(status)) {
512				status =
513				    acpi_ds_method_error(status, walk_state);
514			}
515
516			/*
517			 * If the transfer to the new method method call worked, a new walk
518			 * state was created -- get it
519			 */
520			walk_state = acpi_ds_get_current_walk_state(thread);
521			continue;
522		} else if (status == AE_CTRL_TERMINATE) {
523			status = AE_OK;
524		} else if ((status != AE_OK) && (walk_state->method_desc)) {
525
526			/* Either the method parse or actual execution failed */
527
528			ACPI_ERROR_METHOD("Method parse/execution failed",
529					  walk_state->method_node, NULL,
530					  status);
531
532			/* Check for possible multi-thread reentrancy problem */
533
534			if ((status == AE_ALREADY_EXISTS) &&
535			    (!walk_state->method_desc->method.mutex)) {
536				ACPI_INFO((AE_INFO,
537					   "Marking method %4.4s as Serialized",
538					   walk_state->method_node->name.
539					   ascii));
540
541				walk_state->method_desc->method.method_flags |=
542				    AML_METHOD_SERIALIZED;
543				walk_state->method_desc->method.sync_level = 0;
544			}
545		}
546
547		/* We are done with this walk, move on to the parent if any */
548
549		walk_state = acpi_ds_pop_walk_state(thread);
550
551		/* Reset the current scope to the beginning of scope stack */
552
553		acpi_ds_scope_stack_clear(walk_state);
554
555		/*
556		 * If we just returned from the execution of a control method or if we
557		 * encountered an error during the method parse phase, there's lots of
558		 * cleanup to do
559		 */
560		if (((walk_state->parse_flags & ACPI_PARSE_MODE_MASK) ==
561		     ACPI_PARSE_EXECUTE) || (ACPI_FAILURE(status))) {
562			acpi_ds_terminate_control_method(walk_state->
563							 method_desc,
564							 walk_state);
565		}
566
567		/* Delete this walk state and all linked control states */
568
569		acpi_ps_cleanup_scope(&walk_state->parser_state);
570		previous_walk_state = walk_state;
571
572		ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
573				  "ReturnValue=%p, ImplicitValue=%p State=%p\n",
574				  walk_state->return_desc,
575				  walk_state->implicit_return_obj, walk_state));
576
577		/* Check if we have restarted a preempted walk */
578
579		walk_state = acpi_ds_get_current_walk_state(thread);
580		if (walk_state) {
581			if (ACPI_SUCCESS(status)) {
582				/*
583				 * There is another walk state, restart it.
584				 * If the method return value is not used by the parent,
585				 * The object is deleted
586				 */
587				if (!previous_walk_state->return_desc) {
588					status =
589					    acpi_ds_restart_control_method
590					    (walk_state,
591					     previous_walk_state->
592					     implicit_return_obj);
593				} else {
594					/*
595					 * We have a valid return value, delete any implicit
596					 * return value.
597					 */
598					acpi_ds_clear_implicit_return
599					    (previous_walk_state);
600
601					status =
602					    acpi_ds_restart_control_method
603					    (walk_state,
604					     previous_walk_state->return_desc);
605				}
606				if (ACPI_SUCCESS(status)) {
607					walk_state->walk_type |=
608					    ACPI_WALK_METHOD_RESTART;
609				}
610			} else {
611				/* On error, delete any return object */
612
613				acpi_ut_remove_reference(previous_walk_state->
614							 return_desc);
615			}
616		}
617
618		/*
619		 * Just completed a 1st-level method, save the final internal return
620		 * value (if any)
621		 */
622		else if (previous_walk_state->caller_return_desc) {
623			if (previous_walk_state->implicit_return_obj) {
624				*(previous_walk_state->caller_return_desc) =
625				    previous_walk_state->implicit_return_obj;
626			} else {
627				/* NULL if no return value */
628
629				*(previous_walk_state->caller_return_desc) =
630				    previous_walk_state->return_desc;
631			}
632		} else {
633			if (previous_walk_state->return_desc) {
634
635				/* Caller doesn't want it, must delete it */
636
637				acpi_ut_remove_reference(previous_walk_state->
638							 return_desc);
639			}
640			if (previous_walk_state->implicit_return_obj) {
641
642				/* Caller doesn't want it, must delete it */
643
644				acpi_ut_remove_reference(previous_walk_state->
645							 implicit_return_obj);
646			}
647		}
648
649		acpi_ds_delete_walk_state(previous_walk_state);
650	}
651
652	/* Normal exit */
653
654	acpi_ex_release_all_mutexes(thread);
655	acpi_ut_delete_generic_state(ACPI_CAST_PTR
656				     (union acpi_generic_state, thread));
657	acpi_gbl_current_walk_list = prev_walk_list;
658	return_ACPI_STATUS(status);
659}
660