refactor namespacing for readability
This commit is contained in:
parent
d051e1b0c8
commit
28175d9336
17 changed files with 77 additions and 99 deletions
|
|
@ -4,7 +4,7 @@ const builtin = @import("builtin");
|
||||||
pub fn build(b: *Builder) void {
|
pub fn build(b: *Builder) void {
|
||||||
const kernel = b.addExecutable("kernel", "src/arch/x86/main.zig");
|
const kernel = b.addExecutable("kernel", "src/arch/x86/main.zig");
|
||||||
kernel.addPackagePath("kernel", "src/index.zig");
|
kernel.addPackagePath("kernel", "src/index.zig");
|
||||||
// kernel.addPackagePath("arch", "src/arch/x86/lib/index.zig");
|
kernel.addPackagePath("x86", "src/arch/x86/index.zig");
|
||||||
kernel.setOutputDir("build");
|
kernel.setOutputDir("build");
|
||||||
|
|
||||||
// kernel.addAssemblyFile("src/arch/x86/_start.s");
|
// kernel.addAssemblyFile("src/arch/x86/_start.s");
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
// const tty = @import("tty.zig");
|
// const tty = @import("tty.zig");
|
||||||
const x86 = @import("lib/index.zig");
|
const x86 = @import("x86");
|
||||||
|
|
||||||
// GDT segment selectors.
|
// GDT segment selectors.
|
||||||
pub const KERNEL_CODE = 0x08;
|
pub const KERNEL_CODE = 0x08;
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,6 @@
|
||||||
// https://wiki.osdev.org/IDT
|
// https://wiki.osdev.org/IDT
|
||||||
usingnamespace @import("../../vga.zig");
|
usingnamespace @import("kernel");
|
||||||
const x86 = @import("lib/index.zig");
|
usingnamespace @import("x86");
|
||||||
const interrupt = @import("interrupt.zig");
|
|
||||||
const gdt = @import("gdt.zig");
|
|
||||||
|
|
||||||
// Types of gates.
|
// Types of gates.
|
||||||
pub const INTERRUPT_GATE = 0x8E;
|
pub const INTERRUPT_GATE = 0x8E;
|
||||||
|
|
@ -55,5 +53,5 @@ pub fn initialize() void {
|
||||||
interrupt.initialize();
|
interrupt.initialize();
|
||||||
|
|
||||||
// load IDT
|
// load IDT
|
||||||
x86.lidt(@ptrToInt(&idtr));
|
lidt(@ptrToInt(&idtr));
|
||||||
}
|
}
|
||||||
|
|
|
||||||
8
src/arch/x86/index.zig
Normal file
8
src/arch/x86/index.zig
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
usingnamespace @import("lib/io.zig");
|
||||||
|
usingnamespace @import("lib/instructions.zig");
|
||||||
|
|
||||||
|
const memory = @import("memory.zig");
|
||||||
|
const paging = @import("paging.zig");
|
||||||
|
const idt = @import("idt.zig");
|
||||||
|
const gdt = @import("gdt.zig");
|
||||||
|
const interrupt = @import("interrupt.zig");
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
const x86 = @import("lib/index.zig");
|
usingnamespace @import("kernel");
|
||||||
|
const x86 = @import("x86");
|
||||||
const isr = @import("isr.zig");
|
const isr = @import("isr.zig");
|
||||||
const println = @import("../../vga.zig").println;
|
|
||||||
|
|
||||||
// PIC ports.
|
// PIC ports.
|
||||||
const PIC1_CMD = 0x20;
|
const PIC1_CMD = 0x20;
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
const idt = @import("idt.zig");
|
usingnamespace @import("x86");
|
||||||
|
|
||||||
// Interrupt Service Routines defined externally in assembly.
|
// Interrupt Service Routines defined externally in assembly.
|
||||||
extern fn isr0() void;
|
extern fn isr0() void;
|
||||||
|
|
|
||||||
|
|
@ -1,2 +0,0 @@
|
||||||
usingnamespace @import("io.zig");
|
|
||||||
usingnamespace @import("instructions.zig");
|
|
||||||
|
|
@ -1,13 +1,5 @@
|
||||||
usingnamespace @import("kernel").main;
|
usingnamespace @import("kernel");
|
||||||
usingnamespace @import("kernel").multiboot;
|
usingnamespace @import("x86");
|
||||||
const console = @import("../console.zig");
|
|
||||||
const println = @import("../../vga.zig").println;
|
|
||||||
|
|
||||||
const idt = @import("idt.zig");
|
|
||||||
const memory = @import("memory.zig");
|
|
||||||
const paging = @import("paging.zig");
|
|
||||||
const gdt = @import("gdt.zig");
|
|
||||||
const x86 = @import("lib/index.zig");
|
|
||||||
|
|
||||||
/// x86 specific intialization
|
/// x86 specific intialization
|
||||||
pub fn x86_main(info: *const MultibootInfo) void {
|
pub fn x86_main(info: *const MultibootInfo) void {
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,4 @@
|
||||||
usingnamespace @import("kernel").multiboot;
|
usingnamespace @import("kernel");
|
||||||
usingnamespace @import("kernel").vga;
|
|
||||||
const assert = @import("std").debug.assert;
|
|
||||||
|
|
||||||
var stack: [*]usize = undefined; // Stack of free physical page.
|
var stack: [*]usize = undefined; // Stack of free physical page.
|
||||||
var stack_index: usize = 0; // Index into the stack.
|
var stack_index: usize = 0; // Index into the stack.
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,5 @@
|
||||||
const x86 = @import("lib/index.zig");
|
usingnamespace @import("kernel");
|
||||||
const memory = @import("memory.zig");
|
usingnamespace @import("x86");
|
||||||
const interrupt = @import("interrupt.zig");
|
|
||||||
const assert = @import("std").debug.assert;
|
|
||||||
const println = @import("../../vga.zig").println;
|
|
||||||
|
|
||||||
extern fn setupPaging(phys_pd: usize) void;
|
extern fn setupPaging(phys_pd: usize) void;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,16 +1,11 @@
|
||||||
const interrupt = @import("arch/x86/interrupt.zig");
|
usingnamespace @import("kernel");
|
||||||
const paging = @import("arch/x86/paging.zig");
|
|
||||||
const ps2 = @import("ps2.zig");
|
|
||||||
const pci = @import("pci.zig");
|
|
||||||
const mem = @import("std").mem;
|
|
||||||
usingnamespace @import("vga.zig");
|
|
||||||
|
|
||||||
var command: [10]u8 = undefined;
|
var command: [10]u8 = undefined;
|
||||||
var command_len: usize = 0;
|
var command_len: usize = 0;
|
||||||
|
|
||||||
fn execute(com: []u8) void {
|
fn execute(com: []u8) void {
|
||||||
if (mem.eql(u8, com, "lspci")) pci.lspci();
|
if (@import("std").mem.eql(u8, com, "lspci")) pci.lspci();
|
||||||
if (mem.eql(u8, com, "paging")) paging.addrspace();
|
if (@import("std").mem.eql(u8, com, "paging")) x86.paging.addrspace();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn keypress(char: u8) void {
|
pub fn keypress(char: u8) void {
|
||||||
|
|
@ -35,6 +30,6 @@ pub fn keypress(char: u8) void {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn initialize() void {
|
pub fn initialize() void {
|
||||||
interrupt.registerIRQ(1, ps2.keyboard_handler);
|
@import("x86").interrupt.registerIRQ(1, ps2.keyboard_handler);
|
||||||
print("> ");
|
print("> ");
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,8 @@
|
||||||
pub const vga = @import("vga.zig");
|
usingnamespace @import("vga.zig");
|
||||||
pub const main = @import("main.zig");
|
const main = @import("main.zig");
|
||||||
pub const multiboot = @import("multiboot.zig");
|
const multiboot = @import("multiboot.zig");
|
||||||
|
const console = @import("console.zig");
|
||||||
|
const pci = @import("pci.zig");
|
||||||
|
const ps2 = @import("ps2.zig");
|
||||||
|
|
||||||
|
const assert = @import("std").debug.assert;
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,5 @@
|
||||||
usingnamespace @import("multiboot.zig");
|
usingnamespace @import("kernel");
|
||||||
usingnamespace @import("vga.zig");
|
const x86 = @import("x86");
|
||||||
const pci = @import("pci.zig");
|
|
||||||
const arch = @import("arch/x86/lib/index.zig");
|
|
||||||
const console = @import("console.zig");
|
|
||||||
const x86 = @import("arch/x86/main.zig");
|
|
||||||
const assert = @import("std").debug.assert;
|
|
||||||
|
|
||||||
// arch independant initialization
|
// arch independant initialization
|
||||||
export fn kmain(magic: u32, info: *const MultibootInfo) noreturn {
|
export fn kmain(magic: u32, info: *const MultibootInfo) noreturn {
|
||||||
|
|
|
||||||
|
|
@ -1,18 +1,13 @@
|
||||||
// MULTIBOOT1
|
// MULTIBOOT1
|
||||||
// https://www.gnu.org/software/grub/manual/multiboot/multiboot.html
|
// https://www.gnu.org/software/grub/manual/multiboot/multiboot.html
|
||||||
|
|
||||||
// zig fmt: off
|
|
||||||
const tty = @import("tty.zig");
|
|
||||||
const cstr = @import("std").cstr;
|
|
||||||
const Process = @import("process.zig").Process;
|
|
||||||
|
|
||||||
// This should be in EAX.
|
// This should be in EAX.
|
||||||
pub const MULTIBOOT_BOOTLOADER_MAGIC = 0x2BADB002;
|
pub const MULTIBOOT_BOOTLOADER_MAGIC = 0x2BADB002;
|
||||||
|
|
||||||
// Is there basic lower/upper memory information?
|
// Is there basic lower/upper memory information?
|
||||||
pub const MULTIBOOT_INFO_MEMORY = 0x00000001;
|
pub const MULTIBOOT_INFO_MEMORY = 0x00000001;
|
||||||
// Is there a full memory map?
|
// Is there a full memory map?
|
||||||
pub const MULTIBOOT_INFO_MEM_MAP = 0x00000040;
|
pub const MULTIBOOT_INFO_MEM_MAP = 0x00000040;
|
||||||
|
|
||||||
// System information structure passed by the bootloader.
|
// System information structure passed by the bootloader.
|
||||||
pub const MultibootInfo = packed struct {
|
pub const MultibootInfo = packed struct {
|
||||||
|
|
@ -32,33 +27,33 @@ pub const MultibootInfo = packed struct {
|
||||||
|
|
||||||
// Boot-Module list.
|
// Boot-Module list.
|
||||||
mods_count: u32,
|
mods_count: u32,
|
||||||
mods_addr: u32,
|
mods_addr: u32,
|
||||||
|
|
||||||
syms: extern union {
|
syms: extern union {
|
||||||
// present if flags[4]
|
// present if flags[4]
|
||||||
nlist: extern struct {
|
nlist: extern struct {
|
||||||
tabsize: u32,
|
tabsize: u32,
|
||||||
strsize: u32,
|
strsize: u32,
|
||||||
addr: u32,
|
addr: u32,
|
||||||
_reserved: u32,
|
_reserved: u32,
|
||||||
},
|
},
|
||||||
// present if flags[5]
|
// present if flags[5]
|
||||||
shdr: extern struct {
|
shdr: extern struct {
|
||||||
num: u32,
|
num: u32,
|
||||||
size: u32,
|
size: u32,
|
||||||
addr: u32,
|
addr: u32,
|
||||||
shndx: u32,
|
shndx: u32,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
// Memory Mapping buffer.
|
// Memory Mapping buffer.
|
||||||
// present if flags[6]
|
// present if flags[6]
|
||||||
mmap_length: u32,
|
mmap_length: u32,
|
||||||
mmap_addr: u32,
|
mmap_addr: u32,
|
||||||
|
|
||||||
// Drive Info buffer.
|
// Drive Info buffer.
|
||||||
drives_length: u32,
|
drives_length: u32,
|
||||||
drives_addr: u32,
|
drives_addr: u32,
|
||||||
|
|
||||||
// ROM configuration table.
|
// ROM configuration table.
|
||||||
config_table: u32,
|
config_table: u32,
|
||||||
|
|
@ -70,9 +65,9 @@ pub const MultibootInfo = packed struct {
|
||||||
apm_table: u32,
|
apm_table: u32,
|
||||||
|
|
||||||
// Video.
|
// Video.
|
||||||
vbe_control_info: u32,
|
vbe_control_info: u32,
|
||||||
vbe_mode_info: u32,
|
vbe_mode_info: u32,
|
||||||
vbe_mode: u16,
|
vbe_mode: u16,
|
||||||
vbe_interface_seg: u16,
|
vbe_interface_seg: u16,
|
||||||
vbe_interface_off: u16,
|
vbe_interface_off: u16,
|
||||||
vbe_interface_len: u16,
|
vbe_interface_len: u16,
|
||||||
|
|
@ -109,30 +104,30 @@ pub const MultibootInfo = packed struct {
|
||||||
|
|
||||||
// Types of memory map entries.
|
// Types of memory map entries.
|
||||||
pub const MULTIBOOT_MEMORY_AVAILABLE = 1;
|
pub const MULTIBOOT_MEMORY_AVAILABLE = 1;
|
||||||
pub const MULTIBOOT_MEMORY_RESERVED = 2;
|
pub const MULTIBOOT_MEMORY_RESERVED = 2;
|
||||||
|
|
||||||
// Entries in the memory map.
|
// Entries in the memory map.
|
||||||
pub const MultibootMMapEntry = packed struct {
|
pub const MultibootMMapEntry = packed struct {
|
||||||
size: u32,
|
size: u32,
|
||||||
addr: u64,
|
addr: u64,
|
||||||
len: u64,
|
len: u64,
|
||||||
type: u32,
|
type: u32,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub const MultibootModule = packed struct {
|
pub const MultibootModule = packed struct {
|
||||||
// The memory used goes from bytes 'mod_start' to 'mod_end-1' inclusive.
|
// The memory used goes from bytes 'mod_start' to 'mod_end-1' inclusive.
|
||||||
mod_start: u32,
|
mod_start: u32,
|
||||||
mod_end: u32,
|
mod_end: u32,
|
||||||
|
|
||||||
cmdline: u32, // Module command line.
|
cmdline: u32, // Module command line.
|
||||||
pad: u32, // Padding to take it to 16 bytes (must be zero).
|
pad: u32, // Padding to take it to 16 bytes (must be zero).
|
||||||
};
|
};
|
||||||
|
|
||||||
// Multiboot structure to be read by the bootloader.
|
// Multiboot structure to be read by the bootloader.
|
||||||
const MultibootHeader = packed struct {
|
const MultibootHeader = packed struct {
|
||||||
magic: u32, // Must be equal to header magic number.
|
magic: u32, // Must be equal to header magic number.
|
||||||
flags: u32, // Feature flags.
|
flags: u32, // Feature flags.
|
||||||
checksum: u32, // Above fields plus this one must equal 0 mod 2^32.
|
checksum: u32, // Above fields plus this one must equal 0 mod 2^32.
|
||||||
// following fields are used if flag bit 16 is specified
|
// following fields are used if flag bit 16 is specified
|
||||||
header_addr: u32 = 0,
|
header_addr: u32 = 0,
|
||||||
load_addr: u32 = 0,
|
load_addr: u32 = 0,
|
||||||
|
|
@ -144,15 +139,15 @@ const MultibootHeader = packed struct {
|
||||||
|
|
||||||
// Place the header at the very beginning of the binary.
|
// Place the header at the very beginning of the binary.
|
||||||
export const multiboot_header align(4) linksection(".multiboot") = multiboot: {
|
export const multiboot_header align(4) linksection(".multiboot") = multiboot: {
|
||||||
const MAGIC = u32(0x1BADB002); // multiboot magic
|
const MAGIC = u32(0x1BADB002); // multiboot magic
|
||||||
const ALIGN = u32(1 << 0); // Align loaded modules at 4k
|
const ALIGN = u32(1 << 0); // Align loaded modules at 4k
|
||||||
const MEMINFO = u32(1 << 1); // Receive a memory map from the bootloader.
|
const MEMINFO = u32(1 << 1); // Receive a memory map from the bootloader.
|
||||||
const ADDR = u32(1 << 16); // Load specific addr
|
const ADDR = u32(1 << 16); // Load specific addr
|
||||||
const FLAGS = ALIGN | MEMINFO; // Combine the flags.
|
const FLAGS = ALIGN | MEMINFO; // Combine the flags.
|
||||||
|
|
||||||
break :multiboot MultibootHeader {
|
break :multiboot MultibootHeader{
|
||||||
.magic = MAGIC,
|
.magic = MAGIC,
|
||||||
.flags = FLAGS,
|
.flags = FLAGS,
|
||||||
.checksum = ~(MAGIC +% FLAGS) +% 1,
|
.checksum = ~(MAGIC +% FLAGS) +% 1,
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,10 @@
|
||||||
const arch = @import("arch/x86/lib/index.zig");
|
usingnamespace @import("kernel");
|
||||||
|
const arch = @import("x86");
|
||||||
|
const std = @import("std");
|
||||||
|
const virtio = @import("virtio.zig");
|
||||||
|
|
||||||
const PCI_CONFIG_ADDRESS = 0xCF8;
|
const PCI_CONFIG_ADDRESS = 0xCF8;
|
||||||
const PCI_CONFIG_DATA = 0xCFC;
|
const PCI_CONFIG_DATA = 0xCFC;
|
||||||
usingnamespace @import("vga.zig");
|
|
||||||
const virtio = @import("virtio.zig");
|
|
||||||
const std = @import("std");
|
|
||||||
|
|
||||||
// https://wiki.osdev.org/Pci
|
// https://wiki.osdev.org/Pci
|
||||||
pub const PciAddress = packed struct {
|
pub const PciAddress = packed struct {
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,5 @@
|
||||||
usingnamespace @import("vga.zig");
|
usingnamespace @import("kernel");
|
||||||
const x86 = @import("arch/x86/lib/index.zig");
|
const x86 = @import("x86");
|
||||||
const console = @import("console.zig");
|
|
||||||
|
|
||||||
const PS2_DATA = 0x60;
|
const PS2_DATA = 0x60;
|
||||||
const PS2_STATUS = 0x64;
|
const PS2_STATUS = 0x64;
|
||||||
|
|
|
||||||
10
src/vga.zig
10
src/vga.zig
|
|
@ -1,6 +1,4 @@
|
||||||
const builtin = @import("builtin");
|
const arch = @import("x86");
|
||||||
const mem = @import("std").mem;
|
|
||||||
const arch = @import("arch/x86/lib/index.zig");
|
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
|
||||||
// Screen size.
|
// Screen size.
|
||||||
|
|
@ -79,7 +77,7 @@ const VGA = struct {
|
||||||
////
|
////
|
||||||
// Clear the screen.
|
// Clear the screen.
|
||||||
pub fn clear(self: *VGA) void {
|
pub fn clear(self: *VGA) void {
|
||||||
mem.set(VGAEntry, self.vram[0..VGA_SIZE], self.entry(' '));
|
std.mem.set(VGAEntry, self.vram[0..VGA_SIZE], self.entry(' '));
|
||||||
|
|
||||||
self.cursor = 0;
|
self.cursor = 0;
|
||||||
self.updateCursor();
|
self.updateCursor();
|
||||||
|
|
@ -145,9 +143,9 @@ const VGA = struct {
|
||||||
const last = VGA_SIZE - VGA_WIDTH; // Index of last line.
|
const last = VGA_SIZE - VGA_WIDTH; // Index of last line.
|
||||||
|
|
||||||
// Copy all the screen (apart from the first line) up one line.
|
// Copy all the screen (apart from the first line) up one line.
|
||||||
mem.copy(VGAEntry, self.vram[0..last], self.vram[first..VGA_SIZE]);
|
std.mem.copy(VGAEntry, self.vram[0..last], self.vram[first..VGA_SIZE]);
|
||||||
// Clean the last line.
|
// Clean the last line.
|
||||||
mem.set(VGAEntry, self.vram[last..VGA_SIZE], self.entry(' '));
|
std.mem.set(VGAEntry, self.vram[last..VGA_SIZE], self.entry(' '));
|
||||||
|
|
||||||
// Bring the cursor back to the beginning of the last line.
|
// Bring the cursor back to the beginning of the last line.
|
||||||
self.cursor -= VGA_WIDTH;
|
self.cursor -= VGA_WIDTH;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue