Compare commits

..

2 Commits

Author SHA1 Message Date
Philipp Oppermann
9258e04b7a Update to latest version 2020-07-07 11:22:06 +02:00
Philipp Oppermann
705435da8a Test for new idt::set_default_handler macro of x86_64 crate 2020-07-06 17:05:42 +02:00
14 changed files with 270 additions and 235 deletions

5
.cargo/config Normal file
View File

@@ -0,0 +1,5 @@
[build]
target = "x86_64-blog_os.json"
[target.'cfg(target_os = "none")']
runner = "bootimage runner"

View File

@@ -1,9 +0,0 @@
[unstable]
build-std = ["core", "compiler_builtins", "alloc"]
build-std-features = ["compiler-builtins-mem"]
[build]
target = "x86_64-blog_os.json"
[target.'cfg(target_os = "none")']
runner = "bootimage runner"

96
.github/workflows/build-code.yml vendored Normal file
View File

@@ -0,0 +1,96 @@
name: Build Code
on:
push:
branches:
- '*'
- '!staging.tmp'
tags:
- '*'
schedule:
- cron: '40 3 * * *' # every day at 3:40
pull_request:
jobs:
test:
name: "Test"
strategy:
matrix:
platform: [
ubuntu-latest,
macos-latest,
windows-latest
]
runs-on: ${{ matrix.platform }}
timeout-minutes: 15
steps:
- name: "Checkout Repository"
uses: actions/checkout@v1
- name: Install Rustup
run: |
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --default-toolchain nightly
echo ::add-path::$HOME/.cargo/bin
if: runner.os == 'macOS'
- name: "Print Rust Version"
run: |
rustc -Vv
cargo -Vv
- name: "Install Rustup Components"
run: rustup component add rust-src llvm-tools-preview
- name: "Install cargo-xbuild"
run: cargo install cargo-xbuild --debug
- name: "Install bootimage"
run: cargo install bootimage --debug
- name: "Run cargo xbuild"
run: cargo xbuild
- name: "Create Bootimage"
run: cargo bootimage
# 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 cargo xtest"
run: cargo xtest
check_formatting:
name: "Check Formatting"
runs-on: ubuntu-latest
timeout-minutes: 2
steps:
- uses: actions/checkout@v1
- name: "Use the latest Rust nightly with rustfmt"
uses: actions-rs/toolchain@v1
with:
toolchain: nightly
profile: minimal
components: rustfmt
override: true
- run: cargo fmt -- --check

View File

@@ -1,130 +0,0 @@
name: Code
on:
push:
branches:
- '*'
- '!staging.tmp'
tags:
- '*'
schedule:
- cron: '40 3 * * *' # every day at 3:40
pull_request:
workflow_dispatch:
jobs:
check:
name: Check
strategy:
fail-fast: false
matrix:
platform: [
ubuntu-latest,
macos-latest,
windows-latest
]
runs-on: ${{ matrix.platform }}
steps:
- name: Checkout Repository
uses: actions/checkout@v2
- name: Install Rust Toolchain
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: nightly
- name: Install `rust-src` Rustup Component
run: rustup component add rust-src
- name: Run `cargo check`
uses: actions-rs/cargo@v1
with:
command: check
test:
name: Test
strategy:
fail-fast: false
matrix:
platform: [
ubuntu-latest,
macos-latest,
windows-latest
]
runs-on: ${{ matrix.platform }}
steps:
- name: Install Rust Toolchain
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: nightly
- name: Install bootimage
run: cargo install bootimage --debug
- name: Checkout Repository
uses: actions/checkout@v2
- name: Install Rustup Components
run: rustup component add rust-src llvm-tools-preview
- name: Run `cargo bootimage`
uses: actions-rs/cargo@v1
with:
command: bootimage
# 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 QEMU (Windows)
run: |
choco install qemu --version 2021.5.5
echo "$Env:Programfiles\qemu" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append
if: runner.os == 'Windows'
shell: pwsh
- name: "Print QEMU Version"
run: qemu-system-x86_64 --version
- name: Run `cargo test`
uses: actions-rs/cargo@v1
with:
command: test
check_formatting:
name: Check Formatting
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v2
- name: Install Rust Toolchain
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: nightly
components: rustfmt
override: true
- name: Run `cargo fmt`
uses: actions-rs/cargo@v1
with:
command: fmt
args: --all -- --check
clippy:
name: Clippy
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v2
- name: Install Rust Toolchain
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: nightly
components: clippy, rust-src
override: true
- name: Run `cargo clippy`
uses: actions-rs/cargo@v1
with:
command: clippy

157
Cargo.lock generated
View File

@@ -1,18 +1,16 @@
# This file is automatically @generated by Cargo. # This file is automatically @generated by Cargo.
# It is not intended for manual editing. # It is not intended for manual editing.
version = 3
[[package]] [[package]]
name = "autocfg" name = "autocfg"
version = "1.0.1" version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" checksum = "f8aac770f1885fd7e387acedd76065302551364496e46b3dd00860b2f8359b9d"
[[package]] [[package]]
name = "bit_field" name = "bit_field"
version = "0.10.1" version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dcb6dd1c2376d2e096796e234a70e17e94cc2d5d54ff8ce42b28cef1d0d359a4" checksum = "ed8765909f9009617974ab6b7d332625b320b33c326b1e9321382ef1999b5d56"
[[package]] [[package]]
name = "bitflags" name = "bitflags"
@@ -31,18 +29,18 @@ dependencies = [
"lazy_static", "lazy_static",
"linked_list_allocator", "linked_list_allocator",
"pc-keyboard", "pc-keyboard",
"pic8259", "pic8259_simple",
"spin", "spin",
"uart_16550", "uart_16550",
"volatile 0.2.7", "volatile",
"x86_64", "x86_64 0.11.1",
] ]
[[package]] [[package]]
name = "bootloader" name = "bootloader"
version = "0.9.23" version = "0.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b6e02311b16c9819e7c72866d379cdd3026c3b7b25c1edf161f548f8e887e7ff" checksum = "44ac0bdf4930c3c4d7f0d04eb6f15d7dcb9d5972b1ff9cd2bee0128112260fc7"
[[package]] [[package]]
name = "cfg-if" name = "cfg-if"
@@ -52,9 +50,9 @@ checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
[[package]] [[package]]
name = "conquer-once" name = "conquer-once"
version = "0.2.1" version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "96eb12fb69466716fbae9009d389e6a30830ae8975e170eff2d2cff579f9efa3" checksum = "6f7644600a548ecad74e4a918392af1798f7dd045be610be3203b9e129b4f98f"
dependencies = [ dependencies = [
"conquer-util", "conquer-util",
] ]
@@ -66,14 +64,19 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "654fb2472cc369d311c547103a1fa81d467bef370ae7a0680f65939895b1182a" checksum = "654fb2472cc369d311c547103a1fa81d467bef370ae7a0680f65939895b1182a"
[[package]] [[package]]
name = "crossbeam-queue" name = "cpuio"
version = "0.2.3" version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "774ba60a54c213d409d5353bda12d49cd68d14e45036a285234c8d6f91f92570" checksum = "d531514efb06912141fa65967447de805691b685a7565c87d1765afe34a98aa7"
[[package]]
name = "crossbeam-queue"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c695eeca1e7173472a32221542ae469b3e9aac3a4fc81f7696bcad82029493db"
dependencies = [ dependencies = [
"cfg-if", "cfg-if",
"crossbeam-utils", "crossbeam-utils",
"maybe-uninit",
] ]
[[package]] [[package]]
@@ -88,26 +91,24 @@ dependencies = [
[[package]] [[package]]
name = "futures-core" name = "futures-core"
version = "0.3.15" version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0402f765d8a89a26043b889b26ce3c4679d268fa6bb22cd7c6aad98340e179d1" checksum = "f25592f769825e89b92358db00d26f965761e094951ac44d3663ef25b7ac464a"
[[package]] [[package]]
name = "futures-task" name = "futures-task"
version = "0.3.15" version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a16bef9fc1a4dddb5bee51c989e3fbba26569cbb0e31f5b303c184e3dd33dae" checksum = "7b0a34e53cf6cdcd0178aa573aed466b646eb3db769570841fda0c7ede375a27"
[[package]] [[package]]
name = "futures-util" name = "futures-util"
version = "0.3.15" version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "feb5c238d27e2bf94ffdfd27b2c29e3df4a68c4193bb6427384259e2bf191967" checksum = "22766cf25d64306bedf0384da004d05c9974ab104fcc4528f1236181c18004c5"
dependencies = [ dependencies = [
"autocfg",
"futures-core", "futures-core",
"futures-task", "futures-task",
"pin-project-lite",
"pin-utils", "pin-utils",
] ]
@@ -122,54 +123,60 @@ dependencies = [
[[package]] [[package]]
name = "linked_list_allocator" name = "linked_list_allocator"
version = "0.9.0" version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d0b725207570aa16096962d0b20c79f8a543df2280bd3c903022b9b0b4d7ea68" checksum = "18c618c431dfe4419afbe22852f6aceffbc17bd82ba0a18b982def291000824c"
dependencies = [ dependencies = [
"spinning_top", "spinning_top",
] ]
[[package]] [[package]]
name = "lock_api" name = "lock_api"
version = "0.4.4" version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0382880606dff6d15c9476c416d18690b72742aa7b605bb6dd6ec9030fbf07eb" checksum = "79b2de95ecb4691949fea4716ca53cdbcfccb2c612e19644a8bad05edcf9f47b"
dependencies = [ dependencies = [
"scopeguard", "scopeguard",
] ]
[[package]]
name = "maybe-uninit"
version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "60302e4db3a61da70c0cb7991976248362f30319e88850c487b9b95bbf059e00"
[[package]] [[package]]
name = "pc-keyboard" name = "pc-keyboard"
version = "0.5.1" version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c6f2d937e3b8d63449b01401e2bae4041bc9dd1129c2e3e0d239407cf6635ac" checksum = "c48392db76c4e9a69e0b3be356c5f97ebb7b14413c5e4fd0af4755dbf86e2fce"
[[package]] [[package]]
name = "pic8259" name = "pic8259_simple"
version = "0.10.1" version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "08cc920d83ee33c0f9b73aa441e75468bf2d10c959a3eb6260cf720b05ac91a1" checksum = "af2a5497fb8e59bf8015f67b7dff238d75ef445e03f23edac24ac3a8f09be952"
dependencies = [ dependencies = [
"x86_64", "cpuio",
] ]
[[package]] [[package]]
name = "pin-project-lite" name = "pin-utils"
version = "0.2.6" version = "0.1.0-alpha.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc0e1f259c92177c30a4c9d177246edd0a3568b25756a977d0632cf8fa37e905" checksum = "5894c618ce612a3fa23881b152b608bafb8c56cfc22f434a3ba3120b40f7b587"
[[package]] [[package]]
name = "pin-utils" name = "proc-macro2"
version = "0.1.0" version = "1.0.18"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" checksum = "beae6331a816b1f65d04c45b078fd8e6c93e8071771f41b8163255bbd8d7c8fa"
dependencies = [
"unicode-xid",
]
[[package]]
name = "quote"
version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aa563d17ecb180e500da1cfd2b028310ac758de548efdd203e18f283af693f37"
dependencies = [
"proc-macro2",
]
[[package]] [[package]]
name = "scopeguard" name = "scopeguard"
@@ -185,42 +192,70 @@ checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d"
[[package]] [[package]]
name = "spinning_top" name = "spinning_top"
version = "0.2.4" version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "75adad84ee84b521fb2cca2d4fd0f1dab1d8d026bda3c5bea4ca63b5f9f9293c" checksum = "32d801a3a53bcf5071f85fef8d5cab9e5f638fc5580a37e6eb7aba4b37438d24"
dependencies = [ dependencies = [
"lock_api", "lock_api",
] ]
[[package]] [[package]]
name = "uart_16550" name = "syn"
version = "0.2.14" version = "1.0.33"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "503a6c0e6d82daa87985e662d120c0176b09587c92a68db22781b28ae95405dd" checksum = "e8d5d96e8cbb005d6959f119f773bfaebb5684296108fb32600c00cde305b2cd"
dependencies = [ dependencies = [
"bitflags", "proc-macro2",
"x86_64", "quote",
"unicode-xid",
] ]
[[package]] [[package]]
name = "volatile" name = "uart_16550"
version = "0.2.7" version = "0.2.7"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f6b06ad3ed06fef1713569d547cdbdb439eafed76341820fb0e0344f29a41945" checksum = "e58fc40dc1712664fc9b0a7bd8ca2f21ab49960924fb245a80a05e1e92f3dfe9"
dependencies = [
"bitflags",
"x86_64 0.11.0",
]
[[package]]
name = "unicode-xid"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564"
[[package]] [[package]]
name = "volatile" name = "volatile"
version = "0.4.4" version = "0.2.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e4c2dbd44eb8b53973357e6e207e370f0c1059990df850aca1eca8947cf464f0" checksum = "6af0edf5b4faacc31fc51159244d78d65ec580f021afcef7bd53c04aeabc7f29"
[[package]] [[package]]
name = "x86_64" name = "x86_64"
version = "0.14.7" version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fb611915c917c6296d11e23f71ff1ecfe49c5766daba92cd3df52df6b58285b6" checksum = "365de37eb7c6da582cbb510dd0f3f1235d24ff6309a8a96e8a9909cc9bfd608f"
dependencies = [ dependencies = [
"bit_field", "bit_field",
"bitflags", "bitflags",
"volatile 0.4.4", ]
[[package]]
name = "x86_64"
version = "0.11.1"
dependencies = [
"bit_field",
"bitflags",
"x86_64-idt-general-handler",
]
[[package]]
name = "x86_64-idt-general-handler"
version = "0.1.0"
dependencies = [
"proc-macro2",
"quote",
"syn",
] ]

View File

@@ -13,14 +13,14 @@ name = "stack_overflow"
harness = false harness = false
[dependencies] [dependencies]
bootloader = { version = "0.9.8", features = ["map_physical_memory"]} bootloader = { version = "0.9.3", features = ["map_physical_memory"]}
volatile = "0.2.6" volatile = "0.2.6"
spin = "0.5.2" spin = "0.5.2"
x86_64 = "0.14.2" x86_64 = { path = "../../x86_64" }
uart_16550 = "0.2.0" uart_16550 = "0.2.0"
pic8259 = "0.10.1" pic8259_simple = "0.2.0"
pc-keyboard = "0.5.0" pc-keyboard = "0.5.0"
linked_list_allocator = "0.9.0" linked_list_allocator = "0.8.0"
[dependencies.lazy_static] [dependencies.lazy_static]
version = "1.0" version = "1.0"

View File

@@ -1,6 +1,6 @@
# Blog OS (Async/Await) # Blog OS (Async/Await)
[![Build Status](https://github.com/phil-opp/blog_os/workflows/Code/badge.svg?branch=post-12)](https://github.com/phil-opp/blog_os/actions?query=workflow%3A%22Code%22+branch%3Apost-12) [![Build Status](https://github.com/phil-opp/blog_os/workflows/Build%20Code/badge.svg?branch=post-12)](https://github.com/phil-opp/blog_os/actions?query=workflow%3A%22Build+Code%22+branch%3Apost-12)
This repository contains the source code for the [Async/Await][post] post of the [Writing an OS in Rust](https://os.phil-opp.com) series. This repository contains the source code for the [Async/Await][post] post of the [Writing an OS in Rust](https://os.phil-opp.com) series.
@@ -10,23 +10,19 @@ This repository contains the source code for the [Async/Await][post] post of the
## Building ## Building
This project requires a nightly version of Rust because it uses some unstable features. At least nightly _2020-07-15_ is required for building. You might need to run `rustup update nightly --force` to update to the latest nightly even if some components such as `rustfmt` are missing it. You need a nightly Rust compiler. First you need to install the `cargo-xbuild` and `bootimage` tools:
You can build the project by running:
``` ```
cargo build cargo install cargo-xbuild bootimage
``` ```
To create a bootable disk image from the compiled kernel, you need to install the [`bootimage`] tool: Then you can build the project by running:
[`bootimage`]: https://github.com/rust-osdev/bootimage
``` ```
cargo install bootimage cargo xbuild
``` ```
After installing, you can create the bootable disk image by running: To create a bootable disk image, run:
``` ```
cargo bootimage cargo bootimage
@@ -43,10 +39,10 @@ You can run the disk image in [QEMU] through:
[QEMU]: https://www.qemu.org/ [QEMU]: https://www.qemu.org/
``` ```
cargo run cargo xrun
``` ```
[QEMU] and the [`bootimage`] tool need to be installed for this. Of course [QEMU] needs to be installed for this.
You can also write the image to an USB stick for booting it on a real machine. On Linux, the command for this is: You can also write the image to an USB stick for booting it on a real machine. On Linux, the command for this is:

View File

@@ -31,9 +31,8 @@ pub struct FixedSizeBlockAllocator {
impl FixedSizeBlockAllocator { impl FixedSizeBlockAllocator {
/// Creates an empty FixedSizeBlockAllocator. /// Creates an empty FixedSizeBlockAllocator.
pub const fn new() -> Self { pub const fn new() -> Self {
const EMPTY: Option<&'static mut ListNode> = None;
FixedSizeBlockAllocator { FixedSizeBlockAllocator {
list_heads: [EMPTY; BLOCK_SIZES.len()], list_heads: [None; BLOCK_SIZES.len()],
fallback_allocator: linked_list_allocator::Heap::empty(), fallback_allocator: linked_list_allocator::Heap::empty(),
} }
} }

View File

@@ -9,7 +9,7 @@ lazy_static! {
static ref TSS: TaskStateSegment = { static ref TSS: TaskStateSegment = {
let mut tss = TaskStateSegment::new(); let mut tss = TaskStateSegment::new();
tss.interrupt_stack_table[DOUBLE_FAULT_IST_INDEX as usize] = { tss.interrupt_stack_table[DOUBLE_FAULT_IST_INDEX as usize] = {
const STACK_SIZE: usize = 4096 * 5; const STACK_SIZE: usize = 4096;
static mut STACK: [u8; STACK_SIZE] = [0; STACK_SIZE]; static mut STACK: [u8; STACK_SIZE] = [0; STACK_SIZE];
let stack_start = VirtAddr::from_ptr(unsafe { &STACK }); let stack_start = VirtAddr::from_ptr(unsafe { &STACK });
@@ -41,12 +41,12 @@ struct Selectors {
} }
pub fn init() { pub fn init() {
use x86_64::instructions::segmentation::{Segment, CS}; use x86_64::instructions::segmentation::set_cs;
use x86_64::instructions::tables::load_tss; use x86_64::instructions::tables::load_tss;
GDT.0.load(); GDT.0.load();
unsafe { unsafe {
CS::set_reg(GDT.1.code_selector); set_cs(GDT.1.code_selector);
load_tss(GDT.1.tss_selector); load_tss(GDT.1.tss_selector);
} }
} }

View File

@@ -1,8 +1,10 @@
use crate::{gdt, hlt_loop, print, println}; use crate::{gdt, hlt_loop, print, println};
use lazy_static::lazy_static; use lazy_static::lazy_static;
use pic8259::ChainedPics; use pic8259_simple::ChainedPics;
use spin; use spin;
use x86_64::structures::idt::{InterruptDescriptorTable, InterruptStackFrame, PageFaultErrorCode}; use x86_64::structures::idt::{
self, InterruptDescriptorTable, InterruptStackFrame, PageFaultErrorCode,
};
pub const PIC_1_OFFSET: u8 = 32; pub const PIC_1_OFFSET: u8 = 32;
pub const PIC_2_OFFSET: u8 = PIC_1_OFFSET + 8; pub const PIC_2_OFFSET: u8 = PIC_1_OFFSET + 8;
@@ -30,6 +32,8 @@ pub static PICS: spin::Mutex<ChainedPics> =
lazy_static! { lazy_static! {
static ref IDT: InterruptDescriptorTable = { static ref IDT: InterruptDescriptorTable = {
let mut idt = InterruptDescriptorTable::new(); let mut idt = InterruptDescriptorTable::new();
idt::set_general_handler!(&mut idt, default_handler);
idt.breakpoint.set_handler_fn(breakpoint_handler); idt.breakpoint.set_handler_fn(breakpoint_handler);
idt.page_fault.set_handler_fn(page_fault_handler); idt.page_fault.set_handler_fn(page_fault_handler);
unsafe { unsafe {
@@ -37,8 +41,6 @@ lazy_static! {
.set_handler_fn(double_fault_handler) .set_handler_fn(double_fault_handler)
.set_stack_index(gdt::DOUBLE_FAULT_IST_INDEX); .set_stack_index(gdt::DOUBLE_FAULT_IST_INDEX);
} }
idt[InterruptIndex::Timer.as_usize()].set_handler_fn(timer_interrupt_handler);
idt[InterruptIndex::Keyboard.as_usize()].set_handler_fn(keyboard_interrupt_handler);
idt idt
}; };
} }
@@ -47,12 +49,24 @@ pub fn init_idt() {
IDT.load(); IDT.load();
} }
extern "x86-interrupt" fn breakpoint_handler(stack_frame: InterruptStackFrame) { fn default_handler(stack_frame: &mut InterruptStackFrame, index: u8, error_code: Option<u64>) {
if index == 32 {
print!("{} ", index);
} else {
println!("INTERRUPT {}: \n{:#?}", index, stack_frame);
}
unsafe {
PICS.lock().notify_end_of_interrupt(index);
}
}
extern "x86-interrupt" fn breakpoint_handler(stack_frame: &mut InterruptStackFrame) {
println!("EXCEPTION: BREAKPOINT\n{:#?}", stack_frame); println!("EXCEPTION: BREAKPOINT\n{:#?}", stack_frame);
} }
extern "x86-interrupt" fn page_fault_handler( extern "x86-interrupt" fn page_fault_handler(
stack_frame: InterruptStackFrame, stack_frame: &mut InterruptStackFrame,
error_code: PageFaultErrorCode, error_code: PageFaultErrorCode,
) { ) {
use x86_64::registers::control::Cr2; use x86_64::registers::control::Cr2;
@@ -65,13 +79,13 @@ extern "x86-interrupt" fn page_fault_handler(
} }
extern "x86-interrupt" fn double_fault_handler( extern "x86-interrupt" fn double_fault_handler(
stack_frame: InterruptStackFrame, stack_frame: &mut InterruptStackFrame,
_error_code: u64, _error_code: u64,
) -> ! { ) -> ! {
panic!("EXCEPTION: DOUBLE FAULT\n{:#?}", stack_frame); panic!("EXCEPTION: DOUBLE FAULT\n{:#?}", stack_frame);
} }
extern "x86-interrupt" fn timer_interrupt_handler(_stack_frame: InterruptStackFrame) { extern "x86-interrupt" fn timer_interrupt_handler(_stack_frame: &mut InterruptStackFrame) {
print!("."); print!(".");
unsafe { unsafe {
PICS.lock() PICS.lock()
@@ -79,7 +93,7 @@ extern "x86-interrupt" fn timer_interrupt_handler(_stack_frame: InterruptStackFr
} }
} }
extern "x86-interrupt" fn keyboard_interrupt_handler(_stack_frame: InterruptStackFrame) { extern "x86-interrupt" fn keyboard_interrupt_handler(_stack_frame: &mut InterruptStackFrame) {
use x86_64::instructions::port::Port; use x86_64::instructions::port::Port;
let mut port = Port::new(0x60); let mut port = Port::new(0x60);

View File

@@ -2,11 +2,15 @@
#![cfg_attr(test, no_main)] #![cfg_attr(test, no_main)]
#![feature(custom_test_frameworks)] #![feature(custom_test_frameworks)]
#![feature(abi_x86_interrupt)] #![feature(abi_x86_interrupt)]
#![feature(const_mut_refs)] #![feature(alloc_error_handler)]
#![feature(const_fn)]
#![feature(const_in_array_repeat_expressions)]
#![feature(wake_trait)]
#![test_runner(crate::test_runner)] #![test_runner(crate::test_runner)]
#![reexport_test_harness_main = "test_main"] #![reexport_test_harness_main = "test_main"]
extern crate alloc; extern crate alloc;
use core::panic::PanicInfo; use core::panic::PanicInfo;
pub mod allocator; pub mod allocator;
@@ -94,3 +98,8 @@ fn test_kernel_main(_boot_info: &'static BootInfo) -> ! {
fn panic(info: &PanicInfo) -> ! { fn panic(info: &PanicInfo) -> ! {
test_panic_handler(info) test_panic_handler(info)
} }
#[alloc_error_handler]
fn alloc_error_handler(layout: alloc::alloc::Layout) -> ! {
panic!("allocation error: {:?}", layout)
}

View File

@@ -1,6 +1,8 @@
use bootloader::bootinfo::{MemoryMap, MemoryRegionType}; use bootloader::bootinfo::{MemoryMap, MemoryRegionType};
use x86_64::{ use x86_64::{
structures::paging::{FrameAllocator, OffsetPageTable, PageTable, PhysFrame, Size4KiB}, structures::paging::{
FrameAllocator, Mapper, OffsetPageTable, Page, PageTable, PhysFrame, Size4KiB,
},
PhysAddr, VirtAddr, PhysAddr, VirtAddr,
}; };
@@ -33,6 +35,24 @@ unsafe fn active_level_4_table(physical_memory_offset: VirtAddr) -> &'static mut
&mut *page_table_ptr // unsafe &mut *page_table_ptr // unsafe
} }
/// Creates an example mapping for the given page to frame `0xb8000`.
pub fn create_example_mapping(
page: Page,
mapper: &mut OffsetPageTable,
frame_allocator: &mut impl FrameAllocator<Size4KiB>,
) {
use x86_64::structures::paging::PageTableFlags as Flags;
let frame = PhysFrame::containing_address(PhysAddr::new(0xb8000));
let flags = Flags::PRESENT | Flags::WRITABLE;
let map_to_result = unsafe {
// FIXME: this is not safe, we do it only for testing
mapper.map_to(page, frame, flags, frame_allocator)
};
map_to_result.expect("map_to failed").flush();
}
/// A FrameAllocator that always returns `None`. /// A FrameAllocator that always returns `None`.
pub struct EmptyFrameAllocator; pub struct EmptyFrameAllocator;

View File

@@ -62,11 +62,11 @@ impl Executor {
} }
fn sleep_if_idle(&self) { fn sleep_if_idle(&self) {
use x86_64::instructions::interrupts::{self, enable_and_hlt}; use x86_64::instructions::interrupts::{self, enable_interrupts_and_hlt};
interrupts::disable(); interrupts::disable();
if self.task_queue.is_empty() { if self.task_queue.is_empty() {
enable_and_hlt(); enable_interrupts_and_hlt();
} else { } else {
interrupts::enable(); interrupts::enable();
} }

View File

@@ -44,7 +44,7 @@ pub fn init_test_idt() {
} }
extern "x86-interrupt" fn test_double_fault_handler( extern "x86-interrupt" fn test_double_fault_handler(
_stack_frame: InterruptStackFrame, _stack_frame: &mut InterruptStackFrame,
_error_code: u64, _error_code: u64,
) -> ! { ) -> ! {
serial_println!("[ok]"); serial_println!("[ok]");