Skip to content

Commit

Permalink
Implement syscall and 15:30:24 up 5:24, 1 user, load average: 1,05, 1…
Browse files Browse the repository at this point in the history
…,41, 1,23 application
  • Loading branch information
fruhland committed Feb 26, 2024
1 parent bcb74f5 commit 9bb747d
Show file tree
Hide file tree
Showing 11 changed files with 122 additions and 9 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ resolver = "2"
members = [
"os/kernel",
"os/application/hello",
"os/application/shell"
"os/application/shell",
"os/application/uptime"
]

# [profile.release]
Expand Down
2 changes: 1 addition & 1 deletion Makefile.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions os/application/uptime/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
edition = "2021"
name = "uptime"
version = "0.1.0"
authors = ["Michael Schöttner <[email protected]>, Fabian Ruhland <[email protected]>"]

[lib]
crate-type = ["staticlib"]
path = "src/uptime.rs"

[dependencies]
# Local dependencies
runtime = { path = "../../library/runtime" }
io = { path = "../../library/io" }
time = { path = "../../library/time" }
42 changes: 42 additions & 0 deletions os/application/uptime/Makefile.toml
Original file line number Diff line number Diff line change
@@ -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}" ]
27 changes: 27 additions & 0 deletions os/application/uptime/src/uptime.rs
Original file line number Diff line number Diff line change
@@ -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);
}
}
2 changes: 1 addition & 1 deletion os/kernel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
7 changes: 6 additions & 1 deletion os/kernel/src/syscall/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
}
5 changes: 3 additions & 2 deletions os/kernel/src/syscall/syscall_dispatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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 _
],
}
}
Expand Down
7 changes: 4 additions & 3 deletions os/library/syscall/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![no_std]

use core::arch::asm;
use crate::SystemCall::ApplicationStart;
use crate::SystemCall::SystemTime;

#[repr(usize)]
#[allow(dead_code)]
Expand All @@ -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 {
Expand Down
12 changes: 12 additions & 0 deletions os/library/time/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
edition = "2021"
name = "time"
version = "0.1.0"
authors = ["Michael Schöttner <[email protected]>, Fabian Ruhland <[email protected]>"]

[dependencies]
# Local dependencies
syscall = { path = "../syscall" }

# External dependencies
chrono = { version = "0.4.34", default-features = false, features = ["alloc"] }
9 changes: 9 additions & 0 deletions os/library/time/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 9bb747d

Please sign in to comment.