XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFESystem

【Rust】XFE系统,基于Rust和汇编

公开
关注 0 Fork 0 Star 0
UTF-8
using System.Runtime.InteropServices;

namespace XFESystem.NativeKernel;

[StructLayout(LayoutKind.Sequential)]
public unsafe struct NativeBootInfo
{
    public ulong ProtocolSupported, Hhdm;
    public MemoryMapEntry** MemoryEntries;
    public ulong MemoryEntryCount;
    public LimineFramebuffer* Framebuffer;
    public ulong Rsdp;
}

[StructLayout(LayoutKind.Sequential)]
public struct MemoryMapEntry { public ulong Base, Length, Kind; }

// Only the mandatory revision-zero framebuffer prefix is consumed.
[StructLayout(LayoutKind.Sequential)]
public unsafe struct LimineFramebuffer
{
    public byte* Address;
    public ulong Width, Height, Pitch;
    public ushort BitsPerPixel;
    public byte MemoryModel, RedSize, RedShift, GreenSize, GreenShift, BlueSize, BlueShift;
}

[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct DescriptorPointer { public ushort Limit; public ulong Base; }

[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct InterruptGate
{
    public ushort OffsetLow, Selector;
    public byte Ist, Attributes;
    public ushort OffsetMiddle;
    public uint OffsetHigh, Reserved;
}

[StructLayout(LayoutKind.Sequential)]
public struct InterruptFrame
{
    public ulong R15, R14, R13, R12, R11, R10, R9, R8, Rsi, Rdi, Rbp, Rdx, Rcx, Rbx, Rax;
    public ulong Vector, Error, Rip, Cs, Rflags;
    public ulong Rsp, Ss; // Long-mode interrupt stack frame; verified by INT3 self-test.
}

[StructLayout(LayoutKind.Sequential)]
public struct NativeFrameSurface
{
    public ulong Address, MappedBytes;
    public uint Width, Height, PitchBytes, BitsPerPixel;
    public uint RedMaskSize, RedMaskShift, GreenMaskSize, GreenMaskShift, BlueMaskSize, BlueMaskShift;
}

public static unsafe class Native
{
    const string Library = "xfe_native";
    [DllImport(Library, EntryPoint = "xfe_in8")] public static extern byte In8(ushort port);
    [DllImport(Library, EntryPoint = "xfe_out8")] public static extern void Out8(ushort port, byte value);
    [DllImport(Library, EntryPoint = "xfe_pause")] public static extern void Pause();
    [DllImport(Library, EntryPoint = "xfe_halt")] public static extern void Halt();
    [DllImport(Library, EntryPoint = "xfe_idle")] public static extern void Idle();
    [DllImport(Library, EntryPoint = "xfe_irq_save")] public static extern ulong IrqSave();
    [DllImport(Library, EntryPoint = "xfe_irq_restore")] public static extern void IrqRestore(ulong flags);
    [DllImport(Library, EntryPoint = "xfe_mmio_read32")] public static extern uint MmioRead32(ulong address);
    [DllImport(Library, EntryPoint = "xfe_mmio_write32")] public static extern void MmioWrite32(ulong address, uint value);
    [DllImport(Library, EntryPoint = "xfe_context_switch")] public static extern void ContextSwitch(void* current, void* next);
    [DllImport(Library, EntryPoint = "xfe_context_initialize")] public static extern void ContextInitialize(void* context);
    [DllImport(Library, EntryPoint = "xfe_thread_trampoline_address")] public static extern ulong ThreadTrampolineAddress();
    [DllImport(Library, EntryPoint = "xfe_thread_probe_address")] public static extern ulong ThreadProbeAddress();
    [DllImport(Library, EntryPoint = "xfe_boot_stack_top")] public static extern ulong BootStackTop();
    [DllImport(Library, EntryPoint = "xfe_breakpoint_stack")] public static extern ulong BreakpointStack();
    [DllImport(Library, EntryPoint = "xfe_thread_tls_create")] public static extern ulong ThreadTlsCreate();
    [DllImport(Library, EntryPoint = "xfe_thread_tls_release")] public static extern int ThreadTlsRelease(ulong control);
    [DllImport(Library, EntryPoint = "xfe_thread_runtime_exit")] public static extern int ThreadRuntimeExit(void* runtimeThread);
    [DllImport(Library, EntryPoint = "xfe_thread_try_hijack")] public static extern int TryHijack(void* runtimeThread, void* limitedContext);
    [DllImport(Library, EntryPoint = "xfe_runtime_hardware_exception_bridge")]
    public static extern int HardwareException(InterruptFrame* frame, ulong faultAddress, void* fxSave);
    [DllImport(Library, EntryPoint = "xfe_cpuid")] public static extern void CpuId(uint leaf, uint subleaf, uint* result);
    [DllImport(Library, EntryPoint = "xfe_read_cr2")] public static extern ulong ReadCr2();
    [DllImport(Library, EntryPoint = "xfe_read_cr3")] public static extern ulong ReadCr3();
    [DllImport(Library, EntryPoint = "xfe_read_cr4")] public static extern ulong ReadCr4();
    [DllImport(Library, EntryPoint = "xfe_read_msr")] public static extern ulong ReadMsr(uint register);
    [DllImport(Library, EntryPoint = "xfe_write_msr")] public static extern void WriteMsr(uint register, ulong value);
    [DllImport(Library, EntryPoint = "xfe_invalidate_page")] public static extern void InvalidatePage(ulong address);
    [DllImport(Library, EntryPoint = "xfe_libc_selftest")] public static extern int LibcSelfTest();
    [DllImport(Library, EntryPoint = "xfe_loaded_elf_selftest")] public static extern int LoadedElfSelfTest();
    [DllImport(Library, EntryPoint = "xfe_stdio_selftest")] public static extern int StdioSelfTest();
    [DllImport(Library, EntryPoint = "xfe_probe_info")] public static extern ulong ProbeInfo(uint kind);
    [DllImport(Library, EntryPoint = "xfe_probe_access")] public static extern void ProbeAccess(ulong address, uint kind);
    [DllImport(Library, EntryPoint = "xfe_read_tsc")] public static extern ulong ReadTsc();
    [DllImport(Library, EntryPoint = "xfe_storage")] public static extern void* Storage(uint kind);
    [DllImport(Library, EntryPoint = "xfe_kernel_image_start")] public static extern ulong KernelImageStart();
    [DllImport(Library, EntryPoint = "xfe_kernel_image_end")] public static extern ulong KernelImageEnd();
    [DllImport(Library, EntryPoint = "xfe_load_gdt")] public static extern void LoadGdt(DescriptorPointer* pointer);
    [DllImport(Library, EntryPoint = "xfe_load_idt")] public static extern void LoadIdt(DescriptorPointer* pointer);
    [DllImport(Library, EntryPoint = "xfe_read_gdt")] public static extern void ReadGdt(DescriptorPointer* pointer);
    [DllImport(Library, EntryPoint = "xfe_read_idt")] public static extern void ReadIdt(DescriptorPointer* pointer);
    [DllImport(Library, EntryPoint = "xfe_load_tss")] public static extern void LoadTss(ushort selector);
    [DllImport(Library, EntryPoint = "xfe_isr_address")] public static extern ulong IsrAddress(uint vector);
    [DllImport(Library, EntryPoint = "xfe_breakpoint")] public static extern void Breakpoint();
    [DllImport(Library, EntryPoint = "xfe_memory_barrier")] public static extern void MemoryBarrier();
}