Skip to content

Commit 4d3bd7d

Browse files
committed
swicht from llvm_asm! to asm!
- llvm_asm is deprecated => remove it from eduOS
1 parent 6316e51 commit 4d3bd7d

File tree

7 files changed

+100
-60
lines changed

7 files changed

+100
-60
lines changed

src/arch/x86_64/irq.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,19 @@ const KERNEL_CODE_SELECTOR: SegmentSelector = SegmentSelector::new(1, Ring::Ring
2323

2424
/// Enable Interrupts
2525
pub fn irq_enable() {
26-
unsafe { llvm_asm!("sti" ::: "memory" : "volatile") };
26+
unsafe { asm!("sti", options(nomem, nostack, preserves_flags)) };
2727
}
2828

2929
/// Disable Interrupts
3030
pub fn irq_disable() {
31-
unsafe { llvm_asm!("cli" ::: "memory" : "volatile") };
31+
unsafe { asm!("cli", options(nomem, nostack, preserves_flags)) };
3232
}
3333

3434
/// Determines, if the interrupt flags (IF) is set
3535
pub fn is_irq_enabled() -> bool {
3636
let rflags: u64;
3737

38-
unsafe { llvm_asm!("pushf; pop $0": "=r"(rflags) :: "memory" : "volatile") };
38+
unsafe { asm!("pushf; pop {}", lateout(reg) rflags, options(nomem, nostack, preserves_flags)) };
3939
if (rflags & (1u64 << 9)) != 0 {
4040
return true;
4141
}

src/arch/x86_64/processor.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,17 @@ pub fn mb() {
1515
/// Search the most significant bit
1616
#[inline(always)]
1717
pub fn msb(value: u64) -> Option<u64> {
18+
println!("value {}", value);
1819
if value > 0 {
1920
let ret: u64;
2021
unsafe {
21-
llvm_asm!("bsr $1, $0" : "=r"(ret) : "r"(value) : "cc" : "volatile");
22+
asm!("bsr {0}, {1}",
23+
out(reg) ret,
24+
in(reg) value,
25+
options(nomem, nostack)
26+
);
2227
}
28+
println!("value {} {}", value, ret);
2329
Some(ret)
2430
} else {
2531
None
@@ -32,7 +38,11 @@ pub fn lsb(value: u64) -> Option<u64> {
3238
if value > 0 {
3339
let ret: u64;
3440
unsafe {
35-
llvm_asm!("bsf $1, $0" : "=r"(ret) : "r"(value) : "cc" : "volatile");
41+
asm!("bsf {0}, {1}",
42+
out(reg) ret,
43+
in(reg) value,
44+
options(nomem, nostack)
45+
);
3646
}
3747
Some(ret)
3848
} else {
@@ -42,13 +52,13 @@ pub fn lsb(value: u64) -> Option<u64> {
4252

4353
pub fn halt() {
4454
unsafe {
45-
llvm_asm!("hlt" :::: "volatile");
55+
asm!("hlt", options(nomem, nostack));
4656
}
4757
}
4858

4959
pub fn pause() {
5060
unsafe {
51-
llvm_asm!("pause" :::: "volatile");
61+
asm!("pause", options(nomem, nostack));
5262
}
5363
}
5464

src/arch/x86_64/start.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// http://opensource.org/licenses/MIT>, at your option. This file may not be
66
// copied, modified, or distributed except according to those terms.
77

8-
#[allow(unused_imports)]
8+
use crate::consts::STACK_SIZE;
99
use crate::scheduler::task::BOOT_STACK;
1010

1111
extern "C" {
@@ -14,12 +14,24 @@ extern "C" {
1414

1515
#[cfg(not(test))]
1616
#[no_mangle]
17-
#[naked]
18-
pub unsafe extern "C" fn _start() {
19-
// be sure that rsp is a valid stack pointer
20-
llvm_asm!("mov $0, %rsp" :: "r"(BOOT_STACK.top()) :: "volatile");
21-
17+
pub unsafe extern "C" fn pre_main() -> ! {
2218
main();
2319

2420
loop {}
2521
}
22+
23+
#[cfg(not(test))]
24+
#[no_mangle]
25+
#[naked]
26+
pub unsafe extern "C" fn _start() -> ! {
27+
asm!(
28+
// initialize stack pointer
29+
"mov rsp, {stack}",
30+
"add rsp, {size}",
31+
"call {pre_main}",
32+
stack = sym BOOT_STACK,
33+
size = const STACK_SIZE - 16,
34+
pre_main = sym pre_main,
35+
options(noreturn)
36+
);
37+
}

src/arch/x86_64/switch.rs

Lines changed: 61 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -5,52 +5,69 @@
55
// http://opensource.org/licenses/MIT>, at your option. This file may not be
66
// copied, modified, or distributed except according to those terms.
77

8-
#[inline(never)]
8+
macro_rules! save_context {
9+
() => {
10+
concat!(
11+
r#"
12+
pushfq
13+
push rax
14+
push rcx
15+
push rdx
16+
push rbx
17+
push rbp
18+
push rsi
19+
push rdi
20+
push r8
21+
push r9
22+
push r10
23+
push r11
24+
push r12
25+
push r13
26+
push r14
27+
push r15
28+
"#,
29+
)
30+
};
31+
}
32+
33+
macro_rules! restore_context {
34+
() => {
35+
concat!(
36+
r#"
37+
pop r15
38+
pop r14
39+
pop r13
40+
pop r12
41+
pop r11
42+
pop r10
43+
pop r9
44+
pop r8
45+
pop rdi
46+
pop rsi
47+
pop rbp
48+
pop rbx
49+
pop rdx
50+
pop rcx
51+
pop rax
52+
popfq
53+
ret
54+
"#
55+
)
56+
};
57+
}
58+
959
#[naked]
10-
pub extern "C" fn switch(_old_stack: *mut usize, _new_stack: usize) {
60+
pub unsafe extern "C" fn switch(_old_stack: *mut usize, _new_stack: usize) {
1161
// rdi = old_stack => the address to store the old rsp
1262
// rsi = new_stack => stack pointer of the new task
1363

14-
unsafe {
15-
llvm_asm!(
16-
// store context
17-
"pushfq\n\t\
18-
push %rax\n\t\
19-
push %rcx\n\t\
20-
push %rdx\n\t\
21-
push %rbx\n\t\
22-
sub $$8, %rsp // ignore rsp\n\t\
23-
push %rbp\n\t\
24-
push %rsi\n\t\
25-
push %rdi\n\t\
26-
push %r8\n\t\
27-
push %r9\n\t\
28-
push %r10\n\t\
29-
push %r11\n\t\
30-
push %r12\n\t\
31-
push %r13\n\t\
32-
push %r14\n\t\
33-
push %r15\n\t\
34-
mov %rsp, (%rdi)\n\t\
35-
mov %rsi, %rsp\n\t\
36-
// restore context \n\t\
37-
pop %r15\n\t\
38-
pop %r14\n\t\
39-
pop %r13\n\t\
40-
pop %r12\n\t\
41-
pop %r11\n\t\
42-
pop %r10\n\t\
43-
pop %r9\n\t\
44-
pop %r8\n\t\
45-
pop %rdi\n\t\
46-
pop %rsi\n\t\
47-
pop %rbp\n\t\
48-
add $$8, %rsp\n\t\
49-
pop %rbx\n\t\
50-
pop %rdx\n\t\
51-
pop %rcx\n\t\
52-
pop %rax\n\t\
53-
popfq" :::: "volatile"
54-
);
55-
}
64+
asm!(
65+
save_context!(),
66+
// Store the old `rsp` behind `old_stack`
67+
"mov [rdi], rsp",
68+
// Set `rsp` to `new_stack`
69+
"mov rsp, rsi",
70+
restore_context!(),
71+
options(noreturn)
72+
);
5673
}

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![feature(llvm_asm, const_fn, lang_items)]
1+
#![feature(const_ptr_offset, asm, lang_items)]
22
#![feature(allocator_api)]
33
#![feature(naked_functions)]
44
#![feature(abi_x86_interrupt)]

src/scheduler/scheduler.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,9 @@ impl Scheduler {
234234

235235
self.current_task = new_task;
236236

237-
switch(current_stack_pointer, new_stack_pointer);
237+
unsafe {
238+
switch(current_stack_pointer, new_stack_pointer);
239+
}
238240
}
239241
_ => {}
240242
}

x86_64-eduos.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
"data-layout": "e-m:e-i64:64-f80:128-n8:16:32:64-S128",
99
"features": "-mmx,-sse,-sse2,-sse3,-ssse3,-sse4.1,-sse4.2,-3dnow,-3dnowa,-avx,-avx2,+soft-float",
1010
"disable-redzone": true,
11-
"eliminate-frame-pointer": true,
1211
"executables": true,
1312
"linker-flavor": "ld.lld",
1413
"linker": "rust-lld",

0 commit comments

Comments
 (0)