mirror of
https://github.com/justinian/jsix.git
synced 2025-12-10 00:14:32 -08:00
[kernel] Pass objects not handles to syscall impls
This commit contains a couple large, interdependent changes: - In preparation for capability checking, the _syscall_verify_* functions now load most handles passed in, and verify that they exist and are of the correct type. Lists and out-handles are not converted to objects. - Also in preparation for capability checking, the internal representation of handles has changed. j6_handle_t is now 32 bits, and a new j6_cap_t (also 32 bits) is added. Handles of a process are now a util::map<j6_handle_t, handle> where handle is a new struct containing the id, capabilities, and object pointer. - The kernel object definition DSL gained a few changes to support auto generating the handle -> object conversion in the _syscall_verify_* functions, mostly knowing the object type, and an optional "cname" attribute on objects where their names differ from C++ code. (Specifically vma/vm_area) - Kernel object code and other code under kernel/objects is now in a new obj:: namespace, because fuck you <cstdlib> for putting "system" in the global namespace. Why even have that header then? - Kernel object types constructed with the construct_handle helper now have a creation_caps static member to declare what capabilities a newly created object's handle should have.
This commit is contained in:
@@ -5,7 +5,9 @@
|
||||
#include "objects/channel.h"
|
||||
#include "objects/vm_area.h"
|
||||
|
||||
extern vm_area_guarded g_kernel_buffers;
|
||||
extern obj::vm_area_guarded g_kernel_buffers;
|
||||
|
||||
namespace obj {
|
||||
|
||||
constexpr size_t buffer_bytes = mem::kernel_buffer_pages * mem::frame_size;
|
||||
|
||||
@@ -89,3 +91,5 @@ channel::on_no_handles()
|
||||
kobject::on_no_handles();
|
||||
delete this;
|
||||
}
|
||||
|
||||
} // namespace obj
|
||||
|
||||
@@ -7,11 +7,16 @@
|
||||
|
||||
#include "objects/kobject.h"
|
||||
|
||||
namespace obj {
|
||||
|
||||
/// Channels are bi-directional means of sending messages
|
||||
class channel :
|
||||
public kobject
|
||||
{
|
||||
public:
|
||||
/// Capabilities on a newly constructed channel handle
|
||||
constexpr static j6_cap_t creation_caps = 0;
|
||||
|
||||
channel();
|
||||
virtual ~channel();
|
||||
|
||||
@@ -48,3 +53,5 @@ private:
|
||||
uintptr_t m_data;
|
||||
util::bip_buffer m_buffer;
|
||||
};
|
||||
|
||||
} // namespace obj
|
||||
|
||||
@@ -5,6 +5,8 @@
|
||||
#include "objects/thread.h"
|
||||
#include "vm_space.h"
|
||||
|
||||
namespace obj {
|
||||
|
||||
endpoint::endpoint() :
|
||||
kobject {kobject::type::endpoint}
|
||||
{}
|
||||
@@ -135,3 +137,5 @@ endpoint::do_message_copy(const endpoint::thread_data &sender, endpoint::thread_
|
||||
|
||||
return j6_status_ok;
|
||||
}
|
||||
|
||||
} // namespace obj
|
||||
|
||||
@@ -7,11 +7,16 @@
|
||||
|
||||
#include "objects/kobject.h"
|
||||
|
||||
namespace obj {
|
||||
|
||||
/// Endpoints are objects that enable synchronous message-passing IPC
|
||||
class endpoint :
|
||||
public kobject
|
||||
{
|
||||
public:
|
||||
/// Capabilities on a newly constructed endpoint handle
|
||||
constexpr static j6_cap_t creation_caps = 0;
|
||||
|
||||
endpoint();
|
||||
virtual ~endpoint();
|
||||
|
||||
@@ -69,3 +74,5 @@ private:
|
||||
|
||||
util::vector<thread_data> m_blocked;
|
||||
};
|
||||
|
||||
} // namespace obj
|
||||
|
||||
@@ -4,12 +4,19 @@
|
||||
|
||||
#include "objects/kobject.h"
|
||||
|
||||
namespace obj {
|
||||
|
||||
class event :
|
||||
public kobject
|
||||
{
|
||||
public:
|
||||
/// Capabilities on a newly constructed event handle
|
||||
constexpr static j6_cap_t creation_caps = 0;
|
||||
|
||||
event() :
|
||||
kobject(type::event) {}
|
||||
|
||||
static constexpr kobject::type type = kobject::type::event;
|
||||
};
|
||||
|
||||
} // namespace obj
|
||||
|
||||
82
src/kernel/objects/handle.h
Normal file
82
src/kernel/objects/handle.h
Normal file
@@ -0,0 +1,82 @@
|
||||
#pragma once
|
||||
/// \file handle.h
|
||||
/// Definition of kobject handles
|
||||
|
||||
#include <j6/types.h>
|
||||
#include "objects/kobject.h"
|
||||
|
||||
namespace obj {
|
||||
|
||||
struct handle
|
||||
{
|
||||
inline handle(j6_handle_t in_id, kobject *in_object, j6_cap_t in_caps) :
|
||||
id {in_id}, object {in_object}, caps {in_caps} {
|
||||
if (object) object->handle_retain();
|
||||
}
|
||||
|
||||
inline handle(const handle &other) :
|
||||
id {other.id}, object {other.object}, caps {other.caps} {
|
||||
if (object) object->handle_retain();
|
||||
}
|
||||
|
||||
inline handle(handle &&other) :
|
||||
id {other.id}, object {other.object}, caps {other.caps} {
|
||||
other.id = 0;
|
||||
other.caps = 0;
|
||||
other.object = nullptr;
|
||||
}
|
||||
|
||||
inline handle & operator=(const handle &other) {
|
||||
if (object) object->handle_release();
|
||||
id = other.id; caps = other.caps; object = other.object;
|
||||
if (object) object->handle_retain();
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline handle & operator=(handle &&other) {
|
||||
if (object) object->handle_release();
|
||||
id = other.id; caps = other.caps; object = other.object;
|
||||
other.id = other.caps = 0; other.object = nullptr;
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline ~handle() {
|
||||
if (object) object->handle_release();
|
||||
}
|
||||
|
||||
inline bool has_cap(j6_cap_t test) const {
|
||||
return (caps & test) == test;
|
||||
}
|
||||
|
||||
inline kobject::type type() const {
|
||||
return object->get_type();
|
||||
}
|
||||
|
||||
inline int compare(const handle &o) {
|
||||
return id > o.id ? 1 : id < o.id ? -1 : 0;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline T * as() {
|
||||
if (type() != T::type) return nullptr;
|
||||
return reinterpret_cast<T*>(object);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline const T * as() const {
|
||||
if (type() != T::type) return nullptr;
|
||||
return reinterpret_cast<const T*>(object);
|
||||
}
|
||||
|
||||
template <>
|
||||
inline kobject * as<kobject>() { return object; }
|
||||
|
||||
template <>
|
||||
inline const kobject * as<kobject>() const { return object; }
|
||||
|
||||
j6_handle_t id;
|
||||
j6_cap_t caps;
|
||||
kobject *object;
|
||||
};
|
||||
|
||||
} // namespace obj
|
||||
@@ -6,7 +6,8 @@
|
||||
#include "objects/kobject.h"
|
||||
#include "objects/thread.h"
|
||||
|
||||
// TODO: per-cpu this?
|
||||
namespace obj {
|
||||
|
||||
static j6_koid_t next_koids [static_cast<size_t>(kobject::type::max)] = { 0 };
|
||||
|
||||
kobject::kobject(type t, j6_signal_t signals) :
|
||||
@@ -26,7 +27,8 @@ kobject::koid_generate(type t)
|
||||
{
|
||||
kassert(t < type::max, "Object type out of bounds");
|
||||
uint64_t type_int = static_cast<uint64_t>(t);
|
||||
return (type_int << 48) | next_koids[type_int]++;
|
||||
uint64_t id = __atomic_fetch_add(&next_koids[type_int], 1, __ATOMIC_SEQ_CST);
|
||||
return (type_int << 48) | id;
|
||||
}
|
||||
|
||||
kobject::type
|
||||
@@ -74,3 +76,5 @@ kobject::on_no_handles()
|
||||
{
|
||||
assert_signal(j6_signal_no_handles);
|
||||
}
|
||||
|
||||
} // namespace obj
|
||||
|
||||
@@ -7,6 +7,8 @@
|
||||
#include <j6/types.h>
|
||||
#include <util/vector.h>
|
||||
|
||||
namespace obj {
|
||||
|
||||
class thread;
|
||||
|
||||
/// Base type for all user-interactable kernel objects
|
||||
@@ -98,3 +100,5 @@ private:
|
||||
protected:
|
||||
util::vector<thread*> m_blocked_threads;
|
||||
};
|
||||
|
||||
} // namespace obj
|
||||
|
||||
@@ -7,19 +7,22 @@
|
||||
#include "objects/vm_area.h"
|
||||
#include "scheduler.h"
|
||||
|
||||
|
||||
// This object is initialized _before_ global constructors are called,
|
||||
// so we don't want it to have a global constructor at all, lest it
|
||||
// overwrite the previous initialization.
|
||||
static util::no_construct<process> __g_kernel_process_storage;
|
||||
process &g_kernel_process = __g_kernel_process_storage.value;
|
||||
static util::no_construct<obj::process> __g_kernel_process_storage;
|
||||
obj::process &g_kernel_process = __g_kernel_process_storage.value;
|
||||
|
||||
|
||||
namespace obj {
|
||||
|
||||
process::process() :
|
||||
kobject {kobject::type::process},
|
||||
m_next_handle {1},
|
||||
m_state {state::running}
|
||||
{
|
||||
j6_handle_t self = add_handle(this);
|
||||
j6_handle_t self = add_handle(this, process::self_caps);
|
||||
kassert(self == self_handle(), "Process self-handle is not 1");
|
||||
}
|
||||
|
||||
@@ -34,8 +37,6 @@ process::process(page_table *kpml4) :
|
||||
|
||||
process::~process()
|
||||
{
|
||||
for (auto &it : m_handles)
|
||||
if (it.val) it.val->handle_release();
|
||||
}
|
||||
|
||||
process & process::current() { return *current_cpu().process; }
|
||||
@@ -123,27 +124,27 @@ process::thread_exited(thread *th)
|
||||
}
|
||||
|
||||
j6_handle_t
|
||||
process::add_handle(kobject *obj)
|
||||
process::add_handle(kobject *obj, j6_cap_t caps)
|
||||
{
|
||||
if (!obj)
|
||||
return j6_handle_invalid;
|
||||
|
||||
obj->handle_retain();
|
||||
j6_handle_t handle = m_next_handle++;
|
||||
m_handles.insert(handle, obj);
|
||||
return handle;
|
||||
j6_handle_t id = m_next_handle++;
|
||||
m_handles.insert(id, {id, obj, caps});
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
bool
|
||||
process::remove_handle(j6_handle_t handle)
|
||||
process::remove_handle(j6_handle_t id)
|
||||
{
|
||||
kobject *obj = m_handles.find(handle);
|
||||
if (obj) obj->handle_release();
|
||||
return m_handles.erase(handle);
|
||||
return m_handles.erase(id);
|
||||
}
|
||||
|
||||
kobject *
|
||||
process::lookup_handle(j6_handle_t handle)
|
||||
handle *
|
||||
process::lookup_handle(j6_handle_t id)
|
||||
{
|
||||
return m_handles.find(handle);
|
||||
return m_handles.find(id);
|
||||
}
|
||||
|
||||
} // namespace obj
|
||||
|
||||
@@ -5,14 +5,23 @@
|
||||
#include <util/map.h>
|
||||
#include <util/vector.h>
|
||||
|
||||
#include "objects/handle.h"
|
||||
#include "objects/kobject.h"
|
||||
#include "page_table.h"
|
||||
#include "vm_space.h"
|
||||
|
||||
namespace obj {
|
||||
|
||||
class process :
|
||||
public kobject
|
||||
{
|
||||
public:
|
||||
/// Capabilities on a newly constructed process handle
|
||||
constexpr static j6_cap_t creation_caps = 0;
|
||||
|
||||
/// Capabilities on a process to itself
|
||||
constexpr static j6_cap_t self_caps = 0;
|
||||
|
||||
/// Top of memory area where thread stacks are allocated
|
||||
constexpr static uintptr_t stacks_top = 0x0000800000000000;
|
||||
|
||||
@@ -51,8 +60,9 @@ public:
|
||||
|
||||
/// Start tracking an object with a handle.
|
||||
/// \args obj The object this handle refers to
|
||||
/// \args caps The capabilities on this handle
|
||||
/// \returns The new handle for this object
|
||||
j6_handle_t add_handle(kobject *obj);
|
||||
j6_handle_t add_handle(kobject *obj, j6_cap_t caps);
|
||||
|
||||
/// Stop tracking an object with a handle.
|
||||
/// \args handle The handle that refers to the object
|
||||
@@ -61,8 +71,8 @@ public:
|
||||
|
||||
/// Lookup an object for a handle
|
||||
/// \args handle The handle to the object
|
||||
/// \returns Pointer to the object, or null if not found
|
||||
kobject * lookup_handle(j6_handle_t handle);
|
||||
/// \returns Pointer to the handle struct, or null if not found
|
||||
handle * lookup_handle(j6_handle_t handle);
|
||||
|
||||
/// Inform the process of an exited thread
|
||||
/// \args th The thread which has exited
|
||||
@@ -90,9 +100,12 @@ private:
|
||||
vm_space m_space;
|
||||
|
||||
util::vector<thread*> m_threads;
|
||||
util::map<j6_handle_t, kobject*> m_handles;
|
||||
|
||||
j6_handle_t m_next_handle;
|
||||
util::map<j6_handle_t, handle> m_handles;
|
||||
|
||||
enum class state : uint8_t { running, exited };
|
||||
state m_state;
|
||||
};
|
||||
|
||||
} // namespace obj
|
||||
|
||||
@@ -1,3 +1,7 @@
|
||||
#include "objects/system.h"
|
||||
|
||||
namespace obj {
|
||||
|
||||
system system::s_instance;
|
||||
|
||||
} // namespace obj
|
||||
|
||||
@@ -4,11 +4,16 @@
|
||||
|
||||
#include "objects/kobject.h"
|
||||
|
||||
namespace obj {
|
||||
|
||||
class system :
|
||||
public kobject
|
||||
{
|
||||
public:
|
||||
static constexpr kobject::type type = kobject::type::event;
|
||||
/// Capabilities on system given to init
|
||||
constexpr static j6_cap_t init_caps = 0;
|
||||
|
||||
static constexpr kobject::type type = kobject::type::system;
|
||||
|
||||
inline static system & get() { return s_instance; }
|
||||
|
||||
@@ -16,3 +21,5 @@ private:
|
||||
static system s_instance;
|
||||
system() : kobject(type::system) {}
|
||||
};
|
||||
|
||||
} // namespace obj
|
||||
|
||||
@@ -10,9 +10,12 @@
|
||||
#include "scheduler.h"
|
||||
|
||||
extern "C" void kernel_to_user_trampoline();
|
||||
static constexpr j6_signal_t thread_default_signals = 0;
|
||||
extern obj::vm_area_guarded &g_kernel_stacks;
|
||||
|
||||
extern vm_area_guarded &g_kernel_stacks;
|
||||
|
||||
namespace obj {
|
||||
|
||||
static constexpr j6_signal_t thread_default_signals = 0;
|
||||
|
||||
thread::thread(process &parent, uint8_t pri, uintptr_t rsp0) :
|
||||
kobject(kobject::type::thread, thread_default_signals),
|
||||
@@ -32,7 +35,7 @@ thread::thread(process &parent, uint8_t pri, uintptr_t rsp0) :
|
||||
m_tcb.rsp0 = rsp0;
|
||||
|
||||
m_creator = current_cpu().thread;
|
||||
m_self_handle = parent.add_handle(this);
|
||||
m_self_handle = parent.add_handle(this, thread::parent_caps);
|
||||
}
|
||||
|
||||
thread::~thread()
|
||||
@@ -246,3 +249,5 @@ thread::create_idle_thread(process &kernel, uint8_t pri, uintptr_t rsp0)
|
||||
idle->set_state(state::ready);
|
||||
return idle;
|
||||
}
|
||||
|
||||
} // namespace obj
|
||||
|
||||
@@ -9,7 +9,10 @@
|
||||
#include "objects/kobject.h"
|
||||
|
||||
struct page_table;
|
||||
class process;
|
||||
|
||||
namespace obj {
|
||||
class thread;
|
||||
}
|
||||
|
||||
struct TCB
|
||||
{
|
||||
@@ -22,7 +25,7 @@ struct TCB
|
||||
uintptr_t pml4;
|
||||
// End of area used by asembly
|
||||
|
||||
thread* thread;
|
||||
obj::thread* thread;
|
||||
|
||||
uint8_t priority;
|
||||
// note: 3 bytes padding
|
||||
@@ -38,6 +41,9 @@ struct TCB
|
||||
using tcb_list = util::linked_list<TCB>;
|
||||
using tcb_node = tcb_list::item_type;
|
||||
|
||||
|
||||
namespace obj {
|
||||
|
||||
enum class wait_type : uint8_t
|
||||
{
|
||||
none = 0x00,
|
||||
@@ -47,10 +53,17 @@ enum class wait_type : uint8_t
|
||||
};
|
||||
is_bitfield(wait_type);
|
||||
|
||||
class process;
|
||||
|
||||
class thread :
|
||||
public kobject
|
||||
{
|
||||
public:
|
||||
/// Capabilities on a newly constructed thread handle
|
||||
constexpr static j6_cap_t creation_caps = 0;
|
||||
|
||||
/// Capabilities the parent process gets on new thread handles
|
||||
constexpr static j6_cap_t parent_caps = 0;
|
||||
|
||||
enum class state : uint8_t {
|
||||
ready = 0x01,
|
||||
@@ -203,3 +216,5 @@ private:
|
||||
|
||||
j6_handle_t m_self_handle;
|
||||
};
|
||||
|
||||
} // namespace obj
|
||||
|
||||
@@ -6,6 +6,8 @@
|
||||
#include "page_tree.h"
|
||||
#include "vm_space.h"
|
||||
|
||||
namespace obj {
|
||||
|
||||
using mem::frame_size;
|
||||
|
||||
vm_area::vm_area(size_t size, vm_flags flags) :
|
||||
@@ -184,3 +186,5 @@ vm_area_guarded::get_page(uintptr_t offset, uintptr_t &phys)
|
||||
|
||||
return vm_area_open::get_page(offset, phys);
|
||||
}
|
||||
|
||||
} // namespace obj
|
||||
|
||||
@@ -14,6 +14,9 @@
|
||||
class page_tree;
|
||||
class vm_space;
|
||||
|
||||
|
||||
namespace obj {
|
||||
|
||||
enum class vm_flags : uint32_t
|
||||
{
|
||||
#define VM_FLAG(name, v) name = v,
|
||||
@@ -22,6 +25,7 @@ enum class vm_flags : uint32_t
|
||||
driver_mask = 0x000fffff, ///< flags allowed via syscall for drivers
|
||||
user_mask = 0x0000ffff, ///< flags allowed via syscall for non-drivers
|
||||
};
|
||||
is_bitfield(vm_flags);
|
||||
|
||||
|
||||
/// Virtual memory areas allow control over memory allocation
|
||||
@@ -29,6 +33,9 @@ class vm_area :
|
||||
public kobject
|
||||
{
|
||||
public:
|
||||
/// Capabilities on a newly constructed vma handle
|
||||
constexpr static j6_cap_t creation_caps = 0;
|
||||
|
||||
static constexpr kobject::type type = kobject::type::vma;
|
||||
|
||||
/// Constructor.
|
||||
@@ -170,5 +177,4 @@ private:
|
||||
uintptr_t m_next;
|
||||
};
|
||||
|
||||
|
||||
is_bitfield(vm_flags);
|
||||
} // namespace obj
|
||||
|
||||
Reference in New Issue
Block a user