Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
See what's new at GitLab
4
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Switch to GitLab Next
Sign in / Register
Toggle navigation
ObsidianOS
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Iterations
Merge Requests
0
Merge Requests
0
Requirements
Requirements
List
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Test Cases
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Operations
Operations
Incidents
Environments
Analytics
Analytics
CI / CD
Code Review
Insights
Issue
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
ObsidianOS
ObsidianOS
Commits
3a673ddc
Commit
3a673ddc
authored
Jun 22, 2020
by
Nifou
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Create the structures `Pid` and `Tid` and remove all warnings
parent
7d998171
Changes
8
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
49 additions
and
16 deletions
+49
-16
kernel/src/syscall/memory.rs
kernel/src/syscall/memory.rs
+2
-2
kernel/src/tasking/algo.rs
kernel/src/tasking/algo.rs
+1
-0
kernel/src/tasking/context.rs
kernel/src/tasking/context.rs
+2
-1
kernel/src/tasking/process.rs
kernel/src/tasking/process.rs
+10
-5
kernel/src/tasking/processor.rs
kernel/src/tasking/processor.rs
+17
-3
kernel/src/tasking/scheduler/mod.rs
kernel/src/tasking/scheduler/mod.rs
+1
-0
kernel/src/tasking/scheduler/round_robin.rs
kernel/src/tasking/scheduler/round_robin.rs
+3
-0
kernel/src/tasking/thread.rs
kernel/src/tasking/thread.rs
+13
-5
No files found.
kernel/src/syscall/memory.rs
View file @
3a673ddc
...
...
@@ -78,7 +78,7 @@ impl Syscall {
crate
::
memory
::
switch_to_kernel_page_table
();
// Get the current thread's process
let
mut
current_process
=
PROCESSOR
.inner
()
.scheduler
.current
()
.process
;
let
current_process
=
PROCESSOR
.inner
()
.scheduler
.current
()
.process
;
processes
.get_mut
(
&
current_process
)
.unwrap
()
.memory
.map
(
0x300000
,
0x300000
+
len
as
u64
,
Flags
::
from
(
prot
),
Flags
::
PRESENT
);
processes
.get_mut
(
&
current_process
)
.unwrap
()
.memory
.switch_page_table
();
...
...
@@ -106,7 +106,7 @@ impl Syscall {
crate
::
memory
::
switch_to_kernel_page_table
();
// Get the current thread's process
let
mut
current_process
=
PROCESSOR
.inner
()
.scheduler
.current
()
.process
;
let
current_process
=
PROCESSOR
.inner
()
.scheduler
.current
()
.process
;
processes
.get_mut
(
&
current_process
)
.unwrap
()
.memory
.map
(
0x10000000
,
0x10004000
,
Flags
::
PRESENT
|
Flags
::
WRITABLE
,
Flags
::
PRESENT
);
processes
.get_mut
(
&
current_process
)
.unwrap
()
.memory
.switch_page_table
();
...
...
kernel/src/tasking/algo.rs
View file @
3a673ddc
...
...
@@ -14,6 +14,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see https://www.gnu.org/licenses.
*/
//! A trait which make the addition of other scheduler algorithms easier
use
super
::
thread
::
Thread
;
/// A trait which defines the common functions used in a scheduler algorithm
...
...
kernel/src/tasking/context.rs
View file @
3a673ddc
...
...
@@ -32,7 +32,8 @@ pub const FX_AREA_SIZE: usize = 512;
/// The context of a process (its registers and its stack).
pub
struct
Context
{
regs
:
Registers
,
pub
sse_state
:
u64
,
sse_state
:
u64
,
}
impl
Context
{
...
...
kernel/src/tasking/process.rs
View file @
3a673ddc
...
...
@@ -14,6 +14,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see https://www.gnu.org/licenses.
*/
//! Definition of a Process which regroups a set of resources (memory, ...)
use
core
::
sync
::
atomic
::{
AtomicUsize
,
Ordering
};
use
alloc
::
prelude
::
v1
::
Vec
;
use
x86_64
::
structures
::
paging
::
page_table
::
PageTableFlags
as
Flags
;
...
...
@@ -23,7 +24,7 @@ use super::{
memory
::
ProcessMemory
,
info
::
ProcessInfo
,
processor
::{
PROCESSES
,
PROCESSOR
},
thread
::
Thread
,
thread
::
{
Thread
,
Tid
}
,
};
/// The address of the user stack
...
...
@@ -34,6 +35,10 @@ pub const STACK_SIZE: usize = 65536;
static
NEXT_PID
:
AtomicUsize
=
AtomicUsize
::
new
(
0
);
#[derive(Debug,
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord)]
/// A process ID
pub
struct
Pid
(
usize
);
#[derive(PartialEq,
Debug,
Clone,
Copy)]
/// The state of the process (killed or alive)
pub
enum
ProcessState
{
...
...
@@ -48,7 +53,7 @@ pub enum ProcessState {
/// A process which regroups resources and more
pub
struct
Process
{
/// The process ID
pub
pid
:
usize
,
pub
pid
:
Pid
,
/// The state of this process
pub
state
:
ProcessState
,
...
...
@@ -60,12 +65,12 @@ pub struct Process {
pub
info
:
ProcessInfo
,
/// Threads of the process
pub
threads
:
Vec
<
usize
>
,
pub
threads
:
Vec
<
Tid
>
,
}
impl
Process
{
/// Create a new process
pub
fn
new
(
rip
:
u64
,
mut
info
:
ProcessInfo
)
->
usize
{
pub
fn
new
(
rip
:
u64
,
mut
info
:
ProcessInfo
)
->
Pid
{
// We can use a static address for the stack because the content of it will be switched
// with the switch of page tables
let
mut
processes
=
PROCESSES
.write
();
...
...
@@ -73,7 +78,7 @@ impl Process {
let
rsp
=
info
.push_at
(
STACK_ADDR
as
usize
)
as
u64
;
let
mut
process
=
Self
{
pid
:
NEXT_PID
.fetch_add
(
1
,
Ordering
::
Relaxed
),
pid
:
Pid
(
NEXT_PID
.fetch_add
(
1
,
Ordering
::
Relaxed
)
),
state
:
ProcessState
::
Alive
,
memory
:
ProcessMemory
::
new
(),
info
,
...
...
kernel/src/tasking/processor.rs
View file @
3a673ddc
...
...
@@ -14,6 +14,11 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see https://www.gnu.org/licenses.
*/
//! This file provides a global PROCESSOR which can be used in the kernel
//!
//! To add a new type of scheduler algorithm, you just need to create a new file in the `scheduler`
//! directory, to implement the trait `SchedulerAlgo` on a struct and to create a feature to switch
//! between your scheduler algorithm and others.
use
lazy_static
::
lazy_static
;
use
core
::
cell
::
UnsafeCell
;
use
alloc
::{
...
...
@@ -23,26 +28,32 @@ use alloc::{
use
spin
::
RwLock
;
use
super
::
algo
::
SchedulerAlgo
;
use
super
::
process
::
Process
;
use
super
::
process
::
{
Process
,
Pid
}
;
use
super
::
scheduler
::
round_robin
::
RoundRobinScheduler
;
lazy_static!
{
/// A global Processor
pub
static
ref
PROCESSOR
:
Processor
=
Processor
::
new
(
Box
::
new
(
RoundRobinScheduler
::
new
()));
}
lazy_static!
{
pub
static
ref
PROCESSES
:
RwLock
<
BTreeMap
<
usize
,
Process
>>
=
RwLock
::
new
(
BTreeMap
::
new
());
/// A global array which regroups all processes and their PIDs
pub
static
ref
PROCESSES
:
RwLock
<
BTreeMap
<
Pid
,
Process
>>
=
RwLock
::
new
(
BTreeMap
::
new
());
}
/// The inner of the Processor
pub
struct
ProcessorInner
{
/// The scheduler which choose the next thread
/// The scheduler which choose
s
the next thread
pub
scheduler
:
Box
<
dyn
SchedulerAlgo
>
,
/// Whether or not it is the first time that the processor is running
first_time
:
bool
,
}
/// A Processor which includes a context dispatcher (to change the context of the thread) and a
/// scheduler (to choose the next thread)
pub
struct
Processor
{
/// The inner of the Processor (used to get a mutable reference of the Processor)
pub
inner
:
UnsafeCell
<
ProcessorInner
>
,
}
...
...
@@ -50,12 +61,14 @@ unsafe impl Sync for Processor {}
unsafe
impl
Send
for
Processor
{}
impl
Processor
{
/// Create a new Processor
pub
fn
new
(
scheduler
:
Box
<
dyn
SchedulerAlgo
>
)
->
Self
{
Self
{
inner
:
UnsafeCell
::
new
(
ProcessorInner
::
new
(
scheduler
)),
}
}
/// Get a mutable reference of the inner of the Processor
pub
fn
inner
(
&
self
)
->
&
mut
ProcessorInner
{
unsafe
{
&
mut
*
self
.inner
.get
()
}
}
...
...
@@ -168,6 +181,7 @@ impl Processor {
}
impl
ProcessorInner
{
/// Create a new ProcessorInner
pub
fn
new
(
scheduler
:
Box
<
dyn
SchedulerAlgo
>
)
->
Self
{
Self
{
scheduler
,
...
...
kernel/src/tasking/scheduler/mod.rs
View file @
3a673ddc
...
...
@@ -14,4 +14,5 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see https://www.gnu.org/licenses.
*/
//! This directory regroups some alogithms for scheduler
pub
mod
round_robin
;
kernel/src/tasking/scheduler/round_robin.rs
View file @
3a673ddc
...
...
@@ -14,6 +14,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see https://www.gnu.org/licenses.
*/
//! The [Round Robin](https://en.wikipedia.org/wiki/Round-robin_scheduling) scheduling algorithm
use
alloc
::
collections
::
vec_deque
::
VecDeque
;
use
crate
::
tasking
::{
...
...
@@ -21,11 +22,13 @@ use crate::tasking::{
thread
::
Thread
,
};
/// A [Round Robin](https://en.wikipedia.org/wiki/Round-robin_scheduling) sheduler
pub
struct
RoundRobinScheduler
{
threads
:
VecDeque
<
Thread
>
,
}
impl
RoundRobinScheduler
{
/// Create a new Round Robin scheduler
pub
fn
new
()
->
Self
{
Self
{
threads
:
VecDeque
::
new
(),
...
...
kernel/src/tasking/thread.rs
View file @
3a673ddc
...
...
@@ -14,17 +14,25 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see https://www.gnu.org/licenses.
*/
//! Definition of a thread which will be chosen (by the scheduler) and executed
use
core
::
sync
::
atomic
::{
AtomicUsize
,
Ordering
};
use
super
::
context
::
Context
;
use
super
::{
context
::
Context
,
process
::
Pid
,
};
static
NEXT_TID
:
AtomicUsize
=
AtomicUsize
::
new
(
0
);
#[derive(Debug,
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord)]
/// A thread ID
pub
struct
Tid
(
usize
);
#[derive(Debug,
Clone)]
/// A thread which is chosen by the scheduler
pub
struct
Thread
{
/// The thread ID
pub
tid
:
usize
,
pub
tid
:
Tid
,
/// The context of the thread
pub
ctx
:
Context
,
...
...
@@ -33,14 +41,14 @@ pub struct Thread {
pub
first_time
:
bool
,
/// The process which have this thread
pub
process
:
usize
,
pub
process
:
Pid
,
}
impl
Thread
{
/// Create a new Thread
pub
fn
new
(
process
:
usize
,
rip
:
u64
,
rsp
:
u64
)
->
Self
{
pub
fn
new
(
process
:
Pid
,
rip
:
u64
,
rsp
:
u64
)
->
Self
{
Self
{
tid
:
NEXT_TID
.fetch_add
(
1
,
Ordering
::
Relaxed
),
tid
:
Tid
(
NEXT_TID
.fetch_add
(
1
,
Ordering
::
Relaxed
)
),
ctx
:
Context
::
new
(
rip
,
rsp
),
first_time
:
true
,
process
,
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment