dswload.c 15 KB

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