Skip to content
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

[WIP] Basic Userspace #81

Draft
wants to merge 18 commits into
base: development
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,9 @@ Much thanks to:
- Omarrx024,
- [Mintsuki](https://github.com/mintsuki),
- [Korona](https://github.com/avdgrinten),
- [Qookie](https://gitlab.com/qookei),
- and [Safsom](https://github.com/asfsom);
- the people part of the [rust-osdev](https://github.com/rust-osdev), such as
- the people part of the [rust-osdev](https://github.com/rust-osdev) organisation, such as
- [Isaac Woods](https://github.com/IsaacWoods);
- the [OsDev wiki](http://wiki.osdev.org);
- [Bare Metal Rust](http://www.randomhacks.net/bare-metal-rust/);
Expand Down
7 changes: 3 additions & 4 deletions kernel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,16 @@ bitflags = "^1.0.1"
bit_field = "^0.9.0"
log = "^0.4.3"
static_assertions = "^0.2.5"
acpi = "0.2"

[dependencies.x86_64]
path = "../../x86_64/"
git = "https://github.com/Restioson/x86_64"
branch = "gdt"

[dependencies.multiboot2]
git = "https://github.com/rust-osdev/multiboot2-elf64/"
rev = "9ce9247eee220a0bc2ca480c23c7572132c5ae9c"

[dependencies.acpi]
version = "0.2"

[dependencies.arrayvec]
version = "^0.4.7"
default-features = false
Expand Down
60 changes: 55 additions & 5 deletions kernel/src/gdt.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use core::cell::RefCell;
use core::{ops::Range, cell::RefCell};
use spin::{Once, Mutex};
use x86_64::structures::tss::TaskStateSegment;
use bit_field::BitField;
use crate::serial::PORT_1_ADDR;

pub const DOUBLE_FAULT_IST_INDEX: u16 = 0;
pub const PANICKING_EXCEPTION_IST_INDEX: u16 = 1;
pub const IRQ_IST_INDEX: u16 = 2;

pub static TSS: Once<Mutex<RefCell<TaskStateSegment>>> = Once::new();
pub static TSS: Once<Mutex<Tss>> = Once::new();

use x86_64::structures::gdt::{GlobalDescriptorTable, Descriptor, DescriptorFlags as Flags,
SegmentSelector};
Expand All @@ -20,9 +22,9 @@ lazy_static! {
(Flags::USER_SEGMENT | Flags::PRESENT).bits() | (1 << 41),
));

let tss = unsafe {
gdt.add_entry(Descriptor::tss_segment(&*TSS.wait().unwrap().lock().as_ptr()))
};
let tss = unsafe { gdt.add_entry(
Descriptor::tss_segment(&*TSS.wait().unwrap().lock().tss.as_ptr(), 8193)
)};

let user_cs = gdt.add_entry(Descriptor::UserSegment(
(Flags::USER_SEGMENT | Flags::PRESENT | Flags::EXECUTABLE | Flags::LONG_MODE).bits()
Expand Down Expand Up @@ -52,6 +54,51 @@ struct Selectors {
tss: SegmentSelector,
}

#[repr(C)]
pub struct Tss {
pub tss: RefCell<TaskStateSegment>,
iomap: [u8; 8193],
}

impl Tss {
pub fn new(tss: TaskStateSegment) -> Self {
let mut tss = Tss {
tss: RefCell::new(tss),
iomap: [0xff; 8193],
};

// Absolute values don't matter, only the difference
let tss_addr = tss.tss.as_ptr() as usize;
let iomap_addr = (&tss.iomap) as *const _ as usize;
let iomap_base = (iomap_addr - tss_addr) as u16;

tss.tss.get_mut().iomap_base = iomap_base;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that this approach works. The iomap_addr points to your local stack frame, which is no longer valid after the function returns.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does work, as the iomap base is relative to the TSS start address's. This is why it is iomap_addr - tss_addr. Otherwise yes, it wouldn't work.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah sorry, I confused RefCell and Rc and thought that the tss lived on the heap.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I may have forgot to add the size of the TSS. Not sure if that's needed... In testing it has worked though.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait, I don't, nvm.


tss
}

pub fn set_ports_usable(&mut self, ports: Range<u16>, usable: bool) {
assert!(ports.end / 8 < 8192, "Port 0x{:x} out of bounds", ports.end);

// TODO could be optimised
for port in ports {
let byte_idx = port / 8;
let bit = port % 8;
// For some reason 1 = disabled
self.iomap[byte_idx as usize].set_bit(bit as usize, !usable);
}
}

pub fn is_port_usable(&self, port: u16) -> bool {
assert!(port / 8 < 8192, "Port 0x{:x} out of bounds", port);

let byte_idx = port / 8;
let bit = port % 8;
// For some reason 1 = disabled
!self.iomap[byte_idx as usize].get_bit(bit as usize)
}
}

pub fn init() {
use x86_64::instructions::segmentation::*;
use x86_64::instructions::tables::load_tss;
Expand All @@ -78,5 +125,8 @@ pub fn init() {
trace!("user cs = 0x{:x}", GDT.selectors.user_cs.0);
trace!("user ds = 0x{:x}", GDT.selectors.user_ds.0);


TSS.wait().unwrap().lock().set_ports_usable(PORT_1_ADDR..PORT_1_ADDR + 8, true);
trace!("port enabled: {:?}", TSS.wait().unwrap().lock().is_port_usable(PORT_1_ADDR));
debug!("gdt: initialised");
}
6 changes: 3 additions & 3 deletions kernel/src/memory/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use self::bootstrap_heap::{BootstrapHeap, BOOTSTRAP_HEAP};
use self::paging::{Page, PageSize, PhysicalAddress, VirtualAddress, PAGE_TABLES, EntryFlags,
PageRangeMapping, remap, InvalidateTlb};
use crate::util::round_up_divide;
use crate::gdt;
use crate::gdt::{TSS, Tss};

pub const KERNEL_MAPPING_BEGIN: usize = 0xffffffff80000000;
const IST_STACK_SIZE_PAGES: usize = 3;
Expand Down Expand Up @@ -134,7 +134,7 @@ unsafe fn setup_ist(begin: Page) {
}
}

gdt::TSS.call_once(|| {
TSS.call_once(|| {
let mut tss = TaskStateSegment::new();

for i in 0..7 { // Packed struct; cannot safely borrow fields
Expand All @@ -144,7 +144,7 @@ unsafe fn setup_ist(begin: Page) {
tss.interrupt_stack_table[i] = x86_64::VirtAddr::new(stack_end);
}

Mutex::new(RefCell::new(tss))
Mutex::new(Tss::new(tss))
});
}

Expand Down
2 changes: 1 addition & 1 deletion kernel/src/userspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub unsafe fn jump_usermode() -> ! {
let rsp: u64;
asm!("mov %rsp, $0" : "=r"(rsp));

crate::gdt::TSS.wait().unwrap().lock().get_mut().privilege_stack_table[0] = VirtAddr::new(rsp);
crate::gdt::TSS.wait().unwrap().lock().tss.get_mut().privilege_stack_table[0] = VirtAddr::new(rsp);

asm!("
push 0x2b
Expand Down
5 changes: 3 additions & 2 deletions serial.log
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ Flower kernel boot!
[trace] kernel ds = 0x10
[trace] user cs = 0x2b
[trace] user ds = 0x33
[trace] port enabled: false
[debug] gdt: initialised
[info] interrupts: initializing
[debug] interrupts: initialized idt
Expand All @@ -56,9 +57,9 @@ Flower kernel boot!
[info] interrupts: ready
Panicked at "cpuex: general protection fault 0x0
InterruptStackFrame {
instruction_pointer: VirtAddr(0xffffffff8010e31a),
instruction_pointer: VirtAddr(0xffffffff8010156a),
code_segment: 43,
cpu_flags: 0x202,
stack_pointer: VirtAddr(0xffffffff80374e38),
stack_pointer: VirtAddr(0xffffffff8036fe68),
stack_segment: 51,
}", src/interrupts/exceptions.rs:73