4 #include "kobject_helper.h"
6 class Factory : public Ram_quota, public Kobject_h<Factory>, public Kobject
11 typedef slab_cache_anon Self_alloc;
14 //---------------------------------------------------------------------------
18 #include "kmem_slab.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)
43 PRIVATE static inline NOEXPORT NEEDS["kmem_slab.h"]
47 static Self_alloc* slabs =
48 new Kmem_slab_simple (sizeof (Factory), sizeof (Mword), "Factory");
56 { return static_cast<Factory*>(Ram_quota::root); }
61 Factory::create_factory(unsigned long max)
64 if (alloc(sizeof(Factory) + max) && (nq = allocator()->alloc()))
65 return new (nq) Factory(this, max);
71 void Factory::operator delete (void *_f)
73 Factory *f = (Factory*)_f;
74 LOG_TRACE("Factory delete", "fa del", ::current(), 0, {});
79 Ram_quota *p = f->parent();
81 p->free(sizeof(Factory) + f->limit());
88 Factory::map_obj(Kobject_iface *o, Mword cap, Space *c_space,
93 case 0: return commit_result(-L4_err::ENomem);
94 case -L4_err::EInval: return commit_result(-L4_err::EInval);
95 case -L4_err::EPerm: return commit_result(-L4_err::EPerm);
101 if (!map(o, o_space, c_space, cap, rl.list()))
104 // FIXME: reap stuff if needed
105 return commit_result(-L4_err::ENomem);
108 // FIXME: reap stuff if needed
109 return commit_result(0, 0, 1);
114 PRIVATE inline NOEXPORT
116 Factory::new_factory(Utcb const *u)
118 // XXX: should check for type tag in new call
119 return create_factory(u->values[2]);
123 PRIVATE inline NOEXPORT
125 Factory::new_task(Utcb const *u)
127 L4_fpage utcb_area(0);
128 // XXX: should check for type tag in new call
129 utcb_area = L4_fpage(u->values[2]);
131 Task *new_t = Task::create(Space::Default_factory(), this, utcb_area);
136 if (new_t->state() != Task::Ready || !new_t->initialize())
145 PRIVATE inline NOEXPORT
147 Factory::new_thread(Utcb const *)
149 Thread *t = new (this) Thread();
153 PRIVATE inline NOEXPORT
155 Factory::new_gate(L4_msg_tag const &tag, Utcb const *utcb, Obj_space *o_space)
157 L4_snd_item_iter snd_items(utcb, tag.words());
160 if (tag.items() && snd_items.next())
162 L4_fpage bind_thread(snd_items.get()->d);
163 if (EXPECT_FALSE(!bind_thread.is_objpage()))
164 return reinterpret_cast<Kobject_iface*>(-L4_err::EInval);
166 unsigned char thread_rights = 0;
167 thread = Kobject::dcast<Thread*>(o_space->lookup_local(bind_thread.obj_index(), &thread_rights));
169 if (EXPECT_FALSE(!(thread_rights & L4_fpage::W)))
170 return reinterpret_cast<Kobject_iface*>(-L4_err::EPerm);
174 return reinterpret_cast<Kobject_iface*>(-L4_err::EInval);
176 // should check type tag of varg
177 return static_cast<Ipc_gate_ctl*>(Ipc_gate::create(this, thread, utcb->values[2]));
181 PRIVATE inline NOEXPORT
183 Factory::new_semaphore(Utcb const *)
185 return U_semaphore::alloc(this);
188 PRIVATE inline NOEXPORT
190 Factory::new_irq(Utcb const *)
192 Irq *i = Irq::allocate(this);
199 Factory::kinvoke(L4_obj_ref ref, Mword rights, Syscall_frame *f,
200 Utcb const *utcb, Utcb *)
202 register Context *const c_thread = ::current();
203 register Space *const c_space = c_thread->space();
204 register Obj_space *const o_space = c_space->obj_space();
206 if (EXPECT_FALSE(f->tag().proto() != L4_msg_tag::Label_factory))
207 return commit_result(-L4_err::EBadproto);
209 if (EXPECT_FALSE(!(rights & L4_fpage::W)))
210 return commit_result(-L4_err::EPerm);
212 if (EXPECT_FALSE(!ref.have_recv()))
213 return commit_result(0);
215 if (f->tag().words() < 1)
216 return commit_result(-L4_err::EInval);
221 L4_buf_iter buf(utcb, utcb->buf_desc.obj());
222 L4_buf_iter::Item const *const b = buf.get();
223 if (EXPECT_FALSE(b->b.is_void()
224 || b->b.type() != L4_msg_item::Map))
225 return commit_error(utcb, L4_error(L4_error::Overflow, L4_error::Rcv));
227 buffer = L4_fpage(b->d);
230 if (EXPECT_FALSE(!buffer.is_objpage()))
231 return commit_error(utcb, L4_error(L4_error::Overflow, L4_error::Rcv));
233 Kobject_iface *new_o;
235 switch ((long)utcb->values[0])
237 case 0: // new IPC Gate
238 new_o = new_gate(f->tag(), utcb, o_space);
241 case L4_msg_tag::Label_factory:
242 new_o = new_factory(utcb);
245 case L4_msg_tag::Label_task:
246 new_o = new_task(utcb);
249 case L4_msg_tag::Label_thread:
250 new_o = new_thread(utcb);
253 case L4_msg_tag::Label_semaphore:
254 new_o = new_semaphore(utcb);
257 case L4_msg_tag::Label_irq:
258 new_o = new_irq(utcb);
261 case L4_msg_tag::Label_vm:
262 new_o = new_vm(utcb);
266 return commit_result(-L4_err::ENodev);
269 LOG_TRACE("Kobject create", "new", ::current(), __factory_log_fmt,
270 Log_entry *le = tbe->payload<Log_entry>();
271 le->op = utcb->values[0];
272 le->buffer = buffer.obj_index();
275 le->newo = new_o ? new_o->dbg_info()->dbg_id() : ~0);
277 return map_obj(new_o, buffer.obj_index(), c_space, o_space);
282 //----------------------------------------------------------------------------
283 IMPLEMENTATION [svm]:
285 #include "vm_factory.h"
287 PRIVATE inline NOEXPORT
289 Factory::new_vm(Utcb const *)
291 Vm *new_t = Vm_factory::create(this);
296 if (new_t->state() != Task::Ready || !new_t->initialize())
309 PRIVATE inline NOEXPORT
311 Factory::new_vm(Utcb const *)
313 return Vm::create(this);
316 //----------------------------------------------------------------------------
317 IMPLEMENTATION [!svm && !tz]:
319 PRIVATE inline NOEXPORT
321 Factory::new_vm(Utcb const *)
322 { return (Kobject_iface*)(-L4_err::EInval); }
324 // ------------------------------------------------------------------------
327 #include "tb_entry.h"
329 EXTENSION class Factory
340 static unsigned log_fmt(Tb_entry *, int, char *) asm ("__factory_log_fmt");
343 // ------------------------------------------------------------------------
344 IMPLEMENTATION [debug]:
348 Factory::log_fmt(Tb_entry *e, int maxlen, char *buf)
350 static char const *const ops[] =
351 { /* 0 */ "gate", "irq", 0, 0, 0, 0, 0, 0,
352 /* -8 */ 0, 0, 0, "task", "thread", 0, 0, "factory",
353 /* -16 */ "vm", 0, 0, 0, "sem" };
354 Log_entry *le = e->payload<Log_entry>();
355 char const *op = -le->op <= (int)(sizeof(ops)/sizeof(ops[0]))
356 ? ops[-le->op] : "invalid op";
360 return snprintf(buf, maxlen, "factory=%lx [%s] new=%lx cap=[C:%lx] ram=%lx", le->id, op, le->newo, le->buffer, le->ram);