Compare commits
6 Commits
110f625f04
...
a0a6a061a4
| Author | SHA1 | Date | |
|---|---|---|---|
| a0a6a061a4 | |||
| 4daffa804c | |||
| 4be642f2e5 | |||
| 7dc0c742fa | |||
| 9faa11cddc | |||
| 89d02c72ee |
66
include/kernel/atomic.h
Normal file
66
include/kernel/atomic.h
Normal file
@@ -0,0 +1,66 @@
|
||||
#ifndef KERNEL_ATOMIC_H_
|
||||
#define KERNEL_ATOMIC_H_
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
typedef int64_t atomic_t;
|
||||
|
||||
/* load and return the value pointed to by `v` */
|
||||
static inline atomic_t atomic_load(atomic_t *v)
|
||||
{
|
||||
return __atomic_load_n(v, __ATOMIC_ACQUIRE);
|
||||
}
|
||||
/* store the value `v` to the pointer `dest` */
|
||||
static inline void atomic_store(atomic_t *dest, atomic_t v)
|
||||
{
|
||||
__atomic_store_n(dest, v, __ATOMIC_ACQUIRE);
|
||||
}
|
||||
/* store the value `v` to the pointer `dest`, and return the value previously
|
||||
* stored at `dest` */
|
||||
static inline atomic_t atomic_exchange(atomic_t *dest, atomic_t v)
|
||||
{
|
||||
return __atomic_exchange_n(dest, v, __ATOMIC_ACQUIRE);
|
||||
}
|
||||
/* compare the contents of `ptr` to the contents of `expected`.
|
||||
* if they match, store the value `desired` to the pointer `ptr` and return
|
||||
* true. if the do NOT match, store the value `*ptr` to the pointer `desired`
|
||||
* and return false.
|
||||
*/
|
||||
static inline bool atomic_compare_exchange(
|
||||
atomic_t *ptr,
|
||||
atomic_t *expected,
|
||||
atomic_t desired)
|
||||
{
|
||||
return __atomic_compare_exchange_n(
|
||||
ptr,
|
||||
expected,
|
||||
desired,
|
||||
false,
|
||||
__ATOMIC_ACQUIRE,
|
||||
__ATOMIC_ACQUIRE);
|
||||
}
|
||||
|
||||
/* perform the operation *ptr += val, and return the result */
|
||||
static inline atomic_t atomic_add_fetch(atomic_t *ptr, atomic_t val)
|
||||
{
|
||||
return __atomic_add_fetch(ptr, val, __ATOMIC_ACQUIRE);
|
||||
}
|
||||
/* perform the operation *ptr -= val, and return the result */
|
||||
static inline atomic_t atomic_sub_fetch(atomic_t *ptr, atomic_t val)
|
||||
{
|
||||
return __atomic_sub_fetch(ptr, val, __ATOMIC_ACQUIRE);
|
||||
}
|
||||
|
||||
/* perform the operation *ptr += val, and return the previous value of *ptr */
|
||||
static inline atomic_t atomic_fetch_add(atomic_t *ptr, atomic_t val)
|
||||
{
|
||||
return __atomic_fetch_add(ptr, val, __ATOMIC_ACQUIRE);
|
||||
}
|
||||
/* perform the operation *ptr -= val, and return the previous value of *ptr */
|
||||
static inline atomic_t atomic_fetch_sub(atomic_t *ptr, atomic_t val)
|
||||
{
|
||||
return __atomic_fetch_sub(ptr, val, __ATOMIC_ACQUIRE);
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -13,6 +13,7 @@ enum kmsg_status {
|
||||
KMSG_WAIT_RECEIVE,
|
||||
KMSG_WAIT_REPLY,
|
||||
KMSG_REPLY_SENT,
|
||||
KMSG_ASYNC,
|
||||
};
|
||||
|
||||
struct msg {
|
||||
@@ -20,10 +21,26 @@ struct msg {
|
||||
enum kmsg_status msg_status;
|
||||
struct btree_node msg_node;
|
||||
msgid_t msg_id;
|
||||
kern_status_t msg_result;
|
||||
struct port *msg_sender_port;
|
||||
struct thread *msg_sender_thread;
|
||||
kern_msg_t msg_req, msg_resp;
|
||||
kern_status_t msg_result;
|
||||
kern_msg_type_t msg_type;
|
||||
|
||||
union {
|
||||
/* msg_type = KERN_MSG_TYPE_DATA */
|
||||
struct {
|
||||
kern_msg_t msg_req, msg_resp;
|
||||
};
|
||||
|
||||
/* msg_type = KERN_MSG_TYPE_EVENT */
|
||||
struct {
|
||||
kern_msg_event_type_t msg_event;
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
extern void msg_init(void);
|
||||
extern struct msg *msg_alloc(void);
|
||||
extern void msg_free(struct msg *msg);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#ifndef KERNEL_OBJECT_H_
|
||||
#define KERNEL_OBJECT_H_
|
||||
|
||||
#include <kernel/atomic.h>
|
||||
#include <kernel/flags.h>
|
||||
#include <kernel/locks.h>
|
||||
#include <kernel/vm.h>
|
||||
@@ -79,10 +80,7 @@ enum object_type_flags {
|
||||
};
|
||||
|
||||
struct object_ops {
|
||||
kern_status_t (*destroy)(struct object *obj, struct queue *q);
|
||||
kern_status_t (*destroy_recurse)(
|
||||
struct queue_entry *entry,
|
||||
struct object **out);
|
||||
kern_status_t (*destroy)(struct object *obj);
|
||||
};
|
||||
|
||||
struct object_type {
|
||||
@@ -101,8 +99,7 @@ struct object {
|
||||
struct object_type *ob_type;
|
||||
spin_lock_t ob_lock;
|
||||
uint32_t ob_signals;
|
||||
unsigned int ob_refcount;
|
||||
unsigned int ob_handles;
|
||||
atomic_t ob_refcount;
|
||||
struct queue_entry ob_list;
|
||||
struct waitqueue ob_wq;
|
||||
} __aligned(sizeof(long));
|
||||
@@ -114,8 +111,6 @@ extern kern_status_t object_type_unregister(struct object_type *p);
|
||||
extern struct object *object_create(struct object_type *type);
|
||||
extern struct object *object_ref(struct object *obj);
|
||||
extern void object_unref(struct object *obj);
|
||||
extern void object_add_handle(struct object *obj);
|
||||
extern void object_remove_handle(struct object *obj);
|
||||
extern void object_lock(struct object *obj);
|
||||
extern void object_unlock(struct object *obj);
|
||||
extern void object_lock_irqsave(struct object *obj, unsigned long *flags);
|
||||
|
||||
@@ -13,13 +13,11 @@ enum page_request_status {
|
||||
PAGE_REQUEST_PENDING = 0,
|
||||
PAGE_REQUEST_IN_PROGRESS,
|
||||
PAGE_REQUEST_COMPLETE,
|
||||
PAGE_REQUEST_ASYNC,
|
||||
};
|
||||
|
||||
struct vm_controller {
|
||||
struct object vc_base;
|
||||
/* tree of struct vm_objects bound to this controller, keyed with the
|
||||
* equeue_key_t specified when the object(s) were created. */
|
||||
struct btree vc_objects;
|
||||
/* tree of pending page requests */
|
||||
struct btree vc_requests;
|
||||
/* the equeue to send async page requests to */
|
||||
@@ -36,7 +34,7 @@ struct page_request {
|
||||
enum page_request_status req_status;
|
||||
kern_status_t req_result;
|
||||
spin_lock_t req_lock;
|
||||
struct vm_object *req_object;
|
||||
equeue_key_t req_object;
|
||||
struct thread *req_sender;
|
||||
struct btree_node req_node;
|
||||
off_t req_offset;
|
||||
|
||||
@@ -14,19 +14,25 @@ enum vm_object_flags {
|
||||
VMO_IN_PLACE = 0x01u,
|
||||
/* this vm-object is/was attached to a vm-controller */
|
||||
VMO_CONTROLLER = 0x02u,
|
||||
/* when a vm-object is attached to a controller, and the ref-count of
|
||||
* the object falls to one (i.e. the last reference is the handle to
|
||||
* the object held by the server that created it), the object will
|
||||
* be detached, allowing the server to close the last handle to the
|
||||
* object and dispose of it. */
|
||||
VMO_AUTO_DETACH = 0x04u,
|
||||
|
||||
/* these flags are for use with vm_object_get_page */
|
||||
/**************************************************/
|
||||
|
||||
/* if the relevant page hasn't been allocated yet, it will be allocated
|
||||
* and returned. if this flag isn't specified, NULL will be returned. */
|
||||
VMO_ALLOCATE_MISSING_PAGE = 0x04u,
|
||||
VMO_ALLOCATE_MISSING_PAGE = 0x08u,
|
||||
/* if the vm-object is attached to a vm-controller, and the relevant
|
||||
* page is uncommitted, send a request to the vm-controller to provide
|
||||
* the missing page. will result in the vm-object being unlocked and
|
||||
* the current thread sleeping until the request is fulfilled. the
|
||||
* vm-object will be re-locked before the function returns. */
|
||||
VMO_REQUEST_MISSING_PAGE = 0x08u,
|
||||
VMO_REQUEST_MISSING_PAGE = 0x10u,
|
||||
};
|
||||
|
||||
struct vm_object {
|
||||
|
||||
@@ -78,6 +78,7 @@ void kernel_init(uintptr_t arg)
|
||||
|
||||
port_type_init();
|
||||
channel_type_init();
|
||||
msg_init();
|
||||
|
||||
struct boot_module bsp_image = {0};
|
||||
bsp_get_location(&bsp_image);
|
||||
|
||||
@@ -98,11 +98,18 @@ static struct msg *get_next_msg(
|
||||
while (cur) {
|
||||
struct msg *msg = BTREE_CONTAINER(struct msg, msg_node, cur);
|
||||
spin_lock_irqsave(&msg->msg_lock, lock_flags);
|
||||
if (msg->msg_status == KMSG_WAIT_RECEIVE) {
|
||||
switch (msg->msg_status) {
|
||||
case KMSG_WAIT_RECEIVE:
|
||||
msg->msg_status = KMSG_WAIT_REPLY;
|
||||
msg->msg_sender_port->p_status = PORT_REPLY_BLOCKED;
|
||||
channel->c_msg_waiting--;
|
||||
return msg;
|
||||
case KMSG_ASYNC:
|
||||
btree_delete(&channel->c_msg, &msg->msg_node);
|
||||
channel->c_msg_waiting--;
|
||||
return msg;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
spin_unlock_irqrestore(&msg->msg_lock, *lock_flags);
|
||||
@@ -146,24 +153,22 @@ extern kern_status_t channel_recv_msg(
|
||||
&channel->c_base,
|
||||
CHANNEL_SIGNAL_MSG_RECEIVED);
|
||||
}
|
||||
#if 0
|
||||
wait_item_init(&waiter, self);
|
||||
for (;;) {
|
||||
thread_wait_begin(&waiter, &channel->c_wq);
|
||||
msg = get_next_msg(channel, &msg_lock_flags);
|
||||
if (msg) {
|
||||
break;
|
||||
}
|
||||
|
||||
object_unlock_irqrestore(&channel->c_base, *irq_flags);
|
||||
schedule(SCHED_NORMAL);
|
||||
object_lock_irqsave(&channel->c_base, irq_flags);
|
||||
}
|
||||
thread_wait_end(&waiter, &channel->c_wq);
|
||||
#endif
|
||||
|
||||
/* msg is now set to the next message to process */
|
||||
|
||||
if (msg->msg_type != KERN_MSG_TYPE_DATA) {
|
||||
/* event messages as asynchronous */
|
||||
out_msg->msg_id = msg->msg_id;
|
||||
out_msg->msg_type = msg->msg_type;
|
||||
out_msg->msg_event = msg->msg_event;
|
||||
out_msg->msg_sender = msg->msg_sender_thread->tr_parent->t_id;
|
||||
out_msg->msg_endpoint = msg->msg_sender_port->p_base.ob_id;
|
||||
spin_unlock_irqrestore(&msg->msg_lock, msg_lock_flags);
|
||||
msg_free(msg);
|
||||
|
||||
return KERN_OK;
|
||||
}
|
||||
|
||||
struct task *sender = msg->msg_sender_thread->tr_parent;
|
||||
struct task *receiver = self->tr_parent;
|
||||
|
||||
@@ -218,6 +223,7 @@ extern kern_status_t channel_recv_msg(
|
||||
}
|
||||
|
||||
out_msg->msg_id = msg->msg_id;
|
||||
out_msg->msg_type = msg->msg_type;
|
||||
out_msg->msg_sender = msg->msg_sender_thread->tr_parent->t_id;
|
||||
out_msg->msg_endpoint = msg->msg_sender_port->p_base.ob_id;
|
||||
|
||||
|
||||
@@ -46,7 +46,7 @@ static void do_handle_table_destroy_leaf(struct handle_table *tab)
|
||||
struct handle *child = &tab->t_handles.t_handle_list[index];
|
||||
bitmap_clear(tab->t_subtables.t_subtable_map, index);
|
||||
if (child->h_object) {
|
||||
object_remove_handle(child->h_object);
|
||||
object_unref(child->h_object);
|
||||
child->h_object = NULL;
|
||||
}
|
||||
}
|
||||
@@ -195,7 +195,7 @@ kern_status_t handle_table_free_handle(
|
||||
= &tab->t_handles.t_handle_list[handle_index];
|
||||
|
||||
if (handle_entry->h_object) {
|
||||
object_remove_handle(handle_entry->h_object);
|
||||
object_unref(handle_entry->h_object);
|
||||
}
|
||||
|
||||
memset(handle_entry, 0x0, sizeof *handle_entry);
|
||||
@@ -307,7 +307,7 @@ kern_status_t handle_table_transfer(
|
||||
|
||||
dst_entry->h_object = src_entry->h_object;
|
||||
dst_entry->h_flags = src_entry->h_flags;
|
||||
object_add_handle(dst_entry->h_object);
|
||||
object_ref(dst_entry->h_object);
|
||||
|
||||
handle_table_free_handle(src, src_handles[i].hnd_value);
|
||||
|
||||
@@ -326,7 +326,7 @@ kern_status_t handle_table_transfer(
|
||||
|
||||
dst_entry->h_object = src_entry->h_object;
|
||||
dst_entry->h_flags = src_entry->h_flags;
|
||||
object_add_handle(dst_entry->h_object);
|
||||
object_ref(dst_entry->h_object);
|
||||
|
||||
dst_handle.hnd_mode = src_handles[i].hnd_mode;
|
||||
dst_handle.hnd_value = dst_value;
|
||||
@@ -371,7 +371,7 @@ kern_status_t handle_table_transfer(
|
||||
struct handle *src_entry
|
||||
= handle_table_get_handle(src, handle.hnd_value);
|
||||
if (src_entry) {
|
||||
object_remove_handle(src_entry->h_object);
|
||||
object_unref(src_entry->h_object);
|
||||
handle_table_free_handle(src, handle.hnd_value);
|
||||
}
|
||||
}
|
||||
|
||||
22
kernel/msg.c
Normal file
22
kernel/msg.c
Normal file
@@ -0,0 +1,22 @@
|
||||
#include <kernel/msg.h>
|
||||
#include <kernel/vm.h>
|
||||
|
||||
static struct vm_cache msg_cache = {
|
||||
.c_name = "msg",
|
||||
.c_obj_size = sizeof(struct msg),
|
||||
};
|
||||
|
||||
void msg_init(void)
|
||||
{
|
||||
vm_cache_init(&msg_cache);
|
||||
}
|
||||
|
||||
struct msg *msg_alloc(void)
|
||||
{
|
||||
return vm_cache_alloc(&msg_cache, VM_NORMAL);
|
||||
}
|
||||
|
||||
void msg_free(struct msg *msg)
|
||||
{
|
||||
vm_cache_free(&msg_cache, msg);
|
||||
}
|
||||
@@ -74,90 +74,28 @@ struct object *object_create(struct object_type *type)
|
||||
obj->ob_lock = SPIN_LOCK_INIT;
|
||||
obj->ob_magic = OBJECT_MAGIC;
|
||||
obj->ob_refcount = 1;
|
||||
obj->ob_handles = 0;
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
struct object *object_ref(struct object *obj)
|
||||
{
|
||||
obj->ob_refcount++;
|
||||
atomic_add_fetch(&obj->ob_refcount, 1);
|
||||
return obj;
|
||||
}
|
||||
|
||||
static void __cleanup(struct object *obj, struct queue *queue)
|
||||
{
|
||||
if (HAS_OP(obj, destroy)) {
|
||||
obj->ob_type->ob_ops.destroy(obj, queue);
|
||||
}
|
||||
|
||||
vm_cache_free(&obj->ob_type->ob_cache, obj);
|
||||
}
|
||||
|
||||
static void object_cleanup(struct object *obj, unsigned long flags)
|
||||
{
|
||||
if (obj->ob_refcount > 0 || obj->ob_handles > 0) {
|
||||
spin_unlock_irqrestore(&obj->ob_lock, flags);
|
||||
return;
|
||||
}
|
||||
|
||||
struct queue queue = QUEUE_INIT;
|
||||
__cleanup(obj, &queue);
|
||||
|
||||
if (!HAS_OP(obj, destroy_recurse)) {
|
||||
return;
|
||||
}
|
||||
|
||||
while (!queue_empty(&queue)) {
|
||||
struct queue_entry *entry = queue_pop_front(&queue);
|
||||
struct object *child = NULL;
|
||||
obj->ob_type->ob_ops.destroy_recurse(entry, &child);
|
||||
if (!child) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (child->ob_refcount > 1) {
|
||||
child->ob_refcount--;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (child->ob_refcount == 0 && child->ob_handles == 0) {
|
||||
__cleanup(child, &queue);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void object_unref(struct object *obj)
|
||||
{
|
||||
unsigned long flags;
|
||||
spin_lock_irqsave(&obj->ob_lock, &flags);
|
||||
|
||||
if (obj->ob_refcount == 0) {
|
||||
spin_unlock_irqrestore(&obj->ob_lock, flags);
|
||||
int ref = atomic_sub_fetch(&obj->ob_refcount, 1);
|
||||
if (ref > 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
obj->ob_refcount--;
|
||||
object_cleanup(obj, flags);
|
||||
}
|
||||
|
||||
void object_add_handle(struct object *obj)
|
||||
{
|
||||
obj->ob_handles++;
|
||||
}
|
||||
|
||||
void object_remove_handle(struct object *obj)
|
||||
{
|
||||
unsigned long flags;
|
||||
spin_lock_irqsave(&obj->ob_lock, &flags);
|
||||
|
||||
if (obj->ob_handles == 0) {
|
||||
spin_unlock_irqrestore(&obj->ob_lock, flags);
|
||||
return;
|
||||
if (HAS_OP(obj, destroy)) {
|
||||
obj->ob_type->ob_ops.destroy(obj);
|
||||
}
|
||||
|
||||
obj->ob_handles--;
|
||||
object_cleanup(obj, flags);
|
||||
vm_cache_free(&obj->ob_type->ob_cache, obj);
|
||||
}
|
||||
|
||||
void object_lock(struct object *obj)
|
||||
|
||||
@@ -1,16 +1,29 @@
|
||||
#include <kernel/channel.h>
|
||||
#include <kernel/port.h>
|
||||
#include <kernel/printk.h>
|
||||
#include <kernel/thread.h>
|
||||
#include <kernel/util.h>
|
||||
|
||||
#define PORT_CAST(p) OBJECT_C_CAST(struct port, p_base, &port_type, p)
|
||||
|
||||
static kern_status_t port_cleanup(struct object *obj);
|
||||
|
||||
static struct object_type port_type = {
|
||||
.ob_name = "port",
|
||||
.ob_size = sizeof(struct port),
|
||||
.ob_header_offset = offsetof(struct port, p_base),
|
||||
.ob_ops = {
|
||||
.destroy = port_cleanup,
|
||||
},
|
||||
};
|
||||
|
||||
static kern_status_t port_cleanup(struct object *obj)
|
||||
{
|
||||
struct port *port = PORT_CAST(obj);
|
||||
port_disconnect(port);
|
||||
return KERN_OK;
|
||||
}
|
||||
|
||||
kern_status_t port_type_init(void)
|
||||
{
|
||||
return object_type_register(&port_type);
|
||||
@@ -58,9 +71,26 @@ struct port *port_create(void)
|
||||
kern_status_t port_connect(struct port *port, struct channel *remote)
|
||||
{
|
||||
if (port->p_status != PORT_OFFLINE) {
|
||||
tracek("port_connect: port in bad state (%d)", port->p_status);
|
||||
return KERN_BAD_STATE;
|
||||
}
|
||||
|
||||
struct msg *msg = msg_alloc();
|
||||
if (!msg) {
|
||||
return KERN_NO_MEMORY;
|
||||
}
|
||||
|
||||
msg->msg_status = KMSG_ASYNC;
|
||||
msg->msg_type = KERN_MSG_TYPE_EVENT;
|
||||
msg->msg_event = KERN_MSG_EVENT_CONNECTION;
|
||||
msg->msg_sender_thread = current_thread();
|
||||
msg->msg_sender_port = port;
|
||||
|
||||
unsigned long flags;
|
||||
channel_lock_irqsave(remote, &flags);
|
||||
channel_enqueue_msg(remote, msg);
|
||||
channel_unlock_irqrestore(remote, flags);
|
||||
|
||||
port->p_remote = remote;
|
||||
port->p_status = PORT_READY;
|
||||
return KERN_OK;
|
||||
@@ -69,9 +99,27 @@ kern_status_t port_connect(struct port *port, struct channel *remote)
|
||||
kern_status_t port_disconnect(struct port *port)
|
||||
{
|
||||
if (port->p_status != PORT_READY) {
|
||||
tracek("port_disconnect: port in bad state (%d)",
|
||||
port->p_status);
|
||||
return KERN_BAD_STATE;
|
||||
}
|
||||
|
||||
struct msg *msg = msg_alloc();
|
||||
if (!msg) {
|
||||
return KERN_NO_MEMORY;
|
||||
}
|
||||
|
||||
msg->msg_status = KMSG_ASYNC;
|
||||
msg->msg_type = KERN_MSG_TYPE_EVENT;
|
||||
msg->msg_event = KERN_MSG_EVENT_DISCONNECTION;
|
||||
msg->msg_sender_thread = current_thread();
|
||||
msg->msg_sender_port = port;
|
||||
|
||||
unsigned long flags;
|
||||
channel_lock_irqsave(port->p_remote, &flags);
|
||||
channel_enqueue_msg(port->p_remote, msg);
|
||||
channel_unlock_irqrestore(port->p_remote, flags);
|
||||
|
||||
port->p_remote = NULL;
|
||||
port->p_status = PORT_OFFLINE;
|
||||
return KERN_OK;
|
||||
@@ -84,12 +132,14 @@ kern_status_t port_send_msg(
|
||||
unsigned long *lock_flags)
|
||||
{
|
||||
if (port->p_status != PORT_READY) {
|
||||
tracek("port_send_msg: port in bad state (%d)", port->p_status);
|
||||
return KERN_BAD_STATE;
|
||||
}
|
||||
|
||||
struct thread *self = current_thread();
|
||||
struct msg msg;
|
||||
memset(&msg, 0x0, sizeof msg);
|
||||
msg.msg_type = KERN_MSG_TYPE_DATA;
|
||||
msg.msg_status = KMSG_WAIT_RECEIVE;
|
||||
msg.msg_sender_thread = self;
|
||||
msg.msg_sender_port = port;
|
||||
|
||||
@@ -4,61 +4,71 @@
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#define VM_PROT_READ 0x01u
|
||||
#define VM_PROT_WRITE 0x02u
|
||||
#define VM_PROT_EXEC 0x04u
|
||||
#define VM_PROT_USER 0x08u
|
||||
#define VM_PROT_SVR 0x10u
|
||||
#define VM_PROT_NOCACHE 0x10u
|
||||
#define VM_PROT_MAP_SPECIFIC 0x40u
|
||||
#define VM_PROT_READ 0x01u
|
||||
#define VM_PROT_WRITE 0x02u
|
||||
#define VM_PROT_EXEC 0x04u
|
||||
#define VM_PROT_USER 0x08u
|
||||
#define VM_PROT_SVR 0x10u
|
||||
#define VM_PROT_NOCACHE 0x10u
|
||||
#define VM_PROT_MAP_SPECIFIC 0x40u
|
||||
|
||||
#define MAP_ADDRESS_ANY ((virt_addr_t) - 1)
|
||||
#define MAP_ADDRESS_INVALID ((virt_addr_t)0)
|
||||
#define KERN_HANDLE_INVALID ((kern_handle_t)0xFFFFFFFF)
|
||||
#define MAP_ADDRESS_ANY ((virt_addr_t) - 1)
|
||||
#define MAP_ADDRESS_INVALID ((virt_addr_t)0)
|
||||
#define KERN_HANDLE_INVALID ((kern_handle_t)0xFFFFFFFF)
|
||||
|
||||
/* config keys for use with kern_config_get/kern_config_set */
|
||||
#define KERN_CFG_INVALID 0x00000u
|
||||
#define KERN_CFG_PAGE_SIZE 0x00001u
|
||||
#define KERN_CFG_INVALID 0x00000u
|
||||
#define KERN_CFG_PAGE_SIZE 0x00001u
|
||||
|
||||
/* config keys for use with task_config_get/task_config_set */
|
||||
#define TASK_CFG_INVALID 0x00000u
|
||||
#define TASK_CFG_INVALID 0x00000u
|
||||
|
||||
/* config keys for use with thread_config_get/thread_config_set */
|
||||
#define THREAD_CFG_INVALID 0x00000u
|
||||
#define THREAD_CFG_FSBASE 0x20001u
|
||||
#define THREAD_CFG_GSBASE 0x20002u
|
||||
#define THREAD_CFG_INVALID 0x00000u
|
||||
#define THREAD_CFG_FSBASE 0x20001u
|
||||
#define THREAD_CFG_GSBASE 0x20002u
|
||||
|
||||
/* maximum number of handles that can be sent in a single message */
|
||||
#define KERN_MSG_MAX_HANDLES 64
|
||||
#define KERN_MSG_MAX_HANDLES 64
|
||||
|
||||
/* the corresponding handle should be ignored */
|
||||
#define KERN_MSG_HANDLE_IGNORE 0
|
||||
#define KERN_MSG_HANDLE_IGNORE 0
|
||||
/* the corresponding handle should be moved to the recipient task. the handle
|
||||
* will be closed. */
|
||||
#define KERN_MSG_HANDLE_MOVE 1
|
||||
#define KERN_MSG_HANDLE_MOVE 1
|
||||
/* the corresponding handle should be copied to the recipient task. the handle
|
||||
* will remain valid for the sending task. */
|
||||
#define KERN_MSG_HANDLE_COPY 2
|
||||
#define KERN_MSG_HANDLE_COPY 2
|
||||
|
||||
/* maximum number of objects that can be waited on in a single call to
|
||||
* kern_object_wait */
|
||||
#define KERN_WAIT_MAX_ITEMS 64
|
||||
#define KERN_WAIT_MAX_ITEMS 64
|
||||
|
||||
/* message types */
|
||||
#define KERN_MSG_TYPE_NONE 0
|
||||
#define KERN_MSG_TYPE_DATA 1
|
||||
#define KERN_MSG_TYPE_EVENT 2
|
||||
|
||||
/* event message types */
|
||||
#define KERN_MSG_EVENT_NONE 0
|
||||
#define KERN_MSG_EVENT_CONNECTION 1
|
||||
#define KERN_MSG_EVENT_DISCONNECTION 2
|
||||
|
||||
/* equeue packet types */
|
||||
#define EQUEUE_PKT_PAGE_REQUEST 0x01u
|
||||
#define EQUEUE_PKT_ASYNC_SIGNAL 0x02u
|
||||
#define EQUEUE_PKT_PAGE_REQUEST 0x01u
|
||||
#define EQUEUE_PKT_ASYNC_SIGNAL 0x02u
|
||||
|
||||
/* page request types */
|
||||
#define PAGE_REQUEST_READ 0x01u
|
||||
#define PAGE_REQUEST_DIRTY 0x02u
|
||||
#define PAGE_REQUEST_DETACH 0x03u
|
||||
#define PAGE_REQUEST_READ 0x01u
|
||||
#define PAGE_REQUEST_DIRTY 0x02u
|
||||
#define PAGE_REQUEST_DETACH 0x03u
|
||||
|
||||
/* futex special values */
|
||||
#define FUTEX_WAKE_ALL ((size_t)-1)
|
||||
#define FUTEX_WAKE_ALL ((size_t)-1)
|
||||
|
||||
/* futex flags */
|
||||
#define FUTEX_PRIVATE 0x01u
|
||||
#define FUTEX_SHARED 0x02u
|
||||
#define FUTEX_PRIVATE 0x01u
|
||||
#define FUTEX_SHARED 0x02u
|
||||
|
||||
#define IOVEC(p, len) \
|
||||
{ \
|
||||
@@ -92,6 +102,8 @@ typedef uint32_t kern_config_key_t;
|
||||
typedef uint32_t vm_prot_t;
|
||||
typedef int64_t ssize_t;
|
||||
typedef uint32_t kern_futex_t;
|
||||
typedef uint32_t kern_msg_type_t;
|
||||
typedef uint32_t kern_msg_event_type_t;
|
||||
|
||||
typedef unsigned short equeue_packet_type_t;
|
||||
|
||||
@@ -122,14 +134,27 @@ typedef struct {
|
||||
tid_t msg_sender;
|
||||
/* the id of the port or channel used to send a particular message. */
|
||||
koid_t msg_endpoint;
|
||||
/* a list of iovecs that point to the buffers that make up the main
|
||||
* message data. */
|
||||
kern_iovec_t *msg_data;
|
||||
size_t msg_data_count;
|
||||
/* a list of handle entries that contain the kernel handles included
|
||||
* in a message. */
|
||||
kern_msg_handle_t *msg_handles;
|
||||
size_t msg_handles_count;
|
||||
/* the message type */
|
||||
kern_msg_type_t msg_type;
|
||||
|
||||
union {
|
||||
/* msg_type = KERN_MSG_TYPE_DATA */
|
||||
struct {
|
||||
/* a list of iovecs that point to the buffers that make
|
||||
* up the main message data. */
|
||||
kern_iovec_t *msg_data;
|
||||
size_t msg_data_count;
|
||||
/* a list of handle entries that contain the kernel
|
||||
* handles included in a message. */
|
||||
kern_msg_handle_t *msg_handles;
|
||||
size_t msg_handles_count;
|
||||
};
|
||||
|
||||
/* msg_type = KERN_MSG_TYPE_EVENT */
|
||||
struct {
|
||||
kern_msg_event_type_t msg_event;
|
||||
};
|
||||
};
|
||||
} kern_msg_t;
|
||||
|
||||
typedef struct {
|
||||
|
||||
@@ -334,11 +334,10 @@ void task_exit(int status)
|
||||
self->t_name,
|
||||
self->t_id,
|
||||
cur_thread->tr_id);
|
||||
tracek("task %s[%u] killed (%u, %u)",
|
||||
tracek("task %s[%u] killed (%u)",
|
||||
self->t_name,
|
||||
self->t_id,
|
||||
self->t_base.ob_refcount,
|
||||
self->t_base.ob_handles);
|
||||
self->t_base.ob_refcount);
|
||||
spin_unlock_irqrestore(handles_lock, flags);
|
||||
|
||||
while (1) {
|
||||
@@ -359,7 +358,7 @@ kern_status_t task_open_handle(
|
||||
return status;
|
||||
}
|
||||
|
||||
object_add_handle(obj);
|
||||
object_ref(obj);
|
||||
handle_data->h_object = obj;
|
||||
handle_data->h_flags = flags;
|
||||
|
||||
|
||||
@@ -60,9 +60,9 @@ kern_status_t sys_port_create(kern_handle_t *out)
|
||||
kern_status_t status
|
||||
= task_open_handle(self, &port->p_base, 0, &handle);
|
||||
task_unlock_irqrestore(self, irq_flags);
|
||||
object_unref(&port->p_base);
|
||||
|
||||
if (status != KERN_OK) {
|
||||
object_unref(&port->p_base);
|
||||
return status;
|
||||
}
|
||||
|
||||
@@ -114,6 +114,7 @@ kern_status_t sys_port_connect(
|
||||
status = port_connect(port, remote);
|
||||
port_unlock_irqrestore(port, flags);
|
||||
object_unref(&remote->c_base);
|
||||
object_unref(port_obj);
|
||||
|
||||
return KERN_OK;
|
||||
}
|
||||
|
||||
@@ -38,7 +38,7 @@ kern_status_t sys_task_self(kern_handle_t *out)
|
||||
return status;
|
||||
}
|
||||
|
||||
object_add_handle(&self->t_base);
|
||||
object_ref(&self->t_base);
|
||||
handle_slot->h_object = &self->t_base;
|
||||
|
||||
*out = handle;
|
||||
@@ -129,8 +129,8 @@ kern_status_t sys_task_create(
|
||||
child_handle_slot->h_object = &child->t_base;
|
||||
space_handle_slot->h_object = &child->t_address_space->s_base;
|
||||
|
||||
object_add_handle(&child->t_base);
|
||||
object_add_handle(&child->t_address_space->s_base);
|
||||
object_ref(&child->t_base);
|
||||
object_ref(&child->t_address_space->s_base);
|
||||
|
||||
object_unref(parent_obj);
|
||||
|
||||
@@ -199,7 +199,7 @@ kern_status_t sys_task_create_thread(
|
||||
|
||||
thread_init_user(thread, ip, sp, args, nr_args);
|
||||
target_handle->h_object = &thread->tr_base;
|
||||
object_add_handle(&thread->tr_base);
|
||||
object_ref(&thread->tr_base);
|
||||
|
||||
task_unlock_irqrestore(target, flags);
|
||||
object_unref(target_obj);
|
||||
@@ -254,7 +254,7 @@ kern_status_t sys_task_get_address_space(
|
||||
}
|
||||
|
||||
handle_slot->h_object = &task->t_address_space->s_base;
|
||||
object_add_handle(&task->t_address_space->s_base);
|
||||
object_ref(&task->t_address_space->s_base);
|
||||
task_unlock_irqrestore(self, flags);
|
||||
object_unref(task_obj);
|
||||
|
||||
@@ -286,7 +286,7 @@ kern_status_t sys_thread_self(kern_handle_t *out)
|
||||
return status;
|
||||
}
|
||||
|
||||
object_add_handle(&self_thread->tr_base);
|
||||
object_ref(&self_thread->tr_base);
|
||||
handle_slot->h_object = &self_thread->tr_base;
|
||||
|
||||
*out = handle;
|
||||
|
||||
@@ -186,8 +186,6 @@ kern_status_t sys_vm_controller_create_object(
|
||||
}
|
||||
|
||||
out_slot->h_object = &out_vmo->vo_base;
|
||||
object_add_handle(&out_vmo->vo_base);
|
||||
object_unref(&out_vmo->vo_base);
|
||||
|
||||
*out = out_handle;
|
||||
return KERN_OK;
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
#include <kernel/sched.h>
|
||||
#include <kernel/task.h>
|
||||
#include <kernel/util.h>
|
||||
#include <kernel/vm-controller.h>
|
||||
#include <kernel/vm-object.h>
|
||||
#include <mango/status.h>
|
||||
|
||||
@@ -47,7 +48,7 @@ enum search_direction {
|
||||
SEARCH_RIGHT,
|
||||
};
|
||||
|
||||
static kern_status_t address_space_cleanup(struct object *obj, struct queue *q);
|
||||
static kern_status_t address_space_cleanup(struct object *obj);
|
||||
|
||||
static struct object_type address_space_type = {
|
||||
.ob_name = "address-space",
|
||||
@@ -141,16 +142,16 @@ static struct vm_area *get_entry(
|
||||
|
||||
if (address < child->vma_base) {
|
||||
next = btree_left(cur);
|
||||
if (LEFT_DIFF(address, child)
|
||||
< LEFT_DIFF(address, closest_left)) {
|
||||
closest_left = child;
|
||||
}
|
||||
} else if (address > child->vma_limit) {
|
||||
next = btree_right(cur);
|
||||
if (RIGHT_DIFF(address, child)
|
||||
< RIGHT_DIFF(address, closest_right)) {
|
||||
closest_right = child;
|
||||
}
|
||||
} else if (address > child->vma_limit) {
|
||||
next = btree_right(cur);
|
||||
if (LEFT_DIFF(address, child)
|
||||
< LEFT_DIFF(address, closest_left)) {
|
||||
closest_left = child;
|
||||
}
|
||||
} else {
|
||||
result = child;
|
||||
break;
|
||||
@@ -664,9 +665,10 @@ static void area_unmap(struct vm_area *area)
|
||||
}
|
||||
}
|
||||
|
||||
static kern_status_t address_space_cleanup(struct object *obj, struct queue *q)
|
||||
static kern_status_t address_space_cleanup(struct object *obj)
|
||||
{
|
||||
struct address_space *space = ADDRESS_SPACE_CAST(obj);
|
||||
tracek("begin address space cleanup %p", space);
|
||||
struct btree_node *cur = btree_first(&space->s_mappings);
|
||||
while (cur) {
|
||||
struct btree_node *next = btree_next(cur);
|
||||
@@ -680,6 +682,20 @@ static kern_status_t address_space_cleanup(struct object *obj, struct queue *q)
|
||||
cur = next;
|
||||
}
|
||||
|
||||
cur = btree_first(&space->s_reserved);
|
||||
while (cur) {
|
||||
struct btree_node *next = btree_next(cur);
|
||||
struct vm_area *area
|
||||
= BTREE_CONTAINER(struct vm_area, vma_node, cur);
|
||||
btree_delete(&space->s_reserved, cur);
|
||||
|
||||
delete_area(area, space);
|
||||
vm_cache_free(&vm_area_cache, area);
|
||||
|
||||
cur = next;
|
||||
}
|
||||
|
||||
tracek("end address space cleanup %p", space);
|
||||
return KERN_OK;
|
||||
}
|
||||
|
||||
@@ -706,6 +722,9 @@ kern_status_t address_space_map(
|
||||
}
|
||||
|
||||
tracek("address_space_map(%zx, %zx)", map_address, length);
|
||||
if (map_address == 0xc6a55000) {
|
||||
printk("break");
|
||||
}
|
||||
|
||||
if (!root || !object) {
|
||||
tracek("null pointer");
|
||||
@@ -823,7 +842,7 @@ static kern_status_t split_area(
|
||||
|
||||
put_entry(&root->s_mappings, right);
|
||||
for (size_t i = unmap_base; i < unmap_limit; i += VM_PAGE_SIZE) {
|
||||
tracek("unmapping %zx", i);
|
||||
tracek("pmap_remove %zx", i);
|
||||
pmap_remove(root->s_pmap, i);
|
||||
}
|
||||
|
||||
@@ -860,7 +879,7 @@ static kern_status_t left_reduce_area(
|
||||
return KERN_OK;
|
||||
}
|
||||
|
||||
tracek(" unmapping %zx-%zx (%zx bytes)", base, base + length, length);
|
||||
tracek(" pmap_remove %zx-%zx (%zx bytes)", base, base + length, length);
|
||||
for (size_t i = base; i < limit; i += VM_PAGE_SIZE) {
|
||||
pmap_remove(root->s_pmap, i);
|
||||
}
|
||||
@@ -897,7 +916,7 @@ static kern_status_t right_reduce_area(
|
||||
return KERN_OK;
|
||||
}
|
||||
|
||||
tracek(" unmapping %zx-%zx (%zx bytes)", base, base + length, length);
|
||||
tracek(" pmap_remove %zx-%zx (%zx bytes)", base, base + length, length);
|
||||
for (size_t i = base; i < limit; i += VM_PAGE_SIZE) {
|
||||
pmap_remove(root->s_pmap, i);
|
||||
}
|
||||
@@ -915,9 +934,10 @@ static kern_status_t delete_area(
|
||||
return KERN_OK;
|
||||
}
|
||||
|
||||
tracek("delete mapping [%zx-%zx]",
|
||||
tracek("delete mapping [%zx-%zx] (%zx bytes)",
|
||||
mapping->vma_base,
|
||||
mapping->vma_limit);
|
||||
mapping->vma_limit,
|
||||
mapping->vma_limit - mapping->vma_base);
|
||||
|
||||
for (size_t i = mapping->vma_base; i < mapping->vma_limit;
|
||||
i += VM_PAGE_SIZE) {
|
||||
@@ -931,7 +951,23 @@ static kern_status_t delete_area(
|
||||
&mapping->vma_object->vo_mappings,
|
||||
&mapping->vma_object_entry);
|
||||
mapping->vma_object = NULL;
|
||||
/* if the object is attached to a controller and the ref-count is 2,
|
||||
* then the only other remaining reference to this object is held by
|
||||
* the controller. */
|
||||
struct vm_controller *ctrl = object->vo_ctrl;
|
||||
bool detach = ctrl != NULL && object->vo_base.ob_refcount == 2
|
||||
&& (object->vo_flags & VMO_AUTO_DETACH);
|
||||
vm_object_unlock_irqrestore(object, flags);
|
||||
|
||||
if (detach) {
|
||||
/* TODO find a better way to achieve this, and/or give the
|
||||
* server that created the object more control over when it
|
||||
* should be detached */
|
||||
vm_controller_lock_irqsave(ctrl, &flags);
|
||||
vm_controller_detach_object(ctrl, object);
|
||||
vm_controller_unlock_irqrestore(ctrl, flags);
|
||||
}
|
||||
|
||||
object_unref(&object->vo_base);
|
||||
|
||||
/* don't actually delete the mapping yet. that will be done by
|
||||
@@ -980,9 +1016,11 @@ kern_status_t address_space_unmap(
|
||||
= (area_base <= unmap_base
|
||||
&& area_limit >= unmap_limit);
|
||||
bool left_reduce
|
||||
= (unmap_base <= area_base && unmap_limit < area_limit);
|
||||
= (unmap_base <= area_base && unmap_limit > area_base
|
||||
&& unmap_limit < area_limit);
|
||||
bool right_reduce
|
||||
= (unmap_base > area_base && unmap_limit >= area_limit);
|
||||
= (unmap_base > area_base && unmap_base < area_limit
|
||||
&& unmap_limit >= area_limit);
|
||||
|
||||
if (split) {
|
||||
status = split_area(
|
||||
@@ -1120,9 +1158,10 @@ kern_status_t address_space_release(
|
||||
&& area_limit >= release_limit);
|
||||
bool left_reduce
|
||||
= (release_base <= area_base
|
||||
&& release_limit > area_base
|
||||
&& release_limit < area_limit);
|
||||
bool right_reduce
|
||||
= (release_base > area_base
|
||||
= (release_base > area_base && release_base < area_limit
|
||||
&& release_limit >= area_limit);
|
||||
|
||||
if (split) {
|
||||
|
||||
@@ -23,8 +23,14 @@ static struct object_type vm_controller_type = {
|
||||
.ob_header_offset = offsetof(struct vm_controller, vc_base),
|
||||
};
|
||||
|
||||
static struct vm_cache page_request_cache = {
|
||||
.c_name = "page-request",
|
||||
.c_obj_size = sizeof(struct page_request),
|
||||
};
|
||||
|
||||
kern_status_t vm_controller_type_init(void)
|
||||
{
|
||||
vm_cache_init(&page_request_cache);
|
||||
return object_type_register(&vm_controller_type);
|
||||
}
|
||||
|
||||
@@ -52,10 +58,17 @@ static struct page_request *get_next_request(struct vm_controller *ctrl)
|
||||
struct page_request *req
|
||||
= BTREE_CONTAINER(struct page_request, req_node, cur);
|
||||
spin_lock(&req->req_lock);
|
||||
if (req->req_status == PAGE_REQUEST_PENDING) {
|
||||
switch (req->req_status) {
|
||||
case PAGE_REQUEST_PENDING:
|
||||
req->req_status = PAGE_REQUEST_IN_PROGRESS;
|
||||
ctrl->vc_requests_waiting--;
|
||||
return req;
|
||||
case PAGE_REQUEST_ASYNC:
|
||||
btree_delete(&ctrl->vc_requests, &req->req_node);
|
||||
ctrl->vc_requests_waiting--;
|
||||
return req;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
spin_unlock(&req->req_lock);
|
||||
@@ -65,98 +78,6 @@ static struct page_request *get_next_request(struct vm_controller *ctrl)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
kern_status_t vm_controller_recv(
|
||||
struct vm_controller *ctrl,
|
||||
equeue_packet_page_request_t *out)
|
||||
{
|
||||
struct page_request *req = NULL;
|
||||
|
||||
req = get_next_request(ctrl);
|
||||
if (!req) {
|
||||
return KERN_NO_ENTRY;
|
||||
}
|
||||
|
||||
if (ctrl->vc_requests_waiting == 0) {
|
||||
object_clear_signal(
|
||||
&ctrl->vc_base,
|
||||
VM_CONTROLLER_SIGNAL_REQUEST_RECEIVED);
|
||||
}
|
||||
|
||||
out->req_vmo = req->req_object->vo_key;
|
||||
out->req_type = req->req_type;
|
||||
out->req_offset = req->req_offset;
|
||||
out->req_length = req->req_length;
|
||||
|
||||
spin_unlock(&req->req_lock);
|
||||
return KERN_OK;
|
||||
}
|
||||
|
||||
kern_status_t vm_controller_recv_async(
|
||||
struct vm_controller *ctrl,
|
||||
struct equeue *eq,
|
||||
equeue_key_t key)
|
||||
{
|
||||
if (ctrl->vc_eq) {
|
||||
object_unref(&ctrl->vc_eq->eq_base);
|
||||
}
|
||||
|
||||
object_ref(&eq->eq_base);
|
||||
ctrl->vc_eq = eq;
|
||||
ctrl->vc_eq_key = key;
|
||||
|
||||
return KERN_OK;
|
||||
}
|
||||
|
||||
kern_status_t vm_controller_create_object(
|
||||
struct vm_controller *ctrl,
|
||||
const char *name,
|
||||
size_t name_len,
|
||||
equeue_key_t key,
|
||||
size_t data_len,
|
||||
vm_prot_t prot,
|
||||
struct vm_object **out)
|
||||
{
|
||||
struct vm_object *vmo = get_object(&ctrl->vc_objects, key);
|
||||
if (vmo) {
|
||||
return KERN_NAME_EXISTS;
|
||||
}
|
||||
|
||||
vmo = vm_object_create(name, name_len, data_len, prot);
|
||||
if (!vmo) {
|
||||
return KERN_NO_MEMORY;
|
||||
}
|
||||
|
||||
object_ref(&ctrl->vc_base);
|
||||
object_ref(&vmo->vo_base);
|
||||
|
||||
vmo->vo_flags |= VMO_CONTROLLER;
|
||||
vmo->vo_ctrl = ctrl;
|
||||
vmo->vo_key = key;
|
||||
|
||||
put_object(&ctrl->vc_objects, vmo);
|
||||
|
||||
*out = vmo;
|
||||
return KERN_OK;
|
||||
}
|
||||
|
||||
kern_status_t vm_controller_detach_object(
|
||||
struct vm_controller *ctrl,
|
||||
struct vm_object *vmo)
|
||||
{
|
||||
if (vmo->vo_ctrl != ctrl) {
|
||||
return KERN_INVALID_ARGUMENT;
|
||||
}
|
||||
|
||||
vmo->vo_ctrl = NULL;
|
||||
vmo->vo_key = 0;
|
||||
btree_delete(&ctrl->vc_objects, &vmo->vo_ctrl_node);
|
||||
|
||||
object_unref(&ctrl->vc_base);
|
||||
object_unref(&vmo->vo_base);
|
||||
|
||||
return KERN_OK;
|
||||
}
|
||||
|
||||
static kern_status_t try_enqueue(struct btree *tree, struct page_request *req)
|
||||
{
|
||||
if (!tree->b_root) {
|
||||
@@ -196,6 +117,119 @@ static kern_status_t try_enqueue(struct btree *tree, struct page_request *req)
|
||||
return true;
|
||||
}
|
||||
|
||||
static kern_status_t send_request_async(
|
||||
struct vm_controller *ctrl,
|
||||
struct page_request *req)
|
||||
{
|
||||
fill_random(&req->req_id, sizeof req->req_id);
|
||||
while (!try_enqueue(&ctrl->vc_requests, req)) {
|
||||
req->req_id++;
|
||||
}
|
||||
|
||||
ctrl->vc_requests_waiting++;
|
||||
object_assert_signal(
|
||||
&ctrl->vc_base,
|
||||
VM_CONTROLLER_SIGNAL_REQUEST_RECEIVED);
|
||||
|
||||
return KERN_OK;
|
||||
}
|
||||
|
||||
kern_status_t vm_controller_recv(
|
||||
struct vm_controller *ctrl,
|
||||
equeue_packet_page_request_t *out)
|
||||
{
|
||||
struct page_request *req = NULL;
|
||||
|
||||
req = get_next_request(ctrl);
|
||||
if (!req) {
|
||||
return KERN_NO_ENTRY;
|
||||
}
|
||||
|
||||
if (ctrl->vc_requests_waiting == 0) {
|
||||
object_clear_signal(
|
||||
&ctrl->vc_base,
|
||||
VM_CONTROLLER_SIGNAL_REQUEST_RECEIVED);
|
||||
}
|
||||
|
||||
out->req_vmo = req->req_object;
|
||||
out->req_type = req->req_type;
|
||||
out->req_offset = req->req_offset;
|
||||
out->req_length = req->req_length;
|
||||
|
||||
spin_unlock(&req->req_lock);
|
||||
|
||||
if (req->req_status == PAGE_REQUEST_ASYNC) {
|
||||
vm_cache_free(&page_request_cache, req);
|
||||
}
|
||||
|
||||
return KERN_OK;
|
||||
}
|
||||
|
||||
kern_status_t vm_controller_recv_async(
|
||||
struct vm_controller *ctrl,
|
||||
struct equeue *eq,
|
||||
equeue_key_t key)
|
||||
{
|
||||
if (ctrl->vc_eq) {
|
||||
object_unref(&ctrl->vc_eq->eq_base);
|
||||
}
|
||||
|
||||
object_ref(&eq->eq_base);
|
||||
ctrl->vc_eq = eq;
|
||||
ctrl->vc_eq_key = key;
|
||||
|
||||
return KERN_OK;
|
||||
}
|
||||
|
||||
kern_status_t vm_controller_create_object(
|
||||
struct vm_controller *ctrl,
|
||||
const char *name,
|
||||
size_t name_len,
|
||||
equeue_key_t key,
|
||||
size_t data_len,
|
||||
vm_prot_t prot,
|
||||
struct vm_object **out)
|
||||
{
|
||||
struct vm_object *vmo
|
||||
= vm_object_create(name, name_len, data_len, prot);
|
||||
if (!vmo) {
|
||||
return KERN_NO_MEMORY;
|
||||
}
|
||||
|
||||
object_ref(&ctrl->vc_base);
|
||||
|
||||
/* TODO expose the VMO_AUTO_DETACH flag to userspace */
|
||||
vmo->vo_flags |= VMO_CONTROLLER | VMO_AUTO_DETACH;
|
||||
vmo->vo_ctrl = ctrl;
|
||||
vmo->vo_key = key;
|
||||
|
||||
*out = vmo;
|
||||
return KERN_OK;
|
||||
}
|
||||
|
||||
kern_status_t vm_controller_detach_object(
|
||||
struct vm_controller *ctrl,
|
||||
struct vm_object *vmo)
|
||||
{
|
||||
if (vmo->vo_ctrl != ctrl) {
|
||||
return KERN_INVALID_ARGUMENT;
|
||||
}
|
||||
|
||||
struct page_request *req
|
||||
= vm_cache_alloc(&page_request_cache, VM_NORMAL);
|
||||
req->req_type = PAGE_REQUEST_DETACH;
|
||||
req->req_status = PAGE_REQUEST_ASYNC;
|
||||
req->req_object = vmo->vo_key;
|
||||
req->req_sender = current_thread();
|
||||
send_request_async(ctrl, req);
|
||||
|
||||
vmo->vo_ctrl = NULL;
|
||||
vmo->vo_key = 0;
|
||||
object_unref(&ctrl->vc_base);
|
||||
|
||||
return KERN_OK;
|
||||
}
|
||||
|
||||
static void wait_for_reply(
|
||||
struct vm_controller *ctrl,
|
||||
struct page_request *req,
|
||||
@@ -221,7 +255,7 @@ static void wait_for_reply(
|
||||
|
||||
static void fulfill_requests(
|
||||
struct vm_controller *ctrl,
|
||||
struct vm_object *obj,
|
||||
equeue_key_t object,
|
||||
off_t offset,
|
||||
size_t length,
|
||||
kern_status_t result)
|
||||
@@ -242,7 +276,7 @@ static void fulfill_requests(
|
||||
match = true;
|
||||
}
|
||||
|
||||
if (req->req_object != obj) {
|
||||
if (req->req_object != object) {
|
||||
match = false;
|
||||
}
|
||||
|
||||
@@ -280,7 +314,7 @@ kern_status_t vm_controller_supply_pages(
|
||||
src_offset,
|
||||
count,
|
||||
NULL);
|
||||
fulfill_requests(ctrl, dst, dst_offset, count, status);
|
||||
fulfill_requests(ctrl, dst->vo_key, dst_offset, count, status);
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
(p) += VM_PAGE_SIZE; \
|
||||
}
|
||||
|
||||
static kern_status_t vm_object_cleanup(struct object *obj, struct queue *q)
|
||||
static kern_status_t vm_object_cleanup(struct object *obj)
|
||||
{
|
||||
struct vm_object *vmo = vm_object_cast(obj);
|
||||
struct btree_node *cur = btree_first(&vmo->vo_pages);
|
||||
@@ -29,6 +29,13 @@ static kern_status_t vm_object_cleanup(struct object *obj, struct queue *q)
|
||||
cur = next;
|
||||
}
|
||||
|
||||
if (vmo->vo_ctrl) {
|
||||
unsigned long flags;
|
||||
vm_controller_lock_irqsave(vmo->vo_ctrl, &flags);
|
||||
vm_controller_detach_object(vmo->vo_ctrl, vmo);
|
||||
vm_controller_unlock_irqrestore(vmo->vo_ctrl, flags);
|
||||
}
|
||||
|
||||
return KERN_OK;
|
||||
}
|
||||
|
||||
@@ -366,12 +373,13 @@ static kern_status_t request_page(
|
||||
struct vm_controller *ctrl = vo->vo_ctrl;
|
||||
struct page_request req = {0};
|
||||
req.req_status = PAGE_REQUEST_PENDING;
|
||||
req.req_type = PAGE_REQUEST_READ;
|
||||
req.req_offset = offset;
|
||||
req.req_length = vm_page_order_to_bytes(VM_PAGE_4K);
|
||||
req.req_sender = current_thread();
|
||||
|
||||
object_ref(&vo->vo_base);
|
||||
req.req_object = vo;
|
||||
req.req_object = vo->vo_key;
|
||||
|
||||
vm_object_unlock_irqrestore(vo, *irq_flags);
|
||||
vm_controller_lock_irqsave(ctrl, irq_flags);
|
||||
|
||||
Reference in New Issue
Block a user