exconfig.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. /******************************************************************************
  2. *
  3. * Module Name: exconfig - Namespace reconfiguration (Load/Unload opcodes)
  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 "acinterp.h"
  45. #include "acnamesp.h"
  46. #include "actables.h"
  47. #include "acdispat.h"
  48. #include "acevents.h"
  49. #include "amlcode.h"
  50. #define _COMPONENT ACPI_EXECUTER
  51. ACPI_MODULE_NAME("exconfig")
  52. /* Local prototypes */
  53. static acpi_status
  54. acpi_ex_add_table(u32 table_index, union acpi_operand_object **ddb_handle);
  55. static acpi_status
  56. acpi_ex_region_read(union acpi_operand_object *obj_desc,
  57. u32 length, u8 *buffer);
  58. /*******************************************************************************
  59. *
  60. * FUNCTION: acpi_ex_add_table
  61. *
  62. * PARAMETERS: table - Pointer to raw table
  63. * parent_node - Where to load the table (scope)
  64. * ddb_handle - Where to return the table handle.
  65. *
  66. * RETURN: Status
  67. *
  68. * DESCRIPTION: Common function to Install and Load an ACPI table with a
  69. * returned table handle.
  70. *
  71. ******************************************************************************/
  72. static acpi_status
  73. acpi_ex_add_table(u32 table_index, union acpi_operand_object **ddb_handle)
  74. {
  75. union acpi_operand_object *obj_desc;
  76. ACPI_FUNCTION_TRACE(ex_add_table);
  77. /* Create an object to be the table handle */
  78. obj_desc = acpi_ut_create_internal_object(ACPI_TYPE_LOCAL_REFERENCE);
  79. if (!obj_desc) {
  80. return_ACPI_STATUS(AE_NO_MEMORY);
  81. }
  82. /* Init the table handle */
  83. obj_desc->common.flags |= AOPOBJ_DATA_VALID;
  84. obj_desc->reference.class = ACPI_REFCLASS_TABLE;
  85. obj_desc->reference.value = table_index;
  86. *ddb_handle = obj_desc;
  87. return_ACPI_STATUS(AE_OK);
  88. }
  89. /*******************************************************************************
  90. *
  91. * FUNCTION: acpi_ex_load_table_op
  92. *
  93. * PARAMETERS: walk_state - Current state with operands
  94. * return_desc - Where to store the return object
  95. *
  96. * RETURN: Status
  97. *
  98. * DESCRIPTION: Load an ACPI table from the RSDT/XSDT
  99. *
  100. ******************************************************************************/
  101. acpi_status
  102. acpi_ex_load_table_op(struct acpi_walk_state *walk_state,
  103. union acpi_operand_object **return_desc)
  104. {
  105. acpi_status status;
  106. union acpi_operand_object **operand = &walk_state->operands[0];
  107. struct acpi_namespace_node *parent_node;
  108. struct acpi_namespace_node *start_node;
  109. struct acpi_namespace_node *parameter_node = NULL;
  110. union acpi_operand_object *ddb_handle;
  111. u32 table_index;
  112. ACPI_FUNCTION_TRACE(ex_load_table_op);
  113. /* Find the ACPI table in the RSDT/XSDT */
  114. acpi_ex_exit_interpreter();
  115. status = acpi_tb_find_table(operand[0]->string.pointer,
  116. operand[1]->string.pointer,
  117. operand[2]->string.pointer, &table_index);
  118. acpi_ex_enter_interpreter();
  119. if (ACPI_FAILURE(status)) {
  120. if (status != AE_NOT_FOUND) {
  121. return_ACPI_STATUS(status);
  122. }
  123. /* Table not found, return an Integer=0 and AE_OK */
  124. ddb_handle = acpi_ut_create_integer_object((u64) 0);
  125. if (!ddb_handle) {
  126. return_ACPI_STATUS(AE_NO_MEMORY);
  127. }
  128. *return_desc = ddb_handle;
  129. return_ACPI_STATUS(AE_OK);
  130. }
  131. /* Default nodes */
  132. start_node = walk_state->scope_info->scope.node;
  133. parent_node = acpi_gbl_root_node;
  134. /* root_path (optional parameter) */
  135. if (operand[3]->string.length > 0) {
  136. /*
  137. * Find the node referenced by the root_path_string. This is the
  138. * location within the namespace where the table will be loaded.
  139. */
  140. status = acpi_ns_get_node_unlocked(start_node,
  141. operand[3]->string.pointer,
  142. ACPI_NS_SEARCH_PARENT,
  143. &parent_node);
  144. if (ACPI_FAILURE(status)) {
  145. return_ACPI_STATUS(status);
  146. }
  147. }
  148. /* parameter_path (optional parameter) */
  149. if (operand[4]->string.length > 0) {
  150. if ((operand[4]->string.pointer[0] != AML_ROOT_PREFIX) &&
  151. (operand[4]->string.pointer[0] != AML_PARENT_PREFIX)) {
  152. /*
  153. * Path is not absolute, so it will be relative to the node
  154. * referenced by the root_path_string (or the NS root if omitted)
  155. */
  156. start_node = parent_node;
  157. }
  158. /* Find the node referenced by the parameter_path_string */
  159. status = acpi_ns_get_node_unlocked(start_node,
  160. operand[4]->string.pointer,
  161. ACPI_NS_SEARCH_PARENT,
  162. &parameter_node);
  163. if (ACPI_FAILURE(status)) {
  164. return_ACPI_STATUS(status);
  165. }
  166. }
  167. /* Load the table into the namespace */
  168. ACPI_INFO(("Dynamic OEM Table Load:"));
  169. acpi_ex_exit_interpreter();
  170. status = acpi_tb_load_table(table_index, parent_node);
  171. acpi_ex_enter_interpreter();
  172. if (ACPI_FAILURE(status)) {
  173. return_ACPI_STATUS(status);
  174. }
  175. status = acpi_ex_add_table(table_index, &ddb_handle);
  176. if (ACPI_FAILURE(status)) {
  177. return_ACPI_STATUS(status);
  178. }
  179. /* Parameter Data (optional) */
  180. if (parameter_node) {
  181. /* Store the parameter data into the optional parameter object */
  182. status = acpi_ex_store(operand[5],
  183. ACPI_CAST_PTR(union acpi_operand_object,
  184. parameter_node),
  185. walk_state);
  186. if (ACPI_FAILURE(status)) {
  187. (void)acpi_ex_unload_table(ddb_handle);
  188. acpi_ut_remove_reference(ddb_handle);
  189. return_ACPI_STATUS(status);
  190. }
  191. }
  192. *return_desc = ddb_handle;
  193. return_ACPI_STATUS(status);
  194. }
  195. /*******************************************************************************
  196. *
  197. * FUNCTION: acpi_ex_region_read
  198. *
  199. * PARAMETERS: obj_desc - Region descriptor
  200. * length - Number of bytes to read
  201. * buffer - Pointer to where to put the data
  202. *
  203. * RETURN: Status
  204. *
  205. * DESCRIPTION: Read data from an operation region. The read starts from the
  206. * beginning of the region.
  207. *
  208. ******************************************************************************/
  209. static acpi_status
  210. acpi_ex_region_read(union acpi_operand_object *obj_desc, u32 length, u8 *buffer)
  211. {
  212. acpi_status status;
  213. u64 value;
  214. u32 region_offset = 0;
  215. u32 i;
  216. /* Bytewise reads */
  217. for (i = 0; i < length; i++) {
  218. status =
  219. acpi_ev_address_space_dispatch(obj_desc, NULL, ACPI_READ,
  220. region_offset, 8, &value);
  221. if (ACPI_FAILURE(status)) {
  222. return (status);
  223. }
  224. *buffer = (u8)value;
  225. buffer++;
  226. region_offset++;
  227. }
  228. return (AE_OK);
  229. }
  230. /*******************************************************************************
  231. *
  232. * FUNCTION: acpi_ex_load_op
  233. *
  234. * PARAMETERS: obj_desc - Region or Buffer/Field where the table will be
  235. * obtained
  236. * target - Where a handle to the table will be stored
  237. * walk_state - Current state
  238. *
  239. * RETURN: Status
  240. *
  241. * DESCRIPTION: Load an ACPI table from a field or operation region
  242. *
  243. * NOTE: Region Fields (Field, bank_field, index_fields) are resolved to buffer
  244. * objects before this code is reached.
  245. *
  246. * If source is an operation region, it must refer to system_memory, as
  247. * per the ACPI specification.
  248. *
  249. ******************************************************************************/
  250. acpi_status
  251. acpi_ex_load_op(union acpi_operand_object *obj_desc,
  252. union acpi_operand_object *target,
  253. struct acpi_walk_state *walk_state)
  254. {
  255. union acpi_operand_object *ddb_handle;
  256. struct acpi_table_header *table_header;
  257. struct acpi_table_header *table;
  258. u32 table_index;
  259. acpi_status status;
  260. u32 length;
  261. ACPI_FUNCTION_TRACE(ex_load_op);
  262. /* Source Object can be either an op_region or a Buffer/Field */
  263. switch (obj_desc->common.type) {
  264. case ACPI_TYPE_REGION:
  265. ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
  266. "Load table from Region %p\n", obj_desc));
  267. /* Region must be system_memory (from ACPI spec) */
  268. if (obj_desc->region.space_id != ACPI_ADR_SPACE_SYSTEM_MEMORY) {
  269. return_ACPI_STATUS(AE_AML_OPERAND_TYPE);
  270. }
  271. /*
  272. * If the Region Address and Length have not been previously
  273. * evaluated, evaluate them now and save the results.
  274. */
  275. if (!(obj_desc->common.flags & AOPOBJ_DATA_VALID)) {
  276. status = acpi_ds_get_region_arguments(obj_desc);
  277. if (ACPI_FAILURE(status)) {
  278. return_ACPI_STATUS(status);
  279. }
  280. }
  281. /* Get the table header first so we can get the table length */
  282. table_header = ACPI_ALLOCATE(sizeof(struct acpi_table_header));
  283. if (!table_header) {
  284. return_ACPI_STATUS(AE_NO_MEMORY);
  285. }
  286. status =
  287. acpi_ex_region_read(obj_desc,
  288. sizeof(struct acpi_table_header),
  289. ACPI_CAST_PTR(u8, table_header));
  290. length = table_header->length;
  291. ACPI_FREE(table_header);
  292. if (ACPI_FAILURE(status)) {
  293. return_ACPI_STATUS(status);
  294. }
  295. /* Must have at least an ACPI table header */
  296. if (length < sizeof(struct acpi_table_header)) {
  297. return_ACPI_STATUS(AE_INVALID_TABLE_LENGTH);
  298. }
  299. /*
  300. * The original implementation simply mapped the table, with no copy.
  301. * However, the memory region is not guaranteed to remain stable and
  302. * we must copy the table to a local buffer. For example, the memory
  303. * region is corrupted after suspend on some machines. Dynamically
  304. * loaded tables are usually small, so this overhead is minimal.
  305. *
  306. * The latest implementation (5/2009) does not use a mapping at all.
  307. * We use the low-level operation region interface to read the table
  308. * instead of the obvious optimization of using a direct mapping.
  309. * This maintains a consistent use of operation regions across the
  310. * entire subsystem. This is important if additional processing must
  311. * be performed in the (possibly user-installed) operation region
  312. * handler. For example, acpi_exec and ASLTS depend on this.
  313. */
  314. /* Allocate a buffer for the table */
  315. table = ACPI_ALLOCATE(length);
  316. if (!table) {
  317. return_ACPI_STATUS(AE_NO_MEMORY);
  318. }
  319. /* Read the entire table */
  320. status = acpi_ex_region_read(obj_desc, length,
  321. ACPI_CAST_PTR(u8, table));
  322. if (ACPI_FAILURE(status)) {
  323. ACPI_FREE(table);
  324. return_ACPI_STATUS(status);
  325. }
  326. break;
  327. case ACPI_TYPE_BUFFER: /* Buffer or resolved region_field */
  328. ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
  329. "Load table from Buffer or Field %p\n",
  330. obj_desc));
  331. /* Must have at least an ACPI table header */
  332. if (obj_desc->buffer.length < sizeof(struct acpi_table_header)) {
  333. return_ACPI_STATUS(AE_INVALID_TABLE_LENGTH);
  334. }
  335. /* Get the actual table length from the table header */
  336. table_header =
  337. ACPI_CAST_PTR(struct acpi_table_header,
  338. obj_desc->buffer.pointer);
  339. length = table_header->length;
  340. /* Table cannot extend beyond the buffer */
  341. if (length > obj_desc->buffer.length) {
  342. return_ACPI_STATUS(AE_AML_BUFFER_LIMIT);
  343. }
  344. if (length < sizeof(struct acpi_table_header)) {
  345. return_ACPI_STATUS(AE_INVALID_TABLE_LENGTH);
  346. }
  347. /*
  348. * Copy the table from the buffer because the buffer could be
  349. * modified or even deleted in the future
  350. */
  351. table = ACPI_ALLOCATE(length);
  352. if (!table) {
  353. return_ACPI_STATUS(AE_NO_MEMORY);
  354. }
  355. memcpy(table, table_header, length);
  356. break;
  357. default:
  358. return_ACPI_STATUS(AE_AML_OPERAND_TYPE);
  359. }
  360. /* Install the new table into the local data structures */
  361. ACPI_INFO(("Dynamic OEM Table Load:"));
  362. acpi_ex_exit_interpreter();
  363. status =
  364. acpi_tb_install_and_load_table(table, ACPI_PTR_TO_PHYSADDR(table),
  365. ACPI_TABLE_ORIGIN_INTERNAL_VIRTUAL,
  366. TRUE, &table_index);
  367. acpi_ex_enter_interpreter();
  368. if (ACPI_FAILURE(status)) {
  369. /* Delete allocated table buffer */
  370. ACPI_FREE(table);
  371. return_ACPI_STATUS(status);
  372. }
  373. /*
  374. * Add the table to the namespace.
  375. *
  376. * Note: Load the table objects relative to the root of the namespace.
  377. * This appears to go against the ACPI specification, but we do it for
  378. * compatibility with other ACPI implementations.
  379. */
  380. status = acpi_ex_add_table(table_index, &ddb_handle);
  381. if (ACPI_FAILURE(status)) {
  382. /* On error, table_ptr was deallocated above */
  383. return_ACPI_STATUS(status);
  384. }
  385. /* Store the ddb_handle into the Target operand */
  386. status = acpi_ex_store(ddb_handle, target, walk_state);
  387. if (ACPI_FAILURE(status)) {
  388. (void)acpi_ex_unload_table(ddb_handle);
  389. /* table_ptr was deallocated above */
  390. acpi_ut_remove_reference(ddb_handle);
  391. return_ACPI_STATUS(status);
  392. }
  393. /* Remove the reference by added by acpi_ex_store above */
  394. acpi_ut_remove_reference(ddb_handle);
  395. return_ACPI_STATUS(status);
  396. }
  397. /*******************************************************************************
  398. *
  399. * FUNCTION: acpi_ex_unload_table
  400. *
  401. * PARAMETERS: ddb_handle - Handle to a previously loaded table
  402. *
  403. * RETURN: Status
  404. *
  405. * DESCRIPTION: Unload an ACPI table
  406. *
  407. ******************************************************************************/
  408. acpi_status acpi_ex_unload_table(union acpi_operand_object *ddb_handle)
  409. {
  410. acpi_status status = AE_OK;
  411. union acpi_operand_object *table_desc = ddb_handle;
  412. u32 table_index;
  413. struct acpi_table_header *table;
  414. ACPI_FUNCTION_TRACE(ex_unload_table);
  415. /*
  416. * Temporarily emit a warning so that the ASL for the machine can be
  417. * hopefully obtained. This is to say that the Unload() operator is
  418. * extremely rare if not completely unused.
  419. */
  420. ACPI_WARNING((AE_INFO, "Received request to unload an ACPI table"));
  421. /*
  422. * Validate the handle
  423. * Although the handle is partially validated in acpi_ex_reconfiguration()
  424. * when it calls acpi_ex_resolve_operands(), the handle is more completely
  425. * validated here.
  426. *
  427. * Handle must be a valid operand object of type reference. Also, the
  428. * ddb_handle must still be marked valid (table has not been previously
  429. * unloaded)
  430. */
  431. if ((!ddb_handle) ||
  432. (ACPI_GET_DESCRIPTOR_TYPE(ddb_handle) != ACPI_DESC_TYPE_OPERAND) ||
  433. (ddb_handle->common.type != ACPI_TYPE_LOCAL_REFERENCE) ||
  434. (!(ddb_handle->common.flags & AOPOBJ_DATA_VALID))) {
  435. return_ACPI_STATUS(AE_AML_OPERAND_TYPE);
  436. }
  437. /* Get the table index from the ddb_handle */
  438. table_index = table_desc->reference.value;
  439. /*
  440. * Release the interpreter lock so that the table lock won't have
  441. * strict order requirement against it.
  442. */
  443. acpi_ex_exit_interpreter();
  444. /* Ensure the table is still loaded */
  445. if (!acpi_tb_is_table_loaded(table_index)) {
  446. status = AE_NOT_EXIST;
  447. goto lock_and_exit;
  448. }
  449. /* Invoke table handler if present */
  450. if (acpi_gbl_table_handler) {
  451. status = acpi_get_table_by_index(table_index, &table);
  452. if (ACPI_SUCCESS(status)) {
  453. (void)acpi_gbl_table_handler(ACPI_TABLE_EVENT_UNLOAD,
  454. table,
  455. acpi_gbl_table_handler_context);
  456. }
  457. }
  458. /* Delete the portion of the namespace owned by this table */
  459. status = acpi_tb_delete_namespace_by_owner(table_index);
  460. if (ACPI_FAILURE(status)) {
  461. goto lock_and_exit;
  462. }
  463. (void)acpi_tb_release_owner_id(table_index);
  464. acpi_tb_set_table_loaded_flag(table_index, FALSE);
  465. lock_and_exit:
  466. /* Re-acquire the interpreter lock */
  467. acpi_ex_enter_interpreter();
  468. /*
  469. * Invalidate the handle. We do this because the handle may be stored
  470. * in a named object and may not be actually deleted until much later.
  471. */
  472. if (ACPI_SUCCESS(status)) {
  473. ddb_handle->common.flags &= ~AOPOBJ_DATA_VALID;
  474. }
  475. return_ACPI_STATUS(status);
  476. }