ia64/xen-unstable

view xen/include/xen/sched.h @ 14196:9d36026b1b43

xen: Cleanups and bug fixes after the rcu_lock_domain patch.
Signed-off-by: Keir Fraser <keir@xensource.com>
author kfraser@localhost.localdomain
date Thu Mar 01 11:38:55 2007 +0000 (2007-03-01)
parents a5b5279d2ba3
children 0de2f7d8d89f
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 <xen/shared.h>
10 #include <public/xen.h>
11 #include <public/domctl.h>
12 #include <public/vcpu.h>
13 #include <xen/time.h>
14 #include <xen/timer.h>
15 #include <xen/grant_table.h>
16 #include <xen/rangeset.h>
17 #include <asm/domain.h>
18 #include <xen/xenoprof.h>
19 #include <xen/rcupdate.h>
20 #include <xen/irq.h>
22 #ifdef CONFIG_COMPAT
23 #include <compat/vcpu.h>
24 DEFINE_XEN_GUEST_HANDLE(vcpu_runstate_info_compat_t);
25 #endif
27 extern unsigned long volatile jiffies;
29 /* A global pointer to the initial domain (DOM0). */
30 extern struct domain *dom0;
32 #ifndef CONFIG_COMPAT
33 #define MAX_EVTCHNS(d) NR_EVENT_CHANNELS
34 #else
35 #define MAX_EVTCHNS(d) (!IS_COMPAT(d) ? \
36 NR_EVENT_CHANNELS : \
37 sizeof(unsigned int) * sizeof(unsigned int) * 64)
38 #endif
39 #define EVTCHNS_PER_BUCKET 128
40 #define NR_EVTCHN_BUCKETS (NR_EVENT_CHANNELS / EVTCHNS_PER_BUCKET)
42 struct evtchn
43 {
44 #define ECS_FREE 0 /* Channel is available for use. */
45 #define ECS_RESERVED 1 /* Channel is reserved. */
46 #define ECS_UNBOUND 2 /* Channel is waiting to bind to a remote domain. */
47 #define ECS_INTERDOMAIN 3 /* Channel is bound to another domain. */
48 #define ECS_PIRQ 4 /* Channel is bound to a physical IRQ line. */
49 #define ECS_VIRQ 5 /* Channel is bound to a virtual IRQ line. */
50 #define ECS_IPI 6 /* Channel is bound to a virtual IPI line. */
51 u8 state; /* ECS_* */
52 u8 consumer_is_xen; /* Consumed by Xen or by guest? */
53 u16 notify_vcpu_id; /* VCPU for local delivery notification */
54 union {
55 struct {
56 domid_t remote_domid;
57 } unbound; /* state == ECS_UNBOUND */
58 struct {
59 u16 remote_port;
60 struct domain *remote_dom;
61 } interdomain; /* state == ECS_INTERDOMAIN */
62 u16 pirq; /* state == ECS_PIRQ */
63 u16 virq; /* state == ECS_VIRQ */
64 } u;
65 };
67 int evtchn_init(struct domain *d);
68 void evtchn_destroy(struct domain *d);
70 struct vcpu
71 {
72 int vcpu_id;
74 int processor;
76 vcpu_info_t *vcpu_info;
78 struct domain *domain;
80 struct vcpu *next_in_list;
82 struct timer timer; /* one-shot timer for timeout values */
83 unsigned long sleep_tick; /* tick at which this vcpu started sleep */
85 struct timer poll_timer; /* timeout for SCHEDOP_poll */
87 void *sched_priv; /* scheduler-specific data */
89 struct vcpu_runstate_info runstate;
90 #ifndef CONFIG_COMPAT
91 # define runstate_guest(v) ((v)->runstate_guest)
92 XEN_GUEST_HANDLE(vcpu_runstate_info_t) runstate_guest; /* guest address */
93 #else
94 # define runstate_guest(v) ((v)->runstate_guest.native)
95 union {
96 XEN_GUEST_HANDLE(vcpu_runstate_info_t) native;
97 XEN_GUEST_HANDLE(vcpu_runstate_info_compat_t) compat;
98 } runstate_guest; /* guest address */
99 #endif
101 unsigned long vcpu_flags;
103 spinlock_t pause_lock;
104 unsigned int pause_count;
106 u16 virq_to_evtchn[NR_VIRQS];
108 /* Bitmask of CPUs on which this VCPU may run. */
109 cpumask_t cpu_affinity;
111 unsigned long nmi_addr; /* NMI callback address. */
113 /* Bitmask of CPUs which are holding onto this VCPU's state. */
114 cpumask_t vcpu_dirty_cpumask;
116 struct arch_vcpu arch;
117 };
119 /* Per-domain lock can be recursively acquired in fault handlers. */
120 #define LOCK_BIGLOCK(_d) spin_lock_recursive(&(_d)->big_lock)
121 #define UNLOCK_BIGLOCK(_d) spin_unlock_recursive(&(_d)->big_lock)
123 struct domain
124 {
125 domid_t domain_id;
127 shared_info_t *shared_info; /* shared data area */
129 spinlock_t big_lock;
131 spinlock_t page_alloc_lock; /* protects all the following fields */
132 struct list_head page_list; /* linked list, of size tot_pages */
133 struct list_head xenpage_list; /* linked list, of size xenheap_pages */
134 unsigned int tot_pages; /* number of pages currently possesed */
135 unsigned int max_pages; /* maximum value for tot_pages */
136 unsigned int xenheap_pages; /* # pages allocated from Xen heap */
138 /* Scheduling. */
139 int shutdown_code; /* code value from OS (if DOMF_shutdown) */
140 void *sched_priv; /* scheduler-specific data */
142 struct domain *next_in_list;
143 struct domain *next_in_hashbucket;
145 struct list_head rangesets;
146 spinlock_t rangesets_lock;
148 /* Event channel information. */
149 struct evtchn *evtchn[NR_EVTCHN_BUCKETS];
150 spinlock_t evtchn_lock;
152 struct grant_table *grant_table;
154 /*
155 * Interrupt to event-channel mappings. Updates should be protected by the
156 * domain's event-channel spinlock. Read accesses can also synchronise on
157 * the lock, but races don't usually matter.
158 */
159 u16 pirq_to_evtchn[NR_IRQS];
160 DECLARE_BITMAP(pirq_mask, NR_IRQS);
162 /* I/O capabilities (access to IRQs and memory-mapped I/O). */
163 struct rangeset *iomem_caps;
164 struct rangeset *irq_caps;
166 unsigned long domain_flags;
168 /* Boolean: Is this an HVM guest? */
169 char is_hvm;
171 /* Boolean: Is this guest fully privileged (aka dom0)? */
172 char is_privileged;
174 spinlock_t pause_lock;
175 unsigned int pause_count;
177 unsigned long vm_assist;
179 atomic_t refcnt;
181 struct vcpu *vcpu[MAX_VIRT_CPUS];
183 /* Bitmask of CPUs which are holding onto this domain's state. */
184 cpumask_t domain_dirty_cpumask;
186 struct arch_domain arch;
188 void *ssid; /* sHype security subject identifier */
190 /* Control-plane tools handle for this domain. */
191 xen_domain_handle_t handle;
193 /* OProfile support. */
194 struct xenoprof *xenoprof;
195 int32_t time_offset_seconds;
197 struct rcu_head rcu;
198 };
200 struct domain_setup_info
201 {
202 /* Initialised by caller. */
203 unsigned long image_addr;
204 unsigned long image_len;
205 /* Initialised by loader: Public. */
206 unsigned long v_start;
207 unsigned long v_end;
208 unsigned long v_kernstart;
209 unsigned long v_kernend;
210 unsigned long v_kernentry;
211 #define PAEKERN_no 0
212 #define PAEKERN_yes 1
213 #define PAEKERN_extended_cr3 2
214 #define PAEKERN_bimodal 3
215 unsigned int pae_kernel;
216 /* Initialised by loader: Private. */
217 unsigned long elf_paddr_offset;
218 unsigned int load_symtab;
219 unsigned long symtab_addr;
220 unsigned long symtab_len;
221 };
223 extern struct vcpu *idle_vcpu[NR_CPUS];
224 #define IDLE_DOMAIN_ID (0x7FFFU)
225 #define is_idle_domain(d) ((d)->domain_id == IDLE_DOMAIN_ID)
226 #define is_idle_vcpu(v) (is_idle_domain((v)->domain))
228 #define DOMAIN_DESTROYED (1<<31) /* assumes atomic_t is >= 32 bits */
229 #define put_domain(_d) \
230 if ( atomic_dec_and_test(&(_d)->refcnt) ) domain_destroy(_d)
232 /*
233 * Use this when you don't have an existing reference to @d. It returns
234 * FALSE if @d is being destroyed.
235 */
236 static always_inline int get_domain(struct domain *d)
237 {
238 atomic_t old, new, seen = d->refcnt;
239 do
240 {
241 old = seen;
242 if ( unlikely(_atomic_read(old) & DOMAIN_DESTROYED) )
243 return 0;
244 _atomic_set(new, _atomic_read(old) + 1);
245 seen = atomic_compareandswap(old, new, &d->refcnt);
246 }
247 while ( unlikely(_atomic_read(seen) != _atomic_read(old)) );
248 return 1;
249 }
251 /*
252 * Use this when you already have, or are borrowing, a reference to @d.
253 * In this case we know that @d cannot be destroyed under our feet.
254 */
255 static inline void get_knownalive_domain(struct domain *d)
256 {
257 atomic_inc(&d->refcnt);
258 ASSERT(!(atomic_read(&d->refcnt) & DOMAIN_DESTROYED));
259 }
261 /* Obtain a reference to the currently-running domain. */
262 static inline struct domain *get_current_domain(void)
263 {
264 struct domain *d = current->domain;
265 get_knownalive_domain(d);
266 return d;
267 }
269 struct domain *domain_create(domid_t domid, unsigned int domcr_flags);
270 /* DOMCRF_hvm: Create an HVM domain, as opposed to a PV domain. */
271 #define _DOMCRF_hvm 0
272 #define DOMCRF_hvm (1U<<_DOMCRF_hvm)
274 int construct_dom0(
275 struct domain *d,
276 unsigned long image_start, unsigned long image_len,
277 unsigned long initrd_start, unsigned long initrd_len,
278 char *cmdline);
280 /*
281 * rcu_lock_domain_by_id() is more efficient than get_domain_by_id().
282 * This is the preferred function if the returned domain reference
283 * is short lived, but it cannot be used if the domain reference needs
284 * to be kept beyond the current scope (e.g., across a softirq).
285 * The returned domain reference must be discarded using rcu_unlock_domain().
286 */
287 struct domain *rcu_lock_domain_by_id(domid_t dom);
289 /* Finish a RCU critical region started by rcu_lock_domain_by_id(). */
290 static inline void rcu_unlock_domain(struct domain *d)
291 {
292 rcu_read_unlock(&domlist_read_lock);
293 }
295 static inline struct domain *rcu_lock_current_domain(void)
296 {
297 rcu_read_lock(&domlist_read_lock);
298 return current->domain;
299 }
301 struct domain *get_domain_by_id(domid_t dom);
302 void domain_destroy(struct domain *d);
303 void domain_kill(struct domain *d);
304 void domain_shutdown(struct domain *d, u8 reason);
305 void domain_pause_for_debugger(void);
307 /*
308 * Mark specified domain as crashed. This function always returns, even if the
309 * caller is the specified domain. The domain is not synchronously descheduled
310 * from any processor.
311 */
312 void __domain_crash(struct domain *d);
313 #define domain_crash(d) do { \
314 printk("domain_crash called from %s:%d\n", __FILE__, __LINE__); \
315 __domain_crash(d); \
316 } while (0)
318 /*
319 * Mark current domain as crashed and synchronously deschedule from the local
320 * processor. This function never returns.
321 */
322 void __domain_crash_synchronous(void) __attribute__((noreturn));
323 #define domain_crash_synchronous() do { \
324 printk("domain_crash_sync called from %s:%d\n", __FILE__, __LINE__); \
325 __domain_crash_synchronous(); \
326 } while (0)
328 #define set_current_state(_s) do { current->state = (_s); } while (0)
329 void scheduler_init(void);
330 void schedulers_start(void);
331 int sched_init_vcpu(struct vcpu *v, unsigned int processor);
332 void sched_destroy_vcpu(struct vcpu *v);
333 int sched_init_domain(struct domain *d);
334 void sched_destroy_domain(struct domain *d);
335 long sched_adjust(struct domain *, struct xen_domctl_scheduler_op *);
336 int sched_id(void);
337 void vcpu_wake(struct vcpu *d);
338 void vcpu_sleep_nosync(struct vcpu *d);
339 void vcpu_sleep_sync(struct vcpu *d);
341 /*
342 * Force synchronisation of given VCPU's state. If it is currently descheduled,
343 * this call will ensure that all its state is committed to memory and that
344 * no CPU is using critical state (e.g., page tables) belonging to the VCPU.
345 */
346 void sync_vcpu_execstate(struct vcpu *v);
348 /*
349 * Called by the scheduler to switch to another VCPU. This function must
350 * call context_saved(@prev) when the local CPU is no longer running in
351 * @prev's context, and that context is saved to memory. Alternatively, if
352 * implementing lazy context switching, it suffices to ensure that invoking
353 * sync_vcpu_execstate() will switch and commit @prev's state.
354 */
355 void context_switch(
356 struct vcpu *prev,
357 struct vcpu *next);
359 /*
360 * As described above, context_switch() must call this function when the
361 * local CPU is no longer running in @prev's context, and @prev's context is
362 * saved to memory. Alternatively, if implementing lazy context switching,
363 * ensure that invoking sync_vcpu_execstate() will switch and commit @prev.
364 */
365 void context_saved(struct vcpu *prev);
367 /* Called by the scheduler to continue running the current VCPU. */
368 void continue_running(
369 struct vcpu *same);
371 void startup_cpu_idle_loop(void);
373 /*
374 * Creates a continuation to resume the current hypercall. The caller should
375 * return immediately, propagating the value returned from this invocation.
376 * The format string specifies the types and number of hypercall arguments.
377 * It contains one character per argument as follows:
378 * 'i' [unsigned] {char, int}
379 * 'l' [unsigned] long
380 * 'h' guest handle (XEN_GUEST_HANDLE(foo))
381 */
382 unsigned long hypercall_create_continuation(
383 unsigned int op, const char *format, ...);
385 #define hypercall_preempt_check() (unlikely( \
386 softirq_pending(smp_processor_id()) | \
387 local_events_need_delivery() \
388 ))
390 /* Protect updates/reads (resp.) of domain_list and domain_hash. */
391 extern spinlock_t domlist_update_lock;
392 extern rcu_read_lock_t domlist_read_lock;
394 extern struct domain *domain_list;
396 /* Caller must hold the domlist_read_lock or domlist_update_lock. */
397 #define for_each_domain(_d) \
398 for ( (_d) = rcu_dereference(domain_list); \
399 (_d) != NULL; \
400 (_d) = rcu_dereference((_d)->next_in_list )) \
402 #define for_each_vcpu(_d,_v) \
403 for ( (_v) = (_d)->vcpu[0]; \
404 (_v) != NULL; \
405 (_v) = (_v)->next_in_list )
407 /*
408 * Per-VCPU flags (vcpu_flags).
409 */
410 /* Has the FPU been initialised? */
411 #define _VCPUF_fpu_initialised 0
412 #define VCPUF_fpu_initialised (1UL<<_VCPUF_fpu_initialised)
413 /* Has the FPU been used since it was last saved? */
414 #define _VCPUF_fpu_dirtied 1
415 #define VCPUF_fpu_dirtied (1UL<<_VCPUF_fpu_dirtied)
416 /* Domain is blocked waiting for an event. */
417 #define _VCPUF_blocked 2
418 #define VCPUF_blocked (1UL<<_VCPUF_blocked)
419 /* Currently running on a CPU? */
420 #define _VCPUF_running 3
421 #define VCPUF_running (1UL<<_VCPUF_running)
422 /* Initialization completed. */
423 #define _VCPUF_initialised 4
424 #define VCPUF_initialised (1UL<<_VCPUF_initialised)
425 /* VCPU is offline. */
426 #define _VCPUF_down 5
427 #define VCPUF_down (1UL<<_VCPUF_down)
428 /* NMI callback pending for this VCPU? */
429 #define _VCPUF_nmi_pending 8
430 #define VCPUF_nmi_pending (1UL<<_VCPUF_nmi_pending)
431 /* Avoid NMI reentry by allowing NMIs to be masked for short periods. */
432 #define _VCPUF_nmi_masked 9
433 #define VCPUF_nmi_masked (1UL<<_VCPUF_nmi_masked)
434 /* VCPU is polling a set of event channels (SCHEDOP_poll). */
435 #define _VCPUF_polling 10
436 #define VCPUF_polling (1UL<<_VCPUF_polling)
437 /* VCPU is paused by the hypervisor? */
438 #define _VCPUF_paused 11
439 #define VCPUF_paused (1UL<<_VCPUF_paused)
440 /* VCPU is blocked awaiting an event to be consumed by Xen. */
441 #define _VCPUF_blocked_in_xen 12
442 #define VCPUF_blocked_in_xen (1UL<<_VCPUF_blocked_in_xen)
443 /* VCPU affinity has changed: migrating to a new CPU. */
444 #define _VCPUF_migrating 13
445 #define VCPUF_migrating (1UL<<_VCPUF_migrating)
447 /*
448 * Per-domain flags (domain_flags).
449 */
450 /* Guest shut itself down for some reason. */
451 #define _DOMF_shutdown 0
452 #define DOMF_shutdown (1UL<<_DOMF_shutdown)
453 /* Death rattle. */
454 #define _DOMF_dying 1
455 #define DOMF_dying (1UL<<_DOMF_dying)
456 /* Domain is paused by controller software. */
457 #define _DOMF_ctrl_pause 2
458 #define DOMF_ctrl_pause (1UL<<_DOMF_ctrl_pause)
459 /* Domain is being debugged by controller software. */
460 #define _DOMF_debugging 3
461 #define DOMF_debugging (1UL<<_DOMF_debugging)
462 /* Are any VCPUs polling event channels (SCHEDOP_poll)? */
463 #define _DOMF_polling 4
464 #define DOMF_polling (1UL<<_DOMF_polling)
465 /* Domain is paused by the hypervisor? */
466 #define _DOMF_paused 5
467 #define DOMF_paused (1UL<<_DOMF_paused)
468 /* Domain is a compatibility one? */
469 #define _DOMF_compat 6
470 #define DOMF_compat (1UL<<_DOMF_compat)
472 static inline int vcpu_runnable(struct vcpu *v)
473 {
474 return ( !(v->vcpu_flags &
475 ( VCPUF_blocked |
476 VCPUF_down |
477 VCPUF_paused |
478 VCPUF_blocked_in_xen |
479 VCPUF_migrating )) &&
480 !(v->domain->domain_flags &
481 ( DOMF_shutdown |
482 DOMF_ctrl_pause |
483 DOMF_paused )));
484 }
486 void vcpu_pause(struct vcpu *v);
487 void vcpu_pause_nosync(struct vcpu *v);
488 void domain_pause(struct domain *d);
489 void vcpu_unpause(struct vcpu *v);
490 void domain_unpause(struct domain *d);
491 void domain_pause_by_systemcontroller(struct domain *d);
492 void domain_unpause_by_systemcontroller(struct domain *d);
493 void cpu_init(void);
495 int vcpu_set_affinity(struct vcpu *v, cpumask_t *affinity);
497 void vcpu_runstate_get(struct vcpu *v, struct vcpu_runstate_info *runstate);
499 static inline void vcpu_unblock(struct vcpu *v)
500 {
501 if ( test_and_clear_bit(_VCPUF_blocked, &v->vcpu_flags) )
502 vcpu_wake(v);
503 }
505 #define IS_PRIV(_d) ((_d)->is_privileged)
507 #ifdef CONFIG_COMPAT
508 #define IS_COMPAT(_d) \
509 (test_bit(_DOMF_compat, &(_d)->domain_flags))
510 #else
511 #define IS_COMPAT(_d) 0
512 #endif
514 #define VM_ASSIST(_d,_t) (test_bit((_t), &(_d)->vm_assist))
516 #define is_hvm_domain(d) ((d)->is_hvm)
517 #define is_hvm_vcpu(v) (is_hvm_domain(v->domain))
519 #endif /* __SCHED_H__ */
521 /*
522 * Local variables:
523 * mode: C
524 * c-set-style: "BSD"
525 * c-basic-offset: 4
526 * tab-width: 4
527 * indent-tabs-mode: nil
528 * End:
529 */