4 #include "kobject_helper.h"
6 #include "slab_cache_anon.h"
7 #include "thread_object.h"
13 class Ipc_gate_ctl : public Kobject_h<Ipc_gate_ctl, Kobject_iface>
23 class Ipc_gate : public Kobject
25 friend class Ipc_gate_ctl;
28 Ref_ptr<Thread> _thread;
31 Locked_prio_list _wait_q;
34 class Ipc_gate_obj : public Ipc_gate, public Ipc_gate_ctl
36 FIASCO_DECLARE_KOBJ();
39 friend class Ipc_gate;
40 typedef slab_cache_anon Self_alloc;
43 bool put() { return Ipc_gate::put(); }
45 Thread *thread() const { return _thread.ptr(); }
46 Mword id() const { return _id; }
47 Mword obj_id() const { return _id; }
48 bool is_local(Space *s) const { return _thread->space() == s; }
51 //---------------------------------------------------------------------------
54 EXTENSION class Ipc_gate
57 struct Log_ipc_gate_invoke
64 static unsigned log_fmt(Tb_entry *, int max, char *buf) asm ("__fmt_ipc_gate_invoke");
67 //---------------------------------------------------------------------------
72 #include "entry_frame.h"
73 #include "ipc_timeout.h"
74 #include "kmem_slab.h"
76 #include "ram_quota.h"
77 #include "static_init.h"
79 #include "thread_state.h"
82 FIASCO_DEFINE_KOBJ(Ipc_gate_obj);
86 Ipc_gate_obj::map_root()
87 { return Ipc_gate::map_root(); }
91 Ipc_gate_obj::downgrade(unsigned long attr)
93 if (attr & L4_fpage::C_obj_right_1)
94 return static_cast<Ipc_gate*>(this);
96 return static_cast<Ipc_gate_ctl*>(this);
100 Ipc_gate::Ipc_gate(Ram_quota *q, Thread *t, Mword id)
101 : _thread(t), _id(id), _quota(q), _wait_q()
105 Ipc_gate_obj::Ipc_gate_obj(Ram_quota *q, Thread *t, Mword id)
111 Ipc_gate_obj::unblock_all()
113 while (::Prio_list_elem *h = _wait_q.head())
115 Lock_guard<Cpu_lock> g1(&cpu_lock);
118 Lock_guard<typeof(_wait_q)> g2(&_wait_q);
119 if (EXPECT_FALSE(h != _wait_q.head()))
122 w = static_cast<Thread*>(Sender::cast(h));
123 w->sender_dequeue(&_wait_q);
131 Ipc_gate_obj::initiate_deletion(Kobject ***r)
134 _thread->ipc_gate_deleted(_id);
136 Kobject::initiate_deletion(r);
141 Ipc_gate_obj::destroy(Kobject ***r)
149 Ipc_gate_obj::~Ipc_gate_obj()
154 PUBLIC inline NEEDS[<cstddef>]
156 Ipc_gate_obj::operator new (size_t, void *b)
159 static Kmem_slab_t<Ipc_gate_obj> _ipc_gate_allocator("Ipc_gate");
162 Ipc_gate_obj::Self_alloc *
163 Ipc_gate_obj::allocator()
164 { return &_ipc_gate_allocator; }
168 Ipc_gate::create(Ram_quota *q, Thread *t, Mword id)
171 if (q->alloc(sizeof(Ipc_gate_obj)))
173 if (nq = Ipc_gate_obj::allocator()->alloc())
174 return new (nq) Ipc_gate_obj(q, t, id);
176 q->free(sizeof(Ipc_gate_obj));
183 void Ipc_gate_obj::operator delete (void *_f)
185 register Ipc_gate_obj *f = (Ipc_gate_obj*)_f;
186 Ram_quota *p = f->_quota;
189 p->free(sizeof(Ipc_gate_obj));
191 allocator()->free(f);
194 PRIVATE inline NOEXPORT
196 Ipc_gate_ctl::bind_thread(L4_obj_ref, Mword, Syscall_frame *f, Utcb const *in, Utcb *)
198 L4_msg_tag tag = f->tag();
199 L4_snd_item_iter snd_items(in, tag.words());
201 if (tag.words() < 2 || !tag.items() || !snd_items.next())
202 return commit_result(-L4_err::EInval);
204 L4_fpage bind_thread(snd_items.get()->d);
205 if (EXPECT_FALSE(!bind_thread.is_objpage()))
206 return commit_error(in, L4_error::Overflow);
208 register Context *const c_thread = ::current();
209 register Space *const c_space = c_thread->space();
210 register Obj_space *const o_space = c_space->obj_space();
211 unsigned char t_rights = 0;
212 Thread *t = Kobject::dcast<Thread_object*>(o_space->lookup_local(bind_thread.obj_index(), &t_rights));
214 if (!(t_rights & L4_fpage::CS))
215 return commit_result(-L4_err::EPerm);
218 Ipc_gate_obj *g = static_cast<Ipc_gate_obj*>(this);
219 g->_id = in->values[1];
224 c_thread->rcu_wait();
227 return commit_result(0);
230 PRIVATE inline NOEXPORT
232 Ipc_gate_ctl::get_infos(L4_obj_ref, Mword, Syscall_frame *, Utcb const *, Utcb *out)
234 Ipc_gate_obj *g = static_cast<Ipc_gate_obj*>(this);
235 out->values[0] = g->_id;
236 return commit_result(0, 1);
241 Ipc_gate_ctl::invoke(L4_obj_ref self, Mword rights, Syscall_frame *f, Utcb *utcb)
243 if (f->tag().proto() == L4_msg_tag::Label_kobject)
244 Kobject_h<Ipc_gate_ctl, Kobject_iface>::invoke(self, rights, f, utcb);
246 static_cast<Ipc_gate_obj*>(this)->Ipc_gate::invoke(self, rights, f, utcb);
252 Ipc_gate_ctl::kinvoke(L4_obj_ref self, Mword rights, Syscall_frame *f, Utcb const *in, Utcb *out)
254 L4_msg_tag tag = f->tag();
256 if (EXPECT_FALSE(tag.proto() != L4_msg_tag::Label_kobject))
257 return commit_result(-L4_err::EBadproto);
259 if (EXPECT_FALSE(tag.words() < 1))
260 return commit_result(-L4_err::EInval);
262 switch (in->values[0])
265 return bind_thread(self, rights, f, in, out);
267 return get_infos(self, rights, f, in, out);
269 return static_cast<Ipc_gate_obj*>(this)->kobject_invoke(self, rights, f, in, out);
273 PRIVATE inline NOEXPORT
275 Ipc_gate::block(Thread *ct, L4_timeout const &to, Utcb *u)
280 t = to.microsecs(Timer::system_clock(), u);
282 return L4_error::Timeout;
286 Lock_guard<typeof(_wait_q)> g(&_wait_q);
287 ct->wait_queue(&_wait_q);
288 ct->sender_enqueue(&_wait_q, ct->sched_context()->prio());
290 ct->state_change_dirty(~Thread_ready, Thread_send_wait);
295 timeout.set(t, ct->cpu());
296 ct->set_timeout(&timeout);
301 ct->state_change(~Thread_ipc_mask, Thread_ready);
304 if (EXPECT_FALSE(ct->in_sender_list() && timeout.has_hit()))
306 Lock_guard<typeof(_wait_q)> g(&_wait_q);
307 if (!ct->in_sender_list())
308 return L4_error::None;
310 ct->sender_dequeue(&_wait_q);
311 return L4_error::Timeout;
313 return L4_error::None;
319 Ipc_gate::invoke(L4_obj_ref /*self*/, Mword rights, Syscall_frame *f, Utcb *utcb)
321 Syscall_frame *ipc_f = f;
322 //LOG_MSG_3VAL(current(), "gIPC", Mword(_thread), _id, f->obj_2_flags());
323 //printf("Invoke: Ipc_gate(%lx->%p)...\n", _id, _thread);
324 Thread *ct = current_thread();
327 bool have_rcv = false;
329 if (EXPECT_FALSE(!_thread.ptr()))
331 L4_error e = block(ct, f->timeout().snd, utcb);
334 f->tag(commit_error(utcb, e));
338 if (EXPECT_FALSE(!_thread.ptr()))
340 f->tag(commit_error(utcb, L4_error::Not_existent));
345 bool ipc = _thread->check_sys_ipc(f->ref().flags(), &partner, &sender, &have_rcv);
347 LOG_TRACE("IPC Gate invoke", "gate", current(), __fmt_ipc_gate_invoke,
348 Log_ipc_gate_invoke *l = tbe->payload<Log_ipc_gate_invoke>();
349 l->gate_dbg_id = dbg_id();
350 l->thread_dbg_id = _thread->dbg_id();
351 l->label = _id | rights;
354 if (EXPECT_FALSE(!ipc))
355 f->tag(commit_error(utcb, L4_error::Not_existent));
358 ipc_f->from(_id | rights);
359 ct->do_ipc(f->tag(), partner, partner, have_rcv, sender,
360 f->timeout(), f, rights);
364 //---------------------------------------------------------------------------
365 IMPLEMENTATION [debug]:
369 Ipc_gate_obj::dbg_info() const
370 { return Ipc_gate::dbg_info(); }
374 Ipc_gate::log_fmt(Tb_entry *e, int max, char *buf)
376 Log_ipc_gate_invoke *l = e->payload<Log_ipc_gate_invoke>();
377 return snprintf(buf, max, "D-gate=%lx D-thread=%lx L=%lx",
378 l->gate_dbg_id, l->thread_dbg_id, l->label);