dswload.c 16 KB

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