Skip to content

Commit 42c8cad

Browse files
authored
Miscellaneous code cleanups. (#92)
1 parent 91c94ca commit 42c8cad

5 files changed

Lines changed: 53 additions & 58 deletions

File tree

src/arch/arm.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub(super) unsafe extern "C" fn _start() -> ! {
3131
// stack pointer. On many architectures, the incoming frame pointer is
3232
// already null.
3333
asm!(
34-
"mov r0, sp\n", // Pass the incoming `sp` as the arg to `entry`.
34+
"mov r0, sp", // Pass the incoming `sp` as the arg to `entry`.
3535
"mov lr, #0", // Set the return address to zero.
3636
"b {entry}", // Jump to `entry`.
3737
entry = sym super::program::entry,

src/arch/x86.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ pub(super) unsafe fn relocation_load(ptr: usize) -> usize {
6666
// happens outside the Rust memory model. As far as Rust knows, this is
6767
// just an arbitrary side-effecting opaque operation.
6868
asm!(
69-
"mov {}, DWORD PTR [{}]",
69+
"mov {}, [{}]",
7070
out(reg) r0,
7171
in(reg) ptr,
7272
options(nostack, preserves_flags),
@@ -92,7 +92,7 @@ pub(super) unsafe fn relocation_load(ptr: usize) -> usize {
9292
#[inline]
9393
pub(super) unsafe fn relocation_store(ptr: usize, value: usize) {
9494
asm!(
95-
"mov DWORD PTR [{}], {}",
95+
"mov [{}], {}",
9696
in(reg) ptr,
9797
in(reg) value,
9898
options(nostack, preserves_flags),
@@ -180,7 +180,7 @@ pub(super) unsafe fn clone(
180180
"push ebp", // Save incoming register value.
181181

182182
// Pass `fn_` to the child in ebp.
183-
"mov ebp, DWORD PTR [eax+8]",
183+
"mov ebp, [eax+8]",
184184

185185
"mov esi, [eax+0]", // Pass `newtls` to the `int 0x80`.
186186
"mov eax, [eax+4]", // Pass `__NR_clone` to the `int 0x80`.
@@ -250,7 +250,7 @@ pub(super) unsafe fn set_thread_pointer(ptr: *mut c_void) {
250250
pub(super) fn thread_pointer() -> *mut c_void {
251251
let ptr;
252252
unsafe {
253-
asm!("mov {}, DWORD PTR gs:0", out(reg) ptr, options(nostack, preserves_flags, readonly));
253+
asm!("mov {}, gs:0", out(reg) ptr, options(nostack, preserves_flags, readonly));
254254
}
255255
ptr
256256
}

src/arch/x86_64.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ pub(super) unsafe fn relocation_load(ptr: usize) -> usize {
6262
// happens outside the Rust memory model. As far as Rust knows, this is
6363
// just an arbitrary side-effecting opaque operation.
6464
asm!(
65-
"mov {}, QWORD PTR [{}]",
65+
"mov {}, [{}]",
6666
out(reg) r0,
6767
in(reg) ptr,
6868
options(nostack, preserves_flags),
@@ -88,7 +88,7 @@ pub(super) unsafe fn relocation_load(ptr: usize) -> usize {
8888
#[inline]
8989
pub(super) unsafe fn relocation_store(ptr: usize, value: usize) {
9090
asm!(
91-
"mov QWORD PTR [{}], {}",
91+
"mov [{}], {}",
9292
in(reg) ptr,
9393
in(reg) value,
9494
options(nostack, preserves_flags),
@@ -194,7 +194,7 @@ pub(super) unsafe fn set_thread_pointer(ptr: *mut c_void) {
194194
pub(super) fn thread_pointer() -> *mut c_void {
195195
let ptr;
196196
unsafe {
197-
asm!("mov {}, QWORD PTR fs:0", out(reg) ptr, options(nostack, preserves_flags, readonly));
197+
asm!("mov {}, fs:0", out(reg) ptr, options(nostack, preserves_flags, readonly));
198198
}
199199
ptr
200200
}

src/thread/libc.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use alloc::boxed::Box;
44
use core::any::Any;
55
use core::ffi::c_void;
6+
use core::mem::zeroed;
67
use core::ptr::{from_exposed_addr_mut, null_mut, NonNull};
78
use rustix::io;
89

@@ -73,7 +74,7 @@ pub fn create_thread(
7374
}
7475

7576
unsafe {
76-
let mut attr: libc::pthread_attr_t = core::mem::zeroed();
77+
let mut attr: libc::pthread_attr_t = zeroed();
7778
match libc::pthread_attr_init(&mut attr) {
7879
0 => (),
7980
err => return Err(io::Errno::from_raw_os_error(err)),
@@ -86,7 +87,7 @@ pub fn create_thread(
8687
0 => (),
8788
err => return Err(io::Errno::from_raw_os_error(err)),
8889
}
89-
let mut new_thread = core::mem::zeroed();
90+
let mut new_thread = zeroed();
9091
let arg = Box::into_raw(Box::new(fn_));
9192
match libc::pthread_create(&mut new_thread, &attr, start, arg.cast()) {
9293
0 => (),
@@ -171,7 +172,7 @@ pub fn current_thread_tls_addr(offset: usize) -> *mut c_void {
171172
pub unsafe fn thread_stack(thread: Thread) -> (*mut c_void, usize, usize) {
172173
let thread = thread.0;
173174

174-
let mut attr: libc::pthread_attr_t = core::mem::zeroed();
175+
let mut attr: libc::pthread_attr_t = zeroed();
175176
assert_eq!(libc::pthread_getattr_np(thread, &mut attr), 0);
176177

177178
let mut stack_size = 0;
@@ -221,7 +222,7 @@ pub unsafe fn join_thread(thread: Thread) {
221222
#[must_use]
222223
pub fn default_stack_size() -> usize {
223224
unsafe {
224-
let mut attr: libc::pthread_attr_t = core::mem::zeroed();
225+
let mut attr: libc::pthread_attr_t = zeroed();
225226
assert_eq!(libc::pthread_getattr_np(libc::pthread_self(), &mut attr), 0);
226227

227228
let mut stack_size = 0;
@@ -236,7 +237,7 @@ pub fn default_stack_size() -> usize {
236237
#[must_use]
237238
pub fn default_guard_size() -> usize {
238239
unsafe {
239-
let mut attr: libc::pthread_attr_t = core::mem::zeroed();
240+
let mut attr: libc::pthread_attr_t = zeroed();
240241
assert_eq!(libc::pthread_getattr_np(libc::pthread_self(), &mut attr), 0);
241242

242243
let mut guard_size = 0;

src/thread/linux_raw.rs

Lines changed: 39 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use core::any::Any;
77
use core::cmp::max;
88
use core::ffi::c_void;
99
use core::mem::{align_of, size_of};
10-
use core::ptr::{self, drop_in_place, null, null_mut, NonNull};
10+
use core::ptr::{drop_in_place, null, null_mut, NonNull};
1111
use core::slice;
1212
use core::sync::atomic::Ordering::SeqCst;
1313
use core::sync::atomic::{AtomicI32, AtomicU8};
@@ -328,26 +328,23 @@ pub(super) unsafe fn initialize_main_thread(mem: *mut c_void) {
328328
let tid = rustix::runtime::set_tid_address(thread_id_ptr.cast());
329329

330330
// Initialize the thread metadata.
331-
ptr::write(
332-
metadata,
333-
Metadata {
334-
abi: Abi {
335-
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
336-
this: newtls,
337-
#[cfg(any(target_arch = "aarch64", target_arch = "arm"))]
338-
dtv: null(),
339-
#[cfg(any(target_arch = "aarch64", target_arch = "arm"))]
340-
pad: [0_usize; 1],
341-
},
342-
thread: ThreadData::new(
343-
Some(tid),
344-
stack_least.cast(),
345-
stack_size,
346-
guard_size,
347-
map_size,
348-
),
331+
metadata.write(Metadata {
332+
abi: Abi {
333+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
334+
this: newtls,
335+
#[cfg(any(target_arch = "aarch64", target_arch = "arm"))]
336+
dtv: null(),
337+
#[cfg(any(target_arch = "aarch64", target_arch = "arm"))]
338+
pad: [0_usize; 1],
349339
},
350-
);
340+
thread: ThreadData::new(
341+
Some(tid),
342+
stack_least.cast(),
343+
stack_size,
344+
guard_size,
345+
map_size,
346+
),
347+
});
351348

352349
// Initialize the TLS data with explicit initializer data.
353350
slice::from_raw_parts_mut(tls_data, STARTUP_TLS_INFO.file_size).copy_from_slice(
@@ -456,26 +453,23 @@ pub fn create_thread(
456453
let newtls: *mut Abi = &mut (*metadata).abi;
457454

458455
// Initialize the thread metadata.
459-
ptr::write(
460-
metadata,
461-
Metadata {
462-
abi: Abi {
463-
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
464-
this: newtls,
465-
#[cfg(any(target_arch = "aarch64", target_arch = "arm"))]
466-
dtv: null(),
467-
#[cfg(any(target_arch = "aarch64", target_arch = "arm"))]
468-
pad: [0_usize; 1],
469-
},
470-
thread: ThreadData::new(
471-
None, // the real tid will be written by `clone`.
472-
stack_least.cast(),
473-
stack_size,
474-
guard_size,
475-
map_size,
476-
),
456+
metadata.write(Metadata {
457+
abi: Abi {
458+
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
459+
this: newtls,
460+
#[cfg(any(target_arch = "aarch64", target_arch = "arm"))]
461+
dtv: null(),
462+
#[cfg(any(target_arch = "aarch64", target_arch = "arm"))]
463+
pad: [0_usize; 1],
477464
},
478-
);
465+
thread: ThreadData::new(
466+
None, // the real tid will be written by `clone`.
467+
stack_least.cast(),
468+
stack_size,
469+
guard_size,
470+
map_size,
471+
),
472+
});
479473

480474
// Initialize the TLS data with explicit initializer data.
481475
slice::from_raw_parts_mut(tls_data, STARTUP_TLS_INFO.file_size).copy_from_slice(
@@ -576,17 +570,17 @@ pub(super) unsafe extern "C" fn entry(
576570
}
577571

578572
// Check that the incoming stack pointer is where we expect it to be.
579-
debug_assert_eq!(builtin_return_address(0), core::ptr::null());
580-
debug_assert_ne!(builtin_frame_address(0), core::ptr::null());
573+
debug_assert_eq!(builtin_return_address(0), null());
574+
debug_assert_ne!(builtin_frame_address(0), null());
581575
#[cfg(not(any(target_arch = "x86", target_arch = "arm")))]
582576
debug_assert_eq!(builtin_frame_address(0).addr() & 0xf, 0);
583577
#[cfg(target_arch = "arm")]
584578
debug_assert_eq!(builtin_frame_address(0).addr() & 0x3, 0);
585579
#[cfg(target_arch = "x86")]
586580
debug_assert_eq!(builtin_frame_address(0).addr() & 0xf, 8);
587-
debug_assert_eq!(builtin_frame_address(1), core::ptr::null());
581+
debug_assert_eq!(builtin_frame_address(1), null());
588582
#[cfg(target_arch = "aarch64")]
589-
debug_assert_ne!(builtin_sponentry(), core::ptr::null());
583+
debug_assert_ne!(builtin_sponentry(), null());
590584
#[cfg(target_arch = "aarch64")]
591585
debug_assert_eq!(builtin_sponentry().addr() & 0xf, 0);
592586

@@ -754,8 +748,8 @@ unsafe fn wait_for_thread_exit(thread: Thread) {
754748
// Check whether the thread has exited already; we set the
755749
// `CloneFlags::CHILD_CLEARTID` flag on the clone syscall, so we can test
756750
// for `NONE` here.
757-
let thread = thread.0.as_ref();
758-
let thread_id = &thread.thread_id;
751+
let thread_data = thread.0.as_ref();
752+
let thread_id = &thread_data.thread_id;
759753
while let Some(id_value) = ThreadId::from_raw(thread_id.load(SeqCst)) {
760754
// This doesn't use any shared memory, but we can't use
761755
// `FutexFlags::PRIVATE` because the wake comes from Linux

0 commit comments

Comments
 (0)