kernel: Improve accuracy of KResourceLimit emulation (#7221)

* core: Refactor resource limits

* svc: Implement SetResourceLimitLimitValues

* Also correct existing name and add missing error codes
This commit is contained in:
GPUCode
2023-12-04 13:31:06 +02:00
committed by GitHub
parent 875f5eaad5
commit 59df319f48
28 changed files with 421 additions and 301 deletions
+30 -91
View File
@@ -16,23 +16,24 @@
namespace Kernel {
enum class ResourceLimitCategory : u8 {
APPLICATION = 0,
SYS_APPLET = 1,
LIB_APPLET = 2,
OTHER = 3
Application = 0,
SysApplet = 1,
LibApplet = 2,
Other = 3,
};
enum ResourceTypes {
PRIORITY = 0,
COMMIT = 1,
THREAD = 2,
EVENT = 3,
MUTEX = 4,
SEMAPHORE = 5,
TIMER = 6,
SHARED_MEMORY = 7,
ADDRESS_ARBITER = 8,
CPU_TIME = 9,
enum class ResourceLimitType : u32 {
Priority = 0,
Commit = 1,
Thread = 2,
Event = 3,
Mutex = 4,
Semaphore = 5,
Timer = 6,
SharedMemory = 7,
AddressArbiter = 8,
CpuTime = 9,
Max = 10,
};
class ResourceLimit final : public Object {
@@ -50,7 +51,7 @@ public:
return "ResourceLimit";
}
std::string GetName() const override {
return name;
return m_name;
}
static constexpr HandleType HANDLE_TYPE = HandleType::ResourceLimit;
@@ -58,90 +59,28 @@ public:
return HANDLE_TYPE;
}
/**
* Gets the current value for the specified resource.
* @param resource Requested resource type
* @returns The current value of the resource type
*/
s32 GetCurrentResourceValue(u32 resource) const;
s32 GetCurrentValue(ResourceLimitType type) const;
s32 GetLimitValue(ResourceLimitType type) const;
/**
* Gets the max value for the specified resource.
* @param resource Requested resource type
* @returns The max value of the resource type
*/
u32 GetMaxResourceValue(u32 resource) const;
void SetLimitValue(ResourceLimitType name, s32 value);
/// Name of resource limit object.
std::string name;
bool Reserve(ResourceLimitType type, s32 amount);
bool Release(ResourceLimitType type, s32 amount);
/// Max thread priority that a process in this category can create
s32 max_priority = 0;
/// Max memory that processes in this category can use
s32 max_commit = 0;
///< Max number of objects that can be collectively created by the processes in this category
s32 max_threads = 0;
s32 max_events = 0;
s32 max_mutexes = 0;
s32 max_semaphores = 0;
s32 max_timers = 0;
s32 max_shared_mems = 0;
s32 max_address_arbiters = 0;
/// Max CPU time that the processes in this category can utilize
s32 max_cpu_time = 0;
// TODO(Subv): Increment these in their respective Kernel::T::Create functions, keeping in mind
// that APPLICATION resource limits should not be affected by the objects created by service
// modules.
// Currently we have no way of distinguishing if a Create was called by the running application,
// or by a service module. Approach this once we have separated the service modules into their
// own processes
/// Current memory that the processes in this category are using
s32 current_commit = 0;
///< Current number of objects among all processes in this category
s32 current_threads = 0;
s32 current_events = 0;
s32 current_mutexes = 0;
s32 current_semaphores = 0;
s32 current_timers = 0;
s32 current_shared_mems = 0;
s32 current_address_arbiters = 0;
/// Current CPU time that the processes in this category are utilizing
s32 current_cpu_time = 0;
private:
using ResourceArray = std::array<s32, static_cast<size_t>(ResourceLimitType::Max)>;
ResourceArray m_limit_values{};
ResourceArray m_current_values{};
std::string m_name;
private:
friend class boost::serialization::access;
template <class Archive>
void serialize(Archive& ar, const unsigned int file_version) {
ar& boost::serialization::base_object<Object>(*this);
// NB most of these aren't used at all currently, but we're adding them here for forwards
// compatibility
ar& name;
ar& max_priority;
ar& max_commit;
ar& max_threads;
ar& max_events;
ar& max_mutexes;
ar& max_semaphores;
ar& max_timers;
ar& max_shared_mems;
ar& max_address_arbiters;
ar& max_cpu_time;
ar& current_commit;
ar& current_threads;
ar& current_events;
ar& current_mutexes;
ar& current_semaphores;
ar& current_timers;
ar& current_shared_mems;
ar& current_address_arbiters;
ar& current_cpu_time;
ar& m_name;
ar& m_limit_values;
ar& m_current_values;
}
};