FIXED THE APIC HELL YEAH
This commit is contained in:
Generated
+1
@@ -6,6 +6,7 @@
|
||||
<sourceFolder url="file://$MODULE_DIR$/lib_example/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/libk/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/libm/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/kernel_fonts/src" isTestSource="false" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/target" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
|
||||
Generated
+5
@@ -0,0 +1,5 @@
|
||||
<component name="ProjectCodeStyleConfiguration">
|
||||
<state>
|
||||
<option name="PREFERRED_PROJECT_CODE_STYLE" value="Default" />
|
||||
</state>
|
||||
</component>
|
||||
@@ -1,7 +1,7 @@
|
||||
use core::arch::x86_64::__cpuid;
|
||||
|
||||
use crate::arch::x86_64::memory::mapping::PHYSICAL_MEMORY_OFFSET;
|
||||
use crate::serial_print;
|
||||
use crate::{serial_print, serial_println};
|
||||
use x86_64::{
|
||||
PhysAddr, VirtAddr,
|
||||
structures::paging::{Mapper, Page, PageTableFlags, PhysFrame, Size4KiB},
|
||||
@@ -40,19 +40,21 @@ fn get_apic_base() -> PhysAddr {
|
||||
PhysAddr::new(value & 0xfffff0000)
|
||||
}
|
||||
|
||||
fn write_apic_register(apic_base: &VirtAddr, reg: u8, value: u32) {
|
||||
fn write_apic_register(apic_base: &PhysAddr, reg: u8, value: u32) {
|
||||
let apic_base = apic_base.as_u64();
|
||||
let reg_addr = (apic_base & 0xFFFFF0000) + reg as u64;
|
||||
unsafe { *(reg_addr as *mut u32) = value };
|
||||
let virt_addr = unsafe { phys_to_virt(PhysAddr::new(reg_addr)) };
|
||||
unsafe { *(virt_addr.as_u64() as *mut u32) = value };
|
||||
}
|
||||
|
||||
fn read_apic_register(apic_base: &VirtAddr, reg: u8) -> u32 {
|
||||
fn read_apic_register(apic_base: &PhysAddr, reg: u8) -> u32 {
|
||||
let apic_base = apic_base.as_u64();
|
||||
|
||||
serial_print!("got apic base");
|
||||
|
||||
let reg_addr = (apic_base & 0xFFFFF0000) + reg as u64;
|
||||
unsafe { *(reg_addr as *const u32) }
|
||||
|
||||
serial_println!("reading: {:?}", VirtAddr::new(reg_addr));
|
||||
|
||||
let virt_addr = unsafe { phys_to_virt(PhysAddr::new(reg_addr)) };
|
||||
unsafe { *(virt_addr.as_u64() as *const u32) }
|
||||
}
|
||||
|
||||
pub fn check_apic() -> bool {
|
||||
@@ -74,27 +76,15 @@ pub fn enable_apic() {
|
||||
let apic_phys_addr = get_apic_base();
|
||||
set_apic_base_enable(apic_phys_addr);
|
||||
// map virt address of apic
|
||||
|
||||
let apic_virt = unsafe { phys_to_virt(apic_phys_addr) };
|
||||
let page: Page<Size4KiB> = Page::containing_address(apic_virt);
|
||||
let frame: PhysFrame<Size4KiB> = PhysFrame::containing_address(apic_phys_addr);
|
||||
let flags: PageTableFlags = PageTableFlags::PRESENT | PageTableFlags::WRITABLE;
|
||||
|
||||
unsafe {
|
||||
match mapper.map_to(page, frame, flags, &mut *frame_allocator) {
|
||||
Ok(_) => {}
|
||||
Err(why) => panic!("failed to map apic: {:?}", why),
|
||||
}
|
||||
}
|
||||
serial_println!("Trying to map: {:?} -> {:?}", apic_virt, apic_phys_addr);
|
||||
|
||||
// // FIXME: this causes a page fault
|
||||
// // TODO: map to virtual memory
|
||||
// FIXME: this causes a page fault
|
||||
// TODO: map to virtual memory
|
||||
|
||||
let reg = read_apic_register(&apic_virt, 0xF0);
|
||||
|
||||
serial_print!("ok2");
|
||||
|
||||
write_apic_register(&apic_virt, 0xF0, reg | 0x100);
|
||||
let reg = read_apic_register(&apic_phys_addr, 0xF0);
|
||||
write_apic_register(&apic_phys_addr, 0xF0, reg | 0x100);
|
||||
}
|
||||
|
||||
pub struct Apic {}
|
||||
|
||||
+7
-6
@@ -22,6 +22,7 @@ use core::arch::asm;
|
||||
use limine::BaseRevision;
|
||||
use x86_64::VirtAddr;
|
||||
use arch::x86_64::memory::allocation::page_alloc::FoundryOSFrameAllocator;
|
||||
use crate::arch::x86_64::cpu::apic::enable_apic;
|
||||
|
||||
pub mod arch;
|
||||
pub mod resources;
|
||||
@@ -99,17 +100,17 @@ pub fn boot() -> Result<(), &'static str> {
|
||||
}
|
||||
println_log!("[Success]");
|
||||
|
||||
print_log!(" Enabling PICs... ");
|
||||
interrupts::enable_pic();
|
||||
println_log!("[Success]");
|
||||
// print_log!(" Enabling PICs... ");
|
||||
// interrupts::enable_pic();
|
||||
// println_log!("[Success]");
|
||||
|
||||
// print_log!(" Disabling PICs... ");
|
||||
// interrupts::disable_pic();
|
||||
// println_log!("[Success]");
|
||||
|
||||
// print_log!(" Initialising APIC");
|
||||
// enable_apic();
|
||||
// println_log!("[Success]");
|
||||
print_log!(" Initialising APIC... ");
|
||||
enable_apic();
|
||||
println_log!("[Success]");
|
||||
|
||||
print_log!(" Enabling Interrupts... ");
|
||||
x86_64::instructions::interrupts::enable();
|
||||
|
||||
+31
-46
@@ -40,13 +40,13 @@ pub fn include_font(item: TokenStream) -> TokenStream {
|
||||
filename.value()
|
||||
);
|
||||
|
||||
let font_bytes = match load_file(file_path) {
|
||||
let font_bytes = match std::fs::read(file_path) {
|
||||
Ok(bytes) => bytes,
|
||||
Err(why) => panic!("{}", why),
|
||||
};
|
||||
|
||||
let font_data = match Font::new(font_bytes) {
|
||||
Ok(font) => font.0,
|
||||
let font_data = match FontBuilder::load(font_bytes) {
|
||||
Ok(font) => font.data,
|
||||
Err(why) => panic!("{}", why),
|
||||
};
|
||||
|
||||
@@ -66,40 +66,39 @@ struct FontData {
|
||||
width: u8,
|
||||
height: u8,
|
||||
length: u16,
|
||||
data: Vec<Glyph>,
|
||||
unicode_table: [[u8; 2]; 512],
|
||||
pub data: [[u8; 16]; 512],
|
||||
}
|
||||
|
||||
struct Glyph {
|
||||
bytes: Vec<u8>,
|
||||
}
|
||||
|
||||
enum FontBuilder {
|
||||
Psf1,
|
||||
Psf2,
|
||||
Psf1(FontData),
|
||||
Psf2(FontData),
|
||||
}
|
||||
|
||||
impl FontBuilder {
|
||||
const PSF1_MAGIC: u16 = 0x3604;
|
||||
const PSF2_MAGIC: u32 = 0x72b54a86;
|
||||
|
||||
pub fn load() -> Option<Self> {
|
||||
None
|
||||
fn revision(data: &[u8]) -> u8 {
|
||||
if (data[0] as u16) << 8 | data[1] as u16 == Self::PSF1_MAGIC {
|
||||
1
|
||||
} else if (data[0] as u32) << 24 | (data[1] as u32) << 16 | (data[2] as u32) << 8 | data[3] as u32 == Self::PSF2_MAGIC {
|
||||
2
|
||||
} else {
|
||||
0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Font {
|
||||
const MAGIC: u16 = 0x3604;
|
||||
pub fn load(data: Vec<u8>) -> Result<FontData, &'static str> {
|
||||
match Self::revision(&data[0..4]) {
|
||||
1 => Self::parse_psf1(&data),
|
||||
2 => Self::parse_psf2(&data),
|
||||
_ => panic!("invalid font revision result"),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new(data: [u8; (32 + 2) * 512 + 4]) -> Result<Self, &'static str> {
|
||||
let magic: u16 = (data[0] as u16) << 8 | data[1] as u16;
|
||||
fn parse_psf1(data: &[u8]) -> Result<FontData, &'static str> {
|
||||
let mode = data[2];
|
||||
let size = data[3];
|
||||
|
||||
if magic != Self::MAGIC {
|
||||
return Err("Magic value is invalid!");
|
||||
}
|
||||
|
||||
let has_512_glyphs = (mode & 0x01) != 0;
|
||||
let mut glyphs = [[0; 16]; 512];
|
||||
let glyph_count = if has_512_glyphs { 512 } else { 256 };
|
||||
@@ -112,29 +111,15 @@ impl Font {
|
||||
glyphs[i] = buff;
|
||||
}
|
||||
|
||||
Ok(Self(glyphs))
|
||||
}
|
||||
}
|
||||
|
||||
type FileContents = [u8; (32 + 2) * 512 + 4];
|
||||
fn load_file(filename: String) -> Result<FileContents, std::io::Error> {
|
||||
let mut buf = [0; (32 + 2) * 512 + 4];
|
||||
let mut f = File::open(filename).unwrap();
|
||||
f.seek(SeekFrom::Start(0)).unwrap();
|
||||
|
||||
loop {
|
||||
match f.read(&mut buf) {
|
||||
Ok(read) => {
|
||||
if read == 0 {
|
||||
break;
|
||||
}
|
||||
}
|
||||
Err(why) => {
|
||||
eprintln!("Failed to read PS1 font file: {}", why);
|
||||
return Err(why);
|
||||
}
|
||||
}
|
||||
Ok(FontData {
|
||||
width: 8,
|
||||
height: size,
|
||||
length: glyph_count,
|
||||
data: glyphs,
|
||||
})
|
||||
}
|
||||
|
||||
Ok(buf)
|
||||
fn parse_psf2(data: &[u8]) -> Result<FontData, &'static str> {
|
||||
Err("PSF2 support is not implemented yet!")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user