summaryrefslogtreecommitdiff
path: root/sys
diff options
context:
space:
mode:
authorIan Moffett <ian@osmora.org>2025-11-16 17:17:45 -0500
committerIan Moffett <ian@osmora.org>2025-11-16 17:17:45 -0500
commit812151739c92e86ed3afccb94e2264f12d9380d6 (patch)
tree585ee8ac87418d33eb359bc238ee829357448267 /sys
parent996de9fe0207845223b0c20d0d84df726ab4cc2e (diff)
kern: Add initial ACPI related sources
Signed-off-by: Ian Moffett <ian@osmora.org>
Diffstat (limited to 'sys')
-rw-r--r--sys/acpi/acpi.c141
-rw-r--r--sys/inc/acpi/acpi.h45
-rw-r--r--sys/kern/Makefile1
-rw-r--r--sys/kern/kern_init.c2
4 files changed, 189 insertions, 0 deletions
diff --git a/sys/acpi/acpi.c b/sys/acpi/acpi.c
new file mode 100644
index 0000000..a521e12
--- /dev/null
+++ b/sys/acpi/acpi.c
@@ -0,0 +1,141 @@
+/*
+ * Copyright (c) 2025 Ian Marco Moffett and the Osmora Team.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of Hyra nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+#include <sys/types.h>
+#include <kern/panic.h>
+#include <os/trace.h>
+#include <acpi/acpi.h>
+#include <acpi/tables.h>
+#include <vm/vm.h>
+#include <lib/limine.h>
+#include <lib/string.h>
+
+#define dtrace(fmt, ...) trace("acpi: " fmt, ##__VA_ARGS__)
+
+static struct acpi_rsdp *rsdp = NULL;
+static struct acpi_root_sdt *sdt;
+static size_t sdt_entries = 0;
+
+/*
+ * We could just search for this ourselves but as we
+ * boot both UEFI and BIOS, ask the loader to ensure
+ * we can actually obtain it
+ */
+static struct limine_rsdp_response *rsdp_resp;
+static volatile struct limine_rsdp_request rsdp_req = {
+ .id = LIMINE_RSDP_REQUEST,
+ .revision = 0
+};
+
+/*
+ * Print the OEMID string found within the RSDP
+ */
+static void
+acpi_oemid_print(struct acpi_rsdp *rsdp)
+{
+ uint8_t rev = rsdp->revision;
+
+ /*
+ * Some emulators might not bother to set the revision
+ * in which cases it would most likely be ACPI 1.0, so
+ * we'll just bump that up for the sake of clarity if
+ * this happens.
+ */
+ if (rev == 0) {
+ ++rev;
+ }
+
+ dtrace("detected ACPI %d.0 by ", rev);
+ for (int i = 0; i < OEMID_SIZE; ++i) {
+ trace("%c", rsdp->oemid[i]);
+ }
+ trace("\n");
+}
+
+/*
+ * Verify the checksum of an ACPI header and return
+ * zero if valid
+ */
+static int
+acpi_checksum(struct acpi_header *hdr)
+{
+ uint8_t csum = 0;
+
+ for (int i = 0; i < hdr->length; ++i) {
+ csum += ((char *)hdr)[i];
+ }
+
+ return (csum == 0) ? 0 : -1;
+}
+
+void *
+acpi_query(const char *s)
+{
+ struct acpi_header *hdr;
+
+ for (int i = 0; i < sdt_entries; ++i) {
+ hdr = PHYS_TO_VIRT((uintptr_t)sdt->tables[i]);
+ if (memcmp(hdr->signature, s, 4) == 0) {
+ return acpi_checksum(hdr) == 0 ? (void *)hdr : NULL;
+ }
+ }
+
+ return NULL;
+}
+
+void
+acpi_init(void)
+{
+ rsdp_resp = rsdp_req.response;
+ if (__unlikely(rsdp_resp == NULL)) {
+ panic("acpi: could not obtain rsdp\n");
+ }
+
+ rsdp = rsdp_resp->address;
+ acpi_oemid_print(rsdp);
+
+ /* XSDT if rev > 1.0 */
+ if (rsdp->revision > 1) {
+ sdt = PHYS_TO_VIRT(rsdp->xsdt_addr);
+ sdt_entries = (sdt->hdr.length - sizeof(sdt->hdr)) / 8;
+ dtrace("using xsdt as root sdt\n");
+ } else if (rsdp->revision < 2) {
+ sdt = PHYS_TO_VIRT(rsdp->rsdt_addr);
+ sdt_entries = (sdt->hdr.length - sizeof(sdt->hdr)) / 4;
+ dtrace("using rsdt as root sdt\n");
+ }
+
+ dtrace("verifying sdt integrity...\n");
+ if (acpi_checksum(&sdt->hdr) != 0) {
+ panic("acpi: bad checksum for sdt\n");
+ }
+
+ dtrace("OK\n");
+}
diff --git a/sys/inc/acpi/acpi.h b/sys/inc/acpi/acpi.h
new file mode 100644
index 0000000..d9a2a47
--- /dev/null
+++ b/sys/inc/acpi/acpi.h
@@ -0,0 +1,45 @@
+/*
+ * Copyright (c) 2025 Ian Marco Moffett and the Osmora Team.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of Hyra nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef _ACPI_ACPI_H_
+#define _ACPI_ACPI_H_ 1
+
+#include <sys/types.h>
+
+/*
+ * Initialize the ACPI subsystem
+ */
+void acpi_init(void);
+
+/*
+ * Query an ACPI table using its signature
+ */
+void *acpi_query(const char *s);
+
+#endif /* !_ACPI_ACPI_H_ */
diff --git a/sys/kern/Makefile b/sys/kern/Makefile
index 718df06..b15e8b6 100644
--- a/sys/kern/Makefile
+++ b/sys/kern/Makefile
@@ -7,6 +7,7 @@ CFILES += $(shell find ../os -name "*.c")
CFILES += $(shell find ../vm -name "*.c")
CFILES += $(shell find ../dev -name "*.c")
CFILES += $(shell find ../mu -name "*.c")
+CFILES += $(shell find ../acpi -name "*.c")
OFILES = $(CFILES:.c=.o)
DFILES = $(CFILES:.c=.d)
CC =
diff --git a/sys/kern/kern_init.c b/sys/kern/kern_init.c
index 68fb2fb..2c6e357 100644
--- a/sys/kern/kern_init.c
+++ b/sys/kern/kern_init.c
@@ -30,6 +30,7 @@
#include <sys/types.h>
#include <dev/cons/cons.h>
#include <os/trace.h>
+#include <acpi/acpi.h>
#include <vm/phys.h>
#include <vm/vm.h>
@@ -43,4 +44,5 @@ kmain(void)
trace("bootcons: console online\n");
vm_phys_init();
vm_init();
+ acpi_init();
}