dswload2.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  1. // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
  2. /******************************************************************************
  3. *
  4. * Module Name: dswload2 - Dispatcher second pass namespace load callbacks
  5. *
  6. * Copyright (C) 2000 - 2018, Intel Corp.
  7. *
  8. *****************************************************************************/
  9. #include <acpi/acpi.h>
  10. #include "accommon.h"
  11. #include "acparser.h"
  12. #include "amlcode.h"
  13. #include "acdispat.h"
  14. #include "acinterp.h"
  15. #include "acnamesp.h"
  16. #include "acevents.h"
  17. #define _COMPONENT ACPI_DISPATCHER
  18. ACPI_MODULE_NAME("dswload2")
  19. /*******************************************************************************
  20. *
  21. * FUNCTION: acpi_ds_load2_begin_op
  22. *
  23. * PARAMETERS: walk_state - Current state of the parse tree walk
  24. * out_op - Wher to return op if a new one is created
  25. *
  26. * RETURN: Status
  27. *
  28. * DESCRIPTION: Descending callback used during the loading of ACPI tables.
  29. *
  30. ******************************************************************************/
  31. acpi_status
  32. acpi_ds_load2_begin_op(struct acpi_walk_state *walk_state,
  33. union acpi_parse_object **out_op)
  34. {
  35. union acpi_parse_object *op;
  36. struct acpi_namespace_node *node;
  37. acpi_status status;
  38. acpi_object_type object_type;
  39. char *buffer_ptr;
  40. u32 flags;
  41. ACPI_FUNCTION_TRACE(ds_load2_begin_op);
  42. op = walk_state->op;
  43. ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH, "Op=%p State=%p\n", op,
  44. walk_state));
  45. if (op) {
  46. if ((walk_state->control_state) &&
  47. (walk_state->control_state->common.state ==
  48. ACPI_CONTROL_CONDITIONAL_EXECUTING)) {
  49. /* We are executing a while loop outside of a method */
  50. status = acpi_ds_exec_begin_op(walk_state, out_op);
  51. return_ACPI_STATUS(status);
  52. }
  53. /* We only care about Namespace opcodes here */
  54. if ((!(walk_state->op_info->flags & AML_NSOPCODE) &&
  55. (walk_state->opcode != AML_INT_NAMEPATH_OP)) ||
  56. (!(walk_state->op_info->flags & AML_NAMED))) {
  57. return_ACPI_STATUS(AE_OK);
  58. }
  59. /* Get the name we are going to enter or lookup in the namespace */
  60. if (walk_state->opcode == AML_INT_NAMEPATH_OP) {
  61. /* For Namepath op, get the path string */
  62. buffer_ptr = op->common.value.string;
  63. if (!buffer_ptr) {
  64. /* No name, just exit */
  65. return_ACPI_STATUS(AE_OK);
  66. }
  67. } else {
  68. /* Get name from the op */
  69. buffer_ptr = ACPI_CAST_PTR(char, &op->named.name);
  70. }
  71. } else {
  72. /* Get the namestring from the raw AML */
  73. buffer_ptr =
  74. acpi_ps_get_next_namestring(&walk_state->parser_state);
  75. }
  76. /* Map the opcode into an internal object type */
  77. object_type = walk_state->op_info->object_type;
  78. ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
  79. "State=%p Op=%p Type=%X\n", walk_state, op,
  80. object_type));
  81. switch (walk_state->opcode) {
  82. case AML_FIELD_OP:
  83. case AML_BANK_FIELD_OP:
  84. case AML_INDEX_FIELD_OP:
  85. node = NULL;
  86. status = AE_OK;
  87. break;
  88. case AML_INT_NAMEPATH_OP:
  89. /*
  90. * The name_path is an object reference to an existing object.
  91. * Don't enter the name into the namespace, but look it up
  92. * for use later.
  93. */
  94. status =
  95. acpi_ns_lookup(walk_state->scope_info, buffer_ptr,
  96. object_type, ACPI_IMODE_EXECUTE,
  97. ACPI_NS_SEARCH_PARENT, walk_state, &(node));
  98. break;
  99. case AML_SCOPE_OP:
  100. /* Special case for Scope(\) -> refers to the Root node */
  101. if (op && (op->named.node == acpi_gbl_root_node)) {
  102. node = op->named.node;
  103. status =
  104. acpi_ds_scope_stack_push(node, object_type,
  105. walk_state);
  106. if (ACPI_FAILURE(status)) {
  107. return_ACPI_STATUS(status);
  108. }
  109. } else {
  110. /*
  111. * The Path is an object reference to an existing object.
  112. * Don't enter the name into the namespace, but look it up
  113. * for use later.
  114. */
  115. status =
  116. acpi_ns_lookup(walk_state->scope_info, buffer_ptr,
  117. object_type, ACPI_IMODE_EXECUTE,
  118. ACPI_NS_SEARCH_PARENT, walk_state,
  119. &(node));
  120. if (ACPI_FAILURE(status)) {
  121. #ifdef ACPI_ASL_COMPILER
  122. if (status == AE_NOT_FOUND) {
  123. status = AE_OK;
  124. } else {
  125. ACPI_ERROR_NAMESPACE(walk_state->
  126. scope_info,
  127. buffer_ptr,
  128. status);
  129. }
  130. #else
  131. ACPI_ERROR_NAMESPACE(walk_state->scope_info,
  132. buffer_ptr, status);
  133. #endif
  134. return_ACPI_STATUS(status);
  135. }
  136. }
  137. /*
  138. * We must check to make sure that the target is
  139. * one of the opcodes that actually opens a scope
  140. */
  141. switch (node->type) {
  142. case ACPI_TYPE_ANY:
  143. case ACPI_TYPE_LOCAL_SCOPE: /* Scope */
  144. case ACPI_TYPE_DEVICE:
  145. case ACPI_TYPE_POWER:
  146. case ACPI_TYPE_PROCESSOR:
  147. case ACPI_TYPE_THERMAL:
  148. /* These are acceptable types */
  149. break;
  150. case ACPI_TYPE_INTEGER:
  151. case ACPI_TYPE_STRING:
  152. case ACPI_TYPE_BUFFER:
  153. /*
  154. * These types we will allow, but we will change the type.
  155. * This enables some existing code of the form:
  156. *
  157. * Name (DEB, 0)
  158. * Scope (DEB) { ... }
  159. */
  160. ACPI_WARNING((AE_INFO,
  161. "Type override - [%4.4s] had invalid type (%s) "
  162. "for Scope operator, changed to type ANY",
  163. acpi_ut_get_node_name(node),
  164. acpi_ut_get_type_name(node->type)));
  165. node->type = ACPI_TYPE_ANY;
  166. walk_state->scope_info->common.value = ACPI_TYPE_ANY;
  167. break;
  168. case ACPI_TYPE_METHOD:
  169. /*
  170. * Allow scope change to root during execution of module-level
  171. * code. Root is typed METHOD during this time.
  172. */
  173. if ((node == acpi_gbl_root_node) &&
  174. (walk_state->
  175. parse_flags & ACPI_PARSE_MODULE_LEVEL)) {
  176. break;
  177. }
  178. /*lint -fallthrough */
  179. default:
  180. /* All other types are an error */
  181. ACPI_ERROR((AE_INFO,
  182. "Invalid type (%s) for target of "
  183. "Scope operator [%4.4s] (Cannot override)",
  184. acpi_ut_get_type_name(node->type),
  185. acpi_ut_get_node_name(node)));
  186. return_ACPI_STATUS(AE_AML_OPERAND_TYPE);
  187. }
  188. break;
  189. default:
  190. /* All other opcodes */
  191. if (op && op->common.node) {
  192. /* This op/node was previously entered into the namespace */
  193. node = op->common.node;
  194. if (acpi_ns_opens_scope(object_type)) {
  195. status =
  196. acpi_ds_scope_stack_push(node, object_type,
  197. walk_state);
  198. if (ACPI_FAILURE(status)) {
  199. return_ACPI_STATUS(status);
  200. }
  201. }
  202. return_ACPI_STATUS(AE_OK);
  203. }
  204. /*
  205. * Enter the named type into the internal namespace. We enter the name
  206. * as we go downward in the parse tree. Any necessary subobjects that
  207. * involve arguments to the opcode must be created as we go back up the
  208. * parse tree later.
  209. *
  210. * Note: Name may already exist if we are executing a deferred opcode.
  211. */
  212. if (walk_state->deferred_node) {
  213. /* This name is already in the namespace, get the node */
  214. node = walk_state->deferred_node;
  215. status = AE_OK;
  216. break;
  217. }
  218. flags = ACPI_NS_NO_UPSEARCH;
  219. if (walk_state->pass_number == ACPI_IMODE_EXECUTE) {
  220. /* Execution mode, node cannot already exist, node is temporary */
  221. flags |= ACPI_NS_ERROR_IF_FOUND;
  222. if (!
  223. (walk_state->
  224. parse_flags & ACPI_PARSE_MODULE_LEVEL)) {
  225. flags |= ACPI_NS_TEMPORARY;
  226. }
  227. }
  228. #ifdef ACPI_ASL_COMPILER
  229. /*
  230. * Do not open a scope for AML_EXTERNAL_OP
  231. * acpi_ns_lookup can open a new scope based on the object type
  232. * of this op. AML_EXTERNAL_OP is a declaration rather than a
  233. * definition. In the case that this external is a method object,
  234. * acpi_ns_lookup will open a new scope. However, an AML_EXTERNAL_OP
  235. * associated with the ACPI_TYPE_METHOD is a declaration, rather than
  236. * a definition. Flags is set to avoid opening a scope for any
  237. * AML_EXTERNAL_OP.
  238. */
  239. if (walk_state->opcode == AML_EXTERNAL_OP) {
  240. flags |= ACPI_NS_DONT_OPEN_SCOPE;
  241. }
  242. #endif
  243. /* Add new entry or lookup existing entry */
  244. status =
  245. acpi_ns_lookup(walk_state->scope_info, buffer_ptr,
  246. object_type, ACPI_IMODE_LOAD_PASS2, flags,
  247. walk_state, &node);
  248. if (ACPI_SUCCESS(status) && (flags & ACPI_NS_TEMPORARY)) {
  249. ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
  250. "***New Node [%4.4s] %p is temporary\n",
  251. acpi_ut_get_node_name(node), node));
  252. }
  253. break;
  254. }
  255. if (ACPI_FAILURE(status)) {
  256. ACPI_ERROR_NAMESPACE(walk_state->scope_info,
  257. buffer_ptr, status);
  258. return_ACPI_STATUS(status);
  259. }
  260. if (!op) {
  261. /* Create a new op */
  262. op = acpi_ps_alloc_op(walk_state->opcode, walk_state->aml);
  263. if (!op) {
  264. return_ACPI_STATUS(AE_NO_MEMORY);
  265. }
  266. /* Initialize the new op */
  267. if (node) {
  268. op->named.name = node->name.integer;
  269. }
  270. *out_op = op;
  271. }
  272. /*
  273. * Put the Node in the "op" object that the parser uses, so we
  274. * can get it again quickly when this scope is closed
  275. */
  276. op->common.node = node;
  277. return_ACPI_STATUS(status);
  278. }
  279. /*******************************************************************************
  280. *
  281. * FUNCTION: acpi_ds_load2_end_op
  282. *
  283. * PARAMETERS: walk_state - Current state of the parse tree walk
  284. *
  285. * RETURN: Status
  286. *
  287. * DESCRIPTION: Ascending callback used during the loading of the namespace,
  288. * both control methods and everything else.
  289. *
  290. ******************************************************************************/
  291. acpi_status acpi_ds_load2_end_op(struct acpi_walk_state *walk_state)
  292. {
  293. union acpi_parse_object *op;
  294. acpi_status status = AE_OK;
  295. acpi_object_type object_type;
  296. struct acpi_namespace_node *node;
  297. union acpi_parse_object *arg;
  298. struct acpi_namespace_node *new_node;
  299. #ifndef ACPI_NO_METHOD_EXECUTION
  300. u32 i;
  301. u8 region_space;
  302. #endif
  303. ACPI_FUNCTION_TRACE(ds_load2_end_op);
  304. op = walk_state->op;
  305. ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH, "Opcode [%s] Op %p State %p\n",
  306. walk_state->op_info->name, op, walk_state));
  307. /* Check if opcode had an associated namespace object */
  308. if (!(walk_state->op_info->flags & AML_NSOBJECT)) {
  309. return_ACPI_STATUS(AE_OK);
  310. }
  311. if (op->common.aml_opcode == AML_SCOPE_OP) {
  312. ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
  313. "Ending scope Op=%p State=%p\n", op,
  314. walk_state));
  315. }
  316. object_type = walk_state->op_info->object_type;
  317. /*
  318. * Get the Node/name from the earlier lookup
  319. * (It was saved in the *op structure)
  320. */
  321. node = op->common.node;
  322. /*
  323. * Put the Node on the object stack (Contains the ACPI Name of
  324. * this object)
  325. */
  326. walk_state->operands[0] = (void *)node;
  327. walk_state->num_operands = 1;
  328. /* Pop the scope stack */
  329. if (acpi_ns_opens_scope(object_type) &&
  330. (op->common.aml_opcode != AML_INT_METHODCALL_OP)) {
  331. ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
  332. "(%s) Popping scope for Op %p\n",
  333. acpi_ut_get_type_name(object_type), op));
  334. status = acpi_ds_scope_stack_pop(walk_state);
  335. if (ACPI_FAILURE(status)) {
  336. goto cleanup;
  337. }
  338. }
  339. /*
  340. * Named operations are as follows:
  341. *
  342. * AML_ALIAS
  343. * AML_BANKFIELD
  344. * AML_CREATEBITFIELD
  345. * AML_CREATEBYTEFIELD
  346. * AML_CREATEDWORDFIELD
  347. * AML_CREATEFIELD
  348. * AML_CREATEQWORDFIELD
  349. * AML_CREATEWORDFIELD
  350. * AML_DATA_REGION
  351. * AML_DEVICE
  352. * AML_EVENT
  353. * AML_FIELD
  354. * AML_INDEXFIELD
  355. * AML_METHOD
  356. * AML_METHODCALL
  357. * AML_MUTEX
  358. * AML_NAME
  359. * AML_NAMEDFIELD
  360. * AML_OPREGION
  361. * AML_POWERRES
  362. * AML_PROCESSOR
  363. * AML_SCOPE
  364. * AML_THERMALZONE
  365. */
  366. ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
  367. "Create-Load [%s] State=%p Op=%p NamedObj=%p\n",
  368. acpi_ps_get_opcode_name(op->common.aml_opcode),
  369. walk_state, op, node));
  370. /* Decode the opcode */
  371. arg = op->common.value.arg;
  372. switch (walk_state->op_info->type) {
  373. #ifndef ACPI_NO_METHOD_EXECUTION
  374. case AML_TYPE_CREATE_FIELD:
  375. /*
  376. * Create the field object, but the field buffer and index must
  377. * be evaluated later during the execution phase
  378. */
  379. status = acpi_ds_create_buffer_field(op, walk_state);
  380. break;
  381. case AML_TYPE_NAMED_FIELD:
  382. /*
  383. * If we are executing a method, initialize the field
  384. */
  385. if (walk_state->method_node) {
  386. status = acpi_ds_init_field_objects(op, walk_state);
  387. }
  388. switch (op->common.aml_opcode) {
  389. case AML_INDEX_FIELD_OP:
  390. status =
  391. acpi_ds_create_index_field(op,
  392. (acpi_handle)arg->common.
  393. node, walk_state);
  394. break;
  395. case AML_BANK_FIELD_OP:
  396. status =
  397. acpi_ds_create_bank_field(op, arg->common.node,
  398. walk_state);
  399. break;
  400. case AML_FIELD_OP:
  401. status =
  402. acpi_ds_create_field(op, arg->common.node,
  403. walk_state);
  404. break;
  405. default:
  406. /* All NAMED_FIELD opcodes must be handled above */
  407. break;
  408. }
  409. break;
  410. case AML_TYPE_NAMED_SIMPLE:
  411. status = acpi_ds_create_operands(walk_state, arg);
  412. if (ACPI_FAILURE(status)) {
  413. goto cleanup;
  414. }
  415. switch (op->common.aml_opcode) {
  416. case AML_PROCESSOR_OP:
  417. status = acpi_ex_create_processor(walk_state);
  418. break;
  419. case AML_POWER_RESOURCE_OP:
  420. status = acpi_ex_create_power_resource(walk_state);
  421. break;
  422. case AML_MUTEX_OP:
  423. status = acpi_ex_create_mutex(walk_state);
  424. break;
  425. case AML_EVENT_OP:
  426. status = acpi_ex_create_event(walk_state);
  427. break;
  428. case AML_ALIAS_OP:
  429. status = acpi_ex_create_alias(walk_state);
  430. break;
  431. default:
  432. /* Unknown opcode */
  433. status = AE_OK;
  434. goto cleanup;
  435. }
  436. /* Delete operands */
  437. for (i = 1; i < walk_state->num_operands; i++) {
  438. acpi_ut_remove_reference(walk_state->operands[i]);
  439. walk_state->operands[i] = NULL;
  440. }
  441. break;
  442. #endif /* ACPI_NO_METHOD_EXECUTION */
  443. case AML_TYPE_NAMED_COMPLEX:
  444. switch (op->common.aml_opcode) {
  445. #ifndef ACPI_NO_METHOD_EXECUTION
  446. case AML_REGION_OP:
  447. case AML_DATA_REGION_OP:
  448. if (op->common.aml_opcode == AML_REGION_OP) {
  449. region_space = (acpi_adr_space_type)
  450. ((op->common.value.arg)->common.value.
  451. integer);
  452. } else {
  453. region_space = ACPI_ADR_SPACE_DATA_TABLE;
  454. }
  455. /*
  456. * The op_region is not fully parsed at this time. The only valid
  457. * argument is the space_id. (We must save the address of the
  458. * AML of the address and length operands)
  459. *
  460. * If we have a valid region, initialize it. The namespace is
  461. * unlocked at this point.
  462. *
  463. * Need to unlock interpreter if it is locked (if we are running
  464. * a control method), in order to allow _REG methods to be run
  465. * during acpi_ev_initialize_region.
  466. */
  467. if (walk_state->method_node) {
  468. /*
  469. * Executing a method: initialize the region and unlock
  470. * the interpreter
  471. */
  472. status = acpi_ex_create_region(op->named.data,
  473. op->named.length,
  474. region_space,
  475. walk_state);
  476. if (ACPI_FAILURE(status)) {
  477. return_ACPI_STATUS(status);
  478. }
  479. }
  480. status =
  481. acpi_ev_initialize_region
  482. (acpi_ns_get_attached_object(node));
  483. break;
  484. case AML_NAME_OP:
  485. status = acpi_ds_create_node(walk_state, node, op);
  486. break;
  487. case AML_METHOD_OP:
  488. /*
  489. * method_op pkg_length name_string method_flags term_list
  490. *
  491. * Note: We must create the method node/object pair as soon as we
  492. * see the method declaration. This allows later pass1 parsing
  493. * of invocations of the method (need to know the number of
  494. * arguments.)
  495. */
  496. ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
  497. "LOADING-Method: State=%p Op=%p NamedObj=%p\n",
  498. walk_state, op, op->named.node));
  499. if (!acpi_ns_get_attached_object(op->named.node)) {
  500. walk_state->operands[0] =
  501. ACPI_CAST_PTR(void, op->named.node);
  502. walk_state->num_operands = 1;
  503. status =
  504. acpi_ds_create_operands(walk_state,
  505. op->common.value.
  506. arg);
  507. if (ACPI_SUCCESS(status)) {
  508. status =
  509. acpi_ex_create_method(op->named.
  510. data,
  511. op->named.
  512. length,
  513. walk_state);
  514. }
  515. walk_state->operands[0] = NULL;
  516. walk_state->num_operands = 0;
  517. if (ACPI_FAILURE(status)) {
  518. return_ACPI_STATUS(status);
  519. }
  520. }
  521. break;
  522. #endif /* ACPI_NO_METHOD_EXECUTION */
  523. default:
  524. /* All NAMED_COMPLEX opcodes must be handled above */
  525. break;
  526. }
  527. break;
  528. case AML_CLASS_INTERNAL:
  529. /* case AML_INT_NAMEPATH_OP: */
  530. break;
  531. case AML_CLASS_METHOD_CALL:
  532. ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
  533. "RESOLVING-MethodCall: State=%p Op=%p NamedObj=%p\n",
  534. walk_state, op, node));
  535. /*
  536. * Lookup the method name and save the Node
  537. */
  538. status =
  539. acpi_ns_lookup(walk_state->scope_info,
  540. arg->common.value.string, ACPI_TYPE_ANY,
  541. ACPI_IMODE_LOAD_PASS2,
  542. ACPI_NS_SEARCH_PARENT |
  543. ACPI_NS_DONT_OPEN_SCOPE, walk_state,
  544. &(new_node));
  545. if (ACPI_SUCCESS(status)) {
  546. /*
  547. * Make sure that what we found is indeed a method
  548. * We didn't search for a method on purpose, to see if the name
  549. * would resolve
  550. */
  551. if (new_node->type != ACPI_TYPE_METHOD) {
  552. status = AE_AML_OPERAND_TYPE;
  553. }
  554. /* We could put the returned object (Node) on the object stack for
  555. * later, but for now, we will put it in the "op" object that the
  556. * parser uses, so we can get it again at the end of this scope
  557. */
  558. op->common.node = new_node;
  559. } else {
  560. ACPI_ERROR_NAMESPACE(walk_state->scope_info,
  561. arg->common.value.string, status);
  562. }
  563. break;
  564. default:
  565. break;
  566. }
  567. cleanup:
  568. /* Remove the Node pushed at the very beginning */
  569. walk_state->operands[0] = NULL;
  570. walk_state->num_operands = 0;
  571. return_ACPI_STATUS(status);
  572. }