Skip to content

Commit cd0d5de

Browse files
committed
[cpu] add Default implementations for a bunch of stuff in cpu
1 parent 60347b2 commit cd0d5de

File tree

4 files changed

+22
-21
lines changed

4 files changed

+22
-21
lines changed

cpu/src/x86_all/interrupts/idt/gate32.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ extern {
2424
/// Based on [code](http://wiki.osdev.org/Interrupt_Descriptor_Table#Structure)
2525
/// from the OS Dev Wiki.
2626
#[repr(C, packed)]
27-
#[derive(Copy,Clone)]
27+
#[derive(Copy,Clone, Default)]
2828
pub struct Gate { /// bits 0 - 15 of the offset
2929
pub offset_lower: u16
3030
, /// code segment selector (GDT or LDT)

cpu/src/x86_all/interrupts/idt/gate64.rs

+5-18
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,7 @@ use ::segment;
1111
use super::{Handler, GateFlags};
1212

1313
use core::{convert, mem};
14-
15-
extern {
16-
/// Offset of the 64-bit GDT main code segment.
17-
/// Exported by `boot.asm`
18-
#[link_name = "gdt64_offset"]
19-
static GDT_OFFSET: u16;
20-
}
14+
use core::default::Default;
2115

2216
impl GateFlags {
2317

@@ -38,6 +32,7 @@ impl GateFlags {
3832

3933
}
4034

35+
4136
/// An IDT entry is called a gate.
4237
///
4338
/// Based on [code](http://wiki.osdev.org/Interrupt_Descriptor_Table#Structure)
@@ -47,7 +42,7 @@ impl GateFlags {
4742
/// Gate-Descriptor Types" in the _Intel® 64 and IA-32 Architectures
4843
/// Software Developer’s Manual_
4944
#[repr(C, packed)]
50-
#[derive(Copy,Clone)]
45+
#[derive(Copy, Clone, Default)]
5146
pub struct Gate { /// bits 0 - 15 of the offset
5247
pub offset_lower: u16
5348
, /// code segment selector (GDT or LDT)
@@ -101,14 +96,10 @@ impl convert::From<Handler> for Gate {
10196
let (low, mid, high): (u16, u16, u32) = mem::transmute(handler);
10297

10398
Gate { offset_lower: low
104-
, selector: segment::Selector::from_raw(GDT_OFFSET)
105-
, _zero: 0
106-
// Bit 7 is the present bit
107-
// Bits 4-0 indicate this is an interrupt gate
10899
, flags: GateFlags::new_interrupt()
109100
, offset_mid: mid
110101
, offset_upper: high
111-
, _reserved: 0
102+
, ..Default::default()
112103
}
113104
}
114105
}
@@ -130,14 +121,10 @@ impl convert::From<*const u8> for Gate {
130121
let (low, mid, high): (u16, u16, u32) = mem::transmute(handler);
131122

132123
Gate { offset_lower: low
133-
, selector: segment::Selector::from_raw(GDT_OFFSET)
134-
, _zero: 0
135-
// Bit 7 is the present bit
136-
// Bits 4-0 indicate this is an interrupt gate
137124
, flags: GateFlags::new_interrupt()
138125
, offset_mid: mid
139126
, offset_upper: high
140-
, _reserved: 0
127+
, ..Default::default()
141128
}
142129
}
143130
}

cpu/src/x86_all/interrupts/idt/mod.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ bitflags! {
5454
/// - `0111`: 80286 16-bit trap gate
5555
/// - `1110`: 80386 32-bit interrupt gate
5656
/// - `1111`: 80386 32-bit trap gate
57-
///
57+
///
5858
/// For more information, refer to the _Intel® 64 and IA-32 Architectures
5959
/// Software Developer’s Manual_, Vol. 3A, section 6.11, "IDT Descriptors";
6060
/// and to the OS Dev Wiki
@@ -131,6 +131,11 @@ impl GateFlags {
131131

132132
}
133133

134+
135+
impl Default for GateFlags {
136+
fn default() -> Self { GateFlags { bits: 0 } }
137+
}
138+
134139
//==------------------------------------------------------------------------==
135140
// IDT implementation
136141
/// An Interrupt Descriptor Table

cpu/src/x86_all/segment.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
#![warn(missing_docs)]
2121

2222
use core::{fmt, mem};
23+
use core::default::Default;
24+
2325
use super::PrivilegeLevel;
2426

2527
/// The number of entries in the GDT
@@ -197,6 +199,11 @@ impl Selector {
197199

198200
}
199201

202+
203+
impl Default for Selector {
204+
#[inline] fn default() -> Self { Selector::from_cs() }
205+
}
206+
200207
impl fmt::Display for Selector {
201208
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
202209
// TODO: this could be much less ugly.
@@ -266,9 +273,11 @@ impl Descriptor {
266273
self.flags.get_limit_part() & self.limit as u32
267274
}
268275

269-
270276
}
271277

278+
impl Default for Descriptor {
279+
#[inline] fn default() -> Self { Descriptor::null() }
280+
}
272281

273282
bitflags! {
274283
/// Segment descriptor bitflags field.

0 commit comments

Comments
 (0)