strat9_kernel/arch/xshim.rs
1/*
2 * This file is part of strat9-os.
3 *
4 * strat9-os is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * strat9-os is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with strat9-os. If not, see <https://www.gnu.org/licenses/>.
16 *
17 * Website: https://www.strat9-os.org
18 * Repository: https://github.com/strat9-os/strat9-os
19 *
20 * Copyright (C) 2023-26 Guillaume Gielly
21 */
22
23//! Architecture-neutral shim mirroring the subset of the `x86_64` crate API
24//! used by shared kernel code (address types, page sizes, paging flags).
25//!
26//! Purpose: the ~42 `use x86_64::...` call-sites outside `arch/` are being
27//! rewritten to `use crate::arch::xshim::...` so the riscv64 backend can
28//! provide its own implementations. On x86_64 the shim re-exports the real
29//! crate types so behaviour is bit-identical.
30
31#[cfg(target_arch = "x86_64")]
32pub use x86_64::{
33 structures::paging::{
34 mapper::TranslateResult, PageTableFlags, PhysFrame, Size2MiB, Size4KiB, Translate,
35 },
36 PhysAddr, VirtAddr,
37};
38
39// riscv64 uses the neutral types defined below.
40
41#[cfg(not(target_arch = "x86_64"))]
42mod neutral {
43 pub use crate::ostd::mm::{PhysAddr, VirtAddr};
44
45 /// Page granularity markers (4 KiB / 2 MiB).
46 #[derive(Clone, Copy, PartialEq, Eq, Debug)]
47 pub struct Size4KiB;
48
49 #[derive(Clone, Copy, PartialEq, Eq, Debug)]
50 pub struct Size2MiB;
51
52 pub const SIZE_4KIB: u64 = 4096;
53 pub const SIZE_2MIB: u64 = 2 * 1024 * 1024;
54
55 bitflags::bitflags! {
56 /// Page-table flags, arch-neutral encoding (subset of both ISAs).
57 #[derive(Clone, Copy, PartialEq, Eq, Debug)]
58 pub struct PageTableFlags: u64 {
59 const PRESENT = 1 << 0;
60 const WRITABLE = 1 << 1;
61 const USER_ACCESSIBLE = 1 << 2;
62 const NO_EXECUTE = 1 << 3;
63 const NO_CACHE = 1 << 4;
64 const HUGE_PAGE = 1 << 5;
65 const BIT_9 = 1 << 9; // used as COW marker on x86_64
66 }
67 }
68
69 /// Result of a translation walk.
70 #[derive(Clone, Copy, Debug)]
71 pub enum TranslateResult {
72 Mapped {
73 frame: PhysFrame<Size4KiB>,
74 offset: u64,
75 flags: PageTableFlags,
76 },
77 NotMapped,
78 InvalidFrameAddress(PhysAddr),
79 }
80
81 /// A physical memory frame of a given size.
82 #[derive(Clone, Copy, PartialEq, Eq, Debug)]
83 pub struct PhysFrame<S> {
84 start: PhysAddr,
85 _size: core::marker::PhantomData<S>,
86 }
87
88 impl<S> PhysFrame<S> {
89 pub fn start_address(&self) -> PhysAddr {
90 self.start
91 }
92 }
93
94 impl PhysFrame<Size4KiB> {
95 pub fn containing_address(addr: PhysAddr) -> Self {
96 Self {
97 start: PhysAddr::new(addr.as_u64() & !0xFFF),
98 _size: core::marker::PhantomData,
99 }
100 }
101 }
102
103 impl PhysFrame<Size2MiB> {
104 pub fn containing_address(addr: PhysAddr) -> Self {
105 Self {
106 start: PhysAddr::new(addr.as_u64() & !(SIZE_2MIB - 1)),
107 _size: core::marker::PhantomData,
108 }
109 }
110 }
111}
112
113#[cfg(not(target_arch = "x86_64"))]
114pub use neutral::*;
115
116/// Address bits within a page-table entry (Sv48: PPN[2..0] + flags).
117pub const PTR_MASK: u64 = 0x0000_FFFF_FFFF_F000;
118
119/// Translation trait mirroring `x86_64::structures::paging::Translate`
120/// (neutral version; on riscv64 implemented by the Sv48 mapper).
121#[cfg(not(target_arch = "x86_64"))]
122pub trait Translate {
123 fn translate(&self, virt: VirtAddr) -> Option<(PhysFrame<Size4KiB>, PageTableFlags)>;
124}