mirror of
https://github.com/Atmosphere-NX/Atmosphere.git
synced 2025-05-28 05:34:11 -04:00
kern: implement KUserPointer (and test with QueryMemory) in advance of svc dev
This commit is contained in:
parent
9f9593e05f
commit
efae01c165
17 changed files with 460 additions and 72 deletions
|
@ -16,7 +16,7 @@
|
|||
#pragma once
|
||||
#include <vapours.hpp>
|
||||
#include <mesosphere/arch/arm64/kern_cpu_system_registers.hpp>
|
||||
#include <mesosphere/arch/arm64/kern_userspace_memory_access.hpp>
|
||||
#include <mesosphere/kern_select_userspace_memory_access.hpp>
|
||||
|
||||
namespace ams::kern::arch::arm64::cpu {
|
||||
|
||||
|
|
|
@ -52,6 +52,10 @@ namespace ams::kern::arch::arm64 {
|
|||
return this->page_table.SetMaxHeapSize(size);
|
||||
}
|
||||
|
||||
Result QueryInfo(KMemoryInfo *out_info, ams::svc::PageInfo *out_page_info, KProcessAddress addr) const {
|
||||
return this->page_table.QueryInfo(out_info, out_page_info, addr);
|
||||
}
|
||||
|
||||
Result MapIo(KPhysicalAddress phys_addr, size_t size, KMemoryPermission perm) {
|
||||
return this->page_table.MapIo(phys_addr, size, perm);
|
||||
}
|
||||
|
@ -84,7 +88,7 @@ namespace ams::kern::arch::arm64 {
|
|||
return this->page_table.GetPhysicalAddress(out, address);
|
||||
}
|
||||
|
||||
bool CanContain(KProcessAddress addr, size_t size) const { return this->page_table.CanContain(addr, size); }
|
||||
bool Contains(KProcessAddress addr, size_t size) const { return this->page_table.Contains(addr, size); }
|
||||
bool CanContain(KProcessAddress addr, size_t size, KMemoryState state) const { return this->page_table.CanContain(addr, size, state); }
|
||||
|
||||
KProcessAddress GetAddressSpaceStart() const { return this->page_table.GetAddressSpaceStart(); }
|
||||
|
|
|
@ -18,13 +18,40 @@
|
|||
|
||||
namespace ams::kern::arch::arm64 {
|
||||
|
||||
void UserspaceMemoryAccessFunctionAreaBegin();
|
||||
void UserspaceAccessFunctionAreaBegin();
|
||||
|
||||
bool StoreDataCache(uintptr_t start, uintptr_t end);
|
||||
bool FlushDataCache(uintptr_t start, uintptr_t end);
|
||||
bool InvalidateDataCache(uintptr_t start, uintptr_t end);
|
||||
bool InvalidateInstructionCache(uintptr_t start, uintptr_t end);
|
||||
class UserspaceAccess {
|
||||
public:
|
||||
static bool CopyMemoryFromUser(void *dst, const void *src, size_t size);
|
||||
static bool CopyMemoryFromUserAligned32Bit(void *dst, const void *src, size_t size);
|
||||
static bool CopyMemoryFromUserAligned64Bit(void *dst, const void *src, size_t size);
|
||||
static bool CopyMemoryFromUserSize32Bit(void *dst, const void *src);
|
||||
static s32 CopyStringFromUser(void *dst, const void *src, size_t size);
|
||||
|
||||
void UserspaceMemoryAccessFunctionAreaEnd();
|
||||
static bool CopyMemoryToUser(void *dst, const void *src, size_t size);
|
||||
static bool CopyMemoryToUserAligned32Bit(void *dst, const void *src, size_t size);
|
||||
static bool CopyMemoryToUserAligned64Bit(void *dst, const void *src, size_t size);
|
||||
static bool CopyMemoryToUserSize32Bit(void *dst, const void *src);
|
||||
static s32 CopyStringToUser(void *dst, const void *src, size_t size);
|
||||
|
||||
static bool ClearMemory(void *dst, size_t size);
|
||||
static bool ClearMemoryAligned32Bit(void *dst, size_t size);
|
||||
static bool ClearMemorySize32Bit(void *dst);
|
||||
|
||||
static bool StoreDataCache(uintptr_t start, uintptr_t end);
|
||||
static bool FlushDataCache(uintptr_t start, uintptr_t end);
|
||||
static bool InvalidateDataCache(uintptr_t start, uintptr_t end);
|
||||
static bool InvalidateInstructionCache(uintptr_t start, uintptr_t end);
|
||||
|
||||
static bool ReadIoMemory32Bit(void *dst, const void *src, size_t size);
|
||||
static bool ReadIoMemory16Bit(void *dst, const void *src, size_t size);
|
||||
static bool ReadIoMemory8Bit(void *dst, const void *src, size_t size);
|
||||
static bool WriteIoMemory32Bit(void *dst, const void *src, size_t size);
|
||||
static bool WriteIoMemory16Bit(void *dst, const void *src, size_t size);
|
||||
static bool WriteIoMemory8Bit(void *dst, const void *src, size_t size);
|
||||
};
|
||||
|
||||
|
||||
void UserspaceAccessFunctionAreaEnd();
|
||||
|
||||
}
|
|
@ -221,6 +221,9 @@ namespace ams::kern {
|
|||
constexpr ALWAYS_INLINE void Reset(T *o) {
|
||||
KScopedAutoObject(o).Swap(*this);
|
||||
}
|
||||
|
||||
constexpr ALWAYS_INLINE bool IsNull() const { return this->obj == nullptr; }
|
||||
constexpr ALWAYS_INLINE bool IsNotNull() const { return this->obj != nullptr; }
|
||||
};
|
||||
|
||||
|
||||
|
|
|
@ -26,6 +26,9 @@ namespace ams::kern {
|
|||
return util::BitPack32{handle};
|
||||
}
|
||||
|
||||
class KProcess;
|
||||
class KThread;
|
||||
|
||||
class KHandleTable {
|
||||
NON_COPYABLE(KHandleTable);
|
||||
NON_MOVEABLE(KHandleTable);
|
||||
|
@ -125,6 +128,19 @@ namespace ams::kern {
|
|||
template<typename T = KAutoObject>
|
||||
ALWAYS_INLINE KScopedAutoObject<T> GetObject(ams::svc::Handle handle) const {
|
||||
MESOSPHERE_ASSERT_THIS();
|
||||
|
||||
/* Handle pseudo-handles. */
|
||||
if constexpr (std::is_same<T, KProcess>::value) {
|
||||
if (handle == ams::svc::PseudoHandle::CurrentProcess) {
|
||||
return GetCurrentProcessPointer();
|
||||
}
|
||||
} else if constexpr (std::is_same<T, KThread>::value) {
|
||||
if (handle == ams::svc::PseudoHandle::CurrentThread) {
|
||||
return GetCurrentThreadPointer();
|
||||
}
|
||||
}
|
||||
|
||||
/* Lock and look up in table. */
|
||||
KScopedDisableDispatch dd;
|
||||
KScopedSpinLock lk(this->lock);
|
||||
|
||||
|
@ -139,6 +155,21 @@ namespace ams::kern {
|
|||
ALWAYS_INLINE KScopedAutoObject<T> GetObjectForIpc(ams::svc::Handle handle) const {
|
||||
static_assert(!std::is_base_of<KInterruptEvent, T>::value);
|
||||
|
||||
/* Handle pseudo-handles. */
|
||||
if constexpr (std::is_same<T, KProcess>::value) {
|
||||
if (handle == ams::svc::PseudoHandle::CurrentProcess) {
|
||||
return GetCurrentProcessPointer();
|
||||
}
|
||||
} else if constexpr (std::is_same<T, KThread>::value) {
|
||||
if (handle == ams::svc::PseudoHandle::CurrentThread) {
|
||||
return GetCurrentThreadPointer();
|
||||
}
|
||||
}
|
||||
|
||||
/* Lock and look up in table. */
|
||||
KScopedDisableDispatch dd;
|
||||
KScopedSpinLock lk(this->lock);
|
||||
|
||||
KAutoObject *obj = this->GetObjectImpl(handle);
|
||||
if (obj->DynamicCast<KInterruptEvent *>() != nullptr) {
|
||||
return nullptr;
|
||||
|
|
|
@ -172,11 +172,11 @@ namespace ams::kern {
|
|||
constexpr bool IsKernel() const { return this->is_kernel; }
|
||||
constexpr bool IsAslrEnabled() const { return this->enable_aslr; }
|
||||
|
||||
constexpr bool CanContain(KProcessAddress addr) const {
|
||||
constexpr bool Contains(KProcessAddress addr) const {
|
||||
return this->address_space_start <= addr && addr <= this->address_space_end - 1;
|
||||
}
|
||||
|
||||
constexpr bool CanContain(KProcessAddress addr, size_t size) const {
|
||||
constexpr bool Contains(KProcessAddress addr, size_t size) const {
|
||||
return this->address_space_start <= addr && addr < addr + size && addr + size - 1 <= this->address_space_end - 1;
|
||||
}
|
||||
|
||||
|
@ -249,6 +249,7 @@ namespace ams::kern {
|
|||
Result SetProcessMemoryPermission(KProcessAddress addr, size_t size, ams::svc::MemoryPermission perm);
|
||||
Result SetHeapSize(KProcessAddress *out, size_t size);
|
||||
Result SetMaxHeapSize(size_t size);
|
||||
Result QueryInfo(KMemoryInfo *out_info, ams::svc::PageInfo *out_page_info, KProcessAddress addr) const;
|
||||
Result MapIo(KPhysicalAddress phys_addr, size_t size, KMemoryPermission perm);
|
||||
Result MapStatic(KPhysicalAddress phys_addr, size_t size, KMemoryPermission perm);
|
||||
Result MapRegion(KMemoryRegionType region_type, KMemoryPermission perm);
|
||||
|
|
|
@ -123,6 +123,8 @@ namespace ams::kern {
|
|||
|
||||
Result Initialize(const ams::svc::CreateProcessParameter ¶ms, const KPageGroup &pg, const u32 *caps, s32 num_caps, KResourceLimit *res_limit, KMemoryManager::Pool pool);
|
||||
|
||||
constexpr const char *GetName() const { return this->name; }
|
||||
|
||||
constexpr u64 GetProcessId() const { return this->process_id; }
|
||||
|
||||
constexpr u64 GetCoreMask() const { return this->capabilities.GetCoreMask(); }
|
||||
|
|
|
@ -0,0 +1,31 @@
|
|||
/*
|
||||
* Copyright (c) 2018-2020 Atmosphère-NX
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it
|
||||
* under the terms and conditions of the GNU General Public License,
|
||||
* version 2, as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#pragma once
|
||||
#include <mesosphere/kern_common.hpp>
|
||||
|
||||
#ifdef ATMOSPHERE_ARCH_ARM64
|
||||
#include <mesosphere/arch/arm64/kern_userspace_memory_access.hpp>
|
||||
|
||||
namespace ams::kern {
|
||||
|
||||
using ams::kern::arch::arm64::UserspaceAccess;
|
||||
|
||||
}
|
||||
|
||||
#else
|
||||
#error "Unknown architecture for CPU"
|
||||
#endif
|
||||
|
|
@ -16,18 +16,187 @@
|
|||
#pragma once
|
||||
#include <vapours.hpp>
|
||||
#include <mesosphere/svc/kern_svc_results.hpp>
|
||||
#include <mesosphere/kern_select_userspace_memory_access.hpp>
|
||||
|
||||
namespace ams::kern::svc {
|
||||
|
||||
/* TODO: Actually implement this type. */
|
||||
template<typename T>
|
||||
struct KUserPointer : impl::KUserPointerTag {
|
||||
namespace impl {
|
||||
|
||||
/* TODO: C++20
|
||||
template<typename T>
|
||||
concept Pointer = std::is_pointer<T>::value;
|
||||
|
||||
template<typename T>
|
||||
concept NonConstPointer = Pointer<T> && !std::is_const<typename std::remove_pointer<T>::type>::value;
|
||||
|
||||
template<typename T>
|
||||
concept ConstPointer = Pointer<T> && std::is_const<typename std::remove_pointer<T>::type>::value;
|
||||
|
||||
template<typename T, size_t N>
|
||||
concept AlignedNPointer = Pointer<T> && alignof(typename std::remove_pointer<T>::type) >= N && util::IsAligned(sizeof(typename std::remove_pointer<T>::type), N);
|
||||
|
||||
template<typename T>
|
||||
concept Aligned8Pointer = AlignedNPointer<T, sizeof(u8)>;
|
||||
|
||||
template<typename T>
|
||||
concept Aligned16Pointer = AlignedNPointer<T, sizeof(u16)> && Aligned8<T>;
|
||||
|
||||
template<typename T>
|
||||
concept Aligned32Pointer = AlignedNPointer<T, sizeof(u32)> && Aligned16<T>;
|
||||
|
||||
template<typename T>
|
||||
concept Aligned64Pointer = AlignedNPointer<T, sizeof(u64)> && Aligned32<T>;
|
||||
*/
|
||||
|
||||
template<typename T>
|
||||
constexpr inline bool IsPointer = std::is_pointer<T>::value;
|
||||
|
||||
template<typename T>
|
||||
constexpr inline bool IsConstPointer = IsPointer<T> && std::is_const<typename std::remove_pointer<T>::type>::value;
|
||||
|
||||
template<typename T>
|
||||
constexpr inline bool IsNonConstPointer = IsPointer<T> && !std::is_const<typename std::remove_pointer<T>::type>::value;
|
||||
|
||||
template<typename T, size_t N>
|
||||
constexpr inline bool IsAlignedNPointer = IsPointer<T> && alignof(typename std::remove_pointer<T>::type) >= N && util::IsAligned(sizeof(typename std::remove_pointer<T>::type), N);
|
||||
|
||||
template<typename _T, typename = void> /* requires Aligned8Pointer<_T> */
|
||||
class KUserPointerImplTraits {
|
||||
static_assert(IsAlignedNPointer<_T, sizeof(u8)>);
|
||||
public:
|
||||
using T = typename std::remove_const<typename std::remove_pointer<_T>::type>::type;
|
||||
public:
|
||||
static Result CopyFromUserspace(void *dst, const void *src, size_t size) {
|
||||
R_UNLESS(UserspaceAccess::CopyMemoryFromUser(dst, src, size), svc::ResultInvalidPointer());
|
||||
return ResultSuccess();
|
||||
}
|
||||
|
||||
static Result CopyToUserspace(void *dst, const void *src, size_t size) {
|
||||
R_UNLESS(UserspaceAccess::CopyMemoryToUser(dst, src, size), svc::ResultInvalidPointer());
|
||||
return ResultSuccess();
|
||||
}
|
||||
};
|
||||
|
||||
template<typename _T> /* requires Aligned32Pointer<_T> */
|
||||
class KUserPointerImplTraits<_T, typename std::enable_if<IsAlignedNPointer<_T, sizeof(u32)> && !IsAlignedNPointer<_T, sizeof(u64)>>::type> {
|
||||
static_assert(IsAlignedNPointer<_T, sizeof(u32)>);
|
||||
public:
|
||||
using T = typename std::remove_const<typename std::remove_pointer<_T>::type>::type;
|
||||
public:
|
||||
static Result CopyFromUserspace(void *dst, const void *src, size_t size) {
|
||||
R_UNLESS(UserspaceAccess::CopyMemoryFromUserAligned32Bit(dst, src, size), svc::ResultInvalidPointer());
|
||||
return ResultSuccess();
|
||||
}
|
||||
|
||||
static Result CopyToUserspace(void *dst, const void *src, size_t size) {
|
||||
R_UNLESS(UserspaceAccess::CopyMemoryToUserAligned32Bit(dst, src, size), svc::ResultInvalidPointer());
|
||||
return ResultSuccess();
|
||||
}
|
||||
};
|
||||
|
||||
template<typename _T> /* requires Aligned64Pointer<_T> */
|
||||
class KUserPointerImplTraits<_T, typename std::enable_if<IsAlignedNPointer<_T, sizeof(u64)>>::type> {
|
||||
static_assert(IsAlignedNPointer<_T, sizeof(u64)>);
|
||||
public:
|
||||
using T = typename std::remove_const<typename std::remove_pointer<_T>::type>::type;
|
||||
public:
|
||||
static Result CopyFromUserspace(void *dst, const void *src, size_t size) {
|
||||
R_UNLESS(UserspaceAccess::CopyMemoryFromUserAligned64Bit(dst, src, size), svc::ResultInvalidPointer());
|
||||
return ResultSuccess();
|
||||
}
|
||||
|
||||
static Result CopyToUserspace(void *dst, const void *src, size_t size) {
|
||||
R_UNLESS(UserspaceAccess::CopyMemoryToUserAligned64Bit(dst, src, size), svc::ResultInvalidPointer());
|
||||
return ResultSuccess();
|
||||
}
|
||||
};
|
||||
|
||||
template<typename _T> /* requires Aligned8Pointer<_T> */
|
||||
class KUserPointerImpl : impl::KUserPointerTag {
|
||||
private:
|
||||
using Traits = KUserPointerImplTraits<_T>;
|
||||
protected:
|
||||
using T = typename std::remove_const<typename std::remove_pointer<_T>::type>::type;
|
||||
private:
|
||||
_T *ptr;
|
||||
private:
|
||||
Result CopyToImpl(void *p, size_t size) const {
|
||||
return Traits::CopyFromUserspace(p, this->ptr, size);
|
||||
}
|
||||
|
||||
Result CopyFromImpl(const void *p, size_t size) const {
|
||||
return Traits::CopyToUserspace(this->ptr, p, size);
|
||||
}
|
||||
protected:
|
||||
Result CopyTo(T *p) const { return this->CopyToImpl(p, sizeof(*p)); }
|
||||
Result CopyFrom(const T *p) const { return this->CopyFromImpl(p, sizeof(*p)); }
|
||||
|
||||
Result CopyArrayElementTo(T *p, size_t index) const { return Traits::CopyFromUserspace(p, this->ptr + index, sizeof(*p)); }
|
||||
Result CopyArrayElementFrom(const T *p, size_t index) const { return Traits::CopyToUserspace(this->ptr + index, p, sizeof(*p)); }
|
||||
|
||||
Result CopyArrayTo(T *arr, size_t count) const { return this->CopyToImpl(arr, sizeof(*arr) * count); }
|
||||
Result CopyArrayFrom(const T *arr, size_t count) const { return this->CopyFromImpl(arr, sizeof(*arr) * count); }
|
||||
|
||||
constexpr bool IsNull() const { return this->ptr == nullptr; }
|
||||
};
|
||||
|
||||
template<>
|
||||
class KUserPointerImpl<const char *> : impl::KUserPointerTag {
|
||||
private:
|
||||
using Traits = KUserPointerImplTraits<const char *>;
|
||||
protected:
|
||||
using T = char;
|
||||
private:
|
||||
const char *ptr;
|
||||
protected:
|
||||
Result CopyStringTo(char *dst, size_t size) const {
|
||||
static_assert(sizeof(char) == 1);
|
||||
R_UNLESS(UserspaceAccess::CopyStringFromUser(dst, this->ptr, size) > 0, svc::ResultInvalidPointer());
|
||||
return ResultSuccess();
|
||||
}
|
||||
|
||||
Result CopyArrayElementTo(char *dst, size_t index) const {
|
||||
return Traits::CopyFromUserspace(dst, this->ptr + index, sizeof(*dst));
|
||||
}
|
||||
|
||||
constexpr bool IsNull() const { return this->ptr == nullptr; }
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
template<typename T, typename = void>
|
||||
class KUserPointer;
|
||||
|
||||
template<typename T> /* requires impl::ConstPointer<T> */
|
||||
struct KUserPointer<T, typename std::enable_if<impl::IsConstPointer<T>>::type> : public impl::KUserPointerImpl<T> {
|
||||
public:
|
||||
static_assert(std::is_pointer<T>::value);
|
||||
static constexpr bool IsInput = std::is_const<typename std::remove_pointer<T>::type>::value;
|
||||
private:
|
||||
T pointer;
|
||||
static constexpr bool IsInput = true;
|
||||
public:
|
||||
using impl::KUserPointerImpl<T>::CopyTo;
|
||||
using impl::KUserPointerImpl<T>::CopyArrayElementTo;
|
||||
using impl::KUserPointerImpl<T>::CopyArrayTo;
|
||||
using impl::KUserPointerImpl<T>::IsNull;
|
||||
};
|
||||
|
||||
template<typename T> /* requires impl::NonConstPointer<T> */
|
||||
struct KUserPointer<T, typename std::enable_if<impl::IsNonConstPointer<T>>::type> : public impl::KUserPointerImpl<T> {
|
||||
public:
|
||||
static constexpr bool IsInput = false;
|
||||
public:
|
||||
using impl::KUserPointerImpl<T>::CopyFrom;
|
||||
using impl::KUserPointerImpl<T>::CopyArrayElementFrom;
|
||||
using impl::KUserPointerImpl<T>::CopyArrayFrom;
|
||||
using impl::KUserPointerImpl<T>::IsNull;
|
||||
};
|
||||
|
||||
template<>
|
||||
struct KUserPointer<const char *> : public impl::KUserPointerImpl<const char *> {
|
||||
public:
|
||||
static constexpr bool IsInput = true;
|
||||
public:
|
||||
using impl::KUserPointerImpl<const char *>::CopyStringTo;
|
||||
using impl::KUserPointerImpl<const char *>::CopyArrayElementTo;
|
||||
using impl::KUserPointerImpl<const char *>::IsNull;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue