]> rtime.felk.cvut.cz Git - l4.git/blob - kernel/fiasco/src/kern/factory.cpp
update
[l4.git] / kernel / fiasco / src / kern / factory.cpp
1 INTERFACE:
2
3 #include "ram_quota.h"
4 #include "kobject_helper.h"
5
6 class Factory : public Ram_quota, public Kobject_h<Factory>
7 {
8   FIASCO_DECLARE_KOBJ();
9
10 private:
11   typedef slab_cache_anon Self_alloc;
12 };
13
14 //---------------------------------------------------------------------------
15 IMPLEMENTATION:
16
17 #include "ipc_gate.h"
18 #include "kmem_slab.h"
19 #include "task.h"
20 #include "thread_object.h"
21 #include "static_init.h"
22 #include "l4_buf_iter.h"
23 #include "l4_types.h"
24 #include "u_semaphore.h"
25 #include "irq.h"
26 #include "map_util.h"
27 #include "logdefs.h"
28 #include "entry_frame.h"
29
30 static Factory _root_factory INIT_PRIORITY(ROOT_FACTORY_INIT_PRIO);
31 FIASCO_DEFINE_KOBJ(Factory);
32
33 PUBLIC inline
34 Factory::Factory()
35   : Ram_quota()
36 {}
37
38 PRIVATE inline
39 Factory::Factory(Ram_quota *q, unsigned long max)
40   : Ram_quota(q, max)
41 {}
42
43
44 static Kmem_slab_t<Factory> _factory_allocator("Factory");
45
46 PRIVATE static
47 Factory::Self_alloc *
48 Factory::allocator()
49 { return &_factory_allocator; }
50
51 PUBLIC static inline
52 Factory *
53 Factory::root()
54 { return static_cast<Factory*>(Ram_quota::root); }
55
56
57 PRIVATE
58 Factory *
59 Factory::create_factory(unsigned long max)
60 {
61   void *nq;
62   if (alloc(sizeof(Factory) + max) && (nq = allocator()->alloc()))
63     return new (nq) Factory(this, max);
64
65   return 0;
66 }
67
68 PUBLIC
69 void Factory::operator delete (void *_f)
70 {
71   Factory *f = (Factory*)_f;
72   LOG_TRACE("Factory delete", "fa del", ::current(), 0, {});
73
74   if (!f->parent())
75     return;
76
77   Ram_quota *p = f->parent();
78   if (p)
79     p->free(sizeof(Factory) + f->limit());
80
81   allocator()->free(f);
82 }
83
84 PRIVATE
85 L4_msg_tag
86 Factory::map_obj(Kobject_iface *o, Mword cap, Space *c_space,
87                  Obj_space *o_space)
88 {
89   switch (Mword(o))
90     {
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);
94     default:              break;
95     };
96
97   Reap_list rl;
98
99   if (!map(o, o_space, c_space, cap, rl.list()))
100     {
101       delete o;
102       return commit_result(-L4_err::ENomem);
103     }
104
105   // return a tag with one typed item for the returned capability
106   return commit_result(0, 0, 1);
107 }
108
109
110
111 PRIVATE inline NOEXPORT
112 Kobject_iface *
113 Factory::new_factory(Utcb const *u)
114 {
115   // XXX: should check for type tag in new call
116   return create_factory(u->values[2]);
117 }
118
119
120 PRIVATE inline NOEXPORT
121 Kobject_iface *
122 Factory::new_task(Utcb const *u)
123 {
124   L4_fpage utcb_area(0);
125   // XXX: should check for type tag in new call
126   utcb_area = L4_fpage(u->values[2]);
127
128   Task *new_t = Task::create(Space::Default_factory(), this, utcb_area);
129
130   if (!new_t)
131     return 0;
132
133   if (!new_t->valid() || !new_t->initialize())
134     {
135       delete new_t;
136       return 0;
137     }
138
139   return new_t;
140 }
141
142 PRIVATE inline NOEXPORT
143 Kobject_iface *
144 Factory::new_thread(Utcb const *)
145 {
146   Thread_object *t = new (this) Thread_object();
147   return t;
148 }
149
150 PRIVATE inline NOEXPORT
151 Kobject_iface *
152 Factory::new_gate(L4_msg_tag const &tag, Utcb const *utcb, Obj_space *o_space)
153 {
154   L4_snd_item_iter snd_items(utcb, tag.words());
155   Thread *thread = 0;
156
157   if (tag.items() && snd_items.next())
158     {
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);
162
163       unsigned char thread_rights = 0;
164       thread = Kobject::dcast<Thread_object*>(o_space->lookup_local(bind_thread.obj_index(), &thread_rights));
165
166       if (EXPECT_FALSE(!(thread_rights & L4_fpage::W)))
167         return reinterpret_cast<Kobject_iface*>(-L4_err::EPerm);
168     }
169 #if 0
170   if (!thread)
171     return reinterpret_cast<Kobject_iface*>(-L4_err::EInval);
172 #endif
173   // should check type tag of varg
174   return static_cast<Ipc_gate_ctl*>(Ipc_gate::create(this, thread, utcb->values[2]));
175 }
176
177
178 PRIVATE inline NOEXPORT
179 Kobject_iface *
180 Factory::new_semaphore(Utcb const *)
181 {
182   return U_semaphore::alloc(this);
183 }
184
185 PRIVATE inline NOEXPORT
186 Kobject_iface *
187 Factory::new_irq(unsigned w, Utcb const *utcb)
188 {
189   if (w >= 3 && utcb->values[2])
190     return Irq::allocate<Irq_muxer>(this);
191   else
192     return Irq::allocate<Irq_sender>(this);
193 }
194
195 PUBLIC
196 L4_msg_tag
197 Factory::kinvoke(L4_obj_ref ref, Mword rights, Syscall_frame *f,
198                  Utcb const *utcb, Utcb *)
199 {
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();
203
204   if (EXPECT_FALSE(f->tag().proto() != L4_msg_tag::Label_factory))
205     return commit_result(-L4_err::EBadproto);
206
207   if (EXPECT_FALSE(!(rights & L4_fpage::W)))
208     return commit_result(-L4_err::EPerm);
209
210   if (EXPECT_FALSE(!ref.have_recv()))
211     return commit_result(0);
212
213   if (f->tag().words() < 1)
214     return commit_result(-L4_err::EInval);
215
216   L4_fpage buffer(0);
217
218     {
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));
224
225       buffer = L4_fpage(b->d);
226     }
227
228   if (EXPECT_FALSE(!buffer.is_objpage()))
229     return commit_error(utcb, L4_error(L4_error::Overflow, L4_error::Rcv));
230
231   Kobject_iface *new_o;
232
233   switch ((long)utcb->values[0])
234     {
235     case 0:  // new IPC Gate
236       new_o = new_gate(f->tag(), utcb, o_space);
237       break;
238
239     case L4_msg_tag::Label_factory:
240       new_o = new_factory(utcb);
241       break;
242
243     case L4_msg_tag::Label_task:
244       new_o =  new_task(utcb);
245       break;
246
247     case L4_msg_tag::Label_thread:
248       new_o = new_thread(utcb);
249       break;
250
251     case L4_msg_tag::Label_semaphore:
252       new_o = new_semaphore(utcb);
253       break;
254
255     case L4_msg_tag::Label_irq:
256       new_o = new_irq(f->tag().words(), utcb);
257       break;
258
259     case L4_msg_tag::Label_vm:
260       new_o = new_vm(utcb);
261       break;
262
263     default:
264       return commit_result(-L4_err::ENodev);
265     }
266
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();
272     le->ram = current();
273     le->newo = new_o ? new_o->dbg_info()->dbg_id() : ~0);
274
275   return map_obj(new_o, buffer.obj_index(), c_space, o_space);
276
277 }
278
279
280 //----------------------------------------------------------------------------
281 IMPLEMENTATION [svm || vmx]:
282
283 #include "vm_factory.h"
284
285 PRIVATE inline NOEXPORT
286 Kobject_iface *
287 Factory::new_vm(Utcb const *)
288 {
289   Vm *new_t = Vm_factory::create(this);
290
291   if (!new_t)
292     return 0;
293
294   if (!new_t->valid() || !new_t->initialize())
295     {
296       delete new_t;
297       return 0;
298     }
299
300   return new_t;
301 }
302
303 IMPLEMENTATION [tz]:
304
305 #include "vm.h"
306
307 PRIVATE inline NOEXPORT
308 Kobject_iface *
309 Factory::new_vm(Utcb const *)
310 {
311   return Vm::create(this);
312 }
313
314 //----------------------------------------------------------------------------
315 IMPLEMENTATION [!svm && !tz && !vmx]:
316
317 PRIVATE inline NOEXPORT
318 Kobject_iface *
319 Factory::new_vm(Utcb const *)
320 { return (Kobject_iface*)(-L4_err::EInval); }
321
322 // ------------------------------------------------------------------------
323 INTERFACE [debug]:
324
325 #include "tb_entry.h"
326
327 EXTENSION class Factory
328 {
329 private:
330   struct Log_entry
331   {
332     Smword op;
333     Mword buffer;
334     Mword id;
335     Mword ram;
336     Mword newo;
337   };
338   static unsigned log_fmt(Tb_entry *, int, char *) asm ("__factory_log_fmt");
339 };
340
341 // ------------------------------------------------------------------------
342 IMPLEMENTATION [debug]:
343
344 IMPLEMENT
345 unsigned
346 Factory::log_fmt(Tb_entry *e, int maxlen, char *buf)
347 {
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";
355   if (!op)
356     op = "(nan)";
357
358   return snprintf(buf, maxlen, "factory=%lx [%s] new=%lx cap=[C:%lx] ram=%lx", le->id, op, le->newo, le->buffer, le->ram);
359 }