#include <arch/arch.h>
#include <arch/mmu.h>
#include <arch/console.h>
-#include <ros/timer.h>
+#include <ros/time.h>
#include <error.h>
#include <elf.h>
#include <syscall.h>
#include <kmalloc.h>
#include <stdio.h>
-#include <resource.h>
#include <frontend.h>
#include <colored_caches.h>
#include <hashtable.h>
#include <smp.h>
#include <arsc_server.h>
#include <event.h>
+#include <termios.h>
#ifdef __CONFIG_NETWORKING__
/* Tracing Globals */
int systrace_flags = 0;
struct systrace_record *systrace_buffer = 0;
-unsigned int systrace_bufidx = 0;
+uint32_t systrace_bufidx = 0;
size_t systrace_bufsize = 0;
struct proc *systrace_procs[MAX_NUM_TRACED] = {0};
spinlock_t systrace_lock = SPINLOCK_INITIALIZER;
return false;
}
-/* Helper that "finishes" the current async syscall. This should be used when
- * we are calling a function in a syscall that might not return and won't be
- * able to use the normal syscall return path, such as proc_yield() and
- * resource_req(). Call this from within syscall.c (I don't want it global).
+/* Helper to finish a syscall, signalling if appropriate */
+static void finish_sysc(struct syscall *sysc, struct proc *p)
+{
+ /* Atomically turn on the LOCK and SC_DONE flag. The lock tells userspace
+ * we're messing with the flags and to not proceed. We use it instead of
+ * CASing with userspace. We need the atomics since we're racing with
+ * userspace for the event_queue registration. The 'lock' tells userspace
+ * to not muck with the flags while we're signalling. */
+ atomic_or(&sysc->flags, SC_K_LOCK | SC_DONE);
+ __signal_syscall(sysc, p);
+ atomic_and(&sysc->flags, ~SC_K_LOCK);
+}
+
+/* Helper that "finishes" the current async syscall. This should be used with
+ * care when we are not using the normal syscall completion path.
+ *
+ * Do *NOT* complete the same syscall twice. This is catastrophic for _Ms, and
+ * a bad idea for _S.
*
* It is possible for another user thread to see the syscall being done early -
* they just need to be careful with the weird proc management calls (as in,
*
* *sysc is in user memory, and should be pinned (TODO: UMEM). There may be
* issues with unpinning this if we never return. */
-static void signal_current_sc(int retval)
+static void finish_current_sysc(int retval)
{
struct per_cpu_info *pcpui = &per_cpu_info[core_id()];
+ assert(pcpui->cur_sysc);
pcpui->cur_sysc->retval = retval;
- pcpui->cur_sysc->flags |= SC_DONE;
+ finish_sysc(pcpui->cur_sysc, pcpui->cur_proc);
}
/* Callable by any function while executing a syscall (or otherwise, actually).
}
/* Diagnostic function: blocks the kthread/syscall, to help userspace test its
- * async I/O handling. Don't mix this with things that mess with the interrupt
- * handler, like other sys_blocks or the current blockdev crap. */
-static int sys_block(void)
-{
- struct semaphore local_sem, *sem = &local_sem;
- init_sem(sem, 0);
-#ifdef __i386__ /* Sparc can't register interrupt handlers yet */
- /* Faking an interrupt. The handler runs in interrupt context btw */
- void x86_unblock_handler(struct trapframe *tf, void *data)
- {
- /* Turn off the interrupt, Re-register the old dumb handler */
- set_core_timer(0);
- register_interrupt_handler(interrupt_handlers,
- LAPIC_TIMER_DEFAULT_VECTOR, timer_interrupt,
- NULL);
- struct semaphore *sem = (struct semaphore*)data;
- struct kthread *sleeper = __up_sem(sem);
- if (!sleeper) {
- warn("No one sleeping!");
- return;
- }
- kthread_runnable(sleeper);
- assert(TAILQ_EMPTY(&sem->waiters));
- }
-
- register_interrupt_handler(interrupt_handlers, LAPIC_TIMER_DEFAULT_VECTOR,
- x86_unblock_handler, sem);
- /* This fakes a 100ms delay. Though it might be less, esp in _M mode. TODO
- * KVM-timing. */
- set_core_timer(100000); /* in microseconds */
- printk("[kernel] sys_block(), sleeping at %llu\n", read_tsc());
- sleep_on(sem);
- printk("[kernel] sys_block(), waking up at %llu\n", read_tsc());
+ * async I/O handling. */
+static int sys_block(struct proc *p, unsigned int usec)
+{
+ struct timer_chain *tchain = &per_cpu_info[core_id()].tchain;
+ struct alarm_waiter a_waiter;
+ init_awaiter(&a_waiter, 0);
+ /* Note printing takes a few ms, so your printds won't be perfect. */
+ printd("[kernel] sys_block(), sleeping at %llu\n", read_tsc());
+ set_awaiter_rel(&a_waiter, usec);
+ set_alarm(tchain, &a_waiter);
+ sleep_on_awaiter(&a_waiter);
+ printd("[kernel] sys_block(), waking up at %llu\n", read_tsc());
return 0;
-#else /* sparc */
- set_errno(ENOSYS);
- return -1;
-#endif
}
// Writes 'val' to 'num_writes' entries of the well-known array in the kernel
static int sys_cache_buster(struct proc *p, uint32_t num_writes,
uint32_t num_pages, uint32_t flags)
{ TRUSTEDBLOCK /* zra: this is not really part of the kernel */
- #define BUSTER_ADDR 0xd0000000 // around 512 MB deep
+ #define BUSTER_ADDR 0xd0000000L // around 512 MB deep
#define MAX_WRITES 1048576*8
#define MAX_PAGES 32
#define INSERT_ADDR (UINFO + 2*PGSIZE) // should be free for these tests
// Read a character from the system console.
// Returns the character.
+/* TODO: remove me */
static uint16_t sys_cgetc(struct proc *p)
{
uint16_t c;
- // The cons_getc() primitive doesn't wait for a character,
+ // The cons_get_any_char() primitive doesn't wait for a character,
// but the sys_cgetc() system call does.
- while ((c = cons_getc()) == 0)
+ while ((c = cons_get_any_char()) == 0)
cpu_relax();
return c;
}
-/* Returns the id of the cpu this syscall is executed on. */
-static uint32_t sys_getcpuid(void)
+/* Returns the id of the physical core this syscall is executed on. */
+static uint32_t sys_getpcoreid(void)
{
return core_id();
}
// this is removed from the user interface
static size_t sys_getvcoreid(struct proc *p)
{
- return proc_get_vcoreid(p, core_id());
+ return proc_get_vcoreid(p);
}
/************** Process management syscalls **************/
assert(insert_file(&new_p->open_files, dev_stderr, 0) == 2);
__proc_ready(new_p);
pid = new_p->pid;
- kref_put(&new_p->kref); /* give up the reference created in proc_create() */
+ proc_decref(new_p); /* give up the reference created in proc_create() */
return pid;
late_error:
proc_destroy(new_p);
+ proc_decref(new_p); /* give up the reference created in proc_create() */
mid_error:
kref_put(&program->f_kref);
return -1;
struct proc *target = pid2proc(pid);
error_t retval = 0;
- if (!target)
- return -EBADPROC;
- // note we can get interrupted here. it's not bad.
- spin_lock(&p->proc_lock);
- // make sure we have access and it's in the right state to be activated
+ if (!target) {
+ set_errno(ESRCH);
+ return -1;
+ }
+ /* make sure we have access and it's in the right state to be activated */
if (!proc_controls(p, target)) {
- kref_put(&target->kref);
- retval = -EPERM;
+ set_errno(EPERM);
+ goto out_error;
} else if (target->state != PROC_CREATED) {
- kref_put(&target->kref);
- retval = -EINVAL;
- } else {
- __proc_set_state(target, PROC_RUNNABLE_S);
- schedule_proc(target);
+ set_errno(EINVAL);
+ goto out_error;
}
- spin_unlock(&p->proc_lock);
- kref_put(&target->kref);
- return retval;
+ /* Note a proc can spam this for someone it controls. Seems safe - if it
+ * isn't we can change it. */
+ proc_wakeup(target);
+ proc_decref(target);
+ return 0;
+out_error:
+ proc_decref(target);
+ return -1;
}
/* Destroy proc pid. If this is called by the dying process, it will never
* return. o/w it will return 0 on success, or an error. Errors include:
- * - EBADPROC: if there is no such process with pid
+ * - ESRCH: if there is no such process with pid
* - EPERM: if caller does not control pid */
static error_t sys_proc_destroy(struct proc *p, pid_t pid, int exitcode)
{
return -1;
}
if (!proc_controls(p, p_to_die)) {
- kref_put(&p_to_die->kref);
+ proc_decref(p_to_die);
set_errno(EPERM);
return -1;
}
p->exitcode = exitcode;
printd("[PID %d] proc exiting gracefully (code %d)\n", p->pid,exitcode);
} else {
+ p_to_die->exitcode = exitcode; /* so its parent has some clue */
printd("[%d] destroying proc %d\n", p->pid, p_to_die->pid);
}
proc_destroy(p_to_die);
/* we only get here if we weren't the one to die */
- kref_put(&p_to_die->kref);
- return ESUCCESS;
+ proc_decref(p_to_die);
+ return 0;
}
static int sys_proc_yield(struct proc *p, bool being_nice)
{
+ struct per_cpu_info *pcpui = &per_cpu_info[core_id()];
/* proc_yield() often doesn't return - we need to set the syscall retval
* early. If it doesn't return, it expects to eat our reference (for now).
*/
- signal_current_sc(0);
- kref_get(&p->kref, 1);
+ finish_sysc(pcpui->cur_sysc, pcpui->cur_proc);
+ pcpui->cur_sysc = 0; /* don't touch sysc again */
+ proc_incref(p, 1);
proc_yield(p, being_nice);
- kref_put(&p->kref);
- return 0;
+ proc_decref(p);
+ /* Shouldn't return, to prevent the chance of mucking with cur_sysc. */
+ smp_idle();
+ assert(0);
+}
+
+static int sys_change_vcore(struct proc *p, uint32_t vcoreid,
+ bool enable_my_notif)
+{
+ /* Note retvals can be negative, but we don't mess with errno in case
+ * callers use this in low-level code and want to extract the 'errno'. */
+ return proc_change_to_vcore(p, vcoreid, enable_my_notif);
}
static ssize_t sys_fork(env_t* e)
{
+ struct proc *temp;
+ int8_t state = 0;
// TODO: right now we only support fork for single-core processes
if (e->state != PROC_RUNNING_S) {
set_errno(EINVAL);
env->heap_top = e->heap_top;
env->ppid = e->pid;
+ disable_irqsave(&state); /* protect cur_tf */
/* Can't really fork if we don't have a current_tf to fork */
if (!current_tf) {
set_errno(EINVAL);
return -1;
}
env->env_tf = *current_tf;
-
- /* We need to speculatively say the syscall worked before copying the memory
- * out, since the 'forked' process's call never actually goes through the
- * syscall return path, and will never think it is done. This violates a
- * few things. Just be careful with fork. */
- signal_current_sc(0);
+ enable_irqsave(&state);
env->cache_colors_map = cache_colors_map_alloc();
for(int i=0; i < llc_cache->num_colors; i++)
if(GET_BITMASK_BIT(e->cache_colors_map,i))
cache_color_alloc(llc_cache, env->cache_colors_map);
- duplicate_vmrs(e, env);
-
- int copy_page(env_t* e, pte_t* pte, void* va, void* arg)
- {
- env_t* env = (env_t*)arg;
-
- if(PAGE_PRESENT(*pte))
- {
- page_t* pp;
- if(upage_alloc(env,&pp,0))
- return -1;
- if(page_insert(env->env_pgdir,pp,va,*pte & PTE_PERM))
- {
- page_decref(pp);
- return -1;
- }
- pagecopy(page2kva(pp),ppn2kva(PTE2PPN(*pte)));
- page_decref(pp);
- } else {
- assert(PAGE_PAGED_OUT(*pte));
- /* TODO: (SWAP) will need to either make a copy or CoW/refcnt the
- * backend store. For now, this PTE will be the same as the
- * original PTE */
- panic("Swapping not supported!");
- pte_t* newpte = pgdir_walk(env->env_pgdir,va,1);
- if(!newpte)
- return -1;
- *newpte = *pte;
- }
- return 0;
- }
-
- // TODO: (PC) this won't work. Needs revisiting.
- // copy procdata and procinfo
- memcpy(env->procdata,e->procdata,sizeof(struct procdata));
- memcpy(env->procinfo,e->procinfo,sizeof(struct procinfo));
- env->procinfo->pid = env->pid;
- env->procinfo->ppid = env->ppid;
-
- /* for now, just copy the contents of every present page in the entire
- * address space. */
- if (env_user_mem_walk(e, 0, UMAPTOP, ©_page, env)) {
+ /* Make the new process have the same VMRs as the older. This will copy the
+ * contents of non MAP_SHARED pages to the new VMRs. */
+ if (duplicate_vmrs(e, env)) {
proc_destroy(env); /* this is prob what you want, not decref by 2 */
+ proc_decref(env);
set_errno(ENOMEM);
return -1;
}
+ /* Switch to the new proc's address space and finish the syscall. We'll
+ * never naturally finish this syscall for the new proc, since its memory
+ * is cloned before we return for the original process. If we ever do CoW
+ * for forked memory, this will be the first place that gets CoW'd. */
+ temp = switch_to(env);
+ finish_current_sysc(0);
+ switch_back(env, temp);
+
+ /* In general, a forked process should be a fresh process, and we copy over
+ * whatever stuff is needed between procinfo/procdata. */
+ /* Copy over the procinfo argument stuff in case they don't exec */
+ memcpy(env->procinfo->argp, e->procinfo->argp, sizeof(e->procinfo->argp));
+ memcpy(env->procinfo->argbuf, e->procinfo->argbuf,
+ sizeof(e->procinfo->argbuf));
+ #ifdef __i386__
+ /* new guy needs to know about ldt (everything else in procdata is fresh */
+ env->procdata->ldt = e->procdata->ldt;
+ #endif
+
clone_files(&e->open_files, &env->open_files);
+ /* FYI: once we call ready, the proc is open for concurrent usage */
__proc_ready(env);
- __proc_set_state(env, PROC_RUNNABLE_S);
- schedule_proc(env);
+ proc_wakeup(env);
// don't decref the new process.
// that will happen when the parent waits for it.
char *t_path;
struct file *program;
struct per_cpu_info *pcpui = &per_cpu_info[core_id()];
- struct trapframe *old_cur_tf = pcpui->cur_tf;
+ int8_t state = 0;
/* We probably want it to never be allowed to exec if it ever was _M */
if (p->state != PROC_RUNNING_S) {
set_errno(EINVAL);
return -1;
}
+ /* Copy in the path. Consider putting an upper bound on path_l. */
+ t_path = user_strdup_errno(p, path, path_l);
+ if (!t_path)
+ return -1;
+ disable_irqsave(&state); /* protect cur_tf */
/* Can't exec if we don't have a current_tf to restart (if we fail). This
* isn't 100% true, but I'm okay with it. */
- if (!old_cur_tf) {
+ if (!pcpui->cur_tf) {
+ enable_irqsave(&state);
set_errno(EINVAL);
return -1;
}
- /* Copy in the path. Consider putting an upper bound on path_l. */
- t_path = user_strdup_errno(p, path, path_l);
- if (!t_path)
- return -1;
+ /* Preemptively copy out the cur_tf, in case we fail later (easier on cur_tf
+ * if we do this now) */
+ p->env_tf = *pcpui->cur_tf;
/* Clear the current_tf. We won't be returning the 'normal' way. Even if
* we want to return with an error, we need to go back differently in case
* we succeed. This needs to be done before we could possibly block, but
* unfortunately happens before the point of no return. */
pcpui->cur_tf = 0;
+ enable_irqsave(&state);
/* This could block: */
program = do_file_open(t_path, 0, 0);
user_memdup_free(p, t_path);
sizeof(pi->argbuf)))
goto mid_error;
/* This is the point of no return for the process. */
- /* TODO: issues with this: Need to also assert there are no outstanding
- * users of the sysrings. the ldt page will get freed shortly, so that's
- * okay. Potentially issues with the nm and vcpd if we were in _M before
- * and someone is trying to notify. */
- memset(p->procdata, 0, sizeof(procdata_t));
+ #ifdef __i386__
+ /* clear this, so the new program knows to get an LDT */
+ p->procdata->ldt = 0;
+ #endif
destroy_vmrs(p);
close_all_files(&p->open_files, TRUE);
env_user_mem_free(p, 0, UMAPTOP);
if (load_elf(p, program)) {
kref_put(&program->f_kref);
+ /* Note this is an inedible reference, but proc_destroy now returns */
proc_destroy(p);
/* We don't want to do anything else - we just need to not accidentally
* return to the user (hence the all_out) */
* error value (errno is already set). */
kref_put(&program->f_kref);
early_error:
- p->env_tf = *old_cur_tf;
- signal_current_sc(-1);
+ finish_current_sysc(-1);
success:
- /* Here's how we'll restart the new (or old) process: */
+ /* Here's how we restart the new (on success) or old (on failure) proc: */
spin_lock(&p->proc_lock);
- __proc_set_state(p, PROC_RUNNABLE_S);
- schedule_proc(p);
+ __unmap_vcore(p, 0); /* VC# keep in sync with proc_run_s */
+ __proc_set_state(p, PROC_WAITING); /* fake a yield */
spin_unlock(&p->proc_lock);
+ proc_wakeup(p);
all_out:
/* we can't return, since we'd write retvals to the old location of the
- * sycall struct (which has been freed and is in the old userspace) (or has
+ * syscall struct (which has been freed and is in the old userspace) (or has
* already been written to).*/
+ disable_irq(); /* abandon_core/clear_own wants irqs disabled */
+ clear_owning_proc(core_id());
abandon_core();
- smp_idle();
- assert(0);
-}
-
-static ssize_t sys_trywait(env_t* e, pid_t pid, int* status)
-{
- struct proc* p = pid2proc(pid);
-
- // TODO: this syscall is racy, so we only support for single-core procs
- if(e->state != PROC_RUNNING_S)
- return -1;
-
- // TODO: need to use errno properly. sadly, ROS error codes conflict..
-
- if(p)
- {
- ssize_t ret;
-
- if(current->pid == p->ppid)
- {
- if(p->state == PROC_DYING)
- {
- memcpy_to_user(e,status,&p->exitcode,sizeof(int));
- printd("[PID %d] waited for PID %d (code %d)\n",
- e->pid,p->pid,p->exitcode);
- ret = 0;
- }
- else // not dead yet
- {
- set_errno(ESUCCESS);
- ret = -1;
- }
- }
- else // not a child of the calling process
- {
- set_errno(EPERM);
- ret = -1;
- }
+ smp_idle(); /* will reenable interrupts */
+}
+
+/* Note: we only allow waiting on children (no such thing as threads, for
+ * instance). Right now we only allow waiting on termination (not signals),
+ * and we don't have a way for parents to disown their children (such as
+ * ignoring SIGCHLD, see man 2 waitpid's Notes). */
+static int sys_trywait(struct proc *parent, pid_t pid, int *status)
+{
+ /* TODO:
+ * - WAIT should handle stop and start via signal too
+ * - what semantics? need a wait for every change to state? etc.
+ * - should have an option for WNOHANG, and a bunch of other things.
+ * - think about what functions we want to work with MCPS
+ * */
+ struct proc* child = pid2proc(pid);
+ int ret = -1;
+ int ret_status;
- // if the wait succeeded, decref twice
- if (ret == 0)
- kref_put(&p->kref);
- kref_put(&p->kref);
- return ret;
+ if (!child) {
+ set_errno(ECHILD); /* ECHILD also used for no proc */
+ goto out;
}
-
- set_errno(EPERM);
- return -1;
+ if (!(parent->pid == child->ppid)) {
+ set_errno(ECHILD);
+ goto out_decref;
+ }
+ /* Block til there is some activity (DYING for now) */
+ if (!(child->state == PROC_DYING)) {
+ sleep_on(&child->state_change);
+ cpu_relax();
+ }
+ assert(child->state == PROC_DYING);
+ ret_status = child->exitcode;
+ /* wait succeeded - need to clean up the proc. */
+ proc_disown_child(parent, child);
+ /* fall through */
+out_success:
+ /* ignoring the retval here - don't care if they have a bad addr. */
+ memcpy_to_user(parent, status, &ret_status, sizeof(ret_status));
+ printd("[PID %d] waited for PID %d (code %d)\n", parent->pid,
+ pid, ret_status);
+ ret = 0;
+out_decref:
+ proc_decref(child);
+out:
+ return ret;
}
/************** Memory Management Syscalls **************/
return -1;
}
-
-static int sys_resource_req(struct proc *p, int type, unsigned int amt_wanted,
- unsigned int amt_wanted_min, int flags)
-{
- int retval;
- signal_current_sc(0);
- /* this might not return (if it's a _S -> _M transition) */
- kref_get(&p->kref, 1);
- retval = resource_req(p, type, amt_wanted, amt_wanted_min, flags);
- kref_put(&p->kref);
- return retval;
-}
-
/* Untested. Will notify the target on the given vcore, if the caller controls
* the target. Will honor the target's wanted/vcoreid. u_ne can be NULL. */
static int sys_notify(struct proc *p, int target_pid, unsigned int ev_type,
struct event_msg local_msg = {0};
struct proc *target = pid2proc(target_pid);
if (!target) {
- set_errno(EBADPROC);
+ set_errno(ESRCH);
return -1;
}
if (!proc_controls(p, target)) {
- kref_put(&target->kref);
+ proc_decref(target);
set_errno(EPERM);
return -1;
}
/* if the user provided an ev_msg, copy it in and use that */
if (u_msg) {
if (memcpy_from_user(p, &local_msg, u_msg, sizeof(struct event_msg))) {
- kref_put(&target->kref);
+ proc_decref(target);
set_errno(EINVAL);
return -1;
}
+ } else {
+ local_msg.ev_type = ev_type;
}
send_kernel_event(target, &local_msg, 0);
- kref_put(&target->kref);
+ proc_decref(target);
return 0;
}
/* Will notify the calling process on the given vcore, independently of WANTED
* or advertised vcoreid. If you change the parameters, change pop_ros_tf() */
static int sys_self_notify(struct proc *p, uint32_t vcoreid,
- unsigned int ev_type, struct event_msg *u_msg)
+ unsigned int ev_type, struct event_msg *u_msg,
+ bool priv)
{
struct event_msg local_msg = {0};
set_errno(EINVAL);
return -1;
}
+ } else {
+ local_msg.ev_type = ev_type;
}
/* this will post a message and IPI, regardless of wants/needs/debutantes.*/
- post_vcore_event(p, &local_msg, vcoreid);
+ post_vcore_event(p, &local_msg, vcoreid, priv ? EVENT_VCORE_PRIVATE : 0);
proc_notify(p, vcoreid);
return 0;
}
-/* This will set a local timer for usec, then shut down the core */
+/* This will set a local timer for usec, then shut down the core. There's a
+ * slight race between spinner and halt. For now, the core will wake up for
+ * other interrupts and service them, but will not process routine messages or
+ * do anything other than halt until the alarm goes off. We could just unset
+ * the alarm and return early. On hardware, there are a lot of interrupts that
+ * come in. If we ever use this, we can take a closer look. */
static int sys_halt_core(struct proc *p, unsigned int usec)
{
- /* TODO: ought to check and see if a timer was already active, etc, esp so
- * userspace can't turn off timers. also note we will also call whatever
- * timer_interrupt() will do, though all we care about is just
- * self_ipi/interrupting. */
- set_core_timer(usec);
- cpu_halt();
- set_core_timer(0); /* Disable the timer (we don't have a 0-shot yet) */
+ struct timer_chain *tchain = &per_cpu_info[core_id()].tchain;
+ struct alarm_waiter a_waiter;
+ bool spinner = TRUE;
+ void unblock(struct alarm_waiter *waiter)
+ {
+ spinner = FALSE;
+ }
+ init_awaiter(&a_waiter, unblock);
+ set_awaiter_rel(&a_waiter, MAX(usec, 100));
+ set_alarm(tchain, &a_waiter);
+ enable_irq();
+ /* Could wake up due to another interrupt, but we want to sleep still. */
+ while (spinner) {
+ cpu_halt(); /* slight race between spinner and halt */
+ cpu_relax();
+ }
+ printd("Returning from halting\n");
+ return 0;
+}
+
+/* Changes a process into _M mode, or -EINVAL if it already is an mcp.
+ * __proc_change_to_m() returns and we'll eventually finish the sysc later. The
+ * original context may restart on a remote core before we return and finish,
+ * but that's fine thanks to the async kernel interface. */
+static int sys_change_to_m(struct proc *p)
+{
+ int retval = proc_change_to_m(p);
+ /* convert the kernel error code into (-1, errno) */
+ if (retval) {
+ set_errno(-retval);
+ retval = -1;
+ }
+ return retval;
+}
+/* Not sure what people will need. For now, they can send in the resource they
+ * want. Up to the ksched to support this, and other things (like -1 for all
+ * resources). Might have this info go in via procdata instead. */
+static int sys_poke_ksched(struct proc *p, int res_type)
+{
+ poke_ksched(p, res_type);
return 0;
}
return 0;
#ifdef __CONFIG_SERIAL_IO__
- char *COUNT(len) buf = user_mem_assert(e, _buf, len, PTE_USER_RO);
+ char *COUNT(len) buf = user_mem_assert(e, _buf, len, 1, PTE_USER_RO);
size_t bytes_read = 0;
int c;
while((c = serial_read_byte()) != -1) {
if (len == 0)
return 0;
#ifdef __CONFIG_SERIAL_IO__
- char *COUNT(len) _buf = user_mem_assert(e, buf, len, PTE_USER_RO);
+ char *COUNT(len) _buf = user_mem_assert(e, buf, len, 1, PTE_USER_RO);
for(int i =0; i<len; i++)
serial_send_byte(buf[i]);
return (ssize_t)len;
spin_unlock(&packet_buffers_lock);
- char* _buf = user_mem_assert(e, buf, len, PTE_U);
+ char* _buf = user_mem_assert(e, buf, len, 1, PTE_U);
memcpy(_buf, ptr, len);
spin_unlock(>od_lock);
long long dt = read_tsc();
+ /* TODO: This probably wants its own function, using a struct timeval */
int kbuf[2] = {t0+dt/system_timing.tsc_freq,
(dt%system_timing.tsc_freq)*1000000/system_timing.tsc_freq};
return memcpy_to_user_errno(p,buf,kbuf,sizeof(kbuf));
}
-#define SIZEOF_STRUCT_TERMIOS 60
intreg_t sys_tcgetattr(struct proc *p, int fd, void *termios_p)
{
- int* kbuf = kmalloc(SIZEOF_STRUCT_TERMIOS,0);
- int ret = ufe(tcgetattr,fd,PADDR(kbuf),0,0);
- if(ret != -1 && memcpy_to_user_errno(p,termios_p,kbuf,SIZEOF_STRUCT_TERMIOS))
- ret = -1;
+ int retval = 0;
+ /* TODO: actually support this call on tty FDs. Right now, we just fake
+ * what my linux box reports for a bash pty. */
+ struct termios *kbuf = kmalloc(sizeof(struct termios), 0);
+ kbuf->c_iflag = 0x2d02;
+ kbuf->c_oflag = 0x0005;
+ kbuf->c_cflag = 0x04bf;
+ kbuf->c_lflag = 0x8a3b;
+ kbuf->c_line = 0x0;
+ kbuf->c_ispeed = 0xf;
+ kbuf->c_ospeed = 0xf;
+ kbuf->c_cc[0] = 0x03;
+ kbuf->c_cc[1] = 0x1c;
+ kbuf->c_cc[2] = 0x7f;
+ kbuf->c_cc[3] = 0x15;
+ kbuf->c_cc[4] = 0x04;
+ kbuf->c_cc[5] = 0x00;
+ kbuf->c_cc[6] = 0x01;
+ kbuf->c_cc[7] = 0xff;
+ kbuf->c_cc[8] = 0x11;
+ kbuf->c_cc[9] = 0x13;
+ kbuf->c_cc[10] = 0x1a;
+ kbuf->c_cc[11] = 0xff;
+ kbuf->c_cc[12] = 0x12;
+ kbuf->c_cc[13] = 0x0f;
+ kbuf->c_cc[14] = 0x17;
+ kbuf->c_cc[15] = 0x16;
+ kbuf->c_cc[16] = 0xff;
+ kbuf->c_cc[17] = 0x00;
+ kbuf->c_cc[18] = 0x00;
+ kbuf->c_cc[19] = 0x00;
+ kbuf->c_cc[20] = 0x00;
+ kbuf->c_cc[21] = 0x00;
+ kbuf->c_cc[22] = 0x00;
+ kbuf->c_cc[23] = 0x00;
+ kbuf->c_cc[24] = 0x00;
+ kbuf->c_cc[25] = 0x00;
+ kbuf->c_cc[26] = 0x00;
+ kbuf->c_cc[27] = 0x00;
+ kbuf->c_cc[28] = 0x00;
+ kbuf->c_cc[29] = 0x00;
+ kbuf->c_cc[30] = 0x00;
+ kbuf->c_cc[31] = 0x00;
+
+ if (memcpy_to_user_errno(p, termios_p, kbuf, sizeof(struct termios)))
+ retval = -1;
kfree(kbuf);
- return ret;
+ return retval;
}
intreg_t sys_tcsetattr(struct proc *p, int fd, int optional_actions,
const void *termios_p)
{
- void* kbuf = user_memdup_errno(p,termios_p,SIZEOF_STRUCT_TERMIOS);
- if(kbuf == NULL)
- return -1;
- int ret = ufe(tcsetattr,fd,optional_actions,PADDR(kbuf),0);
- user_memdup_free(p,kbuf);
- return ret;
+ /* TODO: do this properly too. For now, we just say 'it worked' */
+ return 0;
}
/* TODO: we don't have any notion of UIDs or GIDs yet, but don't let that stop a
[SYS_reboot] = {(syscall_t)reboot, "reboot!"},
[SYS_cputs] = {(syscall_t)sys_cputs, "cputs"},
[SYS_cgetc] = {(syscall_t)sys_cgetc, "cgetc"},
- [SYS_getcpuid] = {(syscall_t)sys_getcpuid, "getcpuid"},
+ [SYS_getpcoreid] = {(syscall_t)sys_getpcoreid, "getpcoreid"},
[SYS_getvcoreid] = {(syscall_t)sys_getvcoreid, "getvcoreid"},
[SYS_getpid] = {(syscall_t)sys_getpid, "getpid"},
[SYS_proc_create] = {(syscall_t)sys_proc_create, "proc_create"},
[SYS_proc_run] = {(syscall_t)sys_proc_run, "proc_run"},
[SYS_proc_destroy] = {(syscall_t)sys_proc_destroy, "proc_destroy"},
[SYS_yield] = {(syscall_t)sys_proc_yield, "proc_yield"},
+ [SYS_change_vcore] = {(syscall_t)sys_change_vcore, "change_vcore"},
[SYS_fork] = {(syscall_t)sys_fork, "fork"},
[SYS_exec] = {(syscall_t)sys_exec, "exec"},
[SYS_trywait] = {(syscall_t)sys_trywait, "trywait"},
[SYS_mprotect] = {(syscall_t)sys_mprotect, "mprotect"},
[SYS_shared_page_alloc] = {(syscall_t)sys_shared_page_alloc, "pa"},
[SYS_shared_page_free] = {(syscall_t)sys_shared_page_free, "pf"},
- [SYS_resource_req] = {(syscall_t)sys_resource_req, "resource_req"},
[SYS_notify] = {(syscall_t)sys_notify, "notify"},
[SYS_self_notify] = {(syscall_t)sys_self_notify, "self_notify"},
[SYS_halt_core] = {(syscall_t)sys_halt_core, "halt_core"},
#ifdef __CONFIG_ARSC_SERVER__
[SYS_init_arsc] = {(syscall_t)sys_init_arsc, "init_arsc"},
#endif
+ [SYS_change_to_m] = {(syscall_t)sys_change_to_m, "change_to_m"},
+ [SYS_poke_ksched] = {(syscall_t)sys_poke_ksched, "poke_ksched"},
[SYS_read] = {(syscall_t)sys_read, "read"},
[SYS_write] = {(syscall_t)sys_write, "write"},
[SYS_open] = {(syscall_t)sys_open, "open"},
if (systrace_flags & SYSTRACE_ON) {
if ((systrace_flags & SYSTRACE_ALLPROC) || (proc_is_traced(p))) {
coreid = core_id();
- vcoreid = proc_get_vcoreid(p, coreid);
+ vcoreid = proc_get_vcoreid(p);
if (systrace_flags & SYSTRACE_LOUD) {
printk("[%16llu] Syscall %3d (%12s):(%08p, %08p, %08p, %08p, "
"%08p, %08p) proc: %d core: %d vcore: %d\n", read_tsc(),
a4, a5, p->pid, coreid, vcoreid);
} else {
struct systrace_record *trace;
- unsigned int idx, new_idx;
+ uintptr_t idx, new_idx;
do {
idx = systrace_bufidx;
new_idx = (idx + 1) % systrace_bufsize;
- } while (!atomic_comp_swap(&systrace_bufidx, idx, new_idx));
+ } while (!atomic_cas_u32(&systrace_bufidx, idx, new_idx));
trace = &systrace_buffer[idx];
trace->timestamp = read_tsc();
trace->syscallno = sc_num;
}
}
if (sc_num > max_syscall || syscall_table[sc_num].call == NULL)
- panic("Invalid syscall number %d for proc %x!", sc_num, *p);
+ panic("Invalid syscall number %d for proc %x!", sc_num, p);
return syscall_table[sc_num].call(p, a0, a1, a2, a3, a4, a5);
}
/* Execute the syscall on the local core */
-static void run_local_syscall(struct syscall *sysc)
+void run_local_syscall(struct syscall *sysc)
{
struct per_cpu_info *pcpui = &per_cpu_info[core_id()];
/* TODO: (UMEM) assert / pin the memory for the sysc */
- user_mem_assert(pcpui->cur_proc, sysc, sizeof(struct syscall), PTE_USER_RW);
+ assert(irq_is_enabled()); /* in case we proc destroy */
+ user_mem_assert(pcpui->cur_proc, sysc, sizeof(struct syscall),
+ sizeof(uintptr_t), PTE_USER_RW);
pcpui->cur_sysc = sysc; /* let the core know which sysc it is */
sysc->retval = syscall(pcpui->cur_proc, sysc->num, sysc->arg0, sysc->arg1,
sysc->arg2, sysc->arg3, sysc->arg4, sysc->arg5);
- sysc->flags |= SC_DONE;
- signal_syscall(sysc, pcpui->cur_proc);
- /* Can unpin at this point */
+ /* Need to re-load pcpui, in case we migrated */
+ pcpui = &per_cpu_info[core_id()];
+ finish_sysc(sysc, pcpui->cur_proc);
+ /* Can unpin (UMEM) at this point */
+ pcpui->cur_sysc = 0; /* no longer working on sysc */
}
/* A process can trap and call this function, which will set up the core to
void prep_syscalls(struct proc *p, struct syscall *sysc, unsigned int nr_syscs)
{
int retval;
- struct per_cpu_info *pcpui = &per_cpu_info[core_id()];
+ /* Careful with pcpui here, we could have migrated */
if (!nr_syscs)
return;
/* For all after the first call, send ourselves a KMSG (TODO). */
/* Call this when something happens on the syscall where userspace might want to
* get signaled. Passing p, since the caller should know who the syscall
- * belongs to (probably is current). */
-void signal_syscall(struct syscall *sysc, struct proc *p)
+ * belongs to (probably is current).
+ *
+ * You need to have SC_K_LOCK set when you call this. */
+void __signal_syscall(struct syscall *sysc, struct proc *p)
{
struct event_queue *ev_q;
struct event_msg local_msg;
- ev_q = sysc->ev_q;
- if (ev_q) {
- memset(&local_msg, 0, sizeof(struct event_msg));
- local_msg.ev_type = EV_SYSCALL;
- local_msg.ev_arg3 = sysc;
- send_event(p, ev_q, &local_msg, 0);
+ /* User sets the ev_q then atomically sets the flag (races with SC_DONE) */
+ if (atomic_read(&sysc->flags) & SC_UEVENT) {
+ rmb(); /* read the ev_q after reading the flag */
+ ev_q = sysc->ev_q;
+ if (ev_q) {
+ memset(&local_msg, 0, sizeof(struct event_msg));
+ local_msg.ev_type = EV_SYSCALL;
+ local_msg.ev_arg3 = sysc;
+ send_event(p, ev_q, &local_msg, 0);
+ }
}
}