Compare commits
5 Commits
hello-worl
...
gdt
| Author | SHA1 | Date | |
|---|---|---|---|
| 6fc7266716 | |||
| 29deb20cd7 | |||
| 62302e03d5 | |||
| e6f4200ae9 | |||
| c8df8934b5 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,5 +1,6 @@
|
||||
limine
|
||||
kernel
|
||||
pepperk
|
||||
iso_root
|
||||
*.o
|
||||
*.iso
|
||||
|
||||
18
Makefile
18
Makefile
@@ -1,8 +1,8 @@
|
||||
build:
|
||||
rm -f *.o
|
||||
x86_64-elf-gcc -c -I src src/io/term.c src/io/printf.c src/kmain.c -Wall -Wextra -std=gnu99 -nostdlib -ffreestanding -fno-stack-protector -fno-stack-check -fno-PIC -ffunction-sections -fdata-sections -mcmodel=kernel
|
||||
x86_64-elf-gcc -g -c -I src src/mem/utils.h src/mem/gdt.c src/io/serial.c src/io/term.c src/io/printf.c src/kmain.c -Wall -Wextra -std=gnu99 -nostdlib -ffreestanding -fno-stack-protector -fno-stack-check -fno-PIC -ffunction-sections -fdata-sections -mcmodel=kernel
|
||||
objcopy -O elf64-x86-64 -B i386 -I binary zap-light16.psf zap-light16.o
|
||||
x86_64-elf-ld -o kernel -T linker.ld *.o
|
||||
x86_64-elf-ld -o pepperk -T linker.ld *.o
|
||||
|
||||
limine/limine:
|
||||
rm -rf limine
|
||||
@@ -12,7 +12,7 @@ limine/limine:
|
||||
build-iso: limine/limine build
|
||||
rm -rf iso_root
|
||||
mkdir -p iso_root/boot
|
||||
cp -v kernel iso_root/boot
|
||||
cp -v pepperk iso_root/boot
|
||||
mkdir -p iso_root/boot/limine
|
||||
cp -v limine.conf iso_root/boot/limine
|
||||
mkdir -p iso_root/EFI/BOOT
|
||||
@@ -23,11 +23,15 @@ build-iso: limine/limine build
|
||||
-no-emul-boot -boot-load-size 4 -boot-info-table -hfsplus \
|
||||
-apm-block-size 2048 --efi-boot boot/limine/limine-uefi-cd.bin \
|
||||
-efi-boot-part --efi-boot-image --protective-msdos-label \
|
||||
iso_root -o kernel.iso
|
||||
./limine/limine bios-install kernel.iso
|
||||
iso_root -o pepper.iso
|
||||
./limine/limine bios-install pepper.iso
|
||||
|
||||
debug:
|
||||
qemu-system-x86_64 -drive file=pepper.iso -s -S -d int -no-reboot &
|
||||
gdb pepperk --command=debug.gdb
|
||||
|
||||
run: build-iso
|
||||
qemu-system-x86_64 -cdrom kernel.iso
|
||||
qemu-system-x86_64 -cdrom pepper.iso -serial stdio
|
||||
|
||||
clean:
|
||||
rm -rf *.o kernel iso_root kernel.iso limine
|
||||
rm -rf *.o pepperk iso_root pepper.iso limine
|
||||
|
||||
@@ -13,4 +13,8 @@ PepperOS wouldn't be possible without the following freely-licensed software:
|
||||
|
||||
- the [Limine](https://codeberg.org/Limine/Limine) portable bootloader
|
||||
- Marco Paland's freestanding [printf implementation](https://github.com/mpaland)
|
||||
- the [ZAP](https://www.zap.org.au/projects/console-fonts-zap/) PSF console fonts
|
||||
- the [ZAP](https://www.zap.org.au/projects/console-fonts-zap/) PSF console fonts
|
||||
|
||||
...and without these amazing resources:
|
||||
|
||||
- the [OSDev](https://osdev.org) wiki & forums
|
||||
2
debug.gdb
Normal file
2
debug.gdb
Normal file
@@ -0,0 +1,2 @@
|
||||
target remote localhost:1234
|
||||
set disassembly-flavor intel
|
||||
@@ -3,4 +3,4 @@ timeout: 3
|
||||
/PepperOS
|
||||
protocol: limine
|
||||
|
||||
path: boot():/boot/kernel
|
||||
path: boot():/boot/pepperk
|
||||
|
||||
59
src/io/serial.c
Normal file
59
src/io/serial.c
Normal file
@@ -0,0 +1,59 @@
|
||||
#include "../kernel.h"
|
||||
|
||||
void outb(int port, unsigned char data)
|
||||
{
|
||||
__asm__ __volatile__("outb %%al, %%dx" :: "a" (data),"d" (port));
|
||||
}
|
||||
|
||||
unsigned char inb(int port)
|
||||
{
|
||||
unsigned char data = 0;
|
||||
__asm__ __volatile__("inb %%dx, %%al" : "=a" (data) : "d" (port));
|
||||
return data;
|
||||
}
|
||||
|
||||
// COM1
|
||||
#define PORT 0x3F8
|
||||
|
||||
int serial_init()
|
||||
{
|
||||
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)
|
||||
|
||||
if (inb(PORT) != 0xAE)
|
||||
{
|
||||
return -EIO;
|
||||
}
|
||||
|
||||
// Set normal operation mode
|
||||
outb(PORT + 4, 0x0F);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int is_transmit_empty()
|
||||
{
|
||||
return inb(PORT + 5) & 0x20;
|
||||
}
|
||||
|
||||
void write_serial(char c)
|
||||
{
|
||||
while (!is_transmit_empty()); // wait for free spot
|
||||
outb(PORT, c);
|
||||
}
|
||||
|
||||
void serial_kputs(const char* str)
|
||||
{
|
||||
unsigned int i=0;
|
||||
while (str[i])
|
||||
{
|
||||
write_serial(str[i]);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
10
src/io/serial.h
Normal file
10
src/io/serial.h
Normal file
@@ -0,0 +1,10 @@
|
||||
#ifndef SERIAL_H
|
||||
#define SERIAL_H
|
||||
|
||||
void outb(int port, unsigned char data);
|
||||
unsigned char inb(int port);
|
||||
|
||||
int serial_init();
|
||||
void serial_kputs(const char* str);
|
||||
|
||||
#endif
|
||||
@@ -3,7 +3,8 @@
|
||||
|
||||
enum ErrorCodes
|
||||
{
|
||||
ENOMEM
|
||||
ENOMEM,
|
||||
EIO
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
78
src/kmain.c
78
src/kmain.c
@@ -3,6 +3,9 @@
|
||||
#include <limine.h>
|
||||
#include "io/term.h"
|
||||
#include "io/printf.h"
|
||||
#include "io/serial.h"
|
||||
#include "mem/gdt.h"
|
||||
#include "mem/utils.h"
|
||||
|
||||
// Limine version used
|
||||
__attribute__((used, section(".limine_requests")))
|
||||
@@ -23,74 +26,6 @@ static volatile LIMINE_REQUESTS_END_MARKER;
|
||||
|
||||
struct limine_framebuffer* framebuffer;
|
||||
|
||||
// We won't be linked to standard library, but still need the basic mem* functions
|
||||
// so everything goes allright with the compiler
|
||||
|
||||
// We use the "restrict" keyword on pointers so that the compiler knows it can
|
||||
// do more optimization on them (and as it's a much used function, it's good to
|
||||
// be able to do that)
|
||||
void* memcpy(void* restrict dest, const void* restrict src, size_t n)
|
||||
{
|
||||
uint8_t* restrict pdest = (uint8_t* restrict)dest;
|
||||
const uint8_t* restrict psrc = (const uint8_t* restrict)src;
|
||||
|
||||
for (size_t i=0; i<n; i++)
|
||||
{
|
||||
pdest[i] = psrc[i];
|
||||
}
|
||||
|
||||
return dest;
|
||||
}
|
||||
|
||||
void* memset(void* s, int c, size_t n)
|
||||
{
|
||||
uint8_t* p = (uint8_t*)s;
|
||||
|
||||
for (size_t i=0; i<n; i++)
|
||||
{
|
||||
p[i] = (uint8_t)c;
|
||||
}
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
void* memmove(void *dest, const void* src, size_t n)
|
||||
{
|
||||
uint8_t* pdest = (uint8_t*)dest;
|
||||
const uint8_t* psrc = (uint8_t*)src;
|
||||
|
||||
if (src > dest)
|
||||
{
|
||||
for (size_t i=0; i<n; i++)
|
||||
{
|
||||
pdest[i] = psrc[i];
|
||||
}
|
||||
} else if (src < dest)
|
||||
{
|
||||
for (size_t i=n; i>0; i--)
|
||||
{
|
||||
pdest[i-1] = psrc[i-1];
|
||||
}
|
||||
}
|
||||
return dest;
|
||||
}
|
||||
|
||||
int memcmp(const void* s1, const void* s2, size_t n)
|
||||
{
|
||||
const uint8_t* p1 = (const uint8_t*)s1;
|
||||
const uint8_t* p2 = (const uint8_t*)s2;
|
||||
|
||||
for (size_t i=0; i<n; i++)
|
||||
{
|
||||
if (p1[i] != p2[i])
|
||||
{
|
||||
return p1[i] < p2[i] ? -1 : 1;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Panic
|
||||
static void hcf()
|
||||
{
|
||||
@@ -111,6 +46,13 @@ void kmain()
|
||||
|
||||
if (term_init()) hcf();
|
||||
|
||||
if (serial_init()) kputs("kernel: serial: error: Cannot init serial communication!");
|
||||
|
||||
serial_kputs("\n\nkernel: serial: Hello, world from serial!\n");
|
||||
|
||||
gdt_init();
|
||||
serial_kputs("kernel: gdt: Initialized GDT!");
|
||||
|
||||
// Draw something
|
||||
printf("%s, %s!", "Hello", "world");
|
||||
|
||||
|
||||
79
src/mem/gdt.c
Normal file
79
src/mem/gdt.c
Normal file
@@ -0,0 +1,79 @@
|
||||
#include "gdt.h"
|
||||
#include <stdint.h>
|
||||
|
||||
// Descriptors are 8-byte wide (64bits)
|
||||
// So the selectors will be (in bytes): 0x0, 0x8, 0x10, 0x18, etc..
|
||||
uint64_t gdt_entries[NUM_GDT_ENTRIES];
|
||||
struct GDTR gdtr;
|
||||
|
||||
static void gdt_load()
|
||||
{
|
||||
asm("lgdt %0" : : "m"(gdtr));
|
||||
}
|
||||
|
||||
static void gdt_flush()
|
||||
{
|
||||
// Here, 0x8 is the kernel code selector
|
||||
// and 0x10 is the kernel data selector
|
||||
asm volatile (
|
||||
"mov $0x10, %%ax \n" // Reload segments with kernel data selector
|
||||
"mov %%ax, %%ds \n"
|
||||
"mov %%ax, %%es \n"
|
||||
"mov %%ax, %%fs \n"
|
||||
"mov %%ax, %%gs \n"
|
||||
"mov %%ax, %%ss \n"
|
||||
|
||||
"pushq $0x8 \n" // CS reload
|
||||
"lea 1f(%%rip), %%rax \n"
|
||||
"push %%rax \n"
|
||||
"lretq \n"
|
||||
"1: \n" // Execution continues here after CS reload
|
||||
:
|
||||
:
|
||||
: "rax", "memory"
|
||||
);
|
||||
}
|
||||
|
||||
void gdt_init()
|
||||
{
|
||||
// Null descriptor (required)
|
||||
gdt_entries[0] = 0;
|
||||
|
||||
// Kernel code segment
|
||||
uint64_t kernel_code = 0;
|
||||
kernel_code |= 0b1101 << 8; // Selector type: accessed, read-enable, no conforming
|
||||
kernel_code |= 1 << 12; // not a system descriptor
|
||||
kernel_code |= 0 << 13; // DPL field = 0
|
||||
kernel_code |= 1 << 15; // Present
|
||||
kernel_code |= 1 << 21; // Long mode
|
||||
|
||||
// Left shift 32 bits so we place our stuff in the upper 32 bits of the descriptor.
|
||||
// The lower 32 bits contain limit and part of base and therefore are ignored in Long Mode
|
||||
// (because we'll use paging; segmentation is used only for legacy)
|
||||
gdt_entries[1] = kernel_code << 32;
|
||||
|
||||
uint64_t kernel_data = 0;
|
||||
kernel_data |= 0b0011 << 8;
|
||||
kernel_data |= 1 << 12;
|
||||
kernel_data |= 0 << 13;
|
||||
kernel_data |= 1 << 15;
|
||||
kernel_data |= 1 << 21;
|
||||
|
||||
gdt_entries[2] = kernel_data << 32;
|
||||
|
||||
// We re-use the kernel descriptors here, and just update their DPL fields
|
||||
// (Descriptor privilege level) from ring 0 -> to ring 3 (userspace)
|
||||
uint64_t user_code = kernel_code | (3 << 13);
|
||||
gdt_entries[3] = user_code;
|
||||
|
||||
uint64_t user_data = kernel_data | (3 << 13);
|
||||
gdt_entries[4] = user_data;
|
||||
|
||||
// The -1 subtraction is some wizardry explained in the OSDev wiki -> GDT
|
||||
gdtr.limit = NUM_GDT_ENTRIES * sizeof(uint64_t) - 1;
|
||||
gdtr.address = (uint64_t)gdt_entries;
|
||||
|
||||
// Load the GDT we created, flush the old one
|
||||
gdt_load();
|
||||
gdt_flush();
|
||||
}
|
||||
26
src/mem/gdt.h
Normal file
26
src/mem/gdt.h
Normal file
@@ -0,0 +1,26 @@
|
||||
#ifndef GDT_H
|
||||
#define GDT_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
// We're using the GDT for segmentation, but as we want to target Long Mode,
|
||||
// we'll only use this as a requirement for paging, not more.
|
||||
// This means base 0 and no limit (whole address space)
|
||||
|
||||
#define NUM_GDT_ENTRIES 5
|
||||
|
||||
#define NULL_SELECTOR 0x00
|
||||
#define KERNEL_CODE_SEGMENT 0x08
|
||||
#define KERNEL_DATA_SEGMENT 0x10
|
||||
#define USER_CODE_SEGMENT 0x18
|
||||
#define USER_DATA_SEGMENT 0x20
|
||||
|
||||
struct GDTR
|
||||
{
|
||||
uint16_t limit;
|
||||
uint64_t address;
|
||||
} __attribute__((packed));
|
||||
|
||||
void gdt_init();
|
||||
|
||||
#endif
|
||||
69
src/mem/utils.c
Normal file
69
src/mem/utils.c
Normal file
@@ -0,0 +1,69 @@
|
||||
#include <stddef.h>
|
||||
|
||||
// We won't be linked to standard library, but still need the basic mem* functions
|
||||
// so everything goes allright with the compiler
|
||||
|
||||
// We use the "restrict" keyword on pointers so that the compiler knows it can
|
||||
// do more optimization on them (and as it's a much used function, it's good to
|
||||
// be able to do that)
|
||||
void* memcpy(void* restrict dest, const void* restrict src, size_t n)
|
||||
{
|
||||
uint8_t* restrict pdest = (uint8_t* restrict)dest;
|
||||
const uint8_t* restrict psrc = (const uint8_t* restrict)src;
|
||||
|
||||
for (size_t i=0; i<n; i++)
|
||||
{
|
||||
pdest[i] = psrc[i];
|
||||
}
|
||||
|
||||
return dest;
|
||||
}
|
||||
|
||||
void* memset(void* s, int c, size_t n)
|
||||
{
|
||||
uint8_t* p = (uint8_t*)s;
|
||||
|
||||
for (size_t i=0; i<n; i++)
|
||||
{
|
||||
p[i] = (uint8_t)c;
|
||||
}
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
void* memmove(void *dest, const void* src, size_t n)
|
||||
{
|
||||
uint8_t* pdest = (uint8_t*)dest;
|
||||
const uint8_t* psrc = (uint8_t*)src;
|
||||
|
||||
if (src > dest)
|
||||
{
|
||||
for (size_t i=0; i<n; i++)
|
||||
{
|
||||
pdest[i] = psrc[i];
|
||||
}
|
||||
} else if (src < dest)
|
||||
{
|
||||
for (size_t i=n; i>0; i--)
|
||||
{
|
||||
pdest[i-1] = psrc[i-1];
|
||||
}
|
||||
}
|
||||
return dest;
|
||||
}
|
||||
|
||||
int memcmp(const void* s1, const void* s2, size_t n)
|
||||
{
|
||||
const uint8_t* p1 = (const uint8_t*)s1;
|
||||
const uint8_t* p2 = (const uint8_t*)s2;
|
||||
|
||||
for (size_t i=0; i<n; i++)
|
||||
{
|
||||
if (p1[i] != p2[i])
|
||||
{
|
||||
return p1[i] < p2[i] ? -1 : 1;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
11
src/mem/utils.h
Normal file
11
src/mem/utils.h
Normal file
@@ -0,0 +1,11 @@
|
||||
#ifndef MEM_UTILS_H
|
||||
#define MEM_UTILS_H
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
void* memcpy(void* restrict dest, const void* restrict src, size_t n);
|
||||
void* memset(void* s, int c, size_t n);
|
||||
void* memmove(void *dest, const void* src, size_t n);
|
||||
int memcmp(const void* s1, const void* s2, size_t n);
|
||||
|
||||
#endif
|
||||
BIN
src/mem/utils.h.gch
Normal file
BIN
src/mem/utils.h.gch
Normal file
Binary file not shown.
Reference in New Issue
Block a user