Skip to content

Commit

Permalink
Task manager task manager
Browse files Browse the repository at this point in the history
  • Loading branch information
mrgian committed Jul 12, 2023
1 parent c0e9914 commit df106c8
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 86 deletions.
7 changes: 3 additions & 4 deletions kernel/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@ use drivers::disk::DISK;
use drivers::pic::PICS;
use filesystem::fat::FAT;
use interrupts::idt::IDT;
use shell::shell::SHELL;
use syscalls::print::PRINTER;
use memory::allocator::Allocator;
use memory::paging::PAGING;
use memory::paging::TABLES;
use memory::allocator::Allocator;

use shell::shell::SHELL;
use syscalls::print::PRINTER;

use multitasking::task::TASK_MANAGER;

Expand Down
2 changes: 1 addition & 1 deletion kernel/src/memory/allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ unsafe impl GlobalAlloc for Allocator {
unsafe fn dealloc(&self, _ptr: *mut u8, _layout: Layout) {
panic!("PANIC")
}
}
}
2 changes: 1 addition & 1 deletion kernel/src/memory/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
pub mod allocator;
pub mod paging;
pub mod allocator;
4 changes: 1 addition & 3 deletions kernel/src/memory/paging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ pub static mut PAGING: PageDirectory = PageDirectory {

pub static mut TABLES: [PageTable; 16] = [NULL_TABLE; 16];

pub static NULL_TABLE: PageTable = PageTable {
entries: [0; 1024],
};
pub static NULL_TABLE: PageTable = PageTable { entries: [0; 1024] };

#[repr(align(4096))]
pub struct PageDirectory {
Expand Down
2 changes: 1 addition & 1 deletion kernel/src/multitasking/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
pub mod task;
pub mod task;
112 changes: 42 additions & 70 deletions kernel/src/multitasking/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ pub struct CPUState {
ss: u32,
}

static mut TASKS: [Task; MAX_TASKS as usize] = [NULL_TASK; MAX_TASKS as usize];

static NULL_TASK: Task = Task {
stack: [0; STACK_SIZE],
cpu_state_ptr: 0 as u32, //cpu_state: 0 as *mut CPUState,
Expand Down Expand Up @@ -79,38 +77,36 @@ impl Task {
}

pub struct TaskManager {
tasks: [*mut Task; MAX_TASKS as usize], //arry of pointers to tasks
task_count: i8, //how many tasks are in the queue
current_task: i8, //current running task
tasks: [Task; MAX_TASKS as usize], //arry of tasks
task_count: i8, //how many tasks are in the queue
current_task: i8, //current running task
}

//init null task manager
pub static mut TASK_MANAGER: TaskManager = TaskManager {
tasks: [0 as *mut Task; MAX_TASKS as usize],
tasks: [NULL_TASK; MAX_TASKS as usize],
task_count: 0,
current_task: -1,
};

impl TaskManager {
pub fn init(&mut self) {
unsafe {
TASKS[0].init(idle as u32);
self.add_task(&mut TASKS[0] as *mut Task);
}
self.add_task(idle as u32);
}

//add given task to next slot
pub fn add_task(&mut self, task: *mut Task) {
pub fn add_task(&mut self, entry_point: u32) {
let free_slot = self.get_free_slot();

self.tasks[free_slot as usize] = task;
self.tasks[free_slot as usize].init(entry_point);

self.task_count += 1;
}

//remove task
pub fn remove_task(&mut self, id: usize) {
if id != 0 {
self.tasks[id] = 0 as *mut Task;
self.tasks[id] = NULL_TASK;
self.task_count -= 1;
}
}
Expand All @@ -122,35 +118,31 @@ impl TaskManager {
//CPU SCHEDULER LOGIC
//triggers scheduler with round robin scheduling algorithm, returns new cpu state
pub fn schedule(&mut self, cpu_state: *mut CPUState) -> *mut CPUState {
unsafe {
//if no tasks return current state
if self.task_count <= 0 {
return cpu_state;
}
//if no tasks return current state
if self.task_count <= 0 {
return cpu_state;
}

//save current state of current task
if self.current_task >= 0 {
(*(self.tasks[self.current_task as usize])).cpu_state_ptr = cpu_state as u32;
}
//save current state of current task
if self.current_task >= 0 {
self.tasks[self.current_task as usize].cpu_state_ptr = cpu_state as u32;
}

self.current_task = self.get_next_task();
self.current_task = self.get_next_task();

(*(self.tasks[self.current_task as usize])).cpu_state_ptr as *mut CPUState
}
self.tasks[self.current_task as usize].cpu_state_ptr as *mut CPUState
}

pub fn get_next_task(&self) -> i8 {
unsafe {
let mut i = self.current_task + 1;
while i < MAX_TASKS {
let running = (*(self.tasks[i as usize])).running;

if running {
return i;
}
let mut i = self.current_task + 1;
while i < MAX_TASKS {
let running = self.tasks[i as usize].running;

i = (i + 1) % MAX_TASKS;
if running {
return i;
}

i = (i + 1) % MAX_TASKS;
}

-1
Expand All @@ -159,13 +151,11 @@ impl TaskManager {
pub fn get_free_slot(&self) -> i8 {
let mut slot: i8 = -1;

unsafe {
for i in 0..MAX_TASKS {
let running = (*(self.tasks[i as usize])).running;
if running == false {
slot = i as i8;
return slot;
}
for i in 0..MAX_TASKS {
let running = self.tasks[i as usize].running;
if running == false {
slot = i as i8;
return slot;
}
}

Expand All @@ -175,47 +165,29 @@ impl TaskManager {
pub fn list_tasks(&self) {
libfelix::println!("Running tasks:");

unsafe {
for i in 0..MAX_TASKS {
let running = (*(self.tasks[i as usize])).running;
if running {
libfelix::println!("ID: {}", i);
}
for i in 0..MAX_TASKS {
let running = self.tasks[i as usize].running;
if running {
libfelix::println!("ID: {}", i);
}
}
}

pub fn run_app(&mut self, app_entry_point: u32) {
unsafe {
TASKS[4].init(app_entry_point as u32);
self.add_task(&mut TASKS[4] as *mut Task);
}
}

pub fn add_dummy_task_a(&mut self) {
unsafe {
TASKS[1].init(task_a as u32);
self.add_task(&mut TASKS[1] as *mut Task);
}
self.add_task(task_a as u32);
}

pub fn add_dummy_task_b(&mut self) {
unsafe {
TASKS[2].init(task_b as u32);
self.add_task(&mut TASKS[2] as *mut Task);
}
self.add_task(task_b as u32);
}

pub fn add_dummy_task_c(&mut self) {
unsafe {
TASKS[3].init(task_c as u32);
self.add_task(&mut TASKS[3] as *mut Task);
}
self.add_task(task_c as u32);
}
}

fn idle() {
loop{}
loop {}
}

//EXAMPLE TASKS
Expand All @@ -235,7 +207,7 @@ fn task_a() {
}
a += 1;
}
loop{}
loop {}
}

fn task_b() {
Expand All @@ -254,7 +226,7 @@ fn task_b() {
}
a += 1;
}
loop{}
loop {}
}

fn task_c() {
Expand All @@ -273,5 +245,5 @@ fn task_c() {
}
a += 1;
}
loop{}
loop {}
}
2 changes: 1 addition & 1 deletion kernel/src/shell/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
pub mod shell;
pub mod shell;
7 changes: 3 additions & 4 deletions kernel/src/shell/shell.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//SHELL

use crate::filesystem::fat::FAT;
use crate::syscalls::print::PRINTER;
use crate::multitasking::task::TASK_MANAGER;
use crate::syscalls::print::PRINTER;

const APP_TARGET: u32 = 0x00a0_0000;
const APP_SIGNATURE: u32 = 0xB16B00B5;
Expand Down Expand Up @@ -82,7 +82,7 @@ impl Shell {
//test command
_b if self.is_command("ping") => {
libfelix::println!("PONG!");
},
}

//list root directory
_b if self.is_command("ls") => unsafe {
Expand Down Expand Up @@ -189,7 +189,7 @@ impl Shell {
let signature = *(APP_TARGET as *mut u32);

if signature == APP_SIGNATURE {
TASK_MANAGER.run_app((APP_TARGET + 4) as u32);
TASK_MANAGER.add_task((APP_TARGET + 4) as u32);
} else {
libfelix::println!("File is not a valid executable!");
}
Expand All @@ -210,4 +210,3 @@ impl Shell {
true
}
}

2 changes: 1 addition & 1 deletion kernel/src/syscalls/handler.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//SYSTEM CALLS HANDLER

use crate::drivers::pic::PICS;
use crate::syscalls::print;
use crate::multitasking::task::TASK_MANAGER;
use crate::syscalls::print;
use core::arch::asm;

use core::slice;
Expand Down

0 comments on commit df106c8

Please sign in to comment.