vmm: Added more syscalls and helpers to linuxemu
[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         uth_mutex_t                                     *halt_mtx;
35         uth_cond_var_t                          *halt_cv;
36         unsigned long                           nr_vmexits;
37         struct vmm_gpcore_init          gpci;
38         void                                            *user_data;
39 };
40
41 struct ctlr_thread {
42         struct uthread                          uthread;
43         struct guest_thread                     *buddy;
44         size_t                                          stacksize;
45         void                                            *stacktop;
46 };
47
48 struct task_thread {
49         struct uthread                          uthread;
50         void                                            *(*func)(void *);
51         void                                            *arg;
52         size_t                                          stacksize;
53         void                                            *stacktop;
54 };
55
56 struct virtual_machine;                 /* in vmm/vmm.h */
57 struct vmm_thread {
58         union {
59                 struct guest_thread             guest;
60                 struct ctlr_thread              ctlr;
61                 struct task_thread              task;
62         };
63         int                                                     type;
64         TAILQ_ENTRY(vmm_thread)         tq_next;
65         struct virtual_machine          *vm;
66         /* Sched stats */
67         int                                                     prev_vcoreid;
68         unsigned long                           nr_runs;
69         unsigned long                           nr_resched;
70 };
71
72 TAILQ_HEAD(vmm_thread_tq, vmm_thread);
73
74 extern int vmm_sched_period_usec;
75
76 /* Initialize a VMM for a virtual machine, which the caller fills out, except
77  * for gths.  This will set **gths in the struct virtual machine.  Do not free()
78  * the array.
79  *
80  * Set the parlib control variables (e.g. parlib_wants_to_be_mcp) before calling
81  * this initializer.
82  *
83  * Returns 0 on success, -1 o/w. */
84 int vmm_init(struct virtual_machine *vm, struct vmm_gpcore_init *gpcis,
85              int flags);
86 /* Starts a guest thread/core. */
87 void start_guest_thread(struct guest_thread *gth);
88 /* Start and run a task thread. */
89 struct task_thread *vmm_run_task(struct virtual_machine *vm,
90                                  void *(*func)(void *), void *arg);
91
92 __END_DECLS