Skip to content

Commit 7d054db

Browse files
misc(kernel): refactor and make clippy happier
Signed-off-by: Anhad Singh <[email protected]>
1 parent e276b6e commit 7d054db

File tree

9 files changed

+44
-25
lines changed

9 files changed

+44
-25
lines changed

.vscode/settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"rust-analyzer.linkedProjects": [
33
"./src/Cargo.toml",
4-
"./userland/Cargo.toml"
4+
// "./userland/Cargo.toml"
55
],
66
"rust-analyzer.checkOnSave.allTargets": false,
77
// "rust-analyzer.checkOnSave.extraArgs": [

src/aero_kernel/src/drivers/block/nvme/command.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ pub struct CompletionEntry {
351351

352352
#[repr(C)]
353353
pub union Command {
354-
common: CommonCommand,
354+
pub(super) common: CommonCommand,
355355
identify: IdentifyCommand,
356356
rw: ReadWriteCommand,
357357
create_sq: CreateSQCommand,

src/aero_kernel/src/drivers/block/nvme/queue.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
use core::cell::UnsafeCell;
2+
use core::ptr;
23
use core::sync::atomic::{AtomicU16, Ordering};
34

45
use crate::mem::paging::PhysAddr;
56
use crate::utils::dma::Dma;
7+
use crate::utils::VolatileCell;
68

79
use super::command::{Command, CompletionEntry};
8-
use super::*;
10+
use super::{Error, Registers};
911

1012
const fn calculate_doorbell_offset(queue_id: u16, multiplier: usize, dstrd: usize) -> usize {
1113
0x1000 + ((((queue_id as usize) * 2) + multiplier) * (4 << dstrd))
@@ -136,11 +138,9 @@ impl<'a> QueuePair<'a> {
136138
let mut command = command.into();
137139

138140
unsafe {
139-
// SAFETY: Command Layout:
140-
// - opcode: u8
141-
// - flags: u8
142-
// - command_id: u16 (offset=2 bytes))
143-
*(&mut command as *mut Command as *mut u16).offset(1) = self.cid;
141+
// SAFETY: The offset of the `command_id` field is the same, regardless of the command
142+
// type.
143+
command.common.command_id = self.cid;
144144
}
145145

146146
self.cid += 1;

src/aero_kernel/src/drivers/drm/rawfb.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@
1616
// along with Aero. If not, see <https://www.gnu.org/licenses/>.
1717

1818
use alloc::sync::Arc;
19+
use uapi::drm::DrmModeConStatus;
1920

2021
use crate::fs::{devfs, FileSystem};
2122

2223
use crate::mem::paging::*;
2324

24-
use super::*;
25+
use super::{make_dmt_modes, BufferObject, Connector, Crtc, Drm, DrmDevice, Encoder};
2526
use crate::rendy;
2627

2728
struct RawFramebuffer {}
@@ -54,7 +55,7 @@ impl DrmDevice for RawFramebuffer {
5455
unsafe {
5556
core::ptr::copy_nonoverlapping(
5657
frame.as_slice_mut::<u8>().as_mut_ptr(),
57-
(fb.as_mut_ptr() as *mut u8)
58+
(fb.as_mut_ptr().cast::<u8>())
5859
.offset(i as isize * Size4KiB::SIZE as isize),
5960
4096,
6061
)

src/aero_kernel/src/drivers/e1000.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
// You should have received a copy of the GNU General Public License
1616
// along with Aero. If not, see <https://www.gnu.org/licenses/>.
1717

18+
use core::ptr;
19+
1820
use alloc::boxed::Box;
1921
use alloc::sync::Arc;
2022
use spin::Once;
@@ -570,14 +572,14 @@ impl E1000 {
570572

571573
unsafe fn write_raw(&self, register: u32, value: u32) {
572574
unsafe {
573-
let register = self.base.as_mut_ptr::<u8>().add(register as usize);
574-
core::ptr::write_volatile(register as *mut u32, value);
575+
let register = self.base.as_mut_ptr::<u32>().byte_add(register as usize);
576+
ptr::write_volatile(register, value);
575577
}
576578
}
577579

578580
unsafe fn read_raw(&self, register: u32) -> u32 {
579-
let register = self.base.as_ptr::<u8>().add(register as usize);
580-
core::ptr::read_volatile(register as *const u32)
581+
let register = self.base.as_ptr::<u32>().byte_add(register as usize);
582+
ptr::read_volatile(register)
581583
}
582584
}
583585

src/aero_kernel/src/fs/cache.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ impl Cacheable<INodeCacheKey> for CachedINode {
351351

352352
impl INodeCacheItem {
353353
pub fn make_key(fs: Weak<dyn FileSystem>, id: usize) -> INodeCacheKey {
354-
(Weak::as_ptr(&fs) as *const () as usize, id)
354+
(Weak::as_ptr(&fs).addr(), id)
355355
}
356356
}
357357

src/aero_kernel/src/fs/ext2/disk.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@
1515
// You should have received a copy of the GNU General Public License
1616
// along with Aero. If not, see <https://www.gnu.org/licenses/>.
1717

18+
use core::ptr;
19+
1820
use bit_field::BitField;
1921

2022
use crate::fs::inode;
21-
22-
trait Revsion {}
23+
use crate::utils::IncompleteArrayField;
2324

2425
#[derive(Debug, PartialEq)]
2526
pub enum Revision {
@@ -142,13 +143,14 @@ pub struct GroupDescriptor {
142143

143144
const_assert_eq!(core::mem::size_of::<GroupDescriptor>(), 32);
144145

145-
#[derive(Debug, Copy, Clone)]
146-
#[repr(C, packed)]
146+
#[derive(Debug)]
147+
#[repr(C)]
147148
pub struct DirEntry {
148149
pub inode: u32,
149150
pub entry_size: u16,
150151
pub name_size: u8,
151152
pub file_type: u8,
153+
name: IncompleteArrayField<u8>,
152154
}
153155

154156
impl DirEntry {
@@ -157,10 +159,7 @@ impl DirEntry {
157159

158160
self.name_size = name.len() as u8;
159161

160-
// SAFETY: Above we have verified that the name will fit in the entry.
161-
let name_ptr = unsafe { (self as *mut _ as *mut u8).add(core::mem::size_of::<Self>()) };
162-
let name_bytes = unsafe { core::slice::from_raw_parts_mut(name_ptr, name.len()) };
163-
162+
let name_bytes = unsafe { self.name.as_mut_slice(name.len()) };
164163
name_bytes.copy_from_slice(name.as_bytes());
165164
}
166165
}

src/aero_kernel/src/utils/dma.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use alloc::boxed::Box;
1919

2020
use core::alloc::{AllocError, Allocator, Layout};
2121
use core::mem::MaybeUninit;
22-
use core::ptr::NonNull;
22+
use core::ptr::{self, NonNull};
2323

2424
use crate::mem::paging::*;
2525

@@ -114,7 +114,7 @@ impl<T: ?Sized + core::fmt::Debug> core::fmt::Debug for Dma<T> {
114114
impl<T: ?Sized> Dma<T> {
115115
pub fn addr(&self) -> PhysAddr {
116116
unsafe {
117-
let phys = (&*self.0 as *const T as *const u8) as u64;
117+
let phys = ptr::addr_of!(*self.0).addr() as u64;
118118
PhysAddr::new(phys - crate::PHYSICAL_MEMORY_OFFSET.as_u64())
119119
}
120120
}

src/aero_kernel/src/utils/mod.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use alloc::sync::Arc;
2020
use core::alloc::Layout;
2121
use core::any::Any;
2222
use core::cell::UnsafeCell;
23+
use core::marker::PhantomData;
2324
use core::mem;
2425
use core::ptr::Unique;
2526

@@ -235,3 +236,19 @@ macro_rules! extern_sym {
235236
unsafe { ::core::ptr::addr_of!($sym) }
236237
}};
237238
}
239+
240+
#[repr(C)]
241+
#[derive(Debug, Default)]
242+
pub struct IncompleteArrayField<T>(PhantomData<T>, [T; 0]);
243+
244+
impl<T> IncompleteArrayField<T> {
245+
#[inline]
246+
pub unsafe fn as_mut_ptr(&mut self) -> *mut T {
247+
self as *mut _ as *mut T
248+
}
249+
250+
#[inline]
251+
pub unsafe fn as_mut_slice(&mut self, len: usize) -> &mut [T] {
252+
core::slice::from_raw_parts_mut(self.as_mut_ptr(), len)
253+
}
254+
}

0 commit comments

Comments
 (0)