Skip to content

Commit 2fc2c1e

Browse files
committed
convert inline assmebly to the new assembly format
1 parent 002c229 commit 2fc2c1e

File tree

6 files changed

+125
-74
lines changed

6 files changed

+125
-74
lines changed

src/arch/x86_64/kernel/mod.rs

Lines changed: 66 additions & 19 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-
mod gdt;
8+
pub mod gdt;
99
pub mod irq;
1010
mod pit;
1111
pub mod processor;
@@ -18,7 +18,7 @@ pub mod task;
1818
pub use crate::arch::x86_64::kernel::syscall::syscall_handler;
1919
use core::ptr::read_volatile;
2020

21-
global_asm!(include_str!("user_land.s"));
21+
global_asm!(include_str!("user_land.s"), options(att_syntax));
2222

2323
#[repr(C)]
2424
struct KernelHeader {
@@ -45,7 +45,7 @@ pub fn register_task() {
4545
let sel: u16 = 6u16 << 3;
4646

4747
unsafe {
48-
llvm_asm!("ltr $0" :: "r"(sel) :: "volatile");
48+
asm!("ltr ax", in("ax") sel, options(nostack, nomem));
4949
}
5050
}
5151

@@ -124,7 +124,12 @@ macro_rules! syscall {
124124
pub fn syscall0(arg0: u64) -> u64 {
125125
let mut ret: u64;
126126
unsafe {
127-
llvm_asm!("syscall" : "={rax}" (ret) : "{rax}" (arg0) : "rcx", "r11", "memory" : "volatile");
127+
asm!("syscall",
128+
inlateout("rax") arg0 => ret,
129+
lateout("rcx") _,
130+
lateout("r11") _,
131+
options(preserves_flags, nostack)
132+
);
128133
}
129134
ret
130135
}
@@ -134,8 +139,13 @@ pub fn syscall0(arg0: u64) -> u64 {
134139
pub fn syscall1(arg0: u64, arg1: u64) -> u64 {
135140
let mut ret: u64;
136141
unsafe {
137-
llvm_asm!("syscall" : "={rax}" (ret) : "{rax}" (arg0), "{rdi}" (arg1)
138-
: "rcx", "r11", "memory" : "volatile");
142+
asm!("syscall",
143+
inlateout("rax") arg0 => ret,
144+
in("rdi") arg1,
145+
lateout("rcx") _,
146+
lateout("r11") _,
147+
options(preserves_flags, nostack)
148+
);
139149
}
140150
ret
141151
}
@@ -145,8 +155,14 @@ pub fn syscall1(arg0: u64, arg1: u64) -> u64 {
145155
pub fn syscall2(arg0: u64, arg1: u64, arg2: u64) -> u64 {
146156
let mut ret: u64;
147157
unsafe {
148-
llvm_asm!("syscall" : "={rax}" (ret) : "{rax}" (arg0), "{rdi}" (arg1), "{rsi}" (arg2)
149-
: "rcx", "r11", "memory" : "volatile");
158+
asm!("syscall",
159+
inlateout("rax") arg0 => ret,
160+
in("rdi") arg1,
161+
in("rsi") arg2,
162+
lateout("rcx") _,
163+
lateout("r11") _,
164+
options(preserves_flags, nostack)
165+
);
150166
}
151167
ret
152168
}
@@ -156,8 +172,15 @@ pub fn syscall2(arg0: u64, arg1: u64, arg2: u64) -> u64 {
156172
pub fn syscall3(arg0: u64, arg1: u64, arg2: u64, arg3: u64) -> u64 {
157173
let mut ret: u64;
158174
unsafe {
159-
llvm_asm!("syscall" : "={rax}" (ret) : "{rax}" (arg0), "{rdi}" (arg1), "{rsi}" (arg2),
160-
"{rdx}" (arg3) : "rcx", "r11", "memory" : "volatile");
175+
asm!("syscall",
176+
inlateout("rax") arg0 => ret,
177+
in("rdi") arg1,
178+
in("rsi") arg2,
179+
in("rdx") arg3,
180+
lateout("rcx") _,
181+
lateout("r11") _,
182+
options(preserves_flags, nostack)
183+
);
161184
}
162185
ret
163186
}
@@ -167,9 +190,16 @@ pub fn syscall3(arg0: u64, arg1: u64, arg2: u64, arg3: u64) -> u64 {
167190
pub fn syscall4(arg0: u64, arg1: u64, arg2: u64, arg3: u64, arg4: u64) -> u64 {
168191
let mut ret: u64;
169192
unsafe {
170-
llvm_asm!("syscall" : "={rax}" (ret)
171-
: "{rax}" (arg0), "{rdi}" (arg1), "{rsi}" (arg2), "{rdx}" (arg3),
172-
"{r10}" (arg4) : "rcx", "r11", "memory" : "volatile");
193+
asm!("syscall",
194+
inlateout("rax") arg0 => ret,
195+
in("rdi") arg1,
196+
in("rsi") arg2,
197+
in("rdx") arg3,
198+
in("r10") arg4,
199+
lateout("rcx") _,
200+
lateout("r11") _,
201+
options(preserves_flags, nostack)
202+
);
173203
}
174204
ret
175205
}
@@ -179,9 +209,17 @@ pub fn syscall4(arg0: u64, arg1: u64, arg2: u64, arg3: u64, arg4: u64) -> u64 {
179209
pub fn syscall5(arg0: u64, arg1: u64, arg2: u64, arg3: u64, arg4: u64, arg5: u64) -> u64 {
180210
let mut ret: u64;
181211
unsafe {
182-
llvm_asm!("syscall" : "={rax}" (ret)
183-
: "{rax}" (arg0), "{rdi}" (arg1), "{rsi}" (arg2), "{rdx}" (arg3),
184-
"{r10}" (arg4), "{r8}" (arg5) : "rcx", "r11", "memory" : "volatile");
212+
asm!("syscall",
213+
inlateout("rax") arg0 => ret,
214+
in("rdi") arg1,
215+
in("rsi") arg2,
216+
in("rdx") arg3,
217+
in("r10") arg4,
218+
in("r8") arg5,
219+
lateout("rcx") _,
220+
lateout("r11") _,
221+
options(preserves_flags, nostack)
222+
);
185223
}
186224
ret
187225
}
@@ -199,9 +237,18 @@ pub fn syscall6(
199237
) -> u64 {
200238
let mut ret: u64;
201239
unsafe {
202-
llvm_asm!("syscall" : "={rax}" (ret) : "{rax}" (arg0), "{rdi}" (arg1), "{rsi}" (arg2),
203-
"{rdx}" (arg3), "{r10}" (arg4), "{r8}" (arg5), "{r9}" (arg6)
204-
: "rcx", "r11", "memory" : "volatile");
240+
asm!("syscall",
241+
inlateout("rax") arg0 => ret,
242+
in("rdi") arg1,
243+
in("rsi") arg2,
244+
in("rdx") arg3,
245+
in("r10") arg4,
246+
in("r8") arg5,
247+
in("r9") arg6,
248+
lateout("rcx") _,
249+
lateout("r11") _,
250+
options(preserves_flags, nostack)
251+
);
205252
}
206253
ret
207254
}

src/arch/x86_64/kernel/processor.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ static mut SUPPORTS_1GIB_PAGES: bool = false;
2626
#[inline(always)]
2727
pub fn mb() {
2828
unsafe {
29-
llvm_asm!("mfence" ::: "memory" : "volatile");
29+
asm!("mfence", options(preserves_flags, nostack));
3030
}
3131
}
3232

@@ -37,9 +37,9 @@ pub fn msb(value: u64) -> Option<u64> {
3737
let ret: u64;
3838
unsafe {
3939
asm!("bsr {0}, {1}",
40-
out(reg) ret,
40+
out(reg) ret,
4141
in(reg) value,
42-
options(nomem, nostack)
42+
options(nomem, nostack)
4343
);
4444
}
4545
Some(ret)
@@ -55,9 +55,9 @@ pub fn lsb(value: u64) -> Option<u64> {
5555
let ret: u64;
5656
unsafe {
5757
asm!("bsf {0}, {1}",
58-
out(reg) ret,
58+
out(reg) ret,
5959
in(reg) value,
60-
options(nomem, nostack)
60+
options(nomem, nostack)
6161
);
6262
}
6363
Some(ret)
@@ -179,7 +179,7 @@ pub fn init() {
179179

180180
// reset GS registers
181181
wrmsr(IA32_GS_BASE, 0);
182-
llvm_asm!("wrgsbase $0" :: "r"(BOOT_STACK.top()) :: "volatile");
182+
asm!("wrgsbase {}", in(reg) BOOT_STACK.top(), options(preserves_flags, nomem, nostack));
183183
}
184184

185185
// determin processor features

src/arch/x86_64/kernel/switch.rs

Lines changed: 1 addition & 1 deletion
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-
use crate::arch::x86_64::gdt::set_current_kernel_stack;
8+
use crate::arch::x86_64::kernel::gdt::set_current_kernel_stack;
99

1010
macro_rules! save_context {
1111
() => {

src/arch/x86_64/kernel/syscall.rs

Lines changed: 50 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -5,54 +5,58 @@
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+
use crate::syscall::SYSHANDLER_TABLE;
9+
810
#[no_mangle]
911
#[naked]
1012
pub unsafe extern "C" fn syscall_handler() {
11-
llvm_asm!(
13+
asm!(
1214
// save context, see x86_64 ABI
13-
"push %rcx\n\t\
14-
push %rdx\n\t\
15-
push %rsi\n\t\
16-
push %rdi\n\t\
17-
push %r8\n\t\
18-
push %r9\n\t\
19-
push %r10\n\t\
20-
push %r11\n\t\
21-
// save ds/es and set to kernel data descriptor \n\t\
22-
mov %ds, %rcx\n\t\
23-
push %rcx\n\t\
24-
mov %es, %rcx\n\t\
25-
push %rcx\n\t\
26-
mov $$0x10, %rcx\n\t\
27-
mov %rcx, %ds\n\t\
28-
mov %rcx, %es\n\t\
29-
// switch to kernel stack\n\t
30-
swapgs\n\t
31-
mov %rsp, %rcx\n\t
32-
rdgsbase %rsp\n\t
33-
push %rcx
34-
// copy 4th argument to rcx to adhere x86_64 ABI \n\t\
35-
mov %r10, %rcx\n\t\
36-
sti\n\t\
37-
call *SYSHANDLER_TABLE(,%rax,8)\n\t
38-
// restore context, see x86_64 ABI \n\t\
39-
cli\n\t\
40-
// switch to user stack\n\t
41-
pop %rcx\n\t
42-
mov %rcx, %rsp\n\t
43-
swapgs\n\t\
44-
// restore context
45-
pop %rcx\n\t\
46-
mov %rcx, %es\n\t\
47-
pop %rcx\n\t\
48-
mov %rcx, %ds\n\t\
49-
pop %r11\n\t\
50-
pop %r10\n\t\
51-
pop %r9\n\t\
52-
pop %r8\n\t\
53-
pop %rdi\n\t\
54-
pop %rsi\n\t\
55-
pop %rdx\n\t\
56-
pop %rcx\n\t\
57-
sysretq" :::: "volatile");
15+
"push rcx",
16+
"push rdx",
17+
"push rsi",
18+
"push rdi",
19+
"push r8",
20+
"push r9",
21+
"push r10",
22+
"push r11",
23+
// save ds/es and set to kernel data descriptor
24+
"mov rcx, ds",
25+
"push rcx",
26+
"mov rcx, es",
27+
"push rcx",
28+
"mov rcx, {kernel_ds}",
29+
"mov ds, rcx",
30+
"mov es, rcx",
31+
// switch to kernel stack
32+
"swapgs",
33+
"mov rcx, rsp",
34+
"rdgsbase rsp",
35+
"push rcx",
36+
// copy 4th argument to rcx to adhere x86_64 ABI
37+
"mov rcx, r10",
38+
"sti",
39+
"call [{sys_handler}+8*rax]",
40+
// restore context, see x86_64 ABI
41+
"cli",
42+
// switch to user stack
43+
"pop rcx",
44+
"mov rsp, rcx",
45+
"swapgs",
46+
"pop rcx",
47+
"mov es, rcx",
48+
"pop rcx",
49+
"mov ds, rcx",
50+
"pop r11",
51+
"pop r10",
52+
"pop r9",
53+
"pop r8",
54+
"pop rdi",
55+
"pop rsi",
56+
"pop rdx",
57+
"pop rcx",
58+
"sysretq",
59+
sys_handler = sym SYSHANDLER_TABLE,
60+
kernel_ds = const 0x10,
61+
options(noreturn));
5862
}

src/arch/x86_64/mm/paging.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ impl<S: PageSize> Page<S> {
238238
#[inline(always)]
239239
fn flush_from_tlb(&self) {
240240
unsafe {
241-
llvm_asm!("invlpg ($0)" :: "r"(self.virtual_address) : "memory" : "volatile");
241+
asm!("invlpg [{}]", in(reg) self.virtual_address, options(preserves_flags, nostack));
242242
}
243243
}
244244

src/arch/x86_64/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2017-2018 Stefan Lankes, RWTH Aachen University
1+
// Copyright (c) 2017-2021 Stefan Lankes, RWTH Aachen University
22
//
33
// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
44
// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or

0 commit comments

Comments
 (0)