diff --git a/src/kernel/objects/kobject.cpp b/src/kernel/objects/kobject.cpp index 8b2adb5..f3e4b1c 100644 --- a/src/kernel/objects/kobject.cpp +++ b/src/kernel/objects/kobject.cpp @@ -7,30 +7,29 @@ namespace obj { -static j6_koid_t next_koids [static_cast(kobject::type::max)] = { 0 }; +static constexpr unsigned types_count = static_cast(kobject::type::max); + +static uint32_t next_oids [types_count] = { 0 }; + +static_assert(types_count <= (1 << kobject::koid_type_bits), + "kobject::koid_type_bits cannot represent all kobject types"); + +static uint32_t +oid_generate(kobject::type t) +{ + kassert(t < kobject::type::max, "Object type out of bounds"); + unsigned type_int = static_cast(t); + return __atomic_fetch_add(&next_oids[type_int], 1, __ATOMIC_RELAXED); +} kobject::kobject(type t) : - m_koid(koid_generate(t)), - m_handle_count(0) + m_handle_count {0}, + m_type {t}, + m_obj_id {oid_generate(t)} {} kobject::~kobject() {} -j6_koid_t -kobject::koid_generate(type t) -{ - kassert(t < type::max, "Object type out of bounds"); - uint64_t type_int = static_cast(t); - uint64_t id = __atomic_fetch_add(&next_koids[type_int], 1, __ATOMIC_SEQ_CST); - return (type_int << 48) | id; -} - -kobject::type -kobject::koid_type(j6_koid_t koid) -{ - return static_cast((koid >> 48) & 0xffffull); -} - void kobject::on_no_handles() { diff --git a/src/kernel/objects/kobject.h b/src/kernel/objects/kobject.h index 58fa239..93bdfaa 100644 --- a/src/kernel/objects/kobject.h +++ b/src/kernel/objects/kobject.h @@ -23,24 +23,31 @@ public: max }; + /// The koid's most significant bits represent the object's type + static constexpr unsigned koid_type_bits = 4; + static constexpr unsigned koid_type_mask = (1 << koid_type_bits) - 1; + static constexpr unsigned koid_type_shift = (8 * sizeof(j6_koid_t)) - koid_type_bits; + kobject(type t); virtual ~kobject(); - /// Generate a new koid for a given type - /// \arg t The object type - /// \returns A new unique koid - static j6_koid_t koid_generate(type t); - /// Get the kobject type from a given koid /// \arg koid An existing koid /// \returns The object type for the koid - static type koid_type(j6_koid_t koid); + inline static type koid_type(j6_koid_t koid) { + return static_cast((koid >> koid_type_shift) & koid_type_mask); + } /// Get this object's type - inline type get_type() const { return koid_type(m_koid); } + inline type get_type() const { return m_type; } /// Get this object's koid - inline j6_koid_t koid() const { return m_koid; } + inline j6_koid_t koid() const { + return (static_cast(m_type) << koid_type_shift) | m_obj_id; + } + + /// Get this object's type-relative object id + inline uint32_t obj_id() const { return m_obj_id; } /// Increment the handle refcount inline void handle_retain() { ++m_handle_count; } @@ -63,8 +70,9 @@ private: kobject(const kobject &other) = delete; kobject(const kobject &&other) = delete; - j6_koid_t m_koid; uint16_t m_handle_count; + type m_type; + uint32_t m_obj_id; }; } // namespace obj diff --git a/src/kernel/syscalls/process.cpp b/src/kernel/syscalls/process.cpp index 5157f74..562eb30 100644 --- a/src/kernel/syscalls/process.cpp +++ b/src/kernel/syscalls/process.cpp @@ -13,7 +13,7 @@ j6_status_t process_create(j6_handle_t *self) { process *p = construct_handle(self); - log::info(logs::task, "Process %llx created", p->koid()); + log::info(logs::task, "Process <%02lx> created", p->obj_id()); return j6_status_ok; } @@ -22,7 +22,7 @@ process_kill(process *self) { process &p = process::current(); - log::info(logs::task, "Process %llx killed by process %llx", self->koid(), p.koid()); + log::info(logs::task, "Process <%02lx> killed by process <%02lx>", self->obj_id(), p.obj_id()); self->exit(-1u); return j6_status_ok; @@ -32,7 +32,7 @@ j6_status_t process_exit(int32_t status) { process &p = process::current(); - log::info(logs::task, "Process %llx exiting with code %d", p.koid(), status); + log::info(logs::task, "Process <%02lx> exiting with code %d", p.obj_id(), status); p.exit(status); diff --git a/src/kernel/syscalls/system.cpp b/src/kernel/syscalls/system.cpp index 615f942..2d98a16 100644 --- a/src/kernel/syscalls/system.cpp +++ b/src/kernel/syscalls/system.cpp @@ -24,7 +24,7 @@ j6_status_t log(const char *message) { thread &th = thread::current(); - log::info(logs::syscall, "Message[%llx]: %s", th.koid(), message); + log::info(logs::syscall, "Message <%02lx:%02lx>: %s", th.parent().obj_id(), th.obj_id(), message); return j6_status_ok; } @@ -32,7 +32,7 @@ j6_status_t noop() { thread &th = thread::current(); - log::verbose(logs::syscall, "Thread %llx called noop syscall.", th.koid()); + log::verbose(logs::syscall, "Thread <%02lx:%02lx> called noop syscall.", th.parent().obj_id(), th.koid()); return j6_status_ok; } diff --git a/src/kernel/syscalls/thread.cpp b/src/kernel/syscalls/thread.cpp index 64e83e4..d6f668a 100644 --- a/src/kernel/syscalls/thread.cpp +++ b/src/kernel/syscalls/thread.cpp @@ -22,8 +22,8 @@ thread_create(j6_handle_t *self, process *proc, uintptr_t stack_top, uintptr_t e *self = child->self_handle(); child->set_state(thread::state::ready); - log::verbose(logs::task, "Thread %llx:%llx spawned new thread %llx:%llx", - parent_pr.koid(), parent_th.koid(), proc->koid(), child->koid()); + log::verbose(logs::task, "Thread <%02lx:%02lx> spawned new thread <%02lx:%02lx>", + parent_pr.obj_id(), parent_th.obj_id(), proc->obj_id(), child->obj_id()); return j6_status_ok; } @@ -31,7 +31,7 @@ thread_create(j6_handle_t *self, process *proc, uintptr_t stack_top, uintptr_t e j6_status_t thread_kill(thread *self) { - log::verbose(logs::task, "Killing thread %llx", self->koid()); + log::verbose(logs::task, "Killing thread <%02lx:%02lx>", self->parent().obj_id(), self->obj_id()); self->exit(); return j6_status_ok; } @@ -46,7 +46,7 @@ j6_status_t thread_exit() { thread &th = thread::current(); - log::verbose(logs::task, "Thread %llx exiting", th.koid()); + log::verbose(logs::task, "Thread <%02lx:%02lx> exiting", th.parent().obj_id(), th.obj_id()); th.exit(); log::error(logs::task, "returned to exit syscall"); @@ -60,7 +60,7 @@ thread_sleep(uint64_t duration) uint64_t til = clock::get().value() + duration; - log::verbose(logs::task, "Thread %llx sleeping until %llu", th.koid(), til); + log::verbose(logs::task, "Thread <%02lx:%02lx> sleeping until %llu", th.parent().obj_id(), th.obj_id(), til); th.set_wake_timeout(til); th.block(); return j6_status_ok;