1 /****************************************************************************** 2 * 3 * Module Name: dscontrol - Support for execution control opcodes - 4 * if/else/while/return 5 * 6 *****************************************************************************/ 7 8 /* 9 * Copyright (C) 2000 - 2018, Intel Corp. 10 * All rights reserved. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions, and the following disclaimer, 17 * without modification. 18 * 2. Redistributions in binary form must reproduce at minimum a disclaimer 19 * substantially similar to the "NO WARRANTY" disclaimer below 20 * ("Disclaimer") and any redistribution must be conditioned upon 21 * including a substantially similar Disclaimer requirement for further 22 * binary redistribution. 23 * 3. Neither the names of the above-listed copyright holders nor the names 24 * of any contributors may be used to endorse or promote products derived 25 * from this software without specific prior written permission. 26 * 27 * Alternatively, this software may be distributed under the terms of the 28 * GNU General Public License ("GPL") version 2 as published by the Free 29 * Software Foundation. 30 * 31 * NO WARRANTY 32 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 33 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 34 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR 35 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 36 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 40 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 41 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 42 * POSSIBILITY OF SUCH DAMAGES. 43 */ 44 45 #include <acpi/acpi.h> 46 #include "accommon.h" 47 #include "amlcode.h" 48 #include "acdispat.h" 49 #include "acinterp.h" 50 #include "acdebug.h" 51 52 #define _COMPONENT ACPI_DISPATCHER 53 ACPI_MODULE_NAME("dscontrol") 54 55 /******************************************************************************* 56 * 57 * FUNCTION: acpi_ds_exec_begin_control_op 58 * 59 * PARAMETERS: walk_list - The list that owns the walk stack 60 * op - The control Op 61 * 62 * RETURN: Status 63 * 64 * DESCRIPTION: Handles all control ops encountered during control method 65 * execution. 66 * 67 ******************************************************************************/ 68 acpi_status 69 acpi_ds_exec_begin_control_op(struct acpi_walk_state *walk_state, 70 union acpi_parse_object *op) 71 { 72 acpi_status status = AE_OK; 73 union acpi_generic_state *control_state; 74 75 ACPI_FUNCTION_NAME(ds_exec_begin_control_op); 76 77 ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH, "Op=%p Opcode=%2.2X State=%p\n", 78 op, op->common.aml_opcode, walk_state)); 79 80 switch (op->common.aml_opcode) { 81 case AML_WHILE_OP: 82 /* 83 * If this is an additional iteration of a while loop, continue. 84 * There is no need to allocate a new control state. 85 */ 86 if (walk_state->control_state) { 87 if (walk_state->control_state->control. 88 aml_predicate_start == 89 (walk_state->parser_state.aml - 1)) { 90 91 /* Reset the state to start-of-loop */ 92 93 walk_state->control_state->common.state = 94 ACPI_CONTROL_CONDITIONAL_EXECUTING; 95 break; 96 } 97 } 98 99 /*lint -fallthrough */ 100 101 case AML_IF_OP: 102 /* 103 * IF/WHILE: Create a new control state to manage these 104 * constructs. We need to manage these as a stack, in order 105 * to handle nesting. 106 */ 107 control_state = acpi_ut_create_control_state(); 108 if (!control_state) { 109 status = AE_NO_MEMORY; 110 break; 111 } 112 /* 113 * Save a pointer to the predicate for multiple executions 114 * of a loop 115 */ 116 control_state->control.aml_predicate_start = 117 walk_state->parser_state.aml - 1; 118 control_state->control.package_end = 119 walk_state->parser_state.pkg_end; 120 control_state->control.opcode = op->common.aml_opcode; 121 control_state->control.loop_timeout = acpi_os_get_timer() + 122 (u64)(acpi_gbl_max_loop_iterations * ACPI_100NSEC_PER_SEC); 123 124 /* Push the control state on this walk's control stack */ 125 126 acpi_ut_push_generic_state(&walk_state->control_state, 127 control_state); 128 break; 129 130 case AML_ELSE_OP: 131 132 /* Predicate is in the state object */ 133 /* If predicate is true, the IF was executed, ignore ELSE part */ 134 135 if (walk_state->last_predicate) { 136 status = AE_CTRL_TRUE; 137 } 138 139 break; 140 141 case AML_RETURN_OP: 142 143 break; 144 145 default: 146 147 break; 148 } 149 150 return (status); 151 } 152 153 /******************************************************************************* 154 * 155 * FUNCTION: acpi_ds_exec_end_control_op 156 * 157 * PARAMETERS: walk_list - The list that owns the walk stack 158 * op - The control Op 159 * 160 * RETURN: Status 161 * 162 * DESCRIPTION: Handles all control ops encountered during control method 163 * execution. 164 * 165 ******************************************************************************/ 166 167 acpi_status 168 acpi_ds_exec_end_control_op(struct acpi_walk_state *walk_state, 169 union acpi_parse_object *op) 170 { 171 acpi_status status = AE_OK; 172 union acpi_generic_state *control_state; 173 174 ACPI_FUNCTION_NAME(ds_exec_end_control_op); 175 176 switch (op->common.aml_opcode) { 177 case AML_IF_OP: 178 179 ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH, "[IF_OP] Op=%p\n", op)); 180 181 /* 182 * Save the result of the predicate in case there is an 183 * ELSE to come 184 */ 185 walk_state->last_predicate = 186 (u8)walk_state->control_state->common.value; 187 188 /* 189 * Pop the control state that was created at the start 190 * of the IF and free it 191 */ 192 control_state = 193 acpi_ut_pop_generic_state(&walk_state->control_state); 194 acpi_ut_delete_generic_state(control_state); 195 break; 196 197 case AML_ELSE_OP: 198 199 break; 200 201 case AML_WHILE_OP: 202 203 ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH, "[WHILE_OP] Op=%p\n", op)); 204 205 control_state = walk_state->control_state; 206 if (control_state->common.value) { 207 208 /* Predicate was true, the body of the loop was just executed */ 209 210 /* 211 * This infinite loop detection mechanism allows the interpreter 212 * to escape possibly infinite loops. This can occur in poorly 213 * written AML when the hardware does not respond within a while 214 * loop and the loop does not implement a timeout. 215 */ 216 if (ACPI_TIME_AFTER(acpi_os_get_timer(), 217 control_state->control. 218 loop_timeout)) { 219 status = AE_AML_LOOP_TIMEOUT; 220 break; 221 } 222 223 /* 224 * Go back and evaluate the predicate and maybe execute the loop 225 * another time 226 */ 227 status = AE_CTRL_PENDING; 228 walk_state->aml_last_while = 229 control_state->control.aml_predicate_start; 230 break; 231 } 232 233 /* Predicate was false, terminate this while loop */ 234 235 ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH, 236 "[WHILE_OP] termination! Op=%p\n", op)); 237 238 /* Pop this control state and free it */ 239 240 control_state = 241 acpi_ut_pop_generic_state(&walk_state->control_state); 242 acpi_ut_delete_generic_state(control_state); 243 break; 244 245 case AML_RETURN_OP: 246 247 ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH, 248 "[RETURN_OP] Op=%p Arg=%p\n", op, 249 op->common.value.arg)); 250 251 /* 252 * One optional operand -- the return value 253 * It can be either an immediate operand or a result that 254 * has been bubbled up the tree 255 */ 256 if (op->common.value.arg) { 257 258 /* Since we have a real Return(), delete any implicit return */ 259 260 acpi_ds_clear_implicit_return(walk_state); 261 262 /* Return statement has an immediate operand */ 263 264 status = 265 acpi_ds_create_operands(walk_state, 266 op->common.value.arg); 267 if (ACPI_FAILURE(status)) { 268 return (status); 269 } 270 271 /* 272 * If value being returned is a Reference (such as 273 * an arg or local), resolve it now because it may 274 * cease to exist at the end of the method. 275 */ 276 status = 277 acpi_ex_resolve_to_value(&walk_state->operands[0], 278 walk_state); 279 if (ACPI_FAILURE(status)) { 280 return (status); 281 } 282 283 /* 284 * Get the return value and save as the last result 285 * value. This is the only place where walk_state->return_desc 286 * is set to anything other than zero! 287 */ 288 walk_state->return_desc = walk_state->operands[0]; 289 } else if (walk_state->result_count) { 290 291 /* Since we have a real Return(), delete any implicit return */ 292 293 acpi_ds_clear_implicit_return(walk_state); 294 295 /* 296 * The return value has come from a previous calculation. 297 * 298 * If value being returned is a Reference (such as 299 * an arg or local), resolve it now because it may 300 * cease to exist at the end of the method. 301 * 302 * Allow references created by the Index operator to return 303 * unchanged. 304 */ 305 if ((ACPI_GET_DESCRIPTOR_TYPE 306 (walk_state->results->results.obj_desc[0]) == 307 ACPI_DESC_TYPE_OPERAND) 308 && ((walk_state->results->results.obj_desc[0])-> 309 common.type == ACPI_TYPE_LOCAL_REFERENCE) 310 && ((walk_state->results->results.obj_desc[0])-> 311 reference.class != ACPI_REFCLASS_INDEX)) { 312 status = 313 acpi_ex_resolve_to_value(&walk_state-> 314 results->results. 315 obj_desc[0], 316 walk_state); 317 if (ACPI_FAILURE(status)) { 318 return (status); 319 } 320 } 321 322 walk_state->return_desc = 323 walk_state->results->results.obj_desc[0]; 324 } else { 325 /* No return operand */ 326 327 if (walk_state->num_operands) { 328 acpi_ut_remove_reference(walk_state-> 329 operands[0]); 330 } 331 332 walk_state->operands[0] = NULL; 333 walk_state->num_operands = 0; 334 walk_state->return_desc = NULL; 335 } 336 337 ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH, 338 "Completed RETURN_OP State=%p, RetVal=%p\n", 339 walk_state, walk_state->return_desc)); 340 341 /* End the control method execution right now */ 342 343 status = AE_CTRL_TERMINATE; 344 break; 345 346 case AML_NOOP_OP: 347 348 /* Just do nothing! */ 349 350 break; 351 352 case AML_BREAKPOINT_OP: 353 354 acpi_db_signal_break_point(walk_state); 355 356 /* Call to the OSL in case OS wants a piece of the action */ 357 358 status = acpi_os_signal(ACPI_SIGNAL_BREAKPOINT, 359 "Executed AML Breakpoint opcode"); 360 break; 361 362 case AML_BREAK_OP: 363 case AML_CONTINUE_OP: /* ACPI 2.0 */ 364 365 /* Pop and delete control states until we find a while */ 366 367 while (walk_state->control_state && 368 (walk_state->control_state->control.opcode != 369 AML_WHILE_OP)) { 370 control_state = 371 acpi_ut_pop_generic_state(&walk_state-> 372 control_state); 373 acpi_ut_delete_generic_state(control_state); 374 } 375 376 /* No while found? */ 377 378 if (!walk_state->control_state) { 379 return (AE_AML_NO_WHILE); 380 } 381 382 /* Was: walk_state->aml_last_while = walk_state->control_state->Control.aml_predicate_start; */ 383 384 walk_state->aml_last_while = 385 walk_state->control_state->control.package_end; 386 387 /* Return status depending on opcode */ 388 389 if (op->common.aml_opcode == AML_BREAK_OP) { 390 status = AE_CTRL_BREAK; 391 } else { 392 status = AE_CTRL_CONTINUE; 393 } 394 break; 395 396 default: 397 398 ACPI_ERROR((AE_INFO, "Unknown control opcode=0x%X Op=%p", 399 op->common.aml_opcode, op)); 400 401 status = AE_AML_BAD_OPCODE; 402 break; 403 } 404 405 return (status); 406 } 407