rust-x86 icon indicating copy to clipboard operation
rust-x86 copied to clipboard

Add lifetimes for GDT

Open YtvwlD opened this issue 2 years ago • 2 comments

I was happy to be able to use this crate for creating a GDT like this:

let gdt = DescriptorTablePointer::new_from_slice(
    &[Descriptor::NULL, code_segment, data_segment]
);
unsafe {
    x86::dtables::lgdt(&gdt)
    // inline assembly with a far-jump goes here
}

This worked in a debug build, but failed in a release build because the array containing the descriptors did not live long enough. I spent some time finding this issue, so I figured it might be nice to express these constraints via lifetimes.

I'm not sure about disallowing access to the limit and base pointer but they probably shouldn't be writable.

YtvwlD avatar Dec 04 '23 11:12 YtvwlD

Thanks, any reason why you'd need a non-static lifetime in the new_from_slice? e.g., would this be simpler:

pub fn new_from_slice(slice: &'static [T])

gz avatar Dec 29 '23 21:12 gz

I'm not exactly sure. If you're writing a kernel, then you'll most likely load it with lgdt, so it would have to be 'static either way.

Not requiring it might be helpful for tests or VMs or something like that, but I don't know. I would be fine with the static lifetime just in new and new_from_slice.

YtvwlD avatar Jan 19 '24 14:01 YtvwlD