#include <cpus.h>
#include <norma_vm.h>
#include <mach/boolean.h>
#include <mach/kern_return.h>
#include <mach/message.h>
#include <mach/port.h>
#include <mach/vm_statistics.h>
#include <kern/assert.h>
#include <kern/kalloc.h>
#include <kern/thread.h>
#include <kern/sched_prim.h>
#include <kern/spl.h>
#include <kern/misc_protos.h>
#include <kern/counters.h>
#include <vm/vm_map.h>
#include <vm/vm_object.h>
#include <vm/vm_kern.h>
#include <ipc/port.h>
#include <ipc/ipc_entry.h>
#include <ipc/ipc_kmsg.h>
#include <ipc/ipc_notify.h>
#include <ipc/ipc_object.h>
#include <ipc/ipc_space.h>
#include <ipc/ipc_port.h>
#include <ipc/ipc_right.h>
#include <ipc/ipc_hash.h>
#include <ipc/ipc_table.h>
#include <string.h>
#ifdef ppc
#include <ppc/Firmware.h>
#include <ppc/low_trace.h>
#endif
extern vm_map_t ipc_kernel_copy_map;
extern vm_size_t ipc_kmsg_max_vm_space;
extern vm_size_t msg_ool_size_small;
#define MSG_OOL_SIZE_SMALL msg_ool_size_small
void ipc_kmsg_clean(
ipc_kmsg_t kmsg);
void ipc_kmsg_clean_body(
ipc_kmsg_t kmsg,
mach_msg_type_number_t number);
void ipc_kmsg_clean_partial(
ipc_kmsg_t kmsg,
mach_msg_type_number_t number,
vm_offset_t paddr,
vm_size_t length);
mach_msg_return_t ipc_kmsg_copyout_body(
ipc_kmsg_t kmsg,
ipc_space_t space,
vm_map_t map,
mach_msg_body_t *slist);
mach_msg_return_t ipc_kmsg_copyin_body(
ipc_kmsg_t kmsg,
ipc_space_t space,
vm_map_t map);
void ikm_cache_init(void);
#define IKM_STASH 16
ipc_kmsg_t ipc_kmsg_cache[ NCPUS ][ IKM_STASH ];
unsigned int ipc_kmsg_cache_avail[NCPUS];
void
ipc_kmsg_init()
{
unsigned int cpu, i;
for (cpu = 0; cpu < NCPUS; ++cpu) {
for (i = 0; i < IKM_STASH; ++i) {
ipc_kmsg_t kmsg;
kmsg = (ipc_kmsg_t)
kalloc(ikm_plus_overhead(IKM_SAVED_MSG_SIZE));
if (kmsg == IKM_NULL)
panic("ipc_kmsg_init");
ikm_init(kmsg, IKM_SAVED_MSG_SIZE);
ipc_kmsg_cache[cpu][i] = kmsg;
}
ipc_kmsg_cache_avail[cpu] = IKM_STASH;
}
}
ipc_kmsg_t
ipc_kmsg_alloc(
mach_msg_size_t msg_and_trailer_size)
{
ipc_kmsg_t kmsg;
if ((msg_and_trailer_size <= IKM_SAVED_MSG_SIZE)) {
unsigned int cpu, i;
disable_preemption();
cpu = cpu_number();
if ((i = ipc_kmsg_cache_avail[cpu]) > 0) {
assert(i <= IKM_STASH);
kmsg = ipc_kmsg_cache[cpu][--i];
ipc_kmsg_cache_avail[cpu] = i;
ikm_check_init(kmsg, IKM_SAVED_MSG_SIZE);
enable_preemption();
return (kmsg);
}
enable_preemption();
}
if (msg_and_trailer_size < IKM_SAVED_MSG_SIZE)
msg_and_trailer_size = IKM_SAVED_MSG_SIZE;
kmsg = (ipc_kmsg_t)kalloc(ikm_plus_overhead(msg_and_trailer_size));
if (kmsg != IKM_NULL) {
ikm_init(kmsg, msg_and_trailer_size);
}
return(kmsg);
}
void
ipc_kmsg_free(
ipc_kmsg_t kmsg)
{
mach_msg_size_t size = kmsg->ikm_size;
ipc_port_t port;
port = ikm_prealloc_inuse_port(kmsg);
if (port != IP_NULL) {
ip_lock(port);
ikm_prealloc_clear_inuse(kmsg, port);
if (ip_active(port) && (port->ip_premsg == kmsg)) {
assert(IP_PREALLOC(port));
ip_unlock(port);
return;
}
ip_check_unlock(port);
goto free_it;
}
if (kmsg->ikm_size == IKM_SAVED_MSG_SIZE &&
ipc_kmsg_cache_avail[cpu_number()] < IKM_STASH) {
unsigned int cpu, i;
disable_preemption();
cpu = cpu_number();
i = ipc_kmsg_cache_avail[cpu];
if (i < IKM_STASH) {
assert(i >= 0);
ipc_kmsg_cache[cpu][i] = kmsg;
ipc_kmsg_cache_avail[cpu] = i + 1;
enable_preemption();
return;
}
enable_preemption();
}
free_it:
kfree((vm_offset_t) kmsg, ikm_plus_overhead(size));
}
void
ipc_kmsg_enqueue(
ipc_kmsg_queue_t queue,
ipc_kmsg_t kmsg)
{
ipc_kmsg_enqueue_macro(queue, kmsg);
}
ipc_kmsg_t
ipc_kmsg_dequeue(
ipc_kmsg_queue_t queue)
{
ipc_kmsg_t first;
first = ipc_kmsg_queue_first(queue);
if (first != IKM_NULL)
ipc_kmsg_rmqueue_first_macro(queue, first);
return first;
}
void
ipc_kmsg_rmqueue(
ipc_kmsg_queue_t queue,
ipc_kmsg_t kmsg)
{
ipc_kmsg_t next, prev;
assert(queue->ikmq_base != IKM_NULL);
next = kmsg->ikm_next;
prev = kmsg->ikm_prev;
if (next == kmsg) {
assert(prev == kmsg);
assert(queue->ikmq_base == kmsg);
queue->ikmq_base = IKM_NULL;
} else {
if (queue->ikmq_base == kmsg)
queue->ikmq_base = next;
next->ikm_prev = prev;
prev->ikm_next = next;
}
assert(kmsg->ikm_next = IKM_BOGUS);
assert(kmsg->ikm_prev = IKM_BOGUS);
}
ipc_kmsg_t
ipc_kmsg_queue_next(
ipc_kmsg_queue_t queue,
ipc_kmsg_t kmsg)
{
ipc_kmsg_t next;
assert(queue->ikmq_base != IKM_NULL);
next = kmsg->ikm_next;
if (queue->ikmq_base == next)
next = IKM_NULL;
return next;
}
void
ipc_kmsg_destroy(
ipc_kmsg_t kmsg)
{
ipc_kmsg_queue_t queue;
boolean_t empty;
queue = &(current_thread()->ith_messages);
empty = ipc_kmsg_queue_empty(queue);
ipc_kmsg_enqueue(queue, kmsg);
if (empty) {
while ((kmsg = ipc_kmsg_queue_first(queue)) != IKM_NULL) {
ipc_kmsg_clean(kmsg);
ipc_kmsg_rmqueue(queue, kmsg);
ipc_kmsg_free(kmsg);
}
}
}
ipc_kmsg_destroy_dest(
ipc_kmsg_t kmsg)
{
ipc_port_t port;
port = kmsg->ikm_header.msgh_remote_port;
ipc_port_release(port);
kmsg->ikm_header.msgh_remote_port = MACH_PORT_NULL;
ipc_kmsg_destroy(kmsg);
}
void
ipc_kmsg_clean_body(
ipc_kmsg_t kmsg,
mach_msg_type_number_t number)
{
mach_msg_descriptor_t *saddr, *eaddr;
if ( number == 0 )
return;
saddr = (mach_msg_descriptor_t *)
((mach_msg_base_t *) &kmsg->ikm_header + 1);
eaddr = saddr + number;
for ( ; saddr < eaddr; saddr++ ) {
switch (saddr->type.type) {
case MACH_MSG_PORT_DESCRIPTOR: {
mach_msg_port_descriptor_t *dsc;
dsc = &saddr->port;
if (!IO_VALID((ipc_object_t) dsc->name))
continue;
ipc_object_destroy((ipc_object_t) dsc->name, dsc->disposition);
break;
}
case MACH_MSG_OOL_VOLATILE_DESCRIPTOR:
case MACH_MSG_OOL_DESCRIPTOR : {
mach_msg_ool_descriptor_t *dsc;
dsc = &saddr->out_of_line;
if (dsc->size == 0) {
assert(dsc->address == (void *) 0);
} else {
vm_map_copy_discard((vm_map_copy_t) dsc->address);
}
break;
}
case MACH_MSG_OOL_PORTS_DESCRIPTOR : {
ipc_object_t *objects;
mach_msg_type_number_t j;
mach_msg_ool_ports_descriptor_t *dsc;
dsc = &saddr->ool_ports;
objects = (ipc_object_t *) dsc->address;
if (dsc->count == 0) {
break;
}
assert(objects != (ipc_object_t *) 0);
for (j = 0; j < dsc->count; j++) {
ipc_object_t object = objects[j];
if (!IO_VALID(object))
continue;
ipc_object_destroy(object, dsc->disposition);
}
assert(dsc->count != 0);
kfree((vm_offset_t) dsc->address,
(vm_size_t) dsc->count * sizeof(mach_port_name_t));
break;
}
default : {
printf("cleanup: don't understand this type of descriptor\n");
}
}
}
}
void
ipc_kmsg_clean_partial(
ipc_kmsg_t kmsg,
mach_msg_type_number_t number,
vm_offset_t paddr,
vm_size_t length)
{
ipc_object_t object;
mach_msg_bits_t mbits = kmsg->ikm_header.msgh_bits;
object = (ipc_object_t) kmsg->ikm_header.msgh_remote_port;
assert(IO_VALID(object));
ipc_object_destroy(object, MACH_MSGH_BITS_REMOTE(mbits));
object = (ipc_object_t) kmsg->ikm_header.msgh_local_port;
if (IO_VALID(object))
ipc_object_destroy(object, MACH_MSGH_BITS_LOCAL(mbits));
if (paddr) {
(void) vm_deallocate(ipc_kernel_copy_map, paddr, length);
}
ipc_kmsg_clean_body(kmsg, number);
}
void
ipc_kmsg_clean(
ipc_kmsg_t kmsg)
{
ipc_object_t object;
mach_msg_bits_t mbits;
mbits = kmsg->ikm_header.msgh_bits;
object = (ipc_object_t) kmsg->ikm_header.msgh_remote_port;
if (IO_VALID(object))
ipc_object_destroy(object, MACH_MSGH_BITS_REMOTE(mbits));
object = (ipc_object_t) kmsg->ikm_header.msgh_local_port;
if (IO_VALID(object))
ipc_object_destroy(object, MACH_MSGH_BITS_LOCAL(mbits));
if (mbits & MACH_MSGH_BITS_COMPLEX) {
mach_msg_body_t *body;
body = (mach_msg_body_t *) (&kmsg->ikm_header + 1);
ipc_kmsg_clean_body(kmsg, body->msgh_descriptor_count);
}
}
void
ipc_kmsg_set_prealloc(
ipc_kmsg_t kmsg,
ipc_port_t port)
{
assert(kmsg->ikm_prealloc == IP_NULL);
kmsg->ikm_prealloc = IP_NULL;
IP_SET_PREALLOC(port, kmsg);
}
void
ipc_kmsg_clear_prealloc(
ipc_kmsg_t kmsg,
ipc_port_t port)
{
assert(kmsg->ikm_prealloc == port);
kmsg->ikm_prealloc = IP_NULL;
IP_CLEAR_PREALLOC(port, kmsg);
}
mach_msg_return_t
ipc_kmsg_get(
mach_msg_header_t *msg,
mach_msg_size_t size,
ipc_kmsg_t *kmsgp)
{
mach_msg_size_t msg_and_trailer_size;
ipc_kmsg_t kmsg;
mach_msg_max_trailer_t *trailer;
mach_port_name_t dest_name;
ipc_entry_t dest_entry;
ipc_port_t dest_port;
if ((size < sizeof(mach_msg_header_t)) || (size & 3))
return MACH_SEND_MSG_TOO_SMALL;
msg_and_trailer_size = size + MAX_TRAILER_SIZE;
kmsg = ipc_kmsg_alloc(msg_and_trailer_size);
if (kmsg == IKM_NULL)
return MACH_SEND_NO_BUFFER;
if (copyinmsg((char *) msg, (char *) &kmsg->ikm_header, size)) {
ipc_kmsg_free(kmsg);
return MACH_SEND_INVALID_DATA;
}
kmsg->ikm_header.msgh_size = size;
trailer = (mach_msg_max_trailer_t *) ((vm_offset_t)&kmsg->ikm_header + size);
trailer->msgh_sender = current_act()->task->sec_token;
trailer->msgh_audit = current_act()->task->audit_token;
trailer->msgh_trailer_type = MACH_MSG_TRAILER_FORMAT_0;
trailer->msgh_trailer_size = MACH_MSG_TRAILER_MINIMUM_SIZE;
#ifdef ppc
if(trcWork.traceMask) dbgTrace((unsigned int)kmsg->ikm_header.msgh_id,
(unsigned int)kmsg->ikm_header.msgh_remote_port,
(unsigned int)kmsg->ikm_header.msgh_local_port, 0);
#endif
*kmsgp = kmsg;
return MACH_MSG_SUCCESS;
}
mach_msg_return_t
ipc_kmsg_get_from_kernel(
mach_msg_header_t *msg,
mach_msg_size_t size,
ipc_kmsg_t *kmsgp)
{
ipc_kmsg_t kmsg;
mach_msg_size_t msg_and_trailer_size;
mach_msg_max_trailer_t *trailer;
ipc_port_t dest_port;
assert(size >= sizeof(mach_msg_header_t));
assert((size & 3) == 0);
assert(IP_VALID((ipc_port_t) msg->msgh_remote_port));
dest_port = (ipc_port_t)msg->msgh_remote_port;
msg_and_trailer_size = size + MAX_TRAILER_SIZE;
if (IP_PREALLOC(dest_port)) {
ip_lock(dest_port);
if (!ip_active(dest_port)) {
ip_unlock(dest_port);
return MACH_SEND_NO_BUFFER;
}
assert(IP_PREALLOC(dest_port));
kmsg = dest_port->ip_premsg;
if (msg_and_trailer_size > kmsg->ikm_size) {
ip_unlock(dest_port);
return MACH_SEND_TOO_LARGE;
}
if (ikm_prealloc_inuse(kmsg)) {
ip_unlock(dest_port);
return MACH_SEND_NO_BUFFER;
}
ikm_prealloc_set_inuse(kmsg, dest_port);
ip_unlock(dest_port);
} else {
kmsg = ipc_kmsg_alloc(msg_and_trailer_size);
if (kmsg == IKM_NULL)
return MACH_SEND_NO_BUFFER;
}
(void) memcpy((void *) &kmsg->ikm_header, (const void *) msg, size);
kmsg->ikm_header.msgh_size = size;
trailer = (mach_msg_max_trailer_t *)
((vm_offset_t)&kmsg->ikm_header + size);
trailer->msgh_sender = KERNEL_SECURITY_TOKEN;
trailer->msgh_audit = KERNEL_AUDIT_TOKEN;
trailer->msgh_trailer_type = MACH_MSG_TRAILER_FORMAT_0;
trailer->msgh_trailer_size = MACH_MSG_TRAILER_MINIMUM_SIZE;
*kmsgp = kmsg;
return MACH_MSG_SUCCESS;
}
mach_msg_return_t
ipc_kmsg_send(
ipc_kmsg_t kmsg,
mach_msg_option_t option,
mach_msg_timeout_t timeout)
{
kern_return_t save_wait_result;
ipc_port_t port;
port = (ipc_port_t) kmsg->ikm_header.msgh_remote_port;
assert(IP_VALID(port));
ip_lock(port);
if (port->ip_receiver == ipc_space_kernel) {
assert(ip_active(port));
port->ip_messages.imq_seqno++;
ip_unlock(port);
current_task()->messages_sent++;
kmsg = ipc_kobject_server(kmsg);
if (kmsg == IKM_NULL)
return MACH_MSG_SUCCESS;
port = (ipc_port_t) kmsg->ikm_header.msgh_remote_port;
assert(IP_VALID(port));
ip_lock(port);
}
if (!ip_active(port)) {
ip_release(port);
ip_check_unlock(port);
kmsg->ikm_header.msgh_remote_port = MACH_PORT_NULL;
ipc_kmsg_destroy(kmsg);
return MACH_MSG_SUCCESS;
}
if (kmsg->ikm_header.msgh_bits & MACH_MSGH_BITS_CIRCULAR) {
ip_unlock(port);
ipc_kmsg_destroy(kmsg);
return MACH_MSG_SUCCESS;
}
ip_unlock(port);
return (ipc_mqueue_send(&port->ip_messages, kmsg, option, timeout));
}
mach_msg_return_t
ipc_kmsg_put(
mach_msg_header_t *msg,
ipc_kmsg_t kmsg,
mach_msg_size_t size)
{
mach_msg_return_t mr;
if (copyoutmsg((const char *) &kmsg->ikm_header, (char *) msg, size))
mr = MACH_RCV_INVALID_DATA;
else
mr = MACH_MSG_SUCCESS;
ipc_kmsg_free(kmsg);
return mr;
}
void
ipc_kmsg_put_to_kernel(
mach_msg_header_t *msg,
ipc_kmsg_t kmsg,
mach_msg_size_t size)
{
(void) memcpy((void *) msg, (const void *) &kmsg->ikm_header, size);
ipc_kmsg_free(kmsg);
}
mach_msg_return_t
ipc_kmsg_copyin_header(
mach_msg_header_t *msg,
ipc_space_t space,
mach_port_name_t notify)
{
mach_msg_bits_t mbits = msg->msgh_bits & MACH_MSGH_BITS_USER;
mach_port_name_t dest_name = (mach_port_name_t)msg->msgh_remote_port;
mach_port_name_t reply_name = (mach_port_name_t)msg->msgh_local_port;
kern_return_t kr;
mach_msg_type_name_t dest_type = MACH_MSGH_BITS_REMOTE(mbits);
mach_msg_type_name_t reply_type = MACH_MSGH_BITS_LOCAL(mbits);
ipc_object_t dest_port, reply_port;
ipc_port_t dest_soright, reply_soright;
ipc_port_t notify_port;
if ((mbits != msg->msgh_bits) ||
(!MACH_MSG_TYPE_PORT_ANY_SEND(dest_type)) ||
((reply_type == 0) ?
(reply_name != MACH_PORT_NULL) :
!MACH_MSG_TYPE_PORT_ANY_SEND(reply_type)))
return MACH_SEND_INVALID_HEADER;
reply_soright = IP_NULL;
is_write_lock(space);
if (!space->is_active)
goto invalid_dest;
if (!MACH_PORT_VALID(dest_name))
goto invalid_dest;
if (notify != MACH_PORT_NULL) {
ipc_entry_t entry;
if ((entry = ipc_entry_lookup(space, notify)) == IE_NULL) {
is_write_unlock(space);
return MACH_SEND_INVALID_NOTIFY;
}
if((entry->ie_bits & MACH_PORT_TYPE_RECEIVE) == 0) {
is_write_unlock(space);
return MACH_SEND_INVALID_NOTIFY;
}
notify_port = (ipc_port_t) entry->ie_object;
}
if (dest_name == reply_name) {
ipc_entry_t entry;
mach_port_name_t name = dest_name;
entry = ipc_entry_lookup(space, name);
if (entry == IE_NULL)
goto invalid_dest;
assert(reply_type != 0);
if (!ipc_right_copyin_check(space, name, entry, reply_type))
goto invalid_reply;
if ((dest_type == MACH_MSG_TYPE_MOVE_SEND_ONCE) ||
(reply_type == MACH_MSG_TYPE_MOVE_SEND_ONCE)) {
goto invalid_dest;
} else if ((dest_type == MACH_MSG_TYPE_MAKE_SEND) ||
(dest_type == MACH_MSG_TYPE_MAKE_SEND_ONCE) ||
(reply_type == MACH_MSG_TYPE_MAKE_SEND) ||
(reply_type == MACH_MSG_TYPE_MAKE_SEND_ONCE)) {
kr = ipc_right_copyin(space, name, entry,
dest_type, FALSE,
&dest_port, &dest_soright);
if (kr != KERN_SUCCESS)
goto invalid_dest;
assert(IO_VALID(dest_port));
assert(entry->ie_bits & MACH_PORT_TYPE_RECEIVE);
assert(dest_soright == IP_NULL);
kr = ipc_right_copyin(space, name, entry,
reply_type, TRUE,
&reply_port, &reply_soright);
assert(kr == KERN_SUCCESS);
assert(reply_port == dest_port);
assert(entry->ie_bits & MACH_PORT_TYPE_RECEIVE);
assert(reply_soright == IP_NULL);
} else if ((dest_type == MACH_MSG_TYPE_COPY_SEND) &&
(reply_type == MACH_MSG_TYPE_COPY_SEND)) {
kr = ipc_right_copyin(space, name, entry,
dest_type, FALSE,
&dest_port, &dest_soright);
if (kr != KERN_SUCCESS)
goto invalid_dest;
assert(entry->ie_bits & MACH_PORT_TYPE_SEND);
assert(dest_soright == IP_NULL);
reply_port = (ipc_object_t)
ipc_port_copy_send((ipc_port_t) dest_port);
reply_soright = IP_NULL;
} else if ((dest_type == MACH_MSG_TYPE_MOVE_SEND) &&
(reply_type == MACH_MSG_TYPE_MOVE_SEND)) {
kr = ipc_right_copyin_two(space, name, entry,
&dest_port, &dest_soright);
if (kr != KERN_SUCCESS)
goto invalid_dest;
if (IE_BITS_TYPE(entry->ie_bits) == MACH_PORT_TYPE_NONE)
ipc_entry_dealloc(space, name, entry);
reply_port = dest_port;
reply_soright = IP_NULL;
} else {
ipc_port_t soright;
assert(((dest_type == MACH_MSG_TYPE_COPY_SEND) &&
(reply_type == MACH_MSG_TYPE_MOVE_SEND)) ||
((dest_type == MACH_MSG_TYPE_MOVE_SEND) &&
(reply_type == MACH_MSG_TYPE_COPY_SEND)));
kr = ipc_right_copyin(space, name, entry,
MACH_MSG_TYPE_MOVE_SEND, FALSE,
&dest_port, &soright);
if (kr != KERN_SUCCESS)
goto invalid_dest;
if (IE_BITS_TYPE(entry->ie_bits) == MACH_PORT_TYPE_NONE)
ipc_entry_dealloc(space, name, entry);
reply_port = (ipc_object_t)
ipc_port_copy_send((ipc_port_t) dest_port);
if (dest_type == MACH_MSG_TYPE_MOVE_SEND) {
dest_soright = soright;
reply_soright = IP_NULL;
} else {
dest_soright = IP_NULL;
reply_soright = soright;
}
}
} else if (!MACH_PORT_VALID(reply_name)) {
ipc_entry_t entry;
entry = ipc_entry_lookup(space, dest_name);
if (entry == IE_NULL)
goto invalid_dest;
kr = ipc_right_copyin(space, dest_name, entry,
dest_type, FALSE,
&dest_port, &dest_soright);
if (kr != KERN_SUCCESS)
goto invalid_dest;
if (IE_BITS_TYPE(entry->ie_bits) == MACH_PORT_TYPE_NONE)
ipc_entry_dealloc(space, dest_name, entry);
reply_port = (ipc_object_t) reply_name;
reply_soright = IP_NULL;
} else {
ipc_entry_t dest_entry, reply_entry;
ipc_port_t saved_reply;
dest_entry = ipc_entry_lookup(space, dest_name);
if (dest_entry == IE_NULL)
goto invalid_dest;
reply_entry = ipc_entry_lookup(space, reply_name);
if (reply_entry == IE_NULL)
goto invalid_reply;
assert(dest_entry != reply_entry);
assert(reply_type != 0);
if (!ipc_right_copyin_check(space, reply_name, reply_entry,
reply_type))
goto invalid_reply;
kr = ipc_right_copyin(space, dest_name, dest_entry,
dest_type, FALSE,
&dest_port, &dest_soright);
if (kr != KERN_SUCCESS)
goto invalid_dest;
assert(IO_VALID(dest_port));
kr = ipc_right_copyin(space, reply_name, reply_entry,
reply_type, TRUE,
&reply_port, &reply_soright);
assert(kr == KERN_SUCCESS);
if (IE_BITS_TYPE(reply_entry->ie_bits) == MACH_PORT_TYPE_NONE)
ipc_entry_dealloc(space, reply_name, reply_entry);
if (IE_BITS_TYPE(dest_entry->ie_bits) == MACH_PORT_TYPE_NONE)
ipc_entry_dealloc(space, dest_name, dest_entry);
}
if ((notify != MACH_PORT_NULL) &&
(dest_soright == notify_port)) {
ipc_port_release_sonce(dest_soright);
dest_soright = IP_NULL;
}
is_write_unlock(space);
if (dest_soright != IP_NULL)
ipc_notify_port_deleted(dest_soright, dest_name);
if (reply_soright != IP_NULL)
ipc_notify_port_deleted(reply_soright, reply_name);
dest_type = ipc_object_copyin_type(dest_type);
reply_type = ipc_object_copyin_type(reply_type);
msg->msgh_bits = (MACH_MSGH_BITS_OTHER(mbits) |
MACH_MSGH_BITS(dest_type, reply_type));
msg->msgh_remote_port = (ipc_port_t)dest_port;
msg->msgh_local_port = (ipc_port_t)reply_port;
return MACH_MSG_SUCCESS;
invalid_reply:
is_write_unlock(space);
return MACH_SEND_INVALID_REPLY;
invalid_dest:
is_write_unlock(space);
if (reply_soright != IP_NULL)
ipc_notify_port_deleted(reply_soright, reply_name);
return MACH_SEND_INVALID_DEST;
}
mach_msg_return_t
ipc_kmsg_copyin_body(
ipc_kmsg_t kmsg,
ipc_space_t space,
vm_map_t map)
{
ipc_object_t dest;
mach_msg_body_t *body;
mach_msg_descriptor_t *saddr, *eaddr;
boolean_t complex;
mach_msg_return_t mr;
int i;
kern_return_t kr;
vm_size_t space_needed = 0;
vm_offset_t paddr = 0;
mach_msg_descriptor_t *sstart;
vm_map_copy_t copy = VM_MAP_COPY_NULL;
dest = (ipc_object_t) kmsg->ikm_header.msgh_remote_port;
complex = FALSE;
body = (mach_msg_body_t *) (&kmsg->ikm_header + 1);
saddr = (mach_msg_descriptor_t *) (body + 1);
eaddr = saddr + body->msgh_descriptor_count;
if (eaddr <= saddr ||
eaddr > (mach_msg_descriptor_t *) (&kmsg->ikm_header +
kmsg->ikm_header.msgh_size)) {
ipc_kmsg_clean_partial(kmsg,0,0,0);
return MACH_SEND_MSG_TOO_SMALL;
}
for (sstart = saddr; sstart < eaddr; sstart++) {
if (sstart->type.type == MACH_MSG_OOL_DESCRIPTOR ||
sstart->type.type == MACH_MSG_OOL_VOLATILE_DESCRIPTOR) {
if (sstart->out_of_line.copy != MACH_MSG_PHYSICAL_COPY &&
sstart->out_of_line.copy != MACH_MSG_VIRTUAL_COPY) {
ipc_kmsg_clean_partial(kmsg,0,0,0);
return MACH_SEND_INVALID_TYPE;
}
if ((sstart->out_of_line.size >= MSG_OOL_SIZE_SMALL) &&
(sstart->out_of_line.copy == MACH_MSG_PHYSICAL_COPY) &&
!(sstart->out_of_line.deallocate)) {
space_needed += round_page_32(sstart->out_of_line.size);
if (space_needed > ipc_kmsg_max_vm_space) {
ipc_kmsg_clean_partial(kmsg,0,0,0);
return MACH_MSG_VM_KERNEL;
}
}
}
}
if (space_needed) {
if (vm_allocate(ipc_kernel_copy_map, &paddr, space_needed, TRUE) !=
KERN_SUCCESS) {
ipc_kmsg_clean_partial(kmsg,0,0,0);
return MACH_MSG_VM_KERNEL;
}
}
for (i = 0, sstart = saddr; sstart < eaddr; sstart++) {
switch (sstart->type.type) {
case MACH_MSG_PORT_DESCRIPTOR: {
mach_msg_type_name_t name;
ipc_object_t object;
mach_msg_port_descriptor_t *dsc;
dsc = &sstart->port;
name = dsc->disposition;
dsc->disposition = ipc_object_copyin_type(name);
if (!MACH_PORT_VALID((mach_port_name_t)dsc->name)) {
complex = TRUE;
break;
}
kr = ipc_object_copyin(space, (mach_port_name_t)dsc->name, name, &object);
if (kr != KERN_SUCCESS) {
ipc_kmsg_clean_partial(kmsg, i, paddr, space_needed);
return MACH_SEND_INVALID_RIGHT;
}
if ((dsc->disposition == MACH_MSG_TYPE_PORT_RECEIVE) &&
ipc_port_check_circularity((ipc_port_t) object,
(ipc_port_t) dest)) {
kmsg->ikm_header.msgh_bits |= MACH_MSGH_BITS_CIRCULAR;
}
dsc->name = (ipc_port_t) object;
complex = TRUE;
break;
}
case MACH_MSG_OOL_VOLATILE_DESCRIPTOR:
case MACH_MSG_OOL_DESCRIPTOR: {
vm_size_t length;
boolean_t dealloc;
vm_offset_t addr;
vm_offset_t kaddr;
mach_msg_ool_descriptor_t *dsc;
dsc = &sstart->out_of_line;
dealloc = dsc->deallocate;
addr = (vm_offset_t) dsc->address;
length = dsc->size;
if (length == 0) {
dsc->address = 0;
} else if ((length >= MSG_OOL_SIZE_SMALL) &&
(dsc->copy == MACH_MSG_PHYSICAL_COPY) && !dealloc) {
if (copyin((const char *) addr, (char *) paddr,
length)) {
ipc_kmsg_clean_partial(kmsg, i, paddr,
space_needed);
return MACH_SEND_INVALID_MEMORY;
}
if (!page_aligned(length)) {
(void) memset((void *) (paddr + length), 0,
round_page_32(length) - length);
}
if (vm_map_copyin(ipc_kernel_copy_map, paddr, length,
TRUE, ©) != KERN_SUCCESS) {
ipc_kmsg_clean_partial(kmsg, i, paddr,
space_needed);
return MACH_MSG_VM_KERNEL;
}
dsc->address = (void *) copy;
paddr += round_page_32(length);
space_needed -= round_page_32(length);
} else {
kr = vm_map_copyin(map, addr, length, dealloc, ©);
if (kr != KERN_SUCCESS) {
ipc_kmsg_clean_partial(kmsg,i,paddr,space_needed);
return (kr == KERN_RESOURCE_SHORTAGE) ?
MACH_MSG_VM_KERNEL :
MACH_SEND_INVALID_MEMORY;
}
dsc->address = (void *) copy;
}
complex = TRUE;
break;
}
case MACH_MSG_OOL_PORTS_DESCRIPTOR: {
vm_size_t length;
vm_offset_t data;
vm_offset_t addr;
ipc_object_t *objects;
int j;
mach_msg_type_name_t name;
mach_msg_ool_ports_descriptor_t *dsc;
dsc = &sstart->ool_ports;
addr = (vm_offset_t) dsc->address;
length = dsc->count * sizeof(mach_port_name_t);
if (length == 0) {
complex = TRUE;
dsc->address = (void *) 0;
break;
}
data = kalloc(length);
if (data == 0) {
ipc_kmsg_clean_partial(kmsg, i, paddr, space_needed);
return MACH_SEND_NO_BUFFER;
}
if (copyinmap(map, addr, data, length)) {
kfree(data, length);
ipc_kmsg_clean_partial(kmsg, i, paddr, space_needed);
return MACH_SEND_INVALID_MEMORY;
}
if (dsc->deallocate) {
(void) vm_deallocate(map, addr, length);
}
dsc->address = (void *) data;
name = dsc->disposition;
dsc->disposition = ipc_object_copyin_type(name);
objects = (ipc_object_t *) data;
for ( j = 0; j < dsc->count; j++) {
mach_port_name_t port = (mach_port_name_t) objects[j];
ipc_object_t object;
if (!MACH_PORT_VALID(port))
continue;
kr = ipc_object_copyin(space, port, name, &object);
if (kr != KERN_SUCCESS) {
int k;
for(k = 0; k < j; k++) {
object = objects[k];
if (IPC_OBJECT_VALID(object))
ipc_object_destroy(object, dsc->disposition);
}
kfree(data, length);
ipc_kmsg_clean_partial(kmsg, i, paddr, space_needed);
return MACH_SEND_INVALID_RIGHT;
}
if ((dsc->disposition == MACH_MSG_TYPE_PORT_RECEIVE) &&
ipc_port_check_circularity(
(ipc_port_t) object,
(ipc_port_t) dest))
kmsg->ikm_header.msgh_bits |= MACH_MSGH_BITS_CIRCULAR;
objects[j] = object;
}
complex = TRUE;
break;
}
default: {
ipc_kmsg_clean_partial(kmsg, i, paddr, space_needed);
return MACH_SEND_INVALID_TYPE;
}
}
i++ ;
}
if (!complex)
kmsg->ikm_header.msgh_bits &= ~MACH_MSGH_BITS_COMPLEX;
return MACH_MSG_SUCCESS;
}
mach_msg_return_t
ipc_kmsg_copyin(
ipc_kmsg_t kmsg,
ipc_space_t space,
vm_map_t map,
mach_port_name_t notify)
{
mach_msg_return_t mr;
mr = ipc_kmsg_copyin_header(&kmsg->ikm_header, space, notify);
if (mr != MACH_MSG_SUCCESS)
return mr;
if ((kmsg->ikm_header.msgh_bits & MACH_MSGH_BITS_COMPLEX) == 0)
return MACH_MSG_SUCCESS;
return( ipc_kmsg_copyin_body( kmsg, space, map) );
}
void
ipc_kmsg_copyin_from_kernel(
ipc_kmsg_t kmsg)
{
mach_msg_bits_t bits = kmsg->ikm_header.msgh_bits;
mach_msg_type_name_t rname = MACH_MSGH_BITS_REMOTE(bits);
mach_msg_type_name_t lname = MACH_MSGH_BITS_LOCAL(bits);
ipc_object_t remote = (ipc_object_t) kmsg->ikm_header.msgh_remote_port;
ipc_object_t local = (ipc_object_t) kmsg->ikm_header.msgh_local_port;
ipc_object_copyin_from_kernel(remote, rname);
if (IO_VALID(local))
ipc_object_copyin_from_kernel(local, lname);
if (bits == (MACH_MSGH_BITS_COMPLEX |
MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND, 0))) {
bits = (MACH_MSGH_BITS_COMPLEX |
MACH_MSGH_BITS(MACH_MSG_TYPE_PORT_SEND, 0));
kmsg->ikm_header.msgh_bits = bits;
} else {
bits = (MACH_MSGH_BITS_OTHER(bits) |
MACH_MSGH_BITS(ipc_object_copyin_type(rname),
ipc_object_copyin_type(lname)));
kmsg->ikm_header.msgh_bits = bits;
if ((bits & MACH_MSGH_BITS_COMPLEX) == 0)
return;
}
{
mach_msg_descriptor_t *saddr, *eaddr;
mach_msg_body_t *body;
body = (mach_msg_body_t *) (&kmsg->ikm_header + 1);
saddr = (mach_msg_descriptor_t *) (body + 1);
eaddr = (mach_msg_descriptor_t *) saddr + body->msgh_descriptor_count;
for ( ; saddr < eaddr; saddr++) {
switch (saddr->type.type) {
case MACH_MSG_PORT_DESCRIPTOR: {
mach_msg_type_name_t name;
ipc_object_t object;
mach_msg_port_descriptor_t *dsc;
dsc = &saddr->port;
name = dsc->disposition;
object = (ipc_object_t) dsc->name;
dsc->disposition = ipc_object_copyin_type(name);
if (!IO_VALID(object)) {
break;
}
ipc_object_copyin_from_kernel(object, name);
if (((ipc_port_t)remote)->ip_receiver != ipc_space_kernel) {
if ((dsc->disposition == MACH_MSG_TYPE_PORT_RECEIVE) &&
ipc_port_check_circularity((ipc_port_t) object,
(ipc_port_t) remote)) {
kmsg->ikm_header.msgh_bits |=
MACH_MSGH_BITS_CIRCULAR;
}
}
break;
}
case MACH_MSG_OOL_VOLATILE_DESCRIPTOR:
case MACH_MSG_OOL_DESCRIPTOR: {
break;
}
case MACH_MSG_OOL_PORTS_DESCRIPTOR: {
ipc_object_t *objects;
int j;
mach_msg_type_name_t name;
mach_msg_ool_ports_descriptor_t *dsc;
dsc = &saddr->ool_ports;
name = dsc->disposition;
dsc->disposition = ipc_object_copyin_type(name);
objects = (ipc_object_t *) dsc->address;
for ( j = 0; j < dsc->count; j++) {
ipc_object_t object = objects[j];
if (!IO_VALID(object))
continue;
ipc_object_copyin_from_kernel(object, name);
if ((dsc->disposition == MACH_MSG_TYPE_PORT_RECEIVE) &&
ipc_port_check_circularity(
(ipc_port_t) object,
(ipc_port_t) remote))
kmsg->ikm_header.msgh_bits |= MACH_MSGH_BITS_CIRCULAR;
}
break;
}
default: {
#if MACH_ASSERT
panic("ipc_kmsg_copyin_from_kernel: bad descriptor");
#endif
}
}
}
}
}
mach_msg_return_t
ipc_kmsg_copyout_header(
mach_msg_header_t *msg,
ipc_space_t space,
mach_port_name_t notify)
{
mach_msg_bits_t mbits = msg->msgh_bits;
ipc_port_t dest = (ipc_port_t) msg->msgh_remote_port;
assert(IP_VALID(dest));
{
mach_msg_type_name_t dest_type = MACH_MSGH_BITS_REMOTE(mbits);
mach_msg_type_name_t reply_type = MACH_MSGH_BITS_LOCAL(mbits);
ipc_port_t reply = (ipc_port_t) msg->msgh_local_port;
mach_port_name_t dest_name, reply_name;
if (IP_VALID(reply)) {
ipc_port_t notify_port;
ipc_entry_t entry;
kern_return_t kr;
is_write_lock(space);
for (;;) {
ipc_port_request_index_t request;
if (!space->is_active) {
is_write_unlock(space);
return (MACH_RCV_HEADER_ERROR|
MACH_MSG_IPC_SPACE);
}
if (notify != MACH_PORT_NULL) {
notify_port = ipc_port_lookup_notify(space,
notify);
if (notify_port == IP_NULL) {
is_write_unlock(space);
return MACH_RCV_INVALID_NOTIFY;
}
} else
notify_port = IP_NULL;
if ((reply_type != MACH_MSG_TYPE_PORT_SEND_ONCE) &&
ipc_right_reverse(space, (ipc_object_t) reply,
&reply_name, &entry)) {
assert(entry->ie_bits &
MACH_PORT_TYPE_SEND_RECEIVE);
break;
}
ip_lock(reply);
if (!ip_active(reply)) {
ip_release(reply);
ip_check_unlock(reply);
if (notify_port != IP_NULL)
ipc_port_release_sonce(notify_port);
ip_lock(dest);
is_write_unlock(space);
reply = IP_DEAD;
reply_name = MACH_PORT_DEAD;
goto copyout_dest;
}
reply_name = (mach_port_name_t)reply;
kr = ipc_entry_get(space, &reply_name, &entry);
if (kr != KERN_SUCCESS) {
ip_unlock(reply);
if (notify_port != IP_NULL)
ipc_port_release_sonce(notify_port);
kr = ipc_entry_grow_table(space,
ITS_SIZE_NONE);
if (kr != KERN_SUCCESS) {
if (kr == KERN_RESOURCE_SHORTAGE)
return (MACH_RCV_HEADER_ERROR|
MACH_MSG_IPC_KERNEL);
else
return (MACH_RCV_HEADER_ERROR|
MACH_MSG_IPC_SPACE);
}
continue;
}
assert(IE_BITS_TYPE(entry->ie_bits) ==
MACH_PORT_TYPE_NONE);
assert(entry->ie_object == IO_NULL);
if (notify_port == IP_NULL) {
entry->ie_object = (ipc_object_t) reply;
break;
}
kr = ipc_port_dnrequest(reply, reply_name,
notify_port, &request);
if (kr != KERN_SUCCESS) {
ip_unlock(reply);
ipc_port_release_sonce(notify_port);
ipc_entry_dealloc(space, reply_name, entry);
is_write_unlock(space);
ip_lock(reply);
if (!ip_active(reply)) {
ip_unlock(reply);
is_write_lock(space);
continue;
}
kr = ipc_port_dngrow(reply, ITS_SIZE_NONE);
if (kr != KERN_SUCCESS)
return (MACH_RCV_HEADER_ERROR|
MACH_MSG_IPC_KERNEL);
is_write_lock(space);
continue;
}
notify_port = IP_NULL;
entry->ie_object = (ipc_object_t) reply;
entry->ie_request = request;
break;
}
ip_reference(reply);
kr = ipc_right_copyout(space, reply_name, entry,
reply_type, TRUE, (ipc_object_t) reply);
assert(kr == KERN_SUCCESS);
if (notify_port != IP_NULL)
ipc_port_release_sonce(notify_port);
ip_lock(dest);
is_write_unlock(space);
} else {
is_read_lock(space);
if (!space->is_active) {
is_read_unlock(space);
return MACH_RCV_HEADER_ERROR|MACH_MSG_IPC_SPACE;
}
if (notify != MACH_PORT_NULL) {
ipc_entry_t entry;
if ((entry = ipc_entry_lookup(space, notify)) == IE_NULL) {
is_read_unlock(space);
return MACH_RCV_INVALID_NOTIFY;
}
if ((entry->ie_bits & MACH_PORT_TYPE_RECEIVE) == 0) {
is_read_unlock(space);
return MACH_RCV_INVALID_NOTIFY;
}
}
ip_lock(dest);
is_read_unlock(space);
reply_name = (mach_port_name_t) reply;
}
copyout_dest:
if (ip_active(dest)) {
ipc_object_copyout_dest(space, (ipc_object_t) dest,
dest_type, &dest_name);
} else {
ipc_port_timestamp_t timestamp;
timestamp = dest->ip_timestamp;
ip_release(dest);
ip_check_unlock(dest);
if (IP_VALID(reply)) {
ip_lock(reply);
if (ip_active(reply) ||
IP_TIMESTAMP_ORDER(timestamp,
reply->ip_timestamp))
dest_name = MACH_PORT_DEAD;
else
dest_name = MACH_PORT_NULL;
ip_unlock(reply);
} else
dest_name = MACH_PORT_DEAD;
}
if (IP_VALID(reply))
ipc_port_release(reply);
msg->msgh_bits = (MACH_MSGH_BITS_OTHER(mbits) |
MACH_MSGH_BITS(reply_type, dest_type));
msg->msgh_local_port = (ipc_port_t)dest_name;
msg->msgh_remote_port = (ipc_port_t)reply_name;
}
return MACH_MSG_SUCCESS;
}
mach_msg_return_t
ipc_kmsg_copyout_object(
ipc_space_t space,
ipc_object_t object,
mach_msg_type_name_t msgt_name,
mach_port_name_t *namep)
{
kern_return_t kr;
if (!IO_VALID(object)) {
*namep = (mach_port_name_t) object;
return MACH_MSG_SUCCESS;
}
kr = ipc_object_copyout(space, object, msgt_name, TRUE, namep);
if (kr != KERN_SUCCESS) {
ipc_object_destroy(object, msgt_name);
if (kr == KERN_INVALID_CAPABILITY)
*namep = MACH_PORT_DEAD;
else {
*namep = MACH_PORT_NULL;
if (kr == KERN_RESOURCE_SHORTAGE)
return MACH_MSG_IPC_KERNEL;
else
return MACH_MSG_IPC_SPACE;
}
}
return MACH_MSG_SUCCESS;
}
mach_msg_return_t
ipc_kmsg_copyout_body(
ipc_kmsg_t kmsg,
ipc_space_t space,
vm_map_t map,
mach_msg_body_t *slist)
{
mach_msg_body_t *body;
mach_msg_descriptor_t *saddr, *eaddr;
mach_msg_return_t mr = MACH_MSG_SUCCESS;
kern_return_t kr;
vm_offset_t data;
mach_msg_descriptor_t *sstart, *send;
body = (mach_msg_body_t *) (&kmsg->ikm_header + 1);
saddr = (mach_msg_descriptor_t *) (body + 1);
eaddr = saddr + body->msgh_descriptor_count;
if (slist != MACH_MSG_BODY_NULL) {
sstart = (mach_msg_descriptor_t *) (slist + 1);
send = sstart + slist->msgh_descriptor_count;
}
else {
sstart = MACH_MSG_DESCRIPTOR_NULL;
}
for ( ; saddr < eaddr; saddr++ ) {
switch (saddr->type.type) {
case MACH_MSG_PORT_DESCRIPTOR: {
mach_msg_port_descriptor_t *dsc;
dsc = &saddr->port;
mr |= ipc_kmsg_copyout_object(space,
(ipc_object_t) dsc->name,
dsc->disposition,
(mach_port_name_t *) &dsc->name);
break;
}
case MACH_MSG_OOL_VOLATILE_DESCRIPTOR:
case MACH_MSG_OOL_DESCRIPTOR : {
vm_offset_t rcv_addr;
vm_offset_t snd_addr;
mach_msg_ool_descriptor_t *dsc;
mach_msg_copy_options_t copy_option;
SKIP_PORT_DESCRIPTORS(sstart, send);
dsc = &saddr->out_of_line;
assert(dsc->copy != MACH_MSG_KALLOC_COPY_T);
copy_option = dsc->copy;
if ((snd_addr = (vm_offset_t) dsc->address) != 0) {
if (sstart != MACH_MSG_DESCRIPTOR_NULL &&
sstart->out_of_line.copy == MACH_MSG_OVERWRITE) {
rcv_addr = (vm_offset_t) sstart->out_of_line.address;
dsc->copy = MACH_MSG_OVERWRITE;
} else {
dsc->copy = MACH_MSG_ALLOCATE;
}
if (dsc->copy == MACH_MSG_OVERWRITE) {
kr = vm_map_copy_overwrite(map, rcv_addr,
(vm_map_copy_t) dsc->address, TRUE);
} else {
kr = vm_map_copyout(map, &rcv_addr,
(vm_map_copy_t) dsc->address);
}
if (kr != KERN_SUCCESS) {
if (kr == KERN_RESOURCE_SHORTAGE)
mr |= MACH_MSG_VM_KERNEL;
else
mr |= MACH_MSG_VM_SPACE;
vm_map_copy_discard((vm_map_copy_t) dsc->address);
dsc->address = 0;
INCREMENT_SCATTER(sstart);
break;
}
dsc->address = (void *) rcv_addr;
}
INCREMENT_SCATTER(sstart);
break;
}
case MACH_MSG_OOL_PORTS_DESCRIPTOR : {
vm_offset_t addr;
mach_port_name_t *objects;
mach_msg_type_number_t j;
vm_size_t length;
mach_msg_ool_ports_descriptor_t *dsc;
SKIP_PORT_DESCRIPTORS(sstart, send);
dsc = &saddr->ool_ports;
length = dsc->count * sizeof(mach_port_name_t);
if (length != 0) {
if (sstart != MACH_MSG_DESCRIPTOR_NULL &&
sstart->ool_ports.copy == MACH_MSG_OVERWRITE) {
addr = (vm_offset_t) sstart->out_of_line.address;
dsc->copy = MACH_MSG_OVERWRITE;
}
else {
int anywhere = VM_MAKE_TAG(VM_MEMORY_MACH_MSG)|
VM_FLAGS_ANYWHERE;
dsc->copy = MACH_MSG_ALLOCATE;
if ((kr = vm_allocate(map, &addr, length,
anywhere)) != KERN_SUCCESS) {
ipc_kmsg_clean_body(kmsg,
body->msgh_descriptor_count);
dsc->address = 0;
if (kr == KERN_RESOURCE_SHORTAGE){
mr |= MACH_MSG_VM_KERNEL;
} else {
mr |= MACH_MSG_VM_SPACE;
}
INCREMENT_SCATTER(sstart);
break;
}
}
} else {
INCREMENT_SCATTER(sstart);
break;
}
objects = (mach_port_name_t *) dsc->address ;
for ( j = 0; j < dsc->count ; j++) {
ipc_object_t object =
(ipc_object_t) objects[j];
mr |= ipc_kmsg_copyout_object(space, object,
dsc->disposition, &objects[j]);
}
data = (vm_offset_t) dsc->address;
(void) copyoutmap(map, data, addr, length);
kfree(data, length);
dsc->address = (void *) addr;
INCREMENT_SCATTER(sstart);
break;
}
default : {
panic("untyped IPC copyout body: invalid message descriptor");
}
}
}
return mr;
}
mach_msg_return_t
ipc_kmsg_copyout(
ipc_kmsg_t kmsg,
ipc_space_t space,
vm_map_t map,
mach_port_name_t notify,
mach_msg_body_t *slist)
{
mach_msg_return_t mr;
mr = ipc_kmsg_copyout_header(&kmsg->ikm_header, space, notify);
if (mr != MACH_MSG_SUCCESS)
return mr;
if (kmsg->ikm_header.msgh_bits & MACH_MSGH_BITS_COMPLEX) {
mr = ipc_kmsg_copyout_body(kmsg, space, map, slist);
if (mr != MACH_MSG_SUCCESS)
mr |= MACH_RCV_BODY_ERROR;
}
return mr;
}
mach_msg_return_t
ipc_kmsg_copyout_pseudo(
ipc_kmsg_t kmsg,
ipc_space_t space,
vm_map_t map,
mach_msg_body_t *slist)
{
mach_msg_bits_t mbits = kmsg->ikm_header.msgh_bits;
ipc_object_t dest = (ipc_object_t) kmsg->ikm_header.msgh_remote_port;
ipc_object_t reply = (ipc_object_t) kmsg->ikm_header.msgh_local_port;
mach_msg_type_name_t dest_type = MACH_MSGH_BITS_REMOTE(mbits);
mach_msg_type_name_t reply_type = MACH_MSGH_BITS_LOCAL(mbits);
mach_port_name_t dest_name, reply_name;
mach_msg_return_t mr;
assert(IO_VALID(dest));
mr = (ipc_kmsg_copyout_object(space, dest, dest_type, &dest_name) |
ipc_kmsg_copyout_object(space, reply, reply_type, &reply_name));
kmsg->ikm_header.msgh_bits = mbits &~ MACH_MSGH_BITS_CIRCULAR;
kmsg->ikm_header.msgh_remote_port = (ipc_port_t)dest_name;
kmsg->ikm_header.msgh_local_port = (ipc_port_t)reply_name;
if (mbits & MACH_MSGH_BITS_COMPLEX) {
mr |= ipc_kmsg_copyout_body(kmsg, space, map, slist);
}
return mr;
}
void
ipc_kmsg_copyout_dest(
ipc_kmsg_t kmsg,
ipc_space_t space)
{
mach_msg_bits_t mbits;
ipc_object_t dest;
ipc_object_t reply;
mach_msg_type_name_t dest_type;
mach_msg_type_name_t reply_type;
mach_port_name_t dest_name, reply_name;
mbits = kmsg->ikm_header.msgh_bits;
dest = (ipc_object_t) kmsg->ikm_header.msgh_remote_port;
reply = (ipc_object_t) kmsg->ikm_header.msgh_local_port;
dest_type = MACH_MSGH_BITS_REMOTE(mbits);
reply_type = MACH_MSGH_BITS_LOCAL(mbits);
assert(IO_VALID(dest));
io_lock(dest);
if (io_active(dest)) {
ipc_object_copyout_dest(space, dest, dest_type, &dest_name);
} else {
io_release(dest);
io_check_unlock(dest);
dest_name = MACH_PORT_DEAD;
}
if (IO_VALID(reply)) {
ipc_object_destroy(reply, reply_type);
reply_name = MACH_PORT_NULL;
} else
reply_name = (mach_port_name_t) reply;
kmsg->ikm_header.msgh_bits = (MACH_MSGH_BITS_OTHER(mbits) |
MACH_MSGH_BITS(reply_type, dest_type));
kmsg->ikm_header.msgh_local_port = (ipc_port_t)dest_name;
kmsg->ikm_header.msgh_remote_port = (ipc_port_t)reply_name;
if (mbits & MACH_MSGH_BITS_COMPLEX) {
mach_msg_body_t *body;
body = (mach_msg_body_t *) (&kmsg->ikm_header + 1);
ipc_kmsg_clean_body(kmsg, body->msgh_descriptor_count);
}
}
mach_msg_body_t *
ipc_kmsg_copyin_scatter(
mach_msg_header_t *msg,
mach_msg_size_t slist_size,
ipc_kmsg_t kmsg)
{
mach_msg_body_t *slist;
mach_msg_body_t *body;
mach_msg_descriptor_t *gstart, *gend;
mach_msg_descriptor_t *sstart, *send;
if (slist_size < sizeof(mach_msg_base_t))
return MACH_MSG_BODY_NULL;
slist_size -= sizeof(mach_msg_header_t);
slist = (mach_msg_body_t *)kalloc(slist_size);
if (slist == MACH_MSG_BODY_NULL)
return slist;
if (copyin((char *) (msg + 1), (char *)slist, slist_size)) {
kfree((vm_offset_t)slist, slist_size);
return MACH_MSG_BODY_NULL;
}
if ((slist->msgh_descriptor_count* sizeof(mach_msg_descriptor_t)
+ sizeof(mach_msg_size_t)) > slist_size) {
kfree((vm_offset_t)slist, slist_size);
return MACH_MSG_BODY_NULL;
}
body = (mach_msg_body_t *) (&kmsg->ikm_header + 1);
gstart = (mach_msg_descriptor_t *) (body + 1);
gend = gstart + body->msgh_descriptor_count;
sstart = (mach_msg_descriptor_t *) (slist + 1);
send = sstart + slist->msgh_descriptor_count;
while (gstart < gend) {
mach_msg_descriptor_type_t g_type;
g_type = gstart->type.type;
if (g_type != MACH_MSG_PORT_DESCRIPTOR) {
if (slist->msgh_descriptor_count == 0) {
kfree((vm_offset_t)slist, slist_size);
return MACH_MSG_BODY_NULL;
}
while (sstart < send) {
if (sstart->type.type != MACH_MSG_PORT_DESCRIPTOR)
break;
sstart++;
}
if (sstart >= send) {
break;
}
if (g_type == MACH_MSG_OOL_DESCRIPTOR ||
g_type == MACH_MSG_OOL_VOLATILE_DESCRIPTOR) {
if (sstart->type.type != MACH_MSG_OOL_DESCRIPTOR &&
sstart->type.type != MACH_MSG_OOL_VOLATILE_DESCRIPTOR) {
kfree((vm_offset_t)slist, slist_size);
return MACH_MSG_BODY_NULL;
}
if (sstart->out_of_line.copy == MACH_MSG_OVERWRITE &&
gstart->out_of_line.size > sstart->out_of_line.size) {
kfree((vm_offset_t)slist, slist_size);
return MACH_MSG_BODY_NULL;
}
}
else {
if (sstart->type.type != MACH_MSG_OOL_PORTS_DESCRIPTOR) {
kfree((vm_offset_t)slist, slist_size);
return MACH_MSG_BODY_NULL;
}
if (sstart->ool_ports.copy == MACH_MSG_OVERWRITE &&
gstart->ool_ports.count > sstart->ool_ports.count) {
kfree((vm_offset_t)slist, slist_size);
return MACH_MSG_BODY_NULL;
}
}
sstart++;
}
gstart++;
}
return slist;
}
void
ipc_kmsg_free_scatter(
mach_msg_body_t *slist,
mach_msg_size_t slist_size)
{
slist_size -= sizeof(mach_msg_header_t);
kfree((vm_offset_t)slist, slist_size);
}
void
ipc_kmsg_copyout_to_kernel(
ipc_kmsg_t kmsg,
ipc_space_t space)
{
ipc_object_t dest;
ipc_object_t reply;
mach_msg_type_name_t dest_type;
mach_msg_type_name_t reply_type;
mach_port_name_t dest_name, reply_name;
dest = (ipc_object_t) kmsg->ikm_header.msgh_remote_port;
reply = (ipc_object_t) kmsg->ikm_header.msgh_local_port;
dest_type = MACH_MSGH_BITS_REMOTE(kmsg->ikm_header.msgh_bits);
reply_type = MACH_MSGH_BITS_LOCAL(kmsg->ikm_header.msgh_bits);
assert(IO_VALID(dest));
io_lock(dest);
if (io_active(dest)) {
ipc_object_copyout_dest(space, dest, dest_type, &dest_name);
} else {
io_release(dest);
io_check_unlock(dest);
dest_name = MACH_PORT_DEAD;
}
reply_name = (mach_port_name_t) reply;
kmsg->ikm_header.msgh_bits =
(MACH_MSGH_BITS_OTHER(kmsg->ikm_header.msgh_bits) |
MACH_MSGH_BITS(reply_type, dest_type));
kmsg->ikm_header.msgh_local_port = (ipc_port_t)dest_name;
kmsg->ikm_header.msgh_remote_port = (ipc_port_t)reply_name;
}
#include <mach_kdb.h>
#if MACH_KDB
#include <ddb/db_output.h>
#include <ipc/ipc_print.h>
void ipc_msg_print_untyped(
mach_msg_body_t *body);
char * ipc_type_name(
int type_name,
boolean_t received);
void ipc_print_type_name(
int type_name);
char *
msgh_bit_decode(
mach_msg_bits_t bit);
char *
mm_copy_options_string(
mach_msg_copy_options_t option);
void db_print_msg_uid(mach_msg_header_t *);
char *
ipc_type_name(
int type_name,
boolean_t received)
{
switch (type_name) {
case MACH_MSG_TYPE_PORT_NAME:
return "port_name";
case MACH_MSG_TYPE_MOVE_RECEIVE:
if (received) {
return "port_receive";
} else {
return "move_receive";
}
case MACH_MSG_TYPE_MOVE_SEND:
if (received) {
return "port_send";
} else {
return "move_send";
}
case MACH_MSG_TYPE_MOVE_SEND_ONCE:
if (received) {
return "port_send_once";
} else {
return "move_send_once";
}
case MACH_MSG_TYPE_COPY_SEND:
return "copy_send";
case MACH_MSG_TYPE_MAKE_SEND:
return "make_send";
case MACH_MSG_TYPE_MAKE_SEND_ONCE:
return "make_send_once";
default:
return (char *) 0;
}
}
void
ipc_print_type_name(
int type_name)
{
char *name = ipc_type_name(type_name, TRUE);
if (name) {
printf("%s", name);
} else {
printf("type%d", type_name);
}
}
void
ipc_kmsg_print(
ipc_kmsg_t kmsg)
{
iprintf("kmsg=0x%x\n", kmsg);
iprintf("ikm_next=0x%x, prev=0x%x, size=%d",
kmsg->ikm_next,
kmsg->ikm_prev,
kmsg->ikm_size);
printf("\n");
ipc_msg_print(&kmsg->ikm_header);
}
char *
msgh_bit_decode(
mach_msg_bits_t bit)
{
switch (bit) {
case MACH_MSGH_BITS_COMPLEX: return "complex";
case MACH_MSGH_BITS_CIRCULAR: return "circular";
default: return (char *) 0;
}
}
void
ipc_msg_print(
mach_msg_header_t *msgh)
{
mach_msg_bits_t mbits;
unsigned int bit, i;
char *bit_name;
int needs_comma;
mbits = msgh->msgh_bits;
iprintf("msgh_bits=0x%x: l=0x%x,r=0x%x\n",
mbits,
MACH_MSGH_BITS_LOCAL(msgh->msgh_bits),
MACH_MSGH_BITS_REMOTE(msgh->msgh_bits));
mbits = MACH_MSGH_BITS_OTHER(mbits) & MACH_MSGH_BITS_USED;
db_indent += 2;
if (mbits)
iprintf("decoded bits: ");
needs_comma = 0;
for (i = 0, bit = 1; i < sizeof(mbits) * 8; ++i, bit <<= 1) {
if ((mbits & bit) == 0)
continue;
bit_name = msgh_bit_decode((mach_msg_bits_t)bit);
if (bit_name)
printf("%s%s", needs_comma ? "," : "", bit_name);
else
printf("%sunknown(0x%x),", needs_comma ? "," : "", bit);
++needs_comma;
}
if (msgh->msgh_bits & ~MACH_MSGH_BITS_USED) {
printf("%sunused=0x%x,", needs_comma ? "," : "",
msgh->msgh_bits & ~MACH_MSGH_BITS_USED);
}
printf("\n");
db_indent -= 2;
needs_comma = 1;
if (msgh->msgh_remote_port) {
iprintf("remote=0x%x(", msgh->msgh_remote_port);
ipc_print_type_name(MACH_MSGH_BITS_REMOTE(msgh->msgh_bits));
printf(")");
} else {
iprintf("remote=null");
}
if (msgh->msgh_local_port) {
printf("%slocal=0x%x(", needs_comma ? "," : "",
msgh->msgh_local_port);
ipc_print_type_name(MACH_MSGH_BITS_LOCAL(msgh->msgh_bits));
printf(")\n");
} else {
printf("local=null\n");
}
iprintf("msgh_id=%d, size=%d\n",
msgh->msgh_id,
msgh->msgh_size);
if (mbits & MACH_MSGH_BITS_COMPLEX) {
ipc_msg_print_untyped((mach_msg_body_t *) (msgh + 1));
}
}
char *
mm_copy_options_string(
mach_msg_copy_options_t option)
{
char *name;
switch (option) {
case MACH_MSG_PHYSICAL_COPY:
name = "PHYSICAL";
break;
case MACH_MSG_VIRTUAL_COPY:
name = "VIRTUAL";
break;
case MACH_MSG_OVERWRITE:
name = "OVERWRITE";
break;
case MACH_MSG_ALLOCATE:
name = "ALLOCATE";
break;
case MACH_MSG_KALLOC_COPY_T:
name = "KALLOC_COPY_T";
break;
default:
name = "unknown";
break;
}
return name;
}
void
ipc_msg_print_untyped(
mach_msg_body_t *body)
{
mach_msg_descriptor_t *saddr, *send;
mach_msg_descriptor_type_t type;
iprintf("%d descriptors %d: \n", body->msgh_descriptor_count);
saddr = (mach_msg_descriptor_t *) (body + 1);
send = saddr + body->msgh_descriptor_count;
for ( ; saddr < send; saddr++ ) {
type = saddr->type.type;
switch (type) {
case MACH_MSG_PORT_DESCRIPTOR: {
mach_msg_port_descriptor_t *dsc;
dsc = &saddr->port;
iprintf("-- PORT name = 0x%x disp = ", dsc->name);
ipc_print_type_name(dsc->disposition);
printf("\n");
break;
}
case MACH_MSG_OOL_VOLATILE_DESCRIPTOR:
case MACH_MSG_OOL_DESCRIPTOR: {
mach_msg_ool_descriptor_t *dsc;
dsc = &saddr->out_of_line;
iprintf("-- OOL%s addr = 0x%x size = 0x%x copy = %s %s\n",
type == MACH_MSG_OOL_DESCRIPTOR ? "" : " VOLATILE",
dsc->address, dsc->size,
mm_copy_options_string(dsc->copy),
dsc->deallocate ? "DEALLOC" : "");
break;
}
case MACH_MSG_OOL_PORTS_DESCRIPTOR : {
mach_msg_ool_ports_descriptor_t *dsc;
dsc = &saddr->ool_ports;
iprintf("-- OOL_PORTS addr = 0x%x count = 0x%x ",
dsc->address, dsc->count);
printf("disp = ");
ipc_print_type_name(dsc->disposition);
printf(" copy = %s %s\n",
mm_copy_options_string(dsc->copy),
dsc->deallocate ? "DEALLOC" : "");
break;
}
default: {
iprintf("-- UNKNOWN DESCRIPTOR 0x%x\n", type);
break;
}
}
}
}
#endif