Compare commits
5 Commits
1c7c90ef39
...
b59d0d8948
| Author | SHA1 | Date | |
|---|---|---|---|
| b59d0d8948 | |||
| 8cc877c251 | |||
| 2073cad97b | |||
| eb8758bc5e | |||
| 1cdde0d32e |
@@ -31,6 +31,20 @@ extern "C" {
|
||||
unsigned long flags) \
|
||||
{ \
|
||||
object_unlock_irqrestore(&p->base, flags); \
|
||||
} \
|
||||
static inline void object_name##_lock_pair_irqsave( \
|
||||
struct object_name *a, \
|
||||
struct object_name *b, \
|
||||
unsigned long *flags) \
|
||||
{ \
|
||||
object_lock_pair_irqsave(&a->base, &b->base, flags); \
|
||||
} \
|
||||
static inline void object_name##_unlock_pair_irqrestore( \
|
||||
struct object_name *a, \
|
||||
struct object_name *b, \
|
||||
unsigned long flags) \
|
||||
{ \
|
||||
object_unlock_pair_irqrestore(&a->base, &b->base, flags); \
|
||||
}
|
||||
|
||||
#define OBJECT_MAGIC 0xBADDCAFE
|
||||
@@ -92,6 +106,15 @@ extern void object_unlock(struct object *obj);
|
||||
extern void object_lock_irqsave(struct object *obj, unsigned long *flags);
|
||||
extern void object_unlock_irqrestore(struct object *obj, unsigned long flags);
|
||||
|
||||
extern void object_lock_pair_irqsave(
|
||||
struct object *a,
|
||||
struct object *b,
|
||||
unsigned long *flags);
|
||||
extern void object_unlock_pair_irqrestore(
|
||||
struct object *a,
|
||||
struct object *b,
|
||||
unsigned long flags);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#include <kernel/channel.h>
|
||||
#include <kernel/msg.h>
|
||||
#include <kernel/port.h>
|
||||
#include <kernel/util.h>
|
||||
#include <kernel/vm-region.h>
|
||||
|
||||
@@ -80,6 +81,7 @@ static void kmsg_reply_error(
|
||||
unsigned long *lock_flags)
|
||||
{
|
||||
msg->msg_status = KMSG_REPLY_SENT;
|
||||
msg->msg_sender_port->p_status = PORT_READY;
|
||||
msg->msg_result = status;
|
||||
thread_awaken(msg->msg_sender_thread);
|
||||
spin_unlock_irqrestore(&msg->msg_lock, *lock_flags);
|
||||
@@ -95,6 +97,7 @@ static struct kmsg *get_next_msg(
|
||||
spin_lock_irqsave(&msg->msg_lock, lock_flags);
|
||||
if (msg->msg_status == KMSG_WAIT_RECEIVE) {
|
||||
msg->msg_status = KMSG_WAIT_REPLY;
|
||||
msg->msg_sender_port->p_status = PORT_REPLY_BLOCKED;
|
||||
return msg;
|
||||
}
|
||||
|
||||
@@ -149,17 +152,24 @@ extern kern_status_t channel_recv_msg(
|
||||
struct task *sender = msg->msg_sender_thread->tr_parent;
|
||||
struct task *receiver = self->tr_parent;
|
||||
|
||||
struct vm_region *src = sender->t_address_space,
|
||||
*dst = receiver->t_address_space;
|
||||
unsigned long f;
|
||||
vm_region_lock_pair_irqsave(src, dst, &f);
|
||||
|
||||
kern_status_t status = vm_region_memmove_v(
|
||||
receiver->t_address_space,
|
||||
dst,
|
||||
0,
|
||||
out_msg->msg_data,
|
||||
out_msg->msg_data_count,
|
||||
sender->t_address_space,
|
||||
src,
|
||||
0,
|
||||
msg->msg_req.msg_data,
|
||||
msg->msg_req.msg_data_count,
|
||||
VM_REGION_COPY_ALL,
|
||||
NULL);
|
||||
vm_region_unlock_pair_irqrestore(src, dst, f);
|
||||
|
||||
if (status != KERN_OK) {
|
||||
kmsg_reply_error(msg, status, &msg_lock_flags);
|
||||
return status;
|
||||
@@ -208,17 +218,24 @@ extern kern_status_t channel_reply_msg(
|
||||
/* the task that is about to send the response */
|
||||
struct task *sender = self->tr_parent;
|
||||
|
||||
struct vm_region *src = sender->t_address_space,
|
||||
*dst = receiver->t_address_space;
|
||||
unsigned long f;
|
||||
vm_region_lock_pair_irqsave(src, dst, &f);
|
||||
|
||||
kern_status_t status = vm_region_memmove_v(
|
||||
receiver->t_address_space,
|
||||
dst,
|
||||
0,
|
||||
msg->msg_resp.msg_data,
|
||||
msg->msg_resp.msg_data_count,
|
||||
sender->t_address_space,
|
||||
src,
|
||||
0,
|
||||
resp->msg_data,
|
||||
resp->msg_data_count,
|
||||
VM_REGION_COPY_ALL,
|
||||
NULL);
|
||||
vm_region_unlock_pair_irqrestore(src, dst, f);
|
||||
|
||||
if (status != KERN_OK) {
|
||||
kmsg_reply_error(msg, status, &msg_lock_flags);
|
||||
return status;
|
||||
@@ -262,17 +279,24 @@ extern kern_status_t channel_read_msg(
|
||||
return KERN_INVALID_ARGUMENT;
|
||||
}
|
||||
|
||||
struct vm_region *src_region
|
||||
= msg->msg_sender_thread->tr_parent->t_address_space;
|
||||
|
||||
unsigned long f;
|
||||
vm_region_lock_pair_irqsave(src_region, dest_region, &f);
|
||||
|
||||
kern_status_t status = vm_region_memmove_v(
|
||||
dest_region,
|
||||
0,
|
||||
dest_iov,
|
||||
dest_iov_count,
|
||||
msg->msg_sender_thread->tr_parent->t_address_space,
|
||||
src_region,
|
||||
offset,
|
||||
msg->msg_req.msg_data,
|
||||
msg->msg_req.msg_data_count,
|
||||
VM_REGION_COPY_ALL,
|
||||
nr_read);
|
||||
vm_region_unlock_pair_irqrestore(src_region, dest_region, f);
|
||||
|
||||
spin_unlock_irqrestore(&msg->msg_lock, msg_lock_flags);
|
||||
|
||||
|
||||
@@ -178,6 +178,38 @@ void object_unlock_irqrestore(struct object *obj, unsigned long flags)
|
||||
spin_unlock_irqrestore(&obj->ob_lock, flags);
|
||||
}
|
||||
|
||||
void object_lock_pair_irqsave(
|
||||
struct object *a,
|
||||
struct object *b,
|
||||
unsigned long *flags)
|
||||
{
|
||||
if (a == b) {
|
||||
object_lock_irqsave(a, flags);
|
||||
} else if (a < b) {
|
||||
object_lock_irqsave(a, flags);
|
||||
object_lock(b);
|
||||
} else {
|
||||
object_lock_irqsave(b, flags);
|
||||
object_lock(a);
|
||||
}
|
||||
}
|
||||
|
||||
void object_unlock_pair_irqrestore(
|
||||
struct object *a,
|
||||
struct object *b,
|
||||
unsigned long flags)
|
||||
{
|
||||
if (a == b) {
|
||||
object_unlock_irqrestore(a, flags);
|
||||
} else if (a < b) {
|
||||
object_unlock(b);
|
||||
object_unlock_irqrestore(a, flags);
|
||||
} else {
|
||||
object_unlock(a);
|
||||
object_unlock_irqrestore(b, flags);
|
||||
}
|
||||
}
|
||||
|
||||
void *object_data(struct object *obj)
|
||||
{
|
||||
return (char *)obj + sizeof *obj;
|
||||
|
||||
@@ -103,5 +103,9 @@ kern_status_t port_send_msg(
|
||||
|
||||
wait_for_reply(msg, lock_flags);
|
||||
|
||||
channel_lock_irqsave(port->p_remote, &flags);
|
||||
btree_delete(&port->p_remote->c_msg, &msg->msg_node);
|
||||
channel_unlock_irqrestore(port->p_remote, flags);
|
||||
|
||||
return msg->msg_result;
|
||||
}
|
||||
|
||||
@@ -417,7 +417,6 @@ kern_status_t sys_msg_read(
|
||||
}
|
||||
|
||||
channel_lock_irqsave(channel, &flags);
|
||||
vm_region_lock(self->t_address_space);
|
||||
status = channel_read_msg(
|
||||
channel,
|
||||
id,
|
||||
@@ -426,7 +425,6 @@ kern_status_t sys_msg_read(
|
||||
iov,
|
||||
iov_count,
|
||||
nr_read);
|
||||
vm_region_unlock(self->t_address_space);
|
||||
channel_unlock_irqrestore(channel, flags);
|
||||
object_unref(channel_obj);
|
||||
|
||||
|
||||
122
vm/vm-region.c
122
vm/vm-region.c
@@ -36,6 +36,15 @@
|
||||
region_find_free_area_linear(region, length)
|
||||
#endif
|
||||
|
||||
#define unlock_mapping_parent(p, root) \
|
||||
do { \
|
||||
struct vm_region *parent \
|
||||
= region_from_entry(p->m_entry.e_parent); \
|
||||
if (parent != root) { \
|
||||
vm_region_unlock(parent); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
/* iterates over a range of mapped virtual memory in a region, and provides
|
||||
* a moving buffer through which the memory can be accessed */
|
||||
struct vm_iterator {
|
||||
@@ -296,12 +305,13 @@ static struct vm_region *region_get_child_region_recursive(
|
||||
* this function should be called with `region` locked. if a mapping is found,
|
||||
* it will be returned with its immediate parent locked. */
|
||||
static struct vm_region_mapping *region_get_mapping_recursive(
|
||||
struct vm_region *region,
|
||||
struct vm_region *root,
|
||||
off_t *offp,
|
||||
size_t len)
|
||||
{
|
||||
off_t offset = *offp;
|
||||
region = region_get_child_region_recursive(region, &offset, len);
|
||||
struct vm_region *region
|
||||
= region_get_child_region_recursive(root, &offset, len);
|
||||
if (!region) {
|
||||
return NULL;
|
||||
}
|
||||
@@ -311,6 +321,14 @@ static struct vm_region_mapping *region_get_mapping_recursive(
|
||||
struct vm_region_entry *entry = region_get_entry(region, offset, len);
|
||||
*offp = offset;
|
||||
|
||||
if (!entry) {
|
||||
if (region != root) {
|
||||
vm_region_unlock(region);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* return the mapping with the parent region still locked */
|
||||
return mapping_from_entry(entry);
|
||||
}
|
||||
@@ -593,7 +611,12 @@ static void vm_iterator_begin(
|
||||
|
||||
off_t offset = base - vm_region_get_base_address(region);
|
||||
it->it_mapping = region_get_mapping_recursive(region, &offset, 1);
|
||||
if (!it->it_mapping || (it->it_mapping->m_prot & prot) != prot) {
|
||||
if (!it->it_mapping) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ((it->it_mapping->m_prot & prot) != prot) {
|
||||
unlock_mapping_parent(it->it_mapping, region);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -612,6 +635,7 @@ static void vm_iterator_begin(
|
||||
}
|
||||
|
||||
if (!pg) {
|
||||
unlock_mapping_parent(it->it_mapping, region);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -643,15 +667,6 @@ static void vm_iterator_begin(
|
||||
|
||||
static kern_status_t vm_iterator_seek(struct vm_iterator *it, size_t nr_bytes)
|
||||
{
|
||||
#define UNLOCK_MAPPING_PARENT(p) \
|
||||
do { \
|
||||
struct vm_region *parent \
|
||||
= region_from_entry(p->m_entry.e_parent); \
|
||||
if (parent != it->it_region) { \
|
||||
vm_region_unlock(parent); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
if (nr_bytes < it->it_max) {
|
||||
it->it_base += nr_bytes;
|
||||
it->it_buf = (char *)it->it_buf + nr_bytes;
|
||||
@@ -661,7 +676,7 @@ static kern_status_t vm_iterator_seek(struct vm_iterator *it, size_t nr_bytes)
|
||||
|
||||
/* the parent region of it->it_mapping is locked here. if it is
|
||||
* different from it->it_region, it must be unlocked */
|
||||
UNLOCK_MAPPING_PARENT(it->it_mapping);
|
||||
unlock_mapping_parent(it->it_mapping, it->it_region);
|
||||
|
||||
it->it_base += nr_bytes;
|
||||
off_t offset = it->it_base - vm_region_get_base_address(it->it_region);
|
||||
@@ -674,13 +689,13 @@ static kern_status_t vm_iterator_seek(struct vm_iterator *it, size_t nr_bytes)
|
||||
return KERN_MEMORY_FAULT;
|
||||
}
|
||||
|
||||
/* past this point, if we encounter an error, must remember to unlock
|
||||
* the parent region of next_mapping */
|
||||
/* past this point, if we encounter an error, must remember to
|
||||
* unlock the parent region of next_mapping */
|
||||
|
||||
if ((next_mapping->m_prot & it->it_prot) != it->it_prot) {
|
||||
it->it_buf = NULL;
|
||||
it->it_max = 0;
|
||||
UNLOCK_MAPPING_PARENT(next_mapping);
|
||||
unlock_mapping_parent(next_mapping, it->it_region);
|
||||
return KERN_MEMORY_FAULT;
|
||||
}
|
||||
|
||||
@@ -699,7 +714,7 @@ static kern_status_t vm_iterator_seek(struct vm_iterator *it, size_t nr_bytes)
|
||||
}
|
||||
|
||||
if (!pg) {
|
||||
UNLOCK_MAPPING_PARENT(next_mapping);
|
||||
unlock_mapping_parent(next_mapping, it->it_region);
|
||||
return KERN_NO_MEMORY;
|
||||
}
|
||||
|
||||
@@ -730,9 +745,20 @@ static kern_status_t vm_iterator_seek(struct vm_iterator *it, size_t nr_bytes)
|
||||
return KERN_OK;
|
||||
}
|
||||
|
||||
/* this function must be called with `root` locked. `root` will be the first
|
||||
* entry visited by the iterator. from there, child entries are visited in
|
||||
* depth-first order. */
|
||||
/* this function must be called when you are finished with a
|
||||
* vm_iterator, to ensure that all held locks are released. */
|
||||
static void vm_iterator_finish(struct vm_iterator *it)
|
||||
{
|
||||
if (it->it_mapping) {
|
||||
unlock_mapping_parent(it->it_mapping, it->it_region);
|
||||
}
|
||||
|
||||
memset(it, 0x0, sizeof *it);
|
||||
}
|
||||
|
||||
/* this function must be called with `root` locked. `root` will be the
|
||||
* first entry visited by the iterator. from there, child entries are
|
||||
* visited in depth-first order. */
|
||||
static void entry_iterator_begin(
|
||||
struct entry_iterator *it,
|
||||
struct vm_region *root)
|
||||
@@ -742,8 +768,8 @@ static void entry_iterator_begin(
|
||||
it->it_entry = &root->vr_entry;
|
||||
}
|
||||
|
||||
/* this function must be called when you are finished with an entry_iterator,
|
||||
* to ensure that all held locks are released. */
|
||||
/* this function must be called when you are finished with an
|
||||
* entry_iterator, to ensure that all held locks are released. */
|
||||
static void entry_iterator_finish(struct entry_iterator *it)
|
||||
{
|
||||
struct vm_region_entry *cur = it->it_entry;
|
||||
@@ -771,10 +797,10 @@ static void entry_iterator_finish(struct entry_iterator *it)
|
||||
/* move to the next entry in the traversal order.
|
||||
* when this function returns:
|
||||
* 1. if the visited entry is a region, it will be locked.
|
||||
* 2. if the visited entry is a mapping, its parent region will be locked.
|
||||
* a region will remain locked until all of its children and n-grand-children
|
||||
* have been visited. once iteration is finished, only `it->it_root` will be
|
||||
* locked.
|
||||
* 2. if the visited entry is a mapping, its parent region will be
|
||||
* locked. a region will remain locked until all of its children and
|
||||
* n-grand-children have been visited. once iteration is finished, only
|
||||
* `it->it_root` will be locked.
|
||||
*/
|
||||
static void entry_iterator_move_next(struct entry_iterator *it)
|
||||
{
|
||||
@@ -791,8 +817,9 @@ static void entry_iterator_move_next(struct entry_iterator *it)
|
||||
if (entry->e_type == VM_REGION_ENTRY_REGION) {
|
||||
struct vm_region *child_region
|
||||
= region_from_entry(entry);
|
||||
/* since `region` is locked, interrupts are already
|
||||
* disabled, so don't use lock_irq() here */
|
||||
/* since `region` is locked, interrupts are
|
||||
* already disabled, so don't use lock_irq()
|
||||
* here */
|
||||
vm_region_lock(child_region);
|
||||
}
|
||||
|
||||
@@ -839,8 +866,9 @@ static void entry_iterator_move_next(struct entry_iterator *it)
|
||||
}
|
||||
}
|
||||
|
||||
/* erase the current entry and move to the next entry in the traversal order.
|
||||
* the current entry MUST be a mapping, otherwise nothing will happen.
|
||||
/* erase the current entry and move to the next entry in the traversal
|
||||
* order. the current entry MUST be a mapping, otherwise nothing will
|
||||
* happen.
|
||||
*/
|
||||
static void entry_iterator_erase(struct entry_iterator *it)
|
||||
{
|
||||
@@ -999,7 +1027,8 @@ struct vm_region *vm_region_cast(struct object *obj)
|
||||
return VM_REGION_CAST(obj);
|
||||
}
|
||||
|
||||
/* this function should be called with `parent` locked (if parent is non-NULL)
|
||||
/* this function should be called with `parent` locked (if parent is
|
||||
* non-NULL)
|
||||
*/
|
||||
kern_status_t vm_region_create(
|
||||
struct vm_region *parent,
|
||||
@@ -1088,9 +1117,9 @@ kern_status_t vm_region_kill(
|
||||
= region_from_entry(region->vr_entry.e_parent);
|
||||
region->vr_entry.e_parent = NULL;
|
||||
|
||||
/* locks must be acquired in parent->child order. since we're
|
||||
* going backwards here, unlock `region` before locking its
|
||||
* parent */
|
||||
/* locks must be acquired in parent->child order. since
|
||||
* we're going backwards here, unlock `region` before
|
||||
* locking its parent */
|
||||
vm_region_unlock_irqrestore(region, *lock_flags);
|
||||
vm_region_lock_irqsave(parent, lock_flags);
|
||||
btree_delete(&parent->vr_entries, ®ion->vr_entry.e_node);
|
||||
@@ -1175,8 +1204,8 @@ kern_status_t vm_region_map_object(
|
||||
root,
|
||||
®ion_offset,
|
||||
length);
|
||||
/* if `region` != `root`, it will need to be unlocked at the end
|
||||
* of the function */
|
||||
/* if `region` != `root`, it will need to be unlocked at
|
||||
* the end of the function */
|
||||
}
|
||||
|
||||
if (region->vr_status != VM_REGION_ONLINE) {
|
||||
@@ -1257,8 +1286,8 @@ kern_status_t vm_region_map_object(
|
||||
return KERN_OK;
|
||||
}
|
||||
|
||||
/* unmap some pages in the middle of a mapping, splitting it into two separate
|
||||
* mappings */
|
||||
/* unmap some pages in the middle of a mapping, splitting it into two
|
||||
* separate mappings */
|
||||
static kern_status_t split_mapping(
|
||||
struct vm_region_mapping *mapping,
|
||||
struct vm_region *root,
|
||||
@@ -1488,7 +1517,8 @@ kern_status_t vm_region_unmap(
|
||||
unmap_area_offset,
|
||||
unmap_area_limit);
|
||||
} else {
|
||||
panic("don't know what to do with this mapping");
|
||||
panic("don't know what to do with this "
|
||||
"mapping");
|
||||
}
|
||||
|
||||
if (delete) {
|
||||
@@ -1557,15 +1587,18 @@ bool vm_region_validate_access(
|
||||
return false;
|
||||
}
|
||||
|
||||
if ((mapping->m_prot & prot) != prot) {
|
||||
return false;
|
||||
}
|
||||
bool ok = (mapping->m_prot & prot) == prot;
|
||||
|
||||
struct vm_region *parent
|
||||
= region_from_entry(mapping->m_entry.e_parent);
|
||||
|
||||
if (parent != region) {
|
||||
vm_region_unlock(parent);
|
||||
}
|
||||
|
||||
if (!ok) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
@@ -1671,6 +1704,8 @@ kern_status_t vm_region_read_kernel(
|
||||
dest += to_move;
|
||||
}
|
||||
|
||||
vm_iterator_finish(&src);
|
||||
|
||||
if (nr_read) {
|
||||
*nr_read = r;
|
||||
}
|
||||
@@ -1727,6 +1762,9 @@ kern_status_t vm_region_memmove(
|
||||
r += to_move;
|
||||
}
|
||||
|
||||
vm_iterator_finish(&src);
|
||||
vm_iterator_finish(&dest);
|
||||
|
||||
if (nr_moved) {
|
||||
*nr_moved = r;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user