organize source files into separate directories
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
#include "../include/idt.h"
|
||||
#include <stdbool.h>
|
||||
static bool vectors[sizeof(idt)];
|
||||
|
||||
extern void* isr_stub_table[];
|
||||
|
||||
#define KERNEL_CS 0x08
|
||||
|
||||
void idt_set() {
|
||||
idtr.base = (uint32) &idt;
|
||||
idtr.limit = sizeof(idt) * sizeof(idt_entry_t) - 1;
|
||||
__asm__ __volatile__("lidt %0" : : "m" (idtr));
|
||||
}
|
||||
|
||||
|
||||
void idt_set_descriptor(uint8_t vector, uint32 isr, uint8_t flags) {
|
||||
idt_entry_t* descriptor = &idt[vector];
|
||||
|
||||
descriptor->isr_low = isr & 0xFFFF;
|
||||
descriptor->kernel_cs = 0x08; // this value can be whatever offset your kernel code selector is in your GDT
|
||||
descriptor->attributes = flags;
|
||||
descriptor->isr_high = high_16(isr);//(uint32_t)isr >> 16;
|
||||
descriptor->reserved = 0;
|
||||
}
|
||||
@@ -0,0 +1,289 @@
|
||||
#include "../include/idt.h"
|
||||
#include "../include/isr.h"
|
||||
#include "../include/types.h"
|
||||
#include "../include/pic.h"
|
||||
#include "../include/pit.h"
|
||||
#include "../include/keyboard.h"
|
||||
#include "../include/task.h"
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
typedef size_t uword_t;
|
||||
struct interrupt_frame
|
||||
{
|
||||
uword_t ip;
|
||||
uword_t cs;
|
||||
uword_t flags;
|
||||
uword_t sp;
|
||||
uword_t ss;
|
||||
};
|
||||
|
||||
__attribute__((interrupt)) void isr_timer(struct interrupt_frame *frame);
|
||||
void isr_custom();
|
||||
void default_handler();
|
||||
|
||||
void isr_install() {
|
||||
idt_set_descriptor(0, (uint32)isr0, 0x8E);
|
||||
idt_set_descriptor(1, (uint32)isr1, 0x8E);
|
||||
idt_set_descriptor(2, (uint32)isr2, 0x8E);
|
||||
idt_set_descriptor(3, (uint32)isr3, 0x8E);
|
||||
idt_set_descriptor(4, (uint32)isr4, 0x8E);
|
||||
idt_set_descriptor(5, (uint32)isr5, 0x8E);
|
||||
idt_set_descriptor(6, (uint32)isr6, 0x8E);
|
||||
idt_set_descriptor(7, (uint32)isr7, 0x8E);
|
||||
idt_set_descriptor(8, (uint32)isr8, 0x8E);
|
||||
idt_set_descriptor(9, (uint32)isr9, 0x8E);
|
||||
idt_set_descriptor(10, (uint32)isr10, 0x8E);
|
||||
idt_set_descriptor(11, (uint32)isr11, 0x8E);
|
||||
idt_set_descriptor(12, (uint32)isr12, 0x8E);
|
||||
idt_set_descriptor(13, (uint32)isr13, 0x8E);
|
||||
idt_set_descriptor(14, (uint32)isr14, 0x8E);
|
||||
idt_set_descriptor(15, (uint32)isr15, 0x8E);
|
||||
idt_set_descriptor(16, (uint32)isr16, 0x8E);
|
||||
idt_set_descriptor(17, (uint32)isr17, 0x8E);
|
||||
idt_set_descriptor(18, (uint32)isr18, 0x8E);
|
||||
idt_set_descriptor(19, (uint32)isr19, 0x8E);
|
||||
idt_set_descriptor(20, (uint32)isr20, 0x8E);
|
||||
idt_set_descriptor(21, (uint32)isr21, 0x8E);
|
||||
idt_set_descriptor(22, (uint32)isr22, 0x8E);
|
||||
idt_set_descriptor(23, (uint32)isr23, 0x8E);
|
||||
idt_set_descriptor(24, (uint32)isr24, 0x8E);
|
||||
idt_set_descriptor(25, (uint32)isr25, 0x8E);
|
||||
idt_set_descriptor(26, (uint32)isr26, 0x8E);
|
||||
idt_set_descriptor(27, (uint32)isr27, 0x8E);
|
||||
idt_set_descriptor(28, (uint32)isr28, 0x8E);
|
||||
idt_set_descriptor(29, (uint32)isr29, 0x8E);
|
||||
idt_set_descriptor(30, (uint32)isr30, 0x8E);
|
||||
idt_set_descriptor(31, (uint32)isr31, 0x8E);
|
||||
|
||||
for(int i = 32; i < 255; i++)
|
||||
idt_set_descriptor(i, (uint32)default_handler, 0x8E);
|
||||
|
||||
idt_set_descriptor(0x20, (uint32)isr_timer, 0x8E);
|
||||
idt_set_descriptor(0x21, (uint32)isr_custom, 0x8E);
|
||||
|
||||
|
||||
idt_set(); // Load with ASM
|
||||
printf("isr_install done\n");
|
||||
}
|
||||
|
||||
void default_handler()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
__attribute__((interrupt)) void isr_timer(struct interrupt_frame *frame)
|
||||
{
|
||||
pit_init(100);
|
||||
pic_end_int(0x0);
|
||||
|
||||
scheduler_lock();
|
||||
schedule();
|
||||
scheduler_unlock();
|
||||
}
|
||||
|
||||
__attribute__((interrupt)) void isr_custom(struct interrupt_frame *frame)
|
||||
{
|
||||
//terminal_writestring("YOOOOO!!!!!! I LOVE KATYA!!!!");
|
||||
handle_keyboard();
|
||||
pic_end_int(0x1);
|
||||
}
|
||||
|
||||
void isr0()
|
||||
{
|
||||
printf(exception_messages[0]);
|
||||
asm("hlt");
|
||||
}
|
||||
void isr1()
|
||||
{
|
||||
printf(exception_messages[1]);
|
||||
asm("hlt");
|
||||
}
|
||||
void isr2()
|
||||
{
|
||||
printf(exception_messages[2]);
|
||||
asm("hlt");
|
||||
}
|
||||
void isr3()
|
||||
{
|
||||
printf(exception_messages[3]);
|
||||
asm("hlt");
|
||||
}
|
||||
void isr4()
|
||||
{
|
||||
printf(exception_messages[4]);
|
||||
asm("hlt");
|
||||
}
|
||||
void isr5()
|
||||
{
|
||||
printf(exception_messages[5]);
|
||||
asm("hlt");
|
||||
}
|
||||
void isr6()
|
||||
{
|
||||
printf(exception_messages[6]);
|
||||
asm("hlt");
|
||||
}
|
||||
void isr7()
|
||||
{
|
||||
printf(exception_messages[7]);
|
||||
asm("hlt");
|
||||
}
|
||||
void isr8()
|
||||
{
|
||||
printf(exception_messages[8]);
|
||||
asm("hlt");
|
||||
}
|
||||
void isr9()
|
||||
{
|
||||
printf(exception_messages[9]);
|
||||
asm("hlt");
|
||||
}
|
||||
void isr10()
|
||||
{
|
||||
printf(exception_messages[10]);
|
||||
asm("hlt");
|
||||
}
|
||||
void isr11()
|
||||
{
|
||||
printf(exception_messages[11]);
|
||||
asm("hlt");
|
||||
}
|
||||
void isr12()
|
||||
{
|
||||
printf(exception_messages[12]);
|
||||
asm("hlt");
|
||||
}
|
||||
void isr13()
|
||||
{
|
||||
printf(exception_messages[13]);
|
||||
asm("hlt");
|
||||
}
|
||||
void isr14()
|
||||
{
|
||||
printf(exception_messages[14]);
|
||||
asm("hlt");
|
||||
}
|
||||
void isr15()
|
||||
{
|
||||
printf(exception_messages[15]);
|
||||
asm("hlt");
|
||||
}
|
||||
void isr16()
|
||||
{
|
||||
printf(exception_messages[16]);
|
||||
asm("hlt");
|
||||
}
|
||||
void isr17()
|
||||
{
|
||||
printf(exception_messages[17]);
|
||||
asm("hlt");
|
||||
}
|
||||
void isr18()
|
||||
{
|
||||
printf(exception_messages[18]);
|
||||
asm("hlt");
|
||||
}
|
||||
void isr19()
|
||||
{
|
||||
printf(exception_messages[19]);
|
||||
asm("hlt");
|
||||
}
|
||||
void isr20()
|
||||
{
|
||||
printf(exception_messages[20]);
|
||||
asm("hlt");
|
||||
}
|
||||
void isr21()
|
||||
{
|
||||
printf(exception_messages[21]);
|
||||
asm("hlt");
|
||||
}
|
||||
void isr22()
|
||||
{
|
||||
printf(exception_messages[22]);
|
||||
asm("hlt");
|
||||
}
|
||||
void isr23()
|
||||
{
|
||||
printf(exception_messages[23]);
|
||||
asm("hlt");
|
||||
}
|
||||
void isr24()
|
||||
{
|
||||
printf(exception_messages[24]);
|
||||
asm("hlt");
|
||||
}
|
||||
void isr25()
|
||||
{
|
||||
printf(exception_messages[25]);
|
||||
asm("hlt");
|
||||
}
|
||||
void isr26()
|
||||
{
|
||||
printf(exception_messages[26]);
|
||||
asm("hlt");
|
||||
}
|
||||
void isr27()
|
||||
{
|
||||
printf(exception_messages[27]);
|
||||
asm("hlt");
|
||||
}
|
||||
void isr28()
|
||||
{
|
||||
printf(exception_messages[28]);
|
||||
asm("hlt");
|
||||
}
|
||||
void isr29()
|
||||
{
|
||||
printf(exception_messages[29]);
|
||||
asm("hlt");
|
||||
}
|
||||
void isr30()
|
||||
{
|
||||
printf(exception_messages[30]);
|
||||
asm("hlt");
|
||||
}
|
||||
void isr31()
|
||||
{
|
||||
printf(exception_messages[31]);
|
||||
asm("hlt");
|
||||
}
|
||||
|
||||
static string exception_messages[] = {
|
||||
"Division By Zero",
|
||||
"Debug",
|
||||
"Non Maskable Interrupt",
|
||||
"Breakpoint",
|
||||
"Into Detected Overflow",
|
||||
"Out of Bounds",
|
||||
"Invalid Opcode",
|
||||
"No Coprocessor",
|
||||
|
||||
"Double Fault",
|
||||
"Coprocessor Segment Overrun",
|
||||
"Bad TSS",
|
||||
"Segment Not Present",
|
||||
"Stack Fault",
|
||||
"General Protection Fault",
|
||||
"Page Fault",
|
||||
"Unknown Interrupt",
|
||||
|
||||
"Coprocessor Fault",
|
||||
"Alignment Check",
|
||||
"Machine Check",
|
||||
"Reserved",
|
||||
"Reserved",
|
||||
"Reserved",
|
||||
"Reserved",
|
||||
"Reserved",
|
||||
|
||||
"Reserved",
|
||||
"Reserved",
|
||||
"Reserved",
|
||||
"Reserved",
|
||||
"Reserved",
|
||||
"Reserved",
|
||||
"Reserved",
|
||||
"Reserved"
|
||||
};
|
||||
@@ -0,0 +1,196 @@
|
||||
#include "../include/isr.h"
|
||||
#include "../include/screen.h"
|
||||
#include "../include/stdio.h"
|
||||
#include "../include/utils.h"
|
||||
#include "../include/disk.h"
|
||||
#include "../include/string.h"
|
||||
#include "../include/pic.h"
|
||||
#include "../include/pit.h"
|
||||
#include "../include/paging.h"
|
||||
#include "../include/fat.h"
|
||||
#include "../include/task.h"
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
disk_ops_t ops =
|
||||
{
|
||||
.read = ata_read,
|
||||
.write = ata_write,
|
||||
};
|
||||
|
||||
static fat_t g_fat;
|
||||
static const char* months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
|
||||
|
||||
static int cat(const char* path)
|
||||
{
|
||||
file_t file;
|
||||
int err = fat_fopen(&file, path, "r");
|
||||
if (err)
|
||||
return err;
|
||||
|
||||
uint8_t buf[512];
|
||||
for (;;)
|
||||
{
|
||||
int cnt = fat_fread(&file, buf, 512);
|
||||
if (cnt < 0)
|
||||
return cnt;
|
||||
|
||||
printf("%.*s\n", cnt, buf);
|
||||
printf("%s\n", buf);
|
||||
if (cnt < 512)
|
||||
break;
|
||||
}
|
||||
|
||||
return fat_fclose(&file);
|
||||
}
|
||||
|
||||
static int ls(const char* path)
|
||||
{
|
||||
dir_t dir;
|
||||
dir_info_t info;
|
||||
|
||||
int err = fat_opendir(&dir, path);
|
||||
if (err)
|
||||
return err;
|
||||
|
||||
for (;;)
|
||||
{
|
||||
err = fat_readdir(&dir, &info);
|
||||
if (err == FAT_ERR_EOF)
|
||||
return FAT_ERR_NONE;
|
||||
if (err)
|
||||
return err;
|
||||
|
||||
printf("%X %s %x %x:%x %x %s%c\n",
|
||||
info.size, months[info.modified.month], info.modified.day,
|
||||
info.modified.hour, info.modified.min,
|
||||
info.namelen, info.name, info.attr & FAT_ATTR_DIR ? '/' : ' ');
|
||||
|
||||
err = fat_nextdir(&dir);
|
||||
if (err)
|
||||
return err;
|
||||
}
|
||||
}
|
||||
|
||||
void test(int err, const char* huy)
|
||||
{
|
||||
if(err != 0)
|
||||
printf("ERROR: %X %s\n", err, huy);
|
||||
else
|
||||
printf("%s done", huy);
|
||||
}
|
||||
|
||||
void file_fuckery()
|
||||
{
|
||||
int err, cnt;
|
||||
err = fat_mount(&ops, 0, &g_fat, "ebalo");
|
||||
|
||||
if(err)
|
||||
printf("KATYAKATYAKATYAKATYAKATYA: %X\n", err);
|
||||
|
||||
printf("%X\n", err);
|
||||
|
||||
err = ls("/ebalo");
|
||||
printf("\n\n\n");
|
||||
err = fat_mkdir("/ebalo/numbers");
|
||||
err = fat_mkdir("/ebalo/numbers/nigga");
|
||||
printf("MKDIR ERR: %X\n", err);
|
||||
|
||||
err = fat_unlink("/ebalo/numbers/nigga/");
|
||||
printf("UNLINK ERR: %X\n", err);
|
||||
|
||||
err = fat_unlink("/ebalo/numbers/");
|
||||
printf("UNLINK ERR: %X\n", err);
|
||||
|
||||
/*err = fat_mkdir("/ebalo/numbers");
|
||||
//err = cat("/ebalo/boot/grub/grub.cfg");
|
||||
char *filenames[] = {"/ebalo/numbers/d","/ebalo/numbers/e"};
|
||||
for(int i = 0; i < 2; i++){
|
||||
printf(filenames[i]);
|
||||
file_t file;
|
||||
err = fat_fopen(&file, filenames[i], "w");
|
||||
if (err)
|
||||
{printf("1 err %X\n", err); goto unmount;}
|
||||
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
cnt = fat_fprintf(&file, "This is test number %d\n", i);
|
||||
if (cnt < 0)
|
||||
{printf("2 cnt: %X\n", cnt); goto unmount;}
|
||||
}
|
||||
|
||||
err = fat_fclose(&file);
|
||||
}
|
||||
*/
|
||||
unmount:
|
||||
printf("err%X\n", err);
|
||||
err = fat_umount(&g_fat);
|
||||
test(err, "unmount");
|
||||
}
|
||||
|
||||
void apply_pic_masks()
|
||||
{
|
||||
//0x21 is master pic, 0xa1 is slave pic
|
||||
outb(0x21,0xfc);//0b11111100 (bit 0 is PIT, bit 1 is keyboard and so on)
|
||||
outb(0xa1,0xff);
|
||||
}
|
||||
|
||||
void kmain()
|
||||
{
|
||||
terminal_initialize();
|
||||
printf("KatauOS booting up\n");
|
||||
isr_install();
|
||||
pic_remap(0x20, 0x28);
|
||||
pit_init(100);
|
||||
|
||||
apply_pic_masks();
|
||||
|
||||
paging_init();
|
||||
printf("paging_init done\n");
|
||||
test_paging();
|
||||
init_allocator();
|
||||
heap_init();
|
||||
printf("after heap_init\n");
|
||||
__asm__ __volatile__ ("sti");
|
||||
scheduler_init();
|
||||
printf("Kernel init sequence completed\n");
|
||||
|
||||
char yooo[256] = "heheh";
|
||||
printf("test string: %s\n", yooo);
|
||||
|
||||
disk_info info;
|
||||
get_disk_info(&info);
|
||||
printf("CYL HEAD SECT: %X %X %X\n", info.cylinders, info.heads, info.sectors);
|
||||
|
||||
//file_fuckery();
|
||||
|
||||
char *huy;
|
||||
char *huy2;
|
||||
char temp1[1024] = "rusya_krutoy";
|
||||
char temp2[1024] = "katya_tozhe_krutaya";
|
||||
|
||||
huy = heap_alloc(1024);
|
||||
huy2 = heap_alloc(1024);
|
||||
|
||||
memcpy(huy, temp1, sizeof(temp1));
|
||||
memcpy(huy2, temp2, sizeof(temp1));
|
||||
|
||||
printf("huy1 address: 0x%X huy2 address:0x%X\n", &huy, &huy2);
|
||||
printf("huy1 points to: 0x%X huy2 points to:0x%X\n", huy, huy2);
|
||||
printf("huy1: %s\nhuy2: %s", huy, huy2);
|
||||
|
||||
while(1)
|
||||
{
|
||||
/* if(inportb(0x64) & 0x1)
|
||||
{
|
||||
unsigned char key = inportb(0x60);
|
||||
if(key == 2)
|
||||
terminal_writestring("1\n");
|
||||
else if(key == 4)
|
||||
terminal_writestring("3\n");
|
||||
else
|
||||
terminal_writestring("dunno what is it\n");
|
||||
}
|
||||
*/ }
|
||||
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
#include "../include/pic.h"
|
||||
|
||||
//took this straight outta https://wiki.osdev.org/8259_PIC#Code_Examples
|
||||
|
||||
#define PIC1 0x20 /* IO base address for master PIC */
|
||||
#define PIC2 0xA0 /* IO base address for slave PIC */
|
||||
#define PIC1_COMMAND PIC1
|
||||
#define PIC1_DATA (PIC1+1)
|
||||
#define PIC2_COMMAND PIC2
|
||||
#define PIC2_DATA (PIC2+1)
|
||||
|
||||
#define PIC_EOI 0x20 /* End-of-interrupt command code */
|
||||
|
||||
#define PIC_READ_IRR 0x0a /* OCW3 irq ready next CMD read */
|
||||
#define PIC_READ_ISR 0x0b /* OCW3 irq service next CMD read */
|
||||
|
||||
/* reinitialize the PIC controllers, giving them specified vector offsets
|
||||
rather than 8h and 70h, as configured by default */
|
||||
|
||||
#define ICW1_ICW4 0x01 /* Indicates that ICW4 will be present */
|
||||
#define ICW1_SINGLE 0x02 /* Single (cascade) mode */
|
||||
#define ICW1_INTERVAL4 0x04 /* Call address interval 4 (8) */
|
||||
#define ICW1_LEVEL 0x08 /* Level triggered (edge) mode */
|
||||
#define ICW1_INIT 0x10 /* Initialization - required! */
|
||||
|
||||
#define ICW4_8086 0x01 /* 8086/88 (MCS-80/85) mode */
|
||||
#define ICW4_AUTO 0x02 /* Auto (normal) EOI */
|
||||
#define ICW4_BUF_SLAVE 0x08 /* Buffered mode/slave */
|
||||
#define ICW4_BUF_MASTER 0x0C /* Buffered mode/master */
|
||||
#define ICW4_SFNM 0x10 /* Special fully nested (not) */
|
||||
|
||||
static inline void io_wait()
|
||||
{
|
||||
outb(0x80, 0x00);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
arguments:
|
||||
offset1 - vector offset for master PIC
|
||||
vectors on the master become offset1..offset1+7
|
||||
offset2 - same for slave PIC: offset2..offset2+7
|
||||
*/
|
||||
void pic_remap(int offset1, int offset2)
|
||||
{
|
||||
outb(PIC1_COMMAND, ICW1_INIT | ICW1_ICW4); // starts the initialization sequence (in cascade mode)
|
||||
io_wait();
|
||||
outb(PIC2_COMMAND, ICW1_INIT | ICW1_ICW4);
|
||||
io_wait();
|
||||
outb(PIC1_DATA, offset1); // ICW2: Master PIC vector offset
|
||||
io_wait();
|
||||
outb(PIC2_DATA, offset2); // ICW2: Slave PIC vector offset
|
||||
io_wait();
|
||||
outb(PIC1_DATA, 4); // ICW3: tell Master PIC that there is a slave PIC at IRQ2 (0000 0100)
|
||||
io_wait();
|
||||
outb(PIC2_DATA, 2); // ICW3: tell Slave PIC its cascade identity (0000 0010)
|
||||
io_wait();
|
||||
|
||||
outb(PIC1_DATA, ICW4_8086); // ICW4: have the PICs use 8086 mode (and not 8080 mode)
|
||||
io_wait();
|
||||
outb(PIC2_DATA, ICW4_8086);
|
||||
io_wait();
|
||||
|
||||
// Unmask both PICs.
|
||||
outb(PIC1_DATA, 0);
|
||||
outb(PIC2_DATA, 0);
|
||||
}
|
||||
|
||||
void pic_disable(void) {
|
||||
outb(PIC1_DATA, 0xff);
|
||||
outb(PIC2_DATA, 0xff);
|
||||
}
|
||||
|
||||
void pic_end_int(uint8_t irq) {
|
||||
if (irq >= 8) outb(PIC2_COMMAND, 0x20);
|
||||
outb(PIC1_COMMAND, 0x20);
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
#include "../include/pit.h"
|
||||
|
||||
//https://wiki.osdev.org/Programmable_Interval_Timer
|
||||
|
||||
/*
|
||||
Bit/s Usage
|
||||
7 Output pin state
|
||||
6 Null count flags
|
||||
5 and 4 Access mode :
|
||||
0 0 = Latch count value command
|
||||
0 1 = Access mode: lobyte only
|
||||
1 0 = Access mode: hibyte only
|
||||
1 1 = Access mode: lobyte/hibyte
|
||||
3 to 1 Operating mode :
|
||||
0 0 0 = Mode 0 (interrupt on terminal count)
|
||||
0 0 1 = Mode 1 (hardware re-triggerable one-shot)
|
||||
0 1 0 = Mode 2 (rate generator)
|
||||
0 1 1 = Mode 3 (square wave generator)
|
||||
1 0 0 = Mode 4 (software triggered strobe)
|
||||
1 0 1 = Mode 5 (hardware triggered strobe)
|
||||
1 1 0 = Mode 2 (rate generator, same as 010b)
|
||||
1 1 1 = Mode 3 (square wave generator, same as 011b)
|
||||
0 BCD/Binary mode: 0 = 16-bit binary, 1 = four-digit BCD
|
||||
*/
|
||||
|
||||
#define PIT_COMMAND_PORT 0x43
|
||||
#define PIT_DATA_PORT 0x40
|
||||
|
||||
void pit_init(int freq) {
|
||||
int divisor = 1193180 / freq;
|
||||
uint8_t command = 0x36;//00110110 see above to get the idea wtf is this number
|
||||
|
||||
outb(PIT_COMMAND_PORT, command);
|
||||
|
||||
outb(PIT_DATA_PORT, divisor & 0xFF);
|
||||
outb(PIT_DATA_PORT, divisor >> 8);
|
||||
}
|
||||
Reference in New Issue
Block a user