exstore.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. /******************************************************************************
  2. *
  3. * Module Name: exstore - AML Interpreter object store support
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2019, 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.h"
  43. #include "accommon.h"
  44. #include "acdispat.h"
  45. #include "acinterp.h"
  46. #include "amlcode.h"
  47. #include "acnamesp.h"
  48. #define _COMPONENT ACPI_EXECUTER
  49. ACPI_MODULE_NAME ("exstore")
  50. /* Local prototypes */
  51. static ACPI_STATUS
  52. AcpiExStoreObjectToIndex (
  53. ACPI_OPERAND_OBJECT *ValDesc,
  54. ACPI_OPERAND_OBJECT *DestDesc,
  55. ACPI_WALK_STATE *WalkState);
  56. static ACPI_STATUS
  57. AcpiExStoreDirectToNode (
  58. ACPI_OPERAND_OBJECT *SourceDesc,
  59. ACPI_NAMESPACE_NODE *Node,
  60. ACPI_WALK_STATE *WalkState);
  61. /*******************************************************************************
  62. *
  63. * FUNCTION: AcpiExStore
  64. *
  65. * PARAMETERS: *SourceDesc - Value to be stored
  66. * *DestDesc - Where to store it. Must be an NS node
  67. * or ACPI_OPERAND_OBJECT of type
  68. * Reference;
  69. * WalkState - Current walk state
  70. *
  71. * RETURN: Status
  72. *
  73. * DESCRIPTION: Store the value described by SourceDesc into the location
  74. * described by DestDesc. Called by various interpreter
  75. * functions to store the result of an operation into
  76. * the destination operand -- not just simply the actual "Store"
  77. * ASL operator.
  78. *
  79. ******************************************************************************/
  80. ACPI_STATUS
  81. AcpiExStore (
  82. ACPI_OPERAND_OBJECT *SourceDesc,
  83. ACPI_OPERAND_OBJECT *DestDesc,
  84. ACPI_WALK_STATE *WalkState)
  85. {
  86. ACPI_STATUS Status = AE_OK;
  87. ACPI_OPERAND_OBJECT *RefDesc = DestDesc;
  88. ACPI_FUNCTION_TRACE_PTR (ExStore, DestDesc);
  89. /* Validate parameters */
  90. if (!SourceDesc || !DestDesc)
  91. {
  92. ACPI_ERROR ((AE_INFO, "Null parameter"));
  93. return_ACPI_STATUS (AE_AML_NO_OPERAND);
  94. }
  95. /* DestDesc can be either a namespace node or an ACPI object */
  96. if (ACPI_GET_DESCRIPTOR_TYPE (DestDesc) == ACPI_DESC_TYPE_NAMED)
  97. {
  98. /*
  99. * Dest is a namespace node,
  100. * Storing an object into a Named node.
  101. */
  102. Status = AcpiExStoreObjectToNode (SourceDesc,
  103. (ACPI_NAMESPACE_NODE *) DestDesc, WalkState,
  104. ACPI_IMPLICIT_CONVERSION);
  105. return_ACPI_STATUS (Status);
  106. }
  107. /* Destination object must be a Reference or a Constant object */
  108. switch (DestDesc->Common.Type)
  109. {
  110. case ACPI_TYPE_LOCAL_REFERENCE:
  111. break;
  112. case ACPI_TYPE_INTEGER:
  113. /* Allow stores to Constants -- a Noop as per ACPI spec */
  114. if (DestDesc->Common.Flags & AOPOBJ_AML_CONSTANT)
  115. {
  116. return_ACPI_STATUS (AE_OK);
  117. }
  118. /*lint -fallthrough */
  119. default:
  120. /* Destination is not a Reference object */
  121. ACPI_ERROR ((AE_INFO,
  122. "Target is not a Reference or Constant object - [%s] %p",
  123. AcpiUtGetObjectTypeName (DestDesc), DestDesc));
  124. return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
  125. }
  126. /*
  127. * Examine the Reference class. These cases are handled:
  128. *
  129. * 1) Store to Name (Change the object associated with a name)
  130. * 2) Store to an indexed area of a Buffer or Package
  131. * 3) Store to a Method Local or Arg
  132. * 4) Store to the debug object
  133. */
  134. switch (RefDesc->Reference.Class)
  135. {
  136. case ACPI_REFCLASS_REFOF:
  137. /* Storing an object into a Name "container" */
  138. Status = AcpiExStoreObjectToNode (SourceDesc,
  139. RefDesc->Reference.Object,
  140. WalkState, ACPI_IMPLICIT_CONVERSION);
  141. break;
  142. case ACPI_REFCLASS_INDEX:
  143. /* Storing to an Index (pointer into a packager or buffer) */
  144. Status = AcpiExStoreObjectToIndex (SourceDesc, RefDesc, WalkState);
  145. break;
  146. case ACPI_REFCLASS_LOCAL:
  147. case ACPI_REFCLASS_ARG:
  148. /* Store to a method local/arg */
  149. Status = AcpiDsStoreObjectToLocal (RefDesc->Reference.Class,
  150. RefDesc->Reference.Value, SourceDesc, WalkState);
  151. break;
  152. case ACPI_REFCLASS_DEBUG:
  153. /*
  154. * Storing to the Debug object causes the value stored to be
  155. * displayed and otherwise has no effect -- see ACPI Specification
  156. */
  157. ACPI_DEBUG_PRINT ((ACPI_DB_EXEC,
  158. "**** Write to Debug Object: Object %p [%s] ****:\n\n",
  159. SourceDesc, AcpiUtGetObjectTypeName (SourceDesc)));
  160. ACPI_DEBUG_OBJECT (SourceDesc, 0, 0);
  161. break;
  162. default:
  163. ACPI_ERROR ((AE_INFO, "Unknown Reference Class 0x%2.2X",
  164. RefDesc->Reference.Class));
  165. ACPI_DUMP_ENTRY (RefDesc, ACPI_LV_INFO);
  166. Status = AE_AML_INTERNAL;
  167. break;
  168. }
  169. return_ACPI_STATUS (Status);
  170. }
  171. /*******************************************************************************
  172. *
  173. * FUNCTION: AcpiExStoreObjectToIndex
  174. *
  175. * PARAMETERS: *SourceDesc - Value to be stored
  176. * *DestDesc - Named object to receive the value
  177. * WalkState - Current walk state
  178. *
  179. * RETURN: Status
  180. *
  181. * DESCRIPTION: Store the object to indexed Buffer or Package element
  182. *
  183. ******************************************************************************/
  184. static ACPI_STATUS
  185. AcpiExStoreObjectToIndex (
  186. ACPI_OPERAND_OBJECT *SourceDesc,
  187. ACPI_OPERAND_OBJECT *IndexDesc,
  188. ACPI_WALK_STATE *WalkState)
  189. {
  190. ACPI_STATUS Status = AE_OK;
  191. ACPI_OPERAND_OBJECT *ObjDesc;
  192. ACPI_OPERAND_OBJECT *NewDesc;
  193. UINT8 Value = 0;
  194. UINT32 i;
  195. ACPI_FUNCTION_TRACE (ExStoreObjectToIndex);
  196. /*
  197. * Destination must be a reference pointer, and
  198. * must point to either a buffer or a package
  199. */
  200. switch (IndexDesc->Reference.TargetType)
  201. {
  202. case ACPI_TYPE_PACKAGE:
  203. /*
  204. * Storing to a package element. Copy the object and replace
  205. * any existing object with the new object. No implicit
  206. * conversion is performed.
  207. *
  208. * The object at *(IndexDesc->Reference.Where) is the
  209. * element within the package that is to be modified.
  210. * The parent package object is at IndexDesc->Reference.Object
  211. */
  212. ObjDesc = *(IndexDesc->Reference.Where);
  213. if (SourceDesc->Common.Type == ACPI_TYPE_LOCAL_REFERENCE &&
  214. SourceDesc->Reference.Class == ACPI_REFCLASS_TABLE)
  215. {
  216. /* This is a DDBHandle, just add a reference to it */
  217. AcpiUtAddReference (SourceDesc);
  218. NewDesc = SourceDesc;
  219. }
  220. else
  221. {
  222. /* Normal object, copy it */
  223. Status = AcpiUtCopyIobjectToIobject (
  224. SourceDesc, &NewDesc, WalkState);
  225. if (ACPI_FAILURE (Status))
  226. {
  227. return_ACPI_STATUS (Status);
  228. }
  229. }
  230. if (ObjDesc)
  231. {
  232. /* Decrement reference count by the ref count of the parent package */
  233. for (i = 0;
  234. i < ((ACPI_OPERAND_OBJECT *)
  235. IndexDesc->Reference.Object)->Common.ReferenceCount;
  236. i++)
  237. {
  238. AcpiUtRemoveReference (ObjDesc);
  239. }
  240. }
  241. *(IndexDesc->Reference.Where) = NewDesc;
  242. /* Increment ref count by the ref count of the parent package-1 */
  243. for (i = 1;
  244. i < ((ACPI_OPERAND_OBJECT *)
  245. IndexDesc->Reference.Object)->Common.ReferenceCount;
  246. i++)
  247. {
  248. AcpiUtAddReference (NewDesc);
  249. }
  250. break;
  251. case ACPI_TYPE_BUFFER_FIELD:
  252. /*
  253. * Store into a Buffer or String (not actually a real BufferField)
  254. * at a location defined by an Index.
  255. *
  256. * The first 8-bit element of the source object is written to the
  257. * 8-bit Buffer location defined by the Index destination object,
  258. * according to the ACPI 2.0 specification.
  259. */
  260. /*
  261. * Make sure the target is a Buffer or String. An error should
  262. * not happen here, since the ReferenceObject was constructed
  263. * by the INDEX_OP code.
  264. */
  265. ObjDesc = IndexDesc->Reference.Object;
  266. if ((ObjDesc->Common.Type != ACPI_TYPE_BUFFER) &&
  267. (ObjDesc->Common.Type != ACPI_TYPE_STRING))
  268. {
  269. return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
  270. }
  271. /*
  272. * The assignment of the individual elements will be slightly
  273. * different for each source type.
  274. */
  275. switch (SourceDesc->Common.Type)
  276. {
  277. case ACPI_TYPE_INTEGER:
  278. /* Use the least-significant byte of the integer */
  279. Value = (UINT8) (SourceDesc->Integer.Value);
  280. break;
  281. case ACPI_TYPE_BUFFER:
  282. case ACPI_TYPE_STRING:
  283. /* Note: Takes advantage of common string/buffer fields */
  284. Value = SourceDesc->Buffer.Pointer[0];
  285. break;
  286. default:
  287. /* All other types are invalid */
  288. ACPI_ERROR ((AE_INFO,
  289. "Source must be type [Integer/Buffer/String], found [%s]",
  290. AcpiUtGetObjectTypeName (SourceDesc)));
  291. return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
  292. }
  293. /* Store the source value into the target buffer byte */
  294. ObjDesc->Buffer.Pointer[IndexDesc->Reference.Value] = Value;
  295. break;
  296. default:
  297. ACPI_ERROR ((AE_INFO,
  298. "Target is not of type [Package/BufferField]"));
  299. Status = AE_AML_TARGET_TYPE;
  300. break;
  301. }
  302. return_ACPI_STATUS (Status);
  303. }
  304. /*******************************************************************************
  305. *
  306. * FUNCTION: AcpiExStoreObjectToNode
  307. *
  308. * PARAMETERS: SourceDesc - Value to be stored
  309. * Node - Named object to receive the value
  310. * WalkState - Current walk state
  311. * ImplicitConversion - Perform implicit conversion (yes/no)
  312. *
  313. * RETURN: Status
  314. *
  315. * DESCRIPTION: Store the object to the named object.
  316. *
  317. * The assignment of an object to a named object is handled here.
  318. * The value passed in will replace the current value (if any)
  319. * with the input value.
  320. *
  321. * When storing into an object the data is converted to the
  322. * target object type then stored in the object. This means
  323. * that the target object type (for an initialized target) will
  324. * not be changed by a store operation. A CopyObject can change
  325. * the target type, however.
  326. *
  327. * The ImplicitConversion flag is set to NO/FALSE only when
  328. * storing to an ArgX -- as per the rules of the ACPI spec.
  329. *
  330. * Assumes parameters are already validated.
  331. *
  332. ******************************************************************************/
  333. ACPI_STATUS
  334. AcpiExStoreObjectToNode (
  335. ACPI_OPERAND_OBJECT *SourceDesc,
  336. ACPI_NAMESPACE_NODE *Node,
  337. ACPI_WALK_STATE *WalkState,
  338. UINT8 ImplicitConversion)
  339. {
  340. ACPI_STATUS Status = AE_OK;
  341. ACPI_OPERAND_OBJECT *TargetDesc;
  342. ACPI_OPERAND_OBJECT *NewDesc;
  343. ACPI_OBJECT_TYPE TargetType;
  344. ACPI_FUNCTION_TRACE_PTR (ExStoreObjectToNode, SourceDesc);
  345. /* Get current type of the node, and object attached to Node */
  346. TargetType = AcpiNsGetType (Node);
  347. TargetDesc = AcpiNsGetAttachedObject (Node);
  348. ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "Storing %p [%s] to node %p [%s]\n",
  349. SourceDesc, AcpiUtGetObjectTypeName (SourceDesc),
  350. Node, AcpiUtGetTypeName (TargetType)));
  351. /* Only limited target types possible for everything except CopyObject */
  352. if (WalkState->Opcode != AML_COPY_OBJECT_OP)
  353. {
  354. /*
  355. * Only CopyObject allows all object types to be overwritten. For
  356. * TargetRef(s), there are restrictions on the object types that
  357. * are allowed.
  358. *
  359. * Allowable operations/typing for Store:
  360. *
  361. * 1) Simple Store
  362. * Integer --> Integer (Named/Local/Arg)
  363. * String --> String (Named/Local/Arg)
  364. * Buffer --> Buffer (Named/Local/Arg)
  365. * Package --> Package (Named/Local/Arg)
  366. *
  367. * 2) Store with implicit conversion
  368. * Integer --> String or Buffer (Named)
  369. * String --> Integer or Buffer (Named)
  370. * Buffer --> Integer or String (Named)
  371. */
  372. switch (TargetType)
  373. {
  374. case ACPI_TYPE_PACKAGE:
  375. /*
  376. * Here, can only store a package to an existing package.
  377. * Storing a package to a Local/Arg is OK, and handled
  378. * elsewhere.
  379. */
  380. if (WalkState->Opcode == AML_STORE_OP)
  381. {
  382. if (SourceDesc->Common.Type != ACPI_TYPE_PACKAGE)
  383. {
  384. ACPI_ERROR ((AE_INFO,
  385. "Cannot assign type [%s] to [Package] "
  386. "(source must be type Pkg)",
  387. AcpiUtGetObjectTypeName (SourceDesc)));
  388. return_ACPI_STATUS (AE_AML_TARGET_TYPE);
  389. }
  390. break;
  391. }
  392. /* Fallthrough */
  393. case ACPI_TYPE_DEVICE:
  394. case ACPI_TYPE_EVENT:
  395. case ACPI_TYPE_MUTEX:
  396. case ACPI_TYPE_REGION:
  397. case ACPI_TYPE_POWER:
  398. case ACPI_TYPE_PROCESSOR:
  399. case ACPI_TYPE_THERMAL:
  400. ACPI_ERROR ((AE_INFO,
  401. "Target must be [Buffer/Integer/String/Reference]"
  402. ", found [%s] (%4.4s)",
  403. AcpiUtGetTypeName (Node->Type), Node->Name.Ascii));
  404. return_ACPI_STATUS (AE_AML_TARGET_TYPE);
  405. default:
  406. break;
  407. }
  408. }
  409. /*
  410. * Resolve the source object to an actual value
  411. * (If it is a reference object)
  412. */
  413. Status = AcpiExResolveObject (&SourceDesc, TargetType, WalkState);
  414. if (ACPI_FAILURE (Status))
  415. {
  416. return_ACPI_STATUS (Status);
  417. }
  418. /* Do the actual store operation */
  419. switch (TargetType)
  420. {
  421. /*
  422. * The simple data types all support implicit source operand
  423. * conversion before the store.
  424. */
  425. case ACPI_TYPE_INTEGER:
  426. case ACPI_TYPE_STRING:
  427. case ACPI_TYPE_BUFFER:
  428. if ((WalkState->Opcode == AML_COPY_OBJECT_OP) ||
  429. !ImplicitConversion)
  430. {
  431. /*
  432. * However, CopyObject and Stores to ArgX do not perform
  433. * an implicit conversion, as per the ACPI specification.
  434. * A direct store is performed instead.
  435. */
  436. Status = AcpiExStoreDirectToNode (SourceDesc, Node, WalkState);
  437. break;
  438. }
  439. /* Store with implicit source operand conversion support */
  440. Status = AcpiExStoreObjectToObject (SourceDesc, TargetDesc,
  441. &NewDesc, WalkState);
  442. if (ACPI_FAILURE (Status))
  443. {
  444. return_ACPI_STATUS (Status);
  445. }
  446. if (NewDesc != TargetDesc)
  447. {
  448. /*
  449. * Store the new NewDesc as the new value of the Name, and set
  450. * the Name's type to that of the value being stored in it.
  451. * SourceDesc reference count is incremented by AttachObject.
  452. *
  453. * Note: This may change the type of the node if an explicit
  454. * store has been performed such that the node/object type
  455. * has been changed.
  456. */
  457. Status = AcpiNsAttachObject (
  458. Node, NewDesc, NewDesc->Common.Type);
  459. ACPI_DEBUG_PRINT ((ACPI_DB_EXEC,
  460. "Store type [%s] into [%s] via Convert/Attach\n",
  461. AcpiUtGetObjectTypeName (SourceDesc),
  462. AcpiUtGetObjectTypeName (NewDesc)));
  463. }
  464. break;
  465. case ACPI_TYPE_BUFFER_FIELD:
  466. case ACPI_TYPE_LOCAL_REGION_FIELD:
  467. case ACPI_TYPE_LOCAL_BANK_FIELD:
  468. case ACPI_TYPE_LOCAL_INDEX_FIELD:
  469. /*
  470. * For all fields, always write the source data to the target
  471. * field. Any required implicit source operand conversion is
  472. * performed in the function below as necessary. Note, field
  473. * objects must retain their original type permanently.
  474. */
  475. Status = AcpiExWriteDataToField (SourceDesc, TargetDesc,
  476. &WalkState->ResultObj);
  477. break;
  478. default:
  479. /*
  480. * CopyObject operator: No conversions for all other types.
  481. * Instead, directly store a copy of the source object.
  482. *
  483. * This is the ACPI spec-defined behavior for the CopyObject
  484. * operator. (Note, for this default case, all normal
  485. * Store/Target operations exited above with an error).
  486. */
  487. Status = AcpiExStoreDirectToNode (SourceDesc, Node, WalkState);
  488. break;
  489. }
  490. return_ACPI_STATUS (Status);
  491. }
  492. /*******************************************************************************
  493. *
  494. * FUNCTION: AcpiExStoreDirectToNode
  495. *
  496. * PARAMETERS: SourceDesc - Value to be stored
  497. * Node - Named object to receive the value
  498. * WalkState - Current walk state
  499. *
  500. * RETURN: Status
  501. *
  502. * DESCRIPTION: "Store" an object directly to a node. This involves a copy
  503. * and an attach.
  504. *
  505. ******************************************************************************/
  506. static ACPI_STATUS
  507. AcpiExStoreDirectToNode (
  508. ACPI_OPERAND_OBJECT *SourceDesc,
  509. ACPI_NAMESPACE_NODE *Node,
  510. ACPI_WALK_STATE *WalkState)
  511. {
  512. ACPI_STATUS Status;
  513. ACPI_OPERAND_OBJECT *NewDesc;
  514. ACPI_FUNCTION_TRACE (ExStoreDirectToNode);
  515. ACPI_DEBUG_PRINT ((ACPI_DB_EXEC,
  516. "Storing [%s] (%p) directly into node [%s] (%p)"
  517. " with no implicit conversion\n",
  518. AcpiUtGetObjectTypeName (SourceDesc), SourceDesc,
  519. AcpiUtGetTypeName (Node->Type), Node));
  520. /* Copy the source object to a new object */
  521. Status = AcpiUtCopyIobjectToIobject (SourceDesc, &NewDesc, WalkState);
  522. if (ACPI_FAILURE (Status))
  523. {
  524. return_ACPI_STATUS (Status);
  525. }
  526. /* Attach the new object to the node */
  527. Status = AcpiNsAttachObject (Node, NewDesc, NewDesc->Common.Type);
  528. AcpiUtRemoveReference (NewDesc);
  529. return_ACPI_STATUS (Status);
  530. }