ia64/xen-unstable

view xen/common/domain.c @ 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 215b799fa181
line source
1 /******************************************************************************
2 * domain.c
3 *
4 * Generic domain-handling functions.
5 */
7 #include <xen/config.h>
8 #include <xen/compat.h>
9 #include <xen/init.h>
10 #include <xen/lib.h>
11 #include <xen/errno.h>
12 #include <xen/sched.h>
13 #include <xen/domain.h>
14 #include <xen/mm.h>
15 #include <xen/event.h>
16 #include <xen/time.h>
17 #include <xen/console.h>
18 #include <xen/softirq.h>
19 #include <xen/domain_page.h>
20 #include <xen/rangeset.h>
21 #include <xen/guest_access.h>
22 #include <xen/hypercall.h>
23 #include <xen/delay.h>
24 #include <xen/shutdown.h>
25 #include <xen/percpu.h>
26 #include <xen/multicall.h>
27 #include <xen/rcupdate.h>
28 #include <asm/debugger.h>
29 #include <public/sched.h>
30 #include <public/vcpu.h>
32 /* Protect updates/reads (resp.) of domain_list and domain_hash. */
33 DEFINE_SPINLOCK(domlist_update_lock);
34 DEFINE_RCU_READ_LOCK(domlist_read_lock);
36 #define DOMAIN_HASH_SIZE 256
37 #define DOMAIN_HASH(_id) ((int)(_id)&(DOMAIN_HASH_SIZE-1))
38 static struct domain *domain_hash[DOMAIN_HASH_SIZE];
39 struct domain *domain_list;
41 struct domain *dom0;
43 struct vcpu *idle_vcpu[NR_CPUS] __read_mostly;
45 int current_domain_id(void)
46 {
47 return current->domain->domain_id;
48 }
50 struct domain *alloc_domain(domid_t domid)
51 {
52 struct domain *d;
54 if ( (d = xmalloc(struct domain)) == NULL )
55 return NULL;
57 memset(d, 0, sizeof(*d));
58 d->domain_id = domid;
59 atomic_set(&d->refcnt, 1);
60 spin_lock_init(&d->big_lock);
61 spin_lock_init(&d->page_alloc_lock);
62 spin_lock_init(&d->pause_lock);
63 INIT_LIST_HEAD(&d->page_list);
64 INIT_LIST_HEAD(&d->xenpage_list);
66 return d;
67 }
69 void free_domain(struct domain *d)
70 {
71 struct vcpu *v;
72 int i;
74 for ( i = MAX_VIRT_CPUS-1; i >= 0; i-- )
75 {
76 if ( (v = d->vcpu[i]) == NULL )
77 continue;
78 vcpu_destroy(v);
79 sched_destroy_vcpu(v);
80 free_vcpu_struct(v);
81 }
83 sched_destroy_domain(d);
84 xfree(d);
85 }
87 struct vcpu *alloc_vcpu(
88 struct domain *d, unsigned int vcpu_id, unsigned int cpu_id)
89 {
90 struct vcpu *v;
92 BUG_ON(d->vcpu[vcpu_id] != NULL);
94 if ( (v = alloc_vcpu_struct()) == NULL )
95 return NULL;
97 v->domain = d;
98 v->vcpu_id = vcpu_id;
99 v->vcpu_info = shared_info_addr(d, vcpu_info[vcpu_id]);
100 spin_lock_init(&v->pause_lock);
102 v->runstate.state = is_idle_vcpu(v) ? RUNSTATE_running : RUNSTATE_offline;
103 v->runstate.state_entry_time = NOW();
105 if ( (vcpu_id != 0) && !is_idle_domain(d) )
106 set_bit(_VCPUF_down, &v->vcpu_flags);
108 if ( sched_init_vcpu(v, cpu_id) != 0 )
109 {
110 free_vcpu_struct(v);
111 return NULL;
112 }
114 if ( vcpu_initialise(v) != 0 )
115 {
116 sched_destroy_vcpu(v);
117 free_vcpu_struct(v);
118 return NULL;
119 }
121 d->vcpu[vcpu_id] = v;
122 if ( vcpu_id != 0 )
123 d->vcpu[v->vcpu_id-1]->next_in_list = v;
125 return v;
126 }
128 struct vcpu *alloc_idle_vcpu(unsigned int cpu_id)
129 {
130 struct domain *d;
131 struct vcpu *v;
132 unsigned int vcpu_id = cpu_id % MAX_VIRT_CPUS;
134 if ( (v = idle_vcpu[cpu_id]) != NULL )
135 return v;
137 d = (vcpu_id == 0) ?
138 domain_create(IDLE_DOMAIN_ID, 0) :
139 idle_vcpu[cpu_id - vcpu_id]->domain;
140 BUG_ON(d == NULL);
142 v = alloc_vcpu(d, vcpu_id, cpu_id);
143 idle_vcpu[cpu_id] = v;
145 return v;
146 }
148 struct domain *domain_create(domid_t domid, unsigned int domcr_flags)
149 {
150 struct domain *d, **pd;
152 if ( (d = alloc_domain(domid)) == NULL )
153 return NULL;
155 if ( domcr_flags & DOMCRF_hvm )
156 d->is_hvm = 1;
158 rangeset_domain_initialise(d);
160 if ( !is_idle_domain(d) )
161 {
162 set_bit(_DOMF_ctrl_pause, &d->domain_flags);
163 if ( evtchn_init(d) != 0 )
164 goto fail1;
165 if ( grant_table_create(d) != 0 )
166 goto fail2;
167 }
169 if ( arch_domain_create(d) != 0 )
170 goto fail3;
172 d->iomem_caps = rangeset_new(d, "I/O Memory", RANGESETF_prettyprint_hex);
173 d->irq_caps = rangeset_new(d, "Interrupts", 0);
174 if ( (d->iomem_caps == NULL) || (d->irq_caps == NULL) )
175 goto fail4;
177 if ( sched_init_domain(d) != 0 )
178 goto fail4;
180 if ( !is_idle_domain(d) )
181 {
182 spin_lock(&domlist_update_lock);
183 pd = &domain_list; /* NB. domain_list maintained in order of domid. */
184 for ( pd = &domain_list; *pd != NULL; pd = &(*pd)->next_in_list )
185 if ( (*pd)->domain_id > d->domain_id )
186 break;
187 d->next_in_list = *pd;
188 d->next_in_hashbucket = domain_hash[DOMAIN_HASH(domid)];
189 /* Two rcu assignments are not atomic
190 * Readers may see inconsistent domlist and hash table
191 * That is OK as long as each RCU reader-side critical section uses
192 * only one or them */
193 rcu_assign_pointer(*pd, d);
194 rcu_assign_pointer(domain_hash[DOMAIN_HASH(domid)], d);
195 spin_unlock(&domlist_update_lock);
196 }
198 return d;
200 fail4:
201 arch_domain_destroy(d);
202 fail3:
203 if ( !is_idle_domain(d) )
204 grant_table_destroy(d);
205 fail2:
206 if ( !is_idle_domain(d) )
207 evtchn_destroy(d);
208 fail1:
209 rangeset_domain_destroy(d);
210 free_domain(d);
211 return NULL;
212 }
215 struct domain *get_domain_by_id(domid_t dom)
216 {
217 struct domain *d;
219 rcu_read_lock(&domlist_read_lock);
221 for ( d = rcu_dereference(domain_hash[DOMAIN_HASH(dom)]);
222 d != NULL;
223 d = rcu_dereference(d->next_in_hashbucket) )
224 {
225 if ( d->domain_id == dom )
226 {
227 if ( unlikely(!get_domain(d)) )
228 d = NULL;
229 break;
230 }
231 }
233 rcu_read_unlock(&domlist_read_lock);
235 return d;
236 }
239 struct domain *rcu_lock_domain_by_id(domid_t dom)
240 {
241 struct domain *d;
243 rcu_read_lock(&domlist_read_lock);
245 for ( d = rcu_dereference(domain_hash[DOMAIN_HASH(dom)]);
246 d != NULL;
247 d = rcu_dereference(d->next_in_hashbucket) )
248 {
249 if ( d->domain_id == dom )
250 return d;
251 }
253 rcu_read_unlock(&domlist_read_lock);
255 return NULL;
256 }
259 void domain_kill(struct domain *d)
260 {
261 domain_pause(d);
263 if ( test_and_set_bit(_DOMF_dying, &d->domain_flags) )
264 return;
266 gnttab_release_mappings(d);
267 domain_relinquish_resources(d);
268 put_domain(d);
270 send_guest_global_virq(dom0, VIRQ_DOM_EXC);
271 }
274 void __domain_crash(struct domain *d)
275 {
276 if ( test_bit(_DOMF_shutdown, &d->domain_flags) )
277 {
278 /* Print nothing: the domain is already shutting down. */
279 }
280 else if ( d == current->domain )
281 {
282 printk("Domain %d (vcpu#%d) crashed on cpu#%d:\n",
283 d->domain_id, current->vcpu_id, smp_processor_id());
284 show_execution_state(guest_cpu_user_regs());
285 }
286 else
287 {
288 printk("Domain %d reported crashed by domain %d on cpu#%d:\n",
289 d->domain_id, current->domain->domain_id, smp_processor_id());
290 }
292 domain_shutdown(d, SHUTDOWN_crash);
293 }
296 void __domain_crash_synchronous(void)
297 {
298 __domain_crash(current->domain);
300 /*
301 * Flush multicall state before dying if a multicall is in progress.
302 * This shouldn't be necessary, but some architectures are calling
303 * domain_crash_synchronous() when they really shouldn't (i.e., from
304 * within hypercall context).
305 */
306 if ( this_cpu(mc_state).flags != 0 )
307 {
308 dprintk(XENLOG_ERR,
309 "FIXME: synchronous domain crash during a multicall!\n");
310 this_cpu(mc_state).flags = 0;
311 }
313 for ( ; ; )
314 do_softirq();
315 }
318 void domain_shutdown(struct domain *d, u8 reason)
319 {
320 struct vcpu *v;
322 if ( d->domain_id == 0 )
323 dom0_shutdown(reason);
325 if ( !test_and_set_bit(_DOMF_shutdown, &d->domain_flags) )
326 d->shutdown_code = reason;
328 for_each_vcpu ( d, v )
329 vcpu_sleep_nosync(v);
331 send_guest_global_virq(dom0, VIRQ_DOM_EXC);
332 }
335 void domain_pause_for_debugger(void)
336 {
337 struct domain *d = current->domain;
338 struct vcpu *v;
340 set_bit(_DOMF_ctrl_pause, &d->domain_flags);
342 for_each_vcpu ( d, v )
343 vcpu_sleep_nosync(v);
345 send_guest_global_virq(dom0, VIRQ_DEBUGGER);
346 }
348 /* Complete domain destroy after RCU readers are not holding old references. */
349 static void complete_domain_destroy(struct rcu_head *head)
350 {
351 struct domain *d = container_of(head, struct domain, rcu);
353 rangeset_domain_destroy(d);
355 evtchn_destroy(d);
356 grant_table_destroy(d);
358 arch_domain_destroy(d);
360 free_domain(d);
362 send_guest_global_virq(dom0, VIRQ_DOM_EXC);
363 }
365 /* Release resources belonging to task @p. */
366 void domain_destroy(struct domain *d)
367 {
368 struct domain **pd;
369 atomic_t old, new;
371 BUG_ON(!test_bit(_DOMF_dying, &d->domain_flags));
373 /* May be already destroyed, or get_domain() can race us. */
374 _atomic_set(old, 0);
375 _atomic_set(new, DOMAIN_DESTROYED);
376 old = atomic_compareandswap(old, new, &d->refcnt);
377 if ( _atomic_read(old) != 0 )
378 return;
380 /* Delete from task list and task hashtable. */
381 spin_lock(&domlist_update_lock);
382 pd = &domain_list;
383 while ( *pd != d )
384 pd = &(*pd)->next_in_list;
385 rcu_assign_pointer(*pd, d->next_in_list);
386 pd = &domain_hash[DOMAIN_HASH(d->domain_id)];
387 while ( *pd != d )
388 pd = &(*pd)->next_in_hashbucket;
389 rcu_assign_pointer(*pd, d->next_in_hashbucket);
390 spin_unlock(&domlist_update_lock);
392 /* Schedule RCU asynchronous completion of domain destroy. */
393 call_rcu(&d->rcu, complete_domain_destroy);
394 }
396 static void vcpu_pause_setup(struct vcpu *v)
397 {
398 spin_lock(&v->pause_lock);
399 if ( v->pause_count++ == 0 )
400 set_bit(_VCPUF_paused, &v->vcpu_flags);
401 spin_unlock(&v->pause_lock);
402 }
404 void vcpu_pause(struct vcpu *v)
405 {
406 ASSERT(v != current);
407 vcpu_pause_setup(v);
408 vcpu_sleep_sync(v);
409 }
411 void vcpu_pause_nosync(struct vcpu *v)
412 {
413 vcpu_pause_setup(v);
414 vcpu_sleep_nosync(v);
415 }
417 void vcpu_unpause(struct vcpu *v)
418 {
419 int wake;
421 ASSERT(v != current);
423 spin_lock(&v->pause_lock);
424 wake = (--v->pause_count == 0);
425 if ( wake )
426 clear_bit(_VCPUF_paused, &v->vcpu_flags);
427 spin_unlock(&v->pause_lock);
429 if ( wake )
430 vcpu_wake(v);
431 }
433 void domain_pause(struct domain *d)
434 {
435 struct vcpu *v;
437 ASSERT(d != current->domain);
439 spin_lock(&d->pause_lock);
440 if ( d->pause_count++ == 0 )
441 set_bit(_DOMF_paused, &d->domain_flags);
442 spin_unlock(&d->pause_lock);
444 for_each_vcpu( d, v )
445 vcpu_sleep_sync(v);
446 }
448 void domain_unpause(struct domain *d)
449 {
450 struct vcpu *v;
451 int wake;
453 ASSERT(d != current->domain);
455 spin_lock(&d->pause_lock);
456 wake = (--d->pause_count == 0);
457 if ( wake )
458 clear_bit(_DOMF_paused, &d->domain_flags);
459 spin_unlock(&d->pause_lock);
461 if ( wake )
462 for_each_vcpu( d, v )
463 vcpu_wake(v);
464 }
466 void domain_pause_by_systemcontroller(struct domain *d)
467 {
468 struct vcpu *v;
470 BUG_ON(current->domain == d);
472 if ( !test_and_set_bit(_DOMF_ctrl_pause, &d->domain_flags) )
473 {
474 for_each_vcpu ( d, v )
475 vcpu_sleep_sync(v);
476 }
477 }
479 void domain_unpause_by_systemcontroller(struct domain *d)
480 {
481 struct vcpu *v;
483 if ( test_and_clear_bit(_DOMF_ctrl_pause, &d->domain_flags) )
484 {
485 for_each_vcpu ( d, v )
486 vcpu_wake(v);
487 }
488 }
490 int boot_vcpu(struct domain *d, int vcpuid, vcpu_guest_context_u ctxt)
491 {
492 struct vcpu *v = d->vcpu[vcpuid];
494 BUG_ON(test_bit(_VCPUF_initialised, &v->vcpu_flags));
496 return arch_set_info_guest(v, ctxt);
497 }
499 int vcpu_reset(struct vcpu *v)
500 {
501 struct domain *d = v->domain;
502 int rc;
504 domain_pause(d);
505 LOCK_BIGLOCK(d);
507 rc = arch_vcpu_reset(v);
508 if ( rc != 0 )
509 goto out;
511 set_bit(_VCPUF_down, &v->vcpu_flags);
513 clear_bit(_VCPUF_fpu_initialised, &v->vcpu_flags);
514 clear_bit(_VCPUF_fpu_dirtied, &v->vcpu_flags);
515 clear_bit(_VCPUF_blocked, &v->vcpu_flags);
516 clear_bit(_VCPUF_initialised, &v->vcpu_flags);
517 clear_bit(_VCPUF_nmi_pending, &v->vcpu_flags);
518 clear_bit(_VCPUF_nmi_masked, &v->vcpu_flags);
519 clear_bit(_VCPUF_polling, &v->vcpu_flags);
521 out:
522 UNLOCK_BIGLOCK(v->domain);
523 domain_unpause(d);
525 return rc;
526 }
529 long do_vcpu_op(int cmd, int vcpuid, XEN_GUEST_HANDLE(void) arg)
530 {
531 struct domain *d = current->domain;
532 struct vcpu *v;
533 struct vcpu_guest_context *ctxt;
534 long rc = 0;
536 if ( (vcpuid < 0) || (vcpuid >= MAX_VIRT_CPUS) )
537 return -EINVAL;
539 if ( (v = d->vcpu[vcpuid]) == NULL )
540 return -ENOENT;
542 switch ( cmd )
543 {
544 case VCPUOP_initialise:
545 if ( (ctxt = xmalloc(struct vcpu_guest_context)) == NULL )
546 {
547 rc = -ENOMEM;
548 break;
549 }
551 if ( copy_from_guest(ctxt, arg, 1) )
552 {
553 xfree(ctxt);
554 rc = -EFAULT;
555 break;
556 }
558 LOCK_BIGLOCK(d);
559 rc = -EEXIST;
560 if ( !test_bit(_VCPUF_initialised, &v->vcpu_flags) )
561 rc = boot_vcpu(d, vcpuid, ctxt);
562 UNLOCK_BIGLOCK(d);
564 xfree(ctxt);
565 break;
567 case VCPUOP_up:
568 if ( !test_bit(_VCPUF_initialised, &v->vcpu_flags) )
569 rc = -EINVAL;
570 else if ( test_and_clear_bit(_VCPUF_down, &v->vcpu_flags) )
571 vcpu_wake(v);
572 break;
574 case VCPUOP_down:
575 if ( !test_and_set_bit(_VCPUF_down, &v->vcpu_flags) )
576 vcpu_sleep_nosync(v);
577 break;
579 case VCPUOP_is_up:
580 rc = !test_bit(_VCPUF_down, &v->vcpu_flags);
581 break;
583 case VCPUOP_get_runstate_info:
584 {
585 struct vcpu_runstate_info runstate;
586 vcpu_runstate_get(v, &runstate);
587 if ( copy_to_guest(arg, &runstate, 1) )
588 rc = -EFAULT;
589 break;
590 }
592 default:
593 rc = arch_do_vcpu_op(cmd, v, arg);
594 break;
595 }
597 return rc;
598 }
600 long vm_assist(struct domain *p, unsigned int cmd, unsigned int type)
601 {
602 if ( type > MAX_VMASST_TYPE )
603 return -EINVAL;
605 switch ( cmd )
606 {
607 case VMASST_CMD_enable:
608 set_bit(type, &p->vm_assist);
609 return 0;
610 case VMASST_CMD_disable:
611 clear_bit(type, &p->vm_assist);
612 return 0;
613 }
615 return -ENOSYS;
616 }
618 /*
619 * Local variables:
620 * mode: C
621 * c-set-style: "BSD"
622 * c-basic-offset: 4
623 * tab-width: 4
624 * indent-tabs-mode: nil
625 * End:
626 */