Skip to content

Commit cc7f286

Browse files
committed
Consistently format ASM blocks
This mostly makes the code shorter and easier to read Signed-off-by: Joe Richey <[email protected]>
1 parent ff6cbc0 commit cc7f286

File tree

8 files changed

+85
-159
lines changed

8 files changed

+85
-159
lines changed

src/instructions/interrupts.rs

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,11 @@ pub fn are_enabled() -> bool {
1313
/// This is a wrapper around the `sti` instruction.
1414
#[inline]
1515
pub fn enable() {
16-
#[cfg(feature = "inline_asm")]
1716
unsafe {
17+
#[cfg(feature = "inline_asm")]
1818
asm!("sti", options(nomem, nostack));
19-
}
20-
#[cfg(not(feature = "inline_asm"))]
21-
unsafe {
19+
20+
#[cfg(not(feature = "inline_asm"))]
2221
crate::asm::x86_64_asm_interrupt_enable();
2322
}
2423
}
@@ -28,13 +27,11 @@ pub fn enable() {
2827
/// This is a wrapper around the `cli` instruction.
2928
#[inline]
3029
pub fn disable() {
31-
#[cfg(feature = "inline_asm")]
3230
unsafe {
31+
#[cfg(feature = "inline_asm")]
3332
asm!("cli", options(nomem, nostack));
34-
}
3533

36-
#[cfg(not(feature = "inline_asm"))]
37-
unsafe {
34+
#[cfg(not(feature = "inline_asm"))]
3835
crate::asm::x86_64_asm_interrupt_disable();
3936
}
4037
}
@@ -129,26 +126,23 @@ where
129126
/// information.
130127
#[inline]
131128
pub fn enable_and_hlt() {
132-
#[cfg(feature = "inline_asm")]
133129
unsafe {
130+
#[cfg(feature = "inline_asm")]
134131
asm!("sti; hlt", options(nomem, nostack));
135-
}
136-
#[cfg(not(feature = "inline_asm"))]
137-
unsafe {
132+
133+
#[cfg(not(feature = "inline_asm"))]
138134
crate::asm::x86_64_asm_interrupt_enable_and_hlt();
139135
}
140136
}
141137

142138
/// Cause a breakpoint exception by invoking the `int3` instruction.
143139
#[inline]
144140
pub fn int3() {
145-
#[cfg(feature = "inline_asm")]
146141
unsafe {
142+
#[cfg(feature = "inline_asm")]
147143
asm!("int3", options(nomem, nostack));
148-
}
149144

150-
#[cfg(not(feature = "inline_asm"))]
151-
unsafe {
145+
#[cfg(not(feature = "inline_asm"))]
152146
crate::asm::x86_64_asm_int3();
153147
}
154148
}

src/instructions/mod.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,11 @@ pub mod tlb;
1212
/// Halts the CPU until the next interrupt arrives.
1313
#[inline]
1414
pub fn hlt() {
15-
#[cfg(feature = "inline_asm")]
1615
unsafe {
16+
#[cfg(feature = "inline_asm")]
1717
asm!("hlt", options(nomem, nostack));
18-
}
1918

20-
#[cfg(not(feature = "inline_asm"))]
21-
unsafe {
19+
#[cfg(not(feature = "inline_asm"))]
2220
crate::asm::x86_64_asm_hlt();
2321
}
2422
}
@@ -31,13 +29,11 @@ pub fn hlt() {
3129
/// endless loop away.
3230
#[inline]
3331
pub fn nop() {
34-
#[cfg(feature = "inline_asm")]
3532
unsafe {
33+
#[cfg(feature = "inline_asm")]
3634
asm!("nop", options(nomem, nostack, preserves_flags));
37-
}
3835

39-
#[cfg(not(feature = "inline_asm"))]
40-
unsafe {
36+
#[cfg(not(feature = "inline_asm"))]
4137
crate::asm::x86_64_asm_nop();
4238
}
4339
}

src/instructions/port.rs

Lines changed: 30 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -6,92 +6,77 @@ use core::marker::PhantomData;
66
pub use crate::structures::port::{PortRead, PortWrite};
77

88
impl PortRead for u8 {
9-
#[cfg(feature = "inline_asm")]
10-
#[inline]
11-
unsafe fn read_from_port(port: u16) -> u8 {
12-
let value: u8;
13-
asm!("in al, dx", out("al") value, in("dx") port, options(nomem, nostack));
14-
value
15-
}
16-
17-
#[cfg(not(feature = "inline_asm"))]
189
#[inline]
1910
unsafe fn read_from_port(port: u16) -> u8 {
11+
#[cfg(feature = "inline_asm")]
12+
{
13+
let value: u8;
14+
asm!("in al, dx", out("al") value, in("dx") port, options(nomem, nostack));
15+
value
16+
}
17+
#[cfg(not(feature = "inline_asm"))]
2018
crate::asm::x86_64_asm_read_from_port_u8(port)
2119
}
2220
}
2321

2422
impl PortRead for u16 {
25-
#[cfg(feature = "inline_asm")]
26-
#[inline]
27-
unsafe fn read_from_port(port: u16) -> u16 {
28-
let value: u16;
29-
asm!("in ax, dx", out("ax") value, in("dx") port, options(nomem, nostack));
30-
value
31-
}
32-
33-
#[cfg(not(feature = "inline_asm"))]
3423
#[inline]
3524
unsafe fn read_from_port(port: u16) -> u16 {
25+
#[cfg(feature = "inline_asm")]
26+
{
27+
let value: u16;
28+
asm!("in ax, dx", out("ax") value, in("dx") port, options(nomem, nostack));
29+
value
30+
}
31+
#[cfg(not(feature = "inline_asm"))]
3632
crate::asm::x86_64_asm_read_from_port_u16(port)
3733
}
3834
}
3935

4036
impl PortRead for u32 {
41-
#[cfg(feature = "inline_asm")]
42-
#[inline]
43-
unsafe fn read_from_port(port: u16) -> u32 {
44-
let value: u32;
45-
asm!("in eax, dx", out("eax") value, in("dx") port, options(nomem, nostack));
46-
value
47-
}
48-
49-
#[cfg(not(feature = "inline_asm"))]
5037
#[inline]
5138
unsafe fn read_from_port(port: u16) -> u32 {
39+
#[cfg(feature = "inline_asm")]
40+
{
41+
let value: u32;
42+
asm!("in eax, dx", out("eax") value, in("dx") port, options(nomem, nostack));
43+
value
44+
}
45+
#[cfg(not(feature = "inline_asm"))]
5246
crate::asm::x86_64_asm_read_from_port_u32(port)
5347
}
5448
}
5549

5650
impl PortWrite for u8 {
57-
#[cfg(feature = "inline_asm")]
5851
#[inline]
5952
unsafe fn write_to_port(port: u16, value: u8) {
53+
#[cfg(feature = "inline_asm")]
6054
asm!("out dx, al", in("dx") port, in("al") value, options(nomem, nostack));
61-
}
6255

63-
#[cfg(not(feature = "inline_asm"))]
64-
#[inline]
65-
unsafe fn write_to_port(port: u16, value: u8) {
66-
crate::asm::x86_64_asm_write_to_port_u8(port, value)
56+
#[cfg(not(feature = "inline_asm"))]
57+
crate::asm::x86_64_asm_write_to_port_u8(port, value);
6758
}
6859
}
6960

7061
impl PortWrite for u16 {
71-
#[cfg(feature = "inline_asm")]
7262
#[inline]
7363
unsafe fn write_to_port(port: u16, value: u16) {
64+
#[cfg(feature = "inline_asm")]
7465
asm!("out dx, ax", in("dx") port, in("ax") value, options(nomem, nostack));
75-
}
7666

77-
#[cfg(not(feature = "inline_asm"))]
78-
#[inline]
79-
unsafe fn write_to_port(port: u16, value: u16) {
80-
crate::asm::x86_64_asm_write_to_port_u16(port, value)
67+
#[cfg(not(feature = "inline_asm"))]
68+
crate::asm::x86_64_asm_write_to_port_u16(port, value);
8169
}
8270
}
8371

8472
impl PortWrite for u32 {
85-
#[cfg(feature = "inline_asm")]
8673
#[inline]
8774
unsafe fn write_to_port(port: u16, value: u32) {
75+
#[cfg(feature = "inline_asm")]
8876
asm!("out dx, eax", in("dx") port, in("eax") value, options(nomem, nostack));
89-
}
9077

91-
#[cfg(not(feature = "inline_asm"))]
92-
#[inline]
93-
unsafe fn write_to_port(port: u16, value: u32) {
94-
crate::asm::x86_64_asm_write_to_port_u32(port, value)
78+
#[cfg(not(feature = "inline_asm"))]
79+
crate::asm::x86_64_asm_write_to_port_u32(port, value);
9580
}
9681
}
9782

src/instructions/segmentation.rs

Lines changed: 26 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,18 @@ use crate::structures::gdt::SegmentSelector;
1616
#[inline]
1717
pub unsafe fn set_cs(sel: SegmentSelector) {
1818
#[cfg(feature = "inline_asm")]
19-
#[inline(always)]
20-
unsafe fn inner(sel: SegmentSelector) {
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-
);
30-
}
19+
asm!(
20+
"push {sel}",
21+
"lea {tmp}, [1f + rip]",
22+
"push {tmp}",
23+
"retfq",
24+
"1:",
25+
sel = in(reg) u64::from(sel.0),
26+
tmp = lateout(reg) _,
27+
);
3128

3229
#[cfg(not(feature = "inline_asm"))]
33-
#[inline(always)]
34-
unsafe fn inner(sel: SegmentSelector) {
35-
crate::asm::x86_64_asm_set_cs(u64::from(sel.0))
36-
}
37-
38-
inner(sel)
30+
crate::asm::x86_64_asm_set_cs(u64::from(sel.0));
3931
}
4032

4133
/// Reload stack segment register.
@@ -131,18 +123,18 @@ pub unsafe fn swap_gs() {
131123
/// Returns the current value of the code segment register.
132124
#[inline]
133125
pub fn cs() -> SegmentSelector {
126+
let segment: u16;
127+
134128
#[cfg(feature = "inline_asm")]
135-
{
136-
let segment: u16;
137-
unsafe { asm!("mov {0:x}, cs", out(reg) segment, options(nostack, nomem)) };
138-
SegmentSelector(segment)
129+
unsafe {
130+
asm!("mov {0:x}, cs", out(reg) segment, options(nostack, nomem));
139131
}
140-
141132
#[cfg(not(feature = "inline_asm"))]
142-
{
143-
let segment: u16 = unsafe { crate::asm::x86_64_asm_get_cs() };
144-
SegmentSelector(segment)
133+
unsafe {
134+
segment = crate::asm::x86_64_asm_get_cs();
145135
}
136+
137+
SegmentSelector(segment)
146138
}
147139

148140
/// Writes the FS segment base address
@@ -157,18 +149,10 @@ pub fn cs() -> SegmentSelector {
157149
#[inline]
158150
pub unsafe fn wrfsbase(val: u64) {
159151
#[cfg(feature = "inline_asm")]
160-
#[inline(always)]
161-
unsafe fn inner(val: u64) {
162-
asm!("wrfsbase {}", in(reg) val, options(nomem, nostack));
163-
}
152+
asm!("wrfsbase {}", in(reg) val, options(nomem, nostack));
164153

165154
#[cfg(not(feature = "inline_asm"))]
166-
#[inline(always)]
167-
unsafe fn inner(val: u64) {
168-
crate::asm::x86_64_asm_wrfsbase(val)
169-
}
170-
171-
inner(val)
155+
crate::asm::x86_64_asm_wrfsbase(val);
172156
}
173157

174158
/// Reads the FS segment base address
@@ -179,20 +163,14 @@ pub unsafe fn wrfsbase(val: u64) {
179163
#[inline]
180164
pub unsafe fn rdfsbase() -> u64 {
181165
#[cfg(feature = "inline_asm")]
182-
#[inline(always)]
183-
unsafe fn inner() -> u64 {
166+
{
184167
let val: u64;
185168
asm!("rdfsbase {}", out(reg) val, options(nomem, nostack));
186169
val
187170
}
188171

189172
#[cfg(not(feature = "inline_asm"))]
190-
#[inline(always)]
191-
unsafe fn inner() -> u64 {
192-
crate::asm::x86_64_asm_rdfsbase()
193-
}
194-
195-
inner()
173+
crate::asm::x86_64_asm_rdfsbase()
196174
}
197175

198176
/// Writes the GS segment base address
@@ -206,18 +184,10 @@ pub unsafe fn rdfsbase() -> u64 {
206184
#[inline]
207185
pub unsafe fn wrgsbase(val: u64) {
208186
#[cfg(feature = "inline_asm")]
209-
#[inline(always)]
210-
unsafe fn inner(val: u64) {
211-
asm!("wrgsbase {}", in(reg) val, options(nomem, nostack))
212-
}
187+
asm!("wrgsbase {}", in(reg) val, options(nomem, nostack));
213188

214189
#[cfg(not(feature = "inline_asm"))]
215-
#[inline(always)]
216-
unsafe fn inner(val: u64) {
217-
crate::asm::x86_64_asm_wrgsbase(val)
218-
}
219-
220-
inner(val)
190+
crate::asm::x86_64_asm_wrgsbase(val);
221191
}
222192

223193
/// Reads the GS segment base address
@@ -228,18 +198,12 @@ pub unsafe fn wrgsbase(val: u64) {
228198
#[inline]
229199
pub unsafe fn rdgsbase() -> u64 {
230200
#[cfg(feature = "inline_asm")]
231-
#[inline(always)]
232-
unsafe fn inner() -> u64 {
201+
{
233202
let val: u64;
234203
asm!("rdgsbase {}", out(reg) val, options(nomem, nostack));
235204
val
236205
}
237206

238207
#[cfg(not(feature = "inline_asm"))]
239-
#[inline(always)]
240-
unsafe fn inner() -> u64 {
241-
crate::asm::x86_64_asm_rdgsbase()
242-
}
243-
244-
inner()
208+
crate::asm::x86_64_asm_rdgsbase()
245209
}

src/instructions/tables.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,13 @@ pub fn sidt() -> DescriptorTablePointer {
5252
limit: 0,
5353
base: VirtAddr::new(0),
5454
};
55-
#[cfg(feature = "inline_asm")]
5655
unsafe {
56+
#[cfg(feature = "inline_asm")]
5757
asm!("sidt [{}]", in(reg) &mut idt, options(nostack));
58-
}
59-
#[cfg(not(feature = "inline_asm"))]
60-
unsafe {
58+
59+
#[cfg(not(feature = "inline_asm"))]
6160
crate::asm::x86_64_asm_sidt(&mut idt as *mut _);
6261
}
63-
6462
idt
6563
}
6664

@@ -77,5 +75,5 @@ pub unsafe fn load_tss(sel: SegmentSelector) {
7775
asm!("ltr {0:x}", in(reg) sel.0, options(nostack, nomem));
7876

7977
#[cfg(not(feature = "inline_asm"))]
80-
crate::asm::x86_64_asm_ltr(sel.0)
78+
crate::asm::x86_64_asm_ltr(sel.0);
8179
}

0 commit comments

Comments
 (0)