* Barret Rhoden <brho@cs.berkeley.edu>
* See LICENSE for details. */
-#ifdef __SHARC__
-#pragma nosharc
-#endif
-
-#include <ros/bcq.h>
#include <event.h>
#include <arch/arch.h>
#include <bitmask.h>
#include <smp.h>
#include <pmap.h>
#include <trap.h>
+#include <umem.h>
#include <schedule.h>
#include <manager.h>
#include <stdio.h>
atomic_init(&num_envs, 0);
}
+void proc_set_progname(struct proc *p, char *name)
+{
+ if (name == NULL)
+ name = DEFAULT_PROGNAME;
+
+ /* might have an issue if a dentry name isn't null terminated, and we'd get
+ * extra junk up to progname_sz. Or crash. */
+ strlcpy(p->progname, name, PROC_PROGNAME_SZ);
+}
+
+void proc_replace_binary_path(struct proc *p, char *path)
+{
+ if (p->binary_path)
+ free_path(p, p->binary_path);
+ p->binary_path = path;
+}
+
/* Be sure you init'd the vcore lists before calling this. */
-static void proc_init_procinfo(struct proc* p)
+void proc_init_procinfo(struct proc* p)
{
p->procinfo->pid = p->pid;
p->procinfo->ppid = p->ppid;
p->procinfo->timing_overhead = system_timing.timing_overhead;
p->procinfo->heap_bottom = 0;
/* 0'ing the arguments. Some higher function will need to set them */
- memset(p->procinfo->argp, 0, sizeof(p->procinfo->argp));
- memset(p->procinfo->argbuf, 0, sizeof(p->procinfo->argbuf));
memset(p->procinfo->res_grant, 0, sizeof(p->procinfo->res_grant));
/* 0'ing the vcore/pcore map. Will link the vcores later. */
memset(&p->procinfo->vcoremap, 0, sizeof(p->procinfo->vcoremap));
}
}
-static void proc_init_procdata(struct proc *p)
+void proc_init_procdata(struct proc *p)
{
memset(p->procdata, 0, sizeof(struct procdata));
/* processes can't go into vc context on vc 0 til they unset this. This is
if (!(p = kmem_cache_alloc(proc_cache, 0)))
return -ENOMEM;
/* zero everything by default, other specific items are set below */
- memset(p, 0, sizeof(struct proc));
-
- { INITSTRUCT(*p)
+ memset(p, 0, sizeof(*p));
/* only one ref, which we pass back. the old 'existence' ref is managed by
* the ksched */
kmem_cache_free(proc_cache, p);
return -ENOFREEPID;
}
+ if (parent && parent->binary_path)
+ kstrdup(&p->binary_path, parent->binary_path);
/* Set the basic status variables. */
spinlock_init(&p->proc_lock);
p->exitcode = 1337; /* so we can see processes killed by the kernel */
cv_init(&p->child_wait);
p->state = PROC_CREATED; /* shouldn't go through state machine for init */
p->env_flags = 0;
- p->env_entry = 0; // cheating. this really gets set later
p->heap_top = 0;
spinlock_init(&p->vmr_lock);
spinlock_init(&p->pte_lock);
p->open_files.open_fds = (struct fd_set*)&p->open_files.open_fds_init;
if (parent) {
if (flags & PROC_DUP_FGRP)
- clone_files(&parent->open_files, &p->open_files);
+ clone_fdt(&parent->open_files, &p->open_files);
} else {
/* no parent, we're created from the kernel */
- assert(insert_file(&p->open_files, dev_stdin, 0) == 0);
- assert(insert_file(&p->open_files, dev_stdout, 0) == 1);
- assert(insert_file(&p->open_files, dev_stderr, 0) == 2);
+ int fd;
+ fd = insert_file(&p->open_files, dev_stdin, 0, TRUE, FALSE);
+ assert(fd == 0);
+ fd = insert_file(&p->open_files, dev_stdout, 1, TRUE, FALSE);
+ assert(fd == 1);
+ fd = insert_file(&p->open_files, dev_stderr, 2, TRUE, FALSE);
+ assert(fd == 2);
}
/* Init the ucq hash lock */
p->ucq_hashlock = (struct hashlock*)&p->ucq_hl_noref;
atomic_inc(&num_envs);
frontend_proc_init(p);
- /* this does all the 9ns setup, much of which is done throughout this func
- * for the VFS, including duping the fgrp */
plan9setup(p, parent, flags);
devalarm_init(p);
TAILQ_INIT(&p->abortable_sleepers);
spinlock_init_irqsave(&p->abort_list_lock);
+ memset(&p->vmm, 0, sizeof(struct vmm));
+ qlock_init(&p->vmm.qlock);
printd("[%08x] new process %08x\n", current ? current->pid : 0, p->pid);
- } // INIT_STRUCT
*pp = p;
return 0;
}
error_t r;
if ((r = proc_alloc(&p, current, 0 /* flags */)) < 0)
panic("proc_create: %e", r); /* one of 3 quaint usages of %e */
- procinfo_pack_args(p->procinfo, argv, envp);
- assert(load_elf(p, prog) == 0);
+ int argc = 0, envc = 0;
+ if(argv) while(argv[argc]) argc++;
+ if(envp) while(envp[envc]) envc++;
+ proc_set_progname(p, argc ? argv[0] : NULL);
+ assert(load_elf(p, prog, argc, argv, envc, envp) == 0);
__proc_ready(p);
return p;
}
-static int __cb_assert_no_pg(struct proc *p, pte_t *pte, void *va, void *arg)
+static int __cb_assert_no_pg(struct proc *p, pte_t pte, void *va, void *arg)
{
- assert(!*pte);
+ assert(pte_is_unmapped(pte));
return 0;
}
assert(kref_refcnt(&p->p_kref) == 0);
assert(TAILQ_EMPTY(&p->alarmset.list));
+ __vmm_struct_cleanup(p);
+ p->progname[0] = 0;
+ free_path(p, p->binary_path);
cclose(p->dot);
cclose(p->slash);
p->dot = p->slash = 0; /* catch bugs */
- /* can safely free the fgrp, now that no one is accessing it */
- kfree(p->fgrp->fd);
- kfree(p->fgrp);
kref_put(&p->fs_env.root->d_kref);
kref_put(&p->fs_env.pwd->d_kref);
/* now we'll finally decref files for the file-backed vmrs */
free_cont_pages(p->procdata, LOG2_UP(PROCDATA_NUM_PAGES));
env_pagetable_free(p);
- p->env_pgdir = 0;
+ arch_pgdir_clear(&p->env_pgdir);
p->env_cr3 = 0;
atomic_dec(&num_envs);
return;
case (PROC_RUNNABLE_S):
__proc_set_state(p, PROC_RUNNING_S);
- /* We will want to know where this process is running, even if it is
- * only in RUNNING_S. can use the vcoremap, which makes death easy.
- * Also, this is the signal used in trap.c to know to save the tf in
- * scp_ctx. */
+ /* SCPs don't have full vcores, but they act like they have vcore 0.
+ * We map the vcore, since we will want to know where this process
+ * is running, even if it is only in RUNNING_S. We can use the
+ * vcoremap, which makes death easy. num_vcores is still 0, and we
+ * do account the time online and offline. */
__seq_start_write(&p->procinfo->coremap_seqctr);
- p->procinfo->num_vcores = 0; /* TODO (VC#) */
- /* TODO: For now, we won't count this as an active vcore (on the
- * lists). This gets unmapped in resource.c and yield_s, and needs
- * work. */
- __map_vcore(p, 0, coreid); /* not treated like a true vcore */
- vcore_account_online(p, 0); /* VC# */
+ p->procinfo->num_vcores = 0;
+ __map_vcore(p, 0, coreid);
+ vcore_account_online(p, 0);
__seq_end_write(&p->procinfo->coremap_seqctr);
/* incref, since we're saving a reference in owning proc later */
proc_incref(p, 1);
* for now. can simply clear_owning if we want to. */
assert(!pcpui->owning_proc);
pcpui->owning_proc = p;
- pcpui->owning_vcoreid = 0; /* TODO (VC#) */
+ pcpui->owning_vcoreid = 0;
restore_vc_fp_state(vcpd);
/* similar to the old __startcore, start them in vcore context if
* they have notifs and aren't already in vcore context. o/w, start
vcpd->uthread_ctx = p->scp_ctx;
pcpui->cur_ctx = &pcpui->actual_ctx;
memset(pcpui->cur_ctx, 0, sizeof(struct user_context));
- proc_init_ctx(pcpui->cur_ctx, 0, p->env_entry,
- vcpd->transition_stack, vcpd->vcore_tls_desc);
+ proc_init_ctx(pcpui->cur_ctx, 0, vcpd->vcore_entry,
+ vcpd->vcore_stack, vcpd->vcore_tls_desc);
} else {
/* If they have no transition stack, then they can't receive
* events. The most they are getting is a wakeup from the
assert(!irq_is_enabled());
/* Should never have ktask still set. If we do, future syscalls could try
* to block later and lose track of our address space. */
- assert(!pcpui->cur_kthread->is_ktask);
+ assert(!is_ktask(pcpui->cur_kthread));
__set_proc_current(p);
/* Clear the current_ctx, since it is no longer used */
current_ctx = 0; /* TODO: might not need this... */
+ __set_cpu_state(pcpui, CPU_STATE_USER);
proc_pop_ctx(ctx);
}
send_kernel_message(get_pcoreid(p, 0), __death, 0, 0, 0,
KMSG_ROUTINE);
__seq_start_write(&p->procinfo->coremap_seqctr);
- // TODO: might need to sort num_vcores too later (VC#)
- /* vcore is unmapped on the receive side */
+ __unmap_vcore(p, 0);
__seq_end_write(&p->procinfo->coremap_seqctr);
/* If we ever have RUNNING_S run on non-mgmt cores, we'll need to
* tell the ksched about this now-idle core (after unlocking) */
*
* Also note that any mmap'd files will still be mmapped. You can close the
* file after mmapping, with no effect. */
- close_9ns_files(p, FALSE);
- close_all_files(&p->open_files, FALSE);
+ close_fdt(&p->open_files, FALSE);
/* Tell the ksched about our death, and which cores we freed up */
__sched_proc_destroy(p, pc_arr, nr_cores_revoked);
/* Tell our parent about our state change (to DYING) */
switch (p->state) {
case (PROC_RUNNING_S):
/* issue with if we're async or not (need to preempt it)
- * either of these should trip it. TODO: (ACR) async core req
- * TODO: relies on vcore0 being the caller (VC#) */
+ * either of these should trip it. TODO: (ACR) async core req */
if ((current != p) || (get_pcoreid(p, 0) != core_id()))
panic("We don't handle async RUNNING_S core requests yet.");
struct preempt_data *vcpd = &p->procdata->vcore_preempt_data[0];
assert(current_ctx);
/* Copy uthread0's context to VC 0's uthread slot */
- vcpd->uthread_ctx = *current_ctx;
+ copy_current_ctx_to(&vcpd->uthread_ctx);
clear_owning_proc(core_id()); /* so we don't restart */
save_vc_fp_state(vcpd);
/* Userspace needs to not fuck with notif_disabled before
/* this process no longer runs on its old location (which is
* this core, for now, since we don't handle async calls) */
__seq_start_write(&p->procinfo->coremap_seqctr);
- // TODO: (VC#) might need to adjust num_vcores
// TODO: (ACR) will need to unmap remotely (receive-side)
- __unmap_vcore(p, 0); /* VC# keep in sync with proc_run_s */
- vcore_account_offline(p, 0); /* VC# */
+ __unmap_vcore(p, 0);
+ vcore_account_offline(p, 0);
__seq_end_write(&p->procinfo->coremap_seqctr);
/* change to runnable_m (it's TF is already saved) */
__proc_set_state(p, PROC_RUNNABLE_M);
assert(p->state == PROC_RUNNING_M); // TODO: (ACR) async core req
/* save the context, to be restarted in _S mode */
assert(current_ctx);
- p->scp_ctx = *current_ctx;
+ copy_current_ctx_to(&p->scp_ctx);
clear_owning_proc(core_id()); /* so we don't restart */
save_vc_fp_state(vcpd);
/* sending death, since it's not our job to save contexts or anything in
* In the future, we'll probably use vc0's space for scp_ctx and the silly
* state. If we ever do that, we'll need to stop using scp_ctx (soon to be in
* VCPD) as a location for pcpui->cur_ctx to point (dangerous) */
-void __proc_save_context_s(struct proc *p, struct user_context *ctx)
+void __proc_save_context_s(struct proc *p)
{
- p->scp_ctx = *ctx;
- __unmap_vcore(p, 0); /* VC# keep in sync with proc_run_s */
- vcore_account_offline(p, 0); /* VC# */
+ copy_current_ctx_to(&p->scp_ctx);
+ __seq_start_write(&p->procinfo->coremap_seqctr);
+ __unmap_vcore(p, 0);
+ __seq_end_write(&p->procinfo->coremap_seqctr);
+ vcore_account_offline(p, 0);
}
/* Yields the calling core. Must be called locally (not async) for now.
* We disable interrupts for most of it too, since we need to protect
* current_ctx and not race with __notify (which doesn't play well with
* concurrent yielders). */
-void proc_yield(struct proc *SAFE p, bool being_nice)
+void proc_yield(struct proc *p, bool being_nice)
{
uint32_t vcoreid, pcoreid = core_id();
struct per_cpu_info *pcpui = &per_cpu_info[pcoreid];
/* if we're here, we want to sleep. a concurrent event that
* hasn't already written notif_pending will have seen WAITING,
* and will be spinning while we do this. */
- __proc_save_context_s(p, current_ctx);
+ __proc_save_context_s(p);
spin_unlock(&p->proc_lock);
} else {
/* yielding to allow other processes to run. we're briefly
* WAITING, til we are woken up */
__proc_set_state(p, PROC_WAITING);
- __proc_save_context_s(p, current_ctx);
+ __proc_save_context_s(p);
spin_unlock(&p->proc_lock);
/* immediately wake up the proc (makes it runnable) */
proc_wakeup(p);
__proc_set_state(p, PROC_WAITING);
}
spin_unlock(&p->proc_lock);
+ /* We discard the current context, but we still need to restore the core */
+ arch_finalize_ctx(pcpui->cur_ctx);
/* Hand the now-idle core to the ksched */
__sched_put_idle_core(p, pcoreid);
goto out_yield_core;
return p->procinfo->is_mcp;
}
+bool proc_is_vcctx_ready(struct proc *p)
+{
+ struct preempt_data *vcpd = &p->procdata->vcore_preempt_data[0];
+ return scp_is_vcctx_ready(vcpd);
+}
+
/************************ Preemption Functions ******************************
* Don't rely on these much - I'll be sure to change them up a bit.
*
int __proc_give_cores(struct proc *p, uint32_t *pc_arr, uint32_t num)
{
/* should never happen: */
- assert(num + p->procinfo->num_vcores <= MAX_NUM_CPUS);
+ assert(num + p->procinfo->num_vcores <= MAX_NUM_CORES);
switch (p->state) {
case (PROC_RUNNABLE_S):
case (PROC_RUNNING_S):
pcpui->actual_ctx = vcpd->vcore_ctx;
proc_secure_ctx(&pcpui->actual_ctx);
} else { /* not restarting from a preemption, use a fresh vcore */
- assert(vcpd->transition_stack);
- proc_init_ctx(&pcpui->actual_ctx, vcoreid, p->env_entry,
- vcpd->transition_stack, vcpd->vcore_tls_desc);
+ assert(vcpd->vcore_stack);
+ proc_init_ctx(&pcpui->actual_ctx, vcoreid, vcpd->vcore_entry,
+ vcpd->vcore_stack, vcpd->vcore_tls_desc);
/* Disable/mask active notifications for fresh vcores */
vcpd->notif_disabled = TRUE;
}
* and we don't care about either the uthread_ctx or the vcore_ctx. */
caller_vcpd->notif_disabled = FALSE;
/* Don't need to save the FPU. There should be no uthread or other
- * reason to return to the FPU state. */
+ * reason to return to the FPU state. But we do need to finalize the
+ * context, even though we are throwing it away. We need to return the
+ * pcore to a state where it can run any context and not be bound to
+ * the old context. */
+ arch_finalize_ctx(pcpui->cur_ctx);
} else {
/* need to set up the calling vcore's ctx so that it'll get restarted by
* __startcore, to make the caller look like it was preempted. */
- caller_vcpd->vcore_ctx = *current_ctx;
+ copy_current_ctx_to(&caller_vcpd->vcore_ctx);
save_vc_fp_state(caller_vcpd);
- /* Mark our core as preempted (for userspace recovery). */
- atomic_or(&caller_vcpd->flags, VC_PREEMPTED);
}
+ /* Mark our core as preempted (for userspace recovery). Userspace checks
+ * this in handle_indirs, and it needs to check the mbox regardless of
+ * enable_my_notif. This does mean cores that change-to with no intent to
+ * return will be tracked as PREEMPTED until they start back up (maybe
+ * forever). */
+ atomic_or(&caller_vcpd->flags, VC_PREEMPTED);
/* Either way, unmap and offline our current vcore */
/* Move the caller from online to inactive */
TAILQ_REMOVE(&p->online_vcs, caller_vc, list);
uint32_t vcoreid = (uint32_t)a1;
uint32_t coreid = core_id();
struct per_cpu_info *pcpui = &per_cpu_info[coreid];
- struct proc *p_to_run = (struct proc *CT(1))a0;
+ struct proc *p_to_run = (struct proc *)a0;
uint32_t old_nr_preempts_sent = (uint32_t)a2;
assert(p_to_run);
vcpd->notif_disabled = TRUE;
/* save the old ctx in the uthread slot, build and pop a new one. Note that
* silly state isn't our business for a notification. */
- vcpd->uthread_ctx = *pcpui->cur_ctx;
+ copy_current_ctx_to(&vcpd->uthread_ctx);
memset(pcpui->cur_ctx, 0, sizeof(struct user_context));
- proc_init_ctx(pcpui->cur_ctx, vcoreid, p->env_entry,
- vcpd->transition_stack, vcpd->vcore_tls_desc);
+ proc_init_ctx(pcpui->cur_ctx, vcoreid, vcpd->vcore_entry,
+ vcpd->vcore_stack, vcpd->vcore_tls_desc);
/* this cur_ctx will get run when the kernel returns / idles */
}
* cur_ctx in the uthread slot, and it'll appear to the vcore when it comes
* back up the uthread just took a notification. */
if (vcpd->notif_disabled)
- vcpd->vcore_ctx = *pcpui->cur_ctx;
+ copy_current_ctx_to(&vcpd->vcore_ctx);
else
- vcpd->uthread_ctx = *pcpui->cur_ctx;
+ copy_current_ctx_to(&vcpd->uthread_ctx);
/* Userspace in a preemption handler on another core might be copying FP
* state from memory (VCPD) at the moment, and if so we don't want to
* clobber it. In this rare case, our current core's FPU state should be
vcore_account_offline(p, vcoreid); /* in case anyone is counting */
/* We won't restart the process later. current gets cleared later when
* we notice there is no owning_proc and we have nothing to do
- * (smp_idle, restartcore, etc) */
+ * (smp_idle, restartcore, etc). */
+ arch_finalize_ctx(pcpui->cur_ctx);
clear_owning_proc(coreid);
}
}
void print_allpids(void)
{
- void print_proc_state(void *item)
+ void print_proc_state(void *item, void *opaque)
{
struct proc *p = (struct proc*)item;
assert(p);
- printk("%8d %-10s %6d\n", p->pid, procstate2str(p->state), p->ppid);
+ /* this actually adds an extra space, since no progname is ever
+ * PROGNAME_SZ bytes, due to the \0 counted in PROGNAME. */
+ printk("%8d %-*s %-10s %6d\n", p->pid, PROC_PROGNAME_SZ, p->progname,
+ procstate2str(p->state), p->ppid);
}
- printk(" PID STATE Parent \n");
- printk("------------------------------\n");
+ char dashes[PROC_PROGNAME_SZ];
+ memset(dashes, '-', PROC_PROGNAME_SZ);
+ dashes[PROC_PROGNAME_SZ - 1] = '\0';
+ /* -5, for 'Name ' */
+ printk(" PID Name %-*s State Parent \n",
+ PROC_PROGNAME_SZ - 5, "");
+ printk("------------------------------%s\n", dashes);
spin_lock(&pid_hash_lock);
- hash_for_each(pid_hash, print_proc_state);
+ hash_for_each(pid_hash, print_proc_state, NULL);
spin_unlock(&pid_hash_lock);
}
+void proc_get_set(struct process_set *pset)
+{
+ void enum_proc(void *item, void *opaque)
+ {
+ struct proc *p = (struct proc*) item;
+ struct process_set *pset = (struct process_set *) opaque;
+
+ if (pset->num_processes < pset->size) {
+ proc_incref(p, 1);
+
+ pset->procs[pset->num_processes] = p;
+ pset->num_processes++;
+ }
+ }
+
+ static const size_t num_extra_alloc = 16;
+
+ pset->procs = NULL;
+ do {
+ if (pset->procs)
+ proc_free_set(pset);
+ pset->size = atomic_read(&num_envs) + num_extra_alloc;
+ pset->num_processes = 0;
+ pset->procs = (struct proc **)
+ kzmalloc(pset->size * sizeof(struct proc *), KMALLOC_WAIT);
+ if (!pset->procs)
+ error(-ENOMEM, NULL);
+
+ spin_lock(&pid_hash_lock);
+ hash_for_each(pid_hash, enum_proc, pset);
+ spin_unlock(&pid_hash_lock);
+
+ } while (pset->num_processes == pset->size);
+}
+
+void proc_free_set(struct process_set *pset)
+{
+ for (size_t i = 0; i < pset->num_processes; i++)
+ proc_decref(pset->procs[i]);
+ kfree(pset->procs);
+}
+
void print_proc_info(pid_t pid)
{
int j = 0;
spinlock_debug(&p->proc_lock);
//spin_lock(&p->proc_lock); // No locking!!
printk("struct proc: %p\n", p);
+ printk("Program name: %s\n", p->progname);
printk("PID: %d\n", p->pid);
printk("PPID: %d\n", p->ppid);
printk("State: %s (%p)\n", procstate2str(p->state), p->state);
printk("\tRes type: %02d, amt wanted: %08d, amt granted: %08d\n", i,
p->procdata->res_req[i].amt_wanted, p->procinfo->res_grant[i]);
printk("Open Files:\n");
- struct files_struct *files = &p->open_files;
+ struct fd_table *files = &p->open_files;
+ if (spin_locked(&files->lock)) {
+ spinlock_debug(&files->lock);
+ printk("FILE LOCK HELD, ABORTING\n");
+ proc_decref(p);
+ return;
+ }
spin_lock(&files->lock);
- for (int i = 0; i < files->max_files; i++)
- if (files->fd[i].fd_file) {
- printk("\tFD: %02d, File: %p, File name: %s\n", i,
- files->fd[i].fd_file, file_name(files->fd[i].fd_file));
+ for (int i = 0; i < files->max_files; i++) {
+ if (GET_BITMASK_BIT(files->open_fds->fds_bits, i)) {
+ printk("\tFD: %02d, ", i);
+ if (files->fd[i].fd_file) {
+ printk("File: %p, File name: %s\n", files->fd[i].fd_file,
+ file_name(files->fd[i].fd_file));
+ } else {
+ assert(files->fd[i].fd_chan);
+ print_chaninfo(files->fd[i].fd_chan);
+ }
}
+ }
spin_unlock(&files->lock);
- print_9ns_files(p);
printk("Children: (PID (struct proc *))\n");
TAILQ_FOREACH(child, &p->children, sibling_link)
printk("\t%d (%p)\n", child->pid, child);
void check_my_owner(void)
{
struct per_cpu_info *pcpui = &per_cpu_info[core_id()];
- void shazbot(void *item)
+ void shazbot(void *item, void *opaque)
{
struct proc *p = (struct proc*)item;
struct vcore *vc_i;
extern int booting;
if (!booting && !pcpui->owning_proc) {
spin_lock(&pid_hash_lock);
- hash_for_each(pid_hash, shazbot);
+ hash_for_each(pid_hash, shazbot, NULL);
spin_unlock(&pid_hash_lock);
}
}
-
-/* Use this via kfunc */
-void print_9ns(void)
-{
- void print_proc_9ns(void *item)
- {
- struct proc *p = (struct proc*)item;
- print_9ns_files(p);
- }
- spin_lock(&pid_hash_lock);
- hash_for_each(pid_hash, print_proc_9ns);
- spin_unlock(&pid_hash_lock);
-}