4 #include "kobject_helper.h"
6 class Factory : public Ram_quota, public Kobject_h<Factory>
11 typedef slab_cache_anon Self_alloc;
14 //---------------------------------------------------------------------------
18 #include "kmem_slab.h"
20 #include "thread_object.h"
21 #include "static_init.h"
22 #include "l4_buf_iter.h"
24 #include "u_semaphore.h"
28 #include "entry_frame.h"
30 static Factory _root_factory INIT_PRIORITY(ROOT_FACTORY_INIT_PRIO);
31 FIASCO_DEFINE_KOBJ(Factory);
39 Factory::Factory(Ram_quota *q, unsigned long max)
44 static Kmem_slab_t<Factory> _factory_allocator("Factory");
49 { return &_factory_allocator; }
54 { return static_cast<Factory*>(Ram_quota::root); }
59 Factory::create_factory(unsigned long max)
62 if (alloc(sizeof(Factory) + max) && (nq = allocator()->alloc()))
63 return new (nq) Factory(this, max);
69 void Factory::operator delete (void *_f)
71 Factory *f = (Factory*)_f;
72 LOG_TRACE("Factory delete", "fa del", ::current(), 0, {});
77 Ram_quota *p = f->parent();
79 p->free(sizeof(Factory) + f->limit());
86 Factory::map_obj(Kobject_iface *o, Mword cap, Space *c_space,
91 case 0: return commit_result(-L4_err::ENomem);
92 case -L4_err::EInval: return commit_result(-L4_err::EInval);
93 case -L4_err::EPerm: return commit_result(-L4_err::EPerm);
99 if (!map(o, o_space, c_space, cap, rl.list()))
102 return commit_result(-L4_err::ENomem);
105 // return a tag with one typed item for the returned capability
106 return commit_result(0, 0, 1);
111 PRIVATE inline NOEXPORT
113 Factory::new_factory(Utcb const *u)
115 // XXX: should check for type tag in new call
116 return create_factory(u->values[2]);
120 PRIVATE inline NOEXPORT
122 Factory::new_task(Utcb const *u)
124 L4_fpage utcb_area(0);
125 // XXX: should check for type tag in new call
126 utcb_area = L4_fpage(u->values[2]);
128 Task *new_t = Task::create(Space::Default_factory(), this, utcb_area);
133 if (!new_t->valid() || !new_t->initialize())
142 PRIVATE inline NOEXPORT
144 Factory::new_thread(Utcb const *)
146 Thread_object *t = new (this) Thread_object();
150 PRIVATE inline NOEXPORT
152 Factory::new_gate(L4_msg_tag const &tag, Utcb const *utcb, Obj_space *o_space)
154 L4_snd_item_iter snd_items(utcb, tag.words());
157 if (tag.items() && snd_items.next())
159 L4_fpage bind_thread(snd_items.get()->d);
160 if (EXPECT_FALSE(!bind_thread.is_objpage()))
161 return reinterpret_cast<Kobject_iface*>(-L4_err::EInval);
163 unsigned char thread_rights = 0;
164 thread = Kobject::dcast<Thread_object*>(o_space->lookup_local(bind_thread.obj_index(), &thread_rights));
166 if (EXPECT_FALSE(!(thread_rights & L4_fpage::W)))
167 return reinterpret_cast<Kobject_iface*>(-L4_err::EPerm);
171 return reinterpret_cast<Kobject_iface*>(-L4_err::EInval);
173 // should check type tag of varg
174 return static_cast<Ipc_gate_ctl*>(Ipc_gate::create(this, thread, utcb->values[2]));
178 PRIVATE inline NOEXPORT
180 Factory::new_semaphore(Utcb const *)
182 return U_semaphore::alloc(this);
185 PRIVATE inline NOEXPORT
187 Factory::new_irq(unsigned w, Utcb const *utcb)
189 if (w >= 3 && utcb->values[2])
190 return Irq::allocate<Irq_muxer>(this);
192 return Irq::allocate<Irq_sender>(this);
197 Factory::kinvoke(L4_obj_ref ref, Mword rights, Syscall_frame *f,
198 Utcb const *utcb, Utcb *)
200 register Context *const c_thread = ::current();
201 register Space *const c_space = c_thread->space();
202 register Obj_space *const o_space = c_space->obj_space();
204 if (EXPECT_FALSE(f->tag().proto() != L4_msg_tag::Label_factory))
205 return commit_result(-L4_err::EBadproto);
207 if (EXPECT_FALSE(!(rights & L4_fpage::W)))
208 return commit_result(-L4_err::EPerm);
210 if (EXPECT_FALSE(!ref.have_recv()))
211 return commit_result(0);
213 if (f->tag().words() < 1)
214 return commit_result(-L4_err::EInval);
219 L4_buf_iter buf(utcb, utcb->buf_desc.obj());
220 L4_buf_iter::Item const *const b = buf.get();
221 if (EXPECT_FALSE(b->b.is_void()
222 || b->b.type() != L4_msg_item::Map))
223 return commit_error(utcb, L4_error(L4_error::Overflow, L4_error::Rcv));
225 buffer = L4_fpage(b->d);
228 if (EXPECT_FALSE(!buffer.is_objpage()))
229 return commit_error(utcb, L4_error(L4_error::Overflow, L4_error::Rcv));
231 Kobject_iface *new_o;
233 switch ((long)utcb->values[0])
235 case 0: // new IPC Gate
236 new_o = new_gate(f->tag(), utcb, o_space);
239 case L4_msg_tag::Label_factory:
240 new_o = new_factory(utcb);
243 case L4_msg_tag::Label_task:
244 new_o = new_task(utcb);
247 case L4_msg_tag::Label_thread:
248 new_o = new_thread(utcb);
251 case L4_msg_tag::Label_semaphore:
252 new_o = new_semaphore(utcb);
255 case L4_msg_tag::Label_irq:
256 new_o = new_irq(f->tag().words(), utcb);
259 case L4_msg_tag::Label_vm:
260 new_o = new_vm(utcb);
264 return commit_result(-L4_err::ENodev);
267 LOG_TRACE("Kobject create", "new", ::current(), __factory_log_fmt,
268 Log_entry *le = tbe->payload<Log_entry>();
269 le->op = utcb->values[0];
270 le->buffer = buffer.obj_index();
271 le->id = dbg_info()->dbg_id();
273 le->newo = new_o ? new_o->dbg_info()->dbg_id() : ~0);
275 return map_obj(new_o, buffer.obj_index(), c_space, o_space);
280 //----------------------------------------------------------------------------
281 IMPLEMENTATION [svm || vmx]:
283 #include "vm_factory.h"
285 PRIVATE inline NOEXPORT
287 Factory::new_vm(Utcb const *)
289 Vm *new_t = Vm_factory::create(this);
294 if (!new_t->valid() || !new_t->initialize())
307 PRIVATE inline NOEXPORT
309 Factory::new_vm(Utcb const *)
311 return Vm::create(this);
314 //----------------------------------------------------------------------------
315 IMPLEMENTATION [!svm && !tz && !vmx]:
317 PRIVATE inline NOEXPORT
319 Factory::new_vm(Utcb const *)
320 { return (Kobject_iface*)(-L4_err::EInval); }
322 // ------------------------------------------------------------------------
325 #include "tb_entry.h"
327 EXTENSION class Factory
338 static unsigned log_fmt(Tb_entry *, int, char *) asm ("__factory_log_fmt");
341 // ------------------------------------------------------------------------
342 IMPLEMENTATION [debug]:
346 Factory::log_fmt(Tb_entry *e, int maxlen, char *buf)
348 static char const *const ops[] =
349 { /* 0 */ "gate", "irq", 0, 0, 0, 0, 0, 0,
350 /* -8 */ 0, 0, 0, "task", "thread", 0, 0, "factory",
351 /* -16 */ "vm", 0, 0, 0, "sem" };
352 Log_entry *le = e->payload<Log_entry>();
353 char const *op = -le->op <= (int)(sizeof(ops)/sizeof(ops[0]))
354 ? ops[-le->op] : "invalid op";
358 return snprintf(buf, maxlen, "factory=%lx [%s] new=%lx cap=[C:%lx] ram=%lx", le->id, op, le->newo, le->buffer, le->ram);