6 class Thread_object : public Thread
16 class Obj_cap : public L4_obj_ref
21 // ---------------------------------------------------------------------------
27 #include "processor.h"
29 #include "thread_state.h"
35 Obj_cap::Obj_cap(L4_obj_ref const &o) : L4_obj_ref(o) {}
37 PUBLIC inline NEEDS["kobject.h"]
39 Obj_cap::deref(unsigned char *rights = 0, bool dbg = false)
41 Thread *current = current_thread();
42 if (op() & L4_obj_ref::Ipc_reply)
44 if (rights) *rights = current->caller_rights();
45 Thread *ca = static_cast<Thread*>(current->caller());
47 current->set_caller(0,0);
51 if (EXPECT_FALSE(special()))
56 if (rights) *rights = L4_fpage::RWX;
57 return current_thread();
60 return current->space()->obj_space()->lookup_local(cap(), rights);
63 PUBLIC inline NEEDS["kobject.h"]
65 Obj_cap::revalidate(Kobject_iface *o)
71 Thread_object::Thread_object() : Thread() {}
74 Thread_object::Thread_object(Context_mode_kernel k) : Thread(k) {}
79 { return dec_ref() == 0; }
85 Thread_object::operator delete(void *_t)
87 Thread_object * const t = nonull_static_cast<Thread_object*>(_t);
88 Ram_quota * const q = t->_quota;
89 Mapped_allocator::allocator()->q_unaligned_free(q, Config::thread_block_size, t);
91 LOG_TRACE("Kobject delete", "del", current(), __fmt_kobj_destroy,
92 Log_destroy *l = tbe->payload<Log_destroy>();
96 l->ram = q->current());
102 Thread_object::destroy(Kobject ***rl)
104 Kobject::destroy(rl);
106 assert_kdb(_magic == magic);
111 Thread_object::invoke(L4_obj_ref /*self*/, Mword rights, Syscall_frame *f, Utcb *utcb)
113 register L4_obj_ref::Operation op = f->ref().op();
114 if (((op != 0) && !(op & L4_obj_ref::Ipc_send))
115 || (op & L4_obj_ref::Ipc_reply)
116 || f->tag().proto() != L4_msg_tag::Label_thread)
119 Thread *ct = current_thread();
122 bool have_rcv = false;
124 if (EXPECT_FALSE(!check_sys_ipc(op, &partner, &sender, &have_rcv)))
126 utcb->error = L4_error::Not_existent;
130 ct->do_ipc(f->tag(), partner, partner, have_rcv, sender,
131 f->timeout(), f, rights);
135 switch (utcb->values[0] & Opcode_mask)
138 f->tag(sys_control(rights, f->tag(), utcb));
141 f->tag(sys_ex_regs(f->tag(), utcb));
144 f->tag(sys_thread_switch(f->tag(), utcb));
147 f->tag(sys_thread_stats(f->tag(), utcb));
150 f->tag(sys_vcpu_resume(f->tag(), utcb));
152 case Op_register_del_irq:
153 f->tag(sys_register_delete_irq(f->tag(), utcb, utcb));
155 case Op_modify_senders:
156 f->tag(sys_modify_senders(f->tag(), utcb, utcb));
158 case Op_vcpu_control:
159 f->tag(sys_vcpu_control(rights, f->tag(), utcb));
162 L4_msg_tag tag = f->tag();
163 if (invoke_arch(tag, utcb))
166 f->tag(commit_result(-L4_err::ENosys));
174 Thread_object::sys_vcpu_resume(L4_msg_tag const &tag, Utcb *utcb)
176 if (this != current() || !(state() & Thread_vcpu_enabled))
177 return commit_result(-L4_err::EInval);
179 Obj_space *s = space()->obj_space();
180 Vcpu_state *vcpu = vcpu_state().access(true);
182 L4_obj_ref user_task = vcpu->user_task;
183 if (user_task.valid())
185 unsigned char task_rights = 0;
186 Task *task = Kobject::dcast<Task*>(s->lookup_local(user_task.cap(),
189 if (EXPECT_FALSE(task && !(task_rights & L4_fpage::W)))
190 return commit_result(-L4_err::EPerm);
192 if (task != vcpu_user_space())
193 vcpu_set_user_space(task);
195 vcpu->user_task = L4_obj_ref();
197 else if (user_task.op() == L4_obj_ref::Ipc_reply)
198 vcpu_set_user_space(0);
200 L4_snd_item_iter snd_items(utcb, tag.words());
201 int items = tag.items();
202 for (; items && snd_items.more(); --items)
204 if (EXPECT_FALSE(!snd_items.next()))
207 // XXX: need to take existance lock for map
210 L4_snd_item_iter::Item const *const item = snd_items.get();
211 L4_fpage sfp(item->d);
214 L4_error err = fpage_map(space(), sfp,
215 vcpu_user_space(), L4_fpage::all_spaces(),
221 if (EXPECT_FALSE(!err.ok()))
222 return commit_error(utcb, err);
225 if ((vcpu->_saved_state & Vcpu_state::F_irqs)
226 && (vcpu->sticky_flags & Vcpu_state::Sf_irq_pending))
228 assert_kdb(cpu_lock.test());
229 do_ipc(L4_msg_tag(), 0, 0, true, 0,
230 L4_timeout_pair(L4_timeout::Zero, L4_timeout::Zero),
231 &vcpu->_ipc_regs, 3);
233 vcpu = vcpu_state().access(true);
235 if (EXPECT_TRUE(!vcpu->_ipc_regs.tag().has_error()
236 || this->utcb().access(true)->error.error() == L4_error::R_timeout))
238 vcpu->_ts.set_ipc_upcall();
242 // tried to resume to user mode, so an IRQ enters from user mode
243 if (vcpu->_saved_state & Vcpu_state::F_user_mode)
244 sp = vcpu->_entry_sp;
248 LOG_TRACE("VCPU events", "vcpu", this, __context_vcpu_log_fmt,
249 Vcpu_log *l = tbe->payload<Vcpu_log>();
251 l->state = vcpu->state;
252 l->ip = vcpu->_entry_ip;
254 l->space = static_cast<Task*>(_space.vcpu_aware())->dbg_id();
257 fast_return_to_user(vcpu->_entry_ip, sp, vcpu_state().usr().get());
261 vcpu->state = vcpu->_saved_state;
262 Task *target_space = nonull_static_cast<Task*>(space());
263 bool user_mode = false;
265 if (vcpu->state & Vcpu_state::F_user_mode)
267 if (!vcpu_user_space())
268 return commit_result(-L4_err::EInval);
272 if (!(vcpu->state & Vcpu_state::F_fpu_enabled))
274 state_add_dirty(Thread_vcpu_fpu_disabled);
278 state_del_dirty(Thread_vcpu_fpu_disabled);
280 target_space = static_cast<Task*>(vcpu_user_space());
283 LOG_TRACE("VCPU events", "vcpu", this, __context_vcpu_log_fmt,
284 Vcpu_log *l = tbe->payload<Vcpu_log>();
286 l->state = vcpu->state;
287 l->ip = vcpu->_ts.ip();
288 l->sp = vcpu->_ts.sp();
289 l->space = target_space->dbg_id();
292 return commit_result(target_space->resume_vcpu(this, vcpu, user_mode));
295 PRIVATE inline NOEXPORT NEEDS["processor.h"]
297 Thread_object::sys_modify_senders(L4_msg_tag tag, Utcb const *in, Utcb * /*out*/)
299 if (sender_list()->cursor())
300 return Kobject_iface::commit_result(-L4_err::EBusy);
303 printf("MODIFY ID (%08lx:%08lx->%08lx:%08lx\n",
304 in->values[1], in->values[2],
305 in->values[3], in->values[4]);
308 int elems = tag.words();
311 return Kobject_iface::commit_result(0);
317 ::Prio_list_elem *c = sender_list()->head();
320 // this is kind of arbitrary
321 for (int cnt = 50; c && cnt > 0; --cnt)
323 Sender *s = Sender::cast(c);
324 s->modify_label(&in->values[1], elems);
329 return Kobject_iface::commit_result(0);
331 sender_list()->cursor(c);
332 Proc::preemption_point();
333 c = sender_list()->cursor();
335 return Kobject_iface::commit_result(0);
338 PRIVATE inline NOEXPORT
340 Thread_object::sys_register_delete_irq(L4_msg_tag tag, Utcb const *in, Utcb * /*out*/)
342 L4_snd_item_iter snd_items(in, tag.words());
344 if (!tag.items() || !snd_items.next())
345 return Kobject_iface::commit_result(-L4_err::EInval);
347 L4_fpage bind_irq(snd_items.get()->d);
348 if (EXPECT_FALSE(!bind_irq.is_objpage()))
349 return Kobject_iface::commit_error(in, L4_error::Overflow);
351 register Context *const c_thread = ::current();
352 register Space *const c_space = c_thread->space();
353 register Obj_space *const o_space = c_space->obj_space();
354 unsigned char irq_rights = 0;
356 = Irq_base::dcast(o_space->lookup_local(bind_irq.obj_index(), &irq_rights));
359 return Kobject_iface::commit_result(-L4_err::EInval);
361 if (EXPECT_FALSE(!(irq_rights & L4_fpage::X)))
362 return Kobject_iface::commit_result(-L4_err::EPerm);
364 register_delete_irq(irq);
365 return Kobject_iface::commit_result(0);
369 PRIVATE inline NOEXPORT
371 Thread_object::sys_control(unsigned char rights, L4_msg_tag const &tag, Utcb *utcb)
373 if (EXPECT_FALSE(!(rights & L4_fpage::W)))
374 return commit_result(-L4_err::EPerm);
376 if (EXPECT_FALSE(tag.words() < 6))
377 return commit_result(-L4_err::EInval);
379 Context *curr = current();
380 Obj_space *s = curr->space()->obj_space();
381 L4_snd_item_iter snd_items(utcb, tag.words());
383 User<Utcb>::Ptr utcb_addr(0);
385 Mword flags = utcb->values[0];
387 Mword _old_pager = _pager.raw() << L4_obj_ref::Cap_shift;
388 Mword _old_exc_handler = _exc_handler.raw() << L4_obj_ref::Cap_shift;
390 Thread_ptr _new_pager(~0UL);
391 Thread_ptr _new_exc_handler(~0UL);
393 if (flags & Ctl_set_pager)
394 _new_pager = Thread_ptr(utcb->values[1] >> L4_obj_ref::Cap_shift);
396 if (flags & Ctl_set_exc_handler)
397 _new_exc_handler = Thread_ptr(utcb->values[2] >> L4_obj_ref::Cap_shift);
399 if (flags & Ctl_bind_task)
401 if (EXPECT_FALSE(!tag.items() || !snd_items.next()))
402 return commit_result(-L4_err::EInval);
404 L4_fpage bind_task(snd_items.get()->d);
406 if (EXPECT_FALSE(!bind_task.is_objpage()))
407 return commit_result(-L4_err::EInval);
409 unsigned char task_rights = 0;
410 task = Kobject::dcast<Task*>(s->lookup_local(bind_task.obj_index(), &task_rights));
412 if (EXPECT_FALSE(!(task_rights & L4_fpage::W)))
413 return commit_result(-L4_err::EPerm);
416 return commit_result(-L4_err::EInval);
418 utcb_addr = User<Utcb>::Ptr((Utcb*)utcb->values[5]);
420 if (EXPECT_FALSE(!bind(task, utcb_addr)))
421 return commit_result(-L4_err::EInval); // unbind first !!
427 long res = control(_new_pager, _new_exc_handler);
430 return commit_result(res);
432 if ((res = sys_control_arch(utcb)) < 0)
433 return commit_result(res);
435 // FIXME: must be done xcpu safe, may be some parts above too
436 if (flags & Ctl_alien_thread)
438 if (utcb->values[4] & Ctl_alien_thread)
440 add_state |= Thread_alien;
441 del_state |= Thread_dis_alien;
444 del_state |= Thread_alien;
447 if (del_state || add_state)
448 drq_state_change(~del_state, add_state);
450 utcb->values[1] = _old_pager;
451 utcb->values[2] = _old_exc_handler;
453 return commit_result(0, 3);
457 PRIVATE inline NOEXPORT
459 Thread_object::sys_vcpu_control(unsigned char, L4_msg_tag const &tag,
463 return commit_result(-L4_err::EInval);
465 User<Vcpu_state>::Ptr vcpu(0);
467 if (tag.words() >= 2)
468 vcpu = User<Vcpu_state>::Ptr((Vcpu_state*)utcb->values[1]);
475 Mword size = sizeof(Vcpu_state);
476 if (utcb->values[0] & 0x10000)
478 size = Config::PAGE_SIZE;
479 add_state |= Thread_ext_vcpu_enabled;
482 Space::Ku_mem const *vcpu_m
483 = space()->find_ku_mem(vcpu, size);
486 return commit_result(-L4_err::EInval);
488 add_state |= Thread_vcpu_enabled;
489 _vcpu_state.set(vcpu, vcpu_m->kern_addr(vcpu));
492 return commit_result(-L4_err::EInval);
494 /* hm, we do not allow to disable vCPU mode, it's one way enable
497 del_state |= Thread_vcpu_enabled | Thread_vcpu_user_mode
498 | Thread_vcpu_fpu_disabled | Thread_ext_vcpu_enabled;
502 drq_state_change(~del_state, add_state);
504 return commit_result(0);
508 // -------------------------------------------------------------------
509 // Thread::ex_regs class system calls
513 Thread_object::ex_regs(Address ip, Address sp,
514 Address *o_ip = 0, Address *o_sp = 0, Mword *o_flags = 0,
517 if (state(false) == Thread_invalid || !space())
520 if (current() == this)
523 if (o_sp) *o_sp = user_sp();
524 if (o_ip) *o_ip = user_ip();
525 if (o_flags) *o_flags = user_flags();
527 // Changing the run state is only possible when the thread is not in
529 if (!(ops & Exr_cancel) && (state() & Thread_in_exception))
530 // XXX Maybe we should return false here. Previously, we actually
531 // did so, but we also actually didn't do any state modification.
532 // If you change this value, make sure the logic in
533 // sys_thread_ex_regs still works (in particular,
534 // ex_regs_cap_handler and friends should still be called).
537 if (state() & Thread_dead) // resurrect thread
538 state_change_dirty(~Thread_dead, Thread_ready);
540 else if (ops & Exr_cancel)
541 // cancel ongoing IPC or other activity
542 state_add_dirty(Thread_cancel | Thread_ready);
544 if (ops & Exr_trigger_exception)
546 extern char leave_by_trigger_exception[];
547 do_trigger_exception(regs(), leave_by_trigger_exception);
556 if (current() == this)
564 Thread_object::ex_regs(Utcb *utcb)
566 Address ip = utcb->values[1];
567 Address sp = utcb->values[2];
569 Mword ops = utcb->values[0];
571 LOG_TRACE("Ex-regs", "exr", current(), __fmt_thread_exregs,
572 Log_thread_exregs *l = tbe->payload<Log_thread_exregs>();
574 l->ip = ip; l->sp = sp; l->op = ops;);
576 if (!ex_regs(ip, sp, &ip, &sp, &flags, ops))
577 return commit_result(-L4_err::EInval);
579 utcb->values[0] = flags;
580 utcb->values[1] = ip;
581 utcb->values[2] = sp;
583 return commit_result(0, 3);
588 Thread_object::handle_remote_ex_regs(Drq *, Context *self, void *p)
590 Remote_syscall *params = reinterpret_cast<Remote_syscall*>(p);
591 params->result = nonull_static_cast<Thread_object*>(self)->ex_regs(params->thread->utcb().access());
592 return params->result.proto() == 0 ? Drq::Need_resched : 0;
595 PRIVATE inline NOEXPORT
597 Thread_object::sys_ex_regs(L4_msg_tag const &tag, Utcb *utcb)
599 if (tag.words() != 3)
600 return commit_result(-L4_err::EInval);
602 if (current() == this)
603 return ex_regs(utcb);
605 Remote_syscall params;
606 params.thread = current_thread();
608 drq(handle_remote_ex_regs, ¶ms, 0, Drq::Any_ctxt);
609 return params.result;
612 PRIVATE inline NOEXPORT NEEDS["timer.h"]
614 Thread_object::sys_thread_switch(L4_msg_tag const &/*tag*/, Utcb *utcb)
616 Context *curr = current();
619 return commit_result(0);
621 if (current_cpu() != cpu())
622 return commit_result(0);
625 Sched_context * const cs = current_sched();
629 && ((state() & (Thread_ready | Thread_suspended)) == Thread_ready))
631 curr->switch_exec_schedule_locked (this, Not_Helping);
632 reinterpret_cast<Utcb::Time_val*>(utcb->values)->t = 0; // Assume timeslice was used up
633 return commit_result(0, Utcb::Time_val::Words);
636 #if 0 // FIXME: provide API for multiple sched contexts
637 // Compute remaining quantum length of timeslice
638 regs->left(timeslice_timeout.cpu(cpu())->get_timeout(Timer::system_clock()));
640 // Yield current global timeslice
641 cs->owner()->switch_sched(cs->id() ? cs->next() : cs);
643 reinterpret_cast<Utcb::Time_val*>(utcb->values)->t
644 = timeslice_timeout.cpu(current_cpu())->get_timeout(Timer::system_clock());
647 return commit_result(0, Utcb::Time_val::Words);
652 // -------------------------------------------------------------------
653 // Gather statistics information about thread execution
657 Thread_object::sys_thread_stats_remote(void *data)
659 update_consumed_time();
660 *(Clock::Time *)data = consumed_time();
666 Thread_object::handle_sys_thread_stats_remote(Drq *, Context *self, void *data)
668 return nonull_static_cast<Thread_object*>(self)->sys_thread_stats_remote(data);
671 PRIVATE inline NOEXPORT
673 Thread_object::sys_thread_stats(L4_msg_tag const &/*tag*/, Utcb *utcb)
677 if (cpu() != current_cpu())
678 drq(handle_sys_thread_stats_remote, &value, 0, Drq::Any_ctxt);
681 // Respect the fact that the consumed time is only updated on context switch
682 if (this == current())
683 update_consumed_time();
684 value = consumed_time();
687 reinterpret_cast<Utcb::Time_val *>(utcb->values)->t = value;
689 return commit_result(0, Utcb::Time_val::Words);