#endif
#include <ros/common.h>
-#include <ros/notification.h>
#include <arch/types.h>
#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 <arch/bitmask.h>
-#include <kfs.h> // eventually replace this with vfs.h
+#include <bitmask.h>
+#include <vfs.h>
+#include <devfs.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 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,
+ * don't trust an async fork).
+ *
+ * *sysc is in user memory, and should be pinned (TODO: UMEM). There may be
+ * issues with unpinning this if we never return. */
+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;
+ finish_sysc(pcpui->cur_sysc, pcpui->cur_proc);
+}
+
+/* Callable by any function while executing a syscall (or otherwise, actually).
+ */
+void set_errno(int errno)
+{
+ struct per_cpu_info *pcpui = &per_cpu_info[core_id()];
+ if (pcpui->cur_sysc)
+ pcpui->cur_sysc->err = errno;
+}
+
/************** Utility Syscalls **************/
static int sys_null(void)
return 0;
}
+/* Diagnostic function: blocks the kthread/syscall, to help userspace test its
+ * 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;
+}
+
// Writes 'val' to 'num_writes' entries of the well-known array in the kernel
// address space. It's just #defined to be some random 4MB chunk (which ought
// to be boot_alloced or something). Meant to grab exclusive access to cache
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
upage_alloc(p, &a_page[i],1);
page_insert(p->env_pgdir, a_page[i], (void*)INSERT_ADDR + PGSIZE*i,
PTE_USER_RW);
+ page_decref(a_page[i]);
}
spin_unlock(&buster_lock);
}
// 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 **************/
if (load_elf(new_p, program))
goto late_error;
kref_put(&program->f_kref);
+ /* Connect to stdin, stdout, stderr (part of proc_create()) */
+ assert(insert_file(&new_p->open_files, dev_stdin, 0) == 0);
+ assert(insert_file(&new_p->open_files, dev_stdout, 0) == 1);
+ 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;
}
if (p_to_die == p) {
- // syscall code and pid2proc both have edible references, only need 1.
p->exitcode = exitcode;
- kref_put(&p_to_die->kref);
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);
- kref_put(&p_to_die->kref);
- return ESUCCESS;
+ /* we only get here if we weren't the one to die */
+ 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).
+ */
+ 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);
- 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);
return -1;
}
- /* Can't really fork if we don't have a current_tf to fork */
- if (!current_tf) {
- set_errno(EINVAL);
- return -1;
- }
env_t* env;
assert(!proc_alloc(&env, current));
assert(env != NULL);
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;
+ 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)));
- } 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.
// when the parent dies, or at least decref it
printd("[PID %d] fork PID %d\n",e->pid,env->pid);
-
return env->pid;
}
/* Load the binary "path" into the current process, and start executing it.
* argv and envp are magically bundled in procinfo for now. Keep in sync with
- * glibc's sysdeps/ros/execve.c */
+ * glibc's sysdeps/ros/execve.c. Once past a certain point, this function won't
+ * return. It assumes (and checks) that it is current. Don't give it an extra
+ * refcnt'd *p (syscall won't do that).
+ * Note: if someone batched syscalls with this call, they could clobber their
+ * old memory (and will likely PF and die). Don't do it... */
static int sys_exec(struct proc *p, char *path, size_t path_l,
struct procinfo *pi)
{
int ret = -1;
char *t_path;
struct file *program;
+ struct per_cpu_info *pcpui = &per_cpu_info[core_id()];
+ 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;
}
- /* Can't really exec if we don't have a current_tf to reset */
- if (!current_tf) {
+ if (p != pcpui->cur_proc) {
set_errno(EINVAL);
return -1;
}
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 (!pcpui->cur_tf) {
+ enable_irqsave(&state);
+ set_errno(EINVAL);
+ 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);
if (!program)
- return -1; /* presumably, errno is already set */
+ goto early_error;
/* Set the argument stuff needed by glibc */
if (memcpy_from_user_errno(p, p->procinfo->argp, pi->argp,
sizeof(pi->argp)))
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);
- smp_idle(); /* syscall can't return on failure now */
+ /* We don't want to do anything else - we just need to not accidentally
+ * return to the user (hence the all_out) */
+ goto all_out;
}
printd("[PID %d] exec %s\n", p->pid, file_name(program));
kref_put(&program->f_kref);
- *current_tf = p->env_tf;
- return 0;
+ goto success;
+ /* These error and out paths are so we can handle the async interface, both
+ * for when we want to error/return to the proc, as well as when we succeed
+ * and want to start the newly exec'd _S */
mid_error:
+ /* These two error paths are for when we want to restart the process with an
+ * error value (errno is already set). */
kref_put(&program->f_kref);
- return -1;
+early_error:
+ finish_current_sysc(-1);
+success:
+ /* Here's how we restart the new (on success) or old (on failure) proc: */
+ spin_lock(&p->proc_lock);
+ __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
+ * 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(); /* will reenable interrupts */
}
-static ssize_t sys_trywait(env_t* e, pid_t pid, int* status)
+/* 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)
{
- 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;
- }
+ /* 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 **************/
-static void *sys_mmap(struct proc *p, uintreg_t a1, uintreg_t a2, uintreg_t a3,
- uintreg_t *a456)
+static void *sys_mmap(struct proc *p, uintptr_t addr, size_t len, int prot,
+ int flags, int fd, off_t offset)
{
- uintreg_t _a456[3];
- if (memcpy_from_user(p, _a456, a456, 3 * sizeof(uintreg_t)))
- sys_proc_destroy(p, p->pid, -1);
- return mmap(p, a1, a2, a3, _a456[0], _a456[1], _a456[2]);
+ return mmap(p, addr, len, prot, flags, fd, offset);
}
static intreg_t sys_mprotect(struct proc *p, void *addr, size_t len, int prot)
int p1_flags, int p2_flags
)
{
- /* When we remove/change this, also get rid of page_insert_in_range() */
- printk("[kernel] the current shared page alloc is deprecated.\n");
- //if (!VALID_USER_PERMS(p1_flags)) return -EPERM;
- //if (!VALID_USER_PERMS(p2_flags)) return -EPERM;
-
- void * COUNT(1) * COUNT(1) addr = user_mem_assert(p1, _addr, sizeof(void *),
- PTE_USER_RW);
- struct proc *p2 = pid2proc(p2_id);
- if (!p2)
- return -EBADPROC;
-
- page_t* page;
- error_t e = upage_alloc(p1, &page,1);
- if (e < 0) {
- kref_put(&p2->kref);
- return e;
- }
-
- void* p2_addr = page_insert_in_range(p2->env_pgdir, page,
- (void*SNT)UTEXT, (void*SNT)UTOP, p2_flags);
- if (p2_addr == NULL) {
- page_free(page);
- kref_put(&p2->kref);
- return -EFAIL;
- }
-
- void* p1_addr = page_insert_in_range(p1->env_pgdir, page,
- (void*SNT)UTEXT, (void*SNT)UTOP, p1_flags);
- if(p1_addr == NULL) {
- page_remove(p2->env_pgdir, p2_addr);
- page_free(page);
- kref_put(&p2->kref);
- return -EFAIL;
- }
- *addr = p1_addr;
- kref_put(&p2->kref);
- return ESUCCESS;
+ printk("[kernel] shared page alloc is deprecated/unimplemented.\n");
+ return -1;
}
static int sys_shared_page_free(env_t* p1, void*DANGEROUS addr, pid_t p2)
return -1;
}
-
-/* sys_resource_req(): called directly from dispatch table. */
-
-/* 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 notif,
- struct notif_event *u_ne)
+/* 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 *u_msg)
{
- struct notif_event local_ne;
+ 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 a notif_event, copy it in and use that */
- if (u_ne) {
- if (memcpy_from_user(p, &local_ne, u_ne, sizeof(struct notif_event))) {
- kref_put(&target->kref);
+ /* 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))) {
+ proc_decref(target);
set_errno(EINVAL);
return -1;
}
- proc_notify(target, local_ne.ne_type, &local_ne);
} else {
- proc_notify(target, notif, 0);
+ local_msg.ev_type = ev_type;
}
- kref_put(&target->kref);
+ send_kernel_event(target, &local_msg, 0);
+ 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 notif,
- struct notif_event *u_ne)
+static int sys_self_notify(struct proc *p, uint32_t vcoreid,
+ unsigned int ev_type, struct event_msg *u_msg,
+ bool priv)
{
- struct notif_event local_ne;
+ struct event_msg local_msg = {0};
- printd("[kernel] received self notify for vcoreid %d, notif %d, ne %08p\n",
- vcoreid, notif, u_ne);
- /* if the user provided a notif_event, copy it in and use that */
- if (u_ne) {
- if (memcpy_from_user(p, &local_ne, u_ne, sizeof(struct notif_event))) {
+ printd("[kernel] received self notify for vcoreid %d, type %d, msg %08p\n",
+ vcoreid, ev_type, u_msg);
+ /* 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))) {
set_errno(EINVAL);
return -1;
}
- do_notify(p, vcoreid, local_ne.ne_type, &local_ne);
} else {
- do_notify(p, vcoreid, notif, 0);
+ 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, 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();
+ 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);
set_errno(EBADF);
return -1;
}
+ if (!file->f_op->read) {
+ kref_put(&file->f_kref);
+ set_errno(EINVAL);
+ return -1;
+ }
/* TODO: (UMEM) currently, read() handles user memcpy issues, but we
* probably should user_mem_check and pin the region here, so read doesn't
* worry about it */
static intreg_t sys_write(struct proc *p, int fd, const void *buf, int len)
{
- /* Catch common usage of stdout and stderr. No protections or anything. */
- if (fd == 1) {
- printk("[stdout]: %s\n", buf);
- return len;
- } else if (fd == 2) {
- printk("[stderr]: %s\n", buf);
- return len;
- }
- /* the real sys_write: */
ssize_t ret;
struct file *file = get_file_from_fd(&p->open_files, fd);
if (!file) {
set_errno(EBADF);
return -1;
}
+ if (!file->f_op->write) {
+ kref_put(&file->f_kref);
+ set_errno(EINVAL);
+ return -1;
+ }
/* TODO: (UMEM) */
ret = file->f_op->write(file, buf, len, &file->f_pos);
kref_put(&file->f_kref);
int fd = 0;
struct file *file;
+ printd("File %s Open attempt\n", path);
char *t_path = user_strdup_errno(p, path, path_l);
if (!t_path)
return -1;
+ mode &= ~p->fs_env.umask;
file = do_file_open(t_path, oflag, mode);
user_memdup_free(p, t_path);
if (!file)
return -1;
- fd = insert_file(&p->open_files, file); /* stores the ref to file */
+ fd = insert_file(&p->open_files, file, 0); /* stores the ref to file */
kref_put(&file->f_kref);
if (fd < 0) {
warn("File insertion failed");
}
switch (cmd) {
case (F_DUPFD):
- printk("[kernel] dup not supported yet\n");
+ retval = insert_file(&p->open_files, file, arg);
+ if (retval < 0) {
+ set_errno(-retval);
+ retval = -1;
+ }
break;
case (F_GETFD):
- /* GET and SETFD just care about CLOEXEC. We don't have a separate
- * flag variable for the FD (we might need to, technically). */
- if (file->f_flags & O_CLOEXEC)
- retval = FD_CLOEXEC;
+ retval = p->open_files.fd[fd].fd_flags;
break;
case (F_SETFD):
if (arg == FD_CLOEXEC)
break;
case (F_SETFL):
/* only allowed to set certain flags. */
- arg &= O_APPEND | O_ASYNC | O_DIRECT | O_NOATIME | O_NONBLOCK;
+ arg &= O_FCNTL_FLAGS;
+ file->f_flags = (file->f_flags & ~O_FCNTL_FLAGS) | arg;
break;
default:
warn("Unsupported fcntl cmd %d\n", cmd);
}
kref_put(&file->f_kref);
- return 0;
+ return retval;
}
static intreg_t sys_access(struct proc *p, const char *path, size_t path_l,
int mode)
{
int retval;
-
char *t_path = user_strdup_errno(p, path, path_l);
if (!t_path)
return -1;
- retval = do_file_access(t_path, mode);
+ retval = do_access(t_path, mode);
user_memdup_free(p, t_path);
printd("Access for path: %s retval: %d\n", path, retval);
if (retval < 0) {
intreg_t sys_umask(struct proc *p, int mask)
{
- return ufe(umask,mask,0,0,0);
+ int old_mask = p->fs_env.umask;
+ p->fs_env.umask = mask & S_PMASK;
+ return old_mask;
}
intreg_t sys_chmod(struct proc *p, const char *path, size_t path_l, int mode)
{
- char* fn = user_strdup_errno(p,path,PGSIZE);
- if(fn == NULL)
+ int retval;
+ char *t_path = user_strdup_errno(p, path, path_l);
+ if (!t_path)
return -1;
- int ret = ufe(chmod,PADDR(fn),mode,0,0);
- user_memdup_free(p,fn);
- return ret;
+ retval = do_chmod(t_path, mode);
+ user_memdup_free(p, t_path);
+ if (retval < 0) {
+ set_errno(-retval);
+ return -1;
+ }
+ return retval;
}
static intreg_t sys_lseek(struct proc *p, int fd, off_t offset, int whence)
return ret;
}
-intreg_t sys_link(struct proc *p, const char *_old, size_t old_l,
- const char *_new, size_t new_l)
+intreg_t sys_link(struct proc *p, char *old_path, size_t old_l,
+ char *new_path, size_t new_l)
{
- char* oldpath = user_strdup_errno(p,_old,PGSIZE);
- if(oldpath == NULL)
+ int ret;
+ char *t_oldpath = user_strdup_errno(p, old_path, old_l);
+ if (t_oldpath == NULL)
return -1;
-
- char* newpath = user_strdup_errno(p,_new,PGSIZE);
- if(newpath == NULL)
- {
- user_memdup_free(p,oldpath);
+ char *t_newpath = user_strdup_errno(p, new_path, new_l);
+ if (t_newpath == NULL) {
+ user_memdup_free(p, t_oldpath);
return -1;
}
-
- int ret = ufe(link,PADDR(oldpath),PADDR(newpath),0,0);
- user_memdup_free(p,oldpath);
- user_memdup_free(p,newpath);
+ ret = do_link(t_oldpath, t_newpath);
+ user_memdup_free(p, t_oldpath);
+ user_memdup_free(p, t_newpath);
return ret;
}
intreg_t sys_unlink(struct proc *p, const char *path, size_t path_l)
{
- char* fn = user_strdup_errno(p,path,PGSIZE);
- if(fn == NULL)
+ int retval;
+ char *t_path = user_strdup_errno(p, path, path_l);
+ if (!t_path)
return -1;
- int ret = ufe(unlink,PADDR(fn),0,0,0);
- user_memdup_free(p,fn);
- return ret;
+ retval = do_unlink(t_path);
+ user_memdup_free(p, t_path);
+ return retval;
}
intreg_t sys_symlink(struct proc *p, char *old_path, size_t old_l,
intreg_t sys_chdir(struct proc *p, const char *path, size_t path_l)
{
- char* fn = user_strdup_errno(p,path,PGSIZE);
- if(fn == NULL)
+ int retval;
+ char *t_path = user_strdup_errno(p, path, path_l);
+ if (!t_path)
return -1;
- int ret = ufe(chdir,PADDR(fn),0,0,0);
- user_memdup_free(p,fn);
- return ret;
+ retval = do_chdir(&p->fs_env, t_path);
+ user_memdup_free(p, t_path);
+ if (retval) {
+ set_errno(-retval);
+ return -1;
+ }
+ return 0;
+}
+
+/* Note cwd_l is not a strlen, it's an absolute size */
+intreg_t sys_getcwd(struct proc *p, char *u_cwd, size_t cwd_l)
+{
+ int retval = 0;
+ char *kfree_this;
+ char *k_cwd = do_getcwd(&p->fs_env, &kfree_this, cwd_l);
+ if (!k_cwd)
+ return -1; /* errno set by do_getcwd */
+ if (memcpy_to_user_errno(p, u_cwd, k_cwd, strnlen(k_cwd, cwd_l - 1) + 1))
+ retval = -1;
+ kfree(kfree_this);
+ return retval;
+}
+
+intreg_t sys_mkdir(struct proc *p, const char *path, size_t path_l, int mode)
+{
+ int retval;
+ char *t_path = user_strdup_errno(p, path, path_l);
+ if (!t_path)
+ return -1;
+ mode &= ~p->fs_env.umask;
+ retval = do_mkdir(t_path, mode);
+ user_memdup_free(p, t_path);
+ return retval;
}
-intreg_t sys_getcwd(struct proc *p, char *pwd, int size)
+intreg_t sys_rmdir(struct proc *p, const char *path, size_t path_l)
{
- void* kbuf = kmalloc_errno(size);
- if(kbuf == NULL)
+ int retval;
+ char *t_path = user_strdup_errno(p, path, path_l);
+ if (!t_path)
return -1;
- int ret = ufe(read,PADDR(kbuf),size,0,0);
- if(ret != -1 && memcpy_to_user_errno(p,pwd,kbuf,strnlen(kbuf,size)))
- ret = -1;
- user_memdup_free(p,kbuf);
- return ret;
+ retval = do_rmdir(t_path);
+ user_memdup_free(p, t_path);
+ return retval;
}
intreg_t sys_gettimeofday(struct proc *p, int *buf)
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
+ * process from thinking it can do these. The other alternative is to have
+ * glibc return 0 right away, though someone might want to do something with
+ * these calls. Someday. */
+intreg_t sys_setuid(struct proc *p, uid_t uid)
+{
+ return 0;
+}
+
+intreg_t sys_setgid(struct proc *p, gid_t gid)
+{
+ return 0;
}
/************** Syscall Invokation **************/
+const static struct sys_table_entry syscall_table[] = {
+ [SYS_null] = {(syscall_t)sys_null, "null"},
+ [SYS_block] = {(syscall_t)sys_block, "block"},
+ [SYS_cache_buster] = {(syscall_t)sys_cache_buster, "buster"},
+ [SYS_cache_invalidate] = {(syscall_t)sys_cache_invalidate, "wbinv"},
+ [SYS_reboot] = {(syscall_t)reboot, "reboot!"},
+ [SYS_cputs] = {(syscall_t)sys_cputs, "cputs"},
+ [SYS_cgetc] = {(syscall_t)sys_cgetc, "cgetc"},
+ [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_mmap] = {(syscall_t)sys_mmap, "mmap"},
+ [SYS_munmap] = {(syscall_t)sys_munmap, "munmap"},
+ [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_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_SERIAL_IO__
+ [SYS_serial_read] = {(syscall_t)sys_serial_read, "ser_read"},
+ [SYS_serial_write] = {(syscall_t)sys_serial_write, "ser_write"},
+#endif
+#ifdef __CONFIG_NETWORKING__
+ [SYS_eth_read] = {(syscall_t)sys_eth_read, "eth_read"},
+ [SYS_eth_write] = {(syscall_t)sys_eth_write, "eth_write"},
+ [SYS_eth_get_mac_addr] = {(syscall_t)sys_eth_get_mac_addr, "get_mac"},
+ [SYS_eth_recv_check] = {(syscall_t)sys_eth_recv_check, "recv_check"},
+#endif
+#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"},
+ [SYS_close] = {(syscall_t)sys_close, "close"},
+ [SYS_fstat] = {(syscall_t)sys_fstat, "fstat"},
+ [SYS_stat] = {(syscall_t)sys_stat, "stat"},
+ [SYS_lstat] = {(syscall_t)sys_lstat, "lstat"},
+ [SYS_fcntl] = {(syscall_t)sys_fcntl, "fcntl"},
+ [SYS_access] = {(syscall_t)sys_access, "access"},
+ [SYS_umask] = {(syscall_t)sys_umask, "umask"},
+ [SYS_chmod] = {(syscall_t)sys_chmod, "chmod"},
+ [SYS_lseek] = {(syscall_t)sys_lseek, "lseek"},
+ [SYS_link] = {(syscall_t)sys_link, "link"},
+ [SYS_unlink] = {(syscall_t)sys_unlink, "unlink"},
+ [SYS_symlink] = {(syscall_t)sys_symlink, "symlink"},
+ [SYS_readlink] = {(syscall_t)sys_readlink, "readlink"},
+ [SYS_chdir] = {(syscall_t)sys_chdir, "chdir"},
+ [SYS_getcwd] = {(syscall_t)sys_getcwd, "getcwd"},
+ [SYS_mkdir] = {(syscall_t)sys_mkdir, "mkdri"},
+ [SYS_rmdir] = {(syscall_t)sys_rmdir, "rmdir"},
+ [SYS_gettimeofday] = {(syscall_t)sys_gettimeofday, "gettime"},
+ [SYS_tcgetattr] = {(syscall_t)sys_tcgetattr, "tcgetattr"},
+ [SYS_tcsetattr] = {(syscall_t)sys_tcsetattr, "tcsetattr"},
+ [SYS_setuid] = {(syscall_t)sys_setuid, "setuid"},
+ [SYS_setgid] = {(syscall_t)sys_setgid, "setgid"}
+};
+
/* Executes the given syscall.
*
* Note tf is passed in, which points to the tf of the context on the kernel
*
* This syscall function is used by both local syscall and arsc, and should
* remain oblivious of the caller. */
-intreg_t syscall(struct proc *p, uintreg_t syscallno, uintreg_t a1,
+intreg_t syscall(struct proc *p, uintreg_t sc_num, uintreg_t a0, uintreg_t a1,
uintreg_t a2, uintreg_t a3, uintreg_t a4, uintreg_t a5)
{
- /* Initialize the return value and error code returned to 0 */
- set_retval(ESUCCESS);
- set_errno(ESUCCESS);
-
- typedef intreg_t (*syscall_t)(struct proc*,uintreg_t,uintreg_t,
- uintreg_t,uintreg_t,uintreg_t);
-
- const static syscall_t syscall_table[] = {
- [SYS_null] = (syscall_t)sys_null,
- [SYS_cache_buster] = (syscall_t)sys_cache_buster,
- [SYS_cache_invalidate] = (syscall_t)sys_cache_invalidate,
- [SYS_reboot] = (syscall_t)reboot,
- [SYS_cputs] = (syscall_t)sys_cputs,
- [SYS_cgetc] = (syscall_t)sys_cgetc,
- [SYS_getcpuid] = (syscall_t)sys_getcpuid,
- [SYS_getvcoreid] = (syscall_t)sys_getvcoreid,
- [SYS_getpid] = (syscall_t)sys_getpid,
- [SYS_proc_create] = (syscall_t)sys_proc_create,
- [SYS_proc_run] = (syscall_t)sys_proc_run,
- [SYS_proc_destroy] = (syscall_t)sys_proc_destroy,
- [SYS_yield] = (syscall_t)sys_proc_yield,
- [SYS_fork] = (syscall_t)sys_fork,
- [SYS_exec] = (syscall_t)sys_exec,
- [SYS_trywait] = (syscall_t)sys_trywait,
- [SYS_mmap] = (syscall_t)sys_mmap,
- [SYS_munmap] = (syscall_t)sys_munmap,
- [SYS_mprotect] = (syscall_t)sys_mprotect,
- [SYS_shared_page_alloc] = (syscall_t)sys_shared_page_alloc,
- [SYS_shared_page_free] = (syscall_t)sys_shared_page_free,
- [SYS_resource_req] = (syscall_t)resource_req,
- [SYS_notify] = (syscall_t)sys_notify,
- [SYS_self_notify] = (syscall_t)sys_self_notify,
- [SYS_halt_core] = (syscall_t)sys_halt_core,
- #ifdef __CONFIG_SERIAL_IO__
- [SYS_serial_read] = (syscall_t)sys_serial_read,
- [SYS_serial_write] = (syscall_t)sys_serial_write,
- #endif
- #ifdef __CONFIG_NETWORKING__
- [SYS_eth_read] = (syscall_t)sys_eth_read,
- [SYS_eth_write] = (syscall_t)sys_eth_write,
- [SYS_eth_get_mac_addr] = (syscall_t)sys_eth_get_mac_addr,
- [SYS_eth_recv_check] = (syscall_t)sys_eth_recv_check,
- #endif
- #ifdef __CONFIG_ARSC_SERVER__
- [SYS_init_arsc] = (syscall_t)sys_init_arsc,
- #endif
- // Syscalls serviced by the appserver for now.
- [SYS_read] = (syscall_t)sys_read,
- [SYS_write] = (syscall_t)sys_write,
- [SYS_open] = (syscall_t)sys_open,
- [SYS_close] = (syscall_t)sys_close,
- [SYS_fstat] = (syscall_t)sys_fstat,
- [SYS_stat] = (syscall_t)sys_stat,
- [SYS_lstat] = (syscall_t)sys_lstat,
- [SYS_fcntl] = (syscall_t)sys_fcntl,
- [SYS_access] = (syscall_t)sys_access,
- [SYS_umask] = (syscall_t)sys_umask,
- [SYS_chmod] = (syscall_t)sys_chmod,
- [SYS_lseek] = (syscall_t)sys_lseek,
- [SYS_link] = (syscall_t)sys_link,
- [SYS_unlink] = (syscall_t)sys_unlink,
- [SYS_symlink] = (syscall_t)sys_symlink,
- [SYS_readlink] = (syscall_t)sys_readlink,
- [SYS_chdir] = (syscall_t)sys_chdir,
- [SYS_getcwd] = (syscall_t)sys_getcwd,
- [SYS_gettimeofday] = (syscall_t)sys_gettimeofday,
- [SYS_tcgetattr] = (syscall_t)sys_tcgetattr,
- [SYS_tcsetattr] = (syscall_t)sys_tcsetattr
- };
-
const int max_syscall = sizeof(syscall_table)/sizeof(syscall_table[0]);
uint32_t coreid, vcoreid;
if (systrace_flags & SYSTRACE_ON) {
if ((systrace_flags & SYSTRACE_ALLPROC) || (proc_is_traced(p))) {
coreid = core_id();
- vcoreid = proc_get_vcoreid(p, core_id());
+ vcoreid = proc_get_vcoreid(p);
if (systrace_flags & SYSTRACE_LOUD) {
- printk("[%16llu] Syscall %d for proc %d on core %d, vcore %d\n",
- read_tsc(), syscallno, p->pid, coreid, vcoreid);
+ printk("[%16llu] Syscall %3d (%12s):(%08p, %08p, %08p, %08p, "
+ "%08p, %08p) proc: %d core: %d vcore: %d\n", read_tsc(),
+ sc_num, syscall_table[sc_num].name, a0, a1, a2, a3,
+ 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 = syscallno;
+ trace->syscallno = sc_num;
+ trace->arg0 = a0;
+ trace->arg1 = a1;
+ trace->arg2 = a2;
+ trace->arg3 = a3;
+ trace->arg4 = a4;
+ trace->arg5 = a5;
trace->pid = p->pid;
trace->coreid = coreid;
trace->vcoreid = vcoreid;
}
}
}
- //printk("Incoming syscall on core: %d number: %d\n a1: %x\n "
- // " a2: %x\n a3: %x\n a4: %x\n a5: %x\n", core_id(),
- // syscallno, a1, a2, a3, a4, a5);
+ if (sc_num > max_syscall || syscall_table[sc_num].call == NULL)
+ panic("Invalid syscall number %d for proc %x!", sc_num, p);
- if(syscallno > max_syscall || syscall_table[syscallno] == NULL)
- panic("Invalid syscall number %d for proc %x!", syscallno, *p);
+ return syscall_table[sc_num].call(p, a0, a1, a2, a3, a4, a5);
+}
- return syscall_table[syscallno](p,a1,a2,a3,a4,a5);
+/* Execute the syscall on the local core */
+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 */
+ 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);
+ /* 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
+ * handle all the syscalls. a.k.a. "sys_debutante(needs, wants)". If there is
+ * at least one, it will run it directly. */
+void prep_syscalls(struct proc *p, struct syscall *sysc, unsigned int nr_syscs)
+{
+ int retval;
+ /* Careful with pcpui here, we could have migrated */
+ if (!nr_syscs)
+ return;
+ /* For all after the first call, send ourselves a KMSG (TODO). */
+ if (nr_syscs != 1)
+ warn("Only one supported (Debutante calls: %d)\n", nr_syscs);
+ /* Call the first one directly. (we already checked to make sure there is
+ * 1) */
+ run_local_syscall(sysc);
+}
+
+/* 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).
+ *
+ * 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;
+ /* 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);
+ }
+ }
}
/* Syscall tracing */
* timestamp and loop around. Careful of concurrent writes. */
for (int i = 0; i < systrace_bufsize; i++)
if (systrace_buffer[i].timestamp)
- printk("[%16llu] Syscall %d for proc %d on core %d, vcore %d\n",
+ printk("[%16llu] Syscall %3d (%12s):(%08p, %08p, %08p, %08p, %08p,"
+ "%08p) proc: %d core: %d vcore: %d\n",
systrace_buffer[i].timestamp,
systrace_buffer[i].syscallno,
+ syscall_table[systrace_buffer[i].syscallno].name,
+ systrace_buffer[i].arg0,
+ systrace_buffer[i].arg1,
+ systrace_buffer[i].arg2,
+ systrace_buffer[i].arg3,
+ systrace_buffer[i].arg4,
+ systrace_buffer[i].arg5,
systrace_buffer[i].pid,
systrace_buffer[i].coreid,
systrace_buffer[i].vcoreid);
void systrace_clear_buffer(void)
{
spin_lock_irqsave(&systrace_lock);
- memset(systrace_buffer, 0, sizeof(struct systrace_record)*MAX_NUM_TRACED);
+ memset(systrace_buffer, 0, sizeof(struct systrace_record) * MAX_SYSTRACES);
spin_unlock_irqsave(&systrace_lock);
}
-
-void set_retval(uint32_t retval)
-{
- struct per_cpu_info* coreinfo = &per_cpu_info[core_id()];
- *(coreinfo->cur_ret.returnloc) = retval;
-}
-void set_errno(uint32_t errno)
-{
- struct per_cpu_info* coreinfo = &per_cpu_info[core_id()];
- if (coreinfo && coreinfo->cur_ret.errno_loc)
- *(coreinfo->cur_ret.errno_loc) = errno;
-}