Compare commits

11 Commits

50 changed files with 1527 additions and 869 deletions

View File

@ -5,7 +5,7 @@ CC = ${HOME}/opt/cross/bin/i686-elf-gcc
CPP = ${HOME}/opt/cross/bin/i686-elf-g++ CPP = ${HOME}/opt/cross/bin/i686-elf-g++
CFLAGS = -ffreestanding -O2 -Wall -Wextra CFLAGS = -ffreestanding -O2 -Wall -Wextra
OFILES = $(BUILD_DIR)/boot.o $(BUILD_DIR)/kterm.o $(BUILD_DIR)/kernel.o $(BUILD_DIR)/io.o $(BUILD_DIR)/PageDirectory.o $(BUILD_DIR)/idt.o $(BUILD_DIR)/pic.o $(BUILD_DIR)/string.o OFILES = $(BUILD_DIR)/boot.o $(BUILD_DIR)/window.o $(BUILD_DIR)/cursor.o $(BUILD_DIR)/kterm.o $(BUILD_DIR)/kernel.o $(BUILD_DIR)/PhysicalMemoryManager.o $(BUILD_DIR)/io.o $(BUILD_DIR)/vesa.o $(BUILD_DIR)/PageDirectory.o $(BUILD_DIR)/gdtc.o $(BUILD_DIR)/idt.o $(BUILD_DIR)/pic.o $(BUILD_DIR)/string.o
SRC_DIR = src SRC_DIR = src
BUILD_DIR = build BUILD_DIR = build
@ -19,31 +19,29 @@ OBJ_LINK_LIST = $(CRTI_OBJ) $(CRTBEGIN_OBJ) $(OFILES) $(CRTEND_OBJ) $(CRTN_OBJ)
INTERNAL_OBJS = $(CRTI_OBJ) $(OFILES) $(CRTN_OBJ) INTERNAL_OBJS = $(CRTI_OBJ) $(OFILES) $(CRTN_OBJ)
all: clean build clean_up all: clean build
build: build_kernel build: build_kernel iso
clean_iso: clean_iso:
if [[ -a isodir/* ]] ; then rm isodir/* -d ; fi if [[ -a isodir/boot ]] ; then rm root/boot -rd ; fi
if [ -f barinkOS.iso ] ; then rm barinkOS.iso ; fi if [ -f build/barinkOS.iso ] ; then rm build/barinkOS.iso ; fi
iso: clean_iso build iso: clean_iso clean build
mkdir -p isodir/boot/grub mkdir -p root/boot/grub
cp build/myos.bin isodir/boot/myos.bin cp build/myos.bin root/boot/myos.bin
cp src/grub.cfg isodir/boot/grub/grub.cfg cp src/grub.cfg root/boot/grub/grub.cfg
grub-mkrescue -o barinkOS.iso isodir grub-mkrescue -o build/barinkOS.iso root
clean_up:
rm build/*.o
test: test:
$(EMULATOR) -kernel $(BUILD_DIR)/myos.bin -serial file:CON -vga std -monitor stdio -display gtk -m 2G -cpu core2duo $(EMULATOR) -kernel $(BUILD_DIR)/myos.bin -serial mon:stdio -vga std -display gtk -m 2G -cpu core2duo # -monitor stdio
test_iso:
$(EMULATOR) -cdrom build/barinkOS.iso -serial mon:stdio -vga std -display gtk -m 2G -cpu core2duo
build_kernel: $(OBJ_LINK_LIST) build_kernel: $(OBJ_LINK_LIST)
$(CC) -T $(SRC_DIR)/kernel//linker.ld -o $(BUILD_DIR)/myos.bin \
$(CC) -T $(SRC_DIR)/kernel/arch/i386/linker.ld -o $(BUILD_DIR)/myos.bin \
-ffreestanding -O2 -nostdlib $(OBJ_LINK_LIST) -lgcc -ffreestanding -O2 -nostdlib $(OBJ_LINK_LIST) -lgcc
build_x86_64: build_x86_64:
@ -54,19 +52,19 @@ clean:
rm -f $(BUILD_DIR)/myos.bin $(INTERNAL_OBJS) rm -f $(BUILD_DIR)/myos.bin $(INTERNAL_OBJS)
$(BUILD_DIR)/kernel.o: $(BUILD_DIR)/kernel.o:
$(CPP) -c $(SRC_DIR)/kernel/kernel.cpp -o $(BUILD_DIR)/kernel.o $(CFLAGS) -fno-exceptions -fno-rtti $(CPP) -c $(SRC_DIR)/kernel/kernel.cpp -o $(BUILD_DIR)/kernel.o $(CFLAGS) -fno-exceptions -fno-rtti -fpermissive
$(BUILD_DIR)/kterm.o: $(BUILD_DIR)/kterm.o:
$(CC) -c $(SRC_DIR)/kernel/arch/i386/tty/kterm.c -o $(BUILD_DIR)/kterm.o $(CFLAGS) -std=gnu99 $(CPP) -c $(SRC_DIR)/kernel/tty/kterm.cpp -o $(BUILD_DIR)/kterm.o $(CFLAGS) -fno-exceptions -fno-rtti
$(BUILD_DIR)/boot.o: $(BUILD_DIR)/boot.o:
$(AS) $(SRC_DIR)/kernel/arch/i386/boot.s -o $(BUILD_DIR)/boot.o $(AS) $(SRC_DIR)/kernel//boot.S -o $(BUILD_DIR)/boot.o
$(BUILD_DIR)/crti.o: $(BUILD_DIR)/crti.o:
$(AS) $(SRC_DIR)/kernel/arch/i386/crti.s -o $(BUILD_DIR)/crti.o $(AS) $(SRC_DIR)/kernel/crti.s -o $(BUILD_DIR)/crti.o
$(BUILD_DIR)/crtn.o: $(BUILD_DIR)/crtn.o:
$(AS) $(SRC_DIR)/kernel/arch/i386/crtn.s -o $(BUILD_DIR)/crtn.o $(AS) $(SRC_DIR)/kernel/crtn.s -o $(BUILD_DIR)/crtn.o
$(BUILD_DIR)/io.o: $(BUILD_DIR)/io.o:
$(CPP) -c $(SRC_DIR)/kernel/io.cpp -o $(BUILD_DIR)/io.o $(CFLAGS) -fno-exceptions -fno-rtti $(CPP) -c $(SRC_DIR)/kernel/io.cpp -o $(BUILD_DIR)/io.o $(CFLAGS) -fno-exceptions -fno-rtti
@ -75,10 +73,26 @@ $(BUILD_DIR)/PageDirectory.o:
$(CPP) -c $(SRC_DIR)/kernel/memory/PageDirectory.cpp -o $(BUILD_DIR)/PageDirectory.o $(CFLAGS) -fno-exceptions -fno-rtti $(CPP) -c $(SRC_DIR)/kernel/memory/PageDirectory.cpp -o $(BUILD_DIR)/PageDirectory.o $(CFLAGS) -fno-exceptions -fno-rtti
$(BUILD_DIR)/idt.o: $(BUILD_DIR)/idt.o:
$(CPP) -c $(SRC_DIR)/kernel/arch/i386/idt/idt.cpp -o $(BUILD_DIR)/idt.o $(CFLAGS) -fno-exceptions -fno-rtti $(CPP) -c $(SRC_DIR)/kernel/idt/idt.cpp -o $(BUILD_DIR)/idt.o $(CFLAGS) -fno-exceptions -fno-rtti
$(BUILD_DIR)/gdtc.o:
$(CPP) -c $(SRC_DIR)/kernel/gdt/gdtc.cpp -o $(BUILD_DIR)/gdtc.o $(CFLAGS) -fno-exceptions -fno-rtti
$(BUILD_DIR)/pic.o: $(BUILD_DIR)/pic.o:
$(CPP) -c $(SRC_DIR)/kernel/arch/i386/pic/pic.cpp -o $(BUILD_DIR)/pic.o $(CFLAGS) -fno-exceptions -fno-rtti $(CPP) -c $(SRC_DIR)/kernel/pic/pic.cpp -o $(BUILD_DIR)/pic.o $(CFLAGS) -fno-exceptions -fno-rtti
$(BUILD_DIR)/string.o: $(BUILD_DIR)/string.o:
$(CC) -c $(SRC_DIR)/libc/include/string.c -o $(BUILD_DIR)/string.o $(CFLAGS) -std=gnu99 $(CC) -c $(SRC_DIR)/libc/include/string.c -o $(BUILD_DIR)/string.o $(CFLAGS) -std=gnu99
$(BUILD_DIR)/PhysicalMemoryManager.o:
$(CPP) -c $(SRC_DIR)/kernel/memory/PhysicalMemoryManager.cpp -o $(BUILD_DIR)/PhysicalMemoryManager.o $(CFLAGS) -fno-exceptions -fno-rtti
$(BUILD_DIR)/vesa.o:
$(CPP) -c $(SRC_DIR)/kernel/drivers/vesa/vesa.cpp -o $(BUILD_DIR)/vesa.o $(CFLAGS) -fno-exceptions -fno-rtti
$(BUILD_DIR)/window.o:
$(CPP) -c $(SRC_DIR)/gui/window.cpp -o $(BUILD_DIR)/window.o $(CFLAGS) -fno-exceptions -fno-rtti
$(BUILD_DIR)/cursor.o:
$(CPP) -c $(SRC_DIR)/gui/cursor.cpp -o $(BUILD_DIR)/cursor.o $(CFLAGS) -fno-exceptions -fno-rtti

View File

@ -6,6 +6,9 @@
________________________ ________________________
### Screenshot(s) ### Screenshot(s)
![Graphics mode](screenshots/BarinkOS_VBE_GRAPHICS.png) \
It may not look like much but I am proud of it! We are in graphics mode.
![Scrolling the terminal](screenshots/Screenshot1.png) \ ![Scrolling the terminal](screenshots/Screenshot1.png) \
The first scrolling boot screen. 😲 The first scrolling boot screen. 😲
@ -28,8 +31,8 @@ screen. The terminal/screen has scrolling so the latest messages are visible on
________________________ ________________________
### Planning ### Planning
[See TODO](TODO.md) [See TODO](todo.md) \
[See Features](features.md)
________________________ ________________________
### Docs ### Docs
[Intro](docs/Intro.md) \ [Intro](docs/Intro.md) \

View File

@ -12,7 +12,7 @@
<input type="checkbox" /> Detect CPU speed \ <input type="checkbox" /> Detect CPU speed \
<input type="checkbox" checked/> Interrupt / exception system (API) \ <input type="checkbox" checked/> Interrupt / exception system (API) \
<input type="checkbox" /> Plan your memory map (virtual, and physical) : decide where you want the data to be. \ <input type="checkbox" checked/> Plan your memory map (virtual, and physical) : decide where you want the data to be. \
<input type="checkbox" /> The heap: allocating memory at runtime (malloc and free) is almost impossible to go without. \ <input type="checkbox" /> The heap: allocating memory at runtime (malloc and free) is almost impossible to go without. \
<input type="checkbox" /> Enable SIMD Extensions (SSE) <input type="checkbox" /> Enable SIMD Extensions (SSE)
@ -23,13 +23,13 @@
<input type="checkbox" /> ACPI support ( Or some other basic way to support shutdown, reboot and possibly hibernation ) \ <input type="checkbox" /> ACPI support ( Or some other basic way to support shutdown, reboot and possibly hibernation ) \
<input type="checkbox" /> ATAPI support \ <input type="checkbox" /> ATAPI support \
<input type="checkbox" /> Keyboard support ( P/S2 Keyboard) \ <input type="checkbox" /> Keyboard support ( P/S2 Keyboard) \
<input type="checkbox" /> Memory Management (MMU)\ <input type="checkbox" checked/> Memory Management (MMU)
<input type="checkbox" /> Preemptive multi tasking <input type="checkbox" /> Preemptive multi tasking \
<input type="checkbox" /> Processes <input type="checkbox" /> Processes \
<input type="checkbox" /> Threads <input type="checkbox" /> Threads
<input type="checkbox" /> Scheduling (SRV2 Unix OR Priority Based Round Robin) \ <input type="checkbox" /> Scheduling (SRV2 Unix OR Priority Based Round Robin) \
<input type="checkbox" /> System V ABI compliance (partially) <input type="checkbox" /> System V ABI compliance (partially) \
<input type="checkbox" /> POSIX compliance (partially) <input type="checkbox" /> POSIX compliance (partially) \
<input type="checkbox" /> RPC - for interprocess communication \ <input type="checkbox" /> RPC - for interprocess communication \
<input type="checkbox" /> Sync primitives - Semaphores, Mutexes, spinlocks et al. \ <input type="checkbox" /> Sync primitives - Semaphores, Mutexes, spinlocks et al. \
<input type="checkbox" /> Basic Terminal \ <input type="checkbox" /> Basic Terminal \
@ -37,4 +37,4 @@
<input type="checkbox" /> Basic Window server/client \ <input type="checkbox" /> Basic Window server/client \
## Support for more filesystems if I like the challenge in writing these ... ## Support for more filesystems if I like the challenge in writing these ...
<input type="checkbox" /> FAT Filesystem \ <input type="checkbox" /> FAT Filesystem \
<input type="checkbox" /> EXT2 Filesystem \ <input type="checkbox" /> EXT2 Filesystem

BIN
screenshots/BarinkOS_VBE_GRAPHICS.png (Stored with Git LFS) Normal file

Binary file not shown.

5
src/gui/Graphics.h Normal file
View File

@ -0,0 +1,5 @@
#pragma once
class Graphics {
};

7
src/gui/Widget.h Normal file
View File

@ -0,0 +1,7 @@
#pragma once
class Widget{
virtual void draw();
};

17
src/gui/cursor.cpp Normal file
View File

@ -0,0 +1,17 @@
#include "cursor.h"
void Cursor::draw(){
for(int i = 0; i < this->width; i++){
for(int j = 0; j < this->height; j++){
if(this->bitmap[j * this->width + i] == 1 ){
putPixel(i + this->x,j + this->y, 0xFF000000);
}
}
}
}
Cursor::Cursor(int x, int y){
this->x = x;
this->y = y;
}

38
src/gui/cursor.h Normal file
View File

@ -0,0 +1,38 @@
#pragma once
#include "../kernel/drivers/vesa/vesa.h"
class Cursor{
public:
void draw();
Cursor(int x, int y);
private:
int x;
int y;
const int width= 16;
const int height= 10;
const int bitmap [160] = {
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,
0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,
0,0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,
0,0,0,0,0,1,1,1,1,1,1,1,1,1,0,0,
0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,
0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,
0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
};
};

33
src/gui/window.cpp Normal file
View File

@ -0,0 +1,33 @@
#include "window.h"
int Window::getWidth(){
return this->rect.width;
}
int Window::getHeight(){
return this->rect.height;
}
void Window::setWidth(int& width){
this->rect.width = width;
}
void Window::setHeight(int& height){
this->rect.height = height;
}
int Window::getX(){
return this->rect.x;
}
int Window::getY(){
return this->rect.y;
}
Window::Window (Rect& rect , uint32_t colour){
this->rect = rect;
this->Background_colour = colour;
}
void Window::draw(){
drawRect(this->getX() , this->getY() , this->getWidth() , this->getHeight() ,this->Background_colour );
}

37
src/gui/window.h Normal file
View File

@ -0,0 +1,37 @@
#pragma once
#include "../kernel/drivers/vesa/vesa.h"
#include "Widget.h"
struct Rect {
int width;
int height;
int x;
int y;
};
class Window : Widget{
public:
int getX();
int getY();
int getWidth();
int getHeight();
void setWidth(int&);
void setHeight(int&);
Window (Rect& rect , uint32_t colour);
void draw();
private:
Rect rect;
uint32_t Background_colour;
};

View File

@ -1,566 +0,0 @@
/*
* Multiboot
*/
.set ALIGN, 1<<0 /* align loaded modules on page boundaries */
.set MEMINFO, 1<<1 /* provide memory map */
.set FLAGS, ALIGN | MEMINFO /* this is the Multiboot 'flag' field */
.set MAGIC, 0x1BADB002 /* 'magic number' lets bootloader find the header */
.set CHECKSUM, -(MAGIC + FLAGS) /* checksum of above, to prove we are multiboot */
.section .multiboot
.align 4
.long MAGIC
.long FLAGS
.long CHECKSUM
.section .bss
.align 16
stack_bottom:
.skip 16384 # 16 KiB
stack_top:
.section .text
/*
* Interupt handlers
*/
# NOTE: I have no clue how I should use these macros.
# Have tried to use them in a myriad of ways, none would actually work
.macro irs_NoErrCode code:req
.globl irs\code
irs\code:
cli
pushb $0
pushb \code
jmp irs_common
.endm
.macro irs_ErrCode code
.globl irs\code
irs\code:
cli
pushb \code
jmp irs_common
.endm
.globl irs0
irs0:
cli
push $0
push $0
jmp irs_common
.globl irs1
irs1:
cli
push $0
push $1
jmp irs_common
.globl irs2
irs2:
cli
push $0
push $2
jmp irs_common
.globl irs3
irs3:
cli
push $0
push $3
jmp irs_common
.globl irs4
irs4:
cli
push $0
push $4
jmp irs_common
.globl irs5
irs5:
cli
push $0
push $5
jmp irs_common
.globl irs6
irs6:
cli
push $0
push $6
jmp irs_common
.globl irs7
irs7:
cli
push $0
push $7
jmp irs_common
.globl irs8
irs8:
cli
push $0
push $8
jmp irs_common
.globl irs9
irs9:
cli
push $0
push $9
jmp irs_common
.globl irs10
irs10:
cli
push $0
push $10
jmp irs_common
.globl irs11
irs11:
cli
push $0
push $11
jmp irs_common
.globl irs12
irs12:
cli
push $0
push $12
jmp irs_common
.globl irs13
irs13:
cli
push $13
jmp irs_common
.globl irs14
irs14:
cli
push $0
push $14
jmp irs_common
.globl irs15
irs15:
cli
push $0
push $15
jmp irs_common
.globl irs16
irs16:
cli
push $0
push $16
jmp irs_common
.globl irs17
irs17:
cli
push $0
push $17
jmp irs_common
.globl irs18
irs18:
cli
push $0
push $18
jmp irs_common
.globl irs19
irs19:
cli
push $0
push $19
jmp irs_common
.globl irs20
irs20:
cli
push $0
push $20
jmp irs_common
.globl irs21
irs21:
cli
push $0
push $21
jmp irs_common
.globl irs22
irs22:
cli
push $0
push $22
jmp irs_common
.globl irs23
irs23:
cli
push $0
push $23
jmp irs_common
.globl irs24
irs24:
cli
push $0
push $24
jmp irs_common
.globl irs25
irs25:
cli
push $0
push $25
jmp irs_common
.globl irs26
irs26:
cli
push $0
push $26
jmp irs_common
.globl irs27
irs27:
cli
push $0
push $27
jmp irs_common
.globl irs28
irs28:
cli
push $0
push $28
jmp irs_common
.globl irs29
irs29:
cli
push $0
push $29
jmp irs_common
.globl irs30
irs30:
cli
push $0
push $30
jmp irs_common
.globl irs31
irs31:
cli
push $0
push $31
jmp irs_common
.globl irq0
irq0:
cli
push $0
push $0
jmp irq_common
.globl irq1
irq1:
cli
push $0
push $1
jmp irq_common
.globl irq2
irq2:
cli
push $0
push $2
jmp irq_common
.globl irq3
irq3:
cli
push $0
push $3
jmp irq_common
.globl irq4
irq4:
cli
push $0
push $4
jmp irq_common
.globl irq5
irq5:
cli
push $0
push $5
jmp irq_common
.globl irq6
irq6:
cli
push $0
push $6
jmp irq_common
.globl irq7
irq7:
cli
push $0
push $7
jmp irq_common
.globl irq8
irq8:
cli
push $0
push $8
jmp irq_common
.globl irq9
irq9:
cli
push $0
push $9
jmp irq_common
.globl irq10
irq10:
cli
push $0
push $10
jmp irq_common
.globl irq11
irq11:
cli
push $0
push $11
jmp irq_common
.globl irq12
irq12:
cli
push $0
push $12
jmp irq_common
.globl irq13
irq13:
cli
push $0
push $13
jmp irq_common
.globl irq14
irq14:
cli
push $0
push $14
jmp irq_common
.globl irq15
irq15:
cli
push $0
push $15
jmp irq_common
irq_common:
pusha
mov %ds, %ax
push %eax
mov $0x10, %ax
mov %ax, %ds
mov %ax, %es
mov %ax, %fs
mov %ax, %gs
call irq_handler
pop %eax
mov %ax, %ds
mov %ax, %es
mov %ax, %fs
mov %ax, %gs
popa
add $8, %esp # cleans push error and irs code
sti
iret # pops 5 things at once: CS, EIP, EFLAGS, SS, and ESP
irs_common:
pusha # Pushes edi,esi,ebp,esp,ebx,edx,ecx,eax
mov %ds, %ax
push %eax
/* load the kernel data segment descriptor*/
mov $0x10, %ax
mov %ax, %ds
mov %ax, %es
mov %ax, %fs
mov %ax, %gs
call irs_handler
pop %eax
mov %ax, %ds
mov %ax, %es
mov %ax, %fs
mov %ax, %gs
popa
add $8, %esp # cleans push error and irs code
sti
iret # pops 5 things at once: CS, EIP, EFLAGS, SS, and ESP
.globl idt_flush
idt_flush:
mov 4(%esp), %eax
lidt (%eax)
ret
.globl enablePaging
enablePaging:
push %ebp
mov %esp, %ebp
mov %cr0, %eax
or $0x80000000, %eax
mov %eax, %cr0
mov %ebp, %esp
pop %ebp
ret
.globl loadPageDirectory
loadPageDirectory:
push %ebp
mov %esp, %ebp
mov 8(%esp), %eax
mov %eax, %cr3
mov %ebp, %esp
pop %ebp
ret
.global _start
.type _start, @function
_start:
/*Setup the stack pointer to point to the beginning of our stack */
/* I believe its a high address growing down to lower adress for the stack on x86*/
mov $stack_top, %esp
/*Reset EFLAGS*/
pushl $0
popf
/* push the pointer to the Multiboot information structure*/
pushl %ebx
/* push the magic value */
pushl %eax
call early_main
cli
load_gdt:
lgdt gdt
# set the segment selecters
movw $0x10, %ax
movw %ax, %ds
movw %ax, %es
movw %ax, %fs
movw %ax, %gs
movw %ax, %ss
ljmp $0x08, $flush
flush:
#load idt
call init_idt
sti
# Try enable A20
# mov $0x2401, %ax
# int $0x15
# enable protected mode
mov %cr0, %eax
or $1, %eax
mov %eax, %cr0
call kernel_main
cli
1: hlt
jmp 1b
/* Tell processor to use our gdt*/
gdt:
.word (gdt_end - gdt_start -1) # Size of the GDT in bytes minus 1 for math reasons
.int gdt_start # linear address of our GDT
.att_syntax
.size _start, . - _start
/*
* Create the GDT
*/
.section .data
gdt_start:
gdt_null:
.long 0x0
.long 0x0
gdt_kcode:
.word 0xFFFF # limit
.word 0x0 # base
.byte 0x0 # base
.byte 0b10011010 # 1st flags | type flags
.byte 0b11001111 # 2nd flags | limit
.byte 0x0 # base
gdt_kdata:
.word 0xFFFF # limit
.word 0x0 # base
.byte 0x0 # base
.byte 0b10010010 # 1st flags | type flags
.byte 0b11001111 # 2nd flags | limit
.byte 0x0 # base
gdt_end:

View File

@ -1,31 +0,0 @@
#include "gdtc.h"
#include "../tty/kterm.h"
gdtEntry_t gdt[3];
void gdtSetGate(int num, uint64_t base, uint64_t limit, uint8_t access,
uint8_t gran){
gdt[num].lBase = (base & 0xFFFF);
gdt[num].mBase = (base >> 16) & 0xFF;
gdt[num].hBase = (base >> 24) & 0xFF;
gdt[num].lLimit = (limit & 0xFFFF);
gdt[num].granularity = ((limit >> 16) & 0x0F);
gdt[num].granularity |= (gran & 0xF0);
gdt[num].access = access;
}
void setupGdt(){
printf("setupGdt is called!");
gdtPointer.limit = (sizeof(gdtEntry_t) * 3) - 1;
gdtPointer.base = &gdt;
gdtSetGate(0, 0, 0, 0, 0);
gdtSetGate(1, 0, 0xFFFFFFFF, 0x9A, 0xCF);
gdtSetGate(2, 0, 0xFFFFFFFF, 0x92, 0xCF);
loadGdt();
}

View File

@ -1,20 +0,0 @@
#include <stdint.h>
extern "C"{
typedef struct {
uint16_t lLimit;
uint16_t lBase;
uint8_t mBase;
uint8_t access;
uint8_t granularity;
uint8_t hBase;
} gdtEntry_t;
struct {
uint16_t limit;
uint32_t base;
} gdtPointer;
extern void loadGdt();
void setupGdt();
}

87
src/kernel/boot.S Normal file
View File

@ -0,0 +1,87 @@
/*
* Multiboot
*/
.set ALIGN, 1<<0 /* align loaded modules on page boundaries */
.set MEMINFO, 1<<1 /* provide memory map */
.set VIDEO, 1<<2 /* provide video mode */
.set FLAGS, ALIGN | MEMINFO | VIDEO /* this is the Multiboot 'flag' field */
.set MAGIC, 0x1BADB002 /* 'magic number' lets bootloader find the header */
.set CHECKSUM, -(MAGIC + FLAGS) /* checksum of above, to prove we are multiboot */
.section .multiboot
.align 4
.long MAGIC
.long FLAGS
.long CHECKSUM
.long 0 # unused
.long 0 # .
.long 0 # .
.long 0 # .
.long 0 # unused
.long 0 # set graphics mode
.long 800 # screen witdh
.long 600 # screen height
.long 32 # bpp
.section .bss
.align 16
stack_bottom:
.skip 16384 # 16 KiB
stack_top:
.section .text
.include "./src/kernel/irs_table.s"
.include "./src/kernel/irq_table.s"
.include "./src/kernel/idt/idt.s"
.include "./src/kernel/paging.s"
.global _start
.type _start, @function
_start:
/*Setup the stack pointer to point to the beginning of our stack */
/* I believe its a high address growing down to lower adress for the stack on x86*/
mov $stack_top, %esp
/*Reset EFLAGS*/
pushl $0
popf
/* push the pointer to the Multiboot information structure*/
pushl %ebx
/* push the magic value */
pushl %eax
call early_main
cli
.include "./src/kernel/gdt/gdt.s"
loadIDT:
#load idt
call init_idt
sti
# Try enable A20
# mov $0x2401, %ax
# int $0x15
# enable protected mode
mov %cr0, %eax
or $1, %eax
mov %eax, %cr0
call kernel_main
cli
1: hlt
jmp 1b
.size _start, . - _start

View File

@ -1,73 +1,108 @@
#pragma once #pragma once
#include "bootloader/multiboot.h" #include "bootloader/multiboot.h"
#define CHECK_FLAG(flags, bit) ((flags) & (1 <<(bit))) #define CHECK_FLAG(flags, bit) ((flags) & (1 <<(bit)))
#include "../gui/window.h"
extern "C" { #include "../gui/cursor.h"
#include "arch/i386/tty/kterm.h" #include "tty/kterm.h"
} #include "drivers/vesa/vesa.h"
void CheckMBT ( multiboot_info_t* mbt ){ void CheckMBT ( multiboot_info_t* mbt ){
/* Set MBI to the addresss of the multiboot information structure*/ /* Set MBI to the addresss of the multiboot information structure*/
multiboot_info_t * mbi = (multiboot_info_t *) mbt; multiboot_info_t * mbi = (multiboot_info_t *) mbt;
/* Print out the flags */ /* Print out the flags */
printf("flags = 0x%x\n", (unsigned) mbi->flags); printf("flags = 0x%8x\n", (unsigned) mbi->flags);
/* Are mem_* valid? */ /* Are mem_* valid? */
if ( CHECK_FLAG(mbi->flags,0)){ if ( CHECK_FLAG(mbi->flags,0)){
printf("mem_lower = %uKB, mem_upper = %uKB\n"); printf("mem_lower = %uKB, mem_upper = %uKB\n");
} }
/* is boot device valid ? */ /* is boot device valid ? */
if (CHECK_FLAG (mbi->flags, 1)){ if (CHECK_FLAG (mbi->flags, 1)){
printf("boot_device = 0x0%x\n", (unsigned) mbi->boot_device); printf("boot_device = 0x0%x\n", (unsigned) mbi->boot_device);
} }
/* is the command line passed? */ /* is the command line passed? */
if (CHECK_FLAG ( mbi->flags,2)){ if (CHECK_FLAG ( mbi->flags,2)){
printf("cmdline = %s\n", (char *) mbi->cmdline); printf("cmdline = %s\n", (char *) mbi->cmdline);
} }
/* Are mods_* valid? */ /* Are mods_* valid? */
if(CHECK_FLAG ( mbi->flags, 3)){ if(CHECK_FLAG ( mbi->flags, 3)){
multiboot_module_t *mod; multiboot_module_t *mod;
uint32_t i; uint32_t i;
printf("mods count = %d, mods_addr = 0x%x\n", (int) mbi->mods_count, (int) mbi->mods_addr); printf("mods count = %d, mods_addr = 0x%x\n", (int) mbi->mods_count, (int) mbi->mods_addr);
for(i = 0, mod = (multiboot_module_t *) mbi->mods_addr; i < mbi->mods_count; i++ , mod++){ for(i = 0, mod = (multiboot_module_t *) mbi->mods_addr; i < mbi->mods_count; i++ , mod++){
printf(" mod start = 0x%x, mod_end = 0x%x, cmdline = %s\n", (unsigned) mod->mod_start, (unsigned) mod->mod_end, (char*) mod->cmdline); printf(" mod start = 0x%x, mod_end = 0x%x, cmdline = %s\n", (unsigned) mod->mod_start, (unsigned) mod->mod_end, (char*) mod->cmdline);
} }
} }
/* Bits 4 and 5 are mutually exclusive! */ /* Bits 4 and 5 are mutually exclusive! */
if (CHECK_FLAG (mbi->flags, 4) && CHECK_FLAG(mbi->flags, 5)){ if (CHECK_FLAG (mbi->flags, 4) && CHECK_FLAG(mbi->flags, 5)){
printf("Both bits 4 and 5 are set.\n"); printf("Both bits 4 and 5 are set.\n");
return; return;
} }
/* Is the symbol table of a.out valid? */ /* Is the symbol table of a.out valid? */
if (CHECK_FLAG(mbi->flags, 4)){ if (CHECK_FLAG(mbi->flags, 4)){
multiboot_aout_symbol_table_t *multiboot_aout_sym = &(mbi->u.aout_sym); multiboot_aout_symbol_table_t *multiboot_aout_sym = &(mbi->u.aout_sym);
printf( "multiboot_aout_symbol_table: tabsize = 0x%0x, strsize = 0x%x, addr = 0x%x\n", printf( "multiboot_aout_symbol_table: tabsize = 0x%0x, strsize = 0x%x, addr = 0x%x\n",
(unsigned) multiboot_aout_sym->tabsize, (unsigned) multiboot_aout_sym->tabsize,
(unsigned) multiboot_aout_sym->strsize, (unsigned) multiboot_aout_sym->strsize,
(unsigned) multiboot_aout_sym->addr); (unsigned) multiboot_aout_sym->addr);
} }
/* Is the section header table of ELF valid? */ /* Is the section header table of ELF valid? */
if (CHECK_FLAG(mbi->flags, 5)){ if (CHECK_FLAG(mbi->flags, 5)){
multiboot_elf_section_header_table_t *multiboot_elf_sec = &(mbi->u.elf_sec); multiboot_elf_section_header_table_t *multiboot_elf_sec = &(mbi->u.elf_sec);
printf("multiboot_elf_sec: num = %u, size = 0x%x, addr = 0x%x, shnd = 0x%x\n", printf("multiboot_elf_sec: num = %u, size = 0x%x, addr = 0x%x, shnd = 0x%x\n",
(unsigned) multiboot_elf_sec->num, (unsigned) multiboot_elf_sec->size, (unsigned) multiboot_elf_sec->num, (unsigned) multiboot_elf_sec->size,
(unsigned) multiboot_elf_sec->addr, (unsigned) multiboot_elf_sec->shndx); (unsigned) multiboot_elf_sec->addr, (unsigned) multiboot_elf_sec->shndx);
} }
/* Draw diagonal blue line */
if (CHECK_FLAG (mbt->flags, 11)){
printf("Can draw!");
// Init vesa driver
initVBEDevice(mbt);
// Fill screen with blue
// colours AARRGGBB
drawRect(0, 0 , VbeModeInfo->width,VbeModeInfo->height, 0xFF0000FF);
// Create two windows
Rect rect_window1 {};
Rect rect_window2 {};
rect_window1.height =200;
rect_window1.width = 300;
rect_window1.x = 50;
rect_window1.y = 50;
rect_window2.height =200;
rect_window2.width = 300;
rect_window2.x = 300;
rect_window2.y = 200;
Window window_1 ( rect_window1, 0xFF00F0FF);
Window window_2 (rect_window2, 0xFFAACCDD);
window_1.draw();
window_2.draw();
Cursor cursor (70,100);
cursor.draw();
}
} }

View File

@ -0,0 +1,136 @@
#include "vesa.h"
VbeInfoBlock* vbeInfo;
vbe_mode_info_structure* VbeModeInfo;
void initVBEDevice(multiboot_info_t* mbt){
print_serial("initVBEDevice");
vbeInfo = (VbeInfoBlock*) mbt->vbe_control_info;
printf_serial("Signature: %s, V0x%x\n", vbeInfo->VbeSignature, vbeInfo->VbeVersion);
VbeModeInfo = (vbe_mode_info_structure*) mbt->vbe_mode_info;
printf_serial("VESA video mode info: Width: %d Height: %d BPP: %d\n", VbeModeInfo->width, VbeModeInfo->height , VbeModeInfo->bpp);
printf_serial("VideoMemory Location: 0x%x \n", VbeModeInfo->framebuffer );
}
void putPixel( int x, int y , uint32_t colour){
// printf_serial("putPixel x: %d, y: %d\n", x, y);
///fb + mbt->framebuffer_pitch * y + 4 * x ,NOTE: this calculation is very important
*(uint32_t*) ( VbeModeInfo->framebuffer + VbeModeInfo->pitch * y + 4 * x ) = colour;
}
void drawLine(int x1, int y1, int x2, int y2, uint32_t colour ){
print_serial("drawline\n");
// See Bresenham's line algorithm
int deltaX = x2 - x1;
int deltaY = y2 - y1;
int D = 2 * deltaY - deltaX;
int y = y1;
for ( int x = x1; x < x2; x++){
putPixel(x,y, colour);
if( D > 0){
y +=1;
D = D - 2 * deltaX;
}
D = D + 2 * deltaY;
}
}
void drawRect ( int x, int y, int width, int height, uint32_t colour ){
print_serial("drawRect\n");
for ( int i = x; i < x + width; i ++)
{
for(int j = y; j < y + height; j++){
putPixel(i,j, colour);
}
}
}
void blueDiagnalLineTest(multiboot_info_t* mbt){
multiboot_uint32_t color;
unsigned i;
void *fb = (void *) (unsigned long) mbt->framebuffer_addr;
switch (mbt->framebuffer_type)
{
case MULTIBOOT_FRAMEBUFFER_TYPE_INDEXED:
{
unsigned best_distance, distance;
struct multiboot_color *palette;
palette = (struct multiboot_color *) mbt->framebuffer_palette_addr;
color = 0;
best_distance = 4*256*256;
for (i = 0; i < mbt->framebuffer_palette_num_colors; i++)
{
distance = (0xff - palette[i].blue) * (0xff - palette[i].blue)
+ palette[i].red * palette[i].red
+ palette[i].green * palette[i].green;
if (distance < best_distance)
{
color = i;
best_distance = distance;
}
}
}
break;
case MULTIBOOT_FRAMEBUFFER_TYPE_RGB:
color = ((1 << mbt->framebuffer_blue_mask_size) - 1)
<< mbt->framebuffer_blue_field_position;
break;
case MULTIBOOT_FRAMEBUFFER_TYPE_EGA_TEXT:
color = '\\' | 0x0100;
break;
default:
color = 0xffffffff;
break;
}
for (i = 0; i < mbt->framebuffer_width
&& i < mbt->framebuffer_height; i++)
{
switch (mbt->framebuffer_bpp)
{
case 8:
{
multiboot_uint8_t *pixel = (multiboot_uint8_t*)fb + mbt->framebuffer_pitch * i + i;
*pixel = color;
}
break;
case 15:
case 16:
{
multiboot_uint16_t *pixel
= (multiboot_uint16_t*)fb + mbt->framebuffer_pitch * i + 2 * i;
*pixel = color;
}
break;
case 24:
{
multiboot_uint32_t *pixel
= (multiboot_uint32_t*)fb + mbt->framebuffer_pitch * i + 3 * i;
*pixel = (color & 0xffffff) | (*pixel & 0xff000000);
}
break;
case 32:
{
multiboot_uint32_t *pixel
= (multiboot_uint32_t*)fb + mbt->framebuffer_pitch * i + 4 * i;
*pixel = color;
}
break;
}
}
}

View File

@ -0,0 +1,66 @@
#pragma once
#include <stdint.h>
#include "../../bootloader/multiboot.h"
#include "../../serial.h"
struct vbe_mode_info_structure{
uint16_t attributes;
uint8_t window_a;
uint8_t window_b;
uint16_t granularity;
uint16_t window_size;
uint16_t segment_a;
uint16_t segment_b;
uint32_t win_func_ptr;
uint16_t pitch;
uint16_t width;
uint16_t height;
uint8_t w_char;
uint8_t y_char;
uint8_t planes;
uint8_t bpp;
uint8_t banks;
uint8_t memory_model;
uint8_t bank_size;
uint8_t image_pages;
uint8_t reserved0;
uint8_t red_mask;
uint8_t red_position;
uint8_t green_mask;
uint8_t green_position;
uint8_t blue_mask;
uint8_t blue_position;
uint8_t reserved_mask;
uint8_t reserved_position;
uint8_t direct_color_attributes;
uint32_t framebuffer;
uint32_t off_screen_mem_off;
uint16_t off_screen_mem_size;
uint8_t reserved1[206];
}__attribute__((packed));
struct VbeInfoBlock {
char VbeSignature[4];
uint16_t VbeVersion;
uint16_t OemStringPtr;
uint8_t Capabilities;
uint16_t VideoModePtr;
uint16_t TotalMemory;
}__attribute__((packed));
extern VbeInfoBlock* vbeInfo;
extern vbe_mode_info_structure* VbeModeInfo;
void initVBEDevice(multiboot_info_t* mbt);
void blueDiagnalLineTest(multiboot_info_t* mbt);
// Primitive drawing functions
void putPixel( int x, int y , uint32_t colour);
void drawLine(int x1, int y1, int x2, int y2, uint32_t colour );
void drawRect ( int x, int y, int width, int height, uint32_t colour );

19
src/kernel/gdt/gdt.s Normal file
View File

@ -0,0 +1,19 @@
.global LoadGlobalDescriptorTable
LoadGlobalDescriptorTable:
lgdt gdtDescriptor
movw $16, %ax
movw %ax, %ds
movw %ax, %es
movw %ax, %fs
movw %ax, %gs
movw %ax, %ss
jmp $8,$flush
flush:
ret

62
src/kernel/gdt/gdtc.cpp Normal file
View File

@ -0,0 +1,62 @@
#include "gdtc.h"
#include "../tty/kterm.h"
#define NULL_SEGMENT 0
#define KERNEL_CODE_SEGMENT 1
#define KERNEL_DATA_SEGMENT 2
#define USER_CODE_SEGMENT 3
#define USER_DATA_SEGMENT 4
SegmentDescriptor GlobalDescriptorTable[5];
GlobalDescriptorTableDescriptor gdtDescriptor;
void add_descriptor(int which , unsigned long base, unsigned long limit, unsigned char access, unsigned char granularity ){
GlobalDescriptorTable[which].base_low = (base & 0xFFFF );
GlobalDescriptorTable[which].base_middle = (base >> 6) & 0xFF;
GlobalDescriptorTable[which].base_high = (base >> 24) & 0xFF;
GlobalDescriptorTable[which].limit_low = (limit & 0xFFFF);
GlobalDescriptorTable[which].granularity = ((limit >> 16) & 0x0F);
GlobalDescriptorTable[which].granularity |= (granularity & 0xF0);
GlobalDescriptorTable[which].access = access;
}
void initGDT(){
// NULL segment
add_descriptor(NULL_SEGMENT, 0,0,0,0);
// Kernel Code Segment
add_descriptor(KERNEL_CODE_SEGMENT, 0, 0xFFFFFFFF, 0x9A, 0xCF);
// Kernel Data Segment
add_descriptor(KERNEL_DATA_SEGMENT, 0, 0xFFFFFFFF, 0x92, 0xCF);
// User Code Segment
// TODO:
// User Data Segement
// TODO:
// init Gdt Descriptor
gdtDescriptor.limit = ((sizeof(SegmentDescriptor ) * 5 ) - 1);
gdtDescriptor.base = (unsigned int) &GlobalDescriptorTable;
printf("Hello GDT!\n");
LoadGlobalDescriptorTable();
// while (true)
// asm volatile("hlt");
}

27
src/kernel/gdt/gdtc.h Normal file
View File

@ -0,0 +1,27 @@
#include <stdint.h>
struct SegmentDescriptor {
unsigned short limit_low;
unsigned short base_low;
unsigned char base_middle;
unsigned char access;
unsigned char granularity;
unsigned char base_high;
}__attribute__((packed));
struct GlobalDescriptorTableDescriptor{
unsigned short limit;
unsigned int base;
}__attribute__((packed));
extern SegmentDescriptor GlobalDescriptorTable[];
extern GlobalDescriptorTableDescriptor gdtDescriptor;
void add_descriptor(int which , unsigned long base, unsigned long limit, unsigned char access, unsigned char granularity );
extern "C" void LoadGlobalDescriptorTable();
void initGDT();

View File

@ -5,9 +5,7 @@
#include "../vga/colors.h" #include "../vga/colors.h"
#include "../pic/pic.h" #include "../pic/pic.h"
extern "C"{ #include "../tty/kterm.h"
#include "../tty/kterm.h"
}
extern "C" { extern "C" {

6
src/kernel/idt/idt.s Normal file
View File

@ -0,0 +1,6 @@
.globl idt_flush
idt_flush:
mov 4(%esp), %eax
lidt (%eax)
ret

138
src/kernel/irq_table.s Normal file
View File

@ -0,0 +1,138 @@
.globl irq0
irq0:
cli
push $0
push $0
jmp irq_common
.globl irq1
irq1:
cli
push $0
push $1
jmp irq_common
.globl irq2
irq2:
cli
push $0
push $2
jmp irq_common
.globl irq3
irq3:
cli
push $0
push $3
jmp irq_common
.globl irq4
irq4:
cli
push $0
push $4
jmp irq_common
.globl irq5
irq5:
cli
push $0
push $5
jmp irq_common
.globl irq6
irq6:
cli
push $0
push $6
jmp irq_common
.globl irq7
irq7:
cli
push $0
push $7
jmp irq_common
.globl irq8
irq8:
cli
push $0
push $8
jmp irq_common
.globl irq9
irq9:
cli
push $0
push $9
jmp irq_common
.globl irq10
irq10:
cli
push $0
push $10
jmp irq_common
.globl irq11
irq11:
cli
push $0
push $11
jmp irq_common
.globl irq12
irq12:
cli
push $0
push $12
jmp irq_common
.globl irq13
irq13:
cli
push $0
push $13
jmp irq_common
.globl irq14
irq14:
cli
push $0
push $14
jmp irq_common
.globl irq15
irq15:
cli
push $0
push $15
jmp irq_common
irq_common:
pusha
mov %ds, %ax
push %eax
mov $0x10, %ax
mov %ax, %ds
mov %ax, %es
mov %ax, %fs
mov %ax, %gs
call irq_handler
pop %eax
mov %ax, %ds
mov %ax, %es
mov %ax, %fs
mov %ax, %gs
popa
add $8, %esp # cleans push error and irs code
sti
iret # pops 5 things at once: CS, EIP, EFLAGS, SS, and ESP

256
src/kernel/irs_table.s Normal file
View File

@ -0,0 +1,256 @@
/*
* Interupt handlers
*/
.globl irs0
irs0:
cli
push $0
push $0
jmp irs_common
.globl irs1
irs1:
cli
push $0
push $1
jmp irs_common
.globl irs2
irs2:
cli
push $0
push $2
jmp irs_common
.globl irs3
irs3:
cli
push $0
push $3
jmp irs_common
.globl irs4
irs4:
cli
push $0
push $4
jmp irs_common
.globl irs5
irs5:
cli
push $0
push $5
jmp irs_common
.globl irs6
irs6:
cli
push $0
push $6
jmp irs_common
.globl irs7
irs7:
cli
push $0
push $7
jmp irs_common
.globl irs8
irs8:
cli
push $0
push $8
jmp irs_common
.globl irs9
irs9:
cli
push $0
push $9
jmp irs_common
.globl irs10
irs10:
cli
push $0
push $10
jmp irs_common
.globl irs11
irs11:
cli
push $0
push $11
jmp irs_common
.globl irs12
irs12:
cli
push $0
push $12
jmp irs_common
.globl irs13
irs13:
cli
push $13
jmp irs_common
.globl irs14
irs14:
cli
push $0
push $14
jmp irs_common
.globl irs15
irs15:
cli
push $0
push $15
jmp irs_common
.globl irs16
irs16:
cli
push $0
push $16
jmp irs_common
.globl irs17
irs17:
cli
push $0
push $17
jmp irs_common
.globl irs18
irs18:
cli
push $0
push $18
jmp irs_common
.globl irs19
irs19:
cli
push $0
push $19
jmp irs_common
.globl irs20
irs20:
cli
push $0
push $20
jmp irs_common
.globl irs21
irs21:
cli
push $0
push $21
jmp irs_common
.globl irs22
irs22:
cli
push $0
push $22
jmp irs_common
.globl irs23
irs23:
cli
push $0
push $23
jmp irs_common
.globl irs24
irs24:
cli
push $0
push $24
jmp irs_common
.globl irs25
irs25:
cli
push $0
push $25
jmp irs_common
.globl irs26
irs26:
cli
push $0
push $26
jmp irs_common
.globl irs27
irs27:
cli
push $0
push $27
jmp irs_common
.globl irs28
irs28:
cli
push $0
push $28
jmp irs_common
.globl irs29
irs29:
cli
push $0
push $29
jmp irs_common
.globl irs30
irs30:
cli
push $0
push $30
jmp irs_common
.globl irs31
irs31:
cli
push $0
push $31
jmp irs_common
irs_common:
pusha # Pushes edi,esi,ebp,esp,ebx,edx,ecx,eax
mov %ds, %ax
push %eax
/* load the kernel data segment descriptor*/
mov $0x10, %ax
mov %ax, %ds
mov %ax, %es
mov %ax, %fs
mov %ax, %gs
call irs_handler
pop %eax
mov %ax, %ds
mov %ax, %es
mov %ax, %fs
mov %ax, %gs
popa
add $8, %esp # cleans push error and irs code
sti
iret # pops 5 things at once: CS, EIP, EFLAGS, SS, and ESP

View File

@ -1,91 +1,60 @@
#include "kernel.h" #include "kernel.h"
#include "arch/i386/gdt/gdtc.h" #define GB4 524288
extern "C" { #define GB2 262144
extern "C" void kernel_main (void);
extern "C" void putPixel(int pos_x, int pos_y, unsigned char VGA_COLOR , unsigned char addr , unsigned char pixelWidth, unsigned pitch );
void early_main(unsigned long magic, unsigned long addr){
extern "C" void early_main(unsigned long magic, unsigned long addr){
/** initialize terminal interface */ /** initialize terminal interface */
kterm_init(); // kterm_init();
printf("Magic flag 0x%8x\n", magic);
printf("Magic must be 0x%8x\n", MULTIBOOT_BOOTLOADER_MAGIC);
if (magic != MULTIBOOT_BOOTLOADER_MAGIC){ if (magic != MULTIBOOT_BOOTLOADER_MAGIC){
printf("Invalid magic number: 0x%x\n", magic); printf("Invalid magic number: 0x%8x\n", magic);
return; return;
} }
CheckMBT( (multiboot_info_t *) addr); CheckMBT( (multiboot_info_t *) addr);
multiboot_info_t* mbt = (multiboot_info_t*) addr; multiboot_info_t* mbt = (multiboot_info_t*) addr;
// Map the kernel
//initPhysicalMemoryManager();
// AAAAAH memory map, Yes please!
/* Are mmap_* valid? */ /* Are mmap_* valid? */
if (CHECK_FLAG(mbt->flags, 6)){ if (CHECK_FLAG(mbt->flags, 6)){
multiboot_memory_map_t *mmap = (multiboot_memory_map_t*) mbt->mmap_addr; PhysicalMemoryManager_initialise( mbt->mmap_addr, GB2/* Seriously dangerous hardcoded memory value*/);
uint32_t memorySizeInBytes = 0; PhysicalMemoryManager_initialise_available_regions(mbt->mmap_addr, mbt->mmap_addr + mbt->mmap_length);
uint32_t reservedMemoryInBytes = 0; PhysicalMemoryManager_deinitialise_kernel();
extern uint8_t* kernel_begin;
extern uint8_t* kernel_end;
printf("Kernel MemoryMap:\n");
printf("mmap_addr = 0x%x, mmap_length = 0x%x\n", printf("kernel: 0x%x - 0x%x\n", &kernel_begin , &kernel_end);
(unsigned) mbt->mmap_addr, (unsigned) mbt->mmap_length);
for (; (unsigned long) mmap < mbt->mmap_addr + mbt->mmap_length; mmap = (multiboot_memory_map_t *) ((unsigned long) mmap + mmap->size + sizeof(mmap->size))){
if ( mmap->type == MULTIBOOT_MEMORY_AVAILABLE){
memorySizeInBytes += mmap->len;
} else {
reservedMemoryInBytes += mmap->len;
}
printf(
"size = 0x%x, base_addr = 0x%x%08x, length = 0x%x%08x, type = 0x%x\n",
(unsigned) mmap->size,
(unsigned) (mmap->addr >> 32),
(unsigned) (mmap->addr & 0xffffffff),
(unsigned) (mmap->len >> 32),
(unsigned) (mmap->len & 0xffffffff),
(unsigned) mmap->type);
}
uint32_t memorySizeInGiB = memorySizeInBytes / 1073741824;
printf("Available Memory: 0x%x bytes, 0x%x GiB\n", memorySizeInBytes, memorySizeInGiB );
printf("Reserved Memory: 0x%x bytes\n", reservedMemoryInBytes );
} }
//int cpu_model = get_model(); initGDT();
//int local_apic = check_apic();
//printf( "CPU Model: %x, Local APIC: %D\n", cpu_model, local_apic);
/* Setup Paging and memory Managment*/ kernel_main();
//MMU MemoryManagementUnit = MMU();
//MemoryManagementUnit.enable(); // Warning: Causes triple page fault
//printf("Pages available: %9d\n", pmm_available());
/* Draw diagonal blue line */
if (CHECK_FLAG (mbt->flags, 12)){
printf("Can draw!");
}
//setupGdt();
} }
void kernel_main (void) { extern "C" void kernel_main (void) {
printf("call to init serial\n");
init_serial(); init_serial();
while (false){
while (true){
//Read time indefinetely //Read time indefinetely
read_rtc(); read_rtc();
printf( "UTC time: %02d-%02d-%02d %02d:%02d:%02d [ Formatted as YY-MM-DD h:mm:ss]\r" ,year, month, day, hour, minute, second); printf( "UTC time: %02d-%02d-%02d %02d:%02d:%02d [ Formatted as YY-MM-DD h:mm:ss]\r" ,year, month, day, hour, minute, second);
delay(1000); delay(1000);
} }
} }
}

View File

@ -1,19 +1,24 @@
#pragma once #pragma once
extern "C" { extern "C"{
#include "arch/i386/tty/kterm.h" #include "../libc/include/string.h"
} }
#include "vga/VBE.h"
#include "tty/kterm.h"
#include "../libc/include/string.h"
#include "./bootloader/multiboot.h" #include "./bootloader/multiboot.h"
#include "bootcheck.h" #include "bootcheck.h"
#include "arch/i386/idt/idt.h" #include "memory/PhysicalMemoryManager.h"
#include "gdt/gdtc.h"
#include "idt/idt.h"
#include "io.h" #include "io.h"
#include "time.h" #include "time.h"
#include "cpu.h" #include "cpu.h"
#include "arch/i386/vga/VBE.h" #include "serial.h"
#define CHECK_FLAG(flags, bit) ((flags) & (1 <<(bit)))
#define CHECK_FLAG(flags, bit) ((flags) & (1 <<(bit)))
#define PANIC(message) { return; }
/* This needs to be moved! */ /* This needs to be moved! */
@ -26,89 +31,3 @@ void delay(int t){
for(j=0;j<25000;j++) for(j=0;j<25000;j++)
asm("NOP"); asm("NOP");
} }
class Test {
public:
Test();
void printMe();
~Test();
};
Test::Test(){
kterm_writestring("Create a test object\n");
};
void Test::printMe(){
kterm_writestring("testObject.printMe()\n");
}
Test::~Test(){
kterm_writestring("Destroy testObject! Bye bye\n");
}
#define PORT 0x3f8
static int init_serial() {
outb(PORT + 1, 0x00); // Disable all interrupts
outb(PORT + 3, 0x80); // Enable DLAB (set baud rate divisor)
outb(PORT + 0, 0x03); // Set divisor to 3 (lo byte) 38400 baud
outb(PORT + 1, 0x00); // (hi byte)
outb(PORT + 3, 0x03); // 8 bits, no parity, one stop bit
outb(PORT + 2, 0xC7); // Enable FIFO, clear them, with 14-byte threshold
outb(PORT + 4, 0x0B); // IRQs enabled, RTS/DSR set
outb(PORT + 4, 0x1E); // Set in loopback mode, test the serial chip
outb(PORT + 0, 0xAE); // Test serial chip (send byte 0xAE and check if serial returns same byte)
// Check if serial is faulty (i.e: not same byte as sent)
if(inb(PORT + 0) != 0xAE) {
return 1;
}
// If serial is not faulty set it in normal operation mode
// (not-loopback with IRQs enabled and OUT#1 and OUT#2 bits enabled)
outb(PORT + 4, 0x0F);
return 0;
}
int is_transmit_empty() {
return inb(PORT + 5) & 0x20;
}
void write_serial(char a) {
while (is_transmit_empty() == 0);
outb(PORT,a);
}
int serial_received() {
return inb(PORT + 5) & 1;
}
char read_serial() {
while (serial_received() == 0);
return inb(PORT);
}
void print_serial(const char* string ){
for(size_t i = 0; i < strlen(string); i ++){
write_serial(string[i]);
}
}
void test_serial(){
/** Serial test **/
kterm_writestring("Writing to COM1 serial port:");
init_serial();
write_serial('A');
write_serial('B');
write_serial('C');
write_serial('D');
write_serial('E');
char Character_received = read_serial();
kterm_writestring("\n");
kterm_writestring("received from COM 1: \n");
kterm_put(Character_received);
kterm_writestring("\n");
}

View File

@ -0,0 +1,40 @@
#pragma once
#include <stddef.h>
#include <stdint.h>
inline void bitmap_set( uint32_t* map , int index )
{
map[index/32] |= (1 << (index % 32));
}
inline void bitmap_unset(uint32_t* map , int index)
{
map[index/32] &= ~(1 << (index % 32));
}
inline int bitmap_first_unset( uint32_t* map , int size)
{
uint32_t rem_bits = size % 32;
for(uint32_t i = 0; i < size/32; i++)
{
if(map[i] != 0xFFFFFFFF){
for(int j = 0; j < 32; j++){
if(!(map[i] & (1<< j))){
return (i*32) + j;
}
}
}
}
if(rem_bits){
for(uint32_t j = 0; j < rem_bits; j++){
if(!(map[size/32] & (1 << j ))){
return size + j; // Original author divided size by 32 and then multiplied it by 32 which is a net zero calculation ?!?
}
}
}
return -1;
}

View File

@ -4,7 +4,7 @@
void PageDirectory::enable(){ void PageDirectory::enable(){
// https://wiki.osdev.org/Setting_Up_Paging
//set each entry to not present //set each entry to not present
int i; int i;
for(i = 0; i < 1024; i++) for(i = 0; i < 1024; i++)

View File

@ -1 +0,0 @@
#include "PageFrameAllocator.h"

View File

@ -0,0 +1,38 @@
#include "PageFrameAllocator.h"
MemoryInfo memInfo {};
void mapMultibootMemoryMap( multiboot_info_t *mbt){
printf("mmap_addr = 0x%x, mmap_length = 0x%x\n",
(unsigned) mbt->mmap_addr, (unsigned) mbt->mmap_length);
multiboot_memory_map_t *mmap = (multiboot_memory_map_t*) mbt->mmap_addr;
for (; (unsigned long) mmap < mbt->mmap_addr + mbt->mmap_length; mmap = (multiboot_memory_map_t *) ((unsigned long) mmap + mmap->size + sizeof(mmap->size))){
if ( mmap->type == MULTIBOOT_MEMORY_AVAILABLE){
memInfo.memorySizeInBytes += mmap->len;
} else {
memInfo.reservedMemoryInBytes += mmap->len;
}
print_Multiboot_memory_Map(mmap);
}
uint32_t memorySizeInGiB = memInfo.memorySizeInBytes / 1073741824;
printf("Available Memory: 0x%x bytes, 0x%x GiB\n", memInfo.memorySizeInBytes, memorySizeInGiB );
printf("Reserved Memory: 0x%x bytes\n", memInfo.reservedMemoryInBytes );
}
void print_Multiboot_memory_Map(multiboot_memory_map_t* mmap){
printf(
"size = 0x%x, base_addr = 0x%x%08x, length = 0x%x%08x, type = 0x%x\n",
(unsigned) mmap->size,
(unsigned) (mmap->addr >> 32),
(unsigned) (mmap->addr & 0xffffffff),
(unsigned) (mmap->len >> 32),
(unsigned) (mmap->len & 0xffffffff),
(unsigned) mmap->type
);
}

View File

@ -1,8 +1,20 @@
#pragma once #pragma once
#include <stdint.h> #include "../arch/i386/tty/kterm.h"
#include <stdint.h>
#include "../bootloader/multiboot.h"
struct MemoryInfo{
uint32_t memorySizeInBytes = 0;
uint32_t reservedMemoryInBytes = 0;
};
extern void *kernel_begin; extern void *kernel_begin;
extern void *kernel_end; extern void *kernel_end;
void print_Multiboot_memory_Map(multiboot_memory_map_t*);
void mapMultibootMemoryMap(multiboot_info_t*);

View File

@ -0,0 +1,118 @@
#include "PhysicalMemoryManager.h"
size_t mem_size = 0;
int used_blocks = 0;
size_t max_blocks = 0;
uint32_t* pmmap = 0;
size_t pmmap_size = 0;
void PhysicalMemoryManager_initialise(uint32_t physicalmemorymap_address, size_t size )
{
mem_size = size;
max_blocks = KB_TO_BLOCKS(mem_size);
used_blocks = max_blocks;
pmmap = (uint32_t*) physicalmemorymap_address;
if(max_blocks % BLOCKS_PER_WORD)
pmmap_size++;
memset(pmmap, 0xFF, pmmap_size);
}
void PhysicalMemoryManager_region_initialise(uint32_t base, size_t size)
{
size_t blocks = size /BLOCK_SIZE;
uint32_t align = base / BLOCK_SIZE;
for(size_t i = 0 ; i < blocks; i ++)
{
bitmap_unset(pmmap, align++);
used_blocks--;
}
bitmap_set(pmmap, 0);
}
void PhysicalMemoryManager_region_deinitialise(uint32_t base, size_t size )
{
size_t blocks = size / BLOCK_SIZE;
uint32_t align = base / BLOCK_SIZE;
for(size_t i = 0 ; i < blocks; i++ )
{
bitmap_set(pmmap, align++);
used_blocks++;
}
}
void PhysicalMemoryManager_initialise_available_regions(uint32_t mmap_, uint32_t mmap_end_)
{
multiboot_memory_map_t *mmap = (multiboot_memory_map_t*)mmap_;
multiboot_memory_map_t *mmap_end= (multiboot_memory_map_t*) mmap_end_;
for(int i = 0; mmap < mmap_end ; mmap++, i++)
{
if(mmap->type == MULTIBOOT_MEMORY_AVAILABLE)
{
PhysicalMemoryManager_region_initialise((uint32_t) mmap->addr, (size_t) mmap->len);
}
}
}
void PhysicalMemoryManager_deinitialise_kernel()
{
extern uint8_t kernel_begin;
extern uint8_t kernel_end;
size_t kernel_size = (size_t) &kernel_end - (size_t) &kernel_begin;
uint32_t pmmap_size_aligned = pmmap_size;
if(!IS_ALIGNED(pmmap_size_aligned, BLOCK_SIZE))
{
pmmap_size_aligned = ALIGN(pmmap_size_aligned, BLOCK_SIZE);
}
PhysicalMemoryManager_region_deinitialise((uint32_t) &kernel_begin, kernel_size);
PhysicalMemoryManager_region_deinitialise((uint32_t) &kernel_end, pmmap_size_aligned);
}
void* PhysicalMemoryManager_allocate_block()
{
if(used_blocks - max_blocks <= 0)
{
return 0;
}
int p_index = bitmap_first_unset(pmmap, p_index );
if(p_index == -1){
return 0;
}
bitmap_set(pmmap, p_index);
used_blocks++;
return (void*) (BLOCK_SIZE * p_index);
}
void PhysicalMemoryManager_free_block(void* p){
if(p==0){
return ;
}
uint32_t p_addr = (uint32_t) p;
int index = p_addr / BLOCK_SIZE;
bitmap_unset(pmmap, index);
used_blocks--;
}

View File

@ -0,0 +1,34 @@
#pragma once
#include "../bootloader/multiboot.h"
#include <stdint.h>
#include <stddef.h>
#include "../../libc/include/mem.h"
#include "../kstructures/bitmap.h"
#define BLOCK_SIZE 4092
#define BLOCKS_PER_WORD 32
#define KB_TO_BLOCKS(x) (((x) * 1024 ) / BLOCK_SIZE)
#define IS_ALIGNED(addr, align) !((addr) & ~((align) - 1))
#define ALIGN(addr, align) (((addr) & ~((align) - 1 )) + (align))
extern void PhysicalMemoryManager_initialise(uint32_t, size_t);
extern void PhysicalMemoryManager_region_initialise(uint32_t, size_t);
extern void PhysicalMemoryManager_region_deinitialise(uint32_t,size_t);
extern void PhysicalMemoryManager_initialise_available_regions(uint32_t, uint32_t);
extern void PhysicalMemoryManager_deinitialise_kernel();
extern void* PhysicalMemoryManager_allocate_block();
extern void PhysicalMemoryManager_free_block(void* p);
extern size_t mem_size;
extern int used_blocks;
extern size_t max_blocks;
extern uint32_t* pmmap;
extern size_t pmmap_size ;

20
src/kernel/paging.s Normal file
View File

@ -0,0 +1,20 @@
.globl enablePaging
enablePaging:
push %ebp
mov %esp, %ebp
mov %cr0, %eax
or $0x80000000, %eax
mov %eax, %cr0
mov %ebp, %esp
pop %ebp
ret
.globl loadPageDirectory
loadPageDirectory:
push %ebp
mov %esp, %ebp
mov 8(%esp), %eax
mov %eax, %cr3
mov %ebp, %esp
pop %ebp
ret

View File

@ -1,5 +1,5 @@
#pragma once #pragma once
#include "../../../io.h" #include "../io.h"
#define PIC1 0x20 /* IO base address for master PIC */ #define PIC1 0x20 /* IO base address for master PIC */
#define PIC2 0xA0 /* IO base address for slave PIC */ #define PIC2 0xA0 /* IO base address for slave PIC */

133
src/kernel/serial.h Normal file
View File

@ -0,0 +1,133 @@
#pragma once
#include "tty/kterm.h"
#include "io.h"
#define PORT 0x3f8
inline static int init_serial() {
outb(PORT + 1, 0x00); // Disable all interrupts
outb(PORT + 3, 0x80); // Enable DLAB (set baud rate divisor)
outb(PORT + 0, 0x03); // Set divisor to 3 (lo byte) 38400 baud
outb(PORT + 1, 0x00); // (hi byte)
outb(PORT + 3, 0x03); // 8 bits, no parity, one stop bit
outb(PORT + 2, 0xC7); // Enable FIFO, clear them, with 14-byte threshold
outb(PORT + 4, 0x0B); // IRQs enabled, RTS/DSR set
outb(PORT + 4, 0x1E); // Set in loopback mode, test the serial chip
outb(PORT + 0, 0xAE); // Test serial chip (send byte 0xAE and check if serial returns same byte)
// Check if serial is faulty (i.e: not same byte as sent)
if(inb(PORT + 0) != 0xAE) {
return 1;
}
// If serial is not faulty set it in normal operation mode
// (not-loopback with IRQs enabled and OUT#1 and OUT#2 bits enabled)
outb(PORT + 4, 0x0F);
return 0;
}
inline int is_transmit_empty() {
return inb(PORT + 5) & 0x20;
}
inline void write_serial(char a) {
while (is_transmit_empty() == 0);
outb(PORT,a);
}
inline int serial_received() {
return inb(PORT + 5) & 1;
}
inline char read_serial() {
while (serial_received() == 0);
return inb(PORT);
}
inline void print_serial(const char* string ){
for(size_t i = 0; i < strlen(string); i ++){
write_serial(string[i]);
}
}
inline void printf_serial ( const char *format, ...) {
char **arg = (char **)&format;
int c;
char buf[20];
arg++;
while ((c = *format++) != 0){
if( c != '%')
write_serial(c);
else{
char *p, *p2;
int pad0 = 0, pad = 0;
c = *format++;
if(c =='0'){
pad0 = 1;
c = *format++;
}
if ( c >= '0' && c <= '9'){
pad = c - '0';
c = *format++;
}
switch (c)
{
case 'd':
case 'u':
case 'x':
itoa(buf, c, *((int *) arg++));
p = buf;
goto string;
break;
case 's':
p = *arg++;
if(!p)
p = "(null)";
string:
for (p2 = p; *p2; p2++);
for (; p2 < p + pad; p2++)
write_serial(pad0 ? '0': ' ');
while (*p)
write_serial(*p++);
break;
default:
write_serial(*((int *)arg++));
break;
}
}
}
}
inline void test_serial(){
/** Serial test **/
kterm_writestring("Writing to COM1 serial port:");
init_serial();
write_serial('A');
write_serial('B');
write_serial('C');
write_serial('D');
write_serial('E');
char Character_received = read_serial();
kterm_writestring("\n");
kterm_writestring("received from COM 1: \n");
kterm_put(Character_received);
kterm_writestring("\n");
}

View File

@ -137,7 +137,7 @@ void kterm_writestring(const char* data ){
} }
static void itoa (char *buf, int base, int d) { void itoa (char *buf, int base, int d) {
char *p = buf; char *p = buf;
char *p1, *p2; char *p1, *p2;
unsigned long ud = d; unsigned long ud = d;
@ -174,7 +174,7 @@ static void itoa (char *buf, int base, int d) {
} }
void printf ( const char *format, ...) { void printf ( const char *format, ...) {
return;
char **arg = (char **)&format; char **arg = (char **)&format;
int c; int c;
char buf[20]; char buf[20];
@ -202,10 +202,8 @@ void printf ( const char *format, ...) {
switch (c) switch (c)
{ {
case 'd': case 'd':
kterm_writestring("Not implemented!!");
break;
case 'u': case 'u':
break;
case 'x': case 'x':
itoa(buf, c, *((int *) arg++)); itoa(buf, c, *((int *) arg++));

View File

@ -5,10 +5,11 @@
#include <stdbool.h> #include <stdbool.h>
#include "../vga/colors.h" #include "../vga/colors.h"
#include "../../../io.h" #include "../io.h"
extern "C"{
#include "./../../../../libc/include/string.h" #include "./../../libc/include/string.h"
}
void kterm_init(); void kterm_init();
@ -32,10 +33,9 @@ uint16_t get_cursor_position();
int get_cursor_x (uint16_t cursor_pos); int get_cursor_x (uint16_t cursor_pos);
int get_cursor_y (uint16_t cursor_pos); int get_cursor_y (uint16_t cursor_pos);
extern "C" void itoa (char *buf, int base, int d);
void printf ( const char *format, ...); void printf ( const char *format, ...);
//static void itoa (char *buf, int base, int d);
#define KernelTag "[Kernel]: " #define KernelTag "[Kernel]: "
#define AS_KERNEL() ( kterm_setcolor(VGA_COLOR_LIGHT_BLUE),\ #define AS_KERNEL() ( kterm_setcolor(VGA_COLOR_LIGHT_BLUE),\

8
src/libc/include/mem.h Normal file
View File

@ -0,0 +1,8 @@
inline void* memset (void* ptr, int value, size_t num){
for( int i = 0; i < num; i++ )
{
int* data = (int*)ptr+ i;
*data = value;
}
return ptr;
}

0
todo.md Normal file
View File