Skip to content

Commit 0afc070

Browse files
authored
Merge pull request #165 from rybot666/rybot666-new-asm
Use new assembly syntax
2 parents 7fcea5d + f171cea commit 0afc070

File tree

11 files changed

+57
-51
lines changed

11 files changed

+57
-51
lines changed

Changelog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Unreleased
22

3+
- Use `asm!` instead of perma-unstable `llvm_asm!` macro
4+
35
# 0.12.2 – 2020-09-29
46

57
- Add additional `DescriptorFlags` and aliases compatible with `syscall`/`sysenter` ([#181](https://github.com/rust-osdev/x86_64/pull/181))

src/instructions/interrupts.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub fn are_enabled() -> bool {
1515
pub fn enable() {
1616
#[cfg(feature = "inline_asm")]
1717
unsafe {
18-
llvm_asm!("sti" :::: "volatile");
18+
asm!("sti", options(nomem, nostack));
1919
}
2020
#[cfg(not(feature = "inline_asm"))]
2121
unsafe {
@@ -30,7 +30,7 @@ pub fn enable() {
3030
pub fn disable() {
3131
#[cfg(feature = "inline_asm")]
3232
unsafe {
33-
llvm_asm!("cli" :::: "volatile");
33+
asm!("cli", options(nomem, nostack));
3434
}
3535

3636
#[cfg(not(feature = "inline_asm"))]
@@ -131,7 +131,7 @@ where
131131
pub fn enable_interrupts_and_hlt() {
132132
#[cfg(feature = "inline_asm")]
133133
unsafe {
134-
llvm_asm!("sti; hlt" :::: "volatile");
134+
asm!("sti; hlt", options(nomem, nostack));
135135
}
136136
#[cfg(not(feature = "inline_asm"))]
137137
unsafe {
@@ -144,7 +144,7 @@ pub fn enable_interrupts_and_hlt() {
144144
pub fn int3() {
145145
#[cfg(feature = "inline_asm")]
146146
unsafe {
147-
llvm_asm!("int3" :::: "volatile");
147+
asm!("int3", options(nomem, nostack));
148148
}
149149

150150
#[cfg(not(feature = "inline_asm"))]
@@ -162,7 +162,7 @@ pub fn int3() {
162162
#[macro_export]
163163
macro_rules! software_interrupt {
164164
($x:expr) => {{
165-
llvm_asm!("int $0" :: "N" ($x) :: "volatile");
165+
asm!("int {id}", id = const $x, options(nomem, nostack));
166166
}};
167167
}
168168

src/instructions/mod.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub mod tlb;
1414
pub fn hlt() {
1515
#[cfg(feature = "inline_asm")]
1616
unsafe {
17-
llvm_asm!("hlt" :::: "volatile");
17+
asm!("hlt", options(nomem, nostack));
1818
}
1919

2020
#[cfg(not(feature = "inline_asm"))]
@@ -33,7 +33,7 @@ pub fn hlt() {
3333
pub fn nop() {
3434
#[cfg(feature = "inline_asm")]
3535
unsafe {
36-
llvm_asm!("nop" :::: "volatile");
36+
asm!("nop", options(nomem, nostack, preserves_flags));
3737
}
3838

3939
#[cfg(not(feature = "inline_asm"))]
@@ -48,7 +48,7 @@ pub fn nop() {
4848
#[inline]
4949
pub fn bochs_breakpoint() {
5050
unsafe {
51-
llvm_asm!("xchgw %bx, %bx" :::: "volatile");
51+
asm!("xchgw bx, bx", options(nomem, nostack));
5252
}
5353
}
5454

@@ -59,9 +59,8 @@ pub fn bochs_breakpoint() {
5959
pub fn read_rip() -> u64 {
6060
let rip: u64;
6161
unsafe {
62-
llvm_asm!(
63-
"lea (%rip), $0"
64-
: "=r"(rip) ::: "volatile"
62+
asm!(
63+
"lea {}, [rip]", out(reg) rip, options(nostack, nomem)
6564
);
6665
}
6766
rip

src/instructions/port.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ impl PortRead for u8 {
99
#[inline]
1010
unsafe fn read_from_port(port: u16) -> u8 {
1111
let value: u8;
12-
llvm_asm!("inb $1, $0" : "={al}"(value) : "N{dx}"(port) :: "volatile");
12+
asm!("in al, dx", out("al") value, in("dx") port, options(nomem, nostack));
1313
value
1414
}
1515

@@ -25,7 +25,7 @@ impl PortRead for u16 {
2525
#[inline]
2626
unsafe fn read_from_port(port: u16) -> u16 {
2727
let value: u16;
28-
llvm_asm!("inw $1, $0" : "={ax}"(value) : "N{dx}"(port) :: "volatile");
28+
asm!("in ax, dx", out("ax") value, in("dx") port, options(nomem, nostack));
2929
value
3030
}
3131

@@ -41,7 +41,7 @@ impl PortRead for u32 {
4141
#[inline]
4242
unsafe fn read_from_port(port: u16) -> u32 {
4343
let value: u32;
44-
llvm_asm!("inl $1, $0" : "={eax}"(value) : "N{dx}"(port) :: "volatile");
44+
asm!("in eax, dx", out("eax") value, in("dx") port, options(nomem, nostack));
4545
value
4646
}
4747

@@ -56,7 +56,7 @@ impl PortWrite for u8 {
5656
#[cfg(feature = "inline_asm")]
5757
#[inline]
5858
unsafe fn write_to_port(port: u16, value: u8) {
59-
llvm_asm!("outb $1, $0" :: "N{dx}"(port), "{al}"(value) :: "volatile");
59+
asm!("out dx, al", in("dx") port, in("al") value, options(nomem, nostack));
6060
}
6161

6262
#[cfg(not(feature = "inline_asm"))]
@@ -70,7 +70,7 @@ impl PortWrite for u16 {
7070
#[cfg(feature = "inline_asm")]
7171
#[inline]
7272
unsafe fn write_to_port(port: u16, value: u16) {
73-
llvm_asm!("outw $1, $0" :: "N{dx}"(port), "{ax}"(value) :: "volatile");
73+
asm!("out dx, ax", in("dx") port, in("ax") value, options(nomem, nostack));
7474
}
7575

7676
#[cfg(not(feature = "inline_asm"))]
@@ -84,7 +84,7 @@ impl PortWrite for u32 {
8484
#[cfg(feature = "inline_asm")]
8585
#[inline]
8686
unsafe fn write_to_port(port: u16, value: u32) {
87-
llvm_asm!("outl $1, $0" :: "N{dx}"(port), "{eax}"(value) :: "volatile");
87+
asm!("out dx, eax", in("dx") port, in("eax") value, options(nomem, nostack));
8888
}
8989

9090
#[cfg(not(feature = "inline_asm"))]

src/instructions/segmentation.rs

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ use crate::structures::gdt::SegmentSelector;
55
/// Reload code segment register.
66
///
77
/// Note this is special since we can not directly move
8-
/// to %cs. Instead we push the new segment selector
9-
/// and return value on the stack and use lretq
8+
/// to cs. Instead we push the new segment selector
9+
/// and return value on the stack and use retf
1010
/// to reload cs and continue at 1:.
1111
///
1212
/// ## Safety
@@ -18,11 +18,15 @@ pub unsafe fn set_cs(sel: SegmentSelector) {
1818
#[cfg(feature = "inline_asm")]
1919
#[inline(always)]
2020
unsafe fn inner(sel: SegmentSelector) {
21-
llvm_asm!("pushq $0; \
22-
leaq 1f(%rip), %rax; \
23-
pushq %rax; \
24-
lretq; \
25-
1:" :: "ri" (u64::from(sel.0)) : "rax" "memory");
21+
asm!(
22+
"push {sel}",
23+
"lea {tmp}, [1f + rip]",
24+
"push {tmp}",
25+
"retfq",
26+
"1:",
27+
sel = in(reg) u64::from(sel.0),
28+
tmp = lateout(reg) _,
29+
);
2630
}
2731

2832
#[cfg(not(feature = "inline_asm"))]
@@ -43,7 +47,7 @@ pub unsafe fn set_cs(sel: SegmentSelector) {
4347
#[inline]
4448
pub unsafe fn load_ss(sel: SegmentSelector) {
4549
#[cfg(feature = "inline_asm")]
46-
llvm_asm!("movw $0, %ss " :: "r" (sel.0) : "memory");
50+
asm!("mov ss, {0:x}", in(reg) sel.0, options(nostack));
4751

4852
#[cfg(not(feature = "inline_asm"))]
4953
crate::asm::x86_64_asm_load_ss(sel.0);
@@ -58,7 +62,7 @@ pub unsafe fn load_ss(sel: SegmentSelector) {
5862
#[inline]
5963
pub unsafe fn load_ds(sel: SegmentSelector) {
6064
#[cfg(feature = "inline_asm")]
61-
llvm_asm!("movw $0, %ds " :: "r" (sel.0) : "memory");
65+
asm!("mov ds, {0:x}", in(reg) sel.0, options(nostack));
6266

6367
#[cfg(not(feature = "inline_asm"))]
6468
crate::asm::x86_64_asm_load_ds(sel.0);
@@ -73,7 +77,7 @@ pub unsafe fn load_ds(sel: SegmentSelector) {
7377
#[inline]
7478
pub unsafe fn load_es(sel: SegmentSelector) {
7579
#[cfg(feature = "inline_asm")]
76-
llvm_asm!("movw $0, %es " :: "r" (sel.0) : "memory");
80+
asm!("mov es, {0:x}", in(reg) sel.0, options(nostack));
7781

7882
#[cfg(not(feature = "inline_asm"))]
7983
crate::asm::x86_64_asm_load_es(sel.0);
@@ -88,7 +92,7 @@ pub unsafe fn load_es(sel: SegmentSelector) {
8892
#[inline]
8993
pub unsafe fn load_fs(sel: SegmentSelector) {
9094
#[cfg(feature = "inline_asm")]
91-
llvm_asm!("movw $0, %fs " :: "r" (sel.0) : "memory");
95+
asm!("mov fs, {0:x}", in(reg) sel.0, options(nostack));
9296

9397
#[cfg(not(feature = "inline_asm"))]
9498
crate::asm::x86_64_asm_load_fs(sel.0);
@@ -103,7 +107,7 @@ pub unsafe fn load_fs(sel: SegmentSelector) {
103107
#[inline]
104108
pub unsafe fn load_gs(sel: SegmentSelector) {
105109
#[cfg(feature = "inline_asm")]
106-
llvm_asm!("movw $0, %gs " :: "r" (sel.0) : "memory");
110+
asm!("mov gs, {0:x}", in(reg) sel.0, options(nostack));
107111

108112
#[cfg(not(feature = "inline_asm"))]
109113
crate::asm::x86_64_asm_load_gs(sel.0);
@@ -118,7 +122,7 @@ pub unsafe fn load_gs(sel: SegmentSelector) {
118122
#[inline]
119123
pub unsafe fn swap_gs() {
120124
#[cfg(feature = "inline_asm")]
121-
llvm_asm!("swapgs" ::: "memory" : "volatile");
125+
asm!("swapgs", options(nostack));
122126

123127
#[cfg(not(feature = "inline_asm"))]
124128
crate::asm::x86_64_asm_swapgs();
@@ -130,7 +134,7 @@ pub fn cs() -> SegmentSelector {
130134
#[cfg(feature = "inline_asm")]
131135
{
132136
let segment: u16;
133-
unsafe { llvm_asm!("mov %cs, $0" : "=r" (segment) ) };
137+
unsafe { asm!("mov {0:x}, cs", out(reg) segment, options(nostack, nomem)) };
134138
SegmentSelector(segment)
135139
}
136140

@@ -155,7 +159,7 @@ pub unsafe fn wrfsbase(val: u64) {
155159
#[cfg(feature = "inline_asm")]
156160
#[inline(always)]
157161
unsafe fn inner(val: u64) {
158-
llvm_asm!("wrfsbase $0" :: "r"(val) :: "volatile")
162+
asm!("wrfsbase {}", in(reg) val, options(nomem, nostack));
159163
}
160164

161165
#[cfg(not(feature = "inline_asm"))]
@@ -178,7 +182,7 @@ pub unsafe fn rdfsbase() -> u64 {
178182
#[inline(always)]
179183
unsafe fn inner() -> u64 {
180184
let val: u64;
181-
llvm_asm!("rdfsbase $0" : "=r" (val) ::: "volatile");
185+
asm!("rdfsbase {}", out(reg) val, options(nomem, nostack));
182186
val
183187
}
184188

@@ -204,7 +208,7 @@ pub unsafe fn wrgsbase(val: u64) {
204208
#[cfg(feature = "inline_asm")]
205209
#[inline(always)]
206210
unsafe fn inner(val: u64) {
207-
llvm_asm!("wrgsbase $0" :: "r"(val) :: "volatile")
211+
asm!("wrgsbase {}", in(reg) val, options(nomem, nostack))
208212
}
209213

210214
#[cfg(not(feature = "inline_asm"))]
@@ -227,7 +231,7 @@ pub unsafe fn rdgsbase() -> u64 {
227231
#[inline(always)]
228232
unsafe fn inner() -> u64 {
229233
let val: u64;
230-
llvm_asm!("rdgsbase $0" : "=r" (val) ::: "volatile");
234+
asm!("rdgsbase {}", out(reg) val, options(nomem, nostack));
231235
val
232236
}
233237

src/instructions/tables.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub use crate::structures::DescriptorTablePointer;
1818
#[inline]
1919
pub unsafe fn lgdt(gdt: &DescriptorTablePointer) {
2020
#[cfg(feature = "inline_asm")]
21-
llvm_asm!("lgdt ($0)" :: "r" (gdt) : "memory");
21+
asm!("lgdt [{}]", in(reg) gdt, options(nostack));
2222

2323
#[cfg(not(feature = "inline_asm"))]
2424
crate::asm::x86_64_asm_lgdt(gdt as *const _);
@@ -38,7 +38,7 @@ pub unsafe fn lgdt(gdt: &DescriptorTablePointer) {
3838
#[inline]
3939
pub unsafe fn lidt(idt: &DescriptorTablePointer) {
4040
#[cfg(feature = "inline_asm")]
41-
llvm_asm!("lidt ($0)" :: "r" (idt) : "memory");
41+
asm!("lidt [{}]", in(reg) idt, options(nostack));
4242

4343
#[cfg(not(feature = "inline_asm"))]
4444
crate::asm::x86_64_asm_lidt(idt as *const _);
@@ -54,7 +54,7 @@ pub unsafe fn lidt(idt: &DescriptorTablePointer) {
5454
#[inline]
5555
pub unsafe fn load_tss(sel: SegmentSelector) {
5656
#[cfg(feature = "inline_asm")]
57-
llvm_asm!("ltr $0" :: "r" (sel.0));
57+
asm!("ltr {0:x}", in(reg) sel.0, options(nostack, nomem));
5858

5959
#[cfg(not(feature = "inline_asm"))]
6060
crate::asm::x86_64_asm_ltr(sel.0)

src/instructions/tlb.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::VirtAddr;
77
pub fn flush(addr: VirtAddr) {
88
#[cfg(feature = "inline_asm")]
99
unsafe {
10-
llvm_asm!("invlpg ($0)" :: "r" (addr.as_u64()) : "memory")
10+
asm!("invlpg [{}]", in(reg) addr.as_u64(), options(nostack))
1111
};
1212

1313
#[cfg(not(feature = "inline_asm"))]

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#![cfg_attr(feature = "const_fn", feature(const_mut_refs))]
77
#![cfg_attr(feature = "const_fn", feature(const_fn_fn_ptr_basics))]
88
#![cfg_attr(feature = "const_fn", feature(const_in_array_repeat_expressions))]
9-
#![cfg_attr(feature = "inline_asm", feature(llvm_asm))]
9+
#![cfg_attr(feature = "inline_asm", feature(asm))]
1010
#![cfg_attr(feature = "abi_x86_interrupt", feature(abi_x86_interrupt))]
1111
#![cfg_attr(feature = "deny-warnings", deny(warnings))]
1212
#![cfg_attr(feature = "deny-warnings", deny(missing_docs))]

src/registers/control.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ mod x86_64 {
145145

146146
#[cfg(feature = "inline_asm")]
147147
unsafe {
148-
llvm_asm!("mov %cr0, $0" : "=r" (value));
148+
asm!("mov {}, cr0", out(reg) value, options(nomem));
149149
}
150150

151151
#[cfg(not(feature = "inline_asm"))]
@@ -184,7 +184,7 @@ mod x86_64 {
184184
#[inline]
185185
pub unsafe fn write_raw(value: u64) {
186186
#[cfg(feature = "inline_asm")]
187-
llvm_asm!("mov $0, %cr0" :: "r" (value) : "memory");
187+
asm!("mov cr0, {}", in(reg) value, options(nostack));
188188

189189
#[cfg(not(feature = "inline_asm"))]
190190
crate::asm::x86_64_asm_write_cr0(value);
@@ -217,7 +217,7 @@ mod x86_64 {
217217

218218
#[cfg(feature = "inline_asm")]
219219
unsafe {
220-
llvm_asm!("mov %cr2, $0" : "=r" (value));
220+
asm!("mov {}, cr2", out(reg) value, options(nomem));
221221
}
222222

223223
#[cfg(not(feature = "inline_asm"))]
@@ -237,7 +237,7 @@ mod x86_64 {
237237

238238
#[cfg(feature = "inline_asm")]
239239
unsafe {
240-
llvm_asm!("mov %cr3, $0" : "=r" (value));
240+
asm!("mov {}, cr3", out(reg) value, options(nomem));
241241
}
242242

243243
#[cfg(not(feature = "inline_asm"))]
@@ -262,7 +262,7 @@ mod x86_64 {
262262
let value = addr.as_u64() | flags.bits();
263263

264264
#[cfg(feature = "inline_asm")]
265-
llvm_asm!("mov $0, %cr3" :: "r" (value) : "memory");
265+
asm!("mov cr3, {}", in(reg) value, options(nostack));
266266

267267
#[cfg(not(feature = "inline_asm"))]
268268
crate::asm::x86_64_asm_write_cr3(value)
@@ -283,7 +283,7 @@ mod x86_64 {
283283

284284
#[cfg(feature = "inline_asm")]
285285
unsafe {
286-
llvm_asm!("mov %cr4, $0" : "=r" (value));
286+
asm!("mov {}, cr4", out(reg) value, options(nostack));
287287
}
288288

289289
#[cfg(not(feature = "inline_asm"))]
@@ -324,7 +324,7 @@ mod x86_64 {
324324
#[inline]
325325
pub unsafe fn write_raw(value: u64) {
326326
#[cfg(feature = "inline_asm")]
327-
llvm_asm!("mov $0, %cr4" :: "r" (value) : "memory");
327+
asm!("mov cr4, {}", in(reg) value, options(nostack));
328328

329329
#[cfg(not(feature = "inline_asm"))]
330330
crate::asm::x86_64_asm_write_cr4(value);

0 commit comments

Comments
 (0)