Added support for basic x86 features

Includes 'gdt', 'idt' and a serial driver
This commit is contained in:
Michael Shaw 2023-02-27 18:36:15 -05:00
commit ca5351b847
11 changed files with 231 additions and 67 deletions

View file

@ -3,6 +3,7 @@ override MAKEFLAGS += -rR
override KERNEL := kernel.elf
override CC := x86_64-elf-gcc
override LD := x86_64-elf-ld
# Convenience macro to reliably declare overridable command variables.
define DEFAULT_VAR =
@ -14,14 +15,6 @@ define DEFAULT_VAR =
endif
endef
# It is highly recommended to use a custom built cross toolchain to build a kernel.
# We are only using "cc" as a placeholder here. It may work by using
# the host system's toolchain, but this is not guaranteed.
$(eval $(call DEFAULT_VAR,CC,cc))
# Same thing for "ld" (the linker).
$(eval $(call DEFAULT_VAR,LD,ld))
# User controllable CFLAGS.
CFLAGS ?= -g -O2 -pipe -Wall -Wextra
@ -74,18 +67,21 @@ all: $(KERNEL)
# Link rules for the final kernel executable.
$(KERNEL): $(OBJ)
$(LD) $(OBJ) $(LDFLAGS) -o $@
@$(LD) $(OBJ) $(LDFLAGS) -o $@
@echo " LD $@"
# Include header dependencies.
-include $(HEADER_DEPS)
# Compilation rules for *.c files.
%.o: %.c
$(CC) $(CFLAGS) -c $< -o $@
@$(CC) $(CFLAGS) -c $< -o $@
@echo " CC $@"
# Compilation rules for *.S files.
%.o: %.S
$(CC) $(CFLAGS) -c $< -o $@
@$(CC) $(CFLAGS) -c $< -o $@
@echo " CC $@"
# Remove object files and the final executable.
.PHONY: clean
@ -99,7 +95,3 @@ distclean: clean
.PHONY += run
run:
make -C .. run-hdd
.PHONY += whichcc
whichcc:
echo "$(CC)"

View file

@ -1,5 +1,8 @@
#include <kernel/common.h>
#include <kernel/pio.h>
#include <stdio.h>
#define COM1 0x3F8
int serial_init()
@ -15,10 +18,12 @@ int serial_init()
outb(COM1 | 0, 0xAE); /* Enable loopback mode */
if (inb(COM1 | 0) != 0xAE) { /* Loopback test */
printf("Serial COM1 loopback test failed\n");
return 1;
}
outb(COM1 | 4, 0x0F); /* Enter normal operation */
printf("Serial COM1 initialized\n");
return 0;
}

10
include/kernel/common.h Normal file
View file

@ -0,0 +1,10 @@
#ifndef _COMMON_H_
#define _COMMON_H_
#include <limine/limine.h>
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#endif /* _COMMON_H_ */

37
include/kernel/gdt.h Normal file
View file

@ -0,0 +1,37 @@
#ifndef _GDT_H_
#define _GDT_H_
#define GDT_NUMDESC 64
#define GDT_SIZE (64 * GDT_NUMDESC)
#define GDT_ACCESS_DATA_RING0 0b10010010
#define GDT_ACCESS_CODE_RING0 0b10011010
#define GDT_ACCESS_DATA_RING3 0b11110010
#define GDT_ACCESS_CODE_RING3 0b11111010
#ifndef __ASM__
#include <stdint.h>
struct gdt_desc {
uint16_t limit;
uint16_t base;
uint8_t base_2;
uint8_t access;
uint8_t flags;
uint8_t base_3;
};
struct gdtr {
uint16_t size;
uint64_t offset;
};
extern struct gdtr __gdtr;
extern struct gdt_desc __gdt[GDT_NUMDESC];
void gdt_init();
#endif /* __ASM__ */
#endif /* _GDT_H_ */

34
include/kernel/idt.h Normal file
View file

@ -0,0 +1,34 @@
#ifndef _IDT_H_
#define _IDT_H_
#define GATE_INTR 0x8E
#define GATE_TRAP 0x8F
#define IDT_GATE_SIZE 16
#define IDT_NUM_VECTORS 256
#define IDT_SIZE 32768
#ifndef __ASM__
#include <stdint.h>
struct irqdesc {
uint16_t offset_low;
uint16_t segment;
uint8_t ist;
uint8_t attrs;
uint16_t offset_mid;
uint32_t offset_high;
uint32_t reserved;
};
struct idtr {
uint16_t size;
uint64_t offset;
};
extern struct idtr __idtr;
extern struct irqdesc __idt[IDT_NUM_VECTORS];
#endif /* __ASM__ */
#endif /* _IDT_H_ */

39
init/descriptor.S Normal file
View file

@ -0,0 +1,39 @@
#define __ASM__
#include <kernel/gdt.h>
#include <kernel/idt.h>
/*
* Data section
*/
.data
.global __gdtr
__gdtr:
.word 0
.quad 0
.global __gdt
__gdt:
.skip GDT_SIZE
.global __idtr
__idtr:
.word 0
.quad 0
.global __idt
__idt:
.skip IDT_SIZE
/*
* Code Section
*/
.text
.global gdt_load
gdt_load:
lgdt (__gdtr)
ret
.global idt_load
idt_load:
lidt (__idtr)
ret

32
init/gdt.c Normal file
View file

@ -0,0 +1,32 @@
#include <kernel/gdt.h>
#include <kernel/common.h>
#include <stdio.h>
void gdt_load();
void gdt_insert(uint16_t offset, uint8_t access, uint8_t flags)
{
struct gdt_desc *desc = (__gdt + offset);
desc->limit = 0;
desc->base = 0;
desc->base_2 = 0;
desc->base_3 = 0;
desc->access = access;
desc->flags = flags;
}
void gdt_init()
{
gdt_insert(0, 0, 0);
gdt_insert(1, GDT_ACCESS_CODE_RING0, 0xA0);
gdt_insert(2, GDT_ACCESS_DATA_RING0, 0xC0);
gdt_insert(3, GDT_ACCESS_CODE_RING3, 0xA0);
gdt_insert(4, GDT_ACCESS_DATA_RING3, 0xC0);
__gdtr.size = (sizeof(__gdt) - 1);
__gdtr.offset = (uint64_t)__gdt;
gdt_load();
printf("GDT initialized\n");
}

19
init/idt.c Normal file
View file

@ -0,0 +1,19 @@
#include <kernel/idt.h>
void idt_insert(uint16_t gate, uint8_t type, uint8_t ist,
uint16_t segment, void *func)
{
uintptr_t f = (uintptr_t)(func);
struct irqdesc *irqdesc = (__idt + gate);
irqdesc->offset_low = f & 0xFFFF;
irqdesc->offset_mid = ((f >> 0x10) & 0xFFFF);
irqdesc->offset_high = ((f >> 0x20) & 0xFFFFFFFF);
irqdesc->segment = segment;
irqdesc->ist = ist;
irqdesc->attrs = type;
}
void idt_init()
{
}

View file

@ -1,9 +1,8 @@
#include <limine/limine.h>
#include <stddef.h>
#include <stdint.h>
#include <kernel/common.h>
#include <kernel/gdt.h>
#include <dev/serial.h>
#include <stdio.h>
#include <limine/limine.h>
static void done(void)
{
@ -14,10 +13,8 @@ static void done(void)
void _start(void)
{
gdt_init();
serial_init();
int test = printf("test %d", -304);
printf("\nExpect: 9, actual: %d\n", test);
done();
}

View file

@ -23,7 +23,6 @@ int printf(const char *restrict fmt, ...)
{
int ret = 0;
char c;
char vac;
char *s;
int i;
@ -69,9 +68,9 @@ int printf(const char *restrict fmt, ...)
ret += printf_write(s);
break;
case 'c':
vac = va_arg(args, char);
i = va_arg(args, int);
ret++;
printf_out(vac);
printf_out(i);
}
} else {
printf_out(c);

View file

@ -9,49 +9,49 @@ ENTRY(_start)
/* MMU permissions */
PHDRS
{
text PT_LOAD FLAGS((1 << 0) | (1 << 2)) ; /* Execute + Read */
rodata PT_LOAD FLAGS((1 << 2)) ; /* Read only */
data PT_LOAD FLAGS((1 << 1) | (1 << 2)) ; /* Write + Read */
text PT_LOAD FLAGS((1 << 0) | (1 << 2)) ; /* Execute + Read */
rodata PT_LOAD FLAGS((1 << 2)) ; /* Read only */
data PT_LOAD FLAGS((1 << 1) | (1 << 2)) ; /* Write + Read */
}
SECTIONS
{
/* We wanna be placed in the topmost 2GiB of the address space, for optimisations */
/* and because that is what the Limine spec mandates. */
/* Any address in this region will do, but often 0xffffffff80000000 is chosen as */
/* that is the beginning of the region. */
. = 0xffffffff80000000;
/* We wanna be placed in the topmost 2GiB of the address space, for optimisations */
/* and because that is what the Limine spec mandates. */
/* Any address in this region will do, but often 0xffffffff80000000 is chosen as */
/* that is the beginning of the region. */
. = 0xffffffff80000000;
.text : {
*(.text .text.*)
} :text
.text : {
*(.text .text.*)
} :text
/* Move to the next memory page for .rodata */
. += CONSTANT(MAXPAGESIZE);
/* Move to the next memory page for .rodata */
. += CONSTANT(MAXPAGESIZE);
.rodata : {
*(.rodata .rodata.*)
} :rodata
.rodata : {
*(.rodata .rodata.*)
} :rodata
/* Move to the next memory page for .data */
. += CONSTANT(MAXPAGESIZE);
/* Move to the next memory page for .data */
. += CONSTANT(MAXPAGESIZE);
.data : {
*(.data .data.*)
} :data
.data : {
*(.data .data.*)
} :data
/* NOTE: .bss needs to be the last thing mapped to :data, otherwise lots of */
/* unnecessary zeros will be written to the binary. */
/* If you need, for example, .init_array and .fini_array, those should be placed */
/* above this. */
.bss : {
*(COMMON)
*(.bss .bss.*)
} :data
/* NOTE: .bss needs to be the last thing mapped to :data, otherwise lots of */
/* unnecessary zeros will be written to the binary. */
/* If you need, for example, .init_array and .fini_array, those should be placed */
/* above this. */
.bss : {
*(COMMON)
*(.bss .bss.*)
} :data
/* Discard .note.* and .eh_frame since they may cause issues on some hosts. */
/DISCARD/ : {
*(.eh_frame)
*(.note .note.*)
}
/* Discard .note.* and .eh_frame since they may cause issues on some hosts. */
/DISCARD/ : {
*(.eh_frame)
*(.note .note.*)
}
}