8000 minix/minix/kernel/system.c at master · WilsonCazarre/minix · GitHub
[go: up one dir, main page]

Skip to content

Latest commit

 

History

History
997 lines (860 loc) · 32.4 KB

system.c

File metadata and controls

997 lines (860 loc) · 32.4 KB
{
struct proc **rpp;
if (!RTS_ISSET(rp, RTS_VMREQUEST))
return; /* nothing to do */
for (rpp = &vmrequest; *rpp != NULL;
rpp = &(*rpp)->p_vmrequest.nextrequestor) {
if (*rpp == rp) {
*rpp = rp->p_vmrequest.nextrequestor;
break;
}
}
RTS_UNSET(rp, RTS_VMREQUEST);
}
/*===========================================================================*
* clear_ipc *
*===========================================================================*/
static void clear_ipc(
register struct proc *rc /* slot of process to clean up */
)
{
/* Clear IPC data for a given process slot. */
struct proc **xpp; /* iterate over caller queue */
if (RTS_ISSET(rc, RTS_SENDING)) {
int target_proc;
okendpt(rc->p_sendto_e, &target_proc);
xpp = &proc_addr(target_proc)->p_caller_q; /* destination's queue */
while (*xpp) { /* check entire queue */
if (*xpp == rc) { /* process is on the queue */
*xpp = (*xpp)->p_q_link; /* replace by next process */
#if DEBUG_ENABLE_IPC_WARNINGS
printf("endpoint %d / %s removed from queue at %d\n",
rc->p_endpoint, rc->p_name, rc->p_sendto_e);
#endif
break; /* can only be queued once */
}
xpp = &(*xpp)->p_q_link; /* proceed to next queued */
}
RTS_UNSET(rc, RTS_SENDING);
}
RTS_UNSET(rc, RTS_RECEIVING);
}
/*===========================================================================*
* clear_endpoint *
*===========================================================================*/
void clear_endpoint(struct proc * rc)
{
/* Clean up the slot of the process given as 'rc'. */
if(isemptyp(rc)) panic("clear_proc: empty process: %d", rc->p_endpoint);
#if DEBUG_IPC_HOOK
hook_ipc_clear(rc);
#endif
/* Make sure that the exiting process is no longer scheduled. */
RTS_SET(rc, RTS_NO_ENDPOINT);
if (priv(rc)->s_flags & SYS_PROC)
{
priv(rc)->s_asynsize= 0;
}
/* If the process happens to be queued trying to send a
* message, then it must be removed from the message queues.
*/
clear_ipc(rc);
/* Likewise, if another process was sending or receive a message to or from
* the exiting process, it must be alerted that process no longer is alive.
* Check all processes.
*/
clear_ipc_refs(rc, EDEADSRCDST);
/* Finally, if the process was blocked on a VM request, remove it from the
* queue of processes waiting to be processed by VM.
*/
clear_memreq(rc);
}
/*===========================================================================*
* clear_ipc_refs *
*===========================================================================*/
void clear_ipc_refs(
register struct proc *rc, /* slot of process to clean up */
int caller_ret /* code to return on callers */
)
{
/* Clear IPC references for a given process slot. */
struct proc *rp; /* iterate over process table */
int src_id;
/* Tell processes that sent asynchronous messages to 'rc' they are not
* going to be delivered */
while ((src_id = has_pending_asend(rc, ANY)) != NULL_PRIV_ID)
cancel_async(proc_addr(id_to_nr(src_id)), rc);
for (rp = BEG_PROC_ADDR; rp < END_PROC_ADDR; rp++) {
if(isemptyp(rp))
continue;
/* Unset pending notification bits. */
unset_sys_bit(priv(rp)->s_notify_pending, priv(rc)->s_id);
/* Unset pending asynchronous messages */
unset_sys_bit(priv(rp)->s_asyn_pending, priv(rc)->s_id);
/* Check if process depends on given process. */
if (P_BLOCKEDON(rp) == rc->p_endpoint) {
rp->p_reg.retreg = caller_ret; /* return requested code */
clear_ipc(rp);
}
}
}
/*===========================================================================*
* kernel_call_resume *
*===========================================================================*/
void kernel_call_resume(struct proc *caller)
{
int result;
assert(!RTS_ISSET(caller, RTS_SLOT_FREE));
assert(!RTS_ISSET(caller, RTS_VMREQUEST));
assert(caller->p_vmrequest.saved.reqmsg.m_source == caller->p_endpoint);
/*
printf("KERNEL_CALL restart from %s / %d rts 0x%08x misc 0x%08x\n",
caller->p_name, caller->p_endpoint,
caller->p_rts_flags, caller->p_misc_flags);
*/
/* re-execute the kernel call, with MF_KCALL_RESUME still set so
* the call knows this is a retry.
*/
result = kernel_call_dispatch(caller, &caller->p_vmrequest.saved.reqmsg);
/*
* we are resuming the kernel call so we have to remove this flag so it
* can be set again
*/
caller->p_misc_flags &= ~MF_KCALL_RESUME;
kernel_call_finish(caller, &caller->p_vmrequest.saved.reqmsg, result);
}
/*===========================================================================*
* sched_proc *
*===========================================================================*/
int sched_proc(struct proc *p, int priority, int quantum, int cpu, int niced)
{
/* Make sure the values given are within the allowed range.*/
if ((priority < TASK_Q && priority != -1) || priority > NR_SCHED_QUEUES)
return(EINVAL);
if (quantum < 1 && quantum != -1)
return(EINVAL);
#ifdef CONFIG_SMP
if ((cpu < 0 && cpu != -1) || (cpu > 0 && (unsigned) cpu >= ncpus))
return(EINVAL);
if (cpu != -1 && !(cpu_is_ready(cpu)))
return EBADCPU;
#endif
/* In some cases, we might be rescheduling a runnable process. In such
* a case (i.e. if we are updating the priority) we set the NO_QUANTUM
* flag before the generic unset to dequeue/enqueue the process
*/
/* FIXME this preempts the process, do we really want to do that ?*/
/* FIXME this is a problem for SMP if the processes currently runs on a
* different CPU */
if (proc_is_runnable(p)) {
#ifdef CONFIG_SMP
if (p->p_cpu != cpuid && cpu != -1 && cpu != p->p_cpu) {
smp_schedule_migrate_proc(p, cpu);
}
#endif
RTS_SET(p, RTS_NO_QUANTUM);
}
if (proc_is_runnable(p))
RTS_SET(p, RTS_NO_QUANTUM);
if (priority != -1)
p->p_priority = priority;
if (quantum != -1) {
p->p_quantum_size_ms = quantum;
p->p_cpu_time_left = ms_2_cpu_time(quantum);
}
#ifdef CONFIG_SMP
if (cpu != -1)
p->p_cpu = cpu;
#endif
if (niced)
p->p_misc_flags |= MF_NICED;
else
p->p_misc_flags &= ~MF_NICED;
/* Clear the scheduling bit and enqueue the process */
RTS_UNSET(p, RTS_NO_QUANTUM);
return OK;
}
/*===========================================================================*
* add_ipc_filter *
*===========================================================================*/
int add_ipc_filter(struct proc *rp, int type, vir_bytes address,
size_t length)
{
int num_elements, r;
ipc_filter_t *ipcf, **ipcfp;
/* Validate arguments. */
if (type != IPCF_BLACKLIST && type != IPCF_WHITELIST)
return EINVAL;
if (length % sizeof(ipc_filter_el_t) != 0)
return EINVAL;
num_elements = length / sizeof(ipc_filter_el_t);
if (num_elements <= 0 || num_elements > IPCF_MAX_ELEMENTS)
return E2BIG;
/* Allocate a new IPC filter slot. */
IPCF_POOL_ALLOCATE_SLOT(type, &ipcf);
if (ipcf == NULL)
return ENOMEM;
/* Fill details. */
ipcf->num_elements = num_elements;
ipcf->next = NULL;
r = data_copy(rp->p_endpoint, address,
KERNEL, (vir_bytes)ipcf->elements, length);
if (r == OK)
r = check_ipc_filter(ipcf, TRUE /*fill_flags*/);
if (r != OK) {
IPCF_POOL_FREE_SLOT(ipcf);
return r;
}
/* Add the new filter at the end of the IPC filter chain. */
for (ipcfp = &priv(rp)->s_ipcf; *ipcfp != NULL;
ipcfp = &(*ipcfp)->next)
;
*ipcfp = ipcf;
return OK;
}
/*===========================================================================*
* clear_ipc_filters *
*===========================================================================*/
void clear_ipc_filters(struct proc *rp)
{
ipc_filter_t *curr_ipcf, *ipcf;
ipcf = priv(rp)->s_ipcf;
while (ipcf != NULL) {
curr_ipcf = ipcf;
ipcf = ipcf->next;
IPCF_POOL_FREE_SLOT(curr_ipcf);
}
priv(rp)->s_ipcf = NULL;
/* VM is a special case here: since the cleared IPC filter may have
* blocked memory handling requests, we may now have to tell VM that
* there are "new" requests pending.
*/
if (rp->p_endpoint == VM_PROC_NR && vmrequest != NULL)
if (send_sig(VM_PROC_NR, SIGKMEM) != OK)
panic("send_sig failed");
}
/*===========================================================================*
* check_ipc_filter *
*===========================================================================*/
int check_ipc_filter(ipc_filter_t *ipcf, int fill_flags)
{
ipc_filter_el_t *ipcf_el;
int i, num_elements, flags;
if (ipcf == NULL)
return OK;
num_elements = ipcf->num_elements;
flags = 0;
for (i = 0; i < num_elements; i++) {
ipcf_el = &ipcf->elements[i];
if (!IPCF_EL_CHECK(ipcf_el))
return EINVAL;
flags |= ipcf_el->flags;
}
if (fill_flags)
ipcf->flags = flags;
else if (ipcf->flags != flags)
return EINVAL;
return OK;
}
/*===========================================================================*
* allow_ipc_filtered_msg *
*===========================================================================*/
int allow_ipc_filtered_msg(struct proc *rp, endpoint_t src_e,
vir_bytes m_src_v, message *m_src_p)
{
int i, r, num_elements, get_mtype, allow;
ipc_filter_t *ipcf;
ipc_filter_el_t *ipcf_el;
message m_buff;
ipcf = priv(rp)->s_ipcf;
if (ipcf == NULL)
return TRUE; /* no IPC filters, always allow */
if (m_src_p == NULL) {
assert(m_src_v != 0);
/* Should we copy in the message type? */
get_mtype = FALSE;
do {
#if DEBUG_DUMPIPCF
if (TRUE) {
#else
if (ipcf->flags & IPCF_MATCH_M_TYPE) {
#endif
get_mtype = TRUE;
break;
}
ipcf = ipcf->next;
} while (ipcf);
ipcf = priv(rp)->s_ipcf; /* reset to start */
/* If so, copy it in from the process. */
if (get_mtype) {
r = data_copy(src_e,
m_src_v + offsetof(message, m_type), KERNEL,
(vir_bytes)&m_buff.m_type, sizeof(m_buff.m_type));
if (r != OK) {
/* allow for now, this will fail later anyway */
#if DEBUG_DUMPIPCF
printf("KERNEL: allow_ipc_filtered_msg: data "
"copy error %d, allowing message...\n", r);
#endif
return TRUE;
}
}
m_src_p = &m_buff;
}
m_src_p->m_source = src_e;
/* See if the message is allowed. */
allow = (ipcf->type == IPCF_BLACKLIST);
do {
if (allow != (ipcf->type == IPCF_WHITELIST)) {
num_elements = ipcf->num_elements;
for (i = 0; i < num_elements; i++) {
ipcf_el = &ipcf->elements[i];
if (IPCF_EL_MATCH(ipcf_el, m_src_p)) {
allow = (ipcf->type == IPCF_WHITELIST);
break;
}
}
}
ipcf = ipcf->next;
} while (ipcf);
#if DEBUG_DUMPIPCF
printmsg(m_src_p, proc_addr(_ENDPOINT_P(src_e)), rp, allow ? '+' : '-',
TRUE /*printparams*/);
#endif
return allow;
}
/*===========================================================================*
* allow_ipc_filtered_memreq *
*===========================================================================*/
int allow_ipc_filtered_memreq(struct proc *src_rp, struct proc *dst_rp)
{
/* Determine whether VM should receive a request to handle memory
* that is the result of process 'src_rp' trying to access currently
* unavailable memory in process 'dst_rp'. Return TRUE if VM should
* be given the request, FALSE otherwise.
*/
struct proc *vmp;
message m_buf;
vmp = proc_addr(VM_PROC_NR);
/* If VM has no filter in place, all requests should go through. */
if (priv(vmp)->s_ipcf == NULL)
return TRUE;
/* VM obtains memory requests in response to a SIGKMEM signal, which
* is a notification sent from SYSTEM. Thus, if VM blocks such
* notifications, it also should not get any memory requests. Of
* course, VM should not be asking for requests in that case either,
* but the extra check doesn't hurt.
*/
m_buf.m_type = NOTIFY_MESSAGE;
if (!allow_ipc_filtered_msg(vmp, SYSTEM, 0, &m_buf))
return FALSE;
/* A more refined policy may be implemented here, for example to
* ensure that both the source and the destination (if different)
* are in the group of processes that VM wants to talk to. Since VM
* is basically not able to handle any memory requests during an
* update, we will not get here, and none of that is needed.
*/
return TRUE;
}
/*===========================================================================*
* priv_add_irq *
*===========================================================================*/
int priv_add_irq(struct proc *rp, int irq)
{
struct priv *priv = priv(rp);
int i;
priv->s_flags |= CHECK_IRQ; /* Check IRQ */
/* When restarting a driver, check if it already has the permission */
for (i = 0; i < priv->s_nr_irq; i++) {
if (priv->s_irq_tab[i] == irq)
return OK;
}
i= priv->s_nr_irq;
if (i >= NR_IRQ) {
printf("do_privctl: %d already has %d irq's.\n",
rp->p_endpoint, i);
return ENOMEM;
}
priv->s_irq_tab[i]= irq;
priv->s_nr_irq++;
return OK;
}
/*===========================================================================*
* priv_add_io *
*===========================================================================*/
int priv_add_io(struct proc *rp, struct io_range *ior)
{
struct priv *priv = priv(rp);
int i;
priv->s_flags |= CHECK_IO_PORT; /* Check I/O accesses */
for (i = 0; i < priv->s_nr_io_range; i++) {
if (priv->s_io_tab[i].ior_base == ior->ior_base &&
priv->s_io_tab[i].ior_limit == ior->ior_limit)
return OK;
}
i= priv->s_nr_io_range;
if (i >= NR_IO_RANGE) {
printf("do_privctl: %d already has %d i/o ranges.\n",
rp->p_endpoint, i);
return ENOMEM;
}
priv->s_io_tab[i] = *ior;
priv->s_nr_io_range++;
return OK;
}
/*===========================================================================*
* priv_add_mem *
*===========================================================================*/
int priv_add_mem(struct proc *rp, struct minix_mem_range *memr)
{
struct priv *priv = priv(rp);
int i;
priv->s_flags |= CHECK_MEM; /* Check memory mappings */
/* When restarting a driver, check if it already has the permission */
for (i = 0; i < priv->s_nr_mem_range; i++) {
if (priv->s_mem_tab[i].mr_base == memr->mr_base &&
priv->s_mem_tab[i].mr_limit == memr->mr_limit)
return OK;
}
i= priv->s_nr_mem_range;
if (i >= NR_MEM_RANGE) {
printf("do_privctl: %d already has %d mem ranges.\n",
rp->p_endpoint, i);
return ENOMEM;
}
priv->s_mem_tab[i]= *memr;
priv->s_nr_mem_range++;
return OK;
}
0