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:
|
FORCE:
|
||||||
|
|
||||||
build/libkernel.a: FORCE
|
build:
|
||||||
|
mkdir build
|
||||||
|
|
||||||
|
build/libkernel.a: build FORCE
|
||||||
cargo rustc -- --crate-type staticlib -o $@
|
cargo rustc -- --crate-type staticlib -o $@
|
||||||
#--target $(ARCH)-unknown-none.json
|
#--target $(ARCH)-unknown-none.json
|
||||||
|
|
||||||
|
|
|
@ -4,14 +4,14 @@ static mut INTERRUPTS_ENABLED: bool = false;
|
||||||
|
|
||||||
/// Clear interrupts
|
/// Clear interrupts
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub unsafe fn clear_interrupts() {
|
pub unsafe fn disable_interrupts() {
|
||||||
println!("CLEAR INTERRUPTS");
|
println!("CLEAR INTERRUPTS");
|
||||||
INTERRUPTS_ENABLED = false;
|
INTERRUPTS_ENABLED = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set interrupts
|
/// Set interrupts
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub unsafe fn set_interrupts() {
|
pub unsafe fn enable_interrupts() {
|
||||||
println!("SET INTERRUPTS");
|
println!("SET INTERRUPTS");
|
||||||
INTERRUPTS_ENABLED = true;
|
INTERRUPTS_ENABLED = true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,13 +2,13 @@
|
||||||
|
|
||||||
/// Clear interrupts
|
/// Clear interrupts
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub unsafe fn clear_interrupts() {
|
pub unsafe fn disable_interrupts() {
|
||||||
asm!("cli" : : : : "intel", "volatile");
|
asm!("cli" : : : : "intel", "volatile");
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set interrupts
|
/// Set interrupts
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub unsafe fn set_interrupts() {
|
pub unsafe fn enable_interrupts() {
|
||||||
asm!("sti" : : : : "intel", "volatile");
|
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.
|
//! 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.
|
//! In this case, it is recommended to add one page, 4096 bytes, to the buffer and retry.
|
||||||
|
|
||||||
|
#![feature(const_fn)]
|
||||||
#![feature(lang_items)]
|
#![feature(lang_items)]
|
||||||
#![feature(question_mark)]
|
#![feature(question_mark)]
|
||||||
#![no_std]
|
#![no_std]
|
||||||
|
@ -71,7 +72,7 @@
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate bitflags;
|
extern crate bitflags;
|
||||||
|
|
||||||
use arch::interrupt::{set_interrupts, halt};
|
use arch::interrupt::{enable_interrupts, halt};
|
||||||
|
|
||||||
/// Architecture specific items (test)
|
/// Architecture specific items (test)
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
@ -83,6 +84,9 @@ extern crate arch_test as arch;
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate arch_x86_64 as arch;
|
extern crate arch_x86_64 as arch;
|
||||||
|
|
||||||
|
/// Context management
|
||||||
|
pub mod context;
|
||||||
|
|
||||||
/// Intrinsics for panic handling
|
/// Intrinsics for panic handling
|
||||||
pub mod panic;
|
pub mod panic;
|
||||||
|
|
||||||
|
@ -97,10 +101,15 @@ pub mod tests;
|
||||||
pub extern fn kmain() {
|
pub extern fn kmain() {
|
||||||
println!("TEST");
|
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 {
|
loop {
|
||||||
unsafe { halt() };
|
unsafe {
|
||||||
|
enable_interrupts();
|
||||||
|
halt();
|
||||||
|
}
|
||||||
|
println!("{:?}", syscall::write(1, b"HALT\n"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
//! Filesystem syscalls
|
//! Filesystem syscalls
|
||||||
|
|
||||||
use super::Result;
|
use super::{Error, Result};
|
||||||
|
|
||||||
/// Read syscall
|
/// Read syscall
|
||||||
pub fn read(fd: usize, buf: &mut [u8]) -> Result<usize> {
|
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
|
/// Write syscall
|
||||||
pub fn write(fd: usize, buf: &[u8]) -> Result<usize> {
|
pub fn write(fd: usize, buf: &[u8]) -> Result<usize> {
|
||||||
println!("Write {}: {}", fd, buf.len());
|
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
|
/// Open syscall
|
||||||
pub fn open(path: &[u8], flags: usize) -> Result<usize> {
|
pub fn open(path: &[u8], flags: usize) -> Result<usize> {
|
||||||
println!("Open {:?}: {:X}", ::core::str::from_utf8(path), flags);
|
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
|
/// Close syscall
|
||||||
|
|
|
@ -53,8 +53,12 @@ pub enum Error {
|
||||||
NotPermitted,
|
NotPermitted,
|
||||||
/// No such file or directory
|
/// No such file or directory
|
||||||
NoEntry,
|
NoEntry,
|
||||||
|
/// Bad file number
|
||||||
|
BadFile,
|
||||||
/// Invalid argument
|
/// Invalid argument
|
||||||
InvalidValue,
|
InvalidValue,
|
||||||
|
/// Too many open files
|
||||||
|
TooManyFiles,
|
||||||
/// Syscall not implemented
|
/// Syscall not implemented
|
||||||
NoCall
|
NoCall
|
||||||
}
|
}
|
||||||
|
@ -66,7 +70,9 @@ impl From<Error> for usize {
|
||||||
match err {
|
match err {
|
||||||
Error::NotPermitted => 1,
|
Error::NotPermitted => 1,
|
||||||
Error::NoEntry => 2,
|
Error::NoEntry => 2,
|
||||||
|
Error::BadFile => 9,
|
||||||
Error::InvalidValue => 22,
|
Error::InvalidValue => 22,
|
||||||
|
Error::TooManyFiles => 24,
|
||||||
Error::NoCall => 38
|
Error::NoCall => 38
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
use arch::interrupt::{set_interrupts, halt};
|
use arch::interrupt::{enable_interrupts, halt};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn halt_with_interrupts() {
|
fn halt_with_interrupts() {
|
||||||
unsafe {
|
unsafe {
|
||||||
set_interrupts();
|
enable_interrupts();
|
||||||
halt();
|
halt();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue