* All things processes! As we move away from the old envs to processes,
* we'll move things into here that are designed for multicore processes. */
-#ifndef ROS_KERN_PROCESS_H
-#define ROS_KERN_PROCESS_H
+#pragma once
#include <ros/common.h>
#include <ros/event.h>
#include <trap.h>
#include <atomic.h>
#include <kref.h>
+#include <schedule.h>
/* Process States. Not 100% on the names yet. RUNNABLE_* are waiting to go to
* RUNNING_*. For instance, RUNNABLE_M is expecting to go to RUNNING_M. It
* could be waiting for it's timeslice, or possibly for all the cores it asked
- * for. You use proc_run() to transition between these states.
+ * for.
*
* Difference between the _M and the _S states:
* - _S : legacy process mode
* - The time slicing is at a coarser granularity for _M states. This means
* that when you run an _S on a core, it should be interrupted/time sliced
* more often, which also means the core should be classified differently for
- * a while. Possibly even using it's local APIC timer.
+ * a while. Possibly even using its local APIC timer.
* - A process in an _M state will be informed about changes to its state, e.g.,
* will have a handler run in the event of a page fault
+ *
+ * DYING vs. DYING_ABORT:
+ * - DYING is the initial stage when a process is dying, but before all of its
+ * syscalls should abort. At this point, we start closing FDs and blocking
+ * certain new operations.
+ * - DYING_ABORT is after all FDs were closed and all outstanding syscalls are
+ * aborted.
*/
#define PROC_CREATED 0x01
#define PROC_RUNNING_S 0x04
#define PROC_WAITING 0x08 // can split out to INT and UINT
#define PROC_DYING 0x10
-#define PROC_RUNNABLE_M 0x20
-#define PROC_RUNNING_M 0x40
-
-#define procstate2str(state) ((state)==PROC_CREATED ? "CREATED" : \
- (state)==PROC_RUNNABLE_S ? "RUNNABLE_S" : \
- (state)==PROC_RUNNING_S ? "RUNNING_S" : \
- (state)==PROC_WAITING ? "WAITING" : \
- (state)==PROC_DYING ? "DYING" : \
- (state)==PROC_RUNNABLE_M ? "RUNNABLE_M" : \
- (state)==PROC_RUNNING_M ? "RUNNING_M" : \
- "UNKNOWN")
+#define PROC_DYING_ABORT 0x20
+#define PROC_RUNNABLE_M 0x40
+#define PROC_RUNNING_M 0x80
+
+#define procstate2str(state) ((state) == PROC_CREATED ? "CREATED" : \
+ (state) == PROC_RUNNABLE_S ? "RUNNABLE_S" : \
+ (state) == PROC_RUNNING_S ? "RUNNING_S" : \
+ (state) == PROC_WAITING ? "WAITING" : \
+ (state) == PROC_DYING ? "DYING" : \
+ (state) == PROC_DYING_ABORT ? "DYING_ABORT" : \
+ (state) == PROC_RUNNABLE_M ? "RUNNABLE_M" : \
+ (state) == PROC_RUNNING_M ? "RUNNING_M" : \
+ "UNKNOWN")
+
+#define DEFAULT_PROGNAME ""
#include <env.h>
-TAILQ_HEAD(proc_list, proc); // Declares 'struct proc_list'
+static bool proc_is_dying(struct proc *p)
+{
+ return (p->state == PROC_DYING) || (p->state == PROC_DYING_ABORT);
+}
-extern spinlock_t runnablelist_lock;
-extern struct proc_list LCKD(&runnablelist_lock) proc_runnablelist;
+struct process_set {
+ size_t num_processes;
+ size_t size;
+ struct proc **procs;
+};
/* Can use a htable iterator to iterate through all active procs */
extern struct hashtable *pid_hash;
extern spinlock_t pid_hash_lock;
-/* Idle cores: ones able to be exclusively given to a process (worker cores). */
-extern spinlock_t idle_lock; // never grab this before a proc_lock
-extern uint32_t LCKD(&idle_lock) (RO idlecoremap)[MAX_NUM_CPUS];
-extern uint32_t LCKD(&idle_lock) num_idlecores;
-
/* Initialization */
void proc_init(void);
+void proc_set_username(struct proc *p, char *name);
+void proc_inherit_parent_username(struct proc *child, struct proc *parent);
+void proc_set_progname(struct proc *p, char *name);
+void proc_replace_binary_path(struct proc *p, char *path);
+void proc_init_procinfo(struct proc* p);
+void proc_init_procdata(struct proc* p);
/* Process management: */
-error_t proc_alloc(struct proc **pp, struct proc *parent);
+struct proc *pid_nth(unsigned int n);
+error_t proc_alloc(struct proc **pp, struct proc *parent, int flags);
void __proc_ready(struct proc *p);
struct proc *proc_create(struct file *prog, char **argv, char **envp);
-int __proc_set_state(struct proc *p, uint32_t state) WRITES(p->state);
+int __proc_set_state(struct proc *p, uint32_t state);
struct proc *pid2proc(pid_t pid);
-bool proc_controls(struct proc *SAFE actor, struct proc *SAFE target);
+bool proc_controls(struct proc *actor, struct proc *target);
void proc_incref(struct proc *p, unsigned int val);
void proc_decref(struct proc *p);
-void proc_run(struct proc *SAFE p);
+void proc_run_s(struct proc *p);
+void __proc_run_m(struct proc *p);
+void __proc_startcore(struct proc *p, struct user_context *ctx);
void proc_restartcore(void);
-void proc_destroy(struct proc *SAFE p);
-void __proc_yield_s(struct proc *p, struct trapframe *tf);
-void proc_yield(struct proc *SAFE p, bool being_nice);
+void proc_destroy(struct proc *p);
+void proc_signal_parent(struct proc *child);
+int __proc_disown_child(struct proc *parent, struct proc *child);
+int proc_change_to_m(struct proc *p);
+void __proc_save_fpu_s(struct proc *p);
+void __proc_save_context_s(struct proc *p);
+void proc_yield(struct proc *p, bool being_nice);
void proc_notify(struct proc *p, uint32_t vcoreid);
+void proc_wakeup(struct proc *p);
+bool __proc_is_mcp(struct proc *p);
+bool proc_is_vcctx_ready(struct proc *p);
+int proc_change_to_vcore(struct proc *p, uint32_t new_vcoreid,
+ bool enable_my_notif);
+void proc_get_set(struct process_set *pset);
+void proc_free_set(struct process_set *pset);
/* Vcoremap info: */
-uint32_t proc_get_vcoreid(struct proc *SAFE p, uint32_t pcoreid);
+uint32_t proc_get_vcoreid(struct proc *p);
/* TODO: make all of these inline once we gut the Env crap */
bool vcore_is_mapped(struct proc *p, uint32_t vcoreid);
uint32_t vcore2vcoreid(struct proc *p, struct vcore *vc);
*
* These are internal functions. Error checking is to catch bugs, and you
* shouldn't call these functions with parameters you are not sure about (like
- * an invalid corelist).
- *
- * They also may cause an IPI to be sent to core it is called on. If so, the
- * return value will be true. Once you unlock (and enable interrupts) you will
- * be preempted, and usually lose your stack. There is a helper to unlock and
- * handle the refcnt.
+ * an invalid corelist).
*
* WARNING: YOU MUST HOLD THE PROC_LOCK BEFORE CALLING THESE! */
/* Gives process p the additional num cores listed in corelist */
-bool __proc_give_cores(struct proc *SAFE p, uint32_t *pcorelist, size_t num);
-/* Makes process p's coremap look like corelist (add, remove, etc). Not used */
-bool __proc_set_allcores(struct proc *SAFE p, uint32_t *pcorelist,
- size_t *num, amr_t message, TV(a0t) arg0,
- TV(a1t) arg1, TV(a2t) arg2);
-/* Takes from process p the num cores listed in corelist */
-bool __proc_take_cores(struct proc *p, uint32_t *pcorelist, size_t num,
- amr_t message, long arg0, long arg1, long arg2);
-bool __proc_take_allcores(struct proc *p, amr_t message, long arg0, long arg1,
- long arg2);
-void __proc_kmsg_pending(struct proc *p, bool ipi_pending);
-/* Exposed for kern/src/resource.c for now */
+int __proc_give_cores(struct proc *p, uint32_t *pc_arr, uint32_t num);
+/* Takes from process p the num cores listed in pc_arr */
+void __proc_take_corelist(struct proc *p, uint32_t *pc_arr, uint32_t num,
+ bool preempt);
+/* Takes all cores, returns the count, fills in pc_arr with their pcoreid */
+uint32_t __proc_take_allcores(struct proc *p, uint32_t *pc_arr, bool preempt);
+
+/* Exposed for now for convenience */
void __map_vcore(struct proc *p, uint32_t vcoreid, uint32_t pcoreid);
void __unmap_vcore(struct proc *p, uint32_t vcoreid);
+void vcore_account_online(struct proc *p, uint32_t vcoreid);
+void vcore_account_offline(struct proc *p, uint32_t vcoreid);
+uint64_t vcore_account_gettotal(struct proc *p, uint32_t vcoreid);
/* Preemption management. Some of these will change */
void __proc_preempt_warn(struct proc *p, uint32_t vcoreid, uint64_t when);
void __proc_preempt_warnall(struct proc *p, uint64_t when);
-bool __proc_preempt_core(struct proc *p, uint32_t pcoreid);
-bool __proc_preempt_all(struct proc *p);
-void proc_preempt_core(struct proc *p, uint32_t pcoreid, uint64_t usec);
+void __proc_preempt_core(struct proc *p, uint32_t pcoreid);
+uint32_t __proc_preempt_all(struct proc *p, uint32_t *pc_arr);
+bool proc_preempt_core(struct proc *p, uint32_t pcoreid, uint64_t usec);
void proc_preempt_all(struct proc *p, uint64_t usec);
/* Current / cr3 / context management */
-struct proc *switch_to(struct proc *new_p);
-void switch_back(struct proc *new_p, struct proc *old_proc);
+uintptr_t switch_to(struct proc *new_p);
+void switch_back(struct proc *new_p, uintptr_t old_ret);
void abandon_core(void);
-
-/* Hold the proc_lock, since it'll use the vcoremapping to send an unmapping
- * message for the region from start to end. */
-void __proc_tlbshootdown(struct proc *p, uintptr_t start, uintptr_t end);
+void clear_owning_proc(uint32_t coreid);
+void proc_tlbshootdown(struct proc *p, uintptr_t start, uintptr_t end);
/* Kernel message handlers for process management */
-void __startcore(struct trapframe *tf, uint32_t srcid, long a0, long a1,
- long a2);
-void __notify(struct trapframe *tf, uint32_t srcid, long a0, long a1, long a2);
-void __preempt(trapframe_t *tf, uint32_t srcid, long a0, long a1, long a2);
-void __death(struct trapframe *tf, uint32_t srcid, long a0, long a1, long a2);
-void __tlbshootdown(struct trapframe *tf, uint32_t srcid, long a0, long a1,
- long a2);
+void __startcore(uint32_t srcid, long a0, long a1, long a2);
+void __set_curctx(uint32_t srcid, long a0, long a1, long a2);
+void __notify(uint32_t srcid, long a0, long a1, long a2);
+void __preempt(uint32_t srcid, long a0, long a1, long a2);
+void __death(uint32_t srcid, long a0, long a1, long a2);
+void __tlbshootdown(uint32_t srcid, long a0, long a1, long a2);
/* Arch Specific */
-void proc_init_trapframe(trapframe_t *SAFE tf, uint32_t vcoreid,
- uintptr_t entryp, uintptr_t stack_top);
-void proc_secure_trapframe(struct trapframe *tf);
+void proc_pop_ctx(struct user_context *ctx) __attribute__((noreturn));
+void proc_init_ctx(struct user_context *ctx, uint32_t vcoreid, uintptr_t entryp,
+ uintptr_t stack_top, uintptr_t tls_desc);
+void proc_secure_ctx(struct user_context *ctx);
void __abandon_core(void);
+void __clear_owning_proc(uint32_t coreid);
/* Degubbing */
-void print_idlecoremap(void);
void print_allpids(void);
-void print_proc_info(pid_t pid);
-
-#endif /* !ROS_KERN_PROCESS_H */
+void print_proc_info(pid_t pid, int verbosity);