2022-08-04 09:03:44 -05:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2022, Umut İnan Erdoğan <umutinanerdogan@pm.me>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
|
|
|
*/
|
|
|
|
|
2022-08-04 05:19:05 -05:00
|
|
|
use alloc::sync::Arc;
|
2022-08-03 07:45:49 -05:00
|
|
|
use hashbrown::HashMap;
|
2022-04-11 15:51:54 -05:00
|
|
|
use spin::Lazy;
|
2022-01-13 08:54:33 -06:00
|
|
|
|
2022-08-03 12:30:05 -05:00
|
|
|
use crate::{
|
2022-08-04 09:03:44 -05:00
|
|
|
filesystem::{vfs::FsNode, StorageDevice},
|
2022-08-03 12:30:05 -05:00
|
|
|
handle::{Handle, HandleResource},
|
|
|
|
};
|
2022-08-03 07:45:49 -05:00
|
|
|
|
2022-04-11 15:51:54 -05:00
|
|
|
pub static KERNEL_STATE: Lazy<spin::Mutex<KernelInternalState>> =
|
|
|
|
Lazy::new(|| spin::Mutex::new(KernelInternalState::new()));
|
2022-01-13 08:54:33 -06:00
|
|
|
|
|
|
|
pub struct KernelInternalState {
|
2022-02-19 07:17:44 -06:00
|
|
|
pub hostname: String,
|
2022-08-03 07:45:49 -05:00
|
|
|
storage_devices: HashMap<Handle, Box<dyn StorageDevice>>,
|
2022-08-04 05:19:05 -05:00
|
|
|
// FIXME: should this be per-process?
|
|
|
|
file_table: HashMap<Handle, FileTableEntry>,
|
2022-01-13 08:54:33 -06:00
|
|
|
should_shutdown: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl KernelInternalState {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self {
|
|
|
|
should_shutdown: false,
|
2022-08-03 07:45:49 -05:00
|
|
|
storage_devices: HashMap::new(),
|
2022-08-04 05:19:05 -05:00
|
|
|
file_table: HashMap::new(),
|
2022-01-27 01:37:12 -06:00
|
|
|
hostname: "".to_string(),
|
2022-01-13 08:54:33 -06:00
|
|
|
}
|
|
|
|
}
|
2022-01-27 01:37:12 -06:00
|
|
|
|
|
|
|
pub fn set_hostname(&mut self, hostname: String) {
|
|
|
|
self.hostname = hostname;
|
|
|
|
}
|
2022-08-03 07:45:49 -05:00
|
|
|
|
2022-08-04 09:03:44 -05:00
|
|
|
pub fn add_storage_device(&mut self, device: impl StorageDevice + Send + 'static) {
|
|
|
|
let device = Box::new(device);
|
|
|
|
self.storage_devices.insert(device.device_handle(), device);
|
2022-08-03 07:45:49 -05:00
|
|
|
}
|
|
|
|
|
2022-08-04 09:03:44 -05:00
|
|
|
pub fn storage_device(&self, handle: Handle) -> Option<&dyn StorageDevice> {
|
2022-08-03 07:45:49 -05:00
|
|
|
self.storage_devices.get(&handle).map(|d| &**d)
|
|
|
|
}
|
|
|
|
|
2022-08-04 05:19:05 -05:00
|
|
|
// TODO: implement flags here
|
|
|
|
pub fn open_file_descriptor(&mut self, fs_node: Arc<FsNode>) -> Handle {
|
2022-08-03 13:16:18 -05:00
|
|
|
let handle = Handle::new(HandleResource::FileDescriptor);
|
2022-08-04 05:19:05 -05:00
|
|
|
self.file_table.insert(handle, FileTableEntry::new(fs_node));
|
2022-08-03 13:16:18 -05:00
|
|
|
handle
|
|
|
|
}
|
|
|
|
|
2022-08-04 09:03:44 -05:00
|
|
|
pub fn file_descriptor(&self, handle: Handle) -> Option<&FileTableEntry> {
|
2022-08-04 05:19:05 -05:00
|
|
|
self.file_table.get(&handle)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn close_file_descriptor(&mut self, handle: Handle) {
|
|
|
|
self.file_table.remove(&handle);
|
2022-08-04 01:01:34 -05:00
|
|
|
}
|
|
|
|
|
2022-01-13 08:54:33 -06:00
|
|
|
pub fn shutdown(&mut self) {
|
|
|
|
self.should_shutdown = true;
|
|
|
|
}
|
2022-08-03 07:45:49 -05:00
|
|
|
|
2022-01-13 08:54:33 -06:00
|
|
|
pub fn update_state(&mut self) {
|
|
|
|
if self.should_shutdown {
|
|
|
|
crate::arch::shutdown();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-04-11 16:07:01 -05:00
|
|
|
|
|
|
|
impl Default for KernelInternalState {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::new()
|
|
|
|
}
|
|
|
|
}
|
2022-08-04 05:19:05 -05:00
|
|
|
|
|
|
|
pub struct FileTableEntry {
|
|
|
|
fs_node: Arc<FsNode>,
|
|
|
|
// TODO: permissions, flags, owner, maybe cache stuff here?
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FileTableEntry {
|
|
|
|
fn new(fs_node: Arc<FsNode>) -> Self {
|
2022-08-04 05:56:37 -05:00
|
|
|
Self { fs_node }
|
2022-08-04 05:19:05 -05:00
|
|
|
}
|
2022-08-04 09:03:44 -05:00
|
|
|
|
|
|
|
pub fn fs_node(&self) -> Arc<FsNode> {
|
|
|
|
self.fs_node.clone()
|
|
|
|
}
|
2022-08-04 05:19:05 -05:00
|
|
|
}
|