Add context and file structs
This commit is contained in:
parent
49739d47e8
commit
4e270bb807
5
Makefile
5
Makefile
|
@ -14,7 +14,10 @@ qemu: build/harddrive.bin
|
|||
|
||||
FORCE:
|
||||
|
||||
build/libkernel.a: FORCE
|
||||
build:
|
||||
mkdir build
|
||||
|
||||
build/libkernel.a: build FORCE
|
||||
cargo rustc -- --crate-type staticlib -o $@
|
||||
#--target $(ARCH)-unknown-none.json
|
||||
|
||||
|
|
|
@ -4,14 +4,14 @@ static mut INTERRUPTS_ENABLED: bool = false;
|
|||
|
||||
/// Clear interrupts
|
||||
#[inline(always)]
|
||||
pub unsafe fn clear_interrupts() {
|
||||
pub unsafe fn disable_interrupts() {
|
||||
println!("CLEAR INTERRUPTS");
|
||||
INTERRUPTS_ENABLED = false;
|
||||
}
|
||||
|
||||
/// Set interrupts
|
||||
#[inline(always)]
|
||||
pub unsafe fn set_interrupts() {
|
||||
pub unsafe fn enable_interrupts() {
|
||||
println!("SET INTERRUPTS");
|
||||
INTERRUPTS_ENABLED = true;
|
||||
}
|
||||
|
|
|
@ -2,13 +2,13 @@
|
|||
|
||||
/// Clear interrupts
|
||||
#[inline(always)]
|
||||
pub unsafe fn clear_interrupts() {
|
||||
pub unsafe fn disable_interrupts() {
|
||||
asm!("cli" : : : : "intel", "volatile");
|
||||
}
|
||||
|
||||
/// Set interrupts
|
||||
#[inline(always)]
|
||||
pub unsafe fn set_interrupts() {
|
||||
pub unsafe fn enable_interrupts() {
|
||||
asm!("sti" : : : : "intel", "volatile");
|
||||
}
|
||||
|
||||
|
|
10
kernel/context/file.rs
Normal file
10
kernel/context/file.rs
Normal file
|
@ -0,0 +1,10 @@
|
|||
//! File struct
|
||||
|
||||
/// A file
|
||||
#[derive(Copy, Clone, Debug)]
|
||||
pub struct File {
|
||||
/// The scheme that this file refers to
|
||||
pub scheme: usize,
|
||||
/// The number the scheme uses to refer to this file
|
||||
pub number: usize,
|
||||
}
|
34
kernel/context/mod.rs
Normal file
34
kernel/context/mod.rs
Normal file
|
@ -0,0 +1,34 @@
|
|||
//! Context management
|
||||
|
||||
/// File operations
|
||||
pub mod file;
|
||||
|
||||
/// Context list
|
||||
pub static mut CONTEXT: Context = Context::new();
|
||||
|
||||
/// A context, which identifies either a process or a thread
|
||||
#[derive(Copy, Clone, Debug)]
|
||||
pub struct Context {
|
||||
/// The open files in the scheme
|
||||
pub files: [Option<file::File>; 32]
|
||||
}
|
||||
|
||||
impl Context {
|
||||
pub const fn new() -> Context {
|
||||
Context {
|
||||
files: [None; 32]
|
||||
}
|
||||
}
|
||||
|
||||
/// Add a file to the lowest available slot.
|
||||
/// Return the file descriptor number or None if no slot was found
|
||||
pub fn add_file(&mut self, file: file::File) -> Option<usize> {
|
||||
for (i, mut file_option) in self.files.iter_mut().enumerate() {
|
||||
if file_option.is_none() {
|
||||
*file_option = Some(file);
|
||||
return Some(i);
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
}
|
|
@ -64,6 +64,7 @@
|
|||
//! An error will be returned, `ENOBUFS`, if the buffer is not long enough for the name.
|
||||
//! In this case, it is recommended to add one page, 4096 bytes, to the buffer and retry.
|
||||
|
||||
#![feature(const_fn)]
|
||||
#![feature(lang_items)]
|
||||
#![feature(question_mark)]
|
||||
#![no_std]
|
||||
|
@ -71,7 +72,7 @@
|
|||
#[macro_use]
|
||||
extern crate bitflags;
|
||||
|
||||
use arch::interrupt::{set_interrupts, halt};
|
||||
use arch::interrupt::{enable_interrupts, halt};
|
||||
|
||||
/// Architecture specific items (test)
|
||||
#[cfg(test)]
|
||||
|
@ -83,6 +84,9 @@ extern crate arch_test as arch;
|
|||
#[macro_use]
|
||||
extern crate arch_x86_64 as arch;
|
||||
|
||||
/// Context management
|
||||
pub mod context;
|
||||
|
||||
/// Intrinsics for panic handling
|
||||
pub mod panic;
|
||||
|
||||
|
@ -97,10 +101,15 @@ pub mod tests;
|
|||
pub extern fn kmain() {
|
||||
println!("TEST");
|
||||
|
||||
println!("{:?}", syscall::open(b"file:/test/file", 0));
|
||||
println!("{:?}", syscall::open(b"stdin", 0));
|
||||
println!("{:?}", syscall::open(b"stdout", 0));
|
||||
println!("{:?}", syscall::open(b"stderr", 0));
|
||||
|
||||
unsafe { set_interrupts() };
|
||||
loop {
|
||||
unsafe { halt() };
|
||||
unsafe {
|
||||
enable_interrupts();
|
||||
halt();
|
||||
}
|
||||
println!("{:?}", syscall::write(1, b"HALT\n"));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
//! Filesystem syscalls
|
||||
|
||||
use super::Result;
|
||||
use super::{Error, Result};
|
||||
|
||||
/// Read syscall
|
||||
pub fn read(fd: usize, buf: &mut [u8]) -> Result<usize> {
|
||||
|
@ -11,13 +11,25 @@ pub fn read(fd: usize, buf: &mut [u8]) -> Result<usize> {
|
|||
/// Write syscall
|
||||
pub fn write(fd: usize, buf: &[u8]) -> Result<usize> {
|
||||
println!("Write {}: {}", fd, buf.len());
|
||||
Ok(0)
|
||||
if let Some(file) = unsafe { &mut ::context::CONTEXT }.files.get(fd) {
|
||||
println!("{:?}: {:?}", file, ::core::str::from_utf8(buf));
|
||||
Ok(buf.len())
|
||||
} else {
|
||||
Err(Error::BadFile)
|
||||
}
|
||||
}
|
||||
|
||||
/// Open syscall
|
||||
pub fn open(path: &[u8], flags: usize) -> Result<usize> {
|
||||
println!("Open {:?}: {:X}", ::core::str::from_utf8(path), flags);
|
||||
Ok(0)
|
||||
if let Some(fd) = unsafe { &mut ::context::CONTEXT }.add_file(::context::file::File {
|
||||
scheme: 0,
|
||||
number: 0
|
||||
}) {
|
||||
Ok(fd)
|
||||
} else {
|
||||
Err(Error::TooManyFiles)
|
||||
}
|
||||
}
|
||||
|
||||
/// Close syscall
|
||||
|
|
|
@ -53,8 +53,12 @@ pub enum Error {
|
|||
NotPermitted,
|
||||
/// No such file or directory
|
||||
NoEntry,
|
||||
/// Bad file number
|
||||
BadFile,
|
||||
/// Invalid argument
|
||||
InvalidValue,
|
||||
/// Too many open files
|
||||
TooManyFiles,
|
||||
/// Syscall not implemented
|
||||
NoCall
|
||||
}
|
||||
|
@ -66,7 +70,9 @@ impl From<Error> for usize {
|
|||
match err {
|
||||
Error::NotPermitted => 1,
|
||||
Error::NoEntry => 2,
|
||||
Error::BadFile => 9,
|
||||
Error::InvalidValue => 22,
|
||||
Error::TooManyFiles => 24,
|
||||
Error::NoCall => 38
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
use arch::interrupt::{set_interrupts, halt};
|
||||
use arch::interrupt::{enable_interrupts, halt};
|
||||
|
||||
#[test]
|
||||
fn halt_with_interrupts() {
|
||||
unsafe {
|
||||
set_interrupts();
|
||||
enable_interrupts();
|
||||
halt();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue