vthread: allow programs to have threads exit on halt.
[akaros.git] / user / vmm / include / vmm / sched.h
1 /* Copyright (c) 2016 Google Inc.
2  * Barret Rhoden <brho@cs.berkeley.edu>
3  * See LICENSE for details.
4  *
5  * 2LS for virtual machines */
6
7 #pragma once
8
9 #include <parlib/uthread.h>
10 #include <sys/queue.h>
11
12 __BEGIN_DECLS
13
14 /* Three types of threads.  Guests are actual guest VMs.  Controllers are
15  * threads that are paired to guests and handles their exits.  Guests and
16  * controllers are 1:1 (via *buddy).  Task threads are for the VMM itself, such
17  * as a console thread. */
18
19 #define VMM_THREAD_GUEST                1
20 #define VMM_THREAD_CTLR                 2
21 #define VMM_THREAD_TASK                 3
22
23 #define VMM_THR_STACKSIZE               8192
24
25 struct guest_thread;
26 struct ctlr_thread;
27 struct task_thread;
28
29 struct guest_thread {
30         struct uthread                          uthread;
31         struct ctlr_thread                      *buddy;
32         unsigned int                            gpc_id;
33         bool                                            halt_exit;
34         uth_mutex_t                                     halt_mtx;
35         uth_cond_var_t                          halt_cv;
36 };
37
38 struct ctlr_thread {
39         struct uthread                          uthread;
40         struct guest_thread                     *buddy;
41         size_t                                          stacksize;
42         void                                            *stacktop;
43 };
44
45 struct task_thread {
46         struct uthread                          uthread;
47         void                                            (*func)(void *);
48         void                                            *arg;
49         size_t                                          stacksize;
50         void                                            *stacktop;
51 };
52
53 struct virtual_machine;                 /* in vmm/vmm.h */
54 struct vmm_thread {
55         union {
56                 struct guest_thread             guest;
57                 struct ctlr_thread              ctlr;
58                 struct task_thread              task;
59         };
60         int                                                     type;
61         TAILQ_ENTRY(vmm_thread)         tq_next;
62         struct virtual_machine          *vm;
63 };
64
65 TAILQ_HEAD(vmm_thread_tq, vmm_thread);
66
67 extern int vmm_sched_period_usec;
68
69 /* Initialize a VMM for a virtual machine, which the caller fills out, except
70  * for gths.  This will set **gths in the struct virtual machine.  Do not free()
71  * the array.
72  *
73  * Set the parlib control variables (e.g. parlib_wants_to_be_mcp) before calling
74  * this initializer.
75  *
76  * Returns 0 on success, -1 o/w. */
77 int vmm_init(struct virtual_machine *vm, int flags);
78 /* Starts a guest thread/core. */
79 void start_guest_thread(struct guest_thread *gth);
80 /* Start and run a task thread. */
81 struct task_thread *vmm_run_task(struct virtual_machine *vm,
82                                  void (*func)(void *), void *arg);
83
84 int vthread_attr_init(struct virtual_machine *vm, int vmmflags);
85 int vthread_attr_kernel_init(struct virtual_machine *vm, int vmmflags);
86 int vthread_create(struct virtual_machine *vm, int guest, void *rip, void *arg);
87
88 __END_DECLS