Skip to content

Commit c5d27ad

Browse files
committed
swicht from llvm_asm! to asm!
- llvm_asm is deprecated => remove it from eduOS
1 parent f62c777 commit c5d27ad

File tree

7 files changed

+111
-66
lines changed

7 files changed

+111
-66
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
@@ -28,11 +28,17 @@ pub fn mb() {
2828
/// Search the most significant bit
2929
#[inline(always)]
3030
pub fn msb(value: u64) -> Option<u64> {
31+
println!("value {}", value);
3132
if value > 0 {
3233
let ret: u64;
3334
unsafe {
34-
llvm_asm!("bsr $1, $0" : "=r"(ret) : "r"(value) : "cc" : "volatile");
35+
asm!("bsr {0}, {1}",
36+
out(reg) ret,
37+
in(reg) value,
38+
options(nomem, nostack)
39+
);
3540
}
41+
println!("value {} {}", value, ret);
3642
Some(ret)
3743
} else {
3844
None
@@ -45,7 +51,11 @@ pub fn lsb(value: u64) -> Option<u64> {
4551
if value > 0 {
4652
let ret: u64;
4753
unsafe {
48-
llvm_asm!("bsf $1, $0" : "=r"(ret) : "r"(value) : "cc" : "volatile");
54+
asm!("bsf {0}, {1}",
55+
out(reg) ret,
56+
in(reg) value,
57+
options(nomem, nostack)
58+
);
4959
}
5060
Some(ret)
5161
} else {
@@ -55,13 +65,13 @@ pub fn lsb(value: u64) -> Option<u64> {
5565

5666
pub fn halt() {
5767
unsafe {
58-
llvm_asm!("hlt" :::: "volatile");
68+
asm!("hlt", options(nomem, nostack));
5969
}
6070
}
6171

6272
pub fn pause() {
6373
unsafe {
64-
llvm_asm!("pause" :::: "volatile");
74+
asm!("pause", options(nomem, nostack));
6575
}
6676
}
6777

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: 72 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -5,58 +5,80 @@
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+
use crate::arch::x86_64::gdt::set_current_kernel_stack;
9+
10+
macro_rules! save_context {
11+
() => {
12+
concat!(
13+
r#"
14+
pushfq
15+
push rax
16+
push rcx
17+
push rdx
18+
push rbx
19+
sub rsp, 8
20+
push rbp
21+
push rsi
22+
push rdi
23+
push r8
24+
push r9
25+
push r10
26+
push r11
27+
push r12
28+
push r13
29+
push r14
30+
push r15
31+
"#,
32+
)
33+
};
34+
}
35+
36+
macro_rules! restore_context {
37+
() => {
38+
concat!(
39+
r#"
40+
pop r15
41+
pop r14
42+
pop r13
43+
pop r12
44+
pop r11
45+
pop r10
46+
pop r9
47+
pop r8
48+
pop rdi
49+
pop rsi
50+
pop rbp
51+
add rsp, 8
52+
pop rbx
53+
pop rdx
54+
pop rcx
55+
pop rax
56+
popfq
57+
ret
58+
"#
59+
)
60+
};
61+
}
62+
963
#[naked]
10-
pub extern "C" fn switch(_old_stack: *mut usize, _new_stack: usize) {
64+
pub unsafe extern "C" fn switch(_old_stack: *mut usize, _new_stack: usize) {
1165
// rdi = old_stack => the address to store the old rsp
1266
// rsi = new_stack => stack pointer of the new task
1367

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-
// Set task switched flag \n\t\
37-
mov %cr0, %rax\n\t\
38-
or $$8, %rax\n\t\
39-
mov %rax, %cr0\n\t\
40-
// set stack pointer in TSS \n\t\
41-
call set_current_kernel_stack \n\t\
42-
// restore context \n\t\
43-
pop %r15\n\t\
44-
pop %r14\n\t\
45-
pop %r13\n\t\
46-
pop %r12\n\t\
47-
pop %r11\n\t\
48-
pop %r10\n\t\
49-
pop %r9\n\t\
50-
pop %r8\n\t\
51-
pop %rdi\n\t\
52-
pop %rsi\n\t\
53-
pop %rbp\n\t\
54-
add $$8, %rsp\n\t\
55-
pop %rbx\n\t\
56-
pop %rdx\n\t\
57-
pop %rcx\n\t\
58-
pop %rax\n\t\
59-
popfq" :::: "volatile"
60-
);
61-
}
68+
asm!(
69+
save_context!(),
70+
// Store the old `rsp` behind `old_stack`
71+
"mov [rdi], rsp",
72+
// Set `rsp` to `new_stack`
73+
"mov rsp, rsi",
74+
// Set task switched flag
75+
"mov rax, cr0",
76+
"or rax, 8",
77+
"mov cr0, rax",
78+
// set stack pointer in TSS
79+
"call {set_stack}",
80+
restore_context!(),
81+
set_stack = sym set_current_kernel_stack,
82+
options(noreturn)
83+
);
6284
}

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(global_asm)]
44
#![feature(naked_functions)]

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)