mirror of
https://github.com/iritmaximus/os.git
synced 2026-10-12 19:13:36 +03:00
initial commit
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
*.bin
|
||||
*.o
|
||||
*.d
|
||||
*.a
|
||||
*.swp
|
||||
*.iso
|
||||
|
||||
sysroot
|
||||
isodir
|
||||
@@ -0,0 +1,14 @@
|
||||
# OS-dev
|
||||
|
||||
This will be the start of some OS developed by me :D
|
||||
Progress will be slow and this will be a project that
|
||||
will get periodic bursts, not continuos dev work.
|
||||
|
||||
## End-game
|
||||
|
||||
The end-goal would be to run GNU-software on the OS, but I doupt it will get to
|
||||
that point. Another goal would be to get it to run on real hardware.
|
||||
|
||||
# File-structure
|
||||
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
#!/bin/bash
|
||||
set -e
|
||||
. ./config.sh
|
||||
. ./headers.sh
|
||||
|
||||
for PROJECT in $PROJECTS; do
|
||||
(cd $PROJECT && DESTDIR="$SYSROOT" make install)
|
||||
done
|
||||
@@ -0,0 +1,11 @@
|
||||
#!/bin/bash
|
||||
set -e
|
||||
. ./config.sh
|
||||
|
||||
rm -f *.iso
|
||||
rm -rf isodir
|
||||
rm -rf sysroot
|
||||
|
||||
for PROJECT in $PROJECTS; do
|
||||
(cd $PROJECT && make clean)
|
||||
done
|
||||
@@ -0,0 +1,26 @@
|
||||
SYSTEM_HEADER_PROJECTS="libc kernel"
|
||||
PROJECTS="libc kernel"
|
||||
|
||||
export HOST=i686-elf
|
||||
|
||||
export AR=${HOST}-ar
|
||||
export AS=${HOST}-as
|
||||
export CC=${HOST}-gcc
|
||||
|
||||
export PREFIX=/usr
|
||||
export EXEC_PREFIX=$PREFIX
|
||||
export BOOTDIR=/boot
|
||||
export LIBDIR=$EXEC_PREFIX/lib
|
||||
export INCLUDEDIR=$PREFIX/include
|
||||
|
||||
export CFLAGS="-O2 -g"
|
||||
export CPPFLAGS=""
|
||||
|
||||
export SYSROOT="$(pwd)/sysroot"
|
||||
export CC="$CC --sysroot=$SYSROOT"
|
||||
|
||||
export DESTDIR=$SYSROOT
|
||||
|
||||
if echo "$HOST" | grep -Eq -- '-elf($|-)'; then
|
||||
export CC="$CC -isystem=$INCLUDEDIR"
|
||||
fi
|
||||
@@ -0,0 +1,7 @@
|
||||
#!/bin/bash
|
||||
set -e
|
||||
. ./config.sh
|
||||
|
||||
for PROJECT in $SYSTEM_HEADER_PROJECTS; do
|
||||
(cd $PROJECT && DESTDIR="$SYSROOT" make install-headers);
|
||||
done
|
||||
@@ -0,0 +1,13 @@
|
||||
#!/bin/bash
|
||||
set -e
|
||||
. ./build.sh
|
||||
|
||||
mkdir -p isodir/boot/grub
|
||||
|
||||
cp sysroot/boot/deros.kernel isodir/boot/deros.kernel
|
||||
cat > isodir/boot/grub/grub.cfg << EOF
|
||||
menuentry "deros" {
|
||||
multiboot /boot/deros.kernel
|
||||
}
|
||||
EOF
|
||||
grub-mkrescue -o deros.iso isodir
|
||||
@@ -0,0 +1,77 @@
|
||||
CFLAGS?=-O2 -g
|
||||
CPPFLAGS?=
|
||||
LDFLAGS?=
|
||||
LIBS?=
|
||||
|
||||
DESTDIR?=
|
||||
PREFIX?=/usr/local
|
||||
EXEC_PREFIX?=$(PREFIX)
|
||||
BOOTDIR?=$(PREFIX)
|
||||
INCLUDEDIR?=$(PREFIX)/include
|
||||
|
||||
CFLAGS:=$(CFLAGS) -ffreestanding -Wall -Wextra
|
||||
CPPFLAGS:=$(CPPFLAGS) -D__is_kernel -Iinclude
|
||||
LDFLAGS:=$(LDFLAGS)
|
||||
LIBS:=$(LIBS) -nostdlib -lk -lgcc
|
||||
|
||||
HOSTARCH?=i386
|
||||
ARCHDIR=arch/$(HOSTARCH)
|
||||
|
||||
KERNEL_ARCH_OBJS=\
|
||||
$(ARCHDIR)/boot.o \
|
||||
$(ARCHDIR)/page_enable.o \
|
||||
$(ARCHDIR)/paging.o \
|
||||
$(ARCHDIR)/tty.o \
|
||||
|
||||
KERNEL_OBJS=\
|
||||
$(KERNEL_ARCH_OBJS) \
|
||||
kernel/kernel.o \
|
||||
|
||||
OBJS=\
|
||||
$(ARCHDIR)/crti.o \
|
||||
$(ARCHDIR)/crtbegin.o \
|
||||
$(KERNEL_OBJS) \
|
||||
$(ARCHDIR)/crtend.o \
|
||||
$(ARCHDIR)/crtn.o \
|
||||
|
||||
LINK_LIST=\
|
||||
$(LDFLAGS) \
|
||||
$(ARCHDIR)/crti.o \
|
||||
$(ARCHDIR)/crtbegin.o \
|
||||
$(KERNEL_OBJS) \
|
||||
$(LIBS) \
|
||||
$(ARCHDIR)/crtend.o \
|
||||
$(ARCHDIR)/crtn.o \
|
||||
|
||||
.PHONY: all clean install install-headers install-kernel
|
||||
.SUFFIXES: .o .c .s
|
||||
|
||||
all: deros.kernel
|
||||
|
||||
deros.kernel: $(OBJS) $(ARCHDIR)/linker.ld
|
||||
$(CC) -T $(ARCHDIR)/linker.ld -o $@ $(CFLAGS) $(LINK_LIST)
|
||||
grub-file --is-x86-multiboot deros.kernel
|
||||
|
||||
$(ARCHDIR)/crtbegin.o $(ARCHDIR)/crtend.o:
|
||||
OBJ=`$(CC) $(CFLAGS) $(LDFLAGS) -print-file-name=$(@F)` && cp "$$OBJ" $@
|
||||
|
||||
.s.o:
|
||||
$(AS) $< -o $@
|
||||
|
||||
.c.o:
|
||||
$(CC) -MD -c $< -o $@ $(CFLAGS) $(CPPFLAGS)
|
||||
|
||||
clean:
|
||||
rm -f deros.kernel
|
||||
rm -f $(ARCHDIR)/*.{o,d}
|
||||
rm -f kernel/*.{o,d}
|
||||
|
||||
install: install-headers install-kernel
|
||||
|
||||
install-headers:
|
||||
mkdir -p $(DESTDIR)$(INCLUDEDIR)
|
||||
cp -r include/. $(DESTDIR)$(INCLUDEDIR)/.
|
||||
|
||||
install-kernel: deros.kernel
|
||||
mkdir -p $(DESTDIR)$(BOOTDIR)
|
||||
cp deros.kernel $(DESTDIR)$(BOOTDIR)
|
||||
@@ -0,0 +1,109 @@
|
||||
/* Declare constants for the multiboot header. */
|
||||
.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 */
|
||||
|
||||
/*
|
||||
Declare a multiboot header that marks the program as a kernel. These are magic
|
||||
values that are documented in the multiboot standard. The bootloader will
|
||||
search for this signature in the first 8 KiB of the kernel file, aligned at a
|
||||
32-bit boundary. The signature is in its own section so the header can be
|
||||
forced to be within the first 8 KiB of the kernel file.
|
||||
*/
|
||||
.section .multiboot
|
||||
.align 4
|
||||
.long MAGIC
|
||||
.long FLAGS
|
||||
.long CHECKSUM
|
||||
|
||||
/*
|
||||
The multiboot standard does not define the value of the stack pointer register
|
||||
(esp) and it is up to the kernel to provide a stack. This allocates room for a
|
||||
small stack by creating a symbol at the bottom of it, then allocating 16384
|
||||
bytes for it, and finally creating a symbol at the top. The stack grows
|
||||
downwards on x86. The stack is in its own section so it can be marked nobits,
|
||||
which means the kernel file is smaller because it does not contain an
|
||||
uninitialized stack. The stack on x86 must be 16-byte aligned according to the
|
||||
System V ABI standard and de-facto extensions. The compiler will assume the
|
||||
stack is properly aligned and failure to align the stack will result in
|
||||
undefined behavior.
|
||||
*/
|
||||
.section .bss
|
||||
.align 16
|
||||
stack_bottom:
|
||||
.skip 16384 # 16 KiB
|
||||
stack_top:
|
||||
|
||||
/*
|
||||
The linker script specifies _start as the entry point to the kernel and the
|
||||
bootloader will jump to this position once the kernel has been loaded. It
|
||||
doesn't make sense to return from this function as the bootloader is gone.
|
||||
*/
|
||||
.section .text
|
||||
.global _start
|
||||
.type _start, @function
|
||||
_start:
|
||||
/*
|
||||
The bootloader has loaded us into 32-bit protected mode on a x86
|
||||
machine. Interrupts are disabled. Paging is disabled. The processor
|
||||
state is as defined in the multiboot standard. The kernel has full
|
||||
control of the CPU. The kernel can only make use of hardware features
|
||||
and any code it provides as part of itself. There's no printf
|
||||
function, unless the kernel provides its own <stdio.h> header and a
|
||||
printf implementation. There are no security restrictions, no
|
||||
safeguards, no debugging mechanisms, only what the kernel provides
|
||||
itself. It has absolute and complete power over the
|
||||
machine.
|
||||
*/
|
||||
|
||||
/*
|
||||
To set up a stack, we set the esp register to point to the top of the
|
||||
stack (as it grows downwards on x86 systems). This is necessarily done
|
||||
in assembly as languages such as C cannot function without a stack.
|
||||
*/
|
||||
mov $stack_top, %esp
|
||||
|
||||
/*
|
||||
This is a good place to initialize crucial processor state before the
|
||||
high-level kernel is entered. It's best to minimize the early
|
||||
environment where crucial features are offline. Note that the
|
||||
processor is not fully initialized yet: Features such as floating
|
||||
point instructions and instruction set extensions are not initialized
|
||||
yet. The GDT should be loaded here. Paging should be enabled here.
|
||||
C++ features such as global constructors and exceptions will require
|
||||
runtime support to work as well.
|
||||
*/
|
||||
|
||||
/*
|
||||
Enter the high-level kernel. The ABI requires the stack is 16-byte
|
||||
aligned at the time of the call instruction (which afterwards pushes
|
||||
the return pointer of size 4 bytes). The stack was originally 16-byte
|
||||
aligned above and we've pushed a multiple of 16 bytes to the
|
||||
stack since (pushed 0 bytes so far), so the alignment has thus been
|
||||
preserved and the call is well defined.
|
||||
*/
|
||||
call kernel_main
|
||||
|
||||
/*
|
||||
If the system has nothing more to do, put the computer into an
|
||||
infinite loop. To do that:
|
||||
1) Disable interrupts with cli (clear interrupt enable in eflags).
|
||||
They are already disabled by the bootloader, so this is not needed.
|
||||
Mind that you might later enable interrupts and return from
|
||||
kernel_main (which is sort of nonsensical to do).
|
||||
2) Wait for the next interrupt to arrive with hlt (halt instruction).
|
||||
Since they are disabled, this will lock up the computer.
|
||||
3) Jump to the hlt instruction if it ever wakes up due to a
|
||||
non-maskable interrupt occurring or due to system management mode.
|
||||
*/
|
||||
cli
|
||||
1: hlt
|
||||
jmp 1b
|
||||
|
||||
/*
|
||||
Set the size of the _start symbol to the current location '.' minus its start.
|
||||
This is useful when debugging or when you implement call tracing.
|
||||
*/
|
||||
.size _start, . - _start
|
||||
@@ -0,0 +1,17 @@
|
||||
/* x86 crti.s (32-bit) */
|
||||
|
||||
.section .init
|
||||
.global _init
|
||||
.type _init, @function
|
||||
_init:
|
||||
push %ebp
|
||||
movl %esp, %ebp
|
||||
/* gcc will nicely put the contents of crtbegin.o's .init section here. */
|
||||
|
||||
.section .fini
|
||||
.global _fini
|
||||
.type _fini, @function
|
||||
_fini:
|
||||
push %ebp
|
||||
movl %esp, %ebp
|
||||
/* gcc will nicely put the contents of crtbegin.o's .fini section here. */
|
||||
@@ -0,0 +1,10 @@
|
||||
/* x86 crtn.s */
|
||||
.section .init
|
||||
/* gcc will nicely put the contents of crtend.o's .init section here. */
|
||||
popl %ebp
|
||||
ret
|
||||
|
||||
.section .fini
|
||||
/* gcc will nicely put the contents of crtend.o's .fini section here. */
|
||||
popl %ebp
|
||||
ret
|
||||
@@ -0,0 +1,43 @@
|
||||
/* The bootloader will look at this image and start execution at the symbol
|
||||
designated as the entry point. */
|
||||
ENTRY(_start)
|
||||
|
||||
/* Tell where the various sections of the object files will be put in the final
|
||||
kernel image. */
|
||||
SECTIONS
|
||||
{
|
||||
/* Begin putting sections at 1 MiB, a conventional place for kernels to be
|
||||
loaded at by the bootloader. */
|
||||
. = 1M;
|
||||
|
||||
/* First put the multiboot header, as it is required to be put very early
|
||||
early in the image or the bootloader won't recognize the file format.
|
||||
Next we'll put the .text section. */
|
||||
.text BLOCK(4K) : ALIGN(4K)
|
||||
{
|
||||
*(.multiboot)
|
||||
*(.text)
|
||||
}
|
||||
|
||||
/* Read-only data. */
|
||||
.rodata BLOCK(4K) : ALIGN(4K)
|
||||
{
|
||||
*(.rodata)
|
||||
}
|
||||
|
||||
/* Read-write data (initialized) */
|
||||
.data BLOCK(4K) : ALIGN(4K)
|
||||
{
|
||||
*(.data)
|
||||
}
|
||||
|
||||
/* Read-write data (uninitialized) and stack */
|
||||
.bss BLOCK(4K) : ALIGN(4K)
|
||||
{
|
||||
*(COMMON)
|
||||
*(.bss)
|
||||
}
|
||||
|
||||
/* The compiler may produce other sections, by default it will put them in
|
||||
a segment with the same name. Simply add stuff here as needed. */
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
KERNEL_ARCH_OBJS=\
|
||||
$(ARCHDIR)/boot.o \
|
||||
$(ARCHDIR)/tty.o \
|
||||
@@ -0,0 +1,17 @@
|
||||
.text
|
||||
.global _enablePaging
|
||||
.type _enablePaging, @function
|
||||
_enablePaging:
|
||||
push %ebp
|
||||
mov %esp, %ebp
|
||||
|
||||
mov 8(%esp), %eax
|
||||
mov %eax, %cr3
|
||||
|
||||
mov %cr0, %eax
|
||||
or $0x80000000, %eax
|
||||
mov %eax, %cr0
|
||||
|
||||
mov %ebp, %esp
|
||||
pop %ebp
|
||||
ret
|
||||
@@ -0,0 +1,38 @@
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
|
||||
|
||||
extern void enablePaging(unsigned int*);
|
||||
|
||||
uint32_t page_directory[1024] __attribute__((aligned(4096)));
|
||||
|
||||
|
||||
void init_paging(void)
|
||||
{
|
||||
|
||||
//set each entry to not present
|
||||
for (int 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
|
||||
page_directory[i] = 0x00000002;
|
||||
}
|
||||
|
||||
uint32_t first_page_table[1024] __attribute__((aligned(4096)));
|
||||
|
||||
// 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(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
|
||||
page_directory[0] = ((unsigned int)first_page_table) | 3;
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
#include <string.h>
|
||||
|
||||
#include <kernel/tty.h>
|
||||
#include "vga.h"
|
||||
|
||||
static const size_t VGA_WIDTH = 80;
|
||||
static const size_t VGA_HEIGHT = 25;
|
||||
static uint16_t* const VGA_MEMORY = (uint16_t*) 0xB8000;
|
||||
|
||||
static size_t terminal_row;
|
||||
static size_t terminal_column;
|
||||
static uint8_t terminal_color;
|
||||
static uint16_t* terminal_buffer = VGA_MEMORY;
|
||||
|
||||
|
||||
void terminal_setcolor(uint8_t color)
|
||||
{
|
||||
terminal_color = color;
|
||||
}
|
||||
|
||||
void terminal_putentryat(char c, uint8_t color, size_t x, size_t y)
|
||||
{
|
||||
const size_t index = y * VGA_WIDTH + x;
|
||||
terminal_buffer[index] = vga_entry(c, color);
|
||||
}
|
||||
|
||||
void terminal_initialize(void)
|
||||
{
|
||||
terminal_row = 0;
|
||||
terminal_column = 0;
|
||||
terminal_color = vga_entry_color(VGA_COLOR_LIGHT_GREY, VGA_COLOR_BLACK);
|
||||
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;
|
||||
terminal_buffer[index] = vga_entry(' ', terminal_color);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void clear_line(size_t line)
|
||||
{
|
||||
char c = ' ';
|
||||
for (size_t column = 0; column < VGA_WIDTH; column++)
|
||||
terminal_putentryat(c, terminal_color, column, line);
|
||||
}
|
||||
|
||||
void terminal_scroll(void)
|
||||
{
|
||||
// starting from the second row, iterate through the chars in the buffer and add them
|
||||
// one row "above"
|
||||
for (size_t read_height = 1; read_height < VGA_HEIGHT; read_height++) {
|
||||
clear_line(read_height - 1);
|
||||
for (size_t read_width = 0; read_width < VGA_WIDTH; read_width++) {
|
||||
// index to read the char from
|
||||
size_t read_index = read_height * VGA_WIDTH + read_width;
|
||||
size_t input_index = (read_height - 1) * VGA_WIDTH + read_width;
|
||||
|
||||
terminal_buffer[input_index] = terminal_buffer[read_index];
|
||||
}
|
||||
}
|
||||
|
||||
clear_line(VGA_HEIGHT - 1);
|
||||
terminal_column = -1;
|
||||
terminal_row = VGA_HEIGHT - 1;
|
||||
}
|
||||
|
||||
void terminal_putchar(char c)
|
||||
{
|
||||
if (terminal_row == VGA_HEIGHT)
|
||||
terminal_scroll();
|
||||
|
||||
// checks for newlines and moves the "cursor" to the beginning of next line
|
||||
if (c == '\n') {
|
||||
terminal_column = 0;
|
||||
terminal_row++;
|
||||
return;
|
||||
}
|
||||
|
||||
terminal_putentryat(c, terminal_color, terminal_column, terminal_row);
|
||||
if (++terminal_column > VGA_WIDTH) {
|
||||
terminal_column = 0;
|
||||
terminal_row++;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
void terminal_write(const char* data, size_t size)
|
||||
{
|
||||
for (size_t i = 0; i < size; i++)
|
||||
terminal_putchar(data[i]);
|
||||
}
|
||||
|
||||
void terminal_writestring(const char* data)
|
||||
{
|
||||
terminal_write(data, strlen(data));
|
||||
}
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
#ifndef ARCH_I386_VGA_H
|
||||
#define ARCH_I386_VGA_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
/* Hardware text mode color constants. */
|
||||
enum vga_color {
|
||||
VGA_COLOR_BLACK = 0,
|
||||
VGA_COLOR_BLUE = 1,
|
||||
VGA_COLOR_GREEN = 2,
|
||||
VGA_COLOR_CYAN = 3,
|
||||
VGA_COLOR_RED = 4,
|
||||
VGA_COLOR_MAGENTA = 5,
|
||||
VGA_COLOR_BROWN = 6,
|
||||
VGA_COLOR_LIGHT_GREY = 7,
|
||||
VGA_COLOR_DARK_GREY = 8,
|
||||
VGA_COLOR_LIGHT_BLUE = 9,
|
||||
VGA_COLOR_LIGHT_GREEN = 10,
|
||||
VGA_COLOR_LIGHT_CYAN = 11,
|
||||
VGA_COLOR_LIGHT_RED = 12,
|
||||
VGA_COLOR_LIGHT_MAGENTA = 13,
|
||||
VGA_COLOR_LIGHT_BROWN = 14,
|
||||
VGA_COLOR_WHITE = 15,
|
||||
};
|
||||
|
||||
static inline uint8_t vga_entry_color(enum vga_color fg, enum vga_color bg)
|
||||
{
|
||||
return fg | bg << 4;
|
||||
}
|
||||
|
||||
static inline uint16_t vga_entry(unsigned char uc, uint8_t color)
|
||||
{
|
||||
return (uint16_t) uc | (uint16_t) color << 8;
|
||||
}
|
||||
|
||||
#endif
|
||||
Binary file not shown.
@@ -0,0 +1,8 @@
|
||||
#ifndef _PAGING_H
|
||||
#define _PAGING_H 1
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
void init_paging(void);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,17 @@
|
||||
#ifndef _TTY_H
|
||||
#define _TTY_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
|
||||
|
||||
void terminal_setcolor(uint8_t color);
|
||||
void terminal_putentryat(char c, uint8_t color, size_t x, size_t y);
|
||||
void clear_line(size_t line);
|
||||
void terminal_scroll(void);
|
||||
void terminal_putchar(char c);
|
||||
void terminal_write(const char* data, size_t size);
|
||||
void terminal_writestring(const char* data);
|
||||
void terminal_initialize(void);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,38 @@
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include <kernel/tty.h>
|
||||
#include <kernel/paging.h>
|
||||
|
||||
|
||||
/* Check if the compiler thinks you are targeting the wrong operating system. */
|
||||
#if defined(__linux__)
|
||||
#error "You are not using a cross-compiler, you will most certainly run into trouble"
|
||||
#endif
|
||||
|
||||
/* This tutorial will only work for the 32-bit ix86 targets. */
|
||||
#if !defined(__i386__)
|
||||
#error "This tutorial needs to be compiled with a ix86-elf compiler"
|
||||
#endif
|
||||
|
||||
|
||||
void kernel_main(void)
|
||||
{
|
||||
/* Initialize terminal interface */
|
||||
terminal_initialize();
|
||||
printf("Initializing kernel...\n");
|
||||
|
||||
/* Initializing paging for the os */
|
||||
init_paging();
|
||||
printf("* Paging initialized\n");
|
||||
|
||||
printf("\n");
|
||||
printf("Trying things out...\n");
|
||||
printf("Testing char: %c\n", 'b');
|
||||
printf("Testing string: %s\n", "hello");
|
||||
printf("Testing hex: %x\n", 10);
|
||||
printf("Testing int: %d\n", 10);
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
|
||||
|
||||
extern void enablePaging(unsigned int*);
|
||||
|
||||
void init_paging(void)
|
||||
{
|
||||
uint32_t page_directory[1024] __attribute__((aligned(4096)));
|
||||
|
||||
//set each entry to not present
|
||||
for (int 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
|
||||
page_directory[i] = 0x00000002;
|
||||
}
|
||||
|
||||
uint32_t first_page_table[1024] __attribute__((aligned(4096)));
|
||||
|
||||
// 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(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
|
||||
page_directory[0] = ((unsigned int)first_page_table) | 3;
|
||||
|
||||
printf("HERE WE ARE\n");
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
CFLAGS?=-O2 -g
|
||||
CPPFLAGS?=
|
||||
LDFLAGS?=
|
||||
LIBS?=
|
||||
|
||||
DESTDIR?=
|
||||
PREFIX?=/usr/local
|
||||
EXEC_PREFIX?=$(PREFIX)
|
||||
BOOTDIR?=$(PREFIX)
|
||||
INCLUDEDIR?=$(PREFIX)/include
|
||||
|
||||
CFLAGS:=$(CFLAGS) -ffreestanding -Wall -Wextra
|
||||
CPPFLAGS:=$(CPPFLAGS) -D__is_kernel -Iinclude
|
||||
LIBK_CFLAGS:=$(CFLAGS)
|
||||
LIBK_CPPFLAGS:=$(CPPFLAGS) -D__is_libk
|
||||
|
||||
HOSTARCH?=i386
|
||||
ARCHDIR=arch/$(HOSTARCH)
|
||||
|
||||
|
||||
FREEOBJS=\
|
||||
stdio/printf.o \
|
||||
stdio/putchar.o \
|
||||
stdio/puts.o \
|
||||
stdlib/itoa.o \
|
||||
string/strlen.o \
|
||||
|
||||
ARCH_HOSTEDOBJS=
|
||||
HOSTEDOBJS=\
|
||||
$(ARCH_HOSTEDOBJS) \
|
||||
|
||||
OBJS=\
|
||||
$(FREEOBJS) \
|
||||
$(HOSTEDOBJS) \
|
||||
|
||||
LIBK_OBJS=$(FREEOBJS:.o=.libk.o)
|
||||
|
||||
#BINARIES=libc.a libk.a # later...
|
||||
BINARIES=libk.a
|
||||
|
||||
.PHONY: all install-headers install-libs clean
|
||||
.SUFFIXES: .o .libk.o .c .s
|
||||
|
||||
all: $(BINARIES)
|
||||
|
||||
libk.a: $(LIBK_OBJS)
|
||||
$(AR) rcs $@ $(LIBK_OBJS)
|
||||
|
||||
libc.a: $(OBJS)
|
||||
$(AR) rcs $@ $(OBJS)
|
||||
|
||||
.c.o:
|
||||
$(CC) -MD -c $< -o $@ -std=gnu11 $(CFLAGS) $(CPPFLAGS)
|
||||
|
||||
.s.o:
|
||||
$(AS) $< -o $@
|
||||
|
||||
.c.libk.o:
|
||||
$(CC) -MD -c $< -o $@ -std=gnu11 $(LIBK_CFLAGS) $(LIBK_CPPFLAGS)
|
||||
|
||||
.s.libc.o:
|
||||
$(CC) -MD -c $< -o $@ -std=gnu11 $(LIBK_CFLAGS) $(LIBK_CPPFLAGS)
|
||||
|
||||
install: install-headers install-libs
|
||||
|
||||
install-headers:
|
||||
mkdir -p $(DESTDIR)$(INCLUDEDIR)
|
||||
cp -r include/. $(DESTDIR)$(INCLUDEDIR)/.
|
||||
|
||||
install-libs: $(BINARIES)
|
||||
mkdir -p $(DESTDIR)$(LIBDIR)
|
||||
cp $(BINARIES) $(DESTDIR)$(LIBDIR)
|
||||
|
||||
clean:
|
||||
rm -f string/*.o
|
||||
rm -f *.a
|
||||
@@ -0,0 +1,10 @@
|
||||
#ifndef _STDIO_H
|
||||
#define _STDIO_H 1
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
int printf(const char* format, ...);
|
||||
int putchar(int);
|
||||
int puts(const char*);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,6 @@
|
||||
#ifndef _STDLIB_H
|
||||
#define _STDLIB_H 1
|
||||
|
||||
char* itoa(int, char*, int);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,8 @@
|
||||
#ifndef _STRING_H
|
||||
#define _STRING_H 1
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
size_t strlen(const char* str);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,64 @@
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
// TODO make this conforming to proper libc func
|
||||
void print(const char* str)
|
||||
{
|
||||
while (*str != '\0')
|
||||
{
|
||||
putchar(*str);
|
||||
str++;
|
||||
}
|
||||
}
|
||||
|
||||
int printf(const char* restrict format, ...)
|
||||
{
|
||||
va_list arg_list;
|
||||
va_start(arg_list, format);
|
||||
|
||||
|
||||
while(*format != '\0')
|
||||
{
|
||||
if (*format == '%') {
|
||||
format++;
|
||||
switch(*format)
|
||||
{
|
||||
case 'c':
|
||||
char c = (char) va_arg(arg_list, int);
|
||||
putchar(c);
|
||||
format++;
|
||||
break;
|
||||
case 's':
|
||||
char* str = va_arg(arg_list, char*);
|
||||
format++;
|
||||
print(str);
|
||||
break;
|
||||
case 'x':
|
||||
char* hex_str[33];
|
||||
int hex_value = va_arg(arg_list, int);
|
||||
itoa(hex_value, *hex_str, 16);
|
||||
print(*hex_str);
|
||||
format++;
|
||||
break;
|
||||
default:
|
||||
char* int_str[33];
|
||||
int int_value = va_arg(arg_list, int);
|
||||
itoa(int_value, *int_str, 2);
|
||||
print(*int_str);
|
||||
format++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
putchar(*format);
|
||||
format++;
|
||||
continue;
|
||||
}
|
||||
|
||||
}
|
||||
va_end(arg_list);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
#include <stdio.h>
|
||||
|
||||
#if defined(__is_libk)
|
||||
#include <kernel/tty.h>
|
||||
#endif
|
||||
|
||||
int putchar(int ic)
|
||||
{
|
||||
#if defined(__is_libk)
|
||||
char c = (char) ic;
|
||||
terminal_write(&c, sizeof(c));
|
||||
#else
|
||||
|
||||
#endif
|
||||
return ic;
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int puts(const char* str)
|
||||
{
|
||||
while (*str != '\0') {
|
||||
putchar(*str);
|
||||
str++;
|
||||
}
|
||||
putchar('\n');
|
||||
return (int) str;
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
#include <stdio.h>
|
||||
|
||||
char* itoa(int value, char* str, int base)
|
||||
{
|
||||
// check for incorrect base
|
||||
if (base < 2 || base > 36)
|
||||
{
|
||||
printf("\n ERROR: Incorrect base\n");
|
||||
return '\0';
|
||||
}
|
||||
|
||||
char num[] = "0123456789abcdefghijklmnopqrstuvwxyz";
|
||||
|
||||
while (value/base > 1)
|
||||
{
|
||||
// TODO does this need a check for str_len?
|
||||
*str = num[value % base];
|
||||
str++;
|
||||
value /= base;
|
||||
}
|
||||
|
||||
return str;
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
#include <string.h>
|
||||
|
||||
size_t strlen(const char* str)
|
||||
{
|
||||
size_t len = 0;
|
||||
while (str[len])
|
||||
len++;
|
||||
return len;
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
#!/bin/bash
|
||||
set -e
|
||||
. ./iso.sh
|
||||
|
||||
qemu-system-i386 -cdrom deros.iso
|
||||
@@ -0,0 +1,5 @@
|
||||
#!/bin/bash
|
||||
set -e
|
||||
. ./config.sh
|
||||
|
||||
cd kernel && DESTDIR="$SYSROOT" make $1
|
||||
@@ -0,0 +1,5 @@
|
||||
#!/bin/bash
|
||||
set -e
|
||||
. ./config.sh
|
||||
|
||||
cd libc && DESTDIR="$SYSROOT" make $1
|
||||
Reference in New Issue
Block a user