Skip to content

Commit 11b01a5

Browse files
committed
ram64: Use an external file for 64-bit assembly
This allows us to define all of our long-mode assembly in a separate file, making maintaince easier. Signed-off-by: Joe Richey <joerichey@google.com>
1 parent 89724ed commit 11b01a5

File tree

3 files changed

+43
-29
lines changed

3 files changed

+43
-29
lines changed

layout.ld

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
ENTRY(_start)
1+
ENTRY(ram64_start)
22

33
PHDRS
44
{
@@ -25,7 +25,10 @@ SECTIONS
2525

2626
.rodata : { *(.rodata .rodata.*) } :rodata
2727
.data : { *(.data .data.*) *(.bss .bss.*) } :data
28-
.text : { *(.text .text.*) } :text
28+
.text : {
29+
*(.text .text.*)
30+
*(.ram64)
31+
} :text
2932

3033
firmware_ram_size = . - ram_min;
3134

src/asm/ram64.s

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
.section .ram64, "ax"
2+
.global ram64_start
3+
.code64
4+
5+
ram64_start:
6+
# Indicate (via serial) that we are in long/64-bit mode
7+
movw $0x3f8, %dx
8+
movb $'L', %al
9+
outb %al, %dx
10+
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+
22+
# Setup the stack (at the end of our RAM region)
23+
movq $ram_max, %rsp
24+
25+
jmp rust64_start
26+
27+
halt_loop:
28+
hlt
29+
jmp halt_loop

src/main.rs

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
#![feature(asm)]
15+
#![feature(global_asm)]
1616
#![cfg_attr(not(test), no_std)]
1717
#![cfg_attr(not(test), no_main)]
1818
#![cfg_attr(test, allow(unused_imports))]
@@ -38,28 +38,16 @@ mod pci;
3838
mod pe;
3939
mod virtio;
4040

41-
fn halt() -> ! {
42-
loop {
43-
unsafe { asm!("hlt") };
44-
}
41+
global_asm!(include_str!("asm/ram64.s"));
42+
43+
extern "C" {
44+
fn halt_loop() -> !;
4545
}
4646

4747
#[cfg_attr(not(test), panic_handler)]
4848
fn panic(info: &PanicInfo) -> ! {
4949
log!("PANIC: {}", info);
50-
halt()
51-
}
52-
53-
/// Enable SSE2 for XMM registers (needed for EFI calling)
54-
fn enable_sse2() {
55-
unsafe {
56-
asm!("movq %cr0, %rax");
57-
asm!("or $$0x2, %ax");
58-
asm!("movq %rax, %cr0");
59-
asm!("movq %cr4, %rax");
60-
asm!("or $$0x600, %ax");
61-
asm!("movq %rax, %cr4");
62-
}
50+
unsafe { halt_loop() }
6351
}
6452

6553
/// Setup page tables to provide an identity mapping over the full 4GiB range
@@ -167,14 +155,8 @@ fn boot_from_device(device: &mut block::VirtioBlockDevice) -> bool {
167155
}
168156

169157
#[cfg_attr(not(test), no_mangle)]
170-
pub extern "C" fn _start() -> ! {
171-
unsafe {
172-
asm!("movq $$0x180000, %rsp");
173-
}
174-
175-
log!("Starting..");
176-
177-
enable_sse2();
158+
pub extern "C" fn rust64_start() -> ! {
159+
log!("\nStarting..");
178160
setup_pagetables();
179161

180162
pci::print_bus();
@@ -194,5 +176,5 @@ pub extern "C" fn _start() -> ! {
194176
let mut device = block::VirtioBlockDevice::new(&mut mmio_transport);
195177
boot_from_device(&mut device);
196178

197-
halt()
179+
unsafe { halt_loop() }
198180
}

0 commit comments

Comments
 (0)