summaryrefslogtreecommitdiff
path: root/sys/kern
diff options
context:
space:
mode:
Diffstat (limited to 'sys/kern')
-rw-r--r--sys/kern/init_main.c3
-rw-r--r--sys/kern/kern_exit.c7
-rw-r--r--sys/kern/kern_sched.c66
-rw-r--r--sys/kern/kern_spawn.c72
-rw-r--r--sys/kern/kern_subr.c21
-rw-r--r--sys/kern/kern_syscall.c1
6 files changed, 148 insertions, 22 deletions
diff --git a/sys/kern/init_main.c b/sys/kern/init_main.c
index 6b3e09b..5e351a8 100644
--- a/sys/kern/init_main.c
+++ b/sys/kern/init_main.c
@@ -52,6 +52,7 @@
#endif /* _INSTALL_MEDIA */
struct proc g_proc0;
+struct proc *g_init;
static void
copyright(void)
@@ -114,7 +115,7 @@ main(void)
memset(&g_proc0, 0, sizeof(g_proc0));
/* Startup pid 1 */
- spawn(&g_proc0, start_init, NULL, 0, NULL);
+ spawn(&g_proc0, start_init, NULL, 0, &g_init);
md_inton();
/* Load all early drivers */
diff --git a/sys/kern/kern_exit.c b/sys/kern/kern_exit.c
index 6b41cbd..9377eed 100644
--- a/sys/kern/kern_exit.c
+++ b/sys/kern/kern_exit.c
@@ -46,6 +46,7 @@
#define pr_error(...) pr_trace(__VA_ARGS__)
extern volatile size_t g_nthreads;
+extern struct proc g_init;
static void
unload_td(struct proc *td)
@@ -150,17 +151,17 @@ exit1(struct proc *td, int flags)
curtd = this_td();
curpid = curtd->pid;
+
td->flags |= PROC_EXITING;
parent = td->parent;
/* We have one less process in the system! */
atomic_dec_64(&g_nthreads);
- /* If we have any children, kill them too */
+ /* Reassign children to init */
if (td->nleaves > 0) {
TAILQ_FOREACH(procp, &td->leafq, leaf_link) {
- if (!ISSET(procp->flags, PROC_EXITING))
- exit1(procp, flags);
+ procp->parent = &g_init;
}
}
diff --git a/sys/kern/kern_sched.c b/sys/kern/kern_sched.c
index e259a2c..23a1ebb 100644
--- a/sys/kern/kern_sched.c
+++ b/sys/kern/kern_sched.c
@@ -78,13 +78,37 @@ sched_oneshot(bool now)
timer.oneshot_us(usec);
}
+/*
+ * Returns true if a processor is associated
+ * with a specific thread
+ *
+ * @ci: CPU that wants to take 'td'
+ * @td: Thread to check against
+ */
+static bool
+cpu_is_assoc(struct cpu_info *ci, struct proc *td)
+{
+ /*
+ * If we are not pinned, any processor is
+ * associated.
+ */
+ if (!ISSET(td->flags, PROC_PINNED)) {
+ return true;
+ }
+
+ return ci->id == td->affinity;
+}
+
struct proc *
sched_dequeue_td(void)
{
struct sched_queue *queue;
struct proc *td = NULL;
+ struct cpu_info *ci;
+ uint32_t ncpu = 0;
spinlock_acquire(&tdq_lock);
+ ci = this_cpu();
for (size_t i = 0; i < SCHED_NQUEUE; ++i) {
queue = &qlist[i];
@@ -104,6 +128,19 @@ sched_dequeue_td(void)
}
}
+ /*
+ * If we are on a multicore system and this isn't
+ * our process, don't take it. Some threads might
+ * be pinned to a specific processor.
+ */
+ ncpu = cpu_count();
+ while (!cpu_is_assoc(ci, td) && ncpu > 1) {
+ td = TAILQ_NEXT(td, link);
+ if (td == NULL) {
+ break;
+ }
+ }
+
if (td == NULL) {
continue;
}
@@ -249,6 +286,35 @@ sched_detach(struct proc *td)
spinlock_release(&tdq_lock);
}
+/*
+ * Pin a process to a specific processor
+ *
+ * @td: Process to pin
+ * @cpu: Logical processor ID to pin `td' to.
+ *
+ * XXX: 'cpu' is a machine independent value, representing
+ * CPU<n>
+ */
+void
+proc_pin(struct proc *td, affinity_t cpu)
+{
+ td->affinity = cpu;
+ td->flags |= PROC_PINNED;
+}
+
+/*
+ * Unpin a pinned process, allowing it to be
+ * picked up by any processor
+ *
+ * @td: Process to unpin
+ */
+void
+proc_unpin(struct proc *td)
+{
+ td->affinity = 0;
+ td->flags &= ~PROC_PINNED;
+}
+
void
sched_init(void)
{
diff --git a/sys/kern/kern_spawn.c b/sys/kern/kern_spawn.c
index 75ebaa7..b9551f3 100644
--- a/sys/kern/kern_spawn.c
+++ b/sys/kern/kern_spawn.c
@@ -28,6 +28,7 @@
*/
#include <sys/spawn.h>
+#include <sys/wait.h>
#include <sys/proc.h>
#include <sys/exec.h>
#include <sys/mman.h>
@@ -96,6 +97,35 @@ spawn_thunk(void)
__builtin_unreachable();
}
+pid_t
+waitpid(pid_t pid, int *wstatus, int options)
+{
+ struct proc *child, *td;
+ pid_t ret;
+
+ td = this_td();
+ child = get_child(td, pid);
+
+ if (child == NULL) {
+ return -1;
+ }
+
+ /* Wait for it to be done */
+ while (!ISSET(child->flags, PROC_ZOMB)) {
+ sched_yield();
+ }
+
+
+ /* Give back the status */
+ if (wstatus != NULL) {
+ copyout(&child->exit_status, wstatus, sizeof(*wstatus));
+ }
+
+ ret = child->pid;
+ proc_reap(child);
+ return ret;
+}
+
/*
* Spawn a new process
*
@@ -173,24 +203,6 @@ spawn(struct proc *cur, void(*func)(void), void *p, int flags, struct proc **new
signals_init(newproc);
sched_enqueue_td(newproc);
pid = newproc->pid;
-
- if (ISSET(flags, SPAWN_WAIT)) {
- cur->flags |= PROC_SLEEP;
-
- while (ISSET(cur->flags, PROC_SLEEP)) {
- sched_yield();
- }
- while (!ISSET(newproc->flags, PROC_ZOMB)) {
- sched_yield();
- }
-
- if (newproc->exit_status < 0) {
- pid = newproc->exit_status;
- }
-
- proc_reap(newproc);
- }
-
return pid;
}
@@ -208,6 +220,9 @@ get_child(struct proc *cur, pid_t pid)
struct proc *procp;
TAILQ_FOREACH(procp, &cur->leafq, leaf_link) {
+ if (procp == NULL) {
+ continue;
+ }
if (procp->pid == pid) {
return procp;
}
@@ -217,6 +232,27 @@ get_child(struct proc *cur, pid_t pid)
}
/*
+ * arg0: PID
+ * arg1: wstatus
+ * arg2: options
+ *
+ * Returns PID of terminated child, returns
+ * -1 on failure.
+ */
+scret_t
+sys_waitpid(struct syscall_args *scargs)
+{
+ pid_t pid;
+ int *u_wstatus;
+ int options;
+
+ pid = scargs->arg0;
+ u_wstatus = (void *)scargs->arg1;
+ options = scargs->arg2;
+ return waitpid(pid, u_wstatus, options);
+}
+
+/*
* arg0: The file /path/to/executable
* arg1: Argv
* arg2: Envp (TODO)
diff --git a/sys/kern/kern_subr.c b/sys/kern/kern_subr.c
index f437ec7..8a08f33 100644
--- a/sys/kern/kern_subr.c
+++ b/sys/kern/kern_subr.c
@@ -29,9 +29,12 @@
#include <sys/proc.h>
#include <sys/types.h>
+#include <sys/param.h>
#include <sys/errno.h>
+#include <sys/mman.h>
#include <sys/exec.h>
#include <sys/systm.h>
+#include <vm/vm.h>
#include <string.h>
/*
@@ -45,6 +48,8 @@ static bool
check_uaddr(const void *uaddr)
{
vaddr_t stack_start, stack_end;
+ struct mmap_lgdr *lp;
+ struct mmap_entry find, *res;
struct exec_prog exec;
struct proc *td;
uintptr_t addr;
@@ -61,6 +66,22 @@ check_uaddr(const void *uaddr)
if (addr >= stack_start && addr <= stack_end)
return true;
+ /* Try to grab the mmap ledger */
+ if ((lp = td->mlgdr) == NULL) {
+ return false;
+ }
+
+ /*
+ * Now give an attempt at looking through the
+ * mmap ledger. Perhaps this memory was allocated
+ * in the user heap?
+ */
+ find.va_start = ALIGN_DOWN(addr, DEFAULT_PAGESIZE);
+ res = RBT_FIND(lgdr_entries, &lp->hd, &find);
+ if (res != NULL) {
+ return true;
+ }
+
return false;
}
diff --git a/sys/kern/kern_syscall.c b/sys/kern/kern_syscall.c
index a28d2dd..cb7e1d2 100644
--- a/sys/kern/kern_syscall.c
+++ b/sys/kern/kern_syscall.c
@@ -59,6 +59,7 @@ scret_t(*g_sctab[])(struct syscall_args *) = {
sys_getppid, /* SYS_getppid */
sys_setuid, /* SYS_setuid */
sys_getuid, /* SYS_getuid */
+ sys_waitpid, /* SYS_waitpid */
};
const size_t MAX_SYSCALLS = NELEM(g_sctab);