Nucleus
Barry Interrupt handlers b33d632 (3 years, 3 months ago)
diff --git a/include/io.h b/include/io.h
new file mode 100644
index 0000000..ec0abcf
--- /dev/null
+++ b/include/io.h
@@ -0,0 +1,97 @@
+#ifndef KERNEL_IO_H
+#define KERNEL_IO_H
+
+#include <stdint.h>
+#include <stddef.h>
+
+/* Read byte from port */
+static inline uint8_t
+inb(uint16_t port)
+{
+ uint8_t value;
+ asm volatile("inb %w1, %0" : "=a" (value) : "Nd" (port));
+ return value;
+}
+/* Write byte to port */
+static inline void
+outb(uint16_t port, uint8_t value)
+{
+ asm volatile("outb %b0, %w1" : : "a" (value), "Nd" (port));
+}
+
+/* Read word from port */
+static inline uint16_t
+inw(uint16_t port)
+{
+ uint16_t value;
+ asm volatile("inw %w1, %0" : "=a" (value) : "Nd" (port));
+ return value;
+}
+/* Write word to port */
+static inline void
+outw(uint16_t port, uint16_t value)
+{
+ asm volatile("outw %w0, %w1" : : "a" (value), "Nd" (port));
+}
+
+/* Read dword from port */
+static inline uint32_t
+inl(uint16_t port)
+{
+ uint32_t value;
+ asm volatile("inl %1, %0" : "=a" (value) : "Nd" (port));
+ return value;
+}
+/* Write dword to port */
+static inline void
+outl(uint16_t port, uint32_t value)
+{
+ asm volatile("outl %0, %1" : : "a" (value), "Nd" (port));
+}
+
+/* Wait for IO to be ready */
+static inline void
+io_wait(void)
+{
+ outb(0x80, 0);
+}
+
+/* Read words into buffer */
+static inline void
+insw(uint16_t port, void *addr, size_t count)
+{
+ asm volatile(
+ "cld;"
+ "repne; insw;"
+ : "=D" (addr), "=c" (count)
+ : "d" (port), "0" (addr), "1" (count)
+ : "memory", "cc"
+ );
+}
+/* Write words out from buffer */
+static inline void
+outsw(uint16_t port, void *addr, size_t count)
+{
+ asm volatile(
+ "cld;"
+ "repne; outsw;"
+ : "=D" (addr), "=c" (count)
+ : "d" (port), "0" (addr), "1" (count)
+ : "memory", "cc"
+ );
+}
+
+/* Read dwords into buffer */
+static inline void
+insl(uint16_t port, void *addr, size_t count)
+{
+ asm volatile(
+ "cld;"
+ "repne; insl;"
+ : "=D" (addr), "=c" (count)
+ : "d" (port), "0" (addr), "1" (count)
+ : "memory", "cc"
+ );
+}
+
+#endif
diff --git a/include/nucleus/memory.h b/include/nucleus/memory.h
new file mode 100644
index 0000000..c6446b0
--- /dev/null
+++ b/include/nucleus/memory.h
@@ -0,0 +1,13 @@
+#ifndef _NUCLEUS_MEMORY_H
+#define _NUCLEUS_MEMORY_H
+
+#include <stdint.h>
+#include <stddef.h>
+
+#define PAGE_SIZE 0x1000
+
+uintptr_t alloc_frame(void);
+void free_frame(uintptr_t frame);
+void init_frames(size_t memMapSize, void *memMap);
+
+#endif
diff --git a/include/nucleus/panic.h b/include/nucleus/panic.h
new file mode 100644
index 0000000..413fee6
--- /dev/null
+++ b/include/nucleus/panic.h
@@ -0,0 +1,13 @@
+#ifndef _NUCLEUS_PANIC_H
+#define _NUCLEUS_PANIC_H
+
+_Noreturn void panic(char *fmt, ...);
+
+#define ASSERT(c) ({ \
+ if (__builtin_expect(!(c),0)) \
+ panic("Assertion failed (%s:%d): " #c, \
+ __FILE__, __LINE__); \
+ c; \
+})
+
+#endif