Skip to content

Commit

Permalink
x86_64: Use Rust to setup SSE2
Browse files Browse the repository at this point in the history
Now that we are using the x86_64 crate, we don't need ASM to set the
flags on the CR0 and CR4 registers. This also makes the code much more
readable.
  • Loading branch information
josephlr committed Feb 28, 2020
1 parent 0266f02 commit 7f14f86
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
11 changes: 0 additions & 11 deletions src/asm/ram64.s
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,6 @@ ram64_start:
movb $'L', %al
outb %al, %dx

# Enable SSE2 for XMM registers (needed for EFI calling)
# Clear CR0.EM and Set CR0.MP
movq %cr0, %rax
andb $0b11111011, %al # Clear bit 2
orb $0b00000010, %al # Set bit 1
movq %rax, %cr0
# Set CR4.OSFXSR and CR4.OSXMMEXCPT
movq %cr4, %rax
orb $0b00000110, %ah # Set bits 9 and 10
movq %rax, %cr4

# Setup the stack (at the end of our RAM region)
movq $ram_max, %rsp

Expand Down
18 changes: 17 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@

use core::panic::PanicInfo;

use x86_64::instructions::hlt;
use x86_64::{
instructions::hlt,
registers::control::{Cr0, Cr0Flags, Cr4, Cr4Flags},
};

#[macro_use]
mod serial;
Expand Down Expand Up @@ -83,6 +86,18 @@ fn setup_pagetables() {
log!("Page tables setup");
}

// Enable SSE2 for XMM registers (needed for EFI calling)
fn enable_sse() {
let mut cr0 = Cr0::read();
cr0.remove(Cr0Flags::EMULATE_COPROCESSOR);
cr0.insert(Cr0Flags::MONITOR_COPROCESSOR);
unsafe { Cr0::write(cr0) };
let mut cr4 = Cr4::read();
cr4.insert(Cr4Flags::OSFXSR);
cr4.insert(Cr4Flags::OSXMMEXCPT_ENABLE);
unsafe { Cr4::write(cr4) };
}

const VIRTIO_PCI_VENDOR_ID: u16 = 0x1af4;
const VIRTIO_PCI_BLOCK_DEVICE_ID: u16 = 0x1042;

Expand Down Expand Up @@ -166,6 +181,7 @@ fn boot_from_device(device: &mut block::VirtioBlockDevice) -> bool {
#[cfg_attr(not(test), no_mangle)]
pub extern "C" fn rust64_start() -> ! {
log!("\nStarting..");
enable_sse();
setup_pagetables();

pci::print_bus();
Expand Down

0 comments on commit 7f14f86

Please sign in to comment.