Skip to content

Commit 4174de8

Browse files
committed
Increase alginment for packed structures
While both TaskStateSegment and DescriptorTablePointer can't have their normal C alignment due to their definition. However, this doesn't mean their alignment has to be `1`. This PR sets their alignment to be the max possible while still preserving their ABI definied structure. I added some tests to make sure there is not a regression. This can also result in more efficient code generation on platofrms without unalgined loads. https://rust.godbolt.org/z/EPbbMxeq1 Signed-off-by: Joe Richey <joerichey@google.com>
1 parent aab60f7 commit 4174de8

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

src/structures/mod.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,28 @@ pub mod tss;
1313
/// A struct describing a pointer to a descriptor table (GDT / IDT).
1414
/// This is in a format suitable for giving to 'lgdt' or 'lidt'.
1515
#[derive(Debug, Clone, Copy)]
16-
#[repr(C, packed)]
16+
#[repr(C, packed(2))]
1717
pub struct DescriptorTablePointer {
1818
/// Size of the DT.
1919
pub limit: u16,
2020
/// Pointer to the memory region containing the DT.
2121
pub base: VirtAddr,
2222
}
23+
24+
#[cfg(test)]
25+
mod tests {
26+
use super::*;
27+
use std::mem::size_of;
28+
29+
#[test]
30+
pub fn check_descriptor_pointer_size() {
31+
// Per the SDM, a descriptor pointer has to be 2+8=10 bytes
32+
assert_eq!(size_of::<DescriptorTablePointer>(), 10);
33+
// Make sure that we can reference a pointer's limit
34+
let p = DescriptorTablePointer {
35+
limit: 5,
36+
base: VirtAddr::zero(),
37+
};
38+
let _: &u16 = &p.limit;
39+
}
40+
}

src/structures/tss.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use core::mem::size_of;
88
/// but is used for finding kernel level stack
99
/// if interrupts arrive while in kernel mode.
1010
#[derive(Debug, Clone, Copy)]
11-
#[repr(C, packed)]
11+
#[repr(C, packed(4))]
1212
pub struct TaskStateSegment {
1313
reserved_1: u32,
1414
/// The full 64-bit canonical forms of the stack pointers (RSP) for privilege levels 0-2.

0 commit comments

Comments
 (0)