exoparg3.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
  2. /******************************************************************************
  3. *
  4. * Module Name: exoparg3 - AML execution - opcodes with 3 arguments
  5. *
  6. * Copyright (C) 2000 - 2022, Intel Corp.
  7. *
  8. *****************************************************************************/
  9. #include <acpi/acpi.h>
  10. #include "accommon.h"
  11. #include "acinterp.h"
  12. #include "acparser.h"
  13. #include "amlcode.h"
  14. #define _COMPONENT ACPI_EXECUTER
  15. ACPI_MODULE_NAME("exoparg3")
  16. /*!
  17. * Naming convention for AML interpreter execution routines.
  18. *
  19. * The routines that begin execution of AML opcodes are named with a common
  20. * convention based upon the number of arguments, the number of target operands,
  21. * and whether or not a value is returned:
  22. *
  23. * AcpiExOpcode_xA_yT_zR
  24. *
  25. * Where:
  26. *
  27. * xA - ARGUMENTS: The number of arguments (input operands) that are
  28. * required for this opcode type (1 through 6 args).
  29. * yT - TARGETS: The number of targets (output operands) that are required
  30. * for this opcode type (0, 1, or 2 targets).
  31. * zR - RETURN VALUE: Indicates whether this opcode type returns a value
  32. * as the function return (0 or 1).
  33. *
  34. * The AcpiExOpcode* functions are called via the Dispatcher component with
  35. * fully resolved operands.
  36. !*/
  37. /*******************************************************************************
  38. *
  39. * FUNCTION: acpi_ex_opcode_3A_0T_0R
  40. *
  41. * PARAMETERS: walk_state - Current walk state
  42. *
  43. * RETURN: Status
  44. *
  45. * DESCRIPTION: Execute Triadic operator (3 operands)
  46. *
  47. ******************************************************************************/
  48. acpi_status acpi_ex_opcode_3A_0T_0R(struct acpi_walk_state *walk_state)
  49. {
  50. union acpi_operand_object **operand = &walk_state->operands[0];
  51. struct acpi_signal_fatal_info *fatal;
  52. acpi_status status = AE_OK;
  53. ACPI_FUNCTION_TRACE_STR(ex_opcode_3A_0T_0R,
  54. acpi_ps_get_opcode_name(walk_state->opcode));
  55. switch (walk_state->opcode) {
  56. case AML_FATAL_OP: /* Fatal (fatal_type fatal_code fatal_arg) */
  57. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  58. "FatalOp: Type %X Code %X Arg %X "
  59. "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n",
  60. (u32)operand[0]->integer.value,
  61. (u32)operand[1]->integer.value,
  62. (u32)operand[2]->integer.value));
  63. fatal = ACPI_ALLOCATE(sizeof(struct acpi_signal_fatal_info));
  64. if (fatal) {
  65. fatal->type = (u32) operand[0]->integer.value;
  66. fatal->code = (u32) operand[1]->integer.value;
  67. fatal->argument = (u32) operand[2]->integer.value;
  68. }
  69. /* Always signal the OS! */
  70. status = acpi_os_signal(ACPI_SIGNAL_FATAL, fatal);
  71. /* Might return while OS is shutting down, just continue */
  72. ACPI_FREE(fatal);
  73. goto cleanup;
  74. case AML_EXTERNAL_OP:
  75. /*
  76. * If the interpreter sees this opcode, just ignore it. The External
  77. * op is intended for use by disassemblers in order to properly
  78. * disassemble control method invocations. The opcode or group of
  79. * opcodes should be surrounded by an "if (0)" clause to ensure that
  80. * AML interpreters never see the opcode. Thus, something is
  81. * wrong if an external opcode ever gets here.
  82. */
  83. ACPI_ERROR((AE_INFO, "Executed External Op"));
  84. status = AE_OK;
  85. goto cleanup;
  86. default:
  87. ACPI_ERROR((AE_INFO, "Unknown AML opcode 0x%X",
  88. walk_state->opcode));
  89. status = AE_AML_BAD_OPCODE;
  90. goto cleanup;
  91. }
  92. cleanup:
  93. return_ACPI_STATUS(status);
  94. }
  95. /*******************************************************************************
  96. *
  97. * FUNCTION: acpi_ex_opcode_3A_1T_1R
  98. *
  99. * PARAMETERS: walk_state - Current walk state
  100. *
  101. * RETURN: Status
  102. *
  103. * DESCRIPTION: Execute Triadic operator (3 operands)
  104. *
  105. ******************************************************************************/
  106. acpi_status acpi_ex_opcode_3A_1T_1R(struct acpi_walk_state *walk_state)
  107. {
  108. union acpi_operand_object **operand = &walk_state->operands[0];
  109. union acpi_operand_object *return_desc = NULL;
  110. char *buffer = NULL;
  111. acpi_status status = AE_OK;
  112. u64 index;
  113. acpi_size length;
  114. ACPI_FUNCTION_TRACE_STR(ex_opcode_3A_1T_1R,
  115. acpi_ps_get_opcode_name(walk_state->opcode));
  116. switch (walk_state->opcode) {
  117. case AML_MID_OP: /* Mid (Source[0], Index[1], Length[2], Result[3]) */
  118. /*
  119. * Create the return object. The Source operand is guaranteed to be
  120. * either a String or a Buffer, so just use its type.
  121. */
  122. return_desc = acpi_ut_create_internal_object((operand[0])->
  123. common.type);
  124. if (!return_desc) {
  125. status = AE_NO_MEMORY;
  126. goto cleanup;
  127. }
  128. /* Get the Integer values from the objects */
  129. index = operand[1]->integer.value;
  130. length = (acpi_size)operand[2]->integer.value;
  131. /*
  132. * If the index is beyond the length of the String/Buffer, or if the
  133. * requested length is zero, return a zero-length String/Buffer
  134. */
  135. if (index >= operand[0]->string.length) {
  136. length = 0;
  137. }
  138. /* Truncate request if larger than the actual String/Buffer */
  139. else if ((index + length) > operand[0]->string.length) {
  140. length =
  141. (acpi_size)operand[0]->string.length -
  142. (acpi_size)index;
  143. }
  144. /* Strings always have a sub-pointer, not so for buffers */
  145. switch ((operand[0])->common.type) {
  146. case ACPI_TYPE_STRING:
  147. /* Always allocate a new buffer for the String */
  148. buffer = ACPI_ALLOCATE_ZEROED((acpi_size)length + 1);
  149. if (!buffer) {
  150. status = AE_NO_MEMORY;
  151. goto cleanup;
  152. }
  153. break;
  154. case ACPI_TYPE_BUFFER:
  155. /* If the requested length is zero, don't allocate a buffer */
  156. if (length > 0) {
  157. /* Allocate a new buffer for the Buffer */
  158. buffer = ACPI_ALLOCATE_ZEROED(length);
  159. if (!buffer) {
  160. status = AE_NO_MEMORY;
  161. goto cleanup;
  162. }
  163. }
  164. break;
  165. default: /* Should not happen */
  166. status = AE_AML_OPERAND_TYPE;
  167. goto cleanup;
  168. }
  169. if (buffer) {
  170. /* We have a buffer, copy the portion requested */
  171. memcpy(buffer,
  172. operand[0]->string.pointer + index, length);
  173. }
  174. /* Set the length of the new String/Buffer */
  175. return_desc->string.pointer = buffer;
  176. return_desc->string.length = (u32) length;
  177. /* Mark buffer initialized */
  178. return_desc->buffer.flags |= AOPOBJ_DATA_VALID;
  179. break;
  180. default:
  181. ACPI_ERROR((AE_INFO, "Unknown AML opcode 0x%X",
  182. walk_state->opcode));
  183. status = AE_AML_BAD_OPCODE;
  184. goto cleanup;
  185. }
  186. /* Store the result in the target */
  187. status = acpi_ex_store(return_desc, operand[3], walk_state);
  188. cleanup:
  189. /* Delete return object on error */
  190. if (ACPI_FAILURE(status) || walk_state->result_obj) {
  191. acpi_ut_remove_reference(return_desc);
  192. walk_state->result_obj = NULL;
  193. } else {
  194. /* Set the return object and exit */
  195. walk_state->result_obj = return_desc;
  196. }
  197. return_ACPI_STATUS(status);
  198. }