Add syscall handler, still work in progress

Add elf files
This commit is contained in:
Jeremy Soller 2016-08-18 19:44:31 -06:00
parent 71a17070b1
commit 5588c38178
11 changed files with 263 additions and 18 deletions

View file

@ -85,7 +85,7 @@ pub enum MadtEntry {
InvalidIoApic(usize),
IntSrcOverride(&'static MadtIntSrcOverride),
InvalidIntSrcOverride(usize),
Unknown
Unknown(u8)
}
pub struct MadtIter {
@ -117,7 +117,7 @@ impl Iterator for MadtIter {
} else {
MadtEntry::InvalidIntSrcOverride(entry_len)
},
_ => MadtEntry::Unknown
_ => MadtEntry::Unknown(entry_type)
};
self.i += entry_len;

92
arch/x86_64/src/elf.rs Normal file
View file

@ -0,0 +1,92 @@
pub const ELF_CLASS: u8 = 2;
pub type ElfAddr = u64;
pub type ElfOff = u64;
pub type ElfHalf = u16;
pub type ElfWord = u32;
pub type ElfXword = u64;
/// An ELF header
#[repr(packed)]
#[derive(Debug)]
pub struct ElfHeader {
/// The "magic number" (4 bytes)
pub magic: [u8; 4],
/// 64 or 32 bit?
pub class: u8,
/// Little (1) or big endianness (2)?
pub endian: u8,
/// The ELF version (set to 1 for default)
pub ver: u8,
/// Operating system ABI (0x03 for Linux)
pub abi: [u8; 2],
/// Unused
pub pad: [u8; 7],
/// Specify whether the object is relocatable, executable, shared, or core (in order).
pub _type: ElfHalf,
/// Instruction set archcitecture
pub machine: ElfHalf,
/// Second version
pub ver_2: ElfWord,
/// The ELF entry
pub entry: ElfAddr,
/// The program header table offset
pub ph_off: ElfOff,
/// The section header table offset
pub sh_off: ElfOff,
/// The flags set
pub flags: ElfWord,
/// The header table length
pub h_len: ElfHalf,
/// The program header table entry length
pub ph_ent_len: ElfHalf,
/// The program head table length
pub ph_len: ElfHalf,
/// The section header table entry length
pub sh_ent_len: ElfHalf,
/// The section header table length
pub sh_len: ElfHalf,
/// The section header table string index
pub sh_str_index: ElfHalf,
}
/// An ELF segment
#[repr(packed)]
#[derive(Debug)]
pub struct ElfSegment {
pub _type: ElfWord,
pub flags: ElfWord,
pub off: ElfOff,
pub vaddr: ElfAddr,
pub paddr: ElfAddr,
pub file_len: ElfXword,
pub mem_len: ElfXword,
pub align: ElfXword,
}
/// An ELF section
#[repr(packed)]
#[derive(Debug)]
pub struct ElfSection {
pub name: ElfWord,
pub _type: ElfWord,
pub flags: ElfXword,
pub addr: ElfAddr,
pub off: ElfOff,
pub len: ElfXword,
pub link: ElfWord,
pub info: ElfWord,
pub addr_align: ElfXword,
pub ent_len: ElfXword,
}
/// An ELF symbol
#[repr(packed)]
#[derive(Debug)]
pub struct ElfSymbol {
pub name: ElfWord,
pub info: u8,
pub other: u8,
pub sh_index: ElfHalf,
pub value: ElfAddr,
pub size: ElfXword,
}

View file

@ -20,6 +20,7 @@ pub unsafe fn init() {
entry.set_flags(IDT_PRESENT | IDT_RING_0 | IDT_INTERRUPT);
entry.set_offset(8, blank as usize);
}
IDT[0x80].set_offset(8, syscall as usize);
IDTR.set_slice(&IDT);
init_ap();
@ -54,6 +55,29 @@ interrupt_error!(page_fault, {
}
});
#[naked]
pub unsafe extern fn syscall() {
extern {
fn syscall(a: usize, b: usize, c: usize, d: usize, e: usize, f: usize) -> usize;
}
let a;
let b;
let c;
let d;
let e;
let f;
asm!("" : "={rax}"(a), "={rbx}"(b), "={rcx}"(c), "={rdx}"(d), "={rsi}"(e), "={rdi}"(f)
: : : "intel", "volatile");
let a = syscall(a, b, c, d, e, f);
asm!("" : : "{rax}"(a) : : "intel", "volatile");
// Pop scratch registers, error code, and return
asm!("iretq" : : : : "intel", "volatile");
}
bitflags! {
pub flags IdtFlags: u8 {
const IDT_PRESENT = 1 << 7,

View file

@ -119,6 +119,9 @@ macro_rules! interrupt_error {
/// ACPI table parsing
pub mod acpi;
/// ELF file handling
pub mod elf;
/// Memcpy, memmove, etc.
pub mod externs;

View file

@ -27,7 +27,7 @@ extern {
/// Kernel main function
fn kmain() -> !;
/// Kernel main for APs
fn kmain_ap() -> !;
fn kmain_ap(id: usize) -> !;
}
/// The entry to Rust, all things must be initialized
@ -88,8 +88,6 @@ pub unsafe extern fn kstart() -> ! {
}
BSP_READY.store(true, Ordering::SeqCst);
print!("BSP\n");
}
kmain();
@ -115,7 +113,5 @@ pub unsafe extern fn kstart_ap(stack_start: usize, stack_end: usize) -> ! {
asm!("pause" : : : : "intel", "volatile");
}
print!("{}", ::core::str::from_utf8_unchecked(&[b'A', b'P', b' ', ap_number as u8 + b'0', b'\n']));
kmain_ap();
kmain_ap(ap_number);
}