update v2

This commit is contained in:
2025-07-05 14:31:39 +09:00
parent 163ac1668d
commit 152d6de44d
32 changed files with 455 additions and 2121 deletions

13
kernel/.cargo/config.toml Normal file
View File

@ -0,0 +1,13 @@
[build]
target = "x86_64-unknown-none"
[target.x86_64-unknown-none]
rustflags = [
"-C", "link-arg=-T/Users/syui/git/aios/kernel/kernel.ld",
"-C", "relocation-model=static",
"-C", "code-model=kernel"
]
[unstable]
build-std = ["core", "compiler_builtins"]
build-std-features = ["compiler-builtins-mem"]

14
kernel/Cargo.toml Normal file
View File

@ -0,0 +1,14 @@
[package]
name = "aios-kernel"
version = "0.2.0"
edition = "2021"
[[bin]]
name = "aios-kernel"
path = "src/main.rs"
[dependencies]
bootloader = "0.9.23"
[package.metadata.bootimage]
test-args = ["-nographic"]

22
kernel/i686-aios.json Normal file
View File

@ -0,0 +1,22 @@
{
"llvm-target": "i686-unknown-none",
"target-endian": "little",
"target-pointer-width": "32",
"target-c-int-width": "32",
"data-layout": "e-m:e-p:32:32-f64:32:64-f80:32-n8:16:32-S128",
"arch": "x86",
"os": "none",
"env": "",
"vendor": "unknown",
"linker-flavor": "ld.lld",
"linker": "rust-lld",
"panic-strategy": "abort",
"disable-redzone": true,
"features": "-mmx,-sse,-sse2",
"executables": true,
"relocation-model": "static",
"code-model": "small",
"pre-link-args": {
"ld.lld": ["-Tkernel.ld"]
}
}

35
kernel/kernel.ld Normal file
View File

@ -0,0 +1,35 @@
/* AIOS v2 Kernel Linker Script */
ENTRY(_start)
SECTIONS
{
. = 1M;
.multiboot :
{
/* Ensure the multiboot header is at the very beginning */
KEEP(*(.multiboot))
}
.text : ALIGN(0x1000)
{
*(.text .text.*)
}
.rodata : ALIGN(0x1000)
{
*(.rodata .rodata.*)
}
.data : ALIGN(0x1000)
{
*(.data .data.*)
}
.bss : ALIGN(0x1000)
{
*(COMMON)
*(.bss .bss.*)
}
}

27
kernel/src/boot.s Normal file
View File

@ -0,0 +1,27 @@
# Multiboot header
.section .multiboot
.align 8
multiboot_header:
.long 0x1BADB002 # magic
.long 0x00 # flags
.long -(0x1BADB002 + 0x00) # checksum
# Entry point
.section .text
.global _start
.code64
_start:
# Disable interrupts
cli
# Load null descriptor into data segment registers
xor %ax, %ax
mov %ax, %ds
mov %ax, %es
mov %ax, %ss
mov %ax, %fs
mov %ax, %gs
# Halt
hlt
1: jmp 1b

34
kernel/src/boot32.s Normal file
View File

@ -0,0 +1,34 @@
.code32
.section .multiboot, "ax"
.align 4
multiboot_header:
.long 0x1BADB002 # magic
.long 0x00 # flags
.long -(0x1BADB002 + 0x00) # checksum
.section .text
.global _start
_start:
# We start in 32-bit mode
cli
# Set up a simple stack
movl $stack_top, %esp
# Write 'OK' to VGA buffer
movl $0xb8000, %edi
movb $'O', (%edi)
movb $0x0f, 1(%edi)
movb $'K', 2(%edi)
movb $0x0f, 3(%edi)
# Halt
hang:
hlt
jmp hang
.section .bss
.align 16
stack_bottom:
.space 4096
stack_top:

130
kernel/src/console.rs Normal file
View File

@ -0,0 +1,130 @@
//! Simple VGA console for AIOS v2
//! No external crates, pure Rust implementation
use core::fmt::{self, Write};
const VGA_BUFFER: *mut u16 = 0xb8000 as *mut u16;
const VGA_WIDTH: usize = 80;
const VGA_HEIGHT: usize = 25;
static mut CONSOLE: Console = Console {
col: 0,
row: 0,
color: 0x0f, // White on black
};
struct Console {
col: usize,
row: usize,
color: u8,
}
impl Console {
fn clear(&mut self) {
for i in 0..(VGA_WIDTH * VGA_HEIGHT) {
unsafe {
VGA_BUFFER.add(i).write_volatile(0x0f00); // Clear with white on black
}
}
self.col = 0;
self.row = 0;
}
fn write_byte(&mut self, byte: u8) {
match byte {
b'\n' => {
self.col = 0;
self.row += 1;
if self.row >= VGA_HEIGHT {
self.scroll_up();
}
}
byte => {
if self.col >= VGA_WIDTH {
self.col = 0;
self.row += 1;
if self.row >= VGA_HEIGHT {
self.scroll_up();
}
}
let pos = self.row * VGA_WIDTH + self.col;
let entry = (self.color as u16) << 8 | byte as u16;
unsafe {
VGA_BUFFER.add(pos).write_volatile(entry);
}
self.col += 1;
}
}
}
fn scroll_up(&mut self) {
// Move all lines up by one
for row in 1..VGA_HEIGHT {
for col in 0..VGA_WIDTH {
let pos = row * VGA_WIDTH + col;
let prev_pos = (row - 1) * VGA_WIDTH + col;
unsafe {
let entry = VGA_BUFFER.add(pos).read_volatile();
VGA_BUFFER.add(prev_pos).write_volatile(entry);
}
}
}
// Clear last line
for col in 0..VGA_WIDTH {
let pos = (VGA_HEIGHT - 1) * VGA_WIDTH + col;
unsafe {
VGA_BUFFER.add(pos).write_volatile(0x0f00);
}
}
self.row = VGA_HEIGHT - 1;
}
}
impl Write for Console {
fn write_str(&mut self, s: &str) -> fmt::Result {
for byte in s.bytes() {
self.write_byte(byte);
}
Ok(())
}
}
pub fn init() {
unsafe {
CONSOLE.clear();
}
}
pub fn print(s: &str) {
unsafe {
CONSOLE.write_str(s).unwrap();
}
}
#[macro_export]
macro_rules! kprint {
($($arg:tt)*) => {
$crate::console::_print(format_args!($($arg)*))
};
}
#[macro_export]
macro_rules! kprintln {
() => ($crate::console::print("\n"));
($($arg:tt)*) => ({
$crate::console::_print(format_args!($($arg)*));
$crate::console::print("\n");
})
}
#[doc(hidden)]
pub fn _print(args: fmt::Arguments) {
unsafe {
CONSOLE.write_fmt(args).unwrap();
}
}

58
kernel/src/main.rs Normal file
View File

@ -0,0 +1,58 @@
//! AIOS v2 Kernel - Using bootloader crate
#![no_std]
#![no_main]
use core::panic::PanicInfo;
// Simple VGA buffer writer
struct VgaBuffer {
chars: [[u16; 80]; 25],
}
static mut VGA_BUFFER: *mut VgaBuffer = 0xb8000 as *mut VgaBuffer;
fn print_char(c: u8, x: usize, y: usize) {
unsafe {
let vga = &mut *VGA_BUFFER;
vga.chars[y][x] = (0x0f << 8) | (c as u16);
}
}
fn print_str(s: &str, x: usize, y: usize) {
for (i, byte) in s.bytes().enumerate() {
print_char(byte, x + i, y);
}
}
// Entry point
#[no_mangle]
pub extern "C" fn _start() -> ! {
// Clear screen
for y in 0..25 {
for x in 0..80 {
print_char(b' ', x, y);
}
}
// Print welcome message
print_str("AIOS v2 - AI Operating System", 25, 12);
print_str("Kernel started successfully!", 26, 13);
loop {
unsafe {
core::arch::asm!("hlt");
}
}
}
// Panic handler
#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
print_str("KERNEL PANIC!", 33, 15);
loop {
unsafe {
core::arch::asm!("hlt");
}
}
}

7
kernel/src/memory.rs Normal file
View File

@ -0,0 +1,7 @@
//! Simple memory management for AIOS v2
//! Minimal implementation for initial boot
pub fn init() {
// TODO: Implement proper memory management
// For now, just a placeholder to complete initialization sequence
}

27
kernel/src/panic.rs Normal file
View File

@ -0,0 +1,27 @@
//! Panic handler for AIOS v2
use core::panic::PanicInfo;
use crate::console;
#[panic_handler]
fn panic(info: &PanicInfo) -> ! {
console::print("\n!!! KERNEL PANIC !!!\n");
if let Some(location) = info.location() {
console::print("Location: ");
console::print(location.file());
console::print(":");
// TODO: Convert line number to string
console::print("\n");
}
console::print("Message: ");
console::print("Kernel panic occurred\n");
console::print("System halted.\n");
loop {
unsafe {
core::arch::asm!("hlt");
}
}
}