2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2000-2002 Mitsuru IWASAKI <iwasaki@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
31 #include <sys/param.h>
32 #include <sys/queue.h>
35 #include <sys/stdint.h>
36 #include <sys/types.h>
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>
54 * Dummy DSDT Table Header
57 static ACPI_TABLE_HEADER dummy_dsdt_table = {
58 "DSDT", 123, 1, 123, "OEMID", "OEMTBLID", 1, "CRID", 1
62 * Region space I/O routines on virtual machine
65 static int aml_debug_prompt = 1;
67 struct ACPIRegionContent {
68 TAILQ_ENTRY(ACPIRegionContent) links;
70 ACPI_PHYSICAL_ADDRESS addr;
74 TAILQ_HEAD(ACPIRegionContentList, ACPIRegionContent);
75 static struct ACPIRegionContentList RegionContentList;
77 static int aml_simulation_initialized = 0;
79 ACPI_PHYSICAL_ADDRESS AeLocalGetRootPointer(void);
80 void AeDoObjectOverrides(void);
81 void AeTableOverride(ACPI_TABLE_HEADER *, ACPI_TABLE_HEADER **);
83 static void aml_simulation_init(void);
84 static int aml_simulate_regcontent_add(int regtype,
85 ACPI_PHYSICAL_ADDRESS addr,
87 static int aml_simulate_regcontent_read(int regtype,
88 ACPI_PHYSICAL_ADDRESS addr,
90 static int aml_simulate_regcontent_write(int regtype,
91 ACPI_PHYSICAL_ADDRESS addr,
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);
97 /* Stubs to simplify linkage to the ACPICA core subsystem. */
99 AcpiOsGetRootPointer(void)
106 AeDoObjectOverrides(void)
111 AeTableOverride(ACPI_TABLE_HEADER *ExistingTable, ACPI_TABLE_HEADER **NewTable)
116 MpSaveGpioInfo(ACPI_PARSE_OBJECT *Op, AML_RESOURCE *Resource,
117 UINT32 PinCount, UINT16 *PinList, char *DeviceName)
122 MpSaveSerialInfo(ACPI_PARSE_OBJECT *Op, AML_RESOURCE *Resource,
128 aml_simulation_init(void)
131 aml_simulation_initialized = 1;
132 TAILQ_INIT(&RegionContentList);
133 aml_simulation_regload("region.ini");
137 aml_simulate_regcontent_add(int regtype, ACPI_PHYSICAL_ADDRESS addr, UINT8 value)
139 struct ACPIRegionContent *rc;
141 rc = malloc(sizeof(struct ACPIRegionContent));
143 return (-1); /* malloc fail */
145 rc->regtype = regtype;
149 TAILQ_INSERT_TAIL(&RegionContentList, rc, links);
154 aml_simulate_regcontent_read(int regtype, ACPI_PHYSICAL_ADDRESS addr, UINT8 *valuep)
156 struct ACPIRegionContent *rc;
158 if (!aml_simulation_initialized) {
159 aml_simulation_init();
161 TAILQ_FOREACH(rc, &RegionContentList, links) {
162 if (rc->regtype == regtype && rc->addr == addr) {
164 return (1); /* found */
169 return (aml_simulate_regcontent_add(regtype, addr, *valuep));
173 aml_simulate_regcontent_write(int regtype, ACPI_PHYSICAL_ADDRESS addr, UINT8 *valuep)
175 struct ACPIRegionContent *rc;
177 if (!aml_simulation_initialized) {
178 aml_simulation_init();
180 TAILQ_FOREACH(rc, &RegionContentList, links) {
181 if (rc->regtype == regtype && rc->addr == addr) {
183 return (1); /* exists */
187 return (aml_simulate_regcontent_add(regtype, addr, *valuep));
191 aml_simulate_prompt(char *msg, UINT64 def_val)
201 printf("(default: 0x%jx ", (uintmax_t)val);
202 printf(" / %ju) >>", (uintmax_t)val);
205 bzero(buf, sizeof buf);
207 if (read(0, buf, sizeof buf) == 0) {
210 if (buf[0] == '\n') {
211 break; /* use default value */
213 if (buf[0] == '0' && buf[1] == 'x') {
214 val = strtoq(buf, &ep, 16);
216 val = strtoq(buf, &ep, 10);
224 aml_simulation_regload(const char *dumpfile)
226 char buf[256], *np, *ep;
227 struct ACPIRegionContent rc;
230 if (!aml_simulation_initialized) {
234 if ((fp = fopen(dumpfile, "r")) == NULL) {
238 while (fgets(buf, sizeof buf, fp) != NULL) {
240 /* reading region type */
241 rc.regtype = strtoq(np, &ep, 10);
247 /* reading address */
248 rc.addr = strtoq(np, &ep, 16);
255 rc.value = strtoq(np, &ep, 16);
259 aml_simulate_regcontent_write(rc.regtype, rc.addr, &rc.value);
266 aml_simulation_regdump(const char *dumpfile)
268 struct ACPIRegionContent *rc;
271 if (!aml_simulation_initialized) {
274 if ((fp = fopen(dumpfile, "w")) == NULL) {
275 warn("%s", dumpfile);
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);
287 TAILQ_INIT(&RegionContentList);
291 * Space handlers on virtual machine
295 aml_vm_space_handler(
298 ACPI_PHYSICAL_ADDRESS Address,
307 static const char *space_names[] = {
308 "SYSTEM_MEMORY", "SYSTEM_IO", "PCI_CONFIG",
309 "EC", "SMBUS", "CMOS", "PCI_BAR_TARGET"};
314 for (i = 0; (i * 8) < BitWidth; i++) {
315 state = aml_simulate_regcontent_read(SpaceID,
318 return (AE_NO_MEMORY);
320 value |= val << (i * 8);
324 sprintf(msg, "[read (%s, %2d, 0x%jx)]",
325 space_names[SpaceID], BitWidth,
327 *Value = aml_simulate_prompt(msg, value);
328 if (*Value != value) {
329 return(aml_vm_space_handler(SpaceID,
331 Address, BitWidth, Value, 0));
339 sprintf(msg, "[write(%s, %2d, 0x%jx)]",
340 space_names[SpaceID], BitWidth,
342 value = aml_simulate_prompt(msg, *Value);
345 for (i = 0; (i * 8) < BitWidth; i++) {
347 state = aml_simulate_regcontent_write(SpaceID,
350 return (AE_NO_MEMORY);
359 #define DECLARE_VM_SPACE_HANDLER(name, id); \
361 aml_vm_space_handler_##name ( \
363 ACPI_PHYSICAL_ADDRESS Address, \
367 return (aml_vm_space_handler(id, Function, Address, \
368 BitWidth, Value, aml_debug_prompt)); \
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);
380 * Load DSDT data file and invoke debugger
384 load_dsdt(const char *dsdtfile)
386 char filetmp[PATH_MAX];
387 ACPI_NEW_TABLE_DESC *list;
390 int dounlink, error, fd;
392 fd = open(dsdtfile, O_RDONLY, 0);
397 if (fstat(fd, &sb) == -1) {
402 code = mmap(NULL, (size_t)sb.st_size, PROT_READ, MAP_PRIVATE, fd, (off_t)0);
408 if ((error = AcpiInitializeSubsystem()) != AE_OK) {
409 munmap(code, (size_t)sb.st_size);
414 * make sure DSDT data contains table header or not.
416 if (strncmp((char *)code, "DSDT", 4) == 0) {
418 strlcpy(filetmp, dsdtfile, sizeof(filetmp));
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);
429 fd = open(filetmp, O_WRONLY | O_CREAT | O_TRUNC, mode);
432 munmap(code, (size_t)sb.st_size);
435 write(fd, &dummy_dsdt_table, sizeof(ACPI_TABLE_HEADER));
437 write(fd, code, sb.st_size);
440 munmap(code, (size_t)sb.st_size);
443 * Install the virtual machine version of address space handlers.
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);
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);
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);
466 if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
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);
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);
480 if ((error = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
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);
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);
496 AcGetAllTablesFromFile(filetmp, TRUE, &list);
498 AcpiInitializeDebugger();
499 AcpiGbl_DebuggerConfiguration = 0;
500 AcpiDbUserCommands();
510 usage(const char *progname)
513 printf("usage: %s dsdt_file\n", progname);
518 main(int argc, char *argv[])
528 AcpiDbgLevel = ACPI_DEBUG_DEFAULT;
531 * Match kernel options for the interpreter. Global variable names
532 * can be found in acglobal.h.
534 AcpiGbl_EnableInterpreterSlack = TRUE;
536 aml_simulation_regload("region.ini");
537 if (load_dsdt(argv[1]) == 0) {
538 aml_simulation_regdump("region.dmp");