Prepare scheduling

This commit is contained in:
Baptiste Wicht 2014-01-26 17:18:57 +01:00
parent 5be3be8b64
commit a192ed9613
7 changed files with 123 additions and 12 deletions

View File

@ -20,6 +20,8 @@ struct process_t;
namespace paging { namespace paging {
//TODO Probably some of it could be moved in the source file
typedef uint64_t* page_entry; typedef uint64_t* page_entry;
typedef page_entry* pt_t; typedef page_entry* pt_t;
typedef pt_t* pd_t; typedef pt_t* pd_t;
@ -101,6 +103,8 @@ void map_kernel_inside_user(scheduler::process_t& process);
bool user_map(scheduler::process_t& process, size_t virt, size_t physical); bool user_map(scheduler::process_t& process, size_t virt, size_t physical);
bool user_map_pages(scheduler::process_t& process, size_t virt, size_t physical, size_t pages); bool user_map_pages(scheduler::process_t& process, size_t virt, size_t physical, size_t pages);
size_t get_physical_pml4t();
} //end of namespace paging } //end of namespace paging
#endif #endif

View File

@ -21,12 +21,20 @@ struct segment_t {
}; };
struct process_t { struct process_t {
size_t pid;
bool system;
size_t physical_cr3; size_t physical_cr3;
size_t paging_size; size_t paging_size;
size_t physical_user_stack; size_t physical_user_stack;
size_t physical_kernel_stack; size_t physical_kernel_stack;
size_t rip;
size_t user_rsp;
size_t kernel_rsp;
std::vector<segment_t> segments; std::vector<segment_t> segments;
std::vector<size_t> physical_paging; std::vector<size_t> physical_paging;
}; };

View File

@ -13,6 +13,10 @@ namespace scheduler {
void init(); void init();
void start(); void start();
void kill_current_process();
void reschedule();
} //end of namespace scheduler } //end of namespace scheduler
#endif #endif

View File

@ -84,20 +84,15 @@ public:
//Modifiers //Modifiers
void push_back(const value_type& element){ void push_back(value_type&& element){
if(_capacity == 0){ ensure_capacity();
_capacity = 1;
data = new T[_capacity];
} else if(_capacity == _size){
_capacity= _capacity * 2;
auto new_data = new T[_capacity]; data[_size++] = std::forward<value_type>(element);
std::move_n(new_data, data, _size);
delete[] data;
data = new_data;
} }
void push_back(const value_type& element){
ensure_capacity();
data[_size++] = element; data[_size++] = element;
} }
@ -126,6 +121,22 @@ public:
constexpr const_iterator end() const { constexpr const_iterator end() const {
return const_iterator(&data[_size]); return const_iterator(&data[_size]);
} }
private:
void ensure_capacity(){
if(_capacity == 0){
_capacity = 1;
data = new T[_capacity];
} else if(_capacity == _size){
_capacity= _capacity * 2;
auto new_data = new T[_capacity];
std::move_n(new_data, data, _size);
delete[] data;
data = new_data;
}
}
}; };
} //end of namespace std } //end of namespace std

View File

@ -519,3 +519,7 @@ bool paging::user_map_pages(scheduler::process_t& process, size_t virt, size_t p
return true; return true;
} }
size_t paging::get_physical_pml4t(){
return physical_pml4t_start;
}

77
kernel/src/scheduler.cpp Normal file
View File

@ -0,0 +1,77 @@
//=======================================================================
// Copyright Baptiste Wicht 2013-2014.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//=======================================================================
#include "scheduler.hpp"
#include "process.hpp"
#include "paging.hpp"
#include "assert.hpp"
#include "console.hpp"
namespace {
std::vector<scheduler::process_t> processes;
size_t current_pid;
size_t next_pid = 1;
void idle_task(){
while(true){
asm volatile("hlt");
}
}
size_t idle_stack[64];
size_t idle_kernel_stack[4096]; //TODO Perhaps not good
void create_idle_task(){
scheduler::process_t idle_process;
idle_process.pid = next_pid++;
idle_process.system = true;
idle_process.physical_cr3 = paging::get_physical_pml4t();
idle_process.paging_size = 0;
idle_process.physical_user_stack = 0;
idle_process.physical_kernel_stack = 0;
idle_process.rip = reinterpret_cast<size_t>(&idle_task);
idle_process.user_rsp = reinterpret_cast<size_t>(&idle_stack[63]);
idle_process.kernel_rsp = reinterpret_cast<size_t>(&idle_kernel_stack[4095]);
processes.push_back(std::move(idle_process));
}
void switch_to_process(size_t pid){
current_pid = pid;
k_printf("Switched to %u\n", current_pid);
//TODO
}
} //end of anonymous namespace
void scheduler::init(){
//Create the idle task
create_idle_task();
}
void scheduler::start(){
thor_assert(!processes.empty(), "There should at least be the idle task");
switch_to_process(processes.size() - 1);
}
void scheduler::kill_current_process(){
k_printf("Kill %u\n", current_pid);
//TODO
}
void scheduler::reschedule(){
//TODO
}

View File

@ -24,6 +24,7 @@
#include "gdt.hpp" #include "gdt.hpp"
#include "vesa.hpp" #include "vesa.hpp"
#include "process.hpp" #include "process.hpp"
#include "scheduler.hpp"
#include "physical_allocator.hpp" #include "physical_allocator.hpp"
#include "virtual_allocator.hpp" #include "virtual_allocator.hpp"
@ -868,6 +869,8 @@ void exec_command(const std::vector<std::string>& params){
return; return;
} }
scheduler::init();
auto content = read_elf_file(params[1], "exec"); auto content = read_elf_file(params[1], "exec");
if(!content){ if(!content){