vmm: Moves gpci into guest_thread
[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 <ros/arch/vmm.h>
11 #include <sys/queue.h>
12
13 __BEGIN_DECLS
14
15 /* Three types of threads.  Guests are actual guest VMs.  Controllers are
16  * threads that are paired to guests and handles their exits.  Guests and
17  * controllers are 1:1 (via *buddy).  Task threads are for the VMM itself, such
18  * as a console thread. */
19
20 #define VMM_THREAD_GUEST                1
21 #define VMM_THREAD_CTLR                 2
22 #define VMM_THREAD_TASK                 3
23
24 #define VMM_THR_STACKSIZE               (4 * PGSIZE)
25
26 struct guest_thread;
27 struct ctlr_thread;
28 struct task_thread;
29
30 struct guest_thread {
31         struct uthread                          uthread;
32         struct ctlr_thread                      *buddy;
33         unsigned int                            gpc_id;
34         bool                                            halt_exit;
35         uth_mutex_t                                     *halt_mtx;
36         uth_cond_var_t                          *halt_cv;
37         unsigned long                           nr_vmexits;
38         struct vmm_gpcore_init          gpci;
39         void                                            *user_data;
40         // TODO: work out a real ops strategy.
41         bool (*vmcall)(struct guest_thread *gth, struct vm_trapframe *);
42 };
43
44 struct ctlr_thread {
45         struct uthread                          uthread;
46         struct guest_thread                     *buddy;
47         size_t                                          stacksize;
48         void                                            *stacktop;
49 };
50
51 struct task_thread {
52         struct uthread                          uthread;
53         void                                            *(*func)(void *);
54         void                                            *arg;
55         size_t                                          stacksize;
56         void                                            *stacktop;
57 };
58
59 struct virtual_machine;                 /* in vmm/vmm.h */
60 struct vmm_thread {
61         union {
62                 struct guest_thread             guest;
63                 struct ctlr_thread              ctlr;
64                 struct task_thread              task;
65         };
66         int                                                     type;
67         TAILQ_ENTRY(vmm_thread)         tq_next;
68         struct virtual_machine          *vm;
69         /* Sched stats */
70         int                                                     prev_vcoreid;
71         unsigned long                           nr_runs;
72         unsigned long                           nr_resched;
73 };
74
75 TAILQ_HEAD(vmm_thread_tq, vmm_thread);
76
77 extern int vmm_sched_period_usec;
78
79 /* Initialize a VMM for a virtual machine, which the caller fills out, except
80  * for gths.  This will set **gths in the struct virtual machine.  Do not free()
81  * the array.
82  *
83  * Set the parlib control variables (e.g. parlib_wants_to_be_mcp) before calling
84  * this initializer.
85  *
86  * Returns 0 on success, -1 o/w. */
87 int vmm_init(struct virtual_machine *vm, struct vmm_gpcore_init *gpcis,
88              int flags);
89 /* Starts a guest thread/core. */
90 void start_guest_thread(struct guest_thread *gth);
91 /* Start and run a task thread. */
92 struct task_thread *vmm_run_task(struct virtual_machine *vm,
93                                  void *(*func)(void *), void *arg);
94
95 int vthread_attr_init(struct virtual_machine *vm, int vmmflags);
96 int vthread_create(struct virtual_machine *vm, int guest, void *rip, void *arg);
97
98 __END_DECLS