Skip to content

Commit 36927b9

Browse files
committed
more scheduler rewrite
1 parent 4c4f865 commit 36927b9

File tree

23 files changed

+584
-349
lines changed

23 files changed

+584
-349
lines changed

machine/api/src/lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
use core::{fmt::Display, ops::Range};
44

55
pub mod mem;
6+
pub mod stack;
67

78
#[derive(Default, Debug, PartialEq, Eq, Clone)]
89
pub enum Error {
910
#[default]
1011
Generic,
1112
OutOfMemory(usize),
1213
OutOfBoundsPtr(usize, Range<usize>),
14+
InvalidAddress(usize),
1315
}
1416

1517
pub enum Fault {
@@ -30,7 +32,8 @@ impl Display for Error {
3032
"Pointer {:p} out of bounds (expected in {:p}..{:p})",
3133
*ptr as *const u8, range.start as *const u8, range.end as *const u8
3234
)
33-
}
35+
},
36+
Error::InvalidAddress(addr) => write!(f, "Invalid address {:p}", *addr as *const u8),
3437
}
3538
}
3639
}

machine/api/src/mem.rs

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
2-
pub mod stack;
1+
use core::ops::{Add, Sub, Div, Rem};
32

43
#[repr(transparent)]
54
#[derive(Debug, Clone, Copy, PartialEq, Eq, Ord, PartialOrd, Hash)]
65
pub struct PhysAddr(usize);
76

87
impl PhysAddr {
8+
pub const MAX: Self = Self(usize::MAX);
9+
910
#[inline]
1011
pub fn new(addr: usize) -> Self {
1112
Self(addr)
@@ -15,6 +16,58 @@ impl PhysAddr {
1516
pub fn as_usize(&self) -> usize {
1617
self.0
1718
}
19+
20+
pub fn as_mut_ptr<T>(&self) -> *mut T {
21+
self.0 as *mut T
22+
}
23+
24+
pub fn checked_add(&self, other: usize) -> Option<Self> {
25+
self.0.checked_add(other).map(Self)
26+
}
27+
28+
pub fn checked_sub(&self, other: usize) -> Option<Self> {
29+
self.0.checked_sub(other).map(Self)
30+
}
31+
32+
pub fn is_multiple_of(&self, align: usize) -> bool {
33+
self.0.is_multiple_of(align)
34+
}
35+
}
36+
37+
impl Add<usize> for PhysAddr {
38+
type Output = Self;
39+
40+
#[inline]
41+
fn add(self, rhs: usize) -> Self::Output {
42+
Self(self.0 + rhs)
43+
}
44+
}
45+
46+
impl Sub<usize> for PhysAddr {
47+
type Output = Self;
48+
49+
#[inline]
50+
fn sub(self, rhs: usize) -> Self::Output {
51+
Self(self.0 - rhs)
52+
}
53+
}
54+
55+
impl Div<usize> for PhysAddr {
56+
type Output = Self;
57+
58+
#[inline]
59+
fn div(self, rhs: usize) -> Self::Output {
60+
Self(self.0 / rhs)
61+
}
62+
}
63+
64+
impl Rem<usize> for PhysAddr {
65+
type Output = Self;
66+
67+
#[inline]
68+
fn rem(self, rhs: usize) -> Self::Output {
69+
Self(self.0 % rhs)
70+
}
1871
}
1972

2073
impl From<PhysAddr> for usize {
@@ -38,6 +91,16 @@ impl VirtAddr {
3891
pub fn as_usize(&self) -> usize {
3992
self.0
4093
}
94+
95+
#[inline]
96+
pub fn saturating_add(&self, other: usize) -> Self {
97+
Self(self.0.saturating_add(other))
98+
}
99+
100+
#[inline]
101+
pub fn saturating_sub(&self, other: usize) -> Self {
102+
Self(self.0.saturating_sub(other))
103+
}
41104
}
42105

43106
impl From<VirtAddr> for usize {
Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
use core::{ffi::c_void, num::NonZero};
22
use crate::{Result, mem::PhysAddr};
33

4-
pub struct StackDescriptor {
4+
pub type EntryFn = extern "C" fn();
5+
pub type FinFn = extern "C" fn() -> !;
6+
7+
pub struct Descriptor {
58
pub top: PhysAddr,
69
pub size: NonZero<usize>,
7-
pub entry: extern "C" fn(),
8-
pub fin: Option<extern "C" fn() -> !>,
10+
pub entry: EntryFn,
11+
pub fin: Option<FinFn>,
912
}
1013

1114
pub trait Stacklike {
1215
type ElemSize: Copy;
1316
type StackPtr;
1417

15-
unsafe fn new(desc: StackDescriptor) -> Result<Self>
18+
unsafe fn new(desc: Descriptor) -> Result<Self>
1619
where
1720
Self: Sized;
1821

machine/arm/src/sched.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use core::{
77
ptr::NonNull,
88
};
99

10-
use hal_api::{Result, stack::StackDescriptor};
10+
use hal_api::{Result, stack::Descriptor};
1111

1212
use crate::print::println;
1313

@@ -191,19 +191,19 @@ impl hal_api::stack::Stacklike for ArmStack {
191191
type ElemSize = u32;
192192
type StackPtr = StackPtr;
193193

194-
unsafe fn new(desc: StackDescriptor) -> Result<Self>
194+
unsafe fn new(desc: Descriptor) -> Result<Self>
195195
where
196196
Self: Sized,
197197
{
198-
let StackDescriptor {
198+
let Descriptor {
199199
top,
200200
size,
201201
entry,
202202
fin,
203203
} = desc;
204204

205205
// We expect a PhysAddr, which can be converted to a ptr on nommu.
206-
let top = NonNull::new(top as *mut u32).ok_or(hal_api::Error::InvalidAddress)?;
206+
let top = NonNull::new(top.as_mut_ptr::<u32>()).ok_or(hal_api::Error::InvalidAddress(top.as_usize()))?;
207207

208208
let mut stack = Self {
209209
top,

machine/testing/src/sched.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::ffi::c_void;
22

33
use hal_api::{
44
Result,
5-
stack::{StackDescriptor, Stacklike},
5+
stack::{Descriptor, Stacklike},
66
};
77

88
#[derive(Debug, Clone, Copy)]
@@ -12,7 +12,7 @@ impl Stacklike for TestingStack {
1212
type ElemSize = usize;
1313
type StackPtr = *mut c_void;
1414

15-
unsafe fn new(_desc: StackDescriptor) -> Result<Self>
15+
unsafe fn new(_desc: Descriptor) -> Result<Self>
1616
where
1717
Self: Sized,
1818
{

options.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ description = "Enables the Floating Point Unit (FPU). This is required for appli
2626
type = "Boolean"
2727
default = false
2828

29+
[stackpages]
30+
name = "Stack Pages"
31+
description = "Number of pages to allocate for the kernel stack."
32+
type = { type = "Integer", min = 1 }
33+
default = 4
34+
2935
[tuning.appmemsize]
3036
name = "Application Memory Size"
3137
description = "Sets the size of the initial memory region for the init application. This memory is used for the heap and stack."

src/dispatch.rs

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/dispatch/task.rs

Lines changed: 0 additions & 162 deletions
This file was deleted.

0 commit comments

Comments
 (0)