4 #include "kobject_helper.h"
6 #include "slab_cache.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;
26 friend class Jdb_sender_list;
29 Ref_ptr<Thread> _thread;
32 Locked_prio_list _wait_q;
35 class Ipc_gate_obj : public Ipc_gate, public Ipc_gate_ctl
37 FIASCO_DECLARE_KOBJ();
40 friend class Ipc_gate;
41 typedef Slab_cache Self_alloc;
44 bool put() { return Ipc_gate::put(); }
46 Thread *thread() const { return _thread.ptr(); }
47 Mword id() const { return _id; }
48 Mword obj_id() const { return _id; }
49 bool is_local(Space *s) const { return _thread && _thread->space() == s; }
52 //---------------------------------------------------------------------------
57 EXTENSION class Ipc_gate
60 struct Log_ipc_gate_invoke : public Tb_entry
65 unsigned print(int max, char *buf) const;
70 //---------------------------------------------------------------------------
75 #include "assert_opt.h"
76 #include "entry_frame.h"
77 #include "ipc_timeout.h"
78 #include "kmem_slab.h"
80 #include "ram_quota.h"
81 #include "static_init.h"
83 #include "thread_state.h"
86 FIASCO_DEFINE_KOBJ(Ipc_gate_obj);
90 Ipc_gate_obj::map_root()
91 { return Ipc_gate::map_root(); }
95 Ipc_gate_obj::downgrade(unsigned long attr)
97 if (attr & L4_msg_item::C_obj_right_1)
98 return static_cast<Ipc_gate*>(this);
100 return static_cast<Ipc_gate_ctl*>(this);
104 Ipc_gate::Ipc_gate(Ram_quota *q, Thread *t, Mword id)
105 : _thread(t), _id(id), _quota(q), _wait_q()
109 Ipc_gate_obj::Ipc_gate_obj(Ram_quota *q, Thread *t, Mword id)
115 Ipc_gate_obj::unblock_all()
117 while (::Prio_list_elem *h = _wait_q.first())
119 auto g1 = lock_guard(cpu_lock);
122 auto g2 = lock_guard(_wait_q.lock());
123 if (EXPECT_FALSE(h != _wait_q.first()))
126 w = static_cast<Thread*>(Sender::cast(h));
127 w->sender_dequeue(&_wait_q);
135 Ipc_gate_obj::initiate_deletion(Kobject ***r)
138 _thread->ipc_gate_deleted(_id);
140 Kobject::initiate_deletion(r);
145 Ipc_gate_obj::destroy(Kobject ***r)
153 Ipc_gate_obj::~Ipc_gate_obj()
158 PUBLIC inline NEEDS[<cstddef>]
160 Ipc_gate_obj::operator new (size_t, void *b) throw()
163 static Kmem_slab_t<Ipc_gate_obj> _ipc_gate_allocator("Ipc_gate");
166 Ipc_gate_obj::Self_alloc *
167 Ipc_gate_obj::allocator()
168 { return &_ipc_gate_allocator; }
172 Ipc_gate::create(Ram_quota *q, Thread *t, Mword id)
174 Auto_quota<Ram_quota> quota(q, sizeof(Ipc_gate_obj));
176 if (EXPECT_FALSE(!quota))
179 void *nq = Ipc_gate_obj::allocator()->alloc();
180 if (EXPECT_FALSE(!nq))
184 return new (nq) Ipc_gate_obj(q, t, id);
188 void Ipc_gate_obj::operator delete (void *_f)
190 register Ipc_gate_obj *f = (Ipc_gate_obj*)_f;
191 Ram_quota *p = f->_quota;
193 allocator()->free(f);
195 p->free(sizeof(Ipc_gate_obj));
198 PRIVATE inline NOEXPORT NEEDS["assert_opt.h"]
200 Ipc_gate_ctl::bind_thread(L4_obj_ref, Mword, Syscall_frame *f, Utcb const *in, Utcb *)
202 L4_msg_tag tag = f->tag();
203 L4_snd_item_iter snd_items(in, tag.words());
205 if (tag.words() < 2 || !tag.items() || !snd_items.next())
206 return commit_result(-L4_err::EInval);
208 L4_fpage bind_thread(snd_items.get()->d);
209 if (EXPECT_FALSE(!bind_thread.is_objpage()))
210 return commit_error(in, L4_error::Overflow);
212 register Context *const c_thread = ::current();
213 assert_opt(c_thread);
214 register Space *const c_space = c_thread->space();
215 assert_opt (c_space);
216 unsigned char t_rights = 0;
217 Thread *t = Kobject::dcast<Thread_object*>(c_space->lookup_local(bind_thread.obj_index(), &t_rights));
219 if (!(t_rights & L4_fpage::CS))
220 return commit_result(-L4_err::EPerm);
223 Ipc_gate_obj *g = static_cast<Ipc_gate_obj*>(this);
224 g->_id = in->values[1];
229 c_thread->rcu_wait();
232 return commit_result(0);
235 PRIVATE inline NOEXPORT
237 Ipc_gate_ctl::get_infos(L4_obj_ref, Mword, Syscall_frame *, Utcb const *, Utcb *out)
239 Ipc_gate_obj *g = static_cast<Ipc_gate_obj*>(this);
240 out->values[0] = g->_id;
241 return commit_result(0, 1);
246 Ipc_gate_ctl::invoke(L4_obj_ref self, Mword rights, Syscall_frame *f, Utcb *utcb)
248 if (f->tag().proto() == L4_msg_tag::Label_kobject)
249 Kobject_h<Ipc_gate_ctl, Kobject_iface>::invoke(self, rights, f, utcb);
251 static_cast<Ipc_gate_obj*>(this)->Ipc_gate::invoke(self, rights, f, utcb);
257 Ipc_gate_ctl::kinvoke(L4_obj_ref self, Mword rights, Syscall_frame *f, Utcb const *in, Utcb *out)
259 L4_msg_tag tag = f->tag();
261 if (EXPECT_FALSE(tag.proto() != L4_msg_tag::Label_kobject))
262 return commit_result(-L4_err::EBadproto);
264 if (EXPECT_FALSE(tag.words() < 1))
265 return commit_result(-L4_err::EInval);
267 switch (in->values[0])
270 return bind_thread(self, rights, f, in, out);
272 return get_infos(self, rights, f, in, out);
274 return static_cast<Ipc_gate_obj*>(this)->kobject_invoke(self, rights, f, in, out);
278 PRIVATE inline NOEXPORT
280 Ipc_gate::block(Thread *ct, L4_timeout const &to, Utcb *u)
285 t = to.microsecs(Timer::system_clock(), u);
287 return L4_error::Timeout;
291 auto g = lock_guard(_wait_q.lock());
292 ct->set_wait_queue(&_wait_q);
293 ct->sender_enqueue(&_wait_q, ct->sched_context()->prio());
295 ct->state_change_dirty(~Thread_ready, Thread_send_wait);
300 timeout.set(t, ct->cpu(true));
301 ct->set_timeout(&timeout);
306 ct->state_change(~Thread_ipc_mask, Thread_ready);
309 if (EXPECT_FALSE(ct->in_sender_list() && timeout.has_hit()))
311 auto g = lock_guard(_wait_q.lock());
312 if (!ct->in_sender_list())
313 return L4_error::None;
315 ct->sender_dequeue(&_wait_q);
316 return L4_error::Timeout;
318 return L4_error::None;
324 Ipc_gate::invoke(L4_obj_ref /*self*/, Mword rights, Syscall_frame *f, Utcb *utcb)
326 Syscall_frame *ipc_f = f;
327 //LOG_MSG_3VAL(current(), "gIPC", Mword(_thread), _id, f->obj_2_flags());
328 //printf("Invoke: Ipc_gate(%lx->%p)...\n", _id, _thread);
329 Thread *ct = current_thread();
332 bool have_rcv = false;
334 if (EXPECT_FALSE(!_thread.ptr()))
336 L4_error e = block(ct, f->timeout().snd, utcb);
339 f->tag(commit_error(utcb, e));
343 if (EXPECT_FALSE(!_thread.ptr()))
345 f->tag(commit_error(utcb, L4_error::Not_existent));
350 bool ipc = _thread->check_sys_ipc(f->ref().op(), &partner, &sender, &have_rcv);
352 LOG_TRACE("IPC Gate invoke", "gate", current(), Log_ipc_gate_invoke,
353 l->gate_dbg_id = dbg_id();
354 l->thread_dbg_id = _thread->dbg_id();
355 l->label = _id | rights;
358 if (EXPECT_FALSE(!ipc))
359 f->tag(commit_error(utcb, L4_error::Not_existent));
362 ipc_f->from(_id | rights);
363 ct->do_ipc(f->tag(), partner, partner, have_rcv, sender,
364 f->timeout(), f, rights);
368 //---------------------------------------------------------------------------
369 IMPLEMENTATION [debug]:
373 Ipc_gate_obj::dbg_info() const
374 { return Ipc_gate::dbg_info(); }
378 Ipc_gate::Log_ipc_gate_invoke::print(int max, char *buf) const
380 return snprintf(buf, max, "D-gate=%lx D-thread=%lx L=%lx",
381 gate_dbg_id, thread_dbg_id, label);