Skip to content

Commit 988d530

Browse files
committed
wip
1 parent 0d90f03 commit 988d530

File tree

10 files changed

+85
-29
lines changed

10 files changed

+85
-29
lines changed

build.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,10 @@ fn main() {
99
cmd.arg("--manifest-path").arg("protected_mode/Cargo.toml");
1010
cmd.arg("--target").arg("protected_mode/i686-bootloader.json");
1111
cmd.env("RUSTFLAGS", "");
12-
cmd.status().unwrap();
12+
assert!(cmd.status().unwrap().success());
1313

1414
let out_dir = Path::new("protected_mode/target/i686-bootloader/release").canonicalize().unwrap();
1515

16-
println!("cargo:rustc-link-search=native={}", out_dir.display());
17-
println!("cargo:rustc-link-lib=static=protected_mode");
18-
19-
println!("cargo:rerun-if-changed=protected_mode");
16+
println!("cargo:rerun-if-changed=protected_mode/*");
17+
println!("cargo:rerun-if-changed=protected_mode/**/*");
2018
}

linker.ld

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
ENTRY(entry_point)
1+
ENTRY(first_stage)
22

33
SECTIONS {
44
. = 0x500;

protected_mode/Cargo.lock

Lines changed: 19 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

protected_mode/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ edition = "2018"
88
crate-type = ["staticlib"]
99

1010
[dependencies]
11-
x86_64 = "0.3"
11+
x86_64 = "0.3.3"

protected_mode/src/lib.rs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,20 @@
33

44
#![no_std]
55

6-
use core::panic::PanicInfo;
7-
86
global_asm!(include_str!("e820.s"));
97

108
pub mod stages;
9+
mod printer;
1110

12-
extern "C" {
13-
pub fn first_stage();
14-
}
15-
16-
#[no_mangle]
17-
pub extern "C" fn hello_world() {
18-
unsafe { first_stage() }
19-
}
11+
use core::panic::PanicInfo;
2012

2113
#[panic_handler]
22-
#[no_mangle]
23-
pub extern "C" fn panic(_info: &PanicInfo) -> ! {
14+
extern "C" fn panic(_info: &PanicInfo) -> ! {
2415
loop {}
2516
}
2617

2718
#[lang = "eh_personality"]
2819
#[no_mangle]
29-
pub extern "C" fn eh_personality() {
20+
extern "C" fn eh_personality() {
3021
loop {}
3122
}

protected_mode/src/stages/stage_1.s

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ load_bootloader_rest_from_disk:
5151
mov [dap_blocks], bx
5252

5353
# number of start block
54-
lea ebx, _start
54+
lea ebx, first_stage
5555
sub eax, ebx
5656
shr eax, 9 # divide by 512 (block size)
5757
mov [dap_start_lba], eax

protected_mode/src/stages/stage_2.s

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ load_kernel_from_disk:
3333

3434
# number of start block
3535
lea eax, _kernel_start_addr
36-
lea ebx, _start
36+
lea ebx, first_stage
3737
sub eax, ebx
3838
shr eax, 9 # divide by 512 (block size)
3939
mov [dap_start_lba], eax

protected_mode/src/stages/stage_3.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,46 @@
1+
use x86_64::{
2+
ux::u9,
3+
PhysAddr,
4+
structures::paging::{PageTable, PageTableFlags, PhysFrame, Size2MiB},
5+
};
6+
7+
8+
extern {
9+
static mut _p4: PageTable;
10+
static mut _p3: PageTable;
11+
static mut _p2: PageTable;
12+
}
13+
114
#[no_mangle]
215
extern "C" fn stage_3() -> ! {
16+
// Identity mapping of first gigabyte + recursive mapping of P4 table
17+
/*unsafe {
18+
let p4_addr = PhysAddr::new(&P4 as *const PageTable as u64);
19+
let p3_addr = PhysAddr::new(&P3 as *const PageTable as u64);
20+
let p2_addr = PhysAddr::new(&P2 as *const PageTable as u64);
21+
22+
P4[0].set_addr(p3_addr, PageTableFlags::PRESENT | PageTableFlags::WRITABLE);
23+
P4[511].set_addr(p4_addr, PageTableFlags::PRESENT | PageTableFlags::WRITABLE);
24+
P3[0].set_addr(p2_addr, PageTableFlags::PRESENT | PageTableFlags::WRITABLE);
25+
{
26+
let flags = PageTableFlags::PRESENT | PageTableFlags::WRITABLE | PageTableFlags::HUGE_PAGE;
27+
let start_frame = PhysFrame::<Size2MiB>::containing_address(PhysAddr::new(0));
28+
for i in 0u16..512 {
29+
P2[u9::new(i)].set_addr((start_frame + i.into()).start_address(), flags);
30+
}
31+
}
32+
}
33+
*/
34+
35+
use core::fmt::Write;
36+
unsafe {
37+
write!(crate::printer::Printer, "P4: {}, P3: {}, P2 {}", &_p4 as *const _ as u32, &_p3 as *const _ as u32, &_p2 as *const _ as u32);
38+
}
39+
340
let ptr = 0xb8200 as *mut u16;
441
unsafe {
542
*ptr = 0xffff;
643
}
744
loop {}
845
}
46+

src/main.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,26 @@
1+
#![feature(lang_items)]
2+
13
#![no_std]
24
#![no_main]
35

46
use core::panic::PanicInfo;
57

68
extern "C" {
7-
fn hello_world();
9+
fn first_stage();
810
}
911

1012
#[no_mangle]
11-
pub extern "C" fn entry_point() {
12-
unsafe {hello_world();}
13+
pub extern "C" fn ensure_that_first_stage_is_callable() {
14+
unsafe {first_stage();}
1315
}
1416

1517
#[panic_handler]
16-
extern "C" fn panic(info: &PanicInfo) -> ! {
18+
extern "C" fn panic(_info: &PanicInfo) -> ! {
19+
loop {}
20+
}
21+
22+
#[lang = "eh_personality"]
23+
#[no_mangle]
24+
pub extern "C" fn eh_personality() {
1725
loop {}
1826
}

x86_64-bootloader.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
"--script=linker.ld"
1010
]
1111
},
12+
"post-link-args": {
13+
"ld.lld": [
14+
"protected_mode/target/i686-bootloader/release/libprotected_mode.rlib"
15+
]
16+
},
1217
"target-endian": "little",
1318
"target-pointer-width": "32",
1419
"target-c-int-width": "32",

0 commit comments

Comments
 (0)