2 * (c) 2009 Alexander Warg <warg@os.inf.tu-dresden.de>
3 * economic rights: Technische Universität Dresden (Germany)
5 * This file is part of TUD:OS and distributed under the terms of the
6 * GNU General Public License 2.
7 * Please see the COPYING-GPL-2 file for details.
9 #include "sched_proxy.h"
15 #include <l4/sys/scheduler>
21 blow_up(l4_sched_cpu_set_t const &src, unsigned char gran)
24 gran &= sizeof(l4_umword_t) * 8 - 1;
25 unsigned char og = src.granularity() & (sizeof(l4_umword_t) * 8 - 1);
26 n.set(gran, src.offset() & (~0UL << og));
28 for (unsigned i = 0; i < sizeof(l4_umword_t) * 8; ++i)
29 if (src.map & (1UL << (i >> (og - gran))))
36 l4_sched_cpu_set_t operator & (l4_sched_cpu_set_t const &a, l4_sched_cpu_set_t const &b)
38 l4_sched_cpu_set_t _a, _b;
39 unsigned char const ga = a.granularity() & (sizeof(l4_umword_t) * 8 - 1);
40 unsigned char const gb = b.granularity() & (sizeof(l4_umword_t) * 8 - 1);
57 long ofs_dif = _a.offset() - _b.offset();
58 long unsigned abs_ofs_dif;
60 abs_ofs_dif = -ofs_dif;
62 abs_ofs_dif = ofs_dif;
64 if (abs_ofs_dif >= sizeof(l4_umword_t) * 8)
65 return l4_sched_cpu_set(0, 0, 0);
69 _b.map &= (_a.map >> abs_ofs_dif);
74 _a.map &= (_b.map >> abs_ofs_dif);
79 Sched_proxy::List Sched_proxy::_list;
81 Sched_proxy::Sched_proxy() :
82 Icu(1, &_scheduler_irq),
83 _real_cpus(l4_sched_cpu_set(0, 0, 0)), _cpu_mask(_real_cpus),
85 _prio_offset(0), _prio_limit(0)
88 _list.push_front(this);
92 Sched_proxy::rescan_cpus()
99 int e = l4_error(L4Re::Env::env()->scheduler()->info(&max, &c));
103 _max_cpus = std::min<unsigned>(sizeof(l4_umword_t) * 8, max);
106 _cpus = _real_cpus & _cpu_mask;
109 Sched_proxy::~Sched_proxy()
115 Sched_proxy::info(l4_umword_t *cpu_max, l4_sched_cpu_set_t *cpus)
117 *cpu_max = _max_cpus;
118 unsigned char g = cpus->granularity() & (sizeof(l4_umword_t) * 8 - 1);
119 l4_umword_t offs = cpus->offset() & (~0UL << g);
120 if (offs >= _max_cpus)
125 for (unsigned i = offs; i < _max_cpus && b < sizeof(l4_umword_t) * 8;)
127 if (_cpus.map & (1UL << i))
128 cpus->map |= 1UL << b;
132 if (!(i & ~(~0UL << g)))
141 Sched_proxy::run_thread(L4::Cap<L4::Thread> thread, l4_sched_param_t const &sp)
143 l4_sched_param_t s = sp;
144 s.prio = std::min(sp.prio + _prio_offset, (l4_umword_t)_prio_limit);
145 s.affinity = sp.affinity & _cpus;
148 printf("loader[%p] run_thread: o=%u scheduler affinity = %lx "
149 "sp.m=%lx sp.o=%u sp.g=%u\n",
150 this, _cpus.offset(), _cpus.map, sp.affinity.map,
151 sp.affinity.offset(), sp.affinity.granularity());
153 " s.m=%lx s.o=%u s.g=%u\n",
154 this, s.affinity.map, s.affinity.offset(),
155 s.affinity.granularity());
157 return l4_error(L4Re::Env::env()->scheduler()->run_thread(thread, s));
161 Sched_proxy::idle_time(l4_sched_cpu_set_t const &, l4_kernel_clock_t &)
162 { return -L4_ENOSYS; }
166 Sched_proxy::received_thread(L4::Ipc::Snd_fpage const &fp)
168 if (!fp.cap_received())
169 return L4::Cap<L4::Thread>::Invalid;
171 return L4::Cap<L4::Thread>(Rcv_cap << L4_CAP_SHIFT);
175 Sched_proxy::restrict_cpus(l4_umword_t cpus)
177 _cpu_mask = l4_sched_cpu_set(0, 0, cpus);
178 _cpus = _real_cpus & _cpu_mask;
182 class Cpu_hotplug_server :
183 public L4::Irqep_t<Cpu_hotplug_server, Moe::Server_object>
188 for (auto i : Sched_proxy::_list)
197 L4::Cap<L4::Irq> irq = object_pool.cap_alloc()->alloc<L4::Irq>();
200 Err(Err::Fatal).printf("Could not allocate capability for CPU hotplug\n");
204 if (l4_error(L4::Cap<L4::Factory>(L4_BASE_FACTORY_CAP)->create(irq)) < 0)
206 Err(Err::Fatal).printf("Could not allocate IRQ for CPU hotplug\n");
210 if (l4_error(irq->attach(l4_umword_t(this), L4::Cap<L4::Thread>(L4_BASE_THREAD_CAP))) < 0)
212 Err(Err::Fatal).printf("Could not attach to CPU hotplug IRQ\n");
216 if (l4_error(L4Re::Env::env()->scheduler()->bind(0, irq)) < 0)
218 Err(Err::Fatal).printf("Could not bind CPU hotplug IRQ to scheduler\n");
224 static Cpu_hotplug_server _cpu_hotplug_server;