psutils.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /******************************************************************************
  2. *
  3. * Module Name: psutils - Parser miscellaneous utilities (Parser only)
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2017, 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 "acconvert.h"
  47. #define _COMPONENT ACPI_PARSER
  48. ACPI_MODULE_NAME("psutils")
  49. /*******************************************************************************
  50. *
  51. * FUNCTION: acpi_ps_create_scope_op
  52. *
  53. * PARAMETERS: None
  54. *
  55. * RETURN: A new Scope object, null on failure
  56. *
  57. * DESCRIPTION: Create a Scope and associated namepath op with the root name
  58. *
  59. ******************************************************************************/
  60. union acpi_parse_object *acpi_ps_create_scope_op(u8 *aml)
  61. {
  62. union acpi_parse_object *scope_op;
  63. scope_op = acpi_ps_alloc_op(AML_SCOPE_OP, aml);
  64. if (!scope_op) {
  65. return (NULL);
  66. }
  67. scope_op->named.name = ACPI_ROOT_NAME;
  68. return (scope_op);
  69. }
  70. /*******************************************************************************
  71. *
  72. * FUNCTION: acpi_ps_init_op
  73. *
  74. * PARAMETERS: op - A newly allocated Op object
  75. * opcode - Opcode to store in the Op
  76. *
  77. * RETURN: None
  78. *
  79. * DESCRIPTION: Initialize a parse (Op) object
  80. *
  81. ******************************************************************************/
  82. void acpi_ps_init_op(union acpi_parse_object *op, u16 opcode)
  83. {
  84. ACPI_FUNCTION_ENTRY();
  85. op->common.descriptor_type = ACPI_DESC_TYPE_PARSER;
  86. op->common.aml_opcode = opcode;
  87. ACPI_DISASM_ONLY_MEMBERS(acpi_ut_safe_strncpy(op->common.aml_op_name,
  88. (acpi_ps_get_opcode_info
  89. (opcode))->name,
  90. sizeof(op->common.
  91. aml_op_name)));
  92. }
  93. /*******************************************************************************
  94. *
  95. * FUNCTION: acpi_ps_alloc_op
  96. *
  97. * PARAMETERS: opcode - Opcode that will be stored in the new Op
  98. * aml - Address of the opcode
  99. *
  100. * RETURN: Pointer to the new Op, null on failure
  101. *
  102. * DESCRIPTION: Allocate an acpi_op, choose op type (and thus size) based on
  103. * opcode. A cache of opcodes is available for the pure
  104. * GENERIC_OP, since this is by far the most commonly used.
  105. *
  106. ******************************************************************************/
  107. union acpi_parse_object *acpi_ps_alloc_op(u16 opcode, u8 *aml)
  108. {
  109. union acpi_parse_object *op;
  110. const struct acpi_opcode_info *op_info;
  111. u8 flags = ACPI_PARSEOP_GENERIC;
  112. ACPI_FUNCTION_ENTRY();
  113. op_info = acpi_ps_get_opcode_info(opcode);
  114. /* Determine type of parse_op required */
  115. if (op_info->flags & AML_DEFER) {
  116. flags = ACPI_PARSEOP_DEFERRED;
  117. } else if (op_info->flags & AML_NAMED) {
  118. flags = ACPI_PARSEOP_NAMED_OBJECT;
  119. } else if (opcode == AML_INT_BYTELIST_OP) {
  120. flags = ACPI_PARSEOP_BYTELIST;
  121. }
  122. /* Allocate the minimum required size object */
  123. if (flags == ACPI_PARSEOP_GENERIC) {
  124. /* The generic op (default) is by far the most common (16 to 1) */
  125. op = acpi_os_acquire_object(acpi_gbl_ps_node_cache);
  126. } else {
  127. /* Extended parseop */
  128. op = acpi_os_acquire_object(acpi_gbl_ps_node_ext_cache);
  129. }
  130. /* Initialize the Op */
  131. if (op) {
  132. acpi_ps_init_op(op, opcode);
  133. op->common.aml = aml;
  134. op->common.flags = flags;
  135. ASL_CV_CLEAR_OP_COMMENTS(op);
  136. if (opcode == AML_SCOPE_OP) {
  137. acpi_gbl_current_scope = op;
  138. }
  139. if (acpi_gbl_capture_comments) {
  140. ASL_CV_TRANSFER_COMMENTS(op);
  141. }
  142. }
  143. return (op);
  144. }
  145. /*******************************************************************************
  146. *
  147. * FUNCTION: acpi_ps_free_op
  148. *
  149. * PARAMETERS: op - Op to be freed
  150. *
  151. * RETURN: None.
  152. *
  153. * DESCRIPTION: Free an Op object. Either put it on the GENERIC_OP cache list
  154. * or actually free it.
  155. *
  156. ******************************************************************************/
  157. void acpi_ps_free_op(union acpi_parse_object *op)
  158. {
  159. ACPI_FUNCTION_NAME(ps_free_op);
  160. ASL_CV_CLEAR_OP_COMMENTS(op);
  161. if (op->common.aml_opcode == AML_INT_RETURN_VALUE_OP) {
  162. ACPI_DEBUG_PRINT((ACPI_DB_ALLOCATIONS,
  163. "Free retval op: %p\n", op));
  164. }
  165. if (op->common.flags & ACPI_PARSEOP_GENERIC) {
  166. (void)acpi_os_release_object(acpi_gbl_ps_node_cache, op);
  167. } else {
  168. (void)acpi_os_release_object(acpi_gbl_ps_node_ext_cache, op);
  169. }
  170. }
  171. /*******************************************************************************
  172. *
  173. * FUNCTION: Utility functions
  174. *
  175. * DESCRIPTION: Low level character and object functions
  176. *
  177. ******************************************************************************/
  178. /*
  179. * Is "c" a namestring lead character?
  180. */
  181. u8 acpi_ps_is_leading_char(u32 c)
  182. {
  183. return ((u8) (c == '_' || (c >= 'A' && c <= 'Z')));
  184. }
  185. /*
  186. * Get op's name (4-byte name segment) or 0 if unnamed
  187. */
  188. u32 acpi_ps_get_name(union acpi_parse_object * op)
  189. {
  190. /* The "generic" object has no name associated with it */
  191. if (op->common.flags & ACPI_PARSEOP_GENERIC) {
  192. return (0);
  193. }
  194. /* Only the "Extended" parse objects have a name */
  195. return (op->named.name);
  196. }
  197. /*
  198. * Set op's name
  199. */
  200. void acpi_ps_set_name(union acpi_parse_object *op, u32 name)
  201. {
  202. /* The "generic" object has no name associated with it */
  203. if (op->common.flags & ACPI_PARSEOP_GENERIC) {
  204. return;
  205. }
  206. op->named.name = name;
  207. }