direct-io.hg

view xen/include/xen/sched.h @ 7357:d6e99066959a

Refactor domain/vcpu allocation to be more separated.
Signed-off-by: Keir Fraser <keir@xensource.com>
author kaf24@firebug.cl.cam.ac.uk
date Wed Oct 12 17:01:38 2005 +0100 (2005-10-12)
parents 52b9aca1916a
children bd3268de4145
line source
2 #ifndef __SCHED_H__
3 #define __SCHED_H__
5 #include <xen/config.h>
6 #include <xen/types.h>
7 #include <xen/spinlock.h>
8 #include <xen/smp.h>
9 #include <public/xen.h>
10 #include <public/dom0_ops.h>
11 #include <xen/time.h>
12 #include <xen/ac_timer.h>
13 #include <xen/grant_table.h>
14 #include <asm/domain.h>
16 extern unsigned long volatile jiffies;
17 extern rwlock_t domlist_lock;
19 /* A global pointer to the initial domain (DOM0). */
20 extern struct domain *dom0;
22 #define MAX_EVTCHNS 1024
23 #define EVTCHNS_PER_BUCKET 128
24 #define NR_EVTCHN_BUCKETS (MAX_EVTCHNS / EVTCHNS_PER_BUCKET)
26 struct evtchn
27 {
28 #define ECS_FREE 0 /* Channel is available for use. */
29 #define ECS_RESERVED 1 /* Channel is reserved. */
30 #define ECS_UNBOUND 2 /* Channel is waiting to bind to a remote domain. */
31 #define ECS_INTERDOMAIN 3 /* Channel is bound to another domain. */
32 #define ECS_PIRQ 4 /* Channel is bound to a physical IRQ line. */
33 #define ECS_VIRQ 5 /* Channel is bound to a virtual IRQ line. */
34 #define ECS_IPI 6 /* Channel is bound to a virtual IPI line. */
35 u16 state; /* ECS_* */
36 u16 notify_vcpu_id; /* VCPU for local delivery notification */
37 union {
38 struct {
39 domid_t remote_domid;
40 } unbound; /* state == ECS_UNBOUND */
41 struct {
42 u16 remote_port;
43 struct domain *remote_dom;
44 } interdomain; /* state == ECS_INTERDOMAIN */
45 u16 pirq; /* state == ECS_PIRQ */
46 u16 virq; /* state == ECS_VIRQ */
47 } u;
48 };
50 int evtchn_init(struct domain *d);
51 void evtchn_destroy(struct domain *d);
53 #define CPUMAP_RUNANYWHERE 0xFFFFFFFF
55 struct vcpu
56 {
57 int vcpu_id;
59 int processor;
61 vcpu_info_t *vcpu_info;
63 struct domain *domain;
65 struct vcpu *next_in_list;
67 struct ac_timer timer; /* one-shot timer for timeout values */
68 unsigned long sleep_tick; /* tick at which this vcpu started sleep */
70 s_time_t lastschd; /* time this domain was last scheduled */
71 s_time_t lastdeschd; /* time this domain was last descheduled */
72 s_time_t cpu_time; /* total CPU time received till now */
73 s_time_t wokenup; /* time domain got woken up */
74 void *sched_priv; /* scheduler-specific data */
76 unsigned long vcpu_flags;
78 u16 virq_to_evtchn[NR_VIRQS];
80 atomic_t pausecnt;
82 cpumap_t cpumap; /* which cpus this domain can run on */
84 struct arch_vcpu arch;
85 };
87 /* Per-domain lock can be recursively acquired in fault handlers. */
88 #define LOCK_BIGLOCK(_d) spin_lock_recursive(&(_d)->big_lock)
89 #define UNLOCK_BIGLOCK(_d) spin_unlock_recursive(&(_d)->big_lock)
91 struct domain
92 {
93 domid_t domain_id;
95 shared_info_t *shared_info; /* shared data area */
97 spinlock_t big_lock;
99 spinlock_t page_alloc_lock; /* protects all the following fields */
100 struct list_head page_list; /* linked list, of size tot_pages */
101 struct list_head xenpage_list; /* linked list, of size xenheap_pages */
102 unsigned int tot_pages; /* number of pages currently possesed */
103 unsigned int max_pages; /* maximum value for tot_pages */
104 unsigned int next_io_page; /* next io pfn to give to domain */
105 unsigned int xenheap_pages; /* # pages allocated from Xen heap */
107 /* Scheduling. */
108 int shutdown_code; /* code value from OS (if DOMF_shutdown) */
109 void *sched_priv; /* scheduler-specific data */
111 struct domain *next_in_list;
112 struct domain *next_in_hashbucket;
114 /* Event channel information. */
115 struct evtchn *evtchn[NR_EVTCHN_BUCKETS];
116 spinlock_t evtchn_lock;
118 grant_table_t *grant_table;
120 /*
121 * Interrupt to event-channel mappings. Updates should be protected by the
122 * domain's event-channel spinlock. Read accesses can also synchronise on
123 * the lock, but races don't usually matter.
124 */
125 #define NR_PIRQS 256 /* Put this somewhere sane! */
126 u16 pirq_to_evtchn[NR_PIRQS];
127 u32 pirq_mask[NR_PIRQS/32];
129 unsigned long domain_flags;
130 unsigned long vm_assist;
132 atomic_t refcnt;
134 struct vcpu *vcpu[MAX_VIRT_CPUS];
136 /* Bitmask of CPUs which are holding onto this domain's state. */
137 cpumask_t cpumask;
139 struct arch_domain arch;
141 void *ssid; /* sHype security subject identifier */
142 };
144 struct domain_setup_info
145 {
146 /* Initialised by caller. */
147 unsigned long image_addr;
148 unsigned long image_len;
149 /* Initialised by loader: Public. */
150 unsigned long v_start;
151 unsigned long v_end;
152 unsigned long v_kernstart;
153 unsigned long v_kernend;
154 unsigned long v_kernentry;
155 /* Initialised by loader: Private. */
156 unsigned int load_symtab;
157 unsigned long symtab_addr;
158 unsigned long symtab_len;
159 /* Indicate whether it's xen specific image */
160 char *xen_section_string;
161 };
163 extern struct domain idle0_domain;
164 extern struct vcpu idle0_vcpu;
166 extern struct vcpu *idle_task[NR_CPUS];
167 #define IDLE_DOMAIN_ID (0x7FFFU)
168 #define is_idle_task(_d) (test_bit(_DOMF_idle_domain, &(_d)->domain_flags))
170 struct vcpu *alloc_vcpu(
171 struct domain *d, unsigned int vcpu_id, unsigned int cpu_id);
173 struct domain *alloc_domain(void);
174 void free_domain(struct domain *d);
176 #define DOMAIN_DESTRUCTED (1<<31) /* assumes atomic_t is >= 32 bits */
177 #define put_domain(_d) \
178 if ( atomic_dec_and_test(&(_d)->refcnt) ) domain_destruct(_d)
180 /*
181 * Use this when you don't have an existing reference to @d. It returns
182 * FALSE if @d is being destructed.
183 */
184 static always_inline int get_domain(struct domain *d)
185 {
186 atomic_t old, new, seen = d->refcnt;
187 do
188 {
189 old = seen;
190 if ( unlikely(_atomic_read(old) & DOMAIN_DESTRUCTED) )
191 return 0;
192 _atomic_set(new, _atomic_read(old) + 1);
193 seen = atomic_compareandswap(old, new, &d->refcnt);
194 }
195 while ( unlikely(_atomic_read(seen) != _atomic_read(old)) );
196 return 1;
197 }
199 /*
200 * Use this when you already have, or are borrowing, a reference to @d.
201 * In this case we know that @d cannot be destructed under our feet.
202 */
203 static inline void get_knownalive_domain(struct domain *d)
204 {
205 atomic_inc(&d->refcnt);
206 ASSERT(!(atomic_read(&d->refcnt) & DOMAIN_DESTRUCTED));
207 }
209 extern struct domain *do_createdomain(
210 domid_t dom_id, unsigned int cpu);
211 extern int construct_dom0(
212 struct domain *d,
213 unsigned long image_start, unsigned long image_len,
214 unsigned long initrd_start, unsigned long initrd_len,
215 char *cmdline);
216 extern int set_info_guest(struct domain *d, dom0_setdomaininfo_t *);
218 struct domain *find_domain_by_id(domid_t dom);
219 extern void domain_destruct(struct domain *d);
220 extern void domain_kill(struct domain *d);
221 extern void domain_shutdown(u8 reason);
222 extern void domain_pause_for_debugger(void);
224 /*
225 * Mark current domain as crashed. This function returns: the domain is not
226 * synchronously descheduled from any processor.
227 */
228 extern void domain_crash(void);
230 /*
231 * Mark current domain as crashed and synchronously deschedule from the local
232 * processor. This function never returns.
233 */
234 extern void domain_crash_synchronous(void) __attribute__((noreturn));
236 void new_thread(struct vcpu *d,
237 unsigned long start_pc,
238 unsigned long start_stack,
239 unsigned long start_info);
241 #define set_current_state(_s) do { current->state = (_s); } while (0)
242 void scheduler_init(void);
243 void schedulers_start(void);
244 void sched_add_domain(struct vcpu *);
245 void sched_rem_domain(struct vcpu *);
246 long sched_ctl(struct sched_ctl_cmd *);
247 long sched_adjdom(struct sched_adjdom_cmd *);
248 int sched_id();
249 void vcpu_wake(struct vcpu *d);
250 void vcpu_sleep_nosync(struct vcpu *d);
251 void vcpu_sleep_sync(struct vcpu *d);
253 /*
254 * Force synchronisation of given VCPU's state. If it is currently descheduled,
255 * this call will ensure that all its state is committed to memory and that
256 * no CPU is using critical state (e.g., page tables) belonging to the VCPU.
257 */
258 extern void sync_vcpu_execstate(struct vcpu *v);
260 /*
261 * Called by the scheduler to switch to another VCPU. On entry, although
262 * VCPUF_running is no longer asserted for @prev, its context is still running
263 * on the local CPU and is not committed to memory. The local scheduler lock
264 * is therefore still held, and interrupts are disabled, because the local CPU
265 * is in an inconsistent state.
266 *
267 * The callee must ensure that the local CPU is no longer running in @prev's
268 * context, and that the context is saved to memory, before returning.
269 * Alternatively, if implementing lazy context switching, it suffices to ensure
270 * that invoking sync_vcpu_execstate() will switch and commit @prev's state.
271 */
272 extern void context_switch(
273 struct vcpu *prev,
274 struct vcpu *next);
276 /*
277 * On some architectures (notably x86) it is not possible to entirely load
278 * @next's context with interrupts disabled. These may implement a function to
279 * finalise loading the new context after interrupts are re-enabled. This
280 * function is not given @prev and is not permitted to access it.
281 */
282 extern void context_switch_finalise(
283 struct vcpu *next);
285 /* Called by the scheduler to continue running the current VCPU. */
286 extern void continue_running(
287 struct vcpu *same);
289 /* Is CPU 'cpu' idle right now? */
290 int idle_cpu(int cpu);
292 void startup_cpu_idle_loop(void);
294 unsigned long __hypercall_create_continuation(
295 unsigned int op, unsigned int nr_args, ...);
296 #define hypercall0_create_continuation(_op) \
297 __hypercall_create_continuation((_op), 0)
298 #define hypercall1_create_continuation(_op, _a1) \
299 __hypercall_create_continuation((_op), 1, \
300 (unsigned long)(_a1))
301 #define hypercall2_create_continuation(_op, _a1, _a2) \
302 __hypercall_create_continuation((_op), 2, \
303 (unsigned long)(_a1), (unsigned long)(_a2))
304 #define hypercall3_create_continuation(_op, _a1, _a2, _a3) \
305 __hypercall_create_continuation((_op), 3, \
306 (unsigned long)(_a1), (unsigned long)(_a2), (unsigned long)(_a3))
307 #define hypercall4_create_continuation(_op, _a1, _a2, _a3, _a4) \
308 __hypercall_create_continuation((_op), 4, \
309 (unsigned long)(_a1), (unsigned long)(_a2), (unsigned long)(_a3), \
310 (unsigned long)(_a4))
311 #define hypercall5_create_continuation(_op, _a1, _a2, _a3, _a4, _a5) \
312 __hypercall_create_continuation((_op), 5, \
313 (unsigned long)(_a1), (unsigned long)(_a2), (unsigned long)(_a3), \
314 (unsigned long)(_a4), (unsigned long)(_a5))
315 #define hypercall6_create_continuation(_op, _a1, _a2, _a3, _a4, _a5, _a6) \
316 __hypercall_create_continuation((_op), 6, \
317 (unsigned long)(_a1), (unsigned long)(_a2), (unsigned long)(_a3), \
318 (unsigned long)(_a4), (unsigned long)(_a5), (unsigned long)(_a6))
320 #define hypercall_preempt_check() (unlikely( \
321 softirq_pending(smp_processor_id()) | \
322 event_pending(current) \
323 ))
325 /* This domain_hash and domain_list are protected by the domlist_lock. */
326 #define DOMAIN_HASH_SIZE 256
327 #define DOMAIN_HASH(_id) ((int)(_id)&(DOMAIN_HASH_SIZE-1))
328 extern struct domain *domain_hash[DOMAIN_HASH_SIZE];
329 extern struct domain *domain_list;
331 #define for_each_domain(_d) \
332 for ( (_d) = domain_list; \
333 (_d) != NULL; \
334 (_d) = (_d)->next_in_list )
336 #define for_each_vcpu(_d,_v) \
337 for ( (_v) = (_d)->vcpu[0]; \
338 (_v) != NULL; \
339 (_v) = (_v)->next_in_list )
341 /*
342 * Per-VCPU flags (vcpu_flags).
343 */
344 /* Has the FPU been initialised? */
345 #define _VCPUF_fpu_initialised 0
346 #define VCPUF_fpu_initialised (1UL<<_VCPUF_fpu_initialised)
347 /* Has the FPU been used since it was last saved? */
348 #define _VCPUF_fpu_dirtied 1
349 #define VCPUF_fpu_dirtied (1UL<<_VCPUF_fpu_dirtied)
350 /* Domain is blocked waiting for an event. */
351 #define _VCPUF_blocked 2
352 #define VCPUF_blocked (1UL<<_VCPUF_blocked)
353 /* Currently running on a CPU? */
354 #define _VCPUF_running 3
355 #define VCPUF_running (1UL<<_VCPUF_running)
356 /* Disables auto-migration between CPUs. */
357 #define _VCPUF_cpu_pinned 4
358 #define VCPUF_cpu_pinned (1UL<<_VCPUF_cpu_pinned)
359 /* Domain migrated between CPUs. */
360 #define _VCPUF_cpu_migrated 5
361 #define VCPUF_cpu_migrated (1UL<<_VCPUF_cpu_migrated)
362 /* Initialization completed. */
363 #define _VCPUF_initialised 6
364 #define VCPUF_initialised (1UL<<_VCPUF_initialised)
365 /* VCPU is not-runnable */
366 #define _VCPUF_down 7
367 #define VCPUF_down (1UL<<_VCPUF_down)
369 /*
370 * Per-domain flags (domain_flags).
371 */
372 /* Is this one of the per-CPU idle domains? */
373 #define _DOMF_idle_domain 0
374 #define DOMF_idle_domain (1UL<<_DOMF_idle_domain)
375 /* Is this domain privileged? */
376 #define _DOMF_privileged 1
377 #define DOMF_privileged (1UL<<_DOMF_privileged)
378 /* May this domain do IO to physical devices? */
379 #define _DOMF_physdev_access 2
380 #define DOMF_physdev_access (1UL<<_DOMF_physdev_access)
381 /* Guest shut itself down for some reason. */
382 #define _DOMF_shutdown 3
383 #define DOMF_shutdown (1UL<<_DOMF_shutdown)
384 /* Guest is in process of shutting itself down (becomes DOMF_shutdown). */
385 #define _DOMF_shuttingdown 4
386 #define DOMF_shuttingdown (1UL<<_DOMF_shuttingdown)
387 /* Death rattle. */
388 #define _DOMF_dying 5
389 #define DOMF_dying (1UL<<_DOMF_dying)
390 /* Domain is paused by controller software. */
391 #define _DOMF_ctrl_pause 6
392 #define DOMF_ctrl_pause (1UL<<_DOMF_ctrl_pause)
394 static inline int domain_runnable(struct vcpu *v)
395 {
396 return ( (atomic_read(&v->pausecnt) == 0) &&
397 !(v->vcpu_flags & (VCPUF_blocked|VCPUF_down)) &&
398 !(v->domain->domain_flags &
399 (DOMF_shutdown|DOMF_shuttingdown|DOMF_ctrl_pause)) );
400 }
402 void vcpu_pause(struct vcpu *v);
403 void domain_pause(struct domain *d);
404 void vcpu_unpause(struct vcpu *v);
405 void domain_unpause(struct domain *d);
406 void domain_pause_by_systemcontroller(struct domain *d);
407 void domain_unpause_by_systemcontroller(struct domain *d);
408 void cpu_init(void);
410 static inline void vcpu_unblock(struct vcpu *v)
411 {
412 if ( test_and_clear_bit(_VCPUF_blocked, &v->vcpu_flags) )
413 vcpu_wake(v);
414 }
416 #define IS_PRIV(_d) \
417 (test_bit(_DOMF_privileged, &(_d)->domain_flags))
418 #define IS_CAPABLE_PHYSDEV(_d) \
419 (test_bit(_DOMF_physdev_access, &(_d)->domain_flags))
421 #define VM_ASSIST(_d,_t) (test_bit((_t), &(_d)->vm_assist))
423 #endif /* __SCHED_H__ */
425 /*
426 * Local variables:
427 * mode: C
428 * c-set-style: "BSD"
429 * c-basic-offset: 4
430 * tab-width: 4
431 * indent-tabs-mode: nil
432 * End:
433 */