dswload.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. /******************************************************************************
  2. *
  3. * Module Name: dswload - Dispatcher first pass namespace load callbacks
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2016, Intel Corp.
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions, and the following disclaimer,
  15. * without modification.
  16. * 2. Redistributions in binary form must reproduce at minimum a disclaimer
  17. * substantially similar to the "NO WARRANTY" disclaimer below
  18. * ("Disclaimer") and any redistribution must be conditioned upon
  19. * including a substantially similar Disclaimer requirement for further
  20. * binary redistribution.
  21. * 3. Neither the names of the above-listed copyright holders nor the names
  22. * of any contributors may be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * Alternatively, this software may be distributed under the terms of the
  26. * GNU General Public License ("GPL") version 2 as published by the Free
  27. * Software Foundation.
  28. *
  29. * NO WARRANTY
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  31. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  32. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
  33. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  34. * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  35. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  36. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  37. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  38. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  39. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  40. * POSSIBILITY OF SUCH DAMAGES.
  41. */
  42. #include <acpi/acpi.h>
  43. #include "accommon.h"
  44. #include "acparser.h"
  45. #include "amlcode.h"
  46. #include "acdispat.h"
  47. #include "acinterp.h"
  48. #include "acnamesp.h"
  49. #ifdef ACPI_ASL_COMPILER
  50. #include "acdisasm.h"
  51. #endif
  52. #define _COMPONENT ACPI_DISPATCHER
  53. ACPI_MODULE_NAME("dswload")
  54. /*******************************************************************************
  55. *
  56. * FUNCTION: acpi_ds_init_callbacks
  57. *
  58. * PARAMETERS: walk_state - Current state of the parse tree walk
  59. * pass_number - 1, 2, or 3
  60. *
  61. * RETURN: Status
  62. *
  63. * DESCRIPTION: Init walk state callbacks
  64. *
  65. ******************************************************************************/
  66. acpi_status
  67. acpi_ds_init_callbacks(struct acpi_walk_state *walk_state, u32 pass_number)
  68. {
  69. switch (pass_number) {
  70. case 0:
  71. /* Parse only - caller will setup callbacks */
  72. walk_state->parse_flags = ACPI_PARSE_LOAD_PASS1 |
  73. ACPI_PARSE_DELETE_TREE | ACPI_PARSE_DISASSEMBLE;
  74. walk_state->descending_callback = NULL;
  75. walk_state->ascending_callback = NULL;
  76. break;
  77. case 1:
  78. /* Load pass 1 */
  79. walk_state->parse_flags = ACPI_PARSE_LOAD_PASS1 |
  80. ACPI_PARSE_DELETE_TREE;
  81. walk_state->descending_callback = acpi_ds_load1_begin_op;
  82. walk_state->ascending_callback = acpi_ds_load1_end_op;
  83. break;
  84. case 2:
  85. /* Load pass 2 */
  86. walk_state->parse_flags = ACPI_PARSE_LOAD_PASS1 |
  87. ACPI_PARSE_DELETE_TREE;
  88. walk_state->descending_callback = acpi_ds_load2_begin_op;
  89. walk_state->ascending_callback = acpi_ds_load2_end_op;
  90. break;
  91. case 3:
  92. /* Execution pass */
  93. #ifndef ACPI_NO_METHOD_EXECUTION
  94. walk_state->parse_flags |= ACPI_PARSE_EXECUTE |
  95. ACPI_PARSE_DELETE_TREE;
  96. walk_state->descending_callback = acpi_ds_exec_begin_op;
  97. walk_state->ascending_callback = acpi_ds_exec_end_op;
  98. #endif
  99. break;
  100. default:
  101. return (AE_BAD_PARAMETER);
  102. }
  103. return (AE_OK);
  104. }
  105. /*******************************************************************************
  106. *
  107. * FUNCTION: acpi_ds_load1_begin_op
  108. *
  109. * PARAMETERS: walk_state - Current state of the parse tree walk
  110. * out_op - Where to return op if a new one is created
  111. *
  112. * RETURN: Status
  113. *
  114. * DESCRIPTION: Descending callback used during the loading of ACPI tables.
  115. *
  116. ******************************************************************************/
  117. acpi_status
  118. acpi_ds_load1_begin_op(struct acpi_walk_state *walk_state,
  119. union acpi_parse_object **out_op)
  120. {
  121. union acpi_parse_object *op;
  122. struct acpi_namespace_node *node;
  123. acpi_status status;
  124. acpi_object_type object_type;
  125. char *path;
  126. u32 flags;
  127. ACPI_FUNCTION_TRACE(ds_load1_begin_op);
  128. op = walk_state->op;
  129. ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH, "Op=%p State=%p\n", op,
  130. walk_state));
  131. /* We are only interested in opcodes that have an associated name */
  132. if (op) {
  133. if (!(walk_state->op_info->flags & AML_NAMED)) {
  134. *out_op = op;
  135. return_ACPI_STATUS(AE_OK);
  136. }
  137. /* Check if this object has already been installed in the namespace */
  138. if (op->common.node) {
  139. *out_op = op;
  140. return_ACPI_STATUS(AE_OK);
  141. }
  142. }
  143. path = acpi_ps_get_next_namestring(&walk_state->parser_state);
  144. /* Map the raw opcode into an internal object type */
  145. object_type = walk_state->op_info->object_type;
  146. ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
  147. "State=%p Op=%p [%s]\n", walk_state, op,
  148. acpi_ut_get_type_name(object_type)));
  149. switch (walk_state->opcode) {
  150. case AML_SCOPE_OP:
  151. /*
  152. * The target name of the Scope() operator must exist at this point so
  153. * that we can actually open the scope to enter new names underneath it.
  154. * Allow search-to-root for single namesegs.
  155. */
  156. status =
  157. acpi_ns_lookup(walk_state->scope_info, path, object_type,
  158. ACPI_IMODE_EXECUTE, ACPI_NS_SEARCH_PARENT,
  159. walk_state, &(node));
  160. #ifdef ACPI_ASL_COMPILER
  161. if (status == AE_NOT_FOUND) {
  162. /*
  163. * Table disassembly:
  164. * Target of Scope() not found. Generate an External for it, and
  165. * insert the name into the namespace.
  166. */
  167. acpi_dm_add_op_to_external_list(op, path,
  168. ACPI_TYPE_DEVICE, 0, 0);
  169. status =
  170. acpi_ns_lookup(walk_state->scope_info, path,
  171. object_type, ACPI_IMODE_LOAD_PASS1,
  172. ACPI_NS_SEARCH_PARENT, walk_state,
  173. &node);
  174. }
  175. #endif
  176. if (ACPI_FAILURE(status)) {
  177. ACPI_ERROR_NAMESPACE(path, status);
  178. return_ACPI_STATUS(status);
  179. }
  180. /*
  181. * Check to make sure that the target is
  182. * one of the opcodes that actually opens a scope
  183. */
  184. switch (node->type) {
  185. case ACPI_TYPE_ANY:
  186. case ACPI_TYPE_LOCAL_SCOPE: /* Scope */
  187. case ACPI_TYPE_DEVICE:
  188. case ACPI_TYPE_POWER:
  189. case ACPI_TYPE_PROCESSOR:
  190. case ACPI_TYPE_THERMAL:
  191. /* These are acceptable types */
  192. break;
  193. case ACPI_TYPE_INTEGER:
  194. case ACPI_TYPE_STRING:
  195. case ACPI_TYPE_BUFFER:
  196. /*
  197. * These types we will allow, but we will change the type.
  198. * This enables some existing code of the form:
  199. *
  200. * Name (DEB, 0)
  201. * Scope (DEB) { ... }
  202. *
  203. * Note: silently change the type here. On the second pass,
  204. * we will report a warning
  205. */
  206. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  207. "Type override - [%4.4s] had invalid type (%s) "
  208. "for Scope operator, changed to type ANY\n",
  209. acpi_ut_get_node_name(node),
  210. acpi_ut_get_type_name(node->type)));
  211. node->type = ACPI_TYPE_ANY;
  212. walk_state->scope_info->common.value = ACPI_TYPE_ANY;
  213. break;
  214. case ACPI_TYPE_METHOD:
  215. /*
  216. * Allow scope change to root during execution of module-level
  217. * code. Root is typed METHOD during this time.
  218. */
  219. if ((node == acpi_gbl_root_node) &&
  220. (walk_state->
  221. parse_flags & ACPI_PARSE_MODULE_LEVEL)) {
  222. break;
  223. }
  224. /*lint -fallthrough */
  225. default:
  226. /* All other types are an error */
  227. ACPI_ERROR((AE_INFO,
  228. "Invalid type (%s) for target of "
  229. "Scope operator [%4.4s] (Cannot override)",
  230. acpi_ut_get_type_name(node->type),
  231. acpi_ut_get_node_name(node)));
  232. return_ACPI_STATUS(AE_AML_OPERAND_TYPE);
  233. }
  234. break;
  235. default:
  236. /*
  237. * For all other named opcodes, we will enter the name into
  238. * the namespace.
  239. *
  240. * Setup the search flags.
  241. * Since we are entering a name into the namespace, we do not want to
  242. * enable the search-to-root upsearch.
  243. *
  244. * There are only two conditions where it is acceptable that the name
  245. * already exists:
  246. * 1) the Scope() operator can reopen a scoping object that was
  247. * previously defined (Scope, Method, Device, etc.)
  248. * 2) Whenever we are parsing a deferred opcode (op_region, Buffer,
  249. * buffer_field, or Package), the name of the object is already
  250. * in the namespace.
  251. */
  252. if (walk_state->deferred_node) {
  253. /* This name is already in the namespace, get the node */
  254. node = walk_state->deferred_node;
  255. status = AE_OK;
  256. break;
  257. }
  258. /*
  259. * If we are executing a method, do not create any namespace objects
  260. * during the load phase, only during execution.
  261. */
  262. if (walk_state->method_node) {
  263. node = NULL;
  264. status = AE_OK;
  265. break;
  266. }
  267. flags = ACPI_NS_NO_UPSEARCH;
  268. if ((walk_state->opcode != AML_SCOPE_OP) &&
  269. (!(walk_state->parse_flags & ACPI_PARSE_DEFERRED_OP))) {
  270. if (walk_state->namespace_override) {
  271. flags |= ACPI_NS_OVERRIDE_IF_FOUND;
  272. ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
  273. "[%s] Override allowed\n",
  274. acpi_ut_get_type_name
  275. (object_type)));
  276. } else {
  277. flags |= ACPI_NS_ERROR_IF_FOUND;
  278. ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
  279. "[%s] Cannot already exist\n",
  280. acpi_ut_get_type_name
  281. (object_type)));
  282. }
  283. } else {
  284. ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
  285. "[%s] Both Find or Create allowed\n",
  286. acpi_ut_get_type_name(object_type)));
  287. }
  288. /*
  289. * Enter the named type into the internal namespace. We enter the name
  290. * as we go downward in the parse tree. Any necessary subobjects that
  291. * involve arguments to the opcode must be created as we go back up the
  292. * parse tree later.
  293. */
  294. status =
  295. acpi_ns_lookup(walk_state->scope_info, path, object_type,
  296. ACPI_IMODE_LOAD_PASS1, flags, walk_state,
  297. &node);
  298. if (ACPI_FAILURE(status)) {
  299. if (status == AE_ALREADY_EXISTS) {
  300. /* The name already exists in this scope */
  301. if (node->flags & ANOBJ_IS_EXTERNAL) {
  302. /*
  303. * Allow one create on an object or segment that was
  304. * previously declared External
  305. */
  306. node->flags &= ~ANOBJ_IS_EXTERNAL;
  307. node->type = (u8) object_type;
  308. /* Just retyped a node, probably will need to open a scope */
  309. if (acpi_ns_opens_scope(object_type)) {
  310. status =
  311. acpi_ds_scope_stack_push
  312. (node, object_type,
  313. walk_state);
  314. if (ACPI_FAILURE(status)) {
  315. return_ACPI_STATUS
  316. (status);
  317. }
  318. }
  319. status = AE_OK;
  320. }
  321. }
  322. if (ACPI_FAILURE(status)) {
  323. ACPI_ERROR_NAMESPACE(path, status);
  324. return_ACPI_STATUS(status);
  325. }
  326. }
  327. break;
  328. }
  329. /* Common exit */
  330. if (!op) {
  331. /* Create a new op */
  332. op = acpi_ps_alloc_op(walk_state->opcode, walk_state->aml);
  333. if (!op) {
  334. return_ACPI_STATUS(AE_NO_MEMORY);
  335. }
  336. }
  337. /* Initialize the op */
  338. #if (defined (ACPI_NO_METHOD_EXECUTION) || defined (ACPI_CONSTANT_EVAL_ONLY))
  339. op->named.path = ACPI_CAST_PTR(u8, path);
  340. #endif
  341. if (node) {
  342. /*
  343. * Put the Node in the "op" object that the parser uses, so we
  344. * can get it again quickly when this scope is closed
  345. */
  346. op->common.node = node;
  347. op->named.name = node->name.integer;
  348. }
  349. acpi_ps_append_arg(acpi_ps_get_parent_scope(&walk_state->parser_state),
  350. op);
  351. *out_op = op;
  352. return_ACPI_STATUS(status);
  353. }
  354. /*******************************************************************************
  355. *
  356. * FUNCTION: acpi_ds_load1_end_op
  357. *
  358. * PARAMETERS: walk_state - Current state of the parse tree walk
  359. *
  360. * RETURN: Status
  361. *
  362. * DESCRIPTION: Ascending callback used during the loading of the namespace,
  363. * both control methods and everything else.
  364. *
  365. ******************************************************************************/
  366. acpi_status acpi_ds_load1_end_op(struct acpi_walk_state *walk_state)
  367. {
  368. union acpi_parse_object *op;
  369. acpi_object_type object_type;
  370. acpi_status status = AE_OK;
  371. ACPI_FUNCTION_TRACE(ds_load1_end_op);
  372. op = walk_state->op;
  373. ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH, "Op=%p State=%p\n", op,
  374. walk_state));
  375. /* We are only interested in opcodes that have an associated name */
  376. if (!(walk_state->op_info->flags & (AML_NAMED | AML_FIELD))) {
  377. return_ACPI_STATUS(AE_OK);
  378. }
  379. /* Get the object type to determine if we should pop the scope */
  380. object_type = walk_state->op_info->object_type;
  381. #ifndef ACPI_NO_METHOD_EXECUTION
  382. if (walk_state->op_info->flags & AML_FIELD) {
  383. /*
  384. * If we are executing a method, do not create any namespace objects
  385. * during the load phase, only during execution.
  386. */
  387. if (!walk_state->method_node) {
  388. if (walk_state->opcode == AML_FIELD_OP ||
  389. walk_state->opcode == AML_BANK_FIELD_OP ||
  390. walk_state->opcode == AML_INDEX_FIELD_OP) {
  391. status =
  392. acpi_ds_init_field_objects(op, walk_state);
  393. }
  394. }
  395. return_ACPI_STATUS(status);
  396. }
  397. /*
  398. * If we are executing a method, do not create any namespace objects
  399. * during the load phase, only during execution.
  400. */
  401. if (!walk_state->method_node) {
  402. if (op->common.aml_opcode == AML_REGION_OP) {
  403. status =
  404. acpi_ex_create_region(op->named.data,
  405. op->named.length,
  406. (acpi_adr_space_type)
  407. ((op->common.value.arg)->
  408. common.value.integer),
  409. walk_state);
  410. if (ACPI_FAILURE(status)) {
  411. return_ACPI_STATUS(status);
  412. }
  413. } else if (op->common.aml_opcode == AML_DATA_REGION_OP) {
  414. status =
  415. acpi_ex_create_region(op->named.data,
  416. op->named.length,
  417. ACPI_ADR_SPACE_DATA_TABLE,
  418. walk_state);
  419. if (ACPI_FAILURE(status)) {
  420. return_ACPI_STATUS(status);
  421. }
  422. }
  423. }
  424. #endif
  425. if (op->common.aml_opcode == AML_NAME_OP) {
  426. /* For Name opcode, get the object type from the argument */
  427. if (op->common.value.arg) {
  428. object_type = (acpi_ps_get_opcode_info((op->common.
  429. value.arg)->
  430. common.
  431. aml_opcode))->
  432. object_type;
  433. /* Set node type if we have a namespace node */
  434. if (op->common.node) {
  435. op->common.node->type = (u8) object_type;
  436. }
  437. }
  438. }
  439. /*
  440. * If we are executing a method, do not create any namespace objects
  441. * during the load phase, only during execution.
  442. */
  443. if (!walk_state->method_node) {
  444. if (op->common.aml_opcode == AML_METHOD_OP) {
  445. /*
  446. * method_op pkg_length name_string method_flags term_list
  447. *
  448. * Note: We must create the method node/object pair as soon as we
  449. * see the method declaration. This allows later pass1 parsing
  450. * of invocations of the method (need to know the number of
  451. * arguments.)
  452. */
  453. ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
  454. "LOADING-Method: State=%p Op=%p NamedObj=%p\n",
  455. walk_state, op, op->named.node));
  456. if (!acpi_ns_get_attached_object(op->named.node)) {
  457. walk_state->operands[0] =
  458. ACPI_CAST_PTR(void, op->named.node);
  459. walk_state->num_operands = 1;
  460. status =
  461. acpi_ds_create_operands(walk_state,
  462. op->common.value.
  463. arg);
  464. if (ACPI_SUCCESS(status)) {
  465. status =
  466. acpi_ex_create_method(op->named.
  467. data,
  468. op->named.
  469. length,
  470. walk_state);
  471. }
  472. walk_state->operands[0] = NULL;
  473. walk_state->num_operands = 0;
  474. if (ACPI_FAILURE(status)) {
  475. return_ACPI_STATUS(status);
  476. }
  477. }
  478. }
  479. }
  480. /* Pop the scope stack (only if loading a table) */
  481. if (!walk_state->method_node && acpi_ns_opens_scope(object_type)) {
  482. ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
  483. "(%s): Popping scope for Op %p\n",
  484. acpi_ut_get_type_name(object_type), op));
  485. status = acpi_ds_scope_stack_pop(walk_state);
  486. }
  487. return_ACPI_STATUS(status);
  488. }