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 (flags() & 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(invalid()))
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; }
83 /** Deallocator. This function currently does nothing: We do not free up
84 space allocated to thread-control blocks.
88 Thread_object::operator delete(void *_t)
90 Thread_object * const t = nonull_static_cast<Thread_object*>(_t);
91 Ram_quota * const q = t->_quota;
92 Mapped_allocator::allocator()->q_unaligned_free(q, Config::thread_block_size, t);
94 LOG_TRACE("Kobject delete", "del", current(), __fmt_kobj_destroy,
95 Log_destroy *l = tbe->payload<Log_destroy>();
99 l->ram = q->current());
105 Thread_object::destroy(Kobject ***rl)
107 Kobject::destroy(rl);
109 assert_kdb(_magic == magic);
115 Thread_object::invoke(L4_obj_ref /*self*/, Mword rights, Syscall_frame *f, Utcb *utcb)
117 register unsigned flags = f->ref().flags();
118 if (((flags != 0) && !(flags & L4_obj_ref::Ipc_send))
119 || (flags & L4_obj_ref::Ipc_reply)
120 || f->tag().proto() != L4_msg_tag::Label_thread)
123 Thread *ct = current_thread();
126 bool have_rcv = false;
128 if (EXPECT_FALSE(!check_sys_ipc(flags, &partner, &sender, &have_rcv)))
130 utcb->error = L4_error::Not_existent;
134 ct->do_ipc(f->tag(), partner, partner, have_rcv, sender,
135 f->timeout(), f, rights);
139 switch (utcb->values[0] & Opcode_mask)
142 f->tag(sys_control(rights, f->tag(), utcb));
145 f->tag(sys_ex_regs(f->tag(), utcb));
148 f->tag(sys_thread_switch(f->tag(), utcb));
151 f->tag(sys_thread_stats(f->tag(), utcb));
154 f->tag(sys_vcpu_resume(f->tag(), utcb));
156 case Op_register_del_irq:
157 f->tag(sys_register_delete_irq(f->tag(), utcb, utcb));
159 case Op_modify_senders:
160 f->tag(sys_modify_senders(f->tag(), utcb, utcb));
162 case Op_vcpu_control:
163 f->tag(sys_vcpu_control(rights, f->tag(), utcb));
166 L4_msg_tag tag = f->tag();
167 if (invoke_arch(tag, utcb))
170 f->tag(commit_result(-L4_err::ENosys));
178 Thread_object::sys_vcpu_resume(L4_msg_tag const &tag, Utcb *utcb)
180 if (this != current() || !(state() & Thread_vcpu_enabled))
181 return commit_result(-L4_err::EInval);
183 Obj_space *s = space()->obj_space();
184 Vcpu_state *vcpu = vcpu_state().access(true);
186 L4_obj_ref user_task = vcpu->user_task;
187 if (user_task.valid())
189 unsigned char task_rights = 0;
190 Task *task = Kobject::dcast<Task*>(s->lookup_local(user_task.cap(),
193 if (EXPECT_FALSE(task && !(task_rights & L4_fpage::W)))
194 return commit_result(-L4_err::EPerm);
196 if (task != vcpu_user_space())
197 vcpu_set_user_space(task);
199 vcpu->user_task = L4_obj_ref();
201 else if (user_task.flags() == L4_obj_ref::Ipc_reply)
202 vcpu_set_user_space(0);
204 L4_snd_item_iter snd_items(utcb, tag.words());
205 int items = tag.items();
206 for (; items && snd_items.more(); --items)
208 if (EXPECT_FALSE(!snd_items.next()))
211 // XXX: need to take existance lock for map
214 L4_snd_item_iter::Item const *const item = snd_items.get();
215 L4_fpage sfp(item->d);
218 L4_error err = fpage_map(space(), sfp,
219 vcpu_user_space(), L4_fpage::all_spaces(),
225 if (EXPECT_FALSE(!err.ok()))
226 return commit_error(utcb, err);
229 if ((vcpu->_saved_state & Vcpu_state::F_irqs)
230 && (vcpu->sticky_flags & Vcpu_state::Sf_irq_pending))
232 assert_kdb(cpu_lock.test());
233 do_ipc(L4_msg_tag(), 0, 0, true, 0,
234 L4_timeout_pair(L4_timeout::Zero, L4_timeout::Zero),
235 &vcpu->_ipc_regs, 3);
237 vcpu = vcpu_state().access(true);
239 if (EXPECT_TRUE(!vcpu->_ipc_regs.tag().has_error()
240 || this->utcb().access(true)->error.error() == L4_error::R_timeout))
242 vcpu->_ts.set_ipc_upcall();
246 // tried to resume to user mode, so an IRQ enters from user mode
247 if (vcpu->_saved_state & Vcpu_state::F_user_mode)
248 sp = vcpu->_entry_sp;
252 LOG_TRACE("VCPU events", "vcpu", this, __context_vcpu_log_fmt,
253 Vcpu_log *l = tbe->payload<Vcpu_log>();
255 l->state = vcpu->state;
256 l->ip = vcpu->_entry_ip;
258 l->space = static_cast<Task*>(_space.vcpu_aware())->dbg_id();
261 fast_return_to_user(vcpu->_entry_ip, sp, vcpu_state().usr().get());
265 vcpu->state = vcpu->_saved_state;
266 Task *target_space = nonull_static_cast<Task*>(space());
267 bool user_mode = false;
269 if (vcpu->state & Vcpu_state::F_user_mode)
271 if (!vcpu_user_space())
272 return commit_result(-L4_err::EInval);
276 if (!(vcpu->state & Vcpu_state::F_fpu_enabled))
278 state_add_dirty(Thread_vcpu_fpu_disabled);
282 state_del_dirty(Thread_vcpu_fpu_disabled);
284 target_space = static_cast<Task*>(vcpu_user_space());
287 LOG_TRACE("VCPU events", "vcpu", this, __context_vcpu_log_fmt,
288 Vcpu_log *l = tbe->payload<Vcpu_log>();
290 l->state = vcpu->state;
291 l->ip = vcpu->_ts.ip();
292 l->sp = vcpu->_ts.sp();
293 l->space = target_space->dbg_id();
296 return commit_result(target_space->resume_vcpu(this, vcpu, user_mode));
299 PRIVATE inline NOEXPORT NEEDS["processor.h"]
301 Thread_object::sys_modify_senders(L4_msg_tag tag, Utcb const *in, Utcb * /*out*/)
303 if (sender_list()->cursor())
304 return Kobject_iface::commit_result(-L4_err::EBusy);
307 printf("MODIFY ID (%08lx:%08lx->%08lx:%08lx\n",
308 in->values[1], in->values[2],
309 in->values[3], in->values[4]);
312 int elems = tag.words();
315 return Kobject_iface::commit_result(0);
321 ::Prio_list_elem *c = sender_list()->head();
324 // this is kind of arbitrary
325 for (int cnt = 50; c && cnt > 0; --cnt)
327 Sender *s = Sender::cast(c);
328 s->modify_label(&in->values[1], elems);
333 return Kobject_iface::commit_result(0);
335 sender_list()->cursor(c);
336 Proc::preemption_point();
337 c = sender_list()->cursor();
339 return Kobject_iface::commit_result(0);
342 PRIVATE inline NOEXPORT
344 Thread_object::sys_register_delete_irq(L4_msg_tag tag, Utcb const *in, Utcb * /*out*/)
346 L4_snd_item_iter snd_items(in, tag.words());
348 if (!tag.items() || !snd_items.next())
349 return Kobject_iface::commit_result(-L4_err::EInval);
351 L4_fpage bind_irq(snd_items.get()->d);
352 if (EXPECT_FALSE(!bind_irq.is_objpage()))
353 return Kobject_iface::commit_error(in, L4_error::Overflow);
355 register Context *const c_thread = ::current();
356 register Space *const c_space = c_thread->space();
357 register Obj_space *const o_space = c_space->obj_space();
358 unsigned char irq_rights = 0;
360 = Irq_base::dcast(o_space->lookup_local(bind_irq.obj_index(), &irq_rights));
363 return Kobject_iface::commit_result(-L4_err::EInval);
365 if (EXPECT_FALSE(!(irq_rights & L4_fpage::X)))
366 return Kobject_iface::commit_result(-L4_err::EPerm);
368 register_delete_irq(irq);
369 return Kobject_iface::commit_result(0);
373 PRIVATE inline NOEXPORT
375 Thread_object::sys_control(unsigned char rights, L4_msg_tag const &tag, Utcb *utcb)
377 if (EXPECT_FALSE(!(rights & L4_fpage::W)))
378 return commit_result(-L4_err::EPerm);
380 if (EXPECT_FALSE(tag.words() < 6))
381 return commit_result(-L4_err::EInval);
383 Context *curr = current();
384 Obj_space *s = curr->space()->obj_space();
385 L4_snd_item_iter snd_items(utcb, tag.words());
387 User<Utcb>::Ptr utcb_addr(0);
389 Mword flags = utcb->values[0];
391 Mword _old_pager = _pager.raw() << L4_obj_ref::Cap_shift;
392 Mword _old_exc_handler = _exc_handler.raw() << L4_obj_ref::Cap_shift;
394 Thread_ptr _new_pager(~0UL);
395 Thread_ptr _new_exc_handler(~0UL);
397 if (flags & Ctl_set_pager)
398 _new_pager = Thread_ptr(utcb->values[1] >> L4_obj_ref::Cap_shift);
400 if (flags & Ctl_set_exc_handler)
401 _new_exc_handler = Thread_ptr(utcb->values[2] >> L4_obj_ref::Cap_shift);
403 if (flags & Ctl_bind_task)
405 if (EXPECT_FALSE(!tag.items() || !snd_items.next()))
406 return commit_result(-L4_err::EInval);
408 L4_fpage bind_task(snd_items.get()->d);
410 if (EXPECT_FALSE(!bind_task.is_objpage()))
411 return commit_result(-L4_err::EInval);
413 unsigned char task_rights = 0;
414 task = Kobject::dcast<Task*>(s->lookup_local(bind_task.obj_index(), &task_rights));
416 if (EXPECT_FALSE(!(task_rights & L4_fpage::W)))
417 return commit_result(-L4_err::EPerm);
420 return commit_result(-L4_err::EInval);
422 utcb_addr = User<Utcb>::Ptr((Utcb*)utcb->values[5]);
424 if (EXPECT_FALSE(!bind(task, utcb_addr)))
425 return commit_result(-L4_err::EInval); // unbind first !!
431 long res = control(_new_pager, _new_exc_handler);
434 return commit_result(res);
436 if ((res = sys_control_arch(utcb)) < 0)
437 return commit_result(res);
439 // FIXME: must be done xcpu safe, may be some parts above too
440 if (flags & Ctl_alien_thread)
442 if (utcb->values[4] & Ctl_alien_thread)
444 add_state |= Thread_alien;
445 del_state |= Thread_dis_alien;
448 del_state |= Thread_alien;
451 if (del_state || add_state)
452 drq_state_change(~del_state, add_state);
454 utcb->values[1] = _old_pager;
455 utcb->values[2] = _old_exc_handler;
457 return commit_result(0, 3);
461 PRIVATE inline NOEXPORT
463 Thread_object::sys_vcpu_control(unsigned char, L4_msg_tag const &tag,
467 return commit_result(-L4_err::EInval);
469 User<Vcpu_state>::Ptr vcpu(0);
471 if (tag.words() >= 2)
472 vcpu = User<Vcpu_state>::Ptr((Vcpu_state*)utcb->values[1]);
479 Mword size = sizeof(Vcpu_state);
480 if (utcb->values[0] & 0x10000)
482 size = Config::PAGE_SIZE;
483 add_state |= Thread_ext_vcpu_enabled;
486 Space::Ku_mem const *vcpu_m
487 = space()->find_ku_mem(vcpu, size);
490 return commit_result(-L4_err::EInval);
492 add_state |= Thread_vcpu_enabled;
493 _vcpu_state.set(vcpu, vcpu_m->kern_addr(vcpu));
496 return commit_result(-L4_err::EInval);
498 /* hm, we do not allow to disable vCPU mode, it's one way enable
501 del_state |= Thread_vcpu_enabled | Thread_vcpu_user_mode
502 | Thread_vcpu_fpu_disabled | Thread_ext_vcpu_enabled;
506 drq_state_change(~del_state, add_state);
508 return commit_result(0);
512 // -------------------------------------------------------------------
513 // Thread::ex_regs class system calls
517 Thread_object::ex_regs(Address ip, Address sp,
518 Address *o_ip = 0, Address *o_sp = 0, Mword *o_flags = 0,
521 if (state(false) == Thread_invalid || !space())
524 if (current() == this)
527 if (o_sp) *o_sp = user_sp();
528 if (o_ip) *o_ip = user_ip();
529 if (o_flags) *o_flags = user_flags();
531 // Changing the run state is only possible when the thread is not in
533 if (!(ops & Exr_cancel) && (state() & Thread_in_exception))
534 // XXX Maybe we should return false here. Previously, we actually
535 // did so, but we also actually didn't do any state modification.
536 // If you change this value, make sure the logic in
537 // sys_thread_ex_regs still works (in particular,
538 // ex_regs_cap_handler and friends should still be called).
541 if (state() & Thread_dead) // resurrect thread
542 state_change_dirty(~Thread_dead, Thread_ready);
544 else if (ops & Exr_cancel)
545 // cancel ongoing IPC or other activity
546 state_add_dirty(Thread_cancel | Thread_ready);
548 if (ops & Exr_trigger_exception)
550 extern char leave_by_trigger_exception[];
551 do_trigger_exception(regs(), leave_by_trigger_exception);
560 if (current() == this)
568 Thread_object::ex_regs(Utcb *utcb)
570 Address ip = utcb->values[1];
571 Address sp = utcb->values[2];
573 Mword ops = utcb->values[0];
575 LOG_TRACE("Ex-regs", "exr", current(), __fmt_thread_exregs,
576 Log_thread_exregs *l = tbe->payload<Log_thread_exregs>();
578 l->ip = ip; l->sp = sp; l->op = ops;);
580 if (!ex_regs(ip, sp, &ip, &sp, &flags, ops))
581 return commit_result(-L4_err::EInval);
583 utcb->values[0] = flags;
584 utcb->values[1] = ip;
585 utcb->values[2] = sp;
587 return commit_result(0, 3);
592 Thread_object::handle_remote_ex_regs(Drq *, Context *self, void *p)
594 Remote_syscall *params = reinterpret_cast<Remote_syscall*>(p);
595 params->result = nonull_static_cast<Thread_object*>(self)->ex_regs(params->thread->utcb().access());
596 return params->result.proto() == 0 ? Drq::Need_resched : 0;
599 PRIVATE inline NOEXPORT
601 Thread_object::sys_ex_regs(L4_msg_tag const &tag, Utcb * /*utcb*/)
603 if (tag.words() != 3)
604 return commit_result(-L4_err::EInval);
606 Remote_syscall params;
607 params.thread = current_thread();
609 drq(handle_remote_ex_regs, ¶ms, 0, Drq::Any_ctxt);
610 return params.result;
613 PRIVATE inline NOEXPORT NEEDS["timer.h"]
615 Thread_object::sys_thread_switch(L4_msg_tag const &/*tag*/, Utcb *utcb)
617 Context *curr = current();
620 return commit_result(0);
622 if (current_cpu() != cpu())
623 return commit_result(0);
626 Sched_context * const cs = current_sched();
630 && ((state() & (Thread_ready | Thread_suspended)) == Thread_ready))
632 curr->switch_exec_schedule_locked (this, Not_Helping);
633 reinterpret_cast<Utcb::Time_val*>(utcb->values)->t = 0; // Assume timeslice was used up
634 return commit_result(0, Utcb::Time_val::Words);
637 #if 0 // FIXME: provide API for multiple sched contexts
638 // Compute remaining quantum length of timeslice
639 regs->left(timeslice_timeout.cpu(cpu())->get_timeout(Timer::system_clock()));
641 // Yield current global timeslice
642 cs->owner()->switch_sched(cs->id() ? cs->next() : cs);
644 reinterpret_cast<Utcb::Time_val*>(utcb->values)->t
645 = timeslice_timeout.cpu(current_cpu())->get_timeout(Timer::system_clock());
648 return commit_result(0, Utcb::Time_val::Words);
653 // -------------------------------------------------------------------
654 // Gather statistics information about thread execution
658 Thread_object::sys_thread_stats_remote(void *data)
660 update_consumed_time();
661 *(Clock::Time *)data = consumed_time();
667 Thread_object::handle_sys_thread_stats_remote(Drq *, Context *self, void *data)
669 return nonull_static_cast<Thread_object*>(self)->sys_thread_stats_remote(data);
672 PRIVATE inline NOEXPORT
674 Thread_object::sys_thread_stats(L4_msg_tag const &/*tag*/, Utcb *utcb)
678 if (cpu() != current_cpu())
679 drq(handle_sys_thread_stats_remote, &value, 0, Drq::Any_ctxt);
682 // Respect the fact that the consumed time is only updated on context switch
683 if (this == current())
684 update_consumed_time();
685 value = consumed_time();
688 reinterpret_cast<Utcb::Time_val *>(utcb->values)->t = value;
690 return commit_result(0, Utcb::Time_val::Words);