diff --git a/Cargo.toml b/Cargo.toml index be0b516..318d0c8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,7 +4,8 @@ resolver = "2" members = [ "os/kernel", "os/application/hello", - "os/application/shell" + "os/application/shell", + "os/application/uptime" ] # [profile.release] diff --git a/Makefile.toml b/Makefile.toml index 3b4b8f7..d922345 100644 --- a/Makefile.toml +++ b/Makefile.toml @@ -23,7 +23,7 @@ dependencies = [ "link_members", "initrd" ] [tasks.initrd] cwd = "${INITRD_DIRECTORY}" command = "tar" -args = [ "-cf", "${BOOTLOADER_DIRECTORY}/initrd.tar", "hello", "shell" ] +args = [ "-cf", "${BOOTLOADER_DIRECTORY}/initrd.tar", "hello", "shell", "uptime" ] dependencies = [ "link_members" ] # Cleanup tasks diff --git a/os/application/uptime/Cargo.toml b/os/application/uptime/Cargo.toml new file mode 100644 index 0000000..00fa7c0 --- /dev/null +++ b/os/application/uptime/Cargo.toml @@ -0,0 +1,15 @@ +[package] +edition = "2021" +name = "uptime" +version = "0.1.0" +authors = ["Michael Schöttner , Fabian Ruhland "] + +[lib] +crate-type = ["staticlib"] +path = "src/uptime.rs" + +[dependencies] +# Local dependencies +runtime = { path = "../../library/runtime" } +io = { path = "../../library/io" } +time = { path = "../../library/time" } \ No newline at end of file diff --git a/os/application/uptime/Makefile.toml b/os/application/uptime/Makefile.toml new file mode 100644 index 0000000..e1a6dfe --- /dev/null +++ b/os/application/uptime/Makefile.toml @@ -0,0 +1,42 @@ +[env.development] +CARGO_CFG_TARGET_FAMILY = "${CARGO_MAKE_WORKSPACE_WORKING_DIRECTORY}/d3os_application.json" +BUILD_DIRECTORY = "${CARGO_MAKE_CRATE_TARGET_DIRECTORY}/d3os_application/debug" +CARGO_BUILD_OPTION = "--lib" + +[env.production] +CARGO_CFG_TARGET_FAMILY = "${CARGO_MAKE_WORKSPACE_WORKING_DIRECTORY}/d3os_application.json" +BUILD_DIRECTORY = "${CARGO_MAKE_CRATE_TARGET_DIRECTORY}/d3os_application/release" +CARGO_BUILD_OPTION = "--release" + +[env] +CARGO_MAKE_EXTEND_WORKSPACE_MAKEFILE = true +RUST_TARGET_PATH = "${CARGO_MAKE_WORKING_DIRECTORY}" +SOURCE_DIRECOTRY = "${CARGO_MAKE_WORKING_DIRECTORY}/src" +LINKER_FILE = "${CARGO_MAKE_WORKSPACE_WORKING_DIRECTORY}/os/application/link.ld" +RUST_OBJECT = "${BUILD_DIRECTORY}/lib${CARGO_MAKE_PROJECT_NAME}.a" +APPLICATION = "${INITRD_DIRECTORY}/${CARGO_MAKE_PROJECT_NAME}" + +# Build tasks + +[tasks.default] +alias = "link" + +[tasks.compile] +command = "cargo" +args = [ "build", "-Z", "build-std=core,alloc", "-Z", "build-std-features=compiler-builtins-mem", "--target", "${CARGO_CFG_TARGET_FAMILY}", "${CARGO_BUILD_OPTION}" ] + +[tasks.link] +command = "ld" +args = [ "-n", "-T", "${LINKER_FILE}", "-o", "${APPLICATION}", "${RUST_OBJECT}" ] +dependencies = [ "compile" ] + +# Cleanup tasks + +[tasks.clean] +command = "cargo" +args = [ "clean" ] +dependencies = [ "remove-application" ] + +[tasks.remove-application] +command = "rm" +args = [ "-f", "${APPLICATION}" ] diff --git a/os/application/uptime/src/uptime.rs b/os/application/uptime/src/uptime.rs new file mode 100644 index 0000000..137abf2 --- /dev/null +++ b/os/application/uptime/src/uptime.rs @@ -0,0 +1,27 @@ +#![no_std] + +extern crate alloc; + +#[allow(unused_imports)] +use runtime::*; +use io::{print, println}; +use time::systime; + +#[no_mangle] +pub fn main() { + let systime = systime(); + + if systime.num_seconds() < 60 { + println!("{}", systime.num_seconds()); + } else if systime.num_seconds() < 3600 { + println!("{}:{:0>2}", systime.num_minutes(), systime.num_seconds() % 60); + } else if systime.num_seconds() < 86400 { + let seconds = systime.num_seconds() - (systime.num_minutes() * 60); + println!("{}:{:0>2}:{:0>2}", systime.num_hours(), systime.num_minutes() % 60, seconds); + } else { + let days = systime.num_days(); + let minutes= systime.num_minutes() - (systime.num_hours() % 60); + let seconds = systime.num_seconds() - (systime.num_minutes() * 60); + println!("{} {}, {}:{:0>2}:{:0>2}", days, if days == 1 { "day" } else { "days" }, systime.num_hours(), minutes, seconds); + } +} \ No newline at end of file diff --git a/os/kernel/Cargo.toml b/os/kernel/Cargo.toml index 129978f..ecd5725 100644 --- a/os/kernel/Cargo.toml +++ b/os/kernel/Cargo.toml @@ -24,7 +24,7 @@ multiboot2 = "0.19.0" ps2 = "0.2.0" pc-keyboard = "0.7.0" anstyle-parse = "0.2.3" -chrono = { version = "0.4.32", default-features = false, features = ["alloc"] } +chrono = { version = "0.4.34", default-features = false, features = ["alloc"] } nolock = { version = "0.4.1", default-features = false, features = ["queues"] } acpi = "5.0.0" x2apic = "0.4.3" diff --git a/os/kernel/src/syscall/mod.rs b/os/kernel/src/syscall/mod.rs index 9045bee..5417efb 100644 --- a/os/kernel/src/syscall/mod.rs +++ b/os/kernel/src/syscall/mod.rs @@ -2,7 +2,7 @@ use alloc::rc::Rc; use core::ptr::slice_from_raw_parts; use core::str::from_utf8; use x86_64::structures::paging::PageTableFlags; -use crate::{initrd, scheduler, terminal}; +use crate::{initrd, scheduler, terminal, timer}; use crate::memory::{MemorySpace, PAGE_SIZE}; use crate::memory::r#virtual::{VirtualMemoryArea, VmaType}; use crate::process::process::current_process; @@ -80,4 +80,9 @@ pub extern "C" fn sys_application_start(name_buffer: *const u8, name_length: usi } None => 0 } +} + +#[no_mangle] +pub extern "C" fn sys_get_system_time() -> usize { + timer().read().systime_ms() } \ No newline at end of file diff --git a/os/kernel/src/syscall/syscall_dispatcher.rs b/os/kernel/src/syscall/syscall_dispatcher.rs index 78f0507..13a855c 100644 --- a/os/kernel/src/syscall/syscall_dispatcher.rs +++ b/os/kernel/src/syscall/syscall_dispatcher.rs @@ -4,7 +4,7 @@ use x86_64::registers::model_specific::{LStar, Star}; use x86_64::structures::gdt::SegmentSelector; use x86_64::{PrivilegeLevel, VirtAddr}; use syscall::NUM_SYSCALLS; -use crate::syscall::{sys_write, sys_thread_exit, sys_thread_sleep, sys_thread_switch, sys_process_id, sys_thread_id, sys_read, sys_map_user_heap, sys_thread_join, sys_application_start}; +use crate::syscall::{sys_write, sys_thread_exit, sys_thread_sleep, sys_thread_switch, sys_process_id, sys_thread_id, sys_read, sys_map_user_heap, sys_thread_join, sys_application_start, sys_get_system_time}; pub fn init() { @@ -50,7 +50,8 @@ impl SyscallTable { sys_thread_sleep as *const _, sys_thread_join as *const _, sys_thread_exit as *const _, - sys_application_start as *const _ + sys_application_start as *const _, + sys_get_system_time as *const _ ], } } diff --git a/os/library/syscall/src/lib.rs b/os/library/syscall/src/lib.rs index 16ec043..4a83aaf 100644 --- a/os/library/syscall/src/lib.rs +++ b/os/library/syscall/src/lib.rs @@ -1,7 +1,7 @@ #![no_std] use core::arch::asm; -use crate::SystemCall::ApplicationStart; +use crate::SystemCall::SystemTime; #[repr(usize)] #[allow(dead_code)] @@ -15,10 +15,11 @@ pub enum SystemCall { ThreadSleep, ThreadJoin, ThreadExit, - ApplicationStart + ApplicationStart, + SystemTime } -pub const NUM_SYSCALLS: usize = ApplicationStart as usize + 1; +pub const NUM_SYSCALLS: usize = SystemTime as usize + 1; #[inline(always)] pub fn syscall0(call: SystemCall) -> usize { diff --git a/os/library/time/Cargo.toml b/os/library/time/Cargo.toml new file mode 100644 index 0000000..e5a363b --- /dev/null +++ b/os/library/time/Cargo.toml @@ -0,0 +1,12 @@ +[package] +edition = "2021" +name = "time" +version = "0.1.0" +authors = ["Michael Schöttner , Fabian Ruhland "] + +[dependencies] +# Local dependencies +syscall = { path = "../syscall" } + +# External dependencies +chrono = { version = "0.4.34", default-features = false, features = ["alloc"] } \ No newline at end of file diff --git a/os/library/time/src/lib.rs b/os/library/time/src/lib.rs new file mode 100644 index 0000000..8d45297 --- /dev/null +++ b/os/library/time/src/lib.rs @@ -0,0 +1,9 @@ +#![no_std] + +use chrono::Duration; +use syscall::{syscall0, SystemCall}; + +pub fn systime() -> Duration { + let systime = syscall0(SystemCall::SystemTime); + Duration::milliseconds(systime as i64) +} \ No newline at end of file