Skip to content

Commit fb514ef

Browse files
josephlrrbradford
authored andcommitted
x86_64: Use Rust to setup SSE2
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. Signed-off-by: Joe Richey <joerichey@google.com>
1 parent 03866de commit fb514ef

File tree

2 files changed

+17
-12
lines changed

2 files changed

+17
-12
lines changed

src/asm/ram64.s

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,6 @@ ram64_start:
88
movb $'L', %al
99
outb %al, %dx
1010

11-
# Enable SSE2 for XMM registers (needed for EFI calling)
12-
# Clear CR0.EM and Set CR0.MP
13-
movq %cr0, %rax
14-
andb $0b11111011, %al # Clear bit 2
15-
orb $0b00000010, %al # Set bit 1
16-
movq %rax, %cr0
17-
# Set CR4.OSFXSR and CR4.OSXMMEXCPT
18-
movq %cr4, %rax
19-
orb $0b00000110, %ah # Set bits 9 and 10
20-
movq %rax, %cr4
21-
2211
# Setup the stack (at the end of our RAM region)
2312
movq $ram_max, %rsp
2413

src/main.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@
2020

2121
use core::panic::PanicInfo;
2222

23-
use x86_64::instructions::hlt;
23+
use x86_64::{
24+
instructions::hlt,
25+
registers::control::{Cr0, Cr0Flags, Cr4, Cr4Flags},
26+
};
2427

2528
#[macro_use]
2629
mod serial;
@@ -83,6 +86,18 @@ fn setup_pagetables() {
8386
log!("Page tables setup");
8487
}
8588

89+
// Enable SSE2 for XMM registers (needed for EFI calling)
90+
fn enable_sse() {
91+
let mut cr0 = Cr0::read();
92+
cr0.remove(Cr0Flags::EMULATE_COPROCESSOR);
93+
cr0.insert(Cr0Flags::MONITOR_COPROCESSOR);
94+
unsafe { Cr0::write(cr0) };
95+
let mut cr4 = Cr4::read();
96+
cr4.insert(Cr4Flags::OSFXSR);
97+
cr4.insert(Cr4Flags::OSXMMEXCPT_ENABLE);
98+
unsafe { Cr4::write(cr4) };
99+
}
100+
86101
const VIRTIO_PCI_VENDOR_ID: u16 = 0x1af4;
87102
const VIRTIO_PCI_BLOCK_DEVICE_ID: u16 = 0x1042;
88103

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

171187
pci::print_bus();

0 commit comments

Comments
 (0)