2023-05-30 23:55:42 -07:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
2023-06-06 20:13:07 -07:00
|
|
|
#include "lib/ref_ptr.h"
|
|
|
|
#include "object/kernel_object.h"
|
2023-05-30 23:55:42 -07:00
|
|
|
|
|
|
|
class Process;
|
2023-06-06 16:24:03 -07:00
|
|
|
class Thread;
|
2023-05-30 23:55:42 -07:00
|
|
|
|
2023-06-07 06:21:36 -07:00
|
|
|
class Capability : public RefCounted<Capability> {
|
2023-05-30 23:55:42 -07:00
|
|
|
public:
|
2023-06-16 15:27:09 -07:00
|
|
|
Capability(const RefPtr<KernelObject>& obj, uint64_t permissions)
|
|
|
|
: obj_(obj), permissions_(permissions) {}
|
2023-05-30 23:55:42 -07:00
|
|
|
|
2023-06-06 20:13:07 -07:00
|
|
|
template <typename T>
|
2023-06-16 15:27:09 -07:00
|
|
|
Capability(const RefPtr<T>& obj, uint64_t permissions)
|
|
|
|
: Capability(StaticCastRefPtr<KernelObject>(obj), permissions) {}
|
2023-06-06 20:13:07 -07:00
|
|
|
|
2023-05-30 23:55:42 -07:00
|
|
|
template <typename T>
|
2023-06-07 00:04:53 -07:00
|
|
|
RefPtr<T> obj();
|
2023-05-30 23:55:42 -07:00
|
|
|
|
2023-06-20 15:50:49 -07:00
|
|
|
RefPtr<KernelObject> raw_obj() { return obj_; }
|
|
|
|
|
2023-05-30 23:55:42 -07:00
|
|
|
uint64_t permissions() { return permissions_; }
|
|
|
|
bool HasPermissions(uint64_t requested) {
|
|
|
|
return (permissions_ & requested) == requested;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2023-06-06 20:13:07 -07:00
|
|
|
RefPtr<KernelObject> obj_;
|
2023-05-30 23:55:42 -07:00
|
|
|
uint64_t permissions_;
|
|
|
|
};
|
2023-06-16 14:53:57 -07:00
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
RefPtr<T> Capability::obj() {
|
|
|
|
if (obj_->TypeTag() != KernelObjectTag<T>::type) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return StaticCastRefPtr<T>(obj_);
|
|
|
|
}
|
2023-06-20 15:50:49 -07:00
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
z_err_t ValidateCapability(const RefPtr<Capability>& cap,
|
|
|
|
uint64_t permissions) {
|
|
|
|
if (!cap) {
|
|
|
|
return Z_ERR_CAP_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cap->raw_obj()->TypeTag() != KernelObjectTag<T>::type) {
|
|
|
|
return Z_ERR_CAP_TYPE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!cap->HasPermissions(permissions)) {
|
|
|
|
return Z_ERR_CAP_DENIED;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Z_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define RET_IF_NULL(expr) \
|
|
|
|
{ \
|
|
|
|
if (!expr) { \
|
|
|
|
return Z_ERR_CAP_TYPE; \
|
|
|
|
} \
|
|
|
|
}
|