Compare commits

...

10 Commits

28 changed files with 467 additions and 66 deletions

3
.gitattributes vendored Normal file
View File

@ -0,0 +1,3 @@
*.pdf filter=lfs diff=lfs merge=lfs -text
*.png filter=lfs diff=lfs merge=lfs -text
*.svg filter=lfs diff=lfs merge=lfs -text

View File

@ -2,9 +2,10 @@
EMULATOR = qemu-system-i386
AS = ${HOME}/opt/cross/bin/i686-elf-as
CC = ${HOME}/opt/cross/bin/i686-elf-gcc
CFLAGS = -std=gnu99 -ffreestanding -O2 -Wall -Wextra
CPP = ${HOME}/opt/cross/bin/i686-elf-g++
CFLAGS = -ffreestanding -O2 -Wall -Wextra
OFILES = $(BUILD_DIR)/boot.o $(BUILD_DIR)/kterm.o $(BUILD_DIR)/kernel.o
OFILES = $(BUILD_DIR)/boot.o $(BUILD_DIR)/kterm.o $(BUILD_DIR)/kernel.o $(BUILD_DIR)/io.o $(BUILD_DIR)/MMU.o
SRC_DIR = src
BUILD_DIR = build
@ -23,7 +24,8 @@ all: clean build
build: build_kernel run
run:
$(EMULATOR) -kernel $(BUILD_DIR)/myos.bin
$(EMULATOR) -kernel $(BUILD_DIR)/myos.bin -serial stdio
build_kernel: $(OBJ_LINK_LIST)
$(CC) -T $(SRC_DIR)/kernel/arch/i386/linker.ld -o $(BUILD_DIR)/myos.bin \
@ -37,10 +39,10 @@ clean:
rm -f $(BUILD_DIR)/myos.bin $(INTERNAL_OBJS)
$(BUILD_DIR)/kernel.o:
$(CC) -c $(SRC_DIR)/kernel/kernel.c -o $(BUILD_DIR)/kernel.o $(CFLAGS)
$(CPP) -c $(SRC_DIR)/kernel/kernel.cpp -o $(BUILD_DIR)/kernel.o $(CFLAGS) -fno-exceptions -fno-rtti
$(BUILD_DIR)/kterm.o:
$(CC) -c $(SRC_DIR)/kernel/arch/i386/tty/kterm.c -o $(BUILD_DIR)/kterm.o $(CFLAGS)
$(CC) -c $(SRC_DIR)/kernel/arch/i386/tty/kterm.c -o $(BUILD_DIR)/kterm.o $(CFLAGS) -std=gnu99
$(BUILD_DIR)/boot.o:
$(AS) $(SRC_DIR)/kernel/arch/i386/boot.s -o $(BUILD_DIR)/boot.o
@ -50,3 +52,8 @@ $(BUILD_DIR)/crti.o:
$(BUILD_DIR)/crtn.o:
$(AS) $(SRC_DIR)/kernel/arch/i386/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)/MMU.o:
$(CPP) -c $(SRC_DIR)/kernel/MMU.cpp -o $(BUILD_DIR)/MMU.o $(CFLAGS) -fno-exceptions -fno-rtti

View File

@ -1,52 +1,49 @@
# Writing an Operating system
## As a learning experience... Inspired by people like.. Linus Torvalds and Andreas Kling
## As a learning experience!
Inspired by people like Linus Torvalds and Andreas Kling
![Logo](images/BarinkOS.png)
________________________
### Screenshot(s)
![Scrolling the terminal](screenshots/Screenshot1.png) \
The first scrolling boot screen. 😲
________________________
### The goal
Writing a hobby operating system to better understand the basic building blocks of any operating system.
________________________
### Operating System Technical specs/details
Currently the operating system is in the planning fase.
I hope to soon have the basic output and booting sequence with multiboot done.
The operating system can print strings to the
screen. The terminal/screen has scrolling so the latest messages are visible on the screen.
________________________
### Planning
[See TODO](TODO.md)
[x] Muliboot to kernel \
[ ] Printing strings and integer numbers (both decimal and hex) on the screen is certainly a must. This is one of most basic ways of debugging, and virtually all of us have gone through a kprint() or kout in version 0.01. \
[ ] Outputting to a serial port will save you a lot of debugging time. You don't have to fear losing information due to scrolling. You will be able to test your OS from a console, filter interesting debug messages, and automatize some tests. \
[ ] Having a working and reliable interrupt/exception handling system that can dump the contents of the registers (and perhaps the address of the fault) will be very useful. \
[ ] Plan your memory map (virtual, and physical) : decide where you want the data to be. \
[ ] The heap: allocating memory at runtime (malloc and free) is almost impossible to go without. It should be implemented as soon as possible.
### Other features I am thinking of:
[ ] USTAR Filesystem ( For its simplicity this is very likely the first filesystem the OS is going to support) \
[ ] Memory Management \
[ ] Scheduling (Unknown what the scheduling algorithm will be, as with everything suspect simplicity) \
[ ] RPC - for interprocess communication \
[ ] Sync primitives - Semaphores, Mutexes, spinlocks et al. \
[ ] ACPI support ( Or some other basic way to support shutdown, reboot and possibly hibernation ) \
[ ] ATA support \
[ ] Keyboard support ( must have ) \
[ ] Basic hardware recognition ( CPU codename, memory, ATA harddisk, RAW diskSpace, CPU speed et al. ) \
[ ] Basic Terminal \
[ ] Simplistic draw ( maybe ?!?) \
### Far in the future: \
[ ] Basic Window server/client
#### Support for more filesystems if I like the challenge in writing these ...
[ ] FAT Filesystem
[ ] EXT2 Filesystem
________________________
### Docs
[Intro](docs/Intro.md) \
[Manuals](docs/Manuals.md) \
[Project structure](docs/ProjectStructure.md)
________________________
### Resources:
#### General kernel stuff
[wiki.osdev.org/Main_Page](wiki.osdev.org/Main_Page)
[Modern Operating Systems [book]](https://www.amazon.com/Modern-Operating-Systems-Tanenbaum-Andrew/dp/1292061421/ref=sr_1_1?__mk_nl_NL=%C3%85M%C3%85%C5%BD%C3%95%C3%91&dchild=1&keywords=Modern+Operating+systems&qid=1619967779&sr=8-1)
[whiteheadsoftware.dev](https://whiteheadsoftware.dev/operating-systems-development-for-dummies/)
#### More specific stuff
[VFS explained: science.unitn.it](https://www.science.unitn.it/~fiorella/guidelinux/tlk/node102.html)
[whiteheadsoftware.dev](https://whiteheadsoftware.dev/operating-systems-development-for-dummies/)

40
TODO.md Normal file
View File

@ -0,0 +1,40 @@
# TODO list
## Start planning
<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" /> Printing values/numbers to the screen (a.k.k itoa) \
<input type="checkbox" /> Extend Multiboot implementation \
<input type="checkbox" checked/> Output to serial port \
<input type="checkbox" /> Move to protected mode \
<input type="checkbox" /> Enabel CMOS clock \
<input type="checkbox" /> Time measurement (PIC &| PIT) \
<input type="checkbox" /> Detect CPU speed \
<input type="checkbox" /> Interrupt / exception system (API) \
<input type="checkbox" /> Plan your memory map (virtual, and physical) : decide where you want the data to be. \
<input type="checkbox" /> 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" /> 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" /> Memory Management (MMU)\
<input type="checkbox" /> Preemptive multi tasking
<input type="checkbox" /> Processes
<input type="checkbox" /> Threads
<input type="checkbox" /> Scheduling (SRV2 Unix OR Priority Based Round Robin) \
<input type="checkbox" /> System V ABI compliance (partially)
<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" /> 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 \

0
docs/Intro.md Normal file
View File

0
docs/Manuals.md Normal file
View File

0
docs/ProjectStructure.md Normal file
View File

Binary file not shown.

BIN
docs/Reference Manuals/multiboot.pdf (Stored with Git LFS) Normal file

Binary file not shown.

BIN
docs/Reference Manuals/multiboot_2.0.pdf (Stored with Git LFS) Normal file

Binary file not shown.

BIN
images/BarinkOS.png (Stored with Git LFS) Normal file

Binary file not shown.

BIN
images/BarinkOS_logo(standard).svg (Stored with Git LFS) Normal file

Binary file not shown.

BIN
images/BarinkOS_logo.svg (Stored with Git LFS) Normal file

Binary file not shown.

0
screenshots/.blank Normal file
View File

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

Binary file not shown.

35
src/kernel/MMU.cpp Normal file
View File

@ -0,0 +1,35 @@
#include "MMU.h"
void MMU::enable(){
//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;
loadPageDirectory(this->page_directory);
enablePaging();
}

14
src/kernel/MMU.h Normal file
View File

@ -0,0 +1,14 @@
#pragma once
#include <stdint.h>
extern "C" void loadPageDirectory (long unsigned int* addr );
extern "C" void enablePaging();
class MMU {
public:
void enable ();
private:
uint32_t page_directory[1024] __attribute__((aligned(4096)));
uint32_t first_page_table[1024] __attribute__((aligned(4096)));
};

View File

@ -21,6 +21,32 @@ stack_bottom:
stack_top:
.text
.globl enablePaging
enablePaging:
push %ebp
mov %esp, %ebp
mov %cr0, %eax
or $0x80000000, %eax
mov %eax, %cr0
mov %ebp, %esp
pop %ebp
ret
.text
.globl loadPageDirectory
loadPageDirectory:
push %ebp
mov %esp, %ebp
mov 8(%esp), %eax
mov %eax, %cr3
mov %ebp, %esp
pop %ebp
ret
.section .text
.global _start
.type _start, @function

View File

@ -0,0 +1,19 @@
#include "serial.h"
Serial Serial::init() {
// No clue what to setup yet!
return Serial();
}
void Serial::print(){
// Do nothing!
}
Serial::Serial(){
// Do nothing!
}
Serial::~Serial(){
// Do nothing!
}

View File

@ -0,0 +1,19 @@
#pragma once
class Serial {
public:
static Serial init();
void print();
private:
const int COM1 = 0x3F8;
const int COM2 = 0x2F8;
const int COM3 = 0x3E8;
const int COM4 = 0x2E8;
Serial();
~Serial();
};

View File

@ -88,4 +88,5 @@ void kterm_write(const char* data, size_t size) {
void kterm_writestring(const char* data ){
AS_KERNEL();
kterm_write(data, strlen(data));
}
}

View File

@ -3,6 +3,7 @@
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
#include "../vga/colors.h"
void kterm_init();

59
src/kernel/io.cpp Normal file
View File

@ -0,0 +1,59 @@
#include "io.h"
unsigned char inb_p(unsigned short ){
}
unsigned short inw(unsigned short ){
}
unsigned short inw_p(unsigned short ){
}
unsigned int inl(unsigned short ){
}
unsigned int inl_p(unsigned short ){
}
void outb_p(unsigned char , unsigned short ){
}
void outw(unsigned short , unsigned short ){
}
void outw_p(unsigned short , unsigned short ){
}
void outl(unsigned int , unsigned short ){
}
void outl_p(unsigned int , unsigned short ){
}
void insb(unsigned short , void *,
unsigned long ){
}
void insw(unsigned short , void *,
unsigned long ){
}
void insl(unsigned short , void *,
unsigned long ){
}
void outsb(unsigned short , const void *,
unsigned long ){
}
void outsw(unsigned short , const void *,
unsigned long ){
}
void outsl(unsigned short , const void *,
unsigned long ){
}

43
src/kernel/io.h Normal file
View File

@ -0,0 +1,43 @@
#pragma once
#include <stdint.h>
static inline uint8_t inb(uint16_t port)
{
uint8_t ret;
asm volatile ( "inb %1, %0"
: "=a"(ret)
: "Nd"(port) );
return ret;
}
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);
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_p(unsigned int value, unsigned short port);
void insb(unsigned short port, void *addr,
unsigned long count);
void insw(unsigned short port, void *addr,
unsigned long count);
void insl(unsigned short port, void *addr,
unsigned long count);
void outsb(unsigned short port, const void *addr,
unsigned long count);
void outsw(unsigned short port, const void *addr,
unsigned long count);
void outsl(unsigned short port, const void *addr,
unsigned long count);

View File

@ -1,27 +0,0 @@
#include "kernel.h"
/**
* simple delay function
**/
void delay(int t){
volatile int i,j;
for(i=0;i<t;i++)
for(j=0;j<25000;j++)
asm("NOP");
}
void kernel_main (void) {
/** initialize terminal interface */
kterm_init();
/** Wrtite stuff to the screen to test the terminal**/
kterm_writestring("Hello world!\n");
kterm_writestring("We got newline support!\n");
for(;;){
delay(500);
kterm_writestring("We have implemented terminal scrolling!\n");
}
}

136
src/kernel/kernel.cpp Normal file
View File

@ -0,0 +1,136 @@
#include "kernel.h"
/**
* simple delay function
**/
void delay(int t){
volatile int i,j;
for(i=0;i<t;i++)
for(j=0;j<25000;j++)
asm("NOP");
}
class Test {
public:
Test();
void printMe();
~Test();
};
Test::Test(){
kterm_writestring("Create a test object\n");
};
void Test::printMe(){
kterm_writestring("testObject.printMe()\n");
}
Test::~Test(){
kterm_writestring("Destroy testObject! Bye bye\n");
}
#define PORT 0x3f8
static int init_serial() {
outb(PORT + 1, 0x00); // Disable all interrupts
outb(PORT + 3, 0x80); // Enable DLAB (set baud rate divisor)
outb(PORT + 0, 0x03); // Set divisor to 3 (lo byte) 38400 baud
outb(PORT + 1, 0x00); // (hi byte)
outb(PORT + 3, 0x03); // 8 bits, no parity, one stop bit
outb(PORT + 2, 0xC7); // Enable FIFO, clear them, with 14-byte threshold
outb(PORT + 4, 0x0B); // IRQs enabled, RTS/DSR set
outb(PORT + 4, 0x1E); // Set in loopback mode, test the serial chip
outb(PORT + 0, 0xAE); // Test serial chip (send byte 0xAE and check if serial returns same byte)
// Check if serial is faulty (i.e: not same byte as sent)
if(inb(PORT + 0) != 0xAE) {
return 1;
}
// If serial is not faulty set it in normal operation mode
// (not-loopback with IRQs enabled and OUT#1 and OUT#2 bits enabled)
outb(PORT + 4, 0x0F);
return 0;
}
int is_transmit_empty() {
return inb(PORT + 5) & 0x20;
}
void write_serial(char a) {
while (is_transmit_empty() == 0);
outb(PORT,a);
}
int serial_received() {
return inb(PORT + 5) & 1;
}
char read_serial() {
while (serial_received() == 0);
return inb(PORT);
}
void test_serial(){
/** Serial test **/
kterm_writestring("Writing to COM1 serial port:");
init_serial();
write_serial('A');
write_serial('B');
write_serial('C');
write_serial('D');
write_serial('E');
char Character_received = read_serial();
kterm_writestring("\n");
kterm_writestring("received from COM 1: \n");
kterm_put(Character_received);
kterm_writestring("\n");
}
extern "C" {
void kernel_main (void) {
/** initialize terminal interface */
kterm_init();
/** Wrtite stuff to the screen to test the terminal**/
kterm_writestring("Hello world!\n");
kterm_writestring("We got newline support!\n");
/** Test scrolling **/
for(int i=0; i < 5; i++){
delay(500);
kterm_writestring("We have implemented terminal scrolling!\n");
}
/** Test objective cpp **/
kterm_writestring("Testing C++ object support\n");
auto testObject = Test();
testObject.printMe();
/** Setup the MMU **/
kterm_writestring("Starting MMU...\n");
auto mmu = MMU();
mmu.enable();
kterm_writestring("MMU enabled!\n");
/** Lets start using the serial port for debugging .. **/
// Hopefully once we go into realmode or do something that
// cause the screen to go black.. this serial comms part will give
// some situational awareness
//Serial serialbus = Serial::init();
test_serial();
}
}

View File

@ -1,3 +1,7 @@
#pragma once
extern "C" {
#include "../libc/include/string.h"
#include "arch/i386/tty/kterm.h"
}
#include "MMU.h"
#include "io.h"