Skip to content

Commit bf0153a

Browse files
committed
Adds panic
1 parent a5efa33 commit bf0153a

File tree

4 files changed

+108
-31
lines changed

4 files changed

+108
-31
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ okf = { git = "https://github.com/obhq/kernel-framework.git" }
1414
okf-1100 = { git = "https://github.com/obhq/kernel-framework.git" }
1515
```
1616

17-
Please note that all examples below was designed to use with crates.io so you may need to adjust it.
17+
Please note that all examples below was designed to use with crates.io so you may need to adjust it. Beware that each commit can introduce some breaking changes at anytime so you should commit a `Cargo.lock` or locked to a specific commit.
1818

1919
## Develop a kernel application
2020

ps4-1100/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use okf::malloc::MallocFlags;
1818
use okf::queue::TailQueue;
1919
use okf::socket::SockAddr;
2020
use okf::uio::UioSeg;
21-
use okf::{MappedKernel, StaticMut, offset, panic_handler};
21+
use okf::{Function, MappedKernel, StaticMut, offset, panic_handler};
2222

2323
mod file;
2424
mod lock;
@@ -54,6 +54,8 @@ impl okf::Kernel for Kernel {
5454
#[offset(0x22D0F10)]
5555
const MOUNTLIST_MTX: StaticMut<Self::Mtx>;
5656
const NOCPU: u32 = 0xff;
57+
#[offset(0x1987C0)]
58+
const PANIC: Function<extern "C" fn(*const c_char, ...) -> !>;
5759
const VDIR: c_int = 2;
5860
#[offset(0x15308F0)]
5961
const VOP_LOOKUP: StaticMut<Self::VnodeOp>;

ps4-1100/src/vnode.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ impl okf::vnode::VopRead<crate::Kernel> for VopRead {
6363
cred: *mut Ucred,
6464
) -> Self {
6565
Self {
66-
desc: k.var(crate::Kernel::VOP_READ).ptr(),
66+
desc: k.get(crate::Kernel::VOP_READ).as_mut_ptr(),
6767
vp,
6868
uio,
6969
flags,
@@ -95,7 +95,7 @@ impl okf::vnode::VopReadDir<crate::Kernel> for VopReadDir {
9595
cookies: *mut *mut u64,
9696
) -> Self {
9797
Self {
98-
desc: k.var(crate::Kernel::VOP_READDIR).ptr(),
98+
desc: k.get(crate::Kernel::VOP_READDIR).as_mut_ptr(),
9999
vp,
100100
uio,
101101
cred,
@@ -123,7 +123,7 @@ impl okf::vnode::VopLookup<crate::Kernel> for VopLookup {
123123
cn: *mut ComponentName,
124124
) -> Self {
125125
Self {
126-
desc: k.var(crate::Kernel::VOP_LOOKUP).ptr(),
126+
desc: k.get(crate::Kernel::VOP_LOOKUP).as_mut_ptr(),
127127
vp,
128128
out,
129129
cn,

src/lib.rs

Lines changed: 101 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use self::vnode::{Vnode, VnodeOp, VopLookup, VopRead, VopReadDir, VopUnlock, Vop
1616
use core::alloc::{GlobalAlloc, Layout};
1717
use core::ffi::{c_char, c_int};
1818
use core::marker::PhantomData;
19+
use core::mem::transmute;
1920
use core::num::NonZero;
2021
use core::ops::Deref;
2122
use core::ptr::{null_mut, read_unaligned, write_unaligned};
@@ -62,6 +63,7 @@ pub trait Kernel: MappedKernel {
6263
const MOUNTLIST: StaticMut<TailQueue<Self::Mount>>;
6364
const MOUNTLIST_MTX: StaticMut<Self::Mtx>;
6465
const NOCPU: u32;
66+
const PANIC: Function<extern "C" fn(*const c_char, ...) -> !>;
6567
const VDIR: c_int;
6668
const VOP_LOOKUP: StaticMut<Self::VnodeOp>;
6769
const VOP_READ: StaticMut<Self::VnodeOp>;
@@ -91,10 +93,10 @@ pub trait Kernel: MappedKernel {
9193
type VopUnlock: VopUnlock;
9294
type VopVector: VopVector;
9395

94-
fn var<O: StaticOff>(self, off: O) -> O::Ops {
95-
let value = unsafe { self.addr().add(off.value()) };
96+
fn get<O: Offset>(self, off: O) -> O::Ops {
97+
let addr = unsafe { self.addr().add(off.get()) };
9698

97-
<O::Ops as StaticOps>::new(value)
99+
<O::Ops as OffsetOps>::new(addr)
98100
}
99101

100102
/// # Safety
@@ -284,16 +286,16 @@ pub trait MappedKernel: Default + Sized + Copy + Send + Sync + 'static {
284286
fn addr(self) -> *const u8;
285287
}
286288

287-
/// Offset of a static value in the kernel.
288-
pub trait StaticOff: Copy {
289-
type Ops: StaticOps;
289+
/// Offset of something in the kernel.
290+
pub trait Offset: Copy {
291+
type Ops: OffsetOps;
290292

291-
fn value(self) -> usize;
293+
fn get(self) -> usize;
292294
}
293295

294-
/// Operations on a static value.
295-
pub trait StaticOps: Copy {
296-
fn new(value: *const u8) -> Self;
296+
/// Contains possible operations on an item at the [`Offset`].
297+
pub trait OffsetOps: Copy {
298+
fn new(addr: *const u8) -> Self;
297299
}
298300

299301
/// Offset of an immutable static value in the kernel.
@@ -324,20 +326,20 @@ impl<T> Clone for Static<T> {
324326

325327
impl<T> Copy for Static<T> {}
326328

327-
impl<T> StaticOff for Static<T> {
329+
impl<T> Offset for Static<T> {
328330
type Ops = ImmutableOps<T>;
329331

330-
fn value(self) -> usize {
332+
fn get(self) -> usize {
331333
self.off
332334
}
333335
}
334336

335-
/// Implementation of [`StaticOps`] for [`Static`].
337+
/// Implementation of [`OffsetOps`] for [`Static`].
336338
pub struct ImmutableOps<T>(*const T);
337339

338-
impl<T> StaticOps for ImmutableOps<T> {
339-
fn new(value: *const u8) -> Self {
340-
Self(value.cast())
340+
impl<T> OffsetOps for ImmutableOps<T> {
341+
fn new(addr: *const u8) -> Self {
342+
Self(addr.cast())
341343
}
342344
}
343345

@@ -385,10 +387,10 @@ impl<T> Clone for StaticMut<T> {
385387

386388
impl<T> Copy for StaticMut<T> {}
387389

388-
impl<T> StaticOff for StaticMut<T> {
390+
impl<T> Offset for StaticMut<T> {
389391
type Ops = MutableOps<T>;
390392

391-
fn value(self) -> usize {
393+
fn get(self) -> usize {
392394
self.off
393395
}
394396
}
@@ -397,7 +399,7 @@ impl<T> StaticOff for StaticMut<T> {
397399
pub struct MutableOps<T>(*mut T);
398400

399401
impl<T> MutableOps<T> {
400-
pub fn ptr(self) -> *mut T {
402+
pub fn as_mut_ptr(self) -> *mut T {
401403
self.0
402404
}
403405

@@ -426,9 +428,82 @@ impl<T> Clone for MutableOps<T> {
426428

427429
impl<T> Copy for MutableOps<T> {}
428430

429-
impl<T> StaticOps for MutableOps<T> {
430-
fn new(value: *const u8) -> Self {
431-
Self(value as _)
431+
impl<T> OffsetOps for MutableOps<T> {
432+
fn new(addr: *const u8) -> Self {
433+
Self(addr.cast_mut().cast())
434+
}
435+
}
436+
437+
/// Offset of a function in the kernel.
438+
pub struct Function<T: KernelFn> {
439+
off: usize,
440+
phantom: PhantomData<T>,
441+
}
442+
443+
impl<T: KernelFn> Function<T> {
444+
/// # Safety
445+
/// Behavior is undefined if `off` is not valid.
446+
pub const unsafe fn new(off: usize) -> Self {
447+
Self {
448+
off,
449+
phantom: PhantomData,
450+
}
451+
}
452+
}
453+
454+
impl<T: KernelFn> Clone for Function<T> {
455+
fn clone(&self) -> Self {
456+
*self
457+
}
458+
}
459+
460+
impl<T: KernelFn> Copy for Function<T> {}
461+
462+
impl<T: KernelFn> Offset for Function<T> {
463+
type Ops = ImmutableOps<T>;
464+
465+
fn get(self) -> usize {
466+
self.off
467+
}
468+
}
469+
470+
/// Implementation of [`OffsetOps`] for [`Function`].
471+
pub struct FunctionOps<T> {
472+
addr: *const u8,
473+
phantom: PhantomData<T>,
474+
}
475+
476+
impl<T: KernelFn> FunctionOps<T> {
477+
pub fn as_ptr(self) -> T {
478+
T::from_addr(self.addr)
479+
}
480+
}
481+
482+
impl<T> Clone for FunctionOps<T> {
483+
fn clone(&self) -> Self {
484+
*self
485+
}
486+
}
487+
488+
impl<T> Copy for FunctionOps<T> {}
489+
490+
impl<T> OffsetOps for FunctionOps<T> {
491+
fn new(addr: *const u8) -> Self {
492+
Self {
493+
addr,
494+
phantom: PhantomData,
495+
}
496+
}
497+
}
498+
499+
/// Provides method to cast kernel address into a function pointer.
500+
pub trait KernelFn: Copy {
501+
fn from_addr(addr: *const u8) -> Self;
502+
}
503+
504+
impl<R, A1> KernelFn for extern "C" fn(A1, ...) -> R {
505+
fn from_addr(addr: *const u8) -> Self {
506+
unsafe { transmute(addr) }
432507
}
433508
}
434509

@@ -462,8 +537,8 @@ impl<K: Kernel> Allocator<K> {
462537

463538
// Allocate.
464539
let k = K::default();
465-
let t = k.var(K::M_TEMP);
466-
let mem = unsafe { k.malloc(size, t.ptr(), flags) };
540+
let t = k.get(K::M_TEMP);
541+
let mem = unsafe { k.malloc(size, t.as_mut_ptr(), flags) };
467542

468543
if mem.is_null() {
469544
return null_mut();
@@ -505,9 +580,9 @@ unsafe impl<K: Kernel> GlobalAlloc for Allocator<K> {
505580

506581
// Free the memory.
507582
let k = K::default();
508-
let t = k.var(K::M_TEMP);
583+
let t = k.get(K::M_TEMP);
509584

510-
unsafe { k.free(ptr, t.ptr()) };
585+
unsafe { k.free(ptr, t.as_mut_ptr()) };
511586
}
512587

513588
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {

0 commit comments

Comments
 (0)