reimplmenets the previous iteration with a brand new custom build system
This commit is contained in:
30
kernel/arch/boot.s
Normal file
30
kernel/arch/boot.s
Normal file
@ -0,0 +1,30 @@
|
||||
global loader ; entry symbol for ELF
|
||||
extern kmain
|
||||
|
||||
MAGIC_NUMBER equ 0x1BADB002 ; magic number constant
|
||||
FLAGS equ 0x3
|
||||
CHECKSUM equ -(MAGIC_NUMBER + FLAGS) ; calculate the checksum
|
||||
KERNEL_STACK_SIZE equ 4096
|
||||
|
||||
section .multiboot
|
||||
align 4 ; code must be 4 byte aligned
|
||||
dd MAGIC_NUMBER
|
||||
dd FLAGS
|
||||
dd CHECKSUM
|
||||
|
||||
section .bss
|
||||
align 4
|
||||
kernel_stack:
|
||||
resb KERNEL_STACK_SIZE
|
||||
|
||||
section .text
|
||||
loader:
|
||||
mov esp, kernel_stack + KERNEL_STACK_SIZE
|
||||
|
||||
;mov eax, 0xCAFEBABE
|
||||
|
||||
call kmain
|
||||
|
||||
cli
|
||||
loop: hlt
|
||||
jmp loop
|
19
kernel/arch/gdt_entry.c
Normal file
19
kernel/arch/gdt_entry.c
Normal file
@ -0,0 +1,19 @@
|
||||
#include <kernel/x86/gdt.h>
|
||||
|
||||
uint64_t create_descriptor(uint32_t base, uint32_t limit, uint16_t flag)
|
||||
{
|
||||
uint64_t descriptor;
|
||||
|
||||
descriptor = limit & 0x000F0000;
|
||||
descriptor |= (flag << 8) & 0x00F0FF00;
|
||||
descriptor |= (base >> 16) & 0x000000FF;
|
||||
descriptor |= base & 0xFF000000;
|
||||
|
||||
descriptor <<= 32;
|
||||
|
||||
descriptor |= base << 16;
|
||||
descriptor |= limit & 0x0000FFFF;
|
||||
|
||||
return descriptor;
|
||||
}
|
||||
|
24
kernel/arch/io.h
Normal file
24
kernel/arch/io.h
Normal file
@ -0,0 +1,24 @@
|
||||
#ifndef ARCH_IO_H
|
||||
#define ARCH_IO_H
|
||||
|
||||
/**
|
||||
* outb:
|
||||
* Sends the given data to the I/O port. Defined in io.s
|
||||
*
|
||||
* @param port The I/O port to send the data to
|
||||
* @param data TThe data to send to the I/O port
|
||||
*/
|
||||
void outb(unsigned short port, unsigned char data);
|
||||
|
||||
/**
|
||||
* inb:
|
||||
* Read a byte from an I/O port
|
||||
*
|
||||
* @param port The address of the I/O port
|
||||
* @return The read byte
|
||||
*/
|
||||
unsigned char inb(unsigned short port);
|
||||
|
||||
#endif
|
||||
|
||||
|
20
kernel/arch/io.s
Normal file
20
kernel/arch/io.s
Normal file
@ -0,0 +1,20 @@
|
||||
global outb
|
||||
; out b - send a byte to an I/O port
|
||||
; stack: [esp + 8] the data byte
|
||||
; [esp + 4] the I/O port
|
||||
; [esp ] return address
|
||||
outb:
|
||||
mov al, [esp + 8]
|
||||
mov dx, [esp + 4] ; move the address into dx
|
||||
out dx, al ; send the data to the I/O port
|
||||
ret ; return to the calling function
|
||||
|
||||
global inb
|
||||
; inb - returns a byte from the given I/O port
|
||||
; stack: [esp + 4] The address of the I/O port
|
||||
; [esp ] The return address
|
||||
inb:
|
||||
mov dx, [esp + 4] ; move the address of the I/O port to the dx register
|
||||
in al, dx ; read a byte from the I/O port and store it in the al register
|
||||
ret ; return the read byte
|
||||
|
28
kernel/arch/link.ld
Normal file
28
kernel/arch/link.ld
Normal file
@ -0,0 +1,28 @@
|
||||
ENTRY(loader)
|
||||
|
||||
SECTIONS
|
||||
{
|
||||
. = 1M;
|
||||
|
||||
.text BLOCK(4K) : ALIGN(4K)
|
||||
{
|
||||
*(.multiboot)
|
||||
*(.text)
|
||||
}
|
||||
|
||||
.rodata BLOCK(4K) : ALIGN(4K)
|
||||
{
|
||||
*(.rodata)
|
||||
}
|
||||
|
||||
.data BLOCK(4K) : ALIGN(4K)
|
||||
{
|
||||
*(.data)
|
||||
}
|
||||
|
||||
.bss BLOCK(4K) : ALIGN(4K)
|
||||
{
|
||||
*(COMMON)
|
||||
*(.bss)
|
||||
}
|
||||
}
|
123
kernel/arch/serial.c
Normal file
123
kernel/arch/serial.c
Normal file
@ -0,0 +1,123 @@
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <kernel/serial.h>
|
||||
|
||||
|
||||
#include "io.h"
|
||||
|
||||
/* I/O ports */
|
||||
|
||||
/**
|
||||
* All the I/O ports are calculated relative to their base.
|
||||
* The ports (COM1, COM2, etc) have their ports in the same order,
|
||||
* but they start at different values.
|
||||
*/
|
||||
#define SERIAL_COM1_BASE 0x3F8
|
||||
|
||||
#define SERIAL_DATA_PORT(base) (base)
|
||||
#define SERIAL_FIFO_COMMAND_PORT(base) (base + 2)
|
||||
#define SERIAL_LINE_COMMAND_PORT(base) (base + 3)
|
||||
#define SERIAL_MODEM_COMMAND_PORT(base) (base + 4)
|
||||
#define SERIAL_LINE_STATUS_PORT(base) (base + 5)
|
||||
|
||||
/* I/O port commands */
|
||||
|
||||
/**
|
||||
* SERIAL_LINE_ENABLE_DLAB:
|
||||
* Tells the serial port to expect first the highest 8 bits on the data port,
|
||||
* then the lowest 8 bits will follow
|
||||
*/
|
||||
#define SERIAL_LINE_ENABLE_DLAB 0x80
|
||||
|
||||
void serial_configure_baud_rate(unsigned short com, unsigned short divisor)
|
||||
{
|
||||
outb(SERIAL_LINE_COMMAND_PORT(com),
|
||||
SERIAL_LINE_ENABLE_DLAB);
|
||||
outb(SERIAL_DATA_PORT(com),
|
||||
(divisor >> 8) & 0x00FF);
|
||||
outb(SERIAL_DATA_PORT(com),
|
||||
divisor & 0x00FF);
|
||||
}
|
||||
|
||||
void serial_configure_line(unsigned short com)
|
||||
{
|
||||
/**
|
||||
* Bit: | 7 | 6 | 5 4 3 | 2 | 1 0 |
|
||||
* Content: | d | b | prty | s | dl |
|
||||
* d - Enabled (d = 1) or disables (d = 0) DLAB
|
||||
* b - If break control is enabled (b = 1) or disabled (b = 0)
|
||||
* prty - The number of parity bits to use
|
||||
* s - The number of stop bits to use (s = 0 equals 1, s = 1 equals 1.5 or 2)
|
||||
* dl - Describes the length of the data
|
||||
* Value: | 0 | 0 | 0 0 0 | 0 | 1 1 | = 0x03
|
||||
*/
|
||||
outb(SERIAL_LINE_COMMAND_PORT(com), 0x03);
|
||||
}
|
||||
|
||||
void serial_configure_buffers(unsigned short com)
|
||||
{
|
||||
/**
|
||||
* Bit: | 7 6 | 5 | 4 | 3 | 2 | 1 | 0 |
|
||||
* Content: | lvl | bs | r | dma | clt | clr | e |
|
||||
* lvl - How many bytes should be stored in the FIFO buffers
|
||||
* bs - If the buffers should be 16 or 64 bytes large
|
||||
* r - Reserved for future use
|
||||
* dma - How the serial port data should be accessed
|
||||
* clt - Clear the transmission FIFO buffer
|
||||
* clr - Clear the receiver FIFO buffer
|
||||
* e - If the FIFO buffer should be enabled or not
|
||||
* Value: | 1 1 | 0 | 0 | 0 | 1 | 1 | 1 |
|
||||
*/
|
||||
outb(SERIAL_FIFO_COMMAND_PORT(com), 0xC7);
|
||||
}
|
||||
|
||||
void serial_configure_modem(unsigned short com)
|
||||
{
|
||||
/**
|
||||
* Bit: | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
|
||||
* Content: | r | r | af | lb | ao2 | ao1 | rts | dtr |
|
||||
* r - Reserved
|
||||
* af - Autoflow control enabled
|
||||
* lb - Loopback mode (used for debugging serial ports)
|
||||
* ao2 - Auxiliary output 2, used for receiving interrupts
|
||||
* ao1 - Auxiliary output 1
|
||||
* rts - Ready to transmit
|
||||
* dtr - Data Terminal ready
|
||||
* Value: | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 1 |
|
||||
*/
|
||||
outb(SERIAL_MODEM_COMMAND_PORT(com), 0x03);
|
||||
}
|
||||
|
||||
int serial_is_transmit_fifo_empty(unsigned int com)
|
||||
{
|
||||
/* 0x20 = 0010 0000 */
|
||||
return inb(SERIAL_LINE_STATUS_PORT(com)) & 0x20;
|
||||
}
|
||||
|
||||
void serial_initialize(void)
|
||||
{
|
||||
serial_configure_baud_rate(SERIAL_COM1_BASE, 2);
|
||||
serial_configure_line(SERIAL_COM1_BASE);
|
||||
serial_configure_buffers(SERIAL_COM1_BASE);
|
||||
serial_configure_modem(SERIAL_COM1_BASE);
|
||||
}
|
||||
|
||||
void serial_write_byte(unsigned short com, char c)
|
||||
{
|
||||
while (!serial_is_transmit_fifo_empty(com));
|
||||
outb(SERIAL_DATA_PORT(com), c);
|
||||
}
|
||||
|
||||
void serial_write(const char* buf, size_t len)
|
||||
{
|
||||
for (size_t i = 0; i < len; i++)
|
||||
serial_write_byte(SERIAL_COM1_BASE, buf[i]);
|
||||
}
|
||||
|
||||
void serial_writestring(const char* buf)
|
||||
{
|
||||
serial_write(buf, strlen(buf));
|
||||
}
|
||||
|
131
kernel/arch/tty.c
Normal file
131
kernel/arch/tty.c
Normal file
@ -0,0 +1,131 @@
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <kernel/tty.h>
|
||||
|
||||
#include "vga.h"
|
||||
#include "io.h"
|
||||
|
||||
/* I/O ports */
|
||||
#define VGA_COMMAND_PORT 0x3D4
|
||||
#define VGA_DATA_PORT 0x3D5
|
||||
|
||||
/* I/O port commands */
|
||||
#define VGA_HIGH_BYTE_COMMAND 14
|
||||
#define VGA_LOW_BYTE_COMMAND 15
|
||||
|
||||
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_colour;
|
||||
static struct vga_entry* terminal_buffer;
|
||||
|
||||
struct vga_entry {
|
||||
char c;
|
||||
unsigned char colour;
|
||||
} __attribute__((packed));
|
||||
|
||||
void terminal_set_colour(uint8_t colour)
|
||||
{
|
||||
terminal_colour = colour;
|
||||
}
|
||||
|
||||
void terminal_clear(void)
|
||||
{
|
||||
for (size_t y = 0; y < VGA_HEIGHT; y++)
|
||||
for (size_t x = 0; x < VGA_WIDTH; x++)
|
||||
terminal_write_cell((y * VGA_WIDTH) + x, ' ');
|
||||
terminal_move_cursor(0);
|
||||
}
|
||||
|
||||
void terminal_initialize(void)
|
||||
{
|
||||
terminal_row = 0;
|
||||
terminal_column = 0;
|
||||
|
||||
terminal_buffer = (struct vga_entry*) VGA_MEMORY;
|
||||
terminal_set_colour(vga_entry_colour(VGA_COLOUR_LIGHT_GREY, VGA_COLOUR_BLACK));
|
||||
|
||||
terminal_clear();
|
||||
}
|
||||
|
||||
void terminal_move_cursor(unsigned short pos)
|
||||
{
|
||||
if (terminal_row >= VGA_HEIGHT)
|
||||
terminal_scroll_screen();
|
||||
|
||||
outb(VGA_COMMAND_PORT, VGA_HIGH_BYTE_COMMAND);
|
||||
outb(VGA_DATA_PORT, ((pos >> 8) & 0x00FF));
|
||||
outb(VGA_COMMAND_PORT, VGA_LOW_BYTE_COMMAND);
|
||||
outb(VGA_DATA_PORT, pos & 0x00FF);
|
||||
}
|
||||
|
||||
inline size_t terminal_get_pos(void)
|
||||
{return (terminal_row * VGA_WIDTH) + terminal_column;}
|
||||
|
||||
void terminal_write_cell(unsigned int i, char c)
|
||||
{
|
||||
struct vga_entry entry;
|
||||
|
||||
entry.c = c;
|
||||
entry.colour = terminal_colour;
|
||||
|
||||
terminal_buffer[i] = entry;
|
||||
}
|
||||
|
||||
void terminal_write_character(unsigned int i, char c)
|
||||
{
|
||||
switch(c) {
|
||||
case '\n':
|
||||
terminal_row += 1;
|
||||
__attribute__((fallthrough));
|
||||
case '\r':
|
||||
terminal_column = 0;
|
||||
terminal_move_cursor(terminal_get_pos());
|
||||
break;
|
||||
default:
|
||||
terminal_write_cell(i, c);
|
||||
|
||||
terminal_column += 1;
|
||||
if (terminal_column >= VGA_WIDTH) {
|
||||
terminal_column = 0;
|
||||
terminal_row += 1;
|
||||
}
|
||||
|
||||
terminal_move_cursor(terminal_get_pos());
|
||||
}
|
||||
}
|
||||
|
||||
void terminal_write(const char* buf, size_t len)
|
||||
{
|
||||
for (size_t i = 0; i < len; i++)
|
||||
terminal_write_character(terminal_get_pos(), buf[i]);
|
||||
}
|
||||
|
||||
void terminal_writestring(const char* buf)
|
||||
{
|
||||
terminal_write(buf, strlen(buf));
|
||||
}
|
||||
|
||||
void terminal_scroll_screen(void)
|
||||
{
|
||||
size_t y, x;
|
||||
for (y = 0; y < VGA_HEIGHT -1; y++) {
|
||||
for (x = 0; x < VGA_WIDTH; x++) {
|
||||
terminal_buffer[(y * VGA_WIDTH) + x] =
|
||||
terminal_buffer[((y + 1) * VGA_WIDTH) + x];
|
||||
}
|
||||
}
|
||||
for (x = 0; x < VGA_WIDTH; x++)
|
||||
terminal_write_cell(((VGA_HEIGHT - 1) * VGA_WIDTH) + x, ' ');
|
||||
|
||||
terminal_row -= 1;
|
||||
terminal_column = 0;
|
||||
terminal_move_cursor(terminal_get_pos());
|
||||
}
|
||||
|
30
kernel/arch/vga.h
Normal file
30
kernel/arch/vga.h
Normal file
@ -0,0 +1,30 @@
|
||||
#ifndef ARCH_VGA_H
|
||||
#define ARCH_VGA_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
typedef enum vga_colour {
|
||||
VGA_COLOUR_BLACK = 0,
|
||||
VGA_COLOUR_BLUE = 1,
|
||||
VGA_COLOUR_GREEN = 2,
|
||||
VGA_COLOUR_CYAN = 3,
|
||||
VGA_COLOUR_RED = 4,
|
||||
VGA_COLOUR_MAGENTA = 5,
|
||||
VGA_COLOUR_BROWN = 6,
|
||||
VGA_COLOUR_LIGHT_GREY = 7,
|
||||
VGA_COLOUR_DARK_GREY = 8,
|
||||
VGA_COLOUR_LIGHT_BLUE = 9,
|
||||
VGA_COLOUR_LIGHT_GREEN = 10,
|
||||
VGA_COLOUR_LIGHT_CYAN = 11,
|
||||
VGA_COLOUR_LIGHT_RED = 12,
|
||||
VGA_COLOUR_LIGHT_MAGENTA = 13,
|
||||
VGA_COLOUR_LIGHT_BROWN = 14,
|
||||
VGA_COLOUR_WHITE = 15,
|
||||
} vga_colour_t;
|
||||
|
||||
static inline uint8_t vga_entry_colour(vga_colour_t fg, vga_colour_t bg) {
|
||||
return fg | bg << 4;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
Reference in New Issue
Block a user