dswload.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. /******************************************************************************
  2. *
  3. * Module Name: dswload - Dispatcher first pass namespace load callbacks
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2014, 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. flags |= ACPI_NS_ERROR_IF_FOUND;
  271. ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
  272. "[%s] Cannot already exist\n",
  273. acpi_ut_get_type_name(object_type)));
  274. } else {
  275. ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
  276. "[%s] Both Find or Create allowed\n",
  277. acpi_ut_get_type_name(object_type)));
  278. }
  279. /*
  280. * Enter the named type into the internal namespace. We enter the name
  281. * as we go downward in the parse tree. Any necessary subobjects that
  282. * involve arguments to the opcode must be created as we go back up the
  283. * parse tree later.
  284. */
  285. status =
  286. acpi_ns_lookup(walk_state->scope_info, path, object_type,
  287. ACPI_IMODE_LOAD_PASS1, flags, walk_state,
  288. &node);
  289. if (ACPI_FAILURE(status)) {
  290. if (status == AE_ALREADY_EXISTS) {
  291. /* The name already exists in this scope */
  292. if (node->flags & ANOBJ_IS_EXTERNAL) {
  293. /*
  294. * Allow one create on an object or segment that was
  295. * previously declared External
  296. */
  297. node->flags &= ~ANOBJ_IS_EXTERNAL;
  298. node->type = (u8) object_type;
  299. /* Just retyped a node, probably will need to open a scope */
  300. if (acpi_ns_opens_scope(object_type)) {
  301. status =
  302. acpi_ds_scope_stack_push
  303. (node, object_type,
  304. walk_state);
  305. if (ACPI_FAILURE(status)) {
  306. return_ACPI_STATUS
  307. (status);
  308. }
  309. }
  310. status = AE_OK;
  311. }
  312. }
  313. if (ACPI_FAILURE(status)) {
  314. ACPI_ERROR_NAMESPACE(path, status);
  315. return_ACPI_STATUS(status);
  316. }
  317. }
  318. break;
  319. }
  320. /* Common exit */
  321. if (!op) {
  322. /* Create a new op */
  323. op = acpi_ps_alloc_op(walk_state->opcode);
  324. if (!op) {
  325. return_ACPI_STATUS(AE_NO_MEMORY);
  326. }
  327. }
  328. /* Initialize the op */
  329. #if (defined (ACPI_NO_METHOD_EXECUTION) || defined (ACPI_CONSTANT_EVAL_ONLY))
  330. op->named.path = ACPI_CAST_PTR(u8, path);
  331. #endif
  332. if (node) {
  333. /*
  334. * Put the Node in the "op" object that the parser uses, so we
  335. * can get it again quickly when this scope is closed
  336. */
  337. op->common.node = node;
  338. op->named.name = node->name.integer;
  339. }
  340. acpi_ps_append_arg(acpi_ps_get_parent_scope(&walk_state->parser_state),
  341. op);
  342. *out_op = op;
  343. return_ACPI_STATUS(status);
  344. }
  345. /*******************************************************************************
  346. *
  347. * FUNCTION: acpi_ds_load1_end_op
  348. *
  349. * PARAMETERS: walk_state - Current state of the parse tree walk
  350. *
  351. * RETURN: Status
  352. *
  353. * DESCRIPTION: Ascending callback used during the loading of the namespace,
  354. * both control methods and everything else.
  355. *
  356. ******************************************************************************/
  357. acpi_status acpi_ds_load1_end_op(struct acpi_walk_state *walk_state)
  358. {
  359. union acpi_parse_object *op;
  360. acpi_object_type object_type;
  361. acpi_status status = AE_OK;
  362. ACPI_FUNCTION_TRACE(ds_load1_end_op);
  363. op = walk_state->op;
  364. ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH, "Op=%p State=%p\n", op,
  365. walk_state));
  366. /* We are only interested in opcodes that have an associated name */
  367. if (!(walk_state->op_info->flags & (AML_NAMED | AML_FIELD))) {
  368. return_ACPI_STATUS(AE_OK);
  369. }
  370. /* Get the object type to determine if we should pop the scope */
  371. object_type = walk_state->op_info->object_type;
  372. #ifndef ACPI_NO_METHOD_EXECUTION
  373. if (walk_state->op_info->flags & AML_FIELD) {
  374. /*
  375. * If we are executing a method, do not create any namespace objects
  376. * during the load phase, only during execution.
  377. */
  378. if (!walk_state->method_node) {
  379. if (walk_state->opcode == AML_FIELD_OP ||
  380. walk_state->opcode == AML_BANK_FIELD_OP ||
  381. walk_state->opcode == AML_INDEX_FIELD_OP) {
  382. status =
  383. acpi_ds_init_field_objects(op, walk_state);
  384. }
  385. }
  386. return_ACPI_STATUS(status);
  387. }
  388. /*
  389. * If we are executing a method, do not create any namespace objects
  390. * during the load phase, only during execution.
  391. */
  392. if (!walk_state->method_node) {
  393. if (op->common.aml_opcode == AML_REGION_OP) {
  394. status =
  395. acpi_ex_create_region(op->named.data,
  396. op->named.length,
  397. (acpi_adr_space_type) ((op->
  398. common.
  399. value.
  400. arg)->
  401. common.
  402. value.
  403. integer),
  404. walk_state);
  405. if (ACPI_FAILURE(status)) {
  406. return_ACPI_STATUS(status);
  407. }
  408. } else if (op->common.aml_opcode == AML_DATA_REGION_OP) {
  409. status =
  410. acpi_ex_create_region(op->named.data,
  411. op->named.length,
  412. ACPI_ADR_SPACE_DATA_TABLE,
  413. walk_state);
  414. if (ACPI_FAILURE(status)) {
  415. return_ACPI_STATUS(status);
  416. }
  417. }
  418. }
  419. #endif
  420. if (op->common.aml_opcode == AML_NAME_OP) {
  421. /* For Name opcode, get the object type from the argument */
  422. if (op->common.value.arg) {
  423. object_type = (acpi_ps_get_opcode_info((op->common.
  424. value.arg)->
  425. common.
  426. aml_opcode))->
  427. object_type;
  428. /* Set node type if we have a namespace node */
  429. if (op->common.node) {
  430. op->common.node->type = (u8) object_type;
  431. }
  432. }
  433. }
  434. /*
  435. * If we are executing a method, do not create any namespace objects
  436. * during the load phase, only during execution.
  437. */
  438. if (!walk_state->method_node) {
  439. if (op->common.aml_opcode == AML_METHOD_OP) {
  440. /*
  441. * method_op pkg_length name_string method_flags term_list
  442. *
  443. * Note: We must create the method node/object pair as soon as we
  444. * see the method declaration. This allows later pass1 parsing
  445. * of invocations of the method (need to know the number of
  446. * arguments.)
  447. */
  448. ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
  449. "LOADING-Method: State=%p Op=%p NamedObj=%p\n",
  450. walk_state, op, op->named.node));
  451. if (!acpi_ns_get_attached_object(op->named.node)) {
  452. walk_state->operands[0] =
  453. ACPI_CAST_PTR(void, op->named.node);
  454. walk_state->num_operands = 1;
  455. status =
  456. acpi_ds_create_operands(walk_state,
  457. op->common.value.
  458. arg);
  459. if (ACPI_SUCCESS(status)) {
  460. status =
  461. acpi_ex_create_method(op->named.
  462. data,
  463. op->named.
  464. length,
  465. walk_state);
  466. }
  467. walk_state->operands[0] = NULL;
  468. walk_state->num_operands = 0;
  469. if (ACPI_FAILURE(status)) {
  470. return_ACPI_STATUS(status);
  471. }
  472. }
  473. }
  474. }
  475. /* Pop the scope stack (only if loading a table) */
  476. if (!walk_state->method_node && acpi_ns_opens_scope(object_type)) {
  477. ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
  478. "(%s): Popping scope for Op %p\n",
  479. acpi_ut_get_type_name(object_type), op));
  480. status = acpi_ds_scope_stack_pop(walk_state);
  481. }
  482. return_ACPI_STATUS(status);
  483. }