5 Commits

Author SHA1 Message Date
alnyan 5128286220 Merge branch 'feat/thread' into feat/smp 2021-11-30 11:07:09 +02:00
alnyan b61cb052ec feat: rerun build if linker script changes 2021-11-10 18:37:09 +02:00
alnyan 3e6b7a71e6 feat: send IPIs using GIC 2021-11-10 18:36:29 +02:00
alnyan f242948f82 xxx: doesn't really work 2021-11-10 18:35:32 +02:00
alnyan 0ee19cde96 merge: smp 2021-11-10 18:17:00 +02:00
143 changed files with 2919 additions and 6198 deletions
Generated
+2 -12
View File
@@ -22,9 +22,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "cortex-a"
version = "7.0.1"
version = "6.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5bd95fd055d118f77d4e4d527201b6ceccd13586b19b4dac1270f7081fef0f98"
checksum = "509fc35485a2b4ddbacabe0bf2212cdfff88da93658608e5cc651afcb75b7733"
dependencies = [
"tock-registers",
]
@@ -93,11 +93,9 @@ dependencies = [
"cfg-if",
"cortex-a",
"fdt-rs",
"fs-macros",
"kernel-macros",
"libsys",
"memfs",
"multiboot2",
"tock-registers",
"vfs",
]
@@ -163,14 +161,6 @@ dependencies = [
"autocfg",
]
[[package]]
name = "multiboot2"
version = "0.12.2"
source = "git+https://github.com/alnyan/multiboot2?branch=expose-extra-traits-for-iters#7e86b55fa5ab82e54978021f8022068a1591166b"
dependencies = [
"bitflags",
]
[[package]]
name = "num-derive"
version = "0.3.3"
+10 -32
View File
@@ -17,28 +17,24 @@ CARGO_COMMON_OPTS=
ifeq ($(PROFILE),release)
CARGO_COMMON_OPTS+=--release
endif
ifeq ($(VERBOSE),1)
CARGO_COMMON_OPTS+=--features verbose
endif
CARGO_BUILD_OPTS=$(CARGO_COMMON_OPTS) \
--target=../etc/$(ARCH)-$(MACH).json
ifeq ($(VERBOSE),1)
CARGO_BUILD_OPTS+=--features verbose
endif
ifneq ($(MACH),)
CARGO_BUILD_OPTS+=--features mach_$(MACH)
endif
QEMU_OPTS=-s
ifeq ($(ARCH),x86_64)
MACH=none
QEMU_OPTS+=-cdrom $(O)/image.iso \
-M q35 \
-m 512 \
-serial mon:stdio \
-net none
$(error TODO)
else
ifeq ($(MACH),qemu)
QEMU_OPTS+=-kernel $(O)/kernel.bin \
-initrd $(O)/initrd.img \
-smp cpus=4 \
-M virt,virtualization=on \
-cpu cortex-a72 \
-m 512 \
@@ -50,7 +46,6 @@ endif
ifeq ($(MACH),rpi3)
QEMU_OPTS+=-kernel $(O)/kernel.bin \
-initrd $(O)/initrd.img \
-dtb etc/bcm2837-rpi-3-b-plus.dtb \
-M raspi3b \
-serial mon:stdio \
-display none \
@@ -68,16 +63,13 @@ endif
ifeq ($(QEMU_PAUSE),1)
QEMU_OPTS+=-S
endif
ifeq ($(QEMU_KVM),1)
QEMU_OPTS+=-enable-kvm -cpu host
endif
.PHONY: address error etc kernel src
all: image
all: kernel initrd
kernel:
cd kernel && cargo build $(CARGO_BUILD_OPTS)
cd kernel && ARCH=$(ARCH) MACH=$(MACH) cargo build $(CARGO_BUILD_OPTS)
ifeq ($(ARCH),aarch64)
$(LLVM_BASE)/llvm-strip -o $(O)/kernel.strip $(O)/kernel
$(LLVM_BASE)/llvm-size $(O)/kernel.strip
@@ -96,33 +88,19 @@ ifeq ($(MACH),orangepi3)
$(O)/uImage
endif
image: kernel initrd
ifeq ($(ARCH),x86_64)
mkdir -p $(O)/image/boot/grub
cp etc/x86_64-none.grub $(O)/image/boot/grub/grub.cfg
cp $(O)/kernel $(O)/image/boot/kernel
cp $(O)/initrd.img $(O)/image/boot/initrd.img
grub-mkrescue -o $(O)/image.iso $(O)/image
endif
initrd:
cd user && cargo build \
--target=../etc/$(ARCH)-osdev5.json \
-Z build-std=core,alloc,compiler_builtins \
$(CARGO_COMMON_OPTS)
mkdir -p $(O)/rootfs/bin $(O)/rootfs/sbin $(O)/rootfs/dev $(O)/rootfs/etc $(O)/rootfs/sys
cp etc/initrd/passwd $(O)/rootfs/etc
cp etc/initrd/shadow $(O)/rootfs/etc
touch $(O)/rootfs/dev/.do_not_remove
touch $(O)/rootfs/sys/.do_not_remove
mkdir -p $(O)/rootfs/bin $(O)/rootfs/sbin $(O)/rootfs/dev
touch $(O)/rootfs/dev/.do_no_remove
cp target/$(ARCH)-osdev5/$(PROFILE)/init $(O)/rootfs/init
cp target/$(ARCH)-osdev5/$(PROFILE)/shell $(O)/rootfs/bin
cp target/$(ARCH)-osdev5/$(PROFILE)/fuzzy $(O)/rootfs/bin
cp target/$(ARCH)-osdev5/$(PROFILE)/ls $(O)/rootfs/bin
cp target/$(ARCH)-osdev5/$(PROFILE)/cat $(O)/rootfs/bin
cp target/$(ARCH)-osdev5/$(PROFILE)/hexd $(O)/rootfs/bin
cp target/$(ARCH)-osdev5/$(PROFILE)/mkdir $(O)/rootfs/bin
cp target/$(ARCH)-osdev5/$(PROFILE)/fuzzy $(O)/rootfs/bin
cp target/$(ARCH)-osdev5/$(PROFILE)/login $(O)/rootfs/sbin
cd $(O)/rootfs && tar cf ../initrd.img `find -type f -printf "%P\n"`
ifeq ($(MACH),orangepi3)
Binary file not shown.
Binary file not shown.
-2
View File
@@ -1,2 +0,0 @@
root:0:0:root:/:/bin/shell
alnyan:1000:1000:alnyan:/:/bin/shell
-2
View File
@@ -1,2 +0,0 @@
root:toor
alnyan:
+1 -2
View File
@@ -1,4 +1,3 @@
menuentry "OS" {
multiboot2 /boot/kernel
module2 /boot/initrd.img
multiboot2 /boot/kernel.elf
}
+1 -5
View File
@@ -6,8 +6,7 @@ SECTIONS {
. = 0x400000 + KERNEL_OFFSET;
.text : AT(. - KERNEL_OFFSET) {
KEEP(*(.text._multiboot))
*(.text._entry)
KEEP(*(.multiboot))
*(.text*)
}
@@ -22,8 +21,5 @@ SECTIONS {
.bss : AT(. - KERNEL_OFFSET) {
*(COMMON)
*(.bss*)
. = ALIGN(4K);
}
PROVIDE(__kernel_end = .);
}
-21
View File
@@ -1,21 +0,0 @@
{
"arch": "x86_64",
"cpu": "x86-64",
"data-layout": "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128",
"disable-redzone": true,
"executables": true,
"panic-strategy": "abort",
"linker": "rust-lld",
"linker-flavor": "ld.lld",
"llvm-target": "x86_64-unknown-linux-gnu",
"max-atomic-width": 64,
"target-pointer-width": "64",
"os": "none",
"pre-link-args": {
"ld.lld": [ "-Tetc/x86_64-osdev5.ld" ]
}
}
-32
View File
@@ -1,32 +0,0 @@
ENTRY(_start);
PHDRS {
text PT_LOAD ;
rodata PT_LOAD ;
data PT_LOAD ;
}
SECTIONS {
. = 0x400000;
.text : {
*(.text._start)
*(.text*)
*(.eh_frame*)
} :text
. = ALIGN(0x1000);
.rodata : {
*(.rodata*)
} :rodata
. = ALIGN(0x1000);
.data : {
*(.data*)
} :data
.bss : {
*(COMMON)
*(.bss*)
} :data
}
+2 -2
View File
@@ -1,8 +1,8 @@
use crate::Bpb;
use libsys::{
error::Errno,
stat::{Stat, OpenFlags},
ioctl::IoctlCmd,
stat::{OpenFlags, Stat},
error::Errno
};
use vfs::{VnodeImpl, VnodeKind, VnodeRef};
+4 -1
View File
@@ -12,7 +12,10 @@ extern crate alloc;
use alloc::{boxed::Box, rc::Rc};
use core::any::Any;
use core::cell::{Ref, RefCell};
use libsys::{error::Errno, mem::read_le32};
use libsys::{
mem::read_le32,
error::Errno,
};
use vfs::{BlockDevice, Filesystem, Vnode, VnodeKind, VnodeRef};
pub mod dir;
+161 -161
View File
@@ -1,162 +1,162 @@
// extern crate proc_macro;
// extern crate syn;
// #[macro_use]
// extern crate quote;
//
// use proc_macro::TokenStream;
// use quote::ToTokens;
// use std::collections::HashSet;
// use syn::{parse_macro_input, ImplItem, ItemImpl, Ident};
extern crate proc_macro;
extern crate syn;
#[macro_use]
extern crate quote;
//fn impl_inode_fn<T: ToTokens>(name: &str, behavior: T) -> ImplItem {
// // TODO somehow know if current crate is vfs or not?
// ImplItem::Verbatim(match name {
// "create" => quote! {
// fn create(&mut self, _at: VnodeRef, _name: &str, kind: VnodeKind) ->
// Result<VnodeRef, libsys::error::Errno>
// {
// #behavior
// }
// },
// "remove" => quote! {
// fn remove(&mut self, _at: VnodeRef, _name: &str) -> Result<(), libsys::error::Errno> {
// #behavior
// }
// },
// "lookup" => quote! {
// fn lookup(&mut self, _at: VnodeRef, _name: &str) ->
// Result<VnodeRef, libsys::error::Errno>
// {
// #behavior
// }
// },
// "stat" => quote! {
// fn stat(&mut self, _at: VnodeRef) ->
// Result<libsys::stat::Stat, libsys::error::Errno>
// {
// #behavior
// }
// },
// "truncate" => quote! {
// fn truncate(&mut self, _node: VnodeRef, _size: usize) ->
// Result<(), libsys::error::Errno>
// {
// #behavior
// }
// },
// "size" => quote! {
// fn size(&mut self, _node: VnodeRef) -> Result<usize, libsys::error::Errno> {
// #behavior
// }
// },
// "read" => quote! {
// fn read(&mut self, _node: VnodeRef, _pos: usize, _data: &mut [u8]) ->
// Result<usize, libsys::error::Errno>
// {
// #behavior
// }
// },
// "write" => quote! {
// fn write(&mut self, _node: VnodeRef, _pos: usize, _data: &[u8]) ->
// Result<usize, libsys::error::Errno>
// {
// #behavior
// }
// },
// "open" => quote! {
// fn open(&mut self, _node: VnodeRef, _flags: libsys::stat::OpenFlags) ->
// Result<usize, libsys::error::Errno>
// {
// #behavior
// }
// },
// "close" => quote! {
// fn close(&mut self, _node: VnodeRef) -> Result<(), libsys::error::Errno> {
// #behavior
// }
// },
// "ioctl" => quote! {
// fn ioctl(
// &mut self,
// _node: VnodeRef,
// _cmd: libsys::ioctl::IoctlCmd,
// _ptr: usize,
// _len: usize) ->
// Result<usize, libsys::error::Errno>
// {
// #behavior
// }
// },
// "is_ready" => quote! {
// fn is_ready(&mut self, _node: VnodeRef, _write: bool) ->
// Result<bool, libsys::error::Errno>
// {
// #behavior
// }
// },
// "readdir" => quote! {
// fn readdir(
// &mut self,
// _node: VnodeRef,
// _pos: usize,
// _entries: &mut [libsys::stat::DirectoryEntry]
// ) ->
// Result<usize, libsys::error::Errno>
// {
// #behavior
// }
// },
// _ => panic!("TODO implement {:?}", name),
// })
//}
//
//#[proc_macro_attribute]
//pub fn auto_inode(attr: TokenStream, input: TokenStream) -> TokenStream {
// let mut impl_item = parse_macro_input!(input as ItemImpl);
// let mut missing = HashSet::<String>::new();
// let behavior = if attr.is_empty() {
// "unimplemented".to_string()
// } else {
// parse_macro_input!(attr as Ident).to_string()
// };
// let behavior = match behavior.as_str() {
// "unimplemented" => quote! { unimplemented!() },
// "panic" => quote! { panic!() },
// "error" => quote! { Err(libsys::error::Errno::NotImplemented) },
// _ => panic!("Unknown #[auto_inode] behavior: {:?}", behavior)
// };
//
// missing.insert("create".to_string());
// missing.insert("remove".to_string());
// missing.insert("lookup".to_string());
// missing.insert("open".to_string());
// missing.insert("close".to_string());
// missing.insert("truncate".to_string());
// missing.insert("read".to_string());
// missing.insert("write".to_string());
// missing.insert("stat".to_string());
// missing.insert("size".to_string());
// missing.insert("ioctl".to_string());
// missing.insert("is_ready".to_string());
// missing.insert("readdir".to_string());
//
// for item in &impl_item.items {
// match item {
// ImplItem::Method(method) => {
// let name = &method.sig.ident.to_string();
// if missing.contains(name) {
// missing.remove(name);
// }
// }
// _ => panic!("Unexpected impl item"),
// }
// }
//
// for item in &missing {
// impl_item
// .items
// .push(impl_inode_fn(item, behavior.clone()));
// }
//
// impl_item.to_token_stream().into()
//}
use proc_macro::TokenStream;
use quote::ToTokens;
use std::collections::HashSet;
use syn::{parse_macro_input, ImplItem, ItemImpl, Ident};
fn impl_inode_fn<T: ToTokens>(name: &str, behavior: T) -> ImplItem {
// TODO somehow know if current crate is vfs or not?
ImplItem::Verbatim(match name {
"create" => quote! {
fn create(&mut self, _at: VnodeRef, _name: &str, kind: VnodeKind) ->
Result<VnodeRef, libsys::error::Errno>
{
#behavior
}
},
"remove" => quote! {
fn remove(&mut self, _at: VnodeRef, _name: &str) -> Result<(), libsys::error::Errno> {
#behavior
}
},
"lookup" => quote! {
fn lookup(&mut self, _at: VnodeRef, _name: &str) ->
Result<VnodeRef, libsys::error::Errno>
{
#behavior
}
},
"stat" => quote! {
fn stat(&mut self, _at: VnodeRef) ->
Result<libsys::stat::Stat, libsys::error::Errno>
{
#behavior
}
},
"truncate" => quote! {
fn truncate(&mut self, _node: VnodeRef, _size: usize) ->
Result<(), libsys::error::Errno>
{
#behavior
}
},
"size" => quote! {
fn size(&mut self, _node: VnodeRef) -> Result<usize, libsys::error::Errno> {
#behavior
}
},
"read" => quote! {
fn read(&mut self, _node: VnodeRef, _pos: usize, _data: &mut [u8]) ->
Result<usize, libsys::error::Errno>
{
#behavior
}
},
"write" => quote! {
fn write(&mut self, _node: VnodeRef, _pos: usize, _data: &[u8]) ->
Result<usize, libsys::error::Errno>
{
#behavior
}
},
"open" => quote! {
fn open(&mut self, _node: VnodeRef, _flags: libsys::stat::OpenFlags) ->
Result<usize, libsys::error::Errno>
{
#behavior
}
},
"close" => quote! {
fn close(&mut self, _node: VnodeRef) -> Result<(), libsys::error::Errno> {
#behavior
}
},
"ioctl" => quote! {
fn ioctl(
&mut self,
_node: VnodeRef,
_cmd: libsys::ioctl::IoctlCmd,
_ptr: usize,
_len: usize) ->
Result<usize, libsys::error::Errno>
{
#behavior
}
},
"is_ready" => quote! {
fn is_ready(&mut self, _node: VnodeRef, _write: bool) ->
Result<bool, libsys::error::Errno>
{
#behavior
}
},
"readdir" => quote! {
fn readdir(
&mut self,
_node: VnodeRef,
_pos: usize,
_entries: &mut [libsys::stat::DirectoryEntry]
) ->
Result<usize, libsys::error::Errno>
{
#behavior
}
},
_ => panic!("TODO implement {:?}", name),
})
}
#[proc_macro_attribute]
pub fn auto_inode(attr: TokenStream, input: TokenStream) -> TokenStream {
let mut impl_item = parse_macro_input!(input as ItemImpl);
let mut missing = HashSet::<String>::new();
let behavior = if attr.is_empty() {
"unimplemented".to_string()
} else {
parse_macro_input!(attr as Ident).to_string()
};
let behavior = match behavior.as_str() {
"unimplemented" => quote! { unimplemented!() },
"panic" => quote! { panic!() },
"error" => quote! { Err(libsys::error::Errno::NotImplemented) },
_ => panic!("Unknown #[auto_inode] behavior: {:?}", behavior)
};
missing.insert("create".to_string());
missing.insert("remove".to_string());
missing.insert("lookup".to_string());
missing.insert("open".to_string());
missing.insert("close".to_string());
missing.insert("truncate".to_string());
missing.insert("read".to_string());
missing.insert("write".to_string());
missing.insert("stat".to_string());
missing.insert("size".to_string());
missing.insert("ioctl".to_string());
missing.insert("is_ready".to_string());
missing.insert("readdir".to_string());
for item in &impl_item.items {
match item {
ImplItem::Method(method) => {
let name = &method.sig.ident.to_string();
if missing.contains(name) {
missing.remove(name);
}
}
_ => panic!("Unexpected impl item"),
}
}
for item in &missing {
impl_item
.items
.push(impl_inode_fn(item, behavior.clone()));
}
impl_item.to_token_stream().into()
}
-3
View File
@@ -11,9 +11,6 @@ pub struct BlockRef<'a, A: BlockAllocator + Copy> {
alloc: MaybeUninit<A>,
}
/// # Safety
///
/// This trait is unsafe to implement due to its direct memory management
pub unsafe trait BlockAllocator {
fn alloc(&self) -> *mut u8;
/// # Safety
+12 -67
View File
@@ -1,41 +1,27 @@
use crate::{BlockAllocator, Bvec, FileInode};
use alloc::boxed::Box;
use core::cell::RefCell;
use libsys::{
error::Errno,
ioctl::IoctlCmd,
stat::{DirectoryEntry, OpenFlags, Stat},
};
use vfs::{Vnode, VnodeCommon, VnodeCreateKind, VnodeData, VnodeDirectory, VnodeRef};
use libsys::{error::Errno, stat::Stat};
use vfs::{Vnode, VnodeImpl, VnodeKind, VnodeRef};
pub struct DirInode<A: BlockAllocator + Copy + 'static> {
alloc: A,
}
impl<A: BlockAllocator + Copy + 'static> VnodeDirectory for DirInode<A> {
#[auto_inode]
impl<A: BlockAllocator + Copy + 'static> VnodeImpl for DirInode<A> {
fn create(
&mut self,
_parent: VnodeRef,
name: &str,
kind: VnodeCreateKind,
kind: VnodeKind,
) -> Result<VnodeRef, Errno> {
let data = match kind {
VnodeCreateKind::Directory => {
VnodeData::Directory(RefCell::new(Some(Box::new(DirInode { alloc: self.alloc }))))
}
VnodeCreateKind::File => VnodeData::File(RefCell::new(Some(Box::new(FileInode::new(
Bvec::new(self.alloc),
))))),
};
Ok(Vnode::new(
name,
data,
Vnode::SEEKABLE | Vnode::CACHE_READDIR,
))
// match kind {
// _ => todo!(),
// }
// Ok(vnode)
let vnode = Vnode::new(name, kind, Vnode::SEEKABLE | Vnode::CACHE_READDIR);
match kind {
VnodeKind::Directory => vnode.set_data(Box::new(DirInode { alloc: self.alloc })),
VnodeKind::Regular => vnode.set_data(Box::new(FileInode::new(Bvec::new(self.alloc)))),
_ => todo!(),
}
Ok(vnode)
}
fn lookup(&mut self, _parent: VnodeRef, _name: &str) -> Result<VnodeRef, Errno> {
@@ -46,18 +32,6 @@ impl<A: BlockAllocator + Copy + 'static> VnodeDirectory for DirInode<A> {
Ok(())
}
/// Read directory entries into target buffer
fn readdir(
&mut self,
_node: VnodeRef,
_pos: usize,
_data: &mut [DirectoryEntry],
) -> Result<usize, Errno> {
todo!()
}
}
impl<A: BlockAllocator + Copy + 'static> VnodeCommon for DirInode<A> {
fn stat(&mut self, node: VnodeRef) -> Result<Stat, Errno> {
let props = node.props();
Ok(Stat {
@@ -66,35 +40,6 @@ impl<A: BlockAllocator + Copy + 'static> VnodeCommon for DirInode<A> {
mode: props.mode,
})
}
/// Performs filetype-specific request
fn ioctl(
&mut self,
_node: VnodeRef,
_cmd: IoctlCmd,
_ptr: usize,
_len: usize,
) -> Result<usize, Errno> {
todo!()
}
/// Reports the size of this filesystem object in bytes
fn size(&mut self, _node: VnodeRef) -> Result<usize, Errno> {
todo!()
}
/// Returns `true` if node is ready for an operation
fn ready(&mut self, _node: VnodeRef, _write: bool) -> Result<bool, Errno> {
todo!()
}
fn open(&mut self, _node: VnodeRef, _flags: OpenFlags) -> Result<usize, Errno> {
Ok(0)
}
fn close(&mut self, _node: VnodeRef) -> Result<(), Errno> {
Ok(())
}
}
impl<A: BlockAllocator + Copy + 'static> DirInode<A> {
+16 -34
View File
@@ -1,17 +1,16 @@
use crate::{BlockAllocator, Bvec};
use libsys::{
error::Errno,
ioctl::IoctlCmd,
stat::{OpenFlags, Stat},
};
use vfs::{VnodeCommon, VnodeFile, VnodeRef};
use vfs::{VnodeImpl, VnodeKind, VnodeRef};
pub struct FileInode<'a, A: BlockAllocator + Copy + 'static> {
data: Bvec<'a, A>,
}
// #[auto_inode]
impl<'a, A: BlockAllocator + Copy + 'static> VnodeCommon for FileInode<'a, A> {
#[auto_inode]
impl<'a, A: BlockAllocator + Copy + 'static> VnodeImpl for FileInode<'a, A> {
fn open(&mut self, _node: VnodeRef, _mode: OpenFlags) -> Result<usize, Errno> {
Ok(0)
}
@@ -19,37 +18,7 @@ impl<'a, A: BlockAllocator + Copy + 'static> VnodeCommon for FileInode<'a, A> {
fn close(&mut self, _node: VnodeRef) -> Result<(), Errno> {
Ok(())
}
fn size(&mut self, _node: VnodeRef) -> Result<usize, Errno> {
Ok(self.data.size())
}
fn stat(&mut self, node: VnodeRef) -> Result<Stat, Errno> {
let props = node.props();
Ok(Stat {
size: self.data.size() as u64,
blksize: 4096,
mode: props.mode,
})
}
/// Performs filetype-specific request
fn ioctl(
&mut self,
_node: VnodeRef,
_cmd: IoctlCmd,
_ptr: usize,
_len: usize,
) -> Result<usize, Errno> {
todo!()
}
/// Returns `true` if node is ready for an operation
fn ready(&mut self, _node: VnodeRef, _write: bool) -> Result<bool, Errno> {
todo!()
}
}
impl<'a, A: BlockAllocator + Copy + 'static> VnodeFile for FileInode<'a, A> {
fn read(&mut self, _node: VnodeRef, pos: usize, data: &mut [u8]) -> Result<usize, Errno> {
self.data.read(pos, data)
}
@@ -61,6 +30,19 @@ impl<'a, A: BlockAllocator + Copy + 'static> VnodeFile for FileInode<'a, A> {
fn truncate(&mut self, _node: VnodeRef, size: usize) -> Result<(), Errno> {
self.data.resize((size + 4095) / 4096)
}
fn size(&mut self, _node: VnodeRef) -> Result<usize, Errno> {
Ok(self.data.size())
}
fn stat(&mut self, node: VnodeRef) -> Result<Stat, Errno> {
let props = node.props();
Ok(Stat {
size: self.data.size() as u64,
blksize: 4096,
mode: props.mode
})
}
}
impl<'a, A: BlockAllocator + Copy + 'static> FileInode<'a, A> {
+37 -32
View File
@@ -1,4 +1,9 @@
#![feature(const_fn_trait_bound, const_mut_refs, maybe_uninit_uninit_array)]
#![feature(
const_fn_trait_bound,
const_mut_refs,
maybe_uninit_extra,
maybe_uninit_uninit_array
)]
#![no_std]
extern crate alloc;
@@ -6,6 +11,9 @@ extern crate alloc;
#[macro_use]
extern crate std;
#[macro_use]
extern crate fs_macros;
use alloc::{boxed::Box, rc::Rc};
use core::any::Any;
use core::cell::{Ref, RefCell};
@@ -14,14 +22,14 @@ use libsys::{
path::{path_component_left, path_component_right},
stat::FileMode,
};
use vfs::{BlockDevice, Filesystem, Vnode, VnodeCreateKind, VnodeData, VnodeRef};
use vfs::{BlockDevice, Filesystem, Vnode, VnodeKind, VnodeRef};
mod block;
pub use block::{BlockAllocator, BlockRef};
mod bvec;
use bvec::Bvec;
mod tar;
use tar::{Tar, TarIterator};
use tar::{TarIterator, Tar};
mod file;
use file::FileInode;
mod dir;
@@ -60,16 +68,16 @@ impl<A: BlockAllocator + Copy + 'static> Ramfs<A> {
}
fn create_node_initial(self: Rc<Self>, name: &str, tar: &Tar) -> VnodeRef {
let kind = tar.node_create_kind();
let data = match kind {
VnodeCreateKind::Directory => {
VnodeData::Directory(RefCell::new(Some(Box::new(DirInode::new(self.alloc)))))
}
VnodeCreateKind::File => VnodeData::File(RefCell::new(None)),
};
let node = Vnode::new(name, data, Vnode::SEEKABLE | Vnode::CACHE_READDIR);
let kind = tar.node_kind();
let node = Vnode::new(name, kind, Vnode::SEEKABLE | Vnode::CACHE_READDIR);
node.props_mut().mode = tar.mode();
node.set_fs(self);
node.set_fs(self.clone());
match kind {
VnodeKind::Directory => node.set_data(Box::new(DirInode::new(self.alloc))),
VnodeKind::Regular => {}
VnodeKind::Char => todo!(),
VnodeKind::Block => todo!(),
};
node
}
@@ -93,8 +101,7 @@ impl<A: BlockAllocator + Copy + 'static> Ramfs<A> {
return Err(Errno::DoesNotExist);
}
// TODO file modes
at.create(element, FileMode::default_dir(), VnodeCreateKind::Directory)?
// todo!();
at.create(element, FileMode::default_dir(), VnodeKind::Directory)?
}
};
@@ -106,10 +113,9 @@ impl<A: BlockAllocator + Copy + 'static> Ramfs<A> {
}
unsafe fn load_tar(self: Rc<Self>, base: *const u8, size: usize) -> Result<VnodeRef, Errno> {
let root_data =
VnodeData::Directory(RefCell::new(Some(Box::new(DirInode::new(self.alloc)))));
let root = Vnode::new("", root_data, Vnode::SEEKABLE | Vnode::CACHE_READDIR);
let root = Vnode::new("", VnodeKind::Directory, Vnode::SEEKABLE | Vnode::CACHE_READDIR);
root.set_fs(self.clone());
root.set_data(Box::new(DirInode::new(self.alloc)));
root.props_mut().mode = FileMode::default_dir();
// 1. Create all the paths in TAR
@@ -117,7 +123,10 @@ impl<A: BlockAllocator + Copy + 'static> Ramfs<A> {
let (dirname, basename) = path_component_right(block.path()?);
let parent = self.clone().make_path(root.clone(), dirname, true)?;
let node = self.clone().create_node_initial(basename, block);
let node = self
.clone()
.create_node_initial(basename, block);
assert_eq!(node.kind(), block.node_kind());
parent.attach(node);
}
@@ -126,17 +135,16 @@ impl<A: BlockAllocator + Copy + 'static> Ramfs<A> {
if block.is_file() {
// Will not create any dirs
let node = self.clone().make_path(root.clone(), block.path()?, false)?;
assert_eq!(node.kind(), block.node_kind());
#[cfg(feature = "cow")]
{
let data = block.data();
node.as_file()
.unwrap()
.replace(Some(Box::new(FileInode::new(Bvec::new_copy_on_write(
self.alloc,
data.as_ptr(),
data.len(),
)))));
node.set_data(Box::new(FileInode::new(Bvec::new_copy_on_write(
self.alloc,
data.as_ptr(),
data.len(),
))));
}
#[cfg(not(feature = "cow"))]
{
@@ -159,10 +167,7 @@ impl<A: BlockAllocator + Copy + 'static> Ramfs<A> {
mod tests {
use super::*;
use alloc::boxed::Box;
use libsys::{
stat::{GroupId, OpenFlags, UserId},
traits::Read,
};
use libcommon::Read;
use vfs::Ioctx;
#[test]
@@ -184,15 +189,15 @@ mod tests {
let fs = unsafe { Ramfs::open(data.as_ptr(), data.bytes().len(), A {}).unwrap() };
let root = fs.root().unwrap();
let ioctx = Ioctx::new(root.clone(), UserId::root(), GroupId::root());
let ioctx = Ioctx::new(root.clone());
assert!(Rc::ptr_eq(&ioctx.find(None, "/", true).unwrap(), &root));
let node = ioctx.find(None, "/test1.txt", true).unwrap();
let mut file = node.open(OpenFlags::O_RDONLY).unwrap();
let mut file = node.open().unwrap();
let mut buf = [0u8; 1024];
assert_eq!(file.borrow_mut().read(&mut buf).unwrap(), 20);
assert_eq!(file.read(&mut buf).unwrap(), 20);
let s = core::str::from_utf8(&buf[..20]).unwrap();
assert_eq!(s, "This is a test file\n");
}
+8 -7
View File
@@ -1,5 +1,5 @@
use libsys::{error::Errno, stat::FileMode};
use vfs::VnodeCreateKind;
use vfs::VnodeKind;
#[repr(packed)]
#[allow(dead_code)]
@@ -73,18 +73,19 @@ impl Tar {
core::str::from_utf8(&self.name[..zero_index]).map_err(|_| Errno::InvalidArgument)
}
pub fn node_create_kind(&self) -> VnodeCreateKind {
pub fn node_kind(&self) -> VnodeKind {
match self.type_ {
0 | b'0' => VnodeCreateKind::File,
b'5' => VnodeCreateKind::Directory,
0 | b'0' => VnodeKind::Regular,
b'5' => VnodeKind::Directory,
p => panic!("Unrecognized tar entry type: '{}'", p as char),
}
}
pub fn mode(&self) -> FileMode {
let t = match self.node_create_kind() {
VnodeCreateKind::File => FileMode::S_IFREG,
VnodeCreateKind::Directory => FileMode::S_IFDIR,
let t = match self.node_kind() {
VnodeKind::Regular => FileMode::S_IFREG,
VnodeKind::Directory => FileMode::S_IFDIR,
_ => todo!()
};
FileMode::from_bits(from_octal(&self.mode) as u32).unwrap() | t
}
+49 -1
View File
@@ -1,4 +1,5 @@
use libsys::{error::Errno, ioctl::IoctlCmd};
use crate::{VnodeImpl, VnodeKind, VnodeRef};
use libsys::{error::Errno, ioctl::IoctlCmd, stat::OpenFlags};
/// Generic character device trait
pub trait CharDevice {
@@ -21,3 +22,50 @@ pub trait CharDevice {
/// Returns `true` if the device is ready for an operation
fn is_ready(&self, write: bool) -> Result<bool, Errno>;
}
/// Wrapper struct to attach [VnodeImpl] implementation
/// to [CharDevice]s
pub struct CharDeviceWrapper {
device: &'static dyn CharDevice,
}
#[auto_inode(error)]
impl VnodeImpl for CharDeviceWrapper {
fn open(&mut self, _node: VnodeRef, _opts: OpenFlags) -> Result<usize, Errno> {
Ok(0)
}
fn close(&mut self, _node: VnodeRef) -> Result<(), Errno> {
Ok(())
}
fn read(&mut self, _node: VnodeRef, _pos: usize, data: &mut [u8]) -> Result<usize, Errno> {
self.device.read(true, data)
}
fn write(&mut self, _node: VnodeRef, _pos: usize, data: &[u8]) -> Result<usize, Errno> {
self.device.write(true, data)
}
fn is_ready(&mut self, _node: VnodeRef, write: bool) -> Result<bool, Errno> {
self.device.is_ready(write)
}
fn ioctl(
&mut self,
_node: VnodeRef,
cmd: IoctlCmd,
ptr: usize,
len: usize,
) -> Result<usize, Errno> {
self.device.ioctl(cmd, ptr, len)
}
}
impl CharDeviceWrapper {
/// Creates a wrapper for static [CharDevice] trait object to
/// auto-implement [VnodeImpl] trait for the device
pub const fn new(device: &'static dyn CharDevice) -> Self {
Self { device }
}
}
+29 -58
View File
@@ -1,8 +1,7 @@
use crate::{Vnode, VnodeRef};
use crate::{VnodeKind, VnodeRef, Vnode};
use alloc::rc::Rc;
use core::cell::RefCell;
use core::cmp::min;
use core::str::FromStr;
use libsys::{
error::Errno,
stat::DirectoryEntry,
@@ -39,10 +38,9 @@ impl Read for File {
match &mut self.inner {
FileInner::Normal(inner) => {
let count = inner.vnode.read(inner.pos, data)?;
// TODO
// if inner.vnode.kind() != VnodeKind::Char {
inner.pos += count;
// }
if inner.vnode.kind() != VnodeKind::Char {
inner.pos += count;
}
Ok(count)
}
_ => unimplemented!(),
@@ -59,10 +57,9 @@ impl Write for File {
match &mut self.inner {
FileInner::Normal(inner) => {
let count = inner.vnode.write(inner.pos, data)?;
// TODO
// if inner.vnode.kind() != VnodeKind::Char {
inner.pos += count;
// }
if inner.vnode.kind() != VnodeKind::Char {
inner.pos += count;
}
Ok(count)
}
_ => unimplemented!(),
@@ -129,17 +126,14 @@ impl File {
}
/// Returns `true` if the file is ready for an operation
pub fn ready(&self, write: bool) -> Result<bool, Errno> {
pub fn is_ready(&self, write: bool) -> Result<bool, Errno> {
match &self.inner {
FileInner::Normal(inner) => inner.vnode.ready(write),
FileInner::Normal(inner) => inner.vnode.is_ready(write),
_ => todo!(),
}
}
fn cache_readdir(
inner: &mut NormalFile,
entries: &mut [DirectoryEntry],
) -> Result<usize, Errno> {
fn cache_readdir(inner: &mut NormalFile, entries: &mut [DirectoryEntry]) -> Result<usize, Errno> {
let mut count = entries.len();
let mut offset = 0usize;
@@ -148,7 +142,7 @@ impl File {
return Ok(offset);
}
entries[offset] = DirectoryEntry::from_str(".").unwrap();
entries[offset] = DirectoryEntry::from_str(".");
inner.pos = Self::POS_CACHE_DOT_DOT;
offset += 1;
@@ -160,7 +154,7 @@ impl File {
return Ok(offset);
}
entries[offset] = DirectoryEntry::from_str("..").unwrap();
entries[offset] = DirectoryEntry::from_str("..");
inner.pos = 0;
offset += 1;
@@ -172,7 +166,7 @@ impl File {
}
let count = inner.vnode.for_each_entry(inner.pos, count, |i, e| {
entries[offset + i] = DirectoryEntry::from_str(e.name()).unwrap();
entries[offset + i] = DirectoryEntry::from_str(e.name());
});
inner.pos += count;
Ok(offset + count)
@@ -182,14 +176,14 @@ impl File {
pub fn readdir(&mut self, entries: &mut [DirectoryEntry]) -> Result<usize, Errno> {
match &mut self.inner {
FileInner::Normal(inner) => {
// assert_eq!(inner.vnode.kind(), VnodeKind::Directory);
assert_eq!(inner.vnode.kind(), VnodeKind::Directory);
if inner.vnode.flags() & Vnode::CACHE_READDIR != 0 {
Self::cache_readdir(inner, entries)
} else {
todo!();
}
}
},
_ => todo!(),
}
}
@@ -209,38 +203,24 @@ impl Drop for File {
#[cfg(test)]
mod tests {
use super::*;
use crate::node::{VnodeCommon, VnodeFile};
use crate::{Vnode, VnodeImpl, VnodeKind, VnodeRef};
use libsys::{stat::OpenFlags, ioctl::IoctlCmd, stat::Stat};
use alloc::boxed::Box;
use alloc::rc::Rc;
use libsys::{ioctl::IoctlCmd, stat::OpenFlags, stat::Stat};
struct DummyInode;
impl VnodeCommon for DummyInode {
/// Performs filetype-specific request
fn ioctl(
#[auto_inode]
impl VnodeImpl for DummyInode {
fn create(
&mut self,
node: VnodeRef,
cmd: IoctlCmd,
ptr: usize,
len: usize,
) -> Result<usize, Errno> {
todo!()
}
/// Retrieves file status
fn stat(&mut self, node: VnodeRef) -> Result<Stat, Errno> {
todo!()
}
/// Reports the size of this filesystem object in bytes
fn size(&mut self, node: VnodeRef) -> Result<usize, Errno> {
todo!()
}
/// Returns `true` if node is ready for an operation
fn is_ready(&mut self, node: VnodeRef, write: bool) -> Result<bool, Errno> {
todo!()
_at: VnodeRef,
name: &str,
kind: VnodeKind,
) -> Result<VnodeRef, Errno> {
let node = Vnode::new(name, kind, 0);
node.set_data(Box::new(DummyInode {}));
Ok(node)
}
fn open(&mut self, _node: VnodeRef, _flags: OpenFlags) -> Result<usize, Errno> {
@@ -250,9 +230,7 @@ mod tests {
fn close(&mut self, _node: VnodeRef) -> Result<(), Errno> {
Ok(())
}
}
impl VnodeFile for DummyInode {
fn read(&mut self, _node: VnodeRef, pos: usize, data: &mut [u8]) -> Result<usize, Errno> {
#[cfg(test)]
println!("read {} at {}", data.len(), pos);
@@ -270,19 +248,12 @@ mod tests {
fn write(&mut self, _node: VnodeRef, _pos: usize, _data: &[u8]) -> Result<usize, Errno> {
Err(Errno::NotImplemented)
}
fn truncate(&mut self, node: VnodeRef, size: usize) -> Result<(), Errno> {
todo!()
}
}
#[test]
fn test_normal_read() {
let node = Vnode::new(
"",
VnodeData::File(RefCell::new(Some(Box::new(DummyInode {})))),
0,
);
let node = Vnode::new("", VnodeKind::Regular, 0);
node.set_data(Box::new(DummyInode {}));
let mut file = node.open(OpenFlags::O_RDONLY).unwrap();
let mut buf = [0u8; 4096];
+199 -198
View File
@@ -1,4 +1,4 @@
use crate::{FileRef, VnodeCreateKind, VnodeRef};
use crate::{FileRef, VnodeKind, VnodeRef};
use libsys::{
error::Errno,
path::{path_component_left, path_component_right},
@@ -34,10 +34,9 @@ impl Ioctx {
loop {
(element, rest) = path_component_left(rest);
at.as_directory()?;
// if !at.is_directory() {
// return Err(Errno::NotADirectory);
// }
if !at.is_directory() {
return Err(Errno::NotADirectory);
}
match element {
".." => {
@@ -49,7 +48,7 @@ impl Ioctx {
}
while let Some(target) = at.target() {
// assert!(at.kind() == VnodeKind::Directory);
assert!(at.kind() == VnodeKind::Directory);
at = target;
}
@@ -61,7 +60,7 @@ impl Ioctx {
let mut node = at.lookup_or_load(element)?;
while let Some(target) = node.target() {
// assert!(node.kind() == VnodeKind::Directory);
assert!(node.kind() == VnodeKind::Directory);
node = target;
}
@@ -102,7 +101,7 @@ impl Ioctx {
self.find(at, parent, true)?.create(
name.trim_start_matches('/'),
mode,
VnodeCreateKind::Directory,
VnodeKind::Directory,
)
}
@@ -115,10 +114,10 @@ impl Ioctx {
opts: OpenFlags,
) -> Result<FileRef, Errno> {
let node = match self.find(at.clone(), path, true) {
Err(Errno::DoesNotExist) if opts.contains(OpenFlags::O_CREAT) => {
Err(Errno::DoesNotExist) => {
let (parent, name) = path_component_right(path);
let at = self.find(at, parent, true)?;
at.create(name, mode, VnodeCreateKind::File)
at.create(name, mode, VnodeKind::Regular)
}
o => o,
}?;
@@ -129,196 +128,198 @@ impl Ioctx {
/// Changes current working directory of the process
pub fn chdir(&mut self, path: &str) -> Result<(), Errno> {
let node = self.find(None, path, true)?;
let _dir = node.as_directory()?;
if !node.is_directory() {
return Err(Errno::NotADirectory);
}
self.cwd = node;
Ok(())
}
}
// #[cfg(test)]
// mod tests {
// use super::*;
// use crate::{Vnode, VnodeImpl, VnodeKind};
// use alloc::{boxed::Box, rc::Rc};
// use libsys::{ioctl::IoctlCmd, stat::OpenFlags, stat::Stat};
//
// pub struct DummyInode;
//
// #[auto_inode]
// impl VnodeImpl for DummyInode {
// fn create(
// &mut self,
// _at: VnodeRef,
// name: &str,
// kind: VnodeKind,
// ) -> Result<VnodeRef, Errno> {
// let vnode = Vnode::new(name, kind, 0);
// vnode.set_data(Box::new(DummyInode {}));
// Ok(vnode)
// }
//
// fn lookup(&mut self, _at: VnodeRef, _name: &str) -> Result<VnodeRef, Errno> {
// Err(Errno::DoesNotExist)
// }
// }
//
// #[test]
// fn test_find_existing_absolute() {
// let root = Vnode::new("", VnodeKind::Directory, 0);
// let d0 = Vnode::new("dir0", VnodeKind::Directory, 0);
// let d1 = Vnode::new("dir1", VnodeKind::Directory, 0);
// let d0d0 = Vnode::new("dir0", VnodeKind::Directory, 0);
// let d0f0 = Vnode::new("file0", VnodeKind::Regular, 0);
// let d1f0 = Vnode::new("file0", VnodeKind::Regular, 0);
//
// root.attach(d0.clone());
// root.attach(d1.clone());
// d0.attach(d0d0.clone());
// d0.attach(d0f0.clone());
// d1.attach(d1f0.clone());
//
// let ioctx = Ioctx::new(root.clone());
//
// assert!(Rc::ptr_eq(&root, &ioctx.find(None, "/", false).unwrap()));
// assert!(Rc::ptr_eq(&root, &ioctx.find(None, "/.", false).unwrap()));
// assert!(Rc::ptr_eq(&root, &ioctx.find(None, "/./.", false).unwrap()));
// assert!(Rc::ptr_eq(
// &root,
// &ioctx.find(None, "/.///.", false).unwrap()
// ));
// assert!(Rc::ptr_eq(&root, &ioctx.find(None, "/..", false).unwrap()));
// assert!(Rc::ptr_eq(&root, &ioctx.find(None, "/../", false).unwrap()));
// assert!(Rc::ptr_eq(
// &root,
// &ioctx.find(None, "/../.", false).unwrap()
// ));
// assert!(Rc::ptr_eq(
// &root,
// &ioctx.find(None, "/../..", false).unwrap()
// ));
//
// assert!(Rc::ptr_eq(&d0, &ioctx.find(None, "/dir0", false).unwrap()));
// assert!(Rc::ptr_eq(&d1, &ioctx.find(None, "/dir1", false).unwrap()));
// assert!(Rc::ptr_eq(
// &d0,
// &ioctx.find(None, "/dir1/../dir0", false).unwrap()
// ));
// assert!(Rc::ptr_eq(
// &d1,
// &ioctx
// .find(None, "/dir1/../dir0/./../../.././dir1", false)
// .unwrap()
// ));
//
// assert!(Rc::ptr_eq(
// &d0d0,
// &ioctx.find(None, "/dir0/dir0", false).unwrap()
// ));
// assert!(Rc::ptr_eq(
// &d0d0,
// &ioctx.find(None, "/dir0/dir0/.", false).unwrap()
// ));
// assert!(Rc::ptr_eq(
// &d0,
// &ioctx.find(None, "/dir0/dir0/..", false).unwrap()
// ));
// assert!(Rc::ptr_eq(
// &d0,
// &ioctx.find(None, "/dir0/dir0/../", false).unwrap()
// ));
// assert!(Rc::ptr_eq(
// &d0,
// &ioctx.find(None, "/dir0/dir0/../.", false).unwrap()
// ));
//
// assert!(Rc::ptr_eq(
// &d0f0,
// &ioctx.find(None, "/dir0/file0", false).unwrap()
// ));
// assert!(Rc::ptr_eq(
// &d0f0,
// &ioctx.find(None, "/dir1/../dir0/./file0", false).unwrap()
// ));
// }
//
// #[test]
// fn test_find_rejects_file_dots() {
// let root = Vnode::new("", VnodeKind::Directory, 0);
// let d0 = Vnode::new("dir0", VnodeKind::Directory, 0);
// let d0f0 = Vnode::new("file0", VnodeKind::Regular, 0);
//
// root.attach(d0.clone());
// d0.attach(d0f0.clone());
//
// let ioctx = Ioctx::new(root.clone());
//
// assert_eq!(
// ioctx.find(None, "/dir0/file0/.", false).unwrap_err(),
// Errno::NotADirectory
// );
// assert_eq!(
// ioctx.find(None, "/dir0/file0/..", false).unwrap_err(),
// Errno::NotADirectory
// );
//
// // TODO handle this case
// // assert_eq!(ioctx.find(None, "/dir0/file0/").unwrap_err(), Errno::NotADirectory);
// }
//
// #[test]
// fn test_mkdir() {
// let root = Vnode::new("", VnodeKind::Directory, 0);
// let ioctx = Ioctx::new(root.clone());
//
// root.set_data(Box::new(DummyInode {}));
//
// assert!(ioctx.mkdir(None, "/dir0", FileMode::default_dir()).is_ok());
// assert_eq!(
// ioctx
// .mkdir(None, "/dir0", FileMode::default_dir())
// .unwrap_err(),
// Errno::AlreadyExists
// );
// }
//
// #[test]
// fn test_find_mount() {
// let root_outer = Vnode::new("", VnodeKind::Directory, 0);
// let dir0 = Vnode::new("dir0", VnodeKind::Directory, 0);
// let root_inner = Vnode::new("", VnodeKind::Directory, 0);
// let dir1 = Vnode::new("dir1", VnodeKind::Directory, 0);
//
// root_outer.clone().attach(dir0.clone());
// root_inner.clone().attach(dir1.clone());
//
// let ioctx = Ioctx::new(root_outer.clone());
//
// assert_eq!(
// ioctx.find(None, "/dir0/dir1", false).unwrap_err(),
// Errno::DoesNotExist
// );
//
// dir0.mount(root_inner.clone()).unwrap();
//
// assert!(Rc::ptr_eq(
// &root_inner,
// &ioctx.find(None, "/dir0", false).unwrap()
// ));
// assert!(Rc::ptr_eq(
// &dir1,
// &ioctx.find(None, "/dir0/dir1", false).unwrap()
// ));
// assert!(Rc::ptr_eq(
// &root_inner,
// &ioctx.find(None, "/dir0/dir1/..", false).unwrap()
// ));
// assert!(Rc::ptr_eq(
// &dir0,
// &ioctx.find(None, "/dir0/dir1/../..", false).unwrap()
// ));
// assert!(Rc::ptr_eq(
// &root_outer,
// &ioctx.find(None, "/dir0/dir1/../../..", false).unwrap()
// ));
// }
// }
#[cfg(test)]
mod tests {
use super::*;
use crate::{Vnode, VnodeImpl, VnodeKind};
use alloc::{boxed::Box, rc::Rc};
use libsys::{ioctl::IoctlCmd, stat::OpenFlags, stat::Stat};
pub struct DummyInode;
#[auto_inode]
impl VnodeImpl for DummyInode {
fn create(
&mut self,
_at: VnodeRef,
name: &str,
kind: VnodeKind,
) -> Result<VnodeRef, Errno> {
let vnode = Vnode::new(name, kind, 0);
vnode.set_data(Box::new(DummyInode {}));
Ok(vnode)
}
fn lookup(&mut self, _at: VnodeRef, _name: &str) -> Result<VnodeRef, Errno> {
Err(Errno::DoesNotExist)
}
}
#[test]
fn test_find_existing_absolute() {
let root = Vnode::new("", VnodeKind::Directory, 0);
let d0 = Vnode::new("dir0", VnodeKind::Directory, 0);
let d1 = Vnode::new("dir1", VnodeKind::Directory, 0);
let d0d0 = Vnode::new("dir0", VnodeKind::Directory, 0);
let d0f0 = Vnode::new("file0", VnodeKind::Regular, 0);
let d1f0 = Vnode::new("file0", VnodeKind::Regular, 0);
root.attach(d0.clone());
root.attach(d1.clone());
d0.attach(d0d0.clone());
d0.attach(d0f0.clone());
d1.attach(d1f0.clone());
let ioctx = Ioctx::new(root.clone());
assert!(Rc::ptr_eq(&root, &ioctx.find(None, "/", false).unwrap()));
assert!(Rc::ptr_eq(&root, &ioctx.find(None, "/.", false).unwrap()));
assert!(Rc::ptr_eq(&root, &ioctx.find(None, "/./.", false).unwrap()));
assert!(Rc::ptr_eq(
&root,
&ioctx.find(None, "/.///.", false).unwrap()
));
assert!(Rc::ptr_eq(&root, &ioctx.find(None, "/..", false).unwrap()));
assert!(Rc::ptr_eq(&root, &ioctx.find(None, "/../", false).unwrap()));
assert!(Rc::ptr_eq(
&root,
&ioctx.find(None, "/../.", false).unwrap()
));
assert!(Rc::ptr_eq(
&root,
&ioctx.find(None, "/../..", false).unwrap()
));
assert!(Rc::ptr_eq(&d0, &ioctx.find(None, "/dir0", false).unwrap()));
assert!(Rc::ptr_eq(&d1, &ioctx.find(None, "/dir1", false).unwrap()));
assert!(Rc::ptr_eq(
&d0,
&ioctx.find(None, "/dir1/../dir0", false).unwrap()
));
assert!(Rc::ptr_eq(
&d1,
&ioctx
.find(None, "/dir1/../dir0/./../../.././dir1", false)
.unwrap()
));
assert!(Rc::ptr_eq(
&d0d0,
&ioctx.find(None, "/dir0/dir0", false).unwrap()
));
assert!(Rc::ptr_eq(
&d0d0,
&ioctx.find(None, "/dir0/dir0/.", false).unwrap()
));
assert!(Rc::ptr_eq(
&d0,
&ioctx.find(None, "/dir0/dir0/..", false).unwrap()
));
assert!(Rc::ptr_eq(
&d0,
&ioctx.find(None, "/dir0/dir0/../", false).unwrap()
));
assert!(Rc::ptr_eq(
&d0,
&ioctx.find(None, "/dir0/dir0/../.", false).unwrap()
));
assert!(Rc::ptr_eq(
&d0f0,
&ioctx.find(None, "/dir0/file0", false).unwrap()
));
assert!(Rc::ptr_eq(
&d0f0,
&ioctx.find(None, "/dir1/../dir0/./file0", false).unwrap()
));
}
#[test]
fn test_find_rejects_file_dots() {
let root = Vnode::new("", VnodeKind::Directory, 0);
let d0 = Vnode::new("dir0", VnodeKind::Directory, 0);
let d0f0 = Vnode::new("file0", VnodeKind::Regular, 0);
root.attach(d0.clone());
d0.attach(d0f0.clone());
let ioctx = Ioctx::new(root.clone());
assert_eq!(
ioctx.find(None, "/dir0/file0/.", false).unwrap_err(),
Errno::NotADirectory
);
assert_eq!(
ioctx.find(None, "/dir0/file0/..", false).unwrap_err(),
Errno::NotADirectory
);
// TODO handle this case
// assert_eq!(ioctx.find(None, "/dir0/file0/").unwrap_err(), Errno::NotADirectory);
}
#[test]
fn test_mkdir() {
let root = Vnode::new("", VnodeKind::Directory, 0);
let ioctx = Ioctx::new(root.clone());
root.set_data(Box::new(DummyInode {}));
assert!(ioctx.mkdir(None, "/dir0", FileMode::default_dir()).is_ok());
assert_eq!(
ioctx
.mkdir(None, "/dir0", FileMode::default_dir())
.unwrap_err(),
Errno::AlreadyExists
);
}
#[test]
fn test_find_mount() {
let root_outer = Vnode::new("", VnodeKind::Directory, 0);
let dir0 = Vnode::new("dir0", VnodeKind::Directory, 0);
let root_inner = Vnode::new("", VnodeKind::Directory, 0);
let dir1 = Vnode::new("dir1", VnodeKind::Directory, 0);
root_outer.clone().attach(dir0.clone());
root_inner.clone().attach(dir1.clone());
let ioctx = Ioctx::new(root_outer.clone());
assert_eq!(
ioctx.find(None, "/dir0/dir1", false).unwrap_err(),
Errno::DoesNotExist
);
dir0.mount(root_inner.clone()).unwrap();
assert!(Rc::ptr_eq(
&root_inner,
&ioctx.find(None, "/dir0", false).unwrap()
));
assert!(Rc::ptr_eq(
&dir1,
&ioctx.find(None, "/dir0/dir1", false).unwrap()
));
assert!(Rc::ptr_eq(
&root_inner,
&ioctx.find(None, "/dir0/dir1/..", false).unwrap()
));
assert!(Rc::ptr_eq(
&dir0,
&ioctx.find(None, "/dir0/dir1/../..", false).unwrap()
));
assert!(Rc::ptr_eq(
&root_outer,
&ioctx.find(None, "/dir0/dir1/../../..", false).unwrap()
));
}
}
+6 -5
View File
@@ -1,12 +1,15 @@
//! Virtual filesystem API and facilities
#![warn(missing_docs)]
#![feature(const_fn_trait_bound, const_discriminant)]
#![feature(destructuring_assignment, const_fn_trait_bound)]
#![no_std]
#[cfg(test)]
#[macro_use]
extern crate std;
#[macro_use]
extern crate fs_macros;
extern crate alloc;
// pub use libsys::stat::{FileMode, OpenFlags, Stat};
@@ -17,12 +20,10 @@ pub use block::BlockDevice;
mod fs;
pub use fs::Filesystem;
mod node;
pub use node::{
Vnode, VnodeCommon, VnodeCreateKind, VnodeData, VnodeDirectory, VnodeFile, VnodeRef,
};
pub use node::{Vnode, VnodeImpl, VnodeKind, VnodeRef};
mod ioctx;
pub use ioctx::Ioctx;
mod file;
pub use file::{File, FileRef};
mod char;
pub use crate::char::CharDevice;
pub use crate::char::{CharDevice, CharDeviceWrapper};
+211 -326
View File
@@ -1,8 +1,7 @@
use crate::{CharDevice, File, FileRef, Filesystem, Ioctx};
use crate::{File, FileRef, Filesystem, Ioctx};
use alloc::{borrow::ToOwned, boxed::Box, rc::Rc, string::String, vec::Vec};
use core::cell::{Ref, RefCell, RefMut};
use core::cell::{RefCell, RefMut, Ref};
use core::fmt;
use core::mem::Discriminant;
use libsys::{
error::Errno,
ioctl::IoctlCmd,
@@ -11,81 +10,20 @@ use libsys::{
/// Convenience type alias for [Rc<Vnode>]
pub type VnodeRef = Rc<Vnode>;
pub type VnodeKind = Discriminant<VnodeData>;
/// Trait implemented by both regular files and directories
pub trait VnodeCommon {
/// Performs filetype-specific request
fn ioctl(
&mut self,
node: VnodeRef,
cmd: IoctlCmd,
ptr: usize,
len: usize,
) -> Result<usize, Errno>;
/// Retrieves file status
fn stat(&mut self, node: VnodeRef) -> Result<Stat, Errno>;
/// Reports the size of this filesystem object in bytes
fn size(&mut self, node: VnodeRef) -> Result<usize, Errno>;
/// Returns `true` if node is ready for an operation
fn ready(&mut self, node: VnodeRef, write: bool) -> Result<bool, Errno>;
/// Opens a vnode for access. Returns initial file position.
fn open(&mut self, node: VnodeRef, opts: OpenFlags) -> Result<usize, Errno>;
/// Closes a vnode
fn close(&mut self, node: VnodeRef) -> Result<(), Errno>;
/// List of possible vnode types
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum VnodeKind {
/// Node is a directory with create/lookup/remove operations
Directory,
/// Node is a regular file
Regular,
/// Node is a character device
Char,
/// Node is a block device
Block,
}
/// Regular file access interface
pub trait VnodeFile: VnodeCommon {
/// Changes file's underlying storage size
fn truncate(&mut self, node: VnodeRef, size: usize) -> Result<(), Errno>;
/// Reads `data.len()` bytes into the buffer from file offset `pos`
fn read(&mut self, node: VnodeRef, pos: usize, data: &mut [u8]) -> Result<usize, Errno>;
/// Writes `data.len()` bytes from the buffer to file offset `pos`.
/// Resizes the file storage if necessary.
fn write(&mut self, node: VnodeRef, pos: usize, data: &[u8]) -> Result<usize, Errno>;
}
/// Directory access interface
pub trait VnodeDirectory: VnodeCommon {
/// Creates entry `name` of type `kind` in directory `at`
fn create(
&mut self,
at: VnodeRef,
name: &str,
kind: VnodeCreateKind,
) -> Result<VnodeRef, Errno>;
/// Removes entry `name` for directory `at`
fn remove(&mut self, at: VnodeRef, name: &str) -> Result<(), Errno>;
/// Loads an entry `name` in directory `at` from filesystem
fn lookup(&mut self, at: VnodeRef, name: &str) -> Result<VnodeRef, Errno>;
/// Read directory entries into target buffer
fn readdir(
&mut self,
node: VnodeRef,
pos: usize,
data: &mut [DirectoryEntry],
) -> Result<usize, Errno>;
}
// /// List of possible vnode types
// #[derive(Debug, Clone, Copy, PartialEq)]
// pub enum VnodeKind {
// /// Node is a directory with create/lookup/remove operations
// Directory,
// /// Node is a regular file
// Regular,
// /// Node is a character device
// Char,
// /// Node is a block device
// Block,
// }
pub(crate) struct TreeNode {
parent: Option<VnodeRef>,
children: Vec<VnodeRef>,
@@ -97,25 +35,6 @@ pub struct VnodeProps {
pub mode: FileMode,
}
/// Specific node implementation data
pub enum VnodeData {
/// Directory node
Directory(RefCell<Option<Box<dyn VnodeDirectory>>>),
/// Regular file node
File(RefCell<Option<Box<dyn VnodeFile>>>),
/// Character device node
Char(&'static dyn CharDevice),
}
/// Node types for create() calls
#[derive(Debug, Clone, Copy)]
pub enum VnodeCreateKind {
/// Directory node
Directory,
/// Regular file node
File,
}
/// Virtual filesystem node struct, generalizes access to
/// underlying real filesystems
pub struct Vnode {
@@ -123,13 +42,64 @@ pub struct Vnode {
tree: RefCell<TreeNode>,
props: RefCell<VnodeProps>,
// kind: VnodeKind,
data: VnodeData,
kind: VnodeKind,
flags: u32,
target: RefCell<Option<VnodeRef>>,
fs: RefCell<Option<Rc<dyn Filesystem>>>,
// data: RefCell<Option<Box<dyn VnodeImpl>>>,
data: RefCell<Option<Box<dyn VnodeImpl>>>,
}
/// Interface for "inode" of a real filesystem
pub trait VnodeImpl {
// Directory-only operations
/// Creates a new vnode, sets it up, attaches it (in real FS) to `at` with `name` and
/// returns it
fn create(&mut self, at: VnodeRef, name: &str, kind: VnodeKind) -> Result<VnodeRef, Errno>;
/// Removes the filesystem inode from its parent by erasing its directory entry
fn remove(&mut self, at: VnodeRef, name: &str) -> Result<(), Errno>;
/// Looks up a corresponding directory entry for `name`. If present, loads its inode from
/// storage medium and returns a new vnode associated with it.
fn lookup(&mut self, at: VnodeRef, name: &str) -> Result<VnodeRef, Errno>;
/// Opens a vnode for access. Returns initial file position.
fn open(&mut self, node: VnodeRef, opts: OpenFlags) -> Result<usize, Errno>;
/// Closes a vnode
fn close(&mut self, node: VnodeRef) -> Result<(), Errno>;
/// Changes file's underlying storage size
fn truncate(&mut self, node: VnodeRef, size: usize) -> Result<(), Errno>;
/// Reads `data.len()` bytes into the buffer from file offset `pos`
fn read(&mut self, node: VnodeRef, pos: usize, data: &mut [u8]) -> Result<usize, Errno>;
/// Writes `data.len()` bytes from the buffer to file offset `pos`.
/// Resizes the file storage if necessary.
fn write(&mut self, node: VnodeRef, pos: usize, data: &[u8]) -> Result<usize, Errno>;
/// Read directory entries into target buffer
fn readdir(
&mut self,
node: VnodeRef,
pos: usize,
data: &mut [DirectoryEntry],
) -> Result<usize, Errno>;
/// Retrieves file status
fn stat(&mut self, node: VnodeRef) -> Result<Stat, Errno>;
/// Reports the size of this filesystem object in bytes
fn size(&mut self, node: VnodeRef) -> Result<usize, Errno>;
/// Returns `true` if node is ready for an operation
fn is_ready(&mut self, node: VnodeRef, write: bool) -> Result<bool, Errno>;
/// Performs filetype-specific request
fn ioctl(
&mut self,
node: VnodeRef,
cmd: IoctlCmd,
ptr: usize,
len: usize,
) -> Result<usize, Errno>;
}
impl Vnode {
@@ -144,10 +114,10 @@ impl Vnode {
/// Constructs a new [Vnode], wrapping it in [Rc]. The resulting node
/// then needs to have [Vnode::set_data()] called on it to be usable.
pub fn new(name: &str, data: VnodeData, flags: u32) -> VnodeRef {
pub fn new(name: &str, kind: VnodeKind, flags: u32) -> VnodeRef {
Rc::new(Self {
name: name.to_owned(),
data,
kind,
flags,
props: RefCell::new(VnodeProps {
mode: FileMode::empty(),
@@ -158,6 +128,7 @@ impl Vnode {
}),
target: RefCell::new(None),
fs: RefCell::new(None),
data: RefCell::new(None),
})
}
@@ -176,30 +147,19 @@ impl Vnode {
self.props.borrow()
}
// /// Sets an associated [VnodeImpl] for the [Vnode]
// pub fn set_data(&self, data: Box<dyn VnodeImpl>) {
// *self.data.borrow_mut() = Some(data);
// }
/// Sets an associated [VnodeImpl] for the [Vnode]
pub fn set_data(&self, data: Box<dyn VnodeImpl>) {
*self.data.borrow_mut() = Some(data);
}
/// Sets an associated [Filesystem] for the [Vnode]
pub fn set_fs(&self, fs: Rc<dyn Filesystem>) {
*self.fs.borrow_mut() = Some(fs);
}
/// Returns node's directory implementation data
pub fn as_directory(&self) -> Result<&RefCell<Option<Box<dyn VnodeDirectory>>>, Errno> {
match &self.data {
VnodeData::Directory(data) => Ok(data),
_ => Err(Errno::NotADirectory),
}
}
/// Returns node's regular file implementation data
pub fn as_file(&self) -> Result<&RefCell<Option<Box<dyn VnodeFile>>>, Errno> {
match &self.data {
VnodeData::File(data) => Ok(data),
_ => Err(Errno::IsADirectory),
}
/// Returns a reference to the associated [VnodeImpl]
pub fn data(&self) -> RefMut<Option<Box<dyn VnodeImpl>>> {
self.data.borrow_mut()
}
/// Returns the associated [Fileystem]
@@ -207,6 +167,11 @@ impl Vnode {
self.fs.borrow().clone()
}
/// Returns `true` if the vnode represents a directory
pub fn is_directory(&self) -> bool {
self.kind == VnodeKind::Directory
}
/// Returns `true` if the vnode allows arbitrary seeking
pub fn is_seekable(&self) -> bool {
self.flags & Self::SEEKABLE != 0
@@ -215,7 +180,7 @@ impl Vnode {
/// Returns kind of the vnode
#[inline(always)]
pub const fn kind(&self) -> VnodeKind {
core::mem::discriminant(&self.data)
self.kind
}
/// Returns flags of the vnode
@@ -255,9 +220,12 @@ impl Vnode {
/// Attaches some filesystem's root directory node at another directory
pub fn mount(self: &VnodeRef, root: VnodeRef) -> Result<(), Errno> {
let _dir = self.as_directory()?;
let _root_dir = root.as_directory()?;
if !self.is_directory() {
return Err(Errno::NotADirectory);
}
if !root.is_directory() {
return Err(Errno::NotADirectory);
}
if self.target.borrow().is_some() {
return Err(Errno::Busy);
}
@@ -284,7 +252,7 @@ impl Vnode {
/// Looks up a child `name` in in-memory tree cache
pub fn lookup(self: &VnodeRef, name: &str) -> Option<VnodeRef> {
// assert!(self.is_directory());
assert!(self.is_directory());
self.tree
.borrow()
.children
@@ -299,8 +267,7 @@ impl Vnode {
limit: usize,
mut f: F,
) -> usize {
// TODO
// assert!(self.is_directory());
assert!(self.is_directory());
let mut count = 0;
for (index, item) in self
.tree
@@ -320,12 +287,12 @@ impl Vnode {
/// Looks up a child `name` in `self`. Will first try looking up a cached
/// vnode and will load it from disk if it's missing.
pub fn lookup_or_load(self: &VnodeRef, name: &str) -> Result<VnodeRef, Errno> {
let dir = self.as_directory()?;
if let Some(node) = self.lookup(name) {
Ok(node)
} else if let Some(ref mut dir) = *dir.borrow_mut() {
let vnode = dir.lookup(self.clone(), name)?;
return Ok(node);
}
if let Some(ref mut data) = *self.data() {
let vnode = data.lookup(self.clone(), name)?;
if let Some(fs) = self.fs() {
vnode.set_fs(fs);
}
@@ -341,10 +308,11 @@ impl Vnode {
self: &VnodeRef,
name: &str,
mode: FileMode,
kind: VnodeCreateKind,
kind: VnodeKind,
) -> Result<VnodeRef, Errno> {
let dir = self.as_directory()?;
if self.kind != VnodeKind::Directory {
return Err(Errno::NotADirectory);
}
if name.contains('/') {
return Err(Errno::InvalidArgument);
}
@@ -355,8 +323,8 @@ impl Vnode {
e => return e,
};
if let Some(ref mut dir) = *dir.borrow_mut() {
let vnode = dir.create(self.clone(), name, kind)?;
if let Some(ref mut data) = *self.data() {
let vnode = data.create(self.clone(), name, kind)?;
if let Some(fs) = self.fs() {
vnode.set_fs(fs);
}
@@ -370,14 +338,16 @@ impl Vnode {
/// Removes a directory entry `name` from `self`
pub fn unlink(self: &VnodeRef, name: &str) -> Result<(), Errno> {
let dir = self.as_directory()?;
if self.kind != VnodeKind::Directory {
return Err(Errno::NotADirectory);
}
if name.contains('/') {
return Err(Errno::InvalidArgument);
}
if let Some(ref mut dir) = *dir.borrow_mut() {
if let Some(ref mut data) = *self.data() {
let vnode = self.lookup(name).ok_or(Errno::DoesNotExist)?;
dir.remove(self.clone(), name)?;
data.remove(self.clone(), name)?;
vnode.detach();
Ok(())
} else {
@@ -388,123 +358,102 @@ impl Vnode {
/// Opens a vnode for access
pub fn open(self: &VnodeRef, flags: OpenFlags) -> Result<FileRef, Errno> {
let mut open_flags = 0;
if flags.contains(OpenFlags::O_DIRECTORY) {
if self.kind != VnodeKind::Directory {
return Err(Errno::NotADirectory);
}
if flags & OpenFlags::O_ACCESS != OpenFlags::O_RDONLY {
return Err(Errno::IsADirectory);
}
open_flags = File::READ;
} else {
if self.kind == VnodeKind::Directory {
return Err(Errno::IsADirectory);
}
match flags & OpenFlags::O_ACCESS {
OpenFlags::O_RDONLY => open_flags |= File::READ,
OpenFlags::O_WRONLY => open_flags |= File::WRITE,
OpenFlags::O_RDWR => open_flags |= File::READ | File::WRITE,
_ => unimplemented!(),
}
}
if flags.contains(OpenFlags::O_CLOEXEC) {
open_flags |= File::CLOEXEC;
}
match flags & OpenFlags::O_ACCESS {
OpenFlags::O_RDONLY => open_flags |= File::READ,
OpenFlags::O_WRONLY => open_flags |= File::WRITE,
OpenFlags::O_RDWR => open_flags |= File::READ | File::WRITE,
_ => unimplemented!(),
}
match &self.data {
VnodeData::Directory(_) => {
if !flags.contains(OpenFlags::O_DIRECTORY) {
return Err(Errno::NotADirectory);
}
if self.flags & Vnode::CACHE_READDIR != 0 {
Ok(File::normal(self.clone(), File::POS_CACHE_DOT, open_flags))
} else {
todo!()
}
}
VnodeData::File(file) => {
if flags.contains(OpenFlags::O_DIRECTORY) {
return Err(Errno::IsADirectory);
}
if let Some(ref mut file) = *file.borrow_mut() {
let pos = file.open(self.clone(), flags)?;
Ok(File::normal(self.clone(), pos, open_flags))
} else {
Err(Errno::NotImplemented)
}
}
VnodeData::Char(_) => {
if flags.contains(OpenFlags::O_DIRECTORY) {
return Err(Errno::IsADirectory);
}
Ok(File::normal(self.clone(), 0, open_flags))
if self.kind == VnodeKind::Directory && self.flags & Vnode::CACHE_READDIR != 0 {
Ok(File::normal(self.clone(), File::POS_CACHE_DOT, open_flags))
} else {
if let Some(ref mut data) = *self.data() {
let pos = data.open(self.clone(), flags)?;
Ok(File::normal(self.clone(), pos, open_flags))
} else {
Err(Errno::NotImplemented)
}
}
}
/// Closes a vnode
pub fn close(self: &VnodeRef) -> Result<(), Errno> {
match &self.data {
VnodeData::Directory(dir) => {
if let Some(ref mut dir) = *dir.borrow_mut() {
return dir.close(self.clone());
}
if self.kind == VnodeKind::Directory && self.flags & Vnode::CACHE_READDIR != 0 {
Ok(())
} else {
if let Some(ref mut data) = *self.data() {
data.close(self.clone())
} else {
Err(Errno::NotImplemented)
}
VnodeData::File(file) => {
if let Some(ref mut file) = *file.borrow_mut() {
return file.close(self.clone());
}
}
_ => {}
}
Err(Errno::NotImplemented)
}
/// Reads data from offset `pos` into `buf`
pub fn read(self: &VnodeRef, pos: usize, buf: &mut [u8]) -> Result<usize, Errno> {
match &self.data {
VnodeData::File(file) => file
.borrow_mut()
.as_mut()
.ok_or(Errno::NotImplemented)?
.read(self.clone(), pos, buf),
VnodeData::Char(chr) => chr.read(true, buf),
_ => Err(Errno::InvalidOperation),
if self.kind == VnodeKind::Directory {
return Err(Errno::IsADirectory);
}
if let Some(ref mut data) = *self.data() {
data.read(self.clone(), pos, buf)
} else {
Err(Errno::NotImplemented)
}
}
/// Writes data from `buf` to offset `pos`
pub fn write(self: &VnodeRef, pos: usize, buf: &[u8]) -> Result<usize, Errno> {
match &self.data {
VnodeData::File(file) => file
.borrow_mut()
.as_mut()
.ok_or(Errno::NotImplemented)?
.write(self.clone(), pos, buf),
VnodeData::Char(chr) => chr.write(true, buf),
_ => Err(Errno::InvalidOperation),
if self.kind == VnodeKind::Directory {
return Err(Errno::IsADirectory);
}
if let Some(ref mut data) = *self.data() {
data.write(self.clone(), pos, buf)
} else {
Err(Errno::NotImplemented)
}
}
/// Resizes the vnode data
pub fn truncate(self: &VnodeRef, size: usize) -> Result<(), Errno> {
match &self.data {
VnodeData::File(file) => file
.borrow_mut()
.as_mut()
.ok_or(Errno::NotImplemented)?
.truncate(self.clone(), size),
_ => Err(Errno::InvalidOperation),
if self.kind != VnodeKind::Regular {
return Err(Errno::IsADirectory);
}
if let Some(ref mut data) = *self.data() {
data.truncate(self.clone(), size)
} else {
Err(Errno::NotImplemented)
}
}
/// Returns current vnode data size
pub fn size(self: &VnodeRef) -> Result<usize, Errno> {
match &self.data {
VnodeData::File(file) => file
.borrow_mut()
.as_mut()
.ok_or(Errno::NotImplemented)?
.size(self.clone()),
VnodeData::Directory(dir) => dir
.borrow_mut()
.as_mut()
.ok_or(Errno::NotImplemented)?
.size(self.clone()),
VnodeData::Char(_) => Ok(0),
if let Some(ref mut data) = *self.data() {
data.size(self.clone())
} else {
Err(Errno::NotImplemented)
}
}
@@ -515,43 +464,30 @@ impl Vnode {
Ok(Stat {
blksize: 0,
size: 0,
mode: props.mode,
mode: props.mode
})
} else if let Some(ref mut data) = *self.data() {
data.stat(self.clone())
} else {
match &self.data {
VnodeData::File(file) => file
.borrow_mut()
.as_mut()
.ok_or(Errno::NotADirectory)?
.stat(self.clone()),
VnodeData::Directory(dir) => dir
.borrow_mut()
.as_mut()
.ok_or(Errno::NotImplemented)?
.stat(self.clone()),
// TODO stat() for char/blk devs
_ => todo!(),
}
Err(Errno::NotImplemented)
}
}
/// Performs node-specific requests
pub fn ioctl(self: &VnodeRef, cmd: IoctlCmd, ptr: usize, len: usize) -> Result<usize, Errno> {
match &self.data {
VnodeData::Char(chr) => chr.ioctl(cmd, ptr, len),
_ => Err(Errno::InvalidOperation),
if let Some(ref mut data) = *self.data() {
data.ioctl(self.clone(), cmd, ptr, len)
} else {
Err(Errno::NotImplemented)
}
}
/// Returns `true` if the node is ready for operation
pub fn ready(self: &VnodeRef, write: bool) -> Result<bool, Errno> {
match &self.data {
VnodeData::File(file) => file
.borrow_mut()
.as_mut()
.ok_or(Errno::NotImplemented)?
.ready(self.clone(), write),
_ => Err(Errno::InvalidOperation),
pub fn is_ready(self: &VnodeRef, write: bool) -> Result<bool, Errno> {
if let Some(ref mut data) = *self.data() {
data.is_ready(self.clone(), write)
} else {
Err(Errno::NotImplemented)
}
}
@@ -564,6 +500,7 @@ impl Vnode {
if access.intersects(AccessMode::R_OK | AccessMode::W_OK | AccessMode::X_OK) {
return Err(Errno::InvalidArgument);
}
return Ok(());
} else {
if access.contains(AccessMode::F_OK) {
return Err(Errno::InvalidArgument);
@@ -582,9 +519,9 @@ impl Vnode {
// TODO check group
// TODO check other
}
Ok(())
return Ok(());
}
}
}
@@ -601,80 +538,32 @@ mod tests {
use libsys::{ioctl::IoctlCmd, stat::OpenFlags, stat::Stat};
pub struct DummyInode;
// TODO derive macro for this
impl VnodeCommon for DummyInode {
/// Performs filetype-specific request
fn ioctl(
&mut self,
node: VnodeRef,
cmd: IoctlCmd,
ptr: usize,
len: usize,
) -> Result<usize, Errno> {
todo!()
}
/// Retrieves file status
fn stat(&mut self, node: VnodeRef) -> Result<Stat, Errno> {
todo!()
}
/// Reports the size of this filesystem object in bytes
fn size(&mut self, node: VnodeRef) -> Result<usize, Errno> {
todo!()
}
/// Returns `true` if node is ready for an operation
fn ready(&mut self, node: VnodeRef, write: bool) -> Result<bool, Errno> {
todo!()
}
fn open(&mut self, _node: VnodeRef, _flags: OpenFlags) -> Result<usize, Errno> {
Ok(0)
}
fn close(&mut self, _node: VnodeRef) -> Result<(), Errno> {
Ok(())
}
}
impl VnodeDirectory for DummyInode {
#[auto_inode]
impl VnodeImpl for DummyInode {
fn create(
&mut self,
at: VnodeRef,
_at: VnodeRef,
name: &str,
kind: VnodeCreateKind,
kind: VnodeKind,
) -> Result<VnodeRef, Errno> {
let data = match kind {
VnodeCreateKind::Directory => {
VnodeData::Directory(RefCell::new(Some(Box::new(DummyInode {}))))
}
_ => todo!(),
};
Ok(Vnode::new(name, data, 0))
}
fn remove(&mut self, at: VnodeRef, name: &str) -> Result<(), Errno> {
Ok(())
}
fn lookup(&mut self, at: VnodeRef, name: &str) -> Result<VnodeRef, Errno> {
Err(Errno::DoesNotExist)
let node = Vnode::new(name, kind, 0);
node.set_data(Box::new(DummyInode {}));
Ok(node)
}
/// Read directory entries into target buffer
fn readdir(
&mut self,
node: VnodeRef,
pos: usize,
data: &mut [DirectoryEntry],
) -> Result<usize, Errno> {
todo!()
fn remove(&mut self, _at: VnodeRef, _name: &str) -> Result<(), Errno> {
Ok(())
}
fn lookup(&mut self, _at: VnodeRef, _name: &str) -> Result<VnodeRef, Errno> {
Err(Errno::DoesNotExist)
}
}
#[test]
fn test_parent() {
let root = Vnode::new("", VnodeData::Directory(RefCell::new(None)), 0);
let node = Vnode::new("dir0", VnodeData::Directory(RefCell::new(None)), 0);
let root = Vnode::new("", VnodeKind::Directory, 0);
let node = Vnode::new("dir0", VnodeKind::Directory, 0);
root.attach(node.clone());
@@ -684,27 +573,23 @@ mod tests {
#[test]
fn test_mkdir_unlink() {
let root = Vnode::new(
"",
VnodeData::Directory(RefCell::new(Some(Box::new(DummyInode {})))),
0,
);
let root = Vnode::new("", VnodeKind::Directory, 0);
root.set_data(Box::new(DummyInode {}));
let node = root
.create("test", FileMode::default_dir(), VnodeCreateKind::Directory)
.create("test", FileMode::default_dir(), VnodeKind::Directory)
.unwrap();
assert_eq!(
root.create("test", FileMode::default_dir(), VnodeCreateKind::Directory)
root.create("test", FileMode::default_dir(), VnodeKind::Directory)
.unwrap_err(),
Errno::AlreadyExists
);
assert_eq!(node.props.borrow().mode, FileMode::default_dir());
assert!(Rc::ptr_eq(&node, &root.lookup("test").unwrap()));
let inner = node.as_directory().unwrap();
assert!(matches!(*inner.borrow_mut(), Some(_)));
// assert!(node.data.borrow().is_some());
assert!(node.data.borrow().is_some());
root.unlink("test").unwrap();
@@ -713,9 +598,9 @@ mod tests {
#[test]
fn test_lookup_attach_detach() {
let root = Vnode::new("", VnodeData::Directory(RefCell::new(None)), 0);
let dir0 = Vnode::new("dir0", VnodeData::Directory(RefCell::new(None)), 0);
let dir1 = Vnode::new("dir1", VnodeData::Directory(RefCell::new(None)), 0);
let root = Vnode::new("", VnodeKind::Directory, 0);
let dir0 = Vnode::new("dir0", VnodeKind::Directory, 0);
let dir1 = Vnode::new("dir1", VnodeKind::Directory, 0);
root.attach(dir0.clone());
root.attach(dir1.clone());
+17
View File
@@ -0,0 +1,17 @@
#![feature(asm)]
#![no_std]
#![no_main]
#[macro_use]
extern crate libusr;
#[no_mangle]
fn main() -> i32 {
loop {
trace!("Hello from userspace");
for _ in 0..100000 {
unsafe { asm!("nop"); }
}
}
123
}
+2 -6
View File
@@ -15,16 +15,12 @@ memfs = { path = "../fs/memfs" }
libsys = { path = "../libsys" }
cfg-if = "1.x.x"
tock-registers = "0.7.x"
fdt-rs = { version = "0.x.x", default-features = false }
bitflags = "^1.3.0"
kernel-macros = { path = "macros" }
fs-macros = { path = "../fs/macros" }
[target.'cfg(target_arch = "x86_64")'.dependencies]
multiboot2 = { git = "https://github.com/alnyan/multiboot2", branch = "expose-extra-traits-for-iters" }
[target.'cfg(target_arch = "aarch64")'.dependencies]
cortex-a = { version = "7.0.x" }
fdt-rs = { version = "0.x.x", default-features = false }
cortex-a = { version = "6.x.x" }
[features]
default = ["aggressive_syscall"]
+12
View File
@@ -0,0 +1,12 @@
use std::env;
fn main() -> Result<(), i32> {
let arch = env::var("ARCH").expect("$ARCH is not set");
let mach = if arch == "aarch64" {
env::var("MACH").expect("$MACH is not set")
} else {
"none".to_owned()
};
println!("cargo:rerun-if-changed=../etc/{}-{}.ld", arch, mach);
Ok(())
}
+16
View File
@@ -0,0 +1,16 @@
.macro MOV_L reg, value
mov \reg, #((\value) & 0xFFFF)
movk \reg, #((\value) >> 16), lsl #16
.endm
.macro ADR_REL reg, sym
adrp \reg, \sym
add \reg, \reg, #:lo12:\sym
.endm
.macro ADR_ABS reg, sym
movz \reg, #:abs_g3:\sym
movk \reg, #:abs_g2_nc:\sym
movk \reg, #:abs_g1_nc:\sym
movk \reg, #:abs_g0_nc:\sym
.endm
+51 -24
View File
@@ -4,15 +4,16 @@ use crate::arch::{
aarch64::reg::{CNTKCTL_EL1, CPACR_EL1},
machine,
};
use crate::config::{ConfigKey, CONFIG};
use crate::dev::pseudo;
use crate::dev::{
fdt::{find_prop, DeviceTree},
irq::IntSource,
Device,
use crate::arch::{
aarch64::{
cpu,
smp,
},
};
use crate::fs::{devfs, sysfs};
use core::arch::global_asm;
use crate::config::{ConfigKey, CONFIG};
use crate::dev::fdt::find_prop;
use crate::dev::{fdt::DeviceTree, irq::IntSource, Device};
use crate::fs::devfs;
use libsys::error::Errno;
//use crate::debug::Level;
use crate::mem::{
@@ -22,8 +23,8 @@ use crate::mem::{
};
use crate::proc;
use cortex_a::asm::barrier::{self, dsb, isb};
use cortex_a::registers::{SCTLR_EL1, VBAR_EL1};
use tock_registers::interfaces::{ReadWriteable, Writeable};
use cortex_a::registers::{MPIDR_EL1, SCTLR_EL1, VBAR_EL1};
use tock_registers::interfaces::{ReadWriteable, Readable, Writeable};
fn init_device_tree(fdt_base_phys: usize) -> Result<Option<DeviceTree>, Errno> {
use fdt_rs::prelude::*;
@@ -35,6 +36,12 @@ fn init_device_tree(fdt_base_phys: usize) -> Result<Option<DeviceTree>, Errno> {
return Ok(None);
};
#[cfg(feature = "verbose")]
{
use crate::debug::Level;
fdt.dump(Level::Debug);
}
let mut cfg = CONFIG.lock();
if let Some(chosen) = fdt.node_by_path("/chosen") {
@@ -55,8 +62,7 @@ fn init_device_tree(fdt_base_phys: usize) -> Result<Option<DeviceTree>, Errno> {
Ok(Some(fdt))
}
#[no_mangle]
extern "C" fn __aa64_bsp_main(fdt_base: usize) -> ! {
fn cpu_setup_common() {
// Disable FP instruction trapping
CPACR_EL1.modify(CPACR_EL1::FPEN::TrapNone);
@@ -80,10 +86,34 @@ extern "C" fn __aa64_bsp_main(fdt_base: usize) -> ! {
isb(barrier::SY);
}
unsafe {
cpu::init_self();
}
}
#[no_mangle]
extern "C" fn __aa64_secondary_main() -> ! {
cpu_setup_common();
unsafe {
use crate::dev::irq::IntController;
machine::local_timer().enable().unwrap();
machine::intc().enable_secondary();
machine::intc().enable_irq(machine::IrqNumber::new(30));
proc::enter(false);
}
}
#[no_mangle]
extern "C" fn __aa64_bsp_main(fdt_base: usize) -> ! {
// Boot CPU is MPDIR_EL1 = 0
cpu_setup_common();
// Enable MMU
virt::enable().expect("Failed to initialize virtual memory");
let _fdt = init_device_tree(fdt_base).expect("Device tree init failed");
let fdt = init_device_tree(fdt_base).expect("Device tree init failed");
// Most basic machine init: initialize proper debug output
// physical memory
@@ -98,18 +128,13 @@ extern "C" fn __aa64_bsp_main(fdt_base: usize) -> ! {
}
devfs::init();
sysfs::init();
machine::init_board().unwrap();
// #[cfg(feature = "verbose")]
// if let Some(fdt) = fdt {
// use crate::debug::Level;
// fdt.dump(Level::Debug);
// }
devfs::add_named_char_device(&pseudo::ZERO, "zero").unwrap();
devfs::add_named_char_device(&pseudo::RANDOM, "random").unwrap();
if let Some(fdt) = &fdt {
unsafe {
smp::enable_secondary_cpus(fdt);
}
}
infoln!("Machine init finished");
@@ -117,8 +142,10 @@ extern "C" fn __aa64_bsp_main(fdt_base: usize) -> ! {
machine::local_timer().enable().unwrap();
machine::local_timer().init_irqs().unwrap();
proc::enter();
proc::enter(true);
}
}
global_asm!(include_str!("macros.S"));
global_asm!(include_str!("uboot.S"));
global_asm!(include_str!("upper.S"));
+33 -126
View File
@@ -1,54 +1,5 @@
// vi:ft=a64asm.asm:
.macro MOV_L reg, value
mov \reg, #((\value) & 0xFFFF)
movk \reg, #((\value) >> 16), lsl #16
.endm
.macro ADR_REL reg, sym
adrp \reg, \sym
add \reg, \reg, #:lo12:\sym
.endm
.macro ADR_ABS reg, sym
movz \reg, #:abs_g3:\sym
movk \reg, #:abs_g2_nc:\sym
movk \reg, #:abs_g1_nc:\sym
movk \reg, #:abs_g0_nc:\sym
.endm
.set PTE_BLOCK_AF, 1 << 10
.set PTE_BLOCK_ISH, 3 << 8
.set PTE_PRESENT, 1 << 0
.set MAIR_EL1_Attr0_Normal_Inner_NC, (4 << 0)
.set MAIR_EL1_Attr0_Normal_Outer_NC, (4 << 4)
.set MAIR_EL1_Attr1_Device, (0 << 12)
.set MAIR_EL1_Attr1_Device_nGnRE, (1 << 8)
.set ID_AA64MMFR0_EL1_TGran4, (0xF << 28)
.set TCR_EL1_IPS_SHIFT, 32
.set TCR_EL1_TG1_4K, (2 << 30)
.set TCR_EL1_SH1_Outer, (2 << 28)
.set TCR_EL1_ORGN1_NC, (0 << 26)
.set TCR_EL1_IRGN1_NC, (0 << 24)
.set TCR_EL1_T1SZ_SHIFT, 16
.set TCR_EL1_TG0_4K, (0 << 14)
.set TCR_EL1_SH0_Outer, (2 << 12)
.set TCR_EL1_ORGN0_NC, (0 << 10)
.set TCR_EL1_IRGN0_NC, (0 << 8)
.set TCR_EL1_T0SZ_SHIFT, 0
.set TCR_EL1_ATTRS, (TCR_EL1_TG1_4K | TCR_EL1_SH1_Outer | TCR_EL1_TG0_4K | TCR_EL1_SH0_Outer | (25 << TCR_EL1_T1SZ_SHIFT) | (25 << TCR_EL1_T0SZ_SHIFT))
.set SCTLR_EL1_I, (1 << 12)
.set SCTLR_EL1_C, (1 << 2)
.set SCTLR_EL1_M, (1 << 0)
.set SCTLR_EL2_RES1, 0x30C50830
.set SPSR_EL2_EL1h, 0x5
@@ -64,6 +15,22 @@
_entry:
mov x8, x0
// Zero .bss
ADR_ABS x0, __bss_start_phys
ADR_ABS x1, __bss_end_phys
1:
cmp x0, x1
beq 2f
str xzr, [x0], #8
b 1b
2:
ADR_ABS x9, __aa64_entry_upper
ADR_REL x10, __aa64_enter_upper
_entry_common:
// Test for EL2
mrs x0, CurrentEL
lsr x0, x0, #2
@@ -95,94 +62,34 @@ _entry:
dsb sy
isb
// Zero .bss
ADR_ABS x0, __bss_start_phys
ADR_ABS x1, __bss_end_phys
1:
cmp x0, x1
beq 2f
str xzr, [x0], #8
b 1b
2:
ADR_ABS x9, __aa64_entry_upper
b __aa64_enter_upper
.global __aa64_enter_upper
.type __aa64_enter_upper, %function
__aa64_enter_upper:
// x8 -- FDT base
// x9 -- upper entry point
// Setup TTBR1_EL1
// TODO fix macros
ADR_ABS x5, KERNEL_TTBR1
ADR_ABS x6, KERNEL_OFFSET
// x5 = KERNEL_TTBR1 physical address
sub x5, x5, x6
// Fill KERNEL_TTBR1 table with upper-mapped Normal memory
.fill_ttbr1:
mov x2, #256
1:
sub x2, x2, #1
// x0 = (x2 << 30) | attrs...
lsl x1, x2, #30
mov x0, #(PTE_BLOCK_ISH | PTE_BLOCK_AF | PTE_PRESENT)
orr x0, x0, x1
str x0, [x5, x2, lsl #3]
cbnz x2, 1b
.init_mmu_regs:
mov x0, #(MAIR_EL1_Attr0_Normal_Outer_NC | MAIR_EL1_Attr0_Normal_Inner_NC | MAIR_EL1_Attr1_Device | MAIR_EL1_Attr1_Device_nGnRE)
msr mair_el1, x0
// Test for 4KiB page support
mrs x0, ID_AA64MMFR0_EL1
mov x1, ID_AA64MMFR0_EL1_TGran4
tst x0, x1
bne .no_4k_gran
// x0 = PARange
and x0, x0, #0xF
lsl x0, x0, #TCR_EL1_IPS_SHIFT
MOV_L x1, TCR_EL1_ATTRS
orr x0, x0, x1
msr tcr_el1, x0
msr ttbr0_el1, x5
msr ttbr1_el1, x5
dsb ish
isb
mrs x0, sctlr_el1
orr x0, x0, #SCTLR_EL1_M
msr sctlr_el1, x0
mov x0, x8
br x9
.no_4k_gran:
b .
.size __aa64_enter_upper, . - __aa64_enter_upper
br x10
.section .text._entry_upper
__aa64_entry_upper:
// x0 -- fdt address
ADR_REL x1, bsp_stack_top
ADR_ABS x1, bsp_stack_top
mov sp, x1
mov lr, xzr
bl __aa64_bsp_main
b .
__aa64_entry_upper_secondary:
// x0 -- stack
mov sp, x0
mov lr, xzr
bl __aa64_secondary_main
b .
.section .text._entry_secondary
.global _entry_secondary
_entry_secondary:
mov x8, x0
ADR_ABS x9, __aa64_entry_upper_secondary
ADR_ABS x10, __aa64_enter_upper_secondary
b _entry_common
.section .bss
.p2align 12
bsp_stack_bottom:
+101
View File
@@ -0,0 +1,101 @@
// vi:ft=a64asm:
.set PTE_BLOCK_AF, 1 << 10
.set PTE_BLOCK_ISH, 3 << 8
.set PTE_PRESENT, 1 << 0
.set MAIR_EL1_Attr0_Normal_Inner_NC, (4 << 0)
.set MAIR_EL1_Attr0_Normal_Outer_NC, (4 << 4)
.set MAIR_EL1_Attr1_Device, (0 << 12)
.set MAIR_EL1_Attr1_Device_nGnRE, (1 << 8)
.set ID_AA64MMFR0_EL1_TGran4, (0xF << 28)
.set TCR_EL1_IPS_SHIFT, 32
.set TCR_EL1_TG1_4K, (2 << 30)
.set TCR_EL1_SH1_Outer, (2 << 28)
.set TCR_EL1_ORGN1_NC, (0 << 26)
.set TCR_EL1_IRGN1_NC, (0 << 24)
.set TCR_EL1_T1SZ_SHIFT, 16
.set TCR_EL1_TG0_4K, (0 << 14)
.set TCR_EL1_SH0_Outer, (2 << 12)
.set TCR_EL1_ORGN0_NC, (0 << 10)
.set TCR_EL1_IRGN0_NC, (0 << 8)
.set TCR_EL1_T0SZ_SHIFT, 0
.set TCR_EL1_ATTRS, (TCR_EL1_TG1_4K | TCR_EL1_SH1_Outer | TCR_EL1_TG0_4K | TCR_EL1_SH0_Outer | (25 << TCR_EL1_T1SZ_SHIFT) | (25 << TCR_EL1_T0SZ_SHIFT))
.set SCTLR_EL1_I, (1 << 12)
.set SCTLR_EL1_C, (1 << 2)
.set SCTLR_EL1_M, (1 << 0)
.section .text._entry
.global __aa64_enter_upper
.type __aa64_enter_upper, %function
__aa64_enter_upper:
// x8 -- FDT base
// x9 -- upper entry point
// Setup TTBR1_EL1
// TODO fix macros
ADR_ABS x5, KERNEL_TTBR1
ADR_ABS x6, KERNEL_OFFSET
// x5 = KERNEL_TTBR1 physical address
sub x5, x5, x6
// Fill KERNEL_TTBR1 table with upper-mapped Normal memory
.fill_ttbr1:
mov x2, #256
1:
sub x2, x2, #1
// x0 = (x2 << 30) | attrs...
lsl x1, x2, #30
mov x0, #(PTE_BLOCK_ISH | PTE_BLOCK_AF | PTE_PRESENT)
orr x0, x0, x1
str x0, [x5, x2, lsl #3]
cbnz x2, 1b
__aa64_enter_upper_secondary:
ADR_ABS x5, KERNEL_TTBR1
ADR_ABS x6, KERNEL_OFFSET
// x5 = KERNEL_TTBR1 physical address
sub x5, x5, x6
mov x0, #(MAIR_EL1_Attr0_Normal_Outer_NC | MAIR_EL1_Attr0_Normal_Inner_NC | MAIR_EL1_Attr1_Device | MAIR_EL1_Attr1_Device_nGnRE)
msr mair_el1, x0
// Test for 4KiB page support
mrs x0, ID_AA64MMFR0_EL1
mov x1, ID_AA64MMFR0_EL1_TGran4
tst x0, x1
bne .no_4k_gran
// x0 = PARange
and x0, x0, #0xF
lsl x0, x0, #TCR_EL1_IPS_SHIFT
MOV_L x1, TCR_EL1_ATTRS
orr x0, x0, x1
msr tcr_el1, x0
msr ttbr0_el1, x5
msr ttbr1_el1, x5
dsb ish
isb
mrs x0, sctlr_el1
orr x0, x0, #SCTLR_EL1_M
msr sctlr_el1, x0
mov x0, x8
br x9
.no_4k_gran:
b .
.size __aa64_enter_upper, . - __aa64_enter_upper
+17 -24
View File
@@ -5,7 +5,6 @@ use crate::mem::{
self,
phys::{self, PageUsage},
};
use core::arch::global_asm;
use core::mem::size_of;
struct Stack {
@@ -68,7 +67,7 @@ impl Context {
stack.push(frame.sp_el0 as usize);
// Setup common
stack.push(0); // tpidr_el0
stack.push(0); // tpidr_el0
stack.push(ttbr0);
stack.push(__aa64_ctx_enter_from_fork as usize); // x30/lr
stack.push(frame.x[29]); // x29
@@ -116,7 +115,7 @@ impl Context {
Self {
k_sp: stack.sp,
stack_base: stack.bp,
stack_page_count: 8,
stack_page_count: 8
}
}
@@ -125,13 +124,7 @@ impl Context {
/// # Safety
///
/// Unsafe: may clobber an already active context
pub unsafe fn setup_signal_entry(
&mut self,
entry: usize,
arg: usize,
ttbr0: usize,
ustack: usize,
) {
pub unsafe fn setup_signal_entry(&mut self, entry: usize, arg: usize, ttbr0: usize, ustack: usize) {
let mut stack = Stack::from_base_size(self.stack_base, self.stack_page_count);
stack.push(entry);
@@ -178,25 +171,25 @@ impl Stack {
pub unsafe fn from_base_size(bp: usize, page_count: usize) -> Stack {
Stack {
bp,
sp: bp + page_count * mem::PAGE_SIZE,
sp: bp + page_count * mem::PAGE_SIZE
}
}
pub fn setup_common(&mut self, entry: usize, ttbr: usize) {
self.push(0); // tpidr_el0
self.push(0); // tpidr_el0
self.push(ttbr);
self.push(entry); // x30/lr
self.push(0); // x29
self.push(0); // x28
self.push(0); // x27
self.push(0); // x26
self.push(0); // x25
self.push(0); // x24
self.push(0); // x23
self.push(0); // x22
self.push(0); // x21
self.push(0); // x20
self.push(0); // x19
self.push(entry); // x30/lr
self.push(0); // x29
self.push(0); // x28
self.push(0); // x27
self.push(0); // x26
self.push(0); // x25
self.push(0); // x24
self.push(0); // x23
self.push(0); // x22
self.push(0); // x21
self.push(0); // x20
self.push(0); // x19
}
pub fn push(&mut self, value: usize) {
+72
View File
@@ -0,0 +1,72 @@
#![allow(missing_docs)]
use crate::proc::Scheduler;
use crate::util::InitOnce;
use core::mem::MaybeUninit;
use core::ptr::null_mut;
use core::sync::atomic::{AtomicUsize, Ordering};
use cortex_a::registers::{MPIDR_EL1, TPIDR_EL1};
use tock_registers::interfaces::{Readable, Writeable};
#[repr(C)]
pub struct Cpu {
counter: AtomicUsize, // 0x08
id: usize,
scheduler: Scheduler,
}
impl Cpu {
pub fn new(id: usize) -> Self {
Self {
counter: AtomicUsize::new(0),
id,
scheduler: Scheduler::new(),
}
}
pub fn id(&self) -> usize {
self.id
}
pub fn scheduler(&mut self) -> &Scheduler {
&self.scheduler
}
pub unsafe fn set(&mut self) {
TPIDR_EL1.set(self as *mut _ as u64);
}
pub unsafe fn get() -> &'static mut Self {
&mut *(TPIDR_EL1.get() as *mut Self)
}
}
pub unsafe fn cpus() -> impl Iterator<Item = &'static mut Cpu> {
CPUS[..CPU_COUNT.load(Ordering::Acquire)]
.iter_mut()
.map(|c| c.assume_init_mut())
}
pub unsafe fn by_index(idx: usize) -> &'static mut Cpu {
assert!(idx < CPU_COUNT.load(Ordering::Acquire));
CPUS[idx].assume_init_mut()
}
pub fn count() -> usize {
CPU_COUNT.load(Ordering::Acquire)
}
static CPU_COUNT: AtomicUsize = AtomicUsize::new(0);
static mut CPUS: [MaybeUninit<Cpu>; 8] = MaybeUninit::uninit_array();
pub unsafe fn init_self() {
let cpu_index = CPU_COUNT.load(Ordering::Acquire);
let mpidr_id = (MPIDR_EL1.get() & 0xF) as usize;
CPUS[cpu_index].write(Cpu::new(mpidr_id));
CPUS[cpu_index].assume_init_mut().set();
CPU_COUNT.store(cpu_index + 1, Ordering::Release);
}
+26 -25
View File
@@ -1,14 +1,13 @@
//! AArch64 exception handling
use crate::arch::{intrin, machine};
use crate::arch::machine;
use crate::debug::Level;
use crate::dev::irq::{IntController, IrqContext};
use crate::mem::{self, virt::table::Space};
use crate::mem;
use crate::proc::{sched, Thread};
use crate::syscall;
use core::arch::global_asm;
use cortex_a::registers::{ESR_EL1, FAR_EL1};
use libsys::{abi::SystemCall, error::Errno, signal::Signal};
use libsys::{abi::SystemCall, signal::Signal};
use tock_registers::interfaces::Readable;
/// Trapped SIMD/FP functionality
@@ -59,7 +58,7 @@ const fn data_abort_access_size(iss: u64) -> &'static str {
#[no_mangle]
extern "C" fn __aa64_exc_irq_handler(_exc: &mut ExceptionFrame) {
unsafe {
let ic = IrqContext::new(0);
let ic = IrqContext::new();
machine::intc().handle_pending_irqs(&ic);
}
}
@@ -91,26 +90,28 @@ extern "C" fn __aa64_exc_sync_handler(exc: &mut ExceptionFrame) {
let far = FAR_EL1.get() as usize;
let iss = esr & 0x1FFFFFF;
if iss & (1 << 6) != 0 && far < mem::KERNEL_OFFSET && sched::is_ready() {
// TODO handle scenarios when sheduler is not yet initialized
if iss & (1 << 6) != 0 && far < mem::KERNEL_OFFSET {
let thread = Thread::current();
let proc = thread.owner().unwrap();
let asid = proc.asid();
let res = proc.manipulate_space(|space| {
space.try_cow_copy(far)?;
unsafe {
intrin::flush_tlb_asid(asid);
}
Result::<(), Errno>::Ok(())
});
if res.is_err() {
if proc
.manipulate_space(|space| space.try_cow_copy(far))
.is_err()
{
// Kill program
errorln!("Data abort from {:#x}", exc.elr_el1);
dump_data_abort(Level::Error, esr, far as u64);
proc.enter_fault_signal(thread, Signal::SegmentationFault);
}
unsafe {
use cortex_a::registers::TTBR0_EL1;
let ttbr = TTBR0_EL1.get() as usize;
let asid = (ttbr >> 48) & 0xFF;
asm!("tlbi aside1, {}", in(reg) (asid << 48));
}
return;
}
@@ -127,7 +128,7 @@ extern "C" fn __aa64_exc_sync_handler(exc: &mut ExceptionFrame) {
if num == SystemCall::Fork {
match unsafe { syscall::sys_fork(exc) } {
Ok(pid) => exc.x[0] = u32::from(pid) as usize,
Ok(pid) => exc.x[0] = pid.value() as usize,
Err(err) => {
exc.x[0] = err.to_negative_isize() as usize;
}
@@ -147,14 +148,14 @@ extern "C" fn __aa64_exc_sync_handler(exc: &mut ExceptionFrame) {
_ => {}
}
if sched::is_ready() {
let thread = Thread::current();
errorln!(
"Unhandled exception in thread {:?}, {:?}",
thread.id(),
thread.owner().map(|e| e.id())
);
}
// if sched::is_ready() {
// let thread = Thread::current();
// errorln!(
// "Unhandled exception in thread {}, {:?}",
// thread.id(),
// thread.owner().map(|e| e.id())
// );
// }
errorln!(
"Unhandled exception at ELR={:#018x}, ESR={:#010x}",
-33
View File
@@ -1,33 +0,0 @@
//! AArch64-specific assembly functions
use core::arch::asm;
/// Disables delievery of IRQs
///
/// # Safety
///
/// Unsafe: requires EL0
#[inline(always)]
pub unsafe fn irq_disable() {
asm!("msr daifset, {bits}", bits = const 2, options(nomem, nostack, preserves_flags));
}
/// Discards an entry related to `addr` from TLB cache
///
/// # Safety
///
/// Unsafe: requires EL0
#[inline(always)]
pub unsafe fn flush_tlb_virt(addr: usize) {
asm!("tlbi vaae1, {}", in(reg) addr);
}
/// Discards all entries related to `asid` from TLB cache
///
/// # Safety
///
/// Only safe to use for known [Process]es and their ASIDs
// TODO non-portable
#[inline(always)]
pub unsafe fn flush_tlb_asid(asid: usize) {
asm!("tlbi aside1, {}", in(reg) asid);
}
+12 -2
View File
@@ -1,7 +1,7 @@
use crate::mem::virt::DeviceMemoryIo;
use crate::sync::IrqSafeSpinLock;
use tock_registers::interfaces::{Readable, Writeable};
use tock_registers::registers::{ReadOnly, ReadWrite};
use tock_registers::registers::{ReadOnly, WriteOnly, ReadWrite};
use tock_registers::{register_bitfields, register_structs};
register_bitfields! {
@@ -31,7 +31,9 @@ register_structs! {
(0x820 => ITARGETSR: [ReadWrite<u32, ITARGETSR::Register>; 248]),
(0xC00 => _res2),
(0xC08 => ICFGR: [ReadWrite<u32>; 62]),
(0xC0C => @END),
(0xD00 => _res3),
(0xF00 => SGIR: WriteOnly<u32>),
(0xF04 => @END),
}
}
@@ -103,6 +105,14 @@ impl Gicd {
}
}
pub fn set_sgir(&self, filter: bool, mask: u32, intid: u32) {
let mut value = (mask << 16) | intid;
if filter {
value |= 1 << 24;
}
self.shared_regs.lock().SGIR.set(value);
}
pub fn enable_irq(&self, irq: super::IrqNumber) {
let irq = irq.get();
+37 -2
View File
@@ -1,7 +1,7 @@
//! ARM Generic Interrupt Controller
use crate::dev::{
irq::{IntController, IntSource, IrqContext},
irq::{IntController, IntSource, IrqContext, IpiSender},
Device,
};
use crate::mem::virt::{DeviceMemory, DeviceMemoryIo};
@@ -17,6 +17,8 @@ use gicd::Gicd;
/// Maximum available IRQ number
pub const MAX_IRQ: usize = 300;
const SGI_IRQ: u32 = 2;
/// Range-checked IRQ number type
#[repr(transparent)]
#[derive(Copy, Clone)]
@@ -61,11 +63,12 @@ impl Device for Gic {
let gicc = Gicc::new(gicc_mmio);
gicd.enable();
gicc.enable();
self.gicd.init(gicd);
self.gicc.init(gicc);
self.enable_secondary();
Ok(())
}
}
@@ -85,9 +88,30 @@ impl IntController for Gic {
return;
}
//<<<<<<< HEAD
if irq_number == 1 {
gicc.clear_irq(irq_number as u32, ic);
debugln!("Received IPI");
loop {}
}
//
// if self.scheduler_irq.0 == irq_number {
// use crate::proc::sched;
// use cortex_a::registers::{CNTP_TVAL_EL0, CNTP_CTL_EL0};
// use tock_registers::interfaces::Writeable;
// use crate::arch::platform::cpu::Cpu;
// gicc.clear_irq(irq_number as u32, ic);
// CNTP_TVAL_EL0.set(1000000);
// CNTP_CTL_EL0.write(CNTP_CTL_EL0::ENABLE::SET);
// sched::switch(false);
// return;
// }
//=======
gicc.clear_irq(irq_number as u32, ic);
//>>>>>>> feat/thread
{
// TODO make timer interrupt a special case and drop table lock
let table = self.table.lock();
match table[irq_number] {
None => panic!("No handler registered for irq{}", irq_number),
@@ -117,7 +141,18 @@ impl IntController for Gic {
}
}
impl IpiSender for Gic {
fn send_to_mask(&self, exclude_self: bool, target: u32, data: u64) {
self.gicd.get().set_sgir(exclude_self, target, 1);
}
}
impl Gic {
///
pub unsafe fn enable_secondary(&self) {
self.gicc.get().enable();
}
/// Constructs an instance of GICv2.
///
/// # Safety
@@ -96,4 +96,4 @@ static UART0: Uart = unsafe { Uart::new(UART0_BASE, IrqNumber::new(32)) };
static LOCAL_TIMER: GenericTimer = GenericTimer::new(LOCAL_TIMER_IRQ);
pub(super) static GPIO: Gpio = unsafe { Gpio::new(PIO_BASE) };
static RTC: Rtc = unsafe { Rtc::new(RTC_BASE, RTC_IRQ) };
static GIC: Gic = unsafe { Gic::new(GICD_BASE, GICC_BASE) };
static GIC: Gic = unsafe { Gic::new(GICD_BASE, GICC_BASE, LOCAL_TIMER_IRQ) };
@@ -1,4 +1,3 @@
use crate::arch::intrin;
use crate::dev::Device;
use crate::mem::virt::DeviceMemoryIo;
use crate::sync::IrqSafeSpinLock;
@@ -73,7 +72,7 @@ impl RWdog {
regs.CTRL.write(CTRL::KEY::Value + CTRL::RESTART::SET);
loop {
intrin::hang();
asm!("wfe");
}
}
+7 -1
View File
@@ -72,7 +72,13 @@ pub fn local_timer() -> &'static GenericTimer {
/// Returns CPU's interrupt controller device
#[inline]
pub fn intc() -> &'static impl IntController<IrqNumber = IrqNumber> {
pub fn intc() -> &'static Gic {
&GIC
}
/// Returns CPU's IPI sender device
#[inline]
pub fn ipi_sender() -> &'static Gic {
&GIC
}
-2
View File
@@ -4,7 +4,6 @@ use crate::dev::{
serial::{pl011::Pl011, SerialDevice},
Device,
};
use crate::fs::devfs::{self, CharDeviceType};
use crate::mem::phys;
use libsys::error::Errno;
@@ -38,7 +37,6 @@ pub fn init_board() -> Result<(), Errno> {
unsafe {
IRQCHIP.enable()?;
UART.init_irqs()?;
devfs::add_char_device(&UART, CharDeviceType::TtySerial)?;
EMMC.enable()?;
}
+3 -5
View File
@@ -5,14 +5,12 @@ use tock_registers::interfaces::{Readable, Writeable};
pub mod boot;
pub mod context;
pub mod cpu;
pub mod exception;
pub mod intrin;
pub mod irq;
pub mod reg;
pub mod smp;
pub mod timer;
pub mod virt;
pub use exception::ExceptionFrame as ForkFrame;
cfg_if! {
if #[cfg(feature = "mach_qemu")] {
@@ -38,7 +36,7 @@ cfg_if! {
#[inline(always)]
pub unsafe fn irq_mask_save() -> u64 {
let state = DAIF.get();
intrin::irq_disable();
asm!("msr daifset, {bits}", bits = const 2, options(nomem, nostack, preserves_flags));
state
}
@@ -0,0 +1,48 @@
//! CNTKCTL_EL1 register
#![allow(missing_docs)]
use tock_registers::{
interfaces::{Readable, Writeable},
register_bitfields,
};
register_bitfields! {
u64,
/// Counter-timer Kernel Control Register
pub CNTKCTL_EL1 [
/// If set, disables CNTPCT and CNTFRQ trapping from EL0
EL0PCTEN OFFSET(0) NUMBITS(1) []
]
}
/// CNTKCTL_EL1 register
pub struct Reg;
impl Readable for Reg {
type T = u64;
type R = CNTKCTL_EL1::Register;
#[inline(always)]
fn get(&self) -> Self::T {
let mut tmp;
unsafe {
asm!("mrs {}, cntkctl_el1", out(reg) tmp);
}
tmp
}
}
impl Writeable for Reg {
type T = u64;
type R = CNTKCTL_EL1::Register;
#[inline(always)]
fn set(&self, value: Self::T) {
unsafe {
asm!("msr cntkctl_el1, {}", in(reg) value);
}
}
}
/// CNTKCTL_EL1 register
pub const CNTKCTL_EL1: Reg = Reg;
+57
View File
@@ -0,0 +1,57 @@
//! CPACR_EL1 register
#![allow(missing_docs)]
use tock_registers::{
interfaces::{Readable, Writeable},
register_bitfields,
};
register_bitfields! {
u64,
/// EL1 Architectural Feature Access Control Register
pub CPACR_EL1 [
/// Enable EL0 and EL1 SIMD/FP accesses to EL1
FPEN OFFSET(20) NUMBITS(2) [
/// Trap both EL0 and EL1
TrapAll = 0,
/// Trap EL0
TrapEl0 = 1,
/// Trap EL1
TrapEl1 = 2,
/// Do not trap any SIMD/FP instructions
TrapNone = 3
]
]
}
/// CPACR_EL1 register
pub struct Reg;
impl Readable for Reg {
type T = u64;
type R = CPACR_EL1::Register;
#[inline(always)]
fn get(&self) -> Self::T {
let mut tmp;
unsafe {
asm!("mrs {}, cpacr_el1", out(reg) tmp);
}
tmp
}
}
impl Writeable for Reg {
type T = u64;
type R = CPACR_EL1::Register;
#[inline(always)]
fn set(&self, value: Self::T) {
unsafe {
asm!("msr cpacr_el1, {}", in(reg) value);
}
}
}
/// CPACR_EL1 register
pub const CPACR_EL1: Reg = Reg;
+4 -65
View File
@@ -1,68 +1,7 @@
//! AArch64 architectural registers
use core::arch::asm;
use tock_registers::{
interfaces::{Readable, Writeable},
register_bitfields,
};
pub mod cpacr_el1;
pub use cpacr_el1::CPACR_EL1;
macro_rules! wrap_msr {
($struct_name:ident, $name:ident, $reg:literal, $fields:tt) => {
#[allow(missing_docs)]
pub struct $struct_name;
register_bitfields! {
u64,
#[allow(missing_docs)]
pub $name $fields
}
impl Readable for $struct_name {
type T = u64;
type R = $name::Register;
#[inline(always)]
fn get(&self) -> Self::T {
let mut value;
unsafe {
asm!(concat!("mrs {}, ", $reg), out(reg) value)
}
value
}
}
impl Writeable for $struct_name {
type T = u64;
type R = $name::Register;
#[inline(always)]
fn set(&self, value: Self::T) {
unsafe {
asm!(concat!("msr ", $reg, ", {}"), in(reg) value);
}
}
}
#[allow(missing_docs)]
pub const $name: $struct_name = $struct_name;
};
}
wrap_msr!(CpacrEl1, CPACR_EL1, "cpacr_el1", [
/// Enable EL0 and EL1 SIMD/FP accesses to EL1
FPEN OFFSET(20) NUMBITS(2) [
/// Trap both EL0 and EL1
TrapAll = 0,
/// Trap EL0
TrapEl0 = 1,
/// Trap EL1
TrapEl1 = 2,
/// Do not trap any SIMD/FP instructions
TrapNone = 3
]
]);
wrap_msr!(CntkctlEl1, CNTKCTL_EL1, "cntkctl_el1", [
/// If set, disables CNTPCT and CNTFRQ trapping from EL0
EL0PCTEN OFFSET(0) NUMBITS(1) []
]);
pub mod cntkctl_el1;
pub use cntkctl_el1::CNTKCTL_EL1;
+123
View File
@@ -0,0 +1,123 @@
#![allow(missing_docs)]
use crate::arch::{aarch64::cpu, machine};
use crate::dev::{
fdt::{self, DeviceTree},
irq::IpiSender,
};
use crate::mem::{
self,
phys::{self, PageUsage},
};
use cortex_a::registers::MPIDR_EL1;
use libsys::error::Errno;
use fdt_rs::prelude::*;
use tock_registers::interfaces::Readable;
pub type NodeAddress = u32;
#[derive(Clone, Copy, Debug)]
pub enum PsciError {
NotSupported,
InvalidParameters,
Denied,
AlreadyOn,
OnPending,
InternalFailure,
NotPresent,
Disabled,
InvalidAddress,
}
struct Psci {
use_smc: bool,
}
impl Psci {
const PSCI_VERSION: usize = 0x84000000;
const PSCI_CPU_OFF: usize = 0x84000002;
const PSCI_CPU_ON: usize = 0xC4000003;
pub const fn new() -> Self {
Self { use_smc: true }
}
unsafe fn call(&self, x0: usize, x1: usize, x2: usize, x3: usize) -> usize {
if self.use_smc {
call_smc(x0, x1, x2, x3)
} else {
todo!()
}
}
pub unsafe fn cpu_on(
&self,
target_cpu: usize,
entry_point_address: usize,
context_id: usize,
) -> Result<(), PsciError> {
wrap_psci_ok(self.call(
Self::PSCI_CPU_ON,
target_cpu,
entry_point_address,
context_id,
))
}
}
const SECONDARY_STACK_PAGES: usize = 4;
unsafe fn call_smc(mut x0: usize, x1: usize, x2: usize, x3: usize) -> usize {
asm!("smc #0", inout("x0") x0, in("x1") x1, in("x2") x2, in("x3") x3);
x0
}
pub unsafe fn send_ipi(exclude_self: bool, target_mask: u32, data: u64) {
machine::ipi_sender().send_to_mask(exclude_self, target_mask, data);
}
fn wrap_psci_ok(a: usize) -> Result<(), PsciError> {
const NOT_SUPPORTED: isize = -1;
const INVALID_PARAMETERS: isize = -2;
const DENIED: isize = -3;
const ALREADY_ON: isize = -4;
match a as isize {
0 => Ok(()),
NOT_SUPPORTED => Err(PsciError::NotSupported),
INVALID_PARAMETERS => Err(PsciError::InvalidParameters),
DENIED => Err(PsciError::Denied),
ALREADY_ON => Err(PsciError::AlreadyOn),
_ => unimplemented!(),
}
}
pub unsafe fn enable_secondary_cpus(dt: &DeviceTree) {
extern "C" {
fn _entry_secondary();
}
let cpus = dt.node_by_path("/cpus").unwrap();
let psci = Psci::new();
for cpu_node in cpus.children() {
let reg = fdt::find_prop(cpu_node, "reg").unwrap().u32(0).unwrap();
if reg == 0 {
continue;
}
infoln!("Enabling cpu{}", reg);
let stack_pages =
phys::alloc_contiguous_pages(PageUsage::Kernel, SECONDARY_STACK_PAGES).unwrap();
let count_old = cpu::count();
psci.cpu_on(
reg as usize,
_entry_secondary as usize - mem::KERNEL_OFFSET,
mem::virtualize(stack_pages + SECONDARY_STACK_PAGES * mem::PAGE_SIZE),
)
.unwrap();
while cpu::count() == count_old {
cortex_a::asm::wfe();
}
debugln!("Done");
}
}
+6 -10
View File
@@ -3,11 +3,9 @@
use crate::arch::machine::{self, IrqNumber};
use crate::dev::{
irq::{IntController, IntSource},
pseudo,
timer::TimestampSource,
Device,
};
use crate::proc;
use core::time::Duration;
use cortex_a::registers::{CNTFRQ_EL0, CNTPCT_EL0, CNTP_CTL_EL0, CNTP_TVAL_EL0};
use libsys::error::Errno;
@@ -36,9 +34,9 @@ impl IntSource for GenericTimer {
fn handle_irq(&self) -> Result<(), Errno> {
CNTP_TVAL_EL0.set(TIMER_TICK);
CNTP_CTL_EL0.write(CNTP_CTL_EL0::ENABLE::SET);
proc::wait::tick();
proc::switch();
pseudo::RANDOM.set_state(CNTPCT_EL0.get() as u32);
use crate::proc::{wait, sched};
wait::tick();
sched::switch(false);
Ok(())
}
@@ -52,11 +50,9 @@ impl IntSource for GenericTimer {
impl TimestampSource for GenericTimer {
fn timestamp(&self) -> Result<Duration, Errno> {
let cnt = (CNTPCT_EL0.get() as u128) * 1_000_000_000u128;
let frq = CNTFRQ_EL0.get() as u128;
let secs = ((cnt / frq) / 1_000_000_000) as u64;
let nanos = ((cnt / frq) % 1_000_000_000) as u32;
Ok(Duration::new(secs, nanos))
let cnt = CNTPCT_EL0.get() * 1_000_000_000;
let frq = CNTFRQ_EL0.get();
Ok(Duration::from_nanos(cnt / frq))
}
}
+10
View File
@@ -74,8 +74,18 @@ __aa\bits\()_el\el\ht\()_\kind:
EXC_SAVE_STATE
mov x0, sp
.if \el == 0
sub sp, sp, #16
stp fp, lr, [sp, #0]
.else
mov lr, xzr
.endif
bl __aa64_exc_\kind\()_handler
.if \el == 0
add sp, sp, #16
.endif
EXC_RESTORE_STATE
eret
.endm
-102
View File
@@ -1,102 +0,0 @@
//! AArch64 virtual memory management implementation
use crate::mem::virt::table::MapAttributes;
use cortex_a::{
asm::barrier::{self, isb, dsb},
registers::TTBR0_EL1
};
use tock_registers::interfaces::Writeable;
mod fixed;
mod table;
pub use fixed::{init_device_map, map_device_memory};
pub use table::{EntryImpl, SpaceImpl};
bitflags! {
/// Raw attributes for AArch64 [Entry] implementation
pub struct RawAttributesImpl: u64 {
// TODO use 2 lower bits to determine mapping size?
/// nG bit -- determines whether a TLB entry associated with this mapping
/// applies only to current ASID or all ASIDs.
const NOT_GLOBAL = 1 << 11;
/// AF bit -- must be set by software, otherwise Access Error exception is
/// generated when the page is accessed
const ACCESS = 1 << 10;
/// The memory region is outer-shareable
const SH_OUTER = 2 << 8;
/// This page is used for device-MMIO mapping and uses MAIR attribute #1
const DEVICE = 1 << 2;
/// Pages marked with this bit are Copy-on-Write
const EX_COW = 1 << 55;
/// UXN bit -- if set, page may not be used for instruction fetching from EL0
const UXN = 1 << 54;
/// PXN bit -- if set, page may not be used for instruction fetching from EL1
const PXN = 1 << 53;
// AP field
// Default behavior is: read-write for EL1, no access for EL0
/// If set, the page referred to by this entry is read-only for both EL0/EL1
const AP_BOTH_READONLY = 3 << 6;
/// If set, the page referred to by this entry is read-write for both EL0/EL1
const AP_BOTH_READWRITE = 1 << 6;
}
}
impl From<MapAttributes> for RawAttributesImpl {
fn from(src: MapAttributes) -> Self {
let mut res = RawAttributesImpl::empty();
if src.contains(MapAttributes::SHARE_OUTER) {
res |= RawAttributesImpl::SH_OUTER;
}
if !src.contains(MapAttributes::GLOBAL) {
res |= RawAttributesImpl::NOT_GLOBAL;
}
if !src.contains(MapAttributes::USER_EXEC) {
res |= RawAttributesImpl::UXN;
}
if !src.contains(MapAttributes::KERNEL_EXEC) {
res |= RawAttributesImpl::PXN;
}
if src.contains(MapAttributes::USER_READ) {
if src.contains(MapAttributes::USER_WRITE) {
res |= RawAttributesImpl::AP_BOTH_READWRITE;
} else {
res |= RawAttributesImpl::AP_BOTH_READONLY;
}
}
if src.contains(MapAttributes::DEVICE_MEMORY) {
res |= RawAttributesImpl::DEVICE;
}
if src.contains(MapAttributes::ACCESS) {
res |= RawAttributesImpl::ACCESS;
}
res
}
}
/// Performs initialization of virtual memory control by kernel
///
/// # Safety
///
/// Only safe to be called once during virtual memory init.
pub unsafe fn enable() {
fixed::init_device_map();
dsb(barrier::ISH);
isb(barrier::SY);
// Disable lower-half translation
TTBR0_EL1.set(0);
//TCR_EL1.modify(TCR_EL1::EPD0::SET);
}
-300
View File
@@ -1,300 +0,0 @@
use crate::arch::aarch64::intrin::flush_tlb_virt;
use crate::mem::{
self,
phys::{self, PageUsage},
virt::table::{Entry, MapAttributes, Space},
};
use core::ops::{Index, IndexMut};
use libsys::{error::Errno, mem::memset};
use super::RawAttributesImpl;
/// Transparent wrapper structure representing a single
/// translation table entry
#[derive(Clone, Copy)]
#[repr(transparent)]
pub struct EntryImpl(u64);
/// Structure describing a single level of translation mappings
#[repr(C, align(0x1000))]
pub struct TableImpl {
entries: [EntryImpl; 512],
}
/// Top-level translation table wrapper
#[repr(transparent)]
pub struct SpaceImpl(TableImpl);
impl EntryImpl {
const PRESENT: u64 = 1 << 0;
const TABLE: u64 = 1 << 1;
const PHYS_MASK: u64 = 0x0000FFFFFFFFF000;
}
impl Entry for EntryImpl {
type RawAttributes = RawAttributesImpl;
const EMPTY: Self = Self(0);
#[inline]
fn normal(addr: usize, attrs: MapAttributes) -> Self {
Self((addr as u64) | RawAttributesImpl::from(attrs).bits() | (1 << 1) | (1 << 0))
}
#[inline]
fn block(addr: usize, attrs: MapAttributes) -> Self {
Self((addr as u64) | RawAttributesImpl::from(attrs).bits() | (1 << 0))
}
#[inline]
fn address(self) -> usize {
(self.0 & Self::PHYS_MASK) as usize
}
#[inline]
fn set_address(&mut self, virt: usize) {
self.0 = (self.0 & !Self::PHYS_MASK) | ((virt as u64) & Self::PHYS_MASK);
}
#[inline]
fn is_present(self) -> bool {
self.0 & Self::PRESENT != 0
}
#[inline]
fn is_normal(self) -> bool {
self.0 & Self::TABLE != 0
}
#[inline]
fn fork_with_cow(&mut self) -> Self {
self.0 |= (RawAttributesImpl::AP_BOTH_READONLY | RawAttributesImpl::EX_COW).bits();
*self
}
#[inline]
fn copy_from_cow(self, new_addr: usize) -> Self {
let attrs = self.0
& !(Self::PHYS_MASK
| RawAttributesImpl::AP_BOTH_READONLY.bits()
| RawAttributesImpl::EX_COW.bits());
Self(
((new_addr as u64) & Self::PHYS_MASK)
| (attrs | RawAttributesImpl::AP_BOTH_READWRITE.bits()),
)
}
#[inline]
fn is_cow(self) -> bool {
self.0 & RawAttributesImpl::EX_COW.bits() != 0
}
#[inline]
fn is_user_writable(self) -> bool {
self.0 & RawAttributesImpl::AP_BOTH_READONLY.bits()
== RawAttributesImpl::AP_BOTH_READWRITE.bits()
}
}
impl Space for SpaceImpl {
type Entry = EntryImpl;
fn alloc_empty() -> Result<&'static mut Self, Errno> {
let phys = phys::alloc_page(PageUsage::Paging)?;
let res = unsafe { &mut *(mem::virtualize(phys) as *mut Self) };
res.0.entries.fill(EntryImpl::EMPTY);
Ok(res)
}
unsafe fn release(space: &'static mut Self) {
for l0i in 0..512 {
let l0_entry = space.0[l0i];
if !l0_entry.is_present() {
continue;
}
assert!(l0_entry.is_normal());
let l1_table = &mut *(mem::virtualize(l0_entry.address()) as *mut TableImpl);
for l1i in 0..512 {
let l1_entry = l1_table[l1i];
if !l1_entry.is_present() {
continue;
}
assert!(l1_entry.is_normal());
let l2_table = &mut *(mem::virtualize(l1_entry.address()) as *mut TableImpl);
for l2i in 0..512 {
let entry = l2_table[l2i];
if !entry.is_present() {
continue;
}
assert!(entry.is_normal());
phys::free_page(entry.address()).unwrap();
}
phys::free_page(l1_entry.address()).unwrap();
}
phys::free_page(l0_entry.address()).unwrap();
}
memset(space as *mut Self as *mut u8, 0, 4096);
}
fn fork(&mut self) -> Result<&'static mut Self, Errno> {
let res = Self::alloc_empty()?;
for l0i in 0..512 {
if let Some(l1_table) = self.0.next_level_table_mut(l0i) {
for l1i in 0..512 {
if let Some(l2_table) = l1_table.next_level_table_mut(l1i) {
for l2i in 0..512 {
let entry = &mut l2_table[l2i];
if !entry.is_present() {
continue;
}
assert!(entry.is_normal());
let src_phys = entry.address();
let virt_addr = (l0i << 30) | (l1i << 21) | (l2i << 12);
let dst_phys = unsafe { phys::fork_page(src_phys)? };
let new_entry = if dst_phys != src_phys {
todo!()
} else if entry.is_user_writable() {
entry.fork_with_cow()
} else {
*entry
};
unsafe {
flush_tlb_virt(virt_addr);
res.write_last_level(virt_addr, new_entry, true, false)?;
}
}
}
}
}
}
Ok(res)
}
unsafe fn write_last_level(
&mut self,
virt: usize,
entry: Self::Entry,
_create_intermediate: bool, // TODO handle this properly
overwrite: bool,
) -> Result<(), Errno> {
let l0i = virt >> 30;
let l1i = (virt >> 21) & 0x1FF;
let l2i = (virt >> 12) & 0x1FF;
let l1_table = self.0.next_level_table_or_alloc(l0i)?;
let l2_table = l1_table.next_level_table_or_alloc(l1i)?;
if l2_table[l2i].is_present() && !overwrite {
return Err(Errno::AlreadyExists);
};
l2_table[l2i] = entry;
#[cfg(feature = "verbose")]
debugln!(
"{:#p} Map {:#x} -> {:#x}, {:#x}",
self,
virt,
entry.address(),
entry.0 & !EntryImpl::PHYS_MASK
);
flush_tlb_virt(virt);
Ok(())
}
fn read_last_level(&self, virt: usize) -> Result<Self::Entry, Errno> {
let l0i = virt >> 30;
let l1i = (virt >> 21) & 0x1FF;
let l2i = (virt >> 12) & 0x1FF;
let l1_table = self.0.next_level_table(l0i).ok_or(Errno::DoesNotExist)?;
let l2_table = l1_table.next_level_table(l1i).ok_or(Errno::DoesNotExist)?;
let entry = l2_table[l2i];
if entry.is_present() {
Ok(entry)
} else {
Err(Errno::DoesNotExist)
}
}
}
impl TableImpl {
/// Constructs a table with no valid mappings
pub const fn empty() -> Self {
Self {
entries: [EntryImpl::EMPTY; 512],
}
}
/// Returns next-level translation table reference for `index`, if one is present.
/// If `index` represents a `Block`-type mapping, will return an error.
/// If `index` does not map to any translation table, will try to allocate, init and
/// map a new one, returning it after doing so.
pub fn next_level_table_or_alloc(&mut self, index: usize) -> Result<&'static mut Self, Errno> {
let entry = self[index];
if entry.is_present() {
if !entry.is_normal() {
return Err(Errno::InvalidArgument);
}
Ok(unsafe { &mut *(mem::virtualize(entry.address()) as *mut _) })
} else {
let phys = phys::alloc_page(PageUsage::Paging)?;
let res = unsafe { &mut *(mem::virtualize(phys) as *mut Self) };
self[index] = EntryImpl::normal(phys, MapAttributes::empty());
res.entries.fill(EntryImpl::EMPTY);
Ok(res)
}
}
/// Returns next-level translation table reference for `index`, if one is present.
/// Same as [next_level_table_or_alloc], but returns `None` if no table is mapped.
pub fn next_level_table(&self, index: usize) -> Option<&'static Self> {
let entry = self[index];
if entry.is_present() {
if !entry.is_normal() {
panic!("Entry is not a table: idx={}", index);
}
Some(unsafe { &*(mem::virtualize(entry.address()) as *const _) })
} else {
None
}
}
/// Returns mutable next-level translation table reference for `index`,
/// if one is present. Same as [next_level_table_or_alloc], but returns
/// `None` if no table is mapped.
pub fn next_level_table_mut(&mut self, index: usize) -> Option<&'static mut Self> {
let entry = self[index];
if entry.is_present() {
if !entry.is_normal() {
panic!("Entry is not a table: idx={}", index);
}
Some(unsafe { &mut *(mem::virtualize(entry.address()) as *mut _) })
} else {
None
}
}
}
impl Index<usize> for TableImpl {
type Output = EntryImpl;
fn index(&self, index: usize) -> &Self::Output {
&self.entries[index]
}
}
impl IndexMut<usize> for TableImpl {
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
&mut self.entries[index]
}
}
+1 -7
View File
@@ -14,13 +14,7 @@ cfg_if! {
pub mod aarch64;
pub use aarch64 as platform;
pub use aarch64::{machine, intrin};
} else if #[cfg(target_arch = "x86_64")] {
pub mod x86_64;
pub use x86_64 as platform;
pub use x86_64 as machine;
pub use x86_64::intrin;
pub use aarch64::machine;
}
}
-53
View File
@@ -1,53 +0,0 @@
.section .text._multiboot
.set MAGIC, 0xE85250D6
.set ARCH, 0x0
.set HDRLEN, 16
.set CHKSUM, (-(MAGIC + ARCH + HDRLEN)) & 0xFFFFFFFF
.long MAGIC
.long ARCH
.long HDRLEN
.long CHKSUM
.short 5
.short 0
.long 20
.long 800
.long 600
.long 32
.short 0
.long 8
.section .text._entry
.global _entry
_entry:
.code32
cli
lea (multiboot_registers - KERNEL_OFFSET), %edi
mov %eax, 0(%edi)
mov %ebx, 4(%edi)
// Setup paging tables
lea (_entry_upper - KERNEL_OFFSET), %ebx
jmp __x86_64_enter_upper
.code64
_entry_upper:
movabsq $1f, %rax
jmp *%rax
1:
lea bsp_stack_top(%rip), %rax
mov %rax, %rsp
mov multiboot_registers(%rip), %edi
mov (4 + multiboot_registers)(%rip), %esi
call __x86_64_bsp_main
.section .bss
.align 16
bsp_stack_bottom:
.skip 65536
bsp_stack_top:
multiboot_registers:
.skip 8
-7
View File
@@ -1,7 +0,0 @@
.set KERNEL_OFFSET, 0xFFFFFF8000000000
.set PTE_PRESENT, 1 << 0
.set PTE_WRITABLE, 1 << 1
.set PTE_USERSPACE, 1 << 2
.set PTE_BLOCK, 1 << 7
-122
View File
@@ -1,122 +0,0 @@
//! x86_64 common boot logic
use crate::arch::x86_64::{
self, gdt, idt, intc,
reg::{CR0, CR4},
syscall,
};
use crate::config::{ConfigKey, CONFIG};
use crate::debug;
use crate::dev::{display::FramebufferInfo, irq::IntSource, pseudo, Device};
use crate::font;
use crate::fs::{
devfs::{self, CharDeviceType},
sysfs,
};
use crate::mem::{
self, heap,
phys::{self, MemoryRegion, PageUsage, ReservedRegion},
virt,
};
use crate::proc;
use core::arch::global_asm;
use core::mem::MaybeUninit;
use tock_registers::interfaces::ReadWriteable;
static mut RESERVED_REGION_MB2: MaybeUninit<ReservedRegion> = MaybeUninit::uninit();
#[no_mangle]
extern "C" fn __x86_64_bsp_main(_mb_checksum: u32, mb_info_ptr: u32) -> ! {
CR4.modify(CR4::OSXMMEXCPT::SET + CR4::OSFXSR::SET);
CR0.modify(CR0::EM::CLEAR + CR0::MP::SET);
unsafe {
// Setup a proper GDT
gdt::init();
idt::init(intc::map_isr_entries);
}
virt::enable().expect("Failed to initialize virtual memory");
let mb_info = unsafe {
multiboot2::load_with_offset(mb_info_ptr as usize, mem::KERNEL_OFFSET)
.expect("Failed to load multiboot info structure")
};
unsafe {
let mb_info_page = (mb_info_ptr & !0xFFF) as usize;
RESERVED_REGION_MB2.write(ReservedRegion::new(
mb_info_page,
mb_info_page + ((mb_info.total_size() + 0xFFF) & !0xFFF),
));
phys::reserve("multiboot2", RESERVED_REGION_MB2.as_mut_ptr());
phys::init_from_iter(
mb_info
.memory_map_tag()
.unwrap()
.memory_areas()
.map(|entry| MemoryRegion {
start: ((entry.start_address() + 0xFFF) & !0xFFF) as usize,
end: (entry.end_address() & !0xFFF) as usize,
}),
);
}
// Setup a heap
unsafe {
let heap_base_phys = phys::alloc_contiguous_pages(PageUsage::KernelHeap, 4096)
.expect("Failed to allocate memory for heap");
let heap_base_virt = mem::virtualize(heap_base_phys);
heap::init(heap_base_virt, 16 * 1024 * 1024);
}
let initrd_info = mb_info.module_tags().next().unwrap();
{
let mut cfg = CONFIG.lock();
cfg.set_usize(ConfigKey::InitrdBase, initrd_info.start_address() as usize);
cfg.set_usize(ConfigKey::InitrdSize, initrd_info.module_size() as usize);
}
// Setup hardware
unsafe {
x86_64::INTC.enable().ok();
}
let fb_info = mb_info.framebuffer_tag().unwrap();
let virt = mem::virtualize(fb_info.address as usize);
debugln!(
"Framebuffer base: phys={:#x}, virt={:#x}",
fb_info.address,
virt
);
x86_64::DISPLAY.set_framebuffer(FramebufferInfo {
width: fb_info.width as usize,
height: fb_info.height as usize,
phys_base: fb_info.address as usize,
virt_base: virt,
});
unsafe {
x86_64::COM1.init_irqs().unwrap();
x86_64::local_timer().init_irqs().unwrap();
x86_64::local_timer().enable().unwrap();
}
font::init();
debug::set_display(&x86_64::DISPLAY);
syscall::init();
devfs::init();
sysfs::init();
devfs::add_char_device(&x86_64::COM1, CharDeviceType::TtySerial).unwrap();
devfs::add_named_char_device(&pseudo::ZERO, "zero").unwrap();
devfs::add_named_char_device(&pseudo::RANDOM, "random").unwrap();
unsafe {
proc::enter();
}
}
global_asm!(include_str!("macros.S"), options(att_syntax));
global_asm!(include_str!("entry.S"), options(att_syntax));
global_asm!(include_str!("upper.S"), options(att_syntax));
-94
View File
@@ -1,94 +0,0 @@
.code32
.section .text._entry
__x86_64_enter_upper:
mov $(PTE_PRESENT | PTE_WRITABLE | PTE_USERSPACE), %edx
// Setup PML4
lea (KERNEL_FIXED - KERNEL_OFFSET), %edi
lea (KERNEL_FIXED + 4096 - KERNEL_OFFSET), %esi
mov %edx, %eax
or %esi, %eax
// pml4[0] = %eax
mov %eax, (%edi)
// pml4[511] = %eax
mov %eax, 4088(%edi)
// Setup PDPT
mov %esi, %edi
lea (KERNEL_FIXED + 8192 - KERNEL_OFFSET), %esi
xor %ecx, %ecx
1:
// %eax = &table[%ecx] | attrs
mov %esi, %eax
or %edx, %eax
mov %eax, (%edi, %ecx, 8)
add $4096, %esi
inc %ecx
cmp $16, %ecx
jne 1b
// Setup PDs
lea (KERNEL_FIXED + 8192 - KERNEL_OFFSET), %edi
mov $(PTE_PRESENT | PTE_BLOCK | PTE_WRITABLE), %edx
mov $(512 * 16), %ecx
1:
dec %ecx
// %eax = attrs | (i << 21)
mov %ecx, %eax
shl $21, %eax
or %edx, %eax
mov %eax, (%edi, %ecx, 8)
test %ecx, %ecx
jnz 1b
// Enable PAE/PSE
mov %cr4, %eax
or $((1 << 5) | (1 << 4)), %eax
mov %eax, %cr4
// Enable EFER.LME
mov $0xC0000080, %ecx
rdmsr
or $(1 << 8), %eax
wrmsr
// Set CR3
lea (KERNEL_FIXED - KERNEL_OFFSET), %edi
mov %edi, %cr3
// Enable paging
mov %cr0, %eax
or $(1 << 31), %eax
mov %eax, %cr0
lgdt (gdtr64 - KERNEL_OFFSET)
ljmp $0x08, $(1f - KERNEL_OFFSET)
1:
mov $0x10, %ax
mov %ax, %ds
mov %ax, %es
mov %ax, %fs
mov %ax, %gs
mov %ax, %ss
.code64
movabsq $KERNEL_OFFSET, %rax
add %rax, %rbx
jmp *%rbx
.section .rodata
.code32
.align 16
gdt64:
.quad 0
.quad 0x00209A0000000000
.quad 0x0000920000000000
gdt_end64:
.align 16
gdtr64:
.short gdt_end64 - gdt64 - 1
.long gdt64 - KERNEL_OFFSET
-115
View File
@@ -1,115 +0,0 @@
.section .text
.global __x86_64_ctx_switch
.global __x86_64_ctx_switch_to
.global __x86_64_ctx_enter_kernel
.global __x86_64_ctx_enter_from_fork
__x86_64_ctx_enter_user:
pop %rcx
pop %rdi
pop %rdi
pop %rdx
push %rdx
push $0x1B
push %rcx
push $0x200
push $0x23
push %rdx
iretq
__x86_64_ctx_enter_kernel:
pop %rdi
pop %rdx
// 8-byte fixup for proper stack alignment
push %rdx
mov %rsp, %rcx
push $0x10
push %rcx
push $0x200
push $0x08
push %rdx
iretq
__x86_64_ctx_enter_from_fork:
pop %rdi
pop %rsi
pop %rdx
pop %r10
pop %r8
pop %r9
pop %rax
xor %rax, %rax
pop %r11 // rsp3
pop %rcx // rip3
push $0x1B
push %r11
push $0x200
push $0x23
push %rcx
iretq
__x86_64_ctx_switch:
// %rsi -- src ctx ptr
// %rdi -- dst ctx ptr
// push %tss_rsp0
// TODO save gs_base
mov (4 + TSS)(%rip), %rax
push %rax
// push %cr3
mov %cr3, %rax
push %rax
push %r15
push %r14
push %r13
push %r12
push %rbx
push %rbp
// TODO SAVE FP CONTEXT
mov %rsp, (%rsi)
__x86_64_ctx_switch_to:
mov (%rdi), %rsp
// TODO RESTORE FP CONTEXT
pop %rbp
pop %rbx
pop %r12
pop %r13
pop %r14
pop %r15
// pop %cr3
pop %rax
test %rax, %rax
jz 1f
mov %rax, %cr3
1:
// TODO set gs_base = rax
// pop %tss_rsp0
pop %rax
mov %rax, (4 + TSS)(%rip)
ret
-206
View File
@@ -1,206 +0,0 @@
//! Thread context
use crate::arch::platform::ForkFrame;
use crate::mem::{
self,
phys::{self, PageUsage},
};
use core::arch::global_asm;
use core::mem::size_of;
struct Stack {
bp: usize,
sp: usize,
}
/// Structure representing thread context
#[repr(C)]
pub struct Context {
/// Thread's kernel stack pointer
pub k_sp: usize, // 0x00
stack_base: usize,
stack_page_count: usize,
}
impl Context {
/// Constructs a new kernel-space thread context
pub fn kernel(entry: usize, arg: usize) -> Self {
let mut stack = Stack::new(8);
stack.push(entry);
stack.push(arg);
stack.setup_common(__x86_64_ctx_enter_kernel as usize, 0, 0);
Self {
k_sp: stack.sp,
stack_base: stack.bp,
stack_page_count: 8,
}
}
/// Constructs a new user-space thread context
pub fn user(entry: usize, arg: usize, cr3: usize, ustack: usize) -> Self {
let cr3 = cr3 & 0xFFFFFFFF;
let mut stack = Stack::new(8);
let stack_top = stack.sp;
stack.push(entry);
stack.push(arg);
stack.push(0);
stack.push(ustack);
stack.setup_common(__x86_64_ctx_enter_user as usize, cr3, stack_top);
Self {
k_sp: stack.sp,
stack_base: stack.bp,
stack_page_count: 8,
}
}
/// Constructs an uninitialized thread context
pub fn empty() -> Self {
let stack = Stack::new(8);
Self {
k_sp: stack.sp,
stack_base: stack.bp,
stack_page_count: 8,
}
}
/// Sets up a context for signal entry
///
/// # Safety
///
/// Unsafe: may clobber an already active context
pub unsafe fn setup_signal_entry(
&mut self,
entry: usize,
arg: usize,
cr3: usize,
ustack: usize,
) {
let cr3 = cr3 & 0xFFFFFFFF;
let mut stack = Stack::from_base_size(self.stack_base, self.stack_page_count);
let stack_top = stack.sp;
stack.push(entry);
stack.push(arg);
stack.push(0);
stack.push(ustack);
stack.setup_common(__x86_64_ctx_enter_user as usize, cr3, stack_top);
self.k_sp = stack.sp;
}
/// Clones a process context from given `frame`
pub fn fork(frame: &ForkFrame, cr3: usize) -> Self {
let cr3 = cr3 & 0xFFFFFFFF;
let mut stack = Stack::new(8);
let stack_top = stack.sp;
stack.push(frame.saved_rip);
stack.push(frame.saved_rsp);
stack.push(frame.x[6]); // rax
stack.push(frame.x[5]); // r9
stack.push(frame.x[4]); // r8
stack.push(frame.x[3]); // r10
stack.push(frame.x[2]); // rdx
stack.push(frame.x[1]); // rsi
stack.push(frame.x[0]); // rdi
// Setup common
stack.push(__x86_64_ctx_enter_from_fork as usize); // return address
stack.push(stack_top); // gs_base
stack.push(cr3);
stack.push(frame.x[9]); // r15
stack.push(frame.x[10]); // r14
stack.push(frame.x[11]); // r13
stack.push(frame.x[12]); // r12
stack.push(frame.x[7]); // rbx
stack.push(frame.x[8]); // rbp
Self {
k_sp: stack.sp,
stack_base: stack.bp,
stack_page_count: 8,
}
}
/// Performs initial thread entry
///
/// # Safety
///
/// Unsafe: does not check if any context has already been activated
/// before, so must only be called once.
pub unsafe extern "C" fn enter(&mut self) -> ! {
__x86_64_ctx_switch_to(self);
panic!("This code should not run");
}
/// Performs context switch from `self` to `to`.
///
/// # Safety
///
/// Unsafe: does not check if `self` is actually an active context.
pub unsafe extern "C" fn switch(&mut self, to: &mut Context) {
__x86_64_ctx_switch(to, self);
}
}
impl Stack {
pub fn new(page_count: usize) -> Stack {
let phys = phys::alloc_contiguous_pages(PageUsage::Kernel, page_count).unwrap();
let bp = mem::virtualize(phys);
Stack {
bp,
sp: bp + page_count * mem::PAGE_SIZE,
}
}
pub unsafe fn from_base_size(bp: usize, page_count: usize) -> Stack {
Stack {
bp,
sp: bp + page_count * mem::PAGE_SIZE,
}
}
pub fn setup_common(&mut self, entry: usize, cr3: usize, tss_rsp0: usize) {
self.push(entry); // return address
self.push(tss_rsp0); // gs_base
self.push(cr3);
self.push(0); // r15
self.push(0); // r14
self.push(0); // r13
self.push(0); // r12
self.push(0); // rbx
self.push(0); // rbp
}
pub fn push(&mut self, value: usize) {
if self.bp == self.sp {
panic!("Stack overflow");
}
self.sp -= size_of::<usize>();
unsafe {
*(self.sp as *mut usize) = value;
}
}
}
extern "C" {
fn __x86_64_ctx_enter_from_fork();
fn __x86_64_ctx_enter_kernel();
fn __x86_64_ctx_enter_user();
fn __x86_64_ctx_switch(dst: *mut Context, src: *mut Context);
fn __x86_64_ctx_switch_to(dst: *mut Context);
}
global_asm!(include_str!("context.S"), options(att_syntax));
-117
View File
@@ -1,117 +0,0 @@
use crate::arch::{intrin, x86_64};
use crate::debug::Level;
use crate::dev::irq::{IntController, IrqContext};
use crate::mem::{self, virt::table::Space};
use crate::proc::{sched, Thread};
use core::arch::asm;
use libsys::{error::Errno, signal::Signal};
#[allow(dead_code)]
#[derive(Debug)]
struct ExceptionFrame {
r15: u64,
r14: u64,
r13: u64,
r12: u64,
r11: u64,
r10: u64,
r9: u64,
r8: u64,
rdi: u64,
rsi: u64,
rbp: u64,
rbx: u64,
rdx: u64,
rcx: u64,
rax: u64,
err_no: u64,
err_code: u64,
rip: u64,
cs: u64,
rflags: u64,
rsp: u64,
ss: u64,
}
fn pfault_read_cr2() -> u64 {
let mut res;
unsafe {
asm!("mov %cr2, {}", out(reg) res, options(att_syntax));
}
res
}
fn pfault_access_type(code: u64) -> &'static str {
if code & (1 << 4) != 0 {
"INSTRUCTION FETCH"
} else if code & (1 << 1) != 0 {
"WRITE"
} else {
"READ"
}
}
fn pfault_dump(level: Level, frame: &ExceptionFrame, cr2: usize) {
println!(level, "\x1B[41;1mPage fault:");
println!(
level,
" Illegal {} at {:#018x}\x1B[0m",
pfault_access_type(frame.err_code),
cr2
);
}
#[no_mangle]
extern "C" fn __x86_64_exception_handler(frame: &mut ExceptionFrame) {
if frame.err_no == 14 {
let cr2 = pfault_read_cr2() as usize;
let is_write = frame.err_code & (1 << 1) != 0;
if is_write && cr2 < mem::KERNEL_OFFSET && sched::is_ready() {
let thread = Thread::current();
let proc = thread.owner().unwrap();
let res = proc.manipulate_space(|space| {
space.try_cow_copy(cr2 & !0xFFF)?;
unsafe {
intrin::flush_tlb_virt(cr2 & !0xFFF);
}
Result::<(), Errno>::Ok(())
});
if res.is_err() {
errorln!(
"Page fault at {:#x} in user {:?}",
frame.rip,
thread.owner_id()
);
pfault_dump(Level::Error, frame, cr2);
proc.enter_fault_signal(thread, Signal::SegmentationFault);
}
return;
}
errorln!("Unresolved page fault:");
pfault_dump(Level::Error, frame, cr2);
}
errorln!(
"Exception occurred: err_no={}, err_code={:#x}",
frame.err_no,
frame.err_code,
);
errorln!("cs:rip = {:02x}:{:#x}", frame.cs, frame.rip);
errorln!("ss:rsp = {:02x}:{:#x}", frame.ss, frame.rsp);
panic!("Unhandled exception");
}
#[no_mangle]
extern "C" fn __x86_64_irq_handler(frame: &mut ExceptionFrame) {
unsafe {
let ic = IrqContext::new(frame.err_no as usize);
x86_64::intc().handle_pending_irqs(&ic);
}
}
-153
View File
@@ -1,153 +0,0 @@
use core::arch::asm;
use core::mem::size_of_val;
#[allow(dead_code)]
#[repr(packed)]
struct Entry {
limit_lo: u16,
base_lo: u16,
base_mi: u8,
access: u8,
flags: u8,
base_hi: u8,
}
#[allow(dead_code)]
#[repr(packed)]
struct Tss {
__res0: u32,
rsp0: u64,
rsp1: u64,
rsp2: u64,
__res1: u32,
ist1: u64,
ist2: u64,
ist3: u64,
ist4: u64,
ist5: u64,
ist6: u64,
ist7: u64,
__res2: u64,
__res3: u16,
iopb_base: u16,
}
#[allow(dead_code)]
#[repr(packed)]
struct Pointer {
size: u16,
offset: usize,
}
impl Entry {
const FLAG_LONG: u8 = 1 << 5;
const ACC_PRESENT: u8 = 1 << 7;
const ACC_SYSTEM: u8 = 1 << 4;
const ACC_EXECUTE: u8 = 1 << 3;
const ACC_WRITE: u8 = 1 << 1;
const ACC_RING3: u8 = 3 << 5;
const ACC_ACCESS: u8 = 1 << 0;
const fn new(base: u32, limit: u32, flags: u8, access: u8) -> Self {
Self {
base_lo: (base & 0xFFFF) as u16,
base_mi: ((base >> 16) & 0xFF) as u8,
base_hi: ((base >> 24) & 0xFF) as u8,
access,
flags: (flags & 0xF0) | (((limit >> 16) & 0xF) as u8),
limit_lo: (limit & 0xFFFF) as u16,
}
}
const fn null() -> Self {
Self {
base_lo: 0,
base_mi: 0,
base_hi: 0,
access: 0,
flags: 0,
limit_lo: 0,
}
}
}
impl Tss {
const fn new() -> Self {
Self {
__res0: 0,
rsp0: 0,
rsp1: 0,
rsp2: 0,
__res1: 0,
ist1: 0,
ist2: 0,
ist3: 0,
ist4: 0,
ist5: 0,
ist6: 0,
ist7: 0,
__res2: 0,
__res3: 0,
iopb_base: 0,
}
}
}
const SIZE: usize = 7;
#[no_mangle]
static mut TSS: Tss = Tss::new();
static mut GDT: [Entry; SIZE] = [
Entry::null(),
Entry::new(
0,
0,
Entry::FLAG_LONG,
Entry::ACC_PRESENT | Entry::ACC_SYSTEM | Entry::ACC_EXECUTE,
),
Entry::new(
0,
0,
0,
Entry::ACC_PRESENT | Entry::ACC_SYSTEM | Entry::ACC_WRITE,
),
Entry::new(
0,
0,
0,
Entry::ACC_PRESENT | Entry::ACC_SYSTEM | Entry::ACC_RING3 | Entry::ACC_WRITE,
),
Entry::new(
0,
0,
Entry::FLAG_LONG,
Entry::ACC_PRESENT | Entry::ACC_SYSTEM | Entry::ACC_RING3 | Entry::ACC_EXECUTE,
),
Entry::null(),
Entry::null(),
];
#[inline]
unsafe fn load_gdt(ptr: &Pointer, tr: u16) {
asm!(r#"
lgdt ({})
ltr %ax
"#, in(reg) ptr, in("ax") tr, options(att_syntax));
}
pub unsafe fn init() {
let tss_addr = &TSS as *const _ as usize;
GDT[5] = Entry::new(
(tss_addr & 0xFFFFFFFF) as u32,
size_of_val(&TSS) as u32 - 1,
Entry::FLAG_LONG,
Entry::ACC_ACCESS | Entry::ACC_PRESENT | Entry::ACC_EXECUTE,
);
core::ptr::write(&mut GDT[6] as *mut _ as *mut u64, (tss_addr >> 32) as u64);
let gdtr = Pointer {
size: size_of_val(&GDT) as u16 - 1,
offset: &GDT as *const _ as usize,
};
load_gdt(&gdtr, 0x28);
}
-124
View File
@@ -1,124 +0,0 @@
.macro isr_nerr, n
exc_isr_\n:
cli
pushq $0
pushq $\n
jmp __x86_64_isr_common
.endm
// ISR for exception with an error code
.macro isr_yerr, n
exc_isr_\n:
cli
pushq $\n
jmp __x86_64_isr_common
.endm
.section .text
__x86_64_isr_common:
push %rax
push %rcx
push %rdx
push %rbx
push %rbp
push %rsi
push %rdi
push %r8
push %r9
push %r10
push %r11
push %r12
push %r13
push %r14
push %r15
mov %rsp, %rdi
call __x86_64_exception_handler
pop %r15
pop %r14
pop %r13
pop %r12
pop %r11
pop %r10
pop %r9
pop %r8
pop %rdi
pop %rsi
pop %rbp
pop %rbx
pop %rdx
pop %rcx
pop %rax
add $16, %rsp
iretq
isr_nerr 0
isr_nerr 1
isr_nerr 2
isr_nerr 3
isr_nerr 4
isr_nerr 5
isr_nerr 6
isr_nerr 7
isr_yerr 8
isr_nerr 9
isr_yerr 10
isr_yerr 11
isr_yerr 12
isr_yerr 13
isr_yerr 14
isr_nerr 15
isr_nerr 16
isr_yerr 17
isr_nerr 18
isr_nerr 19
isr_nerr 20
isr_nerr 21
isr_nerr 22
isr_nerr 23
isr_nerr 24
isr_nerr 25
isr_nerr 26
isr_nerr 27
isr_nerr 28
isr_nerr 29
isr_yerr 30
isr_nerr 31
.section .rodata
.global __x86_64_exception_vectors
__x86_64_exception_vectors:
.quad exc_isr_0
.quad exc_isr_1
.quad exc_isr_2
.quad exc_isr_3
.quad exc_isr_4
.quad exc_isr_5
.quad exc_isr_6
.quad exc_isr_7
.quad exc_isr_8
.quad exc_isr_9
.quad exc_isr_10
.quad exc_isr_11
.quad exc_isr_12
.quad exc_isr_13
.quad exc_isr_14
.quad exc_isr_15
.quad exc_isr_16
.quad exc_isr_17
.quad exc_isr_18
.quad exc_isr_19
.quad exc_isr_20
.quad exc_isr_21
.quad exc_isr_22
.quad exc_isr_23
.quad exc_isr_24
.quad exc_isr_25
.quad exc_isr_26
.quad exc_isr_27
.quad exc_isr_28
.quad exc_isr_29
.quad exc_isr_30
.quad exc_isr_31
-80
View File
@@ -1,80 +0,0 @@
use core::arch::{asm, global_asm};
use core::mem::size_of_val;
#[allow(dead_code)]
#[derive(Clone, Copy)]
#[repr(packed)]
pub struct Entry {
base_lo: u16,
selector: u16,
__res0: u8,
flags: u8,
base_hi: u16,
base_ex: u32,
__res1: u32,
}
#[allow(dead_code)]
#[repr(packed)]
struct Pointer {
limit: u16,
offset: usize,
}
pub const SIZE: usize = 256;
impl Entry {
pub const PRESENT: u8 = 1 << 7;
pub const INT32: u8 = 0xE;
pub const fn new(base: usize, selector: u16, flags: u8) -> Self {
Self {
base_lo: (base & 0xFFFF) as u16,
base_hi: ((base >> 16) & 0xFFFF) as u16,
base_ex: (base >> 32) as u32,
selector,
flags,
__res0: 0,
__res1: 0,
}
}
const fn empty() -> Self {
Self {
base_lo: 0,
base_hi: 0,
base_ex: 0,
selector: 0,
flags: 0,
__res0: 0,
__res1: 0,
}
}
}
static mut IDT: [Entry; SIZE] = [Entry::empty(); SIZE];
#[inline]
unsafe fn load_idt(ptr: &Pointer) {
asm!("lidt ({})", in(reg) ptr, options(att_syntax));
}
pub unsafe fn init<F: FnOnce(&mut [Entry; SIZE])>(f: F) {
extern "C" {
static __x86_64_exception_vectors: [usize; 32];
}
for (i, &entry) in __x86_64_exception_vectors.iter().enumerate() {
IDT[i] = Entry::new(entry, 0x08, Entry::PRESENT | Entry::INT32);
}
f(&mut IDT);
let idtr = Pointer {
limit: size_of_val(&IDT) as u16 - 1,
offset: &IDT as *const _ as usize,
};
load_idt(&idtr);
}
global_asm!(include_str!("idt.S"), options(att_syntax));
-144
View File
@@ -1,144 +0,0 @@
use crate::arch::x86_64::{
idt::{Entry as IdtEntry, SIZE as IDT_SIZE},
PortIo,
};
use crate::dev::{
irq::{IntController, IntSource, IrqContext},
Device,
};
use crate::sync::IrqSafeSpinLock;
use core::arch::global_asm;
use libsys::error::Errno;
const ICW1_INIT: u8 = 0x10;
const ICW1_ICW4: u8 = 0x01;
const ICW4_8086: u8 = 0x01;
pub(super) struct I8259 {
cmd_a: PortIo<u8>,
cmd_b: PortIo<u8>,
data_a: PortIo<u8>,
data_b: PortIo<u8>,
table: IrqSafeSpinLock<[Option<&'static (dyn IntSource + Sync)>; 15]>,
}
/// Interrupt line number wrapper struct
#[derive(Clone, Copy, Debug)]
#[repr(transparent)]
pub struct IrqNumber(u32);
impl IrqNumber {
/// IRQ line number limit
pub const MAX: u32 = 16;
/// Constructs a wrapped IRQ line number
pub const fn new(u: u32) -> Self {
if u > Self::MAX {
panic!();
}
Self(u)
}
}
impl Device for I8259 {
fn name(&self) -> &'static str {
"i8259-compatible IRQ controller"
}
unsafe fn enable(&self) -> Result<(), Errno> {
self.cmd_a.write(ICW1_INIT | ICW1_ICW4);
self.cmd_b.write(ICW1_INIT | ICW1_ICW4);
self.data_a.write(32);
self.data_b.write(32 + 8);
self.data_a.write(4);
self.data_b.write(2);
self.data_a.write(ICW4_8086);
self.data_b.write(ICW4_8086);
self.data_a.write(0xFE);
self.data_b.write(0xFF);
Ok(())
}
}
impl IntController for I8259 {
type IrqNumber = IrqNumber;
fn register_handler(
&self,
irq: Self::IrqNumber,
handler: &'static (dyn IntSource + Sync),
) -> Result<(), Errno> {
let index = irq.0 as usize;
let mut lock = self.table.lock();
if lock[index].is_some() {
return Err(Errno::AlreadyExists);
}
lock[index] = Some(handler);
Ok(())
}
fn enable_irq(&self, irq: Self::IrqNumber) -> Result<(), Errno> {
let port = if irq.0 < 8 {
&self.data_a
} else {
&self.data_b
};
let mask = port.read() & !(1 << (irq.0 & 0x7));
port.write(mask);
Ok(())
}
fn handle_pending_irqs<'irq_context>(&'irq_context self, ic: &IrqContext<'irq_context>) {
let irq_number = ic.token();
// Clear irq
if irq_number > 8 {
self.cmd_b.write(0x20);
}
self.cmd_a.write(0x20);
{
let table = self.table.lock();
match table[irq_number] {
None => panic!("No handler registered for irq{}", irq_number),
Some(handler) => {
drop(table);
handler.handle_irq().expect("irq handler failed")
}
}
}
}
}
impl I8259 {
pub const fn new() -> Self {
unsafe {
Self {
cmd_a: PortIo::new(0x20),
data_a: PortIo::new(0x21),
cmd_b: PortIo::new(0xA0),
data_b: PortIo::new(0xA1),
table: IrqSafeSpinLock::new([None; 15]),
}
}
}
}
pub fn map_isr_entries(entries: &mut [IdtEntry; IDT_SIZE]) {
extern "C" {
static __x86_64_irq_vectors: [usize; 16];
}
for (i, &entry) in unsafe { __x86_64_irq_vectors.iter().enumerate() } {
entries[i + 32] = IdtEntry::new(entry, 0x08, IdtEntry::PRESENT | IdtEntry::INT32);
}
}
global_asm!(include_str!("irq_vectors.S"), options(att_syntax));
-56
View File
@@ -1,56 +0,0 @@
//! x86_64-specific assembly functions
use core::arch::asm;
/// Disables delievery of IRQs
///
/// # Safety
///
/// Unsafe: requires ring 0
#[inline(always)]
pub unsafe fn irq_disable() {
asm!("cli");
}
/// Discards an entry related to `addr` from TLB cache
///
/// # Safety
///
/// Unsafe: requires ring 0
#[inline(always)]
pub unsafe fn flush_tlb_virt(addr: usize) {
asm!("invlpg ({})", in(reg) addr, options(att_syntax));
}
/// Discards all entries related to `asid` from TLB cache
///
/// # Safety
///
/// Only safe to use for known [Process]es and their ASIDs
// TODO actually implement this on x86-64
#[inline(always)]
pub unsafe fn flush_tlb_asid(_asid: usize) {}
/// Read a value from a model-specific register
///
/// # Safety
///
/// Unsafe: arbitrary MSR reads, may cause CPU exceptions
#[inline(always)]
pub unsafe fn rdmsr(a: u32) -> u64 {
let mut eax: u32;
let mut edx: u32;
asm!("rdmsr", in("ecx") a, out("eax") eax, out("edx") edx);
(eax as u64) | ((edx as u64) << 32)
}
/// Writes a value to a model-specific register
///
/// # Safety
///
/// Unsafe: arbitrary MSR writes
#[inline(always)]
pub unsafe fn wrmsr(a: u32, b: u64) {
let eax = b as u32;
let edx = (b >> 32) as u32;
asm!("wrmsr", in("ecx") a, in("eax") eax, in("edx") edx);
}
-32
View File
@@ -1,32 +0,0 @@
use core::arch::asm;
use core::marker::PhantomData;
pub struct PortIo<T> {
port: u16,
_pd: PhantomData<T>,
}
impl<T> PortIo<T> {
pub const unsafe fn new(port: u16) -> Self {
Self {
port,
_pd: PhantomData,
}
}
}
impl PortIo<u8> {
pub fn read(&self) -> u8 {
let mut res: u8;
unsafe {
asm!("inb %dx, %al", in("dx") self.port, out("al") res, options(att_syntax));
}
res
}
pub fn write(&self, value: u8) {
unsafe {
asm!("outb %al, %dx", in("dx") self.port, in("al") value, options(att_syntax));
}
}
}
-130
View File
@@ -1,130 +0,0 @@
.macro irq_entry no
__x86_64_irq_\no:
cli
pushq $0
pushq $\no
push %rax
push %rcx
push %rdx
push %rbx
push %rbp
push %rsi
push %rdi
push %r8
push %r9
push %r10
push %r11
push %r12
push %r13
push %r14
push %r15
mov %rsp, %rdi
call __x86_64_irq_handler
pop %r15
pop %r14
pop %r13
pop %r12
pop %r11
pop %r10
pop %r9
pop %r8
pop %rdi
pop %rsi
pop %rbp
pop %rbx
pop %rdx
pop %rcx
pop %rax
add $16, %rsp
iretq
.endm
.section .text
// __x86_64_irq_0:
// cli
//
// push %rax
// push %rcx
// push %rdx
// push %rbx
// push %rbp
// push %rsi
// push %rdi
//
// push %r8
// push %r9
// push %r10
// push %r11
// push %r12
// push %r13
// push %r14
// push %r15
//
// mov $0x20, %al
// mov $0x20, %dx
// outb %al, %dx
//
// call sched_yield
//
// pop %r15
// pop %r14
// pop %r13
// pop %r12
// pop %r11
// pop %r10
// pop %r9
// pop %r8
//
// pop %rdi
// pop %rsi
// pop %rbp
// pop %rbx
// pop %rdx
// pop %rcx
// pop %rax
//
// iretq
irq_entry 0
irq_entry 1
irq_entry 2
irq_entry 3
irq_entry 4
irq_entry 5
irq_entry 6
irq_entry 7
irq_entry 8
irq_entry 9
irq_entry 10
irq_entry 11
irq_entry 12
irq_entry 13
irq_entry 14
irq_entry 15
.section .rodata
.global __x86_64_irq_vectors
__x86_64_irq_vectors:
.quad __x86_64_irq_0
.quad __x86_64_irq_1
.quad __x86_64_irq_2
.quad __x86_64_irq_3
.quad __x86_64_irq_4
.quad __x86_64_irq_5
.quad __x86_64_irq_6
.quad __x86_64_irq_7
.quad __x86_64_irq_8
.quad __x86_64_irq_9
.quad __x86_64_irq_10
.quad __x86_64_irq_11
.quad __x86_64_irq_12
.quad __x86_64_irq_13
.quad __x86_64_irq_14
.quad __x86_64_irq_15
-71
View File
@@ -1,71 +0,0 @@
//! x86_64 platform implementation details
use crate::dev::{display::StaticFramebuffer, irq::IntController, serial::SerialDevice};
use core::arch::asm;
mod uart;
use uart::Uart;
mod intc;
use intc::I8259;
pub use intc::IrqNumber;
mod timer;
use timer::Timer;
mod io;
pub(self) use io::PortIo;
pub mod boot;
pub mod context;
pub(self) mod exception;
pub(self) mod gdt;
pub(self) mod idt;
pub mod intrin;
pub mod reg;
pub(self) mod syscall;
pub mod virt;
pub use syscall::SyscallFrame as ForkFrame;
/// Masks IRQs and returns previous IRQ mask state
///
/// # Safety
///
/// Unsafe: disables IRQ handling temporarily
#[inline(always)]
pub unsafe fn irq_mask_save() -> u64 {
let mut res;
asm!("pushf; cli; pop {}", out(reg) res, options(att_syntax));
res
}
/// Restores IRQ mask state
///
/// # Safety
///
/// Unsafe: modifies interrupt behavior. Must only be used in
/// conjunction with [irq_mask_save]
#[inline(always)]
pub unsafe fn irq_restore(state: u64) {
if state & (1 << 9) != 0 {
asm!("sti");
}
}
/// Returns a reference to interrupt controller device
pub fn intc() -> &'static impl IntController {
&INTC
}
/// Returns a reference to primary console device
pub fn console() -> &'static impl SerialDevice {
&COM1
}
/// Returns a reference to CPU-local timer device
pub fn local_timer() -> &'static Timer {
&TIMER
}
static COM1: Uart = unsafe { Uart::new(0x3F8, IrqNumber::new(4)) };
pub(self) static INTC: I8259 = I8259::new();
pub(self) static DISPLAY: StaticFramebuffer = StaticFramebuffer::uninit();
pub(self) static TIMER: Timer = Timer::new(IrqNumber::new(0));
-152
View File
@@ -1,152 +0,0 @@
//! x86_64 model-specific and control register interfaces
macro_rules! wrap_msr {
($struct_name:ident, $name:ident, $address:expr, $fields:tt) => {
register_bitfields! {
u64,
#[allow(missing_docs)]
pub $name $fields
}
#[allow(missing_docs)]
pub struct $struct_name;
impl Readable for $struct_name {
type T = u64;
type R = $name::Register;
#[inline(always)]
fn get(&self) -> Self::T {
unsafe {
rdmsr($address)
}
}
}
impl Writeable for $struct_name {
type T = u64;
type R = $name::Register;
#[inline(always)]
fn set(&self, value: Self::T) {
unsafe {
wrmsr($address, value);
}
}
}
#[allow(missing_docs)]
pub const $name: $struct_name = $struct_name;
}
}
use tock_registers::{
interfaces::{Readable, Writeable},
register_bitfields,
};
use core::arch::asm;
use crate::arch::x86_64::intrin::{rdmsr, wrmsr};
// CRn registers
register_bitfields! {
u64,
/// Control register CR4 fields
#[allow(missing_docs)]
pub CR4 [
/// Indicates OS support for FXSR/FXRSTOR instructions
OSFXSR OFFSET(9) NUMBITS(1) [],
/// Indicates OS support for unmasked SIMD exceptions
OSXMMEXCPT OFFSET(10) NUMBITS(1) []
]
}
register_bitfields! {
u64,
/// Control register CR0 fields
#[allow(missing_docs)]
pub CR0 [
/// Indicates requirement for x87 emulation
EM OFFSET(2) NUMBITS(1) [],
/// Controls x87 exception handling
MP OFFSET(1) NUMBITS(1) []
]
}
#[allow(missing_docs)]
pub struct Cr4;
#[allow(missing_docs)]
pub struct Cr0;
impl Readable for Cr4 {
type T = u64;
type R = CR4::Register;
#[inline(always)]
fn get(&self) -> Self::T {
let mut res: u64;
unsafe {
asm!("mov %cr4, {}", out(reg) res, options(att_syntax))
}
res
}
}
impl Writeable for Cr4 {
type T = u64;
type R = CR4::Register;
#[inline(always)]
fn set(&self, value: Self::T) {
unsafe {
asm!("mov {}, %cr4", in(reg) value, options(att_syntax));
}
}
}
impl Readable for Cr0 {
type T = u64;
type R = CR0::Register;
#[inline(always)]
fn get(&self) -> Self::T {
let mut res: u64;
unsafe {
asm!("mov %cr0, {}", out(reg) res, options(att_syntax))
}
res
}
}
impl Writeable for Cr0 {
type T = u64;
type R = CR0::Register;
#[inline(always)]
fn set(&self, value: Self::T) {
unsafe {
asm!("mov {}, %cr0", in(reg) value, options(att_syntax));
}
}
}
/// Control register CR4
pub const CR4: Cr4 = Cr4;
/// Control register CR0
pub const CR0: Cr0 = Cr0;
wrap_msr!(MsrIa32Efer, MSR_IA32_EFER, 0xC0000080, [
SCE OFFSET(0) NUMBITS(1) [],
LME OFFSET(8) NUMBITS(1) [],
LMA OFFSET(10) NUMBITS(1) [],
NXE OFFSET(11) NUMBITS(1) []
]);
wrap_msr!(MsrIa32Lstar, MSR_IA32_LSTAR, 0xC0000082, [
VALUE OFFSET(0) NUMBITS(64) []
]);
wrap_msr!(MsrIa32Star, MSR_IA32_STAR, 0xC0000081, [
SYSCALL_CS_SS OFFSET(32) NUMBITS(8) [],
SYSRET_CS_SS OFFSET(48) NUMBITS(8) []
]);
wrap_msr!(MsrIa32Sfmask, MSR_IA32_SFMASK, 0xC0000084, [
IF OFFSET(9) NUMBITS(1) []
]);
@@ -1,13 +0,0 @@
use tock_registers::{
interfaces::{Readable, Writeable},
register_bitfields,
};
use crate::arch::x86_64::intrin::{rdmsr, wrmsr};
register_bitfields! {
u64,
pub MSR_IA32_EFER [
]
}
wrap_msr!(MSR_IA32_EFER, 0xC0000080);
-60
View File
@@ -1,60 +0,0 @@
.section .text
.global __x86_64_syscall_entry
__x86_64_syscall_entry:
// Syscalls only happen from user space, so
// relying on TSS.RSP0 is safe here I guess
mov %rsp, scratch(%rip)
mov (4 + TSS)(%rip), %rsp
// Now on kernel stack
// Push the whole state
push %rcx // saved %rip
push %r11 // saved %rflags
mov scratch(%rip), %r11
push %r11 // saved %rsp
push %r12
push %r13
push %r14
push %r15
push %rbp
push %rbx
push %rax
push %r9
push %r8
push %r10
push %rdx
push %rsi
push %rdi
mov %rsp, %rdi
call __x86_64_syscall
pop %rdi
pop %rsi
pop %rdx
pop %r10
pop %r8
pop %r9
pop %rax
pop %rbx
pop %rbp
pop %r15
pop %r14
pop %r13
pop %r12
pop %rdi
pop %r11
pop %rcx
mov %rdi, %rsp
sysretq
.section .bss
scratch:
.skip 8
-59
View File
@@ -1,59 +0,0 @@
use crate::arch::x86_64::reg::{MSR_IA32_EFER, MSR_IA32_LSTAR, MSR_IA32_SFMASK, MSR_IA32_STAR};
use core::arch::global_asm;
use tock_registers::interfaces::{ReadWriteable, Writeable};
use libsys::abi::SystemCall;
use crate::syscall;
/// Syscall registers
#[allow(dead_code)]
#[derive(Clone, Debug)]
pub struct SyscallFrame {
/// General-purpose registers
pub x: [usize; 13],
/// Caller (ring 3) saved stack pointer
pub saved_rsp: usize,
/// Caller (ring 3) saved processor flags
pub saved_rflags: usize,
/// Caller (ring 3) return address
pub saved_rip: usize,
}
pub(super) fn init() {
extern "C" {
fn __x86_64_syscall_entry();
}
MSR_IA32_SFMASK.write(MSR_IA32_SFMASK::IF::SET);
MSR_IA32_LSTAR.set(__x86_64_syscall_entry as usize as u64);
MSR_IA32_STAR
.write(MSR_IA32_STAR::SYSRET_CS_SS.val(0x1B - 8) + MSR_IA32_STAR::SYSCALL_CS_SS.val(0x08));
MSR_IA32_EFER.modify(MSR_IA32_EFER::SCE::SET);
}
#[no_mangle]
extern "C" fn __x86_64_syscall(frame: &mut SyscallFrame) {
let num = SystemCall::from_repr(frame.x[6]);
if num.is_none() {
todo!();
}
let num = num.unwrap();
if num == SystemCall::Fork {
match unsafe { syscall::sys_fork(frame) } {
Ok(pid) => frame.x[6] = u32::from(pid) as usize,
Err(err) => {
frame.x[6] = err.to_negative_isize() as usize;
}
}
return;
}
match syscall::syscall(num, &frame.x[..6]) {
Ok(val) => frame.x[6] = val,
Err(err) => {
frame.x[6] = err.to_negative_isize() as usize;
}
}
}
global_asm!(include_str!("syscall.S"), options(att_syntax));
-69
View File
@@ -1,69 +0,0 @@
//! i.... timer implementation
use crate::arch::machine::{self, IrqNumber, PortIo};
use crate::dev::{
irq::{IntController, IntSource},
pseudo,
timer::TimestampSource,
Device,
};
use crate::proc;
use core::sync::atomic::{AtomicU64, Ordering};
use core::time::Duration;
use libsys::error::Errno;
// 1.1931816666 MHz base freq
/// Generic timer struct
pub struct Timer {
data0: PortIo<u8>,
counter: AtomicU64,
irq: IrqNumber,
}
impl Device for Timer {
fn name(&self) -> &'static str {
"Intel Timer"
}
unsafe fn enable(&self) -> Result<(), Errno> {
const DIV: u16 = (1193182u32 / 1000) as u16;
self.data0.write((DIV & 0xFF) as u8);
self.data0.write((DIV >> 8) as u8);
Ok(())
}
}
impl TimestampSource for Timer {
fn timestamp(&self) -> Result<Duration, Errno> {
Ok(Duration::from_millis(self.counter.load(Ordering::Relaxed)))
}
}
impl IntSource for Timer {
fn handle_irq(&self) -> Result<(), Errno> {
let value = self.counter.fetch_add(1, Ordering::Relaxed);
proc::wait::tick();
pseudo::RANDOM.set_state(value as u32);
proc::switch();
Ok(())
}
fn init_irqs(&'static self) -> Result<(), Errno> {
machine::INTC.register_handler(self.irq, self)?;
machine::INTC.enable_irq(self.irq)?;
Ok(())
}
}
impl Timer {
/// Constructs a new instance of ARM Generic Timer
pub const fn new(irq: IrqNumber) -> Self {
Self {
data0: unsafe { PortIo::new(0x40) },
counter: AtomicU64::new(0),
irq,
}
}
}
-74
View File
@@ -1,74 +0,0 @@
use crate::arch::x86_64::{self, IrqNumber, PortIo};
use crate::dev::{
irq::{IntController, IntSource},
serial::SerialDevice,
tty::{CharRing, TtyDevice},
Device,
};
use libsys::error::Errno;
#[derive(TtyCharDevice)]
pub(super) struct Uart {
dr: PortIo<u8>,
ier: PortIo<u8>,
isr: PortIo<u8>,
ring: CharRing<16>,
irq: IrqNumber,
}
impl Device for Uart {
fn name(&self) -> &'static str {
"x86 COM-port"
}
unsafe fn enable(&self) -> Result<(), Errno> {
Ok(())
}
}
impl TtyDevice<16> for Uart {
fn ring(&self) -> &CharRing<16> {
&self.ring
}
}
impl IntSource for Uart {
fn handle_irq(&self) -> Result<(), Errno> {
if self.isr.read() != 0 {
self.recv_byte(self.dr.read());
}
Ok(())
}
fn init_irqs(&'static self) -> Result<(), Errno> {
// TODO shared IRQs between COM# ports
x86_64::INTC.register_handler(self.irq, self)?;
self.ier.write(1 << 0);
x86_64::INTC.enable_irq(self.irq)?;
Ok(())
}
}
impl SerialDevice for Uart {
fn send(&self, byte: u8) -> Result<(), Errno> {
self.dr.write(byte);
Ok(())
}
fn recv(&self, _blocking: bool) -> Result<u8, Errno> {
todo!()
}
}
impl Uart {
pub const unsafe fn new(base: u16, irq: IrqNumber) -> Self {
Self {
dr: PortIo::new(base),
ier: PortIo::new(base + 1),
isr: PortIo::new(base + 2),
ring: CharRing::new(),
irq,
}
}
}
-16
View File
@@ -1,16 +0,0 @@
use super::table::TableImpl;
#[repr(C, align(0x1000))]
pub struct FixedTableGroup {
pub pml4: TableImpl,
pub pdpt: TableImpl,
pub pd: [TableImpl; 16],
}
// Upper mappings
#[no_mangle]
pub(super) static mut KERNEL_FIXED: FixedTableGroup = FixedTableGroup {
pml4: TableImpl::empty(),
pdpt: TableImpl::empty(),
pd: [TableImpl::empty(); 16],
};
-61
View File
@@ -1,61 +0,0 @@
//! x86_64 virtual memory management implementation
use crate::mem::virt::table::{MapAttributes, Entry};
use core::arch::asm;
use libsys::error::Errno;
mod table;
mod fixed;
pub use table::{EntryImpl, SpaceImpl};
use fixed::KERNEL_FIXED;
bitflags! {
/// Raw attributes for x86_64 [Entry] implementation
pub struct RawAttributesImpl: u64 {
/// Entry is valid and mapped
const PRESENT = EntryImpl::PRESENT;
/// Entry is writable by user processes
const WRITE = EntryImpl::WRITE;
/// Entry is accessible (readable) by user processes
const USER = EntryImpl::USER;
/// Entry points to a block instead of a next-level table
const BLOCK = EntryImpl::BLOCK;
/// Entry is global across virtual address spaces
const GLOBAL = 1 << 8;
/// Entry is marked as Copy-on-Write
const EX_COW = EntryImpl::EX_COW;
}
}
impl From<MapAttributes> for RawAttributesImpl {
fn from(i: MapAttributes) -> Self {
let mut res = RawAttributesImpl::empty();
if i.contains(MapAttributes::USER_READ) {
res |= RawAttributesImpl::USER;
}
if i.contains(MapAttributes::USER_WRITE) {
res |= RawAttributesImpl::WRITE | RawAttributesImpl::USER;
}
res
}
}
/// Performs initialization of virtual memory control by kernel
///
/// # Safety
///
/// Only safe to be called once during virtual memory init.
pub unsafe fn enable() {
// Remove the lower mapping
KERNEL_FIXED.pml4[0] = EntryImpl::EMPTY;
// Flush the TLB by reloading cr3
asm!("mov %cr3, %rax; mov %rax, %cr3", options(att_syntax));
}
/// Allocates a range of virtual memory of requested size and maps
/// it to specified device memory
pub fn map_device_memory(_phys: usize, _count: usize) -> Result<usize, Errno> {
todo!()
}
-353
View File
@@ -1,353 +0,0 @@
use crate::arch::intrin;
use crate::mem::{
self,
phys::{self, PageUsage},
virt::table::{Entry, MapAttributes, Space},
};
use core::fmt;
use core::ops::{Index, IndexMut};
use libsys::error::Errno;
use super::{RawAttributesImpl, KERNEL_FIXED};
/// Transparent wrapper structure representing a single
/// translation table entry
#[derive(Clone, Copy)]
#[repr(transparent)]
pub struct EntryImpl(u64);
/// Structure describing a single level of translation mappings
#[derive(Clone, Copy)]
#[repr(C, align(0x1000))]
pub struct TableImpl {
entries: [EntryImpl; 512],
}
/// Top-level translation table wrapper
#[repr(transparent)]
pub struct SpaceImpl(TableImpl);
impl EntryImpl {
pub(super) const PRESENT: u64 = 1 << 0;
pub(super) const WRITE: u64 = 1 << 1;
pub(super) const USER: u64 = 1 << 2;
pub(super) const BLOCK: u64 = 1 << 7;
pub(super) const EX_COW: u64 = 1 << 62;
const PHYS_MASK: u64 = 0x0000FFFFFFFFF000;
}
impl fmt::Debug for EntryImpl {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("EntryImpl")
.field("address", &self.address())
.field("flags", &unsafe {
RawAttributesImpl::from_bits_unchecked(self.0 & 0xFFF)
})
.finish_non_exhaustive()
}
}
impl Entry for EntryImpl {
type RawAttributes = RawAttributesImpl;
const EMPTY: Self = Self(0);
#[inline]
fn normal(addr: usize, attrs: MapAttributes) -> Self {
Self((addr as u64) | RawAttributesImpl::from(attrs).bits() | Self::PRESENT)
}
#[inline]
fn block(addr: usize, attrs: MapAttributes) -> Self {
Self((addr as u64) | RawAttributesImpl::from(attrs).bits() | Self::BLOCK | Self::PRESENT)
}
#[inline]
fn address(self) -> usize {
(self.0 & Self::PHYS_MASK) as usize
}
#[inline]
fn set_address(&mut self, virt: usize) {
self.0 &= !Self::PHYS_MASK;
self.0 |= (virt as u64) & Self::PHYS_MASK;
}
#[inline]
fn is_present(self) -> bool {
self.0 & Self::PRESENT != 0
}
#[inline]
fn is_normal(self) -> bool {
self.0 & Self::BLOCK == 0
}
#[inline]
fn fork_with_cow(&mut self) -> Self {
self.0 &= !Self::WRITE;
self.0 |= Self::EX_COW;
*self
}
#[inline]
fn copy_from_cow(self, new_addr: usize) -> Self {
let attrs = self.0 & !(Self::PHYS_MASK | Self::EX_COW);
Self(((new_addr as u64) & Self::PHYS_MASK) | (attrs | Self::WRITE))
}
#[inline]
fn is_cow(self) -> bool {
self.0 & Self::EX_COW != 0
}
#[inline]
fn is_user_writable(self) -> bool {
const BITS: u64 = EntryImpl::USER | EntryImpl::WRITE;
self.0 & BITS == BITS
}
}
impl Space for SpaceImpl {
type Entry = EntryImpl;
fn alloc_empty() -> Result<&'static mut Self, Errno> {
let kernel_pdpt_phys =
unsafe { &KERNEL_FIXED.pdpt as *const _ as usize - mem::KERNEL_OFFSET };
let page = phys::alloc_page(PageUsage::Paging)?;
let res = unsafe { &mut *(mem::virtualize(page) as *mut Self) };
res.0.entries[..511].fill(EntryImpl::EMPTY);
res.0.entries[511] = EntryImpl::normal(
kernel_pdpt_phys,
MapAttributes::SHARE_OUTER
| MapAttributes::KERNEL_EXEC
| MapAttributes::KERNEL_WRITE
| MapAttributes::USER_READ
| MapAttributes::USER_WRITE,
);
Ok(res)
}
unsafe fn release(space: &'static mut Self) {
let pdpt0 = space.0.next_level_table_mut(0).unwrap();
for pdpti in 0..512 {
let pdpt_entry = pdpt0[pdpti];
if !pdpt_entry.is_present() {
continue;
}
assert!(pdpt_entry.is_normal());
let pd = &mut *(mem::virtualize(pdpt_entry.address()) as *mut TableImpl);
for pdi in 0..512 {
let pd_entry = pd[pdi];
if !pd_entry.is_present() {
continue;
}
assert!(pd_entry.is_normal());
let pt = &mut *(mem::virtualize(pd_entry.address()) as *mut TableImpl);
for pti in 0..512 {
let entry = pt[pti];
if !entry.is_present() {
continue;
}
assert!(entry.is_normal());
phys::free_page(entry.address()).unwrap();
}
phys::free_page(pd_entry.address()).unwrap();
}
phys::free_page(pdpt_entry.address()).unwrap();
}
phys::free_page(space.0[0].address()).unwrap();
}
fn fork(&mut self) -> Result<&'static mut Self, Errno> {
let res = Self::alloc_empty()?;
let pdpt0 = self.0.next_level_table_mut(0).unwrap();
for pdpti in 0..512 {
if let Some(pd) = pdpt0.next_level_table_mut(pdpti) {
for pdi in 0..512 {
if let Some(pt) = pd.next_level_table_mut(pdi) {
for pti in 0..512 {
let entry = &mut pt[pti];
let virt_addr = (pdpti << 30) | (pdi << 21) | (pti << 12);
if !entry.is_present() {
continue;
}
assert!(entry.is_normal());
let src_phys = entry.address();
// let dst_phys = phys::alloc_page(PageUsage::UserPrivate)?;
// unsafe {
// use libsys::mem::memcpy;
// memcpy(
// mem::virtualize(dst_phys) as *mut u8,
// mem::virtualize(src_phys) as *const u8,
// 4096
// );
// }
// debugln!("Clone page {:#x}", virt_addr);
// let new_entry = EntryImpl::normal(dst_phys, MapAttributes::USER_WRITE | MapAttributes::USER_READ);
// TODO check exact page usage
let dst_phys = unsafe { phys::fork_page(src_phys)? };
let new_entry = if dst_phys != src_phys {
todo!()
} else if entry.is_user_writable() {
entry.fork_with_cow()
} else {
*entry
};
unsafe {
res.write_last_level(virt_addr, new_entry, true, false)?;
intrin::flush_tlb_virt(virt_addr);
}
}
}
}
}
}
Ok(res)
}
unsafe fn write_last_level(
&mut self,
virt: usize,
entry: Self::Entry,
_create_intermediate: bool, // TODO handle this properly
overwrite: bool,
) -> Result<(), Errno> {
let l0i = virt >> 39;
let l1i = (virt >> 30) & 0x1FF;
let l2i = (virt >> 21) & 0x1FF;
let l3i = (virt >> 12) & 0x1FF;
let l0_table = self.0.next_level_table_or_alloc(l0i)?;
let l1_table = l0_table.next_level_table_or_alloc(l1i)?;
let l2_table = l1_table.next_level_table_or_alloc(l2i)?;
if l2_table[l3i].is_present() && !overwrite {
warnln!(
"Entry already exists for address: virt={:#x}, prev={:#x}, new={:#x}",
virt,
l2_table[l3i].address(),
entry.address()
);
Err(Errno::AlreadyExists)
} else {
l2_table[l3i] = entry;
intrin::flush_tlb_virt(virt);
Ok(())
}
}
fn read_last_level(&self, virt: usize) -> Result<Self::Entry, Errno> {
let l0i = virt >> 39;
let l1i = (virt >> 30) & 0x1FF;
let l2i = (virt >> 21) & 0x1FF;
let l3i = (virt >> 12) & 0x1FF;
let l0_table = self.0.next_level_table(l0i).ok_or(Errno::DoesNotExist)?;
let l1_table = l0_table.next_level_table(l1i).ok_or(Errno::DoesNotExist)?;
let l2_table = l1_table.next_level_table(l2i).ok_or(Errno::DoesNotExist)?;
let entry = l2_table[l3i];
if entry.is_present() {
Ok(entry)
} else {
Err(Errno::DoesNotExist)
}
}
}
impl TableImpl {
/// Constructs a table with no valid mappings
pub const fn empty() -> Self {
Self {
entries: [EntryImpl::EMPTY; 512],
}
}
/// Returns next-level translation table reference for `index`, if one is present.
/// If `index` represents a `Block`-type mapping, will return an error.
/// If `index` does not map to any translation table, will try to allocate, init and
/// map a new one, returning it after doing so.
pub fn next_level_table_or_alloc(&mut self, index: usize) -> Result<&'static mut Self, Errno> {
let entry = self[index];
if entry.is_present() {
if !entry.is_normal() {
return Err(Errno::InvalidArgument);
}
Ok(unsafe { &mut *(mem::virtualize(entry.address()) as *mut _) })
} else {
let phys = phys::alloc_page(PageUsage::Paging)?;
let res = unsafe { &mut *(mem::virtualize(phys) as *mut Self) };
self[index] = EntryImpl::normal(
phys,
MapAttributes::USER_WRITE
| MapAttributes::USER_READ
| MapAttributes::KERNEL_WRITE
| MapAttributes::USER_EXEC,
);
res.entries.fill(EntryImpl::EMPTY);
Ok(res)
}
}
/// Returns next-level translation table reference for `index`, if one is present.
/// Same as [next_level_table_or_alloc], but returns `None` if no table is mapped.
pub fn next_level_table(&self, index: usize) -> Option<&'static Self> {
let entry = self[index];
if entry.is_present() {
if !entry.is_normal() {
panic!("Entry is not a table: idx={}", index);
}
Some(unsafe { &*(mem::virtualize(entry.address()) as *const _) })
} else {
None
}
}
/// Returns mutable next-level translation table reference for `index`,
/// if one is present. Same as [next_level_table_or_alloc], but returns
/// `None` if no table is mapped.
pub fn next_level_table_mut(&mut self, index: usize) -> Option<&'static mut Self> {
let entry = self[index];
if entry.is_present() {
if !entry.is_normal() {
panic!("Entry is not a table: idx={}", index);
}
Some(unsafe { &mut *(mem::virtualize(entry.address()) as *mut _) })
} else {
None
}
}
}
impl Index<usize> for TableImpl {
type Output = EntryImpl;
fn index(&self, index: usize) -> &Self::Output {
&self.entries[index]
}
}
impl IndexMut<usize> for TableImpl {
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
&mut self.entries[index]
}
}
+14 -194
View File
@@ -11,98 +11,22 @@
//! * [warnln!]
//! * [errorln!]
use crate::dev::{
display::{Display, FramebufferInfo},
serial::SerialDevice,
};
use crate::font;
use crate::dev::serial::SerialDevice;
use crate::sync::IrqSafeSpinLock;
use core::convert::TryFrom;
use libsys::debug::TraceLevel;
use core::fmt;
use libsys::{debug::TraceLevel, error::Errno, mem::memcpy};
/// Currently active print level
pub static LEVEL: Level = Level::Debug;
static COLOR_MAP: [u32; 16] = [
0x000000, 0x0000AA, 0x00AA00, 0x00AAAA, 0xAA0000, 0xAA00AA, 0xAA5500, 0xAAAAAA, 0x555555,
0x5555FF, 0x55FF55, 0x55FFFF, 0xFF5555, 0xFF55FF, 0xFFFF55, 0xFFFFFF,
];
static ATTR_MAP: [usize; 10] = [0, 4, 2, 6, 1, 5, 3, 7, 7, 7];
static DISPLAY: IrqSafeSpinLock<FramebufferOutput> = IrqSafeSpinLock::new(FramebufferOutput {
display: None,
col: 0,
row: 0,
fg: 0xBBBBBB,
bg: 0x000000,
esc: EscapeState::None,
esc_argv: [0; 8],
esc_argc: 0,
});
enum EscapeState {
None,
Esc,
Data,
}
struct FramebufferOutput {
display: Option<&'static dyn Display>,
row: usize,
col: usize,
fg: u32,
bg: u32,
esc: EscapeState,
esc_argv: [usize; 8],
esc_argc: usize,
}
impl fmt::Write for FramebufferOutput {
fn write_str(&mut self, s: &str) -> fmt::Result {
if self.display.is_none() {
return Ok(());
}
let fb = self.display.unwrap().framebuffer().unwrap();
for ch in s.chars() {
self.putc(fb, ch);
}
Ok(())
}
}
/// Sets active display for debug console output
pub fn set_display(disp: &'static dyn Display) {
DISPLAY.lock().display = Some(disp);
}
/// Kernel logging levels
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug)]
#[repr(u32)]
#[derive(Clone, Copy, PartialEq)]
pub enum Level {
/// Debugging information
Debug = 1,
Debug,
/// General informational messages
Info = 2,
Info,
/// Non-critical warnings
Warn = 3,
Warn,
/// Critical errors
Error = 4,
}
impl TryFrom<u32> for Level {
type Error = Errno;
#[inline(always)]
fn try_from(l: u32) -> Result<Level, Errno> {
match l {
1 => Ok(Level::Debug),
2 => Ok(Level::Info),
3 => Ok(Level::Warn),
4 => Ok(Level::Error),
_ => Err(Errno::InvalidArgument),
}
}
Error,
}
impl From<TraceLevel> for Level {
@@ -191,119 +115,15 @@ macro_rules! errorln {
}
#[doc(hidden)]
pub fn _debug(level: Level, args: fmt::Arguments) {
pub fn _debug(_level: Level, args: fmt::Arguments) {
static LOCK: IrqSafeSpinLock<()> = IrqSafeSpinLock::new(());
use crate::arch::machine;
use fmt::Write;
if level > Level::Debug {
DISPLAY.lock().write_fmt(args).ok();
}
if level >= LEVEL {
SerialOutput {
inner: machine::console(),
}
.write_fmt(args)
.ok();
}
}
impl FramebufferOutput {
const CW: usize = 8;
const CH: usize = 12;
pub fn set_char(&mut self, fb: &FramebufferInfo, x: usize, y: usize, ch: char) {
if (x + 1) * Self::CW >= fb.width || (y + 1) * Self::CH >= fb.height {
return;
}
font::get().draw(fb, x * Self::CW, y * Self::CH, ch, self.fg, self.bg);
}
pub fn scroll(&mut self, fb: &FramebufferInfo) {
let stride = 4 * Self::CH * fb.width;
let h = fb.height / Self::CH - 1;
if self.row == h {
for y in 0..(h - 1) {
unsafe {
memcpy(
(fb.virt_base + stride * y) as *mut u8,
(fb.virt_base + (y + 1) * stride) as *const u8,
stride,
);
}
}
self.row = h - 1;
}
}
pub fn putc(&mut self, fb: &FramebufferInfo, ch: char) {
#![allow(clippy::single_match)]
match self.esc {
EscapeState::None => {
match ch {
'\x1B' => {
self.esc = EscapeState::Esc;
self.esc_argv.fill(0);
self.esc_argc = 0;
}
' '..='\x7E' => {
self.set_char(fb, self.col, self.row, ch);
// Advance the cursor
self.col += 1;
if (self.col + 1) * Self::CW >= fb.width {
self.col = 0;
self.row += 1;
}
}
'\n' => {
self.col = 0;
self.row += 1;
}
_ => {}
}
}
EscapeState::Esc => match ch {
'[' => {
self.esc = EscapeState::Data;
}
_ => {
self.esc = EscapeState::None;
}
},
EscapeState::Data => {
match ch {
'0'..='9' => {
self.esc_argv[self.esc_argc] *= 10;
self.esc_argv[self.esc_argc] += (ch as u8 - b'0') as usize;
}
';' => {
self.esc_argc += 1;
}
_ => {
self.esc_argc += 1;
self.esc = EscapeState::None;
}
}
match ch {
'm' => {
for i in 0..self.esc_argc {
let item = self.esc_argv[i];
if item / 10 == 4 {
self.bg = COLOR_MAP[ATTR_MAP[(item % 10) as usize]];
}
if item / 10 == 3 {
self.fg = COLOR_MAP[ATTR_MAP[(item % 10) as usize]];
}
}
}
_ => {}
}
}
};
self.scroll(fb);
let _lock = LOCK.lock();
SerialOutput {
inner: machine::console(),
}
.write_fmt(args)
.ok();
}
-72
View File
@@ -1,72 +0,0 @@
//! Graphical display interfaces
use crate::dev::Device;
use libsys::error::Errno;
use crate::util::InitOnce;
/// Description of a framebuffer
pub struct FramebufferInfo {
/// Width in pixels
pub width: usize,
/// Height in pixels
pub height: usize,
/// Physical start address
pub phys_base: usize,
/// Virtual address where the framebuffer is mapped
pub virt_base: usize
}
/// Generic display interface
pub trait Display: Device {
/// Changes currently active display mode
fn set_mode(&self, mode: DisplayMode) -> Result<(), Errno>;
/// Returns currently active framebuffer information
fn framebuffer(&self) -> Result<&FramebufferInfo, Errno>;
}
/// Display configuration details
#[allow(dead_code)]
pub struct DisplayMode {
width: u16,
height: u16,
}
/// Generic single-mode framebuffer
pub struct StaticFramebuffer {
framebuffer: InitOnce<FramebufferInfo>
}
impl Device for StaticFramebuffer {
fn name(&self) -> &'static str {
"Generic framebuffer device"
}
unsafe fn enable(&self) -> Result<(), Errno> {
Ok(())
}
}
impl Display for StaticFramebuffer {
fn set_mode(&self, _mode: DisplayMode) -> Result<(), Errno> {
Err(Errno::InvalidOperation)
}
fn framebuffer(&self) -> Result<&FramebufferInfo, Errno> {
if let Some(fb) = self.framebuffer.as_ref_option() {
Ok(fb)
} else {
Err(Errno::InvalidOperation)
}
}
}
impl StaticFramebuffer {
/// Constructs an empty [StaticFramebuffer] object
pub const fn uninit() -> Self {
Self { framebuffer: InitOnce::new() }
}
/// Initializes the device from existing framebuffer object
pub fn set_framebuffer(&self, framebuffer: FramebufferInfo) {
self.framebuffer.init(framebuffer);
}
}
+18 -2
View File
@@ -3,7 +3,9 @@ use crate::debug::Level;
use fdt_rs::prelude::*;
use fdt_rs::{
base::DevTree,
index::{DevTreeIndex, DevTreeIndexNode, DevTreeIndexProp},
index::{
iters::DevTreeIndexCompatibleNodeIter, DevTreeIndex, DevTreeIndexNode, DevTreeIndexProp,
},
};
use libsys::{error::Errno, path::path_component_left};
@@ -44,7 +46,7 @@ fn dump_node(level: Level, node: &INode, depth: usize) {
print!(level, "{:?} = ", name);
match name {
"compatible" => print!(level, "{:?}", prop.str().unwrap()),
"compatible" | "enable-method" => print!(level, "{:?}", prop.str().unwrap()),
"#address-cells" | "#size-cells" => print!(level, "{}", prop.u32(0).unwrap()),
"reg" => {
print!(level, "<");
@@ -115,6 +117,20 @@ impl DeviceTree {
/// Loads a device tree from physical `base` address and
/// creates an index for it
pub fn compatible<'a, 's>(&'a self, compat: &'s str) -> DevTreeIndexCompatibleNodeIter<'s, 'a, 'a, 'a> {
self.index.compatible_nodes(compat)
}
pub fn initrd(&self) -> Option<(usize, usize)> {
let chosen = self.node_by_path("/chosen")?;
let initrd_start = find_prop(chosen.clone(), "linux,initrd-start")?
.u32(0)
.ok()?;
let initrd_end = find_prop(chosen, "linux,initrd-end")?.u32(0).ok()?;
Some((initrd_start as usize, initrd_end as usize))
}
pub fn from_phys(base: usize) -> Result<DeviceTree, Errno> {
// TODO virtualize address
let tree = unsafe { DevTree::from_raw_pointer(base as *const _) }
+9 -8
View File
@@ -1,11 +1,11 @@
//! Interrupt controller and handler interfaces
use crate::arch::platform::smp::NodeAddress;
use crate::dev::Device;
use core::marker::PhantomData;
use libsys::error::Errno;
/// Token to indicate the local core is running in IRQ context
pub struct IrqContext<'irq_context> {
token: usize,
_0: PhantomData<&'irq_context ()>,
}
@@ -28,6 +28,12 @@ pub trait IntController: Device {
fn handle_pending_irqs<'irq_context>(&'irq_context self, ic: &IrqContext<'irq_context>);
}
/// Inter-processor interrupt delivery method
pub trait IpiSender: Device {
/// Raise an IPI for the target CPU mask, optionally excluding source CPU
fn send_to_mask(&self, except_self: bool, target: u32, data: u64);
}
/// Interface for peripherals capable of emitting IRQs
pub trait IntSource: Device {
/// Handles pending IRQs, if any, of this [IntSource].
@@ -46,12 +52,7 @@ impl<'q> IrqContext<'q> {
///
/// Only allowed to be constructed in top-level IRQ handlers
#[inline(always)]
pub unsafe fn new(token: usize) -> Self {
Self { token, _0: PhantomData }
}
/// Returns the value this object was initialized with
pub const fn token(&self) -> usize {
self.token
pub unsafe fn new() -> Self {
Self { _0: PhantomData }
}
}
-4
View File
@@ -3,18 +3,14 @@
use libsys::error::Errno;
// Device classes
#[cfg(target_arch = "aarch64")]
pub mod fdt;
pub mod gpio;
pub mod irq;
pub mod display;
pub mod pci;
pub mod rtc;
#[cfg(target_arch = "aarch64")]
pub mod sd;
pub mod serial;
pub mod timer;
pub mod pseudo;
pub mod tty;
/// Generic device trait
-96
View File
@@ -1,96 +0,0 @@
//! Virtual (pseudo) device implemetation
use crate::dev::Device;
use core::sync::atomic::{AtomicU32, Ordering};
use libsys::{error::Errno, ioctl::IoctlCmd};
use vfs::CharDevice;
/// Pseudorandom number generator device
pub struct Random {
state: AtomicU32,
}
/// Zero device
pub struct Zero;
impl Device for Random {
fn name(&self) -> &'static str {
"Pseudo-random device"
}
unsafe fn enable(&self) -> Result<(), Errno> {
Ok(())
}
}
impl CharDevice for Random {
fn read(&self, _blocking: bool, data: &mut [u8]) -> Result<usize, Errno> {
for byte in data.iter_mut() {
*byte = self.read_single() as u8;
}
Ok(data.len())
}
fn write(&self, _blocking: bool, _data: &[u8]) -> Result<usize, Errno> {
Ok(0)
}
fn is_ready(&self, _write: bool) -> Result<bool, Errno> {
Ok(true)
}
fn ioctl(&self, _cmd: IoctlCmd, _ptr: usize, _lim: usize) -> Result<usize, Errno> {
Err(Errno::InvalidArgument)
}
}
impl Device for Zero {
fn name(&self) -> &'static str {
"Zero device"
}
unsafe fn enable(&self) -> Result<(), Errno> {
Ok(())
}
}
impl CharDevice for Zero {
fn read(&self, _blocking: bool, data: &mut [u8]) -> Result<usize, Errno> {
data.fill(0);
Ok(data.len())
}
fn write(&self, _blocking: bool, _data: &[u8]) -> Result<usize, Errno> {
Ok(0)
}
fn is_ready(&self, _write: bool) -> Result<bool, Errno> {
Ok(true)
}
fn ioctl(&self, _cmd: IoctlCmd, _ptr: usize, _lim: usize) -> Result<usize, Errno> {
Err(Errno::InvalidArgument)
}
}
impl Random {
/// Initializes PRNG with a seed value
pub fn set_state(&self, state: u32) {
self.state.store(state, Ordering::Release);
}
/// Returns a single pseudo-random value
pub fn read_single(&self) -> u32 {
let mut x = self.state.load(Ordering::Acquire);
x ^= x << 13;
x ^= x >> 7;
x ^= x << 17;
self.state.store(x, Ordering::Release);
x
}
}
/// Pseudorandom number generator device
pub static RANDOM: Random = Random {
state: AtomicU32::new(0),
};
/// Zero device
pub static ZERO: Zero = Zero;
-1
View File
@@ -131,7 +131,6 @@ impl IntSource for Pl011 {
fn handle_irq(&self) -> Result<(), Errno> {
let inner = self.inner.get().lock();
inner.regs.ICR.write(ICR::ALL::CLEAR);
let byte = inner.regs.DR.get();
drop(inner);
+10 -14
View File
@@ -1,19 +1,16 @@
//! Teletype (TTY) device facilities
use crate::dev::serial::SerialDevice;
use crate::proc::{
wait::{Wait, WAIT_SELECT},
Process,
};
use crate::proc::{Process, wait::{Wait, WAIT_SELECT}};
use crate::sync::IrqSafeSpinLock;
use crate::syscall::arg;
use core::mem::size_of;
use libsys::error::Errno;
use libsys::{
ioctl::IoctlCmd,
termios::{Termios, TermiosIflag, TermiosLflag, TermiosOflag},
proc::Pid,
signal::Signal,
termios::{Termios, TermiosIflag, TermiosLflag, TermiosOflag},
ioctl::IoctlCmd
};
use core::mem::size_of;
use crate::syscall::arg;
#[derive(Debug)]
struct CharRingInner<const N: usize> {
@@ -55,18 +52,18 @@ pub trait TtyDevice<const N: usize>: SerialDevice {
let res = arg::struct_mut::<Termios>(ptr)?;
*res = self.ring().config.lock().clone();
Ok(size_of::<Termios>())
}
},
IoctlCmd::TtySetAttributes => {
let src = arg::struct_ref::<Termios>(ptr)?;
*self.ring().config.lock() = src.clone();
Ok(size_of::<Termios>())
}
},
IoctlCmd::TtySetPgrp => {
let src = arg::struct_ref::<u32>(ptr)?;
self.ring().inner.lock().fg_pgid = Some(Pid::try_from(*src)?);
self.ring().inner.lock().fg_pgid = Some(unsafe { Pid::from_raw(*src) });
Ok(0)
}
_ => Err(Errno::InvalidArgument),
},
_ => Err(Errno::InvalidArgument)
}
}
@@ -129,7 +126,6 @@ pub trait TtyDevice<const N: usize>: SerialDevice {
// TODO send to pgid
let proc = Process::get(pgid);
if let Some(proc) = proc {
// TODO
proc.set_signal(Signal::Interrupt);
}
}
-55
View File
@@ -1,55 +0,0 @@
//! Text drawing routines and font support
use crate::util::InitOnce;
use libsys::mem::read_le32;
use crate::dev::display::FramebufferInfo;
static FONT_DATA: &[u8] = include_bytes!("../../etc/default8x16.psfu");
static FONT: InitOnce<Font> = InitOnce::new();
/// Font data description structure
pub struct Font {
char_width: usize,
char_height: usize,
bytes_per_glyph: usize,
data: &'static [u8],
}
impl Font {
/// Renders a glyph onto the framebuffer
pub fn draw(&self, fb: &FramebufferInfo, bx: usize, by: usize, ch: char, fg: u32, bg: u32) {
if (' ' .. '\x7B').contains(&ch) {
let char_data = &self.data[ch as usize * self.bytes_per_glyph..];
for iy in 0..self.char_height {
for ix in 0..self.char_width {
let cx = self.char_width - ix - 1;
let ptr = fb.virt_base + (ix + bx + (iy + by) * fb.width) * 4;
let value = if char_data[iy + (cx) / 8] & (1 << (cx & 0x7)) != 0 {
fg
} else {
bg
};
unsafe { core::ptr::write_volatile(ptr as *mut u32, value) }
}
}
}
}
}
/// Sets up the global [Font] object from PSF
pub fn init() {
assert_eq!(read_le32(FONT_DATA), 0x864ab572);
FONT.init(Font {
char_width: read_le32(&FONT_DATA[28..]) as usize,
char_height: read_le32(&FONT_DATA[24..]) as usize,
bytes_per_glyph: read_le32(&FONT_DATA[20..]) as usize,
data: &FONT_DATA[32..]
});
}
/// Returns a reference to global [Font] object
pub fn get() -> &'static Font {
FONT.get()
}
+8 -16
View File
@@ -1,10 +1,9 @@
//! Device list pseudo-filesystem
use crate::util::InitOnce;
use core::cell::RefCell;
use alloc::boxed::Box;
use core::sync::atomic::{AtomicUsize, Ordering};
use libsys::{error::Errno, stat::FileMode};
use vfs::CharDevice;
use vfs::{Vnode, VnodeData, VnodeRef};
use libsys::{stat::FileMode, error::Errno};
use vfs::{CharDevice, CharDeviceWrapper, Vnode, VnodeKind, VnodeRef};
/// Possible character device kinds
#[derive(Debug)]
@@ -17,12 +16,7 @@ static DEVFS_ROOT: InitOnce<VnodeRef> = InitOnce::new();
/// Initializes devfs
pub fn init() {
let node = Vnode::new(
"",
VnodeData::Directory(RefCell::new(None)),
Vnode::CACHE_READDIR | Vnode::CACHE_STAT,
);
// let node = Vnode::new("", VnodeKind::Directory, Vnode::CACHE_READDIR | Vnode::CACHE_STAT);
let node = Vnode::new("", VnodeKind::Directory, Vnode::CACHE_READDIR | Vnode::CACHE_STAT);
node.props_mut().mode = FileMode::default_dir();
DEVFS_ROOT.init(node);
}
@@ -32,14 +26,12 @@ pub fn root() -> &'static VnodeRef {
DEVFS_ROOT.get()
}
/// Adds device `dev` to devfs with `name`
pub fn add_named_char_device(dev: &'static dyn CharDevice, name: &str) -> Result<(), Errno> {
fn _add_char_device(dev: &'static dyn CharDevice, name: &str) -> Result<(), Errno> {
infoln!("Add char device: {}", name);
let node = Vnode::new(name, VnodeData::Char(dev), Vnode::CACHE_STAT);
// let node = Vnode::new(name, VnodeKind::Char, Vnode::CACHE_STAT);
// node.set_data(Box::new(CharDeviceWrapper::new(dev)));
let node = Vnode::new(name, VnodeKind::Char, Vnode::CACHE_STAT);
node.props_mut().mode = FileMode::from_bits(0o600).unwrap() | FileMode::S_IFCHR;
node.set_data(Box::new(CharDeviceWrapper::new(dev)));
DEVFS_ROOT.get().attach(node);
@@ -64,5 +56,5 @@ pub fn add_char_device(dev: &'static dyn CharDevice, kind: CharDeviceType) -> Re
let name = core::str::from_utf8(&buf[..=prefix.len()]).map_err(|_| Errno::InvalidArgument)?;
add_named_char_device(dev, name)
_add_char_device(dev, name)
}
+5 -6
View File
@@ -4,11 +4,10 @@ use crate::mem::{
phys::{self, PageUsage},
};
use libsys::{error::Errno, stat::MountOptions};
use memfs::BlockAllocator;
use vfs::VnodeRef;
use memfs::BlockAllocator;
pub mod devfs;
pub mod sysfs;
/// Allocator implementation for memfs
#[derive(Clone, Copy)]
@@ -33,9 +32,9 @@ unsafe impl BlockAllocator for MemfsBlockAlloc {
pub fn create_filesystem(options: &MountOptions) -> Result<VnodeRef, Errno> {
let fs_name = options.fs.unwrap();
match fs_name {
"devfs" => Ok(devfs::root().clone()),
"sysfs" => Ok(sysfs::root().clone()),
_ => todo!(),
if fs_name == "devfs" {
Ok(devfs::root().clone())
} else {
todo!();
}
}
-232
View File
@@ -1,232 +0,0 @@
//! System control/info virtual filesystem
use crate::debug::{self, Level};
use crate::util::InitOnce;
use alloc::boxed::Box;
use core::cell::RefCell;
use core::fmt::{self, Write};
use core::str::FromStr;
use libsys::{
error::Errno,
ioctl::IoctlCmd,
stat::{FileMode, OpenFlags, Stat},
};
use vfs::{Vnode, VnodeCommon, VnodeData, VnodeFile, VnodeRef};
struct NodeData<R: Fn(&mut [u8]) -> Result<usize, Errno>, W: Fn(&[u8]) -> Result<usize, Errno>> {
read_func: R,
write_func: W,
}
struct BufferWriter<'a> {
dst: &'a mut [u8],
pos: usize,
}
impl<'a> fmt::Write for BufferWriter<'a> {
fn write_str(&mut self, s: &str) -> fmt::Result {
for byte in s.bytes() {
if self.pos == self.dst.len() {
todo!();
}
self.dst[self.pos] = byte;
self.pos += 1;
}
Ok(())
}
}
impl<'a> BufferWriter<'a> {
pub const fn new(dst: &'a mut [u8]) -> Self {
Self { dst, pos: 0 }
}
pub const fn count(&self) -> usize {
self.pos
}
}
impl<R: Fn(&mut [u8]) -> Result<usize, Errno>, W: Fn(&[u8]) -> Result<usize, Errno>> VnodeCommon
for NodeData<R, W>
{
fn open(&mut self, _node: VnodeRef, _mode: OpenFlags) -> Result<usize, Errno> {
Ok(0)
}
fn close(&mut self, _node: VnodeRef) -> Result<(), Errno> {
Ok(())
}
/// Performs filetype-specific request
fn ioctl(
&mut self,
_node: VnodeRef,
_cmd: IoctlCmd,
_ptr: usize,
_len: usize,
) -> Result<usize, Errno> {
todo!()
}
/// Retrieves file status
fn stat(&mut self, _node: VnodeRef) -> Result<Stat, Errno> {
todo!()
}
/// Reports the size of this filesystem object in bytes
fn size(&mut self, _node: VnodeRef) -> Result<usize, Errno> {
todo!()
}
/// Returns `true` if node is ready for an operation
fn ready(&mut self, _node: VnodeRef, _write: bool) -> Result<bool, Errno> {
todo!()
}
}
impl<R: Fn(&mut [u8]) -> Result<usize, Errno>, W: Fn(&[u8]) -> Result<usize, Errno>> VnodeFile
for NodeData<R, W>
{
fn read(&mut self, _node: VnodeRef, pos: usize, data: &mut [u8]) -> Result<usize, Errno> {
if pos != 0 {
// TODO handle this
Ok(0)
} else {
(self.read_func)(data)
}
}
fn write(&mut self, _node: VnodeRef, pos: usize, data: &[u8]) -> Result<usize, Errno> {
if pos != 0 {
todo!();
}
(self.write_func)(data)
}
fn truncate(&mut self, _node: VnodeRef, _size: usize) -> Result<(), Errno> {
todo!()
}
}
impl<R: Fn(&mut [u8]) -> Result<usize, Errno>, W: Fn(&[u8]) -> Result<usize, Errno>>
NodeData<R, W>
{
pub const fn new(read_func: R, write_func: W) -> Self {
Self {
read_func,
write_func,
}
}
}
static SYSFS_ROOT: InitOnce<VnodeRef> = InitOnce::new();
// TODO subdirs
fn add_generic_node<R, W>(parent: Option<VnodeRef>, name: &str, mode: FileMode, read: R, write: W)
where
R: Fn(&mut [u8]) -> Result<usize, Errno> + 'static,
W: Fn(&[u8]) -> Result<usize, Errno> + 'static,
{
let node = Vnode::new(
name,
VnodeData::File(RefCell::new(Some(Box::new(NodeData::new(read, write))))),
Vnode::CACHE_STAT,
);
node.props_mut().mode = mode | FileMode::S_IFREG;
if let Some(parent) = parent {
parent.attach(node);
} else {
SYSFS_ROOT.get().attach(node);
}
}
/// Adds a node with and `read` and `write` operations
pub fn add_read_write_node<R, W>(parent: Option<VnodeRef>, name: &str, read: R, write: W)
where
R: Fn(&mut [u8]) -> Result<usize, Errno> + 'static,
W: Fn(&[u8]) -> Result<usize, Errno> + 'static,
{
add_generic_node(
parent,
name,
FileMode::from_bits(0o600).unwrap(),
read,
write,
)
}
/// Adds `read`-only node
pub fn add_read_node<R>(parent: Option<VnodeRef>, name: &str, read: R)
where
R: Fn(&mut [u8]) -> Result<usize, Errno> + 'static,
{
add_generic_node(
parent,
name,
FileMode::from_bits(0o400).unwrap(),
read,
|_| Err(Errno::ReadOnly),
)
}
/// Creates a directory in sysfs structure
pub fn add_directory(parent: Option<VnodeRef>, name: &str) -> Result<VnodeRef, Errno> {
let node = Vnode::new(
name,
VnodeData::Directory(RefCell::new(None)),
Vnode::CACHE_READDIR | Vnode::CACHE_STAT,
);
node.props_mut().mode = FileMode::from_bits(0o500).unwrap() | FileMode::S_IFDIR;
if let Some(parent) = parent {
parent.attach(node.clone());
} else {
SYSFS_ROOT.get().attach(node.clone());
}
Ok(node)
}
/// Returns sysfs root node reference
pub fn root() -> &'static VnodeRef {
SYSFS_ROOT.get()
}
/// Sets up the sysfs tree
pub fn init() {
let node = Vnode::new(
"",
VnodeData::Directory(RefCell::new(None)),
Vnode::CACHE_READDIR | Vnode::CACHE_STAT,
);
node.props_mut().mode = FileMode::default_dir();
SYSFS_ROOT.init(node);
let debug_dir = add_directory(None, "debug").unwrap();
add_read_write_node(
Some(debug_dir),
"level",
|buf| {
let mut writer = BufferWriter::new(buf);
writeln!(&mut writer, "{}", debug::LEVEL as u32).map_err(|_| Errno::InvalidArgument)?;
Ok(writer.count())
},
|buf| {
let s = core::str::from_utf8(buf).map_err(|_| Errno::InvalidArgument)?;
let _value = u32::from_str(s)
.map_err(|_| Errno::InvalidArgument)
.and_then(Level::try_from)?;
todo!()
},
);
add_read_node(None, "uptime", |buf| {
use crate::arch::machine;
use crate::dev::timer::TimestampSource;
let mut writer = BufferWriter::new(buf);
let time = machine::local_timer().timestamp()?;
writeln!(&mut writer, "{} {}", time.as_secs(), time.subsec_nanos())
.map_err(|_| Errno::InvalidArgument)?;
Ok(writer.count())
});
}
+1 -1
View File
@@ -4,7 +4,7 @@ use crate::config::{ConfigKey, CONFIG};
use crate::fs::{devfs, MemfsBlockAlloc};
use crate::mem;
use crate::proc::{elf, Process};
use libsys::stat::{FileDescriptor, GroupId, OpenFlags, UserId};
use libsys::stat::{FileDescriptor, OpenFlags, UserId, GroupId};
use memfs::Ramfs;
use vfs::{Filesystem, Ioctx};
+21 -9
View File
@@ -1,16 +1,19 @@
//! osdve5 crate (lol)
#![feature(
asm,
global_asm,
const_for,
const_mut_refs,
const_raw_ptr_deref,
const_fn_fn_ptr_basics,
const_fn_trait_bound,
const_trait_impl,
const_btree_new,
linked_list_cursors,
const_panic,
panic_info_message,
alloc_error_handler,
asm_const,
core_intrinsics,
linked_list_cursors,
const_btree_new,
maybe_uninit_uninit_array
)]
#![no_std]
#![no_main]
@@ -26,12 +29,11 @@ extern crate alloc;
#[macro_use]
pub mod debug;
//
pub mod arch;
pub mod config;
pub mod dev;
pub mod fs;
pub mod font;
pub mod init;
pub mod mem;
pub mod proc;
@@ -42,10 +44,20 @@ pub mod util;
#[panic_handler]
fn panic_handler(pi: &core::panic::PanicInfo) -> ! {
unsafe {
arch::intrin::irq_disable();
asm!("msr daifset, #2");
use crate::arch::platform::cpu::{self, Cpu};
crate::arch::platform::smp::send_ipi(true, (1 << cpu::count()) - 1, 0);
}
errorln!("Panic: {:?}", pi);
use cortex_a::registers::MPIDR_EL1;
use tock_registers::interfaces::Readable;
errorln!("Panic on node{}: {:?}", MPIDR_EL1.get() & 0xF, pi);
// TODO
loop {}
loop {
unsafe {
asm!("wfe");
}
}
}
+1 -1
View File
@@ -59,7 +59,7 @@ static HEAP: InitOnce<IrqSafeSpinLock<Heap>> = InitOnce::new();
pub unsafe fn init(base: usize, size: usize) {
let heap = Heap { base, size, ptr: 0 };
// infoln!("Kernel heap: {:#x}..{:#x}", base, base + size);
infoln!("Kernel heap: {:#x}..{:#x}", base, base + size);
HEAP.init(IrqSafeSpinLock::new(heap));
}
-27
View File
@@ -4,16 +4,6 @@ pub mod heap;
pub mod phys;
pub mod virt;
cfg_if! {
if #[cfg(target_arch = "x86_64")] {
/// 8-byte padding for x86_64 for user stack alignment
pub const USTACK_PADDING: usize = 8;
} else {
/// No stack alignment required
pub const USTACK_PADDING: usize = 0;
}
}
/// Virtual offset applied to kernel address space
pub const KERNEL_OFFSET: usize = 0xFFFFFF8000000000;
@@ -36,20 +26,3 @@ pub fn kernel_end_phys() -> usize {
}
unsafe { &__kernel_end as *const _ as usize - KERNEL_OFFSET }
}
// TODO cross-platform variant
/// Returns `true` if `virt` address is accessible for requested operation
#[cfg(target_arch = "aarch64")]
#[inline(always)]
pub fn is_el0_accessible(virt: usize, write: bool) -> bool {
use core::arch::asm;
let mut res: usize;
unsafe {
if write {
asm!("at s1e0w, {}; mrs {}, par_el1", in(reg) virt, out(reg) res);
} else {
asm!("at s1e0r, {}; mrs {}, par_el1", in(reg) virt, out(reg) res);
}
}
res & 1 == 0
}
+3 -7
View File
@@ -1,12 +1,9 @@
use super::{PageInfo, PageStatistics, PageUsage};
use super::{PageInfo, PageUsage, PageStatistics};
use crate::mem::{virtualize, PAGE_SIZE};
use crate::sync::IrqSafeSpinLock;
use core::mem;
use libsys::{error::Errno, mem::memcpy};
/// # Safety
///
/// Unsafe to implement because of direct memory manipulation
pub unsafe trait Manager {
fn alloc_page(&mut self, pu: PageUsage) -> Result<usize, Errno>;
fn alloc_contiguous_pages(&mut self, pu: PageUsage, count: usize) -> Result<usize, Errno>;
@@ -45,7 +42,7 @@ impl SimpleManager {
kernel_heap: 0,
paging: 0,
user_private: 0,
filesystem: 0,
filesystem: 0
},
pages,
}
@@ -111,8 +108,7 @@ impl SimpleManager {
}
unsafe impl Manager for SimpleManager {
fn alloc_page(&mut self, pu: PageUsage) -> Result<usize, Errno> {
let res = self
.alloc_single_index(pu)
let res = self.alloc_single_index(pu)
.map(|r| (self.base_index + r) * PAGE_SIZE);
if res.is_ok() {
self.update_stats_alloc(pu, 1);
+2 -5
View File
@@ -9,7 +9,7 @@ mod manager;
mod reserved;
use manager::{Manager, SimpleManager, MANAGER};
pub use reserved::{ReservedRegion, reserve};
pub use reserved::ReservedRegion;
type ManagerImpl = SimpleManager;
@@ -213,10 +213,7 @@ pub unsafe fn init_from_iter<T: Iterator<Item = MemoryRegion> + Clone>(iter: T)
// Step 1. Count available memory
let mut total_pages = 0usize;
for reg in iter.clone() {
let upper = (reg.end - mem_base) / PAGE_SIZE;
if upper > total_pages {
total_pages = upper;
}
total_pages += (reg.end - reg.start) / PAGE_SIZE;
}
// TODO maybe instead of size_of::<...> use Layout?
let need_pages = ((total_pages * size_of::<PageInfo>()) + 0xFFF) / 0x1000;
@@ -1,16 +1,21 @@
use super::{table::TableImpl, EntryImpl};
use crate::mem;
use crate::mem::virt::table::{Entry, MapAttributes};
//! Fixed-size table group for device MMIO mappings
use crate::mem::{
self,
virt::{Entry, MapAttributes, Table},
};
use cortex_a::asm::barrier::{self, dsb, isb};
use libsys::error::Errno;
const DEVICE_MAP_OFFSET: usize = mem::KERNEL_OFFSET + (256usize << 30);
/// Fixed-layout group of tables describing device MMIO and kernel identity
/// mappings
#[repr(C, align(0x1000))]
pub struct FixedTableGroup {
l0: TableImpl,
l1: TableImpl,
l2: TableImpl,
l0: Table,
l1: Table,
l2: Table,
pages_4k: usize,
pages_2m: usize,
@@ -22,9 +27,9 @@ impl FixedTableGroup {
/// entries
pub const fn empty() -> Self {
Self {
l0: TableImpl::empty(),
l1: TableImpl::empty(),
l2: TableImpl::empty(),
l0: Table::empty(),
l1: Table::empty(),
l2: Table::empty(),
pages_4k: 0,
pages_2m: 1,
@@ -39,7 +44,7 @@ impl FixedTableGroup {
pub fn map_region(&mut self, phys: usize, count: usize) -> Result<usize, Errno> {
// TODO generalize region allocation
let phys_page = phys & !0xFFF;
let attrs = MapAttributes::SHARE_OUTER | MapAttributes::DEVICE_MEMORY;
let attrs = MapAttributes::SH_OUTER | MapAttributes::DEVICE | MapAttributes::ACCESS;
match count {
262144 => {
@@ -49,7 +54,7 @@ impl FixedTableGroup {
}
self.pages_1g += 1;
self.l0[count + 256] = EntryImpl::block(phys_page, attrs | MapAttributes::ACCESS);
self.l0[count + 256] = Entry::block(phys_page, attrs);
unsafe {
dsb(barrier::SY);
isb(barrier::SY);
@@ -64,7 +69,7 @@ impl FixedTableGroup {
}
self.pages_2m += 1;
self.l1[count] = EntryImpl::block(phys_page, attrs | MapAttributes::ACCESS);
self.l1[count] = Entry::block(phys_page, attrs);
unsafe {
dsb(barrier::SY);
isb(barrier::SY);
@@ -79,7 +84,7 @@ impl FixedTableGroup {
}
self.pages_4k += 1;
self.l2[count] = EntryImpl::normal(phys_page, attrs | MapAttributes::ACCESS);
self.l2[count] = Entry::table(phys_page, attrs);
unsafe {
dsb(barrier::SY);
isb(barrier::SY);
@@ -90,28 +95,13 @@ impl FixedTableGroup {
_ => unimplemented!(),
}
}
}
const DEVICE_MAP_OFFSET: usize = mem::KERNEL_OFFSET + (256usize << 30);
#[no_mangle]
static mut KERNEL_TTBR1: FixedTableGroup = FixedTableGroup::empty();
/// Allocates a range of virtual memory of requested size and maps
/// it to specified device memory
pub fn map_device_memory(phys: usize, count: usize) -> Result<usize, Errno> {
unsafe { KERNEL_TTBR1.map_region(phys, count) }
}
/// Sets up initial mappings for device-memory virtual tables.
///
/// # Safety
///
/// Only safe to be called once during virtual memory init.
pub unsafe fn init_device_map() {
let l1_phys = (&KERNEL_TTBR1.l1 as *const _) as usize - mem::KERNEL_OFFSET;
let l2_phys = (&KERNEL_TTBR1.l2 as *const _) as usize - mem::KERNEL_OFFSET;
KERNEL_TTBR1.l0[256] = Entry::normal(l1_phys, MapAttributes::empty());
KERNEL_TTBR1.l1[0] = Entry::normal(l2_phys, MapAttributes::empty());
/// Sets up initial mappings for 4K, 2M and 1G device memory page translation
pub fn init_device_map(&mut self) {
let l1_phys = (&self.l1 as *const _) as usize - mem::KERNEL_OFFSET;
let l2_phys = (&self.l2 as *const _) as usize - mem::KERNEL_OFFSET;
self.l0[256] = Entry::table(l1_phys, MapAttributes::empty());
self.l1[0] = Entry::table(l2_phys, MapAttributes::empty());
}
}
+18 -42
View File
@@ -2,13 +2,18 @@
use core::marker::PhantomData;
use core::ops::Deref;
use libsys::{mem::memcpy, error::Errno};
use crate::mem::{self, phys::{self, PageUsage}};
use cortex_a::asm::barrier::{self, dsb, isb};
use cortex_a::registers::TTBR0_EL1;
use libsys::error::Errno;
use tock_registers::interfaces::Writeable;
pub mod table;
use crate::arch::platform::virt as virt_impl;
pub use table::{Entry, MapAttributes, Space, Table};
pub mod fixed;
pub use fixed::FixedTableGroup;
use table::{Space, SpaceImpl, MapAttributes};
#[no_mangle]
static mut KERNEL_TTBR1: FixedTableGroup = FixedTableGroup::empty();
/// Structure representing a region of memory used for MMIO/device access
// TODO: this shouldn't be trivially-cloneable and should instead incorporate
@@ -40,7 +45,7 @@ impl DeviceMemory {
///
/// See [FixedTableGroup::map_region]
pub fn map(name: &'static str, phys: usize, count: usize) -> Result<Self, Errno> {
let base = virt_impl::map_device_memory(phys, count)?;
let base = unsafe { KERNEL_TTBR1.map_region(phys, count) }?;
debugln!(
"Mapping {:#x}..{:#x} -> {:#x} for {:?}",
base,
@@ -86,44 +91,15 @@ impl<T> Deref for DeviceMemoryIo<T> {
/// identity-mapped translation
pub fn enable() -> Result<(), Errno> {
unsafe {
virt_impl::enable();
KERNEL_TTBR1.init_device_map();
dsb(barrier::ISH);
isb(barrier::SY);
}
// Disable lower-half translation
TTBR0_EL1.set(0);
//TCR_EL1.modify(TCR_EL1::EPD0::SET);
Ok(())
}
/// Writes a [Copy]able object to `dst` address in `space`. Will allocate any affected pages.
///
/// # Safety
///
/// Unsafe: arbitrary memory write.
pub unsafe fn write_paged<T: Clone + Copy>(space: &mut SpaceImpl, dst: usize, src: T) -> Result<(), Errno> {
write_paged_bytes(space, dst, core::slice::from_raw_parts(&src as *const _ as *const u8, core::mem::size_of::<T>()))
}
/// Writes a byte slice to `dst` address in `space`. Will allocate any affected pages.
///
/// # Safety
///
/// Unsafe: arbitrary memory write.
pub unsafe fn write_paged_bytes(space: &mut SpaceImpl, dst: usize, src: &[u8]) -> Result<(), Errno> {
if (src.len() + (dst % 4096)) > 4096 {
todo!("Object crossed page boundary");
}
let page_virt = dst & !4095;
let page_phys = if let Ok(phys) = space.translate(dst) {
phys
} else {
let page = phys::alloc_page(PageUsage::UserPrivate)?;
let flags = MapAttributes::SHARE_OUTER | MapAttributes::USER_READ;
space.map(page_virt, page, flags)?;
page
};
memcpy(
(mem::virtualize(page_phys) + (dst % 4096)) as *mut u8,
src.as_ptr(),
src.len(),
);
Ok(())
}
+367 -132
View File
@@ -1,122 +1,254 @@
//! Translation table manipulation facilities
use crate::arch::platform::virt as virt_impl;
use crate::mem::{
self,
phys::{self, PageUsage},
};
use core::ffi::c_void;
use libsys::error::Errno;
pub use virt_impl::{EntryImpl, SpaceImpl};
use core::ops::{Index, IndexMut};
use libsys::{error::Errno, mem::memset};
/// Transparent wrapper structure representing a single
/// translation table entry
#[derive(Clone, Copy)]
#[repr(transparent)]
pub struct Entry(u64);
/// Structure describing a single level of translation mappings
#[repr(C, align(0x1000))]
pub struct Table {
entries: [Entry; 512],
}
/// Wrapper for top-most level of address translation tables
#[repr(transparent)]
pub struct Space(Table);
bitflags! {
/// Virtual space entry attributes
/// Attributes attached to each translation [Entry]
pub struct MapAttributes: u64 {
/// Entry is readable by user threads
const USER_READ = 1 << 0;
/// Entry is writable by user threads
const USER_WRITE = 1 << 1;
/// Data from entry can be executed by user threads
const USER_EXEC = 1 << 2;
// TODO use 2 lower bits to determine mapping size?
/// nG bit -- determines whether a TLB entry associated with this mapping
/// applies only to current ASID or all ASIDs.
const NOT_GLOBAL = 1 << 11;
/// AF bit -- must be set by software, otherwise Access Error exception is
/// generated when the page is accessed
const ACCESS = 1 << 10;
/// The memory region is outer-shareable
const SH_OUTER = 2 << 8;
/// This page is used for device-MMIO mapping and uses MAIR attribute #1
const DEVICE = 1 << 2;
/// Entry is writable by kernel
const KERNEL_WRITE = 1 << 3;
/// Data from entry can be executed by kernel
const KERNEL_EXEC = 1 << 4;
/// Pages marked with this bit are Copy-on-Write
const EX_COW = 1 << 55;
/// TODO TBD
const SHARE_OUTER = 1 << 5;
/// Memory is used for device interaction
const DEVICE_MEMORY = 1 << 6;
/// UXN bit -- if set, page may not be used for instruction fetching from EL0
const UXN = 1 << 54;
/// PXN bit -- if set, page may not be used for instruction fetching from EL1
const PXN = 1 << 53;
/// Entry is marked as Copy-on-Write
const COPY_ON_WRITE = 1 << 7;
/// Access flag for entry
const ACCESS = 1 << 8;
/// Entry is global across virtual address spaces
const GLOBAL = 1 << 9;
// AP field
// Default behavior is: read-write for EL1, no access for EL0
/// If set, the page referred to by this entry is read-only for both EL0/EL1
const AP_BOTH_READONLY = 3 << 6;
/// If set, the page referred to by this entry is read-write for both EL0/EL1
const AP_BOTH_READWRITE = 1 << 6;
}
}
/// Interface for a single element of paging mapping
pub trait Entry: Clone + Copy {
/// Platform-specific entry attribute representation
type RawAttributes: From<MapAttributes> + Copy + Clone;
/// Invalid entry with no association
const EMPTY: Self;
impl Table {
/// Returns next-level translation table reference for `index`, if one is present.
/// If `index` represents a `Block`-type mapping, will return an error.
/// If `index` does not map to any translation table, will try to allocate, init and
/// map a new one, returning it after doing so.
pub fn next_level_table_or_alloc(&mut self, index: usize) -> Result<&'static mut Table, Errno> {
let entry = self[index];
if entry.is_present() {
if !entry.is_table() {
return Err(Errno::InvalidArgument);
}
/// Constructs an entry pointing to next-level table or page
fn normal(addr: usize, attrs: MapAttributes) -> Self;
/// Constructs an entry pointing to a contiguous block
fn block(addr: usize, attrs: MapAttributes) -> Self;
/// Returns physical address the entry points to
fn address(self) -> usize;
/// Changes the entry physical address
fn set_address(&mut self, value: usize);
/// Marks page as CoW and removes user write ability
fn fork_with_cow(&mut self) -> Self;
/// Clones a CoW entry
fn copy_from_cow(self, new_addr: usize) -> Self;
/// Returns `true` if entry maps a paging element
fn is_present(self) -> bool;
/// Returns `true` if page is a 4KiB one
fn is_normal(self) -> bool;
/// Returns `true` if page is marked as Copy-on-Write
fn is_cow(self) -> bool;
/// Returns `true` if page is write-accessible for user threads
fn is_user_writable(self) -> bool;
}
/// Interface for virtual address space manipulation
pub trait Space {
/// Single table entry data type
type Entry: Entry;
/// Creates an empty address space
fn alloc_empty() -> Result<&'static mut Self, Errno>;
/// Removes all non-kernel entries from the space.
///
/// # Safety
///
/// Only safe to call on spaces not currently in use, otherwise will
/// trigger undefined behavior and/or page fault.
unsafe fn release(space: &'static mut Self);
/// Forks a process virtual memory space
fn fork(&mut self) -> Result<&'static mut Self, Errno>;
/// Writes an entry corresponding to `virt` address
/// to last-level table of this address space.
///
/// # Safety
///
/// Unsafe: arbitrary memory space manipulation.
unsafe fn write_last_level(
&mut self,
virt: usize,
entry: Self::Entry,
create_intermediate: bool,
overwrite: bool,
) -> Result<(), Errno>;
/// Reads an entry corresponding to `virt` address
fn read_last_level(&self, virt: usize) -> Result<Self::Entry, Errno>;
/// Returns physical address of this table
fn address_phys(&mut self) -> usize {
self as *mut _ as *mut c_void as usize - mem::KERNEL_OFFSET
Ok(unsafe { &mut *(mem::virtualize(entry.address_unchecked()) as *mut _) })
} else {
let phys = phys::alloc_page(PageUsage::Paging)?;
let res = unsafe { &mut *(mem::virtualize(phys) as *mut Self) };
self[index] = Entry::table(phys, MapAttributes::empty());
res.entries.fill(Entry::invalid());
Ok(res)
}
}
/// Performs Copy-on-Write cloning on page fault
fn try_cow_copy(&mut self, virt: usize) -> Result<(), Errno> {
let entry = self.read_last_level(virt)?;
let src_phys = entry.address();
/// Returns next-level translation table reference for `index`, if one is present.
/// Same as [next_level_table_or_alloc], but returns `None` if no table is mapped.
pub fn next_level_table(&mut self, index: usize) -> Option<&'static mut Table> {
let entry = self[index];
if entry.is_present() {
if !entry.is_table() {
panic!("Entry is not a table: idx={}", index);
}
Some(unsafe { &mut *(mem::virtualize(entry.address_unchecked()) as *mut _) })
} else {
None
}
}
/// Constructs and fills a [Table] with non-present mappings
pub const fn empty() -> Table {
Table {
entries: [Entry::invalid(); 512],
}
}
}
impl Index<usize> for Table {
type Output = Entry;
fn index(&self, index: usize) -> &Self::Output {
&self.entries[index]
}
}
impl IndexMut<usize> for Table {
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
&mut self.entries[index]
}
}
impl Entry {
const PRESENT: u64 = 1 << 0;
const TABLE: u64 = 1 << 1;
const PHYS_MASK: u64 = 0x0000FFFFFFFFF000;
/// Constructs a single non-present mapping
pub const fn invalid() -> Self {
Self(0)
}
/// Constructs a `Block`-type memory mapping
pub const fn block(phys: usize, attrs: MapAttributes) -> Self {
Self((phys as u64 & Self::PHYS_MASK) | attrs.bits() | Self::PRESENT)
}
/// Constructs a `Table` or `Page`-type mapping depending on translation level
/// this entry is used at
pub const fn table(phys: usize, attrs: MapAttributes) -> Self {
Self((phys as u64 & Self::PHYS_MASK) | attrs.bits() | Self::PRESENT | Self::TABLE)
}
/// Returns `true` if this entry is not invalid
pub const fn is_present(self) -> bool {
self.0 & Self::PRESENT != 0
}
/// Returns `true` if this entry is a `Table` or `Page`-type mapping
pub const fn is_table(self) -> bool {
self.0 & Self::TABLE != 0
}
/// Returns the target address of this translation entry.
///
/// # Safety
///
/// Does not check if the entry is actually valid.
pub const unsafe fn address_unchecked(self) -> usize {
(self.0 & Self::PHYS_MASK) as usize
}
unsafe fn set_address(&mut self, address: usize) {
self.0 &= !Self::PHYS_MASK;
self.0 |= (address as u64) & Self::PHYS_MASK;
}
unsafe fn fork_flags(self) -> MapAttributes {
MapAttributes::from_bits_unchecked(self.0 & !Self::PHYS_MASK)
}
fn set_cow(&mut self) {
self.0 |= (MapAttributes::AP_BOTH_READONLY | MapAttributes::EX_COW).bits();
}
fn clear_cow(&mut self) {
self.0 &= !(MapAttributes::AP_BOTH_READONLY | MapAttributes::EX_COW).bits();
self.0 |= MapAttributes::AP_BOTH_READWRITE.bits();
}
#[inline]
fn is_cow(self) -> bool {
let attrs = (MapAttributes::AP_BOTH_READONLY | MapAttributes::EX_COW).bits();
self.0 & attrs == attrs
}
}
impl Space {
/// Creates a new virtual address space and fills it with [Entry::invalid()]
/// mappings. Does physical memory page allocation.
pub fn alloc_empty() -> Result<&'static mut Self, Errno> {
let phys = phys::alloc_page(PageUsage::Paging)?;
let res = unsafe { &mut *(mem::virtualize(phys) as *mut Self) };
res.0.entries.fill(Entry::invalid());
Ok(res)
}
/// Inserts a single `virt` -> `phys` translation entry to this address space.
///
/// TODO: only works with 4K-sized pages at this moment.
pub fn map(&mut self, virt: usize, phys: usize, flags: MapAttributes) -> Result<(), Errno> {
let l0i = virt >> 30;
let l1i = (virt >> 21) & 0x1FF;
let l2i = (virt >> 12) & 0x1FF;
let l1_table = self.0.next_level_table_or_alloc(l0i)?;
let l2_table = l1_table.next_level_table_or_alloc(l1i)?;
if l2_table[l2i].is_present() {
Err(Errno::AlreadyExists)
} else {
l2_table[l2i] = Entry::table(phys, flags | MapAttributes::ACCESS);
#[cfg(feature = "verbose")]
debugln!("{:#p} Map {:#x} -> {:#x}, {:?}", self, virt, phys, flags);
Ok(())
}
}
/// Translates a virtual address into a corresponding physical one.
///
/// Only works for 4K pages atm.
// TODO extract attributes
pub fn translate(&mut self, virt: usize) -> Result<usize, Errno> {
let l0i = virt >> 30;
let l1i = (virt >> 21) & 0x1FF;
let l2i = (virt >> 12) & 0x1FF;
let l1_table = self.0.next_level_table(l0i).ok_or(Errno::DoesNotExist)?;
let l2_table = l1_table.next_level_table(l1i).ok_or(Errno::DoesNotExist)?;
let entry = l2_table[l2i];
if entry.is_present() {
Ok(unsafe { entry.address_unchecked() })
} else {
Err(Errno::DoesNotExist)
}
}
/// Attempts to resolve a page fault at `virt` address by copying the
/// underlying Copy-on-Write mapping (if any is present)
pub fn try_cow_copy(&mut self, virt: usize) -> Result<(), Errno> {
let virt = virt & !0xFFF;
let l0i = virt >> 30;
let l1i = (virt >> 21) & 0x1FF;
let l2i = (virt >> 12) & 0x1FF;
let l1_table = self.0.next_level_table(l0i).ok_or(Errno::DoesNotExist)?;
let l2_table = l1_table.next_level_table(l1i).ok_or(Errno::DoesNotExist)?;
let entry = l2_table[l2i];
if !entry.is_present() {
warnln!("Entry is not present: {:#x}", virt);
return Err(Errno::DoesNotExist);
}
let src_phys = unsafe { entry.address_unchecked() };
if !entry.is_cow() {
warnln!(
"Entry is not marked as CoW: {:#x}, points to {:#x}",
@@ -127,46 +259,17 @@ pub trait Space {
}
let dst_phys = unsafe { phys::copy_cow_page(src_phys)? };
unsafe {
self.write_last_level(virt, entry.copy_from_cow(dst_phys), false, true)?;
l2_table[l2i].set_address(dst_phys);
}
Ok(())
}
l2_table[l2i].clear_cow();
/// Creates a new virtual -> physical memory mapping. Will fail if one is
/// already associated with given virtual address.
fn map(&mut self, virt: usize, phys: usize, attrs: MapAttributes) -> Result<(), Errno> {
#[cfg(feature = "verbose")]
debugln!("Map {:#x} -> {:#x}, {:?}", virt, phys, attrs);
unsafe {
self.write_last_level(
virt,
Entry::normal(phys, attrs | MapAttributes::ACCESS),
true,
false,
)
}
}
/// Returns a virtual address physical mapping destination
fn translate(&mut self, virt: usize) -> Result<usize, Errno> {
self.read_last_level(virt).map(Entry::address)
}
/// Releases memory from virtual address range `start`..`start + len * 0x1000`
fn free(&mut self, start: usize, len: usize) -> Result<(), Errno> {
for i in 0..len {
unsafe {
self.write_last_level(start + i * 0x1000, Self::Entry::EMPTY, false, true)?;
}
}
Ok(())
}
/// Allocates a contiguous region from the address space and maps
/// physical pages to it
fn allocate(
pub fn allocate(
&mut self,
start: usize,
end: usize,
@@ -189,4 +292,136 @@ pub trait Space {
}
Err(Errno::OutOfMemory)
}
/// Removes a single 4K page mapping from the table and
/// releases the underlying physical memory
pub fn unmap_single(&mut self, page: usize) -> Result<(), Errno> {
let l0i = page >> 30;
let l1i = (page >> 21) & 0x1FF;
let l2i = (page >> 12) & 0x1FF;
let l1_table = self.0.next_level_table(l0i).ok_or(Errno::DoesNotExist)?;
let l2_table = l1_table.next_level_table(l1i).ok_or(Errno::DoesNotExist)?;
let entry = l2_table[l2i];
if !entry.is_present() {
return Err(Errno::DoesNotExist);
}
let phys = unsafe { entry.address_unchecked() };
unsafe {
phys::free_page(phys)?;
}
l2_table[l2i] = Entry::invalid();
unsafe {
asm!("tlbi vaae1, {}", in(reg) page);
}
// TODO release paging structure memory
Ok(())
}
/// Releases a range of virtual pages and their corresponding physical pages
pub fn free(&mut self, start: usize, len: usize) -> Result<(), Errno> {
for i in 0..len {
self.unmap_single(start + i * 0x1000)?;
}
Ok(())
}
/// Performs a copy of the address space, cloning data owned by it
pub fn fork(&mut self) -> Result<&'static mut Self, Errno> {
let res = Self::alloc_empty()?;
for l0i in 0..512 {
if let Some(l1_table) = self.0.next_level_table(l0i) {
for l1i in 0..512 {
if let Some(l2_table) = l1_table.next_level_table(l1i) {
for l2i in 0..512 {
let entry = l2_table[l2i];
if !entry.is_present() {
continue;
}
assert!(entry.is_table());
let src_phys = unsafe { entry.address_unchecked() };
let virt_addr = (l0i << 30) | (l1i << 21) | (l2i << 12);
let dst_phys = unsafe { phys::fork_page(src_phys)? };
let mut flags = unsafe { entry.fork_flags() };
if dst_phys != src_phys {
todo!();
// res.map(virt_addr, dst_phys, flags)?;
} else {
let writable = flags & MapAttributes::AP_BOTH_READONLY
== MapAttributes::AP_BOTH_READWRITE;
if writable {
flags |=
MapAttributes::AP_BOTH_READONLY | MapAttributes::EX_COW;
l2_table[l2i].set_cow();
unsafe {
asm!("tlbi vaae1, {}", in(reg) virt_addr);
}
}
res.map(virt_addr, dst_phys, flags)?;
}
}
}
}
}
}
Ok(res)
}
/// Releases all the mappings from the address space. Frees all
/// memory pages referenced by this space as well as those used for
/// its paging tables.
///
/// # Safety
///
/// Unsafe: may invalidate currently active address space
pub unsafe fn release(space: &mut Self) {
for l0i in 0..512 {
let l0_entry = space.0[l0i];
if !l0_entry.is_present() {
continue;
}
assert!(l0_entry.is_table());
let l1_table = &mut *(mem::virtualize(l0_entry.address_unchecked()) as *mut Table);
for l1i in 0..512 {
let l1_entry = l1_table[l1i];
if !l1_entry.is_present() {
continue;
}
assert!(l1_entry.is_table());
let l2_table = &mut *(mem::virtualize(l1_entry.address_unchecked()) as *mut Table);
for l2i in 0..512 {
let entry = l2_table[l2i];
if !entry.is_present() {
continue;
}
assert!(entry.is_table());
phys::free_page(entry.address_unchecked()).unwrap();
}
phys::free_page(l1_entry.address_unchecked()).unwrap();
}
phys::free_page(l0_entry.address_unchecked()).unwrap();
}
memset(space as *mut Space as *mut u8, 0, 4096);
}
/// Returns the physical address of this structure
pub fn address_phys(&mut self) -> usize {
(self as *mut _ as usize) - mem::KERNEL_OFFSET
}
}
+17 -21
View File
@@ -2,12 +2,12 @@
use crate::mem::{
self,
phys::{self, PageUsage},
virt::table::{MapAttributes, Space, SpaceImpl},
virt::{MapAttributes, Space},
};
use core::mem::{size_of, MaybeUninit};
use libsys::{
error::Errno,
traits::{Read, Seek, SeekDir},
traits::{Read, Seek, SeekDir}
};
use vfs::FileRef;
@@ -66,10 +66,10 @@ struct Phdr<E: Elf> {
}
fn map_flags(elf_flags: usize) -> MapAttributes {
let mut dst_flags = MapAttributes::SHARE_OUTER;
let mut dst_flags = MapAttributes::NOT_GLOBAL | MapAttributes::SH_OUTER;
if elf_flags & (1 << 0) /* PF_X */ != 0 {
dst_flags |= MapAttributes::USER_EXEC;
if elf_flags & (1 << 0) /* PF_X */ == 0 {
dst_flags |= MapAttributes::UXN | MapAttributes::PXN;
}
match (elf_flags & (3 << 1)) >> 1 {
@@ -78,9 +78,9 @@ fn map_flags(elf_flags: usize) -> MapAttributes {
// Write-only: not sure if such mapping should exist at all
1 => todo!(),
// Read-only
2 => dst_flags |= MapAttributes::USER_READ,
2 => dst_flags |= MapAttributes::AP_BOTH_READONLY,
// Read+Write
3 => dst_flags |= MapAttributes::USER_WRITE | MapAttributes::USER_READ,
3 => dst_flags |= MapAttributes::AP_BOTH_READWRITE,
_ => unreachable!(),
};
@@ -88,7 +88,7 @@ fn map_flags(elf_flags: usize) -> MapAttributes {
}
unsafe fn load_bytes<F>(
space: &mut SpaceImpl,
space: &mut Space,
dst_virt: usize,
mut read: F,
size: usize,
@@ -107,17 +107,15 @@ where
let page_off = (dst_page_off + off) % mem::PAGE_SIZE;
let count = core::cmp::min(rem, mem::PAGE_SIZE - page_off);
let page = if let Ok(page) = space.translate(dst_page + page_idx * mem::PAGE_SIZE) {
page
} else {
let page = phys::alloc_page(PageUsage::UserPrivate)?;
let page = phys::alloc_page(PageUsage::UserPrivate)?;
// TODO fetch existing mapping and test flag equality instead
// if flags differ, bail out
space.map(dst_page + page_idx * mem::PAGE_SIZE, page, map_flags(flags))?;
page
};
// TODO fetch existing mapping and test flag equality instead
// if flags differ, bail out
if let Err(e) = space.map(dst_page + page_idx * mem::PAGE_SIZE, page, map_flags(flags)) {
if e != Errno::AlreadyExists {
return Err(e);
}
}
let dst_page_virt = mem::virtualize(page + page_off);
let dst = core::slice::from_raw_parts_mut(dst_page_virt as *mut u8, count);
@@ -148,7 +146,7 @@ unsafe fn read_struct<T>(src: &FileRef, pos: usize) -> Result<T, Errno> {
}
/// Loads an ELF program from `source` into target `space`
pub fn load_elf(space: &mut SpaceImpl, source: FileRef) -> Result<usize, Errno> {
pub fn load_elf(space: &mut Space, source: FileRef) -> Result<usize, Errno> {
let ehdr: Ehdr<Elf64> = unsafe { read_struct(&source, 0).unwrap() };
if &ehdr.ident[0..4] != b"\x7FELF" {
@@ -171,7 +169,6 @@ pub fn load_elf(space: &mut SpaceImpl, source: FileRef) -> Result<usize, Errno>
);
if phdr.filesz > 0 {
debugln!("Load bytes {:#x}..{:#x}", phdr.vaddr, phdr.vaddr + phdr.filesz);
unsafe {
load_bytes(
space,
@@ -193,7 +190,6 @@ pub fn load_elf(space: &mut SpaceImpl, source: FileRef) -> Result<usize, Errno>
if phdr.memsz > phdr.filesz {
let len = (phdr.memsz - phdr.filesz) as usize;
debugln!("Zero bytes {:#x}..{:#x}", phdr.vaddr + phdr.filesz, phdr.vaddr + phdr.memsz);
unsafe {
load_bytes(
space,
+5 -15
View File
@@ -1,10 +1,7 @@
//! Process file descriptors and I/O context
use alloc::collections::BTreeMap;
use libsys::{
error::Errno,
stat::{FileDescriptor, GroupId, UserId},
};
use vfs::{FileRef, Ioctx, VnodeRef};
use libsys::{error::Errno, stat::{FileDescriptor, UserId, GroupId}};
use vfs::{FileRef, Ioctx, VnodeRef, VnodeKind};
/// Process I/O context. Contains file tables, root/cwd info etc.
pub struct ProcessIo {
@@ -27,7 +24,7 @@ impl ProcessIo {
/// Sets controlling terminal for the process
pub fn set_ctty(&mut self, node: VnodeRef) {
// assert_eq!(node.kind(), VnodeKind::Char);
assert_eq!(node.kind(), VnodeKind::Char);
self.ctty = Some(node);
}
@@ -77,11 +74,7 @@ impl ProcessIo {
}
/// Clones a file descriptor into an available slot or, if specified, requested one
pub fn duplicate_file(
&mut self,
src: FileDescriptor,
dst: Option<FileDescriptor>,
) -> Result<FileDescriptor, Errno> {
pub fn duplicate_file(&mut self, src: FileDescriptor, dst: Option<FileDescriptor>) -> Result<FileDescriptor, Errno> {
let file_ref = self.file(src)?;
if let Some(dst) = dst {
let idx = u32::from(dst);
@@ -98,10 +91,7 @@ impl ProcessIo {
/// Returns [File] struct referred to by file descriptor `idx`
pub fn file(&mut self, fd: FileDescriptor) -> Result<FileRef, Errno> {
self.files
.get(&u32::from(fd))
.cloned()
.ok_or(Errno::InvalidFile)
self.files.get(&u32::from(fd)).cloned().ok_or(Errno::InvalidFile)
}
/// Returns [Ioctx] structure reference of this I/O context
+87 -25
View File
@@ -2,13 +2,19 @@
use crate::init;
use crate::sync::IrqSafeSpinLock;
use alloc::collections::BTreeMap;
use libsys::proc::{Pid, Tid};
use crate::mem;
use alloc::{
boxed::Box,
collections::{BTreeMap},
};
use core::sync::atomic::{AtomicUsize, Ordering};
use crate::arch::platform::cpu::{self, Cpu};
use libsys::proc::Pid;
pub mod elf;
pub mod thread;
pub use thread::{Thread, ThreadRef, State as ThreadState};
pub(self) use thread::Context;
pub use thread::{State as ThreadState, Thread, ThreadRef};
pub mod process;
pub use process::{Process, ProcessRef, ProcessState};
pub mod io;
@@ -18,35 +24,91 @@ pub mod wait;
pub mod sched;
pub use sched::Scheduler;
pub(self) use sched::SCHED;
//pub(self) use sched::SCHED;
/// Performs a task switch.
///
/// See [Scheduler::switch]
pub fn switch() {
SCHED.switch(false);
//<<<<<<< HEAD
// <<<<<<< HEAD
// macro_rules! spawn {
// (fn ($dst_arg:ident : usize) $body:block, $src_arg:expr) => {{
// #[inline(never)]
// extern "C" fn __inner_func($dst_arg : usize) -> ! {
// let __res = $body;
// {
// #![allow(unreachable_code)]
// SCHED.current_process().exit(__res);
// panic!();
// }
// }
//
// let __proc = $crate::proc::Process::new_kernel(__inner_func, $src_arg).unwrap();
// $crate::proc::SCHED.enqueue(__proc.id());
// }};
//
// (fn () $body:block) => (spawn!(fn (_arg: usize) $body, 0usize))
// }
///// Performs a task switch.
/////
///// See [Scheduler::switch]
//pub fn switch() {
// SCHED.switch(false);
//}
// ///
// pub fn process(id: Pid) -> ProcessRef {
// PROCESSES.lock().get(&id).unwrap().clone()
// }
macro_rules! spawn {
(fn ($dst_arg:ident : usize) $body:block, $src_arg:expr) => {{
#[inline(never)]
extern "C" fn __inner_func($dst_arg : usize) -> ! {
let __res = $body;
{
todo!();
// #![allow(unreachable_code)]
// SCHED.current_process().exit(__res);
panic!();
}
}
let __proc = $crate::proc::Process::new_kernel(__inner_func, $src_arg).unwrap();
$crate::proc::sched::enqueue(__proc.id());
}};
(fn () $body:block) => (spawn!(fn (_arg: usize) $body, 0usize))
}
#[no_mangle]
extern "C" fn sched_yield() {
SCHED.switch(false);
}
// /// Global list of all processes in the system
// // =======
// /// Performs a task switch.
// ///
// /// See [Scheduler::switch]
// pub fn switch() {
// SCHED.switch(false);
// }
// >>>>>>> feat/thread
pub(self) static PROCESSES: IrqSafeSpinLock<BTreeMap<Pid, ProcessRef>> =
IrqSafeSpinLock::new(BTreeMap::new());
pub(self) static THREADS: IrqSafeSpinLock<BTreeMap<Tid, ThreadRef>> =
pub(self) static THREADS: IrqSafeSpinLock<BTreeMap<u32, ThreadRef>> =
IrqSafeSpinLock::new(BTreeMap::new());
/// Sets up initial process and enters it.
///
/// See [Scheduler::enter]
///
/// # Safety
///
/// Unsafe: May only be called once.
pub unsafe fn enter() -> ! {
SCHED.init();
Process::new_kernel(init::init_fn, 0).unwrap().enqueue();
SCHED.enter();
pub unsafe fn enter(is_bsp: bool) -> ! {
static COUNTER: AtomicUsize = AtomicUsize::new(0);
let sched = Cpu::get().scheduler();
sched.init();
COUNTER.fetch_add(1, Ordering::Release);
while COUNTER.load(Ordering::Acquire) != cpu::count() {
cortex_a::asm::nop();
}
if is_bsp {
Process::new_kernel(init::init_fn, 0).unwrap().enqueue();
}
sched.enter();
}
+270 -133
View File
@@ -1,18 +1,19 @@
//! Process data and control
use crate::arch::aarch64::exception::ExceptionFrame;
use crate::mem::{
self,
phys::{self, PageUsage},
virt::{write_paged, write_paged_bytes, table::{MapAttributes, Space, SpaceImpl}},
virt::{MapAttributes, Space},
};
use crate::proc::{
wait::Wait, Context, ProcessIo, Thread, ThreadRef, ThreadState, Tid, PROCESSES, SCHED,
wait::Wait, Context, ProcessIo, Thread, ThreadRef, ThreadState, PROCESSES, sched,
};
use crate::arch::{intrin, platform::ForkFrame};
use crate::sync::{IrqSafeSpinLock, IrqSafeSpinLockGuard};
use crate::sync::IrqSafeSpinLock;
use alloc::{rc::Rc, vec::Vec};
use core::sync::atomic::{AtomicU32, Ordering};
use libsys::{
error::Errno,
mem::memcpy,
proc::{ExitCode, Pid},
signal::Signal,
ProgramArgs,
@@ -31,14 +32,14 @@ pub enum ProcessState {
}
struct ProcessInner {
space: Option<&'static mut SpaceImpl>,
space: Option<&'static mut Space>,
state: ProcessState,
id: Pid,
pgid: Pid,
ppid: Option<Pid>,
sid: Pid,
exit: Option<ExitCode>,
threads: Vec<Tid>,
threads: Vec<u32>,
}
/// Structure describing an operating system process
@@ -53,7 +54,7 @@ pub struct Process {
impl Process {
const USTACK_VIRT_TOP: usize = 0x100000000;
const USTACK_PAGES: usize = 8;
const USTACK_PAGES: usize = 4;
/// Returns the process ID
#[inline]
@@ -99,80 +100,11 @@ impl Process {
#[inline]
pub fn manipulate_space<R, F>(&self, f: F) -> R
where
F: FnOnce(&mut SpaceImpl) -> R,
F: FnOnce(&mut Space) -> R,
{
f(self.inner.lock().space.as_mut().unwrap())
}
/// Handles all pending signals (when returning from aborted syscall)
pub fn handle_pending_signals(&self) {
let mut lock = self.inner.lock();
let table = Self::space_phys(&mut lock);
let main_thread = Thread::get(lock.threads[0]).unwrap();
drop(lock);
loop {
let state = self.signal_state.load(Ordering::Acquire);
if let Some(signal) = Self::find1(state).map(|e| Signal::try_from(e as u32).unwrap()) {
self.signal_state.fetch_and(!(1 << (signal as u32)), Ordering::Release);
main_thread.clone().enter_signal(signal, table);
} else {
break;
}
}
}
/// Sets a pending signal for process
pub fn set_signal(&self, signal: Signal) {
let mut lock = self.inner.lock();
let table = Self::space_phys(&mut lock);
let main_thread = Thread::get(lock.threads[0]).unwrap();
drop(lock);
// TODO check that `signal` is not a fault signal
// it is illegal to call this function with
// fault signals
match main_thread.state() {
ThreadState::Running => {
main_thread.enter_signal(signal, table);
}
ThreadState::Waiting => {
self.signal_state.fetch_or(1 << (signal as u32), Ordering::Release);
main_thread.interrupt_wait(true);
}
ThreadState::Ready => {
main_thread.clone().setup_signal(signal, table);
main_thread.interrupt_wait(false);
}
ThreadState::Finished => {
// TODO report error back
todo!()
}
}
}
/// Immediately delivers a signal to requested thread
pub fn enter_fault_signal(&self, thread: ThreadRef, signal: Signal) {
let mut lock = self.inner.lock();
let table = Self::space_phys(&mut lock);
drop(lock);
thread.enter_signal(signal, table);
}
/// Crates a new thread in the process
pub fn new_user_thread(&self, entry: usize, stack: usize, arg: usize) -> Result<Tid, Errno> {
let mut lock = self.inner.lock();
let table = Self::space_phys(&mut lock);
let thread = Thread::new_user(lock.id, entry, stack, arg, table)?;
let tid = thread.id();
lock.threads.push(tid);
SCHED.enqueue(tid);
Ok(tid)
}
/// Creates a new kernel process
pub fn new_kernel(entry: extern "C" fn(usize) -> !, arg: usize) -> Result<ProcessRef, Errno> {
let id = new_kernel_pid();
@@ -205,7 +137,7 @@ impl Process {
pub fn enqueue(&self) {
let inner = self.inner.lock();
for &tid in inner.threads.iter() {
SCHED.enqueue(tid);
sched::enqueue(tid);
}
}
@@ -214,28 +146,158 @@ impl Process {
PROCESSES.lock().get(&pid).cloned()
}
fn find1(a: u32) -> Option<usize> {
for i in 0..32 {
if a & (1 << i) != 0 {
return Some(i);
}
}
None
// <<<<<<< HEAD
// /// Schedules an initial thread for execution
// ///
// /// # Safety
// ///
// /// Unsafe: only allowed to be called once, repeated calls
// /// will generate undefined behavior
// pub unsafe fn enter(cpu: u32, proc: ProcessRef) -> ! {
// // FIXME use some global lock to guarantee atomicity of thread entry?
// proc.inner.lock().state = State::Running;
// proc.cpu.store(cpu, Ordering::SeqCst);
// let ctx = proc.ctx.get();
// // I don't think this is bad: process can't be dropped fully unless
// // it's been reaped (and this function won't run for such process)
// // drop(proc);
// (&mut *ctx).enter()
// }
// =======
/// Sets a pending signal for a process
pub fn set_signal(&self, signal: Signal) {
todo!();
// let mut lock = self.inner.lock();
// let ttbr0 = lock.space.as_mut().unwrap().address_phys() | ((lock.id.asid() as usize) << 48);
// let main_thread = Thread::get(lock.threads[0]).unwrap();
// drop(lock);
// // TODO check that `signal` is not a fault signal
// // it is illegal to call this function with
// // fault signals
// match main_thread.state() {
// ThreadState::Running => {
// main_thread.enter_signal(signal, ttbr0);
// }
// ThreadState::Waiting => {
// main_thread.clone().setup_signal(signal, ttbr0);
// main_thread.interrupt_wait(true);
// }
// ThreadState::Ready => {
// main_thread.clone().setup_signal(signal, ttbr0);
// main_thread.interrupt_wait(false);
// }
// ThreadState::Finished => {
// // TODO report error back
// todo!()
// }
// }
}
fn space_phys(lock: &mut IrqSafeSpinLockGuard<ProcessInner>) -> usize {
lock.space.as_mut().unwrap().address_phys() | ((lock.id.asid() as usize) << 48)
/// Immediately delivers a signal to requested thread
pub fn enter_fault_signal(&self, thread: ThreadRef, signal: Signal) {
todo!();
// let mut lock = self.inner.lock();
// let ttbr0 = lock.space.as_mut().unwrap().address_phys() | ((lock.id.asid() as usize) << 48);
// thread.enter_signal(signal, ttbr0);
}
// /// Schedules a next thread for execution
// ///
// /// # Safety
// ///
// /// Unsafe:
// ///
// /// * Does not ensure src and dst threads are not the same thread
// /// * Does not ensure src is actually current context
// pub unsafe fn switch(cpu: u32, src: ProcessRef, dst: ProcessRef, discard: bool) {
// {
// let mut src_lock = src.inner.lock();
// let mut dst_lock = dst.inner.lock();
// if !discard {
// assert_eq!(src_lock.state, State::Running);
// src_lock.state = State::Ready;
// }
// assert!(dst_lock.state == State::Ready || dst_lock.state == State::Waiting);
// dst_lock.state = State::Running;
// src.cpu.store(Self::CPU_NONE, Ordering::SeqCst);
// dst.cpu.store(cpu, Ordering::SeqCst);
// }
// let src_ctx = src.ctx.get();
// let dst_ctx = dst.ctx.get();
// // See "drop" note in Process::enter()
// // drop(src);
// // drop(dst);
// (&mut *src_ctx).switch(&mut *dst_ctx);
// }
// /// Suspends current process with a "waiting" status
// pub fn enter_wait(&self) {
// let drop = {
// let mut lock = self.inner.lock();
// let drop = lock.state == State::Running;
// lock.state = State::Waiting;
// sched::dequeue(lock.id);
// // SCHED.dequeue(lock.id);
// drop
// };
// if drop {
// sched::switch(true);
// // todo!();
// // SCHED.switch(true);
// }
// }
/// Crates a new thread in the process
pub fn new_user_thread(&self, entry: usize, stack: usize, arg: usize) -> Result<u32, Errno> {
let mut lock = self.inner.lock();
let space_phys = lock.space.as_mut().unwrap().address_phys();
let ttbr0 = space_phys | ((lock.id.asid() as usize) << 48);
let thread = Thread::new_user(lock.id, entry, stack, arg, ttbr0)?;
let tid = thread.id();
lock.threads.push(tid);
sched::enqueue(tid);
Ok(tid)
}
// /// Creates a new kernel process
// pub fn new_kernel(entry: extern "C" fn(usize) -> !, arg: usize) -> Result<ProcessRef, Errno> {
// let id = Pid::new_kernel();
// let res = Rc::new(Self {
// ctx: UnsafeCell::new(Context::kernel(entry as usize, arg)),
// io: IrqSafeSpinLock::new(ProcessIo::new()),
// exit_wait: Wait::new(),
// inner: IrqSafeSpinLock::new(ProcessInner {
// id,
// exit: None,
// space: None,
// wait_flag: false,
// state: State::Ready,
// }),
// cpu: AtomicU32::new(Self::CPU_NONE),
// });
// debugln!("New kernel process: {}", id);
// assert!(PROCESSES.lock().insert(id, res.clone()).is_none());
// Ok(res)
// }
/// Creates a "fork" of the process, cloning its address space and
/// resources
pub fn fork(&self, frame: &mut ForkFrame) -> Result<Pid, Errno> {
pub fn fork(&self, frame: &mut ExceptionFrame) -> Result<Pid, Errno> {
let src_io = self.io.lock();
let mut src_inner = self.inner.lock();
let dst_id = new_user_pid();
let dst_space = src_inner.space.as_mut().unwrap().fork()?;
let dst_space_phys = (dst_space as *mut _ as usize) - mem::KERNEL_OFFSET;
let dst_ttbr0 = dst_space_phys | ((dst_id.asid() as usize) << 48);
@@ -262,7 +324,8 @@ impl Process {
debugln!("Process {:?} forked into {:?}", src_inner.id, dst_id);
assert!(PROCESSES.lock().insert(dst_id, dst).is_none());
SCHED.enqueue(tid);
sched::enqueue(tid);
// SCHED.enqueue(dst_id);
Ok(dst_id)
}
@@ -279,18 +342,15 @@ impl Process {
lock.state = ProcessState::Finished;
for &tid in lock.threads.iter() {
let thread = Thread::get(tid).unwrap();
if thread.state() == ThreadState::Waiting {
todo!()
}
thread.terminate(status);
SCHED.dequeue(tid);
Thread::get(tid).unwrap().terminate(status);
sched::dequeue(tid);
// SCHED.dequeue(tid);
}
if let Some(space) = lock.space.take() {
unsafe {
SpaceImpl::release(space);
intrin::flush_tlb_asid((lock.id.asid() as usize) << 48);
Space::release(space);
asm!("tlbi aside1, {}", in(reg) ((lock.id.asid() as usize) << 48));
}
}
@@ -303,7 +363,7 @@ impl Process {
self.exit_wait.wakeup_all();
if is_running {
SCHED.switch(true);
sched::switch(true);
panic!("This code should never run");
}
}
@@ -327,8 +387,9 @@ impl Process {
lock.threads.retain(|&e| e != tid);
thread.terminate(status);
SCHED.dequeue(tid);
debugln!("Thread {:?} terminated", tid);
todo!();
// SCHED.dequeue(tid);
debugln!("Thread {} terminated", tid);
switch
};
@@ -336,7 +397,8 @@ impl Process {
if switch {
// TODO retain thread ID in process "finished" list and
// drop it when process finishes
SCHED.switch(true);
// SCHED.switch(true);
todo!();
panic!("This code should not run");
} else {
// Can drop this thread: it's not running
@@ -372,16 +434,68 @@ impl Process {
}
}
fn store_arguments(space: &mut SpaceImpl, argv: &[&str]) -> Result<usize, Errno> {
fn write_paged<T>(space: &mut Space, dst: usize, src: T) -> Result<(), Errno> {
let size = core::mem::size_of::<T>();
if (size + (dst % 4096)) > 4096 {
todo!("Object crossed page boundary");
}
let page_virt = dst & !4095;
let page_phys = if let Ok(phys) = space.translate(dst) {
phys
} else {
let page = phys::alloc_page(PageUsage::UserPrivate)?;
let flags = MapAttributes::SH_OUTER
| MapAttributes::NOT_GLOBAL
| MapAttributes::UXN
| MapAttributes::PXN
| MapAttributes::AP_BOTH_READONLY;
space.map(page_virt, page, flags)?;
page
};
unsafe {
core::ptr::write((mem::virtualize(page_phys) + (dst % 4096)) as *mut T, src);
}
Ok(())
}
fn write_paged_bytes(space: &mut Space, dst: usize, src: &[u8]) -> Result<(), Errno> {
if (src.len() + (dst % 4096)) > 4096 {
todo!("Object crossed page boundary");
}
let page_virt = dst & !4095;
let page_phys = if let Ok(phys) = space.translate(dst) {
phys
} else {
let page = phys::alloc_page(PageUsage::UserPrivate)?;
let flags = MapAttributes::SH_OUTER
| MapAttributes::NOT_GLOBAL
| MapAttributes::UXN
| MapAttributes::PXN
| MapAttributes::AP_BOTH_READONLY;
space.map(page_virt, page, flags)?;
page
};
unsafe {
memcpy(
(mem::virtualize(page_phys) + (dst % 4096)) as *mut u8,
src.as_ptr(),
src.len(),
);
}
Ok(())
}
fn store_arguments(space: &mut Space, argv: &[&str]) -> Result<usize, Errno> {
let mut offset = 0usize;
// TODO vmalloc?
let base = 0x60000000;
// 1. Store program argument string bytes
for arg in argv.iter() {
unsafe {
write_paged_bytes(space, base + offset, arg.as_bytes())?;
}
Self::write_paged_bytes(space, base + offset, arg.as_bytes())?;
offset += arg.len();
}
// Align
@@ -392,10 +506,8 @@ impl Process {
let mut data_offset = 0usize;
for arg in argv.iter() {
// XXX this is really unsafe and I am not really sure ABI will stay like this XXX
unsafe {
write_paged(space, base + offset, base + data_offset)?;
write_paged(space, base + offset + 8, arg.len())?;
}
Self::write_paged(space, base + offset + 0, base + data_offset)?;
Self::write_paged(space, base + offset + 8, arg.len())?;
offset += 16;
data_offset += arg.len();
}
@@ -407,33 +519,55 @@ impl Process {
storage: base,
size: offset + core::mem::size_of::<ProgramArgs>(),
};
unsafe {
write_paged(space, base + offset, data)?;
}
Self::write_paged(space, base + offset, data)?;
Ok(base + offset)
}
/// Returns the process's address space ID
pub fn asid(&self) -> usize {
(self.id().asid() as usize) << 48
}
/// Loads a new program into current process address space
pub fn execve<F: FnOnce(&mut SpaceImpl) -> Result<usize, Errno>>(
loader: F,
argv: &[&str],
) -> Result<(), Errno> {
unsafe {
// Run with interrupts disabled
intrin::irq_disable();
}
/// Loads a new program into current process address space
pub fn execve<F: FnOnce(&mut Space) -> Result<usize, Errno>>(
loader: F,
argv: &[&str],
) -> Result<(), Errno> {
unsafe {
// Run with interrupts disabled
asm!("msr daifset, #2");
}
// <<<<<<< HEAD
// let proc = sched::current_process();
// let mut lock = proc.inner.lock();
// if lock.id.is_kernel() {
// let mut proc_lock = PROCESSES.lock();
// let old_pid = lock.id;
// assert!(
// proc_lock.remove(&old_pid).is_some(),
// "Failed to downgrade kernel process (remove kernel pid)"
// );
// lock.id = Pid::new_user();
// debugln!(
// "Process downgrades from kernel to user: {} -> {}",
// old_pid,
// lock.id
// );
// assert!(proc_lock.insert(lock.id, proc.clone()).is_none());
// unsafe {
// use crate::arch::platform::cpu::Cpu;
// Cpu::get().scheduler().hack_current_pid(lock.id);
// }
// } else {
// // Invalidate user ASID
// let input = (lock.id.asid() as usize) << 48;
// unsafe {
// asm!("tlbi aside1, {}", in(reg) input);
// }
// =======
let proc = Process::current();
let mut process_lock = proc.inner.lock();
if process_lock.threads.len() != 1 {
todo!();
// >>>>>>> feat/thread
}
let thread = Thread::get(process_lock.threads[0]).unwrap();
@@ -457,15 +591,17 @@ impl Process {
proc.io.lock().handle_cloexec();
let new_space = SpaceImpl::alloc_empty()?;
let new_space = Space::alloc_empty()?;
let new_space_phys = (new_space as *mut _ as usize) - mem::KERNEL_OFFSET;
let ustack_virt_bottom = Self::USTACK_VIRT_TOP - Self::USTACK_PAGES * mem::PAGE_SIZE;
for i in 0..Self::USTACK_PAGES {
let page = phys::alloc_page(PageUsage::UserPrivate).unwrap();
let flags = MapAttributes::SHARE_OUTER
| MapAttributes::USER_READ
| MapAttributes::USER_WRITE;
let flags = MapAttributes::SH_OUTER
| MapAttributes::NOT_GLOBAL
| MapAttributes::UXN
| MapAttributes::PXN
| MapAttributes::AP_BOTH_READWRITE;
new_space
.map(ustack_virt_bottom + i * mem::PAGE_SIZE, page, flags)
.unwrap();
@@ -474,6 +610,7 @@ impl Process {
let entry = loader(new_space)?;
let arg = Self::store_arguments(new_space, argv)?;
debugln!("Will now enter at {:#x}", entry);
// TODO drop old address space
process_lock.space = Some(new_space);
@@ -481,13 +618,13 @@ impl Process {
// TODO drop old context
let ctx = thread.ctx.get();
let asid = (process_lock.id.asid() as usize) << 48;
intrin::flush_tlb_asid(asid);
asm!("tlbi aside1, {}", in(reg) asid);
ctx.write(Context::user(
entry,
arg,
new_space_phys | asid,
Self::USTACK_VIRT_TOP - mem::USTACK_PADDING,
Self::USTACK_VIRT_TOP,
));
drop(process_lock);

Some files were not shown because too many files have changed in this diff Show More