Compare commits
68 Commits
BasicGraph
...
c9a036bfbb
Author | SHA1 | Date | |
---|---|---|---|
c9a036bfbb | |||
1f90a5d862 | |||
520104a43a | |||
7993a2d172 | |||
27e99fe4f2 | |||
749f2aa492 | |||
891085e151 | |||
364d10d02e | |||
68371475d9 | |||
16e2354019 | |||
a47879f404 | |||
656ca0baa8 | |||
01fcb0aa15 | |||
13e9beea79 | |||
9893a0bd17 | |||
a70ae5ca31 | |||
15443601a6 | |||
c90e90bd84 | |||
a5e7fdd07e | |||
59ba41f3d2 | |||
5051b8903c | |||
0f0fc9f252 | |||
e70f56a005 | |||
560dd64e64 | |||
9436e6e033 | |||
d280aa0584 | |||
7d6c823d79 | |||
bbfea39c23 | |||
3b3e2597a1 | |||
0b0e37b762 | |||
388ac8e7f9 | |||
9172da075a | |||
23c68d9863 | |||
2e2693d1ea | |||
a93bf566c8 | |||
2e59e6593e | |||
b4cff3e667 | |||
7330b81a10 | |||
97606dbf71 | |||
7496299761 | |||
0d8ef065e0 | |||
19b9cfe908 | |||
b8d75dddae | |||
fb2a19e11d | |||
72008b0a7a | |||
2621399349 | |||
767dac7e73 | |||
6d946ddce3 | |||
9173b90eb1 | |||
bd5d3f5d49 | |||
88cc1d75bb | |||
2db83b33e1 | |||
5a68f77b33 | |||
72438ae70d | |||
a36e3d1c16 | |||
08b97af863 | |||
5089da5e9e | |||
ec654143c6 | |||
5f39f7e7ed | |||
d455735ea2 | |||
9dc7a05da1 | |||
59bcc17668 | |||
5f50f8c013 | |||
595a7d5163 | |||
e84d196b00 | |||
f71a3a8ed6 | |||
63ea825e2e | |||
a094f510d3 |
2
.gitignore
vendored
2
.gitignore
vendored
@ -5,4 +5,6 @@ isodir/
|
||||
root/
|
||||
*.iso
|
||||
*.img
|
||||
*.sym
|
||||
|
||||
|
||||
|
3
.gitmodules
vendored
Normal file
3
.gitmodules
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
[submodule "mlibc"]
|
||||
path = mlibc
|
||||
url = https://github.com/managarm/mlibc.git
|
133
Makefile
133
Makefile
@ -3,11 +3,11 @@ EMULATOR = qemu-system-i386
|
||||
AS = ${HOME}/opt/cross/bin/i686-elf-as
|
||||
CC = ${HOME}/opt/cross/bin/i686-elf-gcc
|
||||
CPP = ${HOME}/opt/cross/bin/i686-elf-g++
|
||||
CFLAGS = -ffreestanding -O2 -Wall -Wextra
|
||||
CFLAGS = -ffreestanding -Og -ggdb -Wall -Wextra
|
||||
|
||||
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
|
||||
OFILES =$(BUILD_DIR)/boot.o $(BUILD_DIR)/kterm.o $(BUILD_DIR)/kernel.o $(BUILD_DIR)/memory.o $(BUILD_DIR)/paging.o $(BUILD_DIR)/pit.o $(BUILD_DIR)/time.o $(BUILD_DIR)/keyboard.o $(BUILD_DIR)/io.o $(BUILD_DIR)/gdtc.o $(BUILD_DIR)/idt.o $(BUILD_DIR)/pic.o $(BUILD_DIR)/sv-terminal.o $(BUILD_DIR)/string.o $(BUILD_DIR)/prekernel.o $(BUILD_DIR)/cpu.o $(BUILD_DIR)/KHeap.o $(BUILD_DIR)/pci.o $(BUILD_DIR)/pcidevice.o $(BUILD_DIR)/atapiDevice.o $(BUILD_DIR)/ataDevice.o $(BUILD_DIR)/rsdp.o $(BUILD_DIR)/acpi.o
|
||||
|
||||
SRC_DIR = src
|
||||
SRC_DIR = source
|
||||
BUILD_DIR = build
|
||||
|
||||
CRTBEGIN_OBJ = $(shell $(CC) $(CFLAGS) -print-file-name=crtbegin.o)
|
||||
@ -23,8 +23,6 @@ all: clean build
|
||||
|
||||
build: build_kernel iso
|
||||
|
||||
|
||||
|
||||
clean_iso:
|
||||
if [[ -a isodir/boot ]] ; then rm root/boot -rd ; fi
|
||||
if [ -f build/barinkOS.iso ] ; then rm build/barinkOS.iso ; fi
|
||||
@ -32,17 +30,25 @@ clean_iso:
|
||||
iso: clean_iso clean build
|
||||
mkdir -p root/boot/grub
|
||||
cp build/myos.bin root/boot/myos.bin
|
||||
cp src/grub.cfg root/boot/grub/grub.cfg
|
||||
cp source/grub.cfg root/boot/grub/grub.cfg
|
||||
grub-mkrescue -o build/barinkOS.iso root
|
||||
|
||||
run: all
|
||||
virtualboxvm --startvm "BarinkOS_test"
|
||||
debug: all
|
||||
objcopy --only-keep-debug build/myos.bin kernel.sym
|
||||
$(EMULATOR) -cdrom build/barinkOS.iso -serial stdio -vga std -display gtk -m 2G -cpu core2duo -s -d int -no-shutdown -no-reboot
|
||||
test:
|
||||
$(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
|
||||
$(EMULATOR) -kernel $(BUILD_DIR)/myos.bin -serial stdio -vga std -display gtk -m 2G -cpu core2duo -d int -no-shutdown -no-reboot
|
||||
|
||||
test_iso:
|
||||
$(EMULATOR) -boot d -cdrom $(BUILD_DIR)/barinkOS.iso -serial stdio -vga std -display gtk -m 2G -cpu core2duo -d int -no-reboot -no-shutdown
|
||||
test_disk:
|
||||
$(EMULATOR) -boot d -drive format=raw,file=build/disk.img -serial stdio -vga std -display gtk -m 2G -cpu core2duo
|
||||
|
||||
|
||||
build_kernel: $(OBJ_LINK_LIST)
|
||||
$(CC) -T $(SRC_DIR)/kernel//linker.ld -o $(BUILD_DIR)/myos.bin \
|
||||
-ffreestanding -O2 -nostdlib $(OBJ_LINK_LIST) -lgcc
|
||||
-ffreestanding -ggdb -Og -nostdlib $(OBJ_LINK_LIST) -lgcc
|
||||
|
||||
build_x86_64:
|
||||
$(AS) $(SRC_DIR)/cgc/x86_64/crti.s -o $(BUILD_DIR)/crti_64.o
|
||||
@ -51,48 +57,85 @@ build_x86_64:
|
||||
clean:
|
||||
rm -f $(BUILD_DIR)/myos.bin $(INTERNAL_OBJS)
|
||||
|
||||
# C++ definition -> Object files
|
||||
$(BUILD_DIR)/kernel.o:
|
||||
$(CPP) -c $(SRC_DIR)/kernel/kernel.cpp -o $(BUILD_DIR)/kernel.o $(CFLAGS) -fno-exceptions -fno-rtti -fpermissive
|
||||
$(CPP) -c $(SRC_DIR)/kernel/kernel.cpp -o $(BUILD_DIR)/kernel.o $(CFLAGS) -fno-exceptions -fno-rtti
|
||||
|
||||
$(BUILD_DIR)/kterm.o:
|
||||
$(CPP) -c $(SRC_DIR)/kernel/tty/kterm.cpp -o $(BUILD_DIR)/kterm.o $(CFLAGS) -fno-exceptions -fno-rtti
|
||||
$(CPP) -c $(SRC_DIR)/kernel/terminal/kterm.cpp -o $(BUILD_DIR)/kterm.o $(CFLAGS) -fno-exceptions -fno-rtti
|
||||
|
||||
$(BUILD_DIR)/io.o:
|
||||
$(CPP) -c $(SRC_DIR)/kernel/drivers/io/io.cpp -o $(BUILD_DIR)/io.o $(CFLAGS) -fno-exceptions -fno-rtti
|
||||
|
||||
$(BUILD_DIR)/idt.o:
|
||||
$(CPP) -c $(SRC_DIR)/kernel/interrupts/idt.cpp -o $(BUILD_DIR)/idt.o $(CFLAGS) -fno-exceptions -fno-rtti
|
||||
|
||||
$(BUILD_DIR)/gdtc.o:
|
||||
$(CPP) -c $(SRC_DIR)/kernel/memory/gdt/gdtc.cpp -o $(BUILD_DIR)/gdtc.o $(CFLAGS) -fno-exceptions -fno-rtti
|
||||
|
||||
$(BUILD_DIR)/pic.o:
|
||||
$(CPP) -c $(SRC_DIR)/kernel/drivers/pic/pic.cpp -o $(BUILD_DIR)/pic.o $(CFLAGS) -fno-exceptions -fno-rtti
|
||||
|
||||
$(BUILD_DIR)/string.o:
|
||||
$(CC) -c $(SRC_DIR)/lib/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)/pci.o:
|
||||
$(CPP) -c $(SRC_DIR)/kernel/drivers/pci/pci.cpp -o $(BUILD_DIR)/pci.o $(CFLAGS) -fno-exceptions -fno-rtti
|
||||
|
||||
$(BUILD_DIR)/pcidevice.o:
|
||||
$(CPP) -c $(SRC_DIR)/kernel/drivers/pci/pciDevice.cpp -o $(BUILD_DIR)/pcidevice.o $(CFLAGS) -fno-exceptions -fno-rtti
|
||||
|
||||
$(BUILD_DIR)/atapiDevice.o:
|
||||
$(CPP) -c $(SRC_DIR)/kernel/drivers/atapi/atapiDevice.cpp -o $(BUILD_DIR)/atapiDevice.o $(CFLAGS) -fno-exceptions -fno-rtti
|
||||
|
||||
$(BUILD_DIR)/ataDevice.o:
|
||||
$(CPP) -c $(SRC_DIR)/kernel/drivers/ata/ataDevice.cpp -o $(BUILD_DIR)/ataDevice.o $(CFLAGS) -fno-exceptions -fno-rtti
|
||||
|
||||
$(BUILD_DIR)/rsdp.o:
|
||||
$(CPP) -c $(SRC_DIR)/kernel/drivers/acpi/rsdp.cpp -o $(BUILD_DIR)/rsdp.o $(CFLAGS) -fno-exceptions -fno-rtti
|
||||
|
||||
$(BUILD_DIR)/acpi.o:
|
||||
$(CPP) -c $(SRC_DIR)/kernel/drivers/acpi/acpi.cpp -o $(BUILD_DIR)/acpi.o $(CFLAGS) -fno-exceptions -fno-rtti
|
||||
|
||||
$(BUILD_DIR)/pit.o:
|
||||
$(CPP) -c $(SRC_DIR)/kernel/drivers/pit/pit.cpp -o $(BUILD_DIR)/pit.o $(CFLAGS) -fno-exceptions -fno-rtti
|
||||
|
||||
|
||||
$(BUILD_DIR)/keyboard.o:
|
||||
$(CPP) -c $(SRC_DIR)/kernel/drivers/ps-2/keyboard.cpp -o $(BUILD_DIR)/keyboard.o $(CFLAGS) -fno-exceptions -fno-rtti
|
||||
|
||||
$(BUILD_DIR)/time.o:
|
||||
$(CPP) -c $(SRC_DIR)/kernel/time.cpp -o $(BUILD_DIR)/time.o $(CFLAGS) -fno-exceptions -fno-rtti
|
||||
|
||||
$(BUILD_DIR)/sv-terminal.o:
|
||||
$(CPP) -c $(SRC_DIR)/kernel/supervisorterminal/superVisorTerminal.cpp -o $(BUILD_DIR)/sv-terminal.o $(CFLAGS) -fno-exceptions -fno-rtti
|
||||
|
||||
$(BUILD_DIR)/memory.o:
|
||||
$(CPP) -c $(SRC_DIR)/kernel/memory/PhysicalMemoryManager.cpp -o $(BUILD_DIR)/memory.o $(CFLAGS) -fno-exceptions -fno-rtti
|
||||
|
||||
$(BUILD_DIR)/paging.o:
|
||||
$(CPP) -c $(SRC_DIR)/kernel/memory/VirtualMemoryManager.cpp -o $(BUILD_DIR)/paging.o $(CFLAGS) -fno-exceptions -fno-rtti
|
||||
|
||||
$(BUILD_DIR)/KHeap.o:
|
||||
$(CPP) -c $(SRC_DIR)/kernel/memory/KernelHeap.cpp -o $(BUILD_DIR)/KHeap.o $(CFLAGS) -fno-exceptions -fno-rtti
|
||||
|
||||
$(BUILD_DIR)/prekernel.o:
|
||||
$(CPP) -c $(SRC_DIR)/kernel/prekernel/prekernel.cpp -o $(BUILD_DIR)/prekernel.o $(CFLAGS) -fno-exceptions -fno-rtti
|
||||
|
||||
$(BUILD_DIR)/cpu.o:
|
||||
$(CPP) -c $(SRC_DIR)/kernel/cpu.cpp -o $(BUILD_DIR)/cpu.o $(CFLAGS) -fno-exceptions -fno-rtti
|
||||
|
||||
|
||||
|
||||
# Assembly -> Object files
|
||||
$(BUILD_DIR)/boot.o:
|
||||
$(AS) $(SRC_DIR)/kernel//boot.S -o $(BUILD_DIR)/boot.o
|
||||
$(AS) $(SRC_DIR)/kernel/boot/boot.s -o $(BUILD_DIR)/boot.o
|
||||
|
||||
$(BUILD_DIR)/crti.o:
|
||||
$(AS) $(SRC_DIR)/kernel/crti.s -o $(BUILD_DIR)/crti.o
|
||||
|
||||
$(BUILD_DIR)/crtn.o:
|
||||
$(AS) $(SRC_DIR)/kernel/crtn.s -o $(BUILD_DIR)/crtn.o
|
||||
|
||||
$(BUILD_DIR)/io.o:
|
||||
$(CPP) -c $(SRC_DIR)/kernel/io.cpp -o $(BUILD_DIR)/io.o $(CFLAGS) -fno-exceptions -fno-rtti
|
||||
|
||||
$(BUILD_DIR)/PageDirectory.o:
|
||||
$(CPP) -c $(SRC_DIR)/kernel/memory/PageDirectory.cpp -o $(BUILD_DIR)/PageDirectory.o $(CFLAGS) -fno-exceptions -fno-rtti
|
||||
|
||||
$(BUILD_DIR)/idt.o:
|
||||
$(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:
|
||||
$(CPP) -c $(SRC_DIR)/kernel/pic/pic.cpp -o $(BUILD_DIR)/pic.o $(CFLAGS) -fno-exceptions -fno-rtti
|
||||
|
||||
$(BUILD_DIR)/string.o:
|
||||
$(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
|
||||
|
19
README.md
19
README.md
@ -6,9 +6,6 @@
|
||||
|
||||
________________________
|
||||
### Screenshot(s)
|
||||
 \
|
||||
It may not look like much but I am proud of it! We are in graphics mode.
|
||||
|
||||
 \
|
||||
The first scrolling boot screen. 😲
|
||||
|
||||
@ -19,10 +16,24 @@ W.I.P - Working on interrupt handling
|
||||
|
||||
 \
|
||||
Multiboot information can be read by the kernel.
|
||||
|
||||
 \
|
||||
Enabled paging and am getting page faults!
|
||||
|
||||
 \
|
||||
Enumerating the PCI bus
|
||||
|
||||
 \
|
||||
Correctly identified our ATAPI device 🎉
|
||||
|
||||
 \
|
||||
Reading a file from a FAT-16 Formatted drive
|
||||
|
||||
________________________
|
||||
|
||||
### The goal
|
||||
Writing a hobby operating system to better understand the basic building blocks of any operating system.
|
||||
Writing a hobby operating system to better understand the basic building blocks of any operating system.Initially I'd like for my
|
||||
operating system to be able to run bash.
|
||||
|
||||
________________________
|
||||
### Operating System Technical specs/details
|
||||
|
35
features.md
35
features.md
@ -1,29 +1,37 @@
|
||||
# TODO list
|
||||
## Start planning
|
||||
## Basics
|
||||
<input type="checkbox" checked/> Setup Cross-Compiler \
|
||||
<input type="checkbox" checked/> Multiboot to kernel \
|
||||
<input type="checkbox" checked/> Printing string to the screen \
|
||||
<input type="checkbox" checked/> Printing values/numbers to the screen (a.k.k itoa) \
|
||||
<input type="checkbox" checked/> Printing values/numbers to the screen \
|
||||
<input type="checkbox" checked/> Basic Terminal \
|
||||
<input type="checkbox" checked/> Extend Multiboot implementation \
|
||||
<input type="checkbox" checked/> Output to serial port \
|
||||
<input type="checkbox" checked/> Move to protected mode \
|
||||
<input type="checkbox" checked/> Enabel CMOS clock \
|
||||
<input type="checkbox" /> Time measurement (PIC &| PIT) \
|
||||
<input type="checkbox" checked/> Time measurement (PIC &| PIT) \
|
||||
<input type="checkbox" /> Detect CPU speed \
|
||||
<input type="checkbox" checked/> Interrupt / exception system (API) \
|
||||
|
||||
<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" checked/> PCI support \
|
||||
<input type="checkbox" checked/> ATA PIO Mode support \
|
||||
<input type="checkbox" checked/> FAT Filesystem \
|
||||
<input type="checkbox" /> Virtual filesystem \
|
||||
<input type="checkbox" checked/> Keyboard support ( P/S2 Keyboard) \
|
||||
<input type="checkbox" checked/> Physical memory management \
|
||||
<input type="checkbox" checked/> Paging \
|
||||
<input type="checkbox" checked/> Virtual memory management \
|
||||
<input type="checkbox" checked/> The heap: allocating memory at runtime (malloc and free) is almost impossible to go without. \
|
||||
<input type="checkbox" /> Enable SIMD Extensions (SSE)
|
||||
|
||||
## Other features I am thinking of:
|
||||
<input type="checkbox" /> PCI support \
|
||||
<input type="checkbox" checked/> PCI support \
|
||||
<input type="checkbox" /> ATA PIO Mode support \
|
||||
<input type="checkbox" /> USTAR Filesystem ( For its simplicity this is very likely the first filesystem the OS is going to support) \
|
||||
<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" /> Keyboard support ( P/S2 Keyboard) \
|
||||
<input type="checkbox" checked/> Memory Management (MMU)
|
||||
<input type="checkbox" /> Hardware Management system
|
||||
|
||||
<input type="checkbox" /> Preemptive multi tasking \
|
||||
<input type="checkbox" /> Processes \
|
||||
<input type="checkbox" /> Threads
|
||||
@ -32,9 +40,12 @@
|
||||
<input type="checkbox" /> POSIX compliance (partially) \
|
||||
<input type="checkbox" /> RPC - for interprocess communication \
|
||||
<input type="checkbox" /> Sync primitives - Semaphores, Mutexes, spinlocks et al. \
|
||||
<input type="checkbox" /> Basic Terminal \
|
||||
<input type="checkbox" /> Extend hardware recognition ( CPU codename, memory, ATA harddisk, RAW diskSpace, CPU speed through SMBIOS et al. ) \
|
||||
|
||||
<input type="checkbox" /> ACPI support \
|
||||
<input type="checkbox" /> ATAPI support \
|
||||
|
||||
## Optional
|
||||
<input type="checkbox" /> Basic Window server/client \
|
||||
## Support for more filesystems if I like the challenge in writing these ...
|
||||
<input type="checkbox" /> FAT Filesystem \
|
||||
<input type="checkbox" /> EXT2 Filesystem
|
||||
<input type="checkbox" /> USTAR Filesystem \
|
||||
<input type="checkbox" /> FAT16 Filesystem \
|
||||
|
BIN
images/BarinkOS.png
(Stored with Git LFS)
BIN
images/BarinkOS.png
(Stored with Git LFS)
Binary file not shown.
BIN
images/BarinkOS_logo(standard).svg
(Stored with Git LFS)
BIN
images/BarinkOS_logo(standard).svg
(Stored with Git LFS)
Binary file not shown.
BIN
images/BarinkOS_logo.svg
(Stored with Git LFS)
BIN
images/BarinkOS_logo.svg
(Stored with Git LFS)
Binary file not shown.
1
mlibc
Submodule
1
mlibc
Submodule
Submodule mlibc added at aad4e7f64b
BIN
screenshots/BarinkOS_VBE_GRAPHICS.png
(Stored with Git LFS)
BIN
screenshots/BarinkOS_VBE_GRAPHICS.png
(Stored with Git LFS)
Binary file not shown.
BIN
screenshots/CD-ROM_Identify.png
(Stored with Git LFS)
Normal file
BIN
screenshots/CD-ROM_Identify.png
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
screenshots/PCIBusEnumeration.png
(Stored with Git LFS)
Normal file
BIN
screenshots/PCIBusEnumeration.png
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
screenshots/PageFault.png
(Stored with Git LFS)
Normal file
BIN
screenshots/PageFault.png
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
screenshots/ReadingFilesFromFAT16.png
(Stored with Git LFS)
Normal file
BIN
screenshots/ReadingFilesFromFAT16.png
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
screenshots/must frustrating bug ever.png
(Stored with Git LFS)
Normal file
BIN
screenshots/must frustrating bug ever.png
(Stored with Git LFS)
Normal file
Binary file not shown.
25
scripts/build.py
Executable file
25
scripts/build.py
Executable file
@ -0,0 +1,25 @@
|
||||
#!/usr/bin/python3
|
||||
import os
|
||||
import subprocess
|
||||
|
||||
|
||||
print("Building BarinkOS")
|
||||
|
||||
|
||||
# list and run build scripts
|
||||
print("Running build-scripts")
|
||||
os.chdir("scripts")
|
||||
|
||||
scripts=os.listdir()
|
||||
currentScript=os.path.basename(__file__)
|
||||
|
||||
if currentScript in scripts:
|
||||
scripts.remove(currentScript)
|
||||
|
||||
|
||||
for script in scripts:
|
||||
print(os.getcwd())
|
||||
print("Running:" + script)
|
||||
subprocess.call(script, cwd=os.getcwd())
|
||||
|
||||
os.chdir("..")
|
64
scripts/create-filesystem.sh
Normal file
64
scripts/create-filesystem.sh
Normal file
@ -0,0 +1,64 @@
|
||||
#!/bin/bash
|
||||
#
|
||||
# How to build a boot image
|
||||
# NOTE: This script cant run properly yet
|
||||
# Things described here should be done manually for now
|
||||
#
|
||||
# COPYRIGHT © Nigel Barink 2023
|
||||
#
|
||||
|
||||
echo "Building a FAT16 filesystem"
|
||||
|
||||
|
||||
su
|
||||
|
||||
# dd if=/dev/zero of=diks.img bs=512 count=131072
|
||||
# fdisk disk.img
|
||||
# Use the following options in fdisk (Format Disk Tool)
|
||||
# We want to create a MBR (NOT GPT) Partition table containing 1 logical disk
|
||||
# with a primary FAT16 partition marked bootable
|
||||
|
||||
#OPTIONs
|
||||
|
||||
# Create new DOS disklabel
|
||||
# o
|
||||
# Create new partition
|
||||
# n
|
||||
# Choose Primary as partition type
|
||||
# p
|
||||
# hit enter to choose default for the other options
|
||||
|
||||
# Mark partition 1 as bootable
|
||||
# a
|
||||
|
||||
# Change partition type to FAT16
|
||||
# t
|
||||
# Choose Partition 1
|
||||
# 1
|
||||
# Choose HEX 6 for FAT16
|
||||
# 6
|
||||
|
||||
# Sync and write changes to disk
|
||||
# w
|
||||
|
||||
# Create a "block" device from the disk.img
|
||||
# losetup /dev/loop9 disk.img
|
||||
|
||||
# Format the partition on the disk as FAT16
|
||||
# mkdosfs -F16 /dev/loop9
|
||||
|
||||
# Mount the disk to a folder on our dev machine
|
||||
# mount /dev/loop9 /mnt
|
||||
|
||||
# Install the grub bootloader onto the disk
|
||||
# grub-install --no-floppy --modules="normal multiboot" /dev/loop9 --target=i386-pc --boot-directory=/mnt/boot --force
|
||||
|
||||
# copy the necessary OS files
|
||||
# cp root/boot/myos.bin /mnt/boot/myos.bin
|
||||
# cp root/boot/grub/grub.cfg /mnt/boot/grub/grub.cfg
|
||||
|
||||
# Unmount the device
|
||||
# umount /mnt
|
||||
|
||||
# Destroy the loop device
|
||||
# losetup -d /dev/loop9
|
3
scripts/dev-setup/install.sh
Normal file
3
scripts/dev-setup/install.sh
Normal file
@ -0,0 +1,3 @@
|
||||
#!/bin/bash
|
||||
|
||||
cmake -DLLVM_ENABLE_PROJECTS="clang-tools-extra" -DCMAKE_BUILD_TYPE=Release -G "Unix Makefiles" ../llvm
|
5
scripts/hooks/pre-commit.hook.sh
Normal file
5
scripts/hooks/pre-commit.hook.sh
Normal file
@ -0,0 +1,5 @@
|
||||
#!/bin/bash
|
||||
|
||||
|
||||
# Run clang-tidy
|
||||
|
5
scripts/test.sh
Executable file
5
scripts/test.sh
Executable file
@ -0,0 +1,5 @@
|
||||
#!/bin/bash
|
||||
start=`date +%s`
|
||||
end=`date +%s`
|
||||
echo That took $((end-start)) seconds
|
||||
date +"%c" -d195440409
|
8
source/grub.cfg
Normal file
8
source/grub.cfg
Normal file
@ -0,0 +1,8 @@
|
||||
GRUB_DEFAULT=0
|
||||
GRUB_TIMEOUT=-1
|
||||
GRUB_HIDDEN_TIMEOUT=0
|
||||
GRUB_HIDDEN_TIMEOUT_QUITE=true
|
||||
|
||||
menuentry "BarinkOS" {
|
||||
multiboot /boot/myos.bin
|
||||
}
|
38
source/kernel/bitmap.h
Normal file
38
source/kernel/bitmap.h
Normal file
@ -0,0 +1,38 @@
|
||||
#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 uint32_t bitmap_first_unset( uint32_t* map , int map_size)
|
||||
{
|
||||
for ( int i = 0 ; i < map_size ; i ++ )
|
||||
{
|
||||
// a bit or more is set within this byte!
|
||||
if( (map[i] & 0xFFFFFFFF) > 0 ){
|
||||
|
||||
// which bit is set?
|
||||
for(int j = 0 ; j < 32 ; j++){
|
||||
if ( (map[i] & (0x00000001 << j)) > 0)
|
||||
{
|
||||
return (i*32)+j;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
return -1;
|
||||
|
||||
}
|
118
source/kernel/boot/boot.s
Normal file
118
source/kernel/boot/boot.s
Normal file
@ -0,0 +1,118 @@
|
||||
.include "./source/kernel/boot/multiboot.s"
|
||||
/*
|
||||
* Allocate initial stack
|
||||
*/
|
||||
.section .bootstrap_stack, "aw", @nobits
|
||||
stack_bottom:
|
||||
.skip 16384 # 16 KiB
|
||||
.globl stack_top
|
||||
stack_top:
|
||||
|
||||
/*
|
||||
* Preallocate a couple pages to get us bootstrapped
|
||||
* Being carefull to not use any address the bootloader might
|
||||
* be using for its multiboot structures
|
||||
*/
|
||||
.section .bss, "aw", @nobits
|
||||
.align 4096
|
||||
.globl boot_page_directory
|
||||
boot_page_directory:
|
||||
.skip 4096
|
||||
.globl boot_page_table
|
||||
boot_page_table:
|
||||
.skip 4096
|
||||
.globl multiboot_page_table
|
||||
multiboot_page_table:
|
||||
.skip 4096
|
||||
# More page tables may be required
|
||||
|
||||
# Entry point
|
||||
.section .multiboot.text, "a"
|
||||
.globl _start
|
||||
.type _start, @function
|
||||
_start:
|
||||
|
||||
# Get physical address of the boot_page_table
|
||||
movl $(boot_page_table - 0xC0000000), %edi
|
||||
# Map address 0
|
||||
movl $0, %esi
|
||||
1:
|
||||
cmpl $(kernel_end - 0xC0000000), %esi
|
||||
jge 3f
|
||||
|
||||
# Map physical address as "present and writable"
|
||||
movl %esi, %edx
|
||||
orl $0x003, %edx
|
||||
movl %edx, (%edi)
|
||||
|
||||
2: # Size of page is 4096 bytes
|
||||
addl $4096, %esi
|
||||
# Size of entries in boot_page_table is 4 bytes
|
||||
addl $4, %edi
|
||||
# Loop to the next entry if we haven't finished.
|
||||
loop 1b
|
||||
|
||||
3: # Map VGA video memory to 0xC03FF00 as "present, writable"
|
||||
movl $(0x000B8000 | 0x003), boot_page_table - 0xC0000000 + 1023 * 4
|
||||
|
||||
# Map the page table to both virtual addresss 0x00000000 and 0xC0000000
|
||||
movl $(boot_page_table - 0xC0000000 + 0x003), boot_page_directory - 0xC0000000 + 0
|
||||
movl $(boot_page_table - 0xC0000000 + 0x003), boot_page_directory - 0xC0000000 + 768 * 4
|
||||
|
||||
# Set cr3 to the address of the boot_page_directory
|
||||
movl $(boot_page_directory - 0xC0000000), %ecx
|
||||
movl %ecx, %cr3
|
||||
|
||||
# Enable paging and the write-protect bit
|
||||
movl %cr0, %ecx
|
||||
orl $0x80010000, %ecx
|
||||
movl %ecx, %cr0
|
||||
|
||||
# Jump to higher half with an absolute jump
|
||||
lea 4f, %ecx
|
||||
jmp *%ecx
|
||||
|
||||
.section .text
|
||||
4:
|
||||
# At this point, paging is fully set up and enabled
|
||||
isPaging:
|
||||
|
||||
# Reload cr3 to force tlb flush
|
||||
movl %cr3, %ecx
|
||||
movl %ecx, %cr3
|
||||
|
||||
/*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 prekernelSetup
|
||||
|
||||
# Unmap the identity mapping as it is now unnecessary
|
||||
# movl $0, boot_page_directory + 0
|
||||
|
||||
|
||||
|
||||
|
||||
call early_main
|
||||
|
||||
cli
|
||||
1: hlt
|
||||
jmp 1b
|
||||
|
||||
|
||||
.include "./source/kernel/memory/gdt/gdt.s"
|
||||
.include "./source/kernel/irs_table.s"
|
||||
.include "./source/kernel/irq_table.s"
|
||||
.include "./source/kernel/interrupts/idt.s"
|
||||
|
||||
|
15
source/kernel/boot/multiboot.s
Normal file
15
source/kernel/boot/multiboot.s
Normal file
@ -0,0 +1,15 @@
|
||||
/*
|
||||
* 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.data, "aw"
|
||||
.align 4
|
||||
.long MAGIC
|
||||
.long FLAGS
|
||||
.long CHECKSUM
|
||||
|
91
source/kernel/bootcheck.h
Normal file
91
source/kernel/bootcheck.h
Normal file
@ -0,0 +1,91 @@
|
||||
#pragma once
|
||||
#include "prekernel/multiboot.h"
|
||||
#define CHECK_FLAG(flags, bit) ((flags) & (1 <<(bit)))
|
||||
|
||||
#include "terminal/kterm.h"
|
||||
|
||||
|
||||
|
||||
void CheckMBT ( multiboot_info_t* mbt ){
|
||||
/* Set MBI to the addresss of the multiboot information structure*/
|
||||
multiboot_info_t * mbi = (multiboot_info_t *) mbt;
|
||||
|
||||
#ifdef __VERBOSE__
|
||||
/* Print out the flags */
|
||||
printf("flags = 0x%x\n", (unsigned) mbi->flags);
|
||||
#endif
|
||||
/* Are mem_* valid? */
|
||||
if ( CHECK_FLAG(mbi->flags,0)){
|
||||
// Do nothing
|
||||
}
|
||||
|
||||
/* is boot device valid ? */
|
||||
if (CHECK_FLAG (mbi->flags, 1))
|
||||
{
|
||||
#ifdef __VERBOSE__
|
||||
printf("boot_device = 0x0%x\n", (unsigned) mbi->boot_device);
|
||||
#endif
|
||||
}
|
||||
|
||||
/* is the command line passed? */
|
||||
if (CHECK_FLAG ( mbi->flags,2))
|
||||
{
|
||||
#ifdef __VERBOSE__
|
||||
printf("cmdline = %s\n", (char *) mbi->cmdline);
|
||||
#endif
|
||||
}
|
||||
|
||||
/* Are mods_* valid? */
|
||||
if(CHECK_FLAG ( mbi->flags, 3)){
|
||||
multiboot_module_t *mod;
|
||||
uint32_t i;
|
||||
#ifdef __VERBOSE__
|
||||
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++){
|
||||
printf(" mod start = 0x%x, mod_end = 0x%x, cmdline = %s\n", (unsigned) mod->mod_start, (unsigned) mod->mod_end, (char*) mod->cmdline);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
/* Bits 4 and 5 are mutually exclusive! */
|
||||
if (CHECK_FLAG (mbi->flags, 4) && CHECK_FLAG(mbi->flags, 5))
|
||||
{
|
||||
#ifdef __VERBOSE__
|
||||
printf("Both bits 4 and 5 are set.\n");
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
|
||||
/* Is the symbol table of a.out valid? */
|
||||
if (CHECK_FLAG(mbi->flags, 4)){
|
||||
multiboot_aout_symbol_table_t *multiboot_aout_sym = &(mbi->u.aout_sym);
|
||||
#ifdef __VERBOSE__
|
||||
printf( "multiboot_aout_symbol_table: tabsize = 0x%0x, strsize = 0x%x, addr = 0x%x\n",
|
||||
(unsigned) multiboot_aout_sym->tabsize,
|
||||
(unsigned) multiboot_aout_sym->strsize,
|
||||
(unsigned) multiboot_aout_sym->addr);
|
||||
#endif
|
||||
}
|
||||
|
||||
/* Is the section header table of ELF valid? */
|
||||
if (CHECK_FLAG(mbi->flags, 5)){
|
||||
multiboot_elf_section_header_table_t *multiboot_elf_sec = &(mbi->u.elf_sec);
|
||||
#ifdef __VERBOSE__
|
||||
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->addr, (unsigned) multiboot_elf_sec->shndx);
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
/* Draw diagonal blue line */
|
||||
if (CHECK_FLAG (mbt->flags, 12)){
|
||||
#ifdef __VERBOSE__
|
||||
printf("Can draw!\n");
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
}
|
9
source/kernel/bootinfo.h
Normal file
9
source/kernel/bootinfo.h
Normal file
@ -0,0 +1,9 @@
|
||||
#pragma once
|
||||
#include "memory/memoryinfo.h"
|
||||
|
||||
|
||||
struct BootInfo{
|
||||
const char* BootStructureID = "BarinkOS";
|
||||
MemoryInfo* memory;
|
||||
|
||||
};
|
39
source/kernel/cpu.cpp
Normal file
39
source/kernel/cpu.cpp
Normal file
@ -0,0 +1,39 @@
|
||||
#include "cpu.h"
|
||||
|
||||
|
||||
uint32_t GetEFLAGS()
|
||||
{
|
||||
uint32_t EFLAGS = 0;
|
||||
asm volatile ("pushfl;" "movl 4(%%esp), %%edx" : "=d"(EFLAGS));
|
||||
return EFLAGS;
|
||||
}
|
||||
|
||||
|
||||
uint32_t GetCR0()
|
||||
{
|
||||
uint32_t cr0_value;
|
||||
asm volatile ("movl %%cr0, %%edx" : "=d"(cr0_value));
|
||||
return cr0_value;
|
||||
|
||||
}
|
||||
|
||||
|
||||
uint32_t GetCR2(){
|
||||
uint32_t cr2_value;
|
||||
__asm__ volatile("movl %%cr2, %%edx": "=d"(cr2_value));
|
||||
return cr2_value;
|
||||
}
|
||||
|
||||
|
||||
uint32_t GetCR3(){
|
||||
uint32_t cr3_value;
|
||||
__asm__ volatile("movl %%cr3, %%edx": "=d"(cr3_value));
|
||||
return cr3_value;
|
||||
}
|
||||
|
||||
|
||||
uint32_t GetCR4(){
|
||||
uint32_t cr4_value;
|
||||
__asm__ volatile("movl %%cr4, %%edx": "=d"(cr4_value));
|
||||
return cr4_value;
|
||||
}
|
16
source/kernel/cpu.h
Normal file
16
source/kernel/cpu.h
Normal file
@ -0,0 +1,16 @@
|
||||
#pragma once
|
||||
#include <stdint.h>
|
||||
|
||||
uint32_t GetEFLAGS();
|
||||
|
||||
|
||||
uint32_t GetCR0();
|
||||
|
||||
|
||||
uint32_t GetCR2();
|
||||
|
||||
|
||||
uint32_t GetCR3();
|
||||
|
||||
|
||||
uint32_t GetCR4();
|
9
source/kernel/definitions.h
Normal file
9
source/kernel/definitions.h
Normal file
@ -0,0 +1,9 @@
|
||||
#pragma once
|
||||
/**
|
||||
* Kernel definitions
|
||||
*/
|
||||
|
||||
#define __DEBUG__ false
|
||||
#define KERNEL_VERSION 0
|
||||
|
||||
#define ARCHITECTURE "I386"
|
14
source/kernel/drivers/acpi/acpi.cpp
Normal file
14
source/kernel/drivers/acpi/acpi.cpp
Normal file
@ -0,0 +1,14 @@
|
||||
#include "acpi.h"
|
||||
RSDPTR* ACPI::rsd_ptr;
|
||||
RSDT* ACPI::rsd_table;
|
||||
|
||||
|
||||
|
||||
void ACPI::initialize(){
|
||||
|
||||
// Find the Root System Description Pointer
|
||||
ACPI::rsd_ptr = FindRSD();
|
||||
printRSD(rsd_ptr);
|
||||
// Get the Root System Description Table
|
||||
ACPI::rsd_table = getRSDT(rsd_ptr);
|
||||
}
|
13
source/kernel/drivers/acpi/acpi.h
Normal file
13
source/kernel/drivers/acpi/acpi.h
Normal file
@ -0,0 +1,13 @@
|
||||
#pragma once
|
||||
#include "rsdp.h"
|
||||
class ACPI {
|
||||
public:
|
||||
static void initialize();
|
||||
|
||||
// In the future ACPI might start
|
||||
// doing more systems initialization
|
||||
|
||||
private:
|
||||
static RSDPTR* rsd_ptr;
|
||||
static RSDT* rsd_table;
|
||||
};
|
46
source/kernel/drivers/acpi/rsdp.cpp
Normal file
46
source/kernel/drivers/acpi/rsdp.cpp
Normal file
@ -0,0 +1,46 @@
|
||||
#include "rsdp.h"
|
||||
|
||||
void printRSD(RSDPTR* rsd){
|
||||
printf("Signature: ");
|
||||
for(int i = 0; i < 8; i++){
|
||||
kterm_put(rsd->signature[i]);
|
||||
}
|
||||
kterm_put('\n');
|
||||
|
||||
printf("OEMID: ");
|
||||
for(int i =0; i < 6 ; i++){
|
||||
kterm_put (rsd->OEMID[i]);
|
||||
}
|
||||
kterm_put('\n');
|
||||
|
||||
printf("Revision: %d\n", rsd->Revision);
|
||||
printf("RSDT Address: 0x%x\n", rsd->RsdtAddress );
|
||||
}
|
||||
|
||||
RSDPTR* FindRSD(){
|
||||
char* memory_byte = (char*) 0x000f2e14;
|
||||
const void* string = "RSD PTR ";
|
||||
|
||||
for( ; (uint32_t) memory_byte < 0x0100000; memory_byte+=10){
|
||||
if( memcmp(memory_byte , string , 8 ) == 0 ) {
|
||||
printf("RSD PTR found at 0x%x !\n", memory_byte);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
printRSD((RSDPTR*) memory_byte);
|
||||
return (RSDPTR*) memory_byte;
|
||||
}
|
||||
|
||||
|
||||
RSDT* getRSDT(RSDPTR* rsd){
|
||||
|
||||
RSDT* rsdt = (RSDT*) rsd->RsdtAddress;
|
||||
|
||||
printf("OEMID: ");
|
||||
for(int i = 0; i < 6; i++){
|
||||
kterm_put(rsdt->header.OEMID[i]);
|
||||
}
|
||||
kterm_put('\n');
|
||||
return rsdt;
|
||||
}
|
34
source/kernel/drivers/acpi/rsdp.h
Normal file
34
source/kernel/drivers/acpi/rsdp.h
Normal file
@ -0,0 +1,34 @@
|
||||
#pragma once
|
||||
#include <stdint.h>
|
||||
#include "./../../terminal/kterm.h"
|
||||
#include "../../../lib/include/mem.h"
|
||||
struct RSDPTR {
|
||||
char signature[8];
|
||||
uint8_t Checksum ;
|
||||
char OEMID [6];
|
||||
uint8_t Revision;
|
||||
uint32_t RsdtAddress;
|
||||
}__attribute__((packed));
|
||||
|
||||
struct ACPISDTHeader{
|
||||
char Signature[4];
|
||||
uint32_t Length;
|
||||
uint8_t CheckSum;
|
||||
char OEMID[6];
|
||||
char OEMTableID[8];
|
||||
uint32_t OEMRevision;
|
||||
uint32_t CreatorID;
|
||||
uint32_t CreatorRevision;
|
||||
}__attribute__((packed));
|
||||
|
||||
|
||||
struct RSDT{
|
||||
struct ACPISDTHeader header;
|
||||
uint32_t PointerToSDT[]; // Length of array : (header.Length - sizeof(header))/ 4
|
||||
}__attribute__((packed));
|
||||
|
||||
RSDPTR* FindRSD();
|
||||
|
||||
void printRSD(RSDPTR* rsd);
|
||||
|
||||
RSDT* getRSDT(RSDPTR* rsd);
|
194
source/kernel/drivers/ata/ataDevice.cpp
Normal file
194
source/kernel/drivers/ata/ataDevice.cpp
Normal file
@ -0,0 +1,194 @@
|
||||
#include "ataDevice.h"
|
||||
#define IS_BIT_SET(x, bit) ((x >> bit & 0x1) == 1)
|
||||
|
||||
void ATA_DEVICE::Soft_Reset(uint8_t DEVICE_CHANNEL,DEVICE_DRIVE drive){
|
||||
printf("Soft reseting drive...\n");
|
||||
outb(channels[DEVICE_CHANNEL].base + 7 , 0x4);
|
||||
// wait a bit..
|
||||
for(int i = 0 ; i < 1000000; i++){
|
||||
asm volatile("NOP");
|
||||
}
|
||||
outb(channels[DEVICE_CHANNEL].base + 7 , 0x0);
|
||||
|
||||
}
|
||||
|
||||
|
||||
void ATA_DEVICE::Identify(uint16_t DEVICE_CHANNEL,DEVICE_DRIVE drive ){
|
||||
// lets ignore which port we actually want to check for now !
|
||||
|
||||
/*
|
||||
THE STEPS INVOLVED
|
||||
|
||||
1. Select the target drive by sending master (0x0A) or slave (0x0B) to the
|
||||
drive select IO port
|
||||
|
||||
2. Set the Sectorcount, LBAlo, LBAmid and LBAhi IO ports to 0
|
||||
|
||||
3. Send the identify command (0xEC) to the command IO port
|
||||
|
||||
4. Read the status port
|
||||
4.2 If the value is 0x0 the drive does not exist
|
||||
4.3 If it has any other value continue
|
||||
5. poll the status port until bit 7 is clear.
|
||||
6. Check if the LBAmid and LBAhi ports are non-zero
|
||||
6.2. If non-zero stop polling this is not an ATA device
|
||||
6.3 If zero continue
|
||||
|
||||
7. poll status port until bit 3 is set or bit 0 is set
|
||||
|
||||
8. if err is clear, read the data from the data port
|
||||
|
||||
|
||||
*/
|
||||
|
||||
|
||||
//printf("channel selected: 0x%x", DEVICE_CHANNEL);
|
||||
// Assuming Master here
|
||||
// Select the target drive
|
||||
outb(DEVICE_CHANNEL | 6, drive); // on the primary bus select the master drive
|
||||
outb(DEVICE_CHANNEL | 6 , 0x0); // write 0 to the controlport for some reason
|
||||
|
||||
outb(DEVICE_CHANNEL | 6, drive);
|
||||
uint8_t status = inb(DEVICE_CHANNEL | 7 );
|
||||
if(status == 0x00){
|
||||
printf("No drive\n");
|
||||
return;
|
||||
}
|
||||
// send the identify command;
|
||||
outb(DEVICE_CHANNEL | 7, 0xEC);
|
||||
|
||||
|
||||
// set the sectorCount, LBAlo, LBAmid, LBA,hi IO ports to 0
|
||||
outb(DEVICE_CHANNEL | 2, 0);
|
||||
|
||||
outb(DEVICE_CHANNEL | 3, 0);
|
||||
|
||||
outb(DEVICE_CHANNEL | 4, 0);
|
||||
|
||||
outb(DEVICE_CHANNEL | 5, 0);
|
||||
|
||||
// send the identify command ;
|
||||
//printf("command sent!\n");
|
||||
outb(DEVICE_CHANNEL | 7 , 0xEC);
|
||||
|
||||
// read the status port
|
||||
uint8_t status2 = inb(DEVICE_CHANNEL | 7);
|
||||
if( status2 == 0x00){
|
||||
printf("No drive\n");
|
||||
return;
|
||||
}
|
||||
|
||||
//printf("Waiting until ready...\n");
|
||||
while(((status2 & 0x80 == 0x80)
|
||||
&& (status2 & 0x01) != 0x01)
|
||||
) status2 = inb(DEVICE_CHANNEL | 7);
|
||||
|
||||
if( status2 & 0x01){
|
||||
printf("Error!\n");
|
||||
return ;
|
||||
}
|
||||
|
||||
uint16_t deviceIdentify [256] = {0};
|
||||
|
||||
for ( int i = 0; i < 256; i++){
|
||||
uint16_t data;
|
||||
asm volatile ("inw %1, %0" : "=a"(data): "Nd"(DEVICE_CHANNEL));
|
||||
|
||||
deviceIdentify[i] = data;
|
||||
}
|
||||
|
||||
|
||||
printf("Model-label (ASCII hex): ");
|
||||
for(int i = 27; i < 47; i++){
|
||||
kterm_put((char)(deviceIdentify[i] >> 8));
|
||||
kterm_put((char)(deviceIdentify[i] & 0x00FF));
|
||||
}
|
||||
kterm_put('\n');
|
||||
|
||||
}
|
||||
|
||||
void ATA_DEVICE::Read(uint16_t DEVICE_CHANNEL, DEVICE_DRIVE drive, uint32_t LBA, uint16_t* buffer) {
|
||||
/*
|
||||
Assume you have a sectorcount byte and a 28 bit LBA value. A sectorcount of 0 means 256 sectors = 128K.
|
||||
|
||||
Notes: - When you send a command byte and the RDY bit of the Status Registers is clear, you may have to wait (technically up to 30 seconds) for the drive to spin up, before DRQ sets. You may also need to ignore ERR and DF the first four times that you read the Status, if you are polling.
|
||||
- for polling PIO drivers: After transferring the last uint16_t of a PIO data block to the data IO port, give the drive a 400ns delay to reset its DRQ bit (and possibly set BSY again, while emptying/filling its buffer to/from the drive).
|
||||
- on the "magic bits" sent to port 0x1f6: Bit 6 (value = 0x40) is the LBA bit. This must be set for either LBA28 or LBA48 transfers. It must be clear for CHS transfers. Bits 7 and 5 are obsolete for current ATA drives, but must be set for backwards compatibility with very old (ATA1) drives.
|
||||
|
||||
An example of a 28 bit LBA PIO mode read on the Primary bus:
|
||||
|
||||
*/
|
||||
|
||||
const int sectorCount = 1;
|
||||
|
||||
// Floating bus check
|
||||
uint8_t floatingBus = inb(DEVICE_CHANNEL | 7);
|
||||
if (floatingBus == 0xFF){
|
||||
printf("Floating bus!!");
|
||||
return ;
|
||||
}
|
||||
|
||||
printf("Read LBA: 0x%x\n", LBA);
|
||||
// Send 0xE0 for the "master" or 0xF0 for the "slave", ORed with the highest 4 bits of the LBA to port 0x1F6: outb(0x1F6, 0xE0 | (slavebit << 4) | ((LBA >> 24) & 0x0F))
|
||||
outb(DEVICE_CHANNEL | 6 , ( 0xE0 | (LBA >>28) ) );
|
||||
// Send a NULL byte to port 0x1F1, if you like (it is ignored and wastes lots of CPU time): outb(0x1F1, 0x00)
|
||||
outb(DEVICE_CHANNEL | 1, 0x0000 );
|
||||
//Send the sectorcount to port 0x1F2: outb(0x1F2, (unsigned char) count)
|
||||
outb(DEVICE_CHANNEL | 2, sectorCount);
|
||||
//Send the low 8 bits of the LBA to port 0x1F3: outb(0x1F3, (unsigned char) LBA))
|
||||
outb(DEVICE_CHANNEL | 3, LBA);
|
||||
//Send the next 8 bits of the LBA to port 0x1F4: outb(0x1F4, (unsigned char)(LBA >> 8))
|
||||
outb(DEVICE_CHANNEL | 4, (LBA >> 8));
|
||||
//Send the next 8 bits of the LBA to port 0x1F5: outb(0x1F5, (unsigned char)(LBA >> 16))
|
||||
outb(DEVICE_CHANNEL | 5, (LBA >> 16));
|
||||
//Send the "READ SECTORS" command (0x20) to port 0x1F7: outb(0x1F7, 0x20)
|
||||
outb(DEVICE_CHANNEL | 7, 0x20);
|
||||
|
||||
volatile int i,j;
|
||||
for(i=0;i<2000;i++)
|
||||
for(j=0;j<25000;j++)
|
||||
asm("NOP");
|
||||
|
||||
//Wait for an IRQ or poll.
|
||||
uint8_t status = inb(DEVICE_CHANNEL | 7);
|
||||
if( status == 0x00){
|
||||
printf("No drive\n");
|
||||
return;
|
||||
}
|
||||
|
||||
printf("Status: %x\n", status);
|
||||
// Check if busy!
|
||||
while((status & 0x80) == 0x80){
|
||||
printf("Reading....\r");
|
||||
status = inb(DEVICE_CHANNEL | 7);
|
||||
}
|
||||
|
||||
|
||||
if ((status & 0x01) == 0x01){
|
||||
printf("Error occured during read!\n");
|
||||
return;
|
||||
}
|
||||
|
||||
//Transfer 256 16-bit values, a uint16_t at a time, into your buffer from I/O port 0x1F0.
|
||||
if( status & 0x01){
|
||||
printf("Error!\n");
|
||||
printf("Status: 0x%x\n", status);
|
||||
uint16_t error_register = inb(DEVICE_CHANNEL | 1);
|
||||
printf("Error register 0x%x\n",error_register );
|
||||
return ;
|
||||
}
|
||||
for ( int i = 0; i < 256; i++){
|
||||
uint16_t data;
|
||||
asm volatile ("inw %1, %0" : "=a"(data): "Nd"(DEVICE_CHANNEL));
|
||||
// printf (" %x ", data);
|
||||
|
||||
buffer[i] = data;
|
||||
}
|
||||
|
||||
//Then loop back to waiting for the next IRQ (or poll again -- see next note) for each successive sector.
|
||||
|
||||
}
|
||||
|
||||
void ATA_DEVICE::Write(uint16_t DEVICE_CHANNEL, DEVICE_DRIVE drive) {
|
||||
printf("Not implemented\n");
|
||||
}
|
31
source/kernel/drivers/ata/ataDevice.h
Normal file
31
source/kernel/drivers/ata/ataDevice.h
Normal file
@ -0,0 +1,31 @@
|
||||
#pragma once
|
||||
#include <stdint.h>
|
||||
#include "../io/io.h"
|
||||
#include "../ide/ideCommands.h"
|
||||
#include "../ide/sampleIDE.definitions.h"
|
||||
|
||||
#include "../../terminal/kterm.h"
|
||||
|
||||
/*
|
||||
* This first driver wil make use of IO ports.
|
||||
* Doing so means reading or writing from disk is going
|
||||
* to be very cpu intensive.
|
||||
*
|
||||
*/
|
||||
|
||||
enum DEVICE_DRIVE{
|
||||
MASTER = 0xA0,
|
||||
SLAVE = 0xB0
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
namespace ATA_DEVICE{
|
||||
void Identify(uint16_t, DEVICE_DRIVE);
|
||||
void Read (uint16_t, DEVICE_DRIVE, uint32_t, uint16_t*);
|
||||
void Write(uint16_t, DEVICE_DRIVE);
|
||||
void Soft_Reset(uint8_t ,DEVICE_DRIVE );
|
||||
};
|
||||
|
||||
|
145
source/kernel/drivers/atapi/atapiDevice.cpp
Normal file
145
source/kernel/drivers/atapi/atapiDevice.cpp
Normal file
@ -0,0 +1,145 @@
|
||||
#include "atapiDevice.h"
|
||||
#define IS_BIT_SET(x, bit) ((x >> bit & 0x1) == 1)
|
||||
|
||||
bool isPacketDevice(){
|
||||
|
||||
uint8_t LBAmid = inb(0x174);
|
||||
uint8_t LBAhi = inb(0x175);
|
||||
|
||||
printf(" LBAmid: 0x%x, LBAhi: 0x%x");
|
||||
return LBAmid == 0x14 && LBAhi == 0xEB;
|
||||
|
||||
}
|
||||
|
||||
|
||||
void ATAPI_DEVICE::Identify(uint8_t DEVICE_CHANNEL,DEVICE_DRIVE drive ){
|
||||
// lets ignore which port we actually want to check for now !
|
||||
|
||||
/* THE STEPS INVOLVED
|
||||
|
||||
1. Select the target drive by sending master (0x0A) or slave (0x0B) to the
|
||||
drive select IO port
|
||||
|
||||
2. Set the Sectorcount, LBAlo, LBAmid and LBAhi IO ports to 0
|
||||
|
||||
3. Send the identify command (0xEC) to the command IO port
|
||||
|
||||
4. Read the status port
|
||||
4.2 If the value is 0x0 the drive does not exist
|
||||
4.3 If it has any other value continue
|
||||
5. poll the status port until bit 7 is clear.
|
||||
6. Check if the LBAmid and LBAhi ports are non-zero
|
||||
6.2. If non-zero stop polling this is not an ATA device
|
||||
6.3 If zero continue
|
||||
|
||||
7. poll status port until bit 3 is set or bit 0 is set
|
||||
|
||||
8. if err is clear, read the data from the data port
|
||||
|
||||
|
||||
*/
|
||||
|
||||
// Select the target drive
|
||||
outb(0x176, 0xA0); // on the secondary bus select the master drive
|
||||
outb(0x170 + 0x206 , 0x0); // write 0 to the controlport for some reason
|
||||
|
||||
outb(0x176, 0xA0);
|
||||
// read the status port
|
||||
uint8_t status = inb(0x177);
|
||||
printf("status after drive select: 0x%x\n",status);
|
||||
if( status == 0x00){
|
||||
printf("No drive\n");
|
||||
return;
|
||||
}
|
||||
|
||||
outb(0x176, 0xA0);
|
||||
|
||||
|
||||
// Set the Sectorcount, LBAlo, LBAmid and LBAhi IO ports to 0
|
||||
outb(0x172, 0);
|
||||
|
||||
outb(0x173, 0);
|
||||
|
||||
outb(0x174, 0);
|
||||
|
||||
outb(0x175, 0);
|
||||
|
||||
// send the identify command;
|
||||
printf("command sent!\n");
|
||||
outb(0x177, 0xA1);
|
||||
|
||||
// read the status port
|
||||
uint8_t status2 = inb(0x177);
|
||||
if( status2 == 0x00){
|
||||
printf("No drive\n");
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
printf("Waiting until ready...\n");
|
||||
|
||||
while(((status2 & 0x80 == 0x80)
|
||||
&& (status2 & 0x01) != 0x01)
|
||||
) status2 = inb(0x177);
|
||||
|
||||
|
||||
if(status2 & 0x01){
|
||||
printf("Error!");
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
// READ DATA
|
||||
|
||||
uint16_t deviceIdentify [256] ={0};
|
||||
|
||||
for (int i= 0; i < 256; i++){
|
||||
uint16_t data;
|
||||
asm volatile ( "in %1, %0"
|
||||
: "=a"(data)
|
||||
: "Nd"(0x170) );
|
||||
|
||||
|
||||
deviceIdentify[i] = data ;
|
||||
|
||||
|
||||
}
|
||||
|
||||
printf("Model-label (ASCII hex):\n");
|
||||
for(int i = 27; i < 47; i++){
|
||||
printf(" %x ",deviceIdentify[i]);
|
||||
}
|
||||
|
||||
printf("\nSerial number (ASCII hex):\n");
|
||||
for (int i = 10; i < 19; i++){
|
||||
printf(" %x ", deviceIdentify[i]);
|
||||
}
|
||||
|
||||
printf("\nFirmware revision (ASCII hex):\n");
|
||||
for (int i = 23; i < 26; i++){
|
||||
printf(" %x ", deviceIdentify[i]);
|
||||
}
|
||||
|
||||
printf("\nConfiguration: %x\n", deviceIdentify[0]);
|
||||
|
||||
|
||||
|
||||
printf("\nData received!\n");
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
void ATAPI_DEVICE::Read(uint8_t DEVICE_CHANNEL, DEVICE_DRIVE drive) {
|
||||
printf("Not implemented");
|
||||
}
|
||||
|
||||
void ATAPI_DEVICE::Write(uint8_t DEVICE_CHANNEL, DEVICE_DRIVE drive) {
|
||||
printf("Not implemented");
|
||||
}
|
||||
|
||||
|
29
source/kernel/drivers/atapi/atapiDevice.h
Normal file
29
source/kernel/drivers/atapi/atapiDevice.h
Normal file
@ -0,0 +1,29 @@
|
||||
#pragma once
|
||||
#include <stdint.h>
|
||||
#include "../io/io.h"
|
||||
#include "../ide/ideCommands.h"
|
||||
#include "../ide/sampleIDE.definitions.h"
|
||||
|
||||
#include "../../terminal/kterm.h"
|
||||
|
||||
/*
|
||||
* This first driver wil make use of IO ports.
|
||||
* Doing so means reading or writing from disk is going
|
||||
* to be very cpu intensive.
|
||||
*
|
||||
*/
|
||||
|
||||
enum DEVICE_DRIVE{
|
||||
MASTER = 0xA0,
|
||||
SLAVE = 0xB0
|
||||
};
|
||||
|
||||
|
||||
namespace ATAPI_DEVICE
|
||||
{
|
||||
bool isPacketDevice();
|
||||
void Identify ( uint8_t, DEVICE_DRIVE );
|
||||
void Read ( uint8_t, DEVICE_DRIVE );
|
||||
void Write ( uint8_t, DEVICE_DRIVE );
|
||||
|
||||
};
|
38
source/kernel/drivers/cmos/cmos.cpp
Normal file
38
source/kernel/drivers/cmos/cmos.cpp
Normal file
@ -0,0 +1,38 @@
|
||||
void ReadFromCMOS(unsigned char array[])
|
||||
{
|
||||
unsigned char tvalue, index;
|
||||
|
||||
for (index = 0; index < 128; index++)
|
||||
{
|
||||
asm(
|
||||
"cli\n\t" // Disable interrupts
|
||||
"mov al, index\n\t" // Move index address
|
||||
// since the 0x80 bit of al is not set, NMI is active
|
||||
"out 0x70,al\n\t" // Copy address to CMOS register
|
||||
// some kind of real delay here is probably best
|
||||
"in al,0x71\n\t" // Fetch 1 byte to al
|
||||
"sti\n\t" // Enable interrupts
|
||||
"mov tvalue,al\n\t");
|
||||
|
||||
array[index] = tvalue;
|
||||
}
|
||||
}
|
||||
|
||||
void WriteTOCMOS(unsigned char array[])
|
||||
{
|
||||
unsigned char index;
|
||||
|
||||
for(index = 0; index < 128; index++)
|
||||
{
|
||||
unsigned char tvalue = array[index];
|
||||
|
||||
asm("cli\n\t" // Clear interrupts
|
||||
"mov al,index\n\t" // move index address
|
||||
"out 0x70,al\n\t" // copy address to CMOS register
|
||||
// some kind of real delay here is probably best
|
||||
"mov al,tvalue\n\t" // move value to al
|
||||
"out 0x71,al\n\t" // write 1 byte to CMOS
|
||||
"sti\n\\t" ); // Enable interrupts
|
||||
|
||||
}
|
||||
}
|
98
source/kernel/drivers/ide/ide.h
Normal file
98
source/kernel/drivers/ide/ide.h
Normal file
@ -0,0 +1,98 @@
|
||||
#pragma once
|
||||
#include <stdint.h>
|
||||
#include "../pci/pciDevice.h"
|
||||
#include "../pci/pci.h"
|
||||
#include "../../terminal/kterm.h"
|
||||
#include "ideCommands.h"
|
||||
#include "sampleIDE.h"
|
||||
|
||||
#define IS_BIT_SET(x, bit) ((x >> bit & 0x1) == 1)
|
||||
|
||||
IDEChannelRegisters channels[2];
|
||||
IDE_DEVICE ide_devices[4];
|
||||
|
||||
inline void CheckProgIF(uint8_t ProgIF){
|
||||
if( IS_BIT_SET(ProgIF, 0) ) // Is the 0th bit set
|
||||
{
|
||||
printf ("Primary Channel is in PCI native mode\n");
|
||||
} else{
|
||||
printf("Primary Channel is in Compatibility mode\n");
|
||||
}
|
||||
|
||||
if( IS_BIT_SET(ProgIF, 1)){
|
||||
printf("Bit 0 can be modified\n");
|
||||
}else{
|
||||
printf("Bit 0 cannot be modified\n");
|
||||
}
|
||||
|
||||
if( IS_BIT_SET(ProgIF, 2)){
|
||||
printf("Secondary channel is in PCI native mode\n");
|
||||
}else{
|
||||
printf("Secondary channel is in Compatibility mode\n");
|
||||
}
|
||||
|
||||
if( IS_BIT_SET(ProgIF, 3)){
|
||||
printf("Bit 2 can be modified\n");
|
||||
}else{
|
||||
printf("Bit 2 cannot be modified\n");
|
||||
}
|
||||
|
||||
|
||||
if( IS_BIT_SET(ProgIF , 7)){
|
||||
printf("This is a bus master IDE Controller\n");
|
||||
} else{
|
||||
printf("This controller doesn't support DMA!\n");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
inline void TestIDEController(){
|
||||
// Do stuff
|
||||
printf("Testing IDE controllers\n");
|
||||
|
||||
// NOTE: Testing done with a hard coded known PCI addres
|
||||
// Of an intel PIIX3 IDE Controller
|
||||
int bus = 0;
|
||||
int device =1 , function = 1;
|
||||
PCIBusAddress IDEControllerPCIAddress = PCIBusAddress{bus,device, function};
|
||||
|
||||
uint8_t ProgIF = GetProgIF(IDEControllerPCIAddress);
|
||||
printf( "ProgIF: 0x%x\n" ,ProgIF);
|
||||
|
||||
//CheckProgIF(ProgIF);
|
||||
|
||||
// For this test will just assume all bits are set
|
||||
// the CheckProgIF can check but on the test machine all bits are set anyways
|
||||
|
||||
uint32_t BAR0,BAR1,BAR2,BAR3, BAR4;
|
||||
|
||||
BAR0 = ReadBAR(IDEControllerPCIAddress, 0);
|
||||
|
||||
BAR1 = ReadBAR(IDEControllerPCIAddress, 1);
|
||||
|
||||
BAR2 = ReadBAR(IDEControllerPCIAddress, 2);
|
||||
|
||||
BAR3 = ReadBAR(IDEControllerPCIAddress, 3);
|
||||
|
||||
BAR4 = ReadBAR(IDEControllerPCIAddress, 4);
|
||||
|
||||
// All bars are return 0xffffff for some as of yet mysterious reason!
|
||||
printf( "BAR 0: 0x%x\n", BAR0);
|
||||
|
||||
printf( "BAR 1: 0x%x\n", BAR1);
|
||||
|
||||
printf( "BAR 2: 0x%x\n", BAR2);
|
||||
|
||||
printf( "BAR 3: 0x%x\n", BAR3);
|
||||
|
||||
printf( "BAR 4: 0x%x\n", BAR4);
|
||||
|
||||
init_IDE(BAR0, BAR1, BAR2, BAR3, BAR4);
|
||||
|
||||
// Read Something from disc
|
||||
unsigned int maxByteCount = 20 ;
|
||||
void* MDA_buffer = (void*)0xC0000000;
|
||||
|
||||
|
||||
|
||||
}
|
86
source/kernel/drivers/ide/ideCommands.h
Normal file
86
source/kernel/drivers/ide/ideCommands.h
Normal file
@ -0,0 +1,86 @@
|
||||
#pragma once
|
||||
|
||||
// Commands
|
||||
#define ATA_CMD_READ_PIO 0x20
|
||||
#define ATA_CMD_READ_PIO_EXT 0x24
|
||||
#define ATA_CMD_READ_DMA 0xC8
|
||||
#define ATA_CMD_READ_DMA_EXT 0x25
|
||||
#define ATA_CMD_WRITE_PIO 0x30
|
||||
#define ATA_CMD_WRITE_PIO_EXT 0x34
|
||||
#define ATA_CMD_WRITE_DMA 0xCA
|
||||
#define ATA_CMD_WRITE_DMA_EXT 0x35
|
||||
#define ATA_CMD_CACHE_FLUSH 0xE7
|
||||
#define ATA_CMD_CACHE_FLUSH_EXT 0xEA
|
||||
#define ATA_CMD_PACKET 0xA0
|
||||
#define ATA_CMD_IDENTIFY_PACKET 0xA1
|
||||
#define ATA_CMD_IDENTIFY 0xEC
|
||||
|
||||
#define ATAPI_CMD_READ 0xA8
|
||||
#define ATAPI_CMD_EJECT 0x1B
|
||||
|
||||
#define ATA_IDENT_DEVICETYPE 0
|
||||
#define ATA_IDENT_CYLINDERS 2
|
||||
#define ATA_IDENT_HEADS 6
|
||||
#define ATA_IDENT_SECTORS 12
|
||||
#define ATA_IDENT_SERIAL 20
|
||||
#define ATA_IDENT_MODEL 54
|
||||
#define ATA_IDENT_CAPABILITIES 98
|
||||
#define ATA_IDENT_FIELDVALID 106
|
||||
#define ATA_IDENT_MAX_LBA 120
|
||||
#define ATA_IDENT_COMMANDSETS 164
|
||||
#define ATA_IDENT_MAX_LBA_EXT 200
|
||||
|
||||
#define IDE_ATA 0x00
|
||||
#define IDE_ATAPI 0x01
|
||||
|
||||
#define ATA_MASTER 0x00
|
||||
#define ATA_SLAVE 0x01
|
||||
|
||||
|
||||
#define ATA_REG_DATA 0x00
|
||||
#define ATA_REG_ERROR 0x01
|
||||
#define ATA_REG_FEATURES 0x01
|
||||
#define ATA_REG_SECCOUNT0 0x02
|
||||
#define ATA_REG_LBA0 0x03
|
||||
#define ATA_REG_LBA1 0x04
|
||||
#define ATA_REG_LBA2 0x05
|
||||
#define ATA_REG_HDDEVSEL 0x06
|
||||
#define ATA_REG_COMMAND 0x07
|
||||
#define ATA_REG_STATUS 0x07
|
||||
#define ATA_REG_SECCOUNT1 0x08
|
||||
#define ATA_REG_LBA3 0x09
|
||||
#define ATA_REG_LBA4 0x0A
|
||||
#define ATA_REG_LBA5 0x0B
|
||||
#define ATA_REG_CONTROL 0x0C
|
||||
#define ATA_REG_ALTSTATUS 0x0C
|
||||
#define ATA_REG_DEVADDRESS 0x0D
|
||||
|
||||
// Channels:
|
||||
#define ATA_PRIMARY 0x00
|
||||
#define ATA_SECONDARY 0x01
|
||||
|
||||
// Directions:
|
||||
#define ATA_READ 0x00
|
||||
#define ATA_WRITE 0x01
|
||||
|
||||
|
||||
// Status
|
||||
#define ATA_SR_BSY 0x80 // Busy
|
||||
#define ATA_SR_DRDY 0x40 // Drive ready
|
||||
#define ATA_SR_DF 0x20 // Drive write fault
|
||||
#define ATA_SR_DSC 0x10 // Drive seek complete
|
||||
#define ATA_SR_DRQ 0x08 // Data request ready
|
||||
#define ATA_SR_CORR 0x04 // Corrected data
|
||||
#define ATA_SR_IDX 0x02 // Index
|
||||
#define ATA_SR_ERR 0x01 // Error
|
||||
|
||||
|
||||
// Errors
|
||||
#define ATA_ER_BBK 0x80 // Bad block
|
||||
#define ATA_ER_UNC 0x40 // Uncorrectable data
|
||||
#define ATA_ER_MC 0x20 // Media changed
|
||||
#define ATA_ER_IDNF 0x10 // ID mark not found
|
||||
#define ATA_ER_MCR 0x08 // Media change request
|
||||
#define ATA_ER_ABRT 0x04 // Command aborted
|
||||
#define ATA_ER_TK0NF 0x02 // Track 0 not found
|
||||
#define ATA_ER_AMNF 0x01 // No address mark
|
29
source/kernel/drivers/ide/sampleIDE.definitions.h
Normal file
29
source/kernel/drivers/ide/sampleIDE.definitions.h
Normal file
@ -0,0 +1,29 @@
|
||||
#pragma once
|
||||
|
||||
struct IDEChannelRegisters{
|
||||
unsigned short base; // I/O Base.
|
||||
unsigned short ctrl; // Control Base
|
||||
unsigned short bmide; // Bus Master IDE
|
||||
unsigned char nIEN; // IEN (no interrupt)
|
||||
};
|
||||
|
||||
|
||||
struct IDE_DEVICE {
|
||||
unsigned char Reserved; // 0 (Empty) or 1 (This device exists).
|
||||
unsigned char Channel; // 0 (Primary Channel) or 1 (Secondary Channel).
|
||||
unsigned char Drive; // 0 (Master Drive) or 1 (Slave Drive).
|
||||
unsigned short Type; // 0 ATA, 1:ATAPI
|
||||
unsigned short Signature; // Drive Signature
|
||||
unsigned short Capabilities; // Features.
|
||||
unsigned int CommandSets; // Command Sets Supported.
|
||||
unsigned int Size; // Size in Sectors (NOTE: Seems unused nowadays as i've only seen the value be zero
|
||||
unsigned char Model[41]; // Model in string.
|
||||
} ;
|
||||
|
||||
|
||||
|
||||
extern IDEChannelRegisters channels[2];
|
||||
extern IDE_DEVICE ide_devices[4];
|
||||
extern unsigned char ide_buf[2048];
|
||||
extern unsigned char ide_irq_invoked;
|
||||
extern unsigned char atapi_packet[12];
|
242
source/kernel/drivers/ide/sampleIDE.h
Normal file
242
source/kernel/drivers/ide/sampleIDE.h
Normal file
@ -0,0 +1,242 @@
|
||||
#pragma once
|
||||
#include <stdint.h>
|
||||
#include "../../terminal/kterm.h"
|
||||
#include "sampleIDE.definitions.h"
|
||||
#include "ideCommands.h"
|
||||
|
||||
void Detect_IO_Ports(uint32_t BAR0, uint32_t BAR1,uint32_t BAR2, uint32_t BAR3, uint32_t BAR4);
|
||||
void DetectDevices();
|
||||
|
||||
unsigned char ide_buf[2048] = {0};
|
||||
unsigned char ide_irq_invoked = 0;
|
||||
unsigned char atapi_packet[12] = {0xA8,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
|
||||
|
||||
void wait(int t){
|
||||
volatile int i,j;
|
||||
for(i=0;i<t;i++)
|
||||
for(j=0;j<25000;j++)
|
||||
asm("NOP");
|
||||
}
|
||||
|
||||
void ide_write(unsigned char channel, unsigned char reg, unsigned char data){
|
||||
if (reg > 0x07 && reg < 0x0C)
|
||||
ide_write(channel, ATA_REG_CONTROL, 0x80 | channels[channel].nIEN);
|
||||
if (reg < 0x08)
|
||||
outb(channels[channel].base + reg - 0x00, data);
|
||||
else if (reg < 0x0C)
|
||||
outb(channels[channel].base + reg - 0x06, data);
|
||||
else if (reg < 0x0E)
|
||||
outb(channels[channel].ctrl + reg - 0x0A, data);
|
||||
else if (reg < 0x16)
|
||||
outb(channels[channel].bmide + reg - 0x0E, data);
|
||||
if (reg > 0x07 && reg < 0x0C)
|
||||
ide_write(channel, ATA_REG_CONTROL, channels[channel].nIEN);
|
||||
}
|
||||
|
||||
unsigned char ide_read(unsigned char channel, unsigned char reg){
|
||||
unsigned char result;
|
||||
if( reg > 0x07 && reg < 0x0C)
|
||||
ide_write(channel,ATA_REG_CONTROL, 0x80 | channels[channel].nIEN);
|
||||
if( reg < 0x08)
|
||||
result = inb(channels[channel].base + reg - 0x00);
|
||||
else if (reg < 0x0C)
|
||||
result = inb(channels[channel].base + reg - 0x06);
|
||||
else if (reg < 0x0E)
|
||||
result = inb(channels[channel].ctrl + reg - 0x0A);
|
||||
else if (reg < 0x16)
|
||||
result = inb(channels[channel].bmide + reg - 0x0E);
|
||||
if (reg > 0x07 && reg < 0x0C)
|
||||
ide_write(channel, ATA_REG_CONTROL, channels[channel].nIEN);
|
||||
return result;
|
||||
}
|
||||
|
||||
void ide_read_buffer(unsigned char channel, unsigned char reg, unsigned int buffer, unsigned int quads){
|
||||
if (reg > 0x07 && reg < 0x0C)
|
||||
ide_write(channel, ATA_REG_CONTROL, 0x80 | channels[channel].nIEN);
|
||||
if (reg < 0x08)
|
||||
insl(channels[channel].base + reg - 0x00, (void *)buffer, quads);
|
||||
else if (reg < 0x0C)
|
||||
insl(channels[channel].base + reg - 0x06, (void *)buffer, quads);
|
||||
else if (reg < 0x0E)
|
||||
insl(channels[channel].ctrl + reg - 0x0A, (void *)buffer, quads);
|
||||
else if (reg < 0x16)
|
||||
insl(channels[channel].bmide + reg - 0x0E, (void *)buffer, quads);
|
||||
if (reg > 0x07 && reg < 0x0C)
|
||||
ide_write(channel, ATA_REG_CONTROL, channels[channel].nIEN);
|
||||
}
|
||||
|
||||
unsigned char ide_polling(unsigned char channel, unsigned int advanced_check) {
|
||||
|
||||
// (I) Delay 400 nanosecond for BSY to be set:
|
||||
// -------------------------------------------------
|
||||
for(int i = 0; i < 4; i++)
|
||||
ide_read(channel, ATA_REG_ALTSTATUS); // Reading the Alternate Status port wastes 100ns; loop four times.
|
||||
|
||||
// (II) Wait for BSY to be cleared:
|
||||
// -------------------------------------------------
|
||||
while (ide_read(channel, ATA_REG_STATUS) & ATA_SR_BSY)
|
||||
; // Wait for BSY to be zero.
|
||||
|
||||
if (advanced_check) {
|
||||
unsigned char state = ide_read(channel, ATA_REG_STATUS); // Read Status Register.
|
||||
|
||||
// (III) Check For Errors:
|
||||
// -------------------------------------------------
|
||||
if (state & ATA_SR_ERR)
|
||||
return 2; // Error.
|
||||
|
||||
// (IV) Check If Device fault:
|
||||
// -------------------------------------------------
|
||||
if (state & ATA_SR_DF)
|
||||
return 1; // Device Fault.
|
||||
|
||||
// (V) Check DRQ:
|
||||
// -------------------------------------------------
|
||||
// BSY = 0; DF = 0; ERR = 0 so we should check for DRQ now.
|
||||
if ((state & ATA_SR_DRQ) == 0)
|
||||
return 3; // DRQ should be set
|
||||
|
||||
}
|
||||
|
||||
return 0; // No Error.
|
||||
|
||||
}
|
||||
|
||||
unsigned char ide_print_error(unsigned int drive, unsigned char err) {
|
||||
if (err == 0)
|
||||
return err;
|
||||
|
||||
printf("IDE:");
|
||||
if (err == 1) {printf("- Device Fault\n "); err = 19;}
|
||||
else if (err == 2) {
|
||||
unsigned char st = ide_read(ide_devices[drive].Channel, ATA_REG_ERROR);
|
||||
if (st & ATA_ER_AMNF) {printf("- No Address Mark Found\n "); err = 7;}
|
||||
if (st & ATA_ER_TK0NF) {printf("- No Media or Media Error\n "); err = 3;}
|
||||
if (st & ATA_ER_ABRT) {printf("- Command Aborted\n "); err = 20;}
|
||||
if (st & ATA_ER_MCR) {printf("- No Media or Media Error\n "); err = 3;}
|
||||
if (st & ATA_ER_IDNF) {printf("- ID mark not Found\n "); err = 21;}
|
||||
if (st & ATA_ER_MC) {printf("- No Media or Media Error\n "); err = 3;}
|
||||
if (st & ATA_ER_UNC) {printf("- Uncorrectable Data Error\n "); err = 22;}
|
||||
if (st & ATA_ER_BBK) {printf("- Bad Sectors\n "); err = 13;}
|
||||
} else if (err == 3) {printf("- Reads Nothing\n "); err = 23;}
|
||||
else if (err == 4) {printf("- Write Protected\n "); err = 8;}
|
||||
printf("- [%s %s] %s\n",
|
||||
(const char *[]){"Primary", "Secondary"}[ide_devices[drive].Channel], // Use the channel as an index into the array
|
||||
(const char *[]){"Master", "Slave"}[ide_devices[drive].Drive], // Same as above, using the drive
|
||||
ide_devices[drive].Model);
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
|
||||
inline void init_IDE( uint32_t BAR0, uint32_t BAR1,uint32_t BAR2, uint32_t BAR3, uint32_t BAR4)
|
||||
{
|
||||
Detect_IO_Ports( BAR0, BAR1, BAR2, BAR3, BAR4);
|
||||
|
||||
printf("ATA Primary port, base: 0x%x, ctrl: 0x%x\n", channels[ATA_PRIMARY].base , channels[ATA_PRIMARY].ctrl);
|
||||
printf("ATA Secondary port, base: 0x%x, ctrl: 0x%x\n", channels[ATA_SECONDARY].base , channels[ATA_SECONDARY].ctrl);
|
||||
|
||||
// 2- Disable IRQs:
|
||||
ide_write(ATA_PRIMARY , ATA_REG_CONTROL, 2);
|
||||
ide_write(ATA_SECONDARY, ATA_REG_CONTROL, 2);
|
||||
|
||||
DetectDevices();
|
||||
|
||||
return;
|
||||
// 4- Print Summary:
|
||||
for (int i = 0; i < 4; i++)
|
||||
if (ide_devices[i].Reserved == 1) {
|
||||
printf(" Found %s Drive %d bytes - %x\n",
|
||||
(const char *[]){"ATA", "ATAPI"}[ide_devices[i].Type], /* Type */
|
||||
ide_devices[i].Size / 2, /* Size */
|
||||
ide_devices[i].Model);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 3- Detect ATA-ATAPI Devices:
|
||||
inline void DetectDevices(){
|
||||
int i, j, k, count = 0;
|
||||
|
||||
for (i = 0; i < 2; i++)
|
||||
for (j = 0; j < 2; j++) {
|
||||
|
||||
unsigned char err = 0, type = IDE_ATA, status;
|
||||
ide_devices[count].Reserved = 0; // Assuming that no drive here.
|
||||
|
||||
// (I) Select Drive:
|
||||
ide_write(i, ATA_REG_HDDEVSEL, 0xA0 | (j << 4)); // Select Drive.
|
||||
wait(1000); // Wait 1ms for drive select to work.
|
||||
|
||||
// (II) Send ATA Identify Command:
|
||||
ide_write(i, ATA_REG_COMMAND, ATA_CMD_IDENTIFY);
|
||||
wait(1000);
|
||||
|
||||
// (III) Polling:
|
||||
if (ide_read(i, ATA_REG_STATUS) == 0) continue; // If Status = 0, No Device.
|
||||
|
||||
while(1) {
|
||||
status = ide_read(i, ATA_REG_STATUS);
|
||||
if ((status & ATA_SR_ERR)) {err = 1; break;} // If Err, Device is not ATA.
|
||||
if (!(status & ATA_SR_BSY) && (status & ATA_SR_DRQ)) break; // Everything is right.
|
||||
}
|
||||
|
||||
// (IV) Probe for ATAPI Devices:
|
||||
if (err != 0) {
|
||||
unsigned char cl = ide_read(i, ATA_REG_LBA1);
|
||||
unsigned char ch = ide_read(i, ATA_REG_LBA2);
|
||||
|
||||
if (cl == 0x14 && ch ==0xEB)
|
||||
type = IDE_ATAPI;
|
||||
else if (cl == 0x69 && ch == 0x96)
|
||||
type = IDE_ATAPI;
|
||||
else
|
||||
continue; // Unknown Type (may not be a device).
|
||||
|
||||
ide_write(i, ATA_REG_COMMAND, ATA_CMD_IDENTIFY_PACKET);
|
||||
wait(1000);
|
||||
}
|
||||
|
||||
// (V) Read Identification Space of the Device:
|
||||
ide_read_buffer(i, ATA_REG_DATA, (unsigned int) ide_buf, 128);
|
||||
|
||||
// (VI) Read Device Parameters:
|
||||
ide_devices[count].Reserved = 1;
|
||||
ide_devices[count].Type = type;
|
||||
ide_devices[count].Channel = i;
|
||||
ide_devices[count].Drive = j;
|
||||
ide_devices[count].Signature = *((unsigned short *)(ide_buf + ATA_IDENT_DEVICETYPE));
|
||||
ide_devices[count].Capabilities = *((unsigned short *)(ide_buf + ATA_IDENT_CAPABILITIES));
|
||||
ide_devices[count].CommandSets = *((unsigned int *)(ide_buf + ATA_IDENT_COMMANDSETS));
|
||||
|
||||
// (VII) Get Size:
|
||||
if (ide_devices[count].CommandSets & (1 << 26))
|
||||
// Device uses 48-Bit Addressing:
|
||||
ide_devices[count].Size = *((unsigned int *)(ide_buf + ATA_IDENT_MAX_LBA_EXT));
|
||||
else
|
||||
// Device uses CHS or 28-bit Addressing:
|
||||
ide_devices[count].Size = *((unsigned int *)(ide_buf + ATA_IDENT_MAX_LBA));
|
||||
|
||||
// (VIII) String indicates model of device (like Western Digital HDD and SONY DVD-RW...):
|
||||
for(k = 0; k < 40; k += 2) {
|
||||
ide_devices[count].Model[k] = ide_buf[ATA_IDENT_MODEL + k + 1];
|
||||
ide_devices[count].Model[k + 1] = ide_buf[ATA_IDENT_MODEL + k];}
|
||||
ide_devices[count].Model[40] = 0; // Terminate String.
|
||||
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
inline void Detect_IO_Ports(uint32_t BAR0, uint32_t BAR1,uint32_t BAR2, uint32_t BAR3, uint32_t BAR4){
|
||||
// 1 Detect I/O Ports which interface an IDE Controller
|
||||
|
||||
// Based on the implementation within serenity
|
||||
channels[ATA_PRIMARY].base = (BAR0 == 0x1 || BAR0 == 0x0) ? 0x1F0 : BAR0 & (~1);
|
||||
channels[ATA_PRIMARY ].ctrl = (BAR1 == 0x1 || BAR1 == 0x0) ? 0x3F6 : BAR1 & (~1);
|
||||
channels[ATA_SECONDARY].base = (BAR2 == 0x1 || BAR2 == 0x0) ? 0x170 : BAR2 & (~1);
|
||||
channels[ATA_SECONDARY].ctrl = (BAR3 == 0x1 || BAR3 == 0x0) ? 0x376 : BAR3 & (~1);
|
||||
channels[ATA_PRIMARY ].bmide = (BAR4 & (~1)) + 0; // Bus Master IDE
|
||||
channels[ATA_SECONDARY].bmide = (BAR4 & (~1)) + 8; // Bus Master IDE
|
||||
|
||||
}
|
@ -12,9 +12,10 @@ unsigned short inw_p(unsigned short ){
|
||||
// TODO: implement me!
|
||||
return 0;
|
||||
}
|
||||
unsigned int inl(unsigned short ){
|
||||
// TODO: implement me!
|
||||
return 0;
|
||||
uint32_t inl( int port ){
|
||||
unsigned int data;
|
||||
asm volatile ("inl %w1, %0": "=a" (data): "d" (port));
|
||||
return data;
|
||||
}
|
||||
unsigned int inl_p(unsigned short ){
|
||||
// TODO: implement me!
|
||||
@ -22,7 +23,7 @@ unsigned int inl_p(unsigned short ){
|
||||
}
|
||||
|
||||
|
||||
void outb_p(unsigned char , unsigned short ){
|
||||
void b_p(unsigned char , unsigned short ){
|
||||
|
||||
}
|
||||
void outw(unsigned short , unsigned short ){
|
||||
@ -31,9 +32,12 @@ void outw(unsigned short , unsigned short ){
|
||||
void outw_p(unsigned short , unsigned short ){
|
||||
|
||||
}
|
||||
void outl(unsigned int , unsigned short ){
|
||||
|
||||
void outl( int port , uint32_t data ){
|
||||
asm volatile ("outl %0, %1" :: "a" (data), "dn"(port));
|
||||
}
|
||||
|
||||
|
||||
void outl_p(unsigned int , unsigned short ){
|
||||
|
||||
}
|
@ -12,21 +12,17 @@ static inline uint8_t inb(uint16_t port)
|
||||
unsigned char inb_p(unsigned short port);
|
||||
unsigned short inw(unsigned short port);
|
||||
unsigned short inw_p(unsigned short port);
|
||||
unsigned int inl(unsigned short port);
|
||||
uint32_t inl( int port );
|
||||
unsigned int inl_p(unsigned short port);
|
||||
|
||||
static inline void outb(uint16_t port, uint8_t val)
|
||||
{
|
||||
asm volatile ( "outb %0, %1" : : "a"(val), "Nd"(port) );
|
||||
/* There's an outb %al, $imm8 encoding, for compile-time constant port numbers that fit in 8b. (N constraint).
|
||||
* Wider immediate constants would be truncated at assemble-time (e.g. "i" constraint).
|
||||
* The outb %al, %dx encoding is the only option for all other cases.
|
||||
* %1 expands to %dx because port is a uint16_t. %w1 could be used if we had the port number a wider C type */
|
||||
}
|
||||
void outb_p(unsigned char value, unsigned short port);
|
||||
void outw(unsigned short value, unsigned short port);
|
||||
void outw_p(unsigned short value, unsigned short port);
|
||||
void outl(unsigned int value, unsigned short port);
|
||||
void outl( int port , uint32_t data );
|
||||
void outl_p(unsigned int value, unsigned short port);
|
||||
|
||||
void insb(unsigned short port, void *addr,
|
253
source/kernel/drivers/pci/pci.cpp
Normal file
253
source/kernel/drivers/pci/pci.cpp
Normal file
@ -0,0 +1,253 @@
|
||||
#include "pci.h"
|
||||
|
||||
#define PCI_BUS_ADDR_SHIFT 16
|
||||
#define PCI_DEVICE_ADDR_SHIFT 11
|
||||
#define PCI_FUNCTION_ADDR_SHIFT 8
|
||||
#define PCI_ENABLE_ADDR_SHIFT 31
|
||||
|
||||
const char* GetClassCodeName (uint64_t ClassCode ) {
|
||||
|
||||
switch (ClassCode)
|
||||
{
|
||||
case 0x0 :
|
||||
return "Unclassified";
|
||||
break;
|
||||
|
||||
case 0x1:
|
||||
return "Mass Storage Controller";
|
||||
break;
|
||||
|
||||
case 0x2:
|
||||
return "Network Controller";
|
||||
break;
|
||||
|
||||
case 0x3:
|
||||
return "Display Controller";
|
||||
break;
|
||||
|
||||
case 0x4:
|
||||
return "Multimedia Controller";
|
||||
break;
|
||||
|
||||
case 0x5:
|
||||
return "Memory Controller";
|
||||
break;
|
||||
|
||||
case 0x6:
|
||||
return "Bridge";
|
||||
break;
|
||||
|
||||
case 0x7 :
|
||||
return "Simple Communication Controller";
|
||||
break;
|
||||
|
||||
case 0x8:
|
||||
return "Base System Peripheral";
|
||||
break;
|
||||
|
||||
case 0x9:
|
||||
return "Input Device Controller";
|
||||
break;
|
||||
|
||||
case 0xA:
|
||||
return "Docking station";
|
||||
break;
|
||||
case 0xB:
|
||||
return "Processor";
|
||||
break;
|
||||
|
||||
case 0xC:
|
||||
return "Serial Bus Controller";
|
||||
break;
|
||||
|
||||
case 0xD:
|
||||
return "Wireless Controller";
|
||||
break;
|
||||
|
||||
case 0xE:
|
||||
return "Intelligent Controller";
|
||||
break;
|
||||
|
||||
case 0xF:
|
||||
return "Satellite Communication Controller";
|
||||
break;
|
||||
|
||||
case 0x10:
|
||||
return "Encryption Controller";
|
||||
break;
|
||||
|
||||
case 0x11:
|
||||
return "Signal Processing Controller";
|
||||
break;
|
||||
|
||||
case 0x12:
|
||||
return "Processing Accelerator";
|
||||
break;
|
||||
|
||||
case 0x13:
|
||||
return "Non-Essential Instrumentation";
|
||||
break;
|
||||
|
||||
default:
|
||||
return "Unknown";
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
const char* getVendor( uint32_t VendorID){
|
||||
switch (VendorID)
|
||||
{
|
||||
case 0x8086:
|
||||
return "Intel Corporation";
|
||||
break;
|
||||
|
||||
case 0x10DE:
|
||||
return "NVIDIA Corporation";
|
||||
break;
|
||||
|
||||
case 0x1022:
|
||||
return "Advanced Micro Devices, Inc.[AMD]";
|
||||
break;
|
||||
|
||||
case 0x1002:
|
||||
return "Advanced Micor Devices, Inc.[AMD/ATI]";
|
||||
break;
|
||||
|
||||
case 0xbeef:
|
||||
return "VirtualBox Graphics Adapter";
|
||||
break;
|
||||
|
||||
case 0xcafe:
|
||||
return "VirtualBox Guest Service";
|
||||
break;
|
||||
|
||||
default:
|
||||
return "Vendor Unkown";
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
uint32_t ConfigReadWord ( PCIBusAddress& PCIDeviceAddress , uint8_t offset){
|
||||
outl(CONFIG_ADDRESS , PCIDeviceAddress.getAddress() | offset );
|
||||
return inl(CONFIG_DATA);
|
||||
}
|
||||
|
||||
uint8_t GetProgIF (PCIBusAddress& PCIDeviceAddress){
|
||||
uint32_t data = ConfigReadWord(PCIDeviceAddress, 0x8);
|
||||
return ((data >> 8) & 0xFF);
|
||||
}
|
||||
|
||||
uint32_t ReadBAR ( PCIBusAddress& PCIDeviceAddress, int bar_number){
|
||||
int offsetToBar = 0x10 + (bar_number* 0x4);
|
||||
return ConfigReadWord(PCIDeviceAddress, offsetToBar);
|
||||
}
|
||||
|
||||
uint32_t ConfigReadWord (uint8_t bus, uint8_t device, uint8_t func, uint8_t offset){
|
||||
uint32_t address;
|
||||
|
||||
address = (uint32_t) (
|
||||
((uint32_t) 1 << PCI_ENABLE_ADDR_SHIFT) |
|
||||
((uint32_t)bus << PCI_BUS_ADDR_SHIFT) |
|
||||
((uint32_t)device << PCI_DEVICE_ADDR_SHIFT) |
|
||||
((uint32_t)func << PCI_FUNCTION_ADDR_SHIFT) |
|
||||
offset );
|
||||
|
||||
outl(CONFIG_ADDRESS, address);
|
||||
|
||||
|
||||
return inl(CONFIG_DATA);
|
||||
}
|
||||
|
||||
uint8_t GetHeaderType( PCIBusAddress& PCIDeviceAddress ){
|
||||
uint32_t header_information = ConfigReadWord(PCIDeviceAddress , 0xC);
|
||||
return (uint8_t) (
|
||||
((header_information >> 16) //Get higher half
|
||||
& 0x00FF) // Select the last two bytes
|
||||
& 0x7F ); // Mask bit 7 as it indicates if the device is a mulit function device!
|
||||
}
|
||||
|
||||
uint16_t GetClassCodes( PCIBusAddress& PCIDeviceAddress ){
|
||||
uint32_t classcodes = ConfigReadWord(PCIDeviceAddress, 0x8);
|
||||
return (uint16_t)((uint32_t)classcodes >> 16);
|
||||
|
||||
}
|
||||
|
||||
bool IsMultiFunctionDevice(PCIBusAddress& PCIDeviceAddress){
|
||||
uint32_t header_information = ConfigReadWord(PCIDeviceAddress, 0xC);
|
||||
return (((header_information>>16)
|
||||
& 0x80)
|
||||
>> 7 );
|
||||
}
|
||||
|
||||
void PrintPCIDeviceInfo (PCIBusAddress& PCIDeviceAddress)
|
||||
{
|
||||
uint32_t DeviceID = (GetDevice(PCIDeviceAddress.bus, PCIDeviceAddress.device, PCIDeviceAddress.function) >> 16);
|
||||
uint32_t VendorID = GetDevice(PCIDeviceAddress.bus, PCIDeviceAddress.device, PCIDeviceAddress.function) & 0xFFFF;
|
||||
printf("Device found!\n");
|
||||
printf("Bus: %d, Device: %d, function: %d \n", PCIDeviceAddress.bus, PCIDeviceAddress.device, PCIDeviceAddress.function);
|
||||
printf("DeviceID: 0x%x, Vendor: %s\n",
|
||||
DeviceID
|
||||
, getVendor(VendorID) );
|
||||
|
||||
|
||||
|
||||
|
||||
uint8_t header_type = GetHeaderType(PCIDeviceAddress);
|
||||
printf( "Header type: 0x%x\n", header_type);
|
||||
|
||||
uint16_t deviceClasses = GetClassCodes(PCIDeviceAddress);
|
||||
printf("class: %s, subClass: %d\n\n", GetClassCodeName((deviceClasses >>8)), deviceClasses & 0xFF);
|
||||
|
||||
}
|
||||
|
||||
void PCI_Enumerate(){
|
||||
|
||||
int devicesFound = 0;
|
||||
|
||||
printf("Start finding devices, Found: %d devices");
|
||||
// loop through all possible busses, devices and their functions;
|
||||
for( int bus = 0 ; bus < 256 ; bus++)
|
||||
{
|
||||
|
||||
for(int device = 0; device < 32 ; device ++)
|
||||
{
|
||||
int function = 0;
|
||||
|
||||
uint64_t DeviceIdentify = ConfigReadWord(bus, device, function,0x0);
|
||||
uint32_t DeviceID = GetDevice(bus, device, function) >> 16;
|
||||
|
||||
if( DeviceID != 0xFFFF){
|
||||
PCIBusAddress busAddress =
|
||||
PCIBusAddress{bus, device, function };
|
||||
|
||||
PrintPCIDeviceInfo(busAddress);
|
||||
|
||||
// iterate over the functions if it is a multi function device!
|
||||
if( IsMultiFunctionDevice(busAddress) ){
|
||||
printf("Multi function device! \n");
|
||||
printf("Check remaining Functions\n");
|
||||
for ( function = 1 ; function < 8; function++)
|
||||
{
|
||||
uint32_t DeviceID = GetDevice(bus, device, function) >> 16;
|
||||
|
||||
if( DeviceID != 0xFFFF){
|
||||
PCIBusAddress busAddress2 = PCIBusAddress{bus, device, function};
|
||||
PrintPCIDeviceInfo(busAddress2);
|
||||
devicesFound++;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
devicesFound++;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
printf("Found %d PCI devices!\n", devicesFound);
|
||||
}
|
||||
|
36
source/kernel/drivers/pci/pci.h
Normal file
36
source/kernel/drivers/pci/pci.h
Normal file
@ -0,0 +1,36 @@
|
||||
#pragma once
|
||||
#include <stdint.h>
|
||||
#include "../io/io.h"
|
||||
#include "../../terminal/kterm.h"
|
||||
#include "pciDevice.h"
|
||||
|
||||
// Configuration Space Access Mechanism #1
|
||||
#define CONFIG_ADDRESS 0xCF8 // Configuration adress that is to be accessed
|
||||
#define CONFIG_DATA 0xCFC // Will do the actual configuration operation
|
||||
|
||||
extern const char* ClassCodeTable [0x13];
|
||||
|
||||
// Note: this could be used to make the api for receiving PCI class codes a bit
|
||||
// nicer.
|
||||
struct ClassCodes {
|
||||
uint8_t ClassCode;
|
||||
uint8_t DeviceClass;
|
||||
}__attribute__((packed));
|
||||
|
||||
uint32_t ConfigReadWord (uint8_t bus, uint8_t device, uint8_t func, uint8_t offset);
|
||||
uint32_t ConfigReadWord ( PCIBusAddress& PCIDeviceAddress , uint8_t offset);
|
||||
|
||||
inline uint64_t GetDevice (int bus, int device, int function ){
|
||||
return ConfigReadWord(bus, device, function,0x0);
|
||||
}
|
||||
|
||||
uint8_t GetHeaderType( PCIBusAddress& PCIDeviceAddress );
|
||||
|
||||
uint16_t GetClassCodes( PCIBusAddress& PICDeviceAddress );
|
||||
const char* getVendor( uint64_t VendorID);
|
||||
const char* GetClassCodeName (uint64_t ClassCode );
|
||||
|
||||
uint8_t GetProgIF (PCIBusAddress& PCIDeviceAddress);
|
||||
void PCI_Enumerate();
|
||||
|
||||
uint32_t ReadBAR ( PCIBusAddress& PCIDeviceAddress, int bar_number);
|
7
source/kernel/drivers/pci/pciDevice.cpp
Normal file
7
source/kernel/drivers/pci/pciDevice.cpp
Normal file
@ -0,0 +1,7 @@
|
||||
#include "pciDevice.h"
|
||||
|
||||
// NOTE: we would really like to return a pointer
|
||||
// to the newly created PCIBusAddress struct;
|
||||
PCIBusAddress const PCIDevice::PCIAddress(){
|
||||
return PCIBusAddress{bus ,device, function};
|
||||
}
|
54
source/kernel/drivers/pci/pciDevice.h
Normal file
54
source/kernel/drivers/pci/pciDevice.h
Normal file
@ -0,0 +1,54 @@
|
||||
#pragma once
|
||||
#include <stdint.h>
|
||||
/*
|
||||
* PCI devices API
|
||||
*/
|
||||
struct PCIBusAddress{
|
||||
|
||||
int bus ;
|
||||
int device ;
|
||||
int function;
|
||||
|
||||
|
||||
uint32_t getAddress( ){
|
||||
return ((uint32_t) 1 << 31) |
|
||||
((uint32_t) bus << 16) |
|
||||
((uint32_t) device << 11)|
|
||||
((uint32_t) function << 8) |
|
||||
0x0000;
|
||||
|
||||
};
|
||||
};
|
||||
|
||||
class PCIDevice {
|
||||
public :
|
||||
PCIDevice (PCIBusAddress* , int );
|
||||
~PCIDevice();
|
||||
PCIBusAddress const PCIAddress();
|
||||
|
||||
|
||||
inline const char* getDeviceString(){
|
||||
return "Not implemented"; //GetClassCodeName(deviceclass);
|
||||
}
|
||||
|
||||
inline const char* getVendorString(){
|
||||
return "Not implemented"; // getVendor(VendorID);
|
||||
}
|
||||
|
||||
inline void setVendorID (uint16_t id) {
|
||||
this->VendorID = id;
|
||||
}
|
||||
|
||||
private:
|
||||
int bus;
|
||||
int device;
|
||||
int function;
|
||||
|
||||
uint16_t VendorID;
|
||||
uint16_t DeviceID;
|
||||
uint8_t deviceclass;
|
||||
uint8_t devicesubclass;
|
||||
|
||||
int headerType;
|
||||
|
||||
};
|
@ -1,5 +1,5 @@
|
||||
#pragma once
|
||||
#include "../io.h"
|
||||
#include "../io/io.h"
|
||||
|
||||
#define PIC1 0x20 /* IO base address for master PIC */
|
||||
#define PIC2 0xA0 /* IO base address for slave PIC */
|
54
source/kernel/drivers/pit/pit.cpp
Normal file
54
source/kernel/drivers/pit/pit.cpp
Normal file
@ -0,0 +1,54 @@
|
||||
#include "pit.h"
|
||||
#include "../../terminal/kterm.h"
|
||||
uint32_t pit_tick = 0;
|
||||
|
||||
|
||||
void pit_initialise()
|
||||
{
|
||||
asm volatile("CLI");
|
||||
|
||||
#ifdef __VERBOSE__
|
||||
printf("Init PIT!\n");
|
||||
#endif
|
||||
// clear mask for IRQ 0
|
||||
uint8_t value = inb(0x21) & ~(1<< 0);
|
||||
outb(0x21, value);
|
||||
|
||||
io_wait();
|
||||
|
||||
const int freq = 500;
|
||||
|
||||
uint32_t divisor = 1193180 / freq;
|
||||
|
||||
outb(PIT_COMMAND, 0x36);
|
||||
|
||||
uint8_t l = (uint8_t) (divisor & 0xFF);
|
||||
uint8_t h = (uint8_t) ( (divisor>>8) & 0xff);
|
||||
|
||||
outb(PIT_DATA_0, l);
|
||||
outb(PIT_DATA_0,h);
|
||||
|
||||
|
||||
asm volatile("STI");
|
||||
}
|
||||
|
||||
|
||||
void get_pit_count()
|
||||
{
|
||||
asm volatile ("CLI");
|
||||
|
||||
outb(PIT_COMMAND, 0);
|
||||
uint16_t count = inb(PIT_DATA_0);
|
||||
count |= inb(PIT_DATA_0) << 8;
|
||||
|
||||
printf("PIT count: 0x%x\n", count);
|
||||
|
||||
asm volatile("STI");
|
||||
|
||||
}
|
||||
|
||||
void set_pit_count()
|
||||
{
|
||||
|
||||
}
|
||||
|
18
source/kernel/drivers/pit/pit.h
Normal file
18
source/kernel/drivers/pit/pit.h
Normal file
@ -0,0 +1,18 @@
|
||||
#pragma once
|
||||
#include <stdint.h>
|
||||
#include "../io/io.h"
|
||||
#define PIT_DATA_0 0x40
|
||||
#define PIT_DATA_1 0x41
|
||||
#define PIT_DATA_2 0x42
|
||||
#define PIT_COMMAND 0x43
|
||||
|
||||
|
||||
extern uint32_t pit_tick;
|
||||
|
||||
|
||||
void pit_initialise();
|
||||
|
||||
|
||||
void get_pit_count();
|
||||
|
||||
void set_pit_count();
|
51
source/kernel/drivers/ps-2/keyboard.cpp
Normal file
51
source/kernel/drivers/ps-2/keyboard.cpp
Normal file
@ -0,0 +1,51 @@
|
||||
#include "keyboard.h"
|
||||
|
||||
KeyPressInfo keyPress {};
|
||||
|
||||
void KeyHandled(){
|
||||
keyPress.ScanCode= 0x00;
|
||||
keyPress.PressedModifiers = 0x00;
|
||||
}
|
||||
|
||||
|
||||
char getASCIIKey(){
|
||||
char keyPressed;
|
||||
// Wait until a key is pressed
|
||||
while(keyPress.ScanCode == 0x00) {
|
||||
asm volatile ("NOP");
|
||||
}
|
||||
|
||||
|
||||
// Translate keycode to ascii
|
||||
// Probably a lookup table might be handy
|
||||
// Until 0x37
|
||||
const char* ASCIILookUp =
|
||||
"\01234567890-=\0\0QWERTYUIOP[]\0\0ASDFGHJKL;\'`\0\\ZXCVBNM,./\0";
|
||||
|
||||
uint8_t ASCII_Index = keyPress.ScanCode - 3 ;
|
||||
//printf("ASCII_INDEX: %x\n", ASCII_Index);
|
||||
keyPressed = ASCIILookUp[ASCII_Index];
|
||||
|
||||
KeyHandled();
|
||||
|
||||
return keyPressed;
|
||||
}
|
||||
|
||||
|
||||
|
||||
uint8_t getKey(){
|
||||
// Wait until a key is pressed
|
||||
while(keyPress.ScanCode == 0x00){
|
||||
asm volatile ("NOP");
|
||||
}
|
||||
|
||||
if( keyPress.ScanCode > 0x37){
|
||||
keyPress.ScanCode = 0x00;
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint8_t ScanCode = keyPress.ScanCode;
|
||||
// KeyHandled();
|
||||
|
||||
return ScanCode ;
|
||||
}
|
34
source/kernel/drivers/ps-2/keyboard.h
Normal file
34
source/kernel/drivers/ps-2/keyboard.h
Normal file
@ -0,0 +1,34 @@
|
||||
#pragma once
|
||||
#include <stdint.h>
|
||||
#include "../../terminal/kterm.h"
|
||||
enum ScanCodeSet {
|
||||
None = 0,
|
||||
ScanCodeSet1 = 1,
|
||||
ScanCodeSet2 = 2,
|
||||
ScanCodeSet3 = 3,
|
||||
};
|
||||
|
||||
enum Modifiers {
|
||||
LSHIFT = 1,
|
||||
RSHIFT = 2,
|
||||
|
||||
LCTRL = 3,
|
||||
RCTRL = 4,
|
||||
|
||||
LALT = 5,
|
||||
RALT = 6
|
||||
};
|
||||
|
||||
struct KeyPressInfo{
|
||||
uint8_t PressedModifiers;
|
||||
uint8_t ScanCode;
|
||||
};
|
||||
|
||||
|
||||
|
||||
extern KeyPressInfo keyPress;
|
||||
|
||||
void KeyHandled();
|
||||
|
||||
char getASCIIKey();
|
||||
uint8_t getKey();
|
29
source/kernel/filesystem/EXT2/SuperBlock.h
Normal file
29
source/kernel/filesystem/EXT2/SuperBlock.h
Normal file
@ -0,0 +1,29 @@
|
||||
#pragma once
|
||||
#include <stdint.h>
|
||||
struct SuperBlock {
|
||||
uint32_t NumberOfInodes;
|
||||
uint32_t NumberOfBlocks;
|
||||
uint32_t NumberOfReservedBlocks;
|
||||
uint32_t NumberOfUnallocatedBlocks;
|
||||
uint32_t NumberOfUnallocatedInodes;
|
||||
uint32_t BlockNumberOfSuperBlock;
|
||||
uint32_t BlockSize;// Something about a shift left
|
||||
uint32_t FragmentSize;
|
||||
uint32_t NumberOfBlocksInGroup;
|
||||
uint32_t NumberOfFragmentsInBlockGroup;
|
||||
uint32_t NumberOfInodesInBlockGroup;
|
||||
uint32_t LastMountTime; // POSIX
|
||||
uint32_t LastWrittenTime; // POSIX
|
||||
uint16_t TimesMountedSinceCheck;
|
||||
uint16_t TimesMountedUntilCheck;
|
||||
uint16_t EXT_SIG ; // 0xef53
|
||||
uint16_t FS_STATE;
|
||||
uint16_t ON_ERR;
|
||||
uint16_t VERSION_MINOR;
|
||||
uint32_t TimeLastCheck; // POSIX
|
||||
uint32_t CheckInterval; //POSIX
|
||||
uint32_t OS_ID; // OS the FS was created with
|
||||
uint32_t VERSION_MAJOR;
|
||||
uint16_t UIDReservedBlocks;
|
||||
uint16_t GIDReservedBlocks;
|
||||
}__attribute__((packed));
|
21
source/kernel/filesystem/FAT/BiosParameterBlock.h
Normal file
21
source/kernel/filesystem/FAT/BiosParameterBlock.h
Normal file
@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
#include <stdint.h>
|
||||
#include "./ExtendBootRecord.h"
|
||||
|
||||
struct BiosParameterBlock {
|
||||
uint8_t BootLoaderCodeSection [3];
|
||||
uint8_t OEM_id [8];
|
||||
uint16_t BytesPerSector ; // I suspect would be 512
|
||||
uint8_t SectorsPerCluster ;
|
||||
uint16_t ReservedSectors;
|
||||
uint8_t NumberOfFileAllocationTables; // Probably equals 2
|
||||
uint16_t NumberOfDirectoryEntries; // Root directory must contain entire sectors
|
||||
uint16_t TotalSectorsInLogicalVolume ; // 0 means >65535 sectors in volume , actual count can be found in LargeSectorCount
|
||||
uint8_t MediaDescriptor ; // Indication the media descriptor type
|
||||
uint16_t NumberOfSectorsPerFAT;// only in FAT12 / FAT 16
|
||||
uint16_t NumberOfSectorsPerTrack;
|
||||
uint16_t NumberOfHeadsOnMedia;
|
||||
uint32_t NumberOfHiddenSectors;
|
||||
uint32_t LargeSectorCount;
|
||||
ExtendedBootRecord_FAT16 ebpb;
|
||||
}__attribute__((packed));
|
19
source/kernel/filesystem/FAT/DirectoryEntry.h
Normal file
19
source/kernel/filesystem/FAT/DirectoryEntry.h
Normal file
@ -0,0 +1,19 @@
|
||||
#pragma once
|
||||
#include <stdint.h>
|
||||
|
||||
struct DirectoryEntry {
|
||||
uint8_t filename [8];
|
||||
uint8_t Extension [3];
|
||||
uint8_t attribute;
|
||||
uint8_t Reserved;
|
||||
uint8_t creation;
|
||||
uint16_t CreationTime;
|
||||
uint16_t CreationDate;
|
||||
uint16_t LastAccessDate;
|
||||
uint16_t ReservedFAT32;
|
||||
uint16_t LastWriteTime;
|
||||
uint16_t LastWriteDate;
|
||||
uint16_t StartingCluster;
|
||||
uint32_t FilesizeInBytes;
|
||||
|
||||
}__attribute__((packed));
|
32
source/kernel/filesystem/FAT/ExtendBootRecord.h
Normal file
32
source/kernel/filesystem/FAT/ExtendBootRecord.h
Normal file
@ -0,0 +1,32 @@
|
||||
#pragma once
|
||||
#include <stdint.h>
|
||||
|
||||
struct ExtendedBootRecord_FAT16{
|
||||
uint8_t DriveNumber;
|
||||
uint8_t Reserved;
|
||||
uint8_t Signature;
|
||||
const uint32_t VOLUME_ID_SERIAL_NUMBER;
|
||||
uint8_t volume_label [11];
|
||||
uint8_t Identifier_string [8];
|
||||
uint8_t bootCode [448];
|
||||
uint16_t partitionSignature;
|
||||
}__attribute__((packed));
|
||||
|
||||
struct ExtendedBootRecord_FAT32{
|
||||
uint32_t SectorsPerFAT;
|
||||
uint16_t Flags;
|
||||
const uint16_t FAT_VERSION_NUMBER;
|
||||
uint32_t rootDirectory_clusterNumber;// Often set to 2;
|
||||
uint16_t FSInfo_SectorNumber;
|
||||
uint16_t backup_bpb_sectorNumber;
|
||||
uint8_t Reserved [12];
|
||||
uint8_t DriveNumber;
|
||||
uint8_t Reserved2;
|
||||
uint8_t Signature; // must be 0x28 or 0x29
|
||||
uint32_t VOLUME_ID_SERIAL;
|
||||
uint8_t volume_label[11];
|
||||
uint8_t SystemIdentifierString [8]; // ALWAYS "FAT32 " but spec says do not trust
|
||||
uint8_t BootCode [420]; // NICE
|
||||
uint16_t PartitionSignature; // 0xAA55
|
||||
|
||||
}__attribute__((packed));
|
0
source/kernel/filesystem/FAT/FAT16.cpp
Normal file
0
source/kernel/filesystem/FAT/FAT16.cpp
Normal file
8
source/kernel/filesystem/FAT/FAT16.h
Normal file
8
source/kernel/filesystem/FAT/FAT16.h
Normal file
@ -0,0 +1,8 @@
|
||||
#pragma once
|
||||
#include "../../vfs/File.h"
|
||||
|
||||
|
||||
class FAT16 : File {
|
||||
public:
|
||||
|
||||
};
|
404
source/kernel/interrupts/idt.cpp
Normal file
404
source/kernel/interrupts/idt.cpp
Normal file
@ -0,0 +1,404 @@
|
||||
#include "idt.h"
|
||||
#include "../drivers/pit/pit.h"
|
||||
#include "../drivers/ps-2/keyboard.h"
|
||||
#include "../cpu.h"
|
||||
#include "../memory/VirtualMemoryManager.h"
|
||||
IDT_entry idt_table[256];
|
||||
IDT_ptr idt_ptr;
|
||||
|
||||
|
||||
void set_id_entry (uint8_t num , uint32_t base, uint16_t sel, uint8_t flags){
|
||||
idt_table[num].offset_1 = base & 0xFFFF;
|
||||
idt_table[num].selector = sel;
|
||||
idt_table[num].zero = 0;
|
||||
idt_table[num].type_attr = flags;
|
||||
idt_table[num].offset_2 = (base >> 16) & 0xFFFF;
|
||||
|
||||
};
|
||||
|
||||
void irs_handler (registers* regs) {
|
||||
uint32_t FaultingAddress;
|
||||
//printf("(IRS) Interrupt number: %d \r", regs.int_no);
|
||||
switch (regs->int_no)
|
||||
{
|
||||
case 0:
|
||||
// Divide Error #DE
|
||||
printf("#DE\n");
|
||||
printf("EIP: 0x%x\n", regs->eip);
|
||||
printf("EAX: 0x%x\n", regs->eax);
|
||||
printf("EBP: 0x%x\n", regs->ebp);
|
||||
break;
|
||||
|
||||
case 1:
|
||||
// Debug Exception #DB
|
||||
printf("#DB\n");
|
||||
printf("EIP: 0x%x\n", regs->eip);
|
||||
printf("EAX: 0x%x\n", regs->eax);
|
||||
printf("EBP: 0x%x\n", regs->ebp);
|
||||
break;
|
||||
|
||||
case 2:
|
||||
// NMI Interrupt
|
||||
printf("#NMI\n");
|
||||
break;
|
||||
|
||||
case 3:
|
||||
// Breakpoint Exception #BP
|
||||
printf("#BP\n");
|
||||
printf("EIP: 0x%x\n", regs->eip);
|
||||
printf("EAX: 0x%x\n", regs->eax);
|
||||
printf("EBP: 0x%x\n", regs->ebp);
|
||||
break;
|
||||
|
||||
case 4:
|
||||
// Overflow Exception #OF
|
||||
printf("#OF\n");
|
||||
printf("EIP: 0x%x\n", regs->eip);
|
||||
printf("EAX: 0x%x\n", regs->eax);
|
||||
printf("EBP: 0x%x\n", regs->ebp);
|
||||
break;
|
||||
|
||||
case 5:
|
||||
// BOUND Range Exceeded Exception #BR
|
||||
printf("#BR\n");
|
||||
printf("EIP: 0x%x\n", regs->eip);
|
||||
printf("EAX: 0x%x\n", regs->eax);
|
||||
printf("EBP: 0x%x\n", regs->ebp);
|
||||
break;
|
||||
|
||||
case 6:
|
||||
// Invalid OpCode Exception #UD
|
||||
printf("#UD\n");
|
||||
printf("EIP: 0x%x\n", regs->eip);
|
||||
printf("EAX: 0x%x\n", regs->eax);
|
||||
printf("EBP: 0x%x\n", regs->ebp);
|
||||
break;
|
||||
|
||||
case 7:
|
||||
// Device Not Available Exception #NM
|
||||
printf("#NM\n");
|
||||
printf("EIP: 0x%x\n", regs->eip);
|
||||
printf("EAX: 0x%x\n", regs->eax);
|
||||
printf("EBP: 0x%x\n", regs->ebp);
|
||||
break;
|
||||
|
||||
case 8:
|
||||
// Double Fault Exception #DF
|
||||
printf("#DF\n");
|
||||
printf("EIP: 0x%x\n", regs->eip);
|
||||
printf("EAX: 0x%x\n", regs->eax);
|
||||
printf("EBP: 0x%x\n", regs->ebp);
|
||||
while(true);
|
||||
break;
|
||||
|
||||
case 9:
|
||||
// Coprocessor Segment Overrun
|
||||
printf("Coprocessor Segment overrun!\n");
|
||||
break;
|
||||
|
||||
case 10:
|
||||
// Invalid TSS Exception #TS
|
||||
printf("#TS\n");
|
||||
printf("EIP: 0x%x\n", regs->eip);
|
||||
printf("EAX: 0x%x\n", regs->eax);
|
||||
printf("EBP: 0x%x\n", regs->ebp);
|
||||
__asm__("cli;" "1: hlt;" "jmp 1b;");
|
||||
break;
|
||||
|
||||
case 11:
|
||||
// Segment Not Present #NP
|
||||
printf("#NP\n");
|
||||
printf("EIP: 0x%x\n", regs->eip);
|
||||
printf("EAX: 0x%x\n", regs->eax);
|
||||
printf("EBP: 0x%x\n", regs->ebp);
|
||||
break;
|
||||
|
||||
case 12:
|
||||
// Stack Fault Exception #SS
|
||||
printf("#SS\n");
|
||||
printf("EIP: 0x%x\n", regs->eip);
|
||||
printf("EAX: 0x%x\n", regs->eax);
|
||||
printf("EBP: 0x%x\n", regs->ebp);
|
||||
break;
|
||||
|
||||
case 13:{
|
||||
// General Protection Exception #GP
|
||||
printf("#GP\n");
|
||||
|
||||
printf("Accessing memory caused a general protection exception.\n");
|
||||
printf("Faulting instruction at addres: 0x%x\n", regs->eip );
|
||||
printf("Error code: 0x%x\n", regs->err_code);
|
||||
|
||||
if (regs->err_code != 0){
|
||||
printf("Fault due to entry at index: 0x%x (%d)\n", (regs->err_code >> 3 & 0xFFF ) , regs->err_code);
|
||||
|
||||
|
||||
uint8_t table = regs->err_code >> 1 & 0x3 ;
|
||||
|
||||
if(table == 0 ){
|
||||
printf("* Index references GDT\n");
|
||||
}
|
||||
if(table == 1 ){
|
||||
printf("* Index references IDT\n");
|
||||
}
|
||||
|
||||
if(table == 2 ){
|
||||
printf("* Index references LDT\n");
|
||||
}
|
||||
|
||||
if(table == 3 ){
|
||||
printf("* Index references IDT\n");
|
||||
}
|
||||
|
||||
if( regs->err_code & 0x1)
|
||||
{
|
||||
printf("* Originated externally!\n");
|
||||
}
|
||||
}
|
||||
|
||||
__asm__("cli;" "1: hlt;" "jmp 1b;");
|
||||
|
||||
}
|
||||
break;
|
||||
|
||||
case 14:
|
||||
// Page Fault Exception #PF
|
||||
printf("#PF\n");
|
||||
#define ALIGN(addr, align) (((addr) & ~((align) - 1)) + (align))
|
||||
FaultingAddress = GetCR2();
|
||||
|
||||
printf("Accessing the linear address 0x%x resulted in a page fault!\n\n", FaultingAddress);
|
||||
|
||||
// Error code of 32 bits are on the stack
|
||||
// CR2 register contains the 32-bit linear virtual address that generated the exception
|
||||
// See Intel Software Developers manual Volume 3A Part 1 page 236 for more info
|
||||
#define PF_ERR_PRESENT_BIT 0x1
|
||||
#define PF_ERR_WRITE_BIT 0x2
|
||||
#define PF_ERR_USER_BIT 0x3
|
||||
#define PF_ERR_RESERVERD_WRITE_BIT 0x4
|
||||
#define PF_ERR_INSTRUCTION_FETCH_BIT 0x5
|
||||
#define PF_ERR_PROTECTION_KEY_BIT 0x6
|
||||
#define PF_ERR_SHADOW_STACK_BIT 0x7
|
||||
#define PF_ERR_SOFTWARE_GUARD_EXTENSION_BIT 0xE
|
||||
|
||||
printf("REASON: \n\n");
|
||||
|
||||
|
||||
if (regs->err_code & PF_ERR_PRESENT_BIT ){
|
||||
printf("* Page protection violation!\n");
|
||||
} else{
|
||||
printf("* Page not-present!\n");
|
||||
Immediate_Map(FaultingAddress, FaultingAddress - 0xC0000000);
|
||||
|
||||
}
|
||||
|
||||
if(regs->err_code & PF_ERR_WRITE_BIT){
|
||||
printf("* Write access violation!\n");
|
||||
} else{
|
||||
printf("* Read access violation!\n");
|
||||
}
|
||||
|
||||
if(regs->err_code & PF_ERR_USER_BIT){
|
||||
printf("* Violation from user-space (CPL=3)\n");
|
||||
}
|
||||
|
||||
if(regs->err_code & PF_ERR_INSTRUCTION_FETCH_BIT){
|
||||
printf("* Caused by an instruction fetch. \n");
|
||||
}
|
||||
|
||||
|
||||
__asm__("cli;" "1: hlt;" "jmp 1b;");
|
||||
|
||||
break;
|
||||
|
||||
case 16:
|
||||
// x87 FPU Floating-point Error #MF
|
||||
printf("#MF\n");
|
||||
printf("EIP: 0x%x\n", regs->eip);
|
||||
printf("EAX: 0x%x\n", regs->eax);
|
||||
printf("EBP: 0x%x\n", regs->ebp);
|
||||
break;
|
||||
|
||||
case 17:
|
||||
// Alignment Check Exception #AC
|
||||
printf("#AC\n");
|
||||
printf("EIP: 0x%x\n", regs->eip);
|
||||
printf("EAX: 0x%x\n", regs->eax);
|
||||
printf("EBP: 0x%x\n", regs->ebp);
|
||||
break;
|
||||
|
||||
case 18:
|
||||
// Machine-Check Exception #MC
|
||||
printf("#MC\n");
|
||||
printf("EIP: 0x%x\n", regs->eip);
|
||||
printf("EAX: 0x%x\n", regs->eax);
|
||||
printf("EBP: 0x%x\n", regs->ebp);
|
||||
break;
|
||||
|
||||
case 19:
|
||||
// SIMD Floating-point Exception #XM
|
||||
printf("#XM\n");
|
||||
printf("EIP: 0x%x\n", regs->eip);
|
||||
printf("EAX: 0x%x\n", regs->eax);
|
||||
printf("EBP: 0x%x\n", regs->ebp);
|
||||
break;
|
||||
|
||||
case 20:
|
||||
// Virtualization Exception #VE
|
||||
printf("#VE\n");
|
||||
printf("EIP: 0x%x\n", regs->eip);
|
||||
printf("EAX: 0x%x\n", regs->eax);
|
||||
printf("EBP: 0x%x\n", regs->ebp);
|
||||
break;
|
||||
|
||||
case 21:
|
||||
// Control Protection Exception #CP
|
||||
printf("#CP\n");
|
||||
printf("EIP: 0x%x\n", regs->eip);
|
||||
printf("EAX: 0x%x\n", regs->eax);
|
||||
printf("EBP: 0x%x\n", regs->ebp);
|
||||
break;
|
||||
|
||||
default:
|
||||
// PANIC!!!
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
void irq_handler (registers regs) {
|
||||
|
||||
|
||||
|
||||
switch (regs.int_no) {
|
||||
case 0:
|
||||
pit_tick++;
|
||||
break;
|
||||
case 1:
|
||||
// Keyboard interrupt !!
|
||||
|
||||
int scan;
|
||||
int i;/*register*/
|
||||
|
||||
// Read scancode
|
||||
scan = inb(0x60);
|
||||
|
||||
// Send ack message!
|
||||
i = inb(0x61);
|
||||
outb(0x61, i|0x80);
|
||||
outb(0x61, i);
|
||||
|
||||
// NOTE: check for special scan codes
|
||||
// e.g. modifiers etc..
|
||||
if( scan < 0x37){
|
||||
//printf("Read from IO: 0x%x\n", scan);
|
||||
keyPress.ScanCode = scan ;
|
||||
//printf( "[From Interrupt] Scancode: %x\n", keyPress.ScanCode);
|
||||
|
||||
}
|
||||
|
||||
|
||||
break;
|
||||
case 12:
|
||||
// PS2 Mouse interrupt
|
||||
printf("Mouse event triggered!");
|
||||
//int event = inb(0x60);
|
||||
break;
|
||||
|
||||
default:
|
||||
printf("Interrupt happened!");
|
||||
printf("Received INT: 0x%x\n", regs.int_no);
|
||||
break;
|
||||
}
|
||||
|
||||
outb(0x20, 0x20); // send end of interrupt to master
|
||||
|
||||
if ( regs.int_no > 8 && regs.int_no <= 15) {
|
||||
outb(0xA0, 0x20); // send end of interrupt to slave
|
||||
}
|
||||
|
||||
|
||||
if( regs.int_no == 13){
|
||||
printf(" Error code: %d \n", regs.err_code);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void initidt(){
|
||||
// Initialise the IDT pointer
|
||||
idt_ptr.length = sizeof(IDT_entry) * 255;
|
||||
idt_ptr.base = (uint32_t)&idt_table;
|
||||
|
||||
#ifdef __VERBOSE__
|
||||
printf("Init IDT\n");
|
||||
#endif
|
||||
|
||||
// TODO: Set everything to zero first
|
||||
|
||||
set_id_entry(0, (uint32_t) irs0 , 0x08, 0x8F);
|
||||
set_id_entry(1, (uint32_t) irs1 , 0x08, 0x8E);
|
||||
set_id_entry(2, (uint32_t) irs2 , 0x08, 0x8E);
|
||||
set_id_entry(3, (uint32_t) irs3 , 0x08, 0x8E);
|
||||
set_id_entry(4, (uint32_t) irs4 , 0x08, 0x8E);
|
||||
set_id_entry(5, (uint32_t) irs5 , 0x08, 0x8E);
|
||||
set_id_entry(6, (uint32_t) irs6 , 0x08, 0x8E);
|
||||
set_id_entry(7, (uint32_t) irs7 , 0x08, 0x8E);
|
||||
set_id_entry(8, (uint32_t) irs8 , 0x08, 0x8E);
|
||||
set_id_entry(9, (uint32_t) irs9 , 0x08, 0x8E);
|
||||
set_id_entry(10, (uint32_t) irs10 , 0x08, 0x8E);
|
||||
set_id_entry(11, (uint32_t) irs11 , 0x08, 0x8E);
|
||||
set_id_entry(12, (uint32_t) irs12 , 0x08, 0x8E);
|
||||
set_id_entry(13, (uint32_t) irs13 , 0x08, 0x8E);
|
||||
set_id_entry(14, (uint32_t) irs14 , 0x08, 0x8E);
|
||||
set_id_entry(15, (uint32_t) irs15 , 0x08, 0x8E);
|
||||
set_id_entry(16, (uint32_t) irs16 , 0x08, 0x8E);
|
||||
set_id_entry(17, (uint32_t) irs17 , 0x08, 0x8E);
|
||||
set_id_entry(18, (uint32_t) irs18 , 0x08, 0x8E);
|
||||
set_id_entry(19, (uint32_t) irs19 , 0x08, 0x8E);
|
||||
set_id_entry(20, (uint32_t) irs20 , 0x08, 0x8E);
|
||||
set_id_entry(21, (uint32_t) irs21 , 0x08, 0x8E);
|
||||
set_id_entry(22, (uint32_t) irs22 , 0x08, 0x8E);
|
||||
set_id_entry(23, (uint32_t) irs23 , 0x08, 0x8E);
|
||||
set_id_entry(24, (uint32_t) irs24 , 0x08, 0x8E);
|
||||
set_id_entry(25, (uint32_t) irs25 , 0x08, 0x8E);
|
||||
set_id_entry(26, (uint32_t) irs26 , 0x08, 0x8E);
|
||||
set_id_entry(27, (uint32_t) irs27 , 0x08, 0x8E);
|
||||
set_id_entry(28, (uint32_t) irs28 , 0x08, 0x8E);
|
||||
set_id_entry(29, (uint32_t) irs29 , 0x08, 0x8E);
|
||||
set_id_entry(30, (uint32_t) irs30 , 0x08, 0x8E);
|
||||
set_id_entry(31, (uint32_t) irs31 , 0x08, 0x8E);
|
||||
|
||||
|
||||
//print_serial("Remapping PIC\n");
|
||||
PIC_remap(0x20, 0x28);
|
||||
|
||||
// clear mask for IRQ 12
|
||||
uint8_t value = inb(0x21) & ~(1<< 12);
|
||||
outb(0x21, value);
|
||||
|
||||
|
||||
|
||||
// pic IRQ Table
|
||||
set_id_entry(32, (uint32_t)irq0, 0x08, 0x8E);
|
||||
set_id_entry(33, (uint32_t)irq1, 0x08, 0x8E); // PS2 Keyboard
|
||||
set_id_entry(34, (uint32_t)irq2, 0x08, 0x8E);
|
||||
set_id_entry(35, (uint32_t)irq3, 0x08, 0x8E);
|
||||
set_id_entry(36, (uint32_t)irq4, 0x08, 0x8E);
|
||||
set_id_entry(37, (uint32_t)irq5, 0x08, 0x8E);
|
||||
set_id_entry(38, (uint32_t)irq6, 0x08, 0x8E);
|
||||
set_id_entry(39, (uint32_t)irq7, 0x08, 0x8E);
|
||||
set_id_entry(40, (uint32_t)irq8, 0x08, 0x8E);
|
||||
set_id_entry(41, (uint32_t)irq9, 0x08, 0x8E);
|
||||
set_id_entry(42, (uint32_t)irq10, 0x08, 0x8E);
|
||||
set_id_entry(43, (uint32_t)irq11, 0x08, 0x8E);
|
||||
set_id_entry(44, (uint32_t)irq12, 0x08, 0x8E); // PS2 Mouse
|
||||
set_id_entry(45, (uint32_t)irq13, 0x08, 0x8E);
|
||||
set_id_entry(46, (uint32_t)irq14, 0x08, 0x8E);
|
||||
set_id_entry(47, (uint32_t)irq15, 0x08, 0x8E);
|
||||
|
||||
|
||||
idt_flush((uint32_t)&idt_ptr);
|
||||
}
|
@ -1,11 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
#include "stdint.h"
|
||||
#include "stddef.h"
|
||||
#include "../vga/colors.h"
|
||||
#include "../pic/pic.h"
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include "../drivers/vga/colors.h"
|
||||
#include "../drivers/pic/pic.h"
|
||||
|
||||
#include "../tty/kterm.h"
|
||||
#include "../terminal/kterm.h"
|
||||
|
||||
|
||||
extern "C" {
|
||||
@ -32,11 +32,11 @@ extern "C" {
|
||||
|
||||
extern void idt_flush(uint32_t);
|
||||
void set_id_entry (uint8_t num , uint32_t base, uint16_t sel, uint8_t flags);
|
||||
void init_idt();
|
||||
void initidt();
|
||||
|
||||
void irq_handler (registers regs);
|
||||
|
||||
void irs_handler (registers regs);
|
||||
void irs_handler (registers* regs);
|
||||
|
||||
extern void irs0 ();
|
||||
extern void irs1 ();
|
87
source/kernel/irs_table.s
Normal file
87
source/kernel/irs_table.s
Normal file
@ -0,0 +1,87 @@
|
||||
.code32
|
||||
/*
|
||||
* Interupt handlers
|
||||
*/
|
||||
|
||||
.macro ISR_NOERRORCODE NAME, VECTOR
|
||||
.globl irs\NAME
|
||||
irs\NAME:
|
||||
cli
|
||||
push $0
|
||||
push \VECTOR
|
||||
jmp irs_common
|
||||
.endm
|
||||
|
||||
.macro ISR_ERROCODE NAME, VECTOR
|
||||
.globl irs\NAME
|
||||
irs\NAME:
|
||||
cli
|
||||
push \VECTOR
|
||||
jmp irs_common
|
||||
.endm
|
||||
|
||||
|
||||
ISR_NOERRORCODE 0 $0
|
||||
ISR_NOERRORCODE 1 $1
|
||||
ISR_NOERRORCODE 2 $2
|
||||
ISR_NOERRORCODE 3 $3
|
||||
ISR_NOERRORCODE 4 $4
|
||||
ISR_NOERRORCODE 5 $5
|
||||
ISR_NOERRORCODE 6 $6
|
||||
ISR_NOERRORCODE 7 $7
|
||||
ISR_NOERRORCODE 8 $8
|
||||
ISR_NOERRORCODE 9 $9
|
||||
ISR_NOERRORCODE 10 $10
|
||||
ISR_NOERRORCODE 11 $11
|
||||
ISR_NOERRORCODE 12 $12
|
||||
ISR_NOERRORCODE 13 $13
|
||||
ISR_NOERRORCODE 14 $14
|
||||
ISR_NOERRORCODE 15 $15
|
||||
ISR_NOERRORCODE 16 $16
|
||||
ISR_NOERRORCODE 17 $17
|
||||
ISR_NOERRORCODE 18 $18
|
||||
ISR_NOERRORCODE 19 $19
|
||||
ISR_NOERRORCODE 20 $20
|
||||
ISR_NOERRORCODE 21 $21
|
||||
ISR_NOERRORCODE 22 $22
|
||||
ISR_NOERRORCODE 23 $23
|
||||
ISR_NOERRORCODE 24 $24
|
||||
ISR_NOERRORCODE 25 $25
|
||||
ISR_NOERRORCODE 26 $26
|
||||
ISR_NOERRORCODE 27 $27
|
||||
ISR_NOERRORCODE 28 $28
|
||||
ISR_NOERRORCODE 29 $29
|
||||
ISR_NOERRORCODE 30 $30
|
||||
ISR_NOERRORCODE 31 $31
|
||||
|
||||
|
||||
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
|
||||
|
||||
mov %esp, %eax
|
||||
push %eax
|
||||
|
||||
call irs_handler
|
||||
|
||||
pop %eax
|
||||
pop %ebx // reload ther orignal data segment descriptor
|
||||
mov %bx, %ds
|
||||
mov %bx, %es
|
||||
mov %bx, %fs
|
||||
mov %bx, %gs
|
||||
|
||||
popa
|
||||
add $12, %esp # cleans push error and irs code
|
||||
|
||||
iret # pops 5 things at once: CS, EIP, EFLAGS, SS, and ESP
|
97
source/kernel/kernel.cpp
Normal file
97
source/kernel/kernel.cpp
Normal file
@ -0,0 +1,97 @@
|
||||
|
||||
extern "C"
|
||||
{
|
||||
#include "../lib/include/string.h"
|
||||
}
|
||||
|
||||
#include "prekernel/bootstructure.h"
|
||||
|
||||
#include "memory/memory.h"
|
||||
#include "memory/memoryinfo.h"
|
||||
#include "memory/memory.h"
|
||||
#include "memory/VirtualMemoryManager.h"
|
||||
#include "memory/KernelHeap.h"
|
||||
#include "memory/gdt/gdtc.h"
|
||||
#include "memory/TaskStateSegment.h"
|
||||
|
||||
#include "supervisorterminal/superVisorTerminal.h"
|
||||
|
||||
#include "drivers/io/io.h"
|
||||
#include "drivers/vga/VBE.h"
|
||||
#include "drivers/pci/pci.h"
|
||||
#include "drivers/pit/pit.h"
|
||||
#include "drivers/acpi/acpi.h"
|
||||
#include "drivers/ide/ide.h"
|
||||
|
||||
#include "terminal/kterm.h"
|
||||
|
||||
#include "prekernel/multiboot.h"
|
||||
#include "bootinfo.h"
|
||||
|
||||
#include "bootcheck.h"
|
||||
|
||||
#include "interrupts/idt.h"
|
||||
#include "time.h"
|
||||
#include "cpu.h"
|
||||
#include "serial.h"
|
||||
#include "time.h"
|
||||
#include "definitions.h"
|
||||
extern "C" void LoadGlobalDescriptorTable();
|
||||
|
||||
|
||||
/*
|
||||
Copyright © Nigel Barink 2023
|
||||
*/
|
||||
|
||||
extern "C" void kernel_main ()
|
||||
{
|
||||
/*
|
||||
* Show a little banner for cuteness
|
||||
*/
|
||||
printf("|=== BarinkOS ===|\n");
|
||||
startSuperVisorTerminal();
|
||||
}
|
||||
|
||||
extern "C" void early_main()
|
||||
{
|
||||
init_serial();
|
||||
kterm_init();
|
||||
|
||||
|
||||
setup_tss();
|
||||
initGDT();
|
||||
initidt();
|
||||
LoadGlobalDescriptorTable();
|
||||
flush_tss();
|
||||
printf("Memory setup complete!\n");
|
||||
|
||||
// Enable interrupts
|
||||
asm volatile("STI");
|
||||
|
||||
ACPI::initialize();
|
||||
PCI_Enumerate();
|
||||
|
||||
TestIDEController();
|
||||
|
||||
initHeap();
|
||||
|
||||
printf("Enable Protected mode and jump to kernel main\n");
|
||||
|
||||
|
||||
// Set the protected bit of control register 0
|
||||
// this will put the CPU into protected mode
|
||||
// NOTE: This should really be a assembly procedure
|
||||
// We cant directly write to control register 0
|
||||
// therefor we copy the value of control register 0 into eax
|
||||
// once we are done manipulating the value we write the value in
|
||||
// eax back to control register 0
|
||||
|
||||
asm volatile("mov %cr0, %eax ");
|
||||
asm volatile("or $1, %eax");
|
||||
asm volatile("mov %eax, %cr0");
|
||||
|
||||
pit_initialise();
|
||||
|
||||
kernel_main();
|
||||
|
||||
}
|
8
source/kernel/kernel.h
Normal file
8
source/kernel/kernel.h
Normal file
@ -0,0 +1,8 @@
|
||||
#pragma once
|
||||
|
||||
#define CHECK_FLAG(flags, bit) ((flags) & (1 <<(bit)))
|
||||
#define PANIC(message) {return;}
|
||||
|
||||
|
||||
|
||||
|
42
source/kernel/linker.ld
Normal file
42
source/kernel/linker.ld
Normal file
@ -0,0 +1,42 @@
|
||||
ENTRY(_start)
|
||||
|
||||
/* Tell where the various sections of the object files will be put in the final
|
||||
kernel image. */
|
||||
SECTIONS
|
||||
{
|
||||
. = 0x00100000; /* place code at 1MB mark*/
|
||||
|
||||
_kernel_start = .;
|
||||
kernel_begin = .; /* For legacy reasons */
|
||||
|
||||
.multiboot.data : {
|
||||
*(.multiboot.data)
|
||||
}
|
||||
|
||||
.multiboot.text : {
|
||||
*(multiboot.text)
|
||||
}
|
||||
|
||||
. += 0xC0000000; /* Addresses in the following code need to be above the 3Gb mark */
|
||||
.text ALIGN (4K) : AT (ADDR (.text) - 0xC0000000)
|
||||
{
|
||||
*(.text)
|
||||
}
|
||||
.rodata ALIGN (4K) : AT (ADDR (.rodata) - 0xC0000000)
|
||||
{
|
||||
*(.rodata)
|
||||
}
|
||||
.data ALIGN (4K) : AT (ADDR (.data) - 0xC0000000)
|
||||
{
|
||||
*(.data)
|
||||
}
|
||||
.bss ALIGN (4K) : AT (ADDR (.bss) - 0xC0000000)
|
||||
{
|
||||
*(COMMON)
|
||||
*(.bss)
|
||||
*(.bootstrap_stack)
|
||||
}
|
||||
_kernel_end = .;
|
||||
kernel_end = .; /* For legacy reasons */
|
||||
}
|
||||
|
86
source/kernel/memory/KernelHeap.cpp
Normal file
86
source/kernel/memory/KernelHeap.cpp
Normal file
@ -0,0 +1,86 @@
|
||||
#include "KernelHeap.h"
|
||||
#include "VirtualMemoryManager.h"
|
||||
extern "C" const uint32_t kernel_end;
|
||||
// Size of heap meta data is 5 bytes
|
||||
struct heap_block{
|
||||
uint8_t Used;
|
||||
uint32_t Size;
|
||||
};
|
||||
|
||||
uint32_t heap_size;
|
||||
heap_block* start ;
|
||||
|
||||
void* malloc(size_t size)
|
||||
{
|
||||
printf("Received request for %d bytes of memory\n", size);
|
||||
heap_block* current = start;
|
||||
|
||||
// look for a free block
|
||||
while(current < start + heap_size)
|
||||
{
|
||||
if(current->Size >= size && current->Used == false )
|
||||
{
|
||||
// We found a spot
|
||||
printf("Block found!\n");
|
||||
|
||||
// Set the spot to in-use
|
||||
current->Used = true;
|
||||
|
||||
|
||||
// split the block
|
||||
printf("Split block.\n");
|
||||
uint32_t oldSize = current->Size;
|
||||
current->Size = size;
|
||||
|
||||
heap_block* new_block = current + sizeof(heap_block) + current->Size;
|
||||
new_block->Size = oldSize - ( sizeof(heap_block) + size);
|
||||
new_block->Used = false;
|
||||
|
||||
// return the free address
|
||||
// NOTE: added an offset from the initial address to accomodate for
|
||||
// meta-data.
|
||||
return current + sizeof(heap_block);
|
||||
|
||||
}
|
||||
|
||||
current += current->Size + sizeof(heap_block);
|
||||
}
|
||||
|
||||
// If we are here we need more memory so we should
|
||||
// probably ask the VMM for more
|
||||
// TODO: ask for more memory | Extend kernel heap
|
||||
|
||||
printf("ERROR: OUT OF HEAP MEMORY CONDITION IS NOT IMPLEMENTED. HEAP NEEDS TO BE EXTENDED!\n");
|
||||
}
|
||||
|
||||
void free(void* addr)
|
||||
{
|
||||
// clear the free boolean that corresponds to this adddress
|
||||
// This should be fairly simple
|
||||
heap_block* allocatedBlock = (heap_block*)(addr - sizeof(heap_block));
|
||||
allocatedBlock->Used = false;
|
||||
}
|
||||
|
||||
void initHeap()
|
||||
{
|
||||
void* HEAP_ADDRESS = allocate_block();
|
||||
printf("0x%x HEAP Paddr\n", HEAP_ADDRESS);
|
||||
|
||||
Immediate_Map((uint32_t)HEAP_ADDRESS + 0xC0000000, (uint32_t)HEAP_ADDRESS );
|
||||
start = (heap_block*) ((uint32_t)HEAP_ADDRESS + 0xC0000000);
|
||||
heap_size = 4096;
|
||||
|
||||
printf("Clear heap\n");
|
||||
// Clear the heap
|
||||
printf("set at 0x%x %d bytes to zero\n", start , heap_size);
|
||||
|
||||
memset((void*)start, 0x00, heap_size /4);
|
||||
|
||||
|
||||
printf("Init first heap block\n");
|
||||
// initialzie
|
||||
start->Size = heap_size - sizeof(heap_block);
|
||||
|
||||
start->Used = false;
|
||||
|
||||
}
|
10
source/kernel/memory/KernelHeap.h
Normal file
10
source/kernel/memory/KernelHeap.h
Normal file
@ -0,0 +1,10 @@
|
||||
#pragma once
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include "../terminal/kterm.h"
|
||||
|
||||
void initHeap();
|
||||
|
||||
void* malloc (size_t size );
|
||||
void free(void* addr);
|
||||
|
43
source/kernel/memory/PageDirectory.cpp
Normal file
43
source/kernel/memory/PageDirectory.cpp
Normal file
@ -0,0 +1,43 @@
|
||||
#include "PageDirectory.h"
|
||||
|
||||
void PageDirectory::enable()
|
||||
{
|
||||
|
||||
|
||||
// https://wiki.osdev.org/Setting_Up_Paging
|
||||
//set each entry to not present
|
||||
// int i;
|
||||
// for(i = 0; i < 1024; i++)
|
||||
// {
|
||||
// // This sets the following flags to the pages:
|
||||
// // Supervisor: Only kernel-mode can access them
|
||||
// // Write Enabled: It can be both read from and written to
|
||||
// // Not Present: The page table is not present
|
||||
// this->page_directory[i] = 0x00000002;
|
||||
// }
|
||||
|
||||
// // holds the physical address where we want to start mapping these pages to.
|
||||
// // in this case, we want to map these pages to the very beginning of memory.
|
||||
|
||||
// //we will fill all 1024 entries in the table, mapping 4 megabytes
|
||||
// for(unsigned int i = 0; i < 1024; i++)
|
||||
// {
|
||||
// // As the address is page aligned, it will always leave 12 bits zeroed.
|
||||
// // Those bits are used by the attributes ;)
|
||||
// first_page_table[i] = (i * 0x1000) | 3; // attributes: supervisor level, read/write, present.
|
||||
// }
|
||||
|
||||
// // attributes: supervisor level, read/write, present
|
||||
// this->page_directory[0] = ((unsigned int)first_page_table) | 3;
|
||||
|
||||
printf("Enable Paging!\n");
|
||||
|
||||
loadPageDirectory(this->page_directory);
|
||||
enablePaging();
|
||||
}
|
||||
|
||||
|
||||
void PageDirectory::MapPhysicalToVirtualAddress ( address_t PAddress , address_t VAddress, uint32_t size )
|
||||
{
|
||||
|
||||
}
|
@ -1,17 +1,18 @@
|
||||
#pragma once
|
||||
#include <stdint.h>
|
||||
|
||||
extern "C" void loadPageDirectory (uint32_t* addr );
|
||||
extern "C" void enablePaging();
|
||||
typedef uintptr_t address_t;
|
||||
|
||||
|
||||
#include "./memory.h"
|
||||
#include "./../terminal/kterm.h"
|
||||
#define KB 1024
|
||||
|
||||
|
||||
typedef uintptr_t address_t;
|
||||
|
||||
static const int MAX_PAGES = 1024 * KB; // 4GB , 4kB/page
|
||||
static volatile address_t pmem_stack[MAX_PAGES];
|
||||
static volatile address_t pmem_stack_top = MAX_PAGES; // top down allocation
|
||||
|
||||
extern "C" void loadPageDirectory (uint32_t* addr );
|
||||
extern "C" void enablePaging();
|
||||
|
||||
struct page_directory_entry {};
|
||||
struct page_table_entry{};
|
||||
@ -21,8 +22,10 @@ struct page_table_entry{};
|
||||
class PageDirectory {
|
||||
public:
|
||||
void enable ();
|
||||
|
||||
void MapPhysicalToVirtualAddress ( address_t PAddress , address_t VAddress, uint32_t size );
|
||||
|
||||
private:
|
||||
uint32_t page_directory[1024] __attribute__((aligned(4096)));
|
||||
uint32_t first_page_table[1024] __attribute__((aligned(4096)));
|
||||
uint32_t page_directory[1024] __attribute__((aligned(4096))); // align on 4 kiloByte pages
|
||||
uint32_t first_page_table[1024] __attribute__((aligned(4096))); // align on 4 kiloByte pages
|
||||
|
||||
};
|
115
source/kernel/memory/PhysicalMemoryManager.cpp
Normal file
115
source/kernel/memory/PhysicalMemoryManager.cpp
Normal file
@ -0,0 +1,115 @@
|
||||
#include "./PhysicalMemoryManager.h"
|
||||
#define IS_ALIGNED(addr, align) !((addr) & ~((align) - 1))
|
||||
#define ALIGN(addr, align) (((addr) & ~((align) - 1 )) + (align))
|
||||
|
||||
const uint32_t KERNEL_OFFSET = 0xC0000000;
|
||||
|
||||
uint32_t* memoryBitMap;
|
||||
uint32_t pmmap_size;
|
||||
uint32_t max_blocks;
|
||||
int used_blocks;
|
||||
|
||||
|
||||
void SetupPhysicalMemoryManager(uint32_t mapAddress, uint32_t memorySize )
|
||||
{
|
||||
/*
|
||||
Every byte contains 8 pages
|
||||
A page is 4096 kib
|
||||
Every block (1 bit) represent an page
|
||||
*/
|
||||
|
||||
// Set the maximum number of blocks
|
||||
max_blocks = (uint32_t)memorySize / BLOCK_SIZE ;
|
||||
printf("Max Blocks: %d\n", max_blocks);
|
||||
|
||||
// Set size of the bitmap
|
||||
uint32_t bitmap_size = max_blocks / 32;
|
||||
printf("Bitmap size: %d bytes\n",bitmap_size);
|
||||
|
||||
// Set blocks used to zero
|
||||
used_blocks = max_blocks;
|
||||
|
||||
// set the address of the memory bitmap
|
||||
memoryBitMap = (uint32_t*) mapAddress;
|
||||
|
||||
// Set all places in memory as free
|
||||
memset(memoryBitMap, 0xFFFFFFFF, max_blocks / 32 );
|
||||
}
|
||||
|
||||
// NOTE: This can only give blocks of 4kb at a time!
|
||||
// We might at some point want to allocate multiple blocks at once.
|
||||
void* allocate_block() {
|
||||
uint8_t blocks_available = max_blocks - used_blocks;
|
||||
// Are there any blocks available?
|
||||
if( blocks_available <= 0)
|
||||
{
|
||||
printf("No blocks available. Blocks Delta: 0x%x\n", blocks_available);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Find 1 free block somewhere
|
||||
int free_block_index = bitmap_first_unset(memoryBitMap, max_blocks / 8 );
|
||||
|
||||
if(free_block_index == -1)
|
||||
{
|
||||
printf("Could not find a good block!\n");
|
||||
// Could not find a block
|
||||
return (void*)0xFFFF;
|
||||
}
|
||||
|
||||
if(free_block_index == 0)
|
||||
printf("Somethings wrong!!!\n");
|
||||
|
||||
// Set the block to be used!
|
||||
bitmap_unset(memoryBitMap, free_block_index);
|
||||
// Increase the used_block count!
|
||||
used_blocks++;
|
||||
printf("used blocks: 0x%x\n", used_blocks);
|
||||
// return the pointer to the physical address
|
||||
return (void*) (BLOCK_SIZE * free_block_index);
|
||||
}
|
||||
|
||||
void free_block(void* p) {
|
||||
// If it is a null pointer we don't need to do anything.
|
||||
if(p==0) {
|
||||
return;
|
||||
}
|
||||
// calculate the index into the bitmap
|
||||
int index = ((uint32_t) p) / BLOCK_SIZE;
|
||||
|
||||
// set the block to be free
|
||||
bitmap_set(memoryBitMap, index);
|
||||
used_blocks--;
|
||||
printf("used blocks: 0x%x, after free\n", used_blocks);
|
||||
|
||||
}
|
||||
|
||||
void allocate_region(uint32_t startAddress, uint32_t size) {
|
||||
// every bit should be 4KiB
|
||||
// every byte is 8*4KiB = 32KiB
|
||||
|
||||
int NumberOfBlocksToAllocate = ( size / 1024) / 4 / 8 + 1;
|
||||
int startBlock = (startAddress / 1024) / 4 / 8 ;
|
||||
|
||||
for( int i = 0; i < NumberOfBlocksToAllocate; i++)
|
||||
{
|
||||
bitmap_unset(memoryBitMap, startBlock + i);// allocate region causes #PF Exception
|
||||
used_blocks++;
|
||||
}
|
||||
}
|
||||
|
||||
void deallocate_region(uint32_t StartAddress , uint32_t size ) {
|
||||
// reverse of what happened in allocate_region
|
||||
int NumberOfBlocks = (size / 1024) / 4 / 8 + 1;
|
||||
int startBlock = (StartAddress / 1024) / 4 / 8;
|
||||
|
||||
for(int i = 0; i < NumberOfBlocks; i++)
|
||||
{
|
||||
bitmap_set(memoryBitMap, startBlock + i);
|
||||
used_blocks --;
|
||||
}
|
||||
}
|
||||
|
||||
int GetUsedBlocks (){
|
||||
return used_blocks;
|
||||
}
|
19
source/kernel/memory/PhysicalMemoryManager.h
Normal file
19
source/kernel/memory/PhysicalMemoryManager.h
Normal file
@ -0,0 +1,19 @@
|
||||
#pragma once
|
||||
#include <stddef.h>
|
||||
#include "../prekernel/bootstructure.h"
|
||||
#include "../terminal/kterm.h"
|
||||
#include "../../lib/include/mem.h"
|
||||
#include "../bitmap.h"
|
||||
|
||||
#define BLOCK_SIZE 4092
|
||||
|
||||
|
||||
void SetupPhysicalMemoryManager(uint32_t mapAddress, uint32_t memorySize);
|
||||
|
||||
void* allocate_block();
|
||||
void free_block(void* ptr);
|
||||
|
||||
void allocate_region(uint32_t address, uint32_t size);
|
||||
void deallocate_region(uint32_t address, uint32_t size);
|
||||
|
||||
int GetUsedBlocks();
|
60
source/kernel/memory/TaskStateSegment.h
Normal file
60
source/kernel/memory/TaskStateSegment.h
Normal file
@ -0,0 +1,60 @@
|
||||
#pragma once
|
||||
#include "gdt/gdtc.h"
|
||||
#include "../../lib/include/string.h"
|
||||
|
||||
struct TaskStateSegment {
|
||||
uint32_t prev_tss;
|
||||
uint32_t esp0;
|
||||
uint32_t ss0;
|
||||
// everythinge else is unused
|
||||
uint32_t esp1;
|
||||
uint32_t ss1;
|
||||
uint32_t esp2;
|
||||
uint32_t ss2;
|
||||
uint32_t cr3;
|
||||
uint32_t eip;
|
||||
uint32_t eflags;
|
||||
uint32_t eax;
|
||||
uint32_t ecx;
|
||||
uint32_t edx;
|
||||
uint32_t ebx;
|
||||
uint32_t esp;
|
||||
uint32_t ebp;
|
||||
uint32_t esi;
|
||||
uint32_t edi;
|
||||
uint32_t es;
|
||||
uint32_t cs;
|
||||
uint32_t ss;
|
||||
uint32_t ds;
|
||||
uint32_t fs;
|
||||
uint32_t gs;
|
||||
uint32_t ldt;
|
||||
uint16_t trap;
|
||||
uint16_t iomap_base;
|
||||
}__attribute__((packed));
|
||||
|
||||
|
||||
TaskStateSegment tss0 ={};
|
||||
|
||||
inline void flush_tss()
|
||||
{
|
||||
asm volatile("mov $0x2B, %ax ; ltr %ax");
|
||||
}
|
||||
|
||||
void setup_tss(){
|
||||
// ensure the tss is zero'd
|
||||
memset((void*)&tss0, 0, sizeof(tss0));
|
||||
tss0.ss0 = (uint32_t) &GlobalDescriptorTable[KERNEL_DATA_SEGMENT];
|
||||
|
||||
extern uint32_t stack_top;
|
||||
tss0.esp0 = (unsigned long)&stack_top;
|
||||
|
||||
// Task Segment Descriptor
|
||||
|
||||
uint32_t address = (unsigned long) &tss0;
|
||||
uint32_t size = sizeof(tss0);
|
||||
uint32_t limit = (address + size );
|
||||
|
||||
add_descriptor(TASK_STATE_SEGMENT, address, limit- 1, 0xE9, 0x0);
|
||||
}
|
||||
|
126
source/kernel/memory/VirtualMemoryManager.cpp
Normal file
126
source/kernel/memory/VirtualMemoryManager.cpp
Normal file
@ -0,0 +1,126 @@
|
||||
#include "VirtualMemoryManager.h"
|
||||
#define ALIGN(addr, align) (((addr) & ~((align) - 1 )) + (align))
|
||||
|
||||
extern uint32_t boot_page_directory[1024] ; // points to the wrong location
|
||||
|
||||
extern uint32_t boot_page_table[1024];
|
||||
|
||||
|
||||
void flush_cr3(){
|
||||
asm volatile("movl %cr3, %ecx;"
|
||||
"movl %ecx, %cr3");
|
||||
}
|
||||
|
||||
|
||||
void AllocatePage(uint32_t vaddr)
|
||||
{
|
||||
uint32_t page_aligned_address = ALIGN(vaddr, 4096);
|
||||
|
||||
// allocate a page at virtual address
|
||||
int PageDirectoryEntryIndex = vaddr >> 22;
|
||||
int PageTableEntryIndex = (vaddr >> 12) & 0x1FFF;
|
||||
|
||||
printf("Allocation happening at PDE: %d PTE: %d\n", PageDirectoryEntryIndex, PageTableEntryIndex);
|
||||
|
||||
// check if the page directory entry is marked as present
|
||||
if (boot_page_directory[PageDirectoryEntryIndex] & 0x1 )
|
||||
{
|
||||
printf("Directory entry is marked as present\n");
|
||||
uint32_t* page_table = (uint32_t*)((boot_page_directory[PageDirectoryEntryIndex]) & 0xFFFFE000) ;
|
||||
//page_table = (uint32_t*) ((uint32_t)page_table + 0xC0000000); // Add kernel offset
|
||||
printf("Page table address: 0x%x\n", (uint32_t)&page_table);
|
||||
|
||||
// check if the page table entry is marked as present
|
||||
if ( page_table[PageTableEntryIndex] & 0x1 )
|
||||
{
|
||||
printf("page already present!\n");
|
||||
} else{
|
||||
printf("Mapping a physical page.\n");
|
||||
// Map the entry to a physical page
|
||||
page_table[PageTableEntryIndex] = (uint32_t)allocate_block() | 0x3;
|
||||
}
|
||||
|
||||
} else {
|
||||
printf("Mapping a new page directory entry with a page table\n");
|
||||
// mark the page table as present and allocate a physical block for it
|
||||
boot_page_directory[PageDirectoryEntryIndex] = (uint32_t)allocate_block() | 0x3;
|
||||
|
||||
}
|
||||
|
||||
asm ("cli; invlpg (%0); sti" :: "r" (vaddr) : "memory" );
|
||||
|
||||
|
||||
}
|
||||
|
||||
void FreePage(uint32_t vaddr )
|
||||
{
|
||||
uint32_t page_aligned_address = ALIGN(vaddr, 4096);
|
||||
|
||||
// allocate a page at virtual address
|
||||
int PageDirectoryEntryIndex = vaddr >> 22;
|
||||
int PageTableEntryIndex = (vaddr >> 12) & 0x1FFF;
|
||||
|
||||
uint32_t* pageTable = (uint32_t*)(boot_page_directory[PageDirectoryEntryIndex] & 0xFFFFE000 + 0xC0000000);
|
||||
|
||||
|
||||
void* physicalAddressToFree = (void*)(pageTable[PageTableEntryIndex] & 0xFFFFE000 + 0xC0000000);
|
||||
free_block(physicalAddressToFree);
|
||||
|
||||
pageTable[PageTableEntryIndex] = 0x0;
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
void Immediate_Map ( uint32_t vaddr, uint32_t paddr)
|
||||
{
|
||||
printf("map 0x%x to 0x%x\n", paddr, vaddr);
|
||||
// allocate a page at virtual address
|
||||
int PageDirectoryEntryIndex = vaddr >> 22;
|
||||
int PageTableEntryIndex = (vaddr >> 12) & 0x1FFF;
|
||||
|
||||
printf("Map address at PDE 0x%x PTE 0x%x\n", PageDirectoryEntryIndex, PageTableEntryIndex);
|
||||
|
||||
if ((boot_page_directory - 0xC0000000)[PageDirectoryEntryIndex] & 0x1 )
|
||||
{
|
||||
printf("Directory entry is marked as present\n");
|
||||
|
||||
} else {
|
||||
printf("Mapping a new page directory entry with a page table\n");
|
||||
// mark the page table as present and allocate a physical block for it
|
||||
|
||||
void* new_page_dir = allocate_block();
|
||||
printf("New page directory address 0x%x\n", new_page_dir);
|
||||
boot_page_directory[PageDirectoryEntryIndex] = (uint32_t)new_page_dir | 0x3;
|
||||
|
||||
}
|
||||
|
||||
printf("PDE found at : 0x%x\n", (uint32_t) &boot_page_directory[PageDirectoryEntryIndex]);
|
||||
uint32_t* page_table = (uint32_t*)(boot_page_directory[PageDirectoryEntryIndex] & 0xFFFFE000) ;
|
||||
//page_table = (uint32_t*) ((uint32_t)page_table - 0xC0000000); // remove kernel offset
|
||||
printf("Page table address: 0x%x\n", (uint32_t)page_table);
|
||||
|
||||
// check if the page table entry is marked as present
|
||||
if ( page_table[PageTableEntryIndex] & 0x1 )
|
||||
{
|
||||
printf("page already present!\n");
|
||||
printf("Entry found at addr: 0x%x\n", &(page_table[PageTableEntryIndex]));
|
||||
} else{
|
||||
printf("Mapping a physical page.\n");
|
||||
// Map the entry to a physical page
|
||||
page_table[PageTableEntryIndex] = (uint32_t)(paddr | 0x3);
|
||||
}
|
||||
|
||||
|
||||
asm ("cli; invlpg (%0); sti" :: "r" (vaddr) : "memory" );
|
||||
}
|
||||
|
||||
void Immediate_Unmap(uint32_t vaddr)
|
||||
{
|
||||
// NOTE: I will implement lazy unmapping for now
|
||||
uint32_t page_aligned_address = ALIGN(vaddr, 4096);
|
||||
|
||||
// allocate a page at virtual address
|
||||
int PageDirectoryEntryIndex = vaddr >> 22;
|
||||
int PageTableEntryIndex = (vaddr >> 12) & 0x1FFF;
|
||||
}
|
16
source/kernel/memory/VirtualMemoryManager.h
Normal file
16
source/kernel/memory/VirtualMemoryManager.h
Normal file
@ -0,0 +1,16 @@
|
||||
#pragma once
|
||||
#include "../terminal/kterm.h"
|
||||
#include "../cpu.h"
|
||||
#include "PhysicalMemoryManager.h"
|
||||
|
||||
|
||||
void SetupVMM();
|
||||
|
||||
void AllocatePage(uint32_t v_addr );
|
||||
void FreePage(uint32_t v_addr);
|
||||
|
||||
void Immediate_Map(uint32_t vaddr, uint32_t paddr);
|
||||
void Immediate_Unmap (uint32_t v_addr);
|
||||
|
||||
// void Demand_map(uint32_t p_addr, uint32_t v_addr);
|
||||
// void Demand_Unmap (uint32_t v_addr);
|
@ -1,15 +1,11 @@
|
||||
#include "gdtc.h"
|
||||
#include "../tty/kterm.h"
|
||||
#include "../../terminal/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];
|
||||
SegmentDescriptor GlobalDescriptorTable[6];
|
||||
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;
|
||||
@ -21,7 +17,6 @@ void add_descriptor(int which , unsigned long base, unsigned long limit, unsigne
|
||||
GlobalDescriptorTable[which].granularity |= (granularity & 0xF0);
|
||||
GlobalDescriptorTable[which].access = access;
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@ -39,24 +34,13 @@ void initGDT(){
|
||||
add_descriptor(KERNEL_DATA_SEGMENT, 0, 0xFFFFFFFF, 0x92, 0xCF);
|
||||
|
||||
// User Code Segment
|
||||
// TODO:
|
||||
add_descriptor(USER_CODE_SEGMENT, 0, 0xFFFFFFFF, 0xFA, 0xCF);
|
||||
|
||||
// User Data Segement
|
||||
// TODO:
|
||||
|
||||
add_descriptor(USER_DATA_SEGMENT, 0, 0xFFFFFFFF, 0xF2, 0xCF);
|
||||
|
||||
// init Gdt Descriptor
|
||||
gdtDescriptor.limit = ((sizeof(SegmentDescriptor ) * 5 ) - 1);
|
||||
gdtDescriptor.base = (unsigned int) &GlobalDescriptorTable;
|
||||
|
||||
printf("Hello GDT!\n");
|
||||
|
||||
|
||||
LoadGlobalDescriptorTable();
|
||||
|
||||
// while (true)
|
||||
// asm volatile("hlt");
|
||||
|
||||
|
||||
|
||||
gdtDescriptor.limit = ((sizeof(SegmentDescriptor ) * 6 ) - 1);
|
||||
gdtDescriptor.base = (unsigned int) (&GlobalDescriptorTable);
|
||||
|
||||
}
|
@ -1,6 +1,16 @@
|
||||
#pragma once
|
||||
#include <stdint.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
|
||||
#define TASK_STATE_SEGMENT 5
|
||||
|
||||
|
||||
|
||||
struct SegmentDescriptor {
|
||||
unsigned short limit_low;
|
||||
unsigned short base_low;
|
||||
@ -10,18 +20,16 @@ struct SegmentDescriptor {
|
||||
unsigned char base_high;
|
||||
}__attribute__((packed));
|
||||
|
||||
extern SegmentDescriptor GlobalDescriptorTable[6];
|
||||
|
||||
struct GlobalDescriptorTableDescriptor{
|
||||
unsigned short limit;
|
||||
unsigned int base;
|
||||
}__attribute__((packed));
|
||||
}__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();
|
142
source/kernel/memory/memory.cpp
Normal file
142
source/kernel/memory/memory.cpp
Normal file
@ -0,0 +1,142 @@
|
||||
#include "./memory.h"
|
||||
uint32_t* memoryBitMap;
|
||||
/*
|
||||
|
||||
*/
|
||||
void PhysicalMemory::setup( MemoryInfo* memory) {
|
||||
|
||||
// calculate the maximum number of blocks
|
||||
max_blocks = KB_TO_BLOCKS(memory->TotalMemory);
|
||||
|
||||
used_blocks = 0;
|
||||
|
||||
memoryBitMap = (uint32_t*) 0x00a00000;
|
||||
|
||||
|
||||
printf("Maximum Number of blocks: 0x%x, Number of bytes for memMap: 0x%x\n", max_blocks , (max_blocks/8));
|
||||
|
||||
//Size of memory map
|
||||
uint32_t memMap_size = (max_blocks / 8 ) ;
|
||||
|
||||
printf("Memory Map size: 0x%x\n", memMap_size );
|
||||
printf("size of int in bytes: 0x%x \n" , sizeof(int));
|
||||
|
||||
// Set all places in memory as free
|
||||
memset(memoryBitMap, 0xFF, memMap_size );
|
||||
}
|
||||
|
||||
// NOTE: this can only give blocks of 4kb at a time!
|
||||
void* PhysicalMemory::allocate_block() {
|
||||
uint8_t blocks_available = max_blocks - used_blocks;
|
||||
// Are there any blocks available?
|
||||
if( blocks_available <= 0)
|
||||
{
|
||||
printf("No blocks available. Blocks Delta: 0x%x\n", blocks_available);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Find 1 free block somewhere
|
||||
int free_block_index = bitmap_first_unset(memoryBitMap, (max_blocks /8) /*memMap Size*/ );
|
||||
|
||||
|
||||
|
||||
if(free_block_index == -1)
|
||||
{
|
||||
printf("Could not find a good block!\n");
|
||||
// Could not find a block
|
||||
return (void*)0xFFFF;
|
||||
}
|
||||
|
||||
if(free_block_index == 0)
|
||||
printf("Somethings wrong!!!\n");
|
||||
|
||||
// Set the block to be used!
|
||||
bitmap_unset(memoryBitMap, free_block_index);
|
||||
// Increase the used_block count!
|
||||
used_blocks++;
|
||||
printf("used blocks: 0x%x\n", used_blocks);
|
||||
// return the pointer to the physical address
|
||||
return (void*) (BLOCK_SIZE * free_block_index);
|
||||
}
|
||||
|
||||
|
||||
void PhysicalMemory::free_block(void* p) {
|
||||
// If it is a null pointer we don't need to do anything.
|
||||
if(p==0) {
|
||||
return;
|
||||
}
|
||||
// calculate the index into the bitmap
|
||||
int index = ((uint32_t) p) / BLOCK_SIZE;
|
||||
|
||||
// set the block to be free
|
||||
bitmap_set(memoryBitMap, index);
|
||||
used_blocks--;
|
||||
printf("used blocks: 0x%x, after free\n", used_blocks);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
void PhysicalMemory::allocate_region(uint32_t startAddress, uint32_t size) {
|
||||
// every bit should be 4KiB
|
||||
// every byte is 8*4KiB = 32KiB
|
||||
|
||||
int NumberOfBlocksToAllocate = ( size / 1024) / 4 / 8 + 1;
|
||||
int startBlock = (startAddress / 1024) / 4 / 8 ;
|
||||
|
||||
// printf("NumberOfBlocksToAllocate: 0x%x\n", NumberOfBlocksToAllocate);
|
||||
//printf( "start block: 0x%x\n" , startBlock);
|
||||
for( int i = 0; i < NumberOfBlocksToAllocate; i++)
|
||||
{
|
||||
|
||||
//printf("ALLOCATE BLOCK: 0x%x\n" , startBlock + i );
|
||||
bitmap_unset(memoryBitMap, startBlock+ i);
|
||||
used_blocks++;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
void PhysicalMemory::deallocate_region(uint32_t StartAddress , uint32_t size ) {
|
||||
// NOT IMPLEMENTED YET
|
||||
}
|
||||
|
||||
|
||||
void mapMultibootMemoryMap( MemoryInfo* memInfo , 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->TotalMemory += mmap->len;
|
||||
} else {
|
||||
memInfo->ReservedMemory += mmap->len;
|
||||
}
|
||||
|
||||
print_Multiboot_memory_Map(mmap);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Debug Verbose functions
|
||||
*
|
||||
* @param mmap
|
||||
*/
|
||||
|
||||
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
|
||||
);
|
||||
}
|
||||
|
45
source/kernel/memory/memory.h
Normal file
45
source/kernel/memory/memory.h
Normal file
@ -0,0 +1,45 @@
|
||||
#pragma once
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#include "memoryinfo.h"
|
||||
#include "../prekernel/multiboot.h"
|
||||
#include "../terminal/kterm.h"
|
||||
#include "../../lib/include/mem.h"
|
||||
#include "../bitmap.h"
|
||||
|
||||
#define BLOCK_SIZE 4092
|
||||
#define BLOCKS_PER_WORD 32 // A word is 16 bit in x86 machines according to my google search results!
|
||||
|
||||
#define KB_TO_BLOCKS(x) (x / BLOCK_SIZE)
|
||||
#define IS_ALIGNED(addr, align) !((addr) & ~((align) - 1))
|
||||
#define ALIGN(addr, align) (((addr) & ~((align) - 1 )) + (align))
|
||||
|
||||
void initialise_available_regions(uint32_t memoryMapAddr, uint32_t memoryMapLastAddr, uint32_t* memoryBitMap, int* used_blocks);
|
||||
|
||||
extern uint32_t* memoryBitMap;
|
||||
|
||||
class PhysicalMemory
|
||||
{
|
||||
public:
|
||||
void setup(MemoryInfo* memory);
|
||||
void destroy();
|
||||
void free_block(void* ptr);
|
||||
void* allocate_block();
|
||||
void allocate_region(uint32_t, uint32_t);
|
||||
void deallocate_region(uint32_t , uint32_t );
|
||||
|
||||
private:
|
||||
size_t pmmap_size;
|
||||
size_t max_blocks;
|
||||
int used_blocks;
|
||||
};
|
||||
|
||||
void mapMultibootMemoryMap( MemoryInfo* memInfo , multiboot_info_t *mbt);
|
||||
|
||||
/**
|
||||
* @brief Debug Verbose Functions
|
||||
*
|
||||
* @param mmap
|
||||
*/
|
||||
void print_Multiboot_memory_Map(multiboot_memory_map_t* mmap);
|
20
source/kernel/memory/memoryinfo.h
Normal file
20
source/kernel/memory/memoryinfo.h
Normal file
@ -0,0 +1,20 @@
|
||||
#pragma once
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
|
||||
struct MemoryArea{
|
||||
void* StartAddress;
|
||||
size_t Size;
|
||||
unsigned int type;
|
||||
MemoryArea* Next;
|
||||
|
||||
}__attribute__((packed));
|
||||
|
||||
struct MemoryInfo {
|
||||
uint32_t TotalMemory;
|
||||
uint32_t ReservedMemory;
|
||||
MemoryArea* MemoryRegionList;
|
||||
}__attribute__((packed));
|
||||
|
||||
|
||||
|
11
source/kernel/partitiontable/mbr/MasterBootRecord.h
Normal file
11
source/kernel/partitiontable/mbr/MasterBootRecord.h
Normal file
@ -0,0 +1,11 @@
|
||||
#pragma once
|
||||
#include <stdint.h>
|
||||
#include "PartitionTableEntry.h"
|
||||
|
||||
struct MBR {
|
||||
uint8_t code [440];
|
||||
uint32_t uniqueID;
|
||||
uint16_t Reserved;
|
||||
PartitionTableEntry TableEntries[4];
|
||||
uint16_t ValidBootsector;
|
||||
}__attribute__((packed));
|
11
source/kernel/partitiontable/mbr/PartitionTableEntry.h
Normal file
11
source/kernel/partitiontable/mbr/PartitionTableEntry.h
Normal file
@ -0,0 +1,11 @@
|
||||
#pragma once
|
||||
#include <stdint.h>
|
||||
|
||||
struct PartitionTableEntry{
|
||||
uint8_t driveAttribute;
|
||||
uint8_t CHS_start_address [3];
|
||||
uint8_t PartitionType;
|
||||
uint8_t CHS_lastSector_Address[3];
|
||||
uint32_t LBA_partition_start;
|
||||
uint32_t Number_sectors_inPartition;
|
||||
}__attribute__((packed));
|
30
source/kernel/prekernel/bootstructure.h
Normal file
30
source/kernel/prekernel/bootstructure.h
Normal file
@ -0,0 +1,30 @@
|
||||
#pragma once
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
extern "C" const uint32_t kernel_begin;
|
||||
extern "C" const uint32_t kernel_end;
|
||||
|
||||
#define IS_AVAILABLE_MEM(MEM_TYPE) MEM_TYPE & 0x1
|
||||
#define IS_ACPI_MEM(MEM_TYPE) MEM_TYPE & 0x2
|
||||
#define IS_RESERVED_MEM(MEM_TYPE) MEM_TYPE & 0x4
|
||||
#define IS_NVS_MEMORY(MEM_TYPE) MEM_TYPE & 0x8
|
||||
#define IS_BADRAM_MEMORY(MEM_TYPE) MEM_TYPE & 0x10
|
||||
|
||||
|
||||
struct BootInfoBlock {
|
||||
bool MapIsInvalid;
|
||||
uint32_t bootDeviceID ;
|
||||
|
||||
uint32_t GrubModuleCount;
|
||||
|
||||
bool ValidSymbolTable;
|
||||
uint32_t SymbolTableAddr;
|
||||
uint32_t SymbolTabSize;
|
||||
uint32_t SymbolStrSize;
|
||||
|
||||
bool ValidELFHeader;
|
||||
|
||||
bool EnabledVBE;
|
||||
|
||||
};
|
140
source/kernel/prekernel/prekernel.cpp
Normal file
140
source/kernel/prekernel/prekernel.cpp
Normal file
@ -0,0 +1,140 @@
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include "multiboot.h"
|
||||
#include "../memory/PhysicalMemoryManager.h"
|
||||
|
||||
#define CHECK_FLAG(flags, bit) ((flags) & (1 <<(bit)))
|
||||
#define VADDR_TO_PADDR(vaddr) (vaddr - 0xC0000000)
|
||||
#define PADDR_TO_VADDR(paddr) (paddr + 0xC0000000)
|
||||
|
||||
|
||||
extern "C" void prekernelSetup ( unsigned long magic, multiboot_info_t* mbi)
|
||||
{
|
||||
|
||||
/*
|
||||
* Check Multiboot magic number
|
||||
*/
|
||||
if (magic != MULTIBOOT_BOOTLOADER_MAGIC)
|
||||
{
|
||||
// PANIC!!
|
||||
return;
|
||||
}
|
||||
|
||||
mbi = PADDR_TO_VADDR(mbi);
|
||||
|
||||
|
||||
// Setup the physical memory manager immmediatly
|
||||
// Doing so saves the complications of doing it later when
|
||||
// paging is enabled
|
||||
|
||||
/*
|
||||
If we got a memory map from our bootloader we
|
||||
should be parsing it to find out the memory regions available.
|
||||
*/
|
||||
if (CHECK_FLAG(mbi->flags, 6))
|
||||
{
|
||||
|
||||
// Calculate total memory size
|
||||
uint32_t RAM_size = 0;
|
||||
for(
|
||||
multiboot_memory_map_t* mmap = (multiboot_memory_map_t*) mbi->mmap_addr;
|
||||
(unsigned long)mmap < mbi->mmap_addr + mbi->mmap_length;
|
||||
mmap += mmap->size +sizeof(mmap->size)
|
||||
){
|
||||
RAM_size += mmap->len;
|
||||
}
|
||||
|
||||
// Call SetupPhysicalMemoryManager at its physical address
|
||||
SetupPhysicalMemoryManager ( (uint32_t)VADDR_TO_PADDR(&kernel_end), RAM_size);
|
||||
|
||||
for(
|
||||
multiboot_memory_map_t* mmap = (multiboot_memory_map_t*) mbi->mmap_addr;
|
||||
(unsigned long)mmap < mbi->mmap_addr + mbi->mmap_length;
|
||||
mmap += mmap->size +sizeof(mmap->size)
|
||||
){
|
||||
|
||||
if(mmap->type == MULTIBOOT_MEMORY_AVAILABLE)
|
||||
deallocate_region(mmap->addr, mmap->len);
|
||||
if(mmap->type == MULTIBOOT_MEMORY_ACPI_RECLAIMABLE)
|
||||
allocate_region(mmap->addr, mmap->len);
|
||||
if(mmap->type == MULTIBOOT_MEMORY_RESERVED)
|
||||
allocate_region(mmap->addr, mmap->len);
|
||||
if(mmap->type == MULTIBOOT_MEMORY_NVS)
|
||||
allocate_region(mmap->addr, mmap->len);
|
||||
if(mmap->type == MULTIBOOT_MEMORY_BADRAM)
|
||||
allocate_region(mmap->addr, mmap->len);
|
||||
|
||||
}
|
||||
|
||||
|
||||
// Allocate the kernel
|
||||
allocate_region( (uint32_t)&kernel_begin, ( (uint32_t)&kernel_end - (uint32_t)&kernel_begin)- 0xC0000000 );
|
||||
|
||||
// Allocate the memory region below 1MB
|
||||
allocate_region(0x0000000, 0x00100000);
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
// We didn't get a memory map from our bootloader.
|
||||
// PANIC!!!!
|
||||
return;
|
||||
}
|
||||
|
||||
// allocate a full block for the other boot info!
|
||||
BootInfoBlock* BIB = (BootInfoBlock*) allocate_block();
|
||||
|
||||
|
||||
/* is boot device valid ? */
|
||||
if (CHECK_FLAG (mbi->flags, 1))
|
||||
{
|
||||
BIB->bootDeviceID = mbi->boot_device;
|
||||
} else{
|
||||
BIB->bootDeviceID = 0x11111111;
|
||||
}
|
||||
|
||||
/* Are mods_* valid? */
|
||||
if(CHECK_FLAG ( mbi->flags, 3)){
|
||||
multiboot_module_t *mod;
|
||||
uint32_t i;
|
||||
|
||||
BIB->GrubModuleCount = mbi->mods_count;
|
||||
|
||||
|
||||
for(i = 0, mod = (multiboot_module_t *) mbi->mods_addr; i < mbi->mods_count; i++ , mod++){
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/* Is the symbol table of a.out valid? */
|
||||
if (CHECK_FLAG(mbi->flags, 4))
|
||||
{
|
||||
// NOTE: Do something with it.. (Store it , process it etc...)
|
||||
// printf("- Valid Symbol Table available at 0x%x.\n Tab Size: %d, str Size: %d\n", BootInfo->SymbolTableAddr, BootInfo->SymbolTabSize, BootInfo->SymbolStrSize);
|
||||
BIB->ValidSymbolTable = true;
|
||||
multiboot_aout_symbol_table_t *multiboot_aout_sym = &(mbi->u.aout_sym);
|
||||
|
||||
} else{
|
||||
BIB->ValidSymbolTable = false;
|
||||
}
|
||||
|
||||
/* Is the section header table of ELF valid? */
|
||||
if (CHECK_FLAG(mbi->flags, 5))
|
||||
{
|
||||
// NOTE: Do something with it.. (Store it , process it etc...)
|
||||
BIB->ValidELFHeader = true;
|
||||
multiboot_elf_section_header_table_t *multiboot_elf_sec = &(mbi->u.elf_sec);
|
||||
|
||||
}else{
|
||||
BIB->ValidELFHeader = false;
|
||||
}
|
||||
|
||||
/* Draw diagonal blue line */
|
||||
if (CHECK_FLAG (mbi->flags, 12)){
|
||||
BIB->EnabledVBE = true;
|
||||
|
||||
// NOTE: Do something with it.. (Store it , process it etc...)
|
||||
} else{
|
||||
BIB->EnabledVBE;
|
||||
}
|
||||
}
|
58
source/kernel/serial.h
Normal file
58
source/kernel/serial.h
Normal file
@ -0,0 +1,58 @@
|
||||
#pragma once
|
||||
|
||||
#include "terminal/kterm.h"
|
||||
#include "drivers/io/io.h"
|
||||
#define PORT 0x3f8
|
||||
static int init_serial() {
|
||||
|
||||
#ifdef __VERBOSE__
|
||||
printf("Init Serial\n");
|
||||
#endif
|
||||
|
||||
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]);
|
||||
}
|
||||
}
|
||||
|
226
source/kernel/supervisorterminal/superVisorTerminal.cpp
Normal file
226
source/kernel/supervisorterminal/superVisorTerminal.cpp
Normal file
@ -0,0 +1,226 @@
|
||||
#include "superVisorTerminal.h"
|
||||
#include "../drivers/ata/ataDevice.h"
|
||||
#include "../partitiontable/mbr/MasterBootRecord.h"
|
||||
#include "../filesystem/FAT/BiosParameterBlock.h"
|
||||
#include "../filesystem/FAT/DirectoryEntry.h"
|
||||
bool isRunning = true;
|
||||
void startSuperVisorTerminal(){
|
||||
while (isRunning){
|
||||
|
||||
printf("SUPERVISOR:>$ " );
|
||||
int characterCount = 0;
|
||||
char command[10] = "";
|
||||
|
||||
// NOTE: lets just show a kernel prompt
|
||||
uint8_t ScanCode = getKey();
|
||||
while( ScanCode != 0x1C )
|
||||
{
|
||||
char character = getASCIIKey();
|
||||
kterm_put(character );
|
||||
// wHAT THE HELL
|
||||
|
||||
if( characterCount < 10 ){
|
||||
command[characterCount] = character;
|
||||
characterCount++;
|
||||
}
|
||||
|
||||
ScanCode = getKey();
|
||||
}
|
||||
printf("\n");
|
||||
KeyHandled();
|
||||
|
||||
|
||||
if ( strncmp("DATE", command , characterCount ) == 0 )
|
||||
{
|
||||
read_rtc();
|
||||
printf("======= Time & Date ==========\n");
|
||||
printf(" - Date: %02d-%02d-%02d\n",day, month, year);
|
||||
printf(" - Time: %02d:%02d:%02d\n" , hour, minute, second);
|
||||
printf(" - Ticks: %09d\n", pit_tick);
|
||||
}
|
||||
if( strncmp ("MEMORY" , command , characterCount) == 0 )
|
||||
{
|
||||
// Show memory layout
|
||||
printf("========= Memory (very inaccurate) ==========\n");
|
||||
printf("Kernel MemoryMap:\n");
|
||||
printf("kernel: 0x%x - 0x%x\n", &kernel_begin , &kernel_end);
|
||||
printf("Frames used: %d blocks of 4 KiB\n", GetUsedBlocks());
|
||||
|
||||
}
|
||||
if(strncmp("TEST", command, characterCount) == 0)
|
||||
{
|
||||
// TEST #DE exception
|
||||
asm volatile ("MOV $4, %AX ; MOV $0, %BX ; DIV %BX"); // IRS 0
|
||||
}
|
||||
if (strncmp("VERSION", command , characterCount) == 0)
|
||||
{
|
||||
// Show version information
|
||||
printf("========= Version ========\n");
|
||||
printf("Kernel v%d\n", 0);
|
||||
|
||||
}
|
||||
if(strncmp("CLEAR", command, characterCount) == 0)
|
||||
{
|
||||
kterm_init();
|
||||
printf("|=== BarinkOS ===|\n");
|
||||
}
|
||||
if(strncmp("FAT", command, characterCount) == 0)
|
||||
{
|
||||
|
||||
int devNumber = 0 ;
|
||||
for ( auto device : ide_devices){
|
||||
if (!device.Reserved)
|
||||
continue;
|
||||
printf("Device %d\n" , devNumber);
|
||||
printf (" Device on Channel: (0x%x) %s\n" ,device.Channel, device.Channel == 0 ? "Primary" : "Secondary");
|
||||
printf (" Device drive:(0x%x) %s\n" , device.Drive, device.Drive? "Slave" : "Master");
|
||||
printf (" Device Type:(0x%x) %s\n" , device.Type, device.Type ? "ATAPI" : "ATA");
|
||||
devNumber ++;
|
||||
|
||||
}
|
||||
|
||||
enum BUS_PORT {
|
||||
Primary= 0x1f0,
|
||||
Secondary = 0x170
|
||||
};
|
||||
|
||||
|
||||
|
||||
ATA_DEVICE::Identify((uint16_t) BUS_PORT::Primary, DEVICE_DRIVE::MASTER);
|
||||
|
||||
const int C = 0;
|
||||
const int H = 0;
|
||||
const int HPC = 16;
|
||||
const int SPT = 63;
|
||||
|
||||
int S = 1;
|
||||
uint32_t LBA = (C*HPC+H) * SPT + (S-1);
|
||||
printf("LBA: %d\n" , LBA);
|
||||
uint16_t buffer [256];
|
||||
|
||||
|
||||
ATA_DEVICE::Read(BUS_PORT::Primary, DEVICE_DRIVE::MASTER, LBA, buffer);
|
||||
|
||||
MBR* mbr = (MBR*) buffer;
|
||||
|
||||
printf("BootSector: 0x%x\n", mbr->ValidBootsector );
|
||||
for( int i = 0 ; i < 4 ; i ++){
|
||||
PartitionTableEntry PT = mbr->TableEntries[i];
|
||||
|
||||
printf("Partition %d [ %d sectors, PartitionType: %x, 0x%x, \nLBA Start: 0x%x ]\n" ,
|
||||
i, PT.Number_sectors_inPartition, PT.PartitionType, mbr->uniqueID, PT.LBA_partition_start );
|
||||
}
|
||||
|
||||
// Find the BiosParameter block
|
||||
uint16_t biosparameterblock[256];
|
||||
ATA_DEVICE::Read(BUS_PORT::Primary, DEVICE_DRIVE::MASTER, mbr->TableEntries[0].LBA_partition_start, biosparameterblock);
|
||||
|
||||
BiosParameterBlock* bpb = (BiosParameterBlock*) biosparameterblock;
|
||||
|
||||
|
||||
printf("\nBPB: Bytes per Sector %d\n", bpb->BytesPerSector );
|
||||
printf("OEM ID: %s\n", bpb->OEM_id);
|
||||
printf("Bytes per sector: %d\n", bpb->BytesPerSector);
|
||||
printf("Sectors per cluster: %d\n", bpb->SectorsPerCluster);
|
||||
printf("Reserved sectors: %d\n", bpb->ReservedSectors);
|
||||
printf("Number of FAT: %d\n", bpb->NumberOfFileAllocationTables);
|
||||
printf("Number of Dir entries: %d\n", bpb->NumberOfDirectoryEntries);
|
||||
printf("Total Sectors in volume: %d\n", bpb->TotalSectorsInLogicalVolume);
|
||||
printf("Sectors per FAT: %d\n", bpb->NumberOfSectorsPerFAT);
|
||||
|
||||
/**
|
||||
* @brief File Allocation Table
|
||||
*/
|
||||
|
||||
|
||||
uint32_t FATAddress = mbr->TableEntries[0].LBA_partition_start + bpb->ReservedSectors ;
|
||||
uint16_t FAT[256];
|
||||
ATA_DEVICE::Read(BUS_PORT::Primary, DEVICE_DRIVE::MASTER, FATAddress, FAT );
|
||||
|
||||
// Show data in terminal
|
||||
for(int i = 0; i < 256; i++ ) {
|
||||
printf("%x ", FAT[i]);
|
||||
}
|
||||
kterm_put('\n');
|
||||
|
||||
|
||||
uint32_t RootDirectoryRegion = FATAddress + ( bpb->NumberOfFileAllocationTables * bpb->NumberOfSectorsPerFAT );
|
||||
uint32_t DataRegion = RootDirectoryRegion + ((bpb->NumberOfDirectoryEntries * 32) / bpb->BytesPerSector );
|
||||
|
||||
uint16_t data2 [256];
|
||||
ATA_DEVICE::Read(BUS_PORT::Primary, DEVICE_DRIVE::MASTER, RootDirectoryRegion, data2 );
|
||||
DirectoryEntry* RootDirectory = (DirectoryEntry*) data2;
|
||||
// List files in root
|
||||
for(int i= 0; i < bpb->NumberOfDirectoryEntries ; i++ )
|
||||
{
|
||||
DirectoryEntry* entry = (DirectoryEntry*)((uint32_t) RootDirectory + (i * sizeof(DirectoryEntry)));
|
||||
|
||||
if( entry->filename[0] == (uint8_t) 0x00 )
|
||||
break; // There are no more entries in this directory or the entry is free
|
||||
|
||||
if( entry->attribute & 0x01 == 0x01 || entry->attribute & 0x20 == 0x20)
|
||||
continue; // Skip listing if hidden or Achieve flag is set
|
||||
|
||||
// Print the filename;
|
||||
for( int n = 0; n < 8; n++ ){
|
||||
if(entry->filename[n] == 0x20)
|
||||
break;
|
||||
kterm_put(entry->filename[n]);
|
||||
}kterm_put('\n');
|
||||
|
||||
for( int n = 0; n < 3; n++){
|
||||
kterm_put(entry->Extension[n]);
|
||||
}kterm_put('\n');
|
||||
|
||||
printf("Attribute: %x \n" , entry->attribute);
|
||||
printf("FileSize: %d Bytes\n", entry->FilesizeInBytes);
|
||||
|
||||
if( entry->FilesizeInBytes != 0x0 || entry->attribute & 0x8 == 0x0){
|
||||
printf("Show contents");
|
||||
|
||||
printf( "Start cluster of the file: 0x%x\n" , entry->StartingCluster);
|
||||
|
||||
printf("IS it only 1 cluster? %s\n" , FAT[i] == 0xFFFF? "Yes": "No" );
|
||||
|
||||
uint32_t sector = DataRegion + ((entry->StartingCluster - 0x02 ) * bpb->SectorsPerCluster);
|
||||
|
||||
|
||||
uint16_t dataBlob [256];
|
||||
ATA_DEVICE::Read(BUS_PORT::Primary, DEVICE_DRIVE::MASTER, sector, dataBlob );
|
||||
for( int n = 0; n < 256; n++)
|
||||
{
|
||||
kterm_put(dataBlob[n] & 0x00ff);
|
||||
|
||||
kterm_put(dataBlob[n] >> 8);
|
||||
}kterm_put('\n');
|
||||
|
||||
|
||||
}
|
||||
|
||||
printf("======================\n");
|
||||
|
||||
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
|
||||
if(strncmp("DEVICES", command, characterCount) == 0){
|
||||
printf("================ CONNECTED DEVICES ===============\n");
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
printf("executed command: %s\n", command);
|
||||
|
||||
|
||||
delay(1000);
|
||||
}
|
||||
}
|
11
source/kernel/supervisorterminal/superVisorTerminal.h
Normal file
11
source/kernel/supervisorterminal/superVisorTerminal.h
Normal file
@ -0,0 +1,11 @@
|
||||
#pragma once
|
||||
#include "../terminal/kterm.h"
|
||||
#include "../time.h"
|
||||
#include "../drivers/pit/pit.h"
|
||||
#include "../drivers/ps-2/keyboard.h"
|
||||
#include "../memory/PhysicalMemoryManager.h"
|
||||
extern "C" {
|
||||
#include "../../lib/include/string.h"
|
||||
}
|
||||
|
||||
void startSuperVisorTerminal();
|
@ -21,7 +21,8 @@ void kterm_init () {
|
||||
kterm_row = 0;
|
||||
kterm_column = 0;
|
||||
kterm_color = vga_entry_color ( VGA_COLOR_LIGHT_GREY , VGA_COLOR_BLACK);
|
||||
kterm_buffer = (uint16_t*) 0xB8000;
|
||||
kterm_buffer = (uint16_t*) 0xC03FF000;
|
||||
|
||||
for (size_t y = 0; y < VGA_HEIGHT; y++ ){
|
||||
for( size_t x = 0; x < VGA_WIDTH; x++){
|
||||
const size_t index = y * VGA_WIDTH + x;
|
||||
@ -31,7 +32,6 @@ void kterm_init () {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void kterm_resetcolor(){
|
||||
kterm_color = vga_entry_color ( VGA_COLOR_LIGHT_GREY , VGA_COLOR_BLACK);
|
||||
}
|
||||
@ -42,9 +42,7 @@ void kterm_setcolor(uint8_t color){
|
||||
|
||||
void kterm_putat (char c, uint8_t color, size_t x, size_t y ) {
|
||||
const size_t index = y * VGA_WIDTH + x;
|
||||
|
||||
kterm_buffer[index] = vga_entry(c, color);
|
||||
|
||||
}
|
||||
|
||||
void enable_cursor (uint8_t start_cursor , uint8_t end_cursor ){
|
||||
@ -137,7 +135,7 @@ void kterm_writestring(const char* data ){
|
||||
}
|
||||
|
||||
|
||||
void itoa (char *buf, int base, int d) {
|
||||
static void itoa (char *buf, int base, int d) {
|
||||
char *p = buf;
|
||||
char *p1, *p2;
|
||||
unsigned long ud = d;
|
||||
@ -174,7 +172,7 @@ void itoa (char *buf, int base, int d) {
|
||||
}
|
||||
|
||||
void printf ( const char *format, ...) {
|
||||
return;
|
||||
|
||||
char **arg = (char **)&format;
|
||||
int c;
|
||||
char buf[20];
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user