summaryrefslogtreecommitdiff
path: root/src/include
diff options
context:
space:
mode:
authorBrett Weiland <brett_weiland@bpcspace.com>2021-08-24 14:09:29 -0500
committerBrett Weiland <brett_weiland@bpcspace.com>2021-08-24 14:09:29 -0500
commit9b22a6965579ea1867aea291d910c96f386b518b (patch)
treed06dbb9c4708f1cc713bcb115b32ff9bce4cf9b9 /src/include
parentbad4b0e9bdfee336bfc1c23761408279eaec1558 (diff)
major backup 8.24.21
Diffstat (limited to 'src/include')
-rw-r--r--src/include/acpi.h37
-rw-r--r--src/include/heap.h5
-rw-r--r--src/include/int.h84
-rw-r--r--src/include/io.h38
-rw-r--r--src/include/isv.h25
-rw-r--r--src/include/kernel.h16
-rw-r--r--src/include/libc.h4
-rw-r--r--src/include/madt.h20
-rw-r--r--src/include/math.h6
-rw-r--r--src/include/paging.h95
-rw-r--r--src/include/panic.h60
-rw-r--r--src/include/random.h5
-rw-r--r--src/include/serial.h13
-rw-r--r--src/include/smp.h4
-rw-r--r--src/include/testmalloc.h6
-rw-r--r--src/include/timer.h8
16 files changed, 300 insertions, 126 deletions
diff --git a/src/include/acpi.h b/src/include/acpi.h
index c15e044..f06bc9b 100644
--- a/src/include/acpi.h
+++ b/src/include/acpi.h
@@ -1,30 +1,20 @@
+#ifndef acpi_included
+#define acpi_included
#include <stdint.h>
-#include <libc.h>
+#include <stdbool.h>
+#include <int.h>
-struct rsdp_v1 {
- char sig[8];
- uint8_t checksum;
- char OEMID[6];
- uint8_t version;
- uint32_t rsdt_addr;
-} __attribute__((packed));
+//sdt types
+#define SDT_MADT 0
+#define SDT_HPET 1
-struct rsdp_v2 {
- struct rsdp_v1 v1;
- uint32_t len;
- uint64_t xsdt_addr;
- uint8_t extended_checksum;
- uint8_t reserved[3];
-} __attribute__((packed));
+void find_root_sdp();
+void *find_sdt(int type);
+void debug_acpi();
-typedef union rsdp_t {
- struct rsdp_v1 v1;
- struct rsdp_v2 v2;
-} rsdp_t;
-
-struct acpi_header {
+typedef struct sdt {
char sig[4];
uint32_t length;
uint8_t revision;
@@ -34,6 +24,7 @@ struct acpi_header {
uint32_t OEMRevision;
uint32_t creator_id;
uint32_t creator_revision;
-} __attribute__((packed));
+} __attribute__((packed)) sdt_head;
+
-rsdp_t *find_RSDP();
+#endif
diff --git a/src/include/heap.h b/src/include/heap.h
new file mode 100644
index 0000000..b1d7d6a
--- /dev/null
+++ b/src/include/heap.h
@@ -0,0 +1,5 @@
+void *malloc(size_t size);
+void *realloc(void *old_chunk, size_t size);
+void malloc_init();
+void debug_heap();
+void free(void *size);
diff --git a/src/include/int.h b/src/include/int.h
new file mode 100644
index 0000000..8cc6a88
--- /dev/null
+++ b/src/include/int.h
@@ -0,0 +1,84 @@
+#ifndef int_header
+#define int_header
+
+#include <stdint.h>
+#include <stddef.h>
+
+
+
+void usleep(unsigned int us);
+void init_interrupts();
+
+typedef uint32_t *lapic_t;
+
+//this is only for madt; do we really need it here?
+struct ioapic_t {
+ void *address;
+ unsigned int gsi_base;
+ unsigned int gsi_count;
+};
+
+//future: if we start to do things like this more then once,
+struct ioapic_fixedlen {
+ size_t count;
+ struct ioapic_t *ioapic;
+};
+
+//TODO change name
+struct apic_vt {
+ uint8_t vector;
+ unsigned int delivery_mode:3;
+ unsigned int dest_mode:1;
+ unsigned int delivery_status:1;
+ unsigned int polarity:1; //0 = high, 1 = low.
+ unsigned int remote_irr:1;
+ unsigned int trigger_mode:1;
+ unsigned int mask:1;
+ unsigned long int reserved:40;
+ unsigned int destination:7;
+} __attribute__((packed));
+
+
+//simplified idt_descriptor makes things easier
+struct idt_entry {
+ void *addr;
+ unsigned int ist:3;
+ unsigned int type:4;
+ unsigned int priv:2;
+};
+
+#define IOAPIC_REDIRECT_FIXED(vector, destination) \
+ ((struct apic_vt) { \
+ .vector = vector, \
+ .delivery_mode = 0, \
+ .dest_mode = 0, \
+ .polarity = 0, \
+ .remote_irr = 0, \
+ .trigger_mode = 0, \
+ .mask = 0, \
+ .destination = destination \
+})
+
+#define KERNEL_IDT_GATE(isr) \
+ ((struct idt_entry) { \
+ .addr = isr, \
+ .ist = 0, \
+ .type = INT_GATE_64, \
+ .priv = 0 \
+})
+
+#define TASK_GATE_64 0x5
+#define INT_GATE_64 0xe
+#define TRAP_GATE_64 0xf
+
+#define SPURRIOUS_VECTOR 255
+
+
+void create_fixed_interrupt(unsigned int irq, struct apic_vt *redirect);
+void clear_int();
+unsigned int alloc_idt(struct idt_entry *entry);
+void free_idt(unsigned int entry);
+void modify_idt(struct idt_entry *entry, unsigned int vector);
+
+
+#endif
diff --git a/src/include/io.h b/src/include/io.h
new file mode 100644
index 0000000..fb19bdc
--- /dev/null
+++ b/src/include/io.h
@@ -0,0 +1,38 @@
+#ifndef io_header
+#define io_header
+
+#include <stdint.h>
+#include <printf.h>
+
+static inline void outb(uint16_t port, uint8_t value) {
+ asm volatile("outb %1, %0" :: "a"(value), "Nd"(port));
+}
+
+static inline uint8_t inb(uint16_t port) {
+ uint8_t ret;
+ asm volatile("inb %0, %1" : "=a"(ret) : "Nd"(port));
+ return(ret);
+}
+
+static inline void io_wait() {
+ asm volatile("outb 0x80, %%al"::"a"(0));
+}
+
+static inline uint64_t read_tsc() {
+ uint64_t time;
+ asm volatile("xor rax, rax\n"
+ "rdtsc\n"
+ "shl rdx, 32\n"
+ "or rax, rdx\n"
+ "mov %0, rax\n"
+ :"=r"(time):);
+ return time;
+
+}
+
+void outb_wait(uint16_t port, uint8_t value);
+void read_msr(uint32_t addr, uint64_t *value);
+void write_msr(uint32_t addr, uint64_t value);
+void hlt();
+uint64_t get_rip();
+#endif
diff --git a/src/include/isv.h b/src/include/isv.h
new file mode 100644
index 0000000..d9dfa1b
--- /dev/null
+++ b/src/include/isv.h
@@ -0,0 +1,25 @@
+#ifndef isv_defined
+#define isv_defined
+
+//TODO move to int.h
+
+struct int_frame {
+ void *rip;
+ uint64_t segment_selector;
+ uint64_t rflags;
+ void *rsp;
+ uint64_t stack_segment;
+} __attribute__((packed));
+
+struct exception_frame {
+ uint64_t err;
+ struct int_frame frame;
+} __attribute__((packed));
+
+__attribute__((interrupt)) void spurrious_int(void *unused);
+__attribute__((interrupt)) void kernel_block(void *unused);
+__attribute__((interrupt)) void fatal_hwexception(struct int_frame *frame);
+__attribute__((interrupt)) void fatal_hwexception_errcode(struct exception_frame *frame);
+__attribute__((interrupt)) void lapic_timer_racefixer(void *unused);
+
+#endif
diff --git a/src/include/kernel.h b/src/include/kernel.h
index 2a87f5e..4d8ea22 100644
--- a/src/include/kernel.h
+++ b/src/include/kernel.h
@@ -1,5 +1,13 @@
-#define EASTEREGG_BLOATWARE //TODO move to some kind of global config file
-void panic();
+#ifndef KERNEL
+#define KERNEL
-#define KERNEL_PANIC_INVOKED 0
-//more to come
+
+#define PA_OFFSET 0xffff800000000000
+#define TXT_OFFSET 0xffffffff80000000
+
+#define PHYS_TO_VIRT(addr) ((void *)(addr) + PA_OFFSET)
+
+#define DEV_EMAIL "brett_weiland@bpcspace.com"
+
+
+#endif
diff --git a/src/include/libc.h b/src/include/libc.h
index c7ccda7..d0789cf 100644
--- a/src/include/libc.h
+++ b/src/include/libc.h
@@ -1,13 +1,17 @@
#ifndef _STRING_H_
#define _STRING_H_
#include <stddef.h>
+#include <stdint.h>
void *strcpy(char *dest, char *src);
void *memcpy(void *dest, void *src, size_t n); //TODO
void *bzero(const void *dest, size_t size);
void *memset(void *s, char c, size_t n);
+size_t strlen(const char *s);
int strcmp(const char *str1, const char *str2);
int strncmp(const char *str1, const char *str2, size_t n);
int memcmp(const void *s1, const void *s2, size_t n);
int ceil(double n1);
+int round(double n1);
+
#endif
diff --git a/src/include/madt.h b/src/include/madt.h
new file mode 100644
index 0000000..84bfa5d
--- /dev/null
+++ b/src/include/madt.h
@@ -0,0 +1,20 @@
+#ifndef madt_header
+#define madt_header
+#include <int.h>
+
+struct cores_info {
+ unsigned int corecount;
+ uint8_t apic_id[256];
+ uint8_t bsp;
+};
+
+void init_madt();
+void get_ioapic(struct ioapic_fixedlen *ret);
+void get_coreinfo(struct cores_info *cores);
+void debug_madt();
+unsigned int irq_to_gsi(unsigned int irq);
+lapic_t get_lapic();
+void boot_multicore();
+
+
+#endif
diff --git a/src/include/math.h b/src/include/math.h
new file mode 100644
index 0000000..10b0521
--- /dev/null
+++ b/src/include/math.h
@@ -0,0 +1,6 @@
+#ifndef MATH_INCLUDED
+#define MATH_INCLUDED
+
+#define DIV_ROUND_UP(number, factor) (((number) + ((factor) - 1)) / factor)
+
+#endif
diff --git a/src/include/paging.h b/src/include/paging.h
index 3d31ef2..16afae2 100644
--- a/src/include/paging.h
+++ b/src/include/paging.h
@@ -1,96 +1,23 @@
-#include <stdbool.h>
-//paging errors
-#define PAGEMAP_LOCATION 0x4000
#ifndef _PAGE_H_
#define _PAGE_H_
-#include <stdint.h>
-
-#define PAGE_VIRT_UNALIGNED 0x01
-#define PAGE_PHYS_UNALIGNED 0x02
-#define PAGE_PHYS_INVALID 0x03
-#define PAGE_VIRT_INVALID 0x04
-
-//*
-typedef struct __attribute__((packed)) {
- unsigned int present : 1; // present, must be one when accessed.
- unsigned int read_write : 1; // if set to one, read and write is set
- unsigned int user : 1; // another bit we'll never use, for seperating CPL 0-2 and 3+
- unsigned int writethrough_cache : 1; // honestly maybe I should look into caching
- unsigned int cachable : 1; // hardware chaching. 0 is enabled, whats the worst that could happen?
- unsigned int accessed : 1; // we'll never use any of these!
- unsigned int zg0 : 1; // needs to be (and will be) zero'd
- unsigned int size : 1;
- unsigned int zg1 : 1; // needs to be (and will be) zero'd
- unsigned int software_marks : 3; // available for our own use, I doubt we'll use it in such a simple thing
-
- uintptr_t base_ptr : 40;
- unsigned int sign:11;
-} page_entry;
-
-
-typedef struct __attribute__((packed)) {
- page_entry pml4e[512];
- page_entry pdpe[512];
- page_entry pde[512];
- page_entry pte[512];
-} page_table;
-
-#define MEM_AVAILABLE 1
-#define MEM_RESERVED 2
-#define MEM_APCI_RECLAIMABLE 3
-#define MEM_APCI_NVS 4
-#define MEM_BAD 5
-
-#define PAGE_SIZE_4K 12
-#define PAGE_SIZE_2M 21
-#define PAGE_SIZE_1G 30
-#define MAX_BUDDY_ORDER 8
-
-#define MAX_ZONE_CNT 16 //should cover all cases
-
-struct memory_table {
- void *base;
- uint64_t length;
- uint32_t type;
- uint32_t ACPI;
-} __attribute__((packed));
-
-/**
- * the bsizes are there so we don't have to calculate it every time.
- *
- * Keep in mind that at if the allocator reaches the last buddy,
- * it _will_ have to calculate the bitwidth, even if it's a multiple of 64.
- * This scenario hopefully won't happen much during time sensitive areas,
- * and (I think) linux calculates the buddy size every single time anyway.
-**/
-struct phys_map {
- struct phys_map *next;
- void *zone_paddr;
- uint64_t extra_bits;
- uint64_t *buddy[MAX_BUDDY_ORDER];
-};
+#include <stdbool.h>
+#include <stdint.h>
+#include <stddef.h>
-//clean up doing extra work some other time
-#define LSIZE_FROM_BITLEN(bitlen) (((bitlen) + 63) / 64)
-#define BITLEN_FROM_LSIZE(lsize) ((lsize) * 64)
-#define GET_BUDDY_BITLEN(zone_len, order) ((zone_len) / (0x1000 << (order)))
-#define GET_ORDER_CHUNKSIZE(order) (0x1000 << ((order)))
+void unmap_lowmem();
+size_t map_complete_physical();
+void debug_print_memory();
-extern void* _meminfo_loc;
-extern void* _stage2_pagetable;
-bool map_page(void *virtual_addr, void *physical_addr, uint8_t PAGE_SIZE);
-void debug_print_memory();
-void create_pagetable_stage2(uint64_t free_mem);
-void init_memory(); //TODO removeme
-void init_pmap();
-void *palloc();
-void *pfree();
+struct phys_map *init_pmap(size_t pagetable_size);
+void *palloc(size_t size);
+void pfree(void *addr, size_t size);
void debug_pmap();
+void get_mem_capabilities();
+void ram_stresser();
#endif
-
diff --git a/src/include/panic.h b/src/include/panic.h
index 1386348..6a987f4 100644
--- a/src/include/panic.h
+++ b/src/include/panic.h
@@ -1,27 +1,59 @@
#ifndef PANIC_INCLUDED
#define PANIC_INCLUDED
-#define EASTEREGG_BLOATWARE
-
#include <stdint.h>
-void panic(int reason, int type);
+#define EASTEREGG_BLOATWARE
+
+//TODO this needs to be moved once we find somehwere better
struct stack_frame {
- struct stack_frame *next;
- uint64_t function_base;
+ struct stack_frame *next; //rbp
+ void *function_base; //rip
+} __attribute__((packed));
+
+struct registers {
+ uint64_t rax, rbx, rcx, rdx, rsi, rdi;
} __attribute__((packed));
-//kernel panic reasons, likely to grow at an extremely fast rate
-#define KERNEL_PANIC_PMAPSIZE 0
-#define KERNEL_PANIC_RSDP_UNFOUND 1
-#define KERNEL_PANIC_KERNEL_RETURNED 2
+void panic(int reason, void *frame_p, struct registers *regs);
+
+
+#define PANIC(reason) panic(reason, 0, 0)
+
+//hardware exceptions/faults
+//kernel panic reasons 0-31 reserved for these
+//later on, we can make an ugly function to find our vector.
+//I did a lot of work assuming it was pushed onto the stack,
+//so if I choose not to impliment that these defs will be deleted.
+/**
+#define KERNEL_PANIC_HWE_DE 0
+#define KERNEL_PANIC_HWE_BR 5
+#define KERNEL_PANIC_HWE_UD 6
+#define KERNEL_PANIC_HWE_NM 7
+#define KERNEL_PANIC_HWE_DF 8
+#define KERNEL_PANIC_HWE_TS 10
+#define KERNEL_PANIC_HWE_NP 11
+#define KERNEL_PANIC_HWE_SS 12
+#define KERNEL_PANIC_HWE_GP 13
+#define KERNEL_PANIC_HWE_PF 14
+#define KERNEL_PANIC_HWE_MF 16
+#define KERNEL_PANIC_HWE_AC 17
+#define KERNEL_PANIC_HWE_MC 18
+#define KERNEL_PANIC_HWE_XM 19
+#define KERNEL_PANIC_HWE_VE 20
+#define KERNEL_PANIC_HWE_SE 30
+**/
+#define KERNEL_PANIC_HW_EXCEPTION 0
+#define KERNEL_PANIC_HW_EXCEPTION_ERR 1 //exception with error code
+//kernel generated exceptions
+#define KERNEL_PANIC_RSDP_UNFOUND 32
+#define KERNEL_PANIC_KERNEL_RETURNED 33
+#define KERNEL_PANIC_INVALID_PFREE 34
+#define KERNEL_PANIC_INVALID_RSDT 35
+#define KERNEL_PANIC_INVALID_IOAPIC_VEC 36
+#define KERNEL_PANIC_HPET_REQUIRED 37
-//kernel panic types, may or may not expand once I get further in development
-#define KERNEL_PANIC_INVOKED 0
-#define KERNEL_PANIC_ERROR 1 //i'll change this once I see what happends
-//TODO move this to some kind of global more accesable header
-#define DEV_EMAIL "brett_weiland@bpcspace.com"
#endif
diff --git a/src/include/random.h b/src/include/random.h
new file mode 100644
index 0000000..6f35278
--- /dev/null
+++ b/src/include/random.h
@@ -0,0 +1,5 @@
+#ifndef random_header
+#define random_header
+void randinit();
+unsigned int randint();
+#endif
diff --git a/src/include/serial.h b/src/include/serial.h
index 824e245..ef5eecf 100644
--- a/src/include/serial.h
+++ b/src/include/serial.h
@@ -3,14 +3,25 @@
#define _SERIAL_H_
#include <stdint.h>
+#include <stdbool.h>
+
#define COM1 0x3f8
#define COM2 0x2f8
#define COM3 0x3e8
#define COM4 0x2e8
-int init_serial(uint16_t port);
+
+bool init_serial(uint16_t port);
void serial_out(uint16_t port, char *string);
void _putchar_serial(uint16_t port, char character);
+void move_cursor(unsigned int x, unsigned int y);
+
+
+//TODO fix shitty header
+void _putchar_screen(char character);
+
+void clear_screen();
+
#endif
diff --git a/src/include/smp.h b/src/include/smp.h
new file mode 100644
index 0000000..c4a675a
--- /dev/null
+++ b/src/include/smp.h
@@ -0,0 +1,4 @@
+#ifndef SMP_INCLUDED
+#define SMP_INCLUDED
+void smp_boot();
+#endif
diff --git a/src/include/testmalloc.h b/src/include/testmalloc.h
new file mode 100644
index 0000000..25ab7f3
--- /dev/null
+++ b/src/include/testmalloc.h
@@ -0,0 +1,6 @@
+#ifndef testmalloc_header
+#define testmalloc_header
+
+void test_malloc(unsigned int cnt);
+
+#endif
diff --git a/src/include/timer.h b/src/include/timer.h
new file mode 100644
index 0000000..0277e87
--- /dev/null
+++ b/src/include/timer.h
@@ -0,0 +1,8 @@
+#ifndef timer
+#define timer
+
+void init_timer();
+void usleep(unsigned int us);
+uint64_t timestamp();
+
+#endif