1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2000-2002 Mitsuru IWASAKI <iwasaki@FreeBSD.org> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 * 28 * $FreeBSD$ 29 */ 30 31 #include <sys/param.h> 32 #include <sys/queue.h> 33 #include <sys/mman.h> 34 #include <sys/stat.h> 35 #include <sys/stdint.h> 36 #include <sys/types.h> 37 38 #include <assert.h> 39 #include <ctype.h> 40 #include <err.h> 41 #include <fcntl.h> 42 #include <limits.h> 43 #include <stdio.h> 44 #include <stdlib.h> 45 #include <unistd.h> 46 47 #include <contrib/dev/acpica/include/acpi.h> 48 #include <contrib/dev/acpica/include/accommon.h> 49 #include <contrib/dev/acpica/include/acapps.h> 50 #include <contrib/dev/acpica/include/acdebug.h> 51 #include <contrib/dev/acpica/include/amlresrc.h> 52 53 /* 54 * Dummy DSDT Table Header 55 */ 56 57 static ACPI_TABLE_HEADER dummy_dsdt_table = { 58 "DSDT", 123, 1, 123, "OEMID", "OEMTBLID", 1, "CRID", 1 59 }; 60 61 /* 62 * Region space I/O routines on virtual machine 63 */ 64 65 static int aml_debug_prompt = 1; 66 67 struct ACPIRegionContent { 68 TAILQ_ENTRY(ACPIRegionContent) links; 69 int regtype; 70 ACPI_PHYSICAL_ADDRESS addr; 71 UINT8 value; 72 }; 73 74 TAILQ_HEAD(ACPIRegionContentList, ACPIRegionContent); 75 static struct ACPIRegionContentList RegionContentList; 76 77 static int aml_simulation_initialized = 0; 78 79 ACPI_PHYSICAL_ADDRESS AeLocalGetRootPointer(void); 80 void AeDoObjectOverrides(void); 81 void AeTableOverride(ACPI_TABLE_HEADER *, ACPI_TABLE_HEADER **); 82 83 static void aml_simulation_init(void); 84 static int aml_simulate_regcontent_add(int regtype, 85 ACPI_PHYSICAL_ADDRESS addr, 86 UINT8 value); 87 static int aml_simulate_regcontent_read(int regtype, 88 ACPI_PHYSICAL_ADDRESS addr, 89 UINT8 *valuep); 90 static int aml_simulate_regcontent_write(int regtype, 91 ACPI_PHYSICAL_ADDRESS addr, 92 UINT8 *valuep); 93 static UINT64 aml_simulate_prompt(char *msg, UINT64 def_val); 94 static void aml_simulation_regload(const char *dumpfile); 95 static void aml_simulation_regdump(const char *dumpfile); 96 97 /* Stubs to simplify linkage to the ACPICA core subsystem. */ 98 ACPI_PHYSICAL_ADDRESS 99 AcpiOsGetRootPointer(void) 100 { 101 102 return (0); 103 } 104 105 void 106 AeDoObjectOverrides(void) 107 { 108 } 109 110 void 111 AeTableOverride(ACPI_TABLE_HEADER *ExistingTable, ACPI_TABLE_HEADER **NewTable) 112 { 113 } 114 115 void 116 MpSaveGpioInfo(ACPI_PARSE_OBJECT *Op, AML_RESOURCE *Resource, 117 UINT32 PinCount, UINT16 *PinList, char *DeviceName) 118 { 119 } 120 121 void 122 MpSaveSerialInfo(ACPI_PARSE_OBJECT *Op, AML_RESOURCE *Resource, 123 char *DeviceName) 124 { 125 } 126 127 static void 128 aml_simulation_init(void) 129 { 130 131 aml_simulation_initialized = 1; 132 TAILQ_INIT(&RegionContentList); 133 aml_simulation_regload("region.ini"); 134 } 135 136 static int 137 aml_simulate_regcontent_add(int regtype, ACPI_PHYSICAL_ADDRESS addr, UINT8 value) 138 { 139 struct ACPIRegionContent *rc; 140 141 rc = malloc(sizeof(struct ACPIRegionContent)); 142 if (rc == NULL) { 143 return (-1); /* malloc fail */ 144 } 145 rc->regtype = regtype; 146 rc->addr = addr; 147 rc->value = value; 148 149 TAILQ_INSERT_TAIL(&RegionContentList, rc, links); 150 return (0); 151 } 152 153 static int 154 aml_simulate_regcontent_read(int regtype, ACPI_PHYSICAL_ADDRESS addr, UINT8 *valuep) 155 { 156 struct ACPIRegionContent *rc; 157 158 if (!aml_simulation_initialized) { 159 aml_simulation_init(); 160 } 161 TAILQ_FOREACH(rc, &RegionContentList, links) { 162 if (rc->regtype == regtype && rc->addr == addr) { 163 *valuep = rc->value; 164 return (1); /* found */ 165 } 166 } 167 168 *valuep = 0; 169 return (aml_simulate_regcontent_add(regtype, addr, *valuep)); 170 } 171 172 static int 173 aml_simulate_regcontent_write(int regtype, ACPI_PHYSICAL_ADDRESS addr, UINT8 *valuep) 174 { 175 struct ACPIRegionContent *rc; 176 177 if (!aml_simulation_initialized) { 178 aml_simulation_init(); 179 } 180 TAILQ_FOREACH(rc, &RegionContentList, links) { 181 if (rc->regtype == regtype && rc->addr == addr) { 182 rc->value = *valuep; 183 return (1); /* exists */ 184 } 185 } 186 187 return (aml_simulate_regcontent_add(regtype, addr, *valuep)); 188 } 189 190 static UINT64 191 aml_simulate_prompt(char *msg, UINT64 def_val) 192 { 193 char buf[16], *ep; 194 UINT64 val; 195 196 val = def_val; 197 printf("DEBUG"); 198 if (msg != NULL) { 199 printf("%s", msg); 200 } 201 printf("(default: 0x%jx ", (uintmax_t)val); 202 printf(" / %ju) >>", (uintmax_t)val); 203 fflush(stdout); 204 205 bzero(buf, sizeof buf); 206 while (1) { 207 if (read(0, buf, sizeof buf) == 0) { 208 continue; 209 } 210 if (buf[0] == '\n') { 211 break; /* use default value */ 212 } 213 if (buf[0] == '0' && buf[1] == 'x') { 214 val = strtoq(buf, &ep, 16); 215 } else { 216 val = strtoq(buf, &ep, 10); 217 } 218 break; 219 } 220 return (val); 221 } 222 223 static void 224 aml_simulation_regload(const char *dumpfile) 225 { 226 char buf[256], *np, *ep; 227 struct ACPIRegionContent rc; 228 FILE *fp; 229 230 if (!aml_simulation_initialized) { 231 return; 232 } 233 234 if ((fp = fopen(dumpfile, "r")) == NULL) { 235 return; 236 } 237 238 while (fgets(buf, sizeof buf, fp) != NULL) { 239 np = buf; 240 /* reading region type */ 241 rc.regtype = strtoq(np, &ep, 10); 242 if (np == ep) { 243 continue; 244 } 245 np = ep; 246 247 /* reading address */ 248 rc.addr = strtoq(np, &ep, 16); 249 if (np == ep) { 250 continue; 251 } 252 np = ep; 253 254 /* reading value */ 255 rc.value = strtoq(np, &ep, 16); 256 if (np == ep) { 257 continue; 258 } 259 aml_simulate_regcontent_write(rc.regtype, rc.addr, &rc.value); 260 } 261 262 fclose(fp); 263 } 264 265 static void 266 aml_simulation_regdump(const char *dumpfile) 267 { 268 struct ACPIRegionContent *rc; 269 FILE *fp; 270 271 if (!aml_simulation_initialized) { 272 return; 273 } 274 if ((fp = fopen(dumpfile, "w")) == NULL) { 275 warn("%s", dumpfile); 276 return; 277 } 278 while (!TAILQ_EMPTY(&RegionContentList)) { 279 rc = TAILQ_FIRST(&RegionContentList); 280 fprintf(fp, "%d 0x%jx 0x%x\n", 281 rc->regtype, (uintmax_t)rc->addr, rc->value); 282 TAILQ_REMOVE(&RegionContentList, rc, links); 283 free(rc); 284 } 285 286 fclose(fp); 287 TAILQ_INIT(&RegionContentList); 288 } 289 290 /* 291 * Space handlers on virtual machine 292 */ 293 294 static ACPI_STATUS 295 aml_vm_space_handler( 296 UINT32 SpaceID, 297 UINT32 Function, 298 ACPI_PHYSICAL_ADDRESS Address, 299 UINT32 BitWidth, 300 UINT64 *Value, 301 int Prompt) 302 { 303 int state; 304 UINT8 val; 305 UINT64 value, i; 306 char msg[256]; 307 static const char *space_names[] = { 308 "SYSTEM_MEMORY", "SYSTEM_IO", "PCI_CONFIG", 309 "EC", "SMBUS", "CMOS", "PCI_BAR_TARGET"}; 310 311 switch (Function) { 312 case ACPI_READ: 313 value = 0; 314 for (i = 0; (i * 8) < BitWidth; i++) { 315 state = aml_simulate_regcontent_read(SpaceID, 316 Address + i, &val); 317 if (state == -1) { 318 return (AE_NO_MEMORY); 319 } 320 value |= val << (i * 8); 321 } 322 *Value = value; 323 if (Prompt) { 324 sprintf(msg, "[read (%s, %2d, 0x%jx)]", 325 space_names[SpaceID], BitWidth, 326 (uintmax_t)Address); 327 *Value = aml_simulate_prompt(msg, value); 328 if (*Value != value) { 329 return(aml_vm_space_handler(SpaceID, 330 ACPI_WRITE, 331 Address, BitWidth, Value, 0)); 332 } 333 } 334 break; 335 336 case ACPI_WRITE: 337 value = *Value; 338 if (Prompt) { 339 sprintf(msg, "[write(%s, %2d, 0x%jx)]", 340 space_names[SpaceID], BitWidth, 341 (uintmax_t)Address); 342 value = aml_simulate_prompt(msg, *Value); 343 } 344 *Value = value; 345 for (i = 0; (i * 8) < BitWidth; i++) { 346 val = value & 0xff; 347 state = aml_simulate_regcontent_write(SpaceID, 348 Address + i, &val); 349 if (state == -1) { 350 return (AE_NO_MEMORY); 351 } 352 value = value >> 8; 353 } 354 } 355 356 return (AE_OK); 357 } 358 359 #define DECLARE_VM_SPACE_HANDLER(name, id); \ 360 static ACPI_STATUS \ 361 aml_vm_space_handler_##name ( \ 362 UINT32 Function, \ 363 ACPI_PHYSICAL_ADDRESS Address, \ 364 UINT32 BitWidth, \ 365 UINT64 *Value) \ 366 { \ 367 return (aml_vm_space_handler(id, Function, Address, \ 368 BitWidth, Value, aml_debug_prompt)); \ 369 } 370 371 DECLARE_VM_SPACE_HANDLER(system_memory, ACPI_ADR_SPACE_SYSTEM_MEMORY); 372 DECLARE_VM_SPACE_HANDLER(system_io, ACPI_ADR_SPACE_SYSTEM_IO); 373 DECLARE_VM_SPACE_HANDLER(pci_config, ACPI_ADR_SPACE_PCI_CONFIG); 374 DECLARE_VM_SPACE_HANDLER(ec, ACPI_ADR_SPACE_EC); 375 DECLARE_VM_SPACE_HANDLER(smbus, ACPI_ADR_SPACE_SMBUS); 376 DECLARE_VM_SPACE_HANDLER(cmos, ACPI_ADR_SPACE_CMOS); 377 DECLARE_VM_SPACE_HANDLER(pci_bar_target,ACPI_ADR_SPACE_PCI_BAR_TARGET); 378 379 /* 380 * Load DSDT data file and invoke debugger 381 */ 382 383 static int 384 load_dsdt(const char *dsdtfile) 385 { 386 char filetmp[PATH_MAX]; 387 ACPI_NEW_TABLE_DESC *list; 388 u_int8_t *code; 389 struct stat sb; 390 int dounlink, error, fd; 391 392 fd = open(dsdtfile, O_RDONLY, 0); 393 if (fd == -1) { 394 perror("open"); 395 return (-1); 396 } 397 if (fstat(fd, &sb) == -1) { 398 perror("fstat"); 399 close(fd); 400 return (-1); 401 } 402 code = mmap(NULL, (size_t)sb.st_size, PROT_READ, MAP_PRIVATE, fd, (off_t)0); 403 close(fd); 404 if (code == NULL) { 405 perror("mmap"); 406 return (-1); 407 } 408 if ((error = AcpiInitializeSubsystem()) != AE_OK) { 409 munmap(code, (size_t)sb.st_size); 410 return (-1); 411 } 412 413 /* 414 * make sure DSDT data contains table header or not. 415 */ 416 if (strncmp((char *)code, "DSDT", 4) == 0) { 417 dounlink = 0; 418 strlcpy(filetmp, dsdtfile, sizeof(filetmp)); 419 } else { 420 dounlink = 1; 421 mode_t mode = (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); 422 dummy_dsdt_table.Length = sizeof(ACPI_TABLE_HEADER) + sb.st_size; 423 if ((size_t)snprintf(filetmp, sizeof(filetmp), "%s.tmp", 424 dsdtfile) > sizeof(filetmp) - 1) { 425 fprintf(stderr, "file name too long\n"); 426 munmap(code, (size_t)sb.st_size); 427 return (-1); 428 } 429 fd = open(filetmp, O_WRONLY | O_CREAT | O_TRUNC, mode); 430 if (fd == -1) { 431 perror("open"); 432 munmap(code, (size_t)sb.st_size); 433 return (-1); 434 } 435 write(fd, &dummy_dsdt_table, sizeof(ACPI_TABLE_HEADER)); 436 437 write(fd, code, sb.st_size); 438 close(fd); 439 } 440 munmap(code, (size_t)sb.st_size); 441 442 /* 443 * Install the virtual machine version of address space handlers. 444 */ 445 if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT, 446 ACPI_ADR_SPACE_SYSTEM_MEMORY, 447 (ACPI_ADR_SPACE_HANDLER)aml_vm_space_handler_system_memory, 448 NULL, NULL)) != AE_OK) { 449 fprintf(stderr, "could not initialise SystemMemory handler: %d\n", error); 450 return (-1); 451 } 452 if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT, 453 ACPI_ADR_SPACE_SYSTEM_IO, 454 (ACPI_ADR_SPACE_HANDLER)aml_vm_space_handler_system_io, 455 NULL, NULL)) != AE_OK) { 456 fprintf(stderr, "could not initialise SystemIO handler: %d\n", error); 457 return (-1); 458 } 459 if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT, 460 ACPI_ADR_SPACE_PCI_CONFIG, 461 (ACPI_ADR_SPACE_HANDLER)aml_vm_space_handler_pci_config, 462 NULL, NULL)) != AE_OK) { 463 fprintf(stderr, "could not initialise PciConfig handler: %d\n", error); 464 return (-1); 465 } 466 if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT, 467 ACPI_ADR_SPACE_EC, 468 (ACPI_ADR_SPACE_HANDLER)aml_vm_space_handler_ec, 469 NULL, NULL)) != AE_OK) { 470 fprintf(stderr, "could not initialise EC handler: %d\n", error); 471 return (-1); 472 } 473 if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT, 474 ACPI_ADR_SPACE_SMBUS, 475 (ACPI_ADR_SPACE_HANDLER)aml_vm_space_handler_smbus, 476 NULL, NULL)) != AE_OK) { 477 fprintf(stderr, "could not initialise SMBUS handler: %d\n", error); 478 return (-1); 479 } 480 if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT, 481 ACPI_ADR_SPACE_CMOS, 482 (ACPI_ADR_SPACE_HANDLER)aml_vm_space_handler_cmos, 483 NULL, NULL)) != AE_OK) { 484 fprintf(stderr, "could not initialise CMOS handler: %d\n", error); 485 return (-1); 486 } 487 if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT, 488 ACPI_ADR_SPACE_PCI_BAR_TARGET, 489 (ACPI_ADR_SPACE_HANDLER)aml_vm_space_handler_pci_bar_target, 490 NULL, NULL)) != AE_OK) { 491 fprintf(stderr, "could not initialise PCI BAR TARGET handler: %d\n", error); 492 return (-1); 493 } 494 495 list = NULL; 496 AcGetAllTablesFromFile(filetmp, TRUE, &list); 497 498 AcpiInitializeDebugger(); 499 AcpiGbl_DebuggerConfiguration = 0; 500 AcpiDbUserCommands(); 501 502 if (dounlink) { 503 unlink(filetmp); 504 } 505 506 return (0); 507 } 508 509 static void 510 usage(const char *progname) 511 { 512 513 printf("usage: %s dsdt_file\n", progname); 514 exit(1); 515 } 516 517 int 518 main(int argc, char *argv[]) 519 { 520 char *progname; 521 522 progname = argv[0]; 523 524 if (argc == 1) { 525 usage(progname); 526 } 527 528 AcpiDbgLevel = ACPI_DEBUG_DEFAULT; 529 530 /* 531 * Match kernel options for the interpreter. Global variable names 532 * can be found in acglobal.h. 533 */ 534 AcpiGbl_EnableInterpreterSlack = TRUE; 535 536 aml_simulation_regload("region.ini"); 537 if (load_dsdt(argv[1]) == 0) { 538 aml_simulation_regdump("region.dmp"); 539 } 540 541 return (0); 542 } 543