1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2 /******************************************************************************
3 *
4 * Module Name: dswload - Dispatcher first pass namespace load callbacks
5 *
6 * Copyright (C) 2000 - 2018, Intel Corp.
7 *
8 *****************************************************************************/
9
10 #include <acpi/acpi.h>
11 #include "accommon.h"
12 #include "acparser.h"
13 #include "amlcode.h"
14 #include "acdispat.h"
15 #include "acinterp.h"
16 #include "acnamesp.h"
17
18 #ifdef ACPI_ASL_COMPILER
19 #include "acdisasm.h"
20 #endif
21
22 #define _COMPONENT ACPI_DISPATCHER
23 ACPI_MODULE_NAME("dswload")
24
25 /*******************************************************************************
26 *
27 * FUNCTION: acpi_ds_init_callbacks
28 *
29 * PARAMETERS: walk_state - Current state of the parse tree walk
30 * pass_number - 1, 2, or 3
31 *
32 * RETURN: Status
33 *
34 * DESCRIPTION: Init walk state callbacks
35 *
36 ******************************************************************************/
37 acpi_status
acpi_ds_init_callbacks(struct acpi_walk_state * walk_state,u32 pass_number)38 acpi_ds_init_callbacks(struct acpi_walk_state *walk_state, u32 pass_number)
39 {
40
41 switch (pass_number) {
42 case 0:
43
44 /* Parse only - caller will setup callbacks */
45
46 walk_state->parse_flags = ACPI_PARSE_LOAD_PASS1 |
47 ACPI_PARSE_DELETE_TREE | ACPI_PARSE_DISASSEMBLE;
48 walk_state->descending_callback = NULL;
49 walk_state->ascending_callback = NULL;
50 break;
51
52 case 1:
53
54 /* Load pass 1 */
55
56 walk_state->parse_flags = ACPI_PARSE_LOAD_PASS1 |
57 ACPI_PARSE_DELETE_TREE;
58 walk_state->descending_callback = acpi_ds_load1_begin_op;
59 walk_state->ascending_callback = acpi_ds_load1_end_op;
60 break;
61
62 case 2:
63
64 /* Load pass 2 */
65
66 walk_state->parse_flags = ACPI_PARSE_LOAD_PASS1 |
67 ACPI_PARSE_DELETE_TREE;
68 walk_state->descending_callback = acpi_ds_load2_begin_op;
69 walk_state->ascending_callback = acpi_ds_load2_end_op;
70 break;
71
72 case 3:
73
74 /* Execution pass */
75
76 #ifndef ACPI_NO_METHOD_EXECUTION
77 walk_state->parse_flags |= ACPI_PARSE_EXECUTE |
78 ACPI_PARSE_DELETE_TREE;
79 walk_state->descending_callback = acpi_ds_exec_begin_op;
80 walk_state->ascending_callback = acpi_ds_exec_end_op;
81 #endif
82 break;
83
84 default:
85
86 return (AE_BAD_PARAMETER);
87 }
88
89 return (AE_OK);
90 }
91
92 /*******************************************************************************
93 *
94 * FUNCTION: acpi_ds_load1_begin_op
95 *
96 * PARAMETERS: walk_state - Current state of the parse tree walk
97 * out_op - Where to return op if a new one is created
98 *
99 * RETURN: Status
100 *
101 * DESCRIPTION: Descending callback used during the loading of ACPI tables.
102 *
103 ******************************************************************************/
104
105 acpi_status
acpi_ds_load1_begin_op(struct acpi_walk_state * walk_state,union acpi_parse_object ** out_op)106 acpi_ds_load1_begin_op(struct acpi_walk_state *walk_state,
107 union acpi_parse_object **out_op)
108 {
109 union acpi_parse_object *op;
110 struct acpi_namespace_node *node;
111 acpi_status status;
112 acpi_object_type object_type;
113 char *path;
114 u32 flags;
115
116 ACPI_FUNCTION_TRACE_PTR(ds_load1_begin_op, walk_state->op);
117
118 op = walk_state->op;
119 ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH, "Op=%p State=%p\n", op,
120 walk_state));
121
122 /* We are only interested in opcodes that have an associated name */
123
124 if (op) {
125 if (!(walk_state->op_info->flags & AML_NAMED)) {
126 *out_op = op;
127 return_ACPI_STATUS(AE_OK);
128 }
129
130 /* Check if this object has already been installed in the namespace */
131
132 if (op->common.node) {
133 *out_op = op;
134 return_ACPI_STATUS(AE_OK);
135 }
136 }
137
138 path = acpi_ps_get_next_namestring(&walk_state->parser_state);
139
140 /* Map the raw opcode into an internal object type */
141
142 object_type = walk_state->op_info->object_type;
143
144 ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
145 "State=%p Op=%p [%s]\n", walk_state, op,
146 acpi_ut_get_type_name(object_type)));
147
148 switch (walk_state->opcode) {
149 case AML_SCOPE_OP:
150 /*
151 * The target name of the Scope() operator must exist at this point so
152 * that we can actually open the scope to enter new names underneath it.
153 * Allow search-to-root for single namesegs.
154 */
155 status =
156 acpi_ns_lookup(walk_state->scope_info, path, object_type,
157 ACPI_IMODE_EXECUTE, ACPI_NS_SEARCH_PARENT,
158 walk_state, &(node));
159 #ifdef ACPI_ASL_COMPILER
160 if (status == AE_NOT_FOUND) {
161 /*
162 * Table disassembly:
163 * Target of Scope() not found. Generate an External for it, and
164 * insert the name into the namespace.
165 */
166 acpi_dm_add_op_to_external_list(op, path,
167 ACPI_TYPE_DEVICE, 0, 0);
168 status =
169 acpi_ns_lookup(walk_state->scope_info, path,
170 object_type, ACPI_IMODE_LOAD_PASS1,
171 ACPI_NS_SEARCH_PARENT, walk_state,
172 &node);
173 }
174 #endif
175 if (ACPI_FAILURE(status)) {
176 ACPI_ERROR_NAMESPACE(walk_state->scope_info, path,
177 status);
178 return_ACPI_STATUS(status);
179 }
180
181 /*
182 * Check to make sure that the target is
183 * one of the opcodes that actually opens a scope
184 */
185 switch (node->type) {
186 case ACPI_TYPE_ANY:
187 case ACPI_TYPE_LOCAL_SCOPE: /* Scope */
188 case ACPI_TYPE_DEVICE:
189 case ACPI_TYPE_POWER:
190 case ACPI_TYPE_PROCESSOR:
191 case ACPI_TYPE_THERMAL:
192
193 /* These are acceptable types */
194 break;
195
196 case ACPI_TYPE_INTEGER:
197 case ACPI_TYPE_STRING:
198 case ACPI_TYPE_BUFFER:
199 /*
200 * These types we will allow, but we will change the type.
201 * This enables some existing code of the form:
202 *
203 * Name (DEB, 0)
204 * Scope (DEB) { ... }
205 *
206 * Note: silently change the type here. On the second pass,
207 * we will report a warning
208 */
209 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
210 "Type override - [%4.4s] had invalid type (%s) "
211 "for Scope operator, changed to type ANY\n",
212 acpi_ut_get_node_name(node),
213 acpi_ut_get_type_name(node->type)));
214
215 node->type = ACPI_TYPE_ANY;
216 walk_state->scope_info->common.value = ACPI_TYPE_ANY;
217 break;
218
219 case ACPI_TYPE_METHOD:
220 /*
221 * Allow scope change to root during execution of module-level
222 * code. Root is typed METHOD during this time.
223 */
224 if ((node == acpi_gbl_root_node) &&
225 (walk_state->
226 parse_flags & ACPI_PARSE_MODULE_LEVEL)) {
227 break;
228 }
229
230 /*lint -fallthrough */
231
232 default:
233
234 /* All other types are an error */
235
236 ACPI_ERROR((AE_INFO,
237 "Invalid type (%s) for target of "
238 "Scope operator [%4.4s] (Cannot override)",
239 acpi_ut_get_type_name(node->type),
240 acpi_ut_get_node_name(node)));
241
242 return_ACPI_STATUS(AE_AML_OPERAND_TYPE);
243 }
244 break;
245
246 default:
247 /*
248 * For all other named opcodes, we will enter the name into
249 * the namespace.
250 *
251 * Setup the search flags.
252 * Since we are entering a name into the namespace, we do not want to
253 * enable the search-to-root upsearch.
254 *
255 * There are only two conditions where it is acceptable that the name
256 * already exists:
257 * 1) the Scope() operator can reopen a scoping object that was
258 * previously defined (Scope, Method, Device, etc.)
259 * 2) Whenever we are parsing a deferred opcode (op_region, Buffer,
260 * buffer_field, or Package), the name of the object is already
261 * in the namespace.
262 */
263 if (walk_state->deferred_node) {
264
265 /* This name is already in the namespace, get the node */
266
267 node = walk_state->deferred_node;
268 status = AE_OK;
269 break;
270 }
271
272 /*
273 * If we are executing a method, do not create any namespace objects
274 * during the load phase, only during execution.
275 */
276 if (walk_state->method_node) {
277 node = NULL;
278 status = AE_OK;
279 break;
280 }
281
282 flags = ACPI_NS_NO_UPSEARCH;
283 if ((walk_state->opcode != AML_SCOPE_OP) &&
284 (!(walk_state->parse_flags & ACPI_PARSE_DEFERRED_OP))) {
285 if (walk_state->namespace_override) {
286 flags |= ACPI_NS_OVERRIDE_IF_FOUND;
287 ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
288 "[%s] Override allowed\n",
289 acpi_ut_get_type_name
290 (object_type)));
291 } else {
292 flags |= ACPI_NS_ERROR_IF_FOUND;
293 ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
294 "[%s] Cannot already exist\n",
295 acpi_ut_get_type_name
296 (object_type)));
297 }
298 } else {
299 ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
300 "[%s] Both Find or Create allowed\n",
301 acpi_ut_get_type_name(object_type)));
302 }
303
304 /*
305 * Enter the named type into the internal namespace. We enter the name
306 * as we go downward in the parse tree. Any necessary subobjects that
307 * involve arguments to the opcode must be created as we go back up the
308 * parse tree later.
309 */
310 status =
311 acpi_ns_lookup(walk_state->scope_info, path, object_type,
312 ACPI_IMODE_LOAD_PASS1, flags, walk_state,
313 &node);
314 if (ACPI_FAILURE(status)) {
315 if (status == AE_ALREADY_EXISTS) {
316
317 /* The name already exists in this scope */
318
319 if (node->flags & ANOBJ_IS_EXTERNAL) {
320 /*
321 * Allow one create on an object or segment that was
322 * previously declared External
323 */
324 node->flags &= ~ANOBJ_IS_EXTERNAL;
325 node->type = (u8) object_type;
326
327 /* Just retyped a node, probably will need to open a scope */
328
329 if (acpi_ns_opens_scope(object_type)) {
330 status =
331 acpi_ds_scope_stack_push
332 (node, object_type,
333 walk_state);
334 if (ACPI_FAILURE(status)) {
335 return_ACPI_STATUS
336 (status);
337 }
338 }
339
340 status = AE_OK;
341 }
342 }
343
344 if (ACPI_FAILURE(status)) {
345 ACPI_ERROR_NAMESPACE(walk_state->scope_info,
346 path, status);
347 return_ACPI_STATUS(status);
348 }
349 }
350 break;
351 }
352
353 /* Common exit */
354
355 if (!op) {
356
357 /* Create a new op */
358
359 op = acpi_ps_alloc_op(walk_state->opcode, walk_state->aml);
360 if (!op) {
361 return_ACPI_STATUS(AE_NO_MEMORY);
362 }
363 }
364
365 /* Initialize the op */
366
367 #if (defined (ACPI_NO_METHOD_EXECUTION) || defined (ACPI_CONSTANT_EVAL_ONLY))
368 op->named.path = path;
369 #endif
370
371 if (node) {
372 /*
373 * Put the Node in the "op" object that the parser uses, so we
374 * can get it again quickly when this scope is closed
375 */
376 op->common.node = node;
377 op->named.name = node->name.integer;
378 }
379
380 acpi_ps_append_arg(acpi_ps_get_parent_scope(&walk_state->parser_state),
381 op);
382 *out_op = op;
383 return_ACPI_STATUS(status);
384 }
385
386 /*******************************************************************************
387 *
388 * FUNCTION: acpi_ds_load1_end_op
389 *
390 * PARAMETERS: walk_state - Current state of the parse tree walk
391 *
392 * RETURN: Status
393 *
394 * DESCRIPTION: Ascending callback used during the loading of the namespace,
395 * both control methods and everything else.
396 *
397 ******************************************************************************/
398
acpi_ds_load1_end_op(struct acpi_walk_state * walk_state)399 acpi_status acpi_ds_load1_end_op(struct acpi_walk_state *walk_state)
400 {
401 union acpi_parse_object *op;
402 acpi_object_type object_type;
403 acpi_status status = AE_OK;
404
405 #ifdef ACPI_ASL_COMPILER
406 u8 param_count;
407 #endif
408
409 ACPI_FUNCTION_TRACE(ds_load1_end_op);
410
411 op = walk_state->op;
412 ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH, "Op=%p State=%p\n", op,
413 walk_state));
414
415 /* We are only interested in opcodes that have an associated name */
416
417 if (!(walk_state->op_info->flags & (AML_NAMED | AML_FIELD))) {
418 return_ACPI_STATUS(AE_OK);
419 }
420
421 /* Get the object type to determine if we should pop the scope */
422
423 object_type = walk_state->op_info->object_type;
424
425 #ifndef ACPI_NO_METHOD_EXECUTION
426 if (walk_state->op_info->flags & AML_FIELD) {
427 /*
428 * If we are executing a method, do not create any namespace objects
429 * during the load phase, only during execution.
430 */
431 if (!walk_state->method_node) {
432 if (walk_state->opcode == AML_FIELD_OP ||
433 walk_state->opcode == AML_BANK_FIELD_OP ||
434 walk_state->opcode == AML_INDEX_FIELD_OP) {
435 status =
436 acpi_ds_init_field_objects(op, walk_state);
437 }
438 }
439 return_ACPI_STATUS(status);
440 }
441
442 /*
443 * If we are executing a method, do not create any namespace objects
444 * during the load phase, only during execution.
445 */
446 if (!walk_state->method_node) {
447 if (op->common.aml_opcode == AML_REGION_OP) {
448 status =
449 acpi_ex_create_region(op->named.data,
450 op->named.length,
451 (acpi_adr_space_type)
452 ((op->common.value.arg)->
453 common.value.integer),
454 walk_state);
455 if (ACPI_FAILURE(status)) {
456 return_ACPI_STATUS(status);
457 }
458 } else if (op->common.aml_opcode == AML_DATA_REGION_OP) {
459 status =
460 acpi_ex_create_region(op->named.data,
461 op->named.length,
462 ACPI_ADR_SPACE_DATA_TABLE,
463 walk_state);
464 if (ACPI_FAILURE(status)) {
465 return_ACPI_STATUS(status);
466 }
467 }
468 }
469 #endif
470
471 if (op->common.aml_opcode == AML_NAME_OP) {
472
473 /* For Name opcode, get the object type from the argument */
474
475 if (op->common.value.arg) {
476 object_type = (acpi_ps_get_opcode_info((op->common.
477 value.arg)->
478 common.
479 aml_opcode))->
480 object_type;
481
482 /* Set node type if we have a namespace node */
483
484 if (op->common.node) {
485 op->common.node->type = (u8) object_type;
486 }
487 }
488 }
489 #ifdef ACPI_ASL_COMPILER
490 /*
491 * For external opcode, get the object type from the argument and
492 * get the parameter count from the argument's next.
493 */
494 if (acpi_gbl_disasm_flag &&
495 op->common.node && op->common.aml_opcode == AML_EXTERNAL_OP) {
496 /*
497 * Note, if this external is not a method
498 * Op->Common.Value.Arg->Common.Next->Common.Value.Integer == 0
499 * Therefore, param_count will be 0.
500 */
501 param_count =
502 (u8)op->common.value.arg->common.next->common.value.integer;
503 object_type = (u8)op->common.value.arg->common.value.integer;
504 op->common.node->flags |= ANOBJ_IS_EXTERNAL;
505 op->common.node->type = (u8)object_type;
506
507 acpi_dm_create_subobject_for_external((u8)object_type,
508 &op->common.node,
509 param_count);
510
511 /*
512 * Add the external to the external list because we may be
513 * emitting code based off of the items within the external list.
514 */
515 acpi_dm_add_op_to_external_list(op, op->named.path,
516 (u8)object_type, param_count,
517 ACPI_EXT_ORIGIN_FROM_OPCODE |
518 ACPI_EXT_RESOLVED_REFERENCE);
519 }
520 #endif
521
522 /*
523 * If we are executing a method, do not create any namespace objects
524 * during the load phase, only during execution.
525 */
526 if (!walk_state->method_node) {
527 if (op->common.aml_opcode == AML_METHOD_OP) {
528 /*
529 * method_op pkg_length name_string method_flags term_list
530 *
531 * Note: We must create the method node/object pair as soon as we
532 * see the method declaration. This allows later pass1 parsing
533 * of invocations of the method (need to know the number of
534 * arguments.)
535 */
536 ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
537 "LOADING-Method: State=%p Op=%p NamedObj=%p\n",
538 walk_state, op, op->named.node));
539
540 if (!acpi_ns_get_attached_object(op->named.node)) {
541 walk_state->operands[0] =
542 ACPI_CAST_PTR(void, op->named.node);
543 walk_state->num_operands = 1;
544
545 status =
546 acpi_ds_create_operands(walk_state,
547 op->common.value.
548 arg);
549 if (ACPI_SUCCESS(status)) {
550 status =
551 acpi_ex_create_method(op->named.
552 data,
553 op->named.
554 length,
555 walk_state);
556 }
557
558 walk_state->operands[0] = NULL;
559 walk_state->num_operands = 0;
560
561 if (ACPI_FAILURE(status)) {
562 return_ACPI_STATUS(status);
563 }
564 }
565 }
566 }
567
568 /* Pop the scope stack (only if loading a table) */
569
570 if (!walk_state->method_node &&
571 op->common.aml_opcode != AML_EXTERNAL_OP &&
572 acpi_ns_opens_scope(object_type)) {
573 ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
574 "(%s): Popping scope for Op %p\n",
575 acpi_ut_get_type_name(object_type), op));
576
577 status = acpi_ds_scope_stack_pop(walk_state);
578 }
579
580 return_ACPI_STATUS(status);
581 }
582