exoparg2.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  1. /******************************************************************************
  2. *
  3. * Module Name: exoparg2 - AML execution - opcodes with 2 arguments
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2015, 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 "acinterp.h"
  46. #include "acevents.h"
  47. #include "amlcode.h"
  48. #define _COMPONENT ACPI_EXECUTER
  49. ACPI_MODULE_NAME("exoparg2")
  50. /*!
  51. * Naming convention for AML interpreter execution routines.
  52. *
  53. * The routines that begin execution of AML opcodes are named with a common
  54. * convention based upon the number of arguments, the number of target operands,
  55. * and whether or not a value is returned:
  56. *
  57. * AcpiExOpcode_xA_yT_zR
  58. *
  59. * Where:
  60. *
  61. * xA - ARGUMENTS: The number of arguments (input operands) that are
  62. * required for this opcode type (1 through 6 args).
  63. * yT - TARGETS: The number of targets (output operands) that are required
  64. * for this opcode type (0, 1, or 2 targets).
  65. * zR - RETURN VALUE: Indicates whether this opcode type returns a value
  66. * as the function return (0 or 1).
  67. *
  68. * The AcpiExOpcode* functions are called via the Dispatcher component with
  69. * fully resolved operands.
  70. !*/
  71. /*******************************************************************************
  72. *
  73. * FUNCTION: acpi_ex_opcode_2A_0T_0R
  74. *
  75. * PARAMETERS: walk_state - Current walk state
  76. *
  77. * RETURN: Status
  78. *
  79. * DESCRIPTION: Execute opcode with two arguments, no target, and no return
  80. * value.
  81. *
  82. * ALLOCATION: Deletes both operands
  83. *
  84. ******************************************************************************/
  85. acpi_status acpi_ex_opcode_2A_0T_0R(struct acpi_walk_state *walk_state)
  86. {
  87. union acpi_operand_object **operand = &walk_state->operands[0];
  88. struct acpi_namespace_node *node;
  89. u32 value;
  90. acpi_status status = AE_OK;
  91. ACPI_FUNCTION_TRACE_STR(ex_opcode_2A_0T_0R,
  92. acpi_ps_get_opcode_name(walk_state->opcode));
  93. /* Examine the opcode */
  94. switch (walk_state->opcode) {
  95. case AML_NOTIFY_OP: /* Notify (notify_object, notify_value) */
  96. /* The first operand is a namespace node */
  97. node = (struct acpi_namespace_node *)operand[0];
  98. /* Second value is the notify value */
  99. value = (u32) operand[1]->integer.value;
  100. /* Are notifies allowed on this object? */
  101. if (!acpi_ev_is_notify_object(node)) {
  102. ACPI_ERROR((AE_INFO,
  103. "Unexpected notify object type [%s]",
  104. acpi_ut_get_type_name(node->type)));
  105. status = AE_AML_OPERAND_TYPE;
  106. break;
  107. }
  108. /*
  109. * Dispatch the notify to the appropriate handler
  110. * NOTE: the request is queued for execution after this method
  111. * completes. The notify handlers are NOT invoked synchronously
  112. * from this thread -- because handlers may in turn run other
  113. * control methods.
  114. */
  115. status = acpi_ev_queue_notify_request(node, value);
  116. break;
  117. default:
  118. ACPI_ERROR((AE_INFO, "Unknown AML opcode 0x%X",
  119. walk_state->opcode));
  120. status = AE_AML_BAD_OPCODE;
  121. }
  122. return_ACPI_STATUS(status);
  123. }
  124. /*******************************************************************************
  125. *
  126. * FUNCTION: acpi_ex_opcode_2A_2T_1R
  127. *
  128. * PARAMETERS: walk_state - Current walk state
  129. *
  130. * RETURN: Status
  131. *
  132. * DESCRIPTION: Execute a dyadic operator (2 operands) with 2 output targets
  133. * and one implicit return value.
  134. *
  135. ******************************************************************************/
  136. acpi_status acpi_ex_opcode_2A_2T_1R(struct acpi_walk_state *walk_state)
  137. {
  138. union acpi_operand_object **operand = &walk_state->operands[0];
  139. union acpi_operand_object *return_desc1 = NULL;
  140. union acpi_operand_object *return_desc2 = NULL;
  141. acpi_status status;
  142. ACPI_FUNCTION_TRACE_STR(ex_opcode_2A_2T_1R,
  143. acpi_ps_get_opcode_name(walk_state->opcode));
  144. /* Execute the opcode */
  145. switch (walk_state->opcode) {
  146. case AML_DIVIDE_OP:
  147. /* Divide (Dividend, Divisor, remainder_result quotient_result) */
  148. return_desc1 =
  149. acpi_ut_create_internal_object(ACPI_TYPE_INTEGER);
  150. if (!return_desc1) {
  151. status = AE_NO_MEMORY;
  152. goto cleanup;
  153. }
  154. return_desc2 =
  155. acpi_ut_create_internal_object(ACPI_TYPE_INTEGER);
  156. if (!return_desc2) {
  157. status = AE_NO_MEMORY;
  158. goto cleanup;
  159. }
  160. /* Quotient to return_desc1, remainder to return_desc2 */
  161. status = acpi_ut_divide(operand[0]->integer.value,
  162. operand[1]->integer.value,
  163. &return_desc1->integer.value,
  164. &return_desc2->integer.value);
  165. if (ACPI_FAILURE(status)) {
  166. goto cleanup;
  167. }
  168. break;
  169. default:
  170. ACPI_ERROR((AE_INFO, "Unknown AML opcode 0x%X",
  171. walk_state->opcode));
  172. status = AE_AML_BAD_OPCODE;
  173. goto cleanup;
  174. }
  175. /* Store the results to the target reference operands */
  176. status = acpi_ex_store(return_desc2, operand[2], walk_state);
  177. if (ACPI_FAILURE(status)) {
  178. goto cleanup;
  179. }
  180. status = acpi_ex_store(return_desc1, operand[3], walk_state);
  181. if (ACPI_FAILURE(status)) {
  182. goto cleanup;
  183. }
  184. cleanup:
  185. /*
  186. * Since the remainder is not returned indirectly, remove a reference to
  187. * it. Only the quotient is returned indirectly.
  188. */
  189. acpi_ut_remove_reference(return_desc2);
  190. if (ACPI_FAILURE(status)) {
  191. /* Delete the return object */
  192. acpi_ut_remove_reference(return_desc1);
  193. }
  194. /* Save return object (the remainder) on success */
  195. else {
  196. walk_state->result_obj = return_desc1;
  197. }
  198. return_ACPI_STATUS(status);
  199. }
  200. /*******************************************************************************
  201. *
  202. * FUNCTION: acpi_ex_opcode_2A_1T_1R
  203. *
  204. * PARAMETERS: walk_state - Current walk state
  205. *
  206. * RETURN: Status
  207. *
  208. * DESCRIPTION: Execute opcode with two arguments, one target, and a return
  209. * value.
  210. *
  211. ******************************************************************************/
  212. acpi_status acpi_ex_opcode_2A_1T_1R(struct acpi_walk_state *walk_state)
  213. {
  214. union acpi_operand_object **operand = &walk_state->operands[0];
  215. union acpi_operand_object *return_desc = NULL;
  216. u64 index;
  217. acpi_status status = AE_OK;
  218. acpi_size length = 0;
  219. ACPI_FUNCTION_TRACE_STR(ex_opcode_2A_1T_1R,
  220. acpi_ps_get_opcode_name(walk_state->opcode));
  221. /* Execute the opcode */
  222. if (walk_state->op_info->flags & AML_MATH) {
  223. /* All simple math opcodes (add, etc.) */
  224. return_desc = acpi_ut_create_internal_object(ACPI_TYPE_INTEGER);
  225. if (!return_desc) {
  226. status = AE_NO_MEMORY;
  227. goto cleanup;
  228. }
  229. return_desc->integer.value =
  230. acpi_ex_do_math_op(walk_state->opcode,
  231. operand[0]->integer.value,
  232. operand[1]->integer.value);
  233. goto store_result_to_target;
  234. }
  235. switch (walk_state->opcode) {
  236. case AML_MOD_OP: /* Mod (Dividend, Divisor, remainder_result (ACPI 2.0) */
  237. return_desc = acpi_ut_create_internal_object(ACPI_TYPE_INTEGER);
  238. if (!return_desc) {
  239. status = AE_NO_MEMORY;
  240. goto cleanup;
  241. }
  242. /* return_desc will contain the remainder */
  243. status = acpi_ut_divide(operand[0]->integer.value,
  244. operand[1]->integer.value,
  245. NULL, &return_desc->integer.value);
  246. break;
  247. case AML_CONCAT_OP: /* Concatenate (Data1, Data2, Result) */
  248. status = acpi_ex_do_concatenate(operand[0], operand[1],
  249. &return_desc, walk_state);
  250. break;
  251. case AML_TO_STRING_OP: /* to_string (Buffer, Length, Result) (ACPI 2.0) */
  252. /*
  253. * Input object is guaranteed to be a buffer at this point (it may have
  254. * been converted.) Copy the raw buffer data to a new object of
  255. * type String.
  256. */
  257. /*
  258. * Get the length of the new string. It is the smallest of:
  259. * 1) Length of the input buffer
  260. * 2) Max length as specified in the to_string operator
  261. * 3) Length of input buffer up to a zero byte (null terminator)
  262. *
  263. * NOTE: A length of zero is ok, and will create a zero-length, null
  264. * terminated string.
  265. */
  266. while ((length < operand[0]->buffer.length) &&
  267. (length < operand[1]->integer.value) &&
  268. (operand[0]->buffer.pointer[length])) {
  269. length++;
  270. }
  271. /* Allocate a new string object */
  272. return_desc = acpi_ut_create_string_object(length);
  273. if (!return_desc) {
  274. status = AE_NO_MEMORY;
  275. goto cleanup;
  276. }
  277. /*
  278. * Copy the raw buffer data with no transform.
  279. * (NULL terminated already)
  280. */
  281. memcpy(return_desc->string.pointer,
  282. operand[0]->buffer.pointer, length);
  283. break;
  284. case AML_CONCAT_RES_OP:
  285. /* concatenate_res_template (Buffer, Buffer, Result) (ACPI 2.0) */
  286. status = acpi_ex_concat_template(operand[0], operand[1],
  287. &return_desc, walk_state);
  288. break;
  289. case AML_INDEX_OP: /* Index (Source Index Result) */
  290. /* Create the internal return object */
  291. return_desc =
  292. acpi_ut_create_internal_object(ACPI_TYPE_LOCAL_REFERENCE);
  293. if (!return_desc) {
  294. status = AE_NO_MEMORY;
  295. goto cleanup;
  296. }
  297. /* Initialize the Index reference object */
  298. index = operand[1]->integer.value;
  299. return_desc->reference.value = (u32) index;
  300. return_desc->reference.class = ACPI_REFCLASS_INDEX;
  301. /*
  302. * At this point, the Source operand is a String, Buffer, or Package.
  303. * Verify that the index is within range.
  304. */
  305. switch ((operand[0])->common.type) {
  306. case ACPI_TYPE_STRING:
  307. if (index >= operand[0]->string.length) {
  308. length = operand[0]->string.length;
  309. status = AE_AML_STRING_LIMIT;
  310. }
  311. return_desc->reference.target_type =
  312. ACPI_TYPE_BUFFER_FIELD;
  313. return_desc->reference.index_pointer =
  314. &(operand[0]->buffer.pointer[index]);
  315. break;
  316. case ACPI_TYPE_BUFFER:
  317. if (index >= operand[0]->buffer.length) {
  318. length = operand[0]->buffer.length;
  319. status = AE_AML_BUFFER_LIMIT;
  320. }
  321. return_desc->reference.target_type =
  322. ACPI_TYPE_BUFFER_FIELD;
  323. return_desc->reference.index_pointer =
  324. &(operand[0]->buffer.pointer[index]);
  325. break;
  326. case ACPI_TYPE_PACKAGE:
  327. if (index >= operand[0]->package.count) {
  328. length = operand[0]->package.count;
  329. status = AE_AML_PACKAGE_LIMIT;
  330. }
  331. return_desc->reference.target_type = ACPI_TYPE_PACKAGE;
  332. return_desc->reference.where =
  333. &operand[0]->package.elements[index];
  334. break;
  335. default:
  336. status = AE_AML_INTERNAL;
  337. goto cleanup;
  338. }
  339. /* Failure means that the Index was beyond the end of the object */
  340. if (ACPI_FAILURE(status)) {
  341. ACPI_EXCEPTION((AE_INFO, status,
  342. "Index (0x%X%8.8X) is beyond end of object (length 0x%X)",
  343. ACPI_FORMAT_UINT64(index),
  344. (u32)length));
  345. goto cleanup;
  346. }
  347. /*
  348. * Save the target object and add a reference to it for the life
  349. * of the index
  350. */
  351. return_desc->reference.object = operand[0];
  352. acpi_ut_add_reference(operand[0]);
  353. /* Store the reference to the Target */
  354. status = acpi_ex_store(return_desc, operand[2], walk_state);
  355. /* Return the reference */
  356. walk_state->result_obj = return_desc;
  357. goto cleanup;
  358. default:
  359. ACPI_ERROR((AE_INFO, "Unknown AML opcode 0x%X",
  360. walk_state->opcode));
  361. status = AE_AML_BAD_OPCODE;
  362. break;
  363. }
  364. store_result_to_target:
  365. if (ACPI_SUCCESS(status)) {
  366. /*
  367. * Store the result of the operation (which is now in return_desc) into
  368. * the Target descriptor.
  369. */
  370. status = acpi_ex_store(return_desc, operand[2], walk_state);
  371. if (ACPI_FAILURE(status)) {
  372. goto cleanup;
  373. }
  374. if (!walk_state->result_obj) {
  375. walk_state->result_obj = return_desc;
  376. }
  377. }
  378. cleanup:
  379. /* Delete return object on error */
  380. if (ACPI_FAILURE(status)) {
  381. acpi_ut_remove_reference(return_desc);
  382. walk_state->result_obj = NULL;
  383. }
  384. return_ACPI_STATUS(status);
  385. }
  386. /*******************************************************************************
  387. *
  388. * FUNCTION: acpi_ex_opcode_2A_0T_1R
  389. *
  390. * PARAMETERS: walk_state - Current walk state
  391. *
  392. * RETURN: Status
  393. *
  394. * DESCRIPTION: Execute opcode with 2 arguments, no target, and a return value
  395. *
  396. ******************************************************************************/
  397. acpi_status acpi_ex_opcode_2A_0T_1R(struct acpi_walk_state *walk_state)
  398. {
  399. union acpi_operand_object **operand = &walk_state->operands[0];
  400. union acpi_operand_object *return_desc = NULL;
  401. acpi_status status = AE_OK;
  402. u8 logical_result = FALSE;
  403. ACPI_FUNCTION_TRACE_STR(ex_opcode_2A_0T_1R,
  404. acpi_ps_get_opcode_name(walk_state->opcode));
  405. /* Create the internal return object */
  406. return_desc = acpi_ut_create_internal_object(ACPI_TYPE_INTEGER);
  407. if (!return_desc) {
  408. status = AE_NO_MEMORY;
  409. goto cleanup;
  410. }
  411. /* Execute the Opcode */
  412. if (walk_state->op_info->flags & AML_LOGICAL_NUMERIC) {
  413. /* logical_op (Operand0, Operand1) */
  414. status = acpi_ex_do_logical_numeric_op(walk_state->opcode,
  415. operand[0]->integer.
  416. value,
  417. operand[1]->integer.
  418. value, &logical_result);
  419. goto store_logical_result;
  420. } else if (walk_state->op_info->flags & AML_LOGICAL) {
  421. /* logical_op (Operand0, Operand1) */
  422. status = acpi_ex_do_logical_op(walk_state->opcode, operand[0],
  423. operand[1], &logical_result);
  424. goto store_logical_result;
  425. }
  426. switch (walk_state->opcode) {
  427. case AML_ACQUIRE_OP: /* Acquire (mutex_object, Timeout) */
  428. status =
  429. acpi_ex_acquire_mutex(operand[1], operand[0], walk_state);
  430. if (status == AE_TIME) {
  431. logical_result = TRUE; /* TRUE = Acquire timed out */
  432. status = AE_OK;
  433. }
  434. break;
  435. case AML_WAIT_OP: /* Wait (event_object, Timeout) */
  436. status = acpi_ex_system_wait_event(operand[1], operand[0]);
  437. if (status == AE_TIME) {
  438. logical_result = TRUE; /* TRUE, Wait timed out */
  439. status = AE_OK;
  440. }
  441. break;
  442. default:
  443. ACPI_ERROR((AE_INFO, "Unknown AML opcode 0x%X",
  444. walk_state->opcode));
  445. status = AE_AML_BAD_OPCODE;
  446. goto cleanup;
  447. }
  448. store_logical_result:
  449. /*
  450. * Set return value to according to logical_result. logical TRUE (all ones)
  451. * Default is FALSE (zero)
  452. */
  453. if (logical_result) {
  454. return_desc->integer.value = ACPI_UINT64_MAX;
  455. }
  456. cleanup:
  457. /* Delete return object on error */
  458. if (ACPI_FAILURE(status)) {
  459. acpi_ut_remove_reference(return_desc);
  460. }
  461. /* Save return object on success */
  462. else {
  463. walk_state->result_obj = return_desc;
  464. }
  465. return_ACPI_STATUS(status);
  466. }