dswload.c 15 KB

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