Skip to content

Commit 1ec9e6c

Browse files
Fix GDT, among other things
Currently in a bootable state. I haven't worked on this project in a while, so many of the fixes in this commit I have forgotten. Sorry!
1 parent a91fa97 commit 1ec9e6c

File tree

12 files changed

+46
-104
lines changed

12 files changed

+46
-104
lines changed

src/protected/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ members = [
66
[profile.release]
77
opt-level = "z"
88
panic = "abort"
9-
lto = true
9+
lto = true
10+
debug = true

src/protected/stage_3/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ mod panic;
77

88
#[no_mangle]
99
pub extern "C" fn third_stage() -> ! {
10-
println!("X");
10+
println!("[Bootloader] [32] Stage 3");
1111

1212
loop {}
1313
}

src/real/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,6 @@ lto = true
1212
[profile.release.package.bootsector]
1313
opt-level = "s"
1414
codegen-units = 1
15+
16+
[profile.release.package.stage_2]
17+
debug = true

src/real/bootsector/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ global_asm!(include_str!("bootstrap.s"));
1616

1717
#[no_mangle]
1818
extern "C" fn rust_start(disk_number: u16) -> ! {
19-
real_mode_println(b"Stage 1");
19+
real_mode_println(b"[Bootloader] [16] Bootsector");
2020

2121
check_int13h_extensions(disk_number);
2222

src/real/stage_2/src/lib.rs

Lines changed: 7 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
#![feature(global_asm, llvm_asm)]
22
#![no_std]
33

4-
// FIXME
5-
#![allow(dead_code, unused_variables)]
6-
74
use shared::linker_symbol;
85
use shared::println;
96
use shared::structures::gdt::{Descriptor, GlobalDescriptorTable, TaskStateSegment};
10-
117
use lazy_static::lazy_static;
128

139
mod panic;
@@ -16,8 +12,6 @@ extern "C" {
1612
fn protected_mode_switch() -> !;
1713
}
1814

19-
global_asm!(include_str!("protected_mode.s"));
20-
2115
lazy_static! {
2216
static ref TSS: TaskStateSegment = {
2317
let mut tss = TaskStateSegment::new();
@@ -26,80 +20,31 @@ lazy_static! {
2620

2721
tss
2822
};
23+
2924
static ref GDT: GlobalDescriptorTable = {
3025
let mut gdt = GlobalDescriptorTable::new();
3126

3227
gdt.add_entry(Descriptor::kernel_code_segment());
3328
gdt.add_entry(Descriptor::kernel_data_segment());
34-
gdt.add_entry(Descriptor::user_code_segment());
35-
gdt.add_entry(Descriptor::user_data_segment());
3629

3730
gdt.add_entry(Descriptor::tss_segment(&TSS));
3831

3932
gdt
4033
};
4134
}
4235

36+
global_asm!(include_str!("protected_mode.s"));
37+
4338
#[no_mangle]
4439
pub fn second_stage() -> ! {
45-
println!("Stage 2");
46-
47-
unsafe {
48-
//GDT.load();
49-
50-
println!("Switching to Protected Mode");
51-
52-
protected_mode_switch();
53-
}
54-
}
55-
56-
fn enter_protected_mode() -> ! {
57-
unsafe {
58-
GDT.load();
59-
}
60-
61-
println!("Switching to Protected Mode");
40+
println!("[Bootloader] [16] Stage 2");
6241

6342
enable_a20();
6443

65-
println!("A20 On");
66-
67-
68-
unsafe {
69-
llvm_asm!("cli" :::: "intel", "volatile");
70-
}
71-
72-
println!("Interrupts off");
73-
74-
let ds: u16;
75-
let es: u16;
76-
77-
unsafe {
78-
llvm_asm!("mov ax, ds
79-
mov bx, es"
80-
: "={ax}"(ds), "={bx}"(es)
81-
::: "intel", "volatile");
82-
}
83-
84-
println!("Segments stored");
85-
8644
unsafe {
87-
llvm_asm!("mov bx, 0x0
88-
mov ds, bx
89-
mov es, bx" ::: "bx" : "intel", "volatile");
90-
}
91-
92-
println!("Segments set");
93-
94-
unsafe {
95-
llvm_asm!("mov eax, cr0
96-
or al, 1
97-
mov cr0, eax
98-
99-
push dx
100-
push cx
101-
102-
jmp third_stage" :: "{dx}"(ds), "{cx}"(es) :: "intel", "volatile");
45+
GDT.load();
46+
47+
protected_mode_switch();
10348
}
10449

10550
unreachable!();

src/real/stage_2/src/protected_mode.s

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
protected_mode_switch:
55
cli
66

7-
lgdt [gdt32info]
8-
97
mov eax, cr0
108
or al, 1
119
mov cr0, eax
@@ -15,36 +13,11 @@ protected_mode_switch:
1513
push eax
1614
retf
1715

16+
.code32
1817
protected_mode:
1918
mov bx, 0x10
19+
2020
mov ds, bx
2121
mov es, bx
2222

23-
jmp third_stage
24-
25-
gdt32info:
26-
.word gdt32_end - gdt32 - 1 # last byte in table
27-
.word gdt32 # start of table
28-
29-
gdt32:
30-
# entry 0 is always unused
31-
.quad 0
32-
codedesc:
33-
.byte 0xff
34-
.byte 0xff
35-
.byte 0
36-
.byte 0
37-
.byte 0
38-
.byte 0x9a
39-
.byte 0xcc
40-
.byte 0
41-
datadesc:
42-
.byte 0xff
43-
.byte 0xff
44-
.byte 0
45-
.byte 0
46-
.byte 0
47-
.byte 0x92
48-
.byte 0xcc
49-
.byte 0
50-
gdt32_end:
23+
jmp third_stage

src/shared/Cargo.toml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,10 @@ spin = "0.5"
1212

1313
[dependencies.lazy_static]
1414
version = "1"
15-
features = ["spin_no_std"]
15+
features = ["spin_no_std"]
16+
17+
[profile.release]
18+
debug = true
19+
opt-level = "z"
20+
panic = "abort"
21+
lto = true

src/shared/src/console.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ lazy_static! {
99
/// Used by the `print!` and `println!` macros.
1010
pub static ref WRITER: Mutex<Writer> = Mutex::new(Writer {
1111
column_position: 0,
12-
color_code: ColorCode::new(Color::Red, Color::Black),
12+
color_code: ColorCode::new(Color::LightGray, Color::Black),
1313
buffer: unsafe { &mut *(0xb8000 as *mut Buffer) },
1414
});
1515
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.intel_syntax noprefix
2+
3+
retf: retf

src/shared/src/instructions/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
global_asm!(include_str!("assembly.s"));
2+
3+
extern "C" {
4+
pub fn retf(cs: u32, eip: u32);
5+
}

0 commit comments

Comments
 (0)