Skip to content

Add a flat limit to all GDT segments #112

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 32 additions & 35 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,43 +51,40 @@ jobs:
name: 'Build Test Kernel'

- name: 'Build Bootloader'
run: cargo xbuild --bin bootloader --features binary --release
env:
KERNEL: "test-kernel/target/x86_64-test-kernel/debug/test-kernel"
KERNEL_MANIFEST: "test-kernel/Cargo.toml"
run: cargo xbuild --release

- name: 'Convert Bootloader ELF to Binary'
run: cargo objcopy -- -I elf64-x86-64 -O binary --binary-architecture=i386:x86-64 target/x86_64-bootloader/release/bootloader target/x86_64-bootloader/release/bootloader.bin

# install QEMU
- name: Install QEMU (Linux)
run: sudo apt update && sudo apt install qemu-system-x86
if: runner.os == 'Linux'
- name: Install QEMU (macOS)
run: brew install qemu
if: runner.os == 'macOS'
env:
HOMEBREW_NO_AUTO_UPDATE: 1
HOMEBREW_NO_BOTTLE_SOURCE_FALLBACK: 1
HOMEBREW_NO_INSTALL_CLEANUP: 1
- name: Install Scoop (Windows)
run: |
Invoke-Expression (New-Object System.Net.WebClient).DownloadString('https://get.scoop.sh')
echo ::add-path::$HOME\scoop\shims
if: runner.os == 'Windows'
shell: pwsh
- name: Install QEMU (Windows)
run: scoop install qemu
if: runner.os == 'Windows'
shell: pwsh
- name: "Print QEMU Version"
run: qemu-system-x86_64 --version

- name: 'Run Test Kernel with Bootloader'
run: |
qemu-system-x86_64 -drive format=raw,file=target/x86_64-bootloader/release/bootloader.bin -device isa-debug-exit,iobase=0xf4,iosize=0x04 -display none
if [ $? -eq 123 ]; then (exit 0); else (exit 1); fi
shell: 'bash {0}'
run: cargo objcopy -- -I elf64-i386 -O binary --binary-architecture=i386:x86-64 target/x86_64-real_mode/release/bootsector target/x86_64-real_mode/release/bootsector.bin

# # install QEMU
# - name: Install QEMU (Linux)
# run: sudo apt update && sudo apt install qemu-system-x86
# if: runner.os == 'Linux'
# - name: Install QEMU (macOS)
# run: brew install qemu
# if: runner.os == 'macOS'
# env:
# HOMEBREW_NO_AUTO_UPDATE: 1
# HOMEBREW_NO_BOTTLE_SOURCE_FALLBACK: 1
# HOMEBREW_NO_INSTALL_CLEANUP: 1
# - name: Install Scoop (Windows)
# run: |
# Invoke-Expression (New-Object System.Net.WebClient).DownloadString('https://get.scoop.sh')
# echo ::add-path::$HOME\scoop\shims
# if: runner.os == 'Windows'
# shell: pwsh
# - name: Install QEMU (Windows)
# run: scoop install qemu
# if: runner.os == 'Windows'
# shell: pwsh
# - name: "Print QEMU Version"
# run: qemu-system-x86_64 --version

# - name: 'Run Test Kernel with Bootloader'
# run: |
# qemu-system-x86_64 -drive format=raw,file=target/x86_64-bootloader/release/bootloader.bin -device isa-debug-exit,iobase=0xf4,iosize=0x04 -display none
# if [ $? -eq 123 ]; then (exit 0); else (exit 1); fi
# shell: 'bash {0}'


build_example_kernel:
Expand Down
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ members = [
"src/shared",
"src/v86",
"src/bootsector",
"src/stage_2"
"src/stage_2",
"example-kernel",
"test-kernel",
]

[profile.release]
Expand Down
28 changes: 22 additions & 6 deletions src/v86/src/gdt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ impl GlobalDescriptorTable {
limit: (self.table.len() * size_of::<u64>() - 1) as u16,
};

llvm_asm!("lgdt ($0)" :: "r" (&ptr) : "memory");
unsafe {
llvm_asm!("lgdt ($0)" :: "r" (&ptr) : "memory");
}
}

#[inline]
Expand Down Expand Up @@ -87,6 +89,9 @@ bitflags! {

/// The DPL for this descriptor is Ring 3
const DPL_RING_3 = 3 << 45;

/// If set, limit is in 4k pages
const GRANULARITY = 1 << 55;
}
}

Expand All @@ -98,7 +103,7 @@ impl Descriptor {

let flags =
Flags::USER_SEGMENT | Flags::PRESENT | Flags::EXECUTABLE | Flags::READABLE_WRITABLE;
Descriptor(flags.bits())
Descriptor(flags.bits()).with_flat_limit()
}

/// Creates a segment descriptor for a protected mode kernel data segment.
Expand All @@ -107,7 +112,7 @@ impl Descriptor {
use self::DescriptorFlags as Flags;

let flags = Flags::USER_SEGMENT | Flags::PRESENT | Flags::READABLE_WRITABLE;
Descriptor(flags.bits())
Descriptor(flags.bits()).with_flat_limit()
}

/// Creates a segment descriptor for a protected mode ring 3 data segment.
Expand All @@ -117,7 +122,7 @@ impl Descriptor {

let flags =
Flags::USER_SEGMENT | Flags::PRESENT | Flags::READABLE_WRITABLE | Flags::DPL_RING_3;
Descriptor(flags.bits())
Descriptor(flags.bits()).with_flat_limit()
}

/// Creates a segment descriptor for a protected mode ring 3 code segment.
Expand All @@ -130,7 +135,7 @@ impl Descriptor {
| Flags::EXECUTABLE
| Flags::DPL_RING_3
| Flags::READABLE_WRITABLE;
Descriptor(flags.bits())
Descriptor(flags.bits()).with_flat_limit()
}

/// Creates a TSS system descriptor for the given TSS.
Expand All @@ -152,6 +157,17 @@ impl Descriptor {

Descriptor(val)
}

fn with_flat_limit(mut self) -> Self {
// limit_low
self.0.set_bits(0..16, 0xffff);
// limit high
self.0.set_bits(48..52, 0xff);
// granularity
self.0 |= DescriptorFlags::GRANULARITY.bits();

self
}
}

#[derive(Debug, Clone, Copy)]
Expand Down Expand Up @@ -224,7 +240,7 @@ pub struct Stack {
}

impl Stack {
fn zero() -> Self {
const fn zero() -> Self {
Stack { esp: 0, ss: 0 }
}
}
1 change: 1 addition & 0 deletions src/v86/src/idt.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use core::marker::PhantomData;
use bit_field::BitField;

/// An Interrupt Descriptor Table with 32 entries.
#[derive(Clone)]
Expand Down