ia64/xen-unstable

view xen/common/schedule.c @ 10570:8dc4af3f192c

[IA64] Implement and use DOM0_DOMAIN_STEUP.

DOM0_GETMEMLIST now reads ptes and use gpfn.
Domain builder reworked: calls DOMAIN_SETUP, setup start_info page.
SAL data are now in domain memory.
is_vti field added in domain.arch.
Many cleanups (indentation, static, warnings).

Signed-off-by: Tristan Gingold <tristan.gingold@bull.net>
author awilliam@xenbuild.aw
date Wed Jul 05 09:28:32 2006 -0600 (2006-07-05)
parents a31f3bff4f76
children 7d3df6492d70
line source
1 /****************************************************************************
2 * (C) 2002-2003 - Rolf Neugebauer - Intel Research Cambridge
3 * (C) 2002-2003 University of Cambridge
4 * (C) 2004 - Mark Williamson - Intel Research Cambridge
5 ****************************************************************************
6 *
7 * File: common/schedule.c
8 * Author: Rolf Neugebauer & Keir Fraser
9 * Updated for generic API by Mark Williamson
10 *
11 * Description: Generic CPU scheduling code
12 * implements support functionality for the Xen scheduler API.
13 *
14 */
16 #include <xen/config.h>
17 #include <xen/init.h>
18 #include <xen/lib.h>
19 #include <xen/sched.h>
20 #include <xen/domain.h>
21 #include <xen/delay.h>
22 #include <xen/event.h>
23 #include <xen/time.h>
24 #include <xen/timer.h>
25 #include <xen/perfc.h>
26 #include <xen/sched-if.h>
27 #include <xen/softirq.h>
28 #include <xen/trace.h>
29 #include <xen/mm.h>
30 #include <xen/guest_access.h>
31 #include <public/sched.h>
32 #include <public/sched_ctl.h>
34 extern void arch_getdomaininfo_ctxt(struct vcpu *,
35 struct vcpu_guest_context *);
36 /* opt_sched: scheduler - default to credit */
37 static char opt_sched[10] = "credit";
38 string_param("sched", opt_sched);
40 #define TIME_SLOP (s32)MICROSECS(50) /* allow time to slip a bit */
42 /* Various timer handlers. */
43 static void s_timer_fn(void *unused);
44 static void t_timer_fn(void *unused);
45 static void vcpu_timer_fn(void *data);
46 static void poll_timer_fn(void *data);
48 /* This is global for now so that private implementations can reach it */
49 struct schedule_data schedule_data[NR_CPUS];
51 extern struct scheduler sched_bvt_def;
52 extern struct scheduler sched_sedf_def;
53 extern struct scheduler sched_credit_def;
54 static struct scheduler *schedulers[] = {
55 &sched_bvt_def,
56 &sched_sedf_def,
57 &sched_credit_def,
58 NULL
59 };
61 static void __enter_scheduler(void);
63 static struct scheduler ops;
65 #define SCHED_OP(fn, ...) \
66 (( ops.fn != NULL ) ? ops.fn( __VA_ARGS__ ) \
67 : (typeof(ops.fn(__VA_ARGS__)))0 )
69 /* Per-CPU periodic timer sends an event to the currently-executing domain. */
70 static struct timer t_timer[NR_CPUS];
72 static inline void vcpu_runstate_change(
73 struct vcpu *v, int new_state, s_time_t new_entry_time)
74 {
75 ASSERT(v->runstate.state != new_state);
76 ASSERT(spin_is_locked(&schedule_data[v->processor].schedule_lock));
78 v->runstate.time[v->runstate.state] +=
79 new_entry_time - v->runstate.state_entry_time;
80 v->runstate.state_entry_time = new_entry_time;
81 v->runstate.state = new_state;
82 }
84 void vcpu_runstate_get(struct vcpu *v, struct vcpu_runstate_info *runstate)
85 {
86 if ( likely(v == current) )
87 {
88 /* Fast lock-free path. */
89 memcpy(runstate, &v->runstate, sizeof(*runstate));
90 ASSERT(runstate->state == RUNSTATE_running);
91 runstate->time[RUNSTATE_running] += NOW() - runstate->state_entry_time;
92 }
93 else
94 {
95 vcpu_schedule_lock_irq(v);
96 memcpy(runstate, &v->runstate, sizeof(*runstate));
97 runstate->time[runstate->state] += NOW() - runstate->state_entry_time;
98 vcpu_schedule_unlock_irq(v);
99 }
100 }
102 int sched_init_vcpu(struct vcpu *v)
103 {
104 /* Initialise the per-domain timers. */
105 init_timer(&v->timer, vcpu_timer_fn, v, v->processor);
106 init_timer(&v->poll_timer, poll_timer_fn, v, v->processor);
108 if ( is_idle_vcpu(v) )
109 {
110 schedule_data[v->processor].curr = v;
111 schedule_data[v->processor].idle = v;
112 set_bit(_VCPUF_running, &v->vcpu_flags);
113 }
115 TRACE_2D(TRC_SCHED_DOM_ADD, v->domain->domain_id, v->vcpu_id);
117 return SCHED_OP(init_vcpu, v);
118 }
120 void sched_destroy_domain(struct domain *d)
121 {
122 struct vcpu *v;
124 for_each_vcpu ( d, v )
125 {
126 kill_timer(&v->timer);
127 kill_timer(&v->poll_timer);
128 TRACE_2D(TRC_SCHED_DOM_REM, v->domain->domain_id, v->vcpu_id);
129 }
131 SCHED_OP(destroy_domain, d);
132 }
134 void vcpu_sleep_nosync(struct vcpu *v)
135 {
136 unsigned long flags;
138 vcpu_schedule_lock_irqsave(v, flags);
140 if ( likely(!vcpu_runnable(v)) )
141 {
142 if ( v->runstate.state == RUNSTATE_runnable )
143 vcpu_runstate_change(v, RUNSTATE_offline, NOW());
145 SCHED_OP(sleep, v);
146 }
148 vcpu_schedule_unlock_irqrestore(v, flags);
150 TRACE_2D(TRC_SCHED_SLEEP, v->domain->domain_id, v->vcpu_id);
151 }
153 void vcpu_sleep_sync(struct vcpu *v)
154 {
155 vcpu_sleep_nosync(v);
157 while ( !vcpu_runnable(v) && test_bit(_VCPUF_running, &v->vcpu_flags) )
158 cpu_relax();
160 sync_vcpu_execstate(v);
161 }
163 void vcpu_wake(struct vcpu *v)
164 {
165 unsigned long flags;
167 vcpu_schedule_lock_irqsave(v, flags);
169 if ( likely(vcpu_runnable(v)) )
170 {
171 if ( v->runstate.state >= RUNSTATE_blocked )
172 vcpu_runstate_change(v, RUNSTATE_runnable, NOW());
173 SCHED_OP(wake, v);
174 }
175 else if ( !test_bit(_VCPUF_blocked, &v->vcpu_flags) )
176 {
177 if ( v->runstate.state == RUNSTATE_blocked )
178 vcpu_runstate_change(v, RUNSTATE_offline, NOW());
179 }
181 vcpu_schedule_unlock_irqrestore(v, flags);
183 TRACE_2D(TRC_SCHED_WAKE, v->domain->domain_id, v->vcpu_id);
184 }
186 int vcpu_set_affinity(struct vcpu *v, cpumask_t *affinity)
187 {
188 cpumask_t online_affinity;
190 cpus_and(online_affinity, *affinity, cpu_online_map);
191 if ( cpus_empty(online_affinity) )
192 return -EINVAL;
194 return SCHED_OP(set_affinity, v, affinity);
195 }
197 /* Block the currently-executing domain until a pertinent event occurs. */
198 static long do_block(void)
199 {
200 struct vcpu *v = current;
202 local_event_delivery_enable();
203 set_bit(_VCPUF_blocked, &v->vcpu_flags);
205 /* Check for events /after/ blocking: avoids wakeup waiting race. */
206 if ( local_events_need_delivery() )
207 {
208 clear_bit(_VCPUF_blocked, &v->vcpu_flags);
209 }
210 else
211 {
212 TRACE_2D(TRC_SCHED_BLOCK, v->domain->domain_id, v->vcpu_id);
213 __enter_scheduler();
214 }
216 return 0;
217 }
219 static long do_poll(struct sched_poll *sched_poll)
220 {
221 struct vcpu *v = current;
222 evtchn_port_t port;
223 long rc = 0;
224 unsigned int i;
226 /* Fairly arbitrary limit. */
227 if ( sched_poll->nr_ports > 128 )
228 return -EINVAL;
230 if ( !guest_handle_okay(sched_poll->ports, sched_poll->nr_ports) )
231 return -EFAULT;
233 /* These operations must occur in order. */
234 set_bit(_VCPUF_blocked, &v->vcpu_flags);
235 set_bit(_VCPUF_polling, &v->vcpu_flags);
236 set_bit(_DOMF_polling, &v->domain->domain_flags);
238 /* Check for events /after/ setting flags: avoids wakeup waiting race. */
239 for ( i = 0; i < sched_poll->nr_ports; i++ )
240 {
241 rc = -EFAULT;
242 if ( __copy_from_guest_offset(&port, sched_poll->ports, i, 1) )
243 goto out;
245 rc = -EINVAL;
246 if ( port >= MAX_EVTCHNS )
247 goto out;
249 rc = 0;
250 if ( test_bit(port, v->domain->shared_info->evtchn_pending) )
251 goto out;
252 }
254 if ( sched_poll->timeout != 0 )
255 set_timer(&v->poll_timer, sched_poll->timeout);
257 TRACE_2D(TRC_SCHED_BLOCK, v->domain->domain_id, v->vcpu_id);
258 __enter_scheduler();
260 stop_timer(&v->poll_timer);
262 out:
263 clear_bit(_VCPUF_polling, &v->vcpu_flags);
264 clear_bit(_VCPUF_blocked, &v->vcpu_flags);
265 return rc;
266 }
268 /* Voluntarily yield the processor for this allocation. */
269 static long do_yield(void)
270 {
271 TRACE_2D(TRC_SCHED_YIELD, current->domain->domain_id, current->vcpu_id);
272 __enter_scheduler();
273 return 0;
274 }
276 long do_sched_op_compat(int cmd, unsigned long arg)
277 {
278 long ret = 0;
280 switch ( cmd )
281 {
282 case SCHEDOP_yield:
283 {
284 ret = do_yield();
285 break;
286 }
288 case SCHEDOP_block:
289 {
290 ret = do_block();
291 break;
292 }
294 case SCHEDOP_shutdown:
295 {
296 TRACE_3D(TRC_SCHED_SHUTDOWN,
297 current->domain->domain_id, current->vcpu_id, arg);
298 domain_shutdown(current->domain, (u8)arg);
299 break;
300 }
302 default:
303 ret = -ENOSYS;
304 }
306 return ret;
307 }
309 long do_sched_op(int cmd, XEN_GUEST_HANDLE(void) arg)
310 {
311 long ret = 0;
313 switch ( cmd )
314 {
315 case SCHEDOP_yield:
316 {
317 ret = do_yield();
318 break;
319 }
321 case SCHEDOP_block:
322 {
323 ret = do_block();
324 break;
325 }
327 case SCHEDOP_shutdown:
328 {
329 struct sched_shutdown sched_shutdown;
331 ret = -EFAULT;
332 if ( copy_from_guest(&sched_shutdown, arg, 1) )
333 break;
335 ret = 0;
336 TRACE_3D(TRC_SCHED_SHUTDOWN,
337 current->domain->domain_id, current->vcpu_id,
338 sched_shutdown.reason);
339 domain_shutdown(current->domain, (u8)sched_shutdown.reason);
341 break;
342 }
344 case SCHEDOP_poll:
345 {
346 struct sched_poll sched_poll;
348 ret = -EFAULT;
349 if ( copy_from_guest(&sched_poll, arg, 1) )
350 break;
352 ret = do_poll(&sched_poll);
354 break;
355 }
357 case SCHEDOP_remote_shutdown:
358 {
359 struct domain *d;
360 struct sched_remote_shutdown sched_remote_shutdown;
362 if ( !IS_PRIV(current->domain) )
363 return -EPERM;
365 ret = -EFAULT;
366 if ( copy_from_guest(&sched_remote_shutdown, arg, 1) )
367 break;
369 ret = -ESRCH;
370 d = find_domain_by_id(sched_remote_shutdown.domain_id);
371 if ( d == NULL )
372 break;
374 domain_shutdown(d, (u8)sched_remote_shutdown.reason);
375 put_domain(d);
376 ret = 0;
378 break;
379 }
381 default:
382 ret = -ENOSYS;
383 }
385 return ret;
386 }
388 /* Per-domain one-shot-timer hypercall. */
389 long do_set_timer_op(s_time_t timeout)
390 {
391 struct vcpu *v = current;
393 if ( timeout == 0 )
394 stop_timer(&v->timer);
395 else
396 set_timer(&v->timer, timeout);
398 return 0;
399 }
401 /* sched_id - fetch ID of current scheduler */
402 int sched_id(void)
403 {
404 return ops.sched_id;
405 }
407 long sched_ctl(struct sched_ctl_cmd *cmd)
408 {
409 if ( cmd->sched_id != ops.sched_id )
410 return -EINVAL;
412 SCHED_OP(control, cmd);
413 TRACE_0D(TRC_SCHED_CTL);
414 return 0;
415 }
418 /* Adjust scheduling parameter for a given domain. */
419 long sched_adjdom(struct sched_adjdom_cmd *cmd)
420 {
421 struct domain *d;
422 struct vcpu *v;
424 if ( (cmd->sched_id != ops.sched_id) ||
425 ((cmd->direction != SCHED_INFO_PUT) &&
426 (cmd->direction != SCHED_INFO_GET)) )
427 return -EINVAL;
429 d = find_domain_by_id(cmd->domain);
430 if ( d == NULL )
431 return -ESRCH;
433 /*
434 * Most VCPUs we can simply pause. If we are adjusting this VCPU then
435 * we acquire the local schedule_lock to guard against concurrent updates.
436 *
437 * We only acquire the local schedule lock after we have paused all other
438 * VCPUs in this domain. There are two reasons for this:
439 * 1- We don't want to hold up interrupts as pausing a VCPU can
440 * trigger a tlb shootdown.
441 * 2- Pausing other VCPUs involves briefly locking the schedule
442 * lock of the CPU they are running on. This CPU could be the
443 * same as ours.
444 */
446 for_each_vcpu ( d, v )
447 {
448 if ( v != current )
449 vcpu_pause(v);
450 }
452 if ( d == current->domain )
453 vcpu_schedule_lock_irq(current);
455 SCHED_OP(adjdom, d, cmd);
456 TRACE_1D(TRC_SCHED_ADJDOM, d->domain_id);
458 if ( d == current->domain )
459 vcpu_schedule_unlock_irq(current);
461 for_each_vcpu ( d, v )
462 {
463 if ( v != current )
464 vcpu_unpause(v);
465 }
467 put_domain(d);
469 return 0;
470 }
472 /*
473 * The main function
474 * - deschedule the current domain (scheduler independent).
475 * - pick a new domain (scheduler dependent).
476 */
477 static void __enter_scheduler(void)
478 {
479 struct vcpu *prev = current, *next = NULL;
480 int cpu = smp_processor_id();
481 s_time_t now = NOW();
482 struct task_slice next_slice;
483 s32 r_time; /* time for new dom to run */
485 ASSERT(!in_irq());
487 perfc_incrc(sched_run);
489 spin_lock_irq(&schedule_data[cpu].schedule_lock);
491 stop_timer(&schedule_data[cpu].s_timer);
493 /* get policy-specific decision on scheduling... */
494 next_slice = ops.do_schedule(now);
496 r_time = next_slice.time;
497 next = next_slice.task;
499 schedule_data[cpu].curr = next;
501 set_timer(&schedule_data[cpu].s_timer, now + r_time);
503 if ( unlikely(prev == next) )
504 {
505 spin_unlock_irq(&schedule_data[cpu].schedule_lock);
506 return continue_running(prev);
507 }
509 TRACE_2D(TRC_SCHED_SWITCH_INFPREV,
510 prev->domain->domain_id,
511 now - prev->runstate.state_entry_time);
512 TRACE_3D(TRC_SCHED_SWITCH_INFNEXT,
513 next->domain->domain_id,
514 (next->runstate.state == RUNSTATE_runnable) ?
515 (now - next->runstate.state_entry_time) : 0,
516 r_time);
518 ASSERT(prev->runstate.state == RUNSTATE_running);
519 vcpu_runstate_change(
520 prev,
521 (test_bit(_VCPUF_blocked, &prev->vcpu_flags) ? RUNSTATE_blocked :
522 (vcpu_runnable(prev) ? RUNSTATE_runnable : RUNSTATE_offline)),
523 now);
525 ASSERT(next->runstate.state != RUNSTATE_running);
526 vcpu_runstate_change(next, RUNSTATE_running, now);
528 ASSERT(!test_bit(_VCPUF_running, &next->vcpu_flags));
529 set_bit(_VCPUF_running, &next->vcpu_flags);
531 spin_unlock_irq(&schedule_data[cpu].schedule_lock);
533 perfc_incrc(sched_ctx);
535 prev->sleep_tick = schedule_data[cpu].tick;
537 /* Ensure that the domain has an up-to-date time base. */
538 if ( !is_idle_vcpu(next) )
539 {
540 update_vcpu_system_time(next);
541 if ( next->sleep_tick != schedule_data[cpu].tick )
542 send_timer_event(next);
543 }
545 TRACE_4D(TRC_SCHED_SWITCH,
546 prev->domain->domain_id, prev->vcpu_id,
547 next->domain->domain_id, next->vcpu_id);
549 context_switch(prev, next);
550 }
553 /****************************************************************************
554 * Timers: the scheduler utilises a number of timers
555 * - s_timer: per CPU timer for preemption and scheduling decisions
556 * - t_timer: per CPU periodic timer to send timer interrupt to current dom
557 * - dom_timer: per domain timer to specifiy timeout values
558 ****************************************************************************/
560 /* The scheduler timer: force a run through the scheduler */
561 static void s_timer_fn(void *unused)
562 {
563 raise_softirq(SCHEDULE_SOFTIRQ);
564 perfc_incrc(sched_irq);
565 }
567 /* Periodic tick timer: send timer event to current domain */
568 static void t_timer_fn(void *unused)
569 {
570 struct vcpu *v = current;
571 unsigned int cpu = smp_processor_id();
573 schedule_data[cpu].tick++;
575 if ( !is_idle_vcpu(v) )
576 {
577 update_vcpu_system_time(v);
578 send_timer_event(v);
579 }
581 page_scrub_schedule_work();
583 SCHED_OP(tick, cpu);
585 set_timer(&t_timer[cpu], NOW() + MILLISECS(10));
586 }
588 /* Per-VCPU timer function: sends a virtual timer interrupt. */
589 static void vcpu_timer_fn(void *data)
590 {
591 struct vcpu *v = data;
592 send_timer_event(v);
593 }
595 /* SCHEDOP_poll timeout callback. */
596 static void poll_timer_fn(void *data)
597 {
598 struct vcpu *v = data;
599 if ( test_and_clear_bit(_VCPUF_polling, &v->vcpu_flags) )
600 vcpu_unblock(v);
601 }
603 /* Initialise the data structures. */
604 void __init scheduler_init(void)
605 {
606 int i;
608 open_softirq(SCHEDULE_SOFTIRQ, __enter_scheduler);
610 for ( i = 0; i < NR_CPUS; i++ )
611 {
612 spin_lock_init(&schedule_data[i].schedule_lock);
613 init_timer(&schedule_data[i].s_timer, s_timer_fn, NULL, i);
614 init_timer(&t_timer[i], t_timer_fn, NULL, i);
615 }
617 for ( i = 0; schedulers[i] != NULL; i++ )
618 {
619 ops = *schedulers[i];
620 if ( strcmp(ops.opt_name, opt_sched) == 0 )
621 break;
622 }
624 if ( schedulers[i] == NULL )
625 printk("Could not find scheduler: %s\n", opt_sched);
627 printk("Using scheduler: %s (%s)\n", ops.name, ops.opt_name);
628 SCHED_OP(init);
629 }
631 /*
632 * Start a scheduler for each CPU
633 * This has to be done *after* the timers, e.g., APICs, have been initialised
634 */
635 void schedulers_start(void)
636 {
637 t_timer_fn(0);
638 smp_call_function((void *)t_timer_fn, NULL, 1, 1);
639 }
641 void dump_runq(unsigned char key)
642 {
643 s_time_t now = NOW();
644 int i;
645 unsigned long flags;
647 local_irq_save(flags);
649 printk("Scheduler: %s (%s)\n", ops.name, ops.opt_name);
650 SCHED_OP(dump_settings);
651 printk("NOW=0x%08X%08X\n", (u32)(now>>32), (u32)now);
653 for_each_online_cpu ( i )
654 {
655 spin_lock(&schedule_data[i].schedule_lock);
656 printk("CPU[%02d] ", i);
657 SCHED_OP(dump_cpu_state,i);
658 spin_unlock(&schedule_data[i].schedule_lock);
659 }
661 local_irq_restore(flags);
662 }
664 /*
665 * Local variables:
666 * mode: C
667 * c-set-style: "BSD"
668 * c-basic-offset: 4
669 * tab-width: 4
670 * indent-tabs-mode: nil
671 * End:
672 */