aboutsummaryrefslogtreecommitdiff
path: root/kernel/inc
diff options
context:
space:
mode:
authorMarvin Borner2020-08-09 16:51:01 +0200
committerMarvin Borner2020-08-09 16:51:01 +0200
commit162d024a53e1e31e00ff0b6f47dd4590edebc551 (patch)
tree711d3886c300dfaddffdafaa89b690b45eb2101d /kernel/inc
parent79f2fa136f26a0b87917336e089485712ee49bd6 (diff)
Heavy restructuring of libc, kernel and apps
Diffstat (limited to 'kernel/inc')
-rw-r--r--kernel/inc/boot.h10
-rw-r--r--kernel/inc/cpu.h29
-rw-r--r--kernel/inc/fs.h102
-rw-r--r--kernel/inc/gui.h22
-rw-r--r--kernel/inc/ide.h40
-rw-r--r--kernel/inc/interrupts.h95
-rw-r--r--kernel/inc/keyboard.h8
-rw-r--r--kernel/inc/load.h10
-rw-r--r--kernel/inc/proc.h30
-rw-r--r--kernel/inc/psf.h48
-rw-r--r--kernel/inc/serial.h9
-rw-r--r--kernel/inc/syscall.h8
-rw-r--r--kernel/inc/timer.h11
-rw-r--r--kernel/inc/vesa.h57
14 files changed, 479 insertions, 0 deletions
diff --git a/kernel/inc/boot.h b/kernel/inc/boot.h
new file mode 100644
index 0000000..1a41c32
--- /dev/null
+++ b/kernel/inc/boot.h
@@ -0,0 +1,10 @@
+// MIT License, Copyright (c) 2020 Marvin Borner
+// This file specifies the structs passed by the bootloader
+
+#include <def.h>
+#include <vesa.h>
+
+struct vid_info {
+ u32 mode;
+ struct vbe *info;
+};
diff --git a/kernel/inc/cpu.h b/kernel/inc/cpu.h
new file mode 100644
index 0000000..eb09291
--- /dev/null
+++ b/kernel/inc/cpu.h
@@ -0,0 +1,29 @@
+// MIT License, Copyright (c) 2020 Marvin Borner
+
+#ifndef CPU_H
+#define CPU_H
+
+#include <def.h>
+
+u8 inb(u16 port);
+u16 inw(u16 port);
+u32 inl(u16 port);
+void insl(u16 port, void *addr, int n);
+
+void outb(u16 port, u8 data);
+void outw(u16 port, u16 data);
+void outl(u16 port, u32 data);
+void cli();
+void sti();
+void hlt();
+void idle();
+
+static inline void spinlock(int *ptr)
+{
+ int prev;
+ do
+ __asm__ volatile("lock xchgl %0,%1" : "=a"(prev) : "m"(*ptr), "a"(1));
+ while (prev);
+}
+
+#endif
diff --git a/kernel/inc/fs.h b/kernel/inc/fs.h
new file mode 100644
index 0000000..775a1ce
--- /dev/null
+++ b/kernel/inc/fs.h
@@ -0,0 +1,102 @@
+// MIT License, Copyright (c) 2020 Marvin Borner
+// EXT2 based filesystem
+
+#ifndef FS_H
+#define FS_H
+
+#include <def.h>
+
+#define EXT2_BOOT 0
+#define EXT2_SUPER 1
+#define EXT2_ROOT 2
+#define EXT2_MAGIC 0x0000EF53
+
+struct superblock {
+ u32 total_inodes;
+ u32 total_blocks;
+ u32 su_res_blocks; // Superuser reserved
+ u32 free_blocks;
+ u32 free_inodes;
+ u32 superblock_block_num;
+ u32 log2_block_size;
+ u32 log2_frag_size;
+ u32 blocks_per_group;
+ u32 frags_per_group;
+ u32 inodes_per_group;
+ u32 last_mount_time;
+ u32 last_write_time;
+ u16 mounts_since_fsck;
+ u16 max_mounts_since_fsck;
+ u16 magic;
+ u16 state; // 1 clean; 2 errors
+ u16 error_action;
+ u16 minor_version;
+ u32 last_fsck_time;
+ u32 max_time_since_fsck;
+ u32 creator_os_id;
+ u32 major_version;
+ u16 res_block_uid;
+ u16 res_block_gid;
+};
+
+struct bgd {
+ u32 block_bitmap;
+ u32 inode_bitmap;
+ u32 inode_table;
+ u16 free_blocks;
+ u16 free_inodes;
+ u16 used_dirs;
+ u16 pad;
+ u8 bg_reserved[12];
+};
+
+struct inode {
+ u16 mode;
+ u16 uid;
+ u32 size;
+
+ u32 last_access_time;
+ u32 creation_time;
+ u32 last_modification_time;
+ u32 deletion_time;
+
+ u16 gid;
+ u16 link_count;
+ u32 blocks;
+ u32 flags;
+ u32 os_specific_val1;
+ u32 block[15];
+ u32 generation;
+
+ u32 reserved1;
+ u32 reserved2;
+
+ u32 fragment_addr;
+ u8 os_specific_val2[12];
+};
+
+#define INODE_SIZE (sizeof(struct inode))
+
+struct dirent {
+ u32 inode_num;
+ u16 total_len;
+ u8 name_len;
+ u8 type_indicator;
+ u8 name[];
+};
+
+struct file {
+ struct inode inode;
+ u32 pos;
+ u8 block_index;
+ u8 *buf;
+ u32 curr_block_pos;
+};
+
+int find_inode(const char *name, int dir_inode);
+struct inode *get_inode(int i);
+void *read_inode(struct inode *in);
+void *read_file(char *path);
+void ls_root(); // DEMO ;)
+
+#endif
diff --git a/kernel/inc/gui.h b/kernel/inc/gui.h
new file mode 100644
index 0000000..760bcb9
--- /dev/null
+++ b/kernel/inc/gui.h
@@ -0,0 +1,22 @@
+// MIT License, Copyright (c) 2020 Marvin Borner
+// Some GUI functions
+
+#ifndef GUI_H
+#define GUI_H
+
+#include <def.h>
+
+// Generalized font struct
+struct font {
+ char *chars;
+ int height;
+ int width;
+ int char_size;
+};
+
+void gui_write(int x, int y, const u32 c[3], char *text);
+void gui_term_write_char(char ch);
+void gui_term_write(char *text);
+void gui_init(char *font_path);
+
+#endif
diff --git a/kernel/inc/ide.h b/kernel/inc/ide.h
new file mode 100644
index 0000000..9d753ae
--- /dev/null
+++ b/kernel/inc/ide.h
@@ -0,0 +1,40 @@
+// MIT License, Copyright (c) 2020 Marvin Borner
+
+#ifndef IDE_H
+#define IDE_H
+
+#include <def.h>
+
+#define BLOCK_SIZE 1024
+#define SECTOR_SIZE 512
+
+#define IDE_BUSY (1 << 7)
+#define IDE_READY (1 << 6)
+#define IDE_DRIVE_FAULT (1 << 5)
+#define IDE_ERROR (1 << 0)
+
+#define IDE_IO 0x1F0
+#define IDE_DATA 0x0
+#define IDE_FEATURES 0x1
+#define IDE_SECTOR_COUNT 0x2
+#define IDE_LOW 0x3
+#define IDE_MID 0x4
+#define IDE_HIGH 0x5
+#define IDE_HEAD 0x6
+#define IDE_CMD 0x7
+#define IDE_ALTERNATE 0x3F6
+
+#define LBA_LOW(c) ((u8)(c & 0xFF))
+#define LBA_MID(c) ((u8)(c >> 8) & 0xFF)
+#define LBA_HIGH(c) ((u8)(c >> 16) & 0xFF)
+#define LBA_LAST(c) ((u8)(c >> 24) & 0xF)
+
+#define IDE_CMD_READ (BLOCK_SIZE / SECTOR_SIZE == 1) ? 0x20 : 0xC4
+#define IDE_CMD_WRITE (BLOCK_SIZE / SECTOR_SIZE == 1) ? 0x30 : 0xC5
+#define IDE_CMD_READ_MUL 0xC4
+#define IDE_CMD_WRITE_MUL 0xC5
+
+int ide_wait(int check);
+void *ide_read(void *b, u32 block);
+
+#endif
diff --git a/kernel/inc/interrupts.h b/kernel/inc/interrupts.h
new file mode 100644
index 0000000..e943e5a
--- /dev/null
+++ b/kernel/inc/interrupts.h
@@ -0,0 +1,95 @@
+// MIT License, Copyright (c) 2020 Marvin Borner
+
+#ifndef IDT_H
+#define IDT_H
+
+#include <def.h>
+
+struct regs {
+ u32 gs, fs, es, ds;
+ u32 edi, esi, ebp, esp, ebx, edx, ecx, eax;
+ u32 int_no, err_code;
+ u32 eip, cs, eflags, useresp, ss;
+};
+
+struct idt_entry {
+ u16 base_low;
+ u16 sel; // Kernel segment
+ u8 always0; // Always 0
+ u8 flags;
+ u16 base_high;
+} __attribute__((packed));
+
+struct idt_ptr {
+ u16 limit;
+ void *base;
+} __attribute__((packed));
+
+struct idt_entry idt[256];
+struct idt_ptr idt_ptr;
+
+void idt_set_gate(u8 num, u32 base, u16 sel, u8 flags);
+
+void irq_install_handler(int irq, void (*handler)(struct regs *r));
+void irq_uninstall_handler(int irq);
+
+void isr_install_handler(int isr, void (*handler)(struct regs *r));
+void isr_uninstall_handler(int isr);
+
+void interrupts_install();
+
+// External handlers (ASM)
+
+extern void isr0();
+extern void isr1();
+extern void isr2();
+extern void isr3();
+extern void isr4();
+extern void isr5();
+extern void isr6();
+extern void isr7();
+extern void isr8();
+extern void isr9();
+extern void isr10();
+extern void isr11();
+extern void isr12();
+extern void isr13();
+extern void isr14();
+extern void isr15();
+extern void isr16();
+extern void isr17();
+extern void isr18();
+extern void isr19();
+extern void isr20();
+extern void isr21();
+extern void isr22();
+extern void isr23();
+extern void isr24();
+extern void isr25();
+extern void isr26();
+extern void isr27();
+extern void isr28();
+extern void isr29();
+extern void isr30();
+extern void isr31();
+extern void isr128();
+
+extern void irq0();
+extern void irq1();
+extern void irq2();
+extern void irq3();
+extern void irq4();
+extern void irq5();
+extern void irq6();
+extern void irq7();
+extern void irq8();
+extern void irq9();
+extern void irq10();
+extern void irq11();
+extern void irq12();
+extern void irq13();
+extern void irq14();
+extern void irq15();
+extern void irq128();
+
+#endif
diff --git a/kernel/inc/keyboard.h b/kernel/inc/keyboard.h
new file mode 100644
index 0000000..f0effc7
--- /dev/null
+++ b/kernel/inc/keyboard.h
@@ -0,0 +1,8 @@
+// MIT License, Copyright (c) 2020 Marvin Borner
+
+#ifndef KEYBOARD_H
+#define KEYBOARD_H
+
+void keyboard_install();
+
+#endif
diff --git a/kernel/inc/load.h b/kernel/inc/load.h
new file mode 100644
index 0000000..60fecf9
--- /dev/null
+++ b/kernel/inc/load.h
@@ -0,0 +1,10 @@
+// MIT License, Copyright (c) 2020 Marvin Borner
+
+#ifndef LOAD_H
+#define LOAD_H
+
+#include <proc.h>
+
+void bin_load(char *path, struct proc *proc);
+
+#endif
diff --git a/kernel/inc/proc.h b/kernel/inc/proc.h
new file mode 100644
index 0000000..39ba704
--- /dev/null
+++ b/kernel/inc/proc.h
@@ -0,0 +1,30 @@
+// MIT License, Copyright (c) 2020 Marvin Borner
+
+#ifndef PROC_H
+#define PROC_H
+
+#include <def.h>
+#include <interrupts.h>
+
+#define EFLAGS_ALWAYS 0x2 // Always one
+#define EFLAGS_INTERRUPTS 0x200 // Enable interrupts
+
+#define GDT_USER_CODE_OFFSET 0x1b // User code segment offset in GDT (with ring3 mask)
+#define GDT_USER_DATA_OFFSET 0x23 // User data segment offset in GDT (with ring3 mask)
+
+enum state { PROC_RUNNING, PROC_ASLEEP };
+
+struct proc {
+ u32 pid;
+ enum state state;
+ char name[32];
+ struct regs regs;
+ /* struct proc *parent; */
+ struct proc *next;
+};
+
+void proc_init();
+void proc_print();
+struct proc *proc_make();
+
+#endif
diff --git a/kernel/inc/psf.h b/kernel/inc/psf.h
new file mode 100644
index 0000000..63a3d1e
--- /dev/null
+++ b/kernel/inc/psf.h
@@ -0,0 +1,48 @@
+// MIT License, Copyright (c) 2020 Marvin Borner
+// PSF parser
+
+#ifndef PSF_H
+#define PSF_H
+
+#include <def.h>
+
+/**
+ * PSF version 1
+ */
+
+#define PSF1_MAGIC_0 0x36
+#define PSF1_MAGIC_1 0x04
+#define PSF1_MODE_256 0
+#define PSF1_MODE_512 1
+#define PSF1_MODE_256_UNICODE 2
+#define PSF1_MODE_512_UNICODE 3
+
+struct psf1_header {
+ u8 magic[2];
+ u8 mode;
+ u8 char_size;
+};
+
+/**
+ * PSF version 2
+ */
+
+#define PSF2_MAGIC_0 0x72
+#define PSF2_MAGIC_1 0xb5
+#define PSF2_MAGIC_2 0x4a
+#define PSF2_MAGIC_3 0x86
+
+struct psf2_header {
+ u8 magic[4];
+ u32 version;
+ u32 size;
+ u32 flags;
+ u32 glyph_count;
+ u32 char_size;
+ u32 height;
+ u32 width;
+};
+
+struct font *psf_parse(char *data);
+
+#endif
diff --git a/kernel/inc/serial.h b/kernel/inc/serial.h
new file mode 100644
index 0000000..6511952
--- /dev/null
+++ b/kernel/inc/serial.h
@@ -0,0 +1,9 @@
+// MIT License, Copyright (c) 2020 Marvin Borner
+
+#ifndef SERIAL_H
+#define SERIAL_H
+
+void serial_install();
+void serial_print(const char *data);
+
+#endif
diff --git a/kernel/inc/syscall.h b/kernel/inc/syscall.h
new file mode 100644
index 0000000..3f89365
--- /dev/null
+++ b/kernel/inc/syscall.h
@@ -0,0 +1,8 @@
+// MIT License, Copyright (c) 2020 Marvin Borner
+
+#ifndef SYSCALL_H
+#define SYSCALL_H
+
+void syscall_init();
+
+#endif
diff --git a/kernel/inc/timer.h b/kernel/inc/timer.h
new file mode 100644
index 0000000..7c4f077
--- /dev/null
+++ b/kernel/inc/timer.h
@@ -0,0 +1,11 @@
+// MIT License, Copyright (c) 2020 Marvin Borner
+
+#ifndef TIMER_H
+#define TIMER_H
+
+#include <def.h>
+
+void timer_install();
+void timer_handler(); // For scheduler
+
+#endif
diff --git a/kernel/inc/vesa.h b/kernel/inc/vesa.h
new file mode 100644
index 0000000..e34cdc0
--- /dev/null
+++ b/kernel/inc/vesa.h
@@ -0,0 +1,57 @@
+// MIT License, Copyright (c) 2020 Marvin Borner
+
+#ifndef VBE_H
+#define VBE_H
+
+#include <def.h>
+
+struct vbe {
+ u16 attributes;
+ u8 window_a;
+ u8 window_b;
+ u16 granularity;
+ u16 window_size;
+ u16 segment_a;
+ u16 segment_b;
+ u32 win_func_ptr;
+ u16 pitch;
+ u16 width;
+ u16 height;
+ u8 w_char;
+ u8 y_char;
+ u8 planes;
+ u8 bpp;
+ u8 banks;
+ u8 memory_model;
+ u8 bank_size;
+ u8 image_pages;
+ u8 reserved0;
+
+ u8 red_mask;
+ u8 red_position;
+ u8 green_mask;
+ u8 green_position;
+ u8 blue_mask;
+ u8 blue_position;
+ u8 reserved_mask;
+ u8 reserved_position;
+ u8 direct_color_attributes;
+
+ u32 framebuffer;
+ u32 off_screen_mem_off;
+ u16 off_screen_mem_size;
+ u8 reserved1[206];
+};
+
+struct vbe *vbe;
+int vbe_width;
+int vbe_height;
+int vbe_bpl;
+int vbe_pitch;
+u8 *fb;
+
+void vesa_fill(const u32 color[3]);
+void vesa_set_pixel(u16 x, u16 y, const u32 color[3]);
+void vesa_init(struct vbe *info);
+
+#endif