New directory structure
This commit is contained in:
44
src/kernel/arch/i386/boot.s
Normal file
44
src/kernel/arch/i386/boot.s
Normal file
@ -0,0 +1,44 @@
|
||||
/*
|
||||
* Multiboot
|
||||
*/
|
||||
.set ALIGN, 1<<0 /* align loaded modules on page boundaries */
|
||||
.set MEMINFO, 1<<1 /* provide memory map */
|
||||
.set FLAGS, ALIGN | MEMINFO /* this is the Multiboot 'flag' field */
|
||||
.set MAGIC, 0x1BADB002 /* 'magic number' lets bootloader find the header */
|
||||
.set CHECKSUM, -(MAGIC + FLAGS) /* checksum of above, to prove we are multiboot */
|
||||
|
||||
.section .multiboot
|
||||
.align 4
|
||||
.long MAGIC
|
||||
.long FLAGS
|
||||
.long CHECKSUM
|
||||
|
||||
|
||||
.section .bss
|
||||
.align 16
|
||||
stack_bottom:
|
||||
.skip 16384 # 16 KiB
|
||||
stack_top:
|
||||
|
||||
|
||||
.section .text
|
||||
.global _start
|
||||
.type _start, @function
|
||||
_start:
|
||||
|
||||
|
||||
|
||||
mov $stack_top, %esp
|
||||
|
||||
|
||||
|
||||
call _init
|
||||
call kernel_main
|
||||
|
||||
|
||||
cli
|
||||
1: hlt
|
||||
jmp 1b
|
||||
|
||||
|
||||
.size _start, . - _start
|
14
src/kernel/arch/i386/crti.s
Normal file
14
src/kernel/arch/i386/crti.s
Normal file
@ -0,0 +1,14 @@
|
||||
.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
|
7
src/kernel/arch/i386/crtn.s
Normal file
7
src/kernel/arch/i386/crtn.s
Normal file
@ -0,0 +1,7 @@
|
||||
.section .init
|
||||
popl %ebp
|
||||
ret
|
||||
|
||||
.section .fini
|
||||
popl %ebp
|
||||
ret
|
43
src/kernel/arch/i386/linker.ld
Normal file
43
src/kernel/arch/i386/linker.ld
Normal file
@ -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. */
|
||||
}
|
91
src/kernel/arch/i386/tty/kterm.c
Normal file
91
src/kernel/arch/i386/tty/kterm.c
Normal file
@ -0,0 +1,91 @@
|
||||
#include "kterm.h"
|
||||
static const size_t VGA_WIDTH = 80;
|
||||
static const size_t VGA_HEIGHT = 25;
|
||||
|
||||
size_t kterm_row;
|
||||
size_t kterm_column;
|
||||
uint8_t kterm_color;
|
||||
uint16_t* kterm_buffer;
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
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;
|
||||
kterm_buffer[index] = vga_entry(' ', kterm_color);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void kterm_resetcolor(){
|
||||
kterm_color = vga_entry_color ( VGA_COLOR_LIGHT_GREY , VGA_COLOR_BLACK);
|
||||
}
|
||||
|
||||
void kterm_setcolor(uint8_t color){
|
||||
kterm_color = 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);
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* With the help from:
|
||||
* https://whiteheadsoftware.dev/operating-systems-development-for-dummies/
|
||||
**/
|
||||
void kterm_scrollup(){
|
||||
size_t i ;
|
||||
for(i=0; i < (VGA_WIDTH * VGA_HEIGHT - VGA_WIDTH); i++)
|
||||
kterm_buffer[i] = kterm_buffer[i+VGA_WIDTH];
|
||||
|
||||
for( i=0; i< VGA_WIDTH; i++)
|
||||
kterm_buffer[(VGA_HEIGHT -1) * VGA_WIDTH + i ] = vga_entry(' ', kterm_color);
|
||||
|
||||
}
|
||||
|
||||
void kterm_put (char c) {
|
||||
|
||||
if(++kterm_column == VGA_WIDTH || c == '\n' ) {
|
||||
kterm_column = 0;
|
||||
if(kterm_row == VGA_HEIGHT-1) {
|
||||
kterm_scrollup();
|
||||
} else {
|
||||
kterm_row ++;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if(c == '\n') return;
|
||||
kterm_putat ( c, kterm_color, kterm_column, kterm_row);
|
||||
|
||||
|
||||
}
|
||||
|
||||
void kterm_write(const char* data, size_t size) {
|
||||
for(size_t i = 0; i < size; i++){
|
||||
kterm_put(data[i]);
|
||||
}
|
||||
}
|
||||
|
||||
void kterm_writestring(const char* data ){
|
||||
AS_KERNEL();
|
||||
kterm_write(data, strlen(data));
|
||||
}
|
22
src/kernel/arch/i386/tty/kterm.h
Normal file
22
src/kernel/arch/i386/tty/kterm.h
Normal file
@ -0,0 +1,22 @@
|
||||
#pragma once
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include "../vga/colors.h"
|
||||
|
||||
void kterm_init();
|
||||
|
||||
void kterm_resetcolor();
|
||||
void kterm_setcolor(uint8_t);
|
||||
|
||||
void kterm_putat(char, uint8_t, size_t, size_t);
|
||||
void kterm_put(char);
|
||||
void kterm_write(const char*, size_t);
|
||||
void kterm_writestring(const char*);
|
||||
void kterm_scrollup();
|
||||
|
||||
#define KernelTag "[Kernel]: "
|
||||
#define AS_KERNEL() ( kterm_setcolor(VGA_COLOR_LIGHT_BLUE),\
|
||||
kterm_write(KernelTag, 10 ), \
|
||||
kterm_resetcolor())
|
19
src/kernel/arch/i386/vga/colors.h
Normal file
19
src/kernel/arch/i386/vga/colors.h
Normal file
@ -0,0 +1,19 @@
|
||||
#pragma once
|
||||
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,
|
||||
};
|
27
src/kernel/kernel.c
Normal file
27
src/kernel/kernel.c
Normal file
@ -0,0 +1,27 @@
|
||||
#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");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
3
src/kernel/kernel.h
Normal file
3
src/kernel/kernel.h
Normal file
@ -0,0 +1,3 @@
|
||||
#pragma once
|
||||
#include "../libc/include/string.h"
|
||||
#include "arch/i386/tty/kterm.h"
|
Reference in New Issue
Block a user