#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__
atomic_and(&sysc->flags, ~SC_K_LOCK);
}
-/* 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 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,
// 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 **************/
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)) {
- proc_decref(target);
- retval = -EPERM;
+ set_errno(EPERM);
+ goto out_error;
} else if (target->state != PROC_CREATED) {
- proc_decref(target);
- retval = -EINVAL;
- } else {
- __proc_set_state(target, PROC_RUNNABLE_S);
- schedule_proc(target);
+ set_errno(EINVAL);
+ goto out_error;
}
- spin_unlock(&p->proc_lock);
+ /* 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 retval;
+ 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)
{
proc_destroy(p_to_die);
/* we only get here if we weren't the one to die */
proc_decref(p_to_die);
- return ESUCCESS;
+ 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_current_sysc(0);
+ 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);
proc_decref(p);
- return 0;
+ /* 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. */
- finish_current_sysc(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;
+ /* 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. */
env->procdata->ldt = e->procdata->ldt;
#endif
- /* 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)) {
- proc_destroy(env); /* this is prob what you want, not decref by 2 */
- set_errno(ENOMEM);
- return -1;
- }
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);
env_user_mem_free(p, 0, UMAPTOP);
if (load_elf(p, program)) {
kref_put(&program->f_kref);
- /* Need an edible reference for proc_destroy in case it doesn't return.
- * sys_exec was given current's ref (counted once just for current) */
- proc_incref(p, 1);
+ /* Note this is an inedible reference, but proc_destroy now returns */
proc_destroy(p);
- proc_decref(p);
/* 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;
* error value (errno is already set). */
kref_put(&program->f_kref);
early_error:
- p->env_tf = *old_cur_tf;
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)
- proc_decref(p);
- proc_decref(p);
- 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;
- finish_current_sysc(0);
- /* this might not return (if it's a _S -> _M transition) */
- proc_incref(p, 1);
- retval = resource_req(p, type, amt_wanted, amt_wanted_min, flags);
- proc_decref(p);
- 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)) {
set_errno(EINVAL);
return -1;
}
+ } else {
+ local_msg.ev_type = ev_type;
}
send_kernel_event(target, &local_msg, 0);
proc_decref(target);
/* 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;
}
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;
+}
+
/************** Platform Specific Syscalls **************/
//Read a buffer over the serial port
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(),
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 */
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();
+ 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));