Compare commits
2 Commits
b6caeb4a74
...
3cc9ab2174
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3cc9ab2174 | ||
|
|
751a182122 |
8
makefile
8
makefile
@@ -1,7 +1,7 @@
|
|||||||
CC = x86_64-elf-gcc
|
CC = i386-elf-gcc
|
||||||
LD = x86_64-elf-ld
|
LD = i386-elf-ld
|
||||||
|
|
||||||
CFLAGS = -ffreestanding -m64
|
CFLAGS = -ffreestanding -m32
|
||||||
|
|
||||||
all: .build/nub-os.iso
|
all: .build/nub-os.iso
|
||||||
|
|
||||||
@@ -27,4 +27,4 @@ build-dir:
|
|||||||
$(CC) $(CFLAGS) -c -o .build/print.o src/print.c
|
$(CC) $(CFLAGS) -c -o .build/print.o src/print.c
|
||||||
|
|
||||||
.build/boot.o: build-dir src/boot.asm
|
.build/boot.o: build-dir src/boot.asm
|
||||||
nasm -f elf64 -o .build/boot.o src/boot.asm
|
nasm -f elf32 -o .build/boot.o src/boot.asm
|
||||||
130
src/boot.asm
130
src/boot.asm
@@ -14,14 +14,6 @@ align 4
|
|||||||
header_end:
|
header_end:
|
||||||
|
|
||||||
section .bss
|
section .bss
|
||||||
align 4096
|
|
||||||
page_table_l4:
|
|
||||||
resb 4096
|
|
||||||
page_table_l3:
|
|
||||||
resb 4096
|
|
||||||
page_table_l2:
|
|
||||||
resb 4096
|
|
||||||
|
|
||||||
align 16
|
align 16
|
||||||
resb 16384
|
resb 16384
|
||||||
stack_top:
|
stack_top:
|
||||||
@@ -29,129 +21,11 @@ stack_top:
|
|||||||
extern kernel_main
|
extern kernel_main
|
||||||
|
|
||||||
section .text
|
section .text
|
||||||
bits 32
|
|
||||||
global _start
|
global _start
|
||||||
_start:
|
_start:
|
||||||
mov esp, stack_top
|
mov esp, stack_top
|
||||||
|
|
||||||
call check_cpuid
|
|
||||||
call check_long_mode
|
|
||||||
|
|
||||||
call setup_page_tables
|
|
||||||
call enable_paging
|
|
||||||
|
|
||||||
lgdt [gdt64.pointer]
|
|
||||||
|
|
||||||
jmp gdt64.kernel_code:long_mode_start
|
|
||||||
|
|
||||||
check_cpuid:
|
|
||||||
pushfd
|
|
||||||
pop eax
|
|
||||||
mov ecx, eax
|
|
||||||
xor eax, 1 << 21
|
|
||||||
push eax
|
|
||||||
popfd
|
|
||||||
pushfd
|
|
||||||
pop eax
|
|
||||||
push ecx
|
|
||||||
popfd
|
|
||||||
xor eax, ecx
|
|
||||||
jz .no_cpuid
|
|
||||||
ret
|
|
||||||
.no_cpuid:
|
|
||||||
mov al, 'I'
|
|
||||||
jmp error
|
|
||||||
|
|
||||||
check_long_mode:
|
|
||||||
mov eax, 0x80000000
|
|
||||||
cpuid
|
|
||||||
cmp eax, 0x80000001
|
|
||||||
jb .no_long_mode
|
|
||||||
|
|
||||||
mov eax, 0x80000001
|
|
||||||
cpuid
|
|
||||||
test edx, 1 << 29
|
|
||||||
jz .no_long_mode
|
|
||||||
ret
|
|
||||||
.no_long_mode:
|
|
||||||
mov al, 'L'
|
|
||||||
jmp error
|
|
||||||
|
|
||||||
setup_page_tables:
|
|
||||||
mov edi, page_table_l4
|
|
||||||
mov ecx, 4096 * 3 / 4
|
|
||||||
xor eax, eax
|
|
||||||
rep stosd
|
|
||||||
|
|
||||||
mov eax, page_table_l3
|
|
||||||
or eax, 0b11
|
|
||||||
mov [page_table_l4], eax
|
|
||||||
|
|
||||||
mov eax, page_table_l2
|
|
||||||
or eax, 0b11
|
|
||||||
mov [page_table_l3], eax
|
|
||||||
|
|
||||||
mov ecx, 0
|
|
||||||
.map_p2_table:
|
|
||||||
mov eax, 0x200000
|
|
||||||
mul ecx
|
|
||||||
or eax, 0b10000011
|
|
||||||
mov [page_table_l2 + ecx * 8], eax
|
|
||||||
inc ecx
|
|
||||||
cmp ecx, 512
|
|
||||||
jne .map_p2_table
|
|
||||||
ret
|
|
||||||
|
|
||||||
enable_paging:
|
|
||||||
mov eax, page_table_l4
|
|
||||||
mov cr3, eax
|
|
||||||
|
|
||||||
mov eax, cr4
|
|
||||||
or eax, 1 << 5
|
|
||||||
mov cr4, eax
|
|
||||||
|
|
||||||
mov ecx, 0xC0000080
|
|
||||||
rdmsr
|
|
||||||
or eax, 1 << 8
|
|
||||||
wrmsr
|
|
||||||
|
|
||||||
mov eax, cr0
|
|
||||||
or eax, 1 << 31
|
|
||||||
mov cr0, eax
|
|
||||||
ret
|
|
||||||
|
|
||||||
bits 64
|
|
||||||
long_mode_start:
|
|
||||||
mov ax, gdt64.kernel_data
|
|
||||||
mov ss, ax
|
|
||||||
mov ds, ax
|
|
||||||
mov es, ax
|
|
||||||
mov fs, ax
|
|
||||||
mov gs, ax
|
|
||||||
|
|
||||||
mov rsp, stack_top
|
|
||||||
|
|
||||||
call kernel_main
|
call kernel_main
|
||||||
|
|
||||||
cli
|
|
||||||
hang:
|
hang:
|
||||||
|
cli
|
||||||
hlt
|
hlt
|
||||||
jmp hang
|
jmp hang
|
||||||
|
|
||||||
error:
|
|
||||||
mov dword [0xb8000], 0x4f524f45
|
|
||||||
mov dword [0xb8004], 0x4f3a4f52
|
|
||||||
mov dword [0xb8008], 0x4f204f20
|
|
||||||
mov byte [0xb800a], al
|
|
||||||
hlt
|
|
||||||
|
|
||||||
section .rodata
|
|
||||||
gdt64:
|
|
||||||
dq 0
|
|
||||||
.kernel_code: equ $ - gdt64
|
|
||||||
dq (1<<44) | (1<<47) | (1<<41) | (1<<43) | (1<<53)
|
|
||||||
.kernel_data: equ $ - gdt64
|
|
||||||
dq (1<<44) | (1<<47) | (1<<41)
|
|
||||||
.pointer:
|
|
||||||
dw $ - gdt64 - 1
|
|
||||||
dq gdt64
|
|
||||||
@@ -2,5 +2,5 @@
|
|||||||
|
|
||||||
void kernel_main(void)
|
void kernel_main(void)
|
||||||
{
|
{
|
||||||
print("Starting nub-os\n");
|
print("Welcome to nub OS\n");
|
||||||
}
|
}
|
||||||
42
src/print.c
42
src/print.c
@@ -8,38 +8,6 @@
|
|||||||
#define BUF_START 0xb8000
|
#define BUF_START 0xb8000
|
||||||
#define BUF_END (BUF_START + BUF_SIZE)
|
#define BUF_END (BUF_START + BUF_SIZE)
|
||||||
|
|
||||||
enum FG_COLOR
|
|
||||||
{
|
|
||||||
FG_BLACK = 0x0,
|
|
||||||
FG_BLUE = 0x1,
|
|
||||||
FG_GREEN = 0x2,
|
|
||||||
FG_CYAN = 0x3,
|
|
||||||
FG_RED = 0x4,
|
|
||||||
FG_MAGENTA = 0x5,
|
|
||||||
FG_BROWN = 0x6,
|
|
||||||
FG_LIGHT_GRAY = 0x7,
|
|
||||||
FG_DARK_GRAY = 0x8,
|
|
||||||
FG_LIGHT_BLUE = 0x9,
|
|
||||||
FG_LIGHT_GREEN = 0xA,
|
|
||||||
FG_LIGHT_CYAN = 0xB,
|
|
||||||
FG_LIGHT_RED = 0xC,
|
|
||||||
FG_LIGHT_MAGENTA = 0xD,
|
|
||||||
FG_YELLOW = 0xE,
|
|
||||||
FG_WHITE = 0xF,
|
|
||||||
};
|
|
||||||
|
|
||||||
enum BG_COLOR
|
|
||||||
{
|
|
||||||
BG_BLACK = 0x0,
|
|
||||||
BG_BLUE = 0x1,
|
|
||||||
BG_GREEN = 0x2,
|
|
||||||
BG_CYAN = 0x3,
|
|
||||||
BG_RED = 0x4,
|
|
||||||
BG_MAGENTA = 0x5,
|
|
||||||
BG_BROWN = 0x6,
|
|
||||||
BG_LIGHT_GRAY = 0x7,
|
|
||||||
};
|
|
||||||
|
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
uint8_t character;
|
uint8_t character;
|
||||||
@@ -66,7 +34,7 @@ void scroll(void)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void put_char(char c, uint8_t color)
|
void put_char(char c, FG_COLOR fg_color, BG_COLOR bg_color)
|
||||||
{
|
{
|
||||||
if (c == '\n')
|
if (c == '\n')
|
||||||
{
|
{
|
||||||
@@ -75,6 +43,7 @@ void put_char(char c, uint8_t color)
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
uint8_t color = fg_color | bg_color << 4;
|
||||||
vga_buffer[cursor_row * COLUMNS + cursor_col] = (vga_char){ c, color };
|
vga_buffer[cursor_row * COLUMNS + cursor_col] = (vga_char){ c, color };
|
||||||
cursor_col++;
|
cursor_col++;
|
||||||
}
|
}
|
||||||
@@ -94,11 +63,14 @@ void put_char(char c, uint8_t color)
|
|||||||
|
|
||||||
void print(const char* string)
|
void print(const char* string)
|
||||||
{
|
{
|
||||||
uint8_t color = FG_WHITE | BG_BLACK << 4;
|
print_clr(string, FG_WHITE, BG_BLACK);
|
||||||
|
}
|
||||||
|
|
||||||
|
void print_clr(const char* string, FG_COLOR fg_color, BG_COLOR bg_color)
|
||||||
|
{
|
||||||
for (int i = 0; string[i]; i++)
|
for (int i = 0; string[i]; i++)
|
||||||
{
|
{
|
||||||
put_char(string[i], color);
|
put_char(string[i], fg_color, bg_color);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
36
src/print.h
36
src/print.h
@@ -1,8 +1,40 @@
|
|||||||
|
#pragma once
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
void kernel_print(const char* string);
|
typedef enum
|
||||||
|
{
|
||||||
|
FG_BLACK = 0x0,
|
||||||
|
FG_BLUE = 0x1,
|
||||||
|
FG_GREEN = 0x2,
|
||||||
|
FG_CYAN = 0x3,
|
||||||
|
FG_RED = 0x4,
|
||||||
|
FG_MAGENTA = 0x5,
|
||||||
|
FG_BROWN = 0x6,
|
||||||
|
FG_LIGHT_GRAY = 0x7,
|
||||||
|
FG_DARK_GRAY = 0x8,
|
||||||
|
FG_LIGHT_BLUE = 0x9,
|
||||||
|
FG_LIGHT_GREEN = 0xA,
|
||||||
|
FG_LIGHT_CYAN = 0xB,
|
||||||
|
FG_LIGHT_RED = 0xC,
|
||||||
|
FG_LIGHT_MAGENTA = 0xD,
|
||||||
|
FG_YELLOW = 0xE,
|
||||||
|
FG_WHITE = 0xF,
|
||||||
|
} FG_COLOR;
|
||||||
|
|
||||||
void put_char(char c, uint8_t color);
|
typedef enum
|
||||||
|
{
|
||||||
|
BG_BLACK = 0x0,
|
||||||
|
BG_BLUE = 0x1,
|
||||||
|
BG_GREEN = 0x2,
|
||||||
|
BG_CYAN = 0x3,
|
||||||
|
BG_RED = 0x4,
|
||||||
|
BG_MAGENTA = 0x5,
|
||||||
|
BG_BROWN = 0x6,
|
||||||
|
BG_LIGHT_GRAY = 0x7,
|
||||||
|
} BG_COLOR;
|
||||||
|
|
||||||
|
void put_char(char c, FG_COLOR fg_color, BG_COLOR bg_color);
|
||||||
void print(const char* string);
|
void print(const char* string);
|
||||||
|
void print_clr(const char* string, FG_COLOR fg_color, BG_COLOR bg_color);
|
||||||
void clear_screen(void);
|
void clear_screen(void);
|
||||||
void set_cursor_position(int row, int col);
|
void set_cursor_position(int row, int col);
|
||||||
10
src/string.c
Normal file
10
src/string.c
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
int strcmp(const char* a, const char* b)
|
||||||
|
{
|
||||||
|
while ((*a != '\0' && *b != '\0') && *a == *b)
|
||||||
|
{
|
||||||
|
a++;
|
||||||
|
b++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (*a == *b) ? 0 : (*a > *b) ? 1 : -1;
|
||||||
|
}
|
||||||
3
src/string.h
Normal file
3
src/string.h
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
int strcmp(const char* a, const char* b);
|
||||||
Reference in New Issue
Block a user