Permalink
Browse files

Started work on the kernel. Physical memory manager done, builds virt…

…ual memory manager. Boots.
  • Loading branch information...
MessiahAndrw committed Sep 25, 2014
1 parent ce1e764 commit aeed54f1b266ff07daf610fb771157222fa2ba5e
Showing with 3,834 additions and 1,133 deletions.
  1. BIN Perception.iso
  2. +3 −0 build.sh
  3. +6 −0 fs/boot/grub/grub.cfg
  4. +1 −0 fs/efi.img
  5. +114 −0 kernel/boot.asm
  6. +26 −0 kernel/build.sh
  7. +15 −0 kernel/gdt.asm
  8. +35 −0 kernel/gdt.c
  9. +20 −0 kernel/gdt.h
  10. +7 −0 kernel/idt.asm
  11. +26 −0 kernel/idt.c
  12. +19 −0 kernel/idt.h
  13. +55 −0 kernel/io.c
  14. +9 −0 kernel/io.h
  15. +139 −0 kernel/irq.asm
  16. +81 −0 kernel/irq.c
  17. +10 −0 kernel/irq.h
  18. +277 −0 kernel/isr.asm
  19. +117 −0 kernel/isr.c
  20. +12 −0 kernel/isr.h
  21. +99 −0 kernel/keyboard.c
  22. +6 −0 kernel/keyboard.h
  23. +829 −0 kernel/liballoc.c
  24. +82 −0 kernel/liballoc.h
  25. +78 −0 kernel/liballoc_hooks.c
  26. +61 −0 kernel/linker.ld
  27. +57 −0 kernel/main.c
  28. +54 −0 kernel/multiboot.asm
  29. +368 −0 kernel/multiboot2.h
  30. +133 −0 kernel/physical_allocator.c
  31. +21 −0 kernel/physical_allocator.h
  32. +28 −0 kernel/process.h
  33. +4 −0 kernel/run.sh
  34. +154 −0 kernel/text_terminal.c
  35. +19 −0 kernel/text_terminal.h
  36. +43 −0 kernel/timer.c
  37. +8 −0 kernel/timer.h
  38. +6 −0 kernel/todo.txt
  39. +11 −0 kernel/types.h
  40. +112 −0 kernel/vfs.c
  41. +51 −0 kernel/vfs.h
  42. +591 −0 kernel/virtual_allocator.c
  43. +42 −0 kernel/virtual_allocator.h
  44. 0 perception/Header.h
  45. +0 −7 perception/Windows/.gitignore
  46. +0 −165 perception/Windows/Perception.vcxproj
  47. +0 −69 perception/Windows/Perception.vcxproj.filters
  48. +0 −11 perception/Windows/Perception.vcxproj.user
  49. +0 −35 perception/Windows/Windows.sln
  50. +0 −22 perception/Windows/main.cpp
  51. +0 −122 perception/Windows/platform.cpp
  52. +0 −3 perception/Windows/x64/.gitignore
  53. +0 −334 perception/keyboard.cpp
  54. +0 −118 perception/keyboard.h
  55. +0 −62 perception/mouse.cpp
  56. +0 −22 perception/mouse.h
  57. +0 −18 perception/perception.cpp
  58. +0 −5 perception/perception.h
  59. +0 −25 perception/platform.h
  60. +0 −30 perception/process.cpp
  61. +0 −53 perception/process.h
  62. +0 −9 perception/window.cpp
  63. +0 −22 perception/window.h
  64. +4 −0 run.sh
  65. +1 −1 turkey/.kdev4/turkey.kdev4
BIN +16 MB Perception.iso
Binary file not shown.
@@ -0,0 +1,3 @@
#!/bin/bash
grub-mkrescue -o Perception.iso fs/
@@ -0,0 +1,6 @@
set timeout=3
set default=0
menuentry "Perception" {
multiboot2 /boot/kernel.sys
boot
}
@@ -0,0 +1 @@
@@ -0,0 +1,114 @@
[BITS 32]
[SECTION .boot]
[GLOBAL EntryPoint]
[EXTERN Stack]
EntryPoint:
mov [MultibootInfo], eax
mov [MultibootInfo + 4], ebx
mov eax, Gdtr1
lgdt [eax]
push 0x08
push .GdtReady
retf
.GdtReady:
mov eax, 0x10
mov ds, ax
mov ss, ax
mov esp, Stack
call SetupPagingAndLongMode
mov eax, Gdtr2
lgdt [Gdtr2]
push 0x08
push Gdt2Ready
retf
[EXTERN Pml4]
[EXTERN Pdpt]
[EXTERN Pd]
SetupPagingAndLongMode:
mov eax, Pdpt
or eax, 1
mov [Pml4], eax
mov [Pml4 + 0xFF8], eax
mov eax, Pd
or eax, 1
mov [Pdpt], eax
mov [Pdpt + 0xFF0], eax
mov dword [Pd], 0x000083
mov dword [Pd + 8], 0x200083
mov dword [Pd + 16], 0x400083
mov dword [Pd + 24], 0x600083
; Load CR3 with PML4
mov eax, Pml4
mov cr3, eax
; Enable PAE
mov eax, cr4
or eax, 1 << 5
mov cr4, eax
; Enable Load Mode in the MSR
mov ecx, 0xC0000080
rdmsr
or eax, 1 << 8
wrmsr
; Enable paging
mov eax, cr0
or eax, 1 << 31
mov cr0, eax
ret
TmpGdt:
DQ 0x0000000000000000
DQ 0x00CF9A000000FFFF
DQ 0x00CF92000000FFFF
DQ 0x0000000000000000
DQ 0x00A09A0000000000
DQ 0x00A0920000000000
Gdtr1:
DW 23
DD TmpGdt
Gdtr2:
DW 23
DD TmpGdt + 24
DD 0
Gdtr3:
DW 23
DQ TmpGdt + 24 + 0xFFFFFFFF80000000
[GLOBAL MultibootInfo]
MultibootInfo:
dq 0 ; pointer and magic stuff from grub written here
[BITS 64]
[EXTERN kmain]
Gdt2Ready:
mov eax, 0x10
mov ds, ax
mov es, ax
mov ss, ax
mov rsp, Stack + 0xFFFFFFFF80000000
; mov gdtr to the upper zone
mov rax, Gdtr3
lgdt [rax]
mov rax, kmain
call rax
cli
hlt
jmp $
@@ -0,0 +1,26 @@
#!/bin/bash
NASM_CMD="nasm -felf64 -o"
GCC_CMD="gcc -m64 -mcmodel=kernel -ffreestanding -nostdlib -mno-red-zone -c -o"
$NASM_CMD boot.o boot.asm
# $NASM_CMD gdt_asm.o gdt.asm
# $GCC_CMD gdt.o gdt.c
# $NASM_CMD idt_asm.o idt.asm
# $GCC_CMD idt.o idt.c
$GCC_CMD io.o io.c
# $NASM_CMD irq_asm.o irq.asm
# $GCC_CMD irq.o irq.c
# $NASM_CMD isr_asm.o isr.asm
# $GCC_CMD isr.o isr.c
# $GCC_CMD keyboard.o keyboard.c
# $GCC_CMD liballoc.o liballoc.c
# $GCC_CMD liballoc_hooks.o liballoc_hooks.c
$GCC_CMD main.o main.c
$NASM_CMD multiboot.ao multiboot.asm
$GCC_CMD physical_allocator.o physical_allocator.c
$GCC_CMD text_terminal.o text_terminal.c
# $GCC_CMD timer.o timer.c
# $GCC_CMD vfs.o vfs.c
$GCC_CMD virtual_allocator.o virtual_allocator.c
ld -nodefaultlibs -T linker.ld -o ../fs/boot/kernel.sys multiboot.ao *.o
rm *.ao *.o
@@ -0,0 +1,15 @@
[BITS 64]
global _gdt_flush
extern _gp
_gdt_flush:
lgdt [_gp]
mov ax, 0x10 ; 0x10 is the offset in the GDT to our data segment
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax
mov ss, ax
jmp 0x08:flush2
flush2:
ret
@@ -0,0 +1,35 @@
#include "gdt.h"
struct gdt_entry gdt[3];
struct gdt_ptr gp;
void gdt_set_gate(int num, size_t base, size_t limit, unsigned char access, unsigned char gran) {
/* base address */
gdt[num].base_low = (base & 0xFFFF);
gdt[num].base_middle = (base >> 16) & 0xFF;
gdt[num].base_high = (base >> 24) & 0xFF;
/* descriptor limits */
gdt[num].limit_low = (limit & 0xFFFF);
gdt[num].granularity = ((limit >> 16) & 0xF);
/* granularity and access flags */
gdt[num].granularity |= (gran & 0xF0);
gdt[num].access = access;
}
void gdt_install() {
gp.limit = (sizeof(struct gdt_entry) * 3) - 1;
gp.base = (unsigned int)(size_t)&gdt;
/* null descriptor */
gdt_set_gate(0, 0, 0, 0, 0);
/* code segment */
gdt_set_gate(1, 0, 0xFFFFFFFF, 0x9A, 0xCf);
/* data segment */
gdt_set_gate(2, 0, 0xFFFFFFFF, 0x92, 0xCF);
gdt_flush();
}
@@ -0,0 +1,20 @@
#pragma once
#include "types.h"
struct gdt_entry {
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 gdt_ptr {
unsigned short limit;
unsigned int base;
} __attribute__ ((packed));
extern void gdt_flush();
extern void gdt_set_gate(int num, size_t base, size_t limit, unsigned char access, unsigned char gran);
extern void gdt_install();
@@ -0,0 +1,7 @@
[BITS 64]
global _idt_load
extern _idtp
_idt_load:
lidt [_idtp]
ret
@@ -0,0 +1,26 @@
#include "idt.h"
#include "io.h"
struct idt_entry idt[256];
struct idt_ptr idtp;
void idt_set_gate(unsigned char num, size_t base, unsigned short sel, unsigned char flags) {
/* base address */
idt[num].base_low = (base & 0xFFFF);
idt[num].base_high = (base >> 16) & 0xFFFF;
idt[num].sel = sel;
idt[num].always0 = 0;
idt[num].flags = flags;
}
void idt_install() {
idtp.limit = (sizeof (struct idt_entry) * 256) - 1;
idtp.base = (unsigned int)(size_t)&idt;
memset((unsigned char *)&idt, 0, sizeof(struct idt_entry) * 256);
/* add any new ISrs to the IDT here using idt_set_gate */
idt_load();
}
@@ -0,0 +1,19 @@
#pragma once
#include "types.h"
struct idt_entry {
unsigned short base_low;
unsigned short sel;
unsigned char always0;
unsigned char flags;
unsigned short base_high;
} __attribute__((packed));
struct idt_ptr {
unsigned short limit;
unsigned int base;
} __attribute__ ((packed));
extern void idt_load();
extern void idt_set_gate(unsigned char num, size_t base, unsigned short sel, unsigned char flags);
extern void idt_install();
@@ -0,0 +1,55 @@
#include "io.h"
void memcpy(unsigned char *dest, const unsigned char *src, size_t count) {
while(count > 0) {
*dest = *src;
dest++;
src++;
count--;
}
}
void memset(unsigned char *dest, unsigned char val, size_t count) {
while(count > 0) {
*dest = val;
dest++;
count--;
}
}
bool strcmp(void *a, void *b, size_t count) {
unsigned char *ac = (unsigned char*)a;
unsigned char *bc = (unsigned char*)b;
while(count > 0) {
if(*ac != *bc)
return true;
ac++;
bc++;
count--;
}
return false;
}
size_t strlen(const char *str) {
size_t count = 0;
while(*str) {
count++;
str++;
}
return count;
}
unsigned char inportb (unsigned short _port)
{
unsigned char rv;
__asm__ __volatile__ ("inb %1, %0" : "=a" (rv) : "dN" (_port));
return rv;
}
void outportb (unsigned short _port, unsigned char _data)
{
__asm__ __volatile__ ("outb %1, %0" : : "dN" (_port), "a" (_data));
}
@@ -0,0 +1,9 @@
#pragma once
#include "types.h"
extern void memcpy(unsigned char *dest, const unsigned char *src, size_t count);
extern void memset(unsigned char *dest, unsigned char val, size_t count);
extern bool strcmp(void *a, void *b, size_t count);
extern size_t strlen(const char *str);
extern unsigned char inportb (unsigned short _port);
extern void outportb (unsigned short _port, unsigned char _data);
Oops, something went wrong.

0 comments on commit aeed54f

Please sign in to comment.