Implemented syscalls and jump to usermode.
This commit is contained in:
57
targets/_any/include/os/cpu.hpp
Normal file
57
targets/_any/include/os/cpu.hpp
Normal file
@@ -0,0 +1,57 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#if !defined(BAD_APPLE_OS_CPU_HPP_INCLUDED)
|
||||
#define BAD_APPLE_OS_CPU_HPP_INCLUDED
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
namespace baos::cpu
|
||||
{
|
||||
enum class MSR : std::uint32_t
|
||||
{
|
||||
IA32_EFER = 0xC000'0080,
|
||||
STAR = 0xC000'0081, // segments for syscall
|
||||
LSTAR = 0xC000'0082, // instruction pointer for syscall
|
||||
};
|
||||
inline constexpr std::uint64_t IA32_EFER_SYSTEM_CALL_EXTENSIONS_BIT = (1 << 0);
|
||||
inline constexpr std::uint64_t IA32_EFER_LONG_MODE_ENABLE_BIT = (1 << 8);
|
||||
inline constexpr std::uint64_t IA32_EFER_LONG_MODE_ACTIVE_BIT = (1 << 10);
|
||||
inline constexpr std::uint64_t IA32_EFER_NO_EXECUTE_ENABLE_BIT = (1 << 11);
|
||||
// ...
|
||||
|
||||
inline std::uint64_t readMSR(MSR msr) noexcept
|
||||
{
|
||||
std::uint32_t lowHalf = 0;
|
||||
std::uint32_t highHalf = 0;
|
||||
__asm__ __volatile__(
|
||||
"rdmsr"
|
||||
: "=a"(lowHalf), "=d"(highHalf)
|
||||
: "c"(msr)
|
||||
);
|
||||
return static_cast<std::uint64_t>(highHalf) << 32 | lowHalf;
|
||||
}
|
||||
|
||||
inline void writeMSR(MSR msr, std::uint64_t value) noexcept
|
||||
{
|
||||
std::uint32_t lowHalf = static_cast<std::uint32_t>(value & 0xFFFFFFFF);
|
||||
std::uint32_t highHalf = static_cast<std::uint32_t>(value >> 32);
|
||||
__asm__ __volatile__(
|
||||
"wrmsr"
|
||||
:
|
||||
: "a"(lowHalf), "d"(highHalf), "c"(msr)
|
||||
);
|
||||
}
|
||||
|
||||
inline void setMSRBits(MSR msr, std::uint64_t bits) noexcept
|
||||
{
|
||||
writeMSR(msr, readMSR(msr) | bits);
|
||||
}
|
||||
|
||||
inline void unsetMSRBits(MSR msr, std::uint64_t bits) noexcept
|
||||
{
|
||||
writeMSR(msr, readMSR(msr) & ~bits);
|
||||
}
|
||||
}
|
||||
|
||||
#endif // !defined(BAD_APPLE_OS_CPU_HPP_INCLUDED)
|
||||
18
targets/_any/include/os/segments.hpp
Normal file
18
targets/_any/include/os/segments.hpp
Normal file
@@ -0,0 +1,18 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#if !defined(BAD_APPLE_OS_SEGMENTS_HPP_INCLUDED)
|
||||
#define BAD_APPLE_OS_SEGMENTS_HPP_INCLUDED
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
namespace baos
|
||||
{
|
||||
inline constexpr std::uint16_t SEGIDX_KERNEL_CODE = 1 << 3;
|
||||
inline constexpr std::uint16_t SEGIDX_KERNEL_DATA = 2 << 3;
|
||||
inline constexpr std::uint16_t SEGIDX_USER_CODE = 4 << 3;
|
||||
inline constexpr std::uint16_t SEGIDX_USER_DATA = 3 << 3;
|
||||
inline constexpr std::uint16_t SEGIDX_TSS = 5 << 3;
|
||||
}
|
||||
|
||||
#endif // !defined(BAD_APPLE_OS_SEGMENTS_HPP_INCLUDED)
|
||||
33
targets/_any/include/os/syscall.hpp
Normal file
33
targets/_any/include/os/syscall.hpp
Normal file
@@ -0,0 +1,33 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#if !defined(BAD_APPLE_OS_SYSCALL_HPP_INCLUDED)
|
||||
#define BAD_APPLE_OS_SYSCALL_HPP_INCLUDED
|
||||
|
||||
#include <bit>
|
||||
#include <cstdint>
|
||||
|
||||
namespace baos
|
||||
{
|
||||
enum class Syscall : std::uint64_t
|
||||
{
|
||||
FILE_READ = 0,
|
||||
FILE_WRITE = 1
|
||||
};
|
||||
|
||||
void setupSyscall() noexcept;
|
||||
|
||||
template<typename TParam0, typename TParam1, typename TParam2>
|
||||
inline void doSyscall(Syscall cmd, TParam0 param0 = 0, TParam1 param1 = 0, TParam2 param2 = 0) noexcept
|
||||
{
|
||||
register std::uint64_t r8 asm("r8") = param2;
|
||||
__asm__ __volatile__(
|
||||
"syscall"
|
||||
:
|
||||
: "D"(cmd), "S"(param0), "d"(param1)
|
||||
: "%rcx"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#endif // !defined(BAD_APPLE_OS_SYSCALL_HPP_INCLUDED)
|
||||
Reference in New Issue
Block a user